[Numpy-discussion] getting data pointer to numpy object in Pyrex

Francesc Altet faltet at carabos.com
Wed Mar 29 00:01:08 CST 2006


A Dimecres 29 Març 2006 00:28, Brian Blais va escriure:
> ...but I would like to do something like...
>
> #-------------------------------
> def test2(d):  # pass a python dict
>
>      w=d['w']   # get the numpy object
>
>      cdef double *w_p
>
>      # get the pointers
>      w_p=<double *>w.data
> #-------------------------------
>
>
> Is there a way to do this?

The next works for any numerical package that supports the array
protocol:

      data_address = int(w.__array_data__[0], 16)
      w_p = <double *>PyInt_AsLong(data_address)

and declare PyInt_AsLong as:

cdef extern from "Python.h":
    long PyInt_AsLong(object)

It is important to note that the above solution does not require to
compile against the numpy (or any other numerical package) headers at
all. See "A Pyrex Agnostic Class" recipe in the SciPy cookbook
(http://www.scipy.org/Cookbook) for a more complete example.

Regards,

-- 
>0,0<   Francesc Altet     http://www.carabos.com/
V   V   Cárabos Coop. V.   Enjoy Data
 "-"





More information about the Numpy-discussion mailing list