Multiplying Polynomials
To multiply polynomials you multiply every term of the first by every term of the second and then combine like terms. That is just the distributive property used many times. This lesson covers monomials, the FOIL pattern for two binomials, larger products, and the surprising fact that polynomial multiplication is the same operation as convolution, which programmers meet in signal processing and neural networks.
Monomial times polynomial
Multiply the coefficients and add the exponents of the same variable (exponent laws):
Two binomials: FOIL
For there are four products. FOIL is a memory aid for them: First, Outer, Inner, Last.
FOIL only names the four products of two binomials. For anything bigger, use the general rule: every term times every term.
Bigger products
has products:
A grid (or "box") helps keep track: one polynomial's terms along the top, the other's down the side, a product in each cell, then add the cells that hold the same power.
Tip: The degree of a product is the sum of the degrees: here . The number of terms before combining is the product of the term counts. Both make quick checks on your work.
Multiplication is convolution
Write the polynomials as coefficient lists with the constant first. The term times lands at power , so the result's coefficient at power is the sum of all with . That sum has a name: the convolution of the two lists.
def poly_mul(a, b):
result = [0] * (len(a) + len(b) - 1)
for i, ai in enumerate(a):
for j, bj in enumerate(b):
result[i + j] += ai * bj # x^i · x^j = x^(i+j)
return result
p = [4, -1, 1] # x² − x + 4
q = [3, 2] # 2x + 3
print(poly_mul(p, q))
print(poly_mul([3, 2], [-4, 1])) # (2x + 3)(x − 4)[12, 5, 1, 2]
[-12, -5, 2]
[12, 5, 1, 2] is , the answer above. NumPy's np.convolve computes the same thing (the order of the lists does not matter for convolution as long as both use the same one), and SymPy checks the symbolic form:
import numpy as np
import sympy as sp
print(np.convolve([4, -1, 1], [3, 2]).tolist())
print(np.convolve([1, -1, 4], [2, 3]).tolist()) # highest power first works too
x = sp.symbols("x")
print(sp.expand((x**2 - x + 4) * (2*x + 3)))
print(sp.expand((x + 1)**4))[12, 5, 1, 2]
[2, 1, 5, 12]
2*x**3 + x**2 + 5*x + 12
x**4 + 4*x**3 + 6*x**2 + 4*x + 1
In software
- Long multiplication of numbers. is the polynomial times evaluated at , followed by carrying. Big-integer libraries do exactly this, and for very large numbers they switch to the Fast Fourier Transform, which computes a convolution in about steps instead of (see Signals and Fourier).
- Signal processing and CNNs. Blurring an image, smoothing a sensor reading or applying a convolutional layer in a neural network are all convolutions of a signal with a small kernel, the same double loop as
poly_mul. - Probability. The distribution of the sum of two dice is the convolution of their distributions: multiply by itself and read the coefficients.
import numpy as np
die = [0, 1, 1, 1, 1, 1, 1] # coefficient of x^k = ways to roll k
two = [int(c) for c in np.convolve(die, die)]
for total in range(2, 13):
print(total, two[total])2 1
3 2
4 3
5 4
6 5
7 6
8 5
9 4
10 3
11 2
12 1
Exercises
Exercise 1: Monomial times trinomial
Expand .
Answer
Solution
Exercise 2: FOIL
Expand .
Answer
Solution
Exercise 3: Trinomial times trinomial
Expand .
Answer
Solution
import numpy as np
print(np.convolve([1, 2, -1], [1, -2, 3]).tolist()) # highest power first[1, 0, -2, 8, -3]
Exercise 4: Numbers as polynomials
Use poly_mul on the digit lists of 213 and 32 (constant first), then carry to recover .
Answer
The raw coefficients are [6, 11, 7, 6]; after carrying they give 6816.
Solution
213 is [3, 1, 2] and 32 is [2, 3] with the ones digit first.
def poly_mul(a, b):
r = [0] * (len(a) + len(b) - 1)
for i, ai in enumerate(a):
for j, bj in enumerate(b):
r[i + j] += ai * bj
return r
raw = poly_mul([3, 1, 2], [2, 3])
print("raw:", raw)
digits, carry = [], 0
for c in raw:
carry += c
digits.append(carry % 10)
carry //= 10
while carry:
digits.append(carry % 10)
carry //= 10
print("".join(map(str, reversed(digits))), 213 * 32)raw: [6, 11, 7, 6]
6816 6816
Test yourself
What is the degree of ?
- 12
- 7
- 4
- 3
Expand .
Which operation on coefficient lists equals polynomial multiplication?
- Element-wise product
- Dot product
- Concatenation
- Convolution