A Python implementation of the Thomas Algorithm (Tridiagonal Matrix Algorithm - TDMA) for efficiently solving systems of linear equations with a tridiagonal coefficient matrix.
The Thomas Algorithm is a simplified form of Gaussian Elimination specifically designed for tridiagonal matrices. Since only the main diagonal and the two adjacent diagonals contain non-zero elements, the algorithm significantly reduces computational complexity compared to standard elimination methods.
This project provides a clean Python implementation of the algorithm along with an example problem and visualization of the computed solution.
- Efficient implementation of the Thomas Algorithm
- Solves tridiagonal systems of linear equations
- Simple and well-commented Python code
- Fast forward elimination and backward substitution
- Easy to modify for larger systems
- Suitable for engineering and scientific applications
Thomas-Algorithm-Python/
│
├── thomas_algorithm.py
├── example.py
├── sample_input.txt
├── sample_output.txt
├── figures/
│ ├── solution_plot.png
│ └── flowchart.png
├── presentation/
├── report/
├── LICENSE
└── README.md
The Thomas Algorithm consists of two main stages:
- Modify the upper diagonal coefficients.
- Update the right-hand-side vector.
- Eliminate lower diagonal elements.
- Compute the final unknown values.
- Start from the last equation.
- Continue upward until all variables are determined.
- Read the lower, main, and upper diagonal arrays.
- Read the right-hand-side vector.
- Perform forward elimination.
- Perform backward substitution.
- Display the solution vector.
Given the tridiagonal system:
| 2 -1 0 | |x₁| | 1 |
|-1 2 -1 | |x₂| = | 0 |
| 0 -1 2 | |x₃| | 1 |
Output:
x₁ = 1.0
x₂ = 1.0
x₃ = 1.0
- Python 3.8 or later
Clone the repository:
git clone https://github.com/yourusername/Thomas-Algorithm-Python.gitMove into the project directory:
cd Thomas-Algorithm-PythonRun the program:
python thomas_algorithm.py| Operation | Complexity |
|---|---|
| Forward Elimination | O(n) |
| Backward Substitution | O(n) |
| Total | O(n) |
The Thomas Algorithm is widely used in engineering and scientific computing, including:
- Computational Fluid Dynamics (CFD)
- Heat Transfer Analysis
- Finite Difference Method (FDM)
- Structural Engineering
- Numerical Solution of Differential Equations
- Groundwater Flow Simulation
- Computational Physics
- Electrical Circuit Analysis
- Very fast for tridiagonal systems
- Linear time complexity O(n)
- Low memory usage
- Easy to implement
- More efficient than Gaussian Elimination for tridiagonal matrices
- Applicable only to tridiagonal matrices
- Requires non-zero main diagonal elements
- Less suitable for sparse matrices with arbitrary patterns
- No pivoting, so numerical stability depends on the matrix properties
- Add support for sparse matrix formats
- Develop a graphical user interface (GUI)
- Benchmark against other numerical methods
- Extend to block tridiagonal systems
- Package the implementation as a reusable Python library
- Thomas, L. H. (1949). Elliptic Problems in Linear Differential Equations over a Network.
- Chapra, S. C., & Canale, R. P. Numerical Methods for Engineers.
- Burden, R. L., & Faires, J. D. Numerical Analysis.
David Ojha
Bachelor of Mechanical Engineering
Kathmandu University
GitHub: https://github.com/davdojha
Portfolio: https://davidojha.com.np
Your support is greatly appreciated!