-
Notifications
You must be signed in to change notification settings - Fork 1
/
complexInput.sws
353 lines (334 loc) · 15.2 KB
/
complexInput.sws
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import copy
#Maybe create vertices in faces and not just add them with the addface bit
global nameDictionary
nameDictionary = {}
class Face(object):
'''Creates a face with a string identifier ('name'), dimension an integer >= 0 called dim, and vert is the list of the 0-faces contained inside of the face.
The list vert may consist of 0-faces or their identifiers.
Note: The number of vertices need not be dim+1. Also, when inputting a single vertex, no input for vert is needed.'''
def __init__(self, name, dim, vert=[]):
self.dim = dim
self.vert = vert[:]
self.name = name
self.subface = []
#Add vertex to the points
if dim == 0 and vert == []:
self.vert.append(self)
#Common Errors
#if type(dim) != int:
# raise ValueError("Dimension should be an integer.")
if dim < -1:
raise ValueError("Dimension should be >= 0")
if dim > 0 and len(vert) == 0:
print "Note: You have no vertices for this face. Check that this is ok."
nameDictionary[self.name] = self
#Create vertices as needed
for v in self.vert:
if isinstance(v, Face)==False:
if v in nameDictionary.keys():
self.vert[self.vert.index(v)] = nameDictionary[v]
else:
self.vert[self.vert.index(v)] = Face(v,0)
def getDim(self):
return self.dim
def getActualVertices(self):
#returns vertices as input
return self.vert
def getVertices(self):
verticesNames = []
for i in self.vert:
verticesNames.append(str(i))
return verticesNames
def getName(self):
return self.name
def division(self, faceAdded):
self.subface.append(faceAdded)
def __checkEmptyDivision(self):
if self.subface == []:
return true
else:
return false
def getDivision(self):
check = true
theList = self.__getDivisionRecur()
while check:
check = false
for i in theList:
if isinstance(i, list):
check = true
theList.append(i[0])
theList.remove(i)
del(i[0])
theList.append(i)
if i == []:
theList.remove(i)
return theList
def __getDivisionRecur(self):
temp = []
for i in self.subface:
if i.__checkEmptyDivision():
temp.append(i)
else:
temp.append(i.__getDivisionRecur())
return temp
def __str__(self):
return self.name
def __getattr__(self, attr):
if attr.startswith('__'):
raise AttributeError(attr)
return self[attr]
class Complex(object):
'''Creates a complex with name given by string name. An optional faceDictionary may be provided if faces have already been created.
Else, use addFace command.
The faceDictionary should have dimensions as keys and lists of faces as entries. The (-1)-dimensional face is automatically added and should not be in your dictionary. '''
def __init__(self, faceDictionary = None):
#Create and add the Empty Face
EmptyFace = Face('EmptyFace', -1, [])
if faceDictionary != None:
self.faceDictionary = copy.deepcopy(faceDictionary)
self.faceDictionary[-1] = []
self.faceDictionary[-1].append(EmptyFace)
else:
self.faceDictionary = {-1:[EmptyFace]}
#Builds current name to face dictionary
nameDictionary['EmptyFace'] = [EmptyFace]
if faceDictionary != None:
for i in faceDictionary.keys():
for j in range(len(faceDictionary[i])):
nameDictionary[faceDictionary[i][j].getName()] = faceDictionary[i][j]
def addFace(self, *facelist): #can add multiple faces at the same time
'''Vertices of a face are automatically added.'''
for face in facelist:
if isinstance(face, Face) == False:
if face in nameDictionary.keys():
face = nameDictionary[face]
else:
raise ValueError('This is not a face.')
if face.getDim() in self.faceDictionary.keys():
if face in self.faceDictionary[face.getDim()]:
raise ValueError("This face is already in the complex.")
else:
self.faceDictionary[face.getDim()].append(face)
else:
self.faceDictionary[face.getDim()] = []
self.faceDictionary[face.getDim()].append(face)
#check if the face vertex is already a face; add it if it is not one.
if 0 not in self.faceDictionary.keys():
self.faceDictionary[0] = []
for v in face.getActualVertices():
if v not in self.faceDictionary[0]:
self.faceDictionary[0].append(v)
def getFaces(self):
tempDictionary = copy.deepcopy(self.faceDictionary)
tempNames = {}
for i in tempDictionary.keys():
tempNames[i] = []
for j in tempDictionary[i]:
tempNames[i].append(j.getName())
return tempNames
def getActualFaces(self):
return self.faceDictionary
def delFace(self, face):
if isinstance(face, Face) == False:
if face in nameDictionary.keys():
face = nameDictionary[face]
else:
raise ValueError('This is not a face.')
if face not in self.faceDictionary[face.getDim()]:
raise ValueError('This is not a face in the complex.')
if len(self.faceDictionary[face.getDim()]) == 1:
del self.faceDictionary[face.getDim()]
else:
self.faceDictionary[face.getDim()].pop(self.faceDictionary[face.getDim()].index(face))
def hpoly(self):
fvec = []
for i in range(len(self.faceDictionary)):
fvec.append(len(self.faceDictionary[i-1]))
x = var('x')
fpoly = 0
order = len(fvec)-1
for i in range(len(fvec)):
fpoly = fpoly + fvec[i]*(x-1)^(len(fvec)-1-i)
fpoly.expand()
hpoly = fpoly(x = 1/x) * x^order
return hpoly.expand()
def link(self, face):
if face.lower() == 'emptyface':
return self.getFaces()
faceSet = set(face)
linkList = []
for i in self.faceDictionary.keys():
for j in self.faceDictionary[i]:
if faceSet.issubset(set(j.getName())) and j.getName() != 'EmptyFace':
temp = list(set(j.getName()).difference(faceSet))
if not temp:
linkList.append('EmptyFace')
else:
linkList.append(''.join(sorted(temp)))
return linkList
class SubdividedComplex(Complex):
'''Allows you to divide a complex by inputting a subdivision. Input the original, undivided complex on initialization. Then give the subdivision.'''
def __init__(self, undivided):
self.undivided = undivided
self.faceDictionary = {}
for i in undivided.getActualFaces().keys():
self.faceDictionary[i] = []
for j in range(len(undivided.getActualFaces()[i])):
self.faceDictionary[i].append(undivided.getActualFaces()[i][j])
self.subdivisionDictionary = {}
def setUndivided(self, undividedComplex):
self.undivided = undividedComplex
def addSubFace(self, faceToAddTo, faceAdded):
#Checks if the inputs are faces we know or not. If is a string but is the name of a face, replaces the face for the string.
if isinstance(faceAdded, Face) == False:
if faceAdded in nameDictionary.keys():
faceAdded = nameDictionary[faceAdded]
else:
raise TypeError('The face added is a string and not a face. Please create this face.')
if isinstance(faceToAddTo, Face) == False:
if faceToAddTo in nameDictionary.keys():
faceToAddTo = nameDictionary[faceToAddTo]
else:
raise TypeError('The face added to is a string and not a face. Please create this face.')
if faceToAddTo not in self.faceDictionary[faceToAddTo.getDim()]:
raise ValueError('The face added to is not in the complex given. Please fix.')
#Add the faceadded to the subdivision dictionary
#Might need to change this because I might only need if it is splitting it...
#Do I want to keep all the subdivisions?
if faceToAddTo not in self.subdivisionDictionary.keys():
self.subdivisionDictionary[faceToAddTo] = []
self.subdivisionDictionary[faceToAddTo].append(faceAdded)
#Add the face added to the faceDictionary
if faceAdded.getDim() not in self.faceDictionary.keys():
self.faceDictionary[faceAdded.getDim()] = []
self.faceDictionary[faceAdded.getDim()].append(faceAdded)
#Deal with actually dividing a face
if faceAdded.getDim() == faceToAddTo.getDim()-1:
if faceAdded.getDim() == 0:
for i in faceToAddTo.getActualVertices():
temp = Face(i.getName()+faceAdded.getName(), 1, [i, faceAdded])
self.faceDictionary[1].append(temp)
nameDictionary[temp.getName()] = temp
faceToAddTo.division(temp)
for i in self.faceDictionary.keys():
if i>1:
for j in range(len(self.faceDictionary[i])):
contains = True
for k in faceToAddTo.getVertices():
if k not in self.faceDictionary[i][j].getVertices():
contains = False
if contains == True and faceAdded.getVertices()[0] not in self.faceDictionary[i][j].getVertices():
temp = self.faceDictionary[i][j].getVertices()
temp.append(faceAdded.getVertices()[0])
name = ''.join(sorted(temp))
tempFace = Face(name, i, temp)
self.faceDictionary[i][j].division(tempFace)
self.faceDictionary[i][j] = tempFace
nameDictionary[name] = tempFace
if faceAdded.getDim() > 0:
checkFace = []
for i in self.faceDictionary[1]:
temp = i.getVertices()
checkFace.append(sorted(temp))
for i in range(len(checkFace)):
name = ""
for j in checkFace[i]:
name = name + j
checkFace[i] = name
vertices = [] #contains vertices of faceAdded
vertex = [] #contains the vertex to add to faceAdded
for i in faceAdded.getVertices():
vertices.append(i)
for i in faceToAddTo.getVertices():
possibleFace = []
for j in vertices:
temp = i + j
temp = ''.join(sorted(temp))
possibleFace.append(temp)
contains = True
#print possibleFace
for j in possibleFace:
if j not in checkFace:
contains = False
break
if contains == True:
vertex.append(i)
break
for i in [1,2]:
if i == 2:
temp = faceToAddTo.getVertices()
temp.remove(vertex[0])
vertex = []
vertices = []
for j in temp:
vertex.append(j)
for j in vertex:
vertices.append(j)
for j in faceAdded.getVertices():
vertices.append(j)
vertices.sort()
for j in vertices:
while vertices.count(j) > 1:
vertices.remove(j)
name = ''.join(vertices)
temp = Face(name, faceToAddTo.getDim(), vertices)
self.faceDictionary[faceToAddTo.getDim()].append(temp)
nameDictionary[name] = temp
faceToAddTo.division(temp)
self.delFace(faceToAddTo)
def hpoly(self):
fvec = []
for i in range(len(self.faceDictionary)):
fvec.append(len(self.faceDictionary[i-1]))
x = var('x')
fpoly = 0
order = len(fvec)-1
for i in range(len(fvec)):
fpoly = fpoly + fvec[i]*(x-1)^(len(fvec)-1-i)
fpoly.expand()
hpoly = fpoly(x = 1/x) * x^order
return hpoly.expand()
def localh(self, undividedFace):
if isinstance(undividedFace, str):
undividedFace = nameDictionary[undividedFace]
local = self.hpoly()
for i in sorted(self.undivided.faceDictionary.keys()):
for j in self.undivided.faceDictionary[i]:
if i == -1:
local = local - 1
elif j.getName() in self.getFaces()[i]:
local = local - 0
else:
if undividedFace.getName() in self.undivided.getFaces()[i]:
return local
else:
miniComplex = Complex(self.__makeComplexDict(j))
miniSubComplexDict = {}
for k in range(j.getDim()+1):
miniSubComplexDict[k] = []
for k in j.getDivision():
miniSubComplexDict = self.__makeSubComplexDict(k, miniSubComplexDict)
miniSubComplex = Complex(miniSubComplexDict)
miniSubComplex = SubdividedComplex(miniSubComplex)
miniSubComplex.setUndivided(miniComplex)
local = local - miniSubComplex.localh(j)
def __makeComplexDict(self, mainFace):
mainFaceDict = {}
for i in self.undivided.faceDictionary.keys():
mainFaceDict[i] = []
for j in self.undivided.faceDictionary[i]:
if set(j.getName()).issubset(mainFace.getName()):
mainFaceDict[i].append(j)
for i in mainFaceDict.keys():
if mainFaceDict[i] == []:
mainFaceDict.pop(i)
return mainFaceDict
def __makeSubComplexDict(self, mainFace, mainFaceDict):
for i in self.faceDictionary.keys():
for j in self.faceDictionary[i]:
if set(j.getName()).issubset(mainFace.getName()) and j not in mainFaceDict[i]:
mainFaceDict[i].append(j)
for i in mainFaceDict.keys():
if mainFaceDict[i] == []:
mainFaceDict.pop(i)
return mainFaceDict