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 2011/10/12 01:37:58 UTC

svn commit: r1182147 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math/stat/descriptive/moment/ main/java/org/apache/commons/math/util/ test/java/org/apache/commons/math/stat/descriptive/moment/ test/java/org/apache/commons/math/util/

Author: erans
Date: Tue Oct 11 23:37:57 2011
New Revision: 1182147

URL: http://svn.apache.org/viewvc?rev=1182147&view=rev
Log:
MATH-689
Moved array utilities from "MathUtils" to "MathArrays".

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/descriptive/moment/Variance.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathArrays.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/descriptive/moment/VarianceTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/MathArraysTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/MathUtilsTest.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/descriptive/moment/Variance.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/descriptive/moment/Variance.java?rev=1182147&r1=1182146&r2=1182147&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/descriptive/moment/Variance.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/descriptive/moment/Variance.java Tue Oct 11 23:37:57 2011
@@ -273,7 +273,7 @@ public class Variance extends AbstractSt
      * weights are to be treated as "expansion values," as will be the case if for example
      * the weights represent frequency counts. To normalize weights so that the denominator
      * in the variance computation equals the length of the input vector minus one, use <pre>
-     *   <code>evaluate(values, MathUtils.normalizeArray(weights, values.length)); </code>
+     *   <code>evaluate(values, MathArrays.normalizeArray(weights, values.length)); </code>
      * </pre>
      * <p>
      * Returns 0 for a single-value (i.e. length = 1) sample.</p>
@@ -332,7 +332,7 @@ public class Variance extends AbstractSt
      * weights are to be treated as "expansion values," as will be the case if for example
      * the weights represent frequency counts. To normalize weights so that the denominator
      * in the variance computation equals the length of the input vector minus one, use <pre>
-     *   <code>evaluate(values, MathUtils.normalizeArray(weights, values.length)); </code>
+     *   <code>evaluate(values, MathArrays.normalizeArray(weights, values.length)); </code>
      * </pre>
      * <p>
      * Returns 0 for a single-value (i.e. length = 1) sample.</p>
@@ -462,7 +462,7 @@ public class Variance extends AbstractSt
      * weights are to be treated as "expansion values," as will be the case if for example
      * the weights represent frequency counts. To normalize weights so that the denominator
      * in the variance computation equals the length of the input vector minus one, use <pre>
-     *   <code>evaluate(values, MathUtils.normalizeArray(weights, values.length), mean); </code>
+     *   <code>evaluate(values, MathArrays.normalizeArray(weights, values.length), mean); </code>
      * </pre>
      * <p>
      * Returns 0 for a single-value (i.e. length = 1) sample.</p>
@@ -539,7 +539,7 @@ public class Variance extends AbstractSt
      * weights are to be treated as "expansion values," as will be the case if for example
      * the weights represent frequency counts. To normalize weights so that the denominator
      * in the variance computation equals the length of the input vector minus one, use <pre>
-     *   <code>evaluate(values, MathUtils.normalizeArray(weights, values.length), mean); </code>
+     *   <code>evaluate(values, MathArrays.normalizeArray(weights, values.length), mean); </code>
      * </pre>
      * <p>
      * Returns 0 for a single-value (i.e. length = 1) sample.</p>

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathArrays.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathArrays.java?rev=1182147&r1=1182146&r2=1182147&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathArrays.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathArrays.java Tue Oct 11 23:37:57 2011
@@ -26,6 +26,9 @@ import org.apache.commons.math.exception
 import org.apache.commons.math.exception.MathInternalError;
 import org.apache.commons.math.exception.NonMonotonicSequenceException;
 import org.apache.commons.math.exception.NullArgumentException;
