Change name of method
[linpy.git] / pypol / domains.py
index 23c21af..cdf2de4 100644 (file)
@@ -304,20 +304,13 @@ class Domain(GeometricObject):
         islset = libisl.isl_set_lexmax(islset)
         return self._fromislset(islset, self.symbols)
 
         islset = libisl.isl_set_lexmax(islset)
         return self._fromislset(islset, self.symbols)
 
-    def num_parameters(self):
-        """
-        Return the total number of parameters, input, output or set dimensions.
-        """
-        islbset = self._toislbasicset(self.equalities, self.inequalities, self.symbols)
-        num = libisl.isl_basic_set_dim(islbset, libisl.isl_dim_set)
-        return num
 
 
-    def involves_dims(self, dims):
+    def involves_vars(self, vars):
         """
         """
-        Returns true if set depends on given dimensions.
+        Returns true if set depends on given dimensions.
         """
         islset = self._toislset(self.polyhedra, self.symbols)
         """
         islset = self._toislset(self.polyhedra, self.symbols)
-        dims = sorted(dims)
+        dims = sorted(vars)
         symbols = sorted(list(self.symbols))
         n = 0
         if len(dims)>0:
         symbols = sorted(list(self.symbols))
         n = 0
         if len(dims)>0:
@@ -392,7 +385,7 @@ class Domain(GeometricObject):
                 coordinates[symbol] = coordinate
             points.append(Point(coordinates))
         return points
                 coordinates[symbol] = coordinate
             points.append(Point(coordinates))
         return points
-    
+
     @classmethod
     def _polygon_inner_point(cls, points):
         symbols = points[0].symbols
     @classmethod
     def _polygon_inner_point(cls, points):
         symbols = points[0].symbols
@@ -445,37 +438,41 @@ class Domain(GeometricObject):
         return sorted(points, key=angles.get)
 
     def faces(self):
         return sorted(points, key=angles.get)
 
     def faces(self):
-        vertices = self.vertices()
+        """
+        Returns the vertices of the faces of a polyhedra.
+        """
         faces = []
         faces = []
-        for constraint in self.constraints:
-            face = []
-            for vertex in vertices:
-                if constraint.subs(vertex.coordinates()) == 0:
-                    face.append(vertex)
-            faces.append(face)
+        for polyhedron in self.polyhedra:
+            vertices = polyhedron.vertices()
+            for constraint in polyhedron.constraints:
+                face = []
+                for vertex in vertices:
+                    if constraint.subs(vertex.coordinates()) == 0:
+                        face.append(vertex)
+                if len(face) >= 3:
+                    faces.append(face)
         return faces
 
     def _plot_2d(self, plot=None, **kwargs):
         import matplotlib.pyplot as plt
         from matplotlib.patches import Polygon
         return faces
 
     def _plot_2d(self, plot=None, **kwargs):
         import matplotlib.pyplot as plt
         from matplotlib.patches import Polygon
+        if plot is None:
+            fig = plt.figure()
+            plot = fig.add_subplot(1, 1, 1)
+        xmin, xmax = plot.get_xlim()
+        ymin, ymax = plot.get_ylim()
         for polyhedron in self.polyhedra:
             vertices = polyhedron._sort_polygon_2d(polyhedron.vertices())
             xys = [tuple(vertex.values()) for vertex in vertices]
         for polyhedron in self.polyhedra:
             vertices = polyhedron._sort_polygon_2d(polyhedron.vertices())
             xys = [tuple(vertex.values()) for vertex in vertices]
-            if plot is None:
-                fig = plt.figure()
-                plot = fig.add_subplot(1, 1, 1)
-            xmin, xmax = plot.get_xlim()
-            ymin, ymax = plot.get_xlim()
             xs, ys = zip(*xys)
             xmin, xmax = min(xmin, float(min(xs))), max(xmax, float(max(xs)))
             ymin, ymax = min(ymin, float(min(ys))), max(ymax, float(max(ys)))
             xs, ys = zip(*xys)
             xmin, xmax = min(xmin, float(min(xs))), max(xmax, float(max(xs)))
             ymin, ymax = min(ymin, float(min(ys))), max(ymax, float(max(ys)))
-            plot.set_xlim(xmin, xmax)
-            plot.set_ylim(ymin, ymax)
             plot.add_patch(Polygon(xys, closed=True, **kwargs))
             plot.add_patch(Polygon(xys, closed=True, **kwargs))
