Skip to main content
Logo image

Understanding Linear Algebra (with Python)

Section 1.3 Computation with Python

Linear algebra owes its prominence as a powerful scientific tool to the ever-growing power of computers. Carl Cowen, a former president of the Mathematical Association of America, has said, “No serious application of linear algebra happens without a computer.” Indeed, Cowen notes that, in the 1950s, working with a system of 100 equations in 100 variables was difficult. Today, scientists and mathematicians routinely work on problems that are vastly larger. This is only possible because of today’s computing power.
It is therefore important for any student of linear algebra to become comfortable solving linear algebraic problems on a computer. This section will introduce you to a programming language called Python that can help. While you may be able to do much of this work on a graphing calculator, you are encouraged to become comfortable with Python since we will use increasingly powerful features as we encounter their need.

Subsection 1.3.1 Introduction to Python

There are several ways to access Python.
  • If you are reading this book online, there will be embedded “Python cells” at appropriate places in the text. You have the opportunity to type Python commands into these cells and execute them, provided you are connected to the Internet. Please be aware that your work will be lost if you reload the page.
    Here is a Python cell containing a command that asks Python to multiply 5 and 3. You may execute the command by pressing the Evaluate button.
    Note that, within the online textbook environment, explicitly calling print() on output you’d like to see is required. In a notebook environment like Google Colab (see below), the output from the last line in a code cell will automatically be printed out if any output exists.
  • You may also go to Google Colab, either sign up or sign-in to your Google account, and create a “New Notebook.” For those familiar with Python, Colab Notebooks are Jupyter Notebooks which can be run from your web-browser, inside of your Google Drive. Once inside the notebook, you may enter commands into code cells as shown here, and evaluate them by pressing Enter on your keyboard while holding down the Shift key.
  • You may also elect to use an online Python interpreter rather than the Jupyter Notebook environment provided by Google Colab. There are many options for online Python interpreters, but one such option can be found at Online Python.
    If using Google Colab, any results obtained by evaluating one cell are available in other cells and your work may be saved as Jupyter Notebooks (*.ipynb files) to be revisited later. Another advantage to using Colab is the ability to mix text cells among your code cells, documenting and narrating your work. If you work with an online Python interpreter, however, your work will likely be lost when the page is reloaded.
Throughout the text, we will introduce new Python commands that allow us to explore linear algebraic concepts. These commands are collected and summarized in the reference found in Appendix A.

Activity 1.3.1. Basic Python commands.

  1. Python uses the standard operators +, -, *, / for the usual arithmetic operations. However, it uses ** for exponentiation. By entering text in the cell below, ask Python to evaluate
    \begin{equation*} 3 + 4(2^4 - 1) \end{equation*}
    Remember to call print() so that your result is displayed.
  2. Notice that we can create new lines in a Python cell by pressing Enter and writing additional commands on them. What happens when you evaluate this Python cell?
    Nothing! That’s because we forgot to explicitly call print(), but even in the Jupyter Notebook environment at Google Colab, only the result from the last command is printed out. By explicitly using the print() function, we may see results from additional lines, if we wish. Remember that, in our textbook environment, we’ll always need to call print() in order to display our results.
  3. We may give a name to the result of one command and refer to it in a later command.
    Suppose you have three tests in your linear algebra class and your scores are 90, 100, and 98. In the Python cell below, add your scores together and call the result total. On the next line, find the average of your test scores and print it.

Subsection 1.3.2 Importing and using Python modules

Python is a general purpose programming language. This means that Python is designed to do lots of things, not just mathematics -- you can build websites, design databases, construct and deploy machine learning models, and much more with Python.
To keep Python lightweight, not all of Python’s functionality is made available to you by default. You’ll need to import modules in order to complete many specialized tasks. For the most part, we’ll be making use of the {sympy} (SYmbolic Mathematics in PYthon) module since it has functionality for nearly everything we encounter in an introduction to Linear Algebra. It is also worth knowing, however, the existence of the {numpy} (NUMerical PYthon) module which handles numerical operations on vectors and matrices very well. Throughout the text we’ll use these curly-braces {} to indicate that we are referencing a full module rather than just a Python function/method. When importing modules, we can either load the entire module (ie. import sympy) or we can just load a single function from that module (ie. from numpy.linalg import inv).
With {sympy} imported, we can call and utilize functionality from that module. For example, {sympy} includes a Matrix() function which can be called using sympy.Matrix(), which tells Python to go to the {sympy} module and find the Matrix() function. Because we need to reference the module name when calling functionality from it, Python users often alias their module names when importing in order to reduce typing. We can see this below.

