Matrix Algebra

Sage is a powerful system for studying and exploring many different areas of mathematics. This page presents some topics from Linear Algebra needed for construction of solutions to systems of linear ordinary differential equations and some applications.

 

Basic Operations with Matrices

Our first example deals with economics. Let us consider two families Anderson (A) and Boichuck (B) that have expenses every month such as: utilities, health, entertainment, food, etc... . Let us restrict ourselves to: food, utilities, and health. How would one represent the data collected? Many ways are available but one of them has an advantage of combining the data so that it is easy to manipulate them. Indeed, we will write the data as follows:
\[ \mbox{Month} = \begin{bmatrix} \mbox{Family} & \mbox{Food} & \mbox{Utilities} & \mbox{Entertainment} \\ \mbox{A} & f_1 & u_1 & e_1 \\ \mbox{B} & f_2 & u_2 & e_2 \end{bmatrix} . \]
If we have no problem confusing the names of the families and what the expenses are, then we may record them in matrix form:
\[ \mbox{Month} = \begin{bmatrix} f_1 & u_1 & e_1 \\ f_2 & u_2 & e_2 \end{bmatrix} . \]
The size of the matrix, as a block, is defined by the number of Rows and the number of Columns. In this case, the above matrix has 2 rows and 3 columns. In our case, we say that the matrix is a \( m \times n \) matrix (pronounce m-by-n matrix). Keep in mind that the first entry (meaning m) is the number of rows while the second entry (n) is the number of columns. Our above matrix is a (\( 2\times 3 \) ) matrix.

Let us assume, for example, that the matrices for the months of July, August, and September are

\[ {\bf J} = \begin{bmatrix} 650 & 125 & 50 \\ 600 & 150 & 60 \end{bmatrix} , \qquad {\bf A} = \begin{bmatrix} 700 & 250 & 150 \\ 650 & 200 & 80 \end{bmatrix} , \qquad \mbox{and} \qquad {\bf S} = \begin{bmatrix} 750 & 300 & 200 \\ 650 & 275 & 120 \end{bmatrix} , \]
respectively. The next question may sound easy to answer, but requires a new concept in the matrix context. What is the matrix-expense for the two families for the summer? The idea is to add the three matrices above by adding the corresponding entries:
\begin{align*} \mbox{Summer} &= {\bf J} + {\bf A} + {\bf S} = \begin{bmatrix} 21000 & 675 & 400 \\ 20000 & 775 & 260 \end{bmatrix} . \end{align*}
We can summarize addition or subtraction of matrices of the same size by using arithmetic operations on the corresponding elements:
\[ {\bf A} \pm {\bf B} = \left[ a_{i,j} \right] \pm \left[ b_{i,j} \right] = \left[ a_{i,j} \pm b_{i,j} \right] . \]
Clearly, if you want to double a matrix, it is enough to add the matrix to itself. So we have
\[ \mbox{double of } \, \begin{bmatrix} f_1 & u_1 & e_1 \\ f_2 & u_2 & e_2 \end{bmatrix} = \begin{bmatrix} f_1 & u_1 & e_1 \\ f_2 & u_2 & e_2 \end{bmatrix} + \begin{bmatrix} f_1 & u_1 & e_1 \\ f_2 & u_2 & e_2 \end{bmatrix} = 2\, \begin{bmatrix} f_1 & u_1 & e_1 \\ f_2 & u_2 & e_2 \end{bmatrix} = \begin{bmatrix} 2\,f_1 & 2\,u_1 & 2\,e_1 \\ 2\,f_2 & 2\,u_2 & 2\,e_2 \end{bmatrix} . \]
Therefore, to multiply by a scalar, one needs to multiply by this scalar every entry. What about subtracting two matrices? It is easy, since subtraction is a combination of the two above rules. Indeed, if A and B are two matrices of the same size, then we will write
\[ {\bf A} - {\bf B} = {\bf A} + (-1)\, {\bf B} . \]
The negative of a matrix M, denoted by \( -{\bf M} ,\) is a matrix with elements that are the negatives of the elements in M. ■

Now we are going to introduce matrix multiplication that may at first seem rather strange. We don't know exactly who or when the multiplication of matrices was invented. At least we know that the work of 1812 by Jacques Philippe Marie Binet (1786--1856) contains the definition of the product of matrices.

