I am trying to speed up my Python by translating it into Cython. It uses the function scipy.integrate.quad, which requires a python callable as one of its arguments. Is there any way 开发者_StackOverflowto define and pass a function from within a cdef statement in Cython so I can use this?
Thanks.
Yes, you have just to wrap the function with a python function.
def py_callback(a):
return c_callback(a)
cdef float c_callback(float a):
return a * a
...
scipy.integrate.quad(py_callback)
There is a bit of conversion overload of course. An alternative is to look up how that function is implemented.
- If is a wrapper to a C function try to call directly the C function from the pyx file (creating a pxd that wraps the scipy C-module).
- If is pure Python attempt to translate it to cython.
Otherwise you have to use some other pure C integration function and skip the scipy one.
精彩评论