-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Better inequalities with integer variables #23771
base: master
Are you sure you want to change the base?
Conversation
✅ Hi, I am the SymPy bot (v167). I'm here to help you write a release notes entry. Please read the guide on how to write release notes. Your release notes are in good order. Here is what the release notes will look like: This will be added to https://github.com/sympy/sympy/wiki/Release-Notes-for-1.11. Click here to see the pull request description that was parsed.
|
3c5f97f
to
ce083bf
Compare
This needs more thinking. Currently:
gives
Which breaks a test. |
Could you do the following with Or having a mix of equalities and inequalities?
|
r = self.reversed._eval_simplify(**kwargs) | ||
r = r.canonical | ||
measure = kwargs['measure'] | ||
if measure(r) < kwargs['ratio'] * measure(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't the recursive _eval_simplify
already do this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I do not include this, the last test here will fail:
sympy/sympy/core/tests/test_relational.py
Lines 732 to 736 in 68bd362
# canonical operations are not the same as simplification, | |
# so if there is no simplification, canonicalization will | |
# be done unless the measure forbids it | |
assert simplify(r) == r.canonical | |
assert simplify(r, ratio=0) != r.canonical |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But I agree that it is a bit of a mess to include it everywhere...
@@ -1130,6 +1130,34 @@ def _eval_fuzzy_relation(cls, lhs, rhs): | |||
def strict(self): | |||
return Gt(*self.args) | |||
|
|||
def _eval_simplify(self, **kwargs): | |||
# standard simplify | |||
eorg = e = super()._eval_simplify(**kwargs) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this really necessary? What is the superclass _eval_simplify
in this case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went with the similar approach as in Equality
. I'd expect Relational
to be the superclass.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't notice that method existed. Any reason this logic shouldn't go there?
lhs_is_int = e.lhs.is_integer | ||
rhs_is_int = e.rhs.is_integer | ||
r = e | ||
if lhs_is_int is True: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are all the cases here represented in the tests?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think so.
if lhs_is_int is True: | ||
if rhs_is_int is True: | ||
if e.lhs.is_number: | ||
r = Ge(e.lhs - 1, e.rhs) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it's subjective, but I wonder if it would be better to put use e.rhs <= e.lhs - 1
so that the number is on the right-hand side.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason is that this is a Ge
to start with, so returns it like that. Then, there is a canonical call anyway further down.
This seems to have let to a test failure
|
I'm not a fan of the |
Indeed. I see this primarily as "bounds tightening", which can be beneficial for a more general method as well. |
References to other Issues or PRs
Inspired by #22066 (comment)
Brief description of what is fixed or changed
Inequalities where at least one side is integer are now simplified in a better way. E.g. x < pi is turned into x <= 3.
Other comments
Release Notes