Skip to content

Commit 8073588

Browse files
committed
Improve error handling in FindRoot, FindMaximum, FindMinimum
1 parent c668c19 commit 8073588

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,11 @@ public IExpr evaluate(IAST ast, int argSize, IExpr[] options, EvalEngine engine,
124124
} catch (MathIllegalStateException mise) {
125125
if (mise.getSpecifier().equals(LocalizedCoreFormats.MAX_COUNT_EXCEEDED)) {
126126
Object[] parts = mise.getParts();
127-
// Failed to converge to the requested accuracy or precision within `1` iterations.
128-
return Errors.printMessage(ast.topHead(), "cvmit", F.list(F.$str(parts[0].toString())),
129-
engine);
127+
if (parts != null && parts.length >= 1) {
128+
// Failed to converge to the requested accuracy or precision within `1` iterations.
129+
return Errors.printMessage(ast.topHead(), "cvmit", F.list(F.$str(parts[0].toString())),
130+
engine);
131+
}
130132
}
131133
// `1`.
132134
return Errors.printMessage(ast.topHead(), "error", F.list(F.$str(mise.getMessage())), engine);

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,11 @@ public IExpr evaluate(IAST ast, int argSize, IExpr[] options, EvalEngine engine,
174174
} catch (MathIllegalStateException mise) {
175175
if (mise.getSpecifier().equals(LocalizedCoreFormats.MAX_COUNT_EXCEEDED)) {
176176
Object[] parts = mise.getParts();
177-
// Failed to converge to the requested accuracy or precision within `1` iterations.
178-
return Errors.printMessage(ast.topHead(), "cvmit", F.list(F.$str(parts[0].toString())),
179-
engine);
177+
if (parts != null && parts.length >= 1) {
178+
// Failed to converge to the requested accuracy or precision within `1` iterations.
179+
return Errors.printMessage(ast.topHead(), "cvmit", F.list(F.$str(parts[0].toString())),
180+
engine);
181+
}
180182
}
181183
// `1`.
182184
return Errors.printMessage(ast.topHead(), "error", F.list(F.$str(mise.getMessage())), engine);

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -441,13 +441,18 @@ private static IExpr univariateFindRoot(IExpr listOfEquations, IAST varValuePair
441441
IExpr result = optimizeSupplier.get();
442442
// engine.evalBlock(optimizeSupplier, list);
443443
return F.list(F.Rule(list.arg1(), result));
444-
} catch (MathIllegalStateException miae) {
445-
if (miae.getSpecifier() == LocalizedCoreFormats.CONVERGENCE_FAILED) {
446-
// Failed to converge to the requested accuracy or precision within `1` iterations.
447-
return Errors.printMessage(S.FindRoot, "cvmit", F.list(F.ZZ(maxIterations)), engine);
444+
} catch (MathIllegalStateException mise) {
445+
if (mise.getSpecifier() == LocalizedCoreFormats.CONVERGENCE_FAILED) {
446+
Object[] parts = mise.getParts();
447+
if (parts != null && parts.length >= 1) {
448+
// Failed to converge to the requested accuracy or precision within `1` iterations.
449+
return Errors.printMessage(S.FindRoot, "cvmit", F.list(F.$str(parts[0].toString())),
450+
engine);
451+
}
452+
448453
}
449454
// `1`.
450-
return Errors.printMessage(S.FindRoot, "error", F.list(F.$str(miae.getMessage())),
455+
return Errors.printMessage(S.FindRoot, "error", F.list(F.$str(mise.getMessage())),
451456
engine);
452457
} catch (MathRuntimeException mre) {
453458
if (mre.getSpecifier() == LocalizedCoreFormats.NOT_BRACKETING_INTERVAL

0 commit comments

Comments
 (0)