Ejemplo n.º 1
0
def test_simple_products():
    assert product(2, (k, a, n)) == 2**(n - a + 1)
    assert product(k, (k, 1, n)) == factorial(n)
    assert product(k**3, (k, 1, n)) == factorial(n)**3

    assert product(k + 1, (k, 0, n - 1)) == factorial(n)
    assert product(k + 1, (k, a, n - 1)) == rf(1 + a, n - a)

    assert product(cos(k), (k, 0, 5)) == cos(1)*cos(2)*cos(3)*cos(4)*cos(5)
    assert product(cos(k), (k, 3, 5)) == cos(3)*cos(4)*cos(5)
    assert product(cos(k), (k, 1, Rational(5, 2))) != cos(1)*cos(2)

    assert isinstance(product(k**k, (k, 1, n)), Product)

    assert Product(x**k, (k, 1, n)).variables == [k]

    pytest.raises(ValueError, lambda: Product(n))
    pytest.raises(ValueError, lambda: Product(n*k))
    pytest.raises(ValueError, lambda: Product(n, k))
    pytest.raises(ValueError, lambda: Product(n, k, 1))
    pytest.raises(ValueError, lambda: Product(n, k, 1, 10))
    pytest.raises(ValueError, lambda: Product(n, (k, 1)))

    assert product(1, (n, 1, oo)) == 1  # issue sympy/sympy#8301
    assert product(2, (n, 1, oo)) == oo
    assert isinstance(product(-1, (n, 1, oo)), Product)

    assert product(Kd(n, m), (m, 1, 3)) == 0
    assert product(Kd(n, m), (m, 1, 1)) == Kd(n, 1)
Ejemplo n.º 2
0
def test_evalf_integer_parts():
    a = floor(log(8)/log(2) - exp(-1000), evaluate=False)
    b = floor(log(8)/log(2), evaluate=False)
    assert a.evalf() == 3
    assert b.evalf() == 3
    # equals, as a fallback, can still fail but it might succeed as here
    assert ceiling(10*(sin(1)**2 + cos(1)**2)) == 10

    assert int(floor(factorial(50)/E, evaluate=False).evalf(70)) == \
        int(11188719610782480504630258070757734324011354208865721592720336800)
    assert int(ceiling(factorial(50)/E, evaluate=False).evalf(70)) == \
        int(11188719610782480504630258070757734324011354208865721592720336801)
    assert int(floor((GoldenRatio**999 / sqrt(5) + Rational(1, 2)))
               .evalf(1000)) == fibonacci(999)
    assert int(floor((GoldenRatio**1000 / sqrt(5) + Rational(1, 2)))
               .evalf(1000)) == fibonacci(1000)

    assert ceiling(x).evalf(subs={x: 3}) == 3
    assert ceiling(x).evalf(subs={x: 3*I}) == 3*I
    assert ceiling(x).evalf(subs={x: 2 + 3*I}) == 2 + 3*I

    # issue sympy/sympy#10323
    l = 1206577996382235787095214
    y = ceiling(sqrt(l))
    assert y == 1098443442506
    assert y**2 >= l

    def check(x):
        c, f = ceiling(sqrt(x)), floor(sqrt(x))
        assert (c - 1)**2 < x and c**2 >= x
        assert (f + 1)**2 > x and f**2 <= x

    check(2**30 + 1)
    check(2**100 + 1)
    check(2**112 + 2)
Ejemplo n.º 3
0
def test_factorial_rewrite():
    n = Symbol('n', integer=True)
    k = Symbol('k', integer=True, nonnegative=True)

    assert factorial(n).rewrite(gamma) == gamma(n + 1)
    assert str(factorial(k).rewrite(Product)) == 'Product(_i, (_i, 1, k))'
    assert isinstance(factorial(n).rewrite(Product), factorial)
    assert factorial(n).rewrite('tractable') == exp(loggamma(n + 1))
Ejemplo n.º 4
0
def test_binomial_rewrite():
    n = Symbol('n', integer=True)
    k = Symbol('k', integer=True)

    assert binomial(n, k).rewrite(
        factorial) == factorial(n)/(factorial(k)*factorial(n - k))
    assert binomial(
        n, k).rewrite(gamma) == gamma(n + 1)/(gamma(k + 1)*gamma(n - k + 1))
Ejemplo n.º 5
0
def test_special_products():
    # Wallis product
    assert product((4*k)**2 / (4*k**2 - 1), (k, 1, n)) == \
        4**n*factorial(n)**2/rf(Rational(1, 2), n)/rf(Rational(3, 2), n)

    # Euler's product formula for sin
    assert product(1 + a/k**2, (k, 1, n)) == \
        rf(1 - sqrt(-a), n)*rf(1 + sqrt(-a), n)/factorial(n)**2
Ejemplo n.º 6
0
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)

    pytest.raises(ArgumentIndexError, lambda: factorial(n).fdiff(2))
Ejemplo n.º 7
0
def test_wallis_product():
    # Wallis product, given in two different forms to ensure that Product
    # can factor simple rational expressions
    A = Product(4*n**2 / (4*n**2 - 1), (n, 1, b))
    B = Product((2*n)*(2*n)/(2*n - 1)/(2*n + 1), (n, 1, b))
    half = Rational(1, 2)
    R = pi/2 * factorial(b)**2 / factorial(b - half) / factorial(b + half)
    assert simplify(A.doit()) == R
    assert simplify(B.doit()) == R
Ejemplo n.º 8
0
def test_factorial():
    f = factorial(x)
    assert limit(f, x, oo) == oo
    assert limit(x/f, x, oo) == 0
    # see Stirling's approximation:
    # https//en.wikipedia.org/wiki/Stirling's_approximation
    assert limit(f/(sqrt(2*pi*x)*(x/E)**x), x, oo) == 1
    assert limit(f, x, -oo) == factorial(-oo)
    assert (limit(f, x, x**2) - factorial(x**2)).simplify() == 0
    assert (limit(f, x, -x**2) - factorial(-x**2)).simplify() == 0
Ejemplo n.º 9
0
def test_evalf_sum():
    assert Sum(n, (n, 1, 2)).evalf() == 3.
    assert Sum(I*n, (n, 1, 2)).evalf() == 3.*I
    assert Sum(n, (n, 1, 2)).doit().evalf() == 3.
    # the next test should return instantly
    assert Sum(1/n, (n, 1, 2)).evalf() == 1.5

    # issue sympy/sympy#8219
    assert Sum(E/factorial(n), (n, 0, oo)).evalf() == (E*E).evalf()
    # issue sympy/sympy#8254
    assert Sum(2**n*n/factorial(n), (n, 0, oo)).evalf() == (2*E*E).evalf()
    # issue sympy/sympy#8411
    s = Sum(1/x**2, (x, 100, oo))
    assert s.evalf() == s.doit().evalf()
Ejemplo n.º 10
0
def test_ff_eval_apply():
    x, y = symbols('x,y')

    assert ff(nan, y) == nan

    assert ff(x, y) == ff(x, y)

    assert ff(oo, 0) == 1
    assert ff(-oo, 0) == 1

    assert ff(oo, 6) == oo
    assert ff(-oo, 7) == -oo

    assert ff(oo, -6) == oo
    assert ff(-oo, -7) == oo
    assert ff(-oo, 2) == oo

    assert ff(x, 0) == 1
    assert ff(x, 1) == x
    assert ff(x, 2) == x*(x - 1)
    assert ff(x, 3) == x*(x - 1)*(x - 2)
    assert ff(x, 5) == x*(x - 1)*(x - 2)*(x - 3)*(x - 4)

    assert ff(x, -1) == 1/(x + 1)
    assert ff(x, -2) == 1/((x + 1)*(x + 2))
    assert ff(x, -3) == 1/((x + 1)*(x + 2)*(x + 3))

    assert ff(100, 100) == factorial(100)

    n = Symbol('n', integer=True)
    k = Symbol('k', integer=True)
    m = Symbol('m', integer=True, nonnegative=True)
    assert ff(x, m).is_integer is None
    assert ff(n, k).is_integer is None
    assert ff(n, m).is_integer is True
Ejemplo n.º 11
0
def test_evalf_fast_series():
    # Euler transformed series for sqrt(1+x)
    assert NS(Sum(fac(2*n + 1)/fac(n)**2/2**(3*n + 1), (n, 0, oo)),
              100, strict=False) == NS(sqrt(2), 100)

    # Some series for exp(1)
    estr = NS(E, 100)
    assert NS(Sum(1/fac(n), (n, 0, oo)), 100, strict=False) == estr
    assert NS(1/Sum((1 - 2*n)/fac(2*n), (n, 0, oo)), 100) == estr
    assert NS(Sum((2*n + 1)/fac(2*n), (n, 0, oo)), 100, strict=False) == estr
    assert NS(Sum((4*n + 3)/2**(2*n + 1)/fac(2*n + 1), (n, 0, oo))**2,
              100, strict=False) == estr

    pistr = NS(pi, 100)
    # Ramanujan series for pi
    assert NS(9801/sqrt(8)/Sum(fac(4*n)*(1103 + 26390*n)/fac(n)**4/396**(4*n),
                               (n, 0, oo)), 100, strict=False) == pistr
    assert NS(1/Sum(
        binomial(2*n, n)**3 * (42*n + 5)/2**(12*n + 4), (n, 0, oo)), 100) == pistr
    # Machin's formula for pi
    assert NS(16*Sum((-1)**n/(2*n + 1)/5**(2*n + 1), (n, 0, oo)) -
              4*Sum((-1)**n/(2*n + 1)/239**(2*n + 1), (n, 0, oo)), 100) == pistr

    # Apery's constant
    astr = NS(zeta(3), 100)
    P = 126392*n**5 + 412708*n**4 + 531578*n**3 + 336367*n**2 + 104000 * \
        n + 12463
    assert NS(Sum((-1)**n * P / 24 * (fac(2*n + 1)*fac(2*n)*fac(n))**3 /
                  fac(3*n + 2) / fac(4*n + 3)**3, (n, 0, oo)),
              100, strict=False) == astr
    assert NS(Sum((-1)**n * (205*n**2 + 250*n + 77)/64 * fac(n)**10 /
                  fac(2*n + 1)**5, (n, 0, oo)), 100, strict=False) == astr

    pytest.raises(ValueError, lambda: Sum(factorial(n), (n, 0, oo)).evalf())
