viewof a_integrate = def`async generator function integrate(f, min, max, steps=2*width):
Integrate the function _f_ from _min_ to _max_.
For the moment, we'll use the trapezoid approximation.
This is the same as \`integrate()\`, except as an asynchronous generator so we can animate
the integration. Return values are of the form _[x, y, integral-so-far]_.
May also take a _range_ instead of _min_, _max_, _steps_.
* _steps_: The default is chosen for oversampling the screen resolution.
`(function* a_integrate(f, min, max, steps = 2 * width) {
if (typeof min === 'number') {
yield* a_integrate(f, range(min, max, steps));
} else if (min) {
const r = min;
function* pgen() {
for (const x of r) {
yield [x, f(x)];
}
}
yield* a_integrate(pgen());
} else {
const r = f;
let total = 0;
let v = r.next();
if (v.done) {
yield [0, 0, 0];
return;
}
let [xprevious, yprevious] = v.value;
for (const [x, y] of r) {
const mid = (y + yprevious) / 2;
const delta = x - xprevious;
total += mid * delta;
yprevious = y;
xprevious = x;
yield [x, y, total];
}
}
})