# Struggling with isposdef and floating point errors

**URL:** <https://discourse.julialang.org/t/struggling-with-isposdef-and-floating-point-errors/111582>\
**Category:** General Usage\
**Tags:** question, linearalgebra\
**Created:** [March 13, 2024, 3:05pm UTC](https://discourse.julialang.org/t/struggling-with-isposdef-and-floating-point-errors/111582 "2024-03-13T15:05:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![theplatters](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theplatters/32/52199_2.png) [@theplatters](https://discourse.julialang.org/u/theplatters)\
**Post date:** [March 13, 2024, 3:05pm UTC](https://discourse.julialang.org/t/struggling-with-isposdef-and-floating-point-errors/111582/1 "2024-03-13T15:05:24Z")

</div>

I am trying to do a Quasi-Newton method with a basic cholesky adaptation, to guarantee B is positive definite. The code goes as follows:

```julia
function choleksyadaption!(A,β = 10e-3, max_iter = 1000)
    @info "Adapting Cholesky Factorization to make Hessian positive definite."
    if (isposdef(A))
        return nothing
    end
    if any(diag(A) .<= 0)
     τ = 0
    else
        τ = minimum(diag(A)) - β
    end

    for i in 1:max_iter
        @info i τ, A
		A = A + τ * I
        if isposdef(A)
            return nothing
        else
            τ = max(2 * τ, β)
        end
    end
    return nothing
end

```

The problem is, in my eyes, that `isposidef`, which calls `ishermitian` requires the non diagonal elements to be exactly the same, which isn’t easy in numerics. For example

```julia
ishermitian([1 0.018511830506205723; 0.01851183050620573 1])

```

returns false, which makes sense, but I am unable to work with it. Is there a workarround, that I am missing?

Thanks!

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [March 13, 2024, 3:10pm UTC](https://discourse.julialang.org/t/struggling-with-isposdef-and-floating-point-errors/111582/2 "2024-03-13T15:10:35Z")

</div>

You can use [`Hermitian`](https://docs.julialang.org/en/v1/stdlib/LinearAlgebra/#LinearAlgebra.Hermitian) to tell the program that a matrix is supposed to be Hermitian. Then it will only look at half the matrix (by default, the upper triangle) and assume that the opposite triangle is conjugate-equal.

```julia-repl
julia> using LinearAlgebra

julia> Hermitian([1 2; 3 4], :U) # :U is the default
2×2 Hermitian{Int64, Matrix{Int64}}:
 1 2
 2 4

julia> Hermitian([1 2; 3 4], :L)
2×2 Hermitian{Int64, Matrix{Int64}}:
 1 3
 3 4

```

---

<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:** [March 13, 2024, 7:25pm UTC](https://discourse.julialang.org/t/struggling-with-isposdef-and-floating-point-errors/111582/3 "2024-03-13T19:25:28Z")

</div>

See also: [`isposdef` inconsistent results, numerically unstable? - #10 by andreasnoack](https://discourse.julialang.org/t/isposdef-inconsistent-results-numerically-unstable/9013/10)
