jn.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. // Copyright 2010 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package math
  5. /*
  6. Bessel function of the first and second kinds of order n.
  7. */
  8. // The original C code and the long comment below are
  9. // from FreeBSD's /usr/src/lib/msun/src/e_jn.c and
  10. // came with this notice. The go code is a simplified
  11. // version of the original C.
  12. //
  13. // ====================================================
  14. // Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  15. //
  16. // Developed at SunPro, a Sun Microsystems, Inc. business.
  17. // Permission to use, copy, modify, and distribute this
  18. // software is freely granted, provided that this notice
  19. // is preserved.
  20. // ====================================================
  21. //
  22. // __ieee754_jn(n, x), __ieee754_yn(n, x)
  23. // floating point Bessel's function of the 1st and 2nd kind
  24. // of order n
  25. //
  26. // Special cases:
  27. // y0(0)=y1(0)=yn(n,0) = -inf with division by zero signal;
  28. // y0(-ve)=y1(-ve)=yn(n,-ve) are NaN with invalid signal.
  29. // Note 2. About jn(n,x), yn(n,x)
  30. // For n=0, j0(x) is called,
  31. // for n=1, j1(x) is called,
  32. // for n<x, forward recursion is used starting
  33. // from values of j0(x) and j1(x).
  34. // for n>x, a continued fraction approximation to
  35. // j(n,x)/j(n-1,x) is evaluated and then backward
  36. // recursion is used starting from a supposed value
  37. // for j(n,x). The resulting value of j(0,x) is
  38. // compared with the actual value to correct the
  39. // supposed value of j(n,x).
  40. //
  41. // yn(n,x) is similar in all respects, except
  42. // that forward recursion is used for all
  43. // values of n>1.
  44. // Jn returns the order-n Bessel function of the first kind.
  45. //
  46. // Special cases are:
  47. // Jn(n, ±Inf) = 0
  48. // Jn(n, NaN) = NaN
  49. func Jn(n int, x float64) float64 {
  50. const (
  51. TwoM29 = 1.0 / (1 << 29) // 2**-29 0x3e10000000000000
  52. Two302 = 1 << 302 // 2**302 0x52D0000000000000
  53. )
  54. // special cases
  55. switch {
  56. case IsNaN(x):
  57. return x
  58. case IsInf(x, 0):
  59. return 0
  60. }
  61. // J(-n, x) = (-1)**n * J(n, x), J(n, -x) = (-1)**n * J(n, x)
  62. // Thus, J(-n, x) = J(n, -x)
  63. if n == 0 {
  64. return J0(x)
  65. }
  66. if x == 0 {
  67. return 0
  68. }
  69. if n < 0 {
  70. n, x = -n, -x
  71. }
  72. if n == 1 {
  73. return J1(x)
  74. }
  75. sign := false
  76. if x < 0 {
  77. x = -x
  78. if n&1 == 1 {
  79. sign = true // odd n and negative x
  80. }
  81. }
  82. var b float64
  83. if float64(n) <= x {
  84. // Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x)
  85. if x >= Two302 { // x > 2**302
  86. // (x >> n**2)
  87. // Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
  88. // Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
  89. // Let s=sin(x), c=cos(x),
  90. // xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
  91. //
  92. // n sin(xn)*sqt2 cos(xn)*sqt2
  93. // ----------------------------------
  94. // 0 s-c c+s
  95. // 1 -s-c -c+s
  96. // 2 -s+c -c-s
  97. // 3 s+c c-s
  98. var temp float64
  99. switch n & 3 {
  100. case 0:
  101. temp = Cos(x) + Sin(x)
  102. case 1:
  103. temp = -Cos(x) + Sin(x)
  104. case 2:
  105. temp = -Cos(x) - Sin(x)
  106. case 3:
  107. temp = Cos(x) - Sin(x)
  108. }
  109. b = (1 / SqrtPi) * temp / Sqrt(x)
  110. } else {
  111. b = J1(x)
  112. for i, a := 1, J0(x); i < n; i++ {
  113. a, b = b, b*(float64(i+i)/x)-a // avoid underflow
  114. }
  115. }
  116. } else {
  117. if x < TwoM29 { // x < 2**-29
  118. // x is tiny, return the first Taylor expansion of J(n,x)
  119. // J(n,x) = 1/n!*(x/2)**n - ...
  120. if n > 33 { // underflow
  121. b = 0
  122. } else {
  123. temp := x * 0.5
  124. b = temp
  125. a := 1.0
  126. for i := 2; i <= n; i++ {
  127. a *= float64(i) // a = n!
  128. b *= temp // b = (x/2)**n
  129. }
  130. b /= a
  131. }
  132. } else {
  133. // use backward recurrence
  134. // x x**2 x**2
  135. // J(n,x)/J(n-1,x) = ---- ------ ------ .....
  136. // 2n - 2(n+1) - 2(n+2)
  137. //
  138. // 1 1 1
  139. // (for large x) = ---- ------ ------ .....
  140. // 2n 2(n+1) 2(n+2)
  141. // -- - ------ - ------ -
  142. // x x x
  143. //
  144. // Let w = 2n/x and h=2/x, then the above quotient
  145. // is equal to the continued fraction:
  146. // 1
  147. // = -----------------------
  148. // 1
  149. // w - -----------------
  150. // 1
  151. // w+h - ---------
  152. // w+2h - ...
  153. //
  154. // To determine how many terms needed, let
  155. // Q(0) = w, Q(1) = w(w+h) - 1,
  156. // Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
  157. // When Q(k) > 1e4 good for single
  158. // When Q(k) > 1e9 good for double
  159. // When Q(k) > 1e17 good for quadruple
  160. // determine k
  161. w := float64(n+n) / x
  162. h := 2 / x
  163. q0 := w
  164. z := w + h
  165. q1 := w*z - 1
  166. k := 1
  167. for q1 < 1e9 {
  168. k += 1
  169. z += h
  170. q0, q1 = q1, z*q1-q0
  171. }
  172. m := n + n
  173. t := 0.0
  174. for i := 2 * (n + k); i >= m; i -= 2 {
  175. t = 1 / (float64(i)/x - t)
  176. }
  177. a := t
  178. b = 1
  179. // estimate log((2/x)**n*n!) = n*log(2/x)+n*ln(n)
  180. // Hence, if n*(log(2n/x)) > ...
  181. // single 8.8722839355e+01
  182. // double 7.09782712893383973096e+02
  183. // long double 1.1356523406294143949491931077970765006170e+04
  184. // then recurrent value may overflow and the result is
  185. // likely underflow to zero
  186. tmp := float64(n)
  187. v := 2 / x
  188. tmp = tmp * Log(Abs(v*tmp))
  189. if tmp < 7.09782712893383973096e+02 {
  190. for i := n - 1; i > 0; i-- {
  191. di := float64(i + i)
  192. a, b = b, b*di/x-a
  193. di -= 2
  194. }
  195. } else {
  196. for i := n - 1; i > 0; i-- {
  197. di := float64(i + i)
  198. a, b = b, b*di/x-a
  199. di -= 2
  200. // scale b to avoid spurious overflow
  201. if b > 1e100 {
  202. a /= b
  203. t /= b
  204. b = 1
  205. }
  206. }
  207. }
  208. b = t * J0(x) / b
  209. }
  210. }
  211. if sign {
  212. return -b
  213. }
  214. return b
  215. }
  216. // Yn returns the order-n Bessel function of the second kind.
  217. //
  218. // Special cases are:
  219. // Yn(n, +Inf) = 0
  220. // Yn(n > 0, 0) = -Inf
  221. // Yn(n < 0, 0) = +Inf if n is odd, -Inf if n is even
  222. // Y1(n, x < 0) = NaN
  223. // Y1(n, NaN) = NaN
  224. func Yn(n int, x float64) float64 {
  225. const Two302 = 1 << 302 // 2**302 0x52D0000000000000
  226. // special cases
  227. switch {
  228. case x < 0 || IsNaN(x):
  229. return NaN()
  230. case IsInf(x, 1):
  231. return 0
  232. }
  233. if n == 0 {
  234. return Y0(x)
  235. }
  236. if x == 0 {
  237. if n < 0 && n&1 == 1 {
  238. return Inf(1)
  239. }
  240. return Inf(-1)
  241. }
  242. sign := false
  243. if n < 0 {
  244. n = -n
  245. if n&1 == 1 {
  246. sign = true // sign true if n < 0 && |n| odd
  247. }
  248. }
  249. if n == 1 {
  250. if sign {
  251. return -Y1(x)
  252. }
  253. return Y1(x)
  254. }
  255. var b float64
  256. if x >= Two302 { // x > 2**302
  257. // (x >> n**2)
  258. // Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
  259. // Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
  260. // Let s=sin(x), c=cos(x),
  261. // xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
  262. //
  263. // n sin(xn)*sqt2 cos(xn)*sqt2
  264. // ----------------------------------
  265. // 0 s-c c+s
  266. // 1 -s-c -c+s
  267. // 2 -s+c -c-s
  268. // 3 s+c c-s
  269. var temp float64
  270. switch n & 3 {
  271. case 0:
  272. temp = Sin(x) - Cos(x)
  273. case 1:
  274. temp = -Sin(x) - Cos(x)
  275. case 2:
  276. temp = -Sin(x) + Cos(x)
  277. case 3:
  278. temp = Sin(x) + Cos(x)
  279. }
  280. b = (1 / SqrtPi) * temp / Sqrt(x)
  281. } else {
  282. a := Y0(x)
  283. b = Y1(x)
  284. // quit if b is -inf
  285. for i := 1; i < n && !IsInf(b, -1); i++ {
  286. a, b = b, (float64(i+i)/x)*b-a
  287. }
  288. }
  289. if sign {
  290. return -b
  291. }
  292. return b
  293. }