# Multiplication, sum and Cholesky decomposition of sparse matrices

**URL:** <https://discourse.julialang.org/t/multiplication-sum-and-cholesky-decomposition-of-sparse-matrices/95196>\
**Category:** Performance\
**Created:** [February 25, 2023, 6:06pm UTC](https://discourse.julialang.org/t/multiplication-sum-and-cholesky-decomposition-of-sparse-matrices/95196 "2023-02-25T18:06:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![andresdrenik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andresdrenik/32/35777_2.png) [@andresdrenik](https://discourse.julialang.org/u/andresdrenik)\
**Post date:** [February 25, 2023, 6:06pm UTC](https://discourse.julialang.org/t/multiplication-sum-and-cholesky-decomposition-of-sparse-matrices/95196/1 "2023-02-25T18:06:43Z")

</div>

I’m trying to solve a system of differential equations involving variational inequalities. I’m currently using the LCP solver in LCPsolve.jl. I’ve profiled my code and found that the bottleneck is in lines 216-219 [here](https://github.com/junyuan-chen/LCPsolve.jl/blob/9bac1e787a5d5de763d51f0214db7fd0c2cf374a/src/solver.jl#L216-L219). More specifically, the profiler highlights the multiplication and sum in:

```
H = Jk'*Jk + μ*I

```

where `Jk` is a sparse matrix with size that varies within the `for` loop (but always smaller than 150x150) and

```
typeof(Jk) = SparseMatrixCSC{Float64, Int64}
typeof(μ) = Float64

```

It also seems that those steps also allocate memory. The other operation highlighted by the profiler is

```
-(cholesky(H)\Jphi)

```

where

```
typeof(H) = SparseMatrixCSC{Float64, Int64}
typeof(Jphi) = Vector{Float64}

```

Is there a way to improve the performance and/or memory allocation of those steps? Thank you!

---

<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:** [February 25, 2023, 11:02pm UTC](https://discourse.julialang.org/t/multiplication-sum-and-cholesky-decomposition-of-sparse-matrices/95196/2 "2023-02-25T23:02:30Z")

</div>

> [@andresdrenik](#):
>
> where `Jk` is a sparse matrix with size that varies within the `for` loop (but always smaller than 150x150)

For such a tiny matrix, you might be better off with dense-matrix operations. Does `Jk` have an particular sparsity pattern?

---

<div class="post-metadata">

**Author:** ![andresdrenik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andresdrenik/32/35777_2.png) [@andresdrenik](https://discourse.julialang.org/u/andresdrenik)\
**Post date:** [February 27, 2023, 4:30pm UTC](https://discourse.julialang.org/t/multiplication-sum-and-cholesky-decomposition-of-sparse-matrices/95196/3 "2023-02-27T16:30:55Z")

</div>

It is either a tridiagonal matrix or it looks like this:

````julia
⠻⣦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⠀⠀⠀⠀⠀⠀
⠀⠈⠻⣦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠈⠻⣦⡀⠀⠀⠀⠀⠀⠀⠀⠀⢸⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠈⠻⣦⡀⠀⠀⠀⠀⠀⠀⠈⡆⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠈⠻⣦⡀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠻⣦⡀⠀⠀⠀⡇⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠻⣦⡀⠀⡇⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠻⣦⣹⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢻⣦⡀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡏⠈⠻⣦⡀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡼⠁⠀⠀⠈⠻⣦```
````

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [February 27, 2023, 5:30pm UTC](https://discourse.julialang.org/t/multiplication-sum-and-cholesky-decomposition-of-sparse-matrices/95196/4 "2023-02-27T17:30:26Z")

</div>

This is the same as [julia - Multiplication, sum and Cholesky decomposition of sparse matrices - Stack Overflow](https://stackoverflow.com/questions/75558671/multiplication-sum-and-cholesky-decomposition-of-sparse-matrices), right?

---

<div class="post-metadata">

**Author:** ![andresdrenik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andresdrenik/32/35777_2.png) [@andresdrenik](https://discourse.julialang.org/u/andresdrenik)\
**Post date:** [February 27, 2023, 8:10pm UTC](https://discourse.julialang.org/t/multiplication-sum-and-cholesky-decomposition-of-sparse-matrices/95196/5 "2023-02-27T20:10:38Z")

</div>

Yes it is.
