diff --git a/src/sage/geometry/polyhedron/backend_polymake.py b/src/sage/geometry/polyhedron/backend_polymake.py index 53b73d28966..ee22da06338 100644 --- a/src/sage/geometry/polyhedron/backend_polymake.py +++ b/src/sage/geometry/polyhedron/backend_polymake.py @@ -418,8 +418,7 @@ def _init_Vrepresentation_from_polymake(self): self._Vrepresentation = [] parent = self.parent() p = self._polymake_polytope - for g in p.VERTICES: - g = g.sage() + for g in p.VERTICES.sage(): d = g[0] if d == 0: parent._make_Ray(self, g[1:]) @@ -427,8 +426,7 @@ def _init_Vrepresentation_from_polymake(self): parent._make_Vertex(self, g[1:]) else: raise NotImplementedError("Non-normalized vertex encountered: {}".format(g)) - for g in p.LINEALITY_SPACE: - g = g.sage() + for g in p.LINEALITY_SPACE.sage(): d = g[0] if d == 0: parent._make_Line(self, g[1:]) @@ -458,14 +456,14 @@ def _init_Hrepresentation_from_polymake(self): else: self._Hrepresentation = [] parent = self.parent() - for g in p.FACETS: + for g in p.FACETS.sage(): if all(x==0 for x in g[1:]): # Ignore vertical inequality pass else: - parent._make_Inequality(self, g.sage()) - for g in p.AFFINE_HULL: - parent._make_Equation(self, g.sage()) + parent._make_Inequality(self, g) + for g in p.AFFINE_HULL.sage(): + parent._make_Equation(self, g) self._Hrepresentation = tuple(self._Hrepresentation) @classmethod @@ -499,7 +497,7 @@ def _from_polymake_polytope(cls, parent, polymake_polytope): base_ring = coercion_model.common_parent(*data).base_ring() else: base_ring = QQ - ambient_dim = polymake_polytope.AMBIENT_DIM() + ambient_dim = polymake_polytope.AMBIENT_DIM().sage() parent = Polyhedra(base_ring=base_ring, ambient_dim=ambient_dim, backend='polymake') return cls(parent, None, None, polymake_polytope=polymake_polytope) diff --git a/src/sage/interfaces/polymake.py b/src/sage/interfaces/polymake.py index 6a60b34592a..8ac83e87ad6 100644 --- a/src/sage/interfaces/polymake.py +++ b/src/sage/interfaces/polymake.py @@ -331,8 +331,50 @@ def convert(y): r = self.new("{" + ",".join(A) + "}") r.__sage_dict = z # do this to avoid having the entries of the list be garbage collected return r - else: - return super(PolymakeAbstract, self)._coerce_impl(x, use_special=use_special) + + from sage.rings.all import Integer, Rational, RDF + from sage.rings.number_field.number_field import is_QuadraticField + + def to_str(x): + if isinstance(x, list): + s = '[' + for y in x: + s += to_str(y) + ', ' + s += ']' + return s + if isinstance(x, (Integer, Rational, int)): + return '{}'.format(x) + parent = None + try: + parent = x.parent() + except AttributeError: + pass + + if is_QuadraticField(parent): + return x._polymake_init_() + try: + if x.parent().is_exact(): + # No other exact rings are supported. + raise NotImplementedError + except AttributeError: + pass + + try: + x = RDF(x) + return '{}'.format(x) + except: + pass + + raise NotImplementedError + + # Iteratively calling polymake for conversion takes a long time. + # However, it takes iterated arrays of integers, rationals and floats directly. + try: + return self.new(to_str(x)) + except NotImplementedError: + pass + + return super(PolymakeAbstract, self)._coerce_impl(x, use_special=use_special) def console(self): """ @@ -1512,6 +1554,48 @@ def _sage_(self): """ T1, T2 = self.typeof() self._check_valid() + try: + # Try to just read things from the string representation. + if 'Sparse' in T1: + raise NotImplementedError + + r = self._repr_() + if 'Float' in T1: + from sage.rings.all import RDF + base_ring = RDF + str_to_base_ring = lambda s: RDF(s) + elif 'QuadraticExtension' in T1 and 'r' in r: + i = r.find('r') + i1 = min((r[i:]+' ').find(' '), (r[i:]+'\n').find('\n')) + d = int(r[i+1:i+i1]) + from sage.rings.number_field.number_field import QuadraticField + base_ring = QuadraticField(d) + + def str_to_base_ring(s): + m = re.match(r'(-?[0-9/]+)[+]?((-?[0-9/]+)r([0-9/]+))?', s) + a, b = m.group(1), m.group(3) + return base_ring(a) + base_ring(b)*base_ring.gen() + + elif 'Rational' in T1: + from sage.rings.all import QQ + base_ring = QQ + str_to_base_ring = lambda s: QQ(s) + else: + raise NotImplementedError + + if 'Vector' in T1: + from sage.modules.free_module_element import vector + if r == '': + return vector(base_ring) + return vector(base_ring, [str_to_base_ring(s) for s in r.split(' ')]) + elif 'Matrix' in T1: + from sage.matrix.constructor import matrix + if r == '': + return matrix(base_ring) + return matrix(base_ring, [[str_to_base_ring(s) for s in t.split(' ')] for t in r.split('\n')]) + except: + pass + if T1: Temp = self.typename() if Temp: