@ratmath/algebraic

Square roots, cube roots and every other root, held exactly

A JavaScript library for algebraic numbers: the roots of polynomials with whole-number coefficients. √2 + √3, the cube roots of 2, cos(π/7) and i are values you can add, multiply, compare and test for equality with no rounding, and print to as many digits as you like.

How a number is held

An algebraic number is stored as a polynomial with integer coefficients plus a small region that contains exactly one of its roots: an interval for a real root, a box in the complex plane for the others. The polynomial is the exact part; the region only says which root is meant, and it can be shrunk whenever more digits are wanted.

Arithmetic builds a new polynomial for the result (a resultant), factors it, and keeps the factor the answer is a root of, so every number carries its minimal polynomial. That is why the library can say exactly that (√2 + √3)² = 5 + 2√6, or that a messy expression is precisely 0.

A first program

import { sqrt, add, mul, fromRational, format, roots, cosPi, factor, galoisGroup } from "@ratmath/algebraic";

const r2 = sqrt(fromRational(2));
const s = add(r2, sqrt(fromRational(3)));
s.toString(20);                 // "3.14626436994197234232…"   certified digits
s.poly;                         // [1n, 0n, -10n, 0n, 1n]      x⁴ − 10x² + 1
format(mul(s, s));              // "5 + 2√6"
format(cosPi("1/5"));           // "(1 + √5)/4"
roots([-2, 0, 0, 1], { complex: true }).map((r) => format(r.value));
                                // ∛2 and its two complex partners
factor([-1, 0, 0, 0, 0, 0, 1]); // x⁶ − 1 into four irreducible factors
galoisGroup([-2, 0, 0, 0, 1]).name;  // "D4"

Polynomials are arrays of coefficients, constant term first: [-2, 0, 0, 1] is x³ − 2. Coefficients can be numbers, BigInts, strings like "3/4" or Core Rationals.

What is in it

  • Algebraic numbers, real and complex: arithmetic, roots of any order, exact comparison and equality, modulus, real and imaginary parts, readable exact forms such as (1 + √5)/2 and −1/2 + i√3/2.
  • Exact trigonometry: cos, sin and tan at rational multiples of π, roots of unity, cyclotomic polynomials.
  • Polynomials: factoring over the integers, resultants, discriminants, root counting, interpolation, partial fractions, Galois groups up to degree 7, with their subgroups and fixed fields.
  • Structures: number fields Q(α), finite fields GF(pᵏ), modular arithmetic. Continued fractions and Pell's equation are in @ratmath/numtheory.
  • Recognition: turn a decimal like 1.6180339887 back into (1 + √5)/2 with lattice reduction.

Topics

Each topic page explains the mathematics and lets you run it. Explore is a free-form calculator for all of it; Reference lists every function.

Install

bun add github:ratmath/algebraic    # depends on @ratmath/core

The library is pure JavaScript with BigInt arithmetic and no floating point in any answer. Floats appear only to find a first guess, which is then proven or discarded. @ratmath/linalg uses it for eigenvalues and exact fields.

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