# Logdet of a cholesky! factorized matrix trows a \`DomainError\`

**URL:** https://discourse.julialang.org/t/logdet-of-a-cholesky-factorized-matrix-trows-a-domainerror/47592
**Category:** General Usage
**Tags:** linearalgebra
**Created:** [October 1, 2020, 10:38am UTC](https://discourse.julialang.org/t/logdet-of-a-cholesky-factorized-matrix-trows-a-domainerror/47592 "2020-10-01T10:38:05Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Maximilian](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maximilian/32/12206_2.png) [@Maximilian](https://discourse.julialang.org/u/Maximilian)
#### Post date: [October 1, 2020, 10:38am UTC](https://discourse.julialang.org/t/logdet-of-a-cholesky-factorized-matrix-trows-a-domainerror/47592/1 "2020-10-01T10:38:06Z")

</div>

The following code trows a `DomainError` after `cholesky!()` was called:

```julia
mat = [2.4 2.05
       2.05 2.4]

logdet(mat)

logdet(cholesky(mat))

cholesky!(mat)

logdet(mat)

```

Why is this the case?

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [October 1, 2020, 11:25am UTC](https://discourse.julialang.org/t/logdet-of-a-cholesky-factorized-matrix-trows-a-domainerror/47592/2 "2020-10-01T11:25:12Z")

</div>

`cholesky!` modifies the matrix in place and returns a very thin wrapper excluding the ignored data. The original binding still has that data though:

```julia
julia> using LinearAlgebra

julia> mat = [2.4 2.05
              2.05 2.4]
2×2 Array{Float64,2}:
 2.4 2.05
 2.05 2.4

julia> a = cholesky!(mat)
Cholesky{Float64,Array{Float64,2}}
U factor:
2×2 UpperTriangular{Float64,Array{Float64,2}}:
 1.54919 1.32327
  ⋅ 0.80558

julia> logdet(a)
0.4430819716794719

julia> mat
2×2 Array{Float64,2}:
 1.54919 1.32327
 2.05 0.80558

```

Since `cholesky!` might be called with very large matrices, preserving the existing memory is vital for good performance. Dispatch takes it from there and efficiently works with the wrapper instead of the underlying data.

---

<div class="post-metadata">

### Author: ![Maximilian](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maximilian/32/12206_2.png) [@Maximilian](https://discourse.julialang.org/u/Maximilian)
#### Post date: [October 1, 2020, 11:32am UTC](https://discourse.julialang.org/t/logdet-of-a-cholesky-factorized-matrix-trows-a-domainerror/47592/3 "2020-10-01T11:32:30Z")

</div>

Thanks, so the most performant solution would be (like you did) calling `a = cholesky!(mat)` and then working with `a`?

Also, if I am calling `isposdef!(mat)` to first check whether `mat` is positive definite, is there a way to further work with the resulting cholesky factorization?

Because in my code, I would like to check whether `mat` is positive definite and if that is the case, I would like to perform a cholesky factorization.

---

<div class="post-metadata">

### Author: ![Maximilian](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maximilian/32/12206_2.png) [@Maximilian](https://discourse.julialang.org/u/Maximilian)
#### Post date: [October 5, 2020, 8:39pm UTC](https://discourse.julialang.org/t/logdet-of-a-cholesky-factorized-matrix-trows-a-domainerror/47592/4 "2020-10-05T20:39:17Z")

</div>

Okay, looking at LinearAlgebra I found that

```julia
isposdef!(A::AbstractMatrix) =
    ishermitian(A) && isposdef(cholesky!(Hermitian(A); check = false))

```

since I know that my matrix is hermitian, I guess the best solution in my case is

```julia
mat = [2.4 2.05
       2.05 2.4]

a = cholesky!(Hermitian(mat); check = false)

if isposdef(a)
  ... here I can work with a
else
  ...

```
