Fast, then certified
Certified answers cost more than floating-point ones, sometimes a thousand times more. Someone dragging a slider needs a picture now; someone writing down a result needs it to be true. So every question returns a Refinement that can answer both ways. It is the same class the RatMath linear algebra and graphics libraries use, so one loop can drive all three.
const job = fn("exp(sin(3x))*x").range(R`0:3`, { digits: 6 });
job.quick; // { stage: "float", value: [lo, hi], trust: "rough" }
for (const s of job.stages()) … // float, then progress ticks, then the certified stage
job.exact(); // run to the end now (fine in a Worker)
await job.settle({ signal, sliceMs: 8, onProgress }); // the same, in time slices
A worked run
The range above, run through stages() by hand, yields 21 stages:
| stage | what it holds |
|---|---|
| float | [0, 7.1735…] from 256 samples, trust rough. The upper end is a sampled value, so the true maximum may be a little higher. |
| progress 1 of 256 | partial: a certified range from one Taylor model on all of [0, 3], from about −1.5·10⁹ to 1.5·10⁹. Valid, and useless. |
| progress 2 … 19 | partial: the outer bound tightens as the pieces holding the extremes are split; by the second tick it is already about [−186, 196] |
| exact | an interval from just below 0 to 7.17352…, via "Taylor models (order 8) on 25 pieces" |
Every partial is a true enclosure, so a display may show it as soon as it arrives. of is the budget (maxPieces), not a forecast: this run stopped after 19 of 256 because outer and inner bounds met.
What each question previews
| question | preview | trust |
|---|---|---|
| f.value(x) | the float formula at x | good, or unsure if not finite |
| f.range(I) | min and max of 256 samples | rough |
| f.integral(a, b) | 16-panel Gauss–Legendre | good (rough for piecewise) |
| zeros, extrema, inflections | { kind: "guess", at } from sign changes of f, f′ or f″ on samples | rough |
| p.breaks | none | unsure |
| ode.solve | RK4 samples { t, y } on 200 steps | good, or unsure if it blows up |
How far to trust a preview
trust is good when the float computation is normally accurate (a value, an integral by Gauss–Legendre), rough when it can miss things (a range from samples can miss a narrow peak; zero guesses come from sign changes, so a double root is invisible), and unsure when the float answer is not finite.
Progress and partial answers
Progress ticks look like { stage: "progress", done, of, step, partial }. partial is the answer so far: a range that is already valid but still wide, an integral over the pieces done, the ODE steps validated so far, the zeros found so far. A display can sharpen as they arrive.
Sliders: live
const f = fn("a*sin(x)", { params: { a: 1 } });
const update = live((a) => f.with({ a }).value("1/2"), (stage) => show(stage.value, stage.exact), { idleMs: 30 });
slider.oninput = () => update(`${slider.value}/8`);
Each update shows the preview at once. Certified work starts once the input has been still for idleMs, runs in slices, and is cancelled by the next update, so only the latest question is ever finished.
Your own refinement
A Refinement is two functions: one that returns the preview, and a generator that yields progress and returns the certified answer. Anything built this way gets stages, exact, settle and live for free.
import { Refinement, fn } from "@ratmath/analytic";
const f = fn("exp(-x^2)");
const area = new Refinement(
() => ({ value: 0.8820813907, trust: "good" }), // erf(2)·√π/2 from a table
function* () {
const parts = [];
for (let k = 0; k < 4; k++) {
parts.push(f.integral(`${k}/2`, `${k + 1}/2`, { digits: 15 }).exact());
yield { done: k + 1, of: 4, step: "piece", partial: parts.length };
}
return { value: parts.reduce((a, b) => a.add(b)), via: "four certified integrals" };
},
);
const total = await area.settle({ onProgress: (p) => console.log(p.done, "/", p.of) });
Cancelling is cooperative: settle checks the signal between slices, so a generator that yields often stops quickly. A generator that never yields runs to the end in one slice.
@ratmath/analytic and Core's R, Rational, RationalInterval in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.