# Matrix multiplication

In mathematics, **matrix multiplication** is a binary operation that takes a pair of matrices, and produces another matrix. Numbers such as the real or complex numbers can be multiplied according to elementary arithmetic. On the other hand, matrices are *arrays of numbers*, so there is no unique way to define "the" multiplication of matrices. As such, in general the term "matrix multiplication" refers to a number of different ways to multiply matrices. The key features of any matrix multiplication include: the number of rows and columns the original matrices have (called the "size", "order" or "dimension"), and specifying how the entries of the matrices generate the new matrix.

Like vectors, matrices of any size can be multiplied by scalars, which amounts to multiplying every entry of the matrix by the same number. Similar to the entrywise definition of adding or subtracting matrices, multiplication of two matrices of the same size can be defined by multiplying the corresponding entries, and this is known as the Hadamard product. Another definition is the Kronecker product of two matrices, to obtain a block matrix.

One can form many other definitions. However, the most useful definition can be motivated by linear equations and linear transformations on vectors, which have numerous applications in applied mathematics, physics, and engineering. This definition is often called *the* **matrix product**.^{[1]}^{[2]} In words, if **A** is an *n* × *m* matrix and **B** is a *m* × *p* matrix, their matrix product **AB** is an *n* × *p* matrix, in which the *m* entries across the rows of **A** are multiplied with the *m* entries down the columns of **B** (the precise definition is below).

This definition is not commutative, although it still retains the associative property and is distributive over entrywise addition of matrices. The identity element of the matrix product is the identity matrix (analogous to multiplying numbers by 1), and a square matrix may have an inverse matrix (analogous to the multiplicative inverse of a number). A consequence of the matrix product is determinant multiplicativity. The matrix product is an important operation in linear transformations, matrix groups, and the theory of group representations and irreps. For large matrices and/or products of more than two matrices, this matrix product can be very time consuming to calculate, so more efficient algorithms to compute the matrix product than the mathematical definition have been developed.

This article will use the following notational conventions: matrices are represented by capital letters in bold, e.g. **A**, vectors in lowercase bold, e.g. **a**, and entries of vectors and matrices are italic (since they are scalars), e.g. *A* and *a*. Index notation is often the clearest way to express definitions, and is used as standard in the literature. The *i, j* entry of matrix **A** is indicated by (**A**)_{ij} or *A*_{ij}, whereas a numerical label (not matrix entries) on a collection of matrices is subscripted only, e.g. **A**_{1}, **A**_{2}, etc.

## Scalar multiplication

