Polynomial Division Algorithm

lean4-proofring-theoryvisualization
f=qg+r,degr<deggf = q \cdot g + r, \quad \deg r < \deg g

Statement

Let RR be a commutative ring and gR[x]g \in R[x] a monic polynomial (leading coefficient 11) of degree d1d \ge 1. For every fR[x]f \in R[x] there exist unique q,rR[x]q, r \in R[x] with

f=qg+randdegr<d (or r=0).f = q \cdot g + r \quad \text{and} \quad \deg r < d \text{ (or } r = 0\text{)}.

More generally, if the leading coefficient of gg is a unit in RR, the same division exists. Over a field, any non-zero gg suffices.

Visualization

Divide f(x)=x32x+1f(x) = x^3 - 2x + 1 by g(x)=x1g(x) = x - 1 (monic, degree 1).

         x^2 + x - 1
        _______________
x - 1 | x^3 + 0x^2 - 2x + 1
         x^3 -  x^2
         -----------
               x^2 - 2x
               x^2 -  x
               ---------
                    -x + 1
                    -x + 1
                    ------
                         0

Result: x32x+1=(x2+x1)(x1)+0x^3 - 2x + 1 = (x^2 + x - 1)(x - 1) + 0.

Verification: (x2+x1)(x1)=x3x2+x2xx+1=x32x+1(x^2 + x - 1)(x - 1) = x^3 - x^2 + x^2 - x - x + 1 = x^3 - 2x + 1. Quotient q=x2+x1q = x^2 + x - 1, remainder r=0r = 0.

Non-zero remainder example. Divide f=x4+1f = x^4 + 1 by g=x2+1g = x^2 + 1:

StepDividendMultiplierSubtract
1x4+1x^4 + 1x2(x2+1)x^2 \cdot (x^2+1)x4+x2\to x^4 + x^2
2x2+1-x^2 + 11(x2+1)-1 \cdot (x^2+1)x21\to -x^2 - 1

So x4+1=(x21)(x2+1)+2x^4 + 1 = (x^2 - 1)(x^2 + 1) + 2. Here r=2r = 2, degr=0<2=degg\deg r = 0 < 2 = \deg g.

Proof Sketch

  1. Existence by induction on degf\deg f. If degf<degg\deg f < \deg g, take q=0q = 0, r=fr = f. Otherwise, let f=axn+f = a x^n + \ldots with ndn \ge d and subtract axndga x^{n-d} g to reduce degree: faxndgf - a x^{n-d} g has degree <n< n. Induct.

  2. Monic hypothesis is used. When gg is monic, axndga x^{n-d} g has the same leading term as axna x^n, so subtracting strictly reduces degree. If gg were not monic (or its leading coeff not a unit), this step might fail over a general ring.

  3. Uniqueness. If f=qg+r=qg+rf = qg + r = q'g + r', then (qq)g=rr(q - q')g = r' - r. If rrr \ne r', then deg((qq)g)=deg(qq)+deggdegg>degrdeg(rr)\deg((q-q')g) = \deg(q-q') + \deg g \ge \deg g > \deg r \ge \deg(r'-r), a contradiction. So r=rr = r', hence q=qq = q'.

Connections

Polynomial division is the analogue for k[x]k[x] of the division algorithm in Z\mathbb{Z} that powers the Euclidean AlgorithmEuclidean Algorithmgcd(m,n)=gcd(nmodm,m)\gcd(m,n) = \gcd(n \bmod m, m)The oldest surviving algorithm, computing the greatest common divisor via iterated remaindersRead more →. It makes k[x]k[x] a Euclidean domain, explaining why k[x]k[x] is a PID (see Euclidean DomainEuclidean Domaina,b0,  q,r:  a=qb+r,  N(r)<N(b)\forall a, b \ne 0,\; \exists q, r:\; a = qb + r,\; N(r) < N(b)A domain with a division algorithm: any a, b give a = qb + r with r smaller than b under a norm function.Read more →). The remainder theorem — f(a)=0f(a) = 0 iff (xa)f(x-a) \mid f — is an immediate corollary.

Lean4 Proof

-- Mathlib: Polynomial.modByMonic_add_div
-- in Mathlib.Algebra.Polynomial.Div (line 259)
-- States: p %ₘ q + q * (p /ₘ q) = p  for monic q

open Polynomial in
/-- Division algorithm: for monic g, we have r + g * q = f
    where r = f %ₘ g and q = f /ₘ g. -/
theorem poly_division_algorithm {R : Type*} [CommRing R] (f g : R[X]) (hg : g.Monic) :
    f %ₘ g + g * (f /ₘ g) = f :=
  Polynomial.modByMonic_add_div f hg

/-- The degree of the remainder is less than the degree of the divisor. -/
theorem poly_remainder_degree {R : Type*} [CommRing R] [Nontrivial R]
    (f g : R[X]) (hg : g.Monic) :
    (f %ₘ g).natDegree < g.natDegree :=
  Polynomial.natDegree_modByMonic_lt f hg (Polynomial.Monic.ne_zero hg)