Combining Like Terms
Combining like terms is the basic tidying step of algebra: terms that have exactly the same variable part are added by adding their coefficients, so becomes . This lesson shows how to spot like terms, how to combine them with signs, and how the same idea collects counts in code.
What "like" means
Two terms are like terms when their variable parts are identical: the same letters, each raised to the same power. Only the coefficients may differ.
| Like terms | Not like terms | Why not |
|---|---|---|
| and | and | different powers |
| and | and | different letters |
| and | and | powers on different letters |
| 7 and −2 (constants) | 7 and | one has no variable |
Tip: Think of variables as units. , but cannot become one number. , but stays as it is.
How to combine
Combining like terms is the distributive property read backwards: . In practice:
- Underline or group the like terms, keeping each term's sign with it.
- Add their coefficients.
- Write the result, usually with the highest power first and the constant last.
A longer one with two variables:
SymPy combines like terms automatically as soon as you build the expression:
from sympy import symbols
x, a, b = symbols("x a b")
print(6*x**2 - 2*x + 9 - x**2 + 5*x - 4)
print(3*a - 4*b + 2*a*b - a + 7*b - 5*a*b)
print(x + x + x - 3*x) # everything cancels5*x**2 + 3*x + 5
-3*a*b + 2*a + 3*b
0
Common slips
- Losing a sign. In , the term is , so the terms give , not .
- Adding exponents. , not . Combining adds coefficients; the variable part stays exactly the same.
- Merging unlike terms. is already simplified.
A quick check: substitute a number into the original and the simplified versions. If they disagree, there is an error.
def original(x):
return 6*x**2 - 2*x + 9 - x**2 + 5*x - 4
def simplified(x):
return 5*x**2 + 3*x + 5
def wrong(x): # added the exponents by mistake
return 5*x**4 + 3*x + 5
for v in [0, 1, 2, -3]:
print(v, original(v), simplified(v), wrong(v))0 5 5 5
1 13 13 13
2 31 31 91
-3 41 41 401
The wrong version agrees at 0 and 1 by coincidence ( and ), which is why you should test with values like 2 and −3 as well.
In software: collecting by key
A computer algebra system combines like terms by using the variable part as a key in a dictionary and adding the coefficients that share it. You can write a miniature version in a few lines. Each term is a coefficient and a "monomial" key such as "x^2":
from collections import defaultdict
terms = [(6, "x^2"), (-2, "x"), (9, ""), (-1, "x^2"), (5, "x"), (-4, "")]
combined = defaultdict(int)
for coef, mono in terms:
combined[mono] += coef
for mono, coef in combined.items():
print(f"{coef:+d}{mono}")+5x^2
+3x
+5
The same pattern totals sales per product, counts words per document and accumulates gradients per weight in machine learning frameworks: group by key, add the numbers.
Exercises
Exercise 1: Basic combining
Simplify .
Answer
Solution
Exercise 2: Two variables
Simplify .
Answer
Solution
from sympy import symbols
m, n = symbols("m n")
print(4*m**2*n - 3*m*n**2 + m*n**2 - 6*m**2*n + 2*m*n)-2*m**2*n - 2*m*n**2 + 2*m*n
Exercise 3: Perimeter
A triangle has sides , and (in cm). Write the perimeter in simplest form, then find it for .
Answer
; 26 cm for .
Solution
Exercise 4: Extend the collector
Use the dictionary method to combine [(3, "ab"), (-4, "b"), (2, "ab"), (7, "b"), (-5, "ab"), (1, "a")] and print only the terms whose coefficient is not zero.
Answer
+3b and +1a (the ab terms cancel to 0).
Solution
from collections import defaultdict
terms = [(3, "ab"), (-4, "b"), (2, "ab"), (7, "b"), (-5, "ab"), (1, "a")]
combined = defaultdict(int)
for coef, mono in terms:
combined[mono] += coef
print([f"{c:+d}{m}" for m, c in combined.items() if c != 0])['+3b', '+1a']
Test yourself
Which pair are like terms?
- and
- and
- and
- 2 and
Simplify .
Simplify .