Polynomials

Greatest common divisors

Polynomials in several variables factor uniquely, so any two of them have a greatest common divisor. Finding it without factoring is what keeps fractions of polynomials in lowest terms.

Why a gcd exists

K[x1,…,xn]K[x_1, \dots, x_n] is a unique factorisation domain: every polynomial is a product of irreducible ones, uniquely up to order and constant factors. The gcd of ff and gg is the product of the irreducible factors they share, and it is determined up to a constant; the library returns the one whose leading coefficient is 1.

With one variable Euclid's algorithm finds it by repeated division with remainder. With several variables that fails as it stands: dividing x2+yx^2 + y by yx+1yx + 1 as polynomials in xx would need the coefficient 1/y1/y.

Contents and Gauss's lemma

Pick a main variable, say xx, and view ff as a polynomial in xx whose coefficients are polynomials in the other variables. Its content is the gcd of those coefficients and its primitive part is ff divided by the content:

(y2−1)x2+(y+1)x=(y+1)⏟content⋅((y−1)x2+x)⏟primitive part. (y^2 - 1)x^2 + (y + 1)x = \underbrace{(y + 1)}_{\text{content}} \cdot \underbrace{\big((y - 1)x^2 + x\big)}_{\text{primitive part}} .

Gauss's lemma says the product of primitive polynomials is primitive. It follows that

gcd⁡(f,g)=gcd⁡(cont⁡f,cont⁡g)⋅gcd⁡(pp⁡f,pp⁡g), \gcd(f, g) = \gcd(\operatorname{cont} f, \operatorname{cont} g) \cdot \gcd(\operatorname{pp} f, \operatorname{pp} g),

where the first gcd involves one variable fewer (so the method recurses) and the second can be found by a version of Euclid's algorithm.

Pseudo-remainders

To divide aa by bb in xx without fractions, first multiply aa by a power of the leading coefficient of bb. The pseudo-remainder rr satisfies

lc⁡(b)deg⁡a−deg⁡b+1 a=q b+r,deg⁡xr<deg⁡xb. \operatorname{lc}(b)^{\deg a - \deg b + 1}\, a = q\,b + r, \qquad \deg_x r < \deg_x b .

Replacing (a,b)(a, b) by (b,pp⁡r)(b, \operatorname{pp} r) until the remainder is zero gives the gcd of the primitive parts. Taking the primitive part at every step (the primitive remainder sequence) stops the coefficients from growing out of hand.

import { ring, gcd, lcm, content, primitivePart } from "@ratmath/structures";

const R = ring("x, y");
const f = R.poly("(y^2 - 1) x^2 + (y + 1) x");
String(content(f, "x"));                    // "y + 1"
String(primitivePart(f, "x"));              // "x²y − x² + x"
String(gcd(R.poly("x^3 - y^3"), R.poly("x^2 - y^2")));   // "x − y"
String(lcm(R.poly("x^2 - y^2"), R.poly("(x + y)^2")));   // "x³ + x²y − xy² − y³"
gcd([f, "x y + x", "x^2"]);                 // any number of polynomials: x

The same code runs over any exact field: modulo a prime, in GF(pᵏ), or over a number field.

Square-free decomposition

A polynomial is square-free when no irreducible factor is repeated. In characteristic 0 a repeated factor of ff is also a factor of its derivative: if f=pkhf = p^k h then f′=pk−1(kp′h+ph′)f' = p^{k-1}(k p' h + p h'). So gcd⁡(f,∂f/∂x)\gcd(f, \partial f/\partial x) collects the repeated factors, and Yun's algorithm uses a chain of such gcds to write

f=c⋅f1⋅f2 2⋅f3 3⋯ f = c \cdot f_1 \cdot f_2^{\,2} \cdot f_3^{\,3} \cdots

with every fkf_k square-free and no two sharing a factor. No factoring is needed, and it is the first step of factoring.

import { squareFree, squareFreePart } from "@ratmath/structures";

const g = R.poly("3 (x + y)^3 (x - y)^2 (x y + 1)");
squareFree(g);            // { unit: 3, factors: [{ factor: xy + 1, multiplicity: 1 },
                          //                      { factor: x − y,  multiplicity: 2 },
                          //                      { factor: x + y,  multiplicity: 3 }] }
String(squareFreePart(g));   // the product of the distinct factors
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