Path: blob/master/src/sage/geometry/polyhedron/backend_ppl.py
8817 views
"""1The PPL (Parma Polyhedra Library) backend for polyhedral computations2"""34from sage.rings.all import ZZ, QQ5from sage.rings.integer import LCM_list6from 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 import Polyhedron_base14from base_QQ import Polyhedron_QQ15from base_ZZ import Polyhedron_ZZ161718#########################################################################19class Polyhedron_ppl(Polyhedron_base):20"""21Polyhedra with ppl2223INPUT:2425- ``Vrep`` -- a list ``[vertices, rays, lines]`` or ``None``.2627- ``Hrep`` -- a list ``[ieqs, eqns]`` or ``None``.2829EXAMPLES::3031sage: p = Polyhedron(vertices=[(0,0),(1,0),(0,1)], rays=[(1,1)], lines=[], backend='ppl')32sage: TestSuite(p).run()33"""3435def _init_from_Vrepresentation(self, vertices, rays, lines, minimize=True, verbose=False):36"""37Construct polyhedron from V-representation data.3839INPUT:4041- ``vertices`` -- list of point. Each point can be specified42as any iterable container of43:meth:`~sage.geometry.polyhedron.base.base_ring` elements.4445- ``rays`` -- list of rays. Each ray can be specified as any46iterable container of47:meth:`~sage.geometry.polyhedron.base.base_ring` elements.4849- ``lines`` -- list of lines. Each line can be specified as50any iterable container of51:meth:`~sage.geometry.polyhedron.base.base_ring` elements.5253- ``verbose`` -- boolean (default: ``False``). Whether to print54verbose output for debugging purposes.5556EXAMPLES::5758sage: p = Polyhedron(backend='ppl')59sage: from sage.geometry.polyhedron.backend_ppl import Polyhedron_ppl60sage: Polyhedron_ppl._init_from_Vrepresentation(p, [], [], [])61"""62gs = Generator_System()63if vertices is None: vertices = []64for v in vertices:65d = LCM_list([denominator(v_i) for v_i in v])66if d.is_one():67gs.insert(point(Linear_Expression(v, 0)))68else:69dv = [ d*v_i for v_i in v ]70gs.insert(point(Linear_Expression(dv, 0), d))71if rays is None: rays = []72for r in rays:73d = LCM_list([denominator(r_i) for r_i in r])74if d.is_one():75gs.insert(ray(Linear_Expression(r, 0)))76else:77dr = [ 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_list([denominator(l_i) for l_i in l])82if d.is_one():83gs.insert(line(Linear_Expression(l, 0)))84else:85dl = [ d*l_i for l_i in l ]86gs.insert(line(Linear_Expression(dl, 0)))87if gs.empty():88self._ppl_polyhedron = C_Polyhedron(self.ambient_dim(), 'empty')89else:90self._ppl_polyhedron = C_Polyhedron(gs)91self._init_Vrepresentation_from_ppl(minimize)92self._init_Hrepresentation_from_ppl(minimize)9394def _init_from_Hrepresentation(self, ieqs, eqns, minimize=True, verbose=False):95"""96Construct polyhedron from H-representation data.9798INPUT:99100- ``ieqs`` -- list of inequalities. Each line can be specified101as any iterable container of102:meth:`~sage.geometry.polyhedron.base.base_ring` elements.103104- ``eqns`` -- list of equalities. Each line can be specified105as any iterable container of106:meth:`~sage.geometry.polyhedron.base.base_ring` elements.107108- ``verbose`` -- boolean (default: ``False``). Whether to print109verbose output for debugging purposes.110111EXAMPLES::112113sage: p = Polyhedron(backend='ppl')114sage: from sage.geometry.polyhedron.backend_ppl import Polyhedron_ppl115sage: Polyhedron_ppl._init_from_Hrepresentation(p, [], [])116"""117cs = Constraint_System()118if ieqs is None: ieqs = []119for ieq in ieqs:120d = LCM_list([denominator(ieq_i) for ieq_i in ieq])121dieq = [ ZZ(d*ieq_i) for ieq_i in ieq ]122b = dieq[0]123A = dieq[1:]124cs.insert(Linear_Expression(A, b) >= 0)125if eqns is None: eqns = []126for eqn in eqns:127d = LCM_list([denominator(eqn_i) for eqn_i in eqn])128deqn = [ ZZ(d*eqn_i) for eqn_i in eqn ]129b = deqn[0]130A = deqn[1:]131cs.insert(Linear_Expression(A, b) == 0)132if cs.empty():133self._ppl_polyhedron = C_Polyhedron(self.ambient_dim(), 'universe')134else:135self._ppl_polyhedron = C_Polyhedron(cs)136self._init_Vrepresentation_from_ppl(minimize)137self._init_Hrepresentation_from_ppl(minimize)138139def _init_Vrepresentation_from_ppl(self, minimize):140"""141Create the Vrepresentation objects from the ppl polyhedron.142143EXAMPLES::144145sage: p = Polyhedron(vertices=[(0,1/2),(2,0),(4,5/6)],146... backend='ppl') # indirect doctest147sage: p.Hrepresentation()148(An inequality (1, 4) x - 2 >= 0,149An inequality (1, -12) x + 6 >= 0,150An inequality (-5, 12) x + 10 >= 0)151sage: p._ppl_polyhedron.minimized_constraints()152Constraint_System {x0+4*x1-2>=0, x0-12*x1+6>=0, -5*x0+12*x1+10>=0}153sage: p.Vrepresentation()154(A vertex at (0, 1/2), A vertex at (2, 0), A vertex at (4, 5/6))155sage: p._ppl_polyhedron.minimized_generators()156Generator_System {point(0/2, 1/2), point(2/1, 0/1), point(24/6, 5/6)}157"""158self._Vrepresentation = []159gs = self._ppl_polyhedron.minimized_generators()160parent = self.parent()161for g in gs:162if g.is_point():163d = g.divisor()164if d.is_one():165parent._make_Vertex(self, g.coefficients())166else:167parent._make_Vertex(self, [x/d for x in g.coefficients()])168elif g.is_ray():169parent._make_Ray(self, g.coefficients())170elif g.is_line():171parent._make_Line(self, g.coefficients())172else:173assert False174self._Vrepresentation = tuple(self._Vrepresentation)175176def _init_Hrepresentation_from_ppl(self, minimize):177"""178Create the Hrepresentation objects from the ppl polyhedron.179180EXAMPLES::181182sage: p = Polyhedron(vertices=[(0,1/2),(2,0),(4,5/6)],183... backend='ppl') # indirect doctest184sage: p.Hrepresentation()185(An inequality (1, 4) x - 2 >= 0,186An inequality (1, -12) x + 6 >= 0,187An inequality (-5, 12) x + 10 >= 0)188sage: p._ppl_polyhedron.minimized_constraints()189Constraint_System {x0+4*x1-2>=0, x0-12*x1+6>=0, -5*x0+12*x1+10>=0}190sage: p.Vrepresentation()191(A vertex at (0, 1/2), A vertex at (2, 0), A vertex at (4, 5/6))192sage: p._ppl_polyhedron.minimized_generators()193Generator_System {point(0/2, 1/2), point(2/1, 0/1), point(24/6, 5/6)}194"""195self._Hrepresentation = []196cs = self._ppl_polyhedron.minimized_constraints()197parent = self.parent()198for c in cs:199if c.is_inequality():200parent._make_Inequality(self, (c.inhomogeneous_term(),) + c.coefficients())201elif c.is_equality():202parent._make_Equation(self, (c.inhomogeneous_term(),) + c.coefficients())203self._Hrepresentation = tuple(self._Hrepresentation)204205def _init_empty_polyhedron(self):206"""207Initializes an empty polyhedron.208209TESTS::210211sage: empty = Polyhedron(backend='ppl'); empty212The empty polyhedron in ZZ^0213sage: empty.Vrepresentation()214()215sage: empty.Hrepresentation()216(An equation -1 == 0,)217sage: Polyhedron(vertices = [], backend='ppl')218The empty polyhedron in ZZ^0219sage: Polyhedron(backend='ppl')._init_empty_polyhedron()220"""221super(Polyhedron_ppl, self)._init_empty_polyhedron()222self._ppl_polyhedron = C_Polyhedron(self.ambient_dim(), 'empty')223224225226227#########################################################################228class Polyhedron_QQ_ppl(Polyhedron_ppl, Polyhedron_QQ):229"""230Polyhedra over `\QQ` with ppl231232INPUT:233234- ``Vrep`` -- a list ``[vertices, rays, lines]`` or ``None``.235236- ``Hrep`` -- a list ``[ieqs, eqns]`` or ``None``.237238EXAMPLES::239240sage: p = Polyhedron(vertices=[(0,0),(1,0),(0,1)], rays=[(1,1)], lines=[],241... backend='ppl', base_ring=QQ)242sage: TestSuite(p).run(skip='_test_pickling')243"""244pass245246247#########################################################################248class Polyhedron_ZZ_ppl(Polyhedron_ppl, Polyhedron_ZZ):249"""250Polyhedra over `\ZZ` with ppl251252INPUT:253254- ``Vrep`` -- a list ``[vertices, rays, lines]`` or ``None``.255256- ``Hrep`` -- a list ``[ieqs, eqns]`` or ``None``.257258EXAMPLES::259260sage: p = Polyhedron(vertices=[(0,0),(1,0),(0,1)], rays=[(1,1)], lines=[])261... backend='ppl', base_ring=ZZ)262sage: TestSuite(p).run(skip='_test_pickling')263"""264pass265266267