Skip to content
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
36 changes: 36 additions & 0 deletions src/tensors/abstracttensor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,42 @@ function Base.isapprox(t1::AbstractTensorMap, t2::AbstractTensorMap;
end
end

# Complex, real and imaginary
#----------------------------
function Base.complex(t::AbstractTensorMap)
if scalartype(t) <: Complex
return t
else
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

function Base.real(t::AbstractTensorMap)
if scalartype(t) <: Real
return t
else
tr = similar(t, real(scalartype(t)))
for (c, b) in blocks(t)
block(tr, c) .= real(b)
end
return tr
end
end
function Base.imag(t::AbstractTensorMap)
if scalartype(t) <: Real
return zerovector(t)
else
ti = similar(t, real(scalartype(t)))
for (c, b) in blocks(t)
block(ti, c) .= imag(b)
end
return ti
end
end

# Conversion to Array:
#----------------------
# probably not optimized for speed, only for checking purposes
Expand Down
45 changes: 11 additions & 34 deletions src/tensors/tensor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ struct TensorMap{T,S<:IndexSpace,N₁,N₂,A<:DenseVector{T}} <: AbstractTensorM
N₁,N₂,
A<:DenseVector{T}}
T ⊆ field(S) || @warn("scalartype(data) = $T ⊈ $(field(S)))", maxlog = 1)
I = sectortype(S)
T <: Real && !(sectorscalartype(I) <: Real) &&
@warn("Tensors with real data might be incompatible with sector type $I",
maxlog = 1)
return new{T,S,N₁,N₂,A}(data, space)
end
end
Expand Down Expand Up @@ -385,17 +389,6 @@ end
#-----------------------------
Base.copy(t::TensorMap) = typeof(t)(copy(t.data), t.space)

function Base.complex(t::AbstractTensorMap)
if scalartype(t) <: Complex
return t
else
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
#------------------------------------------------------------------
function Base.convert(::Type{Dict}, t::AbstractTensorMap)
Expand Down Expand Up @@ -578,29 +571,13 @@ function Base.show(io::IO, t::TensorMap)
end
end

# Real and imaginary parts
#---------------------------
function Base.real(t::AbstractTensorMap)
# `isreal` for a `Sector` returns true iff the F and R symbols are real. This guarantees
# that the real/imaginary part of a tensor `t` can be obtained by just taking
# real/imaginary part of the degeneracy data.
if isreal(sectortype(t))
return TensorMap(real(t.data), codomain(t), domain(t))
Copy link
Member Author

Choose a reason for hiding this comment

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

So this one slipped through in the previous change, as it should actually also only work for TensorMap arguments. That one is not entirely straightforward to define for AbstractTensorMap.

else
msg = "`real` has not been implemented for `$(typeof(t))`."
throw(ArgumentError(msg))
end
end

function Base.imag(t::AbstractTensorMap)
# `isreal` for a `Sector` returns true iff the F and R symbols are real. This guarantees
# that the real/imaginary part of a tensor `t` can be obtained by just taking
# real/imaginary part of the degeneracy data.
if isreal(sectortype(t))
return TensorMap(imag(t.data), codomain(t), domain(t))
else
msg = "`imag` has not been implemented for `$(typeof(t))`."
throw(ArgumentError(msg))
# Complex, real and imaginary parts
#-----------------------------------
for f in (:real, :imag, :complex)
@eval begin
function Base.$f(t::TensorMap)
return TensorMap($f(t.data), space(t))
end
end
end

Expand Down