# Matrix multiplication

__: Mathematical operation in linear algebra__

**Short description**In mathematics, particularly in linear algebra, **matrix multiplication** is a binary operation that produces a matrix from two matrices. For matrix multiplication, the number of columns in the first matrix must be equal to the number of rows in the second matrix. The resulting matrix, known as the **matrix product**, has the number of rows of the first and the number of columns of the second matrix. The product of matrices **A** and **B** is denoted as **AB**.^{[1]}

Matrix multiplication was first described by the French mathematician Jacques Philippe Marie Binet in 1812,^{[2]} to represent the composition of linear maps that are represented by matrices. Matrix multiplication is thus a basic tool of linear algebra, and as such has numerous applications in many areas of mathematics, as well as in applied mathematics, statistics, physics, economics, and engineering.^{[3]}^{[4]}
Computing matrix products is a central operation in all computational applications of linear algebra.

## Notation

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 (they are numbers from a field), e.g. *A* and *a*. Index notation is often the clearest way to express definitions, and is used as standard in the literature. The entry in row i, column j of matrix **A** is indicated by (**A**)_{ij}, *A*_{ij} or *a*_{ij}. In contrast, a single subscript, e.g. **A**_{1}, **A**_{2}, is used to select a matrix (not a matrix entry) from a collection of matrices.

## Definition

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

- [math]\displaystyle{ \mathbf{A}=\begin{pmatrix} a_{11} & a_{12} & \cdots & a_{1n} \\ a_{21} & a_{22} & \cdots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m1} & a_{m2} & \cdots & a_{mn} \\ \end{pmatrix},\quad\mathbf{B}=\begin{pmatrix} b_{11} & b_{12} & \cdots & b_{1p} \\ b_{21} & b_{22} & \cdots & b_{2p} \\ \vdots & \vdots & \ddots & \vdots \\ b_{n1} & b_{n2} & \cdots & b_{np} \\ \end{pmatrix} }[/math]

the *matrix product* **C** = **AB** (denoted without multiplication signs or dots) is defined to be the *m* × *p* matrix^{[5]}^{[6]}^{[7]}^{[8]}

- [math]\displaystyle{ \mathbf{C}=\begin{pmatrix} c_{11} & c_{12} & \cdots & c_{1p} \\ c_{21} & c_{22} & \cdots & c_{2p} \\ \vdots & \vdots & \ddots & \vdots \\ c_{m1} & c_{m2} & \cdots & c_{mp} \\ \end{pmatrix} }[/math]

such that

- [math]\displaystyle{ c_{ij} = a_{i1}b_{1j} + a_{i2}b_{2j} +\cdots + a_{in}b_{nj}= \sum_{k=1}^n a_{ik}b_{kj}, }[/math]

for *i* = 1, ..., *m* and *j* = 1, ..., *p*.

That is, the entry [math]\displaystyle{ c_{ij} }[/math] of the product is obtained by multiplying term-by-term the entries of the ith row of **A** and the jth column of **B**, and summing these n products. In other words, [math]\displaystyle{ c_{ij} }[/math] is the dot product of the ith row of **A** and the jth column of **B**.

Therefore, **AB** can also be written as

- [math]\displaystyle{ \mathbf{C}=\begin{pmatrix} a_{11}b_{11} +\cdots + a_{1n}b_{n1} & a_{11}b_{12} +\cdots + a_{1n}b_{n2} & \cdots & a_{11}b_{1p} +\cdots + a_{1n}b_{np} \\ a_{21}b_{11} +\cdots + a_{2n}b_{n1} & a_{21}b_{12} +\cdots + a_{2n}b_{n2} & \cdots & a_{21}b_{1p} +\cdots + a_{2n}b_{np} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m1}b_{11} +\cdots + a_{mn}b_{n1} & a_{m1}b_{12} +\cdots + a_{mn}b_{n2} & \cdots & a_{m1}b_{1p} +\cdots + a_{mn}b_{np} \\ \end{pmatrix} }[/math]

Thus the product **AB** is defined if and only if the number of columns in **A** equals the number of rows in **B**,^{[1]} in this case *n*.

In most scenarios, the entries are numbers, but they may be any kind of mathematical objects for which an addition and a multiplication are defined, that are associative, and such that the addition is commutative, and the multiplication is distributive with respect to the addition. In particular, the entries may be matrices themselves (see block matrix).

### 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**.

- [math]\displaystyle{ \overset{4\times 2 \text{ matrix}}{\begin{bmatrix} a_{11} & a_{12} \\ \cdot & \cdot \\ a_{31} & a_{32} \\ \cdot & \cdot \\ \end{bmatrix}} \overset{2\times 3\text{ matrix}}{\begin{bmatrix} \cdot & b_{12} & b_{13} \\ \cdot & b_{22} & b_{23} \\ \end{bmatrix}} = \overset{4\times 3\text{ matrix}}{\begin{bmatrix} \cdot & c_{12} & \cdot \\ \cdot & \cdot & \cdot \\ \cdot & \cdot & c_{33} \\ \cdot & \cdot & \cdot \\ \end{bmatrix}} }[/math]

