def test_factorial_simplify_fail(): # simplify(factorial(x + 1).diff(x) - ((x + 1)*factorial(x)).diff(x))) == 0 from sympy.abc import x assert ( simplify(x * polygamma(0, x + 1) - x * polygamma(0, x + 2) + polygamma(0, x + 1) - polygamma(0, x + 2) + 1) == 0 )
def test_polygamma_expansion(): # A. & S., pa. 259 and 260 assert polygamma(0, 1 / x).nseries(x, n=3) == -log(x) - x / 2 - x ** 2 / 12 + O(x ** 4) assert polygamma(1, 1 / x).series(x, n=5) == x + x ** 2 / 2 + x ** 3 / 6 + O(x ** 5) assert polygamma(3, 1 / x).nseries(x, n=11) == 2 * x ** 3 + 3 * x ** 4 + 2 * x ** 5 - x ** 7 + 4 * x ** 9 / 3 + O( x ** 11 )
def test_loggamma(): raises(TypeError, lambda: loggamma(2, 3)) raises(ArgumentIndexError, lambda: loggamma(x).fdiff(2)) assert loggamma(x).diff(x) == polygamma(0, x) s1 = loggamma(1/(x + sin(x)) + cos(x)).nseries(x, n=4) s2 = (-log(2*x) - 1)/(2*x) - log(x/pi)/2 + (4 - log(2*x))*x/24 + O(x**2) + \ log(x)*x**2/2 assert (s1 - s2).expand(force=True).removeO() == 0 s1 = loggamma(1/x).series(x) s2 = (1/x - S(1)/2)*log(1/x) - 1/x + log(2*pi)/2 + \ x/12 - x**3/360 + x**5/1260 + O(x**7) assert ((s1 - s2).expand(force=True)).removeO() == 0 assert loggamma(x).rewrite('intractable') == log(gamma(x)) s1 = loggamma(x).series(x) assert s1 == -log(x) - EulerGamma*x + pi**2*x**2/12 + x**3*polygamma(2, 1)/6 + \ pi**4*x**4/360 + x**5*polygamma(4, 1)/120 + O(x**6) assert s1 == loggamma(x).rewrite('intractable').series(x) assert loggamma(x).is_real is None y, z = Symbol('y', real=True), Symbol('z', imaginary=True) assert loggamma(y).is_real assert loggamma(z).is_real is False def tN(N, M): assert loggamma(1/x)._eval_nseries(x, n=N, logx=None).getn() == M tN(0, 0) tN(1, 1) tN(2, 3) tN(3, 3) tN(4, 5) tN(5, 5)
def test_factorial_diff(): n = Symbol('n', integer=True) assert factorial(n).diff(n) == \ gamma(1 + n)*polygamma(0, 1 + n) assert factorial(n**2).diff(n) == \ 2*n*gamma(1 + n**2)*polygamma(0, 1 + n**2)
def fdiff(self, argindex=1): from sympy import polygamma if argindex == 1: # http://functions.wolfram.com/GammaBetaErf/Binomial/20/01/01/ n, k = self.args return binomial(n, k) * (polygamma(0, n + 1) - polygamma(0, n - k + 1)) elif argindex == 2: # http://functions.wolfram.com/GammaBetaErf/Binomial/20/01/02/ n, k = self.args return binomial(n, k) * (polygamma(0, n - k + 1) - polygamma(0, k + 1)) else: raise ArgumentIndexError(self, argindex)
def test_function__eval_nseries(): n = Symbol("n") assert sin(x)._eval_nseries(x, 2, None) == x + O(x ** 2) assert sin(x + 1)._eval_nseries(x, 2, None) == x * cos(1) + sin(1) + O(x ** 2) assert sin(pi * (1 - x))._eval_nseries(x, 2, None) == pi * x + O(x ** 2) assert acos(1 - x ** 2)._eval_nseries(x, 2, None) == sqrt(2) * x + O(x ** 2) assert polygamma(n, x + 1)._eval_nseries(x, 2, None) == polygamma(n, 1) + polygamma(n + 1, 1) * x + O(x ** 2) raises(PoleError, "sin(1/x)._eval_nseries(x,2,None)") raises(PoleError, "acos(1-x)._eval_nseries(x,2,None)") raises(PoleError, "acos(1+x)._eval_nseries(x,2,None)") assert loggamma(1 / x)._eval_nseries(x, 0, None) == log(x) / 2 - log(x) / x - 1 / x + O(1, x) assert loggamma(log(1 / x)).nseries(x, n=1, logx=y) == loggamma(-y)
def test_catalan(): assert catalan(1) == 1 assert catalan(2) == 2 assert catalan(3) == 5 assert catalan(4) == 14 assert catalan(x) == catalan(x) assert catalan(2*x).rewrite(binomial) == binomial(4*x, 2*x)/(2*x + 1) assert catalan(Rational(1,2)).rewrite(gamma) == 8/(3*pi) assert catalan(3*x).rewrite(gamma) == 4**(3*x)*gamma(3*x + Rational(1,2))/(sqrt(pi)*gamma(3*x + 2)) assert catalan(x).rewrite(hyper) == hyper((-x + 1, -x), (2,), 1) assert diff(catalan(x),x) == (polygamma(0, x + Rational(1,2)) - polygamma(0, x + 2) + 2*log(2))*catalan(x) c = catalan(0.5).evalf() assert str(c) == '0.848826363156775'
def test_gamma_series(): assert gamma(x + 1).series(x, 0, 3) == \ 1 - EulerGamma*x + x**2*(EulerGamma**2/2 + pi**2/12) + O(x**3) assert gamma(x).series(x, -1, 3) == \ -1/x + EulerGamma - 1 + x*(-1 - pi**2/12 - EulerGamma**2/2 + EulerGamma) \ + x**2*(-1 - pi**2/12 - EulerGamma**2/2 + EulerGamma**3/6 - polygamma(2, 1)/6 + EulerGamma*pi**2/12 + EulerGamma) + O(x**3)
def fdiff(self, argindex=1): from sympy import gamma, polygamma if argindex == 1: return gamma(self.args[0] + 1) * polygamma(0, self.args[0] + 1) else: raise ArgumentIndexError(self, argindex)
def test_lowergamma(): from sympy import meijerg, exp_polar, I, expint assert lowergamma(x, y).diff(y) == y**(x-1)*exp(-y) assert td(lowergamma(randcplx(), y), y) assert lowergamma(x, y).diff(x) == \ gamma(x)*polygamma(0, x) - uppergamma(x, y)*log(y) \ + meijerg([], [1, 1], [0, 0, x], [], y) assert lowergamma(S.Half, x) == sqrt(pi)*erf(sqrt(x)) assert not lowergamma(S.Half - 3, x).has(lowergamma) assert not lowergamma(S.Half + 3, x).has(lowergamma) assert lowergamma(S.Half, x, evaluate=False).has(lowergamma) assert tn(lowergamma(S.Half + 3, x, evaluate=False), lowergamma(S.Half + 3, x), x) assert tn(lowergamma(S.Half - 3, x, evaluate=False), lowergamma(S.Half - 3, x), x) assert lowergamma(x, y).rewrite(uppergamma) == gamma(x) - uppergamma(x, y) assert tn_branch(-3, lowergamma) assert tn_branch(-4, lowergamma) assert tn_branch(S(1)/3, lowergamma) assert tn_branch(pi, lowergamma) assert lowergamma(3, exp_polar(4*pi*I)*x) == lowergamma(3, x) assert lowergamma(y, exp_polar(5*pi*I)*x) == \ exp(4*I*pi*y)*lowergamma(y, x*exp_polar(pi*I)) assert lowergamma(-2, exp_polar(5*pi*I)*x) == \ lowergamma(-2, x*exp_polar(I*pi)) + 2*pi*I assert lowergamma(x, y).rewrite(expint) == -y**x*expint(-x + 1, y) + gamma(x) k = Symbol('k', integer=True) assert lowergamma(k, y).rewrite(expint) == -y**k*expint(-k + 1, y) + gamma(k) k = Symbol('k', integer=True, positive=False) assert lowergamma(k, y).rewrite(expint) == lowergamma(k, y)
def test_gamma_series(): assert gamma(x + 1).series(x, 0, 3) == \ 1 - EulerGamma*x + x**2*(EulerGamma**2/2 + pi**2/12) + O(x**3) assert gamma(x).series(x, -1, 3) == \ -1/(x + 1) + EulerGamma - 1 + (x + 1)*(-1 - pi**2/12 - EulerGamma**2/2 + \ EulerGamma) + (x + 1)**2*(-1 - pi**2/12 - EulerGamma**2/2 + EulerGamma**3/6 - \ polygamma(2, 1)/6 + EulerGamma*pi**2/12 + EulerGamma) + O((x + 1)**3, (x, -1))
def test_FunctionWrapper(): import sympy n, m, theta, phi = sympy.symbols("n, m, theta, phi") r = sympy.Ynm(n, m, theta, phi) s = Integer(2)*r assert isinstance(s, Mul) assert isinstance(s.args[1]._sympy_(), sympy.Ynm) x = symbols("x") e = x + sympy.loggamma(x) assert str(e) == "x + loggamma(x)" assert isinstance(e, Add) assert e + sympy.loggamma(x) == x + 2*sympy.loggamma(x) f = e.subs({x : 10}) assert f == 10 + log(362880) f = e.subs({x : 2}) assert f == 2 f = e.subs({x : 100}); v = f.n(53, real=True); assert abs(float(v) - 459.13420537) < 1e-7 f = e.diff(x) assert f == 1 + sympy.polygamma(0, x)
def test_gamma(): assert gamma(nan) == nan assert gamma(oo) == oo assert gamma(-100) == zoo assert gamma(0) == zoo assert gamma(1) == 1 assert gamma(2) == 1 assert gamma(3) == 2 assert gamma(102) == factorial(101) assert gamma(Rational(1,2)) == sqrt(pi) assert gamma(Rational(3, 2)) == Rational(1, 2)*sqrt(pi) assert gamma(Rational(5, 2)) == Rational(3, 4)*sqrt(pi) assert gamma(Rational(7, 2)) == Rational(15, 8)*sqrt(pi) assert gamma(Rational(-1, 2)) == -2*sqrt(pi) assert gamma(Rational(-3, 2)) == Rational(4, 3)*sqrt(pi) assert gamma(Rational(-5, 2)) == -Rational(8, 15)*sqrt(pi) assert gamma(Rational(-15, 2)) == Rational(256, 2027025)*sqrt(pi) assert gamma(x).diff(x) == gamma(x)*polygamma(0, x) assert gamma(x - 1).expand(func=True) == gamma(x)/(x-1) assert gamma(x + 2).expand(func=True, mul=False) == x*(x+1)*gamma(x) assert expand_func(gamma(x + Rational(3, 2))) ==\ (x + Rational(1, 2))*gamma(x + Rational(1, 2))
def test_gamma(): assert gamma(nan) == nan assert gamma(oo) == oo assert gamma(-100) == zoo assert gamma(0) == zoo assert gamma(1) == 1 assert gamma(2) == 1 assert gamma(3) == 2 assert gamma(102) == factorial(101) assert gamma(Rational(1, 2)) == sqrt(pi) assert gamma(Rational(3, 2)) == Rational(1, 2)*sqrt(pi) assert gamma(Rational(5, 2)) == Rational(3, 4)*sqrt(pi) assert gamma(Rational(7, 2)) == Rational(15, 8)*sqrt(pi) assert gamma(Rational(-1, 2)) == -2*sqrt(pi) assert gamma(Rational(-3, 2)) == Rational(4, 3)*sqrt(pi) assert gamma(Rational(-5, 2)) == -Rational(8, 15)*sqrt(pi) assert gamma(Rational(-15, 2)) == Rational(256, 2027025)*sqrt(pi) assert gamma(Rational( -11, 8)).expand(func=True) == Rational(64, 33)*gamma(Rational(5, 8)) assert gamma(Rational( -10, 3)).expand(func=True) == Rational(81, 280)*gamma(Rational(2, 3)) assert gamma(Rational( 14, 3)).expand(func=True) == Rational(880, 81)*gamma(Rational(2, 3)) assert gamma(Rational( 17, 7)).expand(func=True) == Rational(30, 49)*gamma(Rational(3, 7)) assert gamma(Rational( 19, 8)).expand(func=True) == Rational(33, 64)*gamma(Rational(3, 8)) assert gamma(x).diff(x) == gamma(x)*polygamma(0, x) assert gamma(x - 1).expand(func=True) == gamma(x)/(x - 1) assert gamma(x + 2).expand(func=True, mul=False) == x*(x + 1)*gamma(x) assert conjugate(gamma(x)) == gamma(conjugate(x)) assert expand_func(gamma(x + Rational(3, 2))) == \ (x + Rational(1, 2))*gamma(x + Rational(1, 2)) assert expand_func(gamma(x - Rational(1, 2))) == \ gamma(Rational(1, 2) + x)/(x - Rational(1, 2)) # Test a bug: assert expand_func(gamma(x + Rational(3, 4))) == gamma(x + Rational(3, 4)) assert gamma(3*exp_polar(I*pi)/4).is_nonnegative is False assert gamma(3*exp_polar(I*pi)/4).is_nonpositive is True # Issue 8526 k = Symbol('k', integer=True, nonnegative=True) assert isinstance(gamma(k), gamma) assert gamma(-k) == zoo
def test_gamma_series(): assert gamma(x + 1).series(x, 0, 3) == \ 1 - x*EulerGamma + x**2*EulerGamma**2/2 + pi**2*x**2/12 + O(x**3) assert gamma(x).series(x, -1, 3) == \ -1/x + EulerGamma - 1 + EulerGamma*x - EulerGamma**2*x/2 - pi**2*x/12 \ - x + EulerGamma*x**2 + EulerGamma*pi**2*x**2/12 - \ x**2*polygamma(2, 1)/6 + EulerGamma**3*x**2/6 - EulerGamma**2*x**2/2 \ - pi**2*x**2/12 - x**2 + O(x**3)
def test_evalf_default(): from sympy.functions.special.gamma_functions import polygamma assert type(sin(4.0)) == Real assert type(re(sin(I + 1.0))) == Real assert type(im(sin(I + 1.0))) == Real assert type(sin(4)) == sin assert type(polygamma(2,4.0)) == Real assert type(sin(Rational(1,4))) == sin
def test_function__eval_nseries(): n = Symbol("n") assert sin(x)._eval_nseries(x, 2, None) == x + O(x ** 2) assert sin(x + 1)._eval_nseries(x, 2, None) == x * cos(1) + sin(1) + O(x ** 2) assert sin(pi * (1 - x))._eval_nseries(x, 2, None) == pi * x + O(x ** 2) assert acos(1 - x ** 2)._eval_nseries(x, 2, None) == sqrt(2) * x + O(x ** 2) assert polygamma(n, x + 1)._eval_nseries(x, 2, None) == polygamma(n, 1) + polygamma(n + 1, 1) * x + O(x ** 2) raises(PoleError, lambda: sin(1 / x)._eval_nseries(x, 2, None)) raises(PoleError, lambda: acos(1 - x)._eval_nseries(x, 2, None)) raises(PoleError, lambda: acos(1 + x)._eval_nseries(x, 2, None)) assert loggamma(1 / x)._eval_nseries(x, 0, None) == log(x) / 2 - log(x) / x - 1 / x + O(1, x) assert loggamma(log(1 / x)).nseries(x, n=1, logx=y) == loggamma(-y) # issue 6725: assert expint(S(3) / 2, -x)._eval_nseries(x, 5, None) == 2 - 2 * sqrt(pi) * sqrt( -x ) - 2 * x - x ** 2 / 3 - x ** 3 / 15 - x ** 4 / 84 + O(x ** 5) assert sin(sqrt(x))._eval_nseries(x, 3, None) == sqrt(x) - x ** (S(3) / 2) / 6 + x ** (S(5) / 2) / 120 + O(x ** 3)
def test_binomial_diff(): n = Symbol('n', integer=True) k = Symbol('k', integer=True) assert binomial(n, k).diff(n) == \ (-polygamma(0, 1 + n - k) + polygamma(0, 1 + n))*binomial(n, k) assert binomial(n**2, k**3).diff(n) == \ 2*n*(-polygamma(0, 1 + n**2 - k**3) + polygamma(0, 1 + n**2))*binomial(n**2, k**3) assert binomial(n, k).diff(k) == \ (-polygamma(0, 1 + k) + polygamma(0, 1 + n - k))*binomial(n, k) assert binomial(n**2, k**3).diff(k) == \ 3*k**2*(-polygamma(0, 1 + k**3) + polygamma(0, 1 + n**2 - k**3))*binomial(n**2, k**3)
def test_lowergamma(): from sympy import meijerg assert lowergamma(x, y).diff(y) == y ** (x - 1) * exp(-y) assert td(lowergamma(randcplx(), y), y) assert lowergamma(x, y).diff(x) == gamma(x) * polygamma(0, x) - uppergamma(x, y) * log(y) + meijerg( [], [1, 1], [0, 0, x], [], y ) assert lowergamma(S.Half, x) == sqrt(pi) * erf(sqrt(x)) assert not lowergamma(S.Half - 3, x).has(lowergamma) assert not lowergamma(S.Half + 3, x).has(lowergamma) assert lowergamma(S.Half, x, evaluate=False).has(lowergamma) assert tn(lowergamma(S.Half + 3, x, evaluate=False), lowergamma(S.Half + 3, x), x) assert tn(lowergamma(S.Half - 3, x, evaluate=False), lowergamma(S.Half - 3, x), x)
def test_harmonic_rewrite_polygamma(): n = Symbol("n") m = Symbol("m") assert harmonic(n).rewrite(digamma) == polygamma(0, n + 1) + EulerGamma assert harmonic(n).rewrite(trigamma) == polygamma(0, n + 1) + EulerGamma assert harmonic(n).rewrite(polygamma) == polygamma(0, n + 1) + EulerGamma assert harmonic(n,3).rewrite(polygamma) == polygamma(2, n + 1)/2 - polygamma(2, 1)/2 assert harmonic(n,m).rewrite(polygamma) == (-1)**m*(polygamma(m - 1, 1) - polygamma(m - 1, n + 1))/factorial(m - 1) assert expand_func(harmonic(n+4)) == harmonic(n) + 1/(n + 4) + 1/(n + 3) + 1/(n + 2) + 1/(n + 1) assert expand_func(harmonic(n-4)) == harmonic(n) - 1/(n - 1) - 1/(n - 2) - 1/(n - 3) - 1/n assert harmonic(n, m).rewrite("tractable") == harmonic(n, m).rewrite(polygamma)
def test_lowergamma(): from sympy import meijerg, exp_polar, I, expint assert lowergamma(x, 0) == 0 assert lowergamma(x, y).diff(y) == y**(x - 1)*exp(-y) assert td(lowergamma(randcplx(), y), y) assert td(lowergamma(x, randcplx()), x) assert lowergamma(x, y).diff(x) == \ gamma(x)*polygamma(0, x) - uppergamma(x, y)*log(y) \ - meijerg([], [1, 1], [0, 0, x], [], y) assert lowergamma(S.Half, x) == sqrt(pi)*erf(sqrt(x)) assert not lowergamma(S.Half - 3, x).has(lowergamma) assert not lowergamma(S.Half + 3, x).has(lowergamma) assert lowergamma(S.Half, x, evaluate=False).has(lowergamma) assert tn(lowergamma(S.Half + 3, x, evaluate=False), lowergamma(S.Half + 3, x), x) assert tn(lowergamma(S.Half - 3, x, evaluate=False), lowergamma(S.Half - 3, x), x) assert tn_branch(-3, lowergamma) assert tn_branch(-4, lowergamma) assert tn_branch(S(1)/3, lowergamma) assert tn_branch(pi, lowergamma) assert lowergamma(3, exp_polar(4*pi*I)*x) == lowergamma(3, x) assert lowergamma(y, exp_polar(5*pi*I)*x) == \ exp(4*I*pi*y)*lowergamma(y, x*exp_polar(pi*I)) assert lowergamma(-2, exp_polar(5*pi*I)*x) == \ lowergamma(-2, x*exp_polar(I*pi)) + 2*pi*I assert conjugate(lowergamma(x, y)) == lowergamma(conjugate(x), conjugate(y)) assert conjugate(lowergamma(x, 0)) == conjugate(lowergamma(x, 0)) assert conjugate(lowergamma(x, -oo)) == conjugate(lowergamma(x, -oo)) assert lowergamma( x, y).rewrite(expint) == -y**x*expint(-x + 1, y) + gamma(x) k = Symbol('k', integer=True) assert lowergamma( k, y).rewrite(expint) == -y**k*expint(-k + 1, y) + gamma(k) k = Symbol('k', integer=True, positive=False) assert lowergamma(k, y).rewrite(expint) == lowergamma(k, y) assert lowergamma(x, y).rewrite(uppergamma) == gamma(x) - uppergamma(x, y) assert lowergamma(70, 6) == factorial(69) - 69035724522603011058660187038367026272747334489677105069435923032634389419656200387949342530805432320 * exp(-6) assert (lowergamma(S(77) / 2, 6) - lowergamma(S(77) / 2, 6, evaluate=False)).evalf() < 1e-16 assert (lowergamma(-S(77) / 2, 6) - lowergamma(-S(77) / 2, 6, evaluate=False)).evalf() < 1e-16
def test_gamma(): assert gamma(nan) == nan assert gamma(oo) == oo assert gamma(-100) == zoo assert gamma(0) == zoo assert gamma(1) == 1 assert gamma(2) == 1 assert gamma(3) == 2 assert gamma(102) == factorial(101) assert gamma(Rational(1,2)) == sqrt(pi) assert gamma(Rational(3, 2)) == Rational(1, 2)*sqrt(pi) assert gamma(Rational(5, 2)) == Rational(3, 4)*sqrt(pi) assert gamma(Rational(7, 2)) == Rational(15, 8)*sqrt(pi) assert gamma(Rational(-1, 2)) == -2*sqrt(pi) assert gamma(Rational(-3, 2)) == Rational(4, 3)*sqrt(pi) assert gamma(Rational(-5, 2)) == -Rational(8, 15)*sqrt(pi) assert gamma(Rational(-15, 2)) == Rational(256, 2027025)*sqrt(pi) assert gamma(Rational(-11, 8)).expand(func=True) == Rational(64, 33)*gamma(Rational(5, 8)) assert gamma(Rational(-10, 3)).expand(func=True) == Rational(81, 280)*gamma(Rational(2, 3)) assert gamma(Rational(14, 3)).expand(func=True) == Rational(880, 81)*gamma(Rational(2, 3)) assert gamma(Rational(17, 7)).expand(func=True) == Rational(30, 49)*gamma(Rational(3, 7)) assert gamma(Rational(19, 8)).expand(func=True) == Rational(33, 64)*gamma(Rational(3, 8)) assert gamma(x).diff(x) == gamma(x)*polygamma(0, x) assert gamma(x - 1).expand(func=True) == gamma(x)/(x-1) assert gamma(x + 2).expand(func=True, mul=False) == x*(x+1)*gamma(x) assert expand_func(gamma(x + Rational(3, 2))) == \ (x + Rational(1, 2))*gamma(x + Rational(1, 2)) assert expand_func(gamma(x - Rational(1, 2))) == \ gamma(Rational(1, 2) + x)/(x - Rational(1, 2)) # Test a bug: assert expand_func(gamma(x + Rational(3, 4))) == gamma(x + Rational(3, 4))
def test_harmonic_rewrite_sum_fail(): n = Symbol("n") m = Symbol("m") assert harmonic(n).rewrite(digamma) == polygamma(0, n + 1) + EulerGamma assert harmonic(n).rewrite(trigamma) == polygamma(0, n + 1) + EulerGamma assert harmonic(n).rewrite(polygamma) == polygamma(0, n + 1) + EulerGamma assert harmonic(n,3).rewrite(polygamma) == polygamma(2, n + 1)/2 - polygamma(2, 1)/2 assert harmonic(n,m).rewrite(polygamma) == (-1)**m*(polygamma(m - 1, 1) - polygamma(m - 1, n + 1))/factorial(m - 1) assert expand_func(harmonic(n+4)) == harmonic(n) + 1/(n + 4) + 1/(n + 3) + 1/(n + 2) + 1/(n + 1) assert expand_func(harmonic(n-4)) == harmonic(n) - 1/(n - 1) - 1/(n - 2) - 1/(n - 3) - 1/n assert harmonic(n, m).rewrite("tractable") == harmonic(n, m).rewrite(polygamma) _k = Dummy("k") assert harmonic(n).rewrite(Sum) == Sum(1/_k, (_k, 1, n)) assert harmonic(n, m).rewrite(Sum) == Sum(_k**(-m), (_k, 1, n))
def test_polygamma(): from sympy import I assert polygamma(n, nan) == nan assert polygamma(0, oo) == oo assert polygamma(0, -oo) == oo assert polygamma(0, I * oo) == oo assert polygamma(0, -I * oo) == oo assert polygamma(1, oo) == 0 assert polygamma(5, oo) == 0 assert polygamma(0, -9) == zoo assert polygamma(0, -9) == zoo assert polygamma(0, -1) == zoo assert polygamma(0, 0) == zoo assert polygamma(0, 1) == -EulerGamma assert polygamma(0, 7) == Rational(49, 20) - EulerGamma assert polygamma(1, 1) == pi**2 / 6 assert polygamma(1, 2) == pi**2 / 6 - 1 assert polygamma(1, 3) == pi**2 / 6 - Rational(5, 4) assert polygamma(3, 1) == pi**4 / 15 assert polygamma(3, 5) == 6 * (Rational(-22369, 20736) + pi**4 / 90) assert polygamma(5, 1) == 8 * pi**6 / 63 def t(m, n): x = S(m) / n r = polygamma(0, x) if r.has(polygamma): return False return abs(polygamma(0, x.n()).n() - r.n()).n() < 1e-10 assert t(1, 2) assert t(3, 2) assert t(-1, 2) assert t(1, 4) assert t(-3, 4) assert t(1, 3) assert t(4, 3) assert t(3, 4) assert t(2, 3) assert polygamma(0, x).rewrite(zeta) == polygamma(0, x) assert polygamma(1, x).rewrite(zeta) == zeta(2, x) assert polygamma(2, x).rewrite(zeta) == -2 * zeta(3, x) assert polygamma(3, 7 * x).diff(x) == 7 * polygamma(4, 7 * x) assert polygamma(0, x).rewrite(harmonic) == harmonic(x - 1) - EulerGamma assert polygamma( 2, x).rewrite(harmonic) == 2 * harmonic(x - 1, 3) - 2 * zeta(3) ni = Symbol("n", integer=True) assert polygamma( ni, x).rewrite(harmonic) == (-1)**(ni + 1) * (-harmonic(x - 1, ni + 1) + zeta(ni + 1)) * factorial(ni) # Polygamma of non-negative integer order is unbranched: from sympy import exp_polar k = Symbol('n', integer=True, nonnegative=True) assert polygamma(k, exp_polar(2 * I * pi) * x) == polygamma(k, x) # but negative integers are branched! k = Symbol('n', integer=True) assert polygamma(k, exp_polar(2 * I * pi) * x).args == (k, exp_polar(2 * I * pi) * x) # Polygamma of order -1 is loggamma: assert polygamma(-1, x) == loggamma(x) # But smaller orders are iterated integrals and don't have a special name assert polygamma(-2, x).func is polygamma # Test a bug assert polygamma(0, -x).expand(func=True) == polygamma(0, -x)
def t(m, n): x = S(m) / n r = polygamma(0, x) if r.has(polygamma): return False return abs(polygamma(0, x.n()).n() - r.n()).n() < 1e-10
def test_loggamma(): raises(TypeError, lambda: loggamma(2, 3)) raises(ArgumentIndexError, lambda: loggamma(x).fdiff(2)) assert loggamma(-1) is oo assert loggamma(-2) is oo assert loggamma(0) is oo assert loggamma(1) == 0 assert loggamma(2) == 0 assert loggamma(3) == log(2) assert loggamma(4) == log(6) n = Symbol("n", integer=True, positive=True) assert loggamma(n) == log(gamma(n)) assert loggamma(-n) is oo assert loggamma(n / 2) == log(2**(-n + 1) * sqrt(pi) * gamma(n) / gamma(n / 2 + S.Half)) assert loggamma(oo) is oo assert loggamma(-oo) is zoo assert loggamma(I * oo) is zoo assert loggamma(-I * oo) is zoo assert loggamma(zoo) is zoo assert loggamma(nan) is nan L = loggamma(Rational(16, 3)) E = -5 * log(3) + loggamma(Rational( 1, 3)) + log(4) + log(7) + log(10) + log(13) assert expand_func(L).doit() == E assert L.n() == E.n() L = loggamma(Rational(19, 4)) E = -4 * log(4) + loggamma(Rational( 3, 4)) + log(3) + log(7) + log(11) + log(15) assert expand_func(L).doit() == E assert L.n() == E.n() L = loggamma(Rational(23, 7)) E = -3 * log(7) + log(2) + loggamma(Rational(2, 7)) + log(9) + log(16) assert expand_func(L).doit() == E assert L.n() == E.n() L = loggamma(Rational(19, 4) - 7) E = -log(9) - log(5) + loggamma(Rational(3, 4)) + 3 * log(4) - 3 * I * pi assert expand_func(L).doit() == E assert L.n() == E.n() L = loggamma(Rational(23, 7) - 6) E = -log(19) - log(12) - log(5) + loggamma(Rational( 2, 7)) + 3 * log(7) - 3 * I * pi assert expand_func(L).doit() == E assert L.n() == E.n() assert loggamma(x).diff(x) == polygamma(0, x) s1 = loggamma(1 / (x + sin(x)) + cos(x)).nseries(x, n=4) s2 = (-log(2*x) - 1)/(2*x) - log(x/pi)/2 + (4 - log(2*x))*x/24 + O(x**2) + \ log(x)*x**2/2 assert (s1 - s2).expand(force=True).removeO() == 0 s1 = loggamma(1 / x).series(x) s2 = (1/x - S.Half)*log(1/x) - 1/x + log(2*pi)/2 + \ x/12 - x**3/360 + x**5/1260 + O(x**7) assert ((s1 - s2).expand(force=True)).removeO() == 0 assert loggamma(x).rewrite('intractable') == log(gamma(x)) s1 = loggamma(x).series(x).cancel() assert s1 == -log(x) - EulerGamma*x + pi**2*x**2/12 + x**3*polygamma(2, 1)/6 + \ pi**4*x**4/360 + x**5*polygamma(4, 1)/120 + O(x**6) assert s1 == loggamma(x).rewrite('intractable').series(x).cancel() assert conjugate(loggamma(x)) == loggamma(conjugate(x)) assert conjugate(loggamma(0)) is oo assert conjugate(loggamma(1)) == loggamma(conjugate(1)) assert conjugate(loggamma(-oo)) == conjugate(zoo) assert loggamma(Symbol('v', positive=True)).is_real is True assert loggamma(Symbol('v', zero=True)).is_real is False assert loggamma(Symbol('v', negative=True)).is_real is False assert loggamma(Symbol('v', nonpositive=True)).is_real is False assert loggamma(Symbol('v', nonnegative=True)).is_real is None assert loggamma(Symbol('v', imaginary=True)).is_real is None assert loggamma(Symbol('v', real=True)).is_real is None assert loggamma(Symbol('v')).is_real is None assert loggamma(S.Half).is_real is True assert loggamma(0).is_real is False assert loggamma(Rational(-1, 2)).is_real is False assert loggamma(I).is_real is None assert loggamma(2 + 3 * I).is_real is None def tN(N, M): assert loggamma(1 / x)._eval_nseries(x, n=N).getn() == M tN(0, 0) tN(1, 1) tN(2, 2) tN(3, 3) tN(4, 4) tN(5, 5)
def test_issue1893(): from sympy import simplify, expand_func, polygamma, gamma a = Symbol('a', positive=True) assert simplify(expand_func(integrate(exp(-x)*log(x)*x**a, (x, 0, oo)))) == \ (a*polygamma(0, a) + 1)*gamma(a)
def test_polygamma_expand_func(): assert polygamma(0, x).expand(func=True) == polygamma(0, x) assert polygamma(0, 2 * x).expand(func=True) == log(2) + polygamma(0, 2 * x) assert polygamma(2, x).expand(func=True, basic=False) == polygamma(2, x) #assert polygamma(2, 3*x).expand(func=True) == polygamma(2, 3*x)/9 #assert polygamma(3, 4*x).expand(func=True,basic=False) == polygamma(3, 4*x)/64 assert polygamma(0, 1 + x).expand(func=True, basic=False) == 1 + x + polygamma(0, x) assert polygamma(0, 2 + x).expand(func=True, basic=False) == 5 + 2 * x + polygamma(0, x) assert polygamma(0, 3 + x).expand( func=True, basic=False) == 12 + 3 * x + polygamma(0, x) assert polygamma(0, 4 + x).expand( func=True, basic=False) == 22 + 4 * x + polygamma(0, x) assert polygamma(1, 1 + x).expand(func=True, basic=False) == -1 - x + polygamma(1, x) assert polygamma(1, 2 + x).expand( func=True, basic=False) == -5 - 2 * x + polygamma(1, x) assert polygamma(1, 3 + x).expand( func=True, basic=False) == -12 - 3 * x + polygamma(1, x) assert polygamma(1, 4 + x).expand( func=True, basic=False) == -22 - 4 * x + polygamma(1, x) assert polygamma(0, x + y).expand(func=True, basic=False) == polygamma(0, x + y) assert polygamma(1, x + y).expand(func=True, basic=False) == polygamma(1, x + y) assert polygamma(1, 3 + 4 * x + y).expand( func=True, basic=False) == -12 - 12 * x - 3 * y + polygamma(1, y + 4 * x) assert polygamma(3, 3 + 4 * x + y).expand( func=True, basic=False) == -72 - 72 * x - 18 * y + polygamma(3, y + 4 * x) assert polygamma(3, 4 * x + y + 1).expand( func=True, basic=False) == -6 - 24 * x - 6 * y + polygamma(3, y + 4 * x) assert polygamma(3, 4 * x + y + S(3) / 2).expand(func=True, basic=False) == polygamma( 3, S(3) / 2 + y + 4 * x) assert polygamma(3, x + y + S(3) / 4).expand(func=True, basic=False) == polygamma( 3, S(3) / 4 + x + y)
def test_gamma(): assert gamma(nan) is nan assert gamma(oo) is oo assert gamma(-100) is zoo assert gamma(0) is zoo assert gamma(-100.0) is zoo assert gamma(1) == 1 assert gamma(2) == 1 assert gamma(3) == 2 assert gamma(102) == factorial(101) assert gamma(S.Half) == sqrt(pi) assert gamma(Rational(3, 2)) == sqrt(pi) * S.Half assert gamma(Rational(5, 2)) == sqrt(pi) * Rational(3, 4) assert gamma(Rational(7, 2)) == sqrt(pi) * Rational(15, 8) assert gamma(Rational(-1, 2)) == -2 * sqrt(pi) assert gamma(Rational(-3, 2)) == sqrt(pi) * Rational(4, 3) assert gamma(Rational(-5, 2)) == sqrt(pi) * Rational(-8, 15) assert gamma(Rational(-15, 2)) == sqrt(pi) * Rational(256, 2027025) assert gamma(Rational( -11, 8)).expand(func=True) == Rational(64, 33) * gamma(Rational(5, 8)) assert gamma(Rational( -10, 3)).expand(func=True) == Rational(81, 280) * gamma(Rational(2, 3)) assert gamma(Rational( 14, 3)).expand(func=True) == Rational(880, 81) * gamma(Rational(2, 3)) assert gamma(Rational( 17, 7)).expand(func=True) == Rational(30, 49) * gamma(Rational(3, 7)) assert gamma(Rational( 19, 8)).expand(func=True) == Rational(33, 64) * gamma(Rational(3, 8)) assert gamma(x).diff(x) == gamma(x) * polygamma(0, x) assert gamma(x - 1).expand(func=True) == gamma(x) / (x - 1) assert gamma(x + 2).expand(func=True, mul=False) == x * (x + 1) * gamma(x) assert conjugate(gamma(x)) == gamma(conjugate(x)) assert expand_func(gamma(x + Rational(3, 2))) == \ (x + S.Half)*gamma(x + S.Half) assert expand_func(gamma(x - S.Half)) == \ gamma(S.Half + x)/(x - S.Half) # Test a bug: assert expand_func(gamma(x + Rational(3, 4))) == gamma(x + Rational(3, 4)) # XXX: Not sure about these tests. I can fix them by defining e.g. # exp_polar.is_integer but I'm not sure if that makes sense. assert gamma(3 * exp_polar(I * pi) / 4).is_nonnegative is False assert gamma(3 * exp_polar(I * pi) / 4).is_extended_nonpositive is True y = Symbol('y', nonpositive=True, integer=True) assert gamma(y).is_real == False y = Symbol('y', positive=True, noninteger=True) assert gamma(y).is_real == True assert gamma(-1.0, evaluate=False).is_real == False assert gamma(0, evaluate=False).is_real == False assert gamma(-2, evaluate=False).is_real == False
def series_small_a_small_b(): """Tylor series expansion of Phi(a, b, x) in a=0 and b=0 up to order 5. Be aware of cancellation of poles in b=0 of digamma(b)/Gamma(b) and polygamma functions. digamma(b)/Gamma(b) = -1 - 2*M_EG*b + O(b^2) digamma(b)^2/Gamma(b) = 1/b + 3*M_EG + b*(-5/12*PI^2+7/2*M_EG^2) + O(b^2) polygamma(1, b)/Gamma(b) = 1/b + M_EG + b*(1/12*PI^2 + 1/2*M_EG^2) + O(b^2) and so on. """ order = 5 a, b, x, k = symbols("a b x k") M_PI, M_EG, M_Z3 = symbols("M_PI M_EG M_Z3") c_subs = {pi: M_PI, EulerGamma: M_EG, zeta(3): M_Z3} A = [] # terms with a X = [] # terms with x B = [] # terms with b (polygammas expanded) C = [] # terms that generate B # Phi(a, b, x) = exp(x) * sum(A[i] * X[i] * B[i]) # B[0] = 1 # B[k] = sum(C[k] * b**k/k!, k=0..) # Note: C[k] can be obtained from a series expansion of 1/gamma(b). expression = gamma(b)/sympy.exp(x) * \ Sum(x**k/factorial(k)/gamma(a*k+b), (k, 0, S.Infinity)) # nth term of taylor series in a=0: a^n/n! * (d^n Phi(a, b, x)/da^n at a=0) for n in range(0, order+1): term = expression.diff(a, n).subs(a, 0).simplify().doit() # set the whole bracket involving polygammas to 1 x_part = (term.subs(polygamma(0, b), 1) .replace(polygamma, lambda *args: 0)) # sign convetion: x part always positive x_part *= (-1)**n # expansion of polygamma part with 1/gamma(b) pg_part = term/x_part/gamma(b) if n >= 1: # Note: highest term is digamma^n pg_part = pg_part.replace(polygamma, lambda k, x: pg_series(k, x, order+1+n)) pg_part = (pg_part.series(b, 0, n=order+1-n) .removeO() .subs(polygamma(2, 1), -2*zeta(3)) .simplify() ) A.append(a**n/factorial(n)) X.append(horner(x_part)) B.append(pg_part) # Calculate C and put in the k! C = sympy.Poly(B[1].subs(c_subs), b).coeffs() C.reverse() for i in range(len(C)): C[i] = (C[i] * factorial(i)).simplify() s = "Tylor series expansion of Phi(a, b, x) in a=0 and b=0 up to order 5." s += "\nPhi(a, b, x) = exp(x) * sum(A[i] * X[i] * B[i], i=0..5)\n" s += "B[0] = 1\n" s += "B[i] = sum(C[k+i-1] * b**k/k!, k=0..)\n" s += "\nM_PI = pi" s += "\nM_EG = EulerGamma" s += "\nM_Z3 = zeta(3)" for name, c in zip(['A', 'X'], [A, X]): for i in range(len(c)): s += f"\n{name}[{i}] = " s += str(c[i]) # For C, do also compute the values numerically for i in range(len(C)): s += f"\n# C[{i}] = " s += str(C[i]) s += f"\nC[{i}] = " s += str(C[i].subs({M_EG: EulerGamma, M_PI: pi, M_Z3: zeta(3)}) .evalf(17)) # Does B have the assumed structure? s += "\n\nTest if B[i] does have the assumed structure." s += "\nC[i] are derived from B[1] allone." s += "\nTest B[2] == C[1] + b*C[2] + b^2/2*C[3] + b^3/6*C[4] + .." test = sum([b**k/factorial(k) * C[k+1] for k in range(order-1)]) test = (test - B[2].subs(c_subs)).simplify() s += f"\ntest successful = {test==S(0)}" s += "\nTest B[3] == C[2] + b*C[3] + b^2/2*C[4] + .." test = sum([b**k/factorial(k) * C[k+2] for k in range(order-2)]) test = (test - B[3].subs(c_subs)).simplify() s += f"\ntest successful = {test==S(0)}" return s
def test_polygamma_expand_func(): assert polygamma(0, x).expand(func=True) == polygamma(0, x)
def test_meijerint(): from sympy import symbols, expand, arg s, t, mu = symbols("s t mu", real=True) assert integrate( meijerg([], [], [0], [], s * t) * meijerg([], [], [mu / 2], [-mu / 2], t ** 2 / 4), (t, 0, oo) ).is_Piecewise s = symbols("s", positive=True) assert integrate(x ** s * meijerg([[], []], [[0], []], x), (x, 0, oo)) == gamma(s + 1) assert integrate(x ** s * meijerg([[], []], [[0], []], x), (x, 0, oo), meijerg=True) == gamma(s + 1) assert isinstance(integrate(x ** s * meijerg([[], []], [[0], []], x), (x, 0, oo), meijerg=False), Integral) assert meijerint_indefinite(exp(x), x) == exp(x) # TODO what simplifications should be done automatically? # This tests "extra case" for antecedents_1. a, b = symbols("a b", positive=True) assert simplify(meijerint_definite(x ** a, x, 0, b)[0]) == b ** (a + 1) / (a + 1) # This tests various conditions and expansions: meijerint_definite((x + 1) ** 3 * exp(-x), x, 0, oo) == (16, True) # Again, how about simplifications? sigma, mu = symbols("sigma mu", positive=True) i, c = meijerint_definite(exp(-((x - mu) / (2 * sigma)) ** 2), x, 0, oo) assert simplify(i) == sqrt(pi) * sigma * (2 - erfc(mu / (2 * sigma))) assert c == True i, _ = meijerint_definite(exp(-mu * x) * exp(sigma * x), x, 0, oo) # TODO it would be nice to test the condition assert simplify(i) == 1 / (mu - sigma) # Test substitutions to change limits assert meijerint_definite(exp(x), x, -oo, 2) == (exp(2), True) # Note: causes a NaN in _check_antecedents assert expand(meijerint_definite(exp(x), x, 0, I)[0]) == exp(I) - 1 assert expand(meijerint_definite(exp(-x), x, 0, x)[0]) == 1 - exp(-exp(I * arg(x)) * abs(x)) # Test -oo to oo assert meijerint_definite(exp(-x ** 2), x, -oo, oo) == (sqrt(pi), True) assert meijerint_definite(exp(-abs(x)), x, -oo, oo) == (2, True) assert meijerint_definite(exp(-(2 * x - 3) ** 2), x, -oo, oo) == (sqrt(pi) / 2, True) assert meijerint_definite(exp(-abs(2 * x - 3)), x, -oo, oo) == (1, True) assert meijerint_definite(exp(-((x - mu) / sigma) ** 2 / 2) / sqrt(2 * pi * sigma ** 2), x, -oo, oo) == (1, True) assert meijerint_definite(sinc(x) ** 2, x, -oo, oo) == (pi, True) # Test one of the extra conditions for 2 g-functinos assert meijerint_definite(exp(-x) * sin(x), x, 0, oo) == (S(1) / 2, True) # Test a bug def res(n): return (1 / (1 + x ** 2)).diff(x, n).subs(x, 1) * (-1) ** n for n in range(6): assert integrate(exp(-x) * sin(x) * x ** n, (x, 0, oo), meijerg=True) == res(n) # This used to test trigexpand... now it is done by linear substitution assert simplify(integrate(exp(-x) * sin(x + a), (x, 0, oo), meijerg=True)) == sqrt(2) * sin(a + pi / 4) / 2 # Test the condition 14 from prudnikov. # (This is besselj*besselj in disguise, to stop the product from being # recognised in the tables.) a, b, s = symbols("a b s") from sympy import And, re assert meijerint_definite( meijerg([], [], [a / 2], [-a / 2], x / 4) * meijerg([], [], [b / 2], [-b / 2], x / 4) * x ** (s - 1), x, 0, oo ) == ( 4 * 2 ** (2 * s - 2) * gamma(-2 * s + 1) * gamma(a / 2 + b / 2 + s) / (gamma(-a / 2 + b / 2 - s + 1) * gamma(a / 2 - b / 2 - s + 1) * gamma(a / 2 + b / 2 - s + 1)), And(0 < -2 * re(4 * s) + 8, 0 < re(a / 2 + b / 2 + s), re(2 * s) < 1), ) # test a bug assert integrate(sin(x ** a) * sin(x ** b), (x, 0, oo), meijerg=True) == Integral( sin(x ** a) * sin(x ** b), (x, 0, oo) ) # test better hyperexpand assert ( integrate(exp(-x ** 2) * log(x), (x, 0, oo), meijerg=True) == (sqrt(pi) * polygamma(0, S(1) / 2) / 4).expand() ) # Test hyperexpand bug. from sympy import lowergamma n = symbols("n", integer=True) assert simplify(integrate(exp(-x) * x ** n, x, meijerg=True)) == lowergamma(n + 1, x) # Test a bug with argument 1/x alpha = symbols("alpha", positive=True) assert meijerint_definite((2 - x) ** alpha * sin(alpha / x), x, 0, 2) == ( sqrt(pi) * alpha * gamma(alpha + 1) * meijerg(((), (alpha / 2 + S(1) / 2, alpha / 2 + 1)), ((0, 0, S(1) / 2), (-S(1) / 2,)), alpha ** S(2) / 16) / 4, True, ) # test a bug related to 3016 a, s = symbols("a s", positive=True) assert ( simplify(integrate(x ** s * exp(-a * x ** 2), (x, -oo, oo))) == a ** (-s / 2 - S(1) / 2) * ((-1) ** s + 1) * gamma(s / 2 + S(1) / 2) / 2 )
def test_issue_4992(): # Note: psi in _check_antecedents becomes NaN. from sympy import simplify, expand_func, polygamma, gamma a = Symbol('a', positive=True) assert simplify(expand_func(integrate(exp(-x)*log(x)*x**a, (x, 0, oo)))) == \ (a*polygamma(0, a) + 1)*gamma(a)
def test_loggamma(): raises(TypeError, lambda: loggamma(2, 3)) raises(ArgumentIndexError, lambda: loggamma(x).fdiff(2)) assert loggamma(-1) == oo assert loggamma(-2) == oo assert loggamma(0) == oo assert loggamma(1) == 0 assert loggamma(2) == 0 assert loggamma(3) == log(2) assert loggamma(4) == log(6) n = Symbol("n", integer=True, positive=True) assert loggamma(n) == log(gamma(n)) assert loggamma(-n) == oo assert loggamma(n / 2) == log(2**(-n + 1) * sqrt(pi) * gamma(n) / gamma(n / 2 + S.Half)) from sympy import I assert loggamma(oo) == oo assert loggamma(-oo) == zoo assert loggamma(I * oo) == zoo assert loggamma(-I * oo) == zoo assert loggamma(zoo) == zoo assert loggamma(nan) == nan L = loggamma(S(16) / 3) E = -5 * log(3) + loggamma(S(1) / 3) + log(4) + log(7) + log(10) + log(13) assert expand_func(L).doit() == E assert L.n() == E.n() L = loggamma(19 / S(4)) E = -4 * log(4) + loggamma(S(3) / 4) + log(3) + log(7) + log(11) + log(15) assert expand_func(L).doit() == E assert L.n() == E.n() L = loggamma(S(23) / 7) E = -3 * log(7) + log(2) + loggamma(S(2) / 7) + log(9) + log(16) assert expand_func(L).doit() == E assert L.n() == E.n() L = loggamma(19 / S(4) - 7) E = -log(9) - log(5) + loggamma(S(3) / 4) + 3 * log(4) - 3 * I * pi assert expand_func(L).doit() == E assert L.n() == E.n() L = loggamma(23 / S(7) - 6) E = -log(19) - log(12) - log(5) + loggamma( S(2) / 7) + 3 * log(7) - 3 * I * pi assert expand_func(L).doit() == E assert L.n() == E.n() assert loggamma(x).diff(x) == polygamma(0, x) s1 = loggamma(1 / (x + sin(x)) + cos(x)).nseries(x, n=4) s2 = (-log(2*x) - 1)/(2*x) - log(x/pi)/2 + (4 - log(2*x))*x/24 + O(x**2) + \ log(x)*x**2/2 assert (s1 - s2).expand(force=True).removeO() == 0 s1 = loggamma(1 / x).series(x) s2 = (1/x - S(1)/2)*log(1/x) - 1/x + log(2*pi)/2 + \ x/12 - x**3/360 + x**5/1260 + O(x**7) assert ((s1 - s2).expand(force=True)).removeO() == 0 assert loggamma(x).rewrite('intractable') == log(gamma(x)) s1 = loggamma(x).series(x) assert s1 == -log(x) - EulerGamma*x + pi**2*x**2/12 + x**3*polygamma(2, 1)/6 + \ pi**4*x**4/360 + x**5*polygamma(4, 1)/120 + O(x**6) assert s1 == loggamma(x).rewrite('intractable').series(x) assert conjugate(loggamma(x)) == loggamma(conjugate(x)) assert conjugate(loggamma(0)) == conjugate(loggamma(0)) assert conjugate(loggamma(1)) == loggamma(conjugate(1)) assert conjugate(loggamma(-oo)) == conjugate(loggamma(-oo)) assert loggamma(x).is_real is None y, z = Symbol('y', real=True), Symbol('z', imaginary=True) assert loggamma(y).is_real assert loggamma(z).is_real is False def tN(N, M): assert loggamma(1 / x)._eval_nseries(x, n=N).getn() == M tN(0, 0) tN(1, 1) tN(2, 3) tN(3, 3) tN(4, 5) tN(5, 5)
def test_meijerint(): from sympy import symbols, expand, arg s, t, mu = symbols("s t mu", real=True) assert integrate( meijerg([], [], [0], [], s * t) * meijerg([], [], [mu / 2], [-mu / 2], t ** 2 / 4), (t, 0, oo), ).is_Piecewise s = symbols("s", positive=True) assert integrate(x ** s * meijerg([[], []], [[0], []], x), (x, 0, oo)) == gamma( s + 1 ) assert integrate( x ** s * meijerg([[], []], [[0], []], x), (x, 0, oo), meijerg=True ) == gamma(s + 1) assert isinstance( integrate(x ** s * meijerg([[], []], [[0], []], x), (x, 0, oo), meijerg=False), Integral, ) assert meijerint_indefinite(exp(x), x) == exp(x) # TODO what simplifications should be done automatically? # This tests "extra case" for antecedents_1. a, b = symbols("a b", positive=True) assert simplify(meijerint_definite(x ** a, x, 0, b)[0]) == b ** (a + 1) / (a + 1) # This tests various conditions and expansions: meijerint_definite((x + 1) ** 3 * exp(-x), x, 0, oo) == (16, True) # Again, how about simplifications? sigma, mu = symbols("sigma mu", positive=True) i, c = meijerint_definite(exp(-(((x - mu) / (2 * sigma)) ** 2)), x, 0, oo) assert simplify(i) == sqrt(pi) * sigma * (2 - erfc(mu / (2 * sigma))) assert c == True i, _ = meijerint_definite(exp(-mu * x) * exp(sigma * x), x, 0, oo) # TODO it would be nice to test the condition assert simplify(i) == 1 / (mu - sigma) # Test substitutions to change limits assert meijerint_definite(exp(x), x, -oo, 2) == (exp(2), True) # Note: causes a NaN in _check_antecedents assert expand(meijerint_definite(exp(x), x, 0, I)[0]) == exp(I) - 1 assert expand(meijerint_definite(exp(-x), x, 0, x)[0]) == 1 - exp( -exp(I * arg(x)) * abs(x) ) # Test -oo to oo assert meijerint_definite(exp(-(x ** 2)), x, -oo, oo) == (sqrt(pi), True) assert meijerint_definite(exp(-abs(x)), x, -oo, oo) == (2, True) assert meijerint_definite(exp(-((2 * x - 3) ** 2)), x, -oo, oo) == ( sqrt(pi) / 2, True, ) assert meijerint_definite(exp(-abs(2 * x - 3)), x, -oo, oo) == (1, True) assert meijerint_definite( exp(-(((x - mu) / sigma) ** 2) / 2) / sqrt(2 * pi * sigma ** 2), x, -oo, oo ) == (1, True) assert meijerint_definite(sinc(x) ** 2, x, -oo, oo) == (pi, True) # Test one of the extra conditions for 2 g-functinos assert meijerint_definite(exp(-x) * sin(x), x, 0, oo) == (S.Half, True) # Test a bug def res(n): return (1 / (1 + x ** 2)).diff(x, n).subs(x, 1) * (-1) ** n for n in range(6): assert integrate(exp(-x) * sin(x) * x ** n, (x, 0, oo), meijerg=True) == res(n) # This used to test trigexpand... now it is done by linear substitution assert ( simplify(integrate(exp(-x) * sin(x + a), (x, 0, oo), meijerg=True)) == sqrt(2) * sin(a + pi / 4) / 2 ) # Test the condition 14 from prudnikov. # (This is besselj*besselj in disguise, to stop the product from being # recognised in the tables.) a, b, s = symbols("a b s") from sympy import And, re assert meijerint_definite( meijerg([], [], [a / 2], [-a / 2], x / 4) * meijerg([], [], [b / 2], [-b / 2], x / 4) * x ** (s - 1), x, 0, oo, ) == ( 4 * 2 ** (2 * s - 2) * gamma(-2 * s + 1) * gamma(a / 2 + b / 2 + s) / ( gamma(-a / 2 + b / 2 - s + 1) * gamma(a / 2 - b / 2 - s + 1) * gamma(a / 2 + b / 2 - s + 1) ), And(0 < -2 * re(4 * s) + 8, 0 < re(a / 2 + b / 2 + s), re(2 * s) < 1), ) # test a bug assert integrate(sin(x ** a) * sin(x ** b), (x, 0, oo), meijerg=True) == Integral( sin(x ** a) * sin(x ** b), (x, 0, oo) ) # test better hyperexpand assert ( integrate(exp(-(x ** 2)) * log(x), (x, 0, oo), meijerg=True) == (sqrt(pi) * polygamma(0, S.Half) / 4).expand() ) # Test hyperexpand bug. from sympy import lowergamma n = symbols("n", integer=True) assert simplify(integrate(exp(-x) * x ** n, x, meijerg=True)) == lowergamma( n + 1, x ) # Test a bug with argument 1/x alpha = symbols("alpha", positive=True) assert meijerint_definite((2 - x) ** alpha * sin(alpha / x), x, 0, 2) == ( sqrt(pi) * alpha * gamma(alpha + 1) * meijerg( ((), (alpha / 2 + S.Half, alpha / 2 + 1)), ((0, 0, S.Half), (Rational(-1, 2),)), alpha ** 2 / 16, ) / 4, True, ) # test a bug related to 3016 a, s = symbols("a s", positive=True) assert ( simplify(integrate(x ** s * exp(-a * x ** 2), (x, -oo, oo))) == a ** (-s / 2 - S.Half) * ((-1) ** s + 1) * gamma(s / 2 + S.Half) / 2 )
def test_meijerint(): from sympy import symbols, expand, arg s, t, mu = symbols('s t mu', real=True) assert integrate(meijerg([], [], [0], [], s*t) *meijerg([], [], [mu/2], [-mu/2], t**2/4), (t, 0, oo)).is_Piecewise s = symbols('s', positive=True) assert integrate(x**s*meijerg([[],[]], [[0],[]], x), (x, 0, oo)) \ == gamma(s + 1) assert integrate(x**s*meijerg([[],[]], [[0],[]], x), (x, 0, oo), meijerg=True) == gamma(s + 1) assert isinstance(integrate(x**s*meijerg([[],[]], [[0],[]], x), (x, 0, oo), meijerg=False), Integral) assert meijerint_indefinite(exp(x), x) == exp(x) # TODO what simplifications should be done automatically? # This tests "extra case" for antecedents_1. a, b = symbols('a b', positive=True) assert simplify(meijerint_definite(x**a, x, 0, b)[0]) \ == b**(a + 1)/(a + 1) # This tests various conditions and expansions: meijerint_definite((x+1)**3*exp(-x), x, 0, oo) == (16, True) # Again, how about simplifications? sigma, mu = symbols('sigma mu', positive=True) i, c = meijerint_definite(exp(-((x-mu)/(2*sigma))**2), x, 0, oo) assert simplify(i) \ == sqrt(pi)*sigma*(erf(mu/(2*sigma)) + 1) assert c is True i, _ = meijerint_definite(exp(-mu*x)*exp(sigma*x), x, 0, oo) # TODO it would be nice to test the condition assert simplify(i) == 1/(mu - sigma) # Test substitutions to change limits assert meijerint_definite(exp(x), x, -oo, 2) == (exp(2), True) assert expand(meijerint_definite(exp(x), x, 0, I)[0]) == exp(I) - 1 assert expand(meijerint_definite(exp(-x), x, 0, x)[0]) == \ 1 - exp(-exp(I*arg(x))*abs(x)) # Test -oo to oo assert meijerint_definite(exp(-x**2), x, -oo, oo) == (sqrt(pi), True) assert meijerint_definite(exp(-abs(x)), x, -oo, oo) == (2, True) assert meijerint_definite(exp(-(2*x-3)**2), x, -oo, oo) == (sqrt(pi)/2, True) assert meijerint_definite(exp(-abs(2*x-3)), x, -oo, oo) == (1, True) assert meijerint_definite(exp(-((x-mu)/sigma)**2/2)/sqrt(2*pi*sigma**2), x, -oo, oo) == (1, True) # Test one of the extra conditions for 2 g-functinos assert meijerint_definite(exp(-x)*sin(x), x, 0, oo) == (S(1)/2, True) # Test a bug def res(n): return (1/(1+x**2)).diff(x, n).subs(x,1)*(-1)**n for n in range(6): assert integrate(exp(-x)*sin(x)*x**n, (x, 0, oo), meijerg=True) == res(n) # This used to test trigexpand... now it is done by linear substitution assert simplify(integrate(exp(-x)*sin(x + a), (x, 0, oo), meijerg=True)).expand().rewrite(sin).expand() == \ sin(a)/2 + cos(a)/2 # Test the condition 14 from prudnikov. # (This is besselj*besselj in disguise, to stop the product from being # recognised in the tables.) a, b, s = symbols('a b s') from sympy import And, re assert meijerint_definite(meijerg([], [], [a/2], [-a/2], x/4) \ *meijerg([], [], [b/2], [-b/2], x/4)*x**(s-1), x, 0, oo) == \ (4*2**(2*s - 2)*gamma(-2*s + 1)*gamma(a/2 + b/2 + s) \ /(gamma(-a/2 + b/2 - s + 1)*gamma(a/2 - b/2 - s + 1) \ *gamma(a/2 + b/2 - s + 1)), And(0 < -2*re(4*s) + 8, 0 < re(a/2 + b/2 + s), re(2*s) < 1)) # test a bug assert integrate(sin(x**a)*sin(x**b), (x, 0, oo), meijerg=True) == \ Integral(sin(x**a)*sin(x**b), (x, 0, oo)) # test better hyperexpand assert integrate(exp(-x**2)*log(x), (x, 0, oo), meijerg=True) == \ (sqrt(pi)*polygamma(0, S(1)/2)/4).expand() # Test hyperexpand bug. from sympy import lowergamma n = symbols('n', integer = True) assert simplify(integrate(exp(-x)*x**n, x, meijerg=True)) == \ lowergamma(n + 1, x) # Test a bug with argument 1/x alpha = symbols('alpha', positive=True) assert meijerint_definite((2-x)**alpha*sin(alpha/x), x, 0, 2) == \ (sqrt(pi)*gamma(alpha + 1) \ *meijerg([S(1)/2, 0, S(1)/2], [1], [], [-alpha/2, -alpha/2 - S(1)/2], 16/alpha**2), True)
def test_polygamma(): assert polygamma(n, nan) is nan assert polygamma(0, oo) is oo assert polygamma(0, -oo) is oo assert polygamma(0, I * oo) is oo assert polygamma(0, -I * oo) is oo assert polygamma(1, oo) == 0 assert polygamma(5, oo) == 0 assert polygamma(0, -9) is zoo assert polygamma(0, -9) is zoo assert polygamma(0, -1) is zoo assert polygamma(0, 0) is zoo assert polygamma(0, 1) == -EulerGamma assert polygamma(0, 7) == Rational(49, 20) - EulerGamma assert polygamma(1, 1) == pi**2 / 6 assert polygamma(1, 2) == pi**2 / 6 - 1 assert polygamma(1, 3) == pi**2 / 6 - Rational(5, 4) assert polygamma(3, 1) == pi**4 / 15 assert polygamma(3, 5) == 6 * (Rational(-22369, 20736) + pi**4 / 90) assert polygamma(5, 1) == 8 * pi**6 / 63 assert polygamma(1, S.Half) == pi**2 / 2 assert polygamma(2, S.Half) == -14 * zeta(3) assert polygamma(11, S.Half) == 176896 * pi**12 def t(m, n): x = S(m) / n r = polygamma(0, x) if r.has(polygamma): return False return abs(polygamma(0, x.n()).n() - r.n()).n() < 1e-10 assert t(1, 2) assert t(3, 2) assert t(-1, 2) assert t(1, 4) assert t(-3, 4) assert t(1, 3) assert t(4, 3) assert t(3, 4) assert t(2, 3) assert t(123, 5) assert polygamma(0, x).rewrite(zeta) == polygamma(0, x) assert polygamma(1, x).rewrite(zeta) == zeta(2, x) assert polygamma(2, x).rewrite(zeta) == -2 * zeta(3, x) assert polygamma(I, 2).rewrite(zeta) == polygamma(I, 2) n1 = Symbol('n1') n2 = Symbol('n2', real=True) n3 = Symbol('n3', integer=True) n4 = Symbol('n4', positive=True) n5 = Symbol('n5', positive=True, integer=True) assert polygamma(n1, x).rewrite(zeta) == polygamma(n1, x) assert polygamma(n2, x).rewrite(zeta) == polygamma(n2, x) assert polygamma(n3, x).rewrite(zeta) == polygamma(n3, x) assert polygamma(n4, x).rewrite(zeta) == polygamma(n4, x) assert polygamma( n5, x).rewrite(zeta) == (-1)**(n5 + 1) * factorial(n5) * zeta(n5 + 1, x) assert polygamma(3, 7 * x).diff(x) == 7 * polygamma(4, 7 * x) assert polygamma(0, x).rewrite(harmonic) == harmonic(x - 1) - EulerGamma assert polygamma( 2, x).rewrite(harmonic) == 2 * harmonic(x - 1, 3) - 2 * zeta(3) ni = Symbol("n", integer=True) assert polygamma( ni, x).rewrite(harmonic) == (-1)**(ni + 1) * (-harmonic(x - 1, ni + 1) + zeta(ni + 1)) * factorial(ni) # Polygamma of non-negative integer order is unbranched: k = Symbol('n', integer=True, nonnegative=True) assert polygamma(k, exp_polar(2 * I * pi) * x) == polygamma(k, x) # but negative integers are branched! k = Symbol('n', integer=True) assert polygamma(k, exp_polar(2 * I * pi) * x).args == (k, exp_polar(2 * I * pi) * x) # Polygamma of order -1 is loggamma: assert polygamma(-1, x) == loggamma(x) # But smaller orders are iterated integrals and don't have a special name assert polygamma(-2, x).func is polygamma # Test a bug assert polygamma(0, -x).expand(func=True) == polygamma(0, -x) assert polygamma(2, 2.5).is_positive == False assert polygamma(2, -2.5).is_positive == False assert polygamma(3, 2.5).is_positive == True assert polygamma(3, -2.5).is_positive is True assert polygamma(-2, -2.5).is_positive is None assert polygamma(-3, -2.5).is_positive is None assert polygamma(2, 2.5).is_negative == True assert polygamma(3, 2.5).is_negative == False assert polygamma(3, -2.5).is_negative == False assert polygamma(2, -2.5).is_negative is True assert polygamma(-2, -2.5).is_negative is None assert polygamma(-3, -2.5).is_negative is None assert polygamma(I, 2).is_positive is None assert polygamma(I, 3).is_negative is None # issue 17350 assert polygamma(pi, 3).evalf() == polygamma(pi, 3) assert (I*polygamma(I, pi)).as_real_imag() == \ (-im(polygamma(I, pi)), re(polygamma(I, pi))) assert (tanh(polygamma(I, 1))).rewrite(exp) == \ (exp(polygamma(I, 1)) - exp(-polygamma(I, 1)))/(exp(polygamma(I, 1)) + exp(-polygamma(I, 1))) assert (I / polygamma(I, 4)).rewrite(exp) == \ I*sqrt(re(polygamma(I, 4))**2 + im(polygamma(I, 4))**2)\ /((re(polygamma(I, 4)) + I*im(polygamma(I, 4)))*Abs(polygamma(I, 4))) assert unchanged(polygamma, 2.3, 1.0) # issue 12569 assert unchanged(im, polygamma(0, I)) assert polygamma(Symbol('a', positive=True), Symbol( 'b', positive=True)).is_real is True assert polygamma(0, I).is_real is None
def test_loggamma(): raises(TypeError, lambda: loggamma(2, 3)) raises(ArgumentIndexError, lambda: loggamma(x).fdiff(2)) assert loggamma(-1) == oo assert loggamma(-2) == oo assert loggamma(0) == oo assert loggamma(1) == 0 assert loggamma(2) == 0 assert loggamma(3) == log(2) assert loggamma(4) == log(6) n = Symbol("n", integer=True, positive=True) assert loggamma(n) == log(gamma(n)) assert loggamma(-n) == oo assert loggamma(n/2) == log(2**(-n + 1)*sqrt(pi)*gamma(n)/gamma(n/2 + S.Half)) from sympy import I assert loggamma(oo) == oo assert loggamma(-oo) == zoo assert loggamma(I*oo) == zoo assert loggamma(-I*oo) == zoo assert loggamma(zoo) == zoo assert loggamma(nan) == nan L = loggamma(S(16)/3) E = -5*log(3) + loggamma(S(1)/3) + log(4) + log(7) + log(10) + log(13) assert expand_func(L).doit() == E assert L.n() == E.n() L = loggamma(19/S(4)) E = -4*log(4) + loggamma(S(3)/4) + log(3) + log(7) + log(11) + log(15) assert expand_func(L).doit() == E assert L.n() == E.n() L = loggamma(S(23)/7) E = -3*log(7) + log(2) + loggamma(S(2)/7) + log(9) + log(16) assert expand_func(L).doit() == E assert L.n() == E.n() L = loggamma(19/S(4)-7) E = -log(9) - log(5) + loggamma(S(3)/4) + 3*log(4) - 3*I*pi assert expand_func(L).doit() == E assert L.n() == E.n() L = loggamma(23/S(7)-6) E = -log(19) - log(12) - log(5) + loggamma(S(2)/7) + 3*log(7) - 3*I*pi assert expand_func(L).doit() == E assert L.n() == E.n() assert loggamma(x).diff(x) == polygamma(0, x) s1 = loggamma(1/(x + sin(x)) + cos(x)).nseries(x, n=4) s2 = (-log(2*x) - 1)/(2*x) - log(x/pi)/2 + (4 - log(2*x))*x/24 + O(x**2) + \ log(x)*x**2/2 assert (s1 - s2).expand(force=True).removeO() == 0 s1 = loggamma(1/x).series(x) s2 = (1/x - S(1)/2)*log(1/x) - 1/x + log(2*pi)/2 + \ x/12 - x**3/360 + x**5/1260 + O(x**7) assert ((s1 - s2).expand(force=True)).removeO() == 0 assert loggamma(x).rewrite('intractable') == log(gamma(x)) s1 = loggamma(x).series(x) assert s1 == -log(x) - EulerGamma*x + pi**2*x**2/12 + x**3*polygamma(2, 1)/6 + \ pi**4*x**4/360 + x**5*polygamma(4, 1)/120 + O(x**6) assert s1 == loggamma(x).rewrite('intractable').series(x) assert conjugate(loggamma(x)) == loggamma(conjugate(x)) assert conjugate(loggamma(0)) == conjugate(loggamma(0)) assert conjugate(loggamma(1)) == loggamma(conjugate(1)) assert conjugate(loggamma(-oo)) == conjugate(loggamma(-oo)) assert loggamma(x).is_real is None y, z = Symbol('y', real=True), Symbol('z', imaginary=True) assert loggamma(y).is_real assert loggamma(z).is_real is False def tN(N, M): assert loggamma(1/x)._eval_nseries(x, n=N).getn() == M tN(0, 0) tN(1, 1) tN(2, 3) tN(3, 3) tN(4, 5) tN(5, 5)
def test_polygamma_expand_func(): assert polygamma(0, x).expand(func=True) == polygamma(0, x) assert polygamma(0, 2*x).expand(func=True) == \ polygamma(0, x)/2 + polygamma(0, Rational(1, 2) + x)/2 + log(2) assert polygamma(1, 2*x).expand(func=True) == \ polygamma(1, x)/4 + polygamma(1, Rational(1, 2) + x)/4 assert polygamma(2, x).expand(func=True) == \ polygamma(2, x) assert polygamma(0, -1 + x).expand(func=True) == \ polygamma(0, x) - 1/(x - 1) assert polygamma(0, 1 + x).expand(func=True) == \ 1/x + polygamma(0, x ) assert polygamma(0, 2 + x).expand(func=True) == \ 1/x + 1/(1 + x) + polygamma(0, x) assert polygamma(0, 3 + x).expand(func=True) == \ polygamma(0, x) + 1/x + 1/(1 + x) + 1/(2 + x) assert polygamma(0, 4 + x).expand(func=True) == \ polygamma(0, x) + 1/x + 1/(1 + x) + 1/(2 + x) + 1/(3 + x) assert polygamma(1, 1 + x).expand(func=True) == \ polygamma(1, x) - 1/x**2 assert polygamma(1, 2 + x).expand(func=True, multinomial=False) == \ polygamma(1, x) - 1/x**2 - 1/(1 + x)**2 assert polygamma(1, 3 + x).expand(func=True, multinomial=False) == \ polygamma(1, x) - 1/x**2 - 1/(1 + x)**2 - 1/(2 + x)**2 assert polygamma(1, 4 + x).expand(func=True, multinomial=False) == \ polygamma(1, x) - 1/x**2 - 1/(1 + x)**2 - \ 1/(2 + x)**2 - 1/(3 + x)**2 assert polygamma(0, x + y).expand(func=True) == \ polygamma(0, x + y) assert polygamma(1, x + y).expand(func=True) == \ polygamma(1, x + y) assert polygamma(1, 3 + 4*x + y).expand(func=True, multinomial=False) == \ polygamma(1, y + 4*x) - 1/(y + 4*x)**2 - \ 1/(1 + y + 4*x)**2 - 1/(2 + y + 4*x)**2 assert polygamma(3, 3 + 4*x + y).expand(func=True, multinomial=False) == \ polygamma(3, y + 4*x) - 6/(y + 4*x)**4 - \ 6/(1 + y + 4*x)**4 - 6/(2 + y + 4*x)**4 assert polygamma(3, 4*x + y + 1).expand(func=True, multinomial=False) == \ polygamma(3, y + 4*x) - 6/(y + 4*x)**4 e = polygamma(3, 4 * x + y + S(3) / 2) assert e.expand(func=True) == e e = polygamma(3, x + y + S(3) / 4) assert e.expand(func=True, basic=False) == e
def test_polygamma(): assert polygamma(n, nan) == nan assert polygamma(0, oo) == oo assert polygamma(1, oo) == 0 assert polygamma(5, oo) == 0 assert polygamma(0, -9) == zoo assert polygamma(0, -9) == zoo assert polygamma(0, -1) == zoo assert polygamma(0, 0) == zoo assert polygamma(0, 1) == -EulerGamma assert polygamma(0, 7) == Rational(49, 20) - EulerGamma assert polygamma(1, 1) == pi**2/6 assert polygamma(1, 2) == pi**2/6 - 1 assert polygamma(1, 3) == pi**2/6 - Rational(5, 4) assert polygamma(3, 1) == pi**4 / 15 assert polygamma(3, 5) == 6*(Rational(-22369,20736) + pi**4/90) assert polygamma(5, 1) == 8 * pi**6 / 63 assert polygamma(3, 7*x).diff(x) == 7*polygamma(4, 7*x)
def test_J5(): assert polygamma(0, R(1, 3)) == -EulerGamma - pi/2*sqrt(R(1, 3)) - R(3, 2)*log(3)
def test_polygamma_expand_func(): assert polygamma(0, x).expand(func=True) == polygamma(0, x) assert polygamma(0, 2*x).expand(func=True) == \ polygamma(0, x)/2 + polygamma(0, Rational(1, 2) + x)/2 + log(2) assert polygamma(1, 2*x).expand(func=True) == \ polygamma(1, x)/4 + polygamma(1, Rational(1, 2) + x)/4 assert polygamma(2, x).expand(func=True) == \ polygamma(2, x) assert polygamma(0, -1 + x).expand(func=True) == \ polygamma(0, x) + 1/(1 - x) assert polygamma(0, 1 + x).expand(func=True) == \ 1/x + polygamma(0, x ) assert polygamma(0, 2 + x).expand(func=True) == \ 1/x + 1/(1 + x) + polygamma(0, x) assert polygamma(0, 3 + x).expand(func=True) == \ polygamma(0, x) + 1/x + 1/(1 + x) + 1/(2 + x) assert polygamma(0, 4 + x).expand(func=True) == \ polygamma(0, x) + 1/x + 1/(1 + x) + 1/(2 + x) + 1/(3 + x) assert polygamma(1, 1 + x).expand(func=True) == \ polygamma(1, x) - 1/x**2 assert polygamma(1, 2 + x).expand(func=True, multinomial=False) == \ polygamma(1, x) - 1/x**2 - 1/(1 + x)**2 assert polygamma(1, 3 + x).expand(func=True, multinomial=False) == \ polygamma(1, x) - 1/x**2 - 1/(1 + x)**2 - 1/(2 + x)**2 assert polygamma(1, 4 + x).expand(func=True, multinomial=False) == \ polygamma(1, x) - 1/x**2 - 1/(1 + x)**2 - \ 1/(2 + x)**2 - 1/(3 + x)**2 assert polygamma(0, x + y).expand(func=True) == \ polygamma(0, x + y) assert polygamma(1, x + y).expand(func=True) == \ polygamma(1, x + y) assert polygamma(1, 3 + 4*x + y).expand(func=True, multinomial=False) == \ polygamma(1, y + 4*x) - 1/(y + 4*x)**2 - \ 1/(1 + y + 4*x)**2 - 1/(2 + y + 4*x)**2 assert polygamma(3, 3 + 4*x + y).expand(func=True, multinomial=False) == \ polygamma(3, y + 4*x) - 6/(y + 4*x)**4 - \ 6/(1 + y + 4*x)**4 - 6/(2 + y + 4*x)**4 assert polygamma(3, 4*x + y + 1).expand(func=True, multinomial=False) == \ polygamma(3, y + 4*x) - 6/(y + 4*x)**4 e = polygamma(3, 4*x + y + S(3)/2) assert e.expand(func=True) == e e = polygamma(3, x + y + S(3)/4) assert e.expand(func = True, basic = False) == e
def test_factorial_simplify_fail(): # simplify(factorial(x + 1).diff(x) - ((x + 1)*factorial(x)).diff(x))) == 0 from sympy.abc import x assert simplify(x*polygamma(0, x + 1) - x*polygamma(0, x + 2) + polygamma(0, x + 1) - polygamma(0, x + 2) + 1) == 0
def test_probability(): # various integrals from probability theory from sympy.abc import x, y from sympy import symbols, Symbol, Abs, expand_mul, combsimp, powsimp, sin mu1, mu2 = symbols("mu1 mu2", real=True, nonzero=True, finite=True) sigma1, sigma2 = symbols("sigma1 sigma2", real=True, nonzero=True, finite=True, positive=True) rate = Symbol("lambda", real=True, positive=True, finite=True) def normal(x, mu, sigma): return 1 / sqrt(2 * pi * sigma ** 2) * exp(-(x - mu) ** 2 / 2 / sigma ** 2) def exponential(x, rate): return rate * exp(-rate * x) assert integrate(normal(x, mu1, sigma1), (x, -oo, oo), meijerg=True) == 1 assert integrate(x * normal(x, mu1, sigma1), (x, -oo, oo), meijerg=True) == mu1 assert integrate(x ** 2 * normal(x, mu1, sigma1), (x, -oo, oo), meijerg=True) == mu1 ** 2 + sigma1 ** 2 assert integrate(x ** 3 * normal(x, mu1, sigma1), (x, -oo, oo), meijerg=True) == mu1 ** 3 + 3 * mu1 * sigma1 ** 2 assert integrate(normal(x, mu1, sigma1) * normal(y, mu2, sigma2), (x, -oo, oo), (y, -oo, oo), meijerg=True) == 1 assert ( integrate(x * normal(x, mu1, sigma1) * normal(y, mu2, sigma2), (x, -oo, oo), (y, -oo, oo), meijerg=True) == mu1 ) assert ( integrate(y * normal(x, mu1, sigma1) * normal(y, mu2, sigma2), (x, -oo, oo), (y, -oo, oo), meijerg=True) == mu2 ) assert ( integrate(x * y * normal(x, mu1, sigma1) * normal(y, mu2, sigma2), (x, -oo, oo), (y, -oo, oo), meijerg=True) == mu1 * mu2 ) assert ( integrate( (x + y + 1) * normal(x, mu1, sigma1) * normal(y, mu2, sigma2), (x, -oo, oo), (y, -oo, oo), meijerg=True ) == 1 + mu1 + mu2 ) assert ( integrate( (x + y - 1) * normal(x, mu1, sigma1) * normal(y, mu2, sigma2), (x, -oo, oo), (y, -oo, oo), meijerg=True ) == -1 + mu1 + mu2 ) i = integrate(x ** 2 * normal(x, mu1, sigma1) * normal(y, mu2, sigma2), (x, -oo, oo), (y, -oo, oo), meijerg=True) assert not i.has(Abs) assert simplify(i) == mu1 ** 2 + sigma1 ** 2 assert ( integrate(y ** 2 * normal(x, mu1, sigma1) * normal(y, mu2, sigma2), (x, -oo, oo), (y, -oo, oo), meijerg=True) == sigma2 ** 2 + mu2 ** 2 ) assert integrate(exponential(x, rate), (x, 0, oo), meijerg=True) == 1 assert integrate(x * exponential(x, rate), (x, 0, oo), meijerg=True) == 1 / rate assert integrate(x ** 2 * exponential(x, rate), (x, 0, oo), meijerg=True) == 2 / rate ** 2 def E(expr): res1 = integrate(expr * exponential(x, rate) * normal(y, mu1, sigma1), (x, 0, oo), (y, -oo, oo), meijerg=True) res2 = integrate(expr * exponential(x, rate) * normal(y, mu1, sigma1), (y, -oo, oo), (x, 0, oo), meijerg=True) assert expand_mul(res1) == expand_mul(res2) return res1 assert E(1) == 1 assert E(x * y) == mu1 / rate assert E(x * y ** 2) == mu1 ** 2 / rate + sigma1 ** 2 / rate ans = sigma1 ** 2 + 1 / rate ** 2 assert simplify(E((x + y + 1) ** 2) - E(x + y + 1) ** 2) == ans assert simplify(E((x + y - 1) ** 2) - E(x + y - 1) ** 2) == ans assert simplify(E((x + y) ** 2) - E(x + y) ** 2) == ans # Beta' distribution alpha, beta = symbols("alpha beta", positive=True) betadist = x ** (alpha - 1) * (1 + x) ** (-alpha - beta) * gamma(alpha + beta) / gamma(alpha) / gamma(beta) assert integrate(betadist, (x, 0, oo), meijerg=True) == 1 i = integrate(x * betadist, (x, 0, oo), meijerg=True, conds="separate") assert (combsimp(i[0]), i[1]) == (alpha / (beta - 1), 1 < beta) j = integrate(x ** 2 * betadist, (x, 0, oo), meijerg=True, conds="separate") assert j[1] == (1 < beta - 1) assert combsimp(j[0] - i[0] ** 2) == (alpha + beta - 1) * alpha / (beta - 2) / (beta - 1) ** 2 # Beta distribution # NOTE: this is evaluated using antiderivatives. It also tests that # meijerint_indefinite returns the simplest possible answer. a, b = symbols("a b", positive=True) betadist = x ** (a - 1) * (-x + 1) ** (b - 1) * gamma(a + b) / (gamma(a) * gamma(b)) assert simplify(integrate(betadist, (x, 0, 1), meijerg=True)) == 1 assert simplify(integrate(x * betadist, (x, 0, 1), meijerg=True)) == a / (a + b) assert simplify(integrate(x ** 2 * betadist, (x, 0, 1), meijerg=True)) == a * (a + 1) / (a + b) / (a + b + 1) assert simplify(integrate(x ** y * betadist, (x, 0, 1), meijerg=True)) == gamma(a + b) * gamma(a + y) / gamma( a ) / gamma(a + b + y) # Chi distribution k = Symbol("k", integer=True, positive=True) chi = 2 ** (1 - k / 2) * x ** (k - 1) * exp(-x ** 2 / 2) / gamma(k / 2) assert powsimp(integrate(chi, (x, 0, oo), meijerg=True)) == 1 assert simplify(integrate(x * chi, (x, 0, oo), meijerg=True)) == sqrt(2) * gamma((k + 1) / 2) / gamma(k / 2) assert simplify(integrate(x ** 2 * chi, (x, 0, oo), meijerg=True)) == k # Chi^2 distribution chisquared = 2 ** (-k / 2) / gamma(k / 2) * x ** (k / 2 - 1) * exp(-x / 2) assert powsimp(integrate(chisquared, (x, 0, oo), meijerg=True)) == 1 assert simplify(integrate(x * chisquared, (x, 0, oo), meijerg=True)) == k assert simplify(integrate(x ** 2 * chisquared, (x, 0, oo), meijerg=True)) == k * (k + 2) assert combsimp(integrate(((x - k) / sqrt(2 * k)) ** 3 * chisquared, (x, 0, oo), meijerg=True)) == 2 * sqrt( 2 ) / sqrt(k) # Dagum distribution a, b, p = symbols("a b p", positive=True) # XXX (x/b)**a does not work dagum = a * p / x * (x / b) ** (a * p) / (1 + x ** a / b ** a) ** (p + 1) assert simplify(integrate(dagum, (x, 0, oo), meijerg=True)) == 1 # XXX conditions are a mess arg = x * dagum assert simplify(integrate(arg, (x, 0, oo), meijerg=True, conds="none")) == a * b * gamma(1 - 1 / a) * gamma( p + 1 + 1 / a ) / ((a * p + 1) * gamma(p)) assert simplify(integrate(x * arg, (x, 0, oo), meijerg=True, conds="none")) == a * b ** 2 * gamma( 1 - 2 / a ) * gamma(p + 1 + 2 / a) / ((a * p + 2) * gamma(p)) # F-distribution d1, d2 = symbols("d1 d2", positive=True) f = ( sqrt(((d1 * x) ** d1 * d2 ** d2) / (d1 * x + d2) ** (d1 + d2)) / x / gamma(d1 / 2) / gamma(d2 / 2) * gamma((d1 + d2) / 2) ) assert simplify(integrate(f, (x, 0, oo), meijerg=True)) == 1 # TODO conditions are a mess assert simplify(integrate(x * f, (x, 0, oo), meijerg=True, conds="none")) == d2 / (d2 - 2) assert simplify(integrate(x ** 2 * f, (x, 0, oo), meijerg=True, conds="none")) == d2 ** 2 * (d1 + 2) / d1 / ( d2 - 4 ) / (d2 - 2) # TODO gamma, rayleigh # inverse gaussian lamda, mu = symbols("lamda mu", positive=True) dist = sqrt(lamda / 2 / pi) * x ** (-S(3) / 2) * exp(-lamda * (x - mu) ** 2 / x / 2 / mu ** 2) mysimp = lambda expr: simplify(expr.rewrite(exp)) assert mysimp(integrate(dist, (x, 0, oo))) == 1 assert mysimp(integrate(x * dist, (x, 0, oo))) == mu assert mysimp(integrate((x - mu) ** 2 * dist, (x, 0, oo))) == mu ** 3 / lamda assert mysimp(integrate((x - mu) ** 3 * dist, (x, 0, oo))) == 3 * mu ** 5 / lamda ** 2 # Levi c = Symbol("c", positive=True) assert integrate(sqrt(c / 2 / pi) * exp(-c / 2 / (x - mu)) / (x - mu) ** S("3/2"), (x, mu, oo)) == 1 # higher moments oo # log-logistic distn = (beta / alpha) * x ** (beta - 1) / alpha ** (beta - 1) / (1 + x ** beta / alpha ** beta) ** 2 assert simplify(integrate(distn, (x, 0, oo))) == 1 # NOTE the conditions are a mess, but correctly state beta > 1 assert simplify(integrate(x * distn, (x, 0, oo), conds="none")) == pi * alpha / beta / sin(pi / beta) # (similar comment for conditions applies) assert simplify(integrate(x ** y * distn, (x, 0, oo), conds="none")) == pi * alpha ** y * y / beta / sin( pi * y / beta ) # weibull k = Symbol("k", positive=True) n = Symbol("n", positive=True) distn = k / lamda * (x / lamda) ** (k - 1) * exp(-(x / lamda) ** k) assert simplify(integrate(distn, (x, 0, oo))) == 1 assert simplify(integrate(x ** n * distn, (x, 0, oo))) == lamda ** n * gamma(1 + n / k) # rice distribution from sympy import besseli nu, sigma = symbols("nu sigma", positive=True) rice = x / sigma ** 2 * exp(-(x ** 2 + nu ** 2) / 2 / sigma ** 2) * besseli(0, x * nu / sigma ** 2) assert integrate(rice, (x, 0, oo), meijerg=True) == 1 # can someone verify higher moments? # Laplace distribution mu = Symbol("mu", real=True) b = Symbol("b", positive=True) laplace = exp(-abs(x - mu) / b) / 2 / b assert integrate(laplace, (x, -oo, oo), meijerg=True) == 1 assert integrate(x * laplace, (x, -oo, oo), meijerg=True) == mu assert integrate(x ** 2 * laplace, (x, -oo, oo), meijerg=True) == 2 * b ** 2 + mu ** 2 # TODO are there other distributions supported on (-oo, oo) that we can do? # misc tests k = Symbol("k", positive=True) assert combsimp(expand_mul(integrate(log(x) * x ** (k - 1) * exp(-x) / gamma(k), (x, 0, oo)))) == polygamma(0, k)
def test_probability(): # various integrals from probability theory from sympy.abc import x, y, z from sympy import symbols, Symbol, Abs, expand_mul, combsimp, powsimp, sin mu1, mu2 = symbols('mu1 mu2', real=True, finite=True, bounded=True) sigma1, sigma2 = symbols('sigma1 sigma2', real=True, finite=True, bounded=True, positive=True) rate = Symbol('lambda', real=True, positive=True, bounded=True) def normal(x, mu, sigma): return 1 / sqrt(2 * pi * sigma**2) * exp(-(x - mu)**2 / 2 / sigma**2) def exponential(x, rate): return rate * exp(-rate * x) assert integrate(normal(x, mu1, sigma1), (x, -oo, oo), meijerg=True) == 1 assert integrate(x*normal(x, mu1, sigma1), (x, -oo, oo), meijerg=True) == \ mu1 assert integrate(x**2*normal(x, mu1, sigma1), (x, -oo, oo), meijerg=True) \ == mu1**2 + sigma1**2 assert integrate(x**3*normal(x, mu1, sigma1), (x, -oo, oo), meijerg=True) \ == mu1**3 + 3*mu1*sigma1**2 assert integrate(normal(x, mu1, sigma1) * normal(y, mu2, sigma2), (x, -oo, oo), (y, -oo, oo), meijerg=True) == 1 assert integrate(x * normal(x, mu1, sigma1) * normal(y, mu2, sigma2), (x, -oo, oo), (y, -oo, oo), meijerg=True) == mu1 assert integrate(y * normal(x, mu1, sigma1) * normal(y, mu2, sigma2), (x, -oo, oo), (y, -oo, oo), meijerg=True) == mu2 assert integrate(x * y * normal(x, mu1, sigma1) * normal(y, mu2, sigma2), (x, -oo, oo), (y, -oo, oo), meijerg=True) == mu1 * mu2 assert integrate( (x + y + 1) * normal(x, mu1, sigma1) * normal(y, mu2, sigma2), (x, -oo, oo), (y, -oo, oo), meijerg=True) == 1 + mu1 + mu2 assert integrate((x + y - 1)*normal(x, mu1, sigma1)*normal(y, mu2, sigma2), (x, -oo, oo), (y, -oo, oo), meijerg=True) == \ -1 + mu1 + mu2 i = integrate(x**2 * normal(x, mu1, sigma1) * normal(y, mu2, sigma2), (x, -oo, oo), (y, -oo, oo), meijerg=True) assert not i.has(Abs) assert simplify(i) == mu1**2 + sigma1**2 assert integrate(y**2*normal(x, mu1, sigma1)*normal(y, mu2, sigma2), (x, -oo, oo), (y, -oo, oo), meijerg=True) == \ sigma2**2 + mu2**2 assert integrate(exponential(x, rate), (x, 0, oo), meijerg=True) == 1 assert integrate(x*exponential(x, rate), (x, 0, oo), meijerg=True) == \ 1/rate assert integrate(x**2*exponential(x, rate), (x, 0, oo), meijerg=True) == \ 2/rate**2 def E(expr): res1 = integrate(expr * exponential(x, rate) * normal(y, mu1, sigma1), (x, 0, oo), (y, -oo, oo), meijerg=True) res2 = integrate(expr * exponential(x, rate) * normal(y, mu1, sigma1), (y, -oo, oo), (x, 0, oo), meijerg=True) assert expand_mul(res1) == expand_mul(res2) return res1 assert E(1) == 1 assert E(x * y) == mu1 / rate assert E(x * y**2) == mu1**2 / rate + sigma1**2 / rate ans = sigma1**2 + 1 / rate**2 assert simplify(E((x + y + 1)**2) - E(x + y + 1)**2) == ans assert simplify(E((x + y - 1)**2) - E(x + y - 1)**2) == ans assert simplify(E((x + y)**2) - E(x + y)**2) == ans # Beta' distribution alpha, beta = symbols('alpha beta', positive=True) betadist = x**(alpha - 1)*(1 + x)**(-alpha - beta)*gamma(alpha + beta) \ /gamma(alpha)/gamma(beta) assert integrate(betadist, (x, 0, oo), meijerg=True) == 1 i = integrate(x * betadist, (x, 0, oo), meijerg=True, conds='separate') assert (combsimp(i[0]), i[1]) == (alpha / (beta - 1), 1 < beta) j = integrate(x**2 * betadist, (x, 0, oo), meijerg=True, conds='separate') assert j[1] == (1 < beta - 1) assert combsimp(j[0] - i[0]**2) == (alpha + beta - 1)*alpha \ /(beta - 2)/(beta - 1)**2 # Beta distribution # NOTE: this is evaluated using antiderivatives. It also tests that # meijerint_indefinite returns the simplest possible answer. a, b = symbols('a b', positive=True) betadist = x**(a - 1) * (-x + 1)**(b - 1) * gamma(a + b) / (gamma(a) * gamma(b)) assert simplify(integrate(betadist, (x, 0, 1), meijerg=True)) == 1 assert simplify(integrate(x*betadist, (x, 0, 1), meijerg=True)) == \ a/(a + b) assert simplify(integrate(x**2*betadist, (x, 0, 1), meijerg=True)) == \ a*(a + 1)/(a + b)/(a + b + 1) assert simplify(integrate(x**y*betadist, (x, 0, 1), meijerg=True)) == \ gamma(a + b)*gamma(a + y)/gamma(a)/gamma(a + b + y) # Chi distribution k = Symbol('k', integer=True, positive=True) chi = 2**(1 - k / 2) * x**(k - 1) * exp(-x**2 / 2) / gamma(k / 2) assert powsimp(integrate(chi, (x, 0, oo), meijerg=True)) == 1 assert simplify(integrate(x*chi, (x, 0, oo), meijerg=True)) == \ sqrt(2)*gamma((k + 1)/2)/gamma(k/2) assert simplify(integrate(x**2 * chi, (x, 0, oo), meijerg=True)) == k # Chi^2 distribution chisquared = 2**(-k / 2) / gamma(k / 2) * x**(k / 2 - 1) * exp(-x / 2) assert powsimp(integrate(chisquared, (x, 0, oo), meijerg=True)) == 1 assert simplify(integrate(x * chisquared, (x, 0, oo), meijerg=True)) == k assert simplify(integrate(x**2*chisquared, (x, 0, oo), meijerg=True)) == \ k*(k + 2) assert combsimp( integrate(((x - k) / sqrt(2 * k))**3 * chisquared, (x, 0, oo), meijerg=True)) == 2 * sqrt(2) / sqrt(k) # Dagum distribution a, b, p = symbols('a b p', positive=True) # XXX (x/b)**a does not work dagum = a * p / x * (x / b)**(a * p) / (1 + x**a / b**a)**(p + 1) assert simplify(integrate(dagum, (x, 0, oo), meijerg=True)) == 1 # XXX conditions are a mess arg = x * dagum assert simplify(integrate( arg, (x, 0, oo), meijerg=True, conds='none')) == a * b * gamma(1 - 1 / a) * gamma(p + 1 + 1 / a) / ( (a * p + 1) * gamma(p)) assert simplify(integrate( x * arg, (x, 0, oo), meijerg=True, conds='none')) == a * b**2 * gamma(1 - 2 / a) * gamma(p + 1 + 2 / a) / ( (a * p + 2) * gamma(p)) # F-distribution d1, d2 = symbols('d1 d2', positive=True) f = sqrt(((d1*x)**d1 * d2**d2)/(d1*x + d2)**(d1 + d2))/x \ /gamma(d1/2)/gamma(d2/2)*gamma((d1 + d2)/2) assert simplify(integrate(f, (x, 0, oo), meijerg=True)) == 1 # TODO conditions are a mess assert simplify(integrate(x * f, (x, 0, oo), meijerg=True, conds='none')) == d2 / (d2 - 2) assert simplify( integrate(x**2 * f, (x, 0, oo), meijerg=True, conds='none')) == d2**2 * (d1 + 2) / d1 / (d2 - 4) / (d2 - 2) # TODO gamma, rayleigh # inverse gaussian lamda, mu = symbols('lamda mu', positive=True) dist = sqrt(lamda / 2 / pi) * x**(-S(3) / 2) * exp( -lamda * (x - mu)**2 / x / 2 / mu**2) mysimp = lambda expr: simplify(expr.rewrite(exp)) assert mysimp(integrate(dist, (x, 0, oo))) == 1 assert mysimp(integrate(x * dist, (x, 0, oo))) == mu assert mysimp(integrate((x - mu)**2 * dist, (x, 0, oo))) == mu**3 / lamda assert mysimp(integrate((x - mu)**3 * dist, (x, 0, oo))) == 3 * mu**5 / lamda**2 # Levi c = Symbol('c', positive=True) assert integrate( sqrt(c / 2 / pi) * exp(-c / 2 / (x - mu)) / (x - mu)**S('3/2'), (x, mu, oo)) == 1 # higher moments oo # log-logistic distn = (beta/alpha)*x**(beta - 1)/alpha**(beta - 1)/ \ (1 + x**beta/alpha**beta)**2 assert simplify(integrate(distn, (x, 0, oo))) == 1 # NOTE the conditions are a mess, but correctly state beta > 1 assert simplify(integrate(x*distn, (x, 0, oo), conds='none')) == \ pi*alpha/beta/sin(pi/beta) # (similar comment for conditions applies) assert simplify(integrate(x**y*distn, (x, 0, oo), conds='none')) == \ pi*alpha**y*y/beta/sin(pi*y/beta) # weibull k = Symbol('k', positive=True) n = Symbol('n', positive=True) distn = k / lamda * (x / lamda)**(k - 1) * exp(-(x / lamda)**k) assert simplify(integrate(distn, (x, 0, oo))) == 1 assert simplify(integrate(x**n*distn, (x, 0, oo))) == \ lamda**n*gamma(1 + n/k) # rice distribution from sympy import besseli nu, sigma = symbols('nu sigma', positive=True) rice = x / sigma**2 * exp(-(x**2 + nu**2) / 2 / sigma**2) * besseli( 0, x * nu / sigma**2) assert integrate(rice, (x, 0, oo), meijerg=True) == 1 # can someone verify higher moments? # Laplace distribution mu = Symbol('mu', real=True) b = Symbol('b', positive=True) laplace = exp(-abs(x - mu) / b) / 2 / b assert integrate(laplace, (x, -oo, oo), meijerg=True) == 1 assert integrate(x * laplace, (x, -oo, oo), meijerg=True) == mu assert integrate(x**2*laplace, (x, -oo, oo), meijerg=True) == \ 2*b**2 + mu**2 # TODO are there other distributions supported on (-oo, oo) that we can do? # misc tests k = Symbol('k', positive=True) assert combsimp( expand_mul( integrate(log(x) * x**(k - 1) * exp(-x) / gamma(k), (x, 0, oo)))) == polygamma(0, k)
def test_probability(): # various integrals from probability theory from sympy.abc import x, y, z from sympy import symbols, Symbol, Abs, expand_mul, combsimp, powsimp mu1, mu2 = symbols('mu1 mu2', real=True, finite=True, bounded=True) sigma1, sigma2 = symbols('sigma1 sigma2', real=True, finite=True, bounded=True, positive=True) rate = Symbol('lambda', real=True, positive=True, bounded=True) def normal(x, mu, sigma): return 1/sqrt(2*pi*sigma**2)*exp(-(x-mu)**2/2/sigma**2) def exponential(x, rate): return rate*exp(-rate*x) assert integrate(normal(x, mu1, sigma1), (x, -oo, oo), meijerg=True) == 1 assert integrate(x*normal(x, mu1, sigma1), (x, -oo, oo), meijerg=True) == mu1 assert integrate(x**2*normal(x, mu1, sigma1), (x, -oo, oo), meijerg=True) \ == mu1**2 + sigma1**2 assert integrate(x**3*normal(x, mu1, sigma1), (x, -oo, oo), meijerg=True) \ == mu1**3 + 3*mu1*sigma1**2 assert integrate(normal(x, mu1, sigma1)*normal(y, mu2, sigma2), (x, -oo, oo), (y, -oo, oo), meijerg=True) == 1 assert integrate(x*normal(x, mu1, sigma1)*normal(y, mu2, sigma2), (x, -oo, oo), (y, -oo, oo), meijerg=True) == mu1 assert integrate(y*normal(x, mu1, sigma1)*normal(y, mu2, sigma2), (x, -oo, oo), (y, -oo, oo), meijerg=True) == mu2 assert integrate(x*y*normal(x, mu1, sigma1)*normal(y, mu2, sigma2), (x, -oo, oo), (y, -oo, oo), meijerg=True) == mu1*mu2 assert integrate((x+y+1)*normal(x, mu1, sigma1)*normal(y, mu2, sigma2), (x, -oo, oo), (y, -oo, oo), meijerg=True) == 1 + mu1 + mu2 assert integrate((x+y-1)*normal(x, mu1, sigma1)*normal(y, mu2, sigma2), (x, -oo, oo), (y, -oo, oo), meijerg=True) == -1 + mu1 + mu2 i = integrate(x**2*normal(x, mu1, sigma1)*normal(y, mu2, sigma2), (x, -oo, oo), (y, -oo, oo), meijerg=True) assert not i.has(Abs) assert simplify(i) == mu1**2 + sigma1**2 assert integrate(y**2*normal(x, mu1, sigma1)*normal(y, mu2, sigma2), (x, -oo, oo), (y, -oo, oo), meijerg=True) == \ sigma2**2 + mu2**2 assert integrate(exponential(x, rate), (x, 0, oo), meijerg=True) == 1 assert integrate(x*exponential(x, rate), (x, 0, oo), meijerg=True) == 1/rate assert integrate(x**2*exponential(x, rate), (x, 0, oo), meijerg=True) \ == 2/rate**2 def E(expr): res1 = integrate(expr*exponential(x, rate)*normal(y, mu1, sigma1), (x, 0, oo), (y, -oo, oo), meijerg=True) res2 = integrate(expr*exponential(x, rate)*normal(y, mu1, sigma1), (y, -oo, oo), (x, 0, oo), meijerg=True) assert expand_mul(res1) == expand_mul(res2) return res1 assert E(1) == 1 assert E(x*y) == mu1/rate assert E(x*y**2) == mu1**2/rate + sigma1**2/rate assert simplify(E((x+y+1)**2) - E(x+y+1)**2) == (rate**2*sigma1**2 + 1)/rate**2 assert simplify(E((x+y-1)**2) - E(x+y-1)**2) == (rate**2*sigma1**2 + 1)/rate**2 assert simplify(E((x+y)**2) - E(x+y)**2) == (rate**2*sigma1**2 + 1)/rate**2 # Beta' distribution alpha, beta = symbols('alpha beta', positive=True) betadist = x**(alpha-1)*(1+x)**(-alpha - beta)*gamma(alpha+beta) \ /gamma(alpha)/gamma(beta) assert integrate(betadist, (x, 0, oo), meijerg=True) == 1 i = integrate(x*betadist, (x, 0, oo), meijerg=True, conds='separate') assert (combsimp(i[0]), i[1]) == (alpha/(beta - 1), 1 < beta) j = integrate(x**2*betadist, (x, 0, oo), meijerg=True, conds='separate') assert j[1] == (1 < beta - 1) assert combsimp(j[0] - i[0]**2) == (alpha + beta - 1)*alpha \ /(beta - 2)/(beta - 1)**2 # Chi distribution k = Symbol('k', integer=True, positive=True) chi = 2**(1-k/2)*x**(k-1)*exp(-x**2/2)/gamma(k/2) assert powsimp(integrate(chi, (x, 0, oo), meijerg=True)) == 1 assert simplify(integrate(x*chi, (x, 0, oo), meijerg=True)) == \ sqrt(2)*gamma((k + 1)/2)/gamma(k/2) assert simplify(integrate(x**2*chi, (x, 0, oo), meijerg=True)) == k # Chi^2 distribution chisquared = 2**(-k/2)/gamma(k/2)*x**(k/2-1)*exp(-x/2) assert powsimp(integrate(chisquared, (x, 0, oo), meijerg=True)) == 1 assert simplify(integrate(x*chisquared, (x, 0, oo), meijerg=True)) == k assert simplify(integrate(x**2*chisquared, (x, 0, oo), meijerg=True)) == \ k*(k + 2) assert combsimp(integrate(((x-k)/sqrt(2*k))**3*chisquared, (x, 0, oo), meijerg=True)) == 2*sqrt(2)/sqrt(k) # Dagum distribution a, b, p = symbols('a b p', positive=True) # XXX (x/b)**a does not work dagum = a*p/x*(x/b)**(a*p)/(1 + x**a/b**a)**(p+1) assert simplify(integrate(dagum, (x, 0, oo), meijerg=True)) == 1 # XXX conditions are a mess assert simplify(integrate(x*dagum, (x, 0, oo), meijerg=True, conds='none')) \ == b*gamma(1 - 1/a)*gamma(p + 1/a)/gamma(p) assert simplify(integrate(x**2*dagum, (x, 0, oo), meijerg=True, conds='none')) \ == b**2*gamma(1 - 2/a)*gamma(p + 2/a)/gamma(p) # F-distribution d1, d2 = symbols('d1 d2', positive=True) f = sqrt(((d1*x)**d1 * d2**d2)/(d1*x + d2)**(d1+d2))/x \ /gamma(d1/2)/gamma(d2/2)*gamma((d1 + d2)/2) assert simplify(integrate(f, (x, 0, oo), meijerg=True)) == 1 # TODO conditions are a mess assert simplify(integrate(x*f, (x, 0, oo), meijerg=True, conds='none')) == \ d2/(d2 - 2) assert simplify(integrate(x**2*f, (x, 0, oo), meijerg=True, conds='none')) == \ d2**2*(d1 + 2)/d1/(d2 - 4)/(d2 - 2) # TODO gamma, inverse gaussian, Levi, log-logistic, rayleigh, weibull # rice distribution from sympy import besseli nu, sigma = symbols('nu sigma', positive=True) rice = x/sigma**2*exp(-(x**2+ nu**2)/2/sigma**2)*besseli(0, x*nu/sigma**2) assert integrate(rice, (x, 0, oo), meijerg=True) == 1 # can someone verify higher moments? # Laplace distribution mu = Symbol('mu', real=True) b = Symbol('b', positive=True) laplace = exp(-abs(x-mu)/b)/2/b assert integrate(laplace, (x, -oo, oo), meijerg=True) == 1 assert integrate(x*laplace, (x, -oo, oo), meijerg=True) == mu assert integrate(x**2*laplace, (x, -oo, oo), meijerg=True) == 2*b**2 + mu**2 # TODO are there other distributions supported on (-oo, oo) that we can do? # misc tests k = Symbol('k', positive=True) assert combsimp(expand_mul(integrate(log(x) * x**(k-1) * exp(-x) / gamma(k), (x, 0, oo)))) == polygamma(0, k)
def test_probability(): # various integrals from probability theory from sympy.abc import x, y from sympy import symbols, Symbol, Abs, expand_mul, gammasimp, powsimp, sin mu1, mu2 = symbols("mu1 mu2", nonzero=True) sigma1, sigma2 = symbols("sigma1 sigma2", positive=True) rate = Symbol("lambda", positive=True) def normal(x, mu, sigma): return 1 / sqrt(2 * pi * sigma ** 2) * exp(-((x - mu) ** 2) / 2 / sigma ** 2) def exponential(x, rate): return rate * exp(-rate * x) assert integrate(normal(x, mu1, sigma1), (x, -oo, oo), meijerg=True) == 1 assert integrate(x * normal(x, mu1, sigma1), (x, -oo, oo), meijerg=True) == mu1 assert ( integrate(x ** 2 * normal(x, mu1, sigma1), (x, -oo, oo), meijerg=True) == mu1 ** 2 + sigma1 ** 2 ) assert ( integrate(x ** 3 * normal(x, mu1, sigma1), (x, -oo, oo), meijerg=True) == mu1 ** 3 + 3 * mu1 * sigma1 ** 2 ) assert ( integrate( normal(x, mu1, sigma1) * normal(y, mu2, sigma2), (x, -oo, oo), (y, -oo, oo), meijerg=True, ) == 1 ) assert ( integrate( x * normal(x, mu1, sigma1) * normal(y, mu2, sigma2), (x, -oo, oo), (y, -oo, oo), meijerg=True, ) == mu1 ) assert ( integrate( y * normal(x, mu1, sigma1) * normal(y, mu2, sigma2), (x, -oo, oo), (y, -oo, oo), meijerg=True, ) == mu2 ) assert ( integrate( x * y * normal(x, mu1, sigma1) * normal(y, mu2, sigma2), (x, -oo, oo), (y, -oo, oo), meijerg=True, ) == mu1 * mu2 ) assert ( integrate( (x + y + 1) * normal(x, mu1, sigma1) * normal(y, mu2, sigma2), (x, -oo, oo), (y, -oo, oo), meijerg=True, ) == 1 + mu1 + mu2 ) assert ( integrate( (x + y - 1) * normal(x, mu1, sigma1) * normal(y, mu2, sigma2), (x, -oo, oo), (y, -oo, oo), meijerg=True, ) == -1 + mu1 + mu2 ) i = integrate( x ** 2 * normal(x, mu1, sigma1) * normal(y, mu2, sigma2), (x, -oo, oo), (y, -oo, oo), meijerg=True, ) assert not i.has(Abs) assert simplify(i) == mu1 ** 2 + sigma1 ** 2 assert ( integrate( y ** 2 * normal(x, mu1, sigma1) * normal(y, mu2, sigma2), (x, -oo, oo), (y, -oo, oo), meijerg=True, ) == sigma2 ** 2 + mu2 ** 2 ) assert integrate(exponential(x, rate), (x, 0, oo), meijerg=True) == 1 assert integrate(x * exponential(x, rate), (x, 0, oo), meijerg=True) == 1 / rate assert ( integrate(x ** 2 * exponential(x, rate), (x, 0, oo), meijerg=True) == 2 / rate ** 2 ) def E(expr): res1 = integrate( expr * exponential(x, rate) * normal(y, mu1, sigma1), (x, 0, oo), (y, -oo, oo), meijerg=True, ) res2 = integrate( expr * exponential(x, rate) * normal(y, mu1, sigma1), (y, -oo, oo), (x, 0, oo), meijerg=True, ) assert expand_mul(res1) == expand_mul(res2) return res1 assert E(1) == 1 assert E(x * y) == mu1 / rate assert E(x * y ** 2) == mu1 ** 2 / rate + sigma1 ** 2 / rate ans = sigma1 ** 2 + 1 / rate ** 2 assert simplify(E((x + y + 1) ** 2) - E(x + y + 1) ** 2) == ans assert simplify(E((x + y - 1) ** 2) - E(x + y - 1) ** 2) == ans assert simplify(E((x + y) ** 2) - E(x + y) ** 2) == ans # Beta' distribution alpha, beta = symbols("alpha beta", positive=True) betadist = ( x ** (alpha - 1) * (1 + x) ** (-alpha - beta) * gamma(alpha + beta) / gamma(alpha) / gamma(beta) ) assert integrate(betadist, (x, 0, oo), meijerg=True) == 1 i = integrate(x * betadist, (x, 0, oo), meijerg=True, conds="separate") assert (gammasimp(i[0]), i[1]) == (alpha / (beta - 1), 1 < beta) j = integrate(x ** 2 * betadist, (x, 0, oo), meijerg=True, conds="separate") assert j[1] == (1 < beta - 1) assert ( gammasimp(j[0] - i[0] ** 2) == (alpha + beta - 1) * alpha / (beta - 2) / (beta - 1) ** 2 ) # Beta distribution # NOTE: this is evaluated using antiderivatives. It also tests that # meijerint_indefinite returns the simplest possible answer. a, b = symbols("a b", positive=True) betadist = x ** (a - 1) * (-x + 1) ** (b - 1) * gamma(a + b) / (gamma(a) * gamma(b)) assert simplify(integrate(betadist, (x, 0, 1), meijerg=True)) == 1 assert simplify(integrate(x * betadist, (x, 0, 1), meijerg=True)) == a / (a + b) assert simplify(integrate(x ** 2 * betadist, (x, 0, 1), meijerg=True)) == a * ( a + 1 ) / (a + b) / (a + b + 1) assert simplify(integrate(x ** y * betadist, (x, 0, 1), meijerg=True)) == gamma( a + b ) * gamma(a + y) / gamma(a) / gamma(a + b + y) # Chi distribution k = Symbol("k", integer=True, positive=True) chi = 2 ** (1 - k / 2) * x ** (k - 1) * exp(-(x ** 2) / 2) / gamma(k / 2) assert powsimp(integrate(chi, (x, 0, oo), meijerg=True)) == 1 assert simplify(integrate(x * chi, (x, 0, oo), meijerg=True)) == sqrt(2) * gamma( (k + 1) / 2 ) / gamma(k / 2) assert simplify(integrate(x ** 2 * chi, (x, 0, oo), meijerg=True)) == k # Chi^2 distribution chisquared = 2 ** (-k / 2) / gamma(k / 2) * x ** (k / 2 - 1) * exp(-x / 2) assert powsimp(integrate(chisquared, (x, 0, oo), meijerg=True)) == 1 assert simplify(integrate(x * chisquared, (x, 0, oo), meijerg=True)) == k assert simplify(integrate(x ** 2 * chisquared, (x, 0, oo), meijerg=True)) == k * ( k + 2 ) assert gammasimp( integrate(((x - k) / sqrt(2 * k)) ** 3 * chisquared, (x, 0, oo), meijerg=True) ) == 2 * sqrt(2) / sqrt(k) # Dagum distribution a, b, p = symbols("a b p", positive=True) # XXX (x/b)**a does not work dagum = a * p / x * (x / b) ** (a * p) / (1 + x ** a / b ** a) ** (p + 1) assert simplify(integrate(dagum, (x, 0, oo), meijerg=True)) == 1 # XXX conditions are a mess arg = x * dagum assert simplify( integrate(arg, (x, 0, oo), meijerg=True, conds="none") ) == a * b * gamma(1 - 1 / a) * gamma(p + 1 + 1 / a) / ((a * p + 1) * gamma(p)) assert simplify( integrate(x * arg, (x, 0, oo), meijerg=True, conds="none") ) == a * b ** 2 * gamma(1 - 2 / a) * gamma(p + 1 + 2 / a) / ((a * p + 2) * gamma(p)) # F-distribution d1, d2 = symbols("d1 d2", positive=True) f = ( sqrt(((d1 * x) ** d1 * d2 ** d2) / (d1 * x + d2) ** (d1 + d2)) / x / gamma(d1 / 2) / gamma(d2 / 2) * gamma((d1 + d2) / 2) ) assert simplify(integrate(f, (x, 0, oo), meijerg=True)) == 1 # TODO conditions are a mess assert simplify(integrate(x * f, (x, 0, oo), meijerg=True, conds="none")) == d2 / ( d2 - 2 ) assert simplify( integrate(x ** 2 * f, (x, 0, oo), meijerg=True, conds="none") ) == d2 ** 2 * (d1 + 2) / d1 / (d2 - 4) / (d2 - 2) # TODO gamma, rayleigh # inverse gaussian lamda, mu = symbols("lamda mu", positive=True) dist = ( sqrt(lamda / 2 / pi) * x ** (Rational(-3, 2)) * exp(-lamda * (x - mu) ** 2 / x / 2 / mu ** 2) ) mysimp = lambda expr: simplify(expr.rewrite(exp)) assert mysimp(integrate(dist, (x, 0, oo))) == 1 assert mysimp(integrate(x * dist, (x, 0, oo))) == mu assert mysimp(integrate((x - mu) ** 2 * dist, (x, 0, oo))) == mu ** 3 / lamda assert ( mysimp(integrate((x - mu) ** 3 * dist, (x, 0, oo))) == 3 * mu ** 5 / lamda ** 2 ) # Levi c = Symbol("c", positive=True) assert ( integrate( sqrt(c / 2 / pi) * exp(-c / 2 / (x - mu)) / (x - mu) ** S("3/2"), (x, mu, oo), ) == 1 ) # higher moments oo # log-logistic alpha, beta = symbols("alpha beta", positive=True) distn = ( (beta / alpha) * x ** (beta - 1) / alpha ** (beta - 1) / (1 + x ** beta / alpha ** beta) ** 2 ) # FIXME: If alpha, beta are not declared as finite the line below hangs # after the changes in: # https://github.com/sympy/sympy/pull/16603 assert simplify(integrate(distn, (x, 0, oo))) == 1 # NOTE the conditions are a mess, but correctly state beta > 1 assert simplify( integrate(x * distn, (x, 0, oo), conds="none") ) == pi * alpha / beta / sin(pi / beta) # (similar comment for conditions applies) assert simplify( integrate(x ** y * distn, (x, 0, oo), conds="none") ) == pi * alpha ** y * y / beta / sin(pi * y / beta) # weibull k = Symbol("k", positive=True) n = Symbol("n", positive=True) distn = k / lamda * (x / lamda) ** (k - 1) * exp(-((x / lamda) ** k)) assert simplify(integrate(distn, (x, 0, oo))) == 1 assert simplify(integrate(x ** n * distn, (x, 0, oo))) == lamda ** n * gamma( 1 + n / k ) # rice distribution from sympy import besseli nu, sigma = symbols("nu sigma", positive=True) rice = ( x / sigma ** 2 * exp(-(x ** 2 + nu ** 2) / 2 / sigma ** 2) * besseli(0, x * nu / sigma ** 2) ) assert integrate(rice, (x, 0, oo), meijerg=True) == 1 # can someone verify higher moments? # Laplace distribution mu = Symbol("mu", real=True) b = Symbol("b", positive=True) laplace = exp(-abs(x - mu) / b) / 2 / b assert integrate(laplace, (x, -oo, oo), meijerg=True) == 1 assert integrate(x * laplace, (x, -oo, oo), meijerg=True) == mu assert ( integrate(x ** 2 * laplace, (x, -oo, oo), meijerg=True) == 2 * b ** 2 + mu ** 2 ) # TODO are there other distributions supported on (-oo, oo) that we can do? # misc tests k = Symbol("k", positive=True) assert gammasimp( expand_mul(integrate(log(x) * x ** (k - 1) * exp(-x) / gamma(k), (x, 0, oo))) ) == polygamma(0, k)