R/matrix-norms.r

#' @title Matrix Norms
#' 
#' @description
#' Use \code{one_norm} to get the \code{p=1} norm of a matrix \cr
#' Use \code{max_norm} to get the maximum norm of a matrix \cr
#' Use \code{inf_norm} to get the infinit norm of a matrix
#' 
#' @details
#' one norm is the maximum absolute column sum of a matrix \cr
#' max norm is the elementwise norm \cr
#' infinit norm is the maximum absolute row sum of a matrix 
#' 
#' @param x a numeric matrix
#' @return the calculated norm
#' @export one_norm max_norm inf_norm
#' @aliases one_norm max_norm inf_norm
#' @examples
#' # matrix
#' M = matrix(1:9, 3, 3)
#' 
#' # one norm
#' one_norm(M)
#' 
#' # max norm
#' max_norm(M)
#' 
#' # inf norm
#' inf_norm(M)
one_norm <- function(x)
{
  if (!is.numeric(x)) 
    stop("'one_norm()' requires a numeric matrix (or vector)")
  
  if (is_vector(x)) {
    return(sum(abs(x)))
  }
  if (is_matrix(x)) {
    # absolute column sum
    abs_col_sum = apply(abs(x), 2, sum)
    return(max(abs_col_sum))    
  }
}

max_norm <- function(x)
{
  if (!is.numeric(x)) 
    stop("'max_norm()' requires a numeric matrix (or vector)")
  
  # output
  max(abs(x))
}

inf_norm <- function(x)
{
  if (!is.numeric(x)) 
    stop("'inf_norm()' requires a numeric matrix (or vector)")
  
  if (is_vector(x)) {
    return(sum(abs(x)))
  }
  if (is_matrix(x)) {
    # absolute row sum
    abs_row_sum = apply(abs(x), 1, sum)
    return(max(abs_row_sum)) 
  }
}


#' @title Euclidean Norm of a Matrix
#' 
#' @description Computes the euclidean (aka Frobenius) norm of a matrix
#' 
#' @param x a numeric matrix
#' @return norm of x
#' @export
#' @seealso \code{\link{vnorm}}
#' @examples
#' M1 = matrix(1:9, 3, 3)
#' 
#' # norm of M1
#' mnorm(M1)
#' 
#' # one-dim (one row) matrix
#' M2 = matrix(1:15, 3, 5)
#' 
#' # norm of row1
#' mnorm(M2)
mnorm <- function(x) 
{
  if (!is_numeric_matrix(x))
    stop("\n'mnorm()' requires a numeric matrix")
  # output
  norm(x, "F")
}
gastonstat/matrixkit documentation built on May 16, 2019, 5:45 p.m.