The values at the intersections, marked with circles in figure to the right, are:

- [math]\displaystyle{ \begin{align} c_{12} & = a_{11} b_{12} + a_{12} b_{22} \\ c_{33} & = a_{31} b_{13} + a_{32} b_{23} \end{align} }[/math]

## Fundamental applications

Historically, matrix multiplication has been introduced for facilitating and clarifying computations in linear algebra. This strong relationship between matrix multiplication and linear algebra remains fundamental in all mathematics, as well as in physics, chemistry, engineering and computer science.

### Linear maps

If a vector space has a finite basis, its vectors are each uniquely represented by a finite sequence of scalars, called a coordinate vector, whose elements are the coordinates of the vector on the basis. These coordinate vectors form another vector space, which is isomorphic to the original vector space. A coordinate vector is commonly organized as a column matrix (also called *column vector*), which is a matrix with only one column. So, a column vector represents both a coordinate vector, and a vector of the original vector space.

A linear map A from a vector space of dimension n into a vector space of dimension m maps a column vector

- [math]\displaystyle{ \mathbf x=\begin{pmatrix}x_1 \\ x_2 \\ \vdots \\ x_n\end{pmatrix} }[/math]

onto the column vector

- [math]\displaystyle{ \mathbf y= A(\mathbf x)= \begin{pmatrix}a_{11}x_1+\cdots + a_{1n}x_n\\ a_{21}x_1+\cdots + a_{2n}x_n \\ \vdots \\ a_{m1}x_1+\cdots + a_{mn}x_n\end{pmatrix}. }[/math]

The linear map A is thus defined by the matrix

- [math]\displaystyle{ \mathbf{A}=\begin{pmatrix} a_{11} & a_{12} & \cdots & a_{1n} \\ a_{21} & a_{22} & \cdots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m1} & a_{m2} & \cdots & a_{mn} \\ \end{pmatrix}, }[/math]

and maps the column vector [math]\displaystyle{ \mathbf x }[/math] to the matrix product

- [math]\displaystyle{ \mathbf y = \mathbf {Ax}. }[/math]

If B is another linear map from the preceding vector space of dimension m, into a vector space of dimension p, it is represented by a [math]\displaystyle{ p\times m }[/math] matrix [math]\displaystyle{ \mathbf B. }[/math] A straightforward computation shows that the matrix of the composite map [math]\displaystyle{ B\circ A }[/math] is the matrix product [math]\displaystyle{ \mathbf {BA}. }[/math] The general formula [math]\displaystyle{ (B\circ A)(\mathbf x) = B(A(\mathbf x)) }[/math]) that defines the function composition is instanced here as a specific case of associativity of matrix product (see § Associativity below):

- [math]\displaystyle{ (\mathbf{BA})\mathbf x = \mathbf{B}(\mathbf {Ax}) = \mathbf{BAx}. }[/math]

#### Geometric rotations

Using a Cartesian coordinate system in a Euclidean plane, the rotation by an angle [math]\displaystyle{ \alpha }[/math] around the origin is a linear map. More precisely, [math]\displaystyle{ \begin{bmatrix} x' \\ y' \end{bmatrix} = \begin{bmatrix} \cos \alpha & - \sin \alpha \\ \sin \alpha & \cos \alpha \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix}, }[/math] where the source point [math]\displaystyle{ (x,y) }[/math] and its image [math]\displaystyle{ (x',y') }[/math] are written as column vectors.

The composition of the rotation by [math]\displaystyle{ \alpha }[/math] and that by [math]\displaystyle{ \beta }[/math] then corresponds to the matrix product [math]\displaystyle{ \begin{bmatrix} \cos \beta & - \sin \beta \\ \sin \beta & \cos \beta \end{bmatrix} \begin{bmatrix} \cos \alpha & - \sin \alpha \\ \sin \alpha & \cos \alpha \end{bmatrix} = \begin{bmatrix} \cos \beta \cos \alpha - \sin \beta \sin \alpha & - \cos \beta \sin \alpha - \sin \beta \cos \alpha \\ \sin \beta \cos \alpha + \cos \beta \sin \alpha & - \sin \beta \sin \alpha + \cos \beta \cos \alpha \end{bmatrix} = \begin{bmatrix} \cos (\alpha+\beta) & - \sin(\alpha+\beta) \\ \sin(\alpha+\beta) & \cos(\alpha+\beta) \end{bmatrix}, }[/math] where appropriate trigonometric identities are employed for the second equality. That is, the composition corresponds to the rotation by angle [math]\displaystyle{ \alpha+\beta }[/math], as expected.

