Introduction to the Numpy.linalg sub-library
This
In this lab, you will keep developing your skills using Python to solve systems of linear equations by using the sub-libraby numpy.linalg. In this notebook you will:
- Use
NumPy
linear algebra package to find the solutions to the system of linear equations - Evaluate the determinant of the matrix to see again the connection between matrix singularity and the number of solutions of the linear system
- Explore the numpy.linalg sub-library to get to know its properties
Packages
Load the NumPy
package to access its functions.
Representing and Solving a System of Linear Equations using Matrices
System of Linear Equations
Here is a system of linear equations (or linear system) with three equations and three unknown variables:
To solve this system of linear equations means to find such values of the variables , , , that all of its equations are simultaneously satisfied.
Solving Systems of Linear Equations using Matrices
Let's prepare to solve the linear system using NumPy
. will be a matrix, each row will represent one equation in the system and each column will correspond to the variable , , . And is a 1-D array of the free (right side) coefficients:
Check the dimensions of and using shape()
function:
Now use np.linalg.solve(A, b)
function to find the solution of the system . The result will be saved in the 1-D array . The elements will correspond to the values of , and :
Try to substitute those values of , and into the original system of equations to check its consistency.
Evaluating the Determinant of a Matrix
Matrix corresponding to the linear system is a square matrix - it has the same number of rows and columns. In the case of a square matrix it is possible to calculate its determinant - a real number that characterizes some properties of the matrix. A linear system containing three equations with three unknown variables will have one solution if and only if the matrix has a non-zero determinant.
Let's calculate the determinant using np.linalg.det(A)
function:
Please, note that its value is non-zero, as expected.
What happens if the system has no unique solution?
Let's explore what happens if we use np.linalg.solve
in a system with no unique solution (no solution at all or infinitely many solutions).
Given another system of linear equations:
Let's find the determinant of the corresponding matrix.
As you can see, it throws a LinAlgError
because the matrix is singular. You can check it by yourself by computing the determinant of the matrix:
The sub-library np.linalg has several linear algebra functions to help linear algebra tasks and we have exhausted so far the functions you have learned in class.
Once you learn more theory, the functions in this library will become clearer. You also will be using them in the assignments in week 3 and 4, but do not worry because you will be guided through them.
Conclusion
Well done! You used the NumPy
functions to solve a system of equations. As expected, using a predefined function is much easier, but gives much less insight into what is happening under the hood. This is why the next assignment will be in Gaussian Elimination, a method to solve linear systems. Remember that np.linalg.solve
gives an error if there are no or infinitely many solutions, thus when implementing it you will have to think carefully so not to make your program crash!