|
| 1 | +using Test, TestExtras |
| 2 | +using TensorKit |
| 3 | +using LinearAlgebra: LinearAlgebra |
| 4 | +using MatrixAlgebraKit: diagview |
| 5 | + |
| 6 | + |
| 7 | +spacelist = if fast_tests |
| 8 | + (Vtr, Vℤ₃, VSU₂) |
| 9 | +elseif get(ENV, "CI", "false") == "true" |
| 10 | + println("Detected running on CI") |
| 11 | + if Sys.iswindows() |
| 12 | + (Vtr, Vℤ₃, VU₁, VfU₁, VCU₁, VSU₂, VIB_diag) |
| 13 | + elseif Sys.isapple() |
| 14 | + (Vtr, Vℤ₃, VfU₁, VfSU₂, VIB_M) |
| 15 | + else |
| 16 | + (Vtr, VU₁, VCU₁, VSU₂, VfSU₂, VIB_diag, VIB_M) |
| 17 | + end |
| 18 | +else |
| 19 | + (Vtr, Vℤ₃, VU₁, VfU₁, VCU₁, VSU₂, VfSU₂, VIB_diag, VIB_M) |
| 20 | +end |
| 21 | + |
| 22 | +eltypes = (Float32, ComplexF64) |
| 23 | + |
| 24 | +for V in spacelist |
| 25 | + I = sectortype(first(V)) |
| 26 | + Istr = TensorKit.type_repr(I) |
| 27 | + println("---------------------------------------") |
| 28 | + println("Factorizations with symmetry: $Istr") |
| 29 | + println("---------------------------------------") |
| 30 | + @timedtestset "Factorizations with symmetry: $Istr" verbose = true begin |
| 31 | + V1, V2, V3, V4, V5 = V |
| 32 | + W = V1 ⊗ V2 |
| 33 | + @assert !isempty(blocksectors(W)) |
| 34 | + @assert !isempty(intersect(blocksectors(V4), blocksectors(W))) |
| 35 | + |
| 36 | + @testset "Condition number and rank" begin |
| 37 | + for T in eltypes, |
| 38 | + t in ( |
| 39 | + rand(T, W, W), rand(T, W, W)', |
| 40 | + rand(T, W, V4), rand(T, V4, W), |
| 41 | + rand(T, W, V4)', rand(T, V4, W)', |
| 42 | + DiagonalTensorMap(rand(T, reduceddim(V1)), V1), |
| 43 | + ) |
| 44 | + |
| 45 | + d1, d2 = dim(codomain(t)), dim(domain(t)) |
| 46 | + r = rank(t) |
| 47 | + @test r == min(d1, d2) |
| 48 | + @test typeof(r) == typeof(d1) |
| 49 | + M = left_null(t) |
| 50 | + @test @constinferred(rank(M)) + r ≈ d1 |
| 51 | + Mᴴ = right_null(t) |
| 52 | + @test rank(Mᴴ) + r ≈ d2 |
| 53 | + end |
| 54 | + for T in eltypes |
| 55 | + u = unitary(T, V1 ⊗ V2, V1 ⊗ V2) |
| 56 | + @test @constinferred(cond(u)) ≈ one(real(T)) |
| 57 | + @test @constinferred(rank(u)) == dim(V1 ⊗ V2) |
| 58 | + |
| 59 | + t = rand(T, zerospace(V1), W) |
| 60 | + @test rank(t) == 0 |
| 61 | + t2 = rand(T, zerospace(V1) * zerospace(V2), zerospace(V1) * zerospace(V2)) |
| 62 | + @test rank(t2) == 0 |
| 63 | + @test cond(t2) == 0.0 |
| 64 | + end |
| 65 | + for T in eltypes, t in (rand(T, W, W), rand(T, W, W)') |
| 66 | + project_hermitian!(t) |
| 67 | + vals = @constinferred LinearAlgebra.eigvals(t) |
| 68 | + λmax = maximum(s -> maximum(abs, s), values(vals)) |
| 69 | + λmin = minimum(s -> minimum(abs, s), values(vals)) |
| 70 | + @test cond(t) ≈ λmax / λmin |
| 71 | + end |
| 72 | + end |
| 73 | + |
| 74 | + @testset "Hermitian projections" begin |
| 75 | + for T in eltypes, |
| 76 | + t in ( |
| 77 | + rand(T, V1, V1), rand(T, W, W), rand(T, W, W)', |
| 78 | + DiagonalTensorMap(rand(T, reduceddim(V1)), V1), |
| 79 | + ) |
| 80 | + normalize!(t) |
| 81 | + noisefactor = eps(real(T))^(3 / 4) |
| 82 | + |
| 83 | + th = (t + t') / 2 |
| 84 | + ta = (t - t') / 2 |
| 85 | + tc = copy(t) |
| 86 | + |
| 87 | + th′ = @constinferred project_hermitian(t) |
| 88 | + @test ishermitian(th′) |
| 89 | + @test th′ ≈ th |
| 90 | + @test t == tc |
| 91 | + th_approx = th + noisefactor * ta |
| 92 | + @test !ishermitian(th_approx) || (T <: Real && t isa DiagonalTensorMap) |
| 93 | + @test ishermitian(th_approx; atol = 10 * noisefactor) |
| 94 | + |
| 95 | + ta′ = project_antihermitian(t) |
| 96 | + @test isantihermitian(ta′) |
| 97 | + @test ta′ ≈ ta |
| 98 | + @test t == tc |
| 99 | + ta_approx = ta + noisefactor * th |
| 100 | + @test !isantihermitian(ta_approx) |
| 101 | + @test isantihermitian(ta_approx; atol = 10 * noisefactor) || (T <: Real && t isa DiagonalTensorMap) |
| 102 | + end |
| 103 | + end |
| 104 | + |
| 105 | + @testset "Isometric projections" begin |
| 106 | + for T in eltypes, |
| 107 | + t in ( |
| 108 | + randn(T, W, W), randn(T, W, W)', |
| 109 | + randn(T, W, V4), randn(T, V4, W)', |
| 110 | + ) |
| 111 | + t2 = project_isometric(t) |
| 112 | + @test isisometric(t2) |
| 113 | + t3 = project_isometric(t2) |
| 114 | + @test t3 ≈ t2 # stability of the projection |
| 115 | + @test t2 * (t2' * t) ≈ t |
| 116 | + |
| 117 | + tc = similar(t) |
| 118 | + t3 = @constinferred project_isometric!(copy!(tc, t), t2) |
| 119 | + @test t3 === t2 |
| 120 | + @test isisometric(t2) |
| 121 | + |
| 122 | + # test that t2 is closer to A then any other isometry |
| 123 | + for k in 1:10 |
| 124 | + δt = randn!(similar(t)) |
| 125 | + t3 = project_isometric(t + δt / 100) |
| 126 | + @test norm(t - t3) > norm(t - t2) |
| 127 | + end |
| 128 | + end |
| 129 | + end |
| 130 | + end |
| 131 | +end |
0 commit comments