from .islhelper import mainctx, libisl
from .geometry import GeometricObject, Point
-from .linexprs import Expression, Rational
+from .linexprs import LinExpr, Rational
from .domains import Domain
class Polyhedron(Domain):
-
+ """
+ Polyhedron class allows users to build and inspect polyherons. Polyhedron inherits from Domain.
+ """
__slots__ = (
'_equalities',
'_inequalities',
)
def __new__(cls, equalities=None, inequalities=None):
+ """
+ Create and return a new Polyhedron from a string or list of equalities and inequalities.
+ """
+
if isinstance(equalities, str):
if inequalities is not None:
raise TypeError('too many arguments')
equalities = []
else:
for i, equality in enumerate(equalities):
- if not isinstance(equality, Expression):
+ if not isinstance(equality, LinExpr):
raise TypeError('equalities must be linear expressions')
equalities[i] = equality.scaleint()
if inequalities is None:
inequalities = []
else:
for i, inequality in enumerate(inequalities):
- if not isinstance(inequality, Expression):
+ if not isinstance(inequality, LinExpr):
raise TypeError('inequalities must be linear expressions')
inequalities[i] = inequality.scaleint()
symbols = cls._xsymbols(equalities + inequalities)
@property
def equalities(self):
"""
- Return a list of the equalities in a set.
+ Return a list of the equalities in a polyhedron.
"""
return self._equalities
@property
def inequalities(self):
"""
- Return a list of the inequalities in a set.
+ Return a list of the inequalities in a polyhedron.
"""
return self._inequalities
@property
def constraints(self):
"""
- Return ta list of the constraints of a set.
+ Return the list of the constraints of a polyhedron.
"""
return self._constraints
def polyhedra(self):
return self,
- def disjoint(self):
+ def make_disjoint(self):
"""
- Return a set as disjoint.
+ Return a polyhedron as disjoint.
"""
return self
def isuniverse(self):
"""
- Return true if a set is the Universe set.
+ Return true if a polyhedron is the Universe set.
"""
islbset = self._toislbasicset(self.equalities, self.inequalities,
self.symbols)
def aspolyhedron(self):
"""
- Return polyhedral hull of a set.
+ Return the polyhedral hull of a polyhedron.
"""
return self
def __contains__(self, point):
+ """
+ Report whether a polyhedron constains an integer point
+ """
if not isinstance(point, Point):
raise TypeError('point must be a Point instance')
if self.symbols != point.symbols:
coefficient = islhelper.isl_val_to_int(coefficient)
if coefficient != 0:
coefficients[symbol] = coefficient
- expression = Expression(coefficients, constant)
+ expression = LinExpr(coefficients, constant)
if libisl.isl_constraint_is_equality(islconstraint):
equalities.append(expression)
else:
@classmethod
def fromstring(cls, string):
+ """
+ Create and return a Polyhedron from a string.
+ """
domain = Domain.fromstring(string)
if not isinstance(domain, Polyhedron):
raise ValueError('non-polyhedral expression: {!r}'.format(string))
@classmethod
def fromsympy(cls, expr):
"""
- Convert a sympy object to an expression.
+ Convert a sympy object to a polyhedron.
"""
domain = Domain.fromsympy(expr)
if not isinstance(domain, Polyhedron):
def tosympy(self):
"""
- Return an expression as a sympy object.
+ Return a polyhedron as a sympy object.
"""
import sympy
constraints = []
def _polymorphic(func):
@functools.wraps(func)
def wrapper(left, right):
- if not isinstance(left, Expression):
+ if not isinstance(left, LinExpr):
if isinstance(left, numbers.Rational):
left = Rational(left)
else:
raise TypeError('left must be a a rational number '
'or a linear expression')
- if not isinstance(right, Expression):
+ if not isinstance(right, LinExpr):
if isinstance(right, numbers.Rational):
right = Rational(right)
else:
@_polymorphic
def Lt(left, right):
"""
- Assert first set is less than the second set.
+ Returns a Polyhedron instance with a single constraint as left less than right.
"""
return Polyhedron([], [right - left - 1])
@_polymorphic
def Le(left, right):
"""
- Assert first set is less than or equal to the second set.
+ Returns a Polyhedron instance with a single constraint as left less than or equal to right.
"""
return Polyhedron([], [right - left])
@_polymorphic
def Eq(left, right):
"""
- Assert first set is equal to the second set.
+ Returns a Polyhedron instance with a single constraint as left equal to right.
"""
return Polyhedron([left - right], [])
@_polymorphic
def Ne(left, right):
"""
- Assert first set is not equal to the second set.
+ Returns a Polyhedron instance with a single constraint as left not equal to right.
"""
return ~Eq(left, right)
@_polymorphic
def Gt(left, right):
"""
- Assert first set is greater than the second set.
+ Returns a Polyhedron instance with a single constraint as left greater than right.
"""
return Polyhedron([], [left - right - 1])
@_polymorphic
def Ge(left, right):
"""
- Assert first set is greater than or equal to the second set.
+ Returns a Polyhedron instance with a single constraint as left greater than or equal to right.
"""
return Polyhedron([], [left - right])