Section 6.1 Special Matrices
Some matrices occur frequently enough to be given special names:
-
A zero matrix is a matrix in which every entry is \(0\text{.}\) The command
zero_matrix(m, n)creates an \(m\) by \(n\) zero matrix.For a square matrix, the command can be shortened tozero_matrix(n)like in the example below. -
A ones matrix is a matrix in which every entry is \(1\text{.}\) The command
ones_matrix(m, n)creates an \(m\) by \(n\) ones matrix.The ones matrix can be useful for example to add a constant offset to all entries of a matrix.For a square matrix, the command can be shortened toones_matrix(n)like in the example below. -
A diagonal matrix is a square matrix that has zero entries everywhere outside the main diagonal. Note that a square zero matrix is a diagonal matrix. Sage offers the command
diagonal_matrix([a_1, a_2, ..., a_n])to create a diagonal matrix with diagonal entries \(a_1, a_2, \ldots, a_n\) and \(0\) elsewhere.Here is an example of a \(3 \times 3\) diagonal matrix with entries \(2\text{,}\) \(4\text{,}\) and \(6\) on the main diagonal.To check if a matrix is diagonal, the methodis_diagonal()can be used. -
An identity matrix is a diagonal matrix with all of its diagonal entries equal to 1. The command
identity_matrix(n)returns an identity matrix of dimension \(n \times n\text{.}\) Here is an example of a \(5 \times 5\) identity matrix. -
Two other common types of square matrices are the Upper and Lower triangular matrices. A square matrix is an upper triangular matrix if all entries below the diagonal are zero. The following is an example of a \(3 \times 3\) upper triangular matrix.Note that Sage does not have these predefined commands to create triangular matrices, or check if a square matrix is of either type (upper or lower triangular). They can however be obtained by leveraging the
LU()method that we will see later on.
Note that Sage also offers a special method
random_matrix() to generate random matrices. For example, the following command generates a random \(3 \times 4\) matrix with integer entries between \(0\) and \(9\text{.}\)
