Skip to content Skip to sidebar Skip to footer

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:

  1. noncommutatives will factor (though they respect the side that the nc expr appears on)
  2. 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"