Skip to content

Commit faf7e6c

Browse files
authored
Add overloads of complex for One and Zero (#194)
* Add overloads for complex * Test complex overloads * Increment version number
1 parent f0a16bd commit faf7e6c

File tree

4 files changed

+19
-1
lines changed

4 files changed

+19
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ChainRulesCore"
22
uuid = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
3-
version = "0.9.3"
3+
version = "0.9.4"
44

55
[deps]
66
MuladdMacro = "46d2c3a1-f734-5fdb-9937-b9b9aeba4221"

src/differential_arithmetic.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ Base.imag(::Zero) = Zero()
6767
Base.real(::One) = One()
6868
Base.imag(::One) = Zero()
6969

70+
Base.complex(::Zero) = Zero()
71+
Base.complex(::Zero, ::Zero) = Zero()
72+
Base.complex(::Zero, i::Real) = complex(oftype(i, 0), i)
73+
Base.complex(r::Real, ::Zero) = complex(r)
74+
75+
Base.complex(::One) = One()
76+
Base.complex(::Zero, ::One) = im
77+
Base.complex(::One, ::Zero) = One()
78+
7079
Base.:+(a::One, b::One) = extern(a) + extern(b)
7180
Base.:*(::One, ::One) = One()
7281
for T in (:AbstractThunk, :Composite, :Any)

test/differentials/abstract_zero.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@
4949
@test reim(z) === (Zero(), Zero())
5050
@test real(z) === Zero()
5151
@test imag(z) === Zero()
52+
53+
@test complex(z) === z
54+
@test complex(z, z) === z
55+
@test complex(z, 2.0) === Complex{Float64}(0.0, 2.0)
56+
@test complex(1.5, z) === Complex{Float64}(1.5, 0.0)
5257
end
5358

5459
@testset "DoesNotExist" begin

test/differentials/one.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,8 @@
1616
@test reim(o) === (One(), Zero())
1717
@test real(o) === One()
1818
@test imag(o) === Zero()
19+
20+
@test complex(o) === o
21+
@test complex(o, Zero()) === o
22+
@test complex(Zero(), o) === im
1923
end

0 commit comments

Comments
 (0)