I have code for inverting large matrices and finding the log determinant in R. I really would like this code to run quicker but am struggling for anymore ideas. I have used the Cholesky decomposition and removed unwanted variables for memory.
Please can someone help with ways this code can be improved?
Woodbury_det1 <- function(A,B,C){
Ua2 <- chol(A) # A=t(U)xU, take U from this
Ub2 <- chol(B)
A_inv <- chol2inv(Ua2)# A^-1
B_inv <- chol2inv(Ub2) # B^-1
Ua2 <- chol(A_inv) # A^-1=U^tU
UC <- Ua2 %*% C # UxC
CA <- crossprod(UC,Ua2) # t(UxC)xU = t(C)xt(U)xU = t(C)xA^-1
BpCAC <- B_inv + crossprod(UC) # C^TxA^-1xC
BpCAC_ch = chol(BpCAC)
BpCAC_inv <- chol2inv(BpCAC_ch) # (B^-1 + t(C)xAxC)^-1
logdetA <- -2*sum(log(diag(Ua2)))
logdetB <- -2*sum(log(diag(Ub2)))
logdetbr <- 2*sum(log(diag(BpCAC_ch)))
logdetin <- logdetbr+logdetB+logdetA # final determinant calculation
V <- chol(BpCAC_inv)
VCA <- V %*% CA
ACYCA <- crossprod(VCA)
ACYCA <- as(ACYCA, "dspMatrix")
X <- A_inv- ACYCA
list(X,logdetin)}