Skip to article frontmatterSkip to article content

Computing with matrices

At a reductive level, a matrix is a table of numbers that obeys certain algebraic laws. But matrices are pervasive in scientific computation, mainly because they represent linear operations on vectors. Moreover, vectors go far beyond the three-dimensional representations of physical quantities you learned about in calculus.

2.2.1Notation

We use capital letters in bold to refer to matrices, and lowercase bold letters for vectors. All named vectors in this book are column vectors. The bold symbol 0\boldsymbol{0} may refer to a vector of all zeros or to a zero matrix, depending on context; we use 0 as the scalar zero only.

To refer to a specific element of a matrix, we use the uppercase name of the matrix without boldface, as in A24A_{24} to mean the (2,4)(2,4) element of A\mathbf{A}.[1] To refer to an element of a vector, we use just one subscript, as in x3x_3. If you see a boldface character with one or more subscripts, then you know that it is a matrix or vector that belongs to a sequence or indexed collection.

We will have frequent need to refer to the individual columns of a matrix as vectors. Our convention is to use a lowercase bold version of the matrix name with a subscript to represent the column number. Thus, a1,a2,,an\mathbf{a}_1,\mathbf{a}_2,\ldots,\mathbf{a}_n are the columns of the m×nm\times n matrix A\mathbf{A}. Conversely, whenever we define a sequence of vectors v1,,vp\mathbf{v}_1,\ldots,\mathbf{v}_p, we can implicitly consider them to be columns of a matrix V\mathbf{V}. Sometimes we might write V=[vj]\mathbf{V}=\bigl[ \mathbf{v}_j \bigr] to emphasize the connection.

The notation AT\mathbf{A}^T is used for the transpose of a matrix, whether it is real or complex. In the case of complex matrices, it’s almost always more desirable to use the adjoint A\mathbf{A}^*, which is the transpose with the complex conjugate of each element.[2] If A\mathbf{A} is real, then A=AT\mathbf{A}^*=\mathbf{A}^T. A symmetric matrix is a square matrix such that AT=A\mathbf{A}^T=\mathbf{A}.

The identity matrix of size nn is denoted I\mathbf{I}, or sometimes In\mathbf{I}_n if emphasizing the size is important in context. For columns of the identity we break with our usual naming convention and denote them by ej\mathbf{e}_j.

2.2.2Block matrix expressions

We will often find it useful to break a matrix into separately named pieces. For example, we might write

A=[A11A12A13A21A22A23],B=[B1B2B3]. \mathbf{A} = \begin{bmatrix} \mathbf{A}_{11} & \mathbf{A}_{12} & \mathbf{A}_{13} \\ \mathbf{A}_{21} & \mathbf{A}_{22} & \mathbf{A}_{23} \end{bmatrix}, \qquad \mathbf{B} = \begin{bmatrix} \mathbf{B}_1 \\ \mathbf{B}_2 \\ \mathbf{B}_3 \end{bmatrix}.

It’s understood that blocks that are on top of one another have the same number of columns, and blocks that are side by side have the same number of rows. Typically, if the blocks all have compatible dimensions, then they can be multiplied as though the blocks were scalars. For instance, continuing with the definitions above, we say that A\mathbf{A} is block-2×32\times 3 and B\mathbf{B} is block-3×13\times 1, so we can write

AB=[A11B1+A12B2+A13B3A21B1+A22B2+A23B3], \mathbf{A} \mathbf{B} = \begin{bmatrix} \mathbf{A}_{11}\mathbf{B}_1 + \mathbf{A}_{12}\mathbf{B}_2 + \mathbf{A}_{13}\mathbf{B}_3 \\ \mathbf{A}_{21}\mathbf{B}_1 + \mathbf{A}_{22}\mathbf{B}_2 + \mathbf{A}_{23}\mathbf{B}_3 \end{bmatrix},

provided that the individual block products are well-defined. For transposes we have, for example,

AT=[A11TA21TA12TA22TA13TA23T]. \mathbf{A}^T = \begin{bmatrix} \mathbf{A}_{11}^T & \mathbf{A}_{21}^T \\[2mm] \mathbf{A}_{12}^T & \mathbf{A}_{22}^T \\[2mm] \mathbf{A}_{13}^T & \mathbf{A}_{23}^T \end{bmatrix}.

2.2.3Vector and matrix basics

Vectors and matrices are integral to scientific computing. All modern languages provide ways to work with them beyond manipulation of individual elements.

2.2.4Row and column operations

A critical identity in matrix multiplication is

Aej=aj. \mathbf{A} \mathbf{e}_j = \mathbf{a}_j.

Furthermore, the expression

A[e1e3e5] \mathbf{A} \begin{bmatrix} \mathbf{e}_1 & \mathbf{e}_3 & \mathbf{e}_5 \end{bmatrix}

reproduces three columns. An equivalent expression in Julia would be A[:,1:2:5].

We can extend the same idea to rows by using the general identity (RS)T=STRT(\mathbf{R}\mathbf{S})^T=\mathbf{S}^T\mathbf{R}^T. Let B=AT\mathbf{B}=\mathbf{A}^T have columns [bj]\bigl[ \mathbf{b}_j \bigr], and note

(bj)T=(Bej)T=ejTBT=ejTA. (\mathbf{b}_j)^T = (\mathbf{B} \mathbf{e}_j)^T = \mathbf{e}_j^T \mathbf{B}^T = \mathbf{e}_j^T \mathbf{A}.

