Skip to content

Commit f95bfd3

Browse files
Construct DiagonalTensorMap from SectorVector (#363)
1 parent 9024530 commit f95bfd3

3 files changed

Lines changed: 23 additions & 0 deletions

File tree

src/tensors/diagonal.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,17 @@ similar_diagonal(d::DiagonalTensorMap) = DiagonalTensorMap(similar(d.data), d.do
8585
similar_diagonal(d::DiagonalTensorMap, ::Type{T}) where {T <: Number} =
8686
DiagonalTensorMap(similar(d.data, T), d.domain)
8787

88+
"""
89+
DiagonalTensorMap(s::SectorVector)
90+
91+
Construct a `DiagonalTensorMap` directly from a `SectorVector`,
92+
from which the codomain (assumed non-dual) is inferred automatically.
93+
"""
94+
function DiagonalTensorMap(s::SectorVector)
95+
V = Vect[sectortype(s)](c => length(b) for (c, b) in blocks(s))
96+
return DiagonalTensorMap(s.data, V)
97+
end
98+
8899
# TODO: more constructors needed?
89100

90101
# Special case adjoint:

test/cuda/factorizations.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ for V in spacelist
217217
@test parent(s′) parent(diagview(s))
218218
@test s′ isa TensorKit.SectorVector
219219

220+
s2 = @constinferred DiagonalTensorMap(s′)
221+
@test s2 s
222+
220223
v, c = @constinferred left_orth(t; alg = :svd)
221224
@test v * c t
222225
@test isisometric(v)
@@ -345,6 +348,9 @@ for V in spacelist
345348
@test parent(d′) parent(diagview(d))
346349
@test d′ isa TensorKit.SectorVector
347350

351+
d2 = @constinferred DiagonalTensorMap(d′)
352+
@test d2 d
353+
348354
vdv = project_hermitian!(v' * v)
349355
@test @constinferred isposdef(vdv)
350356
t isa DiagonalTensorMap || @test !isposdef(t) # unlikely for non-hermitian map

test/tensors/factorizations.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ for V in spacelist
196196
@test s′ diagview(s)
197197
@test s′ isa TensorKit.SectorVector
198198

199+
s2 = @constinferred DiagonalTensorMap(s′)
200+
@test s2 s
201+
199202
v, c = @constinferred left_orth(t; alg = :svd)
200203
@test v * c t
201204
@test isisometric(v)
@@ -316,6 +319,9 @@ for V in spacelist
316319
@test d′ diagview(d)
317320
@test d′ isa TensorKit.SectorVector
318321

322+
d2 = @constinferred DiagonalTensorMap(d′)
323+
@test d2 d
324+
319325
vdv = project_hermitian!(v' * v)
320326
@test @constinferred isposdef(vdv)
321327
t isa DiagonalTensorMap || @test !isposdef(t) # unlikely for non-hermitian map

0 commit comments

Comments
 (0)