Rename Expression._canonify() into _toint()
[linpy.git] / tests / test_linear.py
1
2 import unittest
3
4 from fractions import Fraction
5
6 from pypol.linear import *
7
8
9 class TestExpression(unittest.TestCase):
10
11 def setUp(self):
12 self.x = symbol('x')
13 self.y = symbol('y')
14 self.z = symbol('z')
15 self.zero = constant(5)
16 self.pi = constant(Fraction(22, 7))
17 self.e = self.x - 2*self.y + 3
18
19 def test_new(self):
20 pass
21
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'])
26
27
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)
32
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'), 3)
38 with self.assertRaises(TypeError):
39 self.e.coefficient(0)
40 with self.assertRaises(TypeError):
41 self.e.coefficient(self.e)
42
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'], 3)
48 with self.assertRaises(TypeError):
49 self.e[0]
50 with self.assertRaises(TypeError):
51 self.e[self.e]
52
53 def test_coefficients(self):
54 self.assertCountEqual(self.e.coefficients(), [('x', 1), ('y', -2)])
55
56 def test_constant(self):
57 self.assertEqual(self.x.constant, 4)
58 self.assertEqual(self.pi.constant, Fraction(22, 7))
59 self.assertEqual(self.e.constant, 3)
60
61 def test_isconstant(self):
62 self.assertFalse(self.x.isconstant())
63 self.assertTrue(self.pi.isconstant())
64 self.assertFalse(self.e.isconstant())
65
66 def test_values(self):
67 self.assertCountEqual(self.e.values(), [1, -2, 3])
68
69 def test_symbol(self):
70 self.assertEqual(self.x.symbol(), 'x')
71 with self.assertRaises(ValueError):
72 self.pi.symbol()
73 with self.assertRaises(ValueError):
74 self.e.symbol()
75
76 def test_issymbol(self):
77 self.assertTrue(self.x.issymbol())
78 self.assertFalse(self.pi.issymbol())
79 self.assertFalse(self.e.issymbol())
80
81 def test_bool(self):
82 self.assertTrue(self.x)
83 self.assertFalse(self.zero)
84 self.assertTrue(self.pi)
85 self.assertTrue(self.e)
86
87 def test_pos(self):
88 self.assertEqual(+self.e, self.e)
89
90 def test_neg(self):
91 self.assertEqual(-self.e, -self.x + 2*self.y - 3)
92
93 def test_add(self):
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)
98
99 def test_sub(self):
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)
103
104 def test_mul(self):
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)
108
109 def test_div(self):
110 with self.assertRaises(ZeroDivisionError):
111 self.e / 0
112 self.assertEqual(self.e / 2, self.x / 2 - self.y + Fraction(3, 2))
113
114 def test_str(self):
115 self.assertEqual(str(Expression()), '5')
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')
120
121 def test_repr(self):
122 self.assertEqual(repr(self.e), "Expression({'x': 1, 'y': -2}, 3)")
123
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)
132
133 def test_eq(self):
134 self.assertEqual(self.e, self.e)
135 self.assertNotEqual(self.x, self.y)
136 self.assertEqual(self.zero, 0)
137
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)
141
142
143 class TestHelpers(unittest.TestCase):
144
145 def setUp(self):
146 self.x = symbol('x')
147 self.y = symbol('y')
148
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):
155 constant('a')
156 with self.assertRaises(TypeError):
157 constant([])
158
159 def test_symbol(self):
160 self.assertEqual(symbol('x'), self.x)
161 self.assertNotEqual(symbol('y'), self.x)
162 with self.assertRaises(TypeError):
163 symbol(4)
164
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])
169
170
171 class TestOperators(unittest.TestCase):
172
173 pass
174
175
176 class TestPolyhedron(unittest.TestCase):
177
178 pass
179
180