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 2015/08/20 13:01:21 UTC

[math] MATH-1258

Repository: commons-math
Updated Branches:
  refs/heads/master a7ef0455c -> f70741c9b


MATH-1258

New utility "checkEqualLength" to consistently report failed precondition.


Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/f70741c9
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/f70741c9
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/f70741c9

Branch: refs/heads/master
Commit: f70741c9b216d3836d047c542847920d4778cf9d
Parents: a7ef045
Author: Gilles <er...@apache.org>
Authored: Thu Aug 20 12:58:41 2015 +0200
Committer: Gilles <er...@apache.org>
Committed: Thu Aug 20 12:58:41 2015 +0200

----------------------------------------------------------------------
 .../apache/commons/math4/util/MathArrays.java   | 52 +++++++++++++++-----
 .../commons/math4/util/MathArraysTest.java      | 13 +++++
 2 files changed, 53 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-math/blob/f70741c9/src/main/java/org/apache/commons/math4/util/MathArrays.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math4/util/MathArrays.java b/src/main/java/org/apache/commons/math4/util/MathArrays.java
index 29a8c37..325f71b 100644
--- a/src/main/java/org/apache/commons/math4/util/MathArrays.java
+++ b/src/main/java/org/apache/commons/math4/util/MathArrays.java
@@ -119,9 +119,7 @@ public class MathArrays {
      */
     public static double[] ebeAdd(double[] a, double[] b)
         throws DimensionMismatchException {
-        if (a.length != b.length) {
-            throw new DimensionMismatchException(a.length, b.length);
-        }
+        checkEqualLength(a, b);
 
         final double[] result = a.clone();
         for (int i = 0; i < a.length; i++) {
@@ -141,9 +139,7 @@ public class MathArrays {
      */
     public static double[] ebeSubtract(double[] a, double[] b)
         throws DimensionMismatchException {
-        if (a.length != b.length) {
-            throw new DimensionMismatchException(a.length, b.length);
-        }
+        checkEqualLength(a, b);
 
         final double[] result = a.clone();
         for (int i = 0; i < a.length; i++) {
@@ -163,9 +159,7 @@ public class MathArrays {
      */
     public static double[] ebeMultiply(double[] a, double[] b)
         throws DimensionMismatchException {
-        if (a.length != b.length) {
-            throw new DimensionMismatchException(a.length, b.length);
-        }
+        checkEqualLength(a, b);
 
         final double[] result = a.clone();
         for (int i = 0; i < a.length; i++) {
@@ -185,9 +179,7 @@ public class MathArrays {
      */
     public static double[] ebeDivide(double[] a, double[] b)
         throws DimensionMismatchException {
-        if (a.length != b.length) {
-            throw new DimensionMismatchException(a.length, b.length);
-        }
+        checkEqualLength(a, b);
 
         final double[] result = a.clone();
         for (int i = 0; i < a.length; i++) {
@@ -373,6 +365,41 @@ public class MathArrays {
     }
 
     /**
+     * Check that both arrays have the same length.
+     *
+     * @param a Array.
+     * @param b Array.
+     * @param abort Whether to throw an exception if the check fails.
+     * @return {@code true} if the arrays have the same length.
+     * @throws DimensionMismatchException if the lengths differ and
+     * {@code abort} is {@code true}.
+     */
+    public static boolean checkEqualLength(double[] a,
+                                           double[] b,
+                                           boolean abort) {
+        if (a.length == b.length) {
+            return true;
+        } else {
+            if (abort) {
+                throw new DimensionMismatchException(a.length, b.length);
+            }
+            return false;
+        }
+    }
+
+    /**
+     * Check that both arrays have the same length.
+     *
+     * @param a Array.
+     * @param b Array.
+     * @throws DimensionMismatchException if the lengths differ.
+     */
+    public static void checkEqualLength(double[] a,
+                                        double[] b) {
+        checkEqualLength(a, b, true);
+    }
+
+    /**
      * Check that the given array is sorted.
      *
      * @param val Values.
@@ -1736,6 +1763,7 @@ public class MathArrays {
             throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
         }
 
+        checkEqualLength(weights, values);
         if (weights.length != values.length) {
             throw new DimensionMismatchException(weights.length, values.length);
         }

http://git-wip-us.apache.org/repos/asf/commons-math/blob/f70741c9/src/test/java/org/apache/commons/math4/util/MathArraysTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math4/util/MathArraysTest.java b/src/test/java/org/apache/commons/math4/util/MathArraysTest.java
index 9b837e0..0a7c30f 100644
--- a/src/test/java/org/apache/commons/math4/util/MathArraysTest.java
+++ b/src/test/java/org/apache/commons/math4/util/MathArraysTest.java
@@ -482,6 +482,19 @@ public class MathArraysTest {
         }
     }
 
+    @Test(expected=DimensionMismatchException.class)
+    public void testCheckEqualLength1() {
+        MathArrays.checkEqualLength(new double[] {1, 2, 3},
+                                    new double[] {1, 2, 3, 4});
+    }
+
+    @Test
+    public void testCheckEqualLength2() {
+        final double[] a = new double[] {-1, -12, -23, -34};
+        final double[] b = new double[] {56, 67, 78, 89};
+        Assert.assertTrue(MathArrays.checkEqualLength(a, b, false));
+    }
+
     @Test
     public void testSortInPlace() {
         final double[] x1 = {2,   5,  -3, 1,  4};