Skip to content

Add a rule for three-argument hypot - #123

Merged
devmotion merged 3 commits into
JuliaDiff:masterfrom
Fastaxx:hypot3
Nov 13, 2026
Merged

devmotion merged 3 commits into
JuliaDiff:masterfrom
Fastaxx:hypot3

Conversation

@Fastaxx

@Fastaxx Fastaxx commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Adds the three-argument case of the existing binary rule: the derivatives of hypot(x, y, z) are x / hypot(x, y, z), y / hypot(x, y, z) and z / hypot(x, y, z).

It is the first active ternary rule, so the arity == 3 branch of the "check rules" test loop is re-enabled and covers it. ForwardDiff, ReverseDiff and Tracker only handle arities 1 and 2 and skip it. Symbolics now defines derivative_rule methods for it, which are not reached in practice since hypot(::Num, ::Num, ::Num) expands through Base's implementation.

@codecov

codecov Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.96%. Comparing base (ab1d7ef) to head (1d1768d).
⚠️ Report is 8 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #123      +/-   ##
==========================================
+ Coverage   95.97%   97.96%   +1.99%     
==========================================
  Files           2        3       +1     
  Lines         174      197      +23     
==========================================
+ Hits          167      193      +26     
+ Misses          7        4       -3     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

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

@devmotion

Copy link
Copy Markdown
Member

ForwardDiff skips ternary rules and defines its own ternary hypot, and Symbolics differentiates hypot(x, y, z) through Base's implementation, so neither changes.

So why add it?

@Fastaxx

Fastaxx commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor Author

We are replacing StencilAlgebra.jl's long in-house table of derivative rules with DiffRules : Our builds symbolic derivative formulas from DiffRules' expressions at every arity, and hypot(x, y, z) is common in its formulas: with this rule its derivative comes from DiffRules instead of a rule of its own. If you prefer to keep DiffRules to what the AD packages use, I understand and can close it.

@devmotion devmotion left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! The rule itself looks correct: it matches the binary rule, the derivatives are bounded by 1 in magnitude so there's no over-/underflow (also for inputs around 1e-300/1e300), and Float32 is preserved. At the origin it returns NaN, consistent with the binary rule.

Note that this is the first non-commented ternary rule, so diffrules() returns an arity-3 entry for the first time. I checked downstream packages:

  • ForwardDiff (master) and ReverseDiff only handle arity 1 and 2 and silently skip the new rule; gradients of hypot(x, y, z) are unchanged and correct. Tracker also only handles arity 1 and 2.
  • JuliaDiff/ForwardDiff.jl#872 skips (:Base, :hypot) for all arities, so it is unaffected as well.
  • Symbolics loops over all arities and will now define derivative_rule(::typeof(hypot), ::Val{3}, args, ::Val{i}) methods (loading works). They are not hit in practice since hypot(::Num, ::Num, ::Num) expands via Base's generic implementation. So the sentence in the PR description that Symbolics doesn't change is not quite accurate.

My main comment is about the tests, see below.

Comment thread test/runtests.jl Resolved
@Fastaxx

Fastaxx commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the careful review! Done: the generic arity == 3 branch of the "check rules" loop is re-enabled, written like the arity == 2 one (all three derivatives and their types are checked, no offset), and the separate testset is gone. hypot is now covered by the loop, and ifelse is still excluded through non_diffeable_arg_functions. 1346/1346 tests pass locally. I also corrected the sentence about Symbolics in the description.

Comment thread test/runtests.jl Resolved
Comment thread test/runtests.jl Resolved
Comment thread test/runtests.jl Resolved
Comment thread test/runtests.jl Resolved
@Fastaxx

Fastaxx commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

Done, thanks: atol dropped on the three ternary checks, and the stray blank line removed. Tests pass locally (1346/1346)

@devmotion devmotion left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@devmotion
devmotion merged commit db14b02 into JuliaDiff:master Nov 13, 2026
10 of 12 checks passed
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.

2 participants