Skip to content

Commit 280f38d

Browse files
lkdvoskshyatt
authored andcommitted
add tolerances
1 parent 9b47a36 commit 280f38d

2 files changed

Lines changed: 39 additions & 29 deletions

File tree

test/testsuite/TestSuite.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ testargs_summary(args...) = string(args)
3030
instantiate_matrix(::Type{T}, size) where {T <: Number} = randn(T, size)
3131
instantiate_matrix(::Type{AT}, size) where {AT <: Array} = randn(eltype(AT), size)
3232

33+
precision(::Type{T}) where {T <: Number} = sqrt(eps(real(T)))
34+
precision(::Type{T}) where {T} = precision(eltype(T))
35+
3336
function has_positive_diagonal(A)
3437
T = eltype(A)
3538
return if T <: Real
@@ -39,7 +42,8 @@ function has_positive_diagonal(A)
3942
all((zero(real(T))), imag(diagview(A)))
4043
end
4144
end
42-
isleftnull(N, A; kwargs...) = isapprox(norm(N' * A), 0; kwargs...)
45+
isleftnull(N, A; atol::Real = 0, rtol::Real = precision(eltype(A))) =
46+
isapprox(norm(A' * N), 0; atol = max(atol, norm(A) * rtol))
4347

4448
# TODO: actually make this a test
4549
macro testinferred(ex)

test/testsuite/qr.jl

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function test_qr(::Type{T}, sz; kwargs...) where {T}
1+
function test_qr(T::Type, sz; kwargs...)
22
summary_str = testargs_summary(T, sz)
33
return @testset "qr $summary_str" begin
44
test_qr_compact(T, sz; kwargs...)
@@ -8,9 +8,11 @@ function test_qr(::Type{T}, sz; kwargs...) where {T}
88
end
99

1010
function test_qr_compact(
11-
::Type{T}, sz;
12-
test_positive = true, test_pivoted = true, test_blocksize = true, kwargs...
13-
) where {T <: Number}
11+
T::Type, sz;
12+
test_positive = true, test_pivoted = true, test_blocksize = true,
13+
atol::Real = 0, rtol::Real = precision(T),
14+
kwargs...
15+
)
1416
summary_str = testargs_summary(T, sz)
1517
return @testset "qr_compact! $summary_str" begin
1618
A = instantiate_matrix(T, sz)
@@ -19,21 +21,21 @@ function test_qr_compact(
1921
# does the elementary functionality work
2022
Q, R = @testinferred qr_compact(A)
2123
@test Q * R A
22-
@test isisometric(Q)
24+
@test isisometric(Q; atol, rtol)
2325
@test istriu(R)
2426
@test A == Ac
2527

2628
# can I pass in outputs?
2729
Q2, R2 = @testinferred qr_compact!(deepcopy(A), (Q, R))
2830
@test Q2 * R2 A
29-
@test isisometric(Q2)
31+
@test isisometric(Q2; atol, rtol)
3032
@test istriu(R2)
3133

3234
# do we support `positive = true`?
3335
if test_positive
3436
Qpos, Rpos = @testinferred qr_compact(A; positive = true)
3537
@test Qpos * Rpos A
36-
@test isisometric(Qpos)
38+
@test isisometric(Qpos; atol, rtol)
3739
@test istriu(Rpos)
3840
@test has_positive_diagonal(Rpos)
3941
else
@@ -44,7 +46,7 @@ function test_qr_compact(
4446
if test_pivoted
4547
Qpiv, Rpiv = @testinferred qr_compact(A; pivoted = true)
4648
@test Qpiv * Rpiv A
47-
@test isisometric(Qpos)
49+
@test isisometric(Qpos; atol, rtol)
4850
else
4951
@test_throws ArgumentError qr_compact(A; pivoted = true)
5052
end
@@ -53,17 +55,19 @@ function test_qr_compact(
5355
if test_blocksize
5456
Qblocked, Rblocked = @testinferred qr_compact(A; blocksize = 2)
5557
@test Qblocked * Rblocked A
56-
@test isisometric(Qblocked)
58+
@test isisometric(Qblocked; atol, rtol)
5759
else
5860
@test_throws ArgumentError qr_compact(A; blocksize = 2)
5961
end
6062
end
6163
end
6264

6365
function test_qr_full(
64-
::Type{T}, sz;
65-
test_positive = true, test_pivoted = true, test_blocksize = true, kwargs...
66-
) where {T <: Number}
66+
T::Type, sz;
67+
test_positive = true, test_pivoted = true, test_blocksize = true,
68+
atol::Real = 0, rtol::Real = precision(T),
69+
kwargs...
70+
)
6771
summary_str = testargs_summary(T, sz)
6872
return @testset "qr_full! $summary_str" begin
6973
A = instantiate_matrix(T, sz)
@@ -72,21 +76,21 @@ function test_qr_full(
7276
# does the elementary functionality work
7377
Q, R = @testinferred qr_full(A)
7478
@test Q * R A
75-
@test isunitary(Q)
79+
@test isunitary(Q; atol, rtol)
7680
@test istriu(R)
7781
@test A == Ac
7882

7983
# can I pass in outputs?
8084
Q2, R2 = @testinferred qr_full!(deepcopy(A), (Q, R))
8185
@test Q2 * R2 A
82-
@test isunitary(Q2)
86+
@test isunitary(Q2; atol, rtol)
8387
@test istriu(R2)
8488

8589
# do we support `positive = true`?
8690
if test_positive
8791
Qpos, Rpos = @testinferred qr_full(A; positive = true)
8892
@test Qpos * Rpos A
89-
@test isunitary(Qpos)
93+
@test isunitary(Qpos; atol, rtol)
9094
@test istriu(Rpos)
9195
@test has_positive_diagonal(Rpos)
9296
else
@@ -97,7 +101,7 @@ function test_qr_full(
97101
if test_pivoted
98102
Qpiv, Rpiv = @testinferred qr_full(A; pivoted = true)
99103
@test Qpiv * Rpiv A
100-
@test isunitary(Qpos)
104+
@test isunitary(Qpos; atol, rtol)
101105
else
102106
@test_throws ArgumentError qr_full(A; pivoted = true)
103107
end
@@ -106,47 +110,49 @@ function test_qr_full(
106110
if test_blocksize
107111
Qblocked, Rblocked = @testinferred qr_full(A; blocksize = 2)
108112
@test Qblocked * Rblocked A
109-
@test isunitary(Qblocked)
113+
@test isunitary(Qblocked; atol, rtol)
110114
else
111115
@test_throws ArgumentError qr_full(A; blocksize = 2)
112116
end
113117
end
114118
end
115119

116120
function test_qr_null(
117-
::Type{T}, sz;
118-
test_pivoted = true, test_blocksize = true, kwargs...
119-
) where {T <: Number}
121+
T::Type, sz;
122+
test_pivoted = true, test_blocksize = true,
123+
atol::Real = 0, rtol::Real = precision(T),
124+
kwargs...
125+
)
120126
summary_str = testargs_summary(T, sz)
121127
return @testset "qr_null! $summary_str" begin
122128
A = instantiate_matrix(T, sz)
123129
Ac = deepcopy(A)
124130

125131
# does the elementary functionality work
126132
N = @testinferred qr_null(A)
127-
@test isleftnull(N, A)
128-
@test isisometric(N)
133+
@test isleftnull(N, A; atol, rtol)
134+
@test isisometric(N; atol, rtol)
129135
@test A == Ac
130136

131137
# can I pass in outputs?
132138
N2 = @testinferred qr_null!(deepcopy(A), N)
133-
@test isleftnull(N2, A)
134-
@test isisometric(N2)
139+
@test isleftnull(N2, A; atol, rtol)
140+
@test isisometric(N2; atol, rtol)
135141

136142
# do we support `pivoted = true`?
137143
if test_pivoted
138144
Npiv = @testinferred qr_null(A; pivoted = true)
139-
@test isleftnull(Npiv, A)
140-
@test isisometric(Npiv)
145+
@test isleftnull(Npiv, A; atol, rtol)
146+
@test isisometric(Npiv; atol, rtol)
141147
else
142148
@test_throws ArgumentError qr_null(A; pivoted = true)
143149
end
144150

145151
# do we support `blocksize = Int`?
146152
if test_blocksize
147153
Nblocked = @testinferred qr_null(A; blocksize = 2)
148-
@test isleftnull(Nblocked, A)
149-
@test isisometric(Nblocked)
154+
@test isleftnull(Nblocked, A; atol, rtol)
155+
@test isisometric(Nblocked; atol, rtol)
150156
else
151157
@test_throws ArgumentError qr_null(A; blocksize = 2)
152158
end

0 commit comments

Comments
 (0)