If A is an \( m \times r \) matrix and B is \( r \times n \) matrix, then the product \( {\bf A}\,{\bf B} \) is the \( m \times n \) matrix whose entries are determined as follows. To find the entry in row i and column j of \( {\bf A}\,{\bf B} \) , single out row i from the matrix A and column j from the matrix B. Take the dot product of the corresponding vectors of size r and put it into \( (i,j) \) spot of product \( {\bf A}\,{\bf B} .\)

Example. Consider the matrices

\[ {\bf A} = \begin{bmatrix} -1 & 0 \\ 2 & 3 \end{bmatrix} \qquad \mbox{and} \qquad {\bf B} = \begin{bmatrix} 1 & 2 \\ 3 & 0 \end{bmatrix} . \]
Multiplying gives
\[ {\bf A}\, {\bf B} = \begin{bmatrix} -1 & -2 \\ 11 & 4 \end{bmatrix} \qquad \mbox{and} \qquad {\bf B}\,{\bf A} = \begin{bmatrix} 3 & 6 \\ -3 & 0 \end{bmatrix} . \]
Thus, \( {\bf A}\,{\bf B} \ne {\bf B}\,{\bf A} . \) Note that one of the products (\( {\bf A}\,{\bf B} \) or \( {\bf B}\,{\bf A} \) ) may exit, but another not. ■

The n ext important operation is transposition, which changes row into columns.

sage: A = matrix(QQ,3,3,[2,-1,1,1,-3,3,5,-2,-3])
sage: A
[ 2 -1 1]
[ 1 -3 3]
[ 5 -2 -3]
sage: A.transpose() [ 2 1 5 ]
[ -1 -3 -2]
[ 1 3 -3]
.T is a convenient shortcut for the transpose:
sage: A.T
[ 2 1 5 ]
[ -1 -3 -2]
[ 1 3 -3]

Theorem: If the sizes of the matrices are such that the stated operations can be performed, then:

  • \( \left({\bf A}^{\mathrm T} \right)^{\mathrm T} = {\bf A} \) for any matrix A;
  • \( \left( {\bf A} + {\bf B} \right)^{\mathrm T} = {\bf A}^{\mathrm T} + {\bf B}^{\mathrm T} .\)
  • \( \left( {\bf A} - {\bf B} \right)^{\mathrm T} = {\bf A}^{\mathrm T} - {\bf B}^{\mathrm T} .\)
  • \( \left( k\,{\bf A} \right)^{\mathrm T} = k\, {\bf A}^{\mathrm T} .\)
  • \( \left( {\bf A} \, {\bf B}\right)^{\mathrm T} = {\bf B}^{\mathrm T} \, {\bf A}^{\mathrm T} . \)

 

sage: m = identity_matrix(3)
sage: m
[1 0 0]
[0 1 0]
[0 0 1]
sage: m2= m.insert_row(3, [1,2,3])
sage: m2
[1 0 0]
[0 1 0]
[0 0 1]
[1 2 3]
sage: m.insert_row(2, [1,2,3])
[1 0 0]
[0 1 0]
[1 2 3]
[0 0 1]
sage: m.swap_rows(1,2); m
[1 0 0]
[0 0 1]
[0 1 0]
sage: m.swap_columns(0,1); m
[0 1 0]
[1 0 0]
[0 0 1]
Often you want to give matrices names:
sage: M = matrix([[1,2,3],[4,5,6]])
sage: I = identity_matrix(3)

If a matrix A has complex entries, its complex conjugate is denoted by \( \overline{\bf A} .\) The conjugate transpose of A, denoted by \( {\bf A}^{\ast} , \) is defined by

\[ {\bf A}^{\ast} = \overline{\bf A}^{\mathrm T} = \overline{{\bf A}^{\mathrm T}} . \]

The matrix \( {\bf A}^{\ast} , \) is called adjoint to the matrix A. If \( {\bf A}^{\ast} = {\bf A}, \) then matrix A is called self-adjoint or Hermitian. ■

Example. The matrix

\[ {\bf A} = \begin{bmatrix} 1 & {\bf j} \\ {\bf j} & 2 \end{bmatrix} , \]
where j is a unit vector on the complex plane in positive vertical direction (so \( {\bf j}^2 =- 1 \) ), is symmetric but not self-adjoint. Its adjoint is
\[ {\bf A}^{\ast} = \begin{bmatrix} 1 & -{\bf j} \\ -{\bf j} & 2 \end{bmatrix} , \]
and the products
\[ {\bf A}^{\ast} {\bf A} = \begin{bmatrix} 2 & -{\bf j} \\ {\bf j} & 5 \end{bmatrix} \qquad\mbox{and}\qquad {\bf A}\, {\bf A}^{\ast} = \begin{bmatrix} 2 & {\bf j} \\ -{\bf j} & 5 \end{bmatrix} \]
are self-adjoint matrices. ■

