Download as pdf or txt
Download as pdf or txt
You are on page 1of 116

Divide-and-Conquer:

Polynomial Multiplication

Neil Rhodes
Department of Computer Science and Engineering
University of California, San Diego

Data Structures and Algorithms


Algorithmic Toolbox
Outline

1 Problem Overview

2 Naı̈ve Algorithm

3 Naı̈ve Divide and Conquer Algorithm

4 Faster Divide and Conquer


Uses of multiplying polynomials

Error-correcting codes
Large-integer multiplication
Generating functions
Convolution in signal processing
Multiplying Polynomials

Example
Multiplying Polynomials

Example

A(x) = 3x 2 + 2x + 5
Multiplying Polynomials

Example

A(x) = 3x 2 + 2x + 5
B(x) = 5x 2 + x + 2
Multiplying Polynomials

Example

A(x) = 3x 2 + 2x + 5
B(x) = 5x 2 + x + 2
A(x)B(x) = 15x 4 + 13x 3 + 33x 2 + 9x + 10
Multiplying polynomials
Input: Two n − 1 degree polynomials:
an−1 x n−1 + an−2 x n−2 + · · · + a1 x + a0
bn−1 x n−1 + bn−2 x n−2 + · · · + b1 x + b0
Output:
Multiplying polynomials
Input: Two n − 1 degree polynomials:
an−1 x n−1 + an−2 x n−2 + · · · + a1 x + a0
bn−1 x n−1 + bn−2 x n−2 + · · · + b1 x + b0
Output: The product polynomial:
c2n−2 x 2n−2 + c2n−3 x 2n−3 + · · · + c1 x + c0
Multiplying polynomials
Input: Two n − 1 degree polynomials:
an−1 x n−1 + an−2 x n−2 + · · · + a1 x + a0
bn−1 x n−1 + bn−2 x n−2 + · · · + b1 x + b0
Output: The product polynomial:
c2n−2 x 2n−2 + c2n−3 x 2n−3 + · · · + c1 x + c0
where:
c2n−2 = an−1 bn−1
Multiplying polynomials
Input: Two n − 1 degree polynomials:
an−1 x n−1 + an−2 x n−2 + · · · + a1 x + a0
bn−1 x n−1 + bn−2 x n−2 + · · · + b1 x + b0
Output: The product polynomial:
c2n−2 x 2n−2 + c2n−3 x 2n−3 + · · · + c1 x + c0
where:
c2n−2 = an−1 bn−1
c2n−3 = an−1 bn−2 + an−2 bn−1
Multiplying polynomials
Input: Two n − 1 degree polynomials:
an−1 x n−1 + an−2 x n−2 + · · · + a1 x + a0
bn−1 x n−1 + bn−2 x n−2 + · · · + b1 x + b0
Output: The product polynomial:
c2n−2 x 2n−2 + c2n−3 x 2n−3 + · · · + c1 x + c0
where:
c2n−2 = an−1 bn−1
c2n−3 = an−1 bn−2 + an−2 bn−1
...
c2 = a2 b0 + a1 b1 + a0 b2
Multiplying polynomials
Input: Two n − 1 degree polynomials:
an−1 x n−1 + an−2 x n−2 + · · · + a1 x + a0
bn−1 x n−1 + bn−2 x n−2 + · · · + b1 x + b0
Output: The product polynomial:
c2n−2 x 2n−2 + c2n−3 x 2n−3 + · · · + c1 x + c0
where:
c2n−2 = an−1 bn−1
c2n−3 = an−1 bn−2 + an−2 bn−1
...
c2 = a2 b0 + a1 b1 + a0 b2
c1 = a1 b0 + a0 b1
Multiplying polynomials
Input: Two n − 1 degree polynomials:
an−1 x n−1 + an−2 x n−2 + · · · + a1 x + a0
bn−1 x n−1 + bn−2 x n−2 + · · · + b1 x + b0
Output: The product polynomial:
c2n−2 x 2n−2 + c2n−3 x 2n−3 + · · · + c1 x + c0
where:
c2n−2 = an−1 bn−1
c2n−3 = an−1 bn−2 + an−2 bn−1
...
c2 = a2 b0 + a1 b1 + a0 b2
c1 = a1 b0 + a0 b1
c0 = a0 b0
Multiplying Polynomials
Example
Input: n = 3, A = (3, 2, 5), B = (5, 1, 2)
Multiplying Polynomials
Example
Input: n = 3, A = (3, 2, 5), B = (5, 1, 2)

A(x) = 3x 2 + 2x + 5
Multiplying Polynomials
Example
Input: n = 3, A = (3, 2, 5), B = (5, 1, 2)

