Polar form
Every nonzero complex number is with and an angle . 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 or of , 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 is rational and is rational, then is , or undefined. So exactTurn returns a multiple of 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 . The angle is in radians, as a rational or an interval; or { turns: q } for the exact angle , 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 -th roots of unity are for : the corners of a regular -gon. The four that are complex rationals () come back as exact points. Every nonzero number has distinct -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 listed is , not . 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.
@ratmath/complex and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.