From 64d90284660e19f486fd7b6d44f2dcf528b778fc Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Mon, 3 Jan 2022 11:57:15 -0500 Subject: [PATCH] Trac #33107: return immutable trivial Cholesky factors. We usually return an immutable factor from cholesky(), but in the special trivial case the factor was mutable (a copy of the input matrix). Here we make it immutable in both the superclass method and in the RDF/CDF subclass. --- src/sage/matrix/matrix2.pyx | 7 ++++++- src/sage/matrix/matrix_double_dense.pyx | 4 +++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/sage/matrix/matrix2.pyx b/src/sage/matrix/matrix2.pyx index bb935a2ae8c..ba196895e52 100644 --- a/src/sage/matrix/matrix2.pyx +++ b/src/sage/matrix/matrix2.pyx @@ -12626,6 +12626,9 @@ cdef class Matrix(Matrix1): sage: all( matrix(R,[]).cholesky() == matrix(R,[]) ....: for R in (RR,CC,RDF,CDF,ZZ,QQ,AA,QQbar) ) True + sage: all( matrix(R,[]).cholesky().is_immutable() + ....: for R in (RR,CC,RDF,CDF,ZZ,QQ,AA,QQbar) ) + True """ cdef Matrix C # output matrix @@ -12642,7 +12645,9 @@ cdef class Matrix(Matrix1): # The trivial matrix has a trivial cholesky decomposition. # We special-case this after is_hermitian() to ensure that # the matrix is square. - return self + C = self.__copy__() + C.set_immutable() + return C # Use classical=True to ensure that we don't get a permuted L. cdef Matrix L # block_ldlt() results diff --git a/src/sage/matrix/matrix_double_dense.pyx b/src/sage/matrix/matrix_double_dense.pyx index 8168239ff1c..59e84ab276d 100644 --- a/src/sage/matrix/matrix_double_dense.pyx +++ b/src/sage/matrix/matrix_double_dense.pyx @@ -3746,7 +3746,9 @@ cdef class Matrix_double_dense(Matrix_dense): raise ValueError(msg.format(self.nrows(), self.ncols())) if self._nrows == 0: # special case self.cache(cache_posdef, True) - return self.__copy__() + L = self.__copy__() + L.set_immutable() + return L L = self.fetch(cache_cholesky) if L is None: