Beispiel #1
0
def test_harmonic_limit():
    n = Symbol('n')
    m = Symbol('m', positive=True)
    assert limit(harmonic(n, m + 1), n, oo) == zeta(m + 1)

    assert limit(harmonic(n, 2), n, oo) == pi**2 / 6
    assert limit(harmonic(n, 3), n, oo) == -polygamma(2, 1) / 2
Beispiel #2
0
def test_harmonic_rewrite_sum_fail():
    n = Symbol('n')
    m = Symbol('m')

    _k = Dummy('k')
    assert harmonic(n).rewrite(Sum) == Sum(1 / _k, (_k, 1, n))
    assert harmonic(n, m).rewrite(Sum) == Sum(_k**(-m), (_k, 1, n))
Beispiel #3
0
def test_harmonic_rewrite_sum():
    n = Symbol('n')
    m = Symbol('m')

    _k = Dummy('k')
    assert replace_dummy(harmonic(n).rewrite(Sum),
                         _k) == Sum(1 / _k, (_k, 1, n))
    assert replace_dummy(harmonic(n, m).rewrite(Sum),
                         _k) == Sum(_k**(-m), (_k, 1, n))
Beispiel #4
0
def test_harmonic_evalf():
    assert str(harmonic(1.5).evalf(10)) == '1.280372306'
    assert str(harmonic(
        1.5, 2).evalf(10)) == '1.154576311'  # issue sympy/sympy#7443
Beispiel #5
0
def test_harmonic_rewrite_polygamma():
    n = Symbol('n')
    m = Symbol('m')

    assert harmonic(n).rewrite(digamma) == polygamma(0, n + 1) + EulerGamma
    assert harmonic(n).rewrite(trigamma) == polygamma(0, n + 1) + EulerGamma
    assert harmonic(n).rewrite(polygamma) == polygamma(0, n + 1) + EulerGamma

    assert harmonic(
        n,
        3).rewrite(polygamma) == polygamma(2, n + 1) / 2 - polygamma(2, 1) / 2
    assert harmonic(n, m).rewrite(polygamma) == (-1)**m * (
        polygamma(m - 1, 1) - polygamma(m - 1, n + 1)) / factorial(m - 1)

    assert expand_func(
        harmonic(n + 4)
    ) == harmonic(n) + 1 / (n + 4) + 1 / (n + 3) + 1 / (n + 2) + 1 / (n + 1)
    assert expand_func(harmonic(
        n -
        4)) == harmonic(n) - 1 / (n - 1) - 1 / (n - 2) - 1 / (n - 3) - 1 / n

    assert harmonic(n, m).rewrite('tractable') == harmonic(
        n, m).rewrite(polygamma).rewrite(gamma).rewrite('tractable')

    assert isinstance(expand_func(harmonic(n, 2)), harmonic)

    assert expand_func(harmonic(n + Rational(1, 2))) == expand_func(
        harmonic(n + Rational(1, 2)))
    assert expand_func(harmonic(Rational(-1, 2))) == harmonic(Rational(-1, 2))
    assert expand_func(harmonic(x)) == harmonic(x)
Beispiel #6
0
def test_harmonic():
    n = Symbol('n')

    assert harmonic(n, 0) == n
    assert harmonic(n).evalf() == harmonic(n)
    assert harmonic(n, 1) == harmonic(n)
    assert harmonic(1, n).evalf() == harmonic(1, n)

    assert harmonic(0, 1) == 0
    assert harmonic(1, 1) == 1
    assert harmonic(2, 1) == Rational(3, 2)
    assert harmonic(3, 1) == Rational(11, 6)
    assert harmonic(4, 1) == Rational(25, 12)
    assert harmonic(0, 2) == 0
    assert harmonic(1, 2) == 1
    assert harmonic(2, 2) == Rational(5, 4)
    assert harmonic(3, 2) == Rational(49, 36)
    assert harmonic(4, 2) == Rational(205, 144)
    assert harmonic(0, 3) == 0
    assert harmonic(1, 3) == 1
    assert harmonic(2, 3) == Rational(9, 8)
    assert harmonic(3, 3) == Rational(251, 216)
    assert harmonic(4, 3) == Rational(2035, 1728)

    assert harmonic(oo, -1) == nan
    assert harmonic(oo, 0) == oo
    assert harmonic(oo, Rational(1, 2)) == oo
    assert harmonic(oo, 1) == oo
    assert harmonic(oo, 2) == (pi**2) / 6
    assert harmonic(oo, 3) == zeta(3)
    assert harmonic(oo, x) == harmonic(oo, x, evaluate=False)
