I have an array of vectors, represented as static arrays (SArray
), and want to perform arithmetic operations like addition (vec + vec), scaling (vec * vec) and so on. Obvious ways I tried turn out to behave very unintuitively:
using StaticArrays
a = SVector.([(1, 2, 3), (4, 5, 6)])
b = SVector(1, 2, 3)
a .+ 1 # OK: SArray{Tuple{3},Int64,1,3}[[2, 3, 4], [5, 6, 7]]
a .* 2 # OK: SArray{Tuple{3},Int64,1,3}[[2, 4, 6], [8, 10, 12]]
1 ./ a # Wrong: LinearAlgebra.Transpose{Float64,SArray{Tuple{3},Float64,1,3}}[[0.0714286 0.142857 0.214286], [0.0519481 0.0649351 0.0779221]]
a .+ b # DimensionMismatch("arrays could not be broadcast to a common size")
a .+ [b] # OK: SArray{Tuple{3},Int64,1,3}[[2, 4, 6], [5, 7, 9]]
a .* [b] # MethodError: no method matching *(::SArray{Tuple{3},Int64,1,3}, ::SArray{Tuple{3},Int64,1,3})
a ./ [b] # Wrong: SArray{Tuple{3,3},Float64,2,9}[[0.0714286 0.142857 0.214286; 0.142857 0.285714 0.428571; 0.214286 0.428571 0.642857], [0.285714 0.571429 0.857143; 0.357143 0.714286 1.07143; 0.428571 0.857143 1.28571]]
After reading and thinking about how it works, I can understand how these results appeared. But it still a) is far from intuitive for such a common operation, and b) I don’t see any clear way to actually perform elementwise vector arithmetics here.
Am I missing something?