Polynomials - & - FFT
Polynomials - & - FFT
Ruta Mehta
University of Illinois, Urbana-Champaign
Spring 2021
Polynomials, Convolutions
and FFT
Lecture 2
Jan 26/28, 2021
Our goal:
Multiplication of two degree n polynomials in O(n log n) time.
Surprising and non-obvious.
Algorithmic ideas
change in representation
mathematical properties of polynomials
divide and conquer
Example
p(x) = 3 − 4x + 5x 3
a0 = 3, a1 = −4, a2 = 0, a3 = 5 and deg (p) = 3
Ruta (UIUC) CS473 5 Spring 2021 5 / 55
Polynomials
Definition
A polynomial is a function of one variable built from additions,
subtractions and multiplications (but no divisions).
n−1
X
p(x) = aj x j
j =0
Coefficient Representation
Polynomials represented by vector a = (a0 , a1 , . . . an−1 ) of
coefficients.
Question
How long does evaluation really take? O(n) time?
Ignore this issue for now. Can get around it for applications of
interest where one typically wants to compute p(α) mod m for
some number m.
Definition
The convolution of vectors a = (a0 , a1 , . . . an ) and
b = (b0 , b1 , . . . bm ) is the vector c = (c0 , c1 , . . . cn+m ) where
X
ck = ai · bj
i ,j : i +j =k
Representation
Polynomials represented by vector a = (a0 , a1 , . . . an−1 ) of
coefficients.
Representation
Polynomials represented by vector a = (a0 , a1 , . . . an−1 ) of
coefficients.
Question
Are there other useful ways to represent polynomials?
Representation
Polynomials represented by vector scale factor an−1 and roots
r1 , r2 , . . . , rn−1 .
Representation
Polynomials represented by vector scale factor an−1 and roots
r1 , r2 , . . . , rn−1 .
Representation
Polynomials represented by vector scale factor an−1 and roots
r1 , r2 , . . . , rn−1 .
Representation
Polynomials represented by vector scale factor an−1 and roots
r1 , r2 , . . . , rn−1 .
Representation
Polynomials represented by vector scale factor an−1 and roots
r1 , r2 , . . . , rn−1 .
Representation
Polynomials represented by (x0 , y0 ), (x1 , y1 ), . . . , (xn−1 , yn−1 ).
Representation
Polynomials represented by (x0 , y0 ), (x1 , y1 ), . . . , (xn−1 , yn−1 ).
Representation
Polynomials represented by (x0 , y0 ), (x1 , y1 ), . . . , (xn−1 , yn−1 ).
Theorem
Given a list {(x0 , y0 ), (x1 , y1 ), . . . (xn−1 , yn−1 )} there is exactly
one polynomial p of degree n − 1 such that p(xj ) = yj for
j = 0, 1, . . . , n − 1.
Theorem
Given a list {(x0 , y0 ), (x1 , y1 ), . . . (xn−1 , yn−1 )} there is exactly
one polynomial p of degree n − 1 such that p(xj ) = yj for
j = 0, 1, . . . , n − 1.
So representation is valid.
Theorem
Given a list {(x0 , y0 ), (x1 , y1 ), . . . (xn−1 , yn−1 )} there is exactly
one polynomial p of degree n − 1 such that p(xj ) = yj for
j = 0, 1, . . . , n − 1.
So representation is valid.
Can use same x0 , x1 , . . . , xn−1 for all polynomials of degree n − 1.
No need to store them explicitly and hence need only n numbers
y0 , y1 , . . . , yn−1 .
(
For n = 3, p(x) =
For n = 3, p(x) =
Example
3+4x +6x 2 +2x 3 +x 4 +10x 5 = (3+6x 2 +x 4 )+x(4+2x 2 +10x 4 )
Example
3+4x +6x 2 +2x 3 +x 4 +10x 5 = (3+6x 2 +x 4 )+x(4+2x 2 +10x 4 )
Example
3+4x +6x 2 +2x 3 +x 4 +10x 5 = (3+6x 2 +x 4 )+x(4+2x 2 +10x 4 )
Example
3+4x +6x 2 +2x 3 +x 4 +10x 5 = (3+6x 2 +x 4 )+x(4+2x 2 +10x 4 )
aodd (x) = a1 + a3 x + a5 x 2 + · · ·
aodd (x) = a1 + a3 x + a5 x 2 + · · ·
aodd (x) = a1 + a3 x + a5 x 2 + · · ·
Choose n samples
x0 , x1 , x2 , . . . , xn/2−1 , −x0 , −x1 , . . . , −xn/2−1
Evaluate aeven and aodd at x02 , x12 , x22 , . . . , xn/2−1
2
.
Choose n samples
x0 , x1 , x2 , . . . , xn/2−1 , −x0 , −x1 , . . . , −xn/2−1
Evaluate aeven and aodd at x02 , x12 , x22 , . . . , xn/2−1
2
.
For each i = 0 to (n/2 − 1), evaluate
a(xi ) = aeven (xi2 ) + xi aodd (xi2 )
a(−xi ) = aeven (xi2 ) − xi aodd (xi2 )
Choose n samples
x0 , x1 , x2 , . . . , xn/2−1 , −x0 , −x1 , . . . , −xn/2−1
Evaluate aeven and aodd at x02 , x12 , x22 , . . . , xn/2−1
2
.
For each i = 0 to (n/2 − 1), evaluate
a(xi ) = aeven (xi2 ) + xi aodd (xi2 )
a(−xi ) = aeven (xi2 ) − xi aodd (xi2 )
Total of O(n) work!
Choose n samples
x0 , x1 , x2 , . . . , xn/2−1 , −x0 , −x1 , . . . , −xn/2−1
Evaluate aeven and aodd at x02 , x12 , x22 , . . . , xn/2−1
2
.
For each i = 0 to (n/2 − 1), evaluate
a(xi ) = aeven (xi2 ) + xi aodd (xi2 )
a(−xi ) = aeven (xi2 ) − xi aodd (xi2 )
Total of O(n) work!
Suppose we can make this work recursively. Then
T (n) = 2T (n/2) + O(n) which implies T (n) = O(n log n)
Definition
Given a set X of numbers square(X ) (for square of X ) is the set
{x 2 | x ∈ X }.
Definition
Given a set X of numbers square(X ) (for square of X ) is the set
{x 2 | x ∈ X }.
Definition
A set X of n numbers is collapsible if square(X ) ⊂ X and
|square(X )| = n/2.
Definition
Given a set X of numbers square(X ) (for square of X ) is the set
{x 2 | x ∈ X }.
Definition
A set X of n numbers is collapsible if square(X ) ⊂ X and
|square(X )| = n/2.
Definition
A set X of n numbers (for n a power of 2) is recursively collapsible if
n = 1 or if X is collapsible and square(X ) is recursively collapsible.
√
If z0 = x02 and −z0 = xn/4
2
then x0 = −1xn/4 That is
x0 = ixn/4 where i is the imaginary number.
√
If z0 = x02 and −z0 = xn/4
2
then x0 = −1xn/4 That is
x0 = ixn/4 where i is the imaginary number.
Can continue recursion but need to go to complex numbers.
Notation
√
For the rest of lecture, i stands for −1
Definition
Complex numbers are points lying in the complex plane represented as
√
Cartesian a + ib = a2 + b 2 e (arctan(b/a))i
Polar re θi = r (cos θ + i sin θ)
Thus, e πi = −1 and e 2πi = 1.
e z = 1 + z/1! + z 2 /2! + . . . + z j /j ! + . . .
Therefore
Proposition
Let ωk be e 2πi /k . The equation x k = 1 has k distinct complex roots
given by ωkj = e (2πj )i /k for j = 0, 1, . . . k − 1
Proof.
(ωkj )k = (e 2πji /k )k = e 2πji = (e 2πi )j = (1)j = 1
Ruta (UIUC) CS473 31 Spring 2021 31 / 55
Roots of unity form a collapsible set
Observation 1: ωkj = ωkj mod k
Lemma
Assume n is a power of 2. The n’th roots of unity are a recursively
collapsible set.
Proof.
Let Xn = {1, ωn , ωn2 , . . . , ωnn−1 }.
Lemma
Assume n is a power of 2. The n’th roots of unity are a recursively
collapsible set.
Proof.
Let Xn = {1, ωn , ωn2 , . . . , ωnn−1 }. (ωnn/2+j )2 = ωnn+2j = ωn2j ,
Lemma
Assume n is a power of 2. The n’th roots of unity are a recursively
collapsible set.
Proof.
Let Xn = {1, ωn , ωn2 , . . . , ωnn−1 }. (ωnn/2+j )2 = ωnn+2j = ωn2j , for
each j < n/2.
Lemma
Assume n is a power of 2. The n’th roots of unity are a recursively
collapsible set.
Proof.
Let Xn = {1, ωn , ωn2 , . . . , ωnn−1 }. (ωnn/2+j )2 = ωnn+2j = ωn2j , for
each j < n/2.
Definition
Given vector a = (a0 , a1 , . . . , an−1 ) the Discrete Fourier Transform
(DFT) of a is the vector a0 = (a00 , a10 , . . . , an−1
0
) where aj0 = a(ωnj )
for 0 ≤ j < n.
Definition
Given vector a = (a0 , a1 , . . . , an−1 ) the Discrete Fourier Transform
(DFT) of a is the vector a0 = (a00 , a10 , . . . , an−1
0
) where aj0 = a(ωnj )
for 0 ≤ j < n.
Convolutions (products)
Compute convolution c = (c0 , c1 , . . . , c2n−2 ) of
a = (a0 , a1 , . . . an−1 ) and b = (b0 , b1 , . . . bn−1 )
1 Evaluate a and b at some n sample points.
2 Compute sample representation of product. That is
c 0 = (a00 b00 , a10 b10 , . . . , an−1
0 0
bn−1 ).
3 Compute coefficients of unique polynomial associated with
sample representation of product. That is compute c from c 0 .
Convolutions (products)
Compute convolution c = (c0 , c1 , . . . , c2n−2 ) of
a = (a0 , a1 , . . . an−1 ) and b = (b0 , b1 , . . . bn−1 )
1 Evaluate a and b at the nth roots of unity.
2 Compute sample representation of product. That is
c 0 = (a00 b00 , a10 b10 , . . . , an−1
0 0
bn−1 ).
3 Compute coefficients of unique polynomial associated with
sample representation of product. That is compute c from c 0 .
Convolutions (products)
Compute convolution c = (c0 , c1 , . . . , c2n−2 ) of
a = (a0 , a1 , . . . an−1 ) and b = (b0 , b1 , . . . bn−1 )
1 Evaluate a and b at the nth roots of unity.
2 Compute sample representation of product. That is
c 0 = (a00 b00 , a10 b10 , . . . , an−1
0 0
bn−1 ).
3 Compute coefficients of unique polynomial associated with
sample representation of product. That is compute c from c 0 .
x02 . . . x0n−1
1 x0 a0 a00
n−1
1 x
1
2
x1 . . . x1 a1 a10
.. .
. .. .. .. .. ...
. . . . . .
= a0
n−1
2
aj
1 xj xj . . . xj
.j
.
.. .. .. ... ..
. .. ..
. . .
n−1 0
1 xn−1 2
xn−1 . . . xn−1 an−1 an−1
a0 a00
1 1 1 ... 1
1 ω ω2 ... ω n−1 a1 a10
.. .. .. .. .. .. ..
. . . . .
.
.
= a0
1 ωj ω 2j ... ω j (n−1) aj
.j
.. .. .. .. .. .
.. ..
. . . . .
0
1 ω n−1 ω 2(n−1) ... ω (n−1)(n−1)
an−1 an−1
−1
a0 a00
1 1 1 ... 1
a1 1 ω ω2 ω n−1
... a10
.. .. .. .. ..
..
..
.
. . . .. .
=
j 2j j (n−1) a0
aj 1 ω ω ... ω
.j
. . .. .. .. ..
.. .. ..
. . . .
n−1 2(n−1) (n−1)(n−1) 0
an−1 1 ω ω ... ω an−1
Convolutions
Compute convolution of a = (a0 , a1 , . . . an−1 ) and
b = (b0 , b1 , . . . bn−1 )
1 Compute values of a and b at the 2nth roots of unity
2 Compute sample representation c 0 of product c = a · b
3 Compute c from c 0 using inverse Fourier transform.
we expand this recursive structure completely, we see that the circuit splits naturally into
arts. The left half computes the bit-reversal permutation of the input. To find the position of
n this permutation, write k in binary, and then read the bits backward. For example, in an
ment bit-reversal permutation, P [3] = P [0112 ] ends up in position 6 = 1102 . The right half of
butterfly
T circuit is aRuta (UIUC) network. Butterfly CS473
networks are45often used to route between processors
Spring 2021 45 / 55
Numerical Issues
Proof.
Exercise.
T = 10110010 . . .
Example:
P = 010
T = 10110010 . . .
Example:
P = 010
T = 10110010 . . .
Example:
P = 010