m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

696 lines
30 KiB

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