Repository: sympy/sympy
File: sympy/concrete/summations.py (Python)

Code before:
    if not is_even_function(numer, denom):
        # Try shifting summation and check if the summand can be made
        # even wrt the origin.
        # Sum(f(n), (n, a, b)) => Sum(f(n + s), (n, a - s, b - s))
        shift = get_shift(denom)

        if not shift:
            return None

        if shift.is_Integer:
            numer = numer.shift(shift)
            denom = denom.shift(shift)
            if not is_even_function(numer, denom):
                return None

            if alternating:
                f = S.NegativeOne**i * (S.NegativeOne**shift * numer.as_expr() / denom.as_expr())
            else:
                f = numer.as_expr() / denom.as_expr()
            return eval_sum_residue(f, (i, a-shift, b-shift))
        elif not both_inf:
            return None  # can't make a shift that keeps limits integer

    if both_inf:  # even or not
        poles = get_poles(denom)
        if poles is None:
            return None
        int_roots, nonint_roots = poles
        if int_roots:
            return None

        return -S.Pi * sum(residues(numer, denom, alternating, nonint_roots))

    # handle even function with semi-infinite limits

    if a is S.NegativeInfinity:
        # this is ok for an even function and will be necessary
        # so the extraneous roots can be handled properly below
        a, b = -b, -a

    assert a.is_Integer and b is S.Infinity  # even function with limits (k, oo)

    poles = get_poles(denom)
    if poles is None:
        return None
    int_roots, nonint_roots = poles

    if int_roots:
        int_roots_max = max(int_roots)
        int_roots_min = min(int_roots)
        # Integer valued poles must be next to each other

Review comment (suggestion):
I think that the both_inf case can be handled above before checking `is_even_function` now that the residue calculation is fixed.

Code after:
    both_inf = (a,b) == (S.NegativeInfinity, S.Infinity)

    if not is_even_function(numer, denom):
        # Try shifting summation and check if the summand can be made
        # even wrt the origin.
        # Sum(f(n), (n, a, b)) => Sum(f(n + s), (n, a - s, b - s))
        shift = get_shift(denom)

        if not shift:
            return None

        if shift.is_Integer:
            numer = numer.shift(shift)
            denom = denom.shift(shift)
            if not is_even_function(numer, denom):
                return None

            if alternating:
                f = S.NegativeOne**i * (S.NegativeOne**shift * numer.as_expr() / denom.as_expr())
            else:
                f = numer.as_expr() / denom.as_expr()
            return eval_sum_residue(f, (i, a-shift, b-shift))
        elif not both_inf:
            return None  # can't make a shift that keeps limits integer

    if both_inf:  # even or not
        poles = get_poles(denom)
        if poles is None:
            return None
        int_roots, nonint_roots = poles
        if int_roots:
            return None

        return -S.Pi * sum(residues(numer, denom, alternating, nonint_roots))

    # handle even function with semi-infinite limits

    if a is S.NegativeInfinity:
        # this is ok for an even function and will be necessary
        # so the extraneous roots can be handled properly below
        a, b = -b, -a

    assert a.is_Integer and b is S.Infinity  # even function with limits (k, oo)

    poles = get_poles(denom)
    if poles is None:
        return None
    int_roots, nonint_roots = poles

    if int_roots:
        int_roots_max = max(int_roots)