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

Travis Oliphant oliphant at ee.byu.edu
Tue Nov 30 16:44:23 CST 2004


Todd Miller wrote:

>On Tue, 2004-11-30 at 14:05, Travis Oliphant wrote:
>  
>
>>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.
>>
>>    
>>
>
>The rows of the slice (A[:,:k]) are the same "storage distance" apart as
>the rows of the original array (A) so I don't think the strides change
>in this particular case.
>
>  
>
True, (if the memory has not been resized)

So, good call.

-Travis




More information about the Scipy-dev mailing list