Skip to content

Commit 8bb7f87

Browse files
committed
update tests
1 parent b6ad135 commit 8bb7f87

7 files changed

Lines changed: 62 additions & 68 deletions

File tree

test/algorithms.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using MatrixAlgebraKit
22
using Test
33
using TestExtras
4-
using MatrixAlgebraKit: LAPACK_SVDAlgorithm, NoTruncation, PolarViaSVD, TruncatedAlgorithm,
5-
TruncationKeepBelow, default_algorithm, select_algorithm
4+
using MatrixAlgebraKit: LAPACK_SVDAlgorithm, PolarViaSVD, TruncatedAlgorithm,
5+
default_algorithm, select_algorithm
66

77
@testset "default_algorithm" begin
88
A = randn(3, 3)
@@ -38,19 +38,19 @@ end
3838
A = randn(3, 3)
3939
for f in (svd_trunc!, svd_trunc)
4040
@test @constinferred(select_algorithm(f, A)) ===
41-
TruncatedAlgorithm(LAPACK_DivideAndConquer(), NoTruncation())
41+
TruncatedAlgorithm(LAPACK_DivideAndConquer(), notrunc())
4242
end
4343
for f in (eig_trunc!, eig_trunc)
4444
@test @constinferred(select_algorithm(f, A)) ===
45-
TruncatedAlgorithm(LAPACK_Expert(), NoTruncation())
45+
TruncatedAlgorithm(LAPACK_Expert(), notrunc())
4646
end
4747
for f in (eigh_trunc!, eigh_trunc)
4848
@test @constinferred(select_algorithm(f, A)) ===
4949
TruncatedAlgorithm(LAPACK_MultipleRelativelyRobustRepresentations(),
50-
NoTruncation())
50+
notrunc())
5151
end
5252

53-
alg = TruncatedAlgorithm(LAPACK_Simple(), TruncationKeepBelow(0.1, 0.0))
53+
alg = TruncatedAlgorithm(LAPACK_Simple(), trunctol(; atol=0.1, rev=false))
5454
for f in (eig_trunc!, eigh_trunc!, svd_trunc!)
5555
@test @constinferred(select_algorithm(eig_trunc!, A, alg)) === alg
5656
@test_throws ArgumentError select_algorithm(eig_trunc!, A, alg; trunc=(; maxrank=2))