A(x) = 3x 2 + 2x + 5
B(x) = 5x 2 + x + 2
Multiplying Polynomials
Example
Input: n = 3, A = (3, 2, 5), B = (5, 1, 2)

A(x) = 3x 2 + 2x + 5
B(x) = 5x 2 + x + 2
A(x)B(x) = 15x 4 + 13x 3 + 33x 2 + 9x + 10
Multiplying Polynomials
Example
Input: n = 3, A = (3, 2, 5), B = (5, 1, 2)

A(x) = 3x 2 + 2x + 5
B(x) = 5x 2 + x + 2
A(x)B(x) = 15x 4 + 13x 3 + 33x 2 + 9x + 10

Output: C = (15, 13, 33, 9, 10)


Outline

1 Problem Overview

2 Naı̈ve Algorithm

3 Naı̈ve Divide and Conquer Algorithm

4 Faster Divide and Conquer


MultPoly(A, B, n)
pair ← Array [n][n]
for i from 0 to n − 1:
for j from 0 to n − 1:
pair [i][j] ← A[i] * B[j]
MultPoly(A, B, n)
pair ← Array [n][n]
for i from 0 to n − 1:
for j from 0 to n − 1:
pair [i][j] ← A[i] * B[j]
product ← Array [2n − 1]
for i from 0 to 2n − 1:
product[i] ← 0
MultPoly(A, B, n)
pair ← Array [n][n]
for i from 0 to n − 1:
for j from 0 to n − 1:
pair [i][j] ← A[i] * B[j]
product ← Array [2n − 1]
for i from 0 to 2n − 1:
product[i] ← 0
for i from 0 to n − 1:
for j from 0 to n − 1:
product[i + j] ← product[i + j] + pair [i][j]
MultPoly(A, B, n)
pair ← Array [n][n]
for i from 0 to n − 1:
for j from 0 to n − 1:
pair [i][j] ← A[i] * B[j]
product ← Array [2n − 1]
for i from 0 to 2n − 1:
product[i] ← 0
for i from 0 to n − 1:
for j from 0 to n − 1:
product[i + j] ← product[i + j] + pair [i][j]
return product
Multiplying Polynomials

Naı̈ve Solution: O(n2)


Multiply all ai bj pairs (n2
multiplications)
Multiplying Polynomials

Naı̈ve Solution: O(n2)


Multiply all ai bj pairs (n2
multiplications)
Sum needed pairs (n2 additions)
Outline

1 Problem Overview

2 Naı̈ve Algorithm

3 Naı̈ve Divide and Conquer Algorithm

4 Faster Divide and Conquer


