sympy.Eq, equality versus is

126 views
Skip to first unread message

Paul Royik

unread,
Mar 24, 2015, 7:28:35 AM3/24/15
to sy...@googlegroups.com
Maybe this is property of Python, but I'm not aware about it.

expr = 1/(2*sin(x/2 + pi/4)*cos(x/2 + pi/4))
equation = Eq(expr,expr)
equation == True # outputs True
equation is True # outputs False

Why True equation is not True?

Moreover, in IDE in which I'm working, when I write equation != True, it tells me "this inspection detects equality comparison with a boolean literal".

Thnak you.

Aaditya Nair

unread,
Mar 24, 2015, 7:40:59 AM3/24/15
to sy...@googlegroups.com
equation == True # outputs True
equation is True # outputs False

Why True equation is not True?

The `Eq` function doesn't actually return `True` but returns `S.true` whose value is equal to True.
Hence `is`, which does an object-comparison which results `False` as `True` and `S.true` are separate objects.
But since values of `True` and `S.true` are same, use of `==` operator results `True`.

It is recommended that you use `==` for testing Boolean Equality.

Aaditya M Nair

Joachim Durchholz

unread,
Mar 24, 2015, 8:15:31 AM3/24/15
to sy...@googlegroups.com
Am 24.03.2015 um 12:28 schrieb Paul Royik:
> Maybe this is property of Python, but I'm not aware about it.
>
> expr = 1/(2*sin(x/2 + pi/4)*cos(x/2 + pi/4))
> equation = Eq(expr,expr)
> equation == True # outputs True
> equation is True # outputs False
>
> Why True equation is not True?

In Python, "==" is value equality while "is" is object identity.

The differences are relevant in two ways:
1) "==" may change if you modify an object, "is" never does. This is
less relevant for SymPy because SymPy objects usually are not modified.
2) "is" cannot be redefined, "==" can. SymPy's S.true has its "=="
redefined so that it compares equal with "==".

> Moreover, in IDE in which I'm working, when I write equation != True, it
> tells me "this inspection detects equality comparison with a boolean
> literal".

It's telling you that writing
xxx == True
is silly because you can simply write
xxx

Paul Royik

unread,
Mar 24, 2015, 8:29:14 AM3/24/15
to sy...@googlegroups.com
Thank you.
I've got it now.
Reply all
Reply to author
Forward
0 new messages