Skip to content

Commit 4300722

Browse files
committed
small fixes for diagonal
1 parent 46807e5 commit 4300722

File tree

5 files changed

+25
-95
lines changed

5 files changed

+25
-95
lines changed

src/auxiliary/deprecate.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ _kindof(::MatrixAlgebraKit.LAPACK_HouseholderQR) = :qr
6464
_kindof(::MatrixAlgebraKit.LAPACK_HouseholderLQ) = :lq
6565
_kindof(::MatrixAlgebraKit.LAPACK_SVDAlgorithm) = :svd
6666
_kindof(::MatrixAlgebraKit.PolarViaSVD) = :polar
67+
_kindof(::DiagonalAlgorithm) = :svd # shouldn't really matter
6768

6869
_drop_alg(; alg=nothing, kwargs...) = kwargs
6970
_drop_p(; p=nothing, kwargs...) = kwargs

src/tensors/factorizations/diagonal.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ for f! in (:lq_full!, :lq_compact!)
4949
end
5050
end
5151

52+
function initialize_output(::typeof(left_orth!), d::AdjointTensorMap)
53+
return d, similar(d)
54+
end
55+
function initialize_output(::typeof(right_orth!), d::AdjointTensorMap)
56+
return similar(d), d
57+
end
58+
5259
for f! in
5360
(:qr_full!, :qr_compact!, :lq_full!, :lq_compact!, :eig_full!, :eig_trunc!, :eigh_full!,
5461
:eigh_trunc!, :right_orth!, :left_orth!)

src/tensors/factorizations/factorizations.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export qr_full, qr_compact, qr_null
1111
export qr_full!, qr_compact!, qr_null!
1212
export lq_full, lq_compact, lq_null
1313
export lq_full!, lq_compact!, lq_null!
14-
export copy_oftype, permutedcopy_oftype, one!
14+
export copy_oftype, permutedcopy_oftype, factorisation_scalartype, one!
1515
export TruncationScheme, notrunc, truncbelow, truncerr, truncdim, truncspace, PolarViaSVD
1616

1717
using ..TensorKit

test/diagonal.jl

