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

Allow a dual to be raised to a complex power. #102

Open
wants to merge 1 commit 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
1 change: 1 addition & 0 deletions src/dual.jl
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ for T1 ∈ (:Integer, :Rational, :Number)
@eval Base.:^(z::Dual{T}, n::$T1) where T = pow(z, n)
end

Base.:^(x::Dual, p::Complex) = exp((log(abs2(x))/2 + im * angle(x)) * p)

NaNMath.pow(z::Dual{T}, n::Number) where T = Dual(NaNMath.pow(value(z),n), epsilon(z)*n*NaNMath.pow(value(z),n-1))
NaNMath.pow(z::Number, w::Dual{T}) where T = Dual(NaNMath.pow(z,value(w)), epsilon(w)*NaNMath.pow(z,value(w))*log(z))
Expand Down
8 changes: 8 additions & 0 deletions test/automatic_differentiation_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ y = Dual(2.0, 1)^UInt64(0)
@test !isnan(epsilon(y))
@test epsilon(y) == 0

# test dual^complex
a, b = rand(), rand(ComplexF64)
@test realpart(Dual(a, 1)^b) ≈ a^b
@test dualpart(Dual(a, 1)^b) ≈ b * a^(b - 1)
a, b = rand(ComplexF64), rand(ComplexF64)
@test realpart(Dual(a, 1)^b) ≈ a^b
@test dualpart(Dual(a, 1)^b) ≈ b * a^(b - 1)

y = sin(x)+exp(x)
@test value(y) ≈ sin(2)+exp(2)
@test epsilon(y) ≈ cos(2)+exp(2)
Expand Down
Loading