Solving linear equation systems

Week 1 — Linear systems by hand

Quantitative biology and integrative omics

About this lecture

Today we start the linear-algebra block. By the end you will be able to:

  • Recognise when a biology problem is a system of linear equations.
  • Write such a system in the standard form A\mathbf{x} = \mathbf{b}.
  • Solve small 2\times 2 and 3\times 3 systems by hand using substitution and Gaussian elimination.
  • Use the determinant ad - bc to tell whether a 2\times 2 system has one solution.
  • Check a candidate solution by plugging it back in.

No prior linear algebra is assumed. We build the ideas from a concrete example and keep all numbers small enough to do with pen and paper.

A bulk sample mixes cell types; we want the amount of each

A tumour biopsy is not one cell type. It is a mixture: some tumour cells, some immune cells, some healthy tissue. Suppose we already know two marker readouts per cell type (say, expression of gene G1 and gene G2), measured per “unit” of each cell type:

Tumour Immune
Gene G1 signal 2 1
Gene G2 signal 1 3

We sequence a bulk sample and measure total signal: G1 = 8, G2 = 9.

Question: how many units of tumour cells (x) and immune cells (y) were in the sample?

Each measured signal is a weighted sum of the unknown amounts

Each measured signal is the sum of contributions from each cell type:

\begin{aligned} \underbrace{2x + 1y}_{\text{total G1}} &= 8\\[2pt] \underbrace{1x + 3y}_{\text{total G2}} &= 9 \end{aligned}

Two unknowns (x and y), two equations. This is a system of linear equations.

“Linear” means every unknown appears only to the first power — no x^2, no xy, no \sin x. Just numbers times unknowns, added up. More formally, a function is linear if f(x+y)=f(x)+f(y). You can check that f(x)=2x is linear.

A\mathbf{x} = \mathbf{b} separates knowns, unknowns and measurements

We separate the known coefficients from the unknowns and the measurements:

\begin{bmatrix} 2 & 1 \\ 1 & 3 \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} = \begin{bmatrix} 8 \\ 9 \end{bmatrix} \qquad\text{that is}\qquad A\,\mathbf{x} = \mathbf{b}

  • A — the coefficient matrix. Here, the cell-type signatures (one column per cell type, one row per gene).
  • \mathbf{x} — the unknowns we want (the cell-type amounts).
  • \mathbf{b} — the right-hand side, our measurements.

The first row of A times \mathbf{x} reproduces the first equation; the second row gives the second. This packaging is what lets us scale to hundreds of genes later.

Strategy 1 — Substitution

Solve one equation for one unknown, then substitute into the other.

From equation 1: \;2x + y = 8 \;\Rightarrow\; y = 8 - 2x.

Put that into equation 2:

x + 3(8 - 2x) = 9 \;\Rightarrow\; x + 24 - 6x = 9 \;\Rightarrow\; -5x = -15

So x = 3, and then y = 8 - 2(3) = 2.

Answer: 3 units of tumour, 2 units of immune cells.

The system

\begin{aligned} 2x + y &= 8 \quad(1)\\ x + 3y &= 9 \quad(2) \end{aligned}

Always check your answer

Plug x = 3,\; y = 2 back into the original equations:

\begin{aligned} 2(3) + 1(2) &= 6 + 2 = 8 \quad\checkmark\\ 1(3) + 3(2) &= 3 + 6 = 9 \quad\checkmark \end{aligned}

Both hold, so the solution is correct.

The system

\begin{aligned} 2x + y &= 8 \quad(1)\\ x + 3y &= 9 \quad(2) \end{aligned}

Checking takes little effort and catches almost every arithmetic mistake. Get the habit in to avoid losing points on the exam! (or getting the wrong answer in your research)

Strategy 2 — Gaussian elimination

Substitution gets messy with three or more unknowns. Gaussian elimination is the systematic method that scales (it is what R uses internally).

The idea: add multiples of one equation to another to eliminate a variable, until the system is easy to read off.

Three moves are allowed (they never change the solution):

  1. Multiply an equation by a non-zero number.
  2. Add a multiple of one equation to another.
  3. (Swapping order of two equations. Can sometimes make life more convenient)

Gaussian elimination on our 2×2

Start:

\begin{aligned} 2x + 1y &= 8 \quad(\text{R1})\\ 1x + 3y &= 9 \quad(\text{R2}) \end{aligned}

Eliminate x from R2. Replace R2 by \;\text{R2} - \tfrac{1}{2}\text{R1}:

\Big(1 - \tfrac{1}{2}\cdot 2\Big)x + \Big(3 - \tfrac{1}{2}\cdot 1\Big)y = 9 - \tfrac{1}{2}\cdot 8 \;\Rightarrow\; 0\,x + 2.5\,y = 5

Now R2 says 2.5\,y = 5 \Rightarrow y = 2.

Back-substitute into R1: 2x + 2 = 8 \Rightarrow x = 3.

Same answer as before. \checkmark

Try it: two equations, two lines

