TExcelTAD
Home › Algebra › Multiplying Polynomials

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

3x2⋅(4x3−2x+5)=12x5−6x3+15x2−2ab⋅(a2−3b)=−2a3b+6ab2\begin{aligned} 3x^2 \cdot (4x^3 - 2x + 5) &= 12x^5 - 6x^3 + 15x^2 \\ -2ab \cdot (a^2 - 3b) &= -2a^3b + 6ab^2 \end{aligned}

Two binomials: FOIL

For (a+b)(c+d)(a + b)(c + d) there are four products. FOIL is a memory aid for them: First, Outer, Inner, Last.

(2x+3)(x−4)F:2x⋅x=2x2O:2x⋅(−4)=−8xI:3⋅x=3xL:3⋅(−4)=−12=2x2−5x−12\begin{aligned} &(2x + 3)(x - 4) \\ \text{F:}\quad &2x \cdot x = 2x^2 \\ \text{O:}\quad &2x \cdot (-4) = -8x \\ \text{I:}\quad &3 \cdot x = 3x \\ \text{L:}\quad &3 \cdot (-4) = -12 \\ &= 2x^2 - 5x - 12 \end{aligned}

FOIL only names the four products of two binomials. For anything bigger, use the general rule: every term times every term.

Bigger products

(x2−x+4)(2x+3)(x^2 - x + 4)(2x + 3) has 3×2=63 \times 2 = 6 products:

x2⋅2x=2x3x2⋅3=3x2−x⋅2x=−2x2−x⋅3=−3x4⋅2x=8x4⋅3=12\begin{aligned} x^2 \cdot 2x &= 2x^3 & x^2 \cdot 3 &= 3x^2 \\ -x \cdot 2x &= -2x^2 & -x \cdot 3 &= -3x \\ 4 \cdot 2x &= 8x & 4 \cdot 3 &= 12 \end{aligned}
=2x3+(3−2)x2+(−3+8)x+12=2x3+x2+5x+12\begin{aligned} &= 2x^3 + (3 - 2)x^2 + (-3 + 8)x + 12 \\ &= 2x^3 + x^2 + 5x + 12 \end{aligned}

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 2+1=32 + 1 = 3. 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 aixia_i x^i times bjxjb_j x^j lands at power i+ji + j, so the result's coefficient at power kk is the sum of all ai⋅bja_i \cdot b_j with i+j=ki + j = k. That sum has a name: the convolution of the two lists.

python · try it yourself
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 12+5x+x2+2x312 + 5x + x^2 + 2x^3, 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:

python · try it yourself
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. 213×32213 \times 32 is the polynomial 2x2+x+32x^2 + x + 3 times 3x+23x + 2 evaluated at x=10x = 10, 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 nlog⁡nn \log n steps instead of n2n^2 (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 (x+x2+⋯+x6)(x + x^2 + \cdots + x^6) by itself and read the coefficients.
python · try it yourself
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 −4y3(2y2−y+6)-4y^3(2y^2 - y + 6).

Answer

−8y5+4y4−24y3-8y^5 + 4y^4 - 24y^3

Solution
−4y3⋅2y2=−8y5−4y3⋅(−y)=+4y4−4y3⋅6=−24y3\begin{aligned} -4y^3 \cdot 2y^2 &= -8y^5 \\ -4y^3 \cdot (-y) &= +4y^4 \\ -4y^3 \cdot 6 &= -24y^3 \end{aligned}

Exercise 2: FOIL

Expand (3x−5)(2x+7)(3x - 5)(2x + 7).

Answer

6x2+11x−356x^2 + 11x - 35

Solution
F:3x⋅2x=6x2O:3x⋅7=21xI:−5⋅2x=−10xL:−5⋅7=−3521x−10x=11x\begin{aligned} \text{F:}\quad &3x \cdot 2x = 6x^2 \\ \text{O:}\quad &3x \cdot 7 = 21x \\ \text{I:}\quad &-5 \cdot 2x = -10x \\ \text{L:}\quad &-5 \cdot 7 = -35 \\ &21x - 10x = 11x \end{aligned}

Exercise 3: Trinomial times trinomial

Expand (x2+2x−1)(x2−2x+3)(x^2 + 2x - 1)(x^2 - 2x + 3).

Answer

x4−2x2+8x−3x^4 - 2x^2 + 8x - 3

Solution
x2⋅(x2−2x+3)=x4−2x3+3x22x⋅(x2−2x+3)=2x3−4x2+6x−1⋅(x2−2x+3)=−x2+2x−3x3: −2+2=0;x2: 3−4−1=−2;x: 6+2=8\begin{aligned} x^2 \cdot (x^2 - 2x + 3) &= x^4 - 2x^3 + 3x^2 \\ 2x \cdot (x^2 - 2x + 3) &= 2x^3 - 4x^2 + 6x \\ -1 \cdot (x^2 - 2x + 3) &= -x^2 + 2x - 3 \\ &x^3{:}\ -2 + 2 = 0; \quad x^2{:}\ 3 - 4 - 1 = -2; \quad x{:}\ 6 + 2 = 8 \end{aligned}
python · try it yourself
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 213×32213 \times 32.

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.

python · try it yourself
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 (x3+1)(x4−x)(x^3 + 1)(x^4 - x)?

  1. 12
  2. 7
  3. 4
  4. 3

Expand (x+6)(x−2)(x + 6)(x - 2).

  1. x2−12x^2 - 12
  2. x2+4x+12x^2 + 4x + 12
  3. x2+4x−12x^2 + 4x - 12
  4. x2−8x−12x^2 - 8x - 12

Which operation on coefficient lists equals polynomial multiplication?

  1. Element-wise product
  2. Dot product
  3. Concatenation
  4. Convolution