iv.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* iv.c
  2. *
  3. * Modified Bessel function of noninteger order
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * double v, x, y, iv();
  10. *
  11. * y = iv( v, x );
  12. *
  13. *
  14. *
  15. * DESCRIPTION:
  16. *
  17. * Returns modified Bessel function of order v of the
  18. * argument. If x is negative, v must be integer valued.
  19. *
  20. * The function is defined as Iv(x) = Jv( ix ). It is
  21. * here computed in terms of the confluent hypergeometric
  22. * function, according to the formula
  23. *
  24. * v -x
  25. * Iv(x) = (x/2) e hyperg( v+0.5, 2v+1, 2x ) / gamma(v+1)
  26. *
  27. * If v is a negative integer, then v is replaced by -v.
  28. *
  29. *
  30. * ACCURACY:
  31. *
  32. * Tested at random points (v, x), with v between 0 and
  33. * 30, x between 0 and 28.
  34. * Relative error:
  35. * arithmetic domain # trials peak rms
  36. * DEC 0,30 2000 3.1e-15 5.4e-16
  37. * IEEE 0,30 10000 1.7e-14 2.7e-15
  38. *
  39. * Accuracy is diminished if v is near a negative integer.
  40. *
  41. * See also hyperg.c.
  42. *
  43. */
  44. /* iv.c */
  45. /* Modified Bessel function of noninteger order */
  46. /* If x < 0, then v must be an integer. */
  47. /*
  48. Cephes Math Library Release 2.8: June, 2000
  49. Copyright 1984, 1987, 1988, 2000 by Stephen L. Moshier
  50. */
  51. #include "mconf.h"
  52. #ifdef ANSIPROT
  53. extern double hyperg(double, double, double);
  54. extern double exp(double);
  55. extern double gamma(double);
  56. extern double log(double);
  57. extern double fabs(double);
  58. extern double floor(double);
  59. #else
  60. double hyperg(), exp(), gamma(), log(), fabs(), floor();
  61. #endif
  62. extern double MACHEP, MAXNUM;
  63. double iv(v, x) double v, x;
  64. {
  65. int sign;
  66. double t, ax;
  67. /* If v is a negative integer, invoke symmetry */
  68. t = floor(v);
  69. if (v < 0.0) {
  70. if (t == v) {
  71. v = -v; /* symmetry */
  72. t = -t;
  73. }
  74. }
  75. /* If x is negative, require v to be an integer */
  76. sign = 1;
  77. if (x < 0.0) {
  78. if (t != v) {
  79. mtherr("iv", DOMAIN);
  80. return (0.0);
  81. }
  82. if (v != 2.0 * floor(v / 2.0))
  83. sign = -1;
  84. }
  85. /* Avoid logarithm singularity */
  86. if (x == 0.0) {
  87. if (v == 0.0)
  88. return (1.0);
  89. if (v < 0.0) {
  90. mtherr("iv", OVERFLOW);
  91. return (MAXNUM);
  92. } else
  93. return (0.0);
  94. }
  95. ax = fabs(x);
  96. t = v * log(0.5 * ax) - x;
  97. t = sign * exp(t) / gamma(v + 1.0);
  98. ax = v + 0.5;
  99. return (t * hyperg(ax, 2.0 * ax, 2.0 * x));
  100. }