4 from fractions
import Fraction
6 from pypol
.linear
import *
9 class TestExpression(unittest
.TestCase
):
15 self
.zero
= constant(5)
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'])
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)
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'), 0)
38 with self
.assertRaises(TypeError):
40 with self
.assertRaises(TypeError):
41 self
.e
.coefficient(self
.e
)
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'], 0)
48 with self
.assertRaises(TypeError):
50 with self
.assertRaises(TypeError):
53 def test_coefficients(self
):
54 self
.assertCountEqual(self
.e
.coefficients(), [('x', 1), ('y', -2)])
56 def test_constant(self
):
57 self
.assertEqual(self
.x
.constant
, 0)
58 self
.assertEqual(self
.pi
.constant
, Fraction(22, 7))
59 self
.assertEqual(self
.e
.constant
, 3)
61 def test_isconstant(self
):
62 self
.assertFalse(self
.x
.isconstant())
63 self
.assertTrue(self
.pi
.isconstant())
64 self
.assertFalse(self
.e
.isconstant())
66 def test_values(self
):
67 self
.assertCountEqual(self
.e
.values(), [1, -2, 3])
69 def test_symbol(self
):
70 self
.assertEqual(self
.x
.symbol(), 'x')
71 with self
.assertRaises(ValueError):
73 with self
.assertRaises(ValueError):
76 def test_issymbol(self
):
77 self
.assertTrue(self
.x
.issymbol())
78 self
.assertFalse(self
.pi
.issymbol())
79 self
.assertFalse(self
.e
.issymbol())
82 self
.assertTrue(self
.x
)
83 self
.assertFalse(self
.zero
)
84 self
.assertTrue(self
.pi
)
85 self
.assertTrue(self
.e
)
88 self
.assertEqual(+self
.e
, self
.e
)
91 self
.assertEqual(-self
.e
, -self
.x
+ 2*self
.y
- 3)
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)
100 self
.assertEqual(self
.x
- self
.x
, 0)
101 self
.assertEqual(self
.e
- 3, self
.x
- 2*self
.y
)
104 self
.assertEqual(self
.pi
* 7, 22)
105 self
.assertEqual(self
.e
* 0, 0)
106 self
.assertEqual(self
.e
* 2, 2*self
.x
- 4*self
.y
+ 6)
109 with self
.assertRaises(ZeroDivisionError):
111 self
.assertEqual(self
.e
/ 2, self
.x
/ 2 - self
.y
+ Fraction(3, 2))
114 self
.assertEqual(str(Expression()), '0')
115 self
.assertEqual(str(self
.x
), 'x')
116 self
.assertEqual(str(-self
.x
), '-x')
117 self
.assertEqual(str(self
.pi
), '22/7')
118 self
.assertEqual(str(self
.e
), 'x - 2*y + 3')
121 self
.assertEqual(repr(self
.e
), "Expression({'x': 1, 'y': -2}, 3)")
123 @unittest.expectedFailure
124 def test_fromstring(self
):
125 self
.assertEqual(Expression
.fromstring('x'), self
.x
)
126 self
.assertEqual(Expression
.fromstring('-x'), -self
.x
)
127 self
.assertEqual(Expression
.fromstring('22/7'), self
.pi
)
128 self
.assertEqual(Expression
.fromstring('x - 2y + 3'), self
.e
)
129 self
.assertEqual(Expression
.fromstring('x - (3-1)y + 3'), self
.e
)
130 self
.assertEqual(Expression
.fromstring('x - 2*y + 3'), self
.e
)
133 self
.assertEqual(self
.e
, self
.e
)
134 self
.assertNotEqual(self
.x
, self
.y
)
135 self
.assertEqual(self
.zero
, 0)
137 def test_canonify(self
):
138 self
.assertEqual((self
.x
+ self
.y
/2 + self
.z
/3)._canonify
(),
139 6*self
.x
+ 3*self
.y
+ 2*self
.z
)
142 class TestHelpers(unittest
.TestCase
):
148 def test_constant(self
):
149 self
.assertEqual(constant(3), 3)
150 self
.assertEqual(constant('3'), 3)
151 self
.assertEqual(constant(Fraction(3, 4)), Fraction(3, 4))
152 self
.assertEqual(constant('3/4'), Fraction(3, 4))
153 with self
.assertRaises(ValueError):
155 with self
.assertRaises(TypeError):
158 def test_symbol(self
):
159 self
.assertEqual(symbol('x'), self
.x
)
160 self
.assertNotEqual(symbol('y'), self
.x
)
161 with self
.assertRaises(TypeError):
164 def test_symbols(self
):
165 self
.assertListEqual(list(symbols('x y')), [self
.x
, self
.y
])
166 self
.assertListEqual(list(symbols('x,y')), [self
.x
, self
.y
])
167 self
.assertListEqual(list(symbols(['x', 'y'])), [self
.x
, self
.y
])
170 class TestOperators(unittest
.TestCase
):
175 class TestPolyhedron(unittest
.TestCase
):