The operations on dense coefficient lists are exact and keep the lists normalized full
This proof shows: correctness.
Let R be a commutative ring with 1. For a list a=[a0,…,an−1] of elements of R we write ∣a∣=n and val(a)=∑i<naixi, and we set ai=0 for i≥n. A list is normalized when it is empty or its last entry is nonzero.
Lemma 0 (uniqueness). For each f∈R[x] there is exactly one normalized list a with val(a)=f, and then ∣a∣−1=degf (with −1 for f=0).
Proof. Two lists with the same value have the same entries at each position below the shorter length, and the longer one has only zero entries above it, because the coefficients of a polynomial are unique. If both lists are normalized, the longer one would end in a zero entry, unless the lengths are equal. So the lists are equal. The list of the coefficients of x0,…,xdegf is normalized and has the value f. □
Lemma 1 (NORMALIZE). NORMALIZE(c) returns a normalized list with the same value as c.
Proof. The loop of lines L1 and L2 removes only last entries that are zero, and removing a top zero entry does not change ∑cixi. It stops when the list is empty or its last entry is nonzero, which is the definition of normalized. It stops after at most ∣c∣ steps, since each step shortens the list. □
Lemma 2 (ADD). For normalized a and b, ADD(a,b) returns the normalized list of val(a)+val(b).
Proof. Let N=max(∣a∣,∣b∣). After line L6 has run for i=0,…,N−1, ci=ai+bi for each i<N, with the convention ai=0 for i≥∣a∣. Both operands have no terms of degree N or more, so val(c)=∑i<N(ai+bi)xi=val(a)+val(b). Line L7 and Lemma 1 finish the proof. □
Lemma 3 (MUL). For normalized a and b, MUL(a,b) returns the normalized list of val(a)val(b).
Proof. If ∣a∣=0 or ∣b∣=0, one factor is the zero polynomial, the product is zero, and line L8 returns the empty list. Otherwise let m=∣a∣, n=∣b∣. We show the invariant
(M) before row i of the loop of line L10, ck=∑i′<i, 0≤k−i′<nai′bk−i′ for each 0≤k≤m+n−2.
Initialization. For i=0 the sum is empty, and line L9 sets every ck to 0.
Maintenance. Row i runs line L12 for j=0,…,n−1 and adds aibj to ci+j. Each index k gets the term with i′=i and k−i′=j exactly when 0≤k−i<n, which is the new term of (M) for i+1. The index i+j≤(m−1)+(n−1) is in range.
Termination. After row m−1, (M) with i=m gives ck=∑i+j=kaibj, the coefficient of xk in the product. Every pair (i,j) with i<m, j<n has i+j≤m+n−2, so no term of the product is lost. So val(c)=val(a)val(b), and Lemma 1 gives a normalized result. □
Lemma 4 (EVALUATE). EVALUATE(a,u) returns val(a)(u)=∑j<majuj.
Proof. Let m=∣a∣. We show that at the test of line L15 for the index i (counting down from m−1 to −1), s=∑j=i+1m−1ajuj−i−1. For i=m−1 the sum is empty and line L14 sets s=0. If it holds for i, then line L16 sets s⋅u+ai=∑j=i+1m−1ajuj−i+ai=∑j=im−1ajuj−i, which is the claim for i−1. When the loop ends, i=−1, and s=∑j=0m−1ajuj. This uses that R is commutative only through the identity (∑cjuj)u=∑cjuj+1, which holds in every associative ring. □
Lemma 5 (DIVMOD). Let a and b be normalized, n=∣b∣≥1, and let bn−1 be a unit of R. Then DIVMOD(a,b) returns normalized lists q and r with val(a)=val(q)val(b)+val(r) and ∣r∣≤n−1, that is degr<degb. These q and r are the only polynomials with these properties.
Proof. Let m=∣a∣. If m<n, the loop of line L20 does not run, q is empty, r=a has at most n−1 entries, and val(a)=0⋅val(b)+val(a). Let m≥n. Write Qk=∑j>kqjxj for the part of the quotient that is set before step k (so Qm−n is 0). We show the invariant
(D) before step k of the loop of line L20, val(a)=Qkval(b)+val(r) and rj=0 for each j≥k+n.
Initialization. For k=m−n, line L18 sets r=a and Qk=0, and rj=0 for j≥m because ∣r∣=m.
Maintenance. Step k sets t=rk+n−1v (line L21), stores qk=t (line L22), and subtracts txkval(b) from val(r) in lines L23 and L24: the entry rk+j loses tbj for j=0,…,n−1. Then Qk−1=Qk+txk, and
Qk−1val(b)+val(rnew)=Qkval(b)+txkval(b)+val(rold)−txkval(b)=val(a). The entry at k+n−1 becomes rk+n−1−tbn−1=rk+n−1−rk+n−1vbn−1=0, since vbn−1=1. The entries at j≥k+n are not touched and stay zero. So rj=0 for j≥(k−1)+n, which is (D) for k−1.
Termination. After step k=0, (D) holds with Q−1=val(q) and rj=0 for j≥n−1. So val(r)=val(r0,…,rn−2), which is what line L25 keeps, and val(a)=val(q)val(b)+val(r[0..n−2]). Lemma 1 normalizes both lists without a change of value, and the remainder list has at most n−1 entries.
Uniqueness. Suppose val(a)=qb+r=q′b+r′ with degr,degr′<degb=n−1. Then (q−q′)b=r′−r. If q=q′, the top coefficient of (q−q′)b is lc(q−q′)bn−1, which is nonzero because bn−1 is a unit (if cbn−1=0 then c=cbn−1v=0). So deg((q−q′)b)≥n−1>deg(r′−r), a contradiction. Hence q=q′ and then r=r′. By Lemma 0 the normalized lists are also unique. □
Lemma 6 (derivative and scalar multiple). The list [1⋅a1,…,(n−1)⋅an−1] has the value ∑i≥1iaixi−1, the formal derivative, and [sa0,…,san−1] has the value sval(a). After NORMALIZE both are normalized by Lemma 1. □
Theorem. Each operation of the dense representation takes normalized lists to a normalized list (or pair of lists) whose value is the exact result in R[x], and by Lemma 0 the length of each result is its degree plus one.
Proof. Lemmas 2 to 6, with subtraction as ADD of a and the list [−b0,…,−bn−1], which is normalized because −bn−1=0 when bn−1=0. ■