Skip to content

Commit

Permalink
fix empty spaces and tensors
Browse files Browse the repository at this point in the history
  • Loading branch information
Jutho committed Feb 11, 2020
1 parent a6f4dad commit 41d457c
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
10 changes: 7 additions & 3 deletions src/spaces/representationspace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ function GenericRepresentationSpace{G}(dims; dual::Bool = false) where {G<:Secto
end
return GenericRepresentationSpace{G}(d, dual)
end
GenericRepresentationSpace{G}(; dual::Bool = false) where {G<:Sector} =
GenericRepresentationSpace{G}((); dual = dual)
GenericRepresentationSpace{G}(d1::Pair; dual::Bool = false) where {G<:Sector} =
GenericRepresentationSpace{G}((d1, ); dual = dual)
GenericRepresentationSpace{G}(d1::Pair, dims::Vararg{Pair};
Expand Down Expand Up @@ -60,9 +62,10 @@ function FiniteRepresentationSpace{G}(dims; dual::Bool = false) where {G<:Sector
end
return FiniteRepresentationSpace{G,N}(d, dual)
end
FiniteRepresentationSpace{G}(; dual::Bool = false) where {G<:Sector} =
FiniteRepresentationSpace{G}((); dual = dual)
FiniteRepresentationSpace{G}(d1::Pair; dual::Bool = false) where {G<:Sector} =
FiniteRepresentationSpace{G}((d1,); dual = dual)

FiniteRepresentationSpace{G}(d1::Pair, dims::Vararg{Pair};
dual::Bool = false) where {G<:Sector} =
FiniteRepresentationSpace{G}((d1, dims...); dual = dual)
Expand Down Expand Up @@ -99,12 +102,13 @@ Base.getindex(::ComplexNumbers, d1::Pair{G,Int}, dims::Pair{G,Int}...) where {G<

# Corresponding methods:
# properties
dim(V::GenericRepresentationSpace) = sum(c->dim(c)*V.dims[c], keys(V.dims))
dim(V::GenericRepresentationSpace) =
mapreduce(c->dim(c)*V.dims[c], +, keys(V.dims); init = 0)
dim(V::GenericRepresentationSpace{G}, c::G) where {G<:Sector} =
get(V.dims, isdual(V) ? dual(c) : c, 0)

dim(V::FiniteRepresentationSpace{G}) where {G<:Sector} =
sum(dc*dim(c) for (dc,c) in zip(V.dims, values(G)))
reduce(+, dc*dim(c) for (dc,c) in zip(V.dims, values(G)); init = 0)
dim(V::FiniteRepresentationSpace{G}, c::G) where {G<:Sector} =
V.dims[findindex(values(G), isdual(V) ? dual(c) : c)]

Expand Down
9 changes: 7 additions & 2 deletions src/tensors/tensor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,16 @@ function TensorMap(data::AbstractDict{<:Sector,<:DenseMatrix}, codom::ProductSpa
push!(colr, c=>colrc)
end
if !isreal(G) && eltype(valtype(data)) <: Real
data2 = SectorDict((c=>complex(d)) for (c,d) in data)
b = valtype(data)(undef, (0,0))
V = typeof(complex(b))
K = keytype(data)
data2 = SectorDict{K,V}((c=>complex(b)) for (c,b) in data)
A = typeof(data2)
return TensorMap{S, N₁, N₂, G, A, F₁, F₂}(data2, codom, dom, rowr, colr)
else
data2 = SectorDict((c=>d) for (c,d) in data)
V = valtype(data)
K = keytype(data)
data2 = SectorDict{K,V}((c=>d) for (c,d) in data)
A = typeof(data2)
return TensorMap{S, N₁, N₂, G, A, F₁, F₂}(data2, codom, dom, rowr, colr)
end
Expand Down
2 changes: 2 additions & 0 deletions test/spaces.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ end
@test eval(Meta.parse(sprint(show,typeof(V)))) == typeof(V)
W = RepresentationSpace(one(G)=>1) # space with a single sector
@test W == RepresentationSpace(one(G)=>1, randsector(G) => 0)
# space with no sectors
@test dim(@inferred RepresentationSpace{G}()) == 0
# randsector never returns trivial sector, so this cannot error
@test_throws ArgumentError RepresentationSpace(one(G)=>1, randsector(G) => 0, one(G)=>3)
@test eval(Meta.parse(sprint(show,W))) == W
Expand Down
30 changes: 30 additions & 0 deletions test/tensors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,36 @@ for (G,V) in ((Trivial, Vtr), (ℤ₂, Vℤ₂), (ℤ₃, Vℤ₃), (U₁, VU₁
@test U*S*V permute(t, (3,4,2),(1,5))
end
end
@testset "empty tensor" begin
t = TensorMap(randn, T, V1 V2, typeof(V1)())
@testset "leftorth with $alg" for alg in (TensorKit.QR(), TensorKit.QRpos(), TensorKit.QL(), TensorKit.QLpos(), TensorKit.Polar(), TensorKit.SVD(), TensorKit.SDD())
Q, R = @inferred leftorth(t; alg = alg)
@test Q == t
@test dim(Q) == dim(R) == 0
end
@testset "leftnull with $alg" for alg in (TensorKit.QR(), TensorKit.SVD(), TensorKit.SDD())
N = @inferred leftnull(t; alg = alg)
@test N'*N id(domain(N))
@test N*N' id(codomain(N))
end
@testset "rightorth with $alg" for alg in (TensorKit.RQ(), TensorKit.RQpos(), TensorKit.LQ(), TensorKit.LQpos(), TensorKit.Polar(), TensorKit.SVD(), TensorKit.SDD())
L, Q = @inferred rightorth(t; alg = alg)
@test L == t
@test dim(Q) == dim(R) == 0
end
@testset "rightnull with $alg" for alg in (TensorKit.LQ(), TensorKit.SVD(), TensorKit.SDD())
M = @inferred rightnull(t; alg = alg)
@test M*M' id(codomain(M))
@test M'*M id(domain(M))
end
@testset "tsvd with $alg" for alg in (TensorKit.SVD(), TensorKit.SDD())
U, S, V = @inferred tsvd(t; alg = alg)
@test U == t
@test dim(Q) == dim(R) == dim(V)
end
end



t = Tensor(rand, T, V1 V1' V2 V2')
@testset "eig and isposdef" begin
Expand Down

0 comments on commit 41d457c

Please sign in to comment.