コード例 #1
0
ファイル: test_gruntz.py プロジェクト: goretkin/diofant
def test_gruntz_eval_special():
    # Gruntz, p. 126
    assert gruntz(
        exp(x) * (sin(1 / x + exp(-x)) - sin(1 / x + exp(-x**2))), x) == 1
    assert gruntz((erf(x - exp(-exp(x))) - erf(x)) * exp(exp(x)) * exp(x**2),
                  x) == -2 / sqrt(pi)
    assert gruntz(
        exp(exp(x)) * (exp(sin(1 / x + exp(-exp(x)))) - exp(sin(1 / x))),
        x) == 1
    assert gruntz(exp(x) * (gamma(x + exp(-x)) - gamma(x)), x) == oo
    assert gruntz(exp(exp(digamma(digamma(x)))) / x, x) == exp(-Rational(1, 2))
    assert gruntz(exp(exp(digamma(log(x)))) / x, x) == exp(-Rational(1, 2))
    assert gruntz(digamma(digamma(digamma(x))), x) == oo
    assert gruntz(loggamma(loggamma(x)), x) == oo
    assert gruntz(
        ((gamma(x + 1 / gamma(x)) - gamma(x)) / log(x) - cos(1 / x)) * x *
        log(x), x) == -Rational(1, 2)
    assert gruntz(x * (gamma(x - 1/gamma(x)) - gamma(x) + log(x)), x) \
        == Rational(1, 2)
    assert gruntz((gamma(x + 1 / gamma(x)) - gamma(x)) / log(x), x) == 1
    assert gruntz(
        gamma(x + 1) / sqrt(2 * pi) - exp(-x) *
        (x**(x + Rational(1, 2)) + x**(x - Rational(1, 2)) / 12), x) == oo
    assert gruntz(exp(exp(exp(digamma(digamma(digamma(x)))))) / x, x) == 0
    assert gruntz(exp(gamma(x - exp(-x)) * exp(1 / x)) - exp(gamma(x)),
                  x) == oo
    assert gruntz((Ei(x - exp(-exp(x))) - Ei(x)) * exp(-x) * exp(exp(x)) * x,
                  x) == -1
    assert gruntz(exp((log(2) + 1) * x) * (zeta(x + exp(-x)) - zeta(x)),
                  x) == -log(2)
コード例 #2
0
ファイル: test_gruntz.py プロジェクト: diofant/diofant
def test_intractable():
    assert limit(1/gamma(x), x, oo) == 0
    assert limit(1/loggamma(x), x, oo) == 0
    assert limit(gamma(x)/loggamma(x), x, oo) == oo
    assert limit(exp(gamma(x))/gamma(x), x, oo) == oo
    assert limit(gamma(3 + 1/x), x, oo) == 2
    assert limit(gamma(Rational(1, 7) + 1/x), x, oo) == gamma(Rational(1, 7))
    assert limit(log(x**x)/log(gamma(x)), x, oo) == 1
    assert limit(log(gamma(gamma(x)))/exp(x), x, oo) == oo
    assert limit(acosh(1 + 1/x)*sqrt(x), x, oo) == sqrt(2)

    # issue sympy/sympy#10804
    assert limit(2*airyai(x)*root(x, 4) *
                 exp(2*x**Rational(3, 2)/3), x, oo) == 1/sqrt(pi)
    assert limit(airybi(x)*root(x, 4) *
                 exp(-2*x**Rational(3, 2)/3), x, oo) == 1/sqrt(pi)
    assert limit(airyai(1/x), x, oo) == (3**Rational(5, 6) *
                                         gamma(Rational(1, 3))/(6*pi))
    assert limit(airybi(1/x), x, oo) == cbrt(3)*gamma(Rational(1, 3))/(2*pi)
    assert limit(airyai(2 + 1/x), x, oo) == airyai(2)
    assert limit(airybi(2 + 1/x), x, oo) == airybi(2)

    # issue sympy/sympy#10976
    assert limit(erf(m/x)/erf(1/x), x, oo) == m

    assert limit(Max(x**2, x, exp(x))/x, x, oo) == oo
