From 205879fb118e1c7c3fd954e42022b7fe0c6af119 Mon Sep 17 00:00:00 2001 From: Matthias Koeppe Date: Sun, 26 Mar 2017 11:23:26 -0700 Subject: [PATCH] Matrix, MatrixSpace: Add coercion to polymake interface --- src/sage/matrix/matrix1.pyx | 19 +++++++++++++++++++ src/sage/matrix/matrix_space.py | 15 +++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/sage/matrix/matrix1.pyx b/src/sage/matrix/matrix1.pyx index 940d511049b..e6c21fb5ef4 100644 --- a/src/sage/matrix/matrix1.pyx +++ b/src/sage/matrix/matrix1.pyx @@ -344,6 +344,25 @@ cdef class Matrix(matrix0.Matrix): s = str(self.rows()).replace('(','[').replace(')',']') return "Matrix(%s,%s,%s)"%(self.nrows(), self.ncols(), s) + def _polymake_(self, polymake=None): + """ + Tries to coerce this matrix to a polymake matrix. + + EXAMPLES:: + + sage: M = matrix(ZZ,2,range(4)) + sage: polymake(M) # optional - polymake + 0 1 + 2 3 + sage: K. = QuadraticField(5) + sage: M = matrix(K, [[1, 2], [sqrt5, 3]]) + sage: polymake(M) # optional - polymake + 1 2 + 0+1r5 3 + """ + P = polymake(self.parent()) + return polymake.new_object(P, [ list(r) for r in self.rows(copy=False) ]) + def _singular_(self, singular=None): """ Tries to coerce this matrix to a singular matrix. diff --git a/src/sage/matrix/matrix_space.py b/src/sage/matrix/matrix_space.py index 9dadcc38e80..6175a2c6510 100644 --- a/src/sage/matrix/matrix_space.py +++ b/src/sage/matrix/matrix_space.py @@ -1773,6 +1773,21 @@ def _magma_init_(self, magma): s = 'RMatrixSpace(%s,%s,%s)'%(K.name(), self.__nrows, self.__ncols) return s + def _polymake_init_(self): + r""" + Return the polymake representation of the matrix space. + + EXAMPLES:: + + sage: polymake(MatrixSpace(QQ,3)) # optional - polymake + Matrix + sage: polymake(MatrixSpace(QuadraticField(5),3)) # optional - polymake + Matrix + """ + from sage.interfaces.polymake import polymake + K = polymake(self.base_ring()) + return '"Matrix<{}>"'.format(K) + def dict_to_list(entries, nrows, ncols): """ Given a dictionary of coordinate tuples, return the list given by