\chapter{Frame Camera Models}

Ames Stereo Pipeline supports a generic Pinhole camera model with
several lens distortion models which cover common calibration methods,
and also the somewhat more complicated panoramic (\textit{optical bar})
camera model.

\section{Pinhole Models}
\label{pinholemodels}

\section{Overview}

The generic Pinhole model uses the following parameters:

\begin{itemize}{}
\item  \textit{fu} = The focal length in horizontal pixel units.
\item  \textit{fv} = The focal length in vertical pixel units.
\item  \textit{cu} = The horizontal offset of the principal
point of the camera in the image plane in pixel units, from 0,0.
\item  \textit{cv} = The vertical offset of the principal
point of the camera in the image plane in pixel units, from 0,0.
\item  \textit{pitch} = The size of each pixel in the units used to specify
the four parameters listed above.  This will usually either be 1.0 if they
are specified in pixel units or alternately the size of a pixel in millimeters.
\end{itemize}

The focal length is sometimes known as the \textit{principal
distance}. The value $cu$ is usually approximately half the image width in
pixels times the pitch, while $cv$ is often the image height in pixels
times the pitch, though there are situations when these can be quite different.

A few sample Pinhole models are shown later in the text. The underlying mathematical model
is described in section \ref{pinholemath}.

Along with the basic Pinhole camera parameters, a lens distortion model
can be added.  Note that the units used in the distortion model must
match the units used for the parameters listed above.  For example, if the
camera calibration was performed using units of millimeters the focal lengths etc. must be given in
units of millimeters and the pitch must be equal to the size of each pixel in millimeters.
The following lens distortion models are currently supported:

\begin{itemize}{}
\item  \textbf{Null} = A placeholder model that applies no distortion.

\item  \textbf{Tsai} = A common distortion model similar to the one used by OpenCV and THEIA.
 This model uses the following parameters:
  \begin{description}
    \item \textit{K1, K2} = Radial distortion parameters.
    \item \textit{P1, P2} = Tangential distortion parameters.
  \end{description}
\hfill \\ The following equations describe the distortion, starting with the undistorted pixel $(Px, Py)$:

\[ (x, y) = \left(\frac{Px - cu}{fu}, \frac{Py-cv}{fv}\right) \]
\[ r^{2} = x^{2} + y^{2} \]
\[ x(distorted) = x\left(K_{1}r^{2} + K_{2}r^{4} + 2P_{1}y + P_{2}\left(\frac{r^{2}}{x} + 2x\right)\right)  \]
\[ y(distorted) = y\left(K_{1}r^{2} + K_{2}r^{4} + 2P_{2}x + P_{1}\left(\frac{r^{2}}{y} + 2y\right)\right)  \]
\hfill \\ References:
\begin{description}
   \item Roger Tsai, A Versatile Camera Calibration Technique for a High-Accuracy 3D
          Machine Vision Metrology Using Off-the-shelf TV Cameras and Lenses
\end{description}
\hfill \\ Note that this model uses normalized pixel units.

\item  \textbf{Adjustable Tsai} = A variant of the Tsai model where any number of \textit{K} terms and a skew term (alpha) can be used.  Can apply the AgiSoft Lens calibration parameters.

\item  \textbf{Brown-Conrady} = An older model based on a centering angle.

 This model uses the following parameters:
  \begin{description}
    \item \textit{K1, K2, K3} = Radial distortion parameters.
    \item \textit{P1, P2} = Tangential distortion parameters.
    \item \textit{xp, yp} = Principal point offset.
    \item \textit{phi} = Tangential distortion angle in radians.
  \end{description}
\hfill \\ The following equations describe the distortion:
\[ x = x(distorted) - xp \]
\[ y = y(distorted) - yp \]
\[ r^{2} = x^{2} + y^{2} \]
\[ dr = K_{1}r^{3} + K_{2}r^{5} + K_{3}r^{7} \]
\[ x(undistorted) = x + x\frac{dr}{r} - (P_{1}r^{2} +P_{2}r^{4})\sin(phi) \]
\[ y(undistorted) = y + y\frac{dr}{r} + (P_{1}r^{2} +P_{2}r^{4})\cos(phi) \]
\hfill \\ Note that this model uses non-normalized pixel units, so they
are in mm.

\hfill \\ References:
\begin{description}
   \item Decentering Distortion of Lenses - D.C. Brown,
          Photometric Engineering, pages 444-462, Vol. 32, No. 3, 1966
   \item Close-Range Camera Calibration - D.C. Brown,
          Photogrammetric Engineering, pages 855-866, Vol. 37, No. 8, 1971
\end{description}

\item  \textbf{Photometrix} = A model matching the conventions used by the Australis software from Photometrix.

  \begin{description}
    \item \textit{K1, K2, K3} = Radial distortion parameters.
    \item \textit{P1, P2} = Tangential distortion parameters.
    \item \textit{xp, yp} = Principal point offset.
    \item \textit{B1, B2} = Unused parameters.
  \end{description}
\hfill \\ The following equations describe the distortion:
\[ x = x(distorted) - xp \]
\[ y = y(distorted) - yp \]
\[ r^{2} = x^{2} + y^{2} \]
\[ dr = K_{1}r^{3} + K_{2}r^{5} + K_{3}r^{7} \]
\[ x(undistorted) = x + x\frac{dr}{r} + P_{1}(r^{2} +2x^{2}) + 2P_{2}xy \]
\[ y(undistorted) = y + y\frac{dr}{r} + P_{2}(r^{2} +2y^{2}) + 2P_{1}xy \]
\hfill \\ Note that this model uses non-normalized pixel units, so they
are in mm.

\item  \textbf{RPC} = A rational polynomial coefficient model.

In this model, one goes from distorted coordinates $(x, y)$ to undistorted
coordinates via the formula

\[ x(undistorted) = \frac{P_1(x, y)}{Q_1(x, y)} \]
\[ y(undistorted) = \frac{P_2(x, y)}{Q_2(x, y)} \]

The functions in the numerator and denominator are polynomials in $x$
and $y$ with certain coefficients. The degree of
polynomials can be any positive integer.

RPC distortion models can be generated as approximations to other pre-existing
models with the tool \texttt{convert\_pinhole\_model} (section \ref{convertpinholemodel}).

This tool also creates RPC to speed up the reverse operation, of going
from undistorted to distorted pixels, and those polynomial coefficients
are also saved as part of the model.

\end{itemize}

\hrule
\subsection{File Formats}
\label{file_format}
\bigskip

ASP Pinhole model files are written in an easy to work with plain text format
using the extension \texttt{.tsai}.
A sample file is shown below.

\begin{verbatim}
VERSION_4
PINHOLE
fu = 28.429
fv = 28.429
cu = 17.9712
cv = 11.9808
u_direction = 1  0  0
v_direction = 0  1  0
w_direction = 0  0  1
C = 266.943 -105.583 -2.14189
R = 0.0825447 0.996303 -0.0238243 -0.996008 0.0832884 0.0321213 0.0339869 0.0210777 0.9992
pitch = 0.0064
Photometrix
xp = 0.004
yp = -0.191
k1 = 1.31024e-04
k2 = -2.05354e-07
k3 = -5.28558e-011
p1 = 7.2359e-006
p2 = 2.2656e-006
b1 = 0.0
b2 = 0.0
\end{verbatim}

The first half of the file is the same for all Pinhole models:

\begin{itemize}{}
\item  \texttt{VERSION\_X} = A header line used to track the format of the file.
\item  \texttt{PINHOLE} = The type of camera model, so that other types can be stored with the .tsai extension.
\item  \texttt{fu, fv, cu, cv} = The first four intrinsic parameters described in the previous section.
\item  \texttt{u, v, and w\_direction} = These lines allow an additional permutation of the
axes of the camera coordinates.  By default, the positive column direction aligns with x, the
positive row direction aligns with y, and downward into the image aligns with z.
\item  \texttt{C} = The location of the camera center, usually in the geocentric coordinate system (GCC/ECEF).
\item  \texttt{R} = The rotation matrix describing the camera's absolute pose in the coordinate system (section \ref{pinholemath}).
\item  \texttt{pitch} = The pitch intrinsic parameter described in the previous section.
\end{itemize}

The second half of the file describes the lens distortion model being used.  The name of the
distortion model appears first, followed by a list of the parameters for that model.  The number
of parameters may be different for each distortion type.  Samples of each format are shown below:

\begin{itemize}{}
\item  \textbf{Null}
\begin{verbatim}
NULL
\end{verbatim}

\item  \textbf{Tsai}
\begin{verbatim}
TSAI
k1 = 1.31024e-04
k2 = -2.05354e-07
p1 = 0.5
p2 = 0.4
\end{verbatim}

\item  \textbf{Adjustable Tsai}
\begin{verbatim}
AdjustableTSAI
Radial Coeff: Vector3(1.31024e-04, 1.31024e-07, 1.31024e-08)
Tangential Coeff: Vector2(-2.05354e-07, 1.05354e-07)
Alpha: 0.4
\end{verbatim}

\item  \textbf{Brown-Conrady}
\begin{verbatim}
BrownConrady
xp = 0.5
yp = 0.4
k1 = 1.31024e-04
k2 = -2.05354e-07
k3 = 1.31024e-08
p1 = 0.5
p2 = 0.4
phi = 0.001
\end{verbatim}

\item  \textbf{Photometrix}
\begin{verbatim}
Photometrix
xp = 0.004
yp = -0.191
k1 = 1.31024e-04
k2 = -2.05354e-07
k3 = -5.28558e-011
p1 = 7.2359e-006
p2 = 2.2656e-006
b1 = 0.0
b2 = 0.0
\end{verbatim}

\item  \textbf{RPC}

\begin{verbatim}
RPC
rpc_degree = 1
image_size = 5760 3840
distortion_num_x   = 0 1 0
distortion_den_x   = 1 0 0
distortion_num_y   = 0 0 1
distortion_den_y   = 1 0 0
undistortion_num_x = 0 1 0
undistortion_den_x = 1 0 0
undistortion_num_y = 0 0 1
undistortion_den_y = 1 0 0
\end{verbatim}

This sample RPC lens distortion model represents the case of no distortion,
when the degree of the polynomials is 1, and both the distortion and undistortion
formula leave the pixels unchanged, that is, the distortion transform is
\begin{equation*}
(x, y) \to (x, y) = \left(\frac{ 0 + 1\cdot x + 0\cdot y}{1 + 0\cdot x + 0\cdot y},
 \frac{0 + 0\cdot x + 1\cdot y)}{1 + 0\cdot x + 0\cdot y}\right).
\end{equation*}

In general, if the degree of the polynomials is $n$, there are $2(n+1)(n+2)$ coefficients.
The zero-th degree coefficients in the denominator are always set to 1.

\end{itemize}{}

For several years Ames Stereo Pipeline generated Pinhole files in the binary \texttt{.pinhole} format.
That format is no longer supported.

Also in the past Ames Stereo Pipeline has generated a shorter version of the current file
format, also with the extension \texttt{.tsai}, which only supported the TSAI lens distortion model.
Existing files in that format can still be used by ASP.

Note that the \texttt{orbitviz} tool can be useful for checking the formatting of \texttt{.tsai}
files you create and to estimate the position and orientation. To inspect the orientation use
the optional \texttt{.dae} model file input option and observe the rotation of the 3D model.

\subsection{How the Pinhole model is applied}
\label{pinholemath}

As mentioned in section \ref{file_format}, the ASP Pinhole models store
the focal length as $fu$ and $fv$, the optical center $(cu, cv)$ (which
is the pixel location at which the ray coming from the center of the
camera is perpendicular to the image plane, in units of the pixel
pitch), the vector $C$ which is the camera center in world coordinates
system, and the matrix $R$ that is the transform from camera to world
coordinates.

To go in more detail, a point $Q$ in the camera coordinate system gets transformed
to a point $P$ in the world coordinate system via:
\begin{equation*}
 P = RQ + C
\end{equation*}

Hence, to go from world to camera coordinates one does:
\begin{equation*}
 Q = R^{-1}  P - R^{-1}  C
\end{equation*}

From here the pixel location is computed as:

\begin{equation*}
 \frac{1}{p} \left(fu \frac{Q_1}{Q_3} + cu, fv \frac{Q_2}{Q_3} + cv\right)
