Make All Symbols Commutative In A Sympy Expression
Say you have a number of non commutative symbols within a sympy expression, something like a, c = sympy.symbols('a c', commutative=False) b = sympy.Symbol('b') expr = a * c + b * c
Solution 1:
Two comments:
- noncommutatives will factor (though they respect the side that the nc expr appears on)
- although you have a robust answer, a simple answer that will often be good enough is to just sympify the string version of the expression
Both are shown below:
>>>import sympy>>>a, c = sympy.symbols('a c', commutative=False)>>>b = sympy.Symbol('b')>>>expr = a * c + b * c>>>factor(expr)
(b + a)*c
>>>S(str(_))
c*(a + b)
Post a Comment for "Make All Symbols Commutative In A Sympy Expression"