Ejemplo n.º 12
0
def test_limit():
    assert gruntz(x, x) == oo
    assert gruntz(-x, x) == -oo
    assert gruntz(-x, x) == -oo
    assert gruntz((-x)**2, x) == oo
    assert gruntz(-x**2, x) == -oo
    assert gruntz((1/x)*log(1/x), x) == 0  # Gruntz: p15, 2.11
    assert gruntz(1/x, x) == 0
    assert gruntz(exp(x), x) == oo
    assert gruntz(-exp(x), x) == -oo
    assert gruntz(exp(x)/x, x) == oo
    assert gruntz(1/x - exp(-x), x) == 0
    assert gruntz(x + 1/x, x) == oo

    assert gruntz((1/x)**(1/x), x) == 1  # Gruntz: p15, 2.11
    assert gruntz((exp(1/x) - 1)*x, x) == 1
    assert gruntz(1 + 1/x, x) == 1
    assert gruntz(-exp(1/x), x) == -1
    assert gruntz(x + exp(-x), x) == oo
    assert gruntz(x + exp(-x**2), x) == oo
    assert gruntz(x + exp(-exp(x)), x) == oo
    assert gruntz(13 + 1/x - exp(-x), x) == 13

    a = Symbol('a')
    assert gruntz(x - log(1 + exp(x)), x) == 0
    assert gruntz(x - log(a + exp(x)), x) == 0
    assert gruntz(exp(x)/(1 + exp(x)), x) == 1
    assert gruntz(exp(x)/(a + exp(x)), x) == 1

    assert gruntz((3**x + 5**x)**(1/x), x) == 5  # issue sympy/sympy#3463

    assert gruntz(Ei(x + exp(-x))*exp(-x)*x, x) == 1

    assert gruntz(1/li(x), x) == 0
    assert gruntz(1/Li(x), x) == 0

    # issue diofant/diofant#56
    assert gruntz((log(E + 1/x) - 1)**(1 - sqrt(E + 1/x)), x) == oo

    # issue sympy/sympy#9471
    assert gruntz((((27**(log(x, 3))))/x**3), x) == 1
    assert gruntz((((27**(log(x, 3) + 1)))/x**3), x) == 27

    # issue sympy/sympy#9449
    y = Symbol('y')
    assert gruntz(x*(abs(1/x + y) - abs(y - 1/x))/2, x) == sign(y)

    # issue sympy/sympy#8481
    assert gruntz(m**x * exp(-m) / factorial(x), x) == 0

    # issue sympy/sympy#4187
    assert gruntz(exp(1/x)*log(1/x) - Ei(1/x), x) == -EulerGamma
    assert gruntz(exp(x)*log(x) - Ei(x), x) == oo

    # issue sympy/sympy#10382
    assert gruntz(fibonacci(x + 1)/fibonacci(x), x) == GoldenRatio

    assert gruntz(zeta(x), x) == 1
    assert gruntz(zeta(m)*zeta(x), x) == zeta(m)
Ejemplo n.º 13
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)
Ejemplo n.º 14
0
def test_uniformsum_d():
    n = Symbol("n", integer=True)
    k = Symbol("k")

    X = UniformSum('x', n)
    d = density(X)(x)
    assert d == 1/factorial(n - 1)*Sum((-1)**k*(x - k)**(n - 1) *
                                       binomial(n, k), (k, 0, floor(x)))
Ejemplo n.º 15
0
def test_factorial():
    n = Symbol('n', integer=True)
    assert str(factorial(-2)) == "zoo"
    assert str(factorial(0)) == "1"
    assert str(factorial(7)) == "5040"
    assert str(factorial(n)) == "factorial(n)"
    assert str(factorial(2*n)) == "factorial(2*n)"
    assert str(factorial(factorial(n))) == 'factorial(factorial(n))'
    assert str(factorial(factorial2(n))) == 'factorial(factorial2(n))'
    assert str(factorial2(factorial(n))) == 'factorial2(factorial(n))'
    assert str(factorial2(factorial2(n))) == 'factorial2(factorial2(n))'
    assert str(subfactorial(3)) == "2"
    assert str(subfactorial(n)) == "subfactorial(n)"
    assert str(subfactorial(2*n)) == "subfactorial(2*n)"
Ejemplo n.º 16
0
def test_Poisson():
    l = 3
    x = Poisson('x', l)
    assert E(x) == l
    assert variance(x) == l
    assert density(x) == PoissonDistribution(l)
    assert isinstance(E(x, evaluate=False), Sum)
    assert isinstance(E(2*x, evaluate=False), Sum)
    assert density(x)(z) == 3**z/(exp(3)*factorial(z))
Ejemplo n.º 17
0
def test_user_functions():
    assert fcode(sin(x), user_functions={"sin": "zsin"}) == "      zsin(x)"
    assert fcode(
        gamma(x), user_functions={"gamma": "mygamma"}) == "      mygamma(x)"
    g = Function('g')
    assert fcode(g(x), user_functions={"g": "great"}) == "      great(x)"
    n = symbols('n', integer=True)
    assert fcode(
        factorial(n), user_functions={"factorial": "fct"}) == "      fct(n)"
Ejemplo n.º 18
0
def test_rsolve_hyper():
    assert rsolve_hyper([-1, -1, 1], 0, n) in [
        C0*(Rational(1, 2) - sqrt(5)/2)**n + C1*(Rational(1, 2) + sqrt(5)/2)**n,
        C1*(Rational(1, 2) - sqrt(5)/2)**n + C0*(Rational(1, 2) + sqrt(5)/2)**n,
    ]

    assert rsolve_hyper([n**2 - 2, -2*n - 1, 1], 0, n) in [
        C0*rf(sqrt(2), n) + C1*rf(-sqrt(2), n),
        C1*rf(sqrt(2), n) + C0*rf(-sqrt(2), n),
    ]

    assert rsolve_hyper([n**2 - k, -2*n - 1, 1], 0, n) in [
        C0*rf(sqrt(k), n) + C1*rf(-sqrt(k), n),
        C1*rf(sqrt(k), n) + C0*rf(-sqrt(k), n),
    ]

    assert rsolve_hyper(
        [2*n*(n + 1), -n**2 - 3*n + 2, n - 1], 0, n) == C1*factorial(n) + C0*2**n

    assert rsolve_hyper(
        [n + 2, -(2*n + 3)*(17*n**2 + 51*n + 39), n + 1], 0, n) == 0

    assert rsolve_hyper([-n - 1, -1, 1], 0, n) == 0

    assert rsolve_hyper([-1, 1], n, n).expand() == C0 + n**2/2 - n/2

    assert rsolve_hyper([-1, 1], 1 + n, n).expand() == C0 + n**2/2 + n/2

    assert rsolve_hyper([-1, 1], 3*(n + n**2), n).expand() == C0 + n**3 - n
    assert rsolve_hyper([-1, 1], n + factorial(n), n) is None

    assert rsolve_hyper([-a, 1], 0, n).expand() == C0*a**n

    assert rsolve_hyper([-a, 0, 1], 0, n).expand() == (-1)**n*C1*a**(n/2) + C0*a**(n/2)

    assert rsolve_hyper([1, 1, 1], 0, n).expand() == \
        C0*(-Rational(1, 2) - sqrt(3)*I/2)**n + C1*(-Rational(1, 2) + sqrt(3)*I/2)**n

    assert rsolve_hyper([1, -2*n/a - 2/a, 1], 0, n) == 0

    assert rsolve_hyper([1, 1], sqrt(n), n) is None
    assert rsolve_hyper([1, 1], n + sqrt(n), n) is None
Ejemplo n.º 19
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/2 + b/2 + n/2 + Rational(1, 2))*jacobi(n - 1, a + 1, b + 1, x)

    # XXX see issue sympy/sympy#5539
    assert str(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 str(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))
Ejemplo n.º 20
0
def test_findrecur():
    pytest.raises(ValueError, lambda: Sum(x*y, (x, 0, 2),
                                          (y, 0, 2)).findrecur())
    pytest.raises(ValueError, lambda: Sum(x*y, (x, 0, 2)).findrecur())
    pytest.raises(ValueError, lambda: Sum(x, (x, 0, oo)).findrecur())
    pytest.raises(ValueError, lambda: Sum(sin(x*y)/(x**2 + 1),
                                          (x, 0, oo)).findrecur())

    n, k = symbols("n, k", integer=True)
    F = symbols("F", cls=Function)

    f = Sum(factorial(n)/(factorial(k)*factorial(n - k)), (k, 0, oo))
    g = Sum(k*factorial(n)/(factorial(k)*factorial(n - k)), (k, 0, oo))
    fa = Sum((-1)**k*binomial(n, k)*binomial(2*n - 2*k, n + a), (k, 0, oo))
    fb = Sum(binomial(x, k)*binomial(y, n - k), (k, 0, oo))

    assert f.findrecur(F) == -F(n, k) + F(n - 1, k) + F(n - 1, k - 1)
    assert (Sum(binomial(n, k), (k, 0, oo)).findrecur(F) ==
            -F(n, k) + F(n - 1, k) + F(n - 1, k - 1))
    assert (g.findrecur(F) == (-1 + 1/n)*F(n, k) + F(n - 1, k) +
            F(n - 1, k - 1))
    assert (fa.findrecur(F, n) == F(n - 2, k - 1) -
            (2*n - 1)*F(n - 1, k)/(2*n - 2) -
            (a**2 - n**2)*F(n, k)/(4*n*(n - 1)))
    assert (fb.findrecur(F, n) == -n*F(n, k)/(-n + x + y + 2) +
            (-n + x + 1)*F(n - 1, k - 1)/(-n + x + y + 2) +
            (-n + y + 1)*F(n - 1, k)/(-n + x + y + 2) + F(n - 2, k - 1))
Ejemplo n.º 21
0
def test_multiple_products():
    assert product(x, (n, 1, k), (k, 1, m)) == x**(m**2/2 + m/2)
    assert product(f(n), (
        n, 1, m), (m, 1, k)) == Product(f(n), (n, 1, m), (m, 1, k)).doit()
    assert Product(f(n), (m, 1, k), (n, 1, k)).doit() == \
        Product(Product(f(n), (m, 1, k)), (n, 1, k)).doit() == \
        product(f(n), (m, 1, k), (n, 1, k)) == \
        product(product(f(n), (m, 1, k)), (n, 1, k)) == \
        Product(f(n)**k, (n, 1, k))
    assert Product(
        x, (x, 1, k), (k, 1, n)).doit() == Product(factorial(k), (k, 1, n))

    assert Product(x**k, (n, 1, k), (k, 1, m)).variables == [n, k]
