Skip to content

Conversation

@Zeroto521
Copy link
Contributor

to fix #1061

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.
@jonathanberthias
Copy link
Contributor

This is great @Zeroto521, I just hit the same issue so thanks a lot for working on this!
Just a quick question, would any more changes be needed to make comparison with GenExprs work?

@Zeroto521
Copy link
Contributor Author

Zeroto521 commented Sep 17, 2025

This is great @Zeroto521, I just hit the same issue so thanks a lot for working on this! Just a quick question, would any more changes be needed to make comparison with GenExprs work?

Expr and GenExpr are two different classes. The relationships between Variable, Term, Expr, and GenExpr are kind of complex. I want to merge Expr and GenExpr and simplify the inheritance.
This pr only helps for Expr now.

@Joao-Dionisio Joao-Dionisio requested a review from Copilot October 17, 2025 09:03
Copy link
Contributor

Copilot AI left a 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.

@Zeroto521 Zeroto521 requested a review from Copilot October 18, 2025 03:51
Copy link
Contributor

Copilot AI left a 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.
@Joao-Dionisio Joao-Dionisio merged commit 35c7f3e into scipopt:master Oct 23, 2025
1 check passed
@Zeroto521 Zeroto521 deleted the fix/1061 branch October 31, 2025 05:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: matrix variable can't be compared with expr directly

3 participants