Beispiel #1
0
def test_gosper_sum_indefinite():
    assert gosper_sum(k, k) == k*(k - 1)/2
    assert gosper_sum(k**2, k) == k*(k - 1)*(2*k - 1)/6

    assert gosper_sum(1/(k*(k + 1)), k) == -1/k
    assert gosper_sum(-(27*k**4 + 158*k**3 + 430*k**2 + 678*k + 445)*gamma(2*k + 4)/(3*(3*k + 7)*gamma(3*k + 6)), k) == \
        (3*k + 5)*(k**2 + 2*k + 5)*gamma(2*k + 4)/gamma(3*k + 6)
Beispiel #2
0
def test_gosper_sum_indefinite():
    assert gosper_sum(k, k) == k * (k - 1) / 2
    assert gosper_sum(k**2, k) == k * (k - 1) * (2 * k - 1) / 6

    assert gosper_sum(1 / (k * (k + 1)), k) == -1 / k
    assert gosper_sum(-(27*k**4 + 158*k**3 + 430*k**2 + 678*k + 445)*gamma(2*k + 4)/(3*(3*k + 7)*gamma(3*k + 6)), k) == \
        (3*k + 5)*(k**2 + 2*k + 5)*gamma(2*k + 4)/gamma(3*k + 6)
Beispiel #3
0
def test_gosper_sum_indefinite():
    assert gosper_sum(k, k) == k * (k - 1) / 2
    assert gosper_sum(k**2, k) == k * (k - 1) * (2 * k - 1) / 6

    assert gosper_sum(1 / (k * (k + 1)), k) == -1 / k
    assert gosper_sum(
        -(27 * k**4 + 158 * k**3 + 430 * k**2 + 678 * k + 445) *
        factorial(2 * k + 3) / (3 * (3 * k + 7) * factorial(3 * k + 5)),
        k) == (3 * k + 5) * (k**2 + 2 * k +
                             5) * factorial(2 * k + 3) / factorial(3 * k + 5)
Beispiel #4
0
def test_gosper_sum_iterated():
    f1 = binomial(2*k, k)/4**k
    f2 = (1 + 2*n)*binomial(2*n, n)/4**n
    f3 = (1 + 2*n)*(3 + 2*n)*binomial(2*n, n)/(3*4**n)
    f4 = (1 + 2*n)*(3 + 2*n)*(5 + 2*n)*binomial(2*n, n)/(15*4**n)
    f5 = (1 + 2*n)*(3 + 2*n)*(5 + 2*n)*(7 + 2*n)*binomial(2*n, n)/(105*4**n)

    assert gosper_sum(f1, (k, 0, n)) == f2
    assert gosper_sum(f2, (n, 0, n)) == f3
    assert gosper_sum(f3, (n, 0, n)) == f4
    assert gosper_sum(f4, (n, 0, n)) == f5
Beispiel #5
0
def test_gosper_sum_iterated():
    f1 = binomial(2*k, k)/4**k
    f2 = (1 + 2*n)*binomial(2*n, n)/4**n
    f3 = (1 + 2*n)*(3 + 2*n)*binomial(2*n, n)/(3*4**n)
    f4 = (1 + 2*n)*(3 + 2*n)*(5 + 2*n)*binomial(2*n, n)/(15*4**n)
    f5 = (1 + 2*n)*(3 + 2*n)*(5 + 2*n)*(7 + 2*n)*binomial(2*n, n)/(105*4**n)

    assert gosper_sum(f1, (k, 0, n)) == f2
    assert gosper_sum(f2, (n, 0, n)) == f3
    assert gosper_sum(f3, (n, 0, n)) == f4
    assert gosper_sum(f4, (n, 0, n)) == f5
Beispiel #6
0
def test_gosper_sum_AeqB_part3():
    f3a = 1 / n**4
    f3b = (6 * n + 3) / (4 * n**4 + 8 * n**3 + 8 * n**2 + 4 * n + 3)
    f3c = 2**n * (n**2 - 2 * n - 1) / (n**2 * (n + 1)**2)
    f3d = n**2 * 4**n / ((n + 1) * (n + 2))
    f3e = 2**n / (n + 1)
    f3f = 4 * (n - 1) * (n**2 - 2 * n - 1) / (n**2 * (n + 1)**2 * (n - 2)**2 *
                                              (n - 3)**2)
    f3g = (n**4 - 14 * n**2 - 24 * n - 9) * 2**n / (n**2 * (n + 1)**2 *
                                                    (n + 2)**2 * (n + 3)**2)

    # g3a -> no closed form
    g3b = m * (m + 2) / (2 * m**2 + 4 * m + 3)
    g3c = 2**m / m**2 - 2
    g3d = Rational(2, 3) + 4**(m + 1) * (m - 1) / (m + 2) / 3
    # g3e -> no closed form
    g3f = -(-Rational(1, 16) + 1 / ((m - 2)**2 *
                                    (m + 1)**2))  # the AeqB key is wrong
    g3g = -Rational(2, 9) + 2**(m + 1) / ((m + 1)**2 * (m + 3)**2)

    g = gosper_sum(f3a, (n, 1, m))
    assert g is None
    g = gosper_sum(f3b, (n, 1, m))
    assert g is not None and simplify(g - g3b) == 0
    g = gosper_sum(f3c, (n, 1, m - 1))
    assert g is not None and simplify(g - g3c) == 0
    g = gosper_sum(f3d, (n, 1, m))
    assert g is not None and simplify(g - g3d) == 0
    g = gosper_sum(f3e, (n, 0, m - 1))
    assert g is None
    g = gosper_sum(f3f, (n, 4, m))
    assert g is not None and simplify(g - g3f) == 0
    g = gosper_sum(f3g, (n, 1, m))
    assert g is not None and simplify(g - g3g) == 0