\end{equation*}

where $p$ is the pixel pitch.

\section{Panoramic Camera Model}
\label{panoramic}

ASP also supports a simple panoramic/optical bar camera model for use with images such as
the declassified Corona KH4 and Keyhole KH9 images. It implements the model
from \cite{schenk2003rigorous} with the motion compensation from \cite{sohn2004mathematical}.

Such a model looks as follows:
\begin{verbatim}
VERSION_4
OPTICAL_BAR
image_size = 110507 7904
image_center = 55253.5 3952
pitch = 7.0e-06
f = 0.61000001430511475
scan_time = 0.5
forward_tilt = -0.261799
iC = -1047140.9611702315 5508464.4323527571 3340425.4078937685
iR = -0.96635634448923746 -0.16918164442572045 0.1937343197650008 -0.23427205529446918 0.26804084264169648 -0.93448954557235941 0.10616976770014927 -0.94843643849513648 -0.29865750042675621
speed = 7700
mean_earth_radius = 6371000
mean_surface_elevation = 4000
motion_compensation_factor = 1.0
scan_dir = left
\end{verbatim}

Here, the image size and center are given in pixels, with the width followed by the
height. The pixel pitch and focal length \texttt{f} are in meters. The
scan time is seconds, the forward tilt is in radians,
the speed is in meters per second, and the Earth radius and mean
surface elevation are in meters. The initial camera center \texttt{iC} is
in meters, and the rotation matrix \texttt{iR} stores the absolute pose.
\texttt{scan\_dir} must be set to 'left' or 'right'.
\texttt{scan\_dir} and \texttt{use\_motion\_compensation}
control how the sensor model accounts accounts for the motion of the satellite
during the image scan.  Without the benefit of detailed historical documents it may
require experimentation to find the good initial values for these cameras.  When using
\texttt{bundle\_adjust}, the intrinsic parameters that are solved for are
\texttt{speed}, \texttt{motion\_compensation\_factor}, and \texttt{scan\_time}.


\section{The Classical Banach Spaces}
\subsection{The $L^p$ Spaces}
  \paragraph{1.}
  \begin{proof}
    Put $S=\|f\|_\infty$ and $T=\|g\|_\infty$. Then $|f(t)|\le S$ and $|g(t)|\le
    T$ a.e. Hence, $S+T\ge |f(t)|+|g(t)| \ge |f(t)+g(t)|$ a.e. Namely, $m\{t:
    \,|f(t)+g(t)|>S+T\}=0$. Thus, $S+T\ge \|f+g\|_\infty$ by the definition of
    $\esssup$.
  \end{proof}

  \paragraph{2.}
  \begin{proof}
    Put $S=\|f\|_\infty$. Since $S\ge |f|$ a.e.,
    \[
      \|f\|_p=
      \left\{\int_0^1|f|^p\right\}^{1/p} \le
      \left\{\int_0^1S^p\right\}^{1/p} = S.
    \]
    Therefore, $\uplim_{p\to\infty}\|f\|_p \le S$. For the converse part, let
    $\vep$ be any positive number. Then the measure $\delta$ of $E=\{t:\,|f(t)|>
    S-\vep\}$ is positive. Hence,
    \[
      \left\{\int_0^1|f|^p\right\}^{1/p} \ge
      \left\{\int_E|f|^p\right\}^{1/p} \ge
      \delta^{1/p}(S-\vep) \to
      S-\vep\quad\text{as $p\to\infty$.}
    \]
    Hence, $\lowlim_{p\to\infty}\ge S$, completing the proof.
  \end{proof}

  \paragraph{3.}
  \begin{proof}
    \[
      \|f+g\|_1 = \int|f+g| \le \int|f|+\int|g| = \|f\|_1+\|g\|_1.
    \]
  \end{proof}

  \paragraph{4.}
  \begin{proof}
    For every $M>\|g\|_\infty$, $|g|\le M$ a.e. Hence,
    \[
      \int|fg|\le M\int|f| = \|f\|_1 M.
    \]
    Since the choice of $M$ is arbitrary, $\int|fg|\le \|f\|_1\|g\|_\infty$.
  \end{proof}
% end

\subsection{The Minkowski and Hölder Inequalities}
  \paragraph{8}
  \begin{proof}
    $\,$\par
    (a) The logarithm function is concave, so
    \[
      \log(a^p/p+b^q/q) \ge \frac{1}{p}\log a^p + \frac{1}{q}\log b^q = \log ab.
    \]
    Taking $\exp$ on the both sides yields the inequality. The equality holds
    iff $a^p=b^q$. \par
    (b) The case where $p=\infty$ has been proved in Problem 4 and the case
    where $\|f\|_p=0$ or $\|g\|=0$ is straightforward. Hence, we assume that
    $1<p,q<\infty$ and $\|f\|_p\|g\|_q\ne 0$. \par
    Suppose $\alpha=\|f\|_p$ and $\beta=\|g\|_q$. By Young's inequality,
    \[
      \left|\frac{fg}{\alpha\beta}\right| \le
      \frac{1}{p}\left(\frac{|f|}{\alpha}\right)^p +
      \frac{1}{q}\left(\frac{|g|}{\beta}\right)^q
    \]
    for every $x$. Therefore,
    \begin{equation}
      \label{eq:6.2.8.1}
      \int|fg| = \alpha\beta\int\left|\frac{fg}{\alpha\beta}\right|
      \le \alpha\beta\int\left\{\frac{1}{p}\left(\frac{|f|}{\alpha}\right)^p +
      \frac{1}{q}\left(\frac{|g|}{\beta}\right)^q\right\}
      = \alpha\beta.
    \end{equation}
    The equality holds iff the equality in Young's inequality holds a.e. iff
    $\beta|f|^p = \alpha|g|^q$ a.e.\par
    (c) Let $p\hp=1/p$ and $q\hp=1-p\hp=-q/p$. Then for any nonnegative $c$ and
    $d$, by Young's inequality,
    \[
      cd \le \frac{c^{p\hp}}{p\hp}+\frac{d^{q\hp}}{q\hp}=
      pc^{1/p} - \frac{p}{q}d^{-q/p} \quad\Rightarrow\quad
      c^{1/p} \ge \frac{cd}{p} + \frac{d^{-q/p}}{q}.
    \]
    Putting $c=(ab)^p$ and $d=b^{-p}$ yields the desired inequality.\par
    (d) Just reverse the inequality in \eqref{eq:6.2.8.1}.
  \end{proof}
% end

\subsection{Convergence and Completeness}
  \paragraph{9.}
  \begin{proof}
    Suppose $\langle f_n\rangle\subset X$ converges to $f\in X$. Namely, for
    every $\vep>0$, there exists some $N$ such that for all $n>N$, $\|f_n-f\|<
    \vep$. Hence, for every $n,m>N$, by Minkowski inequality,
    \[
      \|f_n-f_m\| \le \|f_n-f\|+\|f-f_m\| < 2\vep.
    \]
    Hence, $\langle f_n\rangle$ is a Cauchy sequence.
  \end{proof}

  \paragraph{10.}
  \begin{proof}
    Suppose $f_n\to f$. Then $M_n=\|f_n-f\|_\infty=\esssup|f_n-f|\to 0$. Let
    $E_n=\{x:\,|f_n(x)-f(x)|>M_m\}$, each of which is with measure zero. And
    therefore $E=\bigcup_{n=1}^\infty E_n$ is with measure zero. Note that
    $\tilde{E}=\{x:\,|f_n(x)-f(x)|<M_n,\forall\,n\}$, which implies the uniform
    convergence of $f_n$ since $M_n\to 0$.\par
    For the converse part, the uniform convergence on $\tilde{E}$ implies that
    for every $\vep>0$, there exists some $N$ such that for every $n>N$ and $x
    \in\tilde{E}$, $|f_n(x)-f(x)|<\vep$. Since $mE=0$, this implies $\|f_n-f
    \|_\infty=\esssup|f_n(x)-f(x)|<\vep$. Hence, $f_n\to f$ in $L^\infty$.
  \end{proof}

  \paragraph{11.}
  \begin{proof}
    Let $\langle f_n\rangle\subset L^\infty$ be absolutely summable. Put $M_n=
    \|f_n\|_\infty$ and $A_n=\{t:\,|f_n(t)|>M_n\}$. By the definition of $\|
    \cdot\|_\infty$, $mA_n=0$. Hence, $A=\bigcup_{n=1}^\infty A_n$ is of measure
    zero. \par
    Note that $|f_n(x)|\le M_n$ for every $n$ and $x\in E\setminus A$. Thus, by
    the Weierstrass M-test, $\sum_{n=1}^\infty f_n$ converges uniformly. Hence,
    on $E\setminus A$, $\sup|\sum_{n=1}^\infty f_n - \sum_{n=1}^N f_n|\to 0$ as
    $N\to\infty$. Since $mA=0$, this implies the summability of $\langle f_n
    \rangle$.
  \end{proof}

  \paragraph{13.}
  \begin{proof}
    Suppose $\langle f_n\rangle\subset C$ be absolutely summable. Since for
    every $x$, $0\le|f_n(x)|\le\|f_n\|$, $\langle f_n\rangle$ is uniformly
    convergent on $[0,1]$. Put $s=\sum_{n=1}^\infty f_n$. Since each $f_n$ is
    continuous, so is $s$. Therefore, $s\in C$. \par
    For every $\vep>0$, there exists some $N$ such that for every $n>N$ and $x
    \in[0,1]$, $\left|s(x)-\sum_{k=1}^nf_k(x)\right|<\vep$. Hence, $\|s-
    \sum_{k=1}^nf_k\|<\vep$. Thus, $\langle f_n\rangle$ is summable and
    therefore $C$ is a Banach space.
  \end{proof}

  \paragraph{16.}
  \begin{proof}
    Since $\|f_n-f\| \ge |\|f_n\|-\|f\||$, $f_n\to f$ in $L^p$ implies $\|f_n\|
    \to \|f\|$. For the converse part, note that $2^p(|f_n|^p+|f|^p)-|f_n-f|^p
    \ge 0$ and for almost every $x$,
    \[
      2^p(|f_n|^p+|f|^p)-|f_n-f|^p \to 2^{p+1}|f|^p.
    \]
    By Fatou's Lemma,
    \begin{align*}
      2^{p+1}\|f\|^p = 2^{p+1}\int|f|^p
      &\le \lowlim\int\{2^p(|f_n|^p+|f|^p)-|f_n-f|^p\} \\
      &= 2^{p+1}\|f\|^p - \uplim\|f_n-f\|^p.
    \end{align*}
    Hence, $\uplim\|f_n-f\|^p \le 0$. Since clear that $\lowlim\|f_n-f\|^p\ge0$,
    $\lim\|f_n-f\|=0$, i.e., $f_n \to f$ in $L^p$.
  \end{proof}

  \paragraph{17.}
    I assume that $1/p+1/q=1$.
  \begin{proof}
    Since $g\in L^p$, $|g|^q$ is integrable on $E=[0,1]$ and therefore for every
    $\vep>0$, there exists some $\delta$ such that for every $A\subset E$ with
    $mA<\delta$, $\int_A|g|^q<\vep$. Meanwhile, since $f_n(x)\to f(x)$ for
    almost every $x$, by Egoroff's Theorem, there exists some $A\subset E$ with
    $mA<\delta$ such that $f_ng$ converges to $fg$ uniformly on $E\setminus A$.
    \par
    From the uniform convergence we conclude
    \begin{equation}
      \label{eq:6.17.1}
      \int_{E\setminus A} fg = \lim_{n\to\infty}\int_{E\setminus A}f_ng.
    \end{equation}
    Meanwhile, by Hölder inequality,
    \begin{align*}
      \left|\int_A (f-f_n)g\right| \le \int_A|(f-f_n)g|
      &\le\left\{\int_A|f_n-f|^p\right\}^{1/p}\left\{\int_A|g|^q\right\}^{1/q}
      \le M\vep^{1/q}.
    \end{align*}
    Hence, \eqref{eq:6.17.1} can be extended to $E$.\par
    For $p=1$, this is not true. $f_n=n\chi_{[0,1/n]}$ and $g=\chi_{[0,1]}$
    gives a counterexample.
  \end{proof}

  \paragraph{18.}
  \begin{proof}
    By Minkowski inequality,
    \[
      \|g_nf_n-gf\| = \|g_n(f_n-f)+(g_n-g)f\| \le \|g_n(f_n-f)\|+\|(g_n-g)f\|.
    \]
    Fix $\vep>0$. Since $f,g_n,g\in L^p$, $|g_n-g|^p|f|^p$ is integrable and
    therefore there exists some $\delta>0$ such that for all subsets with
    measure $<\delta$, the integral of over it $<\vep$. Meanwhile, since $g_n\to
    g$ a.e., by Egoroff's Theorem, there exists some $A\subset E=[0,1]$ with $mA
    <\delta$ such that $g_n\to g$ uniformly on $E\setminus A$ and therefore
    there exists some $N_1>0$ such that for all $n>N_1$, $|g_n(x)-g(x)|^p<\vep$
    for $x\in E\setminus A$. Thus, for every $n>N_1$,
    \begin{align*}
      \|(g_n-g)f\|
      &=\left\{\int_{E\setminus A}|g_n-g|^p|f|^p\right\}^{1/p} +
        \left\{\int_A|g_n-g|^p|f|^p\right\}^{1/p} \\
      & \le \sqrt[p]{\vep}\|f\| + \sqrt[p]{\vep} \le (\|f\|+1)\vep.
    \end{align*}
    Since $|g_n|\le M$, $\|g_n(f_n-f)\|\le M\|f_n-f\|$. And since $f_n\to f$ in
    $L^p$, there exists some $N_2>0$ such that for all $n>N_2$, $\|f_n-f\|<
    \vep$. Put $N=\max(N_1,N_2)$, then for every $n>N$,
    \[
      \|g_nf_n-gf\| \le (\|f\|+1+M)\vep.
    \]
    Hence, $g_nf_n\to gf$ in $L^p$.
  \end{proof}
