A <- matrix(c(1, 1, 0,
1, 1, 0,
2, 3, 4), 3, 3)
B <- MPinv(A)
A %*% B %*% A - A # essentially zero
B %*% A %*% B - B # essentially zero
attr(B, "rank") # here 2
## demonstration that "svd" and "chol" deliver essentially the same
## results for symmetric matrices:
A <- crossprod(A)
MPinv(A) - MPinv(A, method = "chol") ## (essentially zero)
## now a symmetric example with diagonal submatrix to `eliminate'
A <- matrix(c(1, 0, 2,
0, 2, 3,
2, 3, 4), 3, 3)
B <- MPinv(A, eliminate = 1:2)
A %*% B %*% A - A # essentially zero
B %*% A %*% B - B # essentially zero
attr(B, "rank") # here 3
## demo that eliminate can give substantial speed gains
A <- diag(rnorm(100))
A <- cbind(A, matrix(rnorm(200), 100, 2))
A <- rbind(A, cbind(t(A[, 101:102]), matrix(c(1, 2, 2, 1), 2, 2)))
system.time(for (i in 1:1000) B <- MPinv(A))
## [1] 30.85 0.06 30.91 0.00 0.00
system.time(for (i in 1:1000) B <- MPinv(A, eliminate = 1:100))
## [1] 3.10 0.00 3.11 0.00 0.00
Run the code above in your browser using DataLab