Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicit derivatives for complex analytic functions #727

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
61 changes: 60 additions & 1 deletion src/dual.jl
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,30 @@
val = $Mf(x)
deriv = $(DiffRules.diffrule(M, f, :x))
end)
return quote
real_diff_exp = quote
@inline function $M.$f(d::$FD.Dual{T}) where T
x = $FD.value(d)
$work
return $FD.dual_definition_retval(Val{T}(), val, deriv, $FD.partials(d))
end
end
if (M, f) in ((:Base, :abs), (:Base, :abs2), (:Base, :inv))
return real_diff_exp
else
complex_diff_expr = quote
@inline function $M.$f(d::Complex{<:$FD.Dual{T}}) where{T}
x = complex($FD.value(real(d)), $FD.value(imag(d)))
$work
re_deriv, im_deriv = reim(deriv)
re_partials = $FD.partials(real(d))
im_partials = $FD.partials(imag(d))
re_retval = $FD.dual_definition_retval(Val{T}(), real(val), re_deriv, re_partials, -im_deriv, im_partials)
im_retval = $FD.dual_definition_retval(Val{T}(), imag(val), im_deriv, re_partials, re_deriv, im_partials)
return complex(re_retval, im_retval)
end
end
return Expr(:block, real_diff_exp, complex_diff_expr)
end
end

function binary_dual_definition(M, f)
Expand Down Expand Up @@ -709,6 +726,22 @@
Dual{Tz}(muladd(x, y, value(z)), partials(z)) # z_body
)

# inv(Complex) #
#--------#

function Base.inv(d::Complex{<:Dual{T}}) where{T}
FD = ForwardDiff
x = complex(FD.value(real(d)), FD.value(imag(d)))
val = inv(x)
deriv = - val * val
re_deriv, im_deriv = reim(deriv)
re_partials = FD.partials(real(d))
im_partials = FD.partials(imag(d))
re_retval = FD.dual_definition_retval(Val{T}(), real(val), re_deriv, re_partials, -im_deriv, im_partials)
im_retval = FD.dual_definition_retval(Val{T}(), imag(val), im_deriv, re_partials, re_deriv, im_partials)
return complex(re_retval, im_retval)

Check warning on line 742 in src/dual.jl

View check run for this annotation

Codecov / codecov/patch

src/dual.jl#L732-L742

Added lines #L732 - L742 were not covered by tests
end

# sin/cos #
#--------#

Expand All @@ -727,6 +760,32 @@
return (Dual{T}(sd, cd * partials(d)), Dual{T}(cd, -sd * partials(d)))
end

function Base.sin(d::Complex{<:Dual{T}}) where{T}
FD = ForwardDiff
x = complex(FD.value(real(d)), FD.value(imag(d)))
val = sin(x)
deriv = cos(x)
re_deriv, im_deriv = reim(deriv)
re_partials = FD.partials(real(d))
im_partials = FD.partials(imag(d))
re_retval = FD.dual_definition_retval(Val{T}(), real(val), re_deriv, re_partials, -im_deriv, im_partials)
im_retval = FD.dual_definition_retval(Val{T}(), imag(val), im_deriv, re_partials, re_deriv, im_partials)
return complex(re_retval, im_retval)

Check warning on line 773 in src/dual.jl

View check run for this annotation

Codecov / codecov/patch

src/dual.jl#L763-L773

Added lines #L763 - L773 were not covered by tests
end

function Base.cos(d::Complex{<:Dual{T}}) where{T}
FD = ForwardDiff
x = complex(FD.value(real(d)), FD.value(imag(d)))
val = cos(x)
deriv = -sin(x)
re_deriv, im_deriv = reim(deriv)
re_partials = FD.partials(real(d))
im_partials = FD.partials(imag(d))
re_retval = FD.dual_definition_retval(Val{T}(), real(val), re_deriv, re_partials, -im_deriv, im_partials)
im_retval = FD.dual_definition_retval(Val{T}(), imag(val), im_deriv, re_partials, re_deriv, im_partials)
return complex(re_retval, im_retval)

Check warning on line 786 in src/dual.jl

View check run for this annotation

Codecov / codecov/patch

src/dual.jl#L776-L786

Added lines #L776 - L786 were not covered by tests
end

# sincospi #
#----------#

Expand Down
7 changes: 7 additions & 0 deletions test/DerivativeTest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,11 @@ end
@test ForwardDiff.derivative(x -> (1+im)*x, 0) == (1+im)
end

@testset "analytic functions" begin
dexp(x) = ForwardDiff.derivative(y -> exp(complex(0, y)), x)
@test ForwardDiff.derivative(dexp, 0.0) ≈ -1
@test ForwardDiff.derivative(x -> exp(1im*x), 0.7) ≈ im * cis(0.7)
@test ForwardDiff.derivative(x -> sqrt(im + (1+im) * x), 1.23) ≈ (1+im) / (2 * sqrt(im + (1+im)*1.23))
end

end # module
Loading