# Static matrix equation solve in-place?

**URL:** <https://discourse.julialang.org/t/static-matrix-equation-solve-in-place/57231>\
**Category:** Specific Domains\
**Tags:** linearalgebra\
**Created:** [March 15, 2021, 7:57pm UTC](https://discourse.julialang.org/t/static-matrix-equation-solve-in-place/57231 "2021-03-15T19:57:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![markmbaum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/markmbaum/32/32745_2.png) [@markmbaum](https://discourse.julialang.org/u/markmbaum)\
**Post date:** [March 15, 2021, 7:57pm UTC](https://discourse.julialang.org/t/static-matrix-equation-solve-in-place/57231/1 "2021-03-15T19:57:41Z")

</div>

I’m using static arrays to solve a 3x3 matrix equation that has to be solved repeatedly. Is there a function for solving these equations in place with static arrays (`SMatrix`, `SVector`)? I see all the in-place BLAS routines, but I’m not sure if that will defeat the purpose of using static arrays. So far, `A\b` is considerably faster without decomposing the matrix first for this small system.

---

<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:** [March 15, 2021, 8:00pm UTC](https://discourse.julialang.org/t/static-matrix-equation-solve-in-place/57231/2 "2021-03-15T20:00:00Z")

</div>

I think what you want is `ldiv!`

---

<div class="post-metadata">

**Author:** ![markmbaum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/markmbaum/32/32745_2.png) [@markmbaum](https://discourse.julialang.org/u/markmbaum)\
**Post date:** [March 15, 2021, 8:01pm UTC](https://discourse.julialang.org/t/static-matrix-equation-solve-in-place/57231/3 "2021-03-15T20:01:05Z")

</div>

I gave that a try and got

```julia
julia> ldiv!(A, b)
ERROR: MethodError: no method matching ldiv!(::SArray{Tuple{3,3},Float64,2,9}, ::MArray{Tuple{3},Float64,1,3})

```

The docs for [`ldiv!`](https://docs.julialang.org/en/v1/stdlib/LinearAlgebra/#LinearAlgebra.ldiv!) also say " The argument `A` should _not_ be a matrix," so I’m unsure about applicability to an unfactorized static matrix.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [March 15, 2021, 8:42pm UTC](https://discourse.julialang.org/t/static-matrix-equation-solve-in-place/57231/4 "2021-03-15T20:42:08Z")

</div>

If the arrays are static, what’s the purpose of the in-place solve? I believe StaticArrays have custom methods for 3x3 solves that do not allocate anything?

---

<div class="post-metadata">

**Author:** ![markmbaum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/markmbaum/32/32745_2.png) [@markmbaum](https://discourse.julialang.org/u/markmbaum)\
**Post date:** [March 15, 2021, 8:48pm UTC](https://discourse.julialang.org/t/static-matrix-equation-solve-in-place/57231/5 "2021-03-15T20:48:11Z")

</div>

Is that the case? I could be confused, but does repeated execution of `x = A\b` somehow compile to an in-place operation with `x`? How can there be zero allocation for the result of `A\b`?

---

<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:** [March 15, 2021, 8:52pm UTC](https://discourse.julialang.org/t/static-matrix-equation-solve-in-place/57231/6 "2021-03-15T20:52:34Z")

</div>

The cool thing with `StaticArray`s is that non-inplace methods don’t have to allocate (on the heap) since you know exactly how much memory it will need.

---

<div class="post-metadata">

**Author:** ![markmbaum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/markmbaum/32/32745_2.png) [@markmbaum](https://discourse.julialang.org/u/markmbaum)\
**Post date:** [March 15, 2021, 10:07pm UTC](https://discourse.julialang.org/t/static-matrix-equation-solve-in-place/57231/7 "2021-03-15T22:07:01Z")

</div>

Ok, so there is no reason to fret about where the result of `A\b` goes, because it’s basically just a tuple and will not cause any slowdown?

---

<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:** [March 15, 2021, 10:38pm UTC](https://discourse.julialang.org/t/static-matrix-equation-solve-in-place/57231/8 "2021-03-15T22:38:16Z")

</div>

> [@markmbaum](#):
>
> Ok, so there is no reason to fret about where the result of `A\b` goes, because it’s basically just a tuple and will not cause any slowdown?

Right.
