Hi,

I want to call x[start:stop:skip].sum() on a NumPy array within cython
code.  The problem with doing this is that the call is a Python call,
which is handled in C, whose result is converted into a Python float.
So, I'd have to convert this Python float back into a double.  So it
seems best if I just wrote the summation manually.  I give it a NumPy
array, a start index, stop index, and a skip index.  I can write the
loop, but I am not sure how I should define the cdef function
signature.

Can someone help me out?  I need something like:

cdef double sum(np.ndarray[np.float64_t, ndim=1] x, unsigned int
start, unsigned int stop, int skip):
   cdef unsigned int i
   cdef double result
   for i in range(start, stop, skip):
       result += x[i]
   return result

I'm actually hoping the cython can accept the above and do the magic
for me.  Looking at C which accesses NumPy arrays:

 (*__Pyx_BufPtrStrided1d(__pyx_t_5numpy_float64_t *,
__pyx_bstruct_a.buf, __pyx_t_8, __pyx_bstride_0_a)

seems to suggest I might need something involving buffers and strides.
 Or is it possible to call the NumPy C function (assuming it doesn't
convert to the Python float within that same function)?

Thanks!
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to