Contour integrals
The integral of a function along a path in the plane, returned as a rectangle that is proved to contain it. Cauchy's theorem and the residue theorem can then be watched to hold, to as many digits as wanted.
import { Contour, contourIntegral, residueSum } from "@ratmath/complex";
contourIntegral("z^2", Contour.path(["0", "1", "1 + i"])); // -2/3 + 2/3i, to 27 places
contourIntegral("1/z", Contour.circle(0, 1)); // 0 + 6.2831853072i ± 7.9e-17
contourIntegral("exp(z)/(z - 1/2)", Contour.circle(0, 1)); // 2πi·√e, Cauchy's formula
residueSum("exp(z)/z^3", Contour.circle(0, 1)); // 0.5 ± 3.3e-15
Contours
A contour is a chain of straight pieces and arcs of circles whose joints are exact complex rationals. A circle through rational points has plenty of them: if is any complex rational, lies on the unit circle. So a circle is cut into quarter turns at exact points, and can be cut again wherever the integration needs.
| call | returns |
|---|---|
| Contour.path(points), Contour.polygon(points) | Straight pieces through the points; the polygon closes back to the first |
| Contour.rectangle(rect) | The boundary, counter-clockwise |
| Contour.circle(center, radius, { clockwise }) | Once round, from centre + radius |
| Contour.arc(center, from, to, { clockwise }) | The arc between two points at the same distance from the centre |
| c.join(other), c.reversed() | One after the other; the same path backwards |
| c.closed, c.start, c.end, c.pieces | What it is |
| c.winding(w) | How many times a closed contour goes round w |
| c.sample(perPiece) | Points as doubles, for drawing |
A Rect or a Disc may be given wherever a contour is wanted, and means its boundary.
How the integral is proved
Cover a piece of the contour with a disc and build a Taylor model of the integrand on it. The model proves the integrand analytic on the disc, so the integral over the piece does not depend on its shape, only on its two ends:
where is the antiderivative of the model's polynomial, a polynomial too. An arc of at most a half turn lies inside the disc that has its chord as a diameter, so arcs cost nothing extra.
When the model cannot be built (the disc reaches a singularity) or its bound is too loose, the piece is halved and each half tried again. Pieces near a singularity become small and pieces far from one stay large. A singularity on the contour can never be covered, and the integral is refused with the place named.
Options
| call | returns |
|---|---|
| contourIntegral(f, contour, opts) | A Rect. tolerance is the width aimed for (default 2⁻⁴⁰); order and bits are those of the models (12 and 96); maxDepth and maxPieces limit the subdivision. |
| residueSum(f, contour, opts) | The integral round a closed contour divided by 2πi: the sum of the residues inside |
For a narrower answer raise order and bits together with the tolerance. The answer is always a true enclosure; if the tolerance cannot be met at the working precision, a wider rectangle is returned, never a wrong one.
A principal logarithm or root whose cut crosses the contour is refused, because it is not one analytic function along the path. Turn the cut out of the way: log(-z) + i*pi is a logarithm cut along the positive real axis.
@ratmath/complex and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.