Skip to content

Commit f218222

Browse files
authored
Add and test 2-arg complex (#172)
1 parent fed10f6 commit f218222

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/tensors/tensor.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,9 @@ function Base.complex(t::AbstractTensorMap)
392392
return copy!(similar(t, complex(scalartype(t))), t)
393393
end
394394
end
395+
function Base.complex(r::AbstractTensorMap{<:Real}, i::AbstractTensorMap{<:Real})
396+
return add(r, i, im * one(scalartype(i)))
397+
end
395398

396399
# Conversion between TensorMap and Dict, for read and write purpose
397400
#------------------------------------------------------------------

test/tensors.jl

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,21 @@ for V in spacelist
185185
W = V1 V2
186186
for T in (Float64, ComplexF64, ComplexF32)
187187
t = @constinferred randn(T, W, W)
188-
@test real(convert(Array, t)) == convert(Array, @constinferred real(t))
189-
@test imag(convert(Array, t)) == convert(Array, @constinferred imag(t))
188+
189+
tr = @constinferred real(t)
190+
@test scalartype(tr) <: Real
191+
@test real(convert(Array, t)) == convert(Array, tr)
192+
193+
ti = @constinferred imag(t)
194+
@test scalartype(ti) <: Real
195+
@test imag(convert(Array, t)) == convert(Array, ti)
196+
197+
tc = @inferred complex(t)
198+
@test scalartype(tc) <: Complex
199+
@test complex(convert(Array, t)) == convert(Array, tc)
200+
201+
tc2 = @inferred complex(tr, ti)
202+
@test tc2 tc
190203
end
191204
end
192205
end

0 commit comments

Comments
 (0)