(expr, simp, sign, lt, term_list, variables, dep_variables, build_index_expr, index_expr)
return term_list, other_part
Checks for simplifications that can be made, returning a tuple of the
    simplified list of terms and any terms generated by simplification.

    Parameters
    ==========

    expr: expression
        The expression with Wild terms that will be matched to the terms in
        the sum

    simp: expression
        The expression with Wild terms that is substituted in place of the CG
        terms in the case of simplification

    sign: expression
        The expression with Wild terms denoting the sign that is on expr that
        must match

    lt: expression
        The expression with Wild terms that gives the leading term of the
        matched expr

    term_list: list
        A list of all of the terms is the sum to be simplified

    variables: list
        A list of all the variables that appears in expr

    dep_variables: list
        A list of the variables that must match for all the terms in the sum,
        i.e. the dependant variables

    build_index_expr: expression
        Expression with Wild terms giving the number of elements in cg_index

    index_expr: expression
        Expression with Wild terms giving the index terms have when storing
        them to cg_index
Checks for simplifications that can be made, returning a tuple of the
    simplified list of terms and any terms generated by simplification.
Checks for simplifications that can be made returning a tuple of the simplified list of terms and any terms generated by simplification .
def _check_cg_simp(expr, simp, sign, lt, term_list, variables, dep_variables, build_index_expr, index_expr):
    """ Checks for simplifications that can be made, returning a tuple of the
    simplified list of terms and any terms generated by simplification.

    Parameters
    ==========

    expr: expression
        The expression with Wild terms that will be matched to the terms in
        the sum

    simp: expression
        The expression with Wild terms that is substituted in place of the CG
        terms in the case of simplification

    sign: expression
        The expression with Wild terms denoting the sign that is on expr that
        must match

    lt: expression
        The expression with Wild terms that gives the leading term of the
        matched expr

    term_list: list
        A list of all of the terms is the sum to be simplified

    variables: list
        A list of all the variables that appears in expr

    dep_variables: list
        A list of the variables that must match for all the terms in the sum,
        i.e. the dependant variables

    build_index_expr: expression
        Expression with Wild terms giving the number of elements in cg_index

    index_expr: expression
        Expression with Wild terms giving the index terms have when storing
        them to cg_index

    """
    other_part = 0
    i = 0
    while i < len(term_list):
        sub_1 = _check_cg(term_list[i], expr, len(variables))
        if sub_1 is None:
            i += 1
            continue
        if not sympify(build_index_expr.subs(sub_1)).is_number:
            i += 1
            continue
        sub_dep = [(x, sub_1[x]) for x in dep_variables]
        cg_index = [None] * build_index_expr.subs(sub_1)
        for j in range(i, len(term_list)):
            sub_2 = _check_cg(term_list[j], expr.subs(sub_dep), len(variables) - len(dep_variables), sign=(sign.subs(sub_1), sign.subs(sub_dep)))
            if sub_2 is None:
                continue
            if not sympify(index_expr.subs(sub_dep).subs(sub_2)).is_number:
                continue
            cg_index[index_expr.subs(sub_dep).subs(sub_2)] = j, expr.subs(lt, 1).subs(sub_dep).subs(sub_2), lt.subs(sub_2), sign.subs(sub_dep).subs(sub_2)
        if all(i is not None for i in cg_index):
            min_lt = min(*[ abs(term[2]) for term in cg_index ])
            indicies = [ term[0] for term in cg_index]
            indicies.sort()
            indicies.reverse()
            [ term_list.pop(i) for i in indicies ]
            for term in cg_index:
                if abs(term[2]) > min_lt:
                    term_list.append( (term[2] - min_lt*term[3]) * term[1] )
            other_part += min_lt * (sign*simp).subs(sub_1)
        else:
            i += 1
    return term_list, other_part
def _check_cg_simp ( expr , simp , sign , lt , term_list , variables , dep_variables , build_index_expr , index_expr ) : other_part = 0 i = 0 while i < len ( term_list ) : sub_1 = _check_cg ( term_list [ i ] , expr , len ( variables ) ) if sub_1 is None : i += 1 continue if not sympify ( build_index_expr . subs ( sub_1 ) ) . is_number : i += 1 continue sub_dep = [ ( x , sub_1 [ x ] ) for x in dep_variables ] cg_index = [ None ] * build_index_expr . subs ( sub_1 ) for j in range ( i , len ( term_list ) ) : sub_2 = _check_cg ( term_list [ j ] , expr . subs ( sub_dep ) , len ( variables ) - len ( dep_variables ) , sign = ( sign . subs ( sub_1 ) , sign . subs ( sub_dep ) ) ) if sub_2 is None : continue if not sympify ( index_expr . subs ( sub_dep ) . subs ( sub_2 ) ) . is_number : continue cg_index [ index_expr . subs ( sub_dep ) . subs ( sub_2 ) ] = j , expr . subs ( lt , 1 ) . subs ( sub_dep ) . subs ( sub_2 ) , lt . subs ( sub_2 ) , sign . subs ( sub_dep ) . subs ( sub_2 ) if all ( i is not None for i in cg_index ) : min_lt = min ( * [ abs ( term [ 2 ] ) for term in cg_index ] ) indicies = [ term [ 0 ] for term in cg_index ] indicies . sort ( ) indicies . reverse ( ) [ term_list . pop ( i ) for i in indicies ] for term in cg_index : if abs ( term [ 2 ] ) > min_lt : term_list . append ( ( term [ 2 ] - min_lt * term [ 3 ] ) * term [ 1 ] ) other_part += min_lt * ( sign * simp ) . subs ( sub_1 ) else : i += 1 return term_list , other_part