test_beta_functions.py 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. from sympy.core.function import (diff, expand_func)
  2. from sympy.core.numbers import I, Rational, pi
  3. from sympy.core.singleton import S
  4. from sympy.core.symbol import (Dummy, symbols)
  5. from sympy.functions.combinatorial.numbers import catalan
  6. from sympy.functions.elementary.complexes import conjugate
  7. from sympy.functions.elementary.miscellaneous import sqrt
  8. from sympy.functions.special.beta_functions import (beta, betainc, betainc_regularized)
  9. from sympy.functions.special.gamma_functions import gamma, polygamma
  10. from sympy.functions.special.hyper import hyper
  11. from sympy.integrals.integrals import Integral
  12. from sympy.core.function import ArgumentIndexError
  13. from sympy.core.expr import unchanged
  14. from sympy.testing.pytest import raises
  15. def test_beta():
  16. x, y = symbols('x y')
  17. t = Dummy('t')
  18. assert unchanged(beta, x, y)
  19. assert unchanged(beta, x, x)
  20. assert beta(5, -3).is_real == True
  21. assert beta(3, y).is_real is None
  22. assert expand_func(beta(x, y)) == gamma(x)*gamma(y)/gamma(x + y)
  23. assert expand_func(beta(x, y) - beta(y, x)) == 0 # Symmetric
  24. assert expand_func(beta(x, y)) == expand_func(beta(x, y + 1) + beta(x + 1, y)).simplify()
  25. assert diff(beta(x, y), x) == beta(x, y)*(polygamma(0, x) - polygamma(0, x + y))
  26. assert diff(beta(x, y), y) == beta(x, y)*(polygamma(0, y) - polygamma(0, x + y))
  27. assert conjugate(beta(x, y)) == beta(conjugate(x), conjugate(y))
  28. raises(ArgumentIndexError, lambda: beta(x, y).fdiff(3))
  29. assert beta(x, y).rewrite(gamma) == gamma(x)*gamma(y)/gamma(x + y)
  30. assert beta(x).rewrite(gamma) == gamma(x)**2/gamma(2*x)
  31. assert beta(x, y).rewrite(Integral).dummy_eq(Integral(t**(x - 1) * (1 - t)**(y - 1), (t, 0, 1)))
  32. assert beta(Rational(-19, 10), Rational(-1, 10)) == S.Zero
  33. assert beta(Rational(-19, 10), Rational(-9, 10)) == \
  34. 800*2**(S(4)/5)*sqrt(pi)*gamma(S.One/10)/(171*gamma(-S(7)/5))
  35. assert beta(Rational(19, 10), Rational(29, 10)) == 100/(551*catalan(Rational(19, 10)))
  36. assert beta(1, 0) == S.ComplexInfinity
  37. assert beta(0, 1) == S.ComplexInfinity
  38. assert beta(2, 3) == S.One/12
  39. assert unchanged(beta, x, x + 1)
  40. assert unchanged(beta, x, 1)
  41. assert unchanged(beta, 1, y)
  42. assert beta(x, x + 1).doit() == 1/(x*(x+1)*catalan(x))
  43. assert beta(1, y).doit() == 1/y
  44. assert beta(x, 1).doit() == 1/x
  45. assert beta(Rational(-19, 10), Rational(-1, 10), evaluate=False).doit() == S.Zero
  46. assert beta(2) == beta(2, 2)
  47. assert beta(x, evaluate=False) != beta(x, x)
  48. assert beta(x, evaluate=False).doit() == beta(x, x)
  49. def test_betainc():
  50. a, b, x1, x2 = symbols('a b x1 x2')
  51. assert unchanged(betainc, a, b, x1, x2)
  52. assert unchanged(betainc, a, b, 0, x1)
  53. assert betainc(1, 2, 0, -5).is_real == True
  54. assert betainc(1, 2, 0, x2).is_real is None
  55. assert conjugate(betainc(I, 2, 3 - I, 1 + 4*I)) == betainc(-I, 2, 3 + I, 1 - 4*I)
  56. assert betainc(a, b, 0, 1).rewrite(Integral).dummy_eq(beta(a, b).rewrite(Integral))
  57. assert betainc(1, 2, 0, x2).rewrite(hyper) == x2*hyper((1, -1), (2,), x2)
  58. assert betainc(1, 2, 3, 3).evalf() == 0
  59. def test_betainc_regularized():
  60. a, b, x1, x2 = symbols('a b x1 x2')
  61. assert unchanged(betainc_regularized, a, b, x1, x2)
  62. assert unchanged(betainc_regularized, a, b, 0, x1)
  63. assert betainc_regularized(3, 5, 0, -1).is_real == True
  64. assert betainc_regularized(3, 5, 0, x2).is_real is None
  65. assert conjugate(betainc_regularized(3*I, 1, 2 + I, 1 + 2*I)) == betainc_regularized(-3*I, 1, 2 - I, 1 - 2*I)
  66. assert betainc_regularized(a, b, 0, 1).rewrite(Integral) == 1
  67. assert betainc_regularized(1, 2, x1, x2).rewrite(hyper) == 2*x2*hyper((1, -1), (2,), x2) - 2*x1*hyper((1, -1), (2,), x1)
  68. assert betainc_regularized(4, 1, 5, 5).evalf() == 0