Skip to main content
Logo image

Section 1.7 Some Sage examples

Here are some common uses of Sage with linear algebra applications.

Subsection 1.7.1 Row reduction, echelon form, kernel, column space

A random \(4\times 5\) rational matrix with rank 3. Note that while every matrix has a reduced row echelon form, this algorithm will generate a matrix of a desired size and rank, over a desired ring, whose reduced row-echelon form has only integral values.
The example below was chosen so that the pivots were not all in the first three columns.
It’s reduced row echelon form which allows us to confirm the rank and make it easier to find the kernel.
The kernel or nullspace of the matrix, \(\{ x\in \Q^5\mid Ax = 0\},\) written as row vectors.
The column space of the matrix: a basis of column space written as row vectors; note they are the pivot columns.
The following is simply a basis for the column space of \(A\) written as row vectors. Note that this is the reduced row-echelon form of the matrix formed from the rows above: A matrix and its RREF have the same row space.

Subsection 1.7.2 Eigenvalues, eigenvectors, and diagonalization

Generate a diagonalizable \(8\times 8\) integer matrix.
Compute the characteristic polynomial and factor it. The characteristic polynomial will necessarily factor into linear factors. To make things more interesting, run the Sage script until you get a characteristic polynomial with some algebraic multiplicities greater than one.
Compute the eigenvalues and bases for the corresponding eigenspaces. The output is a list giving each eigenvalue and a basis for the corresponding eigenspace. Watch for these to show up as the columns of the change of basis matrix.
Another way of getting the same data
The diagonalized matrix \(D=P^{-1}BP\) where \(P\) is the change of basis matrix whose columns are the eigenvectors spanning the eigenspaces.

Subsection 1.7.3 Rational and Jordan canonical forms

Example slightly modified from the Sage Reference Manual 1 .
We see the factored characteristic polynomial is divisible by a quadratic which is irreducible over \(\Q\text{,}\) so the matrix will have a rational canonical form, but not a Jordan form over \(\Q.\)
Here is the minimal polynomial, the largest of the invariant factors.
Here is a list of the invariant factors, given as a lists of coefficients of the polynomials they represent.
Here we turn those lists into polynomials. The rational canonical form is a block diagonal matrix with each block being the companion matrix.
The matrix \(C\) is not diagaonalizable over any field since the minimal polynomial has a multiple root.
Since the minimal(characteristic) polynomial has an irreducible quadratic factor, we need to extend the field \(\Q\) to a quadratic extension which contains a root in order to produce a Jordan form.
Now \(C\) has a Jordan canonical form over the field \(K.\)
Example taken from the Sage Reference Manual 2 , has all invariant factors a power of \((x-2).\)