Polynomial Addition Polynomial Arithmetic Algorithms

J.E.D.I

7.5.1.1 Polynomial Addition

In adding two polynomials P and Q, the sum is retained in Q. Three running pointers are needed: • α to point to the current term node in polynomial P and • β to point to the current term in polynomial Q • σ points to the node behind β . This is used during insertion and deletion in Q to make the time complexity O1. The state of α and β , will fall in one of the following cases: • EXPO α EXPO β Action: Advance the pointers to polynomial Q one node up • EXPO α EXPO β Action: Copy the current term in P and insert before the current term in Q, then advance α • EXPO α = EXPO β • If EXPO α 0, both pointers α and β have come full circle and are now pointing to the list heads Action: Terminate the procedure • Else, α and β are pointing to two terms which can be added Action: Add the coefficients. When the result is zero, delete the node from Q. Move P and Q to the next term. For example, add the following two polynomials: P = 67xy 2 z + 32x 2 yz – 45xz 5 + 6x - 2x 3 y 2 z Q = 15x 3 y 2 z - 9xyz + 5y 4 z 3 - 32x 2 yz Since the two polynomials are not in canonical form, their terms have to be reordered before they are represented as: Figure 1.125 Polynomials P and Q Data Structures 138 J.E.D.I Adding the two, Expo α = Expo β adding α and β : Expo α = Expo β adding α and β results to deleting the node pointed to by β : Expo α Expo β insert α between σ and β : Data Structures 139 J.E.D.I Expo α Expo β , advance σ and β : Expo α Expo β , insert α into Q: Expo α Expo β , insert α into Q: Figure 1.126 Addition of Polynomials P and Q Since both P and Q are in canonical form, one pass is sufficient. If the operands are not in canonical form, the procedure will not yield the correct result. If P has m terms and Q has n terms, the time complexity of the algorithm is Om+n. With this algorithm, there is no need for special handling of the zero polynomial. It works with zero P andor Q. However, since the sum is retained in Q, it has to be duplicated if the need to use Q after the addition will arise. It could be done by calling the method addQ, P of class Polynomial, where P is initially the zero polynomial and will contain the duplicate of Q. The following is the Java implementation of this procedure: Performs the operation Q = P + Q, Q is [this] polynomial void addPolynomial P{ Roving pointer in P PolyTerm alpha = P.head.link; Roving pointer in Q PolyTerm beta = head.link; Pointer to the node behind beta, used in insertion to Q Data Structures 140 J.E.D.I PolyTerm sigma = head; PolyTerm tau; while true{ Current term in P current term in Q if alpha.expo beta.expo { Advance pointers in Q sigma = beta; beta = beta.link; } else if alpha.expo beta.expo { Insert the current term in P to Q tau = new PolyTerm; tau.coef = alpha.coef; tau.expo = alpha.expo; sigma.link = tau; tau.link = beta; sigma = tau; alpha = alpha.link; Advance pointer in P } else { Terms in P and Q can be added if alpha.expo 0 return; The sum is already in Q else{ beta.coef = beta.coef + alpha.coef; If adding will cause to cancel out the term if beta.coef == 0{ tau = beta; sigma.link = beta.link; beta = beta.link; } else{ Advance pointers in Q sigma = beta; beta = beta.link; } Advance pointer in P alpha = alpha.link; } } } }

7.5.1.2 Polynomial Subtraction