Skip to content

Find the real roots beside a pole in the Newton search - #2678

Merged
Rafael-SOWNet merged 1 commit into
masterfrom
fix-2545-real-axis-newton
Dec 1, 2026
Merged

Rafael-SOWNet merged 1 commit into
masterfrom
fix-2545-real-axis-newton

Conversation

@Rafael-SOWNet

@Rafael-SOWNet Rafael-SOWNet commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

Part of #2545

What was wrong

When every analytical route declines, Solve answers with the roots Newton's method finds. It starts the method from a 10 x 10 grid over [-10, 10]² and from each sign change of a scan of the real line, with points 0.2 apart. A real root with no sign change between it and the next scan point was left to the grid, and the grid missed it. That happens in two ways:

  • The root is beside a pole. tan(x) = 2x is -0.34 at 4.6 and -21 at 4.8. Its root at 4.6042 and the pole at 3π/2 both lie between those two points.
  • The root is beside a point where the expression has no value. The compiled gamma is NaN at -4, so the interval [-4, -3.8] was skipped. gamma(x) = 1 has its roots at -3.9553 and -3.1436, and is below 1 at both -3.8 and -3.2. 1/gamma(x) = x/10 has its root 2.5e-6 from -9.

Re-measured on master 4674e66

Cause

RealSignChanges in Sources/AngouriMath/Functions/Continuous/Solvers/NumericalSolving/NewtonSolver.cs:417-461 on master. It yielded a start only where two adjacent scan points both had real values of opposite sign (line 447). An interval with ends of one sign, or with an end that had no value, was never looked into.

Fix

The scan now looks more closely at every interval it cannot rule out. The interval is halved when the value at its midpoint does any of these:

  • changes sign;
  • has no real value;
  • lies nearer 0 than both ends;
  • lies further from 0 than both ends.

Only the halves that qualify again are halved further, down to about 1e-8 (CloserLookDepth = 24). The extra evaluations are capped at 16 per scan point (CloserLookBudget), so an expression that oscillates without end, such as sin(1/x), cannot halve forever. Each sign change found is bisected and Newton's method is started there, as before. Every root added passes the same checks as any other: Satisfies, AreIsolated and Refined.

The 2-D grid, the conjugate step and the root verification are unchanged. Nothing outside the real-axis scan is touched.

The answer is still a set that can be short of roots, as any Newton answer can. Saying so in the answer is #1019 item 11, and this PR does not pre-empt it.

Sweep

170 generated equations, with their real roots in [-10, 10] taken from mpmath at 40 digits. The reference scans 40,000 points, refines every sign change with findroot, and also refines every local minimum of |f| so that tangent and near-tangent pairs are counted. The families:

  • a^x = x^k
  • expanded products of shifted exp, sin and polynomial factors, two and three at a time
  • sin and cos against lines and cubics
  • tan(x) = a x and x tan(x) = a
  • 1/gamma(x) = a x + b
  • sin(x) = 1/(x - p) + b and e^(x/3) = 1/(x - p) + 1/(x - q)
  • near-tangent pairs sin(x) + s x / 1000 = 0.97…0.999
  • x^2 cos x = a

A root counts as found within 1e-8 relative. A wrong root is one whose mpmath residual exceeds 1e-8.

real roots (reference) missing extra wrong total time
master 4674e66 740 112 0 0 109.0 s
this branch 740 1 0 0 129.1 s
  • The one root still missing is 0 of 1/gamma(x) = x/10. The library evaluates 1/gamma(0) as NaN, so it is not a root by the library's own evaluation.
  • 17 equations are answered by a periodic set or a set builder on both builds, and one, e^(-x) = sin(3x), does not finish on either (see below). Their roots are counted in the reference total.
  • 19 answers changed, and every one only gained true roots:
    • nine tan(x) = a x, including tan(x) = 2x;
    • x tan(x) = 28;
    • nine 1/gamma(x) = a x + b, including 1/gamma(x) = x/10.
  • Asked of the search directly, SolveNt("gamma(x) - 1") and SolveNt("x! - x") now give the same ten real roots: 1, 2, and two in each of (-10, -9), (-8, -7), (-6, -5) and (-4, -3). Each was checked by an mpmath sign change within 1e-12.

