Example #1
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
Example #2
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
Example #3
0
def test_hyper():
    assert rsolve((n**2 - 2) * f(n) - (2 * n + 1) * f(n + 1) + f(n + 2)) == [{
        f:
        Lambda(n,
               C0 * rf(-sqrt(2), n) + C1 * rf(+sqrt(2), n))
    }]

    assert rsolve((n**2 - k) * f(n) - (2 * n + 1) * f(n + 1) + f(n + 2)) == [{
        f:
        Lambda(n,
               C1 * rf(sqrt(k), n) + C0 * rf(-sqrt(k), n))
    }]

    assert rsolve(2 * n * (n + 1) * f(n) - (n**2 + 3 * n - 2) * f(n + 1) +
                  (n - 1) * f(n + 2)) == [{
                      f:
                      Lambda(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(-n - f(n) + f(n + 1)) == [{
        f: Lambda(n, C0 + n * (n - 1) / 2)
    }]
    assert rsolve(-1 - n - f(n) + f(n + 1)) == [{
        f:
        Lambda(n, C0 + n * (n + 1) / 2)
    }]
    assert rsolve(-3 * (n + n**2) - f(n) + f(n + 1)) == [{
        f:
        Lambda(n, C0 + n**3 - n)
    }]

    assert rsolve(-n - factorial(n) - f(n) + f(n + 1)) is None

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

    assert (rsolve(f(n) + f(n + 1) + f(n + 2)) == [{
        f:
        Lambda(
            n,
            2**-n * (C0 * (-1 - sqrt(3) * I)**n + C1 * (-1 + sqrt(3) * I)**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
Example #4
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)
Example #5
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 8301
    assert product(2, (n, 1, oo)) == oo
    assert product(-1, (n, 1, oo)).func is Product
Example #6
0
def test_sympyissue_9699():
    n, k = symbols('n k', real=True)
    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)
Example #7
0
def test__eval_product():
    # issue sympy/sympy#4809
    a = Function('a')
    assert product(2*a(i), (i, 1, n)) == 2**n * Product(a(i), (i, 1, n))
    # issue sympy/sympy#4810
    assert product(2**i, (i, 1, n)) == 2**(n/2 + n**2/2)

    assert (product((i - 1)*(i**6 + i - 1), (i, n, m)) ==
            rf(n - 1, m - n + 1)*product(i**6 + i - 1, (i, n, m)))

    assert (product(log(i)**2*cos(i)**3, (i, n, m)) ==
            Product(log(i)**2*cos(i)**3, (i, n, m)))
Example #8
0
def test__eval_product():
    # issue sympy/sympy#4809
    a = Function('a')
    assert product(2 * a(i), (i, 1, n)) == 2**n * Product(a(i), (i, 1, n))
    # issue sympy/sympy#4810
    assert product(2**i, (i, 1, n)) == 2**(n / 2 + n**2 / 2)

    assert (product(
        (i - 1) * (i**6 + i - 1),
        (i, n, m)) == rf(n - 1, m - n + 1) * product(i**6 + i - 1, (i, n, m)))

    assert (product(log(i)**2 * cos(i)**3,
                    (i, n, m)) == Product(log(i)**2 * cos(i)**3, (i, n, m)))
Example #9
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
Example #10
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
def test_sympyissue_14822():
    assert rf(Rational(2, 3), 32).evalf() == Float('+6.0994868747569084e+34', dps=15)
    assert ff(Rational(2, 3), 32).evalf() == Float('-2.066175896913914e+32', dps=15)
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))
Example #13
0
def test_Function():
    assert mathematica_code(f(x, y, z)) == 'f[x, y, z]'
    assert mathematica_code(sin(x)**cos(x)) == 'Sin[x]^Cos[x]'
    assert mathematica_code(sign(x)) == 'Sign[x]'

    assert mathematica_code(atanh(x),
                            user_functions={'atanh':
                                            'ArcTanh'}) == 'ArcTanh[x]'

    assert (mathematica_code(meijerg(
        ((1, 1), (3, 4)), ((1, ), ()),
        x)) == 'MeijerG[{{1, 1}, {3, 4}}, {{1}, {}}, x]')
    assert (mathematica_code(hyper(
        (1, 2, 3), (3, 4), x)) == 'HypergeometricPFQ[{1, 2, 3}, {3, 4}, x]')

    assert mathematica_code(Min(x, y)) == 'Min[x, y]'
    assert mathematica_code(Max(x, y)) == 'Max[x, y]'
    assert mathematica_code(Max(x,
                                2)) == 'Max[2, x]'  # issue sympy/sympy#15344

    assert mathematica_code(binomial(x, y)) == 'Binomial[x, y]'

    assert mathematica_code(log(x)) == 'Log[x]'
    assert mathematica_code(tan(x)) == 'Tan[x]'
    assert mathematica_code(cot(x)) == 'Cot[x]'
    assert mathematica_code(asin(x)) == 'ArcSin[x]'
    assert mathematica_code(acos(x)) == 'ArcCos[x]'
    assert mathematica_code(atan(x)) == 'ArcTan[x]'
    assert mathematica_code(acot(x)) == 'ArcCot[x]'
    assert mathematica_code(sinh(x)) == 'Sinh[x]'
    assert mathematica_code(cosh(x)) == 'Cosh[x]'
    assert mathematica_code(tanh(x)) == 'Tanh[x]'
    assert mathematica_code(coth(x)) == 'Coth[x]'
    assert mathematica_code(asinh(x)) == 'ArcSinh[x]'
    assert mathematica_code(acosh(x)) == 'ArcCosh[x]'
    assert mathematica_code(atanh(x)) == 'ArcTanh[x]'
    assert mathematica_code(acoth(x)) == 'ArcCoth[x]'
    assert mathematica_code(sech(x)) == 'Sech[x]'
    assert mathematica_code(csch(x)) == 'Csch[x]'
    assert mathematica_code(erf(x)) == 'Erf[x]'
    assert mathematica_code(erfi(x)) == 'Erfi[x]'
    assert mathematica_code(erfc(x)) == 'Erfc[x]'
    assert mathematica_code(conjugate(x)) == 'Conjugate[x]'
    assert mathematica_code(re(x)) == 'Re[x]'
    assert mathematica_code(im(x)) == 'Im[x]'
    assert mathematica_code(polygamma(x, y)) == 'PolyGamma[x, y]'
    assert mathematica_code(factorial(x)) == 'Factorial[x]'
    assert mathematica_code(factorial2(x)) == 'Factorial2[x]'
    assert mathematica_code(rf(x, y)) == 'Pochhammer[x, y]'
    assert mathematica_code(gamma(x)) == 'Gamma[x]'
    assert mathematica_code(zeta(x)) == 'Zeta[x]'
    assert mathematica_code(Heaviside(x)) == 'UnitStep[x]'
    assert mathematica_code(fibonacci(x)) == 'Fibonacci[x]'
    assert mathematica_code(polylog(x, y)) == 'PolyLog[x, y]'
    assert mathematica_code(loggamma(x)) == 'LogGamma[x]'
    assert mathematica_code(uppergamma(x, y)) == 'Gamma[x, y]'

    class MyFunc1(Function):
        @classmethod
        def eval(cls, x):
            pass

    class MyFunc2(Function):
        @classmethod
        def eval(cls, x, y):
            pass

    pytest.raises(
        ValueError,
        lambda: mathematica_code(MyFunc1(x),
                                 user_functions={'MyFunc1': ['Myfunc1']}))
    assert mathematica_code(MyFunc1(x),
                            user_functions={'MyFunc1':
                                            'Myfunc1'}) == 'Myfunc1[x]'
    assert mathematica_code(
        MyFunc2(x, y),
        user_functions={'MyFunc2':
                        [(lambda *x: False, 'Myfunc2')]}) == 'MyFunc2[x, y]'
Example #14
0
def test_sympyissue_14822():
    assert rf(Rational(2, 3), 32).evalf() == Float('+6.0994868747569084e+34',
                                                   dps=15)
    assert ff(Rational(2, 3), 32).evalf() == Float('-2.066175896913914e+32',
                                                   dps=15)
Example #15
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))
Example #16
0
def test_rational_products():
    assert product(1 + 1/k, (k, 1, n)) == rf(2, n)/factorial(n)
Example #17
0
def test_rational_products():
    assert product(1 + 1 / k, (k, 1, n)) == rf(2, n) / factorial(n)
Example #18
0
def test_sympyissue_9699():
    n, k = symbols('n k', real=True)
    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)