Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

crossprod

Flag t(x) %*% y and x %*% t(y), which are the purpose-built crossprod(x, y) and tcrossprod(x, y)—faster (they go straight to BLAS and never materialize the transpose that t() builds) and clearer.

The rule fires only when one operand is a single-argument t() call and that t resolves to base R; a local redefinition is left alone. When both operands are the same symbol the single-argument form (crossprod(x)) is used.

Transposed matrix products:

a <- t(x) %*% y
b <- x %*% t(y)
warning: crossprod
 --> example.R:1:6
  |
1 | a <- t(x) %*% y
  |      ^^^^^^^^^^ `t(x) %*% y` is the faster, clearer `crossprod(x, y)`
  = help: Use `crossprod(x, y)`.
warning: crossprod
 --> example.R:2:6
  |
2 | b <- x %*% t(y)
  |      ^^^^^^^^^^ `x %*% t(y)` is the faster, clearer `tcrossprod(x, y)`
  = help: Use `tcrossprod(x, y)`.

After applying the fix:

a <- crossprod(x, y)
b <- tcrossprod(x, y)