-
Notifications
You must be signed in to change notification settings - Fork 0
/
combinatorial_objects.py
178 lines (141 loc) · 4.99 KB
/
combinatorial_objects.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
r""" Combinatorial Objects
Combinatorial Objects are the elements of Combinatorial Scalars.
Each element has a weight and sign associated with it.
The sign is +1 or -1, and the weight is a monomial.
AUTHORS:
- Steven Tartakovsky (2012): initial version
"""
#*****************************************************************************
# Copyright (C) 2012 Steven Tartakovsky <startakovsky@gmail.com>,
#
# Distributed under the terms of the GNU General Public License (GPL)
#
# This code is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# The full text of the GPL is available at:
#
# http://www.gnu.org/licenses/
#*****************************************************************************
#from sage.rings import *
#from sage.structure.unique_representation import UniqueRepresentation
#from sage.structure.all import SageObject
from sage.symbolic.expression import Expression
from sage.all import *
class CombinatorialObject(SageObject):
r"""
INPUT:
- object an instance of the class SageObject
- sign a number in the set {1,-1}
- monomial a list indicating the weight monomial (eg: [0,0,1,2,1] is x2*x3^2*x4)
OUTPUT:
Returns l, wrapped as a combinatorial class
EXAMPLES::
sage: C = CombinatorialObject("Cool",-1,[4,3,0,6,2]);C
Combinatorial Object Cool, sign -1, and monomial x1^4*x2^3*x4^6*x5^2.
sage: C.get_tuple()
('Cool', -1, x1^4*x2^3*x4^6*x5^2)
sage: C.get_genfunc()
-x1^4*x2^3*x4^6*x5^2
sage: C.get_object()
'Cool'
sage: C.get_weight()
x1^4*x2^3*x4^6*x5^2
sage: C.get_sign()
-1
"""
def __init__(self, object, sign, weight = [0], row = -1, col = -1):
r"""
Initiates the object and sets monomial list to variable.
See ``CombinatorialObject`` for full documentation.
"""
self._object = object
self._sign = sign
if type(weight) == Expression:
self._weight_monomial = weight
else:
self._weight_monomial = assign_weight_monomial(weight)
self._row = row
self._col = col
def __repr__(self):
return str(self._object)
def __cmp__(self,other):
if bool(self.get_object() != other.get_object()):
return -1
elif bool(self.get_genfunc() != other.get_genfunc()):
return -1
else:
return 0
def __mul__(self,other):
return CombinatorialObject((self,other),self.get_sign()*other.get_sign(),self.get_weight()*other.get_weight())
def __hash__(self):
return hash(self.get_tuple())
def get_detail(self):
return "Combinatorial Object %s, sign %d, and weight %s." %(self._object, self._sign, str(self._weight_monomial))
def get_tuple(self):
return (self.get_object(),self.get_sign(),self.get_weight())
def get_genfunc(self):
return self._sign * self._weight_monomial
def get_object(self):
return self._object
def set_object(self,obj):
self._object = obj
return self
def set_row(self,row):
self._row = row
return self
def set_col(self,col):
self._col = col
return self
def get_row(self):
return self._row
def get_col(self):
return self._col
def get_weight(self):
return self._weight_monomial
def get_sign(self):
return self._sign
def get_cleaned_up_version(self):
r"""
Returns a new cleaned up version of this object.
Eliminates nested tuples. Does not change actual object.
"""
t = deepcopy(self)
return CombinatorialObject(clean_up_object(t),self.get_sign(),self.get_weight())
def create_variables(n):
r"""
Given an integer n, this method returns the string 'x1,..,xn'
"""
variables = ''
for i in range(n):
variables+= 'x' + str(i+1) + ','
variables = variables[0:len(variables)-1]
return variables
def assign_weight_monomial(l):
r"""
Returns weight monomial from list l.
"""
l.append(0) #to handle the case of a power of x1
v = var(create_variables(len(l)))
monomial = 1
for i in range(len(l)):
monomial = monomial * v[i]**l[i]
return monomial
def clean_up_object(l):
r"""
Returns just the objects inside nested tuples and preserves the order.
"""
sign = l.get_sign()
weight = l.get_weight()
K = list()
if type(l.get_object())==tuple:
for i in range(len(l.get_object())):
if type(l.get_object()[i].get_object()) == tuple:
K.extend(clean_up_object(l.get_object()[i]))
else:
K.append(l.get_object()[i])
return tuple(K)
else:
return l