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

Don't throw in cholfact if matrix is not Hermitian #23315

Merged
merged 2 commits into from
Aug 22, 2017
Merged

Conversation

fredrikekre
Copy link
Member

@fredrikekre fredrikekre commented Aug 18, 2017

As discussed here and comments below.

We could perhaps dedicate an info code for non-hermitianness, like -1 or something instead? Then we could throw a more targeted error message (similar to what we had before this PR) or throw a HermitianException, instead of

julia> A = rand(5,5); x = rand(5); cholfact(A)\x
ERROR: Base.LinAlg.PosDefException(1)
Stacktrace:
 [1] A_ldiv_B! at ./linalg/cholesky.jl:412 [inlined]
 [2] \(::Base.LinAlg.Cholesky{Float64,Array{Float64,2}}, ::Array{Float64,1}) at ./linalg/factorization.jl:71

Thoughts?

@fredrikekre fredrikekre added the linear algebra Linear algebra label Aug 18, 2017
@KristofferC
Copy link
Member

Make an Enum for info codes?

Copy link
Member

@andreasnoack andreasnoack left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In LAPACK, a positive info code corresponds to the index of the non-positive diagonal entry. However, we don't use that information right now so it is not critical. We could use a negative error code because they are only used in LAPACK when the input is invalid so that should never happen.

@test_throws ArgumentError cholfact!(copy(A))
@test_throws ArgumentError chol(A)
@test_throws ArgumentError Base.LinAlg.chol!(copy(A))
@test cholfact(A).info == 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe test with !issuccess instead.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, that's better.

@andreasnoack
Copy link
Member

andreasnoack commented Aug 18, 2017

Make an Enum for info codes?

Right now the info field encodes where the factorization failed. Would you just have two values? One for non-Hermitian and one for non-positive pivot?

@fredrikekre
Copy link
Member Author

Right, since a negative info code will error before we exit the LAPACK module, we could use a negative code for non-hermitian. And add a show method for PosDefException, and, in case of info = -1 print something like: ERROR: Base.LinAlg.PosDefException(3): matrix is not Hermitian. Thoughts on that? :)

@@ -32,6 +32,15 @@ end
struct PosDefException <: Exception
info::BlasInt
end
function Base.showerror(io::IO, ex::PosDefException)
print(io, "PosDefException: matrix is not ")
Copy link
Member Author

@fredrikekre fredrikekre Aug 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ERROR: PosDefException: matrix is not Hermitian. is maybe not the nicest though. Should we instead have a CholeskyException that print one of

ERROR: CholeskyException: Cholesky factorization failed; matrix is not Hermitian.
ERROR: CholeskyException: Cholesky factorization failed; matrix is not positive definite.

Edit: Cholesky probably not needed, instead:

ERROR: CholeskyException: factorization failed; matrix is not Hermitian.
ERROR: CholeskyException: factorization failed; matrix is not positive definite.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think PosDef is fine. We use the definition that requires the matrix to be Hermitian consistently. Some users have been taught the other definition that allows for non-Hermitian matrices but I think the error message is sufficiently clear about the reason for the failure.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, as the PR currently? Or should I drop the second commit?

@andreasnoack andreasnoack merged commit 52543df into master Aug 22, 2017
@andreasnoack andreasnoack deleted the fe/chol-nothrow branch August 22, 2017 03:01
@andreasnoack
Copy link
Member

No. I think it is fine as it is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
linear algebra Linear algebra
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants