I was hoping that this would work for centering some data that is e.g. Array{SVector}
d = [rand(2) for i=1:10]
d - mean(d)
or even the broadcasting version
d .- mean(d)
But they don’t work. I understand why they don’t work, but I’m wondering if it would be possible to have consistent broadcasting rules that would make it work.
For now, I know I can do
d .- [mean(d)]
It is just that with scalar elements, one can write
It does work to instead use Base.RefValue but it’s unexported and undocumented, probably for a reason. Some other options, with or without broadcasting:
[x - mean(d) for x in d]
map(x -> x - mean(d), d)
(x -> x - mean(d)).(d)
Your other options all work, but are going to re-compute mean(d) for every element.
I’m also super surprised by the behavior of Ref in this case, but I guess RefArrays behave differently in broadcast? In particular, it looks like a RefArray behaves like a scalar but only using the first element of the contained array. A 1-element tuple has the behavior I would expect, as does wrapping the array in a 1-element array.