← mapVectors & Matrices

Matrix Multiplication

⚗ Dr. Möbius, from the lab

Listen carefully, because I'm about to explain why matrix multiplication looks the way it does — and if you've ever stared at the row-times-column rule and thought "who the hell designed this?", the answer is: mathematics itself, not a sadist. The rule is forced on us, inevitably, the moment you ask "what matrix represents two transformations applied one after the other?" That question has one answer. We're going to derive it right now, in this lab, and when we're done you will never again look at that rule and feel confused. You may feel other things. Possibly awe. Possibly mild existential dread. Both are correct responses.

THE BIG IDEA

Matrix multiplication is defined by the requirement that it represent function composition: (AB)v = A(Bv) for every vector v, and this forces the row-times-column dot-product rule.

The rule, and why it exists

Before I state the rule, I need you to understand where it comes from — because without the "why," you're just memorizing a mechanical procedure, and that is a waste of both our time. We built functions in the Sets stratum. Matrices are functions on Rn\mathbb{R}^n (full story next lesson). When you compose two functions gfg \circ f, you want a single object that represents "apply ff first, then gg." For matrices, that single object is the product ABAB — and the requirement that (AB)v=A(Bv)(AB)\vec{v} = A(B\vec{v}) for every vector v\vec{v} is what forces the multiplication rule. The rule is not arbitrary. It is the only rule consistent with composition. Remember when the Sets stratum showed that composition is almost never commutative? We're about to see that explode spectacularly, and it'll be fucking glorious.

The product rule: For AA (m×nm \times n) and BB (n×pn \times p), the product ABAB is the m×pm \times p matrix whose entry in row ii, column jj is the dot product of row ii of AA with column jj of BB:

(AB)ij=k=1naikbkj=(row i of A)(col j of B)(AB)_{ij} = \sum_{k=1}^{n} a_{ik} b_{kj} = (\text{row } i \text{ of } A) \cdot (\text{col } j \text{ of } B)

Every entry of ABAB is a dot product. This is not coincidental — it's the algebraic trace of the composition requirement.

Dimension compatibility: the handshake requirement

For ABAB to exist, the inner dimensions must match: AA is m×nm \times n, BB must be n×pn \times p for some pp. The result is m×pm \times p.

(m×nA)(n×pB)=(m×pAB)(\underbrace{m \times n}_{A})(\underbrace{n \times p}_{B}) = (\underbrace{m \times p}_{AB})

The inner nn's must agree — they "cancel". Think of it as a handshake: the number of columns in the left matrix must equal the number of rows in the right matrix, or they refuse to shake hands and the product is undefined.

A concrete product

Let A=(1234)A = \begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix} and B=(5678)B = \begin{pmatrix} 5 & 6 \\ 7 & 8 \end{pmatrix}, both 2×22\times 2.

(AB)11=(1)(5)+(2)(7)=5+14=19(AB)_{11} = (1)(5) + (2)(7) = 5 + 14 = 19 (AB)12=(1)(6)+(2)(8)=6+16=22(AB)_{12} = (1)(6) + (2)(8) = 6 + 16 = 22 (AB)21=(3)(5)+(4)(7)=15+28=43(AB)_{21} = (3)(5) + (4)(7) = 15 + 28 = 43 (AB)22=(3)(6)+(4)(8)=18+32=50(AB)_{22} = (3)(6) + (4)(8) = 18 + 32 = 50

AB=(19224350)AB = \begin{pmatrix} 19 & 22 \\ 43 & 50 \end{pmatrix}

Each entry required a dot product: row of the left matrix against column of the right. Every. Single. Entry.

ABBAAB \ne BA in general

Now watch this. Using the same matrices:

(BA)11=(5)(1)+(6)(3)=5+18=23(BA)_{11} = (5)(1) + (6)(3) = 5 + 18 = 23 (BA)12=(5)(2)+(6)(4)=10+24=34(BA)_{12} = (5)(2) + (6)(4) = 10 + 24 = 34 (BA)21=(7)(1)+(8)(3)=7+24=31(BA)_{21} = (7)(1) + (8)(3) = 7 + 24 = 31 (BA)22=(7)(2)+(8)(4)=14+32=46(BA)_{22} = (7)(2) + (8)(4) = 14 + 32 = 46

BA=(23343146)AB=(19224350)BA = \begin{pmatrix} 23 & 34 \\ 31 & 46 \end{pmatrix} \ne AB = \begin{pmatrix} 19 & 22 \\ 43 & 50 \end{pmatrix}