Write the equations as ax + by = c and dx + ey = f. Each is a line; a solution lies on both. Lines cross (one solution), are parallel (none: elimination leaves 0 = a non-zero number) or are the same line (infinitely many: 0 = 0).

Track only the numbers and aim for a staircase of zeros

We do not need to keep writing x and y. Track only the numbers in an augmented matrix [\,A \mid \mathbf{b}\,]:

\left[\begin{array}{cc|c} 2 & 1 & 8 \\ 1 & 3 & 9 \end{array}\right] \xrightarrow{\;\text{R2}\,-\,\frac12\text{R1}\;} \left[\begin{array}{cc|c} 2 & 1 & 8 \\ 0 & 2.5 & 5 \end{array}\right]

The bottom row reads 2.5\,y = 5. The goal is this upper-triangular (“staircase”) shape: zeros below the diagonal. Then solve bottom-up.

Three cell types give a 3×3 system, cleared column by column

Three cell types (tumour, immune, stroma) with amounts x, y, z, three marker genes. Signatures and measurements give:

\begin{aligned} 1x + 1y + 1z &= 6\\ 0x + 2y + 1z &= 7\\ 2x + 1y + 1z &= 8 \end{aligned} \qquad \left[\begin{array}{ccc|c} 1 & 1 & 1 & 6\\ 0 & 2 & 1 & 7\\ 2 & 1 & 1 & 8 \end{array}\right]

We eliminate column by column, top-left to bottom-right.

3×3 elimination — clear column 1

Pivot is the 1 in the top-left. R2 already has a 0 there. Clear R3 with \;\text{R3} - 2\,\text{R1}:

\left[\begin{array}{ccc|c} 1 & 1 & 1 & 6\\ 0 & 2 & 1 & 7\\ 2 & 1 & 1 & 8 \end{array}\right] \xrightarrow{\;\text{R3}-2\text{R1}\;} \left[\begin{array}{ccc|c} 1 & 1 & 1 & 6\\ 0 & 2 & 1 & 7\\ 0 & -1 & -1 & -4 \end{array}\right]

Row by row, what \text{R3} - 2\,\text{R1} means:

\begin{array}{lrrrr} \text{R3} & 2 & 1 & 1 & 8\\ 2\,\text{R1} & 2 & 2 & 2 & 12\\ \hline \text{R3} - 2\,\text{R1} & 0 & -1 & -1 & -4 \end{array}

Column 1 now has zeros below the pivot.

3×3 elimination — clear column 2

Pivot is now the 2 in R2. Clear R3 with \;\text{R3} + \tfrac{1}{2}\text{R2}:

\left[\begin{array}{ccc|c} 1 & 1 & 1 & 6\\ 0 & 2 & 1 & 7\\ 0 & -1 & -1 & -4 \end{array}\right] \xrightarrow{\;\text{R3}+\frac12\text{R2}\;} \left[\begin{array}{ccc|c} 1 & 1 & 1 & 6\\ 0 & 2 & 1 & 7\\ 0 & 0 & -0.5 & -0.5 \end{array}\right]

Row by row, what \text{R3} + \tfrac12\,\text{R2} means:

\begin{array}{lrrrr} \text{R3} & 0 & -1 & -1 & -4\\ \tfrac12\,\text{R2} & 0 & 1 & 0.5 & 3.5\\ \hline \text{R3} + \tfrac12\,\text{R2} & 0 & 0 & -0.5 & -0.5 \end{array}

Now upper-triangular. The bottom row reads -0.5\,z = -0.5.

3×3 — back-substitution

The triangular form from the last slide, written as equations again:

\left[\begin{array}{ccc|c} 1 & 1 & 1 & 6\\ 0 & 2 & 1 & 7\\ 0 & 0 & -0.5 & -0.5 \end{array}\right] \quad\Longleftrightarrow\quad \begin{aligned} x + y + z &= 6\\ 2y + z &= 7\\ -0.5\,z &= -0.5 \end{aligned}

From the bottom up:

\begin{aligned} -0.5\,z &= -0.5 &&\Rightarrow\; z = 1\\ 2y + z &= 7 &&\Rightarrow\; 2y + 1 = 7 \;\Rightarrow\; y = 3\\ x + y + z &= 6 &&\Rightarrow\; x + 3 + 1 = 6 \;\Rightarrow\; x = 2 \end{aligned}

Solution: x = 2,\; y = 3,\; z = 1.

Check in the original: 2+3+1=6\,\checkmark, 0+6+1=7\,\checkmark, 4+3+1=8\,\checkmark.

Try it: elimination step by step

Our 3×3 system, one row operation at a time: pivot circled, changed row in yellow, the new 0 boxed in green.

The inverse undoes A: invert once, reuse for every sample

The inverse A^{-1} is the matrix with A^{-1}A = I. Once you have it, every system with this A is one multiplication:

A\mathbf{x} = \mathbf{b} \quad\Rightarrow\quad \mathbf{x} = A^{-1}\mathbf{b}

Why bother? In deconvolution the signatures A stay the same and only the measurements \mathbf{b} change from sample to sample. Invert once, reuse for every sample.

