iv.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 <math.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 )
  64. double v, x;
  65. {
  66. int sign;
  67. double t, ax;
  68. /* If v is a negative integer, invoke symmetry */
  69. t = floor(v);
  70. if( v < 0.0 )
  71. {
  72. if( t == v )
  73. {
  74. v = -v; /* symmetry */
  75. t = -t;
  76. }
  77. }
  78. /* If x is negative, require v to be an integer */
  79. sign = 1;
  80. if( x < 0.0 )
  81. {
  82. if( t != v )
  83. {
  84. mtherr( "iv", DOMAIN );
  85. return( 0.0 );
  86. }
  87. if( v != 2.0 * floor(v/2.0) )
  88. sign = -1;
  89. }
  90. /* Avoid logarithm singularity */
  91. if( x == 0.0 )
  92. {
  93. if( v == 0.0 )
  94. return( 1.0 );
  95. if( v < 0.0 )
  96. {
  97. mtherr( "iv", OVERFLOW );
  98. return( MAXNUM );
  99. }
  100. else
  101. return( 0.0 );
  102. }
  103. ax = fabs(x);
  104. t = v * log( 0.5 * ax ) - x;
  105. t = sign * exp(t) / gamma( v + 1.0 );
  106. ax = v + 0.5;
  107. return( t * hyperg( ax, 2.0 * ax, 2.0 * x ) );
  108. }