Hello,

I'd like to write some UFuncs in Cython (using
http://wiki.cython.org/MarkLodato/CreatingUfuncs) , but I have struggled
with writing a Generic Loop so I can write UFuncs with types other than
those supplied by numpy. Does anyone have an example they could post? Or
give me a hint at what I am doing incorrectly?

The numpy docs give the following as an example of a valid loop, and I
believe that for Generic Loops, the actual function is passed in as value in
*extra:

static void
double_add(char *args, npy_intp *dimensions, npy_intp *steps,
   void *extra)
{
    npy_intp i;
    npy_intp is1=steps[0], is2=steps[1];
    npy_intp os=steps[2], n=dimensions[0];
    char *i1=args[0], *i2=args[1], *op=args[2];
    for (i=0; i<n; i++) {
        *((double *)op) = *((double *)i1) + \
                          *((double *)i2);
        i1 += is1; i2 += is2; op += os;
     }
}

My attempt at writing a Generic Loop in Cython with the signature    "
double  func(int x)" was the following:

cdef void PyUFunc_i_d(char *args, npy_intp *dimensions, npy_intp *steps,
void *extra):
    cdef npy_intp i
    cdef npy_intp is1 = steps[0],
    cdef npy_intp os = steps[1], n = dimensions[0]
    cdef char *i1 = args[0], *op = args[1]

    for i in range(n):

        *(<double *>op) = *(<double(*)(int)> extra[0]) (  *(<int*> i1) )

        i1 += is1
        op += os
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to