开发者

Use scipy.integrate.quad from within a cdef statement in Cython?

开发者 https://www.devze.com 2023-03-23 06:27 出处:网络
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 开发者_StackOverf

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消