Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix bug in inv(::Dual)
  • Loading branch information
dlfivefifty committed Dec 16, 2016
commit 642388fca88cd8edb02c901b275a489c16b92022
6 changes: 6 additions & 0 deletions src/dual.jl
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ mod(z::Dual, n::Number) = Dual(mod(value(z), n), epsilon(z))
NaNMath.pow(z::Dual, n::Number) = Dual(NaNMath.pow(value(z),n), epsilon(z)*n*NaNMath.pow(value(z),n-1))
NaNMath.pow(z::Number, w::Dual) = Dual(NaNMath.pow(z,value(w)), epsilon(w)*NaNMath.pow(z,value(w))*log(z))

inv(z::Dual) = dual(inv(value(z)),-epsilon(z)/value(z)^2)

# force use of NaNMath functions in derivative calculations
function to_nanmath(x::Expr)
if x.head == :call
Expand All @@ -270,9 +272,13 @@ function to_nanmath(x::Expr)
end
to_nanmath(x) = x




for (funsym, exp) in Calculus.symbolic_derivatives_1arg()
funsym == :exp && continue
funsym == :abs2 && continue
funsym == :inv && continue
@eval function $(funsym)(z::Dual)
x = value(z)
xp = epsilon(z)
Expand Down
4 changes: 4 additions & 0 deletions test/automatic_differentiation_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ a = angle(z)

@test angle(Dual(0.0+im,0.0+im)) == π/2


# check bug in inv
@test inv(dual(1.0+1.0im,1.0)) === 1/dual(1.0+1.0im,1.0) === dual(1.0+1.0im,1.0)^(-1)

#
# Tests limit definition. Let z = a + b ɛ, where a and b ∈ C.
#
Expand Down