---
title: Cofactors and Cramer's rule
heading: Determinants by formula, and what they're good for
eyebrow: A7 · Lesson 31 · Cofactors and Cramer's rule
xp: 40
---

::: hook
Numerical software never uses Cramer's rule; elimination is far faster. Yet engineers reach for it constantly, because it gives answers as **formulas**. Want to know how a sensor bridge's output depends on each resistor, symbolically, so you can see which one matters most? Cramer's rule writes the answer as a ratio of two determinants you can expand by hand. This lesson gives the determinant's rules, the cofactor formula, and Cramer's rule.
:::

::: play
- The columns $\mathbf{a}_1 = (2, 0.5)$ and $\mathbf{a}_2 = (0.5, 1.5)$ are fixed. Move the target $\mathbf{b}$.
- Cramer's rule says $x_1$ is the area of the parallelogram from $\mathbf{b}$ and $\mathbf{a}_2$, divided by the area from $\mathbf{a}_1$ and $\mathbf{a}_2$. The dashed arrows show $x_1\mathbf{a}_1 + x_2\mathbf{a}_2$ landing on $\mathbf{b}$.
- Put $\mathbf{b}$ along $\mathbf{a}_2$. What is $x_1$, and why does Cramer's rule agree?
:::
:::

```sim plot
view: complex
range: 2.5
slider: bx = -3..3 step 0.1 = 2
slider: by = -3..3 step 0.1 = 2
arrow: 2 + 0.5*j | label: a₁
arrow: 0.5 + 1.5*j | color: ch2 | label: a₂
arrow: bx + j*by | color: ch3 | label: b
arrow: ((bx*1.5 - 0.5*by)/2.75)*(2 + 0.5*j) | dashed | label: x₁a₁
arrow: ((2*by - 0.5*bx)/2.75)*(0.5 + 1.5*j) | from: ((bx*1.5 - 0.5*by)/2.75)*(2 + 0.5*j) | dashed | color: ch2 | label: x₂a₂
readout: det A = 2*1.5 - 0.5*0.5 | digits: 4
readout: x1 = (bx*1.5 - 0.5*by)/2.75 | digits: 4
readout: x2 = (2*by - 0.5*bx)/2.75 | digits: 4
```

::: predict
## You swap two rows of a matrix. What happens to its determinant?

- [ ] Nothing
- [x] It changes sign
- [ ] It becomes zero
- [ ] It doubles

::: reveal
It changes sign. Swapping rows reverses the orientation of the shape the rows (or columns) span, like looking at it in a mirror: the area or volume is the same, but its sign flips. That's why a matrix with two equal rows has determinant zero: swapping them changes nothing, yet must flip the sign.
:::
:::

::: nameit
## The rules

The determinant is the signed volume factor of the transformation. Three rules define it:

1. $\det I = 1$.
2. Swapping two rows changes the sign.
3. It's linear in each row separately (scale a row by $k$ and the determinant scales by $k$).

Everything else follows:

- Two equal rows, or a zero row: $\det = 0$.
- Subtracting a multiple of one row from another leaves $\det$ unchanged, so elimination preserves it.
- $\det A = \pm(\text{product of the pivots})$, the sign set by the number of row swaps.
- $\det(AB) = \det A\,\det B$, $\det A^T = \det A$, $\det A^{-1} = 1/\det A$.
- For an $n \times n$ matrix, $\det(kA) = k^n\det A$.

## Cofactor expansion

Along row $i$: $\det A = \sum_j a_{ij}C_{ij}$, where the **cofactor** $C_{ij} = (-1)^{i+j}\det M_{ij}$ and $M_{ij}$ deletes row $i$ and column $j$. The signs follow a checkerboard, $+ - +$. Expand along a row or column with many zeros.

## Cramer's rule and the inverse

For $A\mathbf{x} = \mathbf{b}$ with $\det A \ne 0$:

$$x_j = \frac{\det B_j}{\det A}$$

where $B_j$ is $A$ with column $j$ replaced by $\mathbf{b}$. Likewise $A^{-1} = \frac{1}{\det A}C^T$, which for 2 × 2 is the swap-and-negate formula from lesson 11.

Cramer's rule costs roughly $n!$ operations if the determinants are expanded by cofactors, hopeless beyond a few unknowns. Use it for formulas, not for numbers.
:::

::: example
## Find $\det\begin{bmatrix}2 & 1 & 3\\0 & 4 & 1\\1 & 0 & 2\end{bmatrix}$ by cofactors.

