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.

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:

\eqalignno{ &\text{ZZ: The set of integers} & & \cr &\text{QQ: The set of rational numbers} & & \cr &\text{RR: The real numbers} & & \cr &\text{CC: The complex numbers} & & \cr & & }

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.

\eqalignno{ &m = matrix([[2, 3], [4, 7]]) & & \cr &m.base\text{_}ring() & & \cr &IntegerRing & & \cr &m.echelon\text{_}form() & & \cr &\left [\array{ 2&0 \cr 0&1 } \right ] & & }

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 (\left [\right . , \left .\right ]) 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

\left [\array{ 1& 2 & 3 & 4 \cr 5& 6 & 7 & 8 \cr 9&10&11&12 } \right ]

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\text{_}matrix(QQ, 3)

which yields

\left [\array{ 1&0&0 \cr 0&1&0 \cr 0&0&1} \right ]

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


you will get a listing of the eigenvalues and the eigenspace for each. Let’s do an example.

\eqalignno{ &m = matrix(QQ, [[−13,−8,−4], [12, 7, 4], [24, 16, 7]]) & & \cr &m.eigenspaces() & & \cr &[ & & \cr &(3, [ & & \cr &(1, 2∕3, 1∕3) & & \cr &]), & & \cr &(−1, [ & & \cr &(1, 0, 1∕2), & & \cr &(0, 1,−1∕2) & & \cr &]) & & \cr &] & & \cr & & }

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.