文档discusses使用numba的cfunc
s作为scipy.integrate.quad
的LowLevelCallable
参数。我需要同样的附加参数。在
我基本上是想做这样的事情:
import numpy as np
from numba import cfunc
import numba.types
voidp = numba.types.voidptr
def integrand(t, params):
a = params[0] # this is additional parameter
return np.exp(-t/a) / t**2
nb_integrand = cfunc(numba.float32(numba.float32, voidp))(integrand)
但是,它不起作用,因为params
应该是voidptr
/void*
,它们不能被转换成{
我没有找到任何关于如何从Numba中的void*
提取值的信息。在C语言中,它应该类似于a = *((double*) params)
-在Numba中可以做同样的事情吗?在
1。通过
scipy.integrate.quad
^{} docs 说:
因此,要通过
quad
向integrand
传递额外的参数,最好使用double func(int n, double *xx)
签名。在您可以将decorator写入被积函数,将其转换为
LowLevelCallable
,如下所示:或者,如果不需要decorator,请手动创建
LowLevelCallable
,并将其传递给quad
。在2。包装被积函数
我不确定以下内容是否能满足您的要求,但您也可以包装您的
^{pr2}$integrand
函数以获得相同的结果:3。从
voidptr
转换为python类型我认为这还不可能。从2016年的this discussion开始,
voidptr
似乎只是在这里向C回调传递一个上下文。在尝试以下方法:
似乎也不令人鼓舞!在
相关问题 更多 >
编程相关推荐