# Too much matrix inverse error?

**URL:** https://discourse.julialang.org/t/too-much-matrix-inverse-error/70974
**Category:** New to Julia
**Tags:** linearalgebra, matrices
**Created:** [November 4, 2021, 5:58pm UTC](https://discourse.julialang.org/t/too-much-matrix-inverse-error/70974 "2021-11-04T17:58:24Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![zhangjin](https://avatars.discourse-cdn.com/v4/letter/z/a9a28c/32.png) [@zhangjin](https://discourse.julialang.org/u/zhangjin)
#### Post date: [November 4, 2021, 5:58pm UTC](https://discourse.julialang.org/t/too-much-matrix-inverse-error/70974/1 "2021-11-04T17:58:24Z")

</div>

I use the `inv()` to do the inverse of the matrix, but I find that I don’t get the inverse matrix, or is it due to too much error?

This is my matrix

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/7/d754c69f935ecf063cf269b5c954f5d8369d27b1.png),  
but `inv(M1)*M1` not equal to unit matrix,  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/f/0/f03f5524b51c5c921b896a43b6f376b54783954d.png)  
Why is it happen?

---

<div class="post-metadata">

### Author: ![roflmaostc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roflmaostc/32/30123_2.png) [@roflmaostc](https://discourse.julialang.org/u/roflmaostc)
#### Post date: [November 4, 2021, 6:16pm UTC](https://discourse.julialang.org/t/too-much-matrix-inverse-error/70974/2 "2021-11-04T18:16:32Z")

</div>

Are you sure you really want the inverse matrix and not the solution of the equations which would be `y \ M`.

Also your matrix looks very sparse, maybe try to call explicitly

```julia
julia> using SparseArrays

julia> sparse(zeros((4,4)))
4×4 SparseMatrixCSC{Float64, Int64} with 0 stored entries:
  ⋅ ⋅ ⋅ ⋅ 
  ⋅ ⋅ ⋅ ⋅ 
  ⋅ ⋅ ⋅ ⋅ 
  ⋅ ⋅ ⋅ ⋅ 

```

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [November 4, 2021, 7:15pm UTC](https://discourse.julialang.org/t/too-much-matrix-inverse-error/70974/3 "2021-11-04T19:15:43Z")

</div>

> [@zhangjin](#):
>
> Why is it happen?

Maybe your matrix is badly conditioned (i.e., nearly singular), in which case floating-point roundoff errors are enormously amplified when you try to invert the matrix. Try computing `cond(M1)` to get the condition number — probably it is huge (\sim 10^{15}).

If you are solving systems with an ill-conditioned matrix, you probably need to think carefully about what you are doing. (e.g. if you need a [regularization](https://en.wikipedia.org/wiki/Regularization_(mathematics)), or if the problem is exactly singular and there is some other aspect of your problem that indicates which solution you want.)

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [November 4, 2021, 8:40pm UTC](https://discourse.julialang.org/t/too-much-matrix-inverse-error/70974/4 "2021-11-04T20:40:30Z")

</div>

You shouldn’t be inverting matrices. It’s not as bad as numerical algebra courses will tell you (as I fairly recently learned on here), but it’s still very rarely the best way to achieve what you want.
