4 from fractions
import Fraction
6 from ..linexprs
import *
7 from .libhelper
import requires_sympy
10 class TestExpression(unittest
.TestCase
):
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
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):
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')
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
)
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):
58 with self
.assertRaises(TypeError):
61 def test_coefficients(self
):
62 self
.assertListEqual(list(self
.expr
.coefficients()), [(self
.x
, 1), (self
.y
, -2)])
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)
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
))
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)
79 def test_isconstant(self
):
80 self
.assertFalse(self
.x
.isconstant())
81 self
.assertTrue(self
.pi
.isconstant())
82 self
.assertFalse(self
.expr
.isconstant())
84 def test_issymbol(self
):
85 self
.assertTrue(self
.x
.issymbol())
86 self
.assertFalse(self
.pi
.issymbol())
87 self
.assertFalse(self
.expr
.issymbol())
89 def test_values(self
):
90 self
.assertListEqual(list(self
.expr
.values()), [1, -2, 3])
93 self
.assertTrue(self
.x
)
94 self
.assertFalse(self
.zero
)
95 self
.assertTrue(self
.pi
)
96 self
.assertTrue(self
.expr
)
99 self
.assertEqual(+self
.expr
, self
.expr
)
102 self
.assertEqual(-self
.expr
, -self
.x
+ 2*self
.y
- 3)
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)
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
)
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):
123 def test_truediv(self
):
124 with self
.assertRaises(ZeroDivisionError):
126 self
.assertEqual(self
.expr
/ 2, self
.x
/ 2 - self
.y
+ Fraction(3, 2))
127 with self
.assertRaises(TypeError):
131 self
.assertEqual(self
.expr
, self
.expr
)
132 self
.assertNotEqual(self
.x
, self
.y
)
133 self
.assertEqual(self
.zero
, 0)
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
)
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):
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')
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
)
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')
175 def test_fromsympy(self
):
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
)
185 def test_tosympy(self
):
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)
193 class TestSymbol(unittest
.TestCase
):
200 self
.assertEqual(Symbol(' x '), self
.x
)
201 with self
.assertRaises(TypeError):
203 with self
.assertRaises(TypeError):
207 self
.assertEqual(self
.x
.name
, 'x')
209 def test_issymbol(self
):
210 self
.assertTrue(self
.x
.issymbol())
212 def test_fromstring(self
):
213 self
.assertEqual(Symbol
.fromstring('x'), self
.x
)
214 with self
.assertRaises(SyntaxError):
215 Symbol
.fromstring('1')
218 self
.assertEqual(str(self
.x
), 'x')
221 def test_fromsympy(self
):
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
)
233 class TestDummy(unittest
.TestCase
):
239 self
.assertEqual(self
.x
.name
, 'x')
240 self
.assertTrue(Dummy().name
.startswith('Dummy'))
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())
250 self
.assertEqual(repr(self
.x
), '_x')
253 self
.assertTrue(repr(dummy1
).startswith('_Dummy_'))
254 self
.assertNotEqual(repr(dummy1
), repr(dummy2
))
257 class TestSymbols(unittest
.TestCase
):
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):
269 with self
.assertRaises(TypeError):
273 class TestRational(unittest
.TestCase
):
276 self
.zero
= Rational(0)
277 self
.one
= Rational(1)
278 self
.pi
= Rational(22, 7)
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
)
287 self
.assertEqual(hash(self
.one
), hash(1))
288 self
.assertEqual(hash(self
.pi
), hash(Fraction(22, 7)))
290 def test_isconstant(self
):
291 self
.assertTrue(self
.zero
.isconstant())
294 self
.assertFalse(self
.zero
)
295 self
.assertTrue(self
.pi
)
297 def test_fromstring(self
):
298 self
.assertEqual(Rational
.fromstring('22/7'), self
.pi
)
299 with self
.assertRaises(ValueError):
300 Rational
.fromstring('a')
301 with self
.assertRaises(TypeError):
302 Rational
.fromstring(1)
305 self
.assertEqual(repr(self
.zero
), '0')
306 self
.assertEqual(repr(self
.one
), '1')
307 self
.assertEqual(repr(self
.pi
), '22/7')
310 def test_fromsympy(self
):
312 self
.assertEqual(Rational
.fromsympy(sympy
.Rational(22, 7)), self
.pi
)
313 with self
.assertRaises(TypeError):
314 Rational
.fromsympy(sympy
.Symbol('x'))