test/chainrules.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ end
284284
output_tangent=(ΔU[:, 1:r], ΔS[1:r, 1:r], ΔVᴴ[1:r, :]),
285285
atol=atol, rtol=rtol)
286286
end
287-
truncalg = TruncatedAlgorithm(alg, trunctol(S[1, 1] / 2))
287+
truncalg = TruncatedAlgorithm(alg, trunctol(; atol=S[1, 1] / 2))
288288
r = findlast(>=(S[1, 1] / 2), diagview(S))
289289
test_rrule(copy_svd_trunc, A, truncalg NoTangent();
290290
output_tangent=(ΔU[:, 1:r], ΔS[1:r, 1:r], ΔVᴴ[1:r, :]),
@@ -302,7 +302,7 @@ end
302302
atol=atol, rtol=rtol, rrule_f=rrule_via_ad, check_inferred=false)
303303
end
304304
r = findlast(>=(S[1, 1] / 2), diagview(S))
305-
test_rrule(config, svd_trunc, A; fkwargs=(; trunc=trunctol(S[1, 1] / 2)),
305+
test_rrule(config, svd_trunc, A; fkwargs=(; trunc=trunctol(; atol=S[1, 1] / 2)),
306306
output_tangent=(ΔU[:, 1:r], ΔS[1:r, 1:r], ΔVᴴ[1:r, :]),
307307
atol=atol, rtol=rtol, rrule_f=rrule_via_ad, check_inferred=false)
308308
end

test/eig.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ end
4848
@test A * V1 V1 * D1
4949

5050
s = 1 + sqrt(eps(real(T)))
51-
trunc = trunctol(s * abs(D₀[r + 1]))
51+
trunc = trunctol(; atol=s * abs(D₀[r + 1]))
5252
D2, V2 = @constinferred eig_trunc(A; alg, trunc)
5353
@test length(diagview(D2)) == r
5454
@test A * V2 V2 * D2

test/eigh.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ end
5252
@test A * V1 V1 * D1
5353
@test LinearAlgebra.opnorm(A - V1 * D1 * V1') D₀[r + 1]
5454

55-
trunc = trunctol(s * D₀[r + 1])
55+
trunc = trunctol(; atol=s * D₀[r + 1])
5656
D2, V2 = @constinferred eigh_trunc(A; alg, trunc)
5757
@test length(diagview(D2)) == r
5858
@test isisometry(V2)

test/orthnull.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ using Test
33
using TestExtras
44
using StableRNGs
55
using LinearAlgebra: LinearAlgebra, I, mul!
6-
using MatrixAlgebraKit: TruncationKeepAbove, TruncationKeepBelow
76
using MatrixAlgebraKit: LAPACK_SVDAlgorithm, check_input, copy_input, default_svd_algorithm,
87
initialize_output, AbstractAlgorithm
98

@@ -33,10 +32,12 @@ end
3332
function MatrixAlgebraKit.initialize_output(::typeof(right_orth!), A::LinearMap)
3433
return LinearMap.(initialize_output(right_orth!, parent(A)))
3534
end
36-
function MatrixAlgebraKit.check_input(::typeof(left_orth!), A::LinearMap, VC, alg::AbstractAlgorithm)
35+
function MatrixAlgebraKit.check_input(::typeof(left_orth!), A::LinearMap, VC,
36+
alg::AbstractAlgorithm)
3737
return check_input(left_orth!, parent(A), parent.(VC), alg)
3838
end
39-
function MatrixAlgebraKit.check_input(::typeof(right_orth!), A::LinearMap, VC, alg::AbstractAlgorithm)
39+
function MatrixAlgebraKit.check_input(::typeof(right_orth!), A::LinearMap, VC,
40+
alg::AbstractAlgorithm)
4041
return check_input(right_orth!, parent(A), parent.(VC), alg)
4142
end
4243
function MatrixAlgebraKit.default_svd_algorithm(::Type{LinearMap{A}}; kwargs...) where {A}
@@ -124,7 +125,7 @@ end
124125

125126
rtol = eps(real(T))
126127
for (trunc_orth, trunc_null) in (((; rtol=rtol), (; rtol=rtol)),
127-
(TruncationKeepAbove(0, rtol), TruncationKeepBelow(0, rtol)))
128+
(trunctol(; rtol), trunctol(; rtol, rev=false)))
128129
V2, C2 = @constinferred left_orth!(copy!(Ac, A), (V, C); trunc=trunc_orth)
129130
N2 = @constinferred left_null!(copy!(Ac, A), N; trunc=trunc_null)
130131
@test V2 !== V

test/svd.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ using Test
33
using TestExtras
44
using StableRNGs
55
using LinearAlgebra: LinearAlgebra, Diagonal, I, isposdef, norm
6-
using MatrixAlgebraKit: TruncatedAlgorithm, TruncationKeepAbove, diagview, isisometry
6+
using MatrixAlgebraKit: TruncatedAlgorithm, diagview, isisometry
77

88
const BLASFloats = (Float32, Float64, ComplexF32, ComplexF64)
99

@@ -113,15 +113,15 @@ end
113113
@test LinearAlgebra.opnorm(A - U1 * S1 * V1ᴴ) S₀[r + 1]
114114

115115
s = 1 + sqrt(eps(real(T)))
116-
trunc2 = trunctol(s * S₀[r + 1])
116+
trunc = trunctol(; atol=s * S₀[r + 1])
117117

118-
U2, S2, V2ᴴ = @constinferred svd_trunc(A; alg, trunc=trunctol(s * S₀[r + 1]))
118+
U2, S2, V2ᴴ = @constinferred svd_trunc(A; alg, trunc)
119119
@test length(S2.diag) == r
120120
@test U1 U2
121121
@test S1 S2
122122
@test V1ᴴ V2ᴴ
123123

124-
trunc = truncerror(; atol = s * norm(@view(S₀[(r + 1):end])))
124+
trunc = truncerror(; atol=s * norm(@view(S₀[(r + 1):end])))
125125
U3, S3, V3ᴴ = @constinferred svd_trunc(A; alg, trunc)
126126
@test length(S3.diag) == r
127127
@test U1 U3
@@ -147,7 +147,7 @@ end
147147
A = U * S * Vᴴ
148148

149149
for trunc_fun in ((rtol, maxrank) -> (; rtol, maxrank),
150-
(rtol, maxrank) -> truncrank(maxrank) & TruncationKeepAbove(0, rtol))
150+
(rtol, maxrank) -> truncrank(maxrank) & trunctol(; rtol))
151151
U1, S1, V1ᴴ = svd_trunc(A; alg, trunc=trunc_fun(0.2, 1))
152152
@test length(S1.diag) == 1
153153
@test S1.diag S.diag[1:1] rtol = sqrt(eps(real(T)))
@@ -166,7 +166,7 @@ end
166166
S = Diagonal([0.9, 0.3, 0.1, 0.01])
167167
Vᴴ = qr_compact(randn(rng, T, m, m))[1]
168168
A = U * S * Vᴴ
169-
alg = TruncatedAlgorithm(LAPACK_DivideAndConquer(), TruncationKeepAbove(0.2, 0.0))
169+
alg = TruncatedAlgorithm(LAPACK_DivideAndConquer(), trunctol(; atol=0.2))
170170
U2, S2, V2ᴴ = @constinferred svd_trunc(A; alg)
171171
@test diagview(S2) diagview(S)[1:2] rtol = sqrt(eps(real(T)))
172172
@test_throws ArgumentError svd_trunc(A; alg, trunc=(; maxrank=2))

