def test_issue_9699():
    n, k = symbols('n k', real=True)
    x, y = symbols('x, y')
    assert combsimp((n + 1)*factorial(n)) == factorial(n + 1)
    assert combsimp((x + 1)*factorial(x)/gamma(y)) == gamma(x + 2)/gamma(y)
    assert combsimp(factorial(n)/n) == factorial(n - 1)
    assert combsimp(rf(x + n, k)*binomial(n, k)) == binomial(n, k)*gamma(k + n + x)/gamma(n + x)
Exemple #2
0
def test_issue_9699():
    n, k = symbols('n k', real=True)
    x, y = symbols('x, y')
    assert combsimp((n + 1) * factorial(n)) == factorial(n + 1)
    assert combsimp(
        (x + 1) * factorial(x) / gamma(y)) == gamma(x + 2) / gamma(y)
    assert combsimp(factorial(n) / n) == factorial(n - 1)
    assert combsimp(
        rf(x + n, k) *
        binomial(n, k)) == binomial(n, k) * gamma(k + n + x) / gamma(n + x)
def test_lerchphi():
    from sympy import combsimp, exp_polar, polylog, log, lerchphi
    assert hyperexpand(hyper([1, a], [a + 1], z) / a) == lerchphi(z, 1, a)
    assert hyperexpand(hyper([1, a, a], [a + 1, a + 1], z) / a**2) == lerchphi(
        z, 2, a)
    assert hyperexpand(hyper([1, a, a, a], [a + 1, a + 1, a + 1], z)/a**3) == \
        lerchphi(z, 3, a)
    assert hyperexpand(hyper([1] + [a]*10, [a + 1]*10, z)/a**10) == \
        lerchphi(z, 10, a)
    assert combsimp(
        hyperexpand(meijerg([0, 1 - a], [], [0], [-a],
                            exp_polar(-I * pi) * z))) == lerchphi(z, 1, a)
    assert combsimp(
        hyperexpand(
            meijerg([0, 1 - a, 1 - a], [], [0], [-a, -a],
                    exp_polar(-I * pi) * z))) == lerchphi(z, 2, a)
    assert combsimp(
        hyperexpand(
            meijerg([0, 1 - a, 1 - a, 1 - a], [], [0], [-a, -a, -a],
                    exp_polar(-I * pi) * z))) == lerchphi(z, 3, a)

    assert hyperexpand(z * hyper([1, 1], [2], z)) == -log(1 + -z)
    assert hyperexpand(z * hyper([1, 1, 1], [2, 2], z)) == polylog(2, z)
    assert hyperexpand(z * hyper([1, 1, 1, 1], [2, 2, 2], z)) == polylog(3, z)

    assert hyperexpand(hyper([1, a, 1 + S(1)/2], [a + 1, S(1)/2], z)) == \
        -2*a/(z - 1) + (-2*a**2 + a)*lerchphi(z, 1, a)

    # Now numerical tests. These make sure reductions etc are carried out
    # correctly

    # a rational function (polylog at negative integer order)
    assert can_do([2, 2, 2], [1, 1])

    # NOTE these contain log(1-x) etc ... better make sure we have |z| < 1
    # reduction of order for polylog
    assert can_do([1, 1, 1, b + 5], [2, 2, b], div=10)

    # reduction of order for lerchphi
    # XXX lerchphi in mpmath is flaky
    assert can_do([1, a, a, a, b + 5], [a + 1, a + 1, a + 1, b],
                  numerical=False)

    # test a bug
    from sympy import Abs
    assert hyperexpand(hyper([S(1)/2, S(1)/2, S(1)/2, 1],
                             [S(3)/2, S(3)/2, S(3)/2], S(1)/4)) == \
        Abs(-polylog(3, exp_polar(I*pi)/2) + polylog(3, S(1)/2))
def test_meijerg_expand():
    from sympy import combsimp, simplify
    # from mpmath docs
    assert hyperexpand(meijerg([[], []], [[0], []], -z)) == exp(z)

    assert hyperexpand(meijerg([[1, 1], []], [[1], [0]], z)) == \
        log(z + 1)
    assert hyperexpand(meijerg([[1, 1], []], [[1], [1]], z)) == \
        z/(z + 1)
    assert hyperexpand(meijerg([[], []], [[S(1)/2], [0]], (z/2)**2)) \
           == sin(z)/sqrt(pi)
    assert hyperexpand(meijerg([[], []], [[0], [S(1)/2]], (z/2)**2)) \
           == cos(z)/sqrt(pi)
    assert can_do_meijer([], [a], [a - 1, a - S.Half], [])
    assert can_do_meijer([], [], [a/2], [-a/2], False)  # branches...
    assert can_do_meijer([a], [b], [a], [b, a - 1])

    # wikipedia
    assert hyperexpand(meijerg([1], [], [], [0], z)) == \
       Piecewise((0, abs(z) < 1), (1, abs(1/z) < 1),
                 (meijerg([1], [], [], [0], z), True))
    assert hyperexpand(meijerg([], [1], [0], [], z)) == \
       Piecewise((1, abs(z) < 1), (0, abs(1/z) < 1),
                 (meijerg([], [1], [0], [], z), True))

    # The Special Functions and their Approximations
    assert can_do_meijer([], [], [a + b/2], [a, a - b/2, a + S.Half])
    assert can_do_meijer(
        [], [], [a], [b], False)  # branches only agree for small z
    assert can_do_meijer([], [S.Half], [a], [-a])
    assert can_do_meijer([], [], [a, b], [])
    assert can_do_meijer([], [], [a, b], [])
    assert can_do_meijer([], [], [a, a + S.Half], [b, b + S.Half])
    assert can_do_meijer([], [], [a, -a], [0, S.Half], False)  # dito
    assert can_do_meijer([], [], [a, a + S.Half, b, b + S.Half], [])
    assert can_do_meijer([S.Half], [], [0], [a, -a])
    assert can_do_meijer([S.Half], [], [a], [0, -a], False)  # dito
    assert can_do_meijer([], [a - S.Half], [a, b], [a - S.Half], False)
    assert can_do_meijer([], [a + S.Half], [a + b, a - b, a], [], False)
    assert can_do_meijer([a + S.Half], [], [b, 2*a - b, a], [], False)

    # This for example is actually zero.
    assert can_do_meijer([], [], [], [a, b])

    # Testing a bug:
    assert hyperexpand(meijerg([0, 2], [], [], [-1, 1], z)) == \
        Piecewise((0, abs(z) < 1),
                  (z*(1 - 1/z**2)/2, abs(1/z) < 1),
                  (meijerg([0, 2], [], [], [-1, 1], z), True))

    # Test that the simplest possible answer is returned:
    assert combsimp(
        simplify(hyperexpand(meijerg([1], [1 - a], [-a/2, -a/2 + S(1)/2],
                                                 [], 1/z)))) == \
           -2*sqrt(pi)*(sqrt(z + 1) + 1)**a/a

    # Test that hyper is returned
    assert hyperexpand(meijerg([1], [], [a], [0, 0], z)) == \
           z**a*gamma(a)*hyper(
               (a,), (a + 1, a + 1), z*exp_polar(I*pi))/gamma(a + 1)**2
def test_lerchphi():
    from sympy import combsimp, exp_polar, polylog, log, lerchphi
    assert hyperexpand(hyper([1, a], [a + 1], z)/a) == lerchphi(z, 1, a)
    assert hyperexpand(
        hyper([1, a, a], [a + 1, a + 1], z)/a**2) == lerchphi(z, 2, a)
    assert hyperexpand(hyper([1, a, a, a], [a + 1, a + 1, a + 1], z)/a**3) == \
           lerchphi(z, 3, a)
    assert hyperexpand(hyper([1] + [a]*10, [a + 1]*10, z)/a**10) \
           == lerchphi(z, 10, a)
    assert combsimp(hyperexpand(meijerg([0, 1 - a], [], [0], [-a],
                    exp_polar(-I*pi)*z))) == \
           lerchphi(z, 1, a)
    assert combsimp(hyperexpand(meijerg([0, 1 - a, 1 - a], [], [0], [-a, -a],
                    exp_polar(-I*pi)*z))) == \
           lerchphi(z, 2, a)
    assert combsimp(
        hyperexpand(meijerg([0, 1 - a, 1 - a, 1 - a], [], [0], [-a, -a, -a],
                    exp_polar(-I*pi)*z))) == \
           lerchphi(z, 3, a)

    assert hyperexpand(z*hyper([1, 1], [2], z)) == -log(1 + -z)
    assert hyperexpand(z*hyper([1, 1, 1], [2, 2], z)) == polylog(2, z)
    assert hyperexpand(z*hyper([1, 1, 1, 1], [2, 2, 2], z)) == polylog(3, z)

    assert hyperexpand(hyper([1, a, 1 + S(1)/2], [a + 1, S(1)/2], z)) == \
           -2*a/(z - 1) + (-2*a**2 + a)*lerchphi(z, 1, a)

    # Now numerical tests. These make sure reductions etc are carried out
    # correctly

    # a rational function (polylog at negative integer order)
    assert can_do([2, 2, 2], [1, 1])

    # NOTE these contain log(1-x) etc ... better make sure we have |z| < 1
    # reduction of order for polylog
    assert can_do([1, 1, 1, b + 5], [2, 2, b], div=10)

    # reduction of order for lerchphi
    # XXX lerchphi in mpmath is flaky
    assert can_do(
        [1, a, a, a, b + 5], [a + 1, a + 1, a + 1, b], numerical=False)

    # test a bug
    from sympy import Abs
    assert hyperexpand(hyper([S(1)/2, S(1)/2, S(1)/2, 1],
                             [S(3)/2, S(3)/2, S(3)/2], S(1)/4)) == \
        Abs(-polylog(3, exp_polar(I*pi)/2) + polylog(3, S(1)/2))
Exemple #6
0
def test_meijerg_expand():
    from sympy import combsimp, simplify
    # from mpmath docs
    assert hyperexpand(meijerg([[], []], [[0], []], -z)) == exp(z)

    assert hyperexpand(meijerg([[1, 1], []], [[1], [0]], z)) == \
        log(z + 1)
    assert hyperexpand(meijerg([[1, 1], []], [[1], [1]], z)) == \
        z/(z + 1)
    assert hyperexpand(meijerg([[], []], [[S(1)/2], [0]], (z/2)**2)) \
        == sin(z)/sqrt(pi)
    assert hyperexpand(meijerg([[], []], [[0], [S(1)/2]], (z/2)**2)) \
        == cos(z)/sqrt(pi)
    assert can_do_meijer([], [a], [a - 1, a - S.Half], [])
    assert can_do_meijer([], [], [a / 2], [-a / 2], False)  # branches...
    assert can_do_meijer([a], [b], [a], [b, a - 1])

    # wikipedia
    assert hyperexpand(meijerg([1], [], [], [0], z)) == \
        Piecewise((0, abs(z) < 1), (1, abs(1/z) < 1),
                 (meijerg([1], [], [], [0], z), True))
    assert hyperexpand(meijerg([], [1], [0], [], z)) == \
        Piecewise((1, abs(z) < 1), (0, abs(1/z) < 1),
                 (meijerg([], [1], [0], [], z), True))

    # The Special Functions and their Approximations
    assert can_do_meijer([], [], [a + b / 2], [a, a - b / 2, a + S.Half])
    assert can_do_meijer([], [], [a], [b],
                         False)  # branches only agree for small z
    assert can_do_meijer([], [S.Half], [a], [-a])
    assert can_do_meijer([], [], [a, b], [])
    assert can_do_meijer([], [], [a, b], [])
    assert can_do_meijer([], [], [a, a + S.Half], [b, b + S.Half])
    assert can_do_meijer([], [], [a, -a], [0, S.Half], False)  # dito
    assert can_do_meijer([], [], [a, a + S.Half, b, b + S.Half], [])
    assert can_do_meijer([S.Half], [], [0], [a, -a])
    assert can_do_meijer([S.Half], [], [a], [0, -a], False)  # dito
    assert can_do_meijer([], [a - S.Half], [a, b], [a - S.Half], False)
    assert can_do_meijer([], [a + S.Half], [a + b, a - b, a], [], False)
    assert can_do_meijer([a + S.Half], [], [b, 2 * a - b, a], [], False)

    # This for example is actually zero.
    assert can_do_meijer([], [], [], [a, b])

    # Testing a bug:
    assert hyperexpand(meijerg([0, 2], [], [], [-1, 1], z)) == \
        Piecewise((0, abs(z) < 1),
                  (z/2 - 1/(2*z), abs(1/z) < 1),
                  (meijerg([0, 2], [], [], [-1, 1], z), True))

    # Test that the simplest possible answer is returned:
    assert combsimp(simplify(hyperexpand(
        meijerg([1], [1 - a], [-a/2, -a/2 + S(1)/2], [], 1/z)))) == \
        -2*sqrt(pi)*(sqrt(z + 1) + 1)**a/a

    # Test that hyper is returned
    assert hyperexpand(meijerg([1], [], [a], [0, 0], z)) == hyper(
        (a, ), (a + 1, a + 1),
        z * exp_polar(I * pi)) * z**a * gamma(a) / gamma(a + 1)**2
Exemple #7
0
 def mysimp(expr):
     return powsimp(
         powdenest(expand(
             unpolarify(
                 simplify(
                     expand(combsimp(expand_func(
                         expr.rewrite(besselj))))))),
                   polar=True))
Exemple #8
0
def grow_function_sym(N = Symbol("N"),k = 0):
    """
    @type k int
    """
    from sympy import factorial,summation,combsimp
    ksym = Symbol("ksym")

    def C(k,n):
        return factorial(n) / (factorial(k)*factorial(n-k))
    return combsimp(summation(C(ksym,N),(ksym,0,k-1)))
 def test_the_product(m, n):
     # g
     g = i**3 + 2 * i**2 - 3 * i
     # f = Delta g
     f = simplify(g.subs(i, i + 1) / g)
     # The product
     a = m
     b = n - 1
     P = Product(f, (i, a, b)).doit()
     # Test if Product_{m <= i < n} f(i) = g(n) / g(m)
     assert combsimp(P / (g.subs(i, n) / g.subs(i, m))) == 1
Exemple #10
0
def test_combsimp():
    from sympy.abc import n, k

    assert combsimp(factorial(n)) == factorial(n)
    assert combsimp(binomial(n, k)) == binomial(n, k)

    assert combsimp(factorial(n)/factorial(n - 3)) == n*(-1 + n)*(-2 + n)
    assert combsimp(binomial(n + 1, k + 1)/binomial(n, k)) == (1 + n)/(1 + k)

    assert combsimp(binomial(3*n + 4, n + 1)/binomial(3*n + 1, n)) == \
        S(3)/2*((3*n + 2)*(3*n + 4)/((n + 1)*(2*n + 3)))

    assert combsimp(factorial(n)**2/factorial(n - 3)) == factorial(n)*n*(-1 + n)*(-2 + n)
    assert combsimp(factorial(n)*binomial(n+1, k+1)/binomial(n, k)) == factorial(n)*(1 + n)/(1 + k)

    assert combsimp(binomial(n - 1, k)) == -((-n + k)*binomial(n, k))/n

    assert combsimp(binomial(n + 2, k + S(1)/2)) == \
        4*((n + 1)*(n + 2)*binomial(n, k + S(1)/2))/((2*k - 2*n - 1)*(2*k - 2*n - 3))
    assert combsimp(binomial(n + 2, k + 2.0)) == \
        -((1.0*n + 2.0)*binomial(n + 1.0, k + 2.0))/(k - n)
Exemple #11
0
def test_combsimp():
    from sympy.abc import n, k

    assert combsimp(factorial(n)) == factorial(n)
    assert combsimp(binomial(n, k)) == binomial(n, k)

    assert combsimp(factorial(n)/factorial(n - 3)) == n*(-1 + n)*(-2 + n)
    assert combsimp(binomial(n + 1, k + 1)/binomial(n, k)) == (1 + n)/(1 + k)

    assert combsimp(binomial(3*n + 4, n + 1)/binomial(3*n + 1, n)) == \
        S(3)/2*((3*n + 2)*(3*n + 4)/((n + 1)*(2*n + 3)))

    assert combsimp(factorial(n)**2/factorial(n - 3)) == factorial(n)*n*(-1 + n)*(-2 + n)
    assert combsimp(factorial(n)*binomial(n+1, k+1)/binomial(n, k)) == factorial(n)*(1 + n)/(1 + k)

    assert combsimp(binomial(n - 1, k)) == -((-n + k)*binomial(n, k))/n

    assert combsimp(binomial(n + 2, k + S(1)/2)) == \
        4*((n + 1)*(n + 2)*binomial(n, k + S(1)/2))/((2*k - 2*n - 1)*(2*k - 2*n - 3))
    assert combsimp(binomial(n + 2, k + 2.0)) == \
        -((1.0*n + 2.0)*binomial(n + 1.0, k + 2.0))/(k - n)
Exemple #12
0
def test_action_verbs():
    assert nsimplify((1/(exp(3*pi*x/5)+1))) == (1/(exp(3*pi*x/5)+1)).nsimplify()
    assert ratsimp(1/x + 1/y) == (1/x + 1/y).ratsimp()
    assert trigsimp(log(x), deep=True) == (log(x)).trigsimp(deep = True)
    assert radsimp(1/(2+sqrt(2))) == (1/(2+sqrt(2))).radsimp()
    assert powsimp(x**y*x**z*y**z, combine='all') == (x**y*x**z*y**z).powsimp(combine='all')
    assert simplify(x**y*x**z*y**z) == (x**y*x**z*y**z).simplify()
    assert together(1/x + 1/y) == (1/x + 1/y).together()
    assert separate((x*(y*z)**3)**2) == ((x*(y*z)**3)**2).separate()
    assert collect(a*x**2 + b*x**2 + a*x - b*x + c, x) == (a*x**2 + b*x**2 + a*x - b*x + c).collect(x)
    assert apart(y/(y+2)/(y+1), y) == (y/(y+2)/(y+1)).apart(y)
    assert combsimp(y/(x+2)/(x+1)) == (y/(x+2)/(x+1)).combsimp()
    assert factor(x**2+5*x+6) == (x**2+5*x+6).factor()
    assert refine(sqrt(x**2)) == sqrt(x**2).refine()
    assert cancel((x**2+5*x+6)/(x+2)) == ((x**2+5*x+6)/(x+2)).cancel()
Exemple #13
0
def test_action_verbs():
    assert nsimplify((1/(exp(3*pi*x/5)+1))) == (1/(exp(3*pi*x/5)+1)).nsimplify()
    assert ratsimp(1/x + 1/y) == (1/x + 1/y).ratsimp()
    assert trigsimp(log(x), deep=True) == (log(x)).trigsimp(deep = True)
    assert radsimp(1/(2+sqrt(2))) == (1/(2+sqrt(2))).radsimp()
    assert powsimp(x**y*x**z*y**z, combine='all') == (x**y*x**z*y**z).powsimp(combine='all')
    assert simplify(x**y*x**z*y**z) == (x**y*x**z*y**z).simplify()
    assert together(1/x + 1/y) == (1/x + 1/y).together()
    assert separate((x*(y*z)**3)**2) == ((x*(y*z)**3)**2).separate()
    assert collect(a*x**2 + b*x**2 + a*x - b*x + c, x) == (a*x**2 + b*x**2 + a*x - b*x + c).collect(x)
    assert apart(y/(y+2)/(y+1), y) == (y/(y+2)/(y+1)).apart(y)
    assert combsimp(y/(x+2)/(x+1)) == (y/(x+2)/(x+1)).combsimp()
    assert factor(x**2+5*x+6) == (x**2+5*x+6).factor()
    assert refine(sqrt(x**2)) == sqrt(x**2).refine()
    assert cancel((x**2+5*x+6)/(x+2)) == ((x**2+5*x+6)/(x+2)).cancel()
 def test_the_product(l, n, m):
     # Productmand
     s = i**3
     # First product
     a = l
     b = n - 1
     S1 = Product(s, (i, a, b)).doit()
     # Second product
     a = l
     b = m - 1
     S2 = Product(s, (i, a, b)).doit()
     # Third product
     a = m
     b = n - 1
     S3 = Product(s, (i, a, b)).doit()
     # Test if S1 = S2 * S3 as required
     assert combsimp(S1 / (S2 * S3)) == 1
Exemple #15
0
def test_issue_14528():
    p = symbols("p", integer=True, positive=True)
    assert combsimp(binomial(1, p)) == 1 / (factorial(p) * factorial(1 - p))
    assert combsimp(factorial(2 - p)) == factorial(2 - p)
Exemple #16
0
def test_F4():
    assert combsimp((2**n * factorial(n) * product(2*k - 1, (k, 1, n)))) == factorial(2*n)
Exemple #17
0
def test_F3():
    assert combsimp(2**n * factorial(n) * factorial2(2*n - 1)) == factorial(2*n)
Exemple #18
0
 def lie_combsimp(self, A, B):
     # Returns a combinatorially simplified presentation of self.lie
     return combsimp(self.lie(A, B))
Exemple #19
0
 def mysimp(expr):
     return powsimp(powdenest(expand(unpolarify(simplify(expand(combsimp(expand_func(expr.rewrite(besselj))))))), polar=True))
def test_combsimp():
    from sympy.abc import n, k

    assert combsimp(factorial(n)) == factorial(n)
    assert combsimp(binomial(n, k)) == binomial(n, k)

    assert combsimp(factorial(n)/factorial(n - 3)) == n*(-1 + n)*(-2 + n)
    assert combsimp(binomial(n + 1, k + 1)/binomial(n, k)) == (1 + n)/(1 + k)

    assert combsimp(binomial(3*n + 4, n + 1)/binomial(3*n + 1, n)) == \
        S(3)/2*((3*n + 2)*(3*n + 4)/((n + 1)*(2*n + 3)))

    assert combsimp(factorial(n)**2/factorial(n - 3)) == \
        factorial(n)*n*(-1 + n)*(-2 + n)
    assert combsimp(factorial(n)*binomial(n + 1, k + 1)/binomial(n, k)) == \
        factorial(n + 1)/(1 + k)

    assert combsimp(binomial(n - 1, k)) == -((-n + k)*binomial(n, k))/n

    assert combsimp(binomial(n + 2, k + S(1)/2)) == 4*((n + 1)*(n + 2) *
        binomial(n, k + S(1)/2))/((2*k - 2*n - 1)*(2*k - 2*n - 3))
    assert combsimp(binomial(n + 2, k + 2.0)) == \
        -((1.0*n + 2.0)*binomial(n + 1.0, k + 2.0))/(k - n)

    # coverage tests
    assert combsimp(factorial(n*(1 + n) - n**2 - n)) == 1
    assert combsimp(binomial(n + k - 2, n)) == \
        k*(k - 1)*binomial(n + k, n)/((n + k)*(n + k - 1))
    i = Symbol('i', integer=True)
    e = gamma(i + 3)
    assert combsimp(e) == e
    e = gamma(exp(i))
    assert combsimp(e) == e
    e = gamma(n + S(1)/3)*gamma(n + S(2)/3)
    assert combsimp(e) == e
    assert combsimp(gamma(4*n + S(1)/2)/gamma(2*n - S(3)/4)) == \
        2**(4*n - S(5)/2)*(8*n - 3)*gamma(2*n + S(3)/4)/sqrt(pi)

    assert combsimp(6*FallingFactorial(-4, n)/factorial(n)) == \
        (-1)**n*(n + 1)*(n + 2)*(n + 3)
    assert combsimp(6*FallingFactorial(-4, n - 1)/factorial(n - 1)) == \
        (-1)**(n - 1)*n*(n + 1)*(n + 2)
    assert combsimp(6*FallingFactorial(-4, n - 3)/factorial(n - 3)) == \
        (-1)**(n - 3)*n*(n - 1)*(n - 2)
    assert combsimp(6*FallingFactorial(-4, -n - 1)/factorial(-n - 1)) == \
        -(-1)**(-n - 1)*n*(n - 1)*(n - 2)

    assert combsimp(6*RisingFactorial(4, n)/factorial(n)) == \
        (n + 1)*(n + 2)*(n + 3)
    assert combsimp(6*RisingFactorial(4, n - 1)/factorial(n - 1)) == \
        n*(n + 1)*(n + 2)
    assert combsimp(6*RisingFactorial(4, n - 3)/factorial(n - 3)) == \
        n*(n - 1)*(n - 2)
    assert combsimp(6*RisingFactorial(4, -n - 1)/factorial(-n - 1)) == \
        -n*(n - 1)*(n - 2)
