[Pyrex] Numeric and Pyrex

Nitin Madnani nmadnani at wam.umd.edu
Tue Sep 20 04:58:10 CEST 2005


> Nitin Madnani wrote:
>
>> Hmm, I see. So I can call Numeric pythonically to create the  
>> array  and then use the external type code as demonstrated in your  
>> example  to fill it ? And I can do both of these things in the  
>> same pyrex module?
>>
>
> Yes.
>

Cool ! So I tried this ... the example included with Pyrex shows how  
to read from a given array and now how to fill in one. I just figured  
that it's probably done in a similar way but it doesn't seem to be  
working. Probably another dumb error:

import Numeric

cdef extern from "/nfshomes/nmadnani/local/include/python2.4/Numeric/ 
arrayobject.h":

     struct PyArray_Descr:
         int type_num, elsize
         char type

     ctypedef class Numeric.ArrayType [object PyArrayObject]:
         cdef char *data
         cdef int nd
         cdef int *dimensions, *strides
         cdef object base
         cdef PyArray_Descr *descr
         cdef int flags

def test():
     cdef int i,j, index
     cdef float *elems
     cdef ArrayType m

     m = Numeric.ones((3, 3), Numeric.Float)
     elems = <float *>m.data
     elems[1] = 3.0

     return m

After compilation and usage in a test python file, this returns the  
following array:
[[ 32.   1.   1.]
[  1.   1.   1.]
[  1.   1.   1.]]

What did I do wrong?
Thanks again,
Nitin
PS: I sent this email before as well but I am not sure if it actually  
got through.




More information about the Pyrex mailing list