Repository navigation
Add a rule for three-argument hypot - #123
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
So why add it? |
|
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 |
devmotion
left a comment
There was a problem hiding this comment.
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 sincehypot(::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.
|
Thanks for the careful review! Done: the generic |
|
Done, thanks: |
Adds the three-argument case of the existing binary rule: the derivatives of
hypot(x, y, z)arex / hypot(x, y, z),y / hypot(x, y, z)andz / hypot(x, y, z).It is the first active ternary rule, so the
arity == 3branch 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 definesderivative_rulemethods for it, which are not reached in practice sincehypot(::Num, ::Num, ::Num)expands through Base's implementation.