Exemple #21
0
def test_combsimp():
    assert combsimp(A*B - B*A) == A*B - B*A
Exemple #22
0
def test_combsimp_gamma():
    from sympy.abc import x, y
    assert combsimp(gamma(x)) == gamma(x)
    assert combsimp(gamma(x+1)/x) == gamma(x)
    assert combsimp(gamma(x)/(x-1)) == gamma(x-1)
    assert combsimp(x*gamma(x)) == gamma(x + 1)
    assert combsimp((x+1)*gamma(x+1)) == gamma(x + 2)
    assert combsimp(gamma(x+y)*(x+y)) == gamma(x + y + 1)
    assert combsimp(x/gamma(x+1)) == 1/gamma(x)
    assert combsimp((x+1)**2/gamma(x+2)) == (x + 1)/gamma(x + 1)
    assert combsimp(x*gamma(x) + gamma(x+3)/(x+2)) == gamma(x+1) + gamma(x+2)

    assert combsimp(gamma(2*x)*x) == gamma(2*x + 1)/2
    assert combsimp(gamma(2*x)/(x - S(1)/2)) == 2*gamma(2*x - 1)

    assert combsimp(gamma(x)*gamma(1-x)) == pi/sin(pi*x)
    assert combsimp(gamma(x)*gamma(-x)) == -pi/(x*sin(pi*x))
    assert combsimp(1/gamma(x+3)/gamma(1-x)) == sin(pi*x)/(pi*x*(x+1)*(x+2))

    assert simplify(combsimp(gamma(x)*gamma(x+S(1)/2)*gamma(y)/gamma(x+y))) \
           == 2**(-2*x + 1)*sqrt(pi)*gamma(2*x)*gamma(y)/gamma(x + y)
    assert combsimp(1/gamma(x)/gamma(x-S(1)/3)/gamma(x+S(1)/3)) == \
           3**(3*x + S(1)/2)/(18*pi*gamma(3*x - 1))
    assert simplify(gamma(S(1)/2 + x/2)*gamma(1 + x/2)/gamma(1+x)/sqrt(pi)*2**x) \
           == 1
    assert combsimp(gamma(S(-1)/4)*gamma(S(-3)/4)) == 16*sqrt(2)*pi/3

    assert simplify(combsimp(gamma(2*x)/gamma(x))) == \
           2**(2*x - 1)*gamma(x + S(1)/2)/sqrt(pi)
def test_issue_14528():
    p = symbols("p", integer=True, positive=True)
    assert combsimp(binomial(1,p)) == 1/(factorial(p)*factorial(1-p))
    assert combsimp(factorial(2-p)) == factorial(2-p)
Exemple #24
0
def test_combsimp():
    k, m, n = symbols("k m n", integer=True)

    assert combsimp(factorial(n)) == factorial(n)
    assert combsimp(binomial(n, k)) == binomial(n, k)

    assert combsimp(factorial(n) / factorial(n - 3)) == n * (-1 + n) * (-2 + n)
    assert combsimp(binomial(n + 1, k + 1) / binomial(n, k)) == (1 + n) / (1 + k)

    assert combsimp(binomial(3 * n + 4, n + 1) / binomial(3 * n + 1, n)) == Rational(
        3, 2
    ) * ((3 * n + 2) * (3 * n + 4) / ((n + 1) * (2 * n + 3)))

    assert combsimp(factorial(n) ** 2 / factorial(n - 3)) == factorial(n) * n * (
        -1 + n
    ) * (-2 + n)
    assert combsimp(
        factorial(n) * binomial(n + 1, k + 1) / binomial(n, k)
    ) == factorial(n + 1) / (1 + k)

    assert combsimp(gamma(n + 3)) == factorial(n + 2)

    assert combsimp(factorial(x)) == gamma(x + 1)

    # issue 9699
    assert combsimp((n + 1) * factorial(n)) == factorial(n + 1)
    assert combsimp(factorial(n) / n) == factorial(n - 1)

    # issue 6658
    assert combsimp(binomial(n, n - k)) == binomial(n, k)

    # issue 6341, 7135
    assert combsimp(factorial(n) / (factorial(k) * factorial(n - k))) == binomial(n, k)
    assert combsimp(factorial(k) * factorial(n - k) / factorial(n)) == 1 / binomial(
        n, k
    )
    assert combsimp(factorial(2 * n) / factorial(n) ** 2) == binomial(2 * n, n)
    assert combsimp(
        factorial(2 * n) * factorial(k) * factorial(n - k) / factorial(n) ** 3
    ) == binomial(2 * n, n) / binomial(n, k)

    assert combsimp(factorial(n * (1 + n) - n ** 2 - n)) == 1

    assert combsimp(6 * FallingFactorial(-4, n) / factorial(n)) == (-1) ** n * (
        n + 1
    ) * (n + 2) * (n + 3)
    assert combsimp(6 * FallingFactorial(-4, n - 1) / factorial(n - 1)) == (-1) ** (
        n - 1
    ) * n * (n + 1) * (n + 2)
    assert combsimp(6 * FallingFactorial(-4, n - 3) / factorial(n - 3)) == (-1) ** (
        n - 3
    ) * n * (n - 1) * (n - 2)
    assert combsimp(6 * FallingFactorial(-4, -n - 1) / factorial(-n - 1)) == -(
        (-1) ** (-n - 1)
    ) * n * (n - 1) * (n - 2)

    assert combsimp(6 * RisingFactorial(4, n) / factorial(n)) == (n + 1) * (n + 2) * (
        n + 3
    )
    assert combsimp(6 * RisingFactorial(4, n - 1) / factorial(n - 1)) == n * (n + 1) * (
        n + 2
    )
    assert combsimp(6 * RisingFactorial(4, n - 3) / factorial(n - 3)) == n * (n - 1) * (
        n - 2
    )
    assert combsimp(6 * RisingFactorial(4, -n - 1) / factorial(-n - 1)) == -n * (
        n - 1
    ) * (n - 2)
