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

Use exact algorithm for cond(A,1), and fix for singular matrices #33548

Merged
merged 7 commits into from
Oct 17, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
15 changes: 11 additions & 4 deletions stdlib/LinearAlgebra/src/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1418,15 +1418,22 @@ function cond(A::AbstractMatrix, p::Real=2)
if p == 2
v = svdvals(A)
maxv = maximum(v)
return maxv == 0.0 ? oftype(real(A[1,1]),Inf) : maxv / minimum(v)
return iszero(maxv) ? oftype(real(maxv), Inf) : maxv / minimum(v)
elseif p == 1 || p == Inf
checksquare(A)
return _cond1Inf(A, p)
try
Ainv = inv(A)
return opnorm(A, p)*opnorm(Ainv, p)
catch e
if isa(e, LAPACKException) || isa(e, SingularException)
return convert(float(real(eltype(A))), Inf)
else
rethrow()
end
end
end
throw(ArgumentError("p-norm must be 1, 2 or Inf, got $p"))
end
_cond1Inf(A::StridedMatrix{<:BlasFloat}, p::Real) = _cond1Inf(lu(A), p, opnorm(A, p))
_cond1Inf(A::AbstractMatrix, p::Real) = opnorm(A, p)*opnorm(inv(A), p)

## Lyapunov and Sylvester equation

Expand Down
7 changes: 0 additions & 7 deletions stdlib/LinearAlgebra/src/lu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -478,13 +478,6 @@ inv!(A::LU{T,<:StridedMatrix}) where {T} =
ldiv!(A.factors, copy(A), Matrix{T}(I, size(A, 1), size(A, 1)))
inv(A::LU{<:BlasFloat,<:StridedMatrix}) = inv!(copy(A))

function _cond1Inf(A::LU{<:BlasFloat,<:StridedMatrix}, p::Number, normA::Real)
if p != 1 && p != Inf
throw(ArgumentError("p must be either 1 or Inf"))
end
return inv(LAPACK.gecon!(p == 1 ? '1' : 'I', A.factors, normA))
end

# Tridiagonal

# See dgttrf.f
Expand Down
6 changes: 6 additions & 0 deletions stdlib/LinearAlgebra/test/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ Random.seed!(1234321)
@test_throws ArgumentError cond(a,3)
end
end
@testset "Singular matrices" for p in (1, 2, Inf)
@test cond(zeros(Int, 2, 2), p) == Inf
@test cond(zeros(2, 2), p) == Inf
@test cond([0 0; 1 1], p) == Inf
@test cond([0. 0.; 1. 1.], p) == Inf
end
end

areal = randn(n,n)/2
Expand Down
13 changes: 9 additions & 4 deletions stdlib/LinearAlgebra/test/lu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,6 @@ end
@test_throws DomainError logdet([1 1; 1 -1])
end

@testset "Issue 21453" begin
@test_throws ArgumentError LinearAlgebra._cond1Inf(lu(randn(5,5)), 2, 2.0)
end

@testset "REPL printing" begin
bf = IOBuffer()
show(bf, "text/plain", lu(Matrix(I, 4, 4)))
Expand Down Expand Up @@ -341,4 +337,13 @@ end
@test F.p == []
end

@testset "Issue #33547, condition number of 2x2 matrix" begin
M = [1.0 -2.0; -2.0 -1.5]
@test cond(M, 1) ≈ 2.227272727272727
end
@testset "Issue #33297, condition number of singular matrix" begin
M = [1. 1.; 0. 0.]
@test cond(M, 1) == Inf
end

end # module TestLU