# A \\ b as Vector{SVector} when b is static along one dimension

**URL:** <https://discourse.julialang.org/t/a-b-as-vector-svector-when-b-is-static-along-one-dimension/114606>\
**Category:** General Usage\
**Created:** [May 23, 2024, 5:51am UTC](https://discourse.julialang.org/t/a-b-as-vector-svector-when-b-is-static-along-one-dimension/114606 "2024-05-23T05:51:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 23, 2024, 5:51am UTC](https://discourse.julialang.org/t/a-b-as-vector-svector-when-b-is-static-along-one-dimension/114606/1 "2024-05-23T05:51:39Z")

</div>

I would like to calculate `A \ b` where `A` is NxN, `b` is a vector of `SVector`s that is viewed as a matrix. I want the result to be a `Vector{SVector}` analogously.

MWE for setup:

```julia
using LinearAlgebra, StaticArrays
M = 3
N = 10
b = [rand(SVector{M}) for _ in 1:N];
A = rand(N, N);

```

I tried HybridArrays, but it gives me a `Matrix`:

```julia
using HybridArrays
B = HybridArray{Tuple{M,StaticArrays.Dynamic()}}(reduce(hcat, b))
B / A

```

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [May 23, 2024, 8:03am UTC](https://discourse.julialang.org/t/a-b-as-vector-svector-when-b-is-static-along-one-dimension/114606/2 "2024-05-23T08:03:18Z")

</div>

I think you need `A` to be an SMatrix (i.e. a matrix with compile-time known size):

```julia
julia> AA = SMatrix{10,10}(A)

julia> c = AA \ b;

```

---

<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:** [May 23, 2024, 2:00pm UTC](https://discourse.julialang.org/t/a-b-as-vector-svector-when-b-is-static-along-one-dimension/114606/3 "2024-05-23T14:00:41Z")

</div>

In your title you talk about `\` but your example uses `/`. I’m going to assume you want `\(::Matrix, ::Vector{<:SVector})`, but you can re-work this for alternative arrangements.

`Vector{<:SVector}` and `Matrix` have identical memory layouts, so you can simply `reinterpret` between them for next-to-no cost.

```julia
b = [rand(SVector{N, Float64}) for _ in 1:M]
B = reinterpret(reshape, Float64, b) # reinterpret vector-of-vectors as matrix
b1 = reinterpret(reshape, SVector{N, Float64}, B) # reinterpret matrix as vector-of-vectors

```

As for solves, here are some options

```julia
A \ B # reinterpret to vector-of-vectors if desired
Ref(factorize(A)) .\ b # solve each vector individually using broadcast (pre-factorize to avoid repeated work)
Ref(A) .\ b # NOT RECOMMENDED # re-factorizes A for each solve so needlessly expensive
facA = factorize(A); [facA \ bi for bi in b] # use a comprehension instead of broadcast

```

Note that it matters _which_ dimension of `b` is static. When it is the outer dimension (the one not necessarily shared with `A`), such as in your written example, you can’t solve for individual elements of `b`. In this sense, the matrix-and-reinterpret version is the most flexible. It’s also probably the most performant, at least if `A` is not small and statically sized.