Exemple #25
0
def test_combsimp():
    from sympy.abc import n, k

    assert combsimp(factorial(n)) == factorial(n)
    assert combsimp(binomial(n, k)) == binomial(n, k)

    assert combsimp(factorial(n) / factorial(n - 3)) == n * (-1 + n) * (-2 + n)
    assert combsimp(binomial(n + 1, k + 1) /
                    binomial(n, k)) == (1 + n) / (1 + k)

    assert combsimp(binomial(3*n + 4, n + 1)/binomial(3*n + 1, n)) == \
        S(3)/2*((3*n + 2)*(3*n + 4)/((n + 1)*(2*n + 3)))

    assert combsimp(factorial(n)**2/factorial(n - 3)) == \
        factorial(n)*n*(-1 + n)*(-2 + n)
    assert combsimp(factorial(n)*binomial(n + 1, k + 1)/binomial(n, k)) == \
        factorial(n)*(1 + n)/(1 + k)

    assert combsimp(binomial(n - 1, k)) == -((-n + k) * binomial(n, k)) / n

    assert combsimp(binomial(n + 2, k + S(1) / 2)) == 4 * (
        (n + 1) * (n + 2) * binomial(n, k + S(1) / 2)) / ((2 * k - 2 * n - 1) *
                                                          (2 * k - 2 * n - 3))
    assert combsimp(binomial(n + 2, k + 2.0)) == \
        -((1.0*n + 2.0)*binomial(n + 1.0, k + 2.0))/(k - n)

    # coverage tests
    assert combsimp(factorial(n * (1 + n) - n**2 - n)) == 1
    assert combsimp(binomial(n + k - 2, n)) == \
        k*(k - 1)*binomial(n + k, n)/((n + k)*(n + k - 1))
    i = Symbol('i', integer=True)
    e = gamma(i + 3)
    assert combsimp(e) == e
    e = gamma(exp(i))
    assert combsimp(e) == e
    e = gamma(n + S(1) / 3) * gamma(n + S(2) / 3)
    assert combsimp(e) == e
    assert combsimp(gamma(4*n + S(1)/2)/gamma(2*n - S(3)/4)) == \
        2**(4*n - S(5)/2)*(8*n - 3)*gamma(2*n + S(3)/4)/sqrt(pi)

    assert combsimp(6*FallingFactorial(-4, n)/factorial(n)) == \
        (-1)**n*(n + 1)*(n + 2)*(n + 3)
    assert combsimp(6*FallingFactorial(-4, n - 1)/factorial(n - 1)) == \
        (-1)**(n - 1)*n*(n + 1)*(n + 2)
    assert combsimp(6*FallingFactorial(-4, n - 3)/factorial(n - 3)) == \
        (-1)**(n - 3)*n*(n - 1)*(n - 2)
    assert combsimp(6*FallingFactorial(-4, -n - 1)/factorial(-n - 1)) == \
        -(-1)**(-n - 1)*n*(n - 1)*(n - 2)

    assert combsimp(6*RisingFactorial(4, n)/factorial(n)) == \
        (n + 1)*(n + 2)*(n + 3)
    assert combsimp(6*RisingFactorial(4, n - 1)/factorial(n - 1)) == \
        n*(n + 1)*(n + 2)
    assert combsimp(6*RisingFactorial(4, n - 3)/factorial(n - 3)) == \
        n*(n - 1)*(n - 2)
    assert combsimp(6*RisingFactorial(4, -n - 1)/factorial(-n - 1)) == \
        -n*(n - 1)*(n - 2)
Exemple #26
0
def test_combsimp_gamma():
    from sympy.abc import x, y
    R = Rational

    assert combsimp(gamma(x)) == gamma(x)
    assert combsimp(gamma(x + 1) / x) == gamma(x)
    assert combsimp(gamma(x) / (x - 1)) == gamma(x - 1)
    assert combsimp(x * gamma(x)) == gamma(x + 1)
    assert combsimp((x + 1) * gamma(x + 1)) == gamma(x + 2)
    assert combsimp(gamma(x + y) * (x + y)) == gamma(x + y + 1)
    assert combsimp(x / gamma(x + 1)) == 1 / gamma(x)
    assert combsimp((x + 1)**2 / gamma(x + 2)) == (x + 1) / gamma(x + 1)
    assert combsimp(x*gamma(x) + gamma(x + 3)/(x + 2)) == \
        (x + 2)*gamma(x + 1)

    assert combsimp(gamma(2 * x) * x) == gamma(2 * x + 1) / 2
    assert combsimp(gamma(2 * x) / (x - S(1) / 2)) == 2 * gamma(2 * x - 1)

    assert combsimp(gamma(x) * gamma(1 - x)) == pi / sin(pi * x)
    assert combsimp(gamma(x) * gamma(-x)) == -pi / (x * sin(pi * x))
    assert combsimp(1/gamma(x + 3)/gamma(1 - x)) == \
        sin(pi*x)/(pi*x*(x + 1)*(x + 2))

    assert powsimp(combsimp(
        gamma(x)*gamma(x + S(1)/2)*gamma(y)/gamma(x + y))) == \
        2**(-2*x + 1)*sqrt(pi)*gamma(2*x)*gamma(y)/gamma(x + y)
    assert combsimp(1/gamma(x)/gamma(x - S(1)/3)/gamma(x + S(1)/3)) == \
        3**(3*x - S(3)/2)/(2*pi*gamma(3*x - 1))
    assert simplify(
        gamma(S(1) / 2 + x / 2) * gamma(1 + x / 2) / gamma(1 + x) / sqrt(pi) *
        2**x) == 1
    assert combsimp(gamma(S(-1) / 4) *
                    gamma(S(-3) / 4)) == 16 * sqrt(2) * pi / 3

    assert powsimp(combsimp(gamma(2*x)/gamma(x))) == \
        2**(2*x - 1)*gamma(x + S(1)/2)/sqrt(pi)

    # issue 6792
    e = (-gamma(k) * gamma(k + 2) + gamma(k + 1)**2) / gamma(k)**2
    assert combsimp(e) == -k
    assert combsimp(1 / e) == -1 / k
    e = (gamma(x) + gamma(x + 1)) / gamma(x)
    assert combsimp(e) == x + 1
    assert combsimp(1 / e) == 1 / (x + 1)
    e = (gamma(x) + gamma(x + 2)) * (gamma(x - 1) + gamma(x)) / gamma(x)
    assert combsimp(e) == (x**2 + x + 1) * gamma(x + 1) / (x - 1)
    e = (-gamma(k) * gamma(k + 2) + gamma(k + 1)**2) / gamma(k)**2
    assert combsimp(e**2) == k**2
    assert combsimp(e**2 / gamma(k + 1)) == k / gamma(k)
    a = R(1, 2) + R(1, 3)
    b = a + R(1, 3)
    assert combsimp(gamma(2 * k) / gamma(k) * gamma(k + a) * gamma(k + b))
    3 * 2**(2 * k + 1) * 3**(-3 * k - 2) * sqrt(pi) * gamma(3 * k +
                                                            R(3, 2)) / 2

    A, B = symbols('A B', commutative=False)
    assert combsimp(e * B * A) == combsimp(e) * B * A

    # check iteration
    assert combsimp(gamma(2 * k) / gamma(k) *
                    gamma(-k - R(1, 2))) == (-2**(2 * k + 1) * sqrt(pi) /
                                             (2 * ((2 * k + 1) * cos(pi * k))))
    assert combsimp(
        gamma(k) * gamma(k + R(1, 3)) * gamma(k + R(2, 3)) /
        gamma(3 * k / 2)) == (3 * 2**(3 * k + 1) * 3**(-3 * k - S.Half) *
                              sqrt(pi) * gamma(3 * k / 2 + S.Half) / 2)
Exemple #27
0
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', integer=True)
    assert combsimp(expand_mul(integrate(log(x) * x**(k-1) * exp(-x) / gamma(k),
                                     (x, 0, oo), conds='none'))) == polygamma(0, k)
Exemple #28
0
vars = [T,*n]

#%%
sol = s.linsolve(b*n-t-u*T,*n)
sol
#%%
s.solveset(s.Mod(T+t[1],b[1,1]),T)
#%%
constraint = b*n- t
solveme = list(s.Eq(c,T) for c in constraint)
solveme
#%%
vars = [T,*n]
solution_set = s.linsolve(solveme,vars)
solution_set
#%%

# %%
solutions = []
for left,right in zip(vars,solution_set.args[0]):
    solutions.append(s.Eq(left,right))
solutions

#%%
con = [s.solveset(solveme[i],T,s.S.Naturals) for i in range(d)]

