Skip to content

Commit

Permalink
update TensorKit compat
Browse files Browse the repository at this point in the history
  • Loading branch information
lkdvos committed Dec 30, 2024
1 parent 9c1d24c commit d41cbbc
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ LinearAlgebra = "1"
Random = "1"
Strided = "2"
SafeTestsets = "0.1"
TensorKit = "0.13.2"
TensorKit = "0.13.2, 0.14"
TensorOperations = "5"
Test = "1"
TestExtras = "0.2"
Expand Down
35 changes: 34 additions & 1 deletion src/linalg/factorizations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,27 @@ function TK.tsvd!(t::SparseBlockTensorMap; kwargs...)
return tsvd!(BlockTensorMap(t); kwargs...)
end

function TK._tsvd!(
t::BlockTensorMap, alg::Union{SVD,SDD}, trunc::TruncationScheme, p::Real=2
)
# early return
if isempty(blocksectors(t))
truncerr = zero(real(scalartype(t)))
return TK._empty_svdtensors(t)..., truncerr
end

# compute SVD factorization for each block
S = spacetype(t)
SVDdata, dims = TK._compute_svddata!(t, alg)
Σdata = SectorDict(c => Σ for (c, (U, Σ, V)) in SVDdata)
truncdim = TK._compute_truncdim(Σdata, trunc, p)
truncerr = TK._compute_truncerr(Σdata, truncdim, p)

# construct output tensors
U, Σ, V⁺ = TK._create_svdtensors(t, SVDdata, truncdim)
return U, Σ, V⁺, truncerr
end

function TK._compute_svddata!(t::AbstractBlockTensorMap, alg::Union{SVD,SDD})
InnerProductStyle(t) === EuclideanInnerProduct() || throw_invalid_innerproduct(:tsvd!)
I = sectortype(t)
Expand All @@ -225,7 +246,6 @@ function TK._compute_svddata!(t::AbstractBlockTensorMap, alg::Union{SVD,SDD})
SVDdata = SectorDict(generator)
return SVDdata, dims
end

function TK._create_svdtensors(t::AbstractBlockTensorMap, SVDdata, dims)
S = spacetype(t)
W = S(dims)
Expand All @@ -241,3 +261,16 @@ function TK._create_svdtensors(t::AbstractBlockTensorMap, SVDdata, dims)
end
return U, Σ, V⁺
end

function TK._empty_svdtensors(t::AbstractBlockTensorMap)
T = scalartype(t)
S = spacetype(t)
I = sectortype(t)
dims = SectorDict{I,Int}()
W = S(dims)

U = similar(t, codomain(t) W)
Σ = similar(t, real(T), W W)
V⁺ = similar(t, W domain(t))
return U, Σ, V⁺
end

0 comments on commit d41cbbc

Please sign in to comment.