Formulas and derivatives
Everything in the library that works with "a function" starts from a formula in calculator notation. fn(text) parses it into a small tree, and every question (a value, a range, an integral, zeros, a derivative) is answered by walking that tree in one of three ways.
The notation
| Write | Means |
|---|---|
+ − * / | the usual operations, left to right at equal precedence: x/2y is (x/2)·y |
^ or ** | powers; -x^2 is −(x²) |
2x, 2(x+1), x sin(x) | implicit multiplication |
sin x | a function applied to the next factor, the same as sin(x) |
sin cos tan asin acos atan exp log ln log1p sqrt cbrt sinh cosh tanh erf | the elementary functions (log and ln are both natural log) |
abs min max | non-smooth functions, modelled where they are smooth (see below) |
pi e | constants, certified to the working precision |
1/3, 0.25, 2.5E-3 | exact rationals: 0.25 is 1/4, not a double |
Three ways to walk the tree
- Floats.
compileFloatturns the tree into a JavaScript closure on doubles. This drives every preview, plot and slider: thousands of evaluations a frame. - Taylor models.
modelOfevaluates the tree with Taylor models in place of numbers, which gives polynomial-plus-remainder enclosures on a whole interval. Ranges, integrals and many-digit values use this. - Plain intervals.
encloseAstevaluates with intervals. It is cruder but cheap, and the zero finder uses it thousands of times to throw away pieces where f cannot vanish.
The tree is shared with @ratmath/graphics, so the formula a plot draws is literally the formula the proofs are about.
Symbolic d/dx
Derivatives are taken on the tree with the rules from a first calculus course, and the result is a new formula:
| Rule | d/dx |
|---|---|
| sum, product | u′ + v′, u′v + uv′ |
| quotient | (u′v − uv′)/v² |
| constant power | n·uⁿ⁻¹·u′ |
| general power | uᵛ·(v′·ln u + v·u′/u) |
| chain | sin u → cos u·u′, exp u → exp u·u′, log u → u′/u, atan u → u′/(1 + u²), sqrt u → u′/(2·sqrt u), … |
The builders simplify as they go: constants fold, 0·u and 1·u disappear, u − (−v) becomes u + v, and (uᵐ)ⁿ becomes uᵐⁿ. That keeps derivative trees small enough to evaluate quickly, which matters because the zero finder needs f′, f″ and sometimes f‴. Shared subtrees (sin(u) → cos(u)·u′ keeps the same u) are evaluated once.
fn("x^3 + 2x - 1/x").derivative().toString(); // "3*x^2 + 2 + 1/x^2"
format(differentiate(parse("atan(x^2)"), "x")); // "2*x/(1 + x^4)"
format(differentiate(parse("sin(x)/x"), "x")); // "(cos(x)*x - sin(x))/x^2"
Why not differentiate a Taylor model instead? Because the remainder only bounds f − P, not f′ − P′: f could wiggle inside the remainder band with any slope. The derivative is a new formula, modelled afresh with its own proven remainder.
Parameters, sliders and intervals
Names other than the variable are parameters. They must be given values, and with makes a copy with new values, which is what a slider does:
const f = fn("a*x^2 + b", { params: { a: "3/8", b: 1 } });
f.with({ a: 2 }).value(3).exact(); // 19, exactly
fn("y*t", { variable: "t", params: { y: 2 } }); // a different variable name
A parameter can also be an interval. Then every answer holds for every value of the parameter in that interval at once: the range below is a range over both x and a. This is how uncertainty in a measured constant carries through to a proven conclusion. The answer cannot be sharper than the parameter allows: here the range search stops at its piece budget, and the outer bound it reports is still a valid range for all a at once.
Non-smooth pieces
abs, min and max have no Taylor series at their corners. They are modelled exactly where they are smooth on the domain (the argument of abs keeps one sign, or one argument of min stays below the other) and refused where they are not, with a reason. Split the domain at the corner, or write the function piecewise, where each piece is smooth by construction.
Refusals
A formula that cannot be bounded on the domain is refused with an AnalyticError that has a code and a reason: log of a range that reaches 0, a divisor whose range contains 0, sqrt of something that may be negative, exp of something above 2¹⁶. The library never returns a bound it has not proven.
@ratmath/analytic and Core's R, Rational, RationalInterval in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.