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
/
representation.py
2018 lines (1593 loc) · 62.7 KB
/
representation.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
"""
H(yperplane) and V(ertex) representation objects for polyhedra
"""
# ****************************************************************************
# Copyright (C) 2008 Marshall Hampton <hamptonio@gmail.com>
# 2011-2012 Volker Braun <vbraun.name@gmail.com>
# 2014 André Apitzsch
# 2015 Vincent Delecroix
# 2016 Daniel Krenn
# 2016 Jeroen Demeyer
# 2017-2021 Frédéric Chapoton
# 2018-2019 Jean-Philippe Labbé
# 2019 Julian Ritter
# 2019-2022 Jonathan Kliem
# 2021 Yuan Zhou
# 2021-2022 Matthias Koeppe
#
# 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.structure.sage_object import SageObject
from sage.structure.element import is_Vector
from sage.structure.richcmp import richcmp_method, richcmp
from sage.rings.integer_ring import ZZ
from sage.modules.free_module_element import vector
from copy import copy
# Numeric values to distinguish representation types
STRICT_INEQUALITY = -1
INEQUALITY = 0
EQUATION = 1
VERTEX = 2
RAY = 3
LINE = 4
STRICT_RAY = 5
#########################################################################
# PolyhedronRepresentation
# / \
# / \
# Hrepresentation Vrepresentation
# / | \ / | | \
# / | \ / | | \
# StrictInequality Inequality Equation Vertex Ray Line StrictRay
@richcmp_method
class PolyhedronRepresentation(SageObject):
"""
The internal base class for all representation objects of
``Polyhedron`` (vertices/rays/lines and inequalities/equations)
.. note::
You should not (and cannot) instantiate it yourself. You can
only obtain them from a Polyhedron() class.
TESTS::
sage: import sage.geometry.polyhedron.representation as P
sage: P.PolyhedronRepresentation()
<sage.geometry.polyhedron.representation.PolyhedronRepresentation object at ...>
"""
# Numeric values for the output of the type() method
STRICT_INEQUALITY = STRICT_INEQUALITY
INEQUALITY = INEQUALITY
EQUATION = EQUATION
VERTEX = VERTEX
RAY = RAY
LINE = LINE
STRICT_RAY = STRICT_RAY
def __len__(self):
"""
Return the length of the representation data.
TESTS::
sage: p = Polyhedron(vertices=[[1,2,3]])
sage: v = p.Vrepresentation(0)
sage: v.__len__()
3
"""
return self._vector.degree()
def __getitem__(self, i):
"""
Supports indexing.
TESTS::
sage: p = Polyhedron(vertices=[[1,2,3]])
sage: v = p.Vrepresentation(0)
sage: v.__getitem__(1)
2
"""
return self._vector[i]
def __hash__(self):
r"""
TESTS::
sage: from sage.geometry.polyhedron.representation import Hrepresentation
sage: pr = Hrepresentation(Polyhedron(vertices = [[1,2,3]]).parent())
sage: hash(pr) == hash(tuple([0,0,0,0]))
True
"""
# TODO: ideally the argument self._vector of self should be immutable.
# So that we could change the line below by hash(self._vector). The
# mutability is kept because this argument might be reused (see e.g.
# Hrepresentation._set_data below).
return hash(tuple(self._vector))
def __richcmp__(self, other, op):
"""
Compare two representation objects
This method defines a linear order on the H/V-representation objects.
The order is first determined by the types of the objects,
such that inequality < equation < vertex < ray < line.
Then, representation objects with the same type are ordered
lexicographically according to their canonical vectors.
Thus, two representation objects are equal if and only if they define
the same vertex/ray/line or inequality/equation in the ambient space,
regardless of the polyhedron that they belong to.
INPUT:
- ``other`` -- anything.
OUTPUT:
boolean
EXAMPLES::
sage: triangle = Polyhedron([(0,0), (1,0), (0,1)])
sage: ieq = next(triangle.inequality_generator()); ieq
An inequality (1, 0) x + 0 >= 0
sage: ieq == copy(ieq)
True
sage: square = Polyhedron([(0,0), (1,0), (0,1), (1,1)], base_ring=QQ)
sage: square.Vrepresentation(0) == triangle.Vrepresentation(0)
True
sage: ieq = square.Hrepresentation(0); ieq.vector()
(0, 1, 0)
sage: ieq != Polyhedron([(0,1,0)]).Vrepresentation(0)
True
sage: H = Polyhedron(vertices=[(4,0)], rays=[(1,1)], lines=[(-1,1)])
sage: H.vertices()[0] < H.rays()[0] < H.lines()[0]
True
TESTS:
Check :trac:`30954`::
sage: P = (1/2)*polytopes.cube()
sage: Q = (1/2)*polytopes.cube(backend='field')
sage: for p in P.inequalities():
....: assert p in Q.inequalities()
"""
if not isinstance(other, PolyhedronRepresentation):
return NotImplemented
return richcmp((self.type(), self._vector*self._comparison_scalar()),
(other.type(), other._vector*other._comparison_scalar()), op)
def _comparison_scalar(self):
r"""
Return a number ``a`` such that ``a*self._vector`` is canonical.
Except for vertices, ``self._vector`` is only unique up to a positive scalar.
This is overwritten for the vertex class.
EXAMPLES::
sage: P = Polyhedron(vertices=[[0,0],[1,5]], rays=[[3,4]])
sage: P.Vrepresentation()
(A vertex at (0, 0), A vertex at (1, 5), A ray in the direction (3, 4))
sage: P.Vrepresentation()[0]._comparison_scalar()
1
sage: P.Vrepresentation()[1]._comparison_scalar()
1
sage: P.Vrepresentation()[2]._comparison_scalar()
1/4
sage: P.Hrepresentation()
(An inequality (5, -1) x + 0 >= 0,
An inequality (-4, 3) x + 0 >= 0,
An inequality (4, -3) x + 11 >= 0)
sage: P.Hrepresentation()[0]._comparison_scalar()
1
sage: P.Hrepresentation()[1]._comparison_scalar()
1/3
sage: P.Hrepresentation()[2]._comparison_scalar()
1/3
::
sage: P = Polyhedron(vertices=[[1,3]], lines=[[-1,3]])
sage: P.Vrepresentation()
(A line in the direction (1, -3), A vertex at (2, 0))
sage: P.Vrepresentation()[0]._comparison_scalar()
-1/3
sage: P.Vrepresentation()[1]._comparison_scalar()
1
"""
if self.type() == self.VERTEX:
return 1
lcf = self._vector.leading_coefficient()
if self.type() == self.EQUATION or self.type() == self.LINE:
return 1/lcf
else:
return 1/lcf.abs()
def vector(self, base_ring=None):
"""
Return the vector representation of the H/V-representation object.
INPUT:
- ``base_ring`` -- the base ring of the vector.
OUTPUT:
For a V-representation object, a vector of length
:meth:`~sage.geometry.polyhedron.base.Polyhedron_base.ambient_dim`. For
a H-representation object, a vector of length
:meth:`~sage.geometry.polyhedron.base.Polyhedron_base.ambient_dim`
+ 1.
EXAMPLES::
sage: s = polytopes.cuboctahedron()
sage: v = next(s.vertex_generator())
sage: v
A vertex at (-1, -1, 0)
sage: v.vector()
(-1, -1, 0)
sage: v()
(-1, -1, 0)
sage: type(v())
<class 'sage.modules.vector_integer_dense.Vector_integer_dense'>
Conversion to a different base ring can be forced with the optional argument::
sage: v.vector(RDF)
(-1.0, -1.0, 0.0)
sage: vector(RDF, v)
(-1.0, -1.0, 0.0)
TESTS:
Checks that :trac:`27709` is fixed::
sage: C = polytopes.cube()
sage: C.vertices()[0].vector()[0] = 3
sage: C.vertices()[0]
A vertex at (1, -1, -1)
"""
if (base_ring is None) or (base_ring is self._base_ring):
return copy(self._vector)
else:
return vector(base_ring, self._vector)
_vector_ = vector
def polyhedron(self):
"""
Return the underlying polyhedron.
TESTS::
sage: p = Polyhedron(vertices=[[1,2,3]])
sage: v = p.Vrepresentation(0)
sage: v.polyhedron()
A 0-dimensional polyhedron in ZZ^3 defined as the convex hull of 1 vertex
"""
return self._polyhedron
def __call__(self):
"""
Return the vector representation of the representation
object. Shorthand for the vector() method.
TESTS::
sage: p = Polyhedron(vertices=[[1,2,3]])
sage: v = p.Vrepresentation(0)
sage: v.__call__()
(1, 2, 3)
"""
return copy(self._vector)
def index(self):
"""
Return an arbitrary but fixed number according to the internal
storage order.
.. NOTE::
H-representation and V-representation objects are enumerated
independently. That is, amongst all vertices/rays/lines there
will be one with ``index()==0``, and amongst all
inequalities/equations there will be one with ``index()==0``,
unless the polyhedron is empty or spans the whole space.
EXAMPLES::
sage: s = Polyhedron(vertices=[[1],[-1]])
sage: first_vertex = next(s.vertex_generator())
sage: first_vertex.index()
0
sage: first_vertex == s.Vrepresentation(0)
True
"""
return self._index
def __add__(self, coordinate_list):
"""
Return the coordinates concatenated with ``coordinate_list``.
INPUT:
- ``coordinate_list`` -- a list.
OUTPUT:
The coordinates of ``self`` concatenated with ``coordinate_list``.
EXAMPLES::
sage: p = Polyhedron(ieqs = [[0,1,0],[0,0,1],[1,-1,0,],[1,0,-1]])
sage: v = p.Vrepresentation(0); v
A vertex at (1, 0)
sage: v + [4,5]
[1, 0, 4, 5]
"""
if not isinstance(coordinate_list, list):
raise TypeError('Can only concatenate with a list of coordinates')
return list(self) + coordinate_list
def __radd__(self, coordinate_list):
"""
Return ``coordinate_list`` concatenated with the coordinates.
INPUT:
- ``coordinate_list`` -- a list.
OUTPUT:
``coordinate_list`` concatenated with the coordinates of ``self``.
EXAMPLES::
sage: p = Polyhedron(ieqs = [[0,1,0],[0,0,1],[1,-1,0,],[1,0,-1]])
sage: v = p.Vrepresentation(0); v
A vertex at (1, 0)
sage: [4,5] + v
[4, 5, 1, 0]
"""
if not isinstance(coordinate_list, list):
raise TypeError('Can only concatenate with a list of coordinates')
return coordinate_list + list(self)
def count(self, i):
"""
Count the number of occurrences of ``i`` in the coordinates.
INPUT:
- ``i`` -- Anything.
OUTPUT:
Integer. The number of occurrences of ``i`` in the coordinates.
EXAMPLES::
sage: p = Polyhedron(vertices=[(0,1,1,2,1)])
sage: v = p.Vrepresentation(0); v
A vertex at (0, 1, 1, 2, 1)
sage: v.count(1)
3
"""
return sum([1 for j in self if i == j])
class Hrepresentation(PolyhedronRepresentation):
"""
The internal base class for H-representation objects of
a polyhedron. Inherits from ``PolyhedronRepresentation``.
"""
def __init__(self, polyhedron_parent):
"""
Initializes the PolyhedronRepresentation object.
TESTS::
sage: from sage.geometry.polyhedron.representation import Hrepresentation
sage: pr = Hrepresentation(Polyhedron(vertices = [[1,2,3]]).parent())
sage: tuple(pr)
(0, 0, 0, 0)
sage: TestSuite(pr).run(skip='_test_pickling')
"""
self._polyhedron_parent = polyhedron_parent
self._base_ring = polyhedron_parent.base_ring()
self._vector = polyhedron_parent.Hrepresentation_space()(0)
self._A = polyhedron_parent.ambient_space()(0)
self._b = polyhedron_parent.base_ring()(0)
self._index = 0
def _set_data(self, polyhedron, data):
"""
Initialization function.
The H/V-representation objects are kept in a pool, and this
function is used to reassign new values to already existing
(but unused) objects. You must not call this function on
objects that are in normal use.
INPUT:
- ``polyhedron`` -- the new polyhedron.
- ``data`` -- the H-representation data.
TESTS::
sage: p = Polyhedron(ieqs = [[0,1,0],[0,0,1],[1,-1,0,],[1,0,-1]])
sage: pH = p.Hrepresentation(0) # indirect doctest
sage: TestSuite(pH).run(skip='_test_pickling')
"""
assert polyhedron.parent() is self._polyhedron_parent
if len(data) != self._vector.degree():
raise ValueError('H-representation data requires a list of length ambient_dim+1')
self._vector[:] = data
self._A[:] = data[1:]
self._b = self._base_ring(data[0])
self._index = len(polyhedron._Hrepresentation)
polyhedron._Hrepresentation.append(self)
self._polyhedron = polyhedron
if polyhedron.is_mutable():
polyhedron._add_dependent_object(self)
def is_H(self):
"""
Return True if the object is part of a H-representation
(inequality or equation).
EXAMPLES::
sage: p = Polyhedron(ieqs = [[0,1,0],[0,0,1],[1,-1,0,],[1,0,-1]])
sage: pH = p.Hrepresentation(0)
sage: pH.is_H()
True
"""
return True
def is_inequality(self):
"""
Return True if the object is a (non-strict) inequality of the H-representation.
EXAMPLES::
sage: p = Polyhedron(ieqs = [[0,1,0],[0,0,1],[1,-1,0,],[1,0,-1]])
sage: pH = p.Hrepresentation(0)
sage: pH.is_inequality()
True
"""
return False
def is_equation(self):
"""
Return True if the object is an equation of the H-representation.
EXAMPLES::
sage: p = Polyhedron(ieqs = [[0,1,0],[0,0,1],[1,-1,0,],[1,0,-1]], eqns = [[1,1,-1]])
sage: pH = p.Hrepresentation(0)
sage: pH.is_equation()
True
"""
return False
def A(self):
r"""
Return the coefficient vector `A` in `A\vec{x}+b`.
EXAMPLES::
sage: p = Polyhedron(ieqs = [[0,1,0],[0,0,1],[1,-1,0,],[1,0,-1]])
sage: pH = p.Hrepresentation(2)
sage: pH.A()
(1, 0)
TESTS:
Checks that :trac:`27709` is fixed::
sage: C = polytopes.cube()
sage: C.inequalities()[0].A()[2] = 5
sage: C.inequalities()[0]
An inequality (-1, 0, 0) x + 1 >= 0
"""
return copy(self._A)
def b(self):
r"""
Return the constant `b` in `A\vec{x}+b`.
EXAMPLES::
sage: p = Polyhedron(ieqs = [[0,1,0],[0,0,1],[1,-1,0,],[1,0,-1]])
sage: pH = p.Hrepresentation(2)
sage: pH.b()
0
"""
return self._b
def neighbors(self):
"""
Iterate over the adjacent facets (i.e. inequalities).
Only defined for inequalities.
EXAMPLES::
sage: p = Polyhedron(ieqs = [[0,0,0,1],[0,0,1,0,],[0,1,0,0],
....: [1,-1,0,0],[1,0,-1,0,],[1,0,0,-1]])
sage: pH = p.Hrepresentation(0)
sage: a = list(pH.neighbors())
sage: a[0]
An inequality (0, -1, 0) x + 1 >= 0
sage: list(a[0])
[1, 0, -1, 0]
TESTS:
Checking that :trac:`28463` is fixed::
sage: P = polytopes.simplex()
sage: F1 = P.Hrepresentation()[1]
sage: list(F1.neighbors())
[An inequality (0, 1, 0, 0) x + 0 >= 0,
An inequality (0, 0, 1, 0) x + 0 >= 0,
An inequality (0, 0, 0, 1) x + 0 >= 0]
Does not work for equalities::
sage: F0 = P.Hrepresentation()[0]
sage: list(F0.neighbors())
Traceback (most recent call last):
...
TypeError: must be inequality
"""
# The adjacency matrix does not include equations.
n_eqs = self.polyhedron().n_equations()
if not self.is_inequality():
raise TypeError("must be inequality")
adjacency_matrix = self.polyhedron().facet_adjacency_matrix()
for x in self.polyhedron().Hrep_generator():
if not x.is_equation():
if adjacency_matrix[self.index()-n_eqs, x.index()-n_eqs] == 1:
yield x
def adjacent(self):
"""
Alias for neighbors().
TESTS::
sage: p = Polyhedron(ieqs = [[0,0,0,2],[0,0,1,0,],[0,10,0,0],
....: [1,-1,0,0],[1,0,-1,0,],[1,0,0,-1]])
sage: pH = p.Hrepresentation(0)
sage: a = list(pH.neighbors())
sage: b = list(pH.adjacent())
sage: a==b
True
"""
return self.neighbors()
def is_incident(self, Vobj):
"""
Return whether the incidence matrix element (Vobj,self) == 1
EXAMPLES::
sage: p = Polyhedron(ieqs = [[0,0,0,1],[0,0,1,0,],[0,1,0,0],
....: [1,-1,0,0],[1,0,-1,0,],[1,0,0,-1]])
sage: pH = p.Hrepresentation(0)
sage: pH.is_incident(p.Vrepresentation(1))
True
sage: pH.is_incident(p.Vrepresentation(5))
False
"""
return self.polyhedron().incidence_matrix()[Vobj.index(), self.index()] == 1
def __mul__(self, Vobj):
"""
Shorthand for ``self.eval(x)``
EXAMPLES::
sage: p = Polyhedron(ieqs = [[0,0,0,1],[0,0,1,0,],[0,1,0,0],
....: [1,-1,0,0],[1,0,-1,0,],[1,0,0,-1]])
sage: pH = p.Hrepresentation(0)
sage: pH*p.Vrepresentation(5)
1
"""
return self.eval(Vobj)
def eval(self, Vobj):
r"""
Evaluate the left hand side `A\vec{x}+b` on the given
vertex/ray/line.
.. NOTE:
* Evaluating on a vertex returns `A\vec{x}+b`
* Evaluating on a ray returns `A\vec{r}`. Only the sign or
whether it is zero is meaningful.
* Evaluating on a line returns `A\vec{l}`. Only whether it
is zero or not is meaningful.
EXAMPLES::
sage: triangle = Polyhedron(vertices=[[1,0],[0,1],[-1,-1]])
sage: ineq = next(triangle.inequality_generator())
sage: ineq
An inequality (2, -1) x + 1 >= 0
sage: [ ineq.eval(v) for v in triangle.vertex_generator() ]
[0, 0, 3]
sage: [ ineq * v for v in triangle.vertex_generator() ]
[0, 0, 3]
If you pass a vector, it is assumed to be the coordinate vector of a point::
sage: ineq.eval( vector(ZZ, [3,2]) )
5
"""
if is_Vector(Vobj):
return self.A() * Vobj + self.b()
return Vobj.evaluated_on(self)
def incident(self):
"""
Return a generator for the incident H-representation objects,
that is, the vertices/rays/lines satisfying the (in)equality.
EXAMPLES::
sage: triangle = Polyhedron(vertices=[[1,0],[0,1],[-1,-1]])
sage: ineq = next(triangle.inequality_generator())
sage: ineq
An inequality (2, -1) x + 1 >= 0
sage: [ v for v in ineq.incident()]
[A vertex at (-1, -1), A vertex at (0, 1)]
sage: p = Polyhedron(vertices=[[0,0,0],[0,1,0],[0,0,1]], rays=[[1,-1,-1]])
sage: ineq = p.Hrepresentation(2)
sage: ineq
An inequality (1, 0, 1) x + 0 >= 0
sage: [ x for x in ineq.incident() ]
[A vertex at (0, 0, 0),
A vertex at (0, 1, 0),
A ray in the direction (1, -1, -1)]
"""
incidence_matrix = self.polyhedron().incidence_matrix()
for V in self.polyhedron().Vrep_generator():
if incidence_matrix[V.index(), self.index()] == 1:
yield V
def repr_pretty(self, **kwds):
r"""
Return a pretty representation of this equality/inequality.
INPUT:
- ``prefix`` -- a string
- ``indices`` -- a tuple or other iterable
- ``latex`` -- a boolean
OUTPUT:
A string
EXAMPLES::
sage: P = Polyhedron(ieqs=[(0, 1, 0, 0), (1, 2, 1, 0)],
....: eqns=[(1, -1, -1, 1)])
sage: for h in P.Hrepresentation():
....: print(h.repr_pretty())
x0 + x1 - x2 == 1
x0 >= 0
2*x0 + x1 >= -1
"""
return repr_pretty(self.vector(), self.type(), **kwds)
def _latex_(self):
r"""
Return a LaTeX-representation of this equality/inequality.
OUTPUT:
A string.
EXAMPLES::
sage: P = Polyhedron(ieqs=[(0, 1, 0, 0), (1, 2, 1, 0)],
....: eqns=[(1, -1, -1, 1)])
sage: for h in P.Hrepresentation():
....: print(latex(h))
x_{0} + x_{1} - x_{2} = 1
x_{0} \geq 0
2 x_{0} + x_{1} \geq -1
"""
return self.repr_pretty(latex=True)
class StrictInequality(Hrepresentation):
"""
A strict linear inequality.
Inherits from ``Hrepresentation``.
"""
def type(self):
r"""
Return the type (strict inequality/inequality/equation/vertex/ray/line) as an
integer.
OUTPUT:
This version of the method returns ``PolyhedronRepresentation.STRICT_INEQUALITY``.
EXAMPLES::
sage: p = Polyhedron(vertices=[[0,0,0], [1,1,0], [1,2,0]])
sage: from sage.geometry.polyhedron.representation import StrictInequality
sage: repr_obj = StrictInequality(p.parent())
sage: p._Hrepresentation = list(p._Hrepresentation) # temporary workaround
sage: repr_obj._set_data(p, [-5, 2, 3, 4])
sage: repr_obj.type() == repr_obj.STRICT_INEQUALITY
True
sage: repr_obj.type() == repr_obj.INEQUALITY
False
sage: repr_obj.type() == repr_obj.EQUATION
False
sage: repr_obj.type() == repr_obj.VERTEX
False
sage: repr_obj.type() == repr_obj.RAY
False
sage: repr_obj.type() == repr_obj.LINE
False
sage: repr_obj.type() == repr_obj.STRICT_RAY
False
"""
return self.STRICT_INEQUALITY
def _repr_(self):
"""
The string representation of the inequality.
EXAMPLES::
sage: p = Polyhedron(vertices=[[0,0,0], [1,1,0], [1,2,0]])
sage: from sage.geometry.polyhedron.representation import StrictInequality
sage: repr_obj = StrictInequality(p.parent())
sage: p._Hrepresentation = list(p._Hrepresentation) # temporary workaround
sage: repr_obj._set_data(p, [-5, 2, 3, 4])
sage: repr_obj._repr_()
'A strict inequality (2, 3, 4) x - 5 > 0'
"""
s = 'A strict inequality '
have_A = not self.A().is_zero()
if have_A:
s += repr(self.A()) + ' x '
if self.b() >= 0:
if have_A:
s += '+'
else:
s += '-'
if have_A:
s += ' '
s += repr(abs(self.b())) + ' > 0'
return s
def contains(self, Vobj):
"""
Tests whether the open halfspace defined by the strict inequality contains the given vertex or point.
EXAMPLES::
sage: p = Polyhedron(vertices = [[0,0,0], [1,1,0], [1,2,0]])
sage: from sage.geometry.polyhedron.representation import StrictInequality
sage: repr_obj = StrictInequality(p.parent())
sage: p._Hrepresentation = list(p._Hrepresentation) # temporary workaround
sage: repr_obj._set_data(p, [-5, 2, 3, 4])
sage: [repr_obj.contains(q) for q in p.vertex_generator()]
[False, False, True]
"""
try:
if Vobj.is_vector(): # assume we were passed a point
return self.polyhedron()._is_positive( self.eval(Vobj) )
except AttributeError:
pass
if Vobj.is_vertex():
return self.polyhedron()._is_positive( self.eval(Vobj) )
raise NotImplementedError
interior_contains = contains
def outer_normal(self):
r"""
Return the outer normal vector of ``self``.
OUTPUT:
The normal vector directed away from the interior of the polyhedron.
EXAMPLES::
sage: p = Polyhedron(vertices = [[0,0,0], [1,1,0], [1,2,0]])
sage: from sage.geometry.polyhedron.representation import StrictInequality
sage: repr_obj = StrictInequality(p.parent())
sage: p._Hrepresentation = list(p._Hrepresentation) # temporary workaround
sage: repr_obj._set_data(p, [-5, 2, 3, 4])
sage: repr_obj.outer_normal()
(-2, -3, -4)
"""
return -self.A()
class Inequality(Hrepresentation):
"""
A (non-strict) linear inequality (defining a closed halfspace).
Inherits from ``Hrepresentation``.
"""
def type(self):
r"""
Return the type (equation/inequality/vertex/ray/line) as an
integer.
OUTPUT:
Integer. One of ``PolyhedronRepresentation.INEQUALITY``,
``.EQUATION``, ``.VERTEX``, ``.RAY``, or ``.LINE``.
EXAMPLES::
sage: p = Polyhedron(vertices = [[0,0,0],[1,1,0],[1,2,0]])
sage: repr_obj = next(p.inequality_generator())
sage: repr_obj.type()
0
sage: repr_obj.type() == repr_obj.STRICT_INEQUALITY
False
sage: repr_obj.type() == repr_obj.INEQUALITY
True
sage: repr_obj.type() == repr_obj.EQUATION
False
sage: repr_obj.type() == repr_obj.VERTEX
False
sage: repr_obj.type() == repr_obj.RAY
False
sage: repr_obj.type() == repr_obj.LINE
False
sage: repr_obj.type() == repr_obj.STRICT_RAY
False
"""
return self.INEQUALITY
def is_inequality(self):
"""
Return True since this is, by construction, an inequality.
EXAMPLES::
sage: p = Polyhedron(vertices = [[0,0,0],[1,1,0],[1,2,0]])
sage: a = next(p.inequality_generator())
sage: a.is_inequality()
True
"""
return True
def is_facet_defining_inequality(self, other):
r"""
Check if ``self`` defines a facet of ``other``.
INPUT:
- ``other`` -- a polyhedron
.. SEEALSO::
:meth:`~sage.geometry.polyhedron.base.Polyhedron_base.slack_matrix`
:meth:`~sage.geometry.polyhedron.base.Polyhedron_base.incidence_matrix`
EXAMPLES::
sage: P = Polyhedron(vertices=[[0,0,0],[0,1,0]], rays=[[1,0,0]])
sage: P.inequalities()
(An inequality (1, 0, 0) x + 0 >= 0,
An inequality (0, 1, 0) x + 0 >= 0,
An inequality (0, -1, 0) x + 1 >= 0)
sage: Q = Polyhedron(ieqs=[[0,1,0,0]])
sage: Q.inequalities()[0].is_facet_defining_inequality(P)
True
sage: Q = Polyhedron(ieqs=[[0,2,0,3]])
sage: Q.inequalities()[0].is_facet_defining_inequality(P)
True
sage: Q = Polyhedron(ieqs=[[0,AA(2).sqrt(),0,3]]) # optional - sage.rings.number_field
sage: Q.inequalities()[0].is_facet_defining_inequality(P) # optional - sage.rings.number_field
True
sage: Q = Polyhedron(ieqs=[[1,1,0,0]])
sage: Q.inequalities()[0].is_facet_defining_inequality(P)
False
::
sage: P = Polyhedron(vertices=[[0,0,0],[0,1,0]], lines=[[1,0,0]])
sage: P.inequalities()
(An inequality (0, 1, 0) x + 0 >= 0, An inequality (0, -1, 0) x + 1 >= 0)
sage: Q = Polyhedron(ieqs=[[0,1,0,0]])
sage: Q.inequalities()[0].is_facet_defining_inequality(P)
False
sage: Q = Polyhedron(ieqs=[[0,-1,0,0]])
sage: Q.inequalities()[0].is_facet_defining_inequality(P)
False
sage: Q = Polyhedron(ieqs=[[0,0,1,3]])
sage: Q.inequalities()[0].is_facet_defining_inequality(P)
True
TESTS::
sage: p1 = Polyhedron(backend='normaliz', base_ring=QQ, vertices=[ # optional - pynormaliz
....: (2/3, 2/3, 2/3, 2/3, 2/3, 2/3, 2/3, 2/3, 2/3),
....: (1, 1, 1, 9/10, 4/5, 7/10, 3/5, 0, 0),
....: (1, 1, 1, 1, 4/5, 3/5, 1/2, 1/10, 0),
....: (1, 1, 1, 1, 9/10, 1/2, 2/5, 1/5, 0),
....: (1, 1, 1, 1, 1, 2/5, 3/10, 1/5, 1/10)])
sage: p2 = Polyhedron(backend='ppl', base_ring=QQ, vertices=[
....: (2/3, 2/3, 2/3, 2/3, 2/3, 2/3, 2/3, 2/3, 2/3),
....: (1, 1, 1, 9/10, 4/5, 7/10, 3/5, 0, 0),
....: (1, 1, 1, 1, 4/5, 3/5, 1/2, 1/10, 0),
....: (1, 1, 1, 1, 9/10, 1/2, 2/5, 1/5, 0),
....: (1, 1, 1, 1, 1, 2/5, 3/10, 1/5, 1/10)])
sage: p2 == p1 # optional - pynormaliz
True
sage: for ieq in p1.inequalities(): # optional - pynormaliz
....: assert ieq.is_facet_defining_inequality(p2)
sage: for ieq in p2.inequalities(): # optional - pynormaliz
....: assert ieq.is_facet_defining_inequality(p1)
"""
from sage.geometry.polyhedron.base import Polyhedron_base
if not isinstance(other, Polyhedron_base):
raise ValueError("other must be a polyhedron")
if not other.n_Vrepresentation():
# An empty polytope does not have facets.
return False
# We evaluate ``self`` on the Vrepresentation of other.
from sage.matrix.constructor import matrix
Vrep_matrix = matrix(other.base_ring(), other.Vrepresentation())
# Getting homogeneous coordinates of the Vrepresentation.
hom_helper = matrix(other.base_ring(), [1 if v.is_vertex() else 0 for v in other.Vrepresentation()])
hom_Vrep = hom_helper.stack(Vrep_matrix.transpose())
self_matrix = matrix(self.vector())
cross_slack_matrix = self_matrix * hom_Vrep