scipy.integrate.quad ctypes函数错误“ quadpack.error:quad:第一个参数是签名不正确的ctypes函数指针”。 [英] scipy.integrate.quad ctypes function error "quadpack.error: quad: first argument is a ctypes function pointer with incorrect signature"
问题描述
我正在尝试将 scipy.integrate.nquad
与 ctypes
函数一起使用。我完全按照使用以下方法进行快速集成的说明进行操作Ctypes 。
I am trying to use scipy.integrate.nquad
with a ctypes
function. I exactly followed the instruction on Faster integration using Ctypes.
ctypes
集成可以用几个简单的方法完成步骤:
ctypes
integration can be done in a few simple steps:
- 在C中用函数签名
double f(int n,double args [n ])
,其中args
是一个包含函数f
的参数的数组。 / li>
- Write an integrand function in C with the function signature
double f(int n, double args[n])
, whereargs
is an array containing the arguments of the functionf
.
//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中。
The output library will be referred to as testlib.so
, but it may have a different file extension. A library has now been created that can be loaded into Python with ctypes
.
- 使用
ctypes
将共享库加载到Python中,并设置restypes
和argtypes
-这允许Scipy正确解释函数
:
- Load shared library into Python using
ctypes
and setrestypes
andargtypes
- this allows Scipy to interpret the function correctly:
>>> 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
。
Note that the argtypes will always be (ctypes.c_int, ctypes.c_double)
regardless of the number of parameters, and restype will always be ctypes.c_double
.
- 现在像往常一样集成库函数,这里使用
nquad
:
- Now integrate the library function as normally, here using
nquad
:
>>> integrate.nquad(func, [[0,10],[-10,0],[-1,1]])
(1000.0, 1.1102230246251565e-11)
但是,在最后一步,我没有得到积分的结果,但是而是显示以下错误:
However, at the final step, I didn't get the result of the integral, but the following errors instead:
>>> 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
I am using gcc-4.4.7,python 2.6.6, numpy-1.7.1, scipy-0.13.3
推荐答案
自变量的类型应为: ctypes.POINTER(ctypes.c_double)
The type of the argument should be: ctypes.POINTER(ctypes.c_double)
但是您是否考虑过使用 cffi
?除了比 ctypes
快外,您还不必手写参数,只需复制C声明并让 cffi
解析它们。
But have you considered using cffi
? Besides being faster than ctypes
, you also don't have to hand-write the argument stuff, just copy the C declarations and let cffi
parse them.
这篇关于scipy.integrate.quad ctypes函数错误“ quadpack.error:quad:第一个参数是签名不正确的ctypes函数指针”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!