Beispiel #1
0
    def _init_from_Vrepresentation(self, vertices, rays, lines, minimize=True, verbose=False):
        """
        Construct polyhedron from V-representation data.

        INPUT:

        - ``vertices`` -- list of point. Each point can be specified
           as any iterable container of
           :meth:`~sage.geometry.polyhedron.base.base_ring` elements.

        - ``rays`` -- list of rays. Each ray can be specified as any
          iterable container of
          :meth:`~sage.geometry.polyhedron.base.base_ring` elements.

        - ``lines`` -- list of lines. Each line can be specified as
          any iterable container of
          :meth:`~sage.geometry.polyhedron.base.base_ring` elements.

        - ``verbose`` -- boolean (default: ``False``). Whether to print
          verbose output for debugging purposes.

        EXAMPLES::

            sage: p = Polyhedron(backend='ppl')
            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:
            d = LCM_list([denominator(v_i) for v_i in v])
            if d.is_one():
                gs.insert(point(Linear_Expression(v, 0)))
            else:
                dv = [ d*v_i for v_i in v ]
                gs.insert(point(Linear_Expression(dv, 0), d))
        if rays is None: rays = []
        for r in rays:
            d = LCM_list([denominator(r_i) for r_i in r])
            if d.is_one():
                gs.insert(ray(Linear_Expression(r, 0)))
            else:
                dr = [ d*r_i for r_i in r ]
                gs.insert(ray(Linear_Expression(dr, 0)))
        if lines is None: lines = []
        for l in lines:
            d = LCM_list([denominator(l_i) for l_i in l])
            if d.is_one():
                gs.insert(line(Linear_Expression(l, 0)))
            else:
                dl = [ d*l_i for l_i in l ]
                gs.insert(line(Linear_Expression(dl, 0)))
        if gs.empty():
            self._ppl_polyhedron = C_Polyhedron(self.ambient_dim(), 'empty')
        else:
            self._ppl_polyhedron = C_Polyhedron(gs)
        self._init_Vrepresentation_from_ppl(minimize)
        self._init_Hrepresentation_from_ppl(minimize)
Beispiel #2
0
 def generateRay_cons(self, generators, vs, lvs, all_vars):
     from ppl import Variable, Linear_Expression, ray
     from termination.algorithm.utils import get_free_name
     g_points = [g for g in generators if g.is_point()]
     g_lines = [g for g in generators if g.is_line()]
     g_rays = [g for g in generators if g.is_ray()]
     for l in g_lines:
         exp = Linear_Expression(0)
         for i in range(len(vs)):
             ci = int(l.coefficient(Variable(i)))
             exp += ci * Variable(i)
         g_rays.append(ray(exp))
         g_rays.append(ray(-exp))
     p_vars = get_free_name(all_vars, name="_a", num=len(g_points))
     r_vars = get_free_name(all_vars, name="_br", num=len(g_rays))
     ray_cons = []
     relation = {}
     for i in range(len(vs)):
         exp = Term(0)
         for pi in range(len(g_points)):
             if i == 0:
                 relation[p_vars[pi]] = g_points[pi]
             exp += Term(p_vars[pi], g_points[pi].divisor()) * int(g_points[pi].coefficient(Variable(i)))
         for ri in range(len(g_rays)):
             if i == 0:
                 relation[r_vars[ri]] = g_rays[ri]
             ci = int(g_rays[ri].coefficient(Variable(i)))
             if ci == 0:
                 continue
             exp += Term(r_vars[ri]) * ci
         ray_cons.append(exp == Term(vs[i]))
     exp = Term(0)
     for a in p_vars:
         ai = Term(a)
         exp += ai
         ray_cons.append(ai >= 0)
     ray_cons.append(exp == 1)
     exp = Term(0)
     for b in r_vars:
         bi = Term(b)
         exp += bi
         ray_cons.append(bi >= 0)
     ray_cons.append(exp >= 1)
     ors = []
     for b in r_vars:
         bi = Term(b)
         ors.append(exp == bi)
     ray_cons.append(Or(ors))
     return ray_cons, relation
Beispiel #3
0
def ppl_positive_cone(n):
    r"""
    Return the positive cone in R^n

    EXAMPLES::

        sage: from surface_dynamics.misc.ppl_utils import ppl_positive_cone # optional - pplpy
        sage: C = ppl_positive_cone(3)  # optional - pplpy
        sage: C.minimized_generators()  # optional - pplpy
        Generator_System {point(0/1, 0/1, 0/1), ray(0, 0, 1), ray(0, 1, 0), ray(1, 0, 0)}
    """
    gs = ppl.Generator_System(ppl_zero_point(n))
    for i in range(n):
        gs.insert(ppl.ray(ppl.Variable(i)))
    return ppl.C_Polyhedron(gs)
def ppl_cone(rays):
    r"""
    Convert the list ``rays`` into a ppl cone

    EXAMPLES::

        sage: from surface_dynamics.misc.ppl_utils import ppl_cone  # optional - pplpy
        sage: C = ppl_cone([(0,1,2),(1,1,1),(1,0,1)])   # optional - pplpy
        sage: C.minimized_generators()   # optional - pplpy
        Generator_System {point(0/1, 0/1, 0/1), ray(0, 1, 2), ray(1, 0, 1), ray(1, 1, 1)}
    """
    n = len(rays[0])
    gs = ppl.Generator_System(ppl_zero_point(n))
    for r in rays:
        gs.insert(ppl.ray(sum(j * ppl.Variable(i) for i, j in enumerate(r))))
    return ppl.C_Polyhedron(gs)
Beispiel #5
0
def ppl_cone(rays):
    r"""
    Convert the list ``rays`` into a ppl cone

    EXAMPLES::

        sage: from surface_dynamics.misc.ppl_utils import ppl_cone  # optional - pplpy
        sage: C = ppl_cone([(0,1,2),(1,1,1),(1,0,1)])   # optional - pplpy
        sage: C.minimized_generators()   # optional - pplpy
        Generator_System {point(0/1, 0/1, 0/1), ray(0, 1, 2), ray(1, 0, 1), ray(1, 1, 1)}
    """
    n = len(rays[0])
    gs = ppl.Generator_System(ppl_zero_point(n))
    for r in rays:
        gs.insert(ppl.ray(sum(int(j) * ppl.Variable(i) for i,j in enumerate(r))))
    return ppl.C_Polyhedron(gs)
Beispiel #6
0
def ppl_positive_cone(n):
    r"""
    Return the positive cone in R^n

    EXAMPLES::

        sage: from surface_dynamics.misc.ppl_utils import ppl_positive_cone # optional - pplpy
        sage: C = ppl_positive_cone(3)  # optional - pplpy
        sage: C.minimized_generators()  # optional - pplpy
        Generator_System {point(0/1, 0/1, 0/1), ray(0, 0, 1), ray(0, 1, 0), ray(1, 0, 0)}
    """
    gs = ppl.Generator_System(ppl_zero_point(n))
    l = [0]*n
    for i in range(n):
        gs.insert(ppl.ray(ppl.Variable(i)))
    return ppl.C_Polyhedron(gs)
Beispiel #7
0
    def hyperplanes(self):
        """
        **Description:**
        Returns the inward-pointing normals to the hyperplanes that define the
        cone.

        **Arguments:**
        None.

        **Returns:**
        *(numpy.ndarray)* The list of inward-pointing normals to the
        hyperplanes that define the cone.

        **Example:**
        We construct two cones and find their hyperplane normals.
        ```python {3,6}
        c1 = Cone([[0,1],[1,1]])
        c2 = Cone(hyperplanes=[[0,1],[1,1]])
        c1.hyperplanes()
        # array([[ 1,  0],
        #        [-1,  1]])
        c2.hyperplanes()
        # array([[0, 1],
        #        [1, 1]])
        ```
        """
        if self._hyperplanes is not None:
            return np.array(self._hyperplanes)
        if self._ambient_dim >= 12 and len(self.rays()) != self._ambient_dim:
            print("Warning: This operation might take a while for d > ~12 "
                  "and is likely impossible for d > ~18.")
        gs = ppl.Generator_System()
        vrs = [ppl.Variable(i) for i in range(self._ambient_dim)]
        gs.insert(ppl.point(0))
        for r in self.extremal_rays():
            gs.insert(
                ppl.ray(sum(r[i] * vrs[i] for i in range(self._ambient_dim))))
        cone = ppl.C_Polyhedron(gs)
        hyperplanes = []
        for cstr in cone.minimized_constraints():
            hyperplanes.append(tuple(int(c) for c in cstr.coefficients()))
            if cstr.is_equality():
                hyperplanes.append(tuple(-int(c) for c in cstr.coefficients()))
        self._hyperplanes = np.array(hyperplanes, dtype=int)
        return np.array(self._hyperplanes)
def ppl_convert(P):
    r"""
    Convert a Sage polyhedron to a ppl polyhedron

    EXAMPLES::

        sage: from surface_dynamics.misc.ppl_utils import ppl_convert  # optional - pplpy
        sage: P = ppl_convert(Polyhedron(vertices=[(0,1,0),(1,0,1)], rays=[(0,0,1),[3,2,1]]))  # optional - pplpy
        sage: P.minimized_generators()  # optional - pplpy
        Generator_System {ray(0, 0, 1), point(0/1, 1/1, 0/1), point(1/1, 0/1, 1/1), ray(3, 2, 1)}
    """
    if isinstance(P, ppl.C_Polyhedron):
        return P
    gs = ppl.Generator_System()
    for v in P.vertices_list():
        gs.insert(ppl.point(sum(j * ppl.Variable(i) for i, j in enumerate(v))))
    for r in P.rays_list():
        gs.insert(ppl.ray(sum(j * ppl.Variable(i) for i, j in enumerate(r))))
    for l in P.lines_list():
        gs.insert(ppl.line(sum(j * ppl.Variable(i) for i, j in enumerate(l))))
    return ppl.C_Polyhedron(gs)
Beispiel #9
0
def ppl_convert(P):
    r"""
    Convert a Sage polyhedron to a ppl polyhedron

    EXAMPLES::

        sage: from surface_dynamics.misc.ppl_utils import ppl_convert  # optional - pplpy
        sage: P = ppl_convert(Polyhedron(vertices=[(0,1,0),(1,0,1)], rays=[(0,0,1),[3,2,1]]))  # optional - pplpy
        sage: P.minimized_generators()  # optional - pplpy
        Generator_System {ray(0, 0, 1), point(0/1, 1/1, 0/1), point(1/1, 0/1, 1/1), ray(3, 2, 1)}
    """
    if isinstance(P, ppl.C_Polyhedron):
        return P
    gs = ppl.Generator_System()
    for v in P.vertices_list():
        gs.insert(ppl.point(sum(int(j) * ppl.Variable(i) for i,j in enumerate(v))))
    for r in P.rays_list():
        gs.insert(ppl.ray(sum(int(j) * ppl.Variable(i) for i,j in enumerate(r))))
    for l in P.lines_list():
        gs.insert(ppl.line(sum(int(j) * ppl.Variable(i) for i,j in enumerate(l))))
    return ppl.C_Polyhedron(gs)
Beispiel #10
0
    def _init_from_Vrepresentation(self,
                                   vertices,
                                   rays,
                                   lines,
                                   minimize=True,
                                   verbose=False):
        """
        Construct polyhedron from V-representation data.

        INPUT:

        - ``vertices`` -- list of point. Each point can be specified
           as any iterable container of
           :meth:`~sage.geometry.polyhedron.base.base_ring` elements.

        - ``rays`` -- list of rays. Each ray can be specified as any
          iterable container of
          :meth:`~sage.geometry.polyhedron.base.base_ring` elements.

        - ``lines`` -- list of lines. Each line can be specified as
          any iterable container of
          :meth:`~sage.geometry.polyhedron.base.base_ring` elements.

        - ``verbose`` -- boolean (default: ``False``). Whether to print
          verbose output for debugging purposes.

        EXAMPLES::

            sage: p = Polyhedron(backend='ppl')
            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:
            d = LCM_list([denominator(v_i) for v_i in v])
            if d.is_one():
                gs.insert(point(Linear_Expression(v, 0)))
            else:
                dv = [d * v_i for v_i in v]
                gs.insert(point(Linear_Expression(dv, 0), d))
        if rays is None: rays = []
        for r in rays:
            d = LCM_list([denominator(r_i) for r_i in r])
            if d.is_one():
                gs.insert(ray(Linear_Expression(r, 0)))
            else:
                dr = [d * r_i for r_i in r]
                gs.insert(ray(Linear_Expression(dr, 0)))
        if lines is None: lines = []
        for l in lines:
            d = LCM_list([denominator(l_i) for l_i in l])
            if d.is_one():
                gs.insert(line(Linear_Expression(l, 0)))
            else:
                dl = [d * l_i for l_i in l]
                gs.insert(line(Linear_Expression(dl, 0)))
        if gs.empty():
            self._ppl_polyhedron = C_Polyhedron(self.ambient_dim(), 'empty')
        else:
            self._ppl_polyhedron = C_Polyhedron(gs)
        self._init_Vrepresentation_from_ppl(minimize)
        self._init_Hrepresentation_from_ppl(minimize)