Skip to content

Commit

Permalink
Trac #33678: Make representation integers exportable and small improv…
Browse files Browse the repository at this point in the history
…ements to backend ppl

In `backend_ppl.py` we currently manually copy the integers representing
the different representation types in `representation.py`. In this
ticket we change this to an import. This is easier to read and less
error prone.

In addition we outsource to abstract methods to create a ppl generator
resp. constraint system.

URL: https://trac.sagemath.org/33678
Reported by: gh-kliem
Ticket author(s): Jonathan Kliem
Reviewer(s): Travis Scrimshaw
  • Loading branch information
Release Manager committed May 22, 2022
2 parents 309d32a + 94bd1ee commit cb9d6aa
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 38 deletions.
110 changes: 77 additions & 33 deletions src/sage/geometry/polyhedron/backend_ppl.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from .base_mutable import Polyhedron_mutable
from .base_QQ import Polyhedron_QQ
from .base_ZZ import Polyhedron_ZZ
from .representation import VERTEX, RAY, LINE, INEQUALITY, EQUATION

from sage.misc.lazy_import import lazy_import
from sage.features import PythonModule
Expand Down Expand Up @@ -95,19 +96,7 @@ def _init_from_Vrepresentation(self, vertices, rays, lines, minimize=True, verbo
sage: from sage.geometry.polyhedron.backend_ppl import Polyhedron_ppl
sage: Polyhedron_ppl._init_from_Vrepresentation(p, [], [], [])
"""
gs = Generator_System()
if vertices is None:
vertices = []
for v in vertices:
gs.insert(self._convert_generator_to_ppl(v, 2))
if rays is None:
rays = []
for r in rays:
gs.insert(self._convert_generator_to_ppl(r, 3))
if lines is None:
lines = []
for l in lines:
gs.insert(self._convert_generator_to_ppl(l, 4))
gs = self._convert_generators_to_ppl(vertices, rays, lines)
if gs.empty():
ppl_polyhedron = C_Polyhedron(self.ambient_dim(), 'empty')
else:
Expand Down Expand Up @@ -137,15 +126,7 @@ def _init_from_Hrepresentation(self, ieqs, eqns, minimize=True, verbose=False):
sage: from sage.geometry.polyhedron.backend_ppl import Polyhedron_ppl
sage: Polyhedron_ppl._init_from_Hrepresentation(p, [], [])
"""
cs = Constraint_System()
if ieqs is None:
ieqs = []
for ieq in ieqs:
cs.insert(self._convert_constraint_to_ppl(ieq, 0))
if eqns is None:
eqns = []
for eqn in eqns:
cs.insert(self._convert_constraint_to_ppl(eqn, 1))
cs = self._convert_constraints_to_ppl(ieqs, eqns)
if cs.empty():
ppl_polyhedron = C_Polyhedron(self.ambient_dim(), 'universe')
else:
Expand Down Expand Up @@ -436,21 +417,22 @@ def _convert_generator_to_ppl(v, typ):
- ``v`` -- a vertex, ray, or line.
- ``typ`` -- integer; 2 -- vertex; 3 -- ray; 4 -- line
- ``typ`` -- integer according to `:sage:`~sage.geometry.polyhedron.representation.LINE` etc.
EXAMPLES::
sage: from sage.geometry.polyhedron.representation import VERTEX, RAY, LINE
sage: P = Polyhedron()
sage: P._convert_generator_to_ppl([1, 1/2, 3], 2)
sage: P._convert_generator_to_ppl([1, 1/2, 3], VERTEX)
point(2/2, 1/2, 6/2)
sage: P._convert_generator_to_ppl([1, 1/2, 3], 3)
sage: P._convert_generator_to_ppl([1, 1/2, 3], RAY)
ray(2, 1, 6)
sage: P._convert_generator_to_ppl([1, 1/2, 3], 4)
sage: P._convert_generator_to_ppl([1, 1/2, 3], LINE)
line(2, 1, 6)
"""
if typ == 2:
if typ == VERTEX:
ob = point
elif typ == 3:
elif typ == RAY:
ob = ray
else:
ob = line
Expand All @@ -460,11 +442,45 @@ def _convert_generator_to_ppl(v, typ):
return ob(Linear_Expression(v, 0))
else:
dv = [ d*v_i for v_i in v ]
if typ == 2:
if typ == VERTEX:
return ob(Linear_Expression(dv, 0), d)
else:
return ob(Linear_Expression(dv, 0))

@staticmethod
def _convert_generators_to_ppl(vertices, rays, lines):
r"""
Convert generators to a ``ppl`` generator system.
INPUT:
- ``vertices`` -- iterable of vertices or ``None``
- ``rays`` -- iterable of rays or ``None``
- ``lines`` -- iterable of lines or ``None``
EXAMPLES::
sage: P = Polyhedron()
sage: P._convert_generators_to_ppl([[1, 1/2, 3]], [[0, 1, 3/2]], [[0, 0, 1]])
Generator_System {point(2/2, 1/2, 6/2), ray(0, 2, 3), line(0, 0, 1)}
"""
gs = Generator_System()
if vertices is None:
vertices = []
for v in vertices:
gs.insert(Polyhedron_ppl._convert_generator_to_ppl(v, VERTEX))
if rays is None:
rays = []
for r in rays:
gs.insert(Polyhedron_ppl._convert_generator_to_ppl(r, RAY))
if lines is None:
lines = []
for l in lines:
gs.insert(Polyhedron_ppl._convert_generator_to_ppl(l, LINE))
return gs

@staticmethod
def _convert_constraint_to_ppl(c, typ):
r"""
Expand All @@ -474,25 +490,53 @@ def _convert_constraint_to_ppl(c, typ):
- ``c`` -- an inequality or equation.
- ``typ`` -- integer; 0 -- inequality; 3 -- equation
- ``typ`` -- integer according to `:sage:`~sage.geometry.polyhedron.representation.INEQUALITY` etc.
EXAMPLES::
sage: from sage.geometry.polyhedron.representation import INEQUALITY, EQUATION
sage: P = Polyhedron()
sage: P._convert_constraint_to_ppl([1, 1/2, 3], 0)
sage: P._convert_constraint_to_ppl([1, 1/2, 3], INEQUALITY)
x0+6*x1+2>=0
sage: P._convert_constraint_to_ppl([1, 1/2, 3], 1)
sage: P._convert_constraint_to_ppl([1, 1/2, 3], EQUATION)
x0+6*x1+2==0
"""
d = LCM_list([denominator(c_i) for c_i in c])
dc = [ ZZ(d*c_i) for c_i in c ]
b = dc[0]
A = dc[1:]
if typ == 0:
if typ == INEQUALITY:
return Linear_Expression(A, b) >= 0
else:
return Linear_Expression(A, b) == 0

@staticmethod
def _convert_constraints_to_ppl(ieqs, eqns):
r"""
Convert constraints to a ``ppl`` constraint system.
INPUT:
- ``ieqs`` -- iterable of inequalities or ``None``
- ``eqns`` -- iterable of equations or ``None``
EXAMPLES::
sage: P = Polyhedron()
sage: P._convert_constraints_to_ppl([[1, 1/2, 3]], None)
Constraint_System {x0+6*x1+2>=0}
"""
cs = Constraint_System()
if ieqs is None:
ieqs = []
for ieq in ieqs:
cs.insert(Polyhedron_ppl._convert_constraint_to_ppl(ieq, INEQUALITY))
if eqns is None:
eqns = []
for eqn in eqns:
cs.insert(Polyhedron_ppl._convert_constraint_to_ppl(eqn, EQUATION))
return cs



Expand Down
18 changes: 13 additions & 5 deletions src/sage/geometry/polyhedron/representation.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@
from copy import copy


# Numeric values to distinguish representation types
INEQUALITY = 0
EQUATION = 1
VERTEX = 2
RAY = 3
LINE = 4


#########################################################################
# PolyhedronRepresentation
# / \
Expand Down Expand Up @@ -51,11 +59,11 @@ class PolyhedronRepresentation(SageObject):
"""

# Numeric values for the output of the type() method
INEQUALITY = 0
EQUATION = 1
VERTEX = 2
RAY = 3
LINE = 4
INEQUALITY = INEQUALITY
EQUATION = EQUATION
VERTEX = VERTEX
RAY = RAY
LINE = LINE

def __len__(self):
"""
Expand Down

0 comments on commit cb9d6aa

Please sign in to comment.