Euler Hall — free maths guides

Multiply two matrices

Compute every entry of a product by hand for small matrices. The idea in plain words, a worked example, and a check with the answers shown — part of the free Linear Algebra guides.

One entry at a time, in its own slot

To multiplyABAB, compute one entry at a time: entry(i,j)(i,j) is rowii ofAA dotted with columnjj ofBB. Write it down in position(i,j)(i,j) immediately.

Four entries, four dot products, each of three products. That is2×2×3=122\times2\times3=12 multiplications — the numbermnpmnp again.

Two checks catch almost every slip.

1. Shape.2×32\times3 times3×23\times2 is2×22\times2. If your answer is not that shape, stop and find the error before checking a single number. 2. One entry by a different route. Column 2 ofABAB isA(0,1,1)T=col2(A)+col3(A)=(2+3, 5+6)=(5,11)A(0,1,1)^{\mathsf T}=\text{col}_2(A)+\text{col}_3(A)=(2+3,\,5+6)=(5,11), which matches the second column above. A second route that agrees is real evidence; a second look at the same working is not.

Multiplying entrywise is the classic wrong answer, and it produces a matrix of the right shape, so shape does not catch it.1⋅1, 2⋅0, 3⋅11\cdot1,\,2\cdot0,\,3\cdot1 in a row is not the product of anything.

And order matters.ABAB andBABA need not even be the same size — withAA of size2×32\times3 andBB of size3×23\times2,ABAB is2×22\times2 whileBABA is3×33\times3.

Why the answer ismnpmnp multiplications

Anm×nm\times n times ann×pn\times p givesm×pm\times p entries. Each entry is a dot product overnn terms, sonn multiplications each. Total:m⋅p⋅nm\cdot p\cdot n.

The same count comes out of the column picture: there arepp columns in the answer, each computed asAA times a vector, and each of those costsm⋅nm\cdot n. Againmnpmnp — the arithmetic cannot care which way you organise it.

And the four ways all agree. Entry by entry, a column at a time, a row at a time, or as a sum of rank-one piecesakbkTa_kb_k^{\mathsf T}: four organisations of the samemnpmnp multiplications. That is why you are free to pick the one that suits the question. Row-by-column is fastest by hand; column-at-a-time explains the most; rank-one pieces explain rank.

Worked example, in the shape the exam asks

Question. ComputeABAB for

Step 1 — shapes.AA is2×32\times3,BB is3×23\times2, soABAB is2×22\times2. It exists.

Step 2 — entry(1,1)(1,1). Row 1 ofAA is(2,1,0)(2,1,0); column 1 ofBB is(1,0,3)(1,0,3). Dot:2⋅1+1⋅0+0⋅3=22\cdot1+1\cdot0+0\cdot3=2.

Step 3 — entry(1,2)(1,2). Row 1 with column 2(2,1,0)(2,1,0):2⋅2+1⋅1+0⋅0=52\cdot2+1\cdot1+0\cdot0=5.

Step 4 — entry(2,1)(2,1). Row 2 is(1,3,1)(1,3,1) with column 1(1,0,3)(1,0,3):1⋅1+3⋅0+1⋅3=41\cdot1+3\cdot0+1\cdot3=4.

Step 5 — entry(2,2)(2,2). Row 2 with column 2(2,1,0)(2,1,0):1⋅2+3⋅1+1⋅0=51\cdot2+3\cdot1+1\cdot0=5.

Answer. AB=[2545].AB=\begin{bmatrix}2&5\\4&5\end{bmatrix}.

Step 6 — check by column. Column 1 ofABAB should be

✓, which is the first column above.

Step 7 — the reverse order, for contrast.BABA is3×33\times3, so it is not even the same size:BA≠ABBA\ne AB is decided before any arithmetic.

By the end you can
  • Compute every entry of a product by hand for small matrices
  • Verify a computed product by recomputing one entry a second way
  • Compute both AB and BA and decide whether they agree
  • Reject the entrywise product and the transposed answer
Check yourself

A=[1021],B=[0310].A=\begin{bmatrix}1&0\\2&1\end{bmatrix},\quad B=\begin{bmatrix}0&3\\1&0\end{bmatrix}.

ComputeABAB andBABA, and say whether they are equal.

Answer

AB=[0316],BA=[6310].AB=\begin{bmatrix}0&3\\1&6\end{bmatrix},\qquad BA=\begin{bmatrix}6&3\\1&0\end{bmatrix}.

Working:ABAB entry(1,1)(1,1) is1⋅0+0⋅1=01\cdot0+0\cdot1=0;(1,2)(1,2) is1⋅3+0⋅0=31\cdot3+0\cdot0=3;(2,1)(2,1) is2⋅0+1⋅1=12\cdot0+1\cdot1=1;(2,2)(2,2) is2⋅3+1⋅0=62\cdot3+1\cdot0=6.

BABA entry(1,1)(1,1) is0⋅1+3⋅2=60\cdot1+3\cdot2=6;(1,2)(1,2) is0⋅0+3⋅1=30\cdot0+3\cdot1=3;(2,1)(2,1) is1⋅1+0⋅2=11\cdot1+0\cdot2=1;(2,2)(2,2) is1⋅0+0⋅1=01\cdot0+0\cdot1=0.

They are not equal: three of the four entries differ. Note that(1,2)(1,2) happens to agree in both — which is exactly why one matching entry proves nothing and a full comparison is needed.

Work out where to start — free, no accountAnswer six real questions — free, no account