R/as.tensor.R

as.tensor <- function (x, drop = FALSE)
{
  stopifnot(is.array(x) || is.vector(x))
  if (is.vector(x)) {
    modes <- c(length(x))
    num_modes <- 1L
    new("Tensor", num_modes, modes, data = x)
  }
  else {
    modes <- dim(x)
    num_modes <- length(modes)
    dim1s <- which(modes == 1)
    if (drop && (length(dim1s) > 0)) {
      modes <- modes[-dim1s]
      num_modes <- num_modes - length(dim1s)
      new("Tensor", num_modes, modes, data = array(x, dim = modes))
    }
    else {
      new("Tensor", num_modes, modes, data = x)
    }
  }
}

Try the rTensor2 package in your browser

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

rTensor2 documentation built on May 29, 2024, 8:34 a.m.