kn.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* kn.c
  2. *
  3. * Modified Bessel function, third kind, integer order
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * double x, y, kn();
  10. * int n;
  11. *
  12. * y = kn( n, x );
  13. *
  14. *
  15. *
  16. * DESCRIPTION:
  17. *
  18. * Returns modified Bessel function of the third kind
  19. * of order n of the argument.
  20. *
  21. * The range is partitioned into the two intervals [0,9.55] and
  22. * (9.55, infinity). An ascending power series is used in the
  23. * low range, and an asymptotic expansion in the high range.
  24. *
  25. *
  26. *
  27. * ACCURACY:
  28. *
  29. * Relative error:
  30. * arithmetic domain # trials peak rms
  31. * DEC 0,30 3000 1.3e-9 5.8e-11
  32. * IEEE 0,30 90000 1.8e-8 3.0e-10
  33. *
  34. * Error is high only near the crossover point x = 9.55
  35. * between the two expansions used.
  36. */
  37. /*
  38. Cephes Math Library Release 2.8: June, 2000
  39. Copyright 1984, 1987, 1988, 2000 by Stephen L. Moshier
  40. */
  41. /*
  42. Algorithm for Kn.
  43. n-1
  44. -n - (n-k-1)! 2 k
  45. K (x) = 0.5 (x/2) > -------- (-x /4)
  46. n - k!
  47. k=0
  48. inf. 2 k
  49. n n - (x /4)
  50. + (-1) 0.5(x/2) > {p(k+1) + p(n+k+1) - 2log(x/2)} ---------
  51. - k! (n+k)!
  52. k=0
  53. where p(m) is the psi function: p(1) = -EUL and
  54. m-1
  55. -
  56. p(m) = -EUL + > 1/k
  57. -
  58. k=1
  59. For large x,
  60. 2 2 2
  61. u-1 (u-1 )(u-3 )
  62. K (z) = sqrt(pi/2z) exp(-z) { 1 + ------- + ------------ + ...}
  63. v 1 2
  64. 1! (8z) 2! (8z)
  65. asymptotically, where
  66. 2
  67. u = 4 v .
  68. */
  69. #include <math.h>
  70. #define EUL 5.772156649015328606065e-1
  71. #define MAXFAC 31
  72. #ifdef ANSIPROT
  73. extern double fabs ( double );
  74. extern double exp ( double );
  75. extern double log ( double );
  76. extern double sqrt ( double );
  77. #else
  78. double fabs(), exp(), log(), sqrt();
  79. #endif
  80. extern double MACHEP, MAXNUM, MAXLOG, PI;
  81. double kn( nn, x )
  82. int nn;
  83. double x;
  84. {
  85. double k, kf, nk1f, nkf, zn, t, s, z0, z;
  86. double ans, fn, pn, pk, zmn, tlg, tox;
  87. int i, n;
  88. if( nn < 0 )
  89. n = -nn;
  90. else
  91. n = nn;
  92. if( n > MAXFAC )
  93. {
  94. overf:
  95. mtherr( "kn", OVERFLOW );
  96. return( MAXNUM );
  97. }
  98. if( x <= 0.0 )
  99. {
  100. if( x < 0.0 )
  101. mtherr( "kn", DOMAIN );
  102. else
  103. mtherr( "kn", SING );
  104. return( MAXNUM );
  105. }
  106. if( x > 9.55 )
  107. goto asymp;
  108. ans = 0.0;
  109. z0 = 0.25 * x * x;
  110. fn = 1.0;
  111. pn = 0.0;
  112. zmn = 1.0;
  113. tox = 2.0/x;
  114. if( n > 0 )
  115. {
  116. /* compute factorial of n and psi(n) */
  117. pn = -EUL;
  118. k = 1.0;
  119. for( i=1; i<n; i++ )
  120. {
  121. pn += 1.0/k;
  122. k += 1.0;
  123. fn *= k;
  124. }
  125. zmn = tox;
  126. if( n == 1 )
  127. {
  128. ans = 1.0/x;
  129. }
  130. else
  131. {
  132. nk1f = fn/n;
  133. kf = 1.0;
  134. s = nk1f;
  135. z = -z0;
  136. zn = 1.0;
  137. for( i=1; i<n; i++ )
  138. {
  139. nk1f = nk1f/(n-i);
  140. kf = kf * i;
  141. zn *= z;
  142. t = nk1f * zn / kf;
  143. s += t;
  144. if( (MAXNUM - fabs(t)) < fabs(s) )
  145. goto overf;
  146. if( (tox > 1.0) && ((MAXNUM/tox) < zmn) )
  147. goto overf;
  148. zmn *= tox;
  149. }
  150. s *= 0.5;
  151. t = fabs(s);
  152. if( (zmn > 1.0) && ((MAXNUM/zmn) < t) )
  153. goto overf;
  154. if( (t > 1.0) && ((MAXNUM/t) < zmn) )
  155. goto overf;
  156. ans = s * zmn;
  157. }
  158. }
  159. tlg = 2.0 * log( 0.5 * x );
  160. pk = -EUL;
  161. if( n == 0 )
  162. {
  163. pn = pk;
  164. t = 1.0;
  165. }
  166. else
  167. {
  168. pn = pn + 1.0/n;
  169. t = 1.0/fn;
  170. }
  171. s = (pk+pn-tlg)*t;
  172. k = 1.0;
  173. do
  174. {
  175. t *= z0 / (k * (k+n));
  176. pk += 1.0/k;
  177. pn += 1.0/(k+n);
  178. s += (pk+pn-tlg)*t;
  179. k += 1.0;
  180. }
  181. while( fabs(t/s) > MACHEP );
  182. s = 0.5 * s / zmn;
  183. if( n & 1 )
  184. s = -s;
  185. ans += s;
  186. return(ans);
  187. /* Asymptotic expansion for Kn(x) */
  188. /* Converges to 1.4e-17 for x > 18.4 */
  189. asymp:
  190. if( x > MAXLOG )
  191. {
  192. mtherr( "kn", UNDERFLOW );
  193. return(0.0);
  194. }
  195. k = n;
  196. pn = 4.0 * k * k;
  197. pk = 1.0;
  198. z0 = 8.0 * x;
  199. fn = 1.0;
  200. t = 1.0;
  201. s = t;
  202. nkf = MAXNUM;
  203. i = 0;
  204. do
  205. {
  206. z = pn - pk * pk;
  207. t = t * z /(fn * z0);
  208. nk1f = fabs(t);
  209. if( (i >= n) && (nk1f > nkf) )
  210. {
  211. goto adone;
  212. }
  213. nkf = nk1f;
  214. s += t;
  215. fn += 1.0;
  216. pk += 2.0;
  217. i += 1;
  218. }
  219. while( fabs(t/s) > MACHEP );
  220. adone:
  221. ans = exp(-x) * sqrt( PI/(2.0*x) ) * s;
  222. return(ans);
  223. }