commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pste...@apache.org
Subject svn commit: r1403169 - in /commons/proper/math/trunk/src: changes/ main/java/org/apache/commons/math3/stat/inference/ main/java/org/apache/commons/math3/util/ test/java/org/apache/commons/math3/util/
Date Mon, 29 Oct 2012 04:06:14 GMT
Author: psteitz
Date: Mon Oct 29 04:06:14 2012
New Revision: 1403169

URL: http://svn.apache.org/viewvc?rev=1403169&view=rev
Log:
Moved array argument validation methods from ChiSquareTest to MathArrays. JIRA: MATH-885.

Modified:
    commons/proper/math/trunk/src/changes/changes.xml
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/inference/ChiSquareTest.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/MathArrays.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/util/MathArraysTest.java

Modified: commons/proper/math/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/changes/changes.xml?rev=1403169&r1=1403168&r2=1403169&view=diff
==============================================================================
--- commons/proper/math/trunk/src/changes/changes.xml (original)
+++ commons/proper/math/trunk/src/changes/changes.xml Mon Oct 29 04:06:14 2012
@@ -52,6 +52,9 @@ If the output is not quite correct, chec
   <body>
     <release version="3.1" date="TBD" description="
 ">
+      <action dev="psteitz" type="update" issue="MATH-885">
+        Moved private array argument validation methods from ChiSquareTest to MathArrays.
+      </action>
       <action dev="erans" type="add" issue="MATH-816" due-to="Jared Becksfort">
         New class for generic multivariate mixture model distributions.
       </action>

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/inference/ChiSquareTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/inference/ChiSquareTest.java?rev=1403169&r1=1403168&r2=1403169&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/inference/ChiSquareTest.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/inference/ChiSquareTest.java
Mon Oct 29 04:06:14 2012
@@ -26,7 +26,7 @@ import org.apache.commons.math3.exceptio
 import org.apache.commons.math3.exception.ZeroException;
 import org.apache.commons.math3.exception.util.LocalizedFormats;
 import org.apache.commons.math3.util.FastMath;
-import org.apache.commons.math3.util.MathUtils;
+import org.apache.commons.math3.util.MathArrays;
 
 /**
  * Implements Chi-Square test statistics.
@@ -86,8 +86,8 @@ public class ChiSquareTest {
         if (expected.length != observed.length) {
             throw new DimensionMismatchException(expected.length, observed.length);
         }
-        checkPositive(expected);
-        checkNonNegative(observed);
+        MathArrays.checkPositive(expected);
+        MathArrays.checkNonNegative(observed);
 
         double sumExpected = 0d;
         double sumObserved = 0d;
@@ -158,7 +158,6 @@ public class ChiSquareTest {
         ChiSquaredDistribution distribution =
             new ChiSquaredDistribution(expected.length - 1.0);
         return 1.0 - distribution.cumulativeProbability(chiSquare(expected, observed));
-
     }
 
     /**
@@ -420,8 +419,8 @@ public class ChiSquareTest {
         }
 
         // Ensure non-negative counts
-        checkNonNegative(observed1);
-        checkNonNegative(observed2);
+        MathArrays.checkNonNegative(observed1);
+        MathArrays.checkNonNegative(observed2);
 
         // Compute and compare count sums
         long countSum1 = 0;
@@ -594,84 +593,8 @@ public class ChiSquareTest {
             throw new DimensionMismatchException(in[0].length, 2);
         }
 
-        checkRectangular(in);
-        checkNonNegative(in);
-
-    }
-
-    //---------------------  Private array methods -- should find a utility home for these
-
-    /**
-     * Throws DimensionMismatchException if the input array is not rectangular.
-     *
-     * @param in array to be tested
-     * @throws NullArgumentException if input array is null
-     * @throws DimensionMismatchException if input array is not rectangular
-     */
-    private void checkRectangular(final long[][] in)
-        throws NullArgumentException, DimensionMismatchException {
-
-        MathUtils.checkNotNull(in);
-        for (int i = 1; i < in.length; i++) {
-            if (in[i].length != in[0].length) {
-                throw new DimensionMismatchException(
-                        LocalizedFormats.DIFFERENT_ROWS_LENGTHS,
-                        in[i].length, in[0].length);
-            }
-        }
-
-    }
-
-    /**
-     * Check all entries of the input array are strictly positive.
-     *
-     * @param in Array to be tested.
-     * @throws NotStrictlyPositiveException if one entry is not strictly positive.
-     */
-    private void checkPositive(final double[] in)
-        throws NotStrictlyPositiveException {
-
-        for (int i = 0; i < in.length; i++) {
-            if (in[i] <= 0) {
-                throw new NotStrictlyPositiveException(in[i]);
-            }
-        }
-
-    }
-
-    /**
-     * Check all entries of the input array are >= 0.
-     *
-     * @param in Array to be tested.
-     * @throws NotPositiveException if one entry is negative.
-     */
-    private void checkNonNegative(final long[] in)
-        throws NotPositiveException {
-
-        for (int i = 0; i < in.length; i++) {
-            if (in[i] < 0) {
-                throw new NotPositiveException(in[i]);
-            }
-        }
-
-    }
-
-    /**
-     * Check all entries of the input array are >= 0.
-     *
-     * @param in Array to be tested.
-     * @throws NotPositiveException if one entry is negative.
-     */
-    private void checkNonNegative(final long[][] in)
-        throws NotPositiveException {
-
-        for (int i = 0; i < in.length; i ++) {
-            for (int j = 0; j < in[i].length; j++) {
-                if (in[i][j] < 0) {
-                    throw new NotPositiveException(in[i][j]);
-                }
-            }
-        }
+        MathArrays.checkRectangular(in);
+        MathArrays.checkNonNegative(in);
 
     }
 

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/MathArrays.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/MathArrays.java?rev=1403169&r1=1403168&r2=1403169&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/MathArrays.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/MathArrays.java
Mon Oct 29 04:06:14 2012
@@ -25,6 +25,8 @@ import java.util.Collections;
 import org.apache.commons.math3.exception.DimensionMismatchException;
 import org.apache.commons.math3.exception.MathInternalError;
 import org.apache.commons.math3.exception.NonMonotonicSequenceException;
