R/tr.R

#' Calculate trace of a square matrix
#'
#' @param m square matrix to calculate trace of
#'
#' @return trace of given matrix
#' @export
#'

tr <- function (m)
{
  total_sum <- 0
  if(is.matrix(m))
  {
    row_count <- nrow(m)
    col_count <- ncol(m)
    if(row_count == col_count)
    {
      total_sum <-sum(diag(m))
      total_sum
    }
    else
    {
      message ('Matrix is not square')
    }
  }
  else
  {
    message( 'Object is not a matrix')

  }
}
jlivsey/EMsignal documentation built on March 17, 2024, 5:12 p.m.