# Matrix multiplication

Matrix multiplication can either refer to multiplying a matrix by a scalar, or multiplying a matrix by another matrix. If necessary, refer to the matrix notation page for a review of the notation used to describe the sizes and entries of matrices.

## Matrix-Scalar multiplication

The first kind of matrix multiplication is the multiplication of a matrix by a scalar, which will be referred to as matrix-scalar multiplication. A scalar is a number that makes things larger, smaller, or even negative (think of a negative scalar as "pointing backwards" or "flipping" something in the opposite direction). Given an m × n matrix, A, and a scalar, c, the matrix, cA, or Ac, is the m × n matrix whose i,j^{th} entry is c times the i,j^{th} entry of A. In other words,

for all i=1, ..., m and j = 1, ..., n. For example,

### Properties of Matrix-Scalar multiplication

- Commutative: cA = Ac
- Associative: (cd)A = c(dA)
- Distributive over matrix addition: c(A + B) = cA + cB
- Distributive over scalar addition: (c + d)A = cA + dA

## Matrix-Matrix multiplication

Multiplying two matrices involves the use of an algebraic operation called the dot product. A vector can be seen as a 1 × matrix (row vector) or an n × 1 matrix (column vector). To use the dot product, the vectors must be of equal length, meaning that they have the same number of entries. Given two matrices, A and B, if

and

the dot product of A and B is:

Note that in the equation above, the "·" represents the dot product, **not** multiplication. To multiply matrices, the dot product of the corresponding rows and columns of the matrices being multiplied are computed to determine the entries of the resulting matrix; this is described in detail below.

It follows that, in order to multiply two matrices, A and B, the number of columns of A must equal the number of rows of B. For a product matrix, AB, to be defined in general, A must be m × n, and B must be n × p for some positive integers m, n, and p. If A is m × n, and B is n × p, the product matrix, AB, is the m × p matrix such that the i,j^{th} entry of AB, denoted (AB)_{ij}, is the dot product of the i^{th} row vector of A

with the j^{th} column vector of B

Therefore, (AB)_{ij} is given by the formula:

= | |||

= | |||

= |

for all i = 1,2 ..., m and j = 1,2, ..., p (i runs through the rows of A, of which there are m, and j runs through the columns of B, of which there are p). The row and column highlighted in blue in the matrices below represent the i^{th} row and j^{th} column of the matrices being multiplied:

Thus, the (AB)_{ij} entry is the sum of the products of the entries of row i of matrix A and column j of matrix B:

Example

Multiply A and B if A is the following 2 × 3 matrix and B is the following 3 × 2 matrix:

= | |||

= | |||

= | |||

= |

corresponds to:

= | |||

= | |||

= |

corresponds to:

= | |||

= | |||

= | |||

= |

corresponds to:

and

= | |||

= | |||

= | |||

= |

corresponds to:

Assembling these entries into the matrix product AB yields

However, if instead,

then AB is not defined since A has 3 columns and B has 2 rows, and 3 ≠ 2.

### Properties of matrix multiplication

- Commutative with scalars (i.e. matrix-scalar multiplication above): If A is m × n, B is n × p, and c is a scalar, cAB = AcB = ABc. Note: matrix-matrix multiplication is not commutative. For example, matrix A × matrix B does not necessarily equal matrix B × matrix A and more typically does not.
- Associative: If A is m × n, B is n × p, C is p × q, then (AB)C = A(BC)
- Distributive: Assuming all products and sums are compatible and well-defined,
- A(B + C) = AB + AC
- (A + B)C = AC + BC