Skip to content

Conversation

@Zeroto521
Copy link
Contributor

@Zeroto521 Zeroto521 commented Jan 20, 2026

fixes #1117. Some other methods will return np.ndarray type from MatrixExpr. And __array_ufunc__ could fix them all.
and closes #1139

Replaces custom rich comparison methods for MatrixExpr and MatrixExprCons with __array_ufunc__ implementations, leveraging numpy's ufuncs for elementwise comparisons. Removes legacy helper functions and streamlines operator handling, improving maintainability and performance.
Deleted the 'include "matrix.pxi"' line from expr.pxi as it is no longer needed. This helps clean up the code and avoid unnecessary dependencies.
Updated type checks from MatrixExpr to np.ndarray throughout expr.pxi to improve compatibility with NumPy arrays and simplify matrix expression handling.
Corrects the condition to check for NotImplemented in MatrixExpr's __array_ufunc__ method, ensuring proper delegation to the superclass implementation.
Update the test to assert that the result of 1D @ 1D matrix multiplication is of type Expr instead of MatrixExpr.
MatrixExprCons now only supports the '<=' and '>=' ufuncs, raising NotImplementedError for all others. This clarifies and enforces the intended usage of the class.
Refactored test_matrix_sum_result to use a view of MatrixExpr for sum operation, aligning the test with the expected usage pattern and simplifying result comparison.
Simplifies the __array_ufunc__ method by directly returning a MatrixExpr view when the result is a numpy ndarray, improving code clarity.
Updated type checks in exp, log, sqrt, sin, and cos functions to use MatrixExpr instead of np.ndarray. This change ensures that matrix-specific expression handling is applied only to MatrixExpr instances.
Eliminated explicit method declarations from the MatrixExpr and MatrixExprCons classes in the type stub, leaving only ellipses. This simplifies the type hints and may reflect a change in how these classes are intended to be used or documented.
Added entry noting that MatrixExpr and MatrixExprCons now use the `__array_ufunc__` protocol to control all numpy.ufunc inputs and outputs.
@Zeroto521
Copy link
Contributor Author

Waiting for #1157. And MatrixExpr.sum could be removed from the scip.pyi file.
https://github.com/scipopt/PySCIPOpt/actions/runs/21171222618/job/60888195185?pr=1163

Copilot AI review requested due to automatic review settings January 21, 2026 02:41
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

This PR refactors the MatrixExpr and MatrixExprCons classes to use NumPy's __array_ufunc__ protocol instead of manually overriding individual operators. This provides comprehensive control over all numpy universal function operations on these matrix types.

Changes:

  • Replaced manual operator overrides (__add__, __mul__, __le__, __ge__, etc.) with __array_ufunc__ implementations in both MatrixExpr and MatrixExprCons classes
  • Added vectorized comparison functions (_vec_le, _vec_ge, _vec_eq) using operator module
  • Simplified type stubs by removing explicit method signatures since __array_ufunc__ handles operations automatically

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/pyscipopt/matrix.pxi Replaces manual operator overrides with __array_ufunc__ protocol implementation for comprehensive numpy ufunc control; adds vectorized comparison helpers
src/pyscipopt/scip.pyi Simplifies type stubs by removing explicit operator method signatures (now handled by __array_ufunc__)
CHANGELOG.md Documents the adoption of __array_ufunc__ protocol for numpy ufunc control

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Zeroto521 and others added 6 commits January 21, 2026 10:55
Added a cimport for Expr from pyscipopt.scip to enable usage of the Expr type in this module.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Replaces use of argument unpacking (*args) with explicit indexing (args[0], args[1]) in calls to _vec_le, _vec_ge, and _vec_eq within MatrixExpr and MatrixExprCons. This change clarifies argument passing and may prevent potential issues with argument order or count.
Relocated the definition of _vec_evaluate to group all np.frompyfunc vectorized operator functions together for better code organization and readability.
@Joao-Dionisio Joao-Dionisio merged commit a82bf15 into scipopt:master Jan 22, 2026
3 checks passed
@Zeroto521 Zeroto521 deleted the issue/1139 branch January 22, 2026 10:22
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.

API: use __array_wrap__ to instead of .view(MatrixExpr) BUG: MatrixVariable.sum(axis=...) return type is MatrixVariable not MatrixExpr

2 participants