Polynomials

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.

One polynomial, three orders

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 xa>xbx^a > x^b then xaxc>xbxcx^a x^c > x^b x^c,
  • 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 x>y>zx > y > z:

  • lex (lexicographic): compare the exponent of xx; if equal, of yy; and so on. So x>y100x > y^{100}. 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 xz<y2xz < y^2 in grevlex, although xz>y2xz > y^2 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 ff and divisors g1,…,gkg_1, \dots, g_k, the algorithm writes

f=q1g1+⋯+qkgk+r, f = q_1 g_1 + \dots + q_k g_k + r ,

where no term of the remainder rr is divisible by any leading term LT⁡(gi)\operatorname{LT}(g_i). It works like long division: look at the leading term of what is left; if some LT⁡(gi)\operatorname{LT}(g_i) divides it, subtract the right multiple of gig_i and record it in qiq_i (taking the first gig_i 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 ff. With several divisors something new happens: the remainder can depend on the order in which the divisors are listed. In the last two examples xy2−x=x(y2−1)xy^2 - x = x(y^2 - 1) is plainly a combination of the divisors, yet one listing leaves the remainder −x−y-x - y.

So a nonzero remainder does not show that ff 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)
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