Skip to content

Commit c4dfdb3

Browse files
committed
Use MatrixGenExpr for ufunc results
Return MatrixGenExpr (not MatrixExpr) from _wrap_ufunc for ndarray ufunc results in expr.pxi, and update tests to import and expect MatrixGenExpr. This aligns unary ufunc behavior (e.g., sqrt on lists/arrays) with the generator matrix expression type and fixes related type assertions in tests.
1 parent d388a77 commit c4dfdb3

2 files changed

Lines changed: 7 additions & 7 deletions

File tree

src/pyscipopt/expr.pxi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,7 @@ cdef object _vec_to_const = np.frompyfunc(_to_const, 1, 1)
871871
cdef inline object _wrap_ufunc(object x, object ufunc):
872872
if isinstance(x, (np.ndarray, list, tuple)):
873873
res = ufunc(_vec_to_const(x))
874-
return res.view(MatrixExpr) if isinstance(res, np.ndarray) else res
874+
return res.view(MatrixGenExpr) if isinstance(res, np.ndarray) else res
875875
return ufunc(_to_const(x))
876876

877877

tests/test_expr.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import numpy as np
44
import pytest
55

6-
from pyscipopt import MatrixExpr, Model, cos, exp, log, sin, sqrt
7-
from pyscipopt.scip import CONST, Expr, ExprCons, GenExpr
6+
from pyscipopt import Model, cos, exp, log, sin, sqrt
7+
from pyscipopt.scip import CONST, Expr, ExprCons, GenExpr, MatrixGenExpr
88

99

1010
@pytest.fixture(scope="module")
@@ -263,10 +263,10 @@ def test_unary(model):
263263
assert str(cos([[1]])) == "[[cos(1.0)]]"
264264

265265
assert isinstance(sqrt(2), GenExpr)
266-
assert isinstance(sqrt([2, 2]), MatrixExpr)
267-
assert isinstance(sqrt([[2], [2]]), MatrixExpr)
268-
assert isinstance(sqrt([2, x]), MatrixExpr)
269-
assert isinstance(sqrt([[2], [x]]), MatrixExpr)
266+
assert isinstance(sqrt([2, 2]), MatrixGenExpr)
267+
assert isinstance(sqrt([[2], [2]]), MatrixGenExpr)
268+
assert isinstance(sqrt([2, x]), MatrixGenExpr)
269+
assert isinstance(sqrt([[2], [x]]), MatrixGenExpr)
270270

271271
# test invalid unary operations
272272
with pytest.raises(TypeError):

0 commit comments

Comments
 (0)