Section SAGE  SAGE: Open Source Mathematics Software

From A First Course in Linear Algebra
Version 2.20
© 2004.
Licensed under the GNU Free Documentation License.
http://linear.ups.edu/

Computation Note R.SAGE: Rings

Contributed by Steve Canfield
SAGE uses different rings to denote the type of an object. The rings are as follows:

 ZZ: The set of integers  QQ: The set of rational numbers  RR: The real numbers  CC: The complex numbers

Most objects in SAGE will tell you which they are using with the base_ring() command. Keep this in mind, especially when row reducing or factoring. Here’s a quick example of where you might go wrong.

m = matrix([[2, 3], [4, 7]]) m.base _ring() IntegerRing m.echelon _form() 20 0 1

As you can clearly see, m isn’t even in reduced row-echelon form. This is because m is defined over the ZZ. You have to create matrices with the correct ring or you will get this type of odd result. This problem comes up in more places than just calculating the reduced row-echelon form, so unless you are specifically working with integers take note.

Computation Note ME.SAGE: Matrix Entry

Contributed by Steve Canfield
A matrix in SAGE can be made a few ways. The first is simply to define the matrix as an array of rows. SAGE uses brackets (  ,  ) to delimit arrays. So the input

a = matrix([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])

would create a 3 × 4 matrix named a that is equal to

1 2 3 4 5 6 7 8 9 101112

SAGE will guess what type of matrix you are working with based on the inputs. If all the entries are integers, you will get back an integer matrix. If your matrix contains an entry in the or space, the matrix will be of those types. This can cause problems as integers cannot become fractions, which is an issue when calculating reduced row-echelon form. We therefore recommend using the following construction to make your matrices,

a = matrix(QQ, [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])

This gives you a matrix over the rational numbers which will be sufficient for most of the course. If your matrix has entries that are complex numbers you would replace the QQ with CC .
To display a matrix named a , type a , and the output will be displayed with rows and columns. If you type latex(a) you will get LATEX code to display the matrix. Very handy.

Computation Note RR.SAGE: Row Reduce

Contributed by Steve Canfield and Robert Beezer
Row-reducing a matrix is a simple operation in SAGE. However, because of Sage’s flexibility with different types of numbers (integers, rationals, reals, complexes), we need to be a bit more careful.

If a is a matrix entered in in SAGE (see Computation ME.SAGE) then a.echelon_form() will return a new matrix that is the reduced row-echelon form of a (Definition RREF).

If your matrix has only integer entries (as is the case with many examples and exercises in this book), then row operations might introduce rational numbers (“fractions”). So when you enter your matrix, you need to tell SAGE that rational numbers are allowable in its calculations. This is the advice in Computation R.SAGE to use the ring QQ . As an illustration create

a = matrix(QQ, [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])

and issue the command a.echelon_form() . The result is

1012 0 1 2 3 0 0 0 0

However, if we adjust the entry by neglecting to specify QQ , then SAGE assumes that we only want to work with integers, since every entry of the matrix is an integer. So as an experiment, enter

b = matrix([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])

and issue the command b.echelon_form() . The result is

123 4 0 4 8 12 0 00 0

You can now clearly see Sage’s reluctance to multiply row 2 by 1 4.

The ring QQ will of course suffice if your matrix has rational numbers for entries. Decimal entries are another place to be careful. If an entry of your matrix is the real number 2.17, you are free to enter it as the rational number 217 100 and keep the ring QQ in the specification of your matrix. If you want to consider your entries as real numbers, then you might as well just specify your ring as the complex numbers CC . This advice also applies if you have complex numbers as entries.

If you allow SAGE to work with real or complex numbers, then the problem of round-off error becomes relevant. Computer arithmetic with real numbers is, of necessity, subject to minor inaccuracies and errors. This becomes problematic when row-reducing a matrix. If a zero entry is computed instead as an extremely small number, such as 1.287 × 1018, then an incorrect sequence of row operations will follow (with further incorrect results). So if you use CC be on the lookout for these kinds of potential pitfalls.

