[SciPy-dev] in-situ resizing array in C

Travis Oliphant oliphant at ee.byu.edu
Tue Nov 30 13:05:19 CST 2004


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;

For 2-D arrays you need to set A->strides correctly too.  A->strides 
contains the number of *bytes* to jump to get to the next element in 
that dimension (it's what makes non-contiguous arrays work).  For the 
second case you would have to set:

   A->strides[0] = k*A->descr->elsize.

-Travis





More information about the Scipy-dev mailing list