Exercise: matrix times matrix

Goal

  • Multiply two matrices by hand, one output entry at a time.
  • Decide whether a product \(AB\) is even allowed (shape rule).
  • See with your own eyes that \(AB\) and \(BA\) are usually different.

The rule

To find the entry in row \(i\), column \(j\) of the product \(AB\), take row \(i\) of \(A\) and column \(j\) of \(B\), multiply them term by term, and add up.

That single number is a dot product of a row and a column:

\[ (AB)_{ij} = a_{i1}b_{1j} + a_{i2}b_{2j} + \dots \]

Shape rule (conformability): if \(A\) is \(m\times n\) and \(B\) is \(n\times p\), then \(AB\) is \(m\times p\). The two inner numbers must match (\(n = n\)). The outer numbers give the size of the answer.

\[ (m\times \underbrace{n)\;(n}_{\text{must match}}\times p) \;\longrightarrow\; m\times p \]

Tiny worked example. Multiply

\[ A=\begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}, \qquad B=\begin{bmatrix} 2 & 3 \\ 4 & 5 \end{bmatrix}. \]

Top-left entry: row 1 of \(A\) is \((1,0)\), column 1 of \(B\) is \((2,4)\), so \(1\cdot 2 + 0\cdot 4 = 2\). Doing all four entries the same way gives

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

(Here \(A\) was the identity, so nothing changed — more on that in the next file.)

Try it

Set the sizes with the sliders, then hover over (or tap) a cell of \(C\). The matching row of \(A\) and column of \(B\) light up, each pair of numbers that get multiplied has its own colour, and the sum is written out below. Type your own numbers into \(A\) and \(B\), or click Random. Use it to check your answers to the problems.

Problems

Compute each product by hand. All answers are clean integers.

Warm-up \(2\times2\) times \(2\times2\):

  1. \(\begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}\begin{bmatrix} 7 & 2 \\ 1 & 3 \end{bmatrix}\)

  2. \(\begin{bmatrix} 2 & 0 \\ 0 & 2 \end{bmatrix}\begin{bmatrix} 1 & 1 \\ 1 & 1 \end{bmatrix}\)

  3. \(\begin{bmatrix} 1 & 0 \\ 0 & 0 \end{bmatrix}\begin{bmatrix} 4 & 5 \\ 6 & 7 \end{bmatrix}\)

  4. \(\begin{bmatrix} 1 & 1 \\ 0 & 1 \end{bmatrix}\begin{bmatrix} 1 & 0 \\ 1 & 1 \end{bmatrix}\)

  5. \(\begin{bmatrix} 2 & 1 \\ 0 & 1 \end{bmatrix}\begin{bmatrix} 1 & 0 \\ 3 & 2 \end{bmatrix}\)

  6. \(\begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix}\begin{bmatrix} 5 & 6 \\ 7 & 8 \end{bmatrix}\)

Shape check — “is this product even defined?” (answer yes/no, and if yes give the size):

  1. \(A\) is \(2\times 3\), \(B\) is \(3\times 2\). Is \(AB\) defined?

  2. \(A\) is \(2\times 3\), \(B\) is \(2\times 3\). Is \(AB\) defined?

  3. \(A\) is \(3\times 1\), \(B\) is \(1\times 4\). Is \(AB\) defined?

  4. \(A\) is \(1\times 4\), \(B\) is \(3\times 1\). Is \(AB\) defined?

A few non-square products:

  1. \(\begin{bmatrix} 1 & 2 & 3 \end{bmatrix}\begin{bmatrix} 1 \\ 0 \\ 2 \end{bmatrix}\)

  2. \(\begin{bmatrix} 1 \\ 2 \end{bmatrix}\begin{bmatrix} 3 & 4 \end{bmatrix}\)

  3. \(\begin{bmatrix} 1 & 0 & 2 \\ 0 & 1 & 1 \end{bmatrix}\begin{bmatrix} 1 \\ 2 \\ 3 \end{bmatrix}\)