Multiplying Polynomials
n
Let A(x) = D1(x)x 2 + D0(x) where
n n
D1(x) = an−1x 2 −1 + an−2x 2 −2 + ... + a n2
n n
D0(x) = a n2 −1x 2 −1 + a n2 −2x 2 −2 + ... + a0
Multiplying Polynomials
n
Let A(x) = D1(x)x 2 + D0(x) where
n n
D1(x) = an−1x 2 −1 + an−2x 2 −2 + ... + a n2
n n
D0(x) = a n2 −1x 2 −1 + a n2 −2x 2 −2 + ... + a0
n
Let B(x) = E1(x)x 2 + E0(x) where
n n
E1(x) = bn−1x 2 −1 + bn−2x 2 −2 + ... + b n2
n n
E0(x) = b n2 −1x 2 −1 + b n2 −2x 2 −2 + ... + b0
Multiplying Polynomials
n
Let A(x) = D1(x)x 2 + D0(x) where
n n
D1(x) = an−1x 2 −1 + an−2x 2 −2 + ... + a n2
n n
D0(x) = a n2 −1x 2 −1 + a n2 −2x 2 −2 + ... + a0
n
Let B(x) = E1(x)x 2 + E0(x) where
n n
E1(x) = bn−1x 2 −1 + bn−2x 2 −2 + ... + b n2
n n
E0(x) = b n2 −1x 2 −1 + b n2 −2x 2 −2 + ... + b0
n n
AB = (D1x 2 + D0)(E1x 2 + E0)
n
= (D1E1)x n + (D1E0 + D0E1)x 2 + D0E0
Multiplying Polynomials
n
Let A(x) = D1(x)x 2 + D0(x) where
n n
D1(x) = an−1x 2 −1 + an−2x 2 −2 + ... + a n2
n n
D0(x) = a n2 −1x 2 −1 + a n2 −2x 2 −2 + ... + a0
n
Let B(x) = E1(x)x 2 + E0(x) where
n n
E1(x) = bn−1x 2 −1 + bn−2x 2 −2 + ... + b n2
n n
E0(x) = b n2 −1x 2 −1 + b n2 −2x 2 −2 + ... + b0
n n
AB = (D1x 2 + D0)(E1x 2 + E0)
n
= (D1E1)x n + (D1E0 + D0E1)x 2 + D0E0
Calculate D1E1, D1E0, D0E1, and D0E0
Multiplying Polynomials
n
Let A(x) = D1(x)x 2 + D0(x) where
n n
D1(x) = an−1x 2 −1 + an−2x 2 −2 + ... + a n2
n n
D0(x) = a n2 −1x 2 −1 + a n2 −2x 2 −2 + ... + a0
n
Let B(x) = E1(x)x 2 + E0(x) where
n n
E1(x) = bn−1x 2 −1 + bn−2x 2 −2 + ... + b n2
n n
E0(x) = b n2 −1x 2 −1 + b n2 −2x 2 −2 + ... + b0
n n
AB = (D1x 2 + D0)(E1x 2 + E0)
n
= (D1E1)x n + (D1E0 + D0E1)x 2 + D0E0
Calculate D1E1, D1E0, D0E1, and D0E0
Multiplying Polynomials
n
Let A(x) = D1(x)x 2 + D0(x) where
n n
D1(x) = an−1x 2 −1 + an−2x 2 −2 + ... + a n2
n n
D0(x) = a n2 −1x 2 −1 + a n2 −2x 2 −2 + ... + a0
n
Let B(x) = E1(x)x 2 + E0(x) where
n n
E1(x) = bn−1x 2 −1 + bn−2x 2 −2 + ... + b n2
n n
E0(x) = b n2 −1x 2 −1 + b n2 −2x 2 −2 + ... + b0
n n
AB = (D1x 2 + D0)(E1x 2 + E0)
n
= (D1E1)x n + (D1E0 + D0E1)x 2 + D0E0
Calculate D1E1, D1E0, D0E1, and D0E0
Recurrence: T (n) = 4T ( n2 ) + kn.
Polynomial Mult: Divide & Conquer
A(x) = 4x 3 + 3x 2 + 2x + 1
B(x) = x 3 + 2x 2 + 3x + 4
Polynomial Mult: Divide & Conquer
A(x) = 4x 3 + 3x 2 + 2x + 1
B(x) = x 3 + 2x 2 + 3x + 4
D1 (x) = 4x + 3
Polynomial Mult: Divide & Conquer
A(x) = 4x 3 + 3x 2 + 2x + 1
B(x) = x 3 + 2x 2 + 3x + 4
D1 (x) = 4x + 3 D0 (x) = 2x + 1
Polynomial Mult: Divide & Conquer
A(x) = 4x 3 + 3x 2 + 2x + 1
B(x) = x 3 + 2x 2 + 3x + 4
D1 (x) = 4x + 3 D0 (x) = 2x + 1
E1 (x) = x + 2
Polynomial Mult: Divide & Conquer
A(x) = 4x 3 + 3x 2 + 2x + 1
B(x) = x 3 + 2x 2 + 3x + 4
D1 (x) = 4x + 3 D0 (x) = 2x + 1
E1 (x) = x + 2 E0 (x) = 3x + 4
Polynomial Mult: Divide & Conquer
A(x) = 4x 3 + 3x 2 + 2x + 1
B(x) = x 3 + 2x 2 + 3x + 4
D1 (x) = 4x + 3 D0 (x) = 2x + 1
E1 (x) = x + 2 E0 (x) = 3x + 4
D1 E1 = 4x 2 + 11x + 6
Polynomial Mult: Divide & Conquer
A(x) = 4x 3 + 3x 2 + 2x + 1
B(x) = x 3 + 2x 2 + 3x + 4
D1 (x) = 4x + 3 D0 (x) = 2x + 1
E1 (x) = x + 2 E0 (x) = 3x + 4
D1 E1 = 4x 2 + 11x + 6 D1 E0 = 12x 2 + 25x + 12
Polynomial Mult: Divide & Conquer
A(x) = 4x 3 + 3x 2 + 2x + 1
B(x) = x 3 + 2x 2 + 3x + 4
D1 (x) = 4x + 3 D0 (x) = 2x + 1
E1 (x) = x + 2 E0 (x) = 3x + 4
D1 E1 = 4x 2 + 11x + 6 D1 E0 = 12x 2 + 25x + 12
D0 E1 = 2x 2 + 5x + 2
Polynomial Mult: Divide & Conquer
A(x) = 4x 3 + 3x 2 + 2x + 1
B(x) = x 3 + 2x 2 + 3x + 4
D1 (x) = 4x + 3 D0 (x) = 2x + 1
E1 (x) = x + 2 E0 (x) = 3x + 4
D1 E1 = 4x 2 + 11x + 6 D1 E0 = 12x 2 + 25x + 12
D0 E1 = 2x 2 + 5x + 2 D0 E0 = 6x 2 + 11x + 4
Polynomial Mult: Divide & Conquer
A(x) = 4x 3 + 3x 2 + 2x + 1
B(x) = x 3 + 2x 2 + 3x + 4
D1 (x) = 4x + 3 D0 (x) = 2x + 1
E1 (x) = x + 2 E0 (x) = 3x + 4
D1 E1 = 4x 2 + 11x + 6 D1 E0 = 12x 2 + 25x + 12
D0 E1 = 2x 2 + 5x + 2 D0 E0 = 6x 2 + 11x + 4
AB =
Polynomial Mult: Divide & Conquer
A(x) = 4x 3 + 3x 2 + 2x + 1
B(x) = x 3 + 2x 2 + 3x + 4
D1 (x) = 4x + 3 D0 (x) = 2x + 1
E1 (x) = x + 2 E0 (x) = 3x + 4
D1 E1 = 4x 2 + 11x + 6 D1 E0 = 12x 2 + 25x + 12
D0 E1 = 2x 2 + 5x + 2 D0 E0 = 6x 2 + 11x + 4
AB = (4x 2 + 11x + 6)x 4 +
Polynomial Mult: Divide & Conquer
A(x) = 4x 3 + 3x 2 + 2x + 1
B(x) = x 3 + 2x 2 + 3x + 4
D1 (x) = 4x + 3 D0 (x) = 2x + 1
E1 (x) = x + 2 E0 (x) = 3x + 4
D1 E1 = 4x 2 + 11x + 6 D1 E0 = 12x 2 + 25x + 12
D0 E1 = 2x 2 + 5x + 2 D0 E0 = 6x 2 + 11x + 4
AB = (4x 2 + 11x + 6)x 4 +
( )x 2 +
Polynomial Mult: Divide & Conquer
A(x) = 4x 3 + 3x 2 + 2x + 1
B(x) = x 3 + 2x 2 + 3x + 4
D1 (x) = 4x + 3 D0 (x) = 2x + 1
E1 (x) = x + 2 E0 (x) = 3x + 4
D1 E1 = 4x 2 + 11x + 6 D1 E0 = 12x 2 + 25x + 12
D0 E1 = 2x 2 + 5x + 2 D0 E0 = 6x 2 + 11x + 4
AB = (4x 2 + 11x + 6)x 4 +
(12x 2 + 25x + 12 )x 2 +
Polynomial Mult: Divide & Conquer
A(x) = 4x 3 + 3x 2 + 2x + 1
B(x) = x 3 + 2x 2 + 3x + 4
D1 (x) = 4x + 3 D0 (x) = 2x + 1
E1 (x) = x + 2 E0 (x) = 3x + 4
D1 E1 = 4x 2 + 11x + 6 D1 E0 = 12x 2 + 25x + 12
D0 E1 = 2x 2 + 5x + 2 D0 E0 = 6x 2 + 11x + 4
AB = (4x 2 + 11x + 6)x 4 +
(12x 2 + 25x + 12 + 2x 2 + 5x + 2)x 2 +
Polynomial Mult: Divide & Conquer
A(x) = 4x 3 + 3x 2 + 2x + 1
B(x) = x 3 + 2x 2 + 3x + 4
D1 (x) = 4x + 3 D0 (x) = 2x + 1
E1 (x) = x + 2 E0 (x) = 3x + 4
D1 E1 = 4x 2 + 11x + 6 D1 E0 = 12x 2 + 25x + 12
D0 E1 = 2x 2 + 5x + 2 D0 E0 = 6x 2 + 11x + 4
AB = (4x 2 + 11x + 6)x 4 +
(12x 2 + 25x + 12 + 2x 2 + 5x + 2)x 2 +
6x 2 + 11x + 4
Polynomial Mult: Divide & Conquer
A(x) = 4x 3 + 3x 2 + 2x + 1
B(x) = x 3 + 2x 2 + 3x + 4
D1 (x) = 4x + 3 D0 (x) = 2x + 1
E1 (x) = x + 2 E0 (x) = 3x + 4
D1 E1 = 4x 2 + 11x + 6 D1 E0 = 12x 2 + 25x + 12
D0 E1 = 2x 2 + 5x + 2 D0 E0 = 6x 2 + 11x + 4
AB = (4x 2 + 11x + 6)x 4 +
(12x 2 + 25x + 12 + 2x 2 + 5x + 2)x 2 +
6x 2 + 11x + 4
=4x 6 + 11x 5 + 20x 4 + 30x 3 + 20x 2 + 11x + 4
Function Mult2(A, B, n, al , bl )
Function Mult2(A, B, n, al , bl )
R = array[0..2n − 2]
Function Mult2(A, B, n, al , bl )
R = array[0..2n − 2]
if n = 1:
R[0] = A[al ] * B[bl ] ; return R
Function Mult2(A, B, n, al , bl )
R = array[0..2n − 2]
if n = 1:
R[0] = A[al ] * B[bl ] ; return R
R[0..n − 2] = Mult2(A, B, n2 , al , bl )
Function Mult2(A, B, n, al , bl )
R = array[0..2n − 2]
if n = 1:
R[0] = A[al ] * B[bl ] ; return R
R[0..n − 2] = Mult2(A, B, n2 , al , bl )
R[n..2n − 2] = Mult2(A, B, n2 , al + n2 , bl +
n
2)
Function Mult2(A, B, n, al , bl )
R = array[0..2n − 2]
if n = 1:
R[0] = A[al ] * B[bl ] ; return R
R[0..n − 2] = Mult2(A, B, n2 , al , bl )
R[n..2n − 2] = Mult2(A, B, n2 , al + n2 , bl +
n
2)
D0E1 = Mult2(A, B, n2 , al , bl + n2 )
Function Mult2(A, B, n, al , bl )
R = array[0..2n − 2]
if n = 1:
R[0] = A[al ] * B[bl ] ; return R
R[0..n − 2] = Mult2(A, B, n2 , al , bl )
R[n..2n − 2] = Mult2(A, B, n2 , al + n2 , bl +
n
2)
D0E1 = Mult2(A, B, n2 , al , bl + n2 )
D1E0 = Mult2(A, B, n2 , al + n2 , bl )
R[ n2 . . . n + n2 − 2] += D1E 0 + D0E1
Function Mult2(A, B, n, al , bl )
R = array[0..2n − 2]
if n = 1:
R[0] = A[al ] * B[bl ] ; return R
R[0..n − 2] = Mult2(A, B, n2 , al , bl )
R[n..2n − 2] = Mult2(A, B, n2 , al + n2 , bl +
n
2)
D0E1 = Mult2(A, B, n2 , al , bl + n2 )
D1E0 = Mult2(A, B, n2 , al + n2 , bl )
R[ n2 . . . n + n2 − 2] += D1E 0 + D0E1
return R
n
level
n
n/2 n/2 n/2 n/2
level
0 n
1 n/2 n/2 n/2 n/2
level
0 n
1 n/2 n/2 n/2 n/2
.. ..
. .
i · · · n/2i · · ·
level
0 n
1 n/2 n/2 n/2 n/2
.. ..
. .
i · · · n/2i · · ·
.. ..
. .
log2 n 1 ··· 1
level #
0 n 1
1 n/2 n/2 n/2 n/2
.. ..
. .
i · · · n/2i · · ·
.. ..
. .
log2 n 1 ··· 1
level #
0 n 1
1 n/2 n/2 n/2 n/2 4
.. .. ..
. . .
i · · · n/2i · · ·
.. ..
. .
log2 n 1 ··· 1
level #
0 n 1
1 n/2 n/2 n/2 n/2 4
.. .. ..
. . .
i · · · n/2i · · · 4i
.. ..
. .
log2 n 1 ··· 1
level #
0 n 1
1 n/2 n/2 n/2 n/2 4
.. .. ..
. . .
i · · · n/2i · · · 4i
.. .. ..
. . .
log2 n 1 ··· 1 4log2 n
level # work
0 n 1
1 n/2 n/2 n/2 n/2 4
.. .. ..
. . .
i · · · n/2i · · · 4i
.. .. ..
. . .
log2 n 1 ··· 1 4log2 n
level # work
0 n 1 kn
1 n/2 n/2 n/2 n/2 4
.. .. ..
. . .
i · · · n/2i · · · 4i
.. .. ..
. . .
log2 n 1 ··· 1 4log2 n
level # work
0 n 1 kn
1 n/2 n/2 n/2 n/2 4 4k n2 = k2n
.. .. ..
. . .
i · · · n/2i · · · 4i
.. .. ..
. . .
log2 n 1 ··· 1 4log2 n
level # work
0 n 1 kn
1 n/2 n/2 n/2 n/2 4 4k n2 = k2n
.. .. .. ..
. . . .
i · · · n/2i · · · 4i 4i k 2ni = k2i n
.. .. ..
. . .
log2 n 1 ··· 1 4log2 n
level # work
0 n 1 kn
1 n/2 n/2 n/2 n/2 4 4k n2 = k2n
.. .. .. ..
. . . .
i · · · n/2i · · · 4i 4i k 2ni = k2i n
.. .. .. ..
. . . .
log2 n 1 ··· 1 4log2 n k4log2 n = kn2
level # work
0 n 1 kn
1 n/2 n/2 n/2 n/2 4 4k n2 = k2n
.. .. .. ..
. . . .
i · · · n/2i · · · 4i 4i k 2ni = k2i n
.. .. .. ..
. . . .
log2 n 1 ··· 1 4log2 n k4log2 n = kn2
∑︀log n
Total: i=02 4i k 2ni = Θ(n2 )
Outline

