7 min read

Categories

Matrix Animation
Image credit: Jahobr, CC0, via Wikimedia Commons

Contents

This blog post will guide you through the steps of creating matrices in LaTeX. It will start with the general syntax and then explain how to create row and column vectors, determinants, arbitrary sized matrices and nested matrices. It will conclude with several examples of real world matrices and the use of matrices in mathematical expressions.

General Syntax

In LaTeX, matrices are created using the “bmatrix” environment. The general syntax for a matrix is as follows:

\begin{bmatrix}
    a & b & c \\
    d & e & f \\
    g & h & i \\
\end{bmatrix}
\[\begin{bmatrix} a & b & c \\ d & e & f \\ g & h & i \\ \end{bmatrix}\]

Replace the placeholders (a, b, c, etc.) with your desired matrix elements. The ampersand (&) is used to separate columns, and the double backslash (\\) indicates the end of a row.

Column and Row Vectors

Both column and row vectors are essentially specialized forms of matrices, and you can use the “bmatrix” environment to represent them.

Column Vector

To create a column vector, you can use the “bmatrix” environment with a single column:

  \begin{bmatrix}
      a \\
      b \\
      c \\
  \end{bmatrix}
\[\begin{bmatrix} a \\ b \\ c \\ \end{bmatrix}\]

Row Vector

Similarly, a row vector is a matrix with a single row:

  \begin{bmatrix}
      a & b & c
  \end{bmatrix}
\[\begin{bmatrix} a & b & c \end{bmatrix}\]

Different Bracket Types

LaTeX supports various bracket types for matrices.

Parentheses

To represent matrices with parentheses $(\cdot)$ you can use the “pmatrix” environment:

\begin{pmatrix}
    a & b \\
    c & d \\
\end{pmatrix} 
\[\begin{pmatrix} a & b \\ c & d \\ \end{pmatrix}\]

Determinants

To represent determinants, you can use the “vmatrix” or “Vmatrix” environments:

vmatrix: Vertical bars $\vert \cdot \vert$ as brackets

\begin{vmatrix}
    a & b \\
    c & d \\
\end{vmatrix}
\[\begin{vmatrix} a & b \\ c & d \\ \end{vmatrix}\]

Vmatrix: Double vertical $\Vert \cdot \Vert$ bars as brackets

\begin{Vmatrix}
    a & b \\
    c & d \\
\end{Vmatrix}
\[\begin{Vmatrix} a & b \\ c & d \\ \end{Vmatrix}\]

No brackets

To structure data in a matrix format without any brackets, you can use the “matrix” environment:

\begin{matrix}
    a & b \\
    c & d \\
\end{matrix}
\[\begin{matrix} a & b \\ c & d \end{matrix}\]

Use of Ellipses

Sometimes you want to represent a range of elements in a matrix without explicitly listing them all. For example when you have an arbitrary $m \times n$ matrix and you want to show only a few representative elements.

For this, you can use ellipses. There are different commands for horizontal, vertical and diagonal ellipses

Horizontal

The \dots (or \ldots) command can be used to represent a range of skipped elements in a row. For example, here is row vector showing only the first two and last of $n$ elements

\begin{bmatrix}
    a_1 & a_2 & \dots & a_n
\end{bmatrix}
\[\begin{bmatrix} a_1 & a_2 & \dots & a_n \end{bmatrix}\]

Vertical

The \vdots command can be used to represent a range of skipped elements in a column. For example, here is column vector showing only the first two and last of $n$ elements

\begin{bmatrix}
    a_1 \\
    a_2 \\
    \ldots \\
    a_n
\end{bmatrix}
\[\begin{bmatrix} a_1 \\ a_2 \\ \vdots \\ a_n \end{bmatrix}\]

Diagonal

The \ddots command, often used in combination with \dots and \vdots can be used to skip columns and rows simultaneously.

\begin{bmatrix}
    a_{11} & \dots & a_{1n} \\
    \vdots & \ddots & \vdots \\
    a_{n1} & \dots & a_{nn} \\
