Check for SymPy presence in unittary tests
[linpy.git] / tests / test_linear.py
1 import functools
2 import unittest
3
4 from fractions import Fraction
5
6 from pypol.linear import *
7
8
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
22
23
24 class TestExpression(unittest.TestCase):
25
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
34
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)
42
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')
54
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'])
59
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)
64
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)
74
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]
84
85 def test_coefficients(self):
86 self.assertCountEqual(self.expr.coefficients(), [('x', 1), ('y', -2)])
87
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)
92
93 def test_isconstant(self):
94 self.assertFalse(self.x.isconstant())
95 self.assertTrue(self.pi.isconstant())
96 self.assertFalse(self.expr.isconstant())
97
98 def test_values(self):
99 self.assertCountEqual(self.expr.values(), [1, -2, 3])
100
101 def test_issymbol(self):
102 self.assertTrue(self.x.issymbol())
103 self.assertFalse(self.pi.issymbol())
104 self.assertFalse(self.expr.issymbol())
105
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)
111
112 def test_pos(self):
113 self.assertEqual(+self.expr, self.expr)
114
115 def test_neg(self):
116 self.assertEqual(-self.expr, -self.x + 2*self.y - 3)
117
118 def test_add(self):
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)
123
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)
128
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)
134
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))
139
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')
146
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)")
152
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)
160
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)
165
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)
169
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)
178
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)
185
186
187 class TestConstant(unittest.TestCase):
188
189 def setUp(self):
190 self.zero = Constant(0)
191 self.one = Constant(1)
192 self.pi = Constant(Fraction(22, 7))
193
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'))
199
200
201 class TestSymbol(unittest.TestCase):
202
203 def setUp(self):
204 self.x = Symbol('x')
205 self.y = Symbol('y')
206
207 def test_name(self):
208 self.assertEqual(self.x.name, 'x')
209
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])
214
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)
225
226
227 class TestOperators(unittest.TestCase):
228
229 pass
230
231
232 class TestPolyhedron(unittest.TestCase):
233
234 pass