Repository navigation
Differentiate through an argument that is constant by its spelling without a NaN - #2760
Open
Rafael-SOWNet wants to merge 1 commit into
Open
Rafael-SOWNet wants to merge 1 commit into
Rafael-SOWNet wants to merge 1 commit into
Conversation
…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 Report❌ Patch coverage is
Additional details and impacted files@@ 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
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What was wrong
The chain rule writes
f'(u) u'. Whenuis constant but written with the variable, as inx - x,x / xor1 - sin(x)^2 - cos(x)^2, thenu'is 0 butf'(u)can have no value. That happens forsqrtat 0, forarcsinandarccosat 1, forabsandsgnat 0, and forfloor,ceilandroundwhere they jump. Multiplying by zero kept the factor's condition, and that condition is false everywhere. Sosqrt(x - x) + x^2differentiated to2 * 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'.InnerSimplifiedis 0, or0 provided P, which is kept with its condition; orIntervalEvaluation.Over) fails to showu'nonzero, andu'.Simplify()then gives 0.A
u'that is zero only at some points keeps the term, sosqrt((x - 7/10)^2)is still NaN at 7/10. The check runs only whenumentions 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.diffat 50 digits, and all match.derive::x::sqrt(x - x) + x^2NaN2 * xsqrt(x * 0) + xNaN1abs(x - x) + xNaN1arccos(x / x) + xNaN1 provided not x = 0sqrt(1 - sin(x)^2 - cos(x)^2) + x^2NaN2 * xsgn(x - x) + x,floor(x - x) + x,arcsin(cos(x - x)) + x,(x - x)^(1/3) + xNaN1arcsec(x / x) + x,sqrt(x) * sqrt(x - x) + xNaN1 provided not x = 0sqrt(floor(x)) + x1 provided not floor(x) = 0 and not x in ZZ1 provided not x in ZZln(x - x),1 / (x - x),21^NaNNaNNaNsqrt(x * floor(x)) + xDifferentiateper call (best of 7 × 2000, two runs each):x + 3 + arccos(x + 2) / sqrt(x2 + 1), the gated Kernel inputsqrt(sqrt(x + 1) + x) * arcsin(x / 2) + abs(sin(x))sqrt(x^2 + 1) * arcsin(x^2 - x)The second and third inputs pay for the check: their argument mentions
xtwice. The gated input allocates exactly what it did.sqrt(x * floor(x)) + xis left open. Thereu' = 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