#### Resource allocation in economics

As an example, a fictitious factory uses 4 kinds of basic commodities (de), [math]\displaystyle{ b_1, b_2, b_3, b_4 }[/math] to produce 3 kinds of intermediate goods, [math]\displaystyle{ m_1, m_2, m_3 }[/math], which in turn are used to produce 3 kinds of final products, [math]\displaystyle{ f_1, f_2, f_3 }[/math]. The matrices

- [math]\displaystyle{ \mathbf{A} = \begin{pmatrix} 1 & 0 & 1 \\ 2 & 1 & 1 \\ 0 & 1 & 1 \\ 1 & 1 & 2 \\ \end{pmatrix} }[/math] and [math]\displaystyle{ \mathbf{B} = \begin{pmatrix} 1 & 2 & 1 \\ 2 & 3 & 1 \\ 4 & 2 & 2 \\ \end{pmatrix} }[/math]

provide the amount of basic commodities needed for a given amount of intermediate goods, and the amount of intermediate goods needed for a given amount of final products, respectively. For example, to produce one unit of intermediate good [math]\displaystyle{ m_1 }[/math], one unit of basic commodity [math]\displaystyle{ b_1 }[/math], two units of [math]\displaystyle{ b_2 }[/math], no units of [math]\displaystyle{ b_3 }[/math], and one unit of [math]\displaystyle{ b_4 }[/math] are needed, corresponding to the first column of [math]\displaystyle{ \mathbf{A} }[/math].

Using matrix multiplication, compute

- [math]\displaystyle{ \mathbf{AB} = \begin{pmatrix} 5 & 4 & 3 \\ 8 & 9 & 5 \\\ 6 & 5 & 3 \\ 11 & 9 & 6 \\ \end{pmatrix} ; }[/math]

this matrix directly provides the amounts of basic commodities needed for given amounts of final goods. For example, the bottom left entry of [math]\displaystyle{ \mathbf{AB} }[/math] is computed as [math]\displaystyle{ 1 \cdot 1 + 1 \cdot 2 + 2 \cdot 4 = 11 }[/math], reflecting that [math]\displaystyle{ 11 }[/math] units of [math]\displaystyle{ b_4 }[/math] are needed to produce one unit of [math]\displaystyle{ f_1 }[/math]. Indeed, one [math]\displaystyle{ b_4 }[/math] unit is needed for [math]\displaystyle{ m_1 }[/math], 2 for [math]\displaystyle{ m_2 }[/math], and [math]\displaystyle{ 4 }[/math] for each of the two [math]\displaystyle{ m_3 }[/math] units that go into the [math]\displaystyle{ f_1 }[/math] unit, see picture.

In order to produce e.g. 100 units of the final product [math]\displaystyle{ f_1 }[/math], 80 units of [math]\displaystyle{ f_2 }[/math], and 60 units of [math]\displaystyle{ f_3 }[/math], the necessary amounts of basic goods can be computed as

- [math]\displaystyle{ (\mathbf{AB}) \begin{pmatrix} 100 \\ 80 \\ 60 \\ \end{pmatrix} = \begin{pmatrix} 1000 \\ 1820 \\ 1180 \\ 2180 \end{pmatrix} , }[/math]

that is, [math]\displaystyle{ 1000 }[/math] units of [math]\displaystyle{ b_1 }[/math], [math]\displaystyle{ 1820 }[/math] units of [math]\displaystyle{ b_2 }[/math], [math]\displaystyle{ 1180 }[/math] units of [math]\displaystyle{ b_3 }[/math], [math]\displaystyle{ 2180 }[/math] units of [math]\displaystyle{ b_4 }[/math] are needed.
Similarly, the product matrix [math]\displaystyle{ \mathbf{AB} }[/math] can be used to compute the needed amounts of basic goods for other final-good amount data.^{[9]}

### System of linear equations

The general form of a system of linear equations is

- [math]\displaystyle{ \begin{matrix}a_{11}x_1+\cdots + a_{1n}x_n=b_1 \\ a_{21}x_1+\cdots + a_{2n}x_n =b_2 \\ \vdots \\ a_{m1}x_1+\cdots + a_{mn}x_n =b_m\end{matrix}. }[/math]

Using same notation as above, such a system is equivalent with the single matrix equation

- [math]\displaystyle{ \mathbf{Ax}=\mathbf b. }[/math]

### Dot product, bilinear form and sesquilinear form

The dot product of two column vectors is the unique entry of the matrix product

- [math]\displaystyle{ \mathbf x^\mathsf T \mathbf y, }[/math]

