Algorithm Search⌘K

Proofs: Subproduct tree

These are the human proofs of Subproduct tree, at full length.

The subproduct tree holds the range products, and its two traversals evaluate and combine correctly full

This proof shows: correctness.

Let RR be a commutative ring, n≥1n \ge 1 and u0,…,un−1∈Ru_0, \ldots, u_{n-1} \in R. For integers 0≤l≤r≤n0 \le l \le r \le n write

P(l,r)=∏l≤j<r(x−uj),P(l, r) = \prod_{l \le j < r} (x - u_j),

a monic polynomial of degree r−lr - l, with P(l,l)=1P(l, l) = 1. For a level h≥0h \ge 0 and an index i≥0i \ge 0, let lh,i=i 2hl_{h,i} = i\,2^h and rh,i=min⁡((i+1) 2h,n)r_{h,i} = \min((i + 1)\,2^h, n).

The build

Claim. After line L1, and after each pass of the loop L3 to L8, the list T[h]T[h] has ⌈n/2h⌉\lceil n / 2^h \rceil nodes and its node ii is P(lh,i,rh,i)P(l_{h,i}, r_{h,i}).

Level 0. Line L1 sets node ii to x−ui=P(i,i+1)x - u_i = P(i, i + 1), and r0,i=i+1r_{0,i} = i + 1 because i<ni < n. There are nn nodes.

From hh to h+1h + 1. Let L=⌈n/2h⌉L = \lceil n / 2^h \rceil be the number of nodes at level hh. For a pair 2i,2i+1<L2i, 2i + 1 < L, line L6 forms

P(lh,2i,rh,2i)⋅P(lh,2i+1,rh,2i+1)=P(2i 2h,(2i+1) 2h)⋅P((2i+1) 2h,rh,2i+1)=P(lh+1,i,rh+1,i),P(l_{h,2i}, r_{h,2i}) \cdot P(l_{h,2i+1}, r_{h,2i+1}) = P(2i\,2^h, (2i + 1)\,2^h) \cdot P((2i + 1)\,2^h, r_{h,2i+1}) = P(l_{h+1,i}, r_{h+1,i}),

because rh,2i=(2i+1)2hr_{h,2i} = (2i + 1) 2^h when node 2i+12i + 1 exists, and rh,2i+1=min⁡((2i+2)2h,n)=rh+1,ir_{h,2i+1} = \min((2i + 2) 2^h, n) = r_{h+1,i}. If LL is odd, line L7 copies the last node, of index L−1=2iL - 1 = 2i, as node ii of level h+1h + 1. It is P(2i 2h,n)P(2i\,2^h, n), and lh+1,i=2i 2hl_{h+1,i} = 2i\,2^h and rh+1,i=nr_{h+1,i} = n because (2i+1)2h≥n(2i + 1) 2^h \ge n. So level h+1h + 1 has ⌈L/2⌉=⌈n/2h+1⌉\lceil L / 2 \rceil = \lceil n / 2^{h+1} \rceil nodes of the claimed form.

End. The number of nodes is at least 11 and decreases strictly while it is larger than 11, so the loop stops, at the first kk with ⌈n/2k⌉=1\lceil n / 2^k \rceil = 1, that is k=⌈log⁡2n⌉k = \lceil \log_2 n \rceil. Then T[k]=[P(0,n)]=[m]T[k] = [P(0, n)] = [m] with m=∏j(x−uj)m = \prod_j (x - u_j).

Remainder descent

Let f∈R[x]f \in R[x]. Every node is monic, so division with remainder by a node is defined over RR. EVALUATE sets rk,0=frem⁡mr_{k,0} = f \operatorname{rem} m and then, for each level hh from k−1k - 1 down to 00, rh,i=rh+1,⌊i/2⌋rem⁡T[h][i]r_{h,i} = r_{h+1, \lfloor i/2 \rfloor} \operatorname{rem} T[h][i].

Claim. rh,i=frem⁡T[h][i]r_{h,i} = f \operatorname{rem} T[h][i] for all hh and ii.

