choreo.segm.quad.IntegrateOnSegment#
- IntegrateOnSegment(fun, int ndim, (double, double) x_span, QuadTable quad, Py_ssize_t nint=1, bool DoEFT=True) ndarray #
Computes an approximation of the integral of a function on a segment.
Denoting \(a \eqdef \text{x_span}[0]\), \(b \eqdef \text{x_span}[1]\) and \(\Delta \eqdef \text{x_span}[1]-\text{x_span}[0]\), the integral is first decomposed into
nint
smaller integrals as:\[\int_a^b \operatorname{fun}(x) \dd x = \sum_{i = 0}^{\text{nint}-1} \int_{a + \frac{i}{\text{nint}} * \Delta}^{a + \frac{i+1}{\text{nint}} * \Delta} \operatorname{fun}(x) \dd x\]Each of the smaller integrals is then approximated using the
QuadTable
quad
(cf formula (1)).The integrand can either be:
A Python
callable
taking anumpy.float64
as its sole argument, and returning anumpy.ndarray
(shape=ndim, dtype=np.float64)
.A
scipy.LowLevelCallable
for performance-critical use cases.
See also
Convergence analysis of integration methods on segment for a quick demonstration on smooth functions, and the role of compensated summation.
Benchmark of LowLevelCallable for quadrature on segment for an in-depth example and comparison of the different types of function input.
Example
Let us compare the approximation and exact value of the Wallis integral of order 7.
\[\int_0^{\frac{\pi}{2}} \operatorname{sin}^7(x) \dd x = \frac{16}{35}\]>>> import numpy as np >>> import choreo >>> Gauss = choreo.segm.multiprec_tables.ComputeQuadrature(10, method="Gauss") >>> def fun(x): ... return np.array([np.sin(x)**7]) ... >>> np.abs(choreo.segm.quad.IntegrateOnSegment(fun, ndim=1, x_span=(0.,np.pi/2), quad=Gauss) - 16/35) array([4.65549821e-12])
- Parameters:
fun (
callable
orscipy.LowLevelCallable
) – Function to be integrated.ndim (
int
) – Number of output dimensions of the integrand.x_span (
tuple
(numpy.float64
,numpy.float64
)) – Lower and upper bound of the integration interval.quad (
QuadTable
) – Weights and nodes of the integration.nint (
int
, optional) – Number of sub-intervals for the integration, by default1
.DoEFT (
bool
, optional) – Whether to use an error-free transformation for summation, by defaultTrue
.
- Returns:
The approximated value of the integral.
- Return type:
numpy.ndarray
(shape = (nsteps, ndim), dtype = np.float64)