From 477b84c8119e14a2c8a22329eb8671c70c9dfa19 Mon Sep 17 00:00:00 2001 From: Matthias Koeppe Date: Tue, 22 Feb 2022 12:02:29 -0800 Subject: [PATCH] LatticePolytopeClass, NefPartition, PointCollection, ToricLattice_ambient: Add _sage_input_ methods --- src/sage/geometry/lattice_polytope.py | 47 ++++++++++++++++++++++++++ src/sage/geometry/point_collection.pyx | 33 ++++++++++++++---- src/sage/geometry/toric_lattice.py | 23 +++++++++++++ 3 files changed, 97 insertions(+), 6 deletions(-) diff --git a/src/sage/geometry/lattice_polytope.py b/src/sage/geometry/lattice_polytope.py index 0b3b5f7088b..cfc63377021 100644 --- a/src/sage/geometry/lattice_polytope.py +++ b/src/sage/geometry/lattice_polytope.py @@ -548,6 +548,28 @@ def __init__(self, points=None, compute_vertices=None, self._ambient_facet_indices = tuple(ambient_facet_indices) self._vertices = ambient.vertices(self._ambient_vertex_indices) + def _sage_input_(self, sib, coerced): + """ + Return Sage command to reconstruct ``self``. + + See :mod:`sage.misc.sage_input` for details. + + EXAMPLES:: + + sage: p = lattice_polytope.cross_polytope(2) + sage: sage_input(p, verify=True) + # Verified + LatticePolytope(sage.geometry.point_collection.PointCollection((vector(ZZ, [1, 0]), + vector(ZZ, [0, 1]), + vector(ZZ, [-1, 0]), + vector(ZZ, [0, -1]))), + compute_vertices=False) + """ + if self._ambient is not self: + raise NotImplementedError + data = self._vertices + return sib.name('LatticePolytope')(sib(self._vertices), compute_vertices=False) + def __contains__(self, point): r""" Check if ``point`` is contained in ``self``. @@ -4495,6 +4517,31 @@ def _repr_(self): pass return result + def _sage_input_(self, sib, coerced): + """ + Return Sage command to reconstruct ``self``. + + See :mod:`sage.misc.sage_input` for details. + + EXAMPLES:: + + sage: o = lattice_polytope.cross_polytope(3) + sage: np = o.nef_partitions()[0]; np + Nef-partition {0, 1, 3} U {2, 4, 5} + sage: sage_input(np, verify=True) + # Verified + NefPartition([0, 0, 1, 0, 1, 1], + LatticePolytope(sage.geometry.point_collection.PointCollection((vector(ZZ, [1, 0, 0]), + vector(ZZ, [0, 1, 0]), + vector(ZZ, [0, 0, 1]), + vector(ZZ, [-1, 0, 0]), + vector(ZZ, [0, -1, 0]), + vector(ZZ, [0, 0, -1]))), + compute_vertices=False)) + """ + vertex_to_part = [ZZ(i) for i in self._vertex_to_part] + return sib.name('NefPartition')(vertex_to_part, sib(self.Delta_polar())) + def Delta(self, i=None): r""" Return the polytope $\Delta$ or $\Delta_i$ corresponding to ``self``. diff --git a/src/sage/geometry/point_collection.pyx b/src/sage/geometry/point_collection.pyx index 8b2f7e26828..2769f24e3fc 100644 --- a/src/sage/geometry/point_collection.pyx +++ b/src/sage/geometry/point_collection.pyx @@ -129,12 +129,10 @@ cdef class PointCollection(SageObject): if ``points`` are already accessible to you as a :class:`tuple`, it is preferable to use it for speed and memory consumption reasons; - - ``module`` -- an ambient module for ``points``. If ``None``, it will be - determined as :func:`parent` of the first point. Of course, this cannot - be done if there are no points, so in this case you must give an - appropriate ``module`` directly. Note that ``None`` is *not* the default - value - you always *must* give this argument explicitly, even if it is - ``None``. + - ``module`` -- an ambient module for ``points``. If ``None`` (the default), + it will be determined as :func:`parent` of the first point. Of course, this + cannot be done if there are no points, so in this case you must give an + appropriate ``module`` directly. OUTPUT: @@ -172,6 +170,29 @@ cdef class PointCollection(SageObject): self._points = tuple(points) self._module = self._points[0].parent() if module is None else module + def _sage_input_(self, sib, coerced): + r""" + Return Sage command to reconstruct ``self``. + + See :mod:`sage.misc.sage_input` for details. + + EXAMPLES:: + + sage: c = Cone([(0,0,1), (1,0,1), (0,1,1), (1,1,1)]).rays() + sage: sage_input(c, verify=True) + # Verified + sage.geometry.point_collection.PointCollection((vector(ZZ, [0, 0, 1]), vector(ZZ, [1, 0, 1]), vector(ZZ, [0, 1, 1]), vector(ZZ, [1, 1, 1]))) + + sage: c = sage.geometry.point_collection.PointCollection([], ToricLattice(2, 'U')) + sage: sage_input(c, verify=True) + # Verified + sage.geometry.point_collection.PointCollection((), ToricLattice(2, 'U', 'U*', 'U', 'U^*')) + """ + args = [sib(self._points)] + if not self._points or self._module is not self._points[0].parent(): + args.append(sib(self._module)) + return sib.name('sage.geometry.point_collection.PointCollection')(*args) + def __add__(left, right): r""" Return the joint point collection. diff --git a/src/sage/geometry/toric_lattice.py b/src/sage/geometry/toric_lattice.py index 3584f53f96e..de6880174aa 100644 --- a/src/sage/geometry/toric_lattice.py +++ b/src/sage/geometry/toric_lattice.py @@ -901,6 +901,29 @@ def __init__(self, rank, name, dual_name, latex_name, latex_dual_name): self._latex_name = latex_name self._latex_dual_name = latex_dual_name + def _sage_input_(self, sib, coerced): + r""" + Return Sage command to reconstruct ``self``. + + See :mod:`sage.misc.sage_input` for details. + + EXAMPLES:: + + sage: N = ToricLattice(3, "N", "M", "N", "M") + sage: sage_input(N, verify=True) + # Verified + ToricLattice(3, 'N', 'M') + + sage: N = ToricLattice(3, "N") + sage: sage_input(N, verify=True) + # Verified + ToricLattice(3, 'N', 'N*', 'N', 'N^*') + """ + args = [self.rank(), self._name, self._dual_name] + if self._latex_name != self._name or self._latex_dual_name != self._dual_name: + args.extend([self._latex_name, self._latex_dual_name]) + return sib.name('ToricLattice')(*args) + def __richcmp__(self, right, op): r""" Compare ``self`` and ``right``.