::: solution
1. **Expand along row 1.**
   $$2\begin{vmatrix}4 & 1\\0 & 2\end{vmatrix} - 1\begin{vmatrix}0 & 1\\1 & 2\end{vmatrix} + 3\begin{vmatrix}0 & 4\\1 & 0\end{vmatrix}$$
2. **Evaluate the 2 × 2 determinants and add.**
   $$\begin{aligned} &2(8) - 1(-1) + 3(-4) \\ &= 5 \end{aligned}$$

**Answer:** the determinant is 5.
:::
:::

::: example
## Current $I$ is injected into node 1 of a two-node network with conductances $g_1$ (node 1 to ground), $g_2$ (between the nodes) and $g_3$ (node 2 to ground). Find $v_2$ as a formula.

::: solution
1. **Matrix and source vector.**
   $$G = \begin{bmatrix}g_1 + g_2 & -g_2\\-g_2 & g_2 + g_3\end{bmatrix}$$
   and $\mathbf{i} = (I, 0)$.
2. **Cramer's rule.** Replace column 2 with $\mathbf{i}$:
   $$\begin{aligned} v_2 &= \frac{\begin{vmatrix}g_1 + g_2 & I\\-g_2 & 0\end{vmatrix}}{\det G} \\ &= \frac{g_2I}{(g_1 + g_2)(g_2 + g_3) - g_2^2} \\ &= \frac{g_2I}{g_1g_2 + g_1g_3 + g_2g_3} \end{aligned}$$
3. **What the formula shows.** It shows at once that $v_2 \to 0$ as $g_2 \to 0$ (the nodes come apart), something a number alone wouldn't.

**Answer:** $v_2 = \frac{g_2I}{g_1g_2 + g_1g_3 + g_2g_3}$.
:::
:::

::: example
## Find the determinant of the $n \times n$ matrix with 2 on the diagonal and $-1$ beside it, for $n$ up to 5.

::: solution
1. **Expand along the first row.** The 2 multiplies the same matrix one size smaller, $D_{n-1}$.
2. **The $-1$'s cofactor.** Expanded down its first column, it leaves $D_{n-2}$. So
   $$D_n = 2D_{n-1} - D_{n-2}$$
3. **Run the recurrence.** Starting from $D_1 = 2$ and $D_2 = 3$:
   $$D_3 = 4, \quad D_4 = 5, \quad D_5 = 6$$
   In general $D_n = n + 1$.
4. **What it means.** This matrix is the conductance matrix of a ladder of equal resistors, so it's never singular.

**Answer:** the determinants are 2, 3, 4, 5 and 6 for $n$ = 1 to 5, and in general $D_n = n + 1$.
:::
:::

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

## Expand $\det\begin{bmatrix}\given{a} & 1 & 0\\0 & \given{b} & 1\\1 & 0 & \given{c}\end{bmatrix}$ by cofactors.

::: right
Right. Along row 1: $a(bc - 0) - 1(0 - 1) + 0 = abc + 1$ = {answer}.
:::

::: misses
- [a*b*c] Include the second cofactor: $-1 \times (0 \cdot c - 1 \cdot 1) = +1$.
- [a*b*c - 1] Check the checkerboard sign and the minor: $-1 \times (-1) = +1$.
- [any] Not yet. Expand along the first row.
:::
:::

::: check
given: d = 5 | -3 | 12
answer: -d

## $\det A$ = {d}. Two rows of $A$ are swapped. What is the new determinant?

::: right
Right. A swap flips the sign: {answer}.
:::

::: misses
- [d] Swapping rows reverses orientation.
- [any] Not yet. One swap, one sign change.
:::
:::

::: check
given: n = 2 | 3 | 4; k = 2 | 3; d = 5 | -2
answer: k^n*d

## $A$ is {n} × {n} with $\det A$ = {d}. What is $\det(\given{k}A)$?

::: right
Right. Each of the {n} rows is scaled by {k}: $k^n\det A$ = {answer}.
:::

::: misses
- [k*d] Every row is scaled, and the determinant is linear in each row separately.
- [any] Not yet. Scaling all {n} rows multiplies by $k^n$.
:::
:::

::: check
given: d1 = 2 | 4; d2 = 3 | -5
answer: 1/(d1*d2)
tolerance: 0.5%

## $\det A$ = {d1} and $\det B$ = {d2}. What is $\det\big((AB)^{-1}\big)$?

::: right
Right. $\det(AB) = \det A\det B$, and the inverse inverts it: {answer}.
:::