+import org.apache.commons.math3.exception.NotPositiveException;
+import org.apache.commons.math3.exception.NotStrictlyPositiveException;
 import org.apache.commons.math3.exception.NullArgumentException;
 import org.apache.commons.math3.exception.MathIllegalArgumentException;
 import org.apache.commons.math3.exception.util.LocalizedFormats;
@@ -302,6 +304,73 @@ public class MathArrays {
     }
 
     /**
+     * Throws DimensionMismatchException if the input array is not rectangular.
+     *
+     * @param in array to be tested
+     * @throws NullArgumentException if input array is null
+     * @throws DimensionMismatchException if input array is not rectangular
+     */
+    public static void checkRectangular(final long[][] in)
+        throws NullArgumentException, DimensionMismatchException {
+        MathUtils.checkNotNull(in);
+        for (int i = 1; i < in.length; i++) {
+            if (in[i].length != in[0].length) {
+                throw new DimensionMismatchException(
+                        LocalizedFormats.DIFFERENT_ROWS_LENGTHS,
+                        in[i].length, in[0].length);
+            }
+        }
+    }
+
+    /**
+     * Check that all entries of the input array are strictly positive.
+     *
+     * @param in Array to be tested
+     * @throws NotStrictlyPositiveException if any entries of the array are not
+     * strictly positive.
+     */
+    public static void checkPositive(final double[] in)
+        throws NotStrictlyPositiveException {
+        for (int i = 0; i < in.length; i++) {
+            if (in[i] <= 0) {
+                throw new NotStrictlyPositiveException(in[i]);
+            }
+        }
+    }
+
+    /**
+     * Check that all entries of the input array are >= 0.
+     *
+     * @param in Array to be tested
+     * @throws NotPositiveException if any array entries are less than 0.
+     */
+    public static void checkNonNegative(final long[] in)
+        throws NotPositiveException {
+        for (int i = 0; i < in.length; i++) {
+            if (in[i] < 0) {
+                throw new NotPositiveException(in[i]);
+            }
+        }
+    }
+
+    /**
+     * Check all entries of the input array are >= 0.
+     *
+     * @param in Array to be tested
+     * @throws NotPositiveException if any array entries are less than 0.
+     */
+    public static void checkNonNegative(final long[][] in)
+        throws NotPositiveException {
+        for (int i = 0; i < in.length; i ++) {
+            for (int j = 0; j < in[i].length; j++) {
+                if (in[i][j] < 0) {
+                    throw new NotPositiveException(in[i][j]);
+                }
+            }
+        }
+    }
+
+    /**
      * Returns the Cartesian norm (2-norm), handling both overflow and underflow.
      * Translation of the minpack enorm subroutine.
      *

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/util/MathArraysTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/util/MathArraysTest.java?rev=1403169&r1=1403168&r2=1403169&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/util/MathArraysTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/util/MathArraysTest.java
Mon Oct 29 04:06:14 2012
@@ -16,6 +16,8 @@ package org.apache.commons.math3.util;
 import java.util.Arrays;
 import org.apache.commons.math3.exception.NonMonotonicSequenceException;
 import org.apache.commons.math3.exception.DimensionMismatchException;
+import org.apache.commons.math3.exception.NotPositiveException;
+import org.apache.commons.math3.exception.NotStrictlyPositiveException;
 import org.apache.commons.math3.exception.NullArgumentException;
 import org.apache.commons.math3.exception.MathArithmeticException;
 import org.apache.commons.math3.exception.MathIllegalArgumentException;
@@ -196,6 +198,94 @@ public class MathArraysTest {
                                                                 new Double(-27.5) },
                 MathArrays.OrderDirection.DECREASING, false));
     }
+    
+    @Test
+    public void testCheckRectangular() {
+        final long[][] rect = new long[][] {{0, 1}, {2, 3}};
+        final long[][] ragged = new long[][] {{0, 1}, {2}};
+        final long[][] nullArray = null;
+        final long[][] empty = new long[][] {};
+        MathArrays.checkRectangular(rect);
+        MathArrays.checkRectangular(empty);
+        try {
+            MathArrays.checkRectangular(ragged);
+            Assert.fail("Expecting DimensionMismatchException");
+        } catch (DimensionMismatchException ex) {
+            // Expected
+        }
+        try {
+            MathArrays.checkRectangular(nullArray);
+            Assert.fail("Expecting NullArgumentException");
+        } catch (NullArgumentException ex) {
+            // Expected
+        } 
+    }
+    
+    @Test
+    public void testCheckPositive() {
+        final double[] positive = new double[] {1, 2, 3};
+        final double[] nonNegative = new double[] {0, 1, 2};
+        final double[] nullArray = null;
+        final double[] empty = new double[] {};
+        MathArrays.checkPositive(positive);
+        MathArrays.checkPositive(empty);
+        try {
+            MathArrays.checkPositive(nullArray);
+            Assert.fail("Expecting NullPointerException");
+        } catch (NullPointerException ex) {
+            // Expected
+        }
+        try {
+            MathArrays.checkPositive(nonNegative);
+            Assert.fail("Expecting NotStrictlyPositiveException");
+        } catch (NotStrictlyPositiveException ex) {
+            // Expected
+        }
+    }
+    
+    @Test
+    public void testCheckNonNegative() {
+        final long[] nonNegative = new long[] {0, 1};
+        final long[] hasNegative = new long[] {-1};
+        final long[] nullArray = null;
+        final long[] empty = new long[] {};
+        MathArrays.checkNonNegative(nonNegative);
+        MathArrays.checkNonNegative(empty);
+        try {
+            MathArrays.checkNonNegative(nullArray);
+            Assert.fail("Expecting NullPointerException");
+        } catch (NullPointerException ex) {
+            // Expected
+        }
+        try {
+            MathArrays.checkNonNegative(hasNegative);
+            Assert.fail("Expecting NotPositiveException");
+        } catch (NotPositiveException ex) {
+            // Expected
+        }
+    }
+    
+    @Test
+    public void testCheckNonNegative2D() {
+        final long[][] nonNegative = new long[][] {{0, 1}, {1, 0}};
+        final long[][] hasNegative = new long[][] {{-1}, {0}};
+        final long[][] nullArray = null;
+        final long[][] empty = new long[][] {};
+        MathArrays.checkNonNegative(nonNegative);
+        MathArrays.checkNonNegative(empty);
+        try {
+            MathArrays.checkNonNegative(nullArray);
+            Assert.fail("Expecting NullPointerException");
+        } catch (NullPointerException ex) {
+            // Expected
+        }
+        try {
+            MathArrays.checkNonNegative(hasNegative);
+            Assert.fail("Expecting NotPositiveException");
+        } catch (NotPositiveException ex) {
+            // Expected
+        }
+    }
 
     @Test
     public void testSortInPlace() {



Mime
View raw message