Polynomials

Factoring

x³ + y³ + z³ − 3xyz = (x + y + z)(x² + y² + z² − xy − xz − yz). Factoring finds the irreducible pieces of any polynomial with rational coefficients, in any number of variables.

What the answer means

A polynomial is irreducible over Q when it is not a product of two polynomials of lower degree with rational coefficients. Every nonzero polynomial is a product

f=c⋅p1m1⋯pkmk f = c \cdot p_1^{m_1} \cdots p_k^{m_k}

of irreducible ones, uniquely up to order and sign. The library returns each pip_i with whole-number coefficients having no common factor, and puts the leftover rational number in unit.

The field matters. x2+y2x^2 + y^2 is irreducible over Q but equals (x+iy)(x−iy)(x + iy)(x - iy) once ii is allowed; x2−2y2x^2 - 2y^2 is irreducible over Q but splits over Q(√2). Curiously x2+y2−1x^2 + y^2 - 1, the circle, stays irreducible over every field of characteristic other than 2.

How it is done: Kronecker's substitution

Factoring in one variable is a solved problem (the Zassenhaus method in @ratmath/algebraic). Kronecker's idea reduces several variables to one.

  1. Remove repeated factors with the square-free decomposition, and make the coefficients whole numbers.
  2. Substitute. Choose dd larger than the degree of ff in every variable and replace x1→t,x2→td,x3→td2, … x_1 \to t, \quad x_2 \to t^{d}, \quad x_3 \to t^{d^2}, \ \dots A monomial x1a1x2a2x3a3x_1^{a_1} x_2^{a_2} x_3^{a_3} becomes ta1+a2d+a3d2t^{a_1 + a_2 d + a_3 d^2}: its exponents are the digits of one number in base dd. Distinct monomials with all exponents below dd therefore stay distinct, and the substitution can be undone.
  3. Factor the image f(t,td,td2,… )f(t, t^d, t^{d^2}, \dots) in the single variable tt.
  4. Recombine. Any true factor gg of ff has exponents below dd as well, so its image is a product of some of the one-variable factors. Try each product of 1, 2, … of them: read its exponents back in base dd and test whether the result divides ff. Whatever divides is a factor; if nothing does, ff is irreducible.

For f=x2−y2f = x^2 - y^2 take d=3d = 3: the image is t2−t6=−t2(t−1)(t+1)(t2+1)t^2 - t^6 = -t^2(t - 1)(t + 1)(t^2 + 1). The product t(t2+1)=t3+tt(t^2 + 1) = t^3 + t reads back as y+xy + x, which divides ff.

The method is simple and always right, but the image has degree about dnd^n, so it suits the polynomials of a course or a calculator rather than very large ones. The library refuses (with a clear error) when the image would pass degree 60,000.

import { ring, factor, isIrreducible } from "@ratmath/structures";

const R = ring("x, y");
const { unit, factors } = factor(R.poly("2/3 x^4 y - 2/3 y^5"));
String(unit);                                   // "2/3"
factors.map((t) => [String(t.factor), t.multiplicity]);
        // [["y", 1], ["x + y", 1], ["x − y", 1], ["x² + y²", 1]]
isIrreducible(R.poly("x^2 + y^2 - 1"));         // true
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