ESTRIN returns f(x) with n additions, n + ceil(log2(n + 1)) - 1 multiplications and depth at most 2 ceil(log2(n + 1)) full
This proof shows: correctness.
Let R be a commutative ring, a=[a0,…,an] with n≥0, f=∑k=0nakxk, and fix the point x∈R. Write ℓk for the length of the list c at the start of the k-th pass through line L3 (counting from k=0), and pk for the value of p then.
Lemma 1 (one round). Let c=[c0,…,cℓ−1] with ℓ≥2, let p∈R, and let c′ be the list built by lines L4 and L5. Then ∑ici′(p2)i=∑icipi, and c′ has ⌈ℓ/2⌉ entries.
Proof. For 0≤i<⌊ℓ/2⌋, ci′=c2i+c2i+1p, and ci′(p2)i=c2ip2i+c2i+1p2i+1. If ℓ is odd, the last entry is c(ℓ−1)/2′=cℓ−1, and cℓ−1(p2)(ℓ−1)/2=cℓ−1pℓ−1. Every index of c appears in exactly one of these terms, so the sums agree. The count is ⌊ℓ/2⌋, plus 1 if ℓ is odd, which is ⌈ℓ/2⌉. □
Theorem (correctness). ESTRIN(a,x) returns f(x).
Proof. We show that at the start of each pass through line L3, ∑icipi=f(x). At the first pass, c=a and p=x by lines L1 and L2, so the sum is f(x). If the loop body runs, then c has ℓ≥2 entries, and by Lemma 1 the new list c′ satisfies ∑ici′(p2)i=f(x). Line L6 sets c←c′. If c′ has more than one entry, line L7 sets p←p2 and the invariant holds. If c′ has one entry, p is not updated, but then ∑ici′qi=c0′ for every q, so the invariant holds as well. The length goes from ℓ to ⌈ℓ/2⌉<ℓ for ℓ≥2, so the loop ends, with one entry c0=∑icipi=f(x), which line L8 returns. For an empty list the implementation returns 0=f(x). □
Theorem (cost). For n≥1, let L=⌈log2(n+1)⌉. ESTRIN uses exactly n additions and n+L−1 multiplications, and its depth, the longest chain of operations each of which uses the result of the one before, is at most 2L.
Proof. Rounds. We have ℓ0=n+1 and ℓk+1=⌈ℓk/2⌉. By induction, ℓk=⌈(n+1)/2k⌉, because ⌈⌈m/2k⌉/2⌉=⌈m/2k+1⌉ for positive integers m. So ℓk=1 exactly when 2k≥n+1, that is k≥L, and the loop body runs L times.
Additions and multiplications in line L4. A round on a list of length ℓ does ⌊ℓ/2⌋=ℓ−⌈ℓ/2⌉ additions and as many multiplications. Summed over the rounds this telescopes to ℓ0−ℓL=(n+1)−1=n.
Squarings. Line L7 squares after each round except the last, where the new list has one entry: L−1 squarings.
Depth. Let δk be the largest depth of an entry of c at the start of round k, and let πk be the depth of pk. Then δ0=0 and π0=0, since the inputs have depth 0. Line L7 gives πk+1=πk+1, so πk=k. Line L4 gives δk+1≤max(δk,πk)+2=max(δk+2,k+2), since an entry c2i+c2i+1p needs one multiplication after c2i+1 and p are known, and one addition after that. By induction, δk≤2k: it holds for k=0, and δk+1≤max(2k+2,k+2)=2(k+1). After L rounds the depth of the result is at most 2L. ■