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.

724 lines
28 KiB

6 months ago
  1. from sympy.core.function import expand_func
  2. from sympy.core import EulerGamma
  3. from sympy.core.numbers import (I, Rational, nan, oo, pi, zoo)
  4. from sympy.core.singleton import S
  5. from sympy.core.symbol import (Dummy, Symbol)
  6. from sympy.functions.combinatorial.factorials import factorial
  7. from sympy.functions.combinatorial.numbers import harmonic
  8. from sympy.functions.elementary.complexes import (Abs, conjugate, im, re)
  9. from sympy.functions.elementary.exponential import (exp, exp_polar, log)
  10. from sympy.functions.elementary.hyperbolic import tanh
  11. from sympy.functions.elementary.miscellaneous import sqrt
  12. from sympy.functions.elementary.trigonometric import (cos, sin)
  13. from sympy.functions.special.error_functions import (Ei, erf, erfc)
  14. from sympy.functions.special.gamma_functions import (digamma, gamma, loggamma, lowergamma, multigamma, polygamma, trigamma, uppergamma)
  15. from sympy.functions.special.zeta_functions import zeta
  16. from sympy.series.order import O
  17. from sympy.core.expr import unchanged
  18. from sympy.core.function import ArgumentIndexError
  19. from sympy.testing.pytest import raises
  20. from sympy.core.random import (test_derivative_numerically as td,
  21. random_complex_number as randcplx,
  22. verify_numerically as tn)
  23. x = Symbol('x')
  24. y = Symbol('y')
  25. n = Symbol('n', integer=True)
  26. w = Symbol('w', real=True)
  27. def test_gamma():
  28. assert gamma(nan) is nan
  29. assert gamma(oo) is oo
  30. assert gamma(-100) is zoo
  31. assert gamma(0) is zoo
  32. assert gamma(-100.0) is zoo
  33. assert gamma(1) == 1
  34. assert gamma(2) == 1
  35. assert gamma(3) == 2
  36. assert gamma(102) == factorial(101)
  37. assert gamma(S.Half) == sqrt(pi)
  38. assert gamma(Rational(3, 2)) == sqrt(pi)*S.Half
  39. assert gamma(Rational(5, 2)) == sqrt(pi)*Rational(3, 4)
  40. assert gamma(Rational(7, 2)) == sqrt(pi)*Rational(15, 8)
  41. assert gamma(Rational(-1, 2)) == -2*sqrt(pi)
  42. assert gamma(Rational(-3, 2)) == sqrt(pi)*Rational(4, 3)
  43. assert gamma(Rational(-5, 2)) == sqrt(pi)*Rational(-8, 15)
  44. assert gamma(Rational(-15, 2)) == sqrt(pi)*Rational(256, 2027025)
  45. assert gamma(Rational(
  46. -11, 8)).expand(func=True) == Rational(64, 33)*gamma(Rational(5, 8))
  47. assert gamma(Rational(
  48. -10, 3)).expand(func=True) == Rational(81, 280)*gamma(Rational(2, 3))
  49. assert gamma(Rational(
  50. 14, 3)).expand(func=True) == Rational(880, 81)*gamma(Rational(2, 3))
  51. assert gamma(Rational(
  52. 17, 7)).expand(func=True) == Rational(30, 49)*gamma(Rational(3, 7))
  53. assert gamma(Rational(
  54. 19, 8)).expand(func=True) == Rational(33, 64)*gamma(Rational(3, 8))
  55. assert gamma(x).diff(x) == gamma(x)*polygamma(0, x)
  56. assert gamma(x - 1).expand(func=True) == gamma(x)/(x - 1)
  57. assert gamma(x + 2).expand(func=True, mul=False) == x*(x + 1)*gamma(x)
  58. assert conjugate(gamma(x)) == gamma(conjugate(x))
  59. assert expand_func(gamma(x + Rational(3, 2))) == \
  60. (x + S.Half)*gamma(x + S.Half)
  61. assert expand_func(gamma(x - S.Half)) == \
  62. gamma(S.Half + x)/(x - S.Half)
  63. # Test a bug:
  64. assert expand_func(gamma(x + Rational(3, 4))) == gamma(x + Rational(3, 4))
  65. # XXX: Not sure about these tests. I can fix them by defining e.g.
  66. # exp_polar.is_integer but I'm not sure if that makes sense.
  67. assert gamma(3*exp_polar(I*pi)/4).is_nonnegative is False
  68. assert gamma(3*exp_polar(I*pi)/4).is_extended_nonpositive is True
  69. y = Symbol('y', nonpositive=True, integer=True)
  70. assert gamma(y).is_real == False
  71. y = Symbol('y', positive=True, noninteger=True)
  72. assert gamma(y).is_real == True
  73. assert gamma(-1.0, evaluate=False).is_real == False
  74. assert gamma(0, evaluate=False).is_real == False
  75. assert gamma(-2, evaluate=False).is_real == False
  76. def test_gamma_rewrite():
  77. assert gamma(n).rewrite(factorial) == factorial(n - 1)
  78. def test_gamma_series():
  79. assert gamma(x + 1).series(x, 0, 3) == \
  80. 1 - EulerGamma*x + x**2*(EulerGamma**2/2 + pi**2/12) + O(x**3)
  81. assert gamma(x).series(x, -1, 3) == \
  82. -1/(x + 1) + EulerGamma - 1 + (x + 1)*(-1 - pi**2/12 - EulerGamma**2/2 + \
  83. EulerGamma) + (x + 1)**2*(-1 - pi**2/12 - EulerGamma**2/2 + EulerGamma**3/6 - \
  84. polygamma(2, 1)/6 + EulerGamma*pi**2/12 + EulerGamma) + O((x + 1)**3, (x, -1))
  85. def tn_branch(s, func):
  86. from sympy.core.random import uniform
  87. c = uniform(1, 5)
  88. expr = func(s, c*exp_polar(I*pi)) - func(s, c*exp_polar(-I*pi))
  89. eps = 1e-15
  90. expr2 = func(s + eps, -c + eps*I) - func(s + eps, -c - eps*I)
  91. return abs(expr.n() - expr2.n()).n() < 1e-10
  92. def test_lowergamma():
  93. from sympy.functions.special.error_functions import expint
  94. from sympy.functions.special.hyper import meijerg
  95. assert lowergamma(x, 0) == 0
  96. assert lowergamma(x, y).diff(y) == y**(x - 1)*exp(-y)
  97. assert td(lowergamma(randcplx(), y), y)
  98. assert td(lowergamma(x, randcplx()), x)
  99. assert lowergamma(x, y).diff(x) == \
  100. gamma(x)*digamma(x) - uppergamma(x, y)*log(y) \
  101. - meijerg([], [1, 1], [0, 0, x], [], y)
  102. assert lowergamma(S.Half, x) == sqrt(pi)*erf(sqrt(x))
  103. assert not lowergamma(S.Half - 3, x).has(lowergamma)
  104. assert not lowergamma(S.Half + 3, x).has(lowergamma)
  105. assert lowergamma(S.Half, x, evaluate=False).has(lowergamma)
  106. assert tn(lowergamma(S.Half + 3, x, evaluate=False),
  107. lowergamma(S.Half + 3, x), x)
  108. assert tn(lowergamma(S.Half - 3, x, evaluate=False),
  109. lowergamma(S.Half - 3, x), x)
  110. assert tn_branch(-3, lowergamma)
  111. assert tn_branch(-4, lowergamma)
  112. assert tn_branch(Rational(1, 3), lowergamma)
  113. assert tn_branch(pi, lowergamma)
  114. assert lowergamma(3, exp_polar(4*pi*I)*x) == lowergamma(3, x)
  115. assert lowergamma(y, exp_polar(5*pi*I)*x) == \
  116. exp(4*I*pi*y)*lowergamma(y, x*exp_polar(pi*I))
  117. assert lowergamma(-2, exp_polar(5*pi*I)*x) == \
  118. lowergamma(-2, x*exp_polar(I*pi)) + 2*pi*I
  119. assert conjugate(lowergamma(x, y)) == lowergamma(conjugate(x), conjugate(y))
  120. assert conjugate(lowergamma(x, 0)) == 0
  121. assert unchanged(conjugate, lowergamma(x, -oo))
  122. assert lowergamma(0, x)._eval_is_meromorphic(x, 0) == False
  123. assert lowergamma(S(1)/3, x)._eval_is_meromorphic(x, 0) == False
  124. assert lowergamma(1, x, evaluate=False)._eval_is_meromorphic(x, 0) == True
  125. assert lowergamma(x, x)._eval_is_meromorphic(x, 0) == False
  126. assert lowergamma(x + 1, x)._eval_is_meromorphic(x, 0) == False
  127. assert lowergamma(1/x, x)._eval_is_meromorphic(x, 0) == False
  128. assert lowergamma(0, x + 1)._eval_is_meromorphic(x, 0) == False
  129. assert lowergamma(S(1)/3, x + 1)._eval_is_meromorphic(x, 0) == True
  130. assert lowergamma(1, x + 1, evaluate=False)._eval_is_meromorphic(x, 0) == True
  131. assert lowergamma(x, x + 1)._eval_is_meromorphic(x, 0) == True
  132. assert lowergamma(x + 1, x + 1)._eval_is_meromorphic(x, 0) == True
  133. assert lowergamma(1/x, x + 1)._eval_is_meromorphic(x, 0) == False
  134. assert lowergamma(0, 1/x)._eval_is_meromorphic(x, 0) == False
  135. assert lowergamma(S(1)/3, 1/x)._eval_is_meromorphic(x, 0) == False
  136. assert lowergamma(1, 1/x, evaluate=False)._eval_is_meromorphic(x, 0) == False
  137. assert lowergamma(x, 1/x)._eval_is_meromorphic(x, 0) == False
  138. assert lowergamma(x + 1, 1/x)._eval_is_meromorphic(x, 0) == False
  139. assert lowergamma(1/x, 1/x)._eval_is_meromorphic(x, 0) == False
  140. assert lowergamma(x, 2).series(x, oo, 3) == \
  141. 2**x*(1 + 2/(x + 1))*exp(-2)/x + O(exp(x*log(2))/x**3, (x, oo))
  142. assert lowergamma(
  143. x, y).rewrite(expint) == -y**x*expint(-x + 1, y) + gamma(x)
  144. k = Symbol('k', integer=True)
  145. assert lowergamma(
  146. k, y).rewrite(expint) == -y**k*expint(-k + 1, y) + gamma(k)
  147. k = Symbol('k', integer=True, positive=False)
  148. assert lowergamma(k, y).rewrite(expint) == lowergamma(k, y)
  149. assert lowergamma(x, y).rewrite(uppergamma) == gamma(x) - uppergamma(x, y)
  150. assert lowergamma(70, 6) == factorial(69) - 69035724522603011058660187038367026272747334489677105069435923032634389419656200387949342530805432320 * exp(-6)
  151. assert (lowergamma(S(77) / 2, 6) - lowergamma(S(77) / 2, 6, evaluate=False)).evalf() < 1e-16
  152. assert (lowergamma(-S(77) / 2, 6) - lowergamma(-S(77) / 2, 6, evaluate=False)).evalf() < 1e-16
  153. def test_uppergamma():
  154. from sympy.functions.special.error_functions import expint
  155. from sympy.functions.special.hyper import meijerg
  156. assert uppergamma(4, 0) == 6
  157. assert uppergamma(x, y).diff(y) == -y**(x - 1)*exp(-y)
  158. assert td(uppergamma(randcplx(), y), y)
  159. assert uppergamma(x, y).diff(x) == \
  160. uppergamma(x, y)*log(y) + meijerg([], [1, 1], [0, 0, x], [], y)
  161. assert td(uppergamma(x, randcplx()), x)
  162. p = Symbol('p', positive=True)
  163. assert uppergamma(0, p) == -Ei(-p)
  164. assert uppergamma(p, 0) == gamma(p)
  165. assert uppergamma(S.Half, x) == sqrt(pi)*erfc(sqrt(x))
  166. assert not uppergamma(S.Half - 3, x).has(uppergamma)
  167. assert not uppergamma(S.Half + 3, x).has(uppergamma)
  168. assert uppergamma(S.Half, x, evaluate=False).has(uppergamma)
  169. assert tn(uppergamma(S.Half + 3, x, evaluate=False),
  170. uppergamma(S.Half + 3, x), x)
  171. assert tn(uppergamma(S.Half - 3, x, evaluate=False),
  172. uppergamma(S.Half - 3, x), x)
  173. assert unchanged(uppergamma, x, -oo)
  174. assert unchanged(uppergamma, x, 0)
  175. assert tn_branch(-3, uppergamma)
  176. assert tn_branch(-4, uppergamma)
  177. assert tn_branch(Rational(1, 3), uppergamma)
  178. assert tn_branch(pi, uppergamma)
  179. assert uppergamma(3, exp_polar(4*pi*I)*x) == uppergamma(3, x)
  180. assert uppergamma(y, exp_polar(5*pi*I)*x) == \
  181. exp(4*I*pi*y)*uppergamma(y, x*exp_polar(pi*I)) + \
  182. gamma(y)*(1 - exp(4*pi*I*y))
  183. assert uppergamma(-2, exp_polar(5*pi*I)*x) == \
  184. uppergamma(-2, x*exp_polar(I*pi)) - 2*pi*I
  185. assert uppergamma(-2, x) == expint(3, x)/x**2
  186. assert conjugate(uppergamma(x, y)) == uppergamma(conjugate(x), conjugate(y))
  187. assert unchanged(conjugate, uppergamma(x, -oo))
  188. assert uppergamma(x, y).rewrite(expint) == y**x*expint(-x + 1, y)
  189. assert uppergamma(x, y).rewrite(lowergamma) == gamma(x) - lowergamma(x, y)
  190. assert uppergamma(70, 6) == 69035724522603011058660187038367026272747334489677105069435923032634389419656200387949342530805432320*exp(-6)
  191. assert (uppergamma(S(77) / 2, 6) - uppergamma(S(77) / 2, 6, evaluate=False)).evalf() < 1e-16
  192. assert (uppergamma(-S(77) / 2, 6) - uppergamma(-S(77) / 2, 6, evaluate=False)).evalf() < 1e-16
  193. def test_polygamma():
  194. assert polygamma(n, nan) is nan
  195. assert polygamma(0, oo) is oo
  196. assert polygamma(0, -oo) is oo
  197. assert polygamma(0, I*oo) is oo
  198. assert polygamma(0, -I*oo) is oo
  199. assert polygamma(1, oo) == 0
  200. assert polygamma(5, oo) == 0
  201. assert polygamma(0, -9) is zoo
  202. assert polygamma(0, -9) is zoo
  203. assert polygamma(0, -1) is zoo
  204. assert polygamma(0, 0) is zoo
  205. assert polygamma(0, 1) == -EulerGamma
  206. assert polygamma(0, 7) == Rational(49, 20) - EulerGamma
  207. assert polygamma(1, 1) == pi**2/6
  208. assert polygamma(1, 2) == pi**2/6 - 1
  209. assert polygamma(1, 3) == pi**2/6 - Rational(5, 4)
  210. assert polygamma(3, 1) == pi**4 / 15
  211. assert polygamma(3, 5) == 6*(Rational(-22369, 20736) + pi**4/90)
  212. assert polygamma(5, 1) == 8 * pi**6 / 63
  213. assert polygamma(1, S.Half) == pi**2 / 2
  214. assert polygamma(2, S.Half) == -14*zeta(3)
  215. assert polygamma(11, S.Half) == 176896*pi**12
  216. def t(m, n):
  217. x = S(m)/n
  218. r = polygamma(0, x)
  219. if r.has(polygamma):
  220. return False
  221. return abs(polygamma(0, x.n()).n() - r.n()).n() < 1e-10
  222. assert t(1, 2)
  223. assert t(3, 2)
  224. assert t(-1, 2)
  225. assert t(1, 4)
  226. assert t(-3, 4)
  227. assert t(1, 3)
  228. assert t(4, 3)
  229. assert t(3, 4)
  230. assert t(2, 3)
  231. assert t(123, 5)
  232. assert polygamma(0, x).rewrite(zeta) == polygamma(0, x)
  233. assert polygamma(1, x).rewrite(zeta) == zeta(2, x)
  234. assert polygamma(2, x).rewrite(zeta) == -2*zeta(3, x)
  235. assert polygamma(I, 2).rewrite(zeta) == polygamma(I, 2)
  236. n1 = Symbol('n1')
  237. n2 = Symbol('n2', real=True)
  238. n3 = Symbol('n3', integer=True)
  239. n4 = Symbol('n4', positive=True)
  240. n5 = Symbol('n5', positive=True, integer=True)
  241. assert polygamma(n1, x).rewrite(zeta) == polygamma(n1, x)
  242. assert polygamma(n2, x).rewrite(zeta) == polygamma(n2, x)
  243. assert polygamma(n3, x).rewrite(zeta) == polygamma(n3, x)
  244. assert polygamma(n4, x).rewrite(zeta) == polygamma(n4, x)
  245. assert polygamma(n5, x).rewrite(zeta) == (-1)**(n5 + 1) * factorial(n5) * zeta(n5 + 1, x)
  246. assert polygamma(3, 7*x).diff(x) == 7*polygamma(4, 7*x)
  247. assert polygamma(0, x).rewrite(harmonic) == harmonic(x - 1) - EulerGamma
  248. assert polygamma(2, x).rewrite(harmonic) == 2*harmonic(x - 1, 3) - 2*zeta(3)
  249. ni = Symbol("n", integer=True)
  250. assert polygamma(ni, x).rewrite(harmonic) == (-1)**(ni + 1)*(-harmonic(x - 1, ni + 1)
  251. + zeta(ni + 1))*factorial(ni)
  252. # Polygamma of non-negative integer order is unbranched:
  253. k = Symbol('n', integer=True, nonnegative=True)
  254. assert polygamma(k, exp_polar(2*I*pi)*x) == polygamma(k, x)
  255. # but negative integers are branched!
  256. k = Symbol('n', integer=True)
  257. assert polygamma(k, exp_polar(2*I*pi)*x).args == (k, exp_polar(2*I*pi)*x)
  258. # Polygamma of order -1 is loggamma:
  259. assert polygamma(-1, x) == loggamma(x)
  260. # But smaller orders are iterated integrals and don't have a special name
  261. assert polygamma(-2, x).func is polygamma
  262. # Test a bug
  263. assert polygamma(0, -x).expand(func=True) == polygamma(0, -x)
  264. assert polygamma(2, 2.5).is_positive == False
  265. assert polygamma(2, -2.5).is_positive == False
  266. assert polygamma(3, 2.5).is_positive == True
  267. assert polygamma(3, -2.5).is_positive is True
  268. assert polygamma(-2, -2.5).is_positive is None
  269. assert polygamma(-3, -2.5).is_positive is None
  270. assert polygamma(2, 2.5).is_negative == True
  271. assert polygamma(3, 2.5).is_negative == False
  272. assert polygamma(3, -2.5).is_negative == False
  273. assert polygamma(2, -2.5).is_negative is True
  274. assert polygamma(-2, -2.5).is_negative is None
  275. assert polygamma(-3, -2.5).is_negative is None
  276. assert polygamma(I, 2).is_positive is None
  277. assert polygamma(I, 3).is_negative is None
  278. # issue 17350
  279. assert polygamma(pi, 3).evalf() == polygamma(pi, 3)
  280. assert (I*polygamma(I, pi)).as_real_imag() == \
  281. (-im(polygamma(I, pi)), re(polygamma(I, pi)))
  282. assert (tanh(polygamma(I, 1))).rewrite(exp) == \
  283. (exp(polygamma(I, 1)) - exp(-polygamma(I, 1)))/(exp(polygamma(I, 1)) + exp(-polygamma(I, 1)))
  284. assert (I / polygamma(I, 4)).rewrite(exp) == \
  285. I*sqrt(re(polygamma(I, 4))**2 + im(polygamma(I, 4))**2)\
  286. /((re(polygamma(I, 4)) + I*im(polygamma(I, 4)))*Abs(polygamma(I, 4)))
  287. assert unchanged(polygamma, 2.3, 1.0)
  288. # issue 12569
  289. assert unchanged(im, polygamma(0, I))
  290. assert polygamma(Symbol('a', positive=True), Symbol('b', positive=True)).is_real is True
  291. assert polygamma(0, I).is_real is None
  292. def test_polygamma_expand_func():
  293. assert polygamma(0, x).expand(func=True) == polygamma(0, x)
  294. assert polygamma(0, 2*x).expand(func=True) == \
  295. polygamma(0, x)/2 + polygamma(0, S.Half + x)/2 + log(2)
  296. assert polygamma(1, 2*x).expand(func=True) == \
  297. polygamma(1, x)/4 + polygamma(1, S.Half + x)/4
  298. assert polygamma(2, x).expand(func=True) == \
  299. polygamma(2, x)
  300. assert polygamma(0, -1 + x).expand(func=True) == \
  301. polygamma(0, x) - 1/(x - 1)
  302. assert polygamma(0, 1 + x).expand(func=True) == \
  303. 1/x + polygamma(0, x )
  304. assert polygamma(0, 2 + x).expand(func=True) == \
  305. 1/x + 1/(1 + x) + polygamma(0, x)
  306. assert polygamma(0, 3 + x).expand(func=True) == \
  307. polygamma(0, x) + 1/x + 1/(1 + x) + 1/(2 + x)
  308. assert polygamma(0, 4 + x).expand(func=True) == \
  309. polygamma(0, x) + 1/x + 1/(1 + x) + 1/(2 + x) + 1/(3 + x)
  310. assert polygamma(1, 1 + x).expand(func=True) == \
  311. polygamma(1, x) - 1/x**2
  312. assert polygamma(1, 2 + x).expand(func=True, multinomial=False) == \
  313. polygamma(1, x) - 1/x**2 - 1/(1 + x)**2
  314. assert polygamma(1, 3 + x).expand(func=True, multinomial=False) == \
  315. polygamma(1, x) - 1/x**2 - 1/(1 + x)**2 - 1/(2 + x)**2
  316. assert polygamma(1, 4 + x).expand(func=True, multinomial=False) == \
  317. polygamma(1, x) - 1/x**2 - 1/(1 + x)**2 - \
  318. 1/(2 + x)**2 - 1/(3 + x)**2
  319. assert polygamma(0, x + y).expand(func=True) == \
  320. polygamma(0, x + y)
  321. assert polygamma(1, x + y).expand(func=True) == \
  322. polygamma(1, x + y)
  323. assert polygamma(1, 3 + 4*x + y).expand(func=True, multinomial=False) == \
  324. polygamma(1, y + 4*x) - 1/(y + 4*x)**2 - \
  325. 1/(1 + y + 4*x)**2 - 1/(2 + y + 4*x)**2
  326. assert polygamma(3, 3 + 4*x + y).expand(func=True, multinomial=False) == \
  327. polygamma(3, y + 4*x) - 6/(y + 4*x)**4 - \
  328. 6/(1 + y + 4*x)**4 - 6/(2 + y + 4*x)**4
  329. assert polygamma(3, 4*x + y + 1).expand(func=True, multinomial=False) == \
  330. polygamma(3, y + 4*x) - 6/(y + 4*x)**4
  331. e = polygamma(3, 4*x + y + Rational(3, 2))
  332. assert e.expand(func=True) == e
  333. e = polygamma(3, x + y + Rational(3, 4))
  334. assert e.expand(func=True, basic=False) == e
  335. def test_digamma():
  336. assert digamma(nan) == nan
  337. assert digamma(oo) == oo
  338. assert digamma(-oo) == oo
  339. assert digamma(I*oo) == oo
  340. assert digamma(-I*oo) == oo
  341. assert digamma(-9) == zoo
  342. assert digamma(-9) == zoo
  343. assert digamma(-1) == zoo
  344. assert digamma(0) == zoo
  345. assert digamma(1) == -EulerGamma
  346. assert digamma(7) == Rational(49, 20) - EulerGamma
  347. def t(m, n):
  348. x = S(m)/n
  349. r = digamma(x)
  350. if r.has(digamma):
  351. return False
  352. return abs(digamma(x.n()).n() - r.n()).n() < 1e-10
  353. assert t(1, 2)
  354. assert t(3, 2)
  355. assert t(-1, 2)
  356. assert t(1, 4)
  357. assert t(-3, 4)
  358. assert t(1, 3)
  359. assert t(4, 3)
  360. assert t(3, 4)
  361. assert t(2, 3)
  362. assert t(123, 5)
  363. assert digamma(x).rewrite(zeta) == polygamma(0, x)
  364. assert digamma(x).rewrite(harmonic) == harmonic(x - 1) - EulerGamma
  365. assert digamma(I).is_real is None
  366. assert digamma(x,evaluate=False).fdiff() == polygamma(1, x)
  367. assert digamma(x,evaluate=False).is_real is None
  368. assert digamma(x,evaluate=False).is_positive is None
  369. assert digamma(x,evaluate=False).is_negative is None
  370. assert digamma(x,evaluate=False).rewrite(polygamma) == polygamma(0, x)
  371. def test_digamma_expand_func():
  372. assert digamma(x).expand(func=True) == polygamma(0, x)
  373. assert digamma(2*x).expand(func=True) == \
  374. polygamma(0, x)/2 + polygamma(0, Rational(1, 2) + x)/2 + log(2)
  375. assert digamma(-1 + x).expand(func=True) == \
  376. polygamma(0, x) - 1/(x - 1)
  377. assert digamma(1 + x).expand(func=True) == \
  378. 1/x + polygamma(0, x )
  379. assert digamma(2 + x).expand(func=True) == \
  380. 1/x + 1/(1 + x) + polygamma(0, x)
  381. assert digamma(3 + x).expand(func=True) == \
  382. polygamma(0, x) + 1/x + 1/(1 + x) + 1/(2 + x)
  383. assert digamma(4 + x).expand(func=True) == \
  384. polygamma(0, x) + 1/x + 1/(1 + x) + 1/(2 + x) + 1/(3 + x)
  385. assert digamma(x + y).expand(func=True) == \
  386. polygamma(0, x + y)
  387. def test_trigamma():
  388. assert trigamma(nan) == nan
  389. assert trigamma(oo) == 0
  390. assert trigamma(1) == pi**2/6
  391. assert trigamma(2) == pi**2/6 - 1
  392. assert trigamma(3) == pi**2/6 - Rational(5, 4)
  393. assert trigamma(x, evaluate=False).rewrite(zeta) == zeta(2, x)
  394. assert trigamma(x, evaluate=False).rewrite(harmonic) == \
  395. trigamma(x).rewrite(polygamma).rewrite(harmonic)
  396. assert trigamma(x,evaluate=False).fdiff() == polygamma(2, x)
  397. assert trigamma(x,evaluate=False).is_real is None
  398. assert trigamma(x,evaluate=False).is_positive is None
  399. assert trigamma(x,evaluate=False).is_negative is None
  400. assert trigamma(x,evaluate=False).rewrite(polygamma) == polygamma(1, x)
  401. def test_trigamma_expand_func():
  402. assert trigamma(2*x).expand(func=True) == \
  403. polygamma(1, x)/4 + polygamma(1, Rational(1, 2) + x)/4
  404. assert trigamma(1 + x).expand(func=True) == \
  405. polygamma(1, x) - 1/x**2
  406. assert trigamma(2 + x).expand(func=True, multinomial=False) == \
  407. polygamma(1, x) - 1/x**2 - 1/(1 + x)**2
  408. assert trigamma(3 + x).expand(func=True, multinomial=False) == \
  409. polygamma(1, x) - 1/x**2 - 1/(1 + x)**2 - 1/(2 + x)**2
  410. assert trigamma(4 + x).expand(func=True, multinomial=False) == \
  411. polygamma(1, x) - 1/x**2 - 1/(1 + x)**2 - \
  412. 1/(2 + x)**2 - 1/(3 + x)**2
  413. assert trigamma(x + y).expand(func=True) == \
  414. polygamma(1, x + y)
  415. assert trigamma(3 + 4*x + y).expand(func=True, multinomial=False) == \
  416. polygamma(1, y + 4*x) - 1/(y + 4*x)**2 - \
  417. 1/(1 + y + 4*x)**2 - 1/(2 + y + 4*x)**2
  418. def test_loggamma():
  419. raises(TypeError, lambda: loggamma(2, 3))
  420. raises(ArgumentIndexError, lambda: loggamma(x).fdiff(2))
  421. assert loggamma(-1) is oo
  422. assert loggamma(-2) is oo
  423. assert loggamma(0) is oo
  424. assert loggamma(1) == 0
  425. assert loggamma(2) == 0
  426. assert loggamma(3) == log(2)
  427. assert loggamma(4) == log(6)
  428. n = Symbol("n", integer=True, positive=True)
  429. assert loggamma(n) == log(gamma(n))
  430. assert loggamma(-n) is oo
  431. assert loggamma(n/2) == log(2**(-n + 1)*sqrt(pi)*gamma(n)/gamma(n/2 + S.Half))
  432. assert loggamma(oo) is oo
  433. assert loggamma(-oo) is zoo
  434. assert loggamma(I*oo) is zoo
  435. assert loggamma(-I*oo) is zoo
  436. assert loggamma(zoo) is zoo
  437. assert loggamma(nan) is nan
  438. L = loggamma(Rational(16, 3))
  439. E = -5*log(3) + loggamma(Rational(1, 3)) + log(4) + log(7) + log(10) + log(13)
  440. assert expand_func(L).doit() == E
  441. assert L.n() == E.n()
  442. L = loggamma(Rational(19, 4))
  443. E = -4*log(4) + loggamma(Rational(3, 4)) + log(3) + log(7) + log(11) + log(15)
  444. assert expand_func(L).doit() == E
  445. assert L.n() == E.n()
  446. L = loggamma(Rational(23, 7))
  447. E = -3*log(7) + log(2) + loggamma(Rational(2, 7)) + log(9) + log(16)
  448. assert expand_func(L).doit() == E
  449. assert L.n() == E.n()
  450. L = loggamma(Rational(19, 4) - 7)
  451. E = -log(9) - log(5) + loggamma(Rational(3, 4)) + 3*log(4) - 3*I*pi
  452. assert expand_func(L).doit() == E
  453. assert L.n() == E.n()
  454. L = loggamma(Rational(23, 7) - 6)
  455. E = -log(19) - log(12) - log(5) + loggamma(Rational(2, 7)) + 3*log(7) - 3*I*pi
  456. assert expand_func(L).doit() == E
  457. assert L.n() == E.n()
  458. assert loggamma(x).diff(x) == polygamma(0, x)
  459. s1 = loggamma(1/(x + sin(x)) + cos(x)).nseries(x, n=4)
  460. s2 = (-log(2*x) - 1)/(2*x) - log(x/pi)/2 + (4 - log(2*x))*x/24 + O(x**2) + \
  461. log(x)*x**2/2
  462. assert (s1 - s2).expand(force=True).removeO() == 0
  463. s1 = loggamma(1/x).series(x)
  464. s2 = (1/x - S.Half)*log(1/x) - 1/x + log(2*pi)/2 + \
  465. x/12 - x**3/360 + x**5/1260 + O(x**7)
  466. assert ((s1 - s2).expand(force=True)).removeO() == 0
  467. assert loggamma(x).rewrite('intractable') == log(gamma(x))
  468. s1 = loggamma(x).series(x).cancel()
  469. assert s1 == -log(x) - EulerGamma*x + pi**2*x**2/12 + x**3*polygamma(2, 1)/6 + \
  470. pi**4*x**4/360 + x**5*polygamma(4, 1)/120 + O(x**6)
  471. assert s1 == loggamma(x).rewrite('intractable').series(x).cancel()
  472. assert conjugate(loggamma(x)) == loggamma(conjugate(x))
  473. assert conjugate(loggamma(0)) is oo
  474. assert conjugate(loggamma(1)) == loggamma(conjugate(1))
  475. assert conjugate(loggamma(-oo)) == conjugate(zoo)
  476. assert loggamma(Symbol('v', positive=True)).is_real is True
  477. assert loggamma(Symbol('v', zero=True)).is_real is False
  478. assert loggamma(Symbol('v', negative=True)).is_real is False
  479. assert loggamma(Symbol('v', nonpositive=True)).is_real is False
  480. assert loggamma(Symbol('v', nonnegative=True)).is_real is None
  481. assert loggamma(Symbol('v', imaginary=True)).is_real is None
  482. assert loggamma(Symbol('v', real=True)).is_real is None
  483. assert loggamma(Symbol('v')).is_real is None
  484. assert loggamma(S.Half).is_real is True
  485. assert loggamma(0).is_real is False
  486. assert loggamma(Rational(-1, 2)).is_real is False
  487. assert loggamma(I).is_real is None
  488. assert loggamma(2 + 3*I).is_real is None
  489. def tN(N, M):
  490. assert loggamma(1/x)._eval_nseries(x, n=N).getn() == M
  491. tN(0, 0)
  492. tN(1, 1)
  493. tN(2, 2)
  494. tN(3, 3)
  495. tN(4, 4)
  496. tN(5, 5)
  497. def test_polygamma_expansion():
  498. # A. & S., pa. 259 and 260
  499. assert polygamma(0, 1/x).nseries(x, n=3) == \
  500. -log(x) - x/2 - x**2/12 + O(x**3)
  501. assert polygamma(1, 1/x).series(x, n=5) == \
  502. x + x**2/2 + x**3/6 + O(x**5)
  503. assert polygamma(3, 1/x).nseries(x, n=11) == \
  504. 2*x**3 + 3*x**4 + 2*x**5 - x**7 + 4*x**9/3 + O(x**11)
  505. def test_issue_8657():
  506. n = Symbol('n', negative=True, integer=True)
  507. m = Symbol('m', integer=True)
  508. o = Symbol('o', positive=True)
  509. p = Symbol('p', negative=True, integer=False)
  510. assert gamma(n).is_real is False
  511. assert gamma(m).is_real is None
  512. assert gamma(o).is_real is True
  513. assert gamma(p).is_real is True
  514. assert gamma(w).is_real is None
  515. def test_issue_8524():
  516. x = Symbol('x', positive=True)
  517. y = Symbol('y', negative=True)
  518. z = Symbol('z', positive=False)
  519. p = Symbol('p', negative=False)
  520. q = Symbol('q', integer=True)
  521. r = Symbol('r', integer=False)
  522. e = Symbol('e', even=True, negative=True)
  523. assert gamma(x).is_positive is True
  524. assert gamma(y).is_positive is None
  525. assert gamma(z).is_positive is None
  526. assert gamma(p).is_positive is None
  527. assert gamma(q).is_positive is None
  528. assert gamma(r).is_positive is None
  529. assert gamma(e + S.Half).is_positive is True
  530. assert gamma(e - S.Half).is_positive is False
  531. def test_issue_14450():
  532. assert uppergamma(Rational(3, 8), x).evalf() == uppergamma(Rational(3, 8), x)
  533. assert lowergamma(x, Rational(3, 8)).evalf() == lowergamma(x, Rational(3, 8))
  534. # some values from Wolfram Alpha for comparison
  535. assert abs(uppergamma(Rational(3, 8), 2).evalf() - 0.07105675881) < 1e-9
  536. assert abs(lowergamma(Rational(3, 8), 2).evalf() - 2.2993794256) < 1e-9
  537. def test_issue_14528():
  538. k = Symbol('k', integer=True, nonpositive=True)
  539. assert isinstance(gamma(k), gamma)
  540. def test_multigamma():
  541. from sympy.concrete.products import Product
  542. p = Symbol('p')
  543. _k = Dummy('_k')
  544. assert multigamma(x, p).dummy_eq(pi**(p*(p - 1)/4)*\
  545. Product(gamma(x + (1 - _k)/2), (_k, 1, p)))
  546. assert conjugate(multigamma(x, p)).dummy_eq(pi**((conjugate(p) - 1)*\
  547. conjugate(p)/4)*Product(gamma(conjugate(x) + (1-conjugate(_k))/2), (_k, 1, p)))
  548. assert conjugate(multigamma(x, 1)) == gamma(conjugate(x))
  549. p = Symbol('p', positive=True)
  550. assert conjugate(multigamma(x, p)).dummy_eq(pi**((p - 1)*p/4)*\
  551. Product(gamma(conjugate(x) + (1-conjugate(_k))/2), (_k, 1, p)))
  552. assert multigamma(nan, 1) is nan
  553. assert multigamma(oo, 1).doit() is oo
  554. assert multigamma(1, 1) == 1
  555. assert multigamma(2, 1) == 1
  556. assert multigamma(3, 1) == 2
  557. assert multigamma(102, 1) == factorial(101)
  558. assert multigamma(S.Half, 1) == sqrt(pi)
  559. assert multigamma(1, 2) == pi
  560. assert multigamma(2, 2) == pi/2
  561. assert multigamma(1, 3) is zoo
  562. assert multigamma(2, 3) == pi**2/2
  563. assert multigamma(3, 3) == 3*pi**2/2
  564. assert multigamma(x, 1).diff(x) == gamma(x)*polygamma(0, x)
  565. assert multigamma(x, 2).diff(x) == sqrt(pi)*gamma(x)*gamma(x - S.Half)*\
  566. polygamma(0, x) + sqrt(pi)*gamma(x)*gamma(x - S.Half)*polygamma(0, x - S.Half)
  567. assert multigamma(x - 1, 1).expand(func=True) == gamma(x)/(x - 1)
  568. assert multigamma(x + 2, 1).expand(func=True, mul=False) == x*(x + 1)*\
  569. gamma(x)
  570. assert multigamma(x - 1, 2).expand(func=True) == sqrt(pi)*gamma(x)*\
  571. gamma(x + S.Half)/(x**3 - 3*x**2 + x*Rational(11, 4) - Rational(3, 4))
  572. assert multigamma(x - 1, 3).expand(func=True) == pi**Rational(3, 2)*gamma(x)**2*\
  573. gamma(x + S.Half)/(x**5 - 6*x**4 + 55*x**3/4 - 15*x**2 + x*Rational(31, 4) - Rational(3, 2))
  574. assert multigamma(n, 1).rewrite(factorial) == factorial(n - 1)
  575. assert multigamma(n, 2).rewrite(factorial) == sqrt(pi)*\
  576. factorial(n - Rational(3, 2))*factorial(n - 1)
  577. assert multigamma(n, 3).rewrite(factorial) == pi**Rational(3, 2)*\
  578. factorial(n - 2)*factorial(n - Rational(3, 2))*factorial(n - 1)
  579. assert multigamma(Rational(-1, 2), 3, evaluate=False).is_real == False
  580. assert multigamma(S.Half, 3, evaluate=False).is_real == False
  581. assert multigamma(0, 1, evaluate=False).is_real == False
  582. assert multigamma(1, 3, evaluate=False).is_real == False
  583. assert multigamma(-1.0, 3, evaluate=False).is_real == False
  584. assert multigamma(0.7, 3, evaluate=False).is_real == True
  585. assert multigamma(3, 3, evaluate=False).is_real == True
  586. def test_gamma_as_leading_term():
  587. assert gamma(x).as_leading_term(x) == 1/x
  588. assert gamma(2 + x).as_leading_term(x) == S(1)
  589. assert gamma(cos(x)).as_leading_term(x) == S(1)
  590. assert gamma(sin(x)).as_leading_term(x) == 1/x