Section 2.2 Matrices
In this section, we will see how to define matrices, and perform basic operations on them. A matrix is an \(m \times n\) rectangular array of numbers:
\begin{equation*}
\begin{bmatrix}
a_{11} & a_{12} & \dots & a_{1n} \\
a_{21} & a_{22} & \dots & a_{2n} \\
\vdots & \vdots & \ddots & \vdots \\
a_{m1} & a_{m2} & \dots & a_{mn}
\end{bmatrix}
\end{equation*}
where \(m\) is the number of rows and \(n\) is the number of columns. Each entry \(a_{ij}, i=1\dots m, j=1\dots n,\) corresponds to the value at the intersection of the \(i\)-th row and \(j\)-th column.
Just like vectors, Sage does come with built-in support for matrices. There are many ways to define a matrix in Sage using the command
matrix
, passing for instance the rows of the matrix as a list.Sage also allow constructor a matrix based on a list of vectors (the rows of the matrix).
Alternatively, we can create a matrix by passing the entries as a list, and the dimensions as arguments to the
matrix
command.We don’t need to specify both dimensions of a matrix. Sage can infer the missing dimension from the number of entries in the list.
The entries of the matrix can also be defined programmatically using a list comprehension.
The individual entries of a matrix can be accessed using the row and column indices. For instance, here is how we can retrieve the entry at the second row and third column of the matrix \(M\text{.}\) Recall that the indices in Sage start at 0.
Sage offers specific methods to fetch all the rows, or the columns of a matrix, or to retrieve the entries for specific row or column, or the diagonal of the matrix.
The
rows
method returns all rows of the matrix (as a list of tuples). In the same way, the
columns
method returns all the columns of the matrix (as a list of tuples). The
M.row(i)
returns the \(i\)-th row of the matrix. The
M.column(j)
returns the \(j\)-th column of the matrix. The
M.diagonal()
returns the main diagonal of the matrix. Similarly, Sage offer more methods to help us iterate over the rows and/or the columns of a matrix. For instance, the method
nrows()
returns the number of rows of a matrix. Sage method
ncols()
returns the number of rows of the matrix. Alternatively, the method
dimensions
returns the dimensions of the matrix as a pair (nrows, ncols)
.Note that a matrix with a single list of values creates a vector-like object, but it is NOT a vector. Here is a vector in Sage.
Compare that to this single-row matrix. Observe the square brackets in matrices in lieu of the parenthesis.
Type assertion is a useful tool to check if two objects are identical and are of the same type. For instance, comparing the types of \(v\) and \(m\) yields
False
because they are NOT of the same type. Just like Vectors, Matrices in Sage can also be created in different Fields, inferred from the datatype passed to the
matrix
command, or explicitly when instantiating the matrix. The method
base_ring
returns the base ring of the matrix.Once again, if the field is omitted, Sage will simply infers the field from the datatype of the matrix entries. Sage matrices supports the same fields as vectors (\(ZZ, QQ, RR, CC\)).