gsl_specfunc__hyperg_0F1.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* specfunc/hyperg_0F1.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_sf_hyperg.h"
  27. #include "gsl_specfunc__error.h"
  28. #define locEPS (1000.0*GSL_DBL_EPSILON)
  29. /* Evaluate bessel_I(nu, x), allowing nu < 0.
  30. * This is fine here because we do not not allow
  31. * nu to be a negative integer.
  32. * x > 0.
  33. */
  34. static
  35. int
  36. hyperg_0F1_bessel_I(const double nu, const double x, gsl_sf_result * result)
  37. {
  38. if(x > GSL_LOG_DBL_MAX) {
  39. OVERFLOW_ERROR(result);
  40. }
  41. if(nu < 0.0) {
  42. const double anu = -nu;
  43. const double s = 2.0/M_PI * sin(anu*M_PI);
  44. const double ex = exp(x);
  45. gsl_sf_result I;
  46. gsl_sf_result K;
  47. int stat_I = gsl_sf_bessel_Inu_scaled_e(anu, x, &I);
  48. int stat_K = gsl_sf_bessel_Knu_scaled_e(anu, x, &K);
  49. result->val = ex * I.val + s * (K.val / ex);
  50. result->err = ex * I.err + fabs(s * K.err/ex);
  51. result->err += fabs(s * (K.val/ex)) * GSL_DBL_EPSILON * anu * M_PI;
  52. return GSL_ERROR_SELECT_2(stat_K, stat_I);
  53. }
  54. else {
  55. const double ex = exp(x);
  56. gsl_sf_result I;
  57. int stat_I = gsl_sf_bessel_Inu_scaled_e(nu, x, &I);
  58. result->val = ex * I.val;
  59. result->err = ex * I.err + GSL_DBL_EPSILON * fabs(result->val);
  60. return stat_I;
  61. }
  62. }
  63. /* Evaluate bessel_J(nu, x), allowing nu < 0.
  64. * This is fine here because we do not not allow
  65. * nu to be a negative integer.
  66. * x > 0.
  67. */
  68. static
  69. int
  70. hyperg_0F1_bessel_J(const double nu, const double x, gsl_sf_result * result)
  71. {
  72. if(nu < 0.0) {
  73. const double anu = -nu;
  74. const double s = sin(anu*M_PI);
  75. const double c = cos(anu*M_PI);
  76. gsl_sf_result J;
  77. gsl_sf_result Y;
  78. int stat_J = gsl_sf_bessel_Jnu_e(anu, x, &J);
  79. int stat_Y = gsl_sf_bessel_Ynu_e(anu, x, &Y);
  80. result->val = c * J.val - s * Y.val;
  81. result->err = fabs(c * J.err) + fabs(s * Y.err);
  82. result->err += fabs(anu * M_PI) * GSL_DBL_EPSILON * fabs(J.val + Y.val);
  83. return GSL_ERROR_SELECT_2(stat_Y, stat_J);
  84. }
  85. else {
  86. return gsl_sf_bessel_Jnu_e(nu, x, result);
  87. }
  88. }
  89. /*-*-*-*-*-*-*-*-*-*-*-* Functions with Error Codes *-*-*-*-*-*-*-*-*-*-*-*/
  90. int
  91. gsl_sf_hyperg_0F1_e(double c, double x, gsl_sf_result * result)
  92. {
  93. const double rintc = floor(c + 0.5);
  94. const int c_neg_integer = (c < 0.0 && fabs(c - rintc) < locEPS);
  95. /* CHECK_POINTER(result) */
  96. if(c == 0.0 || c_neg_integer) {
  97. DOMAIN_ERROR(result);
  98. }
  99. else if(x < 0.0) {
  100. gsl_sf_result Jcm1;
  101. gsl_sf_result lg_c;
  102. double sgn;
  103. int stat_g = gsl_sf_lngamma_sgn_e(c, &lg_c, &sgn);
  104. int stat_J = hyperg_0F1_bessel_J(c-1.0, 2.0*sqrt(-x), &Jcm1);
  105. if(stat_g != GSL_SUCCESS) {
  106. result->val = 0.0;
  107. result->err = 0.0;
  108. return stat_g;
  109. }
  110. else if(Jcm1.val == 0.0) {
  111. result->val = 0.0;
  112. result->err = 0.0;
  113. return stat_J;
  114. }
  115. else {
  116. const double tl = log(-x)*0.5*(1.0-c);
  117. double ln_pre_val = lg_c.val + tl;
  118. double ln_pre_err = lg_c.err + 2.0 * GSL_DBL_EPSILON * fabs(tl);
  119. return gsl_sf_exp_mult_err_e(ln_pre_val, ln_pre_err,
  120. sgn*Jcm1.val, Jcm1.err,
  121. result);
  122. }
  123. }
  124. else if(x == 0.0) {
  125. result->val = 1.0;
  126. result->err = 1.0;
  127. return GSL_SUCCESS;
  128. }
  129. else {
  130. gsl_sf_result Icm1;
  131. gsl_sf_result lg_c;
  132. double sgn;
  133. int stat_g = gsl_sf_lngamma_sgn_e(c, &lg_c, &sgn);
  134. int stat_I = hyperg_0F1_bessel_I(c-1.0, 2.0*sqrt(x), &Icm1);
  135. if(stat_g != GSL_SUCCESS) {
  136. result->val = 0.0;
  137. result->err = 0.0;
  138. return stat_g;
  139. }
  140. else if(Icm1.val == 0.0) {
  141. result->val = 0.0;
  142. result->err = 0.0;
  143. return stat_I;
  144. }
  145. else {
  146. const double tl = log(x)*0.5*(1.0-c);
  147. const double ln_pre_val = lg_c.val + tl;
  148. const double ln_pre_err = lg_c.err + 2.0 * GSL_DBL_EPSILON * fabs(tl);
  149. return gsl_sf_exp_mult_err_e(ln_pre_val, ln_pre_err,
  150. sgn*Icm1.val, Icm1.err,
  151. result);
  152. }
  153. }
  154. }
  155. /*-*-*-*-*-*-*-*-*-* Functions w/ Natural Prototypes *-*-*-*-*-*-*-*-*-*-*/
  156. #include "gsl_specfunc__eval.h"
  157. double gsl_sf_hyperg_0F1(const double c, const double x)
  158. {
  159. EVAL_RESULT(gsl_sf_hyperg_0F1_e(c, x, &result));
  160. }