A command-line Python program that solves systems of linear equations by Gaussian elimination.
- Two input modes: a coefficient matrix (augmented, last column is the right-hand side), or equations written with named variables (
2x + 3y - z = 5). - Reduces the matrix to reduced row echelon form (RREF) and prints it.
- Reads the solution off the RREF: a unique value per variable, or, for underdetermined systems, a parametric solution with free variables
t_k. - Pure Python, standard library only.
Python 3.12 or newer (the code uses nested same-quote f-strings, which earlier versions reject).
python3 gaussian_elimination.py
The program first asks for the input mode, m (matrix) or e (equations); M, E and the quoted forms 'm', 'e' are also accepted. It then reads the system from standard input until end of file (Ctrl-D on Linux/macOS, Ctrl-Z Enter on Windows).
Input can also be redirected from a file whose first line is the mode:
python3 gaussian_elimination.py < examples/equations_input.txt
Matrix mode (m). One equation per line, coefficients separated by spaces, the last number being the right-hand side. A variable missing from an equation gets coefficient 0. Entries are parsed as integers. Variables are named x_1, x_2, ... in the output.
m
1 2 3 4
2 4 6 8
Equation mode (e). One equation per line, any variable names, constant on the right-hand side. Allowed operators are +, - and =; coefficients may be decimals with a dot as the decimal mark. Spaces are ignored. Every line, including the last, must end with a newline.
e
2x + 3y - z = 5
x - y + 2z = 3
3x + y + z = 10
$ python3 gaussian_elimination.py < examples/equations_input.txt
Input system of linear equations.
Your input should be the coefficient matrix of the system.
In each row write one linear equation.
Type 0 for variables, which do not participate in your equations:
Please clerify your input method: for matrix enter 'm', for equations enter 'e' :Enter your system of linear equations:
Here is the reduced row echelon form of your matrix:
1 0 0 4.6
0 1 0 -2
0 0 1 -1.8
x = 4.6
y = -2
z = -1.8
A system with infinitely many solutions:
$ python3 gaussian_elimination.py < examples/infinitely_many.txt
...
Here is the reduced row echelon form of your matrix:
1 2 3 4
0 0 0 0
x_1 = 4 - 2t_2- 3t_3
x_2 = t_2
x_3 = t_3
- Arithmetic is in floating point; displayed values are rounded to about four significant decimals (e.g.
-0.8571). - The consistency check described in the developer documentation (
check_consistence()) is defined but not called in the final version, so an inconsistent system is not reported as such; its RREF is printed and a (meaningless) solution is read off. - A variable whose column is entirely zero is printed as
Noneinstead of as a free parameter. - Matrix mode accepts integer entries only; decimals are accepted only in equation mode.
gaussian_elimination.py The program
examples/matrix_input.txt Sample input, matrix mode
examples/equations_input.txt Sample input, equation mode
examples/infinitely_many.txt Sample input with free variables
docs/User_Documentation.md / .pdf
docs/Developer_Documentation.md / .pdf
docs/Project_Proposal.md / .pdf
docs/original/ Original LaTeX sources and PDFs
Hayk Minasyan
Semester project for Programming 1, Charles University, Faculty of Mathematics and Physics (MFF UK), 2024/25