C99 standard compliance for static const initialisers.
[qpms.git] / amos / zairy.f
blob1563d1d000746742f6be7551e29ca40a0c7de93f
1 SUBROUTINE ZAIRY(ZR, ZI, ID, KODE, AIR, AII, NZ, IERR)
2 C***BEGIN PROLOGUE ZAIRY
3 C***DATE WRITTEN 830501 (YYMMDD)
4 C***REVISION DATE 890801 (YYMMDD)
5 C***CATEGORY NO. B5K
6 C***KEYWORDS AIRY FUNCTION,BESSEL FUNCTIONS OF ORDER ONE THIRD
7 C***AUTHOR AMOS, DONALD E., SANDIA NATIONAL LABORATORIES
8 C***PURPOSE TO COMPUTE AIRY FUNCTIONS AI(Z) AND DAI(Z) FOR COMPLEX Z
9 C***DESCRIPTION
11 C ***A DOUBLE PRECISION ROUTINE***
12 C ON KODE=1, ZAIRY COMPUTES THE COMPLEX AIRY FUNCTION AI(Z) OR
13 C ITS DERIVATIVE DAI(Z)/DZ ON ID=0 OR ID=1 RESPECTIVELY. ON
14 C KODE=2, A SCALING OPTION CEXP(ZTA)*AI(Z) OR CEXP(ZTA)*
15 C DAI(Z)/DZ IS PROVIDED TO REMOVE THE EXPONENTIAL DECAY IN
16 C -PI/3.LT.ARG(Z).LT.PI/3 AND THE EXPONENTIAL GROWTH IN
17 C PI/3.LT.ABS(ARG(Z)).LT.PI WHERE ZTA=(2/3)*Z*CSQRT(Z).
19 C WHILE THE AIRY FUNCTIONS AI(Z) AND DAI(Z)/DZ ARE ANALYTIC IN
20 C THE WHOLE Z PLANE, THE CORRESPONDING SCALED FUNCTIONS DEFINED
21 C FOR KODE=2 HAVE A CUT ALONG THE NEGATIVE REAL AXIS.
22 C DEFINITIONS AND NOTATION ARE FOUND IN THE NBS HANDBOOK OF
23 C MATHEMATICAL FUNCTIONS (REF. 1).
25 C INPUT ZR,ZI ARE DOUBLE PRECISION
26 C ZR,ZI - Z=CMPLX(ZR,ZI)
27 C ID - ORDER OF DERIVATIVE, ID=0 OR ID=1
28 C KODE - A PARAMETER TO INDICATE THE SCALING OPTION
29 C KODE= 1 RETURNS
30 C AI=AI(Z) ON ID=0 OR
31 C AI=DAI(Z)/DZ ON ID=1
32 C = 2 RETURNS
33 C AI=CEXP(ZTA)*AI(Z) ON ID=0 OR
34 C AI=CEXP(ZTA)*DAI(Z)/DZ ON ID=1 WHERE
35 C ZTA=(2/3)*Z*CSQRT(Z)
37 C OUTPUT AIR,AII ARE DOUBLE PRECISION
38 C AIR,AII- COMPLEX ANSWER DEPENDING ON THE CHOICES FOR ID AND
39 C KODE
40 C NZ - UNDERFLOW INDICATOR
41 C NZ= 0 , NORMAL RETURN
42 C NZ= 1 , AI=CMPLX(0.0D0,0.0D0) DUE TO UNDERFLOW IN
43 C -PI/3.LT.ARG(Z).LT.PI/3 ON KODE=1
44 C IERR - ERROR FLAG
45 C IERR=0, NORMAL RETURN - COMPUTATION COMPLETED
46 C IERR=1, INPUT ERROR - NO COMPUTATION
47 C IERR=2, OVERFLOW - NO COMPUTATION, REAL(ZTA)
48 C TOO LARGE ON KODE=1
49 C IERR=3, CABS(Z) LARGE - COMPUTATION COMPLETED
50 C LOSSES OF SIGNIFCANCE BY ARGUMENT REDUCTION
51 C PRODUCE LESS THAN HALF OF MACHINE ACCURACY
52 C IERR=4, CABS(Z) TOO LARGE - NO COMPUTATION
53 C COMPLETE LOSS OF ACCURACY BY ARGUMENT
54 C REDUCTION
55 C IERR=5, ERROR - NO COMPUTATION,
56 C ALGORITHM TERMINATION CONDITION NOT MET
58 C***LONG DESCRIPTION
60 C AI AND DAI ARE COMPUTED FOR CABS(Z).GT.1.0 FROM THE K BESSEL
61 C FUNCTIONS BY
63 C AI(Z)=C*SQRT(Z)*K(1/3,ZTA) , DAI(Z)=-C*Z*K(2/3,ZTA)
64 C C=1.0/(PI*SQRT(3.0))
65 C ZTA=(2/3)*Z**(3/2)
67 C WITH THE POWER SERIES FOR CABS(Z).LE.1.0.
69 C IN MOST COMPLEX VARIABLE COMPUTATION, ONE MUST EVALUATE ELE-
70 C MENTARY FUNCTIONS. WHEN THE MAGNITUDE OF Z IS LARGE, LOSSES
71 C OF SIGNIFICANCE BY ARGUMENT REDUCTION OCCUR. CONSEQUENTLY, IF
72 C THE MAGNITUDE OF ZETA=(2/3)*Z**1.5 EXCEEDS U1=SQRT(0.5/UR),
73 C THEN LOSSES EXCEEDING HALF PRECISION ARE LIKELY AND AN ERROR
74 C FLAG IERR=3 IS TRIGGERED WHERE UR=DMAX1(D1MACH(4),1.0D-18) IS
75 C DOUBLE PRECISION UNIT ROUNDOFF LIMITED TO 18 DIGITS PRECISION.
76 C ALSO, IF THE MAGNITUDE OF ZETA IS LARGER THAN U2=0.5/UR, THEN
77 C ALL SIGNIFICANCE IS LOST AND IERR=4. IN ORDER TO USE THE INT
78 C FUNCTION, ZETA MUST BE FURTHER RESTRICTED NOT TO EXCEED THE
79 C LARGEST INTEGER, U3=I1MACH(9). THUS, THE MAGNITUDE OF ZETA
80 C MUST BE RESTRICTED BY MIN(U2,U3). ON 32 BIT MACHINES, U1,U2,
81 C AND U3 ARE APPROXIMATELY 2.0E+3, 4.2E+6, 2.1E+9 IN SINGLE
82 C PRECISION ARITHMETIC AND 1.3E+8, 1.8E+16, 2.1E+9 IN DOUBLE
83 C PRECISION ARITHMETIC RESPECTIVELY. THIS MAKES U2 AND U3 LIMIT-
84 C ING IN THEIR RESPECTIVE ARITHMETICS. THIS MEANS THAT THE MAG-
85 C NITUDE OF Z CANNOT EXCEED 3.1E+4 IN SINGLE AND 2.1E+6 IN
86 C DOUBLE PRECISION ARITHMETIC. THIS ALSO MEANS THAT ONE CAN
87 C EXPECT TO RETAIN, IN THE WORST CASES ON 32 BIT MACHINES,
88 C NO DIGITS IN SINGLE PRECISION AND ONLY 7 DIGITS IN DOUBLE
89 C PRECISION ARITHMETIC. SIMILAR CONSIDERATIONS HOLD FOR OTHER
90 C MACHINES.
92 C THE APPROXIMATE RELATIVE ERROR IN THE MAGNITUDE OF A COMPLEX
93 C BESSEL FUNCTION CAN BE EXPRESSED BY P*10**S WHERE P=MAX(UNIT
94 C ROUNDOFF,1.0E-18) IS THE NOMINAL PRECISION AND 10**S REPRE-
95 C SENTS THE INCREASE IN ERROR DUE TO ARGUMENT REDUCTION IN THE
96 C ELEMENTARY FUNCTIONS. HERE, S=MAX(1,ABS(LOG10(CABS(Z))),
97 C ABS(LOG10(FNU))) APPROXIMATELY (I.E. S=MAX(1,ABS(EXPONENT OF
98 C CABS(Z),ABS(EXPONENT OF FNU)) ). HOWEVER, THE PHASE ANGLE MAY
99 C HAVE ONLY ABSOLUTE ACCURACY. THIS IS MOST LIKELY TO OCCUR WHEN
100 C ONE COMPONENT (IN ABSOLUTE VALUE) IS LARGER THAN THE OTHER BY
101 C SEVERAL ORDERS OF MAGNITUDE. IF ONE COMPONENT IS 10**K LARGER
102 C THAN THE OTHER, THEN ONE CAN EXPECT ONLY MAX(ABS(LOG10(P))-K,
103 C 0) SIGNIFICANT DIGITS; OR, STATED ANOTHER WAY, WHEN K EXCEEDS
104 C THE EXPONENT OF P, NO SIGNIFICANT DIGITS REMAIN IN THE SMALLER
105 C COMPONENT. HOWEVER, THE PHASE ANGLE RETAINS ABSOLUTE ACCURACY
106 C BECAUSE, IN COMPLEX ARITHMETIC WITH PRECISION P, THE SMALLER
107 C COMPONENT WILL NOT (AS A RULE) DECREASE BELOW P TIMES THE
108 C MAGNITUDE OF THE LARGER COMPONENT. IN THESE EXTREME CASES,
109 C THE PRINCIPAL PHASE ANGLE IS ON THE ORDER OF +P, -P, PI/2-P,
110 C OR -PI/2+P.
112 C***REFERENCES HANDBOOK OF MATHEMATICAL FUNCTIONS BY M. ABRAMOWITZ
113 C AND I. A. STEGUN, NBS AMS SERIES 55, U.S. DEPT. OF
114 C COMMERCE, 1955.
116 C COMPUTATION OF BESSEL FUNCTIONS OF COMPLEX ARGUMENT
117 C AND LARGE ORDER BY D. E. AMOS, SAND83-0643, MAY, 1983
119 C A SUBROUTINE PACKAGE FOR BESSEL FUNCTIONS OF A COMPLEX
120 C ARGUMENT AND NONNEGATIVE ORDER BY D. E. AMOS, SAND85-
121 C 1018, MAY, 1985
123 C A PORTABLE PACKAGE FOR BESSEL FUNCTIONS OF A COMPLEX
124 C ARGUMENT AND NONNEGATIVE ORDER BY D. E. AMOS, TRANS.
125 C MATH. SOFTWARE, 1986
127 C***ROUTINES CALLED ZACAI,ZBKNU,AZEXP,AZSQRT,I1MACH,D1MACH
128 C***END PROLOGUE ZAIRY
129 C COMPLEX AI,CONE,CSQ,CY,S1,S2,TRM1,TRM2,Z,ZTA,Z3
130 DOUBLE PRECISION AA, AD, AII, AIR, AK, ALIM, ATRM, AZ, AZ3, BK,
131 * CC, CK, COEF, CONEI, CONER, CSQI, CSQR, CYI, CYR, C1, C2, DIG,
132 * DK, D1, D2, ELIM, FID, FNU, PTR, RL, R1M5, SFAC, STI, STR,
133 * S1I, S1R, S2I, S2R, TOL, TRM1I, TRM1R, TRM2I, TRM2R, TTH, ZEROI,
134 * ZEROR, ZI, ZR, ZTAI, ZTAR, Z3I, Z3R, D1MACH, AZABS, ALAZ, BB
135 INTEGER ID, IERR, IFLAG, K, KODE, K1, K2, MR, NN, NZ, I1MACH
136 DIMENSION CYR(1), CYI(1)
137 DATA TTH, C1, C2, COEF /6.66666666666666667D-01,
138 * 3.55028053887817240D-01,2.58819403792806799D-01,
139 * 1.83776298473930683D-01/
140 DATA ZEROR, ZEROI, CONER, CONEI /0.0D0,0.0D0,1.0D0,0.0D0/
141 C***FIRST EXECUTABLE STATEMENT ZAIRY
142 IERR = 0
143 NZ=0
144 IF (ID.LT.0 .OR. ID.GT.1) IERR=1
145 IF (KODE.LT.1 .OR. KODE.GT.2) IERR=1
146 IF (IERR.NE.0) RETURN
147 AZ = AZABS(ZR,ZI)
148 TOL = DMAX1(D1MACH(4),1.0D-18)
149 FID = DBLE(FLOAT(ID))
150 IF (AZ.GT.1.0D0) GO TO 70
151 C-----------------------------------------------------------------------
152 C POWER SERIES FOR CABS(Z).LE.1.
153 C-----------------------------------------------------------------------
154 S1R = CONER
155 S1I = CONEI
156 S2R = CONER
157 S2I = CONEI
158 IF (AZ.LT.TOL) GO TO 170
159 AA = AZ*AZ
160 IF (AA.LT.TOL/AZ) GO TO 40
161 TRM1R = CONER
162 TRM1I = CONEI
163 TRM2R = CONER
164 TRM2I = CONEI
165 ATRM = 1.0D0
166 STR = ZR*ZR - ZI*ZI
167 STI = ZR*ZI + ZI*ZR
168 Z3R = STR*ZR - STI*ZI
169 Z3I = STR*ZI + STI*ZR
170 AZ3 = AZ*AA
171 AK = 2.0D0 + FID
172 BK = 3.0D0 - FID - FID
173 CK = 4.0D0 - FID
174 DK = 3.0D0 + FID + FID
175 D1 = AK*DK
176 D2 = BK*CK
177 AD = DMIN1(D1,D2)
178 AK = 24.0D0 + 9.0D0*FID
179 BK = 30.0D0 - 9.0D0*FID
180 DO 30 K=1,25
181 STR = (TRM1R*Z3R-TRM1I*Z3I)/D1
182 TRM1I = (TRM1R*Z3I+TRM1I*Z3R)/D1
183 TRM1R = STR
184 S1R = S1R + TRM1R
185 S1I = S1I + TRM1I
186 STR = (TRM2R*Z3R-TRM2I*Z3I)/D2
187 TRM2I = (TRM2R*Z3I+TRM2I*Z3R)/D2
188 TRM2R = STR
189 S2R = S2R + TRM2R
190 S2I = S2I + TRM2I
191 ATRM = ATRM*AZ3/AD
192 D1 = D1 + AK
193 D2 = D2 + BK
194 AD = DMIN1(D1,D2)
195 IF (ATRM.LT.TOL*AD) GO TO 40
196 AK = AK + 18.0D0
197 BK = BK + 18.0D0
198 30 CONTINUE
199 40 CONTINUE
200 IF (ID.EQ.1) GO TO 50
201 AIR = S1R*C1 - C2*(ZR*S2R-ZI*S2I)
202 AII = S1I*C1 - C2*(ZR*S2I+ZI*S2R)
203 IF (KODE.EQ.1) RETURN
204 CALL AZSQRT(ZR, ZI, STR, STI)
205 ZTAR = TTH*(ZR*STR-ZI*STI)
206 ZTAI = TTH*(ZR*STI+ZI*STR)
207 CALL AZEXP(ZTAR, ZTAI, STR, STI)
208 PTR = AIR*STR - AII*STI
209 AII = AIR*STI + AII*STR
210 AIR = PTR
211 RETURN
212 50 CONTINUE
213 AIR = -S2R*C2
214 AII = -S2I*C2
215 IF (AZ.LE.TOL) GO TO 60
216 STR = ZR*S1R - ZI*S1I
217 STI = ZR*S1I + ZI*S1R
218 CC = C1/(1.0D0+FID)
219 AIR = AIR + CC*(STR*ZR-STI*ZI)
220 AII = AII + CC*(STR*ZI+STI*ZR)
221 60 CONTINUE
222 IF (KODE.EQ.1) RETURN
223 CALL AZSQRT(ZR, ZI, STR, STI)
224 ZTAR = TTH*(ZR*STR-ZI*STI)
225 ZTAI = TTH*(ZR*STI+ZI*STR)
226 CALL AZEXP(ZTAR, ZTAI, STR, STI)
227 PTR = STR*AIR - STI*AII
228 AII = STR*AII + STI*AIR
229 AIR = PTR
230 RETURN
231 C-----------------------------------------------------------------------
232 C CASE FOR CABS(Z).GT.1.0
233 C-----------------------------------------------------------------------
234 70 CONTINUE
235 FNU = (1.0D0+FID)/3.0D0
236 C-----------------------------------------------------------------------
237 C SET PARAMETERS RELATED TO MACHINE CONSTANTS.
238 C TOL IS THE APPROXIMATE UNIT ROUNDOFF LIMITED TO 1.0D-18.
239 C ELIM IS THE APPROXIMATE EXPONENTIAL OVER- AND UNDERFLOW LIMIT.
240 C EXP(-ELIM).LT.EXP(-ALIM)=EXP(-ELIM)/TOL AND
241 C EXP(ELIM).GT.EXP(ALIM)=EXP(ELIM)*TOL ARE INTERVALS NEAR
242 C UNDERFLOW AND OVERFLOW LIMITS WHERE SCALED ARITHMETIC IS DONE.
243 C RL IS THE LOWER BOUNDARY OF THE ASYMPTOTIC EXPANSION FOR LARGE Z.
244 C DIG = NUMBER OF BASE 10 DIGITS IN TOL = 10**(-DIG).
245 C-----------------------------------------------------------------------
246 K1 = I1MACH(15)
247 K2 = I1MACH(16)
248 R1M5 = D1MACH(5)
249 K = MIN0(IABS(K1),IABS(K2))
250 ELIM = 2.303D0*(DBLE(FLOAT(K))*R1M5-3.0D0)
251 K1 = I1MACH(14) - 1
252 AA = R1M5*DBLE(FLOAT(K1))
253 DIG = DMIN1(AA,18.0D0)
254 AA = AA*2.303D0
255 ALIM = ELIM + DMAX1(-AA,-41.45D0)
256 RL = 1.2D0*DIG + 3.0D0
257 ALAZ = DLOG(AZ)
258 C--------------------------------------------------------------------------
259 C TEST FOR PROPER RANGE
260 C-----------------------------------------------------------------------
261 AA=0.5D0/TOL
262 BB=DBLE(FLOAT(I1MACH(9)))*0.5D0
263 AA=DMIN1(AA,BB)
264 AA=AA**TTH
265 IF (AZ.GT.AA) GO TO 260
266 AA=DSQRT(AA)
267 IF (AZ.GT.AA) IERR=3
268 CALL AZSQRT(ZR, ZI, CSQR, CSQI)
269 ZTAR = TTH*(ZR*CSQR-ZI*CSQI)
270 ZTAI = TTH*(ZR*CSQI+ZI*CSQR)
271 C-----------------------------------------------------------------------
272 C RE(ZTA).LE.0 WHEN RE(Z).LT.0, ESPECIALLY WHEN IM(Z) IS SMALL
273 C-----------------------------------------------------------------------
274 IFLAG = 0
275 SFAC = 1.0D0
276 AK = ZTAI
277 IF (ZR.GE.0.0D0) GO TO 80
278 BK = ZTAR
279 CK = -DABS(BK)
280 ZTAR = CK
281 ZTAI = AK
282 80 CONTINUE
283 IF (ZI.NE.0.0D0) GO TO 90
284 IF (ZR.GT.0.0D0) GO TO 90
285 ZTAR = 0.0D0
286 ZTAI = AK
287 90 CONTINUE
288 AA = ZTAR
289 IF (AA.GE.0.0D0 .AND. ZR.GT.0.0D0) GO TO 110
290 IF (KODE.EQ.2) GO TO 100
291 C-----------------------------------------------------------------------
292 C OVERFLOW TEST
293 C-----------------------------------------------------------------------
294 IF (AA.GT.(-ALIM)) GO TO 100
295 AA = -AA + 0.25D0*ALAZ
296 IFLAG = 1
297 SFAC = TOL
298 IF (AA.GT.ELIM) GO TO 270
299 100 CONTINUE
300 C-----------------------------------------------------------------------
301 C CBKNU AND CACON RETURN EXP(ZTA)*K(FNU,ZTA) ON KODE=2
302 C-----------------------------------------------------------------------
303 MR = 1
304 IF (ZI.LT.0.0D0) MR = -1
305 CALL ZACAI(ZTAR, ZTAI, FNU, KODE, MR, 1, CYR, CYI, NN, RL, TOL,
306 * ELIM, ALIM)
307 IF (NN.LT.0) GO TO 280
308 NZ = NZ + NN
309 GO TO 130
310 110 CONTINUE
311 IF (KODE.EQ.2) GO TO 120
312 C-----------------------------------------------------------------------
313 C UNDERFLOW TEST
314 C-----------------------------------------------------------------------
315 IF (AA.LT.ALIM) GO TO 120
316 AA = -AA - 0.25D0*ALAZ
317 IFLAG = 2
318 SFAC = 1.0D0/TOL
319 IF (AA.LT.(-ELIM)) GO TO 210
320 120 CONTINUE
321 CALL ZBKNU(ZTAR, ZTAI, FNU, KODE, 1, CYR, CYI, NZ, TOL, ELIM,
322 * ALIM)
323 130 CONTINUE
324 S1R = CYR(1)*COEF
325 S1I = CYI(1)*COEF
326 IF (IFLAG.NE.0) GO TO 150
327 IF (ID.EQ.1) GO TO 140
328 AIR = CSQR*S1R - CSQI*S1I
329 AII = CSQR*S1I + CSQI*S1R
330 RETURN
331 140 CONTINUE
332 AIR = -(ZR*S1R-ZI*S1I)
333 AII = -(ZR*S1I+ZI*S1R)
334 RETURN
335 150 CONTINUE
336 S1R = S1R*SFAC
337 S1I = S1I*SFAC
338 IF (ID.EQ.1) GO TO 160
339 STR = S1R*CSQR - S1I*CSQI
340 S1I = S1R*CSQI + S1I*CSQR
341 S1R = STR
342 AIR = S1R/SFAC
343 AII = S1I/SFAC
344 RETURN
345 160 CONTINUE
346 STR = -(S1R*ZR-S1I*ZI)
347 S1I = -(S1R*ZI+S1I*ZR)
348 S1R = STR
349 AIR = S1R/SFAC
350 AII = S1I/SFAC
351 RETURN
352 170 CONTINUE
353 AA = 1.0D+3*D1MACH(1)
354 S1R = ZEROR
355 S1I = ZEROI
356 IF (ID.EQ.1) GO TO 190
357 IF (AZ.LE.AA) GO TO 180
358 S1R = C2*ZR
359 S1I = C2*ZI
360 180 CONTINUE
361 AIR = C1 - S1R
362 AII = -S1I
363 RETURN
364 190 CONTINUE
365 AIR = -C2
366 AII = 0.0D0
367 AA = DSQRT(AA)
368 IF (AZ.LE.AA) GO TO 200
369 S1R = 0.5D0*(ZR*ZR-ZI*ZI)
370 S1I = ZR*ZI
371 200 CONTINUE
372 AIR = AIR + C1*S1R
373 AII = AII + C1*S1I
374 RETURN
375 210 CONTINUE
376 NZ = 1
377 AIR = ZEROR
378 AII = ZEROI
379 RETURN
380 270 CONTINUE
381 NZ = 0
382 IERR=2
383 RETURN
384 280 CONTINUE
385 IF(NN.EQ.(-1)) GO TO 270
386 NZ=0
387 IERR=5
388 RETURN
389 260 CONTINUE
390 IERR=4
391 NZ=0
392 RETURN