Skip to content

Commit 005ed32

Browse files
committed
Add tests for division and normalization in ProdExpr
Introduces tests for division operations and normalization behavior in ProdExpr, including checks for division by constants, variables, and self, as well as normalization to ConstExpr, PolynomialExpr, and SinExpr. Enhances test coverage for expression handling in the symbolic math module.
1 parent 5396824 commit 005ed32

1 file changed

Lines changed: 39 additions & 2 deletions

File tree

tests/test_ProdExpr.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
22

3-
from pyscipopt import Expr, Model, exp, log, sin, sqrt
4-
from pyscipopt.scip import PolynomialExpr, PowExpr, ProdExpr, Term
3+
from pyscipopt import Expr, Model, exp, sin, sqrt
4+
from pyscipopt.scip import ConstExpr, PolynomialExpr, PowExpr, ProdExpr, SinExpr, Term
55

66

77
@pytest.fixture(scope="module")
@@ -71,6 +71,20 @@ def test_mul(model):
7171
assert str(expr) == "PowExpr(ProdExpr({(Term(x), Term(y)): 3.0}), 2.0)"
7272

7373

74+
def test_div(model):
75+
m, x, y = model
76+
77+
expr = 2 * (sin(x) * y)
78+
assert (
79+
str(expr / 0.5) == "ProdExpr({(SinExpr(Term(x)), Expr({Term(y): 1.0})): 4.0})"
80+
)
81+
assert (
82+
str(expr / x)
83+
== "ProdExpr({(ProdExpr({(SinExpr(Term(x)), Expr({Term(y): 1.0})): 2.0}), PowExpr(Expr({Term(x): 1.0}), -1.0)): 1.0})"
84+
)
85+
assert str(expr / expr) == "Expr({Term(): 1.0})"
86+
87+
7488
def test_cmp(model):
7589
m, x, y = model
7690

@@ -81,3 +95,26 @@ def test_cmp(model):
8195
str(expr1 == 1)
8296
== "ExprCons(ProdExpr({(SinExpr(Term(x)), Expr({Term(y): 1.0})): 1.0}), 1.0, 1.0)"
8397
)
98+
99+
100+
def test_normalize(model):
101+
m, x, y = model
102+
103+
expr = ProdExpr()._normalize()
104+
assert isinstance(expr, ConstExpr)
105+
assert str(expr) == "Expr({Term(): 0.0})"
106+
107+
expr = sin(x) * y
108+
assert isinstance(expr, ProdExpr)
109+
assert str(expr - expr) == "Expr({Term(): 0.0})"
110+
111+
expr = ProdExpr(Term(x))._normalize()
112+
assert type(expr) is PolynomialExpr
113+
assert str(expr) == "Expr({Term(x): 1.0})"
114+
115+
expr = ProdExpr(sin(x))._normalize()
116+
assert isinstance(expr, SinExpr)
117+
assert str(expr) == "SinExpr(Term(x))"
118+
119+
expr = sin(x) * y
120+
assert str(expr._normalize()) == str(expr)

0 commit comments

Comments
 (0)