Skip to content

Commit a4f6d69

Browse files
committed
rework tests and more fixes and cleanup
1 parent 529410e commit a4f6d69

File tree

9 files changed

+382
-319
lines changed

9 files changed

+382
-319
lines changed

src/TensorKit.jl

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,28 +70,30 @@ export inner, dot, norm, normalize, normalize!, tr
7070

7171
# factorizations
7272
export mul!, lmul!, rmul!, adjoint!, pinv, axpy!, axpby!
73-
export leftorth, rightorth, leftnull, rightnull,
74-
leftorth!, rightorth!, leftnull!, rightnull!,
73+
export left_orth, right_orth, left_null, right_null,
74+
left_orth!, right_orth!, left_null!, right_null!,
7575
left_polar, left_polar!, right_polar, right_polar!,
7676
qr_full, qr_compact, qr_null, lq_full, lq_compact, lq_null,
7777
qr_full!, qr_compact!, qr_null!, lq_full!, lq_compact!, lq_null!,
78-
tsvd!, tsvd, eigen, eigen!, eig, eig!, eigh, eigh!, exp, exp!,
79-
eigh_full!, eigh_full, eig_full!, eig_full, eigh_vals!, eigh_vals,
80-
eig_vals!, eig_vals,
78+
svd_compact!, svd_full!, svd_trunc!, svd_compact, svd_full, svd_trunc,
79+
exp, exp!,
80+
eigh_full!, eigh_full, eigh_trunc!, eigh_trunc, eig_full!, eig_full, eig_trunc!,
81+
eig_trunc,
82+
eigh_vals!, eigh_vals, eig_vals!, eig_vals,
8183
isposdef, isposdef!, ishermitian, isisometry, isunitary, sylvester, rank, cond
84+
# deprecate:
85+
export eig, eig!, eigh, eigh!, eigen, eigen!, tsvd, tsvd!, leftorth, leftorth!, rightorth,
86+
rightorth!, leftnull, leftnull!, rightnull, rightnull!
8287
export braid, braid!, permute, permute!, transpose, transpose!, twist, twist!, repartition,
8388
repartition!
8489
export catdomain, catcodomain, absorb, absorb!
8590

86-
export OrthogonalFactorizationAlgorithm, QR, QRpos, QL, QLpos, LQ, LQpos, RQ, RQpos,
87-
SVD, SDD, Polar
88-
8991
# tensor operations
9092
export @tensor, @tensoropt, @ncon, ncon, @planar, @plansor
9193
export scalar, add!, contract!
9294

9395
# truncation schemes
94-
export notrunc, truncerr, truncdim, truncspace, truncbelow
96+
export notrunc, truncerr, truncrank, truncspace, trunctol
9597

9698
# cache management
9799
export empty_globalcaches!

src/auxiliary/deprecate.jl

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,5 @@
11
import Base: transpose
22

