Skip to content

Commit

Permalink
Trac #33107: return immutable trivial Cholesky factors.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
orlitzky committed Jan 3, 2022
1 parent 89c46c6 commit 64d9028
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/sage/matrix/matrix2.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/sage/matrix/matrix_double_dense.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 64d9028

Please sign in to comment.