How to Define Matrices

Matrices are both a very ancient and a very current mathematical concept. References to matrices and systems of equations can be found in Chinese manuscripts dating back to around 200 B.C. The term matrix was first used by the English mathematician James Sylvester (1814--1897), who defined the term in 1850. Over the years, mathematicians and scientists have found many applications of matrices. More recently, the advent of personal and large-scale computers has increased the use of matrices in a wide variety of applications.

A matrix (plural matrices) is a rectangular array of numbers, functions, or any symbols. It can be written as

\[ {\bf A} = \left[ \begin{array}{cccc} a_{1,1} & a_{1,2} & \cdots & a_{1,n} \\ a_{2,1} & a_{2,2} & \cdots & a_{2,n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m,1} & a_{m,2} & \cdots & a_{m,n} \end{array} \right] \qquad \mbox{or} \qquad {\bf A} = \left( \begin{array}{cccc} a_{1,1} & a_{1,2} & \cdots & a_{1,n} \\ a_{2,1} & a_{2,2} & \cdots & a_{2,n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m,1} & a_{m,2} & \cdots & a_{m,n} \end{array} \right) . \]

We denote this array by a single letter A (usually a capital boldfaced letter) or by \( \left( a_{i,j} \right) \) or \( \left[ a_{i,j} \right] , \) depending what notation (parenthesis or brackets) is in use. The symbol \( a_{i,j} ,\) or sometimes \( a_{ij} ,\) in the ith row and jth column is called the \( \left( i, \, j \right) \) entry. We say that A has m rows and n columns, and that it is an \( m \times n \) matrix. We also refer to A as a matrix of size \( m \times n . \)

Any \( m \times n \) matrix can be considered as an array of \( n \) columns

\[ {\bf A} = \left[ \begin{array}{cccc} a_{1,1} & a_{1,2} & \cdots & a_{1,n} \\ a_{2,1} & a_{2,2} & \cdots & a_{2,n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m,1} & a_{m,2} & \cdots & a_{m,n} \end{array} \right] = \left[ \left( \begin{array}{c} a_{1,1} \\ a_{2,1} \\ \vdots \\ a_{m,1} \end{array} \right) , \ \left( \begin{array}{c} a_{1,2} \\ a_{2,2} \\ \vdots \\ a_{m,2} \end{array} \right) , \ \cdots \left( \begin{array}{c} a_{1,n} \\ a_{2,n} \\ \vdots \\ a_{m,n} \end{array} \right) \right] = \left[ {\bf c}_1 , {\bf c}_2 , \ldots {\bf c}_n \right] , \]
or as a collection of \( m \) rows
\[ {\bf A} = \left[ \begin{array}{cccc} a_{1,1} & a_{1,2} & \cdots & a_{1,n} \\ a_{2,1} & a_{2,2} & \cdots & a_{2,n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m,1} & a_{m,2} & \cdots & a_{m,n} \end{array} \right] = \left( \begin{array}{cccc} \langle a_{1,1} , a_{1,2} , \cdots , a_{1,n} \rangle \\ \langle a_{2,1} , a_{2,2} , \cdots , a_{2,n} \rangle \\ \vdots \\ \langle a_{m,1} , a_{m,2} , \cdots , a_{m,n} \rangle \end{array} \right) = \left[ \begin{array}{c} {\bf r}_1 \\ {\bf r}_2 \\ \vdots \\ {\bf r}_m \end{array} \right] . \]
Here the column vector \( {\bf c}_i = \langle a_{1,i} , a_{2,i} , \ldots , a_{m,i} \rangle^T \) in ith row contains entries of matrix A in ith column. Correspondingly, the row vector \( {\bf r}_j = \langle a_{j,1} , a_{j,2} , \ldots , a_{j,n} \rangle \) in jth column contains entries of matrix A in jth row.

Before we can discuss arithmetic operations for matrices, we have to define equality for matrices. Two matrices are equal if they have the same size and their corresponding elements are equal. A matrix with elements that are all 0’s is called a zero or null matrix. A null matrix usually is indicated as 0.

Another very important type of matrices are square matrices that have the same number of rows as columns. In particular, a square matrix having all elements equal to zero except those on the principal diagonal is called a diagonal matrix.

