# FIND CHOLESKY DECOMPOSITION OF A SYMMETRIC, POSITIVE DEFINITE MATRIX. # # R. M. Neal, January 2002 # FIND CHOLESKY DECOMPOSITION. Given a square matrix A that is symmetric # and positive definite, this function finds an upper-triangular matrix U # of the same dimensions for which t(U) %*% U = A. # # This function is just for illustration. The built-in function "chol" # will do the same thing faster. cholesky <- function (A) { if (!is.matrix(A) || nrow(A)!=ncol(A)) { stop("The argument for cholesky must be a square matrix") } p <- nrow(A) U <- matrix(0,p,p) for (i in 1:p) { if (i==1) { U[i,i] <- sqrt (A[i,i]) } else { U[i,i] <- sqrt (A[i,i] - sum(U[1:(i-1),i]^2)) } if (i