But ejT\mathbf{e}_j^T is the jjth row of I\mathbf{I}, and bjT\mathbf{b}_j^T is the transpose of the jjth column of B\mathbf{B}, which is the jjth row of A\mathbf{A} by B=AT\mathbf{B}=\mathbf{A}^T. Thus, multiplication on the left by row jj of the identity extracts the jjth row. Extracting the single element (i,j)(i,j) from the matrix is, therefore, eiTAej\mathbf{e}_i^T \mathbf{A} \mathbf{e}_j.

Being able to extract specific rows and columns of a matrix via algebra makes it straightforward to do row- and column-oriented operations, such as linear combinations.

2.2.5Exercises

  1. ✍ Suppose

    C=[IAIB].\mathbf{C} = \begin{bmatrix} \mathbf{I} & \mathbf{A} \\ -\mathbf{I} & \mathbf{B} \end{bmatrix}.

    Using block notation, find C2\mathbf{C}^2 and C3\mathbf{C}^3.

  2. ⌨ Let

    A=[2110014122021315],B=[31027102],\mathbf{A} = \begin{bmatrix} 2 & 1 & 1 & 0 \\ 0 & -1 & 4 & 1 \\ 2 & 2 & 0 & -2 \\ 1 & 3 & -1 & 5 \end{bmatrix}, \quad \mathbf{B} = \begin{bmatrix} 3 & -1 & 0 & 2 \\ 7 & 1 & 0 & 2 \end{bmatrix},
    u=[2131],v=[πe].\mathbf{u} = \begin{bmatrix} 2 \\ -1 \\ 3 \\ 1 \end{bmatrix}, \quad \mathbf{v} = \begin{bmatrix} \pi \\ e \end{bmatrix}.

    (Do not round off the values in v\mathbf{v}—find them using native Julia commands.) For each expression below, use Julia to find the result, or explain why the result does not exist.

    (a) AB,\mathbf{A}\mathbf{B},\quad (b) BA,\mathbf{B} \mathbf{A},\quad (c) vTB,\mathbf{v}^T \mathbf{B},\quad (d) Bu,\mathbf{B} \mathbf{u},\quad (e) [uAuA2uA3u]\bigl[ \, \mathbf{u}\:\: \mathbf{A}\mathbf{u} \:\: \mathbf{A}^2 \mathbf{u} \:\: \mathbf{A}^3 \mathbf{u} \bigr].

  3. ⌨ Let

    u=[1357911],v=[605040302010].\mathbf{u} = \begin{bmatrix} 1\\3\\5\\7\\9\\11 \end{bmatrix}, \qquad \mathbf{v} = \begin{bmatrix} -60 \\ -50 \\ -40 \\ -30 \\ -20 \\ -10 \end{bmatrix}.

    Find the inner products uTv\mathbf{u}^T\mathbf{v} and vTu\mathbf{v}^T\mathbf{u} and the outer products uvT\mathbf{u}\mathbf{v}^T and vuT\mathbf{v}\mathbf{u}^T.

  4. ⌨ In Julia, give a demonstration of the identity (AB)T=BTAT(\mathbf{A}\mathbf{B})^T=\mathbf{B}^T\mathbf{A}^T for some arbitrarily chosen 3×43\times 4 matrix A\mathbf{A} and 4×24\times 2 matrix B\mathbf{B}.

  5. ✍ Prove that if A\mathbf{A} and B\mathbf{B} are invertible, then (AB)1=B1A1(\mathbf{A}\mathbf{B})^{-1}=\mathbf{B}^{-1}\mathbf{A}^{-1}. (In producing the inverse, it follows that AB\mathbf{A}\mathbf{B} is invertible as well.)

  6. ✍ Suppose B\mathbf{B} is an arbitrary 4×34\times 3 matrix. In each part below a matrix A\mathbf{A} is described in terms of B\mathbf{B}. Express A\mathbf{A} as a product of B\mathbf{B} with one or more other matrices.

    (a) AR4×1\mathbf{A}\in\mathbb{R}^{4 \times 1} is the result of adding the first column of B\mathbf{B} to -2 times the last column of B\mathbf{B}.

    (b) The rows of AR4×3\mathbf{A}\in\mathbb{R}^{4 \times 3} are the rows of B\mathbf{B} in order 4,3,2,1.

    (c) The first column of AR4×3\mathbf{A}\in\mathbb{R}^{4 \times 3} is 1 times the first column of B\mathbf{B}, the second column of A\mathbf{A} is 2 times the second column of B\mathbf{B}, and the third column of A\mathbf{A} is 3 times the third column of B\mathbf{B}.

    (d) AA is the scalar sum of all elements of B\mathbf{B}.

  7. (a) ✍ Prove that for real vectors v\mathbf{v} and w\mathbf{w} of the same length, the inner products vTw\mathbf{v}^T\mathbf{w} and wTv\mathbf{w}^T\mathbf{v} are equal.

    (b) ✍ Prove true, or give a counterexample for, the equivalent statement about outer products, vwT\mathbf{v}\mathbf{w}^T and wvT\mathbf{w}\mathbf{v}^T.

Footnotes
  1. This aspect of our notation is slightly unusual. More frequently one would see the lowercase a24a_{24} in this context. We feel that our notation lends more consistency and clarity to expressions with mixed symbols, and it is more like how computer code is written.

  2. The conjugate of a complex number is found by replacing all references to the imaginary unit ii by i-i.