18 lines
350 B
R
18 lines
350 B
R
|
#' Moore-Penrose Pseudo inverse
|
||
|
#'
|
||
|
#' @param A any matrix
|
||
|
#'
|
||
|
#' @returns another matrix
|
||
|
#'
|
||
|
#' @export
|
||
|
pinv <- function(A) {
|
||
|
A <- as.matrix(A)
|
||
|
if (nrow(A) < ncol(A)) {
|
||
|
crossprod(A, matpow(tcrossprod(A), -1))
|
||
|
} else if (nrow(A) > ncol(A)) {
|
||
|
tcrossprod(matpow(crossprod(A), -1), A)
|
||
|
} else {
|
||
|
matpow(A, -1)
|
||
|
}
|
||
|
}
|