Skip to content

Commit 9301063

Browse files
committed
Improve MatrixExpr.sum to handle scalar results
Updated the MatrixExpr.sum method to return a scalar when the result is a single value, preserving array output otherwise. This enhances usability for matrix expressions that may reduce to a single value.
1 parent ae63825 commit 9301063

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/pyscipopt/matrix.pxi

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@ def _is_number(e):
1717

1818
class MatrixExpr(np.ndarray):
1919
def sum(self, **kwargs):
20-
return super().sum(**kwargs).item()
21-
20+
"""
21+
Based on `numpy.ndarray.sum`, but returns a scalar if the result is a single value.
22+
This is useful for matrix expressions where the sum might reduce to a single value.
23+
"""
24+
res = super().sum(**kwargs)
25+
return res if res.size > 1 else res.item()
26+
2227
def __le__(self, other: Union[float, int, Variable, np.ndarray, 'MatrixExpr']) -> np.ndarray:
2328

2429
expr_cons_matrix = np.empty(self.shape, dtype=object)

0 commit comments

Comments
 (0)