Skip to content

Commit

Permalink
Let cond of an empty matrix return zero
Browse files Browse the repository at this point in the history
Fixes #778.
  • Loading branch information
martinholters committed Jan 14, 2025
1 parent 9bc292d commit 76176a6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1779,6 +1779,7 @@ Condition number of the matrix `M`, computed using the operator `p`-norm. Valid
"""
function cond(A::AbstractMatrix, p::Real=2)
if p == 2
isempty(A) && return zero(real(eigtype(eltype(A))))
v = svdvals(A)
maxv = maximum(v)
return iszero(maxv) ? oftype(real(maxv), Inf) : maxv / minimum(v)
Expand Down
13 changes: 13 additions & 0 deletions test/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@ Random.seed!(1234323)
@test cond(Mars, 2) 6.181867355918493
@test cond(Mars, Inf) 7.1
end
@testset "Empty matrices" begin
@test cond(zeros(Int, 0, 0), 1) === 0.0
@test cond(zeros(Int, 0, 0), 2) === 0.0
@test cond(zeros(Int, 0, 0), Inf) === 0.0
@test cond(zeros(0, 0), 1) === 0.0
@test cond(zeros(0, 0), 2) === 0.0
@test cond(zeros(0, 0), Inf) === 0.0
@test cond(zeros(ComplexF64, 0, 0), 1) === 0.0
@test cond(zeros(ComplexF64, 0, 0), 2) === 0.0
@test cond(zeros(ComplexF64, 0, 0), Inf) === 0.0
@test cond(zeros(10, 0)) === 0.0
@test cond(zeros(0, 10)) === 0.0
end
end

areal = randn(n,n)/2
Expand Down

0 comments on commit 76176a6

Please sign in to comment.