The Numpy library can be used to perform a variety of mathematical/scientific operations such as matrix cross and dot products, finding sine and cosine values, Fourier transform and shape manipulation, etc. The word Numpy is short-hand notation for "Numerical Python".
In this article, you will see how to solve a system of linear equations using Python's Numpy library.
What is a System of Linear Equations?
Wikipedia defines a system of linear equations as:
In mathematics, a system of linear equations (or linear system) is a collection of two or more linear equations involving the same set of variables.
The ultimate goal of solving a system of linear equations is to find the values of the unknown variables. Here is an example of a system of linear equations with two unknown variables,
4x + 3y = 20 -5x + 9y = 26
To solve the above system of linear equations, we need to find the values of the
y variables. There are multiple ways to solve such a system, such as Elimination of Variables, Cramer's Rule, Row Reduction Technique, and the Matrix Solution. In this article we will cover the matrix solution.
In the matrix solution, the system of linear equations to be solved is represented in the form of matrix
AX = B. For instance, we can represent Equation 1 in the form of a matrix as follows:
A = [[ 4 3] [-5 9]] X = [[x] [y]] B = [ ]
To find the value of
y variables in Equation 1, we need to find the values in the matrix
X. To do so, we can take the dot product of the inverse of matrix
A, and the matrix
B as shown below:
X = inverse(A).B
If you are not familiar with how to find the inverse of a matrix, take a look at this link to understand how to manually find the inverse of a matrix. To understand the matrix dot product, check out this article.
Solving a System of Linear Equations with Numpy
From the previous section, we know that to solve a system of linear equations, we need to perform two operations: matrix inversion and a matrix dot product. The Numpy library from Python supports both the operations. If you have not already installed the Numpy library, you can do with the following
$ pip install numpy
Let's now see how to solve a system of linear equations with the Numpy library.
Using the inv() and dot() Methods
First, we will find inverse of matrix
A that we defined in the previous section.
Let's first create the matrix
A in Python. To create a matrix, the
array method of the Numpy module can be used. A matrix can be considered as a list of lists where each list represents a row.
In the following script we create a list named
m_list, which further contains two lists:
[-5,9]. These lists are the two rows in the matrix
A. To create the matrix
A with Numpy, the
m_list is passed to the
array method as shown below:
import numpy as np m_list = [[4, 3], [-5, 9]] A = np.array(m_list)
To find the inverse of a matrix, the matrix is passed to the
linalg.inv() method of the Numpy module:
inv_A = np.linalg.inv(A) print(inv_A)
The next step is to find the dot product between the inverse of matrix
A, and the matrix
B. It is important to mention that matrix dot product is only possible between the matrices if the inner dimensions of the matrices are equal i.e. the number of columns of the left matrix must match the number of rows in the right matrix.
To find the dot product with the Numpy library, the
linalg.dot() function is used. The following script finds the dot product between the inverse of matrix
A and the matrix
B, which is the solution of the Equation 1.
B = np.array([20, 26]) X = np.linalg.inv(A).dot(B) print(X)
4 are the respective values for the unknowns
y in Equation 1. To verify, if you plug
2 in place of the unknown
4 in the place of the unknown
y in equation
4x + 3y, you will see that the result will be 20.
Let's now solve a system of three linear equations, as shown below:
4x + 3y + 2z = 25 -2x + 2y + 3z = -10 3x -5y + 2z = -4
The above equation can be solved using the Numpy library as follows:
A = np.array([[4, 3, 2], [-2, 2, 3], [3, -5, 2]]) B = np.array([25, -10, -4]) X = np.linalg.inv(A).dot(B) print(X)
In the script above the
linalg.inv() and the
linalg.dot() methods are chained together. The variable
X contains the solution for Equation 2, and is printed as follows:
[ 5. 3. -2.]
The value for the unknowns
z are 5, 3, and -2, respectively. You can plug these values in Equation 2 and verify their correctness.
Using the solve() Method
In the previous two examples, we used
linalg.dot() methods to find the solution of system of equations. However, the Numpy library contains the
linalg.solve() method, which can be used to directly find the solution of a system of linear equations:
A = np.array([[4, 3, 2], [-2, 2, 3], [3, -5, 2]]) B = np.array([25, -10, -4]) X2 = np.linalg.solve(A,B) print(X2)
[ 5. 3. -2.]
You can see that the output is same as before.
A Real-World Example
Let's see how a system of linear equation can be used to solve real-world problems.
Suppose, a fruit-seller sold 20 mangoes and 10 oranges in one day for a total of $350. The next day he sold 17 mangoes and 22 oranges for $500. If the prices of the fruits remained unchanged on both the days, what was the price of one mango and one orange?
This problem can be easily solved with a system of two linear equations.
Let's say the price of one mango is
x and the price of one orange is
y. The above problem can be converted like this:
20x + 10y = 350 17x + 22y = 500
The solution for the above system of equations is shown here:
A = np.array([[20, 10], [17, 22]]) B = np.array([350, 500]) X = np.linalg.solve(A,B) print(X)
And here is the output:
The output shows that the price of one mango is $10 and the price of one orange is $15.
The article explains how to solve a system of linear equations using Python's Numpy library. You can either use
linalg.dot() methods in chain to solve a system of linear equations, or you can simply use the
solve() method. The
solve() method is the preferred way.