Adding and Subtracting Polynomials
Adding or subtracting polynomials comes down to one skill you already have: combining like terms. Line up the terms with the same power of the variable, add or subtract their coefficients, and leave the powers alone. The one trap is subtraction, where the minus sign must reach every term of the second polynomial.
Adding: combine like terms
Like terms have the same variables raised to the same powers (Combining Like Terms). and are like terms; and are not.
Some people prefer the vertical layout, which works just like column addition with digits. Leave a gap (or write 0) for a missing power:
5x³ − 2x² + 0x + 4
+ x³ + 6x² − 3x − 9
─────────────────────
6x³ + 4x² − 3x − 5Subtracting: distribute the minus
Subtracting a polynomial means adding its opposite. The minus sign in front of the parentheses changes the sign of every term inside (Removing Parentheses):
Note: The classic mistake is flipping only the first term: . That gives , which is wrong. Check by plugging in a number: at the original is , and ✓, while ✗.
The degree of a sum or difference is at most the larger of the two degrees. It can drop when the leading terms cancel: , which has degree 1.
Adding coefficient lists in code
If each polynomial is a list of coefficients with the constant first (see Polynomial Definitions), adding them is adding the lists position by position. The shorter list is padded with zeros:
from itertools import zip_longest
def add(p, q):
return [a + b for a, b in zip_longest(p, q, fillvalue=0)]
def sub(p, q):
return [a - b for a, b in zip_longest(p, q, fillvalue=0)]
def trim(c):
c = list(c)
while len(c) > 1 and c[-1] == 0:
c.pop()
return c
p = [4, 0, -2, 5] # 5x³ − 2x² + 4
q = [-9, -3, 6, 1] # x³ + 6x² − 3x − 9
print("p + q =", add(p, q))
print("p - q =", sub(p, q))
print("(x² + 1) - (x² - 3x) =", trim(sub([1, 0, 1], [0, -3, 1])))p + q = [-5, -3, 4, 6]
p - q = [13, 3, -8, 4]
(x² + 1) - (x² - 3x) = [1, 3]
[-5, -3, 4, 6] reads as , the same answer as above. The trim step drops the leading zero that appears when the terms cancel.
SymPy does the bookkeeping symbolically, which is a good way to check hand work:
import sympy as sp
x, y = sp.symbols("x y")
print(sp.expand((4*x**2 - 3*x + 2) - (x**2 - 5*x + 8)))
print(sp.expand((2*x*y - y**2 + 3) + (5*y**2 - x*y - 7)))3*x**2 + 2*x - 6
x*y + 4*y**2 - 4
SymPy writes ** for powers and * for multiplication, so 3*x**2 means .
In software
- Vectors in disguise. Adding polynomials is exactly adding vectors of coefficients, and multiplying a polynomial by a number scales the vector. That is why polynomials of degree form a vector space (Vector Spaces), and why NumPy can add them with a single
+on arrays of equal length. - Combining models. A physics engine that adds a spring force to a drag force and a constant gravity term is adding polynomials in the position or the velocity. Simplifying them once, up front, saves work in every frame.
- Diffing. Subtracting two polynomials and checking for the zero polynomial is the standard way to test whether two formulas are the same, which computer algebra systems do constantly.
Exercises
Exercise 1: Add
Simplify .
Answer
Solution
Exercise 2: Subtract
Simplify .
Answer
Solution
Exercise 3: Two variables
Simplify .
Answer
Solution
Exercise 4: Missing piece
What polynomial must be added to to get ?
Answer
Solution
The missing piece is the difference: .
import sympy as sp
x = sp.symbols("x")
missing = sp.expand((7*x**2 - x + 4) - (2*x**2 + 5*x - 3))
print(missing)
print(sp.expand(2*x**2 + 5*x - 3 + missing))5*x**2 - 6*x + 7
7*x**2 - x + 4
Exercise 5: Sum a list of polynomials
Write poly_sum(polys) that adds any number of coefficient lists (constant first) and trims the result. Test it on [1, 2], [0, 0, 3] and [-1, -2, -3].
Answer
[0]: the three polynomials , and cancel completely.
Solution
from itertools import zip_longest
def poly_sum(polys):
total = [sum(col) for col in zip_longest(*polys, fillvalue=0)]
while len(total) > 1 and total[-1] == 0:
total.pop()
return total
print(poly_sum([[1, 2], [0, 0, 3], [-1, -2, -3]]))
print(poly_sum([[1, 2], [0, 0, 3]]))[0]
[1, 2, 3]
Test yourself
Simplify .
Which pair are like terms?
- and
- and
- and
- and
has degree 4 and has degree 4. What can the degree of be?
- Always 4
- Always 8
- 4 or less
- At least 4