rational approximations, software and test methods for sine and cosine integrals

14
Numerical Algorithms 12(1996)259-272 259 Rational approximations, software and test methods for sine and cosine integrals Allan J. MacLeod Department of Mathematics and Statistics, University of Paisley, High St., Paisley, Scotland PA 1 2BE E-mail: [email protected] Received 7 October 1993; revised 11 March 1996 Communicated by C. Brezinski and H. Redivo-Zaglia Rational approximations to the sine integral Si(x) and cosine-integral Ci(x) are developed which give an accuracy of 20sf. The robust construction of software for these functions is discussed, together with a test procedure for assessing the performance of such codes. Use of the tests discovers a major error in the netlib library FN codes for Si. Fortran versions of the codes and tests are available by electronic mail. Keywords: Sine-integral, cosine-integral, rational, software, testing. Subject classification: 65D20, 65V05, 41A20, 33A10. 1. Introduction The sine-integral Si(x) and the cosine-integral Ci(x) are defined respectively as Si(x) = fXsint dt Jo t ' (1) f x cos t -- 1 Ci(x) = "~ + In x + Jo -t dt, (2) where 7 is Euler's constant, and Ci is only defined for x > 0. Various properties of Si and Ci can be found in chapter 5 of Abramowitz and Stegun [1]. Software for their evaluation exists in libraries such as NAG and IMSL, and in public-domain packages such as FN. These programs all use, essentially, Chebyshev expansions. There do not seem to be any codes based on rational approximations, even in Cody's excellent SPECFUN package [4]. The present work develops suitable rational approximations which give a maximal accuracy of 20sf., except near the zeros of Ci for x > 6. The implementa- tion of these approximations into a robust and reliable code is then considered. Finally, a test procedure, to assess the performance of Si and Ci codes, is developed and applied to various available programs. J.C. Baltzer AG, Science Publishers

Upload: allan-j-macleod

Post on 10-Jul-2016

236 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Rational approximations, software and test methods for sine and cosine integrals

Numerical Algorithms 12(1996)259-272 259

Rational approximations, software and test methods for sine and cosine integrals

Allan J. MacLeod

Department of Mathematics and Statistics, University of Paisley, High St., Paisley, Scotland PA 1 2BE E-mail: [email protected]

Received 7 October 1993; revised 11 March 1996 Communicated by C. Brezinski and H. Redivo-Zaglia

Rational approximations to the sine integral Si(x) and cosine-integral Ci(x) are developed which give an accuracy of 20sf. The robust construction of software for these functions is discussed, together with a test procedure for assessing the performance of such codes. Use of the tests discovers a major error in the netlib library FN codes for Si. Fortran versions of the codes and tests are available by electronic mail.

Keywords: Sine-integral, cosine-integral, rational, software, testing.

Subject classification: 65D20, 65V05, 41A20, 33A10.

1. In troduc t ion

The sine-integral Si(x) and the cosine-integral Ci(x) are defined respectively as

S i ( x ) = fXsint dt Jo t '

(1)

f x c o s t - - 1

Ci(x) = "~ + In x + Jo -t dt, (2)

where 7 is Euler's constant, and Ci is only defined for x > 0. Various properties of Si and Ci can be found in chapter 5 of Abramowitz and

Stegun [1]. Software for their evaluation exists in libraries such as N A G and IMSL, and in public-domain packages such as FN. These programs all use, essentially, Chebyshev expansions. There do not seem to be any codes based on rational approximations, even in Cody 's excellent S P E C F U N package [4].

The present work develops suitable rational approximations which give a maximal accuracy of 20sf., except near the zeros of Ci for x > 6. The implementa- tion of these approximations into a robust and reliable code is then considered. Finally, a test procedure, to assess the performance of Si and Ci codes, is developed and applied to various available programs.

�9 J.C. Baltzer AG, Science Publishers

Page 2: Rational approximations, software and test methods for sine and cosine integrals

260 A.J. MacLeod / Computing sine and cosine integrals

The software discussed in this paper is available from the author or via the NUMERALGO section of NETLIB [7]. Further details are given in the final section of the paper.

2. Approximations

