# ERROR: LinearAlgebra.SingularException(5)

**URL:** <https://discourse.julialang.org/t/error-linearalgebra-singularexception-5/106491>\
**Category:** Statistics\
**Tags:** matrices, inverse\
**Created:** [November 20, 2023, 9:23pm UTC](https://discourse.julialang.org/t/error-linearalgebra-singularexception-5/106491 "2023-11-20T21:23:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shayan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shayan/32/33025_2.png) [@Shayan](https://discourse.julialang.org/u/Shayan)\
**Post date:** [November 20, 2023, 9:23pm UTC](https://discourse.julialang.org/t/error-linearalgebra-singularexception-5/106491/1 "2023-11-20T21:23:51Z")

</div>

How should I deal with this error: `ERROR: LinearAlgebra.SingularException(5)`

I have the following matrix:

```julia
Xₜ⁽ˡ⁾ = [
0.997838 1.01925 1.01489 1.00797 0.997964
0.992047 1.02963 0.987861 0.990278 0.991172
]

```

And, I aim to calculate the following expression:

```julia
(Xₜ⁽ˡ⁾'*Xₜ⁽ˡ⁾)^-1

```

But it throws the error I mentioned earlier. This is real data, and it’s not synthetic. Can increasing the number of data points (by adding more rows or/and columns) solve the problem?  
P.S.: Here is the result of `Xₜ⁽ˡ⁾'*Xₜ⁽ˡ⁾`:

```julia
julia> Xₜ⁽ˡ⁾'*Xₜ⁽ˡ⁾
5×5 Matrix{Float64}:
 1.97984 2.03849 1.9927 1.98819 1.9791
 2.03849 2.09901 2.05156 2.04699 2.03772
 1.9927 2.05156 2.00587 2.00124 1.99196
 1.98819 2.04699 2.00124 1.99665 1.98745
 1.9791 2.03772 1.99196 1.98745 1.97835

```

The error is thrown for the inversion operation.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [November 20, 2023, 9:53pm UTC](https://discourse.julialang.org/t/error-linearalgebra-singularexception-5/106491/2 "2023-11-20T21:53:44Z")

</div>

In general you shouldn’t be using the normal equations. Rather than computing `(Xₜ⁽ˡ⁾'*Xₜ⁽ˡ⁾)^-1`, you should probably be using a linear solve with `Xₜ⁽ˡ⁾` (using a factorization like `Qr` if you need to do multiple solves).

---

<div class="post-metadata">

**Author:** ![Shayan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shayan/32/33025_2.png) [@Shayan](https://discourse.julialang.org/u/Shayan)\
**Post date:** [November 20, 2023, 9:57pm UTC](https://discourse.julialang.org/t/error-linearalgebra-singularexception-5/106491/3 "2023-11-20T21:57:02Z")

</div>

> [@Shayan](#):
>
> Can increasing the number of data points (by adding more rows and/or columns) solve the problem?

It might, since the problem is with the determinant of `(Xₜ⁽ˡ⁾'*Xₜ⁽ˡ⁾)`. I’m trying to implement an algorithm that is published in a high-ranked journal. I can not believe they have not encountered this problem, since I have used real data as well. Hence, there are two possibilities:

1. They’ve employed the algorithm on much more data.
2. They might somehow handled this trough a different procedure.

---

<div class="post-metadata">

**Author:** ![Shayan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shayan/32/33025_2.png) [@Shayan](https://discourse.julialang.org/u/Shayan)\
**Post date:** [November 20, 2023, 9:59pm UTC](https://discourse.julialang.org/t/error-linearalgebra-singularexception-5/106491/4 "2023-11-20T21:59:36Z")

</div>

> [@Oscar\_Smith](#):
>
> you should probably be using a linear solve with `Xₜ⁽ˡ⁾` (using a factorization like `Qr` if you need to do multiple solves).

How should I do that?

> [@Oscar\_Smith](#):
>
> if you need to do multiple solves

What do you mean by “multiple solves”? It’s part of an algorithm and should be run multiple times with different inputs.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [November 20, 2023, 10:06pm UTC](https://discourse.julialang.org/t/error-linearalgebra-singularexception-5/106491/5 "2023-11-20T22:06:03Z")

</div>

so rather than computing `(Xₜ⁽ˡ⁾'*Xₜ⁽ˡ⁾)^-1*(Xₜ⁽ˡ⁾'*v)`, you can just compute `Xₜ⁽ˡ⁾\v` which will automatically compute a least-squares or minimum norm solution depending on whether you are over or under-determined.

---

<div class="post-metadata">

**Author:** ![Shayan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shayan/32/33025_2.png) [@Shayan](https://discourse.julialang.org/u/Shayan)\
**Post date:** [November 20, 2023, 10:09pm UTC](https://discourse.julialang.org/t/error-linearalgebra-singularexception-5/106491/6 "2023-11-20T22:09:48Z")

</div>

Thank you so much! I will try what you’ve recommended and let you know the result.

Mr @Oscar_Smith, your solution is great. Thank you so much.
