Skip to main content

Linear Algebra with SageMath: Learn math with open-source software

Section 6.1 Special Matrices

Some matrices occur frequently enough to be given special names:
  • The zero matrix contains only zeros and acts as the additive identity. The command zero_matrix(m, n) creates an \(m\) by \(n\) zero matrix.
    For a square matrix, the command can be shortened to zero_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 diagonal matrix may have some or all its diagonal entries equal to \(0\text{.}\) 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.
    Diagonal matrices are simple to multiply and often appear in eigenvalue problems which we will see in upcoming chapters. To check if a matrix is diagonal, the method is_diagonal() can be used.
  • An identity matrix is a diagonal matrix with all its diagonal elements equals to 1. The command identity_matrix(n) returns an identity matrix if dimension \(n \times n\text{.}\) Here isa= an example of a \(5 \times 5\) identity matrix.
  • Although the ones matrix is not a predefined matrix in Sage, it is common in other similar computational frameworks such as Octave©, and MATLAB©. Essentially, it is a matrix where every entry is 1. It can be created by leverage the built-in list duplication operator * applied on a list of ones.
  • Two other common types of square matrices are the Upper and Lower triangular matrices. A square matrix 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 such 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.