예제 #1
0
def test_hyper_rewrite_sum():
    _k = Dummy("k")
    assert replace_dummy(hyper((1, 2), (1, 3), x).rewrite(Sum), _k) == \
        Sum(x**_k / factorial(_k) * RisingFactorial(2, _k) /
            RisingFactorial(3, _k), (_k, 0, oo))

    assert hyper((1, 2, 3), (-1, 3), z).rewrite(Sum) == \
        hyper((1, 2, 3), (-1, 3), z)
예제 #2
0
def test_combsimp():
    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(binomial(n - 1, k)) == -((-n + k) * binomial(n, k)) / n

    assert combsimp(binomial(n + 2, k + Rational(1, 2))) == 4 * (
        (n + 1) *
        (n + 2) * binomial(n, k + Rational(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 + Rational(1, 3)) * gamma(n + Rational(2, 3))
    assert combsimp(e) == e
    assert combsimp(gamma(4*n + Rational(1, 2))/gamma(2*n - Rational(3, 4))) == \
        2**(4*n - Rational(5, 2))*(8*n - 3)*gamma(2*n + Rational(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)
예제 #3
0
def test_jacobi():
    assert jacobi(0, a, b, x) == 1
    assert jacobi(1, a, b, x) == a/2 - b/2 + x*(a/2 + b/2 + 1)
    assert (jacobi(2, a, b, x) == a**2/8 - a*b/4 - a/8 + b**2/8 - b/8 +
            x**2*(a**2/8 + a*b/4 + 7*a/8 + b**2/8 + 7*b/8 + Rational(3, 2)) +
            x*(a**2/4 + 3*a/4 - b**2/4 - 3*b/4) - Rational(1, 2))

    assert jacobi(n, a, a, x) == RisingFactorial(
        a + 1, n)*gegenbauer(n, a + Rational(1, 2), x)/RisingFactorial(2*a + 1, n)
    assert jacobi(n, a, -a, x) == ((-1)**a*(-x + 1)**(-a/2)*(x + 1)**(a/2)*assoc_legendre(n, a, x) *
                                   factorial(-a + n)*gamma(a + n + 1)/(factorial(a + n)*gamma(n + 1)))
    assert jacobi(n, -b, b, x) == ((-x + 1)**(b/2)*(x + 1)**(-b/2)*assoc_legendre(n, b, x) *
                                   gamma(-b + n + 1)/gamma(n + 1))
    assert jacobi(n, 0, 0, x) == legendre(n, x)
    assert jacobi(n, Rational(1, 2), Rational(1, 2), x) == RisingFactorial(
        Rational(3, 2), n)*chebyshevu(n, x)/factorial(n + 1)
    assert jacobi(n, Rational(-1, 2), Rational(-1, 2), x) == RisingFactorial(
        Rational(1, 2), n)*chebyshevt(n, x)/factorial(n)

    X = jacobi(n, a, b, x)
    assert isinstance(X, jacobi)

    assert jacobi(n, a, b, -x) == (-1)**n*jacobi(n, b, a, x)
    assert jacobi(n, a, b, 0) == 2**(-n)*gamma(a + n + 1)*hyper(
        (-b - n, -n), (a + 1,), -1)/(factorial(n)*gamma(a + 1))
    assert jacobi(n, a, b, 1) == RisingFactorial(a + 1, n)/factorial(n)

    m = Symbol('m', positive=True)
    assert jacobi(m, a, b, oo) == oo*RisingFactorial(a + b + m + 1, m)
    assert jacobi(n, a, b, oo) == jacobi(n, a, b, oo, evaluate=False)

    assert conjugate(jacobi(m, a, b, x)) == \
        jacobi(m, conjugate(a), conjugate(b), conjugate(x))

    assert diff(jacobi(n, a, b, x), n) == Derivative(jacobi(n, a, b, x), n)
    assert diff(jacobi(n, a, b, x), x) == \
        (a + b + n + 1)*jacobi(n - 1, a + 1, b + 1, x)/2

    assert (jacobi(n, a, b, x).diff(a) ==
            Sum((jacobi(n, a, b, x) + (a + b + 2*k + 1)*RisingFactorial(b + k + 1, n - k) *
                jacobi(k, a, b, x)/((n - k)*RisingFactorial(a + b + k + 1, n - k)))/(a + b + n + k + 1), (k, 0, n - 1)))
    assert (jacobi(n, a, b, x).diff(b) ==
            Sum(((-1)**(n - k)*(a + b + 2*k + 1)*RisingFactorial(a + k + 1, n - k) *
                 jacobi(k, a, b, x)/((n - k)*RisingFactorial(a + b + k + 1, n - k)) + jacobi(n, a, b, x))/(a + b + n + k + 1), (k, 0, n - 1)))

    assert jacobi_normalized(n, a, b, x) == \
        (jacobi(n, a, b, x)/sqrt(2**(a + b + 1)*gamma(a + n + 1)*gamma(b + n + 1)
                                 / ((a + b + 2*n + 1)*factorial(n)*gamma(a + b + n + 1))))

    pytest.raises(ValueError, lambda: jacobi(-2.1, a, b, x))
    pytest.raises(ValueError, lambda: jacobi(Dummy(positive=True, integer=True), 1, 2, oo))
    pytest.raises(ArgumentIndexError, lambda: jacobi(n, a, b, x).fdiff(5))
예제 #4
0
def test_gegenbauer():
    assert gegenbauer(0, a, x) == 1
    assert gegenbauer(1, a, x) == 2 * a * x
    assert gegenbauer(2, a, x) == -a + x**2 * (2 * a**2 + 2 * a)
    assert gegenbauer(3, a, x) == \
        x**3*(4*a**3/3 + 4*a**2 + 8*a/3) + x*(-2*a**2 - 2*a)

    assert gegenbauer(-1, a, x) == 0
    assert gegenbauer(n, Rational(1, 2), x) == legendre(n, x)
    assert gegenbauer(n, 1, x) == chebyshevu(n, x)
    assert gegenbauer(n, -1, x) == 0

    assert gegenbauer(n, -2, -1) == gegenbauer(n, -2, -1, evaluate=False)

    X = gegenbauer(n, a, x)
    assert isinstance(X, gegenbauer)

    assert gegenbauer(n, a, -x) == (-1)**n * gegenbauer(n, a, x)
    assert gegenbauer(n, a, 0) == 2**n*sqrt(pi) * \
        gamma(a + n/2)/(gamma(a)*gamma(-n/2 + Rational(1, 2))*gamma(n + 1))
    assert gegenbauer(n, a,
                      1) == gamma(2 * a + n) / (gamma(2 * a) * gamma(n + 1))

    assert gegenbauer(n, Rational(3, 4), -1) == zoo

    m = Symbol("m", positive=True)
    assert gegenbauer(m, a, oo) == oo * RisingFactorial(a, m)
    assert gegenbauer(n, a, oo) == gegenbauer(n, a, oo, evaluate=False)

    assert conjugate(gegenbauer(n,
                                a, x)) == gegenbauer(n, conjugate(a),
                                                     conjugate(x))

    assert diff(gegenbauer(n, a, x), n) == Derivative(gegenbauer(n, a, x), n)
    assert diff(gegenbauer(n, a, x), x) == 2 * a * gegenbauer(n - 1, a + 1, x)

    pytest.raises(ArgumentIndexError, lambda: gegenbauer(n, a, x).fdiff(4))

    # XXX see issue sympy/sympy#5539
    assert str(gegenbauer(n, a, x).diff(a)) == \
        ("Sum((2*(-1)**(n - _k) + 2)*(a + _k)*gegenbauer(_k, a, x)/((n - "
         "_k)*(2*a + n + _k)) + (2/(2*a + n + _k) + (2*_k + 2)/((2*a + "
         "_k)*(2*a + 2*_k + 1)))*gegenbauer(n, a, x), (_k, 0, n - 1))")