Skip to content

Commit 378c150

Browse files
authored
small patch to complex, real and imag (#173)
* small patch to complex, real and imag * better patches to complex, real and imag
1 parent f218222 commit 378c150

File tree

2 files changed

+47
-34
lines changed

2 files changed

+47
-34
lines changed

src/tensors/abstracttensor.jl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,42 @@ function Base.isapprox(t1::AbstractTensorMap, t2::AbstractTensorMap;
451451
end
452452
end
453453

454+
# Complex, real and imaginary
455+
#----------------------------
456+
function Base.complex(t::AbstractTensorMap)
457+
if scalartype(t) <: Complex
458+
return t
459+
else
460+
return copy!(similar(t, complex(scalartype(t))), t)
461+
end
462+
end
463+
function Base.complex(r::AbstractTensorMap{<:Real}, i::AbstractTensorMap{<:Real})
464+
return add(r, i, im * one(scalartype(i)))
465+
end
466+
467+
function Base.real(t::AbstractTensorMap)
468+
if scalartype(t) <: Real
469+
return t
470+
else
471+
tr = similar(t, real(scalartype(t)))
472+
for (c, b) in blocks(t)
473+
block(tr, c) .= real(b)
474+
end
475+
return tr
476+
end
477+
end
478+
function Base.imag(t::AbstractTensorMap)
479+
if scalartype(t) <: Real
480+
return zerovector(t)
481+
else
482+
ti = similar(t, real(scalartype(t)))
483+
for (c, b) in blocks(t)
484+
block(ti, c) .= imag(b)
485+
end
486+
return ti
487+
end
488+
end
489+
454490
# Conversion to Array:
455491
#----------------------
456492
# probably not optimized for speed, only for checking purposes

src/tensors/tensor.jl

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ struct TensorMap{T,S<:IndexSpace,N₁,N₂,A<:DenseVector{T}} <: AbstractTensorM
2727
N₁,N₂,
2828
A<:DenseVector{T}}
2929
T field(S) || @warn("scalartype(data) = $T ⊈ $(field(S)))", maxlog = 1)
30+
I = sectortype(S)
31+
T <: Real && !(sectorscalartype(I) <: Real) &&
32+
@warn("Tensors with real data might be incompatible with sector type $I",
33+
maxlog = 1)
3034
return new{T,S,N₁,N₂,A}(data, space)
3135
end
3236
end
@@ -385,17 +389,6 @@ end
385389
#-----------------------------
386390
Base.copy(t::TensorMap) = typeof(t)(copy(t.data), t.space)
387391

388-
function Base.complex(t::AbstractTensorMap)
389-
if scalartype(t) <: Complex
390-
return t
391-
else
392-
return copy!(similar(t, complex(scalartype(t))), t)
393-
end
394-
end
395-
function Base.complex(r::AbstractTensorMap{<:Real}, i::AbstractTensorMap{<:Real})
396-
return add(r, i, im * one(scalartype(i)))
397-
end
398-
399392
# Conversion between TensorMap and Dict, for read and write purpose
400393
#------------------------------------------------------------------
401394
function Base.convert(::Type{Dict}, t::AbstractTensorMap)
@@ -578,29 +571,13 @@ function Base.show(io::IO, t::TensorMap)
578571
end
579572
end
580573

581-
# Real and imaginary parts
582-
#---------------------------
583-
function Base.real(t::AbstractTensorMap)
584-
# `isreal` for a `Sector` returns true iff the F and R symbols are real. This guarantees
585-
# that the real/imaginary part of a tensor `t` can be obtained by just taking
586-
# real/imaginary part of the degeneracy data.
587-
if isreal(sectortype(t))
588-
return TensorMap(real(t.data), codomain(t), domain(t))
589-
else
590-
msg = "`real` has not been implemented for `$(typeof(t))`."
591-
throw(ArgumentError(msg))
592-
end
593-
end
594-
595-
function Base.imag(t::AbstractTensorMap)
596-
# `isreal` for a `Sector` returns true iff the F and R symbols are real. This guarantees
597-
# that the real/imaginary part of a tensor `t` can be obtained by just taking
598-
# real/imaginary part of the degeneracy data.
599-
if isreal(sectortype(t))
600-
return TensorMap(imag(t.data), codomain(t), domain(t))
601-
else
602-
msg = "`imag` has not been implemented for `$(typeof(t))`."
603-
throw(ArgumentError(msg))
574+
# Complex, real and imaginary parts
575+
#-----------------------------------
576+
for f in (:real, :imag, :complex)
577+
@eval begin
578+
function Base.$f(t::TensorMap)
579+
return TensorMap($f(t.data), space(t))
580+
end
604581
end
605582
end
606583

0 commit comments

Comments
 (0)