Number fields
Q(√2) is every number a + b√2 with rational a and b. Sums, products and quotients stay in that form, so arithmetic is just arithmetic on pairs of fractions. A number field Q(α) does the same for any algebraic α, and it is far cheaper than general algebraic arithmetic when all your numbers live in one field.
Polynomials modulo m
If m is the minimal polynomial of α, of degree n, then Q(α) = Q[x]/(m): every element is a polynomial in α of degree below n, and multiplication is polynomial multiplication followed by reduction modulo m (using m(α) = 0 to rewrite αⁿ). Inverses come from the extended Euclidean algorithm: since m is irreducible, any nonzero g has s·g + t·m = 1, so s is 1/g. Nothing here grows beyond n coefficients, unlike resultant arithmetic, whose degrees multiply.
Norm and trace
Multiplying by an element g is a linear map on the basis 1, α, …, α^(n−1). Its determinant is the norm N(g) and its trace is the trace Tr(g); both are rational, and they equal the product and the sum of the n conjugates of g. In Q(√d), N(a + b√d) = a² − db², and N(g·h) = N(g)·N(h). The units of the ring of integers are exactly the integral elements of norm ±1; the continued fraction page finds them for Q(√d).
In Q(i) the norm of a + bi is a² + b², which is why a prime p ≡ 1 mod 4 is a sum of two squares: it stops being prime in Z[i] and splits as (a + bi)(a − bi).
Embeddings
A field element is an abstract polynomial in α. toAlgebraic() turns it into an actual number by substituting a chosen root of m, the embedding. Q(√2) uses the positive root, Q(i) the root with positive imaginary part, and you can pick any root by index when making a field.
A field object
Every NumberField is also a plain field object { zero, one, add, sub, mul, div, neg, sign }, the interface that @ratmath/linalg's field-generic algorithms (Gaussian elimination, determinants, inverses) run on. So a matrix over Q(√2) is solved with the same code as one over Q.
Factoring over a field, and bigger fields
A polynomial that is irreducible over Q can split once the field grows: x⁴ + 1 has no rational factor, but over Q(√2) it is (x² + √2x + 1)(x² − √2x + 1). K.factor(p) finds such factors by Trager's method. Shift the polynomial by a multiple of α, multiply all its conjugates together (the norm) to get a rational polynomial, factor that over Q with the ordinary factoring code, and take a gcd with each rational factor to pull the pieces back into K.
The same norm gives new fields. If g is irreducible over K, the norm is the minimal polynomial of γ = β + sα for a root β of g, and Q(γ) is all of K(β). So K.extend(g) returns an ordinary NumberField together with the old generator and the new root written in γ. Repeating that until a polynomial has all its roots gives its splitting field (NumberField.splitting), and NumberField.containing([a, b]) builds the smallest field holding several exact numbers at once, which is the way to do fast closed arithmetic with √2 and √3 together.
In code
import { NumberField } from "@ratmath/algebraic";
const K = NumberField.quadratic(2); // Q(√2)
const a = K.of([1, 1]); // 1 + √2
a.inv().toString(); // "−1 + √2"
a.pow(5).toString(); // "41 + 29√2"
a.norm().toString(); // "-1"
a.trace().toString(); // "2"
a.toAlgebraic().toString(20); // "2.41421356237309504880…"
const G = NumberField.gaussian(); // Q(i)
G.of([1, 2]).mul(G.of([1, 2])).toString(); // "−3 + 4i"
const C = new NumberField([-2, 0, 0, 1], { name: "∛2" });
C.gen.pow(3).toRational().toString(); // "2"
K.factor([1, 0, 0, 0, 1]).factors.map((f) => f.factor.join(", ")); // ["1, √2, 1", "1, −√2, 1"]
K.roots([-8, 0, 1]).map((r) => r.value.toString()); // ["2√2", "−2√2"]
const S = NumberField.splitting([-2, 0, 0, 1]); // x³ − 2
S.degree; // 6
const { field, elements: [r2, r3] } = NumberField.containing([sqrt(fromRational(2)), sqrt(fromRational(3))]);
format(r2.mul(r3).toAlgebraic()); // "√6"
@ratmath/algebraic and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.