@ratmath/structures

Groups, rings and polynomials, held exactly

A JavaScript library for abstract algebra. Finite groups with their subgroups, quotients, Sylow subgroups, homomorphisms and actions. Polynomials in several variables with exact coefficients, their greatest common divisors, factors and resultants. Ideals and Gröbner bases, quotient rings, and systems of polynomial equations solved exactly. The integers mod n and quadratic integer rings with their ideals and class groups. Character tables with exact entries. Field extensions with their Galois groups, formulas in radicals, and what ruler and compass can reach. Nothing is rounded, so two answers are equal exactly when they print the same.

Groups

A group is a set with an associative product, an identity, and inverses: the symmetries of a shape, the shuffles of a deck, the invertible matrices over a finite field. This library holds a finite group by listing its elements, so everything a course asks of a group of up to a few thousand elements can be computed outright and checked: its conjugacy classes and centre, every subgroup and which are normal, quotients, Sylow subgroups, whether two groups are isomorphic, and the orbits of an action, with the orbit-counting lemma for colourings up to symmetry.

import { symmetric, dihedral, quaternion, isIsomorphic, identify, PSL, alternating } from "@ratmath/structures";

const S = symmetric(4);
String(S.classEquation());                 // "24 = 1 + 3 + 6 + 6 + 8"
S.normalSubgroups().map((N) => N.order);  // [1, 4, 12, 24]
[2, 3].map((p) => S.sylowCount(p));       // [3, 4]
identify(S.sylowSubgroup(2)).name;         // "D4"
isIsomorphic(dihedral(4), quaternion());   // false
isIsomorphic(PSL(2, 5), alternating(5));   // true
dihedral(6).natural.onColourings(2).countOrbits().count;   // 13 bracelets with 6 beads in 2 colours

Polynomials

A polynomial in several variables is a finite sum of terms, each a coefficient times a product of powers of the variables: x2y−32xz+1x^2y - \tfrac32 xz + 1. The polynomials in fixed variables with coefficients in a field KK form a ring, written K[x,y,z]K[x, y, z]: they can be added, subtracted and multiplied, and sometimes divided.

This library holds such a polynomial as the list of its nonzero terms, with every coefficient exact (a fraction of whole numbers by default). The coefficients can instead come from any exact field in RatMath: the integers modulo a prime, a finite field GF(pᵏ), or a number field such as Q(√2).

@ratmath/structures is the abstract algebra package of the RatMath Shelf. Representations beyond characters are planned for the same package.

Ideals and equations

A system of polynomial equations generates an ideal: everything that follows from the equations by adding and multiplying. A Gröbner basis of the ideal makes questions about the system mechanical. Does this equation follow from those? Are there solutions, and how many? What is left after eliminating a variable? When there are finitely many solutions, solve finds all of them as exact algebraic numbers.

import { ring, ideal, solve } from "@ratmath/structures";

const R = ring("x, y");
const I = ideal(R, ["x^2 + y^2 - 5", "x y - 2"]);
I.basis.map(String);                       // the reduced Gröbner basis
I.contains("x^4 - 5 x^2 + 4");             // true: it follows from the two equations
I.solve().real.map(String);                // ["(x = -2, y = -1)", "(x = -1, y = -2)", "(x = 1, y = 2)", "(x = 2, y = 1)"]
const A = I.quotientRing();                // Q[x, y]/I: arithmetic modulo the equations

Rings of numbers

The integers mod n show units, zero divisors, idempotents and the Chinese remainder theorem in a ring small enough to list. Quadratic integer rings such as Z[i]\mathbb Z[i] and Z[−5]\mathbb Z[\sqrt{-5}] show what happens to prime factorisation when a square root is adjoined: sometimes it survives, sometimes 6=2⋅3=(1+−5)(1−−5)6 = 2 \cdot 3 = (1 + \sqrt{-5})(1 - \sqrt{-5}), and then ideals and the class group take over.

Fields

