Docsity

Implementing the Fast Fourier Transform (FFT) Algorithm for Polynomial Multiplication, Slides of Introduction to Computers

Toggle sidebar
bg1
Lecture Overview
In this lecture we will discuss two main related topics
1. How to multiply two large polynomials (and therefore integers) using
Fourier transforms
2. How to implement the fast Fourier transform (FFT) from scratch
IBy doing this we will reduce the time complexity of polynomial
multiplication from O(n2)to O(nlog n).
IPerhaps more importantly, you will also learn one of the most useful
and widely deployed computational tools in engineering.
Raphaël Clifford
COMS21103 Slide 1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23

Partial preview of the text

Download Implementing the Fast Fourier Transform (FFT) Algorithm for Polynomial Multiplication and more Slides Introduction to Computers in PDF only on Docsity!

Lecture Overview

In this lecture we will discuss two main related topics

  1. How to multiply two large polynomials (and therefore integers) using Fourier transforms
  2. How to implement the fast Fourier transform (FFT) from scratch I (^) By doing this we will reduce the time complexity of polynomial multiplication from O(n^2 ) to O(n log n). I (^) Perhaps more importantly, you will also learn one of the most useful and widely deployed computational tools in engineering.

Raphaël Clifford

Polynomials (1)

I (^) A degree n − 1 polynomial in x can be seen as a function:

A(x) =

n∑− 1

i= 0

ai · xi^.

I (^) Any integer greater than the degree of a polynomial is a degree-bound of that polynomial. I (^) The polynomial A in x is:

a 0 · x^0 + a 1 · x^1 + a 2 · x^2 + · · · + an− 1 xn−^1.

I (^) The values ai are the coefficients, the degree is n − 1 and n, for example, is a degree-bound I (^) We can express any integer as a kind of polynomial by setting x to some base, say for decimal numbers:

A =

∑^ n−^1

i= 0

ai · 10 i^.

Raphaël Clifford

Polynomials (2)

EVALUATE-HORNER(A, n, x)

begin

t ← 0

for i = n − 1 downto 0 step − 1 do

t ← (t · x) + ai

return t

end

Example

Consider A(x) = 2 + 3 x + 1 .x^2 We can evaluate this as

A(x) = 2 + x( 3 + 1 .x)

Raphaël Clifford

Coefficient Based Polynomial Arithmetic

I (^) Once we have our polynomial representations, we might want to do some arithmetic with them. I (^) For a coefficient representation, the addition C = A + B constructs C as the vector:

(a 0 + b 0 , a 1 + b 1 , a 2 + b 2 ,... , an− 1 + bn− 1 ).

I (^) Strictly speaking, A and B should have the same length but in practice we can just pad with zero coefficients to make this so.

Raphaël Clifford

Point Value Polynomial Arithmetic

I (^) For a point-value representation, the addition C = A + B constructs C as:

{(x 0 , y 0 + z 0 ), (x 1 , y 1 + z 1 ), (x 2 , y 2 + z 2 ),... , (xn− 1 , yn− 1 + zn− 1 )}

where xi is a point, yi = A(xi ) and zi = B(xi ). I (^) Note that the two point-value representations must use the same evaluation points. I (^) Both these operations are O(n) in terms of the time they take.

Raphaël Clifford

Polynomial Multiplication

I (^) Computing a polynomial multiplication, sometimes called convolution, is a little bit harder than addition. I (^) For a coefficient representation, the product C = A × B can be calculated with school-book long multiplication:

C(x) =

(^2) ∑n− 2

i= 0

ci xi

where

ci =

∑^ i

j= 0

aj · bi−j

I (^) To do now: multiply 7x^2 − 10 x + 9 and 2x^2 + 4 x − 5 I (^) For a point-value representation, C = A × B is a bit easier:

{(x 0 , y 0 · z 0 ), (x 1 , y 1 · z 1 ), (x 2 , y 2 · z 2 ),... , (xn− 1 , yn− 1 · zn− 1 )}

where xi is a point, yi = A(xi ) and zi = B(xi ). I (^) The first method is O(n^2 ), the second method is O(n)!

Raphaël Clifford

Polynomial Multiplication

I (^) Actually, recall that we can do a bit better than the O(n^2 ) case using the divide and conquer method due to Karatsuba. I (^) We find this gives an algorithm with time complexity:

O(nlog^2 3 ) = O(n^1.^59 )

which is better than our previous method which took O(n^2 ) operations.

Raphaël Clifford

