mathlinear algebra

linear systems

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

report a mistake
.md

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
on this page
  1. the idea
  2. explore
  3. try it
  4. the rules
  5. worked examples
  6. the key idea up close
  7. in code
  8. pitfalls
  9. practice
  10. sources

the idea

one equation in two unknowns, x+2y=5x + 2y = 5, has endless answers: (5,0)(5, 0), (1,2)(1, 2), (−1,3)(-1, 3), every point of a line. a second equation, 3x−y=13x - y = 1, is another line. a pair (x,y)(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 xx left:

(3x−y)−3(x+2y)=1−3⋅5−7y=−14⇒y=2\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⋅2=1x = 5 - 2 \cdot 2 = 1. the crossing is (1,2)(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.

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

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.

the matrices page writes the same system as one equation, Ax=bA\mathbf{x} = \mathbf{b}, with AA the 2×22 \times 2 block of coefficients. the vectors page is where x\mathbf{x} and b\mathbf{b} come from.

explore

two lines, a1x+b1y=c1a_1 x + b_1 y = c_1 and a2x+b2y=c2a_2 x + b_2 y = c_2, from six sliders. the readout gives both equations, the determinant a1b2−a2b1a_1 b_2 - a_2 b_1, and the answer: the one crossing, or why there isn’t one.

−6−4−20246−4−2024(1, 2)
line 1
x + 2y = 5
line 2
3x − y = 1
determinant a₁b₂ − a₂b₁
−7
answer
x = 1, y = 2
a determinant of 0 means the lines point the same way: then it's all or nothing

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.

slide a₂ and b₂ until the determinant reads 0: the lines point the same way, and the answer is all or nothing.

try it

x + 2y = 5 and 2x + 4y = 4. how many answers?

pick one.

0 / 0 right

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=cax + by = c, with no x2x^2, no xyxy, no sin⁡x\sin x. in two unknowns its answers make a line, and in three, ax+by+cz=dax + 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 p\mathbf{p} and q\mathbf{q} are both answers to Ax=bA\mathbf{x} = \mathbf{b}. then for any tt,

A(p+t(q−p))=Ap+t(Aq−Ap)=b+t(b−b)=b\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 p\mathbf{p} and q\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:

each can be undone by a move of the same kind: swap back, scale by 1/c1/c, add −kRj-k R_j. that is why the answers never change, and it is why c=0c = 0 is not allowed: a row times 00 is 0=00 = 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:

as Ax = b

the system is Ax=bA\mathbf{x} = \mathbf{b}, with AA the coefficients, x\mathbf{x} the unknowns and b\mathbf{b} the right-hand sides. for the movie’s system:

(123−1)(xy)=(51)\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 b\mathbf{b} when det⁡A≠0\det A \neq 0, which is when AA has an inverse: then x=A−1b\mathbf{x} = A^{-1}\mathbf{b}. the matrices page is where the determinant comes from. for 2×22 \times 2 it is a1b2−a2b1a_1 b_2 - a_2 b_1, and the answer is cramer’s rule:

x=c1b2−c2b1a1b2−a2b1y=a1c2−a2c1a1b2−a2b1\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⋅(−1)−3⋅2=−7\det A = 1 \cdot (-1) - 3 \cdot 2 = -7, x=(5⋅(−1)−1⋅2)/(−7)=1x = (5 \cdot (-1) - 1 \cdot 2) / (-7) = 1 and y=(1⋅1−3⋅5)/(−7)=2y = (1 \cdot 1 - 3 \cdot 5) / (-7) = 2.

x=A−1b\mathbf{x} = A^{-1}\mathbf{b} is the right formula and the wrong program. to build A−1A^{-1} you solve nn systems at once, one for each column of the identity, and then there is still the multiply by b\mathbf{b}. counting multiplications and divisions, one solve of a 10×1010 \times 10 system by elimination and back substitution takes 430430, against 14301430 to invert that way and multiply. at 100×100100 \times 100 it is 343,300343{,}300 against 1,343,3001{,}343{,}300, close to n3/3n^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 x\mathbf{x}. numpy’s solve hands the system to lapack’s gesv,1 which factors AA by elimination with partial pivoting and solves from the factors.2

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=aic/pivotm = a_{ic} / \text{pivot} for each row ii, and a tiny pivot makes mm huge. the row below becomes Ri−mRpivotR_i - m R_{\text{pivot}}, and when mRpivotm R_{\text{pivot}} is 102010^{20} times the size of RiR_i, every digit RiR_i had is lost in the subtraction. with the biggest entry as the pivot, ∣m∣≤1|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=5x + 2y = 5 and 3x−y=13x - y = 1, as the movie did it:

[1253−11]→R2−3R1  [1250−7−14]→R2/(−7)  [125012]\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=2y = 2. back substitution: x+2⋅2=5x + 2 \cdot 2 = 5, so x=1x = 1. the movie went one step further, R1−2R2R_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.

  • R1 4x + 2y − 2z = 6
  • R2 2x + 3y + z = 9
  • R3 −2x + y + 4z = 4

3 equations, 3 unknowns. the coefficients sit left of the bar, the right-hand sides to its right.

step 1 of 12

pseudocode
eliminate(M):  # M is the augmented matrix [A | b]  r = 0  # the next pivot row  for each column c of A:    p = the row at or below r with the biggest |M[p][c]|    if M[p][c] == 0: continue  # no pivot here: a free variable    swap rows r and p    for each row i below r:      M[i] -= (M[i][c] / M[r][c]) * M[r]  # clears column c in row i    r += 1  return M  # row echelon form back_substitute(M):  if a row reads 0 ... 0 | c with c != 0: no answer  if a column has no pivot: infinitely many, that unknown is free  for each pivot row, bottom up:    x[c] = (rhs - the terms already known) / pivot  return x

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.

the row being changed is white, the row it reads from cyan, the pivot amber. arrow keys step, space plays.

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

a tiny pivot

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

(1−1020) y=2−10201−1020→−10202−1020→−1020\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,3 so the 11 and the 22 vanish next to 102010^{20} and both sides round to the same number. y=1y = 1 exactly, and then x=(1−y)/10−20=0x = (1 - y) / 10^{-20} = 0. wrong by a whole unit. with the swap, the pivot is 11, the multiplier is 10−2010^{-20}, and R2−10−20R1R_2 - 10^{-20} R_1 reads (1−10−20) y=1−2⋅10−20(1 - 10^{-20})\,y = 1 - 2 \cdot 10^{-20}, where both sides round to 11: y=1y = 1, x=2−1=1x = 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 SS and the new one S′S', after R2←R2+kR1R_2 \leftarrow R_2 + k R_1. take any point p\mathbf{p} that solves SS. it makes row 1 true, a1⋅p=c1a_1 \cdot \mathbf{p} = c_1, and row 2 true, a2⋅p=c2a_2 \cdot \mathbf{p} = c_2. the new row 2 is (a2+ka1)⋅x=c2+kc1(a_2 + k a_1) \cdot \mathbf{x} = c_2 + k c_1, and at p\mathbf{p} its left side is

(a2+ka1)⋅p=a2⋅p+k (a1⋅p)=c2+kc1\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 p\mathbf{p} solves S′S' too: every answer of SS is an answer of S′S'.

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

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

a scale by 00 breaks the second half. the new row 0=00 = 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 00 and what is left is x+2y=5x + 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−1610^{-16} is not divided by.

python/math/elimination.py
"""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.0x = 0.0 and y=1.0y = 1.0; with it, x=1.0x = 1.0 and y=1.0y = 1.0. then it takes 0.1x+0.7y=0.80.1x + 0.7y = 0.8 and 0.3x+2.1y=2.40.3x + 2.1y = 2.4, the same line written twice, so infinitely many answers. by hand, R2−3R1R_2 - 3R_1 is a row of zeros. in doubles, 0.10.1 and 0.30.3 are not the decimals they look like, only the closest fractions with a power of two below,3 and 0.3/0.10.3 / 0.1 comes out as 2.99999999999999962.9999999999999996. then 2.1−2.9999999999999996⋅0.72.1 - 2.9999999999999996 \cdot 0.7 is 4.4×10−164.4 \times 10^{-16}, not 00. with tol=0 the code takes that as a pivot, divides by it, and reports one answer, (8,0)(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×10−16-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, yy free.

pitfalls

practice

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

    answer

    the first: the determinant is 2⋅(−1)−1⋅1=−3≠02 \cdot (-1) - 1 \cdot 1 = -3 \neq 0, so one answer, and it is (4/3,1/3)(4/3, 1/3). the second: the determinant is 1⋅4−2⋅2=01 \cdot 4 - 2 \cdot 2 = 0, and row 2’s left side is twice row 1’s while 4≠2⋅54 \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=122x + 3y = 12 and x−y=1x - y = 1 by elimination.

    answer

    swap so the pivot is 11: x−y=1x - y = 1 on top. then R2−2R1R_2 - 2R_1: (2x+3y)−2(x−y)=12−2(2x + 3y) - 2(x - y) = 12 - 2, so 5y=105y = 10 and y=2y = 2. back up: x−2=1x - 2 = 1, so x=3x = 3. check: 6+6=126 + 6 = 12 and 3−2=13 - 2 = 1.

  3. R1R_1: 2x+y=−12x + y = -1. R2R_2: 6x−2y=46x - 2y = 4. which row operation clears the xx from R2R_2, what does R2R_2 become, and what is the answer?

    answer

    6=3⋅26 = 3 \cdot 2, so R2−3R1R_2 - 3R_1: (6x−2y)−3(2x+y)=4−3⋅(−1)(6x - 2y) - 3(2x + y) = 4 - 3 \cdot (-1), which is −5y=7-5y = 7, so y=−7/5y = -7/5. then 2x−7/5=−12x - 7/5 = -1 gives 2x=2/52x = 2/5 and x=1/5x = 1/5. check in R2R_2: 6/5+14/5=20/5=46/5 + 14/5 = 20/5 = 4.

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

    answer

    R2−2R1R_2 - 2R_1 gives −3y−z=−9-3y - z = -9 and R3−R1R_3 - R_1 gives y−2z=−4y - 2z = -4:

    [11160−3−1−901−2−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 11, then R3+3R2R_3 + 3R_2: (−3y−z)+3(y−2z)=−9+3⋅(−4)(-3y - z) + 3(y - 2z) = -9 + 3 \cdot (-4), so −7z=−21-7z = -21 and z=3z = 3. back up: y−6=−4y - 6 = -4 gives y=2y = 2, and x+2+3=6x + 2 + 3 = 6 gives x=1x = 1. the answer is (1,2,3)(1, 2, 3). check: 1+2+3=61 + 2 + 3 = 6, 2−2+3=32 - 2 + 3 = 3, 1+4−3=21 + 4 - 3 = 2.

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

    answer

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

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

    answer

    without the swap the multiplier is 1/10−20=10201 / 10^{-20} = 10^{20}, and R2−1020R1R_2 - 10^{20} R_1 reads (1−1020) y=2−1020(1 - 10^{20})\,y = 2 - 10^{20}. a double holds about 16 digits, so both 1−10201 - 10^{20} and 2−10202 - 10^{20} round to −1020-10^{20}, and y=1y = 1 exactly. then x=(1−1)/10−20=0x = (1 - 1) / 10^{-20} = 0, off by a whole unit from the true x=1x = 1. with the swap the pivot is 11 and the multiplier 10−2010^{-20}: (1−10−20) y=1−2⋅10−20(1 - 10^{-20})\,y = 1 - 2 \cdot 10^{-20}, both sides round to 11, y=1y = 1, and x=2−1=1x = 2 - 1 = 1.

sources

  1. numpy’s numpy.linalg.solve (opens in a new tab) docs: the solutions are computed with the lapack routine _gesv, and the matrix must be square and of full rank. ↩

  2. lapack’s dgesv (opens in a new tab) 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. ↩

  3. python’s tutorial, floating-point arithmetic: issues and limitations (opens in a new tab): 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. ↩ ↩2