Skip to main content

Section B.2 Linear algebra basics

Entering a matrix.

A matrix is entered as a list of rows, where each row is a list of entries.

As mentioned in Section B.1, if you are running Sage locally on your computer you can usually just input A instead of print(A) to get an echo of your input.

Matrix operations.

You can do all the things you think you can with a matrix.

Yep, it's invertible.

Matrix arithmetic.

This all works as you'd expect.

Vectors.

Sage has a vector object type. Notice all the brackets.

Sage treats a vector as a row vector, but does the right thing if you multiply it on the left by a matrix. Here is row-vector-times-matrix.

And now matrix-times-column-vector (but the output will still be as a vector object).

You can use vectors to create a matrix, but it will be as rows.

Usually we want to work with vectors as columns in a matrix. No problem, either transpose …

… or use the column_matrix method in the first place.

Now let's investigate whether our vectors are linearly dependent or independent.

Turns out our vectors are linearly dependent. In particular, from the RREF we see that \(\uvec{v}_3 = \uvec{v}_2 - \uvec{v}_1\text{.}\)

If all we wanted to know was dependence/independence, we could just use rank.

Rank \(2\) for three vectors means dependent. Or, since dimension of row spaces and column spaces are equal, if all we wanted to know was dependence/independence, we could have used rank without the transpose or column_matrix methods.

Yep, still dependent. (But keep in mind that row space won't tell you the dependence relation among the vectors, only column space will.)

We can extract a column from a matrix as a vector. Keep in mind that Sage uses \(0\)-based indexing.

We can also augment a matrix with a vector, usually to solve a system.

But we can also row reduce to determine if a vector is in the span of some other collection of vectors. Recall that P is the matrix we made with columns equal to v1, v2, and v3. Notice how we can chain a bunch of computations into one command.

If we view the above as the reduced form of the augmented matrix

\begin{equation*} \left[\begin{array}{ccc|c} \uvec{v}_1 \amp \uvec{v}_2 \amp \uvec{v}_3 \amp \uvec{w} \end{array}\right] \end{equation*}

then the third row says that this system is inconsistent, so \(\uvec{w}\) is not in \(\Span \{ \uvec{v}_1, \uvec{v}_2, \uvec{v}_3 \}\text{.}\)

Let's cook up another example where \(\uvec{w}\) will definitely be in the span of the \(\uvec{v}\) vectors. This time we'll drop the creation of the intermediate w_column matrix.

OK, we fudged this one with the way we defined \(\uvec{w}\text{,}\) so that by definition it was in \(\Span \{ \uvec{v}_1, \uvec{v}_2, \uvec{v}_3 \}\text{.}\) But row reducing served another purpose: from the fourth column we obtain a simplified expression \(\uvec{w} = 2 \uvec{v}_1 - \uvec{v}_2 \text{.}\) (And we already knew \(\uvec{v}_3\) was dependent with \(\uvec{v}_1\) and \(\uvec{v}_2\text{,}\) so \(\uvec{v}_3\) was not needed as a spanning vector.)

Special forms of matrices.

Zero matrix.

Identity matrix. (But don't use I as a variable name for it, Sage already uses that for the imaginary complex number.)

We'll often need scalar matrices when investigating eigenvalues and eigenvectors.

Or a variable scalar matrix if we don't yet know the eigenvalue to use.

We can also create a diagonal matrix with specific entries.

Later in the course we will deal with block matrices.

Even though the output appears to be split into blocks (which is how we should think of it, since we constructed it that way), it's really just a regular matrix.

If you like vertical dividers in your augmented matrices, you can build them this way instead of using the augment method. But remember that Sage thinks of vectors as row vectors. While Sage does this right thing if we do M.augment(v) for matrix M and vector v and appends the vector as a new column, it isn't so accommodating with the block_matrix method, as there is too much possible ambiguity. So we need to explicitly turn our vector into a column matrix.

If we want certain blocks in our block matrix to be an identity block or a zero block, we can just put numbers 1 or 0, and Sage will figure out the correct size of block to use based on other blocks around them. (But you can only put a 1 where it will definitely be a square block.)

One last special type of matrix we will meet in this course is a block-diagonal matrix, which is a block matrix where all the blocks are zero, except possibly in a diagonal pattern of blocks.

(This time we had to put an explicit identity_matrix(1) instead of just 1, or else Sage would have told us Error: insufficient information to determine dimensions.)