From 3f3df9169ef468286e983fd7589f6b9bbc978ae9 Mon Sep 17 00:00:00 2001 From: James Cook Date: Sat, 2 Mar 2024 15:16:38 +0000 Subject: [PATCH] tests pass --- src/dual.jl | 1 + test/automatic_differentiation_test.jl | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/src/dual.jl b/src/dual.jl index ad0af94..b7c2b7c 100644 --- a/src/dual.jl +++ b/src/dual.jl @@ -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)) diff --git a/test/automatic_differentiation_test.jl b/test/automatic_differentiation_test.jl index 2af2b2a..3ca7614 100644 --- a/test/automatic_differentiation_test.jl +++ b/test/automatic_differentiation_test.jl @@ -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)