where [math]\displaystyle{ \mathbf x^\mathsf T }[/math] is the row vector obtained by transposing [math]\displaystyle{ \mathbf x }[/math]. (As usual, a 1×1 matrix is identified with its unique entry.)

More generally, any bilinear form over a vector space of finite dimension may be expressed as a matrix product

- [math]\displaystyle{ \mathbf x^\mathsf T \mathbf {Ay}, }[/math]

and any sesquilinear form may be expressed as

- [math]\displaystyle{ \mathbf x^\dagger \mathbf {Ay}, }[/math]

where [math]\displaystyle{ \mathbf x^\dagger }[/math] denotes the conjugate transpose of [math]\displaystyle{ \mathbf x }[/math] (conjugate of the transpose, or equivalently transpose of the conjugate).

## General properties

Matrix multiplication shares some properties with usual multiplication. However, matrix multiplication is not defined if the number of columns of the first factor differs from the number of rows of the second factor, and it is non-commutative,^{[10]} even when the product remains defined after changing the order of the factors.^{[11]}^{[12]}

### Non-commutativity

An operation is commutative if, given two elements **A** and **B** such that the product [math]\displaystyle{ \mathbf{A}\mathbf{B} }[/math] is defined, then [math]\displaystyle{ \mathbf{B}\mathbf{A} }[/math] is also defined, and [math]\displaystyle{ \mathbf{A}\mathbf{B}=\mathbf{B}\mathbf{A}. }[/math]

If **A** and **B** are matrices of respective sizes [math]\displaystyle{ m\times n }[/math] and [math]\displaystyle{ p\times q }[/math], then [math]\displaystyle{ \mathbf{A}\mathbf{B} }[/math] is defined if [math]\displaystyle{ n=p }[/math], and [math]\displaystyle{ \mathbf{B}\mathbf{A} }[/math] is defined if [math]\displaystyle{ m=q }[/math]. Therefore, if one of the products is defined, the other one need not be defined. If [math]\displaystyle{ m=q\neq n=p }[/math], the two products are defined, but have different sizes; thus they cannot be equal. Only if [math]\displaystyle{ m=q= n=p }[/math], that is, if **A** and **B** are square matrices of the same size, are both products defined and of the same size. Even in this case, one has in general

- [math]\displaystyle{ \mathbf{A}\mathbf{B} \neq \mathbf{B}\mathbf{A}. }[/math]

For example

- [math]\displaystyle{ \begin{pmatrix} 0 & 1 \\ 0 & 0 \end{pmatrix}\begin{pmatrix} 0 & 0 \\ 1 & 0 \end{pmatrix}=\begin{pmatrix} 1 & 0 \\ 0 & 0 \end{pmatrix}, }[/math]

but

- [math]\displaystyle{ \begin{pmatrix} 0 & 0 \\ 1 & 0 \end{pmatrix}\begin{pmatrix} 0 & 1 \\ 0 & 0 \end{pmatrix} = \begin{pmatrix} 0 & 0 \\ 0 & 1 \end{pmatrix}. }[/math]

This example may be expanded for showing that, if **A** is a [math]\displaystyle{ n\times n }[/math] matrix with entries in a field F, then [math]\displaystyle{ \mathbf{A}\mathbf{B} = \mathbf{B}\mathbf{A} }[/math] for every [math]\displaystyle{ n\times n }[/math] matrix **B** with entries in F, if and only if [math]\displaystyle{ \mathbf{A}=c\,\mathbf{I} }[/math] where [math]\displaystyle{ c\in F }[/math], and **I** is the [math]\displaystyle{ n\times n }[/math] identity matrix. If, instead of a field, the entries are supposed to belong to a ring, then one must add the condition that c belongs to the center of the ring.

One special case where commutativity does occur is when **D** and **E** are two (square) diagonal matrices (of the same size); then **DE** = **ED**.^{[10]} Again, if the matrices are over a general ring rather than a field, the corresponding entries in each must also commute with each other for this to hold.

### Distributivity

The matrix product is distributive with respect to matrix addition. That is, if **A**, **B**, **C**, **D** are matrices of respective sizes *m* × *n*, *n* × *p*, *n* × *p*, and *p* × *q*, one has (left distributivity)

- [math]\displaystyle{ \mathbf{A}(\mathbf{B} + \mathbf{C}) = \mathbf{AB} + \mathbf{AC}, }[/math]

and (right distributivity)

- [math]\displaystyle{ (\mathbf{B} + \mathbf{C} )\mathbf{D} = \mathbf{BD} + \mathbf{CD}. }[/math]
^{[10]}

This results from the distributivity for coefficients by

