1 import functools
2 import unittest
4 from fractions import Fraction
6 from ..linexprs import *
9 try:
10 import sympy
11 def _requires_sympy(func):
12 @functools.wraps(func)
13 def wrapper(self):
14 return func(self)
15 return wrapper
16 except ImportError:
17 def _requires_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(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 with self.assertRaises(TypeError):
43 Expression('x + y', 2)
44 self.assertEqual(Expression({'x': 2}), Expression({self.x: 2}))
45 with self.assertRaises(TypeError):
46 Expression({0: 2})
47 with self.assertRaises(TypeError):
48 Expression({'x': '2'})
49 self.assertEqual(Expression(constant=1), Expression(constant=self.one))
50 self.assertEqual(Expression(constant='1'), Expression(constant=self.one))
51 with self.assertRaises(ValueError):
52 Expression(constant='a')
54 def test_coefficient(self):
55 self.assertEqual(self.expr.coefficient('x'), 1)
56 self.assertEqual(self.expr.coefficient('y'), -2)
57 self.assertEqual(self.expr.coefficient(self.y), -2)
58 self.assertEqual(self.expr.coefficient('z'), 0)
59 with self.assertRaises(TypeError):
60 self.expr.coefficient(0)
61 with self.assertRaises(TypeError):
62 self.expr.coefficient(self.expr)
64 def test_getitem(self):
65 self.assertEqual(self.expr['x'], 1)
66 self.assertEqual(self.expr['y'], -2)
67 self.assertEqual(self.expr[self.y], -2)
68 self.assertEqual(self.expr['z'], 0)
69 with self.assertRaises(TypeError):
70 self.expr[0]
71 with self.assertRaises(TypeError):
72 self.expr[self.expr]
74 def test_coefficients(self):
75 self.assertCountEqual(self.expr.coefficients(), [('x', 1), ('y', -2)])
77 def test_constant(self):
78 self.assertEqual(self.x.constant, 0)
79 self.assertEqual(self.pi.constant, Fraction(22, 7))
80 self.assertEqual(self.expr.constant, 3)
82 def test_symbols(self):
83 self.assertCountEqual(self.x.symbols, ['x'])
84 self.assertCountEqual(self.pi.symbols, [])
85 self.assertCountEqual(self.expr.symbols, ['x', 'y'])
87 def test_dimension(self):
88 self.assertEqual(self.x.dimension, 1)
89 self.assertEqual(self.pi.dimension, 0)
90 self.assertEqual(self.expr.dimension, 2)
92 def test_isconstant(self):
93 self.assertFalse(self.x.isconstant())
94 self.assertTrue(self.pi.isconstant())
95 self.assertFalse(self.expr.isconstant())
97 def test_issymbol(self):
98 self.assertTrue(self.x.issymbol())
99 self.assertFalse(self.pi.issymbol())
100 self.assertFalse(self.expr.issymbol())
102 def test_values(self):
103 self.assertCountEqual(self.expr.values(), [1, -2, 3])
105 def test_bool(self):
106 self.assertTrue(self.x)
107 self.assertFalse(self.zero)
108 self.assertTrue(self.pi)
109 self.assertTrue(self.expr)
111 def test_pos(self):
112 self.assertEqual(+self.expr, self.expr)
114 def test_neg(self):
115 self.assertEqual(-self.expr, -self.x + 2*self.y - 3)
118 self.assertEqual(self.x + Fraction(22, 7), self.x + self.pi)
119 self.assertEqual(Fraction(22, 7) + self.x, self.x + self.pi)
120 self.assertEqual(self.x + self.x, 2 * self.x)
121 self.assertEqual(self.expr + 2*self.y, self.x + 3)
123 def test_sub(self):
124 self.assertEqual(self.x - self.x, 0)
125 self.assertEqual(self.expr - 3, self.x - 2*self.y)
126 self.assertEqual(0 - self.x, -self.x)
128 def test_mul(self):
129 self.assertEqual(self.pi * 7, 22)
130 self.assertEqual(self.expr * 0, 0)
131 self.assertEqual(0 * self.expr, 0)
132 self.assertEqual(self.expr * 2, 2*self.x - 4*self.y + 6)
134 def test_truediv(self):
135 with self.assertRaises(ZeroDivisionError):
136 self.expr / 0
137 self.assertEqual(self.expr / 2, self.x / 2 - self.y + Fraction(3, 2))
139 def test_eq(self):
140 self.assertEqual(self.expr, self.expr)
141 self.assertNotEqual(self.x, self.y)
142 self.assertEqual(self.zero, 0)
144 def test__toint(self):
145 self.assertEqual((self.x + self.y/2 + self.z/3)._toint(),
146 6*self.x + 3*self.y + 2*self.z)
148 def test_fromstring(self):
149 self.assertEqual(Expression.fromstring('x'), self.x)
150 self.assertEqual(Expression.fromstring('-x'), -self.x)
151 self.assertEqual(Expression.fromstring('22/7'), self.pi)
152 self.assertEqual(Expression.fromstring('x - 2y + 3'), self.expr)
153 self.assertEqual(Expression.fromstring('x - (3-1)y + 3'), self.expr)
154 self.assertEqual(Expression.fromstring('x - 2*y + 3'), self.expr)
156 def test_str(self):
157 self.assertEqual(str(Expression()), '0')
158 self.assertEqual(str(self.x), 'x')
159 self.assertEqual(str(-self.x), '-x')
160 self.assertEqual(str(self.pi), '22/7')
161 self.assertEqual(str(self.expr), 'x - 2*y + 3')
163 def test_repr(self):
164 self.assertEqual(repr(self.x), "Symbol('x')")
165 self.assertEqual(repr(self.one), 'Constant(1)')
166 self.assertEqual(repr(self.pi), 'Constant(22, 7)')
167 self.assertEqual(repr(self.x + self.one), "Expression('x + 1')")
168 self.assertEqual(repr(self.expr), "Expression('x - 2*y + 3')")
170 @_requires_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 @_requires_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 TestSymbol(unittest.TestCase):
189 def setUp(self):
190 self.x = Symbol('x')
191 self.y = Symbol('y')
193 def test_new(self):
194 self.assertEqual(Symbol(' x '), self.x)
195 self.assertEqual(Symbol(self.x), self.x)
196 with self.assertRaises(TypeError):
197 Symbol(1)
199 def test_name(self):
200 self.assertEqual(self.x.name, 'x')
202 def test_issymbol(self):
203 self.assertTrue(self.x.issymbol())
205 def test_fromstring(self):
206 self.assertEqual(Symbol.fromstring('x'), self.x)
207 with self.assertRaises(SyntaxError):
208 Symbol.fromstring('1')
210 def test_str(self):
211 self.assertEqual(str(self.x), 'x')
213 def test_repr(self):
214 self.assertEqual(repr(self.x), "Symbol('x')")
216 @_requires_sympy
217 def test_fromsympy(self):
218 sp_x = sympy.Symbol('x')
219 self.assertEqual(Symbol.fromsympy(sp_x), self.x)
220 with self.assertRaises(TypeError):
221 Symbol.fromsympy(sympy.Rational(22, 7))
222 with self.assertRaises(TypeError):
223 Symbol.fromsympy(2 * sp_x)
224 with self.assertRaises(TypeError):
225 Symbol.fromsympy(sp_x*sp_x)
227 def test_symbols(self):
228 self.assertListEqual(list(symbols('x y')), [self.x, self.y])
229 self.assertListEqual(list(symbols('x,y')), [self.x, self.y])
230 self.assertListEqual(list(symbols(['x', 'y'])), [self.x, self.y])
233 class TestConstant(unittest.TestCase):
235 def setUp(self):
236 self.zero = Constant(0)
237 self.one = Constant(1)
238 self.pi = Constant(Fraction(22, 7))
240 def test_new(self):
241 self.assertEqual(Constant(), self.zero)
242 self.assertEqual(Constant(1), self.one)
243 self.assertEqual(Constant(self.pi), self.pi)
244 self.assertEqual(Constant('22/7'), self.pi)
246 def test_isconstant(self):
247 self.assertTrue(self.zero.isconstant())
249 def test_bool(self):
250 self.assertFalse(self.zero)
251 self.assertTrue(self.pi)
253 def test_fromstring(self):
254 self.assertEqual(Constant.fromstring('22/7'), self.pi)
255 with self.assertRaises(ValueError):
256 Constant.fromstring('a')
257 with self.assertRaises(TypeError):
258 Constant.fromstring(1)
260 def test_repr(self):
261 self.assertEqual(repr(self.zero), 'Constant(0)')
262 self.assertEqual(repr(self.one), 'Constant(1)')
263 self.assertEqual(repr(self.pi), 'Constant(22, 7)')
265 @_requires_sympy
266 def test_fromsympy(self):
267 self.assertEqual(Constant.fromsympy(sympy.Rational(22, 7)), self.pi)
268 with self.assertRaises(TypeError):
269 Constant.fromsympy(sympy.Symbol('x'))