Order matters — show \(AB \neq BA\):

  1. With \(A=\begin{bmatrix} 1 & 1 \\ 0 & 1 \end{bmatrix}\) and \(B=\begin{bmatrix} 1 & 0 \\ 1 & 1 \end{bmatrix}\), compute both \(AB\) and \(BA\).

  2. With \(A=\begin{bmatrix} 0 & 1 \\ 0 & 0 \end{bmatrix}\) and \(B=\begin{bmatrix} 0 & 0 \\ 1 & 0 \end{bmatrix}\), compute both \(AB\) and \(BA\).

  3. With \(A=\begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix}\) and \(B=\begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix}\), compute both \(AB\) and \(BA\).

Check in R

In R, matrix multiplication is %*% (a plain * does element-by-element, which is not what we want here). Build matrices with matrix; note byrow = TRUE fills row by row.

A <- matrix(c(1, 2, 3, 4), nrow = 2, byrow = TRUE)
B <- matrix(c(5, 6, 7, 8), nrow = 2, byrow = TRUE)
A %*% B
#      [,1] [,2]
# [1,]   19   22
# [2,]   43   50
# order matters (problem 14):
A <- matrix(c(1, 1, 0, 1), nrow = 2, byrow = TRUE)
B <- matrix(c(1, 0, 1, 1), nrow = 2, byrow = TRUE)
A %*% B
#      [,1] [,2]
# [1,]    2    1
# [2,]    1    1
B %*% A
#      [,1] [,2]
# [1,]    1    1
# [2,]    1    2

Hints

A matrix with 1s on the diagonal and 0s elsewhere leaves the other matrix unchanged. Each output row just picks out one row of the other matrix.

Never try to do the whole product at once. Point at row \(i\) of the left matrix and column \(j\) of the right matrix, multiply the pairs, add. Write that one number down, then move on.

Write the two sizes side by side: \((m\times n)(n\times p)\). The two inner numbers must be equal. If they are, the answer is \(m\times p\); if not, the product is undefined.

A single row times a single column (problem 11) gives one number: a \(1\times 3\) times a \(3\times 1\) is \(1\times 1\). A single column times a single row (problem 12) gives a whole matrix: \(2\times 1\) times \(1\times 2\) is \(2\times 2\).

Just compute both products fully and compare entry by entry. If even one entry differs, then \(AB \neq BA\).

Step-by-step solutions

Problem 1. \(A\) is the identity, so it returns \(B\) unchanged. \[\begin{bmatrix} 7 & 2 \\ 1 & 3 \end{bmatrix}.\] Check one entry: row 1 \((1,0)\) · col 1 \((7,1)\) \(=1\cdot7+0\cdot1=7\). ✓

Problem 2. Each row of the left is \((2,0)\) or \((0,2)\). - \((1,1)\): \((2,0)\cdot(1,1)=2\); \((1,2)\): \((2,0)\cdot(1,1)=2\). - \((2,1)\): \((0,2)\cdot(1,1)=2\); \((2,2)\): \((0,2)\cdot(1,1)=2\). \[\begin{bmatrix} 2 & 2 \\ 2 & 2 \end{bmatrix}.\]

Problem 3. Row 1 of left is \((1,0)\), row 2 is \((0,0)\). - Row 1: \((1,0)\cdot(4,6)=4\), \((1,0)\cdot(5,7)=5\). - Row 2: \((0,0)\cdot(\dots)=0\), \(0\). \[\begin{bmatrix} 4 & 5 \\ 0 & 0 \end{bmatrix}.\]

Problem 4. \(A=\begin{bmatrix}1&1\\0&1\end{bmatrix}\), \(B=\begin{bmatrix}1&0\\1&1\end{bmatrix}\). - \((1,1)\): \((1,1)\cdot(1,1)=2\); \((1,2)\): \((1,1)\cdot(0,1)=1\). - \((2,1)\): \((0,1)\cdot(1,1)=1\); \((2,2)\): \((0,1)\cdot(0,1)=1\). \[\begin{bmatrix} 2 & 1 \\ 1 & 1 \end{bmatrix}.\]

Problem 5. \(A=\begin{bmatrix}2&1\\0&1\end{bmatrix}\), \(B=\begin{bmatrix}1&0\\3&2\end{bmatrix}\). - \((1,1)\): \((2,1)\cdot(1,3)=2+3=5\); \((1,2)\): \((2,1)\cdot(0,2)=0+2=2\). - \((2,1)\): \((0,1)\cdot(1,3)=0+3=3\); \((2,2)\): \((0,1)\cdot(0,2)=0+2=2\). \[\begin{bmatrix} 5 & 2 \\ 3 & 2 \end{bmatrix}.\]

