Appendix A Sage Reference
We have introduced a number of Sage commands throughout the text, and the most important ones are summarized here in a single place.
- Accessing Sage
- In addition to the Sage cellls included throughout the book, there are a number of ways to access Sage.
- There is a freely available Sage cell at
sagecell.sagemath.org
. - You can save your Sage work by creating an account at
cocalc.com
and working in a Sage worksheet. - There is a page of Sage cells at
gvsu.edu/s/0Ng
. The results obtained from evaluating one cell are available in other cells on that page. However, you will lose any work once the page is reloaded.
- Creating matrices
-
There are a couple of ways to create matrices. For instance, the matrixcan be created in either of the two following ways.
matrix(3, 4, [-2, 3, 0, 4, 1,-2, 1,-3, 0, 2, 3, 0])
matrix([ [-2, 3, 0, 4], [ 1,-2, 1,-3], [ 0, 2, 3, 0] ])
Be aware that Sage can treat mathematically equivalent matrices in different ways depending on how they are entered. For instance, the matrixmatrix([ [1, 2], [2, 1] ])
has integer entries whilematrix([ [1.0, 2.0], [2.0, 1.0] ])
has floating point entries. - Special matrices
- Reduced row echelon form
- Vectors
- Addition
- Multiplication
-
The
*
operator performs scalar multiplication of vectors and matrices.v = vector([2,1]) print(3*v) A = matrix([[2,1],[-3,2]]) print(3*A)
- Operations on vectors
- The length of a vector
v
is found usingv.norm()
. - The dot product of two vectors
v
andw
isv*w
.
- Operations on matrices
- The transpose of a matrix
A
is obtained using eitherA.transpose()
orA.T
. - The inverse of a matrix
A
is obtained using eitherA.inverse()
orA^-1
. - The determinant of
A
isA.det()
. - A basis for the null space
is found withA.right_kernel()
. - Pull out a column of
A
using, for instance,A.column(0)
, which returns the vector that is the first column ofA
. - The command
A.matrix_from_columns([0,1,2])
returns the matrix formed by the first three columns ofA
.
- Eigenvectors and eigenvalues
- The eigenvalues of a matrix
A
can be found withA.eigenvalues()
. The number of times that an eigenvalue appears in the list equals its multiplicity. - The eigenvectors of a matrix having rational entries can be found with
A.eigenvectors_right()
. - If
can be diagonalized as thenD, P = A.right_eigenmatrix()
provides the matricesD
andP
. - The characteristic polynomial of
A
isA.charpoly('x')
and its factored formA.fcp('x')
.
- Matrix factorizations
- The
factorization of a matrixP, L, U = A.LU()
gives matrices so that - A singular value decomposition is obtained with
U, Sigma, V = A.SVD()
Itβs important to note that the matrix must be defined usingRDF
. For instance,A = matrix(RDF, 3,2,[1,0,-1,1,1,1])
. - The
factorization ofA
isA.QR()
provided thatA
is defined usingRDF
.