Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions src/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1779,6 +1779,10 @@ Condition number of the matrix `M`, computed using the operator `p`-norm. Valid
"""
function cond(A::AbstractMatrix, p::Real=2)
if p == 2
if isempty(A)
checksquare(A)
return zero(real(eigtype(eltype(A))))
end
v = svdvals(A)
maxv = maximum(v)
return iszero(maxv) ? oftype(real(maxv), Inf) : maxv / minimum(v)
Expand Down
19 changes: 19 additions & 0 deletions test/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,25 @@ Random.seed!(1234323)
@test cond(Mars, 2) ≈ 6.181867355918493
@test cond(Mars, Inf) ≈ 7.1
end
@testset "Empty matrices" begin
# zero for square (i.e. 0×0) matrices
@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
# error for non-square matrices
@test_throws DimensionMismatch cond(zeros(10, 0), 1)
@test_throws DimensionMismatch cond(zeros(0, 10), 1)
@test_throws DimensionMismatch cond(zeros(10, 0), 2)
@test_throws DimensionMismatch cond(zeros(0, 10), 2)
@test_throws DimensionMismatch cond(zeros(10, 0), Inf)
@test_throws DimensionMismatch cond(zeros(0, 10), Inf)
end
end

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