How to Solve Linear Equations in Python Without Math Libraries

SOLVING A LINEAR EQUATION USING BRUTE FORCE (1)

Solving systems of linear equations is useful in many areas like science and math. Normally, you would use a math library in Python like Numpy or SymPy to solve them. But for simple problems, we don’t need complex libraries.

In this article, we will write our own Python function to solve small linear equation systems using basic math concepts. By not relying on advanced Python tools, we lower memory usage and better understand the math. Although our solution won’t be as powerful as library functions, making our own teaches the core method.

We will use easy Python data types and logic without any imports. This approach focuses on the fundamentals rather than complex programming.

Understanding Linear Equations and Their Systems

A linear equation is a mathematical expression that represents a straight line when plotted on a graph or more specifically, on the cartesian plane. It denotes the relationship between two variables, mainly x and y, where x is the independent variable and y is called the dependent variable.

The mathematical expression tells us if there is one unit change in x what impact will it have on the dependent variable y. The general form of a linear equation is ax + by = c, where a, b, and c are the coefficients of x and y, both of which are not equal to 0. Since the powers of x and y are 1, hence the equation is called linear.

A system of linear equations involves two or more linear equations with the same set of variables. The feasible solution is such that their value suffices all equations simultaneously. The set of equations looks like as shown below:

  • a1x + b1y = c1
  • a2x + b2y = c2
  • a3x + b3y = c3
  • ….
  • ….
  • anx + bny = cn

where, x and y are the variables, an , bn, and cn are the coefficients. The n denotes there can be as many number of variables as we want and similarly there can be as many number of equations as we want.

Traditional Methods for Solving Linear Equations

In traditional mathematics, we have three main methods of solving linear equations, they are:

  • Substitution method: Solve one equation for a variable and plug it into the other equations. For example, if you have: x + y = 10 2x – y = 5 Solve the first equation to get y = 10 – x. Plug this y into the second equation: 2x – (10 – x) = 5 Then solve for x.
  • Elimination method: Add or subtract multiples of the equations to cancel out a variable. For example: 2x + 3y = 11 4x – 2y = 2 Multiply the first equation by 2 and the second by 3 and add them: 16x = 28 Then solve for x.
  • Matrix method: Organize variables and coefficients into matrices. Use matrix math like inverses and determinants to solve the system. Helpful for many equations.

The substitution and elimination methods work well to manually solve small sets of equations. Matrix methods are more complex but can handle larger systems.

Since we are going to use brute force to solve a given system of linear equations in a limited range we won’t be using any of the above mentioned methods used in traditional algebra. Instead, we will be using an exhaustive approach where our function will be defined in such a way that it computes all possible combinations until it finds a solution which satisfies both the equations simultaneously. Let’s take a look at the code in the next section.

Suggested: Numpy linalg.solve() – Solve a linear matrix equation or system of linear scalar equations.

Python Brute Force Approach for Linear Equations

The idea for this section is to define a basic function in Python that will take into account all possible values of our variables to satisfy a system of linear equations in a limited range. Let’s take a look at the code. You don’t need any additional packages since we won’t be using any external library for our program.

Our very first step would be to define a function that can iteratively check the values whether it satisfies our equation or not. For ease of understanding, I have taken two equations for our system, 3a + 2b = c and 4a – 5b = c.

The solutions will be stored in a list which is defined in the very beginning of the function.

#defining our function. 
def solve_system_of_equations(a_range, b_range, c_range):
    solutions = []

    for a in range(*a_range):
        for b in range(*b_range):
            for c in range(*c_range):
                # Substituting the values of a, b, c into the equations
                # Example equations: 3a + 2b = c and 4a - 5b = c
                if 3 * a + 2 * b == c and 4 * a - 5 * b == c:
                    solutions.append((a, b, c))
    
    return solutions

The next step would be to modify the coefficient values of a,b and c within a given range. In the given code block we have taken a specific range for each of these coefficients. Their values lies between these ranges.

# Define the ranges for a, b, c
a_range = (-10, 10)  # Example: a varies from -10 to 9
b_range = (-10, 10)  # Example: b varies from -10 to 9
c_range = (-100, 100)  # Example: c varies from -100 to 99

The last and final step would be to print the solutions.

solutions = solve_system_of_equations(a_range, b_range, c_range)
print("Solutions:", solutions)

Let’s take a look at our output.

Solutions: [(-7, -1, -23), (0, 0, 0), (7, 1, 23)]
Brute Force Method Of Solving System Of Linear Equations Using Python
Brute Force Method Of Solving System Of Linear Equations Using Python

Recommended: Numpy’s linalg.tensorsolve: Solving Tensor Equations in Python.

Wrapping Up: The Value of Simplicity in Problem Solving

There are different ways to solve sets of linear equations in math. Traditionally, people would use substitution, elimination, or matrices to manually work through them. But today we have computer tools like NumPy and SymPy that can solve equations for us. These libraries are very helpful! However, sometimes using simple Python functions can be better for small problems. Coding our own brute force solution lets us customize and understand what’s going on behind the scenes. While not as powerful as the advanced libraries, writing basic functions from scratch can be useful for learning. I hope this gave you a helpful introduction to solving linear systems with and without Python!