Methods Expression.fromsympy(), Expression.tosympy()
[linpy.git] / tests / test_linear.py
1 import unittest
2
3 from fractions import Fraction
4
5 from pypol.linear import *
6
7
8 class TestExpression(unittest.TestCase):
9
10 def setUp(self):
11 self.x = Expression({'x': 1})
12 self.y = Expression({'y': 1})
13 self.z = Expression({'z': 1})
14 self.zero = Expression(constant=0)
15 self.one = Expression(constant=1)
16 self.pi = Expression(constant=Fraction(22, 7))
17 self.expr = self.x - 2*self.y + 3
18
19 def test_new_subclass(self):
20 self.assertIsInstance(self.x, Symbol)
21 self.assertIsInstance(self.pi, Constant)
22 self.assertNotIsInstance(self.x + self.pi, Symbol)
23 self.assertNotIsInstance(self.x + self.pi, Constant)
24 xx = Expression({'x': 2})
25 self.assertNotIsInstance(xx, Symbol)
26
27 def test_new_types(self):
28 with self.assertRaises(TypeError):
29 Expression('x + y', 2)
30 self.assertEqual(Expression({'x': 2}), Expression({self.x: 2}))
31 with self.assertRaises(TypeError):
32 Expression({0: 2})
33 with self.assertRaises(TypeError):
34 Expression({'x': '2'})
35 self.assertEqual(Expression(constant=1), Expression(constant=self.one))
36 with self.assertRaises(TypeError):
37 Expression(constant='1')
38
39 def test_symbols(self):
40 self.assertCountEqual(self.x.symbols, ['x'])
41 self.assertCountEqual(self.pi.symbols, [])
42 self.assertCountEqual(self.expr.symbols, ['x', 'y'])
43
44 def test_dimension(self):
45 self.assertEqual(self.x.dimension, 1)
46 self.assertEqual(self.pi.dimension, 0)
47 self.assertEqual(self.expr.dimension, 2)
48
49 def test_coefficient(self):
50 self.assertEqual(self.expr.coefficient('x'), 1)
51 self.assertEqual(self.expr.coefficient('y'), -2)
52 self.assertEqual(self.expr.coefficient(self.y), -2)
53 self.assertEqual(self.expr.coefficient('z'), 0)
54 with self.assertRaises(TypeError):
55 self.expr.coefficient(0)
56 with self.assertRaises(TypeError):
57 self.expr.coefficient(self.expr)
58
59 def test_getitem(self):
60 self.assertEqual(self.expr['x'], 1)
61 self.assertEqual(self.expr['y'], -2)
62 self.assertEqual(self.expr[self.y], -2)
63 self.assertEqual(self.expr['z'], 0)
64 with self.assertRaises(TypeError):
65 self.expr[0]
66 with self.assertRaises(TypeError):
67 self.expr[self.expr]
68
69 def test_coefficients(self):
70 self.assertCountEqual(self.expr.coefficients(), [('x', 1), ('y', -2)])
71
72 def test_constant(self):
73 self.assertEqual(self.x.constant, 0)
74 self.assertEqual(self.pi.constant, Fraction(22, 7))
75 self.assertEqual(self.expr.constant, 3)
76
77 def test_isconstant(self):
78 self.assertFalse(self.x.isconstant())
79 self.assertTrue(self.pi.isconstant())
80 self.assertFalse(self.expr.isconstant())
81
82 def test_values(self):
83 self.assertCountEqual(self.expr.values(), [1, -2, 3])
84
85 def test_issymbol(self):
86 self.assertTrue(self.x.issymbol())
87 self.assertFalse(self.pi.issymbol())
88 self.assertFalse(self.expr.issymbol())
89
90 def test_bool(self):
91 self.assertTrue(self.x)
92 self.assertFalse(self.zero)
93 self.assertTrue(self.pi)
94 self.assertTrue(self.expr)
95
96 def test_pos(self):
97 self.assertEqual(+self.expr, self.expr)
98
99 def test_neg(self):
100 self.assertEqual(-self.expr, -self.x + 2*self.y - 3)
101
102 def test_add(self):
103 self.assertEqual(self.x + Fraction(22, 7), self.x + self.pi)
104 self.assertEqual(Fraction(22, 7) + self.x, self.x + self.pi)
105 self.assertEqual(self.x + self.x, 2 * self.x)
106 self.assertEqual(self.expr + 2*self.y, self.x + 3)
107
108 def test_sub(self):
109 self.assertEqual(self.x - self.x, 0)
110 self.assertEqual(self.expr - 3, self.x - 2*self.y)
111 self.assertEqual(0 - self.x, -self.x)
112
113 def test_mul(self):
114 self.assertEqual(self.pi * 7, 22)
115 self.assertEqual(self.expr * 0, 0)
116 self.assertEqual(0 * self.expr, 0)
117 self.assertEqual(self.expr * 2, 2*self.x - 4*self.y + 6)
118
119 def test_div(self):
120 with self.assertRaises(ZeroDivisionError):
121 self.expr / 0
122 self.assertEqual(self.expr / 2, self.x / 2 - self.y + Fraction(3, 2))
123
124 def test_str(self):
125 self.assertEqual(str(Expression()), '0')
126 self.assertEqual(str(self.x), 'x')
127 self.assertEqual(str(-self.x), '-x')
128 self.assertEqual(str(self.pi), '22/7')
129 self.assertEqual(str(self.expr), 'x - 2*y + 3')
130
131 def test_repr(self):
132 self.assertEqual(repr(self.x), "Symbol('x')")
133 self.assertEqual(repr(self.one), 'Constant(1)')
134 self.assertEqual(repr(self.pi), 'Constant(22, 7)')
135 self.assertEqual(repr(self.expr), "Expression({'x': 1, 'y': -2}, 3)")
136
137 def test_fromstring(self):
138 self.assertEqual(Expression.fromstring('x'), self.x)
139 self.assertEqual(Expression.fromstring('-x'), -self.x)
140 self.assertEqual(Expression.fromstring('22/7'), self.pi)
141 self.assertEqual(Expression.fromstring('x - 2y + 3'), self.expr)
142 self.assertEqual(Expression.fromstring('x - (3-1)y + 3'), self.expr)
143 self.assertEqual(Expression.fromstring('x - 2*y + 3'), self.expr)
144
145 def test_eq(self):
146 self.assertEqual(self.expr, self.expr)
147 self.assertNotEqual(self.x, self.y)
148 self.assertEqual(self.zero, 0)
149
150 def test__toint(self):
151 self.assertEqual((self.x + self.y/2 + self.z/3)._toint(),
152 6*self.x + 3*self.y + 2*self.z)
153
154 def test_fromsympy(self):
155 import sympy
156 sp_x, sp_y = sympy.symbols('x y')
157 self.assertEqual(Expression.fromsympy(sp_x), self.x)
158 self.assertEqual(Expression.fromsympy(sympy.Rational(22, 7)), self.pi)
159 self.assertEqual(Expression.fromsympy(sp_x - 2*sp_y + 3), self.expr)
160 with self.assertRaises(ValueError):
161 Expression.fromsympy(sp_x*sp_y)
162
163 def test_tosympy(self):
164 import sympy
165 sp_x, sp_y = sympy.symbols('x y')
166 self.assertEqual(self.x.tosympy(), sp_x)
167 self.assertEqual(self.pi.tosympy(), sympy.Rational(22, 7))
168 self.assertEqual(self.expr.tosympy(), sp_x - 2*sp_y + 3)
169
170
171 class TestConstant(unittest.TestCase):
172
173 def setUp(self):
174 self.zero = Constant(0)
175 self.one = Constant(1)
176 self.pi = Constant(Fraction(22, 7))
177
178 def test_fromsympy(self):
179 import sympy
180 self.assertEqual(Constant.fromsympy(sympy.Rational(22, 7)), self.pi)
181 with self.assertRaises(TypeError):
182 Constant.fromsympy(sympy.Symbol('x'))
183
184
185 class TestSymbol(unittest.TestCase):
186
187 def setUp(self):
188 self.x = Symbol('x')
189 self.y = Symbol('y')
190
191 def test_name(self):
192 self.assertEqual(self.x.name, 'x')
193
194 def test_symbols(self):
195 self.assertListEqual(list(symbols('x y')), [self.x, self.y])
196 self.assertListEqual(list(symbols('x,y')), [self.x, self.y])
197 self.assertListEqual(list(symbols(['x', 'y'])), [self.x, self.y])
198
199 def test_fromsympy(self):
200 import sympy
201 sp_x = sympy.Symbol('x')
202 self.assertEqual(Symbol.fromsympy(sp_x), self.x)
203 with self.assertRaises(TypeError):
204 Symbol.fromsympy(sympy.Rational(22, 7))
205 with self.assertRaises(TypeError):
206 Symbol.fromsympy(2 * sp_x)
207 with self.assertRaises(TypeError):
208 Symbol.fromsympy(sp_x*sp_x)
209
210
211 class TestOperators(unittest.TestCase):
212
213 pass
214
215
216 class TestPolyhedron(unittest.TestCase):
217
218 pass