-
Notifications
You must be signed in to change notification settings - Fork 2
/
Polyhedra.m2
8963 lines (7590 loc) · 333 KB
/
Polyhedra.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
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
--*- coding: utf-8 -*-
---------------------------------------------------------------------------
--
-- PURPOSE: Computations with convex polyhedra
-- PROGRAMMER : René Birkner
-- UPDATE HISTORY : April 2008, December 2008, March 2009, Juli 2009,
-- September 2009, October 2009, January 2010
---------------------------------------------------------------------------
newPackage("Polyhedra",
Headline => "A package for computations with convex polyhedra",
Version => "1.2",
Date => "March 30, 2011",
Certification => {
"journal name" => "The Journal of Software for Algebra and Geometry: Macaulay2",
"journal URI" => "http://j-sag.org/",
"article title" => "Polyhedra: a package for computations with convex polyhedral objects",
"acceptance date" => "2009-09-07",
"published article URI" => "http://j-sag.org/Volume1/jsag-3-2009.pdf",
"published code URI" => "http://j-sag.org/Volume1/Polyhedra.m2",
"repository code URI" => "svn://svn.macaulay2.com/Macaulay2/trunk/M2/Macaulay2/packages/Polyhedra.m2",
"release at publication" => 9344,
"version at publication" => "1.0.5",
"volume number" => "1",
"volume URI" => "http://j-sag.org/Volume1/"
},
Authors => {
{Name => "René Birkner",
HomePage => "http://page.mi.fu-berlin.de/rbirkner/index.htm",
Email => "[email protected]"}},
DebuggingMode => false
)
---------------------------------------------------------------------------
-- COPYRIGHT NOTICE:
--
-- Copyright 2010 René Birkner
--
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program 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.
--
-- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
--
---------------------------------------------------------------------------
export {"PolyhedralObject",
"Polyhedron",
"Cone",
"Fan",
"PolyhedralComplex",
"convexHull",
"posHull",
"intersection",
"fan",
"addCone",
"polyhedralComplex",
"addPolyhedron",
"ambDim",
"cones",
"maxCones",
"maxPolyhedra",
"halfspaces",
"hyperplanes",
"linSpace",
"polyhedra",
"rays",
"vertices",
"areCompatible",
"commonFace",
"contains",
"isCompact",
"isComplete",
"isEmpty",
"isFace",
"isLatticePolytope",
"isPointed",
"isPolytopal",
"isPure",
"isReflexive",
"isSimplicial",
"isSmooth",
"isVeryAmple",
"boundaryMap",
"dualFaceLattice",
"faceLattice",
"faceOf",
"faces",
"fVector",
"hilbertBasis",
"incompCones",
"incompPolyhedra",
"inInterior",
"interiorPoint",
"interiorVector",
"interiorLatticePoints",
"latticePoints",
"maxFace",
"minFace",
"objectiveVector",
"minkSummandCone",
"mixedVolume",
"polytope",
"proximum",
"skeleton",
"smallestFace",
"smoothSubfan",
"stellarSubdivision",
"tailCone",
"triangulate",
"volume",
"vertexEdgeMatrix",
"vertexFacetMatrix",
"affineHull",
"affineImage",
"affinePreimage",
"bipyramid",
"ccRefinement",
"coneToPolyhedron",
"directProduct",
"dualCayley",
"dualCayleyFace",
"dualCone",
"faceFan",
"imageFan",
"minkowskiSum",
"normalFan",
"polar",
"polarFace",
"pyramid",
"sublatticeBasis",
"toSublattice",
"crossPolytope",
"cellDecompose",
"cyclicPolytope",
"ehrhart",
"emptyPolyhedron",
"hirzebruch",
"hypercube",
"newtonPolytope",
"posOrthant",
"secondaryPolytope",
"statePolytope",
"stdSimplex",
"saveSession"}
needsPackage "FourierMotzkin"
-- WISHLIST
-- -Symmetry group for polytopes
-- Definind the new type PolyhedralObject
PolyhedralObject = new Type of HashTable
globalAssignment PolyhedralObject
-- Defining the new type Polyhedron
Polyhedron = new Type of PolyhedralObject
Polyhedron.synonym = "convex polyhedron"
globalAssignment Polyhedron
-- Defining the new type Cone
Cone = new Type of PolyhedralObject
Cone.synonym = "convex rational cone"
globalAssignment Cone
-- Defining the new type Fan
Fan = new Type of PolyhedralObject
globalAssignment Fan
-- Defining the new type PolyhedralComplex
PolyhedralComplex = new Type of PolyhedralObject
globalAssignment PolyhedralObject
-- Modifying the standard output for a polyhedron to give an overview of its characteristica
net Polyhedron := P -> ( horizontalJoin flatten (
"{",
-- prints the parts vertically
stack (horizontalJoin \ sort apply({"ambient dimension",
"dimension of polyhedron",
"dimension of lineality space",
"number of rays",
"number of vertices",
"number of facets"}, key -> (net key, " => ", net P#key))),
"}" ))
-- Modifying the standard output for a Cone to give an overview of its characteristica
net Cone := C -> ( horizontalJoin flatten (
"{",
-- prints the parts vertically
stack (horizontalJoin \ sort apply({"ambient dimension",
"dimension of the cone",
"dimension of lineality space",
"number of rays",
"number of facets"}, key -> (net key, " => ", net C#key))),
"}" ))
-- Modifying the standard output for a Fan to give an overview of its characteristica
net Fan := F -> ( horizontalJoin flatten (
"{",
-- prints the parts vertically
stack (horizontalJoin \ sort apply({"ambient dimension",
"top dimension of the cones",
"number of generating cones",
"number of rays"}, key -> (net key, " => ", net F#key))),
"}" ))
-- Modifying the standard output for a Polyhedral Complex to give an overview of its characteristica
net PolyhedralComplex := F -> ( horizontalJoin flatten (
"{",
-- prints the parts vertically
stack (horizontalJoin \ sort apply({"ambient dimension",
"top dimension of the polyhedra",
"number of generating polyhedra"}, key -> (net key, " => ", net F#key))),
"}" ))
-- PURPOSE : Computing the Convex Hull of a given set of points and rays
convexHull = method(TypicalValue => Polyhedron)
-- INPUT : 'Mvert' a Matrix containing the generating points as column vectors
-- 'Mrays' a Matrix containing the generating rays as column vectors
-- OUTPUT : 'P' a Polyhedron
-- COMMENT : The description by vertices and rays is stored in P as well as the
-- description by defining half-spaces and hyperplanes.
convexHull(Matrix,Matrix) := (Mvert,Mrays) -> (
-- checking for input errors
if numgens target Mvert =!= numgens target Mrays then error ("points and rays must lie in the same space");
Mvert = chkZZQQ(Mvert,"points");
Mrays = chkZZQQ(Mrays,"rays");
if numRows Mvert == 0 then Mvert = matrix{{0}};
if numColumns Mvert == 0 then Mvert = map(target Mvert,QQ^1,0);
if numRows Mrays == 0 then Mrays = matrix{{0}};
if numColumns Mrays == 0 then Mrays = map(target Mrays,QQ^1,0);
-- homogenization of M
Mvert = map(QQ^1,source Mvert,(i,j)->1) || Mvert;
Mrays = map(QQ^1,source Mrays,0) || Mrays;
M := Mvert | Mrays;
-- Computing generators of the cone M and its dual cone
hyperA := fourierMotzkin M;
-- verticesA := fourierMotzkin hyperA;
local verticesA;
(verticesA,hyperA) = fMReplacement(M,hyperA#0,hyperA#1);
polyhedronBuilder(hyperA,verticesA))
-- INPUT : 'M' a Matrix containing the generating points as column vectors
convexHull Matrix := M -> (
-- Checking for input errors
M = chkZZQQ(M,"points");
if numRows M == 0 then M = matrix{{0}};
if numColumns M == 0 then M = map(target M,QQ^1,0);
-- Generating the zero ray R
R := map(target M,QQ^1,0);
convexHull(M,R))
-- INPUT : '(P1,P2)' two polyhedra
convexHull(Polyhedron,Polyhedron) := (P1,P2) -> (
-- Checking for input errors
if P1#"ambient dimension" =!= P2#"ambient dimension" then error("Polyhedra must lie in the same ambient space");
-- Combining the vertices/rays and the lineality spaces in one matrix each
M := (P1#"homogenizedVertices")#0 | (P2#"homogenizedVertices")#0;
LS := (P1#"homogenizedVertices")#1 | (P2#"homogenizedVertices")#1;
hyperA := fourierMotzkin(M,LS);
-- verticesA := fourierMotzkin hyperA;
local verticesA;
(verticesA,hyperA) = fMReplacement(M,hyperA#0,hyperA#1);
polyhedronBuilder(hyperA,verticesA))
-- INPUT : 'L', a list of Cones, Polyhedra, vertices given by M,
-- and (vertices,rays) given by '(V,R)'
convexHull List := L -> (
-- This function checks if the inserted pair is a pair of matrices that give valid vertices and rays
isValidPair := S -> #S == 2 and if S#1 == 0 then instance(S#0,Matrix) else instance(S#1,Matrix) and numRows S#0 == numRows S#1;
-- Checking for input errors
if L == {} then error("List of convex objects must not be empty");
P := L#0;
-- The first entry in the list determines the ambient dimension 'n'
n := 0;
local V;
local R;
if (not instance(P,Cone)) and (not instance(P,Polyhedron)) and (not instance(P,Sequence)) and (not instance(P,Matrix)) then
error ("The input must be cones, polyhedra, vertices, or (vertices,rays).");
-- Adding the vertices and rays to 'V,R', depending on the type of 'P'
if instance(P,Cone) then (
n = P#"ambient dimension";
V = map(QQ^n,QQ^1,0);
R = rays P | linSpace P | -(linSpace P))
else if instance(P,Polyhedron) then (
n = P#"ambient dimension";
V = vertices P;
R = rays P | linSpace P | -(linSpace P))
else if instance(P,Sequence) then (
-- Checking for input errors
if not isValidPair(P) then error ("Vertices and rays must be given as a sequence of two matrices with the same number of rows");
V = chkZZQQ(P#0,"vertices");
n = numRows V;
if P#1 == 0 then R = map(ZZ^n,ZZ^1,0)
else R = chkQQZZ(P#1,"rays"))
else (
V = chkZZQQ(P,"vertices");
n = numRows P;
R = map(ZZ^n,ZZ^1,0));
-- Adding the vertices and rays to 'V,R', for each remaining element in 'L', depending on the type of 'P'
L = apply(drop(L,1), C1 -> (
-- Checking for further input errors
if (not instance(C1,Cone)) and (not instance(C1,Polyhedron)) and (not instance(C1,Sequence)) and
(not instance(C1,Matrix)) then error("The input must be cones, polyhedra, vertices, or (vertices,rays).");
if instance(C1,Cone) then (
if ambDim C1 != n then error("All Cones and Polyhedra must be in the same ambient space");
({},rays C1 | linSpace C1 | -(linSpace C1)))
else if instance(C1,Polyhedron) then (
if ambDim C1 != n then error("All Cones and Polyhedra must be in the same ambient space");
(vertices C1,rays C1 | linSpace C1 | -(linSpace C1)))
else if instance(C1,Sequence) then (
-- Checking for input errors
if not isValidPair(C1) then error("(Vertices,rays) must be given as a sequence of two matrices with the same number of rows");
if numRows C1#0 != n then error("(Vertices,rays) must be of the correct dimension.");
if C1#1 != 0 then (chkZZQQ(C1#0,"vertices"),chkQQZZ(C1#1,"rays"))
else (chkZZQQ(C1#0,"vertices"),{}))
else (
-- Checking for input errors
if numRows C1 != n then error("Vertices must be of the correct dimension.");
(chkZZQQ(C1,"vertices"),{}))));
LV := flatten apply(L, l -> l#0);
if LV != {} then V = V | matrix {LV};
L = flatten apply(L, l -> l#1);
if L != {} then R = R | matrix {L};
if R == 0 then convexHull V else convexHull(V,R))
-- PURPOSE : Computing the positive hull of a given set of rays lineality
-- space generators
posHull = method(TypicalValue => Cone)
-- INPUT : 'Mrays' a Matrix containing the generating rays as column vectors
-- 'LS' a Matrix containing the generating rays of the
-- lineality space as column vectors
-- OUTPUT : 'C' a Cone
-- COMMENT : The description by rays and lineality space is stored in C as well
-- as the description by defining half-spaces and hyperplanes.
posHull(Matrix,Matrix) := (Mrays,LS) -> (
-- checking for input errors
if numRows Mrays =!= numRows LS then error("rays and linSpace generators must lie in the same space");
Mrays = chkZZQQ(Mrays,"rays");
LS = chkZZQQ(LS,"lineality space");
-- Computing generators of the cone and its dual cone
dualgens := fourierMotzkin(Mrays,LS);
local genrays;
(genrays,dualgens) = fMReplacement(Mrays,dualgens#0,dualgens#1);
-- genrays := fourierMotzkin dualgens;
coneBuilder(genrays,dualgens))
-- INPUT : 'R' a Matrix containing the generating rays as column vectors
posHull Matrix := R -> (
R = chkZZQQ(R,"rays");
-- Generating the zero lineality space LS
LS := map(target R,QQ^1,0);
posHull(R,LS))
-- INPUT : '(C1,C2)' two cones
posHull(Cone,Cone) := (C1,C2) -> (
-- Checking for input errors
if C1#"ambient dimension" =!= C2#"ambient dimension" then error("Cones must lie in the same ambient space");
-- Combining the rays and the lineality spaces into one matrix each
R := C1#"rays" | C2#"rays";
LS := C1#"linealitySpace" | C2#"linealitySpace";
dualgens := fourierMotzkin(R,LS);
local genrays;
(genrays,dualgens) = fMReplacement(R,dualgens#0,dualgens#1);
-- genrays := fourierMotzkin dualgens;
coneBuilder(genrays,dualgens))
-- INPUT : 'P' a Polyhedron
posHull Polyhedron := P -> (
Mrays := makePrimitiveMatrix P#"vertices" | P#"rays";
Mlinspace := P#"linealitySpace";
posHull(Mrays,Mlinspace))
-- INPUT : 'L', a list of Cones, Polyhedra, rays given by R,
-- and (rays,linSpace) given by '(R,LS)'
posHull List := L -> (
-- This function checks if the inserted pair is a pair of matrices that gives valid rays and linSpace
isValidPair := S -> #S == 2 and if S#1 == 0 then instance(S#0,Matrix) else instance(S#1,Matrix) and numRows S#0 == numRows S#1;
-- Checking for input errors
if L == {} then error("List of convex objects must not be empty");
C := L#0;
-- The first entry in the list determines the ambient dimension 'n'
n := 0;
local R;
local LS;
if (not instance(C,Cone)) and (not instance(C,Polyhedron)) and (not instance(C,Sequence)) and (not instance(C,Matrix)) then
error ("The input must be cones, polyhedra, rays, or (rays,linSpace).");
-- Adding the vertices and rays to 'R,LS', depending on the type of 'C'
if instance(C,Cone) then (
n = C#"ambient dimension";
R = rays C;
LS = linSpace C)
else if instance(C,Polyhedron) then (
n = C#"ambient dimension";
R = makePrimitiveMatrix vertices C | rays C;
LS = linSpace C)
else if instance(C,Sequence) then (
-- Checking for input errors
if not isValidPair C then error("Rays and lineality space must be given as a sequence of two matrices with the same number of rows");
R = chkQQZZ(C#0,"rays");
n = numRows R;
LS = if C#1 == 0 then map(ZZ^n,ZZ^1,0) else chkQQZZ(C#1,"lineality space"))
else (
R = chkQQZZ(C,"rays");
n = numRows R;
LS = map(ZZ^n,ZZ^1,0));
-- Adding the rays and lineality spaces to 'R,LS' for each remaining element in 'L', depending on the type of 'C'
L = apply(drop(L,1), C1 -> (
-- Checking for further input errors
if (not instance(C1,Cone)) and (not instance(C1,Polyhedron)) and (not instance(C1,Sequence)) and
(not instance(C1,Matrix)) then
error ("The input must be cones, polyhedra, rays, or (rays,lineality space)");
if instance(C1,Cone) then (
if ambDim C1 != n then error("All Cones and Polyhedra must be in the same ambient space");
(rays C1,linSpace C1))
else if instance(C1,Polyhedron) then (
if ambDim C1 != n then error("All Cones and Polyhedra must be in the same ambient space");
(makePrimitiveMatrix vertices C1 | rays C1,linSpace C1))
else if instance(C1,Sequence) then (
-- Checking for input errors
if not isValidPair C1 then error("(Rays,lineality space) must be given as a sequence of two matrices with the same number of rows");
if numRows C1#0 != n then error("(Rays,lineality space) must be of the correct dimension.");
if C1#1 != 0 then (chkQQZZ(C1#0,"rays"),chkQQZZ(C1#1,"lineality space"))
else (chkQQZZ(C1#0,"rays"),{}))
else (
-- Checking for input errors
if numRows C1 != n then error("Rays must be of the correct dimension.");
(chkQQZZ(C1,"rays"),{}))));
LR := flatten apply(L, l -> l#0);
if LR != {} then R = R | matrix {LR};
L = flatten apply(L, l -> l#1);
if L != {} then LS = LS | matrix {L};
if LS == 0 then posHull R else posHull(R,LS))
-- PURPOSE : Computing a polyhedron as the intersection of affine half-spaces and hyperplanes
intersection = method()
-- INPUT : '(M,v,N,w)', where all four are matrices (although v and w are only vectors), such
-- that the polyhedron is given by P={x | Mx<=v and Nx=w}
-- OUTPUT : 'P', the polyhedron
intersection(Matrix,Matrix,Matrix,Matrix) := (M,v,N,w) -> (
-- checking for input errors
if numColumns M =!= numColumns N then error("equations of half-spaces and hyperplanes must have the same dimension");
if numRows M =!= numRows v or numColumns v =!= 1 then error("invalid condition vector for half-spaces");
if numRows N =!= numRows w or numColumns w =!= 1 then error("invalid condition vector for hyperplanes");
M = -chkZZQQ(v,"condition vector for half-spaces") | chkZZQQ(M,"half-spaces");
N = -chkZZQQ(w,"condition vector for hyperplanes") | chkZZQQ(N,"hyperplanes");
-- Computing generators of the cone and its dual cone
M = transpose M | map(source M,QQ^1,(i,j) -> if i == 0 then -1 else 0);
N = transpose N;
verticesA := fourierMotzkin(M,N);
local hyperA;
(hyperA,verticesA) = fMReplacement(M,verticesA#0,verticesA#1);
-- hyperA := fourierMotzkin verticesA;
polyhedronBuilder(hyperA,verticesA))
-- INPUT : '(M,N)', two matrices where either 'P' is the Cone {x | Mx<=0, Nx=0} if 'M' and 'N' have the same source space
-- or, if 'N' is only a Column vector the Polyhedron {x | Mx<=v}
-- OUTPUT : 'P', the Cone or Polyhedron
intersection(Matrix,Matrix) := (M,N) -> (
-- Checking for input errors
if ((numColumns M =!= numColumns N and numColumns N =!= 1) or (numColumns N == 1 and numRows M =!= numRows N)) and N != 0*N then
error("invalid condition vector for half-spaces");
local genrays;
local dualgens;
M = chkZZQQ(M,"half-spaces");
N = chkZZQQ(N,"condition vector for half-spaces");
-- Decide whether 'M,N' gives the Cone C={p | M*p >= 0, N*p = 0}
if numColumns M == numColumns N and numColumns N != 1 then (
genrays = fourierMotzkin(-transpose M,transpose N);
--dualgens = fourierMotzkin genrays;
local dualgens;
(dualgens,genrays) = fMReplacement(-transpose M,genrays#0,genrays#1);
coneBuilder(genrays, dualgens))
-- or the Cone C={p | M*p >= N=0}
else if numRows N == 0 then (
genrays = fourierMotzkin (-transpose M);
-- dualgens = fourierMotzkin genrays;
local dualgens;
(dualgens,genrays) = fMReplacement(-transpose M,genrays#0,genrays#1);
coneBuilder(genrays,dualgens))
-- or the Polyhedron P={p | M*p >= N != 0}
else ( -- Computing generators of the Polyhedron and its dual cone
M = -N | M;
M = transpose M | map(source M,QQ^1,(i,j) -> if i == 0 then -1 else 0);
verticesA := fourierMotzkin M;
--hyperA := fourierMotzkin verticesA;
local hyperA;
(hyperA,verticesA) = fMReplacement(M,verticesA#0,verticesA#1);
polyhedronBuilder(hyperA,verticesA)))
-- INPUT : '(P1,P2)', two polyhedra
-- OUTPUT : 'P', the polyhedron that is the intersection of both
intersection(Polyhedron,Polyhedron) := (P1,P2) -> (
-- Checking if P1 and P2 lie in the same space
if P1#"ambient dimension" =!= P2#"ambient dimension" then error("Polyhedra must lie in the same ambient space");
-- Combining the Half-spaces and the Hyperplanes
M := (halfspaces P1)#0 || (halfspaces P2)#0;
v := (halfspaces P1)#1 || (halfspaces P2)#1;
N := (hyperplanes P1)#0 || (hyperplanes P2)#0;
w := (hyperplanes P1)#1 || (hyperplanes P2)#1;
intersection(M,v,N,w))
-- INPUT : 'M', a matrix, such that the Cone is given by C={x | Mx>=0}
-- OUTPUT : 'C', the Cone
intersection Matrix := M -> (
-- Checking for input errors
M = chkZZQQ(M,"half-spaces");
-- Computing generators of the cone and its dual cone
genrays := fourierMotzkin (-transpose M);
--dualgens := fourierMotzkin genrays;
local dualgens;
(dualgens,genrays) = fMReplacement(-transpose M,genrays#0,genrays#1);
coneBuilder(genrays,dualgens))
-- INPUT : '(C1,C2)', two Cones
-- OUTPUT : 'C', the Cone that is the intersection of both
intersection(Cone,Cone) := (C1,C2) -> (
-- Checking if C1 and C2 lie in the same space
if C1#"ambient dimension" =!= C2#"ambient dimension" then error("Cones must lie in the same ambient space");
M := halfspaces C1 || halfspaces C2;
N := hyperplanes C1 || hyperplanes C2;
intersection(M,N))
-- INPUT : '(C,P)', a Cone and a Polyhedron
-- OUTPUT : 'Q', the Polyhedron that is the intersection of both
intersection(Cone,Polyhedron) := (C,P) -> intersection {C,P}
-- INPUT : '(P,C)', a Polyhedron and a Cone
-- OUTPUT : 'Q', the Polyhedron that is the intersection of both
intersection(Polyhedron,Cone) := (P,C) -> intersection {P,C}
-- INPUT : 'L', a list of Cones, Polyhedra, inequalities given by (M,v),
-- and hyperplanes given by '{N,w}'
intersection List := L -> (
-- This function checks if the inserted pair is a pair of matrices that gives valid in/equalities
isValidPair := S -> #S == 2 and if S#1 == 0 then instance(S#0,Matrix) else instance(S#1,Matrix) and numRows S#0 == numRows S#1 and numColumns S#1 == 1;
-- Checking for input errors
if L == {} then error("List of cones must not be empty");
C := L#0;
-- The first entry in the list determines the ambient dimension 'n'
n := 0;
local Ml;
local vl;
local Nl;
local wl;
if (not instance(C,Cone)) and (not instance(C,Polyhedron)) and (not instance(C,Sequence)) and (not instance(C,List)) then
error ("The input must be cones, polyhedra, inequalities, equalities.");
-- Adding the inequalities and equalities to 'M,v,N,w', depending on the type of 'C'
if instance(C,Cone) then (
n = C#"ambient dimension";
Ml = -(halfspaces C);
vl = map(target halfspaces C,ZZ^1,0);
Nl = hyperplanes C;
wl = map(target hyperplanes C,ZZ^1,0))
else if instance(C,Polyhedron) then (
n = C#"ambient dimension";
Ml = (halfspaces C)#0;
vl = (halfspaces C)#1;
Nl = (hyperplanes C)#0;
wl = (hyperplanes C)#1)
else if instance(C,Sequence) then (
-- Checking for input errors
if not isValidPair C then error("Inequalities must be given as a sequence of a matrix and a column vector");
--Ml = chkQQZZ(C#0,"half-spaces");
n = numColumns C#0;
Ml = if C#1 == 0 then ((transpose chkQQZZ(transpose C#0,"half-spaces"))|map(ZZ^(numRows C#0),ZZ^1,0)) else transpose chkQQZZ(transpose(C#0|C#1),"halfspaces or condition vector");
vl = Ml_{n};
Ml = submatrix'(Ml,{n});
--vl = if C#1 == 0 then map(target Ml,ZZ^1,0) else chkQQZZ(C#1,"condition vector for half-spaces");
Nl = map(ZZ^1,source Ml,0);
wl = map(ZZ^1,ZZ^1,0))
else (
-- Checking for input errors
if not isValidPair C then error("Equalities must be given as a list of a matrix and a column vector");
--Nl = chkQQZZ(C#0,"hyperplanes");
n = numColumns C#0;
Nl = if C#1 == 0 then ((transpose chkQQZZ(transpose C#0,"hyperplanes"))|map(ZZ^(numRows C#0),ZZ^1,0)) else transpose chkQQZZ(transpose(C#0|C#1),"hyperplanes or condition vector");
wl = Nl_{n};print wl;
Nl = submatrix'(Nl,{n});
Ml = map(ZZ^1,source Nl,0);
vl = map(ZZ^1,ZZ^1,0));
--wl = if C#1 == 0 then map(target Nl,ZZ^1,0) else chkQQZZ(C#1,"condition vector for half-spaces"));
-- Adding the inequalities and equalities to 'M,v,N,w', for each remaining element in 'L', depending on the type of 'C'
L = apply(drop(L,1), C1 -> (
-- Checking for further input errors
if (not instance(C1,Cone)) and (not instance(C1,Polyhedron)) and (not instance(C1,Sequence)) and (not instance(C1,List)) then
error("The input must be cones, polyhedra, inequalities, equalities.");
if instance(C1,Cone) then (
if ambDim C1 != n then error("All Cones and Polyhedra must be in the same ambient space");
(-(halfspaces C1),map(target halfspaces C1,ZZ^1,0),hyperplanes C1,map(target hyperplanes C1,ZZ^1,0)))
else if instance(C1,Polyhedron) then (
if ambDim C1 != n then error("All Cones and Polyhedra must be in the same ambient space");
((halfspaces C1)#0,(halfspaces C1)#1,(hyperplanes C1)#0,(hyperplanes C1)#1))
else if instance(C1,Sequence) then (
-- Checking for input errors
if not isValidPair C1 then error("Inequalities must be given as a sequence of a matrix and a column vector");
if numColumns C1#0 != n then error("Inequalities must be for the same ambient space.");
C1 = if C1#1 == 0 then ((transpose chkQQZZ(transpose C1#0,"half-spaces"))|map(ZZ^(numRows C1#0),ZZ^1,0)) else transpose chkQQZZ(transpose(C1#0|C1#1),"halfspaces or condition vector");
(submatrix'(C1,{n}),C1_{n},map(ZZ^1,ZZ^n,0),map(ZZ^1,ZZ^1,0)))
-- C1 = (chkQQZZ(C1#0,"half-spaces"),chkQQZZ(C1#1,"condition vector for half-spaces"));
-- if C1#1 == 0 then (C1#0,map(target C1#0,ZZ^1,0),map(ZZ^1,source C1#0,0),map(ZZ^1,ZZ^1,0))
-- else (C1#0,C1#1,map(ZZ^1,source C1#0,0),map(ZZ^1,ZZ^1,0)))
else (
-- Checking for input errors
if not isValidPair C1 then error("Equalities must be given as a list of a matrix and a column vector");
if numColumns C1#0 != n then error ("Inequalities must be for the same ambient space.");
C1 = if C1#1 == 0 then ((transpose chkQQZZ(transpose C1#0,"hyperplanes"))|map(ZZ^(numRows C1#0),ZZ^1,0)) else transpose chkQQZZ(transpose(C1#0|C1#1),"hyperplanes or condition vector");
(map(ZZ^1,ZZ^n,0),map(ZZ^1,ZZ^1,0),submatrix'(C1,{n}),C1_{n}))));
-- C1 = (chkQQZZ(C1#0,"hyperplanes"),chkQQZZ(C1#1,"condition vector for hyperplanes"));
-- if C1#1 == 0 then (map(ZZ^1,source C1#0,0),map(ZZ^1,ZZ^1,0),C1#0,map(target C1#0,ZZ^1,0))
-- else (map(ZZ^1,source C1#0,0),map(ZZ^1,ZZ^1,0),C1#0,C1#1))));
LM := flatten apply(L, l -> entries(l#0));
if LM != {} then Ml = Ml || matrix LM;
LM = flatten apply(L, l -> entries(l#1));
if LM != {} then vl = vl || matrix LM;
LM = flatten apply(L, l -> entries(l#2));
if LM != {} then Nl = Nl || matrix LM;
LM = flatten apply(L, l -> entries(l#3));
if LM != {} then wl = wl || matrix LM;
if vl == 0*vl and wl == 0*wl then intersection(-Ml,Nl) else intersection(Ml,vl,Nl,wl));
-- PURPOSE : Building the Fan 'F'
-- INPUT : 'L', a list of cones and fans in the same ambient space
-- OUTPUT : The fan of all Cones in 'L' and all Cones in of the fans in 'L' and all their faces
fan = method(TypicalValue => Fan)
fan List := L -> (
-- Checking for input errors
if L == {} then error("List of cones and fans must not be empty");
if (not instance(L#0,Cone)) and (not instance(L#0,Fan)) then error("Input must be a list of cones and fans");
-- Starting with the first Cone in the list and extracting its information
C := L#0;
L = drop(L,1);
ad := C#"ambient dimension";
local F;
if instance(C,Fan) then F = C
else (
rayList := rays C;
-- Collecting the rays
rayList = apply(numColumns rayList, i-> rayList_{i});
-- Generating the new fan
F = new Fan from {
"generatingCones" => set {C},
"ambient dimension" => ad,
"top dimension of the cones" => C#"dimension of the cone",
"number of generating cones" => 1,
"rays" => set rayList,
"number of rays" => #rayList,
"isPure" => true,
symbol cache => new CacheTable});
-- Checking the remaining list for input errors and reducing fans in the list
-- to their list of generating cones
L = flatten apply(L, C -> if instance(C,Cone) then C else if instance(C,Fan) then toList(C#"generatingCones") else
error ("Input must be a list of cones and fans"));
-- Adding the remaining cones of the list with 'addCone'
scan(L, C -> F = addCone(C,F));
F);
-- INPUT : 'C', a Cone
-- OUTPUT : The Fan given by 'C' and all of its faces
fan Cone := C -> fan {C};
-- PURPOSE : Building the PolyhedralComplex 'PC'
-- INPUT : 'L', a list of polyhedra in the same ambient space
-- OUTPUT : The polyhedral complex of all Polyhedra in 'L' and all their faces
polyhedralComplex = method(TypicalValue => PolyhedralComplex)
polyhedralComplex List := L -> (
-- Checking for input errors
if L == {} then error("List of polyhedra must not be empty");
if (not instance(L#0,Polyhedron)) and (not instance(L#0,PolyhedralComplex)) then error("Input must be a list of polyhedra and polyhedral complexes");
-- Starting with the first Polyhedron in the list and extracting its information
P := L#0;
L = drop(L,1);
ad := P#"ambient dimension";
local PC;
if instance(P,PolyhedralComplex) then PC = P
else (
verticesList := vertices P;
-- Collecting the vertices
verticesList = apply(numColumns verticesList, i-> verticesList_{i});
-- Generating the new fan
PC = new PolyhedralComplex from {
"generatingPolyhedra" => set {P},
"ambient dimension" => ad,
"top dimension of the polyhedra" => P#"dimension of polyhedron",
"number of generating polyhedra" => 1,
"vertices" => set verticesList,
"number of vertices" => #verticesList,
"isPure" => true,
symbol cache => new CacheTable});
-- Checking the remaining list for input errors and reducing polyhedral complexes in the list
-- to their list of generating polyhedra
L = flatten apply(L, e -> if instance(e,Polyhedron) then e else if instance(e,PolyhedralComplex) then toList(e#"generatingPolyhedra") else
error ("Input must be a list of polyhedra and polyhedral complexes"));
-- Adding the remaining polyhedra of the list with 'addPolyhedron'
scan(L, e -> PC = addPolyhedron(e,PC));
PC);
polyhedralComplex Polyhedron := P -> polyhedralComplex {P}
addPolyhedron = method(TypicalValue => PolyhedralComplex)
addPolyhedron (Polyhedron,PolyhedralComplex) := (P,PC) -> (
-- Checking for input errors
if P#"ambient dimension" != PC#"ambient dimension" then error("The polyhedra must lie in the same ambient space.");
-- Extracting data
GP := toList PC#"generatingPolyhedra";
d := P#"dimension of polyhedron";
inserted := false;
-- Polyhedra in the list 'GP' are ordered by decreasing dimension so we start compatibility checks with
-- the cones of higher or equal dimension. For this we divide GP into two seperate lists
GP = partition(Pf -> (dim Pf) >= d,GP);
GP = {if GP#?true then GP#true else {},if GP#?false then GP#false else {}};
if all(GP#0, Pf -> (
(a,b) := areCompatible(Pf,P);
-- if 'Pf' and 'P' are not compatible then there is an error
if not a then error("The polyhedra are not compatible");
-- if they are compatible and 'P' is a face of 'Pf' then 'C' does not
-- need to be added to 'GP'
b != P)) then (
-- otherwise 'Pf' is still a generating Polyhedron and has to be kept and the remaining polyhedra
-- have to be checked
GP = GP#0 | {P} | select(GP#1, Pf -> (
(a,b) := areCompatible(Pf,P);
if not a then error("The polyhedra are not compatible");
-- if one of the remaining polyhedra is a face of 'P' this Polyhedron can be dropped
b != Pf));
inserted = true)
-- Otherwise 'P' was already a face of one of the original polyhedra and does not need to be added
else GP = flatten GP;
-- If 'P' was added to the Polyhedron as a generating polyhedron then the codim 1 faces on the boundary have to changed to check for
-- completeness
verticesList := toList PC#"vertices";
if inserted then (
-- The vertices of 'P' have to be added
Vm := vertices P;
Vm = apply(numColumns Vm, i -> Vm_{i});
verticesList = unique(verticesList|Vm));
-- Saving the polyhedral complex
new PolyhedralComplex from {
"generatingPolyhedra" => set GP,
"ambient dimension" => P#"ambient dimension",
"top dimension of the polyhedra" => (GP#0)#"dimension of polyhedron",
"number of generating polyhedra" => #GP,
"vertices" => set verticesList,
"number of vertices" => #verticesList,
"isPure" => dim first GP == dim last GP,
symbol cache => new CacheTable})
-- INPUT : '(L,PC)', where 'L' is a list of Polyhedra in the same ambient space as the PolyhedralComplex 'PC'
-- OUTPUT : The original PolyhedralComplex 'PC' together with polyhedra in the list 'L'
addPolyhedron (List,PolyhedralComplex) := (L,PC) -> (
-- Checking for input errors
if L == {} then error("The list must not be empty");
if (not instance(L#0,Polyhedron)) and (not instance(L#0,PolyhedralComplex)) then error("The list may only contain polyhedra and polyhedral complexes");
if #L == 1 then addPolyhedron(L#0,PC) else addPolyhedron(drop(L,1),addPolyhedron(L#0,PC)))
-- INPUT : '(PC1,PC2)', where 'PC1' is a PolyhedralComplex in the same ambient space as the PolyhedralComplex 'PC2'
-- OUTPUT : The original fan 'PC2' together with cones of the fan 'PC1'
addPolyhedron (PolyhedralComplex,PolyhedralComplex) := (PC1,PC2) -> (
-- Checking for input errors
if ambDim PC2 != ambDim PC1 then error("The polyhedral complexes must be in the same ambient space");
L := toList PC1#"generatingCones";
addCone(L,PC2))
-- PURPOSE : Adding a Cone to an existing fan
-- INPUT : '(C,F)', where 'C' is a Cone in the same ambient space as 'F'
-- OUTPUT : The original fan 'F' together with 'C' if it is compatible with the already existing cones,
-- if not there is an error
addCone = method(TypicalValue => Fan)
addCone (Cone,Fan) := (C,F) -> (
-- Checking for input errors
if C#"ambient dimension" != F#"ambient dimension" then error("Cones must lie in the same ambient space");
-- Extracting data
GC := toList F#"generatingCones";
d := C#"dimension of the cone";
-- We need to memorize for later if 'C' has been inserted
inserted := false;
-- Cones in the list 'GC' are ordered by decreasing dimension so we start compatibility checks with
-- the cones of higher or equal dimension. For this we divide GC into two seperate lists
GC = partition(Cf -> (dim Cf) >= d,GC);
GC = {if GC#?true then GC#true else {},if GC#?false then GC#false else {}};
if all(GC#0, Cf -> (
(a,b) := areCompatible(Cf,C);
-- if 'Cf' and 'C' are not compatible then there is an error
if not a then error("The cones are not compatible");
-- if they are compatible and 'C' is a face of 'Cf' then 'C' does not
-- need to be added to 'F'
b != C)) then (
-- otherwise 'Cf' is still a generating Cone and has to be kept and the remaining cones
-- have to be checked
GC = GC#0 | {C} | select(GC#1, Cf -> (
(a,b) := areCompatible(Cf,C);
if not a then error("The cones are not compatible");
-- if one of the remaining cones is a face of 'C' this Cone can be dropped
b != Cf));
inserted = true)
-- Otherwise 'C' was already a face of one of the original cones and does not need to be added
else GC = flatten GC;
-- If 'C' was added to the Fan as a generating cone then the codim 1 faces on the boundary have to changed to check for
-- completeness
rayList := toList F#"rays";
if inserted then (
-- The rays of 'C' have to be added
rm := rays C;
rm = apply(numColumns rm, i -> rm_{i});
rayList = unique(rayList|rm));
-- Saving the fan
new Fan from {
"generatingCones" => set GC,
"ambient dimension" => F#"ambient dimension",
"top dimension of the cones" => dim GC#0,
"number of generating cones" => #GC,
"rays" => set rayList,
"number of rays" => #rayList,
"isPure" => dim first GC == dim last GC,
symbol cache => new CacheTable})
-- INPUT : '(L,F)', where 'L' is a list of Cones in the same ambient space as the fan 'F'
-- OUTPUT : The original fan 'F' together with cones in the list 'L'
addCone (List,Fan) := (L,F) -> (
-- Checking for input errors
if L == {} then error("The list must not be empty");
if (not instance(L#0,Cone)) and (not instance(L#0,Fan)) then error("The list may only contain cones and fans");
if #L == 1 then addCone(L#0,F) else addCone(drop(L,1),addCone(L#0,F)))
-- INPUT : '(F1,F)', where 'F1' is a fan in the same ambient space as the fan 'F'
-- OUTPUT : The original fan 'F' together with cones of the fan 'F1'
addCone (Fan,Fan) := (F1,F) -> (
-- Checking for input errors
if ambDim F != ambDim F1 then error("The fans must be in the same ambient space");
L := toList F1#"generatingCones";
addCone(L,F))
Cone ? Cone := (C1,C2) -> (
if C1 == C2 then symbol == else (
if ambDim C1 != ambDim C2 then ambDim C1 ? ambDim C2 else (
if dim C1 != dim C2 then dim C1 ? dim C2 else (
R1 := rays C1;
R2 := rays C2;
if R1 != R2 then (
R1 = apply(numColumns R1, i -> R1_{i});
R2 = apply(numColumns R2, i -> R2_{i});
(a,b) := (set R1,set R2);
r := (sort matrix {join(select(R1,i->not b#?i),select(R2,i->not a#?i))})_{0};
if a#?r then symbol > else symbol <)
else (
R1 = linSpace C1;
R2 = linSpace C2;
R1 = apply(numColumns R1, i -> R1_{i});
R2 = apply(numColumns R2, i -> R2_{i});
(c,d) := (set R1,set R2);
l := (sort matrix {join(select(R1,i->not d#?i),select(R2,i->not c#?i))})_{0};
if c#?l then symbol > else symbol <)))))
-- PURPOSE : Giving the defining affine hyperplanes
ambDim = method(TypicalValue => ZZ)
-- INPUT : 'P' a Polyhedron
-- OUTPUT : an integer, the dimension of the ambient space
ambDim PolyhedralObject := X -> X#"ambient dimension"
-- INPUT : 'C' a Cone
-- OUTPUT : an integer, the dimension of the ambient space
--ambDim Cone := C -> C#"ambient dimension"
-- INPUT : 'F' a Fan
-- OUTPUT : an integer, the dimension of the ambient space
--ambDim Fan := F -> F#"ambient dimension"
-- PURPOSE : Giving the k dimensionial Cones of the Fan
-- INPUT : (k,F) where 'k' is a positive integer and F is a Fan
-- OUTPUT : a List of Cones
cones = method(TypicalValue => List)
cones(ZZ,Fan) := (k,F) -> (
-- Checking for input errors
if k < 0 or dim F < k then error("k must be between 0 and the dimension of the fan.");
L := select(toList F#"generatingCones", C -> dim C >= k);
-- Collecting the 'k'-dim faces of all generating cones of dimension greater than 'k'
unique flatten apply(L, C -> faces(dim(C)-k,C)))
-- PURPOSE : Giving the k dimensionial Polyhedra of the Polyhedral Complex
-- INPUT : (k,PC) where 'k' is a positive integer and PC is a PolyhedralComplex
-- OUTPUT : a List of Polyhedra
polyhedra = method(TypicalValue => List)
polyhedra(ZZ,PolyhedralComplex) := (k,PC) -> (
-- Checking for input errors
if k < 0 or dim PC < k then error("k must be between 0 and the dimension of the fan.");
L := select(toList PC#"generatingPolyhedra", P -> dim P >= k);
-- Collecting the 'k'-dim faces of all generating polyhedra of dimension greater than 'k'
unique flatten apply(L, P -> faces(dim(P)-k,P)))
-- INPUT : 'P' a Polyhedron
-- OUTPUT : an integer, the dimension of the polyhedron
dim Polyhedron := P -> P#"dimension of polyhedron"
-- INPUT : 'C' a Cone
-- OUTPUT : an integer, the dimension of the Cone
dim Cone := C -> C#"dimension of the cone"
-- INPUT : 'F' a Fan
-- OUTPUT : an integer, the highest dimension of Cones in 'F'
dim Fan := F -> F#"top dimension of the cones"
-- INPUT : 'PC' a PolyhedralComplex
-- OUTPUT : an integer, the highest dimension of polyhedra in 'PC'
dim PolyhedralComplex := PC -> PC#"top dimension of the polyhedra"
-- PURPOSE : Giving the generating Cones of the Fan
-- INPUT : 'F' a Fan
-- OUTPUT : a List of Cones
maxCones = method(TypicalValue => List)
maxCones Fan := F -> toList F#"generatingCones"
-- PURPOSE : Giving the generating Polyhedra of the PolyhedralComplex
-- INPUT : 'PC' a PolyhedralComplex
-- OUTPUT : a List of Cones
maxPolyhedra = method(TypicalValue => List)
maxPolyhedra PolyhedralComplex := PC -> toList PC#"generatingPolyhedra"
-- PURPOSE : Giving the defining affine half-spaces
-- INPUT : 'P' a Polyhedron
-- OUTPUT : '(M,v)', where M and v are matrices and P={x in H | Mx<=v}, where
-- H is the intersection of the defining affine hyperplanes
halfspaces = method()
halfspaces Polyhedron := P -> P#"halfspaces"
-- INPUT : 'C' a Cone
-- OUTPUT : 'M', where M is a matrix and C={x in H | Mx>=0}, where
-- H is the intersection of the defining hyperplanes
halfspaces Cone := C -> C#"halfspaces"
-- PURPOSE : Giving the defining affine hyperplanes
-- INPUT : 'P' a Polyhedron
-- OUTPUT : '(N,w)', where M and v are matrices and P={x in HS | Nx=w}, where
-- HS is the intersection of the defining affine half-spaces