Convert Vector{SVector} to Matrix

For example, consider

julia> a = [SVector(1,2,3), SVector(4,5,6)]
2-element Vector{SVector{3, Int64}}:
 [1, 2, 3]
 [4, 5, 6]

julia> A = reinterpret(reshape, Int64, a)
3×2 reinterpret(reshape, Int64, ::Vector{SVector{3, Int64}}) with eltype Int64:
 1  4
 2  5
 3  6

Here, if I set A[1,1] = 0, then a also changes:

julia> A[1,1] = 0
0

julia> a
2-element Vector{SVector{3, Int64}}:
 [0, 2, 3]
 [4, 5, 6]

To me, replacing A[1,1] seems equivalent to replacing the first entry to a[1]. Because a[1] is SVector, being able to replace its first entry seems strange to me.

But you say that replacing A[1,1] is actually equivalent to replacing the entire vector a[1]. I am not sure how replacing one element A[1,1] could be interpreted as replacing the entire a[1] with three entries. Still, I think you are correct, because if we make a an SVector of SVectors, we cannot replace A[1,1]:

julia> a = SVector(SVector(1,2,3), SVector(4,5,6))
2-element SVector{2, SVector{3, Int64}} with indices SOneTo(2):
 [1, 2, 3]
 [4, 5, 6]

julia> A = reinterpret(reshape, Int64, a)
3×2 reinterpret(reshape, Int64, ::SVector{2, SVector{3, Int64}}) with eltype Int64 with indices Base.OneTo(3)×SOneTo(2):
 1  4
 2  5
 3  6

julia> A[1,1] = 0
ERROR: setindex!(::SVector{2, SVector{3, Int64}}, value, ::Int) is not defined.
 Hint: Use `MArray` or `SizedArray` to create a mutable static array
Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:35
 [2] setindex!(a::SVector{2, SVector{3, Int64}}, value::SVector{3, Int64}, i::Int64)
   @ StaticArrays ~/Programming/StaticArrays/src/indexing.jl:3
 [3] _setindex_ra!
   @ ./reinterpretarray.jl:648 [inlined]
 [4] setindex!(::Base.ReinterpretArray{Int64, 2, SVector{3, Int64}, SVector{2, SVector{3, Int64}}, true}, ::Int64, ::Int64, ::Int64)
   @ Base ./reinterpretarray.jl:507
 [5] top-level scope
   @ REPL[33]:1
1 Like