This repository has been archived by the owner on Jan 30, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
base3.py
1836 lines (1474 loc) · 70.7 KB
/
base3.py
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
r"""
Base class for polyhedra, part 3
Define methods related to the combinatorics of a polyhedron
excluding methods relying on :mod:`sage.graphs`.
"""
# ****************************************************************************
# Copyright (C) 2008-2012 Marshall Hampton <hamptonio@gmail.com>
# Copyright (C) 2011-2015 Volker Braun <vbraun.name@gmail.com>
# Copyright (C) 2012-2018 Frederic Chapoton
# Copyright (C) 2013 Andrey Novoseltsev
# Copyright (C) 2014-2017 Moritz Firsching
# Copyright (C) 2014-2019 Thierry Monteil
# Copyright (C) 2015 Nathann Cohen
# Copyright (C) 2015-2017 Jeroen Demeyer
# Copyright (C) 2015-2017 Vincent Delecroix
# Copyright (C) 2015-2018 Dima Pasechnik
# Copyright (C) 2015-2020 Jean-Philippe Labbe <labbe at math.huji.ac.il>
# Copyright (C) 2015-2021 Matthias Koeppe
# Copyright (C) 2016-2019 Daniel Krenn
# Copyright (C) 2017 Marcelo Forets
# Copyright (C) 2017-2018 Mark Bell
# Copyright (C) 2019 Julian Ritter
# Copyright (C) 2019-2020 Laith Rastanawi
# Copyright (C) 2019-2020 Sophia Elia
# Copyright (C) 2019-2021 Jonathan Kliem <jonathan.kliem@fu-berlin.de>
#
# 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 2 of the License, or
# (at your option) any later version.
# https://www.gnu.org/licenses/
# ****************************************************************************
from sage.misc.cachefunc import cached_method
from sage.matrix.constructor import matrix
from sage.rings.integer_ring import ZZ
from sage.rings.rational_field import QQ
from .base2 import Polyhedron_base2
class Polyhedron_base3(Polyhedron_base2):
"""
Methods related to the combinatorics of a polyhedron.
See :class:`sage.geometry.polyhedron.base.Polyhedron_base`.
TESTS::
sage: from sage.geometry.polyhedron.base3 import Polyhedron_base3
sage: P = polytopes.cube()
sage: Polyhedron_base3.is_simple(P)
True
sage: Polyhedron_base3.is_simplicial(P)
False
sage: Polyhedron_base3.is_prism(P)
True
sage: Polyhedron_base3.is_pyramid(P)
False
sage: Polyhedron_base3.combinatorial_polyhedron.f(P)
A 3-dimensional combinatorial polyhedron with 6 facets
sage: Polyhedron_base3.facets(P)
(A 2-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 4 vertices,
A 2-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 4 vertices,
A 2-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 4 vertices,
A 2-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 4 vertices,
A 2-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 4 vertices,
A 2-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 4 vertices)
sage: Polyhedron_base3.f_vector.f(P)
(1, 8, 12, 6, 1)
sage: next(Polyhedron_base3.face_generator(P))
A 3-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 8 vertices
"""
def _init_empty_polyhedron(self):
"""
Initializes an empty polyhedron.
TESTS::
sage: Polyhedron().vertex_adjacency_matrix() # indirect doctest
[]
sage: Polyhedron().facet_adjacency_matrix()
[0]
"""
Polyhedron_base2._init_empty_polyhedron(self)
V_matrix = matrix(ZZ, 0, 0, 0)
V_matrix.set_immutable()
self.vertex_adjacency_matrix.set_cache(V_matrix)
H_matrix = matrix(ZZ, 1, 1, 0)
H_matrix.set_immutable()
self.facet_adjacency_matrix.set_cache(H_matrix)
@cached_method
def slack_matrix(self):
r"""
Return the slack matrix.
The entries correspond to the evaluation of the Hrepresentation
elements on the Vrepresentation elements.
.. NOTE::
The columns correspond to inequalities/equations in the
order :meth:`Hrepresentation`, the rows correspond to
vertices/rays/lines in the order
:meth:`Vrepresentation`.
.. SEEALSO::
:meth:`incidence_matrix`.
EXAMPLES::
sage: P = polytopes.cube()
sage: P.slack_matrix()
[0 2 2 2 0 0]
[0 0 2 2 0 2]
[0 0 0 2 2 2]
[0 2 0 2 2 0]
[2 2 0 0 2 0]
[2 2 2 0 0 0]
[2 0 2 0 0 2]
[2 0 0 0 2 2]
sage: P = polytopes.cube(intervals='zero_one')
sage: P.slack_matrix()
[0 1 1 1 0 0]
[0 0 1 1 0 1]
[0 0 0 1 1 1]
[0 1 0 1 1 0]
[1 1 0 0 1 0]
[1 1 1 0 0 0]
[1 0 1 0 0 1]
[1 0 0 0 1 1]
sage: P = polytopes.dodecahedron().faces(2)[0].as_polyhedron()
sage: P.slack_matrix()
[1/2*sqrt5 - 1/2 0 0 1 1/2*sqrt5 - 1/2 0]
[ 0 0 1/2*sqrt5 - 1/2 1/2*sqrt5 - 1/2 1 0]
[ 0 1/2*sqrt5 - 1/2 1 0 1/2*sqrt5 - 1/2 0]
[ 1 1/2*sqrt5 - 1/2 0 1/2*sqrt5 - 1/2 0 0]
[1/2*sqrt5 - 1/2 1 1/2*sqrt5 - 1/2 0 0 0]
sage: P = Polyhedron(rays=[[1, 0], [0, 1]])
sage: P.slack_matrix()
[0 0]
[0 1]
[1 0]
TESTS::
sage: Polyhedron().slack_matrix()
[]
sage: Polyhedron(base_ring=QuadraticField(2)).slack_matrix().base_ring() # optional - sage.rings.number_field
Number Field in a with defining polynomial x^2 - 2 with a = 1.41...
"""
if not self.n_Vrepresentation() or not self.n_Hrepresentation():
slack_matrix = matrix(self.base_ring(), self.n_Vrepresentation(),
self.n_Hrepresentation(), 0)
else:
Vrep_matrix = matrix(self.base_ring(), self.Vrepresentation())
Hrep_matrix = matrix(self.base_ring(), self.Hrepresentation())
# Getting homogeneous coordinates of the Vrepresentation.
hom_helper = matrix(self.base_ring(), [1 if v.is_vertex() else 0 for v in self.Vrepresentation()])
hom_Vrep = hom_helper.stack(Vrep_matrix.transpose())
slack_matrix = (Hrep_matrix * hom_Vrep).transpose()
slack_matrix.set_immutable()
return slack_matrix
@cached_method
def incidence_matrix(self):
"""
Return the incidence matrix.
.. NOTE::
The columns correspond to inequalities/equations in the
order :meth:`Hrepresentation`, the rows correspond to
vertices/rays/lines in the order
:meth:`Vrepresentation`.
.. SEEALSO::
:meth:`slack_matrix`.
EXAMPLES::
sage: p = polytopes.cuboctahedron()
sage: p.incidence_matrix()
[0 0 1 1 0 1 0 0 0 0 1 0 0 0]
[0 0 0 1 0 0 1 0 1 0 1 0 0 0]
[0 0 1 1 1 0 0 1 0 0 0 0 0 0]
[1 0 0 1 1 0 1 0 0 0 0 0 0 0]
[0 0 0 0 0 1 0 0 1 1 1 0 0 0]
[0 0 1 0 0 1 0 1 0 0 0 1 0 0]
[1 0 0 0 0 0 1 0 1 0 0 0 1 0]
[1 0 0 0 1 0 0 1 0 0 0 0 0 1]
[0 1 0 0 0 1 0 0 0 1 0 1 0 0]
[0 1 0 0 0 0 0 0 1 1 0 0 1 0]
[0 1 0 0 0 0 0 1 0 0 0 1 0 1]
[1 1 0 0 0 0 0 0 0 0 0 0 1 1]
sage: v = p.Vrepresentation(0)
sage: v
A vertex at (-1, -1, 0)
sage: h = p.Hrepresentation(2)
sage: h
An inequality (1, 1, -1) x + 2 >= 0
sage: h.eval(v) # evaluation (1, 1, -1) * (-1/2, -1/2, 0) + 1
0
sage: h*v # same as h.eval(v)
0
sage: p.incidence_matrix() [0,2] # this entry is (v,h)
1
sage: h.contains(v)
True
sage: p.incidence_matrix() [2,0] # note: not symmetric
0
The incidence matrix depends on the ambient dimension::
sage: simplex = polytopes.simplex(); simplex
A 3-dimensional polyhedron in ZZ^4 defined as the convex hull of 4 vertices
sage: simplex.incidence_matrix()
[1 1 1 1 0]
[1 1 1 0 1]
[1 1 0 1 1]
[1 0 1 1 1]
sage: simplex = simplex.affine_hull_projection(); simplex
A 3-dimensional polyhedron in ZZ^3 defined as the convex hull of 4 vertices
sage: simplex.incidence_matrix()
[1 1 1 0]
[1 1 0 1]
[1 0 1 1]
[0 1 1 1]
An incidence matrix does not determine a unique
polyhedron::
sage: P = Polyhedron(vertices=[[0,1],[1,1],[1,0]])
sage: P.incidence_matrix()
[1 1 0]
[1 0 1]
[0 1 1]
sage: Q = Polyhedron(vertices=[[0,1], [1,0]], rays=[[1,1]])
sage: Q.incidence_matrix()
[1 1 0]
[1 0 1]
[0 1 1]
An example of two polyhedra with isomorphic face lattices
but different incidence matrices::
sage: Q.incidence_matrix()
[1 1 0]
[1 0 1]
[0 1 1]
sage: R = Polyhedron(vertices=[[0,1], [1,0]], rays=[[1,3/2], [3/2,1]])
sage: R.incidence_matrix()
[1 1 0]
[1 0 1]
[0 1 0]
[0 0 1]
The incidence matrix has base ring integers. This way one can express various
counting questions::
sage: P = polytopes.twenty_four_cell()
sage: M = P.incidence_matrix()
sage: sum(sum(x) for x in M) == P.flag_f_vector(0, 3) # optional - sage.combinat
True
TESTS:
Check that :trac:`28828` is fixed::
sage: R.incidence_matrix().is_immutable()
True
Test that this method works for inexact base ring
(``cdd`` sets the cache already)::
sage: P = polytopes.dodecahedron(exact=False) # optional - sage.groups
sage: M = P.incidence_matrix.cache # optional - sage.groups
sage: P.incidence_matrix.clear_cache() # optional - sage.groups
sage: M == P.incidence_matrix() # optional - sage.groups
True
"""
if self.base_ring() in (ZZ, QQ):
# Much faster for integers or rationals.
incidence_matrix = self.slack_matrix().zero_pattern_matrix(ZZ)
incidence_matrix.set_immutable()
return incidence_matrix
incidence_matrix = matrix(ZZ, self.n_Vrepresentation(),
self.n_Hrepresentation(), 0)
Vvectors_vertices = tuple((v.vector(), v.index())
for v in self.Vrep_generator()
if v.is_vertex())
Vvectors_rays_lines = tuple((v.vector(), v.index())
for v in self.Vrep_generator()
if not v.is_vertex())
# Determine ``is_zero`` to save lots of time.
if self.base_ring().is_exact():
def is_zero(x):
return not x
else:
is_zero = self._is_zero
for H in self.Hrep_generator():
Hconst = H.b()
Hvec = H.A()
Hindex = H.index()
for Vvec, Vindex in Vvectors_vertices:
if is_zero(Hvec*Vvec + Hconst):
incidence_matrix[Vindex, Hindex] = 1
# A ray or line is considered incident with a hyperplane,
# if it is orthogonal to the normal vector of the hyperplane.
for Vvec, Vindex in Vvectors_rays_lines:
if is_zero(Hvec*Vvec):
incidence_matrix[Vindex, Hindex] = 1
incidence_matrix.set_immutable()
return incidence_matrix
@cached_method
def combinatorial_polyhedron(self):
r"""
Return the combinatorial type of ``self``.
See :class:`sage.geometry.polyhedron.combinatorial_polyhedron.base.CombinatorialPolyhedron`.
EXAMPLES::
sage: polytopes.cube().combinatorial_polyhedron()
A 3-dimensional combinatorial polyhedron with 6 facets
sage: polytopes.cyclic_polytope(4,10).combinatorial_polyhedron()
A 4-dimensional combinatorial polyhedron with 35 facets
sage: Polyhedron(rays=[[0,1], [1,0]]).combinatorial_polyhedron()
A 2-dimensional combinatorial polyhedron with 2 facets
"""
from sage.geometry.polyhedron.combinatorial_polyhedron.base import CombinatorialPolyhedron
return CombinatorialPolyhedron(self)
def _test_combinatorial_polyhedron(self, tester=None, **options):
"""
Run test suite of combinatorial polyhedron.
TESTS::
sage: polytopes.cross_polytope(3)._test_combinatorial_polyhedron()
"""
from sage.misc.sage_unittest import TestSuite
tester = self._tester(tester=tester, **options)
tester.info("\n Running the test suite of self.combinatorial_polyhedron()")
TestSuite(self.combinatorial_polyhedron()).run(verbose=tester._verbose,
prefix=tester._prefix+" ")
tester.info(tester._prefix+" ", newline = False)
def face_generator(self, face_dimension=None, dual=None, algorithm=None):
r"""
Return an iterator over the faces of given dimension.
If dimension is not specified return an iterator over all faces.
INPUT:
- ``face_dimension`` -- integer (default ``None``),
yield only faces of this dimension if specified
- ``dual`` -- boolean (default ``None``);
if ``True``, pick dual algorithm
if ``False``, pick primal algorithm
- ``algorithm`` -- string (optional);
specify whether to start with facets or vertices:
* ``'primal'`` -- start with the facets
* ``'dual'`` -- start with the vertices
* ``None`` -- choose automatically
OUTPUT:
A :class:`~sage.geometry.polyhedron.combinatorial_polyhedron.face_iterator.FaceIterator_geom`.
This class iterates over faces as
:class:`~sage.geometry.polyhedron.face.PolyhedronFace`. See
:mod:`~sage.geometry.polyhedron.face` for details. The order
is random but fixed.
EXAMPLES::
sage: P = polytopes.cube()
sage: it = P.face_generator()
sage: it
Iterator over the faces of a 3-dimensional polyhedron in ZZ^3
sage: list(it)
[A 3-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 8 vertices,
A -1-dimensional face of a Polyhedron in ZZ^3,
A 2-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 4 vertices,
A 2-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 4 vertices,
A 2-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 4 vertices,
A 2-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 4 vertices,
A 2-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 4 vertices,
A 2-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 4 vertices,
A 1-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 2 vertices,
A 1-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 2 vertices,
A 1-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 2 vertices,
A 1-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 2 vertices,
A 0-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 1 vertex,
A 0-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 1 vertex,
A 0-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 1 vertex,
A 0-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 1 vertex,
A 1-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 2 vertices,
A 1-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 2 vertices,
A 1-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 2 vertices,
A 0-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 1 vertex,
A 0-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 1 vertex,
A 1-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 2 vertices,
A 1-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 2 vertices,
A 0-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 1 vertex,
A 1-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 2 vertices,
A 1-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 2 vertices,
A 0-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 1 vertex,
A 1-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 2 vertices]
sage: P = polytopes.hypercube(4)
sage: list(P.face_generator(2))[:4]
[A 2-dimensional face of a Polyhedron in ZZ^4 defined as the convex hull of 4 vertices,
A 2-dimensional face of a Polyhedron in ZZ^4 defined as the convex hull of 4 vertices,
A 2-dimensional face of a Polyhedron in ZZ^4 defined as the convex hull of 4 vertices,
A 2-dimensional face of a Polyhedron in ZZ^4 defined as the convex hull of 4 vertices]
If a polytope has more facets than vertices, the dual mode is chosen::
sage: P = polytopes.cross_polytope(3)
sage: list(P.face_generator())
[A 3-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 6 vertices,
A -1-dimensional face of a Polyhedron in ZZ^3,
A 0-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 1 vertex,
A 0-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 1 vertex,
A 0-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 1 vertex,
A 0-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 1 vertex,
A 0-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 1 vertex,
A 0-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 1 vertex,
A 1-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 2 vertices,
A 1-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 2 vertices,
A 1-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 2 vertices,
A 1-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 2 vertices,
A 2-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 3 vertices,
A 2-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 3 vertices,
A 2-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 3 vertices,
A 2-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 3 vertices,
A 1-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 2 vertices,
A 1-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 2 vertices,
A 1-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 2 vertices,
A 2-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 3 vertices,
A 2-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 3 vertices,
A 1-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 2 vertices,
A 1-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 2 vertices,
A 2-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 3 vertices,
A 1-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 2 vertices,
A 1-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 2 vertices,
A 2-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 3 vertices,
A 1-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 2 vertices]
The face iterator can also be slightly modified.
In non-dual mode we can skip subfaces of the current (proper) face::
sage: P = polytopes.cube()
sage: it = P.face_generator(algorithm='primal')
sage: _ = next(it), next(it)
sage: face = next(it)
sage: face.ambient_H_indices()
(5,)
sage: it.ignore_subfaces()
sage: face = next(it)
sage: face.ambient_H_indices()
(4,)
sage: it.ignore_subfaces()
sage: [face.ambient_H_indices() for face in it]
[(3,),
(2,),
(1,),
(0,),
(2, 3),
(1, 3),
(1, 2, 3),
(1, 2),
(0, 2),
(0, 1, 2),
(0, 1)]
In dual mode we can skip supfaces of the current (proper) face::
sage: P = polytopes.cube()
sage: it = P.face_generator(algorithm='dual')
sage: _ = next(it), next(it)
sage: face = next(it)
sage: face.ambient_V_indices()
(7,)
sage: it.ignore_supfaces()
sage: next(it)
A 0-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 1 vertex
sage: face = next(it)
sage: face.ambient_V_indices()
(5,)
sage: it.ignore_supfaces()
sage: [face.ambient_V_indices() for face in it]
[(4,),
(3,),
(2,),
(1,),
(0,),
(1, 6),
(3, 4),
(2, 3),
(0, 3),
(0, 1, 2, 3),
(1, 2),
(0, 1)]
In non-dual mode, we cannot skip supfaces::
sage: it = P.face_generator(algorithm='primal')
sage: _ = next(it), next(it)
sage: next(it)
A 2-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 4 vertices
sage: it.ignore_supfaces()
Traceback (most recent call last):
...
ValueError: only possible when in dual mode
In dual mode, we cannot skip subfaces::
sage: it = P.face_generator(algorithm='dual')
sage: _ = next(it), next(it)
sage: next(it)
A 0-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 1 vertex
sage: it.ignore_subfaces()
Traceback (most recent call last):
...
ValueError: only possible when not in dual mode
We can only skip sub-/supfaces of proper faces::
sage: it = P.face_generator(algorithm='primal')
sage: next(it)
A 3-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 8 vertices
sage: it.ignore_subfaces()
Traceback (most recent call last):
...
ValueError: iterator not set to a face yet
.. SEEALSO::
:class:`~sage.geometry.polyhedron.combinatorial_polyhedron.face_iterator.FaceIterator_geom`.
ALGORITHM:
See :class:`~sage.geometry.polyhedron.combinatorial_polyhedron.face_iterator.FaceIterator`.
TESTS::
sage: P = polytopes.simplex()
sage: list(P.face_generator(-2))
[]
sage: list(P.face_generator(-1))
[A -1-dimensional face of a Polyhedron in ZZ^4]
sage: list(P.face_generator(3))
[A 3-dimensional face of a Polyhedron in ZZ^4 defined as the convex hull of 4 vertices]
sage: list(Polyhedron().face_generator())
[A -1-dimensional face of a Polyhedron in ZZ^0]
Check that :trac:`29155` is fixed::
sage: P = polytopes.permutahedron(3)
sage: [f] = P.face_generator(2)
sage: f.ambient_Hrepresentation()
(An equation (1, 1, 1) x - 6 == 0,)
Check that ``dual`` keyword still works::
sage: P = polytopes.hypercube(4)
sage: list(P.face_generator(2, dual=False))[:4]
[A 2-dimensional face of a Polyhedron in ZZ^4 defined as the convex hull of 4 vertices,
A 2-dimensional face of a Polyhedron in ZZ^4 defined as the convex hull of 4 vertices,
A 2-dimensional face of a Polyhedron in ZZ^4 defined as the convex hull of 4 vertices,
A 2-dimensional face of a Polyhedron in ZZ^4 defined as the convex hull of 4 vertices]
sage: list(P.face_generator(2, dual=True))[:4]
[A 2-dimensional face of a Polyhedron in ZZ^4 defined as the convex hull of 4 vertices,
A 2-dimensional face of a Polyhedron in ZZ^4 defined as the convex hull of 4 vertices,
A 2-dimensional face of a Polyhedron in ZZ^4 defined as the convex hull of 4 vertices,
A 2-dimensional face of a Polyhedron in ZZ^4 defined as the convex hull of 4 vertices]
Check that we catch incorrect algorithms:
sage: list(P.face_generator(2, algorithm='integrate'))[:4]
Traceback (most recent call last):
...
ValueError: algorithm must be 'primal', 'dual' or None
"""
if algorithm == 'primal':
dual = False
elif algorithm == 'dual':
dual = True
elif algorithm is not None:
raise ValueError("algorithm must be 'primal', 'dual' or None")
from sage.geometry.polyhedron.combinatorial_polyhedron.face_iterator import FaceIterator_geom
return FaceIterator_geom(self, output_dimension=face_dimension, dual=dual)
def faces(self, face_dimension):
"""
Return the faces of given dimension
INPUT:
- ``face_dimension`` -- integer. The dimension of the faces
whose representation will be returned.
OUTPUT:
A tuple of
:class:`~sage.geometry.polyhedron.face.PolyhedronFace`. See
:mod:`~sage.geometry.polyhedron.face` for details. The order
is random but fixed.
.. SEEALSO::
:meth:`face_generator`,
:meth:`facet`.
EXAMPLES:
Here we find the vertex and face indices of the eight three-dimensional
facets of the four-dimensional hypercube::
sage: p = polytopes.hypercube(4)
sage: list(f.ambient_V_indices() for f in p.faces(3))
[(0, 5, 6, 7, 8, 9, 14, 15),
(1, 4, 5, 6, 10, 13, 14, 15),
(1, 2, 6, 7, 8, 10, 11, 15),
(8, 9, 10, 11, 12, 13, 14, 15),
(0, 3, 4, 5, 9, 12, 13, 14),
(0, 2, 3, 7, 8, 9, 11, 12),
(1, 2, 3, 4, 10, 11, 12, 13),
(0, 1, 2, 3, 4, 5, 6, 7)]
sage: face = p.faces(3)[3]
sage: face.ambient_Hrepresentation()
(An inequality (1, 0, 0, 0) x + 1 >= 0,)
sage: face.vertices()
(A vertex at (-1, -1, 1, -1),
A vertex at (-1, -1, 1, 1),
A vertex at (-1, 1, -1, -1),
A vertex at (-1, 1, 1, -1),
A vertex at (-1, 1, 1, 1),
A vertex at (-1, 1, -1, 1),
A vertex at (-1, -1, -1, 1),
A vertex at (-1, -1, -1, -1))
You can use the
:meth:`~sage.geometry.polyhedron.representation.PolyhedronRepresentation.index`
method to enumerate vertices and inequalities::
sage: def get_idx(rep): return rep.index()
sage: [get_idx(_) for _ in face.ambient_Hrepresentation()]
[4]
sage: [get_idx(_) for _ in face.ambient_Vrepresentation()]
[8, 9, 10, 11, 12, 13, 14, 15]
sage: [ ([get_idx(_) for _ in face.ambient_Vrepresentation()],
....: [get_idx(_) for _ in face.ambient_Hrepresentation()])
....: for face in p.faces(3) ]
[([0, 5, 6, 7, 8, 9, 14, 15], [7]),
([1, 4, 5, 6, 10, 13, 14, 15], [6]),
([1, 2, 6, 7, 8, 10, 11, 15], [5]),
([8, 9, 10, 11, 12, 13, 14, 15], [4]),
([0, 3, 4, 5, 9, 12, 13, 14], [3]),
([0, 2, 3, 7, 8, 9, 11, 12], [2]),
([1, 2, 3, 4, 10, 11, 12, 13], [1]),
([0, 1, 2, 3, 4, 5, 6, 7], [0])]
TESTS::
sage: pr = Polyhedron(rays = [[1,0,0],[-1,0,0],[0,1,0]], vertices = [[-1,-1,-1]], lines=[(0,0,1)])
sage: pr.faces(4)
()
sage: pr.faces(3)[0].ambient_V_indices()
(0, 1, 2, 3)
sage: pr.facets()[0].ambient_V_indices()
(0, 1, 2)
sage: pr.faces(1)
()
sage: pr.faces(0)
()
sage: pr.faces(-1)
(A -1-dimensional face of a Polyhedron in QQ^3,)
"""
return tuple(self.face_generator(face_dimension))
def facets(self):
r"""
Return the facets of the polyhedron.
Facets are the maximal nontrivial faces of polyhedra.
The empty face and the polyhedron itself are trivial.
A facet of a `d`-dimensional polyhedron is a face of dimension
`d-1`. For `d \neq 0` the converse is true as well.
OUTPUT:
A tuple of
:class:`~sage.geometry.polyhedron.face.PolyhedronFace`. See
:mod:`~sage.geometry.polyhedron.face` for details. The order
is random but fixed.
.. SEEALSO:: :meth:`facets`
EXAMPLES:
Here we find the eight three-dimensional facets of the
four-dimensional hypercube::
sage: p = polytopes.hypercube(4)
sage: p.facets()
(A 3-dimensional face of a Polyhedron in ZZ^4 defined as the convex hull of 8 vertices,
A 3-dimensional face of a Polyhedron in ZZ^4 defined as the convex hull of 8 vertices,
A 3-dimensional face of a Polyhedron in ZZ^4 defined as the convex hull of 8 vertices,
A 3-dimensional face of a Polyhedron in ZZ^4 defined as the convex hull of 8 vertices,
A 3-dimensional face of a Polyhedron in ZZ^4 defined as the convex hull of 8 vertices,
A 3-dimensional face of a Polyhedron in ZZ^4 defined as the convex hull of 8 vertices,
A 3-dimensional face of a Polyhedron in ZZ^4 defined as the convex hull of 8 vertices,
A 3-dimensional face of a Polyhedron in ZZ^4 defined as the convex hull of 8 vertices)
This is the same result as explicitly finding the
three-dimensional faces::
sage: dim = p.dimension()
sage: p.faces(dim-1)
(A 3-dimensional face of a Polyhedron in ZZ^4 defined as the convex hull of 8 vertices,
A 3-dimensional face of a Polyhedron in ZZ^4 defined as the convex hull of 8 vertices,
A 3-dimensional face of a Polyhedron in ZZ^4 defined as the convex hull of 8 vertices,
A 3-dimensional face of a Polyhedron in ZZ^4 defined as the convex hull of 8 vertices,
A 3-dimensional face of a Polyhedron in ZZ^4 defined as the convex hull of 8 vertices,
A 3-dimensional face of a Polyhedron in ZZ^4 defined as the convex hull of 8 vertices,
A 3-dimensional face of a Polyhedron in ZZ^4 defined as the convex hull of 8 vertices,
A 3-dimensional face of a Polyhedron in ZZ^4 defined as the convex hull of 8 vertices)
The ``0``-dimensional polyhedron does not have facets::
sage: P = Polyhedron([[0]])
sage: P.facets()
()
"""
if self.dimension() == 0:
return ()
return self.faces(self.dimension()-1)
@cached_method(do_pickle=True)
def f_vector(self, num_threads=None, parallelization_depth=None):
r"""
Return the f-vector.
INPUT:
- ``num_threads`` -- integer (optional); specify the number of threads;
otherwise determined by :func:`~sage.parallel.ncpus.ncpus`
- ``parallelization_depth`` -- integer (optional); specify
how deep in the lattice the parallelization is done
OUTPUT:
Return a vector whose `i`-th entry is the number of
`i-2`-dimensional faces of the polytope.
.. NOTE::
The ``vertices`` as given by :meth:`Polyhedron_base.vertices`
do not need to correspond to `0`-dimensional faces. If a polyhedron
contains `k` lines they correspond to `k`-dimensional faces.
See example below
EXAMPLES::
sage: p = Polyhedron(vertices=[[1, 2, 3], [1, 3, 2],
....: [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1], [0, 0, 0]])
sage: p.f_vector()
(1, 7, 12, 7, 1)
sage: polytopes.cyclic_polytope(4,10).f_vector()
(1, 10, 45, 70, 35, 1)
sage: polytopes.hypercube(5).f_vector()
(1, 32, 80, 80, 40, 10, 1)
Polyhedra with lines do not have `0`-faces::
sage: Polyhedron(ieqs=[[1,-1,0,0],[1,1,0,0]]).f_vector()
(1, 0, 0, 2, 1)
However, the method :meth:`Polyhedron_base.vertices` returns
two points that belong to the ``Vrepresentation``::
sage: P = Polyhedron(ieqs=[[1,-1,0],[1,1,0]])
sage: P.vertices()
(A vertex at (1, 0), A vertex at (-1, 0))
sage: P.f_vector()
(1, 0, 2, 1)
TESTS:
Check that :trac:`28828` is fixed::
sage: P.f_vector().is_immutable()
True
The cache of the f-vector is being pickled::
sage: P = polytopes.cube()
sage: P.f_vector()
(1, 8, 12, 6, 1)
sage: Q = loads(dumps(P))
sage: Q.f_vector.is_in_cache()
True
"""
return self.combinatorial_polyhedron().f_vector(num_threads, parallelization_depth)
def bounded_edges(self):
"""
Return the bounded edges (excluding rays and lines).
OUTPUT:
A generator for pairs of vertices, one pair per edge.
EXAMPLES::
sage: p = Polyhedron(vertices=[[1,0],[0,1]], rays=[[1,0],[0,1]])
sage: [ e for e in p.bounded_edges() ]
[(A vertex at (0, 1), A vertex at (1, 0))]
sage: for e in p.bounded_edges(): print(e)
(A vertex at (0, 1), A vertex at (1, 0))
"""
obj = self.Vrepresentation()
for i in range(len(obj)):
if not obj[i].is_vertex():
continue
for j in range(i+1, len(obj)):
if not obj[j].is_vertex():
continue
if self.vertex_adjacency_matrix()[i, j] == 0:
continue
yield (obj[i], obj[j])
@cached_method
def vertex_adjacency_matrix(self):
"""
Return the binary matrix of vertex adjacencies.
EXAMPLES::
sage: polytopes.simplex(4).vertex_adjacency_matrix()
[0 1 1 1 1]
[1 0 1 1 1]
[1 1 0 1 1]
[1 1 1 0 1]
[1 1 1 1 0]
The rows and columns of the vertex adjacency matrix correspond
to the :meth:`Vrepresentation` objects: vertices, rays, and
lines. The `(i,j)` matrix entry equals `1` if the `i`-th and
`j`-th V-representation object are adjacent.
Two vertices are adjacent if they are the endpoints of an
edge, that is, a one-dimensional face. For unbounded polyhedra
this clearly needs to be generalized and we define two
V-representation objects (see
:mod:`sage.geometry.polyhedron.constructor`) to be adjacent if
they together generate a one-face. There are three possible
combinations:
* Two vertices can bound a finite-length edge.
* A vertex and a ray can generate a half-infinite edge
starting at the vertex and with the direction given by the
ray.
* A vertex and a line can generate an infinite edge. The
position of the vertex on the line is arbitrary in this
case, only its transverse position matters. The direction of
the edge is given by the line generator.
For example, take the half-plane::
sage: half_plane = Polyhedron(ieqs=[(0,1,0)])
sage: half_plane.Hrepresentation()
(An inequality (1, 0) x + 0 >= 0,)
Its (non-unique) V-representation consists of a vertex, a ray,
and a line. The only edge is spanned by the vertex and the
line generator, so they are adjacent::
sage: half_plane.Vrepresentation()
(A line in the direction (0, 1), A ray in the direction (1, 0), A vertex at (0, 0))
sage: half_plane.vertex_adjacency_matrix()
[0 0 1]
[0 0 0]
[1 0 0]
In one dimension higher, that is for a half-space in 3
dimensions, there is no one-dimensional face. Hence nothing is
adjacent::
sage: Polyhedron(ieqs=[(0,1,0,0)]).vertex_adjacency_matrix()
[0 0 0 0]
[0 0 0 0]
[0 0 0 0]
[0 0 0 0]
EXAMPLES:
In a bounded polygon, every vertex has precisely two adjacent ones::
sage: P = Polyhedron(vertices=[(0, 1), (1, 0), (3, 0), (4, 1)])
sage: for v in P.Vrep_generator():
....: print("{} {}".format(P.adjacency_matrix().row(v.index()), v))
(0, 1, 0, 1) A vertex at (0, 1)
(1, 0, 1, 0) A vertex at (1, 0)
(0, 1, 0, 1) A vertex at (3, 0)
(1, 0, 1, 0) A vertex at (4, 1)
If the V-representation of the polygon contains vertices and
one ray, then each V-representation object is adjacent to two
V-representation objects::
sage: P = Polyhedron(vertices=[(0, 1), (1, 0), (3, 0), (4, 1)],
....: rays=[(0,1)])
sage: for v in P.Vrep_generator():
....: print("{} {}".format(P.adjacency_matrix().row(v.index()), v))
(0, 1, 0, 0, 1) A ray in the direction (0, 1)
(1, 0, 1, 0, 0) A vertex at (0, 1)
(0, 1, 0, 1, 0) A vertex at (1, 0)
(0, 0, 1, 0, 1) A vertex at (3, 0)
(1, 0, 0, 1, 0) A vertex at (4, 1)
If the V-representation of the polygon contains vertices and
two distinct rays, then each vertex is adjacent to two
V-representation objects (which can now be vertices or
rays). The two rays are not adjacent to each other::
sage: P = Polyhedron(vertices=[(0, 1), (1, 0), (3, 0), (4, 1)],
....: rays=[(0,1), (1,1)])
sage: for v in P.Vrep_generator():
....: print("{} {}".format(P.adjacency_matrix().row(v.index()), v))
(0, 1, 0, 0, 0) A ray in the direction (0, 1)
(1, 0, 1, 0, 0) A vertex at (0, 1)
(0, 1, 0, 0, 1) A vertex at (1, 0)
(0, 0, 0, 0, 1) A ray in the direction (1, 1)
(0, 0, 1, 1, 0) A vertex at (3, 0)
The vertex adjacency matrix has base ring integers. This way one can express various
counting questions::
sage: P = polytopes.cube()
sage: Q = P.stack(P.faces(2)[0])
sage: M = Q.vertex_adjacency_matrix()
sage: sum(M)
(4, 4, 3, 3, 4, 4, 4, 3, 3)
sage: G = Q.vertex_graph() # optional - sage.graphs
sage: G.degree() # optional - sage.graphs
[4, 4, 3, 3, 4, 4, 4, 3, 3]
TESTS:
Check that :trac:`28828` is fixed::
sage: P.adjacency_matrix().is_immutable()
True
"""
return self.combinatorial_polyhedron().vertex_adjacency_matrix()
adjacency_matrix = vertex_adjacency_matrix
@cached_method
def facet_adjacency_matrix(self):