From Abramowitz and Stegun, we have oo (__l)kx2k+l

Si(x) = ~ ] (2k + 1)(2k + 1)!' k=0

x 2k

Ci(x) = "), + lnx + y ~ 2k(2k)! ' k=l

(3)

(4)

with both series converging for all x. We also have the auxiliary functionsfi(x) and gi(x) , defined by

71" Si(x) = -~ - f i ( x ) cos x - gi(x) sin x, (5)

Ci(x) = f i ( x ) sin x - gi(x) cos x, (6)

with the following asymptotic expansions

1 ~ (--1)k(2k)! f i ( x ) ,,~ x k=0 X2 k , (7)

1 ~ (--1)k(2k+ l)! gi(x) "~-~ ~ X2 k (8)

k=0

The cosine-integral has an infinite number of zeros, with the first two being at

r0 = 0.61650 54856 20716 23379 71104 04100,

r I = 3.38418 04228 51186 42639 78511 46402.

These values were calculated by solving C i ( x ) = 0, using a multiple-precision arithmetic package which performed all calculations to 40sf.

To develop good rational approximations, a large amount of experimentation was used to find suitable break-points and efficient functional forms. The following equations resulted:

Si(x) ~- x P"7(x2~) Qi,7(x2) '

Ci(x) ~ ln(x/ro) + (x 2 - ro 2) P2'5(x2) Q 2 , 5 ( x 2 ) '

/'3,fix 2) Ci(x) ~- ln (x / r l ) + ( x 2 - rl 2) Q3,6(x2) ,

Ixl 6, (9)

0 < x < 3, (10)

3 < x < 6 , (11)

Page 3: Rational approximations, software and test methods for sine and cosine integrals

A.J. MacLeod / Computing sine and cosine integrals 261

1 P4,s(1/x 2) f i (x) ~_ xa4,5(1/x2), 6 < Ixl _< 12, (12)

1 Ps,8(1/x 2) gi(x) ~-- --~ Qs,8(1/x2) ,

1 P6,v(1/x2)~ ~ 1 1 x2Q6,7(1/x2)j j q ( x ) _ x -

1 ( 1 P7,8!l/x2)~ gi(x) ~_--~ 1 - x 2 Q7,8(1/x2)j,

6 < Ixl 12, (13)

Ixl > 12, (14)

Ixl > 12, ( 1 5 )

where Pi j denotes a polynomial of degree j (i is just an index), and similarly for

O . The polynomials were all computed using the Cody-Fraser-Har t version of the

second Remes algorithm, see [5]. This was coded in quadruple-precision arithmetic on a Prime 6350, which gives roughly 28sf. Values of the various functions were computed as follows:

