# linear systems

> several equations at once: elimination, and when there's one answer, none, or infinitely many.

source: https://learning.anthnydas.com/math/linear-systems/

## tl;dr

a linear system is several equations about the same unknowns. each one is a line, or a plane, and an answer is a point on all of them. elimination subtracts multiples of one equation from the others until each unknown reads straight off, and no step changes which points are answers.

- **row operations:** swap · scale by c ≠ 0 · add a multiple (each one can be undone, so the answers stay the same)
- **one answer:** det A ≠ 0 (a pivot in every column. two lines crossing once)
- **none:** 0 = c, c ≠ 0 (a row of zeros with a nonzero right-hand side. parallel lines)
- **infinitely many:** 0 = 0 (a column with no pivot is a free unknown. the same line twice)
- **pivot:** the number you divide by (take the biggest in its column. a tiny one blows rounding error up)
- **solve, don't invert:** x from Ax = b, not A⁻¹b (A⁻¹ is n solves in one, plus a multiply, for the same x)
- **reach for it:** unknowns tied together: two prices from two receipts, three currents from three loops, fitting a line through points, where the best fit is the answer to a small system, the matrices page, where a system is Ax = b and the determinant says if there's one answer, eigenvectors, where (A − λI)x = 0 is a system that has to have infinitely many answers

## the idea

one equation in two unknowns, $x + 2y = 5$, has endless answers: $(5, 0)$, $(1, 2)$, $(-1, 3)$, every point of a line. a second equation, $3x - y = 1$, is another line. a pair $(x, y)$ that fits both is a point on both lines: where they cross. two equations, two unknowns, and in the usual case one answer.

finding it is elimination. the second equation, minus three copies of the first, has no $x$ left:

$$
\begin{gathered}
(3x - y) - 3(x + 2y) = 1 - 3 \cdot 5 \\
-7y = -14 \quad\Rightarrow\quad y = 2
\end{gathered}
$$

then the first equation gives $x = 5 - 2 \cdot 2 = 1$. the crossing is $(1, 2)$. the movie does the same thing on the augmented matrix, the equations with their words stripped off: coefficients on the left of the bar, right-hand sides on the right. watch the lines: each row operation turns one of them, and the crossing never moves.

> **interactive figure:** an animation on a plot from x = −2 to 5 and y = −1 to 4, with the augmented matrix beside it. the lines x + 2y = 5 in cyan and 3x − y = 1 in amber cross at (1, 2). row operation R2 − 3·R1 turns the amber line about the crossing until it is horizontal, y = 2, and the matrix's second row goes from 3 −1 | 1 to 0 −7 | −14; halfway through, the row reads 1.5x − 4y = −6.5 and still passes through (1, 2). dividing row 2 by −7 changes its numbers to 0 1 | 2 and leaves the line where it is. then R1 − 2·R2 turns the cyan line about the crossing until it is vertical, x = 1, and the matrix reads 1 0 | 1; 0 1 | 2. the original two lines stay behind, dashed, and the point (1, 2) never moves.

two equations, two lines: $x + 2y = 5$ in cyan and $3x - y = 1$ in amber. they cross at $(1, 2)$, the one pair of numbers that fits both. the augmented matrix beside them holds the same two equations, numbers only.

$R_2 - 3R_1$: take three copies of row 1 away from row 2. the $x$ cancels and row 2 reads $-7y = -14$, a horizontal line. the amber line turns about the crossing as it goes: each in-between multiple is a row operation too, and every one of those lines passes through $(1, 2)$.

divide row 2 by $-7$: $y = 2$. the line doesn't move at all. dividing both sides of an equation by the same number describes exactly the same points.

$R_1 - 2R_2$: two copies of row 2 away from row 1. the $y$ cancels and row 1 reads $x = 1$, a vertical line. the cyan line turns about the crossing to stand straight up.