コード例 #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 factorial(n).rewrite('tractable') == exp(loggamma(n + 1))
コード例 #4
0
def test_specfun():
    for f in [besselj, bessely, besseli, besselk]:
        assert octave_code(f(n, x)) == f.__name__ + '(n, x)'
    assert octave_code(hankel1(n, x)) == 'besselh(n, 1, x)'
    assert octave_code(hankel2(n, x)) == 'besselh(n, 2, x)'
    assert octave_code(airyai(x)) == 'airy(0, x)'
    assert octave_code(airyaiprime(x)) == 'airy(1, x)'
    assert octave_code(airybi(x)) == 'airy(2, x)'
    assert octave_code(airybiprime(x)) == 'airy(3, x)'
    assert octave_code(uppergamma(n, x)) == "gammainc(x, n, 'upper')"
    assert octave_code(lowergamma(n, x)) == "gammainc(x, n, 'lower')"
    assert octave_code(jn(
        n, x)) == 'sqrt(2)*sqrt(pi)*sqrt(1./x).*besselj(n + 1/2, x)/2'
    assert octave_code(yn(
        n, x)) == 'sqrt(2)*sqrt(pi)*sqrt(1./x).*bessely(n + 1/2, x)/2'
    assert octave_code(Chi(x)) == 'coshint(x)'
    assert octave_code(Ci(x)) == 'cosint(x)'
    assert octave_code(laguerre(n, x)) == 'laguerreL(n, x)'
    assert octave_code(li(x)) == 'logint(x)'
    assert octave_code(loggamma(x)) == 'gammaln(x)'
    assert octave_code(polygamma(n, x)) == 'psi(n, x)'
    assert octave_code(Shi(x)) == 'sinhint(x)'
    assert octave_code(Si(x)) == 'sinint(x)'
    assert octave_code(LambertW(x)) == 'lambertw(x)'
    assert octave_code(LambertW(x, n)) == 'lambertw(n, x)'
    assert octave_code(zeta(x)) == 'zeta(x)'
    assert octave_code(zeta(
        x, y)) == '% Not supported in Octave:\n% zeta\nzeta(x, y)'
コード例 #5
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))
コード例 #6
0
ファイル: test_gruntz.py プロジェクト: skirpichev/diofant
def test_gruntz_eval_special():
    # Gruntz, p. 126
    assert gruntz(exp(x)*(sin(1/x + exp(-x)) - sin(1/x + exp(-x**2))), x) == 1
    assert gruntz((erf(x - exp(-exp(x))) - erf(x)) * exp(exp(x)) * exp(x**2),
                  x) == -2/sqrt(pi)
    assert gruntz(exp(exp(x)) * (exp(sin(1/x + exp(-exp(x)))) - exp(sin(1/x))),
                  x) == 1
    assert gruntz(exp(x)*(gamma(x + exp(-x)) - gamma(x)), x) == oo
    assert gruntz(exp(exp(digamma(digamma(x))))/x, x) == exp(-Rational(1, 2))
    assert gruntz(exp(exp(digamma(log(x))))/x, x) == exp(-Rational(1, 2))
    assert gruntz(digamma(digamma(digamma(x))), x) == oo
    assert gruntz(loggamma(loggamma(x)), x) == oo
    assert gruntz(((gamma(x + 1/gamma(x)) - gamma(x))/log(x) - cos(1/x))
                  * x*log(x), x) == -Rational(1, 2)
    assert gruntz(x * (gamma(x - 1/gamma(x)) - gamma(x) + log(x)), x) \
        == Rational(1, 2)
    assert gruntz((gamma(x + 1/gamma(x)) - gamma(x)) / log(x), x) == 1