The node T[h][i]T[h][i] divides its parent T[h+1][⌊i/2⌋]T[h+1][\lfloor i/2 \rfloor], because by the build claim the range of the child is contained in the range of the parent. If g∣Gg \mid G and GG is monic, then (frem⁡G)rem⁡g=frem⁡g(f \operatorname{rem} G) \operatorname{rem} g = f \operatorname{rem} g: write f=qG+(frem⁡G)f = q G + (f \operatorname{rem} G); then f−(frem⁡G)=qGf - (f \operatorname{rem} G) = q G is a multiple of gg, so ff and frem⁡Gf \operatorname{rem} G have the same remainder modulo the monic gg, by the uniqueness of the remainder. The claim follows by induction from the root down. When the degree of rh+1,⌊i/2⌋r_{h+1, \lfloor i/2 \rfloor} is already less than the degree of the node, the remainder is this polynomial itself, which is the shortcut in the implementation.

At level 00 the node is x−ujx - u_j and frem⁡(x−uj)=f(uj)f \operatorname{rem} (x - u_j) = f(u_j) by the polynomial remainder theorem. So EVALUATE returns f(u0),…,f(un−1)f(u_0), \ldots, f(u_{n-1}).

Linear combination

Let c0,…,cn−1∈Rc_0, \ldots, c_{n-1} \in R. COMBINE sets s0,i=cis_{0,i} = c_i and, for each level, sh+1,i=sh,2i⋅T[h][2i+1]+sh,2i+1⋅T[h][2i]s_{h+1,i} = s_{h,2i} \cdot T[h][2i + 1] + s_{h,2i+1} \cdot T[h][2i] for each pair, and copies sh,L−1s_{h, L-1} when LL is odd.

Claim. sh,i=∑l≤j<rcj P(l,r)/(x−uj)s_{h,i} = \sum_{l \le j < r} c_j\, P(l, r) / (x - u_j), where l=lh,il = l_{h,i}, r=rh,ir = r_{h,i}, and P(l,r)/(x−uj)=P(l,j) P(j+1,r)P(l, r) / (x - u_j) = P(l, j)\, P(j + 1, r) is the product of the other factors.

At level 00 the sum has the single term ciP(i,i+1)/(x−ui)=cic_i P(i, i+1) / (x - u_i) = c_i. For a pair, let the left range be [l,t)[l, t) and the right range [t,r)[t, r). For jj in the left range, P(l,r)/(x−uj)=(P(l,t)/(x−uj))⋅P(t,r)P(l, r) / (x - u_j) = (P(l, t) / (x - u_j)) \cdot P(t, r), and P(t,r)P(t, r) is the right node T[h][2i+1]T[h][2i + 1]. For jj in the right range, P(l,r)/(x−uj)=P(l,t)⋅(P(t,r)/(x−uj))P(l, r) / (x - u_j) = P(l, t) \cdot (P(t, r) / (x - u_j)), and P(l,t)P(l, t) is the left node. Adding the two parts gives the formula of line C4. A copied node keeps its range, so its sum does not change. At the root, COMBINE returns ∑jcj m/(x−uj)\sum_j c_j\, m / (x - u_j).

Interpolation

Let RR be a field and the uju_j distinct. Then m′(uj)=∏i≠j(uj−ui)≠0m'(u_j) = \prod_{i \ne j} (u_j - u_i) \ne 0, by the product rule applied to m=(x−uj)⋅m/(x−uj)m = (x - u_j) \cdot m / (x - u_j) at x=ujx = u_j. With cj=vj/m′(uj)c_j = v_j / m'(u_j), the polynomial f=∑jcj m/(x−uj)f = \sum_j c_j\, m / (x - u_j) has degree less than nn, and at x=uix = u_i every term with j≠ij \ne i vanishes, so f(ui)=ci∏j≠i(ui−uj)=vif(u_i) = c_i \prod_{j \ne i} (u_i - u_j) = v_i. This is the Lagrange form of the interpolating polynomial, which is the unique polynomial of degree less than nn with these values. ■\blacksquare

Back to Subproduct tree