0% found this document useful (0 votes)
58 views18 pages

05 Multiply

multiply

Uploaded by

anant_nimkar9243
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views18 pages

05 Multiply

multiply

Uploaded by

anant_nimkar9243
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 18

How to Multiply

integers, matrices, and polynomials

Slides by Kevin Wayne.


Copyright © 2005 Pearson-Addison Wesley.
All rights reserved.

1
Complex Multiplication

Complex multiplication. (a + bi) (c + di) = x + yi.

Grade-school. x = ac - bd, y = bc + ad.

4 multiplications, 2 additions

Q. Is it possible to do with fewer multiplications?

2
Complex Multiplication

Complex multiplication. (a + bi) (c + di) = x + yi.

Grade-school. x = ac - bd, y = bc + ad.

4 multiplications, 2 additions

Q. Is it possible to do with fewer multiplications?


A. Yes. [Gauss] x = ac - bd, y = (a + b) (c + d) - ac - bd.

3 multiplications, 5 additions

Remark. Improvement if no hardware multiply.

3
5.5 Integer Multiplication
Integer Addition

Addition. Given two n-bit integers a and b, compute a + b.


Grade-school. (n) bit operations.

1 1 1 1 1 1 0 1
1 1 0 1 0 1 0 1
+ 0 1 1 1 1 1 0 1
1 0 1 0 1 0 0 1 0

Remark. Grade-school addition algorithm is optimal.

5
Integer Multiplication

1 1 0 1 0 1 0 1
 0 1 1 1 1 1 0 1
1 1 0 1 0 1 0 1
0 0 0 0 0 0 0 0 0
1 1 0 1 0 1 0 1 0
1 1 0 1 0 1 0 1 0
1 1 0 1 0 1 0 1 0
1 1 0 1 0 1 0 1 0
1 1 0 1 0 1 0 1 0
0 0 0 0 0 0 0 0 0
0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 1

6
Divide-and-Conquer Multiplication: Warmup

To multiply two n-bit integers a and b:



Multiply four ½n-bit integers, recursively.

Add and shift to obtain result.

Ex. a = 10001101 b = 11100001


a1 a0 b1 b0

7
Recursion Tree

T(n) n

T(n/2) T(n/2) T(n/2) T(n/2) 4(n/2)

T(n/4) T(n/4) T(n/4) T(n/4) ... T(n/4) T(n/4) T(n/4) T(n/4) 16(n/4)

...
...

T(n / 2k) 4k (n / 2k)

...
...

T(2) T(2) T(2) T(2) ... T(2) T(2) T(2) T(2) 4 lg n


(1)

8
Karatsuba Multiplication

To multiply two n-bit integers a and b:



Add two ½n bit integers.

Multiply three ½n-bit integers, recursively.

Add, subtract, and shift to obtain result.

1 2 1 3 3

9
Karatsuba Multiplication

To multiply two n-bit integers a and b:



Add two ½n bit integers.

Multiply three ½n-bit integers, recursively.

Add, subtract, and shift to obtain result.

1 2 1 3 3

Theorem. [Karatsuba-Ofman 1962] Can multiply two n-bit integers


in O(n1.585) bit operations.

10
Karatsuba: Recursion Tree

T(n) n

T(n/2) T(n/2) T(n/2) 3(n/2)

T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) 9(n/4)

...
...

T(n / 2k) 3k (n / 2k)

...
...

T(2) T(2) T(2) T(2) ... T(2) T(2) T(2) T(2) 3 lg n


(1)

11
Matrix Multiplication
Dot Product

Dot product. Given two length n vectors a and b, compute c = a  b.


Grade-school. (n) arithmetic operations.

Remark. Grade-school dot product algorithm is optimal.

13
Matrix Multiplication

Matrix multiplication. Given two n-by-n matrices A and B, compute C = AB.


Grade-school. (n3) arithmetic operations.

Q. Is grade-school matrix multiplication algorithm optimal?

14
Block Matrix Multiplication

A11 A12 B11


C11

B11

15
Matrix Multiplication: Warmup

To multiply two n-by-n matrices A and B:



Divide: partition A and B into ½n-by-½n blocks.

Conquer: multiply 8 pairs of ½n-by-½n matrices, recursively.

Combine: add appropriate products using 4 matrix additions.

16
Fast Matrix Multiplication

Key idea. multiply 2-by-2 blocks with only 7 multiplications.


7 multiplications.

18 = 8 + 10 additions and subtractions.

17
Fast Matrix Multiplication

To multiply two n-by-n matrices A and B: [Strassen 1969]



Divide: partition A and B into ½n-by-½n blocks.

Compute: 14 ½n-by-½n matrices via 10 matrix additions.

Conquer: multiply 7 pairs of ½n-by-½n matrices, recursively.

Combine: 7 products into 4 terms using 8 matrix additions.

Analysis.

Assume n is a power of 2.

T(n) = # arithmetic operations.

18

You might also like