matmult {base} | R Documentation

## Matrix Multiplication

### Description

Multiplies two matrices, if they are conformable. If one argument is a vector, it will be promoted to either a row or column matrix to make the two arguments conformable. If both are vectors of the same length, it will return the inner product (as a matrix).

### Usage

    x %*% y

### Arguments

`x, y` | numeric or complex matrices or vectors.

### Details

When a vector is promoted to a matrix, its names are not promoted to row or column names, unlike `as.matrix`.

Promotion of a vector to a 1-row or 1-column matrix happens when one of the two choices allows `x` and `y` to get conformable dimensions.

This operator is S4 generic but not S3 generic. S4 methods need to be written for a function of two arguments named `x` and `y`.

### Value

A double or complex matrix product. Use `drop` to remove dimensions which have only one level.

### Note

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.

### See Also

For matrix crossproducts, `crossprod()` and `tcrossprod()` are typically preferable. `matrix`, `Arithmetic`, `diag`.

### Examples

    x <- 1:4
    (z <- x %*% x)    # scalar ("inner") product (1 x 1 matrix)
    drop(z)             # as scalar

    y <- diag(x)
    z <- matrix(1:12, ncol = 3, nrow = 4)
    y %*% z
    y %*% x
    x %*% z

[Package base version 3.5.1 Index]