4 from fractions
import Fraction
6 from pypol
.linear
import *
11 def _with_sympy(func
):
12 @functools.wraps(func
)
17 def _with_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
35 def test_new_subclass(self
):
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
)
43 def test_new_types(self
):
44 with self
.assertRaises(TypeError):
45 Expression('x + y', 2)
46 self
.assertEqual(Expression({'x': 2}), Expression({self
.x
: 2}))
47 with self
.assertRaises(TypeError):
49 with self
.assertRaises(TypeError):
50 Expression({'x': '2'})
51 self
.assertEqual(Expression(constant
=1), Expression(constant
=self
.one
))
52 with self
.assertRaises(TypeError):
53 Expression(constant
='1')
55 def test_symbols(self
):
56 self
.assertCountEqual(self
.x
.symbols
, ['x'])
57 self
.assertCountEqual(self
.pi
.symbols
, [])
58 self
.assertCountEqual(self
.expr
.symbols
, ['x', 'y'])
60 def test_dimension(self
):
61 self
.assertEqual(self
.x
.dimension
, 1)
62 self
.assertEqual(self
.pi
.dimension
, 0)
63 self
.assertEqual(self
.expr
.dimension
, 2)
65 def test_coefficient(self
):
66 self
.assertEqual(self
.expr
.coefficient('x'), 1)
67 self
.assertEqual(self
.expr
.coefficient('y'), -2)
68 self
.assertEqual(self
.expr
.coefficient(self
.y
), -2)
69 self
.assertEqual(self
.expr
.coefficient('z'), 0)
70 with self
.assertRaises(TypeError):
71 self
.expr
.coefficient(0)
72 with self
.assertRaises(TypeError):
73 self
.expr
.coefficient(self
.expr
)
75 def test_getitem(self
):
76 self
.assertEqual(self
.expr
['x'], 1)
77 self
.assertEqual(self
.expr
['y'], -2)
78 self
.assertEqual(self
.expr
[self
.y
], -2)
79 self
.assertEqual(self
.expr
['z'], 0)
80 with self
.assertRaises(TypeError):
82 with self
.assertRaises(TypeError):
85 def test_coefficients(self
):
86 self
.assertCountEqual(self
.expr
.coefficients(), [('x', 1), ('y', -2)])
88 def test_constant(self
):
89 self
.assertEqual(self
.x
.constant
, 0)
90 self
.assertEqual(self
.pi
.constant
, Fraction(22, 7))
91 self
.assertEqual(self
.expr
.constant
, 3)
93 def test_isconstant(self
):
94 self
.assertFalse(self
.x
.isconstant())
95 self
.assertTrue(self
.pi
.isconstant())
96 self
.assertFalse(self
.expr
.isconstant())
98 def test_values(self
):
99 self
.assertCountEqual(self
.expr
.values(), [1, -2, 3])
101 def test_issymbol(self
):
102 self
.assertTrue(self
.x
.issymbol())
103 self
.assertFalse(self
.pi
.issymbol())
104 self
.assertFalse(self
.expr
.issymbol())
107 self
.assertTrue(self
.x
)
108 self
.assertFalse(self
.zero
)
109 self
.assertTrue(self
.pi
)
110 self
.assertTrue(self
.expr
)
113 self
.assertEqual(+self
.expr
, self
.expr
)
116 self
.assertEqual(-self
.expr
, -self
.x
+ 2*self
.y
- 3)
119 self
.assertEqual(self
.x
+ Fraction(22, 7), self
.x
+ self
.pi
)
120 self
.assertEqual(Fraction(22, 7) + self
.x
, self
.x
+ self
.pi
)
121 self
.assertEqual(self
.x
+ self
.x
, 2 * self
.x
)
122 self
.assertEqual(self
.expr
+ 2*self
.y
, self
.x
+ 3)
125 self
.assertEqual(self
.x
- self
.x
, 0)
126 self
.assertEqual(self
.expr
- 3, self
.x
- 2*self
.y
)
127 self
.assertEqual(0 - self
.x
, -self
.x
)
130 self
.assertEqual(self
.pi
* 7, 22)
131 self
.assertEqual(self
.expr
* 0, 0)
132 self
.assertEqual(0 * self
.expr
, 0)
133 self
.assertEqual(self
.expr
* 2, 2*self
.x
- 4*self
.y
+ 6)
136 with self
.assertRaises(ZeroDivisionError):
138 self
.assertEqual(self
.expr
/ 2, self
.x
/ 2 - self
.y
+ Fraction(3, 2))
141 self
.assertEqual(str(Expression()), '0')
142 self
.assertEqual(str(self
.x
), 'x')
143 self
.assertEqual(str(-self
.x
), '-x')
144 self
.assertEqual(str(self
.pi
), '22/7')
145 self
.assertEqual(str(self
.expr
), 'x - 2*y + 3')
148 self
.assertEqual(repr(self
.x
), "Symbol('x')")
149 self
.assertEqual(repr(self
.one
), 'Constant(1)')
150 self
.assertEqual(repr(self
.pi
), 'Constant(22, 7)')
151 self
.assertEqual(repr(self
.expr
), "Expression({'x': 1, 'y': -2}, 3)")
153 def test_fromstring(self
):
154 self
.assertEqual(Expression
.fromstring('x'), self
.x
)
155 self
.assertEqual(Expression
.fromstring('-x'), -self
.x
)
156 self
.assertEqual(Expression
.fromstring('22/7'), self
.pi
)
157 self
.assertEqual(Expression
.fromstring('x - 2y + 3'), self
.expr
)
158 self
.assertEqual(Expression
.fromstring('x - (3-1)y + 3'), self
.expr
)
159 self
.assertEqual(Expression
.fromstring('x - 2*y + 3'), self
.expr
)
162 self
.assertEqual(self
.expr
, self
.expr
)
163 self
.assertNotEqual(self
.x
, self
.y
)
164 self
.assertEqual(self
.zero
, 0)
166 def test__toint(self
):
167 self
.assertEqual((self
.x
+ self
.y
/2 + self
.z
/3)._toint
(),
168 6*self
.x
+ 3*self
.y
+ 2*self
.z
)
171 def test_fromsympy(self
):
172 sp_x
, sp_y
= sympy
.symbols('x y')
173 self
.assertEqual(Expression
.fromsympy(sp_x
), self
.x
)
174 self
.assertEqual(Expression
.fromsympy(sympy
.Rational(22, 7)), self
.pi
)
175 self
.assertEqual(Expression
.fromsympy(sp_x
- 2*sp_y
+ 3), self
.expr
)
176 with self
.assertRaises(ValueError):
177 Expression
.fromsympy(sp_x
*sp_y
)
180 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 TestConstant(unittest
.TestCase
):
190 self
.zero
= Constant(0)
191 self
.one
= Constant(1)
192 self
.pi
= Constant(Fraction(22, 7))
195 def test_fromsympy(self
):
196 self
.assertEqual(Constant
.fromsympy(sympy
.Rational(22, 7)), self
.pi
)
197 with self
.assertRaises(TypeError):
198 Constant
.fromsympy(sympy
.Symbol('x'))
201 class TestSymbol(unittest
.TestCase
):
208 self
.assertEqual(self
.x
.name
, 'x')
210 def test_symbols(self
):
211 self
.assertListEqual(list(symbols('x y')), [self
.x
, self
.y
])
212 self
.assertListEqual(list(symbols('x,y')), [self
.x
, self
.y
])
213 self
.assertListEqual(list(symbols(['x', 'y'])), [self
.x
, self
.y
])
216 def test_fromsympy(self
):
217 sp_x
= sympy
.Symbol('x')
218 self
.assertEqual(Symbol
.fromsympy(sp_x
), self
.x
)
219 with self
.assertRaises(TypeError):
220 Symbol
.fromsympy(sympy
.Rational(22, 7))
221 with self
.assertRaises(TypeError):
222 Symbol
.fromsympy(2 * sp_x
)
223 with self
.assertRaises(TypeError):
224 Symbol
.fromsympy(sp_x
*sp_x
)
227 class TestOperators(unittest
.TestCase
):
232 class TestPolyhedron(unittest
.TestCase
):