Ejemplo n.º 1
0
def test_FunctionWrapper():
    import sympy
    n, m, theta, phi = sympy.symbols("n, m, theta, phi")
    r = sympy.Ynm(n, m, theta, phi)
    s = Integer(2)*r
    assert isinstance(s, Mul)
    assert isinstance(s.args[1]._sympy_(), sympy.Ynm)

    x = symbols("x")
    e = x + sympy.loggamma(x)
    assert str(e) == "x + loggamma(x)"
    assert isinstance(e, Add)
    assert e + sympy.loggamma(x) == x + 2*sympy.loggamma(x)

    f = e.subs({x : 10})
    assert f == 10 + log(362880)

    f = e.subs({x : 2})
    assert f == 2

    f = e.subs({x : 100});
    v = f.n(53, real=True);
    assert abs(float(v) - 459.13420537) < 1e-7

    f = e.diff(x)
    assert f == 1 + sympy.polygamma(0, x)
Ejemplo n.º 2
0
def test_intractable():
    assert gruntz(1/gamma(x), x, oo) == 0
    assert gruntz(1/loggamma(x), x, oo) == 0
    assert gruntz(gamma(x)/loggamma(x), x, oo) == oo
    assert gruntz(exp(gamma(x))/gamma(x), x, oo) == oo
    assert gruntz(gamma(x), x, 3) == 2
    assert gruntz(gamma(S(1)/7 + 1/x), x, oo) == gamma(S(1)/7)
    assert gruntz(log(x**x)/log(gamma(x)), x, oo) == 1
    assert gruntz(log(gamma(gamma(x)))/exp(x), x, oo) == oo
Ejemplo n.º 3
0
def test_loggamma():
    raises(TypeError, lambda: loggamma(2, 3))
    raises(ArgumentIndexError, lambda: loggamma(x).fdiff(2))
    assert loggamma(x).diff(x) == polygamma(0, x)
    s1 = loggamma(1/(x + sin(x)) + cos(x)).nseries(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 - S(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))

    assert loggamma(x).is_real is None
    y, z = Symbol('y', real=True), Symbol('z', imaginary=True)
    assert loggamma(y).is_real
    assert loggamma(z).is_real is False

    def tN(N, M):
        assert loggamma(1/x)._eval_nseries(x, n=N, logx=None).getn() == M
    tN(0, 0)
    tN(1, 1)
    tN(2, 3)
    tN(3, 3)
    tN(4, 5)
    tN(5, 5)
Ejemplo n.º 4
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, oo) == 1
    assert gruntz((erf(x - exp(-exp(x))) - erf(x)) * exp(exp(x)) * exp(x ** 2), x, oo) == -2 / sqrt(pi)
    assert gruntz(exp(exp(x)) * (exp(sin(1 / x + exp(-exp(x)))) - exp(sin(1 / x))), x, oo) == 1
    assert gruntz(exp(x) * (gamma(x + exp(-x)) - gamma(x)), x, oo) == oo
    assert gruntz(exp(exp(digamma(digamma(x)))) / x, x, oo) == exp(-S(1) / 2)
    assert gruntz(exp(exp(digamma(log(x)))) / x, x, oo) == exp(-S(1) / 2)
    assert gruntz(digamma(digamma(digamma(x))), x, oo) == oo
    assert gruntz(loggamma(loggamma(x)), x, oo) == oo
    assert gruntz(((gamma(x + 1 / gamma(x)) - gamma(x)) / log(x) - cos(1 / x)) * x * log(x), x, oo) == -S(1) / 2
    assert gruntz(x * (gamma(x - 1 / gamma(x)) - gamma(x) + log(x)), x, oo) == S(1) / 2
    assert gruntz((gamma(x + 1 / gamma(x)) - gamma(x)) / log(x), x, oo) == 1
Ejemplo n.º 5
0
def test_function__eval_nseries():
    n = Symbol("n")

    assert sin(x)._eval_nseries(x, 2, None) == x + O(x ** 2)
    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 ** 2)
    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)
    raises(PoleError, "sin(1/x)._eval_nseries(x,2,None)")
    raises(PoleError, "acos(1-x)._eval_nseries(x,2,None)")
    raises(PoleError, "acos(1+x)._eval_nseries(x,2,None)")
    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)
