diff --git a/src/main/java/org/apache/commons/math/stat/descriptive/DescriptiveStatistics.java b/src/main/java/org/apache/commons/math/stat/descriptive/DescriptiveStatistics.java index 40f539c8c..c32463944 100644 --- a/src/main/java/org/apache/commons/math/stat/descriptive/DescriptiveStatistics.java +++ b/src/main/java/org/apache/commons/math/stat/descriptive/DescriptiveStatistics.java @@ -123,6 +123,20 @@ public class DescriptiveStatistics implements StatisticalSummary, Serializable { setWindowSize(window); } + /** + * Construct a DescriptiveStatistics instance with an infinite window + * and the initial data values in double[] initialDoubleArray. + * If initialDoubleArray is null, then this constructor corresponds to + * DescriptiveStatistics() + * + * @param initialDoubleArray the initial double[]. + */ + public DescriptiveStatistics(double[] initialDoubleArray) { + if (initialDoubleArray != null) { + eDA = new ResizableDoubleArray(initialDoubleArray); + } + } + /** * Copy constructor. Construct a new DescriptiveStatistics instance that * is a copy of original. diff --git a/src/main/java/org/apache/commons/math/util/DoubleArray.java b/src/main/java/org/apache/commons/math/util/DoubleArray.java index 03c90957b..0bc0a652a 100644 --- a/src/main/java/org/apache/commons/math/util/DoubleArray.java +++ b/src/main/java/org/apache/commons/math/util/DoubleArray.java @@ -67,6 +67,13 @@ public interface DoubleArray { */ void addElement(double value); + /** + * Adds elements to the end of this expandable array + * + * @param values to be added to end of array + */ + void addElements(double[] values); + /** *

* Adds an element to the end of the array and removes the first diff --git a/src/main/java/org/apache/commons/math/util/ResizableDoubleArray.java b/src/main/java/org/apache/commons/math/util/ResizableDoubleArray.java index f55ffb790..08acfc81d 100644 --- a/src/main/java/org/apache/commons/math/util/ResizableDoubleArray.java +++ b/src/main/java/org/apache/commons/math/util/ResizableDoubleArray.java @@ -160,6 +160,29 @@ public class ResizableDoubleArray implements DoubleArray, Serializable { internalArray = new double[this.initialCapacity]; } + /** + * Create a ResizableArray from an existing double[] with the + * initial capacity and numElements corresponding to the size of + * the supplied double[] array. If the supplied array is null, a + * new empty array with the default initial capacity will be created. + * Other properties take default values: + *

+ */ + public ResizableDoubleArray(double[] initialArray) { + if (initialArray == null) { + internalArray = new double[initialCapacity]; + } else { + internalArray = initialArray; + initialCapacity = initialArray.length; + numElements = initialArray.length; + } + } + /** *

* Create a ResizableArray with the specified initial capacity @@ -274,6 +297,20 @@ public class ResizableDoubleArray implements DoubleArray, Serializable { contract(); } } + + /** + * Adds several element to the end of this expandable array. + * + * @param values to be added to end of array + */ + public synchronized void addElements(double[] values) { + final double[] tempArray = new double[numElements + values.length + 1]; + System.arraycopy(internalArray, startIndex, tempArray, 0, numElements); + System.arraycopy(values, 0, tempArray, numElements, values.length); + internalArray = tempArray; + startIndex = 0; + numElements += values.length; + } /** *

diff --git a/src/test/java/org/apache/commons/math/util/ResizableDoubleArrayTest.java b/src/test/java/org/apache/commons/math/util/ResizableDoubleArrayTest.java index b869028d5..40ff96692 100644 --- a/src/test/java/org/apache/commons/math/util/ResizableDoubleArrayTest.java +++ b/src/test/java/org/apache/commons/math/util/ResizableDoubleArrayTest.java @@ -59,6 +59,13 @@ public class ResizableDoubleArrayTest extends DoubleArrayAbstractTest { } catch (IllegalArgumentException ex) { // expected } + + testDa = new ResizableDoubleArray((double[]) null); + assertEquals(0, testDa.getNumElements()); + + double[] initialArray = new double[] { 0, 1, 2 }; + testDa = new ResizableDoubleArray(initialArray); + assertEquals(3, testDa.getNumElements()); testDa = new ResizableDoubleArray(2, 2.0f); assertEquals(0, testDa.getNumElements()); @@ -184,6 +191,33 @@ public class ResizableDoubleArrayTest extends DoubleArrayAbstractTest { "16 and an expansion factor of 2.0", 1024, ((ResizableDoubleArray) da).getInternalLength()); } + + public void testAddElements() { + ResizableDoubleArray testDa = new ResizableDoubleArray(); + + // MULTIPLICATIVE_MODE + testDa.addElements(new double[] {4, 5, 6}); + assertEquals(3, testDa.getNumElements(), 0); + assertEquals(4, testDa.getElement(0), 0); + assertEquals(5, testDa.getElement(1), 0); + assertEquals(6, testDa.getElement(2), 0); + + testDa.addElements(new double[] {4, 5, 6}); + assertEquals(6, testDa.getNumElements()); + + // ADDITIVE_MODE (x's are occupied storage locations, 0's are open) + testDa = new ResizableDoubleArray(2, 2.0f, 2.5f, + ResizableDoubleArray.ADDITIVE_MODE); + assertEquals(2, testDa.getInternalLength()); + testDa.addElements(new double[] { 1d }); // x,0 + testDa.addElements(new double[] { 2d }); // x,x + testDa.addElements(new double[] { 3d }); // x,x,x,0 -- expanded + assertEquals(1d, testDa.getElement(0), 0); + assertEquals(2d, testDa.getElement(1), 0); + assertEquals(3d, testDa.getElement(2), 0); + assertEquals(4, testDa.getInternalLength()); // x,x,x,0 + assertEquals(3, testDa.getNumElements()); + } @Override public void testAddElementRolling() {