# The LinearAlgebra.jl package's \`inv()\` function does not preserve matrix symmetry

**URL:** <https://discourse.julialang.org/t/the-linearalgebra-jl-packages-inv-function-does-not-preserve-matrix-symmetry/56605>\
**Category:** General Usage\
**Created:** [March 6, 2021, 8:12am UTC](https://discourse.julialang.org/t/the-linearalgebra-jl-packages-inv-function-does-not-preserve-matrix-symmetry/56605 "2021-03-06T08:12:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![BridgeBot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bridgebot/32/21491_2.png) [@BridgeBot](https://discourse.julialang.org/u/BridgeBot)\
**Post date:** [March 6, 2021, 8:12am UTC](https://discourse.julialang.org/t/the-linearalgebra-jl-packages-inv-function-does-not-preserve-matrix-symmetry/56605/1 "2021-03-06T08:12:43Z")

</div>

The LinearAlgebra.jl package’s `inv()` function does not preserve matrix symmetry.

```julia
while true
    R = randn((3,3))
    R[2,1] = R[1,2]; R[3,1] = R[1,3]; R[2,3]=R[3,2]
    @assert R == R'
    @assert inv(R) == inv(R)'
end

```

I’m about to open an issue for this, but in the meantime: is there a way to deal with this?

Note that the original poster on Slack cannot see your response here on Discourse. Consider _transcribing the appropriate answer back to Slack_, or pinging the poster here on Discourse so they can _follow this thread_.  
[(Original message :slack:)](https://julialang.slack.com/archives/C6A044SQH/p1615018349449600?thread_ts=1615018349.449600&cid=C6A044SQH) [(More Info)](https://github.com/JuliaCommunity/SlackBridge)

---

<div class="post-metadata">

**Author:** ![andreasnoack](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andreasnoack/32/27_2.png) [@andreasnoack](https://discourse.julialang.org/u/andreasnoack)\
**Post date:** [March 6, 2021, 8:24am UTC](https://discourse.julialang.org/t/the-linearalgebra-jl-packages-inv-function-does-not-preserve-matrix-symmetry/56605/2 "2021-03-06T08:24:37Z")

</div>

You can use `Symmetric` for this. It will make `inv` use the Bunch-Kaufman factorization which preserves symmetry (by only working on one of the triangles) instead of the default LU factorization where rounding errors will make the result slightly asymmetric. I.e.

```julia
julia> issymmetric(inv(R))
false

julia> issymmetric(inv(Symmetric(R)))
true

julia> typeof(inv(Symmetric(R)))
Symmetric{Float64, Matrix{Float64}}

```

---

<div class="post-metadata">

**Author:** ![imLew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/imlew/32/22324_2.png) [@imLew](https://discourse.julialang.org/u/imLew)\
**Post date:** [March 6, 2021, 8:34am UTC](https://discourse.julialang.org/t/the-linearalgebra-jl-packages-inv-function-does-not-preserve-matrix-symmetry/56605/3 "2021-03-06T08:34:58Z")

</div>

Thanks 👍

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [March 6, 2021, 8:39am UTC](https://discourse.julialang.org/t/the-linearalgebra-jl-packages-inv-function-does-not-preserve-matrix-symmetry/56605/4 "2021-03-06T08:39:54Z")

</div>

Also, floating point comparisons should use approximate equality.

---

<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:** [March 6, 2021, 9:47am UTC](https://discourse.julialang.org/t/the-linearalgebra-jl-packages-inv-function-does-not-preserve-matrix-symmetry/56605/5 "2021-03-06T09:47:43Z")

</div>

You shouldn’t really be using matrix inversion anyway.