A tower of field extensions is built one root at a time, and its automorphisms match subgroups with the fields in between. The Galois group of a polynomial decides whether it can be solved by radicals, and when it can the formula is produced and checked; the same theory says which regular polygons ruler and compass can draw. Conway polynomials give the finite fields a standard form, and Schreier–Sims handles permutation groups far too large to list.

A first program

import { ring, gcd, factor, resultant, fractions } from "@ratmath/structures";

const R = ring("x, y, z");                    // the ring Q[x, y, z]
const f = R.poly("x^3 + y^3 + z^3 - 3xyz");
String(f);                                    // "x³ + y³ − 3xyz + z³"
factor(f).factors.map((t) => String(t.factor));
                                              // ["x + y + z", "x² − xy + y² − xz − yz + z²"]
String(gcd(R.poly("x^3 - y^3"), R.poly("x^2 - y^2")));   // "x − y"
String(resultant(R.poly("x^2 + y^2 - 1"), R.poly("y - x^2"), "y"));
                                              // "x⁴ + x² − 1": where the circle meets the parabola
const F = fractions(R);                       // the field Q(x, y, z)
String(F.parse("1/(x + y) + 1/(x - y)"));     // "2x/(x² − y²)"

Text is read the way it is written on paper: 3xy^2, (x + 1)(x - 1), 1.25 x, 2/3 y. Numbers are exact, so 0.1 is one tenth.

What is in it

  • Named groups: cyclic, dihedral, dicyclic and quaternion, symmetric and alternating, GL, SL and PSL over Fp\mathbb F_p, Heisenberg groups, direct and semidirect products, groups from a table or any multiplication.
  • Inside a group: Cayley tables, element orders, conjugacy classes and the class equation, centre, centralisers, normalisers, commutators, derived and central series, solvable, nilpotent and simple tests.
  • Subgroups: every subgroup and its conjugacy class, normal subgroups, cosets, quotients, composition series, Sylow subgroups.
  • Maps: homomorphisms with kernels and images, the isomorphism theorems, isomorphism tests, automorphism groups, and the groups of order up to 20 by name.
  • Actions: orbits, stabilisers, fixed points, and the orbit-counting lemma for necklaces and coloured solids.
  • Presentations: groups from generators and relations, built by Todd–Coxeter coset enumeration.
  • Abelian groups and modules: the Smith normal form over the integers and over Q[x], the structure theorem, invariant factors and elementary divisors, and the rational canonical form of a matrix.
  • Polynomial rings over Q or any exact field, with the monomial orders lex, grlex and grevlex or one of your own.
  • Arithmetic and calculus: sums, products, powers, partial derivatives, gradients, substitution, evaluation, change of variables.
  • Division by one polynomial or by a list of them, with quotients and remainder.
  • Greatest common divisors, least common multiples, contents, square-free parts.
  • Factoring into irreducible polynomials over the rationals.
  • Determinants, resultants and discriminants of polynomials, plus Jacobians and Hessians.
  • Rational functions: the field of fractions, usable as the entries of a matrix with parameters.
  • Gröbner bases by Buchberger's algorithm over any exact field, with certificates: any member of an ideal written out in terms of the generators.
  • Ideals: membership, sums, products, intersections, quotients, saturation, elimination, radicals, dimension, prime and maximal tests.
  • Polynomial systems solved exactly: every solution with its multiplicity, coordinates as algebraic numbers.
  • Quotient rings with inverses, zero divisors, nilpotents, minimal polynomials, and the Chinese remainder theorem.
  • The integers mod n: units, zero divisors, idempotents, ideals, the unit group, primitive roots.
  • Quadratic integer rings: units and Pell's equation, Euclidean division, every factorisation of an element, prime ideals, class groups.
  • Symmetric polynomials: the elementary ones, power sums, and rewriting any symmetric polynomial in terms of them.

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/structures    # depends on @ratmath/core and @ratmath/algebraic

The library is pure JavaScript with BigInt arithmetic and no floating point anywhere.

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