jv.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. /* jv.c
  2. *
  3. * Bessel function of noninteger order
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * double v, x, y, jv();
  10. *
  11. * y = jv( v, x );
  12. *
  13. *
  14. *
  15. * DESCRIPTION:
  16. *
  17. * Returns Bessel function of order v of the argument,
  18. * where v is real. Negative x is allowed if v is an integer.
  19. *
  20. * Several expansions are included: the ascending power
  21. * series, the Hankel expansion, and two transitional
  22. * expansions for large v. If v is not too large, it
  23. * is reduced by recurrence to a region of best accuracy.
  24. * The transitional expansions give 12D accuracy for v > 500.
  25. *
  26. *
  27. *
  28. * ACCURACY:
  29. * Results for integer v are indicated by *, where x and v
  30. * both vary from -125 to +125. Otherwise,
  31. * x ranges from 0 to 125, v ranges as indicated by "domain."
  32. * Error criterion is absolute, except relative when |jv()| > 1.
  33. *
  34. * arithmetic v domain x domain # trials peak rms
  35. * IEEE 0,125 0,125 100000 4.6e-15 2.2e-16
  36. * IEEE -125,0 0,125 40000 5.4e-11 3.7e-13
  37. * IEEE 0,500 0,500 20000 4.4e-15 4.0e-16
  38. * Integer v:
  39. * IEEE -125,125 -125,125 50000 3.5e-15* 1.9e-16*
  40. *
  41. */
  42. /*
  43. Cephes Math Library Release 2.8: June, 2000
  44. Copyright 1984, 1987, 1989, 1992, 2000 by Stephen L. Moshier
  45. */
  46. #include "mconf.h"
  47. #define DEBUG 0
  48. #ifdef DEC
  49. #define MAXGAM 34.84425627277176174
  50. #else
  51. #define MAXGAM 171.624376956302725
  52. #endif
  53. #ifdef ANSIPROT
  54. extern int airy(double, double *, double *, double *, double *);
  55. extern double fabs(double);
  56. extern double floor(double);
  57. extern double frexp(double, int *);
  58. extern double polevl(double, void *, int);
  59. extern double j0(double);
  60. extern double j1(double);
  61. extern double sqrt(double);
  62. extern double cbrt(double);
  63. extern double exp(double);
  64. extern double log(double);
  65. extern double sin(double);
  66. extern double cos(double);
  67. extern double acos(double);
  68. extern double pow(double, double);
  69. extern double gamma(double);
  70. extern double lgam(double);
  71. static double recur(double *, double, double *, int);
  72. static double jvs(double, double);
  73. static double hankel(double, double);
  74. static double jnx(double, double);
  75. static double jnt(double, double);
  76. #else
  77. int airy();
  78. double fabs(), floor(), frexp(), polevl(), j0(), j1(), sqrt(), cbrt();
  79. double exp(), log(), sin(), cos(), acos(), pow(), gamma(), lgam();
  80. static double recur(), jvs(), hankel(), jnx(), jnt();
  81. #endif
  82. extern double MAXNUM, MACHEP, MINLOG, MAXLOG;
  83. #define BIG 1.44115188075855872E+17
  84. double jv(n, x) double n, x;
  85. {
  86. double k, q, t, y, an;
  87. int i, sign, nint;
  88. nint = 0; /* Flag for integer n */
  89. sign = 1; /* Flag for sign inversion */
  90. an = fabs(n);
  91. y = floor(an);
  92. if (y == an) {
  93. nint = 1;
  94. i = an - 16384.0 * floor(an / 16384.0);
  95. if (n < 0.0) {
  96. if (i & 1)
  97. sign = -sign;
  98. n = an;
  99. }
  100. if (x < 0.0) {
  101. if (i & 1)
  102. sign = -sign;
  103. x = -x;
  104. }
  105. if (n == 0.0)
  106. return (j0(x));
  107. if (n == 1.0)
  108. return (sign * j1(x));
  109. }
  110. if ((x < 0.0) && (y != an)) {
  111. mtherr("Jv", DOMAIN);
  112. y = 0.0;
  113. goto done;
  114. }
  115. y = fabs(x);
  116. if (y < MACHEP)
  117. goto underf;
  118. k = 3.6 * sqrt(y);
  119. t = 3.6 * sqrt(an);
  120. if ((y < t) && (an > 21.0))
  121. return (sign * jvs(n, x));
  122. if ((an < k) && (y > 21.0))
  123. return (sign * hankel(n, x));
  124. if (an < 500.0) {
  125. /* Note: if x is too large, the continued
  126. * fraction will fail; but then the
  127. * Hankel expansion can be used.
  128. */
  129. if (nint != 0) {
  130. k = 0.0;
  131. q = recur(&n, x, &k, 1);
  132. if (k == 0.0) {
  133. y = j0(x) / q;
  134. goto done;
  135. }
  136. if (k == 1.0) {
  137. y = j1(x) / q;
  138. goto done;
  139. }
  140. }
  141. if (an > 2.0 * y)
  142. goto rlarger;
  143. if ((n >= 0.0) && (n < 20.0) && (y > 6.0) && (y < 20.0)) {
  144. /* Recur backwards from a larger value of n
  145. */
  146. rlarger:
  147. k = n;
  148. y = y + an + 1.0;
  149. if (y < 30.0)
  150. y = 30.0;
  151. y = n + floor(y - n);
  152. q = recur(&y, x, &k, 0);
  153. y = jvs(y, x) * q;
  154. goto done;
  155. }
  156. if (k <= 30.0) {
  157. k = 2.0;
  158. } else if (k < 90.0) {
  159. k = (3 * k) / 4;
  160. }
  161. if (an > (k + 3.0)) {
  162. if (n < 0.0)
  163. k = -k;
  164. q = n - floor(n);
  165. k = floor(k) + q;
  166. if (n > 0.0)
  167. q = recur(&n, x, &k, 1);
  168. else {
  169. t = k;
  170. k = n;
  171. q = recur(&t, x, &k, 1);
  172. k = t;
  173. }
  174. if (q == 0.0) {
  175. underf:
  176. y = 0.0;
  177. goto done;
  178. }
  179. } else {
  180. k = n;
  181. q = 1.0;
  182. }
  183. /* boundary between convergence of
  184. * power series and Hankel expansion
  185. */
  186. y = fabs(k);
  187. if (y < 26.0)
  188. t = (0.0083 * y + 0.09) * y + 12.9;
  189. else
  190. t = 0.9 * y;
  191. if (x > t)
  192. y = hankel(k, x);
  193. else
  194. y = jvs(k, x);
  195. #if DEBUG
  196. printf("y = %.16e, recur q = %.16e\n", y, q);
  197. #endif
  198. if (n > 0.0)
  199. y /= q;
  200. else
  201. y *= q;
  202. }
  203. else {
  204. /* For large n, use the uniform expansion
  205. * or the transitional expansion.
  206. * But if x is of the order of n**2,
  207. * these may blow up, whereas the
  208. * Hankel expansion will then work.
  209. */
  210. if (n < 0.0) {
  211. mtherr("Jv", TLOSS);
  212. y = 0.0;
  213. goto done;
  214. }
  215. t = x / n;
  216. t /= n;
  217. if (t > 0.3)
  218. y = hankel(n, x);
  219. else
  220. y = jnx(n, x);
  221. }
  222. done:
  223. return (sign * y);
  224. }
  225. /* Reduce the order by backward recurrence.
  226. * AMS55 #9.1.27 and 9.1.73.
  227. */
  228. static double recur(n, x, newn, cancel) double *n;
  229. double x;
  230. double *newn;
  231. int cancel;
  232. {
  233. double pkm2, pkm1, pk, qkm2, qkm1;
  234. /* double pkp1; */
  235. double k, ans, qk, xk, yk, r, t, kf;
  236. static double big = BIG;
  237. int nflag, ctr;
  238. /* continued fraction for Jn(x)/Jn-1(x) */
  239. if (*n < 0.0)
  240. nflag = 1;
  241. else
  242. nflag = 0;
  243. fstart:
  244. #if DEBUG
  245. printf("recur: n = %.6e, newn = %.6e, cfrac = ", *n, *newn);
  246. #endif
  247. pkm2 = 0.0;
  248. qkm2 = 1.0;
  249. pkm1 = x;
  250. qkm1 = *n + *n;
  251. xk = -x * x;
  252. yk = qkm1;
  253. ans = 1.0;
  254. ctr = 0;
  255. do {
  256. yk += 2.0;
  257. pk = pkm1 * yk + pkm2 * xk;
  258. qk = qkm1 * yk + qkm2 * xk;
  259. pkm2 = pkm1;
  260. pkm1 = pk;
  261. qkm2 = qkm1;
  262. qkm1 = qk;
  263. if (qk != 0)
  264. r = pk / qk;
  265. else
  266. r = 0.0;
  267. if (r != 0) {
  268. t = fabs((ans - r) / r);
  269. ans = r;
  270. } else
  271. t = 1.0;
  272. if (++ctr > 1000) {
  273. mtherr("jv", UNDERFLOW);
  274. goto done;
  275. }
  276. if (t < MACHEP)
  277. goto done;
  278. if (fabs(pk) > big) {
  279. pkm2 /= big;
  280. pkm1 /= big;
  281. qkm2 /= big;
  282. qkm1 /= big;
  283. }
  284. } while (t > MACHEP);
  285. done:
  286. #if DEBUG
  287. printf("%.6e\n", ans);
  288. #endif
  289. /* Change n to n-1 if n < 0 and the continued fraction is small
  290. */
  291. if (nflag > 0) {
  292. if (fabs(ans) < 0.125) {
  293. nflag = -1;
  294. *n = *n - 1.0;
  295. goto fstart;
  296. }
  297. }
  298. kf = *newn;
  299. /* backward recurrence
  300. * 2k
  301. * J (x) = --- J (x) - J (x)
  302. * k-1 x k k+1
  303. */
  304. pk = 1.0;
  305. pkm1 = 1.0 / ans;
  306. k = *n - 1.0;
  307. r = 2 * k;
  308. do {
  309. pkm2 = (pkm1 * r - pk * x) / x;
  310. /* pkp1 = pk; */
  311. pk = pkm1;
  312. pkm1 = pkm2;
  313. r -= 2.0;
  314. /*
  315. t = fabs(pkp1) + fabs(pk);
  316. if( (k > (kf + 2.5)) && (fabs(pkm1) < 0.25*t) )
  317. {
  318. k -= 1.0;
  319. t = x*x;
  320. pkm2 = ( (r*(r+2.0)-t)*pk - r*x*pkp1 )/t;
  321. pkp1 = pk;
  322. pk = pkm1;
  323. pkm1 = pkm2;
  324. r -= 2.0;
  325. }
  326. */
  327. k -= 1.0;
  328. } while (k > (kf + 0.5));
  329. /* Take the larger of the last two iterates
  330. * on the theory that it may have less cancellation error.
  331. */
  332. if (cancel) {
  333. if ((kf >= 0.0) && (fabs(pk) > fabs(pkm1))) {
  334. k += 1.0;
  335. pkm2 = pk;
  336. }
  337. }
  338. *newn = k;
  339. #if DEBUG
  340. printf("newn %.6e rans %.6e\n", k, pkm2);
  341. #endif
  342. return (pkm2);
  343. }
  344. /* Ascending power series for Jv(x).
  345. * AMS55 #9.1.10.
  346. */
  347. extern double PI;
  348. extern int sgngam;
  349. static double jvs(n, x) double n, x;
  350. {
  351. double t, u, y, z, k;
  352. int ex;
  353. z = -x * x / 4.0;
  354. u = 1.0;
  355. y = u;
  356. k = 1.0;
  357. t = 1.0;
  358. while (t > MACHEP) {
  359. u *= z / (k * (n + k));
  360. y += u;
  361. k += 1.0;
  362. if (y != 0)
  363. t = fabs(u / y);
  364. }
  365. #if DEBUG
  366. printf("power series=%.5e ", y);
  367. #endif
  368. t = frexp(0.5 * x, &ex);
  369. ex = ex * n;
  370. if ((ex > -1023) && (ex < 1023) && (n > 0.0) && (n < (MAXGAM - 1.0))) {
  371. t = pow(0.5 * x, n) / gamma(n + 1.0);
  372. #if DEBUG
  373. printf("pow(.5*x, %.4e)/gamma(n+1)=%.5e\n", n, t);
  374. #endif
  375. y *= t;
  376. } else {
  377. #if DEBUG
  378. z = n * log(0.5 * x);
  379. k = lgam(n + 1.0);
  380. t = z - k;
  381. printf("log pow=%.5e, lgam(%.4e)=%.5e\n", z, n + 1.0, k);
  382. #else
  383. t = n * log(0.5 * x) - lgam(n + 1.0);
  384. #endif
  385. if (y < 0) {
  386. sgngam = -sgngam;
  387. y = -y;
  388. }
  389. t += log(y);
  390. #if DEBUG
  391. printf("log y=%.5e\n", log(y));
  392. #endif
  393. if (t < -MAXLOG) {
  394. return (0.0);
  395. }
  396. if (t > MAXLOG) {
  397. mtherr("Jv", OVERFLOW);
  398. return (MAXNUM);
  399. }
  400. y = sgngam * exp(t);
  401. }
  402. return (y);
  403. }
  404. /* Hankel's asymptotic expansion
  405. * for large x.
  406. * AMS55 #9.2.5.
  407. */
  408. static double hankel(n, x) double n, x;
  409. {
  410. double t, u, z, k, sign, conv;
  411. double p, q, j, m, pp, qq;
  412. int flag;
  413. m = 4.0 * n * n;
  414. j = 1.0;
  415. z = 8.0 * x;
  416. k = 1.0;
  417. p = 1.0;
  418. u = (m - 1.0) / z;
  419. q = u;
  420. sign = 1.0;
  421. conv = 1.0;
  422. flag = 0;
  423. t = 1.0;
  424. pp = 1.0e38;
  425. qq = 1.0e38;
  426. while (t > MACHEP) {
  427. k += 2.0;
  428. j += 1.0;
  429. sign = -sign;
  430. u *= (m - k * k) / (j * z);
  431. p += sign * u;
  432. k += 2.0;
  433. j += 1.0;
  434. u *= (m - k * k) / (j * z);
  435. q += sign * u;
  436. t = fabs(u / p);
  437. if (t < conv) {
  438. conv = t;
  439. qq = q;
  440. pp = p;
  441. flag = 1;
  442. }
  443. /* stop if the terms start getting larger */
  444. if ((flag != 0) && (t > conv)) {
  445. #if DEBUG
  446. printf("Hankel: convergence to %.4E\n", conv);
  447. #endif
  448. goto hank1;
  449. }
  450. }
  451. hank1:
  452. u = x - (0.5 * n + 0.25) * PI;
  453. t = sqrt(2.0 / (PI * x)) * (pp * cos(u) - qq * sin(u));
  454. #if DEBUG
  455. printf("hank: %.6e\n", t);
  456. #endif
  457. return (t);
  458. }
  459. /* Asymptotic expansion for large n.
  460. * AMS55 #9.3.35.
  461. */
  462. static double lambda[] = {1.0,
  463. 1.041666666666666666666667E-1,
  464. 8.355034722222222222222222E-2,
  465. 1.282265745563271604938272E-1,
  466. 2.918490264641404642489712E-1,
  467. 8.816272674437576524187671E-1,
  468. 3.321408281862767544702647E+0,
  469. 1.499576298686255465867237E+1,
  470. 7.892301301158651813848139E+1,
  471. 4.744515388682643231611949E+2,
  472. 3.207490090890661934704328E+3};
  473. static double mu[] = {1.0,
  474. -1.458333333333333333333333E-1,
  475. -9.874131944444444444444444E-2,
  476. -1.433120539158950617283951E-1,
  477. -3.172272026784135480967078E-1,
  478. -9.424291479571202491373028E-1,
  479. -3.511203040826354261542798E+0,
  480. -1.572726362036804512982712E+1,
  481. -8.228143909718594444224656E+1,
  482. -4.923553705236705240352022E+2,
  483. -3.316218568547972508762102E+3};
  484. static double P1[] = {-2.083333333333333333333333E-1,
  485. 1.250000000000000000000000E-1};
  486. static double P2[] = {3.342013888888888888888889E-1,
  487. -4.010416666666666666666667E-1,
  488. 7.031250000000000000000000E-2};
  489. static double P3[] = {
  490. -1.025812596450617283950617E+0, 1.846462673611111111111111E+0,
  491. -8.912109375000000000000000E-1, 7.324218750000000000000000E-2};
  492. static double P4[] = {
  493. 4.669584423426247427983539E+0, -1.120700261622299382716049E+1,
  494. 8.789123535156250000000000E+0, -2.364086914062500000000000E+0,
  495. 1.121520996093750000000000E-1};
  496. static double P5[] = {-2.8212072558200244877E1, 8.4636217674600734632E1,
  497. -9.1818241543240017361E1, 4.2534998745388454861E1,
  498. -7.3687943594796316964E0, 2.27108001708984375E-1};
  499. static double P6[] = {2.1257013003921712286E2, -7.6525246814118164230E2,
  500. 1.0599904525279998779E3, -6.9957962737613254123E2,
  501. 2.1819051174421159048E2, -2.6491430486951555525E1,
  502. 5.7250142097473144531E-1};
  503. static double P7[] = {-1.9194576623184069963E3, 8.0617221817373093845E3,
  504. -1.3586550006434137439E4, 1.1655393336864533248E4,
  505. -5.3056469786134031084E3, 1.2009029132163524628E3,
  506. -1.0809091978839465550E2, 1.7277275025844573975E0};
  507. static double jnx(n, x) double n, x;
  508. {
  509. double zeta, sqz, zz, zp, np;
  510. double cbn, n23, t, z, sz;
  511. double pp, qq, z32i, zzi;
  512. double ak, bk, akl, bkl;
  513. int sign, doa, dob, nflg, k, s, tk, tkp1, m;
  514. static double u[8];
  515. static double ai, aip, bi, bip;
  516. /* Test for x very close to n.
  517. * Use expansion for transition region if so.
  518. */
  519. cbn = cbrt(n);
  520. z = (x - n) / cbn;
  521. if (fabs(z) <= 0.7)
  522. return (jnt(n, x));
  523. z = x / n;
  524. zz = 1.0 - z * z;
  525. if (zz == 0.0)
  526. return (0.0);
  527. if (zz > 0.0) {
  528. sz = sqrt(zz);
  529. t = 1.5 * (log((1.0 + sz) / z) - sz); /* zeta ** 3/2 */
  530. zeta = cbrt(t * t);
  531. nflg = 1;
  532. } else {
  533. sz = sqrt(-zz);
  534. t = 1.5 * (sz - acos(1.0 / z));
  535. zeta = -cbrt(t * t);
  536. nflg = -1;
  537. }
  538. z32i = fabs(1.0 / t);
  539. sqz = cbrt(t);
  540. /* Airy function */
  541. n23 = cbrt(n * n);
  542. t = n23 * zeta;
  543. #if DEBUG
  544. printf("zeta %.5E, Airy(%.5E)\n", zeta, t);
  545. #endif
  546. airy(t, &ai, &aip, &bi, &bip);
  547. /* polynomials in expansion */
  548. u[0] = 1.0;
  549. zzi = 1.0 / zz;
  550. u[1] = polevl(zzi, P1, 1) / sz;
  551. u[2] = polevl(zzi, P2, 2) / zz;
  552. u[3] = polevl(zzi, P3, 3) / (sz * zz);
  553. pp = zz * zz;
  554. u[4] = polevl(zzi, P4, 4) / pp;
  555. u[5] = polevl(zzi, P5, 5) / (pp * sz);
  556. pp *= zz;
  557. u[6] = polevl(zzi, P6, 6) / pp;
  558. u[7] = polevl(zzi, P7, 7) / (pp * sz);
  559. #if DEBUG
  560. for (k = 0; k <= 7; k++)
  561. printf("u[%d] = %.5E\n", k, u[k]);
  562. #endif
  563. pp = 0.0;
  564. qq = 0.0;
  565. np = 1.0;
  566. /* flags to stop when terms get larger */
  567. doa = 1;
  568. dob = 1;
  569. akl = MAXNUM;
  570. bkl = MAXNUM;
  571. for (k = 0; k <= 3; k++) {
  572. tk = 2 * k;
  573. tkp1 = tk + 1;
  574. zp = 1.0;
  575. ak = 0.0;
  576. bk = 0.0;
  577. for (s = 0; s <= tk; s++) {
  578. if (doa) {
  579. if ((s & 3) > 1)
  580. sign = nflg;
  581. else
  582. sign = 1;
  583. ak += sign * mu[s] * zp * u[tk - s];
  584. }
  585. if (dob) {
  586. m = tkp1 - s;
  587. if (((m + 1) & 3) > 1)
  588. sign = nflg;
  589. else
  590. sign = 1;
  591. bk += sign * lambda[s] * zp * u[m];
  592. }
  593. zp *= z32i;
  594. }
  595. if (doa) {
  596. ak *= np;
  597. t = fabs(ak);
  598. if (t < akl) {
  599. akl = t;
  600. pp += ak;
  601. } else
  602. doa = 0;
  603. }
  604. if (dob) {
  605. bk += lambda[tkp1] * zp * u[0];
  606. bk *= -np / sqz;
  607. t = fabs(bk);
  608. if (t < bkl) {
  609. bkl = t;
  610. qq += bk;
  611. } else
  612. dob = 0;
  613. }
  614. #if DEBUG
  615. printf("a[%d] %.5E, b[%d] %.5E\n", k, ak, k, bk);
  616. #endif
  617. if (np < MACHEP)
  618. break;
  619. np /= n * n;
  620. }
  621. /* normalizing factor ( 4*zeta/(1 - z**2) )**1/4 */
  622. t = 4.0 * zeta / zz;
  623. t = sqrt(sqrt(t));
  624. t *= ai * pp / cbrt(n) + aip * qq / (n23 * n);
  625. return (t);
  626. }
  627. /* Asymptotic expansion for transition region,
  628. * n large and x close to n.
  629. * AMS55 #9.3.23.
  630. */
  631. static double PF2[] = {-9.0000000000000000000e-2, 8.5714285714285714286e-2};
  632. static double PF3[] = {1.3671428571428571429e-1, -5.4920634920634920635e-2,
  633. -4.4444444444444444444e-3};
  634. static double PF4[] = {1.3500000000000000000e-3, -1.6036054421768707483e-1,
  635. 4.2590187590187590188e-2, 2.7330447330447330447e-3};
  636. static double PG1[] = {-2.4285714285714285714e-1, 1.4285714285714285714e-2};
  637. static double PG2[] = {-9.0000000000000000000e-3, 1.9396825396825396825e-1,
  638. -1.1746031746031746032e-2};
  639. static double PG3[] = {1.9607142857142857143e-2, -1.5983694083694083694e-1,
  640. 6.3838383838383838384e-3};
  641. static double jnt(n, x) double n, x;
  642. {
  643. double z, zz, z3;
  644. double cbn, n23, cbtwo;
  645. double ai, aip, bi, bip; /* Airy functions */
  646. double nk, fk, gk, pp, qq;
  647. double F[5], G[4];
  648. int k;
  649. cbn = cbrt(n);
  650. z = (x - n) / cbn;
  651. cbtwo = cbrt(2.0);
  652. /* Airy function */
  653. zz = -cbtwo * z;
  654. airy(zz, &ai, &aip, &bi, &bip);
  655. /* polynomials in expansion */
  656. zz = z * z;
  657. z3 = zz * z;
  658. F[0] = 1.0;
  659. F[1] = -z / 5.0;
  660. F[2] = polevl(z3, PF2, 1) * zz;
  661. F[3] = polevl(z3, PF3, 2);
  662. F[4] = polevl(z3, PF4, 3) * z;
  663. G[0] = 0.3 * zz;
  664. G[1] = polevl(z3, PG1, 1);
  665. G[2] = polevl(z3, PG2, 2) * z;
  666. G[3] = polevl(z3, PG3, 2) * zz;
  667. #if DEBUG
  668. for (k = 0; k <= 4; k++)
  669. printf("F[%d] = %.5E\n", k, F[k]);
  670. for (k = 0; k <= 3; k++)
  671. printf("G[%d] = %.5E\n", k, G[k]);
  672. #endif
  673. pp = 0.0;
  674. qq = 0.0;
  675. nk = 1.0;
  676. n23 = cbrt(n * n);
  677. for (k = 0; k <= 4; k++) {
  678. fk = F[k] * nk;
  679. pp += fk;
  680. if (k != 4) {
  681. gk = G[k] * nk;
  682. qq += gk;
  683. }
  684. #if DEBUG
  685. printf("fk[%d] %.5E, gk[%d] %.5E\n", k, fk, k, gk);
  686. #endif
  687. nk /= n23;
  688. }
  689. fk = cbtwo * ai * pp / cbn + cbrt(4.0) * aip * qq / n;
  690. return (fk);
  691. }