Polynomials FFT and DFT
Polynomials FFT and DFT
Battle Plan
• Polynomials
– Algorithms to add, multiply and evaluate polynomials
– Coefficient and point-value representation
• Fourier Transform
– Discrete Fourier Transform (DFT) and inverse DFT to
translate between polynomial representations
– “A Short Digression on Complex Roots of Unity”
– Fast Fourier Transform (FFT) is a divide-and-conquer
algorithm based on properties of complex roots of unity
2
Polynomials
• A polynomial in the variable 𝑥 is a representation of
a function
𝐴 𝑥 = 𝑎𝑛−1 𝑥 𝑛−1 + ⋯ + 𝑎2 𝑥 2 + 𝑎1 𝑥 + 𝑎0
𝑛−1 𝑗
as a formal sum 𝐴 𝑥 = 𝑗=0 𝑎𝑗 𝑥 .
• We call the values 𝑎0 , 𝑎1, … , 𝑎𝑛−1 the coefficients of
the polynomial
• 𝐴 𝑥 is said to have degree 𝑘 if its highest nonzero
coefficient is 𝑎𝑘 .
• Any integer strictly greater than the degree of a
polynomial is a degree-bound of that polynomial
Examples
• 𝐴 𝑥 = 𝑥 3 − 2𝑥 − 1
– 𝐴(𝑥) has degree 3
– 𝐴(𝑥) has degree-bounds 4, 5, 6, … or all values > degree
– 𝐴(𝑥) has coefficients (−1, −2, 0, 1)
• 𝐵 𝑥 = 𝑥3 + 𝑥2 + 1
– 𝐵(𝑥) has degree 3
– 𝐵(𝑥) has degree bounds 4, 5, 6, … or all values > degree
– 𝐵(𝑥) has coefficients (1, 0, 1, 1)
4
Coefficient Representation
• A coefficient representation of a polynomial
𝐴 𝑥 = 𝑛−1 𝑗
𝑗=0 𝑎𝑗 𝑥 of degree-bound 𝑛 is a vector of
coefficients 𝑎 = 𝑎0 , 𝑎1 , … , 𝑎𝑛−1 .
• More examples
– 𝐴 𝑥 = 6𝑥 3 + 7𝑥 2 − 10𝑥 + 9 (9, −10, 7, 6)
– 𝐵 𝑥 = −2𝑥 3 + 4𝑥 − 5 (−5, 4, 0, −2)
• The operation of evaluating the polynomial 𝐴(𝑥) at
point 𝑥0 consists of computing the value of 𝐴 𝑥0 .
• Evaluation takes time Θ(𝑛) using Horner’s rule
𝐴(𝑥0 ) = 𝑎0 + 𝑥0 (𝑎1 + 𝑥0 (𝑎2 + ⋯ + 𝑥0 𝑎𝑛−2 + 𝑥0 𝑎𝑛−1 ⋯ ))
Adding Polynomials
• Adding two polynomials represented by the
coefficient vectors 𝑎 = (𝑎0 , 𝑎1 , … , 𝑎𝑛−1 ) and
𝑏 = (𝑏0 , 𝑏1 , … , 𝑏𝑛−1 ) takes time Θ(𝑛).
• Sum is the coefficient vector 𝑐 = (𝑐0 , 𝑐1 , … , 𝑐𝑛−1 ),
where 𝑐𝑗 = 𝑎𝑗 + 𝑏𝑗 for 𝑗 = 0,1, … , 𝑛 − 1.
• Example
𝐴 𝑥 = 6𝑥 3 + 7𝑥 2 − 10𝑥 + 9 (9, −10, 7, 6)
𝐵 𝑥 = − 2𝑥 3 + 4𝑥 − 5 (−5, 4, 0, −2)
3 2
𝐶(𝑥) = 4𝑥 + 7𝑥 − 6𝑥 + 4 (4, −6,7,4)
6
Multiplying Polynomials
• For polynomial multiplication, if 𝐴(𝑥) and 𝐵(𝑥) are
polynomials of degree-bound n, we say their product
𝐶(𝑥) is a polynomial of degree-bound 2𝑛 − 1.
• Example
6𝑥 3 + 7𝑥 2 − 10𝑥 + 9
− 2𝑥 3 + 4𝑥 − 5
− 30𝑥 3 2
− 35𝑥 + 50𝑥 − 45
24𝑥 4 + 28𝑥 3 − 40𝑥 2 + 36𝑥
− 12𝑥 6 − 14𝑥 5 + 20𝑥 4 − 18𝑥 3
− 12𝑥 6 − 14𝑥 5 + 44𝑥 4 − 20𝑥 3 − 75𝑥 2 + 86𝑥 − 45
Multiplying Polynomials
• Multiplication of two degree-bound n polynomials
𝐴(𝑥) and 𝐵(𝑥) takes time Θ 𝑛2 , since each
coefficient in vector 𝑎 must be multiplied by each
coefficient in vector 𝑏.
• Another way to express the product C(x) is
2𝑛−1 𝑗 𝑗
𝑗=0 𝑐𝑗 𝑥 , where 𝑐𝑗 = 𝑘=0 𝑎𝑘 𝑏𝑗−𝑘 .
• The resulting coefficient vector 𝑐 = (𝑐0 , 𝑐1 , … 𝑐2𝑛−1 )
is also called the convolution of the input vectors 𝑎
and 𝑏, denoted as 𝑐 = 𝑎⨂𝑏.
8
Point-Value Representation
• A point-value representation of a polynomial 𝐴(𝑥)
of degree-bound 𝑛 is a set of 𝑛 point-value pairs
𝑥0 , 𝑦0 , 𝑥1 , 𝑦1 , … , 𝑥𝑛−1 , 𝑦𝑛−1
such that all of the 𝑥𝑘 are distinct and 𝑦𝑘 = 𝐴(𝑥𝑘 )
for 𝑘 = 0, 1, … , 𝑛 − 1.
• Example 𝐴 𝑥 = 𝑥 3 − 2𝑥 + 1
– 𝑥𝑘 0, 1, 2, 3
* 0, 1 , 1, 0 , 2, 5 , 3, 22 +
– 𝐴(𝑥𝑘 ) 1, 0, 5, 22
• Using Horner’s method, 𝒏-point evaluation takes
time Θ(𝑛2 ).
Point-Value Representation
• The inverse of evaluation is called interpolation
– determines coefficient form of polynomial from point-
value representation
– For any set * 𝑥0 , 𝑦0 , 𝑥1 , 𝑦1 , … , 𝑥𝑛−1 , 𝑦𝑛−1 + of 𝑛 point-
value pairs such that all the 𝑥𝑘 values are distinct, there is
a unique polynomial 𝐴(𝑥) of degree-bound 𝑛 such that
𝑦𝑘 = 𝐴(𝑥𝑘 ) for 𝑘 = 0, 1, … , 𝑛 − 1.
• Lagrange’s formula
𝑛−1
𝑗≠𝑘(𝑥 − 𝑥𝑗 )
𝐴 𝑥 = 𝑦𝑘
𝑘=0 𝑗≠k(𝑥𝑘 − 𝑥𝑗 )
• Using Lagrange’s formula, interpolation takes time
Θ(𝑛2 ).
10
Example
• Using Lagrange’s formula, we interpolate the point-
value representation 0, 1 , 1, 0 , 2, 5 , 3, 22 .
𝑥−1 (𝑥−2)(𝑥−3) 𝑥 3 −6𝑥 2 +11𝑥−6 −𝑥 3 +6𝑥 2 −11𝑥+6
– 1 (0−1)(0−2)(0−3) = =
−6 6
(𝑥−0)(𝑥−2)(𝑥−3)
– 0 (1−0)(1−2)(1−3) = 0
(𝑥−0)(𝑥−1)(𝑥−3) 𝑥 3 −4𝑥 2 +3𝑥 −15𝑥 3 +60𝑥 2 −45𝑥
– 5 (2−0)(2−1)(2−3) = 5 =
−2 6
(𝑥−0)(𝑥−1)(𝑥−2) 𝑥 3 −3𝑥 2 +2𝑥 22𝑥 3 −66𝑥 2 +44𝑥
– 22 (3−0)(3−1)(3−2) = 22 =
6 6
1
– 6𝑥 3 + 0𝑥 2 − 12𝑥 + 6
6
– 𝑥 3 − 2𝑥 + 1
11
Adding Polynomials
• In point-value form, addition 𝐶 𝑥 = 𝐴 𝑥 + 𝐵(𝑥) is
given by 𝐶 𝑥𝑘 = 𝐴 𝑥𝑘 + 𝐵(𝑥𝑘 ) for any point 𝑥𝑘 .
– 𝐴: 𝑥0 , 𝑦0 , 𝑥1 , 𝑦1 , … , 𝑥𝑛−1 , 𝑦𝑛−1
– 𝐵: 𝑥0 , 𝑦0′ , 𝑥1 , 𝑦1′ , … , 𝑥𝑛−1 , 𝑦𝑛−1
′
12
Example
• We add 𝐶 𝑥 = 𝐴 𝑥 + 𝐵(𝑥) in point-value form
– 𝐴 𝑥 = 𝑥 3 − 2𝑥 + 1
– 𝐵 𝑥 = 𝑥3 + 𝑥2 + 1
– 𝑥𝑘 = (0, 1, 2, 3)
– 𝐴: 0, 1 , 1, 0 , 2, 5 , 3, 22
– 𝐵: * 0, 1 , 1, 3 , 2, 13 , 3, 37 +
– 𝐶: * 0, 2 , 1, 3 , 2, 18 , 3, 59 +
13
Multiplying Polynomials
• In point-value form, multiplication 𝐶 𝑥 = 𝐴 𝑥 𝐵(𝑥)
is given by 𝐶 𝑥𝑘 = 𝐴 𝑥𝑘 𝐵(𝑥𝑘 ) for any point 𝑥𝑘 .
• Problem: if 𝐴 and 𝐵 are of degree-bound 𝑛, then 𝐶 is
of degree-bound 2𝑛.
• Need to start with “extended” point-value forms for
𝐴 and 𝐵 consisting of 2𝑛 point-value pairs each.
– 𝐴: 𝑥0 , 𝑦0 , 𝑥1 , 𝑦1 , … , 𝑥2𝑛−1 , 𝑦2𝑛−1
– 𝐵: 𝑥0 , 𝑦0′ , 𝑥1 , 𝑦1′ , … , 𝑥2𝑛−1 , 𝑦2𝑛−1
′
14
Example
• We multiply 𝐶 𝑥 = 𝐴 𝑥 𝐵(𝑥) in point-value form
– 𝐴 𝑥 = 𝑥 3 − 2𝑥 + 1
– 𝐵 𝑥 = 𝑥3 + 𝑥2 + 1
– 𝑥𝑘 = (−3, −2, −1, 0, 1, 2, 3) We need 7 coefficients!
– 𝐴: −3, −17 , −2, −3 , −1,1 , 0, 1 , 1, 0 , 2, 5 , 3, 22
– 𝐵: * −3, −20 , −2, −3 , −1, 2 , 0, 1 , 1, 3 , 2, 13 , 3, 37 +
– 𝐶: * −3,340 , −2,9 , −1,2 , 0, 1 , 1, 0 , 2, 65 , 3, 814 +
15
Evaluation Interpolation
Time Θ(𝑛2 ) Time Θ(𝑛2 )
𝐴 𝑥0 , 𝐵 𝑥0 𝐶 𝑥0
𝐴 𝑥1 , 𝐵 𝑥1 Pointwise multiplication 𝐶 𝑥1 Point-value
⋮ Time Θ(𝑛) ⋮ representations
𝐴 𝑥2𝑛−1 , 𝐵(𝑥2𝑛−1 ) 𝐶(𝑥2𝑛−1 )
• Can we do better?
– Using Fast Fourier Transform (FFT) and its inverse, we can do
evaluation and interpolation in time Θ(𝑛 log 𝑛).
• The product of two polynomials of degree-bound 𝑛 can
be computed in time Θ(𝑛 log 𝑛), with both the input and
output in coefficient form.
16
Fourier Transform
• Fourier Transforms originate from signal processing
– Transform signal from time domain to frequency domain
Amplitude
Weight
Time Frequency
– Input signal is a function mapping time to amplitude
– Output is a weighted sum of phase-shifted sinusoids of
varying frequencies
17
18
Complex Roots of Unity
• A complex 𝒏th root of unity (1) is a complex number
𝜔 such that 𝜔𝑛 = 1.
• There are exactly 𝑛 complex 𝑛th root of unity
2𝜋𝑖𝑘
𝑒 𝑛 for 𝑘 = 0, 1, … , 𝑛 − 1
19
Examples
• The complex 4th roots of unity are
1, −1, 𝑖, −𝑖
where −1 = 𝑖.
• The complex 8th roots of unity are all of the above,
plus four more
1 𝑖 1 𝑖 1 𝑖 1 𝑖
+ , − ,− + , and − −
2 2 2 2 2 2 2 2
• For example
2
1 𝑖 1 2𝑖 𝑖 2
+ = + + =𝑖
2 2 2 2 2
20
Principal 𝑛th Root of Unity
• The value
2𝜋𝑖
𝜔𝑛 = 𝑒 𝑛
imaginary
𝜔82 = 𝑖
𝑖
1 𝑖 1 𝑖
𝜔83 =− + 𝜔18 = +
2 2 2 2
1 𝑖 1 𝑖
𝜔85 = − − 𝜔87 = −
2 2 2 2
−𝑖
𝜔86 = −𝑖
22
Cancellation Lemma
• For any integers 𝑛 ≥ 0, 𝑘 ≥ 0, and 𝑏 > 0,
𝑑𝑘
𝜔𝑑𝑛 = 𝜔𝑛𝑘 .
• Proof
2𝜋𝑖 𝑑𝑘 2𝜋𝑖 𝑘
𝑑𝑘
𝜔𝑑𝑛 = 𝑒 𝑑𝑛 = 𝑒 𝑛 = 𝜔𝑛𝑘
𝑛
• For any even integer 𝑛 > 0, 𝜔𝑛 2 = 𝜔2 = −1.
6
• Example 𝜔24 = 𝜔4
6 6
= 𝑒 2𝜋𝑖 24 = 𝑒
6 2𝜋𝑖 2𝜋𝑖
– 𝜔24 = 𝑒 24 4 = 𝜔4
23
Halving Lemma
• If 𝑛 > 0 is even, then the squares of the 𝑛 complex
𝑛th roots of unity are the 𝑛 2 complex 𝑛 2th roots of
unity.
• Proof
2
– By the cancellation lemma, we have 𝜔𝑛𝑘 = 𝜔𝑛𝑘 2 for any
nonnegative integer 𝑘.
• If we square all of the complex 𝑛th roots of unity,
then each 𝑛 2th root of unity is obtained exactly twice
𝑘+𝑛 2 2
– 𝜔𝑛 2
= 𝜔𝑛2𝑘+𝑛 = 𝜔𝑛2𝑘 𝜔𝑛𝑛 = 𝜔𝑛2𝑘 = 𝜔𝑛𝑘
𝑘+𝑛
– Thus, 𝜔𝑛𝑘 and 𝜔𝑛 2
have the same square
24
Summation Lemma
• For any integer 𝑛 ≥ 1 and nonzero integer 𝑘 not
𝑛−1 𝑗
divisible by 𝑛, 𝑗=0 𝜔𝑛𝑘 = 0.
• Proof
𝑛−1 𝑗 𝑥 𝑛 −1
– Geometric series 𝑗=0 𝑥 = 𝑥−1
𝑘 𝑛 𝑛 𝑘 −1
𝑛−1 𝑗 𝜔𝑛 −1 𝜔𝑛 1 𝑘 −1
– 𝑗=0 𝜔𝑛𝑘 = 𝑘 −1 = 𝑘 −1 = 𝑘 −1 =0
𝜔𝑛 𝜔𝑛 𝜔𝑛
– Requiring that 𝑘 not be divisible by 𝑛 ensures that the
denominator is not 0, since 𝜔𝑛𝑘 = 1 only when k is divisible
by 𝑛
25
26
Fast Fourier Transform (FFT)
• Fast Fourier Transform (FFT) takes advantage of the
special properties of the complex roots of unity to
compute DFT𝑛 (a) in time Θ(𝑛 log 𝑛).
• Divide-and-conquer strategy
– define two new polynomials of degree-bound 𝑛 2, using
even-index and odd-index coefficients of 𝐴(𝑥) separately
𝑛
– 𝐴 0 𝑥 = 𝑎0 + 𝑎2 𝑥 + 𝑎4 𝑥 2 + ⋯ + 𝑎𝑛−2 𝑥 2−1
𝑛
– 𝐴 1 𝑥 = 𝑎1 + 𝑎3 𝑥 + 𝑎5 𝑥 2 + ⋯ + 𝑎𝑛−1 𝑥 2−1
– 𝐴 𝑥 = 𝐴 0 𝑥 2 + 𝑥𝐴 1 (𝑥 2 )
27
28
Example
• 𝐴 𝑥 = 𝑎0 + 𝑎1 𝑥 + 𝑎2 𝑥 2 + 𝑎3 𝑥 3 of degree-bound 4
– 𝐴 𝜔40 =𝐴 1 = 𝑎0 + 𝑎1 + 𝑎2 + 𝑎3
– 𝐴 𝜔41 =𝐴 𝑖 = 𝑎𝑜 + 𝑎1 𝑖 − 𝑎2 − 𝑎3 𝑖
– 𝐴 𝜔42 =𝐴 −1 = 𝑎0 − 𝑎1 + 𝑎2 − 𝑎3
– 𝐴 𝜔43 =𝐴 −𝑖 = 𝑎0 − 𝑎1 𝑖 + 𝑎2 + 𝑎3 𝑖
• Using 𝐴 𝑥 = 𝐴 0 𝑥 2 + 𝑥𝐴 1 𝑥 2
– 𝐴 𝑥 = 𝑎0 + 𝑎2 𝑥 2 + 𝑥 𝑎1 + 𝑎3 𝑥 2
– 𝐴 𝜔40 = 𝐴 1 = 𝑎0 + 𝑎2 + 1(𝑎1 + 𝑎3 )
– 𝐴 𝜔41 = 𝐴 𝑖 = 𝑎0 − 𝑎2 + 𝑖(𝑎1 − 𝑎3 )
– 𝐴 𝜔42 = 𝐴 −1 = 𝑎0 + 𝑎2 − 1 𝑎1 + 𝑎3
– 𝐴 𝜔43 = 𝐴 −𝑖 = 𝑎0 − 𝑎2 − 𝑖(𝑎1 − 𝑎3 )
29
30
Why Does It Work?
• For 𝑦0 , 𝑦1 , … 𝑦𝑛 2−1 (line 11)
0 1
𝑦𝑘 = 𝑦𝑘 + 𝜔𝑛𝑘 𝑦𝑘
0
=𝐴 𝜔𝑛2𝑘 + 𝜔𝑛𝑘 𝐴 1 𝜔𝑛2𝑘
=𝐴 𝜔𝑛𝑘
• For 𝑦𝑛 2 , 𝑦𝑛 2+1 , … , 𝑦𝑛−1 (line 12)
𝑦𝑘+𝑛 2 = 𝑦𝑘0 − 𝜔𝑛𝑘 𝑦𝑘1
𝑘+ 𝑛 2 𝑘+ 𝑛 2
= 𝑦𝑘0 + 𝜔𝑛 𝑦𝑘1 since −𝜔𝑛𝑘 = 𝜔𝑛
𝑘+ 𝑛
0
=𝐴 𝜔𝑛2𝑘 + 𝜔𝑛 2
𝐴 1 𝜔𝑛2𝑘
𝑘+ 𝑛
0
=𝐴 𝜔𝑛2𝑘+𝑛 + 𝜔𝑛 2
𝐴 1 𝜔𝑛2𝑘+𝑛
since 𝜔𝑛2𝑘+𝑛 = 𝜔𝑛2𝑘
𝑘+ 𝑛 2
= 𝐴 𝜔𝑛
31
(𝑎0 , 𝑎1 , 𝑎2 , 𝑎3 , 𝑎4 , 𝑎5 , 𝑎6 , 𝑎7 )
(𝑎0 , 𝑎2 , 𝑎4 , 𝑎6 ) (𝑎1 , 𝑎3 , 𝑎5 , 𝑎7 )
32
Interpolation
• Interpolation by computing the inverse DFT, denoted
by a = DFT𝑛−1 (𝑦).
• By modifying the FFT algorithm, we can compute
DFT𝑛−1 in time Θ(𝑛 log 𝑛).
– switch the roles of 𝑎 and 𝑦
– replace 𝜔𝑛 by 𝜔𝑛−1
– divide each element of the result by 𝑛
33