Subsection 1.3.3 Working with Matrices in Python

When we encounter a matrix, Theorem 1.2.6 tells us that there is exactly one reduced row echelon matrix that is row equivalent to it.
In fact, the uniqueness of this reduced row echelon matrix is what motivates us to define this particular form. When solving a system of linear equations using Gaussian elimination, there are other row equivalent matrices that reveal the structure of the solution space. The reduced row echelon matrix is simply a convenience as it is an agreement we make with one another to seek the same matrix.
An added benefit is that we can ask a computer program, like Python, to find reduced row echelon matrices for us. We will learn how to do this now that we have a little familiarity with Python.
First, notice that a matrix has a certain number of rows and columns. For instance, the matrix
\begin{equation*} \left[ \begin{array}{rrrrr} * \amp * \amp * \amp * \amp * \\ * \amp * \amp * \amp * \amp * \\ * \amp * \amp * \amp * \amp * \\ \end{array} \right] \end{equation*}
has three rows and five columns. We consequently refer to this as a \(3\times 5\) matrix.
We may ask Python to create the \(2\times4\) matrix
\begin{equation*} \left[ \begin{array}{rrrr} -1 \amp 0 \amp 2 \amp 7 \\ 2 \amp 1 \amp -3 \amp -1 \\ \end{array} \right] \end{equation*}
by entering
When evaluated, Python will confirm that we’ve defined the matrix by writing out its rows, each inside square brackets. Notice that there the entire matrix is defined as a list (indicated by the use of the square brackets []), and the elements of the list are also lists! These are the rows of the matrix. If we think about this, what we are declaring is quite intuitive -- a matrix is a list of rows. Items passed to a Python function are called arguments and the sp.Matrix() function takes just a single argument; the matrix in list form. Later, we’ll encounter functions taking multiple arguments. In these cases, we’ll separate the arguments by commas.
A very important thing to notice about our matrix definition above is that we wrote the top-left element of the matrix as -1.0 rather than -1. This is to clarify to Python that we expect to be working within the realm of real numbers (approximated in computers using floats). While this isn’t a requirement for working with {sympy} matrices, it is a requirement for {numpy}. Without explicitly indicating that decimal values are permissible, Python would store the matrix as an integer array/matrix, which would damage our calculations, resuling in incorrect conclusions. This is something we’ll need to pay careful attention to! Because the difference between float and integer arrays is so important to remember when working with {numpy}, I’ll continue to explicitly identify that our matrices contain floats even when working with {sympy} -- this way you won’t forget if and when you switch over to using {numpy} for something.
As a reminder, if we intend to just do numeric computatons like adding, subtracting, multiplying, finding inverses or determinants, etc. with our matrices, then the {numpy} module has functionality that suits these needs perfectly. If, however, we want to perform manipulations like row-reduction, then we’ll need to use {sympy} instead. Since {sympy} includes all of the functionality we’ll need (including for those arithmetic operations), we’ll stick to the {sympy} module throughout the text, for simplicity’s sake. However, the Python cell below shows how you can define a matrix using {numpy}.
Regardless of whether we are using {numpy} or {sympy}, there is no way of specifying whether our matrix is an augmented or coefficient matrix so it will be up to us to use our objects and interpret our results appropriately.
One additional item worth pointing out is the difference between functions and methods in Python. Having completed previous math coursework, you are familiar with the notion of functions and their arguments. For example, if \(f(x) = x^2 - 5x\text{,}\) then the expression \(f(5)\) asks us to use \(5\) as an input (argument) for the function \(f\) and then to evaluate it. In Python, some types of objects have special functions, called methods, which can be applied directly to them. In this case, the format for calling the method on the object is object.method(). For example, we defined the matrix A above as a {sympy} matrix. These matrices have a .rref() method which can be applied to them in order to transform the matrix into its reduced row-echelon form. We apply that method to our matrix below to illustrate this point.

