Skip to content

Commit 2a10c9c

Browse files
committed
More working tests
1 parent 02eac88 commit 2a10c9c

3 files changed

Lines changed: 72 additions & 16 deletions

File tree

test/amd/polar.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ using AMDGPU
3030
@test W * P A
3131
@test isisometric(W)
3232
@test isposdef(P)
33+
34+
noP = similar(P, (0, 0))
35+
W2, P2 = @constinferred left_polar!(copy!(Ac, A), (W, noP), alg)
36+
@test P2 === noP
37+
@test W2 === W
38+
@test isisometric(W)
39+
P = W' * A # compute P explicitly to verify W correctness
40+
@test ishermitian(P; rtol = MatrixAlgebraKit.defaulttol(P))
41+
@test isposdef(project_hermitian!(P))
3342
end
3443
end
3544
end
@@ -58,6 +67,15 @@ end
5867
@test P * Wᴴ A
5968
@test isisometric(Wᴴ; side=:right)
6069
@test isposdef(P)
70+
71+
noP = similar(P, (0, 0))
72+
P2, Wᴴ2 = @constinferred right_polar!(copy!(Ac, A), (noP, Wᴴ), alg)
73+
@test P2 === noP
74+
@test Wᴴ2 === Wᴴ
75+
@test isisometric(Wᴴ; side = :right)
76+
P = A * Wᴴ' # compute P explicitly to verify W correctness
77+
@test ishermitian(P; rtol = MatrixAlgebraKit.defaulttol(P))
78+
@test isposdef(project_hermitian!(P))
6179
end
6280
end
6381
end

test/cuda/orthnull.jl

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ end
5353
@testset "left_orth and left_null for T = $T" for T in (Float32, Float64, ComplexF32, ComplexF64)
5454
rng = StableRNG(123)
5555
m = 54
56-
for n in (37, m, 63)
56+
@testset for n in (37, m, 63)
5757
minmn = min(m, n)
5858
A = CuArray(randn(rng, T, m, n))
5959
V, C = @constinferred left_orth(A)
@@ -115,7 +115,7 @@ end
115115
@test isisometric(N2)
116116
hV2 = collect(V2)
117117
hN2 = collect(N2)
118-
@test hV2 * hV2' I
118+
@test hV2 * hV2' + hN2 * hN2' I
119119

