# How to optimize simple matrix equation solving

**URL:** <https://discourse.julialang.org/t/how-to-optimize-simple-matrix-equation-solving/23035>\
**Category:** Performance\
**Tags:** question, performance, linearalgebra\
**Created:** [April 11, 2019, 1:04pm UTC](https://discourse.julialang.org/t/how-to-optimize-simple-matrix-equation-solving/23035 "2019-04-11T13:04:19Z")\
**Posts on this page:** 1\
**Showing post:** 16

<div class="post-metadata">

**Author:** ![mfalt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mfalt/32/12467_2.png) [@mfalt](https://discourse.julialang.org/u/mfalt)\
**Post date:** [April 23, 2019, 10:36am UTC](https://discourse.julialang.org/t/how-to-optimize-simple-matrix-equation-solving/23035/16 "2019-04-23T10:36:28Z")

</div>

As a fix to that `backsolve_hess!` works on rows instread of columns, the following, quite ugly, but a bit faster code seems to improve it by another 10-20%:

```julia

function backsolve_hess!(H::AbstractMatrix{T}, rhs::AbstractArray{T}) where {T}
    # Make H UpperTriangular and apply givens to rhs
    n = size(H,1)
    Ht = H'
    rotations = LinearAlgebra.Givens{T}[]
    for i = n:-1:2
        Gi, r = givens(Ht, i, i-1, i)
        push!(rotations, Gi')
        rmul!(H, Gi')
    end
    # H is now upper triangular
    ldiv!(UpperTriangular(H), rhs)
    for i in n-1:-1:1
        lmul!(rotations[i], rhs)
    end
    return rhs
end

```

```julia
@btime testfast(H, λs, b) #48.689 ms (2017 allocations: 654.73 KiB)
@btime testfast2(H, λs, b) # 40.799 ms (9017 allocations: 8.65 MiB)

```

some work can probably be done on the allocations, and proper testing of this new code is probably needed.

---

_[View the full topic](https://discourse.julialang.org/t/how-to-optimize-simple-matrix-equation-solving/23035)._
