0

I wrote the following code:

simplification = parse_expr(str_expression, evaluate=True)
expression = parse_expr(str_expression, evaluate=False)

if expression == simplification:
   msg = "Couldn't simplify!"
else:
   msg = "Simplified:"

I thought that if the expression is the same whether or not it has been evaluated, that must mean that it's already as simplified as it can be. But for some reason, for

str_expression = "s+5"

I get that this expression is false:

expression == simplification

Does anybody know why? How do I fix this?

Thank you in advance.

Phil
  • 23
  • 6

1 Answers1

0

Although the two expressions print the same their internal representations are different as the store the args in a different order. The doit method is usually supposed to undo the effect of evaluate=False:

In [10]: expression
Out[10]: s + 5

In [11]: simplification
Out[11]: s + 5

In [12]: expression.args
Out[12]: (s, 5)

In [13]: simplification.args
Out[13]: (5, s)

In [14]: expression.doit().args
Out[14]: (5, s)

In [15]: expression.doit() == simplification
Out[15]: True
Oscar Benjamin
  • 12,649
  • 1
  • 12
  • 14
  • Thank you very much! This solved my problem. Unfortunately I think it created I new one. before, when I entered "a*a" I got "a^2" as a simplification. Now, it presents "a^2" and writes: "Couldn't simplify!", even though it is simplified. – Phil Mar 28 '21 at 11:12