Skip to content

Commit 2a85190

Browse files
github-actions[bot]CompatHelper Julialkdvos
authored
CompatHelper: bump compat for TensorKit to 0.16, (keep existing compat) (#37)
* CompatHelper: bump compat for TensorKit to 0.16, (keep existing compat) * rework factorizations to be MAK v0.6 compatible * temporary fix for TensorKit * cleanup * Bump v0.3.2 --------- Co-authored-by: CompatHelper Julia <compathelper_noreply@julialang.org> Co-authored-by: Lukas Devos <ldevos98@gmail.com>
1 parent dfa3646 commit 2a85190

4 files changed

Lines changed: 184 additions & 249 deletions

File tree

‎Project.toml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "BlockTensorKit"
22
uuid = "5f87ffc2-9cf1-4a46-8172-465d160bd8cd"
3+
version = "0.3.2"
34
authors = ["Lukas Devos <ldevos98@gmail.com> and contributors"]
4-
version = "0.3.1"
55

66
[deps]
77
BlockArrays = "8e7c35d0-a365-5155-bbbb-fb81a777f24e"
@@ -21,11 +21,11 @@ BlockArrays = "1"
2121
Combinatorics = "1"
2222
Compat = "4.13"
2323
LinearAlgebra = "1"
24-
MatrixAlgebraKit = "0.5"
24+
MatrixAlgebraKit = "0.6"
2525
Random = "1"
2626
SafeTestsets = "0.1"
2727
Strided = "2"
28-
TensorKit = "0.15.2"
28+
TensorKit = "0.16"
2929
TensorOperations = "5"
3030
Test = "1"
3131
TestExtras = "0.2, 0.3"

‎src/linalg/factorizations.jl‎

Lines changed: 64 additions & 203 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using MatrixAlgebraKit
22
using MatrixAlgebraKit: AbstractAlgorithm, YALAPACK.BlasMat, Algorithm
33
import MatrixAlgebraKit as MAK
4-
using TensorKit.Factorizations: @check_space, @check_scalar
54

65
# Type piracy for defining the MAK rules on BlockArrays!
76
# -----------------------------------------------------
@@ -16,70 +15,74 @@ function MatrixAlgebraKit.one!(A::BlockBlasMat)
1615
return A
1716
end
1817

19-
for f in (
20-
:svd_compact, :svd_full, :svd_vals, :qr_compact, :qr_full, :qr_null,
21-
:lq_compact, :lq_full, :lq_null, :eig_full, :eig_vals, :eigh_full,
22-
:eigh_vals, :left_polar, :right_polar,
23-
)
18+
for f in
19+
[
20+
:svd_compact, :svd_full, :svd_vals,
21+
:qr_compact, :qr_full, :qr_null,
22+
:lq_compact, :lq_full, :lq_null,
23+
:eig_full, :eig_vals, :eigh_full, :eigh_vals,
24+
:left_polar, :right_polar,
25+
:project_hermitian, :project_antihermitian, :project_isometric,
26+
]
2427
f! = Symbol(f, :!)
25-
@eval MAK.$f!(t::BlockBlasMat, F, alg::AbstractAlgorithm) = $f!(Array(t), alg)
26-
@eval MAK.$f!(t::BlockBlasMat, F, alg::MAK.DiagonalAlgorithm) = error("Not diagonal")
28+
@eval MAK.default_algorithm(::typeof($f!), ::Type{T}; kwargs...) where {T <: AbstractBlockTensorMap} =
29+
MAK.default_algorithm($f!, eltype(T); kwargs...)
2730
end
2831

29-
# disambiguations
30-
for (f!, Alg) in (
31-
(:lq_compact!, :LAPACK_HouseholderLQ), (:lq_full!, :LAPACK_HouseholderLQ), (:lq_null!, :LAPACK_HouseholderLQ),
32-
(:lq_compact!, :LQViaTransposedQR), (:lq_full!, :LQViaTransposedQR), (:lq_null!, :LQViaTransposedQR),
33-
(:qr_compact!, :LAPACK_HouseholderQR), (:qr_full!, :LAPACK_HouseholderQR), (:qr_null!, :LAPACK_HouseholderQR),
34-
(:svd_compact!, :LAPACK_SVDAlgorithm), (:svd_full!, :LAPACK_SVDAlgorithm), (:svd_vals!, :LAPACK_SVDAlgorithm),
35-
(:eig_full!, :LAPACK_EigAlgorithm), (:eig_trunc!, :TruncatedAlgorithm), (:eig_vals!, :LAPACK_EigAlgorithm),
36-
(:eigh_full!, :LAPACK_EighAlgorithm), (:eigh_trunc!, :TruncatedAlgorithm), (:eigh_vals!, :LAPACK_EighAlgorithm),
37-
(:left_polar!, :PolarViaSVD), (:right_polar!, :PolarViaSVD),
32+
for f! in (
33+
:qr_compact!, :qr_full!, :lq_compact!, :lq_full!,
34+
:eig_full!, :eigh_full!, :svd_compact!, :svd_full!,
35+
:left_polar!, :right_polar!,
3836
)
39-
@eval MAK.$f!(t::BlockBlasMat, F, alg::MAK.$Alg) = $f!(Array(t), alg)
40-
end
41-
42-
const GPU_QRAlgorithm = Union{MAK.CUSOLVER_HouseholderQR, MAK.ROCSOLVER_HouseholderQR}
43-
for f! in (:qr_compact!, :qr_full!, :qr_null!)
44-
@eval MAK.$f!(t::BlockBlasMat, QR, alg::GPU_QRAlgorithm) = error()
37+
@eval function MAK.$f!(t::AbstractBlockTensorMap, F, alg::AbstractAlgorithm)
38+
TensorKit.foreachblock(t, F...) do _, (tblock, Fblocks...)
39+
Fblocks′ = MAK.$f!(Array(tblock), alg)
40+
# deal with the case where the output is not in-place
41+
for (b′, b) in zip(Fblocks′, Fblocks)
42+
b === b′ || copy!(b, b′)
43+
end
44+
return nothing
45+
end
46+
return F
47+
end
4548
end
4649

47-
for (f!, Alg) in (
48-
(:eigh_full!, :GPU_EighAlgorithm), (:eigh_vals!, :GPU_EighAlgorithm),
49-
(:eig_full!, :GPU_EigAlgorithm), (:eig_vals!, :GPU_EigAlgorithm),
50-
(:svd_full!, :GPU_SVDAlgorithm), (:svd_compact!, :GPU_SVDAlgorithm), (:svd_vals!, :GPU_SVDAlgorithm),
50+
# Handle these separately because single output instead of tuple
51+
for f! in (
52+
:qr_null!, :lq_null!,
53+
:svd_vals!, :eig_vals!, :eigh_vals!,
54+
:project_hermitian!, :project_antihermitian!, :project_isometric!,
5155
)
52-
@eval MAK.$f!(t::BlockBlasMat, F, alg::MAK.$Alg) = error()
56+
@eval function MAK.$f!(t::AbstractBlockTensorMap, N, alg::AbstractAlgorithm)
57+
TensorKit.foreachblock(t, N) do _, (tblock, Nblock)
58+
Nblock′ = MAK.$f!(Array(tblock), alg)
59+
# deal with the case where the output is not the same as the input
60+
Nblock === Nblock′ || copy!(Nblock, Nblock′)
61+
return nothing
62+
end
63+
return N
64+
end
5365
end
5466

55-
56-
for f in (:qr, :lq, :eig, :eigh, :gen_eig, :svd, :polar)
57-
default_f_algorithm = Symbol(:default_, f, :_algorithm)
58-
@eval MAK.$default_f_algorithm(::Type{<:BlockBlasMat{T}}; kwargs...) where {T} =
59-
MAK.$default_f_algorithm(Matrix{T}; kwargs...)
67+
# specializations until fixes in base package
68+
function MAK.is_left_isometric(A::BlockMatrix; atol::Real = 0, rtol::Real = MAK.defaulttol(A), norm = LinearAlgebra.norm)
69+
P = A' * A
70+
nP = norm(P) # isapprox would use `rtol * max(norm(P), norm(I))`
71+
for I in MAK.diagind(P)
72+
P[I] -= 1
73+
end
74+
return norm(P) <= max(atol, rtol * nP) # assume that the norm of I is `sqrt(n)`
6075
end
61-
62-
# Make sure sparse blocktensormaps have dense outputs
63-
function MAK.check_input(::typeof(qr_full!), t::AbstractBlockTensorMap, QR, ::AbstractAlgorithm)
64-
Q, R = QR
65-
66-
# type checks
67-
@assert Q isa AbstractTensorMap
68-
@assert R isa AbstractTensorMap
69-
70-
# scalartype checks
71-
@check_scalar Q t
72-
@check_scalar R t
73-
74-
# space checks
75-
V_Q = ⊕(fuse(codomain(t)))
76-
@check_space(Q, codomain(t) ← V_Q)
77-
@check_space(R, V_Q ← domain(t))
78-
79-
return nothing
76+
function MAK.is_right_isometric(A::BlockMatrix; atol::Real = 0, rtol::Real = MAK.defaulttol(A), norm = LinearAlgebra.norm)
77+
P = A * A'
78+
nP = norm(P) # isapprox would use `rtol * max(norm(P), norm(I))`
79+
for I in MAK.diagind(P)
80+
P[I] -= 1
81+
end
82+
return norm(P) <= max(atol, rtol * nP) # assume that the norm of I is `sqrt(n)`
8083
end
81-
MAK.check_input(::typeof(qr_full!), t::AbstractBlockTensorMap, QR, ::DiagonalAlgorithm) = error()
8284

85+
# Make sure sparse blocktensormaps have dense outputs
8386
function MAK.initialize_output(::typeof(qr_full!), t::AbstractBlockTensorMap, ::AbstractAlgorithm)
8487
V_Q = ⊕(fuse(codomain(t)))
8588
Q = dense_similar(t, codomain(t) ← V_Q)
@@ -99,26 +102,6 @@ function MAK.initialize_output(::typeof(qr_null!), t::AbstractBlockTensorMap, ::
99102
return N
100103
end
101104

102-
function MAK.check_input(::typeof(lq_full!), t::AbstractBlockTensorMap, LQ, ::AbstractAlgorithm)
103-
L, Q = LQ
104-
105-
# type checks
106-
@assert L isa AbstractTensorMap
107-
@assert Q isa AbstractTensorMap
108-
109-
# scalartype checks
110-
@check_scalar L t
111-
@check_scalar Q t
112-
113-
# space checks
114-
V_Q = ⊕(fuse(domain(t)))
115-
@check_space(L, codomain(t) ← V_Q)
116-
@check_space(Q, V_Q ← domain(t))
117-
118-
return nothing
119-
end
120-
MAK.check_input(::typeof(lq_full!), t::AbstractBlockTensorMap, LQ, ::DiagonalAlgorithm) = error()
121-
122105
function MAK.initialize_output(::typeof(lq_full!), t::AbstractBlockTensorMap, ::AbstractAlgorithm)
123106
V_Q = ⊕(fuse(domain(t)))
124107
L = dense_similar(t, codomain(t) ← V_Q)
@@ -138,99 +121,18 @@ function MAK.initialize_output(::typeof(lq_null!), t::AbstractBlockTensorMap, ::
138121
return N
139122
end
140123

141-
function MAK.check_input(::typeof(MAK.left_orth_polar!), t::AbstractBlockTensorMap, WP, ::AbstractAlgorithm)
142-
codomain(t) ≿ domain(t) ||
143-
throw(ArgumentError("Polar decomposition requires `codomain(t) ≿ domain(t)`"))
144-
145-
W, P = WP
146-
@assert W isa AbstractTensorMap
147-
@assert P isa AbstractTensorMap
148-
149-
# scalartype checks
150-
@check_scalar W t
151-
@check_scalar P t
152-
153-
# space checks
154-
VW = ⊕(fuse(domain(t)))
155-
@check_space(W, codomain(t) ← VW)
156-
@check_space(P, VW ← domain(t))
157-
158-
return nothing
159-
end
160124
function MAK.initialize_output(::typeof(left_polar!), t::AbstractBlockTensorMap, ::AbstractAlgorithm)
161125
W = dense_similar(t, space(t))
162126
P = dense_similar(t, domain(t) ← domain(t))
163127
return W, P
164128
end
165129

166-
function MAK.check_input(::typeof(MAK.right_orth_polar!), t::AbstractBlockTensorMap, PWᴴ, ::AbstractAlgorithm)
167-
codomain(t) ≾ domain(t) ||
168-
throw(ArgumentError("Polar decomposition requires `domain(t) ≿ codomain(t)`"))
169-
170-
P, Wᴴ = PWᴴ
171-
@assert P isa AbstractTensorMap
172-
@assert Wᴴ isa AbstractTensorMap
173-
174-
# scalartype checks
175-
@check_scalar P t
176-
@check_scalar Wᴴ t
177-
178-
# space checks
179-
VW = ⊕(fuse(codomain(t)))
180-
@check_space(P, codomain(t) ← VW)
181-
@check_space(Wᴴ, VW ← domain(t))
182-
183-
return nothing
184-
end
185130
function MAK.initialize_output(::typeof(right_polar!), t::AbstractBlockTensorMap, ::AbstractAlgorithm)
186131
P = dense_similar(t, codomain(t) ← codomain(t))
187132
Wᴴ = dense_similar(t, space(t))
188133
return P, Wᴴ
189134
end
190135

191-
function MAK.initialize_output(::typeof(left_null!), t::AbstractBlockTensorMap, ::AbstractAlgorithm)
192-
V_Q = infimum(fuse(codomain(t)), fuse(domain(t)))
193-
V_N = ⊖(fuse(codomain(t)), V_Q)
194-
return dense_similar(t, codomain(t) ← V_N)
195-
end
196-
function MAK.initialize_output(::typeof(right_null!), t::AbstractBlockTensorMap, ::AbstractAlgorithm)
197-
V_Q = infimum(fuse(codomain(t)), fuse(domain(t)))
198-
V_N = ⊖(fuse(domain(t)), V_Q)
199-
return dense_similar(t, V_N ← domain(t))
200-
end
201-
202-
function MAK.check_input(::typeof(eigh_full!), t::AbstractBlockTensorMap, DV, ::AbstractAlgorithm)
203-
domain(t) == codomain(t) ||
204-
throw(ArgumentError("Eigenvalue decomposition requires square input tensor"))
205-
206-
D, V = DV
207-
208-
# type checks
209-
@assert D isa DiagonalTensorMap
210-
@assert V isa AbstractTensorMap
211-
212-
# scalartype checks
213-
@check_scalar D t real
214-
@check_scalar V t
215-
216-
# space checks
217-
V_D = ⊕(fuse(domain(t)))
218-
@check_space(D, V_D ← V_D)
219-
@check_space(V, codomain(t) ← V_D)
220-
221-
return nothing
222-
end
223-
MAK.check_input(::typeof(eigh_full!), t::AbstractBlockTensorMap, DV, ::DiagonalAlgorithm) = error()
224-
225-
function MAK.check_input(::typeof(eigh_vals!), t::AbstractBlockTensorMap, D, ::AbstractAlgorithm)
226-
@check_scalar D t real
227-
@assert D isa DiagonalTensorMap
228-
V_D = ⊕(fuse(domain(t)))
229-
@check_space(D, V_D ← V_D)
230-
return nothing
231-
end
232-
MAK.check_input(::typeof(eigh_vals!), t::AbstractBlockTensorMap, D, ::DiagonalAlgorithm) = error()
233-
234136
function MAK.initialize_output(::typeof(eigh_full!), t::AbstractBlockTensorMap, ::AbstractAlgorithm)
235137
V_D = ⊕(fuse(domain(t)))
236138
T = real(scalartype(t))
@@ -239,30 +141,6 @@ function MAK.initialize_output(::typeof(eigh_full!), t::AbstractBlockTensorMap,
239141
return D, V
240142
end
241143

242-
243-
function MAK.check_input(::typeof(eig_full!), t::AbstractBlockTensorMap, DV, ::AbstractAlgorithm)
244-
domain(t) == codomain(t) ||
245-
throw(ArgumentError("Eigenvalue decomposition requires square input tensor"))
246-
247-
D, V = DV
248-
249-
# type checks
250-
@assert D isa DiagonalTensorMap
251-
@assert V isa AbstractTensorMap
252-
253-
# scalartype checks
254-
@check_scalar D t complex
255-
@check_scalar V t complex
256-
257-
# space checks
258-
V_D = ⊕(fuse(domain(t)))
259-
@check_space(D, V_D ← V_D)
260-
@check_space(V, codomain(t) ← V_D)
261-
262-
return nothing
263-
end
264-
MAK.check_input(::typeof(eig_full!), t::AbstractBlockTensorMap, DV, ::DiagonalAlgorithm) = error()
265-
266144
function MAK.initialize_output(::typeof(eig_full!), t::AbstractBlockTensorMap, ::AbstractAlgorithm)
267145
V_D = ⊕(fuse(domain(t)))
268146
Tc = complex(scalartype(t))
@@ -271,28 +149,6 @@ function MAK.initialize_output(::typeof(eig_full!), t::AbstractBlockTensorMap, :
271149
return D, V
272150
end
273151

274-
function MAK.check_input(::typeof(svd_full!), t::AbstractBlockTensorMap, USVᴴ, ::AbstractAlgorithm)
275-
U, S, Vᴴ = USVᴴ
276-
277-
# type checks
278-
@assert U isa AbstractTensorMap
279-
@assert S isa AbstractTensorMap
280-
@assert Vᴴ isa AbstractTensorMap
281-
282-
# scalartype checks
283-
@check_scalar U t
284-
@check_scalar S t real
285-
@check_scalar Vᴴ t
286-
287-
# space checks
288-
V_cod = ⊕(fuse(codomain(t)))
289-
V_dom = ⊕(fuse(domain(t)))
290-
@check_space(U, codomain(t) ← V_cod)
291-
@check_space(S, V_cod ← V_dom)
292-
@check_space(Vᴴ, V_dom ← domain(t))
293-
294-
return nothing
295-
end
296152
function MAK.initialize_output(::typeof(svd_full!), t::AbstractBlockTensorMap, ::AbstractAlgorithm)
297153
V_cod = ⊕(fuse(codomain(t)))
298154
V_dom = ⊕(fuse(domain(t)))
@@ -312,8 +168,13 @@ end
312168
# Disambiguate Diagonal implementations
313169
# -------------------------------------
314170
# these shouldn't ever happen as blocktensors aren't diagonal
315-
for f! in (:eig_full!, :eigh_full!, :lq_compact!, :lq_full!, :qr_compact!, :qr_full!, :svd_full!)
316-
@eval function MAK.initialize_output(::typeof($f!), t::AbstractBlockTensorMap, alg::DiagonalAlgorithm)
171+
for f! in (
172+
:eig_full!, :eig_vals!, :eigh_full!, :eigh_vals!,
173+
:lq_compact!, :lq_full!, :qr_compact!, :qr_full!,
174+
:svd_full!, :svd_compact!, :svd_vals!,
175+
)
176+
@eval MAK.initialize_output(::typeof($f!), t::AbstractBlockTensorMap, ::DiagonalAlgorithm) =
177+
error("Blocktensors are incompatible with diagonal algorithm")
178+
@eval MAK.$f!(::AbstractBlockTensorMap, x, ::DiagonalAlgorithm) =
317179
error("Blocktensors are incompatible with diagonal algorithm")
318-
end
319180
end

0 commit comments

Comments
 (0)