-
Notifications
You must be signed in to change notification settings - Fork 279
Move same magic methods from Expr and GenExpr to ExprLike
#1204
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
Open
Zeroto521
wants to merge
23
commits into
scipopt:master
Choose a base branch
from
Zeroto521:ExprLike
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+35
−59
Open
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
6874ae4
Consolidate operator overloads into ExprLike
Zeroto521 d8f2cce
Consolidate ExprLike operator methods
Zeroto521 d140fb7
Move __rtruediv__ to ExprLike base class
Zeroto521 6998944
Update changelog: move magic methods to ExprLike
Zeroto521 9d2d251
Update CHANGELOG.md
Zeroto521 043b032
Mark reflected dunder methods positional-only
Zeroto521 11d1801
Consolidate arithmetic dunders into ExprLike
Zeroto521 b814b40
Update CHANGELOG.md
Zeroto521 27ff00f
Merge branch 'ExprLike' of github.com:Zeroto521/PySCIPOpt into ExprLike
Zeroto521 c7f6715
style: format scip.pyi with ruff
Zeroto521 483528a
Merge branch 'scipopt:master' into ExprLike
Zeroto521 d88206c
Merge branch 'master' into ExprLike
Zeroto521 7dbf944
Refine operator type hints in scip.pyi
Zeroto521 8e8bf59
Update CHANGELOG.md
Zeroto521 64e6947
Merge branch 'master' into ExprLike
Joao-Dionisio 6ca96c9
Make ExprLike.__neg__ positional-only
Zeroto521 823b94c
Refine ExprLike operator return types
Zeroto521 f21d708
Merge branch 'ExprLike' of github.com:Zeroto521/PySCIPOpt into ExprLike
Zeroto521 ec64ba2
Annotate __rtruediv__ return type as GenExpr
Zeroto521 9fdca93
Annotate __neg__ return type
Zeroto521 61cea57
Unify __rtruediv__ typing in stubs
Zeroto521 dcd64b9
Merge branch 'master' into ExprLike
Zeroto521 aa6ae86
Mark __neg__ as positional-only in scip.pyi
Zeroto521 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -257,6 +257,27 @@ cdef class ExprLike: | |
|
|
||
| return NotImplemented | ||
|
|
||
| def __radd__(self, other, /): | ||
| return self + other | ||
|
|
||
| def __sub__(self, other, /): | ||
| return self + (-other) | ||
|
|
||
| def __rsub__(self, other, /): | ||
| return (-self) + other | ||
|
|
||
| def __rmul__(self, other, /): | ||
| return self * other | ||
|
|
||
| def __rtruediv__(self, other, /) -> GenExpr: | ||
| return buildGenExprObj(other) / self | ||
|
|
||
| def __richcmp__(self, other, int op): | ||
| return _expr_richcmp(self, other, op) | ||
|
|
||
| def __neg__(self, /) -> Union[Expr, GenExpr]: | ||
| return self * -1.0 | ||
|
|
||
| def __abs__(self) -> GenExpr: | ||
| return UnaryExpr(Operator.fabs, buildGenExprObj(self)) | ||
|
|
||
|
|
@@ -358,11 +379,10 @@ cdef class Expr(ExprLike): | |
| return 1.0 / other * self | ||
| return buildGenExprObj(self) / other | ||
|
|
||
| def __rtruediv__(self, other): | ||
| ''' other / self ''' | ||
| def __rtruediv__(self, other, /) -> GenExpr: | ||
| if not _is_expr_compatible(other): | ||
| return NotImplemented | ||
| return buildGenExprObj(other) / self | ||
| return super().__rtruediv__(other) | ||
|
|
||
| def __pow__(self, other, modulo): | ||
| if float(other).is_integer() and other >= 0: | ||
|
|
@@ -387,25 +407,6 @@ cdef class Expr(ExprLike): | |
| raise ValueError("Base of a**x must be positive, as expression is reformulated to scip.exp(x * scip.log(a)); got %g" % base) | ||
| return (self * Constant(base).log()).exp() | ||
|
|
||
| def __neg__(self): | ||
| return Expr({v:-c for v,c in self.terms.items()}) | ||
|
|
||
| def __sub__(self, other): | ||
| return self + (-other) | ||
|
|
||
| def __radd__(self, other): | ||
| return self.__add__(other) | ||
|
|
||
| def __rmul__(self, other): | ||
| return self.__mul__(other) | ||
|
|
||
| def __rsub__(self, other): | ||
| return -1.0 * self + other | ||
|
|
||
| def __richcmp__(self, other, int op): | ||
| '''turn it into a constraint''' | ||
| return _expr_richcmp(self, other, op) | ||
|
|
||
| def normalize(self): | ||
| '''remove terms with coefficient of 0''' | ||
| self.terms = {t:c for (t,c) in self.terms.items() if c != 0.0} | ||
|
|
@@ -464,7 +465,6 @@ cdef class ExprCons: | |
| if not self._rhs is None: | ||
| self._rhs -= c | ||
|
|
||
|
|
||
| def __richcmp__(self, other, op): | ||
| '''turn it into a constraint''' | ||
| if not _is_number(other): | ||
|
|
@@ -690,30 +690,10 @@ cdef class GenExpr(ExprLike): | |
| raise ZeroDivisionError("cannot divide by 0") | ||
| return self * divisor**(-1) | ||
|
|
||
| def __rtruediv__(self, other): | ||
| ''' other / self ''' | ||
| def __rtruediv__(self, other, /) -> GenExpr: | ||
| if not _is_genexpr_compatible(other): | ||
| return NotImplemented | ||
| return buildGenExprObj(other) / self | ||
|
|
||
| def __neg__(self): | ||
| return -1.0 * self | ||
|
Comment on lines
-699
to
-700
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be a bit faster now.
|
||
|
|
||
| def __sub__(self, other): | ||
| return self + (-other) | ||
|
|
||
| def __radd__(self, other): | ||
| return self.__add__(other) | ||
|
|
||
| def __rmul__(self, other): | ||
| return self.__mul__(other) | ||
|
|
||
| def __rsub__(self, other): | ||
| return -1.0 * self + other | ||
|
|
||
| def __richcmp__(self, other, int op): | ||
| '''turn it into a constraint''' | ||
| return _expr_richcmp(self, other, op) | ||
| return super().__rtruediv__(other) | ||
|
|
||
| def degree(self): | ||
| '''Note: none of these expressions should be polynomial''' | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
This benefits from #1185:
__neg__forExprvia Python list comprehension: 4.7777s on the following example__neg__forExprvia C-API: 3.8283s on the same example