commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject [math] MATH-1258
Date Thu, 20 Aug 2015 11:09:12 GMT
Repository: commons-math
Updated Branches:
  refs/heads/MATH_3_X 49a9e6e87 -> 9cb16d5b1


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/9cb16d5b
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/9cb16d5b
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/9cb16d5b

Branch: refs/heads/MATH_3_X
Commit: 9cb16d5b1e02ad41fe8481cb7e74fa57da5f08e7
Parents: 49a9e6e
Author: Gilles <erans@apache.org>
Authored: Thu Aug 20 13:08:19 2015 +0200
Committer: Gilles <erans@apache.org>
Committed: Thu Aug 20 13:08:19 2015 +0200

----------------------------------------------------------------------
 .../apache/commons/math3/util/MathArrays.java   | 55 ++++++++++++++------
 .../commons/math3/util/MathArraysTest.java      | 13 +++++
 2 files changed, 53 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-math/blob/9cb16d5b/src/main/java/org/apache/commons/math3/util/MathArrays.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math3/util/MathArrays.java b/src/main/java/org/apache/commons/math3/util/MathArrays.java
index b7148cc..5bf7890 100644
--- a/src/main/java/org/apache/commons/math3/util/MathArrays.java
+++ b/src/main/java/org/apache/commons/math3/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.
@@ -1734,9 +1761,7 @@ public class MathArrays {
             throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
         }
 
-        if (weights.length != values.length) {
-            throw new DimensionMismatchException(weights.length, values.length);
-        }
+        checkEqualLength(weights, values);
 
         boolean containsPositiveWeight = false;
         for (int i = begin; i < begin + length; i++) {

http://git-wip-us.apache.org/repos/asf/commons-math/blob/9cb16d5b/src/test/java/org/apache/commons/math3/util/MathArraysTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math3/util/MathArraysTest.java b/src/test/java/org/apache/commons/math3/util/MathArraysTest.java
index bb2de4e..1361c7e 100644
--- a/src/test/java/org/apache/commons/math3/util/MathArraysTest.java
+++ b/src/test/java/org/apache/commons/math3/util/MathArraysTest.java
@@ -479,6 +479,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};


Mime
View raw message