using cdef in cython

7 views
Skip to first unread message

Anil Raj

unread,
Sep 29, 2009, 1:30:21 PM9/29/09
to apam-pyt...@googlegroups.com
Hi all,
 I'm trying to define a C-function in cython as follows:

FTYPE = np.float32
ITYPE = np.int32
ctypedef np.float32_t FTYPE_t
ctypedef np.int32_t ITYPE_t

cdef getpc(np.ndarray[ITYPE_t, ndim=2] X, np.ndarray[ITYPE_t, ndim=2] Tplist, np.ndarray[FTYPE_t, ndim=2] w, np.ndarray[FTYPE_t, ndim=2] wp, np.ndarray[FTYPE_t, ndim=2] wm, float eps):


However, when generating the .c file, I get this error:

cdef getpc(np.ndarray[ITYPE_t, ndim=2] X, np.ndarray[ITYPE_t, ndim=2] Tplist, np.ndarray[FTYPE_t, ndim=2] w, np.ndarray[FTYPE_t, ndim=2] wp, np.ndarray[FTYPE_t, ndim=2] wm, float eps):
                                  ^
------------------------------------------------------------

/proj/ar2384/gwas/src/gettree.pyx:205:35: Expected ']'

Any thoughts/suggestions on what I might be doing wrong?

Also, if i wanted my C-function to return two ints, is 'cdef int getpc(...):' still the right way to define the function?

Thanks a lot!

Best
Anil

Anil Raj

unread,
Sep 29, 2009, 1:40:38 PM9/29/09
to apam-pyt...@googlegroups.com

Ian Langmore

unread,
Sep 29, 2009, 9:50:32 PM9/29/09
to apam-pyt...@googlegroups.com
I had just given up on passing numpy arrays to cdef functions (see the
page "cdef or def". I wonder if the technique explained in this link
is better than a simple def?

In regards to returning two ints, I don't think this is possible. In C
u can only return one thing. So you could return a pointer, but to
keep things nice you could pass the address of two ints, then let your
cdef function populate them. Alternatively you can return a python
tuple.

Sent from my iPhone

On Sep 29, 2009, at 1:40 PM, Anil Raj <anil....@gmail.com> wrote:

>

Reply all
Reply to author
Forward
0 new messages