Hi. I am playing around with Julia while I revisit my old Linear Algebra textbook. One thing I found is Julia’s LU seems to give different results than python’s scipy for some matrix: e.g.:
As a newbie, it could be I am using a wrong function or function with wrong parameters or wrong package. Any thoughts about it? Thank you very much for your time.
Please add compilable code (that people can copy-paste and test) instead of screenshots. I’d add that the error appears in Julia version 0.7 onward, older versions give the same result as Python and MATLAB (I tested).
This should be A = [1 3 3 2;2 6 9 7; -1 -3 3 4] to match OP (note the third-to-last entry). With that matrix the SingularException is reproducible. The decomposition returned by scipy is valid (P * L * U is indeed A, L is lower-triangular, and U is upper-triangular). Note that scipy’s U has a zero on the diagonal.
With check=false, Julia returns an LU object that prints as
Failed factorization of type LU{Float64,Array{Float64,2}}
but the same L, U and P as the ones returned by scipy’s lu can be obtained from the returned LU object.
Julia’s lu calls lu!, which calls LAPACK.getrf!. Note that
where, according to Linear Algebra · The Julia Language, info == 2 indicates that U[2,2] is singular (and info < 0 indicates failure). Julia considers the LU decomposition to be successful iff info == 0:
So matrix A has rank loss, as indicated by matrix U. Finding the unknown x from A*x = b gives slightly different result depending on how x is computed, but that is due to the pseudo inverse algorithm, I guess.
Yes it seems a bit misleading that this is written as a “Failed factorization”, though the SingularException is arguably a reasonable default (if we think people will typically use the factorization for later solves, rather than directly using the factors for something else?). The lapack documentation says
if INFO = i, U(i,i) is exactly zero. The factorization has been completed, but the factor U is exactly singular.
The function issuccess comes from
though it looks like there wasn’t any discussion about what to do with positive info there and looking in git history doesn’t tell whether this was carefully chosen on purpose. @andreasnoack is this just an oversight?
We have discussed the formulation somewhere. Right now I don’t remember in which issue or if it was only on Slack. Indeed, it’s misleading to call it failed for LU so it would better to change the formulation for LU. The changes that lead to this were motivated by the Cholesky which actually is failed when a positive error message is returned.