How to create an SVector of SVectors?

The following does not work:

using StaticArrays

pos=SVector{3, SVector{3,Float64}}(zeros(3,3))

Any idea?

julia> pos = zeros(SVector{3,SVector{3,Float64}})
3-element SVector{3, SVector{3, Float64}} with indices SOneTo(3):
 [0.0, 0.0, 0.0]
 [0.0, 0.0, 0.0]
 [0.0, 0.0, 0.0]
2 Likes

The other possibility is that you want an SMatrix{3,3,Float64}.

1 Like

What would be the advantages/ disadvantages of an

(SVector{3,SVector{3,Float64}})

compared to a

SMatrix{3,3,Float64}
?

Probably I need:

(SVector{3,MVector{3,Float64}})

actually…

Probably you need either a static matrix or a vector of static vectors (Vector{SVector{}}). But of course more information would be needed to discuss that.

The range of uses of mutable-static matrices and vectors is somewhat limited (surprisingly). A vector of static vector can be mutated like this:

julia> x = [ zeros(SVector{3,Float64}) for i in 1:3 ]
3-element Vector{SVector{3, Float64}}:
 [0.0, 0.0, 0.0]
 [0.0, 0.0, 0.0]
 [0.0, 0.0, 0.0]

julia> x[1] = rand(SVector{3,Float64})
3-element SVector{3, Float64} with indices SOneTo(3):
 0.7361897874026004
 0.9443149525219294
 0.6846526276759228

julia> x
3-element Vector{SVector{3, Float64}}:
 [0.7361897874026004, 0.9443149525219294, 0.6846526276759228]
 [0.0, 0.0, 0.0]
 [0.0, 0.0, 0.0]

This is very efficient, and even if you want to mutate one of the elements of x[1], doing:

julia> x[1] = SVector(x[1][1], x[1][2], 0.)
3-element SVector{3, Float64} with indices SOneTo(3):
 0.7361897874026004
 0.9443149525219294
 0.0

This is still better than keeping x[1] as a mutable vector all the time. Also, there is the Setfield package to make that easier:

julia> using Setfield

julia> @set! x[1][1] = 5.
3-element Vector{SVector{3, Float64}}:
 [5.0, 0.9443149525219294, 0.0]
 [0.0, 0.0, 0.0]
 [0.0, 0.0, 0.0]

1 Like