a3e81b042c082d3e0d4d785320742cca6b89a504
4 from fractions
import Fraction
6 from ..linexprs
import *
11 def _requires_sympy(func
):
12 @functools.wraps(func
)
17 def _requires_sympy(func
):
18 @functools.wraps(func
)
20 raise unittest
.SkipTest('SymPy is not available')
24 class TestExpression(unittest
.TestCase
):
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
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):
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):
71 with self
.assertRaises(TypeError):
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])
106 self
.assertTrue(self
.x
)
107 self
.assertFalse(self
.zero
)
108 self
.assertTrue(self
.pi
)
109 self
.assertTrue(self
.expr
)
112 self
.assertEqual(+self
.expr
, self
.expr
)
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)
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
)
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):
137 self
.assertEqual(self
.expr
/ 2, self
.x
/ 2 - self
.y
+ Fraction(3, 2))
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
)
149 self
.assertEqual(self
.x
.subs('x', 3), 3)
150 self
.assertEqual(self
.x
.subs('x', self
.x
), self
.x
)
151 self
.assertEqual(self
.x
.subs('x', self
.y
), self
.y
)
152 self
.assertEqual(self
.x
.subs('x', self
.x
+ self
.y
), self
.x
+ self
.y
)
153 self
.assertEqual(self
.x
.subs('y', 3), self
.x
)
154 self
.assertEqual(self
.pi
.subs('x', 3), self
.pi
)
155 self
.assertEqual(self
.expr
.subs('x', -3), -2 * self
.y
)
156 self
.assertEqual(self
.expr
.subs([('x', self
.y
), ('y', self
.x
)]), 3 - self
.x
)
157 self
.assertEqual(self
.expr
.subs({'x': self
.y
, 'y': self
.x
}), 3 - self
.x
)
158 self
.assertEqual(self
.expr
.subs({self
.x
: self
.z
, self
.y
: self
.z
}), -self
.z
+ 3)
160 def test_fromstring(self
):
161 self
.assertEqual(Expression
.fromstring('x'), self
.x
)
162 self
.assertEqual(Expression
.fromstring('-x'), -self
.x
)
163 self
.assertEqual(Expression
.fromstring('22/7'), self
.pi
)
164 self
.assertEqual(Expression
.fromstring('x - 2y + 3'), self
.expr
)
165 self
.assertEqual(Expression
.fromstring('x - (3-1)y + 3'), self
.expr
)
166 self
.assertEqual(Expression
.fromstring('x - 2*y + 3'), self
.expr
)
169 self
.assertEqual(str(Expression()), '0')
170 self
.assertEqual(str(self
.x
), 'x')
171 self
.assertEqual(str(-self
.x
), '-x')
172 self
.assertEqual(str(self
.pi
), '22/7')
173 self
.assertEqual(str(self
.expr
), 'x - 2*y + 3')
176 self
.assertEqual(repr(self
.x
), "Symbol('x')")
177 self
.assertEqual(repr(self
.one
), 'Constant(1)')
178 self
.assertEqual(repr(self
.pi
), 'Constant(22, 7)')
179 self
.assertEqual(repr(self
.x
+ self
.one
), "Expression('x + 1')")
180 self
.assertEqual(repr(self
.expr
), "Expression('x - 2*y + 3')")
183 def test_fromsympy(self
):
184 sp_x
, sp_y
= sympy
.symbols('x y')
185 self
.assertEqual(Expression
.fromsympy(sp_x
), self
.x
)
186 self
.assertEqual(Expression
.fromsympy(sympy
.Rational(22, 7)), self
.pi
)
187 self
.assertEqual(Expression
.fromsympy(sp_x
- 2*sp_y
+ 3), self
.expr
)
188 with self
.assertRaises(ValueError):
189 Expression
.fromsympy(sp_x
*sp_y
)
192 def test_tosympy(self
):
193 sp_x
, sp_y
= sympy
.symbols('x y')
194 self
.assertEqual(self
.x
.tosympy(), sp_x
)
195 self
.assertEqual(self
.pi
.tosympy(), sympy
.Rational(22, 7))
196 self
.assertEqual(self
.expr
.tosympy(), sp_x
- 2*sp_y
+ 3)
199 class TestSymbol(unittest
.TestCase
):
206 self
.assertEqual(Symbol(' x '), self
.x
)
207 self
.assertEqual(Symbol(self
.x
), self
.x
)
208 with self
.assertRaises(TypeError):
212 self
.assertEqual(self
.x
.name
, 'x')
214 def test_issymbol(self
):
215 self
.assertTrue(self
.x
.issymbol())
217 def test_fromstring(self
):
218 self
.assertEqual(Symbol
.fromstring('x'), self
.x
)
219 with self
.assertRaises(SyntaxError):
220 Symbol
.fromstring('1')
223 self
.assertEqual(str(self
.x
), 'x')
226 self
.assertEqual(repr(self
.x
), "Symbol('x')")
229 def test_fromsympy(self
):
230 sp_x
= sympy
.Symbol('x')
231 self
.assertEqual(Symbol
.fromsympy(sp_x
), self
.x
)
232 with self
.assertRaises(TypeError):
233 Symbol
.fromsympy(sympy
.Rational(22, 7))
234 with self
.assertRaises(TypeError):
235 Symbol
.fromsympy(2 * sp_x
)
236 with self
.assertRaises(TypeError):
237 Symbol
.fromsympy(sp_x
*sp_x
)
239 def test_symbols(self
):
240 self
.assertListEqual(list(symbols('x y')), [self
.x
, self
.y
])
241 self
.assertListEqual(list(symbols('x,y')), [self
.x
, self
.y
])
242 self
.assertListEqual(list(symbols(['x', 'y'])), [self
.x
, self
.y
])
245 class TestConstant(unittest
.TestCase
):
248 self
.zero
= Constant(0)
249 self
.one
= Constant(1)
250 self
.pi
= Constant(Fraction(22, 7))
253 self
.assertEqual(Constant(), self
.zero
)
254 self
.assertEqual(Constant(1), self
.one
)
255 self
.assertEqual(Constant(self
.pi
), self
.pi
)
256 self
.assertEqual(Constant('22/7'), self
.pi
)
258 def test_isconstant(self
):
259 self
.assertTrue(self
.zero
.isconstant())
262 self
.assertFalse(self
.zero
)
263 self
.assertTrue(self
.pi
)
265 def test_fromstring(self
):
266 self
.assertEqual(Constant
.fromstring('22/7'), self
.pi
)
267 with self
.assertRaises(ValueError):
268 Constant
.fromstring('a')
269 with self
.assertRaises(TypeError):
270 Constant
.fromstring(1)
273 self
.assertEqual(repr(self
.zero
), 'Constant(0)')
274 self
.assertEqual(repr(self
.one
), 'Constant(1)')
275 self
.assertEqual(repr(self
.pi
), 'Constant(22, 7)')
278 def test_fromsympy(self
):
279 self
.assertEqual(Constant
.fromsympy(sympy
.Rational(22, 7)), self
.pi
)
280 with self
.assertRaises(TypeError):
281 Constant
.fromsympy(sympy
.Symbol('x'))