+import org.apache.commons.math.exception.MathIllegalArgumentException;
+import org.apache.commons.math.exception.util.LocalizedFormats;
+import org.apache.commons.math.exception.MathArithmeticException;
 
 /**
  * Arrays utilities.
@@ -1024,4 +1027,57 @@ public class MathArrays {
         }
         return true;
     }
+
+     /**
+      * Normalizes an array to make it sum to a specified value.
+      * Returns the result of the transformation <pre>
+      *    x |-> x * normalizedSum / sum
+      * </pre>
+      * applied to each non-NaN element x of the input array, where sum is the
+      * sum of the non-NaN entries in the input array.</p>
+      *
+      * <p>Throws IllegalArgumentException if {@code normalizedSum} is infinite
+      * or NaN and ArithmeticException if the input array contains any infinite elements
+      * or sums to 0.</p>
+      *
+      * <p>Ignores (i.e., copies unchanged to the output array) NaNs in the input array.</p>
+      *
+      * @param values Input array to be normalized
+      * @param normalizedSum Target sum for the normalized array
+      * @return the normalized array.
+      * @throws MathArithmeticException if the input array contains infinite
+      * elements or sums to zero.
+      * @throws MathIllegalArgumentException if the target sum is infinite or {@code NaN}.
+      * @since 2.1
+      */
+     public static double[] normalizeArray(double[] values, double normalizedSum) {
+         if (Double.isInfinite(normalizedSum)) {
+             throw new MathIllegalArgumentException(LocalizedFormats.NORMALIZE_INFINITE);
+         }
+         if (Double.isNaN(normalizedSum)) {
+             throw new MathIllegalArgumentException(LocalizedFormats.NORMALIZE_NAN);
+         }
+         double sum = 0d;
+         final int len = values.length;
+         double[] out = new double[len];
+         for (int i = 0; i < len; i++) {
+             if (Double.isInfinite(values[i])) {
+                 throw new MathIllegalArgumentException(LocalizedFormats.INFINITE_ARRAY_ELEMENT, values[i], i);
+             }
+             if (!Double.isNaN(values[i])) {
+                 sum += values[i];
+             }
+         }
+         if (sum == 0) {
+             throw new MathArithmeticException(LocalizedFormats.ARRAY_SUMS_TO_ZERO);
+         }
+         for (int i = 0; i < len; i++) {
+             if (Double.isNaN(values[i])) {
+                 out[i] = Double.NaN;
+             } else {
+                 out[i] = values[i] * normalizedSum / sum;
+             }
+         }
+         return out;
+     }
 }

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java?rev=1182147&r1=1182146&r2=1182147&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java Tue Oct 11 23:37:57 2011
@@ -936,58 +936,6 @@ public final class MathUtils {
         return a - p * FastMath.floor((a - offset) / p) - offset;
     }
 
