values used for toisl(some expressions not correct)
[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 = Expression({'x': 1})
13 self.y = Expression({'y': 1})
14 self.z = Expression({'z': 1})
15 self.zero = Expression(constant=0)
16 self.one = Expression(constant=1)
17 self.pi = Expression(constant=Fraction(22, 7))
18 self.expr = self.x - 2*self.y + 3
19
20 def test_new_subclass(self):
21 self.assertIsInstance(self.x, Symbol)
22 self.assertIsInstance(self.pi, Constant)
23 self.assertNotIsInstance(self.x + self.pi, Symbol)
24 self.assertNotIsInstance(self.x + self.pi, Constant)
25 xx = Expression({'x': 2})
26 self.assertNotIsInstance(xx, Symbol)
27
28 def test_new_types(self):
29 with self.assertRaises(TypeError):
30 Expression('x + y', 2)
31 self.assertEqual(Expression({'x': 2}), Expression({self.x: 2}))
32 with self.assertRaises(TypeError):
33 Expression({0: 2})
34 with self.assertRaises(TypeError):
35 Expression({'x': '2'})
36 self.assertEqual(Expression(constant=1), Expression(constant=self.one))
37 with self.assertRaises(TypeError):
38 Expression(constant='1')
39
40 def test_symbols(self):
41 self.assertCountEqual(self.x.symbols, ['x'])
42 self.assertCountEqual(self.pi.symbols, [])
43 self.assertCountEqual(self.expr.symbols, ['x', 'y'])
44
45 def test_dimension(self):
46 self.assertEqual(self.x.dimension, 1)
47 self.assertEqual(self.pi.dimension, 0)
48 self.assertEqual(self.expr.dimension, 2)
49
50 def test_coefficient(self):
51 self.assertEqual(self.expr.coefficient('x'), 1)
52 self.assertEqual(self.expr.coefficient('y'), -2)
53 self.assertEqual(self.expr.coefficient(self.y), -2)
54 self.assertEqual(self.expr.coefficient('z'), 0)
55 with self.assertRaises(TypeError):
56 self.expr.coefficient(0)
57 with self.assertRaises(TypeError):
58 self.expr.coefficient(self.expr)
59
60 def test_getitem(self):
61 self.assertEqual(self.expr['x'], 1)
62 self.assertEqual(self.expr['y'], -2)
63 self.assertEqual(self.expr[self.y], -2)
64 self.assertEqual(self.expr['z'], 0)
65 with self.assertRaises(TypeError):
66 self.expr[0]
67 with self.assertRaises(TypeError):
68 self.expr[self.expr]
69
70 def test_coefficients(self):
71 self.assertCountEqual(self.expr.coefficients(), [('x', 1), ('y', -2)])
72
73 def test_constant(self):
74 self.assertEqual(self.x.constant, 0)
75 self.assertEqual(self.pi.constant, Fraction(22, 7))
76 self.assertEqual(self.expr.constant, 3)
77
78 def test_isconstant(self):
79 self.assertFalse(self.x.isconstant())
80 self.assertTrue(self.pi.isconstant())
81 self.assertFalse(self.expr.isconstant())
82
83 def test_values(self):
84 self.assertCountEqual(self.expr.values(), [1, -2, 3])
85
86 def test_symbol(self):
87 self.assertEqual(self.x.symbol, 'x')
88 with self.assertRaises(ValueError):
89 self.pi.symbol
90 with self.assertRaises(ValueError):
91 self.expr.symbol
92
93 def test_issymbol(self):
94 self.assertTrue(self.x.issymbol())
95 self.assertFalse(self.pi.issymbol())
96 self.assertFalse(self.expr.issymbol())
97
98 def test_bool(self):
99 self.assertTrue(self.x)
100 self.assertFalse(self.zero)
101 self.assertTrue(self.pi)
102 self.assertTrue(self.expr)
103
104 def test_pos(self):
105 self.assertEqual(+self.expr, self.expr)
106
107 def test_neg(self):
108 self.assertEqual(-self.expr, -self.x + 2*self.y - 3)
109
110 def test_add(self):
111 self.assertEqual(self.x + Fraction(22, 7), self.x + self.pi)
112 self.assertEqual(Fraction(22, 7) + self.x, self.x + self.pi)
113 self.assertEqual(self.x + self.x, 2 * self.x)
114 self.assertEqual(self.expr + 2*self.y, self.x + 3)
115
116 def test_sub(self):
117 self.assertEqual(self.x - self.x, 0)
118 self.assertEqual(self.expr - 3, self.x - 2*self.y)
119 self.assertEqual(0 - self.x, -self.x)
120
121 def test_mul(self):
122 self.assertEqual(self.pi * 7, 22)
123 self.assertEqual(self.expr * 0, 0)
124 self.assertEqual(0 * self.expr, 0)
125 self.assertEqual(self.expr * 2, 2*self.x - 4*self.y + 6)
126
127 def test_div(self):
128 with self.assertRaises(ZeroDivisionError):
129 self.expr / 0
130 self.assertEqual(self.expr / 2, self.x / 2 - self.y + Fraction(3, 2))
131
132 def test_str(self):
133 self.assertEqual(str(Expression()), '0')
134 self.assertEqual(str(self.x), 'x')
135 self.assertEqual(str(-self.x), '-x')
136 self.assertEqual(str(self.pi), '22/7')
137 self.assertEqual(str(self.expr), 'x - 2*y + 3')
138
139 def test_repr(self):
140 self.assertEqual(repr(self.x), "Symbol('x')")
141 self.assertEqual(repr(self.one), 'Constant(1)')
142 self.assertEqual(repr(self.expr), "Expression({'x': 1, 'y': -2}, 3)")
143
144 @unittest.expectedFailure
145 def test_fromstring(self):
146 self.assertEqual(Expression.fromstring('x'), self.x)
147 self.assertEqual(Expression.fromstring('-x'), -self.x)
148 self.assertEqual(Expression.fromstring('22/7'), self.pi)
149 self.assertEqual(Expression.fromstring('x - 2y + 3'), self.expr)
150 self.assertEqual(Expression.fromstring('x - (3-1)y + 3'), self.expr)
151 self.assertEqual(Expression.fromstring('x - 2*y + 3'), self.expr)
152
153 def test_eq(self):
154 self.assertEqual(self.expr, self.expr)
155 self.assertNotEqual(self.x, self.y)
156 self.assertEqual(self.zero, 0)
157
158 def test__toint(self):
159 self.assertEqual((self.x + self.y/2 + self.z/3)._toint(),
160 6*self.x + 3*self.y + 2*self.z)
161
162
163 class TestConstant(unittest.TestCase):
164
165 pass
166
167
168 class TestSymbol(unittest.TestCase):
169
170 def setUp(self):
171 self.x = Symbol('x')
172 self.y = Symbol('y')
173
174 def test_symbols(self):
175 self.assertListEqual(list(symbols('x y')), [self.x, self.y])
176 self.assertListEqual(list(symbols('x,y')), [self.x, self.y])
177 self.assertListEqual(list(symbols(['x', 'y'])), [self.x, self.y])
178
179
180 class TestOperators(unittest.TestCase):
181
182 pass
183
184
185 class TestPolyhedron(unittest.TestCase):
186
187 pass