コード例 #7
0
def test_gruntz_eval_special():
    # Gruntz, p. 126
    assert gruntz(exp(x)*(sin(1/x + exp(-x)) - sin(1/x + exp(-x**2))), x) == 1
    assert gruntz((erf(x - exp(-exp(x))) - erf(x)) * exp(exp(x)) * exp(x**2),
                  x) == -2/sqrt(pi)
    assert gruntz(exp(exp(x)) * (exp(sin(1/x + exp(-exp(x)))) - exp(sin(1/x))),
                  x) == 1
    assert gruntz(exp(x)*(gamma(x + exp(-x)) - gamma(x)), x) == oo
    assert gruntz(exp(exp(digamma(digamma(x))))/x, x) == exp(-Rational(1, 2))
    assert gruntz(exp(exp(digamma(log(x))))/x, x) == exp(-Rational(1, 2))
    assert gruntz(digamma(digamma(digamma(x))), x) == oo
    assert gruntz(loggamma(loggamma(x)), x) == oo
    assert gruntz(((gamma(x + 1/gamma(x)) - gamma(x))/log(x) - cos(1/x))
                  * x*log(x), x) == -Rational(1, 2)
    assert gruntz(x * (gamma(x - 1/gamma(x)) - gamma(x) + log(x)), x) \
        == Rational(1, 2)
    assert gruntz((gamma(x + 1/gamma(x)) - gamma(x)) / log(x), x) == 1
コード例 #8
0
ファイル: test_gruntz.py プロジェクト: skirpichev/diofant
def test_intractable():
    assert gruntz(1/gamma(x), x) == 0
    assert gruntz(1/loggamma(x), x) == 0
    assert gruntz(gamma(x)/loggamma(x), x) == oo
    assert gruntz(exp(gamma(x))/gamma(x), x) == oo
    assert gruntz(gamma(3 + 1/x), x) == 2
    assert gruntz(gamma(Rational(1, 7) + 1/x), x) == gamma(Rational(1, 7))
    assert gruntz(log(x**x)/log(gamma(x)), x) == 1
    assert gruntz(log(gamma(gamma(x)))/exp(x), x) == oo

    # issue sympy/sympy#10804
    assert gruntz(2*airyai(x)*root(x, 4) *
                  exp(2*x**Rational(3, 2)/3), x) == 1/sqrt(pi)
    assert gruntz(airybi(x)*root(x, 4) *
                  exp(-2*x**Rational(3, 2)/3), x) == 1/sqrt(pi)
    assert gruntz(airyai(1/x), x) == (3**Rational(5, 6) *
                                      gamma(Rational(1, 3))/(6*pi))
    assert gruntz(airybi(1/x), x) == cbrt(3)*gamma(Rational(1, 3))/(2*pi)
    assert gruntz(airyai(2 + 1/x), x) == airyai(2)
    assert gruntz(airybi(2 + 1/x), x) == airybi(2)
コード例 #9
0
ファイル: test_gruntz.py プロジェクト: Blendify/diofant
def test_intractable():
    assert gruntz(1/gamma(x), x) == 0
    assert gruntz(1/loggamma(x), x) == 0
    assert gruntz(gamma(x)/loggamma(x), x) == oo
    assert gruntz(exp(gamma(x))/gamma(x), x) == oo
    assert gruntz(gamma(3 + 1/x), x) == 2
    assert gruntz(gamma(Rational(1, 7) + 1/x), x) == gamma(Rational(1, 7))
    assert gruntz(log(x**x)/log(gamma(x)), x) == 1
    assert gruntz(log(gamma(gamma(x)))/exp(x), x) == oo

    # issue sympy/sympy#10804
    assert gruntz(2*airyai(x)*root(x, 4) *
                  exp(2*x**Rational(3, 2)/3), x) == 1/sqrt(pi)
    assert gruntz(airybi(x)*root(x, 4) *
                  exp(-2*x**Rational(3, 2)/3), x) == 1/sqrt(pi)
    assert gruntz(airyai(1/x), x) == (3**Rational(5, 6) *
                                      gamma(Rational(1, 3))/(6*pi))
    assert gruntz(airybi(1/x), x) == cbrt(3)*gamma(Rational(1, 3))/(2*pi)
    assert gruntz(airyai(2 + 1/x), x) == airyai(2)
    assert gruntz(airybi(2 + 1/x), x) == airybi(2)
