R/psdmat_inverse.R

Defines functions psdmat_inverse

psdmat_inverse<-function(mat)
{
  # Originally defined for PSD matrices
  p = dim(mat)[1]
  eigendecomp = eigen(mat)
  
  #if(-min(eigendecomp$values)> 0.0001){print("Error: Matrix has negative eigenvalue!");stop()}
  
  if( min(eigendecomp$values)<=0.0001 )
  {
    message("Matrix has nearly zero eigenvalue, perturbation is added. ")
    # print(round(eigendecomp$values,3))
    perturb <- max(max(eigendecomp$values) - p * min(eigendecomp$values), 0)/(p - 1)
  }else
  {
    perturb = 0
  }

  return( (eigendecomp$vectors)%*%diag(1/(perturb+eigendecomp$values))%*%t(eigendecomp$vectors) )
}

Try the LOCUS package in your browser

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

LOCUS documentation built on Oct. 4, 2022, 9:06 a.m.