Polar form

Polar form

Every nonzero complex number is reiθr e^{i\theta} with r=∣z∣>0r = |z| > 0 and an angle θ\theta. For a complex rational both are usually irrational, so they come as enclosures, with the exact facts alongside.

Modulus and argument

import { polar, modulus, argument, exactTurn, fromPolar } from "@ratmath/complex";

const p = polar("3 + 4i");
p.modulus;        // a Core RationalInterval: here the single point 5
p.argument;       // an interval around 0.9272952180…, width below 1e-19
p.exactModulus;   // Rational 5 (null when |z| is irrational)
p.normSquared;    // Rational 25
p.exactTurn;      // null: the angle is not a rational part of a turn

polar("1 + i").exactTurn;       // Rational 1/8
polar("1 + i").exactModulus;    // null: it is √2
modulus("1 + i", 200);          // √2 to 200 bits
argument("-1 - i");             // around −3π/4: principal values lie in (−π, π]

The argument is computed from an arctangent of y/xy/x or of x/yx/y, whichever is at most 1 in size, which keeps the series fast near the imaginary axis as well as near the real one.

When the angle is exact

The angle of a complex rational is a rational fraction of a full turn only on the axes and the two diagonals. That is Niven's theorem in another form: if θ/2π\theta/2\pi is rational and tan⁡θ\tan\theta is rational, then tan⁡θ\tan\theta is 00, ±1\pm1 or undefined. So exactTurn returns a multiple of 1/81/8 or null, and nothing is lost by the short list.

String(exactTurn("-1 + i"));   // "3/8"
String(exactTurn("-5"));       // "1/2"
exactTurn("1 + 2i");           // null

Back from polar form

fromPolar(r, θ) returns a rectangle around reiθr e^{i\theta}. The angle is in radians, as a rational or an interval; or { turns: q } for the exact angle 2πq2\pi q, which gives exact points on the axes.

String(fromPolar(2, { turns: "1/4" }));   // "2i", exactly
String(fromPolar(2, { turns: "1/8" }));   // "1.41421356237 + 1.41421356237i ± 8.5e-22"
String(fromPolar(1, "1/2"));              // e^(i/2): "0.87758256189 + 0.4794255386i ± 4.2e-22"
fromPolar(5, argument("3 + 4i")).contains("3 + 4i");   // true

Roots of unity and all the roots of a number

The nn-th roots of unity are e2πik/ne^{2\pi i k/n} for k=0,…,n−1k = 0, \dots, n - 1: the corners of a regular nn-gon. The four that are complex rationals (1,i,−1,−i1, i, -1, -i) come back as exact points. Every nonzero number has nn distinct nn-th roots: one of them times each root of unity.

import { rootsOfUnity, roots } from "@ratmath/complex";

rootsOfUnity(6).map(String);
// ["1", "0.5 + 0.86602540378i ± 6.4e-22", "-0.5 + 0.86602540378i ± 6.4e-22", "-1", …]
roots("-8", 3).map(String);
// ["1 + 1.73205080757i ± 1.3e-21", "-2 + 0i ± 2.5e-21", "1 - 1.73205080757i ± 1.3e-21"]

Notice that the first cube root of −8-8 listed is 1+3 i1 + \sqrt3\,i, not −2-2. The principal root is the one with the smallest angle, and for a negative number that is not the real one. Branches has more on this.

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