You are on page 1of 45

Contents

1 Working with polynomials

TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, km s kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms January 17, 2017 1 / 17


Working with polynomials

Section outline

Multi-point polynomial
evaluation by DC
1 Working with polynomials Divide and Conquer
Coefficient based Strategy
representation Polynomial multiplication
Multi-point polynomial Polynomial multiplication by
evaluation DC (Karatsuba)
Solving Karatsuba multiplication
T (n) = 2T (n=2) + bn + c

TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, km s kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms January 17, 2017 2 / 17


Working with polynomials Coefficient based representation

Coefficient based representation


Consider polynomials of degree n 1 (coefficients in array):
A(x ) = a0 + a1 x + a2 + : : : + an
x2 1x
n 1

B (x ) = b0 + b1 x + b2 x 2 + : : : + bn 1x
n 1

Coefficients are often real numbers (float)


Common operations on these are:
Addition
Evaluation at a given set of points
Multiplication
Addition is easily done:
A(x ) + B (x ) =
(a0 + b0) + (a1 + b1)x + (a2 + b2)x 2 + : : : + (an 1 + bn 1)x n 1

Number of steps is proportional to the degree of the polynomials


TAdd (n) 2 O (n)
TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, km s kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms January 17, 2017 3 / 17


Working with polynomials Coefficient based representation

Coefficient based representation


Consider polynomials of degree n 1 (coefficients in array):
A(x ) = a0 + a1 x + a2 + : : : + an
x2 1x
n 1

B (x ) = b0 + b1 x + b2 x 2 + : : : + bn 1x
n 1

Coefficients are often real numbers (float)


Common operations on these are:
Addition
Evaluation at a given set of points
Multiplication
Addition is easily done:
A(x ) + B (x ) =
(a0 + b0) + (a1 + b1)x + (a2 + b2)x 2 + : : : + (an 1 + bn 1)x n 1

Number of steps is proportional to the degree of the polynomials


TAdd (n) 2 O (n)
TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, km s kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms January 17, 2017 3 / 17


Working with polynomials Coefficient based representation

Coefficient based representation


Consider polynomials of degree n 1 (coefficients in array):
A(x ) = a0 + a1 x + a2 + : : : + an
x2 1x
n 1

B (x ) = b0 + b1 x + b2 x 2 + : : : + bn 1x
n 1

Coefficients are often real numbers (float)


Common operations on these are:
Addition
Evaluation at a given set of points
Multiplication
Addition is easily done:
A(x ) + B (x ) =
(a0 + b0) + (a1 + b1)x + (a2 + b2)x 2 + : : : + (an 1 + bn 1)x n 1

Number of steps is proportional to the degree of the polynomials


TAdd (n) 2 O (n)
TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, km s kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms January 17, 2017 3 / 17


Working with polynomials Coefficient based representation

Coefficient based representation


Consider polynomials of degree n 1 (coefficients in array):
A(x ) = a0 + a1 x + a2 + : : : + an
x2 1x
n 1

B (x ) = b0 + b1 x + b2 x 2 + : : : + bn 1x
n 1

Coefficients are often real numbers (float)


Common operations on these are:
Addition
Evaluation at a given set of points
Multiplication
Addition is easily done:
A(x ) + B (x ) =
(a0 + b0) + (a1 + b1)x + (a2 + b2)x 2 + : : : + (an 1 + bn 1)x n 1

Number of steps is proportional to the degree of the polynomials


TAdd (n) 2 O (n)
TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, km s kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms January 17, 2017 3 / 17


Working with polynomials Coefficient based representation

Polynomial evaluation
Given A(x ) = a0 + a1 x + a2 x 2 + : : : + an 1 x n 1 , how do we
evaluate it at a given value of x?
n ( n 1)
Direct evaluation would involve multiplications and n 1
2
additions, so may be done in O (n2 ) steps
Factorisation helps to reduce the number of multiplications
A(x ) = a0 + (a1 + (a2 + : : : + an 1 x ) x    ) x [Horners method]
Time complexity of this scheme is: O (n), actually (n)
float horner(float *A, int deg, float x) f
float sum = A[deg]; // an 1 is in A[deg]
while (deg) f
sum *= x; deg--;
sum += A[deg];
g // a0 is in A[0]
return sum; OF
TECHNO
LO

g
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, km s kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms January 17, 2017 4 / 17


Working with polynomials Coefficient based representation

Polynomial evaluation
Given A(x ) = a0 + a1 x + a2 x 2 + : : : + an 1 x n 1 , how do we
evaluate it at a given value of x?
n ( n 1)
Direct evaluation would involve multiplications and n 1
2
additions, so may be done in O (n2 ) steps
Factorisation helps to reduce the number of multiplications
A(x ) = a0 + (a1 + (a2 + : : : + an 1 x ) x    ) x [Horners method]
Time complexity of this scheme is: O (n), actually (n)
float horner(float *A, int deg, float x) f
float sum = A[deg]; // an 1 is in A[deg]
while (deg) f
sum *= x; deg--;
sum += A[deg];
g // a0 is in A[0]
return sum; OF
TECHNO
LO

