Green, Stokes and the divergence theorem
Each of the three theorems says that an integral over a region equals an integral over its boundary. greens, stokes and divergenceTheorem compute both sides, separately and with certified enclosures, and report whether the enclosures meet. The boundary is built from the description of the region, piece by piece, and each piece is returned.
const g = greens(["-y^3", "x^3"], ["x", "y"], { x: [0, 1], y: [0, 1] });
g.statement; // "∮ P dx + Q dy = ∬ (∂Q/∂x − ∂P/∂y) dA"
g.left.value; // [2, 2] the circulation, four sides added
g.left.pieces; // [{ side: "y = 0", curve: { x: "t", y: "0" }, value }, …]
g.right.value; // [2, 2] the double integral of 3x² + 3y²
g.agree; // true
Green's theorem
For a region in the plane with boundary traversed counter-clockwise,
The region is given the way a double integral is written: { x: [a, b], y: [lo, hi] }, where the bounds for y may be formulas in x. Its boundary is then four curves: the bottom from left to right, the right side upward, the top from right to left and the left side downward. A side of no length, such as the corner of a triangle, is left out.
// the triangle 0 ≤ y ≤ x ≤ 1
greens(["x*y", "x^2"], ["x", "y"], { x: [0, 1], y: [0, "x"] });
// the area of a region as a line integral: P = -y/2, Q = x/2 makes the right side ∬ 1 dA
greens(["-y/2", "x/2"], ["x", "y"], { x: [0, 1], y: ["x^2", "x"] }).left.value; // 1/6
// the flux form ∮ F·n ds = ∬ div F dA
greens(["x^2", "y^2"], ["x", "y"], { x: [0, 1], y: [0, 2] }, { flux: true });
Stokes' theorem
For a surface with boundary curve ,
The surface is a parametrisation over a region of the plane, and . The boundary is the image of the boundary of the parameter region, taken counter-clockwise there, which is the orientation that matches this normal.
// the paraboloid z = u² over the unit disc, in polar parameters
const s = stokes(["z", "x", "y"], ["x", "y", "z"],
{ x: "u*cos(v)", y: "u*sin(v)", z: "u^2" }, ["u", "v"], { u: [0, 1], v: [0, "2*pi"] });
s.left.value; // [3.14159247…, 3.14159283…]
s.right.value; // [3.14158913…, 3.14159623…] both hold π
s.right.normal; // the components of r_u × r_v
In that example two of the four sides are the same segment traversed in opposite directions ( and ) and one is the single point . Their contributions cancel or vanish, and the pieces show it.
The divergence theorem
For a solid with closed surface and outward normal ,
The solid is { x: [a, b], y: [c, d], z: [e, f] } with the bounds for y formulas in x and those for z formulas in x and y. Its surface is a top, a bottom and up to four walls. On the top the outward normal times the area element is , on a wall it is , and so on; each face is one double integral.
const d = divergenceTheorem(["x", "y^2", "z"], ["x", "y", "z"],
{ x: [0, 1], y: [0, "1 - x"], z: [0, "1 - x - y"] }); // a tetrahedron
d.left.pieces.map((p) => p.face); // top, bottom, and the walls; two walls have no height
d.left.value; // 5/12
d.right.value; // 5/12
For a surface that is easier to parametrise than to cut into graphs, pass the pieces yourself as { surface: [{ r, parameters, region, orientation }] }, with orientation: -1 where points inward.
What "agree" means
The two sides are computed by different integrals over different sets, so their enclosures are independent. The theorem says the two numbers are equal, hence the enclosures must have a point in common, and overlap is their intersection, which contains the common value. If they did not meet, either the hypotheses would be violated (a field that is not smooth on the region, a wrong orientation) or the library would have a bug. That makes these functions useful as checks of a hand calculation: compute one side yourself and see whether it lies in overlap.
| call | returns |
|---|---|
| greens([P, Q], [x, y], region, { digits = 8, flux }) | { statement, left, right, agree, overlap } |
| stokes(F, [x, y, z], surface, [u, v], region, { digits = 6 }) | the same shape; right also has curl, normal, integrand |
| divergenceTheorem(F, [x, y, z], solid, { digits = 5, surface }) | the same shape; left.pieces lists each face |
| left.value, right.value, overlap | RationalInterval |
See also change of variables, potentials and Lagrange multipliers, and the tools these are built on in Several variables.
@ratmath/analytic and Core's R, Rational, RationalInterval in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.