120120
atol = eps(real(T))
121121
V2, C2 = @constinferred left_orth!(copy!(Ac, A), (V, C); trunc=(; atol=atol))
@@ -127,7 +127,9 @@ end
127127
@test isisometric(V2)
128128
@test LinearAlgebra.norm(A' * N2) 0 atol = MatrixAlgebraKit.defaulttol(T)
129129
@test isisometric(N2)
130-
@test V2 * V2' + N2 * N2' I
130+
hV2 = collect(V2)
131+
hN2 = collect(N2)
132+
@test hV2 * hV2' + hN2 * hN2' I
131133

132134
rtol = eps(real(T))
133135
for (trunc_orth, trunc_null) in (
@@ -143,10 +145,12 @@ end
143145
@test isisometric(V2)
144146
@test LinearAlgebra.norm(A' * N2) 0 atol = MatrixAlgebraKit.defaulttol(T)
145147
@test isisometric(N2)
146-
@test V2 * V2' + N2 * N2' I
148+
hV2 = collect(V2)
149+
hN2 = collect(N2)
150+
@test hV2 * hV2' + hN2 * hN2' I
147151
end
148152

149-
for kind in (:qr, :polar, :svd) # explicit kind kwarg
153+
@testset for kind in (:qr, :polar, :svd) # explicit kind kwarg
150154
m < n && kind == :polar && continue
151155
V2, C2 = @constinferred left_orth!(copy!(Ac, A), (V, C); kind=kind)
152156
@test V2 === V
@@ -176,7 +180,9 @@ end
176180
@test V2' * V2 I
177181
@test LinearAlgebra.norm(A' * N2) 0 atol = MatrixAlgebraKit.defaulttol(T)
178182
@test isisometric(N2)
179-
@test V2 * V2' + N2 * N2' I
183+
hV2 = collect(V2)
184+
hN2 = collect(N2)
185+
@test hV2 * hV2' + hN2 * hN2' I
180186

181187
V2, C2 = @constinferred left_orth!(copy!(Ac, A), (V, C); kind=kind,
182188
trunc=(; rtol=rtol))
@@ -189,7 +195,9 @@ end
189195
@test isisometric(V2)
190196
@test LinearAlgebra.norm(A' * N2) 0 atol = MatrixAlgebraKit.defaulttol(T)
191197
@test isisometric(N2)
192-
@test V2 * V2' + N2 * N2' I
198+
hV2 = collect(V2)
199+
hN2 = collect(N2)
200+
@test hV2 * hV2' + hN2 * hN2' I
193201
else
194202
@test_throws ArgumentError left_orth!(copy!(Ac, A), (V, C); kind=kind,
195203
trunc=(; atol=atol))
@@ -253,7 +261,9 @@ end
253261
@test isisometric(Vᴴ2; side=:right)
254262
@test LinearAlgebra.norm(A * adjoint(Nᴴ2)) 0 atol = MatrixAlgebraKit.defaulttol(T)
255263
@test isisometric(Nᴴ; side=:right)
256-
@test Vᴴ2' * Vᴴ2 + Nᴴ2' * Nᴴ2 I
264+
hVᴴ2 = collect(Vᴴ2)
265+
hNᴴ2 = collect(Nᴴ2)
266+
@test hVᴴ2' * hVᴴ2 + hNᴴ2' * hNᴴ2 I
257267

258268
C2, Vᴴ2 = @constinferred right_orth!(copy!(Ac, A), (C, Vᴴ); trunc=(; rtol=rtol))
259269
Nᴴ2 = @constinferred right_null!(copy!(Ac, A), Nᴴ; trunc=(; rtol=rtol))
@@ -264,7 +274,9 @@ end
264274
@test isisometric(Vᴴ2; side=:right)
265275
@test LinearAlgebra.norm(A * adjoint(Nᴴ2)) 0 atol = MatrixAlgebraKit.defaulttol(T)
266276
@test isisometric(Nᴴ2; side=:right)
267-
@test Vᴴ2' * Vᴴ2 + Nᴴ2' * Nᴴ2 I
277+
hVᴴ2 = collect(Vᴴ2)
278+
hNᴴ2 = collect(Nᴴ2)
279+
@test hVᴴ2' * hVᴴ2 + hNᴴ2' * hNᴴ2 I
268280

269281
@testset "kind = $kind" for kind in (:lq, :polar, :svd)
270282
n < m && kind == :polar && continue
@@ -295,7 +307,9 @@ end
295307
@test isisometric(Vᴴ2; side=:right)
296308
@test LinearAlgebra.norm(A * adjoint(Nᴴ2)) 0 atol = MatrixAlgebraKit.defaulttol(T)
297309
@test isisometric(Nᴴ2; side=:right)
298-
@test Vᴴ2' * Vᴴ2 + Nᴴ2' * Nᴴ2 I
310+
hVᴴ2 = collect(Vᴴ2)
311+
hNᴴ2 = collect(Nᴴ2)
312+
@test hVᴴ2' * hVᴴ2 + hNᴴ2' * hNᴴ2 I
299313

300314
C2, Vᴴ2 = @constinferred right_orth!(copy!(Ac, A), (C, Vᴴ); kind=kind,
301315
trunc=(; rtol=rtol))
@@ -308,7 +322,9 @@ end
308322
@test isisometric(Vᴴ2; side=:right)
309323
@test LinearAlgebra.norm(A * adjoint(Nᴴ2)) 0 atol = MatrixAlgebraKit.defaulttol(T)
310324
@test isisometric(Nᴴ2; side=:right)
311-
@test Vᴴ2' * Vᴴ2 + Nᴴ2' * Nᴴ2 I
325+
hVᴴ2 = collect(Vᴴ2)
326+
hNᴴ2 = collect(Nᴴ2)
327+
@test hVᴴ2' * hVᴴ2 + hNᴴ2' * hNᴴ2 diagm(ones(T, size(Vᴴ2, 2))) atol = m*n*MatrixAlgebraKit.defaulttol(T)
312328
else
313329
@test_throws ArgumentError right_orth!(copy!(Ac, A), (C, Vᴴ); kind=kind,
314330
trunc=(; atol=atol))

test/cuda/polar.jl

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ using MatrixAlgebraKit
22
using Test
33
using TestExtras
44
using StableRNGs
5-
using LinearAlgebra: LinearAlgebra, I, isposdef
5+
using LinearAlgebra: LinearAlgebra, I, isposdef, Hermitian
66
using MatrixAlgebraKit: PolarViaSVD
77
using CUDA
88

@@ -21,15 +21,26 @@ using CUDA
2121
@test P isa CuMatrix{T} && size(P) == (n, n)
2222
@test W * P A
2323
@test isisometric(W)
24-
@test isposdef(P)
24+
# work around extremely strict Julia criteria for Hermiticity
25+
@test ishermitian(P; rtol = MatrixAlgebraKit.defaulttol(P)) && isposdef(Hermitian(P))
2526

2627
Ac = similar(A)
2728
W2, P2 = @constinferred left_polar!(copy!(Ac, A), (W, P), alg)
2829
@test W2 === W
2930
@test P2 === P
3031
@test W * P A
3132
@test isisometric(W)
32-
@test isposdef(P)
33+
# work around extremely strict Julia criteria for Hermiticity
34+
@test ishermitian(P; rtol = MatrixAlgebraKit.defaulttol(P)) && isposdef(Hermitian(P))
35+
36+
noP = similar(P, (0, 0))
37+
W2, P2 = @constinferred left_polar!(copy!(Ac, A), (W, noP), alg)
38+
@test P2 === noP
39+
@test W2 === W
40+
@test isisometric(W)
41+
P = W' * A # compute P explicitly to verify W correctness
42+
@test ishermitian(P; rtol = MatrixAlgebraKit.defaulttol(P))
43+
@test isposdef(Hermitian(project_hermitian!(P)))
3344
end
3445
end
3546
end
@@ -49,15 +60,26 @@ end
4960
@test P isa CuMatrix{T} && size(P) == (m, m)
5061
@test P * Wᴴ A
5162
@test isisometric(Wᴴ; side=:right)
52-
@test isposdef(P)
63+
# work around extremely strict Julia criteria for Hermiticity
64+
@test ishermitian(P; rtol = MatrixAlgebraKit.defaulttol(P)) && isposdef(Hermitian(P))
5365

5466
Ac = similar(A)
5567
P2, Wᴴ2 = @constinferred right_polar!(copy!(Ac, A), (P, Wᴴ), alg)
5668
@test P2 === P
5769
@test Wᴴ2 === Wᴴ
5870
@test P * Wᴴ A
5971
@test isisometric(Wᴴ; side=:right)
60-
@test isposdef(P)
72+
# work around extremely strict Julia criteria for Hermiticity
73+
@test ishermitian(P; rtol = MatrixAlgebraKit.defaulttol(P)) && isposdef(Hermitian(P))
74+
75+
noP = similar(P, (0, 0))
76+
P2, Wᴴ2 = @constinferred right_polar!(copy!(Ac, A), (noP, Wᴴ), alg)
77+
@test P2 === noP
78+
@test Wᴴ2 === Wᴴ
79+
@test isisometric(Wᴴ; side = :right)
80+
P = A * Wᴴ' # compute P explicitly to verify W correctness
81+
@test ishermitian(P; rtol = MatrixAlgebraKit.defaulttol(P))
82+
@test isposdef(Hermitian(project_hermitian!(P)))
6183
end
6284
end
6385
end

0 commit comments

Comments
 (0)