Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 1 addition & 88 deletions test/factorizations/eig.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,102 +3,15 @@ using TensorKit
using LinearAlgebra: LinearAlgebra
using MatrixAlgebraKit: DefaultAlgorithm, diagview


spacelist = factorization_spacelist(fast_tests)

eltypes = (Float32, ComplexF64)

for V in spacelist
I = sectortype(first(V))
Istr = TensorKit.type_repr(I)
println("---------------------------------------")
println("Eigenvalue decompositions with symmetry: $Istr")
println("---------------------------------------")
@timedtestset "Eigenvalue decompositions with symmetry: $Istr" verbose = true begin
V1, V2, V3, V4, V5 = V
W = V1 ⊗ V2 ⊗ V3
Vd = fuse(V1 ⊗ V2)

@testset "Eigenvalue decomposition" begin
for T in eltypes,
t in (
rand(T, V1, V1), rand(T, W, W), rand(T, W, W)',
DiagonalTensorMap(rand(T, reduceddim(Vd)), Vd),
)

d, v = @constinferred eig_full(t)
@test t * v ≈ v * d

d, v = @constinferred eig_full(t, DefaultAlgorithm())
@test t * v ≈ v * d

d′ = @constinferred eig_vals(t)
@test d′ ≈ diagview(d)
@test d′ isa TensorKit.SectorVector

d′ = @constinferred eig_vals(t, DefaultAlgorithm())
@test d′ ≈ diagview(d)
@test d′ isa TensorKit.SectorVector

d2 = @constinferred DiagonalTensorMap(d′)
@test d2 ≈ d

