3 from fractions
import Fraction
5 from pypol
.linear
import *
8 class TestExpression(unittest
.TestCase
):
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
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
)
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):
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')
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'])
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)
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
)
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):
66 with self
.assertRaises(TypeError):
69 def test_coefficients(self
):
70 self
.assertCountEqual(self
.expr
.coefficients(), [('x', 1), ('y', -2)])
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)
77 def test_isconstant(self
):
78 self
.assertFalse(self
.x
.isconstant())
79 self
.assertTrue(self
.pi
.isconstant())
80 self
.assertFalse(self
.expr
.isconstant())
82 def test_values(self
):
83 self
.assertCountEqual(self
.expr
.values(), [1, -2, 3])
85 def test_issymbol(self
):
86 self
.assertTrue(self
.x
.issymbol())
87 self
.assertFalse(self
.pi
.issymbol())
88 self
.assertFalse(self
.expr
.issymbol())
91 self
.assertTrue(self
.x
)
92 self
.assertFalse(self
.zero
)
93 self
.assertTrue(self
.pi
)
94 self
.assertTrue(self
.expr
)
97 self
.assertEqual(+self
.expr
, self
.expr
)
100 self
.assertEqual(-self
.expr
, -self
.x
+ 2*self
.y
- 3)
103 self
.assertEqual(self
.x
+ Fraction(22, 7), self
.x
+ self
.pi
)
104 self
.assertEqual(Fraction(22, 7) + self
.x
, self
.x
+ self
.pi
)
105 self
.assertEqual(self
.x
+ self
.x
, 2 * self
.x
)
106 self
.assertEqual(self
.expr
+ 2*self
.y
, self
.x
+ 3)
109 self
.assertEqual(self
.x
- self
.x
, 0)
110 self
.assertEqual(self
.expr
- 3, self
.x
- 2*self
.y
)
111 self
.assertEqual(0 - self
.x
, -self
.x
)
114 self
.assertEqual(self
.pi
* 7, 22)
115 self
.assertEqual(self
.expr
* 0, 0)
116 self
.assertEqual(0 * self
.expr
, 0)
117 self
.assertEqual(self
.expr
* 2, 2*self
.x
- 4*self
.y
+ 6)
120 with self
.assertRaises(ZeroDivisionError):
122 self
.assertEqual(self
.expr
/ 2, self
.x
/ 2 - self
.y
+ Fraction(3, 2))
125 self
.assertEqual(str(Expression()), '0')
126 self
.assertEqual(str(self
.x
), 'x')
127 self
.assertEqual(str(-self
.x
), '-x')
128 self
.assertEqual(str(self
.pi
), '22/7')
129 self
.assertEqual(str(self
.expr
), 'x - 2*y + 3')
132 self
.assertEqual(repr(self
.x
), "Symbol('x')")
133 self
.assertEqual(repr(self
.one
), 'Constant(1)')
134 self
.assertEqual(repr(self
.expr
), "Expression({'x': 1, 'y': -2}, 3)")
136 @unittest.expectedFailure
137 def test_fromstring(self
):
138 self
.assertEqual(Expression
.fromstring('x'), self
.x
)
139 self
.assertEqual(Expression
.fromstring('-x'), -self
.x
)
140 self
.assertEqual(Expression
.fromstring('22/7'), self
.pi
)
141 self
.assertEqual(Expression
.fromstring('x - 2y + 3'), self
.expr
)
142 self
.assertEqual(Expression
.fromstring('x - (3-1)y + 3'), self
.expr
)
143 self
.assertEqual(Expression
.fromstring('x - 2*y + 3'), self
.expr
)
146 self
.assertEqual(self
.expr
, self
.expr
)
147 self
.assertNotEqual(self
.x
, self
.y
)
148 self
.assertEqual(self
.zero
, 0)
150 def test__toint(self
):
151 self
.assertEqual((self
.x
+ self
.y
/2 + self
.z
/3)._toint
(),
152 6*self
.x
+ 3*self
.y
+ 2*self
.z
)
155 class TestConstant(unittest
.TestCase
):
160 class TestSymbol(unittest
.TestCase
):
167 self
.assertEqual(self
.x
.name
, 'x')
169 def test_symbols(self
):
170 self
.assertListEqual(list(symbols('x y')), [self
.x
, self
.y
])
171 self
.assertListEqual(list(symbols('x,y')), [self
.x
, self
.y
])
172 self
.assertListEqual(list(symbols(['x', 'y'])), [self
.x
, self
.y
])
175 class TestOperators(unittest
.TestCase
):
180 class TestPolyhedron(unittest
.TestCase
):