{{#invoke:main|main}}

The simplest form of multiplication associated with matrices is scalar multiplication, which is a special case of the Kronecker product.

The **left scalar multiplication** of a matrix **A** with a scalar *λ* gives another matrix *λ***A** of the same size as **A**. The entries of *λ***A** are defined by

explicitly:

Similarly, the **right scalar multiplication** of a matrix **A** with a scalar *λ* is defined to be

explicitly:

When the underlying ring is commutative, for example, the real or complex number field, these two multiplications are the same, and are simply called *scalar multiplication*. However, for matrices over a more general ring that are *not* commutative, such as the quaternions, they may not be equal.

For a real scalar and matrix:

For quaternion scalars and matrices:

where *i*, *j*, *k* are the quaternion units. The non-commutativity of quaternion multiplication prevents the transition of changing *ij* = +*k* to *ji* = −*k*.

## Matrix product (two matrices)

Assume two matrices are to be multiplied (the generalization to any number is discussed below).

### General definition of the matrix product

If **A** is an *n* × *m* matrix and **B** is an *m* × *p* matrix,

the **matrix product** **AB** (denoted without multiplication signs or dots) is defined to be the *n* × *p* matrix^{[3]}^{[4]}^{[5]}^{[6]}

where each *i, j* entry is given by multiplying the entries *A _{ik}* (across row

*i*of

**A**) by the entries

*B*(down column

_{kj}*j*of

**B**), for

*k*= 1, 2, ...,

*m*, and summing the results over

*k*:

Thus the product **AB** is defined only if the number of columns in **A** is equal to the number of rows in **B**, in this case *m*. Each entry may be computed one at a time. Sometimes, the summation convention is used as it is understood to sum over the repeated index *k*. To prevent any ambiguity, this convention will not be used in the article.

Usually the entries are numbers or expressions, but can even be matrices themselves (see block matrix). The matrix product can still be calculated exactly the same way. See below for details on how the matrix product can be calculated in terms of blocks taking the forms of rows and columns.

### Illustration

The figure to the right illustrates diagrammatically the product of two matrices **A** and **B**, showing how each intersection in the product matrix corresponds to a row of **A** and a column of **B**.

The values at the intersections marked with circles are:

### Examples of matrix products

#### Row vector and column vector

If

their matrix products are:

and

Note **AB** and **BA** are two different matrices: the first is a 1 × 1 matrix while the second is a 3 × 3 matrix. Such expressions occur for real-valued Euclidean vectors in Cartesian coordinates, displayed as row and column matrices, in which case **AB** is the matrix form of their dot product, while **BA** the matrix form of their dyadic or tensor product.

#### Square matrix and column vector

If

their matrix product is:

however **BA** is not defined.

The product of a square matrix multiplied by a column matrix arises naturally in linear algebra; for solving linear equations and representing linear transformations. By choosing *a, b, c, p, q, r, u, v, w* in **A** appropriately, **A** can represent a variety of transformations such as rotations, scaling and reflections, shears, of a geometric shape in space.

#### Square matrices

If

their matrix products are:

and

In this case, both products **AB** and **BA** are defined, and the entries show that **AB** and **BA** are not equal in general.
Multiplying square matrices which represent linear transformations corresponds to the composite transformation (see below for details).

#### Row vector, square matrix, and column vector

If

their matrix product is:

however **CBA** is not defined. Note that **A**(**BC**) = (**AB**)**C**, this is one of many general properties listed below. Expressions of the form **ABC** occur when calculating the inner product of two vectors displayed as row and column vectors in an arbitrary coordinate system, and the metric tensor in these coordinates written as the square matrix.

#### Rectangular matrices

If

their matrix products are:

and

### Properties of the matrix product (two matrices)

Analogous to numbers (elements of a field), matrices satisfy the following general properties, although there is one subtlety, due to the nature of matrix multiplication.^{[7]}^{[8]}

#### All matrices

{{#invoke:Hatnote|hatnote}}

#### Square matrices only

{{#invoke:main|main}}

## Matrix product (any number)

{{#invoke:main|main}}

Matrix multiplication can be extended to the case of more than two matrices, provided that for each sequential pair, their dimensions match.

The product of *n* matrices **A**_{1}, **A**_{2}, ..., **A**_{n} with sizes *s*_{0} × *s*_{1}, *s*_{1} × *s*_{2}, ..., *s*_{n − 1} × *s*_{n} (where *s*_{0}, *s*_{1}, *s*_{2}, ..., *s*_{n} are all simply positive integers and the subscripts are labels corresponding to the matrices, nothing more), is the *s*_{0} × *s*_{n} matrix:

In index notation:

### Properties of the matrix product (any number)

The same properties will hold, as long as the ordering of matrices is not changed. Some of the previous properties for more than two matrices generalize as follows.

### Examples of chain multiplication

Similarity transformations involving similar matrices are matrix products of the three square matrices, in the form:

where **P** is the similarity matrix and **A** and **B** are said to be similar if this relation holds. This product appears frequently in linear algebra and applications, such as diagonalizing square matrices and the equivalence between different matrix representations of the same linear operator.

## Operations derived from the matrix product

More operations on square matrices can be defined using the matrix product, such as powers and nth roots by repeated matrix products, the matrix exponential can be defined by a power series, the matrix logarithm is the inverse of matrix exponentiation, and so on.

### Powers of matrices

Square matrices can be multiplied by themselves repeatedly in the same way as ordinary numbers, because they always have the same number of rows and columns. This repeated multiplication can be described as a **power of the matrix**, a special case of the ordinary matrix product. On the contrary, *rectangular* matrices do not have the same number of rows and columns so they can *never* be raised to a power. An *n* × *n* matrix **A** raised to a positive integer *k* is defined as

and the following identities hold, where *λ* is a scalar:

The naive computation of matrix powers is to multiply *k* times the matrix **A** to the result, starting with the identity matrix just like the scalar case. This can be improved using exponentiation by squaring, a method commonly used for scalars. For diagonalizable matrices, an even better method is to use the eigenvalue decomposition of **A**. Another method based on the Cayley–Hamilton theorem finds an identity using the matrices' characteristic polynomial, producing a more effective equation for **A**^{k} in which a *scalar* is raised to the required power, rather than an entire *matrix*.

A special case is the power of a diagonal matrix. Since the product of diagonal matrices amounts to simply multiplying corresponding diagonal elements together, the power *k* of a diagonal matrix **A** will have entries raised to the power. Explicitly;

meaning it is easy to raise a diagonal matrix to a power. When raising an arbitrary matrix (not necessarily a diagonal matrix) to a power, it is often helpful to exploit this property by diagonalizing the matrix first.

## Applications of the matrix product

### Linear transformations

{{#invoke:main|main}}

Matrices offer a concise way of representing linear transformations between vector spaces, and matrix multiplication corresponds to the composition of linear transformations. The matrix product of two matrices can be defined when their entries belong to the same ring, and hence can be added and multiplied.

Let *U*, *V*, and *W* be vector spaces over the same field with given bases, *S*: *V* → *W* and *T*: *U* → *V* be linear transformations and *ST*: *U* → *W* be their composition.

Suppose that **A**, **B**, and **C** are the matrices representing the transformations *S*, *T*, and *ST* with respect to the given bases.

Then **AB** = **C**, that is, the matrix of the composition (or the product) of linear transformations is the product of their matrices with respect to the given bases.

### Linear systems of equations

A system of linear equations can be solved by collecting the coefficients of the equations into a square matrix, then inverting the matrix equation.

A similar procedure can be used to solve a system of linear differential equations, see also phase plane.

### Group theory and representation theory

{{#invoke:main|main}}

## The inner and outer products

Given two *column vectors* **a** and **b**, the Euclidean inner product and outer product are the simplest special cases of the matrix product.^{[9]}

### Inner product

The **inner product** of two vectors in matrix form is equivalent to a column vector multiplied on the left by a row vector:

where **a**^{T} denotes the transpose of **a**.

The matrix product itself can be expressed in terms of inner product. Suppose that the first *n* × *m* matrix **A** is decomposed into its row vectors **a**_{i}, and the second *m* × *p* matrix **B** into its column vectors **b**_{i}:^{[1]}

where

Then:

It is also possible to express a matrix product in terms of concatenations of products of matrices and row or column vectors:

These decompositions are particularly useful for matrices that are envisioned as concatenations of particular types of row vectors or column vectors, e.g. orthogonal matrices (whose rows and columns are unit vectors orthogonal to each other) and Markov matrices (whose rows or columns sum to 1).{{ safesubst:#invoke:Unsubst||date=__DATE__ |$B=
{{#invoke:Category handler|main}}{{#invoke:Category handler|main}}^{[citation needed]}
}}

### Outer product

The **outer product** (also known as the **dyadic product** or **tensor product**) of two vectors in matrix form is equivalent to a row vector multiplied on the left by a column vector:

An alternative method is to express the matrix product in terms of the outer product. The decomposition is done the other way around, the first matrix **A** is decomposed into column vectors Template:Overline_{i} and the second matrix **B** into row vectors Template:Overline_{i}:

where this time

This method emphasizes the effect of individual column/row pairs on the result, which is a useful point of view with e.g. covariance matrices, where each such pair corresponds to the effect of a single sample point.{{ safesubst:#invoke:Unsubst||date=__DATE__ |$B=
{{#invoke:Category handler|main}}{{#invoke:Category handler|main}}^{[citation needed]}
}}