gsl_specfunc__bessel_Kn.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /* specfunc/bessel_Kn.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_gamma.h"
  24. #include "gsl_sf_psi.h"
  25. #include "gsl_sf_bessel.h"
  26. #include "gsl_specfunc__error.h"
  27. #include "gsl_specfunc__bessel.h"
  28. /*-*-*-*-*-*-*-*-*-*-*-* Private Section *-*-*-*-*-*-*-*-*-*-*-*/
  29. /* [Abramowitz+Stegun, 9.6.11]
  30. * assumes n >= 1
  31. */
  32. static
  33. int
  34. bessel_Kn_scaled_small_x(const int n, const double x, gsl_sf_result * result)
  35. {
  36. int k;
  37. double y = 0.25 * x * x;
  38. double ln_x_2 = log(0.5*x);
  39. double ex = exp(x);
  40. gsl_sf_result ln_nm1_fact;
  41. double k_term;
  42. double term1, sum1, ln_pre1;
  43. double term2, sum2, pre2;
  44. gsl_sf_lnfact_e((unsigned int)(n-1), &ln_nm1_fact);
  45. ln_pre1 = -n*ln_x_2 + ln_nm1_fact.val;
  46. if(ln_pre1 > GSL_LOG_DBL_MAX - 3.0) GSL_ERROR ("error", GSL_EOVRFLW);
  47. sum1 = 1.0;
  48. k_term = 1.0;
  49. for(k=1; k<=n-1; k++) {
  50. k_term *= -y/(k * (n-k));
  51. sum1 += k_term;
  52. }
  53. term1 = 0.5 * exp(ln_pre1) * sum1;
  54. pre2 = 0.5 * exp(n*ln_x_2);
  55. if(pre2 > 0.0) {
  56. const int KMAX = 20;
  57. gsl_sf_result psi_n;
  58. gsl_sf_result npk_fact;
  59. double yk = 1.0;
  60. double k_fact = 1.0;
  61. double psi_kp1 = -M_EULER;
  62. double psi_npkp1;
  63. gsl_sf_psi_int_e(n, &psi_n);
  64. gsl_sf_fact_e((unsigned int)n, &npk_fact);
  65. psi_npkp1 = psi_n.val + 1.0/n;
  66. sum2 = (psi_kp1 + psi_npkp1 - 2.0*ln_x_2)/npk_fact.val;
  67. for(k=1; k<KMAX; k++) {
  68. psi_kp1 += 1.0/k;
  69. psi_npkp1 += 1.0/(n+k);
  70. k_fact *= k;
  71. npk_fact.val *= n+k;
  72. yk *= y;
  73. k_term = yk*(psi_kp1 + psi_npkp1 - 2.0*ln_x_2)/(k_fact*npk_fact.val);
  74. sum2 += k_term;
  75. }
  76. term2 = ( GSL_IS_ODD(n) ? -1.0 : 1.0 ) * pre2 * sum2;
  77. }
  78. else {
  79. term2 = 0.0;
  80. }
  81. result->val = ex * (term1 + term2);
  82. result->err = ex * GSL_DBL_EPSILON * (fabs(ln_pre1)*fabs(term1) + fabs(term2));
  83. result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
  84. return GSL_SUCCESS;
  85. }
  86. /*-*-*-*-*-*-*-*-*-*-*-* Functions with Error Codes *-*-*-*-*-*-*-*-*-*-*-*/
  87. int gsl_sf_bessel_Kn_scaled_e(int n, const double x, gsl_sf_result * result)
  88. {
  89. n = abs(n); /* K(-n, z) = K(n, z) */
  90. /* CHECK_POINTER(result) */
  91. if(x <= 0.0) {
  92. DOMAIN_ERROR(result);
  93. }
  94. else if(n == 0) {
  95. return gsl_sf_bessel_K0_scaled_e(x, result);
  96. }
  97. else if(n == 1) {
  98. return gsl_sf_bessel_K1_scaled_e(x, result);
  99. }
  100. else if(x <= 5.0) {
  101. return bessel_Kn_scaled_small_x(n, x, result);
  102. }
  103. else if(GSL_ROOT3_DBL_EPSILON * x > 0.25 * (n*n + 1)) {
  104. return gsl_sf_bessel_Knu_scaled_asympx_e((double)n, x, result);
  105. }
  106. else if(GSL_MIN(0.29/(n*n), 0.5/(n*n + x*x)) < GSL_ROOT3_DBL_EPSILON) {
  107. return gsl_sf_bessel_Knu_scaled_asymp_unif_e((double)n, x, result);
  108. }
  109. else {
  110. /* Upward recurrence. [Gradshteyn + Ryzhik, 8.471.1] */
  111. double two_over_x = 2.0/x;
  112. gsl_sf_result r_b_jm1;
  113. gsl_sf_result r_b_j;
  114. int stat_0 = gsl_sf_bessel_K0_scaled_e(x, &r_b_jm1);
  115. int stat_1 = gsl_sf_bessel_K1_scaled_e(x, &r_b_j);
  116. double b_jm1 = r_b_jm1.val;
  117. double b_j = r_b_j.val;
  118. double b_jp1;
  119. int j;
  120. for(j=1; j<n; j++) {
  121. b_jp1 = b_jm1 + j * two_over_x * b_j;
  122. b_jm1 = b_j;
  123. b_j = b_jp1;
  124. }
  125. result->val = b_j;
  126. result->err = n * (fabs(b_j) * (fabs(r_b_jm1.err/r_b_jm1.val) + fabs(r_b_j.err/r_b_j.val)));
  127. result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
  128. return GSL_ERROR_SELECT_2(stat_0, stat_1);
  129. }
  130. }
  131. int gsl_sf_bessel_Kn_e(const int n, const double x, gsl_sf_result * result)
  132. {
  133. const int status = gsl_sf_bessel_Kn_scaled_e(n, x, result);
  134. const double ex = exp(-x);
  135. result->val *= ex;
  136. result->err *= ex;
  137. result->err += x * GSL_DBL_EPSILON * fabs(result->val);
  138. return status;
  139. }
  140. int gsl_sf_bessel_Kn_scaled_array(const int nmin, const int nmax, const double x, double * result_array)
  141. {
  142. /* CHECK_POINTER(result_array) */
  143. if(nmin < 0 || nmax < nmin || x <= 0.0) {
  144. int j;
  145. for(j=0; j<=nmax-nmin; j++) result_array[j] = 0.0;
  146. GSL_ERROR ("domain error", GSL_EDOM);
  147. }
  148. else if(nmax == 0) {
  149. gsl_sf_result b;
  150. int stat = gsl_sf_bessel_K0_scaled_e(x, &b);
  151. result_array[0] = b.val;
  152. return stat;
  153. }
  154. else {
  155. double two_over_x = 2.0/x;
  156. gsl_sf_result r_Knm1;
  157. gsl_sf_result r_Kn;
  158. int stat_0 = gsl_sf_bessel_Kn_scaled_e(nmin, x, &r_Knm1);
  159. int stat_1 = gsl_sf_bessel_Kn_scaled_e(nmin+1, x, &r_Kn);
  160. int stat = GSL_ERROR_SELECT_2(stat_0, stat_1);
  161. double Knp1;
  162. double Kn = r_Kn.val;
  163. double Knm1 = r_Knm1.val;
  164. int n;
  165. for(n=nmin+1; n<=nmax+1; n++) {
  166. if(Knm1 < GSL_DBL_MAX) {
  167. result_array[n-1-nmin] = Knm1;
  168. Knp1 = Knm1 + n * two_over_x * Kn;
  169. Knm1 = Kn;
  170. Kn = Knp1;
  171. }
  172. else {
  173. /* Overflow. Set the rest of the elements to
  174. * zero and bug out.
  175. * FIXME: Note: this relies on the convention
  176. * that the test x < DBL_MIN fails for x not
  177. * a number. This may be only an IEEE convention,
  178. * so the portability is unclear.
  179. */
  180. int j;
  181. for(j=n; j<=nmax+1; j++) result_array[j-1-nmin] = 0.0;
  182. GSL_ERROR ("overflow", GSL_EOVRFLW);
  183. }
  184. }
  185. return stat;
  186. }
  187. }
  188. int
  189. gsl_sf_bessel_Kn_array(const int nmin, const int nmax, const double x, double * result_array)
  190. {
  191. int status = gsl_sf_bessel_Kn_scaled_array(nmin, nmax, x, result_array);
  192. double ex = exp(-x);
  193. int i;
  194. for(i=0; i<=nmax-nmin; i++) result_array[i] *= ex;
  195. return status;
  196. }
  197. /*-*-*-*-*-*-*-*-*-* Functions w/ Natural Prototypes *-*-*-*-*-*-*-*-*-*-*/
  198. #include "gsl_specfunc__eval.h"
  199. double gsl_sf_bessel_Kn_scaled(const int n, const double x)
  200. {
  201. EVAL_RESULT(gsl_sf_bessel_Kn_scaled_e(n, x, &result));
  202. }
  203. double gsl_sf_bessel_Kn(const int n, const double x)
  204. {
  205. EVAL_RESULT(gsl_sf_bessel_Kn_e(n, x, &result));
  206. }