I store velocities for particles as a Vector of SVectors. Each row is an SVector with that indices x, y, z velocity.
SVectors are great for their speed in arithmetic, but they are a bit... difficult to manipulate. How would I add up all of the x, y, z squared velocities in my Vector in a way that is more elegant than the following
using StaticArrays
n = 5
v = [SVector{3}(rand(), rand(), rand()) for i = 1:n]
x, y, z = 0.0, 0.0, 0.0
for i=1:n
x += v[i][1]^2
y += v[i][2]^2
z += v[i][3]^2
end
sumv = SVector{3}(x, y, z)
If I just wanted to sum the x, y, z in my velocities, without squaring, Julia is simple, just sum(v) will give me a vector of the summed columns.
One solution I have is
sum([v[i].^2 for i=1:n])
but there must be a simpler solution that doesn't require a comprehension?