Algorithms
Horners Rule:
Given coefficients (a0,a1,a2,,an-1), defining polynomial
n 1
p ( x ) = ai x i
i =0
Dana Shapira
Lesson #10:
The Fast Fourier Transform
Given x0, we can evaluate p(x0) in O(n) time using the equation
p ( x0 ) = ( ( (an 1 x0 + an 2 ) x0 + an 3 ) x0 + ) + a1 x0 + a0
Eval(A,x){
// A=(a0,a1,a2,,an-1)
result=an-1;
for(i=n-2;i0;i--){
result*=x0+ai
return result;
}
1
Polynomial Multiplication
Problem
C (x ) = A(x )B (x )
cj =
2n 3
+ c2n 2x
2n 2
ak bj k
k =0
Polynomial Evaluation
For any set {(x0 , y 0 ),(x1 , y1 ),.....(xn-1 , yn 1 )} of n point-value
pairs, there is a unique polinom A(x) of degree n-1, such that
a0 , a1 ,...., an 1
Ordinary Mult
b0 , b1 ,...., bn 1
(n 2 )
1 x1
if X =
1 x
n 1
x 02
x12
xn21
x 0n 1
x1n 1
then
xnn11
a0 y0
a y
1
1
=
X
an 1 yn 1
(n logn )
A(2 n 2 ), B(2 n 2 )
a0
y0
a
y
1 = X 1 1
an 1
yn 1
C (0 )
Pointwise Mult
(n )
C (1 )
C (2 n 2 )
Properties of
Primitive Roots of Unity
Interpolation
(n log n )
A(1 ), B (1 )
j <k
Evalation
A(0 ), B(0 )
det(X ) = (xk x j ) 0 if xk's are distinct, thus, a j 's can be solved uniquely.
c0 , c1 ,...., c2 n 2
i = 1
j=0
kj
n 1
kj
=0
j =0
( k ) n 1 ( n ) k 1 (1) k 1
11
=
= k
=
=0
k 1
k 1
1 k 1
Properties of
Primitive Roots of Unity
Reduction Property:
If w is a primitve (2n)-th root of unity, then 2 is a
primitive n-th root of unity.
Proof: If 1,,2,,2n-1 are all distinct, so are
1,2,(2)2,,(2)n-1
1,,2,,n-1
Reflective Property:
If n is even, then n/2 = -1.
Proof: By the cancellation property, for k=n/2:
ij
That is, y j = ai
n 1
i =0
n 1
0 =
Given coefficients (a0,a1,a2,,an-1) for an (n-1)degree polynomial p(x). The Discrete Fourier
Transform is to evaluate p at the values
( n / 2) j
= +
0
n/2
+ +
0
n/2
++ +
0
n/2
= ( n / 2)(1 +
n/2
j =0
10
Correctness of the
inverse DFT
n k =0
If i = j
A[i, i ] =
1 n 1 ki ki 1 n 1 0 1
= n =1
= n
n k =0
n
k =0
n 1
11
If i j A[i, j ] = 1 ( j i ) k = 0 by cancellation
n k =0
property.
12
DFT
Evaluate A(x ) =
DFT
n -1
aj x j
j
=0
at n0 , n1 ,....., nn -1
n -1
2
1 n n
1 2 4
n
n
2( n 1)
n 1
1 n n
y = Vn a, (Vn ) k , j = kjn
a j nkj
y0
y1
y2 =
y
n1
=0
y = DFTn (a )
a0
a1
n2( n 1) a2
(nn 1)( n 1) an 1
1
n 1
n
a = Vn1 y
Convolution
[a0,a1,a2,...,an-1]
[b0,b1,b2,...,bn-1]
[a0,a1,a2,...,an-1,0,0,...,0]
[b0,b1,b2,...,bn-1,0,0,...,0]
DFT
DFT
[y0,y1,y2,...,y2n-1]
[z0,z1,z2,...,z2n-1]
Component
Multiply
[y0z0,y1z1,...,y2n-1z2n-1]
inverse DFT
[c0,c1,c2,...,c2n-1]
(Convolution)
so that
15
16
Example
FFT(n,a0,a1,a2,,an-1,){
if n=1 return p0=y=a
x
0
// recursive call with 2 as (n/2) root
p0,2,4,,n-2(2)=yeven FFT(n/2, a0,a2,a4,,an-2, 2)
p1,3,5,,n-1(2)=yodd FFT(n/2, a1,a3,a5,,an-1, 2)
// combine using x=wi
for(i=0;i<n/2-1;i++) // reflective property
yi yi
even
yi + n 2 yi
+ x yi
even
b ( x ) = x 1
odd
x yi
a ( x) = x + 3
odd
x x
P0 (1) = a0 = 3
P2 (1) = a2 = 0
P1 (1) = a1 = 1
P3 (1) = a3 = 0
return y
}
17
Example
a ( x) = x + 3
Example
b ( x ) = x 1
a ( x) = x + 3
b ( x ) = x 1
Lets compute:
( a b )(1, i, 1, i ) = ( 4 0, ( 3 + i )( 1 + i ) , 2 ( 2 ) , ( 3 i )( 1 i ) ) = ( 0, 2i 4, 4, 2i 4 )
Now, apply the inverse FFT, using the same notation:
18
P0 (1) = b0 = 1
P1 (1) = b1 = 1
P3 (1) = b3 = 0
P1 (1) = 2i 4
P3 (1) = 2i 4
b0,1,2,3 (1, i, 1, i ) = ( 0, 1 + i, 2, 1 i )
P2 (1) = 4
P2 (1) = b2 = 0
P0 (1) = 0
19
20