File: RandomIdeals.m2

package info (click to toggle)
macaulay2 1.21%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 133,096 kB
  • sloc: cpp: 110,377; ansic: 16,306; javascript: 4,193; makefile: 3,821; sh: 3,580; lisp: 764; yacc: 590; xml: 177; python: 140; perl: 114; lex: 65; awk: 3
file content (1510 lines) | stat: -rw-r--r-- 46,353 bytes parent folder | download | duplicates (2)
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
newPackage(
	"RandomIdeals",
    	Version => "2.0", 
    	Date => "May 9, 2016",
        Authors => {
	    {Name => "Katie Ansaldi",
		Email => "kansaldi@gmail.com"},
	    {Name => "David Eisenbud",
         	Email => "de@msri.org",
         	HomePage => "http://www.msri.org/~de"},
     	    {Name => "Robert Krone",
	 	Email => "rckrone@gmail.com",
	 	HomePage => "http://rckr.one"},
	    {Name => "Jay Yang",
		Email => "jkelleyy@gmail.com"}
	    },
    	HomePage => "http://www.msri.org/~de",
    	Headline => "creating random ideals of various sorts",
	Keywords => {"Examples and Random Objects"},
	AuxiliaryFiles => false, -- set to true if package comes with auxiliary files,
	PackageExports =>{"EdgeIdeals", "BinomialEdgeIdeals"},
    	DebuggingMode => false	 -- set to true only during development
    	)

export {
     "randomIdeal",
     "randomMonomialIdeal",
     "randomSquareFreeMonomialIdeal",
     "randomSquareFreeStep",
     "randomBinomialIdeal",
     "randomPureBinomialIdeal",
     "randomSparseIdeal",
     "randomEdgeIdeal",
     "randomBinomialEdgeIdeal",
     "randomToricEdgeIdeal",
     "randomElementsFromIdeal",
     "randomMonomial",
     "squareFree",
     "regSeq",
     "AlexanderProbability",
        "randomAddition", 
	"randomShelling",
        "idealFromShelling",
	"idealChainFromShelling",
        "isShelling",
	"randomShellableIdeal",
	"randomShellableIdealChain"
     }

randomMonomial = method(TypicalValue => RingElement)
randomMonomial(ZZ,Ring) := RingElement => (d,S) -> (
     m := basis(d,S);
     m_(0,random rank source m))

   
randomMonomialIdeal=method(TypicalValue => Ideal)

randomMonomialIdeal(Sequence, Ring) := Ideal => (L,S)-> 
     randomMonomialIdeal(toList L, S)

