Always set xlim, ylim, zlim in plot functions
[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 with self.assertRaises(TypeError):
121 self.x * self.x
122
123 def test_truediv(self):
124 with self.assertRaises(ZeroDivisionError):
125 self.expr / 0
126 self.assertEqual(self.expr / 2, self.x / 2 - self.y + Fraction(3, 2))
127 with self.assertRaises(TypeError):
128 self.x / self.x
129
130 def test_eq(self):
131 self.assertEqual(self.expr, self.expr)
132 self.assertNotEqual(self.x, self.y)
133 self.assertEqual(self.zero, 0)
134
135 def test_scaleint(self):
136 self.assertEqual((self.x + self.y/2 + self.z/3).scaleint(),
137 6*self.x + 3*self.y + 2*self.z)
138
139 def test_subs(self):
140 self.assertEqual(self.x.subs(self.x, 3), 3)
141 self.assertEqual(self.x.subs(self.x, self.x), self.x)
142 self.assertEqual(self.x.subs(self.x, self.y), self.y)
143 self.assertEqual(self.x.subs(self.x, self.x + self.y), self.x + self.y)
144 self.assertEqual(self.x.subs(self.y, 3), self.x)
145 self.assertEqual(self.pi.subs(self.x, 3), self.pi)
146 self.assertEqual(self.expr.subs(self.x, -3), -2 * self.y)
147 self.assertEqual(self.expr.subs([(self.x, self.y), (self.y, self.x)]), 3 - self.x)
148 self.assertEqual(self.expr.subs({self.x: self.z, self.y: self.z}), 3 - self.z)
149 self.assertEqual(self.expr.subs({self.x: self.z, self.y: self.z}), 3 - self.z)
150 with self.assertRaises(TypeError):
151 self.x.subs('x', 3)
152 with self.assertRaises(TypeError):
153 self.expr.subs([('x', self.z), ('y', self.z)])
154 with self.assertRaises(TypeError):
155 self.expr.subs({'x': self.z, 'y': self.z})
156 with self.assertRaises(TypeError):
157 self.expr.subs(self.x, 'x')
158
159 def test_fromstring(self):
160 self.assertEqual(Expression.fromstring('x'), self.x)
161 self.assertEqual(Expression.fromstring('-x'), -self.x)
162 self.assertEqual(Expression.fromstring('22/7'), self.pi)
163 self.assertEqual(Expression.fromstring('x - 2y + 3'), self.expr)
164 self.assertEqual(Expression.fromstring('x - (3-1)y + 3'), self.expr)
165 self.assertEqual(Expression.fromstring('x - 2*y + 3'), self.expr)
166
167 def test_repr(self):
168 self.assertEqual(str(Expression()), '0')
169 self.assertEqual(str(self.x), 'x')
170 self.assertEqual(str(-self.x), '-x')
171 self.assertEqual(str(self.pi), '22/7')
172 self.assertEqual(str(self.expr), 'x - 2*y + 3')
173
174 @requires_sympy
175 def test_fromsympy(self):
176 import sympy
177 sp_x, sp_y = sympy.symbols('x y')
178 self.assertEqual(Expression.fromsympy(sp_x), self.x)
179 self.assertEqual(Expression.fromsympy(sympy.Rational(22, 7)), self.pi)
180 self.assertEqual(Expression.fromsympy(sp_x - 2*sp_y + 3), self.expr)
181 with self.assertRaises(ValueError):
182 Expression.fromsympy(sp_x*sp_y)
183
184 @requires_sympy
185 def test_tosympy(self):
186 import sympy
187 sp_x, sp_y = sympy.symbols('x y')
188 self.assertEqual(self.x.tosympy(), sp_x)
189 self.assertEqual(self.pi.tosympy(), sympy.Rational(22, 7))
190 self.assertEqual(self.expr.tosympy(), sp_x - 2*sp_y + 3)
191
192
193 class TestSymbol(unittest.TestCase):
194
195 def setUp(self):
196 self.x = Symbol('x')
197 self.y = Symbol('y')
198
199 def test_new(self):
200 self.assertEqual(Symbol(' x '), self.x)
201 with self.assertRaises(TypeError):
202 Symbol(self.x)
203 with self.assertRaises(TypeError):
204 Symbol(1)
205
206 def test_name(self):
207 self.assertEqual(self.x.name, 'x')
208
209 def test_issymbol(self):
210 self.assertTrue(self.x.issymbol())
211
212 def test_fromstring(self):
213 self.assertEqual(Symbol.fromstring('x'), self.x)
214 with self.assertRaises(SyntaxError):
215 Symbol.fromstring('1')
216
217 def test_repr(self):
218 self.assertEqual(str(self.x), 'x')
219
220 @requires_sympy
221 def test_fromsympy(self):
222 import sympy
223 sp_x = sympy.Symbol('x')
224 self.assertEqual(Symbol.fromsympy(sp_x), self.x)
225 with self.assertRaises(TypeError):
226 Symbol.fromsympy(sympy.Rational(22, 7))
227 with self.assertRaises(TypeError):
228 Symbol.fromsympy(2 * sp_x)
229 with self.assertRaises(TypeError):
230 Symbol.fromsympy(sp_x*sp_x)
231
232
233 class TestDummy(unittest.TestCase):
234
235 def setUp(self):
236 self.x = Dummy('x')
237
238 def test_new(self):
239 self.assertEqual(self.x.name, 'x')
240 self.assertTrue(Dummy().name.startswith('Dummy'))
241
242 def test_eq(self):
243 self.assertEqual(self.x, self.x)
244 self.assertNotEqual(self.x, Symbol('x'))
245 self.assertNotEqual(Symbol('x'), self.x)
246 self.assertNotEqual(self.x, Dummy('x'))
247 self.assertNotEqual(Dummy(), Dummy())
248
249 def test_repr(self):
250 self.assertEqual(repr(self.x), '_x')
251 dummy1 = Dummy()
252 dummy2 = Dummy()
253 self.assertTrue(repr(dummy1).startswith('_Dummy_'))
254 self.assertNotEqual(repr(dummy1), repr(dummy2))
255
256
257 class TestSymbols(unittest.TestCase):
258
259 def setUp(self):
260 self.x = Symbol('x')
261 self.y = Symbol('y')
262
263 def test(self):
264 self.assertTupleEqual(symbols('x y'), (self.x, self.y))
265 self.assertTupleEqual(symbols('x,y'), (self.x, self.y))
266 self.assertTupleEqual(symbols(['x', 'y']), (self.x, self.y))
267 with self.assertRaises(TypeError):
268 symbols(1)
269 with self.assertRaises(TypeError):
270 symbols(['a', 1])
271
272
273 class TestRational(unittest.TestCase):
274
275 def setUp(self):
276 self.zero = Rational(0)
277 self.one = Rational(1)
278 self.pi = Rational(22, 7)
279
280 def test_new(self):
281 self.assertEqual(Rational(), self.zero)
282 self.assertEqual(Rational(1), self.one)
283 self.assertEqual(Rational(self.pi), self.pi)
284 self.assertEqual(Rational('22/7'), self.pi)
285
286 def test_hash(self):
287 self.assertEqual(hash(self.one), hash(1))
288 self.assertEqual(hash(self.pi), hash(Fraction(22, 7)))
289
290 def test_isconstant(self):
291 self.assertTrue(self.zero.isconstant())
292
293 def test_bool(self):
294 self.assertFalse(self.zero)
295 self.assertTrue(self.pi)
296
297 def test_repr(self):
298 self.assertEqual(repr(self.zero), '0')
299 self.assertEqual(repr(self.one), '1')
300 self.assertEqual(repr(self.pi), '22/7')
301
302 @requires_sympy
303 def test_fromsympy(self):
304 import sympy
305 self.assertEqual(Rational.fromsympy(sympy.Rational(22, 7)), self.pi)
306 with self.assertRaises(TypeError):
307 Rational.fromsympy(sympy.Symbol('x'))