所以我关注了reference如何使用 LowLevelCallable。问题是,我显然无法像以前那样将参数传递给 dblquad 函数(这会破坏我想要加速的所有代码)。我的 C 代码如下所示:
#include <math.h>
// This function is f = ax + by
double f (int n, double *x) {
return 2*(x[2]*x[0]+x[3]*x[1]);
}
然后是 python 代码
import os, ctypes
from scipy import LowLevelCallable
from scipy.integrate import dblquad
lib = ctypes.CDLL(os.path.abspath('./testlib.so'))
lib.f.restype = ctypes.c_double
lib.f.argtypes = (ctypes.c_int, ctypes.POINTER(ctypes.c_double))
func = LowLevelCallable(lib.f)
for a in [1,2,3]:
for b in [1,2,3]:
print(dblquad(func, 0,1, lambda x: 0, lambda y: 1, args = [a,b])[0], a+b)
//编辑:有错别字。现在可以了。没关系。
最佳答案
如所述here ,棘手的事情是将您的参数包含在数组 x 中,即 x = (x0, x1, ..., xn, t0, t1, ..., tm) 其中 x0,...xn 是坐标。
关于python - 使用 LowLevelCallable : How to pass user_data? 与 scipy quad 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48687980/