# NumPy: Solve a System of Linear Equations

We first consider a system of linear equations in two variables $x$ and $y$.

We pick an example from the classic Hall & Knight's text Elementary Algebra1.

$$3x + 7y = 27 \\ 5x + 2y = 16$$

This can be put in the matrix dot product form as

$$\begin{bmatrix} 3 & 7 \\ 5 & 2 \\ \end{bmatrix} \cdot \begin{bmatrix} x \\ y \\ \end{bmatrix} = \begin{bmatrix} 27 \\ 16 \\ \end{bmatrix}$$

If $A$ represents the matrix of coefficients, $x$ the column vector of variables and $B$ the column vector of solutions, the above equation can be shortened to

$$Ax = B$$

The above matrix product will be defined if and only if the number of columns in the coefficient matrix $A$ is equal to the number of rows in the variable matrix $x$.

From school, most of us are familiar with solving such set of linear equations using Cramer's Rule, which involves determinants.

Python's numerical library NumPy has a function numpy.linalg.solve() which solves a linear matrix equation, or system of linear scalar equation.

Here we find the solution to the above set of equations in Python using NumPy's numpy.linalg.solve() function.

				
import numpy as np

a = np.array([[3,7], [5,2]])
b = np.array([27,16])
x = np.linalg.solve(a, b)

print(x)



If the above Python script is executed, we will get the solutions in the column matrix format as

				
[2. 3.]



representing the $x$ and $y$ values respectively. So, $x = 2$ and $y = 3$.

Also you can use the numpy.allclose() function to check if the solution is correct. The following should return True.

				
np.allclose(np.dot(a, x), b)



# Simultaneous Equations in Three Variables

Next we consider simultaneous equations in three unknowns or variables $x$ , $y$ and $z$. We pick another example from the same book Elementary Algebra2 by Hall & Knight.

$$6x + 2y - 5z = 13 \\ 3x + 3y - 2z = 13 \\ 7x + 5y - 3z = 26$$

We represent the above equations in the matrix form

$$\begin{bmatrix} 6 & 2 & -5 \\ 3 & 3 & -2 \\ 7 & 5 & -3 \\ \end{bmatrix} \cdot \begin{bmatrix} x \\ y \\ z \\ \end{bmatrix} = \begin{bmatrix} 13 \\ 13 \\ 26 \\ \end{bmatrix}$$

We solve it with NumPy's numpy.linalg.solve() function

					
import numpy as np

a = np.array([[6,2,-5], [3,3,-2], [7,5,-3]])
b = np.array([13,13,26])
x = np.linalg.solve(a, b)

print(x)



On executing the script, the solution is printed as a column matrix, representing the values for $x$ , $y$ and $z$ respectively.

					
[2. 3. 1.]



## Notes

• 1) H. S. Hall & S. R. Knight, Elementary Algebra. London: Macmillan & Co., Ltd., 1896. Chapter XVII: Simultaneous Equations, p. 148, Ex. 2.
• 2) Ibid., p152