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.

763 lines
32 KiB

6 months ago
  1. from sympy.core.function import expand_func
  2. from sympy.core.numbers import (I, Rational, oo, pi)
  3. from sympy.core.singleton import S
  4. from sympy.core.sorting import default_sort_key
  5. from sympy.functions.elementary.exponential import (exp, exp_polar, log)
  6. from sympy.functions.elementary.hyperbolic import cosh
  7. from sympy.functions.elementary.miscellaneous import sqrt
  8. from sympy.functions.elementary.trigonometric import (cos, sin, sinc)
  9. from sympy.functions.special.error_functions import (erf, erfc)
  10. from sympy.functions.special.gamma_functions import (gamma, polygamma)
  11. from sympy.functions.special.hyper import (hyper, meijerg)
  12. from sympy.integrals.integrals import (Integral, integrate)
  13. from sympy.simplify.hyperexpand import hyperexpand
  14. from sympy.simplify.simplify import simplify
  15. from sympy.integrals.meijerint import (_rewrite_single, _rewrite1,
  16. meijerint_indefinite, _inflate_g, _create_lookup_table,
  17. meijerint_definite, meijerint_inversion)
  18. from sympy.testing.pytest import slow
  19. from sympy.core.random import (verify_numerically,
  20. random_complex_number as randcplx)
  21. from sympy.abc import x, y, a, b, c, d, s, t, z
  22. def test_rewrite_single():
  23. def t(expr, c, m):
  24. e = _rewrite_single(meijerg([a], [b], [c], [d], expr), x)
  25. assert e is not None
  26. assert isinstance(e[0][0][2], meijerg)
  27. assert e[0][0][2].argument.as_coeff_mul(x) == (c, (m,))
  28. def tn(expr):
  29. assert _rewrite_single(meijerg([a], [b], [c], [d], expr), x) is None
  30. t(x, 1, x)
  31. t(x**2, 1, x**2)
  32. t(x**2 + y*x**2, y + 1, x**2)
  33. tn(x**2 + x)
  34. tn(x**y)
  35. def u(expr, x):
  36. from sympy.core.add import Add
  37. r = _rewrite_single(expr, x)
  38. e = Add(*[res[0]*res[2] for res in r[0]]).replace(
  39. exp_polar, exp) # XXX Hack?
  40. assert verify_numerically(e, expr, x)
  41. u(exp(-x)*sin(x), x)
  42. # The following has stopped working because hyperexpand changed slightly.
  43. # It is probably not worth fixing
  44. #u(exp(-x)*sin(x)*cos(x), x)
  45. # This one cannot be done numerically, since it comes out as a g-function
  46. # of argument 4*pi
  47. # NOTE This also tests a bug in inverse mellin transform (which used to
  48. # turn exp(4*pi*I*t) into a factor of exp(4*pi*I)**t instead of
  49. # exp_polar).
  50. #u(exp(x)*sin(x), x)
  51. assert _rewrite_single(exp(x)*sin(x), x) == \
  52. ([(-sqrt(2)/(2*sqrt(pi)), 0,
  53. meijerg(((Rational(-1, 2), 0, Rational(1, 4), S.Half, Rational(3, 4)), (1,)),
  54. ((), (Rational(-1, 2), 0)), 64*exp_polar(-4*I*pi)/x**4))], True)
  55. def test_rewrite1():
  56. assert _rewrite1(x**3*meijerg([a], [b], [c], [d], x**2 + y*x**2)*5, x) == \
  57. (5, x**3, [(1, 0, meijerg([a], [b], [c], [d], x**2*(y + 1)))], True)
  58. def test_meijerint_indefinite_numerically():
  59. def t(fac, arg):
  60. g = meijerg([a], [b], [c], [d], arg)*fac
  61. subs = {a: randcplx()/10, b: randcplx()/10 + I,
  62. c: randcplx(), d: randcplx()}
  63. integral = meijerint_indefinite(g, x)
  64. assert integral is not None
  65. assert verify_numerically(g.subs(subs), integral.diff(x).subs(subs), x)
  66. t(1, x)
  67. t(2, x)
  68. t(1, 2*x)
  69. t(1, x**2)
  70. t(5, x**S('3/2'))
  71. t(x**3, x)
  72. t(3*x**S('3/2'), 4*x**S('7/3'))
  73. def test_meijerint_definite():
  74. v, b = meijerint_definite(x, x, 0, 0)
  75. assert v.is_zero and b is True
  76. v, b = meijerint_definite(x, x, oo, oo)
  77. assert v.is_zero and b is True
  78. def test_inflate():
  79. subs = {a: randcplx()/10, b: randcplx()/10 + I, c: randcplx(),
  80. d: randcplx(), y: randcplx()/10}
  81. def t(a, b, arg, n):
  82. from sympy.core.mul import Mul
  83. m1 = meijerg(a, b, arg)
  84. m2 = Mul(*_inflate_g(m1, n))
  85. # NOTE: (the random number)**9 must still be on the principal sheet.
  86. # Thus make b&d small to create random numbers of small imaginary part.
  87. return verify_numerically(m1.subs(subs), m2.subs(subs), x, b=0.1, d=-0.1)
  88. assert t([[a], [b]], [[c], [d]], x, 3)
  89. assert t([[a, y], [b]], [[c], [d]], x, 3)
  90. assert t([[a], [b]], [[c, y], [d]], 2*x**3, 3)
  91. def test_recursive():
  92. from sympy.core.symbol import symbols
  93. a, b, c = symbols('a b c', positive=True)
  94. r = exp(-(x - a)**2)*exp(-(x - b)**2)
  95. e = integrate(r, (x, 0, oo), meijerg=True)
  96. assert simplify(e.expand()) == (
  97. sqrt(2)*sqrt(pi)*(
  98. (erf(sqrt(2)*(a + b)/2) + 1)*exp(-a**2/2 + a*b - b**2/2))/4)
  99. e = integrate(exp(-(x - a)**2)*exp(-(x - b)**2)*exp(c*x), (x, 0, oo), meijerg=True)
  100. assert simplify(e) == (
  101. sqrt(2)*sqrt(pi)*(erf(sqrt(2)*(2*a + 2*b + c)/4) + 1)*exp(-a**2 - b**2
  102. + (2*a + 2*b + c)**2/8)/4)
  103. assert simplify(integrate(exp(-(x - a - b - c)**2), (x, 0, oo), meijerg=True)) == \
  104. sqrt(pi)/2*(1 + erf(a + b + c))
  105. assert simplify(integrate(exp(-(x + a + b + c)**2), (x, 0, oo), meijerg=True)) == \
  106. sqrt(pi)/2*(1 - erf(a + b + c))
  107. @slow
  108. def test_meijerint():
  109. from sympy.core.function import expand
  110. from sympy.core.symbol import symbols
  111. from sympy.functions.elementary.complexes import arg
  112. s, t, mu = symbols('s t mu', real=True)
  113. assert integrate(meijerg([], [], [0], [], s*t)
  114. *meijerg([], [], [mu/2], [-mu/2], t**2/4),
  115. (t, 0, oo)).is_Piecewise
  116. s = symbols('s', positive=True)
  117. assert integrate(x**s*meijerg([[], []], [[0], []], x), (x, 0, oo)) == \
  118. gamma(s + 1)
  119. assert integrate(x**s*meijerg([[], []], [[0], []], x), (x, 0, oo),
  120. meijerg=True) == gamma(s + 1)
  121. assert isinstance(integrate(x**s*meijerg([[], []], [[0], []], x),
  122. (x, 0, oo), meijerg=False),
  123. Integral)
  124. assert meijerint_indefinite(exp(x), x) == exp(x)
  125. # TODO what simplifications should be done automatically?
  126. # This tests "extra case" for antecedents_1.
  127. a, b = symbols('a b', positive=True)
  128. assert simplify(meijerint_definite(x**a, x, 0, b)[0]) == \
  129. b**(a + 1)/(a + 1)
  130. # This tests various conditions and expansions:
  131. assert meijerint_definite((x + 1)**3*exp(-x), x, 0, oo) == (16, True)
  132. # Again, how about simplifications?
  133. sigma, mu = symbols('sigma mu', positive=True)
  134. i, c = meijerint_definite(exp(-((x - mu)/(2*sigma))**2), x, 0, oo)
  135. assert simplify(i) == sqrt(pi)*sigma*(2 - erfc(mu/(2*sigma)))
  136. assert c == True
  137. i, _ = meijerint_definite(exp(-mu*x)*exp(sigma*x), x, 0, oo)
  138. # TODO it would be nice to test the condition
  139. assert simplify(i) == 1/(mu - sigma)
  140. # Test substitutions to change limits
  141. assert meijerint_definite(exp(x), x, -oo, 2) == (exp(2), True)
  142. # Note: causes a NaN in _check_antecedents
  143. assert expand(meijerint_definite(exp(x), x, 0, I)[0]) == exp(I) - 1
  144. assert expand(meijerint_definite(exp(-x), x, 0, x)[0]) == \
  145. 1 - exp(-exp(I*arg(x))*abs(x))
  146. # Test -oo to oo
  147. assert meijerint_definite(exp(-x**2), x, -oo, oo) == (sqrt(pi), True)
  148. assert meijerint_definite(exp(-abs(x)), x, -oo, oo) == (2, True)
  149. assert meijerint_definite(exp(-(2*x - 3)**2), x, -oo, oo) == \
  150. (sqrt(pi)/2, True)
  151. assert meijerint_definite(exp(-abs(2*x - 3)), x, -oo, oo) == (1, True)
  152. assert meijerint_definite(exp(-((x - mu)/sigma)**2/2)/sqrt(2*pi*sigma**2),
  153. x, -oo, oo) == (1, True)
  154. assert meijerint_definite(sinc(x)**2, x, -oo, oo) == (pi, True)
  155. # Test one of the extra conditions for 2 g-functinos
  156. assert meijerint_definite(exp(-x)*sin(x), x, 0, oo) == (S.Half, True)
  157. # Test a bug
  158. def res(n):
  159. return (1/(1 + x**2)).diff(x, n).subs(x, 1)*(-1)**n
  160. for n in range(6):
  161. assert integrate(exp(-x)*sin(x)*x**n, (x, 0, oo), meijerg=True) == \
  162. res(n)
  163. # This used to test trigexpand... now it is done by linear substitution
  164. assert simplify(integrate(exp(-x)*sin(x + a), (x, 0, oo), meijerg=True)
  165. ) == sqrt(2)*sin(a + pi/4)/2
  166. # Test the condition 14 from prudnikov.
  167. # (This is besselj*besselj in disguise, to stop the product from being
  168. # recognised in the tables.)
  169. a, b, s = symbols('a b s')
  170. from sympy.functions.elementary.complexes import re
  171. assert meijerint_definite(meijerg([], [], [a/2], [-a/2], x/4)
  172. *meijerg([], [], [b/2], [-b/2], x/4)*x**(s - 1), x, 0, oo
  173. ) == (
  174. (4*2**(2*s - 2)*gamma(-2*s + 1)*gamma(a/2 + b/2 + s)
  175. /(gamma(-a/2 + b/2 - s + 1)*gamma(a/2 - b/2 - s + 1)
  176. *gamma(a/2 + b/2 - s + 1)),
  177. (re(s) < 1) & (re(s) < S(1)/2) & (re(a)/2 + re(b)/2 + re(s) > 0)))
  178. # test a bug
  179. assert integrate(sin(x**a)*sin(x**b), (x, 0, oo), meijerg=True) == \
  180. Integral(sin(x**a)*sin(x**b), (x, 0, oo))
  181. # test better hyperexpand
  182. assert integrate(exp(-x**2)*log(x), (x, 0, oo), meijerg=True) == \
  183. (sqrt(pi)*polygamma(0, S.Half)/4).expand()
  184. # Test hyperexpand bug.
  185. from sympy.functions.special.gamma_functions import lowergamma
  186. n = symbols('n', integer=True)
  187. assert simplify(integrate(exp(-x)*x**n, x, meijerg=True)) == \
  188. lowergamma(n + 1, x)
  189. # Test a bug with argument 1/x
  190. alpha = symbols('alpha', positive=True)
  191. assert meijerint_definite((2 - x)**alpha*sin(alpha/x), x, 0, 2) == \
  192. (sqrt(pi)*alpha*gamma(alpha + 1)*meijerg(((), (alpha/2 + S.Half,
  193. alpha/2 + 1)), ((0, 0, S.Half), (Rational(-1, 2),)), alpha**2/16)/4, True)
  194. # test a bug related to 3016
  195. a, s = symbols('a s', positive=True)
  196. assert simplify(integrate(x**s*exp(-a*x**2), (x, -oo, oo))) == \
  197. a**(-s/2 - S.Half)*((-1)**s + 1)*gamma(s/2 + S.Half)/2
  198. def test_bessel():
  199. from sympy.functions.special.bessel import (besseli, besselj)
  200. assert simplify(integrate(besselj(a, z)*besselj(b, z)/z, (z, 0, oo),
  201. meijerg=True, conds='none')) == \
  202. 2*sin(pi*(a/2 - b/2))/(pi*(a - b)*(a + b))
  203. assert simplify(integrate(besselj(a, z)*besselj(a, z)/z, (z, 0, oo),
  204. meijerg=True, conds='none')) == 1/(2*a)
  205. # TODO more orthogonality integrals
  206. assert simplify(integrate(sin(z*x)*(x**2 - 1)**(-(y + S.Half)),
  207. (x, 1, oo), meijerg=True, conds='none')
  208. *2/((z/2)**y*sqrt(pi)*gamma(S.Half - y))) == \
  209. besselj(y, z)
  210. # Werner Rosenheinrich
  211. # SOME INDEFINITE INTEGRALS OF BESSEL FUNCTIONS
  212. assert integrate(x*besselj(0, x), x, meijerg=True) == x*besselj(1, x)
  213. assert integrate(x*besseli(0, x), x, meijerg=True) == x*besseli(1, x)
  214. # TODO can do higher powers, but come out as high order ... should they be
  215. # reduced to order 0, 1?
  216. assert integrate(besselj(1, x), x, meijerg=True) == -besselj(0, x)
  217. assert integrate(besselj(1, x)**2/x, x, meijerg=True) == \
  218. -(besselj(0, x)**2 + besselj(1, x)**2)/2
  219. # TODO more besseli when tables are extended or recursive mellin works
  220. assert integrate(besselj(0, x)**2/x**2, x, meijerg=True) == \
  221. -2*x*besselj(0, x)**2 - 2*x*besselj(1, x)**2 \
  222. + 2*besselj(0, x)*besselj(1, x) - besselj(0, x)**2/x
  223. assert integrate(besselj(0, x)*besselj(1, x), x, meijerg=True) == \
  224. -besselj(0, x)**2/2
  225. assert integrate(x**2*besselj(0, x)*besselj(1, x), x, meijerg=True) == \
  226. x**2*besselj(1, x)**2/2
  227. assert integrate(besselj(0, x)*besselj(1, x)/x, x, meijerg=True) == \
  228. (x*besselj(0, x)**2 + x*besselj(1, x)**2 -
  229. besselj(0, x)*besselj(1, x))
  230. # TODO how does besselj(0, a*x)*besselj(0, b*x) work?
  231. # TODO how does besselj(0, x)**2*besselj(1, x)**2 work?
  232. # TODO sin(x)*besselj(0, x) etc come out a mess
  233. # TODO can x*log(x)*besselj(0, x) be done?
  234. # TODO how does besselj(1, x)*besselj(0, x+a) work?
  235. # TODO more indefinite integrals when struve functions etc are implemented
  236. # test a substitution
  237. assert integrate(besselj(1, x**2)*x, x, meijerg=True) == \
  238. -besselj(0, x**2)/2
  239. def test_inversion():
  240. from sympy.functions.elementary.piecewise import piecewise_fold
  241. from sympy.functions.special.bessel import besselj
  242. from sympy.functions.special.delta_functions import Heaviside
  243. def inv(f):
  244. return piecewise_fold(meijerint_inversion(f, s, t))
  245. assert inv(1/(s**2 + 1)) == sin(t)*Heaviside(t)
  246. assert inv(s/(s**2 + 1)) == cos(t)*Heaviside(t)
  247. assert inv(exp(-s)/s) == Heaviside(t - 1)
  248. assert inv(1/sqrt(1 + s**2)) == besselj(0, t)*Heaviside(t)
  249. # Test some antcedents checking.
  250. assert meijerint_inversion(sqrt(s)/sqrt(1 + s**2), s, t) is None
  251. assert inv(exp(s**2)) is None
  252. assert meijerint_inversion(exp(-s**2), s, t) is None
  253. def test_inversion_conditional_output():
  254. from sympy.core.symbol import Symbol
  255. from sympy.integrals.transforms import InverseLaplaceTransform
  256. a = Symbol('a', positive=True)
  257. F = sqrt(pi/a)*exp(-2*sqrt(a)*sqrt(s))
  258. f = meijerint_inversion(F, s, t)
  259. assert not f.is_Piecewise
  260. b = Symbol('b', real=True)
  261. F = F.subs(a, b)
  262. f2 = meijerint_inversion(F, s, t)
  263. assert f2.is_Piecewise
  264. # first piece is same as f
  265. assert f2.args[0][0] == f.subs(a, b)
  266. # last piece is an unevaluated transform
  267. assert f2.args[-1][1]
  268. ILT = InverseLaplaceTransform(F, s, t, None)
  269. assert f2.args[-1][0] == ILT or f2.args[-1][0] == ILT.as_integral
  270. def test_inversion_exp_real_nonreal_shift():
  271. from sympy.core.symbol import Symbol
  272. from sympy.functions.special.delta_functions import DiracDelta
  273. r = Symbol('r', real=True)
  274. c = Symbol('c', extended_real=False)
  275. a = 1 + 2*I
  276. z = Symbol('z')
  277. assert not meijerint_inversion(exp(r*s), s, t).is_Piecewise
  278. assert meijerint_inversion(exp(a*s), s, t) is None
  279. assert meijerint_inversion(exp(c*s), s, t) is None
  280. f = meijerint_inversion(exp(z*s), s, t)
  281. assert f.is_Piecewise
  282. assert isinstance(f.args[0][0], DiracDelta)
  283. @slow
  284. def test_lookup_table():
  285. from sympy.core.random import uniform, randrange
  286. from sympy.core.add import Add
  287. from sympy.integrals.meijerint import z as z_dummy
  288. table = {}
  289. _create_lookup_table(table)
  290. for _, l in sorted(table.items()):
  291. for formula, terms, cond, hint in sorted(l, key=default_sort_key):
  292. subs = {}
  293. for ai in list(formula.free_symbols) + [z_dummy]:
  294. if hasattr(ai, 'properties') and ai.properties:
  295. # these Wilds match positive integers
  296. subs[ai] = randrange(1, 10)
  297. else:
  298. subs[ai] = uniform(1.5, 2.0)
  299. if not isinstance(terms, list):
  300. terms = terms(subs)
  301. # First test that hyperexpand can do this.
  302. expanded = [hyperexpand(g) for (_, g) in terms]
  303. assert all(x.is_Piecewise or not x.has(meijerg) for x in expanded)
  304. # Now test that the meijer g-function is indeed as advertised.
  305. expanded = Add(*[f*x for (f, x) in terms])
  306. a, b = formula.n(subs=subs), expanded.n(subs=subs)
  307. r = min(abs(a), abs(b))
  308. if r < 1:
  309. assert abs(a - b).n() <= 1e-10
  310. else:
  311. assert (abs(a - b)/r).n() <= 1e-10
  312. def test_branch_bug():
  313. from sympy.functions.special.gamma_functions import lowergamma
  314. from sympy.simplify.powsimp import powdenest
  315. # TODO gammasimp cannot prove that the factor is unity
  316. assert powdenest(integrate(erf(x**3), x, meijerg=True).diff(x),
  317. polar=True) == 2*erf(x**3)*gamma(Rational(2, 3))/3/gamma(Rational(5, 3))
  318. assert integrate(erf(x**3), x, meijerg=True) == \
  319. 2*x*erf(x**3)*gamma(Rational(2, 3))/(3*gamma(Rational(5, 3))) \
  320. - 2*gamma(Rational(2, 3))*lowergamma(Rational(2, 3), x**6)/(3*sqrt(pi)*gamma(Rational(5, 3)))
  321. def test_linear_subs():
  322. from sympy.functions.special.bessel import besselj
  323. assert integrate(sin(x - 1), x, meijerg=True) == -cos(1 - x)
  324. assert integrate(besselj(1, x - 1), x, meijerg=True) == -besselj(0, 1 - x)
  325. @slow
  326. def test_probability():
  327. # various integrals from probability theory
  328. from sympy.core.function import expand_mul
  329. from sympy.core.symbol import (Symbol, symbols)
  330. from sympy.functions.elementary.complexes import Abs
  331. from sympy.simplify.gammasimp import gammasimp
  332. from sympy.simplify.powsimp import powsimp
  333. mu1, mu2 = symbols('mu1 mu2', nonzero=True)
  334. sigma1, sigma2 = symbols('sigma1 sigma2', positive=True)
  335. rate = Symbol('lambda', positive=True)
  336. def normal(x, mu, sigma):
  337. return 1/sqrt(2*pi*sigma**2)*exp(-(x - mu)**2/2/sigma**2)
  338. def exponential(x, rate):
  339. return rate*exp(-rate*x)
  340. assert integrate(normal(x, mu1, sigma1), (x, -oo, oo), meijerg=True) == 1
  341. assert integrate(x*normal(x, mu1, sigma1), (x, -oo, oo), meijerg=True) == \
  342. mu1
  343. assert integrate(x**2*normal(x, mu1, sigma1), (x, -oo, oo), meijerg=True) \
  344. == mu1**2 + sigma1**2
  345. assert integrate(x**3*normal(x, mu1, sigma1), (x, -oo, oo), meijerg=True) \
  346. == mu1**3 + 3*mu1*sigma1**2
  347. assert integrate(normal(x, mu1, sigma1)*normal(y, mu2, sigma2),
  348. (x, -oo, oo), (y, -oo, oo), meijerg=True) == 1
  349. assert integrate(x*normal(x, mu1, sigma1)*normal(y, mu2, sigma2),
  350. (x, -oo, oo), (y, -oo, oo), meijerg=True) == mu1
  351. assert integrate(y*normal(x, mu1, sigma1)*normal(y, mu2, sigma2),
  352. (x, -oo, oo), (y, -oo, oo), meijerg=True) == mu2
  353. assert integrate(x*y*normal(x, mu1, sigma1)*normal(y, mu2, sigma2),
  354. (x, -oo, oo), (y, -oo, oo), meijerg=True) == mu1*mu2
  355. assert integrate((x + y + 1)*normal(x, mu1, sigma1)*normal(y, mu2, sigma2),
  356. (x, -oo, oo), (y, -oo, oo), meijerg=True) == 1 + mu1 + mu2
  357. assert integrate((x + y - 1)*normal(x, mu1, sigma1)*normal(y, mu2, sigma2),
  358. (x, -oo, oo), (y, -oo, oo), meijerg=True) == \
  359. -1 + mu1 + mu2
  360. i = integrate(x**2*normal(x, mu1, sigma1)*normal(y, mu2, sigma2),
  361. (x, -oo, oo), (y, -oo, oo), meijerg=True)
  362. assert not i.has(Abs)
  363. assert simplify(i) == mu1**2 + sigma1**2
  364. assert integrate(y**2*normal(x, mu1, sigma1)*normal(y, mu2, sigma2),
  365. (x, -oo, oo), (y, -oo, oo), meijerg=True) == \
  366. sigma2**2 + mu2**2
  367. assert integrate(exponential(x, rate), (x, 0, oo), meijerg=True) == 1
  368. assert integrate(x*exponential(x, rate), (x, 0, oo), meijerg=True) == \
  369. 1/rate
  370. assert integrate(x**2*exponential(x, rate), (x, 0, oo), meijerg=True) == \
  371. 2/rate**2
  372. def E(expr):
  373. res1 = integrate(expr*exponential(x, rate)*normal(y, mu1, sigma1),
  374. (x, 0, oo), (y, -oo, oo), meijerg=True)
  375. res2 = integrate(expr*exponential(x, rate)*normal(y, mu1, sigma1),
  376. (y, -oo, oo), (x, 0, oo), meijerg=True)
  377. assert expand_mul(res1) == expand_mul(res2)
  378. return res1
  379. assert E(1) == 1
  380. assert E(x*y) == mu1/rate
  381. assert E(x*y**2) == mu1**2/rate + sigma1**2/rate
  382. ans = sigma1**2 + 1/rate**2
  383. assert simplify(E((x + y + 1)**2) - E(x + y + 1)**2) == ans
  384. assert simplify(E((x + y - 1)**2) - E(x + y - 1)**2) == ans
  385. assert simplify(E((x + y)**2) - E(x + y)**2) == ans
  386. # Beta' distribution
  387. alpha, beta = symbols('alpha beta', positive=True)
  388. betadist = x**(alpha - 1)*(1 + x)**(-alpha - beta)*gamma(alpha + beta) \
  389. /gamma(alpha)/gamma(beta)
  390. assert integrate(betadist, (x, 0, oo), meijerg=True) == 1
  391. i = integrate(x*betadist, (x, 0, oo), meijerg=True, conds='separate')
  392. assert (gammasimp(i[0]), i[1]) == (alpha/(beta - 1), 1 < beta)
  393. j = integrate(x**2*betadist, (x, 0, oo), meijerg=True, conds='separate')
  394. assert j[1] == (beta > 2)
  395. assert gammasimp(j[0] - i[0]**2) == (alpha + beta - 1)*alpha \
  396. /(beta - 2)/(beta - 1)**2
  397. # Beta distribution
  398. # NOTE: this is evaluated using antiderivatives. It also tests that
  399. # meijerint_indefinite returns the simplest possible answer.
  400. a, b = symbols('a b', positive=True)
  401. betadist = x**(a - 1)*(-x + 1)**(b - 1)*gamma(a + b)/(gamma(a)*gamma(b))
  402. assert simplify(integrate(betadist, (x, 0, 1), meijerg=True)) == 1
  403. assert simplify(integrate(x*betadist, (x, 0, 1), meijerg=True)) == \
  404. a/(a + b)
  405. assert simplify(integrate(x**2*betadist, (x, 0, 1), meijerg=True)) == \
  406. a*(a + 1)/(a + b)/(a + b + 1)
  407. assert simplify(integrate(x**y*betadist, (x, 0, 1), meijerg=True)) == \
  408. gamma(a + b)*gamma(a + y)/gamma(a)/gamma(a + b + y)
  409. # Chi distribution
  410. k = Symbol('k', integer=True, positive=True)
  411. chi = 2**(1 - k/2)*x**(k - 1)*exp(-x**2/2)/gamma(k/2)
  412. assert powsimp(integrate(chi, (x, 0, oo), meijerg=True)) == 1
  413. assert simplify(integrate(x*chi, (x, 0, oo), meijerg=True)) == \
  414. sqrt(2)*gamma((k + 1)/2)/gamma(k/2)
  415. assert simplify(integrate(x**2*chi, (x, 0, oo), meijerg=True)) == k
  416. # Chi^2 distribution
  417. chisquared = 2**(-k/2)/gamma(k/2)*x**(k/2 - 1)*exp(-x/2)
  418. assert powsimp(integrate(chisquared, (x, 0, oo), meijerg=True)) == 1
  419. assert simplify(integrate(x*chisquared, (x, 0, oo), meijerg=True)) == k
  420. assert simplify(integrate(x**2*chisquared, (x, 0, oo), meijerg=True)) == \
  421. k*(k + 2)
  422. assert gammasimp(integrate(((x - k)/sqrt(2*k))**3*chisquared, (x, 0, oo),
  423. meijerg=True)) == 2*sqrt(2)/sqrt(k)
  424. # Dagum distribution
  425. a, b, p = symbols('a b p', positive=True)
  426. # XXX (x/b)**a does not work
  427. dagum = a*p/x*(x/b)**(a*p)/(1 + x**a/b**a)**(p + 1)
  428. assert simplify(integrate(dagum, (x, 0, oo), meijerg=True)) == 1
  429. # XXX conditions are a mess
  430. arg = x*dagum
  431. assert simplify(integrate(arg, (x, 0, oo), meijerg=True, conds='none')
  432. ) == a*b*gamma(1 - 1/a)*gamma(p + 1 + 1/a)/(
  433. (a*p + 1)*gamma(p))
  434. assert simplify(integrate(x*arg, (x, 0, oo), meijerg=True, conds='none')
  435. ) == a*b**2*gamma(1 - 2/a)*gamma(p + 1 + 2/a)/(
  436. (a*p + 2)*gamma(p))
  437. # F-distribution
  438. d1, d2 = symbols('d1 d2', positive=True)
  439. f = sqrt(((d1*x)**d1 * d2**d2)/(d1*x + d2)**(d1 + d2))/x \
  440. /gamma(d1/2)/gamma(d2/2)*gamma((d1 + d2)/2)
  441. assert simplify(integrate(f, (x, 0, oo), meijerg=True)) == 1
  442. # TODO conditions are a mess
  443. assert simplify(integrate(x*f, (x, 0, oo), meijerg=True, conds='none')
  444. ) == d2/(d2 - 2)
  445. assert simplify(integrate(x**2*f, (x, 0, oo), meijerg=True, conds='none')
  446. ) == d2**2*(d1 + 2)/d1/(d2 - 4)/(d2 - 2)
  447. # TODO gamma, rayleigh
  448. # inverse gaussian
  449. lamda, mu = symbols('lamda mu', positive=True)
  450. dist = sqrt(lamda/2/pi)*x**(Rational(-3, 2))*exp(-lamda*(x - mu)**2/x/2/mu**2)
  451. mysimp = lambda expr: simplify(expr.rewrite(exp))
  452. assert mysimp(integrate(dist, (x, 0, oo))) == 1
  453. assert mysimp(integrate(x*dist, (x, 0, oo))) == mu
  454. assert mysimp(integrate((x - mu)**2*dist, (x, 0, oo))) == mu**3/lamda
  455. assert mysimp(integrate((x - mu)**3*dist, (x, 0, oo))) == 3*mu**5/lamda**2
  456. # Levi
  457. c = Symbol('c', positive=True)
  458. assert integrate(sqrt(c/2/pi)*exp(-c/2/(x - mu))/(x - mu)**S('3/2'),
  459. (x, mu, oo)) == 1
  460. # higher moments oo
  461. # log-logistic
  462. alpha, beta = symbols('alpha beta', positive=True)
  463. distn = (beta/alpha)*x**(beta - 1)/alpha**(beta - 1)/ \
  464. (1 + x**beta/alpha**beta)**2
  465. # FIXME: If alpha, beta are not declared as finite the line below hangs
  466. # after the changes in:
  467. # https://github.com/sympy/sympy/pull/16603
  468. assert simplify(integrate(distn, (x, 0, oo))) == 1
  469. # NOTE the conditions are a mess, but correctly state beta > 1
  470. assert simplify(integrate(x*distn, (x, 0, oo), conds='none')) == \
  471. pi*alpha/beta/sin(pi/beta)
  472. # (similar comment for conditions applies)
  473. assert simplify(integrate(x**y*distn, (x, 0, oo), conds='none')) == \
  474. pi*alpha**y*y/beta/sin(pi*y/beta)
  475. # weibull
  476. k = Symbol('k', positive=True)
  477. n = Symbol('n', positive=True)
  478. distn = k/lamda*(x/lamda)**(k - 1)*exp(-(x/lamda)**k)
  479. assert simplify(integrate(distn, (x, 0, oo))) == 1
  480. assert simplify(integrate(x**n*distn, (x, 0, oo))) == \
  481. lamda**n*gamma(1 + n/k)
  482. # rice distribution
  483. from sympy.functions.special.bessel import besseli
  484. nu, sigma = symbols('nu sigma', positive=True)
  485. rice = x/sigma**2*exp(-(x**2 + nu**2)/2/sigma**2)*besseli(0, x*nu/sigma**2)
  486. assert integrate(rice, (x, 0, oo), meijerg=True) == 1
  487. # can someone verify higher moments?
  488. # Laplace distribution
  489. mu = Symbol('mu', real=True)
  490. b = Symbol('b', positive=True)
  491. laplace = exp(-abs(x - mu)/b)/2/b
  492. assert integrate(laplace, (x, -oo, oo), meijerg=True) == 1
  493. assert integrate(x*laplace, (x, -oo, oo), meijerg=True) == mu
  494. assert integrate(x**2*laplace, (x, -oo, oo), meijerg=True) == \
  495. 2*b**2 + mu**2
  496. # TODO are there other distributions supported on (-oo, oo) that we can do?
  497. # misc tests
  498. k = Symbol('k', positive=True)
  499. assert gammasimp(expand_mul(integrate(log(x)*x**(k - 1)*exp(-x)/gamma(k),
  500. (x, 0, oo)))) == polygamma(0, k)
  501. @slow
  502. def test_expint():
  503. """ Test various exponential integrals. """
  504. from sympy.core.symbol import Symbol
  505. from sympy.functions.elementary.complexes import unpolarify
  506. from sympy.functions.elementary.hyperbolic import sinh
  507. from sympy.functions.special.error_functions import (Chi, Ci, Ei, Shi, Si, expint)
  508. assert simplify(unpolarify(integrate(exp(-z*x)/x**y, (x, 1, oo),
  509. meijerg=True, conds='none'
  510. ).rewrite(expint).expand(func=True))) == expint(y, z)
  511. assert integrate(exp(-z*x)/x, (x, 1, oo), meijerg=True,
  512. conds='none').rewrite(expint).expand() == \
  513. expint(1, z)
  514. assert integrate(exp(-z*x)/x**2, (x, 1, oo), meijerg=True,
  515. conds='none').rewrite(expint).expand() == \
  516. expint(2, z).rewrite(Ei).rewrite(expint)
  517. assert integrate(exp(-z*x)/x**3, (x, 1, oo), meijerg=True,
  518. conds='none').rewrite(expint).expand() == \
  519. expint(3, z).rewrite(Ei).rewrite(expint).expand()
  520. t = Symbol('t', positive=True)
  521. assert integrate(-cos(x)/x, (x, t, oo), meijerg=True).expand() == Ci(t)
  522. assert integrate(-sin(x)/x, (x, t, oo), meijerg=True).expand() == \
  523. Si(t) - pi/2
  524. assert integrate(sin(x)/x, (x, 0, z), meijerg=True) == Si(z)
  525. assert integrate(sinh(x)/x, (x, 0, z), meijerg=True) == Shi(z)
  526. assert integrate(exp(-x)/x, x, meijerg=True).expand().rewrite(expint) == \
  527. I*pi - expint(1, x)
  528. assert integrate(exp(-x)/x**2, x, meijerg=True).rewrite(expint).expand() \
  529. == expint(1, x) - exp(-x)/x - I*pi
  530. u = Symbol('u', polar=True)
  531. assert integrate(cos(u)/u, u, meijerg=True).expand().as_independent(u)[1] \
  532. == Ci(u)
  533. assert integrate(cosh(u)/u, u, meijerg=True).expand().as_independent(u)[1] \
  534. == Chi(u)
  535. assert integrate(expint(1, x), x, meijerg=True
  536. ).rewrite(expint).expand() == x*expint(1, x) - exp(-x)
  537. assert integrate(expint(2, x), x, meijerg=True
  538. ).rewrite(expint).expand() == \
  539. -x**2*expint(1, x)/2 + x*exp(-x)/2 - exp(-x)/2
  540. assert simplify(unpolarify(integrate(expint(y, x), x,
  541. meijerg=True).rewrite(expint).expand(func=True))) == \
  542. -expint(y + 1, x)
  543. assert integrate(Si(x), x, meijerg=True) == x*Si(x) + cos(x)
  544. assert integrate(Ci(u), u, meijerg=True).expand() == u*Ci(u) - sin(u)
  545. assert integrate(Shi(x), x, meijerg=True) == x*Shi(x) - cosh(x)
  546. assert integrate(Chi(u), u, meijerg=True).expand() == u*Chi(u) - sinh(u)
  547. assert integrate(Si(x)*exp(-x), (x, 0, oo), meijerg=True) == pi/4
  548. assert integrate(expint(1, x)*sin(x), (x, 0, oo), meijerg=True) == log(2)/2
  549. def test_messy():
  550. from sympy.functions.elementary.complexes import re
  551. from sympy.functions.elementary.hyperbolic import (acosh, acoth)
  552. from sympy.functions.elementary.piecewise import Piecewise
  553. from sympy.functions.elementary.trigonometric import (asin, atan)
  554. from sympy.functions.special.bessel import besselj
  555. from sympy.functions.special.error_functions import (Chi, E1, Shi, Si)
  556. from sympy.integrals.transforms import (fourier_transform, laplace_transform)
  557. assert laplace_transform(Si(x), x, s) == ((-atan(s) + pi/2)/s, 0, True)
  558. assert laplace_transform(Shi(x), x, s) == (
  559. acoth(s)/s, -oo, s**2 > 1)
  560. # where should the logs be simplified?
  561. assert laplace_transform(Chi(x), x, s) == (
  562. (log(s**(-2)) - log(1 - 1/s**2))/(2*s), -oo, s**2 > 1)
  563. # TODO maybe simplify the inequalities? when the simplification
  564. # allows for generators instead of symbols this will work
  565. assert laplace_transform(besselj(a, x), x, s)[1:] == \
  566. (0, (re(a) > -2) & (re(a) > -1))
  567. # NOTE s < 0 can be done, but argument reduction is not good enough yet
  568. ans = fourier_transform(besselj(1, x)/x, x, s, noconds=False)
  569. assert tuple([ans[0].factor(deep=True).expand(), ans[1]]) == \
  570. (Piecewise((0, (s > 1/(2*pi)) | (s < -1/(2*pi))),
  571. (2*sqrt(-4*pi**2*s**2 + 1), True)), s > 0)
  572. # TODO FT(besselj(0,x)) - conditions are messy (but for acceptable reasons)
  573. # - folding could be better
  574. assert integrate(E1(x)*besselj(0, x), (x, 0, oo), meijerg=True) == \
  575. log(1 + sqrt(2))
  576. assert integrate(E1(x)*besselj(1, x), (x, 0, oo), meijerg=True) == \
  577. log(S.Half + sqrt(2)/2)
  578. assert integrate(1/x/sqrt(1 - x**2), x, meijerg=True) == \
  579. Piecewise((-acosh(1/x), abs(x**(-2)) > 1), (I*asin(1/x), True))
  580. def test_issue_6122():
  581. assert integrate(exp(-I*x**2), (x, -oo, oo), meijerg=True) == \
  582. -I*sqrt(pi)*exp(I*pi/4)
  583. def test_issue_6252():
  584. expr = 1/x/(a + b*x)**Rational(1, 3)
  585. anti = integrate(expr, x, meijerg=True)
  586. assert not anti.has(hyper)
  587. # XXX the expression is a mess, but actually upon differentiation and
  588. # putting in numerical values seems to work...
  589. def test_issue_6348():
  590. assert integrate(exp(I*x)/(1 + x**2), (x, -oo, oo)).simplify().rewrite(exp) \
  591. == pi*exp(-1)
  592. def test_fresnel():
  593. from sympy.functions.special.error_functions import (fresnelc, fresnels)
  594. assert expand_func(integrate(sin(pi*x**2/2), x)) == fresnels(x)
  595. assert expand_func(integrate(cos(pi*x**2/2), x)) == fresnelc(x)
  596. def test_issue_6860():
  597. assert meijerint_indefinite(x**x**x, x) is None
  598. def test_issue_7337():
  599. f = meijerint_indefinite(x*sqrt(2*x + 3), x).together()
  600. assert f == sqrt(2*x + 3)*(2*x**2 + x - 3)/5
  601. assert f._eval_interval(x, S.NegativeOne, S.One) == Rational(2, 5)
  602. def test_issue_8368():
  603. assert meijerint_indefinite(cosh(x)*exp(-x*t), x) == (
  604. (-t - 1)*exp(x) + (-t + 1)*exp(-x))*exp(-t*x)/2/(t**2 - 1)
  605. def test_issue_10211():
  606. from sympy.abc import h, w
  607. assert integrate((1/sqrt((y-x)**2 + h**2)**3), (x,0,w), (y,0,w)) == \
  608. 2*sqrt(1 + w**2/h**2)/h - 2/h
  609. def test_issue_11806():
  610. from sympy.core.symbol import symbols
  611. y, L = symbols('y L', positive=True)
  612. assert integrate(1/sqrt(x**2 + y**2)**3, (x, -L, L)) == \
  613. 2*L/(y**2*sqrt(L**2 + y**2))
  614. def test_issue_10681():
  615. from sympy.polys.domains.realfield import RR
  616. from sympy.abc import R, r
  617. f = integrate(r**2*(R**2-r**2)**0.5, r, meijerg=True)
  618. g = (1.0/3)*R**1.0*r**3*hyper((-0.5, Rational(3, 2)), (Rational(5, 2),),
  619. r**2*exp_polar(2*I*pi)/R**2)
  620. assert RR.almosteq((f/g).n(), 1.0, 1e-12)
  621. def test_issue_13536():
  622. from sympy.core.symbol import Symbol
  623. a = Symbol('a', positive=True)
  624. assert integrate(1/x**2, (x, oo, a)) == -1/a
  625. def test_issue_6462():
  626. from sympy.core.symbol import Symbol
  627. x = Symbol('x')
  628. n = Symbol('n')
  629. # Not the actual issue, still wrong answer for n = 1, but that there is no
  630. # exception
  631. assert integrate(cos(x**n)/x**n, x, meijerg=True).subs(n, 2).equals(
  632. integrate(cos(x**2)/x**2, x, meijerg=True))
  633. def test_indefinite_1_bug():
  634. assert integrate((b + t)**(-a), t, meijerg=True
  635. ) == -b/(b**a*(1 + t/b)**a*(a - 1)
  636. ) - t/(b**a*(1 + t/b)**a*(a - 1))