I'm trying to compare two equations to determine if they're mathematically equivalent using SymPy
. For example, I have the string 2y = x^2 + x
and I want to see if it is mathematically equivalent to y = x^2 + 2x - x - y
(with some basic algebra you can see that they are). This doesn't have to be mathematically rigorous, just something that handles a vast majority of relatively simple cases.
Another constraint I have is that I don't necessarily know the variable names present in the string. It could have x, it could have y, it could have t, etc. etc. since I am creating a front-serving service, and I don't want users to have to worry about these definitions.
I know when comparing expressions, the best-practice way is to use .equals()
or sympify(Ex2 - Ex1)==0
. However, I don't think it is so simple when dealing with equations, since you have to deal with proportionality and other cases (e.g, x=2y is the same as 2x=4y). Another way I saw online to handle comparing expressions is to brute-force a bunch of numbers and see if it works out. How would I go about implementing this for equations? Especially since I don't know what the names of my variables will be? Is there a way to programmatically determine what the variables are?
EDIT FOR REVIEW: My question is unique, because the one that was linked is about evaluating equivalent expressions. This is subtly but significantly different than evaluating equivalent equations in my opinion, and it is not clear from those answers how to solve my problem.