randomMonomialIdeal(List, Ring) := Ideal => (L,S)->(
     --produces an ideal minimally generated 
     --by random monomials of degrees given in L,
     --unless the low degree terms generate all monomials 
     --in one of the higher degrees requested,
     --in which case the ideal fulfills as many of 
     --the conditions as possible (from lowest degree),
     --and produces a message.
     --
     --first produce a list in ascending order of degree, 
     --specifying how many elements are required from that degree
     Lu := sort unique L;
     Ls := apply(Lu, t -> #positions(L, s->(s==t)));
     --handle the initial degree
     M := flatten entries basis(Lu#0, S^1);
     if Ls#0 < #M then I := ideal((random M)_{0..Ls#0-1})
	  else (
	       I = ideal(M);
       	       <<"***** there are only " 
	       << binomial(Lu#0+numgens S-1,Lu#0)
	       <<" monomials of degree " 
	       << Lu#0 
	       <<endl;
	       return I
	       );
     --now the rest of the degrees, if any.
     for t from 1 to #Lu-1 do (
	  M = flatten entries compress (basis(Lu#t,S^1) % I);
	  if Ls#t < #M then I = I+ideal((random M)_{0..Ls#t-1})
	  else (
	       if #M=!=0 then I = I+ideal(M);
	       print("low degree gens generated everything"); 
	       break)
	  );
     I
     ) 
randomSquareFreeMonomialIdeal=method(TypicalValue => Ideal)

randomSquareFreeMonomialIdeal(Sequence, Ring) := Ideal => (L,S)-> 
     randomSquareFreeMonomialIdeal(toList L, S)

randomSquareFreeMonomialIdeal(List, Ring) := Ideal => (L,S)->(
     --L is a list of degrees desired, The routine
     --produces an ideal minimally generated by random 
     --square-free monomials of exactly the numbers and degrees given in L,
     --unless the low degree terms generate 
     --all monomials in one of the higher degrees requested,
     --in which case the ideal fulfills as 
     --many of the conditions as possible (from lowest degree).
--
     --first produce a list in ascending order of degree, specifying how many elements are required from that degree
     Lu := sort unique L;
     Ls := apply(Lu, t -> #positions(L, s->(s==t)));
     M := flatten entries gens squareFree(Lu#0, S);
     if Ls#0 < #M then I := ideal((random M)_{0..Ls#0-1})
	  else (I = ideal(M);
	       <<"***** there are only " <<binomial(numgens S,Lu#0)<<
		    " square-free monomials of degree " << Lu#0 <<endl;
	       return I
	       );
     for t from 1 to #Lu-1 do (
	  M=flatten entries compress (gens squareFree(Lu#t,S) % I);
	  if Ls#t < #M then I = I+ideal((random M)_{0..Ls#t-1})
	  else (if #M=!=0 then I = I+ideal(M);
	       print("low degree gens generated everything");
	       break
	       )
	  );
     I
     ) 


randomSquareFreeStep = method(Options=>{AlexanderProbability => .05})

soc = I -> (
     --I should be a MonomialIdeal
     p:=product gens ring I;
     apply(flatten entries gens dual I, m->p//m))

prepare= I->(
     --I should be a MonomialIdeal
     Igens := flatten entries gens I;
     ISocgens := soc I;
     {I,Igens,ISocgens})

randomSquareFreeStep(Ideal) := o -> (I) ->(
     if  isMonomialIdeal I then randomSquareFreeStep(monomialIdeal I, o)
     else error "ideal not generated by monomials")
randomSquareFreeStep(MonomialIdeal) := o -> (I) ->(
     if isSquareFree I then randomSquareFreeStep (prepare I, o)
     else error "ideal not square-free")
     
     
     
     

randomSquareFreeStep(List) := o -> (L) ->(
     I := L_0;
     S := ring I;
     mm := monomialIdeal vars S;
     mm2 := monomialIdeal apply (flatten entries vars S, p->p^2);
     --With probability given by the option AlexanderProbability (default .05),
     --the routine simply returns the Alexander dual of I. 
     if random RR <o.AlexanderProbability then return prepare dual I;
     --form a candidate step
     Igens := L_1;
     nI := #Igens;
     ISocgens := L_2;
     nSocI := #ISocgens;
     if random RR < nSocI/(nI+nSocI) then
	  J := I + monomialIdeal ISocgens_(random nSocI)
     else (
	  --p := random nI;
	  p := monomialIdeal Igens_(random nI);
--	  p' := select(toList(0..nI-1), j->j!=p);
	  p' := I-p; -- returns the ideal generated by all the gens but p
	  sqFreeMults := (mm*p)-mm2;
--  	  sqFreeMults := substitute(monomialIdeal(gens(mm*ideal(Igens_p))%mm2), S);
--	  J = trim (ideal(Igens_p')+sqFreeMults)
  	  J = p'+sqFreeMults
	  );
     --Decide whether to accept the step or not
     Jgens := flatten entries gens J;
     nJ := #Jgens;
     JSocgens := soc J;
     nSocJ := #JSocgens;
     --accept with probability P (if P<1) or unconditionally (P>=1):
     P := (nI+nSocI)/(nJ+nSocJ);
     if random RR < P then {J,Jgens,JSocgens} else {I,Igens,ISocgens}
     )


squareFree = method(TypicalValue => Ideal)

squareFree(ZZ,Ring) := Ideal => (d,S) ->
--returns the ideal generated by all square free monomials of degree d
ideal compress(basis(d,S^1) % ideal apply (flatten entries vars S, p -> p^2))

randomPureBinomialIdeal = method(TypicalValue => Ideal)
randomPureBinomialIdeal(Sequence, Ring) := Ideal => (L,S)->randomPureBinomialIdeal(toList L, S)
randomPureBinomialIdeal(List, Ring) := Ideal => (L,S)->(
     --L=list of degrees of the generators
     trim ideal apply(L, d->randomMonomial(d,S)-randomMonomial(d,S))
     )

randomBinomialIdeal = method(TypicalValue => Ideal)
randomBinomialIdeal(Sequence, Ring) := Ideal => (L,S)->randomPureBinomialIdeal(toList L, S)
randomBinomialIdeal(List, Ring) := Ideal => (L,S)->(
     --L=list of degrees of the generators
     kk:=ultimate (coefficientRing, S);
     trim ideal apply(L, d->randomMonomial(d,S)-random(kk)*randomMonomial(d,S))
     )

///
///

randomSparseIdeal = method(TypicalValue => Ideal)
randomSparseIdeal(Matrix, ZZ, ZZ) := Ideal => (B,s,n) -> (
     -- B is a 1xt matrix of monomials
     -- s is the size of each poly
     -- n is the number of polys
     S := ring B;
     t := rank source B;
     BB := (first entries B);
     kk := ultimate(coefficientRing, S);
     trim ideal apply(n, j -> 
     sum apply(s, i -> random kk * BB#(random t)))
     )

randomIdeal = method(TypicalValue => Ideal)
randomIdeal(Sequence,Matrix) := Ideal => (L,B) -> randomIdeal(toList L, B)
randomIdeal(List,Matrix) := Ideal => (L,B) -> (     
     -- B is a 1 x n matrix of homogeneous polynomials
     -- L is a list of degrees
     trim ideal(B * random(source B, (ring B)^(-L)))
     )

regSeq = method(TypicalValue => Ideal)
regSeq (Sequence, Ring) := Ideal => (L,S)-> regSeq(toList L, S)
regSeq (List, Ring) := Ideal => (L,S)->(
     --forms an ideal generated by powers of the variables.
     --L=list of NN. uses the initial subsequence of L as powers
     ideal for m to min(#L,rank source vars S)-1 list S_m^(L_m))

randomElementsFromIdeal =  method(TypicalValue => Ideal)
randomElementsFromIdeal(List, Ideal) := Ideal => (L,I)->(
trim ideal((gens I)*random(source gens I, (ring I)^(-L))))


----------From Shelling

testNewSimplex = method()
testNewSimplex(List, List) := (P, D) ->(
--given a pure, d-dimensional simplicial complex (sc) as a list of ordered lists of d+1 vertices in [n], and
--a simplex D as such a list, tests whether the intersection of D with P is a union of facets of D.
     d := #D-1; --dimension
     ints := apply(P, D' -> intersectLists(D',D));
     facets := apply(unique select(ints, E -> #E==d),set);
     antiFacets := apply(facets,F -> (D-F)#0);
     if facets == {} then return false;
     smalls := unique select(ints, E -> #E<d);
     all(smalls, e -> any(antiFacets, v -> not member(v,e)))
)

intersectLists = (D',D) -> D - set(D-set D')

randomSubset = (n,m) -> (
    L := new MutableList from toList (0..m-1);
    for i from m to n-1 do (
	j := random(i+1);
    	if j < m then L#j = i;
	);
    sort toList L
    )

randomAddition = method()
randomAddition(ZZ,ZZ,List) := (n,m,P) ->(
    if #P == 0 then return {randomSubset(n,m+1)};
    Plarge := select(P, D-> #D >= m+1); -- the facets big enough to be glued to
    if #Plarge == 0 then error "m is too large";
    t := false;
    D' := {null};
    D := Plarge#(random(#Plarge)); -- a random facet from Plarge
    compD := toList(0..n-1) - set D;
    count := 0;
    while not t and count < 20 do (
    	i := random (#compD);
    	J := randomSubset(#D,#D-m);
    	D' = sort(D - set apply(J, j->D_j) | {compD_i});
    	t = (testNewSimplex(P,D'));
	count = count+1);
    if count == 20 then return P;
    unique (P|{D'})
    )

listsToMonomials = (P,R) -> apply(P, D -> product apply(D,d->R_d))
squareFreeMonomialsToLists = (L,R)->(
varToIndex := i->position(gens R, j->j==i);
apply (L/support, j->j/varToIndex))
    
///
S = ZZ/101[a,b,c]
L = flatten entries matrix"ab,a2,abc2"
varToIndex b
viewHelp position
///

randomAddition(Ring,ZZ,List) := (R,m,L) -> (
    P := squareFreeMonomialsToLists(L,R);
    listsToMonomials(randomAddition(numgens R,m,P),R)
    )

idealFromShelling = method()
idealFromShelling (Ring,List) := (S,P) -> (
    Delta := toList (0..numgens S - 1);
    V := vars S;
    monomialIdeal intersect apply(P, D -> monomialIdeal {V_(Delta - set D)})
    )


idealFromShelling List := P -> (
    n := (max flatten P)+1;
    x := symbol x;
    S := QQ[x_0..x_(n-1)];
    idealFromShelling(S,P)
    )
///
R = ZZ/101[x_0..x_4];
I = randomShellableIdeal(R,2,6)
S
///

idealChainFromShelling = method()
idealChainFromShelling(Ring,List) := (S,P) -> toList apply(#P,i->idealFromShelling(S,take(P,i+1)))
idealChainFromShelling List := P -> toList apply(#P,i->idealFromShelling(take(P,i+1)))

isShelling = method()
isShelling(List) := P -> all(#P, i-> i==0 or testNewSimplex(take(P,i),P#i))

randomShelling = method()
-- random chain of shellable complexes on n vertices, with pure dim m, up to the complete m skeleton

randomShelling(ZZ,ZZ) := (n,m) -> randomShelling(n,m,binomial(n,m+1))
-- random chain of shellable complexes on n vertices, with pure dim m, and k facets

--Should we change the following to start with {{0..m}, {0..m-1,m} to diminish autos?
randomShelling(ZZ,ZZ,ZZ) := (n,m,k) -> (
    if k > binomial(n,m+1) then error "k is too large";
    P := {};
    while #P < k do P = randomAddition(n,m,P);
    P
    )
randomShellableIdeal=method()
randomShellableIdeal(Ring,ZZ,ZZ) := (R,dimProj,deg) -> (
    idealFromShelling(R,randomShelling(numgens R ,dimProj, deg))
    )

randomShellableIdealChain=method()
randomShellableIdealChain(Ring,ZZ,ZZ) := (R,dimProj,deg)->(
    idealChainFromShelling(R,randomShelling(numgens R,dimProj,deg))
    )
randomShellableIdealChain(Ring,ZZ) := (R,dimProj)->(
    idealChainFromShelling(R,randomShelling(numgens R,dimProj))
    )
///
S = ZZ/101[x_0..x_5]
I = randomShellableIdeal(S,2,5)
dim I == 3
degree I == 5

S = ZZ/101[x_0..x_4]
I = randomShellableIdeal(S,2,6)

///

randomShelling(Ring,ZZ,ZZ) := (R,m,k) -> listsToMonomials(randomShelling(numgens R,m,k),R)
randomShelling(Ring,ZZ)    := (R,m)   -> listsToMonomials(randomShelling(numgens R,m),R)




randomEdgeIdeal = method()
randomEdgeIdeal(ZZ,ZZ) := (n,t) -> (
    	    needsPackage "EdgeIdeals";
	    needsPackage "BinomialEdgeIdeals";
    	    x:=symbol x;
    	    G:=randomGraph(QQ[x_1..x_n],t);
	    (G, edgeIdeal(G))
		)
	    

TEST///
randomEdgeIdeal(8, 5)
randomBinomialEdgeIdeal(7, 4)
randomToricEdgeIdeal(6,10)
///
--Random binomial edge ideal
--n is number of vertices, t is number of edges of the graph

randomBinomialEdgeIdeal = method();
randomBinomialEdgeIdeal(ZZ,ZZ) := (n, t) -> ( 
    	    needsPackage "EdgeIdeals";
	    needsPackage "BinomialEdgeIdeals";
    x := symbol x;
    G := randomGraph(QQ[x_1..x_n], t); 
    E := apply(edges G, i -> apply(i, j -> index j+1));
    return (binomialEdgeIdeal(E), G)
    )


--Random toric edge ideal 
--n is number of variables, t is number of edges of the graph

randomToricEdgeIdeal = method();
randomToricEdgeIdeal(ZZ,ZZ) := (n, t) -> (
    needsPackage "BinomialEdgeIdeals";
    needsPackage "EdgeIdeals";
    e := local e;
    x := local x;
    R := QQ[x_1..x_n];
    S := QQ[e_1..e_t];
    G := randomGraph(R,t);
    E := apply(edges G, product);
    (ker map(R,S,E), G)
    )

------------------------------------------------------------
-- DOCUMENTATION RandomIdeals -- documentation
------------------------------------------------------------

beginDocumentation()

doc ///
Key 
     RandomIdeals
Headline 
     A package to construct various sorts of random ideals
Description
 Text
     This package can be used to make experiments, trying many ideals, perhaps
     over small fields. For example...what would you expect the regularities of
     "typical" monomial ideals with 10 generators of degree 3 in 6 variables to be?
     Try a bunch of examples -- it's fast.
     Here we do only 500 -- this takes about a second on a fast machine --
     but with a little patience, thousands can be done conveniently.
 Example
     setRandomSeed(currentTime())
     kk=ZZ/101;
     S=kk[vars(0..5)];
     time tally for n from 1 to 500 list regularity randomMonomialIdeal(10:3,S)
 Text
     How does this compare with the case of binomial ideals? or pure binomial ideals?
     We invite the reader to experiment, replacing "randomMonomialIdeal" above with
     "randomBinomialIdeal" or "randomPureBinomialIdeal", or taking larger numbers
     of examples. Click the link "Finding Extreme Examples" below to see
     some other, more elaborate ways to search. 
SeeAlso
     "Finding Extreme Examples"
     randomIdeal
     randomMonomialIdeal
     randomSquareFreeMonomialIdeal
     randomSquareFreeStep
     randomBinomialIdeal
     randomPureBinomialIdeal
     randomSparseIdeal
     randomElementsFromIdeal
     randomMonomial
     randomShellableIdeal
     randomShellableIdealChain
     randomShelling
///

------------------------------------------------------------
-- DOCUMENTATION randomShelling
------------------------------------------------------------

doc ///
     Key
          randomShelling
	  (randomShelling,ZZ,ZZ)
	  (randomShelling,ZZ,ZZ,ZZ)
	  (randomShelling,Ring,ZZ)
	  (randomShelling,Ring,ZZ,ZZ)
     Headline
          produces a random chain of shellable complexes
     Usage
          P=randomShelling(n,m)
	  P=randomShelling(n,m,k)
	  P=randomShelling(R,m)
	  P=randomShelling(R,m,k)
     Inputs
          n:ZZ
	       the number of vertices
	  R:Ring
	       a polynomial ring with a variable for each vertex
	  m:ZZ
	       the dimension of the facets
	  k:ZZ
	       the number of facets (if omitted, the number will be {\tt n} choose {\tt m+1})
	      
     Outputs
          P:List
	       A list of lists of integers.  Each list of integers is a facet of the complex and the order is a shelling.  If called with a Ring {\tt R} instead of an integer {\tt n}, each facet is represented by a square-free monomial instead of a list.
     Description
          Text
              The function produces a list of facets of a random shellable simplicial complex.
	      The order of the facets is a shelling.
	  Text
	      The algorithm works by choosing one of the previous facets at random, and replacing one of its vertices with a new vertex chosen at random.
	      If the choice meets the criteria of a shelling, that facet is added to list, otherwise it is discarded and the algorithm tries again.
	      The first facet is chosen uniformly at random.

	      The call randomShelling(n,m) produces a *complete* chain -- that is, a shelling
	      of the m-skeleton of the (n-1)-simplex, with the simplices listed in order,
	      so that any initial subsequence of length d gives a (random) shellable simplicial
	      complex with d facets. 
	      
	      The probability distribution for this random selection
	      is presumably not the uniform one; it would be nice to write a reversible
	      markov chain that could be used with the Metropolis algorithm to produce
	      the uniform distribution, as is done in randomSquareFreeStep, and the
	      randomSquareFreeMonomialIdeal codes
          Example
               P = randomShelling(6,3,10)
	       Q = randomShelling(6,3)
     Caveat
     	   No claim is made on the distribution of the random chain.
     SeeAlso
     	 randomAddition
	 idealChainFromShelling
	 idealFromShelling
	 randomShellableIdeal
	 randomSquareFreeStep
	 randomSquareFreeMonomialIdeal
///


------------------------------------------------------------
-- DOCUMENTATION isShelling
------------------------------------------------------------
doc ///
     Key
          isShelling
	  (isShelling,List)
     Headline
          determines whether a list represents a shelling of a simplicial complex.
     Usage
          b = isShelling(P)
     Inputs
          P:List
	       A list of lists of integers.  Each list of integers is a facet of the complex and the order is a possible shelling.
     Outputs
          b:Boolean
	       true if and only if P is a shelling.
     Description
          Text
	      An ordering $F_1,..F_d$ of the facets of a simplicial complex $P$ is shellable
	       if $(F_1 \cup .. \cup F_{k-1}) \cap F_k$ is pure of dim$F_k -1$ for all $k = 2,..,d$.
              Determines if a list of faces is a shelling order of the simplicial complex. 
          Example
	      P = {{1, 2, 3}, {1, 2, 5}};
	      isShelling(P)
	      Q = {{1,2,3},{3,4,5},{2,3,4}};
	      isShelling(Q)	     
///


------------------------------------------------------------
-- DOCUMENTATION randomAddition
------------------------------------------------------------
doc ///
     Key
          randomAddition
	  (randomAddition,ZZ,ZZ,List)
	  (randomAddition,Ring,ZZ,List)
     Headline
          Adds a random facet to a shellable complex
     Usage
          p=randomAddition(n,m,P)
	  p=randomAddition(R,m,P)
     Inputs
     	  n:ZZ
	       the number of vertices (if a ring is specified, {\tt n} is the number of variables. 
	  m:ZZ
	       the dimension of the new facet
          P:List
	       A list of lists of integers.  Each list of integers is a facet of the complex and the order is a shelling.
	  R:Ring
	      A polynomial ring. 
     Outputs
          p:List
	       A list of lists of integers.  Each list of integers is a facet of the complex and the order is a shelling.
     Description
          Text
            This function randomly chooses a facet of size {\tt m+1} and checks whether the facet can be shellably added to the shelling. 
	    If it can be shellably added to the shelling, it is added to the shelling and the new shelling is returned. 
	    Otherwise, the process repeats up to 20 times.
          Text
	    This function can be used to randomly construct non-pure shellable complexes.  A new {\tt m}-simplex can only be
	    glued to previous simplices of dimension at least {\tt m}.  If all previous simplices are smaller, then the addition will fail.
          Example
            P={{1,2,3}}
	    P=randomAddition(6,2,P)
            P=randomAddition(6,1,P)
     Caveat
	  If the input is not a shellable simplicial complex, the new complex will not be shellable. The function does not check whether the input is shellable.
     SeeAlso
     	  randomShelling
	  idealChainFromShelling
	  idealFromShelling
///


------------------------------------------------------------
-- DOCUMENTATION idealFromShelling
------------------------------------------------------------
doc ///
     Key
          idealFromShelling
	  (idealFromShelling,List)
	  (idealFromShelling,Ring,List)
     Headline
          Produces an ideal from a shelling
     Usage
          I = idealFromShelling(P)
	  I = idealFromShelling(S,P)
     Inputs
	  S:Ring
	      (If omitted, it will use {\tt S=QQ[x_0..x_{n-1}]} where {\tt n} is the maximum integer in the lists of {\tt P}.  
          P:List
	       A list of lists of integers.  Each list of integers is a facet of the complex and the order is a shelling.
	  S:Ring
	      (If omitted, it will use {\tt S=QQ[x_0..x_{n-1}]} where {\tt n} is the maximum integer in the lists of {\tt P}.  
     Outputs
          I:Ideal
	      generated by the monomials representing the minimal nonfaces of {\tt P}
     Description
      Text
      	This gives the Stanley-Reisner ideal for the simplicial complex, that is the ideal generated by the monomials representing the minimal nonfaces of {\tt P}. 
      Example  
	      S = QQ[x_0,x_1,x_2,x_3,x_4]
	      P =  {{1, 2, 4}, {0, 1, 4}, {0, 2, 4}, {0, 3, 4}};
	      idealFromShelling(S,P)
     SeeAlso
    	idealChainFromShelling
	randomShellableIdeal
///


------------------------------------------------------------
-- DOCUMENTATION idealChainFromShelling
------------------------------------------------------------
doc ///
     Key
          idealChainFromShelling
	  (idealChainFromShelling,List)
	  (idealChainFromShelling,Ring,List)
     Headline
          Produces chains of ideals from a shelling.
     Usage
          L = idealChainFromShelling(P)
	  L = idealChainFromShelling(R,P)
     Inputs
     	  R:Ring
	      Polynomial ring
          P:List
	       A (possibly impure) shelled simplicial complex, represented by a
	       list of lists of integers.
	       Each list of integers is a facet of the 
	       complex and the order is a shelling. If the ring R is specified, the output
	       is a list of ideals in R; else it is a list of ideals in
	       QQ[x_0..x_{n-1}], where n is the maximum number of elements in one of the lists
	       of integers
     Outputs
          L:List
	      a list of ideals
     Description
          Text  
	     Outputs the Stanley-Reisner ideal for each successive simplicial complex formed by truncating the shelling. 
	  Example
	      P =  {{1, 2, 4}, {0, 1, 4}, {0, 2, 4}, {0, 3, 4}};
	      idealChainFromShelling(P)
     SeeAlso
     	 idealFromShelling
	 randomShellableIdealChain
     
///

------------------------------------------------------------
-- DOCUMENTATION randomShellableIdeal
------------------------------------------------------------
doc ///
     Key
          randomShellableIdeal
	  (randomShellableIdeal,Ring,ZZ,ZZ)
     Headline
          Produces a ideal from a random shellable simplicial complex
     Usage
          I = randomShellableIdeal(R,m,k)
     Inputs
          R:Ring
	      a polynomial ring
	  m:ZZ
	      dimension of facets in shellable complex
	  k:ZZ
	      the degree of the shellable complex
     Outputs
          I:MonomialIdeal
	      the Stanley-Reisner ideal of a random shellable complex
     Description
     	  Text  
	    The Stanley-Reisner ideal of a shellable simplicial complex is always
	    Cohen-Macaulay; the converse is not true, although, to paraphrase Arnol'd,
	    square-free monomial ideals that have a serious reason to be Cohen-Macaulay
	    generally do come from shellable complexes.
	    
	    The program makes a (Cohen-Macaulay) square-free monomial ideal
	    from the Stanley-Reisner ideal of a random shellable simplicial complex.
	    simplicial complex relies on the code for producing random shellable simplicial
	    complexes; see randomShelling for a description.
          Example
	      R = ZZ/101[x_0..x_4];
	      I = randomShellableIdeal(R,2,6)
     Caveat
     	  No claim is made on the distribution of the ideal.
     SeeAlso
     	 randomShelling
	 idealFromShelling
	 idealChainFromShelling
	 randomShellableIdealChain
///         

------------------------------------------------------------
-- DOCUMENTATION randomShellableIdealChain
------------------------------------------------------------
doc ///
     Key
          randomShellableIdealChain
	  (randomShellableIdealChain,Ring,ZZ,ZZ)
	  (randomShellableIdealChain,Ring,ZZ)
     Headline
          Produces a chain of ideals from a random shelling
     Usage
          L = randomShellableIdealChain(R,m,k)
	  L = randomShellableIdealChain(R,m)
     Inputs
          R:Ring
	      a polynomial ring
	  m:ZZ
	      dimension of the facets in the shellable complex
	  k:ZZ
	      the degree of the smallest ideal
     Outputs
          L:List
	      list of Stanley-Riesner ideals of the simplicial complexes of the truncations of the shelling.
     Description
     	  Text  
          Example
	      R = ZZ/101[x_0..x_3];
	      L = randomShellableIdealChain(R,1)
     Caveat
     	  No claim is made on the distribution of the ideal.
     SeeAlso
     	 randomShelling
	 idealFromShelling
	 idealChainFromShelling
	 randomShellableIdeal
///         

TEST///
setRandomSeed 0
S = ZZ/101[a,b,c,d,e]
I = randomShellableIdeal(S,2,3) 
I == monomialIdeal (a, c*d*e)
///

TEST///
assert(#randomShelling(5,2,6)==6)
assert(#randomShelling(5,2)==binomial(5,3))
R=QQ[x1,x2,x3,x4,x5];
assert(#randomShelling(R,2,6)==6)
///


TEST///
assert(isShelling({}))
assert(isShelling({{1,2,3}}))
assert(isShelling({{1,2,3},{2,3,4}}))
assert(isShelling(randomShelling(5,3,5)))
--non pure shellings
assert(isShelling({{1,2,3},{2,4}}))
assert(isShelling({{1},{2}}))
assert(not isShelling({{1,3},{2,4}}))
assert(isShelling({{1,2},{3}}))
assert(not isShelling({{3},{1,2}}))
///


TEST///
setRandomSeed(0);
assert(#randomAddition(6,2,{{1,2,3}})==2)
assert(#randomAddition(6,3,{{1,2,3,4}})==2)
///

TEST///
needsPackage "SimplicialComplexes"
needsPackage "SimplicialDecomposability"
R=QQ[x1,x2,x3,x4,x5];
assert(isShellable simplicialComplex randomShelling(R,2,6))
///




beginDocumentation()




doc ///
Key 
   randomMonomial
   (randomMonomial, ZZ, Ring)
Headline 
   Choose a random monomial of given degree in a given ring
Usage 
   m = randomMonomial(d,S)
Inputs 
   d: ZZ
       non-negative
   S: Ring
       polynomial ring
Outputs
   m: RingElement
       monomial of S
Description
 Text
    Chooses a random monomial.
 Example
    setRandomSeed(currentTime())
    kk=ZZ/101
    S=kk[a,b,c]
    randomMonomial(3,S)
SeeAlso
 randomMonomialIdeal
 randomSquareFreeMonomialIdeal
///


doc ///
Key
  randomSquareFreeMonomialIdeal
  (randomSquareFreeMonomialIdeal, List, Ring)
  (randomSquareFreeMonomialIdeal, Sequence, Ring)
Headline
  random square-free monomial ideal with given degree generators
Usage
  I = randomSquareFreeMonomialIdeal(L,S)
Inputs
  L:List
   or sequence of non-negative integers
  S:Ring
   Polynomial ring
Outputs
  I:Ideal
   square-free monomial ideal with generators of specified degrees
Description
 Text
  Choose a random square-free monomial
  ideal whose generators have the degrees 
  specified by the list or sequence L.
 Example
  setRandomSeed(currentTime())
  kk=ZZ/101
  S=kk[a..e]
  L={3,5,7}
  randomSquareFreeMonomialIdeal(L, S)
  randomSquareFreeMonomialIdeal(5:2, S)
Caveat 
  The ideal is constructed degree by degree, starting from the lowest degree
  specified. If there are not enough monomials of the next specified degree that
  are not already in the ideal, the function prints a warning and returns an ideal
  containing all the generators of that degree.
SeeAlso
  randomMonomial
  randomMonomialIdeal
///

doc ///
Key
  randomSquareFreeStep
  (randomSquareFreeStep, MonomialIdeal) 
  (randomSquareFreeStep, Ideal) 
  (randomSquareFreeStep, List) 
  [randomSquareFreeStep,AlexanderProbability]
Headline
 A step in a random walk with uniform distribution over all monomial ideals
Usage
  M = randomSquareFreeStep(I)
  M = randomSquareFreeStep(I, AlexanderProbability => p)  
  M = randomSquareFreeStep(L)
  M  = randomSquareFreeStep(L, AlexanderProbability => p)
Inputs
  I:Ideal
   square-free monomial Ideal or MonomialIdeal
  L:List
   {I,Igens,ISocgens} where I is a square-free MonomialIdeal,
   Igens is a List of its minimal generators,
   ISocgens is a List of the minimal generators of the socle mod I.
Outputs
  M:List
   {J,Jgens,JSocgens} where J is a square-free MonomialIdeal,
   Jgens is a List of its minimal generators,
   JSocgens is a List of the minimal generators of the socle mod J.
Description
  Text
   With probability p the routine takes the Alexander dual of I;
   the default value of p is .05, and it can be set with the option
   AlexanderProbility.
  
   Otherwise uses the Metropolis algorithm to produce a random walk on the space
   of square-free ideals. Note that there are a LOT of square-free ideals;
   these are the Dedekind numbers, and the sequence (with 1,2,3,4,5,6,7,8 variables) 
   begins
   3,6,20,168,7581, 7828354, 2414682040998, 56130437228687557907788.
   (see the Online Encyclopedia of Integer Sequences for more information).
   Given I in a polynomial ring S, we make a list
   ISocgens of the square-free minimal monomial generators of the socle of S/(squares+I)
   and a list of minimal generators Igens of I. A candidate "next" ideal J is formed as follows:
   We choose randomly from the 
   union of these lists; if a socle element is chosen, it's added to I; if
   a minimal generator is chosen, it's replaced by the square-free part of
   the maximal ideal times it.
   the chance of making the given move is then 1/(#ISocgens+#Igens), and
   the chance of making the move back would be the similar quantity for J,
   so we make the move or not depending on whether 
   random RR < (nJ+nSocJ)/(nI+nSocI) or not; here random RR is
   a random number in [0,1].
  Example
   setRandomSeed(currentTime())
   S=ZZ/2[vars(0..3)]
   J = monomialIdeal"ab,ad, bcd"
   randomSquareFreeStep J
  Text
   With 4 variables and 168 possible monomial ideals, a run of 5000
   takes less than 6 seconds on a reasonably fast machine. With
   10 variables a run of 1000 takes about 2 seconds.
  Example
   setRandomSeed(1)
   rsfs = randomSquareFreeStep
   J = monomialIdeal 0_S
   time T=tally for t from 1 to 5000 list first (J=rsfs(J,AlexanderProbability => .01));
   #T
   T
   J
///


doc ///
Key
  AlexanderProbability
Headline
  option to randomSquareFreeStep
Usage
  M = randomSquareFreeStep(L, AlexanderProbability => p)
Inputs
  p: RR
   real number between 0 and 1.
Description
 Text
  Controls how often the Alexander dual is taken
SeeAlso
  randomSquareFreeStep
///


doc ///
Key
  squareFree  
Headline
  ideal of all square-free monomials of given degree
Usage
  I = squareFree(d,S)
Inputs
  d:ZZ
   positive
  S:Ring
   Polynomial ring
Outputs
  I:Ideal
   all square-free monomials of degree d
Description
 Example
  kk=ZZ/101
  S=kk[a..e]
  squareFree(3, S)
SeeAlso
  randomSquareFreeMonomialIdeal
///

doc ///
Key
  (squareFree, ZZ, Ring)
Headline
  ideal of all square-free monomials of given degree
Usage
  I = squareFree(d,S)
Inputs
  d:ZZ
   positive
  S:Ring
   Polynomial ring
Outputs
  I:Ideal
   all square-free monomials of degree d
Description
 Example
  kk=ZZ/101
  S=kk[a..e]
  squareFree(3, S)
SeeAlso
  randomSquareFreeMonomialIdeal
///

doc ///
Key
  regSeq
  (regSeq, List, Ring)
  (regSeq, Sequence, Ring)  
Headline
  regular sequence of powers of the variables, in given degrees
Usage
  I = regSeq(L,S)
Inputs
  L:List
   or sequence of positive integers
  S:Ring
   Polynomial ring
Outputs
  I:Ideal
   generated by the given powers of the variables
Description
 Example
  kk=ZZ/101
  S=kk[a..e]
  regSeq((2,3,4),S)
Caveat
 If the number of elements of L differs from the number of
 variables in the ring, the length of the regular sequence
 will be the minimum of the two.
///

doc ///
Key
  randomIdeal
  (randomIdeal, List, Matrix)
  (randomIdeal, Sequence, Matrix)
Headline
  randomIdeal made from a given set of monomials
Usage
  I = randomIdeal(L,m)
Inputs
  L:List
   or sequence of positive integers
  m: Matrix
   1xn matrix of homogeneous polynomials in a ring S
Outputs
  I:Ideal
   generated by random linear combinations of degrees given by L of the given polynomials
Description
 Text
  This function composes m with a random map from a free module with degrees
  specified by L to the source of m.
 Example
  kk=ZZ/101
  S=kk[a..e]
  L={3,3,4,6}
  m = matrix{{a^3,b^4+c^4,d^5}}
  I=randomIdeal(L,m)
SeeAlso
 randomMonomialIdeal
 randomSquareFreeMonomialIdeal
 randomMonomial
 randomBinomialIdeal
 randomPureBinomialIdeal
 randomElementsFromIdeal
///

doc ///
Key
  randomBinomialIdeal
  (randomBinomialIdeal, List, Ring)
  (randomBinomialIdeal, Sequence, Ring)
Headline
  randomBinomialIdeal with binomials of given degrees
Usage
  I = randomBinomialIdeal(L,S)
Inputs
  L:List
   or sequence of positive integers
  S: Ring
   Polynomial ring
Outputs
  I:Ideal
   generated by random binomials of the given degrees
Description
 Example
  kk=ZZ/101
  S=kk[a..e]
  L={3,3,4,6}
  I=randomBinomialIdeal(L,S)
Caveat
  The binomials are generated one at a time, and there is no checking to
  see whether the ideal returned is minally generated by fewer elements,
  so the number of minimal generators may not be what you expect.
SeeAlso
 randomIdeal
 randomMonomialIdeal
 randomSquareFreeMonomialIdeal
 randomMonomial
 randomPureBinomialIdeal
 randomElementsFromIdeal
///

doc ///
Key
  randomPureBinomialIdeal
  (randomPureBinomialIdeal, List, Ring)
  (randomPureBinomialIdeal, Sequence, Ring)
Headline
  randomPureBinomialIdeal with binomials of given degrees
Usage
  I = randomPureBinomialIdeal(L,S)
Inputs
  L:List
   or sequence of positive integers
  S: Ring
   Polynomial ring
Outputs
  I:Ideal
   generated by random pure binomials (that is, differences of monomials without coefficients)  of the given degrees
Description
 Text
 Example
  kk=ZZ/101
  S=kk[a..e]
  L={3,3,4,6}
  I=randomPureBinomialIdeal(L,S)
Caveat
  The binomials are generated one at a time, and there is no checking to
  see whether the ideal returned is minally generated by fewer elements,
  so the number of minimal generators may not be what you expect.
SeeAlso
 randomIdeal
 randomMonomialIdeal
 randomSquareFreeMonomialIdeal
 randomMonomial
 randomBinomialIdeal
 randomElementsFromIdeal
///

doc ///
Key
  randomSparseIdeal
  (randomSparseIdeal, Matrix, ZZ, ZZ)
Headline
  randomSparseIdeal made from a given set of monomials
Usage
  I = randomSparseIdeal(B,s,n)
Inputs
  B:Matrix
   1xn matrix of monomials
  s: ZZ
   positive integer, the number of terms in the generators of I
  n: ZZ
   positive integer, the number of generators of I
Outputs
  I:Ideal
   generated by n polynomials, each a random linear combination of s monomials 
Description
 Text
  Each generator of I is formed by randomly choosing s (the sparsity) entries 
  of the matrix B and taking a random linear combinations with coefficients in
  the (ultimate) coefficient ring of S, the ring in which the monomials lie.
 Example
  kk=ZZ/101
  S=kk[a..e]
  L={3,3,4,6}
  B = matrix{{a^3,b^4,d^5,a*b*c,e}}
  I=randomSparseIdeal(B,3,2)
SeeAlso
 randomIdeal
 randomMonomialIdeal
 randomSquareFreeMonomialIdeal
 randomMonomial
 randomBinomialIdeal
 randomPureBinomialIdeal
 randomElementsFromIdeal
///


doc ///
Key
  randomElementsFromIdeal
  (randomElementsFromIdeal,List, Ideal)
Headline
  Chooses random elements of given degrees in a given ideal.
Usage
  I = randomElementsFromIdeal(L,I)
Inputs
  L:List
   of integers
  I:Ideal
   that should be homogeneous
Outputs
  I:Ideal
   generated by (at most) n homogeneous polynomials that are random linear combination of the
   generators of I, with degrees specified by the list L
Description
 Example
  kk=ZZ/101
  S=kk[a..c]
  L={3,3,4,6}
  I = ideal(a^3,b^3, c^3)
  J=randomElementsFromIdeal(L,I)
SeeAlso
 "Finding Extreme Examples"
 randomIdeal
 randomMonomialIdeal
 randomSquareFreeMonomialIdeal
 randomMonomial
 randomBinomialIdeal
 randomPureBinomialIdeal
///


doc ///
Key
  randomMonomialIdeal
  (randomMonomialIdeal, List, Ring)
  (randomMonomialIdeal, Sequence, Ring)
Headline
  random monomial ideal with given degree generators
Usage
  I = randomMonomialIdeal(L,S)
Inputs
  L:List
   or sequence of non-negative integers
  S:Ring
   Polynomial ring
Outputs
  I:Ideal
   monomial ideal with generators of specified degrees
Description
 Text
  Choose a random ideal whose generators have the degrees specified by the list or sequence L.
 Example
  kk=ZZ/101
  S=kk[a..e]
  L={3,5,7}
  randomMonomialIdeal(L, S)
  randomMonomialIdeal(5:2, S)
Caveat 
  The ideal is constructed degree by degree, starting from the lowest degree
  specified. If there are not enough monomials of the next specified degree that
  are not already in the ideal, the function prints a warning and returns an ideal
  containing all the generators of that degree.
SeeAlso
  randomMonomial
  randomSquareFreeMonomialIdeal
///



doc ///
Key
  "Finding Extreme Examples"
Headline
  Ways to use random ideals to search for (counter)-examples
Description
 Text
  A common use of Macaulay2 is to look for extreme or particularly
  interesting examples. Here are some examples of how this may be done.
   
  Supposing first that some space of examples is finite; for
  example, we might be interested in monomial ideals with a certain
  number of generators of a certain degree d. Suppose, to be concrete,
  that we want to compare the
  maximum degree of a first syzygy with the regularity of the ideal,
  and also with the maximum degree of the last syzygy. (To make the 
  comparison interesting, it seems reasonable to subtract i from the maximum
  degree of an i-th syzygy.)
 Text
  We may have no idea where to look for extreme examples, and it seems
  that examples with small numbers of variables and generators may not
  show the range of phenomena that actually occur. In large degree there
  may be too many examples to search systematically; so instead we may
  choose many examples at random, and hope to see a pattern. 
   
  Here is a simple example
  First we tally the projective dimensions of 500
  random square-free monomial ideals (what's the average?), then 
  looking how big the difference between the regularity of R/I and the
  "relation degree"-2 can be. It turns out this the differences are rather
  small, only 1 in a typical run of 5000. So one might look for ideals with
  a difference of 2, as in the following (in a real run, one would make
  the number of iterations much bigger; here we keep it small so
  that Macaulay2 doesn't take too long to build it's documentation files.)
 Example
  kk=ZZ/101
  S=kk[vars(0..5)]
  L=for n from 1 to 100 list res randomSquareFreeMonomialIdeal(10:3,S);
  tally apply(L, F -> length F)
  tally apply(L, F -> regularity F - ((max flatten degrees F_2) - 2))
  L=for n from 1 to 500 list res randomSquareFreeMonomialIdeal(10:3,S);
  scan(L, F -> if 1<(regularity F - (max flatten degrees F_2) + 2) then print F.dd_1)
 Text
  A typical problem might be to find how high the regularity of R/I can
  be when R has reasonably few variables, and the degrees of the generators of
  I are reasonably small; despite the wild examples of Mayr-Mayer, we don't
  know how to make examples with large regularity without letting the
  number of variables become large. The following program computes
  "rep" examples of random ideals with monomial and binomial generators,
  and prints any whose regularity exceeds the number "bound"

    
  looper = (rep,bound, L1, L2) -> (for i from 1 to rep do (
  if i % 1000 == 0 then << "." << flush;
  J := randomMonomialIdeal(L1,S) + randomBinomialIdeal(L2,S);
  m := regularity coker gens J;
  if m >= bound then << "reg " << m << " " << toString J << endl;))


  For example:
  kk=ZZ/2
  S=kk[a,b,c,d]
  looper(30000,10,{4},{4,4}) -- finds examples with on monomial of degree 4
  and 2 binomials of degree 4. The largest largest regularity it has found
  (and the largest I know for an ideal in 4 variables of degree 4) is 14.
  Here is an example it found:
  ideal(a*b^3,a^4+b^4,b*c^3+a*d^3)
   
  Similarly:
  
  looper(30000,10,{4,4},{4}) -- looks for examples with
  2 monomials and 1 binomial of degree 4. Suggestively, the
  largest regularity it found was also 14:
  betti res ideal(c^4,b^4,a^3*c+b*d^3) -- reg 14
 Text   
  A more sophisticated and difficult situation arises when the space
  of examples is not necessarily finite (except over a finite field) but
  is a unirational
  variety (such as the space of ideals generated by (at most) a certain
  number of forms of certain given degrees, or the space of smooth curves
  of genus g for some g <= 14) one may be able to do a search for random
  examples, taking a rational parametrization of the space of examples
  and plugging in random inputs.
   
  If the "interesting" examples live in
  a subvariety whose codimension is small, then, working over a small field
  (say 2,3, or 5 elements) one might hope to see elements of the subvariety
  "not too rarely". This principle has been used to good effect for example
  by (Caviglia and Decker-Schreyer, ****--Schreyer). 
SeeAlso
  randomIdeal
  randomMonomialIdeal
  randomSquareFreeMonomialIdeal
  randomMonomial
  randomBinomialIdeal
  randomPureBinomialIdeal
///

--Documentation
doc ///
   Key
    randomEdgeIdeal
    (randomEdgeIdeal, ZZ, ZZ)
   Headline
    Creates an edge ideal from a random graph with n vertices and t edges.
   Usage
    (I,G) = randomEdgeIdeal(n,t)
   Inputs
    n:ZZ
     number of vertices
    t:ZZ
     number of edges
   Outputs
    I:Ideal
     a random edge ideal
    G:Graph
     the graph underlying I
   Description
    Text
     The edge ideal of a graph is the quadratic monomial ideal generated by x_v*x_w for all edges (v,w) in the graph.
     This method returns the edge ideal {\tt I} of a random graph {\tt G} which has n vertices and t edges.
    Example
     randomEdgeIdeal(7, 4) 
   SeeAlso
    randomGraph
    edgeIdeal
///

doc ///
   Key
    randomBinomialEdgeIdeal
    (randomBinomialEdgeIdeal, ZZ, ZZ)
   Headline
    Creates a binomial edge ideal from a random graph with n vertices and t edges.
   Usage
    (I,G) = randomBinomialEdgeIdeal(n,t)
   Inputs
    n:ZZ
     number of vertices
    t:ZZ
     number of edges
   Outputs
    I:Ideal
     a random binomial edge ideal
    G:Graph
     the graph underlying I
   Description
    Text
     The binomial edge ideal associated to a graph G is the quadratic binomial ideal generated by the set containing x_v*y_w-x_w-y_v for every edge (v,w) in G.
     This method returns the binomial edge ideal {\tt I} of a random graph {\tt G} which has n vertices and t edges.
    Example
     randomBinomialEdgeIdeal(7, 4) 
   SeeAlso
    randomGraph
    binomialEdgeIdeal
    randomToricEdgeIdeal
///

doc ///
   Key
    randomToricEdgeIdeal
    (randomToricEdgeIdeal, ZZ, ZZ)
   Headline
    Creates a toric edge ideal from a random graph with n vertices and t edges.
   Usage
    (I, G) = randomToricEdgeIdeal(n,t)
   Inputs
    n:ZZ
     number of vertices
    t:ZZ
     number of edges
   Outputs
    I:Ideal
     a random toric edge ideal
    G:Graph
     the graph underlying I
   Description
    Text
     The toric edge ideal of a graph G is the kernel of the map from the polynomial ring k[edges(G)] to the polynomial ring k[vertices G] taking an x_e to y_i*y_j, where e = (i,j).
     This method returns the toric edge ideal {\tt I} of a random graph {\tt G} which has n vertices and t edges.  {\tt I} is the kernel of the homomorphism from QQ[x_1..x_n] to QQ/101[e_1..e_t] which sends each vertex in the graph {\tt G} to the product of its endpoints.
    Example
     randomToricEdgeIdeal(4,5)     
    Text
     Note that his is different than the randomBinomialEdgeIdeal!
    Example
     randomBinomialEdgeIdeal(4,5)
   SeeAlso
    randomGraph
    randomBinomialEdgeIdeal
///


TEST ///
S=ZZ/101[a..e]
setRandomSeed 123456
assert (randomMonomial(7,S)==a*b^3*c^3)
setRandomSeed 123456
assert(randomMonomialIdeal({3,4,5}, S)==ideal(d*e^2,a*b*d^2,b*c^3*e))
setRandomSeed 123456
assert(randomSquareFreeMonomialIdeal({6,4,4},S)==ideal(a*b*c*e,a*c*d*e))
setRandomSeed 123456
assert(ideal(8*a^2+5*a*b+4*b^2+35*b*c+3*b*d+36*b*e,29*a^2+22*a*b+32*b^2-44*b*c-6*b*d+40*b*e) == randomIdeal({2,2},matrix{{a^2,b}}))
setRandomSeed 123456
assert(ideal(-29*b^2+b*e,-4*a^2+b*c,45*a*d) == randomBinomialIdeal({2,2,2},S))
setRandomSeed 123456
assert(ideal(b*e-d*e,b^2-b*c,-a^2+a*e)== randomPureBinomialIdeal({2,2,2}, S))
setRandomSeed 123456
assert(randomSparseIdeal(matrix"a2,ab,b2", 2,2)==ideal(8*a*b+5*b^2,4*a^2+35*a*b))
assert(ideal(a*b,a*c,a*d,a*e,b*c,b*d,b*e,c*d,c*e,d*e)==squareFree(2,S))
assert( regSeq((1,2,3,4,5,6), S)==ideal(a,b^2,c^3,d^4,e^5))
setRandomSeed 123456
assert(degrees randomElementsFromIdeal({2,3,6},ideal"a2,ab,c5") == {{2}, {3}, {6}})
S=ZZ/2[a,b]
setRandomSeed 1
--assert(prepare monomialIdeal(a^2, a*b)=={monomialIdeal (a^2 , a*b), {a^2 , a*b}, {0, 1}})
setRandomSeed 1
S=ZZ/2[vars(0..3)]
J = ideal"ab,ad, bcd"
assert( (randomSquareFreeStep J) === {monomialIdeal map((S)^1,(S)^{{-2},{-2}},{{a*b, a*d}}),{a*b,a*d},{b*c*d,a*c}} );
///




end--  
restart
loadPackage ("RandomIdeals", Reload =>true)
load "RandomIdeals.m2"

uninstallPackage "RandomIdeals"
restart
installPackage "RandomIdeals"
check "RandomIdeals"
viewHelp RandomIdeals