R/tr.R

Defines functions tr

Documented in tr

#' Matrix Trace
#'
#' @family matrix
#' @keywords matrix
#'
#' @description Computes the trace of a square numeric matrix \emph{A}.
#'
#' @note If the argument is not a square numeric matrix, the function presents an error and
#'      terminates.
#'
#' @param A Square matrix.
#'
#' @examples
#' A <- matrix(seq(1, 16, 1), nrow = 4, byrow = TRUE)
#' A
#' tr(A)
#' tr(I(3))
#' @export

tr <- function(A) {
  # if( !is.numeric(A) & !is.matrix(A) )
  #      stop(paste( "A", "must be a numeric matrix."))
  if (nrow(A) != ncol(A)) {
    stop(paste("A", "is not a square matrix"))
  }
  stopifnot(nrow(A) == ncol(A))
  sum(diag(A))
}

Try the matrixNormal package in your browser

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

matrixNormal documentation built on Sept. 16, 2022, 5:07 p.m.