Algorithm Search⌘K

Proofs: Horner's rule

These are the human proofs of Horner's rule, at full length.

Horner's rule, synthetic division and the Taylor shift compute what they claim full

This proof shows: correctness.

Let RR be a commutative ring with 11, let f=∑i=0naixi∈R[x]f = \sum_{i=0}^{n} a_i x^i \in R[x] with n≥0n \ge 0 and an≠0a_n \ne 0, stored as the list a=[a0,…,an]a = [a_0, \ldots, a_n], and let u∈Ru \in R. For HORNER the point uu may also lie in an associative ring SS that contains the coefficients and in which uu commutes with them; the argument below uses only the ring axioms of SS.

Lemma 1 (HORNER). HORNER(a,u)(a, u) returns f(u)=∑i=0naiuif(u) = \sum_{i=0}^{n} a_i u^i, and HORNER([ ],u)([\,], u) returns 00.

Proof. The empty list is the zero polynomial, and line L1 returns 00. Let n≥0n \ge 0. We prove the invariant

(H) before the step of line L4 with index ii (for i=n−1,…,0i = n - 1, \ldots, 0), and after the loop with i=−1i = -1, s=∑j=i+1najuj−i−1s = \sum_{j=i+1}^{n} a_j u^{j-i-1}.

Initialization. Line L2 sets s=ans = a_n. For i=n−1i = n - 1 the sum in (H) is the single term anu0=ana_n u^0 = a_n.

Maintenance. Suppose (H) holds before the step with index ii. Line L4 computes

s⋅u+ai=(∑j=i+1najuj−i−1)u+ai=∑j=i+1najuj−i+aiu0=∑j=inajuj−i.s \cdot u + a_i = \Bigl(\sum_{j=i+1}^{n} a_j u^{j-i-1}\Bigr) u + a_i = \sum_{j=i+1}^{n} a_j u^{j-i} + a_i u^0 = \sum_{j=i}^{n} a_j u^{j-i}.

The middle step is distributivity and associativity of SS (with ajuku=ajuk+1a_j u^{k} u = a_j u^{k+1}). This is (H) for the index i−1i - 1.

Termination. The loop runs for i=n−1i = n - 1 down to 00 and stops; then (H) holds with i=−1i = -1, so s=∑j=0najuj=f(u)s = \sum_{j=0}^{n} a_j u^j = f(u), which line L5 returns. If n=0n = 0 the loop does not run and s=a0=f(u)s = a_0 = f(u). □\square

Lemma 2 (SYNTHETIC-DIVISION). Let n≥1n \ge 1. SYNTHETIC-DIVISION(a,u)(a, u) returns b=[b0,…,bn−1]b = [b_0, \ldots, b_{n-1}] and rr with

f=(x−u)∑j=0n−1bjxj+r,r=f(u),bn−1=an.f = (x - u) \sum_{j=0}^{n-1} b_j x^j + r, \qquad r = f(u), \qquad b_{n-1} = a_n.

Proof. Lines S1 to S4 define bn−1=anb_{n-1} = a_n, bi−1=biu+aib_{i-1} = b_i u + a_i for i=n−1,…,1i = n - 1, \ldots, 1, and r=b0u+a0r = b_0 u + a_0. Let q=∑j=0n−1bjxjq = \sum_{j=0}^{n-1} b_j x^j. Then

(x−u)q+r=∑j=0n−1bjxj+1−∑j=0n−1ubjxj+r.(x - u) q + r = \sum_{j=0}^{n-1} b_j x^{j+1} - \sum_{j=0}^{n-1} u b_j x^j + r.

Collect the coefficient of each power. For xnx^n it is bn−1=anb_{n-1} = a_n. For xix^i with 1≤i≤n−11 \le i \le n - 1 it is bi−1−ubi=(biu+ai)−ubi=aib_{i-1} - u b_i = (b_i u + a_i) - u b_i = a_i, by line S3 and commutativity of RR. For x0x^0 it is r−ub0=a0r - u b_0 = a_0, by line S4. So (x−u)q+r=f(x - u) q + r = f. The values bn−1,…,b0,rb_{n-1}, \ldots, b_0, r are exactly the successive values of ss in HORNER(a,u)(a, u) (line L2, then line L4 for i=n−1,…,0i = n - 1, \ldots, 0), so r=f(u)r = f(u) by Lemma 1. □\square

Lemma 3 (TAYLOR-SHIFT). TAYLOR-SHIFT(a,u)(a, u) returns c=[c0,…,cn]c = [c_0, \ldots, c_n] with f(x)=∑k=0nck(x−u)kf(x) = \sum_{k=0}^{n} c_k (x - u)^k. Equivalently, f(x+u)=∑k=0nckxkf(x + u) = \sum_{k=0}^{n} c_k x^k.

