R/svd.r

Defines functions svd2

Documented in svd2

#' SVD
#' 
#' Singular value decomposition.
#' 
#' @param x
#' The input matrix or dataframe.
#' @param nu
#' Number of left singular vectors to return.
#' @param nv
#' Number of right singular vectors to return.
#' 
#' @rdname svd
#' @export
svd2 <- function(x, nu=min(n, p), nv=min(n, p))
{
  n <- nrow(x)
  p <- ncol(x)

  assert.type(x, "numeric")
  assert.natnum(nu)
  assert.natnum(nv)

  if (!is.matrix(x))
    x <- as.matrix(x)
  
  if (!is.double(x))
    storage.mode(x) <- "double"
  
  ret <- .Call(R_pcapack_svd, as.integer(nu), as.integer(nv), x)
  
  k <- min(dim(x))
  
  if (nu)
  {
    if (nu != k && nu != n)
      ret$u <- ret$u[, 1L:nu]
  }
  
  if (nv)
  {
    if (nv != k && nv != p)
      ret$vt <- ret$vt[1L:nv, ]
  }
  
  return( ret )
}
wrathematics/pcapack documentation built on May 4, 2019, 10:53 a.m.