Skip to content

Commit

Permalink
Trac #34341: Fix bool(expr1 != expr2) for nontrivially equal expres…
Browse files Browse the repository at this point in the history
…sions

As reported on [https://groups.google.com/g/sage-devel/c/rXZeGZjAIdU
sage-devel]:

{{{
sage: a = x^2 + 2*x + 1
sage: b = (x + 1)^2
sage: c = a != b
sage: bool(c)
True
}}}

Note that

{{{
sage: bool(c.expand())
False
}}}

This is due to calling `is_trivial_zero` instead
of `is_zero` in the `__bool__` method for symbolic expressions.

The fix provided by this ticket is not intended to fix other existing
`bool(expr1 != expr2)` issues (e.g. #33698).

URL: https://trac.sagemath.org/34341
Reported by: tmonteil
Ticket author(s): Thierry Monteil
Reviewer(s): Samuel Lelièvre
  • Loading branch information
Release Manager committed Aug 29, 2022
2 parents 5114e87 + 5ca40a8 commit 420bbe2
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/sage/symbolic/expression.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3456,6 +3456,11 @@ cdef class Expression(Expression_abc):
sage: val = pi - 2286635172367940241408/1029347477390786609545*sqrt(2)
sage: bool(val>0)
False
Check that :trac:`34341` is fixed::
sage: bool(x^2 + 2*x + 1 != (x + 1)^2)
False
"""
if self.is_relational():
# constants are wrappers around Sage objects, compare directly
Expand Down Expand Up @@ -3499,8 +3504,8 @@ cdef class Expression(Expression_abc):

# Use interval fields to try and falsify the relation
if not need_assumptions:
if pynac_result == relational_notimplemented and self.operator()==operator.ne:
return not (self.lhs()-self.rhs()).is_trivial_zero()
if pynac_result == relational_notimplemented and self.operator() == operator.ne:
return not (self.lhs()-self.rhs()).is_zero()
res = self.test_relation()
if res in (True, False):
return res
Expand Down

0 comments on commit 420bbe2

Please sign in to comment.