Algorithm Search⌘K

Proofs: Sparse polynomial multiplication

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

The heap method returns the normalized product of two sparse polynomials full

This proof shows: correctness.

Let RR be a commutative ring and let a=[(e0,a0),…,(es−1,as−1)]a = [(e_0, a_0), \ldots, (e_{s-1}, a_{s-1})] and b=[(f0,b0),…,(ft−1,bt−1)]b = [(f_0, b_0), \ldots, (f_{t-1}, b_{t-1})] be lists that meet invariant (S): the exponents increase strictly and no coefficient is zero. The product is

val⁡(a)val⁡(b)=∑i<s∑j<taibj xei+fj.\operatorname{val}(a) \operatorname{val}(b) = \sum_{i < s} \sum_{j < t} a_i b_j\, x^{e_i + f_j} .

Call (i,j)(i, j) a pair and k(i,j)=ei+fjk(i, j) = e_i + f_j its key. If s=0s = 0 or t=0t = 0, the product is 00 and line L1 returns the empty list, which is the list of 00. Assume now s,t≥1s, t \ge 1.

Claim 1. The heap holds at most one pair from each row. Line L2 puts the pair (i,0)(i, 0) for each row ii. Line L9 pushes (i,j+1)(i, j + 1) only right after it pops (i,j)(i, j). So by induction, at each test of line L4 the heap holds, for each row ii, either nothing or the single pair (i,ji)(i, j_i), where jij_i is the number of pairs of row ii already popped. In particular the heap never has more than ss entries.

Claim 2. Every pair is popped exactly once. By Claim 1, the pairs of row ii are popped in the order (i,0),(i,1),…(i, 0), (i, 1), \ldots, and after (i,j)(i, j) is popped the pair (i,j+1)(i, j + 1) is pushed if j+1<tj + 1 < t. So all tt pairs of row ii are popped, each once, and then the row adds nothing more. The loop stops when the heap is empty, that is after all sts t pairs are popped.

Claim 3. The keys are popped in nondecreasing order. Suppose line L5 pops a pair with key kk. Every pair still in the heap has a key at least kk, because the heap returns a minimum. Every pair (i,j′)(i, j') that is pushed later comes after the pair (i,ji)(i, j_i) that is now in the heap or was just popped, with j′>jij' > j_i, so k(i,j′)=ei+fj′>ei+fji≥kk(i, j') = e_i + f_{j'} > e_i + f_{j_i} \ge k, because f0<f1<⋯f_0 < f_1 < \cdots. So all later pops have keys at least kk.

Claim 4. Before the final filter, cc lists, for each key value kk that occurs, one pair (k,∑k(i,j)=kaibj)(k, \sum_{k(i,j) = k} a_i b_j), in increasing order of kk. By Claim 3, the pairs with the same key are popped one after the other, with no other key between them. Lines L7 and L8 start a new term when the key differs from the last exponent of cc, and add the product to the last term when it is the same. So each block of equal keys gives one term, whose coefficient is the sum of the products aibja_i b_j of the block, and by Claim 2 each product is counted once. The exponents of cc increase strictly, because the keys do not decrease and a new term starts only when the key changes.

Conclusion. By Claim 4, ∑(k,v)∈cvxk\sum_{(k, v) \in c} v x^k equals the double sum above, which is val⁡(a)val⁡(b)\operatorname{val}(a) \operatorname{val}(b). The terms of cc have strictly increasing exponents. Line L10 deletes the terms whose coefficient is 00: they contribute nothing to the sum, and the deletion keeps the order. So the result is the list of nonzero terms of the product in increasing order of exponent, which is the only list that meets (S) for that polynomial. ■\blacksquare

Back to Sparse polynomial multiplication