コード例 #10
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))
コード例 #11
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))
コード例 #12
0
ファイル: test_function.py プロジェクト: skirpichev/diofant
def test_function__eval_nseries():
    n = Symbol('n')

    assert sin(x)._eval_nseries(x, 2, None) == x + O(x**3)
    assert sin(x + 1)._eval_nseries(x, 2, None) == x*cos(1) + sin(1) + O(x**2)
    assert sin(pi*(1 - x))._eval_nseries(x, 2, None) == pi*x + O(x**3)
    assert acos(1 - x**2)._eval_nseries(x, 2, None) == sqrt(2)*x + O(x**2)
    assert polygamma(n, x + 1)._eval_nseries(x, 2, None) == \
        polygamma(n, 1) + polygamma(n + 1, 1)*x + O(x**2)
    pytest.raises(PoleError, lambda: sin(1/x)._eval_nseries(x, 2, None))
    assert acos(1 - x)._eval_nseries(x, 4, None) == sqrt(2)*sqrt(x) + \
        sqrt(2)*x**Rational(3, 2)/12 + O(x**2)
    assert acos(1 + x)._eval_nseries(x, 4, None) == sqrt(2)*I*sqrt(x) - \
        sqrt(2)*I*x**(3/2)/12 + O(x**2)  # XXX: wrong, branch cuts
    assert loggamma(1/x)._eval_nseries(x, 0, None) == \
        log(x)/2 - log(x)/x - 1/x + O(1, x)
    assert loggamma(log(1/x)).nseries(x, n=1, logx=y) == loggamma(-y)

    # issue sympy/sympy#6725:
    assert expint(Rational(3, 2), -x)._eval_nseries(x, 8, None) == \
        2 - 2*I*sqrt(pi)*sqrt(x) - 2*x - x**2/3 - x**3/15 - x**4/84 + O(x**5)
    assert sin(sqrt(x))._eval_nseries(x, 6, None) == \
        sqrt(x) - x**Rational(3, 2)/6 + x**Rational(5, 2)/120 + O(x**Rational(7, 2))
コード例 #13
0
def test_function__eval_nseries():
    n = Symbol('n')

    assert sin(x)._eval_nseries(x, 2, None) == x + O(x**3)
    assert sin(x + 1)._eval_nseries(x, 2, None) == x*cos(1) + sin(1) + O(x**2)
    assert sin(pi*(1 - x))._eval_nseries(x, 2, None) == pi*x + O(x**3)
    assert acos(1 - x**2)._eval_nseries(x, 2, None) == sqrt(2)*x + O(x**2)
    assert polygamma(n, x + 1)._eval_nseries(x, 2, None) == \
        polygamma(n, 1) + polygamma(n + 1, 1)*x + O(x**2)
    pytest.raises(PoleError, lambda: sin(1/x)._eval_nseries(x, 2, None))
    assert acos(1 - x)._eval_nseries(x, 4, None) == sqrt(2)*sqrt(x) + \
        sqrt(2)*x**Rational(3, 2)/12 + O(x**2)
    assert acos(1 + x)._eval_nseries(x, 4, None) == sqrt(2)*I*sqrt(x) - \
        sqrt(2)*I*x**(3/2)/12 + O(x**2)  # XXX: wrong, branch cuts
    assert loggamma(1/x)._eval_nseries(x, 0, None) == \
        log(x)/2 - log(x)/x - 1/x + O(1, x)
    assert loggamma(log(1/x)).nseries(x, n=1, logx=y) == loggamma(-y)

    # issue sympy/sympy#6725:
    assert expint(Rational(3, 2), -x)._eval_nseries(x, 8, None) == \
        2 - 2*I*sqrt(pi)*sqrt(x) - 2*x - x**2/3 - x**3/15 - x**4/84 + O(x**5)
    assert sin(sqrt(x))._eval_nseries(x, 6, None) == \
        sqrt(x) - x**Rational(3, 2)/6 + x**Rational(5, 2)/120 + O(x**Rational(7, 2))