\end{bmatrix}
\[\begin{bmatrix} a_{11} & \dots & a_{1n} \\ \vdots & \ddots & \vdots \\ a_{n1} & \dots & a_{nn} \\ \end{bmatrix}\]

Some example matrices

Matrix elements can be arbitrary LaTeX expressions and the matrix will automatically adjust to accommodate the size of the expression.

Example: 2x2 rotation matrix

R = \begin{bmatrix}
    \cos \theta &-\sin \theta \\
    \sin \theta &\cos \theta
\end{bmatrix}
\[R = \begin{bmatrix} \cos \theta &-\sin \theta \\ \sin \theta &\cos \theta \end{bmatrix}\]

Example: curl of a vector field expressed as a determinant

\nabla \times \mathbf {F} =
\begin{vmatrix}
  \boldsymbol {\hat {\imath }} & {\boldsymbol {\hat {\jmath }}} & {\boldsymbol {\hat {k}}}
  \\ frac {\partial }{\partial x} & \frac {\partial }{\partial y} & \frac {\partial }{\partial z}\\F_{x} & F_{y} & F_{z}
\end{vmatrix}
\[\nabla \times \mathbf {F} = \begin{vmatrix} \boldsymbol {\hat {\imath }} & {\boldsymbol {\hat {\jmath }}} & {\boldsymbol {\hat {k}}} \\ \frac {\partial }{\partial x} & \frac {\partial }{\partial y} & \frac {\partial }{\partial z}\\ F_{x} & F_{y} & F_{z} \end{vmatrix}\]

Example: N-point discrete Fourier transform matrix

W = \frac{1}{\sqrt{N}} \begin{bmatrix}
1 & 1 & 1 & 1 & \cdots & 1 \\
1 & \omega & \omega^2 & \omega^3 & \cdots & \omega^{N-1} \\
1 & \omega^2 & \omega^4 & \omega^6 & \cdots & \omega^{2(N-1)} \\
1 & \omega^3 & \omega^6 & \omega^9 & \cdots & \omega^{3(N-1)} \\
\vdots & \vdots & \vdots & \vdots & \ddots & \vdots \\
1 & \omega^{N-1} & \omega^{2(N-1)} & \omega^{3(N-1)} & \cdots & \omega^{(N-1)(N-1)}
\end{bmatrix}
\[W = \frac{1}{\sqrt{N}} \begin{bmatrix} 1 & 1 & 1 & 1 & \cdots & 1 \\ 1 & \omega & \omega^2 & \omega^3 & \cdots & \omega^{N-1} \\ 1 & \omega^2 & \omega^4 & \omega^6 & \cdots & \omega^{2(N-1)} \\ 1 & \omega^3 & \omega^6 & \omega^9 & \cdots & \omega^{3(N-1)} \\ \vdots & \vdots & \vdots & \vdots & \ddots & \vdots \\ 1 & \omega^{N-1} & \omega^{2(N-1)} & \omega^{3(N-1)} & \cdots & \omega^{(N-1)(N-1)} \end{bmatrix}\]

where $\omega = e^{-2\pi i/N}$

Nested matrices

You can also nest matrices within matrices. Here’s an example of a block diagonal matrix that represents an important quantum computing logic gate called the CNOT gate:

\begin{bmatrix}
I_2 & 0 \\
0 & X
\end{bmatrix}
= \begin{bmatrix}
    \begin{bmatrix}
        1 & 0 \\
        0 & 1 
    \end{bmatrix} & 0 \\
    0 & \begin{bmatrix}
        0 & 1 \\
        1 & 0 
        \end{bmatrix}
\end{bmatrix}
\[\begin{bmatrix} I_2 & 0 \\ 0 & X \end{bmatrix} = \begin{bmatrix} \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix} & 0 \\ 0 & \begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix} \end{bmatrix}\]

where $I_2$ is the $2 \times 2$ identity matrix and $X$ is the Pauli-X matrix

You can nest matrices to any depth as needed. For example, the matrix for another quantum logic gate, the Toffoli gate is a block diagonal matrix with a nested matrix in the bottom right corner:

