(f, n)
r"""
    Compute Gosper's hypergeometric term for ``f``.

    Suppose ``f`` is a hypergeometric term such that:

    .. math::
        s_n = \sum_{k=0}^{n-1} f_k

    and `f_k` doesn't depend on `n`. Returns a hypergeometric
    term `g_n` such that `g_{n+1} - g_n = f_n`.

    Examples
    ========

    >>> from sympy.concrete.gosper import gosper_term
    >>> from sympy.functions import factorial
    >>> from sympy.abc import n

    >>> gosper_term((4*n + 1)*factorial(n)/factorial(2*n + 1), n)
    (-n - 1/2)/(n + 1/4)
r"""
    Compute Gosper's hypergeometric term for ``f``.
r Compute Gosper s hypergeometric term for f .
def gosper_term(f, n):
    r"""
    Compute Gosper's hypergeometric term for ``f``.

    Suppose ``f`` is a hypergeometric term such that:

    .. math::
        s_n = \sum_{k=0}^{n-1} f_k

    and `f_k` doesn't depend on `n`. Returns a hypergeometric
    term `g_n` such that `g_{n+1} - g_n = f_n`.

    Examples
    ========

    >>> from sympy.concrete.gosper import gosper_term
    >>> from sympy.functions import factorial
    >>> from sympy.abc import n

    >>> gosper_term((4*n + 1)*factorial(n)/factorial(2*n + 1), n)
    (-n - 1/2)/(n + 1/4)

    """
    r = hypersimp(f, n)

    if r is None:
        return None    # 'f' is *not* a hypergeometric term

    p, q = r.as_numer_denom()

    A, B, C = gosper_normal(p, q, n)
    B = B.shift(-1)

    N = S(A.degree())
    M = S(B.degree())
    K = S(C.degree())

    if (N != M) or (A.LC() != B.LC()):
        D = set([K - max(N, M)])
    elif not N:
        D = set([K - N + 1, S(0)])
    else:
        D = set([K - N + 1, (B.nth(N - 1) - A.nth(N - 1))/A.LC()])

    for d in set(D):
        if not d.is_Integer or d < 0:
            D.remove(d)

    if not D:
        return None    # 'f(n)' is *not* Gosper-summable

    d = max(D)

    coeffs = symbols('c:%s' % (d + 1), cls=Dummy)
    domain = A.get_domain().inject(*coeffs)

    x = Poly(coeffs, n, domain=domain)
    H = A*x.shift(1) - B*x - C

    solution = solve(H.coeffs(), coeffs)

    if solution is None:
        return None    # 'f(n)' is *not* Gosper-summable

    x = x.as_expr().subs(solution)

    for coeff in coeffs:
        if coeff not in solution:
            x = x.subs(coeff, 0)

    if x is S.Zero:
        return None    # 'f(n)' is *not* Gosper-summable
    else:
        return B.as_expr()*x/C.as_expr()
def gosper_term ( f , n ) : r = hypersimp ( f , n ) if r is None : return None # 'f' is *not* a hypergeometric term p , q = r . as_numer_denom ( ) A , B , C = gosper_normal ( p , q , n ) B = B . shift ( - 1 ) N = S ( A . degree ( ) ) M = S ( B . degree ( ) ) K = S ( C . degree ( ) ) if ( N != M ) or ( A . LC ( ) != B . LC ( ) ) : D = set ( [ K - max ( N , M ) ] ) elif not N : D = set ( [ K - N + 1 , S ( 0 ) ] ) else : D = set ( [ K - N + 1 , ( B . nth ( N - 1 ) - A . nth ( N - 1 ) ) / A . LC ( ) ] ) for d in set ( D ) : if not d . is_Integer or d < 0 : D . remove ( d ) if not D : return None # 'f(n)' is *not* Gosper-summable d = max ( D ) coeffs = symbols ( 'c:%s' % ( d + 1 ) , cls = Dummy ) domain = A . get_domain ( ) . inject ( * coeffs ) x = Poly ( coeffs , n , domain = domain ) H = A * x . shift ( 1 ) - B * x - C solution = solve ( H . coeffs ( ) , coeffs ) if solution is None : return None # 'f(n)' is *not* Gosper-summable x = x . as_expr ( ) . subs ( solution ) for coeff in coeffs : if coeff not in solution : x = x . subs ( coeff , 0 ) if x is S . Zero : return None # 'f(n)' is *not* Gosper-summable else : return B . as_expr ( ) * x / C . as_expr ( )