python - 使用 LowLevelCallable : How to pass user_data? 与 scipy quad 集成

标签 python c scipy

所以我关注了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/

相关文章:

python - Dockerfile scrapy未知命令:爬网

C - 结构数组元素交换

c - 在 C 中,如果数组在声明时初始化,则维度是可选的。为什么?

python - 假设自动线程化的 scipy 和 numpy 函数没有使用多核

python - 如何从样本的最大值、最小值和平均值中找到分布函数

python - 使用 genextreme 和 weibull_min 将 Weibull 拟合到分布

python - celery ,作为 worker 和调度程序的推荐方法是什么?

python - deque.popleft() 和 list.pop(0)。有性能差异吗?

python:两个时间日期字符串的区别

C程序崩溃逆文件txt