Docsity

Applied Symbolic Computation: Karatsuba's Algorithm for Integer Multiplication, Papers of Computer Science

Partial preview of the text

Download Applied Symbolic Computation: Karatsuba's Algorithm for Integer Multiplication and more Papers Computer Science in PDF only on Docsity!

Applied Symbolic Computation

Applied Symbolic Computation

(CS 300)

Karatsuba’s Algorithm for Integer Multiplication

Jeremy R. Johnson

Applied Symbolic Computation

Introduction

-^ Objective: To derive a family of asymptotically fast integermultiplication 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.

Applied Symbolic Computation

Complexity of Karatsuba’s Algorithm • Let T(n) be the time to compute the product of two n-digitnumbers using Karatsuba’s algorithm. Assume n = 2

k.

T(n) =^ Θ(n

lg(3)), lg(3)

≈^ 1.

-^ T(n)^ ≤^

3T(n/2) + cn ≤^ 3(3T(n/4) + c(n/2)) + cn = 3

22 T(n/2) + cn(3/2 + 1) (^2) ≤ 3 (3T(n/ 3 ) + c(n/4)) + cn(3/2 + 1) (^33) = 3T(n/

(^2) ) + cn(3/

…ii ≤ 3 T(n/2) + cn(

i-1i-1^ /2+ … + 3/2 + 1) ... k ≤ cn[((3/2)

- 1)/(3/2 -1)]

--- Assuming T(1)

≤^ c

k^ ≤ 2c(3- 2 klg(n))^ ≤^ 2c

lg(3)= 2cn

Applied Symbolic Computation

Divide & Conquer Recurrence Assume T(n) = aT(n/b) +

Θ(n)

-^ T(n) =^

Θ(n)^

[a < b]

-^ T(n) =^

Θ(nlog(n))

[a = b]

-^ T(n) =^

log(a)Θ(n)^ b [a > b]

Applied Symbolic Computation

Interpolation

-^ A polynomial of degree n is uniquely determined by itsvalue at (n+1) distinct points. Theorem: Let A(x) and B(x) be polynomials of degree m. IfA(α) = B(i

α) for i = 0,…,m, then A(x) = B(x).i 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(

α) = A(α) -ii

B(α) = 0, for m+1 points, C(x) = 0, and A(x) must equal B(x).i

Applied Symbolic Computation

Lagrange Interpolation Formula • Find a polynomial of degree m given its value at (m+1)distinct points. Assume A(

α) = yii

-^ Observe that

y^ i m i^ ij^

xj ji xA ∑^

∏= ≠^

⎞ ⎟ ⎟ ⎠ ⎛ ⎜ ⎜ ⎝

− − =^0

) (

)( )(

α^ αα

yy ki

m i^ ij^

jk ji

A^ k

⎞ ⎟=⎟ ⎠ ⎛ ⎜ ⎜ ⎝

− − =^ ∑^

∏= 0 ≠

) (

) ( )(

αα αα α

Applied Symbolic Computation

Matrix Interpretation of Interpolation • Let A(x) = a

n^ x+ … + an

x +abe a polynomial of degree n. 10 The problem of determining the (n+1) coefficients a

,…,a,an^10

from the (n+1) values A(

α),…,A(α^0

) is equivalent to solvingn

the linear system

⎤⎤⎡ ⎥⎥⎢ ⎥⎥⎢ ⎥⎥⎢ ⎥⎥⎢ ⎥^ ⎥⎢⎦⎣⎦ ⎡⎤ ⎢⎥ ⎢⎥=⎢⎥ ⎢⎥ ⎢⎥⎦⎣ ⎡ ⎢ ⎢ ⎢ ⎢ ⎢⎣

a a n an n

n n

A A An

0 1 3 1

1 1 1

0 1 0 (^01)

... ... 1

...... ... 1 ... 1 ...... )( )( ... )(

α α

α α

α α α α α

Applied Symbolic Computation

Vandermonde Matrix

α^

n n n

n n

n

V

1

1 1 1

0 1 0 0

det(

α α i j ji

V^

=^ ∏<

V(α,…,^ α^0

) is non-singular whenn

α,…,^ α^0 n^

are distinct.

Applied Symbolic Computation

Interpolation and Karatsuba’s

Algorithm

-^ Let A(x) = A

x + A, B(x) = B 10

x + BC(x) = A(x)B(x) = C 1 0,^

(^2) x+ 2

Cx + C^10 • Then A(

k) = A, B(

k) = B, and C = C(

kk) = A(

k)B(10) =

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

, C, and C 21 0

-^ Compute C = C

2k^10 + C^1021 k^ + C^0

Applied Symbolic Computation

Matrix Equation for Karatsuba’s

Algorithm

-^ Modified Vandermonde Matrix •^ Interpolation

⎤⎡⎤ ⎥⎢⎥ ⎥⎢⎥ ⎥⎢⎥⎦⎦⎣ ⎡⎤ ⎢⎥=⎢⎥ ⎢⎥⎣⎦ ⎡ ⎢ ⎢ ⎢∞^ ⎣

c c c C C C

0 1 12 (^00111100) ) (^0) ( ) (^1) ( )(

(^

)(^

⎤ ⎥ )⎥ ⎥⎦ ⎡ ⎢ ⎢ ⎢⎣

⎤ ⎥+⎥ ⎥⎦ ⎡ ⎢ ⎢ ⎢⎣

− ⎤ ⎥ −=⎥ ⎥ ⎦ ⎡ ⎢ ⎢ ⎢ ⎣

BBA BA A^

BA

c c c

(^10111) 0

(^00)

0 1 2

(^10) 0

(^11) 1

(^00) 1

Applied Symbolic Computation

Asymptotically Fast Integer

Multiplication

-^ We can obtain a sequence of asymptotically fastermultiplication algorithms by splitting the inputs into moreand more pieces. •^ If we split A and B into k equal parts, then thecorresponding multiplication algorithm is obtained from aninterpolation based polynomial multiplication algorithm oftwo degree (k-1) polynomials. •^ Since the product polynomial is of degree 2(k-1), we needto 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)log=^ Θ(n

(2k-1)).k

Applied Symbolic Computation

Asymptotically Fast Integer

Multiplication

-^ Using the previous construction we can find an algorithmto multiply two n digit integers in time

1+^ ε Θ(n) for any

positive^ ε

. – log(2k-1) = log(k(2-1/k)) = 1 + logkk

(2-1/k)k

-^ log(2-1/k)k

≤^ log(2) = ln(2)/ln(k)k

→^ 0.

-^ Can we do better? •^ The answer is yes. There is a faster algorithm, withcomputing 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.