- [math]\displaystyle{ \sum_k a_{ik}(b_{kj} + c_{kj}) = \sum_k a_{ik}b_{kj} + \sum_k a_{ik}c_{kj} }[/math]
- [math]\displaystyle{ \sum_k (b_{ik} + c_{ik}) d_{kj} = \sum_k b_{ik}d_{kj} + \sum_k c_{ik}d_{kj}. }[/math]

### Product with a scalar

If **A** is a matrix and c a scalar, then the matrices [math]\displaystyle{ c\mathbf{A} }[/math] and [math]\displaystyle{ \mathbf{A}c }[/math] are obtained by left or right multiplying all entries of **A** by c. If the scalars have the commutative property, then [math]\displaystyle{ c\mathbf{A} = \mathbf{A}c. }[/math]

If the product [math]\displaystyle{ \mathbf{AB} }[/math] is defined (that is, the number of columns of **A** equals the number of rows of **B**), then

- [math]\displaystyle{ c(\mathbf{AB}) = (c \mathbf{A})\mathbf{B} }[/math] and [math]\displaystyle{ (\mathbf{A} \mathbf{B})c=\mathbf{A}(\mathbf{B}c). }[/math]

If the scalars have the commutative property, then all four matrices are equal. More generally, all four are equal if *c* belongs to the center of a ring containing the entries of the matrices, because in this case, *c***X** = **X***c* for all matrices **X**.

These properties result from the bilinearity of the product of scalars:

- [math]\displaystyle{ c \left(\sum_k a_{ik}b_{kj}\right) = \sum_k (c a_{ik} ) b_{kj} }[/math]
- [math]\displaystyle{ \left(\sum_k a_{ik}b_{kj}\right) c = \sum_k a_{ik} ( b_{kj}c). }[/math]

### Transpose

If the scalars have the commutative property, the transpose of a product of matrices is the product, in the reverse order, of the transposes of the factors. That is

- [math]\displaystyle{ (\mathbf{AB})^\mathsf{T} = \mathbf{B}^\mathsf{T}\mathbf{A}^\mathsf{T} }[/math]

where ^{T} denotes the transpose, that is the interchange of rows and columns.

This identity does not hold for noncommutative entries, since the order between the entries of **A** and **B** is reversed, when one expands the definition of the matrix product.

### Complex conjugate

If **A** and **B** have complex entries, then

- [math]\displaystyle{ (\mathbf{AB})^* = \mathbf{A}^*\mathbf{B}^* }[/math]

where ^{*} denotes the entry-wise complex conjugate of a matrix.

This results from applying to the definition of matrix product the fact that the conjugate of a sum is the sum of the conjugates of the summands and the conjugate of a product is the product of the conjugates of the factors.

Transposition acts on the indices of the entries, while conjugation acts independently on the entries themselves. It results that, if **A** and **B** have complex entries, one has

- [math]\displaystyle{ (\mathbf{AB})^\dagger = \mathbf{B}^\dagger\mathbf{A}^\dagger , }[/math]

where ^{†} denotes the conjugate transpose (conjugate of the transpose, or equivalently transpose of the conjugate).

### Associativity

Given three matrices **A**, **B** and **C**, the products (**AB**)**C** and **A**(**BC**) are defined if and only if the number of columns of **A** equals the number of rows of **B**, and the number of columns of **B** equals the number of rows of **C** (in particular, if one of the products is defined, then the other is also defined). In this case, one has the associative property

- [math]\displaystyle{ (\mathbf{AB})\mathbf{C}=\mathbf{A}(\mathbf{BC}). }[/math]

As for any associative operation, this allows omitting parentheses, and writing the above products as [math]\displaystyle{ \mathbf{ABC}. }[/math]

This extends naturally to the product of any number of matrices provided that the dimensions match. That is, if **A**_{1}, **A**_{2}, ..., **A**_{n} are matrices such that the number of columns of **A**_{i} equals the number of rows of **A**_{i + 1} for *i* = 1, ..., *n* – 1, then the product

- [math]\displaystyle{ \prod_{i=1}^n \mathbf{A}_i = \mathbf{A}_1\mathbf{A}_2\cdots\mathbf{A}_n }[/math]

is defined and does not depend on the order of the multiplications, if the order of the matrices is kept fixed.

These properties may be proved by straightforward but complicated summation manipulations. This result also follows from the fact that matrices represent linear maps. Therefore, the associative property of matrices is simply a specific case of the associative property of function composition.

#### Computational complexity depends on parenthezation

Although the result of a sequence of matrix products does not depend on the order of operation (provided that the order of the matrices is not changed), the computational complexity may depend dramatically on this order.

For example, if **A**, **B** and **C** are matrices of respective sizes 10×30, 30×5, 5×60, computing (**AB**)**C** needs 10×30×5 + 10×5×60 = 4,500 multiplications, while computing **A**(**BC**) needs 30×5×60 + 10×30×60 = 27,000 multiplications.