g
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, km s kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms January 17, 2017 4 / 17


Working with polynomials Coefficient based representation

Polynomial evaluation
Given A(x ) = a0 + a1 x + a2 x 2 + : : : + an 1 x n 1 , how do we
evaluate it at a given value of x?
n ( n 1)
Direct evaluation would involve multiplications and n 1
2
additions, so may be done in O (n2 ) steps
Factorisation helps to reduce the number of multiplications
A(x ) = a0 + (a1 + (a2 + : : : + an 1 x ) x    ) x [Horners method]
Time complexity of this scheme is: O (n), actually (n)
float horner(float *A, int deg, float x) f
float sum = A[deg]; // an 1 is in A[deg]
while (deg) f
sum *= x; deg--;
sum += A[deg];
g // a0 is in A[0]
return sum; OF
TECHNO
LO

g
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, km s kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms January 17, 2017 4 / 17


Working with polynomials Coefficient based representation

Polynomial evaluation
Given A(x ) = a0 + a1 x + a2 x 2 + : : : + an 1 x n 1 , how do we
evaluate it at a given value of x?
n ( n 1)
Direct evaluation would involve multiplications and n 1
2
additions, so may be done in O (n2 ) steps
Factorisation helps to reduce the number of multiplications
A(x ) = a0 + (a1 + (a2 + : : : + an 1 x ) x    ) x [Horners method]
Time complexity of this scheme is: O (n), actually (n)
float horner(float *A, int deg, float x) f
float sum = A[deg]; // an 1 is in A[deg]
while (deg) f
sum *= x; deg--;
sum += A[deg];
g // a0 is in A[0]
return sum; OF
TECHNO
LO

g
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, km s kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms January 17, 2017 4 / 17


Working with polynomials Multi-point polynomial evaluation

Multi-point polynomial evaluation


Evaluation of A(x ) at points xi , 0  i < n may be represented as
2 3 2 32 3
y0 1 x0 x02 : : : x0n 1 a0
6 y1 7 6 1 x1 x12 : : : x1n 1 7 6
7 6 a1
7
7=6 . .
6 7 6 7
6 . .. .. .. 76 . 7
4 .. 5 4 .. .. . . . 5 4 .
. 5
yn 1 1 xn 1 xn2 1 : : : xnn 11 an 1
This may be represented more compactly as y = V(x )a, V being
the Vandermonde matrix in x
Can evaluate y in (n2 ) time by n applications of Horners method
We may reduce our effort to compute A(1) A( 1) as follows:
AE (x ) = a0 + a2 x + a4 x 2 + : : :
AO (x ) = a1 + a3 x + a5 x 2 + : : :
A(x ) = AE (x 2 ) + xAO (x 2 )
A(1) = AE (1) + AO (1)
A( 1) = AE (1) AO (1) TE
OF
TECHNO
LO

GY
ITU
IAN INST

KH
ARAGPUR
IND
Evaluation is achieved at two points at almost half the effort
 

19 5 1

yog, km s kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms January 17, 2017 5 / 17


Working with polynomials Multi-point polynomial evaluation

Multi-point polynomial evaluation


Evaluation of A(x ) at points xi , 0  i < n may be represented as
2 3 2 32 3
y0 1 x0 x02 : : : x0n 1 a0
6 y1 7 6 1 x1 x12 : : : x1n 1 7 6
7 6 a1
7
7=6 . .
6 7 6 7
6 . .. .. .. 76 . 7
4 .. 5 4 .. .. . . . 5 4 .
. 5
yn 1 1 xn 1 xn2 1 : : : xnn 11 an 1
This may be represented more compactly as y = V(x )a, V being
the Vandermonde matrix in x
Can evaluate y in (n2 ) time by n applications of Horners method
We may reduce our effort to compute A(1) A( 1) as follows:
AE (x ) = a0 + a2 x + a4 x 2 + : : :
AO (x ) = a1 + a3 x + a5 x 2 + : : :
A(x ) = AE (x 2 ) + xAO (x 2 )
A(1) = AE (1) + AO (1)
A( 1) = AE (1) AO (1) TE
OF
TECHNO
LO

GY
ITU
IAN INST

KH
ARAGPUR
IND
Evaluation is achieved at two points at almost half the effort
 

19 5 1

yog, km s kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms January 17, 2017 5 / 17


Working with polynomials Multi-point polynomial evaluation

Multi-point polynomial evaluation (contd.)


Note that A(1), A({), A( 1) A( {) may be computed as follows:
A(1) = AE (1) + AO (1)
A( 1) = AE (1) AO (1)
A({) = AE ( 1) + {AO ( 1)
A( {) = AE ( 1) {AO ( 1)
Further, AE (1) and AE ( 1) may be recursively computed in terms
of AEE (1) and AEO (1)
Similarly, AO (1) and AO ( 1) may be recursively computed in
terms of AOE (1) and AOO (1)
This suggests that if n = 2d and we want to evaluate A(x ) of
degree n 1 at suitably chosen n points, we might be able to
develop a divide
(
and conquer strategy
a   n=1
So, T (n) =
2T
n
2
+ bn + c n > 1; n = 2d ; d  0
Asymptotic bound: T (n) 2 (n lg n)
TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, km s kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms January 17, 2017 6 / 17


Working with polynomials Multi-point polynomial evaluation

Multi-point polynomial evaluation (contd.)


Note that A(1), A({), A( 1) A( {) may be computed as follows:
A(1) = AE (1) + AO (1)
A( 1) = AE (1) AO (1)
A({) = AE ( 1) + {AO ( 1)
A( {) = AE ( 1) {AO ( 1)
Further, AE (1) and AE ( 1) may be recursively computed in terms
of AEE (1) and AEO (1)
Similarly, AO (1) and AO ( 1) may be recursively computed in
terms of AOE (1) and AOO (1)
This suggests that if n = 2d and we want to evaluate A(x ) of
degree n 1 at suitably chosen n points, we might be able to
develop a divide
(
and conquer strategy
a   n=1
So, T (n) =
2T
n
2
+ bn + c n > 1; n = 2d ; d  0
Asymptotic bound: T (n) 2 (n lg n)
TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, km s kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms January 17, 2017 6 / 17


Working with polynomials Multi-point polynomial evaluation

Multi-point polynomial evaluation (contd.)


Note that A(1), A({), A( 1) A( {) may be computed as follows:
A(1) = AE (1) + AO (1)
A( 1) = AE (1) AO (1)
A({) = AE ( 1) + {AO ( 1)
A( {) = AE ( 1) {AO ( 1)
Further, AE (1) and AE ( 1) may be recursively computed in terms
of AEE (1) and AEO (1)
Similarly, AO (1) and AO ( 1) may be recursively computed in
terms of AOE (1) and AOO (1)
This suggests that if n = 2d and we want to evaluate A(x ) of
degree n 1 at suitably chosen n points, we might be able to
develop a divide
(
and conquer strategy
a   n=1
So, T (n) =
2T
n
2
+ bn + c n > 1; n = 2d ; d  0
Asymptotic bound: T (n) 2 (n lg n)
TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, km s kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms January 17, 2017 6 / 17


Working with polynomials Solving T (n) = 2T (n=2) + bn + c

Solving T (n) = 2T (n=2) + bn + c


(
a   n=1
T (n) =
2T
n
2
+ bn + c n > 1; n = 2d ; d 0
8.0 b  8:0  1:0 + c  1:0

4.0 4.0 b  4:0  2:0 + c  2:0

2.0 2.0 2.0 2.0 b  2:0  4:0 + c  4:0

1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 a  8:0

Total: 8  3  b + 7  c + 8  a
If n = 2d , summation is: 2d (db + a + c ) c
Asymptotic bound: T (n) 2 (n lg n)
TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, km s kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms January 17, 2017 7 / 17


Working with polynomials Solving T (n) = 2T (n=2) + bn + c

Solving T (n) = 2T (n=2) + bn + c


(
a   n=1
T (n) =
2T
n
2
+ bn + c n > 1; n = 2d ; d 0
8.0 b  8:0  1:0 + c  1:0

4.0 4.0 b  4:0  2:0 + c  2:0

2.0 2.0 2.0 2.0 b  2:0  4:0 + c  4:0

1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 a  8:0

Total: 8  3  b + 7  c + 8  a
If n = 2d , summation is: 2d (db + a + c ) c
Asymptotic bound: T (n) 2 (n lg n)
TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, km s kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms January 17, 2017 7 / 17


Working with polynomials Solving T (n) = 2T (n=2) + bn + c

Solving T (n) = 2T (n=2) + bn + c


(
a   n=1
T (n) =
2T
n
2
+ bn + c n > 1; n = 2d ; d 0
8.0 b  8:0  1:0 + c  1:0

4.0 4.0 b  4:0  2:0 + c  2:0

2.0 2.0 2.0 2.0 b  2:0  4:0 + c  4:0

1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 a  8:0

Total: 8  3  b + 7  c + 8  a
If n = 2d , summation is: 2d (db + a + c ) c
Asymptotic bound: T (n) 2 (n lg n)
TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, km s kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms January 17, 2017 7 / 17


Working with polynomials Solving T (n) = 2T (n=2) + bn + c

Multi-point polynomial evaluation by DC

Note that 1; i ; 1; i are the four roots of unity:


40 , 41 , 42 , 43
We want to compute A(x ) of degree 7 at 8 points: 80 ; 81 ; : : : ; 87
= =
 1 82
4
83 81
84
42 40 80
O < O <
85 87
43 86
0  
Eval AE (x ) and AO (x ) at 82 1 , . . . , 82 3  40 , 41 , . . . , 43
82 ,
Note, A(80 ), A(82 ), A(84 ) and A(86 ) already evaluated as A(1),
A({), A( 1) and A( {) using AE (40 ),AE (42 ), AO (40 ) and AO (42 )
TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, km s kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms January 17, 2017 8 / 17


Working with polynomials Solving T (n) = 2T (n=2) + bn + c

Multi-point polynomial evaluation by DC

Note that 1; i ; 1; i are the four roots of unity:


40 , 41 , 42 , 43
We want to compute A(x ) of degree 7 at 8 points: 80 ; 81 ; : : : ; 87
= =
 1 82
4
83 81
84
42 40 80
O < O <
85 87
43 86
0  
Eval AE (x ) and AO (x ) at 82 1 , . . . , 82 3  40 , 41 , . . . , 43
82 ,
Note, A(80 ), A(82 ), A(84 ) and A(86 ) already evaluated as A(1),
A({), A( 1) and A( {) using AE (40 ),AE (42 ), AO (40 ) and AO (42 )
TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, km s kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms January 17, 2017 8 / 17


Working with polynomials Solving T (n) = 2T (n=2) + bn + c

Multi-point polynomial evaluating (contd.)


= =
82 41
83 81
84
80 42 40
O < O <
85 87
 6
8 43
A(81 )= AE (82 ) + 8 AO (82 )
= AE (41 ) + 8 AO (41 )
2 2
A(8 ) =
5 AE ( 85 ) + 85 AO ( 85 )
= AE (88+2 ) + 85 AO (88+2 )
= AE (82 ) + 85 AO (82 )
= AE (41 ) 8 AO (41 )
TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, km s kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms January 17, 2017 9 / 17


Working with polynomials Solving T (n) = 2T (n=2) + bn + c

Multi-point polynomial evaluating (contd.)


= =
82 41
83 81
84
80 42 40
O < O <
85 87
 6
8 43
A(81 )= AE (82 ) + 8 AO (82 )
= AE (41 ) + 8 AO (41 )
2 2
A(8 ) =
5 AE ( 85 ) + 85 AO ( 85 )
= AE (88+2 ) + 85 AO (88+2 )
= AE (82 ) + 85 AO (82 )
= AE (41 ) 8 AO (41 )
TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, km s kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms January 17, 2017 9 / 17


Working with polynomials Multi-point polynomial evaluation by DC

Multi-point polynomial evaluation by DC


= =
 2
41
83 8
81
84
80 42 40
O < O <
85 87
86 43
2 2
A(83 )= AE ( 83 ) + 83 AO ( 83 )
= AE (86 ) + 83 AO (86 )
 
= AE (43) + 83AO (43)
A(8 ) = AE ( 87 ) + 87 AO ( 87 )
7 2 2

= AE (88+6 ) + 87 AO (88+6 )


= AE (86 ) + 87 AO (86 ) = AE (43) 83AO (43) TE
OF
TECHNO
LO

GY
ITU
IAN INST

KH
ARAGPUR
Divde and conquer works for n=8 (8 points)

IND
 

19 5 1

yog, km s kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms January 17, 2017 10 / 17


Working with polynomials Multi-point polynomial evaluation by DC

Multi-point polynomial evaluation by DC


= =
 2
41
83 8
81
84
80 42 40
O < O <
85 87
86 43
2 2
A(83 )= AE ( 83 ) + 83 AO ( 83 )
= AE (86 ) + 83 AO (86 )
 
= AE (43) + 83AO (43)
A(8 ) = AE ( 87 ) + 87 AO ( 87 )
7 2 2

= AE (88+6 ) + 87 AO (88+6 )


= AE (86 ) + 87 AO (86 ) = AE (43) 83AO (43) TE
OF
TECHNO
LO

GY
ITU
IAN INST

KH
ARAGPUR
Divde and conquer works for n=8 (8 points)

IND
 

19 5 1

yog, km s kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms January 17, 2017 10 / 17


Working with polynomials Multi-point polynomial evaluation by DC

Multi-point polynomial evaluation by DC (contd.)


2
Let 2d = e 2d
i
; in general a primitive root of x 2
d
1 = 0, so that
= e , gcd(r ; 2d ) = 1, so r is odd (r = 2k + 1)
2 r
2d 2d
i

  2  2 d 1
 d 1

2 = e = e = e 2k 1 i = e2ki ei  = 1
2 r 2 (2k +1)
2 d 1 i i ( + )
2d 2d
d

 i 
Also, gcd(r ; 2d 1 ) = 1, so 22 = e
2 r
is a primitive 2d 1 -th root
d
2d 1

of unity 
     
A 2kd = AE  kd 2 + 2kd AO 2kd 2 ; k = 0; : : : ; 2d
 2 
1 1
k 
= AE 22d ) + 2kd AO ( 22d
k

= AE (2kd 1 ) + 2kd AO (2kd 1 )


  
   2  2 
A  k +2d
2d
1
= AE 2d

k +2d 1 k +2d 1
2d
AO+  k +2d 1
2d
     
= AE 2d
2 k 2d
2d + 

k 2d 1 A
d
2 2 d O  2 k 2d
2d 2

d

= ( )  ( )
TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
AE 2kd 1 k A k

ARAGPUR
IND
 

2d O 2d 1
19 5 1

yog, km s kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms January 17, 2017 11 / 17


Working with polynomials Multi-point polynomial evaluation by DC

Multi-point polynomial evaluation by DC (contd.)


2
Let 2d = e 2d
i
; in general a primitive root of x 2
d
1 = 0, so that
= e , gcd(r ; 2d ) = 1, so r is odd (r = 2k + 1)
2 r
2d 2d
i

  2  2 d 1
 d 1

2 = e = e = e 2k 1 i = e2ki ei  = 1
2 r 2 (2k +1)
2 d 1 i i ( + )
2d 2d
d

 i 
Also, gcd(r ; 2d 1 ) = 1, so 22 = e
2 r
is a primitive 2d 1 -th root
d
2d 1

of unity 
     
A 2kd = AE  kd 2 + 2kd AO 2kd 2 ; k = 0; : : : ; 2d
 2 
1 1
k 
= AE 22d ) + 2kd AO ( 22d
k

= AE (2kd 1 ) + 2kd AO (2kd 1 )


  
   2  2 
A  k +2d
2d
1
= AE 2d

k +2d 1 k +2d 1
2d
AO+  k +2d 1
2d
     
= AE 2d
2 k 2d
2d + 

k 2d 1 A
d
2 2 d O  2 k 2d
2d 2

d

= ( )  ( )
TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
AE 2kd 1 k A k

ARAGPUR
IND
 

2d O 2d 1
19 5 1

yog, km s kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms January 17, 2017 11 / 17


Working with polynomials Multi-point polynomial evaluation by DC

Multi-point polynomial evaluation by DC (contd.)


2
Let 2d = e 2d
i
; in general a primitive root of x 2
d
1 = 0, so that
= e , gcd(r ; 2d ) = 1, so r is odd (r = 2k + 1)
2 r
2d 2d
i

  2  2 d 1
 d 1

2 = e = e = e 2k 1 i = e2ki ei  = 1
2 r 2 (2k +1)
2 d 1 i i ( + )
2d 2d
d

 i 
Also, gcd(r ; 2d 1 ) = 1, so 22 = e
2 r
is a primitive 2d 1 -th root
d
2d 1

of unity 
     
A 2kd = AE  kd 2 + 2kd AO 2kd 2 ; k = 0; : : : ; 2d
 2 
1 1
k 
= AE 22d ) + 2kd AO ( 22d
k

= AE (2kd 1 ) + 2kd AO (2kd 1 )


  
   2  2 
A  k +2d
2d
1
= AE 2d

k +2d 1 k +2d 1
2d
AO+  k +2d 1
2d
     
= AE 2d
2 k 2d
2d + 

k 2d 1 A
d
2 2 d O  2 k 2d
2d 2

d

= ( )  ( )
TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
AE 2kd 1 k A k

ARAGPUR
IND
 

2d O 2d 1
19 5 1

yog, km s kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms January 17, 2017 11 / 17


Working with polynomials Multi-point polynomial evaluation by DC

Multi-point polynomial evaluation by DC (contd.)


2
Let 2d = e 2d
i
; in general a primitive root of x 2
d
1 = 0, so that
= e , gcd(r ; 2d ) = 1, so r is odd (r = 2k + 1)
2 r
2d 2d
i

  2  2 d 1
 d 1

2 = e = e = e 2k 1 i = e2ki ei  = 1
2 r 2 (2k +1)
2 d 1 i i ( + )
2d 2d
d

 i 
Also, gcd(r ; 2d 1 ) = 1, so 22 = e
2 r
is a primitive 2d 1 -th root
d
2d 1

of unity 
     
A 2kd = AE  kd 2 + 2kd AO 2kd 2 ; k = 0; : : : ; 2d
 2 
1 1
k 
= AE 22d ) + 2kd AO ( 22d
k

= AE (2kd 1 ) + 2kd AO (2kd 1 )


  
   2  2 
A  k +2d
2d
1
= AE 2d

k +2d 1 k +2d 1
2d
AO+  k +2d 1
2d
     
= AE 2d
2 k 2d
2d + 

k 2d 1 A
d
2 2 d O  2 k 2d
2d 2

d

= ( )  ( )
TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
AE 2kd 1 k A k

ARAGPUR
IND
 

2d O 2d 1
19 5 1

yog, km s kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms January 17, 2017 11 / 17


Working with polynomials Multi-point polynomial evaluation by DC

Multi-point polynomial evaluation by DC (contd.)


2
Let 2d = e 2d
i
; in general a primitive root of x 2
d
1 = 0, so that
= e , gcd(r ; 2d ) = 1, so r is odd (r = 2k + 1)
2 r
2d 2d
i

  2  2 d 1
 d 1

2 = e = e = e 2k 1 i = e2ki ei  = 1
2 r 2 (2k +1)
2 d 1 i i ( + )
2d 2d
d

 i 
Also, gcd(r ; 2d 1 ) = 1, so 22 = e
2 r
is a primitive 2d 1 -th root
d
2d 1

of unity 
     
A 2kd = AE  kd 2 + 2kd AO 2kd 2 ; k = 0; : : : ; 2d
 2 
1 1
k 
= AE 22d ) + 2kd AO ( 22d
k

= AE (2kd 1 ) + 2kd AO (2kd 1 )


  
   2  2 
A  k +2d
2d
1
= AE 2d

k +2d 1 k +2d 1
2d
AO+  k +2d 1
2d
     
= AE 2d
2 k 2d
2d + 

k 2d 1 A
d
2 2 d O  2 k 2d
2d 2

d

= ( )  ( )
TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
AE 2kd 1 k A k

ARAGPUR
IND
 

2d O 2d 1
19 5 1

yog, km s kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms January 17, 2017 11 / 17


Working with polynomials Multi-point polynomial evaluation by DC

Multi-point polynomial evaluation by DC (contd.)

When d = 0, the coefficients may be directly returned (base case)


Evaluation of A(x ) at points 2i , 0  i < 2d may be represented as
2 3
d
 d
2
y0
3
6
1  0
2d
 0 2
2d
::: 20d 2 1
7
2
a0
3
6   d 76
6 y1 7 6 1  1  1 2 ::: 21d 2 1
76 a1 7
6
6
4
..
7
7
5
= 6
6 .. ..
2d
..
2d
.. .. 76
74 ..
7
7
5
. 6 . . . . . 7 .
4  2  2d 1 5
y2d a2d
22d 22d ::: 22d
d 1 d 1 d 1
1 1 1

This may be represented more compactly as y = F2d a, F2d being


the Fourier transform matrix in 2d
FFT was invented by Carl Friedrich Gauss
Re-invented and popularised after 160 years in 1965 by James
William Cooley and John Tukey TE
OF
TECHNO
LO

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, km s kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms January 17, 2017 12 / 17


Working with polynomials Multi-point polynomial evaluation by DC

Multi-point polynomial evaluation by DC (contd.)


fft(int n, complex A[], complex F[]) f
int j, k, sft=n/2; complex omegak, omega;
if (n == 1) f F[0]=A[0]; return g // base case
complex E[n/2], O[n/2], EF[n/2], OF[n/2];
for (j=k=0; k<n; j++,k+=2) f
E[j]=A[k]; O[j]=A[k+1]; // separate out odd/even
g
fft(n/2, E, EF); // solve sub-problem recursively
fft(n/2, O, OF); // solve sub-problem recursively
2
omegak = omega = e n i ; // use Eulers formula
for (k=0; k<n/2; k++, omegak*=omega) f // with A(nk=2 )
F[k] = EF[k] + omegak*OF[k]; // compute A(nk )
F[k+sft] = EF[k] - omegak*OF[k]; // and A(n )
k +n=2

g
g
TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, km s kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms January 17, 2017 13 / 17


Working with polynomials Divide and Conquer Strategy

Divide and Conquer Strategy

1 Given a problem, identify a small number of smaller subproblems


of the same type and of similarly sizes
2 Solve each subproblem recursively (the smallest possible size of a
subproblem is a base-case)
3 Combine these solutions into a solution for the main problem
A few examples:
Polynomial multiplication
Merge sort
Quick sort
Maximum Sum Subarray
Matrix Multiplication OF
TECHNO
LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, km s kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms January 17, 2017 14 / 17


Working with polynomials Polynomial multiplication

Polynomial multiplication
P2 P
C (x ) = A(x )B (x ) = =
2n
ci xi , ci aj bi j
i =0  
0 j ;i j n 1
[Note the convolution of the coefficients]
Time complexity of this scheme is: O (n2 ), actually (n2 )
float polyMul(float *A, *B, *C, int deg) f
int j, k;
for (j=0; j<=2*deg-2; j++) C[j] = 0;
for (j=0; j<=deg-1; j++)
for (k=0; k<=deg-1; k++)
C[j+k] += A[j] * B[k];
g
Can we do better than (n2 ), how could we do that?
Since the time grows with the degree, can we compute the TE
OF
TECHNO
LO

GY
ITU
product multiplying smaller polynomials?

IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, km s kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms January 17, 2017 15 / 17


Working with polynomials Polynomial multiplication

Polynomial multiplication
P2 P
C (x ) = A(x )B (x ) = =
2n
ci xi , ci aj bi j
i =0  
0 j ;i j n 1
[Note the convolution of the coefficients]
Time complexity of this scheme is: O (n2 ), actually (n2 )
float polyMul(float *A, *B, *C, int deg) f
int j, k;
for (j=0; j<=2*deg-2; j++) C[j] = 0;
for (j=0; j<=deg-1; j++)
for (k=0; k<=deg-1; k++)
C[j+k] += A[j] * B[k];
g
Can we do better than (n2 ), how could we do that?
Since the time grows with the degree, can we compute the TE
OF
TECHNO
LO

GY
ITU
product multiplying smaller polynomials?

IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, km s kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms January 17, 2017 15 / 17


Working with polynomials Polynomial multiplication

Polynomial multiplication
P2 P
C (x ) = A(x )B (x ) = =
2n
ci xi , ci aj bi j
i =0  
0 j ;i j n 1
[Note the convolution of the coefficients]
Time complexity of this scheme is: O (n2 ), actually (n2 )
float polyMul(float *A, *B, *C, int deg) f
int j, k;
for (j=0; j<=2*deg-2; j++) C[j] = 0;
for (j=0; j<=deg-1; j++)
for (k=0; k<=deg-1; k++)
C[j+k] += A[j] * B[k];
g
Can we do better than (n2 ), how could we do that?
Since the time grows with the degree, can we compute the TE
OF
TECHNO
LO

GY
ITU
product multiplying smaller polynomials?

IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, km s kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms January 17, 2017 15 / 17


Working with polynomials Polynomial multiplication

Polynomial multiplication
P2 P
C (x ) = A(x )B (x ) = =
2n
ci xi , ci aj bi j
i =0  
0 j ;i j n 1
[Note the convolution of the coefficients]
Time complexity of this scheme is: O (n2 ), actually (n2 )
float polyMul(float *A, *B, *C, int deg) f
int j, k;
for (j=0; j<=2*deg-2; j++) C[j] = 0;
for (j=0; j<=deg-1; j++)
for (k=0; k<=deg-1; k++)
C[j+k] += A[j] * B[k];
g
Can we do better than (n2 ), how could we do that?
Since the time grows with the degree, can we compute the TE
OF
TECHNO
LO

GY
ITU
product multiplying smaller polynomials?

IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, km s kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms January 17, 2017 15 / 17


Working with polynomials Polynomial multiplication by DC (Karatsuba)

Polynomial multiplication by DC (Karatsuba)

A(x ) = AL (x ) + x t AH (x ), AL (x ) = a0 + a1 x + : : : + at 1 x t 1,

AH (x ) = at + at +1 x + : : : + an 1 x n 1 t
jnk
t= , so that both AL (x ) and AH (x ) are nearly equal
2
Ideally, n is a power of 2, n = 2d ; d  0
Similarly, B (x ) = BL + x t BH , where BL  BL(x ) and BH  BH (x )
C (x ) = A(x )B (x ) = x 2t AH BH + (AH BL + ALBH ) + ALBL
xt
Smaller polynomials recursively multiplied until the degree
reduces to 0, when the coefficients are directly multiplied
(
a [n = 1]
TmulHL (n) = n
4TmulHL
2
+ bn + c [n > 1; n = 2d ; d  0]

Solution (by standard methods): TmulHL (n) 2  n2
TECHNO
OF LO
TE

GY
ITU
No improvement, but why? There are too many sub-problems

IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, km s kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms January 17, 2017 16 / 17


Working with polynomials Polynomial multiplication by DC (Karatsuba)

Polynomial multiplication by DC (Karatsuba)

A(x ) = AL (x ) + x t AH (x ), AL (x ) = a0 + a1 x + : : : + at 1 x t 1,

AH (x ) = at + at +1 x + : : : + an 1 x n 1 t
jnk
t= , so that both AL (x ) and AH (x ) are nearly equal
2
Ideally, n is a power of 2, n = 2d ; d  0
Similarly, B (x ) = BL + x t BH , where BL  BL(x ) and BH  BH (x )
C (x ) = A(x )B (x ) = x 2t AH BH + (AH BL + ALBH ) + ALBL
xt
Smaller polynomials recursively multiplied until the degree
reduces to 0, when the coefficients are directly multiplied
(
a [n = 1]
TmulHL (n) = n
4TmulHL
2
+ bn + c [n > 1; n = 2d ; d  0]

Solution (by standard methods): TmulHL (n) 2  n2
TECHNO
OF LO
TE

GY
ITU
No improvement, but why? There are too many sub-problems

IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, km s kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms January 17, 2017 16 / 17


Working with polynomials Polynomial multiplication by DC (Karatsuba)

Polynomial multiplication by DC (Karatsuba)

A(x ) = AL (x ) + x t AH (x ), AL (x ) = a0 + a1 x + : : : + at 1 x t 1,

AH (x ) = at + at +1 x + : : : + an 1 x n 1 t
jnk
t= , so that both AL (x ) and AH (x ) are nearly equal
2
Ideally, n is a power of 2, n = 2d ; d  0
Similarly, B (x ) = BL + x t BH , where BL  BL(x ) and BH  BH (x )
C (x ) = A(x )B (x ) = x 2t AH BH + (AH BL + ALBH ) + ALBL
xt
Smaller polynomials recursively multiplied until the degree
reduces to 0, when the coefficients are directly multiplied
(
a [n = 1]
TmulHL (n) = n
4TmulHL
2
+ bn + c [n > 1; n = 2d ; d  0]

Solution (by standard methods): TmulHL (n) 2  n2
TECHNO
OF LO
TE

GY
ITU
No improvement, but why? There are too many sub-problems

IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, km s kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms January 17, 2017 16 / 17


Working with polynomials Polynomial multiplication by DC (Karatsuba)

Polynomial multiplication by DC (Karatsuba)

A(x ) = AL (x ) + x t AH (x ), AL (x ) = a0 + a1 x + : : : + at 1 x t 1,

AH (x ) = at + at +1 x + : : : + an 1 x n 1 t
jnk
t= , so that both AL (x ) and AH (x ) are nearly equal
2
Ideally, n is a power of 2, n = 2d ; d  0
Similarly, B (x ) = BL + x t BH , where BL  BL(x ) and BH  BH (x )
C (x ) = A(x )B (x ) = x 2t AH BH + (AH BL + ALBH ) + ALBL
xt
Smaller polynomials recursively multiplied until the degree
reduces to 0, when the coefficients are directly multiplied
(
a [n = 1]
TmulHL (n) = n
4TmulHL
2
+ bn + c [n > 1; n = 2d ; d  0]

Solution (by standard methods): TmulHL (n) 2  n2
TECHNO
OF LO
TE

GY
ITU
No improvement, but why? There are too many sub-problems

IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, km s kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms January 17, 2017 16 / 17


Working with polynomials Polynomial multiplication by DC (Karatsuba)

Polynomial multiplication by DC (Karatsuba)

A(x ) = AL (x ) + x t AH (x ), AL (x ) = a0 + a1 x + : : : + at 1 x t 1,

AH (x ) = at + at +1 x + : : : + an 1 x n 1 t
jnk
t= , so that both AL (x ) and AH (x ) are nearly equal
2
Ideally, n is a power of 2, n = 2d ; d  0
Similarly, B (x ) = BL + x t BH , where BL  BL(x ) and BH  BH (x )
C (x ) = A(x )B (x ) = x 2t AH BH + (AH BL + ALBH ) + ALBL
xt
Smaller polynomials recursively multiplied until the degree
reduces to 0, when the coefficients are directly multiplied
(
a [n = 1]
TmulHL (n) = n
4TmulHL
2
+ bn + c [n > 1; n = 2d ; d  0]

Solution (by standard methods): TmulHL (n) 2  n2
TECHNO
OF LO
TE

GY
ITU
No improvement, but why? There are too many sub-problems

IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, km s kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms January 17, 2017 16 / 17


Working with polynomials Karatsuba multiplication

Karatsuba multiplication

C (x ) = A(x )B (x ) = x 2t AH BH
+ x t (AH BL + ALBH ) + ALBL
AH BL + AL BH = (AH + AL )(BH + BL ) AH BH AL BL
Store and reuse AH BH and AL BL computed earlier
Smaller polynomials recursively multiplied until the degree
reduces to 0, when the coefficients are directly multiplied
(
a [n = 1]
TmulHL (n) = n
0 0
3TmulHL
2
+ b n + c [n > 1; n = 2d ; d  0]
More polynomial additions, so the constants b0 and c 0 are
expected to be somewhat larger than before

Solution (by standard methods): TmulHL (n) 2  nlog2 3 ,
log2 3 = 1:58496 : : :
TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, km s kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms January 17, 2017 17 / 17


Working with polynomials Karatsuba multiplication

Karatsuba multiplication

C (x ) = A(x )B (x ) = x 2t AH BH
+ x t (AH BL + ALBH ) + ALBL
AH BL + AL BH = (AH + AL )(BH + BL ) AH BH AL BL
Store and reuse AH BH and AL BL computed earlier
Smaller polynomials recursively multiplied until the degree
reduces to 0, when the coefficients are directly multiplied
(
a [n = 1]
TmulHL (n) = n
0 0
3TmulHL
2
+ b n + c [n > 1; n = 2d ; d  0]
More polynomial additions, so the constants b0 and c 0 are
expected to be somewhat larger than before

Solution (by standard methods): TmulHL (n) 2  nlog2 3 ,
log2 3 = 1:58496 : : :
TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, km s kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms January 17, 2017 17 / 17


Working with polynomials Karatsuba multiplication

Karatsuba multiplication

C (x ) = A(x )B (x ) = x 2t AH BH
+ x t (AH BL + ALBH ) + ALBL
AH BL + AL BH = (AH + AL )(BH + BL ) AH BH AL BL
Store and reuse AH BH and AL BL computed earlier
Smaller polynomials recursively multiplied until the degree
reduces to 0, when the coefficients are directly multiplied
(
a [n = 1]
TmulHL (n) = n
0 0
3TmulHL
2
+ b n + c [n > 1; n = 2d ; d  0]
More polynomial additions, so the constants b0 and c 0 are
expected to be somewhat larger than before

Solution (by standard methods): TmulHL (n) 2  nlog2 3 ,
log2 3 = 1:58496 : : :
TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, km s kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms January 17, 2017 17 / 17


Working with polynomials Karatsuba multiplication

Karatsuba multiplication

C (x ) = A(x )B (x ) = x 2t AH BH
+ x t (AH BL + ALBH ) + ALBL
AH BL + AL BH = (AH + AL )(BH + BL ) AH BH AL BL
Store and reuse AH BH and AL BL computed earlier
Smaller polynomials recursively multiplied until the degree
reduces to 0, when the coefficients are directly multiplied
(
a [n = 1]
TmulHL (n) = n
0 0
3TmulHL
2
+ b n + c [n > 1; n = 2d ; d  0]
More polynomial additions, so the constants b0 and c 0 are
expected to be somewhat larger than before

Solution (by standard methods): TmulHL (n) 2  nlog2 3 ,
log2 3 = 1:58496 : : :
TECHNO
OF LO
TE

GY
ITU
IAN INST

KH
ARAGPUR
IND
 

19 5 1

yog, km s kOflm^

Chittaranjan Mandal (IIT Kharagpur) Algorithms January 17, 2017 17 / 17