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 2023/01/05 18:54:09 UTC
[commons-math] branch master updated: Add "checkFinite" utility method.
This is an automated email from the ASF dual-hosted git repository.
erans pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-math.git
The following commit(s) were added to refs/heads/master by this push:
new 093c7ecbf Add "checkFinite" utility method.
093c7ecbf is described below
commit 093c7ecbf514d5f196bcb4afd9a67932e9e28f21
Author: Gilles Sadowski <gi...@gmail.com>
AuthorDate: Thu Jan 5 19:52:17 2023 +0100
Add "checkFinite" utility method.
Functionality was defined in class "MathUtils" (in v3.6.1).
---
.../commons/math4/legacy/core/MathArrays.java | 19 ++++++++++++++++++
.../commons/math4/legacy/core/MathArraysTest.java | 23 ++++++++++++++++++++++
2 files changed, 42 insertions(+)
diff --git a/commons-math-legacy-core/src/main/java/org/apache/commons/math4/legacy/core/MathArrays.java b/commons-math-legacy-core/src/main/java/org/apache/commons/math4/legacy/core/MathArrays.java
index 7b4284b8f..869d10ad5 100644
--- a/commons-math-legacy-core/src/main/java/org/apache/commons/math4/legacy/core/MathArrays.java
+++ b/commons-math-legacy-core/src/main/java/org/apache/commons/math4/legacy/core/MathArrays.java
@@ -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.
*
diff --git a/commons-math-legacy-core/src/test/java/org/apache/commons/math4/legacy/core/MathArraysTest.java b/commons-math-legacy-core/src/test/java/org/apache/commons/math4/legacy/core/MathArraysTest.java
index 54677765b..99f09ff02 100644
--- a/commons-math-legacy-core/src/test/java/org/apache/commons/math4/legacy/core/MathArraysTest.java
+++ b/commons-math-legacy-core/src/test/java/org/apache/commons/math4/legacy/core/MathArraysTest.java
@@ -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
+ }
+ }
}
Re: [commons-math] branch master updated: Add "checkFinite" utility method.
Posted by Gilles Sadowski <gi...@gmail.com>.
Le ven. 6 janv. 2023 à 02:18, Alex Herbert <al...@gmail.com> a écrit :
>
> Note there is an isFinite method.
>
> if (!Double.isFinite(x)) {
> ...
>
> The isFinite method is a JVM intrinsic method in JDK 20. It was added
> in JDK 8 so can be used in [math].
Thanks.
>
> PMD rules would also change this to a forEach loop:
>
> > + for (double x : value) {
> > + if (!Double.isFinite(x)) {
> > + throw new NotFiniteNumberException(x);
> > + }
> > + }
>
> This does remove the requirement to declare x = val[i] for reuse of x.
Done.
Regards,
Gilles
>> [...]
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
Re: [commons-math] branch master updated: Add "checkFinite" utility method.
Posted by Alex Herbert <al...@gmail.com>.
Note there is an isFinite method.
if (!Double.isFinite(x)) {
...
The isFinite method is a JVM intrinsic method in JDK 20. It was added
in JDK 8 so can be used in [math].
PMD rules would also change this to a forEach loop:
> + for (double x : value) {
> + if (!Double.isFinite(x)) {
> + throw new NotFiniteNumberException(x);
> + }
> + }
This does remove the requirement to declare x = val[i] for reuse of x.
Alex
On Thu, 5 Jan 2023 at 18:54, <er...@apache.org> wrote:
>
> This is an automated email from the ASF dual-hosted git repository.
>
> erans pushed a commit to branch master
> in repository https://gitbox.apache.org/repos/asf/commons-math.git
>
>
> The following commit(s) were added to refs/heads/master by this push:
> new 093c7ecbf Add "checkFinite" utility method.
> 093c7ecbf is described below
>
> commit 093c7ecbf514d5f196bcb4afd9a67932e9e28f21
> Author: Gilles Sadowski <gi...@gmail.com>
> AuthorDate: Thu Jan 5 19:52:17 2023 +0100
>
> Add "checkFinite" utility method.
>
> Functionality was defined in class "MathUtils" (in v3.6.1).
> ---
> .../commons/math4/legacy/core/MathArrays.java | 19 ++++++++++++++++++
> .../commons/math4/legacy/core/MathArraysTest.java | 23 ++++++++++++++++++++++
> 2 files changed, 42 insertions(+)
>
> diff --git a/commons-math-legacy-core/src/main/java/org/apache/commons/math4/legacy/core/MathArrays.java b/commons-math-legacy-core/src/main/java/org/apache/commons/math4/legacy/core/MathArrays.java
> index 7b4284b8f..869d10ad5 100644
> --- a/commons-math-legacy-core/src/main/java/org/apache/commons/math4/legacy/core/MathArrays.java
> +++ b/commons-math-legacy-core/src/main/java/org/apache/commons/math4/legacy/core/MathArrays.java
> @@ -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.
> *
> diff --git a/commons-math-legacy-core/src/test/java/org/apache/commons/math4/legacy/core/MathArraysTest.java b/commons-math-legacy-core/src/test/java/org/apache/commons/math4/legacy/core/MathArraysTest.java
> index 54677765b..99f09ff02 100644
> --- a/commons-math-legacy-core/src/test/java/org/apache/commons/math4/legacy/core/MathArraysTest.java
> +++ b/commons-math-legacy-core/src/test/java/org/apache/commons/math4/legacy/core/MathArraysTest.java
> @@ -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
> + }
> + }
> }
>
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org