Broadcasting with vectors of static vectors

Broadcasting works only on the outermost shape of each and every argument. As you’ve written it, you’re attempting to broadcast a 5-element vector and a 3-element vector.

Now, what you want is for the 3-element vector to behave like a “scalar” — that is, broadcast should repeat it for each element of the 5-element vector. To do that, you can “protect” it inside a 1-element container like a tuple:

julia> a .+ (Vec(2,2,2),)
5-element Array{SArray{Tuple{3},Float64,1,3},1}:
 [3.0, 3.0, 3.0]
 [4.0, 4.0, 4.0]
 [5.0, 5.0, 5.0]
 [6.0, 6.0, 6.0]
 [7.0, 7.0, 7.0]

Note that this doesn’t really have anything to do with StaticArrays — except for the fact that you might be slightly more likely to think of a static vector as a “scalar-like” thing unto itself.

8 Likes