I am trying to use scipy.integrate.nquad
with a ctypes
function. I exactly followed the instruction on Faster integration using Ctypes.
ctypes
integration can be done in a few simple steps:
- 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 }
- Now compile this file to a shared/dynamic library (a quick search will help with this as it is OS-dependent). The user must link any math libraries, etc. used. On Linux this looks like:
$ gcc -shared -o testlib.so -fPIC testlib.c
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 withctypes
.
- 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)
Note that the argtypes will always be
(ctypes.c_int, ctypes.c_double)
regardless of the number of parameters, and restype will always bectypes.c_double
.
- 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
I am using gcc-4.4.7,python 2.6.6, numpy-1.7.1, scipy-0.13.3
The type of the argument should be:
ctypes.POINTER(ctypes.c_double)
But have you considered using
cffi
? Besides being faster thanctypes
, you also don't have to hand-write the argument stuff, just copy the C declarations and letcffi
parse them.