1 Problem Overview

2 Naı̈ve Algorithm

3 Naı̈ve Divide and Conquer Algorithm

4 Faster Divide and Conquer


Karatsuba approach
Karatsuba approach
A(x) =a1x + a0
Karatsuba approach
A(x) =a1x + a0
B(x) =b1x + b0
Karatsuba approach
A(x) =a1x + a0
B(x) =b1x + b0
C (x) =a1b1x 2 + (a1b0 + a0b1)x + a0b0
Karatsuba approach
A(x) =a1x + a0
B(x) =b1x + b0
C (x) =a1b1x 2 + (a1b0 + a0b1)x + a0b0
Needs 4 multiplications
Karatsuba approach
A(x) =a1x + a0
B(x) =b1x + b0
C (x) =a1b1x 2 + (a1b0 + a0b1)x + a0b0
Needs 4 multiplications

Rewrite as:
Karatsuba approach
A(x) =a1x + a0
B(x) =b1x + b0
C (x) =a1b1x 2 + (a1b0 + a0b1)x + a0b0
Needs 4 multiplications

Rewrite as:
C (x) =a1b1x 2+
((a1 + a0)(b1 + b0) − a1b1 − a0b0)x+
a0b0
Karatsuba approach
A(x) =a1x + a0
B(x) =b1x + b0
C (x) =a1b1x 2 + (a1b0 + a0b1)x + a0b0
Needs 4 multiplications

