4 from fractions
import Fraction
6 from pypol
.linear
import *
9 class TestExpression(unittest
.TestCase
):
15 self
.zero
= constant(0)
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'])
27 def test_dimension(self
):
28 self
.assertEqual(self
.x
.dimension
, 1)
29 self
.assertEqual(self
.pi
.dimension
, 0)
30 self
.assertEqual(self
.e
.dimension
, 2)
32 def test_coefficient(self
):
33 self
.assertEqual(self
.e
.coefficient('x'), 1)
34 self
.assertEqual(self
.e
.coefficient('y'), -2)
35 self
.assertEqual(self
.e
.coefficient(self
.y
), -2)
36 self
.assertEqual(self
.e
.coefficient('z'), 0)
37 with self
.assertRaises(TypeError):
39 with self
.assertRaises(TypeError):
40 self
.e
.coefficient(self
.e
)
42 def test_getitem(self
):
43 self
.assertEqual(self
.e
['x'], 1)
44 self
.assertEqual(self
.e
['y'], -2)
45 self
.assertEqual(self
.e
[self
.y
], -2)
46 self
.assertEqual(self
.e
['z'], 0)
47 with self
.assertRaises(TypeError):
49 with self
.assertRaises(TypeError):
52 def test_coefficients(self
):
53 self
.assertCountEqual(self
.e
.coefficients(), [('x', 1), ('y', -2)])
55 def test_constant(self
):
56 self
.assertEqual(self
.x
.constant
, 0)
57 self
.assertEqual(self
.pi
.constant
, Fraction(22, 7))
58 self
.assertEqual(self
.e
.constant
, 3)
60 def test_isconstant(self
):
61 self
.assertFalse(self
.x
.isconstant())
62 self
.assertTrue(self
.pi
.isconstant())
63 self
.assertFalse(self
.e
.isconstant())
65 def test_values(self
):
66 self
.assertCountEqual(self
.e
.values(), [1, -2, 3])
68 def test_symbol(self
):
69 self
.assertEqual(self
.x
.symbol(), 'x')
70 with self
.assertRaises(ValueError):
72 with self
.assertRaises(ValueError):
75 def test_issymbol(self
):
76 self
.assertTrue(self
.x
.issymbol())
77 self
.assertFalse(self
.pi
.issymbol())
78 self
.assertFalse(self
.e
.issymbol())
81 self
.assertTrue(self
.x
)
82 self
.assertFalse(self
.zero
)
83 self
.assertTrue(self
.pi
)
84 self
.assertTrue(self
.e
)
87 self
.assertEqual(+self
.e
, self
.e
)
90 self
.assertEqual(-self
.e
, -self
.x
+ 2*self
.y
- 3)
93 self
.assertEqual(self
.x
+ Fraction(22, 7), self
.x
+ self
.pi
)
94 self
.assertEqual(Fraction(22, 7) + self
.x
, self
.x
+ self
.pi
)
95 self
.assertEqual(self
.x
+ self
.x
, 2 * self
.x
)
96 self
.assertEqual(self
.e
+ 2*self
.y
, self
.x
+ 3)
99 self
.assertEqual(self
.x
- self
.x
, 0)
100 self
.assertEqual(self
.e
- 3, self
.x
- 2*self
.y
)
103 self
.assertEqual(self
.pi
* 7, 22)
104 self
.assertEqual(self
.e
* 0, 0)
105 self
.assertEqual(self
.e
* 2, 2*self
.x
- 4*self
.y
+ 6)
108 with self
.assertRaises(ZeroDivisionError):
110 self
.assertEqual(self
.e
/ 2, self
.x
/ 2 - self
.y
+ Fraction(3, 2))
113 self
.assertEqual(str(Expression()), '0')
114 self
.assertEqual(str(self
.x
), 'x')
115 self
.assertEqual(str(-self
.x
), '-x')
116 self
.assertEqual(str(self
.pi
), '22/7')
117 self
.assertEqual(str(self
.e
), 'x - 2*y + 3')
120 self
.assertEqual(repr(self
.e
), "Expression({'x': 1, 'y': -2}, 3)")
122 @unittest.expectedFailure
123 def test_fromstring(self
):
124 self
.assertEqual(Expression
.fromstring('x'), self
.x
)
125 self
.assertEqual(Expression
.fromstring('-x'), -self
.x
)
126 self
.assertEqual(Expression
.fromstring('22/7'), self
.pi
)
127 self
.assertEqual(Expression
.fromstring('x - 2y + 3'), self
.e
)
128 self
.assertEqual(Expression
.fromstring('x - (3-1)y + 3'), self
.e
)
129 self
.assertEqual(Expression
.fromstring('x - 2*y + 3'), self
.e
)
132 self
.assertEqual(self
.e
, self
.e
)
133 self
.assertNotEqual(self
.x
, self
.y
)
134 self
.assertEqual(self
.zero
, 0)
136 def test_canonify(self
):
137 self
.assertEqual((self
.x
+ self
.y
/2 + self
.z
/3)._canonify
(),
138 6*self
.x
+ 3*self
.y
+ 2*self
.z
)
141 class TestHelpers(unittest
.TestCase
):
147 def test_constant(self
):
148 self
.assertEqual(constant(3), 3)
149 self
.assertEqual(constant('3'), 3)
150 self
.assertEqual(constant(Fraction(3, 4)), Fraction(3, 4))
151 self
.assertEqual(constant('3/4'), Fraction(3, 4))
152 with self
.assertRaises(ValueError):
154 with self
.assertRaises(TypeError):
157 def test_symbol(self
):
158 self
.assertEqual(symbol('x'), self
.x
)
159 self
.assertNotEqual(symbol('y'), self
.x
)
160 with self
.assertRaises(TypeError):
163 def test_symbols(self
):
164 self
.assertListEqual(list(symbols('x y')), [self
.x
, self
.y
])
165 self
.assertListEqual(list(symbols('x,y')), [self
.x
, self
.y
])
166 self
.assertListEqual(list(symbols(['x', 'y'])), [self
.x
, self
.y
])
169 class TestOperators(unittest
.TestCase
):
174 class TestPolyhedron(unittest
.TestCase
):