matrix-products

0th

Percentile

Matrix (Cross) Products (of Transpose)

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.

Keywords
methods, algebra
Usage
# S4 method for CsparseMatrix,diagonalMatrix
%*%(x, y)

# S4 method for dgeMatrix,missing crossprod(x, y = NULL, boolArith = NA, …) # S4 method for CsparseMatrix,diagonalMatrix crossprod(x, y = NULL, boolArith = NA, …) ## .... and for many more signatures

# S4 method for CsparseMatrix,ddenseMatrix tcrossprod(x, y = NULL, boolArith = NA, …) # S4 method for TsparseMatrix,missing tcrossprod(x, y = NULL, boolArith = NA, …) ## .... and for many more signatures

Arguments
x

a matrix-like object

y

a matrix-like object, or for [t]crossprod() NULL (by default); the latter case is formally equivalent to y = x.

boolArith

logical, i.e., NA, TRUE, or FALSE. If true the result is (coerced to) a pattern matrix, i.e., "'>nMatrix", unless there are NA entries and the result will be a "'>lMatrix". If false the result is (coerced to) numeric. When NA, currently the default, the result is a pattern matrix when x and y are "'>nsparseMatrix" and numeric otherwise.

potentially more arguments passed to and from methods.

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.

boolArith = TRUE for regular (“non cross”) matrix products, %*% cannot be specified. Instead, we provide the %&% operator for boolean matrix products.

Value

A '>Matrix object, in the one argument case of an appropriate symmetric matrix class, i.e., inheriting from '>symmetricMatrix.

Note

boolArith = TRUE, FALSE or NA has been newly introduced for Matrix 1.2.0 (March 2015). Its implementation may be incomplete and partly missing. Please report such omissions if detected!

Currently, boolArith = TRUE is implemented via '>CsparseMatrix coercions which may be quite inefficient for dense matrices. Contributions for efficiency improvements are welcome.

Methods

%*%

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).

crossprod

signature(x = "dgeMatrix", y = "dgeMatrix"): ditto for several other signatures, use showMethods("crossprod", class = "dgeMatrix"), matrix crossproduct, an efficient version of t(x) %*% y.

crossprod

signature(x = "CsparseMatrix", y = "missing") returns t(x) %*% x as an dsCMatrix object.

crossprod

signature(x = "TsparseMatrix", y = "missing") returns t(x) %*% x as an dsCMatrix object.

crossprod,tcrossprod

signature(x = "dtrMatrix", y = "matrix") and other signatures, see "%*%" above.

See Also

tcrossprod in R's base, crossprod and %*%.

Aliases
  • %*%-methods
  • crossprod-methods
  • tcrossprod-methods
  • %*%,ddenseMatrix,ldenseMatrix-method
  • %*%,ddenseMatrix,matrix-method
  • %*%,ddenseMatrix,ndenseMatrix-method
  • %*%,ldenseMatrix,ddenseMatrix-method
  • %*%,ldenseMatrix,ldenseMatrix-method
  • %*%,ldenseMatrix,matrix-method
  • %*%,ldenseMatrix,ndenseMatrix-method
  • %*%,matrix,ddenseMatrix-method
  • %*%,matrix,ldenseMatrix-method
  • %*%,matrix,ndenseMatrix-method
  • %*%,ndenseMatrix,ddenseMatrix-method
  • %*%,ndenseMatrix,ldenseMatrix-method
  • %*%,ndenseMatrix,matrix-method
  • %*%,ndenseMatrix,ndenseMatrix-method
  • %*%,dgeMatrix,dgeMatrix-method
  • %*%,dgeMatrix,matrix-method
  • %*%,dgeMatrix,numLike-method
  • %*%,matrix,dgeMatrix-method
  • %*%,numLike,dgeMatrix-method
  • %*%,numLike,CsparseMatrix-method
  • %*%,CsparseMatrix,CsparseMatrix-method
  • %*%,CsparseMatrix,ddenseMatrix-method
  • %*%,CsparseMatrix,matrix-method
  • %*%,CsparseMatrix,numLike-method
  • %*%,ddenseMatrix,CsparseMatrix-method
  • %*%,matrix,CsparseMatrix-method
  • %*%,matrix,sparseMatrix-method
  • %*%,sparseMatrix,matrix-method
  • %*%,ddenseMatrix,ddenseMatrix-method
  • %*%,dgeMatrix,diagonalMatrix-method
  • %*%,matrix,diagonalMatrix-method
  • %*%,diagonalMatrix,dgeMatrix-method
  • %*%,diagonalMatrix,diagonalMatrix-method
  • %*%,diagonalMatrix,matrix-method
  • %*%,diagonalMatrix,CsparseMatrix-method
  • %*%,diagonalMatrix,sparseMatrix-method
  • %*%,CsparseMatrix,diagonalMatrix-method
  • %*%,sparseMatrix,diagonalMatrix-method
  • %*%,denseMatrix,diagonalMatrix-method
  • %*%,diagonalMatrix,denseMatrix-method
  • %*%,diagonalMatrix,lgeMatrix-method
  • %*%,lgeMatrix,diagonalMatrix-method
  • %*%,nMatrix,lMatrix-method
  • %*%,lMatrix,nMatrix-method
  • %*%,lMatrix,lMatrix-method
  • %*%,nMatrix,nMatrix-method
  • %*%,dMatrix,lMatrix-method
  • %*%,dMatrix,nMatrix-method
  • %*%,lMatrix,dMatrix-method
  • %*%,nMatrix,dMatrix-method
  • %*%,dMatrix,integer-method
  • %*%,integer,dMatrix-method
  • %*%,Matrix,numLike-method
  • %*%,numLike,Matrix-method
  • %*%,Matrix,ANY-method
  • %*%,ANY,Matrix-method
  • %*%,Matrix,matrix-method
  • %*%,matrix,Matrix-method
  • %*%,ddenseMatrix,dsparseMatrix-method
  • %*%,dgeMatrix,dsparseMatrix-method
  • %*%,dsparseMatrix,ddenseMatrix-method
  • %*%,dsparseMatrix,dgeMatrix-method
  • %*%,ddenseMatrix,dsyMatrix-method
  • %*%,matrix,dsyMatrix-method
  • %*%,dsyMatrix,dsyMatrix-method
  • %*%,dspMatrix,ddenseMatrix-method
  • %*%,dspMatrix,matrix-method
  • %*%,dsyMatrix,ddenseMatrix-method
  • %*%,dsyMatrix,matrix-method
  • %*%,dtpMatrix,ddenseMatrix-method
  • %*%,dgeMatrix,dtpMatrix-method
  • %*%,dtpMatrix,matrix-method
  • %*%,matrix,dtpMatrix-method
  • %*%,dtrMatrix,dtrMatrix-method
  • %*%,ddenseMatrix,dtrMatrix-method
  • %*%,dtrMatrix,ddenseMatrix-method
  • %*%,dtrMatrix,matrix-method
  • %*%,matrix,dtrMatrix-method
  • %*%,matrix,indMatrix-method
  • %*%,indMatrix,matrix-method
  • %*%,indMatrix,indMatrix-method
  • %*%,Matrix,indMatrix-method
  • %*%,indMatrix,Matrix-method
  • %*%,lgCMatrix,lgCMatrix-method
  • %*%,lsparseMatrix,lsparseMatrix-method
  • %*%,lsparseMatrix,ldenseMatrix-method
  • %*%,ldenseMatrix,lsparseMatrix-method
  • %*%,ngCMatrix,ngCMatrix-method
  • %*%,nsparseMatrix,nsparseMatrix-method
  • %*%,nsparseMatrix,ndenseMatrix-method
  • %*%,ndenseMatrix,nsparseMatrix-method
  • %*%,matrix,pMatrix-method
  • %*%,pMatrix,matrix-method
  • %*%,pMatrix,pMatrix-method
  • %*%,Matrix,pMatrix-method
  • %*%,pMatrix,Matrix-method
  • %*%,mMatrix,sparseVector-method
  • %*%,sparseVector,mMatrix-method
  • %*%,sparseVector,sparseVector-method
  • %*%,numLike,sparseVector-method
  • %*%,sparseVector,numLike-method
  • %*%,TsparseMatrix,ANY-method
  • %*%,ANY,TsparseMatrix-method
  • %*%,TsparseMatrix,Matrix-method
  • %*%,Matrix,TsparseMatrix-method
  • %*%,TsparseMatrix,TsparseMatrix-method
  • crossprod-methods
  • crossprod,dgeMatrix,dgeMatrix-method
  • crossprod,dgeMatrix,matrix-method
  • crossprod,dgeMatrix,numLike-method
  • crossprod,dgeMatrix,missing-method
  • crossprod,matrix,dgeMatrix-method
  • crossprod,numLike,dgeMatrix-method
  • crossprod,numLike,CsparseMatrix-method
  • crossprod,CsparseMatrix,missing-method
  • crossprod,CsparseMatrix,CsparseMatrix-method
  • crossprod,CsparseMatrix,ddenseMatrix-method
  • crossprod,CsparseMatrix,matrix-method
  • crossprod,CsparseMatrix,numLike-method
  • crossprod,ddenseMatrix,CsparseMatrix-method
  • crossprod,matrix,CsparseMatrix-method
  • crossprod,ddenseMatrix,missing-method
  • crossprod,ddenseMatrix,dgCMatrix-method
  • crossprod,dgCMatrix,dgeMatrix-method
  • crossprod,CsparseMatrix,diagonalMatrix-method
  • crossprod,diagonalMatrix,CsparseMatrix-method
  • crossprod,diagonalMatrix,dgeMatrix-method
  • crossprod,diagonalMatrix,diagonalMatrix-method
  • crossprod,diagonalMatrix,lgeMatrix-method
  • crossprod,diagonalMatrix,matrix-method
  • crossprod,diagonalMatrix,missing-method
  • crossprod,diagonalMatrix,sparseMatrix-method
  • crossprod,sparseMatrix,diagonalMatrix-method
  • crossprod,ANY,Matrix-method
  • crossprod,Matrix,numLike-method
  • crossprod,numLike,Matrix-method
  • crossprod,Matrix,ANY-method
  • crossprod,Matrix,missing-method
  • crossprod,Matrix,Matrix-method
  • crossprod,Matrix,matrix-method
  • crossprod,matrix,Matrix-method
  • crossprod,matrix,diagonalMatrix-method
  • crossprod,ddenseMatrix,dsparseMatrix-method
  • crossprod,dgeMatrix,dsparseMatrix-method
  • crossprod,dsparseMatrix,ddenseMatrix-method
  • crossprod,dsparseMatrix,dgeMatrix-method
  • crossprod,dtpMatrix,ddenseMatrix-method
  • crossprod,dtpMatrix,matrix-method
  • crossprod,dtrMatrix,ddenseMatrix-method
  • crossprod,dtrMatrix,dtrMatrix-method
  • crossprod,dtrMatrix,matrix-method
  • crossprod,matrix,dtrMatrix-method
  • crossprod,dtrMatrix,missing-method
  • crossprod,indMatrix,matrix-method
  • crossprod,indMatrix,Matrix-method
  • crossprod,indMatrix,indMatrix-method
  • crossprod,indMatrix,missing-method
  • crossprod,lgCMatrix,missing-method
  • crossprod,lgTMatrix,missing-method
  • crossprod,lsparseMatrix-method
  • crossprod,lsparseMatrix,missing-method
  • crossprod,lsparseMatrix,lsparseMatrix-method
  • crossprod,lsparseMatrix,ldenseMatrix-method
  • crossprod,ldenseMatrix,lsparseMatrix-method
  • crossprod,ngCMatrix,missing-method
  • crossprod,ngTMatrix,missing-method
  • crossprod,nsparseMatrix-method
  • crossprod,nsparseMatrix,missing-method
  • crossprod,nsparseMatrix,nsparseMatrix-method
  • crossprod,nsparseMatrix,ndenseMatrix-method
  • crossprod,ndenseMatrix,nsparseMatrix-method
  • crossprod,pMatrix,matrix-method
  • crossprod,pMatrix,Matrix-method
  • crossprod,pMatrix,pMatrix-method
  • crossprod,pMatrix,missing-method
  • crossprod,mMatrix,sparseVector-method
  • crossprod,sparseVector,mMatrix-method
  • crossprod,sparseVector,missing-method
  • crossprod,sparseVector,sparseVector-method
  • crossprod,numLike,sparseVector-method
  • crossprod,sparseVector,numLike-method
  • crossprod,TsparseMatrix,ANY-method
  • crossprod,ANY,TsparseMatrix-method
  • crossprod,TsparseMatrix,Matrix-method
  • crossprod,Matrix,TsparseMatrix-method
  • crossprod,TsparseMatrix,TsparseMatrix-method
  • crossprod,TsparseMatrix,missing-method
  • crossprod,symmetricMatrix,Matrix-method
  • crossprod,symmetricMatrix,missing-method
  • crossprod,symmetricMatrix,ANY-method
  • crossprod,ddenseMatrix,ddenseMatrix-method
  • crossprod,ddenseMatrix,ldenseMatrix-method
  • crossprod,ddenseMatrix,matrix-method
  • crossprod,ddenseMatrix,ndenseMatrix-method
  • crossprod,ldenseMatrix,ddenseMatrix-method
  • crossprod,ldenseMatrix,ldenseMatrix-method
  • crossprod,ldenseMatrix,matrix-method
  • crossprod,ldenseMatrix,missing-method
  • crossprod,ldenseMatrix,ndenseMatrix-method
  • crossprod,ndenseMatrix,ddenseMatrix-method
  • crossprod,ndenseMatrix,ldenseMatrix-method
  • crossprod,ndenseMatrix,matrix-method
  • crossprod,ndenseMatrix,missing-method
  • crossprod,ndenseMatrix,ndenseMatrix-method
  • tcrossprod-methods
  • tcrossprod,dgeMatrix,missing-method
  • tcrossprod,dgeMatrix,dgeMatrix-method
  • tcrossprod,dgeMatrix,matrix-method
  • tcrossprod,dgeMatrix,numLike-method
  • tcrossprod,matrix,dgeMatrix-method
  • tcrossprod,numLike,dgeMatrix-method
  • tcrossprod,CsparseMatrix,ddenseMatrix-method
  • tcrossprod,CsparseMatrix,matrix-method
  • tcrossprod,CsparseMatrix,numLike-method
  • tcrossprod,ddenseMatrix,CsparseMatrix-method
  • tcrossprod,matrix,CsparseMatrix-method
  • tcrossprod,numLike,CsparseMatrix-method
  • tcrossprod,CsparseMatrix,CsparseMatrix-method
  • tcrossprod,CsparseMatrix,missing-method
  • tcrossprod,ddenseMatrix,missing-method
  • tcrossprod,CsparseMatrix,diagonalMatrix-method
  • tcrossprod,dgeMatrix,diagonalMatrix-method
  • tcrossprod,diagonalMatrix,CsparseMatrix-method
  • tcrossprod,diagonalMatrix,diagonalMatrix-method
  • tcrossprod,diagonalMatrix,matrix-method
  • tcrossprod,diagonalMatrix,missing-method
  • tcrossprod,diagonalMatrix,sparseMatrix-method
  • tcrossprod,lgeMatrix,diagonalMatrix-method
  • tcrossprod,matrix,diagonalMatrix-method
  • tcrossprod,sparseMatrix,diagonalMatrix-method
  • tcrossprod,ANY,Matrix-method
  • tcrossprod,numLike,Matrix-method
  • tcrossprod,Matrix,numLike-method
  • tcrossprod,Matrix,ANY-method
  • tcrossprod,Matrix,missing-method
  • tcrossprod,Matrix,Matrix-method
  • tcrossprod,Matrix,matrix-method
  • tcrossprod,matrix,Matrix-method
  • tcrossprod,ddenseMatrix,dtrMatrix-method
  • tcrossprod,dtrMatrix,dtrMatrix-method
  • tcrossprod,matrix,dtrMatrix-method
  • tcrossprod,matrix,indMatrix-method
  • tcrossprod,Matrix,indMatrix-method
  • tcrossprod,indMatrix,indMatrix-method
  • tcrossprod,indMatrix,missing-method
  • tcrossprod,lgCMatrix,missing-method
  • tcrossprod,lgTMatrix,missing-method
  • tcrossprod,lsparseMatrix,missing-method
  • tcrossprod,ngCMatrix,missing-method
  • tcrossprod,ngTMatrix,missing-method
  • tcrossprod,nsparseMatrix,missing-method
  • tcrossprod,matrix,pMatrix-method
  • tcrossprod,Matrix,pMatrix-method
  • tcrossprod,pMatrix,pMatrix-method
  • tcrossprod,pMatrix,missing-method
  • tcrossprod,mMatrix,sparseVector-method
  • tcrossprod,sparseMatrix,sparseVector-method
  • tcrossprod,sparseVector,mMatrix-method
  • tcrossprod,sparseVector,missing-method
  • tcrossprod,sparseVector,sparseMatrix-method
  • tcrossprod,sparseVector,sparseVector-method
  • tcrossprod,numLike,sparseVector-method
  • tcrossprod,sparseVector,numLike-method
  • tcrossprod,TsparseMatrix,ANY-method
  • tcrossprod,ANY,TsparseMatrix-method
  • tcrossprod,TsparseMatrix,Matrix-method
  • tcrossprod,Matrix,TsparseMatrix-method
  • tcrossprod,TsparseMatrix,TsparseMatrix-method
  • tcrossprod,TsparseMatrix,missing-method
  • tcrossprod,Matrix,symmetricMatrix-method
  • tcrossprod,ANY,symmetricMatrix-method
  • tcrossprod,ddenseMatrix,ddenseMatrix-method
  • tcrossprod,ddenseMatrix,ldenseMatrix-method
  • tcrossprod,ddenseMatrix,matrix-method
  • tcrossprod,ddenseMatrix,ndenseMatrix-method
  • tcrossprod,ldenseMatrix,ddenseMatrix-method
  • tcrossprod,ldenseMatrix,ldenseMatrix-method
  • tcrossprod,ldenseMatrix,matrix-method
  • tcrossprod,ldenseMatrix,missing-method
  • tcrossprod,ldenseMatrix,ndenseMatrix-method
  • tcrossprod,ndenseMatrix,ddenseMatrix-method
  • tcrossprod,ndenseMatrix,ldenseMatrix-method
  • tcrossprod,ndenseMatrix,matrix-method
  • tcrossprod,ndenseMatrix,missing-method
  • tcrossprod,ndenseMatrix,ndenseMatrix-method
  • tcrossprod,ddenseMatrix,dsCMatrix-method
  • tcrossprod,ddenseMatrix,lsCMatrix-method
  • tcrossprod,ddenseMatrix,nsCMatrix-method
  • tcrossprod,matrix,dsCMatrix-method
  • tcrossprod,matrix,lsCMatrix-method
  • tcrossprod,matrix,nsCMatrix-method
  • %*%
  • crossprod
  • tcrossprod
Examples
# NOT RUN {
 ## 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]
# }
Documentation reproduced from package Matrix, version 1.2-15, License: GPL (>= 2) | file LICENCE

Community examples

Looks like there are no examples yet.