Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add and test 2-arg complex #172

Merged
merged 1 commit into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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