Learn R Programming

Matrix (version 0.97-4)

tcrossprod: Cross-product of transpose

Description

Take the cross-product of the transpose of a matrix. This is formally equivalent to, but faster than, the call x %*% t(x).

Usage

tcrossprod(x)

Arguments

x
a matrix-like object

Value

  • An object of an appropriate symmetric matrix class.

Details

For some classes in the Matrix package, such as the dgCMatrix-class, it is much faster to calculate the cross-product of the transpose directly instead of calculating the transpose first and then its cross-product.

See Also

crossprod

Examples

Run this code
## A random sparce "incidence" matrix :
 m <- matrix(0, 400, 500)
 set.seed(12)
 m[runif(314, 0, length(m))] <- 1
 mm <- as(m, "dgCMatrix")
 object.size(m) / object.size(mm) # smaller by a factor of 242.88

 ## tcrossprod() is very fast:
 system.time(tCmm <- tcrossprod(mm))# "0" practically
 system.time(cm <- crossprod(t(m))) #

 stopifnot(identical(cm, as(tCmm, "matrix")))

 ## show "sparse" (sub) matrix
 tc <- cm[1:16, 1:30]
 storage.mode(tc) <- "character" ; tc[tc == "0"] <- "."
 dimnames(tc)[[2]] <- rep("", ncol(tc))
 noquote(tc)

Run the code above in your browser using DataLab