Problem 6. \(A=\begin{bmatrix}1&2\\3&4\end{bmatrix}\), \(B=\begin{bmatrix}5&6\\7&8\end{bmatrix}\). - \((1,1)\): \((1,2)\cdot(5,7)=5+14=19\); \((1,2)\): \((1,2)\cdot(6,8)=6+16=22\). - \((2,1)\): \((3,4)\cdot(5,7)=15+28=43\); \((2,2)\): \((3,4)\cdot(6,8)=18+32=50\). \[\begin{bmatrix} 19 & 22 \\ 43 & 50 \end{bmatrix}.\]

Problem 7. Sizes \((2\times 3)(3\times 2)\). Inner numbers \(3=3\) match. Yes, defined; result is \(2\times 2\).

Problem 8. Sizes \((2\times 3)(2\times 3)\). Inner numbers \(3\) and \(2\) do not match. No, undefined.

Problem 9. Sizes \((3\times 1)(1\times 4)\). Inner numbers \(1=1\) match. Yes, defined; result is \(3\times 4\).

Problem 10. Sizes \((1\times 4)(3\times 1)\). Inner numbers \(4\) and \(3\) do not match. No, undefined.

Problem 11. \((1\times 3)(3\times 1)\) gives a single number: \[1\cdot1 + 2\cdot0 + 3\cdot2 = 1 + 0 + 6 = 7 \;\Rightarrow\; \begin{bmatrix} 7 \end{bmatrix}.\]

Problem 12. \((2\times 1)(1\times 2)\) gives a \(2\times 2\) matrix. Each entry is (row entry)·(column entry): \[\begin{bmatrix} 1\cdot3 & 1\cdot4 \\ 2\cdot3 & 2\cdot4 \end{bmatrix}=\begin{bmatrix} 3 & 4 \\ 6 & 8 \end{bmatrix}.\]

Problem 13. \((2\times 3)(3\times 1)\) gives a \(2\times 1\) column. - Row 1: \((1,0,2)\cdot(1,2,3)=1+0+6=7\). - Row 2: \((0,1,1)\cdot(1,2,3)=0+2+3=5\). \[\begin{bmatrix} 7 \\ 5 \end{bmatrix}.\]

Problem 14. \(A=\begin{bmatrix}1&1\\0&1\end{bmatrix}\), \(B=\begin{bmatrix}1&0\\1&1\end{bmatrix}\). \[AB=\begin{bmatrix} 2 & 1 \\ 1 & 1 \end{bmatrix},\qquad BA=\begin{bmatrix} 1 & 1 \\ 1 & 2 \end{bmatrix}.\] (For \(BA\): row 1 \((1,0)\cdot(1,0)=1\), \((1,0)\cdot(1,1)=1\); row 2 \((1,1)\cdot(1,0)=1\), \((1,1)\cdot(1,1)=2\).) Since \(AB\neq BA\), order matters.

Problem 15. \(A=\begin{bmatrix}0&1\\0&0\end{bmatrix}\), \(B=\begin{bmatrix}0&0\\1&0\end{bmatrix}\). \[AB=\begin{bmatrix} 1 & 0 \\ 0 & 0 \end{bmatrix},\qquad BA=\begin{bmatrix} 0 & 0 \\ 0 & 1 \end{bmatrix}.\] (For \(AB\): row 1 \((0,1)\cdot(0,1)=1\), \((0,1)\cdot(0,0)=0\); row 2 all \(0\). For \(BA\): row 1 all \(0\); row 2 \((1,0)\cdot(0,0)=0\), \((1,0)\cdot(1,0)=1\).) Again \(AB\neq BA\).

Problem 16. \(A=\begin{bmatrix}1&2\\3&4\end{bmatrix}\), \(B=\begin{bmatrix}0&1\\1&0\end{bmatrix}\) (this swaps columns / rows). \[AB=\begin{bmatrix} 2 & 1 \\ 4 & 3 \end{bmatrix},\qquad BA=\begin{bmatrix} 3 & 4 \\ 1 & 2 \end{bmatrix}.\] (\(AB\) swaps the columns of \(A\); \(BA\) swaps the rows of \(A\).) So \(AB\neq BA\).