To enter the full rank, i.e. positively defined matrix x , we need
Q <- chol(x, TRUE) oo <- order(attr(Q, 'pivot')) unpivQ <- Q[, oo] all.equal(crossprod(unpivQ), x)
For an acceptable rank deficit of input, i.e. positive semidefinite matrix x (an indefinite matrix with negative eigenvalues ββis illegal, but not checked in chol ), do not forget the zero defective final diagonal block:
Q <- chol(x, TRUE) r <- attr(Q, 'rank') if (r < nrow(x)) Q[(r+1):nrow(x), (r+1):nrow(x)] <- 0 oo <- order(attr(Q, 'pivot')) unpivQ <- Q[, oo] all.equal(crossprod(unpivQ), x)
Some people call this the βerrorβ chol , but itβs actually a function of the basic LAPACK dpstrf procedure. Factorization continues until the first diagonal element, which is below the tolerance, leaving the back matrix just untouched upon exit.
Thanks to Jan for the following observation:
You can use negative indexing R in Q[-(1:r): -(1:r)] <- 0 to skip the if .
source share