From 43f9254b7c6d48912a5a219a6b07a8bff60d841d Mon Sep 17 00:00:00 2001 From: axexlck Date: Wed, 13 Sep 2023 19:22:34 +0200 Subject: [PATCH] WIP #822 catch exceptions in root finding with LaguerreSolver --- .../core/builtin/RootsFunctions.java | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/builtin/RootsFunctions.java b/symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/builtin/RootsFunctions.java index 37738e8952..35d142f98e 100644 --- a/symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/builtin/RootsFunctions.java +++ b/symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/builtin/RootsFunctions.java @@ -525,26 +525,33 @@ public static IAST roots(final IExpr arg1, boolean numericSolutions, IAST variab * root finding of real coefficient polynomials * * @param coefficients coefficients of the polynomial. - * @return the roots of the polynomial + * @return the roots of the polynomial or {@link F#NIL} if an exception occurs */ protected static IAST findRoots(double... coefficients) { - LaguerreSolver laguerreSolver = new LaguerreSolver(); - org.hipparchus.complex.Complex[] solveAllComplex = - laguerreSolver.solveAllComplex(coefficients, 0.0); - return F.mapRange(0, solveAllComplex.length, - i -> F.chopExpr( - F.complexNum(solveAllComplex[i].getReal(), solveAllComplex[i].getImaginary()), - Config.DEFAULT_ROOTS_CHOP_DELTA)); + try { + LaguerreSolver laguerreSolver = new LaguerreSolver(); + org.hipparchus.complex.Complex[] solveAllComplex = + laguerreSolver.solveAllComplex(coefficients, 0.0); + return F.mapRange(0, solveAllComplex.length, + i -> F.chopExpr( + F.complexNum(solveAllComplex[i].getReal(), solveAllComplex[i].getImaginary()), + Config.DEFAULT_ROOTS_CHOP_DELTA)); + } catch (RuntimeException rex) { + // solveAllComplex may throw MathIllegalArgumentException, NullArgumentException, + // MathIllegalStateException + Errors.printMessage(S.Roots, rex, EvalEngine.get()); + } + return F.NIL; } /** * Compute a set of polynomial coefficients and the roots for the polynomial coefficients. * Depending on the polynomial being considered the roots may contain complex numbers. When - * complex numbers are present they will come in pairs of complex conjugates. + * complex numbers are present they will come in pairs of complex conjugate's. * * @param polynomialExpr * @param variables - * @return + * @return the roots of the polynomial or {@link F#NIL} if an exception occurs */ public static IAST findRoots(IExpr polynomialExpr, final IAST variables) { double[] coefficients = coefficients(polynomialExpr, (ISymbol) variables.arg1());