# crossprod

0th

Percentile

##### Matrix Crossproduct

Given matrices x and y as arguments, return a matrix cross-product. This is formally equivalent to (but usually slightly faster than) the call t(x) %*% y (crossprod) or x %*% t(y) (tcrossprod).

Keywords
algebra, array
##### Usage
crossprod(x, y = NULL)tcrossprod(x, y = NULL)
##### Arguments
x, y

numeric or complex matrices (or vectors): y = NULL is taken to be the same matrix as x. Vectors are promoted to single-column or single-row matrices, depending on the context.

##### Value

A double or complex matrix, with appropriate dimnames taken from x and y.

##### Note

When x or y are not matrices, they are treated as column or row matrices, but their names are usually not promoted to dimnames. Hence, currently, the last example has empty dimnames.

In the same situation, these matrix products (also %*%) are more flexible in promotion of vectors to row or column matrices, such that more cases are allowed, since R 3.2.0.

The propagation of NaN/Inf values, precision, and performance of matrix products can be controlled by options("matprod").

##### References

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.

%*% and outer product %o%.

• crossprod
• tcrossprod
##### Examples
library(base) # NOT RUN { (z <- crossprod(1:4)) # = sum(1 + 2^2 + 3^2 + 4^2) drop(z) # scalar x <- 1:4; names(x) <- letters[1:4]; x tcrossprod(as.matrix(x)) # is identical(tcrossprod(as.matrix(x)), crossprod(t(x))) tcrossprod(x) # no dimnames m <- matrix(1:6, 2,3) ; v <- 1:3; v2 <- 2:1 stopifnot(identical(tcrossprod(v, m), v %*% t(m)), identical(tcrossprod(v, m), crossprod(v, t(m))), identical(crossprod(m, v2), t(m) %*% v2)) # } 
Documentation reproduced from package base, version 3.6.0, License: Part of R 3.6.0

### Community examples

afonso.lenzi@gmail.com at Nov 2, 2018 base v3.5.1

#example to use crossprod to calculate de variance of this famous dataset #http://genomicsclass.github.io/book/pages/matrix_algebra_examples.html library(UsingR) y <- father.son\$sheight N <- length(y) Y<- matrix(y,N,1) A <- matrix(1,N,1) barY=crossprod(A,Y) / N print(barY) r <- y - barY crossprod(r)/N