Polynomials and The FFT: Chapter 32 in CLR Chapter 30 in CLRS
Polynomials and The FFT: Chapter 32 in CLR Chapter 30 in CLRS
Chapter 32 in CLR
Chapter 30 in CLRS
Goal
Goal: Show how the Fast Fourier Transform (FFT) can reduce the time to
multiply polynomials to θ(nlogn).
2
Polynomials
A polynomial in the variable x over the field F is a function A(x) that can be
represented as
A(x) = ∑j=0,..,n-1 aj xj.
We call n a degree-bound of the polynomial, and the values a0,a1,*,an-1 are the
coefficients of the polynomial (they are drawn from the field F). A polynomial is
said to have degree k if its highest nonzero coefficient is ak.
3
Polynomial addition
If A(x) and B(x) are polynomials of degree bound n, we say that their sum is a
polynomial C(x), also of degree bound n, such that C(x) = A(x) + B(x) for all x in F.
That is, if
A(x) = ∑j=0,..,n-1 aj xj
and
B(x) = ∑j=0,..,n-1 bj xj
then
C(x) = ∑j=0,..,n-1 cj xj,
where cj = aj + bj for j = 0,1,*,n-1.
4
Polynomial multiplication
If A(x) and B(x) are polynomials of degree bound n, we say that their product is a
polynomial C(x) (of degree bound 2n-1, but we may also say that 2n is a degree
bound), such that C(x) = A(x) B(x) for all x in F. That is, if
A(x) = ∑j=0,..,n-1 aj xj
and
B(x) = ∑j=0,..,n-1 bj xj
then
C(x) = ∑j=0,..,2n-2 cj xj,
where cj = ∑k=0,..,j ak bj-k for j = 0,1,*,2n-2. The resulting coefficient vector c is
also called the convolution of the input vectors a and b, and is denoted by
c=a ⊗ b.
Clearly, we can calculate C(x) in θ(n2) time.
5
32.1: Representation of polynomials
A polynomial has only one coefficient representation (given n), and many point-
value representations.
6
32.1: Representation of polynomials
7
32.1: Representation of polynomials
8
32.1: Representation of polynomials
Proof:
The set of equations {yk = A(xk)} k=0,1,*,n-1 may be written in matrix form as
9
32.1: Representation of polynomials
and the existence of its solution is equivalent to the existence of the inverse of the
matrix of coefficients. This matrix on the left, denoted by V(x0,x1,*,xn-1) is the
Vandermonde matrix whose determinant is ∏j<k(xk-xj), and therefore it is invertible
if the xk –s are distinct. Therefore, the vector of coefficients, a, can be solved for
uniquely given the point value representation:
a = V(x0,x1,*,xn-1) -1 y. █
10
32.1: Representation of polynomials
The proof describes an algorithm via a solution of a set of linear equations. This
may be done in time O(n3).
An easy exercise shows that using this formula, one may calculate the
coefficients in time θ(n2).
11
32.1: Representation of polynomials
12
32.1: Representation of polynomials
Similarly, for multiplication, if C(x) = A(x)B(x), then C(xk) = A(xk)B(xk), for any point
xk. However, this time, n points will not suffice, since the degree bound for C is
now 2n. So now we need to represent A via {(x0,y0),(x1,y1),*,(x2n-1,y2n-1)}, and B
via {(x0,y’0),(x1,y’1),*,(x2n-1,y’2n-1)} (again, same xk–s!), and the point-value
representation for C is {(x0,y0y’0),(x1,y1y’1),*,(x2n-1,y2n-1y’2n-1)} (calculated in time
θ(n)).
13
32.1: Representation of polynomials
14
32.1: Representation of polynomials
15
32.1: Representation of polynomials
Given the FFT, we have the following θ(nlogn)-time procedure for multiplying two
polynomials A(x) and B(x) of degree-bound n, where the input and output
representations are in coefficient form. We assume that n is a power of 2; this
requirement can always be met by adding high-order zero coefficients.
16
32.1: Representation of polynomials
Steps (1) & (3) take time θ(n), and steps (2) & (4) take time θ(nlogn). Therefore,
once we show how to use FFT, we will have proven the following theorem:
17
32.1: Representation of polynomials
Theorem 32.2:
18
32.2: The DFT and FFT – mathematical background
19
32.2: The DFT and FFT – mathematical background
Essential properties of the complex n-th roots of unity are given in the
following lemmas.
20
32.2: The DFT and FFT – mathematical background
Lemma 32.5 (Halving lemma): If n > 0 is even, then the squares of the n
complex n-th roots of unity are the n/2 complex (n/2)-th roots of unity. [This is
essential for the Divide and Conquer approach of FFT.]
Lemma 32.6 (Summation lemma): For any integer n > 1 and nonnegative
integer k not divisible by n,
∑j=0,..,n-1 (ωnk)j = 0.
21
32.2: The DFT and FFT
23
32.2: The DFT and FFT
24
32.2: The DFT and FFT
By the halving lemma, the list of values (ωn0)2, (ωn1)2, *, (ωnn-1)2 consists
not of n distinct values but only of the n/2 complex (n/2)-th roots of unity,
with each root occurring exactly twice. Therefore, the polynomials A[0](x) and
A[1](x) of degree-bound n/2 are recursively evaluated at the n/2 complex
(n/2)-th roots of unity. These two sub-problems have exactly the same form
of the original problem, but are half the size. We have now successfully
divided an n-element DFTn computation into two n/2-element DFTn/2
computations.
This decomposition is the basis for the following recursive FFT algorithm,
which computes the DFT of an n-element vector a = (a0,a1,*,an-1), where n
is a power of 2.
25
32.2: The DFT and FFT
RECURSIVE-FFT(a)
n ← length[a] ► n is a power of 2
if n = 1
then return a
ωn ← e2πi/n
ω←1
a[0] ← (a0,a2,*,an-2)
a[1] ← (a1,a3, *,an-1)
y[0] ← RECURSIVE-FFT(a[0])
y[1] ← RECURSIVE-FFT(a[1])
for k ← 0 to n/2 – 1
do yk ← yk[0] + ωyk[1]
yk+(n/2) ← yk[0] – ωyk[1]
ω ← ω ωn
return y ► y is assumed to be column vector
26
32.2: The DFT and FFT
27
32.2: Interpolation at the complex roots of unity
28
32.2: Interpolation at the complex roots of unity
Theorem 32.7: For j, k = 0,1,*,n-1, The (j,k) entry of Vn-1 is ωn-kj /n.
Proof: Multiply.
29
32.2: Interpolation at the complex roots of unity
We now compare:
and discover that by modifying the FFT algorithm to switch the roles of a
and y, and replace ωn by ωn-1 and divide each element of the result by n, we
compute the inverse DFT. Thus, DFT-1n can be computed in θ(nlogn) time as
well.
30
32.2: Interpolation at the complex roots of unity
Thus, by using the FFT and the inverse FFT, we can transform a polynomial
of degree bound n back and forth between its coefficient representation and
a point-value representation in time θ(nlogn). In the context of polynomial
multiplication, we have shown the following:
Theorem 32.7: For any two vectors a and b of length n, where n is a power
of 2,
a ⊗ b = DFT-12n(DFT2n(a) DFT2n(b)),
where the vectors a and b are padded with 0’s to length 2n and “” denotes
the componentwise product of two 2n-element vectors.
31
32.3: Implementation
32
What’s next?
33
Lemma: Let m = 2tn/2 + 1 where t is a positive integer, and n is an even
positive number. Then ω = 2t is an n-th root of unity in Zm, and we may use
it instead of ωn to perform FFT over Zm.
34