From the FAQ :
Why does SymPy say that two equal expressions are not equal?
The equality operator (==) checks to see if the expressions are identical in shape and not mathematically equivalent.
To make equality testing useful in basic cases, SymPy tries to rewrite mathematically equivalent expressions in canonical form when evaluating them. For example, SymPy evaluates both x + x and - (- 2 * x) to 2 * x, and x * x - x ** 2.
The simplest example, when the default transformations cannot generate the canonical form, are nonlinear polynomials, which can be represented in both factorized and expanded form. Although mathematically mathematically a(1+b) = a+ab , SymPy gives:
>>> bool(a*(1+b) == a + a*b) False
Similarly, SymPy cannot detect that the difference is zero:
>>> bool(a*(1+b) - (a+a*b) == 0) False
If you want to determine the mathematical equivalence of nontrivial expressions, you must apply a more advanced simplification procedure to both sides of the equation. In the case of polynomials, expressions can be rewritten in canonical form, completely decomposing them. This is done using the .expand() method in SymPy:
>>> A, B = a*(1+b), a + a*b >>> bool(A.expand() == B.expand()) True >>> (A - B).expand() 0
If .expand() does not help, try simplify() , trigsimp() , etc., which try to perform more complex conversions. For instance,
>>> trigsimp(cos(x)**2 + sin(x)**2) == 1 True