+        plot.set_xlim(xmin, xmax)
+        plot.set_ylim(ymin, ymax)
         return plot
 
     def _plot_3d(self, plot=None, **kwargs):
         return plot
 
     def _plot_3d(self, plot=None, **kwargs):
-        from .polyhedra import Polyhedron
         import matplotlib.pyplot as plt
         from mpl_toolkits.mplot3d import Axes3D
         from mpl_toolkits.mplot3d.art3d import Poly3DCollection
         import matplotlib.pyplot as plt
         from mpl_toolkits.mplot3d import Axes3D
         from mpl_toolkits.mplot3d.art3d import Poly3DCollection
@@ -485,33 +482,33 @@ class Domain(GeometricObject):
         else:
             axes = plot
         xmin, xmax = axes.get_xlim()
         else:
             axes = plot
         xmin, xmax = axes.get_xlim()
-        ymin, ymax = axes.get_xlim()
-        zmin, zmax = axes.get_xlim()
+        ymin, ymax = axes.get_ylim()
+        zmin, zmax = axes.get_zlim()
         poly_xyzs = []
         poly_xyzs = []
-        for polyhedron in self.polyhedra:
-            for vertices in polyhedron.faces():
-                if len(vertices) == 0:
-                    continue
-                vertices = Polyhedron._sort_polygon_3d(vertices)
-                vertices.append(vertices[0])
-                face_xyzs = [tuple(vertex.values()) for vertex in vertices]
-                xs, ys, zs = zip(*face_xyzs)
-                xmin, xmax = min(xmin, float(min(xs))), max(xmax, float(max(xs)))
-                ymin, ymax = min(ymin, float(min(ys))), max(ymax, float(max(ys)))
-                zmin, zmax = min(zmin, float(min(zs))), max(zmax, float(max(zs)))
-                poly_xyzs.append(face_xyzs)
-            collection = Poly3DCollection(poly_xyzs, **kwargs)
-            axes.add_collection3d(collection)
-            axes.set_xlim(xmin, xmax)
-            axes.set_ylim(ymin, ymax)
-            axes.set_zlim(zmin, zmax)
+        for vertices in self.faces():
+            vertices = self._sort_polygon_3d(vertices)
+            vertices.append(vertices[0])
+            face_xyzs = [tuple(vertex.values()) for vertex in vertices]
+            xs, ys, zs = zip(*face_xyzs)
+            xmin, xmax = min(xmin, float(min(xs))), max(xmax, float(max(xs)))
+            ymin, ymax = min(ymin, float(min(ys))), max(ymax, float(max(ys)))
+            zmin, zmax = min(zmin, float(min(zs))), max(zmax, float(max(zs)))
+            poly_xyzs.append(face_xyzs)
+        collection = Poly3DCollection(poly_xyzs, **kwargs)
+        axes.add_collection3d(collection)
+        axes.set_xlim(xmin, xmax)
+        axes.set_ylim(ymin, ymax)
+        axes.set_zlim(zmin, zmax)
         return axes
 
         return axes
 
+
     def plot(self, plot=None, **kwargs):
         """
         Display plot of this set.
         """
     def plot(self, plot=None, **kwargs):
         """
         Display plot of this set.
         """
-        if self.dimension == 2:
+        if not self.isbounded():
+            raise ValueError('domain must be bounded')
+        elif self.dimension == 2:
             return self._plot_2d(plot=plot, **kwargs)
         elif self.dimension == 3:
             return self._plot_3d(plot=plot, **kwargs)
             return self._plot_2d(plot=plot, **kwargs)
         elif self.dimension == 3:
             return self._plot_3d(plot=plot, **kwargs)
@@ -525,6 +522,10 @@ class Domain(GeometricObject):
         return False
 
     def subs(self, symbol, expression=None):
         return False
 
     def subs(self, symbol, expression=None):
+        """
+        Subsitute the given value into an expression and return the resulting
+        expression.
+        """
         polyhedra = [polyhedron.subs(symbol, expression)
             for polyhedron in self.polyhedra]
         return Domain(*polyhedra)
         polyhedra = [polyhedron.subs(symbol, expression)
             for polyhedron in self.polyhedra]
         return Domain(*polyhedra)