bc062b6438f335c14d65444bbbbea6607e1f61fb
4 from fractions
import Fraction
6 from ..linexprs
import *
7 from .libhelper
import requires_sympy
10 class TestExpression(unittest
.TestCase
):
13 self
.x
= Expression({'x': 1})
14 self
.y
= Expression({'y': 1})
15 self
.z
= Expression({'z': 1})
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(self
.x
, Symbol
)
23 self
.assertIsInstance(self
.pi
, Constant
)
24 self
.assertNotIsInstance(self
.x
+ self
.pi
, Symbol
)
25 self
.assertNotIsInstance(self
.x
+ self
.pi
, Constant
)
26 xx
= Expression({'x': 2})
27 self
.assertNotIsInstance(xx
, Symbol
)
28 with self
.assertRaises(TypeError):
29 Expression('x + y', 2)
30 self
.assertEqual(Expression({'x': 2}), Expression({self
.x
: 2}))
31 with self
.assertRaises(TypeError):
33 with self
.assertRaises(TypeError):
34 Expression({'x': '2'})
35 self
.assertEqual(Expression(constant
=1), Expression(constant
=self
.one
))
36 self
.assertEqual(Expression(constant
='1'), Expression(constant
=self
.one
))
37 with self
.assertRaises(ValueError):
38 Expression(constant
='a')
40 def test_coefficient(self
):
41 self
.assertEqual(self
.expr
.coefficient('x'), 1)
42 self
.assertEqual(self
.expr
.coefficient('y'), -2)
43 self
.assertEqual(self
.expr
.coefficient(self
.y
), -2)
44 self
.assertEqual(self
.expr
.coefficient('z'), 0)
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
['x'], 1)
52 self
.assertEqual(self
.expr
['y'], -2)
53 self
.assertEqual(self
.expr
[self
.y
], -2)
54 self
.assertEqual(self
.expr
['z'], 0)
55 with self
.assertRaises(TypeError):
57 with self
.assertRaises(TypeError):
60 def test_coefficients(self
):
61 self
.assertCountEqual(self
.expr
.coefficients(), [('x', 1), ('y', -2)])
63 def test_constant(self
):
64 self
.assertEqual(self
.x
.constant
, 0)
65 self
.assertEqual(self
.pi
.constant
, Fraction(22, 7))
66 self
.assertEqual(self
.expr
.constant
, 3)
68 def test_symbols(self
):
69 self
.assertCountEqual(self
.x
.symbols
, ['x'])
70 self
.assertCountEqual(self
.pi
.symbols
, [])
71 self
.assertCountEqual(self
.expr
.symbols
, ['x', 'y'])
73 def test_dimension(self
):
74 self
.assertEqual(self
.x
.dimension
, 1)
75 self
.assertEqual(self
.pi
.dimension
, 0)
76 self
.assertEqual(self
.expr
.dimension
, 2)
78 def test_isconstant(self
):
79 self
.assertFalse(self
.x
.isconstant())
80 self
.assertTrue(self
.pi
.isconstant())
81 self
.assertFalse(self
.expr
.isconstant())
83 def test_issymbol(self
):
84 self
.assertTrue(self
.x
.issymbol())
85 self
.assertFalse(self
.pi
.issymbol())
86 self
.assertFalse(self
.expr
.issymbol())
88 def test_values(self
):
89 self
.assertCountEqual(self
.expr
.values(), [1, -2, 3])
92 self
.assertTrue(self
.x
)
93 self
.assertFalse(self
.zero
)
94 self
.assertTrue(self
.pi
)
95 self
.assertTrue(self
.expr
)
98 self
.assertEqual(+self
.expr
, self
.expr
)
101 self
.assertEqual(-self
.expr
, -self
.x
+ 2*self
.y
- 3)
104 self
.assertEqual(self
.x
+ Fraction(22, 7), self
.x
+ self
.pi
)
105 self
.assertEqual(Fraction(22, 7) + self
.x
, self
.x
+ self
.pi
)
106 self
.assertEqual(self
.x
+ self
.x
, 2 * self
.x
)
107 self
.assertEqual(self
.expr
+ 2*self
.y
, self
.x
+ 3)
110 self
.assertEqual(self
.x
- self
.x
, 0)
111 self
.assertEqual(self
.expr
- 3, self
.x
- 2*self
.y
)
112 self
.assertEqual(0 - self
.x
, -self
.x
)
115 self
.assertEqual(self
.pi
* 7, 22)
116 self
.assertEqual(self
.expr
* 0, 0)
117 self
.assertEqual(0 * self
.expr
, 0)
118 self
.assertEqual(self
.expr
* 2, 2*self
.x
- 4*self
.y
+ 6)
120 def test_truediv(self
):
121 with self
.assertRaises(ZeroDivisionError):
123 self
.assertEqual(self
.expr
/ 2, self
.x
/ 2 - self
.y
+ Fraction(3, 2))
126 self
.assertEqual(self
.expr
, self
.expr
)
127 self
.assertNotEqual(self
.x
, self
.y
)
128 self
.assertEqual(self
.zero
, 0)
130 def test__toint(self
):
131 self
.assertEqual((self
.x
+ self
.y
/2 + self
.z
/3)._toint
(),
132 6*self
.x
+ 3*self
.y
+ 2*self
.z
)
135 self
.assertEqual(self
.x
.subs('x', 3), 3)
136 self
.assertEqual(self
.x
.subs('x', self
.x
), self
.x
)
137 self
.assertEqual(self
.x
.subs('x', self
.y
), self
.y
)
138 self
.assertEqual(self
.x
.subs('x', self
.x
+ self
.y
), self
.x
+ self
.y
)
139 self
.assertEqual(self
.x
.subs('y', 3), self
.x
)
140 self
.assertEqual(self
.pi
.subs('x', 3), self
.pi
)
141 self
.assertEqual(self
.expr
.subs('x', -3), -2 * self
.y
)
142 self
.assertEqual(self
.expr
.subs([('x', self
.y
), ('y', self
.x
)]), 3 - self
.x
)
143 self
.assertEqual(self
.expr
.subs({'x': self
.z
, 'y': self
.z
}), 3 - self
.z
)
144 self
.assertEqual(self
.expr
.subs({self
.x
: self
.z
, self
.y
: self
.z
}), 3 - self
.z
)
146 def test_fromstring(self
):
147 self
.assertEqual(Expression
.fromstring('x'), self
.x
)
148 self
.assertEqual(Expression
.fromstring('-x'), -self
.x
)
149 self
.assertEqual(Expression
.fromstring('22/7'), self
.pi
)
150 self
.assertEqual(Expression
.fromstring('x - 2y + 3'), self
.expr
)
151 self
.assertEqual(Expression
.fromstring('x - (3-1)y + 3'), self
.expr
)
152 self
.assertEqual(Expression
.fromstring('x - 2*y + 3'), self
.expr
)
155 self
.assertEqual(str(Expression()), '0')
156 self
.assertEqual(str(self
.x
), 'x')
157 self
.assertEqual(str(-self
.x
), '-x')
158 self
.assertEqual(str(self
.pi
), '22/7')
159 self
.assertEqual(str(self
.expr
), 'x - 2*y + 3')
162 self
.assertEqual(repr(self
.x
), "Symbol('x')")
163 self
.assertEqual(repr(self
.one
), 'Constant(1)')
164 self
.assertEqual(repr(self
.pi
), 'Constant(22, 7)')
165 self
.assertEqual(repr(self
.x
+ self
.one
), "Expression('x + 1')")
166 self
.assertEqual(repr(self
.expr
), "Expression('x - 2*y + 3')")
169 def test_fromsympy(self
):
171 sp_x
, sp_y
= sympy
.symbols('x y')
172 self
.assertEqual(Expression
.fromsympy(sp_x
), self
.x
)
173 self
.assertEqual(Expression
.fromsympy(sympy
.Rational(22, 7)), self
.pi
)
174 self
.assertEqual(Expression
.fromsympy(sp_x
- 2*sp_y
+ 3), self
.expr
)
175 with self
.assertRaises(ValueError):
176 Expression
.fromsympy(sp_x
*sp_y
)
179 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
):
194 self
.assertEqual(Symbol(' x '), self
.x
)
195 self
.assertEqual(Symbol(self
.x
), self
.x
)
196 with self
.assertRaises(TypeError):
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')
211 self
.assertEqual(str(self
.x
), 'x')
214 self
.assertEqual(repr(self
.x
), "Symbol('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
)
228 def test_symbols(self
):
229 self
.assertListEqual(list(symbols('x y')), [self
.x
, self
.y
])
230 self
.assertListEqual(list(symbols('x,y')), [self
.x
, self
.y
])
231 self
.assertListEqual(list(symbols(['x', 'y'])), [self
.x
, self
.y
])
234 class TestConstant(unittest
.TestCase
):
237 self
.zero
= Constant(0)
238 self
.one
= Constant(1)
239 self
.pi
= Constant(Fraction(22, 7))
242 self
.assertEqual(Constant(), self
.zero
)
243 self
.assertEqual(Constant(1), self
.one
)
244 self
.assertEqual(Constant(self
.pi
), self
.pi
)
245 self
.assertEqual(Constant('22/7'), self
.pi
)
247 def test_isconstant(self
):
248 self
.assertTrue(self
.zero
.isconstant())
251 self
.assertFalse(self
.zero
)
252 self
.assertTrue(self
.pi
)
254 def test_fromstring(self
):
255 self
.assertEqual(Constant
.fromstring('22/7'), self
.pi
)
256 with self
.assertRaises(ValueError):
257 Constant
.fromstring('a')
258 with self
.assertRaises(TypeError):
259 Constant
.fromstring(1)
262 self
.assertEqual(repr(self
.zero
), 'Constant(0)')
263 self
.assertEqual(repr(self
.one
), 'Constant(1)')
264 self
.assertEqual(repr(self
.pi
), 'Constant(22, 7)')
267 def test_fromsympy(self
):
269 self
.assertEqual(Constant
.fromsympy(sympy
.Rational(22, 7)), self
.pi
)
270 with self
.assertRaises(TypeError):
271 Constant
.fromsympy(sympy
.Symbol('x'))