Algorithm Search⌘K

Proofs: Fast polynomial division

These are the human proofs of Fast polynomial division, at full length.

FAST-DIVMOD returns the quotient and remainder of division with remainder in O(M(n)) operations full

This proof shows: correctness.

Let RR be a commutative ring with 11, a,b∈R[x]a, b \in R[x] with b≠0b \ne 0 and lc⁡(b)\operatorname{lc}(b) a unit, n=deg⁡an = \deg a and m=deg⁡bm = \deg b after line L1. For ff with deg⁡f≤j\deg f \le j write rev⁡j(f)=xjf(1/x)\operatorname{rev}_j(f) = x^j f(1/x), a polynomial of degree at most jj whose coefficient of xix^i is the coefficient of xj−ix^{j-i} in ff.

Lemma 1 (uniqueness). There is at most one pair (q,r)(q, r) with a=qb+ra = qb + r and deg⁡r<m\deg r < m.

Proof. If qb+r=q′b+r′qb + r = q'b + r' with deg⁡r,deg⁡r′<m\deg r, \deg r' < m, then (q−q′)b=r′−r(q - q')b = r' - r. If q≠q′q \ne q', the leading coefficient of (q−q′)b(q - q')b is lc⁡(q−q′)lc⁡(b)\operatorname{lc}(q - q') \operatorname{lc}(b), which is nonzero because lc⁡(b)\operatorname{lc}(b) is a unit. So deg⁡((q−q′)b)≥m>deg⁡(r′−r)\deg((q - q')b) \ge m > \deg(r' - r), a contradiction. So q=q′q = q' and then r=r′r = r'. □\square

Existence of the pair follows from long division, which needs only that lc⁡(b)\operatorname{lc}(b) is a unit. It also follows from the theorem below, which constructs the pair.

Lemma 2 (reversal). Let n≥mn \ge m, and let q,rq, r satisfy a=qb+ra = qb + r with deg⁡q≤n−m\deg q \le n - m and deg⁡r≤m−1\deg r \le m - 1. Then

rev⁡n(a)=rev⁡n−m(q) rev⁡m(b)+xn−m+1rev⁡m−1(r),\operatorname{rev}_n(a) = \operatorname{rev}_{n-m}(q)\, \operatorname{rev}_m(b) + x^{n-m+1} \operatorname{rev}_{m-1}(r),

where rev⁡m−1(r)=0\operatorname{rev}_{m-1}(r) = 0 if m=0m = 0 (then r=0r = 0).

Proof. Substitute 1/x1/x for xx in a=qb+ra = qb + r in the ring R[x,1/x]R[x, 1/x] and multiply by xn=xn−m⋅xm=xn−m+1⋅xm−1x^n = x^{n-m} \cdot x^m = x^{n-m+1} \cdot x^{m-1}. This gives xna(1/x)=xn−mq(1/x)⋅xmb(1/x)+xn−m+1⋅xm−1r(1/x)x^n a(1/x) = x^{n-m} q(1/x) \cdot x^m b(1/x) + x^{n-m+1} \cdot x^{m-1} r(1/x). Each of the four factors of the form xjf(1/x)x^j f(1/x) has deg⁡f≤j\deg f \le j, so it is the polynomial rev⁡j(f)\operatorname{rev}_j(f). □\square

Lemma 3 (the reversed divisor is invertible). The constant term of rev⁡m(b)\operatorname{rev}_m(b) is lc⁡(b)\operatorname{lc}(b), a unit, so for each k≥1k \ge 1 there is a unique hh of degree less than kk with rev⁡m(b)⋅h≡1(modxk)\operatorname{rev}_m(b) \cdot h \equiv 1 \pmod{x^k}, and line L4 computes it.

Proof. The constant term is the coefficient of xmx^m in bb, which is lc⁡(b)\operatorname{lc}(b). Existence and uniqueness of hh, and the fact that NEWTON-INVERSE returns it, are Lemma 1 and the correctness theorem of the proof for Newton power series inversion. □\square

Theorem (correctness). FAST-DIVMOD(a,b)(a, b) returns the unique pair (q,r)(q, r) with a=qb+ra = qb + r and deg⁡r<deg⁡b\deg r < \deg b.

Proof. If a=0a = 0 or n<mn < m, line L2 returns (0,a)(0, a), which satisfies a=0⋅b+aa = 0 \cdot b + a with deg⁡a<m\deg a < m. By Lemma 1 it is the unique pair.

Let n≥mn \ge m and k=n−m+1≥1k = n - m + 1 \ge 1 (line L3). Let (q∗,r∗)(q^*, r^*) be the pair of long division, so a=q∗b+r∗a = q^* b + r^*, deg⁡r∗<m\deg r^* < m, and deg⁡q∗=n−m\deg q^* = n - m because deg⁡(q∗b)=deg⁡(a−r∗)=n\deg(q^* b) = \deg(a - r^*) = n and lc⁡(b)\operatorname{lc}(b) is a unit. By Lemma 2,

rev⁡n(a)≡rev⁡n−m(q∗) rev⁡m(b)(modxk).\operatorname{rev}_n(a) \equiv \operatorname{rev}_{n-m}(q^*)\, \operatorname{rev}_m(b) \pmod{x^k}.

Multiply both sides by the hh of line L4. By Lemma 3, rev⁡m(b)h≡1(modxk)\operatorname{rev}_m(b) h \equiv 1 \pmod{x^k}, so

rev⁡n(a) h≡rev⁡n−m(q∗)(modxk).\operatorname{rev}_n(a)\, h \equiv \operatorname{rev}_{n-m}(q^*) \pmod{x^k}.

Line L5 sets s=rev⁡n(a) mod xks = \operatorname{rev}_n(a) \bmod x^k, and sh≡rev⁡n(a)h(modxk)s h \equiv \operatorname{rev}_n(a) h \pmod{x^k}. So the tt of line L6, which has degree less than kk, is congruent to rev⁡n−m(q∗)\operatorname{rev}_{n-m}(q^*) modulo xkx^k. Both have degree less than kk, so t=rev⁡n−m(q∗)t = \operatorname{rev}_{n-m}(q^*), and line L7 gives q=rev⁡n−m(t)=q∗q = \operatorname{rev}_{n-m}(t) = q^*, since reversing twice with the same index is the identity on polynomials of degree at most n−mn - m.

If m=0m = 0, then r∗=0r^* = 0 and the algorithm returns (q∗,0)(q^*, 0). Otherwise line L8 sets u=q∗b mod xmu = q^* b \bmod x^m and line L9 sets r=(a mod xm)−u=(a−q∗b) mod xm=r∗ mod xm=r∗r = (a \bmod x^m) - u = (a - q^* b) \bmod x^m = r^* \bmod x^m = r^*, because deg⁡r∗<m\deg r^* < m. So the algorithm returns (q∗,r∗)(q^*, r^*). □\square

Theorem (cost). Assume that multiplying a polynomial with jj coefficients by one with at most jj coefficients costs at most M(j)M(j) ring operations, and that M(j)/jM(j)/j is nondecreasing. Let k=n−m+1k = n - m + 1. Then FAST-DIVMOD uses at most

4M(k)+M(m)+2⌈log⁡2k⌉M(k)k+O(n)4M(k) + M(m) + 2 \lceil \log_2 k \rceil \frac{M(k)}{k} + O(n)

ring operations, plus one inversion of lc⁡(b)\operatorname{lc}(b), and this is at most 4M(n+1)+o(M(n))+O(n)4M(n + 1) + o(M(n)) + O(n). With a precomputed inverse it uses at most M(k)+M(m)+O(n)M(k) + M(m) + O(n).

Proof. Lines L1, L3, L5, L7 and L9 touch each coefficient a bounded number of times: O(n)O(n). Line L4 costs at most 3M(k)+2⌈log⁡2k⌉M(k)/k+O(k)3M(k) + 2\lceil \log_2 k \rceil M(k)/k + O(k) by the cost theorem for Newton inversion. Line L6 multiplies two polynomials with kk coefficients: at most M(k)M(k). Line L8 needs only q mod xmq \bmod x^m, which has at most min⁡(k,m)\min(k, m) coefficients, times b mod xmb \bmod x^m, which has mm coefficients: at most M(m)M(m). Adding these gives the first bound, and dropping line L4 gives the bound with a precomputed inverse.

For the second bound, M(j)/jM(j)/j nondecreasing gives M(k)≤kk+mM(k+m)M(k) \le \frac{k}{k+m} M(k + m) and M(m)≤mk+mM(k+m)M(m) \le \frac{m}{k+m} M(k + m), so 4M(k)+M(m)≤4(M(k)+M(m))≤4M(k+m)=4M(n+1)4M(k) + M(m) \le 4(M(k) + M(m)) \le 4M(k + m) = 4M(n + 1). Also 2⌈log⁡2k⌉M(k)/k≤2⌈log⁡2(n+1)⌉M(n+1)/(n+1)2\lceil \log_2 k \rceil M(k)/k \le 2 \lceil \log_2 (n + 1) \rceil M(n + 1)/(n + 1), which is o(M(n+1))o(M(n + 1)) because log⁡n/n→0\log n / n \to 0. ■\blacksquare

Back to Fast polynomial division