Small Group Tutorials

Here to help students catch up, keep up, and move ahead. Book a consultation here.

How to Learn the Thomas Algorithm: Tridiagonal Systems, Forward Elimination, Back Substitution and Numerical Stability

Three students studying together in an eduKate small-group classroom.

Wait, What?

A matrix that looks like ordinary linear algebra can become a completely different algorithm once you notice that almost all of it is zero.

The Thomas algorithm solves a tridiagonal linear system in linear time by exploiting structure that generic Gaussian elimination does not need to assume. A tridiagonal matrix has nonzero values only on the main diagonal and the diagonals immediately above and below it.

For beginners, this is a lesson in recognising shape before calculating. For intermediate learners, it is a compact example of forward elimination followed by back substitution. At advanced level, it teaches operation counts and numerical assumptions. At professional level, the real questions are stability, pivoting, singular or nearly singular pivots, multiple right-hand sides, cyclic or block variants, and when to use a trusted library routine instead of hand-written code.

Quick Answer

Learn the Thomas algorithm in this order: linear systems → tridiagonal structure → three stored diagonals → ordinary elimination → specialised forward sweep → back substitution → O(n) cost → diagonal dominance/SPD intuition → pivoting limits → library solvers → PDE and spline applications → verification.

1. See the structure first

A tridiagonal system looks like:

b0 c0  0  0   ...      x0   d0
a1 b1 c1  0   ...      x1   d1
0  a2 b2 c2   ...  *   x2 = d2
...                         ...
       an bn             xn   dn

Each row couples one unknown mainly to its immediate neighbours. Instead of storing an n × n matrix, store three length-n vectors:

  • a: subdiagonal;
  • b: main diagonal;
  • c: superdiagonal.

This representation already teaches an important professional habit: exploit known structure in both algorithm design and memory layout.

2. The algorithm is specialised Gaussian elimination

Generic Gaussian elimination removes entries below the diagonal across a dense matrix. In a tridiagonal matrix there is only one subdiagonal entry to eliminate in each row.

That means the elimination wave can move from top to bottom without creating a dense matrix when the assumptions hold.

3. Forward elimination

For row i, eliminate a[i] using the previous pivot:

w = a[i] / b[i-1]
b[i] = b[i] - w * c[i-1]
d[i] = d[i] - w * d[i-1]

After the forward sweep, the system is upper bidiagonal. The lower diagonal has effectively been removed.

4. Back substitution

Start from the last unknown:

x[n-1] = d[n-1] / b[n-1]

Then move upward:

x[i] = (d[i] - c[i] * x[i+1]) / b[i]

That is the whole computational skeleton: one forward pass, one backward pass.

5. Why the cost is O(n)

Each row performs a constant amount of arithmetic during the forward sweep and a constant amount during back substitution. The number of rows is n, so the total arithmetic work grows linearly.

This is dramatically cheaper than treating the matrix as dense, but the improvement comes from structural information—not from a mysterious shortcut.

6. A four-equation worked example

Consider:

2x0 - x1           = 1
-x0 + 2x1 - x2     = 0
     -x1 + 2x2-x3  = 0
          -x2+2x3  = 1

Store:

a = [ -, -1, -1, -1]
b = [ 2,  2,  2,  2]
c = [-1, -1, -1,  -]
d = [ 1,  0,  0,  1]

Trace the modified diagonal and right-hand side after each elimination step. Do not jump directly to code. The educational goal is to see how the same local operation propagates through the system.

7. The invariant to watch

After processing row i in the forward sweep, every subdiagonal entry up through row i has been eliminated, and the transformed system has the same solution as the original system, provided no invalid division has occurred.

This connects procedural code to algebraic meaning.

8. The dangerous assumption: pivots cannot be blindly trusted

The textbook Thomas algorithm is usually presented without pivoting. That is not a universal numerical guarantee.

If a pivot becomes zero or extremely small, division can fail or amplify floating-point error. Special matrix classes—such as many strictly diagonally dominant or symmetric positive-definite tridiagonal systems—provide much safer conditions, but a general tridiagonal matrix may need pivoting.

This is why professional numerical libraries distinguish the simple structural idea from robust production solvers.

9. LAPACK gives the professional reality check

LAPACK’s DGTSV solves real tridiagonal systems by Gaussian elimination with partial pivoting. That detail matters: a production solver does not merely copy the no-pivot classroom recurrence and assume every input will behave nicely.

When reliability matters, use well-tested numerical libraries unless there is a clear reason not to.

10. In-place storage is possible