# %%
s.combsimp(con[0])
# %%
# %%
Exemple #29
0
def test_combsimp():
    k, m, n = symbols('k m n', integer = True)

    assert combsimp(factorial(n)) == factorial(n)
    assert combsimp(binomial(n, k)) == binomial(n, k)

    assert combsimp(factorial(n)/factorial(n - 3)) == n*(-1 + n)*(-2 + n)
    assert combsimp(binomial(n + 1, k + 1)/binomial(n, k)) == (1 + n)/(1 + k)

    assert combsimp(binomial(3*n + 4, n + 1)/binomial(3*n + 1, n)) == \
        S(3)/2*((3*n + 2)*(3*n + 4)/((n + 1)*(2*n + 3)))

    assert combsimp(factorial(n)**2/factorial(n - 3)) == \
        factorial(n)*n*(-1 + n)*(-2 + n)
    assert combsimp(factorial(n)*binomial(n + 1, k + 1)/binomial(n, k)) == \
        factorial(n + 1)/(1 + k)

    assert combsimp(gamma(n + 3)) == factorial(n + 2)

    assert combsimp(factorial(x)) == gamma(x + 1)

    # issue 9699
    assert combsimp((n + 1)*factorial(n)) == factorial(n + 1)
    assert combsimp(factorial(n)/n) == factorial(n-1)

    # issue 6658
    assert combsimp(binomial(n, n - k)) == binomial(n, k)

    # issue 6341, 7135
    assert combsimp(factorial(n)/(factorial(k)*factorial(n - k))) == \
        binomial(n, k)
    assert combsimp(factorial(k)*factorial(n - k)/factorial(n)) == \
        1/binomial(n, k)
    assert combsimp(factorial(2*n)/factorial(n)**2) == binomial(2*n, n)
    assert combsimp(factorial(2*n)*factorial(k)*factorial(n - k)/
        factorial(n)**3) == binomial(2*n, n)/binomial(n, k)

    assert combsimp(factorial(n*(1 + n) - n**2 - n)) == 1

    assert combsimp(6*FallingFactorial(-4, n)/factorial(n)) == \
        (-1)**n*(n + 1)*(n + 2)*(n + 3)
    assert combsimp(6*FallingFactorial(-4, n - 1)/factorial(n - 1)) == \
        (-1)**(n - 1)*n*(n + 1)*(n + 2)
    assert combsimp(6*FallingFactorial(-4, n - 3)/factorial(n - 3)) == \
        (-1)**(n - 3)*n*(n - 1)*(n - 2)
    assert combsimp(6*FallingFactorial(-4, -n - 1)/factorial(-n - 1)) == \
        -(-1)**(-n - 1)*n*(n - 1)*(n - 2)

    assert combsimp(6*RisingFactorial(4, n)/factorial(n)) == \
        (n + 1)*(n + 2)*(n + 3)
    assert combsimp(6*RisingFactorial(4, n - 1)/factorial(n - 1)) == \
        n*(n + 1)*(n + 2)
    assert combsimp(6*RisingFactorial(4, n - 3)/factorial(n - 3)) == \
        n*(n - 1)*(n - 2)
    assert combsimp(6*RisingFactorial(4, -n - 1)/factorial(-n - 1)) == \
        -n*(n - 1)*(n - 2)
Exemple #30
0
def test_F3():
    assert combsimp(2**n * factorial(n) * factorial2(2 * n - 1)) == factorial(
        2 * n)
def test_combsimp_gamma():
    from sympy.abc import x, y
    R = Rational

    assert combsimp(gamma(x)) == gamma(x)
    assert combsimp(gamma(x + 1)/x) == gamma(x)
    assert combsimp(gamma(x)/(x - 1)) == gamma(x - 1)
    assert combsimp(x*gamma(x)) == gamma(x + 1)
    assert combsimp((x + 1)*gamma(x + 1)) == gamma(x + 2)
    assert combsimp(gamma(x + y)*(x + y)) == gamma(x + y + 1)
    assert combsimp(x/gamma(x + 1)) == 1/gamma(x)
    assert combsimp((x + 1)**2/gamma(x + 2)) == (x + 1)/gamma(x + 1)
    assert combsimp(x*gamma(x) + gamma(x + 3)/(x + 2)) == \
        (x + 2)*gamma(x + 1)

    assert combsimp(gamma(2*x)*x) == gamma(2*x + 1)/2
    assert combsimp(gamma(2*x)/(x - S(1)/2)) == 2*gamma(2*x - 1)

    assert combsimp(gamma(x)*gamma(1 - x)) == pi/sin(pi*x)
    assert combsimp(gamma(x)*gamma(-x)) == -pi/(x*sin(pi*x))
    assert combsimp(1/gamma(x + 3)/gamma(1 - x)) == \
        sin(pi*x)/(pi*x*(x + 1)*(x + 2))

    assert powsimp(combsimp(
        gamma(x)*gamma(x + S(1)/2)*gamma(y)/gamma(x + y))) == \
        2**(-2*x + 1)*sqrt(pi)*gamma(2*x)*gamma(y)/gamma(x + y)
    assert combsimp(1/gamma(x)/gamma(x - S(1)/3)/gamma(x + S(1)/3)) == \
        3**(3*x - S(3)/2)/(2*pi*gamma(3*x - 1))
    assert simplify(
        gamma(S(1)/2 + x/2)*gamma(1 + x/2)/gamma(1 + x)/sqrt(pi)*2**x) == 1
    assert combsimp(gamma(S(-1)/4)*gamma(S(-3)/4)) == 16*sqrt(2)*pi/3

    assert powsimp(combsimp(gamma(2*x)/gamma(x))) == \
        2**(2*x - 1)*gamma(x + S(1)/2)/sqrt(pi)

    # issue 6792
    e = (-gamma(k)*gamma(k + 2) + gamma(k + 1)**2)/gamma(k)**2
    assert combsimp(e) == -k
    assert combsimp(1/e) == -1/k
    e = (gamma(x) + gamma(x + 1))/gamma(x)
    assert combsimp(e) == x + 1
    assert combsimp(1/e) == 1/(x + 1)
    e = (gamma(x) + gamma(x + 2))*(gamma(x - 1) + gamma(x))/gamma(x)
    assert combsimp(e) == (x**2 + x + 1)*gamma(x + 1)/(x - 1)
    e = (-gamma(k)*gamma(k + 2) + gamma(k + 1)**2)/gamma(k)**2
    assert combsimp(e**2) == k**2
    assert combsimp(e**2/gamma(k + 1)) == k/gamma(k)
    a = R(1, 2) + R(1, 3)
    b = a + R(1, 3)
    assert combsimp(gamma(2*k)/gamma(k)*gamma(k + a)*gamma(k + b))
    3*2**(2*k + 1)*3**(-3*k - 2)*sqrt(pi)*gamma(3*k + R(3, 2))/2

    A, B = symbols('A B', commutative=False)
    assert combsimp(e*B*A) == combsimp(e)*B*A

    # check iteration
    assert combsimp(gamma(2*k)/gamma(k)*gamma(-k - R(1, 2))) == (
        -2**(2*k + 1)*sqrt(pi)/(2*((2*k + 1)*cos(pi*k))))
    assert combsimp(
        gamma(k)*gamma(k + R(1, 3))*gamma(k + R(2, 3))/gamma(3*k/2)) == (
        3*2**(3*k + 1)*3**(-3*k - S.Half)*sqrt(pi)*gamma(3*k/2 + S.Half)/2)
Exemple #32
0
def test_F4():
    assert combsimp(
        (2**n * factorial(n) * product(2 * k - 1,
                                       (k, 1, n)))) == factorial(2 * n)
Exemple #33
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)
Exemple #34
0
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)
Exemple #35
0
def test_mellin_transform_bessel():
    from sympy import Max, Min, hyper, meijerg
    MT = mellin_transform

    # 8.4.19
    assert MT(besselj(a, 2*sqrt(x)), x, s) == \
        (gamma(a/2 + s)/gamma(a/2 - s + 1), (-re(a)/2, S(3)/4), True)
    assert MT(sin(sqrt(x))*besselj(a, sqrt(x)), x, s) == \
        (2**a*gamma(-2*s + S(1)/2)*gamma(a/2 + s + S(1)/2)/(
        gamma(-a/2 - s + 1)*gamma(a - 2*s + 1)), (
        -re(a)/2 - S(1)/2, S(1)/4), True)
    assert MT(cos(sqrt(x))*besselj(a, sqrt(x)), x, s) == \
        (2**a*gamma(a/2 + s)*gamma(-2*s + S(1)/2)/(
        gamma(-a/2 - s + S(1)/2)*gamma(a - 2*s + 1)), (
        -re(a)/2, S(1)/4), True)
    assert MT(besselj(a, sqrt(x))**2, x, s) == \
        (gamma(a + s)*gamma(S(1)/2 - s)
         / (sqrt(pi)*gamma(1 - s)*gamma(1 + a - s)),
            (-re(a), S(1)/2), True)
    assert MT(besselj(a, sqrt(x))*besselj(-a, sqrt(x)), x, s) == \
        (gamma(s)*gamma(S(1)/2 - s)
         / (sqrt(pi)*gamma(1 - a - s)*gamma(1 + a - s)),
            (0, S(1)/2), True)
    # NOTE: prudnikov gives the strip below as (1/2 - re(a), 1). As far as
    #       I can see this is wrong (since besselj(z) ~ 1/sqrt(z) for z large)
    assert MT(besselj(a - 1, sqrt(x))*besselj(a, sqrt(x)), x, s) == \
        (gamma(1 - s)*gamma(a + s - S(1)/2)
         / (sqrt(pi)*gamma(S(3)/2 - s)*gamma(a - s + S(1)/2)),
            (S(1)/2 - re(a), S(1)/2), True)
    assert MT(besselj(a, sqrt(x))*besselj(b, sqrt(x)), x, s) == \
        (4**s*gamma(1 - 2*s)*gamma((a + b)/2 + s)
         / (gamma(1 - s + (b - a)/2)*gamma(1 - s + (a - b)/2)
            *gamma( 1 - s + (a + b)/2)),
            (-(re(a) + re(b))/2, S(1)/2), True)
    assert MT(besselj(a, sqrt(x))**2 + besselj(-a, sqrt(x))**2, x, s)[1:] == \
        ((Max(re(a), -re(a)), S(1)/2), True)

    # Section 8.4.20
    assert MT(bessely(a, 2*sqrt(x)), x, s) == \
        (-cos(pi*(a/2 - s))*gamma(s - a/2)*gamma(s + a/2)/pi,
            (Max(-re(a)/2, re(a)/2), S(3)/4), True)
    assert MT(sin(sqrt(x))*bessely(a, sqrt(x)), x, s) == \
        (-4**s*sin(pi*(a/2 - s))*gamma(S(1)/2 - 2*s)
         * gamma((1 - a)/2 + s)*gamma((1 + a)/2 + s)
         / (sqrt(pi)*gamma(1 - s - a/2)*gamma(1 - s + a/2)),
            (Max(-(re(a) + 1)/2, (re(a) - 1)/2), S(1)/4), True)
    assert MT(cos(sqrt(x))*bessely(a, sqrt(x)), x, s) == \
        (-4**s*cos(pi*(a/2 - s))*gamma(s - a/2)*gamma(s + a/2)*gamma(S(1)/2 - 2*s)
         / (sqrt(pi)*gamma(S(1)/2 - s - a/2)*gamma(S(1)/2 - s + a/2)),
            (Max(-re(a)/2, re(a)/2), S(1)/4), True)
    assert MT(besselj(a, sqrt(x))*bessely(a, sqrt(x)), x, s) == \
        (-cos(pi*s)*gamma(s)*gamma(a + s)*gamma(S(1)/2 - s)
         / (pi**S('3/2')*gamma(1 + a - s)),
            (Max(-re(a), 0), S(1)/2), True)
    assert MT(besselj(a, sqrt(x))*bessely(b, sqrt(x)), x, s) == \
        (-4**s*cos(pi*(a/2 - b/2 + s))*gamma(1 - 2*s)
         * gamma(a/2 - b/2 + s)*gamma(a/2 + b/2 + s)
         / (pi*gamma(a/2 - b/2 - s + 1)*gamma(a/2 + b/2 - s + 1)),
            (Max((-re(a) + re(b))/2, (-re(a) - re(b))/2), S(1)/2), True)
    # NOTE bessely(a, sqrt(x))**2 and bessely(a, sqrt(x))*bessely(b, sqrt(x))
    # are a mess (no matter what way you look at it ...)
    assert MT(bessely(a, sqrt(x))**2, x, s)[1:] == \
             ((Max(-re(a), 0, re(a)), S(1)/2), True)

    # Section 8.4.22
    # TODO we can't do any of these (delicate cancellation)

    # Section 8.4.23
    assert MT(besselk(a, 2*sqrt(x)), x, s) == \
        (gamma(
         s - a/2)*gamma(s + a/2)/2, (Max(-re(a)/2, re(a)/2), oo), True)
    assert MT(besselj(a, 2*sqrt(2*sqrt(x)))*besselk(
        a, 2*sqrt(2*sqrt(x))), x, s) == (4**(-s)*gamma(2*s)*
        gamma(a/2 + s)/(2*gamma(a/2 - s + 1)), (Max(0, -re(a)/2), oo), True)
    # TODO bessely(a, x)*besselk(a, x) is a mess
    assert MT(besseli(a, sqrt(x))*besselk(a, sqrt(x)), x, s) == \
        (gamma(s)*gamma(
        a + s)*gamma(-s + S(1)/2)/(2*sqrt(pi)*gamma(a - s + 1)),
        (Max(-re(a), 0), S(1)/2), True)
    assert MT(besseli(b, sqrt(x))*besselk(a, sqrt(x)), x, s) == \
        (2**(2*s - 1)*gamma(-2*s + 1)*gamma(-a/2 + b/2 + s)* \
        gamma(a/2 + b/2 + s)/(gamma(-a/2 + b/2 - s + 1)* \
        gamma(a/2 + b/2 - s + 1)), (Max(-re(a)/2 - re(b)/2, \
        re(a)/2 - re(b)/2), S(1)/2), True)

    # TODO products of besselk are a mess

    mt = MT(exp(-x/2)*besselk(a, x/2), x, s)
    mt0 = combsimp((trigsimp(combsimp(mt[0].expand(func=True)))))
    assert mt0 == 2*pi**(S(3)/2)*cos(pi*s)*gamma(-s + S(1)/2)/(
        (cos(2*pi*a) - cos(2*pi*s))*gamma(-a - s + 1)*gamma(a - s + 1))
    assert mt[1:] == ((Max(-re(a), re(a)), oo), True)
Exemple #36
0
def test_combsimp_gamma():
    from sympy.abc import x, y
    assert combsimp(gamma(x)) == gamma(x)
    assert combsimp(gamma(x + 1) / x) == gamma(x)
    assert combsimp(gamma(x) / (x - 1)) == gamma(x - 1)
    assert combsimp(x * gamma(x)) == gamma(x + 1)
    assert combsimp((x + 1) * gamma(x + 1)) == gamma(x + 2)
    assert combsimp(gamma(x + y) * (x + y)) == gamma(x + y + 1)
    assert combsimp(x / gamma(x + 1)) == 1 / gamma(x)
    assert combsimp((x + 1)**2 / gamma(x + 2)) == (x + 1) / gamma(x + 1)
    assert combsimp(x * gamma(x) + gamma(x + 3) /
                    (x + 2)) == gamma(x + 1) + gamma(x + 2)

    assert combsimp(gamma(2 * x) * x) == gamma(2 * x + 1) / 2
    assert combsimp(gamma(2 * x) / (x - S(1) / 2)) == 2 * gamma(2 * x - 1)

    assert combsimp(gamma(x) * gamma(1 - x)) == pi / sin(pi * x)
    assert combsimp(gamma(x) * gamma(-x)) == -pi / (x * sin(pi * x))
    assert combsimp(1 / gamma(x + 3) /
                    gamma(1 - x)) == sin(pi * x) / (pi * x * (x + 1) * (x + 2))

    assert simplify(combsimp(gamma(x)*gamma(x+S(1)/2)*gamma(y)/gamma(x+y))) \
           == 2**(-2*x + 1)*sqrt(pi)*gamma(2*x)*gamma(y)/gamma(x + y)
    assert combsimp(1/gamma(x)/gamma(x-S(1)/3)/gamma(x+S(1)/3)) == \
           3**(3*x + S(1)/2)/(18*pi*gamma(3*x - 1))
    assert simplify(gamma(S(1)/2 + x/2)*gamma(1 + x/2)/gamma(1+x)/sqrt(pi)*2**x) \
           == 1
    assert combsimp(gamma(S(-1) / 4) *
                    gamma(S(-3) / 4)) == 16 * sqrt(2) * pi / 3

    assert simplify(combsimp(gamma(2*x)/gamma(x))) == \
           2**(2*x - 1)*gamma(x + S(1)/2)/sqrt(pi)