How: put the identity on the right, [\,A \mid I\,], and keep doing row operations until the left side is I. Then the right side is A^{-1}:

[\,A \mid I\,] \;\longrightarrow\; \cdots \;\longrightarrow\; [\,I \mid A^{-1}\,]

  • Same row operations as before, applied to the whole row.
  • New: after reaching the staircase, also eliminate upwards, then divide each row by its diagonal number. (This is called Gauss–Jordan elimination.)

Inverse by hand: our 2×2

A = \begin{pmatrix} 2 & 1 \\ 1 & 3 \end{pmatrix} from before. Clear below the diagonal, then above it, then scale:

\left[\begin{array}{cc|cc} 2 & 1 & 1 & 0 \\ 1 & 3 & 0 & 1 \end{array}\right] \xrightarrow{\;\text{R2}\,-\,\frac12\text{R1}\;} \left[\begin{array}{cc|cc} 2 & 1 & 1 & 0 \\ 0 & 2.5 & -0.5 & 1 \end{array}\right] \xrightarrow{\;\text{R2}\,\div\,2.5\;} \left[\begin{array}{cc|cc} 2 & 1 & 1 & 0 \\ 0 & 1 & -0.2 & 0.4 \end{array}\right]

\xrightarrow{\;\text{R1}\,-\,\text{R2}\;} \left[\begin{array}{cc|cc} 2 & 0 & 1.2 & -0.4 \\ 0 & 1 & -0.2 & 0.4 \end{array}\right] \xrightarrow{\;\text{R1}\,\div\,2\;} \left[\begin{array}{cc|cc} 1 & 0 & 0.6 & -0.2 \\ 0 & 1 & -0.2 & 0.4 \end{array}\right]

So A^{-1} = \begin{pmatrix} 0.6 & -0.2 \\ -0.2 & 0.4 \end{pmatrix}. Check: A^{-1}\mathbf{b} = \begin{pmatrix} 0.6\cdot 8 - 0.2\cdot 9 \\ -0.2\cdot 8 + 0.4\cdot 9 \end{pmatrix} = \begin{pmatrix} 3 \\ 2 \end{pmatrix}, the same answer as before. \checkmark

No inverse? If elimination leaves a row of zeros on the left, you can never reach I: the matrix is singular (its columns point along the same line) and has no inverse.

Try it: the inverse step by step

[\,A \mid I\,] to [\,I \mid A^{-1}\,] one row operation at a time, then the check A\,A^{-1} = I.

The 2\times2 shortcut: the determinant

For A = \begin{pmatrix} a & b \\ c & d \end{pmatrix}, one number decides everything: the determinant

\det A = ad - bc

  • \det A \neq 0: A has an inverse, and A\mathbf{x} = \mathbf{b} has exactly one solution: A^{-1} = \frac{1}{ad - bc}\begin{pmatrix} d & -b \\ -c & a \end{pmatrix} \qquad \text{(swap } a, d\text{; negate } b, c\text{; divide by } \det A)
  • \det A = 0: the columns are parallel, there is no inverse, and no unique solution.

Our A = \begin{pmatrix} 2 & 1 \\ 1 & 3 \end{pmatrix}: \det A = 2\cdot3 - 1\cdot1 = 5, so A^{-1} = \frac15\begin{pmatrix} 3 & -1 \\ -1 & 2 \end{pmatrix} = \begin{pmatrix} 0.6 & -0.2 \\ -0.2 & 0.4 \end{pmatrix}, as before. ✓

\begin{pmatrix} 1 & 2 \\ 2 & 4 \end{pmatrix}: \det = 1\cdot4 - 2\cdot2 = 0. Column 2 is twice column 1: no inverse.

In R, solve() does the elimination for you

In Linear systems in R we let the computer do the elimination. The code is short:

A <- matrix(c(1, 1, 1,
              0, 2, 1,
              2, 1, 1), nrow = 3, byrow = TRUE)
b <- c(6, 7, 8)
solve(A, b)
# [1] 2 3 1
solve(A)       # one argument: the inverse

solve() runs Gaussian elimination internally — exactly what you just did by hand, but on any size. With one argument it returns A^{-1}.

Summary

  • A linear system = weighted sums of unknowns set equal to numbers.
  • Standard form: A\mathbf{x} = \mathbf{b} (A coefficients, \mathbf{x} unknowns, \mathbf{b} measurements).
  • Substitution for tiny systems; Gaussian elimination (via the augmented matrix) for anything bigger — eliminate to upper-triangular, then back-substitute.
  • Inverse: row-reduce [\,A \mid I\,] to [\,I \mid A^{-1}\,]; then \mathbf{x} = A^{-1}\mathbf{b} for any \mathbf{b}. A zero row on the left means no inverse.
  • Determinant of a 2\times2: ad - bc. Zero means parallel columns, no inverse, no unique solution.
  • Always check by substituting back into the original equations.
  • Biology hook: recovering cell-type amounts from bulk measurements.

Next: Linear systems in R — what happens when there is no solution or infinitely many.