[Pyrex] Passing objects back to Python?

Lenard Lindstrom len-l at telus.net
Sun Feb 4 18:13:57 UTC 2007


Glenn wrote:
> It looks like the problem I was seeing was caused by the way the data  
> array was indexed:
>
>
>   
>>>     for i from 0<=i<arr.dimensions[0]:
>>>         print '  ',10.0*(<double*>arr.data)[0]
>>>         els.append((<double*>arr.data)[0])
>>>         arr.data = arr.data + arr.strides[0]
>>>       
>
> I adapted the code from an example I found and didn't think enough  
> about what the code actually did.
>
> Using
>   
>> for i from 0<=i<arr.dimensions[0]:
>>         print '  ',(<double *>(arr.data + i*arr.strides[0]))[0]
>>     
>
> doesn't seem to mangle the data portion of the ndarray.
>
>   

If you haven't already figured it out, the reason the array was 
"mangled" was that you changed the value of the arr.data field which 
tells NumPy where the C array starts. Otherwise the first approach is 
the more efficient. Instead of using arr.data, create a local char *data 
variable initialized to arr.data. If you know the arrays you will pass 
in are contiguous - not slices - then you should be able to use double * 
pointers instead and ignore the strides field. But I haven't confirmed that.

-- 
Lenard Lindstrom
<len-l at telus.net>




More information about the Pyrex mailing list