Commit a82bf15
API: use
* Refactor matrix comparison logic using __array_ufunc__
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.
* Remove unused matrix.pxi include from expr.pxi
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.
* Replace MatrixExpr checks with np.ndarray in expr.pxi
Updated type checks from MatrixExpr to np.ndarray throughout expr.pxi to improve compatibility with NumPy arrays and simplify matrix expression handling.
* Fix logic error in MatrixExpr __array_ufunc__ handling
Corrects the condition to check for NotImplemented in MatrixExpr's __array_ufunc__ method, ensuring proper delegation to the superclass implementation.
* Fix matmul return type assertion in test
Update the test to assert that the result of 1D @ 1D matrix multiplication is of type Expr instead of MatrixExpr.
* Restrict supported ufuncs in MatrixExprCons
MatrixExprCons now only supports the '<=' and '>=' ufuncs, raising NotImplementedError for all others. This clarifies and enforces the intended usage of the class.
* Update matrix sum test to use MatrixExpr view
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.
* Refactor __array_ufunc__ return handling in MatrixExpr
Simplifies the __array_ufunc__ method by directly returning a MatrixExpr view when the result is a numpy ndarray, improving code clarity.
* Replace np.ndarray checks with MatrixExpr in unary ops
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.
* Remove method stubs from MatrixExpr and MatrixExprCons
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.
* Update CHANGELOG for MatrixExpr __array_ufunc__ support
Added entry noting that MatrixExpr and MatrixExprCons now use the `__array_ufunc__` protocol to control all numpy.ufunc inputs and outputs.
* Import Expr from pyscipopt.scip in matrix.pxi
Added a cimport for Expr from pyscipopt.scip to enable usage of the Expr type in this module.
* Fix error message grammar
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* Fix error message grammar
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* Fix argument unpacking in ufunc handling for MatrixExpr
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.
* Move _vec_evaluate definition above _ensure_array
Relocated the definition of _vec_evaluate to group all np.frompyfunc vectorized operator functions together for better code organization and readability.
---------
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>__array_ufunc__ as numpy ufunc enter (#1163)1 parent b3f298d commit a82bf15
3 files changed
+25
-106
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| 23 | + | |
23 | 24 | | |
24 | 25 | | |
25 | 26 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | | - | |
| 1 | + | |
5 | 2 | | |
6 | 3 | | |
7 | 4 | | |
| |||
18 | 15 | | |
19 | 16 | | |
20 | 17 | | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | 18 | | |
58 | 19 | | |
59 | 20 | | |
| |||
97 | 58 | | |
98 | 59 | | |
99 | 60 | | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
100 | 69 | | |
101 | 70 | | |
102 | 71 | | |
103 | 72 | | |
104 | 73 | | |
105 | 74 | | |
106 | 75 | | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | | - | |
119 | | - | |
120 | | - | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | | - | |
140 | | - | |
141 | | - | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | 76 | | |
147 | 77 | | |
148 | 78 | | |
| |||
154 | 84 | | |
155 | 85 | | |
156 | 86 | | |
157 | | - | |
158 | | - | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
159 | 95 | | |
160 | | - | |
161 | | - | |
162 | 96 | | |
163 | | - | |
164 | | - | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
165 | 101 | | |
166 | 102 | | |
167 | 103 | | |
| |||
172 | 108 | | |
173 | 109 | | |
174 | 110 | | |
175 | | - | |
176 | | - | |
177 | | - | |
178 | 111 | | |
179 | 112 | | |
180 | 113 | | |
| |||
261 | 194 | | |
262 | 195 | | |
263 | 196 | | |
264 | | - | |
| 197 | + | |
265 | 198 | | |
266 | 199 | | |
267 | 200 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
510 | 510 | | |
511 | 511 | | |
512 | 512 | | |
513 | | - | |
514 | | - | |
515 | | - | |
516 | | - | |
517 | | - | |
518 | | - | |
519 | | - | |
520 | | - | |
521 | | - | |
522 | | - | |
523 | | - | |
524 | | - | |
525 | | - | |
526 | | - | |
| 513 | + | |
527 | 514 | | |
528 | 515 | | |
529 | | - | |
530 | | - | |
531 | | - | |
| 516 | + | |
532 | 517 | | |
533 | 518 | | |
534 | 519 | | |
| |||
0 commit comments