% end

\subsection{Approximation in $L^p$}
  \paragraph{19.}
  \begin{proof}
    Since $\|T_\Delta f\|\le \|T_\Delta|f|\|$ and $\|f\|=\||f|\|$, we may assume
    without loss of generality that $f\ge 0$. For $p>1$, by Jensen's inequality,
    \begin{align*}
      \|T_\Delta f\|_p^p
      &= \sum_{k=1}^m\int_{\xi_{k-1}}^{\xi_k}
      \left(\frac{1}{\xi_k-\xi_{k-1}}\int_{\xi_{k=1}}^{\xi_k}f\right)^p \\
      &\le \sum_{k=1}^m\int_{\xi_{k-1}}^{\xi_k}
      \frac{1}{\xi_k-\xi_{k-1}}\int_{\xi_{k-1}}^{\xi_k}f^p \\
      &= \sum_{k=1}^m\int_{\xi_{k-1}}^{\xi_k}f^p \\
      &= \int_0^1f^p = \|f\|_p^p.
    \end{align*}
  \end{proof}
% end


\chapter{Good behaviour inside math mode}

\section{Using the right commands and symbols}

\subsection{General symbols}
\index{symbols}

Use the correct symbols.
\Cref{wrong symbol list} shows some popular sources of this problem.
\begin{table}[tb]
  \begin{center}
  \begin{tabular}{@{}lllll@{}}
      \toprule
      \theading{symbol}
      &
      \multicolumn{2}{c}{\theading{right commands}}
      &
      \multicolumn{2}{c}{\theading{wrong commands}}
    \\
    \midrule
      element relation
      &
      \comname{in}%
      \massindex{in}[\comname]
      &
      $\in$
      &
      \comname{epsilon}%
      \massindex{epsilon}[\comname]
      &
      $\epsilon$
    \\
      {}
      &
      {}
      &
      {}
      &
      \comname{varepsilon}%
      \massindex{varepsilon}[\comname]
      &
      $\varepsilon$
    \\
      {}
      &
      \comname{ni}%
      \massindex{ni}[\comname]
      &
      $\ni$
      &
      \comname{backepsilon}%
      \massindex{backepsilon}[\comname]
      &
      $\backepsilon$
    \\
    \cmidrule(lr){2-3} \cmidrule(l){4-5}
      empty set
      &
      \comname{emptyset}%
      \massindex{emptyset}[\comname]
      &
      $\emptyset$
      &
      \comname{phi}%
      \massindex{phi}[\comname]
      &
      $\phi$
    \\
      {}
      &
      \comname{varnothing}%
      \massindex{varnothing}[\comname]
      &
      $\varnothing$
      &
      {}
      &
      {}
    \\
    \cmidrule(lr){2-3} \cmidrule(l){4-5}
      set difference
      &
      \inlinecode{A {\tbs}setminus B}%
      \massindex{setminus}[\comname]
      &
      $A \setminus B$
      &
      \inlinecode{A {\tbs}backslash B}%
      \massindex{backslash}[\comname]
      &
      $A \backslash B$
    \\
      {}
      &
      \inlinecode{A {\tbs}smallsetminus B}%
      \massindex{smallsetminus}[\comname]
      &
      $A \smallsetminus B$
      &
      {}
      &
      {}
    \\
      {}
      &
      \inlinecode{A - B}\index{minus sign}
      &
      $A - B$
      &
      {}
      &
      {}
    \\
    \cmidrule(lr){2-3} \cmidrule(l){4-5}
      implication
      &
      \comname{implies}%
      \massindex{implies}[\comname]
      &
      $\implies$
      &
      \comname{Rightarrow}%
      \massindex[arrows]{Rightarrow}[\comname]
      &
      $\Rightarrow$
    \\
      {}
      &
      {}
      &
      {}
      &
      \inlinecode{=>}
      &
      $=>$
    \\
      {}
      &
      \comname{impliedby}%
      \massindex{impliedby}[\comname]
      &
      $\impliedby$
      &
      \comname{Leftarrow}%
      \massindex[arrows]{Leftarrow}[\comname]
      &
      $\Leftarrow$
    \\
      {}
      &
      {}
      &
      {}
      &
      \inlinecode{<=}
      &
      $<=$
    \\
    \cmidrule(lr){2-3} \cmidrule(l){4-5}
      equivalence
      &
      \comname{iff}%
      \massindex{iff}[\comname]
      &
      $\iff$
      &
      \comname{Leftrightarrow}%
      \massindex[arrows]{Leftrightarrow}[\comname]
      &
      $\Leftrightarrow$
    \\
      {}
      &
      {}
      &
      {}
      &
      \inlinecode{<=>}
      &
      <=>
    \\
    \cmidrule(lr){2-3} \cmidrule(l){4-5}
      definition
      &
      \comname{coloneqq}%
      \massindex{coloneqq}[\comname]
      &
      $\coloneqq$
      &
      \inlinecode{:=}
      &
      $:=$
    \\
      {}
      &
      \comname{eqqcolon}%
      \massindex{eqqcolon}[\comname]
      &
      $\eqqcolon$
      &
      \inlinecode{=:}
      &
      $=:$
    \\
    \cmidrule(lr){2-3} \cmidrule(l){4-5}
      norm
      &
      \inlinecode{{\tbs}| x {\tbs}|}%
      \massindex[delimiters]{\indexline}[\comname]
      &
      $\| x \|$
      &
      \inlinecode{|| x ||}
      &
      $|| x ||$
    \\
      {}
      &
      \inlinecode{{\tbs}lVert x {\tbs}rVert}%
      \massindex[delimiters]{lVert}[\comname]%
      \massindex[delimiters]{rVert}[\comname]
      &
      {}
      &
      {}
      &
      {}
    \\
    \cmidrule(lr){2-3} \cmidrule(l){4-5}
      pointy brackets
      &
      \inlinecode{{\tbs}langle x {\tbs}rangle}%
      \massindex[delimiters]{langle}[\comname]%
      \massindex[delimiters]{rangle}[\comname]
      &
      $\langle x \rangle$
      &
      \inlinecode{< x >}
      &
      $< x >$
    \\
    \cmidrule(lr){2-3} \cmidrule(l){4-5}
      infinity
      &
      \comname{infty}%
      \massindex{infty}[\comname]
      &
      $\infty$
      &
      \inlinecode{oo}
      &
      $oo$
    \\
    \cmidrule(lr){2-3} \cmidrule(l){4-5}
      function colon
      &
      \inlinecode{f {\tbs}colon X {\tbs}to Y}%
      \massindex{colon}[\comname]%
      \massindex{ni}[\comname]
      &
      $f \colon X \to Y$
      &
      \inlinecode{f : X {\tbs}to Y}
      &
      $f : X \to Y$
    \\
    \bottomrule
  \end{tabular}
  \end{center}
  \caption{Right symbols and wrong symbols.}
  \label{wrong symbol list}
\end{table}
Note that the commands~\comname{rightarrow}\massindex[arrows]{rightarrow}[\comname] and~\comname{to}\massindex[arrows]{to}[\comname] give the same arrow.
So use whichever is more appropriate in the given situation.

\subsection{Operations}

Many mathematical operations have both a binary version and an operator version, where the operation can range over some index set.
One should not confuse the two of them.
\Cref{binary vs operator} shows some popular binary operations and their operator counterpart.
\begin{table}[tb]
  \begin{center}
    \begingroup
    \renewcommand{\arraystretch}{1.3}
    \begin{tabular}{@{}llclc@{}}
      \toprule
      \theading{operation}
      &
      \multicolumn{2}{c}{\theading{binary}}
      &
      \multicolumn{2}{c}{\theading{generalized}}
      \\
      \cmidrule(lr){2-3}
      \cmidrule(l){4-5}
      sum
      &
      \inlinecode{+}
      &
      $a + b$
      &
      \comname{sum}
      &
      $\sum_i x_i$
      \\
      multiplication
      &
      \comname{cdot}
      &
      $a \cdot b$
      &
      \comname{prod}
      &
      $\prod_i x_i$
      \\
      direct sum
      &
      \comname{oplus}
      &
      $A \oplus B$
      &
      \comname{bigoplus}
      &
      $\bigoplus_i X_i$
      \\
      tensor product
      &
      \comname{otimes}
      &
      $A \otimes B$
      &
      \comname{bigotimes}
      &
      $\bigotimes_i X_i$
      \\
      wedge
      &
      \comname{wedge}
      &
      $a \wedge b$
      &
      \comname{bigwedge}
      &
      $\bigwedge_i X_i$
      \\
      union
      &
      \comname{cup}
      &
      $A \cup B$
      &
      \comname{bigcup}
      &
      $\bigcup_i X_i$
      \\
      intersection
      &
      \comname{cap}
      &
      $A \cap B$
      &
      \comname{bigcap}
      &
      $\bigcap_i X_i$
      \\
      product
      &
      \comname{times}
      &
      $A \times B$
      &
      \comname{prod}
      &
      $\prod_i X_i$
      \\
      {}
      &
      \comname{sqcap}
      &
      $A \sqcap B$
      &
      \comname{bigsqcap}
      &
      $\bigsqcap_i X_i$
      \\
      coproduct
      &
      \comname{amalg}
      &
      $A \amalg B$
      &
      \comname{coprod}
      &
      $\coprod_i X_i$
      \\
      {}
      &
      \comname{sqcup}
      &
      $A \sqcup B$
      &
      \comname{bigsqcup}
      &
      $\bigsqcup_i X_i$
      \\
      \bottomrule
    \end{tabular}
    \endgroup
  \end{center}
  \caption{Binary operation and operator version.}
  \label{binary vs operator}
\end{table}
The command \comname{bigsqcap} requires the package \packname{stmaryrd}.

\subsection{Negations}

For some mathematical symbols there also exists a negated version, which is formed by diagonal line through the symbol.
A general way of introducing such a line is the command \comname{not}\massindex[negation]{not}[\comname].
\begin{showlatex}*{Using \comname{not}}
  It follows that $A \not\ni x$.
\end{showlatex}
But in practice this command should seldom be used, as it often produces very bad looking output.
Consider the following example:
\begin{showlatex}{Why not to use \comname{not}}
  Hence $A \not\implies B$.
\end{showlatex}
There are two solutions to this problem:

