Repository navigation
Conversation
Rules that need branches, or that are only stable together with the new `hypot` rule: - hypot (2 and 3 arguments): rescale by the argument of largest magnitude, fixing cancellation (e.g., at (1, 1e-8)) and underflow (e.g., at (1e200, 1e200)) in the derivatives - asin, acos, asind, acosd, atanh, asech, NaNMath.asin/acos/atanh: compute `1 - x^2` without cancellation near 0 and near |x| = 1 - atan, acot, atand, acotd, asinh, acsch, atan(x, y): use `hypot` to avoid over- and underflow for large |x| - log1pmx, logmxp1: avoid cancellation in the derivatives for large x - logsubexp: avoid over- and underflow for large |x - y| The Base rules branch with `ifelse` instead of `?:` and `&&` to support symbolic arguments. `hypot(1, x)` is written as `hypot(one(x), x)` to avoid promotion to Float64 on Julia < 1.12. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
devmotion
force-pushed
the
dw/higher-order-stability
branch
from
December 5, 2026 20:43
c42d9c4 to
7b3a18e
Compare
devmotion
changed the base branch from
master
to
dw/higher-order-stability-basic
December 5, 2026 20:51
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.
Stacked on #125, which should be merged first. This PR targets #125's branch, so its diff shows only the changes on top of #125.
Like #125, this makes rules stable in higher-order derivatives computed with nested forward-mode AD. This PR contains the remaining fixes: rules that need branches,
hypot, and the rules that are only stable together with the newhypotrule. The oldhypotpartialx / hypot(x, 1)differentiates to(h - x·x/h) / h², which cancels to exactly 0 for large x.hypot(2 and 3 args)ifelseasin,acos,asind,acosd,atanh,asech, NaNMathasin/acos/atanh(1 - x) * (1 + x)ifelse(abs(x) < 0.5, 1 - x^2, (1 - x) * (1 + x))(fmawould be exact, but it is not defined for allReals)atan,acot,atand,acotd,acschabs2(inv(hypot(one(x), x)))asinhinv(hypot(x, one(x)))atan(x, y)y / hypot(x, y) / hypot(x, y)log1pmx,logmxp1logsubexp-0/NaNat 2nd/3rd order for x − y ≪ 0exp(z) / expm1(z), branching on the sign of z = x − yThe Base rules branch with
ifelseinstead of?:and&&because comparisons on symbolic arguments (Symbolics.jl) do not return aBool.one(x)is used inhypot(one(x), x)becausehypot(1, 1f0)is aFloat64on Julia < 1.12.With both PRs, the accuracy check flags only three points, where the intermediate values over- or underflow no matter how the rule is written: the 3rd derivative of
hypotin Float32 at (1e-20, 1e-21), where the true value is ≈ 3e38, and two mixed 2nd derivatives ofatan(x, y)at (1e-200, 1e-200).Expected downstream failures: these tests check the exact form or the exact value of a derivative. They are not numerical errors.
test/diff.jl:156expects the oldhypotderivative expression, andforwarddiff_symbolic_dual_ops.jl:73compares two expressions withisequalthat differ only in whetherabs(2.0)was simplified.sdesystem.jl:194and:200compare with==against1 / (1 + u0^2). The newatanderivative differs from it only in the last few decimal places.Not addressed (SpecialFunctions): fixing these would need series or asymptotic expansions.
logerfc(about 100 ε at 3rd order near x ≈ 1)erfcx,logerfcxanddawsonat large xellipkandellipeat small mbetaandlogbetawhen b ≪ a🤖 Generated with Claude Code