Algorithm Search⌘K

Proofs: Sparse polynomial representation

These are the human proofs of Sparse polynomial representation, at full length.

The sparse operations return the normalized term list of the exact result full

This proof shows: correctness.

Let RR be a commutative ring. A list a=[(e0,c0),…,(et−1,ct−1)]a = [(e_0, c_0), \ldots, (e_{t-1}, c_{t-1})] meets (S) when e0<⋯<et−1e_0 < \cdots < e_{t-1} and every ci≠0c_i \ne 0, and val⁡(a)=∑icixei\operatorname{val}(a) = \sum_i c_i x^{e_i}.

Lemma 1 (uniqueness). Two lists that meet (S) and have the same value are equal.

Proof. The coefficient of xkx^k in val⁡(a)\operatorname{val}(a) is cic_i if k=eik = e_i and 00 if kk is not an exponent of aa, because the exponents are distinct. So the set of exponents of aa is the set of kk with a nonzero coefficient in val⁡(a)\operatorname{val}(a), and the coefficients are the coefficients of val⁡(a)\operatorname{val}(a). Both are determined by the value, and the order is fixed by (S). □\square

So it suffices to show that each operation returns a list that meets (S) and has the right value.

ADD. Let aa and bb meet (S), with ss and tt terms. Consider the statement I(i,j)I(i, j):

  1. cc meets (S);
  2. val⁡(c)=∑k<ickaxeka+∑k<jckbxekb\operatorname{val}(c) = \sum_{k < i} c^a_k x^{e^a_k} + \sum_{k < j} c^b_k x^{e^b_k}, the sum of the terms already read;
  3. every exponent in cc is less than eiae^a_i if i<si < s, and less than ejbe^b_j if j<tj < t;
  4. the exponents of a[0..i−1]a[0 .. i-1] and b[0..j−1]b[0 .. j-1] are all less than eiae^a_i if i<si < s and less than ejbe^b_j if j<tj < t.

Initialization. At line L1, i=j=0i = j = 0 and cc is empty, so all four parts hold.

Maintenance. Assume I(i,j)I(i, j) at line L2 with i<si < s and j<tj < t.

  • Case line L3, eia<ejbe^a_i < e^b_j. The term a[i]a[i] is appended. By part 3 its exponent is larger than all exponents in cc, and cia≠0c^a_i \ne 0, so cc still meets (S). Part 2 holds for (i+1,j)(i + 1, j). For part 3, the new last exponent eiae^a_i is less than ei+1ae^a_{i+1} by (S) for aa, and less than ejbe^b_j by the case. For part 4, eia<ei+1ae^a_i < e^a_{i+1} and eia<ejbe^a_i < e^b_j, and the old terms were already below both bounds, since ei+1a>eiae^a_{i+1} > e^a_i.
  • Case line L4 is symmetric.
  • Case lines L5 to L8, eia=ejb=ee^a_i = e^b_j = e. The two terms add to (cia+cjb)xe=uxe(c^a_i + c^b_j) x^e = u x^e. If u≠0u \ne 0, the term (e,u)(e, u) is appended; its exponent is larger than all of cc by part 3, so (S) holds. If u=0u = 0, nothing is appended and the value changes by 00. In both cases part 2 holds for (i+1,j+1)(i + 1, j + 1). Parts 3 and 4 hold because e<ei+1ae < e^a_{i+1} and e<ej+1be < e^b_{j+1} by (S) for aa and bb.

Termination. Each pass increases i+ji + j by at least 11, and i+j≤s+ti + j \le s + t, so the loop stops after at most s+t−1s + t - 1 passes, with i=si = s or j=tj = t.

End. Say i=si = s (the case j=tj = t is symmetric). Then line L9 appends b[j..t−1]b[j .. t-1], and the tail of aa is empty. By part 3, every exponent of cc is less than ejbe^b_j, and the tail is increasing with nonzero coefficients, so the result meets (S). By part 2, its value is val⁡(a)+val⁡(b)\operatorname{val}(a) + \operatorname{val}(b). ■\blacksquare for ADD.

MUL. For each kk, the final value of D[k]D[k] is ∑uv\sum u v over the pairs of terms (e,u)(e, u) of aa and (d,v)(d, v) of bb with e+d=ke + d = k, since line M4 adds each such product once and no other. This sum is the coefficient of xkx^k in val⁡(a)val⁡(b)=∑e,duv xe+d\operatorname{val}(a) \operatorname{val}(b) = \sum_{e, d} u v\, x^{e + d}. Every exponent kk with a nonzero coefficient in the product is a key of DD, because a nonzero sum has a nonzero term. Line M5 returns the keys with D[k]≠0D[k] \ne 0, each once, in increasing order. So the result meets (S) and has the value of the product.

EVALUATE. Let the terms be (e0,u0),…,(et−1,ut−1)(e_0, u_0), \ldots, (e_{t-1}, u_{t-1}). Claim: before the pass for term ii of line E2, p=zprevp = z^{\text{prev}}, prev=ei−1\text{prev} = e_{i-1} (or 00 when i=0i = 0) and y=∑k<iukzeky = \sum_{k < i} u_k z^{e_k}. At line E1 this holds with p=1=z0p = 1 = z^0 and y=0y = 0. In the pass, ei≥preve_i \ge \text{prev} by (S) (and e0≥0e_0 \ge 0), so line E3 sets p=zprevzei−prev=zeip = z^{\text{prev}} z^{e_i - \text{prev}} = z^{e_i}, line E4 sets prev=ei\text{prev} = e_i, and line E5 adds uizeiu_i z^{e_i} to yy. After the last pass, y=∑kukzek=val⁡(a)(z)y = \sum_k u_k z^{e_k} = \operatorname{val}(a)(z). Binary powering computes zgz^g correctly by the identity zg=(z2)⌊g/2⌋zg mod 2z^g = (z^2)^{\lfloor g/2 \rfloor} z^{g \bmod 2}.

Derivative and scalar multiple. Both map the term with exponent ee to a term with exponent e−1e - 1 or ee, a strictly increasing map on the exponents, so the order of (S) is kept. Terms whose new coefficient is 00 are dropped, so the coefficients are nonzero. The value is ∑e≥1ec xe−1=val⁡(a)′\sum_{e \ge 1} e c\, x^{e-1} = \operatorname{val}(a)' and ∑uc xe=uval⁡(a)\sum u c\, x^e = u \operatorname{val}(a), by linearity. ■\blacksquare

Back to Sparse polynomial representation