Fast Fourier Transform Integer multiplication Multiplying two n-bit - - PowerPoint PPT Presentation

fast fourier transform integer multiplication
SMART_READER_LITE
LIVE PREVIEW

Fast Fourier Transform Integer multiplication Multiplying two n-bit - - PowerPoint PPT Presentation

15-252: More Great Ideas in Theoretical Computer Science Spring 2017 Fast Fourier Transform Integer multiplication Multiplying two n-bit integers A and B: Grade School Method: O(n 2 ) time. Karatsubas Algorithm: O(n log 2 3 ) = O(n 1.58


slide-1
SLIDE 1

15-252: More Great Ideas in Theoretical Computer Science

Fast Fourier Transform

Spring 2017

slide-2
SLIDE 2

Integer multiplication

Multiplying two n-bit integers A and B: “Grade School” Method: O(n2) time. Karatsuba’s Algorithm: O(nlog23) = O(n1.58…)

(what Python uses)

Generalizations thereof: O(n1+ϵ) Fürer 2007: circuits of size n (log n) 2O(log*n) Schönhage−Strassen late ’60s : O(n) time

(in RAM model)

(!!) via Fast Fourier Transform

slide-3
SLIDE 3

Volker Strassen & Arnold Schönhage, late ’60s

slide-4
SLIDE 4

Ideas discussed on the homework…

  • 1. Multiplying integers reduces to multiplying

polynomials with integer coefficients.

  • 2. Multiplying polynomials is easy in the

“Values Representation”.

  • 3. With a magic set of interpolation points,

going between “Coefficients Representation” and “Values Representation” is super-fast.

slide-5
SLIDE 5

Goal

Multiplying two polynomials with degree < N (and coefficients fitting in a “word”) in O(N log N) time. Implies O(n) time multiplication of n-bit integers.

slide-6
SLIDE 6

Polynomial multiplication

Let P(x) and Q(x) be polynomials of degree < N. Assumed in “Coefficients Representation”, P (x) = a0 + a1 x + a2 x2 + ··· + aN−1 xN−1 Q (x) = b0 + b1 x + b2 x2 + ··· + bN−1 xN−1 (where aj’s, bk’s are ints fitting in a word). Let R(x) = P(x)·Q(x), of degree < 2N. Task is to get R(x) in Coefficients Representation.

slide-7
SLIDE 7

Polynomial multiplication

Let P(x) and Q(x) be polynomials of degree < N. Assumed in “Coefficients Representation”, Let R(x) = P(x)·Q(x), of degree < 2N. Task is to get R(x) in Coefficients Representation. If only everything were in “Values Representation” instead…

slide-8
SLIDE 8

Polynomial multiplication

Let P(x) and Q(x) be polynomials of degree < N. Assumed in “Coefficients Representation”, Let R(x) = P(x)·Q(x), of degree < 2N. Task is to get R(x) in Coefficients Representation. If only we knew P (1), P (2), …, P (2N), Q(1), Q(2), ..., Q(2N), R(1), R(2), ..., R(2N) uniquely determines R(x) by interpolation

slide-9
SLIDE 9

N coefficients of P(x) If we could somehow pass between Coefficients Representation & Values Representation in O(N log N) time, we’d be done. N values of P(x), say x = 1, 2, …, N evaluation interpolation Unfortunately, these seem to take O(N2) time.

slide-10
SLIDE 10

N coefficients of P(x) If we could somehow pass between Coefficients Representation & Values Representation in O(N log N) time, we’d be done. N values of P(x), say x = 1, 2, …, N evaluation interpolation Unfortunately, these seem to take O(N2) time.

slide-11
SLIDE 11

N coefficients of P(x) If we could somehow pass between Coefficients Representation & Values Representation in O(N log N) time, we’d be done. N values of P(x), evaluation interpolation

  • n ‘roots of unity’

Voila! O(N log N) ops with “FFT”.

slide-12
SLIDE 12

Discrete Fourier Transform (& Inverse)

Let N be a power of 2. is the set of N “complex roots of unity” that I’ll describe shortly. Let P(x) be a polynomial of degree N−1. P’s values on SN P’s coefficients P’s coefficients P’s values on SN IDFTN DFTN

interpolation evaluation

slide-13
SLIDE 13

Fast Fourier Transform

P’s values on SN P’s coefficients P’s coefficients P’s values on SN IDFTN DFTN

interpolation evaluation

A recursive algorithm for DFTN and IDFTN that uses only O(N log N) arithmetic operations.

slide-14
SLIDE 14

Fast Fourier Transform

  • G. Strang, ’94: “The most important numerical

algorithm of our lifetime.”

“Brigham [1974] says that [Richard] Garwin asked [John] Tukey to give him a rapid way to compute the Fourier transform during a meeting of the President's [Kennedy’s] Scientific Advisory Committee. Then Garwin went to the computing center at IBM Research in Yorktown Heights where [James] Cooley programmed the Fourier transform, because he had nothing better to do. After receiving many requests for the program, Cooley and Tukey published their paper in 1965.” −A. Terras, ’99

slide-15
SLIDE 15

Fast Fourier Transform

1965

  • G. Strang, ’94: “The most important numerical

algorithm of our lifetime.”

slide-16
SLIDE 16

Fast Fourier Transform

  • G. Strang, ’94: “The most important numerical

algorithm of our lifetime.”

“Heideman et al. [1984] note that [Carl Friedrich] Gauss discovered the fast Fourier transform in 1805 [two years before Fourier invented Fourier series!] while computing the eccentricity of the orbit of the asteroid Juno.” −A. Terras, ’99

slide-17
SLIDE 17

Fast Fourier Transform

  • G. Strang, ’94: “The most important numerical

algorithm of our lifetime.” OG, 1805

slide-18
SLIDE 18

Multiplying polynomials with the FFT

Let P(x), Q(x) be polynomials of degree < N. Want R(x) = P(x)·Q(x), which has degree < 2N.

  • 1. Use DFT2N to get P(w), Q(w) for all w∈S2N d
  • 2. Multiply pairs, getting R(w) for all w∈S2N d
  • 3. Use IDFT2N to get R’s coefficients

P’s values on SN P’s coefficients P’s coefficients P’s values on SN IDFTN DFTN

interpolation evaluation

slide-19
SLIDE 19

Multiplying polynomials with the FFT

Let P(x), Q(x) be polynomials of degree < N. Want R(x) = P(x)·Q(x), which has degree < 2N.

  • 1. O(N log N) arithmetic ops
  • 2. O(N) arithmetic ops
  • 3. O(N log N) arithmetic ops

O(N log N) arithmetic ops Time:

  • 1. Use DFT2N to get P(w), Q(w) for all w∈S2N d
  • 2. Multiply pairs, getting R(w) for all w∈S2N d
  • 3. Use IDFT2N to get R’s coefficients
slide-20
SLIDE 20

Multiplying polynomials with the FFT

Can multiply two degree-N polynomials using O(N log N) arithmetic operations. If the coefficients are ints fitting in a word, can multiply polynomials in O(N log N) time.

* Requires proving that you can compute the Nth roots of unity to O(log N) bits of precision in O(N log N) time, and that this precision is sufficient. This is fairly easy to prove, but also boring to prove.

slide-21
SLIDE 21

Multiplying polynomials with the FFT

Can multiply two degree-N polynomials using O(N log N) arithmetic operations. If the coefficients are ints fitting in a word, can multiply polynomials in O(N log N) time. Implies O(n)-time multiplication of n-bit integers (in the Word RAM model).

slide-22
SLIDE 22

The Discrete Fourier Transform & The Fast Fourier Transform

slide-23
SLIDE 23

The complex numbers ℂ

z = .6−.8i

.6 −.8

|z| = magnitude of z = 1, in this case

slide-24
SLIDE 24

The complex numbers ℂ

complex #’s

  • f magnitude 1

θ≈53°

defined by angle θ from x-axis Key Rule: Multiplication by z = rotation by θ. z = .6−.8i

slide-25
SLIDE 25

The complex numbers ℂ

Key Rule: Multiplication by z = rotation by θ. z2 = −.28−.96i

θ≈53°

z = .6−.8i complex #’s

  • f magnitude 1

defined by angle θ from x-axis

slide-26
SLIDE 26

Unity

1 +0i

(angle θ = 0)

slide-27
SLIDE 27

Square Roots of Unity

1 −1

(angle θ = 180°)

slide-28
SLIDE 28

Cube Roots of Unity

1

  • f a circle

= rotation by

slide-29
SLIDE 29

Cube Roots of Unity

1

  • f a circle

= rotation by

  • f a circle

= rotation by =

slide-30
SLIDE 30

4th Roots of Unity

1

  • f a circle

by

= −i

= rotation

slide-31
SLIDE 31

8th Roots of Unity

slide-32
SLIDE 32

16th Roots of Unity

slide-33
SLIDE 33

Discrete Fourier Transform (& Inverse)

Let N be a power of 2. Let . Let P(x) be a polynomial of degree N−1. P’s values on SN P’s coefficients P’s coefficients P’s values on SN IDFTN DFTN

interpolation evaluation

slide-34
SLIDE 34

Discrete Fourier Transform (& Inverse)

Let N be 8, and let . Let P(x) be a polynomial of degree 7. Let . P’s values on S8 P’s coefficients P’s coefficients P’s values on S8 IDFT8 DFT8

interpolation evaluation

slide-35
SLIDE 35

Evaluation at

Say P(x) = a0+a1x+a2x2+a3x3+a4x4+a5x5+a6x6+a7x7.

Since ω8 = 1, we can reduce all exponents mod 8.

slide-36
SLIDE 36

Evaluation at

Say P(x) = a0+a1x+a2x2+a3x3+a4x4+a5x5+a6x6+a7x7.

DFT8[j,k] = DFT8

(0 ≤ j, k < 7)

ωjk mod 8

slide-37
SLIDE 37

Evaluation at

Say P(x) = a0+a1x+a2x2+a3x3+a4x4+a5x5+a6x6+a7x7.

  • 1

2 3 4 5 6 7 1 1 2 3 4 5 6 7 2 2 4 6 2 4 6 3 3 6 1 4 7 2 5 4 4 4 4 4 5 5 2 7 4 1 6 3 6 6 4 2 6 4 2 7 7 6 5 4 3 2 1

Multiplication modulo 8 table DFT8[j,k] =

(0 ≤ j, k < 7)

ωjk mod 8

slide-38
SLIDE 38

Evaluation at

Say P(x) = a0+a1x+a2x2+a3x3+a4x4+a5x5+a6x6+a7x7.

DFT8[j,k] =

(0 ≤ j, k < 7)

ωjk mod 8

slide-39
SLIDE 39

Evaluation at

Say P(x) = a0+a1x+a2x2+a3x3+a4x4+a5x5+a6x6+a7x7.

DFT8 ·

slide-40
SLIDE 40

Interpolation?

Say P(x) = a0+a1x+a2x2+a3x3+a4x4+a5x5+a6x6+a7x7. Given P(1), P(ω), …, P(ω7), how to get a0, a1, …, a7?

DFT8 ·

slide-41
SLIDE 41

Interpolation?

Say P(x) = a0+a1x+a2x2+a3x3+a4x4+a5x5+a6x6+a7x7. Given P(1), P(ω), …, P(ω7), how to get a0, a1, …, a7?

DFT8

−1 ·

also known as

IDFT8

slide-42
SLIDE 42

Interpolation?

Say P(x) = a0+a1x+a2x2+a3x3+a4x4+a5x5+a6x6+a7x7. Given P(1), P(ω), …, P(ω7), how to get a0, a1, …, a7?

DFT8

−1 ·

also known as

IDFT8

P’s values on SN P’s coefficients P’s coefficients P’s values on SN IDFTN DFTN

interpolation evaluation

slide-43
SLIDE 43

Interpolation?

Say P(x) = a0+a1x+a2x2+a3x3+a4x4+a5x5+a6x6+a7x7. Given P(1), P(ω), …, P(ω7), how to get a0, a1, …, a7?

DFT8

−1 ·

also known as

IDFT8

slide-44
SLIDE 44

DFT versus IDFT

Question: We know what matrix DFT8 is. What is its inverse matrix, IDFT8? Answer: It’s extremely similar to DFT8.

slide-45
SLIDE 45

DFT8 IDFT8

DFTN[j,k] =

(0 ≤ j, k < N, ω = ωN is Nth root of unity)

ωjk mod N IDFTN[j,k] = ω−jk mod N

slide-46
SLIDE 46

DFT8 IDFT8

Proof illustration.

We’ll show the product =

slide-47
SLIDE 47

DFT8 IDFT8

Proof by picture.

We’ll show the product =

slide-48
SLIDE 48

DFT8 IDFT8

Proof by picture.

We’ll show the product =

slide-49
SLIDE 49

DFT8 IDFT8

Proof by picture.

We’ll show the product =

slide-50
SLIDE 50

DFT8 IDFT8

Proof by picture.

We’ll show the product =

slide-51
SLIDE 51

DFT8 IDFT8

Proof by picture.

We’ll show the product =

average is 0

slide-52
SLIDE 52

DFT8 IDFT8

Proof by picture.

We’ll show the product =

average is 0

slide-53
SLIDE 53

DFT8 IDFT8

Proof by picture.

We’ll show the product =

average is 0

slide-54
SLIDE 54

DFT8 IDFT8

Proof by picture.

We’ll show the product =

average is 0

slide-55
SLIDE 55

DFT8 IDFT8

Proof by picture.

We’ll show the product =

average is 0

slide-56
SLIDE 56

DFT8 IDFT8

Proof by picture.

We’ll show the product =

average is 0

slide-57
SLIDE 57

DFT8 IDFT8

Proof by picture.

We’ll show the product =

average is 0

slide-58
SLIDE 58

DFT8 IDFT8

Proof by picture.

We’ll show the product =

average is 0

slide-59
SLIDE 59

DFT8 IDFT8

Proof by picture.

We’ll show the product =

Well, looks pretty true. Proof is an exercise. J

slide-60
SLIDE 60

DFTN ·

Last piece of the puzzle: FFT Computing this in O(N log N) ops

slide-61
SLIDE 61

Terminology: sequence transformed sequence

DFTN ·

slide-62
SLIDE 62

DFTN ·

Claim: DFTN reduces to 2 applications of DFTN/2, plus O(N) additional operations. ⇒ T(N) = 2T(N/2) + O(N) ⇒ T(N) = O(N log N)

slide-63
SLIDE 63

Claim: DFT8 reduces to 2 applications of DFT4, plus “O(8)” additional operations.

slide-64
SLIDE 64

Claim: DFT8 reduces to 2 applications of DFT4, plus “O(8)” additional operations.

slide-65
SLIDE 65

Claim: DFT8 reduces to 2 applications of DFT4, plus “O(8)” additional operations.

slide-66
SLIDE 66

Claim: DFT8 reduces to 2 applications of DFT4, plus “O(8)” additional operations. ditto

slide-67
SLIDE 67

Claim: DFT8 reduces to 2 applications of DFT4, plus “O(8)” additional operations. ditto

slide-68
SLIDE 68

Claim: DFT8 reduces to 2 applications of DFT4, plus “O(8)” additional operations. ditto

slide-69
SLIDE 69

Claim: DFT8 reduces to 2 applications of DFT4, plus “O(8)” additional operations. ditto

slide-70
SLIDE 70

Claim: DFT8 reduces to 2 applications of DFT4, plus “O(8)” additional operations. ditto

slide-71
SLIDE 71

Claim: DFT8 reduces to 2 applications of DFT4, plus “O(8)” additional operations. Computable with 1 application of DFT4 to (a0,a2,a4,a6), and some copying. Now to get this, apply the above to (a1,a3,a5,a7), and then multiply the jth row by ωj, for 0 ≤ j < 7. Total: 2 applications of DFT4, plus “O(8)” more operations.

slide-72
SLIDE 72

Summary

  • Multiplying two n-bit integers is doable

in O(n) time in the Word RAM model

  • It reduces to multiplying two polynomials
  • f degree < N in O(N log N) time.
  • DFTN reduces Coefficients Representation to

Values Representation over roots of unity.

  • FFTN computes DFTN (and inverse)

in O(N log N) time.

  • DFTN has myriad uses in CS & Engineering.