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