Ejemplo n.º 22
0
def test_simple_products():
    assert Product(nan, (x, 1, 3)) is nan
    assert product(nan, (x, 1, 3)) is nan
    assert Product(x, (n, a, a)).doit() == x
    assert Product(x, (x, a, a)).doit() == a
    assert Product(x, (y, 1, a)).doit() == x**a

    lo, hi = 1, 2
    s1 = Product(n, (n, lo, hi))
    s2 = Product(n, (n, hi, lo))
    assert s1 != s2
    # This IS correct according to Karr product convention
    assert s1.doit() == 2
    assert s2.doit() == 1

    lo, hi = x, x + 1
    s1 = Product(n, (n, lo, hi))
    s2 = Product(n, (n, hi, lo))
    s3 = 1 / Product(n, (n, hi + 1, lo - 1))
    assert s1 != s2
    # This IS correct according to Karr product convention
    assert s1.doit() == x*(x + 1)
    assert s2.doit() == 1
    assert s3.doit() == x*(x + 1)

    assert Product(Integral(2*x, (x, 1, y)) + 2*x, (x, 1, 2)).doit() == \
        (y**2 + 1)*(y**2 + 3)
    assert product(2, (n, a, b)) == 2**(b - a + 1)
    assert product(n, (n, 1, b)) == factorial(b)
    assert product(n**3, (n, 1, b)) == factorial(b)**3
    assert product(3**(2 + n), (n, a, b)) \
        == 3**(2*(1 - a + b) + b/2 + (b**2)/2 + a/2 - (a**2)/2)
    assert product(cos(n), (n, 3, 5)) == cos(3)*cos(4)*cos(5)
    assert product(cos(n), (n, x, x + 2)) == cos(x)*cos(x + 1)*cos(x + 2)
    assert isinstance(product(cos(n), (n, x, x + Rational(1, 2))), Product)
    # If Product managed to evaluate this one, it most likely got it wrong!
    assert isinstance(Product(n**n, (n, 1, b)), Product)
Ejemplo n.º 23
0
def test_bell_perm():
    assert [len(set(generate_bell(i))) for i in range(1, 7)] == [
        factorial(i) for i in range(1, 7)]
    assert list(generate_bell(3)) == [
        (0, 1, 2), (0, 2, 1), (2, 0, 1), (2, 1, 0), (1, 2, 0), (1, 0, 2)]
    # generate_bell and trotterjohnson are advertised to return the same
    # permutations; this is not technically necessary so this test could
    # be removed
    for n in range(1, 5):
        p = Permutation(range(n))
        b = generate_bell(n)
        for bi in b:
            assert bi == tuple(p.array_form)
            p = p.next_trotterjohnson()
    pytest.raises(ValueError, lambda: list(generate_bell(0)))  # XXX is this consistent with other permutation algorithms?
Ejemplo n.º 24
0
def test_gosper_nan():
    a = Symbol('a', positive=True)
    b = Symbol('b', positive=True)
    n = Symbol('n', integer=True)
    m = Symbol('m', integer=True)
    f2d = n*(n + a + b)*a**n*b**n/(factorial(n + a)*factorial(n + b))
    g2d = 1/(factorial(a - 1)*factorial(
        b - 1)) - a**(m + 1)*b**(m + 1)/(factorial(a + m)*factorial(b + m))
    g = gosper_sum(f2d, (n, 0, m))
    assert simplify(g - g2d) == 0
Ejemplo n.º 25
0
def test_hypersum():
    assert simplify(summation(x**n/fac(n), (n, 1, oo))) == -1 + exp(x)
    assert summation((-1)**n * x**(2*n) / fac(2*n), (n, 0, oo)) == cos(x)
    assert simplify(summation((-1)**n*x**(2*n + 1) /
                              factorial(2*n + 1), (n, 3, oo))) == -x + sin(x) + x**3/6 - x**5/120

    assert summation(1/(n + 2)**3, (n, 1, oo)) == -Rational(9, 8) + zeta(3)
    assert summation(1/n**4, (n, 1, oo)) == pi**4/90

    s = summation(x**n*n, (n, -oo, 0))
    assert s.is_Piecewise
    assert s.args[0].args[0] == -1/(x*(1 - 1/x)**2)
    assert s.args[0].args[1] == (abs(1/x) < 1)

    m = Symbol('n', integer=True, positive=True)
    assert summation(binomial(m, k), (k, 0, m)) == 2**m
Ejemplo n.º 26
0
def test_Ynm():
    # https//en.wikipedia.org/wiki/Spherical_harmonics
    th, ph = Symbol("theta", extended_real=True), Symbol("phi",
                                                         extended_real=True)

    assert Ynm(0, 0, th, ph).expand(func=True) == 1 / (2 * sqrt(pi))
    assert Ynm(1, -1, th, ph) == -exp(-2 * I * ph) * Ynm(1, 1, th, ph)
    assert Ynm(1, -1, th, ph).expand(
        func=True) == sqrt(6) * sin(th) * exp(-I * ph) / (4 * sqrt(pi))
    assert Ynm(1, -1, th, ph).expand(
        func=True) == sqrt(6) * sin(th) * exp(-I * ph) / (4 * sqrt(pi))
    assert Ynm(1, 0, th,
               ph).expand(func=True) == sqrt(3) * cos(th) / (2 * sqrt(pi))
    assert Ynm(1, 1, th, ph).expand(
        func=True) == -sqrt(6) * sin(th) * exp(I * ph) / (4 * sqrt(pi))
    assert Ynm(2, 0, th, ph).expand(
        func=True
    ) == 3 * sqrt(5) * cos(th)**2 / (4 * sqrt(pi)) - sqrt(5) / (4 * sqrt(pi))
    assert Ynm(2, 1, th, ph).expand(func=True) == -sqrt(30) * sin(th) * exp(
        I * ph) * cos(th) / (4 * sqrt(pi))
    assert Ynm(
        2, -2, th,
        ph).expand(func=True) == (-sqrt(30) * exp(-2 * I * ph) * cos(th)**2 /
                                  (8 * sqrt(pi)) +
                                  sqrt(30) * exp(-2 * I * ph) / (8 * sqrt(pi)))
    assert Ynm(
        2, 2, th,
        ph).expand(func=True) == (-sqrt(30) * exp(2 * I * ph) * cos(th)**2 /
                                  (8 * sqrt(pi)) + sqrt(30) * exp(2 * I * ph) /
                                  (8 * sqrt(pi)))

    assert diff(Ynm(
        n, m, th, ph), th) == (m * cot(th) * Ynm(n, m, th, ph) + sqrt(
            (-m + n) * (m + n + 1)) * exp(-I * ph) * Ynm(n, m + 1, th, ph))
    assert diff(Ynm(n, m, th, ph), ph) == I * m * Ynm(n, m, th, ph)
    pytest.raises(ArgumentIndexError, lambda: Ynm(n, m, th, ph).fdiff(1))

    assert conjugate(
        Ynm(n, m, th,
            ph)) == (-1)**(2 * m) * exp(-2 * I * m * ph) * Ynm(n, m, th, ph)

    assert Ynm(n, m, -th, ph) == Ynm(n, m, th, ph)
    assert Ynm(n, m, th, -ph) == exp(-2 * I * m * ph) * Ynm(n, m, th, ph)
    assert Ynm(n, -m, th,
               ph) == (-1)**m * exp(-2 * I * m * ph) * Ynm(n, m, th, ph)

    assert (Ynm(n, m, th, ph).rewrite(sin) == Ynm(
        n, m, th, ph).rewrite(cos) == exp(I * m * ph) * sqrt(
            (2 * n + 1) * factorial(-m + n) / factorial(m + n)) *
            assoc_legendre(n, m, cos(th)) / (2 * sqrt(pi)))
    assert (Ynm(n, m, th, ph).as_real_imag() == (
        sqrt((2 * n + 1) * factorial(-m + n) / factorial(m + n)) *
        cos(m * ph) * assoc_legendre(n, m, cos(th)) / (2 * sqrt(pi)),
        sqrt((2 * n + 1) * factorial(-m + n) / factorial(m + n)) *
        sin(m * ph) * assoc_legendre(n, m, cos(th)) / (2 * sqrt(pi))))
Ejemplo n.º 27
0
def test_bell_perm():
    assert [len(set(generate_bell(i)))
            for i in range(1, 7)] == [factorial(i) for i in range(1, 7)]
    assert list(generate_bell(3)) == [(0, 1, 2), (0, 2, 1), (2, 0, 1),
                                      (2, 1, 0), (1, 2, 0), (1, 0, 2)]
    # generate_bell and trotterjohnson are advertised to return the same
    # permutations; this is not technically necessary so this test could
    # be removed
    for n in range(1, 5):
        p = Permutation(range(n))
        b = generate_bell(n)
        for bi in b:
            assert bi == tuple(p.array_form)
            p = p.next_trotterjohnson()
    pytest.raises(ValueError, lambda: list(generate_bell(
        0)))  # XXX is this consistent with other permutation algorithms?
Ejemplo n.º 28
0
def test_hypersum():
    assert simplify(summation(x**n/fac(n), (n, 1, oo))) == -1 + exp(x)
    assert summation((-1)**n * x**(2*n) / fac(2*n), (n, 0, oo)) == cos(x)
    assert simplify(summation((-1)**n*x**(2*n + 1) /
                              factorial(2*n + 1), (n, 3, oo))) == -x + sin(x) + x**3/6 - x**5/120

    assert summation(1/(n + 2)**3, (n, 1, oo)) == -Rational(9, 8) + zeta(3)
    assert summation(1/n**4, (n, 1, oo)) == pi**4/90

    s = summation(x**n*n, (n, -oo, 0))
    assert s.is_Piecewise
    assert s.args[0].args[0] == -1/(x*(1 - 1/x)**2)
    assert s.args[0].args[1] == (abs(1/x) < 1)

    m = Symbol('n', integer=True, positive=True)
    assert summation(binomial(m, k), (k, 0, m)) == 2**m