Timings

On the 150 sweep equations whose answer did not change, the total was 15.6 s on master and 14.6 s on the branch. That difference is noise: the first row carries the JIT.

The time goes up on the 19 equations that gained roots, 33.4 s to 54.6 s. Nearly all of it is in the 1/gamma rows, from about 2.5–5 s to 4.3–9.5 s each. That cost comes from verifying and refining each added root in the library's own gamma evaluation, not from the scan.

Harnesses, built from this tree and run three times each, alternating, with the clock inside the lock:

master branch
RootCheck 3.06 / 3.34 / 2.92 s 2.95 / 2.91 / 3.01 s
CasBench 1.80 / 1.81 / 1.83 s 1.80 / 1.80 / 1.81 s

Results are identical on both builds:

  • RootCheck: 596/596 clean.
  • CasBench: 120/121 solved, 0 wrong.

Tests

  • C# suite: 22026 passed, 0 failed.
  • F# suite: 135 passed.
  • New tests in SolverRegressionTest: Issue2545_ARootBesideAPoleIsAnswered and Issue2545_GammaIsOneAtEveryRealRootInTheBox. All four cases fail on master and pass here.
  • BREAKING-CHANGES.md has an entry with the 2.10.0 values, measured on the 2.10.0 package. They are the same as master's.

Not done here

e^(-x) = sin(3x) overflows the stack on master as well as here, after about 136 s. The overflow is in IsUndefinedExactly, evaluating sin at a working precision large enough that computing π recurses too deep, beneath a chain of SolveInverting frames. It is separate from this PR.

🤖 Generated with Claude Code

Newton's method is run from each sign change of a scan of the real line,
points 0.2 apart. A root with no sign change between it and the next point
was left to the 2-D grid, which missed it: tan(x) = 2x at 4.6042, beside the
pole at 3pi/2, and gamma(x) = 1 at -3.9553 and -3.1436, either side of points
where the scan reads no value. The scan now halves an interval where its
midpoint changes sign, has no real value, or lies nearer 0 or further from it
than both ends, down to about 1e-8, within a budget of 16 evaluations per scan
point, and Newton's method starts from each sign change that turns up. Every
root it adds passes the same check as before.

Of 170 generated equations with 740 real roots in [-10, 10] by mpmath, master
missed 112 and the branch misses 1 (0 of 1/gamma(x) = x/10, where gamma has
its pole and the library evaluates NaN), with no extra or wrong roots.

Part of #2545

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 86.65%. Comparing base (4674e66) to head (7c4e83b).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2678      +/-   ##
==========================================
+ Coverage   86.63%   86.65%   +0.01%     
==========================================
  Files         346      346              
  Lines       76133    76191      +58     
  Branches    22314    22335      +21     
==========================================
+ Hits        65961    66022      +61     
+ Misses       5503     5499       -4     
- Partials     4669     4670       +1     
Flag Coverage Δ
cpp 37.77% <ø> (ø)
csharp 87.14% <100.00%> (+0.01%) ⬆️
fsharp 32.79% <ø> (ø)
interactive 40.09% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...ontinuous/Solvers/NumericalSolving/NewtonSolver.cs 94.21% <100.00%> (+0.66%) ⬆️

... and 11 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Rafael-SOWNet
Rafael-SOWNet merged commit 00a9048 into master Dec 1, 2026
45 of 50 checks passed
@github-project-automation github-project-automation Bot moved this from Todo to Done in AngouriMath Dec 1, 2026
@Rafael-SOWNet
Rafael-SOWNet deleted the fix-2545-real-axis-newton branch December 1, 2026 23:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant