table-based tests for bessel function software

10
Advances in Computational Mathematics 2(1994)251-260 251 Table-based tests for Bessel function software Allan J. Macleod Department of Mathematics and Statistics, University of Paisley, High Street, Paisley PAl 2BE, Scotland E-mail: [email protected] Received 28 October 1992; revised 11 October 1993 A test procedure is developed for software which evaluates the Bessel functions J0, "/1, Yo, ]"1. The tests are highly accurate and are applied to various available codes. Results are presented on the performance of the codes. Keywords: Bessel functions, testing, software, rounding errors, tables. AMS(MOS) subject classification: 33A40, 65D20, 65G05. 1. Introduction The Bessel functions J0, J~, Yo, Y~ are amongst the most important of all special functions. All the major numerical software libraries contain codes to evaluate these functions, with alternative codes also available in public-domain packages such as SPECFUN or FNLIB, and many published papers. Some of this software contains information on accuracy, but not all, and the methods used to assess accuracy vary greatly. Claims of accuracy need to be checked. Even the libraries are not immune to problems - as Cody and Stoltz [2] pointed out in the case of the NAG exponential integral. General test procedures for such software are very rare. The only portable test is that contained in SPECFUN, described in Cody [1]. Unfortunately, the tests for J0 and J1 involve both functions in each test, as is the case for Y0 and Y1. Thus, any observed errors are difficult to assign to one specific function. A user might also have only one code available, rather than a pair. In this paper, we present a procedure which only tests a single function. The test is based on the table-based methods used by Liu [5] and Tang [9] for elementary functions. The tables of precomputed values allow the test a much higher degree of resolution than the SPECFUN tests. This is important since the Bessel codes are usually well written. LC. Baitzer AG, Science Publishers

Upload: allan-j-macleod

Post on 22-Aug-2016

217 views

Category:

Documents


1 download

TRANSCRIPT

Advances in Computational Mathematics 2(1994)251-260 251

Table-based tests for Bessel function software

Al lan J. M a c l e o d Department of Mathematics and Statistics, University of Paisley,

High Street, Paisley PAl 2BE, Scotland E-mail: [email protected]

Received 28 October 1992; revised 11 October 1993

A test procedure is developed for software which evaluates the Bessel functions J0, "/1, Yo, ]"1. The tests are highly accurate and are applied to various available codes. Results are presented on the performance of the codes.

Keywords: Bessel functions, testing, software, rounding errors, tables.

AMS(MOS) subject classification: 33A40, 65D20, 65G05.

1. Introduction

The Bessel functions J0, J~, Yo, Y~ are amongst the most important of all special functions. All the major numerical software libraries contain codes to evaluate these functions, with alternative codes also available in public-domain packages such as SPECFUN or FNLIB, and many published papers.

Some of this software contains information on accuracy, but not all, and the methods used to assess accuracy vary greatly. Claims of accuracy need to be checked. Even the libraries are not immune to problems - as Cody and Stoltz [2] pointed out in the case of the NAG exponential integral.

General test procedures for such software are very rare. The only portable test is that contained in SPECFUN, described in Cody [1]. Unfortunately, the tests for J0 and J1 involve both functions in each test, as is the case for Y0 and Y1. Thus, any observed errors are difficult to assign to one specific function. A user might also have only one code available, rather than a pair.

In this paper, we present a procedure which only tests a single function. The test is based on the table-based methods used by Liu [5] and Tang [9] for elementary functions. The tables of precomputed values allow the test a much higher degree of resolution than the SPECFUN tests. This is important since the Bessel codes are usually well written.

�9 LC. Baitzer AG, Science Publishers

252 A.J. Macleod, Testing Bessel funct ions

As both Liu and Tang did, we assume that the computer used satisfies the IEEE standard for floating-pt arithmetic. The clean arithmetic environment in such machines makes the error analysis much more straightforward.

2. Basic test procedure

In this section, we describe, in detail, the general accuracy test applied to single-precision codes to evaluate Jo(x). To properly assess the fundamental underlying accuracy, we assume that x is not close to a zero of J0. In fact, we test in regions where IJo(x) l > 1/16 and IJ6(x)/Jo(x)l = IJl(x) /Jo(x) l< 1.

Let x be a floating-pt argument, with BESS(x) the value returned by the code being assessed. The basic aim of the test is to estimate, as accurately as possible, the relative error

I BESS(x) (2.1) Jo(x) e(x) = -f~o(~

Let e denote 1 unit in the last place of the floating-pt representation of 1, i.e. e = 2 -23. Let a = 1/64, I integer, and let h satisfy 0 < h < 1/64. Set x = a + h.

Then,

Jo(x) = Jo(a + h)

h 2 .,,, . = Jo(a) + hJ(~(a) + -~. Jb(a) + . . .

= Jo(a) - hJ l (a) + ~_~ Jtoil(a) + E~, (2.2) i=2

where EN is the truncation error of the Taylor series after the term in h N. A suitable choice of N will give I ENI small enough for

Jo(x) = Jo(a) - hJ l (a) + RN (2.3)

tO full machine accuracy�9 N = 6 is sufficient for single-precision, with R N obviously ~/~/ffi 2 ( hil l! )J[oil(a) �9

Let Jo(a) be expanded as a binary number. Define Jol to be the floating-pt representation of the number with at most 12 significant bits which is

(a) nearest to and <J0(a), or

(b) nearest to and >Jo(a).

Let J02 be the value of J o l - Jo(a) calculated in extended precision so that

Jo(a) = Jol - Jo2 + O(2-11e). (2.4)

A.J. Macleod, Testing Bessel functions 253

Define J11, J12 similarly for Jl(a) (JH having the 12 significant bits), giving in (2.3)

J o ( x ) = (J01 - J 0 2 ) - h ( J n - ,/12) + R,v + 0(2-I~

= Jo l - (J02 + hJl l ) + (h J12 + R N ) + 0(2-1~ ( 2 . 5 )

Let B1 = fl(J01), B2 = 11(./02 + h J l l ) , a n d B 3 = fl(hJl2 + RN) , where fl(z) denotes the floating-pt representation of z.

Clearly, Bl = Jol exactly. Since J~l has at most 12 significant bits, i f h has less than 12 significant bits, hJn will be exact. To do this, we use h =j /65536, j -- 1 . . . . . 1023, which covers [a, a + h) uniformly. The values of a used are such that a + h is computed exactly. To prevent any possible loss of significant bits in B2, w e force Jo2 and Jll to have the same sign. Thus,

where

B2 =(Jo2 +hJll)(l+~), I ~ d < e

= J02 + hJll + 0(l J02 + h J l l l e ) ,

(2.6)

(2.7)

IJo2 + hJld = I-/o21 + IhJld

-< 2 -12 + 2-61Jld. (2.8)

The third value B3 seems the most difficult to analyse for rounding error. We have IhJ121 < 2 -is and IRN I < 2 -13, so, if they are completed accurately, the rounding error is negligible compared to (2.7).

The higher derivatives of Jo can be shown to satisfy

Pi(a)Jo(a) Qi(a)Jl(a) J~l(a) = ai_ 2 + ai_l (2.9)

for i >_ 2, with P2(x) = - 1, O2(x) = + 1, and Pi, Qi even-degree polynomials satisfying

Thus,

~ + l ( x ) = x e l ( x ) - ( i - 2 ) ~ ( x ) + Qi(x ) ,

a i+ l ( x ) = x Q f ( x ) - x 2 P / ( x ) - i ~ 2 ( x ) .

e 3 ( x ) = 1,

P4(x) = x 2 - 3,

Ps(X) = 12 - 2x 2,

P6(x) = - x 4 + 9x 2 - 60,

Q3(x) = x 2 - 2,

Q4(x) = 6 - 2x 2,

Qs(x) = - x 4 + 7x 2 - 24,

Q6(x) = 3x 4 - 33x 2 + 12o.

(2.10)

(2.11)

254 A.J. Macleod, Testing Bessel functions

These functions are evaluated explicitly, rather than using the recurrences (2.10). If we define

Sl = P2(a) + P3(a) ( h ) e6(a) (h~ 4 2[ ---~. \"d) + ' ' " + 6! \ a j '

(2.12)

then

1 ~Q2(a) + Q 3 ( a ) ( h ) + 4} $2 "- a [ T --~.I ka j "'" + ~ . t a ) ( h ) ,

RN = h2 {SiJo(a) + S2Jl(a)}

-.- h2{(S1Jol + S 2 J I I ) - (S1J02 + $2J12) } (2.13)

+ O(2-23e).

S1, S 2 are evaluated by nested multiplication, and exhaustive numerical testing shows that RN is evaluated with minimal rounding error. Thus, we can be confident that

B 3 = hJl2 + R N + 0(2-12e). (2.14)

Combining (2.8) with (2.14), we have

Jo(x) = / h - B2 + B3 + O((2-61Jld + 2-11)e),

so that the relative error in approximating Jo by BI - B2 + B3 is less than

(2-61jd/IJol + 2-1~

_< 2-Se (2.15)

using the restrictions mentioned at the start of this section. The final possible source of rounding errors is in the computation of e(x).

Suppose BESS(x)= Jo(x)+ E1 and define

d~ = B E S S ( x ) - B~,

d : = d ~ + B 2 ,

d3 = d : - t ~ ,

(2.16)

giving e ( x ) - - I d 3 1 ( B l - B 2 + B3)I. If E1 is large relative to J0(x), the error will be large enough to be unaffected by rounding error, so we consider only the case where I El l is small relative to Jo.

The test-intervals used are such that Jo has a constant exponent when expressed in binary floating-point. Thus, dl will be calculated exactly. Now,

A.J. Macleod, Testing Bessel functions 255

dl = (JoCx) + El) - (Jo(a) + E2)

= hJ~(y) + (El - E2),

with a < y < x. Similarly, B 2 ~ - hJ~(a), so if I E11 is small, dl and B 2 will be of similar sizes and opposite signs. Thus, the rounding error in computing d2 will be at most O(he), and this will be the dominant error term. Thus, rounding error will not greatly affect the computation of e(x).

In the next section, we shall show that the above bounds are very pessimistic, and the test procedure is extremely accurate with a high degree of resolution.

3. Results

The test procedure from section 2 was implemented on an IBM PS/2 model 70, fitted with a Maths coprocessor, using the IBM FORTRAN/2 compiler with the Iz option which suppresses code optimisation.

Given an integral multiple of �89 denoted by A, the interval [A, A + �89 was divided into 32 equal sub-intervals [A + il64, A + (i + 1)/64], i = 0 . . . . . 31. The values Jo(A + il64), J1 (A + il64) were calculated using a multiple-precision package to sum the power series for each function. The calculations were done twice (as a check), with different numbers of series terms and different precision levels. The corresponding J01, Jo2, Jli and J12 values were expressed in exact hexadecimal notation to prevent error in transforming decimal numbers.

The tests were applied to 5 codes available in the open literature. These are

(a) BESJO from Cody's SPECFUN package. This is available on NETLIB. The code uses rational approximations with a possible accuracy of 18 significant decimals.

(b) BESJO from Fullerton's FNLIB [3], which is also available via NETLIB. This uses Chebyshev polynomials with a possible accuracy of up to 17 decimal places.

(c) BESLRI, written by Sookne [8]. This uses the backward recurrence method to obtain a sequence of Bessel function values, J0(x), Jl(x) . . . . . Jn(x). Thus, it is different in character and intention from codes producing a single value. The code requires a parameter NSIG for the number of significant decimal places required - we used NSIG = 8.

(d) BESSJO from Press et al. [7]. This is a very popular book and might well be where an interested user would look first. The program uses rational approximations, but with no indication of the underlying accuracy. Although defined as a single-precision function, the internal computations are all done in double precision and we keep this form, which seems to be the authors' intention.

256 A.J. Macleod, Testing Bessel functions

(e) BESJP, written by Piessens [6]. This uses a mixture of backward recurrences, series and asymptotic expansion. The code is given in double precision, but can be easily changed to run in single-precision form by redefining variables and data constants.

The tests were also applied to a code which evaluates Jo(x) from its power series, with all internal computations done in double precision. Apart from a very few isolated cases, this code will return the nearest floating-point value to Jo(x). Thus, a perfect test would measure a maximum error of 0.5 ULP. Any increase on this defines essentially the resolution of the test.

The tests were run on five different intervals of width 1/2. In each sub- interval of width 1/64, 1023 arguments are tested, giving 163,680 test arguments in all. For each argument, the error was calculated in units of the last place of 1, and overall summary statistics for each interval are

(i) the maximum observed error (denoted ME),

(ii) the root-mean-square error (RMS).

These values are given in table 1, where CALIB denotes the calibration code, and the other codes are denoted by the relevant author names as given in (a)-(e) .

Table 1

Results for testing Jo in single-precision.

Interval

[~-, 1] [3, 3~-] [6.5, 7] [10.5, 11] [13.5, 14]

C o ~ ME RMS ME RMS ME RMS ME RMS ME RMS

CALIB 0.5024 0.289 0.5067 0.287 0.5010 0.289 0.5062 0.289 0.5035 0.289

Cody 3.41 1.16 4.32 1.40 2.69 0.75 2.84 0.72 2.80 0.80

Fullerton 3.48 0.97 1.62 0.49 2.47 0.68 6.61 2.36 4.92 1.66

Sookne 3.04 0.99 2.56 0.66 2.97 0.81 5.78 1.49 5.70 1.51

Press 0.547 0.29 0.60 0.30 0.60 0.29 3.89 2.43 2.99 1.62

Piessens 12.79 8.60 12.26 6.83 7.48 1.56 5.75 2.88 4.85 2.79

As we can see from the calibration code's results, the tests have a resolution much higher than the theoretical bound of e/32. The extreme accuracy of these results gives confidence that the results for the other codes are an accurate assessment of their performance.

All the codes perform well, with none showing poor accuracy. Clearly, though, there are variations in performance. Piessens' BESJP is worse for small x than the

A.J. Macleod, Testing Bessel functions 257

others. This is primarily due to the fact that the code is a general one for Jr(x), and the method used does not take any account of the simplifications when p = 0. The performance improves as the asymptotic expansion comes into play.

For small x, BESSJO from Numerical Recipes is very accurate, certainly due to the internal double-precision computations. The most consistent code results are those of BESJO from SPECFUN.

4. Further results

The above test procedure can be used for J~, I"o and I/1 with minor changes. All five codes from section 3 can either be used directly for Jl or have equivalents. For Yo and Y1, only SPECFUN, FNLIB and Numerical Recipes provide codes.

The results forJ~ were essentially the same as those for Jo, with BESSJ1 from Numerical Recipes performing best of all.

For Yo and I"1, we included a test on [�88 ~], where the behaviour of function and derivative do not imply such a good error bound as for J0 or J~. The single- precision results for both Yo and }'1 are given in tables 2 and 3, respectively. The calibration results for [0.25, 0.75] show the reduction in accuracy of the tests in this interval, while, in the other intervals, the tests are as accurate as those for Jo.

Table 2

Accuracy results for I/o (maximum error).

Interval Source [0.25, 0.75] [2.25, 2.75] [4.75, 5.25] [II, 11.5] [15, 15.5]

Calibration 0.554 0.504 0.503 0.504 0.505 SPECFUN 4.35 3.20 2.44 2.86 3.11 FNLIB 12.66 1.09 2.98 6.68 4.77 Numerical Recipes 2.69 0.96 1.76 4.40 2.61

Table 3

Accuracy results for I/1 (maximum error).

Interval Source [0.25, 0.75] [2.5, 3] [6.25, 6.75] [10.25, 10.75] [13.5, 14]

Calibration 0.520 0.512 0.506 0.503 0.502 SPECFUN 2.13 2.87 5.21 4.06 4.09 FNLIB 4.85 2.93 5.33 5.74 1.86 Numedcal Recipes 1.75 1.86 1.79 0.83 0.78

258 A.J. Macleod, Testing Bessel functions

The results show good accuracy for all three available codes, except for the FNLIB Y0 code in [0.25, 0.75]. In this range, Y0 is computed as

Yo(x) = 2 ln(x/2)Jo(x) + 0.375 + g(t), ~g

(4.1)

where g(t) is a Chebyshev expansion in t = x2/8 - 1. The code BESJO in FNLIB is used to compute Jo(x). Extensive testing indicated a small stability problem in evaluating g(t), but it is not clear if this is enough to cause the problem.

The tests can also be applied in double-precision. In IEEE arithmetic this gives 53 bits of precision, so our Jol,Jll values are given to 24 significant bits. This also enables the use of smaller h values while keeping hJll exact. In fact, in each sub-interval of width 1/64 we test 8191 arguments with h = i/(8191 • 64), i = 1 . . . . . 8191.

Table 4

Maximum error results for double-precision J1 codes.

Interval

Code [1.25, 1.75] [4.75, 5.25] [7.5, 8.0] [11.75, 12.25] [15, 15.5]

Cody 2.00 2.43 3.04 4.07 3.01

Fullerton 1.1)9 2.37 4.86 4.67 5.12

Sookne 1.55 2.98 6.22 6.58 7.08

Piessens 9.82 18.99 17.05 35.42 33.87

Table 4 shows the results applied to double-precision versions of codes for �9 /1. In most cases, the results mirror the single-precision results.

Overall, the results show that most codes for Bessel functions are very well written. The tests do not consider the robustness of a code, that is, the ability to detect possible problems such as overflow and underflow. A study of the codes indicates that most have taken some precautions in this area, except those codes from Numerical Recipes, which give no consideration to this problem.

5. Testing near zeros

The procedure described in section 2 is not suitable for assessing performance near a zero of one of the Bessel functions. It is very difficult to devise a test of equivalent generality, so the following simpler method was derived. We describe the method in terms of Jo for ease of presentation.

A.J. Macleod, Testing Bessel functions 259

Let Z be a strictly positive zero of Jo, and let Z-, Z + be the two nearest floating-pt arguments to Z with Z- < Z < Z +. Suppose Z = S x 2 e with 0.5 < S < 1, and let the floating-pt significands have p significant bits. Let t~ = 2 s-t ' and define

Z / = Z - - ( 2 / - 1 ) ~ , Z : ' = Z ++ (2 i - 1 ) S (5.1)

for i = 0 , 1 . . . . . 11. By using a multiple-precision package, we can calculate Z to any required

precision and hence easily determine Z/-, Z[ . The values of J0 at these points can also be determined to any precison. Let

Jo(ZT) Lil + Li2,

Jo(Z +) ---- uil + ui2,

i=O . . . . . 11, (5.2)

where Lil, Li2, Uil, Ui2 are floating-pt arguments with Lil, Uil having 12 significant bits only and Li2, Ui2 the correctly rounded remainders.

Then the absolute and relative errors at ZF are

abs error = I (BESS(Z/-) - L/l) - L/21,

rel error = abs error

141 + L 21'

(5.3)

with equivalent formulae for Z~. All the required values are represented in hexadecimal form to prevent data transformation errors.

This technique is applied to the first 5 positive zeros for each of J0, -/1, Y0 and Yb All the codes show good absolute error results, as one would expect from the results in sections 3 and 4.

The relative error results, however, are generally very poor. All codes lose accuracy very close to the roots, with most giving very few significant figures. The Cody codes perform better than the others for the smaller roots since they use approximations of the form

Jo(x) = (x 2 - root 2) * rational approx (5.4)

and equivalent forms for the other functions. When the asymptotic forms are used, all the codes lose all significant figures near the zero.

Acknowledgements

The author would like to thank two referees and the editor for several helpful comments which have helped to clarify several portions of the paper.

260 A.I. Macleod, Testing Bessel functions

References

[1] W.J. Cody, SPECFUN - A portable special function package, in: New Computing Environments: Microcomputers in Large-Scale Scientific Computing, ed. A. Wouk (SIAM, Philadelphia, 1987).

[2] W.J. Cody and L. Stoltz, The use of Taylor series to test accuracy of function programs, ACM Trans. Math. Softw. 17(1991)55-63.

[3] L.W. Fullerton, Portable special function routines, in: Portability of Numerical Software, ed. W. Cowell, Lecture Notes in Computer Science, Vol. 57 (Springer, Berlin, 1977).

[4] J.F. Hart, E.W. Cheney, C.L. Lawson, H.J. Maehly, C.K. Mesitenyi, J.R. Rice, H.G. Thacher, Jr. and C. Witzgall, Computer Approximations (Wiley, New York, 1968).

[5] Z.A. Liu, Berkeley elementary function test suite, M.S. Thesis, Computer Science Division, Department of Electrical Engineering and Computer Science, University of California at Berkeley (1987).

[6] R. Piessens, The computation of Bessel functions on a small computer, Comp. Math. Appl. 10(1984) 161-166.

[7] W.H. Press, B.P. Flannery, S.A. Teukolsky and W.T. Vetterling, Numerical Recipes (Cambridge University Press, Cambridge, 1986).

[8] D.J. Sookne, Bessel functions of real argument and integer order, L Res. Nat. Bur. Stand., Series B 77a(1973)125-132.

[9] P.T.P. Tang, Accurate and efficient testing of the exponential and logarithm functions, ACM Trans. Math. Softw. 16(1990)185-200.