On 4/23/07, Christopher Barker Chris.Barker@noaa.gov wrote:
Gael Varoquaux wrote:
Unless I miss something obvious "a.reshape()" doesn't modify a, which is somewhat missleading, IMHO.
quite correct. .reshape() creates a new array that shared data with the original:
Sometimes it do, sometimes it don't:
In [8]: x = ones((8,8))
In [9]: y = x[:3,:6]
In [10]: z = y.reshape(2,9)
In [11]: z[...] = 0
In [12]: x Out[12]: array([[ 1., 1., 1., 1., 1., 1., 1., 1.], [ 1., 1., 1., 1., 1., 1., 1., 1.], [ 1., 1., 1., 1., 1., 1., 1., 1.], [ 1., 1., 1., 1., 1., 1., 1., 1.], [ 1., 1., 1., 1., 1., 1., 1., 1.], [ 1., 1., 1., 1., 1., 1., 1., 1.], [ 1., 1., 1., 1., 1., 1., 1., 1.], [ 1., 1., 1., 1., 1., 1., 1., 1.]])
In [13]: z Out[13]: array([[ 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0., 0.]
As Ann pointed out, sometimes reusing the same array isn't possible. This makes the use of reshaped arrays as l-values subject to subtle errors, so caution is advised.
Chuck