Skip to content

Commit 76176a6

Browse files
committed
Let cond of an empty matrix return zero
Fixes #778.
1 parent 9bc292d commit 76176a6

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/dense.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,6 +1779,7 @@ 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+
isempty(A) && return zero(real(eigtype(eltype(A))))
17821783
v = svdvals(A)
17831784
maxv = maximum(v)
17841785
return iszero(maxv) ? oftype(real(maxv), Inf) : maxv / minimum(v)

test/dense.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,19 @@ 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+
@test cond(zeros(Int, 0, 0), 1) === 0.0
61+
@test cond(zeros(Int, 0, 0), 2) === 0.0
62+
@test cond(zeros(Int, 0, 0), Inf) === 0.0
63+
@test cond(zeros(0, 0), 1) === 0.0
64+
@test cond(zeros(0, 0), 2) === 0.0
65+
@test cond(zeros(0, 0), Inf) === 0.0
66+
@test cond(zeros(ComplexF64, 0, 0), 1) === 0.0
67+
@test cond(zeros(ComplexF64, 0, 0), 2) === 0.0
68+
@test cond(zeros(ComplexF64, 0, 0), Inf) === 0.0
69+
@test cond(zeros(10, 0)) === 0.0
70+
@test cond(zeros(0, 10)) === 0.0
71+
end
5972
end
6073

6174
areal = randn(n,n)/2

0 commit comments

Comments
 (0)