---
title: Matrix multiplication
heading: Doing one transformation after another
eyebrow: A7 · Lesson 5 · Matrix multiplication
xp: 40
---

::: hook
A filter feeds an amplifier, which feeds a cable, which feeds a load. Each stage can be described by a 2 × 2 matrix relating the voltage and current at its input to those at its output. To get the whole chain, you multiply the matrices. The rule for multiplying them looks arbitrary at first, rows times columns, but it's forced by one requirement: the product must do what the two matrices do, one after the other.
:::

::: play
- Matrix $A$ rotates the plane by $\theta$; matrix $B$ stretches it sideways by $s$.
- The solid curve is the unit circle after $AB$ (stretch first, then rotate). The dashed curve is after $BA$ (rotate first, then stretch).
- Are they the same shape? Find settings where they match, and settings where they're far apart.
:::
:::

```sim plot
view: complex
range: 3
slider: t = 0..180 step 5 = 40 °
slider: s = 0.5..2.5 step 0.1 = 2
curve: (s*cos(t°)*cos(u) - sin(t°)*sin(u)) + j*(s*sin(t°)*cos(u) + cos(t°)*sin(u)) | over: u = 0..2*pi | label: AB
curve: (s*cos(t°)*cos(u) - s*sin(t°)*sin(u)) + j*(sin(t°)*cos(u) + cos(t°)*sin(u)) | over: u = 0..2*pi | dashed | color: ch2 | label: BA
arrow: s*cos(t°) + j*s*sin(t°) | label: AB e₁
arrow: s*cos(t°) + j*sin(t°) | color: ch2 | label: BA e₁
readout: gap between AB e1 and BA e1 = abs(s - 1)*abs(sin(t°)) | digits: 3
```

::: predict
## You stretch the plane sideways and then rotate it, or rotate first and then stretch. Do you get the same result?

- [ ] Always
- [x] Only in special cases, such as no rotation or no stretch
- [ ] Never
- [ ] Only if the stretch is negative

::: reveal
Only in special cases. Stretching then rotating tilts the stretched direction; rotating then stretching always stretches along the $x$ axis. They agree when $s = 1$ (no stretch) or $\theta$ is 0° or 180°. In general $AB \ne BA$: the order of operations matters, and matrix multiplication keeps track of it.
:::
:::

::: nameit
## The rule

If $A$ is $m \times n$ and $B$ is $n \times p$, then $AB$ is $m \times p$, and its entry in row $i$, column $j$ is row $i$ of $A$ dotted with column $j$ of $B$:

$$(AB)_{ij} = \sum_k a_{ik}b_{kj}$$

The inner sizes must match ($n$), and the outer sizes give the shape of the result.

## Why it's this rule

$(AB)\mathbf{x}$ must equal $A(B\mathbf{x})$: first apply $B$, then $A$. Column $j$ of $AB$ is $A$ times column $j$ of $B$, which is where the $j$th basis vector ends up after both steps. Writing that out gives the row-times-column rule.

## Properties

- **Not commutative:** usually $AB \ne BA$. Read $AB$ right to left: $B$ acts first.
- **Associative:** $(AB)C = A(BC)$, so a chain of stages can be grouped any way.
- **Identity:** $I$ has ones on the diagonal; $AI = IA = A$.
- **Distributive:** $A(B + C) = AB + AC$.

## Cascading two-ports

A two-port network (a stage with an input and an output) has a **transmission matrix** relating input voltage and current to output voltage and current:

$$\begin{bmatrix}V_1\\I_1\end{bmatrix} = \begin{bmatrix}\mathcal{A} & \mathcal{B}\\ \mathcal{C} & \mathcal{D}\end{bmatrix}\begin{bmatrix}V_2\\I_2\end{bmatrix}$$

A series impedance $Z$ is $\begin{bmatrix}1 & Z\\0 & 1\end{bmatrix}$; a shunt admittance $Y$ is $\begin{bmatrix}1 & 0\\Y & 1\end{bmatrix}$. A chain of stages has the product of their matrices, in order from input to output. With the output open ($I_2 = 0$), the voltage gain is $V_2/V_1 = 1/\mathcal{A}$. S6 uses these for RF networks.
:::

::: example
## Compute $AB$ and $BA$ for $A = \begin{bmatrix}1 & 2\\3 & 4\end{bmatrix}$, $B = \begin{bmatrix}5 & 6\\7 & 8\end{bmatrix}$.

::: solution
1. **Row 1 of $AB$.** Row 1 of $A$ times the columns of $B$:
   $$1 \cdot 5 + 2 \cdot 7 = 19$$
   $$1 \cdot 6 + 2 \cdot 8 = 22$$
2. **Row 2 of $AB$.**
   $$3 \cdot 5 + 4 \cdot 7 = 43$$
   $$3 \cdot 6 + 4 \cdot 8 = 50$$
3. **The two products.**
   $$AB = \begin{bmatrix}19 & 22\\43 & 50\end{bmatrix}$$
   $$BA = \begin{bmatrix}23 & 34\\31 & 46\end{bmatrix}$$
   Different, as expected.

