Anda di halaman 1dari 5

Polynomial Evaluation

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

Polynomial Interpolation &


Polynomial Multiplication


A(x ) = a0 + a1x + ..... + an 1x n 1


B (x ) = b0 + b1x + ..... + bn 1x n 1

C (x ) = A(x )B (x )

= c0 + c1x + c2x + ..... + c2n 3x


2

cj =

Given a set of n points in the plane with distinct


x-coordinates, there is exactly one (n-1)-degree
polynomial going through all these points.
Alternate approach to computing p(x)q(x):

2n 3

+ c2n 2x

2n 2




ak bj k

k =0

A straightforward evaluation would take O(n2) time.

Calculate p() on 2n-1 x-values, x0,x1,,x2n-2.


Calculate q() on the same 2n-1 x values.
Find the (2n-2)-degree polynomial that goes through
the points:
{(x0,p(x0)q(x0)), (x1,p(x1)q(x1)), , (x2n-2,p(x2n-2)q(x2n-2))}.

A straightforward evaluation would still take


O(n2) (Why?)
4

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 )

yk = A (xk ) for k=0, 1,...., n-1


1 x0

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 )

where is a primitive 2n-1 th root of unity

Properties of
Primitive Roots of Unity

Primitive Roots of Unity


A number is a primitive n-th root of unity, for n>1,
if

n = 1

The numbers 1, , 2, , n-1 are all distinct
2 k
2 k
Example 2: The complex number zk = cos
+ i sin

is a primitive n-th root of unity, where


and 1 k < n, ( k , n ) = 1

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

Inverse Property: If is a primitive root of unity,


then -1=n-1

Proof: n-1=n=1

Cancellation Property: For non-zero -n<k<n,



Proof:
n 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


The Discrete Fourier Transform

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:

We produce (y0,y1,y2,,yn-1), where yj=p(j)

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

Matrix form: y=Fa, where F[i,j]=ij.

j =0

Corollary: k+n/2= -k.

10

Correctness of the
inverse DFT

The Discrete Fourier Transform




The Inverse Discrete Fourier Transform recovers


the coefficients of an (n-1)-degree polynomial given
its values at 1,,2,,n-1

The DFT and inverse DFT are inverse operations


Proof:
Let A=F -1F. We want to show that A=I,
1 n 1 ki kj
where

A[i, j ] =

n k =0

Matrix form: a=F -1y, where F -1[i,j]=-ij/n.




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

Let a =< a0 , a1 ,...., an -1 >, and yk = A(nk ) =

n -1

Interpolation at the complex roots of unity:


1
1 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 =< y 0 , y1 ,...., yn -1 > is the DFT of the coefficient


vector a=<a0 , a1 ,...., an-1 >,

y = DFTn (a )







a0

a1
n2( n 1) a2




(nn 1)( n 1) an 1
1

n 1
n

a = Vn1 y

Convolution


The DFT and the


inverse DFT can be
used to multiply two
polynomials

So we can get the


coefficients of the
product polynomial
quickly if we can
compute the DFT (and
its inverse) quickly

[a0,a1,a2,...,an-1]

[b0,b1,b2,...,bn-1]

Pad with n 0's

Pad with n 0's

[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]

The Fast Fourier Transform - FFT





An efficient algorithm for computing the DFT


Based on the divide-and-conquer paradigm:

If n is even, we can divide a polynomial

into two polynomials

Component
Multiply

[y0z0,y1z1,...,y2n-1z2n-1]

inverse DFT

[c0,c1,c2,...,c2n-1]
(Convolution)

so that
15

16

The FFT Algorithm

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

Running FFT on a(x) we get:


P0,1,2,3 (1) = P0,2 (1) + 1 P1,3 (1) = 3 + 1 = 4
P0,1,2,3 ( i ) = P0,2 ( 1) + i P1,3 ( 1) = 3 + i
P0,1,2,3 ( 1) = P0,2 (1) 1 P1,3 (1) = 3 1 = 2
P0,1,2,3 ( i ) = P0,2 ( 1) i P1,3 ( 1) = 3 i
P0,2 (1) = P0 (1) + 1 P2 (1) = 3

odd

x yi

a ( x) = x + 3

odd

x x

P0 (1) = a0 = 3

P0,2 ( 1) = P0 (1) + ( 1) P2 (1) = 3

P2 (1) = a2 = 0

P1,3 (1) = P1 (1) + 1 P3 (1) = 1

P1 (1) = a1 = 1

P1,3 ( 1) = P1 (1) + ( 1) P3 (1) = 1

P3 (1) = a3 = 0

return y
}

The running time is O(n log n). [inverse FFT is similar]

a0,1,2,3 (1, i, 1, i ) = ( 4,3 + i, 2,3 i )

17

Example

a ( x) = x + 3

Example

b ( x ) = x 1

P0,1,2,3 (1) = P0,2 (1) + 1 P1,3 (1) = 1 + 1 = 0


P0,1,2,3 ( i ) = P0,2 ( 1) + i P1,3 ( 1) = 1 + i
P0,1,2,3 ( 1) = P0,2 (1) 1 P1,3 (1) = 1 1 = 2
P0,1,2,3 ( i ) = P0,2 ( 1) i P1,3 ( 1) = 1 i

P0,2 ( 1) = P0 (1) + ( 1) P2 (1) = 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:

Running FFT on b(x) we get:

P0,2 (1) = P0 (1) + 1 P2 (1) = 1

18

P0 (1) = b0 = 1
P1 (1) = b1 = 1

P1,3 ( 1) = P1 (1) + ( 1) P3 (1) = 1

P3 (1) = b3 = 0

P0,2 ( 1) = P0 (1) + ( 1) P2 (1) = 4

P1 (1) = 2i 4

P1,3 (1) = P1 (1) + 1 P3 (1) = 8

P3 (1) = 2i 4

P1,3 ( 1) = P1 (1) + ( 1) P3 (1) = 4i

P0,1,2,3 ( i ) = P0,2 ( 1) i P1,3 ( 1) = 4 i 4i = 8


P0,1,2,3 ( 1) = P0,2 (1) 1 P1,3 (1) = 4 + 8 = 4
P0,1,2,3 ( i ) = P0,2 ( 1) + i P1,3 ( 1) = 4 + i 4i = 0

b0,1,2,3 (1, i, 1, i ) = ( 0, 1 + i, 2, 1 i )

P0,2 (1) = P0 (1) + 1 P2 (1) = 4

P2 (1) = 4

P0,1,2,3 (1) = P0,2 (1) + 1 P1,3 (1) = 4 8 = 12

P2 (1) = b2 = 0

P1,3 (1) = P1 (1) + 1 P3 (1) = 1

P0 (1) = 0

19

This means that the coefficients of (ab)(x) are


1
( a b )( x ) = ( 12,8, 4, 0 ) = ( 3, 2,1, 0 ) as expected
4

20

Anda mungkin juga menyukai