zbesj.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /* zbesj.f -- translated by f2c (version 20100827).
  2. This file no longer depends on f2c.
  3. */
  4. #include "slatec-internal.hpp"
  5. /* Table of constant values */
  6. integer const c__4 = 4;
  7. integer const c__15 = 15;
  8. integer const c__16 = 16;
  9. integer const c__5 = 5;
  10. integer const c__14 = 14;
  11. integer const c__9 = 9;
  12. integer const c__1 = 1;
  13. int zbesj_(double *zr, double *zi, double const *fnu,
  14. integer const *kode, integer const *n, double *cyr, double *cyi, integer *
  15. nz, integer *ierr)
  16. {
  17. /* Initialized data */
  18. constexpr double hpi = 1.57079632679489662;
  19. /* System generated locals */
  20. integer i__1, i__2;
  21. double d__1, d__2;
  22. /* Local variables */
  23. integer i__, k, k1, k2;
  24. double aa, bb, fn;
  25. integer nl;
  26. double az;
  27. integer ir;
  28. double rl, dig, cii, arg, r1m5;
  29. integer inu;
  30. double tol, sti, zni, str, znr, alim, elim;
  31. double atol;
  32. integer inuh;
  33. double fnul, rtol, ascle, csgni, csgnr;
  34. /* ***BEGIN PROLOGUE ZBESJ */
  35. /* ***PURPOSE Compute a sequence of the Bessel functions J(a,z) for */
  36. /* complex argument z and real nonnegative orders a=b,b+1, */
  37. /* b+2,... where b>0. A scaling option is available to */
  38. /* help avoid overflow. */
  39. /* ***LIBRARY SLATEC */
  40. /* ***CATEGORY C10A4 */
  41. /* ***TYPE COMPLEX (CBESJ-C, ZBESJ-C) */
  42. /* ***KEYWORDS BESSEL FUNCTIONS OF COMPLEX ARGUMENT, */
  43. /* BESSEL FUNCTIONS OF THE FIRST KIND, J BESSEL FUNCTIONS */
  44. /* ***AUTHOR Amos, D. E., (SNL) */
  45. /* ***DESCRIPTION */
  46. /* ***A DOUBLE PRECISION ROUTINE*** */
  47. /* On KODE=1, ZBESJ computes an N member sequence of complex */
  48. /* Bessel functions CY(L)=J(FNU+L-1,Z) for real nonnegative */
  49. /* orders FNU+L-1, L=1,...,N and complex Z in the cut plane */
  50. /* -pi<arg(Z)<=pi where Z=ZR+i*ZI. On KODE=2, CBESJ returns */
  51. /* the scaled functions */
  52. /* CY(L) = exp(-abs(Y))*J(FNU+L-1,Z), L=1,...,N and Y=Im(Z) */
  53. /* which remove the exponential growth in both the upper and */
  54. /* lower half planes as Z goes to infinity. Definitions and */
  55. /* notation are found in the NBS Handbook of Mathematical */
  56. /* Functions (Ref. 1). */
  57. /* Input */
  58. /* ZR - DOUBLE PRECISION real part of argument Z */
  59. /* ZI - DOUBLE PRECISION imag part of argument Z */
  60. /* FNU - DOUBLE PRECISION initial order, FNU>=0 */
  61. /* KODE - A parameter to indicate the scaling option */
  62. /* KODE=1 returns */
  63. /* CY(L)=J(FNU+L-1,Z), L=1,...,N */
  64. /* =2 returns */
  65. /* CY(L)=J(FNU+L-1,Z)*exp(-abs(Y)), L=1,...,N */
  66. /* where Y=Im(Z) */
  67. /* N - Number of terms in the sequence, N>=1 */
  68. /* Output */
  69. /* CYR - DOUBLE PRECISION real part of result vector */
  70. /* CYI - DOUBLE PRECISION imag part of result vector */
  71. /* NZ - Number of underflows set to zero */
  72. /* NZ=0 Normal return */
  73. /* NZ>0 CY(L)=0, L=N-NZ+1,...,N */
  74. /* IERR - Error flag */
  75. /* IERR=0 Normal return - COMPUTATION COMPLETED */
  76. /* IERR=1 Input error - NO COMPUTATION */
  77. /* IERR=2 Overflow - NO COMPUTATION */
  78. /* (Im(Z) too large on KODE=1) */
  79. /* IERR=3 Precision warning - COMPUTATION COMPLETED */
  80. /* (Result has half precision or less */
  81. /* because abs(Z) or FNU+N-1 is large) */
  82. /* IERR=4 Precision error - NO COMPUTATION */
  83. /* (Result has no precision because */
  84. /* abs(Z) or FNU+N-1 is too large) */
  85. /* IERR=5 Algorithmic error - NO COMPUTATION */
  86. /* (Termination condition not met) */
  87. /* *Long Description: */
  88. /* The computation is carried out by the formulae */
  89. /* J(a,z) = exp( a*pi*i/2)*I(a,-i*z), Im(z)>=0 */
  90. /* J(a,z) = exp(-a*pi*i/2)*I(a, i*z), Im(z)<0 */
  91. /* where the I Bessel function is computed as described in the */
  92. /* prologue to CBESI. */
  93. /* For negative orders, the formula */
  94. /* J(-a,z) = J(a,z)*cos(a*pi) - Y(a,z)*sin(a*pi) */
  95. /* can be used. However, for large orders close to integers, the */
  96. /* the function changes radically. When a is a large positive */
  97. /* integer, the magnitude of J(-a,z)=J(a,z)*cos(a*pi) is a */
  98. /* large negative power of ten. But when a is not an integer, */
  99. /* Y(a,z) dominates in magnitude with a large positive power of */
  100. /* ten and the most that the second term can be reduced is by */
  101. /* unit roundoff from the coefficient. Thus, wide changes can */
  102. /* occur within unit roundoff of a large integer for a. Here, */
  103. /* large means a>abs(z). */
  104. /* In most complex variable computation, one must evaluate ele- */
  105. /* mentary functions. When the magnitude of Z or FNU+N-1 is */
  106. /* large, losses of significance by argument reduction occur. */
  107. /* Consequently, if either one exceeds U1=SQRT(0.5/UR), then */
  108. /* losses exceeding half precision are likely and an error flag */
  109. /* IERR=3 is triggered where UR=MAX(D1MACH(4),1.0D-18) is double */
  110. /* precision unit roundoff limited to 18 digits precision. Also, */
  111. /* if either is larger than U2=0.5/UR, then all significance is */
  112. /* lost and IERR=4. In order to use the INT function, arguments */
  113. /* must be further restricted not to exceed the largest machine */
  114. /* integer, U3=I1MACH(9). Thus, the magnitude of Z and FNU+N-1 */
  115. /* is restricted by MIN(U2,U3). In IEEE arithmetic, U1,U2, and */
  116. /* U3 approximate 2.0E+3, 4.2E+6, 2.1E+9 in single precision */
  117. /* and 4.7E+7, 2.3E+15 and 2.1E+9 in double precision. This */
  118. /* makes U2 limiting in single precision and U3 limiting in */
  119. /* double precision. This means that one can expect to retain, */
  120. /* in the worst cases on IEEE machines, no digits in single pre- */
  121. /* cision and only 6 digits in double precision. Similar con- */
  122. /* siderations hold for other machines. */
  123. /* The approximate relative error in the magnitude of a complex */
  124. /* Bessel function can be expressed as P*10**S where P=MAX(UNIT */
  125. /* ROUNDOFF,1.0E-18) is the nominal precision and 10**S repre- */
  126. /* sents the increase in error due to argument reduction in the */
  127. /* elementary functions. Here, S=MAX(1,ABS(LOG10(ABS(Z))), */
  128. /* ABS(LOG10(FNU))) approximately (i.e., S=MAX(1,ABS(EXPONENT OF */
  129. /* ABS(Z),ABS(EXPONENT OF FNU)) ). However, the phase angle may */
  130. /* have only absolute accuracy. This is most likely to occur */
  131. /* when one component (in magnitude) is larger than the other by */
  132. /* several orders of magnitude. If one component is 10**K larger */
  133. /* than the other, then one can expect only MAX(ABS(LOG10(P))-K, */
  134. /* 0) significant digits; or, stated another way, when K exceeds */
  135. /* the exponent of P, no significant digits remain in the smaller */
  136. /* component. However, the phase angle retains absolute accuracy */
  137. /* because, in complex arithmetic with precision P, the smaller */
  138. /* component will not (as a rule) decrease below P times the */
  139. /* magnitude of the larger component. In these extreme cases, */
  140. /* the principal phase angle is on the order of +P, -P, PI/2-P, */
  141. /* or -PI/2+P. */
  142. /* ***REFERENCES 1. M. Abramowitz and I. A. Stegun, Handbook of Mathe- */
  143. /* matical Functions, National Bureau of Standards */
  144. /* Applied Mathematics Series 55, U. S. Department */
  145. /* of Commerce, Tenth Printing (1972) or later. */
  146. /* 2. D. E. Amos, Computation of Bessel Functions of */
  147. /* Complex Argument, Report SAND83-0086, Sandia National */
  148. /* Laboratories, Albuquerque, NM, May 1983. */
  149. /* 3. D. E. Amos, Computation of Bessel Functions of */
  150. /* Complex Argument and Large Order, Report SAND83-0643, */
  151. /* Sandia National Laboratories, Albuquerque, NM, May */
  152. /* 1983. */
  153. /* 4. D. E. Amos, A Subroutine Package for Bessel Functions */
  154. /* of a Complex Argument and Nonnegative Order, Report */
  155. /* SAND85-1018, Sandia National Laboratory, Albuquerque, */
  156. /* NM, May 1985. */
  157. /* 5. D. E. Amos, A portable package for Bessel functions */
  158. /* of a complex argument and nonnegative order, ACM */
  159. /* Transactions on Mathematical Software, 12 (September */
  160. /* 1986), pp. 265-273. */
  161. /* ***ROUTINES CALLED D1MACH, I1MACH, ZABS, ZBINU */
  162. /* ***REVISION HISTORY (YYMMDD) */
  163. /* 830501 DATE WRITTEN */
  164. /* 890801 REVISION DATE from Version 3.2 */
  165. /* 910415 Prologue converted to Version 4.0 format. (BAB) */
  166. /* 920128 Category corrected. (WRB) */
  167. /* 920811 Prologue revised. (DWL) */
  168. /* ***END PROLOGUE ZBESJ */
  169. /* COMPLEX CI,CSGN,CY,Z,ZN */
  170. /* Parameter adjustments */
  171. --cyi;
  172. --cyr;
  173. /* Function Body */
  174. /* ***FIRST EXECUTABLE STATEMENT ZBESJ */
  175. *ierr = 0;
  176. *nz = 0;
  177. if (*fnu < 0.) {
  178. *ierr = 1;
  179. }
  180. if (*kode < 1 || *kode > 2) {
  181. *ierr = 1;
  182. }
  183. if (*n < 1) {
  184. *ierr = 1;
  185. }
  186. if (*ierr != 0) {
  187. return 0;
  188. }
  189. /* ----------------------------------------------------------------------- */
  190. /* SET PARAMETERS RELATED TO MACHINE CONSTANTS. */
  191. /* TOL IS THE APPROXIMATE UNIT ROUNDOFF LIMITED TO 1.0E-18. */
  192. /* ELIM IS THE APPROXIMATE EXPONENTIAL OVER- AND UNDERFLOW LIMIT. */
  193. /* EXP(-ELIM).LT.EXP(-ALIM)=EXP(-ELIM)/TOL AND */
  194. /* EXP(ELIM).GT.EXP(ALIM)=EXP(ELIM)*TOL ARE INTERVALS NEAR */
  195. /* UNDERFLOW AND OVERFLOW LIMITS WHERE SCALED ARITHMETIC IS DONE. */
  196. /* RL IS THE LOWER BOUNDARY OF THE ASYMPTOTIC EXPANSION FOR LARGE Z. */
  197. /* DIG = NUMBER OF BASE 10 DIGITS IN TOL = 10**(-DIG). */
  198. /* FNUL IS THE LOWER BOUNDARY OF THE ASYMPTOTIC SERIES FOR LARGE FNU. */
  199. /* ----------------------------------------------------------------------- */
  200. /* Computing MAX */
  201. d__1 = d1mach_(4);
  202. tol = max(d__1,1e-18);
  203. k1 = i1mach_(15);
  204. k2 = i1mach_(16);
  205. r1m5 = d1mach_(5);
  206. /* Computing MIN */
  207. i__1 = abs(k1), i__2 = abs(k2);
  208. k = min(i__1,i__2);
  209. elim = (k * r1m5 - 3.) * 2.303;
  210. k1 = i1mach_(14) - 1;
  211. aa = r1m5 * k1;
  212. dig = min(aa,18.);
  213. aa *= 2.303;
  214. /* Computing MAX */
  215. d__1 = -aa;
  216. alim = elim + max(d__1,-41.45);
  217. rl = dig * 1.2 + 3.;
  218. fnul = (dig - 3.) * 6. + 10.;
  219. /* ----------------------------------------------------------------------- */
  220. /* TEST FOR PROPER RANGE */
  221. /* ----------------------------------------------------------------------- */
  222. az = zabs_(zr, zi);
  223. fn = *fnu + (*n - 1);
  224. aa = .5 / tol;
  225. bb = i1mach_(9) * .5;
  226. aa = min(aa,bb);
  227. if (az > aa) {
  228. goto L260;
  229. }
  230. if (fn > aa) {
  231. goto L260;
  232. }
  233. aa = sqrt(aa);
  234. if (az > aa) {
  235. *ierr = 3;
  236. }
  237. if (fn > aa) {
  238. *ierr = 3;
  239. }
  240. /* ----------------------------------------------------------------------- */
  241. /* CALCULATE CSGN=EXP(FNU*HPI*I) TO MINIMIZE LOSSES OF SIGNIFICANCE */
  242. /* WHEN FNU IS LARGE */
  243. /* ----------------------------------------------------------------------- */
  244. cii = 1.;
  245. inu = (integer) (*fnu);
  246. inuh = inu / 2;
  247. ir = inu - (inuh << 1);
  248. arg = (*fnu - (inu - ir)) * hpi;
  249. csgnr = cos(arg);
  250. csgni = sin(arg);
  251. if (inuh % 2 == 0) {
  252. goto L40;
  253. }
  254. csgnr = -csgnr;
  255. csgni = -csgni;
  256. L40:
  257. /* ----------------------------------------------------------------------- */
  258. /* ZN IS IN THE RIGHT HALF PLANE */
  259. /* ----------------------------------------------------------------------- */
  260. znr = *zi;
  261. zni = -(*zr);
  262. if (*zi >= 0.) {
  263. goto L50;
  264. }
  265. znr = -znr;
  266. zni = -zni;
  267. csgni = -csgni;
  268. cii = -cii;
  269. L50:
  270. zbinu_(&znr, &zni, fnu, kode, n, &cyr[1], &cyi[1], nz, &rl, &fnul, &tol, &elim, &alim);
  271. if (*nz < 0) {
  272. goto L130;
  273. }
  274. nl = *n - *nz;
  275. if (nl == 0) {
  276. return 0;
  277. }
  278. rtol = 1. / tol;
  279. ascle = d1mach_(1) * rtol * 1e3;
  280. i__1 = nl;
  281. for (i__ = 1; i__ <= i__1; ++i__) {
  282. /* STR = CYR(I)*CSGNR - CYI(I)*CSGNI */
  283. /* CYI(I) = CYR(I)*CSGNI + CYI(I)*CSGNR */
  284. /* CYR(I) = STR */
  285. aa = cyr[i__];
  286. bb = cyi[i__];
  287. atol = 1.;
  288. /* Computing MAX */
  289. d__1 = abs(aa), d__2 = abs(bb);
  290. if (max(d__1,d__2) > ascle) {
  291. goto L55;
  292. }
  293. aa *= rtol;
  294. bb *= rtol;
  295. atol = tol;
  296. L55:
  297. str = aa * csgnr - bb * csgni;
  298. sti = aa * csgni + bb * csgnr;
  299. cyr[i__] = str * atol;
  300. cyi[i__] = sti * atol;
  301. str = -csgni * cii;
  302. csgni = csgnr * cii;
  303. csgnr = str;
  304. /* L60: */
  305. }
  306. return 0;
  307. L130:
  308. if (*nz == -2) {
  309. goto L140;
  310. }
  311. *nz = 0;
  312. *ierr = 2;
  313. return 0;
  314. L140:
  315. *nz = 0;
  316. *ierr = 5;
  317. return 0;
  318. L260:
  319. *nz = 0;
  320. *ierr = 4;
  321. return 0;
  322. } /* zbesj_ */