Skip to content

Make branching and hypot-based rules stable in higher-order derivatives - #124

Draft
devmotion wants to merge 1 commit into
dw/higher-order-stability-basicfrom
dw/higher-order-stability
Draft

devmotion wants to merge 1 commit into
dw/higher-order-stability-basicfrom
dw/higher-order-stability

Conversation

@devmotion

@devmotion devmotion commented Dec 4, 2026 •

Copy link
Copy Markdown
Member

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 new hypot rule. The old hypot partial x / hypot(x, 1) differentiates to (h - x·x/h) / h², which cancels to exactly 0 for large x.

Rule Problem New form
hypot (2 and 3 args) cancellation, e.g. ∂²/∂x² is 0 instead of 1e-16 at (1, 1e-8); a factor-of-2 error at (1e200, 1e200) rescaled by the argument of largest magnitude, branching with ifelse
asin, acos, asind, acosd, atanh, asech, NaNMath asin/acos/atanh cancellation near 0 in the AD derivative of (1 - x) * (1 + x) ifelse(abs(x) < 0.5, 1 - x^2, (1 - x) * (1 + x)) (fma would be exact, but it is not defined for all Reals)
atan, acot, atand, acotd, acsch 2nd/3rd order underflow to 0 for |x| ≳ 1e77 e.g. abs2(inv(hypot(one(x), x)))
asinh first derivative 0 for |x| > 1.3e154 inv(hypot(x, one(x)))
atan(x, y) first derivative 0 for huge or tiny arguments y / hypot(x, y) / hypot(x, y)
log1pmx, logmxp1 ~400 ε at 2nd/3rd order for large x branch for large x
logsubexp -0/NaN at 2nd/3rd order for x − y ≪ 0 exp(z) / expm1(z), branching on the sign of z = x − y

The Base rules branch with ifelse instead of ?: and && because comparisons on symbolic arguments (Symbolics.jl) do not return a Bool. one(x) is used in hypot(one(x), x) because hypot(1, 1f0) is a Float64 on 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 hypot in Float32 at (1e-20, 1e-21), where the true value is ≈ 3e38, and two mixed 2nd derivatives of atan(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.

  • Symbolics test/diff.jl:156 expects the old hypot derivative expression, and forwarddiff_symbolic_dual_ops.jl:73 compares two expressions with isequal that differ only in whether abs(2.0) was simplified.
  • ModelingToolkit sdesystem.jl:194 and :200 compare with == against 1 / (1 + u0^2). The new atan derivative 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, logerfcx and dawson at large x
  • ellipk and ellipe at small m
  • the scaled Airy and Bessel functions at large x
  • beta and logbeta when b ≪ a

🤖 Generated with Claude Code

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
devmotion force-pushed the dw/higher-order-stability branch from c42d9c4 to 7b3a18e Compare December 5, 2026 20:43
@devmotion devmotion changed the title Make rules numerically stable in higher-order derivatives Make branching and hypot-based rules stable in higher-order derivatives Dec 5, 2026
@devmotion
devmotion changed the base branch from master to dw/higher-order-stability-basic December 5, 2026 20:51

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

None yet

Development

Successfully merging this pull request may close these issues.

1 participant