From 5487c67cc851fe4febec8e61c04a526986d074f0 Mon Sep 17 00:00:00 2001 From: Simon King Date: Thu, 6 Feb 2014 09:27:41 +0100 Subject: [PATCH] Trac 15104: Faster creation of transposed matrix' parent --- src/sage/matrix/matrix1.pyx | 7 +++++-- src/sage/matrix/matrix_space.py | 9 +++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/sage/matrix/matrix1.pyx b/src/sage/matrix/matrix1.pyx index eb42a18cc9e..8fa4b92bc7f 100644 --- a/src/sage/matrix/matrix1.pyx +++ b/src/sage/matrix/matrix1.pyx @@ -2206,8 +2206,11 @@ cdef class Matrix(matrix0.Matrix): Full MatrixSpace of 2 by 3 dense matrices over Real Field with 53 bits of precision """ - if self._nrows == nrows and self._ncols == ncols and (sparse is None or self.is_sparse() == sparse): - return self._parent(entries=entries, coerce=coerce, copy=copy) + if (sparse is None or self.is_sparse() == sparse): + if self._nrows == nrows and self._ncols == ncols: + return self._parent(entries=entries, coerce=coerce, copy=copy) + elif self._nrows == ncols and self._ncols == nrows: + return self._parent.transposed(entries=entries, coerce=coerce, copy=copy) return self.matrix_space(nrows, ncols, sparse=sparse)(entries=entries, coerce=coerce, copy=copy) def block_sum(self, Matrix other): diff --git a/src/sage/matrix/matrix_space.py b/src/sage/matrix/matrix_space.py index 0e8d24490b5..030208083b8 100644 --- a/src/sage/matrix/matrix_space.py +++ b/src/sage/matrix/matrix_space.py @@ -339,6 +339,15 @@ def full_category_initialisation(self): self._category_is_initialised = True sage.structure.parent.Parent.__init__(self, category=category) + @lazy_attribute + def transposed(self): + """ + The transposed matrix space, having the same base ring and sparseness, + but number of columns and rows is swapped. + + """ + return MatrixSpace(self._base, self.__ncols, self.__nrows, self.__is_sparse) + @lazy_attribute def _copy_zero(self): """