# How to preallocate a static array for use with \`lu!\`

**URL:** <https://discourse.julialang.org/t/how-to-preallocate-a-static-array-for-use-with-lu/89712>\
**Category:** General Usage\
**Tags:** staticarrays\
**Created:** [November 3, 2022, 1:38pm UTC](https://discourse.julialang.org/t/how-to-preallocate-a-static-array-for-use-with-lu/89712 "2022-11-03T13:38:23Z")\
**Posts on this page:** 5\
**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, 1:38pm UTC](https://discourse.julialang.org/t/how-to-preallocate-a-static-array-for-use-with-lu/89712/1 "2022-11-03T13:38:23Z")

</div>

If you call `lu` on an `SMatrix`, you get a static `LU` factorization—cool:

```julia
using StaticArrays
using LinearAlgebra

A = SMatrix{3, 3}(rand(3, 3))
lu(A)
# StaticArrays.LU
# L factor:
# 3×3 LowerTriangular{Float64, SMatrix{3, 3, Float64, 9}}:
# 1.0 ⋅ ⋅ 
# 0.706739 1.0 ⋅ 
# 0.648963 0.100059 1.0
# U factor:
# 3×3 UpperTriangular{Float64, SMatrix{3, 3, Float64, 9}}:
# 0.807758 0.146236 0.681894
# ⋅ 0.76478 0.338519
# ⋅ ⋅ 0.412485

```

But sometimes, you want to preallocate space for the L and U factors. In this case, I would like L to be a `LowerTriangular` based on an `MMatrix` (needs to be mutable, obviously). How can I initialize an empty such LU factorization to be used as follows?

```julia
lu!(what_goes_here, A)

```

My attempt:

```julia
my_LU = StaticArrays.LU(
    LowerTriangular{Float64, zeros(MMatrix{3, 3, Float64})},
    UpperTriangular{Float64, zeros(MMatrix{3, 3, Float64})}
)
lu!(my_LU, A)
# TypeError: in Type, in parameter, expected Type, got a value of type MMatrix{3, 3, Float64, 9}

```

(And why isn’t `MMatrix` a subtype of `AbstractMatrix`?)

---

<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, 1:45pm UTC](https://discourse.julialang.org/t/how-to-preallocate-a-static-array-for-use-with-lu/89712/2 "2022-11-03T13:45:21Z")

</div>

> [@maxkapur](#):
>
> But sometimes, you want to preallocate space for the L and U factors.

Preallocation doesn’t make sense for `SMatrix` — there is no “allocation” cost in the first place. So what is your motivation?

---

<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, 1:49pm UTC](https://discourse.julialang.org/t/how-to-preallocate-a-static-array-for-use-with-lu/89712/3 "2022-11-03T13:49:52Z")

</div>

For small matrices, won’t `my_LU \ b` benefit from having `my_LU` as an `SArray` for the same reason that `A \ b` does? (Or is the latter assumption also incorrect?)

---

<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:02pm UTC](https://discourse.julialang.org/t/how-to-preallocate-a-static-array-for-use-with-lu/89712/4 "2022-11-03T14:02:01Z")

</div>

`lu(smatrix)` already returns L and U factors that are backed by `SMatrix`. What more do you want?

(Note that StaticArrays already has [optimized methods](https://github.com/JuliaArrays/StaticArrays.jl/blob/a7ba6a2192351531a7535fa8b68aa389d54efaff/src/triangular.jl) for triangular solves with `SMatrix` storage.)

---

<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:28pm UTC](https://discourse.julialang.org/t/how-to-preallocate-a-static-array-for-use-with-lu/89712/5 "2022-11-03T14:28:51Z")

</div>

2 posts were split to a new topic: [How to preallocate LU factorization](https://discourse.julialang.org/t/how-to-preallocate-lu-factorization/89718)