Matrix multiplication is not commutative. This should not be surprising — you already knew from the Sets stratum that composition of functions is not commutative. "Put on socks then shoes" and "put on shoes then socks" are not the same sequence. ABAB is "apply BB first, then AA". BABA is "apply AA first, then BB". Different order, different result. The non-commutativity of matrix multiplication is not a bug, it is not a quirk, it is not something to be apologized for. It is the direct mathematical signature of the non-commutativity of function composition, made concrete and inescapable. Every time a student writes AB=BAAB = BA without checking, somewhere in this lab a beaker cracks.

The matrix-vector product: columns are key

A special and vital reading of matrix multiplication: AxA\vec{x} for a matrix AA (m×nm \times n) and a column vector x\vec{x} (n×1n \times 1) is a matrix product yielding an m×1m \times 1 vector.

Ax=(a11a12a21a22)(x1x2)=(a11x1+a12x2a21x1+a22x2)A\vec{x} = \begin{pmatrix} a_{11} & a_{12} \\ a_{21} & a_{22} \end{pmatrix} \begin{pmatrix} x_1 \\ x_2 \end{pmatrix} = \begin{pmatrix} a_{11} x_1 + a_{12} x_2 \\ a_{21} x_1 + a_{22} x_2 \end{pmatrix}

But here's the reading you should burn into your memory: if c1,c2\vec{c}_1, \vec{c}_2 are the columns of AA, then

Ax=x1c1+x2c2A\vec{x} = x_1 \vec{c}_1 + x_2 \vec{c}_2

AxA\vec{x} is a linear combination of AA's columns, with the entries of x\vec{x} as the coefficients. The components of x\vec{x} are the mixing weights. This is THE most important reading of matrix-vector multiplication, and we will use it constantly — burn it into your skull: in understanding systems of equations (Ax=bA\vec{x} = \vec{b} is solvable iff b\vec{b} is a combination of AA's columns), in understanding transformations (next lesson), and throughout the Vector Spaces stratum. If you take nothing else from this lesson, take that sentence. Write it on your hand. Tattoo it somewhere tasteful.

🔬 SPECIMENS (worked examples)

Worked example 1 — computing a product step by step

Compute ABAB for A=(2113)A = \begin{pmatrix} 2 & 1 \\ -1 & 3 \end{pmatrix} and B=(1042)B = \begin{pmatrix} 1 & 0 \\ 4 & -2 \end{pmatrix}.

Both matrices are 2×22\times 2, so ABAB is 2×22\times 2. Compute all four entries as dot products:

(AB)11=(row 1 of A)(col 1 of B)=(2)(1)+(1)(4)=2+4=6(AB)_{11} = (\text{row }1\text{ of }A)\cdot(\text{col }1\text{ of }B) = (2)(1) + (1)(4) = 2 + 4 = 6

(AB)12=(row 1 of A)(col 2 of B)=(2)(0)+(1)(2)=02=2(AB)_{12} = (\text{row }1\text{ of }A)\cdot(\text{col }2\text{ of }B) = (2)(0) + (1)(-2) = 0 - 2 = -2

(AB)21=(row 2 of A)(col 1 of B)=(1)(1)+(3)(4)=1+12=11(AB)_{21} = (\text{row }2\text{ of }A)\cdot(\text{col }1\text{ of }B) = (-1)(1) + (3)(4) = -1 + 12 = 11

(AB)22=(row 2 of A)(col 2 of B)=(1)(0)+(3)(2)=06=6(AB)_{22} = (\text{row }2\text{ of }A)\cdot(\text{col }2\text{ of }B) = (-1)(0) + (3)(-2) = 0 - 6 = -6

AB=(62116)AB = \begin{pmatrix} 6 & -2 \\ 11 & -6 \end{pmatrix}

To verify: check (AB)21(AB)_{21} again. Row 2 of AA is (1,3)(-1, 3); column 1 of BB is (1,4)T(1, 4)^T. Dot product: 1+12=11-1+12=11. Correct.

Worked example 2 — the columns-of-A reading

Express AxA\vec{x} as a linear combination of the columns of AA, for A=(3125)A = \begin{pmatrix} 3 & -1 \\ 2 & 5 \end{pmatrix} and x=(42)\vec{x} = \begin{pmatrix} 4 \\ -2 \end{pmatrix}.

Column reading: The columns of AA are c1=(32)\vec{c}_1 = \begin{pmatrix}3\\2\end{pmatrix} and c2=(15)\vec{c}_2 = \begin{pmatrix}-1\\5\end{pmatrix}.