Ejemplo n.º 29
0
def test_gosper_sum_AeqB_part2():
    f2a = n**2*a**n
    f2b = (n - r/2)*binomial(r, n)
    f2c = factorial(n - 1)**2/(factorial(n - x)*factorial(n + x))

    g2a = -a*(a + 1)/(a - 1)**3 + a**(
        m + 1)*(a**2*m**2 - 2*a*m**2 + m**2 - 2*a*m + 2*m + a + 1)/(a - 1)**3
    g2b = (m - r)*binomial(r, m)/2
    ff = factorial(1 - x)*factorial(1 + x)
    g2c = 1/ff*(
        1 - 1/x**2) + factorial(m)**2/(x**2*factorial(m - x)*factorial(m + x))

    g = gosper_sum(f2a, (n, 0, m))
    assert g is not None and simplify(g - g2a) == 0
    g = gosper_sum(f2b, (n, 0, m))
    assert g is not None and simplify(g - g2b) == 0
    g = gosper_sum(f2c, (n, 1, m))
    assert g is not None and simplify(g - g2c) == 0
Ejemplo n.º 30
0
def test_gosper_sum_AeqB_part2():
    f2a = n**2*a**n
    f2b = (n - r/2)*binomial(r, n)
    f2c = factorial(n - 1)**2/(factorial(n - x)*factorial(n + x))

    g2a = -a*(a + 1)/(a - 1)**3 + a**(
        m + 1)*(a**2*m**2 - 2*a*m**2 + m**2 - 2*a*m + 2*m + a + 1)/(a - 1)**3
    g2b = (m - r)*binomial(r, m)/2
    ff = factorial(1 - x)*factorial(1 + x)
    g2c = 1/ff*(
        1 - 1/x**2) + factorial(m)**2/(x**2*factorial(m - x)*factorial(m + x))

    g = gosper_sum(f2a, (n, 0, m))
    assert g is not None and simplify(g - g2a) == 0
    g = gosper_sum(f2b, (n, 0, m))
    assert g is not None and simplify(g - g2b) == 0
    g = gosper_sum(f2c, (n, 1, m))
    assert g is not None and simplify(g - g2c) == 0
Ejemplo n.º 31
0
def test_evalf_fast_series_sympyissue_4021():
    # Catalan's constant
    assert NS(Sum((-1)**(n - 1) * 2**(8 * n) * (40 * n**2 - 24 * n + 3) *
                  factorial(2 * n)**3 * factorial(n)**2 / n**3 /
                  (2 * n - 1) / factorial(4 * n)**2, (n, 1, oo)) / 64,
              100,
              strict=False) == NS(Catalan, 100)
    astr = NS(zeta(3), 100)
    assert NS(
        5 * Sum((-1)**(n - 1) * factorial(n)**2 / n**3 / factorial(2 * n),
                (n, 1, oo)) / 2, 100) == astr
    assert NS(Sum((-1)**(n - 1) * (56 * n**2 - 32 * n + 5) /
                  (2 * n - 1)**2 * factorial(n - 1)**3 / factorial(3 * n),
                  (n, 1, oo)) / 4,
              100,
              strict=False) == astr
Ejemplo n.º 32
0
def test_rsolve_hyper():
    assert rsolve_hyper([-1, -1, 1], 0, n) in [
        C0 * (S.Half - S.Half * sqrt(5))**n + C1 *
        (S.Half + S.Half * sqrt(5))**n,
        C1 * (S.Half - S.Half * sqrt(5))**n + C0 *
        (S.Half + S.Half * sqrt(5))**n,
    ]

    assert rsolve_hyper([n**2 - 2, -2 * n - 1, 1], 0, n) in [
        C0 * rf(sqrt(2), n) + C1 * rf(-sqrt(2), n),
        C1 * rf(sqrt(2), n) + C0 * rf(-sqrt(2), n),
    ]

    assert rsolve_hyper([n**2 - k, -2 * n - 1, 1], 0, n) in [
        C0 * rf(sqrt(k), n) + C1 * rf(-sqrt(k), n),
        C1 * rf(sqrt(k), n) + C0 * rf(-sqrt(k), n),
    ]

    assert rsolve_hyper([2 * n * (n + 1), -n**2 - 3 * n + 2, n - 1], 0,
                        n) == C1 * factorial(n) + C0 * 2**n

    assert rsolve_hyper(
        [n + 2, -(2 * n + 3) * (17 * n**2 + 51 * n + 39), n + 1], 0, n) == 0

    assert rsolve_hyper([-n - 1, -1, 1], 0, n) == 0

    assert rsolve_hyper([-1, 1], n, n).expand() == C0 + n**2 / 2 - n / 2

    assert rsolve_hyper([-1, 1], 1 + n, n).expand() == C0 + n**2 / 2 + n / 2

    assert rsolve_hyper([-1, 1], 3 * (n + n**2), n).expand() == C0 + n**3 - n

    assert rsolve_hyper([-a, 1], 0, n).expand() == C0 * a**n

    assert rsolve_hyper(
        [-a, 0, 1], 0,
        n).expand() == (-1)**n * C1 * a**(n / 2) + C0 * a**(n / 2)

    assert rsolve_hyper([1, 1, 1], 0, n).expand() == \
        C0*(-Rational(1, 2) - sqrt(3)*I/2)**n + C1*(-Rational(1, 2) + sqrt(3)*I/2)**n

    assert rsolve_hyper([1, -2 * n / a - 2 / a, 1], 0, n) == 0

    assert rsolve_hyper([1, 1], sqrt(n), n) is None
    assert rsolve_hyper([1, 1], n + sqrt(n), n) is None
Ejemplo n.º 33
0
def test_hypersimp():
    n, k = symbols('n,k', integer=True)

    assert hypersimp(factorial(k), k) == k + 1
    assert hypersimp(factorial(k**2), k) is None

    assert hypersimp(1/factorial(k), k) == 1/(k + 1)

    assert hypersimp(2**k/factorial(k)**2, k) == 2/(k + 1)**2

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

    term = (4*k + 1)*factorial(k)/factorial(2*k + 1)
    assert hypersimp(term, k) == ((4*k + 5)/(3 + 14*k + 8*k**2))/2

    term = 1/((2*k - 1)*factorial(2*k + 1))
    assert hypersimp(term, k) == (k - Rational(1, 2))/((k + 1)*(2*k + 1)*(2*k + 3))

    term = binomial(n, k)*(-1)**k/factorial(k)
    assert hypersimp(term, k) == (k - n)/(k + 1)**2
Ejemplo n.º 34
0
def test_maxima_functions():
    assert parse_maxima('expand( (x+1)^2)') == x**2 + 2*x + 1
    assert parse_maxima('factor( x**2 + 2*x + 1)') == (x + 1)**2
    assert parse_maxima('2*cos(x)^2 + sin(x)^2') == 2*cos(x)**2 + sin(x)**2
    assert parse_maxima('trigexpand(sin(2*x)+cos(2*x))') == \
        -1 + 2*cos(x)**2 + 2*cos(x)*sin(x)
    assert parse_maxima('solve(x^2-4,x)') == [{x: -2}, {x: 2}]
    assert parse_maxima('limit((1+1/x)^x,x,inf)') == E
    assert parse_maxima('limit(sqrt(-x)/x,x,0,minus)') == -oo
    assert parse_maxima('diff(x^x, x)') == x**x*(1 + log(x))
    assert parse_maxima('sum(k, k, 1, n)',
                        name_dict={'k': Symbol('k', integer=True),
                                   'n': n}) == (n**2 + n)/2
    assert parse_maxima('product(k, k, 1, n)',
                        name_dict={'k': Symbol('k', integer=True),
                                   'n': n}) == factorial(n)
    assert parse_maxima('ratsimp((x^2-1)/(x+1))') == x - 1
    assert Abs( parse_maxima(
        'float(sec(%pi/3) + csc(%pi/3))') - 3.154700538379252) < 10**(-5)
Ejemplo n.º 35
0
def test_rf_eval_apply():
    x, y = symbols('x,y')

    assert rf(nan, y) == nan

    assert rf(x, y) == rf(x, y)

    assert rf(oo, 0) == 1
    assert rf(-oo, 0) == 1

    assert rf(oo, 6) == oo
    assert rf(-oo, 7) == -oo

    assert rf(oo, -6) == oo
    assert rf(-oo, -7) == oo
    assert rf(-oo, 2) == oo

    assert rf(x, 0) == 1
    assert rf(x, 1) == x
    assert rf(x, 2) == x*(x + 1)
    assert rf(x, 3) == x*(x + 1)*(x + 2)
    assert rf(x, 5) == x*(x + 1)*(x + 2)*(x + 3)*(x + 4)

    assert rf(x, -1) == 1/(x - 1)
    assert rf(x, -2) == 1/((x - 1)*(x - 2))
    assert rf(x, -3) == 1/((x - 1)*(x - 2)*(x - 3))

    assert rf(1, 100) == factorial(100)

    n = Symbol('n', integer=True)
    k = Symbol('k', integer=True)
    m = Symbol('m', integer=True, nonnegative=True)
    assert rf(x, m).is_integer is None
    assert rf(n, k).is_integer is None
    assert rf(n, m).is_integer is True

    assert rf(x, y).rewrite('tractable') == \
        exp(-loggamma(x))*exp(loggamma(x + y))
Ejemplo n.º 36
0
def test_rf_eval_apply():
    x, y = symbols('x,y')

    assert rf(nan, y) == nan

    assert rf(x, y) == rf(x, y)

    assert rf(oo, 0) == 1
    assert rf(-oo, 0) == 1

    assert rf(oo, 6) == oo
    assert rf(-oo, 7) == -oo

    assert rf(oo, -6) == oo
    assert rf(-oo, -7) == oo
    assert rf(-oo, 2) == oo

    assert rf(x, 0) == 1
    assert rf(x, 1) == x
    assert rf(x, 2) == x * (x + 1)
    assert rf(x, 3) == x * (x + 1) * (x + 2)
    assert rf(x, 5) == x * (x + 1) * (x + 2) * (x + 3) * (x + 4)

    assert rf(x, -1) == 1 / (x - 1)
    assert rf(x, -2) == 1 / ((x - 1) * (x - 2))
    assert rf(x, -3) == 1 / ((x - 1) * (x - 2) * (x - 3))

    assert rf(1, 100) == factorial(100)

    n = Symbol('n', integer=True)
    k = Symbol('k', integer=True)
    m = Symbol('m', integer=True, nonnegative=True)
    assert rf(x, m).is_integer is None
    assert rf(n, k).is_integer is None
    assert rf(n, m).is_integer is True

    assert rf(x, y).rewrite('tractable') == \
        exp(-loggamma(x))*exp(loggamma(x + y))
