[Numpy-discussion] resize in old Numeric

Travis E. Oliphant oliphant@enthought....
Wed Dec 12 13:37:57 CST 2007

Christian Meesters wrote:
> Hi,
> For compatibility reasons (work with Biopython) I would like to use to
> Numeric in some code of mine. (Of course, I could make a little detour
> converting into numpy.array, but first I wonder whether somebody might
> know a solution for the problem with Numeric.)
> I need to flatten a simple 3x3 array and try to do that with resize.
> First time works, next time it doesn't. Here is an example:
> In [1]: from Numeric import *
> In [2]: a = array([[ 0.50622966, -0.54764389, -0.66619644],
> [ 0.44338279, -0.4973092, 0.7457228 ],[-0.73969131, -0.67288704,
> -0.00893311]])
> In [3]: a.resize((9,))
> Out[3]: 
> array([ 0.50622966, -0.54764389, -0.66619644,  0.44338279, -0.4973092 ,
> 0.7457228 ,
>             -0.73969131, -0.67288704, -0.00893311])
> In [4]: a.resize((9,))
> ---------------------------------------------------------------------------
> <type 'exceptions.ValueError'>            Traceback (most recent call
> last)
> /home/cm/Documents/projects/SAXS/Eury/MC_rigid/<ipython console> in
> <module>()
> <type 'exceptions.ValueError'>: cannot resize an array that has been
> referenced or is referencing
>   another array in this way.  Use the resize function.
> Unfortunately, when applying resize on the actual array I have, I get a
> similar, yet slightly different error:

The problem is that Numeric has no way of knowing whether a reference 
held is safe or not.  In other-words, is the reference because of a view 
into a part of the array or a simple name binding (like having the '_' 
variable in the interpreter hold on to the old reference).

A reallocation of the memory while other views are expecting to be able 
to walk over the old pointer is a recipe for problems and is the reason 
that Numeric has this restriction.

NumPy has the same restriction but allows you to 'over-ride' it if you 
know what you are doing.

To fix the problem, you need to get rid of all other references to the 
array that you want to resize.

-Travis O.

More information about the Numpy-discussion mailing list