Skip to content

Commit ea18c2c

Browse files
committed
Add tests for Expr/GenExpr ops and numpy
Introduce numpy import and expand test coverage for interactions between Expr and GenExpr: replace 1/x with sqrt(x) and add assertions for Expr+GenExpr, Expr*GenExpr, in-place +=, and Expr + numpy array. Also tidy import ordering from pyscipopt and pyscipopt.scip. These changes validate operator overloading and interoperability with numpy arrays.
1 parent f347bb3 commit ea18c2c

1 file changed

Lines changed: 20 additions & 3 deletions

File tree

tests/test_expr.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import math
22

3+
import numpy as np
34
import pytest
45

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

89

910
@pytest.fixture(scope="module")
@@ -287,7 +288,7 @@ def test_NotImplemented():
287288
with pytest.raises(TypeError):
288289
x == "1"
289290

290-
genexpr = 1 /x
291+
genexpr = sqrt(x)
291292

292293
with pytest.raises(TypeError):
293294
"y" + genexpr
@@ -320,3 +321,19 @@ def test_NotImplemented():
320321
"1" == genexpr
321322
with pytest.raises(TypeError):
322323
genexpr == "1"
324+
325+
# test Expr + GenExpr
326+
assert str(x + genexpr) == "sum(0.0,sqrt(sum(0.0,prod(1.0,x))),prod(1.0,x))"
327+
assert str(genexpr + x) == "sum(0.0,sqrt(sum(0.0,prod(1.0,x))),prod(1.0,x))"
328+
329+
# test Expr * GenExpr
330+
assert (
331+
str(x * genexpr) == "prod(1.0,sqrt(sum(0.0,prod(1.0,x))),sum(0.0,prod(1.0,x)))"
332+
)
333+
334+
# test Expr += GenExpr
335+
x += genexpr
336+
assert str(x) == "sum(0.0,sqrt(sum(0.0,prod(1.0,x))),prod(1.0,x))"
337+
338+
# test Expr + array
339+
assert str(x + np.array([1])) == "[sum(1.0,sqrt(sum(0.0,prod(1.0,x))),prod(1.0,x))]"

0 commit comments

Comments
 (0)