commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject [3/4] [math] MATH-1426: Add constructor that takes a "Double[]" argument.
Date Wed, 09 Aug 2017 12:38:48 GMT
MATH-1426: Add constructor that takes a "Double[]" argument.

Thanks to Karl Richter.


Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/bd82fe9f
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/bd82fe9f
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/bd82fe9f

Branch: refs/heads/master
Commit: bd82fe9fc034e7921eb589420bb4b86dc767a11e
Parents: 496934e
Author: Gilles <erans@apache.org>
Authored: Wed Aug 9 14:32:56 2017 +0200
Committer: Gilles <erans@apache.org>
Committed: Wed Aug 9 14:32:56 2017 +0200

----------------------------------------------------------------------
 pom.xml                                         |  3 +
 src/changes/changes.xml                         |  3 +
 .../stat/descriptive/DescriptiveStatistics.java | 17 ++++
 .../descriptive/DescriptiveStatisticsTest.java  | 93 ++++++++++++++++++++
 4 files changed, 116 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-math/blob/bd82fe9f/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index 3a754a2..e2e44a3 100644
--- a/pom.xml
+++ b/pom.xml
@@ -300,6 +300,9 @@
       <name>S&#233;bastien Riou</name>
     </contributor>
     <contributor>
+      <name>Karl Richter</name>
+    </contributor>
+    <contributor>
       <name>Benedikt Ritter</name>
     </contributor>
     <contributor>

http://git-wip-us.apache.org/repos/asf/commons-math/blob/bd82fe9f/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 9e4f579..aa5227f 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -54,6 +54,9 @@ If the output is not quite correct, check for invisible trailing spaces!
     </release>
 
     <release version="4.0" date="XXXX-XX-XX" description="">
+      <action dev="erans" type="add" issue="MATH-1426" due-to="Karl Richter">
+        "DescriptiveStatistics": allow "Double[]" as initializer.
+      </action>
       <action dev="erans" type="fix" issue="MATH-1419" due-to="Hang Park">
         "PolynomialSplineFunction": incorrect usage of exception.
       </action>

