TExcelTAD
Home › Algebra › Adding and Subtracting Polynomials

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). 3x23x^2 and −7x2-7x^2 are like terms; 3x23x^2 and 3x3x are not.

(5x3−2x2+4)+(x3+6x2−3x−9)x3 terms:5x3+x3=6x3x2 terms:−2x2+6x2=4x2x terms:−3x=−3xnumbers:4−9=−5=6x3+4x2−3x−5\begin{aligned} &(5x^3 - 2x^2 + 4) + (x^3 + 6x^2 - 3x - 9) \\[1ex] x^3\ \text{terms:}\quad &5x^3 + x^3 = 6x^3 \\ x^2\ \text{terms:}\quad &{-2x^2} + 6x^2 = 4x^2 \\ x\ \text{terms:}\quad &{-3x} = -3x \\ \text{numbers:}\quad &4 - 9 = -5 \\[1ex] &= 6x^3 + 4x^2 - 3x - 5 \end{aligned}

Some people prefer the vertical layout, which works just like column addition with digits. Leave a gap (or write 0) for a missing power:

text
   5x³ − 2x² + 0x + 4
 +  x³ + 6x² − 3x − 9
 ─────────────────────
   6x³ + 4x² − 3x − 5

Subtracting: 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):

(4x2−3x+2)−(x2−5x+8)=4x2−3x+2−x2+5x−8every sign inside the second group flips=3x2+2x−6\begin{aligned} &(4x^2 - 3x + 2) - (x^2 - 5x + 8) \\ &= 4x^2 - 3x + 2 - x^2 + 5x - 8 && \text{every sign inside the second group flips} \\ &= 3x^2 + 2x - 6 \end{aligned}

Note: The classic mistake is flipping only the first term: 4x2−3x+2−x2−5x+84x^2 - 3x + 2 - x^2 - 5x + 8. That gives 3x2−8x+103x^2 - 8x + 10, which is wrong. Check by plugging in a number: at x=1x = 1 the original is 3−4=−13 - 4 = -1, and 3+2−6=−13 + 2 - 6 = -1 ✓, while 3−8+10=53 - 8 + 10 = 5 ✗.

The degree of a sum or difference is at most the larger of the two degrees. It can drop when the leading terms cancel: (x2+1)−(x2−3x)=3x+1(x^2 + 1) - (x^2 - 3x) = 3x + 1, 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:

python · try it yourself
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 −5−3x+4x2+6x3-5 - 3x + 4x^2 + 6x^3, the same answer as above. The trim step drops the leading zero that appears when the x2x^2 terms cancel.

SymPy does the bookkeeping symbolically, which is a good way to check hand work:

python · try it yourself
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 3x23x^2.

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 ≤n\le n 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 −kx-kx 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 (3x2−7x+1)+(−x2+2x+9)(3x^2 - 7x + 1) + (-x^2 + 2x + 9).

Answer

2x2−5x+102x^2 - 5x + 10

Solution
x2:3−1=2x:−7+2=−51:1+9=10\begin{aligned} x^2{:}&\quad 3 - 1 = 2 \\ x{:}&\quad -7 + 2 = -5 \\ 1{:}&\quad 1 + 9 = 10 \end{aligned}

Exercise 2: Subtract

Simplify (6y3−2y+5)−(2y3+y2−4y+5)(6y^3 - 2y + 5) - (2y^3 + y^2 - 4y + 5).

Answer

4y3−y2+2y4y^3 - y^2 + 2y

Solution
=6y3−2y+5−2y3−y2+4y−5flip every sign in the second group=(6−2)y3−y2+(−2+4)y+(5−5)=4y3−y2+2y\begin{aligned} &= 6y^3 - 2y + 5 - 2y^3 - y^2 + 4y - 5 && \text{flip every sign in the second group} \\ &= (6 - 2)y^3 - y^2 + (-2 + 4)y + (5 - 5) \\ &= 4y^3 - y^2 + 2y \end{aligned}

Exercise 3: Two variables

Simplify (a2b−3ab+b2)−(4a2b+ab−2b2)(a^2b - 3ab + b^2) - (4a^2b + ab - 2b^2).

Answer

−3a2b−4ab+3b2-3a^2b - 4ab + 3b^2

Solution
a2b:1−4=−3ab:−3−1=−4b2:1+2=3\begin{aligned} a^2b{:}&\quad 1 - 4 = -3 \\ ab{:}&\quad -3 - 1 = -4 \\ b^2{:}&\quad 1 + 2 = 3 \end{aligned}

Exercise 4: Missing piece

What polynomial must be added to 2x2+5x−32x^2 + 5x - 3 to get 7x2−x+47x^2 - x + 4?

Answer

5x2−6x+75x^2 - 6x + 7

Solution

The missing piece is the difference: (7x2−x+4)−(2x2+5x−3)=5x2−6x+7(7x^2 - x + 4) - (2x^2 + 5x - 3) = 5x^2 - 6x + 7.

python · try it yourself
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 1+2x1 + 2x, 3x23x^2 and −1−2x−3x2-1 - 2x - 3x^2 cancel completely.

Solution
python · try it yourself
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 (2x2+3x)−(x2−4x)(2x^2 + 3x) - (x^2 - 4x).

  1. x2−xx^2 - x
  2. x2+7xx^2 + 7x
  3. 3x2−x3x^2 - x
  4. x2+7x^2 + 7

Which pair are like terms?

  1. 4x2y4x^2y and −x2y-x^2y
  2. 3x23x^2 and 3x3x
  3. 5xy5xy and 5x5x
  4. x3x^3 and 3x3x

pp has degree 4 and qq has degree 4. What can the degree of p+qp + q be?

  1. Always 4
  2. Always 8
  3. 4 or less
  4. At least 4