Beispiel #7
0
def test_gosper_sum_AeqB_part3():
    f3a = 1/n**4
    f3b = (6*n + 3)/(4*n**4 + 8*n**3 + 8*n**2 + 4*n + 3)
    f3c = 2**n*(n**2 - 2*n - 1)/(n**2*(n + 1)**2)
    f3d = n**2*4**n/((n + 1)*(n + 2))
    f3e = 2**n/(n + 1)
    f3f = 4*(n - 1)*(n**2 - 2*n - 1)/(n**2*(n + 1)**2*(n - 2)**2*(n - 3)**2)
    f3g = (n**4 - 14*n**2 - 24*n - 9)*2**n/(n**2*(n + 1)**2*(n + 2)**2 *
                                            (n + 3)**2)

    # g3a -> no closed form
    g3b = m*(m + 2)/(2*m**2 + 4*m + 3)
    g3c = 2**m/m**2 - 2
    g3d = Rational(2, 3) + 4**(m + 1)*(m - 1)/(m + 2)/3
    # g3e -> no closed form
    g3f = -(-Rational(1, 16) + 1/((m - 2)**2*(m + 1)**2))  # the AeqB key is wrong
    g3g = -Rational(2, 9) + 2**(m + 1)/((m + 1)**2*(m + 3)**2)

    g = gosper_sum(f3a, (n, 1, m))
    assert g is None
    g = gosper_sum(f3b, (n, 1, m))
    assert g is not None and simplify(g - g3b) == 0
    g = gosper_sum(f3c, (n, 1, m - 1))
    assert g is not None and simplify(g - g3c) == 0
    g = gosper_sum(f3d, (n, 1, m))
    assert g is not None and simplify(g - g3d) == 0
    g = gosper_sum(f3e, (n, 0, m - 1))
    assert g is None
    g = gosper_sum(f3f, (n, 4, m))
    assert g is not None and simplify(g - g3f) == 0
    g = gosper_sum(f3g, (n, 1, m))
    assert g is not None and simplify(g - g3g) == 0
Beispiel #8
0
def test_gosper_sum_AeqB_part3():
    # Ex. 5.7.3
    f3a = 1 / n**4
    f3b = (6 * n + 3) / (4 * n**4 + 8 * n**3 + 8 * n**2 + 4 * n + 3)
    f3c = 2**n * (n**2 - 2 * n - 1) / (n**2 * (n + 1)**2)
    f3d = n**2 * 4**n / ((n + 1) * (n + 2))
    f3e = 2**n / (n + 1)
    f3f = 4 * (n - 1) * (n**2 - 2 * n - 1) / (n**2 * (n + 1)**2 * (n - 2)**2 *
                                              (n - 3)**2)
    f3g = (n**4 - 14 * n**2 - 24 * n - 9) * 2**n / (n**2 * (n + 1)**2 *
                                                    (n + 2)**2 * (n + 3)**2)

    g3b = m * (m + 2) / (2 * m**2 + 4 * m + 3)
    g3c = 2**m / m**2 - 2
    g3d = Rational(2, 3) + 4**(m + 1) * (m - 1) / (m + 2) / 3
    g3f = Rational(1, 16) - 1 / ((m - 2)**2 * (m + 1)**2)  # sign corrected
    g3g = -Rational(2, 9) + 2**(m + 1) / ((m + 1)**2 * (m + 3)**2)

    assert gosper_sum(f3a, (n, 1, m)) is None
    assert gosper_sum(f3b, (n, 1, m)).equals(g3b)
    assert gosper_sum(f3c, (n, 1, m - 1)).equals(g3c)
    assert gosper_sum(f3d, (n, 1, m)).equals(g3d)
    assert gosper_sum(f3e, (n, 0, m - 1)) is None
    assert gosper_sum(f3f, (n, 4, m)).equals(g3f)
    assert gosper_sum(f3g, (n, 1, m)).equals(g3g)
Beispiel #9
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
Beispiel #10
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
Beispiel #11
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
Beispiel #12
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
Beispiel #13
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)
Beispiel #14
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
Beispiel #15
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
Beispiel #16
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)
Beispiel #17
0
def test_sympyissue_12018():
    # Ex. 5.7.11a
    f = binomial(n + 1, k) / 2**(n + 1) - binomial(n, k) / 2**n
    assert gosper_sum(f, (k, 0, n)).equals(-2**(-n - 1))
