Ticket #12544: trac_12544_switch_cones_to_PointCollection.patch

File trac_12544_switch_cones_to_PointCollection.patch, 12.1 KB (added by novoselt, 9 years ago)
  • sage/geometry/cone.py

    # HG changeset patch
    # User Andrey Novoseltsev <novoselt@gmail.com>
    # Date 1331053635 25200
    # Node ID f71249f0b399aa5f252d063b70c13a6dc4bf95a5
    # Parent  3854bc3d83deeaf44bc96b252cd35a7602986932
    Switch cones to using PointCollection for ray generators instead of tuples.
    
    diff --git a/sage/geometry/cone.py b/sage/geometry/cone.py
    a b  
    175175
    176176from sage.combinat.posets.posets import FinitePoset
    177177from sage.geometry.lattice_polytope import LatticePolytope, integral_length
     178from sage.geometry.point_collection import PointCollection
    178179from sage.geometry.polyhedron.constructor import Polyhedron
    179180from sage.geometry.polyhedron.base import is_Polyhedron
    180181from sage.geometry.hasse_diagram import Hasse_diagram_from_incidences
     
    183184from sage.geometry.toric_plotter import ToricPlotter, label_list
    184185from sage.graphs.all import DiGraph
    185186from sage.matrix.all import matrix, identity_matrix
    186 from sage.misc.all import flatten, latex, prod
     187from sage.misc.all import cached_method, flatten, latex, prod
    187188from sage.modules.all import span, vector
    188189from sage.rings.all import QQ, RR, ZZ, gcd
    189190from sage.structure.all import SageObject
     
    381382            raise ValueError("the apex of %s is not at the origin!"
    382383                             % polyhedron)
    383384        rays = normalize_rays(polyhedron.rays(), lattice)
    384         strict_rays = tuple(rays)
    385         lines = tuple(normalize_rays(polyhedron.lines(), lattice))
    386         for line in lines:
     385        for line in normalize_rays(polyhedron.lines(), lattice):
    387386            rays.append(line)
    388387            rays.append(-line)
    389388            rays[-1].set_immutable()
    390         cone = ConvexRationalPolyhedralCone(rays, lattice)
    391         # Save constructed stuff for later use
    392         cone._polyhedron = polyhedron
    393         cone._strict_rays = strict_rays
    394         cone._lines = lines
    395         return cone
     389        return ConvexRationalPolyhedralCone(rays, lattice)
    396390    # Cone from rays
    397391    if check or normalize:
    398392        rays = normalize_rays(rays, lattice)
     
    598592            ((1, 0),)
    599593            sage: TestSuite(c).run()
    600594        """
    601         self._rays = tuple(rays)
     595        self._rays = PointCollection(rays, lattice)
    602596        self._lattice = self._rays[0].parent() if lattice is None else lattice
    603597
    604598    def __cmp__(self, right):
     
    962956            [ 0  1 -1]
    963957            [ 1  0  0]
    964958        """
    965         if "_ray_matrix" not in self.__dict__:
    966             self._ray_matrix = matrix(ZZ, self.nrays(),
    967                             self.lattice().degree(), self._rays).transpose()
    968             self._ray_matrix.set_immutable()
    969         return self._ray_matrix
     959        return self._rays.matrix()
    970960
    971961    def ray_set(self):
    972962        r"""
     
    26052595        """
    26062596        return self.nrays() == self.dim()
    26072597
     2598    @cached_method
    26082599    def is_smooth(self):
    26092600        r"""
    26102601        Check if ``self`` is smooth.
     
    26372628            sage: Cone([(1,0,0), (2,1,2)]).is_smooth()
    26382629            True
    26392630        """
    2640         if "_is_smooth" not in self.__dict__:
    2641             if not self.is_simplicial():
    2642                 self._is_smooth = False
    2643             else:
    2644                 elementary_divisors = self.ray_matrix().transpose().elementary_divisors()
    2645                 self._is_smooth = (elementary_divisors == [1]*self.nrays())
    2646         return self._is_smooth
     2631        if not self.is_simplicial():
     2632            return False
     2633        return self.rays().matrix().transpose().elementary_divisors() == \
     2634               [1] * self.nrays()
    26472635
    26482636    def is_trivial(self):
    26492637        """
     
    27572745            self._line_set = frozenset(self.lines())
    27582746        return self._line_set
    27592747
     2748    @cached_method
    27602749    def linear_subspace(self):
    27612750        r"""
    27622751        Return the largest linear subspace contained inside of ``self``.
     
    27732762            Basis matrix:
    27742763            [1 0]
    27752764        """
    2776         if "_linear_subspace" not in self.__dict__:
    2777             if self.is_strictly_convex():
    2778                 self._linear_subspace = span(
    2779                                     [vector(QQ, self.lattice_dim())], QQ)
    2780             else:
    2781                 self._linear_subspace = span(self.lines(), QQ)
    2782         return self._linear_subspace
    2783 
     2765        if self.is_strictly_convex():
     2766            return span([vector(QQ, self.lattice_dim())], QQ)
     2767        return span(self.lines(), QQ)
     2768
     2769    @cached_method
    27842770    def lines(self):
    27852771        r"""
    27862772        Return lines generating the linear subspace of ``self``.
     
    28012787            sage: fullplane.lines()
    28022788            (N(0, 1), N(1, 0))
    28032789        """
    2804         if "_lines" not in self.__dict__:
    2805             lines = []
    2806             for g in self._PPL_cone().minimized_generators():
    2807                 if g.is_line():
    2808                     lines.append(g.coefficients())
    2809             N = self.lattice()
    2810             lines = tuple(map(N,lines))
    2811             for l in lines:
    2812                 l.set_immutable()
    2813             self._lines = lines
    2814         return self._lines
     2790        lines = []
     2791        for g in self._PPL_cone().minimized_generators():
     2792            if g.is_line():
     2793                lines.append(g.coefficients())
     2794        N = self.lattice()
     2795        lines = tuple(map(N, lines))
     2796        for l in lines:
     2797            l.set_immutable()
     2798        return PointCollection(lines, N)
    28152799
    28162800    def plot(self, **options):
    28172801        r"""
     
    35373521        if not self.is_simplicial():   
    35383522            from sage.geometry.triangulation.point_configuration import PointConfiguration
    35393523            origin = self.nrays() # last one in pc
    3540             pc = PointConfiguration(self.rays() + (N(0),), star=origin)
     3524            pc = PointConfiguration(tuple(self.rays()) + (N(0),), star=origin)
    35413525            triangulation = pc.triangulate()
    35423526            subcones = [ Cone([self.ray(i) for i in simplex if i!=origin],
    35433527                              lattice=N, check=False)
     
    35513535        gens = filter(lambda v:gcd(v)==1, gens)
    35523536        return tuple(gens)
    35533537
     3538    @cached_method
    35543539    def Hilbert_basis(self):
    35553540        r"""
    35563541        Return the Hilbert basis of the cone.
     
    36283613            Normaliz.
    36293614            http://www.mathematik.uni-osnabrueck.de/normaliz/
    36303615        """
    3631         if '_Hilbert_basis' in self.__dict__:
    3632             return self._Hilbert_basis
    3633 
    36343616        if self.is_strictly_convex():
    36353617            def not_in_linear_subspace(x): return True
    36363618        else:
     
    36513633                   for y in irreducible+gens):
    36523634                continue
    36533635            irreducible.append(x)
    3654            
    3655         Hilbert_basis = tuple(irreducible)
    3656         self._Hilbert_basis = Hilbert_basis
    3657         return Hilbert_basis
     3636        return tuple(irreducible)
    36583637
    36593638    def Hilbert_coefficients(self, point):
    36603639        r"""
     
    37253704        p.solve()
    37263705
    37273706        return vector(ZZ, p.get_values(x))
    3728 
    3729 
  • sage/geometry/integral_points.pyx

    diff --git a/sage/geometry/integral_points.pyx b/sage/geometry/integral_points.pyx
    a b  
    1010#                  http://www.gnu.org/licenses/
    1111#*****************************************************************************
    1212
     13from sage.geometry.point_collection import is_PointCollection
    1314from sage.matrix.constructor import matrix, column_matrix, vector, diagonal_matrix
    1415from sage.rings.all import QQ, RR, ZZ, gcd, lcm
    1516from sage.combinat.permutation import Permutation
     
    127128        sage: len(parallelotope_points(rays, ZZ^4))
    128129        967
    129130    """
    130     R = matrix(spanning_points).transpose()
     131    if is_PointCollection(spanning_points):
     132        R = spanning_points.matrix()
     133    else:
     134        R = matrix(spanning_points).transpose()
    131135    e, d, VDinv = ray_matrix_normal_form(R)
    132136    points = loop_over_parallelotope_points(e, d, VDinv, R, lattice)
    133137    for p in points:
  • sage/geometry/lattice_polytope.py

    diff --git a/sage/geometry/lattice_polytope.py b/sage/geometry/lattice_polytope.py
    a b  
    100100#                  http://www.gnu.org/licenses/
    101101#*****************************************************************************
    102102
     103from sage.geometry.point_collection import is_PointCollection
    103104from sage.graphs.all import Graph
    104105from sage.interfaces.all import maxima
    105106from sage.matrix.all import matrix, is_Matrix
     
    282283    if isinstance(data, LatticePolytopeClass):
    283284        return data
    284285
    285     if not is_Matrix(data) and not isinstance(data,(file,basestring)):
     286    if is_PointCollection(data):
     287        data = data.matrix()
     288    elif not is_Matrix(data) and not isinstance(data,(file,basestring)):
    286289        try:
    287290            data = matrix(ZZ,data).transpose()
    288291        except ValueError:
  • sage/geometry/point_collection.pyx

    diff --git a/sage/geometry/point_collection.pyx b/sage/geometry/point_collection.pyx
    a b  
    100100        False
    101101        sage: from sage.geometry.point_collection import PointCollection
    102102        sage: c = Cone([(0,0,1), (1,0,1), (0,1,1), (1,1,1)])
    103         sage: is_PointCollection(c.rays()) # TODO: make it True
    104         False
     103        sage: is_PointCollection(c.rays())
     104        True
    105105        sage: c = PointCollection(c.rays(), c.lattice())
    106106        sage: is_PointCollection(c)
    107107        True
     
    322322        """
    323323        return len(self._points)
    324324   
     325    def __list__(self):
     326        r"""
     327        Return a list of points of ``self``.
     328
     329        OUTPUT:
     330
     331        - a list.
     332
     333        TESTS::
     334
     335            sage: from sage.geometry.point_collection import PointCollection
     336            sage: c = Cone([(0,0,1), (1,0,1), (0,1,1), (1,1,1)])
     337            sage: c = PointCollection(c.rays(), c.lattice())
     338            sage: list(c)
     339            [N(0, 0, 1), N(1, 0, 1), N(0, 1, 1), N(1, 1, 1)]
     340        """
     341        return list(self._points)
     342
    325343    def __reduce__(self):
    326344        r"""
    327345        Prepare ``self`` for pickling.
     
    346364        """
    347365        return (PointCollection, (self._points, self._module))
    348366
     367    def __tuple__(self):
     368        r"""
     369        Return the tuple of points of ``self``.
     370
     371        OUTPUT:
     372
     373        - a tuple.
     374
     375        TESTS::
     376
     377            sage: from sage.geometry.point_collection import PointCollection
     378            sage: c = Cone([(0,0,1), (1,0,1), (0,1,1), (1,1,1)])
     379            sage: c = PointCollection(c.rays(), c.lattice())
     380            sage: tuple(c)
     381            (N(0, 0, 1), N(1, 0, 1), N(0, 1, 1), N(1, 1, 1))
     382        """
     383        return self._points
     384
    349385    def _latex_(self):
    350386        r"""
    351387        Return a LaTeX representation of ``self``.
     
    528564            # TODO: add support for torsion modules as well?
    529565            return M.base_ring() ** M.dimension()
    530566   
     567    def index(self, *args):
     568        r"""
     569        Return the index of the first occurrence of ``point`` in ``self``.
     570
     571        INPUT:
     572
     573        - ``point`` -- a point of ``self``;
     574       
     575        - ``start`` -- (optional) an integer, if given, the search will start
     576          at this position;
     577
     578        - ``stop`` -- (optional) an integer, if given, the search will stop
     579          at this position.
     580         
     581        OUTPUT:
     582
     583        - an integer if ``point`` is in ``self[start:stop]``, otherwise a
     584          ``ValueError`` exception is raised.
     585
     586        EXAMPLES::
     587
     588            sage: from sage.geometry.point_collection import PointCollection
     589            sage: c = Cone([(0,0,1), (1,0,1), (0,1,1), (1,1,1)])
     590            sage: c = PointCollection(c.rays(), c.lattice())
     591            sage: c.index((0,1,1))
     592            Traceback (most recent call last):
     593            ...
     594            ValueError: tuple.index(x): x not in tuple
     595           
     596        Note that this was not a mistake: the *tuple* ``(0,1,1)`` is *not* a
     597        point of ``c``! We need to pass actual element of the ambient module of
     598        ``c`` to get their indices::
     599       
     600            sage: N = c.module()
     601            sage: c.index(N(0,1,1))
     602            2
     603            sage: c[2]
     604            N(0, 1, 1)
     605        """
     606        return self._points.index(*args)
     607
    531608    def matrix(self):
    532609        r"""
    533610        Return a matrix whose columns are points of ``self``.