Matrices are fundamental objects in linear algebra and in Sage, so there are a variety of ways to construct a matrix in Sage. Generally, you need to specify what types of entries the matrix contains (more on that to come), the number of rows and columns, and the entries themselves. First, let’s dissect an example:

sage: B = matrix(QQ, [[1, 2, 3], [4, 5, 6]]); B 
[ 1 2 3 ]
[ 4 5 6 ]
Here QQ is the set of all rational numbers (fractions with an integer numerator and denominator), 2 is the number of rows, 3 is the number of columns. Sage understands a list of items as delimited by brackets ( [,]) and the items in the list can again be lists themselves. So
[[1, 2, 3], [4, 5, 6]]
is a list of lists, and in this context the inner lists are rows of the matrix. There are various shortcuts you can employ when creating a matrix. For example, Sage is able to infer the size of the matrix from the lists of entries. Now, let's enter a matrix.
sage: M=matrix(QQ,[[2,4,0,8],[-1,3,3,-2],[0,1,1,0]]); M 
Or you can specify how many rows the matrix will have and provide one big grand list of entries, which will get chopped up, row by row, if you prefer.
sage: C = matrix(QQ, 2, [1, 2, 3, 4, 5, 6]); C 
[1 2 3]
[4 5 6]
Matrices with symbolic entries:
sage: matrix(SR, 2, 2, range(4)) 
[0 1]
[2 3]
sage: matrix(SR, 2, 2, var('t'))
[t 0]
[0 t]
The function matrix() or Matrix() is used to do this. For now the first argument to the function should be "QQ", this means that the matrix will contain either integers or rational numbers (fractions).
sage: C.parent()
Full MatrixSpace of 2 by 3 dense matrices over Integer Ring
sage: A = matrix(2, 3, [[1, cos(3.14), 3], [4, 5, 6]])
sage: A.parent()
Full MatrixSpace of 2 by 3 dense matrices over
Real Field with 53 bits of precision

The matrix A is defined with two rows and three columns. Notice how the matrix was specified row-by-row, with each row inside a pair of square brackets and all three rows enclosed in another set of square brackets; commas are used to separate matrix entries and rows.

Computer scientists and computer languages prefer to begin counting from zero, while mathematicians and written mathematics prefer to begin counting at one.

Perhaps the most confusing thing about using Sage for matrix work is that rows and columns are numbered starting at 0 rather than 1 as is usually done for matrices in mathematical work. This means that the rows in M are numbered 0, 1, and 2 while the columns are numbered 0, 1, 2, and 3. For example, we can access the first and second rows with:

sage: M.row(0); M.row(1)
sage: B.nrows(), B.ncols()
(2, 3)
sage: B.base_ring() Rational Field
sage: B[1,1] 5

Notice how the function row() is used; it is "attached" to the matrix varible with a dot. This means that the row function operates on the matrix M.

A matrix can be defined by a formula using python command lambda :

sage: m = matrix(QQ, 3, 3, lambda i, j: i+j); m 
[0 1 2] [1 2 3] [2 3 4]
sage: m = matrix(3, lambda i,j: i-j); m
[ 0 -1 -2] [ 1 0 -1] [ 2 1 0]

sage: matrix(QQ, 2, 3, lambda x, y: x+y)
[0 1 2] [1 2 3]
sage: matrix(QQ, 5, 5, lambda x, y: (x+1) / (y+1))
[ 1 1/2 1/3 1/4 1/5] [ 2 1 2/3 1/2 2/5] [ 3 3/2 1 3/4 3/5] [ 4 2 4/3 1 4/5] [ 5 5/2 5/3 5/4 1]

Execute the following three compute cells in the Sage notebook, and notice how the three matrices are constructed to have entries from the integers, the rationals and the reals.

The norm of a matrix may be thought of as its size because it is a nonnegative number. Matrix norms are directly related to vector norms. The definitions are summarized below for an \( n \times n \) matrix A.

\[ {\bf A} = \left[ \begin{array}{cccc} a_{1,1} & a_{1,2} & \cdots & a_{1,n} \\ a_{2,1} & a_{2,2} & \cdots & a_{2,n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{n,1} & a_{n,2} & \cdots & a_{n,n} \end{array} \right] . \]
The operator norm corresponding to the p-norm for vectors, p ≥ 1, is:
\[ \| {\bf A} \|_p = \sup_{{\bf x} \ne 0} \, \frac{\| {\bf A}\,{\bf x} \|_p}{\| {\bf x} \|_p} , \]
where \( \| {\bf x} \|_p = \left( x_1^p + x_2^p + \cdots + x_n^p \right)^{1/p} .\) The most important norms are

1-norm (is commonly known as the maximum column sum norm) of a matrix A may be computed as

\[ \| {\bf A} \|_1 = \max_{1 \le j \le n} \,\sum_{i=1}^n | a_{i,j} | . \]
The infinity norm, \( \infty - \) norm of matrix A may be computed as
\[ \| {\bf A} \|_{\infty} = \max_{1 \le i \le n} \,\sum_{j=1}^n | a_{i,j} | , \]
which is simply the maximum absolute row sum of the matrix.
In the special case of p = 2 we get the Euclidean norm (which is equal to the largest singular value of a matrix)
\[ \| {\bf A} \|_2 = \sup_{\bf x} \left\{ \| {\bf A}\, {\bf x} \|_2 \, : \quad \mbox{with} \quad \| {\bf x} \|_2 =1 \right\} . \]
The Frobenius norm:
\[ \| {\bf A} \|_F = \left( \sum_{i=1}^m \sum_{j=1}^n |a_{i.j} |^2 \right)^{1/2} = \left( \mbox{tr}\, {\bf A} \,{\bf A}^{\ast} \right)^{1/2} . \]

Some properties of the matrix norms are presented in the following

Theorem: Let A and B be \( n \times n \) matrices and let \( k \) be a scalar.

  • \( \| {\bf A} \| \ge 0 \) for any square matrix A.
  • \( \| {\bf A} \| =0 \) if and only if the matrix A is zero: \( {\bf A} = {\bf 0}. \)
  • \( \| k\,{\bf A} \| = |k| \, \| {\bf A} \| \) for any scalar \( k. \)
  • \( \| {\bf A} + {\bf B}\| \le \| {\bf A} \| + \| {\bf B} \| .\)
  • \( \| {\bf A} \, {\bf B}\| \le \| {\bf A} \| \, \| {\bf B} \| \)

The Frobenius norm:

\[ \| {\bf A} \|_F = \left( \sum_{i=1}^m \sum_{j=1}^n |a_{i.j} |^2 \right)^{1/2} = \left( 1+49+4+9 \right)^{1/2} = \sqrt{63} = \left( \mbox{tr}\, {\bf A} \,{\bf A}^{\ast} \right)^{1/2} . \]

Example. Evaluate the norms of the matrix \( {\bf A} = \left[ \begin{array}{cc} 1 & -7 \\ -2 & -3 \end{array} \right] . \)

The absolute column sums of A are \( 1 + | -2 | =3 \) and \( |-7| + | -3 | =10 . \) The larger of these is 10 and therefore \( \| {\bf A} \|_1 = 10 . \)

The absolute row sums of A are \( 1 + | -7 | =8 \) and \( | -2 | + |-3| = 5 , \) therefore, \( \| {\bf A} \|_{\infty} = 8 . \)

The Euclidean norm of A is

\[ \| {\bf A} \|_2 = \sup_{\bf x} \left\{ \, \sqrt{(x_1 - 7\,x_2 )^2 + (2\,x_1 + 3\,x_2 )^2} \, : \quad \mbox{with} \quad x_1^2 + x_2^2 =1 \right\} . \]
To find its exact value, we evaluate the product
\[ {\bf A}\,{\bf A}^{\ast} = \left[ \begin{array}{cc} 1 & -7 \\ -2 & -3 \end{array} \right] \, \left[ \begin{array}{cc} 1 & -2 \\ -7 & -3 \end{array} \right] = \left[ \begin{array}{cc} 50 & 19 \\ 19 & 13 \end{array} \right] . \]

This matrix \( {\bf A}\,{\bf A}^{\ast} \) has two eigenvalues \( \frac{1}{2} \left( 63 \pm \sqrt{2813} \right) . \) Hence, the Euclidean norm of the matrix A is \( \sqrt{\frac{1}{2} \left( 63 + \sqrt{2813} \right)} \approx 7.61701, \) and its Frobenius norm is \( \sqrt{63} \approx 7.93725 . \)

sage: M=matrix(QQ,[[2,4,0,8],[-1,3,3,-2],[0,1,1,0]]); M 

 

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.)