Polynomial Multiplication

I (^) The problem is that even this is too slow ... we know that using a point-value representation is O(n)! I (^) So a better technique would be to traverse around this diagram:

Coefficient Representation Coefficient Representation

Point-Value Representation

Point-Value- Representation

Evaluate Interpolate

Point-Value Multiply

Coefficient Multiply

I (^) Note that the opposite of evaluation is called interpolation. I (^) So we evaluate to a point-value representation, multiply and then interpolate back again. I (^) The question is, are we quicker than the normal multiply?

Raphaël Clifford

The Main Idea -Part 2

The main steps for fast multiplication of two polynomials A and B each of degree n are:

  1. Double degree-bound: Create coefficient representations of A(x) and B(x) as degree-bound 2n polynomials by adding n high-order zero coefficients to each
  2. Evaluate: Compute point-value representations of A(x) and B(x) of length 2n through two applications of the FFT of order 2n.
  3. Pointwise multiply: Compute a point-value representation of C(x) = A(x)B(x) by multiplying the values pointwise
  4. Interpolate: Create a coefficient representation of C(x) through a single application of the inverse FFT. The first and third steps are easy to perform in O(n) time. The claim is that if we evaluate at the complex roots of unity then we can perform steps 2 and 4 in O(n log n) time.

Raphaël Clifford

Evaluation at Roots of Unity

I (^) First let’s address evaluation: I (^) We need to evaluate a polynomial of degree n at n different points (ignore the degree-bound doubling for the moment). I (^) Appears complexity of our method will be O(n^2 ). I (^) Is there a faster way of doing this than just using Horner’s Rule? I (^) Yes there is, we select the points we evaluate at to be special. I (^) These special points are chosen to be the N-th Complex Roots of Unity: I (^) That is, the values ωN = e^2 πij/N^ for j = 0 , 1 ,... , N − 1. I (^) Say we are evaluating at N points so we take the N-th complex roots of unity ωN. I (^) That is, we evaluate the polynomial at the points:

ω^0 N , ω^1 N , ω^2 N ,... , ω NN −^1.

Raphaël Clifford

Discrete Fourier Transform

We want to evaluate a polynomial A at the n roots of unity. I (^) Therefore we evaluate

A(x) =

n∑− 1

j= 0

aj ωnkj

for every k = 0 , 1 ,... , n − 1. I (^) Let’s define the vector of results of these evaluations as

yk = A(ωkn )

I (^) This vector y = (y 0 ,... , yn− 1 ) is the Discrete Fourier Transform (DFT) of the coefficient vector a = (a 0 , a 1 ,... , an− 1 ).

Example

The discrete Fourier transform of 0 + 0 x + x^2 − x^3 is 0, − 1 + i, 2 , − 1 − i

Raphaël Clifford

A Couple of Lemmas

Lemma

The Cancellation Lemma: ωdkdN = ωkN.

Lemma

The 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.

Proof.

By the cancellation lemma, we have (ωnk )^2 = ωkn/ 2 , for any nonnegative integer k.

Raphaël Clifford

Fast Fourier Transform

I (^) The basic idea of the Fast Fourier Transform (FFT), a fast version of the DFT, is define two new polynomials:

A^0 = a 0 + a 2 x + · · · + aN− 2 xN/^2 −^1 A^1 = a 1 + a 3 x + · · · + aN− 1 xN/^2 −^1

and use these to divide and conquer the problem. I (^) From the above, we have:

A(x) = A^0 + xA^1 .

I (^) So the problem of evaluating A at ω N^0 , ω N^1 ,... , ωN N −^1 is reduced to evaluating A[^0 ]^ and A[^1 ]^ at the points:

(ω^0 N )^2 , (ω^1 N )^2 , (ω^2 N )^2 ,... , (ωN N −^1 )^2

then combining the results. I (^) The Halving Lemma tells us there are only N/2 complex N/2-th roots of unity, each one must occur twice!

Raphaël Clifford

Example of Divide Step

Example

Consider A[x] = 0 + 0 x + 1 .x^2 − x^3 again.

A[^0 ][x] = a 0 + a 2 x = 0 + 1 .x A[^1 ][x] = a 1 + a 3 x = 0 − 1 .x We can check by seeing that

A[x] = A[^0 ][x^2 ] + x.A[^1 ][x^2 ] = 0 + 1 .x^2 + x( 0 − 1 .x^2 ) = x^2 − x^3

as required.

Raphaël Clifford