Ax=x1c1+x2c2=4(32)+(2)(15)A\vec{x} = x_1 \vec{c}_1 + x_2 \vec{c}_2 = 4\begin{pmatrix}3\\2\end{pmatrix} + (-2)\begin{pmatrix}-1\\5\end{pmatrix}

=(128)+(210)=(142)= \begin{pmatrix}12\\8\end{pmatrix} + \begin{pmatrix}2\\-10\end{pmatrix} = \begin{pmatrix}14\\-2\end{pmatrix}

Verify with the row rule: Row 1 of AA times x\vec{x}: (3)(4)+(1)(2)=12+2=14(3)(4) + (-1)(-2) = 12 + 2 = 14. Row 2: (2)(4)+(5)(2)=810=2(2)(4) + (5)(-2) = 8 - 10 = -2. Same answer, different view. Both views are always correct — use whichever serves the problem better.

Worked example 3 — AB and BA differ: the non-commutativity specimen

Let A=(0100)A = \begin{pmatrix} 0 & 1 \\ 0 & 0 \end{pmatrix} and B=(0010)B = \begin{pmatrix} 0 & 0 \\ 1 & 0 \end{pmatrix}. Compute both ABAB and BABA and compare.

Compute ABAB: (AB)11=(0)(0)+(1)(1)=1,(AB)12=(0)(0)+(1)(0)=0(AB)_{11} = (0)(0) + (1)(1) = 1, \quad (AB)_{12} = (0)(0) + (1)(0) = 0 (AB)21=(0)(0)+(0)(1)=0,(AB)22=(0)(0)+(0)(0)=0(AB)_{21} = (0)(0) + (0)(1) = 0, \quad (AB)_{22} = (0)(0) + (0)(0) = 0 AB=(1000)AB = \begin{pmatrix} 1 & 0 \\ 0 & 0 \end{pmatrix}

Compute BABA: (BA)11=(0)(0)+(0)(0)=0,(BA)12=(0)(1)+(0)(0)=0(BA)_{11} = (0)(0) + (0)(0) = 0, \quad (BA)_{12} = (0)(1) + (0)(0) = 0 (BA)21=(1)(0)+(0)(0)=0,(BA)22=(1)(1)+(0)(0)=1(BA)_{21} = (1)(0) + (0)(0) = 0, \quad (BA)_{22} = (1)(1) + (0)(0) = 1 BA=(0001)BA = \begin{pmatrix} 0 & 0 \\ 0 & 1 \end{pmatrix}

ABBAAB \ne BA. In fact, ABAB has its 11 in the top-left, BABA has its 11 in the bottom-right. These matrices are as different as possible for 2×22\times 2 diagonal matrices. This is your go-to example when you need to prove noncommutativity of matrix multiplication to a skeptic.

☠ KNOWN HAZARDS

  • Trying to commute. AB=BAAB = BA is almost never true. Always re-derive the product in the order specified. Never flip the order to make computation easier.

  • Ignoring dimension compatibility. Trying to multiply a 2×32\times 3 matrix by a 2×32\times 3 matrix is undefined — the inner dimensions 33 and 22 don't match. Check the handshake before starting any computation.

  • Multiplying entrywise. Matrix multiplication is NOT aijbija_{ij} \cdot b_{ij} (that's the Hadamard product, a different and much less useful operation that you will basically never encounter in serious work). It's row-times-column dot products. Period. Full stop. No exceptions. Do not make me say this again.

  • Thinking AB=0AB = 0 implies A=0A = 0 or B=0B = 0. This is false for matrices — demonstrably, provably, infuriatingly false. You can have two nonzero matrices whose product is zero. The zero-product property of real numbers does not survive the journey into matrix-land. Another thing you assumed was universal that isn't. Welcome to abstraction; it keeps pulling the rug out.

TL;DR

  • Matrix multiplication is forced by function composition: (AB)v=A(Bv)(AB)\vec{v} = A(B\vec{v}) for all v\vec{v}.

  • Rule: (AB)ij=(row i of A)(col j of B)(AB)_{ij} = (\text{row }i \text{ of }A) \cdot (\text{col }j\text{ of }B). Every entry is a dot product.

  • Dimension compatibility: (m×n)(n×p)=(m×p)(m\times n)(n\times p) = (m\times p). Inner dimensions must match.

  • ABBAAB \ne BA in general — noncommutativity is inherited from function composition.

  • Ax=x1c1+x2c2+A\vec{x} = x_1\vec{c}_1 + x_2\vec{c}_2 + \cdots — the matrix-vector product is a linear combination of AA's columns.

unlocks