def test_issue_6878():
    n = symbols('n', integer=True)
    assert combsimp(RisingFactorial(
        -10, n)) == 3628800 * (-1)**n / factorial(10 - n)
Exemple #38
0
def test_mellin_transform_bessel():
    from sympy import Max
    MT = mellin_transform

    # 8.4.19
    assert MT(besselj(a, 2*sqrt(x)), x, s) == \
        (gamma(a/2 + s)/gamma(a/2 - s + 1), (-re(a)/2, S(3)/4), True)
    assert MT(sin(sqrt(x))*besselj(a, sqrt(x)), x, s) == \
        (2**a*gamma(-2*s + S(1)/2)*gamma(a/2 + s + S(1)/2)/(
        gamma(-a/2 - s + 1)*gamma(a - 2*s + 1)), (
        -re(a)/2 - S(1)/2, S(1)/4), True)
    assert MT(cos(sqrt(x))*besselj(a, sqrt(x)), x, s) == \
        (2**a*gamma(a/2 + s)*gamma(-2*s + S(1)/2)/(
        gamma(-a/2 - s + S(1)/2)*gamma(a - 2*s + 1)), (
        -re(a)/2, S(1)/4), True)
    assert MT(besselj(a, sqrt(x))**2, x, s) == \
        (gamma(a + s)*gamma(S(1)/2 - s)
         / (sqrt(pi)*gamma(1 - s)*gamma(1 + a - s)),
            (-re(a), S(1)/2), True)
    assert MT(besselj(a, sqrt(x))*besselj(-a, sqrt(x)), x, s) == \
        (gamma(s)*gamma(S(1)/2 - s)
         / (sqrt(pi)*gamma(1 - a - s)*gamma(1 + a - s)),
            (0, S(1)/2), True)
    # NOTE: prudnikov gives the strip below as (1/2 - re(a), 1). As far as
    #       I can see this is wrong (since besselj(z) ~ 1/sqrt(z) for z large)
    assert MT(besselj(a - 1, sqrt(x))*besselj(a, sqrt(x)), x, s) == \
        (gamma(1 - s)*gamma(a + s - S(1)/2)
         / (sqrt(pi)*gamma(S(3)/2 - s)*gamma(a - s + S(1)/2)),
            (S(1)/2 - re(a), S(1)/2), True)
    assert MT(besselj(a, sqrt(x))*besselj(b, sqrt(x)), x, s) == \
        (4**s*gamma(1 - 2*s)*gamma((a + b)/2 + s)
         / (gamma(1 - s + (b - a)/2)*gamma(1 - s + (a - b)/2)
            *gamma( 1 - s + (a + b)/2)),
            (-(re(a) + re(b))/2, S(1)/2), True)
    assert MT(besselj(a, sqrt(x))**2 + besselj(-a, sqrt(x))**2, x, s)[1:] == \
        ((Max(re(a), -re(a)), S(1)/2), True)

    # Section 8.4.20
    assert MT(bessely(a, 2*sqrt(x)), x, s) == \
        (-cos(pi*(a/2 - s))*gamma(s - a/2)*gamma(s + a/2)/pi,
            (Max(-re(a)/2, re(a)/2), S(3)/4), True)
    assert MT(sin(sqrt(x))*bessely(a, sqrt(x)), x, s) == \
        (-4**s*sin(pi*(a/2 - s))*gamma(S(1)/2 - 2*s)
         * gamma((1 - a)/2 + s)*gamma((1 + a)/2 + s)
         / (sqrt(pi)*gamma(1 - s - a/2)*gamma(1 - s + a/2)),
            (Max(-(re(a) + 1)/2, (re(a) - 1)/2), S(1)/4), True)
    assert MT(cos(sqrt(x))*bessely(a, sqrt(x)), x, s) == \
        (-4**s*cos(pi*(a/2 - s))*gamma(s - a/2)*gamma(s + a/2)*gamma(S(1)/2 - 2*s)
         / (sqrt(pi)*gamma(S(1)/2 - s - a/2)*gamma(S(1)/2 - s + a/2)),
            (Max(-re(a)/2, re(a)/2), S(1)/4), True)
    assert MT(besselj(a, sqrt(x))*bessely(a, sqrt(x)), x, s) == \
        (-cos(pi*s)*gamma(s)*gamma(a + s)*gamma(S(1)/2 - s)
         / (pi**S('3/2')*gamma(1 + a - s)),
            (Max(-re(a), 0), S(1)/2), True)
    assert MT(besselj(a, sqrt(x))*bessely(b, sqrt(x)), x, s) == \
        (-4**s*cos(pi*(a/2 - b/2 + s))*gamma(1 - 2*s)
         * gamma(a/2 - b/2 + s)*gamma(a/2 + b/2 + s)
         / (pi*gamma(a/2 - b/2 - s + 1)*gamma(a/2 + b/2 - s + 1)),
            (Max((-re(a) + re(b))/2, (-re(a) - re(b))/2), S(1)/2), True)
    # NOTE bessely(a, sqrt(x))**2 and bessely(a, sqrt(x))*bessely(b, sqrt(x))
    # are a mess (no matter what way you look at it ...)
    assert MT(bessely(a, sqrt(x))**2, x, s)[1:] == \
             ((Max(-re(a), 0, re(a)), S(1)/2), True)

    # Section 8.4.22
    # TODO we can't do any of these (delicate cancellation)

    # Section 8.4.23
    assert MT(besselk(a, 2*sqrt(x)), x, s) == \
        (gamma(
         s - a/2)*gamma(s + a/2)/2, (Max(-re(a)/2, re(a)/2), oo), True)
    assert MT(besselj(a, 2*sqrt(2*sqrt(x)))*besselk(
        a, 2*sqrt(2*sqrt(x))), x, s) == (4**(-s)*gamma(2*s)*
        gamma(a/2 + s)/(2*gamma(a/2 - s + 1)), (Max(0, -re(a)/2), oo), True)
    # TODO bessely(a, x)*besselk(a, x) is a mess
    assert MT(besseli(a, sqrt(x))*besselk(a, sqrt(x)), x, s) == \
        (gamma(s)*gamma(
        a + s)*gamma(-s + S(1)/2)/(2*sqrt(pi)*gamma(a - s + 1)),
        (Max(-re(a), 0), S(1)/2), True)
    assert MT(besseli(b, sqrt(x))*besselk(a, sqrt(x)), x, s) == \
        (2**(2*s - 1)*gamma(-2*s + 1)*gamma(-a/2 + b/2 + s)* \
        gamma(a/2 + b/2 + s)/(gamma(-a/2 + b/2 - s + 1)* \
        gamma(a/2 + b/2 - s + 1)), (Max(-re(a)/2 - re(b)/2, \
        re(a)/2 - re(b)/2), S(1)/2), True)

    # TODO products of besselk are a mess

    mt = MT(exp(-x/2)*besselk(a, x/2), x, s)
    mt0 = combsimp((trigsimp(combsimp(mt[0].expand(func=True)))))
    assert mt0 == 2*pi**(S(3)/2)*cos(pi*s)*gamma(-s + S(1)/2)/(
        (cos(2*pi*a) - cos(2*pi*s))*gamma(-a - s + 1)*gamma(a - s + 1))
    assert mt[1:] == ((Max(-re(a), re(a)), oo), True)
Exemple #39
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)
Exemple #40
0
def simplify_density_matrix(density_matrix):
    for row in xrange(4):
        for col in xrange(4):
            density_matrix[row, col] = subsitute(combsimp(density_matrix[row, col]))
    return density_matrix