简体   繁体   中英

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

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 to 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM