Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/geometry/polyhedron/backend_ppl.py
4096 views
1
"""
2
The PPL (Parma Polyhedra Library) backend for polyhedral computations
3
"""
4
5
from sage.rings.all import ZZ, QQ
6
from sage.rings.arith import lcm
7
from sage.misc.functional import denominator
8
from sage.matrix.constructor import matrix
9
from sage.libs.ppl import (
10
C_Polyhedron, Constraint_System, Generator_System,
11
Variable, Linear_Expression,
12
line, ray, point )
13
14
from base_QQ import Polyhedron_QQ
15
from representation import (
16
PolyhedronRepresentation,
17
Hrepresentation,
18
Inequality, Equation,
19
Vrepresentation,
20
Vertex, Ray, Line )
21
22
23
24
#########################################################################
25
class Polyhedron_QQ_ppl(Polyhedron_QQ):
26
"""
27
Polyhedra over `\QQ` with ppl
28
29
INPUT:
30
31
- ``ambient_dim`` -- integer. The dimension of the ambient space.
32
33
- ``Vrep`` -- a list ``[vertices, rays, lines]``.
34
35
- ``Hrep`` -- a list ``[ieqs, eqns]``.
36
37
EXAMPLES::
38
39
sage: p = Polyhedron(vertices=[(0,0),(1,0),(0,1)], rays=[(1,1)], lines=[], backend='ppl')
40
sage: TestSuite(p).run()
41
"""
42
43
def _init_from_Vrepresentation(self, ambient_dim, vertices, rays, lines, minimize=True):
44
"""
45
Construct polyhedron from V-representation data.
46
47
INPUT:
48
49
- ``ambient_dim`` -- integer. The dimension of the ambient space.
50
51
- ``vertices`` -- list of point. Each point can be specified
52
as any iterable container of
53
:meth:`~sage.geometry.polyhedron.base.base_ring` elements.
54
55
- ``rays`` -- list of rays. Each ray can be specified as any
56
iterable container of
57
:meth:`~sage.geometry.polyhedron.base.base_ring` elements.
58
59
- ``lines`` -- list of lines. Each line can be specified as
60
any iterable container of
61
:meth:`~sage.geometry.polyhedron.base.base_ring` elements.
62
63
EXAMPLES::
64
65
sage: p = Polyhedron(backend='ppl')
66
sage: from sage.geometry.polyhedron.backend_ppl import Polyhedron_QQ_ppl
67
sage: Polyhedron_QQ_ppl._init_from_Vrepresentation(p, 2, [], [], [])
68
"""
69
gs = Generator_System()
70
if vertices is None: vertices = []
71
for v in vertices:
72
d = lcm([denominator(v_i) for v_i in v])
73
dv = [ ZZ(d*v_i) for v_i in v ]
74
gs.insert(point(Linear_Expression(dv, 0), d))
75
if rays is None: rays = []
76
for r in rays:
77
d = lcm([denominator(r_i) for r_i in r])
78
dr = [ ZZ(d*r_i) for r_i in r ]
79
gs.insert(ray(Linear_Expression(dr, 0)))
80
if lines is None: lines = []
81
for l in lines:
82
d = lcm([denominator(l_i) for l_i in l])
83
dl = [ ZZ(d*l_i) for l_i in l ]
84
gs.insert(line(Linear_Expression(dl, 0)))
85
self._ppl_polyhedron = C_Polyhedron(gs)
86
self._init_Vrepresentation_from_ppl(minimize)
87
self._init_Hrepresentation_from_ppl(minimize)
88
89
90
def _init_from_Hrepresentation(self, ambient_dim, ieqs, eqns, minimize=True):
91
"""
92
Construct polyhedron from H-representation data.
93
94
INPUT:
95
96
- ``ambient_dim`` -- integer. The dimension of the ambient space.
97
98
- ``ieqs`` -- list of inequalities. Each line can be specified
99
as any iterable container of
100
:meth:`~sage.geometry.polyhedron.base.base_ring` elements.
101
102
- ``eqns`` -- list of equalities. Each line can be specified
103
as any iterable container of
104
:meth:`~sage.geometry.polyhedron.base.base_ring` elements.
105
106
EXAMPLES::
107
108
sage: p = Polyhedron(backend='ppl')
109
sage: from sage.geometry.polyhedron.backend_ppl import Polyhedron_QQ_ppl
110
sage: Polyhedron_QQ_ppl._init_from_Hrepresentation(p, 2, [], [])
111
"""
112
cs = Constraint_System()
113
if ieqs is None: ieqs = []
114
for ieq in ieqs:
115
d = lcm([denominator(ieq_i) for ieq_i in ieq])
116
dieq = [ ZZ(d*ieq_i) for ieq_i in ieq ]
117
b = dieq[0]
118
A = dieq[1:]
119
cs.insert(Linear_Expression(A, b) >= 0)
120
if eqns is None: eqns = []
121
for eqn in eqns:
122
d = lcm([denominator(eqn_i) for eqn_i in eqn])
123
deqn = [ ZZ(d*eqn_i) for eqn_i in eqn ]
124
b = deqn[0]
125
A = deqn[1:]
126
cs.insert(Linear_Expression(A, b) == 0)
127
self._ppl_polyhedron = C_Polyhedron(cs)
128
self._init_Vrepresentation_from_ppl(minimize)
129
self._init_Hrepresentation_from_ppl(minimize)
130
131
132
def _init_Vrepresentation_from_ppl(self, minimize):
133
"""
134
Create the Vrepresentation objects from the ppl polyhedron.
135
136
EXAMPLES::
137
138
sage: p = Polyhedron(vertices=[(0,1/2),(2,0),(4,5/6)],
139
... backend='ppl') # indirect doctest
140
sage: p.Hrepresentation()
141
(An inequality (1, 4) x - 2 >= 0,
142
An inequality (1, -12) x + 6 >= 0,
143
An inequality (-5, 12) x + 10 >= 0)
144
sage: p._ppl_polyhedron.minimized_constraints()
145
Constraint_System {x0+4*x1-2>=0, x0-12*x1+6>=0, -5*x0+12*x1+10>=0}
146
sage: p.Vrepresentation()
147
(A vertex at (0, 1/2), A vertex at (2, 0), A vertex at (4, 5/6))
148
sage: p._ppl_polyhedron.minimized_generators()
149
Generator_System {point(0/2, 1/2), point(2/1, 0/1), point(24/6, 5/6)}
150
"""
151
self._Vrepresentation = []
152
gs = self._ppl_polyhedron.minimized_generators()
153
for g in gs:
154
if g.is_point():
155
d = g.divisor()
156
Vertex(self, [x/d for x in g.coefficients()])
157
elif g.is_ray():
158
Ray(self, g.coefficients())
159
elif g.is_line():
160
Line(self, g.coefficients())
161
else:
162
assert False
163
self._Vrepresentation = tuple(self._Vrepresentation)
164
165
166
def _init_Hrepresentation_from_ppl(self, minimize):
167
"""
168
Create the Vrepresentation objects from the ppl polyhedron.
169
170
EXAMPLES::
171
172
sage: p = Polyhedron(vertices=[(0,1/2),(2,0),(4,5/6)],
173
... backend='ppl') # indirect doctest
174
sage: p.Hrepresentation()
175
(An inequality (1, 4) x - 2 >= 0,
176
An inequality (1, -12) x + 6 >= 0,
177
An inequality (-5, 12) x + 10 >= 0)
178
sage: p._ppl_polyhedron.minimized_constraints()
179
Constraint_System {x0+4*x1-2>=0, x0-12*x1+6>=0, -5*x0+12*x1+10>=0}
180
sage: p.Vrepresentation()
181
(A vertex at (0, 1/2), A vertex at (2, 0), A vertex at (4, 5/6))
182
sage: p._ppl_polyhedron.minimized_generators()
183
Generator_System {point(0/2, 1/2), point(2/1, 0/1), point(24/6, 5/6)}
184
"""
185
self._Hrepresentation = []
186
cs = self._ppl_polyhedron.minimized_constraints()
187
for c in cs:
188
if c.is_inequality():
189
Inequality(self, (c.inhomogeneous_term(),) + c.coefficients())
190
elif c.is_equality():
191
Equation(self, (c.inhomogeneous_term(),) + c.coefficients())
192
self._Hrepresentation = tuple(self._Hrepresentation)
193
194
195
def _init_empty_polyhedron(self, ambient_dim):
196
"""
197
Initializes an empty polyhedron.
198
199
INPUT:
200
201
- ``ambient_dim`` -- integer. The dimension of the ambient space.
202
203
TESTS::
204
205
sage: empty = Polyhedron(backend='ppl'); empty
206
The empty polyhedron in QQ^0
207
sage: empty.Vrepresentation()
208
()
209
sage: empty.Hrepresentation()
210
(An equation -1 == 0,)
211
sage: Polyhedron(vertices = [], backend='ppl')
212
The empty polyhedron in QQ^0
213
sage: Polyhedron(backend='ppl')._init_empty_polyhedron(0)
214
"""
215
super(Polyhedron_QQ_ppl, self)._init_empty_polyhedron(ambient_dim)
216
self._ppl_polyhedron = C_Polyhedron(ambient_dim, 'empty')
217
218
219
220