R/is_invertible.R

Defines functions is_invertible

Documented in is_invertible

# ---------------------------------------------------------------------------- #
#' Check if a matrix is invertible
#'
#' Checks a matrix to see if it is invertible.
#'
#' @param x A numeric matrix which should be checked to see if it is invertible.
#'
#' @return TRUE if the supplied matrix is invertible, FALSE otherwise.
#'
#' @examples
#' is_invertible(matrix(c(1, 2, 2, 1), 2, 2)) # => TRUE
#'
#' is_invertible(matrix(c(1, 1, 1, 1), 2, 2)) # => FALSE
#'
#' @export
#'
is_invertible <- function(x) {
  res <- tryCatch(
    {
      solve(x)
    },
    error = function(e) {
      e
    }
  )

  if (inherits(res, "error")) {
    FALSE
  } else {
    TRUE
  }
}

# ---------------------------------------------------------------------------- #
toniprice/jute documentation built on Jan. 11, 2023, 8:23 a.m.