Proof. For 0≤k≤n0 \le k \le n write Pk(x)=∑j=0n−kc[k+j] xjP_k(x) = \sum_{j=0}^{n-k} c[k + j]\, x^j for the polynomial formed by the top part of the current list. We prove the invariant

(T) before round kk of the loop of line T2 (for k=0,…,nk = 0, \ldots, n, with k=nk = n meaning after the loop), f(x)=∑j<kc[j](x−u)j+(x−u)kPk(x)f(x) = \sum_{j<k} c[j] (x - u)^j + (x - u)^k P_k(x).

Initialization. For k=0k = 0, line T1 sets c=ac = a, so P0=fP_0 = f, the first sum is empty, and (T) reads f=ff = f.

Maintenance. Round kk runs line T4 for i=n−1i = n - 1 down to kk. Write pj=c[k+j]p_j = c[k + j] for the entries before the round, so Pk=∑j=0n−kpjxjP_k = \sum_{j=0}^{n-k} p_j x^j, and write c′c' for the entries after the round. The entry c[n]c[n] is not changed, so c′[n]=pn−kc'[n] = p_{n-k}. For ii from n−1n - 1 down to kk, line T4 sets c′[i]=c[i]+u⋅c′[i+1]c'[i] = c[i] + u \cdot c'[i + 1], because c[i+1]c[i + 1] was already updated (or is c[n]c[n]). With βj=c′[k+1+j]\beta_{j} = c'[k + 1 + j] for 0≤j≤n−k−10 \le j \le n - k - 1, this reads βn−k−1=pn−k\beta_{n-k-1} = p_{n-k}, βj−1=βju+pj\beta_{j-1} = \beta_j u + p_j for j=n−k−1,…,1j = n - k - 1, \ldots, 1, and c′[k]=β0u+p0c'[k] = \beta_0 u + p_0. These are the recurrences of SYNTHETIC-DIVISION applied to PkP_k, if n−k≥1n - k \ge 1. By Lemma 2, Pk=(x−u)Pk+1+c′[k]P_k = (x - u) P_{k+1} + c'[k], where Pk+1=∑jβjxjP_{k+1} = \sum_j \beta_j x^j is the polynomial formed by c′[k+1..n]c'[k+1..n], and c′[k]=Pk(u)c'[k] = P_k(u). Entries below kk are not touched. Substituting into (T):

f=∑j<kc′[j](x−u)j+(x−u)k((x−u)Pk+1+c′[k])=∑j<k+1c′[j](x−u)j+(x−u)k+1Pk+1,f = \sum_{j<k} c'[j](x - u)^j + (x - u)^k \bigl((x - u) P_{k+1} + c'[k]\bigr) = \sum_{j<k+1} c'[j] (x - u)^j + (x - u)^{k+1} P_{k+1},

which is (T) for k+1k + 1. (Lemma 2 is stated for the list aa, but its proof uses only the recurrences, so it applies to any list of length at least 2.)

Termination. The loop runs for k=0,…,n−1k = 0, \ldots, n - 1. Then (T) holds for k=nk = n, and Pn=c[n]P_n = c[n] is a constant. So f=∑j<nc[j](x−u)j+c[n](x−u)n=∑k=0nck(x−u)kf = \sum_{j<n} c[j](x - u)^j + c[n](x - u)^n = \sum_{k=0}^{n} c_k (x - u)^k. Substituting x+ux + u for xx, which is a ring homomorphism of R[x]R[x], gives f(x+u)=∑ckxkf(x + u) = \sum c_k x^k. If n=0n = 0, the loop does not run and c=[a0]c = [a_0], which is correct. □\square

Corollary (Taylor coefficients). If k!k! is a unit in RR, then ck=f(k)(u)/k!c_k = f^{(k)}(u)/k!, where f(k)f^{(k)} is the kk-th formal derivative. Proof. Differentiate f=∑jcj(x−u)jf = \sum_j c_j (x - u)^j formally kk times: the terms with j<kj < k vanish, the term j=kj = k gives k! ckk!\, c_k, and the terms with j>kj > k keep a factor (x−u)(x - u), which vanishes at x=ux = u. So f(k)(u)=k! ckf^{(k)}(u) = k!\, c_k. In particular c0=f(u)c_0 = f(u) and c1=f′(u)c_1 = f'(u) over every commutative ring. □\square

Theorem. HORNER, SYNTHETIC-DIVISION and TAYLOR-SHIFT are correct, by Lemmas 1, 2 and 3. HORNER uses exactly nn multiplications and nn additions for degree nn, because line L4 runs nn times; SYNTHETIC-DIVISION uses the same; TAYLOR-SHIFT runs line T4 ∑k=0n−1(n−k)=n(n+1)/2\sum_{k=0}^{n-1} (n - k) = n(n + 1)/2 times. ■\blacksquare

Back to Horner's rule