Sage has a matrix method, .augment() , that will join two matrices side-by-side provided they both have the same number of rows. The same method will allow you to augment a matrix with a column vector. Some methods allow optional input, typically using keywords. Matrices can track subdivisions, making breaks between rows and/or columns. When augmenting, you can ask for the subdivision to be included. Evalute the compute cell above if you have not already, so that A and b are defined, and then evaluate:

sage: M = A.augment(b, subdivide=True)
sage: M
[1-1 2|1]
[2 1 1|8]
[1 1 0|5]
sage: v = vector(QQ, 3, [1, 2, 3])
sage: m3=m.augment(v)
sage: m3
[1 0 0 1]
[0 1 0 2]
[0 0 1 3]
sage: m3[1,:]
[0 1 0 2]
sage:m3[:,3]
[1]
[2]
[3]
sage:mm3[:,2]
[0]
[0]
[1]
To insert a column, take the transpose, insert a row, and take the transpose again.
sage: m3=m2.matrix_from_columns([0,1,3])
sage: m3
sage:m2=m2.transpose(); m2
[1 0 0 1]
[0 1 0 2]
[0 0 1 3]
sage: m = identity_matrix(3)
sage: m2= m.insert_row(3, [1,2,3])
To substitute the first column in a matrix with a given column-vector, use the following command:
sage: m[:,0] = vector([5,4,3])
sage: m
[5 0 0]
[4 1 0]
[3 0 1]
Similarly,
sage: m = identity_matrix(3)
sage: m[:,1] = vector([5,4,3]); m
[1 5 0]
[0 4 0]
[0 3 1]

It is frequently necessary to deal separately with various groups of elements or blocks/submatrices within a larger matrix. This situation can asire when the size of a matrix becomes too large for convenient handling, and it becomes imperative to work with only a portion of the matrix. Also, there will be cases in which one part of a matrix will have a physical significance that is different from the remainder, and it is instructive to isolate that portion and identify it by a special symbol.

Any matrix can be interpreted as having been broken into sections called blocks or submatrices. A matrix interpreted as a block matrix can be visualized as the original matrix with inserting horizontal and vertical lines between selected rows and columns, which break it up, or partition it, into a collection of smaller matrices.

For example, the following are three possible partitions of a general \( 3 \times 4 \) matrix---the first is a partition of A into four submatrices \( {\bf A}_{11}, \ {\bf A}_{12}, \ {\bf A}_{21} , \ \mbox{and} \ {\bf A}_{22} ; \) the second is a partition of A into its row vectors \( {\bf r}_1, \ {\bf r}_2 , \ \mbox{and}\ {\bf r}_3 ; \) and the third is a partition of A into its column vectors \( {\bf c}_1 , \ {\bf c}_2 , \ {\bf c}_3 , \ \mbox{and}\ {\bf c}_4 : \)

\begin{align*} {\bf A} &= \left[ \begin{array}{ccc|c} a_{11} & a_{12} & a_{13} & a_{14} \\ a_{21} & a_{22} & a_{23} & a_{24} \\ \hline a_{31} & a_{32} & a_{33} & a_{34} \end{array} \right] = \left[ \begin{array}{cc} {\bf A}_{11} & {\bf A}_{12} \\ {\bf A}_{21} & {\bf A}_{22} \end{array} \right] , \\ {\bf A} &= \left[ \begin{array}{cccc} a_{11} & a_{12} & a_{13} & a_{14} \\ \hline a_{21} & a_{22} & a_{23} & a_{24} \\ \hline a_{31} & a_{32} & a_{33} & a_{34} \end{array} \right] = \left[ \begin{array}{c} {\bf r}_1 \\ {\bf r}_2 \\ {\bf r}_3 \end{array} \right] , \\ {\bf A} &= \left[ \begin{array}{c|c|c|c} a_{11} & a_{12} & a_{13} & a_{14} \\ a_{21} & a_{22} & a_{23} & a_{24} \\ a_{31} & a_{32} & a_{33} & a_{34} \end{array} \right] = \left[ \begin{array}{cccc} {\bf c}_1 & {\bf c}_2 & {\bf c}_3 & {\bf c}_4 \end{array} \right] . \end{align*}