(1) For Si with Ix[ < 6, the power series form (3) was used. (2) For Ci in (0, 6], the power series (4) was used, except when x was close to r 0 or

r~. In such regions a Taylor expansion about the root was used, with derivative values generated by the recurrence relations discussed by Gautschi [9].

(3) For x > 6, the funct ionsf i and gi were expanded in terms of Chebyshev poly- nomials. The coefficients were determined by applying Clenshaw's method [2] to the differential equations satisfied by f i and gi.

The rational approximations in S P E C F U N are given to roughly 20sf. and this accuracy level is used to derive the coefficients for Si and Ci. These are given in tables 1-7, with the first value being the constant term, and successive values corresponding to increasing powers.

Table 1 Coefficients for Si in [0, 6].

Degree PI.7 QI,7

1.00000000000000000000E 0 -0.44663 99893 1312457298 E - t

0.11209 1464431123 69449E- 2 -0.13276 124407928422367E-4

0.85118014179823463879 E - 7 -0.29989314303147656479 E - 9

0.55401971660186204711E - 12 -0.4240635343 31332 12926 E - 15

1.00000000000000000000E 0 0.10891556624243098264 E - I

0.593344567691868 35896 E - 4 0.21231 112954641805908 E - 6 0.54747 121846510390750 E - 9 0.10378 56151 13318 14674 E - 11 0.13754880327250272679 E - 14 0.10223981202236205703 E - 17

Page 4: Rational approximations, software and test methods for sine and cosine integrals

262 A.J. MacLeod / Computing sine and cosine integrals

Table 2 Coefficients for Ci in (0,3].

Degree P2,s Q2,5

-0.24607411378767540707E 0 0.72113492241301534559 E - 2

-0.1186712783 6204767056 E - 3 0.90542655466969866243 E - 6

-0.3432224241 24444 09037 E - 8 0.51950683460656886834 E - 11

1.00000000000000000000 E 0 0.12670095552700637845E - l 0.78168450570724148921E - 4 0.29959200177005821677 E - 6 0.73191 67776 13288 3 8 2 1 6 E - 9 0.94351 17453 09075 29061 E - 12

Table 3 Coefficients for Ci in (3,6].

Degree P3,7 03.6

-0.15684781827145408780 E 0 0.66253 165609605468916E- 2

-0.12822297297864512864 E - 3 0.12360964097729408891E - 5

-0.6645097511 2876224532 E - 8 0.203269364668031 5 9 4 4 6 E - 10

-0.3359088313 5343844613 E - 13 0.2368693496 14350 15119 E - 16

1.00000000000000000000 E 0 0.9616604438 8828741188 E - 2 0.45257 51459 1 2 5 7 0 3 5 0 0 6 E - 4 0.1354492265 9627723233E - 6 0.27715 36568 6 5 7 0 0 0 2 0 8 1 E - 9 0.3771867630 16889 3 2 9 2 6 E - 12 0.2770684449 7155995398E- 15 0.00000000000000000000 E 0

Table 4 Coefficients fo r f i in (6,12].

Degree P4,7 04,7

0.99999999962173909991 E 0 0.36451 06033 8631902917 E 3 0.44218 54804 1288440874 E 5 0.22467 5694059611 51887 E 7 0.49315 316723035561922 E 8 0.43186795279670283193 E 9 0.11847992519956804350 E 10 0.45573 26759 37951 03181 E 9

1.00000000000000000000 E 0 0.36651 06027 3229347594 E 3 0.44927569814970692777 E 5 0.23285 354882204041700 E 7 0.53117852017228262911E 8 0.503353106672418 70372 E 9 0.16575285015623175410 E 10 0.11746532837038341076 E 10

3. C o d i n g Si

Since Si is an odd function, a simple-minded program for evaluation might be:

input x

Y = Ixl if y _< 6 then

use (9) for Si(y) else

if y < 12 then

Page 5: Rational approximations, software and test methods for sine and cosine integrals

A.J. MacLeod / Computing sine and cosine integrals

Table 5 Coefficients for gi in (6,12].

263

Degree Ps.s Qs,8

0 0.99999999920484901956 E 0 1.00000000000000000000 E 0 1 0.51385504875307321394 E 3 0.51985504708814870209 E 3 2 0.9229348345 20138 10811 E 5 0.95292615508125947321 E 5 3 0.74071341863359841727 E 7 0.79215459679762667578 E 7 4 0.28142 3561628413 56551E 9 0.31977 5677907337 81460 E 9 5 0.4928089035 7734623984 E 10 0.62273 1347024390 12114 E 10 6 0.3552476268 5554302472 E 11 0.54570971054996441467 E 11 7 0.79194271662085049376 E 1l 0.18241 750166645704670 E 12 8 0.17942 5226244138 98907 E 11 0.15407 148148861454434 E 12

Table 6 Coefficients fo r f i for x > 12.

Degree P6,7 Q6,7

0.19999999999999978257 E 1 0.22206 1193804349 58727 E 4 0.84749007623988236808 E 6 0.13959267954823943232 E 9 0.10197205463267975592 E 11 0.30229865264524075951E 12 0.27504053804288471142 E 13 0.218189897046868 74983 E 13

1.00000000000000000000 E 0 0.11223 0596902171 67788 E 4 0.436852709748513 13242 E 6 0.74654702140658116258 E 8 0.58580034751805687471E 10 0.2015798037 9272098841E 12 0.26229 141857684496445 E 13 0.87852907334918467516 E 13

Table 7 Coefficients for gi for x > 12.

Degree P7,8 Q7,8

0 0.5999999999 9999993089 E 1 1 0.96527746044997139158 E 4 2 0.56077626996568834185 E 7 3 0.150226677189273 17198 E 10 4 0.19644271064733088465 E 12 5 0.12191368281163225043 E 14 6 0.3192438989 8645609533 E 15 7 0.2587605301 0027485934 E 16 8 0.1275497889 62688 78403 E 16

1.00000000000000000000 0.16287957674166143196 0.96636303195787870963 0.26839734750950667021 0.37388 5105480292 19241 0.26028 585666152144496 0.8513428371 6950697226 0.1130407936 16279 52930 0.42519 841479489798424

E 0 E 4 E 6 E 9 E l l E 13 E 14 E 16 E 16

g e t f i ( y ) f r o m (12), gi(y) f r o m (13)

e l se

g e t f i ( y ) f r o m (14), gi(y) f r o m (15)

e n d i f

u s e (5) f o r Si(y) e n d i f

o u t p u t Si(x) = s ign (x) �9 Si(y)

Page 6: Rational approximations, software and test methods for sine and cosine integrals

264 A.J. MacLeod / Computing sine and cosine integrals

Such a code would, however, cause possible overflow and underflow problems for certain arguments. To prevent these difficulties we must analyse the behaviour of Si more deeply. Again we assume x _> 0.

For x < 6, (9) shows we must compute x 2 as the polynomial argument, giving a possible underflow if x is very small. (3), however, implies that

x 1 - i-8 <- Si(x) <_ x. (16)

Define EPSNEG to be the smallest positive floating-point argument (on the computer being used) such that

1 - EPSNEG < 1 (17)

on the machine. Clearly, if x2/18 < EPSNEG, then Si(x) = x to machine precision. Thus, if we

define XLOW = v/18 �9 EPSNEG, then we can just set Si(x) to x, if x < XLOW. At the other end of the range, consider x > 12. Again we need to compute x 2

giving now the more serious possibility of overflow. Consideration of the asymp- totic series expansions shows that, if x z > 6/EPSNEG, then, to machine-precision, f i ( x ) = 1 / x and gi(x) = 1/X 2. Define XHIGH1 = V/6/EPSNEG. Thus, if x > XHIGH1, we have

7r cos(x) sin(x) Si(x) - 2 x x 2 (18)

As x gets larger, the contribution from the second and third terms tends to zero.

~ ( 1 + 1 ) ~ ( 1 1 ) (19) 2 <- S i ( x ) < + .

Define EPS to be the smallest positive floating-point argument such that 1 + EPS > 1. Then Si(x) = 7r/2 to machine-precision if

1 1 x + ~-~ < min (EPS, EPSNEG). (20)

To find the exact boundary-value for this inequality would require the solution of a quadratic equation. To avoid this we can use the fact that, for x > 1,

1 1 2 - + < - , (21) x ~-~ x

so that, if 2 / x < min (EPS, EPSNEG), we have Si(x) = rr/2. Define XHIGH2 = 2/min (EPS, EPSNEG).

There is also a problem in computing trigonometric functions for large values of x. If ]x[ > 7r/EPS then Ix I and Ix[ + 7r are identical to the machine, making attempts to compute cos or sin futile. If we define XHIGH3 = 7r/EPS, then we should set Si(x) = 7r/2 if x > XHIGH3.

We have

Page 7: Rational approximations, software and test methods for sine and cosine integrals

A.J. MacLeod / Computing sine and cosine integrals 265

Thus a more robust code for Si would be

input x

Y = Ixl if y < 6 then

if y < X L O W then Si(y) = y

else use (9) for Si(y)

endif endif if y > 6 and y < 12 then

ge t f i (y ) Iron (12), gi(y) f rom (13) use (5) for Si(y)

endif if y > 12 then

X H I G H = min ( X H I G H 2 , X H I G H 3 ) if y > X H I G H then

Si(y) = rr/2 else

i f y > X H I G H 1 then Si(y) = 7r/2 - co s (y ) /y - sin(y)/y2

else ge t f i (y ) f rom (14), gi(y) from(15) use (5) for Si(y)

endif endif

endif ou tpu t Si(x) = sign (x) �9 Si(y)

There is still the possibility of underf low in the use of (5), if the a rgument x is close to a zero of cos or sin. It is impossible to adequately control this due to the infinite n u m b e r of possible zeroes.

Values of EPS and E P S N E G for a large selection of machine/compi ler combina t ions are given by Cody [3].

Using the ideas of this section, single and double precision codes to evaluate Si have been developed.

4. C o d i n g Ci

For x << l, (4) shows that

x 2 3, + In x - ~- < Ci(x) < "~ + In x < 0, (22)

Page 8: Rational approximations, software and test methods for sine and cosine integrals

266 A.J. MacLeod / Comput#Tg sine and cosine #,tegrals

giving Ci = "7 + In x to machine-precis ion if

x2 I 4(~, + lnx) < EPS. (23)

If we assume that x < 1/e 2 (not unreasonably) then

X2 ] X2 4(7 + lnx) < 4 - ' (24)

so that we can set Ci(x) = 7 + In x if x < 2 ~ . Define X L O W = 2v/E-PS. For x close to r0, we express

(x 2 - ro 2) = (x - ro)(X + to) = ((x - ro,) - ro2)(x + ro), (25)

where ro = rol + ro2, with rol having a small number of significant figures, and ro2 specified to full accuracy. For example, we use ro~ = 631/1024 and ro2 = 0.29454812071623379711 E - 3.

In S P E C F U N , Cody computes ln(x/ro) as follows

ln(x/r0) = ln(1 + (x - r 0 ) / r 0 ) = ln(1 + ((x - r0,) - r02) / r0) (26)

and uses a rational approx imat ion for ln(1 + t). We follow this idea but use the rational approximat ion f rom Hart et al. [12] with code name L O G E 2740. Numerical testing shows this improves per formance on certain machines.

For large x, the sin and cos problems ment ioned in the previous section also play a part. Thus, for large x, we cannot expect to compute Ci to any relative accuracy, so we just set the result to 0.0.

Again, single and double precision codes for Ci have been written.

5. Testing

Since the sine and cosine integrals are closely l inked to the exponential integral, we use a very similar procedure to that described in Cody and Stoltz [6]. Since that paper describes the general phi losophy of Taylor series test me thods in full detail, we only give a basic derivation of the relevant results.

L e t f ( x ) be the mathemat ica l funct ion being approximated , and F(x) the value generated by the computer , with the assumpt ion that x is an exactly represented number . The relative error is

6-- F(x) - f ( x ) f (x) (27)

Let x = y + h , so

f (x) = f ( y ) + fCi)(y) = f ( y ) + Z h ( i ) . , ~ f (.vj + RN, (28) i = 1 i = 1 "

where Ru is the error in t runcat ing after N terms of the Taylor series.

Page 9: Rational approximations, software and test methods for sine and cosine integrals

A.J. MacLeod / Computing sine and cosine integrals 267

Let Fi(y) be compu ted values of f ( ; / (y) , such that Fi(y ) =f(il(y)(1 +6i ) , i = 1 , . . . , N. Let F(y) be the computed value o f f ( y ) with F(y) = f ( y ) ( 1 + e).

We approximate 6 by

N

F(x) - F(y) - Z hiFi(y)/i! i=1 (29)

E = F(x)

To first order, we have

f (Y )e hT(il(y) RN (30) E = ~ -j---(-~ - Z N ~ 6, +f(x"--)"

i=I

The last term in this expression can be ignored if we take N large enough, while if [h[ is small and [f(i)(y)/f(x)[ does not grow exponentially, we have, essentially

f (Y) e (31) E,,~ 6--f-(--~

and we can ensure f ( y ) / f ( x ) l < 1. Thus E will be a reasonable es t imator of 5. Fo r the specific case of f ( x ) = Si(x), we have f ' ( x ) = sin(x)/x, and f(il(x)

satisfies the following recurrence

f(n+l)(x ) + n fC,,)(x) = a,,(x_____~), (32) X X

where o-1 -- cosx , cr 2 = - s inx, o-3 = - cosx , 04 = s inx, cr 5 = al , or6 = o-~, . . . . This recurrence has been studied by Gautschi [9] and Gautschi and Klein [10], with codes given in [8] and [11].

Combin ing all of the above ideas, we can construct a test code similar to that in S P E C F U N for the exponential integral. Details on obtaining the codes are given in the next section.

For an interval [a, b], we test 5000 r a n d o m arguments x with the error E expressed as a loss of significant digits by the formula

loss = max (0, p + In ([EI)/lnB), (33)

where B is the base of the number system, and p the number of base-B digits in the significand of a f loating-point number . Such values can be found in the paper of Cody [3], and the test code includes a copy of the M A C H A R code. For each interval tested, we report the m a x i m u m loss observed and the root -mean-square loss.

The per formance of the test procedure itself can be assessed by applying it to a code where all internal computa t ions are done using a higher precision than used in the tests. Figures for such calibration codes show that the tests seem to be effective.

The tests are applied to various codes:

1. The calibration codes, denoted CAL. 2. The codes described in the previous sections, denoted NEW.

Page 10: Rational approximations, software and test methods for sine and cosine integrals

268 A.J. MaeLeod / Computing sine and cosine hTtegrals

(3) Codes from the FN library, obtained using NETLIB and denoted FNL. (4) Codes from the NAG library, denoted NAG.

The tests were run on a SUN SPARCstation IPC, and a Prime 4050. The NAG library is only available on the Prime and only in double precision. The tests were run using the SUN and Prime F77 compilers with any code optimisation reduced to the lowest possible level to get the correct results from MACHAR. The register allocation process on the Prime also dictated the use of the frn option. The Prime compiler also gives quadruple precision enabling a calibration code to be written for double precision tests.

The results are summarised in tables 8-11, with sp denoting single precision tests and dp double precision tests. For each interval, we quote the root-mean-square loss then the maximum loss. As stated, the calibration codes all show the expected

Table 8 Results for Si on the SUN.

Code [0.5, 1] [-2.5, -2 ] [5,6] [ -1 I , -10] [14, 15]

CAL(sp) 0.00 0.80 0.00 0.25 0.00 0.44 0.00 0.29 0.00 0.33 NEW(sp) 0.23 2.25 0.21 2.24 1.26 3.13 0.00 1.08 0.00 1.22 FNL(sp) 0.00 1.51 0.00 1.22 0.00 1.28 12.3 13.2 0.00 1.22

NEW(dp) 0.21 1.96 0.23 2.07 1.28 3.57 0.00 1.12 0.00 1.15 FNL(dp) 0.00 1.62 0.00 1.21 0.00 1.31 41.3 42.2 0.00 1.15

Table 9 Results for Si on the Prime.

Code [0.5, 1] [-2.5, -2] [5, 6] [ -11 , -10] [14, 15]

CAL(sp) 0.00 0.84 0.00 0.20 0.00 0.43 0.00 0.29 0.00 0.33 NEW(sp) 0.05 1.81 0.07 1.85 0.91 2.79 0.00 1.22 0.00 1.16 FNL(sp) 0.00 1.29 0.00 0.41 0.00 1.43 11.3 12.2 0.00 1.16

CAL(dp) 0.00 0.76 0.00 0.23 0.00 0.45 0.00 0.27 0.00 0.33 NEW(dp) 0.23 2.38 0.19 1.95 1.19 3.21 0.00 1.10 0.00 1.18 FNL(dp) 0.00 1.58 0.00 1.18 0.00 1.30 35.3 36.2 0.00 1.18 NAG(dp) 1.75 3.46 1.72 3.34 0.78 2.62 0.00 1.58 0.00 1.87

Table 10 Results for Ci on the SUN.

Code [0.25, 0.375] [2, 2.5] [4, 4.5] [8, 8.5] [13.5, 14]

CAL(sp) 0.00 1.16 0.00 0.60 0.00 0.72 0.00 0.27 0.00 0.88 NEW(sp) 0.64 2.90 2.12 4.12 2.45 4.38 0.68 2.69 0.46 2.30 FNL(sp) 2.49 4.49 0.93 2.90 0.27 2.09 0.00 1.86 0.45 2.30

NEW(dp) 0.95 2.90 2.10 4.50 2.63 4.36 0.81 2.57 0.47 2.35 FNL(dp) 2.48 4.60 0.90 3.11 0.28 2.30 0.00 1.84 0.47 2.35

Page 11: Rational approximations, software and test methods for sine and cosine integrals

A.J. MacLeod / Computing sine and cosine integrals 269

Table 11 Results for Ci on the Prime.

Code [0.25, 0.375] [2, 2.5] [4, 4.5] [8, 8.5] [13.5, 14]

CAL(sp) 0.00 1.29 0.00 0.64 0.00 0.59 0.00 0.31 0.00 0.91 NEW(sp) 0.67 2.83 1.84 4.16 1.86 3.79 0.51 2.42 0.43 2.43 FNL(sp) 2.14 4.19 0.46 2.34 0.18 1.99 0.00 1.60 0.42 2.43

CAL(dp) 0.24 1.84 0.00 0.72 0.00 0.69 0.00 0.35 0.00 0.97 NEW(dp) 1.20 2.91 2.11 4.08 2.58 4.60 0.56 2.37 0.49 2.27 FNL(dp) 2.53 4.46 1.15 3.00 0.32 2.16 0.00 1.97 0.50 2.37 NAG(dp) 5.16 7.13 4.79 6.70 4.36 6.41 4.26 6.12 5.20 6.95

losses indicating the test procedure to be reasonable. There also seems to be general agreement between the machines.

The mos t obvious result is the very poor per formance of the F N codes for Si in the interval [ - 1 1 , - 1 0 ] . The fact that both error measures are so close indi- cates a severe problem over the whole interval and not just a one-off effect. An investigation of the code shows the following segment, where x is the func- tion argument , absx = [x[, and r9sifg is an auxiliary subrout ine to compute f i and gi.

call r9sifg (absx, f, g) cosx = cos (absx) si = pi2 - f �9 cosx - g �9 sin (x) if (x.lt.0.0) si = - s i

It is clear that the p rob lem comes f rom sin being evaluated with a rgument x rather than Ix[. Since cos is even, the second line is not needed. To correct the error, the following new code is in the spirit of the old one.

call r9sifg (absx, f, g) sinx = sin (absx) si = pi2 - f �9 cos (x) - g �9 sinx if (x.lt.0.0) si = - s i

Mak ing this change and running the tests again gives error results in [ -11 , -10 ] of the same order of magni tude as the other intervals.

The N A G Ci code also gives disappoint ing results, especially compared to the F N code, as both use Chebyshev expansions. The N A G code approximates Ci over (0, 16] by In x + p(x) with p(x) expanded as a Chebyshev series, and essentially the asymptot ic expansion for x > 16. FN, however, uses l n x - 0.5 + q(x) with q(x) a Chebyshev series but only over (0,4], and then the asymptot ic form. The behaviour of Ci over an interval as big as (0,16] makes a good approxi- ma t ion difficult, and we would r ecommend splitting this interval for improved performance.

Page 12: Rational approximations, software and test methods for sine and cosine integrals

270 A.J. MacLeod / Computing sine and cosine integrals

6. S o f t w a r e

The following software is available directly from the author or from the N U M E R A L G O section of NETLIB. We describe the single-precision codes, and then just list the corresponding double-precision files.

1. COSINT.F - Evaluate Ci by a function called COSINT. 2. S ININT.F - Evaluate Si by a function called SININT. 3. CITESTSP.F - Test code for Ci software. This contains a main program

CITEST, and a subroutine CIDERS which computes the derivatives of Ci. 4. SITESTSP.F - Test code for Si software. This contains a main program

SITEST, and a subroutine SIDERS which computes the derivatives o f Si. 5. S I C I S U B S . F - Subprograms common to both test codes. These are a sub-

routine T E R M E Q to determine the number of derivatives needed, a function T L N T I N which solves t log t = c for given c, the M A C H A R subroutine, and a simple r andom-number function REN.

6. CINT.F - The CITEST program assumes the Ci function is computed by a function called CINT. For other names this file allows the user to define CINT to be the name of their particular code. In the codes provided CINT is defined to be the result of COSINT.

7. SINT.F - As for CINT but related to Si.

The double-precision files are essentially the same as their single-precision equivalents, so we only state their names, and the names of the corresponding components

1. D C O S I N T . F - Contains DCOSINT. 2. D S I N I N T . F - Contains DSININT. 3. CITESTDP.F - Contains DCITEST and DC ID E R . 4. SITESTDP.F - Contains DSITEST and DSIDER. 5. SICISUBD.F - Contains D T E R M S , DTLNTI , D M A C H R , and DREN. 6. D P C I N T . F - Contains DPCINT. 7. DPSINT.F - Contains DPSINT.

Also provided for Unix users (courtesy o f the Algori thms Editor) is a makefile program. Thus to implement the tests, the user should adopt one of the following strategies.

1. For testing the author 's functions (Ci and Si), simply use the makefile provided, for example, make eitest, make s i t e s t , . . .

2. For testing the author 's functions without the makefile (a) Combine (for Ci single precision) the files CITESTSP.F, SICISUBS.F,

CINT.F and COSINT.F. (b) Compile, link and run. and similarly for the other test programs.

3. For testing another function (Ci or Si) with the makefile, (for Ci single precision, as an example)

Page 13: Rational approximations, software and test methods for sine and cosine integrals

A.J. MacLeod/ Computing sine and cos&e &tegrals 271

.

(a) Edit CINT.F and change the name COSINT into the name of the new Ci function.

(b) Edit the M A K E F I L E and changes all the occurences of COSINT.F with the name of the new file provided, containing the new function Ci to be tested.

(c) Use the commands make citest. and similarly for the other test programs. For testing another function (Ci or Si) without the makefile, (for Ci single precision, as an example) (a) Edit CINT.F and change the name COSINT into the name of the new Ci

function. (b) Combine CITESTSP.F, SICISUBS.F, CINT.F and the new file containing

the new function Ci to be tested. (c) Compile, link and run. and similarly for the other test programs.

A c k n o w l e d g e m e n t s

The author would like to thank the referees and, especially, the Algorithms Editor for their very useful and detailed comments, which have helped to improve the presentation of both the paper and the associated software. Any remaining errors are, of course, the sole responsibility of the author.

References

[1] M. Abramowitz and I.A. Stegun (eds.), Handbook of Mathematical Functions, Dover, New York, 1965.

[2] C.W. Clenshaw, The numerical solution of linear differential equations in Chebyshev series, Proc. Camb. Phil. Soc. 53 (1957) 134-149.

[3] W.J. Cody, Algorithm 665: MACHAR: A subroutine to dynamically determine machine parameters, ACM Trans. Math. Soft. 14 (1988) 303-311.

[4] W.J. Cody, Algorithm 715: SPECFUN - A portable FORTRAN package of special function routines and test drivers, ACM Trans. Math. Softw. 19 (1993) 22-32.

[5] W.J. Cody, W. Fraser and J.F. Hart, Rational Chebyshev approximaion using linear equations, Numer. Math. 12 (1968) 242-251.

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

[7] J.J. Dongarra and E. Grosse, Distribution of mathematical software via electronic mail, Comm. ACM 30 (1987) 403-407.

[8] W. Gautschi, Algorithm 282 - derivatives of eX/x, cos x/x, and sin x/x, Comm. ACM 9 (1966) 272.

[9] W. Gautschi, Computation of successive derivatives of f(z)/z, Math. Comp. 20 (1966) 209-214.

Page 14: Rational approximations, software and test methods for sine and cosine integrals

272 A.J. MacLeod / Computing sine and cosine integrals

[10] W. Gautschi and B.J. Klein, Recursive computation of certain derivatives - a study of error propogation, Comm. ACM 13 (1970) 7-9.

[11] W. Gautschi and B.J. Klein, Remark on Algorithm 282 - Derivatives of eVx, cosx/x, and sinx/x, Comm. ACM 13 (1970) 53-54.

[12] J.F. Hart, E.W. Cheney, C.L. Lawson, H.J. Maehhly, C.K. Mesztenyi, J.R. Rice, H.G. Thacher and C. Witzgall, Computer Approximations, Wiley, New York, 1968.