Giới thiệu về các thuật toán -
Trang 16.006 Introduction to Algorithms
Spring 2008
For information about citing these materials or our Terms of Use, visit: htto://ocw.mit.edu/terms.
Trang 2Lecture 24: Numerics II
Lecture Overview
e Review:
— high precision arithmetic
— multiplication
e Division
— Algorithm
— Error Analysis
e Termination
Review:
Want millionth digit of 2:
[v2 - 102 đ = 109
Compute |./a]| via Newton’s Method
xo = 1 (initial guess) Xi+1 = ae Tự — division!
Converges quadratically; ÿ correct digits doubles each step
Multiplication:
1 Naive Divide & Conquer method: 6(d?) time
2 Karatsuba: 0(d198: 3) — 60(d1584 )
3 Toom-Cook generalizes Karatsuba (break into k > 2 parts )
T(d) = 5T(d/3) + 0(d) = 0 (a?) = 9 (a4)
4 Schonhage-Strassen - almost linear! 6(d lgdlglgd) using FFT All of these are in gmpy package
5 Furer(2007): @ (n log n20 los" ”) where log” ?› is iterated logarithm ‡ times log needs
to be applied to get a number that is less than or equal to 1
Trang 3High Precision Division
a
We want high precision rep of 3
1
e Compute high-precision rep of ọ first
e High-precision rep of ; means lz! where R is large value s.t it is easy to divide by
R
Ex: R = 2" for binary representations
Division
Rk Newton’s Method for computing ?
f(z) = = RB (zero atx = 2)
—Ì
¿-9
Xi+1 — Xi — Fx) = Xi — —
1 6b by;? > multiply
2 2
Example
R 2'© 65536
t — = — = — =13107.2
216
Try initial guess —— = 21
Xo = 2'* = 16384
Xxi = 2- (16384) — 5(16384)?/65536 = 12288 x2 = 2- (12288) — 5(12288)?/65536 = 13056 x3 = 2- (13056) — 5(13056)?/65536 = 13107
Trang 4Error Analysis
Yur = XA R Assume x; = — (1+ €;) b
= 2 (1+4) 2— 7) —ms | x} n1) (1+ 4) Gta
= (2+ 24) = (Lt 26+ i ))
= x=ø7)=T (+) where €j41 = —e;"
Quadratic convergence; { digits doubles at each step
Therefore complexity of division = complexity of multiplication
Termination
Iteration: yj;41 = it Lebel)
Do floors hurt? Does program terminate?
Iteration is
Nit -@
Nit
= n7 wherey = 5 + Sand0<7<1
Since 5 > Vab, 5 > Va, so subtracting + always leaves us > |vwœ| This won't stay stuck above ife; <1 (good initial guess)