I would like to calculate A \ b where A is NxN, b is a vector of SVectors that is viewed as a matrix. I want the result to be a Vector{SVector} analogously.
MWE for setup:
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:
using HybridArrays
B = HybridArray{Tuple{M,StaticArrays.Dynamic()}}(reduce(hcat, b))
B / A
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.
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
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.