I would like to be able to simplify mathematical expressions from a string in Python. There are several "commutative" ways of doing it. Is there a non-commutative function for that?
I know that sympify from sympy can do some non-commutative jobs, here you have an example:
from sympy import *
x=Symbol('x',commutative=False)
y=Symbol('y',commutative=False)
print sympify(3*x*y - y*x - 2*x*y)
it will print xy -yx, however if we apply sympify to the string, that is,
print sympify('3*x*y - y*x - 2*x*y')
The result is 0.
Is there a way of simplifying the above string to preserve non-commutativity of x and y?
I found that someone has already asked about it here http://osdir.com/ml/python-sympy/2012-02/msg00250.html and someone has answered http://osdir.com/ml/python-sympy/2012-02/msg00255.html, however the solution seems not to work in general.
I preferred to ask first, if there is no immediate solution I guess that I will have to code it myself.
You still need to tell Sympy that there are constraints on the symbols x and y. To do this, still create
Symbol
instances for them, and then just pass those parameters in aslocals
tosympify
:To do it programmatically, SymPy provides some nice parsing tools for extracting symbols from a string expression. The key idea is that you have to suppress evaluation since normal evaluation will make commutativity assumptions that ruin your ability to extract what you need:
It does not appear that there is a direct way to mutate the assumption state of an already-created
Symbol
, which is unfortunate. But you can iterate through these symbols, and make a new collection of symbols with the same names and the non-commutative assumption, and use that forlocals
insympify
.Which gives, e.g.: