| matmult-methods | R Documentation |
The basic matrix product, %*% 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 “cross products”, ideally implemented
efficiently without computing t(.)'s unnecessarily.
They also return symmetricMatrix 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.
## S4 method for signature 'CsparseMatrix,diagonalMatrix'
x %*% y
## S4 method for signature 'CsparseMatrix,diagonalMatrix'
crossprod(x, y = NULL, boolArith = NA, ...)
## .... and for many more signatures
## S4 method for signature 'TsparseMatrix,missing'
tcrossprod(x, y = NULL, boolArith = NA, ...)
## .... and for many more signatures
x |
a matrix-like object |
y |
a matrix-like object, or for |
boolArith |
|
... |
potentially more arguments passed to and from methods. |
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.
boolArith = TRUE for regular (“non cross”) matrix
products, %*% cannot be specified. Instead, we provide the
%&% operator for boolean matrix products.
A Matrix object, in the one argument case
of an appropriate symmetric matrix class, i.e., inheriting from
symmetricMatrix.
signature(x = "dgeMatrix", y = "dgeMatrix"):
Matrix multiplication; ditto for several other signature
combinations, see showMethods("%*%", class = "dgeMatrix").
signature(x = "dtrMatrix", y = "matrix") and other
signatures (use showMethods("%*%", class="dtrMatrix")):
matrix multiplication. Multiplication of (matching) triangular
matrices now should remain triangular (in the sense of class
triangularMatrix).
signature(x = "dgeMatrix", y = "dgeMatrix"):
ditto for several other signatures, use
showMethods("crossprod", class = "dgeMatrix"), matrix
crossproduct, an efficient version of t(x) %*% y.
signature(x = "CsparseMatrix", y = "missing")
returns t(x) %*% x as an dsCMatrix object.
signature(x = "TsparseMatrix", y = "missing")
returns t(x) %*% x as an dsCMatrix object.
signature(x = "dtrMatrix", y =
"matrix") and other signatures, see "%*%" above.
boolArith = TRUE, FALSE or NA has been newly
introduced for Matrix 1.2.0 (March 2015). Its implementation
has still not been tested extensively. Notably the behaviour for
sparse matrices with x slots containing extra zeros had not been
documented previously, see the %&% help page.
Currently, boolArith = TRUE is implemented via
CsparseMatrix coercions which may be quite
inefficient for dense matrices. Contributions for efficiency
improvements are welcome.
tcrossprod in R's base, and
crossprod and %*%.
Matrix package %&% for boolean matrix product
methods.
## A random sparse "incidence" matrix :
m <- matrix(0, 400, 500)
set.seed(12)
m[runif(314, 0, length(m))] <- 1
mm <- as(m, "CsparseMatrix")
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]
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.