[SciPy-user] NumpyArray.resize(0)

Travis Oliphant oliphant.travis at ieee.org
Thu Jan 5 22:51:37 CST 2006


Brendan Simons wrote:

>Congrats Travis, Pearu and all.  I'm very excited that we'll soon  
>have a standard for Python.
>I apologize if the following has been discussed before:
>
>When I try the following code:
>
>a = numpy.ones(5)
>a.resize(0)
>
>I get:
>
>ValueError: newsize is zero; cannot delete an array in this wa0
>  
>
>Numeric gave this result too, while Numarray hapily returns a 0d  
>array.  I prefer the latter because it simplifies my current  
>requirement:  a data structure, based on numpy, which holds an  
>arbitrary number of data points, and which can be extended or  
>truncated at will.  Is there a good reason for Numeric/Numpy's  
>behaviour, as opposed to Numarray's?
>  
>
I can't think of a good reason except that's the way Numeric did it.    
I don't think a change in this behavior would hurt anybody. 

But let's be clear.   A 0d array and a size-0 array are two different 
things.

A 0d array actually has room for one element while a size-0 array has 
one of the dimensions as 0. I just checked, and numarray returns a 
size-0 array.

I can change this. 

But, note also that if another object is referencing a you can't do a 
resize like this:

i.e. 

a = numpy.ones(5)
b = a
a.resize(...)  will give an error because the object a has more than one 
reference to it.

I don't see anyway around this problem short of not using system malloc 
to construct memory (but instead going through a separate memory object 
like numarray does).  This will have a performance impact for small 
arrays -- I'm not sure how much of a one, though.

The point of resize is to be able to modify the size of the memory 
pointer for the array.  You can't do this is another array is using the 
same memory pointer (which it might be if this array has more than one 
reference).

If you really need the numarray ability to swap out the memory address 
of the array for another one, then you might as well use the resize 
function:

a = resize(a,(0,))   # which we will have to fix to make work right ;-)

-Travis



More information about the SciPy-user mailing list