Algorithms have been designed for choosing the best order of products, see Matrix chain multiplication. When the number n of matrices increases, it has been shown that the choice of the best order has a complexity of [math]\displaystyle{ O(n \log n). }[/math]

#### Application to similarity

Any invertible matrix [math]\displaystyle{ \mathbf{P} }[/math] defines a similarity transformation (on square matrices of the same size as [math]\displaystyle{ \mathbf{P} }[/math])

- [math]\displaystyle{ S_\mathbf{P}(\mathbf{A}) = \mathbf{P}^{-1} \mathbf{A} \mathbf{P}. }[/math]

Similarity transformations map product to products, that is

- [math]\displaystyle{ S_\mathbf{P}(\mathbf{AB}) = S_\mathbf{P}(\mathbf{A})S_\mathbf{P}(\mathbf{B}). }[/math]

In fact, one has

- [math]\displaystyle{ \mathbf{P}^{-1} (\mathbf{AB}) \mathbf{P} = \mathbf{P}^{-1} \mathbf{A}(\mathbf{P}\mathbf{P}^{-1})\mathbf{B} \mathbf{P} =(\mathbf{P}^{-1} \mathbf{A}\mathbf{P})(\mathbf{P}^{-1}\mathbf{B} \mathbf{P}). }[/math]

## Square matrices

Let us denote [math]\displaystyle{ \mathcal M_n(R) }[/math] the set of *n*×*n* square matrices with entries in a ring R, which, in practice, is often a field.

In [math]\displaystyle{ \mathcal M_n(R) }[/math], the product is defined for every pair of matrices. This makes [math]\displaystyle{ \mathcal M_n(R) }[/math] a ring, which has the identity matrix **I** as identity element (the matrix whose diagonal entries are equal to 1 and all other entries are 0). This ring is also an associative R-algebra.

If *n* > 1, many matrices do not have a multiplicative inverse. For example, a matrix such that all entries of a row (or a column) are 0 does not have an inverse. If it exists, the inverse of a matrix **A** is denoted **A**^{−1}, and, thus verifies

- [math]\displaystyle{ \mathbf{A}\mathbf{A}^{-1} = \mathbf{A}^{-1}\mathbf{A} = \mathbf{I}. }[/math]

A matrix that has an inverse is an invertible matrix. Otherwise, it is a singular matrix.

A product of matrices is invertible if and only if each factor is invertible. In this case, one has

- [math]\displaystyle{ (\mathbf{A}\mathbf{B})^{-1} = \mathbf{B}^{-1}\mathbf{A}^{-1}. }[/math]

When R is commutative, and, in particular, when it is a field, the determinant of a product is the product of the determinants. As determinants are scalars, and scalars commute, one has thus

- [math]\displaystyle{ \det(\mathbf{AB}) = \det(\mathbf{BA}) =\det(\mathbf{A})\det(\mathbf{B}). }[/math]

The other matrix invariants do not behave as well with products. Nevertheless, if R is commutative, **AB** and **BA** have the same trace, the same characteristic polynomial, and the same eigenvalues with the same multiplicities. However, the eigenvectors are generally different if **AB** ≠ **BA**.

### Powers of a matrix

One may raise a square matrix to any nonnegative integer power multiplying it by itself repeatedly in the same way as for ordinary numbers. That is,

- [math]\displaystyle{ \mathbf{A}^0 = \mathbf{I}, }[/math]
- [math]\displaystyle{ \mathbf{A}^1 = \mathbf{A}, }[/math]
- [math]\displaystyle{ \mathbf{A}^k = \underbrace{\mathbf{A}\mathbf{A}\cdots\mathbf{A}}_{k\text{ times}}. }[/math]

Computing the kth power of a matrix needs *k* – 1 times the time of a single matrix multiplication, if it is done with the trivial algorithm (repeated multiplication). As this may be very time consuming, one generally prefers using exponentiation by squaring, which requires less than 2 log_{2} *k* matrix multiplications, and is therefore much more efficient.

An easy case for exponentiation is that of a diagonal matrix. Since the product of diagonal matrices amounts to simply multiplying corresponding diagonal elements together, the kth power of a diagonal matrix is obtained by raising the entries to the power k:

- [math]\displaystyle{ \begin{bmatrix} a_{11} & 0 & \cdots & 0 \\ 0 & a_{22} & \cdots & 0 \\ \vdots & \vdots & \ddots & \vdots \\ 0 & 0 & \cdots & a_{nn} \end{bmatrix}^k = \begin{bmatrix} a_{11}^k & 0 & \cdots & 0 \\ 0 & a_{22}^k & \cdots & 0 \\ \vdots & \vdots & \ddots & \vdots \\ 0 & 0 & \cdots & a_{nn}^k \end{bmatrix}. }[/math]

