Skip to content

Commit 265a626

Browse files
committed
WIP #1034 new ClenshawCurtisRule, GaussLobattoRule,NewtonCotesRule
method options for NIntegrate
1 parent 206c0c2 commit 265a626

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/reflection/system/NIntegrate.java

+29-4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
import org.matheclipse.core.interfaces.IExpr;
3030
import org.matheclipse.core.interfaces.IReal;
3131
import org.matheclipse.core.interfaces.ISymbol;
32+
import org.matheclipse.core.numerics.integral.ClenshawCurtis;
33+
import org.matheclipse.core.numerics.integral.GaussLobatto;
34+
import org.matheclipse.core.numerics.integral.NewtonCotes;
3235
import org.matheclipse.core.numerics.integral.Quadrature;
3336
import org.matheclipse.core.numerics.integral.Quadrature.QuadratureResult;
3437
import org.matheclipse.core.numerics.integral.TanhSinh;
@@ -127,17 +130,39 @@ public static double integrate(String method, IAST list, final double min, final
127130
integrator = new TrapezoidIntegrator();
128131
} else if ("GaussKronrod".equalsIgnoreCase(method)) {
129132
return gausKronrodRule(maxIterations, f, min, max);
133+
} else if ("ClenshawCurtisRule".equalsIgnoreCase(method)) {
134+
Quadrature quadrature = new ClenshawCurtis(Config.SPECIAL_FUNCTIONS_TOLERANCE, 1000);
135+
QuadratureResult result = quadrature.integrate(f, min, max);
136+
if (result.converged) {
137+
return result.estimate;
138+
}
139+
// NIntegrate failed to converge after `1` refinements in `2` in the region `3`.
140+
throw new ArgumentTypeException("ncvi", F.List(F.ZZ(result.evaluations), xVar, list.rest()));
130141
} else if ("DoubleExponential".equalsIgnoreCase(method)) {
131-
Quadrature quadrature = new TanhSinh(1e-8, 1000);
142+
Quadrature quadrature = new TanhSinh(Config.SPECIAL_FUNCTIONS_TOLERANCE, maxIterations);
143+
QuadratureResult result = quadrature.integrate(f, min, max);
144+
if (result.converged) {
145+
return result.estimate;
146+
}
147+
// NIntegrate failed to converge after `1` refinements in `2` in the region `3`.
148+
throw new ArgumentTypeException("ncvi", F.List(F.ZZ(result.evaluations), xVar, list.rest()));
149+
} else if ("GaussLobatto".equalsIgnoreCase(method)) {
150+
Quadrature quadrature = new GaussLobatto(Config.SPECIAL_FUNCTIONS_TOLERANCE, 1000);
151+
QuadratureResult result = quadrature.integrate(f, min, max);
152+
if (result.converged) {
153+
return result.estimate;
154+
}
155+
// NIntegrate failed to converge after `1` refinements in `2` in the region `3`.
156+
throw new ArgumentTypeException("ncvi", F.List(F.ZZ(result.evaluations), xVar, list.rest()));
157+
158+
} else if ("NewtonCotesRule".equalsIgnoreCase(method)) {
159+
Quadrature quadrature = new NewtonCotes(Config.SPECIAL_FUNCTIONS_TOLERANCE, 1000);
132160
QuadratureResult result = quadrature.integrate(f, min, max);
133161
if (result.converged) {
134162
return result.estimate;
135163
}
136164
// NIntegrate failed to converge after `1` refinements in `2` in the region `3`.
137165
throw new ArgumentTypeException("ncvi", F.List(F.ZZ(result.evaluations), xVar, list.rest()));
138-
// Errors. printMessage(S.NIntegrate, "ncvi", F.List(F.ZZ(result.evaluations), xVar,
139-
// list.rest()),
140-
// engine);
141166
} else {
142167
if (maxPoints > 1000) {
143168
// github 150 - avoid StackOverflow from recursion

symja_android_library/matheclipse-core/src/test/java/org/matheclipse/core/system/IntegrateTest.java

+8
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,14 @@ public void testIntegrate() {
296296

297297
@Test
298298
public void testNIntegrate() {
299+
// avoid Indeterminate at 0
300+
checkNumeric("NIntegrate(1/Sqrt(x), {x, 0.001, 1}, Method -> \"GaussLobatto\")", //
301+
"1.9367544467966367");
302+
checkNumeric("NIntegrate(1/Sqrt(x), {x, 0.001, 1}, Method -> \"NewtonCotesRule\")", //
303+
"1.936754446797901");
304+
checkNumeric("NIntegrate(Sqrt(x), {x, 0, 1}, Method -> \"ClenshawCurtisRule\")", //
305+
"0.6666666666773636");
306+
299307
// "tanh-sinh" strategy
300308
checkNumeric("NIntegrate(Exp(x)/x, {x, -Infinity, -1}, Method ->DoubleExponential)", //
301309
"-0.2193839343955203");

0 commit comments

Comments
 (0)