From A First Course in Linear Algebra
Version 2.20
© 2004.
Licensed under the GNU Free Documentation License.
http://linear.ups.edu/
Contributed by Steve Canfield
SAGE uses different rings to denote the type of an object. The rings are as
follows:
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.
$$\begin{array}{llll}\hfill & m=matrix\left(\left[\left[2,3\right],\left[4,7\right]\right]\right)\phantom{\rule{2em}{0ex}}& \hfill & \phantom{\rule{2em}{0ex}}\\ \hfill & m.base\text{\_}ring\left(\right)\phantom{\rule{2em}{0ex}}& \hfill & \phantom{\rule{2em}{0ex}}\\ \hfill & IntegerRing\phantom{\rule{2em}{0ex}}& \hfill & \phantom{\rule{2em}{0ex}}\\ \hfill & m.echelon\text{\_}form\left(\right)\phantom{\rule{2em}{0ex}}& \hfill & \phantom{\rule{2em}{0ex}}\\ \hfill & \left[\begin{array}{cc}\hfill 2\hfill & \hfill 0\hfill \\ \hfill 0\hfill & \hfill 1\hfill \end{array}\right]\phantom{\rule{2em}{0ex}}& \hfill & \phantom{\rule{2em}{0ex}}\end{array}$$As you can clearly see, m isn’t even in reduced rowechelon 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 rowechelon form, so unless you are specifically working with integers take note.
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
would create a $3\times 4$ matrix named a that is equal to
$$\left[\begin{array}{cccc}\hfill 1\hfill & \hfill 2\hfill & \hfill 3\hfill & \hfill 4\hfill \\ \hfill 5\hfill & \hfill 6\hfill & \hfill 7\hfill & \hfill 8\hfill \\ \hfill 9\hfill & \hfill 10\hfill & \hfill 11\hfill & \hfill 12\hfill \end{array}\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 ${\mathbb{R}}^{}$ or $\u2102$space, the matrix will be of those types. This can cause problems as integers cannot become fractions, which is an issue when calculating reduced rowechelon form. We therefore recommend using the following construction to make your matrices,
$$a=matrix\left(QQ,\left[\left[1,2,3,4\right],\left[5,6,7,8\right],\left[9,10,11,12\right]\right]\right)$$ 
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.
Contributed by Steve Canfield and Robert Beezer
Rowreducing 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 rowechelon 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
$$\begin{array}{lll}\hfill a=matrix\left(QQ,\left[\left[1,2,3,4\right],\left[5,6,7,8\right],\left[9,10,11,12\right]\right]\right)& \phantom{\rule{2em}{0ex}}& \hfill \end{array}$$and issue the command a.echelon_form() . The result is
$$\begin{array}{lll}\hfill \left[\begin{array}{cccc}\hfill 1\hfill & \hfill 0\hfill & \hfill 1\hfill & \hfill 2\hfill \\ \hfill 0\hfill & \hfill 1\hfill & \hfill 2\hfill & \hfill 3\hfill \\ \hfill 0\hfill & \hfill 0\hfill & \hfill 0\hfill & \hfill 0\hfill \end{array}\right]& \phantom{\rule{2em}{0ex}}& \hfill \end{array}$$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
$$\begin{array}{lll}\hfill b=matrix\left(\left[\left[1,2,3,4\right],\left[5,6,7,8\right],\left[9,10,11,12\right]\right]\right)& \phantom{\rule{2em}{0ex}}& \hfill \end{array}$$and issue the command b.echelon_form() . The result is
$$\begin{array}{lll}\hfill \left[\begin{array}{cccc}\hfill 1\hfill & \hfill 2\hfill & \hfill 3\hfill & \hfill 4\hfill \\ \hfill 0\hfill & \hfill 4\hfill & \hfill 8\hfill & \hfill 12\hfill \\ \hfill 0\hfill & \hfill 0\hfill & \hfill 0\hfill & \hfill 0\hfill \end{array}\right]& \phantom{\rule{2em}{0ex}}& \hfill \end{array}$$You can now clearly see Sage’s reluctance to multiply row 2 by $\frac{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 $\frac{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 roundoff error becomes relevant. Computer arithmetic with real numbers is, of necessity, subject to minor inaccuracies and errors. This becomes problematic when rowreducing a matrix. If a zero entry is computed instead as an extremely small number, such as $1.287\times 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 rowechelon form.
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.
Contributed by Robert Beezer
Vectors in SAGE are constructed from lists, and are displayed horizontally. For
example, the vector
$$v=\left[\begin{array}{c}\hfill 1\hfill \\ \hfill 2\hfill \\ \hfill 3\hfill \\ \hfill 4\hfill \end{array}\right]$$ 
would be entered and named via the command
$$v=vector\left(QQ,\left[1,\phantom{\rule{0.3em}{0ex}}2,\phantom{\rule{0.3em}{0ex}}3,\phantom{\rule{0.3em}{0ex}}4\right]\right)$$ 
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\ast u+\left(3\right)\ast 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).”
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).
Suppose a is the name of a matrix stored in SAGE. Then a.transpose() will return the transpose of a .
Contributed by Steve Canfield
SAGE can compute eigenspaces and eigenvalues for you. If you have a matrix
named a and you type
$$a.eigenspaces\left(\right)$$ 
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.
$$\begin{array}{llll}\hfill & m=matrix\left(QQ,\left[\left[13,8,4\right],\left[12,7,4\right],\left[24,16,7\right]\right]\right)\phantom{\rule{2em}{0ex}}& \hfill & \phantom{\rule{2em}{0ex}}\\ \hfill & m.eigenspaces\left(\right)\phantom{\rule{2em}{0ex}}& \hfill & \phantom{\rule{2em}{0ex}}\\ \hfill & \left[\left(3,\left[\left(1,2\u22153,1\u22153\right)\right]\right),\left(1,\left[\left(1,0,1\u22152\right),\left(0,1,1\u22152\right)\right]\right)\right]\phantom{\rule{2em}{0ex}}& \hfill & \phantom{\rule{2em}{0ex}}\\ \hfill & \phantom{\rule{2em}{0ex}}& \hfill \end{array}$$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 $\lambda =3$ and 2 vectors for $\lambda =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.