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
is a unique factorisation domain: every polynomial is a product of irreducible ones, uniquely up to order and constant factors. The gcd of and 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 by as polynomials in would need the coefficient .
Contents and Gauss's lemma
Pick a main variable, say , and view as a polynomial in whose coefficients are polynomials in the other variables. Its content is the gcd of those coefficients and its primitive part is divided by the content:
Gauss's lemma says the product of primitive polynomials is primitive. It follows that
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 by in without fractions, first multiply by a power of the leading coefficient of . The pseudo-remainder satisfies
Replacing by 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 is also a factor of its derivative: if then . So collects the repeated factors, and Yun's algorithm uses a chain of such gcds to write
with every 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
@ratmath/structures and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.