Beispiel #7
0
def test_harmonic_rational():
    ne = Integer(6)
    no = Integer(5)
    pe = Integer(8)
    po = Integer(9)
    qe = Integer(10)
    qo = Integer(13)

    Heee = harmonic(ne + pe / qe)
    Aeee = (-log(10) + 2 * (Rational(-1, 4) + sqrt(5) / 4) *
            log(sqrt(-sqrt(5) / 8 + Rational(5, 8))) + 2 *
            (-sqrt(5) / 4 - Rational(1, 4)) *
            log(sqrt(sqrt(5) / 8 + Rational(5, 8))) + pi *
            (Rational(1, 4) + sqrt(5) / 4) /
            (2 * sqrt(-sqrt(5) / 8 + Rational(5, 8))) +
            Rational(13944145, 4720968))

    Heeo = harmonic(ne + pe / qo)
    Aeeo = (-log(26) + 2 * log(sin(3 * pi / 13)) * cos(4 * pi / 13) +
            2 * log(sin(2 * pi / 13)) * cos(32 * pi / 13) +
            2 * log(sin(5 * pi / 13)) * cos(80 * pi / 13) -
            2 * log(sin(6 * pi / 13)) * cos(5 * pi / 13) -
            2 * log(sin(4 * pi / 13)) * cos(pi / 13) +
            pi * cot(5 * pi / 13) / 2 -
            2 * log(sin(pi / 13)) * cos(3 * pi / 13) +
            Rational(2422020029, 702257080))

    Heoe = harmonic(ne + po / qe)
    Aeoe = (
        -log(20) + 2 *
        (Rational(1, 4) + sqrt(5) / 4) * log(Rational(-1, 4) + sqrt(5) / 4) +
        2 * (Rational(-1, 4) + sqrt(5) / 4) *
        log(sqrt(-sqrt(5) / 8 + Rational(5, 8))) + 2 *
        (-sqrt(5) / 4 - Rational(1, 4)) *
        log(sqrt(sqrt(5) / 8 + Rational(5, 8))) + 2 *
        (-sqrt(5) / 4 + Rational(1, 4)) * log(Rational(1, 4) + sqrt(5) / 4) +
        Rational(11818877030, 4286604231) + pi *
        (sqrt(5) / 8 + Rational(5, 8)) / sqrt(-sqrt(5) / 8 + Rational(5, 8)))

    Heoo = harmonic(ne + po / qo)
    Aeoo = (-log(26) + 2 * log(sin(3 * pi / 13)) * cos(54 * pi / 13) +
            2 * log(sin(4 * pi / 13)) * cos(6 * pi / 13) +
            2 * log(sin(6 * pi / 13)) * cos(108 * pi / 13) -
            2 * log(sin(5 * pi / 13)) * cos(pi / 13) -
            2 * log(sin(pi / 13)) * cos(5 * pi / 13) +
            pi * cot(4 * pi / 13) / 2 -
            2 * log(sin(2 * pi / 13)) * cos(3 * pi / 13) +
            Rational(11669332571, 3628714320))

    Hoee = harmonic(no + pe / qe)
    Aoee = (-log(10) + 2 * (Rational(-1, 4) + sqrt(5) / 4) *
            log(sqrt(-sqrt(5) / 8 + Rational(5, 8))) + 2 *
            (-sqrt(5) / 4 - Rational(1, 4)) *
            log(sqrt(sqrt(5) / 8 + Rational(5, 8))) + pi *
            (Rational(1, 4) + sqrt(5) / 4) /
            (2 * sqrt(-sqrt(5) / 8 + Rational(5, 8))) +
            Rational(779405, 277704))

    Hoeo = harmonic(no + pe / qo)
    Aoeo = (-log(26) + 2 * log(sin(3 * pi / 13)) * cos(4 * pi / 13) +
            2 * log(sin(2 * pi / 13)) * cos(32 * pi / 13) +
            2 * log(sin(5 * pi / 13)) * cos(80 * pi / 13) -
            2 * log(sin(6 * pi / 13)) * cos(5 * pi / 13) -
            2 * log(sin(4 * pi / 13)) * cos(pi / 13) +
            pi * cot(5 * pi / 13) / 2 -
            2 * log(sin(pi / 13)) * cos(3 * pi / 13) +
            Rational(53857323, 16331560))

    Hooe = harmonic(no + po / qe)
    Aooe = (
        -log(20) + 2 *
        (Rational(1, 4) + sqrt(5) / 4) * log(Rational(-1, 4) + sqrt(5) / 4) +
        2 * (Rational(-1, 4) + sqrt(5) / 4) *
        log(sqrt(-sqrt(5) / 8 + Rational(5, 8))) + 2 *
        (-sqrt(5) / 4 - Rational(1, 4)) *
        log(sqrt(sqrt(5) / 8 + Rational(5, 8))) + 2 *
        (-sqrt(5) / 4 + Rational(1, 4)) * log(Rational(1, 4) + sqrt(5) / 4) +
        Rational(486853480, 186374097) + pi *
        (sqrt(5) / 8 + Rational(5, 8)) / sqrt(-sqrt(5) / 8 + Rational(5, 8)))

    Hooo = harmonic(no + po / qo)
    Aooo = (-log(26) + 2 * log(sin(3 * pi / 13)) * cos(54 * pi / 13) +
            2 * log(sin(4 * pi / 13)) * cos(6 * pi / 13) +
            2 * log(sin(6 * pi / 13)) * cos(108 * pi / 13) -
            2 * log(sin(5 * pi / 13)) * cos(pi / 13) -
            2 * log(sin(pi / 13)) * cos(5 * pi / 13) +
            pi * cot(4 * pi / 13) / 2 -
            2 * log(sin(2 * pi / 13)) * cos(3 * pi / 13) +
            Rational(383693479, 125128080))

    H = [Heee, Heeo, Heoe, Heoo, Hoee, Hoeo, Hooe, Hooo]
    A = [Aeee, Aeeo, Aeoe, Aeoo, Aoee, Aoeo, Aooe, Aooo]

    for h, a in zip(H, A):
        e = expand_func(h).doit()
        assert cancel(e / a) == 1
        assert h.evalf() == a.evalf()
def test_harmonic_sums():
    assert summation(1/k, (k, 0, n)) == Sum(1/k, (k, 0, n))
    assert summation(1/k, (k, 1, n)) == harmonic(n)
    assert summation(n/k, (k, 1, n)) == n*harmonic(n)
    assert summation(1/k, (k, 5, n)) == harmonic(n) - harmonic(4)
Beispiel #9
0
def test_harmonic_sums():
    assert summation(1 / k, (k, 0, n)) == Sum(1 / k, (k, 0, n))
    assert summation(1 / k, (k, 1, n)) == harmonic(n)
    assert summation(n / k, (k, 1, n)) == n * harmonic(n)
    assert summation(1 / k, (k, 5, n)) == harmonic(n) - harmonic(4)
Beispiel #10
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)
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)
Beispiel #12
0
def test_harmonic_rewrite_sum():
    assert harmonic(n).rewrite(Sum) == Sum(1 / k, (k, 1, n))
    assert harmonic(n, m).rewrite(Sum) == Sum(k**(-m), (k, 1, n))