Skip to content

Commit 1a0135a

Browse files
Let cond of an empty square matrix return zero (#1169)
Co-authored-by: Steven G. Johnson <stevenj@alum.mit.edu>
1 parent 57e9a0d commit 1a0135a

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/dense.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,6 +1779,10 @@ Condition number of the matrix `M`, computed using the operator `p`-norm. Valid
17791779
"""
17801780
function cond(A::AbstractMatrix, p::Real=2)
17811781
if p == 2
1782+
if isempty(A)
1783+
checksquare(A)
1784+
return zero(real(eigtype(eltype(A))))
1785+
end
17821786
v = svdvals(A)
17831787
maxv = maximum(v)
17841788
return iszero(maxv) ? oftype(real(maxv), Inf) : maxv / minimum(v)

test/dense.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@ Random.seed!(1234323)
5656
@test cond(Mars, 2) 6.181867355918493
5757
@test cond(Mars, Inf) 7.1
5858
end
59+
@testset "Empty matrices" begin
60+
for p in (1,2,Inf)
61+
# zero for square (i.e. 0×0) matrices
62+
@test cond(zeros(Int, 0, 0), p) === 0.0
63+
@test cond(zeros(0, 0), p) === 0.0
64+
@test cond(zeros(ComplexF64, 0, 0), p) === 0.0
65+
# error for non-square matrices
66+
for size in ((10,0), (0,10))
67+
@test_throws DimensionMismatch cond(zeros(size...), p)
68+
end
69+
end
70+
end
5971
end
6072

6173
areal = randn(n,n)/2

0 commit comments

Comments
 (0)