(expr)
If a sympy 'Expr' is of the form:

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

    where all the a_j are noncommuting symbols in basis then

    proj(expr) returns the sum of those terms where a_j is in plist
If a sympy 'Expr' is of the form:
If a sympy Expr is of the form :
def non_scalar_projection(expr):
    """
    If a sympy 'Expr' is of the form:

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

    where all the a_j are noncommuting symbols in basis then

    proj(expr) returns the sum of those terms where a_j is in plist
    """
    if expr.is_commutative:  # return scalar projection
        return S.Zero
    expr = expand(expr)
    if isinstance(expr, Mul):  # expr has single term
        (coefs, bases) = expr.args_cnc()
        if bases[0] != ONE_NC:  # vector term to be projected
            return Mul(*coefs) * bases[0]
        else:
            return S.Zero
    elif isinstance(expr, Symbol):  # base vector to be projected
        if expr != ONE_NC:
            return expr
        else:
            return S.Zero
    elif isinstance(expr, Add):  # expr has multiple terms
        result = S.Zero
        for arg in expr.args:
            term = arg.args_cnc()
            if term[1] != ONE_NC:  # vector term to be projected
                    result += Mul(*term[0]) * term[1][0]
        return result
def non_scalar_projection ( expr ) : if expr . is_commutative : # return scalar projection return S . Zero expr = expand ( expr ) if isinstance ( expr , Mul ) : # expr has single term ( coefs , bases ) = expr . args_cnc ( ) if bases [ 0 ] != ONE_NC : # vector term to be projected return Mul ( * coefs ) * bases [ 0 ] else : return S . Zero elif isinstance ( expr , Symbol ) : # base vector to be projected if expr != ONE_NC : return expr else : return S . Zero elif isinstance ( expr , Add ) : # expr has multiple terms result = S . Zero for arg in expr . args : term = arg . args_cnc ( ) if term [ 1 ] != ONE_NC : # vector term to be projected result += Mul ( * term [ 0 ] ) * term [ 1 ] [ 0 ] return result