test_bessel.py 36 KB

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