From 6f18e4077fb78144f39da0ce5ab97fdae720b667 Mon Sep 17 00:00:00 2001 From: lkdvos Date: Fri, 28 Jun 2024 11:47:21 +0200 Subject: [PATCH 1/2] Soften type restrictions on tsvd kwargs --- src/tensors/factorizations.jl | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/src/tensors/factorizations.jl b/src/tensors/factorizations.jl index 524e2760..02ce23e1 100644 --- a/src/tensors/factorizations.jl +++ b/src/tensors/factorizations.jl @@ -246,10 +246,7 @@ function LinearAlgebra.isposdef(t::AbstractTensorMap, (p₁, p₂)::Index2Tuple) return isposdef!(permute(t, (p₁, p₂); copy=true)) end -function tsvd(t::AbstractTensorMap; trunc::TruncationScheme=NoTruncation(), - p::Real=2, alg::Union{SVD,SDD}=SDD()) - return tsvd!(copy(t); trunc=trunc, p=p, alg=alg) -end +tsvd(t::AbstractTensorMap; kwargs...) = tsvd!(copy(t); kwargs...) function leftorth(t::AbstractTensorMap; alg::OFA=QRpos(), kwargs...) return leftorth!(copy(t); alg=alg, kwargs...) end @@ -413,19 +410,17 @@ end #------------------------------# # Singular value decomposition # #------------------------------# -function tsvd!(t::AdjointTensorMap; - trunc::TruncationScheme=NoTruncation(), - p::Real=2, - alg::Union{SVD,SDD}=SDD()) +function tsvd!(t::AdjointTensorMap; trunc=NoTruncation(), p::Real=2, alg=SDD()) u, s, vt, err = tsvd!(adjoint(t); trunc=trunc, p=p, alg=alg) return adjoint(vt), adjoint(s), adjoint(u), err end +function tsvd!(t::TensorMap; trunc=NoTruncation(), p::Real=2, alg=SDD()) + return _tsvd!(t, alg, trunc, p) +end -function tsvd!(t::TensorMap; - trunc::TruncationScheme=NoTruncation(), - p::Real=2, - alg::Union{SVD,SDD}=SDD()) - #early return +# implementation dispatches on algorithm +function _tsvd!(t, alg::Union{SVD,SDD}, trunc::TruncationScheme, p::Real=2) + # early return if isempty(blocksectors(t)) truncerr = zero(real(scalartype(t))) return _empty_svdtensors(t)..., truncerr @@ -440,6 +435,8 @@ function tsvd!(t::TensorMap; else truncerr = abs(zero(scalartype(t))) W = S(dims) + # TODO: do we really want this behaviour? this changes the arrows of the S legs, but + # only sometimes? if length(domain(t)) == 1 && domain(t)[1] ≅ W W = domain(t)[1] elseif length(codomain(t)) == 1 && codomain(t)[1] ≅ W From 39f39ff9fffb191432297ceaf7673e1003002d3a Mon Sep 17 00:00:00 2001 From: lkdvos Date: Mon, 1 Jul 2024 11:15:04 +0200 Subject: [PATCH 2/2] Fix spaces in truncated svd to non-dual --- src/tensors/factorizations.jl | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/tensors/factorizations.jl b/src/tensors/factorizations.jl index 02ce23e1..6b325d48 100644 --- a/src/tensors/factorizations.jl +++ b/src/tensors/factorizations.jl @@ -428,21 +428,13 @@ function _tsvd!(t, alg::Union{SVD,SDD}, trunc::TruncationScheme, p::Real=2) S = spacetype(t) Udata, Σdata, Vdata, dims = _compute_svddata!(t, alg) - if !isa(trunc, NoTruncation) + if trunc isa NoTruncation + truncerr = abs(zero(scalartype(t))) + else Σdata, truncerr = _truncate!(Σdata, trunc, p) Udata, Σdata, Vdata, dims = _implement_svdtruncation!(t, Udata, Σdata, Vdata, dims) - W = S(dims) - else - truncerr = abs(zero(scalartype(t))) - W = S(dims) - # TODO: do we really want this behaviour? this changes the arrows of the S legs, but - # only sometimes? - if length(domain(t)) == 1 && domain(t)[1] ≅ W - W = domain(t)[1] - elseif length(codomain(t)) == 1 && codomain(t)[1] ≅ W - W = codomain(t)[1] - end end + W = S(dims) return _create_svdtensors(t, Udata, Σdata, Vdata, W)..., truncerr end