\begin{bmatrix}
I_4 & 0 \\
0 & CNOT
\end{bmatrix}
= \begin{bmatrix}
I_4 & 0 \\
0 & \begin{bmatrix}
I_2 & 0 \\
0 & X
\end{bmatrix}
\end{bmatrix}
= \begin{bmatrix}
    I_4 & 0 \\
    0 & \begin{bmatrix}
        \begin{bmatrix}
        1 & 0 \\
        0 & 1 
    \end{bmatrix} & 0 \\
    0 & \begin{bmatrix}
        0 & 1 \\
        1 & 0 
        \end{bmatrix}
        \end{bmatrix}
\end{bmatrix}
\[\begin{bmatrix} I_4 & 0 \\ 0 & CNOT \end{bmatrix} = \begin{bmatrix} I_4 & 0 \\ 0 & \begin{bmatrix} I_2 & 0 \\ 0 & X \end{bmatrix} \end{bmatrix} = \begin{bmatrix} I_4 & 0 \\ 0 & \begin{bmatrix} \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix} & 0 \\ 0 & \begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix} \end{bmatrix} \end{bmatrix}\]

where $I_4$ is the $4 \times 4$ identity matrix.

Matrices in Mathematical Expressions

Matrices can be incorporated into mathematical expressions just like any other variable. For example, here of the rotation of an arbitrary vector $\mathbf{v}$ by an angle $\theta$:

R\mathbf {v} = 
  \begin{bmatrix}
  \cos \theta &-\sin \theta \\
  \sin \theta &\cos \theta 
  \end{bmatrix}
  \begin{bmatrix}
    x\\y
  \end{bmatrix}=
  \begin{bmatrix}
  x\cos \theta -y\sin \theta 
  \\x\sin \theta +y\cos \theta 
  \end{bmatrix}
\[R\mathbf {v} = \begin{bmatrix} \cos \theta &-\sin \theta \\ \sin \theta &\cos \theta \end{bmatrix} \begin{bmatrix} x\\y \end{bmatrix}= \begin{bmatrix} x\cos \theta -y\sin \theta \\x\sin \theta +y\cos \theta \end{bmatrix}\]

Here is another example of the exponential of a diagonal matrix that demonstrates how matrices in LaTeX can be seamlessly integrated into a variety of mathematical expressions and how they play nicely with other LaTeX capabilities like superscripts and brackets.

\begin{aligned}
e^{\operatorname{diag}\left(
\begin{bmatrix}
    a_1 \\
    a_2 
\end{bmatrix}
\right)} &= \exp\left(\begin{bmatrix}
    a_1 & 0 \\
    0 & a_2 
\end{bmatrix}\right)
\\&= \sum_{i=0}^\infty \frac{1}{i!} \begin{bmatrix}
    a_1 & 0 \\
    0 & a_2 
\end{bmatrix}^i
\\&= \sum_{i=0}^\infty \frac{1}{i!} \begin{bmatrix}
    a_1^i & 0 \\
    0 & a_2^i
\end{bmatrix}
\\&= \begin{bmatrix}
    e^{a_1} & 0 \\
    0 & e^{a_2}
\end{bmatrix}
\end{aligned}
\[\begin{aligned} e^{\operatorname{diag}\left( \begin{bmatrix} a_1 \\ a_2 \end{bmatrix} \right)} &= \exp\left(\begin{bmatrix} a_1 & 0 \\ 0 & a_2 \end{bmatrix}\right) \\&= \sum_{i=0}^\infty \frac{1}{i!} \begin{bmatrix} a_1 & 0 \\ 0 & a_2 \end{bmatrix}^i \\&= \sum_{i=0}^\infty \frac{1}{i!} \begin{bmatrix} a_1^i & 0 \\ 0 & a_2^i \end{bmatrix} \\&= \begin{bmatrix} e^{a_1} & 0 \\ 0 & e^{a_2} \end{bmatrix} \end{aligned}\]

Conclusion

In this blog post we covered the key features of representing matrices in LaTeX. You should now be able to create a variety of matrices and incorporate them in your LaTeX code.