Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/dual.jl
Original file line number Diff line number Diff line change
Expand Up @@ -587,11 +587,18 @@ end
#-------#

@inline function calc_hypot(x, y, z, ::Type{T}) where T
pm1(x) = signbit(x) ? -1 : 1

vx = value(x)
vy = value(y)
vz = value(z)
h = hypot(vx, vy, vz)
p = (vx / h) * partials(x) + (vy / h) * partials(y) + (vz / h) * partials(z)
p = if iszero(h)
hp = sqrt(sum(abs2, partials(x)) + sum(abs2, partials(y)) + sum(abs2, partials(z)))
pm1(vx) * partials(x) / hp + pm1(vy) * partials(y) / hp + pm1(vz) * partials(z) / hp
else
(vx / h) * partials(x) + (vy / h) * partials(y) + (vz / h) * partials(z)
end
return Dual{T}(h, p)
end

Expand Down
3 changes: 3 additions & 0 deletions test/MiscTest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ let i, j
i != j && @test h[i, j] ≈ 0.0
end
end
hypotx(x) = hypot(x, 0.0, 0.0)
@test ForwardDiff.derivative(hypotx, 0.0) ≈ 1
@test ForwardDiff.derivative(hypotx, -0.0) ≈ -1

########
# misc #
Expand Down