Presentation is loading. Please wait.

Presentation is loading. Please wait.

Applied Symbolic Computation1 Applied Symbolic Computation (CS 300) Karatsuba’s Algorithm for Integer Multiplication Jeremy R. Johnson.

Similar presentations


Presentation on theme: "Applied Symbolic Computation1 Applied Symbolic Computation (CS 300) Karatsuba’s Algorithm for Integer Multiplication Jeremy R. Johnson."— Presentation transcript:

1 Applied Symbolic Computation1 Applied Symbolic Computation (CS 300) Karatsuba’s Algorithm for Integer Multiplication Jeremy R. Johnson

2 Applied Symbolic Computation2 Introduction Objective: To derive a family of asymptotically fast integer multiplication algorithms using polynomial interpolation –Karatsuba’s Algorithm –Polynomial algebra –Interpolation –Vandermonde Matrices –Toom-Cook algorithm –Polynomial multiplication using interpolation –Faster algorithms for integer multiplication References: Lipson, Cormen et al.

3 Applied Symbolic Computation3 Karatsuba’s Algorithm Using the classical pen and paper algorithm two n digit integers can be multiplied in O(n 2 ) operations. Karatsuba came up with a faster algorithm. Let A and B be two integers with –A = A 1 10 k + A 0, A 0 < 10 k –B = B 1 10 k + B 0, B 0 < 10 k –C = A*B = (A 1 10 k + A 0 )(B 1 10 k + B 0 ) = A 1 B 1 10 2k + (A 1 B 0 + A 0 B 1 )10 k + A 0 B 0 Instead this can be computed with 3 multiplications T 0 = A 0 B 0 T 1 = (A 1 + A 0 )(B 1 + B 0 ) T 2 = A 1 B 1 C = T 2 10 2k + (T 1 - T 0 - T 2 )10 k + T 0

4 Applied Symbolic Computation4 Complexity of Karatsuba’s Algorithm Let T(n) be the time to compute the product of two n-digit numbers using Karatsuba’s algorithm. Assume n = 2 k. T(n) =  (n lg(3) ), lg(3)  1.58 T(n)  3T(n/2) + cn  3(3T(n/4) + c(n/2)) + cn = 3 2 T(n/2 2 ) + cn(3/2 + 1)  3 2 (3T(n/2 3 ) + c(n/4)) + cn(3/2 + 1) = 3 3 T(n/2 3 ) + cn(3 2 /2 2 + 3/2 + 1) …  3 i T(n/2 i ) + cn(3 i-1 /2 i-1 + … + 3/2 + 1)...  c3 k + cn[((3/2) k - 1)/(3/2 -1)] --- Assuming T(1)  c  c3 k + 2c(3 k - 2 k )  3c3 lg(n) = 3cn lg(3)

5 Applied Symbolic Computation5 Divide & Conquer Recurrence Assume T(n) = aT(n/b) +  (n) T(n) =  (n) [a < b] T(n) =  (nlog(n)) [a = b] T(n) =  (n log b (a) ) [a > b]

6 Applied Symbolic Computation6 Polynomial Algebra Let F[x] denote the set of polynomials in the variable x whose coefficients are in the field F. F[x] becomes an algebra where +, * are defined by polynomial addition and multiplication.

7 Applied Symbolic Computation7 Interpolation A polynomial of degree n is uniquely determined by its value at (n+1) distinct points. Theorem: Let A(x) and B(x) be polynomials of degree m. If A(  i ) = B(  i ) for i = 0,…,m, then A(x) = B(x). Proof. Recall that a polynomial of degree m has m roots. A(x) = Q(x)(x-  ) + A(  ), if A(  ) = 0, A(x) = Q(x)(x-  ), and deg(Q) = m-1 Consider the polynomial C(x) = A(x) - B(x). Since C(  i ) = A(  i ) - B(  i ) = 0, for m+1 points, C(x) = 0, and A(x) must equal B(x).

8 Applied Symbolic Computation8 Lagrange Interpolation Formula Find a polynomial of degree m given its value at (m+1) distinct points. Assume A(  i ) = y i Observe that

9 Applied Symbolic Computation9 Matrix Version of Polynomial Evaluation Let A(x) = a 3 x 3 + a 2 x 2 + a 1 x + a 0 Evaluation at the points , , ,  is obtained from the following matrix-vector product

