[Numpy-discussion] Re: [SciPy-dev] in-situ resizing array in C

Todd Miller jmiller at stsci.edu
Mon Nov 29 14:38:02 CST 2004

On Mon, 2004-11-29 at 16:33, Pearu Peterson wrote:
> Hi,
> I have a question to Numeric/Numarray experts:
> Let A be a contiguous one or two-dimensional array with shape (n,) or 
> (m,n), respectively. Let k be a positive integer less than n.
> Reference count for A is 1. The objective is to execute A=A[:k] 
> or A=A[:,:k], respctively, in C.
> Q: Is it safe to resize the array A by simply resetting its dimensions, 
> that is,
>     A->dimensions[0] = k;
> or
>     A->dimensions[1] = k;

Sounds good to me for numarray.  Numeric's "dimensions" points to a C
array not necessarily owned by the object... I don't know if that's a
problem or not.  The dimensions array storage is built into the numarray
object so it's not a problem there.

> if A->nd is 1 or 2, respectively, in C? This appears to work but may be 
> there are some corner cases that I have overlooked.

Since you said 0 < k < n I think any corners are covered.  It's probably
good to handle m or n being 0.

> Would that work also when using Numarray?

This should work unaltered in numarray.  Here are a few general

1. Since you're modifying in-situ,  the changes are permanent unless
reversed later.  This is in contrast to a slice (view) which has a
temporary copy of the array shape (not data) and leaves the original
array unaltered.

2. Since you're not changing the size of the data,  I'd call this
"reshaping" and not "resizing".

3. Ref count of A is 1 eliminates any (non-data) sharing issues for


More information about the Numpy-discussion mailing list