{"action":"created","issue":{"url":"https://api.github.com/repos/JuliaLang/julia/issues/9874","labels_url":"https://api.github.com/repos/JuliaLang/julia/issues/9874/labels{/name}","comments_url":"https://api.github.com/repos/JuliaLang/julia/issues/9874/comments","events_url":"https://api.github.com/repos/JuliaLang/julia/issues/9874/events","html_url":"https://github.com/JuliaLang/julia/issues/9874","id":55074440,"number":9874,"title":"Should reshaping a SubArray produce another SubArray?","user":{"login":"johnmyleswhite","id":22064,"avatar_url":"https://avatars.githubusercontent.com/u/22064?v=3","gravatar_id":"","url":"https://api.github.com/users/johnmyleswhite","html_url":"https://github.com/johnmyleswhite","followers_url":"https://api.github.com/users/johnmyleswhite/followers","following_url":"https://api.github.com/users/johnmyleswhite/following{/other_user}","gists_url":"https://api.github.com/users/johnmyleswhite/gists{/gist_id}","starred_url":"https://api.github.com/users/johnmyleswhite/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/johnmyleswhite/subscriptions","organizations_url":"https://api.github.com/users/johnmyleswhite/orgs","repos_url":"https://api.github.com/users/johnmyleswhite/repos","events_url":"https://api.github.com/users/johnmyleswhite/events{/privacy}","received_events_url":"https://api.github.com/users/johnmyleswhite/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/JuliaLang/julia/labels/decision","name":"decision","color":"F08000"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":28,"created_at":"2015-01-21T20:44:42Z","updated_at":"2015-03-02T16:51:05Z","closed_at":null,"body":"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.\r\n\r\n```jl\r\njulia> a = ones(9)\r\n9-element Array{Float64,1}:\r\n 1.0\r\n 1.0\r\n 1.0\r\n 1.0\r\n 1.0\r\n 1.0\r\n 1.0\r\n 1.0\r\n 1.0\r\n\r\njulia> b = reshape(a, 3, 3)\r\n3x3 Array{Float64,2}:\r\n 1.0  1.0  1.0\r\n 1.0  1.0  1.0\r\n 1.0  1.0  1.0\r\n\r\njulia> b[3, 3] = 9.0\r\n9.0\r\n\r\njulia> a\r\n9-element Array{Float64,1}:\r\n 1.0\r\n 1.0\r\n 1.0\r\n 1.0\r\n 1.0\r\n 1.0\r\n 1.0\r\n 1.0\r\n 9.0\r\n\r\njulia> c = slice(a, 4:9)\r\n6-element SubArray{Float64,1,Array{Float64,1},(UnitRange{Int64},),1}:\r\n 1.0\r\n 1.0\r\n 1.0\r\n 1.0\r\n 1.0\r\n 9.0\r\n\r\njulia> c[1] = 4.0\r\n4.0\r\n\r\njulia> a\r\n9-element Array{Float64,1}:\r\n 1.0\r\n 1.0\r\n 1.0\r\n 4.0\r\n 1.0\r\n 1.0\r\n 1.0\r\n 1.0\r\n 9.0\r\n\r\njulia> d = reshape(c, 3, 2)\r\n3x2 Array{Float64,2}:\r\n 4.0  1.0\r\n 1.0  1.0\r\n 1.0  9.0\r\n\r\njulia> d[2, 1] = 5.0\r\n5.0\r\n\r\njulia> a\r\n9-element Array{Float64,1}:\r\n 1.0\r\n 1.0\r\n 1.0\r\n 4.0\r\n 1.0\r\n 1.0\r\n 1.0\r\n 1.0\r\n 9.0\r\n```\r\n"},"comment":{"url":"https://api.github.com/repos/JuliaLang/julia/issues/comments/76749293","html_url":"https://github.com/JuliaLang/julia/issues/9874#issuecomment-76749293","issue_url":"https://api.github.com/repos/JuliaLang/julia/issues/9874","id":76749293,"user":{"login":"timholy","id":1525481,"avatar_url":"https://avatars.githubusercontent.com/u/1525481?v=3","gravatar_id":"","url":"https://api.github.com/users/timholy","html_url":"https://github.com/timholy","followers_url":"https://api.github.com/users/timholy/followers","following_url":"https://api.github.com/users/timholy/following{/other_user}","gists_url":"https://api.github.com/users/timholy/gists{/gist_id}","starred_url":"https://api.github.com/users/timholy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/timholy/subscriptions","organizations_url":"https://api.github.com/users/timholy/orgs","repos_url":"https://api.github.com/users/timholy/repos","events_url":"https://api.github.com/users/timholy/events{/privacy}","received_events_url":"https://api.github.com/users/timholy/received_events","type":"User","site_admin":false},"created_at":"2015-03-02T16:51:05Z","updated_at":"2015-03-02T16:51:05Z","body":"A lot of it comes down to having to save a `dims` tuple. If you want really fast column views, you basically have to do this:\r\n```\r\nimmutable ColVector{T,M<:AbstractMatrix} <: AbstractVector{T}\r\n    parent::M\r\n    col::Int\r\nend\r\nColVector(A::AbstractMatrix, col::Integer) = ColVector{eltype(A), typeof(A)}(A, Int(col))\r\n\r\ngetindex(v::ColVector, i::Int) = v.parent[i,v.col]\r\nsize(v::ColVector) = (size(v.parent, 1),)\r\nsize(v::ColVector, d) = d == 1 ? size(v.parent, 1) : 1\r\n```\r\nEven this has its negatives; I think @mlubin noticed that even storing a reference to the parent array means this can't be elided, and so he was storing a pointer instead (which means you need to manually maintain a reference)."}}