Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
37 changes: 37 additions & 0 deletions src/tensors/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@

Construct a `DiagonalTensorMap` with uninitialized data.
"""
function DiagonalTensorMap{T}(::UndefInitializer, V::TensorMapSpace) where {T}
(numin(V) == numout(V) == 1 && domain(V) == codomain(V)) ||

Check warning on line 41 in src/tensors/diagonal.jl

View check run for this annotation

Codecov / codecov/patch

src/tensors/diagonal.jl#L40-L41

Added lines #L40 - L41 were not covered by tests
throw(SpaceMismatch("DiagonalTensorMap requires a space with equal domain and codomain and 2 indices"))
return DiagonalTensorMap{T}(undef, domain(V))

Check warning on line 43 in src/tensors/diagonal.jl

View check run for this annotation

Codecov / codecov/patch

src/tensors/diagonal.jl#L43

Added line #L43 was not covered by tests
end
function DiagonalTensorMap{T}(::UndefInitializer, V::ProductSpace) where {T}
length(V) == 1 ||

Check warning on line 46 in src/tensors/diagonal.jl

View check run for this annotation

Codecov / codecov/patch

src/tensors/diagonal.jl#L45-L46

Added lines #L45 - L46 were not covered by tests
throw(DimensionMismatch("DiagonalTensorMap requires `numin(d) == numout(d) == 1`"))
return DiagonalTensorMap{T}(undef, only(V))

Check warning on line 48 in src/tensors/diagonal.jl

View check run for this annotation

Codecov / codecov/patch

src/tensors/diagonal.jl#L48

Added line #L48 was not covered by tests
end
function DiagonalTensorMap{T}(::UndefInitializer, V::S) where {T,S<:IndexSpace}
return DiagonalTensorMap{T,S,Vector{T}}(undef, V)
end
Expand Down Expand Up @@ -265,6 +275,22 @@
return dC
end

function LinearAlgebra.lmul!(D::DiagonalTensorMap, t::AbstractTensorMap)
domain(D) == codomain(t) || throw(SpaceMismatch())
for (c, b) in blocks(t)
lmul!(block(D, c), b)
end
return t
end

function LinearAlgebra.rmul!(t::AbstractTensorMap, D::DiagonalTensorMap)
codomain(D) == domain(t) || throw(SpaceMismatch())
for (c, b) in blocks(t)
rmul!(b, block(D, c))
end
return t
end

Base.inv(d::DiagonalTensorMap) = DiagonalTensorMap(inv.(d.data), d.domain)
function Base.:\(d1::DiagonalTensorMap, d2::DiagonalTensorMap)
d1.domain == d2.domain || throw(SpaceMismatch())
Expand Down Expand Up @@ -339,6 +365,17 @@
return SVDdata, dims
end

function LinearAlgebra.svdvals(d::DiagonalTensorMap)
return SectorDict(c => LinearAlgebra.svdvals(b) for (c, b) in blocks(d))
end
function LinearAlgebra.eigvals(d::DiagonalTensorMap)
return SectorDict(c => LinearAlgebra.eigvals(b) for (c, b) in blocks(d))
end

function LinearAlgebra.cond(d::DiagonalTensorMap, p::Real=2)
return LinearAlgebra.cond(Diagonal(d.data), p)
end

# matrix functions
for f in
(:exp, :cos, :sin, :tan, :cot, :cosh, :sinh, :tanh, :coth, :atan, :acot, :asinh, :sqrt,
Expand Down
22 changes: 22 additions & 0 deletions test/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,16 @@ diagspacelist = ((ℂ^4)', ℂ[Z2Irrep](0 => 2, 1 => 3),
@test u / t1 ≈ u / TensorMap(t1)
@test t1 * u' ≈ TensorMap(t1) * u'
@test t1 \ u' ≈ TensorMap(t1) \ u'

t3 = rand(Float64, V ← V^2)
t4 = rand(ComplexF64, V ← V^2)
@test t1 * t3 ≈ lmul!(t1, copy(t3))
@test t2 * t4 ≈ lmul!(t2, copy(t4))

t3 = rand(Float64, V^2 ← V)
t4 = rand(ComplexF64, V^2 ← V)
@test t3 * t1 ≈ rmul!(copy(t3), t1)
@test t4 * t2 ≈ rmul!(copy(t4), t2)
end
@timedtestset "Tensor contraction" begin
d = DiagonalTensorMap(rand(ComplexF64, reduceddim(V)), V)
Expand Down Expand Up @@ -175,6 +185,12 @@ diagspacelist = ((ℂ^4)', ℂ[Z2Irrep](0 => 2, 1 => 3),
VdV2 = V2' * V2
@test VdV2 ≈ one(VdV2)
@test t2 * V2 ≈ V2 * D2

@test rank(D) ≈ rank(t)
@test cond(D) ≈ cond(t)
@test all(((s, t),) -> isapprox(s, t),
zip(values(LinearAlgebra.eigvals(D)),
values(LinearAlgebra.eigvals(t))))
end
@testset "leftorth with $alg" for alg in (TensorKit.QR(), TensorKit.QL())
Q, R = @constinferred leftorth(t; alg=alg)
Expand All @@ -201,6 +217,12 @@ diagspacelist = ((ℂ^4)', ℂ[Z2Irrep](0 => 2, 1 => 3),
VdV = Vᴴ * Vᴴ'
@test VdV ≈ one(VdV)
@test U * S * Vᴴ ≈ t

@test rank(S) ≈ rank(t)
@test cond(S) ≈ cond(t)
@test all(((s, t),) -> isapprox(s, t),
zip(values(LinearAlgebra.svdvals(S)),
values(LinearAlgebra.svdvals(t))))
end
end
end
Expand Down
Loading