@ratmath/complex

Complex numbers that say which branch they took

Exact complex rationals, certified enclosures of everything that is not rational, and elementary functions that report what happens at a branch cut instead of silently picking a side. Quaternions, octonions and the rest of the doubling ladder are here too, exact.

What is here

Complex numbers with their functions; Möbius maps and the Riemann sphere; complex analysis with proofs (Taylor models, contour integrals, zeros, residues, the gamma and zeta functions); and the Cayley–Dickson numbers. Every phase of the roadmap for this package is built.

A first program

import { complex, exp, log, sqrt, polar } from "@ratmath/complex";

String(complex("(1 + 2i)/(3 - i)"));     // "1/10 + 7/10i", exactly
String(complex("(1 + i)^8"));            // "16"
String(sqrt("3 + 4i"));                  // "2 + i": exact when the root is rational

String(exp("1 + i"));                    // "1.4686939399 + 2.2873552872i ± 1.7e-21"
String(exp("1 + i", 200).toString(40));  // forty digits, still certified

String(log("-1"));
// on the cut (the negative real axis).  above: 0 + 3.1415926536i ± 1.7e-21;
// below: 0 - 3.1415926536i ± 1.7e-21.  Convention: above
String(log("-1", { side: "below" }));    // "0 - 3.1415926536i ± 1.7e-21"

Numbers are written as text ("3 - 4i", "1/2 + 0.#3i"), as a pair complex(3, -4), or as anything Core reads. The quaternions and their relatives are a separate entry point, so a program that only wants complex numbers does not load them:

import { quaternion, rotate, fourSquares } from "@ratmath/complex/cayley";

String(quaternion("1 + 2i").mul("j"));   // "j + 2k"
rotate("1 + k", [1, 0, 0]).map(String);  // ["0", "1", "0"]: a quarter turn about z, exactly
fourSquares(2026);                       // [23n, 23n, 22n, 22n]

Four ideas

Exact where the answer is rational. Sums, products, quotients and whole-number powers of complex rationals are complex rationals, and they are computed as such. So is 3+4i=2+i\sqrt{3 + 4i} = 2 + i, and so is any rotation by a rational quaternion.

Enclosed where it is not. e1+ie^{1+i} is not rational, so the answer is a small rectangle with rational corners that is proven to contain it. Ask for more bits and the rectangle shrinks. An uncertain input (a rectangle or a disc) gives a rectangle containing every possible value. See Rectangles and discs.

Branch cuts are reported. log⁡\log, roots, powers and the inverse trigonometric functions jump across a cut. When the input is on the cut, or an uncertain input lies across it, the result holds the value on each side and says so; a number is never returned from the wrong side by accident. See Branches.

Whole functions are enclosed too. A Taylor model is a polynomial with a proven bound on what it leaves out, valid on a whole disc. From it come contour integrals as rectangles, the number of zeros inside a contour as a proven whole number, residues, and real integrals with the steps shown. The zeros of the zeta function are found this way, each one proved to lie on the critical line.

Maps of the sphere are exact. A Möbius map with complex rational coefficients sends rational points to rational points and circles to circles, exactly, and stereographic projection onto the Riemann sphere is rational too.

The roadmap

From the library roadmap. All six phases are built.

PhaseContentsState
1Exact complex rationals, rectangles and discs, polar form, elementary functions with branch reportingbuilt
2Cayley–Dickson numbers, quaternion rotations, integer quaternionsbuilt
3Complex Taylor models; contour integrals; argument principle and zero isolationbuilt
4Residues, Laurent series, singularities, real integrals by residuesbuilt
5Möbius and conformal maps; graphics hooksbuilt (pictures in graphics)
6Complex gamma and zetabuilt

It builds on Core and on analytic, whose certified real exp, log, sine, cosine and arctangent are the only transcendental ingredients.

Try itEdit the code and run it with @ratmath/complex and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs