Skip to content
Merged
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
18 changes: 10 additions & 8 deletions src/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -990,14 +990,16 @@ small enough to store dense.
function cond(A::AbstractSparseMatrixCSC, p::Real=2)
# as for a dense matrix; the estimator of the inverse's norm needs at least one column
(p == 1 || p == Inf) && checksquare(A) == 0 && return zero(real(float(eltype(A))))
if p == 1
normAinv = opnormestinv(A)
normA = opnorm(A, 1)
return normA * normAinv
elseif p == Inf
normAinv = opnormestinv(copy(A'))
normA = opnorm(A, Inf)
return normA * normAinv
if p == 1 || p == Inf
# a singular matrix has an infinite condition number, as for a dense matrix; a
# triangular or diagonal `factorize(A)` reports it only in the solve
normAinv = try
p == 1 ? opnormestinv(A) : opnormestinv(copy(A'))
catch e
e isa SingularException || rethrow()
return convert(float(real(eltype(A))), Inf)
end
return opnorm(A, p) * normAinv
elseif p == 2
throw(ArgumentError("only 1- and Inf-norm condition numbers are implemented for sparse matrices, for 2-norm try cond(Array(A), 2) instead"))
else
Expand Down
6 changes: 6 additions & 0 deletions test/solvers/issues.jl
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,10 @@ end
@test factorize(sparse([0.0 1; 1 0])) \ b ≈ [0.0 1; 1 0] \ b
end

@testset "cond of a singular matrix is Inf" begin
@test cond(sparse([1.0 2; 3 6]), 1) === Inf
# the triangular solve, not the factorization, finds this one singular
@test cond(sparse([1.0 0; 2 0]), Inf) === Inf
end

end # module
Loading