R/matrix-rank.R

Defines functions mtxrank

Documented in mtxrank

#' Matrix rank
#'
#' @param x      numeric matrix.
#' @param method method used for computing rank.
#' @param tol    the tolerance for detecting linear dependencies in the columns of x.
#'
#' @seealso
#' \code{\link{qr}}, \code{\link{chol}}, \code{\link{svd}}
#'
#' @export

mtxrank <- function(x, method = c("qr", "qr.lapack", "chol", "svd"), tol = 1e-7) {
  method <- match.arg(method)
  switch(method,
         "svd" = sum(svd(x)$d > tol),
         "chol" = attr(chol(x, pivot = TRUE, tol = tol), "rank"),
         "qr.lapack" = qr(x, LAPACK = TRUE, tol = tol)$rank,
         qr(x, tol = tol)$rank
  )
}
twolodzko/twextras documentation built on May 3, 2019, 1:52 p.m.