diff --git a/pom.xml b/pom.xml index 3a754a20c..e2e44a3eb 100644 --- a/pom.xml +++ b/pom.xml @@ -299,6 +299,9 @@ Sébastien Riou + + Karl Richter + Benedikt Ritter diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 9e4f57965..aa5227f92 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! + + "DescriptiveStatistics": allow "Double[]" as initializer. + "PolynomialSplineFunction": incorrect usage of exception. 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 70fbcd965..2c555c1ab 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 @@ -142,6 +142,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}. 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 d5a4ae42d..3b6fca080 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 /**