Many symbols already have a predefined lined-out version available.
A few of them are collected in~\cref{negation list}.
\begin{table}[tb]
  \begin{center}
  \begin{tabular}{@{}lclc@{}}
    \toprule
      \multicolumn{2}{c}{\theading{right}}
    &
      \multicolumn{2}{c}{\theading{wrong}}
    \\
    \cmidrule(r){1-2} \cmidrule(l){3-4}
      \comname{notin}%
      \massindex[negation]{notin}[\comname]
      &
      $\notin$
      &
      \inlinecode{{\tbs}not{\tbs}in}
      &
      $\not\in$
    \\
      \comname{nexists}%
      \massindex[negation]{nexists}[\comname]
      &
      $\nexists$
      &
      \inlinecode{{\tbs}not{\tbs}exists}
      &
      $\not\exists$
    \\
      \comname{neq}%
      \massindex[negation]{neq}[\comname]
      &
      $\neq$
      &
      \inlinecode{{\tbs}not =}
      &
      $\not =$
    \\
      \comname{nleq}%
      \massindex[negation]{nleq}[\comname]
      &
      $\nleq$
      &
      \inlinecode{{\tbs}not{\tbs}leq}
      &
      $\not\leq$
    \\
      \comname{nrightarrow}%
      \massindex[negation,arrows]{notrightarrow}[\comname]
      &
      $\nrightarrow$
      &
      \inlinecode{{\tbs}not{\tbs}rightarrow}
      &
      $\not\rightarrow$
    \\
    \bottomrule
  \end{tabular}
  \end{center}
  \caption{Negated versions of popular symbols.}
  \label{negation list}
\end{table}
If the required symbol is not predefined then the command~\comname{centernot}\massindex[negation]{centernot}[\comname] from the package~\packname{centernot}\massindex[packages]{centernot}[\packname] often produces better looking output than~\comname{not}:
\begin{showlatex}{Using the command~\comname{centernot}}
  Consider
  \[
    A \centernot\ni x
    \quad\text{versus}\quad
    \quad
    A \not\ni x \,.
  \]
  Consider also
  \[
    A \centernot\implies B
    \quad\text{versus}\quad
    A \not\implies B \,.
  \]
\end{showlatex}

\subsection{Don’t use \comtitle{limits}}

People seem to think that they have to put \comname{limits}\massindex[limits]{limits}[\comname] after a command to add limits to it:
\begin{showlatex}*{Using~\comname{limits}}
\[
  \sum\limits_{k=1}^n k
  =
  \frac{n(n+1)}{2}
\]
\end{showlatex}
But this is not only unnecessary, but also dangerous.
It is unnecessary because (most of) the commands in questions already have this functionality built in:
\begin{showlatex}*{Using built-in limits}
\[
  \sum_{k=1}^n k
  =
  \frac{n(n+1)}{2}
\]
\end{showlatex}
These built-in limits have the advantage that they can distinguish between inline math and display math:
\begin{showlatex}*{Inline math vs.\ display math with built-in limits}
The sum $\sum_{k=0}^n 2^k = 2^{n+1} - 1$ is inline while the sum
\[
  \sum_{k=0}^n 3^k
  \neq
  3^{n+1} - 1
\]
is in display mode.
\end{showlatex}
We can see that the inline version does not only use the smaller summation sign but also sets the limits to the right of the summation sign.
This is a feature which \comname{limits} version is missing:
\begin{showlatex}*{Inline math with~\comname{limits}}
So here is some text which will generate some lines.
The text itself isn’t important, but we really want it to fill some lines.
The important thing is the sum $\sum\limits_{k=0}^n k$.
Well, not really the sum itself, but the use of \texttt{{\textbackslash}limits} for typesetting it.
\end{showlatex}
The limits are still placed above the top and below the summation sign.
This has its price:
The line in which the sum resides breaks the usual vertical space between lines, which gives the text an inconsistent and unorganized look.
Compare this to the version without \comname{limits}:
\begin{showlatex}*{Inline with built-in limits}
So here is some text which will generate some lines.
The text itself isn’t important, but we really want it to fill some lines.
The important thing is the sum $\sum_{k=0}^n k$.
Well, not really the sum itself, but use of built-in limits for typesetting it.
\end{showlatex}
Here the line distance is nicely consistent and pleasing to the eye.

The usual predefined commands on which one would expect limits already have them defined, e.g.\ \comname{sum}, \comname{prod} or \comname{lim}, as the following example shows:
\begin{showlatex}{Inline vs.\ display for different commands with built-in limits}
Compare the inline versions $\sum_{k=0}^n k$ and $\prod_{k=1}^n k$ and $\lim_{n \to \infty} a_n$ with the display versions
\[
  \sum_{k=0}^n k \,,
  \quad
  \prod_{k=1}^n k \,,
  \quad
  \lim_{n \to \infty} a_n \,.
\]
\end{showlatex}
If a new commands are defined with~\comname{DeclareMathOperator}\massindex[defining commands]{DeclareMathOperator}[\comname] or~\comname{operatorname}\massindex[defining commands]{operatorname}[\comname] then one can make them support limits by using \comname{DeclareMathOperator*}\massindex[defining commands, limits]{DeclareMathOperator*}[\comname] and \comname{operatorname*}\massindex[defining commands, limits]{operatorname*}[\comname] instead.
Suppose for example that we have made in the preamble the following definition:
\begin{showcode}{Using \comname{DeclareMathOperator*} to define~\comname{colim}}
\DeclareMathOperator*{\colim}{colim}
\end{showcode}
We can then do the following:
\begin{showlatex}{Using~\comname{colim}}
Inline we have $\colim_{X' \leq X} F(X')$ and in display mode we get
\[
  \colim_{X' \leq X} F(X') \,.
\]
\end{showlatex}
The command~\comname{operatorname*} was named~\comname{operatornamewithlimits} in the past, but this name is deprecated.

% If for some extremly strange reason one \emph{really} needs the limits to be in display style, then one should commit to it by using \comname{displaystyle}.
% Consider the following example:
% \begin{showlatex}{Forcing displaystyle with \comname{displaystyle}}
% \[
%   \begin{pmatrix}
%     \frac{n^2+1}{n^2 + 2}
%     &
%     \frac{n^2+2}{n^2 + 3}
%     \\
%     \frac{n^2+2}{n^2 + 3}
%     &
%     \frac{n^2+3}{n^2 + 4}
%   \end{pmatrix}
%   =
%   \begin{pmatrix}
%     \displaystyle
%     \frac{n^2+1}{n^2 + 2}
%     &
%     \displaystyle
%     \frac{n^2+2}{n^2 + 3}
%     \\[1.5em]
%     \displaystyle
%     \frac{n^2+2}{n^2 + 3}
%     &
%     \displaystyle
%     \frac{n^2+3}{n^2 + 4}
%   \end{pmatrix}
% \]
% \end{showlatex}
% In the above example we have put~\comname{displaystyle} before every matrix entry to ensure that it has the usual style of display mathematics.
% We have also put replaced the basic line break~\comname{\tbs} by~\inlinecode{{\tbs}{\tbs}[1.5em]} for some additional spacing between the two rows of the resulting matrix.
% (Otherwise the fractions are far too close.)

\subsection{Use extensible arrows instead of \comtitle{overset} and \comtitle{underset}}
\label{extensible arrows}

Some people put text above or under arrows by wrongly using the commands~\comname{overset}\massindex{overset}[\comname] or \comname{underset}\massindex{underset}[\comname]:
\begin{showlatex}{Using~\comname{overset} and~\comname{underset} to put text above or below an arrow}
\[
  X
  \overset{f}{\longrightarrow}
  Y
  \underset{g \circ h \circ k}{\longrightarrow}
  Z
\]
\end{showlatex}
We can see above that the length of the arrow does not adjust to the size of the text above it or below it.
The proper way to put text on top of an arrow or bellow an arrow of the form~\enquote{$\to$} is therefore the command~\comname{xrightarrow}\massindex[arrows]{xrightarrow}[\comname]:
\begin{showlatex}{Using~\comname{xrightarrow} to put text above or below an arrow}
\[
  X
  \xrightarrow{f}
  Y
  \xrightarrow[g \circ h \circ k]{}
  Z \,.
\]
\end{showlatex}
The package~\packname{amsmath}\massindex[packages]{amsmath}[\packname] defines only the two most basic extensible arrows\index{extensible arrows}\index{arrows!extensible}.
Many more kinds of extensible arrows are provided by the package~\packname{mathtools}\massindex[packages]{mathtools}[\packname] and some more are contained in the package~\packname{extarrows}\massindex[packages]{extarrows}[\packname].
An overview of the various kinds of extensible arrows can be found in \cref{extensible arrow table}.
\begin{table}[tb]
  \begin{center}
  \begingroup
  \renewcommand{\arraystretch}{0.9}
  \begin{tabular}{@{}lccc@{}}
    \toprule
    \packname{amsmath}
    &
    \begin{tabular}{@{}c@{}}
      $A \xrightarrow{f} B$
      \\
      \comname{xrightarrow}%
      \massindex[arrows]{xrightarrow}[\comname]
    \end{tabular}
    &
    \begin{tabular}{@{}c@{}}
      $A \xleftarrow{f} B$
      \\
      \comname{xleftarrow}%
      \massindex[arrows]{xleftarrow}[\comname]
    \end{tabular}
    &
    {}
    \\[1.5em]
%     \midrule
    \packname{mathtools}
    &
    \begin{tabular}{@{}c@{}}
      $A \xmapsto{f} B$
      \\
      \comname{xmapsto}%
      \massindex[arrows]{xmapsto}[\comname]
    \end{tabular}
    &
    \begin{tabular}{@{}c@{}}
      $A \xleftrightarrow{f} B$
      \\
      \comname{xleftrightarrow}%
      \massindex[arrows]{xleftrightarrow}[\comname]
    \end{tabular}
    &
    \begin{tabular}{@{}c@{}}
      $A \xRightarrow{f} B$
      \\
      \comname{xRightarrow}%
      \massindex[arrows]{xRightarrow}[\comname]
    \end{tabular}
    \\[1.5em]
    {}
    &
    \begin{tabular}{@{}c@{}}
      $A \xLeftarrow{f} B$
      \\
      \comname{xLeftarrow}%
      \massindex[arrows]{xLeftarrow}[\comname]
    \end{tabular}
    &
    \begin{tabular}{@{}c@{}}
      $A \xLeftrightarrow{f} B$
      \\
      \comname{xLeftrightarrow}%
      \massindex[arrows]{xLeftrightarrow}[\comname]
    \end{tabular}
    &
    \begin{tabular}{@{}c@{}}
      $A \xhookleftarrow{f} B$
      \\
      \comname{xhookleftarrow}%
      \massindex[arrows]{xhookleftarrow}[\comname]
    \end{tabular}
    \\[1.5em]
    {}
    &
    \begin{tabular}{@{}c@{}}
      $A \xhookrightarrow{f} B$
      \\
      \comname{xhookrightarrow}%
      \massindex[arrows]{xhookrightarrow}[\comname]
    \end{tabular}
    &
    \begin{tabular}{@{}c@{}}
      $A \xrightharpoondown{f} B$
      \\
      \comname{xrightharpoondown}%
      \massindex[arrows]{xrightharpoondown}[\comname]
    \end{tabular}
    &
    \begin{tabular}{@{}c@{}}
      $A \xrightharpoonup{f} B$
      \\
      \comname{xrightharpoonup}%
      \massindex[arrows]{xrightharpoonup}[\comname]
    \end{tabular}
    \\[1.5em]
    {}
    &
    \begin{tabular}{@{}c@{}}
      $A \xrightleftharpoons{f} B$
      \\
      \comname{xrightleftharpoons}%
      \massindex[arrows]{xrightleftharpoons}[\comname]
    \end{tabular}
    &
    \begin{tabular}{@{}c@{}}
      $A \xleftharpoondown{f} B$
      \\
      \comname{xleftharpoondown}%
      \massindex[arrows]{xleftharpoondown}[\comname]
    \end{tabular}
    &
    \begin{tabular}{@{}c@{}}
      $A \xleftharpoonup{f} B$
      \\
      \comname{xleftharpoonup}%
      \massindex[arrows]{xleftharpoonup}[\comname]
    \end{tabular}
    \\[1.5em]
    {}
    &
    \begin{tabular}{@{}c@{}}
      $A \xleftrightharpoons{f} B$
      \\
      \comname{xleftrightharpoons}%
      \massindex[arrows]{xleftrightharpoons}[\comname]
    \end{tabular}
    &
    {}
    &
    {}
    \\[1.5em]