Python syntax.

Some common mistakes are
  • to forget the square brackets around the list of entries,
  • to omit an entry from the list or to add an extra one,
  • to forget to separate the rows, columns, and entries by commas,
  • to omit the parentheses around the arguments after np.array() or sympy.Matrix(),
  • spelling or capitalization errors like using a capital “A” in np.array or a lowercase “m” in sympy.Matrix(),
  • forgetting to include the function namespace, for example using Matrix() instead of sp.Matrix(), and
  • attempting to apply a function like a method and vice-versa.
If you see an error message, carefully proofread your input and try again.

Activity 1.3.2. Using Python to find row reduced echelon matrices.

  1. Enter the following matrix into Python using {sympy} and sp.Matrix().
    \begin{equation*} \left[ \begin{array}{rrrr} -1 \amp -2 \amp 2 \amp -1 \\ 2 \amp 4 \amp -1 \amp 5 \\ 1 \amp 2 \amp 0 \amp 3 \end{array} \right] \end{equation*}
  2. Don’t forget to import the library/libraries you want to use, and give the matrix the name \(A\) by entering
    			import sympy
    			A = sympy.Matrix([ ... ])
    	      
    We may then find its reduced row echelon form by entering
    			A_rref = A.rref()
    			A_rref
    	      
    A common mistake is to forget the parentheses after .rref.
    Use Python to find the reduced row echelon form of the matrix from Item a of this activity.
  3. Use Python to help you describe the solution space of the system of linear equations
    \begin{equation*} \begin{alignedat}{5} -x_1 \amp \amp \amp \amp \amp {}+{} \amp 2x_4 \amp {}={} \amp 4 \\ \amp \amp 3x_2 \amp {}+{} \amp x_3 \amp {}+{} \amp 2x_4 \amp {}={} \amp 3 \\ 4x_1 \amp {}-{} \amp 3x_2 \amp \amp \amp {}+{} \amp x_4 \amp {}={} \amp 14 \\ \amp \amp 2x_2 \amp {}+{} \amp 2x_3 \amp {}+{} \amp x_4 \amp {}={} \amp 1 \\ \end{alignedat} \end{equation*}
  4. Consider the two matrices:
    \begin{equation*} \begin{array}{rcl} A \amp = \amp \left[ \begin{array}{rrrr} 1 \amp -2 \amp 1 \amp -3 \\ -2 \amp 4 \amp 1 \amp 1 \\ -4 \amp 8 \amp -1 \amp 7 \\ \end{array}\right] \\ B \amp = \amp \left[ \begin{array}{rrrrrr} 1 \amp -2 \amp 1 \amp -3 \amp 0 \amp 3 \\ -2 \amp 4 \amp 1 \amp 1 \amp 1 \amp -1 \\ -4 \amp 8 \amp -1 \amp 7 \amp 3 \amp 2 \\ \end{array}\right] \\ \end{array} \end{equation*}
    We say that \(B\) is an augmentation of \(A\) because it is obtained from \(A\) by adding some more columns.
    Using Python, define the matrices and compare their reduced row echelon forms. What do you notice about the relationship between the two reduced row echelon forms?
  5. Using the system of equations in Item c, write the augmented matrix corresponding to the system of equations. What did you find for the reduced row echelon form of the augmented matrix?
    Now write the coefficient matrix of this system of equations. What does Item d of this activity tell you about its reduced row echelon form?

Python practices.

