Skip to content

Differentiate through an argument that is constant by its spelling without a NaN - #2760

Open
Rafael-SOWNet wants to merge 1 commit into
masterfrom
fix-2734-derivative-nan-constant-branch-point
Open

Rafael-SOWNet wants to merge 1 commit into
masterfrom
fix-2734-derivative-nan-constant-branch-point

Conversation

@Rafael-SOWNet

Copy link
Copy Markdown
Member

What was wrong

The chain rule writes f'(u) u'. When u is constant but written with the variable, as in x - x, x / x or 1 - sin(x)^2 - cos(x)^2, then u' is 0 but f'(u) can have no value. That happens for sqrt at 0, for arcsin and arccos at 1, for abs and sgn at 0, and for floor, ceil and round where they jump. Multiplying by zero kept the factor's condition, and that condition is false everywhere. So sqrt(x - x) + x^2 differentiated to 2 * x provided not x - x = 0, which is NaN after Simplify.

The fix

Those rules now return zero when u' is proved zero identically:

  • u'.InnerSimplified is 0, or 0 provided P, which is kept with its condition; or
  • an interval evaluation at two points (IntervalEvaluation.Over) fails to show u' nonzero, and u'.Simplify() then gives 0.

A u' that is zero only at some points keeps the term, so sqrt((x - 7/10)^2) is still NaN at 7/10. The check runs only when u mentions the variable twice or contains something that can flatten one occurrence: a literal 0, a step function, a sign, or re/im/arg. An argument that mentions the variable once allocates exactly what it did before.

Measured

Rows from the issue and variants of them, master 2a72476 against this branch. The derivatives at x = 0.7 were checked against mpmath.diff at 50 digits, and all match.

derive::x:: master branch
sqrt(x - x) + x^2 NaN 2 * x
sqrt(x * 0) + x NaN 1
abs(x - x) + x NaN 1
arccos(x / x) + x NaN 1 provided not x = 0
sqrt(1 - sin(x)^2 - cos(x)^2) + x^2 NaN 2 * x
sgn(x - x) + x, floor(x - x) + x, arcsin(cos(x - x)) + x, (x - x)^(1/3) + x NaN 1
arcsec(x / x) + x, sqrt(x) * sqrt(x - x) + x NaN 1 provided not x = 0
sqrt(floor(x)) + x 1 provided not floor(x) = 0 and not x in ZZ 1 provided not x in ZZ
ln(x - x), 1 / (x - x), 21^NaN NaN NaN
sqrt(x * floor(x)) + x unchanged, see below
  • Derivative sweep, the 4500 generated rows that found this: 31 rows improved: 23 went from NaN/unchecked to right, 7 from declined to right, and 1 from timeout to right. No row got worse, and the 26 WRONG rows (other issues) are identical.
  • Harnesses: PropCheck 1355 checks, 0 failures. SimpSweep 10463/10463. CasBench 120/121, 0 wrong. All three are the same as master.
  • Unit suite: 22355 passed, 0 failed.
  • Differentiate per call (best of 7 × 2000, two runs each):
input master branch
x + 3 + arccos(x + 2) / sqrt(x2 + 1), the gated Kernel input 24488 B, 9.0–13.8 µs 24488 B, 10.3–16.6 µs
sqrt(sqrt(x + 1) + x) * arcsin(x / 2) + abs(sin(x)) 40520 B, 16–18 µs 45104 B, 19–21 µs
sqrt(x^2 + 1) * arcsin(x^2 - x) 24576 B, 7.6–8.0 µs 27008 B, 9.5–9.9 µs

The second and third inputs pay for the check: their argument mentions x twice. The gated input allocates exactly what it did.

sqrt(x * floor(x)) + x is left open. There u' = floor(x) + x * (0 provided not x in ZZ) is zero on (0, 1) but not identically, so the answer evaluates to NaN only on that interval. It needs a piecewise answer, not the identical-zero rule this PR adds, and it stays open on the issue.

Part of #2734

🤖 Generated with Claude Code

…thout a NaN

The chain rule writes f'(u) u'. Where u is constant but written with the
variable -- x - x, x / x, 1 - sin(x)^2 - cos(x)^2 -- u' is 0, but f'(u) can
have no value: sqrt's at 0, arccos's and arcsin's at 1, the modulus's and the
sign's at 0, a step's at an integer. Zero times that factor keeps the factor's
condition, which is false everywhere, so sqrt(x - x) + x^2 differentiated to
2 x provided not x - x = 0, NaN after Simplify.

Those rules now return zero where u' is proved zero identically: its
InnerSimplified is 0 (or 0 provided a condition, kept), or it simplifies to 0
after an interval evaluation at two points has failed to show it nonzero. A u'
zero only at some points keeps the term. The proof is asked only where u
mentions the variable twice or holds what can flatten one occurrence (a zero,
a step, a part), so a derivative through an argument that mentions the
variable once allocates what it did.

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

codecov Bot commented Dec 6, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 83.63636% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.69%. Comparing base (2a72476) to head (154a8d2).
⚠️ Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
...ngouriMath/Functions/Continuous/Differentiation.cs 83.63% 2 Missing and 7 partials ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2760      +/-   ##
==========================================
+ Coverage   86.68%   86.69%   +0.01%     
==========================================
  Files         346      346              
  Lines       76971    77106     +135     
  Branches    22648    22694      +46     
==========================================
+ Hits        66720    66850     +130     
- Misses       5523     5534      +11     
+ Partials     4728     4722       -6     
Flag Coverage Δ
cpp 37.77% <ø> (ø)
csharp 87.18% <83.63%> (+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 Δ
...ngouriMath/Functions/Continuous/Differentiation.cs 87.44% <83.63%> (+0.33%) ⬆️

... and 10 files with indirect coverage changes

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

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

1 participant