http://git-wip-us.apache.org/repos/asf/commons-math/blob/bd82fe9f/src/main/java/org/apache/commons/math4/stat/descriptive/DescriptiveStatistics.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math4/stat/descriptive/DescriptiveStatistics.java
b/src/main/java/org/apache/commons/math4/stat/descriptive/DescriptiveStatistics.java
index 70fbcd9..2c555c1 100644
--- a/src/main/java/org/apache/commons/math4/stat/descriptive/DescriptiveStatistics.java
+++ b/src/main/java/org/apache/commons/math4/stat/descriptive/DescriptiveStatistics.java
@@ -143,6 +143,23 @@ public class DescriptiveStatistics implements StatisticalSummary, Serializable
{
     }
 
     /**
+     * Construct a DescriptiveStatistics instance with an infinite window
+     * and the initial data values in {@code initialDoubleArray}.
+     * If {@code initialDoubleArray} is {@code null}, then this constructor
+     * corresponds to {@link #DescriptiveStatistics() }.
+     *
+     * @param initialDoubleArray the initial Double[].
+     */
+    public DescriptiveStatistics(Double[] initialDoubleArray) {
+        if (initialDoubleArray != null) {
+            eDA = new ResizableDoubleArray(initialDoubleArray.length);
+            for(double initialValue : initialDoubleArray) {
+                eDA.addElement(initialValue);
+            }
+        }
+    }
+
+    /**
      * Copy constructor. Construct a new {@code DescriptiveStatistics} instance
      * that is a copy of {@code original}.
      *

http://git-wip-us.apache.org/repos/asf/commons-math/blob/bd82fe9f/src/test/java/org/apache/commons/math4/stat/descriptive/DescriptiveStatisticsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math4/stat/descriptive/DescriptiveStatisticsTest.java
b/src/test/java/org/apache/commons/math4/stat/descriptive/DescriptiveStatisticsTest.java
index d5a4ae4..3b6fca0 100644
--- a/src/test/java/org/apache/commons/math4/stat/descriptive/DescriptiveStatisticsTest.java
+++ b/src/test/java/org/apache/commons/math4/stat/descriptive/DescriptiveStatisticsTest.java
@@ -17,7 +17,9 @@ import java.util.Locale;
 
 import org.apache.commons.math4.TestUtils;
 import org.apache.commons.math4.exception.MathIllegalArgumentException;
+import org.apache.commons.math4.exception.NullArgumentException;
 import org.apache.commons.math4.stat.descriptive.DescriptiveStatistics;
+import static org.apache.commons.math4.stat.descriptive.DescriptiveStatistics.copy;
 import org.apache.commons.math4.stat.descriptive.SummaryStatistics;
 import org.apache.commons.math4.stat.descriptive.UnivariateStatistic;
 import org.apache.commons.math4.stat.descriptive.moment.GeometricMean;
@@ -28,14 +30,19 @@ import org.apache.commons.math4.stat.descriptive.rank.Min;
 import org.apache.commons.math4.stat.descriptive.rank.Percentile;
 import org.apache.commons.math4.stat.descriptive.summary.Sum;
 import org.apache.commons.math4.stat.descriptive.summary.SumOfSquares;
+import org.apache.commons.math4.util.ResizableDoubleArray;
 import org.apache.commons.numbers.core.Precision;
+import org.apache.commons.rng.UniformRandomProvider;
+import org.apache.commons.rng.simple.RandomSource;
 import org.junit.Assert;
+import org.junit.BeforeClass;
 import org.junit.Test;
 
 /**
  * Test cases for the {@link DescriptiveStatistics} class.
  */
 public class DescriptiveStatisticsTest {
+    private static UniformRandomProvider random = RandomSource.create(RandomSource.WELL_1024_A,
2345789432894l);
 
     protected DescriptiveStatistics createDescriptiveStatistics() {
         return new DescriptiveStatistics();
@@ -292,6 +299,83 @@ public class DescriptiveStatisticsTest {
         Assert.assertTrue(iqr >= 0);
     }
 
+    @Test
+    public void testInit0() {
+        //test window constructor
+        int window = 1 + random.nextInt(Integer.MAX_VALUE-1);
+        DescriptiveStatistics instance = new DescriptiveStatistics(window);
+        Assert.assertEquals(window,
+                            instance.getWindowSize());
+    }
+
+    @Test
+    public void testInitDouble() {
+        //test double[] constructor
+        double[] initialDoubleArray = null;
+        new DescriptiveStatistics(initialDoubleArray);
+            //a null argument corresponds to DescriptiveStatistics(), so test
+            //that no exception is thrown
+        int initialDoubleArraySize = random.nextInt(1024 //some random
+            //memory consumption and test size limitation
+        );
+//        System.out.println(String.format("initialDoubleArraySize: %s",
+//                initialDoubleArraySize));
+        initialDoubleArray = new double[initialDoubleArraySize];
+        for(int i = 0; i < initialDoubleArraySize; i++) {
+            double value = random.nextDouble();
+            initialDoubleArray[i] = value;
+        }
+        new DescriptiveStatistics(initialDoubleArray);
+    }
+
+    @Test
+    public void testInitDoubleWrapper() {
+        //test Double[] constructor
+        Double[] initialDoubleWrapperArray = null;
+        new DescriptiveStatistics(initialDoubleWrapperArray);
+        int initialDoubleWrapperArraySize = random.nextInt(1024 //some random
+            //memory consumption and test size limitation
+        );
+        initialDoubleWrapperArray = generateInitialDoubleArray(initialDoubleWrapperArraySize);
+        new DescriptiveStatistics(initialDoubleWrapperArray);
+    }
+
+    @Test
+    public void testInitCopy() {
+        //test copy constructor
+        int initialDoubleArray = random.nextInt(1024 //some random
+            //memory consumption and test size limitation
+        );
+        DescriptiveStatistics original = new DescriptiveStatistics(initialDoubleArray);
+        DescriptiveStatistics instance = new DescriptiveStatistics(original);
+        Assert.assertEquals(original.getGeometricMean(),
+                            instance.getGeometricMean(),
+                            0);
+        Assert.assertEquals(original.getKurtosis(),
+                            instance.getKurtosis(),
+                            0);
+        Assert.assertEquals(original.getMax(),
+                            instance.getMax(),
+                            0);
+        Assert.assertEquals(original.getMean(),
+                            instance.getMean(),
+                            0);
+        Assert.assertEquals(original.getMin(),
+                            instance.getMin(),
+                            0);
+        Assert.assertEquals(original.getN(),
+                            instance.getN());
+        Assert.assertEquals(original.getSkewness(),
+                            instance.getSkewness(),
+                            0);
+        Assert.assertArrayEquals(original.getValues(),
+                                 instance.getValues(),
+                                 0);
+        Assert.assertEquals(original.getWindowSize(),
+                            instance.getWindowSize());
+            //doesn't implement equals
+    }
+
     public void checkremoval(DescriptiveStatistics dstat, int wsize,
                              double mean1, double mean2, double mean3) {
 
@@ -310,6 +394,15 @@ public class DescriptiveStatisticsTest {
 
     }
 
+    private Double[] generateInitialDoubleArray(int size) {
+        Double[] retValue = new Double[size];
+        for(int i = 0; i < size; i++) {
+            Double value = random.nextDouble();
+            retValue[i] = value;
+        }
+        return retValue;
+    }
+
     // Test UnivariateStatistics impls for setter injection tests
 
     /**


Mime
View raw message