gsl_specfunc__bessel_j.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /* specfunc/bessel_j.c
  2. *
  3. * Copyright (C) 1996,1997,1998,1999,2000,2001,2002,2003 Gerard Jungman
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. /* Author: G. Jungman */
  20. #include "gsl__config.h"
  21. #include "gsl_math.h"
  22. #include "gsl_errno.h"
  23. #include "gsl_sf_pow_int.h"
  24. #include "gsl_sf_trig.h"
  25. #include "gsl_sf_bessel.h"
  26. #include "gsl_specfunc__error.h"
  27. #include "gsl_specfunc__bessel.h"
  28. #include "gsl_specfunc__bessel_olver.h"
  29. /*-*-*-*-*-*-*-*-*-*-*-* Functions with Error Codes *-*-*-*-*-*-*-*-*-*-*-*/
  30. int gsl_sf_bessel_j0_e(const double x, gsl_sf_result * result)
  31. {
  32. double ax = fabs(x);
  33. /* CHECK_POINTER(result) */
  34. if(ax < 0.5) {
  35. const double y = x*x;
  36. const double c1 = -1.0/6.0;
  37. const double c2 = 1.0/120.0;
  38. const double c3 = -1.0/5040.0;
  39. const double c4 = 1.0/362880.0;
  40. const double c5 = -1.0/39916800.0;
  41. const double c6 = 1.0/6227020800.0;
  42. result->val = 1.0 + y*(c1 + y*(c2 + y*(c3 + y*(c4 + y*(c5 + y*c6)))));
  43. result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val);
  44. return GSL_SUCCESS;
  45. }
  46. else {
  47. gsl_sf_result sin_result;
  48. const int stat = gsl_sf_sin_e(x, &sin_result);
  49. result->val = sin_result.val/x;
  50. result->err = fabs(sin_result.err/x);
  51. result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
  52. return stat;
  53. }
  54. }
  55. int gsl_sf_bessel_j1_e(const double x, gsl_sf_result * result)
  56. {
  57. double ax = fabs(x);
  58. /* CHECK_POINTER(result) */
  59. if(x == 0.0) {
  60. result->val = 0.0;
  61. result->err = 0.0;
  62. return GSL_SUCCESS;
  63. }
  64. else if(ax < 3.1*GSL_DBL_MIN) {
  65. UNDERFLOW_ERROR(result);
  66. }
  67. else if(ax < 0.25) {
  68. const double y = x*x;
  69. const double c1 = -1.0/10.0;
  70. const double c2 = 1.0/280.0;
  71. const double c3 = -1.0/15120.0;
  72. const double c4 = 1.0/1330560.0;
  73. const double c5 = -1.0/172972800.0;
  74. const double sum = 1.0 + y*(c1 + y*(c2 + y*(c3 + y*(c4 + y*c5))));
  75. result->val = x/3.0 * sum;
  76. result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val);
  77. return GSL_SUCCESS;
  78. }
  79. else {
  80. gsl_sf_result cos_result;
  81. gsl_sf_result sin_result;
  82. const int stat_cos = gsl_sf_cos_e(x, &cos_result);
  83. const int stat_sin = gsl_sf_sin_e(x, &sin_result);
  84. const double cos_x = cos_result.val;
  85. const double sin_x = sin_result.val;
  86. result->val = (sin_x/x - cos_x)/x;
  87. result->err = (fabs(sin_result.err/x) + fabs(cos_result.err))/fabs(x);
  88. result->err += 2.0 * GSL_DBL_EPSILON * (fabs(sin_x/(x*x)) + fabs(cos_x/x));
  89. result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
  90. return GSL_ERROR_SELECT_2(stat_cos, stat_sin);
  91. }
  92. }
  93. int gsl_sf_bessel_j2_e(const double x, gsl_sf_result * result)
  94. {
  95. double ax = fabs(x);
  96. /* CHECK_POINTER(result) */
  97. if(x == 0.0) {
  98. result->val = 0.0;
  99. result->err = 0.0;
  100. return GSL_SUCCESS;
  101. }
  102. else if(ax < 4.0*GSL_SQRT_DBL_MIN) {
  103. UNDERFLOW_ERROR(result);
  104. }
  105. else if(ax < 1.3) {
  106. const double y = x*x;
  107. const double c1 = -1.0/14.0;
  108. const double c2 = 1.0/504.0;
  109. const double c3 = -1.0/33264.0;
  110. const double c4 = 1.0/3459456.0;
  111. const double c5 = -1.0/518918400;
  112. const double c6 = 1.0/105859353600.0;
  113. const double c7 = -1.0/28158588057600.0;
  114. const double c8 = 1.0/9461285587353600.0;
  115. const double c9 = -1.0/3916972233164390400.0;
  116. const double sum = 1.0+y*(c1+y*(c2+y*(c3+y*(c4+y*(c5+y*(c6+y*(c7+y*(c8+y*c9))))))));
  117. result->val = y/15.0 * sum;
  118. result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val);
  119. return GSL_SUCCESS;
  120. }
  121. else {
  122. gsl_sf_result cos_result;
  123. gsl_sf_result sin_result;
  124. const int stat_cos = gsl_sf_cos_e(x, &cos_result);
  125. const int stat_sin = gsl_sf_sin_e(x, &sin_result);
  126. const double cos_x = cos_result.val;
  127. const double sin_x = sin_result.val;
  128. const double f = (3.0/(x*x) - 1.0);
  129. result->val = (f * sin_x - 3.0*cos_x/x)/x;
  130. result->err = fabs(f * sin_result.err/x) + fabs((3.0*cos_result.err/x)/x);
  131. result->err += 2.0 * GSL_DBL_EPSILON * (fabs(f*sin_x/x) + 3.0*fabs(cos_x/(x*x)));
  132. result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
  133. return GSL_ERROR_SELECT_2(stat_cos, stat_sin);
  134. }
  135. }
  136. int
  137. gsl_sf_bessel_jl_e(const int l, const double x, gsl_sf_result * result)
  138. {
  139. if(l < 0 || x < 0.0) {
  140. DOMAIN_ERROR(result);
  141. }
  142. else if(x == 0.0) {
  143. result->val = ( l > 0 ? 0.0 : 1.0 );
  144. result->err = 0.0;
  145. return GSL_SUCCESS;
  146. }
  147. else if(l == 0) {
  148. return gsl_sf_bessel_j0_e(x, result);
  149. }
  150. else if(l == 1) {
  151. return gsl_sf_bessel_j1_e(x, result);
  152. }
  153. else if(l == 2) {
  154. return gsl_sf_bessel_j2_e(x, result);
  155. }
  156. else if(x*x < 10.0*(l+0.5)/M_E) {
  157. gsl_sf_result b;
  158. int status = gsl_sf_bessel_IJ_taylor_e(l+0.5, x, -1, 50, GSL_DBL_EPSILON, &b);
  159. double pre = sqrt((0.5*M_PI)/x);
  160. result->val = pre * b.val;
  161. result->err = pre * b.err;
  162. result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
  163. return status;
  164. }
  165. else if(GSL_ROOT4_DBL_EPSILON * x > (l*l + l + 1.0)) {
  166. gsl_sf_result b;
  167. int status = gsl_sf_bessel_Jnu_asympx_e(l + 0.5, x, &b);
  168. double pre = sqrt((0.5*M_PI)/x);
  169. result->val = pre * b.val;
  170. result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val) + pre * b.err;
  171. return status;
  172. }
  173. else if(l > 1.0/GSL_ROOT6_DBL_EPSILON) {
  174. gsl_sf_result b;
  175. int status = gsl_sf_bessel_Jnu_asymp_Olver_e(l + 0.5, x, &b);
  176. double pre = sqrt((0.5*M_PI)/x);
  177. result->val = pre * b.val;
  178. result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val) + pre * b.err;
  179. return status;
  180. }
  181. else if(x > 1000.0 && x > 100.0*l*l)
  182. {
  183. /* We need this to avoid feeding large x to CF1; note that
  184. * due to the above check, we know that n <= 50.
  185. */
  186. gsl_sf_result b;
  187. int status = gsl_sf_bessel_Jnu_asympx_e(l + 0.5, x, &b);
  188. double pre = sqrt((0.5*M_PI)/x);
  189. result->val = pre * b.val;
  190. result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val) + pre * b.err;
  191. return status;
  192. }
  193. else {
  194. double sgn;
  195. double ratio;
  196. int stat_CF1 = gsl_sf_bessel_J_CF1(l+0.5, x, &ratio, &sgn);
  197. double jellp1 = GSL_SQRT_DBL_EPSILON * ratio;
  198. double jell = GSL_SQRT_DBL_EPSILON;
  199. double jellm1;
  200. int ell;
  201. for(ell = l; ell > 0; ell--) {
  202. jellm1 = -jellp1 + (2*ell + 1)/x * jell;
  203. jellp1 = jell;
  204. jell = jellm1;
  205. }
  206. if(fabs(jell) > fabs(jellp1)) {
  207. gsl_sf_result j0_result;
  208. int stat_j0 = gsl_sf_bessel_j0_e(x, &j0_result);
  209. double pre = GSL_SQRT_DBL_EPSILON / jell;
  210. result->val = j0_result.val * pre;
  211. result->err = j0_result.err * fabs(pre);
  212. result->err += 2.0 * GSL_DBL_EPSILON * (0.5*l + 1.0) * fabs(result->val);
  213. return GSL_ERROR_SELECT_2(stat_j0, stat_CF1);
  214. }
  215. else {
  216. gsl_sf_result j1_result;
  217. int stat_j1 = gsl_sf_bessel_j1_e(x, &j1_result);
  218. double pre = GSL_SQRT_DBL_EPSILON / jellp1;
  219. result->val = j1_result.val * pre;
  220. result->err = j1_result.err * fabs(pre);
  221. result->err += 2.0 * GSL_DBL_EPSILON * (0.5*l + 1.0) * fabs(result->val);
  222. return GSL_ERROR_SELECT_2(stat_j1, stat_CF1);
  223. }
  224. }
  225. }
  226. int
  227. gsl_sf_bessel_jl_array(const int lmax, const double x, double * result_array)
  228. {
  229. /* CHECK_POINTER(result_array) */
  230. if(lmax < 0 || x < 0.0) {
  231. int j;
  232. for(j=0; j<=lmax; j++) result_array[j] = 0.0;
  233. GSL_ERROR ("error", GSL_EDOM);
  234. }
  235. else if(x == 0.0) {
  236. int j;
  237. for(j=1; j<=lmax; j++) result_array[j] = 0.0;
  238. result_array[0] = 1.0;
  239. return GSL_SUCCESS;
  240. }
  241. else {
  242. gsl_sf_result r_jellp1;
  243. gsl_sf_result r_jell;
  244. int stat_0 = gsl_sf_bessel_jl_e(lmax+1, x, &r_jellp1);
  245. int stat_1 = gsl_sf_bessel_jl_e(lmax, x, &r_jell);
  246. double jellp1 = r_jellp1.val;
  247. double jell = r_jell.val;
  248. double jellm1;
  249. int ell;
  250. result_array[lmax] = jell;
  251. for(ell = lmax; ell >= 1; ell--) {
  252. jellm1 = -jellp1 + (2*ell + 1)/x * jell;
  253. jellp1 = jell;
  254. jell = jellm1;
  255. result_array[ell-1] = jellm1;
  256. }
  257. return GSL_ERROR_SELECT_2(stat_0, stat_1);
  258. }
  259. }
  260. int gsl_sf_bessel_jl_steed_array(const int lmax, const double x, double * jl_x)
  261. {
  262. /* CHECK_POINTER(jl_x) */
  263. if(lmax < 0 || x < 0.0) {
  264. int j;
  265. for(j=0; j<=lmax; j++) jl_x[j] = 0.0;
  266. GSL_ERROR ("error", GSL_EDOM);
  267. }
  268. else if(x == 0.0) {
  269. int j;
  270. for(j=1; j<=lmax; j++) jl_x[j] = 0.0;
  271. jl_x[0] = 1.0;
  272. return GSL_SUCCESS;
  273. }
  274. else if(x < 2.0*GSL_ROOT4_DBL_EPSILON) {
  275. /* first two terms of Taylor series */
  276. double inv_fact = 1.0; /* 1/(1 3 5 ... (2l+1)) */
  277. double x_l = 1.0; /* x^l */
  278. int l;
  279. for(l=0; l<=lmax; l++) {
  280. jl_x[l] = x_l * inv_fact;
  281. jl_x[l] *= 1.0 - 0.5*x*x/(2.0*l+3.0);
  282. inv_fact /= 2.0*l+3.0;
  283. x_l *= x;
  284. }
  285. return GSL_SUCCESS;
  286. }
  287. else {
  288. /* Steed/Barnett algorithm [Comp. Phys. Comm. 21, 297 (1981)] */
  289. double x_inv = 1.0/x;
  290. double W = 2.0*x_inv;
  291. double F = 1.0;
  292. double FP = (lmax+1.0) * x_inv;
  293. double B = 2.0*FP + x_inv;
  294. double end = B + 20000.0*W;
  295. double D = 1.0/B;
  296. double del = -D;
  297. FP += del;
  298. /* continued fraction */
  299. do {
  300. B += W;
  301. D = 1.0/(B-D);
  302. del *= (B*D - 1.);
  303. FP += del;
  304. if(D < 0.0) F = -F;
  305. if(B > end) {
  306. GSL_ERROR ("error", GSL_EMAXITER);
  307. }
  308. }
  309. while(fabs(del) >= fabs(FP) * GSL_DBL_EPSILON);
  310. FP *= F;
  311. if(lmax > 0) {
  312. /* downward recursion */
  313. double XP2 = FP;
  314. double PL = lmax * x_inv;
  315. int L = lmax;
  316. int LP;
  317. jl_x[lmax] = F;
  318. for(LP = 1; LP<=lmax; LP++) {
  319. jl_x[L-1] = PL * jl_x[L] + XP2;
  320. FP = PL*jl_x[L-1] - jl_x[L];
  321. XP2 = FP;
  322. PL -= x_inv;
  323. --L;
  324. }
  325. F = jl_x[0];
  326. }
  327. /* normalization */
  328. W = x_inv / hypot(FP, F);
  329. jl_x[0] = W*F;
  330. if(lmax > 0) {
  331. int L;
  332. for(L=1; L<=lmax; L++) {
  333. jl_x[L] *= W;
  334. }
  335. }
  336. return GSL_SUCCESS;
  337. }
  338. }
  339. /*-*-*-*-*-*-*-*-*-* Functions w/ Natural Prototypes *-*-*-*-*-*-*-*-*-*-*/
  340. #include "gsl_specfunc__eval.h"
  341. double gsl_sf_bessel_j0(const double x)
  342. {
  343. EVAL_RESULT(gsl_sf_bessel_j0_e(x, &result));
  344. }
  345. double gsl_sf_bessel_j1(const double x)
  346. {
  347. EVAL_RESULT(gsl_sf_bessel_j1_e(x, &result));
  348. }
  349. double gsl_sf_bessel_j2(const double x)
  350. {
  351. EVAL_RESULT(gsl_sf_bessel_j2_e(x, &result));
  352. }
  353. double gsl_sf_bessel_jl(const int l, const double x)
  354. {
  355. EVAL_RESULT(gsl_sf_bessel_jl_e(l, x, &result));
  356. }