1 import functools
2 import unittest
4 from fractions import Fraction
6 from pypol.linear import *
9 try:
10 import sympy
11 def _with_sympy(func):
12 @functools.wraps(func)
13 def wrapper(self):
14 return func(self)
15 return wrapper
16 except ImportError:
17 def _with_sympy(func):
18 @functools.wraps(func)
19 def wrapper(self):
20 raise unittest.SkipTest('SymPy is not available')
21 return wrapper
24 class TestExpression(unittest.TestCase):
26 def setUp(self):
27 self.x = Expression({'x': 1})
28 self.y = Expression({'y': 1})
29 self.z = Expression({'z': 1})
30 self.zero = Expression(constant=0)
31 self.one = Expression(constant=1)
32 self.pi = Expression(constant=Fraction(22, 7))
33 self.expr = self.x - 2*self.y + 3
35 def test_new_subclass(self):
36 self.assertIsInstance(self.x, Symbol)
37 self.assertIsInstance(self.pi, Constant)
38 self.assertNotIsInstance(self.x + self.pi, Symbol)
39 self.assertNotIsInstance(self.x + self.pi, Constant)
40 xx = Expression({'x': 2})
41 self.assertNotIsInstance(xx, Symbol)
43 def test_new_types(self):
44 with self.assertRaises(TypeError):
45 Expression('x + y', 2)
46 self.assertEqual(Expression({'x': 2}), Expression({self.x: 2}))
47 with self.assertRaises(TypeError):
48 Expression({0: 2})
49 with self.assertRaises(TypeError):
50 Expression({'x': '2'})
51 self.assertEqual(Expression(constant=1), Expression(constant=self.one))
52 with self.assertRaises(TypeError):
53 Expression(constant='1')
55 def test_symbols(self):
56 self.assertCountEqual(self.x.symbols, ['x'])
57 self.assertCountEqual(self.pi.symbols, [])
58 self.assertCountEqual(self.expr.symbols, ['x', 'y'])
60 def test_dimension(self):
61 self.assertEqual(self.x.dimension, 1)
62 self.assertEqual(self.pi.dimension, 0)
63 self.assertEqual(self.expr.dimension, 2)
65 def test_coefficient(self):
66 self.assertEqual(self.expr.coefficient('x'), 1)
67 self.assertEqual(self.expr.coefficient('y'), -2)
68 self.assertEqual(self.expr.coefficient(self.y), -2)
69 self.assertEqual(self.expr.coefficient('z'), 0)
70 with self.assertRaises(TypeError):
71 self.expr.coefficient(0)
72 with self.assertRaises(TypeError):
73 self.expr.coefficient(self.expr)
75 def test_getitem(self):
76 self.assertEqual(self.expr['x'], 1)
77 self.assertEqual(self.expr['y'], -2)
78 self.assertEqual(self.expr[self.y], -2)
79 self.assertEqual(self.expr['z'], 0)
80 with self.assertRaises(TypeError):
81 self.expr[0]
82 with self.assertRaises(TypeError):
83 self.expr[self.expr]
85 def test_coefficients(self):
86 self.assertCountEqual(self.expr.coefficients(), [('x', 1), ('y', -2)])
88 def test_constant(self):
89 self.assertEqual(self.x.constant, 0)
90 self.assertEqual(self.pi.constant, Fraction(22, 7))
91 self.assertEqual(self.expr.constant, 3)
93 def test_isconstant(self):
94 self.assertFalse(self.x.isconstant())
95 self.assertTrue(self.pi.isconstant())
96 self.assertFalse(self.expr.isconstant())
98 def test_values(self):
99 self.assertCountEqual(self.expr.values(), [1, -2, 3])
101 def test_issymbol(self):
102 self.assertTrue(self.x.issymbol())
103 self.assertFalse(self.pi.issymbol())
104 self.assertFalse(self.expr.issymbol())
106 def test_bool(self):
107 self.assertTrue(self.x)
108 self.assertFalse(self.zero)
109 self.assertTrue(self.pi)
110 self.assertTrue(self.expr)
112 def test_pos(self):
113 self.assertEqual(+self.expr, self.expr)
115 def test_neg(self):
116 self.assertEqual(-self.expr, -self.x + 2*self.y - 3)
119 self.assertEqual(self.x + Fraction(22, 7), self.x + self.pi)
120 self.assertEqual(Fraction(22, 7) + self.x, self.x + self.pi)
121 self.assertEqual(self.x + self.x, 2 * self.x)
122 self.assertEqual(self.expr + 2*self.y, self.x + 3)
124 def test_sub(self):
125 self.assertEqual(self.x - self.x, 0)
126 self.assertEqual(self.expr - 3, self.x - 2*self.y)
127 self.assertEqual(0 - self.x, -self.x)
129 def test_mul(self):
130 self.assertEqual(self.pi * 7, 22)
131 self.assertEqual(self.expr * 0, 0)
132 self.assertEqual(0 * self.expr, 0)
133 self.assertEqual(self.expr * 2, 2*self.x - 4*self.y + 6)
135 def test_div(self):
136 with self.assertRaises(ZeroDivisionError):
137 self.expr / 0
138 self.assertEqual(self.expr / 2, self.x / 2 - self.y + Fraction(3, 2))
140 def test_str(self):
141 self.assertEqual(str(Expression()), '0')
142 self.assertEqual(str(self.x), 'x')
143 self.assertEqual(str(-self.x), '-x')
144 self.assertEqual(str(self.pi), '22/7')
145 self.assertEqual(str(self.expr), 'x - 2*y + 3')
147 def test_repr(self):
148 self.assertEqual(repr(self.x), "Symbol('x')")
149 self.assertEqual(repr(self.one), 'Constant(1)')
150 self.assertEqual(repr(self.pi), 'Constant(22, 7)')
151 self.assertEqual(repr(self.expr), "Expression({'x': 1, 'y': -2}, 3)")
153 def test_fromstring(self):
154 self.assertEqual(Expression.fromstring('x'), self.x)
155 self.assertEqual(Expression.fromstring('-x'), -self.x)
156 self.assertEqual(Expression.fromstring('22/7'), self.pi)
157 self.assertEqual(Expression.fromstring('x - 2y + 3'), self.expr)
158 self.assertEqual(Expression.fromstring('x - (3-1)y + 3'), self.expr)
159 self.assertEqual(Expression.fromstring('x - 2*y + 3'), self.expr)
161 def test_eq(self):
162 self.assertEqual(self.expr, self.expr)
163 self.assertNotEqual(self.x, self.y)
164 self.assertEqual(self.zero, 0)
166 def test__toint(self):
167 self.assertEqual((self.x + self.y/2 + self.z/3)._toint(),
168 6*self.x + 3*self.y + 2*self.z)
170 @_with_sympy
171 def test_fromsympy(self):
172 sp_x, sp_y = sympy.symbols('x y')
173 self.assertEqual(Expression.fromsympy(sp_x), self.x)
174 self.assertEqual(Expression.fromsympy(sympy.Rational(22, 7)), self.pi)
175 self.assertEqual(Expression.fromsympy(sp_x - 2*sp_y + 3), self.expr)
176 with self.assertRaises(ValueError):
177 Expression.fromsympy(sp_x*sp_y)
179 @_with_sympy
180 def test_tosympy(self):
181 sp_x, sp_y = sympy.symbols('x y')
182 self.assertEqual(self.x.tosympy(), sp_x)
183 self.assertEqual(self.pi.tosympy(), sympy.Rational(22, 7))
184 self.assertEqual(self.expr.tosympy(), sp_x - 2*sp_y + 3)
187 class TestConstant(unittest.TestCase):
189 def setUp(self):
190 self.zero = Constant(0)
191 self.one = Constant(1)
192 self.pi = Constant(Fraction(22, 7))
194 @_with_sympy
195 def test_fromsympy(self):
196 self.assertEqual(Constant.fromsympy(sympy.Rational(22, 7)), self.pi)
197 with self.assertRaises(TypeError):
198 Constant.fromsympy(sympy.Symbol('x'))
201 class TestSymbol(unittest.TestCase):
203 def setUp(self):
204 self.x = Symbol('x')
205 self.y = Symbol('y')
207 def test_name(self):
208 self.assertEqual(self.x.name, 'x')
210 def test_symbols(self):
211 self.assertListEqual(list(symbols('x y')), [self.x, self.y])
212 self.assertListEqual(list(symbols('x,y')), [self.x, self.y])
213 self.assertListEqual(list(symbols(['x', 'y'])), [self.x, self.y])
215 @_with_sympy
216 def test_fromsympy(self):
217 sp_x = sympy.Symbol('x')
218 self.assertEqual(Symbol.fromsympy(sp_x), self.x)
219 with self.assertRaises(TypeError):
220 Symbol.fromsympy(sympy.Rational(22, 7))
221 with self.assertRaises(TypeError):
222 Symbol.fromsympy(2 * sp_x)
223 with self.assertRaises(TypeError):
224 Symbol.fromsympy(sp_x*sp_x)
227 class TestOperators(unittest.TestCase):
229 pass
232 class TestPolyhedron(unittest.TestCase):
234 def setUp(self):
235 x, y = symbols('x y')
236 self.square = Polyhedron(inequalities=[x, 1 - x, y, 1 - y])
238 def test_symbols(self):
239 self.assertCountEqual(self.square.symbols, ['x', 'y'])
241 def test_dimension(self):
242 self.assertEqual(self.square.dimension, 2)
244 def test_tostring(self):
245 self.assertEqual(str(self.square),
246 '{x >= 0, -x + 1 >= 0, y >= 0, -y + 1 >= 0}')
248 def test_fromstring(self):
249 self.assertEqual(Polyhedron.fromstring('{x >= 0, -x + 1 >= 0, '
250 'y >= 0, -y + 1 >= 0}'), self.square)
252 def test_isempty(self):
253 self.assertFalse(self.square.isempty())
255 def test_isuniverse(self):
256 self.assertFalse(self.square.isuniverse())
258 @unittest.expectedFailure
259 @_with_sympy
260 def test_fromsympy(self):
261 sp_x, sp_y = sympy.symbols('x y')
262 self.assertEqual(Polyhedron.fromsympy((sp_x >= 0) & (sp_x <= 1) &
263 (sp_y >= 0) & (sp_y <= 1)), self.square)
265 @_with_sympy
266 def test_tosympy(self):
267 sp_x, sp_y = sympy.symbols('x y')
268 self.assertEqual(self.square.tosympy(),
269 sympy.And(-sp_x + 1 >= 0, -sp_y + 1 >= 0, sp_x >= 0, sp_y >= 0))