Add "checkFinite" utility method.
Functionality was defined in class "MathUtils" (in v3.6.1).
This commit is contained in:
parent
90d2d4edde
commit
093c7ecbf5
|
@ -34,6 +34,7 @@ import org.apache.commons.math4.legacy.exception.NotPositiveException;
|
|||
import org.apache.commons.math4.legacy.exception.NotStrictlyPositiveException;
|
||||
import org.apache.commons.math4.legacy.exception.NullArgumentException;
|
||||
import org.apache.commons.math4.legacy.exception.NumberIsTooLargeException;
|
||||
import org.apache.commons.math4.legacy.exception.NotFiniteNumberException;
|
||||
import org.apache.commons.math4.legacy.exception.util.LocalizedFormats;
|
||||
import org.apache.commons.math4.core.jdkmath.JdkMath;
|
||||
|
||||
|
@ -573,6 +574,24 @@ public final class MathArrays {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that all the elements are real numbers.
|
||||
*
|
||||
* @param val Arguments.
|
||||
* @throws NotFiniteNumberException if any values of the array is not a
|
||||
* finite real number.
|
||||
*/
|
||||
public static void checkFinite(final double[] val)
|
||||
throws NotFiniteNumberException {
|
||||
for (int i = 0; i < val.length; i++) {
|
||||
final double x = val[i];
|
||||
if (Double.isInfinite(x) ||
|
||||
Double.isNaN(x)) {
|
||||
throw new NotFiniteNumberException(val[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that all entries of the input array are >= 0.
|
||||
*
|
||||
|
|
|
@ -31,6 +31,7 @@ import org.apache.commons.math4.legacy.exception.NotANumberException;
|
|||
import org.apache.commons.math4.legacy.exception.NotPositiveException;
|
||||
import org.apache.commons.math4.legacy.exception.NotStrictlyPositiveException;
|
||||
import org.apache.commons.math4.legacy.exception.NullArgumentException;
|
||||
import org.apache.commons.math4.legacy.exception.NotFiniteNumberException;
|
||||
import org.apache.commons.math4.core.jdkmath.JdkMath;
|
||||
|
||||
/**
|
||||
|
@ -757,4 +758,26 @@ public class MathArraysTest {
|
|||
public void testUniqueNullArgument() {
|
||||
MathArrays.unique(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCheckFinite() {
|
||||
try {
|
||||
MathArrays.checkFinite(new double[] {0, -1, Double.POSITIVE_INFINITY, -2, 3});
|
||||
Assert.fail("an exception should have been thrown");
|
||||
} catch (NotFiniteNumberException e) {
|
||||
// Expected
|
||||
}
|
||||
try {
|
||||
MathArrays.checkFinite(new double[] {1, Double.NEGATIVE_INFINITY, -2, 3});
|
||||
Assert.fail("an exception should have been thrown");
|
||||
} catch (NotFiniteNumberException e) {
|
||||
// Expected
|
||||
}
|
||||
try {
|
||||
MathArrays.checkFinite(new double[] {4, 3, -1, Double.NaN, -2, 1});
|
||||
Assert.fail("an exception should have been thrown");
|
||||
} catch (NotFiniteNumberException e) {
|
||||
// Expected
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue