4 from fractions
import Fraction
6 from pypol
.linear
import *
9 class TestExpression(unittest
.TestCase
):
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
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
)
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):
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')
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'])
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)
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
)
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):
67 with self
.assertRaises(TypeError):
70 def test_coefficients(self
):
71 self
.assertCountEqual(self
.expr
.coefficients(), [('x', 1), ('y', -2)])
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)
78 def test_isconstant(self
):
79 self
.assertFalse(self
.x
.isconstant())
80 self
.assertTrue(self
.pi
.isconstant())
81 self
.assertFalse(self
.expr
.isconstant())
83 def test_values(self
):
84 self
.assertCountEqual(self
.expr
.values(), [1, -2, 3])
86 def test_symbol(self
):
87 self
.assertEqual(self
.x
.symbol
, 'x')
88 with self
.assertRaises(ValueError):
90 with self
.assertRaises(ValueError):
93 def test_issymbol(self
):
94 self
.assertTrue(self
.x
.issymbol())
95 self
.assertFalse(self
.pi
.issymbol())
96 self
.assertFalse(self
.expr
.issymbol())
99 self
.assertTrue(self
.x
)
100 self
.assertFalse(self
.zero
)
101 self
.assertTrue(self
.pi
)
102 self
.assertTrue(self
.expr
)
105 self
.assertEqual(+self
.expr
, self
.expr
)
108 self
.assertEqual(-self
.expr
, -self
.x
+ 2*self
.y
- 3)
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)
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
)
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)
128 with self
.assertRaises(ZeroDivisionError):
130 self
.assertEqual(self
.expr
/ 2, self
.x
/ 2 - self
.y
+ Fraction(3, 2))
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')
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)")
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
)
154 self
.assertEqual(self
.expr
, self
.expr
)
155 self
.assertNotEqual(self
.x
, self
.y
)
156 self
.assertEqual(self
.zero
, 0)
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
)
163 class TestConstant(unittest
.TestCase
):
168 class TestSymbol(unittest
.TestCase
):
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
])
180 class TestOperators(unittest
.TestCase
):
185 class TestPolyhedron(unittest
.TestCase
):