3-
#! format: off
4-
# Base.@deprecate(permute(t::AbstractTensorMap, p1::IndexTuple, p2::IndexTuple; copy::Bool=false),
5-
# permute(t, (p1, p2); copy=copy))
6-
# Base.@deprecate(transpose(t::AbstractTensorMap, p1::IndexTuple, p2::IndexTuple; copy::Bool=false),
7-
# transpose(t, (p1, p2); copy=copy))
8-
# Base.@deprecate(braid(t::AbstractTensorMap, p1::IndexTuple, p2::IndexTuple, levels; copy::Bool=false),
9-
# braid(t, (p1, p2), levels; copy=copy))
10-
11-
# Base.@deprecate(tsvd(t::AbstractTensorMap, p₁::IndexTuple, p₂::IndexTuple; kwargs...),
12-
# tsvd(t, (p₁, p₂); kwargs...))
13-
# Base.@deprecate(leftorth(t::AbstractTensorMap, p₁::IndexTuple, p₂::IndexTuple; kwargs...),
14-
# leftorth(t, (p₁, p₂); kwargs...))
15-
# Base.@deprecate(rightorth(t::AbstractTensorMap, p₁::IndexTuple, p₂::IndexTuple; kwargs...),
16-
# rightorth(t, (p₁, p₂); kwargs...))
17-
# Base.@deprecate(leftnull(t::AbstractTensorMap, p₁::IndexTuple, p₂::IndexTuple; kwargs...),
18-
# leftnull(t, (p₁, p₂); kwargs...))
19-
# Base.@deprecate(rightnull(t::AbstractTensorMap, p₁::IndexTuple, p₂::IndexTuple; kwargs...),
20-
# rightnull(t, (p₁, p₂); kwargs...))
21-
# Base.@deprecate(LinearAlgebra.eigen(t::AbstractTensorMap, p₁::IndexTuple, p₂::IndexTuple; kwargs...),
22-
# LinearAlgebra.eigen(t, (p₁, p₂); kwargs...), false)
23-
# Base.@deprecate(eig(t::AbstractTensorMap, p₁::IndexTuple, p₂::IndexTuple; kwargs...),
24-
# eig(t, (p₁, p₂); kwargs...))
25-
# Base.@deprecate(eigh(t::AbstractTensorMap, p₁::IndexTuple, p₂::IndexTuple; kwargs...),
26-
# eigh(t, (p₁, p₂); kwargs...))
27-
283
for f in (:rand, :randn, :zeros, :ones)
294
@eval begin
305
Base.@deprecate TensorMap(::typeof($f), T::Type, P::HomSpace) $f(T, P)

src/tensors/factorizations/deprecations.jl

Lines changed: 66 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,58 +23,97 @@ const TruncationScheme = TruncationStrategy
2323

2424
# factorizations
2525
# --------------
26+
_kindof(::LAPACK_HouseholderQR) = :qr
27+
_kindof(::LAPACK_HouseholderLQ) = :lq
28+
_kindof(::LAPACK_SVDAlgorithm) = :svd
29+
_kindof(::PolarViaSVD) = :polar
30+
31+
_drop_alg(; alg=nothing, kwargs...) = kwargs
32+
_drop_p(; p=nothing, kwargs...) = kwargs
33+
2634
# orthogonalization
27-
@deprecate(leftorth(t::AbstractTensorMap, p::Index2Tuple; kwargs...),
28-
leftorth!(permutedcopy_oftype(t, factorization_scalartype(leftorth, t), p);
29-
kwargs...))
30-
@deprecate(rightorth(t::AbstractTensorMap, p::Index2Tuple; kwargs...),
31-
rightorth!(permutedcopy_oftype(t, factorisation_scalartype(rightorth, t), p);
32-
kwargs...))
33-
function leftorth(t::AbstractTensorMap; kwargs...)
35+
export leftorth, leftorth!, rightorth, rightorth!
36+
function leftorth(t::AbstractTensorMap, p::Index2Tuple; kwargs...)
3437
Base.depwarn("`leftorth` is no longer supported, use `left_orth` instead", :leftorth)
35-
return left_orth(t; kwargs...)
38+
return leftorth!(permutedcopy_oftype(t, factorisation_scalartype(leftorth, t), p);
39+
kwargs...)
3640
end
37-
function leftorth!(t::AbstractTensorMap; kwargs...)
38-
Base.depwarn("`leftorth!` is no longer supported, use `left_orth!` instead", :leftorth!)
39-
return left_orth!(t; kwargs...)
41+
function rightorth(t::AbstractTensorMap, p::Index2Tuple; kwargs...)
42+
Base.depwarn("`rightorth` is no longer supported, use `right_orth` instead", :rightorth)
43+
return rightorth!(permutedcopy_oftype(t, factorisation_scalartype(rightorth, t), p);
44+
kwargs...)
45+
end
46+
function leftorth(t::AbstractTensorMap; kwargs...)
47+
Base.depwarn("`leftorth` is no longer supported, use `left_orth` instead", :leftorth)
48+
return leftorth!(copy_oftype(t, factorisation_scalartype(leftorth, t)); kwargs...)
4049
end
4150
function rightorth(t::AbstractTensorMap; kwargs...)
4251
Base.depwarn("`rightorth` is no longer supported, use `right_orth` instead", :rightorth)
43-
return right_orth(t; kwargs...)
52+
return rightorth!(copy_oftype(t, factorisation_scalartype(rightorth, t)); kwargs...)
53+
end
54+
function leftorth!(t::AbstractTensorMap; kwargs...)
55+
Base.depwarn("`leftorth!` is no longer supported, use `left_orth!` instead", :leftorth!)
56+
haskey(kwargs, :alg) || return left_orth!(t; kwargs...)
57+
alg = kwargs[:alg]
58+
kind = _kindof(alg)
59+
kind === :svd && return left_orth!(t; kind, alg_svd=alg, _drop_alg(; kwargs...)...)
60+
kind === :qr && return left_orth!(t; kind, alg_qr=alg, _drop_alg(; kwargs...)...)
61+
kind === :polar && return left_orth!(t; kind, alg_polar=alg, _drop_alg(; kwargs...)...)
62+
throw(ArgumentError("invalid leftorth kind"))
4463
end
4564
function rightorth!(t::AbstractTensorMap; kwargs...)
4665
Base.depwarn("`rightorth!` is no longer supported, use `right_orth!` instead",
4766
:rightorth!)
48-
return right_orth!(t; kwargs...)
67+
haskey(kwargs, :alg) || return right_orth!(t; kwargs...)
68+
alg = kwargs[:alg]
69+
kind = _kindof(alg)
70+
kind === :svd && return right_orth!(t; kind, alg_svd=alg, _drop_alg(; kwargs...)...)
71+
kind === :lq && return right_orth!(t; kind, alg_lq=alg, _drop_alg(; kwargs...)...)
72+
kind === :polar && return right_orth!(t; kind, alg_polar=alg, _drop_alg(; kwargs...)...)
73+
throw(ArgumentError("invalid rightorth kind"))
4974
end
5075

