Algorithm Search⌘K

Proofs: Pairwise summation

These are the human proofs of Pairwise summation, at full length.

Pairwise summation satisfies the gamma of ceil(log2 n) error bound full

This proof shows: correctness.

Setting. A floating-point system with unit roundoff uu in which every addition satisfies the standard model fl(a+b)=(a+b)(1+δ)\mathrm{fl}(a + b) = (a + b)(1 + \delta), ∣δ∣≤u|\delta| \le u. This holds for round to nearest with u=12β1−pu = \tfrac12\beta^{1-p} and for directed rounding with u=β1−pu = \beta^{1-p}, as long as the result does not overflow; an addition that underflows is exact, so it satisfies the model with δ=0\delta = 0. Write γh=hu/(1−hu)\gamma_h = hu/(1 - hu) for hu<1hu < 1.

Lemma 1 (Higham, Lemma 3.1). If ∣δj∣≤u|\delta_j| \le u and ρj=±1\rho_j = \pm 1 for j=1,…,hj = 1, \ldots, h, and hu<1hu < 1, then ∏j=1h(1+δj)ρj=1+θh\prod_{j=1}^h (1 + \delta_j)^{\rho_j} = 1 + \theta_h with ∣θh∣≤γh|\theta_h| \le \gamma_h. Only the case ρj=1\rho_j = 1 is needed here, and for it the proof is short: ∣∏(1+δj)−1∣≤(1+u)h−1|\prod(1 + \delta_j) - 1| \le (1 + u)^h - 1, and (1+u)h≤1/(1−u)h≤1/(1−hu)(1 + u)^h \le 1/(1 - u)^h \le 1/(1 - hu), the last step by Bernoulli's inequality (1−u)h≥1−hu(1 - u)^h \ge 1 - hu. So (1+u)h−1≤hu/(1−hu)=γh(1 + u)^h - 1 \le hu/(1 - hu) = \gamma_h.

The tree. A run of PAIRWISE-SUM on x1,…,xnx_1, \ldots, x_n, n≥1n \ge 1, defines a binary tree: a call with n≤bn \le b terms is a chain in which the terms are added one by one, and a call with n>bn > b terms is a node whose two children are the calls on the two halves. Every addition (line L4 or line L9) is an internal node with two inputs. For a term xix_i, let hih_i be the number of additions on the path from xix_i to the root, and let h=max⁡ihih = \max_i h_i.

Lemma 2 (the computed value). If no addition overflows and hu<1hu < 1, the value returned by any call on a range RR is ∑i∈Rxi∏j∈Pi(1+δj)\sum_{i \in R} x_i \prod_{j \in P_i} (1 + \delta_j), where PiP_i is the set of additions on the path from xix_i to the root of that call, and ∣δj∣≤u|\delta_j| \le u.

Proof. By induction on the size of the call. A single term is returned unchanged, and the product over the empty path is 11. For a chain in lines L3 and L4, after the step that adds xkx_k the value is fl(s+xk)=(s+xk)(1+δ)\mathrm{fl}(s + x_k) = (s + x_k)(1 + \delta); by induction ss has the stated form for the earlier terms, and multiplying by 1+δ1 + \delta adds the new addition to the path of every earlier term and of xkx_k. For line L9, fl(s1+s2)=(s1+s2)(1+δ)\mathrm{fl}(s_1 + s_2) = (s_1 + s_2)(1 + \delta), and s1s_1 and s2s_2 have the stated form by induction on the two halves; the addition of line L9 is on the path of every term of the range. □\square

Theorem. Under the same assumptions, s^=∑i=1nxi(1+θi)\hat s = \sum_{i=1}^n x_i (1 + \theta_i) with ∣θi∣≤γhi≤γh|\theta_i| \le \gamma_{h_i} \le \gamma_h, hence

∣s^−s∣≤γh∑i=1n∣xi∣.|\hat s - s| \le \gamma_h \sum_{i=1}^n |x_i|.

Proof. Apply Lemma 2 to the top-level call and Lemma 1 to each product, which has hi≤hh_i \le h factors; γ\gamma is increasing in hh. Then ∣s^−s∣=∣∑xiθi∣≤∑∣xi∣∣θi∣≤γh∑∣xi∣|\hat s - s| = |\sum x_i \theta_i| \le \sum |x_i| |\theta_i| \le \gamma_h \sum|x_i|. □\square

Lemma 3 (the depth for b=1b = 1). With b=1b = 1, h=d(n)h = d(n) where d(1)=0d(1) = 0 and d(n)=1+d(⌈n/2⌉)d(n) = 1 + d(\lceil n/2 \rceil) for n≥2n \ge 2, and d(n)=⌈log⁡2n⌉d(n) = \lceil \log_2 n \rceil.

Proof. A call on n≥2n \ge 2 terms makes one addition and two calls on ⌊n/2⌋\lfloor n/2 \rfloor and ⌈n/2⌉\lceil n/2 \rceil terms. The largest path goes through the larger half, because dd is nondecreasing (by induction: if m≤nm \le n then ⌈m/2⌉≤⌈n/2⌉\lceil m/2 \rceil \le \lceil n/2 \rceil). So h=1+d(⌈n/2⌉)h = 1 + d(\lceil n/2 \rceil). For the closed form, let k=⌈log⁡2n⌉≥1k = \lceil \log_2 n \rceil \ge 1 for n≥2n \ge 2, so 2k−1<n≤2k2^{k-1} < n \le 2^k. Then 2k−2<n/2≤2k−12^{k-2} < n/2 \le 2^{k-1}, and since 2k−12^{k-1} is an integer, ⌈n/2⌉≤2k−1\lceil n/2 \rceil \le 2^{k-1}; also ⌈n/2⌉≥n/2>2k−2\lceil n/2 \rceil \ge n/2 > 2^{k-2}. Hence ⌈log⁡2⌈n/2⌉⌉=k−1\lceil \log_2 \lceil n/2 \rceil \rceil = k - 1, and by induction d(n)=1+(k−1)=kd(n) = 1 + (k - 1) = k. □\square

Lemma 4 (the depth with blocks). With block size b≥1b \ge 1, h≤(b−1)+⌈log⁡2⌈n/b⌉⌉h \le (b - 1) + \lceil \log_2 \lceil n/b \rceil \rceil for n≥1n \ge 1.

Proof. After kk halvings the largest range has ⌈n/2k⌉\lceil n/2^k \rceil terms (the ceiling of a ceiling of halves is the ceiling of n/2kn/2^k). Recursion stops on a path once the range has at most bb terms, which happens after at most KK halvings, the least kk with ⌈n/2k⌉≤b\lceil n/2^k \rceil \le b, that is 2k≥n/b2^k \ge n/b, that is 2k≥⌈n/b⌉2^k \ge \lceil n/b \rceil; so K=⌈log⁡2⌈n/b⌉⌉K = \lceil \log_2 \lceil n/b \rceil \rceil. Each halving contributes one addition (line L9) to the path, and the leaf chain contributes at most b−1b - 1 additions. □\square

Bottom-up variant. Level ℓ\ell of the bottom-up method has ⌈n/2ℓ⌉\lceil n/2^\ell \rceil values, each the sum of a contiguous group of at most 2ℓ2^\ell terms, and each level adds at most one addition to the path of every term. After ⌈log⁡2n⌉\lceil \log_2 n \rceil levels one value is left, so the same theorem holds with h=⌈log⁡2n⌉h = \lceil \log_2 n \rceil.

Cost. Every addition merges two values into one, and the algorithm starts with nn values and ends with one, so it makes exactly n−1n - 1 additions.

Back to Pairwise summation