Exp(x) Smith

advertisement
Exp(x)
Smith
This function is discussed in Efficient Multiple-Precision Evaluation of
Elementary Functions, by David M. Smith Mathematics of Computation 52 (1989) 131
– 134 – locally Smith.PDF
2. Exponential and related functions. Function computations can often be
speeded up by using various identities to reduce the size of the argument prior to
summing
a series and then reversing the reduction at the end. The exponential identity

exp  x   exp  x / 2
k

2k
outer parenthesis are due to Bob
can be used as follows. Compute y = x/2k using a few divide by integer
operations, then
sum the series for exp(y), then do k squarings to recover exp(x). Brent uses this
technique
in [3] to obtain an algorithm with speed O(t 1/2M(t)).
Since the power series for exp(x) consists of terms which are closely related, the
next
term can be obtained from the previous term by one division by a small integer
and one
O(M(t)) operation to get the next power of x. The operations with integers and the
addition of the terms are all O(t), so reducing the number of multiplications is
important.
The direct sum
exp( x)  1  x  x 2 / 2  x3 / 3!  x n / n!
requires (n - 1) multiplications, (n - 1) divisions by an integer, and n additions.”
…
...
To estimate the time for this algorithm, assume that the argument is about 1 in
magnitude, base b arithmetic with t digits is used, and k halvings are done before
the j
sums are computed. The original argument x is assumed to lie in some fixed,
bounded
interval, and the number of terms n needed from a Taylor series is assumed to
be a single precision integer. The value of n is determined by the equation
2 
k
n
 b  t Bob j=n-1
n!
Using Stirling’s approximation for n! provides an approximation for the number of
terms in the series which must be taken:
t log b
n
log t  k log 2
Including the k squarings needed to reverse the argument reduction, the total
work,
W, is estimated by the number of multiplications:
t log b
W
 jk
j  log t  k log 2 
Choosing j and k to minimize W gives
j  t1/ 3 (log b / log 2)1/ 3
k  t1/ 3 (log b / log 2)1/ 3  log t / log 2
Letting j and k be the nearest integers to these values gives an algorithm with
O(t1/3)
multiplications, and it follows that exp(x) can be computed in O(t 1/3M(t)) time.
Smith uses Newton’s method in combination with the exponential to calclulate the
logarithm.
Bob
Smith ignores the ability to subtract log(b) from x. For b = 2, this is implicit in C
Abramowitz and Stegun 4.2.43 (AEXP.for). He also assumes that x is on the order of 1.
The code as he describes it is in tmexps.wpj. N and K are both ~ 22 and nothing much
changes them. The simplicity of the code convinces me to leave it alone.
N and K
The number of terms in the sum is determined by
x/ 2 
k
n
 b t 1
(1)
n!
Stirling’s approximation is
1
1

log(n !)  log  2    n   log  n   n  O /(12n) (Abramowitz and Stegun 6.1.38)
2
2

Using Stirling’s approximation, equation (1) gives k in terms of n.
1
1

n log  x / 2k    t  1 log(b)  log  2    n   log n  n
2
2

1
1

n log x  kn log 2    t  1 log(b)  log  2    n   log n  n
(2)
2
2

1
1
kn log 2   t  1 log(b)  log  2   n log n  log n  n  n log x
2
2
In Bob’s notation b = 214, so that log(b)/log(2) = 14 for all values of t
1
 1

k   t  114 / n    log  2   n log n  log n  n  n log x  /  n log 2  (3)
2
 2

The first term dominates for most values of n so that the total number of operations is
approximately
Ops   n  k  t 2
(4)

  t  114   2
 n  
t




n



Set the derivative with respect to n to zero to find
0  log 2   t  1 log(b) / n 2 
0  n 2 log 2   t  1 log(b)
Or
n
t  1 log(b) / log(2)  t  114
In this approximation, n is independent of x. The ide for the test code is tmexps.wpj. The
results are compared to quad precsion calculator results in TESTexps.OUT. The code is
in MPEXPS.for.
Ln(x)
The approximation of the logarithm benefits from the fact that the multiple
precision exponential exists.
In double precision find
Define
(1)
 DP  log  xDP 
The term MP,DP is DP converted multiple precision
xDP , MP  exp MP  MP , DP 
(2)
RMP  xMP / xDP ,MP  xMP  exp MP ( MP , DP ) (3)

log MP  RMP   log MP  xMP   log MP exp MP   MP , DP 

 log MP  xMP    MP , DP
 MP  RMP  1
(4)
(5)
This last term MP ~ 10 .
log MP  RMP   log 1   MP 
-15

log MP (1   )    1
n 1
n 1

n
n
(6)
 1  x  1 (Jeffrey 2.6.1.1) (7)
For the accuracy required
10 n15
 b  t 1
(8)
n
So that
log n  t  1 log b
n

(9)
15
15
Log(b) = 9.7
For t=31, 32 log b/15 = 20.7, then log(n)/15 = .2 so that the final n=21. Thus at most 21
multiplications in (7) produce logMP(RMP) which is converted to logMP(xMP) using (4).
Newton’s method
Newton’s method is used indirectly above to take the M’th root of x. When it is
used directly, the exponential is needed 5 times. This requires ~5*(n+k) operations. The
above method requires n operations plus a single exponential and should thus be 4 times
faster. nrlog.doc
Digit loss
Consider an x that is approximately 10-20. Expand x as a decimal so that with 0 
xi  9
128
x  1020 10  i xi
i 1
The exponential of x is
e  exp  x   1  x  x2 / 2  x3 / 6  ...
This expands as
128
128
i 1
i 1
e  1010 i ei  1  1020 10 i ci
The first ei is 1. The next 20 are zero. Then if the series ends at 128, there are no ei’s
representing the last 20 ci’s in this expansion. Information has been lost.
The log of the exponential of an x near 1, cannot reproduce exactly the x of
interest. In this case the information could have been kept by defining the exponential a
1+ term, where term would start at 10-20.
Testing
tmexps.wpj
Download