4 from fractions
import Fraction
6 from pypol
.linear
import *
9 class TestExpression(unittest
.TestCase
):
15 self
.zero
= constant(0)
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'])
27 def test_dimension(self
):
28 self
.assertEqual(self
.x
.dimension
, 1)
29 self
.assertEqual(self
.pi
.dimension
, 0)
30 self
.assertEqual(self
.e
.dimension
, 2)
32 def test_coefficient(self
):
33 self
.assertEqual(self
.e
.coefficient('x'), 1)
34 self
.assertEqual(self
.e
.coefficient('y'), -2)
35 self
.assertEqual(self
.e
.coefficient(self
.y
), -2)
36 self
.assertEqual(self
.e
.coefficient('z'), 0)
37 with self
.assertRaises(TypeError):
39 with self
.assertRaises(TypeError):
40 self
.e
.coefficient(self
.e
)
42 def test_getitem(self
):
43 self
.assertEqual(self
.e
['x'], 1)
44 self
.assertEqual(self
.e
['y'], -2)
45 self
.assertEqual(self
.e
[self
.y
], -2)
46 self
.assertEqual(self
.e
['z'], 0)
47 with self
.assertRaises(TypeError):
49 with self
.assertRaises(TypeError):
52 def test_coefficients(self
):
53 self
.assertCountEqual(self
.e
.coefficients(), [('x', 1), ('y', -2)])
55 def test_constant(self
):
56 self
.assertEqual(self
.x
.constant
, 0)
57 self
.assertEqual(self
.pi
.constant
, Fraction(22, 7))
58 self
.assertEqual(self
.e
.constant
, 3)
60 def test_isconstant(self
):
61 self
.assertFalse(self
.x
.isconstant())
62 self
.assertTrue(self
.pi
.isconstant())
63 self
.assertFalse(self
.e
.isconstant())
65 def test_values(self
):
66 self
.assertCountEqual(self
.e
.values(), [1, -2, 3])
68 def test_symbol(self
):
69 self
.assertEqual(self
.x
.symbol(), 'x')
70 with self
.assertRaises(ValueError):
72 with self
.assertRaises(ValueError):
75 def test_issymbol(self
):
76 self
.assertTrue(self
.x
.issymbol())
77 self
.assertFalse(self
.pi
.issymbol())
78 self
.assertFalse(self
.e
.issymbol())
81 self
.assertTrue(self
.x
)
82 self
.assertFalse(self
.zero
)
83 self
.assertTrue(self
.pi
)
84 self
.assertTrue(self
.e
)
87 self
.assertEqual(+self
.e
, self
.e
)
90 self
.assertEqual(-self
.e
, -self
.x
+ 2*self
.y
- 3)
93 self
.assertEqual(self
.x
+ Fraction(22, 7), self
.x
+ self
.pi
)
94 self
.assertEqual(Fraction(22, 7) + self
.x
, self
.x
+ self
.pi
)
95 self
.assertEqual(self
.x
+ self
.x
, 2 * self
.x
)
96 self
.assertEqual(self
.e
+ 2*self
.y
, self
.x
+ 3)
99 self
.assertEqual(self
.x
- self
.x
, 0)
100 self
.assertEqual(self
.e
- 3, self
.x
- 2*self
.y
)
103 self
.assertEqual(self
.pi
* 7, 22)
104 self
.assertEqual(self
.e
* 0, 0)
105 self
.assertEqual(self
.e
* 2, 2*self
.x
- 4*self
.y
+ 6)
108 with self
.assertRaises(ZeroDivisionError):
110 self
.assertEqual(self
.e
/ 2, self
.x
/ 2 - self
.y
+ Fraction(3, 2))
113 self
.assertEqual(str(self
.x
), 'x')
114 self
.assertEqual(str(-self
.x
), '-x')
115 self
.assertEqual(str(self
.pi
), '22/7')
116 self
.assertEqual(str(self
.e
), 'x - 2*y + 3')
119 self
.assertEqual(repr(self
.e
), "Expression({'x': 1, 'y': -2}, 3)")
121 @unittest.expectedFailure
122 def test_fromstring(self
):
123 self
.assertEqual(Expression
.fromstring('x'), self
.x
)
124 self
.assertEqual(Expression
.fromstring('-x'), -self
.x
)
125 self
.assertEqual(Expression
.fromstring('22/7'), self
.pi
)
126 self
.assertEqual(Expression
.fromstring('x - 2y + 3'), self
.e
)
127 self
.assertEqual(Expression
.fromstring('x - (3-1)y + 3'), self
.e
)
128 self
.assertEqual(Expression
.fromstring('x - 2*y + 3'), self
.e
)
131 self
.assertEqual(self
.e
, self
.e
)
132 self
.assertNotEqual(self
.x
, self
.y
)
133 self
.assertEqual(self
.zero
, 0)
135 def test_canonify(self
):
136 self
.assertEqual((self
.x
+ self
.y
/2 + self
.z
/3)._canonify
(),
137 6*self
.x
+ 3*self
.y
+ 2*self
.z
)
140 class TestHelpers(unittest
.TestCase
):
146 def test_constant(self
):
147 self
.assertEqual(constant(3), 3)
148 self
.assertEqual(constant('3'), 3)
149 self
.assertEqual(constant(Fraction(3, 4)), Fraction(3, 4))
150 self
.assertEqual(constant('3/4'), Fraction(3, 4))
151 with self
.assertRaises(ValueError):
153 with self
.assertRaises(TypeError):
156 def test_symbol(self
):
157 self
.assertEqual(symbol('x'), self
.x
)
158 self
.assertNotEqual(symbol('y'), self
.x
)
159 with self
.assertRaises(TypeError):
162 def test_symbols(self
):
163 self
.assertListEqual(list(symbols('x y')), [self
.x
, self
.y
])
164 self
.assertListEqual(list(symbols('x,y')), [self
.x
, self
.y
])
165 self
.assertListEqual(list(symbols(['x', 'y'])), [self
.x
, self
.y
])
168 class TestOperators(unittest
.TestCase
):
173 class TestPolyhedron(unittest
.TestCase
):