Rewrite as:
C (x) =a1b1x 2+
((a1 + a0)(b1 + b0) − a1b1 − a0b0)x+
a0b0
Needs 3 multiplications
Karatsuba approach
A(x) =a1x + a0
B(x) =b1x + b0
C (x) =a1b1x 2 + (a1b0 + a0b1)x + a0b0
Needs 4 multiplications

Rewrite as:
C (x) =a1b1x 2+
((a1 + a0)(b1 + b0) − a1b1 − a0b0)x+
a0b0
Needs 3 multiplications
Karatsuba approach
A(x) =a1x + a0
B(x) =b1x + b0
C (x) =a1b1x 2 + (a1b0 + a0b1)x + a0b0
Needs 4 multiplications

Rewrite as:
C (x) =a1b1x 2+
((a1 + a0)(b1 + b0) − a1b1 − a0b0)x+
a0b0
Needs 3 multiplications
Karatsuba Example
A(x) = 4x 3 + 3x 2 + 2x + 1
B(x) = x 3 + 2x 2 + 3x + 4
Karatsuba Example
A(x) = 4x 3 + 3x 2 + 2x + 1
B(x) = x 3 + 2x 2 + 3x + 4
D1(x) = 4x + 3
Karatsuba Example
A(x) = 4x 3 + 3x 2 + 2x + 1
B(x) = x 3 + 2x 2 + 3x + 4
D1(x) = 4x + 3 D0(x) = 2x + 1
Karatsuba Example
A(x) = 4x 3 + 3x 2 + 2x + 1
B(x) = x 3 + 2x 2 + 3x + 4
D1(x) = 4x + 3 D0(x) = 2x + 1
E1(x) = x + 2
Karatsuba Example
A(x) = 4x 3 + 3x 2 + 2x + 1
B(x) = x 3 + 2x 2 + 3x + 4
D1(x) = 4x + 3 D0(x) = 2x + 1
E1(x) = x + 2 E0(x) = 3x + 4
Karatsuba Example
A(x) = 4x 3 + 3x 2 + 2x + 1
B(x) = x 3 + 2x 2 + 3x + 4
D1(x) = 4x + 3 D0(x) = 2x + 1
E1(x) = x + 2 E0(x) = 3x + 4
2
D1E1 = 4x + 11x + 6
Karatsuba Example
A(x) = 4x 3 + 3x 2 + 2x + 1
B(x) = x 3 + 2x 2 + 3x + 4
D1(x) = 4x + 3 D0(x) = 2x + 1
E1(x) = x + 2 E0(x) = 3x + 4
2
D1E1 = 4x + 11x + 6 D0E0 = 6x 2 + 11x + 4
Karatsuba Example
A(x) = 4x 3 + 3x 2 + 2x + 1
B(x) = x 3 + 2x 2 + 3x + 4
D1(x) = 4x + 3 D0(x) = 2x + 1
E1(x) = x + 2 E0(x) = 3x + 4
2
D1E1 = 4x + 11x + 6 D0E0 = 6x 2 + 11x + 4
(D1 + D0)(E1 + E0) =
Karatsuba Example
A(x) = 4x 3 + 3x 2 + 2x + 1
B(x) = x 3 + 2x 2 + 3x + 4
D1(x) = 4x + 3 D0(x) = 2x + 1
E1(x) = x + 2 E0(x) = 3x + 4
2
D1E1 = 4x + 11x + 6 D0E0 = 6x 2 + 11x + 4
(D1 + D0)(E1 + E0) = (6x + 4)
Karatsuba Example
A(x) = 4x 3 + 3x 2 + 2x + 1
B(x) = x 3 + 2x 2 + 3x + 4
D1(x) = 4x + 3 D0(x) = 2x + 1
E1(x) = x + 2 E0(x) = 3x + 4
2
D1E1 = 4x + 11x + 6 D0E0 = 6x 2 + 11x + 4
(D1 + D0)(E1 + E0) = (6x + 4)(4x + 6)
Karatsuba Example
A(x) = 4x 3 + 3x 2 + 2x + 1
B(x) = x 3 + 2x 2 + 3x + 4
D1(x) = 4x + 3 D0(x) = 2x + 1
E1(x) = x + 2 E0(x) = 3x + 4
2
D1E1 = 4x + 11x + 6 D0E0 = 6x 2 + 11x + 4
(D1 + D0)(E1 + E0) = (6x + 4)(4x + 6)
= 24x 2 + 52x + 24
Karatsuba Example
A(x) = 4x 3 + 3x 2 + 2x + 1
B(x) = x 3 + 2x 2 + 3x + 4
D1(x) = 4x + 3 D0(x) = 2x + 1
E1(x) = x + 2 E0(x) = 3x + 4
2
D1E1 = 4x + 11x + 6 D0E0 = 6x 2 + 11x + 4
(D1 + D0)(E1 + E0) = (6x + 4)(4x + 6)
= 24x 2 + 52x + 24
AB = (4x 2 + 11x + 6)x 4 +
Karatsuba Example
A(x) = 4x 3 + 3x 2 + 2x + 1
B(x) = x 3 + 2x 2 + 3x + 4
D1(x) = 4x + 3 D0(x) = 2x + 1
E1(x) = x + 2 E0(x) = 3x + 4
2
D1E1 = 4x + 11x + 6 D0E0 = 6x 2 + 11x + 4
(D1 + D0)(E1 + E0) = (6x + 4)(4x + 6)
= 24x 2 + 52x + 24
AB = (4x 2 + 11x + 6)x 4 +
(
)x 2 +
Karatsuba Example
A(x) = 4x 3 + 3x 2 + 2x + 1
B(x) = x 3 + 2x 2 + 3x + 4
D1(x) = 4x + 3 D0(x) = 2x + 1
E1(x) = x + 2 E0(x) = 3x + 4
2
D1E1 = 4x + 11x + 6 D0E0 = 6x 2 + 11x + 4
(D1 + D0)(E1 + E0) = (6x + 4)(4x + 6)
= 24x 2 + 52x + 24
AB = (4x 2 + 11x + 6)x 4 +
(24x 2 + 52x + 24
)x 2 +
Karatsuba Example
A(x) = 4x 3 + 3x 2 + 2x + 1
B(x) = x 3 + 2x 2 + 3x + 4
D1(x) = 4x + 3 D0(x) = 2x + 1
E1(x) = x + 2 E0(x) = 3x + 4
2
D1E1 = 4x + 11x + 6 D0E0 = 6x 2 + 11x + 4
(D1 + D0)(E1 + E0) = (6x + 4)(4x + 6)
= 24x 2 + 52x + 24
AB = (4x 2 + 11x + 6)x 4 +
(24x 2 + 52x + 24 − (4x 2 + 11x + 6)
)x 2 +
Karatsuba Example
A(x) = 4x 3 + 3x 2 + 2x + 1
B(x) = x 3 + 2x 2 + 3x + 4
D1(x) = 4x + 3 D0(x) = 2x + 1
E1(x) = x + 2 E0(x) = 3x + 4
2
D1E1 = 4x + 11x + 6 D0E0 = 6x 2 + 11x + 4
(D1 + D0)(E1 + E0) = (6x + 4)(4x + 6)
= 24x 2 + 52x + 24
AB = (4x 2 + 11x + 6)x 4 +
(24x 2 + 52x + 24 − (4x 2 + 11x + 6)
− (6x 2 + 11x + 4))x 2 +
Karatsuba Example
A(x) = 4x 3 + 3x 2 + 2x + 1
B(x) = x 3 + 2x 2 + 3x + 4
D1(x) = 4x + 3 D0(x) = 2x + 1
E1(x) = x + 2 E0(x) = 3x + 4
2
D1E1 = 4x + 11x + 6 D0E0 = 6x 2 + 11x + 4
(D1 + D0)(E1 + E0) = (6x + 4)(4x + 6)
= 24x 2 + 52x + 24
AB = (4x 2 + 11x + 6)x 4 +
(24x 2 + 52x + 24 − (4x 2 + 11x + 6)
− (6x 2 + 11x + 4))x 2 +
6x 2 + 11x + 4
Karatsuba Example
A(x) = 4x 3 + 3x 2 + 2x + 1
B(x) = x 3 + 2x 2 + 3x + 4
D1(x) = 4x + 3 D0(x) = 2x + 1
E1(x) = x + 2 E0(x) = 3x + 4
2
D1E1 = 4x + 11x + 6 D0E0 = 6x 2 + 11x + 4
(D1 + D0)(E1 + E0) = (6x + 4)(4x + 6)
= 24x 2 + 52x + 24
AB = (4x 2 + 11x + 6)x 4 +
(24x 2 + 52x + 24 − (4x 2 + 11x + 6)
− (6x 2 + 11x + 4))x 2 +
6x 2 + 11x + 4
=4x 6 + 11x 5 + 20x 4 + 30x 3 + 20x 2 + 11x + 4
n
level
n
n/2 n/2 n/2
level
0 n
1 n/2 n/2 n/2
level
0 n
1 n/2 n/2 n/2
... ...
i · · · n/2i · · ·
level
0 n
1 n/2 n/2 n/2
... ...
i · · · n/2i · · ·
... ...
log2 n 1 ··· 1
level #
0 n 1
1 n/2 n/2 n/2
... ...
i · · · n/2i · · ·
... ...
log2 n 1 ··· 1
level #
0 n 1
1 n/2 n/2 n/2 3
... ... ...
i · · · n/2i · · ·
... ...
log2 n 1 ··· 1
level #
0 n 1
1 n/2 n/2 n/2 3
... ... ...
i · · · n/2i · · · 3i
... ...
log2 n 1 ··· 1
level #
0 n 1
1 n/2 n/2 n/2 3
... ... ...
i · · · n/2i · · · 3i
... ... ...
log2 n 1 ··· 1 3log2 n
level # work
0 n 1
1 n/2 n/2 n/2 3
... ... ...
i · · · n/2i · · · 3i
... ... ...
log2 n 1 ··· 1 3log2 n
level # work
0 n 1 kn
1 n/2 n/2 n/2 3
... ... ...
i · · · n/2i · · · 3i
... ... ...
log2 n 1 ··· 1 3log2 n
level # work
0 n 1 kn
1 n/2 n/2 n/2 3 3k n2 = k 32 n
... ... ...
i · · · n/2i · · · 3i
... ... ...
log2 n 1 ··· 1 3log2 n
level # work
0 n 1 kn
1 n/2 n/2 n/2 3 3k n2 = k 32 n
... ... ... ...
i · · · n/2i · · · 3i 3i k 2ni = k( 32 )i n
... ... ...
log2 n 1 ··· 1 3log2 n
level # work
0 n 1 kn
1 n/2 n/2 n/2 3 3k n2 = k 32 n
... ... ... ...
i · · · n/2i · · · 3i 3i k 2ni = k( 32 )i n
... ... ... ...
log2 n 1 ··· 1 3log2 n k3log2 n = knlog2 3
level # work
0 n 1 kn
1 n/2 n/2 n/2 3 3k n2 = k 32 n
... ... ... ...
i · · · n/2i · · · 3i 3i k 2ni = k( 32 )i n
... ... ... ...
log2 n 1 ··· 1 3log2 n k3log2 n = knlog2 3
∑︀log n
Total: i=02 3i k 2ni = Θ(nlog2 3)
= Θ(n1.58)

You might also like