## Abstract algebra

The definition of matrix product requires that the entries belong to a semiring, and does not require multiplication of elements of the semiring to be commutative. In many applications, the matrix elements belong to a field, although the tropical semiring is also a common choice for graph shortest path problems.^{[13]} Even in the case of matrices over fields, the product is not commutative in general, although it is associative and is distributive over matrix addition. The identity matrices (which are the square matrices whose entries are zero outside of the main diagonal and 1 on the main diagonal) are identity elements of the matrix product. It follows that the *n* × *n* matrices over a ring form a ring, which is noncommutative except if *n* = 1 and the ground ring is commutative.

A square matrix may have a multiplicative inverse, called an inverse matrix. In the common case where the entries belong to a commutative ring R, a matrix has an inverse if and only if its determinant has a multiplicative inverse in R. The determinant of a product of square matrices is the product of the determinants of the factors. The *n* × *n* matrices that have an inverse form a group under matrix multiplication, the subgroups of which are called matrix groups. Many classical groups (including all finite groups) are isomorphic to matrix groups; this is the starting point of the theory of group representations.

## Computational complexity

The matrix multiplication algorithm that results from the definition requires, in the worst case, [math]\displaystyle{ n^3 }[/math] multiplications and [math]\displaystyle{ (n-1)n^2 }[/math] additions of scalars to compute the product of two square *n*×*n* matrices. Its computational complexity is therefore [math]\displaystyle{ O(n^3) }[/math], in a model of computation for which the scalar operations take constant time.

Rather surprisingly, this complexity is not optimal, as shown in 1969 by Volker Strassen, who provided an algorithm, now called Strassen's algorithm, with a complexity of [math]\displaystyle{ O( n^{\log_{2}7}) \approx O(n^{2.8074}). }[/math]^{[14]}
Strassen's algorithm can be parallelized to further improve the performance.
(As of December 2020), the best matrix multiplication algorithm is by Josh Alman and Virginia Vassilevska Williams and has complexity *O*(*n*^{2.3728596}).^{[15]}
It is not known whether matrix multiplication can be performed in *n*^{2 + o(1)} time. This would be optimal, since one must read the [math]\displaystyle{ n^2 }[/math] elements of a matrix in order to multiply it with another matrix.

Since matrix multiplication forms the basis for many algorithms, and many operations on matrices even have the same complexity as matrix multiplication (up to a multiplicative constant), the computational complexity of matrix multiplication appears throughout numerical linear algebra and theoretical computer science.

## Generalizations

Other types of products of matrices include:

- Block matrix multiplication
- Cracovian product, defined as
**A**∧**B**=**B**^{T}**A** - Frobenius inner product, the dot product of matrices considered as vectors, or, equivalently the sum of the entries of the Hadamard product
- Hadamard product of two matrices of the same size, resulting in a matrix of the same size, which is the product entry-by-entry
- Kronecker product or tensor product, the generalization to any size of the preceding
- Khatri-Rao product and Face-splitting product
- Outer product, also called dyadic product or tensor product of two column matrices, which is [math]\displaystyle{ \mathbf{a}\mathbf{b}^\mathsf{T} }[/math]
- Scalar multiplication

## See also

- Matrix calculus, for the interaction of matrix multiplication with operations from calculus

## Notes

