test_gammasimp.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. from sympy.core.function import Function
  2. from sympy.core.numbers import (Rational, pi)
  3. from sympy.core.singleton import S
  4. from sympy.core.symbol import symbols
  5. from sympy.functions.combinatorial.factorials import (rf, binomial, factorial)
  6. from sympy.functions.elementary.exponential import exp
  7. from sympy.functions.elementary.miscellaneous import sqrt
  8. from sympy.functions.elementary.piecewise import Piecewise
  9. from sympy.functions.elementary.trigonometric import (cos, sin)
  10. from sympy.functions.special.gamma_functions import gamma
  11. from sympy.simplify.gammasimp import gammasimp
  12. from sympy.simplify.powsimp import powsimp
  13. from sympy.simplify.simplify import simplify
  14. from sympy.abc import x, y, n, k
  15. def test_gammasimp():
  16. R = Rational
  17. # was part of test_combsimp_gamma() in test_combsimp.py
  18. assert gammasimp(gamma(x)) == gamma(x)
  19. assert gammasimp(gamma(x + 1)/x) == gamma(x)
  20. assert gammasimp(gamma(x)/(x - 1)) == gamma(x - 1)
  21. assert gammasimp(x*gamma(x)) == gamma(x + 1)
  22. assert gammasimp((x + 1)*gamma(x + 1)) == gamma(x + 2)
  23. assert gammasimp(gamma(x + y)*(x + y)) == gamma(x + y + 1)
  24. assert gammasimp(x/gamma(x + 1)) == 1/gamma(x)
  25. assert gammasimp((x + 1)**2/gamma(x + 2)) == (x + 1)/gamma(x + 1)
  26. assert gammasimp(x*gamma(x) + gamma(x + 3)/(x + 2)) == \
  27. (x + 2)*gamma(x + 1)
  28. assert gammasimp(gamma(2*x)*x) == gamma(2*x + 1)/2
  29. assert gammasimp(gamma(2*x)/(x - S.Half)) == 2*gamma(2*x - 1)
  30. assert gammasimp(gamma(x)*gamma(1 - x)) == pi/sin(pi*x)
  31. assert gammasimp(gamma(x)*gamma(-x)) == -pi/(x*sin(pi*x))
  32. assert gammasimp(1/gamma(x + 3)/gamma(1 - x)) == \
  33. sin(pi*x)/(pi*x*(x + 1)*(x + 2))
  34. assert gammasimp(factorial(n + 2)) == gamma(n + 3)
  35. assert gammasimp(binomial(n, k)) == \
  36. gamma(n + 1)/(gamma(k + 1)*gamma(-k + n + 1))
  37. assert powsimp(gammasimp(
  38. gamma(x)*gamma(x + S.Half)*gamma(y)/gamma(x + y))) == \
  39. 2**(-2*x + 1)*sqrt(pi)*gamma(2*x)*gamma(y)/gamma(x + y)
  40. assert gammasimp(1/gamma(x)/gamma(x - Rational(1, 3))/gamma(x + Rational(1, 3))) == \
  41. 3**(3*x - Rational(3, 2))/(2*pi*gamma(3*x - 1))
  42. assert simplify(
  43. gamma(S.Half + x/2)*gamma(1 + x/2)/gamma(1 + x)/sqrt(pi)*2**x) == 1
  44. assert gammasimp(gamma(Rational(-1, 4))*gamma(Rational(-3, 4))) == 16*sqrt(2)*pi/3
  45. assert powsimp(gammasimp(gamma(2*x)/gamma(x))) == \
  46. 2**(2*x - 1)*gamma(x + S.Half)/sqrt(pi)
  47. # issue 6792
  48. e = (-gamma(k)*gamma(k + 2) + gamma(k + 1)**2)/gamma(k)**2
  49. assert gammasimp(e) == -k
  50. assert gammasimp(1/e) == -1/k
  51. e = (gamma(x) + gamma(x + 1))/gamma(x)
  52. assert gammasimp(e) == x + 1
  53. assert gammasimp(1/e) == 1/(x + 1)
  54. e = (gamma(x) + gamma(x + 2))*(gamma(x - 1) + gamma(x))/gamma(x)
  55. assert gammasimp(e) == (x**2 + x + 1)*gamma(x + 1)/(x - 1)
  56. e = (-gamma(k)*gamma(k + 2) + gamma(k + 1)**2)/gamma(k)**2
  57. assert gammasimp(e**2) == k**2
  58. assert gammasimp(e**2/gamma(k + 1)) == k/gamma(k)
  59. a = R(1, 2) + R(1, 3)
  60. b = a + R(1, 3)
  61. assert gammasimp(gamma(2*k)/gamma(k)*gamma(k + a)*gamma(k + b)
  62. ) == 3*2**(2*k + 1)*3**(-3*k - 2)*sqrt(pi)*gamma(3*k + R(3, 2))/2
  63. # issue 9699
  64. assert gammasimp((x + 1)*factorial(x)/gamma(y)) == gamma(x + 2)/gamma(y)
  65. assert gammasimp(rf(x + n, k)*binomial(n, k)).simplify() == Piecewise(
  66. (gamma(n + 1)*gamma(k + n + x)/(gamma(k + 1)*gamma(n + x)*gamma(-k + n + 1)), n > -x),
  67. ((-1)**k*gamma(n + 1)*gamma(-n - x + 1)/(gamma(k + 1)*gamma(-k + n + 1)*gamma(-k - n - x + 1)), True))
  68. A, B = symbols('A B', commutative=False)
  69. assert gammasimp(e*B*A) == gammasimp(e)*B*A
  70. # check iteration
  71. assert gammasimp(gamma(2*k)/gamma(k)*gamma(-k - R(1, 2))) == (
  72. -2**(2*k + 1)*sqrt(pi)/(2*((2*k + 1)*cos(pi*k))))
  73. assert gammasimp(
  74. gamma(k)*gamma(k + R(1, 3))*gamma(k + R(2, 3))/gamma(k*R(3, 2))) == (
  75. 3*2**(3*k + 1)*3**(-3*k - S.Half)*sqrt(pi)*gamma(k*R(3, 2) + S.Half)/2)
  76. # issue 6153
  77. assert gammasimp(gamma(Rational(1, 4))/gamma(Rational(5, 4))) == 4
  78. # was part of test_combsimp() in test_combsimp.py
  79. assert gammasimp(binomial(n + 2, k + S.Half)) == gamma(n + 3)/ \
  80. (gamma(k + R(3, 2))*gamma(-k + n + R(5, 2)))
  81. assert gammasimp(binomial(n + 2, k + 2.0)) == \
  82. gamma(n + 3)/(gamma(k + 3.0)*gamma(-k + n + 1))
  83. # issue 11548
  84. assert gammasimp(binomial(0, x)) == sin(pi*x)/(pi*x)
  85. e = gamma(n + Rational(1, 3))*gamma(n + R(2, 3))
  86. assert gammasimp(e) == e
  87. assert gammasimp(gamma(4*n + S.Half)/gamma(2*n - R(3, 4))) == \
  88. 2**(4*n - R(5, 2))*(8*n - 3)*gamma(2*n + R(3, 4))/sqrt(pi)
  89. i, m = symbols('i m', integer = True)
  90. e = gamma(exp(i))
  91. assert gammasimp(e) == e
  92. e = gamma(m + 3)
  93. assert gammasimp(e) == e
  94. e = gamma(m + 1)/(gamma(i + 1)*gamma(-i + m + 1))
  95. assert gammasimp(e) == e
  96. p = symbols("p", integer=True, positive=True)
  97. assert gammasimp(gamma(-p + 4)) == gamma(-p + 4)
  98. def test_issue_22606():
  99. fx = Function('f')(x)
  100. eq = x + gamma(y)
  101. # seems like ans should be `eq`, not `(x*y + gamma(y + 1))/y`
  102. ans = gammasimp(eq)
  103. assert gammasimp(eq.subs(x, fx)).subs(fx, x) == ans
  104. assert gammasimp(eq.subs(x, cos(x))).subs(cos(x), x) == ans
  105. assert 1/gammasimp(1/eq) == ans
  106. assert gammasimp(fx.subs(x, eq)).args[0] == ans