Skip to content

Commit dc95495

Browse files
committed
Move polar to testsuite
1 parent 6fff749 commit dc95495

File tree

3 files changed

+103
-74
lines changed

3 files changed

+103
-74
lines changed

test/polar.jl

Lines changed: 21 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -4,80 +4,27 @@ using TestExtras
44
using StableRNGs
55
using LinearAlgebra: LinearAlgebra, I, isposdef
66

7-
@testset "left_polar! for T = $T" for T in (Float32, Float64, ComplexF32, ComplexF64)
8-
rng = StableRNG(123)
9-
m = 54
10-
@testset "size ($m, $n)" for n in (37, m)
11-
k = min(m, n)
12-
if LinearAlgebra.LAPACK.version() < v"3.12.0"
13-
svdalgs = (LAPACK_DivideAndConquer(), LAPACK_QRIteration(), LAPACK_Bisection())
14-
else
15-
svdalgs = (LAPACK_DivideAndConquer(), LAPACK_QRIteration(), LAPACK_Bisection(), LAPACK_Jacobi())
16-
end
17-
algs = (PolarViaSVD.(svdalgs)..., PolarNewton())
18-
@testset "algorithm $alg" for alg in algs
19-
A = randn(rng, T, m, n)
20-
21-
W, P = left_polar(A; alg)
22-
@test W isa Matrix{T} && size(W) == (m, n)
23-
@test P isa Matrix{T} && size(P) == (n, n)
24-
@test W * P A
25-
@test isisometric(W)
26-
@test isposdef(P)
27-
28-
Ac = similar(A)
29-
W2, P2 = @constinferred left_polar!(copy!(Ac, A), (W, P), alg)
30-
@test W2 === W
31-
@test P2 === P
32-
@test W * P A
33-
@test isisometric(W)
34-
@test isposdef(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(project_hermitian!(P))
44-
end
7+
BLASFloats = (Float32, Float64, ComplexF32, ComplexF64)
8+
GenericFloats = (Float16, BigFloat, Complex{BigFloat})
9+
10+
@isdefined(TestSuite) || include("testsuite/TestSuite.jl")
11+
using .TestSuite
12+
13+
m = 54
14+
for T in BLASFloats, n in (37, m, 63)
15+
TestSuite.seed_rng!(123)
16+
TestSuite.test_polar(T, (m, n))
17+
if CUDA.functional()
18+
TestSuite.test_polar(CuMatrix{T}, (m, n); test_pivoted = false, test_blocksize = false)
19+
TestSuite.test_polar(Diagonal{T, CuVector{T}}, m; test_pivoted = false, test_blocksize = false)
4520
end
46-
end
47-
48-
@testset "right_polar! for T = $T" for T in (Float32, Float64, ComplexF32, ComplexF64)
49-
rng = StableRNG(123)
50-
n = 54
51-
@testset "size ($m, $n)" for m in (37, n)
52-
k = min(m, n)
53-
svdalgs = (LAPACK_DivideAndConquer(), LAPACK_QRIteration(), LAPACK_Bisection())
54-
algs = (PolarViaSVD.(svdalgs)..., PolarNewton())
55-
@testset "algorithm $alg" for alg in algs
56-
A = randn(rng, T, m, n)
57-
58-
P, Wᴴ = right_polar(A; alg)
59-
@test Wᴴ isa Matrix{T} && size(Wᴴ) == (m, n)
60-
@test P isa Matrix{T} && size(P) == (m, m)
61-
@test P * Wᴴ ≈ A
62-
@test isisometric(Wᴴ; side = :right)
63-
@test isposdef(P)
64-
65-
Ac = similar(A)
66-
P2, Wᴴ2 = @constinferred right_polar!(copy!(Ac, A), (P, Wᴴ), alg)
67-
@test P2 === P
68-
@test Wᴴ2 === Wᴴ
69-
@test P * Wᴴ ≈ A
70-
@test isisometric(Wᴴ; side = :right)
71-
@test isposdef(P)
72-
73-
noP = similar(P, (0, 0))
74-
P2, Wᴴ2 = @constinferred right_polar!(copy!(Ac, A), (noP, Wᴴ), alg)
75-
@test P2 === noP
76-
@test Wᴴ2 === Wᴴ
77-
@test isisometric(Wᴴ; side = :right)
78-
P = A * Wᴴ' # compute P explicitly to verify W correctness
79-
@test ishermitian(P; rtol = MatrixAlgebraKit.defaulttol(P))
80-
@test isposdef(project_hermitian!(P))
81-
end
21+
if AMDGPU.functional()
22+
TestSuite.test_polar(ROCMatrix{T}, (m, n); test_pivoted = false, test_blocksize = false)
23+
TestSuite.test_polar(Diagonal{T, ROCVector{T}}, m; test_pivoted = false, test_blocksize = false)
8224
end
8325
end
26+
for T in (BLASFloats..., GenericFloats...)
27+
AT = Diagonal{T, Vector{T}}
28+
TestSuite.test_polar(AT, m; test_pivoted = false, test_blocksize = false)
29+
end
30+

test/testsuite/TestSuite.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,6 @@ end
6565

6666
include("qr.jl")
6767
include("lq.jl")
68+
include("polar.jl")
6869

6970
end

test/testsuite/polar.jl

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
function test_polar(T::Type, sz; kwargs...)
2+
summary_str = testargs_summary(T, sz)
3+
return @testset "polar $summary_str" begin
4+
test_left_polar(T, sz; kwargs...)
5+
test_right_polar(T, sz; kwargs...)
6+
end
7+
end
8+
9+
function test_left_polar(
10+
T::Type, sz;
11+
atol::Real = 0, rtol::Real = precision(T),
12+
kwargs...
13+
)
14+
summary_str = testargs_summary(T, sz)
15+
return @testset "left_polar! $summary_str" begin
16+
algs = (PolarViaSVD(), PolarNewton())
17+
@testset "algorithm $alg" for alg in algs
18+
A = instantiate_matrix(T, sz)
19+
Ac = deepcopy(A)
20+
W, P = left_polar(A; alg)
21+
@test eltype(W) == eltype(A) && size(W) == (size(A, 1), size(A, 2))
22+
@test eltype(P) == eltype(A) && size(P) == (size(A, 2), size(A, 2))
23+
@test W * P A
24+
@test isisometric(W)
25+
@test isposdef(P)
26+
27+
W2, P2 = @constinferred left_polar!(Ac, (W, P), alg)
28+
@test W2 === W
29+
@test P2 === P
30+
@test W * P A
31+
@test isisometric(W)
32+
@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))
42+
end
43+
end
44+
end
45+
46+
function test_right_polar(
47+
T::Type, sz;
48+
atol::Real = 0, rtol::Real = precision(T),
49+
kwargs...
50+
)
51+
summary_str = testargs_summary(T, sz)
52+
return @testset "right_polar! $summary_str" begin
53+
algs = (PolarViaSVD(), PolarNewton())
54+
@testset "algorithm $alg" for alg in algs
55+
A = instantiate_matrix(T, sz)
56+
Ac = deepcopy(A)
57+
P, Wᴴ = right_polar(A; alg)
58+
@test eltype(Wᴴ) == eltype(A) && size(Wᴴ) == (size(A, 1), size(A, 2))
59+
@test eltype(P) == eltype(A) && size(P) == (size(A, 1), size(A, 1))
60+
@test P * Wᴴ ≈ A
61+
@test isisometric(Wᴴ; side = :right)
62+
@test isposdef(P)
63+
64+
P2, Wᴴ2 = @constinferred right_polar!(Ac, (P, Wᴴ), alg)
65+
@test P2 === P
66+
@test Wᴴ2 === Wᴴ
67+
@test P * Wᴴ ≈ A
68+
@test isisometric(Wᴴ; side = :right)
69+
@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))
79+
end
80+
end
81+
end

0 commit comments

Comments
 (0)