quadrature

quadrature(
    f,
    lower,
    upper,
    method='gauss_kronrod',
    absolute_tolerance=None,
    relative_tolerance=None,
    max_function_evaluations=None,
    steps=None,
    min_depth=None,
    max_depth=None,
)

Integrate a user-written function over a finite interval.

Computes the definite integral of f over [lower, upper]. The default method, "gauss_kronrod", is the ported Numerics adaptive Gauss-Kronrod rule (10-point Gauss, 21-point Kronrod), which subdivides the interval until the two nested estimates agree to the requested tolerance. Nine other ported methods (P2 “math extras”) are available: three more adaptive rules ("simpsons", "trapezoidal", "adaptive_simpsons", "gauss_lobatto") and five fixed rules with no adaptive refinement ("gauss_legendre", "gauss_legendre20", "simpsons_fixed", "trapezoidal_fixed", "midpoint").

Named quadrature rather than integrate to stay in step with corehydror, where the latter would mask stats::integrate.

Parameters

Name Type Description Default
f callable A function taking one number and returning one number. required
lower float The limits of integration. upper must be above lower; neither may be infinite. required
upper float The limits of integration. upper must be above lower; neither may be infinite. required
method str One of "gauss_kronrod" (the default), "simpsons", "trapezoidal", "adaptive_simpsons", "gauss_lobatto", "gauss_legendre", "gauss_legendre20", "simpsons_fixed", "trapezoidal_fixed", or "midpoint". 'gauss_kronrod'
absolute_tolerance float The convergence tolerances on the difference between the two nested estimates the adaptive methods compare ("gauss_kronrod", "simpsons", "trapezoidal", "adaptive_simpsons", "gauss_lobatto"). Each must lie between 1e-15 and 1. Left unset, the ported integrator’s own defaults (1e-8) apply. Only apply to the adaptive methods; supplying either for one of the five fixed-rule methods raises ValueError. None
relative_tolerance float The convergence tolerances on the difference between the two nested estimates the adaptive methods compare ("gauss_kronrod", "simpsons", "trapezoidal", "adaptive_simpsons", "gauss_lobatto"). Each must lie between 1e-15 and 1. Left unset, the ported integrator’s own defaults (1e-8) apply. Only apply to the adaptive methods; supplying either for one of the five fixed-rule methods raises ValueError. None
max_function_evaluations int The cap on evaluations of f, for the adaptive methods. Reaching it stops the subdivision and is reported in the status rather than raising. Left unset, the ported integrator’s own default applies. Supplying it for one of the five fixed-rule methods raises ValueError. None
steps int The number of integration steps, for method "simpsons_fixed", "trapezoidal_fixed", or "midpoint" alone. Left unset, the ported static’s own default (2) applies. None
min_depth int The recursion-depth bounds, for method="adaptive_simpsons" alone. Left unset, the ported class’s own defaults (0 and 100) apply. None
max_depth int The recursion-depth bounds, for method="adaptive_simpsons" alone. Left unset, the ported class’s own defaults (0 and 100) apply. None

Returns

Name Type Description
QuadratureResult The integral, a float carrying status, function_evaluations and standard_error. For the five fixed-rule methods, which have no adaptive status or evaluation count of their own, status is always "Success" and function_evaluations/standard_error are None; standard_error is also 0.0 for "simpsons", "trapezoidal", and "gauss_lobatto", which have no such estimate of their own either.

Examples

>>> import corehydropy as ch
>>> round(ch.quadrature(lambda x: x**2, 0, 3), 10)
9.0
>>> ch.quadrature(lambda x: x**2, 0, 3).status
'Success'
>>> round(ch.quadrature(lambda x: x**3, 0, 1, method="gauss_lobatto"), 3)
0.25
>>> round(ch.quadrature(lambda x: x**3, 0, 1, method="midpoint", steps=1000), 3)
0.25