Sage Reference: Linear Algebra
Types of Numbers: A Quick Review
You have likely heard that there are different sets of numbers - real, complex, rational, integer, and so on. When defining vectors and matrices in Sage, it's important to specify what kinds of numbers the entries can be, even when we define a matrix or vector explicitly. For now, you just need to know how to work with them in Sage:
- The integers, which include all numbers on the real number line (the continuum), are denoted by
ZZ
in Sage.
- The real numbers, which include positive, negative, rational, irrational, integer, and natural numbers, but no complex numbers, are denoted
RR
in Sage.
- The rationals, which include positive and negative real numbers that can be expressed as a fraction, are denoted by
QQ
in Sage.
- The complex numbers, which include real and imaginary numbers, are denoted by
CC
in Sage.
By default, all entries will assumed to be integers (whole numbers), but we often need to work with matrices that have fractions or even complex numbers. In the examples below, we mostly stick to the default integer entries, and sometimes rational or real. We will see some problems later that will make use of complex numbers, too.
Vectors
Vectors are not specified as rows or columns, but will be interpreted by Sage:
Vector arithmetic works as usual, when defined. Be sure to evaluate the cell above before this one, or else the vectors will be undefined.
Dot products:
Norms and sum of entries:
Matrices and Common Matrix Operations
Matrices are defined by a list of rows. Each row must have the same number of entries, but the number of rows and columns may differ:
Multiply vectors and matrices on the right or left - Sage will decide if the vector is a row or column. But the dimensions still have to 'match'!
Augmented matrices can be created from two matrices, two vectors, or a matrix and a vector:
You can get lists of the rows or columns of a matrix:
or pick out specific rows, columns, or entries of a matrix:
The tools above can be used to change a matrix as well:
Some useful information: the determinant, trace, and rank of a matrix:
Find the inverse of a matrix using exponent notation for the multiplicative inverse:
Transposes
You can generate a random matrix of any size, with entries from any ring:
Row Operations
When performing elementary row operations, it's a good habit to define a new matrix with each step, rather than redefining the matrix itself. Below, we'll apply three operations to the matrix A, but we'll create a new matrix for each.
If you want to change to alter a matrix (so you don't have to define all the new ones in between), you can apply commands to the matrix itself. Note that this will redefine the matrix each time, so the operations build on each other:
Of course, there's an even easier way of reducing a matrix: have Sage do it for you!
Solving Systems of Linear Equations
If \(A\) is a matrix with \(m\) rows and \(b\) is a vector with \(m\) entries, you can find solutions (if any) to the system \(Ax=b \):
Sometimes there will be no solutions, which Sage indicates as a error (the last line of output is the clue):
There's a slight catch. If the system has infinitely many solutions, Sage will only return one. The free variables are all set equal to 0, so it looks like the system below has a single solution, but that's not the case:
It's a good idea to check the reduced echelon form of the system:
Now you can see where Sage got it's answer from, and why it didn't tell the whole story. Use the RREF!
Eigenvalues and Eigenvectors
Sage can give us the characteristic polynomial of a matrix:
We can use multiple commands to find the eigenvalues. The
roots
function returns the roots of the characteristic polynomial, along with the multiplicity of each, so we know exactly what the eigenvalues are:
Or we can just jump to the eigenvalues. Notice the warning that an approximation is being used; for our purposes, this is quite precise:
And of course, the associated eigenvectors, returned as a list of lists with three items: an eigenvalue, an eigenvector, and the multiplicity of the eigenvalue:
Diagonalization
Before we try to diagonalize a matrix, let's make sure we can:
We have a good matrix, now lets find a diagonal matrix \(D\) and invertible matrix \(S\) so that \(A = SDS^{-1}\):