Here are some practices that you may find helpful when working with matrices in Python.
  • Break the matrix entries across lines, one for each row, for better readability by pressing Enter between rows.
    #A = np.array([[1.0, 2, -1, 0],
    #    			[-3, 0,  4, 3 ]])
    A = sp.Matrix([[1.0, 2, -1, 0],
    			[-3, 0,  4, 3 ]])
    	
  • Print your original matrix to check that you have entered it correctly. You may want to also print a dividing line to separate matrices.
    #A = np.array([[ 1.0, 2],
    #                   [2, 2]])
    A = sp.Matrix([[ 1.0, 2],
                [2, 2]])
    print(A)
    print("---------")
    A_reduced = A.rref()
    print(A_reduced)
    	
The last part of the previous activity, Item d, demonstrates something that will be helpful for us in the future. In that activity, we started with a matrix \(A\text{,}\) which we augmented by adding some columns to obtain a matrix \(B\text{.}\) We then noticed that the reduced row echelon form of \(B\) is itself an augmentation of the reduced row echelon form of \(A\text{.}\)
To illustrate, we can consider the reduced row echelon form of the augmented matrix:
\begin{equation*} \left[ \begin{array}{ccc|c} -2 \amp 3 \amp 0 \amp 2 \\ -1 \amp 4 \amp 1 \amp 3 \\ 3 \amp 0 \amp 2 \amp 2 \\ 1 \amp 5 \amp 3 \amp 7 \\ \end{array} \right] \sim \left[ \begin{array}{ccc|c} 1 \amp 0 \amp 0 \amp -4 \\ 0 \amp 1 \amp 0 \amp -2 \\ 0 \amp 0 \amp 1 \amp 7 \\ 0 \amp 0 \amp 0 \amp 0 \\ \end{array} \right] \end{equation*}
We can then determine the reduced row echelon form of the coefficient matrix by looking inside the augmented matrix.
\begin{equation*} \left[ \begin{array}{ccc} -2 \amp 3 \amp 0 \\ -1 \amp 4 \amp 1 \\ 3 \amp 0 \amp 2 \\ 1 \amp 5 \amp 3 \\ \end{array} \right] \sim \left[ \begin{array}{ccc} 1 \amp 0 \amp 0 \\ 0 \amp 1 \amp 0 \\ 0 \amp 0 \amp 1 \\ 0 \amp 0 \amp 0 \\ \end{array} \right] \end{equation*}
If we trace through the steps in the Gaussian elimination algorithm carefully, we see that this is a general principle, which we now state.

Subsection 1.3.4 Computational effort

At the beginning of this section, we indicated that linear algebra has become more prominent as computers have grown more powerful. Computers, however, still have limits. Let’s consider how much effort is expended when we ask to find the reduced row echelon form of a matrix. We will measure, very roughly, the effort by the number of times the algorithm requires us to multiply or add two numbers.
We will assume that our matrix has the same number of rows as columns, which we call \(n\text{.}\) We are mainly interested in the case when \(n\) is very large, which is when we need to worry about how much effort is required.
Let’s first consider the effort required for each of our row operations.
  • Scaling a row multiplies each of the \(n\) entries in a row by some number, which requires \(n\) operations.
  • Interchanging two rows requires no multiplications or additions so we won’t worry about the effort required by an interchange.
  • A replacement requires us to multiply each entry in a row by some number, which takes \(n\) operations, and then add the resulting entries to another row, which requires another \(n\) operations. The total number of operations is \(2n\text{.}\)
Our goal is to transform a matrix to its reduced row echelon form, which looks something like this:
\begin{equation*} \left[ \begin{array}{cccc} 1 \amp 0 \amp \ldots \amp 0 \\ 0 \amp 1 \amp \ldots \amp 0 \\ \vdots \amp \vdots \amp \ddots \amp 0 \\ 0 \amp 0 \amp \ldots \amp 1 \\ \end{array} \right]\text{.} \end{equation*}
We roughly perform one replacement operation for every 0 entry in the reduced row echelon matrix. When \(n\) is very large, most of the \(n^2\) entries in the reduced row echelon form are 0 so we need roughly \(n^2\) replacements. Since each replacement operation requires \(2n\) operations, the number of operations resulting from the needed replacements is roughly \(n^2(2n) = 2n^3\text{.}\)
Each row is scaled roughly one time so there are roughly \(n\) scaling operations, each of which requires \(n\) operations. The number of operations due to scaling is roughly \(n^2\text{.}\)
Therefore, the total number of operations is roughly
\begin{equation*} 2n^3 + n^2\text{.} \end{equation*}
When \(n\) is very large, the \(n^2\) term is much smaller than the \(n^3\) term. We therefore state that