コード例 #14
0
 def _eval_rewrite_as_tractable(self, n):
     from diofant import loggamma, exp
     return exp(loggamma(n + 1))
コード例 #15
0
 def tN(N, M):
     assert loggamma(1/x)._eval_nseries(x, n=N).getn() == M
コード例 #16
0
def test_loggamma():
    pytest.raises(TypeError, lambda: loggamma(2, 3))
    pytest.raises(ArgumentIndexError, lambda: loggamma(x).fdiff(2))

    assert loggamma(-1) == oo
    assert loggamma(-2) == oo
    assert loggamma(0) == oo
    assert loggamma(1) == 0
    assert loggamma(2) == 0
    assert loggamma(3) == log(2)
    assert loggamma(4) == log(6)

    n = Symbol('n', integer=True, positive=True)
    assert loggamma(n) == log(gamma(n))
    assert loggamma(-n) == oo
    assert loggamma(n/2) == log(2**(-n + 1)*sqrt(pi)*gamma(n)/gamma(n/2 + Rational(1, 2)))

    assert loggamma(oo) == oo
    assert loggamma(-oo) == zoo
    assert loggamma(I*oo) == zoo
    assert loggamma(-I*oo) == zoo
    assert loggamma(zoo) == zoo
    assert loggamma(nan) == nan

    L = loggamma(Rational(16, 3))
    E = -5*log(3) + loggamma(Rational(1, 3)) + log(4) + log(7) + log(10) + log(13)
    assert expand_func(L).doit() == E
    assert L.evalf() == E.evalf()

    L = loggamma(Rational(19, 4))
    E = -4*log(4) + loggamma(Rational(3, 4)) + log(3) + log(7) + log(11) + log(15)
    assert expand_func(L).doit() == E
    assert L.evalf() == E.evalf()

    L = loggamma(Rational(23, 7))
    E = -3*log(7) + log(2) + loggamma(Rational(2, 7)) + log(9) + log(16)
    assert expand_func(L).doit() == E
    assert L.evalf() == E.evalf()

    L = loggamma(Rational(19, 4) - 7)
    E = -log(9) - log(5) + loggamma(Rational(3, 4)) + 3*log(4) - 3*I*pi
    assert expand_func(L).doit() == E
    assert L.evalf() == E.evalf()

    L = loggamma(Rational(23, 7) - 6)
    E = -log(19) - log(12) - log(5) + loggamma(Rational(2, 7)) + 3*log(7) - 3*I*pi
    assert expand_func(L).doit() == E
    assert L.evalf() == E.evalf()

    assert expand_func(loggamma(x)) == loggamma(x)
    assert expand_func(loggamma(Rational(1, 3))) == loggamma(Rational(1, 3))

    assert loggamma(x).diff(x) == polygamma(0, x)
    s1 = loggamma(1/(x + sin(x)) + cos(x)).series(x, n=4)
    s2 = (-log(2*x) - 1)/(2*x) - log(x/pi)/2 + (4 - log(2*x))*x/24 + O(x**2) + \
        log(x)*x**2/2
    assert (s1 - s2).expand(force=True).removeO() == 0
    s1 = loggamma(1/x).series(x)
    s2 = (1/x - Rational(1, 2))*log(1/x) - 1/x + log(2*pi)/2 + \
        x/12 - x**3/360 + x**5/1260 + O(x**7)
    assert ((s1 - s2).expand(force=True)).removeO() == 0

    assert loggamma(x).rewrite('intractable') == log(gamma(x))

    s1 = loggamma(x).series(x)
    assert s1 == -log(x) - EulerGamma*x + pi**2*x**2/12 + x**3*polygamma(2, 1)/6 + \
        pi**4*x**4/360 + x**5*polygamma(4, 1)/120 + O(x**6)
    assert s1 == loggamma(x).rewrite('intractable').series(x)

    assert conjugate(loggamma(x)) == conjugate(loggamma(x), evaluate=False)
    p = Symbol('p', positive=True)
    c = Symbol('c', complex=True, extended_real=False)
    assert conjugate(loggamma(p)) == loggamma(p)
    assert conjugate(loggamma(c)) == loggamma(conjugate(c))
    assert conjugate(loggamma(0)) == conjugate(loggamma(0))
    assert conjugate(loggamma(1)) == loggamma(conjugate(1))
    assert conjugate(loggamma(-oo)) == conjugate(loggamma(-oo))
    assert loggamma(x).is_extended_real is None
    y = Symbol('y', nonnegative=True)
    assert loggamma(y).is_extended_real
    assert loggamma(w).is_extended_real is None

    def tN(N, M):
        assert loggamma(1/x)._eval_nseries(x, n=N).getn() == M
    tN(0, 0)
    tN(1, 1)
    tN(2, 3)
    tN(3, 3)
    tN(4, 5)
    tN(5, 5)
