且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

是否可以将cython函数作为参数传递给scipy函数?

更新时间:2023-02-26 17:16:57

不能传入 cdef 函数目前。也不可能将回调函数传递给基础的Fortran代码,因为它被f2py封装,而f2py并不了解Cython。

Passing cdef functions in is not possible currently. Passing callback functions to the underlying Fortran code is also not possible, as it's wrapped with f2py which doesn't know about Cython.

您可以做的是:


  • 将您的函数编写为Cython的 def 函数。这些可以传递到所有Scipy的例程中。这不会消除使用Python的回调机制带来的额外函数调用开销(这可能并不重要!),但是您可以加快函数的实现速度,这很可能就足够了。只要记住要像写Cython代码一样照常 cdef 出现的变量。

  • Write your function as Cython's def function. These can be passed in to all Scipy's routines. This does not remove the additional function call overhead which comes from using Python's callback mechanism (which might not be significant!), but you can speed up the implementation of your function, and this might well be enough. Just remember to cdef the variables appearing in, as usual when writing Cython code.

复制MINPACK源代码来自Scipy或netlib.org,并直接自己使用。通过用低级的函数代替Python函数回调机制,可以消除剩余的函数调用开销。

Copy the MINPACK source codes from Scipy or netlib.org, and use them directly yourself. This gets rid of the remaining function call overhead by replacing Python function callback mechanism by the low-level one.

(已经讨论过添加用于为此目的传递低级函数指针的协议,该协议可以被任何需要它的基于Python的系统采用,但是AFAIK的设计尚未完成,并且未在Cython中实现& Scipy。)

(There have been discussions on adding a protocol for passing low-level function pointers around exactly for this purpose, which could be adopted by any Python-based system having a need for it, but AFAIK the design is not completed, and not implemented in Cython & Scipy.)