%*%
is implemented for all our
Matrix
and also for
sparseVector
classes, fully analogously to R's
base matrix
and vector objects. The functions crossprod
and tcrossprod
are
matrix products or t(.)
's unnecessarily.
They also return
classed
matrices when easily detectable, e.g., in crossprod(m)
, the one
argument case.
tcrossprod()
takes 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)
.
Boolean matrix products are computed via either
%&%
or boolArith = TRUE
.
## S3 method for class 'CsparseMatrix,diagonalMatrix':
\%*\%(x, y)## S3 method for class 'dgeMatrix,missing':
crossprod(x, y = NULL, boolArith = NA, \dots)
## S3 method for class 'CsparseMatrix,diagonalMatrix':
crossprod(x, y = NULL, boolArith = NA, \dots)
## .... and for many more signatures
## S3 method for class 'CsparseMatrix,ddenseMatrix':
tcrossprod(x, y = NULL, boolArith = NA, \dots)
## S3 method for class 'TsparseMatrix,missing':
tcrossprod(x, y = NULL, boolArith = NA, \dots)
## .... and for many more signatures
[t]crossprod()
NULL
(by default); the latter case is formally equivalent to
y = x
.logical
, i.e., NA
, TRUE
,
or FALSE
. If true the result is (coerced to) a pattern
matrix, i.e., "nMatrix "
, unless there Matrix
object, in the one argument case
of an appropriate symmetric matrix class.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. boolArith = TRUE
for regular (%*%
cannot be specified. Instead, we provide the
%&%
operator for boolean matrix products.
tcrossprod
in R's base,
crossprod
and %*%
.## A random sparse "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]
Run the code above in your browser using DataLab