[Numpy-discussion] Bug in resize method?

Christopher Barker Chris.Barker@noaa....
Wed Aug 29 11:52:15 CDT 2007


Charles R Harris wrote:
> What *should* the resize method do? It looks like 
> it is equivalent to assigning a shape tuple to a.shape,

No, that's what reshape does.

 > so why do we need it?

resize() will change the SIZE of the array (number of elements), where 
reshape() will only change the shape, but not the number of elements. 
The fact that the size is changing is why it won't work if if doesn't 
own the data.

 >>> a = N.array((1,2,3))
 >>> a.reshape((6,))
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
ValueError: total size of new array must be unchanged

can't reshape to a shape that is a different size.

 >>> b = a.resize((6,))
 >>> repr(b)
'None'

resize changes the array in place, so it returns None, but a has been 
changed:
 >>> a
array([1, 2, 3, 0, 0, 0])

Perhaps you want the function, rather than the method:

 >>> b = N.resize(a, (12,))
 >>> b
array([1, 2, 3, 0, 0, 0, 1, 2, 3, 0, 0, 0])

 >>> a
array([1, 2, 3, 0, 0, 0])

a hasn't been changed, b is a brand new array.

-CHB



  Apart from that, the reshape method looks like it would serve
> for most cases.
> 
> Chuck
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion@scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion

-- 
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker@noaa.gov


More information about the Numpy-discussion mailing list