From 2436090484ee89ba36cc135c394c7d1b4a66b2f3 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Mon, 28 Sep 2026 23:11:30 +0000 Subject: [PATCH] Newton's solver, the spurious-root filter and Gruntz without catch-alls Six catches wrapped code that declines by returning NaN, null or false already: four in Newton's solver (one catching an OverflowException that System.Numerics.Complex arithmetic does not throw), the spurious-root filter's residual check, and the Gruntz limit's entry. Each site was instrumented and logged every exception it caught, over the whole unit suite and a 598-problem sample of Rubi's families 0 to 8: none caught anything. They go, so that a defect below them reaches the caller instead of reading as a decline, and so does the OperationCanceledException that four of them took for one. A residual Newton cannot rank, one that is not a finite number, still ranks last, now by a test rather than a catch. The unit tests pass (net10.0, 13354), every target builds, and the allocation gate passes. Closes #1547. Co-Authored-By: Claude Opus 5.5 --- .../Continuous/Limits/Gruntz/Gruntz.cs | 5 +-- .../Solvers/EquationSolver/SolveStatement.cs | 19 ++++------ .../Solvers/NumericalSolving/NewtonSolver.cs | 37 +++++++------------ 3 files changed, 21 insertions(+), 40 deletions(-) diff --git a/Sources/AngouriMath/Functions/Continuous/Limits/Gruntz/Gruntz.cs b/Sources/AngouriMath/Functions/Continuous/Limits/Gruntz/Gruntz.cs index c5dfb0f43..32ff6c504 100644 --- a/Sources/AngouriMath/Functions/Continuous/Limits/Gruntz/Gruntz.cs +++ b/Sources/AngouriMath/Functions/Continuous/Limits/Gruntz/Gruntz.cs @@ -79,10 +79,7 @@ private static void Trace(string what, object? value) if (depth > 0) return null; // already inside; the caller is the entry expr = AsExponentials(expr, x); - try { return LimitInf(expr, x); } - catch (Core.Exceptions.AngouriBugException) { throw; } - catch (OperationCanceledException) { throw; } - catch (Exception) { return null; } + return LimitInf(expr, x); } private static Entity? LimitInf(Entity e, Variable x) diff --git a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs index 948312c0a..69de0ecd1 100644 --- a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs +++ b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs @@ -123,18 +123,13 @@ private static bool IsSpurious(Entity equation, Variable x, Entity root) return true; if (root.Vars.Any()) return false; - try - { - var substituted = equation.Substitute(x, root); - if (substituted.Evaled is not Number.Complex residual || !residual.IsFinite) - return false; - var size = residual.Abs().EDecimal; - if (size.LessThan(MathS.Settings.PrecisionErrorCommon)) - return false; - return size.GreaterThan(LargestTerm(substituted).Multiply(RelativeResidualTolerance)); - } - catch (Core.Exceptions.AngouriBugException) { throw; } - catch (System.Exception) { return false; } + var substituted = equation.Substitute(x, root); + if (substituted.Evaled is not Number.Complex residual || !residual.IsFinite) + return false; + var size = residual.Abs().EDecimal; + if (size.LessThan(MathS.Settings.PrecisionErrorCommon)) + return false; + return size.GreaterThan(LargestTerm(substituted).Multiply(RelativeResidualTolerance)); } /// diff --git a/Sources/AngouriMath/Functions/Continuous/Solvers/NumericalSolving/NewtonSolver.cs b/Sources/AngouriMath/Functions/Continuous/Solvers/NumericalSolving/NewtonSolver.cs index 79b94cc6b..c2f2a7d43 100644 --- a/Sources/AngouriMath/Functions/Continuous/Solvers/NumericalSolving/NewtonSolver.cs +++ b/Sources/AngouriMath/Functions/Continuous/Solvers/NumericalSolving/NewtonSolver.cs @@ -42,18 +42,10 @@ NumericsComplex ChooseGood() => { if (i == precision - 1) prev = value;//.Copy(); - try // TODO: remove try catch in for - { - - var dfv = df.Substitute(value); - if (dfv == 0) - return ChooseGood(); - value -= f.Substitute(value) / dfv; - } - catch (OverflowException) - { + var dfv = df.Substitute(value); + if (dfv == 0) return ChooseGood(); - } + value -= f.Substitute(value) / dfv; if (i > minCheckIters && prev == value) return value; } @@ -146,9 +138,7 @@ private static IEnumerable RealSignChanges( // intermediate value theorem has nothing to say about a complex value. double? RealValueAt(double at) { - NumericsComplex value; - try { value = f.Call(new NumericsComplex(at, 0)); } - catch (System.Exception) { return null; } + var value = f.Call(new NumericsComplex(at, 0)); if (double.IsNaN(value.Real) || double.IsInfinity(value.Real) || double.IsNaN(value.Imaginary) || double.IsInfinity(value.Imaginary)) return null; @@ -215,10 +205,14 @@ private static bool AreTheSameRoot(Complex a, Complex b) /// private static HashSet OnePerRoot(HashSet roots, FastExpression f) { + // A residual that is not a finite number ranks last. EDecimal Residual(Complex root) { - try { return f.Call(root.ToNumerics()).ToNumber().Abs().EDecimal; } - catch (System.Exception) { return EDecimal.PositiveInfinity; } + var residual = f.Call(root.ToNumerics()); + return double.IsNaN(residual.Real) || double.IsInfinity(residual.Real) + || double.IsNaN(residual.Imaginary) || double.IsInfinity(residual.Imaginary) + ? EDecimal.PositiveInfinity + : residual.ToNumber().Abs().EDecimal; } var kept = new List(); foreach (var root in roots @@ -277,14 +271,9 @@ private static Entity WithoutConditions(Entity expr) /// private static bool Satisfies(Entity expr, Entity.Variable v, Complex root) { - try - { - return expr.Substitute(v, root).Evaled is not Complex residual - || !residual.IsFinite - || residual.Abs().EDecimal.LessThan(MathS.Settings.PrecisionErrorCommon); - } - catch (Core.Exceptions.AngouriBugException) { throw; } - catch (System.Exception) { return true; } + return expr.Substitute(v, root).Evaled is not Complex residual + || !residual.IsFinite + || residual.Abs().EDecimal.LessThan(MathS.Settings.PrecisionErrorCommon); } } }