Path: blob/master/sage/geometry/polyhedron/backend_ppl.py
4096 views
"""1The PPL (Parma Polyhedra Library) backend for polyhedral computations2"""34from sage.rings.all import ZZ, QQ5from sage.rings.arith import lcm6from sage.misc.functional import denominator7from sage.matrix.constructor import matrix8from sage.libs.ppl import (9C_Polyhedron, Constraint_System, Generator_System,10Variable, Linear_Expression,11line, ray, point )1213from base_QQ import Polyhedron_QQ14from representation import (15PolyhedronRepresentation,16Hrepresentation,17Inequality, Equation,18Vrepresentation,19Vertex, Ray, Line )20212223#########################################################################24class Polyhedron_QQ_ppl(Polyhedron_QQ):25"""26Polyhedra over `\QQ` with ppl2728INPUT:2930- ``ambient_dim`` -- integer. The dimension of the ambient space.3132- ``Vrep`` -- a list ``[vertices, rays, lines]``.3334- ``Hrep`` -- a list ``[ieqs, eqns]``.3536EXAMPLES::3738sage: p = Polyhedron(vertices=[(0,0),(1,0),(0,1)], rays=[(1,1)], lines=[], backend='ppl')39sage: TestSuite(p).run()40"""4142def _init_from_Vrepresentation(self, ambient_dim, vertices, rays, lines, minimize=True):43"""44Construct polyhedron from V-representation data.4546INPUT:4748- ``ambient_dim`` -- integer. The dimension of the ambient space.4950- ``vertices`` -- list of point. Each point can be specified51as any iterable container of52:meth:`~sage.geometry.polyhedron.base.base_ring` elements.5354- ``rays`` -- list of rays. Each ray can be specified as any55iterable container of56:meth:`~sage.geometry.polyhedron.base.base_ring` elements.5758- ``lines`` -- list of lines. Each line can be specified as59any iterable container of60:meth:`~sage.geometry.polyhedron.base.base_ring` elements.6162EXAMPLES::6364sage: p = Polyhedron(backend='ppl')65sage: from sage.geometry.polyhedron.backend_ppl import Polyhedron_QQ_ppl66sage: Polyhedron_QQ_ppl._init_from_Vrepresentation(p, 2, [], [], [])67"""68gs = Generator_System()69if vertices is None: vertices = []70for v in vertices:71d = lcm([denominator(v_i) for v_i in v])72dv = [ ZZ(d*v_i) for v_i in v ]73gs.insert(point(Linear_Expression(dv, 0), d))74if rays is None: rays = []75for r in rays:76d = lcm([denominator(r_i) for r_i in r])77dr = [ ZZ(d*r_i) for r_i in r ]78gs.insert(ray(Linear_Expression(dr, 0)))79if lines is None: lines = []80for l in lines:81d = lcm([denominator(l_i) for l_i in l])82dl = [ ZZ(d*l_i) for l_i in l ]83gs.insert(line(Linear_Expression(dl, 0)))84self._ppl_polyhedron = C_Polyhedron(gs)85self._init_Vrepresentation_from_ppl(minimize)86self._init_Hrepresentation_from_ppl(minimize)878889def _init_from_Hrepresentation(self, ambient_dim, ieqs, eqns, minimize=True):90"""91Construct polyhedron from H-representation data.9293INPUT:9495- ``ambient_dim`` -- integer. The dimension of the ambient space.9697- ``ieqs`` -- list of inequalities. Each line can be specified98as any iterable container of99:meth:`~sage.geometry.polyhedron.base.base_ring` elements.100101- ``eqns`` -- list of equalities. Each line can be specified102as any iterable container of103:meth:`~sage.geometry.polyhedron.base.base_ring` elements.104105EXAMPLES::106107sage: p = Polyhedron(backend='ppl')108sage: from sage.geometry.polyhedron.backend_ppl import Polyhedron_QQ_ppl109sage: Polyhedron_QQ_ppl._init_from_Hrepresentation(p, 2, [], [])110"""111cs = Constraint_System()112if ieqs is None: ieqs = []113for ieq in ieqs:114d = lcm([denominator(ieq_i) for ieq_i in ieq])115dieq = [ ZZ(d*ieq_i) for ieq_i in ieq ]116b = dieq[0]117A = dieq[1:]118cs.insert(Linear_Expression(A, b) >= 0)119if eqns is None: eqns = []120for eqn in eqns:121d = lcm([denominator(eqn_i) for eqn_i in eqn])122deqn = [ ZZ(d*eqn_i) for eqn_i in eqn ]123b = deqn[0]124A = deqn[1:]125cs.insert(Linear_Expression(A, b) == 0)126self._ppl_polyhedron = C_Polyhedron(cs)127self._init_Vrepresentation_from_ppl(minimize)128self._init_Hrepresentation_from_ppl(minimize)129130131def _init_Vrepresentation_from_ppl(self, minimize):132"""133Create the Vrepresentation objects from the ppl polyhedron.134135EXAMPLES::136137sage: p = Polyhedron(vertices=[(0,1/2),(2,0),(4,5/6)],138... backend='ppl') # indirect doctest139sage: p.Hrepresentation()140(An inequality (1, 4) x - 2 >= 0,141An inequality (1, -12) x + 6 >= 0,142An inequality (-5, 12) x + 10 >= 0)143sage: p._ppl_polyhedron.minimized_constraints()144Constraint_System {x0+4*x1-2>=0, x0-12*x1+6>=0, -5*x0+12*x1+10>=0}145sage: p.Vrepresentation()146(A vertex at (0, 1/2), A vertex at (2, 0), A vertex at (4, 5/6))147sage: p._ppl_polyhedron.minimized_generators()148Generator_System {point(0/2, 1/2), point(2/1, 0/1), point(24/6, 5/6)}149"""150self._Vrepresentation = []151gs = self._ppl_polyhedron.minimized_generators()152for g in gs:153if g.is_point():154d = g.divisor()155Vertex(self, [x/d for x in g.coefficients()])156elif g.is_ray():157Ray(self, g.coefficients())158elif g.is_line():159Line(self, g.coefficients())160else:161assert False162self._Vrepresentation = tuple(self._Vrepresentation)163164165def _init_Hrepresentation_from_ppl(self, minimize):166"""167Create the Vrepresentation objects from the ppl polyhedron.168169EXAMPLES::170171sage: p = Polyhedron(vertices=[(0,1/2),(2,0),(4,5/6)],172... backend='ppl') # indirect doctest173sage: p.Hrepresentation()174(An inequality (1, 4) x - 2 >= 0,175An inequality (1, -12) x + 6 >= 0,176An inequality (-5, 12) x + 10 >= 0)177sage: p._ppl_polyhedron.minimized_constraints()178Constraint_System {x0+4*x1-2>=0, x0-12*x1+6>=0, -5*x0+12*x1+10>=0}179sage: p.Vrepresentation()180(A vertex at (0, 1/2), A vertex at (2, 0), A vertex at (4, 5/6))181sage: p._ppl_polyhedron.minimized_generators()182Generator_System {point(0/2, 1/2), point(2/1, 0/1), point(24/6, 5/6)}183"""184self._Hrepresentation = []185cs = self._ppl_polyhedron.minimized_constraints()186for c in cs:187if c.is_inequality():188Inequality(self, (c.inhomogeneous_term(),) + c.coefficients())189elif c.is_equality():190Equation(self, (c.inhomogeneous_term(),) + c.coefficients())191self._Hrepresentation = tuple(self._Hrepresentation)192193194def _init_empty_polyhedron(self, ambient_dim):195"""196Initializes an empty polyhedron.197198INPUT:199200- ``ambient_dim`` -- integer. The dimension of the ambient space.201202TESTS::203204sage: empty = Polyhedron(backend='ppl'); empty205The empty polyhedron in QQ^0206sage: empty.Vrepresentation()207()208sage: empty.Hrepresentation()209(An equation -1 == 0,)210sage: Polyhedron(vertices = [], backend='ppl')211The empty polyhedron in QQ^0212sage: Polyhedron(backend='ppl')._init_empty_polyhedron(0)213"""214super(Polyhedron_QQ_ppl, self)._init_empty_polyhedron(ambient_dim)215self._ppl_polyhedron = C_Polyhedron(ambient_dim, 'empty')216217218219220