|
| 1 | +import pytest |
| 2 | + |
| 3 | +from pyscipopt import Model |
| 4 | +from pyscipopt.scip import ConstExpr, PolynomialExpr, PowExpr, ProdExpr, Term |
| 5 | + |
| 6 | + |
| 7 | +@pytest.fixture(scope="module") |
| 8 | +def model(): |
| 9 | + m = Model() |
| 10 | + x = m.addVar("x") |
| 11 | + y = m.addVar("y") |
| 12 | + return m, x, y |
| 13 | + |
| 14 | + |
| 15 | +def test_degree(model): |
| 16 | + m, x, y = model |
| 17 | + |
| 18 | + assert PowExpr(Term(x), 3.0).degree() == float("inf") |
| 19 | + |
| 20 | + |
| 21 | +def test_mul(model): |
| 22 | + m, x, y = model |
| 23 | + |
| 24 | + expr = PowExpr(Term(x), 2.0) |
| 25 | + res = expr * expr |
| 26 | + assert isinstance(res, PowExpr) |
| 27 | + assert str(res) == "PowExpr(Term(x), 4.0)" |
| 28 | + |
| 29 | + res = expr * PowExpr(Term(x), 1.0) |
| 30 | + assert isinstance(res, PowExpr) |
| 31 | + assert str(res) == "PowExpr(Term(x), 3.0)" |
| 32 | + |
| 33 | + res = expr * PowExpr(Term(x), -1.0) |
| 34 | + assert isinstance(res, PolynomialExpr) |
| 35 | + assert str(res) == "Expr({Term(x): 1.0})" |
| 36 | + |
| 37 | + res = PowExpr(Term(x), 1.0) * PowExpr(Term(x), -1.0) |
| 38 | + assert isinstance(res, ConstExpr) |
| 39 | + assert str(res) == "Expr({Term(): 1.0})" |
| 40 | + |
| 41 | + res = PowExpr(Term(x), 1.0) * PowExpr(Term(x), -1.0) |
| 42 | + assert isinstance(res, ConstExpr) |
| 43 | + assert str(res) == "Expr({Term(): 1.0})" |
| 44 | + |
| 45 | + |
| 46 | +def test_imul(model): |
| 47 | + m, x, y = model |
| 48 | + |
| 49 | + expr = PowExpr(Term(x), 2.0) |
| 50 | + expr *= expr |
| 51 | + assert isinstance(expr, PowExpr) |
| 52 | + assert str(expr) == "PowExpr(Term(x), 4.0)" |
| 53 | + |
| 54 | + expr = PowExpr(Term(x), 2.0) |
| 55 | + expr *= PowExpr(Term(x), 1.0) |
| 56 | + assert isinstance(expr, PowExpr) |
| 57 | + assert str(expr) == "PowExpr(Term(x), 3.0)" |
| 58 | + |
| 59 | + expr = PowExpr(Term(x), 2.0) |
| 60 | + expr *= PowExpr(Term(x), -1.0) |
| 61 | + assert isinstance(expr, PolynomialExpr) |
| 62 | + assert str(expr) == "Expr({Term(x): 1.0})" |
| 63 | + |
| 64 | + expr = PowExpr(Term(x), 1.0) |
| 65 | + expr *= PowExpr(Term(x), -1.0) |
| 66 | + assert isinstance(expr, ConstExpr) |
| 67 | + assert str(expr) == "Expr({Term(): 1.0})" |
| 68 | + |
| 69 | + expr = PowExpr(Term(x), 1.0) |
| 70 | + expr *= x |
| 71 | + assert isinstance(expr, ProdExpr) |
| 72 | + assert str(expr) == "ProdExpr({(PowExpr(Term(x), 1.0), Expr({Term(x): 1.0})): 1.0})" |
| 73 | + |
| 74 | + |
| 75 | +def test_div(model): |
| 76 | + m, x, y = model |
| 77 | + |
| 78 | + expr = PowExpr(Term(x), 2.0) |
| 79 | + res = expr / PowExpr(Term(x), 1.0) |
| 80 | + assert isinstance(res, PolynomialExpr) |
| 81 | + assert str(res) == "Expr({Term(x): 1.0})" |
| 82 | + |
| 83 | + expr = PowExpr(Term(x), 2.0) |
| 84 | + res = expr / expr |
| 85 | + assert isinstance(res, ConstExpr) |
| 86 | + assert str(res) == "Expr({Term(): 1.0})" |
| 87 | + |
| 88 | + expr = PowExpr(Term(x), 2.0) |
| 89 | + res = expr / x |
| 90 | + assert isinstance(res, ProdExpr) |
| 91 | + assert ( |
| 92 | + str(res) |
| 93 | + == "ProdExpr({(PowExpr(Term(x), 2.0), PowExpr(Expr({Term(x): 1.0}), -1.0)): 1.0})" |
| 94 | + ) |
| 95 | + |
| 96 | + |
| 97 | +def test_cmp(model): |
| 98 | + m, x, y = model |
| 99 | + |
| 100 | + expr1 = PowExpr(Term(x), 2.0) |
| 101 | + expr2 = PowExpr(Term(y), -2.0) |
| 102 | + |
| 103 | + assert ( |
| 104 | + str(expr1 == expr2) |
| 105 | + == "ExprCons(Expr({PowExpr(Term(y), -2.0): -1.0, PowExpr(Term(x), 2.0): 1.0}), 0.0, 0.0)" |
| 106 | + ) |
| 107 | + assert str(expr1 <= 1) == "ExprCons(PowExpr(Term(x), 2.0), None, 1.0)" |
0 commit comments