Ejemplo n.º 6
0
def test_loggamma():
    s1 = loggamma(1/(x+sin(x))+cos(x)).nseries(x,n=4)
    s2 = (-log(2*x)-1)/(2*x) - log(x/pi)/2 + (4-log(2*x))*x/24 + O(x**2)
    assert cancel(s1 - s2).removeO() == 0
    s1 = loggamma(1/x).series(x)
    s2 = (1/x-S(1)/2)*log(1/x) - 1/x  + log(2*pi)/2 + \
         x/12 - x**3/360 + x**5/1260 +  O(x**7)
    assert cancel(s1 - s2).removeO() == 0

    def tN(N, M):
        assert loggamma(1/x)._eval_nseries(x,n=N,logx=None).getn() == M
    tN(0, 0)
    tN(1, 1)
    tN(2, 3)
    tN(3, 3)
    tN(4, 5)
    tN(5, 5)
Ejemplo n.º 7
0
def test_function__eval_nseries():
    n = Symbol("n")

    assert sin(x)._eval_nseries(x, 2, None) == x + O(x ** 2)
    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 ** 2)
    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)
    raises(PoleError, lambda: sin(1 / x)._eval_nseries(x, 2, None))
    raises(PoleError, lambda: acos(1 - x)._eval_nseries(x, 2, None))
    raises(PoleError, lambda: acos(1 + x)._eval_nseries(x, 2, None))
    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 6725:
    assert expint(S(3) / 2, -x)._eval_nseries(x, 5, None) == 2 - 2 * 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, 3, None) == sqrt(x) - x ** (S(3) / 2) / 6 + x ** (S(5) / 2) / 120 + O(x ** 3)
Ejemplo n.º 8
0
 ('$PRED S22 = ABS(1 + 2 + SIN(X))', S('S22'),
  sympy.Abs(3 + sympy.sin(S('X')))),
 ('$PRED CL = TAN(X) * EXP(Y)', S('CL'),
  sympy.tan(S('X')) * sympy.exp(S('Y'))),
 (
     '$PRED K_ = ATAN(1) - ASIN(X)/ACOS(X)',
     S('K_'),
     sympy.atan(1) - sympy.asin(S('X')) / sympy.acos(S('X')),
 ),
 ('$PRED CL = INT(-2.2)', S('CL'), -2),
 ('$PRED cl = int(-2.2)', S('CL'), -2),
 ('$PRED CL = INT(0.2)', S('CL'), 0),
 ('$PRED CL = SQRT(X)', S('CL'), sympy.sqrt(S('X'))),
 ('$PRED CL = MOD(1, 2)', S('CL'), sympy.Mod(1, 2)),
 ('$PRED CL = GAMLN(2 + X)   ;COMMENT', S('CL'),
  sympy.loggamma(S('X') + 2)),
 ('$PRED C02 = PHI(2 + X)', S('C02'),
  (1 + sympy.erf(2 + S('X')) / sympy.sqrt(2)) / 2),
 ('$PRED IF (X.EQ.2) CL=23', S('CL'),
  sympy.Piecewise((23, sympy.Eq(S('X'), 2)))),
 ('$PRED if (x.EQ.2) Cl=23', S('CL'),
  sympy.Piecewise((23, sympy.Eq(S('X'), 2)))),
 (
     '$PRED IF (X.NE.1.5) CL=THETA(1)',
     S('CL'),
     sympy.Piecewise((S('THETA(1)'), sympy.Ne(S('X'), 1.5))),
 ),
 ('$PRED IF (X.EQ.2+1) CL=23', S('CL'),
  sympy.Piecewise((23, sympy.Eq(S('X'), 3)))),
 (
     '$PRED IF (X < ETA(1)) CL=23',
Ejemplo n.º 9
0
def test_loggamma():
    raises(TypeError, lambda: loggamma(2, 3))
    raises(ArgumentIndexError, lambda: loggamma(x).fdiff(2))
    assert loggamma(x).diff(x) == polygamma(0, x)
    s1 = loggamma(1 / (x + sin(x)) + cos(x)).nseries(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 - S(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 loggamma(x).is_real is None
    y, z = Symbol('y', real=True), Symbol('z', imaginary=True)
    assert loggamma(y).is_real
    assert loggamma(z).is_real is False

    def tN(N, M):
        assert loggamma(1 / x)._eval_nseries(x, n=N, logx=None).getn() == M

    tN(0, 0)
    tN(1, 1)
    tN(2, 3)
    tN(3, 3)
    tN(4, 5)
    tN(5, 5)
Ejemplo n.º 10
0
 def tN(N, M):
     assert loggamma(1/x)._eval_nseries(x,n=N,logx=None).getn() == M
Ejemplo n.º 11
0
def test_polygamma():
    from sympy 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

    def t(m, n):
        x = S(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 sympy 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)
Ejemplo n.º 12
0
def test_loggamma():
    raises(TypeError, lambda: loggamma(2, 3))
    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 + S.Half))

    from sympy import I

    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(S(16)/3)
    E = -5*log(3) + loggamma(S(1)/3) + log(4) + log(7) + log(10) + log(13)
    assert expand_func(L).doit() == E
    assert L.n() == E.n()

    L = loggamma(19/S(4))
    E = -4*log(4) + loggamma(S(3)/4) + log(3) + log(7) + log(11) + log(15)
    assert expand_func(L).doit() == E
    assert L.n() == E.n()

    L = loggamma(S(23)/7)
    E = -3*log(7) + log(2) + loggamma(S(2)/7) + log(9) + log(16)
    assert expand_func(L).doit() == E
    assert L.n() == E.n()

    L = loggamma(19/S(4)-7)
    E = -log(9) - log(5) + loggamma(S(3)/4) + 3*log(4) - 3*I*pi
    assert expand_func(L).doit() == E
    assert L.n() == E.n()

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

    assert loggamma(x).diff(x) == polygamma(0, x)
    s1 = loggamma(1/(x + sin(x)) + cos(x)).nseries(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 - S(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)) == loggamma(conjugate(x))
    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_real is None
    y, z = Symbol('y', real=True), Symbol('z', imaginary=True)
    assert loggamma(y).is_real
    assert loggamma(z).is_real is False

    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)
