Skip to content

Commit 882c1eb

Browse files
Newton's solver, the spurious-root filter and Gruntz without catch-alls (#1568)
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 <noreply@anthropic.com>
1 parent e9aee49 commit 882c1eb

3 files changed

Lines changed: 21 additions & 40 deletions

File tree

‎Sources/AngouriMath/Functions/Continuous/Limits/Gruntz/Gruntz.cs‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,7 @@ private static void Trace(string what, object? value)
7979
if (depth > 0)
8080
return null; // already inside; the caller is the entry
8181
expr = AsExponentials(expr, x);
82-
try { return LimitInf(expr, x); }
83-
catch (Core.Exceptions.AngouriBugException) { throw; }
84-
catch (OperationCanceledException) { throw; }
85-
catch (Exception) { return null; }
82+
return LimitInf(expr, x);
8683
}
8784

8885
private static Entity? LimitInf(Entity e, Variable x)

‎Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs‎

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -123,18 +123,13 @@ private static bool IsSpurious(Entity equation, Variable x, Entity root)
123123
return true;
124124
if (root.Vars.Any())
125125
return false;
126-
try
127-
{
128-
var substituted = equation.Substitute(x, root);
129-
if (substituted.Evaled is not Number.Complex residual || !residual.IsFinite)
130-
return false;
131-
var size = residual.Abs().EDecimal;
132-
if (size.LessThan(MathS.Settings.PrecisionErrorCommon))
133-
return false;
134-
return size.GreaterThan(LargestTerm(substituted).Multiply(RelativeResidualTolerance));
135-
}
136-
catch (Core.Exceptions.AngouriBugException) { throw; }
137-
catch (System.Exception) { return false; }
126+
var substituted = equation.Substitute(x, root);
127+
if (substituted.Evaled is not Number.Complex residual || !residual.IsFinite)
128+
return false;
129+
var size = residual.Abs().EDecimal;
130+
if (size.LessThan(MathS.Settings.PrecisionErrorCommon))
131+
return false;
132+
return size.GreaterThan(LargestTerm(substituted).Multiply(RelativeResidualTolerance));
138133
}
139134

140135
/// <summary>

‎Sources/AngouriMath/Functions/Continuous/Solvers/NumericalSolving/NewtonSolver.cs‎

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,10 @@ NumericsComplex ChooseGood() =>
4242
{
4343
if (i == precision - 1)
4444
prev = value;//.Copy();
45-
try // TODO: remove try catch in for
46-
{
47-
48-
var dfv = df.Substitute(value);
49-
if (dfv == 0)
50-
return ChooseGood();
51-
value -= f.Substitute(value) / dfv;
52-
}
53-
catch (OverflowException)
54-
{
45+
var dfv = df.Substitute(value);
46+
if (dfv == 0)
5547
return ChooseGood();
56-
}
48+
value -= f.Substitute(value) / dfv;
5749
if (i > minCheckIters && prev == value)
5850
return value;
5951
}
@@ -146,9 +138,7 @@ private static IEnumerable<NumericsComplex> RealSignChanges(
146138
// intermediate value theorem has nothing to say about a complex value.
147139
double? RealValueAt(double at)
148140
{
149-
NumericsComplex value;
150-
try { value = f.Call(new NumericsComplex(at, 0)); }
151-
catch (System.Exception) { return null; }
141+
var value = f.Call(new NumericsComplex(at, 0));
152142
if (double.IsNaN(value.Real) || double.IsInfinity(value.Real)
153143
|| double.IsNaN(value.Imaginary) || double.IsInfinity(value.Imaginary))
154144
return null;
@@ -215,10 +205,14 @@ private static bool AreTheSameRoot(Complex a, Complex b)
215205
/// </summary>
216206
private static HashSet<Complex> OnePerRoot(HashSet<Complex> roots, FastExpression f)
217207
{
208+
// A residual that is not a finite number ranks last.
218209
EDecimal Residual(Complex root)
219210
{
220-
try { return f.Call(root.ToNumerics()).ToNumber().Abs().EDecimal; }
221-
catch (System.Exception) { return EDecimal.PositiveInfinity; }
211+
var residual = f.Call(root.ToNumerics());
212+
return double.IsNaN(residual.Real) || double.IsInfinity(residual.Real)
213+
|| double.IsNaN(residual.Imaginary) || double.IsInfinity(residual.Imaginary)
214+
? EDecimal.PositiveInfinity
215+
: residual.ToNumber().Abs().EDecimal;
222216
}
223217
var kept = new List<Complex>();
224218
foreach (var root in roots
@@ -277,14 +271,9 @@ private static Entity WithoutConditions(Entity expr)
277271
/// </returns>
278272
private static bool Satisfies(Entity expr, Entity.Variable v, Complex root)
279273
{
280-
try
281-
{
282-
return expr.Substitute(v, root).Evaled is not Complex residual
283-
|| !residual.IsFinite
284-
|| residual.Abs().EDecimal.LessThan(MathS.Settings.PrecisionErrorCommon);
285-
}
286-
catch (Core.Exceptions.AngouriBugException) { throw; }
287-
catch (System.Exception) { return true; }
274+
return expr.Substitute(v, root).Evaled is not Complex residual
275+
|| !residual.IsFinite
276+
|| residual.Abs().EDecimal.LessThan(MathS.Settings.PrecisionErrorCommon);
288277
}
289278
}
290279
}

0 commit comments

Comments
 (0)