-     /**
-      * <p>Normalizes an array to make it sum to a specified value.
-      * Returns the result of the transformation <pre>
-      *    x |-> x * normalizedSum / sum
-      * </pre>
-      * applied to each non-NaN element x of the input array, where sum is the
-      * sum of the non-NaN entries in the input array.</p>
-      *
-      * <p>Throws IllegalArgumentException if {@code normalizedSum} is infinite
-      * or NaN and ArithmeticException if the input array contains any infinite elements
-      * or sums to 0</p>
-      *
-      * <p>Ignores (i.e., copies unchanged to the output array) NaNs in the input array.</p>
-      *
-      * @param values input array to be normalized
-      * @param normalizedSum target sum for the normalized array
-      * @return normalized array
-      * @throws MathArithmeticException if the input array contains infinite elements or sums to zero
-      * @throws MathIllegalArgumentException if the target sum is infinite or NaN
-      * @since 2.1
-      */
-     public static double[] normalizeArray(double[] values, double normalizedSum) {
-         if (Double.isInfinite(normalizedSum)) {
-             throw new MathIllegalArgumentException(LocalizedFormats.NORMALIZE_INFINITE);
-         }
-         if (Double.isNaN(normalizedSum)) {
-             throw new MathIllegalArgumentException(LocalizedFormats.NORMALIZE_NAN);
-         }
-         double sum = 0d;
-         final int len = values.length;
-         double[] out = new double[len];
-         for (int i = 0; i < len; i++) {
-             if (Double.isInfinite(values[i])) {
-                 throw new MathIllegalArgumentException(LocalizedFormats.INFINITE_ARRAY_ELEMENT, values[i], i);
-             }
-             if (!Double.isNaN(values[i])) {
-                 sum += values[i];
-             }
-         }
-         if (sum == 0) {
-             throw new MathArithmeticException(LocalizedFormats.ARRAY_SUMS_TO_ZERO);
-         }
-         for (int i = 0; i < len; i++) {
-             if (Double.isNaN(values[i])) {
-                 out[i] = Double.NaN;
-             } else {
-                 out[i] = values[i] * normalizedSum / sum;
-             }
-         }
-         return out;
-     }
-
     /**
      * Round the given value to the specified number of decimal places. The
      * value is rounded using the {@link BigDecimal#ROUND_HALF_UP} method.

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/descriptive/moment/VarianceTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/descriptive/moment/VarianceTest.java?rev=1182147&r1=1182146&r2=1182147&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/descriptive/moment/VarianceTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/descriptive/moment/VarianceTest.java Tue Oct 11 23:37:57 2011
@@ -18,7 +18,7 @@ package org.apache.commons.math.stat.des
 
 import org.apache.commons.math.stat.descriptive.StorelessUnivariateStatisticAbstractTest;
 import org.apache.commons.math.stat.descriptive.UnivariateStatistic;
-import org.apache.commons.math.util.MathUtils;
+import org.apache.commons.math.util.MathArrays;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -110,7 +110,7 @@ public class VarianceTest extends Storel
         // All weights the same -> when weights are normalized to sum to the length of the values array,
         // weighted variance = unweighted value
         Assert.assertEquals(expectedValue(),
-                variance.evaluate(testArray, MathUtils.normalizeArray(identicalWeightsArray, testArray.length),
+                variance.evaluate(testArray, MathArrays.normalizeArray(identicalWeightsArray, testArray.length),
                         0, testArray.length), getTolerance());
 
     }

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/MathArraysTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/MathArraysTest.java?rev=1182147&r1=1182146&r2=1182147&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/MathArraysTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/MathArraysTest.java Tue Oct 11 23:37:57 2011
@@ -17,7 +17,10 @@ import java.util.Arrays;
 import org.apache.commons.math.exception.NonMonotonicSequenceException;
 import org.apache.commons.math.exception.DimensionMismatchException;
 import org.apache.commons.math.exception.NullArgumentException;
+import org.apache.commons.math.exception.MathArithmeticException;
+import org.apache.commons.math.exception.MathIllegalArgumentException;
 import org.apache.commons.math.random.Well1024a;
+import org.apache.commons.math.TestUtils;
 
 import org.junit.Assert;
 import org.junit.Test;
@@ -586,4 +589,49 @@ public class MathArraysTest {
         Assert.assertFalse(MathArrays.equalsIncludingNaN(new double[] { 1d },
                                                          new double[] { FastMath.nextAfter(FastMath.nextAfter(1d, 2d), 2d) }));
     }
+
+    @Test
+    public void testNormalizeArray() {
+        double[] testValues1 = new double[] {1, 1, 2};
+        TestUtils.assertEquals( new double[] {.25, .25, .5},
+                                MathArrays.normalizeArray(testValues1, 1),
+                                Double.MIN_VALUE);
+
+        double[] testValues2 = new double[] {-1, -1, 1};
+        TestUtils.assertEquals( new double[] {1, 1, -1},
+                                MathArrays.normalizeArray(testValues2, 1),
+                                Double.MIN_VALUE);
+
+        // Ignore NaNs
+        double[] testValues3 = new double[] {-1, -1, Double.NaN, 1, Double.NaN};
+        TestUtils.assertEquals( new double[] {1, 1,Double.NaN, -1, Double.NaN},
+                                MathArrays.normalizeArray(testValues3, 1),
+                                Double.MIN_VALUE);
+
+        // Zero sum -> MathArithmeticException
+        double[] zeroSum = new double[] {-1, 1};
+        try {
+            MathArrays.normalizeArray(zeroSum, 1);
+            Assert.fail("expecting MathArithmeticException");
+        } catch (MathArithmeticException ex) {}
+
+        // Infinite elements -> MathArithmeticException
+        double[] hasInf = new double[] {1, 2, 1, Double.NEGATIVE_INFINITY};
+        try {
+            MathArrays.normalizeArray(hasInf, 1);
+            Assert.fail("expecting MathIllegalArgumentException");
+        } catch (MathIllegalArgumentException ex) {}
+
+        // Infinite target -> MathIllegalArgumentException
+        try {
+            MathArrays.normalizeArray(testValues1, Double.POSITIVE_INFINITY);
+            Assert.fail("expecting MathIllegalArgumentException");
+        } catch (MathIllegalArgumentException ex) {}
+
+        // NaN target -> MathIllegalArgumentException
+        try {
+            MathArrays.normalizeArray(testValues1, Double.NaN);
+            Assert.fail("expecting MathIllegalArgumentException");
+        } catch (MathIllegalArgumentException ex) {}
+    }
 }

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/MathUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/MathUtilsTest.java?rev=1182147&r1=1182146&r2=1182147&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/MathUtilsTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/MathUtilsTest.java Tue Oct 11 23:37:57 2011
@@ -843,55 +843,6 @@ public final class MathUtilsTest {
     }
 
     @Test
-    public void testNormalizeArray() {
-        double[] testValues1 = new double[] {1, 1, 2};
-        TestUtils.assertEquals(
-                new double[] {.25, .25, .5},
-                MathUtils.normalizeArray(testValues1, 1),
-                Double.MIN_VALUE);
-
-        double[] testValues2 = new double[] {-1, -1, 1};
-        TestUtils.assertEquals(
-                new double[] {1, 1, -1},
-                MathUtils.normalizeArray(testValues2, 1),
-                Double.MIN_VALUE);
-
-        // Ignore NaNs
-        double[] testValues3 = new double[] {-1, -1, Double.NaN, 1, Double.NaN};
-        TestUtils.assertEquals(
-                new double[] {1, 1,Double.NaN, -1, Double.NaN},
-                MathUtils.normalizeArray(testValues3, 1),
-                Double.MIN_VALUE);
-
-        // Zero sum -> MathArithmeticException
-        double[] zeroSum = new double[] {-1, 1};
-        try {
-            MathUtils.normalizeArray(zeroSum, 1);
-            Assert.fail("expecting MathArithmeticException");
-        } catch (MathArithmeticException ex) {}
-
-        // Infinite elements -> MathArithmeticException
-        double[] hasInf = new double[] {1, 2, 1, Double.NEGATIVE_INFINITY};
-        try {
-            MathUtils.normalizeArray(hasInf, 1);
-            Assert.fail("expecting MathIllegalArgumentException");
-        } catch (MathIllegalArgumentException ex) {}
-
-        // Infinite target -> MathIllegalArgumentException
-        try {
-            MathUtils.normalizeArray(testValues1, Double.POSITIVE_INFINITY);
-            Assert.fail("expecting MathIllegalArgumentException");
-        } catch (MathIllegalArgumentException ex) {}
-
-        // NaN target -> MathIllegalArgumentException
-        try {
-            MathUtils.normalizeArray(testValues1, Double.NaN);
-            Assert.fail("expecting MathIllegalArgumentException");
-        } catch (MathIllegalArgumentException ex) {}
-
-    }
-
-    @Test
     public void testRoundDouble() {
         double x = 1.234567890;
         Assert.assertEquals(1.23, MathUtils.round(x, 2), 0.0);