Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let cond of an empty matrix return zero #38372

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions stdlib/LinearAlgebra/src/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1456,6 +1456,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)
martinholters marked this conversation as resolved.
Show resolved Hide resolved
maxv = maximum(v)
return iszero(maxv) ? oftype(real(maxv), Inf) : maxv / minimum(v)
Expand Down
13 changes: 13 additions & 0 deletions stdlib/LinearAlgebra/test/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ Random.seed!(1234321)
M = [1.0 -2.0; -2.0 -1.5]
@test cond(M, 1) ≈ 2.227272727272727
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
martinholters marked this conversation as resolved.
Show resolved Hide resolved
@test cond(zeros(10, 0)) === 0.0
@test cond(zeros(0, 10)) === 0.0
end
end

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