# Is it posible to have MVectors with overlaping data?

**URL:** <https://discourse.julialang.org/t/is-it-posible-to-have-mvectors-with-overlaping-data/110275>\
**Category:** General Usage\
**Tags:** memory, staticarrays\
**Created:** [February 16, 2024, 12:11am UTC](https://discourse.julialang.org/t/is-it-posible-to-have-mvectors-with-overlaping-data/110275 "2024-02-16T00:11:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joaquin\_Rodriguez](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joaquin_rodriguez/32/202978_2.png) [@Joaquin\_Rodriguez](https://discourse.julialang.org/u/Joaquin_Rodriguez)\
**Post date:** [February 16, 2024, 12:11am UTC](https://discourse.julialang.org/t/is-it-posible-to-have-mvectors-with-overlaping-data/110275/1 "2024-02-16T00:11:07Z")

</div>

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:

 ![Screenshot from 2024-02-15 20-52-02](https://global.discourse-cdn.com/julialang/original/3X/3/7/37cfc6dd7c3b1cb6edfab8587287380eed78a522.png)

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 `Svector`s instead of `MVector`s, I could solve this with `reinterpret`; but it doesn’t work in this case beacuse `MVector`s 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?

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [February 16, 2024, 3:59am UTC](https://discourse.julialang.org/t/is-it-posible-to-have-mvectors-with-overlaping-data/110275/2 "2024-02-16T03:59:44Z")

</div>

```julia-repl
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

```

---

<div class="post-metadata">

**Author:** ![Joaquin\_Rodriguez](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joaquin_rodriguez/32/202978_2.png) [@Joaquin\_Rodriguez](https://discourse.julialang.org/u/Joaquin_Rodriguez)\
**Post date:** [February 16, 2024, 7:17pm UTC](https://discourse.julialang.org/t/is-it-posible-to-have-mvectors-with-overlaping-data/110275/3 "2024-02-16T19:17:57Z")

</div>

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 `SizedVector`s instead of `MVector`s:

```julia
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}`?