10 Applied Symbolic Computation10 Matrix Interpretation of Interpolation Let A(x) = a n x n + … + a 1 x +a 0 be a polynomial of degree n. The problem of determining the (n+1) coefficients a n,…,a 1,a 0 from the (n+1) values A(  0 ),…,A(  n ) is equivalent to solving the linear system

11 Applied Symbolic Computation11 Vandermonde Matrix V(  0,…,  n ) is non-singular when  0,…,  n are distinct.

12 Applied Symbolic Computation12 Polynomial Multiplication using Interpolation Compute C(x) = A(x)B(x), where degree(A(x)) = m, and degree(B(x)) = n. Degree(C(x)) = m+n, and C(x) is uniquely determined by its value at m+n+1 distinct points. [Evaluation] Compute A(  i ) and B(  i ) for distinct  i, i=0,…,m+n. [Pointwise Product] Compute C(  i ) = A(  i ) * B(  i ) for i=0,…,m+n. [Interpolation] Compute the coefficients of C(x) = c n x m+n + … + c 1 x +c 0 from the points C(  i ) = A(  i ) * B(  i ) for i=0,…,m+n.

13 Applied Symbolic Computation13 Interpolation and Karatsuba’s Algorithm Let A(x) = A 1 x + A 0, B(x) = B 1 x + B 0, C(x) = A(x)B(x) = C 2 x 2 + C 1 x + C 0 Then A(10 k ) = A, B(10 k ) = B, and C = C(10 k ) = A(10 k )B(10 k ) = AB Use interpolation based algorithm: –Evaluate A(  ), A(  ), A(  ) and B(  ), B(  ), B(  ) for  = 0,  = 1, and  = . –Compute C(  ) = A(  )B(  ), C(  ) = A(  ) B(  ), C(  ) = A(  )B(  ) –Interpolate the coefficients C 2, C 1, and C 0 –Compute C = C 2 10 2k + C 1 10 k + C 0

14 Applied Symbolic Computation14 Matrix Equation for Karatsuba’s Algorithm Modified Vandermonde Matrix Interpolation

15 Applied Symbolic Computation15 Integer Multiplication Splitting the Inputs into 3 Parts Instead of breaking up the inputs into 2 equal parts as is done for Karatsuba’s algorithm, we can split the inputs into three equal parts. This algorithm is based on an interpolation based polynomial product of two quadratic polynomials. Let A(x) = A 2 x 2 + A 1 x + A 0, B(x) = B 2 x 2 + B 1 x + B, C(x) = A(x)B(x) = C 4 x 4 + C 3 x 3 + C 2 x 2 + C 1 x + C 0 Thus there are 5 products. The divide and conquer part still takes time = O(n). Therefore the total computing time T(n) = 5T(n/3) + O(n) =  (n log 3 (5) ), log 3 (5)  1.46

16 Applied Symbolic Computation16 Asymptotically Fast Integer Multiplication We can obtain a sequence of asymptotically faster multiplication algorithms by splitting the inputs into more and more pieces. If we split A and B into k equal parts, then the corresponding multiplication algorithm is obtained from an interpolation based polynomial multiplication algorithm of two degree (k-1) polynomials. Since the product polynomial is of degree 2(k-1), we need to evaluate at 2k-1 points. Thus there are (2k-1) products. The divide and conquer part still takes time = O(n). Therefore the total computing time T(n) = (2k-1)T(n/k) + O(n) =  (n log k (2k-1) ).

17 Applied Symbolic Computation17 Asymptotically Fast Integer Multiplication Using the previous construction we can find an algorithm to multiply two n digit integers in time  (n 1+  ) for any positive . –log k (2k-1) = log k (k(2-1/k)) = 1 + log k (2-1/k) –log k (2-1/k)  log k (2) = ln(2)/ln(k)  0. Can we do better? The answer is yes. There is a faster algorithm, with computing time  (nlog(n)loglog(n)), based on the fast Fourier transform (FFT). This algorithm is also based on interpolation and the polynomial version of the CRT.


Download ppt "Applied Symbolic Computation1 Applied Symbolic Computation (CS 300) Karatsuba’s Algorithm for Integer Multiplication Jeremy R. Johnson."

Similar presentations


Ads by Google