## Moore Penrose --------------------------------------------------------------
set.seed(1824)
M <- 11
L <- 5
diag_lambda <- rchisq(M, df = 1)
Lambda <- diag(diag_lambda)
U <- matrix(rnorm(M * L), nrow = M)
V <- U %*% t(U)
LHS <- solve(diag(M) + V %*% Lambda) %*% V
ULU <- t(U) %*% Lambda %*% U
middle <- V - U %*% solve(diag(L) + ULU ) %*% ULU %*% t(U)
RHS <- V - U %*% solve(solve(t(U) %*% Lambda %*% U) + diag(L)) %*% t(U)
plot(LHS, RHS)
plot(middle, LHS)
plot(middle, RHS)
Now test for large M
rm(list = ls())
M <- 2000
L <- 11
diag_lambda <- rchisq(M, df = 1)
Lambda <- diag(diag_lambda)
U <- matrix(rnorm(M * L), nrow = M)
V <- U %*% t(U)
Sys.sleep(1)
lstart <- proc.time()
LHS <- solve(diag(M) + V %*% Lambda) %*% V
ltot <- proc.time() - lstart
mstart <- proc.time()
ULU <- t(U) %*% Lambda %*% U
middle <- V - U %*% solve(diag(L) + ULU ) %*% ULU %*% t(U)
mtot <- proc.time() - mstart
rstart <- proc.time()
RHS <- V - U %*% solve(solve(t(U) %*% Lambda %*% U) + diag(L)) %*% t(U)
rtot <- proc.time() - rstart
ltot
mtot
rtot