reshape() and shared data

143 views
Skip to first unread message

cormu...@mac.com

unread,
Jan 7, 2016, 4:55:24 AM1/7/16
to julia-users
I wondered why there was no reshape!() function, just reshape().

julia> a = Float64[]
0-element Array{Float64,1}

julia> for i = 1:8
          push!(a, i)
       end

julia> a
8-element Array{Float64,1}:
 1.0
 2.0
 3.0
 4.0
 5.0
 6.0
 7.0
 8.0

julia> reshape(a, 4, 2)
4x2 Array{Float64,2}:
 1.0  5.0
 2.0  6.0
 3.0  7.0
 4.0  8.0

julia> a
8-element Array{Float64,1}:
 1.0
 2.0
 3.0
 4.0
 5.0
 6.0
 7.0
 8.0

a appears to be unchanged by the reshaping. So perhaps there would also be a reshape!()  function... 

But in fact, a has changed:

julia> for i = 1:8
          push!(a, i)
       end
ERROR: cannot resize array with shared data
 in push! at array.jl:433
 [inlined code] from none:2
 in anonymous at no file:0

Is there any way to tell a is now shared? Should the reshape() function report such a change? (The documentation suggests that some types of array will be shared, although I haven't yet found one which isn't.)

Tim Holy

unread,
Jan 7, 2016, 8:44:50 AM1/7/16
to julia...@googlegroups.com
On Thursday, January 07, 2016 01:55:24 AM cormu...@mac.com wrote:
> I wondered why there was no reshape!() function, just reshape().

Because reshape can change the type of the array, e.g., from Array{Float64,1}
to Array{Float64,2}. You can't change the type of an existing object.

> But in fact, a has changed:
>
> julia> for i = 1:8
> push!(a, i)
> end
> ERROR: cannot resize array with shared data
> in push! at array.jl:433
> [inlined code] from none:2
> in anonymous at no file:0
>
>
> Is there any way to tell a is now shared?

Not without writing a bit of C code. Follow the breadcrumbs from
@edit push!(a, 10)
to see why.

--Tim

cormu...@mac.com

unread,
Jan 8, 2016, 8:40:41 AM1/8/16
to julia-users
Thanks for the explanation, Tim! Seems a bit odd, but if everybody's happy with it, that's cool. :)

Tim Holy

unread,
Jan 8, 2016, 9:18:33 AM1/8/16
to julia...@googlegroups.com
On Friday, January 08, 2016 05:40:41 AM cormu...@mac.com wrote:
> Thanks for the explanation, Tim! Seems a bit odd, but if everybody's happy
> with it, that's cool. :)

I guess the question is, "what would you do with the information?" And maybe
even more relevant, it seems that no one has wanted that information badly
enough to implement it. But if you need it, you could change that ;-).

--Tim

cormu...@mac.com

unread,
Jan 8, 2016, 10:31:13 AM1/8/16
to julia-users
I was thinking more that the lack of an exclamation after 'reshape' makes you think that the array is unchanged, whereas it has changed, since you can no longer push to it as before. But it's more a problem for my understanding than anything else... :)
Reply all
Reply to author
Forward
0 new messages