Observation 1.3.2.

The number of operations required to find the reduced row echelon form of an \(n\times n\) matrix is roughly proportional to \(n^3\text{.}\)
This is a very rough measure of the effort required to find the reduced row echelon form; a more careful accounting shows that the number of arithmetic operations is roughly \(\frac23 n^3\text{.}\) As we have seen, some matrices require more effort than others, but the upshot of this observation is that the effort is proportional to \(n^3\text{.}\) We can think of this in the following way: If the size of the matrix grows by a factor of 10, then the effort required grows by a factor of \(10^3 = 1000\text{.}\)
While today’s computers are powerful, they cannot handle every problem we might ask of them. Eventually, we would like to be able to consider matrices that have \(n=10^{12}\) (a trillion) rows and columns. In very broad terms, the effort required to find the reduced row echelon matrix will require roughly \((10^{12})^3 = 10^{36}\) operations.
To put this into context, imagine we need to solve a linear system with a trillion equations and a trillion variables and that we have a computer that can perform a trillion, \(10^{12}\text{,}\) operations every second. Finding the reduced row echelon form would take about \(10^{16}\) years. At this time, the universe is estimated to be approximately \(10^{10}\) years old. If we started the calculation when the universe was born, we’d be about one-millionth of the way through.
This may seem like an absurd situation, but we’ll see in Subsection 4.5.3 how we use the results of such a computation every day. Clearly, we will need some better tools to deal with really big problems like this one.

Subsection 1.3.5 Summary

