# How to preallocate LU factorization

**URL:** <https://discourse.julialang.org/t/how-to-preallocate-lu-factorization/89718>\
**Category:** Numerics\
**Tags:** linearalgebra\
**Created:** [November 3, 2022, 2:07pm UTC](https://discourse.julialang.org/t/how-to-preallocate-lu-factorization/89718 "2022-11-03T14:07:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![maxkapur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxkapur/32/21208_2.png) [@maxkapur](https://discourse.julialang.org/u/maxkapur)\
**Post date:** [November 3, 2022, 2:07pm UTC](https://discourse.julialang.org/t/how-to-preallocate-lu-factorization/89718/1 "2022-11-03T14:07:50Z")

</div>

OK, I see what you mean.

Suppose, then, that I’m not using StaticArrays and just want to allocate an empty LU with normal Julia arrays—how do I do that?

```julia
zeros(LU{Float64, zeros(Matrix{Float64}), zeros(Vector{Int64})})

```

is an error. (I realize this is a different question.)

Edit: Kind of hacky:

```julia
LU(zeros(3,3), [1, 2, 3], 0)

```

---

<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:** [November 3, 2022, 2:26pm UTC](https://discourse.julialang.org/t/how-to-preallocate-lu-factorization/89718/2 "2022-11-03T14:26:56Z")

</div>

> [@maxkapur](#):
>
> Suppose, then, that I’m not using StaticArrays and just want to allocate an empty LU with normal Julia arrays—how do I do that?

The easiest thing is to just do `F = lu(rand(n,n))`. The only reason to pre-allocate is if you are going to re-use the factorization object many times, in which case doing one extra factorization at the beginning has negligible cost.

---

<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:** [November 3, 2022, 3:02pm UTC](https://discourse.julialang.org/t/how-to-preallocate-lu-factorization/89718/3 "2022-11-03T15:02:03Z")

</div>

I don’t think Julia exposes any way to preallocate the permutation vector. Perhaps you already knew this, but the space for the `L` and `U` outputs can be “preallocated” by using the `lu!` function. `lu!` uses the input array’s storage to store the `L` and `U` components. However, the original input matrix is mutilated in the process and, further, its memory is now shared with `L` and `U` so can’t be used for other purposes until you’re done with those.  
This can be seen in this demonstration

```julia-repl
julia> x = reshape(1.0:9,3,3).^2
3×3 Matrix{Float64}:
 1.0 16.0 49.0
 4.0 25.0 64.0
 9.0 36.0 81.0

julia> lu!(x)
LU{Float64, Matrix{Float64}, Vector{Int64}}
L factor:
3×3 Matrix{Float64}:
 1.0 0.0 0.0
 0.111111 1.0 0.0
 0.444444 0.75 1.0
U factor:
3×3 Matrix{Float64}:
 9.0 36.0 81.0
 0.0 12.0 40.0
 0.0 0.0 -2.0

julia> x # L in the lower triangle and U in the upper 
3×3 Matrix{Float64}:
 9.0 36.0 81.0
 0.111111 12.0 40.0
 0.444444 0.75 -2.0

```

There are many `___!` functions in `LinearAlgebra` that follow this same pattern.

---

<div class="post-metadata">

**Author:** ![RoyiAvital](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/royiavital/32/571_2.png) [@RoyiAvital](https://discourse.julialang.org/u/RoyiAvital)\
**Post date:** [November 3, 2022, 5:59pm UTC](https://discourse.julialang.org/t/how-to-preallocate-lu-factorization/89718/4 "2022-11-03T17:59:09Z")

</div>

You may think of the cases you want to build a library based on Julia code and one of the requirements is to be non allocating code.

I can think of many more case the option to have API which exposes all memory allocations is useable.
