(expr)
return coefs, bases
If a sympy 'Expr' is of the form:

    expr = expr_0 + expr_1*a_1 + ... + expr_n*a_n

    where all the a_j are noncommuting symbols in basis then

    (expr_0, ..., expr_n) and (1, a_1, ..., a_n) are returned.  Note that
    expr_j*a_j does not have to be of that form, but rather can be any
    Mul with a_j as a factor (it doen not have to be a postmultiplier).
    expr_0 is the scalar part of the expression.
If a sympy 'Expr' is of the form:
If a sympy Expr is of the form :
def linear_expand(expr):
    """
    If a sympy 'Expr' is of the form:

    expr = expr_0 + expr_1*a_1 + ... + expr_n*a_n

    where all the a_j are noncommuting symbols in basis then

    (expr_0, ..., expr_n) and (1, a_1, ..., a_n) are returned.  Note that
    expr_j*a_j does not have to be of that form, but rather can be any
    Mul with a_j as a factor (it doen not have to be a postmultiplier).
    expr_0 is the scalar part of the expression.
    """
    expr = expand(expr)
    if expr.is_commutative:  # commutative expr only contains expr_0
        return (expr, ), (S.One, )

    if isinstance(expr, Mul):  # expr only contains one term
        (coefs, bases) = expr.args_cnc()
        coefs = Mul(*coefs)
        bases = bases[0]
    elif isinstance(expr, Symbol):  # term is Symbol
        coefs = S.One
        bases = expr
    elif isinstance(expr, Add):  # expr has multiple terms
        coefs = []
        bases = []
        for arg in expr.args:
            term = arg.args_cnc()
            coef = Mul(*term[0])
            base = term[1][0]
            if base in bases:  # increment coefficient of base
                ibase = list(bases).index(base)  # Python 2.5
                coefs[ibase] += coef
            else:  # add base to list
                coefs.append(coef)
                bases.append(base)
    else:
        raise NotImplementedError("linear_expand for type %s" % type(expr))


    if not isinstance(coefs, list):  # convert single coef to list
        coefs = [coefs]
    if not isinstance(bases, list):  # convert single base to list
        bases = [bases]
    coefs = tuple(coefs)
    bases = tuple(bases)
    return coefs, bases
def linear_expand ( expr ) : expr = expand ( expr ) if expr . is_commutative : # commutative expr only contains expr_0 return ( expr , ) , ( S . One , ) if isinstance ( expr , Mul ) : # expr only contains one term ( coefs , bases ) = expr . args_cnc ( ) coefs = Mul ( * coefs ) bases = bases [ 0 ] elif isinstance ( expr , Symbol ) : # term is Symbol coefs = S . One bases = expr elif isinstance ( expr , Add ) : # expr has multiple terms coefs = [ ] bases = [ ] for arg in expr . args : term = arg . args_cnc ( ) coef = Mul ( * term [ 0 ] ) base = term [ 1 ] [ 0 ] if base in bases : # increment coefficient of base ibase = list ( bases ) . index ( base ) # Python 2.5 coefs [ ibase ] += coef else : # add base to list coefs . append ( coef ) bases . append ( base ) else : raise NotImplementedError ( "linear_expand for type %s" % type ( expr ) ) if not isinstance ( coefs , list ) : # convert single coef to list coefs = [ coefs ] if not isinstance ( bases , list ) : # convert single base to list bases = [ bases ] coefs = tuple ( coefs ) bases = tuple ( bases ) return coefs , bases