1.3: Computation with Sage
Linear algebra owes its prominence as a powerful scientific tool to the evergrowing 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 program called Sage that can help. While you may be able to do much of this work on a graphing calculator.
1.3.1 Introduction to Sage
There are several ways to access Sage but for this edition of Understanding Linear Algebra we will only point to one of them
-
You may go to
cocalc.com
, sign up for an account, open a new project, and create a “Sage worksheet.” Once inside the worksheet, you may enter commands as shown here, and evaluate them by pressing Enter on your keyboard while holding down the Shift key.
Here is a Sage cell containing a command that asks Sage to multiply 5 and 3. You may execute the command by pressing the Evaluate button.
None of the Sage shells in this edition are live, you must use cocalc.com or a computer where you have access to Sage
Throughout the text, we will introduce new Sage commands that allow us to explore linear algebraic concepts. These commands are collected and summarized in the reference found in Appendix A .
It is a remarkable fact that algebra, which is about equations and their solutions, and geometry are intimately connected. For instance, the solution set of a linear equation in two unknowns, such as \(2x + y = 1\text{,}\) can be represented graphically as a straight line. The aim of this section is to further this connection by introducing vectors, which will help us to apply geometric intuition to our thinking about linear systems.
In this page and the rest of the book we will not show the Sage cells but rather provide the entries in text that you can enter into your Sage cells
Activity 1.3.1. Basic Sage commands.
a. Sage uses the standard operators \(+,-, *, 1\), and \(\wedge\) for the usual arithmetic operations. By entering text in the cell, ask Sage to evaluate
\[3+4\left(2^4-1\right)\]
b. Notice that we can create new lines by pressing Enter and entering additional commands on them. Below we show the entry in plain text and then with a Sage cell image. What happens when you evaluate a Sage cell where you have entered
line 1 5 * 3
line 2 10 - 4
Notice that we only see the result from the last command. With the print command, we may see earlier results, if we wish.
line 1 print(5 * 3)
line 2 print(10 - 4
c. We may give a name to the result of one command and refer to it in a later command. (below we will simply use the line numbers and eliminate printing line)
1 income = 1500 * 12
2 taxes = income * 0.15
3 print(taxes)
Suppose you have three tests in your linear algebra class and your scores are 90,100, and 98. In the Sage 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.
d. If you are not a programmer, you may ignore this part. If you are an experienced programmer, however, you should know that Sage is written in the Python programming language and that you may enter Python code into a Sage cell.
1 for i in range (10)
2. print(i)
1.3.2 Sage and matrices
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 Sage, to find reduced row echelon matrices for us. We will learn how to do this now that we have a little familiarity with Sage.
First, notice that a matrix has a certain number of rows and columns. For instance, the matrix
\[
\left[\begin{array}{lllll}
* & * & * & * & * \\
* & * & * & * & * \\
* & * & * & * & *
\end{array}\right]
\]
has three rows and five columns. We consequently refer to this as a \(3 \times 5\) matrix.
We may ask Sage to create the \(2 \times 4\) matrix
\[
\left[\begin{array}{rrrr}
-1 & 0 & 2 & 7 \\
2 & 1 & -3 & -1
\end{array}\right]
\]
by entering
\[ 1 \quad \text { matrix(2, 4, }[-1,0,2,7,2,1,-3,-1]) \] Evaluate (Sage)
When evaluated, Sage will confirm the matrix by writing out the rows of the matrix, each inside square brackets.
Notice that there are three separate things (we call them arguments) inside the parentheses: the number of rows, the number of columns, and the entries of the matrix listed by row inside square brackets. These three arguments are separated by commas. Notice that there is no way of specifying whether this is an augmented or coefficient matrix so it will be up to us to interpret our results appropriately.
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, and
-
to omit the parentheses around the arguments after
matrix
.
If you see an error message, carefully proofread your input and try again.
Alternatively, you can create a matrix by simply listing its rows, like this
matrix([ [-1, 0, 2, 7], [ 2, 1,-3,-1] ])
Activity 1.3.2. Using Sage to find row reduced echelon matrices.
a. Enter the following matrix into Sage.
\[
\left[\begin{array}{rrrr}
-1 & -2 & 2 & -1 \\
2 & 4 & -1 & 5 \\
1 & 2 & 0 & 3
\end{array}\right]
\]
b. Give the matrix the name \(A\) by entering
A = matrix( ..., ..., [ ... ])
We may then find its reduced row echelon form by entering
A = matrix( ..., ..., [ ... ]) A.rref()
A common mistake is to forget the parentheses after rref.
Use Sage to find the reduced row echelon form of the matrix from Item a of this activity.
c. Use Sage to describe the solution space of the system of linear equations
\[
\begin{array}{rr}
-x_1 \quad 2 x_4 & =4 \\
3 x_2+x_3+2 x_4 & =3
\end{array}
\]
d. Consider the two matrices:
\[
\begin{array}{l}
A=\left[\begin{array}{rrrr}
1 & -2 & 1 & -3 \\
-2 & 4 & 1 & 1 \\
-4 & 8 & -1 & 7
\end{array}\right] \\
B=\left[\begin{array}{rrrrrr}
1 & -2 & 1 & -3 & 0 & 3 \\
-2 & 4 & 1 & 1 & 1 & -1 \\
-4 & 8 & -1 & 7 & 3 & 2
\end{array}\right] \\
\end{array}
\]
We say that \(B\) is an augmentation of \(A\) because it is obtained from \(A\) by adding some more columns.
Using Sage, define the matrices and compare their reduced row echelon forms. What do you notice about the relationship between the two reduced row echelon forms?
e. 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?
Here are some practices that you may find helpful when working with matrices in Sage.
-
Break the matrix entries across lines, one for each row, for better readability by pressing Enter between rows.
A = matrix(2, 4, [ 1, 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 = matrix(2, 2, [ 1, 2, 2, 2]) print (A) print ("---------") A.rref()
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\), which we augmented by adding some columns to obtain a matrix \(B\). We then noticed that the reduced row echelon form of \(B\) is itself an augmentation of the reduced row echelon form of \(A\).
To illustrate, we can consider the reduced row echelon form of the augmented matrix:
\[
\left[\begin{array}{ccc|c}
-2 & 3 & 0 & 2 \\
-1 & 4 & 1 & 3 \\
3 & 0 & 2 & 2 \\
1 & 5 & 3 & 7
\end{array}\right] \sim\left[\begin{array}{ccc|c}
1 & 0 & 0 & -4 \\
0 & 1 & 0 & -2 \\
0 & 0 & 1 & 7 \\
0 & 0 & 0 & 0
\end{array}\right]
\]
We can then determine the reduced row echelon form of the coefficient matrix by looking inside the augmented matrix.
\[
\left[\begin{array}{ccc}
-2 & 3 & 0 \\
-1 & 4 & 1 \\
3 & 0 & 2 \\
1 & 5 & 3
\end{array}\right] \sim\left[\begin{array}{lll}
1 & 0 & 0 \\
0 & 1 & 0 \\
0 & 0 & 1 \\
0 & 0 & 0
\end{array}\right]
\]
If we trace through the steps in the Gaussian elimination algorithm carefully, we see that this is a general principle, which we now state.
If matrix \(B\) is an augmentation of matrix \(A\), then the reduced row echelon form of \(B\) is an augmentation of the reduced row echelon form of \(A\).
1.3.3 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\). 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 \(2 n\).
Our goal is to transform a matrix to its reduced row echelon form, which looks something like this:
\[
\left[\begin{array}{cccc}
1 & 0 & \ldots & 0 \\
0 & 1 & \ldots & 0 \\
\vdots & \vdots & \ddots & 0 \\
0 & 0 & \ldots & 1
\end{array}\right] .
\]
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 \(2 n\) operations, the number of operations resulting from the needed replacements is roughly \(n^2(2 n)=2 n^3\).
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\).
Therefore, the total number of operations is roughly
\[
2 n^3+n^2
\]
When \(n\) is very large, the \(n^2\) term is much smaller than the \(n^3\) term. We therefore state that
The number of operations required to find the reduced row echelon form of an \(n \times n\) matrix is roughly proportional to \(n^3\).
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 \(\frac{2}{3} n^3\). 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\). 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\).
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 \(\left(10^{12}\right)^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}\), 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.
1.3.4 Summary
We learned some basic features of Sage with an emphasis on finding the reduced row echelon form of a matrix.
- Sage can perform basic arithmetic using standard operators. Sage can also save results from one command to be reused in a later command.
- We may define matrices in Sage and find the reduced row echelon form using the rref command.
- 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\).
Appendix A contains a reference outlining the Sage commands that we have encountered.
1.3.5 Exercises
1. Consider the linear system
\[
\begin{array}{rr}
x+2 y-z= & 1 \\
3 x+2 y+2 z & =7 \\
-x+4 z & =-3
\end{array}
\]
Write this system as an augmented matrix and use Sage 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.
a. Let's begin with the following traffic pattern.
i. 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, y, z\), and \(w\).
ii. Form three more linear equations from the other three intersections to form a linear system having four equations in four variables. Then use Sage to find the solution space to this system.
iii. Is there exactly one solution or infinitely many solutions? Explain why you would expect this given the information provided.
b. Another traffic pattern is shown below.
i. Once again, write a linear system for the quantities \(x, y, z\), and \(w\) and solve the system using the Sage cell below.
ii. 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.
iii. What is the smallest possible amount of traffic flowing through \(x\) ?
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,
\[
T_1=\left(10+15+T_2+T_4\right) / 4
\]
which we may rewrite as
\[
4 T_1-T_2-T_4=25
\]
Set up a linear system to find the temperature at these six points inside the plate. Then use Sage to solve the linear system. If all the entries of the matrix are integers, Sage will compute the reduced row echelon form using rational numbers. To view a decimal approximation of the results, you may use
A.rref().numerical_approx(digits=4)
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.
a. 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\). How many pounds of fuel did the first shift produce and how many did the second shift produce?
b. How much sulfur would you expect to find in the mixture?
5. This exercise is about balancing chemical reactions.
a. Chemists denote a molecule of water as \(\mathrm{H}_2 \mathrm{O}\), which means it is composed of two atoms of hydrogen \((\mathrm{H})\) and one atom of oxygen \((\mathrm{O})\). The process by which hydrogen burns is described by the chemical reaction
\[
x \mathrm{H}_2+y \mathrm{O}_2 \rightarrow z \mathrm{H}_2 \mathrm{O}
\]
This means that \(x\) molecules of hydrogen \(\mathrm{H}_2\) combine with \(y\) molecules of oxygen \(\mathrm{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, y\), and \(z\), how many hydrogen atoms are there before the reaction? How many hydrogen atoms are there after the reaction? Find a linear equation in \(x, y\), and \(z\) by equating these quantities.
2. Find a second linear equation in \(x, y\), 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, y\), and \(z\) should be positive integers. Find the solution where \(x, y\), and \(z\) are the smallest possible positive integers.
b. Now consider the reaction where potassium permanganate and manganese sulfate combine with water to produce manganese dioxide, potassium sulfate, and sulfuric acid:
\[
x_1 \mathrm{KMnO}_4+x_2 \mathrm{MnSO}_4+x_3 \mathrm{H}_2 \mathrm{O} \rightarrow x_4 \mathrm{MnO}_2+x_5 \mathrm{~K}_2 \mathrm{SO}_4+x_6 \mathrm{H}_2 \mathrm{SO}_4 \text {. }
\]
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
\[
\pi=3.141592653589793238462643383279502884197169399 \ldots
\]
would be approximated inside a computer by, say,
\[
\pi \approx 3.141592653589793
\]
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.
a. Let's first see an example showing that computer arithmetic really is an approximation. First, consider the linear system
\[
\begin{aligned}
x+\frac{1}{2} y+\frac{1}{3} z & =1 \\
\frac{1}{2} x+\frac{1}{3} y+\frac{1}{4} z & =0 \\
\frac{1}{3} x+\frac{1}{4} y+\frac{1}{5} z & =0
\end{aligned}
\]
If the coefficients are entered into Sage as fractions, Sage will find the exact reduced row echelon form. Find the exact solution to this linear system.
Now let's ask Sage 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{aligned}
x+0.5 y+\frac{1}{3} z & =1 \\
\frac{1}{2} x+\frac{1}{3} y+\frac{1}{4} z & =0 \\
\frac{1}{3} x+\frac{1}{4} y+\frac{1}{5} z & =0
\end{aligned}
\]
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.
R = RealNumber RealNumber = RealField(10) # enter the matrix below A = matrix( ..., ..., [ ... ] ) print (A.rref()) RealNumber = R
What does Sage give for the solution now? Compare this to the exact solution that you found previously.
b. 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{array}{l}
x+y=2 \\
x+1.001 y=2
\end{array}
\]
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{aligned}
x+y & =2 \\
x+1.001 y & =2.001
\end{aligned}
\]
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.