5176
# nullspaces
52-
@deprecate(leftnull(t::AbstractTensorMap, p::Index2Tuple; kwargs...),
53-
leftnull!(permutedcopy_oftype(t, factorization_scalartype(leftnull, t), p);
54-
kwargs...))
55-
@deprecate(rightnull(t::AbstractTensorMap, p::Index2Tuple; kwargs...),
56-
rightnull!(permutedcopy_oftype(t, factorisation_scalartype(rightnull, t), p);
57-
kwargs...))
77+
export leftnull, leftnull!, rightnull, rightnull!
5878
function leftnull(t::AbstractTensorMap; kwargs...)
5979
Base.depwarn("`leftnull` is no longer supported, use `left_null` instead", :leftnull)
60-
return left_null(t; kwargs...)
80+
return leftnull!(copy_oftype(t, factorisation_scalartype(leftnull, t)); kwargs...)
6181
end
62-
function leftnull!(t::AbstractTensorMap; kwargs...)
63-
Base.depwarn("`left_null!` is no longer supported, use `left_null!` instead",
64-
:leftnull!)
65-
return left_null!(t; kwargs...)
82+
function leftnull(t::AbstractTensorMap, p::Index2Tuple; kwargs...)
83+
Base.depwarn("`leftnull` is no longer supported, use `left_null` instead", :leftnull)
84+
return leftnull!(permutedcopy_oftype(t, factorisation_scalartype(leftnull, t), p); kwargs...)
6685
end
6786
function rightnull(t::AbstractTensorMap; kwargs...)
6887
Base.depwarn("`rightnull` is no longer supported, use `right_null` instead", :rightnull)
69-
return right_null(t; kwargs...)
88+
return rightnull!(copy_oftype(t, factorisation_scalartype(rightnull, t)); kwargs...)
89+
end
90+
function rightnull(t::AbstractTensorMap, p::Index2Tuple; kwargs...)
91+
Base.depwarn("`rightnull` is no longer supported, use `right_null` instead", :rightnull)
92+
return rightnull!(permutedcopy_oftype(t, factorisation_scalartype(rightnull, t), p); kwargs...)
93+
end
94+
function leftnull!(t::AbstractTensorMap; kwargs...)
95+
Base.depwarn("`left_null!` is no longer supported, use `left_null!` instead",
96+
:leftnull!)
97+
haskey(kwargs, :alg) || return left_null!(t; kwargs...)
98+
alg = kwargs[:alg]
99+
kind = _kindof(alg)
100+
kind === :svd && return left_null!(t; kind, alg_svd=alg, _drop_alg(; kwargs...)...)
101+
kind === :qr && return left_null!(t; kind, alg_qr=alg, _drop_alg(; kwargs...)...)
102+
throw(ArgumentError("invalid leftnull kind"))
70103
end
71104
function rightnull!(t::AbstractTensorMap; kwargs...)
72105
Base.depwarn("`rightnull!` is no longer supported, use `right_null!` instead",
73106
:rightnull!)
74-
return right_null!(t; kwargs...)
107+
haskey(kwargs, :alg) || return right_null!(t; kwargs...)
108+
alg = kwargs[:alg]
109+
kind = _kindof(alg)
110+
kind === :svd && return right_null!(t; kind, alg_svd=alg, _drop_alg(; kwargs...)...)
111+
kind === :lq && return right_null!(t; kind, alg_lq=alg, _drop_alg(; kwargs...)...)
112+
throw(ArgumentError("invalid rightnull kind"))
75113
end
76114

