scijit.integrate.newton_cotes¶
- scijit.integrate.newton_cotes(rn, equal=0)¶
Weights and error coefficient of a Newton-Cotes rule.
Takes NO callback of either style: it returns the rule, it does not apply it. For an equally-spaced rule, integrate with
dx * sum(an * f(a + arange(N+1) * dx))wheredx = (b - a) / N.- Parameters:
- rnint, float or 1-D array_like
Either the order
N, the number of intervals, so the rule usesN + 1equally-spaced points; or theN + 1sample positions, which must start at 0 and end atN. A float order is accepted and used by value.- equalint, optional
Set to 1 to treat the samples as equally spaced whatever
rnholds, which also replaces the positions with0..N. Default 0, and then equal spacing is detected fromrnitself when every gap is exactly 1, in which case the positions are kept.
- Returns:
- anfloat64 array, shape (N+1,)
Weights, scaled so the integral is
dx * sum(an * f_i).- Bfloat
Error coefficient. The error term is
B * dx**(N+2) * f**(N+1)(xi), andB * dx**(N+3) * f**(N+2)(xi)when the samples are equally spaced andNis even.
- Raises:
- IndexError
An empty
rn.- ValueError
The sample positions do not start at 0 and end at
N;N < 1;rnof rank 2 or more.
See also
scipy.integrate.newton_cotesThe scipy routine this mirrors.
Notes
For
N = 1..14equally spaced the weights matchscipy.integrate. newton_cotesto rounding and the error coefficientBis exact.Above 14, and for unequally spaced samples, the weights are built through the Vandermonde path and lose accuracy as the system grows ill-conditioned. High-order Newton-Cotes weights alternate in sign and grow, so rules above about
N = 8are numerically unusable regardless of who computes them. Prefer composite low-order rules,romb(), orquad().N < 1raises here. scipy reachesrn / float(N), emits a numpyRuntimeWarning: invalid value encountered in divide, and then raisesValueError: math domain errorfrommath.log(0).An
rnof rank 2 or more raises here. scipy reads its order fromlen(rn), which is the first axis alone, so the rest of the array selects nothing and the rule returned describes an order the caller did not ask for.Pure
@njit, no state, so prange-safe.Examples
>>> import numpy as np >>> from numba import njit >>> import scijit.integrate as si >>> @njit ... def run(): ... return si.newton_cotes(4, 0) >>> an, B = run() >>> an array([0.31111111, 1.42222222, 0.53333333, 1.42222222, 0.31111111]) >>> B -0.008465608465608466