From 6ec23dc57252ffe01aa60595fc499f580381e4a9 Mon Sep 17 00:00:00 2001 From: Vivien Maisonneuve Date: Fri, 11 Jul 2014 18:29:17 +0200 Subject: [PATCH 1/1] Add abstract class GeometricObject --- pypol/coordinates.py | 3 ++- pypol/domains.py | 18 +++++++++++------- pypol/geometry.py | 25 +++++++++++++++++++++++++ pypol/polyhedra.py | 7 ++----- 4 files changed, 40 insertions(+), 13 deletions(-) create mode 100644 pypol/geometry.py diff --git a/pypol/coordinates.py b/pypol/coordinates.py index d392e37..ceab418 100644 --- a/pypol/coordinates.py +++ b/pypol/coordinates.py @@ -4,6 +4,7 @@ import operator from collections import OrderedDict, Mapping +from .geometry import GeometricObject from .linexprs import Symbol @@ -78,7 +79,7 @@ class Coordinates: yield symbol, func(coordinate1, coordinate2) -class Point(Coordinates): +class Point(Coordinates, GeometricObject): """ This class represents points in space. """ diff --git a/pypol/domains.py b/pypol/domains.py index 255c995..cd118e8 100644 --- a/pypol/domains.py +++ b/pypol/domains.py @@ -6,6 +6,7 @@ from fractions import Fraction from . import islhelper from .islhelper import mainctx, libisl, isl_set_basic_sets +from .geometry import GeometricObject from .coordinates import Point from .linexprs import Expression, Symbol @@ -17,7 +18,7 @@ __all__ = [ @functools.total_ordering -class Domain: +class Domain(GeometricObject): __slots__ = ( '_polyhedra', @@ -28,14 +29,14 @@ class Domain: def __new__(cls, *polyhedra): from .polyhedra import Polyhedron if len(polyhedra) == 1: - polyhedron = polyhedra[0] - if isinstance(polyhedron, str): - return cls.fromstring(polyhedron) - elif isinstance(polyhedron, Polyhedron): - return polyhedron + argument = polyhedra[0] + if isinstance(argument, str): + return cls.fromstring(argument) + elif isinstance(argument, GeometricObject): + return argument.aspolyhedron() else: raise TypeError('argument must be a string ' - 'or a Polyhedron instance') + 'or a GeometricObject instance') else: for polyhedron in polyhedra: if not isinstance(polyhedron, Polyhedron): @@ -154,6 +155,9 @@ class Domain: islbset = libisl.isl_set_polyhedral_hull(islset) return Polyhedron._fromislbasicset(islbset, self.symbols) + def asdomain(self): + return self + def project(self, dims): # use to remove certain variables islset = self._toislset(self.polyhedra, self.symbols) diff --git a/pypol/geometry.py b/pypol/geometry.py new file mode 100644 index 0000000..d1d6770 --- /dev/null +++ b/pypol/geometry.py @@ -0,0 +1,25 @@ + +from abc import ABC, abstractmethod, abstractproperty + + +__all__ = [ + 'GeometricObject', +] + + +class GeometricObject(ABC): + + @abstractproperty + def symbols(self): + pass + + @property + def dimension(self): + return len(self.symbols) + + @abstractmethod + def aspolyhedron(self): + pass + + def asdomain(self): + return self.aspolyhedron() diff --git a/pypol/polyhedra.py b/pypol/polyhedra.py index a08213d..2eaa7b5 100644 --- a/pypol/polyhedra.py +++ b/pypol/polyhedra.py @@ -6,6 +6,7 @@ import numbers from . import islhelper from .islhelper import mainctx, libisl +from .geometry import GeometricObject from .coordinates import Point from .linexprs import Expression, Symbol, Rational from .domains import Domain @@ -33,11 +34,7 @@ class Polyhedron(Domain): if inequalities is not None: raise TypeError('too many arguments') return cls.fromstring(equalities) - elif isinstance(equalities, Polyhedron): - if inequalities is not None: - raise TypeError('too many arguments') - return equalities - elif isinstance(equalities, Domain): + elif isinstance(equalities, GeometricObject): if inequalities is not None: raise TypeError('too many arguments') return equalities.aspolyhedron() -- 2.20.1