%     \midrule
    \packname{extarrows}
    &
    \begin{tabular}{@{}c@{}}
      $A \xlongequal{f} B$
      \\
      \comname{xlongequal}%
      \massindex[arrows]{xlongequal}[\comname]
    \end{tabular}
    &
    \begin{tabular}{@{}c@{}}
      $A \xleftrightarrow{f} B$
      \\
      \comname{xleftrightarrow}%
      \massindex[arrows]{xleftrightarrow}[\comname]
    \end{tabular}
    &
    \begin{tabular}{@{}c@{}}
      $A \xLeftrightarrow{f} B$
      \\
      \comname{xLeftrightarrow}%
      \massindex[arrows]{xLeftrightarrow}[\comname]
    \end{tabular}
    \\[1.5em]
    {}
    &
    \begin{tabular}{@{}c@{}}
      $A \xlongleftarrow{f} B$
      \\
      \comname{xlongleftarrow}%
      \massindex[arrows]{xlongleftarrow}[\comname]
    \end{tabular}
    &
    \begin{tabular}{@{}c@{}}
      $A \xlongrightarrow{f} B$
      \\
      \comname{xlongrightarrow}%
      \massindex[arrows]{xlongrightarrow}[\comname]
    \end{tabular}
    &
    \begin{tabular}{@{}c@{}}
      $A \xlongleftrightarrow{f} B$
      \\
      \comname{xlongleftrightarrow}%
      \massindex[arrows]{xlongleftrightarrow}[\comname]
    \end{tabular}
    \\[1.5em]
    {}
    &
    \begin{tabular}{@{}c@{}}
      $A \xLongleftarrow{f} B$
      \\
      \comname{xLongleftarrow}%
      \massindex[arrows]{xLongleftarrow}[\comname]
    \end{tabular}
    &
    \begin{tabular}{@{}c@{}}
      $A \xLongrightarrow{f} B$
      \\
      \comname{xLongrightarrow}%
      \massindex[arrows]{xLongrightarrow}[\comname]
    \end{tabular}
    &
    \begin{tabular}{@{}c@{}}
      $A \xLongleftrightarrow{f} B$
      \\
      \comname{xLongleftrightarrow}%
      \massindex[arrows]{xLongrightarrow}[\comname]
    \end{tabular}
    \\
    \bottomrule
  \end{tabular}
  \endgroup
  \end{center}
 \caption{Extensible arrows in \packname{amsmath}, \packname{mathtools} and \packname{extarrows}.}
 \label{extensible arrow table}
\end{table}

