scijit.integrate.fixed_quad

scijit.integrate.fixed_quad(func, a, b, args=(), n=5)

Definite integral by fixed-order Gauss-Legendre quadrature.

Non-adaptive: it evaluates func at exactly n points and returns, with no error estimate and no refinement. Use quad() when the integrand is not smooth or the accuracy matters.

func is a plain @njit function passed as a first-class argument, and it is vectorized: it receives the whole array of abscissae at once:

@njit
def f(x):                  # x is a 1-D float64 array
    return np.exp(-x * x)  # an array of the same length

val, _ = fixed_quad(f, 0.0, 1.0, (), 5)

Parameters bound to the integrand travel in args and arrive as separate arguments:

@njit
def g(x, alpha, beta):
    return beta * np.exp(-alpha * x * x)

val, _ = fixed_quad(g, 0.0, 1.0, (2.0, 3.0), 5)
Parameters:
func@njit function

Integrand, vectorized over its first argument, called as func(x_array, *args). A scalar-in/scalar-out function will not work. A vector-valued integrand returns shape (..., len(x)) and the result carries the leading axes.

a, bfloat

Finite integration limits. Infinite limits raise ValueError.

argstuple, optional

Extra arguments, splatted into func. Default ().

nint, optional

Number of Gauss-Legendre points, so the rule is exact for polynomials up to degree 2n - 1. Default 5.

Returns:
valfloat or ndarray

The integral.

noneNone

Always None, present as the second element so a caller can unpack two names.

Raises:
TypeError

func not callable; args not iterable.

ValueError

n < 1, from the Gauss-Legendre roots; infinite a or b. The roots are resolved first, so n < 1 is reported ahead of an infinite limit.

See also

scipy.integrate.fixed_quad

The scipy routine this mirrors.

Notes

args must be a tuple inside @njit. From the interpreter a list or an array is also accepted and splatted.

Pure @njit, no state, so prange-safe.

Examples

>>> import numpy as np
>>> from numba import njit
>>> import scijit.integrate as si
>>> @njit
... def f(x, alpha):                # x is the whole abscissa array
...     return alpha * np.exp(-x * x)
>>> @njit
... def run():
...     return si.fixed_quad(f, 0.0, 1.0, (2.0,), 5)
>>> val, none = run()
>>> val
1.4936482535324966
>>> none is None
True