【发布时间】:2015-06-23 22:24:12
【问题描述】:
我正在尝试将scipy.integrate.nquad 与ctypes 函数一起使用。我完全按照Faster integration using Ctypes 上的说明进行操作。
ctypes集成只需几个简单的步骤即可完成:
- 用函数签名
double f(int n, double args[n])在C 中编写一个被积函数,其中args是一个包含函数f的参数的数组。//testlib.c double f(int n, double args[n]) { return args[0] - args[1] * args[2]; //corresponds to x0 - x1 * x2 }
- 现在将此文件编译为共享/动态库(快速搜索将对此有所帮助,因为它依赖于操作系统)。用户必须链接使用的任何数学库等。在 Linux 上,这看起来像:
$ gcc -shared -o testlib.so -fPIC testlib.c输出库将被称为
testlib.so,但它可能具有不同的文件扩展名。现在已经创建了一个库,可以使用ctypes将其加载到 Python 中。
- 使用
ctypes将共享库加载到Python 中并设置restypes和argtypes- 这允许Scipy 解释函数 正确:>>> import ctypes >>> from scipy import integrate >>> lib = ctypes.CDLL('/**/testlib.so') # Use absolute path to testlib >>> func = lib.f # Assign specific function to name func (for simplicity) >>> func.restype = ctypes.c_double >>> func.argtypes = (ctypes.c_int, ctypes.c_double)请注意,无论参数数量如何,argtypes 将始终为
(ctypes.c_int, ctypes.c_double),而 restype 将始终为ctypes.c_double。
- 现在像往常一样集成库函数,这里使用
nquad:>>> integrate.nquad(func, [[0,10],[-10,0],[-1,1]]) (1000.0, 1.1102230246251565e-11)
但是,在最后一步,我并没有得到积分的结果,而是出现以下错误:
>>> integrate.nquad(func,[[0,1.0],[-2.0,3.0],[1.0,2.0]])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/bfn097/apps/scipy/0.13.3_mkl-11.1.2_gcc-4.4.7/lib64/python/scipy/integrate/quadpack.py", line 618, in nquad
return _NQuad(func, ranges, opts).integrate(*args)
File "/home/bfn097/apps/scipy/0.13.3_mkl-11.1.2_gcc-4.4.7/lib64/python/scipy/integrate/quadpack.py", line 670, in integrate
value, abserr = quad(f, low, high, args=args, **opt)
File "/home/bfn097/apps/scipy/0.13.3_mkl-11.1.2_gcc-4.4.7/lib64/python/scipy/integrate/quadpack.py", line 254, in quad
retval = _quad(func,a,b,args,full_output,epsabs,epsrel,limit,points)
File "/home/bfn097/apps/scipy/0.13.3_mkl-11.1.2_gcc-4.4.7/lib64/python/scipy/integrate/quadpack.py", line 319, in _quad
return _quadpack._qagse(func,a,b,args,full_output,epsabs,epsrel,limit)
File "/home/bfn097/apps/scipy/0.13.3_mkl-11.1.2_gcc-4.4.7/lib64/python/scipy/integrate/quadpack.py", line 670, in integrate
value, abserr = quad(f, low, high, args=args, **opt)
File "/home/bfn097/apps/scipy/0.13.3_mkl-11.1.2_gcc-4.4.7/lib64/python/scipy/integrate/quadpack.py", line 254, in quad
retval = _quad(func,a,b,args,full_output,epsabs,epsrel,limit,points)
File "/home/bfn097/apps/scipy/0.13.3_mkl-11.1.2_gcc-4.4.7/lib64/python/scipy/integrate/quadpack.py", line 319, in _quad
return _quadpack._qagse(func,a,b,args,full_output,epsabs,epsrel,limit)
File "/home/bfn097/apps/scipy/0.13.3_mkl-11.1.2_gcc-4.4.7/lib64/python/scipy/integrate/quadpack.py", line 670, in integrate
value, abserr = quad(f, low, high, args=args, **opt)
File "/home/bfn097/apps/scipy/0.13.3_mkl-11.1.2_gcc-4.4.7/lib64/python/scipy/integrate/quadpack.py", line 254, in quad
retval = _quad(func,a,b,args,full_output,epsabs,epsrel,limit,points)
File "/home/bfn097/apps/scipy/0.13.3_mkl-11.1.2_gcc-4.4.7/lib64/python/scipy/integrate/quadpack.py", line 319, in _quad
return _quadpack._qagse(func,a,b,args,full_output,epsabs,epsrel,limit)
quadpack.error: quad: first argument is a ctypes function pointer with incorrect signature
我正在使用 gcc-4.4.7、python 2.6.6、numpy-1.7.1、scipy-0.13.3
【问题讨论】:
-
函数签名中的
double args[n]与double *args相同 -
您已关注faster-integration-using-ctypes。看来这是0.14或0.13中不存在的新功能。
-
我使用的是 scipy-0.15.1。
integrate.nquad(func,[[0,1.0],[-2.0,3.0],[1.0,2.0]])产生(-1.25, 1.4017986003800292e-13)其中func是您问题中的ctypes函数。 -
@nymk 你是对的。我尝试了 0.15.1,它可以工作。
标签: python-2.7 scipy ctypes integrate