Topic

Several variables

A field is a formula in several named variables. It answers the questions of multivariable calculus the same way fn does in one variable: a quick Float64 preview, then an enclosure with a proof.

const f = field("x^2*y + sin(x*y)", ["x", "y"]);
f.gradient();                       // [2*x*y + cos(x*y)*y, x^2 + cos(x*y)*x], as fields
f.jet([1, "1/2"]);                  // value, gradient and Hessian as intervals
f.integral({ x: [0, 1], y: [0, 1] }).exact();
f.range("0:x:1 && 0:y:x").exact();  // over a triangle
f.criticalPoints({ x: [-2, 2], y: [-2, 2] }).exact();

Partials, gradients, tangent planes

partial, gradient, hessian and laplacian are symbolic and return fields. jet(point) evaluates the value, the gradient and the Hessian together with interval arithmetic; given a box instead of a point, it encloses them over the whole box. Where the formula has a kink inside the box (abs, min, a square root at 0), smooth is false and no derivative is reported. directional(point, direction) and linearization(point) build on the same pieces.

Regions

Integrals and ranges take a region in one of three forms.

  • A box: { x: [0, 1], y: [0, 2] }.
  • Iterated bounds: { x: [-1, 1], y: ["-sqrt(1-x^2)", "sqrt(1-x^2)"] }, each bound a formula in the variables before it. This is the usual way to write a double integral, and it covers curved sides.
  • A region in Core's notation: "0:x:_ && 0:y:_ && _:y:4-x && _:y:3-x/2", or a MultiRegion from @ratmath/core. Core cuts it into cells with one lower and one upper bound per variable, and each cell becomes iterated bounds. Polygons and polytopes are therefore integrated exactly when the integrand is a polynomial of low degree.

Each piece is mapped to a box by y=lo+s (hi−lo)y = \text{lo} + s\,(\text{hi} - \text{lo}), which multiplies the integrand by hi−lo\text{hi} - \text{lo}.

How an integral is proven

On a box, the product Boole rule (five points a side) is exact for polynomials up to degree five in each variable. Its error on a side of width w is at most w7max⁡∣∂6f∣/1935360w^7 \max|\partial^6 f| / 1935360, so the whole error is at most

vol⋅∑iwi6 ∣c6,i∣2688,c6,i∋16!∂6f∂xi6 over the box. \mathrm{vol} \cdot \sum_i \frac{w_i^6\,|c_{6,i}|}{2688}, \qquad c_{6,i} \ni \frac{1}{6!}\frac{\partial^6 f}{\partial x_i^6} \text{ over the box.}

The Taylor coefficients c6,ic_{6,i} come from one pass of interval Taylor arithmetic in each direction, done in fixed point. Simpson's rule with the fourth derivative is tried as well, and the smaller bound wins. Boxes wait in a heap ordered by the width of their enclosure, and the widest is halved along the side that contributes most. Where the integrand is not smooth on a box, the plain bound (volume times range) is used, which still converges as the boxes shrink.

A cylinder of radius 1 and a sphere show the cost of curved sides: the area of the unit disc to six digits takes about 0.2 s, because the square root is not smooth at the ends of the x range.

Ranges and critical points

range is branch and bound: each box gets an enclosure from the mean value form f(m)+∇f(B)⋅(B−m)f(m) + \nabla f(B)\cdot(B - m), boxes that cannot hold the minimum or the maximum are dropped, and the rest are halved.

criticalPoints looks for zeros of the gradient. A box where some partial derivative has one sign is dropped. On the others an interval Newton step (Krawczyk's operator, with the interval Hessian as the Jacobian) is tried: if its image lies inside the box, there is exactly one critical point there, and repeating the step narrows it. The kind comes from the signs of the Hessian's pivots, by Sylvester's law of inertia, in interval arithmetic: all positive is a minimum, all negative a maximum, mixed a saddle. A saddle like x·y, whose diagonal entries vanish, is settled with a 2×2 pivot. Where the Hessian may be singular the point is degenerate. A curve of critical points (x²y² along the axes) cannot be isolated; it comes back as one possible record around the whole set.

Vector calculus

divergence, curl and jacobian are symbolic. lineIntegral(F, variables, curve, [a, b]) substitutes the curve and integrates in one variable; with one expression in place of the list it is the integral with respect to arc length. surfaceIntegral(F, variables, surface, [u, v], region) is the flux of F through a parametrised surface, or with one expression the integral over its area. Surface areas of curved surfaces are slow (tens of seconds for a sphere to four digits), because the integrand is a square root of a sum of squares and interval arithmetic does not see that sin² + cos² = 1.

callreturns
field(expr, variables, { params })ScalarField
f.partial(...names), f.gradient(), f.hessian(), f.laplacian()fields (symbolic)
f.jet(point or box, { digits, order }){ value, gradient, hessian, smooth }
f.value(point), f.directional(point, direction)R RationalInterval
f.linearization(point), f.substitute({ y: 2 }), f.along(curve)field or function
f.integral(region, { digits = 6, maxBoxes = 4000 })R RationalInterval
f.range(region, { digits })R RationalInterval
f.criticalPoints(box, { digits, maxBoxes })R records { kind, unique, point, value }
divergence, curl, jacobian, lineIntegral, surfaceIntegral
Try itEdit the code and run it with @ratmath/analytic and Core's R, Rational, RationalInterval in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs