R/vech2m.R

Defines functions vech2m

Documented in vech2m

##' Convert from the vech vector to the full matrix.
##'
##'
##' @export
vech2m <- function(vech, diag = TRUE)
{
  c <- length(vech)
  p <- (1 - 2*diag +sqrt(8*c+1))/2 # n^2 -n  =  2c; or n^2 + n  =  2c
  if(!identical(p,  round(p)))
  {
    stop ("Input vector length not match with the length of matrix triangular.")
  }
  out <- matrix(1, p, p)
  idx.lower <- as.vector(lower.tri(out, diag = diag))
  out[idx.lower] <- vech
  idx.upper <- as.vector(upper.tri(out, diag = diag))
  out.t <- t(out)
  out[idx.upper] <- out.t[idx.upper]
  return(out)
}
thiyangt/fformpp documentation built on Jan. 5, 2024, 5:44 a.m.