c55c842be0811e0e593910faaaa0118b5763e295
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)
121 def test_truediv(self
):
122 with self
.assertRaises(ZeroDivisionError):
124 self
.assertEqual(self
.expr
/ 2, self
.x
/ 2 - self
.y
+ Fraction(3, 2))
127 self
.assertEqual(self
.expr
, self
.expr
)
128 self
.assertNotEqual(self
.x
, self
.y
)
129 self
.assertEqual(self
.zero
, 0)
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
)
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):
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')
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
)
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')
171 def test_fromsympy(self
):
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
)
181 def test_tosympy(self
):
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)
189 class TestSymbol(unittest
.TestCase
):
196 self
.assertEqual(Symbol(' x '), self
.x
)
197 with self
.assertRaises(TypeError):
199 with self
.assertRaises(TypeError):
203 self
.assertEqual(self
.x
.name
, 'x')
205 def test_issymbol(self
):
206 self
.assertTrue(self
.x
.issymbol())
208 def test_fromstring(self
):
209 self
.assertEqual(Symbol
.fromstring('x'), self
.x
)
210 with self
.assertRaises(SyntaxError):
211 Symbol
.fromstring('1')
214 self
.assertEqual(str(self
.x
), 'x')
217 def test_fromsympy(self
):
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
)
229 class TestDummy(unittest
.TestCase
):
235 self
.assertEqual(self
.x
.name
, 'x')
236 self
.assertTrue(Dummy().name
.startswith('Dummy'))
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())
246 self
.assertEqual(repr(self
.x
), '_x')
249 self
.assertTrue(repr(dummy1
).startswith('_Dummy_'))
250 self
.assertNotEqual(repr(dummy1
), repr(dummy2
))
253 class TestSymbols(unittest
.TestCase
):
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):
265 with self
.assertRaises(TypeError):
269 class TestRational(unittest
.TestCase
):
272 self
.zero
= Rational(0)
273 self
.one
= Rational(1)
274 self
.pi
= Rational(Fraction(22, 7))
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
)
282 def test_isconstant(self
):
283 self
.assertTrue(self
.zero
.isconstant())
286 self
.assertFalse(self
.zero
)
287 self
.assertTrue(self
.pi
)
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)
297 self
.assertEqual(repr(self
.zero
), '0')
298 self
.assertEqual(repr(self
.one
), '1')
299 self
.assertEqual(repr(self
.pi
), '22/7')
302 def test_fromsympy(self
):
304 self
.assertEqual(Rational
.fromsympy(sympy
.Rational(22, 7)), self
.pi
)
305 with self
.assertRaises(TypeError):
306 Rational
.fromsympy(sympy
.Symbol('x'))