For matrix multiplication, the number of columns in the first matrix must be equal to the number of rows in the second matrix. The result matrix has the number of rows of the first and the number of columns of the second matrix.
Inmathematics, specifically inlinear algebra,matrix multiplication is abinary operation that produces amatrix 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 thematrix product, has the number of rows of the first and the number of columns of the second matrix. The product of matricesA andB is denoted asAB.[1]
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 anda.Index notation is often the clearest way to express definitions, and is used as standard in the literature. The entry in rowi, columnj of matrixA is indicated by(A)ij,Aij oraij. In contrast, a single subscript, e.g.A1,A2, is used to select a matrix (not a matrix entry) from a collection of matrices.
IfA is anm ×n matrix andB is ann ×p matrix,thematrix productC =AB (denoted without multiplication signs or dots) is defined to be them ×p matrix[5][6][7][8]such thatfori = 1, ...,m andj = 1, ...,p.
That is, the entry of the product is obtained by multiplying term-by-term the entries of theith row ofA and thejth column ofB, and summing thesen products. In other words, is thedot product of theith row ofA and thejth column ofB.
Therefore,AB can also be written as
Thus the productAB is defined if and only if the number of columns inA equals the number of rows inB,[1] in this casen.
In most scenarios, the entries are numbers, but they may be any kind ofmathematical objects for which an addition and a multiplication are defined, that areassociative, and such that the addition iscommutative, and the multiplication isdistributive with respect to the addition. In particular, the entries may be matrices themselves (seeblock matrix).
A vector of length can be viewed as acolumn vector, corresponding to an matrix whose entries are given by If is an matrix, the matrix-times-vector product denoted by is then the vector that, viewed as a column vector, is equal to the matrix In index notation, this amounts to:
One way of looking at this is that the changes from "plain" vector to column vector and back are assumed and left implicit.
Similarly, a vector of length can be viewed as arow vector, corresponding to a matrix. To make it clear that a row vector is meant, it is customary in this context to represent it as thetranspose of a column vector; thus, one will see notations such as The identity holds. In index notation, if is an matrix, amounts to:
Thedot product of two vectors and of equal length is equal to the single entry of the matrix resulting from multiplying these vectors as a row and a column vector, thus: (or which results in the same matrix).
The figure to the right illustrates diagrammatically the product of two matricesA andB, showing how each intersection in the product matrix corresponds to a row ofA and a column ofB.
The values at the intersections, marked with circles in figure to the right, are:
Historically, matrix multiplication has been introduced for facilitating and clarifying computations inlinear algebra. This strong relationship between matrix multiplication and linear algebra remains fundamental in all mathematics, as well as inphysics,chemistry,engineering andcomputer science.
If avector space has a finitebasis, its vectors are each uniquely represented by a finitesequence of scalars, called acoordinate vector, whose elements are thecoordinates of the vector on the basis. These coordinate vectors form another vector space, which isisomorphic to the original vector space. A coordinate vector is commonly organized as acolumn matrix (also called acolumn 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.
Alinear mapA from a vector space of dimensionn into a vector space of dimensionm maps a column vector
onto the column vector
The linear mapA is thus defined by the matrix
and maps the column vector to the matrix product
IfB is another linear map from the preceding vector space of dimensionm, into a vector space of dimensionp, it is represented by a matrix A straightforward computation shows that the matrix of thecomposite map is the matrix product The general formula) that defines the function composition is instanced here as a specific case of associativity of matrix product (see§ Associativity below):
Using aCartesian coordinate system in a Euclidean plane, therotation by an angle around theorigin is a linear map.More precisely,where the source point and its image are written as column vectors.
The composition of the rotation by and that by then corresponds to the matrix productwhere appropriatetrigonometric identities are employed for the second equality.That is, the composition corresponds to the rotation by angle, as expected.
The computation of the bottom left entry of corresponds to the consideration of all paths (highlighted) from basic commodity to final product in the production flow graph.
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, one unit of basic commodity, two units of, no units of, and one unit of are needed, corresponding to the first column of.
Using matrix multiplication, compute
this matrix directly provides the amounts of basic commodities needed for given amounts of final goods. For example, the bottom left entry of is computed as, reflecting that units of are needed to produce one unit of. Indeed, one unit is needed for, one for each of two, and for each of the four units that go into the unit, see picture.
In order to produce e.g. 100 units of the final product, 80 units of, and 60 units of, the necessary amounts of basic goods can be computed as
that is, units of, units of, units of, units of are needed.Similarly, the product matrix can be used to compute the needed amounts of basic goods for other final-good amount data.[9]
Matrix multiplication shares some properties with usualmultiplication. 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 isnon-commutative,[10] even when the product remains defined after changing the order of the factors.[11][12]
An operation iscommutative if, given two elementsA andB such that the product is defined, then is also defined, and
IfA andB are matrices of respective sizes and, then is defined if, and is defined if. Therefore, if one of the products is defined, the other one need not be defined. If, the two products are defined, but have different sizes; thus they cannot be equal. Only if, that is, ifA andB aresquare 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, ifA is a matrix with entries in afieldF, then for every matrixB with entries inF,if and only if where, andI is theidentity matrix. If, instead of a field, the entries are supposed to belong to aring, then one must add the condition thatc belongs to thecenter of the ring.
One special case where commutativity does occur is whenD andE are two (square)diagonal matrices (of the same size); thenDE =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.
The matrix product isdistributive with respect tomatrix addition. That is, ifA,B,C,D are matrices of respective sizesm ×n,n ×p,n ×p, andp ×q, respectively, one has (left distributivity)
IfA is a matrix andc a scalar, then the matrices and are obtained by left or right multiplying all entries ofA byc. If the scalars have thecommutative property, then
If the product is defined (that is, the number of columns ofA equals the number of rows ofB), then
and
If the scalars have the commutative property, then all four matrices are equal. More generally, all four are equal ifc belongs to thecenter of aring containing the entries of the matrices, because in this case,cX =Xc for all matricesX.
These properties result from thebilinearity of the product of scalars:
If the scalars have thecommutative property, thetranspose of a product of matrices is the product, in the reverse order, of the transposes of the factors. That is
whereT 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 ofA andB is reversed, when one expands the definition of the matrix product.
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, ifA andB have complex entries, one has
where† denotes theconjugate transpose (conjugate of the transpose, or equivalently transpose of the conjugate).
Given three matricesA,B andC, the products(AB)C andA(BC) are defined if and only if the number of columns ofA equals the number of rows ofB, and the number of columns ofB equals the number of rows ofC (in particular, if one of the products is defined, then the other is also defined). In this case, one has theassociative 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, ifA1,A2, ...,An are matrices such that the number of columns ofAi equals the number of rows ofAi + 1 fori = 1, ...,n – 1, then the product
These properties may be proved by straightforward but complicatedsummation manipulations. This result also follows from the fact that matrices representlinear maps. Therefore, the associative property of matrices is simply a specific case of the associative property offunction composition.
Computational complexity depends on parenthesization
Although the result of a sequence of matrix products does not depend on theorder of operation (provided that the order of the matrices is not changed), thecomputational complexity may depend dramatically on this order.
For example, ifA,B andC are matrices of respective sizes10×30, 30×5, 5×60, computing(AB)C needs10×30×5 + 10×5×60 = 4,500 multiplications, while computingA(BC) needs30×5×60 + 10×30×60 = 27,000 multiplications.
Algorithms have been designed for choosing the best order of products; seeMatrix chain multiplication. When the numbern of matrices increases, it has been shown that the choice of the best order has a complexity of[13][14]
Let us denote the set ofn×nsquare matrices with entries in aringR, which, in practice, is often afield.
In, the product is defined for every pair of matrices. This makes aring, which has theidentity matrixI as anidentity element (the matrix whose diagonal entries are equal to 1 and all other entries are 0). This ring is also anassociativeR-algebra.
Ifn > 1, many matrices do not have amultiplicative 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 matrixA is denotedA−1, and, thus verifies
A product of matrices is invertible if and only if each factor is invertible. In this case, one has
WhenR iscommutative, and, in particular, when it is a field, thedeterminant of a product is the product of the determinants. As determinants are scalars, and scalars commute, one has thus
The other matrixinvariants do not behave as well with products. Nevertheless, ifR is commutative,AB andBA have the sametrace, the samecharacteristic polynomial, and the sameeigenvalues with the same multiplicities. However, theeigenvectors are generally different ifAB ≠BA.
One may raise a square matrix to anynonnegative integer power multiplying it by itself repeatedly in the same way as for ordinary numbers. That is,
Computing thekth power of a matrix needsk – 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 usingexponentiation by squaring, which requires less than2 log2k matrix multiplications, and is therefore much more efficient.
An easy case for exponentiation is that of adiagonal matrix. Since the product of diagonal matrices amounts to simply multiplying corresponding diagonal elements together, thekth power of a diagonal matrix is obtained by raising the entries to the powerk:
The definition of matrix product requires that the entries belong to a semiring, and does not require multiplication of elements of the semiring to becommutative. In many applications, the matrix elements belong to a field, although thetropical semiring is also a common choice for graphshortest path problems.[15] Even in the case of matrices over fields, the product is not commutative in general, although it isassociative and isdistributive overmatrix addition. Theidentity matrices (which are thesquare matrices whose entries are zero outside of the main diagonal and 1 on the main diagonal) areidentity elements of the matrix product. It follows that then ×n matrices over aring form a ring, which is noncommutative except ifn = 1 and the ground ring is commutative.
A square matrix may have amultiplicative inverse, called aninverse matrix. In the common case where the entries belong to acommutative ringR, a matrix has an inverse if and only if itsdeterminant has a multiplicative inverse inR. The determinant of a product of square matrices is the product of the determinants of the factors. Then ×n matrices that have an inverse form agroup under matrix multiplication, thesubgroups of which are calledmatrix groups. Many classical groups (including allfinite groups) areisomorphic to matrix groups; this is the starting point of the theory ofgroup representations.
Matrices are themorphisms of acategory, thecategory of matrices. The objects are thenatural numbers that measure the size of matrices, and the composition of morphisms is matrix multiplication. The source of a morphism is the number of columns of the corresponding matrix, and the target is the number of rows.
Improvement of estimates of exponentω over time for the computational complexity of matrix multiplication
The matrix multiplicationalgorithm that results from the definition requires, in theworst case, multiplications and additions of scalars to compute the product of two squaren×n matrices. Itscomputational complexity is therefore, in amodel of computation for which the scalar operations take constant time.
Rather surprisingly, this complexity is not optimal, as shown in 1969 byVolker Strassen, who provided an algorithm, now calledStrassen's algorithm, with a complexity of[16]Strassen's algorithm can be parallelized to further improve the performance.[17]As of January 2024[update], the best peer-reviewed matrix multiplication algorithm is byVirginia Vassilevska Williams, Yinzhan Xu, Zixuan Xu, and Renfei Zhou and has complexityO(n2.371552).[18][19]It is not known whether matrix multiplication can be performed inn2 + o(1) time.[20] This would be optimal, since one must read the 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 throughoutnumerical linear algebra andtheoretical computer science.
^Vassilevska Williams, Virginia; Xu, Yinzhan; Xu, Zixuan; Zhou, Renfei.New Bounds for Matrix Multiplication: from Alpha to Omega. Proceedings of the 2024 Annual ACM-SIAM Symposium on Discrete Algorithms (SODA). pp. 3792–3835.arXiv:2307.07970.doi:10.1137/1.9781611977912.134.
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.
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.