Skip to content

Commit 162191c

Browse files
authored
Avoid testing condition number of random matrices against hard coded (#42361)
numbers. Instead, use definitions of the condition number of exercise the method for all BlasFloat element types and test a fixed matrix against fixed values to ensure the regressions in opnorm and svdvals would be detected.
1 parent b9fba13 commit 162191c

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

stdlib/LinearAlgebra/test/dense.jl

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,39 @@ n2 = 2*n1
1818
Random.seed!(1234323)
1919

2020
@testset "Matrix condition number" begin
21-
ainit = rand(n,n)
21+
ainit = rand(n, n)
2222
@testset "for $elty" for elty in (Float32, Float64, ComplexF32, ComplexF64)
2323
ainit = convert(Matrix{elty}, ainit)
2424
for a in (copy(ainit), view(ainit, 1:n, 1:n))
25-
@test cond(a,1) 198.3324294531168 atol=0.5
26-
@test cond(a,2) 85.93920079319506 atol=0.5
27-
@test cond(a,Inf) 149.7523084803039 atol=0.4
28-
@test cond(a[:,1:5]) 8.319279144493297 atol=0.01
25+
ainv = inv(a)
26+
@test cond(a, 1) == opnorm(a, 1) *opnorm(ainv, 1)
27+
@test cond(a, Inf) == opnorm(a, Inf)*opnorm(ainv, Inf)
28+
@test cond(a[:, 1:5]) == (\)(extrema(svdvals(a[:, 1:5]))...)
2929
@test_throws ArgumentError cond(a,3)
3030
end
3131
end
3232
@testset "Singular matrices" for p in (1, 2, Inf)
3333
@test cond(zeros(Int, 2, 2), p) == Inf
34-
@test cond(zeros(2, 2), p) == Inf
35-
@test cond([0 0; 1 1], p) == Inf
36-
@test cond([0. 0.; 1. 1.], p) == Inf
34+
@test cond(zeros(2, 2), p) == Inf
35+
@test cond([0 0; 1 1], p) == Inf
36+
@test cond([0. 0.; 1. 1.], p) == Inf
3737
end
3838
@testset "Issue #33547, condition number of 2x2 matrix" begin
39-
M = [1.0 -2.0; -2.0 -1.5]
39+
M = [1.0 -2.0
40+
-2.0 -1.5]
4041
@test cond(M, 1) 2.227272727272727
4142
end
43+
@testset "Condition numbers of a non-random matrix" begin
44+
# To ensure that we detect any regressions in the underlying functions
45+
Mars= [11 24 7 20 3
46+
4 12 25 8 16
47+
17 5 13 21 9
48+
10 18 1 14 22
49+
23 6 19 2 15]
50+
@test cond(Mars, 1) 7.1
51+
@test cond(Mars, 2) 6.181867355918493
52+
@test cond(Mars, Inf) 7.1
53+
end
4254
end
4355

4456
areal = randn(n,n)/2

0 commit comments

Comments
 (0)