taocp

This site has no text of the book. Keep your own copy of TAOCP next to it. See what is checked.

Exercise · Volume 1 · 2.2.4-9

Exercise 2.2.4-9

not stated yet in Lean solution written

Rating23
KindK2, decide: say whether a claim holds from the written solution
SectionSection 2.2.4 of Volume 1

The task

Decide whether Algorithm A works when P=QP = Q, and whether Algorithm M works when P=MP = M, when P=QP = Q and when M=QM = Q.

The exact wording is in your copy of the book.

Hints

Open one hint at a time. Try the exercise again after each.

Hint 1

Trace each algorithm with a small example, and ask which node is changed or freed while the other pointer still needs it.

Hint 2

Remember that a term whose sum of coefficients is 00 is removed from the list.

Solution

Show the solution

A with P=QP = Q. Each term is added to itself, so its coefficient is doubled, and the answer is 2⋅polynomial(P)2 \cdot \mathrm{polynomial}(P), as it should be. The coefficient would become 00 only if it was 00 already, which a proper polynomial never has. If a stored term has coefficient 00 and a non-negative ABC\mathrm{ABC}, the algorithm frees the node that PP still needs, and it fails.

M with P=MP = M. Algorithm M only adds shifted copies of PP to QQ and never changes PP and MM. So it works, and Q←Q+P⋅PQ \leftarrow Q + P \cdot P.

M with P=QP = Q. Now PP changes during the algorithm. If M=t1+⋯+tkM = t_1 + \dots + t_k is the sum of the terms of MM, then after the cycle for tit_i the polynomial is multiplied by 1+ti1 + t_i. So the result is Q⋅(1+t1)⋯(1+tk)Q \cdot (1 + t_1) \cdots (1 + t_k), and not Q+M⋅QQ + M \cdot Q.

M with M=QM = Q. Now the list that is read as the multiplier is changed during the run. Look at the cycle for the term tt of MM. It adds the terms c t uc \, t \, u to QQ for the terms c uc\,u of PP. Every uu has ABC≥0\mathrm{ABC} \ge 0, so every changed or new term of QQ has an exponent at least as large as that of tt, and in the decreasing order these terms lie at tt or before it. The terms that are still to be read lie after tt and are not touched. The coefficient of tt itself changes only in the last step of the cycle, when the constant term of PP is added, after the product that uses it. So the loop reads the original multiplier, and the result is Q+Q⋅PQ + Q \cdot P, as it should be. It fails when the constant term of PP is −1-1. Then the term tt cancels in the last step, its node is freed, and the next step of the loop follows the link of a freed node.

Check

On the simulator, with P=MP = M, P=x+2yP = x + 2y and Q=5Q = 5, the result was 5+P2=x2+4xy+4y2+55 + P^2 = x^2 + 4xy + 4y^2 + 5. With Q=x+2Q = x + 2 and P=QP = Q, M=3yM = 3y, the result was (x+2)(1+3y)=3xy+x+6y+2(x + 2)(1 + 3y) = 3xy + x + 6y + 2, and with M=3y+zM = 3y + z it was (x+2)(1+3y)(1+z)(x + 2)(1 + 3y)(1 + z). With M=Q=y+3M = Q = y + 3 and P=2xP = 2x, the result was 2xy+6x+y+3=Q+QP2xy + 6x + y + 3 = Q + QP. With P=2x−1P = 2x - 1 the run gave 2xy+32xy + 3 instead of 2xy+6x2xy + 6x. Algorithm A with P=Q=2x+3P = Q = 2x + 3 gave 4x+64x + 6, and with a stored zero coefficient the run failed.

Remarks

The safe rule is to use different lists for the multiplier, the multiplicand and the result, or to copy a list first with the subroutine of Exercise 11.

This solution is drafted, and a person has not reviewed it yet. The source.

In Lean

not stated yet The exercise is not stated in Lean yet.

The status is computed from the Lean build. The node of the map.

Something is wrong? Report it.