::: misses
- [d1*d2] That's $\det(AB)$. The inverse's determinant is its reciprocal.
- [1/d1 + 1/d2] Determinants multiply, not add.
- [any] Not yet. $\det(AB)$ first, then invert.
:::
:::

::: check
given: a = 2 | 3; b = 1 | -1; c = 1 | 4; d = 3 | 5; b1 = 4 | 7; b2 = 1 | 2
answer: (b1*d - b*b2)/(a*d - b*c)
tolerance: 0.5%

## Use Cramer's rule on $\begin{bmatrix}\given{a} & \given{b}\\\given{c} & \given{d}\end{bmatrix}\mathbf{x} = (\given{b1}, \given{b2})$. What is $x_1$?

::: right
Right. Replace column 1 by $\mathbf{b}$: $\frac{b_1d - b\,b_2}{ad - bc}$ = {answer}.
:::

::: misses
- [(a*b2 - c*b1)/(a*d - b*c)] That's $x_2$: column 2 replaced.
- [any] Not yet. $x_1 = \det B_1/\det A$.
:::
:::

::: check
given: g1 = 1 | 2; g2 = 0.5 | 3; g3 = 1 | 4; I = 1 | 2
answer: g2*I/(g1*g2 + g1*g3 + g2*g3)
unit: V
tolerance: 0.5%

## A two-node network has $g_1$ = {g1} mS (node 1 to ground), $g_2$ = {g2} mS (between the nodes) and $g_3$ = {g3} mS (node 2 to ground). {I} mA is injected into node 1. What is $v_2$?

::: right
Right. $\frac{g_2I}{g_1g_2 + g_1g_3 + g_2g_3}$ = {answer}.
:::

::: misses
- [I*(g2 + g3)/(g1*g2 + g1*g3 + g2*g3)] That's $v_1$: column 1 replaced.
- [any] Not yet. Cramer's rule with column 2 replaced by $(I, 0)$.
:::
:::

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

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

::: part
answer: a*b*c - a - c
tolerance: 0.5%

### What is $\det A$?

::: misses
- [a*b*c] The off-diagonal 1's subtract terms.
- [any] Expand along the first row: $a(bc - 1) - 1(c - 0)$.
:::
:::

::: part
answer: (b*c - 1)/(a*b*c - a - c)
tolerance: 0.5%

### By Cramer's rule, what is $x_1$ in $A\mathbf{x} = (1, 0, 0)$?

::: misses
- [any] Replace column 1 with $(1, 0, 0)$; its determinant is $bc - 1$.
:::
:::
:::

::: check
kind: derivation
vars: x = 1.5..4

## Solve $\begin{bmatrix}x & 1\\1 & x\end{bmatrix}\mathbf{u} = (1, 0)$ by Cramer's rule.

::: step
answer: x^2 - 1

### What is the determinant?
:::

::: step
answer: x/(x^2 - 1)

### What is $u_1$?
:::

::: right
Right. $u_1 = \frac{x}{x^2 - 1}$: the determinant in the denominator is why a nearly singular system gives huge answers.
:::
:::

::: explain
## Why is $\det(AB) = \det A \times \det B$?

::: model
The determinant is the factor by which a matrix scales signed areas or volumes. Applying $AB$ means applying $B$ first, which scales every area by $\det B$, then $A$, which scales the result by $\det A$. The combined effect scales areas by the product. Since $\det(AB)$ is defined as exactly that combined scale factor, it equals $\det A\det B$.
:::
:::

::: practice

::: check
given: a = 2 | 5; b = 3 | 4; c = 2 | 6
answer: a*b*c

## What is the determinant of an upper triangular matrix with diagonal {a}, {b}, {c}?

::: misses
- [a + b + c] Multiply the diagonal.
- [any] The product of the diagonal entries.
:::
:::

::: check
given: d = -9 | -6 | -2 | 3 | 4 | 7 | 12
answer: d

## $\det A$ = {d}. What is $\det A^T$?

::: misses
- [-d] Transposing doesn't change the determinant.
- [any] $\det A^T = \det A$.
:::
:::

::: check
given: g1 = 1 | 2; g2 = 2 | 4; g3 = 1 | 3; I = 2 | 5
answer: I*(g2 + g3)/(g1*g2 + g1*g3 + g2*g3)
unit: V
tolerance: 0.5%

## For the same two-node network ($g_1$ = {g1}, $g_2$ = {g2}, $g_3$ = {g3} mS, {I} mA into node 1), what is $v_1$?

::: misses
- [any] Replace column 1 of $G$ with $(I, 0)$.
:::
:::
:::