77115
# eigen values
116+
export eig, eig!, eigh, eigh!, eigen, eigen!
78117
@deprecate(eig(t::AbstractTensorMap, p::Index2Tuple; kwargs...),
79118
eig!(permutedcopy_oftype(t, factorisation_scalartype(eig, t), p); kwargs...))
80119
@deprecate(eigh(t::AbstractTensorMap, p::Index2Tuple; kwargs...),
@@ -103,7 +142,7 @@ function eigh!(t::AbstractTensorMap; kwargs...)
103142
end
104143

105144
# singular values
106-
_drop_p(; p=nothing, kwargs...) = kwargs
145+
export tsvd, tsvd!
107146
@deprecate(tsvd(t::AbstractTensorMap, p::Index2Tuple; kwargs...),
108147
tsvd!(permutedcopy_oftype(t, factorisation_scalartype(tsvd, t), p); kwargs...))
109148
function tsvd(t::AbstractTensorMap; kwargs...)

src/tensors/factorizations/factorizations.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ const RealOrComplexFloat = Union{AbstractFloat,Complex{<:AbstractFloat}}
6565
# LinearAlgebra overloads
6666
#------------------------------#
6767

68+
function eigen(t::AbstractTensorMap; kwargs...)
69+
return ishermitian(t) ? eigh_full(t; kwargs...) : eig_full(t; kwargs...)
70+
end
71+
function eigen!(t::AbstractTensorMap; kwargs...)
72+
return ishermitian(t) ? eigh_full!(t; kwargs...) : eig_full!(t; kwargs...)
73+
end
74+
6875
function LinearAlgebra.eigvals(t::AbstractTensorMap; kwargs...)
6976
tcopy = copy_oftype(t, factorisation_scalartype(eigen, t))
7077
return LinearAlgebra.eigvals!(tcopy; kwargs...)
@@ -89,6 +96,9 @@ function LinearAlgebra.ishermitian(t::AbstractTensorMap)
8996
return true
9097
end
9198

99+
function LinearAlgebra.isposdef(t::AbstractTensorMap)
100+
return isposdef!(copy_oftype(t, factorisation_scalartype(isposdef, t)))
101+
end
92102
function LinearAlgebra.isposdef!(t::AbstractTensorMap)
93103
domain(t) == codomain(t) ||
94104
throw(SpaceMismatch("`isposdef` requires domain and codomain to be the same"))

src/tensors/factorizations/matrixalgebrakit.jl

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
# Algorithm selection
22
# -------------------
3-
for f! in
4-
[:svd_compact!, :svd_full!, :svd_trunc!, :svd_vals!, :qr_compact!, :qr_full!, :qr_null!,
5-
:lq_compact!, :lq_full!, :lq_null!, :eig_full!, :eig_trunc!, :eig_vals!, :eigh_full!,
6-
:eigh_trunc!, :eigh_vals!, :left_polar!, :right_polar!]
3+
for f in
4+
[:svd_compact, :svd_full, :svd_trunc, :svd_vals, :qr_compact, :qr_full, :qr_null,
5+
:lq_compact, :lq_full, :lq_null, :eig_full, :eig_trunc, :eig_vals, :eigh_full,
6+
:eigh_trunc, :eigh_vals, :left_polar, :right_polar]
7+
f! = Symbol(f, :!)
78
@eval function default_algorithm(::typeof($f!), ::Type{T};
89
kwargs...) where {T<:AbstractTensorMap}
910
return default_algorithm($f!, blocktype(T); kwargs...)
1011
end
12+
@eval function copy_input(::typeof($f), t::AbstractTensorMap)
13+
return copy_oftype(t, factorisation_scalartype($f, t))
14+
end
1115
end
1216

1317
function _select_truncation(f, ::AbstractTensorMap,
@@ -388,7 +392,7 @@ function check_input(::typeof(lq_full!), t::AbstractTensorMap, LQ, ::AbstractAlg
388392

389393
# type checks
390394
@assert L isa AbstractTensorMap
391-
@assert R isa AbstractTensorMap
395+
@assert Q isa AbstractTensorMap
392396

393397
# scalartype checks
394398
@check_scalar L t
@@ -407,7 +411,7 @@ function check_input(::typeof(lq_compact!), t::AbstractTensorMap, LQ, ::Abstract
407411

408412
# type checks
409413
@assert L isa AbstractTensorMap
410-
@assert R isa AbstractTensorMap
414+
@assert Q isa AbstractTensorMap
411415

412416
# scalartype checks
413417
@check_scalar L t
@@ -546,7 +550,7 @@ function initialize_output(::typeof(right_polar!), t::AbstractTensorMap,
546550
::AbstractAlgorithm)
547551
P = similar(t, codomain(t) codomain(t))
548552
Wᴴ = similar(t, space(t))
549-
return Wᴴ, P
553+
return P, Wᴴ
550554
end
551555

552556
# Needed to get algorithm selection to behave

src/tensors/factorizations/truncation.jl

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -64,48 +64,29 @@ function truncate!(::typeof(left_null!),
6464
return
6565
end
6666

67-
function truncate!(::typeof(eigh_trunc!),
68-
(D, V)::Tuple{AbstractTensorMap,AbstractTensorMap},
69-
strategy::TruncationStrategy)
70-
ind = findtruncated(diagview(D), strategy)
71-
V_truncated = spacetype(D)(c => length(I) for (c, I) in ind)
72-
73-
= DiagonalTensorMap{scalartype(D)}(undef, V_truncated)
74-
for (c, b) in blocks(D̃)
75-
I = get(ind, c, nothing)
76-
@assert !isnothing(I)
77-
copy!(b.diag, @view(block(D, c).diag[I]))
78-
end
79-
80-
= similar(V, V_truncated domain(V))
81-
for (c, b) in blocks(Ṽ)
82-
I = get(ind, c, nothing)
83-
@assert !isnothing(I)
84-
copy!(b, @view(block(V, c)[I, :]))
85-
end
86-
87-
return D̃, Ṽ
88-
end
89-
function truncate!(::typeof(eig_trunc!), (D, V)::Tuple{AbstractTensorMap,AbstractTensorMap},
90-
strategy::TruncationStrategy)
91-
ind = findtruncated(diagview(D), strategy)
92-
V_truncated = spacetype(D)(c => length(I) for (c, I) in ind)
67+
for f! in (:eig_trunc!, :eigh_trunc!)
68+
@eval function truncate!(::typeof($f!),
69+
(D, V)::Tuple{AbstractTensorMap,AbstractTensorMap},
70+
strategy::TruncationStrategy)
71+
ind = findtruncated(diagview(D), strategy)
72+
V_truncated = spacetype(D)(c => length(I) for (c, I) in ind)
73+
74+
= DiagonalTensorMap{scalartype(D)}(undef, V_truncated)
75+
for (c, b) in blocks(D̃)
76+
I = get(ind, c, nothing)
77+
@assert !isnothing(I)
78+
copy!(b.diag, @view(block(D, c).diag[I]))
79+
end
9380

94-
= DiagonalTensorMap{scalartype(D)}(undef, V_truncated)
95-
for (c, b) in blocks()
96-
I = get(ind, c, nothing)
97-
@assert !isnothing(I)
98-
copy!(b.diag, @view(block(D, c).diag[I]))
99-
end
81+
= similar(V, codomain(V) V_truncated)
82+
for (c, b) in blocks()
83+
I = get(ind, c, nothing)
84+
@assert !isnothing(I)
85+
copy!(b, @view(block(V, c)[:, I]))
86+
end
10087

101-
= similar(V, V_truncated domain(V))
102-
for (c, b) in blocks(Ṽ)
103-
I = get(ind, c, nothing)
104-
@assert !isnothing(I)
105-
copy!(b, @view(block(V, c)[I, :]))
88+
return D̃, Ṽ
10689
end
107-
108-
return D̃, Ṽ
10990
end
11091

11192
# Find truncation
@@ -201,7 +182,7 @@ function findtruncated(Sd::SectorDict, strategy::TruncationKeepSorted)
201182
truncdim[cmin] -= 1
202183
totaldim -= dim(cmin)
203184
if totaldim < strategy.howmany
204-
truncdim[cmin] += 1
185+
# truncdim[cmin] += 1
205186
break
206187
end
207188
if truncdim[cmin] == 0

src/tensors/factorizations/utility.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function permutedcopy_oftype(t::AbstractTensorMap, T::Type{<:Number}, p::Index2T
1616
return permute!(similar(t, T, permute(space(t), p)), t, p)
1717
end
1818
function copy_oftype(t::AbstractTensorMap, T::Type{<:Number})
19-
return copy!(similar(t, T), t)
19+
return copy!(similar(t, T, space(t)), t)
2020
end
2121

2222
function _reverse!(t::AbstractTensorMap; dims=:)

src/tensors/linalg.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,12 +287,13 @@ end
287287

288288
_default_rtol(t) = eps(real(float(scalartype(t)))) * min(dim(domain(t)), dim(codomain(t)))
289289

290-
function LinearAlgebra.rank(t::AbstractTensorMap; atol::Real=0,
291-
rtol::Real=atol > 0 ? 0 : _default_rtol(t))
292-
dim(t) == 0 && return 0
290+
function LinearAlgebra.rank(t::AbstractTensorMap;
291+
atol::Real=0, rtol::Real=atol > 0 ? 0 : _default_rtol(t))
292+
init = dim(one(sectortype(t))) * 0
293+
dim(t) == 0 && return init
293294
S = LinearAlgebra.svdvals(t)
294295
tol = max(atol, rtol * maximum(first, values(S)))
295-
return sum(cs -> dim(cs[1]) * count(>(tol), cs[2]), S)
296+
return sum(((c, b),) -> dim(c) * count(>(tol), b), S; init)
296297
end
297298

298299
function LinearAlgebra.cond(t::AbstractTensorMap, p::Real=2)

0 commit comments

Comments
 (0)