Complex numbers polynomial multiplication

14

Click here to load reader

Transcript of Complex numbers polynomial multiplication

Page 1: Complex numbers polynomial multiplication

Multiplying Polynomials Fast

Why do we need Complex Numbers?

Page 2: Complex numbers polynomial multiplication

The Problem

• (an xn + an-1 xn-1+…..+ a0 x0) * (bn xn + bn-1 xn-1+…..+ b0 x0)

• O(n^2) time– (Σi=0..k ai * bk-i) is coefficient of xk

• Can one do better?

Page 3: Complex numbers polynomial multiplication

Applications

• Where all does a pattern string P appear in a text string T?

– P 0’s, 1’s and don’t cares.

– T 0’s and 1’s

• Easy in O(|P|*|T|) time

– Can one do better?

Page 4: Complex numbers polynomial multiplication

Conversion to Polynomial Multiplication

• Treat P and T as polynomials

– T=0101 1 x0 + 0 x1 + 1 x2 + 0 x3

– P=01D1 0 x0 + 1 x1 + 0 x2 + 1 x3

• Multiply Prev and T

– (Σi=0..k previ * tk-i) is coefficient of xk

– (Σi=0..k p|P|-i * tk-i) is coefficient of xk

• >=1 if and only if a 1 in P aligns with a 0 in T when P is placed with end at tk– 2 polynomial multiplications suffice to find all matches of P in T

1 0 1 0 1 1 0 0 1

1 0 0 0 1 0 0

tktk-1tk-2tk-i

p-0p-1p-i

Page 5: Complex numbers polynomial multiplication

Other Applications

• Image Processing

Slide this mask all over the bigger one

At this location

Multiply each bit in the mask with

the corresponding bit

in the image, sum these up

Page 6: Complex numbers polynomial multiplication

Polynomial MultiplicationAn Equivalent Form

• Evaluate each polynomial at 2n+1 distinct x’s

– A(x) = (an xn + an-1 xn-1+…..+ a0 x0) -> A(v0)…..A(v2n)

– B(x) = (bn xn + bn-1 xn-1+…..+ b0 x0) -> B(v0)…..B(v2n)

• A(x) * B(x) -> A(v0)*B(v0)………..A(v2n)*B(v2n)

– Convolution in one domain=Simple multiplication in another

– O(n) time!!!

Page 7: Complex numbers polynomial multiplication

Multi-Point Polynomial Evaluation

• Evaluate A(x) at v0 ……vn

– O(n) time per vi using Horner’s rule

• Problems

– O(n2) time

– Large numbers with n log vi bits

Page 8: Complex numbers polynomial multiplication

Multi-Point Polynomial EvaluationSpeed Up

• A(x) mod (x-v)

– A(v)

– O(n) time using high school polynomial division

• A’(x) = A(x) mod (x-v0) (x-v1) [how fast?]

– A’(x) mod (x-v0) = A(v0) [O(1)]

– A’(x) mod (x-v1) = A(v1) [O(1)]

– 2 expensive polynomial divisions could potentially be replaced by 1

Page 9: Complex numbers polynomial multiplication

Fast Multi-Point Polynomial Evaluation

T0(x)=A(x) mod (x-v0) (x-v1).. (x-vn)

T2 (x)=T0 (x) mod (x-v(n+1)/2) (x-vn) T1(x)=T0 (x) mod (x-v0)..(x-v(n+1)/2-1)

• If T(x) mod (x-vi).. (x-vj) can be done in O(deg(T)) time, then what is the total time taken?

– O(n log n)!!

Page 10: Complex numbers polynomial multiplication

Computing T(x) mod (x-v1).. (x-vk)

• High school algorithm

– Time taken: O( deg(T) * k )

– How do we make this faster?

• Stroke of genius

• Can we choose vi’s so (x-v1).. (x-vk) = xk-v?

• Then we get O(deg(T) ) time

Page 11: Complex numbers polynomial multiplication

Choosing vi’s

• When is (x-v1)(x-v2) = x2+v1v2 ?

– v2+v1=0

• When is (x-a)(x+a)(x-b)(x+b) = x4+ a2b2?

– b2-a2=0 => b2=-a2

• => b= sqrt(-1) a

• the 4 numbers are 1 –i -1 i

• alternatively (–i)0 (–i)1 (–i)2 (–i)3

Page 12: Complex numbers polynomial multiplication

Choosing vi’s

• Choose vi to be roots of xn -1

= Cos(2Πk/n) + i Sin(2Πk/n) = e i 2Πk/n

– Powering just goes around the unit circle

• Computing with log n bits suffices

Page 13: Complex numbers polynomial multiplication

Exercise

• Choose vi to be roots of xn -1

= Cos(2Πk/n) + i Sin(2Πk/n) = e i 2Πk/n

– Organize these roots so we get polynomials with just 2 terms in every node of the tree on slide 9 • Assume n+1 is a power of 2

Page 14: Complex numbers polynomial multiplication

Conclusion

• Also called : Fast Fourier Transform

• Complex Numbers are interesting!

• Reality can be explained elegantly only with complex numbers!!