简体   繁体   中英

python sympy simplify and Eq

This may not be a question, just an observation, but is sympy supposed to work this way.

I have a two complicated expression, A and E, and I am trying to find out if they are equivalent. If I simplify one, say E, and use Eq(A,E) it does not return True, but the two separated with "==". If would have expected that sympy would be smart enough to work out they are equal. Eq(simplify(A),E) returns True. Here is the code ...

from sympy import *

B = symbols('B')
C = symbols('C')
F = symbols('F')
G = symbols('G')
H = symbols('H')

A = (B - C)*(G*(B + C) - (B  - C - F)*H)**2
D = 2*(B**2+B*F-C**2)**2
E = A/D

ED=simplify(E*D)
print("E*D= {0}").format(str(ED))
print("A  = {0}").format(str(A))
print("0  = {0}").format(str(simplify(A-ED)))
print("T  = {0}").format(Eq(A,ED))
print("T  = {0}").format(Eq(simplify(A),ED))

and the output

E*D= (B - C)*(G*(B + C) + H*(-B + C + F))**2
A  = (B - C)*(G*(B + C) - H*(B - C - F))**2
0  = 0
T  = (B - C)*(G*(B + C) - H*(B - C - F))**2 == (B - C)*(G*(B + C) + H*(-B + C + F))**2
T  = True

Note the -H versus +H in the last expression.

Equality does not do any simplification and two objects are identical only if they are structurally (not mathematically) zero. Proving mathematical equality (in general) is not a simple problem so if they aren't identical (as in this case) SymPy doesn't even begin chasing the "equality rabbit" to its hole :-). This is the expected behavior. If you want to let SymPy try some simplification on its own, try using the equals method:

>>> A.equals(simplify(E*D))
True

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM