Rectangles and discs

Rectangles and discs

Most complex numbers worth computing are not rational. An enclosure is a set with rational data that is proven to contain the number: a rectangle with sides along the axes, or a disc. Arithmetic on enclosures gives enclosures of every possible result.

Rectangles

A Rect is a real interval for the real part and another for the imaginary part.

import { rect, Rect } from "@ratmath/complex";

rect([1, 2], [0, 1]);        // [1, 2] + i[0, 1]
rect("1 + i", "1/10");       // the square of half-width 1/10 around 1 + i
rect("3 - 4i");              // a single point

const r = rect("1 + i", "1/10");
String(r);                   // "1 + 1i ± 1e-1": the midpoint, then the half-size
String(r.center);            // "1 + i"
r.contains("21/20 + 19/20i");    // true
r.real;                      // a Core RationalInterval; r.imag too
r.corners();                 // four Complex values
r.split();                   // four quarters

The operations are add sub mul div inv neg conj scale pow, and hull, intersect, contains, intersects for sets. Each returns a rectangle containing z∘wz \circ w for every zz in the first and ww in the second:

([a]+i[b]) ([c]+i[d])=([a][c]−[b][d])+i ([a][d]+[b][c]) ([a] + i[b])\,([c] + i[d]) = ([a][c] - [b][d]) + i\,([a][d] + [b][c])

with real interval arithmetic on the right. Division multiplies by wˉ/∣w∣2\bar w / |w|^2 and refuses (code division-by-zero) when the divisor contains 0.

Reading a printed rectangle

A point prints exactly. Anything wider prints its midpoint to the digits asked for and then ± the half-size, so 1.4686939399 + 2.2873552872i ± 1.7e-21 means both parts are within 1.7×10−211.7\times10^{-21} of an exact rational midpoint whose leading digits are shown. toString(40) shows forty digits; toJSON() gives the four exact corners.

Discs

A Disc is a rational centre and a rational radius.

import { disc } from "@ratmath/complex";

const d = disc("1 + i", "1/10");
String(d.mul(disc("2", "1/10")));   // "disc(2 + 2i, 0.35142135624)"
String(disc("2", 1).inv());         // "disc(2/3, 0.33333333333)": exactly centre 2/3, radius 1/3
disc(0, 1).contains("3/5 + 4/5i");  // true, decided exactly
d.toRect();  r.toDisc();            // each contains the other shape

Two facts make discs pleasant. Inclusion and intersection are decided exactly from ∣c1−c2∣2|c_1 - c_2|^2 with no square roots, and the image of a disc under z↦1/zz \mapsto 1/z is again a disc, exactly:

1D(c,r)=D ⁣(cˉ∣c∣2−r2, r∣c∣2−r2)(∣c∣>r). \frac{1}{D(c, r)} = D\!\left(\frac{\bar c}{|c|^2 - r^2},\ \frac{r}{|c|^2 - r^2}\right) \qquad (|c| > r).

A product uses ∣zw−c1c2∣≤∣c1∣r2+∣c2∣r1+r1r2|zw - c_1c_2| \le |c_1| r_2 + |c_2| r_1 + r_1 r_2, with the moduli rounded upward.

Which to use

Rectangles fit the functions: eze^z, sin⁡z\sin z and log⁡z\log z separate into real functions of xx and yy, so their images of a rectangle are easy to bound tightly. That is why the functions in this package return rectangles. Discs fit multiplication: multiplying by a number of modulus 1 turns a disc without growing it, while a rectangle must be redrawn around its turned self and grows by up to 2\sqrt2 each time. The box at the end of this page shows a rectangle growing thousands of times over in twenty multiplications, while the disc grows only as much as twenty-one uncertain factors must.

Functions of an enclosure

Every function in Functions takes a rectangle or a disc as well as an exact number. The output then contains the value at every point of the input, so an uncertain measurement can be carried through a calculation and the uncertainty of the result read off at the end.

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