test/truncate.jl

Lines changed: 41 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,35 @@
11
using MatrixAlgebraKit
22
using Test
33
using TestExtras
4-
using MatrixAlgebraKit: NoTruncation, TruncationIntersection, TruncationKeepAbove,
5-
TruncationKeepBelow, TruncationStrategy, findtruncated,
4+
using MatrixAlgebraKit: NoTruncation, TruncationIntersection, TruncationByOrder,
5+
TruncationByValue, TruncationStrategy, findtruncated,
66
findtruncated_sorted
77

88
@testset "truncate" begin
99
trunc = @constinferred TruncationStrategy()
1010
@test trunc isa NoTruncation
1111

12-
trunc = @constinferred TruncationStrategy(; atol=1e-2, rtol=1e-3)
13-
@test trunc isa TruncationKeepAbove
14-
@test trunc == TruncationKeepAbove(1e-2, 1e-3)
15-
@test trunc.atol == 1e-2
16-
@test trunc.rtol == 1e-3
12+
atol = 1e-2
13+
rtol = 1e-3
14+
maxrank = 10
1715

18-
trunc = @constinferred TruncationStrategy(; maxrank=10)
19-
@test trunc isa TruncationKeepSorted
20-
@test trunc == truncrank(10)
21-
@test trunc.howmany == 10
16+
trunc = @constinferred TruncationStrategy(; atol, rtol)
17+
@test trunc isa TruncationByValue
18+
@test trunc == trunctol(; atol, rtol)
19+
@test trunc.atol == atol
20+
@test trunc.rtol == rtol
21+
@test trunc.rev
22+
23+
trunc = @constinferred TruncationStrategy(; maxrank)
24+
@test trunc isa TruncationByOrder
25+
@test trunc == truncrank(maxrank)
26+
@test trunc.howmany == maxrank
2227
@test trunc.by == abs
23-
@test trunc.rev == true
28+
@test trunc.rev
2429

25-
trunc = @constinferred TruncationStrategy(; atol=1e-2, rtol=1e-3, maxrank=10)
30+
trunc = @constinferred TruncationStrategy(; atol, rtol, maxrank)
2631
@test trunc isa TruncationIntersection
27-
@test trunc == truncrank(10) & TruncationKeepAbove(1e-2, 1e-3)
28-
@test trunc.components[1] == truncrank(10)
29-
@test trunc.components[2] == TruncationKeepAbove(1e-2, 1e-3)
32+
@test trunc == truncrank(maxrank) & trunctol(; atol, rtol)
3033

3134
values = [1, 0.9, 0.5, -0.3, 0.01]
3235
@test @constinferred(findtruncated(values, truncrank(2))) == 1:2
@@ -35,42 +38,32 @@ using MatrixAlgebraKit: NoTruncation, TruncationIntersection, TruncationKeepAbov
3538
@test @constinferred(findtruncated_sorted(values, truncrank(2))) === 1:2
3639

