You are on page 1of 5

Lecture 12

Numerics II

6.006 Fall 2011

Lecture 12: Numerics II


Lecture Overview
Review:
high precision arithmetic
multiplication
Division
Algorithm
Error Analysis
Termination

Review:
Want millionth digit of

2:

b 2 102d c

d = 106

Compute b ac via Newtons Method


0 = 1 (initial guess)
i + a/i
i+1 =
division!
2

Error Analysis of Newtons Method


Suppose Xn =
Then,

a (1 + n ) n may be + or -

Xn+1 =
=

Xn + a/Xn
2
a(1 + n ) +


p
(a)

a
a(1+n )

2
(1 + n ) +

1
(1+n )

2

2 + 2n + n 2
2(1 + n )


p
n 2
=
(a) 1 +
2(1 + n )
p
=
(a)

Lecture 12

Numerics II

6.006 Fall 2011

Therefore,
n+1 =

n 2
2(1 + n )

Quadratic convergence, as ] correct digits doubles each step.


Newtons method requires high-precision division. We covered multiplication in
Lecture 12.

Multiplication Algorithms:
1. Naive Divide & Conquer method: (d2 ) time
2. Karatsuba: (dlog2 3 ) = (d1.584... )
3. Toom-Cook generalizes Karatsuba (break into k 2 parts )


T (d) = 5T (d/3) + (d) = dlog3 5 = d1.465...
4. Schnhage-Strassen - almost linear! (d lg d lg lg d) using FFT. All of these are
in gmpy package


5. Furer (2007): n log n 2O(log n) where log n is iterated logarithm. ] times


log needs to be applied to get a number that is less than or equal to 1.

High Precision Division


We want high precision rep of

a
b

Compute high-precision rep of

1
first
b

1
R
High-precision rep of
means b c where R is large value s.t. it is easy to
b
b
divide by R
Ex: R = 2k for binary representations

Lecture 12

Numerics II

6.006 Fall 2011

Division
R
b


1
b
R

zero at x =
x R
b
1
x2


1
b

i
R
f (i )
i 0
= i
f (i )
1/i 2


bi 2 multiply
1
b
i + i 2

= 2i
R easy div
i R

Newtons Method for computing


f (x) =
f 0 (x) =
i+1 =
i+1 =

Example
R
216
65536
=
=
= 13107.2
b
5
5
216
Try initial guess
= 214
4

Want

0 = 214 = 16384
1 = 2 (16384) 5(16384)2 /65536 = 12288
2 = 2 (12288) 5(12288)2 /65536 = 13056
3 = 2 (13056) 5(13056)2 /65536 = 13107

Error Analysis
bi 2
R

R
Assume i = (1 + i )
b
 2
R
b R
= 2 (1 + i )
(1 + i )2
b
R b

R
=
(2 + 2i ) (1 + 2i + i 2 )
b
 R
R
1 i 2 = (1 + i+1 ) where i+1 = i 2
=
b
b

i+1 = 2i

Quadratic convergence; ] digits doubles at each step


One might think that the complexity of division is lg d times the complexity of
multiplication given that we will have lg d multiplications in the lg d iterations required
3

Lecture 12

Numerics II

6.006 Fall 2011

to reach precision d. However, the complexity of division equals the complexity of


multiplication.
To understand this, assume that the complexity of multiplication is (n ) for ndigit numbers, with 1. Division requires multiplication of different-sized numbers
at each iteration. Initially the numbers are small, and then they grow to d digits.
The number of operations in division are:
 
 
d
d

+ c
+ c d < 2 c d
c 1 + c 2 + c 4 + + c
4
2

Complexity of Computing Square Roots


We apply a first level of Newtons method to solve f (x) = x2 a. Each iteration
of this first level1 requires a division. If we set the precision to d digits right from
the beginning, then convergence at the first level will require lg d iterations. This
means the complexity of computing a square root will be (d lg d) if the complexity
of multiplication is (d ), given that we have shown that the complexity of division
is the same as the complexity of multiplication.
However, we can do better, if we recognize that the number of digits of precision
we need at beginning of the first level of Newtons method starts out small and then
grows. If the complexity of a d-digit division is (d ), then a similar summation to
the one above tells us that the complexity of computing square roots is (d ).

Termination
i + ba/i c
c
2
Do floors hurt? Does program terminate? ( and are the fractional parts below.)
Iteration is

Iteration: i+1 = b

i+1 =
=

i +

a
i

2
i +
2

a
i

where =

+ and 0 < 1
2

i + ai

a+b
Since
ab,
a, so subtracting always leaves us b ac. This
2
2
wont stay stuck above if i < 1 (good initial guess).

We are calling this the first level, since Newtons method is used within division, which would
be the second level of applying it when we are computing square roots.

MIT OpenCourseWare
http://ocw.mit.edu

6.006 Introduction to Algorithms


Fall 2011

For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.

You might also like