tensor_predictors/tensorPredictors/R/mcrossprod.R

22 lines
689 B
R
Raw Normal View History

#' Tensor Mode Crossproduct
#'
#' C = A_(m) t(A_(m))
#'
#' For a matrix `A`, the first mode is `mcrossprod(A, 1)` equivalent to
#' `A %*% t(A)` (`tcrossprod`). On the other hand for mode two `mcrossprod(A, 2)`
#' the equivalence is `t(A) %*% A` (`crossprod`).
#'
#' @param A multi-dimensional array
#' @param mode index (1-indexed)
#'
#' @returns matrix of dimensions \code{dim(A)[mode] by dim(A)[mode]}.
#'
#' @note equivalent to \code{tcrossprod(mat(A, mode))} with around the same
#' performance but only allocates the result matrix.
#'
#' @export
mcrossprod <- function(A, mode = length(dim(A))) {
storage.mode(A) <- "double"
.Call("C_mcrossprod", A, as.integer(mode))
}