# How to force a matrix to be Hermitian?

**URL:** https://discourse.julialang.org/t/how-to-force-a-matrix-to-be-hermitian/18479
**Category:** General Usage
**Tags:** linearalgebra
**Created:** [December 9, 2018, 6:36am UTC](https://discourse.julialang.org/t/how-to-force-a-matrix-to-be-hermitian/18479 "2018-12-09T06:36:05Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Jameson\_Quinn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson_quinn/32/1490_2.png) [@Jameson\_Quinn](https://discourse.julialang.org/u/Jameson_Quinn)
#### Post date: [December 9, 2018, 6:36am UTC](https://discourse.julialang.org/t/how-to-force-a-matrix-to-be-hermitian/18479/1 "2018-12-09T06:36:05Z")

</div>

I’m getting “ArgumentError: matrix is not symmetric/Hermitian.” on a call `MvNormal(x[r],Matrix(Hermitian((noiseMatrix(m)+fuzz)[r,r])))`. How do I avoid this if `Matrix(Hermitian(...))` doesn’t? Note, both noiseMatrix(m) and fuzz are created using quadratic forms, so the only non-Hermitian nature they have should be from rounding error. I just want to ignore it and move on. How do I do this?

Note, this is on 0.6. If updating to 1.x would help, let me know.

Thanks,  
Jameson

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [December 9, 2018, 7:01am UTC](https://discourse.julialang.org/t/how-to-force-a-matrix-to-be-hermitian/18479/2 "2018-12-09T07:01:32Z")

</div>

This works on my machine:

```julia
julia> using Distributions

julia> MvNormal(rand(3), Matrix(Hermitian(rand(3,3) + I)))
FullNormal(
dim: 3
μ: [0.652153, 0.850454, 0.706293]
Σ: [1.25598 0.361987 0.768328; 0.361987 1.75802 0.954967; 0.768328 0.954967 1.52256]
)

```

---

<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 9, 2018, 12:23pm UTC](https://discourse.julialang.org/t/how-to-force-a-matrix-to-be-hermitian/18479/3 "2018-12-09T12:23:47Z")

</div>

> [@mohamed82008](#):
>
> `MvNormal(rand(3), Matrix(Hermitian(rand(3,3) + I)))`

You shouldn’t have to convert back to a `Matrix` — `Hermitian` is a special matrix type that tells linear-algebra functions to take advantage of the Hermitian property if they can.

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [December 10, 2018, 4:51am UTC](https://discourse.julialang.org/t/how-to-force-a-matrix-to-be-hermitian/18479/4 "2018-12-10T04:51:50Z")

</div>

It can sometimes backfire in speed though at least in multiplication.

---

<div class="post-metadata">

### Author: ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)
#### Post date: [December 10, 2018, 9:33am UTC](https://discourse.julialang.org/t/how-to-force-a-matrix-to-be-hermitian/18479/5 "2018-12-10T09:33:09Z")

</div>

Take a bigger matrix and chances are higher that you get rounding errors (with 6x6 you already have something like 50% 😉 :

```julia
julia> MvNormal(rand(6), Matrix(Hermitian(rand(6,6) + I)))
FullNormal(
dim: 6
μ: [0.209424, 0.0390436, 0.944169, 0.669357, 0.624151, 0.0911069]
Σ: [1.59011 0.289772 … 0.244625 0.620135; 0.289772 1.2934 … 0.219601 0.932076; … ; 0.244625 0.219601 … 1.73461 0.12922; 0.620135 0.932076 … 0.12922 1.24544]
)

julia> MvNormal(rand(6), Matrix(Hermitian(rand(6,6) + I)))
ERROR: PosDefException: matrix is not positive definite; Cholesky factorization failed.
Stacktrace:
 [1] checkpositivedefinite at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v1.0/LinearAlgebra/src/factorization.jl:11 [inlined]
 [2] #cholesky!#90(::Bool, ::Function, ::Hermitian{Float64,Array{Float64,2}}, ::Val{false}) at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v1.0/LinearAlgebra/src/cholesky.jl:153
 [3] #cholesky! at ./none:0 [inlined]
 [4] #cholesky!#91(::Bool, ::Function, ::Array{Float64,2}, ::Val{false}) at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v1.0/LinearAlgebra/src/cholesky.jl:185
 [5] #cholesky! at ./none:0 [inlined] (repeats 2 times)
 [6] #cholesky#95 at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v1.0/LinearAlgebra/src/cholesky.jl:275 [inlined]
 [7] cholesky at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v1.0/LinearAlgebra/src/cholesky.jl:275 [inlined] (repeats 2 times)
 [8] Type at /Users/tamasgal/.julia/packages/PDMats/Kouno/src/pdmat.jl:19 [inlined]
 [9] MvNormal(::Array{Float64,1}, ::Array{Float64,2}) at /Users/tamasgal/.julia/packages/Distributions/WHjOk/src/multivariate/mvnormal.jl:208
 [10] top-level scope at none:0

```

---

<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: [December 10, 2018, 9:50am UTC](https://discourse.julialang.org/t/how-to-force-a-matrix-to-be-hermitian/18479/6 "2018-12-10T09:50:22Z")

</div>

> [@tamasgal](#):
>
> bigger matrix and chances are higher that you get rounding errors

In your example, I think it is lack of positive definiteness rather than rounding errors that is causing the problems.

More generally, while working with the covariance matrix is traditional analytically, numerical calculations almost always use a factorization of it, commonly Cholesky.

I think that `Distributions.MvNormal`’s interface is less than ideal, so I am experimenting with a WIP [alternative](https://github.com/tpapp/AltDistributions.jl/blob/83ad31d335b1127d4860a5ea5076258b69da063f/src/AltDistributions.jl#L113), which I use for Bayesian inference.

---

<div class="post-metadata">

### Author: ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)
#### Post date: [December 10, 2018, 10:08am UTC](https://discourse.julialang.org/t/how-to-force-a-matrix-to-be-hermitian/18479/7 "2018-12-10T10:08:48Z")

</div>

Ahm yes, you’re right!

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [December 10, 2018, 10:38am UTC](https://discourse.julialang.org/t/how-to-force-a-matrix-to-be-hermitian/18479/8 "2018-12-10T10:38:52Z")

</div>

Nice tip: a symmetric [strongly diagonally dominant](https://en.wikipedia.org/wiki/Diagonally_dominant_matrix) matrix is positive definite 😉

```julia
julia> using LinearAlgebra

julia> isposdef(Matrix(Hermitian(rand(6,6) + 5I)))
true

```
