-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinequality.py
98 lines (81 loc) · 2.78 KB
/
inequality.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
class NonMinusVariable:
def __init__(self, name, is_integer=False):
self.name = name
self.is_integer = is_integer
def __add__(self, other):
if type(other) == NonMinusVariable:
return [[1, self], [1, other]]
if type(other) == list:
return other + [[1, self]]
def __radd__(self, other):
if type(other) == NonMinusVariable:
return [[1, self], [1, other]]
if type(other) == list:
return other + [[1, self]]
def __sub__(self, other):
if type(other) == NonMinusVariable:
return [[1, self], [-1, other]]
if type(other) == list:
return other + [[-1, self]]
def __rsub__(self, other):
if type(other) == NonMinusVariable:
return [[1, self], [-1, other]]
if type(other) == list:
return other + [[-1, self]]
def __mul__(self, other):
return [[other, self]]
def __rmul__(self, other):
return [[other, self]]
def __neg__(self):
return [[-1, self]]
def __str__(self):
return self.name
class Polynomial:
def __init__(self, items):
self.items = []
if type(items) == NonMinusVariable:
self.variable_num = 1
self.items.append([1, items])
if type(items) == list:
self.variable_num = len(items)
for item in items:
self.items.append(item)
def __str__(self):
string = ""
for i, item in enumerate(self.items):
coefficient = item[0]
variable = item[1].name
if coefficient > 0 and i != 0:
string += '+'
string += str(coefficient)
string += "·"
string += variable
string += ' '
return string
def __getitem__(self, item):
return self.items[item]
def add_items(self, items):
if type(items) == NonMinusVariable:
self.variable_num += 1
self.items.append([1, items])
if type(items) == list:
for item in items:
self.variable_num += 1
self.items.append(item)
def get_coefficient(self, variable):
for item in self.items:
if variable == item[1]:
return item[0]
return 0
class Inequality:
def __init__(self, LHS, inequality_type, RHS):
self.LHS = Polynomial(LHS)
self.RHS = RHS
if inequality_type == ">=":
self.type = ">="
elif inequality_type == "<=":
self.type = "<="
else:
self.type = "="
def __str__(self):
return str(self.LHS) + self.type + " " + str(self.RHS)