MATH-1258

New utility "checkEqualLength" to consistently report failed precondition.
This commit is contained in:
Gilles 2015-08-20 12:58:41 +02:00
parent a7ef0455c3
commit f70741c9b2
2 changed files with 53 additions and 12 deletions

View File

@ -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++) {
@ -372,6 +364,41 @@ public class MathArrays {
return checkOrder(val, dir, strict, false);
}
/**
* 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.
*
@ -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);
}

View File

@ -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};