the matrix reads $x = 1$ and $y = 2$: the identity on the left, the answer on the right. three row operations, three new pairs of lines, and the crossing stayed put. that is the whole reason elimination works.

the [matrices page](/math/matrices/) writes the same system as one equation, $A\mathbf{x} = \mathbf{b}$, with $A$ the $2 \times 2$ block of coefficients. the [vectors page](/math/vectors/) is where $\mathbf{x}$ and $\mathbf{b}$ come from.

## explore

two lines, $a_1 x + b_1 y = c_1$ and $a_2 x + b_2 y = c_2$, from six sliders. the readout gives both equations, the determinant $a_1 b_2 - a_2 b_1$, and the answer: the one crossing, or why there isn't one.

> **interactive figure:** a plot from x = −6 to 6 and y = −4 to 4 with two lines, a₁x + b₁y = c₁ in cyan and a₂x + b₂y = c₂ in amber, each from three sliders: a and b from −4 to 4, c from −8 to 8, whole numbers. buttons set three starting systems. it opens on x + 2y = 5 and 3x − y = 1: determinant −7, the one answer x = 1, y = 2, marked on the plot. the second preset is x + 2y = 5 and 2x + 4y = 4: determinant 0, no answer, parallel lines. the third is x + 2y = 5 and 2x + 4y = 10: determinant 0, infinitely many, the same line twice, drawn dashed over itself. the readout gives both equations, the determinant and the answer, as fractions when the crossing is off the grid.

- the determinant is $0$ exactly when the second line's direction is a multiple of the first's. then the lines are parallel, and never meet, or they are the same line, and meet everywhere
- a row like $0x + 0y = 4$ is no line at all: $0 = 4$ holds nowhere, so the system has no answer. $0 = 0$ holds everywhere, and the other line is the whole answer
- the crossing is a fraction more often than not. $2x + y = 3$ and $x - y = 1$ meet at $(4/3, 1/3)$

## try it

> **interactive figure:** a practice exercise with four kinds of question: say whether a system of two equations has one answer, none or infinitely many, pick the point that is on both of two lines, name the row operation that clears x from the second row, and say which row partial pivoting brings to the top. once there is a pick, a question about two lines draws them. it opens on x + 2y = 5 and 2x + 4y = 4, which has no answer, then x + y = 3 and x − y = 1, which meet at (2, 1).

## the rules

### a linear equation is a line, or a plane

an equation is linear when each unknown appears on its own, multiplied by a number: $ax + by = c$, with no $x^2$, no $xy$, no $\sin x$. in two unknowns its answers make a line, and in three, $ax + by + cz = d$, a plane. a linear system is several of these about the same unknowns, and a solution is a point that fits every one.

### one, none, or infinitely many

a linear system has exactly one answer, no answer, or infinitely many. never two, never seven.

why: suppose $\mathbf{p}$ and $\mathbf{q}$ are both answers to $A\mathbf{x} = \mathbf{b}$. then for any $t$,

$$
\begin{gathered}
A\big(\mathbf{p} + t(\mathbf{q} - \mathbf{p})\big) \\
= A\mathbf{p} + t(A\mathbf{q} - A\mathbf{p}) \\
= \mathbf{b} + t(\mathbf{b} - \mathbf{b}) = \mathbf{b}
\end{gathered}
$$

so every point on the line through $\mathbf{p}$ and $\mathbf{q}$ is an answer too. two answers force infinitely many. for two lines in a plane, that is the picture from the explore figure: they cross once, run parallel, or are the same line.

### row operations

three moves on the rows of the augmented matrix, each one a move on the equations:

- **swap** two rows: the same equations in a different order
- **scale** a row by a number $c \neq 0$: both sides of one equation times $c$
- **add a multiple** of one row to another: $R_i \leftarrow R_i + k R_j$

each can be undone by a move of the same kind: swap back, scale by $1/c$, add $-k R_j$. that is why the answers never change, and it is why $c = 0$ is not allowed: a row times $0$ is $0 = 0$, and nothing brings the equation back.

