Is there a convenient and efficient way of slicing StaticArrays
, or let’s say, dropping parts of an SArray
?
julia> vs = SVector{4}(rand(4))
4-element SVector{4,Float64}:
0.580171
0.719607
0.651051
0.430415
julia> vs[1:3]
3-element Array{Float64,1}:
0.580171
0.719607
0.651051
Slicing like this works, but returns an ordinary Array
and isn’t particularly fast:
julia> @btime $vs[1:3]
52.049 ns (3 allocations: 192 bytes)
3-element Array{Float64,1}:
0.580171
0.719607
0.651051
julia> @btime SVector($vs[1], $vs[2], $vs[3])
2.030 ns (0 allocations: 0 bytes)
3-element SVector{3,Float64}:
0.580171
0.719607
0.651051
I get that the compiler cannot figure out the type of vs[1:3]
, but if I could tell it to "drop the last element of vs
" somehow, then it would know to the return type SVector{3}
.
I also need to do this for SVector{3}
, SMatrix{4,4}
and SMatrix{3,3}
. I can of course write several methods like this:
droplast(vs::SVector{4}) = SVector{3}(vs[1], vs[2], vs[3])
but it seems like there must be a more general approach, perhaps one that is already in use?