Section SAGE  SAGE: Open Source Mathematics Software

From A First Course in Linear Algebra
Version 2.02
© 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:

\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

\eqalignno{ 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 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

\eqalignno{ 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

\eqalignno{ \left [\array{ 1&0&−1&−2 \cr 0&1& 2 & 3 \cr 0&0& 0 & 0 } \right ] & & }

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

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

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

\eqalignno{ \left [\array{ 1&2&3& 4 \cr 0&4&8&12 \cr 0&0&0& 0 } \right ] & & }

You can now clearly see Sage’s reluctance to multiply row 2 by {1\over 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\over 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 × 1{0}^{−18}, 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 = \left [\array{ 1 \cr 2 \cr 3 \cr 4 } \right ]

would be entered and named via the command

v = vector(QQ, [1,\kern 1.95872pt 2,\kern 1.95872pt 3,\kern 1.95872pt 4])

See the notes about rings (Computenote R.SAGE) and matrix entry (Computenote 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

2u + (−3)v

will compute the correct vector. it can be assigned to a variable (which will 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.

\eqalignno{ &m = matrix(QQ, [[−13,−8,−4], [12, 7, 4], [24, 16, 7]]) & & \cr &m.eigenspaces() & & \cr &[(3, [(1, 2∕3, 1∕3)]), (−1, [(1, 0, 1∕2), (0, 1,−1∕2)])] & & \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.