gsl_specfunc__bessel_In.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /* specfunc/bessel_In.c
  2. *
  3. * Copyright (C) 1996, 1997, 1998, 1999, 2000 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_bessel.h"
  24. #include "gsl_specfunc__error.h"
  25. #include "gsl_specfunc__bessel.h"
  26. /*-*-*-*-*-*-*-*-*-*-*-* Functions with Error Codes *-*-*-*-*-*-*-*-*-*-*-*/
  27. int
  28. gsl_sf_bessel_In_scaled_e(int n, const double x, gsl_sf_result * result)
  29. {
  30. const double ax = fabs(x);
  31. n = abs(n); /* I(-n, z) = I(n, z) */
  32. /* CHECK_POINTER(result) */
  33. if(n == 0) {
  34. return gsl_sf_bessel_I0_scaled_e(x, result);
  35. }
  36. else if(n == 1) {
  37. return gsl_sf_bessel_I1_scaled_e(x, result);
  38. }
  39. else if(x == 0.0) {
  40. result->val = 0.0;
  41. result->err = 0.0;
  42. return GSL_SUCCESS;
  43. }
  44. else if(x*x < 10.0*(n+1.0)/M_E) {
  45. gsl_sf_result t;
  46. double ex = exp(-ax);
  47. int stat_In = gsl_sf_bessel_IJ_taylor_e((double)n, ax, 1, 50, GSL_DBL_EPSILON, &t);
  48. result->val = t.val * ex;
  49. result->err = t.err * ex;
  50. result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
  51. if(x < 0.0 && GSL_IS_ODD(n)) result->val = -result->val;
  52. return stat_In;
  53. }
  54. else if(n < 150 && ax < 1e7) {
  55. gsl_sf_result I0_scaled;
  56. int stat_I0 = gsl_sf_bessel_I0_scaled_e(ax, &I0_scaled);
  57. double rat;
  58. int stat_CF1 = gsl_sf_bessel_I_CF1_ser((double)n, ax, &rat);
  59. double Ikp1 = rat * GSL_SQRT_DBL_MIN;
  60. double Ik = GSL_SQRT_DBL_MIN;
  61. double Ikm1;
  62. int k;
  63. for(k=n; k >= 1; k--) {
  64. Ikm1 = Ikp1 + 2.0*k/ax * Ik;
  65. Ikp1 = Ik;
  66. Ik = Ikm1;
  67. }
  68. result->val = I0_scaled.val * (GSL_SQRT_DBL_MIN / Ik);
  69. result->err = I0_scaled.err * (GSL_SQRT_DBL_MIN / Ik);
  70. result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
  71. if(x < 0.0 && GSL_IS_ODD(n)) result->val = -result->val;
  72. return GSL_ERROR_SELECT_2(stat_I0, stat_CF1);
  73. }
  74. else if( GSL_MIN( 0.29/(n*n), 0.5/(n*n + x*x) ) < 0.5*GSL_ROOT3_DBL_EPSILON) {
  75. int stat_as = gsl_sf_bessel_Inu_scaled_asymp_unif_e((double)n, ax, result);
  76. if(x < 0.0 && GSL_IS_ODD(n)) result->val = -result->val;
  77. return stat_as;
  78. }
  79. else {
  80. const int nhi = 2 + (int) (1.2 / GSL_ROOT6_DBL_EPSILON);
  81. gsl_sf_result r_Ikp1;
  82. gsl_sf_result r_Ik;
  83. int stat_a1 = gsl_sf_bessel_Inu_scaled_asymp_unif_e(nhi+1.0, ax, &r_Ikp1);
  84. int stat_a2 = gsl_sf_bessel_Inu_scaled_asymp_unif_e((double)nhi, ax, &r_Ik);
  85. double Ikp1 = r_Ikp1.val;
  86. double Ik = r_Ik.val;
  87. double Ikm1;
  88. int k;
  89. for(k=nhi; k > n; k--) {
  90. Ikm1 = Ikp1 + 2.0*k/ax * Ik;
  91. Ikp1 = Ik;
  92. Ik = Ikm1;
  93. }
  94. result->val = Ik;
  95. result->err = Ik * (r_Ikp1.err/r_Ikp1.val + r_Ik.err/r_Ik.val);
  96. if(x < 0.0 && GSL_IS_ODD(n)) result->val = -result->val;
  97. return GSL_ERROR_SELECT_2(stat_a1, stat_a2);
  98. }
  99. }
  100. int
  101. gsl_sf_bessel_In_scaled_array(const int nmin, const int nmax, const double x, double * result_array)
  102. {
  103. /* CHECK_POINTER(result_array) */
  104. if(nmax < nmin || nmin < 0) {
  105. int j;
  106. for(j=0; j<=nmax-nmin; j++) result_array[j] = 0.0;
  107. GSL_ERROR ("domain error", GSL_EDOM);
  108. }
  109. else if(x == 0.0) {
  110. int j;
  111. for(j=0; j<=nmax-nmin; j++) result_array[j] = 0.0;
  112. if(nmin == 0) result_array[0] = 1.0;
  113. return GSL_SUCCESS;
  114. }
  115. else if(nmax == 0) {
  116. gsl_sf_result I0_scaled;
  117. int stat = gsl_sf_bessel_I0_scaled_e(x, &I0_scaled);
  118. result_array[0] = I0_scaled.val;
  119. return stat;
  120. }
  121. else {
  122. const double ax = fabs(x);
  123. const double two_over_x = 2.0/ax;
  124. /* starting values */
  125. gsl_sf_result r_Inp1;
  126. gsl_sf_result r_In;
  127. int stat_0 = gsl_sf_bessel_In_scaled_e(nmax+1, ax, &r_Inp1);
  128. int stat_1 = gsl_sf_bessel_In_scaled_e(nmax, ax, &r_In);
  129. double Inp1 = r_Inp1.val;
  130. double In = r_In.val;
  131. double Inm1;
  132. int n;
  133. for(n=nmax; n>=nmin; n--) {
  134. result_array[n-nmin] = In;
  135. Inm1 = Inp1 + n * two_over_x * In;
  136. Inp1 = In;
  137. In = Inm1;
  138. }
  139. /* deal with signs */
  140. if(x < 0.0) {
  141. for(n=nmin; n<=nmax; n++) {
  142. if(GSL_IS_ODD(n)) result_array[n-nmin] = -result_array[n-nmin];
  143. }
  144. }
  145. return GSL_ERROR_SELECT_2(stat_0, stat_1);
  146. }
  147. }
  148. int
  149. gsl_sf_bessel_In_e(const int n_in, const double x, gsl_sf_result * result)
  150. {
  151. const double ax = fabs(x);
  152. const int n = abs(n_in); /* I(-n, z) = I(n, z) */
  153. gsl_sf_result In_scaled;
  154. const int stat_In_scaled = gsl_sf_bessel_In_scaled_e(n, ax, &In_scaled);
  155. /* In_scaled is always less than 1,
  156. * so this overflow check is conservative.
  157. */
  158. if(ax > GSL_LOG_DBL_MAX - 1.0) {
  159. OVERFLOW_ERROR(result);
  160. }
  161. else {
  162. const double ex = exp(ax);
  163. result->val = ex * In_scaled.val;
  164. result->err = ex * In_scaled.err;
  165. result->err += ax * GSL_DBL_EPSILON * fabs(result->val);
  166. if(x < 0.0 && GSL_IS_ODD(n)) result->val = -result->val;
  167. return stat_In_scaled;
  168. }
  169. }
  170. int
  171. gsl_sf_bessel_In_array(const int nmin, const int nmax, const double x, double * result_array)
  172. {
  173. double ax = fabs(x);
  174. /* CHECK_POINTER(result_array) */
  175. if(ax > GSL_LOG_DBL_MAX - 1.0) {
  176. int j;
  177. for(j=0; j<=nmax-nmin; j++) result_array[j] = 0.0; /* FIXME: should be Inf */
  178. GSL_ERROR ("overflow", GSL_EOVRFLW);
  179. }
  180. else {
  181. int j;
  182. double eax = exp(ax);
  183. int status = gsl_sf_bessel_In_scaled_array(nmin, nmax, x, result_array);
  184. for(j=0; j<=nmax-nmin; j++) result_array[j] *= eax;
  185. return status;
  186. }
  187. }
  188. /*-*-*-*-*-*-*-*-*-* Functions w/ Natural Prototypes *-*-*-*-*-*-*-*-*-*-*/
  189. #include "gsl_specfunc__eval.h"
  190. double gsl_sf_bessel_In_scaled(const int n, const double x)
  191. {
  192. EVAL_RESULT(gsl_sf_bessel_In_scaled_e(n, x, &result));
  193. }
  194. double gsl_sf_bessel_In(const int n, const double x)
  195. {
  196. EVAL_RESULT(gsl_sf_bessel_In_e(n, x, &result));
  197. }