Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168692
Image: ubuntu2004

Links to other worksheets:

Book, (1) Linear Algebra Arithmetic, (2) Linear Algebra Applications, (4) Linear Transformations, (5) Changing Bases

Some basics - How do you evaluate cells, add new cells, and add text to explain what you are doing?

You can evaluate cells by typing Shift+Enter (or clicking in them and clicking "evaluate"). Do this on the 2+3 cell below.

You can add new input cells in which to type Sage commands by clicking above or below each cell when a horizontal bar appears. Try adding a new cell above and below the 2+3 cell.

You can add new text cells by holding down shift when you click to enter a new cell. Try doing this on the cell below.

You can edit existing text cells by double clicking them.  Try modifying this text cell by double clicking on it.

2+3
5

Now you're ready to begin.

Vectors

Use the vector command to enter vectors. Functions always require paranthesis ( ) and lists always require brackets [ ].

u=vector([1,2]) u
(1, 2)
v=vector([3,1]) v
(3, 1)

The magnitude of a vector is also called the length or norm.  Use .norm() to find lengths in Sage.

u.norm()
sqrt(5)

The dot product is simply an asterisk *.

u*v
5

Here is an example of how to plot vectors. This works in both 2D and 3D.

plot(u)+plot(v)

To create an arrow with its base at vv and head at uu, use arrow(v,u).  To get this to work in 3D, you have to use the command arrow3d(v,u) instead of just arrow(v,u).

plot(u)+plot(v)+arrow(u,u+v)+plot(u+v,color='black')
plot(u)+plot(v)+plot(u-v,color='black')+arrow(v,u,color='black',linestyle='dashed')

Here's the 3d code, put in a nice html table. If you get an error below, just reevaluate this line of code.

u=vector([1,2,3]) v=vector([3,1,0]) html.table([ ["u", u], ["v", v], ["|u|", u.norm()], ["|v|", v.norm()], ["u.v", u*v], ]) plot(u)+plot(v)
u \left(1,2,3\right)
v \left(3,1,0\right)
|u| \sqrt{14}
|v| \sqrt{10}
u.v 5
plot(u)+plot(v)+arrow3d(u,u+v)+plot(u+v,color='black')
plot(u)+plot(v)+plot(u-v,color='black')+arrow3d(v,u,color='black',linestyle='dashed')

Entering matrices - and rref (or echelon_form() )

To enter a matrix, use the matrix command. To store the matrix using a name, use A = matrix( ... ).  Here is an example. 

A = matrix([[-1,2,3,4],[4,5,6,0],[3,2,8,-4]]) A
[-1 2 3 4] [ 4 5 6 0] [ 3 2 8 -4]

To row reduce the matrix, type the name of the matrix, a period, and then type echelon_form(). If you are familar with tab completion, Sage includes tab completion.  This means that you can type "A.ec" and then type "Tab" and it will show you all the commands that start with "ec" which can be applied to the matrix A. The command .rref() is availalbe in Sage version 4.3.4, so the next official release will allow us to type A.rref().

A.echelon_form()
[ 1 0 59 -20] [ 0 1 31 -8] [ 0 0 77 -24]

Notice that the 77 above is not a leading 1.  This is because Sage keeps track of the number system behind the matrix.  If you type in a matrix with all integer entries, then Sage will assume that all operations are to be done using only integers.  It will not fully reduce a matrix if the matrix requires division that will result in fractions.  To fix this, you can tell Sage that you want to work with fractions by typing QQ at the beginning of your matrix.  QQ is the Sage symbol for "Rational Numbers" or Q\mathbb Q 

A = matrix(QQ,[[-1,2,3,4],[4,5,6,0],[3,2,8,-4]]) A
[-1 2 3 4] [ 4 5 6 0] [ 3 2 8 -4]
A.echelon_form()
[ 1 0 0 -124/77] [ 0 1 0 128/77] [ 0 0 1 -24/77]

A quick way to enter matrices.

I prefer the following way to enter matrices.  Type QQ to allow fractional arithmetic, then the number of rows, numbers of columns, and then a list of the entries read left to right, top to bottom.  You can eliminate writing lots of brackets this way.

A=matrix(QQ,2,3,[1,2,3,4,5,6]) A
[1 2 3] [4 5 6]

Making a matrix of column vectors - Using transpose()

If you want to make a matrix of vectors, Sage will quickly place the vectors into rows.  To get them to be columns, use the transpose command.

u=vector([1,2]) v=vector([3,4]) w=vector([5,6]) A=matrix(QQ,[u,v,w]) A
[1 2] [3 4] [5 6]
B=A.transpose() B
[1 3 5] [2 4 6]

Selecting entries of a matrix

You can access specific entries in a matrix by typing A[i,j].  However, note that Sage begins all numbering at 0 instead of 1.  So the a11a_{11} spot in the matrix is A[0,0]A[0,0].

A=matrix(QQ,2,3,[1,2,3,4,5,6]) A
[1 2 3] [4 5 6]
A[0,0]
1
A[0]
(1, 2, 3)
A[1,2]
6

