Skip to content

Commit

Permalink
Trac #33107: Generic cholesky() fails on the trivial matrix
Browse files Browse the repository at this point in the history
{{{
sage: A = matrix(QQ,[])
sage: A.cholesky()
...
IndexError: matrix index out of range
}}}

If we simply return `A` itself in this case, the definition of the
decomposition is satisfied.

URL: https://trac.sagemath.org/33107
Reported by: mjo
Ticket author(s): Michael Orlitzky
Reviewer(s): Markus Wageringel
  • Loading branch information
Release Manager committed Feb 12, 2022
2 parents 88a98d5 + 64d9028 commit 9cc853b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/sage/matrix/matrix2.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -12556,6 +12556,16 @@ cdef class Matrix(Matrix1):
sage: A.cholesky()
[ 1.0 0.0]
[-1.0*I 1.0]

Try the trivial case (:trac:`33107`)::

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
C = self.fetch('cholesky')
Expand All @@ -12567,6 +12577,14 @@ cdef class Matrix(Matrix1):
if not self.is_hermitian():
raise ValueError("matrix is not Hermitian")

if n == 0:
# The trivial matrix has a trivial cholesky decomposition.
# We special-case this after is_hermitian() to ensure that
# the matrix is square.
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
cdef list d # 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 @@ -3698,7 +3698,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 9cc853b

Please sign in to comment.