Re: [sympy] Why doesn't Eq simplify?

27 views
Skip to first unread message

Aaron Meurer

unread,
Aug 27, 2012, 10:07:48 PM8/27/12
to sy...@googlegroups.com
I'm a little unclear on what you're asking, so let me just tell you some things and hopefully it will contain the answer you are looking for.  

a == b does structural comparison only. So x + 1 == x + 1 will give True, but (x +1)*x == x**2 + x will give False. 

To test for mathematical equality, simplify(a - b) is your best bet. If you know something about the structure of a and b, you can use something more directed (and efficient) than simplify(), but in the general case we try to make simplify() all encompassing, so that it performs all possible simplifications known to SymPy. 

Our assumption system is not smart enough at the moment to perform assumptions based comparison, unfortunately, but that's something to think about.  Usually simplify(a - b) tells you if a = b if it reduces to zero, but nothing if it doesn't. But you could potentially do the reverse with something like Symbol("x", positive=True) != Symbol("y", negative=True). I'm not sure what a good interface to that would be, for whenever it is implemented. 

Finally, I want to point out that currently Relational serves a dual role, one of asking about the property and one of assuming it. See http://code.google.com/p/sympy/issues/detail?id=1887

Aaron Meurer

On Aug 27, 2012, at 6:03 PM, "David M. Rogers" <predictiv...@gmail.com> wrote:

I'm wondering how to make the following work:

In: x, y = symbols('x y')
In: Eq(x, y)
Out: Eq(x, y)
In: Eq(x, x)
Out: True

It seems that it just require something like an
if a == b: True else Eq(a,b)

test, perhaps right in the __new__ generator of Relational.  I don't know what the policy is on calling simplify() to test for zero / nonzero difference between the two sides, but if a zero is found, then any of the relations give an immediate result.  There are also some number theoretic results involving valid variable ranges that could immediately simplify symbolic comparisons.

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To view this discussion on the web visit https://groups.google.com/d/msg/sympy/-/Kq24S46MI-gJ.
To post to this group, send email to sy...@googlegroups.com.
To unsubscribe from this group, send email to sympy+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sympy?hl=en.

Chris Smith

unread,
Aug 27, 2012, 10:49:21 PM8/27/12
to sy...@googlegroups.com
On Tue, Aug 28, 2012 at 7:52 AM, Aaron Meurer <asme...@gmail.com> wrote:
> I'm a little unclear on what you're asking, so let me just tell you some
> things and hopefully it will contain the answer you are looking for.

I think what he is pointing out is that even trivial cases involving
symbols are not resolved:


>>> Eq(x,x)
x == x
>>> Eq(1,1)
True

That first case should trivially resolve to True, shouldn't it?

Matthew Rocklin

unread,
Aug 27, 2012, 10:57:50 PM8/27/12
to sy...@googlegroups.com
I remember bringing this up a year ago. Someone pointed me to it having been brought up roughly a year prior. We're right on schedule! 

Delve here

--
You received this message because you are subscribed to the Google Groups "sympy" group.

Aaron Meurer

unread,
Aug 28, 2012, 12:28:53 AM8/28/12
to sy...@googlegroups.com
Ah, yes. And the answer is always the same: "If we're going to fix
it, let's fix it the correct way". Which it's yet to be fixed,
because the correct way is not so simple.

Aaron Meurer

Chris Smith

unread,
Aug 28, 2012, 12:48:44 AM8/28/12
to sy...@googlegroups.com
On Tue, Aug 28, 2012 at 10:13 AM, Aaron Meurer <asme...@gmail.com> wrote:
> Ah, yes. And the answer is always the same: "If we're going to fix
> it, let's fix it the correct way". Which it's yet to be fixed,
> because the correct way is not so simple.

Since Eq(1,1) gives True, I am in favor of an iterim fix that makes
Eq(x,x) give True, too.

David M. Rogers

unread,
Aug 29, 2012, 11:42:09 AM8/29/12
to sy...@googlegroups.com
There's another reference I was looking at here:
https://groups.google.com/d/topic/sympy/xP_uM49pXeo/discussion
where Chris Smith said that 'Equal' stored an unsimplified relation, and the Eq() should simplify.
I tried changing relational.py to always check if (lhs - rhs).equals(0), but ran into some infinite recursions
when testing nontrivial expressions, because equals simplifies its argument, which then calls equals again.

David M. Rogers

unread,
Aug 29, 2012, 2:54:34 PM8/29/12
to sy...@googlegroups.com
This has been implemented in the last 3 commits to:
https://bitbucket.org/frobnitzem/sympy-cg
$ git cherry-pick 894e2579e4a5..48d92278714b
I'll switch to a github repo soon for pulls to work.

It fixes the infinite recursion problems, but leaves an error in test_eval_power.py and test_expand.py related to evalf_log returning
a dictionary containing a substitution object with some (None, None, ...) tuples in there.
evalf_log later calls mpf_cmp with those None objects and it chokes on them.

I don't know the internals of evalf, but this seems like a separate issue.  There may be other issues elsewhere caused by changing the behavior of Eq as well...

~ David.

Aaron Meurer

unread,
Aug 30, 2012, 8:01:42 PM8/30/12
to sy...@googlegroups.com
On Wed, Aug 29, 2012 at 12:54 PM, David M. Rogers
<predictiv...@gmail.com> wrote:
> This has been implemented in the last 3 commits to:
> https://bitbucket.org/frobnitzem/sympy-cg
> $ git cherry-pick 894e2579e4a5..48d92278714b

Cool. I didn't know that you could cherry-pick a range of commits like that.

> I'll switch to a github repo soon for pulls to work.

Yes, you should definitely do that, because we probably won't end up
reviewing anything unless it's submitted as a pull request (it's not
that we don't want to, it's just that it's too much work otherwise).

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/sympy/-/zKMXBWCGEB4J.
Reply all
Reply to author
Forward
0 new messages