Ejemplo n.º 37
0
def test_evalf_fast_series():
    # Euler transformed series for sqrt(1+x)
    assert NS(Sum(fac(2*n + 1)/fac(n)**2/2**(3*n + 1), (n, 0, oo)),
              100, strict=False) == NS(sqrt(2), 100)

    # Some series for exp(1)
    estr = NS(E, 100)
    assert NS(Sum(1/fac(n), (n, 0, oo)), 100, strict=False) == estr
    assert NS(1/Sum((1 - 2*n)/fac(2*n), (n, 0, oo)), 100) == estr
    assert NS(Sum((2*n + 1)/fac(2*n), (n, 0, oo)), 100, strict=False) == estr
    assert NS(Sum((4*n + 3)/2**(2*n + 1)/fac(2*n + 1), (n, 0, oo))**2,
              100, strict=False) == estr

    pistr = NS(pi, 100)
    # Ramanujan series for pi
    assert NS(9801/sqrt(8)/Sum(fac(4*n)*(1103 + 26390*n)/fac(n)**4/396**(4*n),
                               (n, 0, oo)), 100, strict=False) == pistr
    assert NS(1/Sum(
        binomial(2*n, n)**3 * (42*n + 5)/2**(12*n + 4), (n, 0, oo)), 100) == pistr
    # Machin's formula for pi
    assert NS(16*Sum((-1)**n/(2*n + 1)/5**(2*n + 1), (n, 0, oo)) -
              4*Sum((-1)**n/(2*n + 1)/239**(2*n + 1), (n, 0, oo)), 100) == pistr

    # Apery's constant
    astr = NS(zeta(3), 100)
    P = 126392*n**5 + 412708*n**4 + 531578*n**3 + 336367*n**2 + 104000 * \
        n + 12463
    assert NS(Sum((-1)**n * P / 24 * (fac(2*n + 1)*fac(2*n)*fac(n))**3 /
                  fac(3*n + 2) / fac(4*n + 3)**3, (n, 0, oo)),
              100, strict=False) == astr
    assert NS(Sum((-1)**n * (205*n**2 + 250*n + 77)/64 * fac(n)**10 /
                  fac(2*n + 1)**5, (n, 0, oo)), 100, strict=False) == astr

    pytest.raises(ValueError, lambda: Sum(factorial(n), (n, 0, oo)).evalf())

    # issue sympy/sympy#6274
    assert NS(Sum(n, (n, 10, 5))) == '-30.0000000000000'
Ejemplo n.º 38
0
def test_findrecur():
    a, x, y = symbols("a, x, y")
    n, k = symbols("n, k", integer=True)
    F = symbols("F", cls=Function)

    f = Sum(factorial(n) / (factorial(k) * factorial(n - k)), (k, 0, oo))
    g = Sum(k * factorial(n) / (factorial(k) * factorial(n - k)), (k, 0, oo))
    fa = Sum((-1)**k * binomial(n, k) * binomial(2 * n - 2 * k, n + a),
             (k, 0, oo))
    fb = Sum(binomial(x, k) * binomial(y, n - k), (k, 0, oo))

    assert f.findrecur(F) == -F(n, k) + F(n - 1, k) + F(n - 1, k - 1)
    assert (Sum(binomial(n, k), (k, 0, oo)).findrecur(F) == -F(n, k) +
            F(n - 1, k) + F(n - 1, k - 1))
    assert (g.findrecur(F) == (-1 + 1 / n) * F(n, k) + F(n - 1, k) +
            F(n - 1, k - 1))
    assert (fa.findrecur(F, n) == F(n - 2, k - 1) - (2 * n - 1) * F(n - 1, k) /
            (2 * n - 2) - (a**2 - n**2) * F(n, k) / (4 * n * (n - 1)))
    assert (fb.findrecur(F, n) == -n * F(n, k) / (-n + x + y + 2) +
            (-n + x + 1) * F(n - 1, k - 1) / (-n + x + y + 2) +
            (-n + y + 1) * F(n - 1, k) / (-n + x + y + 2) + F(n - 2, k - 1))
Ejemplo n.º 39
0
def test_Ynm():
    # https//en.wikipedia.org/wiki/Spherical_harmonics
    th, ph = Symbol("theta", extended_real=True), Symbol("phi", extended_real=True)

    assert Ynm(0, 0, th, ph).expand(func=True) == 1/(2*sqrt(pi))
    assert Ynm(1, -1, th, ph) == -exp(-2*I*ph)*Ynm(1, 1, th, ph)
    assert Ynm(1, -1, th, ph).expand(func=True) == sqrt(6)*sin(th)*exp(-I*ph)/(4*sqrt(pi))
    assert Ynm(1, -1, th, ph).expand(func=True) == sqrt(6)*sin(th)*exp(-I*ph)/(4*sqrt(pi))
    assert Ynm(1, 0, th, ph).expand(func=True) == sqrt(3)*cos(th)/(2*sqrt(pi))
    assert Ynm(1, 1, th, ph).expand(func=True) == -sqrt(6)*sin(th)*exp(I*ph)/(4*sqrt(pi))
    assert Ynm(2, 0, th, ph).expand(func=True) == 3*sqrt(5)*cos(th)**2/(4*sqrt(pi)) - sqrt(5)/(4*sqrt(pi))
    assert Ynm(2, 1, th, ph).expand(func=True) == -sqrt(30)*sin(th)*exp(I*ph)*cos(th)/(4*sqrt(pi))
    assert Ynm(2, -2, th, ph).expand(func=True) == (-sqrt(30)*exp(-2*I*ph)*cos(th)**2/(8*sqrt(pi))
                                                    + sqrt(30)*exp(-2*I*ph)/(8*sqrt(pi)))
    assert Ynm(2, 2, th, ph).expand(func=True) == (-sqrt(30)*exp(2*I*ph)*cos(th)**2/(8*sqrt(pi))
                                                   + sqrt(30)*exp(2*I*ph)/(8*sqrt(pi)))

    assert diff(Ynm(n, m, th, ph), th) == (m*cot(th)*Ynm(n, m, th, ph)
                                           + sqrt((-m + n)*(m + n + 1))*exp(-I*ph)*Ynm(n, m + 1, th, ph))
    assert diff(Ynm(n, m, th, ph), ph) == I*m*Ynm(n, m, th, ph)
    pytest.raises(ArgumentIndexError, lambda: Ynm(n, m, th, ph).fdiff(1))

    assert conjugate(Ynm(n, m, th, ph)) == (-1)**(2*m)*exp(-2*I*m*ph)*Ynm(n, m, th, ph)

    assert Ynm(n, m, -th, ph) == Ynm(n, m, th, ph)
    assert Ynm(n, m, th, -ph) == exp(-2*I*m*ph)*Ynm(n, m, th, ph)
    assert Ynm(n, -m, th, ph) == (-1)**m*exp(-2*I*m*ph)*Ynm(n, m, th, ph)

    assert (Ynm(n, m, th, ph).rewrite(sin) ==
            Ynm(n, m, th, ph).rewrite(cos) ==
            exp(I*m*ph)*sqrt((2*n + 1)*factorial(-m + n)/factorial(m + n)) *
            assoc_legendre(n, m, cos(th))/(2*sqrt(pi)))
    assert (Ynm(n, m, th, ph).as_real_imag() ==
            (sqrt((2*n + 1)*factorial(-m + n)/factorial(m + n))*cos(m*ph) *
             assoc_legendre(n, m, cos(th))/(2*sqrt(pi)),
             sqrt((2*n + 1)*factorial(-m + n)/factorial(m + n))*sin(m*ph) *
             assoc_legendre(n, m, cos(th))/(2*sqrt(pi))))
Ejemplo n.º 40
0
def test_gosper_sum():
    assert gosper_sum(1, (k, 0, n)) == 1 + n
    assert gosper_sum(k, (k, 0, n)) == n * (1 + n) / 2
    assert gosper_sum(k**2, (k, 0, n)) == n * (1 + n) * (1 + 2 * n) / 6
    assert gosper_sum(k**3, (k, 0, n)) == n**2 * (1 + n)**2 / 4

    assert gosper_sum(2**k, (k, 0, n)) == 2 * 2**n - 1

    assert gosper_sum(factorial(k), (k, 0, n)) is None
    assert gosper_sum(binomial(n, k), (k, 0, n)) is None

    assert gosper_sum(factorial(k) / k**2, (k, 0, n)) is None
    assert gosper_sum((k - 3) * factorial(k), (k, 0, n)) is None

    assert gosper_sum(k * factorial(k), k) == factorial(k)
    assert gosper_sum(k * factorial(k),
                      (k, 0, n)) == n * factorial(n) + factorial(n) - 1

    assert gosper_sum((-1)**k * binomial(n, k), (k, 0, n)) == 0
    assert gosper_sum((-1)**k * binomial(n, k),
                      (k, 0, m)) == -(-1)**m * (m - n) * binomial(n, m) / n

    assert gosper_sum((4*k + 1)*factorial(k)/factorial(2*k + 1), (k, 0, n)) == \
        (2*factorial(2*n + 1) - factorial(n))/factorial(2*n + 1)

    # issue sympy/sympy#6033:
    assert gosper_sum(
        n*(n + a + b)*a**n*b**n/(factorial(n + a)*factorial(n + b)),
        (n, 0, m)).rewrite(factorial) == \
        -a*b*(a**m*b**m*factorial(a) *
        factorial(b) - factorial(a + m)*factorial(b + m))/(factorial(a) *
        factorial(b)*factorial(a + m)*factorial(b + m))
