k0.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /* k0.c
  2. *
  3. * Modified Bessel function, third kind, order zero
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * double x, y, k0();
  10. *
  11. * y = k0( x );
  12. *
  13. *
  14. *
  15. * DESCRIPTION:
  16. *
  17. * Returns modified Bessel function of the third kind
  18. * of order zero of the argument.
  19. *
  20. * The range is partitioned into the two intervals [0,8] and
  21. * (8, infinity). Chebyshev polynomial expansions are employed
  22. * in each interval.
  23. *
  24. *
  25. *
  26. * ACCURACY:
  27. *
  28. * Tested at 2000 random points between 0 and 8. Peak absolute
  29. * error (relative when K0 > 1) was 1.46e-14; rms, 4.26e-15.
  30. * Relative error:
  31. * arithmetic domain # trials peak rms
  32. * DEC 0, 30 3100 1.3e-16 2.1e-17
  33. * IEEE 0, 30 30000 1.2e-15 1.6e-16
  34. *
  35. * ERROR MESSAGES:
  36. *
  37. * message condition value returned
  38. * K0 domain x <= 0 MAXNUM
  39. *
  40. */
  41. /* k0e()
  42. *
  43. * Modified Bessel function, third kind, order zero,
  44. * exponentially scaled
  45. *
  46. *
  47. *
  48. * SYNOPSIS:
  49. *
  50. * double x, y, k0e();
  51. *
  52. * y = k0e( x );
  53. *
  54. *
  55. *
  56. * DESCRIPTION:
  57. *
  58. * Returns exponentially scaled modified Bessel function
  59. * of the third kind of order zero of the argument.
  60. *
  61. *
  62. *
  63. * ACCURACY:
  64. *
  65. * Relative error:
  66. * arithmetic domain # trials peak rms
  67. * IEEE 0, 30 30000 1.4e-15 1.4e-16
  68. * See k0().
  69. *
  70. */
  71. /*
  72. Cephes Math Library Release 2.8: June, 2000
  73. Copyright 1984, 1987, 2000 by Stephen L. Moshier
  74. */
  75. #include <math.h>
  76. /* Chebyshev coefficients for K0(x) + log(x/2) I0(x)
  77. * in the interval [0,2]. The odd order coefficients are all
  78. * zero; only the even order coefficients are listed.
  79. *
  80. * lim(x->0){ K0(x) + log(x/2) I0(x) } = -EUL.
  81. */
  82. #ifdef UNK
  83. static double A[] =
  84. {
  85. 1.37446543561352307156E-16,
  86. 4.25981614279661018399E-14,
  87. 1.03496952576338420167E-11,
  88. 1.90451637722020886025E-9,
  89. 2.53479107902614945675E-7,
  90. 2.28621210311945178607E-5,
  91. 1.26461541144692592338E-3,
  92. 3.59799365153615016266E-2,
  93. 3.44289899924628486886E-1,
  94. -5.35327393233902768720E-1
  95. };
  96. #endif
  97. #ifdef DEC
  98. static unsigned short A[] = {
  99. 0023036,0073417,0032477,0165673,
  100. 0025077,0154126,0016046,0012517,
  101. 0027066,0011342,0035211,0005041,
  102. 0031002,0160233,0037454,0050224,
  103. 0032610,0012747,0037712,0173741,
  104. 0034277,0144007,0172147,0162375,
  105. 0035645,0140563,0125431,0165626,
  106. 0037023,0057662,0125124,0102051,
  107. 0037660,0043304,0004411,0166707,
  108. 0140011,0005467,0047227,0130370
  109. };
  110. #endif
  111. #ifdef IBMPC
  112. static unsigned short A[] = {
  113. 0xfd77,0xe6a7,0xcee1,0x3ca3,
  114. 0xc2aa,0xc384,0xfb0a,0x3d27,
  115. 0x2144,0x4751,0xc25c,0x3da6,
  116. 0x8a13,0x67e5,0x5c13,0x3e20,
  117. 0x5efc,0xe7f9,0x02bc,0x3e91,
  118. 0xfca0,0xfe8c,0xf900,0x3ef7,
  119. 0x3d73,0x7563,0xb82e,0x3f54,
  120. 0x9085,0x554a,0x6bf6,0x3fa2,
  121. 0x3db9,0x8121,0x08d8,0x3fd6,
  122. 0xf61f,0xe9d2,0x2166,0xbfe1
  123. };
  124. #endif
  125. #ifdef MIEEE
  126. static unsigned short A[] = {
  127. 0x3ca3,0xcee1,0xe6a7,0xfd77,
  128. 0x3d27,0xfb0a,0xc384,0xc2aa,
  129. 0x3da6,0xc25c,0x4751,0x2144,
  130. 0x3e20,0x5c13,0x67e5,0x8a13,
  131. 0x3e91,0x02bc,0xe7f9,0x5efc,
  132. 0x3ef7,0xf900,0xfe8c,0xfca0,
  133. 0x3f54,0xb82e,0x7563,0x3d73,
  134. 0x3fa2,0x6bf6,0x554a,0x9085,
  135. 0x3fd6,0x08d8,0x8121,0x3db9,
  136. 0xbfe1,0x2166,0xe9d2,0xf61f
  137. };
  138. #endif
  139. /* Chebyshev coefficients for exp(x) sqrt(x) K0(x)
  140. * in the inverted interval [2,infinity].
  141. *
  142. * lim(x->inf){ exp(x) sqrt(x) K0(x) } = sqrt(pi/2).
  143. */
  144. #ifdef UNK
  145. static double B[] = {
  146. 5.30043377268626276149E-18,
  147. -1.64758043015242134646E-17,
  148. 5.21039150503902756861E-17,
  149. -1.67823109680541210385E-16,
  150. 5.51205597852431940784E-16,
  151. -1.84859337734377901440E-15,
  152. 6.34007647740507060557E-15,
  153. -2.22751332699166985548E-14,
  154. 8.03289077536357521100E-14,
  155. -2.98009692317273043925E-13,
  156. 1.14034058820847496303E-12,
  157. -4.51459788337394416547E-12,
  158. 1.85594911495471785253E-11,
  159. -7.95748924447710747776E-11,
  160. 3.57739728140030116597E-10,
  161. -1.69753450938905987466E-9,
  162. 8.57403401741422608519E-9,
  163. -4.66048989768794782956E-8,
  164. 2.76681363944501510342E-7,
  165. -1.83175552271911948767E-6,
  166. 1.39498137188764993662E-5,
  167. -1.28495495816278026384E-4,
  168. 1.56988388573005337491E-3,
  169. -3.14481013119645005427E-2,
  170. 2.44030308206595545468E0
  171. };
  172. #endif
  173. #ifdef DEC
  174. static unsigned short B[] = {
  175. 0021703,0106456,0076144,0173406,
  176. 0122227,0173144,0116011,0030033,
  177. 0022560,0044562,0006506,0067642,
  178. 0123101,0076243,0123273,0131013,
  179. 0023436,0157713,0056243,0141331,
  180. 0124005,0032207,0063726,0164664,
  181. 0024344,0066342,0051756,0162300,
  182. 0124710,0121365,0154053,0077022,
  183. 0025264,0161166,0066246,0077420,
  184. 0125647,0141671,0006443,0103212,
  185. 0026240,0076431,0077147,0160445,
  186. 0126636,0153741,0174002,0105031,
  187. 0027243,0040102,0035375,0163073,
  188. 0127656,0176256,0113476,0044653,
  189. 0030304,0125544,0006377,0130104,
  190. 0130751,0047257,0110537,0127324,
  191. 0031423,0046400,0014772,0012164,
  192. 0132110,0025240,0155247,0112570,
  193. 0032624,0105314,0007437,0021574,
  194. 0133365,0155243,0174306,0116506,
  195. 0034152,0004776,0061643,0102504,
  196. 0135006,0136277,0036104,0175023,
  197. 0035715,0142217,0162474,0115022,
  198. 0137000,0147671,0065177,0134356,
  199. 0040434,0026754,0175163,0044070
  200. };
  201. #endif
  202. #ifdef IBMPC
  203. static unsigned short B[] = {
  204. 0x9ee1,0xcf8c,0x71a5,0x3c58,
  205. 0x2603,0x9381,0xfecc,0xbc72,
  206. 0xcdf4,0x41a8,0x092e,0x3c8e,
  207. 0x7641,0x74d7,0x2f94,0xbca8,
  208. 0x785b,0x6b94,0xdbf9,0x3cc3,
  209. 0xdd36,0xecfa,0xa690,0xbce0,
  210. 0xdc98,0x4a7d,0x8d9c,0x3cfc,
  211. 0x6fc2,0xbb05,0x145e,0xbd19,
  212. 0xcfe2,0xcd94,0x9c4e,0x3d36,
  213. 0x70d1,0x21a4,0xf877,0xbd54,
  214. 0xfc25,0x2fcc,0x0fa3,0x3d74,
  215. 0x5143,0x3f00,0xdafc,0xbd93,
  216. 0xbcc7,0x475f,0x6808,0x3db4,
  217. 0xc935,0xd2e7,0xdf95,0xbdd5,
  218. 0xf608,0x819f,0x956c,0x3df8,
  219. 0xf5db,0xf22b,0x29d5,0xbe1d,
  220. 0x428e,0x033f,0x69a0,0x3e42,
  221. 0xf2af,0x1b54,0x0554,0xbe69,
  222. 0xe46f,0x81e3,0x9159,0x3e92,
  223. 0xd3a9,0x7f18,0xbb54,0xbebe,
  224. 0x70a9,0xcc74,0x413f,0x3eed,
  225. 0x9f42,0xe788,0xd797,0xbf20,
  226. 0x9342,0xfca7,0xb891,0x3f59,
  227. 0xf71e,0x2d4f,0x19f7,0xbfa0,
  228. 0x6907,0x9f4e,0x85bd,0x4003
  229. };
  230. #endif
  231. #ifdef MIEEE
  232. static unsigned short B[] = {
  233. 0x3c58,0x71a5,0xcf8c,0x9ee1,
  234. 0xbc72,0xfecc,0x9381,0x2603,
  235. 0x3c8e,0x092e,0x41a8,0xcdf4,
  236. 0xbca8,0x2f94,0x74d7,0x7641,
  237. 0x3cc3,0xdbf9,0x6b94,0x785b,
  238. 0xbce0,0xa690,0xecfa,0xdd36,
  239. 0x3cfc,0x8d9c,0x4a7d,0xdc98,
  240. 0xbd19,0x145e,0xbb05,0x6fc2,
  241. 0x3d36,0x9c4e,0xcd94,0xcfe2,
  242. 0xbd54,0xf877,0x21a4,0x70d1,
  243. 0x3d74,0x0fa3,0x2fcc,0xfc25,
  244. 0xbd93,0xdafc,0x3f00,0x5143,
  245. 0x3db4,0x6808,0x475f,0xbcc7,
  246. 0xbdd5,0xdf95,0xd2e7,0xc935,
  247. 0x3df8,0x956c,0x819f,0xf608,
  248. 0xbe1d,0x29d5,0xf22b,0xf5db,
  249. 0x3e42,0x69a0,0x033f,0x428e,
  250. 0xbe69,0x0554,0x1b54,0xf2af,
  251. 0x3e92,0x9159,0x81e3,0xe46f,
  252. 0xbebe,0xbb54,0x7f18,0xd3a9,
  253. 0x3eed,0x413f,0xcc74,0x70a9,
  254. 0xbf20,0xd797,0xe788,0x9f42,
  255. 0x3f59,0xb891,0xfca7,0x9342,
  256. 0xbfa0,0x19f7,0x2d4f,0xf71e,
  257. 0x4003,0x85bd,0x9f4e,0x6907
  258. };
  259. #endif
  260. /* k0.c */
  261. #ifdef ANSIPROT
  262. extern double chbevl ( double, void *, int );
  263. extern double exp ( double );
  264. extern double i0 ( double );
  265. extern double log ( double );
  266. extern double sqrt ( double );
  267. #else
  268. double chbevl(), exp(), i0(), log(), sqrt();
  269. #endif
  270. extern double PI;
  271. extern double MAXNUM;
  272. double k0(x)
  273. double x;
  274. {
  275. double y, z;
  276. if( x <= 0.0 )
  277. {
  278. mtherr( "k0", DOMAIN );
  279. return( MAXNUM );
  280. }
  281. if( x <= 2.0 )
  282. {
  283. y = x * x - 2.0;
  284. y = chbevl( y, A, 10 ) - log( 0.5 * x ) * i0(x);
  285. return( y );
  286. }
  287. z = 8.0/x - 2.0;
  288. y = exp(-x) * chbevl( z, B, 25 ) / sqrt(x);
  289. return(y);
  290. }
  291. double k0e( x )
  292. double x;
  293. {
  294. double y;
  295. if( x <= 0.0 )
  296. {
  297. mtherr( "k0e", DOMAIN );
  298. return( MAXNUM );
  299. }
  300. if( x <= 2.0 )
  301. {
  302. y = x * x - 2.0;
  303. y = chbevl( y, A, 10 ) - log( 0.5 * x ) * i0(x);
  304. return( y * exp(x) );
  305. }
  306. y = chbevl( 8.0/x - 2.0, B, 25 ) / sqrt(x);
  307. return(y);
  308. }