MAT232 Introduction to Sage

2130 days ago by jonathan.senning

Welcome to the MAT232 Introduction to Sage worksheet.  This tutorial will guide you through the basic operations necessary to use Sage to handle basic matrix operations.  Sage is a powerful tool; we'll only just begin to see its capabilities here.  If you are interested you can learn much more about its capabilities at http://www.sagemath.org.

We'll begin by learning how to interact with the worksheet interface.  It's quite straightforward with the exception that to evaluate a command you must either press "shift-enter" or click the "evaluate" link below the command entry cell.  Pressing "enter" alone will merely create a new line in the command entry cell.

Click on the cell below, hold the shift key down, and press the enter key.

a=1 
       

You may be surprised that nothing appeared to happen.  In fact, the value 1 was assigned to the variable a, but this result was not echoed.  Repeat on the cell below; click on the cell, then use "shift-enter" or click on "evaluate".

b=2; b 
       

This time the value 2 was assigned to b and then the value of b was echoed.  The semicolon is used to separate commands appearing on the same line.  The same thing could be done by typing "b=2", pressing enter, typing "b" and then using "shift-enter".

b=2 b 
       

Feel free to use whichever of these approaches is most comfortable to you.

Now, let's enter a matrix.  The function matrix() is used to do this.  For now the first argument to the function should be "QQ", this means that the matrix will contain either integers or rational numbers (fractions).  The second argument is the matrix data.

M=matrix(QQ,[[2,4,0,8],[-1,3,3,-2],[0,1,1,0]]); M 
       

The matrix M is defined with three rows and four columns.  Notice how the matrix was specified row-by-row, with each row inside a pair of square brackets and all three rows enclosed in another set of square brackets; commas are used to separate matrix entries and rows.

Perhaps the most confusing thing about using Sage for matrix work is that rows and columns are numbered starting at 0 rather than 1 as is usually done for matrices in mathematical work.  This means that the rows in M are numbered 0, 1, and 2 while the columns are numbered 0, 1, 2, and 3.  For example, we can access the first and second rows with

M.row(0); M.row(1) 
       

Notice how the function row() is used; it is "attached" to the matrix varible with a dot.  This means that the row function operates on the matrix M.

Suppose M is the augmented matrix of a linear system.  We can solve the linear system by performing elementary row operations on M.  In Sage these row operations are implemented with the functions

  • swap_rows() - interchange two rows
  • rescale_row() - scale a row by using a scale factor
  • add_multiple_of_row() - add a multiple of one row to another row, replacing the row

Remember, row numbering starts at 0.  Pay careful attention to the changes made to the matrix by each of the following commands.

We want to start with a 1 in the first position of the first column, so we begin by scaling the first row by 1/2.

M.rescale_row(0,1/2); M 
       

The first argument to rescale_row() is the index of the row to be scaled and the second argument is the scale factor.  We could, of course, use 0.5 rather than 1/2 for the scale factor.

Now that there is a 1 in the first position of the first column we continue by eliminating the entry below it.

M.add_multiple_of_row(1,0,1); M 
       

The first argument is the index of the row to be replaced, the second argument is the row to form a multiple of, and the final argument is the scale factor.  Thus M.add_multiple_of_row(n,m,a) would replace row n with (row n)+a*(row m).

Since the last entry in the first column is already zero we can move on to the second column.  Our first step is to get a 1 in the second position of the second column.  Normally we would do this by scaling the row but in this case we can swap the second and third rows.

M.swap_rows(1,2); M 
       

The arguments to swap_rows() are fairly obvious, just remember that row 1 is the second row and row 2 is the third row.

Now we want to eliminate the 5 below the 1.  This is done by multiplying the second row by $-5$ and adding it to the third row.

M.add_multiple_of_row(2,1,-5); M 
       

To get a 1 as the last entry in the third column we can scale the third row by $-1/2$.

M.rescale_row(2,-1/2); M 
       

At this point the matrix is in echelon form (well, having the 1's down the diagonal of the matrix is not required, but it does make our work easier).  All that remains to find the solution is to put the matrix in reduced echelon form which requires that we replace all entries in the first three columns that are not on the main diagonal (where the 1's are) with zeros.  We will start with the third column and work our way up and to the left.  Remember that this is an augmented matrix and we are going to ignore the right-most column; it just "goes along for the ride."

M.add_multiple_of_row(1,2,-1); M 
       
M.add_multiple_of_row(0,1,-2); M 
       

At this point our solution is complete.  We see that the solution to our linear system is $x_1=2$, $x_2=1$, and $x_3=-1$.

There is an easy way to check your work, or to carry out these steps in the future.  First, let's reload the matrix M.

M=matrix(QQ,[[2,4,0,8],[-1,3,3,-2],[0,1,1,0]]); M 
       

The function echelon_form() will display the echelon form of a matrix without changing the matrix.

M.echelon_form() 
       

Notice that the matrix M is unchanged.

       

To replace M with its reduced echelon form, use the echelonize() function.

M.echelonize(); M 
       

This brings us to the end of this demonstration.