-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscrete-gradients.sws
361 lines (281 loc) · 11.8 KB
/
discrete-gradients.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
354
355
356
357
358
359
360
361
#OFFICIAL FILE.
def apply(L,fn):
res = []
for elt in L:
res = res + [fn(elt)]
return res
def SizeOfComplex(K):
res = 0
for n in range(0,dim(K)+1):
res = res + len(K.faces()[n])
return res
def evenLaplacian(K,d): #2nd var is dimension, this is EVEN Lap bet. d and d-1, linear map on C_(d-1) to itself
co_K = K.chain_complex()
return co_K.differential(d)*co_K.differential(d).transpose()
def oddLaplacian(K,d): #2nd var is dimension, this is ODD Lap bet. d and d-1, linear map on C_(d) to itself
co_K = K.chain_complex()
return co_K.differential(d).transpose()*co_K.differential(d)
def Laplacian(K,d):
return evenLaplacian(K,d+1) + oddLaplacian(K,d)
from sage.graphs.independent_sets import IndependentSets
def acyclicmatchings(K): #takes input complex K and returns list of acyclic matchings, esentially all possible gradients on K
hasse_graph = K.face_poset().hasse_diagram().to_directed().reverse()
hasse_graph = DiGraph(hasse_graph.edges(labels=False))
all_matchings = [x for x in IndependentSets(hasse_graph.to_undirected().line_graph(labels=False))] #needs each element ordered higher to lower dim
HG = DiGraph(hasse_graph.edges()) #the graph to-be-messed-with, other is holder
res = []
for matching in all_matchings:
clean_matching = []
for path in matching:
if len(path[0]) < len(path[1]):
clean_matching = clean_matching + [path[::-1]]
else:
clean_matching = clean_matching + [path]
HG.reverse_edges(clean_matching)
if HG.is_directed_acyclic() == True:
res = res + [clean_matching]
HG = DiGraph(hasse_graph.edges(labels=False))
return res
from sage.graphs.independent_sets import IndependentSets
def limited_acyclicmatchings(K,L):
P = K.face_poset()
ignored_levels = []
for n in range(len(L)):
if L[n] == len(K.faces()[n]): #if num crit cells is num faces, face to be deleted
ignored_levels = ignored_levels + [n]
kept_elements = []
for elt in list(P):
if not (P.rank(elt) in ignored_levels):
kept_elements = kept_elements + [elt]
hasse_graph = P.subposet(kept_elements).hasse_diagram().to_directed().reverse()
hasse_graph = DiGraph(hasse_graph.edges(labels=False))
all_matchings = [x for x in IndependentSets(hasse_graph.to_undirected().line_graph(labels=False))] #needs each element ordered higher to lower dim
HG = DiGraph(hasse_graph.edges()) #the graph to-be-messed-with, other is holder
res = []
for matching in all_matchings:
clean_matching = []
for path in matching:
if len(path[0]) < len(path[1]):
clean_matching = clean_matching + [path[::-1]]
else:
clean_matching = clean_matching + [path]
HG.reverse_edges(clean_matching)
if HG.is_directed_acyclic() == True:
res = res + [clean_matching]
HG = DiGraph(hasse_graph.edges(labels=False))
return res
#takes input complex K and list L = [n_0, n_1, n_2, ..., n_dimK]
#where the critical vector is [K_0 - n_1, K_1 - n_2, ..., K_dim(K) - n_dim(K)]
def gradients(K,L):
reversal = []
for k in range(len(L)):
reversal = reversal + [len(K.faces()[k]) - L[k]]
L = reversal
KE = K.euler_characteristic()
safety = 0
for k in range(0,len(L)):
safety = safety + (-1)^k*L[k]
if KE != safety:
print("Error: List does not satisfy Weak Morse Inequality. Euler characteristic is " + str(KE) + ".")
return
res = []
d = dim(K)
pool = limited_acyclicmatchings(K,L)
#num_cells_list = [len(K.faces()[n]) for n in [0..d]]
for matching in pool:
for n in range(0,d+1):
num_cells = len(K.faces()[n])
num_in_matching = len([tuple for tuple in matching if ((len(tuple[0]) == n+1) or (len(tuple[1]) == n+1)) ])
if L[n] != num_cells - num_in_matching: #checks correct num crit cells
break
elif n==d:
res = res + [matching]
return res
def hasse_levels(K,L): #L is the list of levels desired (ideally adjacent)
P = K.face_poset()
cared_for = P.subposet([x for x in list(P) if (P.rank(x) in L)])
return cared_for
##########
#CONJECTURE CHECKING ITEMS BELOW
##########
def morse_set_vectors(K,d):
L = [0 .. dim(K)]
for n in L:
L[n] = len(K.faces()[n])
LOW = min(L[d],L[d-1])
res = list(range(LOW+1))
for n in range(LOW+1):
vec = []
for j in range(len(L)):
if j == d or j == d-1:
vec = vec + [L[j] - n]
else:
vec = vec + [L[j]]
res[n] = vec
#res.sort()
#res.reverse()
return res
def limited_acyclicmatchings2(K,d):
P = K.face_poset()
ignored_levels = []
for n in range(dim(K)):
if n != d and n != d-1: #if num crit cells is num faces, face to be deleted
ignored_levels = ignored_levels + [n]
kept_elements = []
for elt in list(P):
if not (P.rank(elt) in ignored_levels):
kept_elements = kept_elements + [elt]
hasse_graph = P.subposet(kept_elements).hasse_diagram().to_directed().reverse()
hasse_graph = DiGraph(hasse_graph.edges(labels=False))
all_matchings = [x for x in IndependentSets(hasse_graph.to_undirected().line_graph(labels=False))] #needs each element ordered higher to lower dim
HG = DiGraph(hasse_graph.edges()) #the graph to-be-messed-with, other is holder
res = []
for matching in all_matchings:
clean_matching = []
for path in matching:
if len(path[0]) < len(path[1]):
clean_matching = clean_matching + [path[::-1]]
else:
clean_matching = clean_matching + [path]
HG.reverse_edges(clean_matching)
if HG.is_directed_acyclic() == True:
res = res + [clean_matching]
HG = DiGraph(hasse_graph.edges(labels=False))
return res
def strainer(K,d,pool,vec):
res = []
d = dim(K)
for matching in pool:
for n in range(0,d+1):
num_cells = len(K.faces()[n])
num_in_matching = len([tuple for tuple in matching if ((len(tuple[0]) == n+1) or (len(tuple[1]) == n+1)) ])
if vec[n] != num_cells - num_in_matching: #checks correct num crit cells
break
elif n==d:
res = res + [matching]
return res
def grad_list(K,d):
vec_list = morse_set_vectors(K,d)
#pool = limited_acyclicmatchings(K)
grads = []
pool = limited_acyclicmatchings2(K,d)
for k in range(len(vec_list)):
vec = vec_list[k]
res = strainer(K,d,pool,vec)
grads = grads + [len(res)]
return grads
def evenLap_coeff(K,d):
poly = evenLaplacian(K,d).charpoly().coefficients(sparse=False)
poly.reverse()
return apply(poly,abs)
def checker(K,d):
L1 = evenLap_coeff(K,d)
L2 = grad_list(K,d)
for n in range(min(len(L1),len(L2))):
if L1[n] != L2[n]:
#print(str(L1))
#print(str(L2))
return False
#print(str(L1))
#print(str(L2))
return True
def conj_checker(K):
fails = []
status = True
for d in [1 .. dim(K)]:
if checker(K,d) == False:
status = False
fails = fails + [d]
#print("Failure for dimension d = " + str(d) + ":")
#print("")
return [status,fails]
def poset_iterator(n): #iterates over order complexes of all possible posets on n-many elements
resevoir = posets(n)
print("Will check " + str(len(resevoir)) + " posets.")
for k in range(len(resevoir)):
K = resevoir[k].order_complex()
res = conj_checker(K)
if res[0] == False:
print("Failure on poset " + str(k) + ". Dimension: " + str(dim(K)) + ". Errors in following dimensions:" + str(res[1]))
elif res[0] == True:
print("Success for poset " + str(k) + ".")
#print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
#print("Checking poset " + str(k) + " in list.")
#print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
#K = resevoir[k].order_complex()
#conj_checker(K)
return
#for returning list (of lists) of all cells of K, for input into topaz
def topaz(K):
res = []
for n in range(dim(K)+1):
res = res + apply(list(K.faces()[n]),list)
print("$s = new SimplicialComplex(INPUT_FACES=>" + str(res) + ");")
print("$s->VISUAL;")
return
#x = S[5][1]
#F = x[0]
#R = x[1]
#rootedForest_hasse(x[0],x[1],im = True)
#show(posets(3)[1].hasse_diagram())
##########
#UNNECCESARY
##########
def boxedregion(K,d,num_matching): #input: complex K, dimension (for Laplacian) d, num matchings in boxed range [rem.]
P = K.face_poset() #output: all acyclic matchings occuring in red box
keepers = []
for elt in list(P):
if ((P.rank(elt) == d) or (P.rank(elt) == d-1) or (P.rank(elt) == d-2)):
keepers = keepers +[elt]
hasse_graph = P.subposet(keepers).hasse_diagram().to_directed().reverse() #here on, the code from acyclicmatchings is copied
hasse_graph = DiGraph(hasse_graph.edges(labels=False))
all_matchings = [x for x in IndependentSets(hasse_graph.to_undirected().line_graph(labels=False))] #needs each element ordered higher to lower dim
HG = DiGraph(hasse_graph.edges()) #the graph to-be-messed-with, other is holder -- beyond here acyclicity is checked
res = []
for matching in all_matchings:
clean_matching = []
for path in matching:
if len(path[0]) < len(path[1]):
clean_matching = clean_matching + [path[::-1]]
else:
clean_matching = clean_matching + [path]
HG.reverse_edges(clean_matching)
if HG.is_directed_acyclic() == True:
res = res + [clean_matching]
HG = DiGraph(hasse_graph.edges(labels=False))
corrected_res = [x for x in res if len(x) == num_matching]
return corrected_res
def boxedregion2(K,d,n): #complex K, dimension Laplacian d, coeff of x^n
P = K.face_poset()
keepers_higher = []
keepers_lower = []
for elt in list(P):
if ((P.rank(elt) == d) or (P.rank(elt) == d-1)):
keepers_higher = keepers_higher +[elt]
#if ((P.rank(elt) == d-1) or (P.rank(elt) == d-2)):
if False == True:
keepers_lower = keepers_lower +[elt]
keepers_total = list(set().union(keepers_higher,keepers_lower))
hasse_graph_higher = P.subposet(keepers_higher).hasse_diagram().to_directed().reverse()
hasse_graph_lower = P.subposet(keepers_lower).hasse_diagram().to_directed().reverse()
hasse_graph = P.subposet(keepers_total).hasse_diagram().to_directed().reverse()
all_matchings_higher = [x for x in IndependentSets(hasse_graph_higher.to_undirected().line_graph(labels=False)) if len(x) == n]
all_matchings_lower = [x for x in IndependentSets(hasse_graph_lower.to_undirected().line_graph(labels=False)) if len(x) == n]
all_matchings = all_matchings_higher + all_matchings_lower
HG = DiGraph(hasse_graph.edges()) #the graph to-be-messed-with, other is holder -- beyond here acyclicity is checked
res = []
for matching in all_matchings:
clean_matching = []
for path in matching:
if len(path[0]) < len(path[1]):
clean_matching = clean_matching + [path[::-1]]
else:
clean_matching = clean_matching + [path]
HG.reverse_edges(clean_matching)
if HG.is_directed_acyclic() == True:
res = res + [clean_matching]
HG = DiGraph(hasse_graph.edges(labels=False))
#corrected_res = [x for x in res if len(x) == n]
return res
#returns LIST of acyclic matchings with those conditions
#can go through list (within list?) checking that c_2 (e.g) many 2-dim cells (i.e. 3-tuples) are omitted?