what does Equal and == do in sympy

679 views
Skip to first unread message

Robert

unread,
Apr 27, 2012, 6:16:17 PM4/27/12
to sympy
Hello,

I have a question about == and Equal in sympy.

Their behavior is strange to me.

In [108]: expr = sympy.simplify('Equal(A,B)')
In [111]: expr.subs(dict(A=1, B=1))
Equal(1, 1)

In [112]: expr = sympy.simplify('A==B')
In [113]: expr
False

In [115]: A = sympy.Symbol('A')
In [116]: B = sympy.Symbol('B')

In [117]: A==B
False

In [118]: sympy.__version__
'0.7.1'

Aaron Meurer

unread,
Apr 27, 2012, 6:29:53 PM4/27/12
to sy...@googlegroups.com
Equal is not anything. You've just created an undefined function
called Equal (Function('Equal')) with that sympify command.

What you are probably looking for is Eq() (or Equality()). This
creates a symbolic equality. You can manipulate different sides of
the equation using Eq.lhs and Eq.rhs, like

In [496]: Eq(x, y)
Out[496]: x = y

In [497]: Eq(x, y).lhs
Out[497]: x

In [498]: Eq(x, y).rhs
Out[498]: y

Eq() will reduce to True when or False when it can see that the
expressions are always equal or unequal:

In [504]: Eq(x, y).subs({x: 1, y: 1})
Out[504]: True

In [505]: Eq(x, y).subs({x: 1, y: 2})
Out[505]: False

== is a lot different. This is used to exactly compare expressions.
a == b will be a boolean, True if a and be are exactly equal and False
otherwise. I say "exactly" equal because == does structural
comparison, not mathematical comparison. So we have

In [499]: x*(y + z) == x*y + x*z
Out[499]: False

In [500]: x*(y + z) == x*(y + z)
Out[500]: True

If you want to do a mathematical comparison, the best way is to
subtract one expression from the other and pass it to simplify(), and
see if it goes to 0. For example:

In [501]: a = x*(y + z)

In [502]: b = x*y + x*z

In [503]: simplify(a - b)
Out[503]: 0

See http://docs.sympy.org/dev/gotchas.html#equals-signs for more
discussion on this.

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> 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,
Apr 27, 2012, 8:27:14 PM4/27/12
to sy...@googlegroups.com
> If you want to do a mathematical comparison, the best way is to
> subtract one expression from the other and pass it to simplify(), and
> see if it goes to 0.  For example:
>
You can also use a.equals(b) which, after simplifications, use
numerical methods. At worst, it will take some time to return None
(instead of T/F) for an expression that *is* zero but won't simplify
to zero.

Robert

unread,
Apr 28, 2012, 1:41:09 PM4/28/12
to sympy
Hello,

I have a question about == and Equal in sympy.

Their behavior is strange to me.

In [108]: expr = sympy.simplify('Equal(A,B)')
In [111]: expr.subs(dict(A=1, B=1))
Equal(1, 1)

In [112]: expr = sympy.simplify('A==B')
In [113]: expr
False

In [115]: A = sympy.Symbol('A')
In [116]: B = sympy.Symbol('B')

In [117]: A==B
False

In [118]: sympy.__version__
'0.7.1'

Any help would be appreciated,
Thanks,
Rob

Robert

unread,
May 29, 2015, 5:31:59 AM5/29/15
to sy...@googlegroups.com
Hello,

Thanks for the help previously. I've run into a different issue with regards to Eq.

Sympify will automatically convert Eq into ==
>>> sympy.sympify('Eq(a, b)')
a == b

which if you sympify again will have the behavior you described originally.
>>> sympy.sympify(str(sympy.sympify('Eq(a, b)')))
False

Do you know of any way to stop the converion of Eq to ==?

Thanks,
Rob

Robert

unread,
May 29, 2015, 6:21:33 AM5/29/15
to sy...@googlegroups.com
I was able to write a LambdaPrinter that does what I want for the time being:


class NonRelationalOpPrinter(lambdarepr.LambdaPrinter):

    '''
    >>> from structures import symbolic
    >>> for op in ['Eq', 'Ne', 'Gt', 'Lt', 'Ge', 'Le']:
    ...    sympy.sympify((symbolic.NonRelationalOpPrinter().doprint(sympy.sympify('%s(%s(a, b), c)' % (op, op)))))
    (a == b) == c
    (a != b) != c
    (a > b) > c
    (a < b) < c
    (a >= b) >= c
    (a <= b) <= c
    '''

    def _printRelation(self, expr, name):
        return '%s(%s)' % (name, ", ".join(map(self._print, expr.args)))

    def _print_Equality(self, expr):
        return self._printRelation(expr, 'Equality')

    def _print_Unequality(self, expr):
        return self._printRelation(expr, 'Unequality')

    def _print_GreaterThan(self, expr):
        return self._printRelation(expr, 'GreaterThan')

    def _print_LessThan(self, expr):
        return self._printRelation(expr, 'LessThan')

    def _print_StrictGreaterThan(self, expr):
        return self._printRelation(expr, 'StrictGreaterThan')

    def _print_StrictLessThan(self, expr):
        return self._printRelation(expr, 'StrictLessThan')

Aaron Meurer

unread,
Oct 21, 2015, 1:45:17 PM10/21/15
to sy...@googlegroups.com
In the next version of SymPy str(Eq(x, y)) will give 'Eq(x, y)'.

Aaron Meurer
> 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.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/db78709d-2af8-426a-968a-20434f211058%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages