c55c842be0811e0e593910faaaa0118b5763e295
[linpy.git] / pypol / tests / test_linexprs.py
1 import functools
2 import unittest
3
4 from fractions import Fraction
5
6 from ..linexprs import *
7 from .libhelper import requires_sympy
8
9
10 class TestExpression(unittest.TestCase):
11
12 def setUp(self):
13 self.x = Symbol('x')
14 self.y = Symbol('y')
15 self.z = Symbol('z')
16 self.zero = Expression(constant=0)
17 self.one = Expression(constant=1)
18 self.pi = Expression(constant=Fraction(22, 7))
19 self.expr = self.x - 2*self.y + 3
20
21 def test_new(self):
22 self.assertIsInstance(Expression(coefficients={self.x: 1}), Symbol)
23 self.assertIsInstance(Expression(constant=self.pi), Rational)
24 self.assertNotIsInstance(self.x + self.pi, Symbol)
25 self.assertNotIsInstance(self.x + self.pi, Rational)
26 xx = Expression({self.x: 2})
27 self.assertNotIsInstance(xx, Symbol)
28 with self.assertRaises(TypeError):
29 Expression('x + y', 2)
30 with self.assertRaises(TypeError):
31 Expression({0: 2})
32 with self.assertRaises(TypeError):
33 Expression({'x': '2'})
34 self.assertEqual(Expression(constant=1), Expression(constant=self.one))
35 self.assertEqual(Expression(constant='1'), Expression(constant=self.one))
36 with self.assertRaises(ValueError):
37 Expression(constant='a')
38
39 def test_coefficient(self):
40 self.assertEqual(self.expr.coefficient(self.x), 1)
41 self.assertEqual(self.expr.coefficient(self.y), -2)
42 self.assertEqual(self.expr.coefficient(self.z), 0)
43 with self.assertRaises(TypeError):
44 self.expr.coefficients('x')
45 with self.assertRaises(TypeError):
46 self.expr.coefficient(0)
47 with self.assertRaises(TypeError):
48 self.expr.coefficient(self.expr)
49
50 def test_getitem(self):
51 self.assertEqual(self.expr[self.x], 1)
52 self.assertEqual(self.expr[self.y], -2)
53 self.assertEqual(self.expr[self.z], 0)
54 with self.assertRaises(TypeError):
55 self.assertEqual(self.expr['x'], 1)
56 with self.assertRaises(TypeError):
57 self.expr[0]
58 with self.assertRaises(TypeError):
59 self.expr[self.expr]
60
61 def test_coefficients(self):
62 self.assertListEqual(list(self.expr.coefficients()), [(self.x, 1), (self.y, -2)])
63
64 def test_constant(self):
65 self.assertEqual(self.x.constant, 0)
66 self.assertEqual(self.pi.constant, Fraction(22, 7))
67 self.assertEqual(self.expr.constant, 3)
68
69 def test_symbols(self):
70 self.assertTupleEqual(self.x.symbols, (self.x,))
71 self.assertTupleEqual(self.pi.symbols, ())
72 self.assertTupleEqual(self.expr.symbols, (self.x, self.y))
73
74 def test_dimension(self):
75 self.assertEqual(self.x.dimension, 1)
76 self.assertEqual(self.pi.dimension, 0)
77 self.assertEqual(self.expr.dimension, 2)
78
79 def test_isconstant(self):
80 self.assertFalse(self.x.isconstant())
81 self.assertTrue(self.pi.isconstant())
82 self.assertFalse(self.expr.isconstant())
83
84 def test_issymbol(self):
85 self.assertTrue(self.x.issymbol())
86 self.assertFalse(self.pi.issymbol())
87 self.assertFalse(self.expr.issymbol())
88
89 def test_values(self):
90 self.assertListEqual(list(self.expr.values()), [1, -2, 3])
91
92 def test_bool(self):
93 self.assertTrue(self.x)
94 self.assertFalse(self.zero)
95 self.assertTrue(self.pi)
96 self.assertTrue(self.expr)
97
98 def test_pos(self):
99 self.assertEqual(+self.expr, self.expr)
100
101 def test_neg(self):
102 self.assertEqual(-self.expr, -self.x + 2*self.y - 3)
103
104 def test_add(self):
105 self.assertEqual(self.x + Fraction(22, 7), self.x + self.pi)
106 self.assertEqual(Fraction(22, 7) + self.x, self.x + self.pi)
107 self.assertEqual(self.x + self.x, 2 * self.x)
108 self.assertEqual(self.expr + 2*self.y, self.x + 3)
109
110 def test_sub(self):
111 self.assertEqual(self.x - self.x, 0)
112 self.assertEqual(self.expr - 3, self.x - 2*self.y)
113 self.assertEqual(0 - self.x, -self.x)
114
115 def test_mul(self):
116 self.assertEqual(self.pi * 7, 22)
117 self.assertEqual(self.expr * 0, 0)
118 self.assertEqual(0 * self.expr, 0)
119 self.assertEqual(self.expr * 2, 2*self.x - 4*self.y + 6)
120
121 def test_truediv(self):
122 with self.assertRaises(ZeroDivisionError):
123 self.expr / 0
124 self.assertEqual(self.expr / 2, self.x / 2 - self.y + Fraction(3, 2))
125
126 def test_eq(self):
127 self.assertEqual(self.expr, self.expr)
128 self.assertNotEqual(self.x, self.y)
129 self.assertEqual(self.zero, 0)
130
131 def test_scaleint(self):
132 self.assertEqual((self.x + self.y/2 + self.z/3).scaleint(),
133 6*self.x + 3*self.y + 2*self.z)
134
135 def test_subs(self):
136 self.assertEqual(self.x.subs(self.x, 3), 3)
137 self.assertEqual(self.x.subs(self.x, self.x), self.x)
138 self.assertEqual(self.x.subs(self.x, self.y), self.y)
139 self.assertEqual(self.x.subs(self.x, self.x + self.y), self.x + self.y)
140 self.assertEqual(self.x.subs(self.y, 3), self.x)
141 self.assertEqual(self.pi.subs(self.x, 3), self.pi)
142 self.assertEqual(self.expr.subs(self.x, -3), -2 * self.y)
143 self.assertEqual(self.expr.subs([(self.x, self.y), (self.y, self.x)]), 3 - self.x)
144 self.assertEqual(self.expr.subs({self.x: self.z, self.y: self.z}), 3 - self.z)
145 self.assertEqual(self.expr.subs({self.x: self.z, self.y: self.z}), 3 - self.z)
146 with self.assertRaises(TypeError):
147 self.x.subs('x', 3)
148 with self.assertRaises(TypeError):
149 self.expr.subs([('x', self.z), ('y', self.z)])
150 with self.assertRaises(TypeError):
151 self.expr.subs({'x': self.z, 'y': self.z})
152 with self.assertRaises(TypeError):
153 self.expr.subs(self.x, 'x')
154
155 def test_fromstring(self):
156 self.assertEqual(Expression.fromstring('x'), self.x)
157 self.assertEqual(Expression.fromstring('-x'), -self.x)
158 self.assertEqual(Expression.fromstring('22/7'), self.pi)
159 self.assertEqual(Expression.fromstring('x - 2y + 3'), self.expr)
160 self.assertEqual(Expression.fromstring('x - (3-1)y + 3'), self.expr)
161 self.assertEqual(Expression.fromstring('x - 2*y + 3'), self.expr)
162
163 def test_repr(self):
164 self.assertEqual(str(Expression()), '0')
165 self.assertEqual(str(self.x), 'x')
166 self.assertEqual(str(-self.x), '-x')
167 self.assertEqual(str(self.pi), '22/7')
168 self.assertEqual(str(self.expr), 'x - 2*y + 3')
169
170 @requires_sympy
171 def test_fromsympy(self):
172 import sympy
173 sp_x, sp_y = sympy.symbols('x y')
174 self.assertEqual(Expression.fromsympy(sp_x), self.x)
175 self.assertEqual(Expression.fromsympy(sympy.Rational(22, 7)), self.pi)
176 self.assertEqual(Expression.fromsympy(sp_x - 2*sp_y + 3), self.expr)
177 with self.assertRaises(ValueError):
178 Expression.fromsympy(sp_x*sp_y)
179
180 @requires_sympy
181 def test_tosympy(self):
182 import sympy
183 sp_x, sp_y = sympy.symbols('x y')
184 self.assertEqual(self.x.tosympy(), sp_x)
185 self.assertEqual(self.pi.tosympy(), sympy.Rational(22, 7))
186 self.assertEqual(self.expr.tosympy(), sp_x - 2*sp_y + 3)
187
188
189 class TestSymbol(unittest.TestCase):
190
191 def setUp(self):
192 self.x = Symbol('x')
193 self.y = Symbol('y')
194
195 def test_new(self):
196 self.assertEqual(Symbol(' x '), self.x)
197 with self.assertRaises(TypeError):
198 Symbol(self.x)
199 with self.assertRaises(TypeError):
200 Symbol(1)
201
202 def test_name(self):
203 self.assertEqual(self.x.name, 'x')
204
205 def test_issymbol(self):
206 self.assertTrue(self.x.issymbol())
207
208 def test_fromstring(self):
209 self.assertEqual(Symbol.fromstring('x'), self.x)
210 with self.assertRaises(SyntaxError):
211 Symbol.fromstring('1')
212
213 def test_repr(self):
214 self.assertEqual(str(self.x), 'x')
215
216 @requires_sympy
217 def test_fromsympy(self):
218 import sympy
219 sp_x = sympy.Symbol('x')
220 self.assertEqual(Symbol.fromsympy(sp_x), self.x)
221 with self.assertRaises(TypeError):
222 Symbol.fromsympy(sympy.Rational(22, 7))
223 with self.assertRaises(TypeError):
224 Symbol.fromsympy(2 * sp_x)
225 with self.assertRaises(TypeError):
226 Symbol.fromsympy(sp_x*sp_x)
227
228
229 class TestDummy(unittest.TestCase):
230
231 def setUp(self):
232 self.x = Dummy('x')
233
234 def test_new(self):
235 self.assertEqual(self.x.name, 'x')
236 self.assertTrue(Dummy().name.startswith('Dummy'))
237
238 def test_eq(self):
239 self.assertEqual(self.x, self.x)
240 self.assertNotEqual(self.x, Symbol('x'))
241 self.assertNotEqual(Symbol('x'), self.x)
242 self.assertNotEqual(self.x, Dummy('x'))
243 self.assertNotEqual(Dummy(), Dummy())
244
245 def test_repr(self):
246 self.assertEqual(repr(self.x), '_x')
247 dummy1 = Dummy()
248 dummy2 = Dummy()
249 self.assertTrue(repr(dummy1).startswith('_Dummy_'))
250 self.assertNotEqual(repr(dummy1), repr(dummy2))
251
252
253 class TestSymbols(unittest.TestCase):
254
255 def setUp(self):
256 self.x = Symbol('x')
257 self.y = Symbol('y')
258
259 def test(self):
260 self.assertTupleEqual(symbols('x y'), (self.x, self.y))
261 self.assertTupleEqual(symbols('x,y'), (self.x, self.y))
262 self.assertTupleEqual(symbols(['x', 'y']), (self.x, self.y))
263 with self.assertRaises(TypeError):
264 symbols(1)
265 with self.assertRaises(TypeError):
266 symbols(['a', 1])
267
268
269 class TestRational(unittest.TestCase):
270
271 def setUp(self):
272 self.zero = Rational(0)
273 self.one = Rational(1)
274 self.pi = Rational(Fraction(22, 7))
275
276 def test_new(self):
277 self.assertEqual(Rational(), self.zero)
278 self.assertEqual(Rational(1), self.one)
279 self.assertEqual(Rational(self.pi), self.pi)
280 self.assertEqual(Rational('22/7'), self.pi)
281
282 def test_isconstant(self):
283 self.assertTrue(self.zero.isconstant())
284
285 def test_bool(self):
286 self.assertFalse(self.zero)
287 self.assertTrue(self.pi)
288
289 def test_fromstring(self):
290 self.assertEqual(Rational.fromstring('22/7'), self.pi)
291 with self.assertRaises(ValueError):
292 Rational.fromstring('a')
293 with self.assertRaises(TypeError):
294 Rational.fromstring(1)
295
296 def test_repr(self):
297 self.assertEqual(repr(self.zero), '0')
298 self.assertEqual(repr(self.one), '1')
299 self.assertEqual(repr(self.pi), '22/7')
300
301 @requires_sympy
302 def test_fromsympy(self):
303 import sympy
304 self.assertEqual(Rational.fromsympy(sympy.Rational(22, 7)), self.pi)
305 with self.assertRaises(TypeError):
306 Rational.fromsympy(sympy.Symbol('x'))