Skip to content

Conversation

@Zeroto521
Copy link
Contributor

closes to #1156

Added numpy to build requirements in pyproject.toml and setup.py, ensuring numpy headers are included during compilation. Refactored matrix.pxi to improve matrix operation support, including custom __array_ufunc__ handling for dot/matmul, utility functions for type checking and array conversion, and a vectorized _core_dot implementation for efficient matrix multiplication.
Introduces a new parameterized test for matrix dot product performance and updates an assertion in test_matrix_matmul_return_type to expect Expr instead of MatrixExpr for 1D @ 1D operations.
Updated type checks throughout expr.pxi to use np.ndarray instead of MatrixExpr, improving compatibility with numpy arrays. Also adjusted matrix.pxi to ensure ufunc results are returned as MatrixExpr views when appropriate.
Deleted unnecessary conversion of the 'out' keyword argument to an array in MatrixExpr.__array_ufunc__, as it is not required for correct operation.
Deleted the overridden __matmul__ method in MatrixExpr, reverting to the default numpy ndarray behavior for matrix multiplication. This simplifies the class and avoids unnecessary type casting.
Copy link
Member

@Joao-Dionisio Joao-Dionisio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some things I need clarification on.
Also, changes should be, as much as possible, minimal, in the sense that they should only fix the one issue in the PR, otherwise it gets hard to follow. Not saying you're not doing this, just that there is a bunch of code I don't understand :)

Introduces a new _core_dot function to handle matrix multiplication between constant arrays and arrays of Expr objects, supporting both 1-D and N-D cases. The original _core_dot is renamed to _core_dot_2d and improved for clarity and efficiency. Updates __array_ufunc__ to use the new logic, ensuring correct handling of mixed-type matrix operations.
Adjusted tests in test_matrix_matmul_return_type to check the return type when performing matrix multiplication with numpy arrays and matrix variables, including new cases for ND arrays. Ensures correct type inference for various matrix multiplication scenarios.
@codecov
Copy link

codecov bot commented Jan 18, 2026

Codecov Report

❌ Patch coverage is 78.57143% with 12 lines in your changes missing coverage. Please review.
✅ Project coverage is 55.28%. Comparing base (5ceac82) to head (035fbe2).
⚠️ Report is 6 commits behind head on master.

Files with missing lines Patch % Lines
src/pyscipopt/matrix.pxi 76.59% 11 Missing ⚠️
src/pyscipopt/expr.pxi 88.88% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1159      +/-   ##
==========================================
+ Coverage   55.07%   55.28%   +0.21%     
==========================================
  Files          24       24              
  Lines        5420     5484      +64     
==========================================
+ Hits         2985     3032      +47     
- Misses       2435     2452      +17     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Replaces the _is_num_dt helper function with direct dtype.kind checks for numeric types in _core_dot. This simplifies the code and removes an unnecessary inline function.
@Joao-Dionisio
Copy link
Member

I'm sure you're noticed by now @Zeroto521 , but I'm not a user of numpy :) So, to be safe: are the end results of your changes something that a non-expert user would be expecting, syntax-wise? While performance is amazing, the goal is also to be as attrition-free for the user as possible.

Added detailed type annotations and a docstring to the MatrixExpr.__array_ufunc__ method for improved clarity and maintainability. Also clarified handling of ufunc methods and argument unboxing.
@Joao-Dionisio
Copy link
Member

Hey @Zeroto521 , please take a moment to address this comment. Is the end result what would be expected in the numpy world, and in gurobipy/xpresspy?

Renamed the test function to better reflect its purpose of testing performance for matrix dot operations.
Introduces test_matrix_dot_value to verify correct value retrieval from matrix variable dot products using getVal. Ensures expected results for both 1D and higher-dimensional dot operations.
@Zeroto521
Copy link
Contributor Author

Hey @Zeroto521 , please take a moment to address this comment. Is the end result what would be expected in the numpy world, and in gurobipy/xpresspy?

test_matrix_variable.py::test_matrix_dot_value and test_matrix_variable.py::test_matrix_matmul_return_type.py show that the result value and type of A @ X are expected.
The input, output, and behavior are the same as numpy and the old MatrixExpr.

@Joao-Dionisio Joao-Dionisio merged commit f3c0f82 into scipopt:master Jan 20, 2026
3 checks passed
@Zeroto521 Zeroto521 deleted the issue/1153 branch January 20, 2026 11: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.

2 participants