# Why can't we resize a reinterpreted matrix?

**URL:** <https://discourse.julialang.org/t/why-cant-we-resize-a-reinterpreted-matrix/84780>\
**Category:** General Usage\
**Created:** [July 26, 2022, 12:23am UTC](https://discourse.julialang.org/t/why-cant-we-resize-a-reinterpreted-matrix/84780 "2022-07-26T00:23:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [July 26, 2022, 12:23am UTC](https://discourse.julialang.org/t/why-cant-we-resize-a-reinterpreted-matrix/84780/1 "2022-07-26T00:23:23Z")

</div>

We can resize a vector of static arrays:

```julia
julia> v = [rand(SVector{3,Float64}) for _ in 1:2];

julia> resize!(v, 3)
3-element Vector{SVector{3, Float64}}:
 [0.49021421932808495, 0.04375803657360211, 0.6957281082155673]
 [0.08420472085130659, 0.6281088019727706, 0.004126731413303553]
 [0.0, 0.0, 0.0]

```

AFAIU, a (3,N) matrix has the same memory layout, thus, effectively, we can reinterpret such a matrix as a vector of SVectors:

```julia
julia> x = rand(3,2)
3×2 Matrix{Float64}:
 0.948469 0.260338
 0.773314 0.616487
 0.0618051 0.279425

julia> y = reinterpret(reshape, SVector{3,Float64}, x)
2-element reinterpret(reshape, SVector{3, Float64}, ::Matrix{Float64}) with eltype SVector{3, Float64}:
 [0.948469068098922, 0.7733139138943798, 0.06180507432707616]
 [0.2603384894132955, 0.6164869874966521, 0.2794245241939306]

```

Why can’t we `resize!` that resulting reinterpreted vector of SVectors? (or, similary, why can’t we add or remove columns from a matrix?). Or, if there is intrinsic reasons for that not being possible, why _can_ we `resize!` the vector of SVectors in the first place?

---

<div class="post-metadata">

**Author:** ![ranocha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ranocha/32/35588_2.png) [@ranocha](https://discourse.julialang.org/u/ranocha)\
**Post date:** [July 26, 2022, 7:16am UTC](https://discourse.julialang.org/t/why-cant-we-resize-a-reinterpreted-matrix/84780/2 "2022-07-26T07:16:25Z")

</div>

> [@lmiq](#):
>
> Why can’t we `resize!` that resulting reinterpreted vector of SVectors? (or, similary, why can’t we add or remove columns from a matrix?).

I asked a similar question some time ago, see [resize! for multidimensional arrays · Issue #37900 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/37900)

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [July 26, 2022, 9:17am UTC](https://discourse.julialang.org/t/why-cant-we-resize-a-reinterpreted-matrix/84780/3 "2022-07-26T09:17:10Z")

</div>

Even if the parent array were a `Vector`, you couldn’t resize the `reinterpret`ed array. `reinterpret` made a `Base.ReinterpretArray`, a wrapper struct with a `.parent` field referencing the parent array, so it doesn’t actually own its data. If you want to resize it, you have to change the parent array in 1 of 2 unsavory ways:

1. Resize the parent array implicitly, and I can’t justify mutating any instance implicitly.
2. Append data to the underlying buffer just for the `ReinterpretArray`, but now you have to disallow resizing the parent array or else you would corrupt the data belonging to the `ReinterpretArray`.

That’s not to say that you’re restricted from doing all unsafe things with shared buffers; you can [mutate the parent array in ways that changes the wrappers](https://discourse.julialang.org/t/push-to-reshape-d-vector-unshares-data-with-parent-array/84664/9). It’s just in this case your parent array is not 1-dimensional, so you can’t resize it. The linked github issue in the last comment ended on a question I really want answered, though; I had always assumed they drew the resizability line at vectors because appending data for N-1 dimensions along 1 specific dimension would be too large and inflexible a behavior, but it reads like there’s a whole other dimension to the issue.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [July 26, 2022, 10:22am UTC](https://discourse.julialang.org/t/why-cant-we-resize-a-reinterpreted-matrix/84780/4 "2022-07-26T10:22:06Z")

</div>

> [@Benny](#):
>
> The linked github issue in the last comment ended on a question I really want answered

Effectively, that would answer it all.
