Skip to content

Commit

Permalink
Let cond of an emtpy matrix return zero
Browse files Browse the repository at this point in the history
Fixes #38327.
  • Loading branch information
martinholters committed Nov 10, 2020
1 parent 21c2c51 commit 4717d69
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
1 change: 1 addition & 0 deletions stdlib/LinearAlgebra/src/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1457,6 +1457,7 @@ Condition number of the matrix `M`, computed using the operator `p`-norm. Valid
function cond(A::AbstractMatrix, p::Real=2)
if p == 2
v = svdvals(A)
isempty(v) && return zero(eltype(v))
maxv = maximum(v)
return iszero(maxv) ? oftype(real(maxv), Inf) : maxv / minimum(v)
elseif p == 1 || p == Inf
Expand Down
5 changes: 5 additions & 0 deletions stdlib/LinearAlgebra/test/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ Random.seed!(1234321)
M = [1.0 -2.0; -2.0 -1.5]
@test cond(M, 1) 2.227272727272727
end
@testset "Empty matrices" for p in (1, 2, Inf)
@test cond(zeros(Int, 0, 0), p) === 0.0
@test cond(zeros(0, 0), p) === 0.0
@test cond(zeros(ComplexF64, 0, 0), p) === 0.0
end
end

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

0 comments on commit 4717d69

Please sign in to comment.