A Mini Project on System of Linear Equation Solver

As part of my learning journey in Python and NumPy, today I built a small project that takes a system of linear equations, transforms it into row echelon form, and then determines whether the system has a:
✅ Unique solution
♾️ Infinitely many solutions
❌ No solution
This project was not only a hands-on exercise with NumPy but also a chance to implement Gaussian elimination manually. It helped me understand how even simple-looking logic can run into complex edge cases.
Here’s how I approached the problem — and the subtle bugs I encountered during development.
💻 Project Overview
Objective:
Take user input for a linear system
Form the augmented matrix
Convert it to row echelon form using Gaussian elimination
Use matrix rank to determine the nature of the solution
🔍 The Python Code (With Fixes)
import numpy as np
class SystemOFLineaEquation:
def __init__(self):
variable_set = ['x', 'y', 'z', 't', 'p', 'q']
nVariables = int(input("Enter the number of variables: "))
self.cols = nVariables+1
nEq = int(input("Enter the number of equations: "))
self.rows = nEq
print(f"The variables are x, y, z, t, p, q, from them starting {nVariables} variables will be selected")
systemOfEq = []
for i in range(nEq):
equation = []
print("Equation", i+1, ":")
for j in range(nVariables):
constantOfVariable = int(input(f"Enter the constant for variable {variable_set[j]}: "))
equation.append(constantOfVariable)
equation.append(int(input("The equation equal to: ")))
systemOfEq.append(equation)
self.system = np.array(systemOfEq)
if(self.system[:, nVariables].all() == 0):
print("The system is consistent")
print(self.system, len(self.system))
def rowechaelon_conversion(self):
self.rowechaelon = self.system.copy().astype(float)
pivot = 0
for i in range(len(self.rowechaelon)):
if pivot >= self.rowechaelon.shape[1]:
break
if self.rowechaelon[i, pivot] != 0:
for j in range(i+1, len(self.rowechaelon)):
if self.rowechaelon[j, pivot] == 0:
continue
else:
mult = self.rowechaelon[j, pivot]/self.rowechaelon[i, pivot]
self.rowechaelon[j] -= self.rowechaelon[i] * mult
pivot += 1
else:
swapIndex = -1
for k in range(i+1, len(self.rowechaelon)):
if self.rowechaelon[k, pivot] != 0:
swapIndex = k
break
if swapIndex != -1:
self.rowechaelon[[i, swapIndex]] = self.rowechaelon[[swapIndex, i]]
i = i -1
else:
pivot = pivot +1
i = i-1
print("row echelon: ")
print(self.rowechaelon)
def getRankOfMatrix(self)->int:
rank1 = 0
rank2 = 0
for row in self.rowechaelon[:,0:self.cols]:
if not np.all(row == 0):
rank1 += 1
for row in self.rowechaelon:
if not np.all(row == 0):
rank2 += 1
print("system rank:", rank1)
print("Augmented rank:", rank2)
if rank1 != rank2:
print("The system is inconsistent — no solution.")
elif rank1 == self.system.shape[1] - 1:
print("Unique solution exists.")
else:
print("Infinitely many solutions exist.")
return 0
system1 = SystemOFLineaEquation()
system1.rowechaelon_conversion()
system1.getRankOfMatrix()
⚠️ Subtle Issues I Faced
1. Pivot Column Full of Zeros Below
When trying to eliminate entries below a pivot, I encountered columns where every element below the current row was zero. Continuing with the elimination step caused invalid results.
Solution:
Add a condition to break safely:
if pivot >= self.rowechaelon.shape[1]:
break
This ensures we don't run into index errors or try to pivot into the solution column.
2. NumPy TypeError in Row Operations
While subtracting scaled rows, I hit a UFuncTypeError
:
self.rowechaelon[j] -= self.rowechaelon[i] * mult
Root Cause: NumPy doesn’t allow float-to-int assignment implicitly.
Fix:
I explicitly cast the matrix to float
:
self.rowechaelon = self.rowechaelon.astype(float)
3. Pivot Value Zero, Required Row Swap
In some cases, the pivot element was 0
, which meant elimination couldn't proceed unless I swapped it with a row below that had a non-zero entry in the same column.
Fix:
Added a search and swap block:
if swapIndex != -1:
self.rowechaelon[[i, swapIndex]] = self.rowechaelon[[swapIndex, i]]
📘 What I Learnt
Through this mini project, I learned a lot more than just linear algebra logic:
Edge case thinking is essential when implementing math algorithms
NumPy has strict type conversion rules — especially when it comes to mixing
int
andfloat
A deeper understanding of how Gaussian elimination and matrix rank work in practice
This experience has helped me move one step closer to building more math-based tools in the future.
Feedback Welcome!
This project helped me learn the hard way — by debugging and fixing it one issue at a time.
If you've ever implemented Gaussian elimination, matrix solvers, or anything with NumPy:
What approach did you take?
Have you faced similar issues?
How would you improve or simplify this?
I’d love to hear your thoughts! Let’s connect and learn together.
Subscribe to my newsletter
Read articles from Pranav Zambare directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
