R/utils.R

Defines functions vect_norm force_symmetry sqrt_diag mean_sqrt_diag matrix_power remove_zeros

Documented in force_symmetry matrix_power mean_sqrt_diag remove_zeros sqrt_diag vect_norm

#' Replace a Value in a vector/array
#'
#' @param x the array
#' @param rem the value to replace, default 0
#' @param rep the value to replace with, default 1
#' @return  the array with rem replaces with rep
#' @export
remove_zeros <- function(x, rem = 0, rep = 1){
  x[x == rem] <- rep
  return(x)
}


#' Raise a Square Matrix to a Non-Integer Power
#'
#' Raise a Square Matrix to a non-integer power using it's Spectral Decomposition
#' @param x a square matrix
#' @param power a real
#' @return the matrix raised to the power
#' @export
matrix_power <- function(x, power){
  eig <- eigen(x)
  out <- eig$vectors %*% diag(eig$values ^ power) %*% t(eig$vectors)
  return(out)
}


#' Get the Average of the Square-rooted Diagonal of a Matrix
#' @param x a square matrix
#' @export
mean_sqrt_diag <- function(x) return(mean(sqrt(diag(x))))


#' Get the Square-rooted Diagonal of a Matrix
#' @param x a square matrix
#' @export
sqrt_diag <- function(x) return(sqrt(diag(x)))


#' Force a Matrix to be Symmetrical
#'
#' Force a matrix to be symmetrical by averaging between the matrix and it's transpose
#' @param matr the matrix to enforce symmetry
#' @export
force_symmetry <- function(matr) return(0.5 * (t(matr) + matr))


#' Calculate the Mahalonobis Norm of a Vector
#' @param x a vector
#' @param matr the weighting matrix in the Mahalonobis norm. if null, the l2 norm is calculated.
#' @param sqrt if false, don't take the sqrt of the product
#' @param solve_matr if true, invert matr before calculating the norm
#' @return the norm, a real number >= 0
#' @export
vect_norm <- function(x, matr = NULL, sqrt = TRUE, solve_matr = FALSE)
{
  if(is.null(matr)){
    out <- sum(x^2)
  } else {
    if(solve_matr)
      matr <- solve(matr)
    out <- as.vector(t(x) %*% matr %*% x)
  }

  if(sqrt)
    out <- sqrt(out)

  return(out)
}
itamarfaran/corrfuncs documentation built on Nov. 26, 2021, 12:02 p.m.