gsl_specfunc__bessel_Knu.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* specfunc/bessel_Knu.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_exp.h"
  24. #include "gsl_sf_gamma.h"
  25. #include "gsl_sf_bessel.h"
  26. #include "gsl_specfunc__error.h"
  27. #include "gsl_specfunc__bessel.h"
  28. #include "gsl_specfunc__bessel_temme.h"
  29. /*-*-*-*-*-*-*-*-*-*-*-* Functions with Error Codes *-*-*-*-*-*-*-*-*-*-*-*/
  30. int
  31. gsl_sf_bessel_Knu_scaled_e(const double nu, const double x, gsl_sf_result * result)
  32. {
  33. /* CHECK_POINTER(result) */
  34. if(x <= 0.0 || nu < 0.0) {
  35. DOMAIN_ERROR(result);
  36. }
  37. else {
  38. int N = (int)(nu + 0.5);
  39. double mu = nu - N; /* -1/2 <= mu <= 1/2 */
  40. double K_mu, K_mup1, Kp_mu;
  41. double K_nu, K_nup1, K_num1;
  42. int n;
  43. if(x < 2.0) {
  44. gsl_sf_bessel_K_scaled_temme(mu, x, &K_mu, &K_mup1, &Kp_mu);
  45. }
  46. else {
  47. gsl_sf_bessel_K_scaled_steed_temme_CF2(mu, x, &K_mu, &K_mup1, &Kp_mu);
  48. }
  49. /* recurse forward to obtain K_num1, K_nu */
  50. K_nu = K_mu;
  51. K_nup1 = K_mup1;
  52. for(n=0; n<N; n++) {
  53. K_num1 = K_nu;
  54. K_nu = K_nup1;
  55. K_nup1 = 2.0*(mu+n+1)/x * K_nu + K_num1;
  56. }
  57. result->val = K_nu;
  58. result->err = 2.0 * GSL_DBL_EPSILON * (N + 4.0) * fabs(result->val);
  59. return GSL_SUCCESS;
  60. }
  61. }
  62. int
  63. gsl_sf_bessel_Knu_e(const double nu, const double x, gsl_sf_result * result)
  64. {
  65. gsl_sf_result b;
  66. int stat_K = gsl_sf_bessel_Knu_scaled_e(nu, x, &b);
  67. int stat_e = gsl_sf_exp_mult_err_e(-x, 0.0, b.val, b.err, result);
  68. return GSL_ERROR_SELECT_2(stat_e, stat_K);
  69. }
  70. int
  71. gsl_sf_bessel_lnKnu_e(const double nu, const double x, gsl_sf_result * result)
  72. {
  73. /* CHECK_POINTER(result) */
  74. if(x <= 0.0 || nu < 0.0) {
  75. DOMAIN_ERROR(result);
  76. }
  77. else if(nu == 0.0) {
  78. gsl_sf_result K_scaled;
  79. /* This cannot underflow, and
  80. * it will not throw GSL_EDOM
  81. * since that is already checked.
  82. */
  83. gsl_sf_bessel_K0_scaled_e(x, &K_scaled);
  84. result->val = -x + log(fabs(K_scaled.val));
  85. result->err = GSL_DBL_EPSILON * fabs(x) + fabs(K_scaled.err/K_scaled.val);
  86. result->err += GSL_DBL_EPSILON * fabs(result->val);
  87. return GSL_SUCCESS;
  88. }
  89. else if(x < 2.0 && nu > 1.0) {
  90. /* Make use of the inequality
  91. * Knu(x) <= 1/2 (2/x)^nu Gamma(nu),
  92. * which follows from the integral representation
  93. * [Abramowitz+Stegun, 9.6.23 (2)]. With this
  94. * we decide whether or not there is an overflow
  95. * problem because x is small.
  96. */
  97. double ln_bound;
  98. gsl_sf_result lg_nu;
  99. gsl_sf_lngamma_e(nu, &lg_nu);
  100. ln_bound = -M_LN2 - nu*log(0.5*x) + lg_nu.val;
  101. if(ln_bound > GSL_LOG_DBL_MAX - 20.0) {
  102. /* x must be very small or nu very large (or both).
  103. */
  104. double xi = 0.25*x*x;
  105. double sum = 1.0 - xi/(nu-1.0);
  106. if(nu > 2.0) sum += (xi/(nu-1.0)) * (xi/(nu-2.0));
  107. result->val = ln_bound + log(sum);
  108. result->err = lg_nu.err;
  109. result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
  110. return GSL_SUCCESS;
  111. }
  112. /* can drop-through here */
  113. }
  114. {
  115. /* We passed the above tests, so no problem.
  116. * Evaluate as usual. Note the possible drop-through
  117. * in the above code!
  118. */
  119. gsl_sf_result K_scaled;
  120. gsl_sf_bessel_Knu_scaled_e(nu, x, &K_scaled);
  121. result->val = -x + log(fabs(K_scaled.val));
  122. result->err = GSL_DBL_EPSILON * fabs(x) + fabs(K_scaled.err/K_scaled.val);
  123. result->err += GSL_DBL_EPSILON * fabs(result->val);
  124. return GSL_SUCCESS;
  125. }
  126. }
  127. /*-*-*-*-*-*-*-*-*-* Functions w/ Natural Prototypes *-*-*-*-*-*-*-*-*-*-*/
  128. #include "gsl_specfunc__eval.h"
  129. double gsl_sf_bessel_Knu_scaled(const double nu, const double x)
  130. {
  131. EVAL_RESULT(gsl_sf_bessel_Knu_scaled_e(nu, x, &result));
  132. }
  133. double gsl_sf_bessel_Knu(const double nu, const double x)
  134. {
  135. EVAL_RESULT(gsl_sf_bessel_Knu_e(nu, x, &result));
  136. }
  137. double gsl_sf_bessel_lnKnu(const double nu, const double x)
  138. {
  139. EVAL_RESULT(gsl_sf_bessel_lnKnu_e(nu, x, &result));
  140. }