Reputation: 363
I understand that in Sympy, the == equivalence does not work for symbolic expressions and hence cannot be used to check for symbolic equivalence. From the documentations, it is recommended to use simplify (a-b)
and check if the result is 0. E.g.,
>>> simplify((x + 1)**2 - (x**2 + 2*x + 1))
However, this does not seem to work for expressions with equals in them. E.g., I want to compare (2x=6 and x=3), which should be equal.
>>> a = Eq(2*x,6)
>>> b = Eq(x,3)
>>> simplify(a-b)
−x=3+(2x=6)
Or more complex equations which should be equivalent
>>> a = Eq(x*(y+1),6)
>>> b = Eq(2*x*y + 2*x, 12)
>>> simplify(a-b)
(x(y+1)=6)−(2x(y+1)=12)
Wondering if there is a good way or trick to do this in Sympy.
Thanks!
Upvotes: 2
Views: 2638
Reputation: 468
Each expression has two useful methods equals()
and compare()
:
x,y = symbols('x,y')
a = (9*x+30*x+21) / 3
b = x*(3+10)+7
a.equals(b)
Output:
True
Upvotes: 0
Reputation: 3491
What makes these equations equivalent is that they have the same solution set or that they are the same when solved for x
. Have sympy solve them and compare the solutions:
from sympy import *
x, y = symbols('x y')
a = Eq(2*x,6)
b = Eq(x,3)
print(solve(a) == solve(b)) #True
The same works for your more complex example:
a = Eq(x*(y+1),6)
b = Eq(2*x*y + 2*x, 12)
print(solve(a)) # [{x: 6/(y + 1)}]
print(solve(b)) # [{x: 6/(y + 1)}]
print(solve(a) == solve(b)) # True
Upvotes: 2