- ↑
^{1.0}^{1.1}Nykamp, Duane. "Multiplying matrices and vectors". https://mathinsight.org/matrix_vector_multiplication. - ↑ O'Connor, John J.; Robertson, Edmund F., "Jacques Philippe Marie Binet",
*MacTutor History of Mathematics archive*, University of St Andrews, http://www-history.mcs.st-andrews.ac.uk/Biographies/Binet.html. - ↑ Lerner, R. G.; Trigg, G. L. (1991).
*Encyclopaedia of Physics*(2nd ed.). VHC publishers. ISBN 978-3-527-26954-9. - ↑ Parker, C. B. (1994).
*McGraw Hill Encyclopaedia of Physics*(2nd ed.). ISBN 978-0-07-051400-3. https://archive.org/details/mcgrawhillencycl1993park. - ↑ Lipschutz, S.; Lipson, M. (2009).
*Linear Algebra*. Schaum's Outlines (4th ed.). McGraw Hill (USA). pp. 30–31. ISBN 978-0-07-154352-1. - ↑ Riley, K. F.; Hobson, M. P.; Bence, S. J. (2010).
*Mathematical methods for physics and engineering*. Cambridge University Press. ISBN 978-0-521-86153-3. https://archive.org/details/mathematicalmeth00rile. - ↑ Adams, R. A. (1995).
*Calculus, A Complete Course*(3rd ed.). Addison Wesley. p. 627. ISBN 0-201-82823-5. - ↑ Horn, Johnson (2013).
*Matrix Analysis*(2nd ed.). Cambridge University Press. p. 6. ISBN 978-0-521-54823-6. - ↑ Peter Stingl (1996) (in German).
*Mathematik für Fachhochschulen – Technik und Informatik*(5th ed.). Munich: Carl Hanser Verlag. ISBN 3-446-18668-9. Here: Exm.5.4.10, p.205-206 - ↑
^{10.0}^{10.1}^{10.2}Weisstein, Eric W.. "Matrix Multiplication" (in en). https://mathworld.wolfram.com/MatrixMultiplication.html. - ↑ Lipcshutz, S.; Lipson, M. (2009). "2".
*Linear Algebra*. Schaum's Outlines (4th ed.). McGraw Hill (USA). ISBN 978-0-07-154352-1. - ↑ Horn, Johnson (2013). "Chapter 0".
*Matrix Analysis*(2nd ed.). Cambridge University Press. ISBN 978-0-521-54823-6. - ↑ Motwani, Rajeev; Raghavan, Prabhakar (1995).
*Randomized Algorithms*. Cambridge University Press. p. 280. ISBN 9780521474658. https://books.google.com/books?id=QKVY4mDivBEC&pg=PA280. - ↑
Volker Strassen (Aug 1969). "Gaussian elimination is not optimal".
*Numerische Mathematik***13**(4): 354–356. doi:10.1007/BF02165411. http://www.digizeitschriften.de/dms/img/?PID=GDZPPN001168215. - ↑
Alman, Josh; Williams, Virginia Vassilevska (2020), "A Refined Laser Method and Faster Matrix Multiplication",
*32nd Annual ACM-SIAM Symposium on Discrete Algorithms (SODA 2021)*, https://www.siam.org/conferences/cm/program/accepted-papers/soda21-accepted-papers

## References

The Wikibook Linear Algebra has a page on the topic of: Matrix multiplication |

- Henry Cohn, Robert Kleinberg, Balázs Szegedy, and Chris Umans. Group-theoretic Algorithms for Matrix Multiplication. arXiv:math.GR/0511460.
*Proceedings of the 46th Annual Symposium on Foundations of Computer Science*, 23–25 October 2005, Pittsburgh, PA, IEEE Computer Society, pp. 379–388. - Henry Cohn, Chris Umans. A Group-theoretic Approach to Fast Matrix Multiplication. arXiv:math.GR/0307321.
*Proceedings of the 44th Annual IEEE Symposium on Foundations of Computer Science*, 11–14 October 2003, Cambridge, MA, IEEE Computer Society, pp. 438–449. - Coppersmith, D.; Winograd, S. (1990). "Matrix multiplication via arithmetic progressions".
*J. Symbolic Comput.***9**(3): 251–280. doi:10.1016/s0747-7171(08)80013-2. - Horn, Roger A.; Johnson, Charles R. (1991),
*Topics in Matrix Analysis*,*Cambridge University Press*, ISBN 978-0-521-46713-1 - Knuth, D.E.,
*The Art of Computer Programming Volume 2: Seminumerical Algorithms*. Addison-Wesley Professional; 3 edition (November 14, 1997). ISBN 978-0-201-89684-8. pp. 501. - Press, William H.; Flannery, Brian P.; Teukolsky, Saul A.; Vetterling, William T. (2007),
*Numerical Recipes: The Art of Scientific Computing*(3rd ed.),*Cambridge University Press*, ISBN 978-0-521-88068-8. - Ran Raz. On the complexity of matrix product. In Proceedings of the thirty-fourth annual ACM symposium on Theory of computing. ACM Press, 2002. doi:10.1145/509907.509932.
- Robinson, Sara,
*Toward an Optimal Algorithm for Matrix Multiplication,*SIAM News 38(9), November 2005. PDF - Strassen, Volker,
*Gaussian Elimination is not Optimal*, Numer. Math. 13, p. 354-356, 1969. - Styan, George P. H. (1973), "Hadamard Products and Multivariate Statistical Analysis",
*Linear Algebra and Its Applications***6**: 217–240, doi:10.1016/0024-3795(73)90023-2, http://dml.cz/bitstream/handle/10338.dmlcz/102190/CzechMathJ_37-1987-4_14.pdf - Williams, Virginia Vassilevska (2012-05-19). "Multiplying matrices faster than coppersmith-winograd".
*Proceedings of the 44th symposium on Theory of Computing - STOC '12*. ACM. pp. 887–898. doi:10.1145/2213977.2214056. ISBN 9781450312455. http://dl.acm.org/citation.cfm?id=2213977.2214056.

Original source: https://en.wikipedia.org/wiki/Matrix multiplication.
Read more |