Lines changed: 0 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -185,93 +185,6 @@ diagspacelist = ((ℂ^4)', ℂ[Z2Irrep](0 => 2, 1 => 3),
185185
@planar E2[-1 -2 -3; -4 -5] = B[-1 -2 1; -4 -5] * t'[-3; 1]
186186
@test E1 E2
187187
end
188-
@timedtestset "Factorization" begin
189-
for T in (Float32, ComplexF64)
190-
t = DiagonalTensorMap(rand(T, reduceddim(V)), V)
191-
@testset "eig/eigh" begin
192-
D, W = @constinferred eig_full(t)
193-
@test t * W W * D
194-
D, W = @constinferred eig(t)
195-
@test t * W W * D
196-
t2 = t + t'
197-
D2, V2 = @constinferred eigh(t2)
198-
VdV2 = V2' * V2
199-
@test VdV2 one(VdV2)
200-
@test t2 * V2 V2 * D2
201-
202-
D3 = @constinferred eigh_vals(t2)
203-
@test D2 D3
204-
205-
@test rank(D) rank(t)
206-
@test cond(D) cond(t)
207-
@test all(((s, t),) -> isapprox(s, t),
208-
zip(values(LinearAlgebra.eigvals(D)),
209-
values(LinearAlgebra.eigvals(t))))
210-
D, W = @constinferred eig!(t)
211-
@test D === t
212-
@test W == one(t)
213-
@test t * W W * D
214-
D2, V2 = @constinferred eigh!(t2)
215-
if T <: Real
216-
@test D2 === t2
217-
end
218-
@test V2 == one(t)
219-
@test t2 * V2 V2 * D2
220-
end
221-
@testset "leftorth with $alg" for alg in (TensorKit.DiagonalAlgorithm(),)
222-
Q, R = @constinferred leftorth(t; alg=alg)
223-
QdQ = Q' * Q
224-
@test QdQ one(QdQ)
225-
@test Q * R t
226-
if alg isa Polar
227-
@test isposdef(R)
228-
end
229-
end
230-
@testset "rightorth with $alg" for alg in (TensorKit.DiagonalAlgorithm(),)
231-
L, Q = @constinferred rightorth(t; alg=alg)
232-
QQd = Q * Q'
233-
@test QQd one(QQd)
234-
@test L * Q t
235-
if alg isa Polar
236-
@test isposdef(L)
237-
end
238-
end
239-
@testset "qr_full with $alg" for alg in (TensorKit.DiagonalAlgorithm(),)
240-
Q, R = @constinferred qr_full(t; alg=alg)
241-
@test isisometry(Q)
242-
@test Q * R t
243-
end
244-
@testset "qr_compact with $alg" for alg in (TensorKit.DiagonalAlgorithm(),)
245-
Q, R = @constinferred qr_compact(t; alg=alg)
246-
@test isisometry(Q)
247-
@test Q * R t
248-
end
249-
@testset "lq_full with $alg" for alg in (TensorKit.DiagonalAlgorithm(),)
250-
L, Q = @constinferred lq_full(t; alg=alg)
251-
@test isisometry(Q; side=:right)
252-
@test L * Q t
253-
end
254-
@testset "lq_compact with $alg" for alg in (TensorKit.DiagonalAlgorithm(),)
255-
L, Q = @constinferred lq_compact(t; alg=alg)
256-
@test isisometry(Q; side=:right)
257-
@test L * Q t
258-
end
259-
@testset "tsvd with $alg" for alg in (TensorKit.DiagonalAlgorithm(),)
260-
U, S, Vᴴ = @constinferred tsvd(t; alg=alg)
261-
UdU = U' * U
262-
@test UdU one(UdU)
263-
VdV = Vᴴ * Vᴴ'
264-
@test VdV one(VdV)
265-
@test U * S * Vᴴ t
266-
267-
@test rank(S) rank(t)
268-
@test cond(S) cond(t)
269-
@test all(((s, t),) -> isapprox(s, t),
270-
zip(values(LinearAlgebra.svdvals(S)),
271-
values(LinearAlgebra.svdvals(t))))
272-
end
273-
end
274-
end
275188
@timedtestset "Tensor functions" begin
276189
for T in (Float64, ComplexF64)
277190
d = DiagonalTensorMap(rand(T, reduceddim(V)), V)

test/factorizations.jl

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ for V in spacelist
2828

2929
@testset "QR decomposition" begin
3030
for T in eltypes,
31-
t in (rand(T, W, W), rand(T, W, W)', rand(T, W, V1), rand(T, V1, W)')
31+
t in (rand(T, W, W), rand(T, W, W)', rand(T, W, V1), rand(T, V1, W)',
32+
DiagonalTensorMap(rand(T, reduceddim(V1)), V1))
3233

3334
Q, R = @constinferred qr_full(t)
3435
@test Q * R t
@@ -78,7 +79,8 @@ for V in spacelist
7879

7980
@testset "LQ decomposition" begin
8081
for T in eltypes,
81-
t in (rand(T, W, W), rand(T, W, W)', rand(T, W, V1), rand(T, V1, W)')
82+
t in (rand(T, W, W), rand(T, W, W)', rand(T, W, V1), rand(T, V1, W)',
83+
DiagonalTensorMap(rand(T, reduceddim(V1)), V1))
8284

8385
L, Q = @constinferred lq_full(t)
8486
@test L * Q t
@@ -124,7 +126,8 @@ for V in spacelist
124126

125127
@testset "Polar decomposition" begin
126128
for T in eltypes,
127-
t in (rand(T, W, W), rand(T, W, W)', rand(T, W, V1), rand(T, V1, W)')
129+
t in (rand(T, W, W), rand(T, W, W)', rand(T, W, V1), rand(T, V1, W)',
130+
DiagonalTensorMap(rand(T, reduceddim(V1)), V1))
128131

129132
@assert domain(t) codomain(t)
130133
w, p = @constinferred left_polar(t)
@@ -156,7 +159,8 @@ for V in spacelist
156159
for T in eltypes,
157160
t in (rand(T, W, W), rand(T, W, W)',
158161
rand(T, W, V1), rand(T, V1, W),
159-
rand(T, W, V1)', rand(T, V1, W)')
162+
rand(T, W, V1)', rand(T, V1, W)',
163+
DiagonalTensorMap(rand(T, reduceddim(V1)), V1))
160164

161165
u, s, vᴴ = @constinferred svd_full(t)
162166
@test u * s * vᴴ t
@@ -201,7 +205,11 @@ for V in spacelist
201205
end
202206

203207
@testset "Eigenvalue decomposition" begin
204-
for T in eltypes, t in (rand(T, V1, V1), rand(T, W, W), rand(T, W, W)')
208+
for T in eltypes,
209+
t in
210+
(rand(T, V1, V1), rand(T, W, W), rand(T, W, W)',
211+
DiagonalTensorMap(rand(T, reduceddim(V1)), V1))
212+
205213
d, v = @constinferred eig_full(t)
206214
@test t * v v * d
207215

@@ -213,7 +221,7 @@ for V in spacelist
213221
vdv = v' * v
214222
vdv = (vdv + vdv') / 2
215223
@test @constinferred isposdef(vdv)
216-
@test !isposdef(t) # unlikely for non-hermitian map
224+
t isa DiagonalTensorMap || @test !isposdef(t) # unlikely for non-hermitian map
217225

218226
d, v = @constinferred eig_trunc(t; trunc=truncrank(dim(domain(t)) ÷ 2))
219227
@test t * v v * d
@@ -254,7 +262,8 @@ for V in spacelist
254262
for T in eltypes,
255263
t in (rand(T, W, W), rand(T, W, W)',
256264
rand(T, W, V1), rand(T, V1, W),
257-
rand(T, W, V1)', rand(T, V1, W)')
265+
rand(T, W, V1)', rand(T, V1, W)',
266+
DiagonalTensorMap(rand(T, reduceddim(V1)), V1))
258267

259268
d1, d2 = dim(codomain(t)), dim(domain(t))
260269
@test rank(t) == min(d1, d2)

0 commit comments

Comments
 (0)