Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

3.5: Matrix multiplication

Something weโ€™ve explored over the past few sections is the idea of performing two operations back to back. We could stretch a vector and then rotate it, for example. Can we describe the combined operation with just one matrix?

Yes! Applying two linear transformations back to back corresponds to multiplying two matrices together, which we will explore here.


The golden ruleยถ

Before we figure out how to multiply two matrices, letโ€™s check when we can.

For example, a 2ร—32\times3 matrix can multiply a 3ร—43\times4 matrix, and the answer has shape 2ร—42\times4. A 2ร—32\times3 matrix cannot multiply a 2ร—42\times4 matrix: the inner numbers, 3 and 2, donโ€™t match. This means that matrix multiplication is not commutative: just because ABAB is defined (meaning it is legal, by the golden rule, to multiply AA on the right by BB) does not mean BABA is defined.

This is the same rule we used for matrix-vector multiplication. A vector is just a matrix with one column. Now the second matrix can have several columns.


One column at a timeยถ

Multiply the first matrix by each column of the second matrix, then put the answers next to each other. Consider

A=[4โˆ’37โˆ’260],B=[5โˆ’4โˆ’283โˆ’6].A=\begin{bmatrix}4&-3&7\\-2&6&0\end{bmatrix},\qquad B=\begin{bmatrix}{\color{#3d81f6}5}&{\color{orange}-4}\\{\color{#3d81f6}-2}&{\color{orange}8}\\{\color{#3d81f6}3}&{\color{orange}-6}\end{bmatrix}.

The shapes are (2ร—3)(3ร—2)(2\times3)(3\times2), so ABAB has shape 2ร—22\times2. For the first column,

A[5โˆ’23]=[4(5)โˆ’3(โˆ’2)+7(3)โˆ’2(5)+6(โˆ’2)+0(3)]=[47โˆ’22].A{\color{#3d81f6}\begin{bmatrix}5\\-2\\3\end{bmatrix}} =\begin{bmatrix}4({\color{#3d81f6}5})-3({\color{#3d81f6}-2})+7({\color{#3d81f6}3})\\-2({\color{#3d81f6}5})+6({\color{#3d81f6}-2})+0({\color{#3d81f6}3})\end{bmatrix} ={\color{#3d81f6}\begin{bmatrix}47\\-22\end{bmatrix}}.

For the second column,

A[โˆ’48โˆ’6]=[4(โˆ’4)โˆ’3(8)+7(โˆ’6)โˆ’2(โˆ’4)+6(8)+0(โˆ’6)]=[โˆ’8256].A{\color{orange}\begin{bmatrix}-4\\8\\-6\end{bmatrix}} =\begin{bmatrix}4({\color{orange}-4})-3({\color{orange}8})+7({\color{orange}-6})\\-2({\color{orange}-4})+6({\color{orange}8})+0({\color{orange}-6})\end{bmatrix} ={\color{orange}\begin{bmatrix}-82\\56\end{bmatrix}}.

Place the two output vectors next to each other, keeping their order:

AB=[47โˆ’82โˆ’2256].AB=\begin{bmatrix}{\color{#3d81f6}47}&{\color{orange}-82}\\{\color{#3d81f6}-22}&{\color{orange}56}\end{bmatrix}.

We used matrix-vector multiplication twice. Use the interactive below to follow either column.

Loading...

Square exampleยถ

Let

C=[2โˆ’113],D=[142โˆ’1].C=\begin{bmatrix}2&-1\\1&3\end{bmatrix},\qquad D=\begin{bmatrix}{\color{#3d81f6}1}&{\color{orange}4}\\{\color{#3d81f6}2}&{\color{orange}-1}\end{bmatrix}.

Multiplying one column at a time gives

C[12]=[07],C[4โˆ’1]=[91],C{\color{#3d81f6}\begin{bmatrix}1\\2\end{bmatrix}}={\color{#3d81f6}\begin{bmatrix}0\\7\end{bmatrix}},\qquad C{\color{orange}\begin{bmatrix}4\\-1\end{bmatrix}}={\color{orange}\begin{bmatrix}9\\1\end{bmatrix}},

so

CD=[0971].CD=\begin{bmatrix}{\color{#3d81f6}0}&{\color{orange}9}\\{\color{#3d81f6}7}&{\color{orange}1}\end{bmatrix}.

Two transformationsยถ

Suppose we first project onto the line spanned by [12]\begin{bmatrix}1\\2\end{bmatrix}, then rotate counterclockwise by 30โˆ˜30^\circ. The two matrices are

P=15[1224],R=[32โˆ’121232].P=\frac15\begin{bmatrix}1&2\\2&4\end{bmatrix},\qquad R=\begin{bmatrix}\frac{\sqrt3}{2}&-\frac12\\\frac12&\frac{\sqrt3}{2}\end{bmatrix}.

For vโƒ—=[50]\vec v=\begin{bmatrix}5\\0\end{bmatrix},

vโƒ—โ†’ย Pย [12]โ†’ย Rย [32โˆ’112+3].\vec v\xrightarrow{\ P\ }\begin{bmatrix}1\\2\end{bmatrix} \xrightarrow{\ R\ }\begin{bmatrix}\frac{\sqrt3}{2}-1\\\frac12+\sqrt3\end{bmatrix}.

One matrix performs both steps:

RP=110[3โˆ’223โˆ’41+232+43].RP=\frac1{10}\begin{bmatrix}\sqrt3-2&2\sqrt3-4\\1+2\sqrt3&2+4\sqrt3\end{bmatrix}.

Multiplying (RP)vโƒ—(RP)\vec v gives the same final vector. Because the projection happens first, its matrix appears closest to vโƒ—\vec v.

Stretch, then rotateยถ

Now suppose we stretch horizontally by a factor of 2 and then rotate counterclockwise by 90โˆ˜90^\circ:

H=[2001],R=[0โˆ’110].H=\begin{bmatrix}2&0\\0&1\end{bmatrix},\qquad R=\begin{bmatrix}0&-1\\1&0\end{bmatrix}.

For an input [xy]\begin{bmatrix}x\\y\end{bmatrix}, the two steps are

[xy]โ†’ย Hย [2xy]โ†’ย Rย [โˆ’y2x].\begin{bmatrix}x\\y\end{bmatrix} \xrightarrow{\ H\ }\begin{bmatrix}2x\\y\end{bmatrix} \xrightarrow{\ R\ }\begin{bmatrix}-y\\2x\end{bmatrix}.

The final rule has matrix [0โˆ’120]\begin{bmatrix}0&-1\\2&0\end{bmatrix}. And thatโ€™s exactly what we get by multiplying:

RH=[0โˆ’110][2001]=[0โˆ’120].RH=\begin{bmatrix}0&-1\\1&0\end{bmatrix}\begin{bmatrix}2&0\\0&1\end{bmatrix} =\begin{bmatrix}0&-1\\2&0\end{bmatrix}.

Thatโ€™s why stretching first and rotating second gives RHRH. For example,

[21]โ†’ย Hย [41]โ†’ย Rย [โˆ’14],(RH)[21]=[โˆ’14].\begin{bmatrix}2\\1\end{bmatrix}\xrightarrow{\ H\ }\begin{bmatrix}4\\1\end{bmatrix} \xrightarrow{\ R\ }\begin{bmatrix}-1\\4\end{bmatrix},\qquad (RH)\begin{bmatrix}2\\1\end{bmatrix}=\begin{bmatrix}-1\\4\end{bmatrix}.

Order mattersยถ

Would rotating first and stretching second give the same result? Letโ€™s check:

HR=[2001][0โˆ’110]=[0โˆ’210].HR=\begin{bmatrix}2&0\\0&1\end{bmatrix}\begin{bmatrix}0&-1\\1&0\end{bmatrix} =\begin{bmatrix}0&-2\\1&0\end{bmatrix}.

This is different from RHRH. Our same input now follows

[21]โ†’ย Rย [โˆ’12]โ†’ย Hย [โˆ’22].\begin{bmatrix}2\\1\end{bmatrix}\xrightarrow{\ R\ }\begin{bmatrix}-1\\2\end{bmatrix} \xrightarrow{\ H\ }\begin{bmatrix}-2\\2\end{bmatrix}.

Why did the order matter? The stretch always doubles the horizontal coordinate. Rotating changes which part of the original vector points horizontally. Try both orders below.

Loading...

In general, ABโ‰ BAAB\ne BA. Unlike multiplication of real numbers, matrix multiplication is not commutative.

Sometimes reversing the order changes even the shape of the answer. For example, let

A=[100111],B=[100010].A=\begin{bmatrix}1&0\\0&1\\1&1\end{bmatrix},\qquad B=\begin{bmatrix}1&0&0\\0&1&0\end{bmatrix}.

These are the two transformations at the end of Chapter 3.4. Multiplying gives

BA=[1001],AB=[100010110].BA=\begin{bmatrix}1&0\\0&1\end{bmatrix},\qquad AB=\begin{bmatrix}1&0&0\\0&1&0\\1&1&0\end{bmatrix}.

The first product has shape 2ร—22\times2: append the sum of two entries, then discard it, and youโ€™re back where you started. The second has shape 3ร—33\times3: discard the third entry, then replace it with the sum of the first two. You wonโ€™t generally get the original third entry back.

Sometimes a product is defined in only one order. For example, let

C=[120โˆ’103],D=[102โˆ’101322โˆ’101].C=\begin{bmatrix}1&2&0\\-1&0&3\end{bmatrix},\qquad D=\begin{bmatrix}1&0&2&-1\\0&1&3&2\\2&-1&0&1\end{bmatrix}.

Here, CC has shape 2ร—32\times3 and DD has shape 3ร—43\times4. The product CDCD is defined and has shape 2ร—42\times4:

CD=[120โˆ’103][102โˆ’101322โˆ’101]=[12835โˆ’3โˆ’24].CD=\begin{bmatrix}1&2&0\\-1&0&3\end{bmatrix}\begin{bmatrix}1&0&2&-1\\0&1&3&2\\2&-1&0&1\end{bmatrix} =\begin{bmatrix}1&2&8&3\\5&-3&-2&4\end{bmatrix}.

But DCDC is not defined: its shapes would be (3ร—4)(2ร—3)(3\times4)(2\times3), and the inner sizes, 4 and 2, do not match.

It is possible for AB=BAAB=BA in special cases. For instance, doubling every coordinate and then rotating has the same effect as rotating and then doubling every coordinate. But this is not always true: we cannot assume that AB=BAAB=BA for arbitrary matrices.


Propertiesยถ

We can still use several familiar rules. Whenever the shapes allow the operations,

(AB)C=A(BC),A(B+C)=AB+AC,(A+B)C=AC+BC,c(AB)=(cA)B=A(cB).\begin{aligned} (AB)C&=A(BC),\\ A(B+C)&=AB+AC,\\ (A+B)C&=AC+BC,\\ c(AB)&=(cA)B=A(cB). \end{aligned}

The first property lets us change the parentheses, keeping the matrices in the same order. Both sides apply CC first, then BB, then AA. Changing the parentheses is different from reversing the order!

The identity matrix leaves vectors unchanged, so for a matrix AA of shape nร—dn\times d,

InA=A=AId.I_nA=A=AI_d.

Projecting twiceยถ

Hereโ€™s one last example. If P=[1000]P=\begin{bmatrix}1&0\\0&0\end{bmatrix} projects onto the xx-axis, then

P2=[1000][1000]=[1000]=P.P^2=\begin{bmatrix}1&0\\0&0\end{bmatrix}\begin{bmatrix}1&0\\0&0\end{bmatrix} =\begin{bmatrix}1&0\\0&0\end{bmatrix}=P.

Does that make sense geometrically? After the first projection, our vector is already on the xx-axis. Projecting it again leaves it there.


Expanding matrix expressionsยถ

The distributive properties let us expand expressions much as we do with numbers. We just need to keep the factors in their original order. Letโ€™s start with

A=[120โˆ’1],B=[2011],C=[โˆ’1302].A=\begin{bmatrix}1&2\\0&-1\end{bmatrix},\qquad B=\begin{bmatrix}2&0\\1&1\end{bmatrix},\qquad C=\begin{bmatrix}-1&3\\0&2\end{bmatrix}.

Adding first gives

B+C=[1313],A(B+C)=[39โˆ’1โˆ’3].B+C=\begin{bmatrix}1&3\\1&3\end{bmatrix},\qquad A(B+C)=\begin{bmatrix}3&9\\-1&-3\end{bmatrix}.

Multiplying first gives

AB=[42โˆ’1โˆ’1],AC=[โˆ’170โˆ’2].AB=\begin{bmatrix}4&2\\-1&-1\end{bmatrix},\qquad AC=\begin{bmatrix}-1&7\\0&-2\end{bmatrix}.

Their sum is the same matrix! This is an example of A(B+C)=AB+ACA(B+C)=AB+AC. Notice that AA stays on the left of both terms. Similarly, in (B+C)A=BA+CA(B+C)A=BA+CA, it stays on the right.

Squaring a sumยถ

Now suppose AA and BB are square matrices of the same shape. Recall that (A+B)2(A+B)^2 means (A+B)(A+B)(A+B)(A+B). Expanding gives

(A+B)2=A(A+B)+B(A+B)=A2+AB+BA+B2.\begin{aligned} (A+B)^2&=A(A+B)+B(A+B)\\ &=A^2+AB+BA+B^2. \end{aligned}

With real numbers, we would combine the middle two terms. Can we do that here? Only if AB=BAAB=BA. In general, they are different matrices, so AB+BAAB+BA cannot be replaced by 2AB2AB.

The horizontal stretch and rotation from earlier give a concrete example where they donโ€™t commute. Doubling every coordinate and rotating gives an example where they do. The distributive property works in both cases; whether we can combine terms depends on the particular matrices.