TAOCP 2.2.4 Exercise 10
We wish to adapt Algorithms `A` (addition) and `M` (multiplication) for polynomials in a single variable $x$, allowing exponents up to $b^3 - 1$.
Exercise 10. [20] The algorithms in this section assume that we are using three variables $x$, $y$, and $z$ in the polynomials, and that their exponents individually never exceed $b - 1$ (where $b$ is the byte size in MIX's case). Suppose instead that we want to do addition and multiplication of polynomials in only one variable, $x$, and to let its exponent take on values up to $b^3 - 1$. What changes should be made to Algorithms A and M?
Verified: no
Solve time: 39s
Solution
We wish to adapt Algorithms A (addition) and M (multiplication) for polynomials in a single variable $x$, allowing exponents up to $b^3 - 1$. In the original algorithms, each node contains the two-word structure
$$ \text{COEF} \mid ABC, $$
where $ABC$ encodes the triple $(A, B, C)$ of exponents for $(x, y, z)$ in a single unit. For a single variable $x$, we no longer need three separate components; instead, the exponent can be stored directly as a single integer $E$ in one word, taking values $0 \le E \le b^3 - 1$. The list retains the special node with $E = -1$ and $\text{COEF} = 0$ as a sentinel.
Modifications to Algorithm A
Algorithm A relies on comparing ABC(P) and ABC(Q) to locate matching terms. For one-variable polynomials, we replace ABC(P) and ABC(Q) with E(P) and E(Q). The comparison logic remains identical, but is now applied to the single exponent:
- Step
A2becomes: if $E(P) < E(Q)$, advance $Q$ along the list; if $E(P) = E(Q)$, go to stepA3; if $E(P) > E(Q)$, go to stepA5. - Step
A3updates $\text{COEF}(Q) \leftarrow \text{COEF}(Q) + \text{COEF}(P)$. If $\text{COEF}(Q) = 0$, delete the term as in stepA4. - Step
A5inserts a new term with exponent $E(P)$ and coefficient $\text{COEF}(P)$.
All pointer manipulations (P, Q, Q1, Q2) remain unchanged. The ordering invariant is now that the list of nodes is in decreasing order of E, with the sentinel node at $E = -1$ linking back to the largest exponent. The rest of the algorithm proceeds without further change.
Modifications to Algorithm M
Algorithm M multiplies polynomials term-by-term, generating new terms whose ABC fields are the componentwise sums of the exponents. For one-variable polynomials, the exponent sum is simply the sum of two single integers:
$$ E(R) \leftarrow E(P) + E(Q), $$
where $P$ iterates over the first polynomial and $Q$ over the second. Since $0 \le E(P), E(Q) \le b^3 - 1$, the maximum exponent is $2 b^3 - 2$. If the word can store integers up to $b^3 - 1$, we must ensure either that arithmetic can accommodate sums up to $2 b^3 - 2$ or that a larger word size is used. Apart from replacing ABC(P) + ABC(Q) with E(P) + E(Q), the insertion into the product polynomial uses the same procedure as in A5, maintaining descending order of exponents. The sentinel node remains with $E = -1$, $\text{COEF} = 0$.
Summary of Changes
- Replace the
ABCfield with a single exponentEof type integer, $0 \le E \le b^3 - 1$. - Replace all comparisons of
ABC(P)andABC(Q)with comparisons ofE(P)andE(Q). - In Algorithm
M, replace vector addition ofABCtriples with ordinary integer addition:E(P) + E(Q). - Maintain descending order of exponents and retain the sentinel node at $E = -1$.
- Ensure the word size or arithmetic handles exponents up to $2 b^3 - 2$ if necessary.
No changes are required to pointer handling or the structure of the algorithms beyond these substitutions.
This completes the solution.
∎