Strange case of permutedims() preventing further growth of its input

Most probably it is a feature, not a bug, but the following behavior is a surprising inconvenience for me. I’d appreciate if some expert would care to comment or suggest a workaround.

julia> a = [1,2,3]
3-element Vector{Int64}:
 1
 2
 3

julia> b = permutedims(a)
1×3 Matrix{Int64}:
 1  2  3

julia> push!(a, 4)
ERROR: cannot resize array with shared data
Stacktrace:
 [1] _growend!
   @ ./array.jl:884 [inlined]
 [2] push!(a::Vector{Int64}, item::Int64)
   @ Base ./array.jl:929
 [3] top-level scope
   @ REPL[8]:1

Related:

I think there’s another issue too but I can’t seem to find it right now. Does this workaround work for your case?

julia> a = [1,2,3]
3-element Vector{Int64}:
 1
 2
 3

julia> b = permutedims(copy(a))
1×3 Matrix{Int64}:
 1  2  3

julia> push!(a, 4)
4-element Vector{Int64}:
 1
 2
 3
 4

Thanks! It’s amazing how reactive the Julia community is.

2 Likes

It’s a bit strange (although documented) that the data is shared for a vector but not for arrays of other dimensionalities

Good point. I was not aware of that.