tensor_predictors/tensorPredictors/R/pinv.R

18 lines
350 B
R
Raw Normal View History

#' 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)
}
}