vdv = project_hermitian!(v' * v)
@test @constinferred isposdef(vdv)
t isa DiagonalTensorMap || @test !isposdef(t) # unlikely for non-hermitian map

nvals = round(Int, dim(domain(t)) / 2)
d, v = @constinferred eig_trunc(t; trunc = truncrank(nvals))
@test t * v ≈ v * d
@test abs(dim(domain(d)) - nvals) ≤ maximum(c -> blockdim(domain(t), c), blocksectors(t); init = 1)

d, v = @constinferred eig_trunc(t, DefaultAlgorithm(; trunc = truncrank(nvals)))
@test t * v ≈ v * d
@test abs(dim(domain(d)) - nvals) ≤ maximum(c -> blockdim(domain(t), c), blocksectors(t); init = 1)

t2 = @constinferred project_hermitian(t)
D, V = eigen(t2)
@test isisometric(V)
D̃, Ṽ = @constinferred eigh_full(t2)
@test D ≈ D̃
@test V ≈ Ṽ

D̃, Ṽ = @constinferred eigh_full(t2, DefaultAlgorithm())
@test D ≈ D̃
@test V ≈ Ṽ

λ = minimum(real, diagview(D))
@test cond(Ṽ) ≈ one(real(T))
@test isposdef(t2) == isposdef(λ)
@test isposdef(t2 - λ * one(t2) + 0.1 * one(t2))
@test !isposdef(t2 - λ * one(t2) - 0.1 * one(t2))

d, v = @constinferred eigh_full(t2)
@test t2 * v ≈ v * d
@test isunitary(v)

d′ = @constinferred eigh_vals(t2)
@test d′ ≈ diagview(d)
@test d′ isa TensorKit.SectorVector

d′ = @constinferred eigh_vals(t2, DefaultAlgorithm())
@test d′ ≈ diagview(d)
@test d′ isa TensorKit.SectorVector

λ = minimum(real, diagview(d))
@test cond(v) ≈ one(real(T))
@test isposdef(t2) == isposdef(λ)
@test isposdef(t2 - λ * one(t) + 0.1 * one(t2))
@test !isposdef(t2 - λ * one(t) - 0.1 * one(t2))

d, v = @constinferred eigh_trunc(t2; trunc = truncrank(nvals))
@test t2 * v ≈ v * d
@test abs(dim(domain(d)) - nvals) ≤ maximum(c -> blockdim(domain(t), c), blocksectors(t); init = 1)

d, v = @constinferred eigh_trunc(t2, DefaultAlgorithm(; trunc = truncrank(nvals)))
@test t2 * v ≈ v * d
@test abs(dim(domain(d)) - nvals) ≤ maximum(c -> blockdim(domain(t), c), blocksectors(t); init = 1)
end
end
TensorKitTestSuite.run_testsuite(:factorizations, "eigenvalue decomposition", V)
end
end
188 changes: 2 additions & 186 deletions test/factorizations/ortho.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,200 +3,16 @@ using TensorKit
using LinearAlgebra: LinearAlgebra
using MatrixAlgebraKit: DefaultAlgorithm, diagview


spacelist = factorization_spacelist(fast_tests)

eltypes = (Float32, ComplexF64)

for V in spacelist
I = sectortype(first(V))
Istr = TensorKit.type_repr(I)
println("---------------------------------------")
println("QR and LQ decompositions with symmetry: $Istr")
println("---------------------------------------")
@timedtestset "QR and LQ decompositions with symmetry: $Istr" verbose = true begin
V1, V2, V3, V4, V5 = V
W = V1 ⊗ V2 ⊗ V3
Vd = fuse(V1 ⊗ V2)

@testset "QR decomposition" begin
for T in eltypes,
t in (
rand(T, W, W), rand(T, W, W)',
rand(T, (V1 ⊗ V2 ⊗ V3), (V4 ⊗ V5)'), rand(T, (V1 ⊗ V2 ⊗ V3), (V4 ⊗ V5)')',
rand(T, (V1 ⊗ V2)', (V3 ⊗ V4 ⊗ V5)), rand(T, (V1 ⊗ V2)', (V3 ⊗ V4 ⊗ V5))',
DiagonalTensorMap(rand(T, reduceddim(Vd)), Vd),
)

Q, R = @constinferred qr_full(t)
@test Q * R ≈ t
@test isunitary(Q)

Q, R = @constinferred qr_full(t, DefaultAlgorithm())
@test Q * R ≈ t
@test isunitary(Q)

Q, R = @constinferred qr_compact(t)
@test Q * R ≈ t
@test isisometric(Q)

Q, R = @constinferred qr_compact(t, DefaultAlgorithm())
@test Q * R ≈ t
@test isisometric(Q)

Q, R = @constinferred left_orth(t)
@test Q * R ≈ t
@test isisometric(Q)

Q, R = @constinferred left_orth(t, DefaultAlgorithm())
@test Q * R ≈ t
@test isisometric(Q)

N = @constinferred qr_null(t)
@test isisometric(N)
@test norm(N' * t) ≈ 0 atol = 100 * eps(norm(t))

N = @constinferred qr_null(t, DefaultAlgorithm())
@test isisometric(N)
@test norm(N' * t) ≈ 0 atol = 100 * eps(norm(t))

N = @constinferred left_null(t)
@test isisometric(N)
@test norm(N' * t) ≈ 0 atol = 100 * eps(norm(t))

N = @constinferred left_null(t, DefaultAlgorithm())
@test isisometric(N)
@test norm(N' * t) ≈ 0 atol = 100 * eps(norm(t))
end

# empty tensor
for T in eltypes
t = rand(T, V1 ⊗ V2, zerospace(V1))

Q, R = @constinferred qr_full(t)
@test Q * R ≈ t
@test isunitary(Q)
@test dim(R) == dim(t) == 0

Q, R = @constinferred qr_full(t, DefaultAlgorithm())
@test Q * R ≈ t
@test isunitary(Q)
@test dim(R) == dim(t) == 0

Q, R = @constinferred qr_compact(t)
@test Q * R ≈ t
@test isisometric(Q)
@test dim(Q) == dim(R) == dim(t)

Q, R = @constinferred qr_compact(t, DefaultAlgorithm())
@test Q * R ≈ t
@test isisometric(Q)
@test dim(Q) == dim(R) == dim(t)

Q, R = @constinferred left_orth(t)
@test Q * R ≈ t
@test isisometric(Q)
@test dim(Q) == dim(R) == dim(t)

Q, R = @constinferred left_orth(t, DefaultAlgorithm())
@test Q * R ≈ t
@test isisometric(Q)
@test dim(Q) == dim(R) == dim(t)

N = @constinferred qr_null(t)
@test isunitary(N)
@test norm(N' * t) ≈ 0 atol = 100 * eps(norm(t))

N = @constinferred qr_null(t, DefaultAlgorithm())
@test isunitary(N)
@test norm(N' * t) ≈ 0 atol = 100 * eps(norm(t))
end
end

@testset "LQ decomposition" begin
for T in eltypes,
t in (
rand(T, W, W), rand(T, W, W)',
rand(T, (V1 ⊗ V2), (V3 ⊗ V4 ⊗ V5)'), rand(T, (V1 ⊗ V2), (V3 ⊗ V4 ⊗ V5)')',
rand(T, (V1 ⊗ V2 ⊗ V3)', (V4 ⊗ V5)), rand(T, (V1 ⊗ V2 ⊗ V3)', (V4 ⊗ V5))',
DiagonalTensorMap(rand(T, reduceddim(Vd)), Vd),
)

L, Q = @constinferred lq_full(t)
@test L * Q ≈ t
@test isunitary(Q)

L, Q = @constinferred lq_full(t, DefaultAlgorithm())
@test L * Q ≈ t
@test isunitary(Q)

L, Q = @constinferred lq_compact(t)
@test L * Q ≈ t
@test isisometric(Q; side = :right)

L, Q = @constinferred lq_compact(t, DefaultAlgorithm())
@test L * Q ≈ t
@test isisometric(Q; side = :right)

L, Q = @constinferred right_orth(t)
@test L * Q ≈ t
@test isisometric(Q; side = :right)

L, Q = @constinferred right_orth(t, DefaultAlgorithm())
@test L * Q ≈ t
@test isisometric(Q; side = :right)

Nᴴ = @constinferred lq_null(t)
@test isisometric(Nᴴ; side = :right)
@test norm(t * Nᴴ') ≈ 0 atol = 100 * eps(norm(t))

Nᴴ = @constinferred lq_null(t, DefaultAlgorithm())
@test isisometric(Nᴴ; side = :right)
@test norm(t * Nᴴ') ≈ 0 atol = 100 * eps(norm(t))
end

for T in eltypes
# empty tensor
t = rand(T, zerospace(V1), V1 ⊗ V2)

L, Q = @constinferred lq_full(t)
@test L * Q ≈ t
@test isunitary(Q)
@test dim(L) == dim(t) == 0

L, Q = @constinferred lq_full(t, DefaultAlgorithm())
@test L * Q ≈ t
@test isunitary(Q)
@test dim(L) == dim(t) == 0

L, Q = @constinferred lq_compact(t)
@test L * Q ≈ t
@test isisometric(Q; side = :right)
@test dim(Q) == dim(L) == dim(t)

L, Q = @constinferred lq_compact(t, DefaultAlgorithm())
@test L * Q ≈ t
@test isisometric(Q; side = :right)
@test dim(Q) == dim(L) == dim(t)

L, Q = @constinferred right_orth(t)
@test L * Q ≈ t
@test isisometric(Q; side = :right)
@test dim(Q) == dim(L) == dim(t)

L, Q = @constinferred right_orth(t, DefaultAlgorithm())
@test L * Q ≈ t
@test isisometric(Q; side = :right)
@test dim(Q) == dim(L) == dim(t)

Nᴴ = @constinferred lq_null(t)
@test isunitary(Nᴴ)
@test norm(t * Nᴴ') ≈ 0 atol = 100 * eps(norm(t))

Nᴴ = @constinferred lq_null(t, DefaultAlgorithm())
@test isunitary(Nᴴ)
@test norm(t * Nᴴ') ≈ 0 atol = 100 * eps(norm(t))
end
end
TensorKitTestSuite.run_testsuite(:factorizations, "QR decomposition", V)
TensorKitTestSuite.run_testsuite(:factorizations, "LQ decomposition", V)
end
end
Loading
Loading