R/base.R

Defines functions chol_solve

Documented in chol_solve

#' Invert Symmetric Positive Definite Matrix via Cholesky Decomposition
#'
#' Consider a matrix that is symmetric and has positive eigenvalues, and is
#'   therefore positive definite. Then finds the inverse of the cholesky
#'   decomposition of this matrix as a more numerically stable alternative
#'   to `base::solve(x)`.
#'
#' @param x matrix. symmetric (up to `tol`) and positive definite.
#' @param tol numeric. Differences of symmetry smaller than this value
#'   are not considered significant.
#'
#' @return the matrix inverse of `x`.
#'
#' @export
chol_solve <- function(x, tol = 100 * .Machine$double.eps) {
  stopifnot(
    "x is not symmetric" = isSymmetric(x, tol),
    "x is not positive definite" = all(eigen(x)$values > tol)
  )
  return(chol2inv(chol(x)))
}
nclJoshCowley/jcutils documentation built on Nov. 22, 2022, 10:16 a.m.