scijit.integrate.quad¶
- scijit.integrate.quad(func, a, b, args=(), full_output=0, epsabs=1.49e-08, epsrel=1.49e-08, limit=50, points=None, weight=None, wvar=None, wopts=None, maxp1=50, limlst=50, complex_func=False)¶
Adaptive integration of
funcfromatob.One name over seven QUADPACK entry points, chosen from the values of
aandb, frompointsand fromweight.call
QUADPACK
quad(f, 0.0, 1.0)dqagsquad(f, 0.0, np.inf)dqagiquad(f, 0.0, 1.0, points=p)dqagpweight='cos'/'sin', finitedqawoweight='cos'/'sin', b=infdqawfweight='alg'…dqawsweight='cauchy'dqawcfuncis a plain@njitfunction, called asfunc(x, *args).Calls nest, to any depth. A
quadinside anotherquad’s integrand is correct: onint_0^1 int_0^1 exp(-x y) dy dx, whose value is 0.796599599297053, the nested call returns 0.7965995992970532. Each wrapper saves the thread’s callback slot on entry and restores it on exit, which is what makes that work.nquad()is that nesting with its depth taken from the length ofranges, anddblquadandtplquadarenquadat depth two and three.- Parameters:
- func@njit function
The integrand. It is called as
func(x, *args), so it takes one argument whenargsis empty and1 + len(args)otherwise, and returns the integrand value.- a, bfloat
Interval endpoints. Either may be
np.infor-np.inf.b < agives the negated integral anda == bgives(0.0, 0.0).- argstuple, optional
Extra parameters, handed to
funcafterx. A value that is not a tuple is read as a one-item tuple, soargs=Noneis one argument and not none. An entry is a real number or an array of real numbers. A scalar keeps its type, so an integer arrives as an integer; an array’s data arrives as float64 at its own rank and shape. Default().- full_outputbool or int, optional
Return the diagnostics as a third value. Default 0. Inside
@njitit must be a compile-time constant: it selects the number of return values.- epsabs, epsrelfloat, optional
Requested absolute and relative accuracy. Both default to 1.49e-8.
- limitint, optional
Maximum number of subintervals. Default 50.
- pointsfloat64 array or None, optional
Interior break points, where the integrand has a known singularity or kink. Sorted, deduplicated and filtered to
a < p < bfirst, so unsorted input, duplicates, out-of-range values, the endpoints themselves andnan/infall drop out. Only with finite limits: an infinite one raisesValueError. Inside@njitNoneagainst an array is a compile-time choice.- weightstr or None, optional
Weight function folded into the integrand, one of
'cos','sin','alg','alg-loga','alg-logb','alg-log','cauchy'. Case sensitive. Inside@njitit must be a string literal, since it selects which QUADPACK routine is called.- wvarfloat or 2-element sequence, optional
The weight’s parameter:
omegafor'cos'/'sin',cfor'cauchy',(alpha, beta)for the'alg'family. A value the weight does not take raisesTypeError. For awvarARRAY under the'alg'family the length is read when the call runs, so the message names the requirement and not the length received.- woptsoptional
Accepted and ignored. Its purpose is to hand back precomputed Chebyshev moments.
- maxp1int, optional
Upper bound on the number of Chebyshev moment sets for
'cos'/'sin'. Default 50.- limlstint, optional
Upper bound on the number of cycles for
'cos'/'sin'withb = inf. Default 50.- complex_funcbool, optional
Integrate a complex-valued
func, by integrating its real and its imaginary part separately and recombining. Default False. Inside@njitit must be a compile-time constant, and it cannot be combined there withfull_output.
- Returns:
- yfloat or complex
The integral. Complex when
complex_funcis true.- abserrfloat or complex
Estimate of the absolute error.
- infodictdict
Present only when
full_outputis true. The key set for the routine the call reached:neval,last,alist,blist,rlist,elistandiordon the five ordinary routes,pts,levelandndinbeside them on the break-point route,nnlog,chebmoandmomcomon the oscillatory one, andneval,lst,rslst,erlstandierlston the cos/sin route to infinity. An empty interval,a == b, carries the unweighted key set whatever the route. The arrays have lengthlimit, and lengthlimlstfor the cycle arrays on the cos/sin route to infinity; only[:last], or[:lst]there, is meaningful. One key is additive:ier, QUADPACK’s exit code, 0 for success.EVERY VALUE IS A 1-D float64 ARRAY.
neval,last,lstandierare length 1, soinfodict['neval']readsarray([21.])and the value itself isinfodict['neval'][0].chebmois flat, of length25 * maxp1;.reshape(25, maxp1)recovers a two-dimensional shape. The value types are described under Notes.With
complex_functhe two runs arrive under'real'and'imag'.
- Raises:
- ValueError
What QUADPACK reports as an invalid input: a
limitbelow 1; amaxp1below 1 or alimlstbelow 3 on a route that reads them; more break points thanlimit, or break points outside the interval;alphaorbetabelow -1; a Cauchywvarequal to a limit; anepsabsat or below 0 with too small anepsrel. Also break points with an infinite limit, a cos/sin weight over(-inf, inf), analgorcauchyweight over an infinite interval, and an unrecognisedweight.- TypeError
funcgiven as an address rather than as a function, with an arity thatargsdoes not fit, or returning a complex value whilecomplex_funcis false.- An empty interval,
a == b, returns before any of these are - checked. Whatever the integrand raises is propagated.
- Warns:
- IntegrationWarning
On a soft QUADPACK failure (
ierin 1, 2, 3, 4, 5, 7) withfull_outputfalse, and onpointsgiven together withweight, which is ignored.
See also
scipy.integrate.quadThe scipy routine this mirrors.
Notes
Three arguments are compile-time constants inside
@njit:full_output,weightandcomplex_func. A runtime value in one of those slots raisesTypingErrornaming it.IntegrationWarningis a different class object fromscipy.integrate.IntegrationWarning, with the same name and base, so a filter naming scipy’s class does not select it. Filter on this package’s class or onUserWarning.The infodict’s values. Every value is a 1-D float64 array, where scipy’s
infodictholds an int underneval,last,lstandmomcom, int32 arrays underiord,level,ndin,nnlogandierlst, and a two-dimensionalchebmo. Read a count asint(info['neval'][0])and the moments asinfo['chebmo'].reshape(25, maxp1).A
funcreturning a complex value whilecomplex_funcis false raisesTypeError. scipy raises the same for a Pythoncomplexand, for a numpycomplex128, warns once per evaluation and integrates the real part.With
complex_functrue andfull_outputfalse, an empty interval returns0j, where scipy returns0.0.Every diagnostic array is zeroed past
last, where scipy leaves the tail uninitialised.On a nonzero
ierthe Fortran layer also prints a short line to standard output, which numba cannot suppress.Examples
A plain
@njitintegrand, with its extra parameters in theargstuple:>>> import numpy as np >>> from numba import njit >>> import scijit.integrate as si >>> @njit ... def f(x, c, d): ... return c * np.exp(-d * x * x) >>> @njit ... def run(): ... return si.quad(f, 0.0, 1.0, (2.0, 1.0)) >>> run() (1.4936482656248542, 1.658282695188145e-14)
full_outputadds the diagnostics, and must be a constant:>>> @njit ... def g(x): ... return np.exp(-x * x) >>> @njit ... def run_full(): ... v, e, info = si.quad(g, 0.0, 1.0, (), True) ... return v, info['neval'][0], info['last'][0], info['ier'][0] >>> run_full() (0.7468241328124271, 21.0, 1.0, 0.0)
A complex integrand, with
complex_func:>>> @njit ... def h(x, w): ... return np.exp(1j * w * x) >>> @njit ... def run_cx(): ... return si.quad(h, 0.0, 1.0, (2.0,), complex_func=True) >>> run_cx() ((0.45464871341284085+0.7080734182735712j), (6.045545055588785e-15+7.861194120923578e-15j))