Ejemplo n.º 41
0
def test_rsolve():
    eq = f(n + 2) - f(n + 1) - f(n)
    res = [{
        f:
        Lambda(n, 2**(-n) * (C0 * (1 + sqrt(5))**n + C1 * (-sqrt(5) + 1)**n))
    }]

    assert rsolve(eq) == res

    res = [{
        k: v.subs({
            C0: sqrt(5),
            C1: -sqrt(5)
        }).simplify()
        for k, v in r.items()
    } for r in res]

    assert rsolve(eq, init={f(0): 0, f(1): 5}) == res
    assert rsolve(f(n) - f(n - 1) - f(n - 2), init={f(0): 0, f(1): 5}) == res
    assert rsolve(Eq(f(n),
                     f(n - 1) + f(n - 2)), init={
                         f(0): 0,
                         f(1): 5
                     }) == res

    eq = (n - 1) * f(n + 2) - (n**2 + 3 * n -
                               2) * f(n + 1) + 2 * n * (n + 1) * f(n)
    res = [{f: Lambda(n, C1 * factorial(n) + C0 * 2**n)}]

    assert rsolve(eq) == res

    res = [{f: Lambda(n, -3 * factorial(n) + 3 * 2**n)}]

    assert rsolve(eq, init={f(0): 0, f(1): 3}) == res

    eq = f(n) - f(n - 1) - 2

    assert rsolve(eq, f(n)) == [{f: Lambda(n, C0 + 2 * n)}]
    assert rsolve(eq) == [{f: Lambda(n, C0 + 2 * n)}]
    assert rsolve(eq, init={f(0): 0}) == [{f: Lambda(n, 2 * n)}]
    assert rsolve(eq, init={f(0): 1}) == [{f: Lambda(n, 2 * n + 1)}]
    assert rsolve(eq, init={f(0): 0, f(1): 1}) is None

    eq = 3 * f(n - 1) - f(n) - 1

    assert rsolve(eq) == [{f: Lambda(n, 3**n * C0 + 1 / 2)}]
    assert rsolve(eq, init={f(0): 0}) == [{f: Lambda(n, -3**n / 2 + 1 / 2)}]
    assert rsolve(eq, init={f(0): 1}) == [{f: Lambda(n, 3**n / 2 + 1 / 2)}]
    assert rsolve(eq, init={f(0): 2}) == [{f: Lambda(n, 3 * 3**n / 2 + 1 / 2)}]

    assert rsolve(f(n) - 1 / n * f(n - 1), f(n)) == [{
        f:
        Lambda(n, C0 / factorial(n))
    }]
    assert rsolve(f(n) - 1 / n * f(n - 1) - 1, f(n)) is None

    eq = 2 * f(n - 1) + (1 - n) * f(n) / n

    assert rsolve(eq) == [{f: Lambda(n, 2**n * C0 * n)}]
    assert rsolve([eq]) == [{f: Lambda(n, 2**n * C0 * n)}]
    assert rsolve(eq, init={f(1): 1}) == [{f: Lambda(n, 2**(n - 1) * n)}]
    assert rsolve(eq, init={f(1): 2}, simplify=False) == [{
                                f:
                                Lambda(n, 2**(n - 1) * n * 2)
                            }]
    assert rsolve(eq, init={f(1): 2}) == [{f: Lambda(n, 2**n * n)}]
    assert rsolve(eq, init={f(1): 3}) == [{f: Lambda(n, 3 * 2**n * n / 2)}]

    eq = (n - 1) * (n - 2) * f(n + 2) - (n + 1) * (n + 2) * f(n)

    assert rsolve(eq) == [{
        f:
        Lambda(n,
               n * (n - 2) * (n - 1) * ((-1)**n * C1 + C0))
    }]
    assert rsolve(eq, init={
        f(3): 6,
        f(4): 24
    }) == [{
        f: Lambda((n),
                  n * (n - 1) * (n - 2))
    }]
    assert (rsolve(eq, init={
        f(3): 6,
        f(4): -24
    }) == [{
        f: Lambda(n, (-1)**(n + 1) * n * (n - 2) * (n - 1))
    }])

    assert rsolve(Eq(f(n + 1), a * f(n)), init={f(1): a}) == [{
                                                    f:
                                                    Lambda(n, a**n)
                                                }]

    assert (rsolve(f(n) - a * f(n - 2),
                   init={
                       f(1): sqrt(a) * (a + b),
                       f(2): a * (a - b)
                   }) == [{
                       f: Lambda(n,
                                 a**(n / 2) * (-(-1)**n * b + a))
                   }])

    eq = (-16 * n**2 + 32 * n - 12) * f(n - 1) + (4 * n**2 - 12 * n + 9) * f(n)

    assert (rsolve(eq, init={f(1): binomial(2 * n + 1, 3)}) == [{
                                 f:
                                 Lambda(
                                     n, 4**n * n * (2 * n - 1) *
                                     gamma(n + 3 / 2) / (3 * gamma(n - 1 / 2)))
                             }])

    assert (rsolve(f(n) + a * (f(n + 1) + f(n - 1)) / 2) == [{
        f:
        Lambda(
            n, a**-n * (C0 * (-a * sqrt(-1 + a**-2) - 1)**n + C1 *
                        (a * sqrt(-1 + a**-2) - 1)**n))
    }])
Ejemplo n.º 42
0
def test_gosper_term():
    assert gosper_term((4 * k + 1) * factorial(k) / factorial(2 * k + 1),
                       k) == (-k - Rational(1, 2)) / (k + Rational(1, 4))
Ejemplo n.º 43
0
def test_polygamma():
    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(n, oo) == polygamma(n, oo, evaluate=False)

    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(0, -Rational(3, 11)) == polygamma(0, -Rational(3, 11),
                                                       evaluate=False)

    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 trigamma(x) == polygamma(1, x)

    def t(m, n):
        x = Integer(m)/n
        r = polygamma(0, x)
        if r.has(polygamma):
            return False
        return abs(polygamma(0, x.evalf()).evalf(strict=False) - r.evalf()).evalf(strict=False) < 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)
    pytest.raises(ArgumentIndexError, lambda: polygamma(3, 7*x).fdiff(3))

    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)
    assert polygamma(x, y).rewrite(harmonic) == polygamma(x, y)

    # 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 isinstance(polygamma(-2, x), polygamma)

    # Test a bug
    assert polygamma(0, -x).expand(func=True) == polygamma(0, -x)

    assert polygamma(1, x).as_leading_term(x) == polygamma(1, x)
Ejemplo n.º 44
0
def test_sympyissue_14793():
    e = ((x + Rational(1, 2))*log(x) - x +
         log(2*pi)/2 - log(factorial(x)) + 1/(12*x))*x**3
    assert limit(e, x, oo) == Rational(1, 360)
Ejemplo n.º 45
0
def test_sympyissue_22836():
    assert O(2**x + factorial(x), (x, oo)) == O(factorial(x), (x, oo))
    assert O(2**x + factorial(x) + x**x, (x, oo)) == O((1 / x)**(-x), (x, oo))
    assert O(x + factorial(x), (x, oo)) == O(factorial(x), (x, oo))
Ejemplo n.º 46
0
def test_gamma_rewrite():
    assert gamma(n).rewrite(factorial) == factorial(n - 1)
Ejemplo n.º 47
0
def test_gosper_sum_AeqB_part1():
    f1a = n**4
    f1b = n**3 * 2**n
    f1c = 1 / (n**2 + sqrt(5) * n - 1)
    f1d = n**4 * 4**n / binomial(2 * n, n)
    f1e = factorial(
        3 * n) / (factorial(n) * factorial(n + 1) * factorial(n + 2) * 27**n)
    f1f = binomial(2 * n, n)**2 / ((n + 1) * 4**(2 * n))
    f1g = (4 * n - 1) * binomial(2 * n, n)**2 / ((2 * n - 1)**2 * 4**(2 * n))
    f1h = n * factorial(n - Rational(1, 2))**2 / factorial(n + 1)**2

    g1a = m * (m + 1) * (2 * m + 1) * (3 * m**2 + 3 * m - 1) / 30
    g1b = 26 + 2**(m + 1) * (m**3 - 3 * m**2 + 9 * m - 13)
    g1c = (m + 1) * (m * (m**2 - 7 * m + 3) * sqrt(5) -
                     (3 * m**3 - 7 * m**2 + 19 * m - 6)) / (
                         2 * m**3 * sqrt(5) + m**4 + 5 * m**2 - 1) / 6
    g1d = -Rational(2, 231) + 2 * 4**m * (m + 1) * (
        63 * m**4 + 112 * m**3 + 18 * m**2 - 22 * m + 3) / (693 *
                                                            binomial(2 * m, m))
    g1e = -Rational(
        9, 2) + (81 * m**2 + 261 * m + 200) * factorial(3 * m + 2) / (
            40 * 27**m * factorial(m) * factorial(m + 1) * factorial(m + 2))
    g1f = (2 * m + 1)**2 * binomial(2 * m, m)**2 / (4**(2 * m) * (m + 1))
    g1g = -binomial(2 * m, m)**2 / 4**(2 * m)
    g1h = 4 * pi - (2 * m + 1)**2 * (
        3 * m + 4) * factorial(m - Rational(1, 2))**2 / factorial(m + 1)**2

    g = gosper_sum(f1a, (n, 0, m))
    assert g is not None and simplify(g - g1a) == 0
    g = gosper_sum(f1b, (n, 0, m))
    assert g is not None and simplify(g - g1b) == 0
    g = gosper_sum(f1c, (n, 0, m))
    assert g is not None and simplify(g - g1c) == 0
    g = gosper_sum(f1d, (n, 0, m))
    assert g is not None and simplify(g - g1d) == 0
    g = gosper_sum(f1e, (n, 0, m))
    assert g is not None and simplify(g - g1e) == 0
    g = gosper_sum(f1f, (n, 0, m))
    assert g is not None and simplify(g - g1f) == 0
    g = gosper_sum(f1g, (n, 0, m))
    assert g is not None and simplify(g - g1g) == 0
    g = gosper_sum(f1h, (n, 0, m))
    # need to call rewrite(gamma) here because we have terms involving
    # factorial(1/2)
    assert g is not None and simplify(g - g1h).rewrite(gamma) == 0
Ejemplo n.º 48
0
def test_sympyissue_4171():
    assert summation(factorial(2*k + 1)/factorial(2*k), (k, 0, oo)) == oo
    assert summation(2*k + 1, (k, 0, oo)) == oo
Ejemplo n.º 49
0
def test_sympyissue_4170():
    assert summation(1 / factorial(k), (k, 0, oo)) == E
Ejemplo n.º 50
0
def test_gosper_sum_AeqB_part1():
    # Ex. 5.7.1
    f1a = n**4
    f1b = n**3 * 2**n
    f1c = 1 / (n**2 + sqrt(5) * n - 1)
    f1d = n**4 * 4**n / binomial(2 * n, n)
    f1e = factorial(
        3 * n) / (factorial(n) * factorial(n + 1) * factorial(n + 2) * 27**n)
    f1f = binomial(2 * n, n)**2 / ((n + 1) * 4**(2 * n))
    f1g = (4 * n - 1) * binomial(2 * n, n)**2 / ((2 * n - 1)**2 * 4**(2 * n))
    f1h = n * factorial(n - Rational(1, 2))**2 / factorial(n + 1)**2

    g1a = m * (m + 1) * (2 * m + 1) * (3 * m**2 + 3 * m - 1) / 30
    g1b = 26 + 2**(m + 1) * (m**3 - 3 * m**2 + 9 * m - 13)
    g1c = (m + 1) * (m * (m**2 - 7 * m + 3) * sqrt(5) -
                     (3 * m**3 - 7 * m**2 + 19 * m - 6)) / (
                         2 * m**3 * sqrt(5) + m**4 + 5 * m**2 - 1) / 6
    g1d = -Rational(2, 231) + 2 * 4**m * (m + 1) * (
        63 * m**4 + 112 * m**3 + 18 * m**2 - 22 * m + 3) / (693 *
                                                            binomial(2 * m, m))
    g1e = -Rational(
        9, 2) + (81 * m**2 + 261 * m + 200) * factorial(3 * m + 2) / (
            40 * 27**m * factorial(m) * factorial(m + 1) * factorial(m + 2))
    g1f = (2 * m + 1)**2 * binomial(2 * m, m)**2 / (4**(2 * m) * (m + 1))
    g1g = -binomial(2 * m, m)**2 / 4**(2 * m)
    g1h = (
        4 * pi - (2 * m + 1)**2 *
        (3 * m + 4) * factorial(m - Rational(1, 2))**2 / factorial(m + 1)**2)

    assert gosper_sum(f1a, (n, 0, m)).equals(g1a)
    assert gosper_sum(f1b, (n, 0, m)).equals(g1b)
    assert gosper_sum(f1c, (n, 0, m)).equals(g1c)
    assert gosper_sum(f1d, (n, 0, m)).equals(g1d)
    assert gosper_sum(f1e, (n, 0, m)).equals(g1e)
    assert gosper_sum(f1f, (n, 0, m)).equals(g1f)
    assert gosper_sum(f1g, (n, 0, m)).equals(g1g)
    assert gosper_sum(f1h, (n, 0, m)).equals(g1h)
Ejemplo n.º 51
0
def test_sympyissue_4171():
    assert summation(factorial(2 * k + 1) / factorial(2 * k), (k, 0, oo)) == oo
    assert summation(2 * k + 1, (k, 0, oo)) == oo
Ejemplo n.º 52
0
def test_sympyissue_15943():
    s = Sum(binomial(n, k) * factorial(n - k), (k, 0, n))
    assert s.doit().simplify() == E * (gamma(n + 1) - lowergamma(n + 1, 1))
Ejemplo n.º 53
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)
Ejemplo n.º 54
0
 def pdf(self, k):
     return self.lamda**k / factorial(k) * exp(-self.lamda)
Ejemplo n.º 55
0
def test_factorial_simplify():
    # There are more tests in test_factorials.py. These are just to
    # ensure that simplify() calls factorial_simplify correctly
    assert simplify(factorial(x) / x) == factorial(x - 1)
    assert simplify(factorial(factorial(x))) == factorial(factorial(x))
Ejemplo n.º 56
0
def test_inverse_mellin_transform():
    from diofant import (sin, simplify, Max, Min, expand, powsimp, exp_polar,
                         cos, cot)
    IMT = inverse_mellin_transform

    assert IMT(gamma(s), s, x, (0, oo)) == exp(-x)
    assert IMT(gamma(-s), s, x, (-oo, 0)) == exp(-1 / x)
    assert simplify(IMT(s/(2*s**2 - 2), s, x, (2, oo))) == \
        (x**2 + 1)*Heaviside(1 - x)/(4*x)

    # test passing "None"
    assert IMT(1/(s**2 - 1), s, x, (-1, None)) == \
        -x*Heaviside(-x + 1)/2 - Heaviside(x - 1)/(2*x)
    assert IMT(1/(s**2 - 1), s, x, (None, 1)) == \
        (-x/2 + 1/(2*x))*Heaviside(-x + 1)

    # test expansion of sums
    assert IMT(gamma(s) + gamma(s - 1), s, x, (1, oo)) == (x + 1) * exp(-x) / x

    # test factorisation of polys
    r = symbols('r', extended_real=True)
    assert IMT(1/(s**2 + 1), s, exp(-x), (None, oo)
              ).subs(x, r).rewrite(sin).simplify() \
        == sin(r)*Heaviside(1 - exp(-r))

    # test multiplicative substitution
    _a, _b = symbols('a b', positive=True)
    assert IMT(_b**(-s / _a) * factorial(s / _a) / s, s, x,
               (0, oo)) == exp(-_b * x**_a)
    assert IMT(factorial(_a / _b + s / _b) / (_a + s), s, x,
               (-_a, oo)) == x**_a * exp(-x**_b)

    def simp_pows(expr):
        return simplify(powsimp(expand_mul(expr, deep=False),
                                force=True)).replace(exp_polar, exp)

    # Now test the inverses of all direct transforms tested above

    # Section 8.4.2
    nu = symbols('nu', extended_real=True, finite=True)
    assert IMT(-1 / (nu + s), s, x, (-oo, None)) == x**nu * Heaviside(x - 1)
    assert IMT(1 / (nu + s), s, x, (None, oo)) == x**nu * Heaviside(1 - x)
    assert simp_pows(IMT(gamma(beta)*gamma(s)/gamma(s + beta), s, x, (0, oo))) \
        == (1 - x)**(beta - 1)*Heaviside(1 - x)
    assert simp_pows(IMT(gamma(beta)*gamma(1 - beta - s)/gamma(1 - s),
                         s, x, (-oo, None))) \
        == (x - 1)**(beta - 1)*Heaviside(x - 1)
    assert simp_pows(IMT(gamma(s)*gamma(rho - s)/gamma(rho), s, x, (0, None))) \
        == (1/(x + 1))**rho
    assert simp_pows(IMT(d**c*d**(s - 1)*sin(pi*c)
                         * gamma(s)*gamma(s + c)*gamma(1 - s)*gamma(1 - s - c)/pi,
                         s, x, (Max(-re(c), 0), Min(1 - re(c), 1)))) \
        == (x**c - d**c)/(x - d)

    assert simplify(IMT(1/sqrt(pi)*(-c/2)*gamma(s)*gamma((1 - c)/2 - s)
                        * gamma(-c/2 - s)/gamma(1 - c - s),
                        s, x, (0, -re(c)/2))) == \
        (1 + sqrt(x + 1))**c
    assert simplify(IMT(2**(a + 2*s)*b**(a + 2*s - 1)*gamma(s)*gamma(1 - a - 2*s)
                        / gamma(1 - a - s), s, x, (0, (-re(a) + 1)/2))) == \
        b**(a - 1)*(sqrt(1 + x/b**2) + 1)**(a - 1)*(b**2*sqrt(1 + x/b**2) +
        b**2 + x)/(b**2 + x)
    assert simplify(IMT(-2**(c + 2*s)*c*b**(c + 2*s)*gamma(s)*gamma(-c - 2*s)
                        / gamma(-c - s + 1), s, x, (0, -re(c)/2))) == \
        b**c*(sqrt(1 + x/b**2) + 1)**c

    # Section 8.4.5
    assert IMT(24 / s**5, s, x, (0, oo)) == log(x)**4 * Heaviside(1 - x)
    assert expand(IMT(6/s**4, s, x, (-oo, 0)), force=True) == \
        log(x)**3*Heaviside(x - 1)
    assert IMT(pi / (s * sin(pi * s)), s, x, (-1, 0)) == log(x + 1)
    assert IMT(pi / (s * sin(pi * s / 2)), s, x, (-2, 0)) == log(x**2 + 1)
    assert IMT(pi / (s * sin(2 * pi * s)), s, x,
               (-Rational(1, 2), 0)) == log(sqrt(x) + 1)
    assert IMT(pi / (s * sin(pi * s)), s, x, (0, 1)) == log(1 + 1 / x)

    # TODO
    def mysimp(expr):
        from diofant import expand, logcombine, powsimp
        return expand(powsimp(logcombine(expr, force=True),
                              force=True,
                              deep=True),
                      force=True).replace(exp_polar, exp)

    assert mysimp(mysimp(IMT(pi / (s * tan(pi * s)), s, x, (-1, 0)))) in [
        log(1 - x) * Heaviside(1 - x) + log(x - 1) * Heaviside(x - 1),
        log(x) * Heaviside(x - 1) + log(1 - 1 / x) * Heaviside(x - 1) +
        log(-x + 1) * Heaviside(-x + 1)
    ]
    # test passing cot
    assert mysimp(IMT(pi * cot(pi * s) / s, s, x, (0, 1))) in [
        log(1 / x - 1) * Heaviside(1 - x) + log(1 - 1 / x) * Heaviside(x - 1),
        -log(x) * Heaviside(-x + 1) + log(1 - 1 / x) * Heaviside(x - 1) +
        log(-x + 1) * Heaviside(-x + 1),
    ]

    # 8.4.14
    assert IMT(-gamma(s + Rational(1, 2))/(sqrt(pi)*s), s, x, (-Rational(1, 2), 0)) == \
        erf(sqrt(x))

    # 8.4.19
    assert simplify(IMT(gamma(a/2 + s)/gamma(a/2 - s + 1), s, x, (-re(a)/2, Rational(3, 4)))) \
        == besselj(a, 2*sqrt(x))
    assert simplify(IMT(2**a*gamma(Rational(1, 2) - 2*s)*gamma(s + (a + 1)/2)
                      / (gamma(1 - s - a/2)*gamma(1 - 2*s + a)),
                      s, x, (-(re(a) + 1)/2, Rational(1, 4)))) == \
        sin(sqrt(x))*besselj(a, sqrt(x))
    assert simplify(IMT(2**a*gamma(a/2 + s)*gamma(Rational(1, 2) - 2*s)
                      / (gamma(Rational(1, 2) - s - a/2)*gamma(1 - 2*s + a)),
                      s, x, (-re(a)/2, Rational(1, 4)))) == \
        cos(sqrt(x))*besselj(a, sqrt(x))
    # TODO this comes out as an amazing mess, but simplifies nicely
    assert simplify(IMT(gamma(a + s)*gamma(Rational(1, 2) - s)
                      / (sqrt(pi)*gamma(1 - s)*gamma(1 + a - s)),
                      s, x, (-re(a), Rational(1, 2)))) == \
        besselj(a, sqrt(x))**2
    assert simplify(IMT(gamma(s)*gamma(Rational(1, 2) - s)
                      / (sqrt(pi)*gamma(1 - s - a)*gamma(1 + a - s)),
                      s, x, (0, Rational(1, 2)))) == \
        besselj(-a, sqrt(x))*besselj(a, sqrt(x))
    assert simplify(IMT(4**s*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)),
                      s, x, (-(re(a) + re(b))/2, Rational(1, 2)))) == \
        besselj(a, sqrt(x))*besselj(b, sqrt(x))

    # Section 8.4.20
    # TODO this can be further simplified!
    assert simplify(IMT(-2**(2*s)*cos(pi*a/2 - pi*b/2 + pi*s)*gamma(-2*s + 1) *
                    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)),
                    s, x,
                    (Max(-re(a)/2 - re(b)/2, -re(a)/2 + re(b)/2), Rational(1, 2)))) == \
                    besselj(a, sqrt(x))*-(besselj(-b, sqrt(x)) -
                    besselj(b, sqrt(x))*cos(pi*b))/sin(pi*b)
    # TODO more

    # for coverage

    assert IMT(pi / cos(pi * s), s, x,
               (0, Rational(1, 2))) == sqrt(x) / (x + 1)
