4079d7501fe280321955761d04ef95af275288b6
4 from fractions
import Fraction
6 from pypol
.linear
import *
9 class TestExpression(unittest
.TestCase
):
15 self
.zero
= constant(5)
16 self
.pi
= constant(Fraction(22, 7))
17 self
.e
= self
.x
- 2*self
.y
+ 3
22 def test_symbols(self
):
23 self
.assertCountEqual(self
.x
.symbols(), ['x'])
24 self
.assertCountEqual(self
.pi
.symbols(), [])
25 self
.assertCountEqual(self
.e
.symbols(), ['x', 'y'])
28 def test_dimension(self
):
29 self
.assertEqual(self
.x
.dimension
, 1)
30 self
.assertEqual(self
.pi
.dimension
, 0)
31 self
.assertEqual(self
.e
.dimension
, 2)
33 def test_coefficient(self
):
34 self
.assertEqual(self
.e
.coefficient('x'), 1)
35 self
.assertEqual(self
.e
.coefficient('y'), -2)
36 self
.assertEqual(self
.e
.coefficient(self
.y
), -2)
37 self
.assertEqual(self
.e
.coefficient('z'), 0)
38 with self
.assertRaises(TypeError):
40 with self
.assertRaises(TypeError):
41 self
.e
.coefficient(self
.e
)
43 def test_getitem(self
):
44 self
.assertEqual(self
.e
['x'], 1)
45 self
.assertEqual(self
.e
['y'], -2)
46 self
.assertEqual(self
.e
[self
.y
], -2)
47 self
.assertEqual(self
.e
['z'], 0)
48 with self
.assertRaises(TypeError):
50 with self
.assertRaises(TypeError):
53 def test_coefficients(self
):
54 self
.assertCountEqual(self
.e
.coefficients(), [('x', 1), ('y', -2)])
56 def test_constant(self
):
57 self
.assertEqual(self
.x
.constant
, 0)
58 self
.assertEqual(self
.pi
.constant
, Fraction(22, 7))
59 self
.assertEqual(self
.e
.constant
, 3)
61 def test_isconstant(self
):
62 self
.assertFalse(self
.x
.isconstant())
63 self
.assertTrue(self
.pi
.isconstant())
64 self
.assertFalse(self
.e
.isconstant())
66 def test_values(self
):
67 self
.assertCountEqual(self
.e
.values(), [1, -2, 3])
69 def test_symbol(self
):
70 self
.assertEqual(self
.x
.symbol(), 'x')
71 with self
.assertRaises(ValueError):
73 with self
.assertRaises(ValueError):
76 def test_issymbol(self
):
77 self
.assertTrue(self
.x
.issymbol())
78 self
.assertFalse(self
.pi
.issymbol())
79 self
.assertFalse(self
.e
.issymbol())
82 self
.assertTrue(self
.x
)
83 self
.assertFalse(self
.zero
)
84 self
.assertTrue(self
.pi
)
85 self
.assertTrue(self
.e
)
88 self
.assertEqual(+self
.e
, self
.e
)
91 self
.assertEqual(-self
.e
, -self
.x
+ 2*self
.y
- 3)
94 self
.assertEqual(self
.x
+ Fraction(22, 7), self
.x
+ self
.pi
)
95 self
.assertEqual(Fraction(22, 7) + self
.x
, self
.x
+ self
.pi
)
96 self
.assertEqual(self
.x
+ self
.x
, 2 * self
.x
)
97 self
.assertEqual(self
.e
+ 2*self
.y
, self
.x
+ 3)
100 self
.assertEqual(self
.x
- self
.x
, 0)
101 self
.assertEqual(self
.e
- 3, self
.x
- 2*self
.y
)
102 self
.assertEqual(0 - self
.x
, -self
.x
)
105 self
.assertEqual(self
.pi
* 7, 22)
106 self
.assertEqual(self
.e
* 0, 0)
107 self
.assertEqual(self
.e
* 2, 2*self
.x
- 4*self
.y
+ 6)
110 with self
.assertRaises(ZeroDivisionError):
112 self
.assertEqual(self
.e
/ 2, self
.x
/ 2 - self
.y
+ Fraction(3, 2))
115 self
.assertEqual(str(Expression()), '0')
116 self
.assertEqual(str(self
.x
), 'x')
117 self
.assertEqual(str(-self
.x
), '-x')
118 self
.assertEqual(str(self
.pi
), '22/7')
119 self
.assertEqual(str(self
.e
), 'x - 2*y + 3')
122 self
.assertEqual(repr(self
.e
), "Expression({'x': 1, 'y': -2}, 3)")
124 @unittest.expectedFailure
125 def test_fromstring(self
):
126 self
.assertEqual(Expression
.fromstring('x'), self
.x
)
127 self
.assertEqual(Expression
.fromstring('-x'), -self
.x
)
128 self
.assertEqual(Expression
.fromstring('22/7'), self
.pi
)
129 self
.assertEqual(Expression
.fromstring('x - 2y + 3'), self
.e
)
130 self
.assertEqual(Expression
.fromstring('x - (3-1)y + 3'), self
.e
)
131 self
.assertEqual(Expression
.fromstring('x - 2*y + 3'), self
.e
)
134 self
.assertEqual(self
.e
, self
.e
)
135 self
.assertNotEqual(self
.x
, self
.y
)
136 self
.assertEqual(self
.zero
, 0)
138 def test_canonify(self
):
139 self
.assertEqual((self
.x
+ self
.y
/2 + self
.z
/3)._canonify
(),
140 6*self
.x
+ 3*self
.y
+ 2*self
.z
)
143 class TestHelpers(unittest
.TestCase
):
149 def test_constant(self
):
150 self
.assertEqual(constant(3), 3)
151 self
.assertEqual(constant('3'), 3)
152 self
.assertEqual(constant(Fraction(3, 4)), Fraction(3, 4))
153 self
.assertEqual(constant('3/4'), Fraction(3, 4))
154 with self
.assertRaises(ValueError):
156 with self
.assertRaises(TypeError):
159 def test_symbol(self
):
160 self
.assertEqual(symbol('x'), self
.x
)
161 self
.assertNotEqual(symbol('y'), self
.x
)
162 with self
.assertRaises(TypeError):
165 def test_symbols(self
):
166 self
.assertListEqual(list(symbols('x y')), [self
.x
, self
.y
])
167 self
.assertListEqual(list(symbols('x,y')), [self
.x
, self
.y
])
168 self
.assertListEqual(list(symbols(['x', 'y'])), [self
.x
, self
.y
])
171 class TestOperators(unittest
.TestCase
):
176 class TestPolyhedron(unittest
.TestCase
):