You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2012/12/14 15:57:59 UTC

svn commit: r1421910 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math3/distribution/ main/java/org/apache/commons/math3/random/ test/java/org/apache/commons/math3/random/

Author: erans
Date: Fri Dec 14 14:57:56 2012
New Revision: 1421910

URL: http://svn.apache.org/viewvc?rev=1421910&view=rev
Log:
MATH-915
Restore backwards-compatibility (for release 3.1).

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/AbstractRealDistribution.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/random/EmpiricalDistribution.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/random/EmpiricalDistributionTest.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/AbstractRealDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/AbstractRealDistribution.java?rev=1421910&r1=1421909&r2=1421910&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/AbstractRealDistribution.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/AbstractRealDistribution.java Fri Dec 14 14:57:56 2012
@@ -48,7 +48,7 @@ implements RealDistribution, Serializabl
       * {@link #random} instance variable instead.
       */
     @Deprecated
-    protected final RandomDataImpl randomData = new RandomDataImpl();
+    protected RandomDataImpl randomData = new RandomDataImpl();
     /** RNG instance used to generate samples from the distribution. */
     protected final RandomGenerator random;
     /** Solver absolute accuracy for inverse cumulative computation */

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/random/EmpiricalDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/random/EmpiricalDistribution.java?rev=1421910&r1=1421909&r2=1421910&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/random/EmpiricalDistribution.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/random/EmpiricalDistribution.java Fri Dec 14 14:57:56 2012
@@ -29,8 +29,8 @@ import java.util.ArrayList;
 import java.util.List;
 
 import org.apache.commons.math3.distribution.AbstractRealDistribution;
-import org.apache.commons.math3.distribution.NormalDistribution;
 import org.apache.commons.math3.distribution.RealDistribution;
+import org.apache.commons.math3.distribution.NormalDistribution;
 import org.apache.commons.math3.exception.MathIllegalStateException;
 import org.apache.commons.math3.exception.MathInternalError;
 import org.apache.commons.math3.exception.NullArgumentException;
@@ -134,14 +134,18 @@ public class EmpiricalDistribution exten
     /** upper bounds of subintervals in (0,1) "belonging" to the bins */
     private double[] upperBounds = null;
 
-    /** RandomDataImpl instance to use in repeated calls to getNext() */
-    private final RandomDataGenerator randomData;
+    /** Data generator. */
+    private final RandomDataGenerator randomDataGen;
+    /**
+     * XXX Enable backward-compatibility (to be removed in 4.0).
+     */
+    private final boolean useRandomDataImpl;
 
     /**
      * Creates a new EmpiricalDistribution with the default bin count.
      */
     public EmpiricalDistribution() {
-        this(DEFAULT_BIN_COUNT, new RandomDataGenerator());
+        this(DEFAULT_BIN_COUNT);
     }
 
     /**
@@ -150,7 +154,7 @@ public class EmpiricalDistribution exten
      * @param binCount number of bins
      */
     public EmpiricalDistribution(int binCount) {
-        this(binCount, new RandomDataGenerator());
+        this(binCount, (RandomGenerator) null);
     }
 
     /**
@@ -158,54 +162,82 @@ public class EmpiricalDistribution exten
      * provided {@link RandomGenerator} as the source of random data.
      *
      * @param binCount number of bins
-     * @param generator random data generator (may be null, resulting in default JDK generator)
-     * @since 3.0
+     * @param randomData random data generator (may be null, resulting in a default generator)
+     * @deprecated As of 3.1. To be removed in 4.0. Please use
+     * {@link #EmpiricalDistribution(int,RandomDataGenerator)} instead.
      */
-    public EmpiricalDistribution(int binCount, RandomGenerator generator) {
-        super(generator);
+    @Deprecated
+    public EmpiricalDistribution(int binCount, RandomDataImpl randomData) {
         this.binCount = binCount;
-        this.randomData = new RandomDataGenerator(generator);
-        this.binStats = new ArrayList<SummaryStatistics>();
+        this.randomData = randomData == null ?
+            new RandomDataImpl() :
+            randomData;
+        binStats = new ArrayList<SummaryStatistics>();
+        useRandomDataImpl = true;
+        randomDataGen = null;
     }
-
     /**
-     * Creates a new EmpiricalDistribution with default bin count using the
+     * Creates a new EmpiricalDistribution with the specified bin count using the
      * provided {@link RandomGenerator} as the source of random data.
      *
-     * @param generator random data generator (may be null, resulting in default JDK generator)
-     * @since 3.0
+     * @param randomData random data generator (may be null, resulting in a default generator)
+     * @deprecated As of 3.1. To be removed in 4.0. Please use
+     * {@link #EmpiricalDistribution(RandomDataGenerator)} instead.
      */
-    public EmpiricalDistribution(RandomGenerator generator) {
-        this(DEFAULT_BIN_COUNT, generator);
+    @Deprecated
+    public EmpiricalDistribution(RandomDataImpl randomData) {
+        this(DEFAULT_BIN_COUNT, randomData);
     }
 
     /**
      * Creates a new EmpiricalDistribution with the specified bin count using the
-     * provided {@link RandomDataImpl} instance as the source of random data.
+     * provided {@link RandomGenerator} as the source of random data.
      *
      * @param binCount number of bins
-     * @param randomData random data generator (may be null, resulting in default JDK generator)
-     * @since 3.0
+     * @param randomData random data generator (may be null, resulting in a default generator)
      */
-    @SuppressWarnings("deprecation")  // Superclass argumentless constructor is deprecated
     public EmpiricalDistribution(int binCount, RandomDataGenerator randomData) {
         this.binCount = binCount;
-        this.randomData = randomData;
+        this.randomDataGen = randomData == null ?
+            new RandomDataGenerator() :
+            randomData;
         binStats = new ArrayList<SummaryStatistics>();
+        useRandomDataImpl = false; // XXX Remove in 4.0
+    }
+    /**
+     * Creates a new EmpiricalDistribution with the specified bin count using the
+     * provided {@link RandomGenerator} as the source of random data.
+     *
+     * @param randomData random data generator (may be null, resulting in a default generator)
+     */
+    public EmpiricalDistribution(RandomDataGenerator randomData) {
+        this(DEFAULT_BIN_COUNT, randomData);
+    }
+
+    /**
+     * Creates a new EmpiricalDistribution with the specified bin count using the
+     * provided {@link RandomGenerator} as the source of random data.
+     *
+     * @param binCount number of bins
+     * @param generator random data generator (may be null, resulting in a default generator)
+     * @since 3.0
+     */
+    public EmpiricalDistribution(int binCount, RandomGenerator generator) {
+        this(binCount, new RandomDataGenerator(generator));
     }
 
     /**
      * Creates a new EmpiricalDistribution with default bin count using the
-     * provided {@link RandomDataImpl} as the source of random data.
+     * provided {@link RandomGenerator} as the source of random data.
      *
-     * @param randomData random data generator (may be null, resulting in default JDK generator)
+     * @param generator random data generator (may be null, resulting in default generator)
      * @since 3.0
      */
-    public EmpiricalDistribution(RandomDataGenerator randomData) {
-        this(DEFAULT_BIN_COUNT, randomData);
+    public EmpiricalDistribution(RandomGenerator generator) {
+        this(DEFAULT_BIN_COUNT, generator);
     }
 
-     /**
+    /**
      * Computes the empirical distribution from the provided
      * array of numbers.
      *
@@ -465,22 +497,41 @@ public class EmpiricalDistribution exten
             throw new MathIllegalStateException(LocalizedFormats.DISTRIBUTION_NOT_LOADED);
         }
 
-        // Start with a uniformly distributed random number in (0,1)
-        final double x = randomData.nextUniform(0,1);
-
-        // Use this to select the bin and generate a Gaussian within the bin
-        for (int i = 0; i < binCount; i++) {
-           if (x <= upperBounds[i]) {
-               SummaryStatistics stats = binStats.get(i);
-               if (stats.getN() > 0) {
-                   if (stats.getStandardDeviation() > 0) {  // more than one obs
-                       return randomData.nextGaussian(stats.getMean(),
-                                                      stats.getStandardDeviation());
-                   } else {
-                       return stats.getMean(); // only one obs in bin
-                   }
-               }
-           }
+        if (useRandomDataImpl) {
+            // XXX backward compatibility.
+            // Start with a uniformly distributed random number in (0, 1)
+            final double x = randomData.nextUniform(0,1);
+            // Use this to select the bin and generate a Gaussian within the bin
+            for (int i = 0; i < binCount; i++) {
+                if (x <= upperBounds[i]) {
+                    SummaryStatistics stats = binStats.get(i);
+                    if (stats.getN() > 0) {
+                        if (stats.getStandardDeviation() > 0) {  // more than one obs
+                            return randomData.nextGaussian(stats.getMean(),
+                                                           stats.getStandardDeviation());
+                        } else {
+                            return stats.getMean(); // only one obs in bin
+                        }
+                    }
+                }
+            }
+        } else {
+            // Start with a uniformly distributed random number in (0, 1)
+            final double x = randomDataGen.nextUniform(0, 1);
+            // Use this to select the bin and generate a Gaussian within the bin
+            for (int i = 0; i < binCount; i++) {
+                if (x <= upperBounds[i]) {
+                    SummaryStatistics stats = binStats.get(i);
+                    if (stats.getN() > 0) {
+                        if (stats.getStandardDeviation() > 0) {  // more than one obs
+                            return randomDataGen.nextGaussian(stats.getMean(),
+                                                              stats.getStandardDeviation());
+                        } else {
+                            return stats.getMean(); // only one obs in bin
+                        }
+                    }
+                }
+            }
         }
         throw new MathIllegalStateException(LocalizedFormats.NO_BIN_SELECTED);
     }
@@ -573,7 +624,12 @@ public class EmpiricalDistribution exten
      * @since 3.0
      */
     public void reSeed(long seed) {
-        randomData.reSeed(seed);
+        if (useRandomDataImpl) {
+            // XXX backward compatibility.
+            randomData.reSeed(seed);
+        } else {
+            randomDataGen.reSeed(seed);
+        }
     }
 
     // Distribution methods ---------------------------
@@ -763,7 +819,7 @@ public class EmpiricalDistribution exten
      */
     @Override
     public void reseedRandomGenerator(long seed) {
-        randomData.reSeed(seed);
+        reSeed(seed);
     }
 
     /**

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/random/EmpiricalDistributionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/random/EmpiricalDistributionTest.java?rev=1421910&r1=1421909&r2=1421910&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/random/EmpiricalDistributionTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/random/EmpiricalDistributionTest.java Fri Dec 14 14:57:56 2012
@@ -55,10 +55,12 @@ public final class EmpiricalDistribution
     public void setUp() {
         super.setUp();
         empiricalDistribution = new EmpiricalDistribution(100);
+//         empiricalDistribution = new EmpiricalDistribution(100, new RandomDataImpl()); // XXX Deprecated API
         url = getClass().getResource("testData.txt");
         final ArrayList<Double> list = new ArrayList<Double>();
         try {
             empiricalDistribution2 = new EmpiricalDistribution(100);
+//             empiricalDistribution2 = new EmpiricalDistribution(100, new RandomDataImpl()); // XXX Deprecated API
             BufferedReader in =
                 new BufferedReader(new InputStreamReader(
                         url.openStream()));