コード例 #17
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)
コード例 #18
0
def test_polygamma():
    from diofant import I

    assert polygamma(n, nan) == nan

    assert polygamma(0, oo) == oo
    assert polygamma(0, -oo) == oo
    assert polygamma(0, I * oo) == oo
    assert polygamma(0, -I * oo) == oo
    assert polygamma(1, oo) == 0
    assert polygamma(5, oo) == 0

    assert polygamma(0, -9) == zoo

    assert polygamma(0, -9) == zoo
    assert polygamma(0, -1) == zoo

    assert polygamma(0, 0) == zoo

    assert polygamma(0, 1) == -EulerGamma
    assert polygamma(0, 7) == Rational(49, 20) - EulerGamma

    assert polygamma(1, 1) == pi**2 / 6
    assert polygamma(1, 2) == pi**2 / 6 - 1
    assert polygamma(1, 3) == pi**2 / 6 - Rational(5, 4)
    assert polygamma(3, 1) == pi**4 / 15
    assert polygamma(3, 5) == 6 * (Rational(-22369, 20736) + pi**4 / 90)
    assert polygamma(5, 1) == 8 * pi**6 / 63
    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.n()).n() - r.n()).n() < 1e-10

    assert t(1, 2)
    assert t(3, 2)
    assert t(-1, 2)
    assert t(1, 4)
    assert t(-3, 4)
    assert t(1, 3)
    assert t(4, 3)
    assert t(3, 4)
    assert t(2, 3)

    assert polygamma(0, x).rewrite(zeta) == polygamma(0, x)
    assert polygamma(1, x).rewrite(zeta) == zeta(2, x)
    assert polygamma(2, x).rewrite(zeta) == -2 * zeta(3, x)

    assert polygamma(3, 7 * x).diff(x) == 7 * polygamma(4, 7 * x)

    assert polygamma(0, x).rewrite(harmonic) == harmonic(x - 1) - EulerGamma
    assert polygamma(
        2, x).rewrite(harmonic) == 2 * harmonic(x - 1, 3) - 2 * zeta(3)
    ni = Symbol("n", integer=True)
    assert polygamma(
        ni,
        x).rewrite(harmonic) == (-1)**(ni + 1) * (-harmonic(x - 1, ni + 1) +
                                                  zeta(ni + 1)) * factorial(ni)

    # Polygamma of non-negative integer order is unbranched:
    from diofant import exp_polar
    k = Symbol('n', integer=True, nonnegative=True)
    assert polygamma(k, exp_polar(2 * I * pi) * x) == polygamma(k, x)

    # but negative integers are branched!
    k = Symbol('n', integer=True)
    assert polygamma(k,
                     exp_polar(2 * I * pi) *
                     x).args == (k, exp_polar(2 * I * pi) * x)

    # Polygamma of order -1 is loggamma:
    assert polygamma(-1, x) == loggamma(x)

    # But smaller orders are iterated integrals and don't have a special name
    assert polygamma(-2, x).func is polygamma

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

    assert polygamma(1, x).as_leading_term(x) == polygamma(1, x)
コード例 #19
0
ファイル: test_mathematica.py プロジェクト: diofant/diofant
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]'