Skip to content

Commit

Permalink
Implement complex for dual arguments (#90)
Browse files Browse the repository at this point in the history
* Implement complex wrappers

* Test complex implementations

* Increment version number

* Overload complex for types
  • Loading branch information
sethaxen committed Mar 6, 2022
1 parent 7848bf4 commit 1036d45
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "DualNumbers"
uuid = "fa6b7ba4-c1ee-5f82-b5fc-ecf0adba8f74"
version = "0.6.6"
version = "0.6.7"

[deps]
Calculus = "49dc2e85-a5d0-5ad3-a950-438e2897f1b9"
Expand Down
7 changes: 7 additions & 0 deletions src/dual.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ dual(x::ReComp, y::ReComp) = Dual(x, y)
dual(x::ReComp) = Dual(x)
dual(z::Dual) = z

function Base.complex(x::Dual, y::Dual)
dual(complex(value(x), value(y)), complex(epsilon(x), epsilon(y)))
end
Base.complex(x::Real, y::Dual) = complex(dual(x), y)
Base.complex(x::Dual, y::Real) = complex(x, dual(y))
Base.complex(::Type{Dual{T}}) where {T} = Dual{complex(T)}

const realpart = value
const dualpart = epsilon

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 @@ -200,6 +200,14 @@ test(x, y) = x^2 + y
@test epsilon(Dual(-2.0,1.0)^Dual(2.0,0.0)) == -4


# test complex and dual mixing
@test complex(dual(1, 2), dual(3, 4)) == dual(complex(1, 3), complex(2, 4))
@test complex(1, dual(2, 3)) == dual(complex(1, 2), complex(0, 3))
@test complex(dual(1, 2), 3) == dual(complex(1, 3), complex(2, 0))
@test complex(dual(1, 2)) == dual(complex(1, 0), complex(2, 0))
@test complex(Dual128) === DualComplex256
@test complex(Dual64) === DualComplex128

# test for flipsign
flipsign(Dual(1.0,1.0),2.0) == Dual(1.0,1.0)
flipsign(Dual(1.0,1.0),-2.0) == Dual(-1.0,-1.0)
Expand Down

2 comments on commit 1036d45

@sethaxen
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/56070

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.6.7 -m "<description of version>" 1036d45b5d3b782a3f695d050e8fc226572e5289
git push origin v0.6.7

Please sign in to comment.