cbesk.f 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. *DECK CBESK
  2. SUBROUTINE CBESK (Z, FNU, KODE, N, CY, NZ, IERR)
  3. C***BEGIN PROLOGUE CBESK
  4. C***PURPOSE Compute a sequence of the Bessel functions K(a,z) for
  5. C complex argument z and real nonnegative orders a=b,b+1,
  6. C b+2,... where b>0. A scaling option is available to
  7. C help avoid overflow.
  8. C***LIBRARY SLATEC
  9. C***CATEGORY C10B4
  10. C***TYPE COMPLEX (CBESK-C, ZBESK-C)
  11. C***KEYWORDS BESSEL FUNCTIONS OF COMPLEX ARGUMENT, K BESSEL FUNCTIONS,
  12. C MODIFIED BESSEL FUNCTIONS
  13. C***AUTHOR Amos, D. E., (SNL)
  14. C***DESCRIPTION
  15. C
  16. C On KODE=1, CBESK computes an N member sequence of complex
  17. C Bessel functions CY(L)=K(FNU+L-1,Z) for real nonnegative
  18. C orders FNU+L-1, L=1,...,N and complex Z.NE.0 in the cut
  19. C plane -pi<arg(Z)<=pi. On KODE=2, CBESJ returns the scaled
  20. C functions
  21. C
  22. C CY(L) = exp(Z)*K(FNU+L-1,Z), L=1,...,N
  23. C
  24. C which remove the exponential growth in both the left and
  25. C right half planes as Z goes to infinity. Definitions and
  26. C notation are found in the NBS Handbook of Mathematical
  27. C Functions (Ref. 1).
  28. C
  29. C Input
  30. C Z - Nonzero argument of type COMPLEX
  31. C FNU - Initial order of type REAL, FNU>=0
  32. C KODE - A parameter to indicate the scaling option
  33. C KODE=1 returns
  34. C CY(L)=K(FNU+L-1,Z), L=1,...,N
  35. C =2 returns
  36. C CY(L)=K(FNU+L-1,Z)*EXP(Z), L=1,...,N
  37. C N - Number of terms in the sequence, N>=1
  38. C
  39. C Output
  40. C CY - Result vector of type COMPLEX
  41. C NZ - Number of underflows set to zero
  42. C NZ=0 Normal return
  43. C NZ>0 CY(L)=0 for NZ values of L (if Re(Z)>0
  44. C then CY(L)=0 for L=1,...,NZ; in the
  45. C complementary half plane the underflows
  46. C may not be in an uninterrupted sequence)
  47. C IERR - Error flag
  48. C IERR=0 Normal return - COMPUTATION COMPLETED
  49. C IERR=1 Input error - NO COMPUTATION
  50. C IERR=2 Overflow - NO COMPUTATION
  51. C (abs(Z) too small and/or FNU+N-1
  52. C too large)
  53. C IERR=3 Precision warning - COMPUTATION COMPLETED
  54. C (Result has half precision or less
  55. C because abs(Z) or FNU+N-1 is large)
  56. C IERR=4 Precision error - NO COMPUTATION
  57. C (Result has no precision because
  58. C abs(Z) or FNU+N-1 is too large)
  59. C IERR=5 Algorithmic error - NO COMPUTATION
  60. C (Termination condition not met)
  61. C
  62. C *Long Description:
  63. C
  64. C Equations of the reference are implemented to compute K(a,z)
  65. C for small orders a and a+1 in the right half plane Re(z)>=0.
  66. C Forward recurrence generates higher orders. The formula
  67. C
  68. C K(a,z*exp((t)) = exp(-t)*K(a,z) - t*I(a,z), Re(z)>0
  69. C t = i*pi or -i*pi
  70. C
  71. C continues K to the left half plane.
  72. C
  73. C For large orders, K(a,z) is computed by means of its uniform
  74. C asymptotic expansion.
  75. C
  76. C For negative orders, the formula
  77. C
  78. C K(-a,z) = K(a,z)
  79. C
  80. C can be used.
  81. C
  82. C CBESK assumes that a significant digit sinh function is
  83. C available.
  84. C
  85. C In most complex variable computation, one must evaluate ele-
  86. C mentary functions. When the magnitude of Z or FNU+N-1 is
  87. C large, losses of significance by argument reduction occur.
  88. C Consequently, if either one exceeds U1=SQRT(0.5/UR), then
  89. C losses exceeding half precision are likely and an error flag
  90. C IERR=3 is triggered where UR=R1MACH(4)=UNIT ROUNDOFF. Also,
  91. C if either is larger than U2=0.5/UR, then all significance is
  92. C lost and IERR=4. In order to use the INT function, arguments
  93. C must be further restricted not to exceed the largest machine
  94. C integer, U3=I1MACH(9). Thus, the magnitude of Z and FNU+N-1
  95. C is restricted by MIN(U2,U3). In IEEE arithmetic, U1,U2, and
  96. C U3 approximate 2.0E+3, 4.2E+6, 2.1E+9 in single precision
  97. C and 4.7E+7, 2.3E+15 and 2.1E+9 in double precision. This
  98. C makes U2 limiting in single precision and U3 limiting in
  99. C double precision. This means that one can expect to retain,
  100. C in the worst cases on IEEE machines, no digits in single pre-
  101. C cision and only 6 digits in double precision. Similar con-
  102. C siderations hold for other machines.
  103. C
  104. C The approximate relative error in the magnitude of a complex
  105. C Bessel function can be expressed as P*10**S where P=MAX(UNIT
  106. C ROUNDOFF,1.0E-18) is the nominal precision and 10**S repre-
  107. C sents the increase in error due to argument reduction in the
  108. C elementary functions. Here, S=MAX(1,ABS(LOG10(ABS(Z))),
  109. C ABS(LOG10(FNU))) approximately (i.e., S=MAX(1,ABS(EXPONENT OF
  110. C ABS(Z),ABS(EXPONENT OF FNU)) ). However, the phase angle may
  111. C have only absolute accuracy. This is most likely to occur
  112. C when one component (in magnitude) is larger than the other by
  113. C several orders of magnitude. If one component is 10**K larger
  114. C than the other, then one can expect only MAX(ABS(LOG10(P))-K,
  115. C 0) significant digits; or, stated another way, when K exceeds
  116. C the exponent of P, no significant digits remain in the smaller
  117. C component. However, the phase angle retains absolute accuracy
  118. C because, in complex arithmetic with precision P, the smaller
  119. C component will not (as a rule) decrease below P times the
  120. C magnitude of the larger component. In these extreme cases,
  121. C the principal phase angle is on the order of +P, -P, PI/2-P,
  122. C or -PI/2+P.
  123. C
  124. C***REFERENCES 1. M. Abramowitz and I. A. Stegun, Handbook of Mathe-
  125. C matical Functions, National Bureau of Standards
  126. C Applied Mathematics Series 55, U. S. Department
  127. C of Commerce, Tenth Printing (1972) or later.
  128. C 2. D. E. Amos, Computation of Bessel Functions of
  129. C Complex Argument, Report SAND83-0086, Sandia National
  130. C Laboratories, Albuquerque, NM, May 1983.
  131. C 3. D. E. Amos, Computation of Bessel Functions of
  132. C Complex Argument and Large Order, Report SAND83-0643,
  133. C Sandia National Laboratories, Albuquerque, NM, May
  134. C 1983.
  135. C 4. D. E. Amos, A Subroutine Package for Bessel Functions
  136. C of a Complex Argument and Nonnegative Order, Report
  137. C SAND85-1018, Sandia National Laboratory, Albuquerque,
  138. C NM, May 1985.
  139. C 5. D. E. Amos, A portable package for Bessel functions
  140. C of a complex argument and nonnegative order, ACM
  141. C Transactions on Mathematical Software, 12 (September
  142. C 1986), pp. 265-273.
  143. C
  144. C***ROUTINES CALLED CACON, CBKNU, CBUNK, CUOIK, I1MACH, R1MACH
  145. C***REVISION HISTORY (YYMMDD)
  146. C 830501 DATE WRITTEN
  147. C 890801 REVISION DATE from Version 3.2
  148. C 910415 Prologue converted to Version 4.0 format. (BAB)
  149. C 920128 Category corrected. (WRB)
  150. C 920811 Prologue revised. (DWL)
  151. C***END PROLOGUE CBESK
  152. C
  153. COMPLEX CY, Z
  154. REAL AA, ALIM, ALN, ARG, AZ, DIG, ELIM, FN, FNU, FNUL, RL, R1M5,
  155. * TOL, UFL, XX, YY, R1MACH, BB
  156. INTEGER IERR, K, KODE, K1, K2, MR, N, NN, NUF, NW, NZ, I1MACH
  157. DIMENSION CY(N)
  158. C***FIRST EXECUTABLE STATEMENT CBESK
  159. IERR = 0
  160. NZ=0
  161. XX = REAL(Z)
  162. YY = AIMAG(Z)
  163. IF (YY.EQ.0.0E0 .AND. XX.EQ.0.0E0) IERR=1
  164. IF (FNU.LT.0.0E0) IERR=1
  165. IF (KODE.LT.1 .OR. KODE.GT.2) IERR=1
  166. IF (N.LT.1) IERR=1
  167. IF (IERR.NE.0) RETURN
  168. NN = N
  169. C-----------------------------------------------------------------------
  170. C SET PARAMETERS RELATED TO MACHINE CONSTANTS.
  171. C TOL IS THE APPROXIMATE UNIT ROUNDOFF LIMITED TO 1.0E-18.
  172. C ELIM IS THE APPROXIMATE EXPONENTIAL OVER- AND UNDERFLOW LIMIT.
  173. C EXP(-ELIM).LT.EXP(-ALIM)=EXP(-ELIM)/TOL AND
  174. C EXP(ELIM).GT.EXP(ALIM)=EXP(ELIM)*TOL ARE INTERVALS NEAR
  175. C UNDERFLOW AND OVERFLOW LIMITS WHERE SCALED ARITHMETIC IS DONE.
  176. C RL IS THE LOWER BOUNDARY OF THE ASYMPTOTIC EXPANSION FOR LARGE Z.
  177. C DIG = NUMBER OF BASE 10 DIGITS IN TOL = 10**(-DIG).
  178. C FNUL IS THE LOWER BOUNDARY OF THE ASYMPTOTIC SERIES FOR LARGE FNU
  179. C-----------------------------------------------------------------------
  180. TOL = MAX(R1MACH(4),1.0E-18)
  181. K1 = I1MACH(12)
  182. K2 = I1MACH(13)
  183. R1M5 = R1MACH(5)
  184. K = MIN(ABS(K1),ABS(K2))
  185. ELIM = 2.303E0*(K*R1M5-3.0E0)
  186. K1 = I1MACH(11) - 1
  187. AA = R1M5*K1
  188. DIG = MIN(AA,18.0E0)
  189. AA = AA*2.303E0
  190. ALIM = ELIM + MAX(-AA,-41.45E0)
  191. FNUL = 10.0E0 + 6.0E0*(DIG-3.0E0)
  192. RL = 1.2E0*DIG + 3.0E0
  193. AZ = ABS(Z)
  194. FN = FNU + (NN-1)
  195. C-----------------------------------------------------------------------
  196. C TEST FOR RANGE
  197. C-----------------------------------------------------------------------
  198. AA = 0.5E0/TOL
  199. BB=I1MACH(9)*0.5E0
  200. AA=MIN(AA,BB)
  201. IF(AZ.GT.AA) GO TO 210
  202. IF(FN.GT.AA) GO TO 210
  203. AA=SQRT(AA)
  204. IF(AZ.GT.AA) IERR=3
  205. IF(FN.GT.AA) IERR=3
  206. C-----------------------------------------------------------------------
  207. C OVERFLOW TEST ON THE LAST MEMBER OF THE SEQUENCE
  208. C-----------------------------------------------------------------------
  209. C UFL = EXP(-ELIM)
  210. UFL = R1MACH(1)*1.0E+3
  211. IF (AZ.LT.UFL) GO TO 180
  212. IF (FNU.GT.FNUL) GO TO 80
  213. IF (FN.LE.1.0E0) GO TO 60
  214. IF (FN.GT.2.0E0) GO TO 50
  215. IF (AZ.GT.TOL) GO TO 60
  216. ARG = 0.5E0*AZ
  217. ALN = -FN*ALOG(ARG)
  218. IF (ALN.GT.ELIM) GO TO 180
  219. GO TO 60
  220. 50 CONTINUE
  221. CALL CUOIK(Z, FNU, KODE, 2, NN, CY, NUF, TOL, ELIM, ALIM)
  222. IF (NUF.LT.0) GO TO 180
  223. NZ = NZ + NUF
  224. NN = NN - NUF
  225. C-----------------------------------------------------------------------
  226. C HERE NN=N OR NN=0 SINCE NUF=0,NN, OR -1 ON RETURN FROM CUOIK
  227. C IF NUF=NN, THEN CY(I)=CZERO FOR ALL I
  228. C-----------------------------------------------------------------------
  229. IF (NN.EQ.0) GO TO 100
  230. 60 CONTINUE
  231. IF (XX.LT.0.0E0) GO TO 70
  232. C-----------------------------------------------------------------------
  233. C RIGHT HALF PLANE COMPUTATION, REAL(Z).GE.0.
  234. C-----------------------------------------------------------------------
  235. CALL CBKNU(Z, FNU, KODE, NN, CY, NW, TOL, ELIM, ALIM)
  236. IF (NW.LT.0) GO TO 200
  237. NZ=NW
  238. RETURN
  239. C-----------------------------------------------------------------------
  240. C LEFT HALF PLANE COMPUTATION
  241. C PI/2.LT.ARG(Z).LE.PI AND -PI.LT.ARG(Z).LT.-PI/2.
  242. C-----------------------------------------------------------------------
  243. 70 CONTINUE
  244. IF (NZ.NE.0) GO TO 180
  245. MR = 1
  246. IF (YY.LT.0.0E0) MR = -1
  247. CALL CACON(Z, FNU, KODE, MR, NN, CY, NW, RL, FNUL, TOL, ELIM,
  248. * ALIM)
  249. IF (NW.LT.0) GO TO 200
  250. NZ=NW
  251. RETURN
  252. C-----------------------------------------------------------------------
  253. C UNIFORM ASYMPTOTIC EXPANSIONS FOR FNU.GT.FNUL
  254. C-----------------------------------------------------------------------
  255. 80 CONTINUE
  256. MR = 0
  257. IF (XX.GE.0.0E0) GO TO 90
  258. MR = 1
  259. IF (YY.LT.0.0E0) MR = -1
  260. 90 CONTINUE
  261. CALL CBUNK(Z, FNU, KODE, MR, NN, CY, NW, TOL, ELIM, ALIM)
  262. IF (NW.LT.0) GO TO 200
  263. NZ = NZ + NW
  264. RETURN
  265. 100 CONTINUE
  266. IF (XX.LT.0.0E0) GO TO 180
  267. RETURN
  268. 180 CONTINUE
  269. NZ = 0
  270. IERR=2
  271. RETURN
  272. 200 CONTINUE
  273. IF(NW.EQ.(-1)) GO TO 180
  274. NZ=0
  275. IERR=5
  276. RETURN
  277. 210 CONTINUE
  278. NZ=0
  279. IERR=4
  280. RETURN
  281. END