The forward sweep can overwrite diagonal and right-hand-side arrays because earlier values are no longer needed in their original form.

This reduces memory, but it changes the function contract. If callers need the original matrix later, either copy the inputs or document destructive behaviour explicitly.

11. Multiple right-hand sides

If the same tridiagonal matrix must solve many vectors d, repeated work may be factored or organised differently. Professional libraries often expose routines designed for multiple right-hand sides and separate factorisation from solve phases.

The key idea is reusable structure: do not recompute what is invariant across solves.

12. Where tridiagonal systems come from

They appear naturally when each unknown interacts only with immediate neighbours. Examples include:

  • finite-difference discretisations of one-dimensional differential equations;
  • implicit time-stepping schemes;
  • cubic spline calculations;
  • some smoothing and interpolation problems;
  • line solves inside larger multidimensional numerical methods.

Recognising the matrix pattern can therefore save enormous computational effort.

13. Cyclic tridiagonal systems are a different shape

If the first and last unknowns also couple to each other, the matrix has corner entries. Ordinary Thomas no longer applies directly.

Techniques such as Sherman–Morrison-type corrections or specialised cyclic tridiagonal solvers may be used. This is a useful lesson in algorithm boundaries: a nearly tridiagonal matrix is not always the same problem.

14. Block tridiagonal systems

Sometimes each “entry” is itself a small matrix block. The same neighbour-coupling pattern remains, but scalar division becomes solving block systems. Numerical stability becomes even more important, and explicit matrix inversion is usually a poor choice.

This extension is where classroom familiarity with the scalar algorithm starts connecting to serious scientific computing.

15. Parallelism exposes a trade-off

The ordinary Thomas sweep is sequential: row i depends on row i-1. For large parallel machines, methods such as cyclic reduction and parallel cyclic reduction may offer more concurrency at the cost of different arithmetic and implementation complexity.

The fastest serial algorithm is not automatically the fastest parallel algorithm.

16. Learn by predicting transformed coefficients

Use a short system and hide the next modified coefficient. Ask the learner to predict it before running the code.

This follows a useful programming-education pattern: prediction, execution, investigation, modification and eventual independent construction. Worked examples and code tracing reduce the load of learning syntax, algebra and algorithm state all at once.

17. Use a trusted dense solve as an oracle

For small random tridiagonal matrices, construct the full dense matrix and solve it using a trusted numerical library. Compare:

  • the computed solution;
  • the residual ||Ax-b||;
  • behaviour near poorly conditioned cases.

A small residual does not prove every numerical property, but it is a basic professional check.

18. Test cases that matter

  • n=1;
  • n=2;
  • strict diagonal dominance;
  • symmetric positive-definite systems;
  • zero pivot;
  • nearly zero pivot;
  • very different coefficient magnitudes;
  • multiple right-hand sides;
  • cyclic corner coupling;
  • comparison with a pivoting library solver.

19. Beginner-to-professional learning ladder

  • Beginner: identify the three nonzero diagonals and solve a tiny example by elimination.
  • Foundation: store the system as three arrays and trace the forward sweep.
  • Intermediate: implement forward elimination and back substitution and explain O(n) time.
  • Advanced: reason about pivots, residuals, conditioning and matrix classes that improve stability.
  • Professional: compare against LAPACK, decide when pivoting or another solver is required, and handle repeated, cyclic, block or parallel workloads appropriately.

20. Ownership boundary

This article owns the Thomas algorithm as a learning object: tridiagonal representation, specialised Gaussian elimination, forward/back sweeps, linear-time structure and numerical-stability boundaries. It does not replace the broader numerical-linear-algebra estate, PDE modelling, spline theory, learner measurement, MindOS, Bolt or Student/Studying Interface canonical jobs.

Sources and further reading

  • LAPACK 3.12.1 project and release information: Netlib LAPACK.
  • LAPACK DGTSV, tridiagonal solve by Gaussian elimination with partial pivoting: DGTSV documentation.
  • ACM/IEEE-CS/AAAI CS2023, Algorithmic Foundations: CS2023.
  • MIT Teaching + Learning Lab, worked examples for novice learning: Worked Examples.
  • Chia-Yu Chen et al., 2025 research on worked examples, explanation types and cognitive load in programming problem solving: ACM Transactions on Computing Education.

Professional rule: you understand the Thomas algorithm when you can explain exactly which matrix structure makes the linear sweep possible, detect when its no-pivot assumptions are unsafe, and know when a tested tridiagonal library solver is the better engineering decision.