Skip to content

Commit 443d474

Browse files
committed
Allow for ctypes-based callbacks.
Then callbacks from compiled C code or Sympy JIT can be used for faster integrand evaluation.
1 parent 8dace43 commit 443d474

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

pycuba/__init__.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ class BOUNDS(ctypes.Structure):
3434
POINTER(c_double), POINTER(c_int), POINTER(c_double), c_void_p)
3535
peakfinder_type = ctypes.CFUNCTYPE(c_void_p, POINTER(c_int), POINTER(BOUNDS),
3636
POINTER(c_int), POINTER(c_double))
37+
38+
def wrap_integrand(integrand):
39+
use_raw_callback = isinstance(integrand, ctypes._CFuncPtr)
40+
if use_raw_callback:
41+
return integrand
42+
else:
43+
return integrand_type(integrand)
3744

3845
def Vegas(integrand, ndim, userdata=NULL,
3946
epsrel=EPSREL, epsabs=EPSABS, verbose=0, ncomp=1, seed=None,
@@ -92,7 +99,7 @@ def integrand(ndim, x, ncomp, f, userdata, weight, iter):
9299
if seed is None:
93100
seed = 0
94101

95-
lib.Vegas(ndim, ncomp, integrand_type(integrand), userdata,
102+
lib.Vegas(ndim, ncomp, wrap_integrand(integrand), userdata,
96103
c_int(nvec), c_double(epsrel), c_double(epsabs), verbose, seed,
97104
mineval, maxeval, nstart, nincrease, nbatch,
98105
gridno, statefile, spin,
@@ -137,7 +144,7 @@ def Suave(integrand, ndim, nnew=1000, nmin=2, flatness=50., userdata=NULL,
137144
if seed is None:
138145
seed = 0
139146

140-
lib.Suave(ndim, ncomp, integrand_type(integrand), userdata,
147+
lib.Suave(ndim, ncomp, wrap_integrand(integrand), userdata,
141148
c_int(nvec), c_double(epsrel), c_double(epsabs), verbose, seed,
142149
mineval, maxeval, nnew, nmin, c_double(flatness), statefile, spin,
143150
byref(nregions), byref(neval), byref(fail), integral, error, prob)
@@ -280,7 +287,7 @@ def integrand(ndim, x, ncomp, f, phase):
280287
if seed is None:
281288
seed = 0
282289

283-
lib.Divonne(ndim, ncomp, integrand_type(integrand), userdata,
290+
lib.Divonne(ndim, ncomp, wrap_integrand(integrand), userdata,
284291
c_int(nvec), c_double(epsrel), c_double(epsabs), verbose, seed,
285292
mineval, maxeval, key1, key2, key3, maxpass,
286293
c_double(border), c_double(maxchisq), c_double(mindeviation),
@@ -318,7 +325,7 @@ def Cuhre(integrand, ndim,
318325
if seed is None:
319326
seed = 0
320327

321-
lib.Cuhre(ndim, ncomp, integrand_type(integrand), userdata,
328+
lib.Cuhre(ndim, ncomp, wrap_integrand(integrand), userdata,
322329
c_int(nvec), c_double(epsrel), c_double(epsabs), verbose,
323330
mineval, maxeval, key, statefile, spin,
324331
byref(nregions), byref(neval), byref(fail), integral, error, prob)

0 commit comments

Comments
 (0)