From 1c51b464bfeb3f1dbf333cf8882d5eca4409896a Mon Sep 17 00:00:00 2001 From: jishnub Date: Fri, 30 Apr 2021 17:18:54 +0400 Subject: [PATCH 1/3] implement complex(::Type{<:Dual}) --- src/dual.jl | 1 + test/runtests.jl | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/src/dual.jl b/src/dual.jl index 0812821..b4badf4 100644 --- a/src/dual.jl +++ b/src/dual.jl @@ -179,6 +179,7 @@ Base.hash(z::Dual) = (x = hash(value(z)); epsilon(z)==0 ? x : bitmix(x,hash(epsi Base.float(z::Union{Dual{T}, Dual{Complex{T}}}) where {T<:AbstractFloat} = z Base.complex(z::Dual{<:Complex}) = z +Base.complex(::Type{Dual{R}}) where {R} = Dual{Complex{R}} Base.floor(z::Dual) = floor(value(z)) Base.ceil(z::Dual) = ceil(value(z)) diff --git a/test/runtests.jl b/test/runtests.jl index 9ff3071..274d906 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -9,3 +9,9 @@ using Test module TestAutomaticDifferentiation include("automatic_differentiation_test.jl") end + +@testset "complex" begin + for D in [Dual32, Dual64, Dual128] + @test typeof(complex(zero(D))) == complex(D) + end +end From 2b18535e796dbc2f49b9ef5e843f83867e68b999 Mon Sep 17 00:00:00 2001 From: jishnub Date: Fri, 30 Apr 2021 17:33:50 +0400 Subject: [PATCH 2/3] use complex function to construct type --- src/dual.jl | 2 +- test/runtests.jl | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/dual.jl b/src/dual.jl index b4badf4..c9ba3ad 100644 --- a/src/dual.jl +++ b/src/dual.jl @@ -179,7 +179,7 @@ Base.hash(z::Dual) = (x = hash(value(z)); epsilon(z)==0 ? x : bitmix(x,hash(epsi Base.float(z::Union{Dual{T}, Dual{Complex{T}}}) where {T<:AbstractFloat} = z Base.complex(z::Dual{<:Complex}) = z -Base.complex(::Type{Dual{R}}) where {R} = Dual{Complex{R}} +Base.complex(::Type{Dual{R}}) where {R} = Dual{complex(R)} Base.floor(z::Dual) = floor(value(z)) Base.ceil(z::Dual) = ceil(value(z)) diff --git a/test/runtests.jl b/test/runtests.jl index 274d906..e782bf2 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -11,7 +11,8 @@ include("automatic_differentiation_test.jl") end @testset "complex" begin - for D in [Dual32, Dual64, Dual128] + for T in [Float16, Float32, Float64, BigFloat, Int8, Int16, Int32, Int64, Int128, BigInt, Bool] + D = Dual{T} @test typeof(complex(zero(D))) == complex(D) end end From 3a3c45fdf64d34fd2745245748e0c952bb9904b0 Mon Sep 17 00:00:00 2001 From: jishnub Date: Fri, 30 Apr 2021 17:35:55 +0400 Subject: [PATCH 3/3] add tests for complex types --- test/runtests.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/runtests.jl b/test/runtests.jl index e782bf2..940337a 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -14,5 +14,7 @@ end for T in [Float16, Float32, Float64, BigFloat, Int8, Int16, Int32, Int64, Int128, BigInt, Bool] D = Dual{T} @test typeof(complex(zero(D))) == complex(D) + D = Dual{Complex{T}} + @test typeof(complex(zero(D))) == complex(D) end end