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
24 changes: 23 additions & 1 deletion src/tensors/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,29 @@ function permute(
end
return d′
else
throw(ArgumentError("invalid permutation $((p₁, p₂)) for tensor in space $(space(d))"))
throw(ArgumentError(lazy"invalid permutation $((p₁, p₂)) for tensor in space $(space(d))"))
end
end

function LinearAlgebra.transpose(
d::DiagonalTensorMap, (p₁, p₂)::Index2Tuple{1, 1}; copy::Bool = false
)
if p₁ === (1,) && p₂ === (2,)
return copy ? Base.copy(d) : d
elseif p₁ === (2,) && p₂ === (1,) # transpose
if has_shared_permute(d, (p₁, p₂)) # tranpose for bosonic sectors
return DiagonalTensorMap(copy ? Base.copy(d.data) : d.data, dual(d.domain))
end
d′ = typeof(d)(undef, dual(d.domain))
for (c, b) in blocks(d)
f = only(fusiontrees(codomain(d), c))
((f′, _), coeff) = only(transpose(f, f, p₁, p₂))
c′ = f′.coupled
scale!(block(d′, c′), b, coeff)
end
return d′
else
throw(ArgumentError(lazy"invalid transposition $((p₁, p₂)) for tensor in space $(space(d))"))
end
end

Expand Down
23 changes: 15 additions & 8 deletions test/tensors/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,26 @@ diagspacelist = (
if BraidingStyle(I) isa SymmetricBraiding
@timedtestset "Permutations" begin
t = DiagonalTensorMap(randn(ComplexF64, reduceddim(V)), V)
t_tm = convert(TensorMap, t)

# preserving diagonal
t1 = @constinferred permute(t, $(((2,), (1,))))
if BraidingStyle(sectortype(V)) isa Bosonic
@test t1 ≈ transpose(t)
end
@test convert(TensorMap, t1) == permute(convert(TensorMap, t), (((2,), (1,))))
@test t1 isa DiagonalTensorMap
@test convert(TensorMap, t1) == permute(t_tm, (((2,), (1,))))
t1′ = @constinferred transpose(t)
@test t1′ isa DiagonalTensorMap
@test convert(TensorMap, t1′) == transpose(t_tm)
BraidingStyle(I) isa Bosonic && @test t1 ≈ t1′

# not preserving diagonal
t2 = @constinferred permute(t, $(((1, 2), ())))
@test convert(TensorMap, t2) == permute(convert(TensorMap, t), (((1, 2), ())))
@test convert(TensorMap, t2) == permute(t_tm, (((1, 2), ())))
t3 = @constinferred permute(t, $(((2, 1), ())))
@test convert(TensorMap, t3) == permute(convert(TensorMap, t), (((2, 1), ())))
@test convert(TensorMap, t3) == permute(t_tm, (((2, 1), ())))
t4 = @constinferred permute(t, $(((), (1, 2))))
@test convert(TensorMap, t4) == permute(convert(TensorMap, t), (((), (1, 2))))
@test convert(TensorMap, t4) == permute(t_tm, (((), (1, 2))))
t5 = @constinferred permute(t, $(((), (2, 1))))
@test convert(TensorMap, t5) == permute(convert(TensorMap, t), (((), (2, 1))))
@test convert(TensorMap, t5) == permute(t_tm, (((), (2, 1))))
end
end
@timedtestset "Trace, Multiplication and inverse" begin
Expand Down
Loading