-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPolyhedra2.m2
1311 lines (1124 loc) · 52.7 KB
/
Polyhedra2.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 : Nathan Ilten, Josephine Yu, Qingchun Ren
-- UPDATE HISTORY : August 2012
---------------------------------------------------------------------------
newPackage("Polyhedra2",
Headline => "A package for computations with convex polyhedra",
Version => ".1",
Date => "August 5, 2011",
Authors => {
{Name => "Nathan Ilten",
HomePage => "http://math.berkeley.edu/~nilten",
Email => "[email protected]"},
{Name => "Qingchun Ren"},
{Name => "Josephine Yu"}
},
Configuration => {"DefaultUsePolymake"=>false},
DebuggingMode => true
)
---------------------------------------------------------------------------
-- COPYRIGHT NOTICE:
--
-- Copyright 2012 Nathan Ilten, Josephine Yu, and Qingchun Ren
-- Some parts copyright 2010 Rene 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/>.
--
---------------------------------------------------------------------------
DefaultUsePolymake := (options Polyhedra2).Configuration#"DefaultUsePolymake"
pmopt:={UsePolymake=>DefaultUsePolymake}
export { "isFace",
"faces",
"mixedVolume",
"toSublattice",
"sublatticeBasis",
"volume",
"triangulate",
"ehrhart",
"newtonPolytope",
"crossPolytope",
"cyclicPolytope",
"hypercube",
"posOrthant",
"bipyramid",
"pyramid",
"stdSimplex",
"tailCone",
"latticePoints",
"isCompact",
"affinePreimage",
"affineImage",
"hilbertBasis",
"coneToPolyhedron",
"directProduct",
"UsePolymake",
"PolymakePath",
"emptyPolyhedron",
"isEmpty",
"minkowskiSum",
"dualCone",
"affineHull",
"intersection",
"linSpace",
"vertices",
"rays",
"ambDim",
"hyperplanes",
"halfspaces",
"contains",
"convexHull",
"posHull"}
needsPackage "FourierMotzkin"
needsPackage "PolyhedralObjects"
needsPackage("PolymakeInterface")
Cone == Cone := (C1,C2)->(
contains(C1,C2) and contains(C2,C1)
)
Polyhedron == Polyhedron := (C1,C2)->(
contains(C1,C2) and contains(C2,C1)
)
convexHull = method()
convexHull (Matrix,Matrix,Matrix):=(M,N,L)->(
new Polyhedron from hashTable {
"Points"=>promote(homCoordinates(transpose M,transpose N),QQ),
"InputLineality"=>promote(homRays(transpose L),QQ)}
)
convexHull (Matrix,Matrix):=(M,N)->(convexHull(M,N,map(QQ^(numRows M),QQ^0,0)))
convexHull Matrix :=M->(convexHull(M,map(QQ^(numRows M),QQ^0,0)))
convexHull (Polyhedron,Polyhedron):=(P1,P2)->convexHull {P1,P2}
convexHull (Cone,Cone):=(C1,C2)->posHull {C1,C2}
convexHull List := L->(
datalist:=apply(L,P->(
if instance(P,Polyhedron) then (
if not P#?"Points" and not P#?"Vertices" then (rays P; linSpace P);
if P#?"Vertices" and P#?"LinealitySpace" then return(P#"Vertices",P#"LinealitySpace");
if not P#?"InputLineality" then P#"InputLineality"=map(QQ^0,numColumns P#"Points",0);
return (P#"Points",P#"InputLineality")
)
else if instance(P,Cone) then (
if not P#?"InputRays" and not P#?"Rays" then (rays P; linSpace P);
if P#?"Rays" then return(homRays P#"Rays",homRays P#"LinealitySpace");
if not P#?"InputLineality" then P#"InputLineality"=map(QQ^0,numColumns P#"InputRays",0);
return (homRays P#"InputRays",homRays P#"InputLineality")
)
else if instance(P,Matrix) then (
return (homPoints transpose promote(P,QQ),transpose map(QQ^(1+numRows P),QQ^0,0))
)
else return (promote(homCoordinates(transpose P#0,transpose P#1),QQ),
transpose map(QQ^(1+numRows P),QQ^0,0))));
vlist:=matrix apply(datalist,i->{i#0});
llist:=matrix apply(datalist,i->{i#1});
new Polyhedron from hashTable {
"Points"=>vlist,
"InputLineality"=>llist})
posHull = method()
posHull (Matrix, Matrix):= (M,N)-> (
new Cone from hashTable {
"InputLineality"=>promote(transpose N,QQ),
"InputRays"=>promote(transpose M,QQ)}
)
posHull Matrix:=M ->(posHull(M,map(QQ^(numRows M),QQ^0,0)))
posHull (Cone,Cone):=(C1,C2)->(posHull {C1,C2})
posHull Polyhedron:=C1->(posHull {C1})
posHull List:=L->(
datalist:=apply(L,P->(
if instance(P,Polyhedron) then (
if not P#?"Vertices" and not P#?"Points" then (rays P,linSpace P);
if P#?"Vertices" then return(dehom P#"Vertices",dehom P#"LinealitySpace");
if not P#?"InputLineality" then P#"InputLineality"=map(QQ^0,numColumns P#"Points",0);
return(dehom P#"Points",dehom P#"InputLineality")
)
else if instance(P,Cone) then (
if not P#?"Rays" and not P#?"InputRays" then (rays P; linSpace P);
if P#?"Rays" then return(P#"Rays",P#"LinealitySpace");
if not P#?"InputLineality" then P#"InputLineality"=map(QQ^0,numColumns P#"InputRays",0);
return(P#"InputRays",P#"InputLineality")
)
else if instance(P,Matrix) then (
return(promote(transpose P,QQ),map(QQ^0,numRows P,0))
)
else (
return(transpose P#0,transpose P#1)
)
));
rlist:= matrix apply(datalist,i->{i#0});
llist:= matrix apply(datalist,i->{i#1});
new Cone from hashTable {
"InputLineality"=>llist,
"InputRays"=>rlist}
)
intersection = method()
intersection (Matrix,Matrix):=(M,N)->(
if not numColumns N ==1 then return new Cone from hashTable {
"Equations"=>promote(- N,QQ),
"Inequalities"=>promote(- M,QQ)};
intersection(M,N,map(QQ^0,QQ^(1+numColumns M),0),map(QQ^0,QQ^0,0))
)
intersection Matrix:=M->(intersection(M,map(QQ^0,QQ^(numColumns M),0)))
intersection (Matrix,Matrix,Matrix,Matrix):=(M,v,N,w)->(
new Polyhedron from hashTable {
"Inequalities"=>promote(v|(-M),QQ),
"Equations"=>promote(w|(-N),QQ)
}
)
intersection (Cone,Cone):=(P1,P2)->intersection {P1,P2}
intersection (Polyhedron,Polyhedron):=(P1,P2)->intersection {P1,P2}
intersection List := L -> (
datalist:=apply(L,P->(
if instance(P,Polyhedron) then (
if not P#?"Facets" and not P#?"Inequalities" then (hyperplanes P;halfspaces P);
if P#?"Facets" then return(P#"Facets",P#"AffineHull");
if not P#?"Equations" then P#"Equations"=map(QQ^0,numColumns P#"Inequalities",0);
return(P#"Inequalities",P#"Equations")
)
else if instance(P,Cone) then (
if not P#?"Facets" and not P#?"Inequalities" then (hyperplanes P;halfspaces P);
if P#?"Facets" then return(homRays P#"Facets",homRays P#"LinearSpan");
if not P#?"Equations" then P#"Equations"=map(QQ^0,numColumns P#"Inequalities",0);
return(homRays P#"Inequalities",homRays P#"Equations")
)
else if instance(P,Sequence) then (
return(promote(P#1|(-P#0),QQ),map(QQ^(numRows P#0),QQ^(1+numColumns P#0),0))
)
else (
return(map(QQ^(numRows P#0),QQ^(1+numColumns P#0),0),promote(P#1|(-P#0),QQ))
)
));
ilist:=matrix apply(datalist,i->{i#0});
elist:=matrix apply(datalist,i->{i#1});
new Polyhedron from hashTable{
"Inequalities"=>ilist,
"Equations"=>elist}
)
hyperplanes = method(Options=>pmopt)
hyperplanes Cone := opts->P -> (
if P#?"LinearSpan" then return P#"LinearSpan";
if opts#UsePolymake then runPolymake(P,"LinearSpan")
else computeFacets P;
P#"LinearSpan")
hyperplanes Polyhedron := opts->P -> (
if not P#?"AffineHull" then
if opts#UsePolymake then runPolymake(P,"AffineHull")
else computeFacets P;
M:=P#"AffineHull";
(-M_(toList(1..numColumns M-1)),M_{0})
)
halfspaces = method(Options=>pmopt)
halfspaces Cone := opts->P -> (
if P#?"Facets" then return -P#"Facets";
if opts#UsePolymake then runPolymake(P,"Facets")
else computeFacets P;
-P#"Facets")
halfspaces Polyhedron := opts->P -> (
if not P#?"Facets" then
if opts#UsePolymake then runPolymake(P,"Facets")
else computeFacets P;
computeFacets P;
M:=P#"Facets";
(-M_(toList(1..numColumns M-1)),M_{0})
)
rays = method(Options=>pmopt)
rays Cone := opts->P -> (
if P#?"Rays" then return transpose P#"Rays";
if opts#UsePolymake then runPolymake(P,"Rays")
else computeRays P;
transpose P#"Rays")
rays Polyhedron := opts->P -> (
if not P#?"Vertices" then
if opts#UsePolymake then runPolymake(P,"Vertices")
else computeRays P;
transpose (dehomCoordinates P#"Vertices")_1)
vertices = method(Options=>pmopt)
vertices Polyhedron := opts->P -> (
if not P#?"Vertices" then
if opts#UsePolymake then runPolymake(P,"Vertices")
else computeRays P;
transpose (dehomCoordinates P#"Vertices")_0)
linSpace = method(Options=>pmopt)
linSpace Cone := opts->P -> (
if P#?"LinealitySpace" then return transpose P#"LinealitySpace";
if opts#UsePolymake then runPolymake(P,"LinealitySpace")
else computeRays P;
transpose P#"LinealitySpace")
linSpace Polyhedron := opts->P -> (
if P#?"LinealitySpace" then return transpose dehom P#"LinealitySpace";
if opts#UsePolymake then runPolymake(P,"LinealitySpace")
else computeRays P;
transpose dehom P#"LinealitySpace")
ambDim = method (Options=>pmopt)
ambDim Cone:=opts->C->(
if not C#?"ConeAmbientDim" then (
if opts#UsePolymake then runPolymake(C,"ConeAmbientDim")
else
(
if C#?"Rays" then C#"ConeAmbientDim"=numColumns C#"Rays"
else if C#?"InputRays" then C#"ConeAmbientDim"=numColumns C#"InputRays"
else if C#?"Inequalities" then C#"ConeAmbientDim"=numColumns C#"Inequalities"
else if C#?"Facets" then C#"ConeAmbientDim"=numColumns C#"Facets"
else error ("Your cone is ill-defined")));
C#"ConeAmbientDim")
ambDim Polyhedron:=opts->C->(
if not C#?"ConeAmbientDim" then (
if opts#UsePolymake then runPolymake(C,"ConeAmbientDim")
else
(
if C#?"Vertices" then C#"ConeAmbientDim"=numColumns C#"Vertices"
else if C#?"Points" then C#"ConeAmbientDim"=numColumns C#"Points"
else if C#?"Inequalities" then C#"ConeAmbientDim"=numColumns C#"Inequalities"
else if C#"Facets" then C#"ConeAmbientDim"=numColumns C#"Facets"
else error ("Your cone is ill-defined")));
C#"ConeAmbientDim"-1)
dim Cone:=C->(if not C#?"ConeDim" then C#"ConeDim"=ambDim C-numRows ((hyperplanes C));C#"ConeDim")
dim Polyhedron:=C->(if not C#?"ConeDim" then C#"ConeDim"=ambDim C-numRows ((hyperplanes C)_0);C#"ConeDim")
affineHull = method ()
affineHull Polyhedron := P->(hp:=hyperplanes P;
intersection(map(QQ^0,QQ^(1+ambDim P),0),map(QQ^0,QQ^0,0),hp#0,hp#1))
dualCone = method ()
dualCone Cone:=C->(
C2:=new Cone from hashTable {};
if C#?"InputRays" then C2#"Inequalities"=C#"InputRays";
if C#?"InputLineality" then C2#"Equations"=C#"InputLineality";
if C#?"Rays" then C2#"Facets"=C#"Rays";
if C#?"LinealitySpace" then C2#"LinearSpan"=C#"LinealitySpace";
if C#?"Facets" then C2#"Vertices"=C#"Facets";
if C#?"LinearSpan" then C2#"LinealitySpace"=C#"LinearSpan";
if C#?"Inequalities" then C2#"InputRays"=C#"Inequalities";
if C#?"Equations" then C2#"InputLineality"=C#"Equations";
C2)
affineImage = method ()
affineImage (Matrix,Polyhedron,Matrix) := (A,P,v)->(
if not P#?"Points" and not P#?"Vertices" then (vertices P,linSpace P);
Q:=new Polyhedron from hashTable {};
M:=(transpose (map(QQ^1,1+numColumns A,(i,j)->if j==0 then 1 else 0)||(v|A)));
if P#?"Vertices" then (Q#"Points"=P#"Vertices"*M;
Q#"InputLineality"=P#"LinealitySpace"*M)
else (Q#"Points"=P#"Points"*M;
if not P#?"InputLineality" then P#"InputLineality"=map(QQ^0,numColumns P#"Points",0);
Q#"InputLineality"=P#"InputLineality"*M);
Q)
affineImage (Matrix,Polyhedron) := (A,P)->(affineImage(A,P,map(QQ^(numRows A),QQ^1,0)))
affineImage (Polyhedron,Matrix) := (P,v)->(affineImage(id_(QQ^(ambDim P)),P,v))
affineImage (Matrix,Cone):=(A,P)->(
if not P#?"InputRays" and not P#?"Rays" then (rays P,linSpace P);
Q:=new Cone from hashTable {};
if P#?"Rays" then (Q#"InputRays"=P#"Rays"*(transpose (A));
Q#"InputLineality"=P#"LinealitySpace"*(transpose (A)))
else (Q#"InputRays"=P#"InputRays"*(transpose (A));
if not P#?"InputLineality" then P#"InputLineality"=map(QQ^0,numColumns P#"InputRays",0);
Q#"InputLineality"=P#"InputLineality"*(transpose (A)));
Q)
affineImage (Matrix,Cone,Matrix) := (A,P,v)->(
if v==0 then return affineImage(A,P);
affineImage(A,coneToPolyhedron P,v))
affineImage (Cone,Matrix) :=(P,v)->(
if v==0 then return P;
affineImage(coneToPolyhedron,v))
affinePreimage = method ()
affinePreimage(Matrix,Polyhedron,Matrix) := (A,P,b) -> (
--note: could set up to use eq/ineq if facets don't exist
-- Checking for input errors
if ambDim P =!= numRows A then error("Matrix source must be ambient space");
if numRows A =!= numRows b then error("Vector must lie in target space of matrix");
if numColumns b =!= 1 then error("Second argument must be a vector");
-- Constructing the new half-spaces and hyperplanes
(M,v) := halfspaces P;
(N,w) := hyperplanes P;
v = v - (M * b);
w = w - (N * b);
M = M * A;
N = N * A;
intersection(M,v,N,w))
affinePreimage(Matrix,Polyhedron) := (A,P) -> (
affinePreimage(A,P,map(target A,QQ^1,0)))
affinePreimage(Polyhedron,Matrix) := (P,b) -> affinePreimage(map(QQ^(ambDim P),QQ^(ambDim P),1),P,b)
affinePreimage(Matrix,Cone,Matrix) := (A,C,b) -> if b == 0 then affinePreimage(A,C) else affinePreimage(A,coneToPolyhedron C,b)
affinePreimage(Matrix,Cone) := (A,C) -> posHull affinePreimage(A,coneToPolyhedron C)
affinePreimage(Cone,Matrix) := (C,b) -> affinePreimage(coneToPolyhedron C,b)
latticePoints = method(TypicalValue => List,Options=>pmopt)
latticePoints Polyhedron := opts -> P -> (
if not P#?"LatticePoints" then (
if opts#UsePolymake then runPolymake(P,"LatticePoints")
else (
-- Checking for input errors
if not isCompact P then error("The polyhedron must be compact");
-- Recursive function that intersects the polyhedron with parallel hyperplanes in the axis direction
-- in which P has its minimum extension
latticePointsRec := P -> (
-- Finding the direction with minimum extension of P
V := entries vertices P;
n := ambDim P;
minv := apply(V,min);
maxv := apply(V,max);
minmaxv := maxv-minv;
pos := min minmaxv;
pos = position(minmaxv,v -> v == pos);
-- Determining the lattice heights in this direction
L := toList({{ceiling minv_pos}}..{{floor maxv_pos}});
-- If the dimension is one, then it is just a line and we take the lattice points
if n == 1 then apply(L,matrix)
-- Otherwise intersect with the hyperplanes and project into the hyperplane
else flatten apply(L,p -> (
NP := intersection {P,{map(QQ^1,QQ^n,(i,j) -> if j == pos then 1 else 0),matrix p}};
if numColumns vertices P == 1 then (
v := vertices NP;
if promote(substitute(v,ZZ),QQ) == v then substitute(v,ZZ) else {})
else (
A := matrix drop((entries id_(QQ^n)),{pos,pos});
apply(latticePointsRec affineImage(A,NP),v -> v^{0..(pos-1)} || matrix p || v^{pos..(n-2)})))));
-- Checking if the polytope is just a point
if dim P == 0 then P#"LatticePoints" = if liftable(vertices P,ZZ) then homPoints transpose lift(vertices P,ZZ) else map(ZZ^0,ZZ^(ambDim P,0))
-- Checking if the polytope is full dimensional
else if (dim P == ambDim P) then P#"LatticePoints" = homPoints transpose matrix {latticePointsRec P}
-- If not checking first if the affine hull of P contains lattice points at all and if so projecting the polytope down
-- so that it becomes full dimensional with a map that keeps the lattice
else (
(M,v) := hyperplanes P;
-- Finding a lattice point in the affine hull of P
b := if all(entries M, e -> gcd e == 1) then (
-- Computing the Smith Normal Form to solve the equation over ZZ
(M1,Lmatrix,Rmatrix) := smithNormalForm substitute(M,ZZ);
v1 := flatten entries (Lmatrix*v);
w := apply(numRows M1, i -> M1_(i,i));
-- Checking if the system is at least solvable over QQ
if all(#w, i -> w#i != 0 or v1#i == 0) then (
-- If it is, then solve over QQ
w = apply(#w, i -> (v1#i/w#i,v1#i%w#i));
if all(w, e -> e#1 == 0) then (
-- If the solution is in fact in ZZ then return it
w = transpose matrix{apply(w,first) | toList(numColumns M1 - numRows M1:0)};
Rmatrix * w)));
-- If there is no lattice point in the affine hull then P has none
if b === null then P#"LatticePoints" = homPoints map(ZZ^0,ZZ^(ambDim P),0)
else (
A := gens ker substitute(M,ZZ);
-- Project the translated polytope, compute the lattice points and map them back
P#"LatticePoints" = homPoints transpose matrix {apply(latticePoints affinePreimage(A,P,b),e -> substitute(A*e + b,ZZ))}))));
apply(numRows dehom P#"LatticePoints",i->(transpose dehom P#"LatticePoints")_{i})
)
emptyPolyhedron=method ()
emptyPolyhedron ZZ:=n->(
if n < 1 then error("The ambient dimension must be positive");
C:=convexHull map(QQ^n,QQ^0,0);
C#"ConeAmbientDim"=n;
C#"ConeDim"=-1;
C)
isEmpty=method()
isEmpty Polyhedron:=P->(dim P==-1)
isCompact = method(TypicalValue => Boolean)
isCompact Polyhedron := P -> (linSpace P == 0 and rays P == 0)
-- PURPOSE : Tests if the first Polyhedron/Cone is a face of the second Polyhedron/Cone
isFace = method(TypicalValue => Boolean)
-- INPUT : '(P,Q)' two Polyhedra
-- OUTPUT : 'true' or 'false'
isFace(Polyhedron,Polyhedron) := (P,Q) -> (
-- Checking if the two polyhedra lie in the same space and computing the dimension difference
c := dim Q - dim P;
if ambDim P == ambDim Q and c >= 0 then (
-- Checking if P is the empty polyhedron
if c > dim Q then true
-- Checking if one of the codim 'c' faces of Q is P
else any(faces(c,Q), f -> f === P))
else false)
-- INPUT : '(C1,C2)' two Cones
-- OUTPUT : 'true' or 'false'
isFace(Cone,Cone) := (C1,C2) -> (
c := dim C2 - dim C1;
-- Checking if the two cones lie in the same space and the dimension difference is positive
if ambDim C1 == ambDim C2 and c >= 0 then (
-- Checking if one of the codim 'c' faces of C2 is C1
any(faces(c,C2), f -> f === C1))
else false)
minkowskiSum = method()
minkowskiSum(Polyhedron,Polyhedron) := (P1,P2) -> (
if (ambDim P1) =!= (ambDim P2) then error("Polyhedral objects must lie in the same space");
if isEmpty P1 or isEmpty P2 then emptyPolyhedron ambDim P1 else if P1 == P2 then 2 * P1;
V1 := vertices P1;
V2 := vertices P2;
R := promote(rays P1 | rays P2,QQ) | map(target V1,QQ^1,0);
Vnew := map(target V1,QQ^0,0);
-- Collecting all sums of vertices of P1 with vertices of P2
Vnew = matrix {unique flatten apply(numColumns V1, i -> apply(numColumns V2, j -> V1_{i}+V2_{j}))};
convexHull(Vnew,R))
minkowskiSum(Cone,Cone) := (C1,C2) -> (
-- Checking for input errors
if (ambDim C1) =!= (ambDim C2) then error("Cones must lie in the same space");
posHull((rays C1)|(rays C2),(linSpace C1)|linSpace C2))
minkowskiSum(Cone,Polyhedron) := (C,P) -> minkowskiSum(coneToPolyhedron C,P)
minkowskiSum(Polyhedron,Cone) := (P,C) -> minkowskiSum(P,coneToPolyhedron C)
QQ * Polyhedron := (k,P) -> (
-- Checking for input errors
if k <= 0 then error("The factor must be strictly positiv");
Q:=new Polyhedron from hashTable {};
if P#?"Points" then Q#"Points"=homCoordinates(k*(dehomCoordinates P#"Points")_0,(dehomCoordinates P#"Points")_1);
if P#?"InputLineality" then Q#"InputLineality"=P#"InputLineality";
if P#?"Vertices" then Q#"Vertices"=homCoordinates(k*(dehomCoordinates P#"Vertices")_0,(dehomCoordinates P#"Vertices")_1);
if P#?"LinealitySpace" then Q#"LinealitySpace"=P#"LinealitySpace";
if P#?"Inequalities" then Q#"Inequalities"=((k*(P#"Inequalities")_{0})|(P#"Inequalities")_(toList(1..numColumns P#"Inequalities")));
if P#?"Equations" then Q#"Equations"=P#"Equations";
if P#?"Facets" then Q#"Facets"=((k*(P#"Facets")_{0})|(P#"Facets")_(toList(1..numColumns P#"Facets"-1)));
if P#?"AffineHull" then Q#"AffineHull"=P#"AffineHull";
if P#?"AmbDim" then Q#"AmbDim"=P#"AmbDim";
if P#?"ConeDim" then Q#"ConeDim"=P#"ConeDim";
Q)
coneToPolyhedron=method()
coneToPolyhedron Cone:=P->(
Q:=new Polyhedron from hashTable {};
if P#?"InputRays" then Q#"Points"=homRays(P#"InputRays");
if P#?"InputLineality" then Q#"InputLineality"=homRays P#"InputLineality";
if P#?"Rays" then Q#"Vertices"=homRays P#"Rays";
if P#?"LinealitySpace" then Q#"LinealitySpace"=homRays P#"LinealitySpace";
if P#?"Inequalities" then Q#"Inequalities"=homRays P#"Inequalities";
if P#?"Equations" then Q#"Equations"=homRays P#"Equations";
if P#?"Facets" then Q#"Facets"=homRays P#"Facets";
if P#?"LinSpan" then Q#"LinSpan"=homRays P#"LinSpan";
if P#?"AmbDim" then Q#"AmbDim"=P#"AmbDim";
if P#?"ConeDim" then Q#"ConeDim"=P#"ConeDim";
Q)
ZZ * Polyhedron := (k,P) -> promote(k,QQ) * P
Cone + Polyhedron := minkowskiSum
Cone + Cone := minkowskiSum
directProduct = method ()
directProduct(Cone,Polyhedron) :=(C,P)->directProduct((coneToPolyhedron C),P)
directProduct (Polyhedron ,Cone):=(P,C)->directProduct(P,(coneToPolyhedron C))
directProduct (Polyhedron, Polyhedron):=(P1,P2)->(
--very lazy implementation; we should really check what keys exist
C:=convexHull((vertices P1)++(vertices P2),(rays P1)++(rays P2),linSpace P1++linSpace P2);
C#"LinealitySpace"=C#"InputLineality";
C#"Vertices"=C#"Points";
C )
directProduct (Cone, Cone):=(P1,P2)->(
--very lazy implementation; we should really check what keys exist
C:=posHull(rays P1++rays P2,linSpace P1++linSpace P2);
C#"LinealitySpace"=C#"InputLineality";
C#"Rays"=C#"InputRays";
C
)
PolyhedralObject * PolyhedralObject := directProduct
Polyhedron + Polyhedron := minkowskiSum
Polyhedron + Cone := minkowskiSum
-- PURPOSE : Computing the Hilbert basis of a Cone
-- INPUT : 'C', a Cone
-- OUTPUT : 'L', a list containing the Hilbert basis as one column matrices
hilbertBasis = method(TypicalValue => List,Options=>pmopt)
hilbertBasis Cone := opts->C -> (
if C#?"HilbertBasis" then return (apply(numRows C#"HilbertBasis",i->(transpose C#"HilbertBasis")_{i}));
if opts#UsePolymake then runPolymake(C,"HilbertBasis")
else (
-- Computing the row echolon form of the matrix M
ref := M -> (
n := numColumns M;
s := numRows M;
BC := map(ZZ^n,ZZ^n,1);
m := min(n,s);
-- Scan through the first square part of 'M'
i := 0;
stopper := 0;
while i < m and stopper < n do (
-- Selecting the first non-zero entry after the i-th row in the i-th column
j := select(1,toList(i..s-1),k -> M_i_k != 0);
-- if there is a non-zero entry, scan the remaining entries and compute the reduced form for this column
if j != {} then (
j = j#0;
scan((j+1)..(s-1), k -> (
if M_i_k != 0 then (
a := M_i_j;
b := M_i_k;
L := gcdCoefficients(a,b);
a = substitute(a/(L#0),ZZ);
b = substitute(b/(L#0),ZZ);
M = M^{0..j-1} || (L#1)*M^{j} + (L#2)*M^{k} || M^{j+1..k-1} || (-b)*M^{j} + a*M^{k} || M^{k+1..s-1})));
if i != j then (
M = M^{0..i-1} || M^{j} || M^{i+1..j-1} || M^{i} || M^{j+1..s-1});
if M_i_i < 0 then M = M^{0..i-1} || -M^{i} || M^{i+1..s-1})
else (
M = M_{0..i-1} | M_{i+1..n-1} | M_{i};
BC = BC_{0..i-1} | BC_{i+1..n-1} | BC_{i};
i = i-1);
i = i+1;
stopper = stopper + 1);
(M,BC));
-- Function to compute the/one preimage of h under A
preim := (h,A) -> (
-- Take the generators of the kernel of '-h|A' and find an element with 1 as first entry -> the other entrys are a preimage
-- vector
N := gens ker(-h|A);
N = transpose (ref transpose N)#0;
N_{0}^{1..(numRows N)-1});
A := -halfspaces C;
if hyperplanes C =!= 0 then A = A || hyperplanes C || -hyperplanes C;
A = substitute(A,ZZ);
-- Use the project and lift algorithm to compute a basis of the space of vectors positive on 'A' whose preimages are the HilbertBasis
(B,BC) := ref transpose A;
H := constructHilbertBasis B;
BC = inverse transpose BC;
C#"HilbertBasis"=transpose matrix {apply(H,h -> preim(BC*h,A))});
(apply(numRows C#"HilbertBasis",i->(transpose C#"HilbertBasis")_{i})))
-- PURPOSE : Check if 'P' contains 'Q'
contains = method(TypicalValue => Boolean)
contains(Polyhedron,Polyhedron) := (P,Q) -> (
vertices Q;
halfspaces P;
hyperplanes P;
linSpace Q;
A:=Q#"Vertices";
B:=Q#"LinealitySpace";
C:=P#"Facets";
D:=P#"AffineHull";
((A||B) * transpose D)==0 and (B*(transpose C)==0) and
all(flatten entries ( A* transpose C),i->i>=0))
contains(Cone,Cone) := (P,Q) -> (
vertices Q;
halfspaces P;
hyperplanes P;
linSpace Q;
A:=Q#"Rays";
B:=Q#"LinealitySpace";
C:=P#"Facets";
D:=P#"LinearSpan";
((A||B) * transpose D)==0 and (B*(transpose C)==0) and
all(flatten entries ( A* transpose C),i->i>=0))
contains(Polyhedron,Matrix) := (P,p) -> (
-- checking for input errors
if ambDim P =!= numRows p then error("Polyhedron and point must lie in the same ambient space");
if numColumns p =!= 1 then error("The point must be given as a one row matrix");
contains(P,convexHull p))
contains(Cone,Matrix) := (C,p) -> (
-- checking for input errors
if ambDim C =!= numRows p then error("Polyhedron and point must lie in the same ambient space");
if numColumns p =!= 1 then error("The point must be given as a one row matrix");
contains(C,convexHull p))
contains(Cone,Polyhedron):=(P,Q)->(coneToPolyhedron P,Q)
contains(Polyhedron,Cone):=(P,Q)->(P,coneToPolyhedron Q)
contains(List,Cone) := (L,C) -> any(L, C1 -> C1 == C)
contains(List,Polyhedron) := (L,P) -> any(L, Q -> Q == P)
tailCone = method(TypicalValue => Cone)
tailCone Polyhedron := P -> posHull(rays P,linSpace P) --computes more than necessary
stdSimplex = method(TypicalValue => Polyhedron)
stdSimplex ZZ := d -> (
-- Checking for input errors
if d < 0 then error("dimension must not be negative");
-- Generating the standard basis
convexHull map(QQ^(d+1),QQ^(d+1),1))
posOrthant = method(TypicalValue => Cone)
posOrthant ZZ := n -> posHull map(QQ^n,QQ^n,1)
pyramid = method(TypicalValue => Polyhedron)
pyramid Polyhedron := P -> (
vertices P;
A:=P#"Vertices";
B:=P#"LinealitySpace";
n:=ambDim P;
new Polyhedron from hashTable{
"Vertices"=>(A|(map(QQ^(numRows A),QQ^1,0))||map(QQ^1,QQ^(n+2),(j,i)->(if i==0 or i==n+1 then 1 else 0))),
"LinealitySpace"=>B|map(QQ^(numRows B),QQ^1,0)
})
-- PURPOSE : Computing the bipyramid over the polyhedron 'P'
-- INPUT : 'P', a polyhedron
-- OUTPUT : A polyhedron, the convex hull of 'P', embedded into ambientdim+1 space and the
-- points (barycenter of 'P',+-1)
bipyramid = method(TypicalValue => Polyhedron)
bipyramid Polyhedron := P -> (
-- Saving the vertices
V := promote(vertices P,QQ);
linSpace P;
A:=P#"Vertices";
B:=P#"LinealitySpace";
n := numColumns V;
if n == 0 then error("P must not be empty");
-- Computing the barycenter of P
v := matrix toList(n:{1_QQ});
v = (1/n)*V*v;
vplus:=matrix {{1}} | (transpose v) | matrix {{1}};
vminus:=matrix {{1}} | (transpose v) | matrix {{-1}};
new Polyhedron from hashTable{
"Vertices"=>(A|(map(QQ^(numRows A),QQ^1,0))||vplus||vminus),
"LinealitySpace"=>B|map(QQ^(numRows B),QQ^1,0)
})
-- PURPOSE : Generating the 'd'-dimensional crosspolytope with edge length 2*'s'
crossPolytope = method(TypicalValue => Polyhedron)
-- INPUT : '(d,s)', where 'd' is a strictly positive integer, the dimension of the polytope, and 's' is
-- a strictly positive rational number, the distance of the vertices to the origin
-- OUTPUT : The 'd'-dimensional crosspolytope with vertex-origin distance 's'
crossPolytope(ZZ,QQ) := (d,s) -> (
-- Checking for input errors
if d < 1 then error("dimension must at least be 1");
if s <= 0 then error("size of the crosspolytope must be positive");
constructMatrix := (d,v) -> (
if d != 0 then flatten {constructMatrix(d-1,v|{-1}),constructMatrix(d-1,v|{1})}
else {v});
homVert := (matrix {toList(2*d:1_QQ)} || (map(QQ^d,QQ^d,s) | map(QQ^d,QQ^d,-s)));
new Polyhedron from hashTable {
"Vertices"=>transpose homVert,
"LinealitySpace"=>map(QQ^0,QQ^(d+1),0)})
-- INPUT : '(d,s)', where 'd' is a strictly positive integer, the dimension of the polytope, and 's' is a
-- strictly positive integer, the distance of the vertices to the origin
crossPolytope(ZZ,ZZ) := (d,s) -> crossPolytope(d,promote(s,QQ))
-- INPUT : 'd', where 'd' is a strictly positive integer, the dimension of the polytope
crossPolytope ZZ := d -> crossPolytope(d,1_QQ)
-- PURPOSE : Computing the cyclic polytope of n points in QQ^d
-- INPUT : '(d,n)', two positive integers
-- OUTPUT : A polyhedron, the convex hull of 'n' points on the moment curve in 'd' space
-- COMMENT : The moment curve is defined by t -> (t,t^2,...,t^d) in QQ^d, if we say we take 'n' points
-- on the moment curve, we mean the images of 0,...,n-1
cyclicPolytope = method(TypicalValue => Polyhedron)
cyclicPolytope(ZZ,ZZ) := (d,n) -> (
-- Checking for input errors
if d < 1 then error("The dimension must be positive");
if n < 1 then error("There must be a positive number of points");
convexHull map(ZZ^d, ZZ^n, (i,j) -> j^(i+1)))
-- PURPOSE : Generating the 'd'-dimensional hypercube with edge length 2*'s'
hypercube = method(TypicalValue => Polyhedron)
-- INPUT : '(d,s)', where 'd' is a strictly positive integer, the dimension of the polytope, and
-- 's' is a positive rational number, half of the edge length
-- OUTPUT : The 'd'-dimensional hypercube with edge length 2*'s' as a polyhedron
hypercube(ZZ,QQ) := (d,s) -> (
-- Checking for input errors
if d < 1 then error("dimension must at least be 1");
if s <= 0 then error("size of the hypercube must be positive");
-- Generating half-spaces matrix and vector
intersection(map(QQ^d,QQ^d,1) || -map(QQ^d,QQ^d,1),matrix toList(2*d:{s})))
-- INPUT : '(d,s)', where 'd' is a strictly positive integer, the dimension of the polytope, and
-- 's' is a positive integer, half of the edge length
hypercube(ZZ,ZZ) := (d,s) -> hypercube(d,promote(s,QQ))
-- INPUT : 'd', is a strictly positive integer, the dimension of the polytope
hypercube ZZ := d -> hypercube(d,1_QQ)
-- PURPOSE : Computing the Newton polytope for a given (Laurent) polynomial
-- INPUT : 'p', a RingElement
-- OUTPUT : The polyhedron that has the exponent vectors of the monomials of 'p' as vertices
newtonPolytope = method(TypicalValue => Polyhedron)
newtonPolytope RingElement := p -> (
if class class p===PolynomialRing then convexHull transpose matrix exponents p
else if class class p===FractionField then (
f:=numerator p;
l:=(exponents denominator p);
if #l =!=1 then error("Not a (Laurent) polynomial");
convexHull transpose matrix apply(exponents f,i->i-l_0))
else error ("Not a (Laurent) polynomial"))
-- PURPOSE : Computing the Ehrhart polynomial of a polytope
-- INPUT : 'P', a polyhedron which must be compact, i.e. a polytope
-- OUTPUT : A polynomial in QQ[x], the Ehrhart polynomial
-- COMMENT : Compactness is checked within latticePoints
ehrhart = method(TypicalValue => RingElement,Options =>pmopt)
ehrhart Polyhedron := opts->P -> (
n := dim P;
if not P#?"EhrhartPolynomialCoeff" then (
if opts#UsePolymake then runPolymake(P,"EhrhartPolynomialCoeff")
else (
v := matrix apply(n,k -> {-1+#latticePoints( (k+1)*P)});
M := promote(matrix apply(n,i -> reverse apply(n, j -> (i+1)^(j+1))),QQ);
P#"EhrhartPolynomialCoeff"={1}|reverse flatten entries ((inverse M)*v)));
R := QQ[getSymbol "x"];
x := R_"x";
sum apply(n+1,i -> P#"EhrhartPolynomialCoeff"#i * x^(i)))
-- PURPOSE : Triangulating a compact Polyhedron
-- INPUT : 'P', a Polyhedron
-- OUTPUT : A list of the simplices of the triangulation. Each simplex is given by a list
-- of its vertices.
--COMMENTS : The triangulation is build recursively, for each face that is not a simplex it takes
-- the weighted centre of the face. for each codim 1 face of this face it either takes the
-- convex hull with the centre if it is a simplex or triangulates this in the same way.
triangulate = method()
triangulate Polyhedron := P -> (
-- Defining the recursive face triangulation
-- This takes a polytope and computes all facets. For each facet that is not a simplex, it calls itself
-- again to replace this facet by a triangulation of it. then it has a list of simplices triangulating
-- the facets. Then it computes for each of these simplices the convex hull with the weighted centre of
-- the input polytope. The weighted centre is the sum of the vertices divided by the number of vertices.
-- It returns the resulting list of simplices in a list, where each simplex is given by a list of its
-- vertices.
-- The function also needs the dimension of the Polyhedron 'd', the list of facets of the original
-- polytope, the list 'L' of triangulations computed so far and the dimension of the original Polytope.
-- 'L' contains a hash table for each dimension of faces of the original Polytope (i.e. from 0 to 'n').
-- If a face has been triangulated than the list of simplices is saved in the hash table of the
-- corresponding dimension with the weighted centre of the original face as key.
recursiveFaceTriangulation := (P,d,originalFacets,L,n) -> (
-- Computing the facets of P, given as lists of their vertices
F := intersectionWithFacets({(set P,set{})},originalFacets);
F = apply(F, f -> toList(f#0));
d = d-1;
-- if the facets are at least 2 dimensional, then check if they are simplices, if not call this
-- function again
if d > 1 then (
F = flatten apply(F, f -> (
-- Check if the face is a simplex
if #f != d+1 then (
-- Computing the weighted centre
p := (sum f)*(1/#f);
-- Taking the hash table of the corresponding dimension
-- Checking if the triangulation has been computed already
if L#d#?p then L#d#p
else (
-- if not, call this function again for 'f' and then save this in 'L'
(f,L) = recursiveFaceTriangulation(f,d,originalFacets,L,n);
L = merge(L,hashTable {d => hashTable{p => f}},(x,y) -> merge(x,y,));
f))
else {f})));
-- Adding the weighted centre to each face simplex
q := (sum P)*(1/#P);
P = apply(F, f -> f | {q});
(P,L));
-- Checking for input errors
if not isCompact P then error("The polytope must be compact!");
n := dim P;
-- Computing the facets of P as lists of their vertices
(HS,v) := halfspaces P;
(HP,w) := hyperplanes P;
originalFacets := apply(numRows HS, i -> intersection(HS,v, HP || HS^{i}, w || v^{i}));
originalFacets = apply(originalFacets, f -> (
V := vertices f;
(set apply(numColumns V, i -> V_{i}),set {})));
-- Making a list of the vertices of P
P = vertices P;
P = apply(numColumns P, i -> P_{i});
if #P == n+1 then {P} else (
d := n;
-- Initiating the list of already computed triangulations
L := hashTable apply(n+1, i -> i => hashTable {});
(P,L) = recursiveFaceTriangulation(P,d,originalFacets,L,n);
P))
-- PURPOSE : Computing the volume of a full dimensional polytope
-- INPUT : 'P', a compact polyhedron
-- OUTPUT : QQ, giving the volume of the polytope
volume = method(TypicalValue => QQ,Options=>pmopt)
volume Polyhedron := opts->Q -> (
d := dim(Q);
if not Q#?"LatticeVolume" then (
if not isCompact Q then error("The polyhedron must be compact, i.e. a polytope.");
if opts#UsePolymake then runPolymake(Q,"LatticeVolume")
else (
-- If P is not full dimensional then project it down
P:=Q;
if d != ambDim Q then (
A := substitute((hyperplanes Q)#0,ZZ);
A = inverse (smithNormalForm A)#2;
n := ambDim Q;
A = A^{n-d..n-1};
P = affineImage(A,Q));
-- Computing the triangulation of P
P = triangulate P;
-- Computing the volume of each simplex without the dimension factor, by
-- taking the absolute of the determinant of |v_1-v_0..v_d-v_0|
P = apply(P, p -> abs det matrix transpose apply(toList(1..d), i -> flatten entries(p#i - p#0)));
-- Summing up the volumes and dividing out the dimension factor
Q#"LatticeVolume"=(sum P)));
(Q#"LatticeVolume")/d!)
sublatticeBasis = method(TypicalValue => Matrix,Options=>pmopt)
-- INPUT : 'M', a Matrix
-- OUTPUT : A matrix, a basis of the sublattice spanned by the lattice points in 'M'
sublatticeBasis Matrix := pmopt-> M -> (
-- Checking for input errors
M = promote(M,QQ);
M = if promote(substitute(M,ZZ),QQ) == M then substitute(M,ZZ) else error("The matrix must contain only lattice points.");
-- The sublattice is isomorphic to source mod kernel, i.e. A/K
A := source M;
K := ker M;
-- Taking minimal generators and applying M gives a basis in target M
M*(mingens (A/K)))
-- INPUT : 'P', a polyhedron,
-- OUTPUT : A matrix, a basis of the sublattice spanned by the lattice points of 'P'
sublatticeBasis Polyhedron := opts->P -> (
L := latticePoints(P,opts);
-- Checking for input errors
if L == {} then error("The polytope must contain lattice points.");
-- Translating 'P' so that it contains the origin if it did not already
if all(L,l -> l != 0) then L = apply(L, l -> l - L#0);
sublatticeBasis(matrix {L}))
-- PURPOSE : Calculating the preimage of a polytope in the sublattice generated by its lattice points
-- INPUT : 'P', a polyhedron
-- OUTPUT : A polyhedron, the projected polyhedron, which is now normal
toSublattice = method(Options=>pmopt)
toSublattice Polyhedron := opts->P -> (
L := latticePoints P;
-- Checking for input errors
if L == {} then error("The polytope must contain lattice points.");
b := L#0;
-- Translating 'P' so that it contains the origin if it did not already
if all(L,l -> l != 0) then L = apply(L, l -> l - L#0);
affinePreimage(sublatticeBasis matrix {L},P,b))
-- PURPOSE : Computes the mixed volume of n polytopes in n-space
-- INPUT : 'L' a list of n polytopes in n-space
-- OUTPUT : the mixed volume
-- COMMENT : Note that at the moment the input is NOT checked!
mixedVolume = method()
mixedVolume List := L -> (