Confused about equality with symbols

23 views
Skip to first unread message

Duane Nykamp

unread,
Mar 26, 2013, 11:42:01 AM3/26/13
to sy...@googlegroups.com
I'm trying to understand how equality works with symbols.  In particular, this behavior confuses me:

In [70]: from sympy.abc import x

In [71]: bool(x != x)
Out[71]: False

In [72]: bool(Not(x==x))
Out[72]: False

In [73]: bool(Not(Eq(x,x)))
Out[73]: True

In [74]: bool(Eq(x,x))
Out[74]: True

Why is Not(Eq(x,x)) different than the other cases and evaluate to True?

I need to use the syntax Not(Eq(x,x)) because I'm parsing the expression from a string and then performing substitutions.  Both x==x and x!=x evaluate to booleans before I get a chance to do the substitutions.

Is this a bug, or is there a logic behind this that I don't understand?

Thanks,
Duane


Aaron Meurer

unread,
Mar 26, 2013, 3:17:51 PM3/26/13
to sy...@googlegroups.com
On Tue, Mar 26, 2013 at 9:42 AM, Duane Nykamp <dqny...@comcast.net> wrote:
> I'm trying to understand how equality works with symbols. In particular,
> this behavior confuses me:
>
> In [70]: from sympy.abc import x
>
> In [71]: bool(x != x)
> Out[71]: False
>
> In [72]: bool(Not(x==x))
> Out[72]: False
>
> In [73]: bool(Not(Eq(x,x)))
> Out[73]: True
>
> In [74]: bool(Eq(x,x))
> Out[74]: True
>
> Why is Not(Eq(x,x)) different than the other cases and evaluate to True?

It gives False for me. What version of SymPy are you using?

Anyway, you might be interested in
https://code.google.com/p/sympy/issues/detail?id=1887. There's
currently no way to make Eq never evaluate, but you could probably
make a simple version that does by subclassing Equality and overriding
eval.

Aaron Meurer

>
> I need to use the syntax Not(Eq(x,x)) because I'm parsing the expression
> from a string and then performing substitutions. Both x==x and x!=x
> evaluate to booleans before I get a chance to do the substitutions.
>
> Is this a bug, or is there a logic behind this that I don't understand?
>
> Thanks,
> Duane
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sympy?hl=en-US.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Duane Nykamp

unread,
Mar 26, 2013, 3:49:17 PM3/26/13
to sy...@googlegroups.com
Great.  I see this is fixed in 0.7.2-git.  (I was running 0.7.2.)  I won't be able to get the git version running on my webserver (as I don't administer that), but I worked around the problem using bool(x-x).  Next time, I'll be sure to check that the behavior remains in the git version.

Using Eq(x,x) seems to work to delay evaluation of equality, as it has different behavior than x==x, which always evaluates right away.  If I understand issue 1887 correctly, once that is fixed, I should be able to use x==x.   That will be much better, as it will be more natural syntax to have folks enter.  (I was quite confused for awhile as x < y didn't evaluate right away but x==y did.  But, eventually, I realized I could use Eq(x,y), .)

Thanks,
Duane

Aaron Meurer

unread,
Mar 26, 2013, 3:55:08 PM3/26/13
to sy...@googlegroups.com
On Tue, Mar 26, 2013 at 1:49 PM, Duane Nykamp <dqny...@comcast.net> wrote:
> Great. I see this is fixed in 0.7.2-git. (I was running 0.7.2.) I won't
> be able to get the git version running on my webserver (as I don't
> administer that), but I worked around the problem using bool(x-x). Next
> time, I'll be sure to check that the behavior remains in the git version.
>
> Using Eq(x,x) seems to work to delay evaluation of equality, as it has
> different behavior than x==x, which always evaluates right away. If I
> understand issue 1887 correctly, once that is fixed, I should be able to use
> x==x. That will be much better, as it will be more natural syntax to have
> folks enter. (I was quite confused for awhile as x < y didn't evaluate
> right away but x==y did. But, eventually, I realized I could use Eq(x,y),
> .)

No, == will always be structural equality. This is not going to change
any time soon, as we use == all over the code base to mean this. And
anyway, structural equality testing is used way more often than
creation of Eq instances.

If you want people to be able to input ==, there is work to make it
work with the parser at https://github.com/sympy/sympy/pull/1541.

Aaron Meurer
Reply all
Reply to author
Forward
0 new messages