# How to check if LU factorization failed?

**URL:** <https://discourse.julialang.org/t/how-to-check-if-lu-factorization-failed/5553>\
**Category:** Numerics\
**Tags:** question, linearalgebra\
**Created:** [August 25, 2017, 3:50am UTC](https://discourse.julialang.org/t/how-to-check-if-lu-factorization-failed/5553 "2017-08-25T03:50:26Z")\
**Posts on this page:** 10\
**Page:** 2

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [September 20, 2017, 6:13pm UTC](https://discourse.julialang.org/t/how-to-check-if-lu-factorization-failed/5553/21 "2017-09-20T18:13:27Z")

</div>

> [@juliohm](#):
>
> I have a try/catch block that is being executed thousands of times.

This also does not seem like the best solution, even if you could use `issuccess()`

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [September 20, 2017, 6:30pm UTC](https://discourse.julialang.org/t/how-to-check-if-lu-factorization-failed/5553/22 "2017-09-20T18:30:28Z")

</div>

Thank you @fredrikekre, what oneliner should I use for now?

> [@fredrikekre](#):
>
> This also does not seem like the best solution, even if you could use issuccess()

Yes, I am not sure what I could do instead, I need a check inside of a loop, at least `issuccess()` would be more performant?

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [September 20, 2017, 9:35pm UTC](https://discourse.julialang.org/t/how-to-check-if-lu-factorization-failed/5553/23 "2017-09-20T21:35:14Z")

</div>

> [@juliohm](#):
>
> what oneliner should I use for now?

For LU-factorization: `issuccess(F::LU) = F.info == 0`

> [@juliohm](#):
>
> Yes, I am not sure what I could do instead, I need a check inside of a loop, at least `issuccess()` would be more performant?

I don’t know your use case so its hard to tell. Why are the matrices possibly singular in the first place? What do you do if the factorization fails and what do you do if it succeeds?

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [September 20, 2017, 9:48pm UTC](https://discourse.julialang.org/t/how-to-check-if-lu-factorization-failed/5553/24 "2017-09-20T21:48:30Z")

</div>

@fredrikekre, I think I misinterpreted the interface then. I thought that `LU.info` returns 0 when the LU factorization succeeds and that `LinAlg.issuccess` stores the status after trying to use the already factorized object to solve the system `LU \ b`?

My use case is still under development, but you can see the try/catch block here: [https://github.com/juliohm/GeoStats.jl/blob/master/src/solvers/sgsim\_solver.jl#L143-L160](https://github.com/juliohm/GeoStats.jl/blob/master/src/solvers/sgsim_solver.jl#L143-L160)

Please ignore that line of code that says “fix possible numerical issues”, I will remove it soon I fix the estimator interface. Basically, I need to add some preconditioning code in the estimator before I can do anything else.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [September 20, 2017, 9:51pm UTC](https://discourse.julialang.org/t/how-to-check-if-lu-factorization-failed/5553/25 "2017-09-20T21:51:10Z")

</div>

> [@juliohm](#):
>
> I thought that `LU.info` returns 0 when the LU factorization succeeds

yes

> [@juliohm](#):
>
> and that LinAlg.issuccess stores the status

What do you mean? `LinAlg.issuccess` is just a function with the exact definition from my last post.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [September 20, 2017, 9:55pm UTC](https://discourse.julialang.org/t/how-to-check-if-lu-factorization-failed/5553/26 "2017-09-20T21:55:55Z")

</div>

I thought that `LinAlg.SingularException` was replaced by `LinAlg.issuccess`, and that any form of exception handling was removed from LinAlg in Julia v0.7. So basically, I if I want to check for failure, I need to use exceptions, right?

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [September 20, 2017, 10:07pm UTC](https://discourse.julialang.org/t/how-to-check-if-lu-factorization-failed/5553/27 "2017-09-20T22:07:21Z")

</div>

Perhaps an example:

```julia
julia> A = zeros(2,2); b = rand(2);

julia> F = lufact(A); # this doesn't fail even though the matrix is singular

julia> LinAlg.issuccess(F) # false, since the factorization F is failed
false

julia> F\b # in the backsolve we throw if F is failed
ERROR: Base.LinAlg.SingularException(1)
[...]

```

So we still throw an `SingularException`, but `issuccess` can be used to know if we will throw before doing the backsolve.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [September 20, 2017, 10:24pm UTC](https://discourse.julialang.org/t/how-to-check-if-lu-factorization-failed/5553/28 "2017-09-20T22:24:05Z")

</div>

What about the success check for Cholesky? I don’t see a field `info` for it so that I could write my own issuccess in Julia \<= v0.6.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [September 20, 2017, 10:31pm UTC](https://discourse.julialang.org/t/how-to-check-if-lu-factorization-failed/5553/29 "2017-09-20T22:31:37Z")

</div>

That field was implemented in [https://github.com/JuliaLang/julia/pull/21976](https://github.com/JuliaLang/julia/pull/21976) so it does not exist on 0.6. You can probably do some local backporting of that PR if you really need it for 0.6.

But if you sometimes will use matrices that are not positive definite, why not just use `LU`? Are you switching to `LU` in case of a `PosDefException`?

Edit: On 0.6 we actually have `issuccess(F::Cholesky) = true` since if you manage to obtain a `Cholesky` object it is always a success.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [September 20, 2017, 10:41pm UTC](https://discourse.julialang.org/t/how-to-check-if-lu-factorization-failed/5553/30 "2017-09-20T22:41:17Z")

</div>

Ok, so I can just write `issuccess(F::Cholesky) = true` for now on my package and wait for Julia v0.7 where I can remove these definitions and use the ones in LinAlg.

[Previous page](https://discourse.julialang.org/t/how-to-check-if-lu-factorization-failed/5553.md?page=1)
