tcrossprod {Matrix}R Documentation

Cross-product of transpose

Description

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

Usage

tcrossprod(x, y = NULL)

Arguments

x

a matrix-like object

y

a matrix-like object or NULL (by default); the latter case is formally equivalent to y = x.

Details

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

Value

An object of an appropriate symmetric matrix class.

Methods

x = "dgCMatrix"

method for compressed, sparse, column-oriented matrices.

See Also

crossprod

Examples

 ## 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 > 200

 ## tcrossprod() is very fast:
 system.time(tCmm <- tcrossprod(mm))# 0   (PIII, 933 MHz)
 system.time(cm <- crossprod(t(m))) # 0.16
 system.time(cm. <- tcrossprod(m))  # 0.02

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

 ## show sparse sub matrix
 tCmm[1:16, 1:30]

[Package Matrix version 1.0-6 Index]