Commit 0a63048
* Add test for matrix sum return type
Adds a test to ensure that summing a matrix variable along an axis returns a MatrixExpr type instead of MatrixVariable, addressing issue #1117.
* return `MatrixExpr` type
* Update CHANGELOG.md
* Speed up `MatrixExpr.sum(axis=...)`
Enhanced the MatrixExpr.sum method to accept axis as int or tuple, handle keepdims, and provide better error checking for axis bounds. This improves compatibility with numpy's sum behavior and allows more flexible summation over matrix expressions.
* Test `MatrixExpr.sum(axis=...)`
Removed n=200 from the sum performance test to limit test size. Added additional performance assertions comparing np.ndarray.sum and the optimized sum method for matrix variables.
* Test `MatrixExpr.sum(axis=tuple(range(ndim))`
Renamed test_matrix_sum_argument to test_matrix_sum_axis and updated tests to use explicit axis arguments in sum operations. This clarifies the behavior when summing over all axes and improves test coverage for axis handling.
* Refactor MatrixExpr.sum axis handling with numpy utility
Replaces manual axis validation and normalization in MatrixExpr.sum with numpy's normalize_axis_tuple for improved reliability and code clarity. Updates type hints and simplifies logic for summing across all axes.
* Add tests for matrix sum error
Added tests to verify error handling in matrix variable sum operations for invalid axis types, out-of-range values, and duplicate axes.
* Add tests for matrix sum with keepdims parameter
Introduces test cases to verify the shape of matrix variable sums when using the keepdims argument, ensuring correct behavior for both full and axis-specific summation.
* call `.sum` via positional argument
Replaces the use of the 'axis' keyword argument with a positional argument in the z.sum() method call to align with the expected function signature.
* Refactor sum method to use np.apply_along_axis
Replaces np.fromiter with np.apply_along_axis for summing along specified axes in MatrixExpr. This simplifies the code and improves readability.
* Directly test the `.sum` result
Added a comment to clarify the purpose of the test_matrix_sum_axis function, indicating it compares the result of summing a matrix variable after optimization.
* Update CHANGELOG.md
* Expand docstring for MatrixExpr.sum method
The docstring for the MatrixExpr.sum method was updated to provide detailed information about parameters, return values, and behavior, improving clarity and alignment with numpy conventions.
* Clarify MatrixExpr.sum docstring and note quicksum usage
Updated the docstring for MatrixExpr.sum to specify that it uses quicksum for speed optimization instead of numpy.ndarray.sum. Added a detailed note explaining the difference between quicksum (using __iadd__) and numpy's sum (using __add__).
* Split up two costing time test cases
Renamed the existing performance test to clarify it tests the case where axis is None. Added a new test to measure performance when summing along a specific axis.
* Supports numpy 1.x
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* Supports Python 3.8
* Simplify a bit
Refactored the sum method in MatrixExpr to clarify axis typing and simplify the application of np.apply_along_axis. The new implementation improves readability and maintains the intended behavior.
* suggestion
---------
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Joao-Dionisio <up201606210@up.pt>
Co-authored-by: João Dionísio <57299939+Joao-Dionisio@users.noreply.github.com>
1 parent ac1465a commit 0a63048
3 files changed
Lines changed: 141 additions & 16 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
| 8 | + | |
7 | 9 | | |
8 | 10 | | |
9 | 11 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
7 | | - | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
8 | 14 | | |
9 | 15 | | |
10 | 16 | | |
| |||
44 | 50 | | |
45 | 51 | | |
46 | 52 | | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
51 | 60 | | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
52 | 86 | | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
57 | 108 | | |
58 | 109 | | |
59 | 110 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
22 | | - | |
| 22 | + | |
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
| |||
181 | 181 | | |
182 | 182 | | |
183 | 183 | | |
184 | | - | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
185 | 208 | | |
186 | 209 | | |
187 | 210 | | |
| |||
190 | 213 | | |
191 | 214 | | |
192 | 215 | | |
193 | | - | |
| 216 | + | |
| 217 | + | |
194 | 218 | | |
195 | 219 | | |
196 | 220 | | |
197 | 221 | | |
198 | 222 | | |
199 | 223 | | |
200 | 224 | | |
201 | | - | |
| 225 | + | |
202 | 226 | | |
203 | 227 | | |
204 | 228 | | |
205 | 229 | | |
206 | 230 | | |
207 | | - | |
| 231 | + | |
208 | 232 | | |
209 | 233 | | |
210 | 234 | | |
211 | 235 | | |
212 | 236 | | |
213 | 237 | | |
214 | | - | |
215 | | - | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
216 | 262 | | |
217 | 263 | | |
218 | 264 | | |
| |||
229 | 275 | | |
230 | 276 | | |
231 | 277 | | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
232 | 296 | | |
233 | 297 | | |
234 | 298 | | |
| |||
521 | 585 | | |
522 | 586 | | |
523 | 587 | | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
524 | 596 | | |
525 | 597 | | |
526 | 598 | | |
| |||
0 commit comments