3740
values = [1, 0.9, 0.5, -0.3, 0.01]
38-
for strategy in (TruncationKeepAbove(; atol=0.4, rtol=0),
39-
TruncationKeepAbove(0.4, 0))
40-
@test @constinferred(findtruncated(values, strategy)) == 1:3
41-
@test @constinferred(findtruncated_sorted(values, strategy)) === 1:3
42-
end
43-
for strategy in (TruncationKeepBelow(; atol=0.4, rtol=0),
44-
TruncationKeepBelow(0.4, 0))
45-
@test @constinferred(findtruncated(values, strategy)) == 4:5
46-
@test @constinferred(findtruncated_sorted(values, strategy)) === 4:5
47-
end
41+
strategy = trunctol(; atol=0.4)
42+
@test @constinferred(findtruncated(values, strategy)) == 1:3
43+
@test @constinferred(findtruncated_sorted(values, strategy)) === 1:3
44+
strategy = trunctol(; atol=0.4, rev=false)
45+
@test @constinferred(findtruncated(values, strategy)) == 4:5
46+
@test @constinferred(findtruncated_sorted(values, strategy)) === 4:5
4847

4948
values = [0.01, 1, 0.9, -0.3, 0.5]
50-
for strategy in (TruncationKeepAbove(; atol=0.4, rtol=0),
51-
TruncationKeepAbove(; atol=0.4, rtol=0, by=abs),
52-
TruncationKeepAbove(0.4, 0),
53-
TruncationKeepAbove(; atol=0.2, rtol=0.0, by=identity))
49+
for strategy in (trunctol(; atol=0.4), trunctol(; atol=0.2, by=identity))
5450
@test @constinferred(findtruncated(values, strategy)) == [2, 3, 5]
5551
end
56-
for strategy in (TruncationKeepAbove(; atol=0.2, rtol=0),
57-
TruncationKeepAbove(; atol=0.2, rtol=0, by=abs),
58-
TruncationKeepAbove(0.2, 0))
59-
@test @constinferred(findtruncated(values, strategy)) == [2, 3, 4, 5]
60-
end
61-
for strategy in (TruncationKeepBelow(; atol=0.4, rtol=0),
62-
TruncationKeepBelow(; atol=0.4, rtol=0, by=abs),
63-
TruncationKeepBelow(0.4, 0),
64-
TruncationKeepBelow(; atol=0.2, rtol=0.0, by=identity))
52+
strategy = trunctol(; atol=0.2)
53+
@test @constinferred(findtruncated(values, strategy)) == [2, 3, 4, 5]
54+
55+
for strategy in
56+
(trunctol(; atol=0.4, rev=false), trunctol(; atol=0.2, by=identity, rev=false))
6557
@test @constinferred(findtruncated(values, strategy)) == [1, 4]
6658
end
67-
for strategy in (TruncationKeepBelow(; atol=0.2, rtol=0),
68-
TruncationKeepBelow(; atol=0.2, rtol=0, by=abs),
69-
TruncationKeepBelow(0.2, 0))
70-
@test @constinferred(findtruncated(values, strategy)) == [1]
71-
end
72-
for strategy in (truncerror(; atol=0.2, rtol=0),)
73-
@test issetequal(@constinferred(findtruncated(values, strategy)), 2:5)
74-
@test @constinferred(findtruncated_sorted(sort(values; by=abs, rev=true), strategy)) == 1:4
75-
end
59+
strategy = trunctol(; atol=0.2, rev=false)
60+
@test @constinferred(findtruncated(values, strategy)) == [1]
61+
62+
strategy = truncfilter(x -> 0.1 < x < 1)
63+
@test @constinferred(findtruncated(values, strategy)) == [3, 5]
64+
65+
strategy = truncerror(; atol=0.2, rtol=0)
66+
@test issetequal(@constinferred(findtruncated(values, strategy)), 2:5)
67+
vals_sorted = sort(values; by=abs, rev=true)
68+
@test @constinferred(findtruncated_sorted(vals_sorted, strategy)) == 1:4
7669
end

0 commit comments

Comments
 (0)