# Implementation for accessing Cholesky factors

**URL:** <https://discourse.julialang.org/t/implementation-for-accessing-cholesky-factors/56219>\
**Category:** Numerics\
**Tags:** linearalgebra\
**Created:** [February 28, 2021, 11:34pm UTC](https://discourse.julialang.org/t/implementation-for-accessing-cholesky-factors/56219 "2021-02-28T23:34:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ksil](https://avatars.discourse-cdn.com/v4/letter/k/d6d6ee/32.png) [@ksil](https://discourse.julialang.org/u/ksil)\
**Post date:** [February 28, 2021, 11:34pm UTC](https://discourse.julialang.org/t/implementation-for-accessing-cholesky-factors/56219/1 "2021-02-28T23:34:59Z")

</div>

Hi everyone!

I’ve been a Julia user for several years, but this is my first time posting here, so apologies if this has been asked somewhere before. I was curious about Julia’s implementation of Cholesky factorization — specifically accessing and using the factors of the Cholesky data structure.

Say I have some (positive definite) matrix A = L L^T, I perform the Cholesky factorization as  
`C = cholesky(A)`, and I want to use L for some calculations (e.g., a matrix-vector multiplication). I realized recently that, given some vector `v`,

```julia
C.L*v

```

is quite a bit slower than

```julia
C.U'*v

```

because `C.L` actually forms a copy of the underlying `factors` matrix every time it’s called (and because storage of the factor in the upper part is the default behavior). The relevant code in `cholesky.jl` is:

```julia
if d === :U
    return UpperTriangular(Cuplo === char_uplo(d) ? Cfactors : copy(Cfactors'))
elseif d === :L
    return LowerTriangular(Cuplo === char_uplo(d) ? Cfactors : copy(Cfactors'))
elseif d === :UL
    return (Cuplo === 'U' ? UpperTriangular(Cfactors) : LowerTriangular(Cfactors))
else

```

I could imagine that many a user might naively call `C.L` without realizing the significant performance hit, so I’m curious: why not return something like `LowerTriangular(Cfactors')` instead of `LowerTriangular(copy(Cfactors'))` (and similarly for `C.U` if the factor is stored in the lower part)? Of course, there’s a difference in meaning by returning an adjoint, and maybe something would break (I can’t think of anything at the moment) for an `AbstractMatrix` data structure that’s not just a simple array of floats. But for most use cases (e.g., multiplication, solves, changing coefficients, accessing coefficients, etc.), I would imagine this is advantageous. Are there some deep reasons why this choice of copying was made?

Thanks 🙂!

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [March 1, 2021, 11:13am UTC](https://discourse.julialang.org/t/implementation-for-accessing-cholesky-factors/56219/2 "2021-03-01T11:13:09Z")

</div>

Please submit a PR. I did a similar change for QR

[https://github.com/JuliaLang/julia/pull/38389](https://github.com/JuliaLang/julia/pull/38389)

---

<div class="post-metadata">

**Author:** ![ksil](https://avatars.discourse-cdn.com/v4/letter/k/d6d6ee/32.png) [@ksil](https://discourse.julialang.org/u/ksil)\
**Post date:** [March 2, 2021, 3:53am UTC](https://discourse.julialang.org/t/implementation-for-accessing-cholesky-factors/56219/3 "2021-03-02T03:53:42Z")

</div>

Thanks, I’ll do that.
