Skip to content

Commit

Permalink
Add and test 2-arg complex
Browse files Browse the repository at this point in the history
  • Loading branch information
lkdvos committed Nov 10, 2024
1 parent 426b1ab commit 24c7636
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/tensors/tensor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,9 @@ function Base.complex(t::AbstractTensorMap)
return copy!(similar(t, complex(scalartype(t))), t)
end
end
function Base.complex(r::AbstractTensorMap{<:Real}, i::AbstractTensorMap{<:Real})
return add(r, i, im * one(scalartype(i)))
end

# Conversion between TensorMap and Dict, for read and write purpose
#------------------------------------------------------------------
Expand Down
17 changes: 15 additions & 2 deletions test/tensors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,21 @@ for V in spacelist
W = V1 V2
for T in (Float64, ComplexF64, ComplexF32)
t = @constinferred randn(T, W, W)
@test real(convert(Array, t)) == convert(Array, @constinferred real(t))
@test imag(convert(Array, t)) == convert(Array, @constinferred imag(t))

tr = @constinferred real(t)
@test scalartype(tr) <: Real
@test real(convert(Array, t)) == convert(Array, tr)

ti = @constinferred imag(t)
@test scalartype(ti) <: Real
@test imag(convert(Array, t)) == convert(Array, ti)

tc = @inferred complex(t)
@test scalartype(tc) <: Complex
@test complex(convert(Array, t)) == convert(Array, tc)

tc2 = @inferred complex(tr, ti)
@test tc2 tc
end
end
end
Expand Down

0 comments on commit 24c7636

Please sign in to comment.