Is it posible to have MVectors with overlaping data?

I am defining a type which contains a 6-element MVector. I would like to have to views into each half of this vector, like so:

Of course, I could create them using view1 = view(myvector, 1:3) and view2 = view(myvector, 4:6), but this creates SubArrays, which are not statically sized and end up creating non-static vectors when used in LinearAlgebra operations.

If I where using Svectors instead of MVectors, I could solve this with reinterpret; but it doesn’t work in this case beacuse MVectors are not isbits types.

I summary, my requirements are:

  • Define a structure with fields myvector, view1, and view2.
  • view1 and view2 must use the same underlying data as myvector, so that changes in any of the six elements are automatically seen in both fields that reference that element.
  • myvector, view1, and view2 must all be static arrays, so that operations with them also produce static arrays.

Can this be done?

2 Likes
julia> my_vector = @MArray [10,20,30,40,50,60]
6-element MVector{6, Int64} with indices SOneTo(6):
 10
 20
 30
 40
 50
 60

julia> view1 = SizedArray{Tuple{3}}(@view(my_vector[1:3]))
3-element SizedVector{3, Int64, SubArray{Int64, 1, MVector{6, Int64}, Tuple{UnitRange{Int64}}, true}} with indices SOneTo(3):
 10
 20
 30

julia> view2 = SizedArray{Tuple{3}}(@view(my_vector[4:6]))
3-element SizedVector{3, Int64, SubArray{Int64, 1, MVector{6, Int64}, Tuple{UnitRange{Int64}}, true}} with indices SOneTo(3):
 40
 50
 60

julia> my_vector[1]=-10
-10

julia> view1
3-element SizedVector{3, Int64, SubArray{Int64, 1, MVector{6, Int64}, Tuple{UnitRange{Int64}}, true}} with indices SOneTo(3):
 -10
  20
  30
6 Likes

Thanks for the answer, I guess this is as good as we can get… but I am not convinced this meets my requirements because linalg operations with these views give SizedVectors instead of MVectors:

julia> typeof( view1 × view2 )
SizedVector{3, Int64, Vector{Int64}} (alias for SizedArray{Tuple{3}, Int64, 1, 1, Array{Int64, 1}})

Is there a performance difference between a SizedVector{3, Int64, Vector{Int64}} and an MVector{3, Int64}?