# Checking the symmetry of a matrix

**URL:** <https://discourse.julialang.org/t/checking-the-symmetry-of-a-matrix/73206>\
**Category:** General Usage\
**Tags:** question, linearalgebra, matrices\
**Created:** [December 16, 2021, 3:05pm UTC](https://discourse.julialang.org/t/checking-the-symmetry-of-a-matrix/73206 "2021-12-16T15:05:29Z")\
**Posts on this page:** 1\
**Showing post:** 3

<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:** [December 16, 2021, 3:33pm UTC](https://discourse.julialang.org/t/checking-the-symmetry-of-a-matrix/73206/3 "2021-12-16T15:33:30Z")

</div>

> [@trahflow](#):
>
> There is the build-in function `isapprox` , which allows you to specify absolute and relative tolerances. You could just broadcast this over the full array ( `A - A'` ) and reduce with `all` .

**Don’t broadcast** it! `isapprox` works directly for matrices. Just do `isapprox(A, A', rtol=1e-12)` or whatever your tolerance is.

The reason you don’t want to broadcast `isapprox` elementwise is that when you are asking “is this difference small” you need to know “small compared to what?” And the correct scale to compare to is generally based on the _whole_ matrix.

For example, this matrix A is _very_ non-symmetrical, and in fact is [defective](https://en.wikipedia.org/wiki/Defective_matrix):

```julia
julia> A = [1e-15 1e-15; 0 1e-15]
2×2 Matrix{Float64}:
 1.0e-15 1.0e-15
 0.0 1.0e-15

```

whereas this matrix B is nearly Hermitian:

```julia
julia> B = [1 1e-15; 0 1]
2×2 Matrix{Float64}:
 1.0 1.0e-15
 0.0 1.0

```

_even though_ A - A^T = B - B^T!

```julia
julia> A - A' == B - B'
true

```

If you use `isapprox` on the whole matrix, it will tell you this:

```julia
julia> isapprox(A, A', rtol=1e-12)
false

julia> isapprox(B, B', rtol=1e-12)
true

```

whereas if you try to use `isapprox` element-wise it will fail for `B`:

```julia
julia> all(isapprox.(B, B', rtol=1e-12))
false

```

The reason is that, if you look elementwise, the test `0 ≈ 1e-15` must necessarily fail with any _relative_ tolerance, because you don’t have any yardstick to know that `1e-15` is small in an “absolute” sense. See also [this discussion](https://discourse.julialang.org/t/bug-with-isapprox/7600/5) and the [`isapprox` documentation](https://docs.julialang.org/en/v1/base/math/#Base.isapprox) on comparison to zero.

---

_[View the full topic](https://discourse.julialang.org/t/checking-the-symmetry-of-a-matrix/73206)._