Example. Multiply two matrices using partition procedure.

\[ {\bf A} = \left[ \begin{array}{cc|c} 3&0&-1 \\ -5&2&4 \\ \hline -2&-6&3 \end{array} \right] = \begin{bmatrix} {\bf A}_{11} & {\bf A}_{12} \\ {\bf A}_{21} & {\bf A}_{22} \end{bmatrix} \qquad \mbox{and} \qquad {\bf B} = \left[ \begin{array}{cc} 1&2 \\ -3&4 \\ \hline 2&1 \end{array} \right] = \begin{bmatrix} {\bf B}_1 \\ {\bf B}_2 \end{bmatrix} . \]
Using partition, we have
\[ {\bf A} \, {\bf B} = \begin{bmatrix} {\bf A}_{11} & {\bf A}_{12} \\ {\bf A}_{21} & {\bf A}_{22} \end{bmatrix} \times \begin{bmatrix} {\bf B}_1 \\ {\bf B}_2 \end{bmatrix} = \begin{bmatrix} {\bf A}_{11} \,{\bf B}_1 + {\bf A}_{12}\, {\bf B}_2 \\ {\bf A}_{21} \, {\bf B}_1 + {\bf A}_{22} \, {\bf B}_2 \end{bmatrix} , \]
where
\begin{align*} {\bf A}_{11} \, {\bf B}_1 &= \begin{bmatrix} 3&0 \\ -5& 2 \end{bmatrix} \times \begin{bmatrix} 1&2 \\ -3&4 \end{bmatrix} = \begin{bmatrix} 3\times 1 + 0 \times (-3) & 3\times 2 + 0 \times 4 \\ -5\times 1 + 2\times (-3) & -5\times 2 + 2\times 4 \end{bmatrix} = \begin{bmatrix} 3&6 \\ -11& -2 \end{bmatrix} , \\ \\ {\bf A}_{12} \, {\bf B}_2 &= \begin{bmatrix} -1 \\ 4 \end{bmatrix} \times \begin{bmatrix} 2 & 1 \end{bmatrix} = \begin{bmatrix} -1\times 2 & -1\times 1 \\ 4\times 2 & 4\times 1 \end{bmatrix} = \begin{bmatrix} -2& -1 \\ 8 & 4 \end{bmatrix} , \\ \\ {\bf A}_{21} \, {\bf B}_1 &= \begin{bmatrix} -2 & -6 \end{bmatrix} \times \begin{bmatrix} 1& 2 \\ -3& 4 \end{bmatrix} = \begin{bmatrix} -2\times 1 - 6 \times (-3) & -2 \times 2 -6 \times 4 \end{bmatrix} = \begin{bmatrix} 16 & -28 \end{bmatrix} , \\ \\ {\bf A}_{22} \, {\bf B}_2 &= \begin{bmatrix} 3\end{bmatrix} \times \begin{bmatrix} 2& 1\end{bmatrix} = \begin{bmatrix} 3\times 2 & 3\times 1\end{bmatrix} = \begin{bmatrix} 6& 3 \end{bmatrix} , \\ \\ {\bf A}_{11} \, {\bf B}_1 + {\bf A}_{12} \, {\bf B}_2 &= \begin{bmatrix} 3& 6 \\ -11& 2 \end{bmatrix} + \begin{bmatrix} -2& -1 \\ 8&4 \end{bmatrix} = \begin{bmatrix} 1& 5 \\ -3 & 2 \end{bmatrix} , \\ {\bf A}_{21} \, {\bf B}_1 + {\bf A}_{22} \, {\bf B}_2 &= \begin{bmatrix} 16 & -28 \end{bmatrix} + \begin{bmatrix} 6&3\end{bmatrix} = \begin{bmatrix} 22& -25\end{bmatrix} . \end{align*}
Finally, we get
\[ {\bf A} \, {\bf B} = \left[ \begin{array}{cc} 1&5 \\ -3&2 \\ \hline 22&-25 \end{array} \right] . \qquad ■ \]

Partition has many uses, some of them are for finding particular rows or columns of a matrix product \( {\bf A}\,{\bf B} \) without computing the entire product, or determing the inverse matrix. It is possible to use a block partitioned matrix product that involves only algebra on submatrices of the factors. The partitioning of the factors is not arbitrary, however, and the dimensions of partition matrices A and B should match up such that all submatrix products that will be used are defined. Given an \( m \times n \) matrix A with q row partitions and s column partitions

