Matrix multiplication

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 and is denoted as .
Matrix multiplication was first described by the French mathematician Jacques Philippe Marie Binet in 1812, 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.
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. ; vectors in lowercase bold, e.g. ; and entries of vectors and matrices are italic (since they are numbers from a field), e.g. and . Index notation is often the clearest way to express definitions, and is used as standard in the literature. The entry of matrix is indicated by , or , whereas a numerical label (not matrix entries) on a collection of matrices is subscripted only, e.g. , etc.
Definition.
If is an matrix and is an matrix,
the "matrix product" (denoted without multiplication signs or dots) is defined to be the matrix
such that 
for and .
That is, the entry of the product is obtained by multiplying term-by-term the entries of the th row of and the th column of , and summing these products. In other words, is the dot product of the th row of and the th column of .
Therefore, can also be written as
Thus the product is defined if and only if the number of columns in equals the number of rows in , in this case .
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 and , showing how each intersection in the product matrix corresponds to a row of and a column of .
The values at the intersections marked with circles are:
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, 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 from a vector space of dimension into a vector space of dimension maps a column vector
onto the column vector
The linear map is thus defined by the matrix 
and maps the column vector formula_10 to the matrix product 
If is another linear map from the preceding vector space of dimension , into a vector space of dimension , it is represented by a matrix formula_12 A straightforward computation shows that the matrix of the composite map is the matrix product formula_13 The general formula ) that defines the function composition is instanced here as a specific case of associativity of matrix product (see below):
System of linear equations.
The general form of a system of linear equations is
Using same notation as above, such a system is equivalent with the single matrix equation
Dot product, bilinear form and inner product.
The dot product of two column vectors is the matrix product 
where formula_18 is the row vector obtained by transposing formula_10 and the resulting 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
and any inner product may be expressed as 
where formula_22 denotes the conjugate transpose of formula_10 (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, even when the product remains definite after changing the order of the factors.
Non-commutativity.
An operation is commutative if, given two elements and such that the product formula_24 is defined, then formula_25 is also defined, and formula_26
If and are matrices of respective sizes and , then formula_24 is defined if , and formula_25 is defined if . Therefore, if one of the products is defined, the other is not defined in general. If , the two products are defined, but have different sizes; thus they cannot be equal. Only if , that is, if and are square matrices of the same size, are both products defined and of the same size. Even in this case, one has in general
For example
but
This example may be expanded for showing that, if is a matrix with entries in a field , then formula_32 for every matrix with entries in , if and only if formula_33 where , and is the identity matrix. If, instead of a field, the entries are supposed to belong to a ring, then one must add the condition that belongs to the center of the ring.
One special case where commutativity does occur is when and are two (square) diagonal matrices (of the same size); then . 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 are matrices of respective sizes , , , and , one has (left distributivity)
and (right distributivity)
This results from the distributivity for coefficients by 
Product with a scalar.
If is a matrix and a scalar, then the matrices formula_38 and formula_39 are obtained by left or right multiplying all entries of by . If the scalars have the commutative property, then formula_40
If the product formula_41 is defined (that is, the number of columns of equals the number of rows of ), then 
If the scalars have the commutative property, then all four matrices are equal. More generally, all four are equal if belongs to the center of a ring containing the entries of the matrices, because in this case, for all matrices .
These properties result from the bilinearity of the product of scalars:
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 
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 and is reversed, when one expands the definition of the matrix product.
Complex conjugate.
If and have complex entries, then
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 and have complex entries, one has
where denotes the conjugate transpose (conjugate of the transpose, or equivalently transpose of the conjugate).
Associativity.
Given three matrices and , the products and are defined if and only if the number of columns of equals the number of rows of , and the number of columns of equals the number of rows of (in particular, if one of the products is defined, then the other is also defined). In this case, one has the associative property
As for any associative operation, this allows omitting parentheses, and writing the above products as 
This extends naturally to the product of any number of matrices provided that the dimensions match. That is, if are matrices such that the number of columns of equals the number of rows of for , then the product 
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.
Complexity is not associative.
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 and are matrices of respective sizes , computing needs multiplications, while computing needs multiplications.
Algorithms have been designed for choosing the best order of products, see Matrix chain multiplication. When the number of matrices increases, it has been shown that the choice of the best order has a complexity of formula_51
Application to similarity.
Any invertible matrix formula_52 defines a similarity transformation (on square matrices of the same size as formula_52)
Similarity transformations map product to products, that is 
In fact, one has 
Square matrices.
Let us denote formula_57 the set of square matrices with entries in a ring , which, in practice, is often a field.
In formula_57, the product is defined for every pair of matrices. This makes formula_57 a ring, which has the identity matrix as identity element (the matrix whose diagonal entries are equal to 1 and all other entries are 0). This ring is also an associative -algebra.
If , 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 is denoted , and, thus verifies
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
When 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 
The other matrix invariants do not behave as well with products. Nevertheless, if is commutative, and have the same trace, the same characteristic polynomial, and the same eigenvalues with the same multiplicities. However, the eigenvectors are generally different if .
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,
Computing the th power of a matrix needs 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 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 th power of a diagonal matrix is obtained by raising the entries to the power :
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. 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 matrices over a ring form a ring, which is noncommutative except if 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 , a matrix has an inverse if and only if its determinant has a multiplicative inverse in . The determinant of a product of square matrices is the product of the determinants of the factors. The 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 of the definition requires, in the worst case, multiplications of scalars and additions for computing the product of two square matrices. Its computational complexity is therefore , in a model of computation for which the scalar operations require a constant time (in practice, this is the case for floating point numbers, but not for integers).
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 formula_67
The exponent appearing in the complexity of matrix multiplication has been improved several times,
leading to the Coppersmith–Winograd algorithm with a complexity of (1990).
This algorithm has been slightly improved in 2010 by Stothers to a complexity of ,
in 2013 by Virginia Vassilevska Williams to ,
and in 2014 by François Le Gall to .
This was further refined in 2020 by Josh Alman and Virginia Vassilevska Williams to a final (up to date) complexity of .
The greatest lower bound for the exponent of matrix multiplication algorithm is generally called . It is trivially true that , because one must read the elements of a matrix in order to multiply it with another matrix. Thus . It is unknown whether . The largest known lower bound for matrix-multiplication complexity is , for a restricted kind of arithmetic circuits, and is due to Ran Raz.
Related complexities.
The importance of the computational complexity of matrix multiplication relies on the facts that many algorithmic problems may be solved by means of matrix computation, and most problems on matrices have a complexity which is either the same as that of matrix multiplication (up to a multiplicative constant), or may be expressed in term of the complexity of matrix multiplication or its exponent formula_68
There are several advantages of expressing complexities in terms of the exponent formula_69 of matrix multiplication. Firstly, if formula_69 is improved, this will automatically improve the known upper bound of complexity of many algorithms. Secondly, in practical implementations, one never uses the matrix multiplication algorithm that has the best asymptotical complexity, because the constant hidden behind the big O notation is too large for making the algorithm competitive for sizes of matrices that can be manipulated in a computer. Thus expressing complexities in terms of formula_69 provide a more realistic complexity, since it remains valid whichever algorithm is chosen for matrix computation.
Problems that have the same asymptotic complexity as matrix multiplication include determinant, matrix inversion, Gaussian elimination (see next section). Problems with complexity that is expressible in terms of formula_69 include characteristic polynomial, eigenvalues (but not eigenvectors), Hermite normal form, and Smith normal form.
Matrix inversion, determinant and Gaussian elimination.
In his 1969 paper, where he proved the complexity formula_73 for matrix computation, Strassen proved also that matrix inversion, determinant and Gaussian elimination have, up to a multiplicative constant, the same computational complexity as matrix multiplication. The proof does not make any assumptions on matrix multiplication that is used, except that its complexity is formula_74 for some formula_75
The starting point of Strassen's proof is using block matrix multiplication. Specifically, a matrix of even dimension may be partitioned in four blocks
Under this form, its inverse is 
provided that and formula_78 are invertible.
Thus, the inverse of a matrix may be computed with two inversions, six multiplications and four additions or additive inverses of matrices. It follows that, denoting respectively by , and the number of operations needed for inverting, multiplying and adding matrices, one has 
If formula_80 one may apply this formula recursively:
If formula_82 and formula_83 one gets eventually 
for some constant .
For matrices whose dimension is not a power of two, the same complexity is reached by increasing the dimension of the matrix to a power of two, by padding the matrix with rows and columns whose entries are 1 on the diagonal and 0 elsewhere.
This proves the asserted complexity for matrices such that all submatrices that have to be inverted are indeed invertible. This complexity is thus proved for almost all matrices, as a matrix with randomly chosen entries is invertible with probability one.
The same argument applies to LU decomposition, as, if the matrix is invertible, the equality
defines a block LU decomposition that may be applied recursively to formula_86 and formula_87 for getting eventually a true LU decomposition of the original matrix.
The argument applies also for the determinant, since it results from the block LU decomposition that 