@@ -2,7 +2,9 @@ using MatrixAlgebraKit
22using Test
33using TestExtras
44using MatrixAlgebraKit: NoTruncation, TruncationIntersection, TruncationUnion,
5- TruncationByOrder, TruncationByValue, TruncationStrategy, findtruncated, findtruncated_svd
5+ TruncationByOrder, TruncationByValue, TruncationMultiplets, TruncationStrategy,
6+ findtruncated, findtruncated_svd
7+ using LinearAlgebra: Diagonal, norm
68
79@testset " truncate" begin
810 trunc = @constinferred TruncationStrategy ()
@@ -94,3 +96,78 @@ using MatrixAlgebraKit: NoTruncation, TruncationIntersection, TruncationUnion,
9496 @test union2 isa TruncationUnion
9597 @test length (union2. components) == 3
9698end
99+
100+ @testset " truncmultiplets" begin
101+ trunc = @constinferred truncmultiplets (truncrank (2 ))
102+ @test trunc isa TruncationMultiplets
103+ @test trunc. trunc == truncrank (2 )
104+ @test trunc. atol == 0 && trunc. rtol == 1.0e-10
105+ @test trunc. mode === :down
106+ @test trunc == truncmultiplets (truncrank (2 ))
107+ @test trunc != truncmultiplets (truncrank (2 ); mode = :up )
108+ @test_throws ArgumentError truncmultiplets (truncrank (2 ); mode = :sideways )
109+
110+ # a degenerate group of three values around 0.5, split by truncrank(2) and truncrank(3)
111+ ϵ = 1.0e-14
112+ vals_sorted = [1.0 , 0.5 + ϵ, 0.5 , - 0.5 + ϵ, 0.1 ]
113+ vals_unsorted = vals_sorted[[3 , 5 , 1 , 4 , 2 ]]
114+ for howmany in (2 , 3 )
115+ down = truncmultiplets (truncrank (howmany))
116+ up = truncmultiplets (truncrank (howmany); mode = :up )
117+ @test @constinferred (findtruncated_svd (vals_sorted, down)) == [1 ]
118+ @test @constinferred (findtruncated_svd (vals_sorted, up)) == 1 : 4
119+ @test sort (vals_unsorted[@constinferred (findtruncated (vals_unsorted, down))]) == [1.0 ]
120+ @test sort (vals_unsorted[@constinferred (findtruncated (vals_unsorted, up))]) ==
121+ sort (vals_sorted[1 : 4 ])
122+ end
123+
124+ # nothing changes if the cut does not split a degenerate cluster
125+ for howmany in (0 , 1 , 4 , 5 , 6 )
126+ for mode in (:down , :up )
127+ strategy = truncmultiplets (truncrank (howmany); mode)
128+ @test findtruncated_svd (vals_sorted, strategy) ==
129+ findtruncated_svd (vals_sorted, truncrank (howmany))
130+ @test sort (findtruncated (vals_unsorted, strategy)) ==
131+ sort (findtruncated (vals_unsorted, truncrank (howmany)))
132+ end
133+ end
134+ @test findtruncated_svd (vals_sorted, truncmultiplets (notrunc ())) == 1 : 5
135+ @test isempty (findtruncated_svd (Float64[], truncmultiplets (truncrank (2 ))))
136+
137+ # without tolerance, only exactly degenerate values are grouped
138+ @test findtruncated_svd (vals_sorted, truncmultiplets (truncrank (2 ); rtol = 0 )) == 1 : 2
139+ @test findtruncated_svd ([1.0 , 0.5 , 0.5 , 0.1 ], truncmultiplets (truncrank (2 ); rtol = 0 )) == [1 ]
140+
141+ vals_chain = [1.0 , 0.9 , 0.8 , 0.7 , 0.1 ]
142+ @test findtruncated_svd (vals_chain, truncmultiplets (truncrank (2 ); atol = 0.15 )) == Int[]
143+ @test findtruncated_svd (vals_chain, truncmultiplets (truncrank (2 ); atol = 0.15 , mode = :up )) == 1 : 4
144+ @test findtruncated_svd (vals_chain, truncmultiplets (truncrank (2 ); rtol = 0.12 )) == Int[]
145+ @test findtruncated_svd (vals_chain, truncmultiplets (truncrank (2 ); rtol = 0.12 , mode = :up )) == 1 : 3
146+
147+ strategy = truncmultiplets (truncrank (3 ) & trunctol (; atol = 0.2 ))
148+ @test findtruncated_svd (vals_sorted, strategy) == [1 ]
149+ strategy = truncmultiplets (trunctol (; atol = 0.5 ) | truncrank (1 ); mode = :up )
150+ @test findtruncated_svd (vals_sorted, strategy) == 1 : 4
151+ strategy = truncmultiplets (truncfilter (x -> x < 0.5 + ϵ / 2 ); mode = :up )
152+ @test sort (findtruncated (vals_unsorted, strategy)) == sort (findtruncated (vals_unsorted, truncfilter (x -> x < 0.9 )))
153+ @test truncmultiplets (truncrank (2 )) & trunctol (; atol = 0.2 ) isa TruncationIntersection
154+
155+ # exactly degenerate singular values
156+ for T in (Float64, ComplexF64)
157+ n = 6
158+ Q₁ = qr_compact (randn (T, n, n))[1 ]
159+ Q₂ = qr_compact (randn (T, n, n))[1 ]
160+ Sref = [3.0 , 2.0 , 2.0 , 2.0 , 1.0 , 0.5 ]
161+ A = Q₁ * Diagonal (Sref) * Q₂'
162+ for howmany in 2 : 3
163+ U, S, Vᴴ, ϵ = svd_trunc (A; trunc = truncmultiplets (truncrank (howmany); rtol = 1.0e-8 ))
164+ @test diagview (S) ≈ Sref[1 : 1 ]
165+ @test ϵ ≈ norm (Sref[2 : end ])
166+ @test U * S * Vᴴ ≈ Sref[1 ] * Q₁[:, 1 ] * Q₂[:, 1 ]'
167+ U, S, Vᴴ, ϵ = svd_trunc (A; trunc = truncmultiplets (truncrank (howmany); rtol = 1.0e-8 , mode = :up ))
168+ @test diagview (S) ≈ Sref[1 : 4 ]
169+ @test ϵ ≈ norm (Sref[5 : end ])
170+ @test U * S * Vᴴ ≈ Q₁[:, 1 : 4 ] * Diagonal (Sref[1 : 4 ]) * Q₂[:, 1 : 4 ]'
171+ end
172+ end
173+ end
0 commit comments