test_bessel.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. from itertools import product
  2. from sympy.concrete.summations import Sum
  3. from sympy.core.function import (diff, expand_func)
  4. from sympy.core.numbers import (I, Rational, oo, pi)
  5. from sympy.core.singleton import S
  6. from sympy.core.symbol import (Symbol, symbols)
  7. from sympy.functions.elementary.complexes import (conjugate, polar_lift)
  8. from sympy.functions.elementary.exponential import (exp, exp_polar, log)
  9. from sympy.functions.elementary.hyperbolic import (cosh, sinh)
  10. from sympy.functions.elementary.miscellaneous import sqrt
  11. from sympy.functions.elementary.trigonometric import (cos, sin)
  12. from sympy.functions.special.bessel import (besseli, besselj, besselk, bessely, hankel1, hankel2, hn1, hn2, jn, jn_zeros, yn)
  13. from sympy.functions.special.gamma_functions import (gamma, uppergamma)
  14. from sympy.functions.special.hyper import hyper
  15. from sympy.integrals.integrals import Integral
  16. from sympy.series.order import O
  17. from sympy.series.series import series
  18. from sympy.functions.special.bessel import (airyai, airybi,
  19. airyaiprime, airybiprime, marcumq)
  20. from sympy.core.random import (random_complex_number as randcplx,
  21. verify_numerically as tn,
  22. test_derivative_numerically as td,
  23. _randint)
  24. from sympy.simplify import besselsimp
  25. from sympy.testing.pytest import raises, slow
  26. from sympy.abc import z, n, k, x
  27. randint = _randint()
  28. def test_bessel_rand():
  29. for f in [besselj, bessely, besseli, besselk, hankel1, hankel2]:
  30. assert td(f(randcplx(), z), z)
  31. for f in [jn, yn, hn1, hn2]:
  32. assert td(f(randint(-10, 10), z), z)
  33. def test_bessel_twoinputs():
  34. for f in [besselj, bessely, besseli, besselk, hankel1, hankel2, jn, yn]:
  35. raises(TypeError, lambda: f(1))
  36. raises(TypeError, lambda: f(1, 2, 3))
  37. def test_besselj_leading_term():
  38. assert besselj(0, x).as_leading_term(x) == 1
  39. assert besselj(1, sin(x)).as_leading_term(x) == x/2
  40. assert besselj(1, 2*sqrt(x)).as_leading_term(x) == sqrt(x)
  41. # https://github.com/sympy/sympy/issues/21701
  42. assert (besselj(z, x)/x**z).as_leading_term(x) == 1/(2**z*gamma(z + 1))
  43. def test_bessely_leading_term():
  44. assert bessely(0, x).as_leading_term(x) == (2*log(x) - 2*log(2) + 2*S.EulerGamma)/pi
  45. assert bessely(1, sin(x)).as_leading_term(x) == -2/(pi*x)
  46. assert bessely(1, 2*sqrt(x)).as_leading_term(x) == -1/(pi*sqrt(x))
  47. def test_besseli_leading_term():
  48. assert besseli(0, x).as_leading_term(x) == 1
  49. assert besseli(1, sin(x)).as_leading_term(x) == x/2
  50. assert besseli(1, 2*sqrt(x)).as_leading_term(x) == sqrt(x)
  51. def test_besselk_leading_term():
  52. assert besselk(0, x).as_leading_term(x) == -log(x) - S.EulerGamma + log(2)
  53. assert besselk(1, sin(x)).as_leading_term(x) == 1/x
  54. assert besselk(1, 2*sqrt(x)).as_leading_term(x) == 1/(2*sqrt(x))
  55. def test_besselj_series():
  56. assert besselj(0, x).series(x) == 1 - x**2/4 + x**4/64 + O(x**6)
  57. assert besselj(0, x**(1.1)).series(x) == 1 + x**4.4/64 - x**2.2/4 + O(x**6)
  58. assert besselj(0, x**2 + x).series(x) == 1 - x**2/4 - x**3/2\
  59. - 15*x**4/64 + x**5/16 + O(x**6)
  60. assert besselj(0, sqrt(x) + x).series(x, n=4) == 1 - x/4 - 15*x**2/64\
  61. + 215*x**3/2304 - x**Rational(3, 2)/2 + x**Rational(5, 2)/16\
  62. + 23*x**Rational(7, 2)/384 + O(x**4)
  63. assert besselj(0, x/(1 - x)).series(x) == 1 - x**2/4 - x**3/2 - 47*x**4/64\
  64. - 15*x**5/16 + O(x**6)
  65. assert besselj(0, log(1 + x)).series(x) == 1 - x**2/4 + x**3/4\
  66. - 41*x**4/192 + 17*x**5/96 + O(x**6)
  67. assert besselj(1, sin(x)).series(x) == x/2 - 7*x**3/48 + 73*x**5/1920 + O(x**6)
  68. assert besselj(1, 2*sqrt(x)).series(x) == sqrt(x) - x**Rational(3, 2)/2\
  69. + x**Rational(5, 2)/12 - x**Rational(7, 2)/144 + x**Rational(9, 2)/2880\
  70. - x**Rational(11, 2)/86400 + O(x**6)
  71. assert besselj(-2, sin(x)).series(x, n=4) == besselj(2, sin(x)).series(x, n=4)
  72. def test_bessely_series():
  73. const = 2*S.EulerGamma/pi - 2*log(2)/pi + 2*log(x)/pi
  74. assert bessely(0, x).series(x, n=4) == const + x**2*(-log(x)/(2*pi)\
  75. + (2 - 2*S.EulerGamma)/(4*pi) + log(2)/(2*pi)) + O(x**4*log(x))
  76. assert bessely(1, x).series(x, n=4) == -2/(pi*x) + x*(log(x)/pi - log(2)/pi - \
  77. (1 - 2*S.EulerGamma)/(2*pi)) + x**3*(-log(x)/(8*pi) + \
  78. (S(5)/2 - 2*S.EulerGamma)/(16*pi) + log(2)/(8*pi)) + O(x**4*log(x))
  79. assert bessely(2, x).series(x, n=4) == -4/(pi*x**2) - 1/pi + x**2*(log(x)/(4*pi) - \
  80. log(2)/(4*pi) - (S(3)/2 - 2*S.EulerGamma)/(8*pi)) + O(x**4*log(x))
  81. assert bessely(3, x).series(x, n=4) == -16/(pi*x**3) - 2/(pi*x) - \
  82. x/(4*pi) + x**3*(log(x)/(24*pi) - log(2)/(24*pi) - \
  83. (S(11)/6 - 2*S.EulerGamma)/(48*pi)) + O(x**4*log(x))
  84. assert bessely(0, x**(1.1)).series(x, n=4) == 2*S.EulerGamma/pi\
  85. - 2*log(2)/pi + 2.2*log(x)/pi + x**2.2*(-0.55*log(x)/pi\
  86. + (2 - 2*S.EulerGamma)/(4*pi) + log(2)/(2*pi)) + O(x**4*log(x))
  87. assert bessely(0, x**2 + x).series(x, n=4) == \
  88. const - (2 - 2*S.EulerGamma)*(-x**3/(2*pi) - x**2/(4*pi)) + 2*x/pi\
  89. + x**2*(-log(x)/(2*pi) - 1/pi + log(2)/(2*pi))\
  90. + x**3*(-log(x)/pi + 1/(6*pi) + log(2)/pi) + O(x**4*log(x))
  91. assert bessely(0, x/(1 - x)).series(x, n=3) == const\
  92. + 2*x/pi + x**2*(-log(x)/(2*pi) + (2 - 2*S.EulerGamma)/(4*pi)\
  93. + log(2)/(2*pi) + 1/pi) + O(x**3*log(x))
  94. assert bessely(0, log(1 + x)).series(x, n=3) == const\
  95. - x/pi + x**2*(-log(x)/(2*pi) + (2 - 2*S.EulerGamma)/(4*pi)\
  96. + log(2)/(2*pi) + 5/(12*pi)) + O(x**3*log(x))
  97. assert bessely(1, sin(x)).series(x, n=4) == -1/(pi*(-x**3/12 + x/2)) - \
  98. (1 - 2*S.EulerGamma)*(-x**3/12 + x/2)/pi + x*(log(x)/pi - log(2)/pi) + \
  99. x**3*(-7*log(x)/(24*pi) - 1/(6*pi) + (S(5)/2 - 2*S.EulerGamma)/(16*pi) +
  100. 7*log(2)/(24*pi)) + O(x**4*log(x))
  101. assert bessely(1, 2*sqrt(x)).series(x, n=3) == -1/(pi*sqrt(x)) + \
  102. sqrt(x)*(log(x)/pi - (1 - 2*S.EulerGamma)/pi) + x**(S(3)/2)*(-log(x)/(2*pi) + \
  103. (S(5)/2 - 2*S.EulerGamma)/(2*pi)) + x**(S(5)/2)*(log(x)/(12*pi) - \
  104. (S(10)/3 - 2*S.EulerGamma)/(12*pi)) + O(x**3*log(x))
  105. assert bessely(-2, sin(x)).series(x, n=4) == bessely(2, sin(x)).series(x, n=4)
  106. def test_besseli_series():
  107. assert besseli(0, x).series(x) == 1 + x**2/4 + x**4/64 + O(x**6)
  108. assert besseli(0, x**(1.1)).series(x) == 1 + x**4.4/64 + x**2.2/4 + O(x**6)
  109. assert besseli(0, x**2 + x).series(x) == 1 + x**2/4 + x**3/2 + 17*x**4/64 + \
  110. x**5/16 + O(x**6)
  111. assert besseli(0, sqrt(x) + x).series(x, n=4) == 1 + x/4 + 17*x**2/64 + \
  112. 217*x**3/2304 + x**(S(3)/2)/2 + x**(S(5)/2)/16 + 25*x**(S(7)/2)/384 + O(x**4)
  113. assert besseli(0, x/(1 - x)).series(x) == 1 + x**2/4 + x**3/2 + 49*x**4/64 + \
  114. 17*x**5/16 + O(x**6)
  115. assert besseli(0, log(1 + x)).series(x) == 1 + x**2/4 - x**3/4 + 47*x**4/192 - \
  116. 23*x**5/96 + O(x**6)
  117. assert besseli(1, sin(x)).series(x) == x/2 - x**3/48 - 47*x**5/1920 + O(x**6)
  118. assert besseli(1, 2*sqrt(x)).series(x) == sqrt(x) + x**(S(3)/2)/2 + x**(S(5)/2)/12 + \
  119. x**(S(7)/2)/144 + x**(S(9)/2)/2880 + x**(S(11)/2)/86400 + O(x**6)
  120. assert besseli(-2, sin(x)).series(x, n=4) == besseli(2, sin(x)).series(x, n=4)
  121. def test_besselk_series():
  122. const = log(2) - S.EulerGamma - log(x)
  123. assert besselk(0, x).series(x, n=4) == const + \
  124. x**2*(-log(x)/4 - S.EulerGamma/4 + log(2)/4 + S(1)/4) + O(x**4*log(x))
  125. assert besselk(1, x).series(x, n=4) == 1/x + x*(log(x)/2 - log(2)/2 - \
  126. S(1)/4 + S.EulerGamma/2) + x**3*(log(x)/16 - S(5)/64 - log(2)/16 + \
  127. S.EulerGamma/16) + O(x**4*log(x))
  128. assert besselk(2, x).series(x, n=4) == 2/x**2 - S(1)/2 + x**2*(-log(x)/8 - \
  129. S.EulerGamma/8 + log(2)/8 + S(3)/32) + O(x**4*log(x))
  130. assert besselk(0, x**(1.1)).series(x, n=4) == log(2) - S.EulerGamma - \
  131. 1.1*log(x) + x**2.2*(-0.275*log(x) - S.EulerGamma/4 + \
  132. log(2)/4 + S(1)/4) + O(x**4*log(x))
  133. assert besselk(0, x**2 + x).series(x, n=4) == const + \
  134. (2 - 2*S.EulerGamma)*(x**3/4 + x**2/8) - x + x**2*(-log(x)/4 + \
  135. log(2)/4 + S(1)/2) + x**3*(-log(x)/2 - S(7)/12 + log(2)/2) + O(x**4*log(x))
  136. assert besselk(0, x/(1 - x)).series(x, n=3) == const - x + x**2*(-log(x)/4 - \
  137. S(1)/4 - S.EulerGamma/4 + log(2)/4) + O(x**3*log(x))
  138. assert besselk(0, log(1 + x)).series(x, n=3) == const + x/2 + \
  139. x**2*(-log(x)/4 - S.EulerGamma/4 + S(1)/24 + log(2)/4) + O(x**3*log(x))
  140. assert besselk(1, 2*sqrt(x)).series(x, n=3) == 1/(2*sqrt(x)) + \
  141. sqrt(x)*(log(x)/2 - S(1)/2 + S.EulerGamma) + x**(S(3)/2)*(log(x)/4 - S(5)/8 + \
  142. S.EulerGamma/2) + x**(S(5)/2)*(log(x)/24 - S(5)/36 + S.EulerGamma/12) + O(x**3*log(x))
  143. assert besselk(-2, sin(x)).series(x, n=4) == besselk(2, sin(x)).series(x, n=4)
  144. def test_diff():
  145. assert besselj(n, z).diff(z) == besselj(n - 1, z)/2 - besselj(n + 1, z)/2
  146. assert bessely(n, z).diff(z) == bessely(n - 1, z)/2 - bessely(n + 1, z)/2
  147. assert besseli(n, z).diff(z) == besseli(n - 1, z)/2 + besseli(n + 1, z)/2
  148. assert besselk(n, z).diff(z) == -besselk(n - 1, z)/2 - besselk(n + 1, z)/2
  149. assert hankel1(n, z).diff(z) == hankel1(n - 1, z)/2 - hankel1(n + 1, z)/2
  150. assert hankel2(n, z).diff(z) == hankel2(n - 1, z)/2 - hankel2(n + 1, z)/2
  151. def test_rewrite():
  152. assert besselj(n, z).rewrite(jn) == sqrt(2*z/pi)*jn(n - S.Half, z)
  153. assert bessely(n, z).rewrite(yn) == sqrt(2*z/pi)*yn(n - S.Half, z)
  154. assert besseli(n, z).rewrite(besselj) == \
  155. exp(-I*n*pi/2)*besselj(n, polar_lift(I)*z)
  156. assert besselj(n, z).rewrite(besseli) == \
  157. exp(I*n*pi/2)*besseli(n, polar_lift(-I)*z)
  158. nu = randcplx()
  159. assert tn(besselj(nu, z), besselj(nu, z).rewrite(besseli), z)
  160. assert tn(besselj(nu, z), besselj(nu, z).rewrite(bessely), z)
  161. assert tn(besseli(nu, z), besseli(nu, z).rewrite(besselj), z)
  162. assert tn(besseli(nu, z), besseli(nu, z).rewrite(bessely), z)
  163. assert tn(bessely(nu, z), bessely(nu, z).rewrite(besselj), z)
  164. assert tn(bessely(nu, z), bessely(nu, z).rewrite(besseli), z)
  165. assert tn(besselk(nu, z), besselk(nu, z).rewrite(besselj), z)
  166. assert tn(besselk(nu, z), besselk(nu, z).rewrite(besseli), z)
  167. assert tn(besselk(nu, z), besselk(nu, z).rewrite(bessely), z)
  168. # check that a rewrite was triggered, when the order is set to a generic
  169. # symbol 'nu'
  170. assert yn(nu, z) != yn(nu, z).rewrite(jn)
  171. assert hn1(nu, z) != hn1(nu, z).rewrite(jn)
  172. assert hn2(nu, z) != hn2(nu, z).rewrite(jn)
  173. assert jn(nu, z) != jn(nu, z).rewrite(yn)
  174. assert hn1(nu, z) != hn1(nu, z).rewrite(yn)
  175. assert hn2(nu, z) != hn2(nu, z).rewrite(yn)
  176. # rewriting spherical bessel functions (SBFs) w.r.t. besselj, bessely is
  177. # not allowed if a generic symbol 'nu' is used as the order of the SBFs
  178. # to avoid inconsistencies (the order of bessel[jy] is allowed to be
  179. # complex-valued, whereas SBFs are defined only for integer orders)
  180. order = nu
  181. for f in (besselj, bessely):
  182. assert hn1(order, z) == hn1(order, z).rewrite(f)
  183. assert hn2(order, z) == hn2(order, z).rewrite(f)
  184. assert jn(order, z).rewrite(besselj) == sqrt(2)*sqrt(pi)*sqrt(1/z)*besselj(order + S.Half, z)/2
  185. assert jn(order, z).rewrite(bessely) == (-1)**nu*sqrt(2)*sqrt(pi)*sqrt(1/z)*bessely(-order - S.Half, z)/2
  186. # for integral orders rewriting SBFs w.r.t bessel[jy] is allowed
  187. N = Symbol('n', integer=True)
  188. ri = randint(-11, 10)
  189. for order in (ri, N):
  190. for f in (besselj, bessely):
  191. assert yn(order, z) != yn(order, z).rewrite(f)
  192. assert jn(order, z) != jn(order, z).rewrite(f)
  193. assert hn1(order, z) != hn1(order, z).rewrite(f)
  194. assert hn2(order, z) != hn2(order, z).rewrite(f)
  195. for func, refunc in product((yn, jn, hn1, hn2),
  196. (jn, yn, besselj, bessely)):
  197. assert tn(func(ri, z), func(ri, z).rewrite(refunc), z)
  198. def test_expand():
  199. assert expand_func(besselj(S.Half, z).rewrite(jn)) == \
  200. sqrt(2)*sin(z)/(sqrt(pi)*sqrt(z))
  201. assert expand_func(bessely(S.Half, z).rewrite(yn)) == \
  202. -sqrt(2)*cos(z)/(sqrt(pi)*sqrt(z))
  203. # XXX: teach sin/cos to work around arguments like
  204. # x*exp_polar(I*pi*n/2). Then change besselsimp -> expand_func
  205. assert besselsimp(besselj(S.Half, z)) == sqrt(2)*sin(z)/(sqrt(pi)*sqrt(z))
  206. assert besselsimp(besselj(Rational(-1, 2), z)) == sqrt(2)*cos(z)/(sqrt(pi)*sqrt(z))
  207. assert besselsimp(besselj(Rational(5, 2), z)) == \
  208. -sqrt(2)*(z**2*sin(z) + 3*z*cos(z) - 3*sin(z))/(sqrt(pi)*z**Rational(5, 2))
  209. assert besselsimp(besselj(Rational(-5, 2), z)) == \
  210. -sqrt(2)*(z**2*cos(z) - 3*z*sin(z) - 3*cos(z))/(sqrt(pi)*z**Rational(5, 2))
  211. assert besselsimp(bessely(S.Half, z)) == \
  212. -(sqrt(2)*cos(z))/(sqrt(pi)*sqrt(z))
  213. assert besselsimp(bessely(Rational(-1, 2), z)) == sqrt(2)*sin(z)/(sqrt(pi)*sqrt(z))
  214. assert besselsimp(bessely(Rational(5, 2), z)) == \
  215. sqrt(2)*(z**2*cos(z) - 3*z*sin(z) - 3*cos(z))/(sqrt(pi)*z**Rational(5, 2))
  216. assert besselsimp(bessely(Rational(-5, 2), z)) == \
  217. -sqrt(2)*(z**2*sin(z) + 3*z*cos(z) - 3*sin(z))/(sqrt(pi)*z**Rational(5, 2))
  218. assert besselsimp(besseli(S.Half, z)) == sqrt(2)*sinh(z)/(sqrt(pi)*sqrt(z))
  219. assert besselsimp(besseli(Rational(-1, 2), z)) == \
  220. sqrt(2)*cosh(z)/(sqrt(pi)*sqrt(z))
  221. assert besselsimp(besseli(Rational(5, 2), z)) == \
  222. sqrt(2)*(z**2*sinh(z) - 3*z*cosh(z) + 3*sinh(z))/(sqrt(pi)*z**Rational(5, 2))
  223. assert besselsimp(besseli(Rational(-5, 2), z)) == \
  224. sqrt(2)*(z**2*cosh(z) - 3*z*sinh(z) + 3*cosh(z))/(sqrt(pi)*z**Rational(5, 2))
  225. assert besselsimp(besselk(S.Half, z)) == \
  226. besselsimp(besselk(Rational(-1, 2), z)) == sqrt(pi)*exp(-z)/(sqrt(2)*sqrt(z))
  227. assert besselsimp(besselk(Rational(5, 2), z)) == \
  228. besselsimp(besselk(Rational(-5, 2), z)) == \
  229. sqrt(2)*sqrt(pi)*(z**2 + 3*z + 3)*exp(-z)/(2*z**Rational(5, 2))
  230. n = Symbol('n', integer=True, positive=True)
  231. assert expand_func(besseli(n + 2, z)) == \
  232. besseli(n, z) + (-2*n - 2)*(-2*n*besseli(n, z)/z + besseli(n - 1, z))/z
  233. assert expand_func(besselj(n + 2, z)) == \
  234. -besselj(n, z) + (2*n + 2)*(2*n*besselj(n, z)/z - besselj(n - 1, z))/z
  235. assert expand_func(besselk(n + 2, z)) == \
  236. besselk(n, z) + (2*n + 2)*(2*n*besselk(n, z)/z + besselk(n - 1, z))/z
  237. assert expand_func(bessely(n + 2, z)) == \
  238. -bessely(n, z) + (2*n + 2)*(2*n*bessely(n, z)/z - bessely(n - 1, z))/z
  239. assert expand_func(besseli(n + S.Half, z).rewrite(jn)) == \
  240. (sqrt(2)*sqrt(z)*exp(-I*pi*(n + S.Half)/2) *
  241. exp_polar(I*pi/4)*jn(n, z*exp_polar(I*pi/2))/sqrt(pi))
  242. assert expand_func(besselj(n + S.Half, z).rewrite(jn)) == \
  243. sqrt(2)*sqrt(z)*jn(n, z)/sqrt(pi)
  244. r = Symbol('r', real=True)
  245. p = Symbol('p', positive=True)
  246. i = Symbol('i', integer=True)
  247. for besselx in [besselj, bessely, besseli, besselk]:
  248. assert besselx(i, p).is_extended_real is True
  249. assert besselx(i, x).is_extended_real is None
  250. assert besselx(x, z).is_extended_real is None
  251. for besselx in [besselj, besseli]:
  252. assert besselx(i, r).is_extended_real is True
  253. for besselx in [bessely, besselk]:
  254. assert besselx(i, r).is_extended_real is None
  255. for besselx in [besselj, bessely, besseli, besselk]:
  256. assert expand_func(besselx(oo, x)) == besselx(oo, x, evaluate=False)
  257. assert expand_func(besselx(-oo, x)) == besselx(-oo, x, evaluate=False)
  258. # Quite varying time, but often really slow
  259. @slow
  260. def test_slow_expand():
  261. def check(eq, ans):
  262. return tn(eq, ans) and eq == ans
  263. rn = randcplx(a=1, b=0, d=0, c=2)
  264. for besselx in [besselj, bessely, besseli, besselk]:
  265. ri = S(2*randint(-11, 10) + 1) / 2 # half integer in [-21/2, 21/2]
  266. assert tn(besselsimp(besselx(ri, z)), besselx(ri, z))
  267. assert check(expand_func(besseli(rn, x)),
  268. besseli(rn - 2, x) - 2*(rn - 1)*besseli(rn - 1, x)/x)
  269. assert check(expand_func(besseli(-rn, x)),
  270. besseli(-rn + 2, x) + 2*(-rn + 1)*besseli(-rn + 1, x)/x)
  271. assert check(expand_func(besselj(rn, x)),
  272. -besselj(rn - 2, x) + 2*(rn - 1)*besselj(rn - 1, x)/x)
  273. assert check(expand_func(besselj(-rn, x)),
  274. -besselj(-rn + 2, x) + 2*(-rn + 1)*besselj(-rn + 1, x)/x)
  275. assert check(expand_func(besselk(rn, x)),
  276. besselk(rn - 2, x) + 2*(rn - 1)*besselk(rn - 1, x)/x)
  277. assert check(expand_func(besselk(-rn, x)),
  278. besselk(-rn + 2, x) - 2*(-rn + 1)*besselk(-rn + 1, x)/x)
  279. assert check(expand_func(bessely(rn, x)),
  280. -bessely(rn - 2, x) + 2*(rn - 1)*bessely(rn - 1, x)/x)
  281. assert check(expand_func(bessely(-rn, x)),
  282. -bessely(-rn + 2, x) + 2*(-rn + 1)*bessely(-rn + 1, x)/x)
  283. def mjn(n, z):
  284. return expand_func(jn(n, z))
  285. def myn(n, z):
  286. return expand_func(yn(n, z))
  287. def test_jn():
  288. z = symbols("z")
  289. assert jn(0, 0) == 1
  290. assert jn(1, 0) == 0
  291. assert jn(-1, 0) == S.ComplexInfinity
  292. assert jn(z, 0) == jn(z, 0, evaluate=False)
  293. assert jn(0, oo) == 0
  294. assert jn(0, -oo) == 0
  295. assert mjn(0, z) == sin(z)/z
  296. assert mjn(1, z) == sin(z)/z**2 - cos(z)/z
  297. assert mjn(2, z) == (3/z**3 - 1/z)*sin(z) - (3/z**2) * cos(z)
  298. assert mjn(3, z) == (15/z**4 - 6/z**2)*sin(z) + (1/z - 15/z**3)*cos(z)
  299. assert mjn(4, z) == (1/z + 105/z**5 - 45/z**3)*sin(z) + \
  300. (-105/z**4 + 10/z**2)*cos(z)
  301. assert mjn(5, z) == (945/z**6 - 420/z**4 + 15/z**2)*sin(z) + \
  302. (-1/z - 945/z**5 + 105/z**3)*cos(z)
  303. assert mjn(6, z) == (-1/z + 10395/z**7 - 4725/z**5 + 210/z**3)*sin(z) + \
  304. (-10395/z**6 + 1260/z**4 - 21/z**2)*cos(z)
  305. assert expand_func(jn(n, z)) == jn(n, z)
  306. # SBFs not defined for complex-valued orders
  307. assert jn(2+3j, 5.2+0.3j).evalf() == jn(2+3j, 5.2+0.3j)
  308. assert eq([jn(2, 5.2+0.3j).evalf(10)],
  309. [0.09941975672 - 0.05452508024*I])
  310. def test_yn():
  311. z = symbols("z")
  312. assert myn(0, z) == -cos(z)/z
  313. assert myn(1, z) == -cos(z)/z**2 - sin(z)/z
  314. assert myn(2, z) == -((3/z**3 - 1/z)*cos(z) + (3/z**2)*sin(z))
  315. assert expand_func(yn(n, z)) == yn(n, z)
  316. # SBFs not defined for complex-valued orders
  317. assert yn(2+3j, 5.2+0.3j).evalf() == yn(2+3j, 5.2+0.3j)
  318. assert eq([yn(2, 5.2+0.3j).evalf(10)],
  319. [0.185250342 + 0.01489557397*I])
  320. def test_sympify_yn():
  321. assert S(15) in myn(3, pi).atoms()
  322. assert myn(3, pi) == 15/pi**4 - 6/pi**2
  323. def eq(a, b, tol=1e-6):
  324. for u, v in zip(a, b):
  325. if not (abs(u - v) < tol):
  326. return False
  327. return True
  328. def test_jn_zeros():
  329. assert eq(jn_zeros(0, 4), [3.141592, 6.283185, 9.424777, 12.566370])
  330. assert eq(jn_zeros(1, 4), [4.493409, 7.725251, 10.904121, 14.066193])
  331. assert eq(jn_zeros(2, 4), [5.763459, 9.095011, 12.322940, 15.514603])
  332. assert eq(jn_zeros(3, 4), [6.987932, 10.417118, 13.698023, 16.923621])
  333. assert eq(jn_zeros(4, 4), [8.182561, 11.704907, 15.039664, 18.301255])
  334. def test_bessel_eval():
  335. n, m, k = Symbol('n', integer=True), Symbol('m'), Symbol('k', integer=True, zero=False)
  336. for f in [besselj, besseli]:
  337. assert f(0, 0) is S.One
  338. assert f(2.1, 0) is S.Zero
  339. assert f(-3, 0) is S.Zero
  340. assert f(-10.2, 0) is S.ComplexInfinity
  341. assert f(1 + 3*I, 0) is S.Zero
  342. assert f(-3 + I, 0) is S.ComplexInfinity
  343. assert f(-2*I, 0) is S.NaN
  344. assert f(n, 0) != S.One and f(n, 0) != S.Zero
  345. assert f(m, 0) != S.One and f(m, 0) != S.Zero
  346. assert f(k, 0) is S.Zero
  347. assert bessely(0, 0) is S.NegativeInfinity
  348. assert besselk(0, 0) is S.Infinity
  349. for f in [bessely, besselk]:
  350. assert f(1 + I, 0) is S.ComplexInfinity
  351. assert f(I, 0) is S.NaN
  352. for f in [besselj, bessely]:
  353. assert f(m, S.Infinity) is S.Zero
  354. assert f(m, S.NegativeInfinity) is S.Zero
  355. for f in [besseli, besselk]:
  356. assert f(m, I*S.Infinity) is S.Zero
  357. assert f(m, I*S.NegativeInfinity) is S.Zero
  358. for f in [besseli, besselk]:
  359. assert f(-4, z) == f(4, z)
  360. assert f(-3, z) == f(3, z)
  361. assert f(-n, z) == f(n, z)
  362. assert f(-m, z) != f(m, z)
  363. for f in [besselj, bessely]:
  364. assert f(-4, z) == f(4, z)
  365. assert f(-3, z) == -f(3, z)
  366. assert f(-n, z) == (-1)**n*f(n, z)
  367. assert f(-m, z) != (-1)**m*f(m, z)
  368. for f in [besselj, besseli]:
  369. assert f(m, -z) == (-z)**m*z**(-m)*f(m, z)
  370. assert besseli(2, -z) == besseli(2, z)
  371. assert besseli(3, -z) == -besseli(3, z)
  372. assert besselj(0, -z) == besselj(0, z)
  373. assert besselj(1, -z) == -besselj(1, z)
  374. assert besseli(0, I*z) == besselj(0, z)
  375. assert besseli(1, I*z) == I*besselj(1, z)
  376. assert besselj(3, I*z) == -I*besseli(3, z)
  377. def test_bessel_nan():
  378. # FIXME: could have these return NaN; for now just fix infinite recursion
  379. for f in [besselj, bessely, besseli, besselk, hankel1, hankel2, yn, jn]:
  380. assert f(1, S.NaN) == f(1, S.NaN, evaluate=False)
  381. def test_meromorphic():
  382. assert besselj(2, x).is_meromorphic(x, 1) == True
  383. assert besselj(2, x).is_meromorphic(x, 0) == True
  384. assert besselj(2, x).is_meromorphic(x, oo) == False
  385. assert besselj(S(2)/3, x).is_meromorphic(x, 1) == True
  386. assert besselj(S(2)/3, x).is_meromorphic(x, 0) == False
  387. assert besselj(S(2)/3, x).is_meromorphic(x, oo) == False
  388. assert besselj(x, 2*x).is_meromorphic(x, 2) == False
  389. assert besselk(0, x).is_meromorphic(x, 1) == True
  390. assert besselk(2, x).is_meromorphic(x, 0) == True
  391. assert besseli(0, x).is_meromorphic(x, 1) == True
  392. assert besseli(2, x).is_meromorphic(x, 0) == True
  393. assert bessely(0, x).is_meromorphic(x, 1) == True
  394. assert bessely(0, x).is_meromorphic(x, 0) == False
  395. assert bessely(2, x).is_meromorphic(x, 0) == True
  396. assert hankel1(3, x**2 + 2*x).is_meromorphic(x, 1) == True
  397. assert hankel1(0, x).is_meromorphic(x, 0) == False
  398. assert hankel2(11, 4).is_meromorphic(x, 5) == True
  399. assert hn1(6, 7*x**3 + 4).is_meromorphic(x, 7) == True
  400. assert hn2(3, 2*x).is_meromorphic(x, 9) == True
  401. assert jn(5, 2*x + 7).is_meromorphic(x, 4) == True
  402. assert yn(8, x**2 + 11).is_meromorphic(x, 6) == True
  403. def test_conjugate():
  404. n = Symbol('n')
  405. z = Symbol('z', extended_real=False)
  406. x = Symbol('x', extended_real=True)
  407. y = Symbol('y', positive=True)
  408. t = Symbol('t', negative=True)
  409. for f in [besseli, besselj, besselk, bessely, hankel1, hankel2]:
  410. assert f(n, -1).conjugate() != f(conjugate(n), -1)
  411. assert f(n, x).conjugate() != f(conjugate(n), x)
  412. assert f(n, t).conjugate() != f(conjugate(n), t)
  413. rz = randcplx(b=0.5)
  414. for f in [besseli, besselj, besselk, bessely]:
  415. assert f(n, 1 + I).conjugate() == f(conjugate(n), 1 - I)
  416. assert f(n, 0).conjugate() == f(conjugate(n), 0)
  417. assert f(n, 1).conjugate() == f(conjugate(n), 1)
  418. assert f(n, z).conjugate() == f(conjugate(n), conjugate(z))
  419. assert f(n, y).conjugate() == f(conjugate(n), y)
  420. assert tn(f(n, rz).conjugate(), f(conjugate(n), conjugate(rz)))
  421. assert hankel1(n, 1 + I).conjugate() == hankel2(conjugate(n), 1 - I)
  422. assert hankel1(n, 0).conjugate() == hankel2(conjugate(n), 0)
  423. assert hankel1(n, 1).conjugate() == hankel2(conjugate(n), 1)
  424. assert hankel1(n, y).conjugate() == hankel2(conjugate(n), y)
  425. assert hankel1(n, z).conjugate() == hankel2(conjugate(n), conjugate(z))
  426. assert tn(hankel1(n, rz).conjugate(), hankel2(conjugate(n), conjugate(rz)))
  427. assert hankel2(n, 1 + I).conjugate() == hankel1(conjugate(n), 1 - I)
  428. assert hankel2(n, 0).conjugate() == hankel1(conjugate(n), 0)
  429. assert hankel2(n, 1).conjugate() == hankel1(conjugate(n), 1)
  430. assert hankel2(n, y).conjugate() == hankel1(conjugate(n), y)
  431. assert hankel2(n, z).conjugate() == hankel1(conjugate(n), conjugate(z))
  432. assert tn(hankel2(n, rz).conjugate(), hankel1(conjugate(n), conjugate(rz)))
  433. def test_branching():
  434. assert besselj(polar_lift(k), x) == besselj(k, x)
  435. assert besseli(polar_lift(k), x) == besseli(k, x)
  436. n = Symbol('n', integer=True)
  437. assert besselj(n, exp_polar(2*pi*I)*x) == besselj(n, x)
  438. assert besselj(n, polar_lift(x)) == besselj(n, x)
  439. assert besseli(n, exp_polar(2*pi*I)*x) == besseli(n, x)
  440. assert besseli(n, polar_lift(x)) == besseli(n, x)
  441. def tn(func, s):
  442. from sympy.core.random import uniform
  443. c = uniform(1, 5)
  444. expr = func(s, c*exp_polar(I*pi)) - func(s, c*exp_polar(-I*pi))
  445. eps = 1e-15
  446. expr2 = func(s + eps, -c + eps*I) - func(s + eps, -c - eps*I)
  447. return abs(expr.n() - expr2.n()).n() < 1e-10
  448. nu = Symbol('nu')
  449. assert besselj(nu, exp_polar(2*pi*I)*x) == exp(2*pi*I*nu)*besselj(nu, x)
  450. assert besseli(nu, exp_polar(2*pi*I)*x) == exp(2*pi*I*nu)*besseli(nu, x)
  451. assert tn(besselj, 2)
  452. assert tn(besselj, pi)
  453. assert tn(besselj, I)
  454. assert tn(besseli, 2)
  455. assert tn(besseli, pi)
  456. assert tn(besseli, I)
  457. def test_airy_base():
  458. z = Symbol('z')
  459. x = Symbol('x', real=True)
  460. y = Symbol('y', real=True)
  461. assert conjugate(airyai(z)) == airyai(conjugate(z))
  462. assert airyai(x).is_extended_real
  463. assert airyai(x+I*y).as_real_imag() == (
  464. airyai(x - I*y)/2 + airyai(x + I*y)/2,
  465. I*(airyai(x - I*y) - airyai(x + I*y))/2)
  466. def test_airyai():
  467. z = Symbol('z', real=False)
  468. t = Symbol('t', negative=True)
  469. p = Symbol('p', positive=True)
  470. assert isinstance(airyai(z), airyai)
  471. assert airyai(0) == 3**Rational(1, 3)/(3*gamma(Rational(2, 3)))
  472. assert airyai(oo) == 0
  473. assert airyai(-oo) == 0
  474. assert diff(airyai(z), z) == airyaiprime(z)
  475. assert series(airyai(z), z, 0, 3) == (
  476. 3**Rational(5, 6)*gamma(Rational(1, 3))/(6*pi) - 3**Rational(1, 6)*z*gamma(Rational(2, 3))/(2*pi) + O(z**3))
  477. assert airyai(z).rewrite(hyper) == (
  478. -3**Rational(2, 3)*z*hyper((), (Rational(4, 3),), z**3/9)/(3*gamma(Rational(1, 3))) +
  479. 3**Rational(1, 3)*hyper((), (Rational(2, 3),), z**3/9)/(3*gamma(Rational(2, 3))))
  480. assert isinstance(airyai(z).rewrite(besselj), airyai)
  481. assert airyai(t).rewrite(besselj) == (
  482. sqrt(-t)*(besselj(Rational(-1, 3), 2*(-t)**Rational(3, 2)/3) +
  483. besselj(Rational(1, 3), 2*(-t)**Rational(3, 2)/3))/3)
  484. assert airyai(z).rewrite(besseli) == (
  485. -z*besseli(Rational(1, 3), 2*z**Rational(3, 2)/3)/(3*(z**Rational(3, 2))**Rational(1, 3)) +
  486. (z**Rational(3, 2))**Rational(1, 3)*besseli(Rational(-1, 3), 2*z**Rational(3, 2)/3)/3)
  487. assert airyai(p).rewrite(besseli) == (
  488. sqrt(p)*(besseli(Rational(-1, 3), 2*p**Rational(3, 2)/3) -
  489. besseli(Rational(1, 3), 2*p**Rational(3, 2)/3))/3)
  490. assert expand_func(airyai(2*(3*z**5)**Rational(1, 3))) == (
  491. -sqrt(3)*(-1 + (z**5)**Rational(1, 3)/z**Rational(5, 3))*airybi(2*3**Rational(1, 3)*z**Rational(5, 3))/6 +
  492. (1 + (z**5)**Rational(1, 3)/z**Rational(5, 3))*airyai(2*3**Rational(1, 3)*z**Rational(5, 3))/2)
  493. def test_airybi():
  494. z = Symbol('z', real=False)
  495. t = Symbol('t', negative=True)
  496. p = Symbol('p', positive=True)
  497. assert isinstance(airybi(z), airybi)
  498. assert airybi(0) == 3**Rational(5, 6)/(3*gamma(Rational(2, 3)))
  499. assert airybi(oo) is oo
  500. assert airybi(-oo) == 0
  501. assert diff(airybi(z), z) == airybiprime(z)
  502. assert series(airybi(z), z, 0, 3) == (
  503. 3**Rational(1, 3)*gamma(Rational(1, 3))/(2*pi) + 3**Rational(2, 3)*z*gamma(Rational(2, 3))/(2*pi) + O(z**3))
  504. assert airybi(z).rewrite(hyper) == (
  505. 3**Rational(1, 6)*z*hyper((), (Rational(4, 3),), z**3/9)/gamma(Rational(1, 3)) +
  506. 3**Rational(5, 6)*hyper((), (Rational(2, 3),), z**3/9)/(3*gamma(Rational(2, 3))))
  507. assert isinstance(airybi(z).rewrite(besselj), airybi)
  508. assert airyai(t).rewrite(besselj) == (
  509. sqrt(-t)*(besselj(Rational(-1, 3), 2*(-t)**Rational(3, 2)/3) +
  510. besselj(Rational(1, 3), 2*(-t)**Rational(3, 2)/3))/3)
  511. assert airybi(z).rewrite(besseli) == (
  512. sqrt(3)*(z*besseli(Rational(1, 3), 2*z**Rational(3, 2)/3)/(z**Rational(3, 2))**Rational(1, 3) +
  513. (z**Rational(3, 2))**Rational(1, 3)*besseli(Rational(-1, 3), 2*z**Rational(3, 2)/3))/3)
  514. assert airybi(p).rewrite(besseli) == (
  515. sqrt(3)*sqrt(p)*(besseli(Rational(-1, 3), 2*p**Rational(3, 2)/3) +
  516. besseli(Rational(1, 3), 2*p**Rational(3, 2)/3))/3)
  517. assert expand_func(airybi(2*(3*z**5)**Rational(1, 3))) == (
  518. sqrt(3)*(1 - (z**5)**Rational(1, 3)/z**Rational(5, 3))*airyai(2*3**Rational(1, 3)*z**Rational(5, 3))/2 +
  519. (1 + (z**5)**Rational(1, 3)/z**Rational(5, 3))*airybi(2*3**Rational(1, 3)*z**Rational(5, 3))/2)
  520. def test_airyaiprime():
  521. z = Symbol('z', real=False)
  522. t = Symbol('t', negative=True)
  523. p = Symbol('p', positive=True)
  524. assert isinstance(airyaiprime(z), airyaiprime)
  525. assert airyaiprime(0) == -3**Rational(2, 3)/(3*gamma(Rational(1, 3)))
  526. assert airyaiprime(oo) == 0
  527. assert diff(airyaiprime(z), z) == z*airyai(z)
  528. assert series(airyaiprime(z), z, 0, 3) == (
  529. -3**Rational(2, 3)/(3*gamma(Rational(1, 3))) + 3**Rational(1, 3)*z**2/(6*gamma(Rational(2, 3))) + O(z**3))
  530. assert airyaiprime(z).rewrite(hyper) == (
  531. 3**Rational(1, 3)*z**2*hyper((), (Rational(5, 3),), z**3/9)/(6*gamma(Rational(2, 3))) -
  532. 3**Rational(2, 3)*hyper((), (Rational(1, 3),), z**3/9)/(3*gamma(Rational(1, 3))))
  533. assert isinstance(airyaiprime(z).rewrite(besselj), airyaiprime)
  534. assert airyai(t).rewrite(besselj) == (
  535. sqrt(-t)*(besselj(Rational(-1, 3), 2*(-t)**Rational(3, 2)/3) +
  536. besselj(Rational(1, 3), 2*(-t)**Rational(3, 2)/3))/3)
  537. assert airyaiprime(z).rewrite(besseli) == (
  538. z**2*besseli(Rational(2, 3), 2*z**Rational(3, 2)/3)/(3*(z**Rational(3, 2))**Rational(2, 3)) -
  539. (z**Rational(3, 2))**Rational(2, 3)*besseli(Rational(-1, 3), 2*z**Rational(3, 2)/3)/3)
  540. assert airyaiprime(p).rewrite(besseli) == (
  541. p*(-besseli(Rational(-2, 3), 2*p**Rational(3, 2)/3) + besseli(Rational(2, 3), 2*p**Rational(3, 2)/3))/3)
  542. assert expand_func(airyaiprime(2*(3*z**5)**Rational(1, 3))) == (
  543. sqrt(3)*(z**Rational(5, 3)/(z**5)**Rational(1, 3) - 1)*airybiprime(2*3**Rational(1, 3)*z**Rational(5, 3))/6 +
  544. (z**Rational(5, 3)/(z**5)**Rational(1, 3) + 1)*airyaiprime(2*3**Rational(1, 3)*z**Rational(5, 3))/2)
  545. def test_airybiprime():
  546. z = Symbol('z', real=False)
  547. t = Symbol('t', negative=True)
  548. p = Symbol('p', positive=True)
  549. assert isinstance(airybiprime(z), airybiprime)
  550. assert airybiprime(0) == 3**Rational(1, 6)/gamma(Rational(1, 3))
  551. assert airybiprime(oo) is oo
  552. assert airybiprime(-oo) == 0
  553. assert diff(airybiprime(z), z) == z*airybi(z)
  554. assert series(airybiprime(z), z, 0, 3) == (
  555. 3**Rational(1, 6)/gamma(Rational(1, 3)) + 3**Rational(5, 6)*z**2/(6*gamma(Rational(2, 3))) + O(z**3))
  556. assert airybiprime(z).rewrite(hyper) == (
  557. 3**Rational(5, 6)*z**2*hyper((), (Rational(5, 3),), z**3/9)/(6*gamma(Rational(2, 3))) +
  558. 3**Rational(1, 6)*hyper((), (Rational(1, 3),), z**3/9)/gamma(Rational(1, 3)))
  559. assert isinstance(airybiprime(z).rewrite(besselj), airybiprime)
  560. assert airyai(t).rewrite(besselj) == (
  561. sqrt(-t)*(besselj(Rational(-1, 3), 2*(-t)**Rational(3, 2)/3) +
  562. besselj(Rational(1, 3), 2*(-t)**Rational(3, 2)/3))/3)
  563. assert airybiprime(z).rewrite(besseli) == (
  564. sqrt(3)*(z**2*besseli(Rational(2, 3), 2*z**Rational(3, 2)/3)/(z**Rational(3, 2))**Rational(2, 3) +
  565. (z**Rational(3, 2))**Rational(2, 3)*besseli(Rational(-2, 3), 2*z**Rational(3, 2)/3))/3)
  566. assert airybiprime(p).rewrite(besseli) == (
  567. sqrt(3)*p*(besseli(Rational(-2, 3), 2*p**Rational(3, 2)/3) + besseli(Rational(2, 3), 2*p**Rational(3, 2)/3))/3)
  568. assert expand_func(airybiprime(2*(3*z**5)**Rational(1, 3))) == (
  569. sqrt(3)*(z**Rational(5, 3)/(z**5)**Rational(1, 3) - 1)*airyaiprime(2*3**Rational(1, 3)*z**Rational(5, 3))/2 +
  570. (z**Rational(5, 3)/(z**5)**Rational(1, 3) + 1)*airybiprime(2*3**Rational(1, 3)*z**Rational(5, 3))/2)
  571. def test_marcumq():
  572. m = Symbol('m')
  573. a = Symbol('a')
  574. b = Symbol('b')
  575. assert marcumq(0, 0, 0) == 0
  576. assert marcumq(m, 0, b) == uppergamma(m, b**2/2)/gamma(m)
  577. assert marcumq(2, 0, 5) == 27*exp(Rational(-25, 2))/2
  578. assert marcumq(0, a, 0) == 1 - exp(-a**2/2)
  579. assert marcumq(0, pi, 0) == 1 - exp(-pi**2/2)
  580. assert marcumq(1, a, a) == S.Half + exp(-a**2)*besseli(0, a**2)/2
  581. assert marcumq(2, a, a) == S.Half + exp(-a**2)*besseli(0, a**2)/2 + exp(-a**2)*besseli(1, a**2)
  582. assert diff(marcumq(1, a, 3), a) == a*(-marcumq(1, a, 3) + marcumq(2, a, 3))
  583. assert diff(marcumq(2, 3, b), b) == -b**2*exp(-b**2/2 - Rational(9, 2))*besseli(1, 3*b)/3
  584. x = Symbol('x')
  585. assert marcumq(2, 3, 4).rewrite(Integral, x=x) == \
  586. Integral(x**2*exp(-x**2/2 - Rational(9, 2))*besseli(1, 3*x), (x, 4, oo))/3
  587. assert eq([marcumq(5, -2, 3).rewrite(Integral).evalf(10)],
  588. [0.7905769565])
  589. k = Symbol('k')
  590. assert marcumq(-3, -5, -7).rewrite(Sum, k=k) == \
  591. exp(-37)*Sum((Rational(5, 7))**k*besseli(k, 35), (k, 4, oo))
  592. assert eq([marcumq(1, 3, 1).rewrite(Sum).evalf(10)],
  593. [0.9891705502])
  594. assert marcumq(1, a, a, evaluate=False).rewrite(besseli) == S.Half + exp(-a**2)*besseli(0, a**2)/2
  595. assert marcumq(2, a, a, evaluate=False).rewrite(besseli) == S.Half + exp(-a**2)*besseli(0, a**2)/2 + \
  596. exp(-a**2)*besseli(1, a**2)
  597. assert marcumq(3, a, a).rewrite(besseli) == (besseli(1, a**2) + besseli(2, a**2))*exp(-a**2) + \
  598. S.Half + exp(-a**2)*besseli(0, a**2)/2
  599. assert marcumq(5, 8, 8).rewrite(besseli) == exp(-64)*besseli(0, 64)/2 + \
  600. (besseli(4, 64) + besseli(3, 64) + besseli(2, 64) + besseli(1, 64))*exp(-64) + S.Half
  601. assert marcumq(m, a, a).rewrite(besseli) == marcumq(m, a, a)
  602. x = Symbol('x', integer=True)
  603. assert marcumq(x, a, a).rewrite(besseli) == marcumq(x, a, a)