|
| 1 | +spacelist = try |
| 2 | + if ENV["CI"] == "true" |
| 3 | + println("Detected running on CI") |
| 4 | + if Sys.iswindows() |
| 5 | + (Vtr, Vℤ₃, VU₁, VfU₁, VCU₁, VSU₂) |
| 6 | + elseif Sys.isapple() |
| 7 | + (Vtr, Vℤ₃, VfU₁, VfSU₂) |
| 8 | + else |
| 9 | + (Vtr, VU₁, VCU₁, VSU₂, VfSU₂) |
| 10 | + end |
| 11 | + else |
| 12 | + (Vtr, Vℤ₃, VU₁, VfU₁, VCU₁, VSU₂, VfSU₂) |
| 13 | + end |
| 14 | +catch |
| 15 | + (Vtr, Vℤ₃, VU₁, VfU₁, VCU₁, VSU₂, VfSU₂) |
| 16 | +end |
| 17 | + |
| 18 | +for V in spacelist |
| 19 | + I = sectortype(first(V)) |
| 20 | + Istr = TensorKit.type_repr(I) |
| 21 | + println("---------------------------------------") |
| 22 | + println("Tensors with symmetry: $Istr") |
| 23 | + println("---------------------------------------") |
| 24 | + @timedtestset "Tensors with symmetry: $Istr" verbose = true begin |
| 25 | + V1, V2, V3, V4, V5 = V |
| 26 | + @timedtestset "Factorization" begin |
| 27 | + W = V1 ⊗ V2 |
| 28 | + @testset for T in (Float32, ComplexF64) |
| 29 | + # Test both a normal tensor and an adjoint one. |
| 30 | + ts = (rand(T, W, W'), rand(T, W, W')') |
| 31 | + @testset for t in ts |
| 32 | + # test squares and rectangles here |
| 33 | + @testset "leftorth with $alg" for alg in |
| 34 | + (TensorKit.LAPACK_HouseholderQR(), |
| 35 | + TensorKit.LAPACK_HouseholderQR(positive=true), |
| 36 | + #TensorKit.QL(), |
| 37 | + #TensorKit.QLpos(), |
| 38 | + TensorKit.PolarViaSVD(TensorKit.LAPACK_QRIteration()), |
| 39 | + TensorKit.PolarViaSVD(TensorKit.LAPACK_DivideAndConquer()), |
| 40 | + TensorKit.LAPACK_QRIteration(), |
| 41 | + TensorKit.LAPACK_DivideAndConquer()) |
| 42 | + Q, R = @constinferred leftorth(t; alg=alg) |
| 43 | + @test isisometry(Q) |
| 44 | + tQR = Q * R |
| 45 | + @test tQR ≈ t |
| 46 | + end |
| 47 | + @testset "leftnull with $alg" for alg in |
| 48 | + (TensorKit.LAPACK_HouseholderQR(), |
| 49 | + TensorKit.LAPACK_QRIteration(), |
| 50 | + TensorKit.LAPACK_DivideAndConquer()) |
| 51 | + N = @constinferred leftnull(t; alg=alg) |
| 52 | + @test isisometry(N) |
| 53 | + @test norm(N' * t) < 100 * eps(norm(t)) |
| 54 | + end |
| 55 | + @testset "rightorth with $alg" for alg in |
| 56 | + (#TensorKit.RQ(), TensorKit.RQpos(), |
| 57 | + TensorKit.LAPACK_HouseholderLQ(), |
| 58 | + TensorKit.LAPACK_HouseholderLQ(positive=true), |
| 59 | + TensorKit.PolarViaSVD(TensorKit.LAPACK_QRIteration()), |
| 60 | + TensorKit.PolarViaSVD(TensorKit.LAPACK_DivideAndConquer()), |
| 61 | + TensorKit.LAPACK_QRIteration(), |
| 62 | + TensorKit.LAPACK_DivideAndConquer()) |
| 63 | + L, Q = @constinferred rightorth(t; alg=alg) |
| 64 | + @test isisometry(Q; side=:right) |
| 65 | + @test L * Q ≈ t |
| 66 | + end |
| 67 | + @testset "rightnull with $alg" for alg in |
| 68 | + (TensorKit.LAPACK_HouseholderLQ(), |
| 69 | + TensorKit.LAPACK_QRIteration(), |
| 70 | + TensorKit.LAPACK_DivideAndConquer()) |
| 71 | + M = @constinferred rightnull(t; alg=alg) |
| 72 | + @test isisometry(M; side=:right) |
| 73 | + @test norm(t * M') < 100 * eps(norm(t)) |
| 74 | + end |
| 75 | + @testset "tsvd with $alg" for alg in (TensorKit.LAPACK_QRIteration(), |
| 76 | + TensorKit.LAPACK_DivideAndConquer()) |
| 77 | + U, S, V = @constinferred tsvd(t; alg=alg) |
| 78 | + @test isisometry(U) |
| 79 | + @test isisometry(V; side=:right) |
| 80 | + @test U * S * V ≈ t |
| 81 | + |
| 82 | + s = LinearAlgebra.svdvals(t) |
| 83 | + s′ = LinearAlgebra.diag(S) |
| 84 | + for (c, b) in s |
| 85 | + @test b ≈ s′[c] |
| 86 | + end |
| 87 | + s = LinearAlgebra.svdvals(t') |
| 88 | + s′ = LinearAlgebra.diag(S') |
| 89 | + for (c, b) in s |
| 90 | + @test b ≈ s′[c] |
| 91 | + end |
| 92 | + end |
| 93 | + @testset "cond and rank" begin |
| 94 | + d1 = dim(codomain(t)) |
| 95 | + d2 = dim(domain(t)) |
| 96 | + @test rank(t) == min(d1, d2) |
| 97 | + M = leftnull(t) |
| 98 | + @test rank(M) == max(d1, d2) - min(d1, d2) |
| 99 | + t3 = unitary(T, V1 ⊗ V2, V1 ⊗ V2) |
| 100 | + @test cond(t3) ≈ one(real(T)) |
| 101 | + @test rank(t3) == dim(V1 ⊗ V2) |
| 102 | + t4 = randn(T, V1 ⊗ V2, V1 ⊗ V2) |
| 103 | + t4 = (t4 + t4') / 2 |
| 104 | + vals = LinearAlgebra.eigvals(t4) |
| 105 | + λmax = maximum(s -> maximum(abs, s), values(vals)) |
| 106 | + λmin = minimum(s -> minimum(abs, s), values(vals)) |
| 107 | + @test cond(t4) ≈ λmax / λmin |
| 108 | + vals = LinearAlgebra.eigvals(t4') |
| 109 | + λmax = maximum(s -> maximum(abs, s), values(vals)) |
| 110 | + λmin = minimum(s -> minimum(abs, s), values(vals)) |
| 111 | + @test cond(t4') ≈ λmax / λmin |
| 112 | + end |
| 113 | + end |
| 114 | + @testset "empty tensor" begin |
| 115 | + t = randn(T, V1 ⊗ V2, zero(V1)) |
| 116 | + @testset "leftorth with $alg" for alg in |
| 117 | + (TensorKit.LAPACK_HouseholderQR(), |
| 118 | + TensorKit.LAPACK_HouseholderQR(positive=true), |
| 119 | + #TensorKit.QL(), TensorKit.QLpos(), |
| 120 | + TensorKit.PolarViaSVD(TensorKit.LAPACK_QRIteration()), |
| 121 | + TensorKit.PolarViaSVD(TensorKit.LAPACK_DivideAndConquer()), |
| 122 | + TensorKit.LAPACK_QRIteration(), |
| 123 | + TensorKit.LAPACK_DivideAndConquer()) |
| 124 | + Q, R = @constinferred leftorth(t; alg=alg) |
| 125 | + @test Q == t |
| 126 | + @test dim(Q) == dim(R) == 0 |
| 127 | + end |
| 128 | + @testset "leftnull with $alg" for alg in |
| 129 | + (TensorKit.LAPACK_HouseholderQR(), |
| 130 | + TensorKit.LAPACK_QRIteration(), |
| 131 | + TensorKit.LAPACK_DivideAndConquer()) |
| 132 | + N = @constinferred leftnull(t; alg=alg) |
| 133 | + @test isunitary(N) |
| 134 | + end |
| 135 | + @testset "rightorth with $alg" for alg in |
| 136 | + (#TensorKit.RQ(), TensorKit.RQpos(), |
| 137 | + TensorKit.LAPACK_HouseholderLQ(), |
| 138 | + TensorKit.LAPACK_HouseholderLQ(positive=true), |
| 139 | + TensorKit.PolarViaSVD(TensorKit.LAPACK_QRIteration()), |
| 140 | + TensorKit.PolarViaSVD(TensorKit.LAPACK_DivideAndConquer()), |
| 141 | + TensorKit.LAPACK_QRIteration(), |
| 142 | + TensorKit.LAPACK_DivideAndConquer()) |
| 143 | + L, Q = @constinferred rightorth(copy(t'); alg=alg) |
| 144 | + @test Q == t' |
| 145 | + @test dim(Q) == dim(L) == 0 |
| 146 | + end |
| 147 | + @testset "rightnull with $alg" for alg in |
| 148 | + (TensorKit.LAPACK_HouseholderLQ(), |
| 149 | + TensorKit.LAPACK_QRIteration(), |
| 150 | + TensorKit.LAPACK_DivideAndConquer()) |
| 151 | + M = @constinferred rightnull(copy(t'); alg=alg) |
| 152 | + @test isunitary(M) |
| 153 | + end |
| 154 | + @testset "tsvd with $alg" for alg in (TensorKit.LAPACK_QRIteration(), |
| 155 | + TensorKit.LAPACK_DivideAndConquer()) |
| 156 | + U, S, V = @constinferred tsvd(t; alg=alg) |
| 157 | + @test U == t |
| 158 | + @test dim(U) == dim(S) == dim(V) |
| 159 | + end |
| 160 | + @testset "cond and rank" begin |
| 161 | + @test rank(t) == 0 |
| 162 | + W2 = zero(V1) * zero(V2) |
| 163 | + t2 = rand(W2, W2) |
| 164 | + @test rank(t2) == 0 |
| 165 | + @test cond(t2) == 0.0 |
| 166 | + end |
| 167 | + end |
| 168 | + @testset "eig and isposdef" begin |
| 169 | + t = rand(T, V1, V1) |
| 170 | + D, V = eigen(t) |
| 171 | + @test t * V ≈ V * D |
| 172 | + |
| 173 | + d = LinearAlgebra.eigvals(t; sortby=nothing) |
| 174 | + d′ = LinearAlgebra.diag(D) |
| 175 | + for (c, b) in d |
| 176 | + @test b ≈ d′[c] |
| 177 | + end |
| 178 | + |
| 179 | + # Somehow moving these test before the previous one gives rise to errors |
| 180 | + # with T=Float32 on x86 platforms. Is this an OpenBLAS issue? |
| 181 | + VdV = V' * V |
| 182 | + VdV = (VdV + VdV') / 2 |
| 183 | + @test isposdef(VdV) |
| 184 | + |
| 185 | + @test !isposdef(t) # unlikely for non-hermitian map |
| 186 | + t2 = (t + t') |
| 187 | + D, V = eigen(t2) |
| 188 | + @test isisometry(V) |
| 189 | + D̃, Ṽ = @constinferred eigh(t2) |
| 190 | + @test D ≈ D̃ |
| 191 | + @test V ≈ Ṽ |
| 192 | + λ = minimum(minimum(real(LinearAlgebra.diag(b))) |
| 193 | + for (c, b) in blocks(D)) |
| 194 | + @test cond(Ṽ) ≈ one(real(T)) |
| 195 | + @test isposdef(t2) == isposdef(λ) |
| 196 | + @test isposdef(t2 - λ * one(t2) + 0.1 * one(t2)) |
| 197 | + @test !isposdef(t2 - λ * one(t2) - 0.1 * one(t2)) |
| 198 | + end |
| 199 | + end |
| 200 | + end |
| 201 | + end |
| 202 | +end |
0 commit comments