adfec7d26f1cfd05db6a43fb57b988f15e8ef612
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(self
.x
, Symbol
)
23 self
.assertIsInstance(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
.assertCountEqual(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
.assertCountEqual(self
.x
.symbols
, [self
.x
])
71 self
.assertCountEqual(self
.pi
.symbols
, [])
72 self
.assertCountEqual(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
.assertCountEqual(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
)
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 TestRational(unittest
.TestCase
):
237 self
.zero
= Rational(0)
238 self
.one
= Rational(1)
239 self
.pi
= Rational(Fraction(22, 7))
242 self
.assertEqual(Rational(), self
.zero
)
243 self
.assertEqual(Rational(1), self
.one
)
244 self
.assertEqual(Rational(self
.pi
), self
.pi
)
245 self
.assertEqual(Rational('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(Rational
.fromstring('22/7'), self
.pi
)
256 with self
.assertRaises(ValueError):
257 Rational
.fromstring('a')
258 with self
.assertRaises(TypeError):
259 Rational
.fromstring(1)
262 self
.assertEqual(repr(self
.zero
), '0')
263 self
.assertEqual(repr(self
.one
), '1')
264 self
.assertEqual(repr(self
.pi
), '22/7')
267 def test_fromsympy(self
):
269 self
.assertEqual(Rational
.fromsympy(sympy
.Rational(22, 7)), self
.pi
)
270 with self
.assertRaises(TypeError):
271 Rational
.fromsympy(sympy
.Symbol('x'))