Return-Path: X-Original-To: apmail-commons-commits-archive@minotaur.apache.org Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id D4434184D0 for ; Thu, 20 Aug 2015 11:09:15 +0000 (UTC) Received: (qmail 2862 invoked by uid 500); 20 Aug 2015 11:09:12 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 2765 invoked by uid 500); 20 Aug 2015 11:09:12 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 2755 invoked by uid 99); 20 Aug 2015 11:09:12 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 20 Aug 2015 11:09:12 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 87144E7151; Thu, 20 Aug 2015 11:09:12 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: erans@apache.org To: commits@commons.apache.org Message-Id: X-Mailer: ASF-Git Admin Mailer Subject: [math] MATH-1258 Date: Thu, 20 Aug 2015 11:09:12 +0000 (UTC) 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 Authored: Thu Aug 20 13:08:19 2015 +0200 Committer: Gilles 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};