Skip to content

Should reshaping a SubArray produce another SubArray? #9874

@johnmyleswhite

Description

@johnmyleswhite

Right now, calling reshape on a SubArray allocates memory, which means that reshaping a slice causes you to lose your view into the original data. See the example below for the kinds of situations you might find yourself in.

julia> a = ones(9)
9-element Array{Float64,1}:
 1.0
 1.0
 1.0
 1.0
 1.0
 1.0
 1.0
 1.0
 1.0

julia> b = reshape(a, 3, 3)
3x3 Array{Float64,2}:
 1.0  1.0  1.0
 1.0  1.0  1.0
 1.0  1.0  1.0

julia> b[3, 3] = 9.0
9.0

julia> a
9-element Array{Float64,1}:
 1.0
 1.0
 1.0
 1.0
 1.0
 1.0
 1.0
 1.0
 9.0

julia> c = slice(a, 4:9)
6-element SubArray{Float64,1,Array{Float64,1},(UnitRange{Int64},),1}:
 1.0
 1.0
 1.0
 1.0
 1.0
 9.0

julia> c[1] = 4.0
4.0

julia> a
9-element Array{Float64,1}:
 1.0
 1.0
 1.0
 4.0
 1.0
 1.0
 1.0
 1.0
 9.0

julia> d = reshape(c, 3, 2)
3x2 Array{Float64,2}:
 4.0  1.0
 1.0  1.0
 1.0  9.0

julia> d[2, 1] = 5.0
5.0

julia> a
9-element Array{Float64,1}:
 1.0
 1.0
 1.0
 4.0
 1.0
 1.0
 1.0
 1.0
 9.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs decisionA decision on this change is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions