# Polynomial Computation ```Advanced Algebraic Algorithms
on Integers and Polynomials
Analysis of Algorithms
Prepared by
John Reif, Ph.D.
Integer and Polynomial
Computations
a) Newton Iteration: application to division
b) Evaluation and Interpolation via Chinese
Remaindering
Main Lecture Material:
Algorithms on Integers
Extension of Algorithms to Polynomials
Taylor Expansion
(x-x 0 )2
f (x) = f (x 0 ) + (x - x 0 ) f ' (x 0 ) +
f &quot; (x 0 ) + ...
2
Taylor Expansion (cont’d)
• To find root f(x), use Newton
Iteration:
f (x i )
xi +1 =xi -
f '(x i )
• Example:
1
– To find reciprocal of x choose f ( y ) = 1 -
1
y=
find root
x
f ( yi )
yi +1 = yi = yi (2 - yi x)
f '( yi )
xy
Application of Newton Iteration to
Reciprocal of an Integer
• Input integer x, accuracy bound k
• Initialize y0 = 2- n if x has n bits
for i=1 to k do
yi +1 &not; yi (2 - yi x)
output yk where 1 - yk x &pound;
1
2
proof let error e k = 1 - yk x
2k
then e k +1 = 1 - yk +1 x = 1 - xyk (2 - yk x)
2k
= (e k ) = (e 0 ) = 2
2
-2k
1
since e 0 &pound;
2
Application of Newton Iteration to
Reciprocal of an Integer (cont’d)
• Theorem Integer Reciprocal can
be computed to accuracy 2-n in
O(log n) integer mults and
Steven Cooks’s Improvement
(His Harvard Ph.D. Thesis)
• Key Trick: Since 1 - yk x &pound;
1
2
2k
we need only compute yk up to
2k+1 bit accuracy of error on kth stage
• Total Time Cost
log n
c &times; &aring; M (2k +1 ) &pound; O( M (n))
k =0
• Where M(n) is time cost to multiply
two n bit integers
Other Applications of Newton
Iterations on Integers
• O(M(n)) time algorithms:
• Quotient + divisor of integer
division
• Square root
• Sin, cosine, etc.
• Used in practice!
Polynomial Reciprocal
Definition of Polynomial Reciprocal
x 2n-2
• Reciprocal ( P ( x)) =
= polynomial r(x)
P( x)&ucirc;
&euml;
• where r ( x) &times; p( x) = x
2 n-2
+ e ( x)
and (x) has degree &lt; n-1
Algorithm: Reciprocal (P(x))
• Input polynomial P ( x) =
n -1
i
a
x
&aring; i
i =0
degree n-1, n is power of 2
1
 if n  1 then return
else
a0
 r1 (x)  Reciprocal (P1 ( x)) where P1 ( x ) 
 R ( x)  2r1 ( x) x (3/ 2) n2  r1 ( x) 2 P( x)
R( x)
 return r ( x)  n2
x 

n 1
ax
i n / 2
i
i
n
2
Proof of the Reciprocal Algorithm
• Theorem: The Algorithm Correctly
Computes Reciprocal(P(x))
• Proof by induction
1
basis n = 1 &THORN; P( x) = a0 so r ( x) =
a0
n
2
inductive step let P ( x) = P1 ( x) x + P2 ( x)
n
n
where deg (P1 ) = - 1, deg (P2 ) &pound; - 1
2
2
Proof Algorithm Computes
Reciprocal (P(x)) (cont’d)
• By induction hypothesis, if
r1 ( x) = reciprocal ( P1 ( x)) then
r1 ( x) &times; P1 ( x) = x n - 2 + e1 ( x)
n
where e1 ( x) has degree &lt; - 1
2
Proof Algorithm Computes
Reciprocal (P(x)) (cont’d)
• At line  we compute
R( x) &not; 2r1 ( x) x
(3/ 2) n - 2
- r1 ( x) P( x)
2
n
2
since P( x) = P1 ( x) x + P2 ( x)
P( x) R( x) = 2r1 ( x) P1 ( x) x 2 n - 2 + 2r1 ( x) P2 ( x) x (3/ 2) n - 2
&aelig;
&ouml;
- &ccedil; r1 ( x) p1 ( x) x + r1 ( x) p2 ( x) &divide;
&egrave;
&oslash;
n
2
2
Proof Algorithm Computes
Reciprocal (P(x)) (cont’d)
Substituting x
n-2
+e1 (x) for r1 ( x) p1 ( x),
we get
&aelig;
&ouml;
R( x) p( x) = x
- &ccedil; e1 ( x) x + r1 ( x) p2 ( x) &divide;
&egrave;
&oslash;
= x3n - 4 - 0( x 2 n -4 )
R( x)
But r( x) = n - 2
so
x &ucirc;
&euml;
3n-4
n
2
r ( x ) &times; p ( x ) = x 2 n - 2 - o( x n - 2 )
2
Modular Arithmetic
• Assume relatively prime P0, P1, …, Pk-1
k -1
• Let
p = &Otilde; pi
given x, 0 &lt; x &lt; p
i =0
x &not;&frac34;&reg;
( x0 , ..., xk -1 ) where
1-1
xi = x mod pi for i = 0, ..., k -1
Applications to Arithmetic
Compute u op v by computing for i=0,...,k-1
w i = u i op vi mod pi
where u i = u mod pi
vi = v mod pi
op  {+, -, }
• But doesn’t extend to division
(overflow problems)
Super Moduli Computation
• Input p0, p1, …, pk-1, where pi &lt; 2b
• Output Super modular Tree:
Super Moduli Computation (cont’d)
• Time Cost
k 
i

 iM 2b
i 0  2 
 O ( M (k b) log k )
log k
 
Algorithm Residue Computation
k -1
• Input
x, 0 &pound; x &lt; P = &Otilde; Pi
i =0
• Output x0, x1, …, xk-1, when
xi =x mod Pi
i=0, and k-1
• Recursive algorithm
 compute quotient and remainders:
x = q1v1 + r1 , v1 =
( k -1) / 2
&Otilde;
i =0
Pi
k -1
&Otilde;
x = q2 v2 + r2 , v2 =
i=
( k -1)
+1
2
Pi
Algorithm Residue Computation
(cont’d)
 recursively compute
(k - 1)
(2.1) r1 mod Pi for i = 0,...,
2
(k - 1)
(2.2) r2 mod Pi for i =
+ 1,..., k - 1
2
 output for i = 0, 1, …, k-1
(k - 1)
&igrave;
&iuml;&iuml;r1 mod Pi for i &pound; 2
x mod Pi = &iacute;
&iuml;r mod P for i &gt; (k - 1)
i
&iuml;&icirc; 2
2
Time Cost for Residue Computation
• Let D(n) = time cost for integer division
= O(M(n))
• Total Time for input size n = k&middot; b
&aelig;n&ouml;
T (n) = 2T &ccedil; &divide; + 2kD(b) + O(n)
&egrave;2&oslash;
&aelig;n&ouml;
&pound; 2T &ccedil; &divide; + O( M ( n))
&egrave;2&oslash;
&pound; O( M (n) log n)
Proof of Algorithm for Residue
Computation
• Idea of Proof of algorithm
Uses fact:
if x = q v + r and v mod Pi = 0,
then
X mod Pi = r mod Pi
Computation on Polynomials
• Input moduli P0(x), P1(x), …, Pk-1(x)
assume each degree &lt; d
and relatively prime
• Algorithm uses similar Super modular
Tree tre , but using polynomials rather
than integers
Computation on Polynomials (cont’d)
• Output for i=0, …, k-1
Qi(x) = Q(x) mod Pi(x)
Q(x) has degree &lt; kd
• Theorem The Residue Computation
can be done in time O(M(n) log n)
where n = k&middot; d
• Proof Idea use same algorithm as in
integer case
Evaluation of Polynomials by
Residue Computation
• Input polynomial f(x) degree n-1 and
points x0, x1, …, xn-1
 for i=0, …, n-1 let Pi(x) = (x-xi)
 By Residue Algorithm Computer
for
i=0, …, n-1
f(xi) = f(x) mod Pi(x)
 output f(x0), …, f(xn-1)
• Time Cost
O(M(n) log n),
= O(n(log n)2)
Polynomial Interpolation
• Input
evaluation points x0, …, xn-1
values y0, …, yn-1
• Output P(x) where yk = P(xk)
for k=0, …, n-1
Polynomial Interpolation (cont’d)
• Interpolation formula:
n -1
P( x) = &aring; yk ak &Otilde; ( x - xi )
k =0
i&sup1;k
• Where
1
ak =
&Otilde; ( xk - xi )
i&sup1;k
Proof of Polynomial Interpolation
• Proof uses identities:


 ak  ( x  xi )  mod( x  xk )  1
 ik



 ak  ( x  xi )  mod( x  x j )  0 for j  k
 ik

Using Chinese Remaindering for
Integer Interpolation
• Input relatively prime P0, P1, …, Pn-1
and yi ∈ {0,…Pi-1} for i=0,…,n-1
• Problem compute y &lt; P0 P1 … Pn-1
yi = y mod Pi
i=0, …, n-1
s.t.
Using Chinese Remaindering for
Integer Interpolation
• Generalized Interpolation Formula:
n -1
y = &aring; yk ak &Otilde; Pi
k =0
• Where
i&sup1;k
ak = &Otilde; si ,k
and
i&sup1;k
si ,k = ( Pi ) -1 mod Pk
• proof
&igrave;1 j = k
&aelig;
&ouml;
&ccedil; ak &Otilde; Pi &divide; mod Pj = &iacute;0 j &sup1; k
&egrave; i&sup1;k &oslash;
&icirc;
Interpolation
Preconditioned Case assumes
coefficients {ak | k=0, …, n-1} precomputed
• Use Divide &amp; Conquer
n -1
y = &aring; yk ak &Otilde; Pi
k =0
i&sup1;k
&ouml;
&aelig; ( n-1) / 2
&ouml; &aelig; n -1
n -1
n -1
= &ccedil; &aring; yk ak &Otilde; Pi &divide; + &ccedil; &aring; yk ak &Otilde; Pi &divide;
&divide;
&ccedil; k =0
&divide; &ccedil; ( n -1)
i
&sup1;
k
i
&sup1;
k
&ccedil;
&divide; &ccedil; k = +1
&divide;
i
=
0
i
=
0
&egrave;
&oslash; &egrave; 2
&oslash;
Preconditioned Interpolation
(cont’d)
&eacute;
&ugrave;
( n -1)
&uacute;
&aelig; ( n -1) / 2 &ouml; &ecirc; n -1
= &ccedil; &Otilde; Pi &divide; &ecirc; &aring; yk ak &Otilde; Pi &uacute;
&uacute;
i&sup1;k
&egrave; i =0
&oslash; &ecirc; k = ( n -1) +1
( n -1)
&ecirc; 2
&uacute;
i=
+1
2
&euml;
&ucirc;
&aelig; n -1
&ouml; &eacute; ( n -1) / 2
&ugrave;
( n -1) / 2
+ &ccedil; &Otilde; Pi &divide; &ecirc; &aring; yk ak &Otilde; Pi &uacute;
&ccedil; ( n -1)
&divide; &ecirc; k =0
&uacute;
i
&sup1;
k
&ccedil; i = +1 &divide; &ecirc;
&uacute;&ucirc;
i =0
&egrave; 2
&oslash;&euml;
Time Cost for Preconditioned
Interpolation
&aelig;n&ouml;
&aelig;n&ouml;
T (n) = 2T &ccedil; &divide; + 2M &ccedil; &divide;
&egrave;2&oslash;
&egrave;2&oslash;
= O( M (n) log n)
• Assuming {a0, …, an-1} precomputed
Precomputation of {a0, …, an-1}
n -1
1) Compute
P = &Otilde; Pi
i =0
2) Compute bk where
bkPk = P mod (Pk)2
by Residue Computation O(M(n) log n)
3) Compute ak = (bk)-1 mod Pk by Extended
GCD algorithm
Proof of Precomputation of {a0, …, an-1}
• proof
since b k Pk = P mod (Pk )
2
then P = d (Pk ) + b k Pk
so
&Otilde;P = d P + b
i
k
i&sup1;k
k
so b k = &Otilde; Pi mod Pk
i&sup1;k
2
Precomputation of {a0, …, an-1} for
Polynomial Interpolations
•
Here Pi = (x-xi) for i=0, …, n-1
n-1
Q(x)
bk 
where Q(x)=  (x-x j )
(x - x k )
j=0
Q(x) - Q(x k )
=
since Q(x k ) = 0
(x - x k )
d Q(x) 
=
dx  x = x
k
 reduces to multipoint evaluation of
derivative of Q(x) O(M(n) log n) time!
Conclusion
• Polynomial and Integer Computations use
similar divide and conquer techniques to
solve:
1. Multiplication
2. Division
3. Interpolation and evaluation
• Open Problem
Reduce from time O(M(n)log n) to O(M(n))
Newton Iteration and
Polynomial Computation
Analysis of Algorithms
Prepared by
John Reif, Ph.D.
```