-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPosets.m2
363 lines (297 loc) · 11.3 KB
/
Posets.m2
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
newPackage(
"Posets",
Version => "0.1",
Date => "July 7th, 2008",
Authors => {
{Name => "Sonja Mapes", Email => "mapes@math.columbia.edu", HomePage => "http://www.math.columbia.edu/~mapes/"},
{Name => "Gwyn Whieldon", Email => "whieldon@math.cornell.edu", HomePage => "http://www.math.cornell.edu/People/Grads/whieldon.html"},
{Name => "Josephine Yu", Email => "jyu@math.mit.edu", HomePage => "http://www-math.mit.edu/~jyu/"}
},
Headline => "Package for processing posets and order complexes",
DebuggingMode => true
)
export {
Poset,
poset,
DirectedGraph,
directedGraph,
allPairsShortestPath,
transitiveClosure,
-- FullRelationMatrix,
RelationMatrix,
compare,
indexElement,
nonnull,
OrderIdeal,
Filter,
Relations,
GroundSet,
Edges,
PosetMeet,
PosetJoin,
isLattice,
lcm,
lcmLattice
}
Poset = new Type of HashTable
poset = method()
poset(List,List) := (I,C) ->
new Poset from {
symbol GroundSet => I,
symbol Relations => C,
symbol RelationMatrix => transitiveClosure(I,C),
symbol cache => CacheTable
}
----------------------------
-- some toy examples
-----------------------------
I={a,b,c,d,e,f,g,h}
C={(a,b),(a,c),(a,d),(b,e),(b,f),(c,e),(c,g),(d,f),(d,g),(e,h),(f,h),(g,h)}
--P=poset(I,C)
--G=directedGraph(I,C)
--A=adjacencyMatrix(I,C) -- not exported
--allPairsShortestPath(A) -- not exported
--adjacencyMatrix(G) -- not exported
--adjacencyMatrix(P) -- not exported
--transitiveClosure(I,C)
I1={a,b,c,d,e,f}
C1={(a,c),(a,d),(b,c),(b,d),(c,e),(d,e),(e,f)}
--P1=poset(I1,C1)
--Poset P1 with additional relations (a,e) and (a,f) added
I2={a,b,c,d,e,f}
C2={(a,c),(a,d),(b,c),(b,d),(c,e),(d,e),(a,e),(a,f),(e,f)}
--P2=poset(I2,C2)
-------------
DirectedGraph = new Type of HashTable
directedGraph = method()
directedGraph(List, List) := (I,C) ->
new DirectedGraph from {
symbol GroundSet => I,
symbol Edges => C,
symbol cache => CacheTable
}
--------------
--inputs: (I,C), I is a List (ground set) and C is a List of pairs of elements in I
-- OR DirectedGraph OR Poset
--output: a matrix whose rows and columns are indexed by I,
-- where (i,j) entry is infinity (i.e. 1/0.) if (i,j) is not in C
-- and 1 otherwise (i.e. tropicalization of the "usual" adjacency matrix)
--caveat: diagonal entries are 0
adjacencyMatrix = method()
adjacencyMatrix(List,List) := Matrix => (I, C) -> (
M := mutableMatrix table(#I, #I, (i,j)->1/0.);
ind := hashTable( apply(I, i-> i=> position(I,j-> j==i) ) ); --indices
scan(C, e -> M_(ind#(e#0), ind#(e#1))= 1);
scan(numrows M, i-> M_(i,i) = 0);
matrix M
)
adjacencyMatrix(DirectedGraph) := Matrix => (G) -> adjacencyMatrix(G.GroundSet,G.Edges)
adjacencyMatrix(Poset) := Matrix => (P) -> adjacencyMatrix(P.GroundSet,P.Relations)
--input: adjacency matrix of a directed graph
--output: a matrix whose (i,j) entry is the length of the shortest path from i to j
--algorithm: Floyd–Warshall algorithm for all pairs shortest path
allPairsShortestPath = method()
allPairsShortestPath(Matrix) := Matrix => (A) -> (
D := mutableMatrix(A);
n := numrows D;
scan(n, k->
table(n,n,(i,j)-> D_(i,j) = min(D_(i,j), D_(i,k)+D_(k,j)))
);
matrix D
)
allPairsShortestPath(DirectedGraph) := Matrix => (G)-> allPairsShortestPath(adjacencyMatrix(G))
-- input: a poset, and an element A from I
-- output: the index of A in the ground set of P
-- usage: compare, OrderIdeal
indexElement := (P,A) -> (
sum apply(#P.GroundSet, i-> if P.GroundSet#i == A then i else 0))
-- input: a list, potentially with nulls
-- output: a list w/out nulls
-- usage: OrderIdeal, Filter
nonnull :=(L) -> (
select(L, i-> i =!= null))
--------------------------------------------------
--Transitive Closure and Element Inclusion
--------------------------------------------------
--input: (I,C). I=List, ground set. C=List, pairs
--output: matrix where 1 in (i,j) position where i <= j, 0 otherwise
transitiveClosure = method()
transitiveClosure(List,List) := List => (I,C)-> (
A := adjacencyMatrix(I,C);
D := mutableMatrix allPairsShortestPath(A);
scan(numrows D, i-> D_(i,i) = 0);
table(numrows D, numrows D, (i,j)->(
if D_(i,j) ==1/0. then D_(i,j) = 0 else D_(i,j) = 1;
)
);
matrix D
)
--FullRelationMatrix:= (P) -> (
-- M:=matrix apply (#P.GroundSet, i->
-- apply(#P.GroundSet, j-> if member((P.GroundSet#i,P.GroundSet#j), P.CRelations) then 1 else if i==j then 1 else 0));
-- n:=#P.GroundSet;
-- N:=M^n
-- )
--input: A poset with any type of relation C (minimal, maximal, etc.)
--output: The transitive closure of relations in C in our poset
fullPosetRelation:= (P) -> (
M:=P.RelationMatrix;
L = toList sum apply(numrows(M), i-> set(nonnull(apply(numrows(M),
j-> if (M_j)_i=!=0 and i=!=j then (P.GroundSet#i,I#j)))))
)
--input: A poset P with any type of relation C (minimal, maximal, etc.)
--output: The poset P' on the same ground set with the transitive closure of C
fullPoset:= (P) -> (
L = poset(P.GroundSet,fullPosetRelation(P))
)
-- input: A poset, and two elements A and B from I
-- output: true if A<= B, false else
compare:= (P,A,B) -> (
Aindex:=indexElement(P,A);
Bindex:=indexElement(P,B);
if P.RelationMatrix_Bindex_Aindex==0 then false else true
)
--------------------------------------------------
--Covering Relations
--------------------------------------------------
testcover=(P,A,B) -> (
L:=poset(P.GroundSet,fullPosetRelation(P));
k:=#L.GroundSet-2;
if sum(nonnull(apply(k, i-> if compare(L,A,(toList(set(L.GroundSet)-{A,B}))_i)==true and
compare(L,(toList(set(L.GroundSet)-{A,B}))_i,B)==true
then 1)))=!=0 then C=C+set{(A,B)};
C
)
--input: A poset with any type of relation C (minimal, maximal, etc.)
--output: The minimal relations defining our poset
coveringRelations:=(P) -> (
C=set{};
apply(#P.CRelations,i->testcover(P,P.CRelations#i#0,P.CRelations#i#1));
toList(set(P.CRelations)-C)
)
--input: A poset with any type of relation C (minimal, maximal, etc.)
--output: A new poset P with the minimal relations
coveringRelationsPoset:=(P) -> (
L=poset(P.GroundSet,coveringRelations(P))
)
--------------------------------------------------
--Minimal Element Construction
--------------------------------------------------
minimalElementIndex:=(P)-> (
M:=P.RelationMatrix;
nonnull(apply(numcols(M), k-> if (apply(numcols(M), j-> (sum((apply(numrows(M),i-> (transpose(M))_i))))_j))#k==1 then k))
)
minimalElements:=(P) -> (
L:=minimalElementIndex(P);
apply(#L,i-> P.GroundSet#(L#i))
)
PosetMinusMins:=(P)-> (
L:=minimalElements(P);
K:=fullPoset(P);
N:=set{};
S:=apply(#L, j-> apply(#K.CRelations,i->(K.CRelations#i)#0===L#j));
E:=sum set nonnull(apply(#K.CRelations,l->if member(true,set apply(#L,k->S#k#l)) then N=N+set{K.CRelations#l}));
C:=toList (set(K.CRelations)-N);
I:=toList (set(K.GroundSet)-set(L));
poset(I,C)
)
--------------------------------------------------
--Order and Filter Ideals
--------------------------------------------------
-- input: a poset, and an element from I
-- output: the order ideal of a, i.e. all elements in the poset that are >= a
-- usage:
OrderIdeal:= (P, a) -> (
M:=P.RelationMatrix;
aindex := indexElement (P,a);
GreaterThana:= entries((transpose(M))_aindex);
nonnull(apply(#GreaterThana, i-> if GreaterThana_i == 1 then P.GroundSet#i))
)
-- input: a poset, and an element from I
-- output: the filter of a, i.e. all elements in the poset that are <= a
-- usage:
Filter:=(P,a) -> (
M:=P.RelationMatrix;
aindex := indexElement (P,a);
LessThana:= entries M_aindex;
nonnull(apply(#LessThana, i-> if LessThana_i == 1 then P.GroundSet#i))
)
----------------------------------------------------
--Joins, Meets, Lattices and Atoms
----------------------------------------------------
-- inputs: P, poset, and two elements of P.GroundSet
-- outputs: the element of P.GroundSet that is the join of these, or "not comparable" or "not unique" if those situations occur
-- usage:
PosetJoin = (P,a,b) -> (
OIa := OrderIdeal(P,a);
OIb := OrderIdeal(P,b);
upperBounds := toList (set(OIa)*set(OIb));
if upperBounds == {} then ("not comparable") else (M := P.RelationMatrix;
heightUpperBounds := flatten apply(upperBounds, element-> sum entries M_{indexElement(P,element)});
if #(select(heightUpperBounds, i-> i== min heightUpperBounds)) > 1 then "not unique" else(
upperBounds_{position (heightUpperBounds, l -> l == min heightUpperBounds)})
))
--inputs: P a poset, and 2 elements of P.GroundSet
--outputs: the element in P.GroundSet that is the meet of these, or "not comparable" or "not unique"
-- usage:
PosetMeet = (P,a,b) ->(
Fa:= Filter(P,a);
Fb:= Filter(P,b);
lowerBounds:= toList (set(Fa)*set(Fb));
if lowerBounds == {} then ("not comparable") else (
M := P.RelationMatrix;
heightLowerBounds := flatten apply(lowerBounds, element-> sum entries M_{indexElement(P,element)});
if #(select(heightLowerBounds, i-> i== max heightLowerBounds)) > 1 then "not unique" else(
lowerBounds_{position (heightLowerBounds, l -> l == max heightLowerBounds)}))
)
--inputs: a poset P
--output: boolean value for whether or not it is a lattice
--usage:
isLattice = (P) -> (
checkJoins := unique flatten flatten apply(P.GroundSet, elt ->
apply (P.GroundSet, elt2-> PosetJoin(P,elt, elt2)));
checkMeets := unique flatten flatten apply(P.GroundSet, elt ->
apply (P.GroundSet, elt2-> PosetMeet(P,elt, elt2) ));
checkNons := {member("not unique", set(flatten{checkJoins,checkMeets})),member("not comparable", set(flatten{checkJoins,checkMeets}))};
if member(true, set checkNons) === true then false else true
)
-----------------------------------------------
-- LCM lattices
-----------------------------------------------
--input: a set of monomials
-- output: the lcm of those monomials
lcm = (L) -> (
flatten entries gens intersect apply(L, i-> ideal (i))
)
-- input: generators of a monomial ideal
-- output: lcm lattice of that monomial ideal, without the minimal element
lcmLattice = method()
lcmLattice(Ideal) := Poset => (I) -> (
L := flatten entries gens I;
subsetsL := flatten apply(#L, i-> subsets (L,i+1));
Ground := unique flatten apply (subsetsL, r-> lcm(r));
Rels := nonnull unique flatten apply (Ground, r-> apply(Ground, s-> if s%r == 0 then (r,s)));
P = poset (Ground, Rels);
P)
beginDocumentation()
document { Key => Poset,
Headline => "Betti diagram routines",
EM "BoijSoederberg", " is a package designed to help with the investigation of
the Boij-Soederberg conjectures and theorems. For the definitions and conjectures, see
math.AC/0611081, \"Graded Betti numbers of Cohen-Macaulay modules and
the Multiplicity conjecture\", by Mats Boij, Jonas Soederberg."
}
end
document {
Key => {},
Headline => "",
Usage => "",
Inputs => {
},
Outputs => {
},
EXAMPLE lines ///
///,
Caveat => {},
SeeAlso => {}
}