\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}


\documentclass[a4paper]{article}

\title{Efficient numerical methods to solve ballistic equations}
\author{Javier Burguete}
\date{\today}

\newcommand{\ABS}[1]{\left|#1\right|}
\newcommand{\C}[1]{\left[#1\right]}
\newcommand{\COMB}[2]{\left(\begin{array}{c}#1\\#2\end{array}\right)}
\newcommand{\D}[3]{\DD{^{#3}#1}{#2^{#3}}}
\newcommand{\DD}[2]{\frac{d#1}{d#2}}
\newcommand{\EQ}[2]{\begin{equation}#1\label{#2}\end{equation}}
\newcommand{\PA}[1]{\left(#1\right)}
\newcommand{\PARTIAL}[2]{\frac{\partial #1}{\partial #2}}

\begin{document}

\maketitle

\tableofcontents

\section{Introduction}

We are interested in solving ordinary differential equations (ODE) as:
\EQ{\DD{u}{t}=f\PA{t,\,u(t)}.}{EqODE}

The following notation has been used in this work:
\[u_0=u\PA{t_0},\quad f_0=f\PA{t_0,\,u\PA{t_0}},\]
\[
	f_{n\,t}=\PARTIAL{^nf}{t^n},\quad
	f_{n\,t,m\,u}=\PARTIAL{^{n+m}f}{t^n\,\partial u^m},
\]
\[F_{0,0}=f_0,\quad F_{1,0}=\PA{f_t+f\,f_u}_0,\quad F_{1,1}=\PA{f_u}_0,\]
\[
	F_{2,0}=\PA{f_{2t}+2\,f\,f_{t,u}+f^2\,f_{2u}}_0,\quad
	F_{2,1}=\PA{f_{t,u}+f\,f_{2u}}_0,\quad
	F_{2,2}=\PA{f_{2u}}_0,
\]
\[\cdots\]
\[
	F_{i,j}=\C{\sum_{k=0}^{i-j}\COMB{i-j}{k}\,f^k\,f_{(i-j-k)\,t,(j+k)\,u}}_0,
	\quad j\leq i.
\]

The following recurrence rule is hold:
\EQ{\DD{F_{i,j}}{t}=F_{i+1,j}+(i-j)\,F_{i,j+1}\,F_{1,0}.}{EqdF}

Then,
\[\left.\DD{u}{t}\right|_0=F_{0,0},\]
\[\left.\D{u}{t}{2}\right|_0=F_{1,0},\]
\[\left.\D{u}{t}{3}\right|_0=F_{2,0}+F_{1,1}\,F_{1,0},\]
\[
	\left.\D{u}{t}{4}\right|_0=F_{3,0}+3\,F_{2,1}\,F_{1,0}+F_{2,0}\,F_{1,1}
	+F_{1,1}^2\,F_{1,0},
\]
\[
	\left.\D{u}{t}{5}\right|_0=F_{4,0}+6\,F_{3,1}\,F_{1,0}+F_{3,0}\,F_{1,1}
	+3\,F_{2,2}\,F_{1,0}^2+4\,F_{2,1}\,F_{2,0}+7\,F_{2,1}\,F_{1,1}\,F_{1,0}
\]
\[
	+F_{2,0}\,F_{1,1}^2+F_{1,1}^3\,F_{1,0},
\]

When $f=f(t)$ is a function depending only in time, the above relatons are
considerably simplified:
\[
	\left.\D{u}{t}{n}\right|_0=F_{n,0};\quad
	F_{i,j}=0,\;\forall j>0.
\]

Numerical methods to solve ODE (\ref{EqODE}) are based in approximations of the
Taylor's serie. For instance, in 5-th order:
\[
	u\PA{t_0+h}=u_0+h\,\left.\DD{u}{t}\right|_0
	+\frac{h^2}{2}\,\left.\D{u}{t}{2}\right|_0
	+\frac{h^3}{6}\,\left.\D{u}{t}{3}\right|_0
	+\frac{h^4}{24}\,\left.\D{u}{t}{4}\right|_0
	+\frac{h^5}{120}\,\left.\D{u}{t}{5}\right|_0
\]
\[+O\PA{h^6}\]
\[
	=u_0+h\,F_{0,0}+\frac{h^2}{2}\,F_{1,0}
	+\frac{h^3}{6}\,\PA{F_{2,0}+F_{1,1}\,F_{1,0}}
\]
\[
	+\frac{h^4}{24}\,\PA{F_{3,0}+3\,F_{2,1}\,F_{1,0}+F_{2,0}\,F_{1,1}
	+F_{1,1}^2\,F_{1,0}}
\]
\[
	+\frac{h^5}{120}\,\left(F_{4,0}+6\,F_{3,1}\,F_{1,0}+F_{3,0}\,F_{1,1}
	+3\,F_{2,2}\,F_{1,0}^2+4\,F_{2,1}\,F_{2,0}+7\,F_{2,1}\,F_{1,1}\,F_{1,0}
	\right.
\]
\EQ{\left.+F_{2,0}\,F_{1,1}^2+F_{1,1}^3\,F_{1,0}\right)+O\PA{h^6}.}{EqTaylor}

\subsection{Decay ODE}

A common particular case of ODE are the ''decay equations'' defined as:
\EQ{\DD{u}{t}=-|g(u)|\,u,\quad g_u\geq0.}{EqDODE}
with $g$ an increasing function on $u$. In these equations the variable module
decreases always in time:
\[\frac{du}{u}=-|g|\,dt\Rightarrow\]
\[u(t)=u_0\,\exp\PA{-\int_0^t|g|\,dt}\Rightarrow\]
\EQ{|u(t)|=\ABS{u_0}\,\exp\PA{-\int_0^t|g|\,dt.}}{EqDecay}
As $|g|$ is positive the integral is an increasing function. Then, $|u|$ is a
decreasing function. Another interesting property of these equations is that $u$
cannot change the sign:
\EQ{\frac{u(t)}{u_0}=\exp\PA{-\int_0^t|g|\,dt}\geq 0.}{EqDODEP}
Solving DODE (\ref{EqDODE}) numerically in first order using the Euler's rule:
\[u\PA{t_0+h}=u\PA{t_0}-h\,\ABS{g\PA{u\PA{t_0}}}\,u\PA{t_0}\Rightarrow\]
\EQ{\frac{u\PA{t_0+h}}{u\PA{t_0}}=1-h\,\ABS{g\PA{u\PA{t_0}}}.}{EqDODEI}
Then, to prevent numerical variable sign changes the time step size has to be
limited by:
\EQ
{
	\frac{u\PA{t_0+h}}{u\PA{t_0}}\geq0\Rightarrow\;
	1-h\,\ABS{g\PA{u\PA{t_0}}}\geq0\Rightarrow\;
	h\leq\frac{1}{|g|}.
}{EqDODELimit}
In these equations, we use the time step size as:
\EQ{h=\frac{k_t}{|g|},}{EqDODE dt}
with $k_t\leq1$ the dimensionless stability factor.

\subsection{Ballistic ODE}

Ballistic equations are a particular form of ODE follow a form:
\EQ{\ddot{\vec{r}}(t)=\ddot{\vec{r}}\PA{\dot{\vec{r}}(t)},}{EqBODE}
where $\vec{r}$, $\dot{\vec{r}}$, $\ddot{\vec{r}}$ are the vectors of position,
velocity and acceleration respectively.

\section{Runge-Kutta methods}

We use a variation of the Einstein's notation, where repeated indeces are
summed, in order to simplify the notation:
\EQ{f_{i,j}\,g_j\equiv\sum_{j=0}^{i-1}f_{i,j}\,g_j.}{EqNotation}
Runge-Kutta methods are methods in the form:
\EQ
{
	u_i=u_0+h\,b_{i,j}\,f_j,\quad
	f_i=f\PA{t+d_i\,h,u_i},\quad
	d_0=0,\quad
	d_i=\sum_{j=0}^{i-1}b_{i,j}.
}{EqRK}
Every $i$-th step introduces $i$ new freedom degrees.

Developing the steps in Taylor's series to 5-th order:
\[u_1=u_0+h\,d_1\,F_{0,0},\]
\[
	f_1=F_{0,0}+h\,d_1\,F_{1,0}+\frac{h^2\,d_1^2}{2}\,F_{2,0}
	+\frac{h^3\,d_1^3}{6}\,F_{3,0}+\frac{h^4\,d_1^4}{24}\,F_{4,0}+O\PA{h^5},
\]
\[
	u_2=u_0+h\,d_2\,F_{0,0}+h^2\,b_{2,j}\,d_j\,F_{1,0}
	+\frac{h^3\,b_{2,j}\,d_j^2}{2}\,F_{2,0}
	+\frac{h^4\,b_{2,j}\,d_j^3}{6}\,F_{3,0}
\]
\[	+\frac{h^5\,b_{2,j}\,d_j^4}{24}\,F_{4,0}+O\PA{h^6},\]
\[
	f_2=F_{0,0}+h\,d_2\,F_{1,0}+\frac{h^2\,d_2^2}{2}\,F_{2,0}
	+h^2\,b_{2,j}\,d_j\,F_{1,1}\,F_{1,0}+\frac{h^3\,d_2^3}{6}\,F_{3,0}
\]
\[
	+\frac{h^3\,b_{2,j}\,d_j^2}{2}\,F_{2,0}\,F_{1,1}
	+\frac{h^4\,d_2^4}{24}\,F_{4,0}
	+\frac{h^4\,d_2^2\,b_{2,j}\,d_j}{2}\,F_{3,1}\,F_{1,0}
\]
\[
	+\frac{h^4\,b_{2,j}\,d_j^3}{6}\,F_{3,0}\,F_{1,1}
	+\frac{h^4\,\PA{b_{2,j}\,d_j}^2}{2}\,F_{2,2}\,F_{1,0}^2
	+\frac{h^4\,d_2\,b_{2,j}\,d_j^2}{2}\,F_{2,1}\,F_{2,0}
	+O\PA{h^5},
\]
\[
	u_3=u_0+h\,d_3\,F_{0,0}+h^2\,b_{3,j}\,d_j\,F_{1,0}
	+\frac{h^3\,b_{3,j}\,d_j^2}{2}\,F_{2,0}
	+h^3\,b_{3,j}\,b_{j,k}\,d_k\,F_{1,1}\,F_{1,0}
\]
\[
	+\frac{h^4\,b_{3,j}\,d_j^3}{6}\,F_{3,0}
	+\frac{h^4\,b_{3,j}\,b_{j,k}\,d_k^2}{2}\,F_{2,0}\,F_{1,1}
	+\frac{h^5\,b_{3,j}\,d_j^4}{24}\,F_{4,0}
\]
\[
	+\frac{h^5\,b_{3,j}\,d_j^2\,b_{j,k}\,d_k}{2}\,F_{3,1}\,F_{1,0}
	+\frac{h^5\,b_{3,j}\,b_{j,k}\,d_k^3}{6}\,F_{3,0}\,F_{1,1}
	+\frac{h^5\,b_{3,j}\,\PA{b_{j,k}\,d_k}^2}{2}\,F_{2,2}\,F_{1,0}^2
\]
\[
	+\frac{h^5\,b_{3,j}\,d_j\,b_{j,k}\,d_k^2}{2}\,F_{2,1}\,F_{2,0}
	+O\PA{h^6}
\]
\[
	f_3=F_{0,0}+h\,d_3\,F_{1,0}+\frac{h^2\,d_3^2}{2}\,F_{2,0}
	+h^2\,b_{3,j}\,d_j\,F_{1,1}\,F_{1,0}+\frac{h^3\,d_3^3}{6}\,F_{3,0}
\]
\[
	+h^3\,d_3\,b_{3,j}\,d_j\,F_{2,1}\,F_{1,0}
	+\frac{h^3\,b_{3,j}\,d_j^2}{2}\,F_{2,0}\,F_{1,1}
	+h^3\,b_{3,j}\,b_{j,k}\,d_k\,F_{1,1}^2\,F_{1,0}
	+\frac{h^4\,d_3^4}{24}\,F_{4,0}
\]
\[
	+\frac{h^4\,d_3^2\,b_{3,j}\,d_j}{2}\,F_{3,1}\,F_{1,0}
	+\frac{h^4\,b_{3,j}\,d_j^3}{6}\,F_{3,0}\,F_{1,1}
	+\frac{h^4\,\PA{b_{3,j}\,d_j}^2}{2}\,F_{2,2}\,F_{1,0}^2
\]
\[
	+\frac{h^4\,d_3\,b_{3,j}\,d_j^2}{2}\,F_{2,1}\,F_{2,0}
	+h^4\,d_3\,b_{3,j}\,b_{j,k}\,d_k\,F_{2,1}\,F_{1,1}\,F_{1,0}
	+\frac{h^4\,b_{3,j}\,b_{j,k}\,d_k^2}{2}\,F_{2,0}\,F_{1,1}^2
\]
\[+O\PA{h^5},\]
\[
	u_4=u_0+h\,d_4\,F_{0,0}+h^2\,b_{4,j}\,d_j\,F_{1,0}
	+\frac{h^3\,b_{4,j}\,d_j^2}{2}\,F_{2,0}
	+h^3\,b_{4,j}\,b_{j,k}\,d_k\,F_{1,1}\,F_{1,0}
\]
\[
	+\frac{h^4\,b_{4,j}\,d_j^3}{6}\,F_{3,0}
	+h^4\,b_{4,j}\,d_j\,b_{j,k}\,d_k\,F_{2,1}\,F_{1,0}
	+\frac{h^4\,b_{4,j}\,b_{j,k}\,d_k^2}{2}\,F_{2,0}\,F_{1,1}
\]
\[
	+h^4\,b_{4,j}\,b_{j,k}\,b_{k,l}\,d_l\,F_{1,1}^2\,F_{1,0}
	+\frac{h^5\,b_{4,j}\,d_j^4}{24}\,F_{4,0}
	+\frac{h^5\,b_{4,j}\,d_j^2\,b_{j,k}\,d_k}{2}\,F_{3,1}\,F_{1,0}
\]
\[
	+\frac{h^5\,b_{4,j}\,b_{j,k}\,d_k^3}{6}\,F_{3,0}\,F_{1,1}
	+\frac{h^5\,b_{4,j}\,\PA{b_{j,k}\,d_k}^2}{2}\,F_{2,2}\,F_{1,0}^2
	+\frac{h^5\,b_{4,j}\,d_j\,b_{j,k}\,d_k^2}{2}\,F_{2,1}\,F_{2,0}
\]
\[
	+h^5\,b_{4,j}\,d_j\,b_{j,k}\,b_{k,l}\,d_l\,F_{2,1}\,F_{1,1}\,F_{1,0}
	+\frac{h^5\,b_{4,j}\,b_{j,k}\,b_{k,l}\,d_k^2}{2}\,F_{2,0}\,F_{1,1}^2
	+O\PA{h^6}
\]

Comparing with the Taylor's series (\ref{EqTaylor}) a $i$-steps Runge-Kutta
method has to hold the following conditions to get different approximation
orders:
\begin{itemize}
\item First order:
	\EQ{d_i=1.}{EqRKI}
\item Second order:
	\EQ{b_{i,j}\,d_j=\frac12.}{EqRKII}
\item Third order:
	\EQ
	{
		b_{i,j}\,d_j^2=\frac13,\quad
		b_{i,j}\,b_{j,k}\,d_k=\frac16.
	}{EqRKIII}
\item Fourth order:
	\EQ
	{
		b_{i,j}\,d_j^3=\frac14,\quad
		b_{i,j}\,d_j\,b_{j,k}\,d_k=\frac18,\quad
		b_{i,j}\,b_{j,k}\,d_k^2=\frac1{12},\quad
		b_{i,j}\,b_{j,k}\,b_{k,l}\,d_l=\frac1{24}.
	}{EqRKIV}
\item Fifth order:
	\[
		b_{i,j}\,d_j^4=\frac15,\quad
		b_{i,j}\,d_j^2\,b_{j,k}\,d_k=\frac1{10},\quad
		b_{i,j}\,b_{j,k}\,d_k^3=\frac1{20},
	\]
	\[
		b_{i,j}\,\PA{b_{j,k}\,d_k}^2=\frac1{20},\quad
		b_{i,j}\,d_j\,b_{j,k}\,d_k^2=\frac1{15},\quad
		b_{i,j}\,d_j\,b_{j,k}\,b_{k,l}\,d_l=\frac7{120},
	\]
	\EQ
	{
		b_{i,j}\,b_{j,k}\,b_{k,l}\,d_l^2=\frac1{60},\quad
		b_{i,j}\,b_{j,k}\,b_{k,l}\,b_{l,m}\,d_m=\frac1{120}.
	}{EqRKV}
\end{itemize}

On equations depending only in time, first of the above equations are
sufficient:
\begin{itemize}
\item $n$-th order:
	\EQ{b_{i,j}\,d_j^{n-1}=\frac1n.}{EqRKt}
\end{itemize}

\subsection{Strong stability}

General Runge-Kutta methods (\ref{EqRK}) can also be expressed as:
\EQ
{
	u_i=\sum_{j=0}^{i-1}a_{i,j}\,\PA{u_j+h\,c_{i,j}\,f_j},\quad
	\sum_{j=0}^{i-1}a_{i,j}=1.
}{EqRKac}
The following relation is hold:
\EQ{b_{i,j}=(a\,c)_{i,j}+\sum_{k=j+1}^{i-1}a_{i,k}\,b_{k,j}.}{EqRKacb}
This form introduces new $i-1$ freedom degrees for each $i$-th step.

In the context of propagation equations it has been demonstrated that a
Runge-Kutta method preserves monotonicity, and therefore absence of numerical
spurious oscillations, if:
\EQ{a_{i,j},\,c_{i,j}\geq0,\quad k_t\leq\frac1{\max\PA{c_{i,j}}}}{EqRKTVD}

\subsection{Simple stability}

On decay ODE a more simple stability condition can be obtained. In these
equations we define a Runge-Kutta method as ''simple stable'' when it preserves
the variable sign for each time step. Assuming positive $u$:
\[
	b_{i,j}\geq0\Rightarrow\;
	u_i=u_0-h\,b_{i,j}\,\ABS{g_j}\,u_j\leq u_0\Rightarrow\;
	f_i\leq f_0\Rightarrow
\]
\EQ
{
	u_i\geq u_0-h\,\sum_jb_{i,j}\,\ABS{g_0}\,u_0
	=u_0\,\PA{1-h\,d_i\,\ABS{g_0}}
}{EqRKSimpleCondition}
wherefrom preserving sign in $u$ is ensured by:
\EQ{b_{i,j}\geq0,\quad k_t\leq\frac{1}{\max\PA{d_i}}}{EqRKSimple}
These conditions are sufficient but not necessary.

A strong stable method is always simple stable, (\ref{EqRKTVD}) conditions
ensures:
\[\frac{u_j+h\,c_{i,j}\,f_j}{u_j}\geq0\Rightarrow\]
\EQ{\frac{u_i}{u_0}=\sum_ja_{i,j}\,\frac{u_j+h\,c_{i,j}\,f_j}{u_j}\geq0.}
{EqRKSimpleStrong}

\subsection{Ballistic ODE}

For ballistic ODE, Runge-Kutta methods are adapted by doing:
\EQ
{
	\vec{r}_i=\vec{r}_0+h\,b_{i,j}\,\dot{\vec{r}}_j,\quad
	\dot{\vec{r}}_i=\dot{\vec{r}}_0+h\,b_{i,j}\,\ddot{\vec{r}}_j,\quad
	\ddot{\vec{r}}_i=\ddot{\vec{r}}\PA{\dot{\vec{r}}_i}.
}{EqRKBODE}

\subsection{One step first order (Euler's method)}

The one step first order method, proposed by Euler, is defined by the
coefficient matrices of table~\ref{TableRKiI}.
\begin{table}[ht]
	\centering
	\begin{tabular}{c|c}
		$d_{i}$ & $b_{i,j}$ \\ \hline
		1 & 1
	\end{tabular},~
	\begin{tabular}{c}
		$a_{i,j}$ \\ \hline
		1
	\end{tabular},~
	\begin{tabular}{c}
		$c_{i,j}$ \\ \hline
		1
	\end{tabular}
	\caption{Coefficients of the one step first order Runge-Kutta method (RK1-1).
		\label{TableRKiI}}
\end{table}
This a first order method with strong and simple stability for $k_t\leq1$.

\subsection{Two steps second order}

We consider two interesting two steps second order methods defined by the
coefficients matrices of tables~\ref{TableRKiiIIa} and~\ref{TableRKiiIIb}.
First method is second order, third order in equations depending only in time,
simple stable for $k_t\leq1$ and strong stable for $k_t\leq1/2$. Second method
is second order and simple and strong stable for $k_t\leq1$.
\begin{table}[ht]
	\centering
	\begin{tabular}{c|cc}
		$d_{i}$ & $b_{i,j}$ \\ \hline
		2/3 & 2/3 \\
		1 & 1/4 & 3/4
	\end{tabular},~
	\begin{tabular}{cc}
		$a_{i,j}$ \\ \hline
		1 \\
		5/8 & 3/8
	\end{tabular},~
	\begin{tabular}{cc}
		$c_{i,j}$ \\ \hline
		2/3 \\
		0 & 2
	\end{tabular}
	\caption{Coefficients of the two steps second order Runge-Kutta method optime
		in accuracy (RK2-2(3)).\label{TableRKiiIIa}}
\end{table}
\begin{table}[ht]
	\centering
	\begin{tabular}{c|cc}
		$d_{i}$ & $b_{i,j}$ \\ \hline
		1 & 1 \\
		1 & 1/2 & 1/2
	\end{tabular},~
	\begin{tabular}{cc}
		$a_{i,j}$ \\ \hline
		1 \\
		1/2 & 1/2
	\end{tabular},~
	\begin{tabular}{cc}
		$c_{i,j}$ \\ \hline
		1 \\
		0 & 1
	\end{tabular}
	\caption{Coefficients of the two steps second order Runge-Kutta method optime
		in strong stability (RK2-2).\label{TableRKiiIIb}}
\end{table}

\subsection{Three steps third order}

The most interesting three steps Runge-Kutta is the defined by the coefficients
of the table~\ref{TableRKiiiIII}.
This method is third order, fourth order in equations depending only in time and
simple and strong stable for $k_t\leq1$.
\begin{table}[ht]
	\centering
	\begin{tabular}{c|ccc}
		$d_{i}$ & $b_{i,j}$ \\ \hline
		1 & 1 \\
		1/2 & 1/4 & 1/4 \\
		1 & 1/6 & 1/6 & 2/3
	\end{tabular},~
	\begin{tabular}{ccc}
		$a_{i,j}$ \\ \hline
		1 \\
		3/4 & 1/4 \\
		1/3 & 0 & 2/3
	\end{tabular},~
	\begin{tabular}{ccc}
		$c_{i,j}$ \\ \hline
		1 \\
		0 & 1 \\
		0 & 0 & 1
	\end{tabular}
	\caption{Coefficients of the three steps third order Runge-Kutta method
		(RK3-3(4)).\label{TableRKiiiIII}}
\end{table}

\subsection{Four steps fourth order}

The only four steps fourth order simple stable for $k_t\leq1$ is the defined by
the coefficients of the table~\ref{TableRKivIV}.
They are not fourth order strong stable Runge-Kutta methods of four steps.
\begin{table}[ht]
	\centering
	\begin{tabular}{c|cccc}
		$d_{i}$ & $b_{i,j}$ \\ \hline
		1/2 & 1/2 \\
		1/2 & 0 & 1/2 \\
		1 & 0 & 0 & 1 \\
		1 & 1/6 & 1/3 & 1/3 & 1/6
	\end{tabular}
	\caption{Coefficients of the four steps fourth order Runge-Kutta method
		(RK4-4).\label{TableRKivIV}}
\end{table}

\subsection{Five steps fourth order}

It is not possible to build a fifth order Runge-Kutta method with five steps.
The method defined in table~\ref{TableRKvIV} is simple stable for $k_t\leq1$,
fourth order and fifth order in equations depending only on time.
\begin{table}[ht]
	\renewcommand*{\arraystretch}{1.3}
	\centering
	\begin{tabular}{c|ccccc}
		$d_{i}$ & $b_{i,j}$ \\ \hline
		$\frac{11}{40}$ & $\frac{11}{40}$ \\
		1 & $\frac{223}{803}$ & $\frac{580}{803}$ \\
		$\frac12$ & $\frac{1.41}{146}$ & $\frac25$ & $\frac{13.19}{146}$ \\
		$\frac{17}{20}$ & $\frac{661.15057}{19272}$ &
			$\frac{11289.7731}{46574}$ & $\frac{156.50557}{50808}$
			& $\frac{2737}{4800}$ \\
		1 & $\frac{107}{1122}$ & $\frac{64000}{198099}$ & $\frac7{522}$ &
			$\frac{46}{189}$ & $\frac{8000}{24633}$
	\end{tabular}
	\caption{Coefficients of the five steps fourth order, fifth order in
		equations depending only in time, Runge-Kutta method (RK5-4(5)).
		\label{TableRKvIV}}
\end{table}

\section{Runge-Kutta pairs}

\EQ{\hat{u}_i=u_0+h\,\hat{b}_{i,j}\,f_j.}{EqRKpair}

\EQ{E_i=\hat{u}_i-u_i=h\,\PA{\hat{b}-b}_{i,j}\,f_j.}{EqRKpairE}

\subsection{Two steps first-second order pairs}

The two second order methods defined by the coefficients matrices of
tables~\ref{TableRKiiIIa} and~\ref{TableRKiiIIb} can be combined con the
Euler's method defined in table~\ref{TableRKiI} to build the two steps second
order Runge-Kutta pairs defined in tables~\ref{TableRKIiiIIa}
and~\ref{TableRKIiiIIb}.
\begin{table}[ht]
	\centering
	\begin{tabular}{c|cc}
		$d_{i}$ & $b_{i,j}$ \\ \hline
		2/3 & 2/3 \\
		1 & 1/4 & 3/4 \\ \hline
		$\hat{b}_{2,j}$ & 1 & 0 \\ \hline
		$e_{2,j}$ & 3/4 & -3/4
	\end{tabular}
	\caption{Coefficients of the two steps first-second order, first-third order
		in equations depending only in time, Runge-Kutta pair (RK2-1-2(3)).
		\label{TableRKIiiIIa}}
\end{table}
\begin{table}[ht]
	\centering
	\begin{tabular}{c|cc}
		$d_{i}$ & $b_{i,j}$ \\ \hline
		1 & 1 \\
		1 & 1/2 & 1/2 \\ \hline
		$\hat{b}_{2,j}$ & 1 & 0 \\ \hline
		$e_{2,j}$ & 1/2 & -1/2
	\end{tabular}
	\caption{Coefficients of the two steps first-second order Runge-Kutta pair
		optime in strong stability (RK2-1-2).\label{TableRKIiiIIb}}
\end{table}

\subsection{Three steps second-third order pair}

The three steps third order Runge-Kutta defined in table~\ref{TableRKiiiIII}
can be combined with the second order defined in table~\ref{TableRKiiIIb} to
produce the three steps second-third order pair defined in
table~\ref{TableRKIIiiiIII}.
\begin{table}[ht]
	\centering
	\begin{tabular}{c|ccc}
		$d_{i}$ & $b_{i,j}$ \\ \hline
		1 & 1 \\
		1/2 & 1/4 & 1/4 \\
		1 & 1/6 & 1/6 & 2/3 \\ \hline
		$\hat{b}_{3,j}$ & 1/2 & 1/2 & 0 \\ \hline
		$e_{3,j}$ & 1/3 & 1/3 & -2/3
	\end{tabular}
	\caption{Coefficients of the three steps second-third order, second-fourth
		order in equations depending only in time, Runge-Kutta pair (RK3-2-3(4)).
		\label{TableRKIIiiiIII}}
\end{table}

\subsection{Four steps second-third order pair}

It is not possible to build a four steps third-fourth order pair but they are
infinity two-three order pairs of four steps. The defined in
table~\ref{TableRKIIivIII} is moreover simple stable for $k_t\leq1$ and
third-fourth order in equations depending only on time.
\begin{table}[ht]
	\renewcommand*{\arraystretch}{1.3}
	\centering
	\begin{tabular}{c|cccc}
		$d_{i}$ & $b_{i,j}$ \\ \hline
		$\frac13$ & $\frac13$ \\
		1 & 0 & 1 \\
		$\frac23$ & $\frac16$ & $\frac14$ & $\frac14$ \\
		1 & $\frac18$ & $\frac38$ & $\frac18$ & $\frac38$ \\ \hline
		$\hat{b}_{4,j}$ & 0 & $\frac34$ & $\frac14$ & 0 \\ \hline
		$e_{4,j}$ & $-\frac18$ & $\frac38$ & $\frac18$ & $-\frac38$
	\end{tabular}
	\caption{Coefficients of the four steps second-third order, third-fourth order
		in equations depending only in time, Runge-Kutta pair (RK4-2(3)-3(4)).
		\label{TableRKIIivIII}}
\end{table}

\subsection{Five steps third-fourth order pair}

They are infinity third-fourth order pairs of five steps. The defined in
table~\ref{TableRKIIIvIV} is moreover simple stable for $k_t\leq1$ and
fourth-fifth order in equations depending only on time.
\begin{table}[ht]
	\renewcommand*{\arraystretch}{1.3}
	\centering
	\begin{tabular}{c|ccccc}
		$d_{i}$ & $b_{i,j}$ \\ \hline
		$\frac{11}{40}$ & $\frac{11}{40}$ \\
		1 & $\frac{223}{803}$ & $\frac{580}{803}$ \\
		$\frac12$ & $\frac{141}{14600}$ & $\frac25$ & $\frac{1319}{14600}$ \\
		$\frac{17}{20}$ & $\frac{66115057}{1927200000}$ &
			$\frac{112897731}{465740000}$ & $\frac{15650557}{5080800000}$
			& $\frac{2737}{4800}$ \\
		1 & $\frac{107}{1122}$ & $\frac{64000}{198099}$ & $\frac7{522}$ &
			$\frac{46}{189}$ & $\frac{8000}{24633}$ \\ \hline
		$\hat{b}_{5,j}$ & $\frac16$ & 0 & $\frac16$ & $\frac23$ & 0 \\ \hline
		$e_{5,j}$ & $\frac{40}{561}$ & $-\frac{64000}{198099}$ & $\frac{40}{261}$
			& $\frac{80}{189}$ & $-\frac{8000}{24633}$
	\end{tabular}
	\caption{Coefficients of the five steps third-fourth order, fourth-fifth order
		in equations depending only in time, Runge-Kutta pair (RK-3(4)-4(5)).
		\label{TableRKIIIvIV}}
\end{table}

\section{Multi-steps methods}

\section{Multi-steps pairs}

\section{Tests}

\section{Conclusions}

\end{document}


\subsection{Support Vector Machine}

\begin{figure}[h]
\center\includegraphics[width = .8\textwidth]{svm.png}
\caption{Parameter Tuning of SVM}
\label{svm}
\end{figure}

Kernel support vector machines (SVMs) are supervised learning models with associated learning algorithms commonly used in classification.\cite{cristianini2000introduction}

The effectiveness of SVM depends on the selection of kernel, the kernel's parameters, and soft margin parameter C. A common choice is a Gaussian kernel, which has a single parameter $\gamma$. The best combination of C and $\gamma$ is often selected by a grid search with exponentially growing sequences of C and $\gamma$. Each combination of parameter choices is checked using cross validation, and the parameters with best cross-validation accuracy are picked. We can do this with function \emph{tune} given a list of C. In Figure \ref{svm} we can see the CV error and test error of SVM using different cost and gamma. The best C is 0.05 and the best $\gamma$ is 0.135.

With the parameters tunned we can conduct classfication. The test error are shown in Table \ref{tblsvm}. The Type \uppercase\expandafter{\romannumeral1} error is slightly larger than random forest, but Type \uppercase\expandafter{\romannumeral2} error is significantly lower than random forest. So SVM made less mistake in predicting raining days compared to random forest. This could partly be explained by the fact that this isn't a linearly separable case.

\begin{table}[h]
\setlength{\belowcaptionskip}{5pt}
\caption{Confusion Matrix and Error Rates of SVM}
\label{tblsvm}
\centering
\renewcommand\arraystretch{1.5}
\begin{tabular}{rrrrr}
\hline
\hline
 & & \multicolumn{2}{c}{True Condition} & \\
\hline
 & & Non-Precipitation & Precipitation & \\
\cline{1-4}
\multirow{2}{*}{Prediction} & {Non-Precipitation} & 326 & 47 & \\
\cline{2-4}
&Precipitation&54&231&\\
\hline
&Error Rate & 0.142 & 0.169 & 0.1535\\
\cline{2-5}
& & Type \uppercase\expandafter{\romannumeral1} & Type \uppercase\expandafter{\romannumeral2} & Overall\\
\hline
\end{tabular}
\end{table}


\section*{Exercise 7.1}
The estimator of p is
\spl{
    \hat{p}=\frac{E}{n}=\frac{\overline{X}}{n}=\frac{0\cdot39+1\cdot23+2\cdot12+3\cdot1}{75\cdot24}=\frac{1}{36}.
}

The expected frequency can be calculated by $E_i=nP[X=i]=n\binom{n}{i}\hat{p}^i(1-\hat{p})^{n-i}$.
\begin{table}[H]
    \centering
    \begin{tabular}{ccccc}\hline
        Values & 0 & 1 & 2 & 3\\\hline
        Expected Frequency & 38.145 & 26.156 & 8.594 & 2.105\\\hline
    \end{tabular}
\end{table}

We notice that $E_i\geq5$ less than 80\% categories. Thus we merge the last two categories.
\begin{table}[H]
    \centering
    \begin{tabular}{ccccc}\hline
        Values & 0 & 1 & 2\\\hline
        Expected Frequency & 38.145 & 26.156 & 10.699\\\hline
    \end{tabular}
\end{table}

Then,we calculate the Pearson's Statistic
\spl{
    X^2=\sum_{i=1}^N\frac{(O_i-E_i)^2}{E_i}=\frac{(39-38.145)^2}{38.145}+\frac{(23-26.156)^2}{26.156}+\frac{(13-10.699)^2}{10.699}=0.876.
}

The degree of freedom is $3-1-1=1$. Hence, $X^2=0.876<\chi^2_{0.05,1}=3.84$. Hence, we conclude that follows binomial distribution at 5\% level of significance.

\section*{Exercise 7.2}
\begin{table}[h]
    \centering
    \begin{tabular}{c|cccc|c}
        Mounting Position & A & B & C & D & \\\hline
        1 & 22 & 46 & 18 & 9 & 95\\
        2 & 4 & 17 & 6 & 12 & 39\\\hline
        & 26 & 63 & 24 & 21 & 134\\
    \end{tabular}
\end{table}

\spl{
    H_0:\ p_{ij}=p_{i\cdot}p_{\cdot j}
}

Since $E_{ij}=\frac{n_{i\cdot}n_{\cdot j}}{n}$, then
\spl{
    E_{11}&=18.43,\quad E_{12}=44.66,\quad E_{13}=17.01,\quad E_{14}=14.88\\
    E_{21}&=7.57,\quad E_{22}=18.34,\quad E_{23}=6.99,\quad E_{24}=6.11\\
}

\spl{
    X_{(2-1)(4-1)}^2=\sum_{i=1}^r\sum_{j=1}^c\frac{(O_{ij}-E_{ij})^2}{E_{ij}}=10.71.
}

Since the statistic follows 3 degrees of freedom chi-squared distribution, we compare the value with $\chi_{0.05,3}^2=7.8147$. We find that $X_3^2>\chi_{0.05,3}^2$. Thus, we reject the hypothesis at 5\% level of significance. We claim that the type of failure depends on the mounting position.

\section*{Exercise 7.3}
\begin{table}[h]
    \centering
    \begin{tabular}{c|ccccc|c}
        & 1 & 2 & 3 & 4 & 5 &\\\hline
        Male & 50 &47 & 103 & 76 & 24 & 300\\
        Female & 21 & 27 & 50 & 35 & 17 & 150\\\hline
        & 71 & 74 & 153 & 111 & 41 & 450\\
    \end{tabular}
\end{table}
We assume that the increase in the salary is independent od the workers' gender, saying that
\spl{
    H_0:\ p_{ij}=p_{i\cdot}p_{\cdot j}
}

Since $E_{ij}=\frac{n_{i\cdot}n_{\cdot j}}{n}$, then
\spl{
    E_{11}&=47.33,\quad E_{12}=49.33,\quad E_{13}=102,\quad E_{14}=74,\quad E_{15}=27.33\\
    E_{21}&=23.67,\quad E_{22}=24.67,\quad E_{23}=51,\quad E_{24}=37,\quad E_{25}=13.67\\
}

\spl{
    X_{(2-1)(5-1)}^2=\sum_{i=1}^r\sum_{j=1}^c\frac{(O_{ij}-E_{ij})^2}{E_{ij}}=2.19.
}

Since the statistic follows 4 degrees of freedom chi-squared distribution, we compare the value with $\chi_{0.05,4}^2=9.4877$. We find that $X_4^2<\chi_{0.05,3}^2$. Thus, we support the hypothesis at 5\% level of significance. We claim that increase in salary is independent of the gender.

\section*{Exercise 7.4}
Using the Helmert transformation $\textbf{Y}\rightarrow\textbf{D}$, we rewrite $SSE_{pe}$
\spl{
    SSE_{pe}&=\frac{1}{\sigma^2}\sum_{i=1}^k\sum_{j=1}^{n_i}(Y_{ij}-\overline{Y_{ij}})^2\\
    &=\frac{1}{\sigma^2}\sum_{i=1}^k\bigg[\sum_{j=1}^{n_i}Y_{ij}^2-n_i\overline{Y_{ij}}^2\bigg]\\
    &=\frac{1}{\sigma^2}\sum_{i=1}^k\bigg[\sum_{j=1}^{n_i}D_{ij}^2-D_{i1}^2\bigg]\\
    &=\frac{1}{\sigma^2}\sum_{i=1}^k\bigg[\sum_{j=2}^{n_i}D_{ij}^2\bigg]\\
    &=\frac{1}{\sigma^2}\bigg[\sum_{i=1}^nD_i^2-\sum_{i=1}^kD_{i1}^2\bigg]\\
    &=\sum_{i=1}^n\frac{D_i^2}{\sigma^2}-\sum_{i=1}^k\frac{D_{i1}^2}{\sigma^2},
}
which is the sum of square of $(n-k)$ normally distributed variables. Hence, it follows $(n-k)$ degrees of freedom chi-squared distribution.

\section*{Exercise 7.5}
Consider $\cov(\overline{Y},\hat{\beta}_1)$, we obtain
\spl{
    \hat{\beta}_1&=B_1=\frac{S_{xy}}{S_{xx}}\\
    \cov(\overline{Y},\hat{\beta}_1)&=E[\overline{Y}\hat{\beta}_1]-E[\overline{Y}]E[B_1]\\
    &=E\bigg[\frac{\sum_{i=1}^n(x_i-\bar{x})Y_i\overline{Y}}{\sum_{i=1}^n(x_i-\bar{x})^2}\bigg]-(\beta_0+\beta_1\bar{x})\beta_1\\
    &=\frac{\sum_{i=1}^n(x_i-\bar{x})E[Y_i\overline{Y}]}{\sum_{i=1}^n(x_i-\bar{x})^2}-(\beta_0+\beta_1\bar{x})\beta_1\\
    &=\frac{\sum_{i=1}^n(x_i-\bar{x})(\beta_0+\beta_1 x_i)(\beta_0+\beta_1\bar{x})}{\sum_{i=1}^n(x_i-\bar{x})^2}-(\beta_0+\beta_1\bar{x})\beta_1\\
    &=0
}

Consider $\cov(\hat{\beta_0},\hat{\beta_1})$, we obtain
\spl{
    \cov(\hat{\beta_0},\hat{\beta_1})&=E[B_0B_1]-E[B_0]E[B_1]\\
    &=E[(\overline{Y}-B_1\bar{x})B_1]-\beta_0\beta_1\\
    &=E[B_1\overline{Y}-B_1^2\bar{x}]-\beta_0\beta_1\\
    &=E[B_1\overline{Y}]-E[B_1^2\bar{x}]-\beta_0\beta_1\\
    &=(\beta_0+\beta_1\bar{x})\beta_1-\bar{x}E[B_1^2]-\beta_0\beta_1\\
    &=\bar{x}(\beta_1^2-E[B_1^2])\\
    &=-\bar{x}(E\bigg[(\frac{S_{xy}}{S_{xx}})^2\bigg]-E\bigg[\frac{S_{xy}}{S_{xx}}\bigg]^2)\\
    &=-\bar{x}\var(B_1)\\
    &=-\bar{x}\frac{\sigma^2}{S_{xx}}\\
    &=-\frac{\bar{x}}{S_{xx}}\sigma^2.
}

\section*{Exercise 7.6}
\enum{
\item
\spl{
    B_1&=\frac{S_{xy}}{S_{xx}}=184.6\\
    B_0&=\overline{Y}-B_1\bar{x}=-2150
}
\item
\spl{
    S^2=\hat{\sigma}^2=\frac{S_{yy}-B_1S_{xy}}{n-2}=1.15\e{5}.
}
\item
For a 100(1-0.1)\% confidence interval,
\spl{
    B_1\pm t_{0.1/2,n-2}\frac{S}{\sqrt{S_{xx}}}&=184.6\pm1.68\cdot\frac{339.2185}{\sqrt{828.2412}}=184.6\pm19.8.\\
    B_0\pm t_{0.1/2,n-2}\frac{S\sqrt{\sum x_i^2}}{\sqrt{nS_{xx}}}&=-2150\pm1.68\cdot\frac{339.2185\cdot182.8296}{\sqrt{42\cdot828.2412}}=-2150\pm560.\\
}
\item
\spl{
    &H_0:\ \text{the linear regression is appropriate}\\
    &H_1:\ \text{the linear regression is not appropriate}
}
\spl{
    SSE_{pe}&=\sum_{i=1}^k\sum_{j=1}^{n_i}(Y_{ij}-\overline{Y}_i)^2=109500.\\
    SSE_{lf}&=SSE-SSE_{pe}=4.49\e{6}.
}
Hence,
\spl{
    F_{37-2,42-37}=\frac{4.49\e{6}/(37-2)}{109500/(42-37)}=5.8621
}

Since $f_{0.05,35,5}=4.4775<F_{35,5}$, then we claim that the linear regression model is not appropriate at 5\% level of significance.

\item
\spl{
    R^2=\frac{S_{yy}-SSE}{S_{yy}}=0.8597.
}
The coefficient of determination is the proportion in $S_{yy}$ that is determined by $X$. The larger $R^2$ is, the more $Y$ are determined by $X$.

\item
\begin{figure}[H]
    \centering
    \includegraphics[height=9cm]{images/1}
    \caption{Residuals vs. Density}
\end{figure}
The assumption of constant variance is unreliable because the is irregular.
}

\section*{Exercise 7.7}
\enum{
\item
\begin{figure}[H]
    \centering
    \includegraphics[height=9cm]{images/2}
    \caption{Linear regression model}
\end{figure}

\item
\spl{
    R^2=\frac{S_{yy}-SSE}{S_{yy}}=0.9998.
}
Hence,
\spl{
    \frac{R\sqrt{n-2}}{\sqrt{1-R^2}}=191.994>t_{0.025,12}=2.18.
}
It implies that our linear regression model is appropriate.

\item
\spl{
    &H_0:\ \text{the linear regression is appropriate}\\
    &H_1:\ \text{the linear regression is not appropriate}
}
\spl{
    SSE_{pe}&=\sum_{i=1}^k\sum_{j=1}^{n_i}(L_{ij}-\overline{L}_i)^2=0.0322.\\
    SSE_{lf}&=SSE-SSE_{pe}=0.0398547-0.0322=0.00765.
}
Hence,
\spl{
    F_{8-2,14-8}=\frac{0.00765/(8-2)}{0.0322/(14-8)}=0.2376.
}

Since $f_{0.05,6,6}=4.2839>F_{6,6}$, then we claim that the linear regression model is appropriate at 5\% level of significance.
}

\section*{Exercise 7.8}
\spl{
    \textbf{LHS}-\textbf{RHS}&=\frac{S_{xy}}{S\sqrt{S_{xx}}}-\frac{\sqrt{n-2}S_{xy}/\sqrt{S_{xx}S_{yy}}}{\sqrt{1-S_{xy}^2/S_{xx}S_{yy}}}\\
    &=\frac{S_{xy}}{S\sqrt{S_{xx}}}-\frac{\sqrt{n-2}S_{xy}}{\sqrt{S_{xx}S_{yy}-S_{xy}^2}}\\
    &=\frac{S_{xy}}{\sqrt{(S_{xx}S_{yy}-S_{xy}^2)/(n-2)}}-\frac{\sqrt{n-2}S_{xy}}{\sqrt{S_{xx}S_{yy}-S_{xy}^2}}\\
    &=\frac{\sqrt{n-2}S_{xy}}{\sqrt{S_{xx}S_{yy}-S_{xy}^2}}-\frac{\sqrt{n-2}S_{xy}}{\sqrt{S_{xx}S_{yy}-S_{xy}^2}}\\
    &=0.\\
}


\section*{Appendix: Curve fitting}
In order to plot the evolution of both Lift and Drag for each phase, the computated velocity - solved from each respective ODE system - has been fitted from the resultant set of data points into a curve.\\
For this, the native \texttt{polyfit} and \texttt{poly2sym} \textsc{matlab} functions have ben employed.

To correctly fit a set of points into a curve and avoid overfitting the polynomial (thus dealing with excessive oscillations and Runge's phenomemenon among others), the lienar Least Squares Method (LLS) has been used. The following plots show the square root sum and the norm of the squared regression value for each phase's velocity fit.

\begin{center}
	\includegraphics[width=0.45\linewidth]{../matlab/1/1reg.jpg}
	\includegraphics[width=0.45\linewidth]{../matlab/1/1fit.jpg}
	\vspace{0.5cm}
	\captionof{figure}{Lift and drag on the cruise phase. Own elaboration.}
	\label{fig:1reg}
	\captionof{figure}{Lift and drag on the cruise phase. Own elaboration.}\vspace{0.25cm}
	\label{fig:1fit}
\end{center}

\begin{center}
	\includegraphics[width=0.45\linewidth]{../matlab/2reg.jpg}
	\includegraphics[width=0.45\linewidth]{../matlab/2fit.jpg}
	\vspace{0.5cm}
	\captionof{figure}{Lift and drag on the half loop. Own elaboration.}
	\label{fig:2reg}
	\captionof{figure}{Lift and drag on the half loop. Own elaboration.}\vspace{0.25cm}
	\label{fig:2fit}
\end{center}

\begin{center}
	\includegraphics[width=0.45\linewidth]{../matlab/3reg.jpg}
	\includegraphics[width=0.45\linewidth]{../matlab/3fit.jpg}
	\vspace{0.5cm}
	\captionof{figure}{Lift and drag on the half loop. Own elaboration.}
	\label{fig:3reg}
	\captionof{figure}{Lift and drag on the half loop. Own elaboration.}\vspace{0.25cm}
	\label{fig:3fit}
\end{center}


Implements the SMT compliant \emph{Simplex} method presented in \cite{DM06}.
Hence, this module can decide the consistency of any conjunction
consisting only of linear real arithmetic constraints. Furthermore,
it might also find the consistency of a conjunction of constraints
even if they are not all linear and calls a backend after removing
some redundant linear constraints, if the linear constraints are satisfiable
and the found solution does not satisfy the non-linear constraints. Note that the
\lraModuleClass might need to communicate a lemma/tautology to a preceding
\satModuleClass, if it receives a constraint with the relation symbol $\neq$
 and the strategy needs for this reason to define a \satModuleClass at any
 position before an \lraModuleClass.

\paragraph{Integer arithmetic} In order to find integer solutions, this
module applies, depending on which settings are used, branch-and-bound,
the construction of Gomory cuts and the generation of cuts from
proofs~\cite{DilligDA11}. It is also supported to combine these approaches. Note that
for all of them the \lraModuleClass needs to communicate a lemma/tautology to a
preceding \satModuleClass and the strategy needs for this reason to define a
\satModuleClass at any position before an \lraModuleClass.

\paragraph{Efficiency} The worst case complexity of the implemented
approach is exponential in the number of variables occurring in the
problem to solve. However, in practice, it performs much faster, and
the worst case applies only on very artificial examples. This module
outperforms any module implementing a method that is designed for
solving formulas with non-linear constraints. If the received formula
contains integer valued variables, the aforementioned methods might not
terminate.


\subsection{Changing the basis}

For any two bases, there is a unique linear mapping from of the element vectors to the other.


%!TEX root = ../main.tex

\chapter{Notation}
\label{appendix:notation}
\vspace{-7mm}

This appendix contains a summary of the notation used in this book.

\softsection{Math notation}

{ \small
\noindent
\begin{tabularx}{\textwidth}{@{}rlp{5.9cm}@{}}
\toprule
Expression  	&	Read as  	& Used to denote			\\
\midrule
$a,b,x,y$
	&
	& variables \\
$=$
	& is equal to
	& expressions that have the same value \\ % have the same value	\\
$\eqdef$
	& is defined as
	& a new variable definition  \\
$a+b$
	& $a$ plus $b$
	& the combined lengths of $a$ and $b$ \\
$a-b$
	& $a$ minus $b$
	& the difference in lengths between $a$ and $b$ \\
$a\times b = ab$
	& $a$ times $b$
	& the area of a rectangle   \\
$a^2= aa$
	& $a$ squared
	& the area of a square of side length $a$ \\
$a^3= aaa$
	& $a$ cubed
	& the volume of a cube of side length $a$ \\
$a^n$
	& $a$ to the $n$
	%raised to the $n$\textsuperscript{th} power
	& $a$ multiplied by itself $n$ times 		\\
$\sqrt{a} = a^{\frac{1}{2}}$
	& square root of $a$
	& the side length of a square of area $a$ \\
$\sqrt[3]{a}= a^{\frac{1}{3}}$
	& cube root of $a$
	& the side length of a cube with volume $a$  \\
$a/b = \frac{a}{b}$
	& $a$ divided by $b$
	& $a$ parts of a whole split into $b$ parts \\[0.8mm]
$a^{-1}= \frac{1}{a}$
	& one over $a$
	& division by $a$ 					\\[2mm]
$f(x)$
	& $f$ of $x$
	& the function $f$ applied to input $x$ 	\\
$f^{-1}$
	& $f$ inverse
	& the inverse function of $f(x)$   \\
$f \circ g$
	& $f$ compose $g$
	& function composition; $f \circ g(x) = f(g(x))$   \\[2mm]
$e^x$
	& $e$ to the $x$
	& the exponential function base $e$ \\
$\ln(x)$
	& natural log of $x$
	& the logarithm base $e$ 				\\
$a^x$
	& $a$ to the $x$
	& the exponential function base $a$ \\
$\log_a(x)$
	& log base $a$ of $x$
	& the logarithm base $a$ 				\\[2mm]
$\theta,\phi$
	& \emph{theta}, \emph{phi}
	& angles 					\\
$\sin,\cos,\tan$
	& sin, cos, tan
	& trigonometric ratios 			\\
%\; \\
$\%$
	& percent
	& proportions of a total; $a\%=\frac{a}{100}$ 		\\
\bottomrule
\end{tabularx}
}%