Beispiel #18
0
def test_gosper_sum_algebraic():
    assert gosper_sum(n**2 + sqrt(2),
                      (n, 0, m)) == (m + 1) * (2 * m**2 + m + 6 * sqrt(2)) / 6
Beispiel #19
0
def test_gosper_sum_parametric():
    assert gosper_sum(binomial(Rational(1, 2), m - j + 1)*binomial(Rational(1, 2), m + j), (j, 1, n)) == \
        n*(1 + m - n)*(-1 + 2*m + 2*n)*binomial(Rational(1, 2), 1 + m - n) * \
        binomial(Rational(1, 2), m + n)/(m*(1 + 2*m))
Beispiel #20
0
def test_sympyissue_12018():
    f = binomial(n + 1, k)/2**(n + 1) - binomial(n, k)/2**n  # Ex. 5.7.11a
    assert gosper_sum(f, (k, 0, n)).simplify() == -2**(-n - 1)
Beispiel #21
0
def test_sympyissue_12018():
    f = binomial(n + 1, k) / 2**(n + 1) - binomial(n, k) / 2**n  # Ex. 5.7.11a
    assert gosper_sum(f, (k, 0, n)).simplify() == -2**(-n - 1)
Beispiel #22
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))
Beispiel #23
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))
Beispiel #24
0
def test_gosper_sum_parametric():
    assert gosper_sum(binomial(Rational(1, 2), m - j + 1)*binomial(Rational(1, 2), m + j), (j, 1, n)) == \
        n*(1 + m - n)*(-1 + 2*m + 2*n)*binomial(Rational(1, 2), 1 + m - n) * \
        binomial(Rational(1, 2), m + n)/(m*(1 + 2*m))
Beispiel #25
0
def test_gosper_sum_algebraic():
    assert gosper_sum(
        n**2 + sqrt(2), (n, 0, m)) == (m + 1)*(2*m**2 + m + 6*sqrt(2))/6
Beispiel #26
0
def test_gosper_sum_parametric():
    assert gosper_sum(
        binomial(Rational(1, 2), m - k + 1) * binomial(Rational(1, 2), m + k),
        (k, 1, n)) == (n * (1 + m - n) * (-1 + 2 * m + 2 * n) *
                       binomial(Rational(1, 2), 1 + m - n) *
                       binomial(Rational(1, 2), m + n) / (m * (1 + 2 * m)))
Beispiel #27
0
def eval_sum_symbolic(f, limits):
    from diofant.functions import harmonic, bernoulli

    f_orig = f
    (i, a, b) = limits
    if not f.has(i):
        return f * (b - a + 1)

    # Linearity
    if f.is_Mul:
        L, R = f.as_two_terms()

        if not L.has(i):
            sR = eval_sum_symbolic(R, (i, a, b))
            if sR:
                return L * sR

        if not R.has(i):
            sL = eval_sum_symbolic(L, (i, a, b))
            if sL:
                return R * sL

        try:
            f = apart(f, i)  # see if it becomes an Add
        except PolynomialError:
            pass

    if f.is_Add:
        L, R = f.as_two_terms()
        lrsum = telescopic(L, R, (i, a, b))

        if lrsum:
            return lrsum

        lsum = eval_sum_symbolic(L, (i, a, b))
        rsum = eval_sum_symbolic(R, (i, a, b))

        if None not in (lsum, rsum):
            r = lsum + rsum
            if r is not S.NaN:
                return r

    # Polynomial terms with Faulhaber's formula
    n = Wild('n')
    result = f.match(i**n)

    if result is not None:
        n = result[n]

        if n.is_Integer:
            if n >= 0:
                if (b is S.Infinity and a is not S.NegativeInfinity) or \
                   (a is S.NegativeInfinity and b is not S.Infinity):
                    return S.Infinity
                return ((bernoulli(n + 1, b + 1) - bernoulli(n + 1, a)) /
                        (n + 1)).expand()
            elif a.is_Integer and a >= 1:
                if n == -1:
                    return harmonic(b) - harmonic(a - 1)
                else:
                    return harmonic(b, abs(n)) - harmonic(a - 1, abs(n))

    if not (a.has(S.Infinity, S.NegativeInfinity)
            or b.has(S.Infinity, S.NegativeInfinity)):
        # Geometric terms
        c1 = Wild('c1', exclude=[i])
        c2 = Wild('c2', exclude=[i])
        c3 = Wild('c3', exclude=[i])

        e = f.match(c1**(c2 * i + c3))

        if e is not None:
            p = (c1**c3).subs(e)
            q = (c1**c2).subs(e)

            r = p * (q**a - q**(b + 1)) / (1 - q)
            l = p * (b - a + 1)

            return Piecewise((l, Eq(q, S.One)), (r, True))

    r = gosper_sum(f, (i, a, b))
    if r is not None and r.is_finite:
        return r

    return eval_sum_hyper(f_orig, (i, a, b))
Beispiel #28
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))