Skip to content

Commit

Permalink
Improve NIntegrate quadrature methos (Spotbugs findings)
Browse files Browse the repository at this point in the history
  • Loading branch information
axkr committed Oct 25, 2024
1 parent e29ea68 commit d4c89b7
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.matheclipse.core.numerics.integral;

import java.util.function.DoubleUnaryOperator;
import org.matheclipse.core.expression.F;
import org.matheclipse.core.numerics.utils.Constants;
import org.matheclipse.core.numerics.utils.SimpleMath;

Expand Down Expand Up @@ -484,7 +485,7 @@ private static final void adapquad(final DoubleUnaryOperator f, double a, double
if (next == 4) {

// UPDATE
if (n != 4 || !(caution || (xa == a && div == 0))) {
if (n != 4 || !(caution || (F.isEqual(xa, a) && div == 0))) {
if (e < re) {
e = re;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.matheclipse.core.numerics.integral;

import java.util.function.DoubleUnaryOperator;
import org.matheclipse.core.expression.F;
import org.matheclipse.core.numerics.utils.Constants;

/**
Expand Down Expand Up @@ -78,7 +79,7 @@ private final QuadratureResult dlob8e(final DoubleUnaryOperator f, final double
s += Y[4] * (y5 + y9) + Y[5] * (f5 + f6) + Y[6] * y7;
s *= h;
double rtol = myTol;
if (est1 != s) {
if (!F.isEqual(est1, s)) {
final double r = Math.abs(est2 - s) / Math.abs(est1 - s);
if (r > 0.0 && r < 1.0) {
rtol /= r;
Expand Down Expand Up @@ -128,7 +129,7 @@ private final double dlob8(final DoubleUnaryOperator f, final double a, final do
est2 *= (h / 1470.0);

// check the convergence
if (s + (est2 - est1) == s || mll <= a || b <= mrr) {
if (F.isZero(est2 - est1) || mll <= a || b <= mrr) {
return est2;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.PriorityQueue;
import java.util.Stack;
import java.util.function.DoubleUnaryOperator;
import org.matheclipse.core.expression.F;

/**
* A local and global adaptive numerical integrator based on Simpson's rule [1]. For all
Expand Down Expand Up @@ -224,7 +225,7 @@ private final QuadratureResult localSimpson(final DoubleUnaryOperator func, fina
final double err = Math.abs(fine - crude);

// if the error is within the tolerance, update the global integral estimate
if (fev > 5 && (err <= 15.0 * eps || wid == 0.0 || eps * 2 == eps)) {
if (fev > 5 && (err <= 15.0 * eps || F.isZero(wid) || F.isZero(eps + eps, eps))) {
est += fine + (fine - crude) / 15.0;
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ public void testIntegrate() {
public void testNIntegrate() {
// avoid Indeterminate at 0
checkNumeric("NIntegrate(1/Sqrt(x), {x, 0.001, 1}, Method -> \"GaussLobattoRule\")", //
"1.9367544467966367");
"1.9367544467966324");
checkNumeric("NIntegrate(1/Sqrt(x), {x, 0.001, 1}, Method -> \"NewtonCotesRule\")", //
"1.936754446797901");
checkNumeric("NIntegrate(Sqrt(x), {x, 0, 1}, Method -> \"ClenshawCurtisRule\")", //
Expand Down

0 comments on commit d4c89b7

Please sign in to comment.