Section SAGE  SAGE: Open Source Mathematics Software

From A First Course in Linear Algebra
Version 2.00
© 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 constructor 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 you are dealing with 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. Finally, SAGE will build an identity matrix for you with

im = identity _matrix(QQ, 3)

which yields

100 0 10 0 01

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.

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.