Author: mikl Date: Tue Dec 28 09:03:40 2010 New Revision: 1053282 URL: http://svn.apache.org/viewvc?rev=1053282&view=rev Log: Fixes MATH-384
Modified: commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/stat/descriptive/DescriptiveStatistics.java commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/util/ResizableDoubleArray.java commons/proper/math/branches/MATH_2_X/src/test/java/org/apache/commons/math/util/ResizableDoubleArrayTest.java Modified: commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/stat/descriptive/DescriptiveStatistics.java URL: http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/stat/descriptive/DescriptiveStatistics.java?rev=1053282&r1=1053281&r2=1053282&view=diff ============================================================================== --- commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/stat/descriptive/DescriptiveStatistics.java (original) +++ commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/stat/descriptive/DescriptiveStatistics.java Tue Dec 28 09:03:40 2010 @@ -124,6 +124,20 @@ public class DescriptiveStatistics imple } /** + * 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. * Modified: commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/util/ResizableDoubleArray.java URL: http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/util/ResizableDoubleArray.java?rev=1053282&r1=1053281&r2=1053282&view=diff ============================================================================== --- commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/util/ResizableDoubleArray.java (original) +++ commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/util/ResizableDoubleArray.java Tue Dec 28 09:03:40 2010 @@ -161,6 +161,29 @@ public class ResizableDoubleArray implem } /** + * 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: + * <ul> + * <li><code>initialCapacity = 16</code></li> + * <li><code>expansionMode = MULTIPLICATIVE_MODE</code></li> + * <li><code>expansionFactor = 2.5</code></li> + * <li><code>contractionFactor = 2.0</code></li> + * </ul> + */ + public ResizableDoubleArray(double[] initialArray) { + if (initialArray == null) { + internalArray = new double[initialCapacity]; + } else { + internalArray = initialArray; + initialCapacity = initialArray.length; + numElements = initialArray.length; + } + } + + /** * <p> * Create a ResizableArray with the specified initial capacity * and expansion factor. The remaining properties take default @@ -274,6 +297,20 @@ public class ResizableDoubleArray implem 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; + } /** * <p> Modified: commons/proper/math/branches/MATH_2_X/src/test/java/org/apache/commons/math/util/ResizableDoubleArrayTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_X/src/test/java/org/apache/commons/math/util/ResizableDoubleArrayTest.java?rev=1053282&r1=1053281&r2=1053282&view=diff ============================================================================== --- commons/proper/math/branches/MATH_2_X/src/test/java/org/apache/commons/math/util/ResizableDoubleArrayTest.java (original) +++ commons/proper/math/branches/MATH_2_X/src/test/java/org/apache/commons/math/util/ResizableDoubleArrayTest.java Tue Dec 28 09:03:40 2010 @@ -59,6 +59,13 @@ public class ResizableDoubleArrayTest ex } 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 ex "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() {