Ejemplo n.º 57
0
def test_gosper_sum():
    assert gosper_sum(1, (k, 0, n)) == 1 + n
    assert gosper_sum(k, (k, 0, n)) == n * (1 + n) / 2
    assert gosper_sum(k**2, (k, 0, n)) == n * (1 + n) * (1 + 2 * n) / 6
    assert gosper_sum(k**3, (k, 0, n)) == n**2 * (1 + n)**2 / 4

    assert gosper_sum(2**k, (k, 0, n)) == 2 * 2**n - 1

    assert gosper_sum(factorial(k), (k, 0, n)) is None
    assert gosper_sum(binomial(n, k), (k, 0, n)) is None

    assert gosper_sum(factorial(k) / k**2, (k, 0, n)) is None
    assert gosper_sum((k - 3) * factorial(k), (k, 0, n)) is None

    f = k * factorial(k)
    assert gosper_sum(f, k) == factorial(k)
    assert gosper_sum(f, (k, 0, n)) == n * factorial(n) + factorial(n) - 1

    f = (-1)**k * binomial(n, k)
    assert gosper_sum(f, (k, 0, n)) == 0
    assert gosper_sum(f, (k, 0, m)) == -(-1)**m * (m - n) * binomial(n, m) / n

    f = (4 * k + 1) * factorial(k) / factorial(2 * k + 1)
    assert gosper_sum(f, (k, 0, n)) == (2 * factorial(2 * n + 1) -
                                        factorial(n)) / factorial(2 * n + 1)
    assert gosper_sum(
        f, (k, 3, n)) == (-60 * factorial(n) +
                          factorial(2 * n + 1)) / (60 * factorial(2 * n + 1))
Ejemplo n.º 58
0
def test_rsolve():
    f = y(n + 2) - y(n + 1) - y(n)
    h = sqrt(5)*(Rational(1, 2) + sqrt(5)/2)**n \
        - sqrt(5)*(Rational(1, 2) - sqrt(5)/2)**n

    assert rsolve(f, y(n)) in [
        C0 * (Rational(1, 2) - sqrt(5) / 2)**n + C1 *
        (Rational(1, 2) + sqrt(5) / 2)**n,
        C1 * (Rational(1, 2) - sqrt(5) / 2)**n + C0 *
        (Rational(1, 2) + sqrt(5) / 2)**n,
    ]

    assert rsolve(f, y(n), [0, 5]) == h
    assert rsolve(f, y(n), {0: 0, 1: 5}) == h
    assert rsolve(f, y(n), {y(0): 0, y(1): 5}) == h
    assert rsolve(y(n) - y(n - 1) - y(n - 2), y(n), [0, 5]) == h
    assert rsolve(Eq(y(n), y(n - 1) + y(n - 2)), y(n), [0, 5]) == h

    assert f.subs(y, Lambda(k, rsolve(f, y(n)).subs(n, k))).simplify() == 0

    f = (n - 1) * y(n + 2) - (n**2 + 3 * n - 2) * y(n + 1) + 2 * n * (n +
                                                                      1) * y(n)
    g = C1 * factorial(n) + C0 * 2**n
    h = -3 * factorial(n) + 3 * 2**n

    assert rsolve(f, y(n)) == g
    assert rsolve(f, y(n), []) == g
    assert rsolve(f, y(n), {}) == g

    assert rsolve(f, y(n), [0, 3]) == h
    assert rsolve(f, y(n), {0: 0, 1: 3}) == h
    assert rsolve(f, y(n), {y(0): 0, y(1): 3}) == h

    assert f.subs(y, Lambda(k, rsolve(f, y(n)).subs(n, k))).simplify() == 0

    f = y(n) - y(n - 1) - 2

    assert rsolve(f, y(n), {y(0): 0}) == 2 * n
    assert rsolve(f, y(n), {y(0): 1}) == 2 * n + 1
    assert rsolve(f, y(n), {y(0): 0, y(1): 1}) is None

    assert f.subs(y, Lambda(k, rsolve(f, y(n)).subs(n, k))).simplify() == 0

    f = 3 * y(n - 1) - y(n) - 1

    assert rsolve(f, y(n), {y(0): 0}) == -3**n / 2 + Rational(1, 2)
    assert rsolve(f, y(n), {y(0): 1}) == 3**n / 2 + Rational(1, 2)
    assert rsolve(f, y(n), {y(0): 2}) == 3 * 3**n / 2 + Rational(1, 2)

    assert f.subs(y, Lambda(k, rsolve(f, y(n)).subs(n, k))).simplify() == 0

    f = y(n) - 1 / n * y(n - 1)
    assert rsolve(f, y(n)) == C0 / factorial(n)
    assert f.subs(y, Lambda(k, rsolve(f, y(n)).subs(n, k))).simplify() == 0

    f = y(n) - 1 / n * y(n - 1) - 1
    assert rsolve(f, y(n)) is None

    f = 2 * y(n - 1) + (1 - n) * y(n) / n

    assert rsolve(f, y(n), {y(1): 1}) == 2**(n - 1) * n
    assert rsolve(f, y(n), {y(1): 2}) == 2**(n - 1) * n * 2
    assert rsolve(f, y(n), {y(1): 3}) == 2**(n - 1) * n * 3

    assert f.subs(y, Lambda(k, rsolve(f, y(n)).subs(n, k))).simplify() == 0

    f = (n - 1) * (n - 2) * y(n + 2) - (n + 1) * (n + 2) * y(n)

    assert rsolve(f, y(n), {y(3): 6, y(4): 24}) == n * (n - 1) * (n - 2)
    assert rsolve(f, y(n), {
        y(3): 6,
        y(4): -24
    }) == -n * (n - 1) * (n - 2) * (-1)**(n)

    assert f.subs(y, Lambda(k, rsolve(f, y(n)).subs(n, k))).simplify() == 0

    assert rsolve(Eq(y(n + 1), a * y(n)), y(n), {y(1): a}).simplify() == a**n

    assert rsolve(y(n) - a*y(n-2), y(n),
                  {y(1): sqrt(a)*(a + b), y(2): a*(a - b)}).simplify() == \
        a**(n/2)*(-(-1)**n*b + a)

    f = (-16 * n**2 + 32 * n - 12) * y(n - 1) + (4 * n**2 - 12 * n + 9) * y(n)

    assert expand_func(rsolve(f, y(n),
                              {y(1): binomial(2*n + 1, 3)}).rewrite(gamma)).simplify() == \
        2**(2*n)*n*(2*n - 1)*(4*n**2 - 1)/12

    assert (rsolve(y(n) + a * (y(n + 1) + y(n - 1)) / 2, y(n)) -
            (C0 * (-sqrt(-1 + a**(-2)) - 1 / a)**n + C1 *
             (sqrt(-1 + a**(-2)) - 1 / a)**n)).simplify() == 0
Ejemplo n.º 59
0
def test_gosper_sum_AeqB_part2():
    # Ex. 5.7.2
    f2a = n**2 * a**n
    f2b = (n - r / 2) * binomial(r, n)
    f2c = factorial(n - 1)**2 / (factorial(n - x) * factorial(n + x))
    f2d = n * (n + a + b) * a**n * b**n / (factorial(n + a) * factorial(n + b))

    g2a = -a * (a + 1) / (a - 1)**3 + a**(m + 1) * (
        a**2 * m**2 - 2 * a * m**2 + m**2 - 2 * a * m + 2 * m + a + 1) / (a -
                                                                          1)**3
    g2b = (m - r) * binomial(r, m) / 2
    g2c = (factorial(m)**2 / (x**2 * factorial(-x + m) * factorial(x + m)) -
           sin(pi * x) / (pi * x**3))
    g2d = (1 / (factorial(a - 1) * factorial(b - 1)) -
           a**(m + 1) * b**(m + 1) / (factorial(a + m) * factorial(b + m)))

    assert gosper_sum(f2a, (n, 0, m)).equals(g2a)
    assert gosper_sum(f2b, (n, 0, m)).equals(g2b)
    assert gosper_sum(f2c, (n, 1, m)).equals(g2c)
    assert gosper_sum(f2d, (n, 0, m)).equals(g2d)