Return-Path: X-Original-To: apmail-commons-commits-archive@minotaur.apache.org Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id F170D195D7 for ; Thu, 21 Apr 2016 23:14:57 +0000 (UTC) Received: (qmail 81828 invoked by uid 500); 21 Apr 2016 23:14:50 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 80897 invoked by uid 500); 21 Apr 2016 23:14:49 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 79553 invoked by uid 99); 21 Apr 2016 23:14:48 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 21 Apr 2016 23:14:48 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 2AB1AE02A2; Thu, 21 Apr 2016 23:14:48 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: erans@apache.org To: commits@commons.apache.org Date: Thu, 21 Apr 2016 23:15:23 -0000 Message-Id: <42161080de4f4a55a94ec4a5ea5e86c3@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [37/53] [abbrv] [math] MATH-1337 MATH-1337 Adaptor from new to old API. Class is deprecated: it is temporarily provided to allow testing of the new RNGs (in user applications) with minimal changes. Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/34515698 Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/34515698 Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/34515698 Branch: refs/heads/develop Commit: 34515698878ff0a4641e72486f0ae8a7e30df0d5 Parents: 6ddf476 Author: Gilles Authored: Mon Mar 21 00:20:50 2016 +0100 Committer: Gilles Committed: Mon Mar 21 00:20:50 2016 +0100 ---------------------------------------------------------------------- .../apache/commons/math4/random/RngAdaptor.java | 229 +++++++++++++++++++ 1 file changed, 229 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-math/blob/34515698/src/main/java/org/apache/commons/math4/random/RngAdaptor.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/random/RngAdaptor.java b/src/main/java/org/apache/commons/math4/random/RngAdaptor.java new file mode 100644 index 0000000..d132c95 --- /dev/null +++ b/src/main/java/org/apache/commons/math4/random/RngAdaptor.java @@ -0,0 +1,229 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.math4.random; + +import java.io.Serializable; +import java.io.IOException; +import java.io.ObjectOutputStream; +import java.io.ObjectInputStream; +import org.apache.commons.math4.util.FastMath; +import org.apache.commons.math4.rng.UniformRandomProvider; +import org.apache.commons.math4.rng.RandomSource; + +/** + * Adaptor that delegates to a {@link UniformRandomProvider} instance. + *

+ * It is provided for users who wish to test the new RNG implementations + * the current generators (up to version 3.6 of Commons Math) in + * codes that require the {@code RandomGenerator} interface. + *

+ *

+ * Applications should upgrade to use the new generators ASAP. + * If problems are found that jeopardize the upgrade, please report them + * on the project's + * + * issue tracking system. + *

+ * + *

+ * Notes: + *

    + *
  • + * The + * {@link RandomGenerator#setSeed(int) setSeed(int)}, + * {@link RandomGenerator#setSeed(int[]) setSeed(int[])} and + * {@link RandomGenerator#setSeed(long) setSeed(long)} + * methods of the {@link RandomGenerator} are not part of the + * {@link UniformRandomProvider new API}. + *
  • + *
  • + * The new RNG implementations are not {@code Serializable}. + * Use {@link RandomSource#saveState(UniformRandomProvider)} + * instead. + *
  • + *
  • + * {@link RandomGenerator#nextGaussian() nextGaussian()} is not + * part of the {@link UniformRandomProvider new API} as it defines + * a "post-processing" of the output of a uniform RNG in + * order to follow a different distribution. + *
  • + *

    + * + * @since 4.0 + * + * @deprecated As of 4.0. This class is made available for testing + * the {@link RandomSource new RNG implementations} in existing + * applications. + * It will be removed in the next major release. + */ +@Deprecated +public final class RngAdaptor + implements RandomGenerator, + Serializable { + /** Serializable version identifier. */ + private static final long serialVersionUID = 12345L; + /** Source. */ + private final RandomSource source; + /** Delegate. */ + private transient UniformRandomProvider delegate; + /** Next gaussian. */ + private double nextGaussian = Double.NaN; + + /** + * Creates a new instance. + * + * @param source Source of randomness. + */ + public RngAdaptor(RandomSource source) { + this(source, null); + } + + /** + * Creates a new instance. + * + * @param source Source of randomness. + * @param seed Seed. Can be {@code null}. + */ + public RngAdaptor(RandomSource source, + Object seed) { + this.source = source; + delegate = RandomSource.create(source, seed); + } + + /** {@inheritDoc} */ + @Override + public void setSeed(int seed) { + delegate = RandomSource.create(source, seed); + clear(); + } + + /** {@inheritDoc} */ + @Override + public void setSeed(int[] seed) { + delegate = RandomSource.create(source, seed); + clear(); + } + + /** {@inheritDoc} */ + @Override + public void setSeed(long seed) { + delegate = RandomSource.create(source, seed); + clear(); + } + + /** {@inheritDoc} */ + @Override + public boolean nextBoolean() { + return delegate.nextBoolean(); + } + + /** {@inheritDoc} */ + @Override + public void nextBytes(byte[] bytes) { + delegate.nextBytes(bytes); + } + + /** {@inheritDoc} */ + @Override + public double nextDouble() { + return delegate.nextDouble(); + } + + /** {@inheritDoc} */ + @Override + public float nextFloat() { + return delegate.nextFloat(); + } + + /** {@inheritDoc} */ + @Override + public double nextGaussian() { + final double random; + if (Double.isNaN(nextGaussian)) { + // generate a new pair of gaussian numbers + final double x = nextDouble(); + final double y = nextDouble(); + final double alpha = 2 * FastMath.PI * x; + final double r = FastMath.sqrt(-2 * FastMath.log(y)); + random = r * FastMath.cos(alpha); + nextGaussian = r * FastMath.sin(alpha); + } else { + // use the second element of the pair already generated + random = nextGaussian; + nextGaussian = Double.NaN; + } + + return random; + } + + /** {@inheritDoc} */ + @Override + public int nextInt() { + return delegate.nextInt(); + } + + /** {@inheritDoc} */ + @Override + public int nextInt(int n) { + return delegate.nextInt(n); + } + + /** {@inheritDoc} */ + @Override + public long nextLong() { + return delegate.nextLong(); + } + + /** + * Clears the cache used by the default implementation of + * {@link #nextGaussian}. + */ + private void clear() { + nextGaussian = Double.NaN; + } + + /** + * @param out Output stream. + * @throws IOException if an error occurs. + */ + private void writeObject(ObjectOutputStream out) + throws IOException { + // Write non-transient fields. + out.defaultWriteObject(); + + // Save current state. + out.writeObject(RandomSource.saveState(delegate)); + } + + /** + * @param in Input stream. + * @throws IOException if an error occurs. + * @throws ClassNotFoundException if an error occurs. + */ + private void readObject(ObjectInputStream in) + throws IOException, + ClassNotFoundException { + // Read non-transient fields. + in.defaultReadObject(); + + // Recreate the "delegate" from serialized info. + delegate = RandomSource.create(source); + // And restore its state. + final RandomSource.State state = (RandomSource.State) in.readObject(); + RandomSource.restoreState(delegate, state); + } +}