Algorithms ad Data Structures 204 Exercises ad Solutios Week 3 Toom-Cook (cotiued) Durig the last lecture, two polyomials A(x) a 0 + a x ad B(x) b 0 + b x both of degree were multiplied, first by evaluatig A ad B at three distict poits we picked 0, ad as cocrete values the these results were multiplied pairwise, ad fially the value represetatio of the resultig polyomial C(x) c 0 + c x + c 2 x 2 (of degree 2) was traslated back usig the iverse of the correspodig evaluatio matrix.. Repeat this procedure, but ow with 0,, as evaluatio poits. Evaluatig A at meas that we just take A s most leadig coefficiet a. Therefore, evaluatig C at (beig c 2 ) amouts to computig the product a b. 2. Explai the relatio of this procedure with Karatsuba s multiplicatio algorithm. 3. Istead of usig polyomials of degree we repeat the whole procedure with polyomials of degree 2. The the multiplicatio result will have degree 4, so we eed 5 evaluatio poits for iterpolatio (the traslatio back from values to coefficiets). Evaluate both A(x) a 0 + a x + a 2 x 2 ad B(x) b 0 + b x + b 2 x 2 at 0,,, 2,. Express the evaluatio of C(x) c 0 +c x+c 2 x 2 +c 3 x 3 +c 4 x 4 i these poits by meas of a 5 5 matrix, ad compute the iverse of this matrix. Agai, explai the relatio with the Toom-Cook3 algorithm (see exercise 4, week 2). Solutio. The evaluatio matrix correspodig to these evaluatio poits is M 00 0 0 2 0 2 0 0 0 0 0 0 ad its iverse is give by Thus, c 0 c c 2 M 0 0. 0 0 0 0 C(0) C() 0 0 C( ) 0 0 A(0)B(0) A()B() 0 0 A( )B( ) 0 0 a 0 b 0 (a 0 + a )(b 0 + b ) 0 0 a b a 0 b 0 (a 0 + a )(b 0 + b ) a 0 b 0 a b. a b You were allowed to use a computer to obtai iverse matrices.
2. The resultig formulas for the coefficiets of C are exactly the oes used by Karatsuba s multiplicatio algorithm. 3. The evaluatio matrix is ow 0 0 0 0 2 0 3 0 4 0 0 0 0 0 2 3 4 M ( ) 0 ( ) ( ) 2 ( ) 3 ( ) 4 2 0 2 2 2 2 3 2 4 2 4 8 6, 0 0 0 0 0 0 0 0 with iverse 6 0 0 0 0 M 3 6 2 2 6 6 3 3 0 6 3 3 2. 0 0 0 0 6 Now we have c 0 6 0 0 0 0 C(0) c c 2 c 3 3 6 2 2 C() 6 6 3 3 0 6 C( ) 3 3 2 C(2) c 4 0 0 0 0 6 C( ) 6 0 0 0 0 A(0)B(0) 3 6 2 2 A()B() 6 6 3 3 0 6 A( )B( ) 3 3 2 A(2)B(2) 0 0 0 0 6 A( )B( ) 6 0 0 0 0 a 0 b 0 3 6 2 2 (a 0 + a + a 2 )(b 0 + b + b 2 ) 6 6 3 3 0 6 (a 0 a + a 2 )(b 0 b + b 2 ) 3 3 2 (a 0 + 2a + 4a 2 )(b 0 + 2b + 4b 2 ). 0 0 0 0 6 a 2 b 2 With some imagiatio, these are see to be precisely the calculatios performed by Toom- Cook3. 2 Fast Fourier Trasform 2. Evaluatio Give a algorithm that evaluates a polyomial A(x) a 0 + a x + a 2 x 2 + + a x at poit x i O() time. Solutio : fuctio EVAL(A, x) 2: v 0 3: p 4: for i from 0 to do 5: v v + a i p 6: p x p 7: retur v
2.2 Basic properties of th roots of uity. What is the sum of the th roots of uity? 2. What is their product if is odd? If is eve? Solutio. For, the oly th root of uity is ω 0 cos 2π 0 + i si 2π 0. Now suppose >. Usig the formula for a geometric series, we see that ω k ω ω cos 2π 2π + i si ω cos(2π) + i si(2π) ω cos(2π) ω ω 0 ω 0. 2. Usig the laws show durig the lecture, we fid that 2.3 Iverse FFT ( ω k cos 2π k + i si 2π k ) ( ) ( ) 2π k 2π k cos + i si ( ) ( ) 2π 2π cos k + i si k ( ) ( ) 2π( ) 2π( ) cos + i si 2 2 cos(π( )) + i si(π( )) cos(π( )) { if is eve otherwise. Show that the matrices M(, ω, ω 2,..., ω ) ad M(, ω, ω 2,..., ω ( ) ) are ideed iverses of each other. Hit: Show that their product is the idetity matrix. Solutio We ame M M(, ω, ω 2,..., ω ) ad N M(, ω, ω 2,..., ω ( ) ). That is, M ij ω ij ad N ij ω ij if we umber the idices from 0 to. We wat to show that
MN I, where I is the idetity matrix. We have (MN) ij M ik N kj ω ik ω kj (ω i j ) k. Obviously, this yields if i j, givig us the desired diagoal. If i j, we use agai the geometric series formula to obtai (ω i j ) k (ωi j ) ω i j cos 2π(i j) + i si 2π(i j) ω i j cos(2π(i j)) + i si(2π(i j)) ω i j ω i j 0 ω i j 0. 2.4 Practice with the fast Fourier Trasform. What is the FFT of (, 0, 0, 0) What is the appropriate value of ω i this case? Ad of which sequece is (, 0, 0, 0) the FFT? 2. Repeat for (, 0,, ). Solutio. We are give 4 coefficiets, so ω cos 2π 4 + i si 2π 4 i. However, i this case the value of ω is irrelevat, sice FFT((, 0, 0, 0), ω) ω ω 2 ω 3 0 ω 2 ω 4 ω 6 0 ω 3 ω 6 ω 9 0.
The same holds for the iverse FFT, as FFT 0 0 4 FFT((, 0, 0, 0), ω ) 0 ω ω 2 ω 3 0 4 ω 2 ω 4 ω 6 0 ω 3 ω 6 ω 9 0 4. 2. Now we have (fillig i i for ω as obtaied i the previous part) FFT((, 0,, ), i) i i 2 i 3 0 i 2 i 4 i 6 i 3 i 6 i 9 i i 0 i i i 3. i Note that i i i 2 i i. Therefore, FFT 0 FFT((, 0,, ), i) 4 i i 2 i 3 0 4 i 2 i 4 i 6 i 3 i 6 i 9 i i 0 4 i i i 4. 2.5 Practice with polyomial multiplicatio by FFT 3 i. Suppose that you wat to multiply the two polyomials x + ad x 2 + usig the FFT. Choose a appropriate power of two, fid the FFT of the two sequeces, multiply the results
compoetwise, ad compute the iverse FFT to get the fial result. 2. Repeat for the pair of polyomials + x + 2x 2 ad 2 + 3x. Solutio. Multiplicatio of a polyomial of degree with a polyomial of degree 2 yields a polyomial of degree 3. Obviously, 3 < 4, ad 4 is a power of 2. As i the previous exercise, ω i. The FFT of the first polyomial is FFT((,, 0, 0), i) i i 2 i 3 i 2 i 4 i 6 0 i 3 i 6 i 9 0 i i 0 i i 0 2 + i 0. i For the secod polyomial we have FFT((, 0,, 0), i) i i 2 i 3 0 i 2 i 4 i 6 i 3 i 6 i 9 0 i i 0 i i 0 2 0 2. 0 The coefficiets of the product polyomial are ow give by the iverse FFT of the poitwise product of these value represetatios. That is, 4 FFT 0 0 4 FFT((4, 0, 0, 0), i ) 0 4 i i 2 i 3 0 4 i 2 i 4 i 6 0 i 3 i 6 i 9 0. We coclude that the resultig polyomial is + x + x 2 + x 3.
2. We still have ω i. I this case, the FFT of the first polyomial is FFT((,, 2, 0), i) i i 2 i 3 i 2 i 4 i 6 2 i 3 i 6 i 9 0 i i 2 i i 0 4 i 2. i For the secod polyomial we have 2 FFT((2, 3, 0, 0), i) i i 2 i 3 3 i 2 i 4 i 6 0 i 3 i 6 i 9 0 2 i i 3 0 i i 0 5 2 + 3i. 2 3i Now we obtai the coefficiets of the product polyomial as 20 FFT 5 i 2 4 FFT((20, 5 i, 2, i 5), i ) i 5 20 i i 2 i 3 5 i 4 i 2 i 4 i 6 2 i 3 i 6 i 9 i 5 20 i i 5 i 4 2 i i i 5 2 5 7, 6 so the product polyomial is 2 + 5x + 7x 2 + 6x 3.
3 FFT i modular arithmetic This exercise illustrates how to do the Fourier Trasform (FT) i modular arithmetic, for example, modulo 7.. There is a umber ω such that all the powers ω 0, ω,..., ω 5 are distict (modulo 7). Fid this ω ad show that ω 0 + ω + + ω 5 0 (modulo 7). 2. Usig the matrix form of the FT, produce the trasform of the sequece (0,,,, 5, 2) modulo 7; that is, multiply this vector by the matrix M(ω 0, ω,..., ω 5 ). I the matrix multiplicatio, all calculatios should be performed modulo 7. 3. Write dow the matrix ecessary to perform the iverse FT. Show that multiplyig by this matrix returs the origial sequece. (Agai all arithmetic should be performed modulo 7.) 4. Now show how to multiply the polyomials x 2 + x + ad x 3 + 2x usig the FT modulo 7.