Skip to content

Commit f611197

Browse files
committed
Refactor out checks into functions
1 parent de9d8b1 commit f611197

1 file changed

Lines changed: 53 additions & 131 deletions

File tree

  • test/testsuite/decompositions

‎test/testsuite/decompositions/svd.jl‎

Lines changed: 53 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,31 @@ function test_svd_batched_algs(T::Type, sz, batch_size::Int, algs; test_compact:
4040
end
4141
end
4242

43+
function is_valid_svd_compact(A, U, S, Vᴴ)
44+
m, n = size(A)
45+
minmn = min(m, n)
46+
@test size(U) == (m, minmn)
47+
@test S isa Diagonal{real(eltype(A))} && size(S) == (minmn, minmn)
48+
@test size(Vᴴ) == (minmn, n)
49+
@test U * S * Vᴴ ≈ A
50+
@test isisometric(U)
51+
@test isisometric(Vᴴ; side = :right)
52+
@test isposdef(S)
53+
return
54+
end
55+
56+
function is_valid_svd_full(A, U, S, Vᴴ)
57+
m, n = size(A)
58+
@test size(U) == (m, m)
59+
@test eltype(S) == real(eltype(T)) && size(S) == (m, n)
60+
@test size(Vᴴ) == (n, n)
61+
@test U * S * Vᴴ ≈ A
62+
@test isunitary(U)
63+
@test isunitary(Vᴴ)
64+
@test all(isposdef, diagview(S))
65+
return
66+
end
67+
4368
function test_svd_compact(
4469
T::Type, sz;
4570
atol::Real = 0, rtol::Real = precision(eltype(T)),
@@ -52,20 +77,11 @@ function test_svd_compact(
5277
m, n = size(A)
5378
minmn = min(m, n)
5479
U, S, Vᴴ = @testinferred svd_compact(A)
55-
@test size(U) == (m, minmn)
56-
@test S isa Diagonal{real(eltype(T))} && size(S) == (minmn, minmn)
57-
@test size(Vᴴ) == (minmn, n)
58-
@test U * S * Vᴴ ≈ A
59-
@test isisometric(U)
60-
@test isisometric(Vᴴ; side = :right)
61-
@test isposdef(S)
80+
is_valid_svd_compact(A, U, S, Vᴴ)
6281

6382
Sc = similar(A, real(eltype(T)), min(m, n))
6483
U2, S2, V2ᴴ = @testinferred svd_compact!(Ac, (U, S, Vᴴ))
65-
@test U2 * S2 * V2ᴴ ≈ A
66-
@test isisometric(U2)
67-
@test isisometric(V2ᴴ; side = :right)
68-
@test isposdef(S2)
84+
is_valid_svd_compact(A, U2, S2, V2ᴴ)
6985

7086
if test_vals
7187
Sd = @testinferred svd_vals(A)
@@ -91,23 +107,14 @@ function test_svd_compact_batched(
91107
@test length(S) == batch_size
92108
@test length(Vᴴ) == batch_size
93109
for (a, u, s, vᴴ) in zip(As, U, S, Vᴴ)
94-
@test size(u) == (m, minmn)
95-
@test size(vᴴ) == (minmn, n)
96-
@test s isa Diagonal{real(eltype(T))} && size(s) == (minmn, minmn)
97-
@test u * s * vᴴ ≈ a
98-
@test isisometric(u)
99-
@test isisometric(vᴴ; side = :right)
100-
@test isposdef(s)
110+
is_valid_svd_compact(a, u, s, vᴴ)
101111
end
102112
U, S, Vᴴ = @testinferred batched_svd_compact(Ad)
103113
@test size(U) == (m, minmn, batch_size)
104114
@test S isa AbstractMatrix{real(eltype(T))} && size(S) == (minmn, batch_size)
105115
@test size(Vᴴ) == (minmn, n, batch_size)
106116
for (a, u, s, vᴴ) in zip(As, eachslice(U, dims = 3), eachslice(S, dims = 2), eachslice(Vᴴ, dims = 3))
107-
@test u * Diagonal(s) * vᴴ ≈ a
108-
@test isisometric(u)
109-
@test isisometric(vᴴ; side = :right)
110-
@test isposdef(Diagonal(s))
117+
is_valid_svd_compact(a, u, s, vᴴ)
111118
end
112119

113120
Sc = similar(diagview(S))
@@ -116,10 +123,7 @@ function test_svd_compact_batched(
116123
@test S2 === S
117124
@test V2ᴴ === Vᴴ
118125
for (a, u, s, vᴴ) in zip(As, eachslice(U2, dims = 3), eachslice(S2, dims = 2), eachslice(V2ᴴ, dims = 3))
119-
@test u * Diagonal(s) * vᴴ ≈ a
120-
@test isisometric(u)
121-
@test isisometric(vᴴ; side = :right)
122-
@test isposdef(Diagonal(s))
126+
is_valid_svd_compact(a, u, Diagonal(s), vᴴ)
123127
end
124128

125129
if test_vals
@@ -147,19 +151,10 @@ function test_svd_compact_algs(
147151
m, n = size(A)
148152
minmn = min(m, n)
149153
U, S, Vᴴ = @testinferred svd_compact(A; alg)
150-
@test size(U) == (m, minmn)
151-
@test S isa Diagonal{real(eltype(T))} && size(S) == (minmn, minmn)
152-
@test size(Vᴴ) == (minmn, n)
153-
@test U * S * Vᴴ ≈ A
154-
@test isisometric(U)
155-
@test isisometric(Vᴴ; side = :right)
156-
@test isposdef(S)
154+
is_valid_svd_compact(A, U, S, Vᴴ)
157155

158156
U2, S2, V2ᴴ = @testinferred svd_compact!(Ac, (U, S, Vᴴ); alg)
159-
@test U2 * S2 * V2ᴴ ≈ A
160-
@test isisometric(U2)
161-
@test isisometric(V2ᴴ; side = :right)
162-
@test isposdef(S2)
157+
is_valid_svd_compact(A, U2, S2, V2ᴴ)
163158

164159
if test_vals
165160
Sd = @testinferred svd_vals(A; alg)
@@ -185,34 +180,22 @@ function test_svd_compact_algs_batched(
185180
@test length(S) == batch_size
186181
@test length(Vᴴ) == batch_size
187182
for (a, u, s, vᴴ) in zip(As, U, S, Vᴴ)
188-
@test size(u) == (m, minmn)
189-
@test size(vᴴ) == (minmn, n)
190-
@test s isa Diagonal{real(eltype(T))} && size(s) == (minmn, minmn)
191-
@test u * s * vᴴ ≈ a
192-
@test isisometric(u)
193-
@test isisometric(vᴴ; side = :right)
194-
@test isposdef(s)
183+
is_valid_svd_compact(a, u, s, vᴴ)
195184
end
196185
U, S, Vᴴ = @testinferred batched_svd_compact(Ad; alg)
197186
@test size(U) == (m, minmn, batch_size)
198187
@test S isa AbstractMatrix{real(eltype(T))} && size(S) == (minmn, batch_size)
199188
@test size(Vᴴ) == (minmn, n, batch_size)
200189
for (a, u, s, vᴴ) in zip(As, eachslice(U, dims = 3), eachslice(S, dims = 2), eachslice(Vᴴ, dims = 3))
201-
@test u * Diagonal(s) * vᴴ ≈ a
202-
@test isisometric(u)
203-
@test isisometric(vᴴ; side = :right)
204-
@test isposdef(Diagonal(s))
190+
is_valid_svd_compact(a, u, Diagonal(s), vᴴ)
205191
end
206192

207193
U2, S2, V2ᴴ = @testinferred batched_svd_compact!(Ac, (U, S, Vᴴ); alg)
208194
@test U2 === U
209195
@test S2 === S
210196
@test V2ᴴ === Vᴴ
211197
for (a, u, s, vᴴ) in zip(As, eachslice(U2, dims = 3), eachslice(S2, dims = 2), eachslice(V2ᴴ, dims = 3))
212-
@test u * Diagonal(s) * vᴴ ≈ a
213-
@test isisometric(u)
214-
@test isisometric(vᴴ; side = :right)
215-
@test isposdef(Diagonal(s))
198+
is_valid_svd_compact(a, u, Diagonal(s), vᴴ)
216199
end
217200

218201
if test_vals
@@ -239,17 +222,13 @@ function test_svd_compact_algs_batched(
239222
@test S3 === Ss
240223
@test V3ᴴ === Vᴴs
241224
for (a, u, s, vᴴ) in zip(Ar, U3, S3, V3ᴴ)
242-
@test u * s * vᴴ ≈ a
243-
@test isisometric(u)
244-
@test isisometric(vᴴ; side = :right)
225+
is_valid_svd_compact(a, u, s, vᴴ)
245226
end
246227

247228
U4, S4, V4ᴴ = @testinferred batched_svd_compact(Ar; alg)
248229
@test S4 isa AbstractVector{<:Diagonal}
249230
for (a, u, s, vᴴ) in zip(Ar, U4, S4, V4ᴴ)
250-
@test u * s * vᴴ ≈ a
251-
@test isisometric(u)
252-
@test isisometric(vᴴ; side = :right)
231+
is_valid_svd_compact(a, u, s, vᴴ)
253232
end
254233

255234
if test_vals
@@ -280,19 +259,10 @@ function test_svd_full(
280259
minmn = min(m, n)
281260

282261
U, S, Vᴴ = @testinferred svd_full(A)
283-
@test size(U) == (m, m)
284-
@test eltype(S) == real(eltype(T)) && size(S) == (m, n)
285-
@test size(Vᴴ) == (n, n)
286-
@test U * S * Vᴴ ≈ A
287-
@test isunitary(U)
288-
@test isunitary(Vᴴ)
289-
@test all(isposdef, diagview(S))
262+
is_valid_svd_full(A, U, S, Vᴴ)
290263

291264
U2, S2, V2ᴴ = @testinferred svd_full!(Ac, (U, S, Vᴴ))
292-
@test U2 * S2 * V2ᴴ ≈ A
293-
@test isunitary(U2)
294-
@test isunitary(V2ᴴ)
295-
@test all(isposdef, diagview(S2))
265+
is_valid_svd_full(A, U2, S2, V2ᴴ)
296266

297267
Sc = similar(A, real(eltype(T)), min(m, n))
298268
Sc2 = @testinferred svd_vals!(copy!(Ac, A), Sc)
@@ -317,34 +287,22 @@ function test_svd_full_batched(
317287
@test length(S) == batch_size
318288
@test length(Vᴴ) == batch_size
319289
for (a, u, s, vᴴ) in zip(As, U, S, Vᴴ)
320-
@test size(u) == (m, m)
321-
@test size(vᴴ) == (n, n)
322-
@test s isa AbstractMatrix{real(eltype(T))} && size(s) == (m, n)
323-
@test u * s * vᴴ ≈ a
324-
@test isunitary(u)
325-
@test isunitary(vᴴ)
326-
@test all(isposdef, diagview(s))
290+
is_valid_svd_full(a, u, s, vᴴ)
327291
end
328292
U, S, Vᴴ = @testinferred batched_svd_full(Ad)
329293
@test size(U) == (m, m, batch_size)
330294
@test S isa AbstractArray{real(eltype(T)), 3} && size(S) == (m, n, batch_size)
331295
@test size(Vᴴ) == (n, n, batch_size)
332296
for (a, u, s, vᴴ) in zip(As, eachslice(U, dims = 3), eachslice(S, dims = 3), eachslice(Vᴴ, dims = 3))
333-
@test u * s * vᴴ ≈ a
334-
@test isunitary(u)
335-
@test isunitary(vᴴ)
336-
@test all(isposdef, diagview(s))
297+
is_valid_svd_full(a, u, s, vᴴ)
337298
end
338299

339300
U2, S2, V2ᴴ = @testinferred batched_svd_full!(Ac, (U, S, Vᴴ))
340301
@test U2 === U
341302
@test S2 === S
342303
@test V2ᴴ === Vᴴ
343304
for (a, u, s, vᴴ) in zip(As, eachslice(U2, dims = 3), eachslice(S2, dims = 3), eachslice(V2ᴴ, dims = 3))
344-
@test u * s * vᴴ ≈ a
345-
@test isunitary(u)
346-
@test isunitary(vᴴ)
347-
@test all(isposdef, diagview(s))
305+
is_valid_svd_full(a, u, s, vᴴ)
348306
end
349307

350308
Sc = similar(first(As), real(eltype(T)), min(m, n), batch_size)
@@ -368,22 +326,13 @@ function test_svd_full_algs(
368326
minmn = min(m, n)
369327

370328
U, S, Vᴴ = @testinferred svd_full(A; alg)
371-
@test size(U) == (m, m)
372-
@test eltype(S) == real(eltype(T)) && size(S) == (m, n)
373-
@test size(Vᴴ) == (n, n)
374-
@test U * S * Vᴴ ≈ A
375-
@test isunitary(U)
376-
@test isunitary(Vᴴ)
377-
@test all(isposdef, diagview(S))
329+
is_valid_svd_full(A, U, S, Vᴴ)
378330

379331
U2, S2, V2ᴴ = @testinferred svd_full!(Ac, (U, S, Vᴴ); alg)
380332
@test U2 === U
381333
@test S2 === S
382334
@test V2ᴴ === Vᴴ
383-
@test U2 * S2 * V2ᴴ ≈ A
384-
@test isunitary(U2)
385-
@test isunitary(V2ᴴ)
386-
@test all(isposdef, diagview(S2))
335+
is_valid_svd_full(A, U2, S2, V2ᴴ)
387336

388337
Sc = similar(A, real(eltype(T)), min(m, n))
389338
Sc2 = @testinferred svd_vals!(copy!(Ac, A), Sc; alg)
@@ -408,31 +357,19 @@ function test_svd_full_algs_batched(
408357
@test length(S) == batch_size
409358
@test length(Vᴴ) == batch_size
410359
for (a, u, s, vᴴ) in zip(As, U, S, Vᴴ)
411-
@test size(u) == (m, m)
412-
@test size(vᴴ) == (n, n)
413-
@test s isa AbstractMatrix{real(eltype(T))} && size(s) == (m, n)
414-
@test u * s * vᴴ ≈ a
415-
@test isunitary(u)
416-
@test isunitary(vᴴ)
417-
@test all(isposdef, diagview(s))
360+
is_valid_svd_full(a, u, s, vᴴ)
418361
end
419362
U, S, Vᴴ = @testinferred batched_svd_full(Ad; alg)
420363
@test size(U) == (m, m, batch_size)
421364
@test S isa AbstractArray{real(eltype(T)), 3} && size(S) == (m, n, batch_size)
422365
@test size(Vᴴ) == (n, n, batch_size)
423366
for (a, u, s, vᴴ) in zip(As, eachslice(U, dims = 3), eachslice(S, dims = 3), eachslice(Vᴴ, dims = 3))
424-
@test u * s * vᴴ ≈ a
425-
@test isunitary(u)
426-
@test isunitary(vᴴ)
427-
@test all(isposdef, diagview(s))
367+
is_valid_svd_full(a, u, s, vᴴ)
428368
end
429369

430370
U2, S2, V2ᴴ = @testinferred batched_svd_full!(Ac, (U, S, Vᴴ); alg)
431371
for (a, u, s, vᴴ) in zip(As, eachslice(U2, dims = 3), eachslice(S2, dims = 3), eachslice(V2ᴴ, dims = 3))
432-
@test u * s * vᴴ ≈ a
433-
@test isunitary(u)
434-
@test isunitary(vᴴ)
435-
@test all(isposdef, diagview(s))
372+
is_valid_svd_full(a, u, s, vᴴ)
436373
end
437374

438375
Sc = similar(first(As), real(eltype(T)), min(m, n), batch_size)
@@ -451,19 +388,12 @@ function test_svd_full_algs_batched(
451388
Vᴴs = [similar(a, size(a, 2), size(a, 2)) for a in Ar]
452389
U3, S3, V3ᴴ = @testinferred batched_svd_full!(deepcopy(Ar), (Us, Ss, Vᴴs); alg)
453390
for (a, u, s, vᴴ) in zip(Ar, U3, S3, V3ᴴ)
454-
@test size(u) == (size(a, 1), size(a, 1))
455-
@test size(s) == size(a)
456-
@test size(vᴴ) == (size(a, 2), size(a, 2))
457-
@test u * s * vᴴ ≈ a
458-
@test isunitary(u)
459-
@test isunitary(vᴴ)
391+
is_valid_svd_full(a, u, s, vᴴ)
460392
end
461393

462394
U4, S4, V4ᴴ = @testinferred batched_svd_full(Ar; alg)
463395
for (a, u, s, vᴴ) in zip(Ar, U4, S4, V4ᴴ)
464-
@test u * s * vᴴ ≈ a
465-
@test isunitary(u)
466-
@test isunitary(vᴴ)
396+
is_valid_svd_full(a, u, s, vᴴ)
467397
end
468398

469399
Sv = [similar(a, real(eltype(T)), minimum(size(a))) for a in Ar]
@@ -494,16 +424,12 @@ function test_svd_algs_batched_oversized(
494424

495425
U, S, Vᴴ = @testinferred batched_svd_compact(Ar; alg)
496426
for (a, u, s, vᴴ) in zip(Ar, U, S, Vᴴ)
497-
@test u * s * vᴴ ≈ a
498-
@test isisometric(u)
499-
@test isisometric(vᴴ; side = :right)
427+
is_valid_svd_compact(a, u, s, vᴴ)
500428
end
501429

502430
Uf, Sf, Vfᴴ = @testinferred batched_svd_full(Ar; alg)
503431
for (a, u, s, vᴴ) in zip(Ar, Uf, Sf, Vfᴴ)
504-
@test u * s * vᴴ ≈ a
505-
@test isunitary(u)
506-
@test isunitary(vᴴ)
432+
is_valid_svd_full(a, u, s, vᴴ)
507433
end
508434

509435
Sv = @testinferred batched_svd_vals(Ar; alg)
@@ -538,16 +464,12 @@ function test_svd_algs_batched_ragged_support(
538464

539465
U, S, Vᴴ = @testinferred batched_svd_compact(Ar; alg)
540466
for (a, u, s, vᴴ) in zip(Ar, U, S, Vᴴ)
541-
@test u * s * vᴴ ≈ a
542-
@test isisometric(u)
543-
@test isisometric(vᴴ; side = :right)
467+
is_valid_svd_compact(a, u, s, vᴴ)
544468
end
545469

546470
Uf, Sf, Vfᴴ = @testinferred batched_svd_full(Ar; alg)
547471
for (a, u, s, vᴴ) in zip(Ar, Uf, Sf, Vfᴴ)
548-
@test u * s * vᴴ ≈ a
549-
@test isunitary(u)
550-
@test isunitary(vᴴ)
472+
is_valid_svd_full(a, u, s, vᴴ)
551473
end
552474

553475
Sv = @testinferred batched_svd_vals(Ar; alg)

0 commit comments

Comments
 (0)