We learned some basic features of Python with an emphasis on finding the reduced row echelon form of a matrix.
  • Python can perform basic arithmetic using standard operators. Python can also save results from one command to be reused in a later command.
  • In order to do specialized analyses (such as working with matrices), we’ll need to extend Python’s base functionality with modules like {numpy} and {sympy}. We load these modules with the command import MODULE_NAME.
  • We may define matrices in Python using either sp.Matrix() (or np.array(), and can find the reduced row echelon form of a {sympy} matrix using the .rref() method. Note that .rref() will not work with a {numpy} array.
  • We saw an example of the Augmentation Principle, which we then stated as a general principle.
  • We saw that the computational effort required to find the reduced row echelon form of an \(n\times n\) matrix is proportional to \(n^3\text{.}\)
Appendix A contains a reference outlining the Python commands that we have encountered.

Exercises 1.3.6 Exercises

1.

Consider the linear system
\begin{equation*} \begin{alignedat}{4} x \amp {}+{} \amp 2y \amp {}-{} \amp z \amp {}={} \amp 1 \\ 3x \amp {}+{} \amp 2y \amp {}+{} \amp 2z \amp {}={} \amp 7 \\ -x \amp \amp \amp {}+{} \amp 4z \amp {}={} \amp -3 \\ \end{alignedat} \end{equation*}
Write this system as an augmented matrix and use Python to find a description of the solution space.

2.

Shown below are some traffic patterns in the downtown area of a large city. The figures give the number of cars per hour traveling along each road. Any car that drives into an intersection must also leave the intersection. This means that the number of cars entering an intersection in an hour is equal to the number of cars leaving the intersection.
  1. Let’s begin with the following traffic pattern.
    1. How many cars per hour enter the upper left intersection? How many cars per hour leave this intersection? Use this to form a linear equation in the variables \(x\text{,}\) \(y\text{,}\) \(z\text{,}\) and \(w\text{.}\)
    2. Form three more linear equations from the other three intersections to form a linear system having four equations in four variables. Then use Python to find the solution space to this system.
    3. Is there exactly one solution or infinitely many solutions? Explain why you would expect this given the information provided.
  2. Another traffic pattern is shown below.
    1. Once again, write a linear system for the quantities \(x\text{,}\) \(y\text{,}\) \(z\text{,}\) and \(w\) and solve the system using the Python cell below.
    2. What can you say about the solution of this linear system? Is there exactly one solution or infinitely many solutions? Explain why you would expect this given the information provided.
    3. What is the smallest possible amount of traffic flowing through \(x\text{?}\)

3.

A typical problem in thermodynamics is to find the steady-state temperature distribution inside a thin plate if we know the temperature around the boundary. Let \(T_1, T_2, \ldots, T_6\) be the temperatures at the six nodes inside the plate as shown below.
The temperature at a node is approximately the average of the four nearest nodes: for instance,
\begin{equation*} T_1 = (10 + 15 + T_2 + T_4)/4\text{,} \end{equation*}
which we may rewrite as
\begin{equation*} 4T_1 - T_2 - T_4 = 25\text{.} \end{equation*}
Set up a linear system to find the temperature at these six points inside the plate. Then use Python to solve the linear system. By default {sympy} will try to produce an exact solution, displaying entries as fractions. The closely focused reader will notice that the output from .rref() consists of two components -- first, the matrix in reduced row echelon form and, second, a tuple identifying the pivot columns of the matrix. To view a decimal approximation of the results we can first extract the matrix component of the output (which is in the 0-th position), and then we can use the .evalf() method with the number of digits of precision we’d like to retain as its sole argument.
		A.rref()[0].evalf(5)
	
In the real world, the approximation becomes better as we add more and more points into the grid. This is a situation where we may want to solve a linear system having millions of equations and millions of variables.

4.

The fuel inside model rocket motors is a black powder mixture that ideally consists of 60% charcoal, 30% potassium nitrate, and 10% sulfur by weight.
Suppose you work at a company that makes model rocket motors. When you come into work one morning, you learn that yesterday’s first shift made a perfect batch of fuel. The second shift, however, misread the recipe and used 50% charcoal, 20% potassium nitrate and 30% sulfur. Then the two batches were mixed together. A chemical analysis shows that there are 100.3 pounds of charcoal in the mixture and 46.9 pounds of potassium nitrate.
  1. Assuming the first shift produced \(x\) pounds of fuel and the second \(y\) pounds, set up a linear system in terms of \(x\) and \(y\text{.}\) How many pounds of fuel did the first shift produce and how many did the second shift produce?
  2. How much sulfur would you expect to find in the mixture?

5.

This exercise is about balancing chemical reactions.
  1. Chemists denote a molecule of water as \(\text{H}_2\text{O}\text{,}\) which means it is composed of two atoms of hydrogen (H) and one atom of oxygen (O). The process by which hydrogen burns is described by the chemical reaction
    \begin{equation*} x\thinspace \text{H}_2 + y\thinspace\text{O}_2 \to z\thinspace \text{H}_2\text{O} \end{equation*}
    This means that \(x\) molecules of hydrogen \(\text{H}_2\) combine with \(y\) molecules of oxygen \(\text{O}_2\) to produce \(z\) water molecules. The number of hydrogen atoms is the same before and after the reaction; the same is true of the oxygen atoms.
    1. In terms of \(x\text{,}\) \(y\text{,}\) and \(z\text{,}\) how many hydrogen atoms are there before the reaction? How many hydrogen atoms are there after the reaction? Find a linear equation in \(x\text{,}\) \(y\text{,}\) and \(z\) by equating these quantities.
    2. Find a second linear equation in \(x\text{,}\) \(y\text{,}\) and \(z\) by equating the number of oxygen atoms before and after the reaction.
    3. Find the solutions of this linear system. Why are there infinitely many solutions?
    4. In this chemical setting, \(x\text{,}\) \(y\text{,}\) and \(z\) should be positive integers. Find the solution where \(x\text{,}\) \(y\text{,}\) and \(z\) are the smallest possible positive integers.
  2. Now consider the reaction where potassium permanganate and manganese sulfate combine with water to produce manganese dioxide, potassium sulfate, and sulfuric acid:
    \begin{equation*} x_1\thinspace \text{K}\text{Mn}\text{O}_4 + x_2\thinspace \text{Mn}\text{S}\text{O}_4 + x_3\thinspace \text{H}_2\text{O} \to x_4\thinspace \text{Mn}\text{O}_2 + x_5\thinspace \text{K}_2\text{S}\text{O}_4 + x_6\thinspace \text{H}_2\text{S}\text{O}_4. \end{equation*}
    As in the previous exercise, find the appropriate values for \(x_1, x_2, \ldots, x_6\) to balance the chemical reaction.

6.

We began this section by stating that increasing computational power has helped linear algebra assume a prominent role as a scientific tool. Later, we looked at one computational limitation: once a matrix gets to be too big, it is not reasonable to apply Gaussian elimination to find its reduced row echelon form.
In this exercise, we will see another limitation: computer arithmetic with real numbers is only an approximation because computers represent real numbers with only a finite number of bits. For instance, the number pi
\begin{equation*} \pi = 3.141592653589793238462643383279502884197169399\ldots \end{equation*}
would be approximated inside a computer by, say,
\begin{equation*} \pi\approx 3.141592653589793 \end{equation*}
Most of the time, this is not a problem. However, when we perform millions or even billions of arithmetic operations, the error in these approximations starts to accumulate and can lead to results that are wildly inaccurate. Here are two examples demonstrating this.
  1. Let’s first see an example showing that computer arithmetic really is an approximation. First, consider the linear system
    \begin{equation*} \begin{alignedat}{4} x \amp {}+{} \amp \frac12y \amp {}+{} \amp \frac13z \amp {}={} \amp 1 \\ \frac12x \amp {}+{} \amp \frac13y \amp {}+{} \amp \frac14z \amp {}={} \amp 0 \\ \frac13x \amp {}+{} \amp \frac14y \amp {}+{} \amp \frac15z \amp {}={} \amp 0 \\ \end{alignedat} \end{equation*}
    As we mentioned, by default, the .rref() method from Python’s {sympy} module will, try to find the exact reduced row echelon form. Find the exact solution to this linear system.
    Now let’s ask Python to compute with real numbers. We can do this by representing one of the coefficients as a decimal. For instance, the same linear system can be represented as
    \begin{equation*} \begin{alignedat}{4} x \amp {}+{} \amp 0.5y \amp {}+{} \amp \frac13z \amp {}={} \amp 1 \\ \frac12x \amp {}+{} \amp \frac13y \amp {}+{} \amp \frac14z \amp {}={} \amp 0 \\ \frac13x \amp {}+{} \amp \frac14y \amp {}+{} \amp \frac15z \amp {}={} \amp 0 \\ \end{alignedat} \end{equation*}
    Most computers do arithmetic using either 32 or 64 bits. To magnify the problem so that we can see it better, we will ask Sage to do arithmetic using only 10 bits as follows.
    What does Sage give for the solution now? Compare this to the exact solution that you found previously.
  2. Some types of linear systems are particularly sensitive to errors resulting from computers’ approximate arithmetic. For instance, suppose we are interested in the linear system
    \begin{equation*} \begin{alignedat}{3} x \amp {}+{} \amp y \amp {}={} \amp 2 \\ x \amp {}+{} 1.001\amp y \amp {}={} \amp 2 \\ \end{alignedat} \end{equation*}
    Find the solution to this linear system.
    Suppose now that the computer has accumulated some error in one of the entries of this system so that it incorrectly stores the system as
    \begin{equation*} \begin{alignedat}{3} x \amp {}+{} \amp y \amp {}={} \amp 2 \\ x \amp {}+{} 1.001\amp y \amp {}={} \amp 2.001 \\ \end{alignedat} \end{equation*}
    Find the solution to this linear system.
    Notice how a small error in one of the entries in the linear system leads to a solution that has a dramatically large error. Fortunately, this is an issue that has been well studied, and there are techniques that mitigate this type of behavior.