Monomial orders and division
With one variable the terms of a polynomial line up by degree. With several there is a choice to make, and the choice decides what "the leading term" is and what a remainder looks like.
What an order must do
A monomial order is a way of comparing monomials that is
- total: any two different monomials are comparable,
- compatible with multiplication: if then ,
- a well-order: 1 is the smallest monomial, so no sequence of monomials decreases for ever.
The last property is what makes division stop. The three standard orders, with variables ranked :
- lex (lexicographic): compare the exponent of ; if equal, of ; and so on. So . It is the order that eliminates variables.
- grlex (graded lexicographic): larger total degree wins, ties are broken by lex.
- grevlex (graded reverse lexicographic): larger total degree wins; among equals, the monomial with the smaller power of the last variable is larger. So in grevlex, although in grlex. It is the default here because computations in it tend to stay smallest.
import { ring } from "@ratmath/structures";
const f = "x y^2 z + x^2 + y^3 + x z^2";
String(ring("x, y, z", { order: "lex" }).poly(f)); // "x² + xy²z + xz² + y³"
String(ring("x, y, z", { order: "grlex" }).poly(f)); // "xy²z + xz² + y³ + x²"
String(ring("x, y, z", { order: "grevlex" }).poly(f)); // "xy²z + y³ + xz² + x²"
// your own: any comparison of exponent lists, here lex with z ranked first
const zFirst = (a, b) => a[2] - b[2] || a[0] - b[0] || a[1] - b[1];
String(ring("x, y, z", { order: zFirst }).poly(f)); // "xz² + xy²z + x² + y³"
The division algorithm
Given and divisors , the algorithm writes
where no term of the remainder is divisible by any leading term . It works like long division: look at the leading term of what is left; if some divides it, subtract the right multiple of and record it in (taking the first that works); otherwise move that term to the remainder. Each step lowers the leading monomial, so the well-order guarantees an end.
With one divisor the remainder is zero exactly when it divides . With several divisors something new happens: the remainder can depend on the order in which the divisors are listed. In the last two examples is plainly a combination of the divisors, yet one listing leaves the remainder .
So a nonzero remainder does not show that lies outside the ideal generated by the divisors. The cure is to replace the divisors by a Gröbner basis of the same ideal, for which the remainder is unique. The page on Gröbner bases takes that up.
const R = ring("x, y", { order: "lex" });
const f = R.poly("x^2 y + x y^2 + y^2");
const { quotients, remainder } = f.divmod([R.poly("x y - 1"), R.poly("y^2 - 1")]);
quotients.map(String); // ["x + y", "1"]
String(remainder); // "x + y + 1"
f.rem([R.poly("y^2 - 1"), R.poly("x y - 1")]); // 2x + 1: the other listing
R.poly("x^3 + y^3").div("x + y"); // x² − xy + y² (exact division, or it throws)
@ratmath/structures and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.