**Answer:** $AB = \begin{bmatrix}19 & 22\\43 & 50\end{bmatrix}$ and $BA = \begin{bmatrix}23 & 34\\31 & 46\end{bmatrix}$, which are different.
:::
:::

::: example
## A 1 kΩ series resistor is followed by a 1 kΩ shunt resistor. Find the chain's transmission matrix and its open-circuit voltage gain.

::: solution
1. **Multiply the series and shunt matrices.**
   $$\begin{aligned} &\begin{bmatrix}1 & 1000\\0 & 1\end{bmatrix}\begin{bmatrix}1 & 0\\0.001 & 1\end{bmatrix} \\ &= \begin{bmatrix}1 + 1 & 1000\\0.001 & 1\end{bmatrix} \\ &= \begin{bmatrix}2 & 1000\\0.001 & 1\end{bmatrix} \end{aligned}$$
2. **Gain.** The open-circuit gain is one over the top-left entry:
   $$1/\mathcal{A} = \frac{1}{2}$$
   an ordinary voltage divider, found by multiplying matrices.

**Answer:** the chain's transmission matrix is $\begin{bmatrix}2 & 1000\\0.001 & 1\end{bmatrix}$, and its open-circuit gain is $\frac12$.
:::
:::

::: example
## Multiply the 30° rotation matrix by itself and show the product is the 60° rotation.

::: solution
1. **The 30° rotation.**
   $$R_{30} = \begin{bmatrix}0.866 & -0.5\\0.5 & 0.866\end{bmatrix}$$
2. **Diagonal entries of $R_{30}R_{30}$.**
   $$0.866^2 - 0.25 = 0.5$$
3. **Off-diagonal entries.** The bottom-left is
   $$2(0.5)(0.866) = 0.866$$
   and the top-right is $-2(0.866)(0.5) = -0.866$.
4. **The product.**
   $$R_{30}R_{30} = \begin{bmatrix}0.5 & -0.866\\0.866 & 0.5\end{bmatrix}$$
   That's $\cos 60°$ and $\sin 60°$ in the right places.
5. **The double-angle formulas.** The entries are
   $$\cos 2\theta = \cos^2\theta - \sin^2\theta$$
   $$\sin 2\theta = 2\sin\theta\cos\theta$$
   which matrix multiplication proves without any trigonometry.

**Answer:** $R_{30}R_{30} = \begin{bmatrix}0.5 & -0.866\\0.866 & 0.5\end{bmatrix}$, the 60° rotation.
:::
:::

::: check
given: a = 1 | 2; b = 3 | -1; m = 4 | 2; k = 5 | -3
answer: a*m + b*k

## For $A = \begin{bmatrix}\given{a} & \given{b}\\1 & 2\end{bmatrix}$ and $B = \begin{bmatrix}\given{m} & 0\\\given{k} & 1\end{bmatrix}$, what is the top-left entry of $AB$?

::: right
Right. Row 1 of $A$ dotted with column 1 of $B$: $\given{a} \times \given{m} + \given{b} \times \given{k}$ = {answer}.
:::

::: misses
- [a*m] Include the second product.
- [b*m] Pair the first entry of the row with the first entry of the column.
- [any] Not yet. Row 1 of $A$ times column 1 of $B$.
:::
:::

::: check
given: c = 2 | -2; d = 3 | 5; m = 1 | 4; k = 2 | -1
answer: c*m + d*k

## For $A = \begin{bmatrix}1 & 0\\\given{c} & \given{d}\end{bmatrix}$ and $B = \begin{bmatrix}\given{m} & 7\\\given{k} & 2\end{bmatrix}$, what is the bottom-left entry of $AB$?

::: right
Right. Row 2 of $A$ times column 1 of $B$: $\given{c} \times \given{m} + \given{d} \times \given{k}$ = {answer}.
:::

::: misses
- [c*7 + d*2] That's column 2 of $B$. The bottom-left entry uses column 1.
- [c*m] Use all of row 2 of $A$: add $\given{d} \times \given{k}$.
- [any] Not yet. Row 2 times column 1.
:::
:::

::: check
kind: choice

## $A$ is 3 × 2 and $B$ is 2 × 4. What is $AB$?

- [x] 3 × 4
- [ ] 2 × 2
- [ ] 4 × 3
- [ ] It isn't defined

::: right
Right. The inner sizes (2 and 2) match, and the outer sizes give 3 × 4.
:::

::: misses
- [2] That's the shared inner size twice. The result takes the outer sizes.
- [4] That would be $B^TA^T$'s shape.
- [any] Inner sizes must match; outer sizes give the shape.
:::
:::

::: check
given: R1 = 1 | 4.7; R2 = 2 | 10
answer: 1 + R1/R2
tolerance: 0.5%

## A series resistor $R_1$ = {R1} kΩ is followed by a shunt $R_2$ = {R2} kΩ. What is the top-left entry $\mathcal{A}$ of the chain's transmission matrix?

::: right
Right. $\begin{bmatrix}1 & R_1\\0 & 1\end{bmatrix}\begin{bmatrix}1 & 0\\1/R_2 & 1\end{bmatrix}$ has top-left entry $1 + R_1/R_2$ = {answer}, so the gain is $\frac{R_2}{R_1 + R_2}$.
:::

::: misses
- [1 + R2/R1] The shunt contributes $1/R_2$, which meets $R_1$ in the product.
- [R2/(R1 + R2)] That's the gain, $1/\mathcal{A}$.
- [any] Not yet. Multiply the series matrix by the shunt matrix.
:::
:::

::: check
given: p = 2 | 3; q = -1 | 4
answer: p*q + 1

## For $A = \begin{bmatrix}1 & \given{p}\\0 & 1\end{bmatrix}$ and $B = \begin{bmatrix}1 & 0\\\given{q} & 1\end{bmatrix}$, what is the top-left entry of $AB$? (Compare $BA$, whose top-left entry is 1.)

::: right
Right. Row $(1, \given{p})$ times column $(1, \given{q})$ is $1 + pq$ = {answer}. Different from $BA$'s 1: the order matters.
:::

::: misses
- [1] That's $BA$'s top-left entry. In $AB$, row 1 of $A$ includes {p}.
- [any] Not yet. Row 1 of $A$ times column 1 of $B$.
:::
:::

::: check
given: n = 3 | 5 | 10
answer: n

## What is the top-right entry of $\begin{bmatrix}1 & 1\\0 & 1\end{bmatrix}^{\given{n}}$?

::: right
Right. Each multiplication adds 1 to the top-right entry, so after {n} factors it's {answer}.
:::

::: misses
- [1] Square it once and look: the top-right entry becomes 2.
- [2^n] It grows by adding 1 each time, not doubling.
- [any] Not yet. Compute the square and the cube and spot the pattern.
:::
:::

::: check
kind: parts
given: a = 2 | 3; b = 1 | 5

## Take $A = \begin{bmatrix}\given{a} & 1\\0 & 1\end{bmatrix}$ and $B = \begin{bmatrix}1 & 0\\\given{b} & 1\end{bmatrix}$.

::: part
answer: a + b
tolerance: 0.5%

### What is the top-left entry of $AB$?

::: misses
- [a] That's $BA$'s top-left entry.
- [any] Row 1 of $A$ times column 1 of $B$.
:::
:::

::: part
answer: b + 1
tolerance: 0.5%

### What is the bottom-right entry of $BA$?

::: misses
- [any] Row 2 of $B$ times column 2 of $A$: $b \cdot 1 + 1 \cdot 1$.
:::
:::
:::

::: check
kind: derivation
vars: x = 0.5..3, y = 0.5..3

## A series impedance $x$ is followed by a shunt admittance $y$.

::: step
answer: 1 + x*y

### What is the top-left entry $\mathcal{A}$ of $\begin{bmatrix}1 & x\\0 & 1\end{bmatrix}\begin{bmatrix}1 & 0\\y & 1\end{bmatrix}$?
:::

::: step
answer: 1/(1 + x*y)

### What is the open-circuit voltage gain?
:::

::: right
Right. $\frac{1}{1 + xy}$ is the voltage divider $\frac{1/y}{x + 1/y}$, found by multiplying matrices.
:::
:::

::: explain
## Why is $AB$ usually different from $BA$?

::: model
$AB$ means "apply $B$, then apply $A$", and $BA$ means the reverse. Transformations generally don't commute: stretching sideways and then rotating tilts the stretch, while rotating first and then stretching stretches along the original axis. The two results are different shapes, so their matrices are different. Multiplication rules for numbers don't carry over, because a matrix is an action, and the order of actions matters.
:::
:::

::: practice

::: check
given: a = 2 | -1; b = 1 | 3; f = 4 | 2; h = -2 | 5
answer: a*f + b*h

## For $A = \begin{bmatrix}\given{a} & \given{b}\\0 & 1\end{bmatrix}$ and $B = \begin{bmatrix}1 & \given{f}\\0 & \given{h}\end{bmatrix}$, what is the top-right entry of $AB$?

::: misses
- [any] Row 1 of $A$ times column 2 of $B$.
:::
:::

::: check
given: Z = 50 | 100; Y = 0.01 | 0.02
answer: 1 + Z*Y
tolerance: 0.5%

## A series impedance of {Z} Ω is followed by a shunt admittance of {Y} S. What is $\mathcal{A}$ for the chain?

::: misses
- [any] $\begin{bmatrix}1 & Z\\0 & 1\end{bmatrix}\begin{bmatrix}1 & 0\\Y & 1\end{bmatrix}$.
:::
:::

::: check
given: k = 2 | 3; n = 3 | 4
answer: k^n

## What is the top-left entry of $\begin{bmatrix}\given{k} & 0\\0 & 1\end{bmatrix}^{\given{n}}$?

::: misses
- [k*n] Multiplying diagonal matrices multiplies their entries.
- [any] Powers of a diagonal matrix raise each diagonal entry.
:::
:::
:::
