ynf.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* ynf.c
  2. *
  3. * Bessel function of second kind of integer order
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * float x, y, ynf();
  10. * int n;
  11. *
  12. * y = ynf( n, x );
  13. *
  14. *
  15. *
  16. * DESCRIPTION:
  17. *
  18. * Returns Bessel function of order n, where n is a
  19. * (possibly negative) integer.
  20. *
  21. * The function is evaluated by forward recurrence on
  22. * n, starting with values computed by the routines
  23. * y0() and y1().
  24. *
  25. * If n = 0 or 1 the routine for y0 or y1 is called
  26. * directly.
  27. *
  28. *
  29. *
  30. * ACCURACY:
  31. *
  32. *
  33. * Absolute error, except relative when y > 1:
  34. *
  35. * arithmetic domain # trials peak rms
  36. * IEEE 0, 30 10000 2.3e-6 3.4e-7
  37. *
  38. *
  39. * ERROR MESSAGES:
  40. *
  41. * message condition value returned
  42. * yn singularity x = 0 MAXNUMF
  43. * yn overflow MAXNUMF
  44. *
  45. * Spot checked against tables for x, n between 0 and 100.
  46. *
  47. */
  48. /*
  49. Cephes Math Library Release 2.2: June, 1992
  50. Copyright 1984, 1987, 1992 by Stephen L. Moshier
  51. Direct inquiries to 30 Frost Street, Cambridge, MA 02140
  52. */
  53. #include <math.h>
  54. extern float MAXNUMF, MAXLOGF;
  55. float y0f(float), y1f(float), logf(float);
  56. float ynf( int nn, float xx )
  57. {
  58. float x, an, anm1, anm2, r, xinv;
  59. int k, n, sign;
  60. x = xx;
  61. n = nn;
  62. if( n < 0 )
  63. {
  64. n = -n;
  65. if( (n & 1) == 0 ) /* -1**n */
  66. sign = 1;
  67. else
  68. sign = -1;
  69. }
  70. else
  71. sign = 1;
  72. if( n == 0 )
  73. return( sign * y0f(x) );
  74. if( n == 1 )
  75. return( sign * y1f(x) );
  76. /* test for overflow */
  77. if( x <= 0.0 )
  78. {
  79. mtherr( "ynf", SING );
  80. return( -MAXNUMF );
  81. }
  82. if( (x < 1.0) || (n > 29) )
  83. {
  84. an = (float )n;
  85. r = an * logf( an/x );
  86. if( r > MAXLOGF )
  87. {
  88. mtherr( "ynf", OVERFLOW );
  89. return( -MAXNUMF );
  90. }
  91. }
  92. /* forward recurrence on n */
  93. anm2 = y0f(x);
  94. anm1 = y1f(x);
  95. k = 1;
  96. r = 2 * k;
  97. xinv = 1.0/x;
  98. do
  99. {
  100. an = r * anm1 * xinv - anm2;
  101. anm2 = anm1;
  102. anm1 = an;
  103. r += 2.0;
  104. ++k;
  105. }
  106. while( k < n );
  107. return( sign * an );
  108. }