Jack Clarke
Jack Clarke

Reputation: 1

A statement in sympy is returning false when it shouldn't be

For context: I'm using sympy in python 2.7. Part of my project involves simplifying a mathematical expression, but I ran into a problem when using sympy:

from sympy import *
x = symbols ("x")
(-x*exp(-x) + exp(-x)) == (1-x)*(exp(-x))

the code above returns me

False

Both my own maths and wolframalpha disagree with this - did I type something wrong or is this some shortcoming of sympy that I'm not yet aware of?

Upvotes: 0

Views: 933

Answers (2)

Francesco Bonazzi
Francesco Bonazzi

Reputation: 1957

If you want to create a symbolic equality, use Eq:

In [1]: Eq((-x*exp(-x) + exp(-x)), (1-x)*(exp(-x)))
Out[1]: 
     -x    -x             -x
- x⋅ℯ   + ℯ   = (-x + 1)⋅ℯ  

The == operator has to immediately return a boolean expression, this is a Python standard. Therefore the == operator matches the rude structure of the expressions without performing any mathematical transformation (apart from previous minor automatic transformations happening at the expression construction).

Upvotes: 0

dsm
dsm

Reputation: 2253

From the docs page: http://docs.sympy.org/dev/gotchas.html

If you want to test for symbolic equality, one way is to subtract one expression from the other and run it through functions likeexpand(), simplify(), and trigsimp() and see if the equation reduces to 0.

Upvotes: 1

Related Questions