-
Notifications
You must be signed in to change notification settings - Fork 271
BUG: MatrixExpr can't be compared with Expr #1069
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
Conversation
Updated type hints and isinstance checks in MatrixExpr comparison methods to use Expr instead of Variable. This change improves compatibility with broader expression types in matrix operations.
Updated type hints and isinstance checks in MatrixExprCons.__le__ and __ge__ methods to use Expr instead of Variable. This change improves consistency with the expected types for matrix expression constraints.
Introduces test_ranged_matrix_cons to verify correct behavior when adding a ranged matrix constraint to the model. Ensures that the matrix variable x is set to ones as expected.
Introduced a shared _matrixexpr_richcmp helper to handle rich comparison logic for MatrixExpr and MatrixExprCons, reducing code duplication and improving maintainability. Updated __le__, __ge__, and __eq__ methods to use this helper, and removed redundant code.
The __eq__ method of MatrixExprCons now raises NotImplementedError with a descriptive message instead of TypeError, clarifying that '==' comparison is not supported.
Added tests for '<=', '>=', and '==' operators in matrix constraints. Verified correct exception is raised for unsupported '==' operator.
Relocated the _is_number utility from expr.pxi to matrix.pxi for better modularity. Updated _matrixexpr_richcmp to use a local _richcmp helper for comparison operations.
Replaces usage of undefined 'shape' variable with 'self.shape' when creating the result array in _matrixexpr_richcmp, ensuring correct array dimensions.
Removed unnecessary double .all() calls in assertions for matrix variable tests, simplifying the checks for equality with np.ones(3).
Updated assertions in test_matrix_variable.py to use m.getVal(x) and m.getVal(y) instead of direct variable comparison. This ensures the tests check the evaluated values from the model rather than the symbolic variables.
|
This is great @Zeroto521, I just hit the same issue so thanks a lot for working on 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.
Pull Request Overview
Fixes the inability to compare MatrixExpr with Expr by introducing shared comparison logic and adjusting operator overloads, plus tests and changelog updates.
- Add support for MatrixExpr <=, >=, == comparisons against Expr (and arrays) via a central helper.
- Disallow == on ranged Matrix constraints (MatrixExprCons) with a clear error, and add tests.
- Update error types/messages for unsupported comparisons; update CHANGELOG.
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| tests/test_matrix_variable.py | Adds tests for MatrixExpr vs Expr comparisons and ranged Matrix constraints behavior. |
| src/pyscipopt/matrix.pxi | Refactors comparison handling via _matrixexpr_richcmp; updates MatrixExpr and MatrixExprCons rich comparisons. |
| src/pyscipopt/expr.pxi | Harmonizes error types/messages and removes duplicate _is_number; integrates with matrix changes. |
| CHANGELOG.md | Notes new capability; adjusts version header format. |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
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.
Pull Request Overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
Replaced explicit loops with list comprehensions for element-wise comparison, improving readability and potentially performance in matrix expression comparisons.
Raises a ValueError if the shapes of self and the other ndarray do not match during comparison, preventing invalid element-wise operations.
to fix #1061