### elimination

forward elimination takes the matrix to row echelon form: each row's first nonzero entry, its pivot, sits to the right of the pivot above it, and the rows below a pivot have zeros under it. column by column: pick the pivot row, swap it up, and add multiples of it to the rows below to clear the column.

back substitution then reads the answer off, bottom row first. the last pivot row has one unknown left, the one above it has two with one already known, and so on up.

the echelon form also says how many answers there are:

- a row $0 \;\, 0 \;\cdots\; 0 \;|\; c$ with $c \neq 0$ reads $0 = c$: **no answer**
- a column with no pivot is a **free unknown**: it can be anything, the others follow from it, and there are **infinitely many** answers (unless a row says there are none)
- a pivot in every column, and no $0 = c$ row: **one answer**

### as Ax = b

the system is $A\mathbf{x} = \mathbf{b}$, with $A$ the coefficients, $\mathbf{x}$ the unknowns and $\mathbf{b}$ the right-hand sides. for the movie's system:

$$
\begin{pmatrix} 1 & 2 \\ 3 & -1 \end{pmatrix}
\begin{pmatrix} x \\ y \end{pmatrix}
=
\begin{pmatrix} 5 \\ 1 \end{pmatrix}
$$

there is exactly one answer for every $\mathbf{b}$ when $\det A \neq 0$, which is when $A$ has an inverse: then $\mathbf{x} = A^{-1}\mathbf{b}$. the [matrices page](/math/matrices/#the-determinant) is where the determinant comes from. for $2 \times 2$ it is $a_1 b_2 - a_2 b_1$, and the answer is cramer's rule:

$$
\begin{gathered}
x = \frac{c_1 b_2 - c_2 b_1}{a_1 b_2 - a_2 b_1} \\[0.6em]
y = \frac{a_1 c_2 - a_2 c_1}{a_1 b_2 - a_2 b_1}
\end{gathered}
$$

with $\det A = 1 \cdot (-1) - 3 \cdot 2 = -7$, $x = (5 \cdot (-1) - 1 \cdot 2) / (-7) = 1$ and $y = (1 \cdot 1 - 3 \cdot 5) / (-7) = 2$.

$\mathbf{x} = A^{-1}\mathbf{b}$ is the right formula and the wrong program. to build $A^{-1}$ you solve $n$ systems at once, one for each column of the identity, and then there is still the multiply by $\mathbf{b}$. counting multiplications and divisions, one solve of a $10 \times 10$ system by elimination and back substitution takes $430$, against $1430$ to invert that way and multiply. at $100 \times 100$ it is $343{,}300$ against $1{,}343{,}300$, close to $n^3 / 3$ for the solve and almost four times that for the inverse. every extra operation is another rounding, so the inverse is slower and less accurate for the same $\mathbf{x}$. numpy's `solve` hands the system to lapack's `gesv`,[^numpy-solve] which factors $A$ by elimination with partial pivoting and solves from the factors.[^dgesv]

### partial pivoting

when a column has several candidates for the pivot, take the one with the biggest absolute value and swap its row up. clearing the column below multiplies the pivot row by $m = a_{ic} / \text{pivot}$ for each row $i$, and a tiny pivot makes $m$ huge. the row below becomes $R_i - m R_{\text{pivot}}$, and when $m R_{\text{pivot}}$ is $10^{20}$ times the size of $R_i$, every digit $R_i$ had is lost in the subtraction. with the biggest entry as the pivot, $|m| \le 1$, and the rows keep their digits. the worked example below has the numbers.

### three unknowns: planes

with three unknowns each equation is a plane, and the same three outcomes appear. three planes meet at one point, like two walls and a floor; or along a line, like the pages of an open book; or nowhere, when two are parallel, or when each pair meets but the three lines of meeting run side by side, like a triangular prism. elimination tells them apart by the echelon form, and the stepper below runs all three.

## worked examples

### two equations, by hand

$x + 2y = 5$ and $3x - y = 1$, as the movie did it:

$$
\begin{gathered}
\left[\begin{array}{cc|c} 1 & 2 & 5 \\ 3 & -1 & 1 \end{array}\right] \\[0.6em]
\xrightarrow{R_2 - 3R_1}\;
\left[\begin{array}{cc|c} 1 & 2 & 5 \\ 0 & -7 & -14 \end{array}\right] \\[0.6em]
\xrightarrow{R_2 / (-7)}\;
\left[\begin{array}{cc|c} 1 & 2 & 5 \\ 0 & 1 & 2 \end{array}\right]
\end{gathered}
$$

that is row echelon form, and row 2 says $y = 2$. back substitution: $x + 2 \cdot 2 = 5$, so $x = 1$. the movie went one step further, $R_1 - 2R_2$, to make the left block the identity: that is reduced row echelon form, and the answer is then the right-hand column.

### three equations, step by step

the stepper runs elimination with partial pivoting on three systems that share two rows. only the middle row differs, and that is enough to give one answer, none, or infinitely many.

> **interactive figure:** a stepper over a 3 × 4 augmented matrix with the pseudocode beside it, for one of three systems. all three have rows 4 2 −2 | 6 and −2 1 4 | 4, and differ in the middle row. with 2 3 1 | 9: the pivot 4 is already on top, R2 − 1/2·R1 and R3 + 1/2·R1 clear column 1, the pivot 2 needs no swap, R3 − R2 clears column 2, and the echelon form 4 2 −2 | 6; 0 2 2 | 6; 0 0 1 | 1 has pivots 4, 2, 1. back substitution gives z = 1, y = 2, x = 1. with 2 1 −1 | 4: R2 − 1/2·R1 leaves 0 0 0 | 1, a swap brings 0 2 3 | 7 up, and the last row reads 0 = 1, so there is no answer. with 2 1 −1 | 3: the same steps leave 0 0 0 | 0, z is free, y = (7 − 3t)/2 and x = (5t − 1)/4, a line of answers; t = 1 gives (1, 2, 1). the row being changed is white, the row it reads from cyan, and the pivot ringed in amber.

for the first system, back substitution reads: row 3, $z = 1$. row 2, $2y + 2z = 6$, so $y = (6 - 2) / 2 = 2$. row 1, $4x + 2y - 2z = 6$, so $x = (6 - 4 + 2) / 4 = 1$. the answer is $(1, 2, 1)$, and putting it back into the three original equations checks it: $4 + 4 - 2 = 6$, $2 + 6 + 1 = 9$, $-2 + 2 + 4 = 4$.

### a tiny pivot

$10^{-20} x + y = 1$ and $x + y = 2$. the answer is $x = y = 1$ to twenty digits: from the second equation $x = 2 - y$, and the first then says $y = 1$ up to a term of size $10^{-20}$. in doubles, with the rows as they come, the pivot is $10^{-20}$ and the multiplier is $m = 1 / 10^{-20} = 10^{20}$, so $R_2 - 10^{20} R_1$ reads

$$
\begin{gathered}
(1 - 10^{20})\,y = 2 - 10^{20} \\
1 - 10^{20} \to -10^{20} \\
2 - 10^{20} \to -10^{20}
\end{gathered}
$$

a double carries 53 bits, about 16 decimal digits,[^py-float] so the $1$ and the $2$ vanish next to $10^{20}$ and both sides round to the same number. $y = 1$ exactly, and then $x = (1 - y) / 10^{-20} = 0$. wrong by a whole unit. with the swap, the pivot is $1$, the multiplier is $10^{-20}$, and $R_2 - 10^{-20} R_1$ reads $(1 - 10^{-20})\,y = 1 - 2 \cdot 10^{-20}$, where both sides round to $1$: $y = 1$, $x = 2 - 1 = 1$. the same rounding, to the same digit, and this time it lands on the answer.

## the key idea up close

### why a row operation never changes the answers

call the old system $S$ and the new one $S'$, after $R_2 \leftarrow R_2 + k R_1$. take any point $\mathbf{p}$ that solves $S$. it makes row 1 true, $a_1 \cdot \mathbf{p} = c_1$, and row 2 true, $a_2 \cdot \mathbf{p} = c_2$. the new row 2 is $(a_2 + k a_1) \cdot \mathbf{x} = c_2 + k c_1$, and at $\mathbf{p}$ its left side is

$$
\begin{gathered}
(a_2 + k a_1) \cdot \mathbf{p} = a_2 \cdot \mathbf{p} + k\,(a_1 \cdot \mathbf{p}) \\
= c_2 + k c_1
\end{gathered}
$$

the new right side. so $\mathbf{p}$ solves $S'$ too: every answer of $S$ is an answer of $S'$.

now the other direction. $S$ comes from $S'$ by the same kind of move, $R_2 \leftarrow R_2 - k R_1$, so the argument runs again and every answer of $S'$ is an answer of $S$. the two systems have exactly the same answers. a swap and a scale by $c \neq 0$ undo the same way, so the same holds for them.

the movie shows it: as $k$ runs from $0$ to $-3$, every in-between system $R_2 + k R_1$ is a legitimate row operation away from the original, so every in-between amber line passes through $(1, 2)$. the line turns, the crossing stays.

a scale by $0$ breaks the second half. the new row $0 = 0$ is true everywhere, so every old answer is still an answer, but there is no move back, and the new system can have more answers than the old. scale row 2 of the movie's system by $0$ and what is left is $x + 2y = 5$ alone: a whole line of answers where there was one.

## in code

elimination with partial pivoting and back substitution, saying when there is one answer, none, or infinitely many. it runs on `fractions.Fraction` for the exact examples above, with `tol=0`, and on floats for the two rounding points. an entry smaller than `tol` times the biggest entry in the matrix counts as zero, so a pivot that rounding left at $10^{-16}$ is not divided by.

`python/math/elimination.py`

```python
"""gaussian elimination with partial pivoting and back substitution, saying when a system has one answer, none, or infinitely many. it works on fractions.Fraction for exact small examples and on floats for the two rounding points the page makes: a pivot of 1e-20 gives x = 0 instead of 1 unless the rows are swapped first, and a pivot that should be 0 can come out as 4.4e-16, which only a tolerance catches. run the file to print both."""

from __future__ import annotations

from fractions import Fraction
from typing import NamedTuple

Number = float | Fraction
Matrix = list[list[Number]]


class Solution(NamedTuple):
    """How many answers, and which: `kind` is "one", "none" or "many". `values` holds the one answer, or one
    answer among the many with every free unknown set to 0. `free` lists the free unknowns' columns."""

    kind: str
    values: list[Number] | None
    free: list[int]


def augment(a: list[list[Number]], b: list[Number]) -> Matrix:
    """Return [A | b]: each row of A with its right-hand side appended."""
    return [list(row) + [rhs] for row, rhs in zip(a, b, strict=True)]


def eliminate(m: Matrix, pivoting: bool = True, tol: float = 1e-12) -> tuple[Matrix, list[tuple[int, int]]]:
    """Return the row echelon form of the augmented matrix and its pivot positions, by row operations on a copy.

    For each column, the row at or below the next pivot row with the biggest entry comes up (partial pivoting),
    and multiples of it clear the column below. With `pivoting` off, rows are swapped only past a zero pivot. An
    entry within `tol` times the biggest entry of the original matrix counts as 0, so a pivot that rounding left
    at 1e-16 is not divided by. With Fractions, pass tol=0 for an exact zero test.
    """
    m = [list(row) for row in m]
    rows, cols = len(m), len(m[0]) - 1
    scale = max((abs(v) for row in m for v in row[:cols]), default=0) or 1
    zero = lambda v: abs(v) <= tol * scale  # noqa: E731
    pivots: list[tuple[int, int]] = []
    r = 0
    for c in range(cols):
        if r == rows:
            break
        if pivoting:
            p = max(range(r, rows), key=lambda i: abs(m[i][c]))
        else:
            p = next((i for i in range(r, rows) if not zero(m[i][c])), r)
        if zero(m[p][c]):
            continue
        m[r], m[p] = m[p], m[r]
        for i in range(r + 1, rows):
            k = m[i][c] / m[r][c]
            m[i] = [v - k * w for v, w in zip(m[i], m[r], strict=True)]
            m[i][c] = 0 * m[i][c]  # exactly 0, in the row's own number type
        pivots.append((r, c))
        r += 1
    return m, pivots


def back_substitute(m: Matrix, pivots: list[tuple[int, int]], tol: float = 1e-12) -> Solution:
    """Read the answer off a row echelon form.

    A row of zeros with a nonzero right-hand side means no answer. A column without a pivot is a free unknown,
    so there are infinitely many; the one returned sets every free unknown to 0. Otherwise the pivot unknowns
    come out bottom row up.
    """
    cols = len(m[0]) - 1
    scale = max((abs(v) for row in m for v in row), default=0) or 1
    for row in m:
        if all(abs(v) <= tol * scale for v in row[:cols]) and abs(row[cols]) > tol * scale:
            return Solution("none", None, [])
    pivot_cols = {c for _, c in pivots}
    free = [c for c in range(cols) if c not in pivot_cols]
    values: list[Number] = [0 * m[0][0]] * cols
    for r, c in reversed(pivots):
        rhs = m[r][cols]
        for j in range(c + 1, cols):
            rhs = rhs - m[r][j] * values[j]
        values[c] = rhs / m[r][c]
    return Solution("many" if free else "one", values, free)


def solve(a: list[list[Number]], b: list[Number], pivoting: bool = True, tol: float = 1e-12) -> Solution:
    """Solve A x = b by elimination and back substitution."""
    m, pivots = eliminate(augment(a, b), pivoting, tol)
    return back_substitute(m, pivots, tol)


def main() -> None:
    """Print the two rounding points: the tiny pivot with and without a swap, and the zero that isn't one."""
    eps = 1e-20
    a = [[eps, 1.0], [1.0, 1.0]]
    b = [1.0, 2.0]
    print(f"{eps:g}x + y = 1 and x + y = 2: the answer is x = y = 1 to 20 digits")
    for pivoting in (False, True):
        s = solve(a, b, pivoting=pivoting, tol=0)
        print(f"  {'with' if pivoting else 'without'} the swap: x = {s.values[0]}, y = {s.values[1]}")
    a = [[0.1, 0.7], [0.3, 2.1]]
    b = [0.8, 2.4]
    print("0.1x + 0.7y = 0.8 and 0.3x + 2.1y = 2.4: the same line twice, so infinitely many")
    for pivoting in (False, True):
        m, _ = eliminate(augment(a, b), pivoting=pivoting, tol=0)
        s = solve(a, b, pivoting=pivoting, tol=0)
        print(f"  {'with' if pivoting else 'without'} the swap, tol = 0: the second pivot is {m[1][1]}, so {s.kind}: {s.values}")
    s = solve(a, b)
    print(f"  tol = 1e-12: {s.kind}, y is free")


if __name__ == "__main__":
    main()
```

the run first does the tiny pivot above: without the swap, $x = 0.0$ and $y = 1.0$; with it, $x = 1.0$ and $y = 1.0$. then it takes $0.1x + 0.7y = 0.8$ and $0.3x + 2.1y = 2.4$, the same line written twice, so infinitely many answers. by hand, $R_2 - 3R_1$ is a row of zeros. in doubles, $0.1$ and $0.3$ are not the decimals they look like, only the closest fractions with a power of two below,[^py-float] and $0.3 / 0.1$ comes out as $2.9999999999999996$. then $2.1 - 2.9999999999999996 \cdot 0.7$ is $4.4 \times 10^{-16}$, not $0$. with `tol=0` the code takes that as a pivot, divides by it, and reports one answer, $(8, 0)$: a point on the line, offered as the only one. the swap doesn't help, since the pivot then comes out as $-1.1 \times 10^{-16}$ and the report is the same. with the default `tol=1e-12` both count as the zero they should be, and the answer is infinitely many, $y$ free.

## pitfalls

- **dividing by a tiny pivot.** the multiplier is the entry over the pivot, so a pivot of $10^{-20}$ turns the next row into $10^{20}$ copies of the pivot row and the row's own digits are gone. swap the biggest entry up first. without the swap the example above gets $x = 0$ instead of $1$
- **assuming there's exactly one answer.** $\det A = 0$ happens, and it means parallel lines or the same line twice. before dividing by a determinant or a pivot, check it isn't zero, and read a $0 = c$ row as "no answer", not as a bug
- **a "zero" pivot that is really $10^{-16}$.** rounding rarely gives an exact zero: $2.1 - 2.9999999999999996 \cdot 0.7$ is $4.4 \times 10^{-16}$. test against a tolerance relative to the matrix's biggest entry, or a system with infinitely many answers reports one, and a system with none reports one too
- **inverting a matrix to solve one system.** $A^{-1}\mathbf{b}$ is $n$ solves and a multiply where one solve would do, and every extra operation rounds. $430$ operations against $1430$ for $n = 10$. call `solve`, not `inv`
- **multiplying a row by 0.** it's not a row operation. the row becomes $0 = 0$, the equation is gone, and the system picks up answers it never had. scaling by $c \neq 0$ is fine
- **forgetting the right-hand side.** a row operation acts on the whole row, bar included. $R_2 - 3R_1$ on the coefficients alone changes which points are answers
- **swapping only on an exact zero.** a pivot of $10^{-20}$ isn't zero, so a rule of "swap when the pivot is 0" leaves it in place. partial pivoting swaps whenever something below is bigger

## practice

1. how many answers does each system have? $2x + y = 3$ and $x - y = 1$. then $x + 2y = 5$ and $2x + 4y = 4$. then $x + 2y = 5$ and $2x + 4y = 10$.

   **answer:** the first: the determinant is $2 \cdot (-1) - 1 \cdot 1 = -3 \neq 0$, so one answer, and it is $(4/3, 1/3)$. the second: the determinant is $1 \cdot 4 - 2 \cdot 2 = 0$, and row 2's left side is twice row 1's while $4 \neq 2 \cdot 5$: parallel lines, no answer. the third: row 2 is exactly twice row 1, right-hand side included: the same line, infinitely many answers.

2. solve $2x + 3y = 12$ and $x - y = 1$ by elimination.

   **answer:** swap so the pivot is $1$: $x - y = 1$ on top. then $R_2 - 2R_1$: $(2x + 3y) - 2(x - y) = 12 - 2$, so $5y = 10$ and $y = 2$. back up: $x - 2 = 1$, so $x = 3$. check: $6 + 6 = 12$ and $3 - 2 = 1$.

3. $R_1$: $2x + y = -1$. $R_2$: $6x - 2y = 4$. which row operation clears the $x$ from $R_2$, what does $R_2$ become, and what is the answer?

   **answer:** $6 = 3 \cdot 2$, so $R_2 - 3R_1$: $(6x - 2y) - 3(2x + y) = 4 - 3 \cdot (-1)$, which is $-5y = 7$, so $y = -7/5$. then $2x - 7/5 = -1$ gives $2x = 2/5$ and $x = 1/5$. check in $R_2$: $6/5 + 14/5 = 20/5 = 4$.

4. solve $x + y + z = 6$, $2x - y + z = 3$, $x + 2y - z = 2$.

   **answer:** $R_2 - 2R_1$ gives $-3y - z = -9$ and $R_3 - R_1$ gives $y - 2z = -4$:

   $$
   \left[\begin{array}{ccc|c} 1 & 1 & 1 & 6 \\ 0 & -3 & -1 & -9 \\ 0 & 1 & -2 & -4 \end{array}\right]
   $$

   swap rows 2 and 3 so the pivot is $1$, then $R_3 + 3R_2$: $(-3y - z) + 3(y - 2z) = -9 + 3 \cdot (-4)$, so $-7z = -21$ and $z = 3$. back up: $y - 6 = -4$ gives $y = 2$, and $x + 2 + 3 = 6$ gives $x = 1$. the answer is $(1, 2, 3)$. check: $1 + 2 + 3 = 6$, $2 - 2 + 3 = 3$, $1 + 4 - 3 = 2$.

5. $x + 2y = 5$ and $3x + ky = 1$. for which $k$ is there no answer? is there a $k$ with infinitely many? and solve it for $k = 5$.

   **answer:** the determinant is $1 \cdot k - 3 \cdot 2 = k - 6$. it is $0$ only at $k = 6$, where row 2's left side is three times row 1's but $1 \neq 3 \cdot 5 = 15$: parallel lines, no answer. no $k$ gives infinitely many, since that would need the right-hand side to be $15$ as well. every other $k$ gives one answer. at $k = 5$ the determinant is $-1$, and cramer's rule gives $x = (5 \cdot 5 - 1 \cdot 2) / (-1) = -23$ and $y = (1 \cdot 1 - 3 \cdot 5) / (-1) = 14$. check: $-23 + 28 = 5$ and $-69 + 70 = 1$.

6. $10^{-20} x + y = 1$ and $x + y = 2$, in doubles. say what elimination without a swap computes for $y$ and for $x$, and why, and what it computes with the swap.

   **answer:** without the swap the multiplier is $1 / 10^{-20} = 10^{20}$, and $R_2 - 10^{20} R_1$ reads $(1 - 10^{20})\,y = 2 - 10^{20}$. a double holds about 16 digits, so both $1 - 10^{20}$ and $2 - 10^{20}$ round to $-10^{20}$, and $y = 1$ exactly. then $x = (1 - 1) / 10^{-20} = 0$, off by a whole unit from the true $x = 1$. with the swap the pivot is $1$ and the multiplier $10^{-20}$: $(1 - 10^{-20})\,y = 1 - 2 \cdot 10^{-20}$, both sides round to $1$, $y = 1$, and $x = 2 - 1 = 1$.

[^numpy-solve]: numpy's [numpy.linalg.solve](https://numpy.org/doc/stable/reference/generated/numpy.linalg.solve.html) docs: the solutions are computed with the lapack routine `_gesv`, and the matrix must be square and of full rank.

[^dgesv]: lapack's [dgesv](https://netlib.org/lapack/explore-html/d8/d72/dgesv_8f_source.html) source: it computes the solution of a real system A · X = B by factoring A as P · L · U with an lu decomposition with partial pivoting and row interchanges, then solving from the factored form.

[^py-float]: python's tutorial, [floating-point arithmetic: issues and limitations](https://docs.python.org/3/tutorial/floatingpoint.html): almost all platforms map python floats to ieee 754 binary64 doubles, which carry 53 bits of precision, and a decimal like 0.1 is stored as the closest fraction of the form j / 2ⁿ, which for 0.1 is a little above a tenth.

## related

- https://learning.anthnydas.com/math/matrices.md
- https://learning.anthnydas.com/math/vectors.md
- https://learning.anthnydas.com/math/eigenvectors.md
- https://learning.anthnydas.com/math/functions.md