Ejemplo n.º 13
0
def test_polygamma():
    from sympy import I

    assert polygamma(n, nan) is nan

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

    assert polygamma(0, -9) is zoo

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

    assert polygamma(0, 0) is 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 polygamma(1, S.Half) == pi**2 / 2
    assert polygamma(2, S.Half) == -14 * zeta(3)
    assert polygamma(11, S.Half) == 176896 * pi**12

    def t(m, n):
        x = S(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 t(123, 5)

    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(I, 2).rewrite(zeta) == polygamma(I, 2)
    n1 = Symbol('n1')
    n2 = Symbol('n2', real=True)
    n3 = Symbol('n3', integer=True)
    n4 = Symbol('n4', positive=True)
    n5 = Symbol('n5', positive=True, integer=True)
    assert polygamma(n1, x).rewrite(zeta) == polygamma(n1, x)
    assert polygamma(n2, x).rewrite(zeta) == polygamma(n2, x)
    assert polygamma(n3, x).rewrite(zeta) == polygamma(n3, x)
    assert polygamma(n4, x).rewrite(zeta) == polygamma(n4, x)
    assert polygamma(
        n5,
        x).rewrite(zeta) == (-1)**(n5 + 1) * factorial(n5) * zeta(n5 + 1, 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 sympy 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(2, 2.5).is_positive == False
    assert polygamma(2, -2.5).is_positive == False
    assert polygamma(3, 2.5).is_positive == True
    assert polygamma(3, -2.5).is_positive is True
    assert polygamma(-2, -2.5).is_positive is None
    assert polygamma(-3, -2.5).is_positive is None

    assert polygamma(2, 2.5).is_negative == True
    assert polygamma(3, 2.5).is_negative == False
    assert polygamma(3, -2.5).is_negative == False
    assert polygamma(2, -2.5).is_negative is True
    assert polygamma(-2, -2.5).is_negative is None
    assert polygamma(-3, -2.5).is_negative is None

    assert polygamma(I, 2).is_positive is None
    assert polygamma(I, 3).is_negative is None

    # issue 17350
    assert polygamma(pi, 3).evalf() == polygamma(pi, 3)
    assert (I*polygamma(I, pi)).as_real_imag() == \
           (-im(polygamma(I, pi)), re(polygamma(I, pi)))
    assert (tanh(polygamma(I, 1))).rewrite(exp) == \
           (exp(polygamma(I, 1)) - exp(-polygamma(I, 1)))/(exp(polygamma(I, 1)) + exp(-polygamma(I, 1)))
    assert (I / polygamma(I, 4)).rewrite(exp) == \
           I*sqrt(re(polygamma(I, 4))**2 + im(polygamma(I, 4))**2)\
           /((re(polygamma(I, 4)) + I*im(polygamma(I, 4)))*Abs(polygamma(I, 4)))
    assert unchanged(polygamma, 2.3, 1.0)

    # issue 12569
    assert unchanged(im, polygamma(0, I))
    assert polygamma(Symbol('a', positive=True), Symbol(
        'b', positive=True)).is_real is True
    assert polygamma(0, I).is_real is None
Ejemplo n.º 14
0
def test_loggamma():
    raises(TypeError, lambda: loggamma(2, 3))
    raises(ArgumentIndexError, lambda: loggamma(x).fdiff(2))
    assert loggamma(x).diff(x) == polygamma(0, x)
    s1 = loggamma(1 / (x + sin(x)) + cos(x)).nseries(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 - S(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)) == loggamma(conjugate(x))
    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_real is None
    y, z = Symbol("y", real=True), Symbol("z", imaginary=True)
    assert loggamma(y).is_real
    assert loggamma(z).is_real is False

    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)
Ejemplo n.º 15
0
def test_loggamma():
    raises(TypeError, lambda: loggamma(2, 3))
    raises(ArgumentIndexError, lambda: loggamma(x).fdiff(2))

    assert loggamma(-1) is oo
    assert loggamma(-2) is oo
    assert loggamma(0) is 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) is oo
    assert loggamma(n / 2) == log(2**(-n + 1) * sqrt(pi) * gamma(n) /
                                  gamma(n / 2 + S.Half))

    from sympy import I

    assert loggamma(oo) is oo
    assert loggamma(-oo) is zoo
    assert loggamma(I * oo) is zoo
    assert loggamma(-I * oo) is zoo
    assert loggamma(zoo) is zoo
    assert loggamma(nan) is 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.n() == E.n()

    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.n() == E.n()

    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.n() == E.n()

    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.n() == E.n()

    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.n() == E.n()

    assert loggamma(x).diff(x) == polygamma(0, x)
    s1 = loggamma(1 / (x + sin(x)) + cos(x)).nseries(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 - S.Half)*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)) == loggamma(conjugate(x))
    assert conjugate(loggamma(0)) is oo
    assert conjugate(loggamma(1)) == loggamma(conjugate(1))
    assert conjugate(loggamma(-oo)) == conjugate(zoo)

    assert loggamma(Symbol('v', positive=True)).is_real is True
    assert loggamma(Symbol('v', zero=True)).is_real is False
    assert loggamma(Symbol('v', negative=True)).is_real is False
    assert loggamma(Symbol('v', nonpositive=True)).is_real is False
    assert loggamma(Symbol('v', nonnegative=True)).is_real is None
    assert loggamma(Symbol('v', imaginary=True)).is_real is None
    assert loggamma(Symbol('v', real=True)).is_real is None
    assert loggamma(Symbol('v')).is_real is None

    assert loggamma(S.Half).is_real is True
    assert loggamma(0).is_real is False
    assert loggamma(Rational(-1, 2)).is_real is False
    assert loggamma(I).is_real is None
    assert loggamma(2 + 3 * I).is_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)
Ejemplo n.º 16
0
def test_polygamma():
    from sympy import I

    assert polygamma(n, nan) == nan

    assert polygamma(0, 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

    def t(m, n):
        x = S(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 sympy 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)
Ejemplo n.º 17
0
 def tN(N, M):
     assert loggamma(1 / x)._eval_nseries(x, n=N).getn() == M