The author recommends to define custom commands as shortcuts for the most used arrows.
\begin{showlatex}{Defining arrow commands as shortcuts}
\newcommand{\xto}{\xrightarrow}
\newcommand{\xlongto}[1]{\xlongrightarrow{\;#1\;}}
The function $A \xto{f} B$ is the same as
\[
  A \xlongto{f} B \,.
\]
\end{showlatex}

% TODO: Defining new kind of stretchable arrows

\subsection{Know your ellipses}
\index{dots!zzzz@\igobble |see {ellipsis}}
\index{ellipses|(}

An ellipsis\index{ellipses} should \emph{never} by written as~\inlinecode{...} (three single dots).
{\LaTeX} instead provides different kinds of ellipses to use in math mode, namely
\begin{center}
    \comname{dotsb},
    \quad
    \comname{dotsc},
    \quad
    \comname{dotsm},
    \quad
    \comname{dotsi},
    \quad
    \comname{dotso},
    \\
    \comname{cdots},
    \quad
    \comname{ddots},
    \quad
    \comname{vdots},
    \quad
    \comname{ldots}.
\end{center}
Each of these have they own role and (mostly) distinct look and feel.
There are two groups of ellipses.
The first group consists of \comname{dotsb}, \comname{dotsc}, \comname{dotsm}, \comname{dotsi},\comname{dotso}, whereas the second group consists of \comname{cdots}, \comname{ddots}, \comname{vdots}, \comname{ldots}.

The ellipses in the first group are named after their function, see \cref{ellipses with function}.
\begin{table}[tb]
  \begin{center}
  \begingroup
  \renewcommand{\arraystretch}{1.3}
  \begin{tabular}{@{}lll@{}}
    \toprule
    %
    \theading{command}
    &
    \theading{where to use}
    &
    \theading{example}
    \\
    \midrule
    %
    \comname{dotsb}%
    \massindex[ellipses]{dotsb}[\comname]
    &
    between binary relations
    &
    $x_1 \leq \dotsb \leq x_n$
    \\
    {}
    &
    and binary operations
    &
    $x_1 + \dotsb + x_n$
    \\
    \comname{dotsc}%
    \massindex[ellipses]{dotsc}[\comname]
    &
    between commas
    &
    $x_1, \dotsc, x_n$
    \\
    \comname{dotsm}%
    \massindex[ellipses]{dotsm}[\comname]
    &
    abbreviating multiplication
    &
    $x_1 \dotsm x_n$
    \\
    \comname{dotsi}%
    \massindex[ellipses]{dotsi}[\comname]
    &
    iterated integrals\index{integral}
    &
    $\int_{X_1} \dotsi \int_{X_n}$
    \\
    \comname{dotso}%
    \massindex[ellipses]{dotso}[\comname]
    &
    others
    &
    {}
    \\
    \bottomrule
  \end{tabular}
  \endgroup
  \end{center}
  \caption{Ellipses with a specific functions.}
  \label{ellipses with function}
\end{table}
Note that the members of this group all have names of the form \comname{dots*}, where \optname{*} is a letter that specifies the semantic use of the ellipsis.

The ellipses in the second group are not named after their function but after their orientation, see \cref{ellipses with orientation}
\begin{table}[tb]
  \begin{center}
  \begingroup
  \renewcommand{\arraystretch}{1.3}
  \begin{tabular}{@{}llc@{}}
    \toprule
    %
    \theading{command}
    &
    \theading{description}
    &
    \theading{look}
    \\
    \midrule
    %
    \comname{cdots}%
    \massindex[ellipses]{cdots}[\comname]
    &
    Horizontal dots, vertically centered.
    &
    $\cdots$
    \\
    \comname{ddots}%
    \massindex[ellipses]{ddots}[\comname]
    &
    Diagonal dots.
    &
    $\ddots$
    \\
    \comname{vdots}%
    \massindex[ellipses]{vdots}[\comname]
    &
    Vertical dots, horizontally centered.
    &
    $\vdots$
    \\
    \comname{ldots}%
    \massindex[ellipses]{ldots}[\comname]
    &
    Lowered dots.
    &
    $\ldots$
    % making this line as high as the others
    \vphantom{$\vdots$}
    \\
    \bottomrule
  \end{tabular}
  \endgroup
  \end{center}
  \caption{Ellipses with a specific orientation.}
  \label{ellipses with orientation}
\end{table}
The ellipses~\comname{cdots},~\comname{ddots} and~\comname{vdots} are typically used in matrices.
\begin{showlatex}{Typical usage of the ellipses~\comname{cdots},~\comname{vdots},~\comname{ddots}}
\[
  \begin{pmatrix}
    a_{11} & \cdots & a_{1n} \\
    \vdots & \ddots & \vdots \\
    a_{n1} & \cdots & a_{nn}
  \end{pmatrix}
\]
\end{showlatex}
The command~\comname{ldots} may be used to denote left out digits or symbols.
\begin{showlatex}{Using the ellipsis~\comname{ldots}}
We find that
\[
  x = 0.1234567891011\ldots
\]
Consider the word $w = a_1 \ldots a_n$ where $a_1, \dotsc, a_n$ are letters in an alphabet $\Sigma$.
\end{showlatex}
Note that the members of this second group of dots all have names of the form~\comname{*dots}, where~\optname{*} is a letter that specified the positioning of these dots.

% The following should \emph{never} be used to denote a product:
% \begin{showcode}{Wrong way of abbreviating multiplication~I}
% x_1 \cdot {any kind of dots} \cdot x_n
% \end{showcode}
% So all of the following are \emph{wrong}, and some of them look even worse then the other ones.
% \begin{showlatex}*{Wrong way of abbreviating multiplication~II}
% \begin{gather*}
%   x_1 \cdot \dotsb \cdot x_n \\
%   x_1 \cdot \dotsc \cdot x_n \\
%   x_1 \cdot \dotsm \cdot x_n \\
%   x_1 \cdot \dotsi \cdot x_n \\
%   x_1 \cdot \cdots \cdot x_n \\
%   x_1 \cdot \ldots \cdot x_n
% \end{gather*}
% \end{showlatex}
% (The author hopes that nobody is stupid enough to even trying using \comname{ddots} or \comname{vdots} in this situation.)

So overall one should use the ellipses~\comname{cdots}, \comname{ddots} and \comname{vdots} for matrices, and otherwise the ellipses~\comname{dotsb}, \comname{dotsc}, \comname{dotsm}, \comname{dotsi} and occasionally \comname{dotso}.

There also exists the generic command~\comname{dots}\massindex[ellipses]{dots}[\comname] which tries to automagically use the right kind of positioning and spacing.
But the author recommends not using this command as it can lead to inconsistent results:
\begin{showlatex}{Inconsistent results with \comname{dots}}
The ellipses in $x_1 \leq x_2 \leq \dots \leq x_n$ and $y_1 \leq y_2 \leq \dots$ should look the same.
\end{showlatex}

\index{ellipses|)}

\subsection{Know your matrices}
\index{matrices|(}

The package~\packname{amsmath}\massindex[packages]{amsmath}[\packname] provides various environments for matrices, the most basic of which is the environment~\envname{matrix}\massindex[matrices!normal sized]{matrix}[\envname].
\begin{showlatex}*{The basic matrix environment~\envname{matrix}}
\[
  \begin{matrix}
    a & b \\
    c & d
  \end{matrix}
\]
\end{showlatex}
There are five more variants of this basic matrix environment, that put different delimiters around the matrix.
The package~\packname{mathtools}\massindex[packages]{mathtools}[\packname] also provides small versions of all six kinds of matrices.
See \cref{table of matrices} for a table of all these kinds of matrices.
\begin{table}
  \begin{center}
  \begin{tabular}{@{}lccc@{}}
    \toprule
    %
    \theading{size}
    &
    \multicolumn{3}{c}{\theading{matrices}}
    \\
    \midrule
    %
    normal
    &
    \begin{tabular}{c}
      $\begin{matrix} a & b \\ c & d \end{matrix}$ \\
      \envname{matrix}%
      \massindex[matrices!normal sized]{matrix}[\envname]
    \end{tabular}
    &
    \begin{tabular}{c}
      $\begin{pmatrix} a & b \\ c & d \end{pmatrix}$ \\
      \envname{pmatrix}%
      \massindex[matrices!normal sized]{pmatrix}[\envname]
    \end{tabular}
    &
    \begin{tabular}{c}
      $\begin{bmatrix} a & b \\ c & d \end{bmatrix}$ \\
      \envname{bmatrix}%
      \massindex[matrices!normal sized]{bmatrix}[\envname]
    \end{tabular}
    \\[2.5em]
    {}
    &
    \begin{tabular}{c}
      $\begin{Bmatrix} a & b \\ c & d \end{Bmatrix}$ \\
      \envname{Bmatrix}%
      \massindex[matrices!normal sized]{Bmatrix}[\envname]
    \end{tabular}
    &
    \begin{tabular}{c}
      $\begin{vmatrix} a & b \\ c & d \end{vmatrix}$ \\
      \envname{vmatrix}%
      \massindex[matrices!normal sized]{vmatrix}[\envname]
    \end{tabular}
    &
    \begin{tabular}{c}
      $\begin{Vmatrix} a & b \\ c & d \end{Vmatrix}$ \\
      \envname{Vmatrix}%
      \massindex[matrices!normal sized]{Vmatrix}[\envname]
    \end{tabular}
    \\[2.5em]
    small
    &
    \begin{tabular}{c}
      $\begin{smallmatrix} a & b \\ c & d \end{smallmatrix}$ \\
      \envname{smallmatrix}%
      \massindex[matrices!small sized]{smallmatrix}[\envname]
    \end{tabular}
    &
    \begin{tabular}{c}
      $\begin{psmallmatrix} a & b \\ c & d \end{psmallmatrix}$ \\
      \envname{psmallmatrix}%
      \massindex[matrices!small sized]{psmallmatrix}[\envname]
    \end{tabular}
    &
    \begin{tabular}{c}
      $\begin{bsmallmatrix} a & b \\ c & d \end{bsmallmatrix}$ \\
      \envname{bsmallmatrix}%
      \massindex[matrices!small sized]{bsmallmatrix}[\envname]
    \end{tabular}
    \\[2.5em]
    {}
    &
    \begin{tabular}{c}
      $\begin{Bsmallmatrix} a & b \\ c & d \end{Bsmallmatrix}$ \\
      \envname{Bsmallmatrix}%
      \massindex[matrices!small sized]{Bsmallmatrix}[\envname]
    \end{tabular}
    &
    \begin{tabular}{c}
      $\begin{vsmallmatrix} a & b \\ c & d \end{vsmallmatrix}$ \\
      \envname{vsmallmatrix}%
      \massindex[matrices!small sized]{vsmallmatrix}[\envname]
    \end{tabular}
    &
    \begin{tabular}{c}
      $\begin{Vsmallmatrix} a & b \\ c & d \end{Vsmallmatrix}$ \\
      \envname{Vsmallmatrix}%
      \massindex[matrices!small sized]{Vsmallmatrix}[\envname]
    \end{tabular}
    \\
    \bottomrule
  \end{tabular}
  \end{center}
  \caption{Different kinds of matrices.}
  \label{table of matrices}
\end{table}

The package~\packname{mathtools}\massindex[packages]{mathtools}[\packname] also provides starred versions of the matrices.
These starred versions allow to specify as an optional argument the alignment of the columns, see \cref{table of matrices}.
The three possible arguments are~\optname{l}\massindex{l}[\optname] for left-alignment,~\optname{c}\massindex{c}[\optname] for centered alignment and~\optname{r}\massindex{r}[\optname] for right-alignment.
The standard alignment is~\optname{c}.
\begin{showlatex}{Aligning matrix entries}
\[
  \begin{bmatrix*}[l]
      a & -b \\
     -c &  d
  \end{bmatrix*}
  \qquad
  \begin{bmatrix*}[c]
    a & -b \\
    -c &  d
  \end{bmatrix*}
  \qquad
  \begin{bmatrix*}[r]
     a & -b \\
    -c &  d
  \end{bmatrix*}
  \qquad
  \begin{bmatrix}
     a & -b \\
    -c &  d
  \end{bmatrix}
\]
\end{showlatex}

One should choose the kind of matrix, its size and alignment dependent on convention, usage and the matrix entries.
\begin{showlatex}*{Using the right kind of matrix}
We consider the morphism
\[
  X \oplus Y
  \xrightarrow{\,
    \begin{bsmallmatrix*}[r]
      f & -d \\
      0 &  g
    \end{bsmallmatrix*}
    \,
  }
  X' \oplus Y' \,.
\]
\end{showlatex}

\index{matrices|)}

\section{Avoid bad notation}

\subsection{Don’t use \comtitle{subset}}

Some people use~$\subset$~(\comname{subset}\massindex{subset}[\comname]) to denote inclusion and~$\subsetneq$~(\comname{subsetneq}\massindex{subsetneq}[\comname]) to denote proper inclusion, while some other people use~$\subseteq$~(\comname{subseteq}\massindex{subseteq}[\comname]) to denote inclusion and~$\subset$ to denote proper inclusion.
The second convention has the advantage of making sense and being consistent with the usual use of~$\leq$ and~$<$, whereas the first convention has the non-advantage of existing.
The problem is that both conventions are wide-spread while using the symbol~$\subset$ is different ways.

The conflict between the above two conventions has abused the symbol~$\subset$ to a point that it should \emph{never} be used.
Instead one should always use~$\subseteq$ for inclusion, $\subsetneq$ for proper inclusion and~$\nsubseteq$~(\comname{nsubseteq}\massindex{nsubseteq}[\comname]) for non-inclusion.

Some people prefer the symbols~$\subseteqq$~(\comname{subseteqq}\massindex{subseteqq}[\comname]) and~$\subsetneqq$~(\comname{subsetneqq}\massindex{subsetneqq}[\comname]) instead.
The author thinks that these symbols are unnecessary large and recommends not to use them.
(But they do at least leave poor old~$\subset$ alone.)

\subsection{Don’t underline}
\index{underlining}

Underlining works well on the blackboard, in handwriting, and was useful in the in the (dark) age of typewriters.
Don’t do it in {\LaTeX}.

\subsection{Use \texorpdfstring{$\mathrm{d}x$}{dx} instead of \texorpdfstring{$dx$}{dx}}

A differential\index{differential} is written as \inlinecode{{\tbs}mathrm\{d\}x}, not as \inlinecode{dx}.
When it occurs at the end of an integral\index{integral} then a slight spacing~\comname{,}\index{spacing!in math mode} is also introduced in front of it.
So don’t do the following:
\begin{showlatex}*{Wrong kind of differentials}
\[
  \int_a^b f(x) dx
\]
\end{showlatex}
Instead do the following:
\begin{showlatex}*{Right kind of differential}
\[
  \int_a^b f(x) \,\mathrm{d}x
\]
\end{showlatex}

\subsection{Don’t force fancy fractions}
\index{fractions|(}

When fractions are placed inline, are part of an exponent or part of an index, then they should be of the form $a/b$.
The notation
\[
  \frac{a}{b}
\]
is reserved for display style.
So don’t do the following:
\begin{showlatex}{Full fractions in exponent, index and inline}
Consider $e^{\frac{1}{x}}$ and $x_{\frac{1}{n}}$ and $\frac{2}{3}$.
\end{showlatex}
Do the following instead:
\begin{showlatex}{Flat fractions in exponent, index and inline}
Consider $e^{1/x}$ and $x_{1/n}$ and $2/3$.
\end{showlatex}

Don’t use funky fractions like $\faktor{a}{b}$. They’ll make you go blind and burn down your house.

% TODO: Finding out the proper term for flat fractions. See Chicago Manual?

\index{fractions|)}

\section{Defining new commands}

Many mathematical operators have predefined commands, which should be used when needed.
\begin{showlatex}{Using commands vs.\ not using them}
Use $\sin(x)$ instead of $sin x$, use $\dim V$ instead of $dim V$ and use $\lim_{n \to \infty} a_n$ instead of $lim_{n \to \infty} a_n$.
\end{showlatex}
New commands can be defined in various ways:

\subsection{\comtitle{DeclareMathOperator}}

Commands of the form~\comname{WordCommand}, that are supposed to print~\enquote{$\mathrm{WordOutupt}$}, can easily be defined using the command~\comname{DeclareMathOperator}\massindex[defining commands]{DeclareMathOperator}[\comname].
\begin{showcode}{Syntax of~\comname{DeclareMathOperator}}
\DeclareMathOperator{\WordCommand}{WordOutput}
\end{showcode}
The command \comname{DeclareMathOperator} can only be used in the preamble.

To define the command~\comname{End} we use the following text in the preamble:
\begin{showcode}{Declaring a mathematical operator with~\comname{DeclareMathOperator}}
% in the preamble:
\DeclareMathOperator{\End}{End}
\end{showcode}
The command \comname{End} can then be used in the usual way:
\begin{showlatex}{Using a declared mathematical operator}
Thus $\End(V) = \End_k(V)$ becomes a vector space.
\end{showlatex}

If a command~\comname{WordCommand} is defined with \comname{DeclareMathOperator} then {\LaTeX} will automatically insert some space around\index{spacing!in math mode} \comname{WordCommand} when needed:
\begin{showlatex}*{Automatic spacing of~\comname{DeclareMathOperator}}
\begin{align*}
&x \End V
\\
&x \End(V)
\\
&x \End {(V)}
\end{align*}
\end{showlatex}
Note that in the first expression {\LaTeX} inserts some spacing both to the left and to the right of $\End$.
In the second expression {\LaTeX} observes that the used math operator is follows by a parenthesis and thus inserts no additional spacing.
For the third expression we prevent {\LaTeX} from making such an observation by using a pair of curly brackets.

This behavior leads to a bad looking output when \comname{DeclareMathoperator} is abused.
Suppose that we want a command \comname{Complex} that inserts the code \comname{mathbb\{C\}}.
\begin{showcode}{Wrong way of using \comname{DeclareMathOperator}}
% in the preamble:
\DeclareMathOperator{\Complex}{\mathbb{C}}
\end{showcode}
This will lead to the following problem:
\begin{showlatex}{Wrong output when \comname{DeclareMathOperator} is abused}
The span of $x_1, \dotsc, x_n \in \Complex^m$ equals $\Complex x_1 + \dotsb + \Complex x_n$.
\end{showlatex}
We expect the output $\mathbb{C} x_1 + \dotsb + \mathbb{C} x_n$ but get some unwanted spacing instead.

\subsection{\comtitle{operatorname}}

The command~\comname{operatorname}\massindex[defining commands]{operatorname}[\comname] can be used to give the formatting of a mathemical operator without defining a new command.
\begin{showlatex}{Using \comname{operatorname}}
Thus $\operatorname{Hom}(V,W) = \operatorname{Hom}_k(V,W)$ becomes a vector space.
\end{showlatex}
If the same command is used multiple times then one should use \comname{DeclareMathOperator} instead of \comname{operatorname}, to keep the code clean.

\subsection{Don’t abuse \comtitle{mathrm}}

The commands~\comname{mathrm}\massindex{mathrm}[\comname] and \comname{operatorname} do not give the same formatting.
With \comname{operatorname} we get the necessary spacing\index{spacing!in math mode} when not using parentheses
We don’t get this from \comname{mathrm}.
\begin{showlatex}{Missing spacing after~\comname{mathrm}}
Compare $\operatorname{End} V$ to $\mathrm{End} V$, and also~$2 \operatorname{Fr}(x)$ to~$\mathrm{Fr}(x)$.
\end{showlatex}

\subsection{\comtitle{newcommand}}

A very general way of defining new commands is given by \comname{newcommand}\massindex[defining commands]{newcommand}[\comname].
Its syntax is as follows:
\begin{showcode}{Syntax of \comname{newcommand}}
\newcommand{\name}[number of arguments n]{ definition including #1, ..., #n }
\end{showcode}
Consider the following example:
\begin{showlatex}{Using the command~\comname{newcommand}}
\newcommand{\bimodule}[2]{#1-#2-bimodule}
Let $M$ be an \bimodule{$A$}{$B$}.
\end{showlatex}
One may think about \comname{DeclareMathOperator} as a combination of \comname{newcommand} and \comname{operatorname}:
\begin{showlatex}{\comname{DeclareMathOperator} = \comname{newcommand} plus \comname{operatorname}}
\newcommand{\Ouv}{\operatorname{Ouv}}
$\Ouv X$
\end{showlatex}

Trying to define an already existing command with \comname{newcommand} will lead to an error.
To overwrite an already existing command one can use \comname{renewcommand}\massindex[defining commands]{renewcommand}[\comname] instead.
But this shouldn’t really be done (unless you really, \emph{really} know what you’re doing).
Even if you don’t like a particular command it may happen that some of the packages which you’re using rely on it:
overwriting the command can then easily lead to some unexpected new problems.

\subsection{\comtitle{DeclarePairedDelimiter}}
\index{delimiters!zzzz@\igobble |seealso {\comname{DeclarePairedDelimiter}}}

Mathematical operations like the absolute value~$\lvert \,\cdot\, \rvert$ and a norm~$\lVert \,\cdot\, \rVert$ are denoted by putting certain delimiters around the argument.
To define a corresponding {\LaTeX} command like~\comname{abs} or \comname{norm} one should use the command~\comname{DeclarePairedDelimiter}\massindex[defining commands]{DeclarePairedDelimiter}[\comname] (which is provided by the package~\packname{mathtools}).
\begin{showcode}{Syntax of \comname{DeclarePairedDelimiter}}
\DeclarePairedDelimiter{\name}{left delimiter}{right delimiter}
\end{showcode}
As an example we use \comname{DeclarePairedDelimiter} to define a command~\comname{abs} for absolute value:
\begin{showcode}{Defining the command~\comname{abs} with \comname{DeclarePairedDelimiter}}
\DeclarePairedDelimiter{\abs}{\lvert}{\rvert}
\end{showcode}
The defined command can now be used as expected:
\begin{showlatex}{Using a declared delimiter}
  \[
    \abs{-5} = 5
  \]
\end{showlatex}

Declaring a command via~\comname{DeclarePairedDelimiter} will automatically also define a starred version that scales the surrounding delimiters according to the given content between them.
One can also specify a scaling size like \comname{big}\massindex[scaling]{big}[\comname], \comname{bigg}\massindex[scaling]{bigg}[\comname], etc.\ to scale the delimiters.
\begin{showlatex}{Scaling of declared delimiters}
\begin{align*}
  \abs{-\frac{1}{2}}
  &=
  \frac{1}{2} \,,
  \\
  \abs*{-\frac{1}{2}}
  &=
  \frac{1}{2} \,,
  \\
  \abs[\bigg]{-\frac{1}{2}}
  &=
  \frac{1}{2}
\end{align*}
\end{showlatex}

The variant~\comname{DeclarePairedDelimiterX}\massindex[defining commands]{DeclarePairedDelimiterX}[\comname] allows building more sophisticated commands:
\begin{showcode}{Syntax of \comname{DeclarePairedDelimiterX}}
\DeclarePairedDelimiterX{\name}
  [number of arguments n]
  {left delimiter}{right delimiter}
  {expression build from #1, #2, ..., #n}
\end{showcode}
If the built up expression contains a delimiter, then by prefixing this delimiter with the command~\comname{delimsize}\massindex[scaling]{delimsize}[\comname] will ensure that the delimiter scales in the same way as the two surrounding delimiters.
As an example we define a command~\comname{inner} for inner product.
\begin{showcode}{Using \comname{DeclarePairedDelimiterX} for more advanced delimiters}
\DeclarePairedDelimiterX{\inner}[2]{\langle}{\rangle}{#1 \,\delimsize\vert\, #2}
\end{showcode}
The defined command~\comname{inner} takes two arguments and inserts a line~(\comname{vert}) with some surrounding space~(\comname{,}) between them.
\begin{showlatex}{Using more advanced delimiters}
\[
  \inner{\psi_1}{\psi_2}
  \quad
  \inner*{\frac{f}{g}}{\frac{h}{k}}
\]
\end{showlatex}
There is also the variant~\comname{DeclarePairedDelimiterXPP}\massindex[defining commands]{DeclarePairedDelimiterXPP}[\comname] that is even more flexible than \comname{DeclarePairedDelimiterX}.
We refer to \cite{mathtools} for more details on these commands.

\subsection{The package~\packtitle{xparse}}

A useful way for defining commands -- in particular more involved ones -- is provided by the package~\packname{xparse}\massindex[packages]{xparse}[\packname].
This package provides the command~\comname{NewDocumentCommand}\massindex[defining commands]{NewDocumentCommand}[\comname].
\begin{showcode}{Syntax of \comname{NewDocumentCommand}}
\NewDocumentCommand{\name}{arguments}{definition}
\end{showcode}
The field~\inlinecode{arguments} specifies what kinds of arguments the command~\comname{name} will take:

\subsubsection{Mandatory arguments}

Mandatory arguments can be declared with the option~\optname{m}\expandafter\index\expandafter{\ciname{NewDocumentCommand}!mandatory argument}:
\begin{showlatex}{Mandatory arguments with \comname{NewDocumentCommand}~II}
% in the preamble
\NewDocumentCommand{\double}{m}{#1 #1}
% in the main text
\double{sometext}
\end{showlatex}
One can also specify multiple arguments:
\begin{showlatex}{Mandatory arguments with \comname{NewDocumentCommand}~II}
% in the preamble
\NewDocumentCommand{\swap}{m m}{#2 #1}
% in the main text
\swap{first}{second}
\end{showlatex}

\subsubsection{Optional arguments without default value}

Optional arguments can be declared with the option~\optname{o}\massindex{o}[\optname]\expandafter\index\expandafter{\ciname{NewDocumentCommand}!optional argument!without default value}.
It needs to be checked with the command~\comname{IfNoValueTF} if this optional argument was assigned a value.
\begin{showcode}[label = {syntax of ifvaluetf}]{Syntax of \comname{IfValueTF}}
\IfValueTF{#number of argument}
  {if the argument has been set}
  {if the argument has not been set}
\end{showcode}
One can similarly use~\comname{IfNoValueTF} instead of~\comname{IfNoValueTF}.
This has the same effect as switching the cases in \cref{syntax of ifvaluetf}.

Let’s look at a specific example:
\begin{showlatex}{Optional arguments with \comname{NewDocumentCommand},~I}
% in the preamble
\NewDocumentCommand
{\module}
{m o}
{\IfNoValueTF{#2}{#1-module}{#1-#2-bimodule}}
% in the main text
Let $M$ be an \module{$R$} and let $N$ be an \module{$R$}[$S$].
\end{showlatex}
It is useful to properly indent the source code.
But we have to be careful here:
It can easily happen that this indentation introduces some unwanted whitespace.
Let’s consider the naive code first:
\begin{showlatex}{Optional arguments with \comname{NewDocumentCommand},~I}
% in the preamble
\NewDocumentCommand
{\module}
{m o}
{
  \IfNoValueTF{#2}
    {#1-module}
    {#1-#2-bimodule}
}
% in the main text
Let $M$ be an \module{$R$} and let $N$ be an \module{$R$}[$S$].
\end{showlatex}
Now let’s \enquote{comment out} the additional whitespace.
\begin{showlatex}{Optional arguments with \comname{NewDocumentCommand},~I}
% in the preamble
\NewDocumentCommand
{\module}
{m o}
{%
  \IfNoValueTF{#2}
    {#1-module}
    {#1-#2-bimodule}%
}
% in the main text
Let $M$ be an \module{$R$} and let $N$ be an \module{$R$}[$S$].
\end{showlatex}

\subsubsection{Optional arguments with default value}

One can use \optname{O\{default value\}}\massindex{O\{ \}}[\optname]\expandafter\index\expandafter{\ciname{NewDocumentCommand}!optional argument!with default value} instead of \optname{o} to declare an optional argument that has a default value.

Let’s start with an example where this default value is empty:
\begin{showlatex}{Optional arguments with \comname{NewDocumentCommand},~II}
% in the preamble
\NewDocumentCommand{\restrict}{m m O{}}{#1|_{#2}^{#3}}
% in the main text
Let $f = \restrict{g}{X}$ and $f' = \restrict{g'}{X}[Y]$
\end{showlatex}
Now let’s do an example which has a standard value:
\begin{showlatex}{Optional arguments with \comname{NewDocumentCommand},~III}
% in the preamble
\NewDocumentCommand{\favorite}{O{colour} m}{My favorite #1 is #2.}
% in the main text
\favorite{blue}
\favorite[food]{spam}
\end{showlatex}

\subsubsection{Starred versions}

One can use the argument~\optname{*} together with the command~\comname{IfBooleanTF}\massindex[\ciname{NewDocumentCommand}]{IfBooleanTF}[\comname] to check for the occurrence of a star.
This can be used to also define a starred version of a command.
\begin{showlatex}{Starred versions with \comname{NewDocumentCommand}}
% in the preamble
\NewDocumentCommand{\choice}{s m m}{\IfBooleanTF{#1}{#3}{#2}}
% in the main text
Don’t confuse \choice{first}{second} with \choice*{first}{second}.
\end{showlatex}

\section{Stretch your arrows}

If some expression occurs atop or below an arrow, then this arrow must be stretched suffciently long to accommodate this expression.
The extensible arrows introduced in \cref{extensible arrows} automatically do so.

\subsection{Stretching columns}

If an arrow in a commutative diagram isn’t long enough then this arrow must also be stretched:
\begin{showlatex}{Commutative diagram with an arrow too short}
\[
\begin{tikzcd}
  X \arrow{r}{f \circ g - g \circ f} \arrow{d}
  &
  Y \arrow{r}{k} \arrow{d}
  &
  Z \arrow[equal]{d}
  \\
  X' \arrow[dashed]{r}{h'}
  &
  Y' \arrow{r}{k'}
  &
  Z'
\end{tikzcd}
\]
\end{showlatex}
The column distance of a commutative diagram is governed by the option~\optname{column~sep}\massindex[\piname{tikz-cd}]{column sep}[\optname].
The value of \optname{column~sep} is expected to be a distance, e.g.~\optname{4em}.
Some standard distances are predefined, see \cref{column sep settings}.
\begin{table}[tb]
  \begin{center}
  \begin{tabular}{@{}lcccccc@{}}
    \toprule
    \theading{name}
    &
    \optname{tiny}\massindex[\piname{tikz-cd}!\optname{column sep}]{tiny}[\optname]
    &
    \optname{small}\massindex[\piname{tikz-cd}!\optname{column sep}]{small}[\optname]
    &
    \optname{scriptsize}\massindex[\piname{tikz-cd}!\optname{column sep}]{scriptsize}[\optname]
    &
    \optname{normal}\massindex[\piname{tikz-cd}!\optname{column sep}]{normal}[\optname]
    &
    \optname{large}\massindex[\piname{tikz-cd}!\optname{column sep}]{large}[\optname]
    &
    \optname{huge}\massindex[\piname{tikz-cd}!\optname{column sep}]{huge}[\optname]
    \\
    \theading{distance}
    &
    \optname{0.6em}
    &
    \optname{1.2em}
    &
    \optname{1.8em}
    &
    \optname{2.4em}
    &
    \optname{3.6em}
    &
    \optname{4.8em}
    \\
    \bottomrule
  \end{tabular}
  \end{center}
  \caption{Standard distances for~\optname{column sep}.}
  \label{column sep settings}
\end{table}
With~\optname{column~sep} one can fix the above diagram.
\begin{showlatex}{Using~\optname{column sep}}
\[
\begin{tikzcd}[column sep = huge]
  X \arrow{r}{f \circ g - g \circ f} \arrow{d}
  &
  Y \arrow{r}{k} \arrow{d}
  &
  Z \arrow[equal]{d}
  \\
  X' \arrow[dashed]{r}{h'}
  &
  Y' \arrow{r}{k'}
  &
  Z'
\end{tikzcd}
\]
\end{showlatex}
One can also increase the width of a specific column by specifying the additional width at the correct~\&\nbd-symbol in the first row.
(In the above examples, the difference between \optname{normal} and \optname{huge} is~\optname{2.4em}.)
\begin{showlatex}{Explicit column spacing}
\[
\begin{tikzcd}
  X \arrow{r}{f \circ g - g \circ f} \arrow{d}
  &[2.4em]
  Y \arrow{r}{k} \arrow{d}
  &
  Z \arrow[equal]{d}
  \\
  X' \arrow[dashed]{r}{h'}
  &
  Y' \arrow{r}{k'}
  &
  Z'
\end{tikzcd}
\]
\end{showlatex}

\subsection{Stretch rows}

One can similarly use the option~\optname{row~sep} to change the distance of the rows.
Some predefined distances can be found in \cref{row sep settings}.
\begin{table}[tb]
  \begin{center}
  \begin{tabular}{@{}lcccccc@{}}
    \toprule
    \theading{name}
    &
    \optname{tiny}\massindex[\piname{tikz-cd}!\optname{row sep}]{tiny}[\optname]
    &
    \optname{small}\massindex[\piname{tikz-cd}!\optname{row sep}]{small}[\optname]
    &
    \optname{scriptsize}\massindex[\piname{tikz-cd}!\optname{row sep}]{scriptsize}[\optname]
    &
    \optname{normal}\massindex[\piname{tikz-cd}!\optname{row sep}]{normal}[\optname]
    &
    \optname{large}\massindex[\piname{tikz-cd}!\optname{row sep}]{large}[\optname]
    &
    \optname{huge}\massindex[\piname{tikz-cd}!\optname{row sep}]{huge}[\optname]
    \\
    \theading{distance}
    &
    \inlinecode{0.45em}
    &
    \inlinecode{0.9em}
    &
    \inlinecode{1.35em}
    &
    \inlinecode{1.8em}
    &
    \inlinecode{2.7em}
    &
    \inlinecode{3.6em}
    \\
    \bottomrule
  \end{tabular}
  \end{center}
  \caption{Standard distances for~\optname{row sep}.}
  \label{row sep settings}
\end{table}
They are the same as for~\optname{column~sep} but scaled down by a factor of~$0.75$.
One can also explicitely specify an additional space between two rows after the correct~\comname{\tbs}.
\begin{showlatex}{Using~\optname{row sep} and explicit row spacing}
\[
  \begin{tikzcd}[row sep = huge]
    A \arrow{r} \arrow{d}
    &
    B \arrow{d}
    \\
    C \arrow{r} \arrow{d}
    &
    D \arrow{d}
    \\
    E \arrow{r}
    &
    F
  \end{tikzcd}
  \qquad
  \begin{tikzcd}
    A \arrow{r} \arrow{d}
    &
    B \arrow{d}
    \\[1.8em]
    C \arrow{r} \arrow{d}
    &
    D \arrow{d}
    \\
    E \arrow{r}
    &
    F
  \end{tikzcd}
\]
\end{showlatex}

\section{Beware of spacings}

\index{spacing!in math mode|(}

{\LaTeX} classifies symbols and expressions into certain groups and then adds spacing around these symbols, which depends on the group they belong to.
Three of these groups are \enquote{operators}, \enquote{relation symbols} and \enquote{binary operations}.
The symbols~\inlinecode{=} and~\inlinecode{<} are for example treated as relations symbols, and the symbols~\inlinecode{+} and~\comname{cdot} as binary operations.
We can see in the following example how some space is automatically added around these symbols:
\begin{showlatex}{Standard spacing around relation symbols and binary operators}
\[
  a = b  \qquad  a < b  \qquad  a + b  \qquad  a \cdot b
\]
\end{showlatex}
To compare this to a version without spacing we can surround the symbols by a pair of curly brackets.
This circumvents {\LaTeX} from taking the surrounding code into consideration.
\begin{showlatex}{Disabling the standard spacing around a symbol}
\[
  a {=} b   \qquad  a {<} b  \qquad  a {+} b  \qquad  a {\cdot} b
\]
\end{showlatex}
The automatic spacing can become a problem, as the following examples illustrate:
\begin{showlatex}*{Clashing spacings around symbols}
\[
  X/\sim
  \quad
  R/\operatorname{J}(R)
  \quad
  \operatorname{id} \otimes h
\]
\end{showlatex}
These problems can be fixed by surrounding the respective symbols with curly brackets.
\begin{showlatex}*{Preventing a clash of spacings}
\[
  X/{\sim}
  \quad
  R/{\operatorname{J}(R)}
  \quad
  {\operatorname{id}} \otimes h
\]
\end{showlatex}
One can also tell {\LaTeX} a symbol should be treated.
\begin{showlatex}*{Specifying the role (and thus spacing) of a symbol}
\[
  a | b
  \quad
  a \mathop{|} b
  \quad
  a \mathrel{|} b
  \quad
  a \mathbin{|} b
\]
\end{showlatex}
We can thus define a command~\comname{divides}, which expresses that a number~$n$ divides a number~$m$, as follows:
\begin{showlatex}*{Defining and using \comname{divides}}
\newcommand{\divides}{\mathrel{|}}
\[
  n \divides m
\]
\end{showlatex}
For more on this topic we refer to \cite{tex_binrel}.

\index{spacing!in math mode|)}


\section{Background}

% Description
Hopfield \parencite{hopfield1982} proposed a model in which memory storage and retrieval emerge as properties of the collective behavior of its units, or neurons.
This connectionist model is capable of recovering a previously presented pattern or patterns from partial cues, being able to complete the missing information.

Hopfield networks behave as fixed-point attractor networks as their internal state evolves towards a stable single state or fixed point.
This is given by their energy function.
These types of systems have been used as models of associative memory \parencite{amit1992}.

% Currents
In the classic model as described by Hopfield \parencite{hopfield1982}, neurons are binary units: the activation state of each neuron can be either firing or not (on or off). The activity of each unit asynchronously changes in a discrete time scale.

% Weights
As in other connectionist systems, the strength of connections between nodes is described by its weight matrix.
Weights are only updated upon network initialization and depend on the patterns presented to all network units.
The weight matrix takes the shape of a square, symmetric matrix in which all the values in the main diagonal are always zero.
All neurons are connected to each other.
Also, every neuron is both an input and output node for memory pattern presentation and retrieval.
To compose the weight matrix, each node is updated according to a local incremental learning rule, related to Hebbian learning.
Hebb's rule states that neurons that fire together when a certain pattern is present strengthen the connections between them \parencite{hebb1949}.

% Modifications
In the work by Recantesi \textit{et al.} \parencite{recanatesi2015}, modifications to the original Hopfield model have been introduced, with new properties of memory retrieval: the model was adapted to induce transitions between attractor states (recalled memories).


\vspace*{-0.4cm}
\section{Techniques description}

The different coding schemes that have been implemented rely on several transforms and entropy coders that are introduced here.

\subsection{Run-Length Encoding (RLE)}

The RLE transforms a bilevel image into a sequence of runs. A run is the number of successive occurrences of the same value in the file. For example, the sequence $[0,0,0,1,1,1,1]$ would be coded $[3,4]$ by a coder assuming the first received symbol is a $0$.

\subsection{Move to Front transform (MTF)}

The purpose of the MTF is to reduce the entropy of the image \cite{benzid}. It is also based on the concept of runs but replaces each symbol of a run by 0 except the first one. The first one is replaced by the index of the symbol in the alphabet of the source. For example, for a sequence $[1,1,1,0,0,0,0]$, the alphabet is $[0,1]$. The index of 1 in the alphabet is 1, so that the sequence becomes $[\textbf{1},\textbf{0},\textbf{0},0,0,0,0]$. The 1 is moved to the front of the alphabet (hence the name of the MTF), and the sequence of 0's now needs to be encoded. The alphabet is now $[1,0]$, so that the index of 0 is 1. The final sequence is $[1,0,0,1,0,0,0]$ and the final alphabet is $[0,1]$.

\subsection{Two-Role Encoder (TRE)}

The TRE is a variant of the RLE which is more suited when one symbol is more probable than the other \cite{benzid}, so that it is meant to be used after the MTF. Each non-zero element is shifted by 24 bits, while the sequence of zeros is simply encoded as the run length. To follow the previous example, the sequence $[1,0,0,1,0,0,0]$ would be encoded as $[2^{23},2,2^{23},3]$. The stream can be decoded knowing that a symbol that is repeated is encoded with a number greater than $2^{23}$, while a run cannot be greater than $2^{23}$.

\subsection{Exp-Golomb coding}

The Exp-Golomb coding is a variable length prefix code meant for entropy coding. The $i$\textsuperscript{th} symbol is mapped to the binary representation of $i+1$, preceded by M zeros with $M=\left \lfloor{\log_2(i)}\right \rfloor $. When the short codewords are assigned to most probable symbols, compression can be achieved \cite{H264}.

\subsection{Arithmetic coding}

An arithmetic encoder converts a sequence of data symbols into a single fractional number and can approach the optimal fractional number of bits required to represent each symbol. In general, it outperforms prefix codes \cite{H264}. An integer version of the coder was implemented to cope with finite precision problems. When the interval corresponding to the symbol stream becomes too small, some bits are already extracted and the interval is made wider again.


\documentclass{article}
%\usepackage{fullpage}
%\usepackage{nopageno}
\usepackage[margin=1.5in]{geometry}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage[normalem]{ulem}
\usepackage{fancyhdr}
%\renewcommand\headheight{12pt}
\pagestyle{fancy}
\lhead{February 19, 2014}
\rhead{Jon Allen}
\allowdisplaybreaks

\newcommand{\abs}[1]{\left\lvert #1 \right\rvert}

\begin{document}
\section*{Chapter 3}
\subsection*{4.}
Show that if $n+1$ integers are chosen from the set $\{1,2,\dots,2n\}$, then there are always two which differ by 1.
\subsection*{proof}
Since we want all the integers to differ by more than one, we can only pickevery other integer from $\{1,2,\dots,2n\}$. This gives us a maximum of $n$ integers. Since we are choosing $n+1$ integers, we know that at least two of them must differ by only one.$\Box$
\subsection*{5.}
Show that if $n+1$ distinct integers are chosen from the set $\{1,2,\dots,3n\}$, then there are always two which differ by at most 2.
\subsection*{proof}
Since we want all the integers to differ by more than two, we can only pickevery third integer from $\{1,2,\dots,3n\}$. This gives us a maximum of $n$ integers. Since we are choosing $n+1$ integers, we know that at least two of them must differ by two or less.$\Box$
\subsection*{6.}
Generalize Exercises 4 and 5.
\subsection*{hypothesis}
If $n+1$ distinct integers are chosen from the set $\{1,2,\dots,mn\}$ where $m$ is a positive integer then there are always two which differ by at most $m-1$.
\subsection*{proof}
We can select at most $n$ integers which have a difference of $m$ or more. Since we are selecting $n+1$ integers then we must have at least two which differ by $m-1$ or less.$\Box$
\subsection*{8.}
Use the pigeonhole principle to prove that the decimal expansion of a rational number $m/n$ eventually is repeating. For example,
\begin{align*}
  \frac{34,478}{99,900}&=0.345125125125\cdots.
\end{align*}
\subsection*{proof}
We assume the $n>0$ because if $n=0$ we don't really have a rational number and if $n<0$ we can simply multiply by $\frac{-1}{-1}$ to make $n$ positive.

Now we will start building our decimal representation of our number by dividing.
\begin{align*}
  m&=q_0n+r_0,\qquad 0\le r_0\le n-1
\end{align*}
Now we have an integer part $q_0$ and our fractional decimal part $\frac{r_0}{n}$. We expand our fractional decimal digits by multiplying successive remainders by 10 and dividing by $n$ repeatedly. So for our $\frac{1}{10^i}$ place we have:
\begin{align*}
  r_{i-1}\cdot10&=q_in+r_i
\end{align*}
This will give us $q_i$ which is the digit in the $\frac{1}{10^i}$th spot. Notice that because we are dividing by $n$ our remainders will always satisfy $0\le r_i\le n-1$. Now lets take some sequence of $n$ remainders from our fractional expansion. Say $r_i,\dots,r_{i+n-1}$. Because we have $n$ remainders which can have $n-1$ possible values, we know from the pigeon hole principle that at least two of these remainders are the same. Lets pick $r_j$ from the sequence $r_i,\dots,r_{i+n-1}$ such that $r_j=r_i$. Applying the division algorithm to obtain:
\begin{align*}
  r_{i}\cdot10&=q_{i+1}n+r_{i+1}=r_{j}\cdot10\\
  r_{j}\cdot10&=q_{j+1}n+r_{j+1}=r_{i}\cdot10
\end{align*}
We know that because the remainders and quotients of division are unique $q_{i+1}=q_{j+1}$ and $r_{i+1}=r_{j+1}$. Given this we can say from induction that for any integer $1\le k$
\begin{align*}
  q_{i+k}&=q_{j+k}\\
  r_{i+k}&=r_{j+k}
\end{align*}
And in fact $q_{i+k}=q_{j+k}=q_{i+2(j-i)+k}=q_{i+3(j-i)+k}=q_{i+4(j-i)+k}=\dots$ and so we see that fractional part of the decimal representation of the ratio will at some point begin to repeat.$\Box$
\subsection*{12.}
Show by example that the conclusion of the Chinese rmainder theorem (Application 6) need not hold when $m$ and $n$ are not relatively prime.

Take 3 and 9 for $m$ and $n$. Take $2$ and $4$ for $a$ and $b$. Then we should be able to find an $x$ such that:
\begin{align*}
  x&=3p+2\\
  x&=9q+4\\
  3p+2&=9q+4\\
  3p&=9q+2\\
  3&\mid 3p\\
  3&\nmid 9q+2\\
  3p&\neq9q+2\\
\end{align*}
So we see that $x$ does not exist
\end{document}