simpsArray function
Integrate y(x) using samples along the given axis and the composite Simpson's rule. If x is None, spacing of dx is assumed. If there are an even number of samples, N, then there are an odd number of intervals (N-1), but Simpson's rule requires an even number of intervals. The parameter 'even' controls how this is handled.
Parameters
y
: Array to be integrated.x
: Optional. If given, the points at whichy
is sampled.dx
: Optional. Spacing of integration points along axis ofy
. Only used whenx
is null(not informed). Default is 1.even
: Even {avg, first, last}, optional [avg] : Average two results:1) use the first N-2 intervals with a trapezoidal rule on the last interval and 2) use the last N-2 intervals with a trapezoidal rule on the first interval. [first] : Use Simpson's rule for the first N-2 intervals with a trapezoidal rule on the last interval. [last] : Use Simpson's rule for the last N-2 intervals with a trapezoidal rule on the first interval.
References
- "numpy.simps". https://github.com/scipy/scipy/blob/v1.3.0/scipy/integrate/quadrature.py#L384-L506. Retrieved 2019-07-31.
- "doc numpy.simps". https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.simps.html#scipy.integrate.simps. Retrieved 2019-07-31.
Examples
var x = createArrayRange(start: 0, stop: 10);
var y = createArrayRange(start: 0, stop: 10);
print(simpsArray(y, x: x));
/* output:
40.5
*/
Implementation
double simpsArray(Array y, {Array? x, int dx = 1, Even even = Even.last}) {
checkParamsGetRangeArray(y, x, dx);
var result;
var N = y.length;
if (N % 2 == 0) {
var val = 0.0;
result = 0.0;
// Compute using Simpson's rule on first intervals
if (even == Even.avg || even == Even.first) {
var yslice1 = y[y.length - 1];
var yslice2 = y[y.length - 2];
var last_dx;
if (x != null) {
var xslice1 = x[x.length - 1];
var xslice2 = x[x.length - 2];
last_dx = xslice1 - xslice2;
}
val += 0.5 * last_dx * (yslice1 + yslice2);
result = _basicSimps(y, 0, N - 3, x, dx);
}
// Compute using Simpson's rule on last set of intervals
if (even == Even.avg || even == Even.last) {
var yslice1 = y[0];
var yslice2 = y[1];
var first_dx;
if (x != null) {
var xslice1 = x[0];
var xslice2 = x[1];
first_dx = xslice2 - xslice1;
}
val += 0.5 * first_dx * (yslice2 + yslice1);
result += _basicSimps(y, 1, N - 2, x, dx);
}
if (even == Even.avg) {
val /= 2.0;
result /= 2.0;
}
result = result + val;
} else {
result = _basicSimps(y, 0, N - 2, x, dx);
}
return result;
}