\[ {\bf A} = \left[ \begin{array}{cccc} {\bf A}_{11} & {\bf A}_{12} & \cdots & {\bf A}_{1s} \\ {\bf A}_{21} & {\bf A}_{22} & \cdots & {\bf A}_{2s} \\ \vdots & \vdots & \ddots & \vdots \\ {\bf A}_{q1} & {\bf A}_{q2} & \cdots & {\bf A}_{qs} \end{array} \right] \]
and an \( n \times k \) matrix B with s row partitions and rr column partitions
\[ {\bf B} = \left[ \begin{array}{cccc} {\bf B}_{11} & {\bf B}_{12} & \cdots & {\bf B}_{1r} \\ {\bf B}_{21} & {\bf B}_{22} & \cdots & {\bf B}_{2s} \\ \vdots & \vdots & \ddots & \vdots \\ {\bf B}_{s1} & {\bf B}_{s2} & \cdots & {\bf B}_{sr} \end{array} \right] \]
that are compatible with the partitions of A, the matrix product
\[ {\bf C} = {\bf A}\,{\bf B} \]
can be formed blockwise, yielding C as an \( m \times n \) matrix with q row partitions and r column partitions. The matrices in the resulting matrix C are calculated by multiplying:
\[ {\bf C}_{i,j} = \sum_{\gamma =1}^s {\bf A}_{i,\gamma} {\bf B}_{\gamma , j} . \]

Let us consider two matrices A and B, where A has m rows and B has n columns. We assume that dimensions of these two matrices allow us to multiply them. Then we partition matrix A into row vectors and B into column vectors:

\[ {\bf A} = \left[ \begin{array}{c} {\bf a}_1 \\ {\bf a}_2 \\ \vdots \\ {\bf a}_m \end{array} \right] \qquad\mbox{and} \qquad {\bf B} = \left[ \begin{array}{cccc} {\bf b}_1 & {\bf b}_2 & \cdots & {\bf b}_n \end{array} \right] . \]
Now we compute their product as
\[ {\bf A} \,{\bf B} = {\bf A} \left[ {\bf b}_1 , \ {\bf b}_2 , \ \cdots , \ {\bf b}_n \right] = \left[ {\bf A} \,{\bf b}_1 , \ {\bf A} \,{\bf b}_2 , \ \cdots , \ {\bf A} \,{\bf b}_n \right] , \]
or as
\[ {\bf A} \,{\bf B} = \left[ \begin{array}{c} {\bf a}_1 \\ {\bf a}_2 \\ \vdots \\ {\bf a}_m \end{array} \right] {\bf B} = \left[ \begin{array}{c} {\bf a}_1 \,{\bf B} \\ {\bf a}_2 \,{\bf B} \\ \vdots \\ {\bf a}_m \,{\bf B} \end{array} \right] . \]

Theorem: If A is an \( m \times n \) matrix, and if \( {\bf x} = \left[ x_1 , x_2 , \ldots , x_n \right]^{\mathrm T} \) is an \( n \times 1 \) column vector, then the product \( {\bf A}\,{\bf x} \) can be expressed as a linear combination of the column vectors of A in which the coefficients are the entries of x:

\[ {\bf A} \,{\bf x} = x_1 \left( \mbox{column 1} \right) + x_2 \left( \mbox{column 2} \right) + \cdots + x_n \left( \mbox{column}\ n \right) . \]

As a partial demonstration of manipulating subdivisions of matrices, we can reset the subdivisions of M with the .subdivide() method. We provide a list of rows to subdivide before, then a list of columns to subdivide before, where we remember that counting begins at zero.

sage: M.subdivide([1,2],[1])
sage: M
[1|-1 2 1]
[--+--------]
[2|1 1 8]
[--+--------]
[1|1 0 5]
Sage will perform individual row operations on a matrix. This can get a bit tedious, but it is better than doing the computations by hand, and it can be useful when building up more complicated procedures for a matrix. For each row operation, there are two similar methods. One changes the matrix “in-place” while the other creates a new matrix that is a modified version of the original. This is an important distinction that you should understand for every new Sage command you learn that might change a matrix or vector. Consider the first row operation, which swaps two rows. There are two matrix methods to do this, a “with” version that will create a new, changed matrix, which you will likely want to save, and a plain version that will change the matrix it operates on “in-place.” The copy() function, which is a general-purpose command, is a way to make a copy of a matrix before you make changes to it. Study the example below carefully, and then read the explanation following. (Remember that counting begins with zero.)

 

 

Applications