# Matrix multiplied by inverse matrix does not reproduce identity matrix

**URL:** <https://discourse.julialang.org/t/matrix-multiplied-by-inverse-matrix-does-not-reproduce-identity-matrix/65706>\
**Category:** General Usage\
**Tags:** linearalgebra, inverse\
**Created:** [August 3, 2021, 12:14am UTC](https://discourse.julialang.org/t/matrix-multiplied-by-inverse-matrix-does-not-reproduce-identity-matrix/65706 "2021-08-03T00:14:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mengxiaoliu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mengxiaoliu/32/23601_2.png) [@mengxiaoliu](https://discourse.julialang.org/u/mengxiaoliu)\
**Post date:** [August 3, 2021, 12:14am UTC](https://discourse.julialang.org/t/matrix-multiplied-by-inverse-matrix-does-not-reproduce-identity-matrix/65706/1 "2021-08-03T00:14:17Z")

</div>

Hello,  
I found that multiplying a matrix by its inverse does not yield an identity matrix. Please see below a MWE:

```julia
matrix = [5.93419 4.8067; 4.8067 5.93419]
invmatrix = inv(matrix)
matrix*invmatrix

```

The last line generates the following output:

> 2×2 Matrix{Float64}:  
> 1.0 4.44089e-16  
> 4.44089e-16 1.0

Is there any way to make the off-diagonal elements zero instead of some value that is very close to zero? Thanks!

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [August 3, 2021, 12:58am UTC](https://discourse.julialang.org/t/matrix-multiplied-by-inverse-matrix-does-not-reproduce-identity-matrix/65706/2 "2021-08-03T00:58:02Z")

</div>

One solution is posted [here](https://discourse.julialang.org/t/how-to-print-a-number-to-x-digits/50526/3), for display purposes.

---

<div class="post-metadata">

**Author:** ![mengxiaoliu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mengxiaoliu/32/23601_2.png) [@mengxiaoliu](https://discourse.julialang.org/u/mengxiaoliu)\
**Post date:** [August 3, 2021, 1:44am UTC](https://discourse.julialang.org/t/matrix-multiplied-by-inverse-matrix-does-not-reproduce-identity-matrix/65706/3 "2021-08-03T01:44:19Z")

</div>

Thanks! That solves the previous MWE, but does not solve the issue I am having. Please see below a new MWE with the method you suggested:

```julia
using Printf
Base.show(io::IO, f::Float64) = @printf(io, "%.4f", f)
Σzz = [5.9342 5.3408 4.8067 4.3260; 5.3408 5.9342 5.3408 4.8067; 4.8067 5.3408 5.9342 5.3408; 4.3260 4.8067 5.3408 5.9342]
Σzz⁺ = [5.3408 4.8067 4.3260; 5.9342 5.3408 4.8067; 5.3408 5.9342 5.3408; 4.8067 5.3408 5.9342]
Σz⁺z⁺ = [5.9342 5.3408 4.8067; 5.3408 5.9342 5.3408; 4.8067 5.3408 5.9342]
iΣz⁺z⁺ = inv(Σz⁺z⁺)
tΣzz⁺ = transpose(Σzz⁺)
varc .= Σzz - Σzz⁺ * iΣz⁺z⁺ * tΣzz⁺
issymmetric(varc)

```

Σzz⁺ and Σz⁺z⁺ are just partitions of Σzz.  
varc is supposed to be symmetric, but the last line of code shows that it is not.  
Is there any way to make varc symmetric?  
Thank you!

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [August 3, 2021, 2:36am UTC](https://discourse.julialang.org/t/matrix-multiplied-by-inverse-matrix-does-not-reproduce-identity-matrix/65706/4 "2021-08-03T02:36:49Z")

</div>

It’s _nearly_ symmetric. Because of the [imprecision inherent in floating point numbers](https://discourse.julialang.org/t/psa-floating-point-arithmetic/8678), it’s best to use approximate comparisons like this:

```julia
julia> varc ≈ varc'
true

```

The approximate equality symbol can be typed in the REPL with `\approx<TAB>`, or you can use the equivalent function call: `isapprox(varc, varc')`. If you know your matrix is structurally symmetric, you can use the `Symmetric` wrapper type:

```julia
julia> Symmetric(varc)
4×4 Symmetric{Float64, Matrix{Float64}}:
  1.1275 -0.0000 -0.0000 -0.0000
 -0.0000 -0.0000 -0.0000 -0.0000
 -0.0000 -0.0000 -0.0000 -0.0000
 -0.0000 -0.0000 -0.0000 -0.0000

julia> issymmetric(ans)
true

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 3, 2021, 9:12am UTC](https://discourse.julialang.org/t/matrix-multiplied-by-inverse-matrix-does-not-reproduce-identity-matrix/65706/5 "2021-08-03T09:12:47Z")

</div>

> [@mengxiaoliu](#):
>
> make the off-diagonal elements zero instead of some value that is very close to zero?

You can zero them out manually, eg as in

```julia
M = matrix*invmatrix;
M[abs.(M) .≤ 1e-10] .= 0;
M

```

but this is hard to do in a disciplined manner in general.
