115115 r = minmn - 2
116116
117117 U1, S1, V1ᴴ, ϵ1 = @constinferred svd_trunc (A; alg, trunc = truncrank (r))
118- @test length (S1. diag) == r
118+ @test length (diagview (S1)) == r
119+ @test diagview (S1) ≈ S₀[1 : r]
119120 @test LinearAlgebra. opnorm (A - U1 * S1 * V1ᴴ) ≈ S₀[r + 1 ]
120121 # Test truncation error
121122 @test ϵ1 ≈ norm (view (S₀, (r + 1 ): minmn)) atol = atol
@@ -124,19 +125,19 @@ end
124125 trunc = trunctol (; atol = s * S₀[r + 1 ])
125126
126127 U2, S2, V2ᴴ, ϵ2 = @constinferred svd_trunc (A; alg, trunc)
127- @test length (S2 . diag ) == r
128+ @test length (diagview (S2) ) == r
128129 @test U1 ≈ U2
129130 @test S1 ≈ S2
130131 @test V1ᴴ ≈ V2ᴴ
131- @test ϵ2 ≈ ϵ1
132+ @test ϵ2 ≈ norm ( view (S₀, (r + 1 ) : minmn)) atol = atol
132133
133134 trunc = truncerror (; atol = s * norm (@view (S₀[(r + 1 ): end ])))
134135 U3, S3, V3ᴴ, ϵ3 = @constinferred svd_trunc (A; alg, trunc)
135- @test length (S3 . diag ) == r
136+ @test length (diagview (S3) ) == r
136137 @test U1 ≈ U3
137138 @test S1 ≈ S3
138139 @test V1ᴴ ≈ V3ᴴ
139- @test ϵ3 ≈ ϵ1
140+ @test ϵ3 ≈ norm ( view (S₀, (r + 1 ) : minmn)) atol = atol
140141 end
141142 end
142143end
@@ -162,27 +163,28 @@ end
162163 (rtol, maxrank) -> truncrank (maxrank) & trunctol (; rtol),
163164 )
164165 U1, S1, V1ᴴ, ϵ1 = svd_trunc (A; alg, trunc = trunc_fun (0.2 , 1 ))
165- @test length (S1 . diag ) == 1
166- @test S1 . diag ≈ S . diag [1 : 1 ] rtol = sqrt ( eps ( real (T)))
166+ @test length (diagview (S1) ) == 1
167+ @test diagview (S1) ≈ diagview (S) [1 : 1 ]
167168
168169 U2, S2, V2ᴴ, ϵ2 = svd_trunc (A; alg, trunc = trunc_fun (0.2 , 3 ))
169- @test length (S2 . diag ) == 2
170- @test S2 . diag ≈ S . diag [1 : 2 ] rtol = sqrt ( eps ( real (T)))
170+ @test length (diagview (S2) ) == 2
171+ @test diagview (S2) ≈ diagview (S) [1 : 2 ]
171172 end
172173 end
173174end
174175
175176@testset " svd_trunc! specify truncation algorithm T = $T " for T in BLASFloats
176177 rng = StableRNG (123 )
178+ atol = sqrt (eps (real (T)))
177179 m = 4
178180 U = qr_compact (randn (rng, T, m, m))[1 ]
179181 S = Diagonal (real (T)[0.9 , 0.3 , 0.1 , 0.01 ])
180182 Vᴴ = qr_compact (randn (rng, T, m, m))[1 ]
181183 A = U * S * Vᴴ
182184 alg = TruncatedAlgorithm (LAPACK_DivideAndConquer (), trunctol (; atol = 0.2 ))
183185 U2, S2, V2ᴴ, ϵ2 = @constinferred svd_trunc (A; alg)
184- @test diagview (S2) ≈ diagview (S)[1 : 2 ] rtol = sqrt ( eps ( real (T)))
185- @test ϵ2 ≈ norm (diagview (S)[3 : 4 ])
186+ @test diagview (S2) ≈ diagview (S)[1 : 2 ]
187+ @test ϵ2 ≈ norm (diagview (S)[3 : 4 ]) atol = atol
186188 @test_throws ArgumentError svd_trunc (A; alg, trunc = (; maxrank = 2 ))
187189end
188190
0 commit comments