(s)
return result
Main function for Sum simplification
Main function for Sum simplification
Main function for Sum simplification
def sum_simplify(s):
    """Main function for Sum simplification"""
    from sympy.concrete.summations import Sum

    terms = Add.make_args(s)
    s_t = [] # Sum Terms
    o_t = [] # Other Terms

    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:
                # Insert the constant inside the Sum
                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:
                        if isinstance(sum_add(s_term1, s_term2, method), Sum):
                            s_t[i] = sum_add(s_term1, s_term2, method)
                            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
def sum_simplify ( s ) : from sympy . concrete . summations import Sum terms = Add . make_args ( s ) s_t = [ ] # Sum Terms o_t = [ ] # Other Terms 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 : # Insert the constant inside the Sum 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 : if isinstance ( sum_add ( s_term1 , s_term2 , method ) , Sum ) : s_t [ i ] = sum_add ( s_term1 , s_term2 , method ) 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