Proofs: Sparse polynomial representation
These are the human proofs of Sparse polynomial representation, at full length.
Correctness of sparse distributed addition and heap multiplication full
This proof shows: correctness.
Setting. is a commutative ring and is a monomial order on the monomials of : a total order with . A list meets (D) when and all .
Lemma 1 (uniqueness). Each polynomial has exactly one list that meets (D). Proof. The monomials with nonzero coefficient in form a finite set, and the order is total, so there is exactly one strictly decreasing listing of them. The coefficients are those of .
So it is enough to show that each operation returns a list that meets (D) and whose terms sum to the right polynomial.
ADD. Let and meet (D). Loop invariant at line A2: meets (D); every monomial in is larger than every monomial of and of ; and represents the part of on the monomials larger than both and (with a missing entry treated as below every monomial). It holds at the start with empty. In case A3, is larger than every monomial left in , so the coefficient of that monomial in is that of , which is nonzero, and it is smaller than every monomial of ; appending keeps (D) and the invariant. Case A4 is symmetric. In case A5 to A7 the monomial occurs in both, its coefficient in is , and it is appended exactly when . Each step increases , so the loop ends. At line A8 one list is exhausted, the rest of the other list is smaller than everything in and has nonzero coefficients that equal those of , so the output meets (D) and equals .
MUL. Let and meet (D), with (line L1 handles the empty case, since the product with is ). Write and . Then . By compatibility, for each row .
Invariant (H). At line L4, for each row there is a number such that the products have been used (each exactly once), the others have not, and the heap holds exactly one entry for each row with , and nothing else. Line L2 sets for all rows. One pass of the loop extracts the entry of some row , uses and, at line L9, inserts the entry of the next column if there is one. So (H) holds again with increased by 1.
Order of extraction. Let be the monomial extracted in some pass. Each unused product has , so , because is the maximum of the heap. Every product used later is unused now, so its monomial is at most . Hence the extracted monomials form a non-increasing sequence , and all products with the same monomial are extracted in consecutive passes.
Invariant (O). After each pass, is a list of pairs with strictly decreasing monomials; all pairs except the last have nonzero coefficient; and represents exactly the sum of the used products. Before the first pass is empty. In a pass with monomial : if the last monomial of is , line L7 adds the new product to it, and the sum is still right. Otherwise is smaller than the last monomial of , by the order of extraction; the run of products with that last monomial is complete, so its coefficient is final, and line L8 deletes it if it is zero, which changes no sum. Then is appended, and the monomials stay strictly decreasing. So (O) holds.
Termination and result. Each pass uses one product and no product is used twice, so the loop runs exactly times. Then all products are used, and by (O) represents . The coefficient of each monomial in is the sum of all with , which is the coefficient of in . Every pair except the last has a nonzero coefficient, and line L10 deletes the last if it is zero. So the output meets (D), and by Lemma 1 it is the representation of .
Cost. By (H) the heap has at most entries at all times. There are extractions and at most insertions, each with comparisons of monomials, and coefficient products and at most additions. Swapping the factors so that does not change the product, because is commutative.
mul_term. For a term , the list is strictly decreasing by compatibility, and it represents . Dropping the entries with gives (D).