# In place lufact for sparse matrices and prellocation for lufact output

**URL:** <https://discourse.julialang.org/t/in-place-lufact-for-sparse-matrices-and-prellocation-for-lufact-output/6090>\
**Category:** Numerics\
**Tags:** performance, linearalgebra\
**Created:** [September 26, 2017, 5:22pm UTC](https://discourse.julialang.org/t/in-place-lufact-for-sparse-matrices-and-prellocation-for-lufact-output/6090 "2017-09-26T17:22:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tryingtosolve](https://avatars.discourse-cdn.com/v4/letter/t/bbe5ce/32.png) [@tryingtosolve](https://discourse.julialang.org/u/tryingtosolve)\
**Post date:** [September 26, 2017, 5:22pm UTC](https://discourse.julialang.org/t/in-place-lufact-for-sparse-matrices-and-prellocation-for-lufact-output/6090/1 "2017-09-26T17:22:49Z")

</div>

Currently there is a `lufact!` for dense matrices. Is there an equivalent of `lufact!` for sparse matrices?

Also, related to that, how do I preallocate for the output of `lufact(A)`, assuming that `A` is a sparse matrix? This is what I have tried:

```
S = lufact(speye(5))
S .= lufact(sparse(rand(5,5)))

MethodError: no method matching broadcast!(::Base.#identity, 
::Base.SparseArrays.UMFPACK.UmfpackLU{Float64,Int64}, ::Base.SparseArrays.UMFPACK.UmfpackLU{Float64,Int64})

```

My code has a loop that solves `Ax=b` where `A` stays constant but `b` changes every time during the loop. I think it’d help if I store the output of `lufact(A)` in terms of memory allocations (be it through preallocation or in-place operation).

---

<div class="post-metadata">

**Author:** ![andreasnoack](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andreasnoack/32/27_2.png) [@andreasnoack](https://discourse.julialang.org/u/andreasnoack)\
**Post date:** [September 26, 2017, 5:41pm UTC](https://discourse.julialang.org/t/in-place-lufact-for-sparse-matrices-and-prellocation-for-lufact-output/6090/2 "2017-09-26T17:41:25Z")

</div>

Sparse factorizations are quite different so it is not possible to preallocate the memory. In short, the reason is that for sparse factorizations you don’t know how much memory you’ll need before you compute the factorization. Factorizing the matrix before the loop is still a good idea, though, so just compute `F = lufact(Asparse)` outside the loop.