So, in summary, remember to always specify the ring you will be using for your matrices, and most matrices can be handled with a choice of QQ or CC .

When you need to do significant scientific computing with SAGE, there are extra facilities that will help you work with these subtleties.

Finally, you can also use a command of the form a.echelonize() to replace a with its reduced row-echelon form.

Computation Note LS.SAGE: Linear Solve

SAGE can solve a variety of systems of equations with the solve( ) command, even when the equations are not linear (see Exercise SSLE.M70). But we can afford to specialize here to just linear systems. First, you must specify your variables in advance, so for example, var(’x1,x2,x3’) might precede a system with three equations. Equations are then written just as you might expect, except that equality is written as == , since computer programs have traditionally reserved = to assign values to variables. And remember to use a * to indicate that a coefficient multiplies a variable.

The example below illustrates the use of the command and the possibilities for results. Each system would be preceded by establishing the variables with the command var(’x,y’) . In the case of an infinite solution set, free variables are denoted as rx where x is an integer that increases throughout a session. The style of this description of a solution set is reminiscent of the style we used in Chapter SLE before we were accustomed to using linear combinations of vectors (Theorem VFSLS).

System
Solution Set
Result
solve([2*x+y==5, 3*x+2*y==15], x, y) Unique [[x == -5, y == 15]]
solve([2*x+y==5, 6*x+3*y==15], x, y) Infinite [[x == (5 - r1)/2, y == r1]]
solve([2*x+y==5, 6*x+3*y==10], x, y) Empty ValueError: Unable to solve

Notice how the output contains equations written a format that might be suitable as input for further use within SAGE.

Computation Note VLC.SAGE: Vector Linear Combinations

Contributed by Robert Beezer
Vectors in SAGE are constructed from lists, and are displayed horizontally. For example, the vector

v = 1 2 3 4

would be entered and named via the command

v = vector(QQ, [1,2,3,4])

See the notes about rings (Computation R.SAGE) and matrix entry (Computation ME.SAGE) for reminders about specifying the relevant ring.

Vector addition and scalar multiplication are then very natural. If u and v are two vectors of the same size, then

2 u + (3) v

will compute the correct vector. The result can be assigned to a variable (which will then contain a vector), or be printed. If printed, it will be written horizontally with parentheses for grouping. If u and v have different sizes, then SAGE will complain about “unsupported operand(s).”

Computation Note MI.SAGE: Matrix Inverse

Contributed by Steve Canfield
If a is a matrix defined in SAGE, then a.inverse() will return the inverse of a, should it exist. In the case where a does not have an inverse SAGE will tell you the matrix must be nonsingular (see Theorem NI).

Computation Note TM.SAGE: Transpose of a Matrix

Suppose a is the name of a matrix stored in SAGE. Then a.transpose() will return the transpose of a .

Computation Note E.SAGE: Eigenspaces

Contributed by Steve Canfield
SAGE can compute eigenspaces and eigenvalues for you. If you have a matrix named a and you type

a.eigenspaces()

you will get a listing of the eigenvalues and the eigenspace for each. Let’s do an example. Your output may be formatted slightly different from what we have here.

m = matrix(QQ, [[13,8,4], [12, 7, 4], [24, 16, 7]]) m.eigenspaces() [(3, [(1, 23, 13)]), (1, [(1, 0, 12), (0, 1,12)])]

Whew, that looks like a mess. At the top level, eigenspaces() returns a dictionary whose keys are the eigenvalues. So in this case we have eigenvalues 3 and -1. Each eigenvalue has an array after it that forms the basis of the eigenspace. In our example, there is 1 vector for λ = 3 and 2 vectors for λ = 1. Finally, the vectors SAGE spits out may not be the nicest ones to work with. In particular, we might want to scale the vectors to get rid of fractions.