

def sum_simplify(s):
    'Main function for Sum simplification'
    from sympy.concrete.summations import Sum
    terms = Add.make_args(s)
    s_t = []
    o_t = []
    for term in terms:
        if isinstance(term, Mul):
            constant = 1
            other = 1
            s = 0
            n_sum_terms = 0
            for j in range(len(term.args)):
                if isinstance(term.args[j], Sum):
                    s = term.args[j]
                    n_sum_terms = (n_sum_terms + 1)
                elif (term.args[j].is_number == True):
                    constant = (constant * term.args[j])
                else:
                    other = (other * term.args[j])
            if ((other == 1) and (n_sum_terms == 1)):
                s_t.append(Sum((constant * s.function), *s.limits))
            elif ((other != 1) and (n_sum_terms == 1)):
                o_t.append((other * Sum((constant * s.function), *s.limits)))
            else:
                o_t.append(term)
        elif isinstance(term, Sum):
            s_t.append(term)
        else:
            o_t.append(term)
    used = ([False] * len(s_t))
    for method in range(2):
        for (i, s_term1) in enumerate(s_t):
            if (not used[i]):
                for (j, s_term2) in enumerate(s_t):
                    if ((not used[j]) and (i != j)):
                        temp = sum_add(s_term1, s_term2, method)
                        if isinstance(temp, Sum):
                            s_t[i] = temp
                            s_term1 = s_t[i]
                            used[j] = True
    result = Add(*o_t)
    for (i, s_term) in enumerate(s_t):
        if (not used[i]):
            result = Add(result, s_term)
    return result

dataset/ETHPy150Open sympy/sympy/sympy/simplify/simplify.py sum_simplify/original