Matrix Multiplication

Just enter both matrices, and then use an asterisk * to multiply them.

A=matrix(QQ,2,3,[1,2,3,4,5,6,]) A
[1 2 3] [4 5 6]
B=matrix(QQ,3,2,[1,2,3,4,5,6,]) B
[1 2] [3 4] [5 6]
A*B
[22 28] [49 64]
B*A
[ 9 12 15] [19 26 33] [29 40 51]

You'll get an error if matrix multiplication doesn't make sense.

A*A
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "_sage_input_27.py", line 4, in <module> exec compile(ur'A*A' + '\n', '', 'single') File "", line 1, in <module> File "element.pyx", line 2028, in sage.structure.element.Matrix.__mul__ (sage/structure/element.c:13799) File "matrix_rational_dense.pyx", line 1028, in sage.matrix.matrix_rational_dense.Matrix_rational_dense._matrix_times_matrix_ (sage/matrix/matrix_rational_dense.c:11135) File "matrix_rational_dense.pyx", line 2504, in sage.matrix.matrix_rational_dense.Matrix_rational_dense._multiply_pari (sage/matrix/matrix_rational_dense.c:21860) ArithmeticError: self must be a square matrix

Rank, Determinants, Inverses, Eigenvalues, and Eigenvectors

Most commands we will need all semester long are available using tab completion.  First enter your matrix, then type A and a period, and then type the "Tab" key.  This will bring up a list of commands.  Try doing this now in the blank cell below (after evaluating the first cell by clicking in it and evaluating it (type Shift+Enter).

A=matrix(QQ,3,3,[2,1,0,1,2,0,0,0,1]) A
[2 1 0] [1 2 0] [0 0 1]

We'll be using the rank(), det(), inverse(), eigenvalues(), and eigenvectors_right() commands.

A.rank()
3
A.det()
3
A.echelon_form()
[1 0 0] [0 1 0] [0 0 1]
A.inverse()
[ 2/3 -1/3 0] [-1/3 2/3 0] [ 0 0 1/3]
A.eigenvalues()
[3, 1, 1]
A.eigenvectors_right()
[(3, [ (1, 1, 0) ], 1), (1, [ (1, -1, 0), (0, 0, 1) ], 2)]

Two questions:

  1. Why "right"?
  2. How do you read this?

We use "right" because we are trying to solve Ax=λxA\vec x = \lambda \vec x instead of xA=xλ\vec x A = \vec x \lambda. The latter are called "left" eigenvectors.

To read the output, you are given a list of values for each eigenvalue.  In particular, you are given (the eigenvalue, a set of linearly independent eigenvectors whose span gives all the eigenvalues, and the multiplicity of the eigenvalue - how many times it is a root).

[(3, [             -the eigenvalue
(1, 1, 0) -an eigenvector whose span gives all the rest
], 1), -3 has multiplicity 1.
(1, [ -1 is the next eigenvalue
(1, -1, 0), -Here there are 2 independent eigenvectors whose span gives the rest
(0, 0, 1)
], 2)] -$\lambda=1$ has multiplicty 2

Everything in a nice table format (provided the answers are nice)

This final example just computes all the values above for a square matrix, and displays them in an easy to read format. If the eigenvalues are rather complicated, then the display may get ugly and you are better off using the commands above.

A=matrix(QQ,3,3,[2,1,0,1,2,0,0,4,3]) EV=A.eigenvectors_right() if A.det()==0: inv="No Inverse Exists" ; else: inv=A.inverse(); list=([ ["Matrix",A], ["Transpose",A.transpose()], ["RREF",A.echelon_form()], ["Rank",A.rank()], ["Determinant",A.det()], ["Inverse",inv], ["Eigenvalues",A.eigenvalues()],[],[],[],[], ["Eigenvalue","Linearly Independent Eigenvectors", "Multiplicity of Eigenvalue"]]) for n in range(len(EV)): list.append(EV[n]) html.table(list)
Matrix \left(210120043\begin{array}{rrr} 2 & 1 & 0 \\ 1 & 2 & 0 \\ 0 & 4 & 3 \end{array}\right)
Transpose \left(210124003\begin{array}{rrr} 2 & 1 & 0 \\ 1 & 2 & 4 \\ 0 & 0 & 3 \end{array}\right)
RREF \left(100010001\begin{array}{rrr} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{array}\right)
Rank 3
Determinant 9
Inverse \left(2313013230498913\begin{array}{rrr} \frac{2}{3} & -\frac{1}{3} & 0 \\ -\frac{1}{3} & \frac{2}{3} & 0 \\ \frac{4}{9} & -\frac{8}{9} & \frac{1}{3} \end{array}\right)
Eigenvalues \left[1, 3, 3\right]
Eigenvalue Linearly Independent Eigenvectors Multiplicity of Eigenvalue
1 \left[\left(1,-1,2\right)\right] 1
3 \left[\left(0,0,1\right)\right] 2