T J wrote:
> 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
>   
The conversion to float isn't the problem, that's almost free compared 
to taking that x[start:stop:skip] call and doing the sum() itself.

I expect you are doing lots and lots of really small summations? 
Otherwise there's no real point.

> 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):
>   
You unfortunately need to do

cdef double sum(object x, unsigned int
start, unsigned int stop, int skip):
    cdef np.ndarray[np.float64_t, ndim=1] x_buf = x


then use x_buf. There's a slight speed penalty with that assignment which

Also, please use Py_ssize_t instead of unsigned int. unsigned ints are 
dangerous in Cython.

>    cdef unsigned int i
>    cdef double result
>   
You need to initialize result to 0.

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

Reply via email to