R/is_symmetric.R

#' Tests whether the given matrix is symmetric.
#'
#' @param mat Matrix to be tested for symmetry.
#' @return Whether the matrix is symmetric.
#'
#' @export
is.symmetric <- function(mat) {
  if (!is.matrix(mat)) {
    stop("supplied argument is not a matrix")
  }

  if (nrow(mat) != ncol(mat)) {
    stop("given matrix is not square")
  }

  tmat <- t(mat)
  identical(mat[lower.tri(mat)], tmat[lower.tri(mat)])
}

Try the mp package in your browser

Any scripts or data that you put into this service are public.

mp documentation built on May 1, 2019, 10:19 p.m.