% Twentyfold Way
\section{Distributions: The Twentyfold Way}
\label{twentyfoldway}

These notes (in particular)	 need review. There is a deeper perspective on distributions that is constructed in terms of functions and equivalence classes, which is poorly developed here so far, and I think there are some errors too. The different "interpretations" are also not well-developed here.

The twentyfold way is a taxonomy of distribution problems developed by Kenneth Bogard in his book \textit{Combinatorics through Guided Discovery} \cite{bogart2004combinatorics}. It divides up the way in which $k$ objects may be assigned to $n$ individuals, subject to whether the objects are distinct or identical, and subject to conditions on how the objects are received.

\begin{quote}
\textit{When we are passing out objects to recipients, we may think of the objects as being either identical or distinct. We may also think of the recipients as being either identical (as in the case of putting fruit into plastic bags in the grocery store) or distinct (as in the case of passing fruit out to children). We may restrict the distributions to those that give at least one object to each recipient, or those that give exactly one object to each recipient, or those that give at most one object to each recipient, or we may have no such restrictions. If the objects are distinct, it may be that the order in which the objects are received is relevant (think about putting books onto the shelves in a bookcase) or that the order in which the objects are received is irrelevant (think about dropping a handful of candy into a child’s trick or treat bag). If we ignore the possibility that the order in which objects are received matters, we have created $2\times2\times4 = 16$ distribution problems. In the cases where a recipient can receive more than one distinct object, we also have four more problems when the order objects are received matters. Thus we have 20 possible distribution problems.} - Bogart, \textit{Combinatorics Though Guided Discovery}, Chapter 3.
\end{quote}

What I like about this approach is that the challenge with most of the basic combinatorics problems is to figure out the right way of counting. For this reason, the idea of having a unified handbook-like taxonomy is very appealing. The weakness (in my opinion) is that the language of "objects" and "recipients" is unclear because in practice it's not obvious which is which: if there are $k$ students and $n$ teachers, do the teachers receive students, or do the students receive a teacher?

A way to resolve this is to say that an object can have only one recipient, but that a recipient might receive more than one object. A more formal path is to think of the act of creating combinations in terms of functions.

\begin{itemize}
\item The elements of the domain are the objects.
\item The elements of the range are the recipients.
\item A function can be many-to-one, but it should not be one-to-many.
\end{itemize}

\subparagraph{Favorite Teachers} At a school with $k$ students and $n$ teachers, the students all have a favorite teacher. (They might all like the same one.). How many ways are there for all of the $k$ students to pick a favorite?

\textit{Objects:} $k$ students. \textit{Recipients:} $n$ teachers. Many students might have one favorite teacher. There are $n^k$ combinations.

\subparagraph{Assembling a Team} Out of a choice of $n$ athletes, a coach must assemble a team of $k$. How many ways are there to form a team?

\textit{Objects:} n athletes. \textit{Recipients:} team, not on the team. Many athletes can be assigned to one outcome of being on the team or not being on the team. There are ${n \choose k}$ combinations for the team, which is the same number as the ${n \choose n-k}$ selections for the bench.

\begin{figure}
  \caption{Bogart's Twentyfold Way}
  \centering
    \includegraphics[width=0.5\textwidth]{twentyfoldway.png}
\end{figure}

% Distinct Objects, No Conditions
\subsection{Distinct Objects}

%% Distinct Recipients
\subsubsection{Distinct Recipients}
The $k$ objects are assigned to $n$ recipients with no conditions as to the number of objects each recipient receives. This is the same as assigning the elements of a $k$-tuple from a selection of $n$ \underline{with} replacement.

\begin{equation}
\begin{array}{l}
S = \{ (a_1,a_2,...,a_k) | a_i \in A, |A| = n \}\\
\\
|S| = n^k
\end{array}
\end{equation}

\subparagraph{Pool Balls into Labeled Buckets} All possible ways to put $k$ pool balls, which all have different numbers and colors, into $n$ labeled buckets. Some of the buckets might be empty, and others might contain more than one of the pool balls.

\subparagraph{Functions} All possible functions $f:x \rightarrow y$ with $\{x | x\in A, |A| = k \}$ and $\{y | y\in B, |B| = n\}$.

\subparagraph{Binary Strings of Length $k$} The $k$ distinct positions of a binary string $(i_1,i_2,...,i_k)$ of length $k$ are assigned to an element of the set $A\in[0,1]$. The number of possible binary strings of length $k$ is $2^k$.

\subparagraph{Subsets of a $k$-Element Set} The subsets of a set of $k$ distinct elements are formed by assigning each of its $k$ distinguishable elements to one of the two labels $A\in [\mathrm{included},\mathrm{excluded}]$. The number of possible subsets, including the empty subset and the full set, is $2^k$.

%% Indistinct Recipients
\subsubsection{Indistinct Recipients}
The $k$ objects are assigned to a recipient that is not distinct.

\begin{equation}
|A| = \sum_{i=1}^k S(n,i)
\end{equation}

Where $S(k,n)$ is the Stirling number of the second kind that gives the number of ways that $k$ objects can be distributed across $n$ non-empty indistinct sets. The sum above takes care of the case where the $k$ objects are divided into \underline{up to} $n$ collections.

A closed form expression for the Stirling Numbers of the second kind is (c.f. section \ref{sec:stirling2}):

\begin{equation}
S(k,n) = \left\{ \begin{array}{c} k \\n \end{array} \right\} = \frac{1}{n!}\sum_{j=0}^n (-1)^{n-j} {n \choose j }j^k
\label{eq:stirling2}
\end{equation}

\subparagraph{Pool Balls into Unlabeled Bags} All possible ways to put $k$ pool balls, which all have different numbers and colors, into $n$ unlabeled bags. Some of the bags might be empty, and others might contain more than one of the pool balls.

% Distinct Objects, At Most One
\subsection{Distinct Objects, Every Recipient Receives At Most One}

%% Distinct Recipients
\subsubsection{Distinct Recipients}
At most one of $k$ distinct objects are assigned to one of $n$ distinct recipients. This is the same as assigning the elements of a $k$-tuple from a selection of $n$ \underline{without} replacement, so that first there are $n$ choices, then $n-1$ choices, $n-2$, etc.

\begin{equation}
\begin{array}{l}
S = \{ (a_1,a_2,...,a_k) | a_i \in A, |A| = n, a_i\neq a_j\}\\
\\
|S| = \frac{n!}{(n-k)!} = n^{\underline{k}}\ \ \mathrm{if\ }k\leq n,\ 0\ \mathrm{otherwise.}
\end{array}
\end{equation}

\subparagraph{At Most One Pool Ball into Labeled Buckets} All possible ways to put $k$ pool balls, which all have different numbers and colors, into $n$ labeled buckets, but the buckets can have at most one pool ball in them. In other words, you choose any $k$ out of the $n$ labeled buckets and put one pool ball into them.

\subparagraph{One-to-One Functions} All possible functions $f:x \rightarrow y$ with $\{x | x\in A, |A| = k \}$ and $\{y | y\in B, |B| = n\}$ subject to the constraint that $f(a) = f(b)$ implies $a=b$. That is, the functions are one-to-one, or injective.

\subparagraph{k-element Permutations of $n$ elements}  Each of the $k$ positions in a $k$-element permutation are distinct objects. These are each assigned to one of $n$ possible values, where each value can only show up once.

\subparagraph{Books on a Shelf} How many ways are there to order $k$ books on a library shelf when there are $n$ different books available.

%% Indistinct Recipients
\subsubsection{Indistinct Recipients}
At most one of $k$ distinct objects are assigned to one of $n$ indistinct recipients. This is the same as assigning the elements of a $k$-tuple from a selection of $n$ \underline{without} replacement. Except ,since the recipients are all indistinct, there is only one type of choice of recipient for each object. Either each object finds a recipient if $k\leq n$, or it is impossible to distribute at most one object to each recipient because $n < k$.

\begin{equation}
\begin{array}{l}
S = \{ (a_1,a_2,...,a_k) | a_i \in A, |A| = n, a_i=a_j\}\\
\\
|S| = 1\ \ \mathrm{if\ }k\leq n,\ 0\ \mathrm{otherwise.}
\end{array}
\end{equation}

\subparagraph{At Most One Pool Ball into Unlabeled Bags} All possible ways to put $k$ pool balls, which all have different numbers and colors, into $n$ unlabeled bags, but the bags can have at most one pool ball in them. The bags are identical, so the only result is one where there are $k$ bags with a ball in them and $n-k$ without a ball in them. If there are not enough bags, then there is no possible result.

\subparagraph{Distributing Candy} There are $n$ pieces of identical candy and $k$ kids. How many ways are there to give each kid a piece of candy? If there is enough candy, the answer is one. Everyone gets candy. If there is not enough candy then the answer is zero. There is no way to give everyone candy if there's not enough candy.

% Distinct Objects, At Least One
\subsection{Distinct Objects, Every Recipient Receives at Least One}

%% Distinct Recipients
\subsubsection{Distinct Recipients}

\begin{equation}
|A| = n!S(k,n) =n! \left\{ \begin{array}{c} k \\n \end{array}\right\} = n! \frac{1}{n!}\sum_{j=0}^n (-1)^{n-j} {n \choose j }j^k\end{equation}

Where $S(k,n)$	 denotes the Stirling function of the second kind.

\subparagraph{At Least One Pool Ball into Labeled Buckets} All possible ways to put $k$ pool balls, which all have different numbers and colors, into $n$ labeled buckets, so that all of the buckets have at least one ball in them. This is the same number of combinations as if the buckets were unlabeled, but multiplied with $n!$ ways of applying a label to them.

\subparagraph{Onto Functions} All possible functions $f:x \rightarrow y$ with $\{x | x\in A, |A| = k \}$ and $\{y | y\in B, |B| = n\}$ subject to the constraint that there is an element $x$ in the domain so that $f(x)=y$ for each element $y$ of the codomain. That is the functions are onto, or surjective.

%% Indistinct Recipients
\subsubsection{Indistinct Recipients}
The number of ways to divide $k$ distinct objects into $n$ non-empty subets is given by the Stirling number of the second kind (c.f. section \ref{sec:stirling2}):

\begin{equation}
|A| = S(k,n) = \left\{ \begin{array}{c} k \\n \end{array} \right\} = \frac{1}{n!}\sum_{j=0}^n (-1)^{n-j} {n \choose j }j^k	\end{equation}

\subparagraph{At Least One Pool Ball into Unlabeled Bags} All possible ways to put $k$ pool balls, which all have different numbers and colors, into $n$ unlabeled bags, so that none of the bags are empty.

% Distinct Objects, Everyone received exactly one
\subsection{Distinct Objects, Every Recipient Receives Exactly One}

% distinct recipients
\subsubsection{Distinct Recipients}
One of $k$ distinct objects are assigned to each of $n$ distinct recipients. This is the same as assigning the elements of a $k$-tuple from a selection of $n$ \underline{without} replacement and with the requirement that all of the $n$ are selected.

\begin{equation}
\begin{array}{l}
S = \{ (a_1,a_2,...,a_k) | a_i \in A, |A| = k = n, a_i\neq a_j\}\\
\\
|S| = n! = k!\ \ \mathrm{if\ }k=n,\ 0\ \mathrm{otherwise.}
\end{array}
\end{equation}

\subparagraph{Exactly One Pool Ball into Each Labeled Buckets} All possible ways to put $k$ pool balls, which all have different numbers and colors, into $n$ labeled buckets, that there is one pool ball in each bucket. This is the same as putting one pool ball into unlabeled buckets and multiplying by the $n!$ ways of attaching a label to the buckets. If there isn't the same amount of balls and buckets then there is no possible way for them to be matched one-for-one.

\subparagraph{Bijective Functions} All possible functions $f:x \rightarrow y$ with $\{x | x\in A, |A| = k \}$ and $\{y | y\in B, |B| = n\}$ subject to the constraints that $f(a) = f(b)$ implies $a=b$ and that there is an element $x$ in the domain so that $f(x)=y$ for each element $y$ of the codomain. That is, the functions are one-to-one and onto, of bijective.

\subparagraph{Permutations} Since each of the $k$ objects is given to a different one of the $n$ recipients, there must be as many recipients as there are objects and $k=n$. The number of ways of assigning $k$ objects to $n$ recipients is $k!=n!$.

\subparagraph{Unique Identifiers} Each of $k$ entries in a database is given one of $n=k$ unique identifiers, so that each identifier leads to an entry and each entry has an identifier.

% indistinct recipients
\subsubsection{Indistinct Recipients}

\begin{equation}
\begin{array}{l}
S = \{ (a_1,a_2,...,a_k) | a_i \in A, |A| = k, a_i=a_j\}\\
\\
|S| = 1\ \ \mathrm{if\ }k = n,\ 0\ \mathrm{otherwise.}
\end{array}
\end{equation}

\subparagraph{Exactly One Pool Ball into Each Unlabeled Bag} All possible ways to put $k$ pool balls, which all have different numbers and colors, into $n$ unlabeled bags, that there is one pool ball in each bag. The result is that you either have $n$ bags with $n=k$ balls in them, if the two have matching numbers, or that you either have a bag or a ball left over and there is no way to match them one-for-one.

\subparagraph{Distribute without Leftovers} $k$ students are assigned to $n$ identical textbooks. How many ways are there for each child to have a textbook so that there are no textbooks left over?

% Distinct Objects, Ordered Groups
\subsection{Distinct Objects, Distributed in Ordered Groups}

%% Distinct Recipients
\subsubsection{Distinct Recipients}
$k$ objects are distributed to $n$ different recipients with an internal ordering, so that each recipient receives an ordered list. This is the same as creating a list of $n$ sequences that are sampled from $k$ \underline{without replacement}.

\begin{equation}
\begin{array}{l}
S = \{ (\mathbf{a}_{\{i_1\}}  = ( a_{i_{1,1}},a_{i_{1,1}},...,a_{i_{1,l}} ),\mathbf{a}_{\{i_2\}},...,\mathbf{a}_{\{i_n\}}) | \sum^n_j |\mathbf{a}_{i_j}| = k, a_{i_{j,i}} \in A, |A| = n \} \\
\\
|S| = \frac{(k + n - 1)!}{(n-1)!} = ( k+n-1)^{\underline{k}} = k! {k+n-1 \choose k}
\end{array}
\end{equation}

\subparagraph{Books on Labeled Bookshelves} $k$ books are distributed across $n$ different bookshelves. The books may all be on the same shelf, or shelves may be empty. The ordering of the books on each of the shelves matters. This is the same as taking all the $k!$ permutations of the $k$ books and multiplying it by the way of dividing that permutation up onto $n$ shelves.

\subparagraph{Ordered Functions}  All functions $f: x \rightarrow y$ with $\{x | x\in A, |A| = k \}$ and $\{y | y\in B, |B| = n\}$ that assign ordered sequences of elements in $x$ to elements of $y$.

%% Indistinct Recipients
\subsubsection{Indistinct Recipients}
$k$ objects are broken up into $n$

\begin{equation}
|S| = \sum_{i=1}^n L(k,i)
\end{equation}

where $L(k,n)$ are the Lah numbers, which describe "How many ways can $k$ objects be distributed to $n$ recipients if order matters and each recipient receives at least 1". They are given by:

\begin{equation}
L(k,n)= {k \choose n} (k-1)^{\underline{k-n}} = \frac{k!}{n!}{ k-1 \choose n-1 }
\end{equation}

The sum considers the case where all $k$ objects are given to $i\leq n$ recipients, with the remaining recipients receiving none. Note that the expression is more complicated than simply dividing the case for indistinct recipients by $n!$, because more than one of the recipients might receive the same number of objects, in which case i.e. the distribution $[(0),(0),(1,2)]$ would be counted twice. The expression is analogous to the expression for distinct objects distributed to indistinct recipients, but where the objects did not have an internal ordering. In that case, instead of a sum over the Lah numbers, the sum is over the Stirling numbers of the second kind.

The Lah numbers can be derived by imagining that first one of the $k$ distinct objects is distributed to the $n$ indistinct recipients, to make sure that each recipient receives at least one, and then adding the remaining objects to the first without restrictions. After the first $n$ distinct objects have been distributed to the recipients, the recipients are no longer indistinct, because they each have been labeled by the object they have already received. There are ${k \choose n}$ ways of distributing the first $n$ objects and $(k-n)!{k-n+n-1 \choose k-n}$ ways to add the $k-n$ remaining objects.

\subparagraph{Books into Unlabeled Boxes} $k$ books are stacked into up to $n$ different unlabeled boxes. Some of the boxes may be empty, and others may contain more than one book. The sequence in which the books are stacked in each box matters. If there are $n-r$ empty boxes and $r$ boxes that have at least one book in them, then there are ${k \choose r}$ ways of putting the first book in each of the boxes. Then there are $(k-r)!{k-r+r-1 \choose k-n}$ ways to stack the remaining books on top of those first books.

\subparagraph{Broken Permutations $\leq n$ Parts} The permutations of $k$ distinct elements are ordered sequences of length $k$. If the sequence is cut up into \underline{up to} $n$ different parts of non-zero length, then what results are \textit{broken permutations}.

\subparagraph{Books into Boxes} $k$ different books are put into $n$ identical boxes. How many ways are there to pack the boxes if you keep track of the order in which the books in each box are stacked?

% Distinct Objects, Distributed in Ordered Groups of At Least One
\subsection{Distinct Objects, Distributed in Ordered Groups of At Least One}

% Distinct Recipients
\subsubsection{Distinct Recipients}

\begin{equation}
|S| = \frac{k!}{(k-n)!}\frac{(k-1)!}{(n-1)!} = k^{\underline{n}} (k-1)^{\underline{k-n}}
\end{equation}

\subparagraph{Books on Labeled Bookshelves} $k$ books are distributed across $n$ different labeled bookshelves. There is at least one book on each shelf. This is the same as picking $n$ books to go on the shelves first, so that all of the shelves have at least one book on them, and then distributing the remainder without restrictions. There are $k^{\underline{n}}$ ways of picking the first $n$ books for each of the $n$ shelves, and $(k-n+n-1)^{\underline{k-n}}$ ways to add the remaining $k-n$ books.

\subparagraph{Ordered Onto Functions} All functions $f: x \rightarrow y$ with $\{x | x\in A, |A| = k \}$ and $\{y | y\in B, |B| = n\}$ that assign ordered sequences of elements in $x$ to elements of $y$, where every element $y\in B$ has an assignment of at least one element.

%% Indistinct Recipients
\subsubsection{Indistinct Recipients}
$k$ elements are divided into $n$ ordered sequences of minimum length 1.

\begin{equation}
L(k,n)= {k \choose n} (k-1)^{\underline{k-n}} = \frac{k!}{n!}{ k-1 \choose n-1 }
\end{equation}

\subparagraph{Books into Unlabeled Boxes} $k$ books are stacked into up to $n$ different unlabeled boxes. All of the $n$ boxes have at least one book in them. The sequence in which the books are stacked in each box matters. There are ${k \choose n}$ ways of putting the first book in each of the boxes. Then there are $(k-n)!{k-n+n-1 \choose k-n}$ ways to stack the remaining $k-n$ books on top of those first books.

\subparagraph{Broken Permutations $n$ parts} The permutations of $k$ distinct elements are ordered sequences of length $k$. If the sequence is cut up into up to $n$ different parts, then what results are \textit{broken permutations}.

\subparagraph{Books into Boxes} $k$ different books are put into $n$ identical boxes, so that there is at least one book in each box. How many ways are there to pack the boxes if you keep track of the order in which the books in each box are stacked?

% Identical Objects
\subsection{Identical Objects}

%% Distinct Recipients
\subsubsection{Distinct Recipients}

\begin{equation}
|S| = {k+n-1 \choose k}
\end{equation}

This coefficient can be easily derives using the "Stars and Bars" concept. A short way of explaining this is as follows: picture putting all the $k$ identical objects in a line (stars). Picture having dividers (bars) to divide the $k$ objects into $n$ groups. For $n$ groups, it is necessary to use $n-1$ dividers. Empty groups result when the dividers sit right next to each other with no object in between them, or if a divider is at the end of a sequence. In total, the objects and the dividers make for a sequence in which $k+n-1$ spots are taken up by either an object or a divider. To pick a particular way of dividing the $k$ objects up, you can either pick the $n-1$ locations of the sequence in which the dividers are located, or, equivalently, the locations in which the objects are located. There are ${k+n-1\choose n-1}={k+n-1\choose k}$ to do so.

\subparagraph{Ping Pong Balls into Labeled Buckets} $k$ identical ping pong balls are put into $n$ labeled buckets. Some of the buckets may be empty, and other buckets might have more than one ball in them.

\subparagraph{Multisets} Multisets are sets in which identical elements might show up several times. For example $\{a,a,b,b,b\}$. They can also be described in terms of the multiplicity of their elements. For example $[a:2, b:3, c:0]$. How many multisets can be formed with $k$ different elements of $n$ different classes?

\subparagraph{Integer Sums} How many different configurations of the $n$ integers $\{ x_i\}_n$ satisfy $x_1 + x_2 + ... + x_n = k$?

\subparagraph{Bosons in Degenerate States} In how many ways might $k$ Bosons populate $n$ degenerate states?

%% Indistinct Recipients
\subsubsection{Indistinct Recipients}

\begin{equation}
|S| = \sum_{i=1}^n P(k,i)
\end{equation}

It turns out that there is no known formula for $P(k,n)$.

\subparagraph{Ping Pong Balls into Unlabeled Bags} $k$ identical ping pong balls are put into $n$ unlabeled bags. Some of the bags might be empty, and other bags might have more than one ball in them.

\subparagraph{Number Partitions} How many ways are there to divide $k$ objects across \underline{up to} $n$ piles. How many ways are there to divide an integer $k$ into a sum of $n$ integers (including zeros). For example: for $k=5$, $n=3$, the partitions are $5+0+0, 4+1+0, 3+2+0, 3+1+1, 2+2+1$.

\subparagraph{Unlabeled Multiplicities of Multisets} For multisets of $k$ elements with $n$ different classes, what is the number of possible multiplicities? For example, a multiset of $k=3$ elements from $n=2$ classes could have multiplicities $[a:3,b:0], [a:2,b:1],[a:1,b:2],[a:0,b:3]$. If we do not care about the labels $a,b$, then the ways that the the $k$ elements migth be distributed are $[3,0]$ and $[2,1]$.

\subparagraph{Boxes of Marbles} $k$ marbles are randomly put into $n$ boxes. How many ways are there for the weight to be distributed among the boxes?

% Identical Objects, Each gets at most one
\subsection{Identical Objects, Each Receives At Most One}

%% Distinct Recipients
\subsubsection{Distinct Recipients}
$k$ identical objects are distributed across $n$ recipients so that each recipient recieves at most one. That amounts to choosing $k$ out of the $n$ recipients who will receive an object.

\begin{equation}
|S| = { n \choose k}
\end{equation}

\subparagraph{At Most One Ping Pong Balls into Labeled Buckets} $k$ identical ping pong balls are distributed across $n$ different buckets, so that $k$ buckets have one ball in them and $n-k$ buckets are empty. This is the same as choosing $k$ out of the $n$ buckets, for which there are $n\times(n-1)\times(n-2)\times\hdots\times(n-k+1)$ choices for the $k$ balls, and correcting for the internal orderings of the $k$ balls by dividing by $k!$ because the balls are identical.

\subparagraph{Subsets} $k$ element subsets of a set of size $n$. The subsets are formed by either choosing the $k$ elements that are included or the $n-k$ elements that are excluded.

\subparagraph{Set Binary Labels} The problem can also be thought of as assigning a binary label to $n$ elements, where there are $k$ times $1$ and $n-k$ times 0.

%% Indistinct Recipients
\subsubsection{Indistinct Recipients}

\begin{equation}
|S| = 1\ \ \mathrm{if\ }k \leq n,\ 0\ \mathrm{otherwise}
\end{equation}

\subparagraph{At Most One Ping Pong Ball into Unlabeled Bags}  $k$ identical ping pong balls are put into $n$ identical bags. The result is either $k$ bags with ping pong balls and $n-k$ empty bags, or there is no possible result if there are not enough bags (i.e. if $k>n$).

\subparagraph{Boxes} How many ways are there to put $k$ marbles in $n$ boxes, if each box is only big enough for one marble. One, if there are enough boxes, or zero, if there aren't enough boxes.

% Identical Everyone Gets At Least one
\subsection{Identical Objects, Each Receives At Least One}

%% Distinct Recipients
\subsubsection{Distinct Recipients}
This problem is the same as giving each of $n$ recipients one of $k$ objects, and then distributing the remaining $k-n$ objects arbitrarily.

\begin{equation}
|S| = {k+n-1-k \choose k-n}= {n-1 \choose k-1}
\end{equation}

This can be derived by picturing first distributing one object to each of the $n$ recipients, ensuring that each recipient has at least one, and then distributing the remaining $k-n$ objects arbitrarily. There is only one way to give each recipient one of the identical objects, and then there are ${k-n+n-1\choose k-n}$ ways to distribute the remaining $k-n$ objects arbitrarily across the recipients.

\subparagraph{At Least One Ping Pong Ball into Labeled Buckets} $k$ ping pong balls are distributed into $n$ labeled buckets so that there is at least one ping pong ball in each bucket. The labeled buckets may have more than one ping pong ball in them.

\subparagraph{Compositions $n$ Parts} How many ways are there to assign $k$ identical objects to $n$ labeled sets of at least one object?

% Indistinct Recipients
\subsubsection{Indistinct Recipients}

\begin{equation}
|S| = P(k,n)
\end{equation}

It turns out that there is no known formula for $P(k,n)$.

\subparagraph{At Least One Ping Pong Ball in Unlabeled Bags} $k$ identical ping pong balls are distributed across $n$ unlabeled bags, so that none of the bags are empty.

\subparagraph{Partitions in $n$ Parts} How many ways are there to make $n$ piles from $k$ objects.

% Identical Each Gets Exactly one
\subsection{Identical Objects, Each Receives Exactly One}

%% Distinct Recipients
\subsubsection{Distinct Recipients}

\begin{equation}
|S| = 1\ \ \mathrm{if\ }k = n,\ 0\ \mathrm{otherwise}
\end{equation}

\subparagraph{One Ping Pong Ball into Each Labeled Bucket} $k$ identical ping pong balls are distributed across $n$ labeled buckets so that each bucket has one ping pong ball in it. If $k=n$, then there is one way to put one ball in each bucket. If the numbers do not match up, then there is no way to match them one-for-one.

%% Indistinct Recipients
\subsubsection{Indistinct Recipients}

\begin{equation}
|S| = 1\ \ \mathrm{if\ }k = n,\ 0\ \mathrm{otherwise}
\end{equation}

\subparagraph{One Ping Pong Ball into Each Unlabeled Bag} $k$ identical ping pong balls are distributed across $n$ identical bags so that each bags has one ping pong ball in it. If $k=n$, then there is one way to put one ball in each bag. If the numbers do not match up, then there is no way to match them one-for-one.


\documentclass[12pt]{pylatex}
\usepackage{examples}

\begin{document}

\section*{Displaying long expressions}

This example uses a simple (though contrived) example of a Taylor series expansion of $1/(1+x)$ to demonstrate the problems that can arise when displaying very long expressions.

% \vspace{12pt}

\begin{minipage}[t]{0.50\textwidth}
\begin{python}
   from sympy import *
   x = Symbol('x')
   ans    = 1/(1+x)                 # py (ans.511,ans)
   taylor = ans.series(x, 0, 10)    # py (ans.512,taylor)
   taylor = ans.series(x, 0, 20)    # py (ans.513,taylor)
   taylor = ans.series(x, 0, 23)    # py (ans.514,taylor)
\end{python}
\end{minipage}
\hskip 0.5cm
\begin{minipage}[t]{0.50\textwidth}
\begin{latex}
   \begin{dgroup*}[spread={5pt}]
      \begin{dmath*} f(x) = \Py*{ans.511} \end{dmath*}
      \begin{dmath*}    {}= \Py*{ans.512} \end{dmath*}
      \begin{dmath*}    {}= \Py*{ans.513} \end{dmath*}
      \begin{dmath*}    {}= \Py*{ans.514} \end{dmath*}
      \begin{dmath*}    {}= \Py*[\hskip 2cm]{ans.514} \end{dmath*}
   \end{dgroup*}
\end{latex}
\end{minipage}

\vspace{18pt}

The first four lines of the following output were set using {\tt\small\verb|\Py*|}
while the final line used {\tt\small\verb|\Py*[\hskip=2cm]|}. The last pair of lines displays
the output for the same tag {\tt\small ans.514} and clearly the formatting of the second
last line is not ideal as the text has overlapped the tag. This was corrected in the final
line by using the optional argument {\tt\small\verb|[\hskip=2cm]|} in the call to {\tt\small\verb|\Py*|}.

\begin{dgroup*}[spread={5pt}]
   \begin{dmath*} f(x) = \Py*{ans.511} \end{dmath*}
   \begin{dmath*}    {}= \Py*{ans.512} \end{dmath*}
   \begin{dmath*}    {}= \Py*{ans.513} \end{dmath*}
   \begin{dmath*}    {}= \Py*{ans.514} \end{dmath*}
   \begin{dmath*}    {}= \Py*[\hskip 2cm]{ans.514} \end{dmath*}
\end{dgroup*}

\end{document}


\lesson{5}{Sep 27 2021 Mon (07:10:04)}{Operations on Complex Numbers}{Unit 1}

\subsubsection*{Multiplying Complex Numbers}

While the process of adding and subtracting complex numbers was similar to that
of polynomials and radicals, multiplying complex numbers is just a little bit
different. Think about in which order this multiplication should take place.

Let's see what happens when the imaginary number is factored first.

\begin{align}
    \sqrt{-12} \times \sqrt{-5} &= (\sqrt{-1} \times \sqrt{12}) \times (\sqrt{-1} \times \sqrt{5}) \\
                                &= i \sqrt{12} \times i \sqrt{5}
\end{align}

Once the imaginary numbers have been factored, multiply the imaginary numbers
together and multiply the radical factors together.

\begin{align}
    i\sqrt{12} \times i\sqrt{5} = i^2 \sqrt{60}
\end{align}

Now that the two factors have been multiplied, simplify the remaining radicand
by either rewriting it as a product of its prime factors or by finding the
perfect square factor.

\newpage


\documentclass[10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\usepackage[T1]{fontenc}
\usepackage{pagecolor}
\usepackage{amssymb}
\usepackage{lmodern}
\usepackage{mathtools, nccmath}
\usepackage{courier}
\usepackage[dvipsnames]{xcolor}

\definecolor{myyellow}{RGB}{225,225,100}
\definecolor{myred}{RGB}{220,100,100}
\definecolor{mygreen}{RGB}{120,225,120}
\definecolor{myblue}{RGB}{100,200,255}
\definecolor{mypurple}{RGB}{200,50,255}
\color{white}
\title{\Huge{Solution to Calculus \#2}}
\author{@all.about.mathematics}
\date{}
\begin{document}
\Large
\maketitle
\pagecolor{black}
\section{Problem}
Evaluate the integral
\large
$$\int_{0}^{\infty}\frac{(x^2-1)\ln x}{1+x^6}dx$$
\large
\newpage
\section{Solution}
In our last post, we had established this result for $\frac m n \not \in \mathbb{Z}$
\begin{equation}
I(m,n)=\int_{0}^{\infty}\frac{x^{m-1}}{1+x^n}dx=\frac{\pi}{n\sin\left(\frac{m\pi}{m}\right)}=\frac{\pi}{n}\csc\left(\frac{m\pi}{n}\right)
\end{equation}
Differentiating $(1)$ with respect to $m$,
$$\frac{dI(m,n)}{dm}=\frac{d}{dm}\int_{0}^{\infty}\frac{x^{m-1}}{1+x^n}dx= \int_{0}^{\infty}\frac{\partial}{\partial m}\frac{x^{m-1}}{1+x^n}dx= \int_{0}^{\infty}\frac{x^{m-1} \ln x}{1+x^n}dx$$
We also have
$$\frac{dI(m,n)}{dm}=\frac{d}{dm}\left[\frac{\pi}{n}\csc\left(\frac{m\pi}{n}\right)\right]=-\frac{\pi^2}{n^2}\csc\left(\frac{m\pi}{n}\right)\cot\left(\frac{m\pi}{n}\right)$$
Rewriting our integral,
$$\int_{0}^{\infty}\frac{(x^2-1)\ln x}{1+x^6}dx=\int_{0}^{\infty}\frac{x^2\ln x}{1+x^6}dx-\int_{0}^{\infty}\frac{\ln x}{1+x^6}dx$$
$$=\frac{dI(3,6)}{dm}-\frac{dI(1,6)}{dm}$$
$$=-\frac{\pi^2}{6^2}\csc\left(\frac{3\pi}{6}\right)\cot\left(\frac{3\pi}{6}\right)-\left(-\frac{\pi^2}{6^2}\csc\left(\frac{\pi}{6}\right)\cot\left(\frac{\pi}{6}\right)\right)$$
$$=\frac{\pi^2}{6\sqrt{3}}$$

\end{document}


\documentclass{article}

\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{fixltx2e}
\usepackage{graphicx}
\usepackage{float}
\usepackage{multirow}

\bibliographystyle{plain}

\title{Bundle Adjustment Notes}
\author{James Balasalle}
\date{\today}

\DeclareMathOperator*{\argmin}{arg\,min}

\begin{document}

\maketitle

\begin{abstract}
  These purpose of these notes is to capture what I learn while trying
  to understand and implement the computer vision task known as
  \emph{Bundle Adjustment}.  The intent is that I can use these notes
  for reference, but also that they serve a didactic purpose: helping
  others who are new to the field come up to speed on the subject
  quickly.  It is my hope that I can explain, derive, or otherwise
  show all of the mathematical steps required to implement a bundle
  adjustment program.
\end{abstract}

\section{Introduction}
\label{sec:intro}

Bundle adjustment is a processes that uses mathematical optimization
techniques to reduce the error found in cameras, their positions, and
the images they create.  When a camera captures an image, all of the
objects in its field of view are converted from their intrinsic 3D
coordinates to a 2D image at the location of the camera.  The points
themselves, in 3D, are called observations.  The process of bundle
adjustment looks at the 3D points and where they end up in the 2D
image and corrects for any errors introduced by the imaging process.
I am not entirely sure, but I think error can be introduced by several
factors: the camera lens, distortion introduced by the lens,
imperfections in the lens, movement, and the ``non-discreteness'' of
today's camera sensors.  What this means is that objects show up in
slightly the wrong place in the 2D image.  Bundle adjustment is a
method which minimizes these errors.  Bundle adjustment can reduce the
error in the points themselves, as well as the location and pointing
direction of the cameras.

\section{A Simple Example}
\label{sec:simple-example}
In this section, I present a small, simple example.  Here we use a
bundle adjustment process to reconstuct the location and pointing
direction of a single camera.  The location and pointing direction of
each camera are known as \textit{camera parameters}.  These include
the 3D position of the camera, which is represented by 3 coordinates:
$x$, $y$, and $z$, and where the camera is ``looking,'' which is
typically represented by three angles: $\phi$, $\chi$, and $\psi$.  In
this example we will only be adjusting the camera parameters, while
assuming the 3D positions of the points and their corresponding 2D
projects are exact and have no error.
\\
\\
In this example, we have some 3D points, let's say $10$ points.  We
also know the exact location of these points in some world coordinate
system.  Next we place a camera somewhere, again in the world
coordinate system, and point it in a direction that can see all
points.  This means we fix the camera parameters: we therefore know
$x$, $y$, $z$, $\phi$, $\chi$, and $\psi$.  Now that we know where the
points are and we know the details of the camera we can ``take a
picture'' of those points.  To do this we \emph{project} the 3D points
into the 2D plane of the camera.  This process takes each 3D point, in
world coordinates, and converts it to a pixel location in the image.
Projecting a 3D point into a 2D camera is a two step process: 1)
convert the 3D point from the world coordinate system to a 3D point in
the camera's coordinate system and then 2) project the 3D point in the
camera's coordinate system to a 2D plane.  We have already determined
the camera parameters by fixing them to our favorite location and
angles.  We encode the parameters in a 4x4 homogeneous matrix
$\mathbf{T}$, which has the following form:
\\
\begin{equation}
  \label{eq:T-matrix}
  \mathbf{T} = \left[
  \begin{array}{ccc|c}
      \multicolumn{3}{c}{\multirow{3}{*}{\Large $\mathbf{R}$}} & t_x \\
      & & & t_y \\
      & & & t_z \\ \hline
      0 & 0 & 0 & 1
  \end{array}
  \right]
\end{equation}
As you can see the upper-left 3x3 of the $\mathbf{T}$ matrix is simply
the rotation matrix $\mathbf{R}$.  The rightmost column of
$\mathbf{T}$ is the location of the camera with a $1$ in the bottom
right position.  This is the matrix we use to convert the 3D points in
world coordinates to the camera's coordinate system:
\\
\begin{equation}
  \label{eq:pose-composition}
  \mathbf{\Gamma}(\mathbf{x}) = \mathbf{P_{C}} = \mathbf{T^{-1}}\mathbf{P_{W}}
\end{equation}
where $\mathbf{P_{C}}$ denotes the resulting point in the camera's
coorinate system and $\mathbf{P_{W}}$ is the point in the world
cooridnate system.  Notice here that to convert from the world
coordinate system to the camera coordinate system we actually need the
\emph{inverse} of $\mathbf{T}$.  $\mathbf{T}$ is an orthogonal matrix,
so we know the inverse exists.  I have not done this, but since we
need the inverse, we could actually parameterize the camera using the
inverse.  That is, instead of finding the $\mathbf{T}$ matrix, we
could find $\mathbf{T^{-1}}$ during the solution process, and then
simply invert $\mathbf{T}$ at the end to find our solution.
\\
\\
Now that we have transformed $\mathbf{P_{W}}$ to $\mathbf{P_{W}}$ we
project $\mathbf{P_{C}}$ into the plane of the camera.  In the
computer vision literature I have read, the projection function is
referred to as $\boldsymbol{\pi}$.  Actually I think
$\boldsymbol{\pi}$ is used for the \emph{de-homogenization} process:
when we go from homegeneous coordinates to non-homegenous coordinates.
This holds true for $n$ to $n-1$ dimensions.  But in the 3D to 2D case
there is also a geometric reasoning that has to do with similar
triangles.  The projection or dehomogenization function is:
\begin{equation}
  \label{eq:dehomog}
  \boldsymbol{\pi}(\mathbf{v}) =  \frac{1}{v_n} \left(
  \begin{array}{c}
    v_1 \\
    v_2 \\
    \vdots \\
    v_{n-1}
  \end{array}
  \right)
\end{equation}
\\
It's pretty instructive to show this function,
$\boldsymbol{\pi}(\mathbf{v})$, in the 3D to 2D case.  Here,
$\mathbf{P_C}$, is a vector with length 3, representing the $x, y, z$
coordinates of the point. The result of this operation would be a
vector of length two, the elements of which we will denote as $u$ and
$v$.  Therefore the function $\boldsymbol{\pi}$ is actually a system
of two equations: one for $u$ and one for $v$.
\begin{align}
  \label{eq:proj-3d-to-2d}
    \pi_u(x,y,z) = \frac{x}{z} \\ \nonumber
    \pi_v(x,y,z) = \frac{y}{z}
\end{align}
\\
We have now shown how to take a point in world coordinates and convert
it to pixel coordinates of the camera: $ \mathbf{\Gamma}(\mathbf{x}) =
\mathbf{P_{C}} = \boldsymbol{\pi}(\mathbf{T^{-1}}\mathbf{P_{W}})$.
\\
\\
So far, we have discussed a \emph{single} point.  But in our program,
and in the real world bundle adjustment problem, we will have many
points.  Each 3D point is a vector of length 3, and the projected
points, in 2D, are also vectors, of length 2.  When we have multiple
points, we can store all the $u$ and $v$ values in a single vector, as
demonstrated here.  In this example we show the results of projecting
3 points in a single vector.  The superscripts denote which points
have been projected into each $u$ and $v$ value.
\begin{equation}
  \label{eq:single-vec}
  \mathbf{X} = \left(
  \begin{array}{c}
    u^{(1)} \\
    v^{(1)} \\
    u^{(2)} \\
    v^{(2)} \\
    u^{(3)} \\
    v^{(3)}
  \end{array}
  \right)
\end{equation}

\subsection{Introduction to Optimization}
\label{sec:optimization-intro}
Bundle Adjustment is a mathematical optimization process.  The 3D
points, and by extension the 2D pixel coordinates are our
observations.  We want to minimize the distance between the
observations (where we actually saw the points) and where they
``should'' be.  Mathematically this means we want to minimize the norm
of the difference between the observations and the correct locations
of the points.  We will be using a Newton-style method to solve the
optimization problem, which means we need an initial guess, as all
Netwon methods do.  The initial guess is our first estimate of the
solution, and in this problem we are solving for the camera
parameters, or $\mathbf{T}$ matrix.  So our intial guess is simply a
guess for $\mathbf{T}$.  Using this guess, we then transform all the
points and project them into the camera.  This gives us a guess for
all $u$ and $v$ values.  We then subtract this guess from the true $u$
and $v$ values.  It is the norm of this difference that we are trying
to minimize.
\\
\\
We can write this several ways, but we will look at one of the more
common forms:
\begin{equation}
  \label{eq:argmin}
  \mathbf{x^*} = \argmin_x \mathbf{F}(\mathbf{x})
\end{equation}

where
\begin{equation}
  \label{eq:argmin-F}
  \mathbf{F}(\mathbf{x}) = \frac{1}{2} \sum\limits_{i=1}^n f_i(\mathbf{x})^2 = \| \mathbf{f}(\mathbf{x}) \|
\end{equation}

What is $f_i(\mathbf{x})$?  It is the difference between the
observation of point $i$ and guess for point $i$.  So, it is actually
two functions, one for $u$ and one for $v$.  Using the same notation
as above, for 3 points here is the full definition of
$\mathbf{F}(\mathbf{x})$:
\begin{eqnarray}
  \label{eq:full-F}
  \mathbf{F}(\mathbf{x}) &=& \frac{1}{2} [ (u_O^{(1)} - u_P^{(1)})^2 + (v_O^{(1)} - v_P^{(1)})^2 + (u_O^{(2)} - u_P^{(2)})^2 + \\
    && (v_O^{(2)} - v_P^{(2)})^2 + (u_O^{(3)} - u_P^{(3)})^2 + (v_O^{(3)} - v_P^{(3)})^2 ] \nonumber
\end{eqnarray}
where $u_O^{(1)}$ is the $u$ pixel location of observation number 1
and $v_P^{(3)}$ is the $v$ pixel location of the third point when
projected with the current guess for $\mathbf{T}$.
\\
\\
How do we solve equation~\ref{eq:argmin}?  This is a rather
complicated process, that is beyond the scope of these notes.  I will
give a brief explanation, however.  We will only consider finding a
minimum, it might not be the global minimum; finding the global
minimum is out of the scope of this discussion.  A minimum of
$\mathbf{F(\mathbf{x})}$ exists where the derivative of
$\mathbf{F(\mathbf{x})} = 0$.  To find where an equation equals zero,
we use Newton's method.  Here is a review of Newton's method for a
single variable:

\begin{equation}
  \label{eq:newton-1d}
  x_{k+1} = x_k - \frac{f(x)}{f^{-1}(x)}
\end{equation}

Of course, bundle adjustment is a more complicated problem.  Since we
have many more observations than unknowns, we have an over-determined
system, there is no exact solution.  Instead we are trying to find the
solution that minimized the error, as stated in the formulation of the
minimization problem.  The way we will do this is to solve
the following problem:
\begin{equation}
  \label{eq:JtJ}
  \mathbf{J}^T\mathbf{J}\cdot\mathbf{h} = \mathbf{J}^T\mathbf{R}
\end{equation}
where $\mathbf{R}$ is the difference between observations and the
current guess, as discussed above.  Equation~\ref{eq:JtJ} is known as
the \emph{normal equation}.  After we have a solution, $\mathbf{h}$,
we use this as our Netwon update.  And then we solve the problem
again with the updated guess, and we do this until the norm of our
error, $\mathbf{R}$, is no longer decreasing.
\\
\\
How do we take the derivative and compute $\mathbf{J}$?  We compute a
Jacobian matrix where each entry is the partial derivative of one of
the equations with respect to one of the unknowns.  In our case, the
Jacobian for each point is a 2x6 matrix.  The function we are taking
the derivative of is
\begin{equation}
  \label{eq:deriv}
  \mathbf{R} = \mathbf{z} - \boldsymbol{\pi}(\mathbf{T^{-1}}\mathbf{P_{W}})
\end{equation}
where $\mathbf{z}$ is one observation (which is a 2-vector of $u$ and
$v$).  This is how we take the derivative of this: $\mathbf{z}$ is a
constant, so the derivative is zero; for the remaining part we need to
use the chain rule:

\begin{equation}
  \label{eq:chain-rule}
  \frac{\partial\boldsymbol{\pi}(\mathbf{x})}{\partial\mathbf{x}} \frac{\partial\mathbf{\Gamma}(\mathbf{y})}{\partial\mathbf{y}}
\end{equation}
where $\mathbf{\Gamma}(\mathbf{y})$ is a function that represents
transforming a point by a matrix as shown in
equationπ~\ref{eq:pose-composition}.  The chain rule simply says we
take the derivative of the outer function, $\boldsymbol{\pi}$, and
multiply by the derivative of the inner function, $\mathbf{\Gamma}$.

Equation~\ref{eq:proj-3d-to-2d}, $\boldsymbol{\pi}(\mathbf{x})$, is
instructive for computing the Jacobian.  As stated above, there at two
equations (one for $u$ and one for $v$), and three unknowns: $x$, $y$,
and $z$.  Taking the derivatives analytically gives us the following
Jacobian:
\begin{equation}
  \label{eq:proj-jacobian}
  \mathbf{J} =
  \begin{bmatrix}
    \frac{1}{z} & 0 & \frac{-x}{z^2} \\
    0 & \frac{1}{z} & \frac{-y}{z^2}
  \end{bmatrix}
\end{equation}
Next we need to take the derivative of $\mathbf{\Gamma}(\mathbf{y})$.  This
is much more complicated, and I will not explain it fully.
Essentially, we use Lie groups, and multiply each unknown by a
specific 4x4 matrix.  Then once we have solved the linear system, we
convert the solution from ``Lie algebra space'' back to our Euclidean
space.  The end result is that the derivative of $\mathbf{T}$ is a 4x4
matrix.
\\
\\
I have also left out something called the \emph{intrinsic matrix} of
the camera.  This is a 3x4 matrix which represents internal
calibration parameters of the camera, such as focal length and optical
distortion.  For this problem, I have simply used a 3x4 identity
matrix.  I mention it here because it is important for the matrix
multiplication.  So far our chain rule step looks like this (in terms
of matrix sizes):
\begin{equation}
  \label{eq:T-matrix}
  \mathbf{J} =
  \left[
  \begin{array}{c}
      2 \textrm{x} 3
  \end{array}
  \cdot
  \begin{array}{c}
      3 \textrm{x} 4
  \end{array}
  \cdot
  \begin{array}{c}
      4 \textrm{x} 4
  \end{array}
  \cdot
  \begin{array}{c}
      4 \textrm{x} 1
  \end{array}
  \right]
\end{equation}
which clearly shows that the matrix multiplication is defined because
all the sizes are legal, and the end result is a 2x1 matrix, which is
actually what we expect: a value for $u$ and a value for $v$.  The
point we are transforming is 4x1 because it is represented in
homogeneous coordinates. As just stated, this multiplication results
in two values, and from this we can see that for every point, we will
have two rows.  Now we need to compute all the columns.  In our
problem we have 6 unknowns, we have to do this 6 times.  We do this by
changing the 4x4 matrix in the Lie algebra step.  These 4x4 matrices
are well-known and can be easily found in the literature; see Hauke's
thesis \cite{strasdat2012local} for a much better and more detailed
explanation.  So our Jacobian for a single point results in a 2x6
matrix.
\\
\\
Obviously for more points, we will have a ``taller'' matrix.  This is
to be expected, because bundle adjustment is a least squares problem:
we are minimizing the error, not finding an exact solution.  We do
this for each point, adding two rows.  When we have done this for all
our observations, we finally computed $\mathbf{J}$ and now we can
solve the linear system defined in~\ref{eq:JtJ}.
\\
\\
As stated above, our toy problem has 10 points, which results in a
very tractable size.  We are able to easily compute
$\mathbf{J}^T\cdot\mathbf{J}$ and $\mathbf{J}^T\cdot\mathbf{R}$,
followed by solving the resulting system exactly using LU
factorization.  Once we have done this, we have a solution,
$\mathbf{h}$.  Since we used the Lie algebra method, we need to
convert this solution back to euclidean space.  This is done via a C++
package called \emph{Sophus} and is a very easy and simple operation.
We now have our Newton update to $\mathbf{T}$.  Instead of adding this
result to our previous guess, we right-multiply by the update.  Now we
have our new guess in euclidean space.  From here, we repeat the
process: project all the points with our new guess, compute the error
vector, $\mathbf{R}$, form the Jacobian and sovle the system for our
next update.  We stop the process when the norm of $\mathbf{R}$ falls
below some threshold.
\\
\\
Here is the process of our toy problem:
\begin{enumerate}
\item Create 10 3D points, store them in a vector $\mathbf{P}$.
\item Construct a reasonable camera and corresponding $\mathbf{T}$
  matrix
\item Project each point into the camera, store the results in a vector
  $\mathbf{O}$.
\item \emph{Forget} $\mathbf{T}$
\item Create a ``reasonable'' guess for $\mathbf{T}$, call this $\mathbf{T_{Guess}}$
\item Project each point using $\mathbf{T_{Guess}}$
\item Compute the $\mathbf{J}$ matrix
\item Compute the $\mathbf{R}$ vector
\item Sovle the follwing system: $\mathbf{J}^T\mathbf{J}\cdot\mathbf{h} =
  \mathbf{J}^T\mathbf{R}$
\item Convert back to eucliding coordinates
\item Update the guess: $\mathbf{T_{Guess + 1}} = \mathbf{T_{Guess}}
  \cdot \mathbf{X}$
\item Compute $\|\mathbf{T_{Guess + 1}}\|$ and stop once it has
  reached some threshold
\end{enumerate}

\bibliography{ba_notes}

\end{document}


\chapter{Mathematical layout}

\section{Environments for display mathematics}
\label{display environments}

\subsection{Don’t use \inlinecodetitle{\$\$  \$\$} or \envtitle{eqnarray}}

There are many good ways to put mathematical content into display mode, but \inlinecode{\$\$~\$\$}\massindex[display math environment]{\$\$~\$\$}[\inlinecode] and \envname{eqnarray}\massindex[display math environment]{eqnarray}[\envname] are none of them.
The method~\inlinecode{\$\$  \$\$} is too low level, and the environment~\envname{eqnarray} has too many problem and has long been deprecated.
One can instead use any of the following, depending on the planned usage.

\subsection{The environments~\inlinecodetitle{{\tbs}[ {\tbs}]} and \envtitle{equation*}}

The environments~\inlinecode{{\tbs}[ {\tbs}]}\massindex[display math environment]{{\tbs}[ {\tbs}]}[\inlinecode] and \envname{equation*}\massindex[display math environment]{equation*}[\envname] can be used for a single line of display math mode.
Both commands do the exactly same thing (when the package~\packname{amsmath}\massindex[packages]{amsmath}[\packname] is loaded).
\begin{showlatex}*{Using the environments~\inlinecode{{\tbs}[ {\tbs}]} and \envname{equation*}}
Suppose that both the formula
\[
  a + b = c
\]
and the formula
\begin{equation*}
  2a - b = c \,.
\end{equation*}
hold.
Then $a$ and $b$ are unique.
\end{showlatex}
The non-starred version~\envname{equation}\massindex[display math environment, tagging and numbering]{equation}[\envname] numbers the equation.
\begin{showlatex}*{Using the environment~\envname{equation}}
The formula
\begin{equation}
  a^2 - b^2 = (a + b)(a - b)
\end{equation}
is one of the binomial formulas.
\end{showlatex}

\subsection{The environment~\envtitle{gather*}}

The environment~\envname{gather*}\massindex[display math environment, multi-line mathematics]{gather*}[\envname] is meant for multiple lines that are non-aligned.
\begin{showlatex}{Using the environment~\envname{gather*}}
We consider for every integer $n \geq 0$ the polynomial
\[
  p_n
  =
  \sum_{k=0}^n x^k \,.
\]
In particular
\begin{gather*}
  p_0 = 1 \,,
  \qquad
  p_1 = 1 + x \,,
  \qquad
  p_2 = 1 + x + x^2 \,,
  \qquad
  p_3 = 1 + x + x^2 + x^3 \,,
  \\
  p_4 = 1 + x + x^2 + x^3 + x^4 \,,
  \qquad
  p_5 = 1 + x + x^2 + x^3 + x^4 + x^5 \,.
\end{gather*}
\end{showlatex}
The non-starred version~\envname{gather}\massindex[display math environment, multi-line mathematics, tagging and numbering]{gather}[\envname] numbers each line.
\begin{showlatex}{Using the environment~\envname{gather}}
We have the polynomials
\begin{gather}
  p_0 = 1 \,,
  \qquad
  p_1 = 1 + x \,,
  \qquad
  p_2 = 1 + x + x^2 \,,
  \qquad
  p_3 = 1 + x + x^2 + x^3 \,,
  \\
  p_4 = 1 + x + x^2 + x^3 + x^4 \,,
  \qquad
  p_5 = 1 + x + x^2 + x^3 + x^4 + x^5 \,.
\end{gather}
\end{showlatex}

\subsection{The environments~\envtitle{align*} and \envtitle{alignat*}}

The environment~\envname{align*}\massindex[display math environment, multi-line mathematics]{align*}[\envname] allows for multiple lines.
Each line contains the symbol~\inlinecode{\&}\massindex{\&}[\inlinecode] once, and the occurrences of this symbol are then aligned.
\begin{showlatex}*{Using the environment~\envname{align*}}
We find that
\begin{align*}
  a + b + c
  &=
  d + e + f + g
  \\
  &=
  h + i + j
  \\
  &=
  k + l + m + n \,.
\end{align*}
\end{showlatex}
The unstarred version~\envname{align}\massindex[display math environment, multi-line mathematics, tagging and numbering]{align}[\envname] numbers the lines.
\begin{showlatex}*{Using the environment \envname{align}}
We find again that
\begin{align}
  a + b + c
  &=
  d + e + f + g
  \\
  &=
  h + i + j
  \\
  &=
  k + l + m + n \,.
\end{align}
\end{showlatex}
One can also use multiple aligned columns, which then need to be separated by an additional~\inlinecode{\&}\massindex{\&}[\inlinecode].
For~$n$ aligned columns we hence need~$2n-1$ occurrences of~\inlinecode{\&} per line.
\begin{showlatex}{Using \envname{align*} with multiple columns}
We consider the values
\begin{align*}
  x_1 &= 1 \,,  &   x_2 &= 2 \,,  &   x_3 &= 3 \,,  \\
  x_4 &= 4 \,,  &   x_5 &= 5 \,,  &   x_6 &= 6 \,,  \\
  x_7 &= 7 \,,  &   x_8 &= 8 \,,  &   x_9 &= 9 \,.
\end{align*}
\end{showlatex}

The environment~\envname{alignat*}\massindex[display math environment, multi-line mathematics]{alignat*}[\envname] is similar to the environment~\envname{align*} but doesn’t add any built-in spacing between the aligned columns.
Any such spacing must therefore by added by hand.
One also has to specify the number of columns beforehand.
\begin{showlatex}{Using the environment~\envname{alignat*}}
We also consider the values
\begin{alignat*}{3}
  y_1 &= 9 \,,  &\qquad   y_2 &= 8 \,,  &\qquad   y_3 &= 7 \,,  \\
  y_4 &= 6 \,,  &         y_5 &= 5 \,,  &         y_6 &= 4 \,,  \\
  y_7 &= 3 \,,  &         y_8 &= 2 \,,  &         y_9 &= 1 \,.
\end{alignat*}
\end{showlatex}
To align multiple columns one should use \envname{alignat*} instead of \envname{align*} to get (manually) a good looking distance between the aligned columns.
The environment~\envname{alignat}\massindex[display math environment, multi-line mathematics]{alignat}[\envname] works in the same way as \envname{alignat*} but automatically numbers the lines.

\subsection{Don’t use \envtitle{center} plus \inlinecodetitle{\$ \$}}
\massindex{center}[\envname]
\massindex{\$ \$}[\inlinecode]

By all that is holy, don’t do the following:
\begin{showlatex}*{Using~\comname{center} with~\inlinecode{\$ \$}}
It holds that
\begin{center}
  $\sum_{i=0}^n 2^i = 2^{n+1} - 1$.
\end{center}
This can be shown by induction.
\end{showlatex}
% TODO: Explain all the things that go wrong

\subsection{The environments~\envtitle{gathered}, \envtitle{aligned} and \envtitle{alignedat}}

The environments~\envname{gathered}\massindex[multi-line mathematics]{gathered}[\envname], \envname{aligned}\massindex[multi-line mathematics]{aligned}[\envname] and \envname{alignedat}\massindex[multi-line mathematics]{alignedat}[\envname] are variations of the environments~\envname{gather*}, \envname{align*} and \envname{alignat*} that can be used inside an already existing math environment.
\begin{showlatex}*{Using \envname{aligned}}
\[
  \left\{
    \begin{aligned}
      a + b &= c      \\
      d     &= e + f
    \end{aligned}
  \right\}
\]
\end{showlatex}

\subsection{Overview}

One should always use the most basic environment that does the job:
Using overpowered environments can lead to unexpected problems.
Consider the following example:
\begin{showlatex}{Improper use of~\envname{align*}}
\begin{align*}
  ABCD = EF = GHI = JKL = M = NOP
  \\
  QRS = TUV = WX = Y = Z
\end{align*}
\end{showlatex}
The environment~\envname{align*} automatically alignes both lines on the right since no information about alignment was given.
In the above situation one should use \envname{gather*} instead.
\begin{showlatex}{Using \envname{gather*} for non-aligned equations}
\begin{gather*}
  ABCD = EF = GHI = JKL = M = NOP
  \\
  QRS = TUV = WX = Y = Z
\end{gather*}
\end{showlatex}
If a multi-line display mode environment is used for a single line then one can get spacing issues, see \cref{spacing before multi-line}.

The flowchart in \cref{environment flow chart} explains how to choose the correct display environment.
(This flowchart is partly inspired by \cite{flowchart}.)
\begin{figure}[tb]
  \begin{center}
  \begin{tikzpicture}[
    node distance = 5em,
    >={Latex[width=2mm,length=2mm]},
    every text node part/.style = { align = center },
    start/.style    = { rectangle,
                        rounded corners,
                        fill = green!20!white,
                        draw = black,
                        minimum width  = 5em,
                        minimum height = 2em
                      },
    question/.style = { rectangle,
                        rounded corners,
                        fill = black!10!white,
                        draw = black,
                        minimum width  = 9em,
                        minimum height = 2.5em
                      },
    answer/.style   = { rectangle,
                        rounded corners,
                        draw = black,
                        minimum width  = 7em,
                        minimum height = 1.5em
                      }
  ]
    % nodes
    \node (start)
          [start]
          {formula};
    \node (multi-line)
          [question, below of = start]
          {multi-line?};
    \node (alignment)
          [question, below of = multi-line]
          {alignment?};
    \node (multicolumn)
          [question, below of = alignment]
          {multiple columns?};
    \node (equation)
          [answer, right of = multi-line, xshift = 7em]
          {\inlinecode{{\tbs}[ {\tbs}]}\\\envname{equation*}};
    \node (gather)
          [answer, right of = alignment, xshift = 7em]
          {\inlinecode{gather*}};
    \node (align)
          [answer, right of = multicolumn, xshift = 7em]
          {\inlinecode{align*}};
    \node (alignat)
          [answer, below of = align]
          {\inlinecode{alignat*}};
    % arrows
    \draw[->] (start) -- (multi-line);
    \draw[->] (multi-line)   -- node[anchor=south] {no} (equation);
    \draw[->] (multi-line)   -- node[anchor=east] {yes} (alignment);
    \draw[->] (alignment)   -- node[anchor=south] {no} (gather);
    \draw[->] (alignment)   -- node[anchor=east] {yes} (multicolumn);
    \draw[->] (multicolumn) -- node[anchor=south] {no} (align);
    \draw[->] (multicolumn) |- node[anchor=east, yshift=1.7em] {yes} (alignat);
  \end{tikzpicture}
  \end{center}
  If numbering of the line(s) is needed then the unstarred version is to be used.
  \caption{Deciding on a math environment.}
  \label{environment flow chart}
\end{figure}

\section{Where to break and align formulas}
\label{break and align ponts}

Oftentimes a formula is broken among multiple lines.
This is done for at least two reasons:
\begin{myitemize}
  \item
    To improve the readability of the given formula.
  \item
    To prevent that the formula goes over the margins of the text area.
\end{myitemize}
If a formula is broken among multiple lines then one has to choose at which places the formula should be broken, and how the resulting parts of the formula will then be aligned.
In this section we present some standard ways of doing so.

\subsection{When to break a formula}
\index{line breaks!in math mode|(}

We first discuss when a formula needs to be broken.

\subsubsection{When the formula is too long}

If a formula is too long to physically fit into the text area, i.e.\ if it goes over the margins of the text area, then it must be broken up.
These occurrences are easy to spot since they give will give an \enquote{overfull hbox} warning.
Consider the following example:
\begingroup
\begin{showlatex}[before lower = {\hfuzz = 40pt}, after lower = {\hfuzz = 0pt}]{An overfull hbox}
  It follows from
  \[
    aaaaaaa
    =
    bbbbbbb
    =
    ccccccc
    \leq
    ddddddd
    =
    eeeeeee
    =
    fffffff
    \leq
    ggggggg
    =
    hhhhhhh
  \]
  that $a \leq h$.
\end{showlatex}
\endgroup

\subsubsection{When the formula is visually too long}

Sometimes a formula does fit into a single line, but barely so.
Consider the following example:
\begin{showlatex}{Visually overfull hbox}
Here is some text.
\[
  aaaaaaaaa = bbbbbbbbbbbbbb = cccccccccccccc = dddddddddddddd = eeeeeeeeee
\]
Here is some more text.
\end{showlatex}
The formula in the above example does -- technically speaking -- not go over the margin.
But it has stretched the display mode beyond its visual limits.
The formula does therefore need to be broken up.

\subsubsection{Expressing structure}

Often the breaking up of a formula is done to better express the structure -- and thus content -- of the displayed formula.
Consider the following example:
\begin{showlatex}[label={unreadable formula}]{A formula that should be broken up for readability}
If $k$ is algebraically closed with~$\operatorname{char}(k) \neq 2$ and $i$ is a square root of $-1$ then
\[
  k[x]/(x^2 + 1)
  =
  k[x]/( (x - i) (x + i) )
  \cong
  k[x]/(x - i) \times k[x]/(x + i)
  \cong
  k \times k
\]
by the Chinese remainder theorem.
\end{showlatex}
The above output still has an appropriate length to be put into a single line, and if space is sparse then this is an acceptable solution.
But this single-line approach to the formula does not help to display its internal structure.
This can be done by splitting up the formula as done in the next example:
\begin{showlatex}{Broken up version of \cref*{unreadable formula}}
If $k$ is algebraically closed and $i$ is a square root of $-1$ then
\begin{align*}
  k[x]/(x^2 + 1)
  &=
  k[x]/( (x - i) (x + i) )
  \\
  &\cong
  k[x]/(x - i) \times k[x]/(x + i)
  \\
  &\cong
  k \times k
\end{align*}
by the Chinese remainder theorem.
\end{showlatex}
This form makes it clear where the equalities and isomorphisms occur.

\subsection{Where to break and align a formula}
\index{aligning formulas|(}

We now discuss at which points a formula can be broken, and how these broken parts can then be aligned.

\subsubsection{Aligning at relation symbols I}

A first approach is to put all relation symbols underneath each other.
\begin{showlatex}{Aligning relation symbols~I}
Let $x$, $y$ be two commuting, nilpotent elements of~$A$.
Then
\begin{align*}
  \exp(x) \exp(y)
  &=
  \left( \sum_{k=0}^\infty \frac{x^k}{k!} \right)
  \left( \sum_{l=0}^\infty \frac{y^l}{l!} \right)
  \\
  &=
  \sum_{k,l=0}^\infty \frac{x^k y^l}{k! \, l!}
  \\
  &=
  \sum_{n=0}^\infty \, \sum_{k+l = n} \frac{x^k y^l}{k! \, l!}
  \\
  &=
  \sum_{n=0}^\infty \frac{1}{n!} \sum_{k=0}^n \binom{n}{k} x^k y^{n-k}
  \\
  &=
  \sum_{n=0}^\infty \frac{1}{n!} (x + y)^n
  \\
  &=
  \exp(x + y) \,.
\end{align*}
\end{showlatex}

\subsubsection{Aligning at relation symbols II}

One can also align all relation symbols to the left, so that the broken up parts of the formula all lie on top of each other.
\begin{showlatex}{Aligning relation symbols~II}
Let~$R$ and~$S$ be two commutative rings.
Then for every~$(r,s) \in R \times S$,
\begin{align*}
  {}&
  (r, s) \in (R \times S)^\times
  \\
  \iff{}&
  \text{there exist $(r', s') \in R \times S$ with $(r,s)(r',s') = (1,1)$}
  \\
  \iff{}&
  \text{there exist $r' \in R$ and $s' \in S$ with $rr' = 1$ and $ss' = 1$}
  \\
  \iff{}&
  \text{$r \in R^\times$ and $s \in S^\times$}
  \\
  \iff{}&
  (r,s) \in R^\times \times S^\times \,,
\end{align*}
and therefore $(R \times S)^{\times} = R^\times \times S^\times$.
\end{showlatex}
The empty pair of curly brackets in \inlinecode{{\tbs}iff\{\}\&} ensures that the spacings coming from \comname{iff} and~\inlinecode{\&} do not interfere with each other.
Otherwise something like this happens:
\begin{showlatex}*{Wrong spacing when alignment points are set wrong}
  \begin{align*}
     &\text{some stuff} \\
    =&\text{some other stuff}
  \end{align*}
\end{showlatex}

\subsubsection{Breaking at a binary operator}

Sometimes it is also useful to break a long term of a formula at a binary operator.
In this case this operator needs to occur in the line after the break.
The following example does it wrong:
\begin{showlatex}*{Wrong aligning at a binary operator~I}
\begin{align*}
  & a + b + c + \\
  & d + e
\end{align*}
\end{showlatex}
The following should be done instead:
\begin{showlatex}*{Right breaking at a binary operator~I}
\begin{align*}
  & a + b + c \\
  & + d + e
\end{align*}
\end{showlatex}
%TODO: Explain the above example better.
If a formula is broken at relation symbols and one of the resulting terms is broken at a binary operator, then the operator is not aligned together with the relation symbols.
Instead the binary operator appears after the relation symbols.
The following example does it wrong:
\begin{showlatex}*{Wrong breaking at a binary operator~II}
\begin{align*}
  a + a
  &=
  b + b + b + b
  \\
  &+
  b + b + b + b
  \\
  &=
  c + c + c + c + c
\end{align*}
\end{showlatex}
Instead the following has to be done:
\begin{showlatex}*{Right breaking at a binary operator~II}
\begin{align*}
  a + a
  ={}&
  b + b + b + b
  \\
  {}&
  +b + b + b + b
  \\
  ={}&
  c + c + c + c + c
\end{align*}
\end{showlatex}

\subsubsection{A single term in another line}

If a single line equation is too long, then it is sometimes appropriate to put the last term in a new line, such that the last term occurs below the second to last term.
\begin{showlatex}{Single term in new line}
It follows that
\begin{align*}
  aaaaaaaaaaaa
  =
  bbbbbbbbbbb
  =
  cccccccccc
  =
  ddddddddd
  &=
  eeeeeeee
  \\
  &=
  ffffff
\end{align*}
and hence $2 + 2 = 5$.
\end{showlatex}

\index{aligning formulas|)}
\index{aligning formulas!zzzza@\igobble |seealso {\comname{align*}}}
\index{aligning formulas!zzzzb@\igobble |seealso {\comname{alignat*}}}

\subsection{Don’t break formulas badly}

One should always keep in mind that breaking up a formula isn’t just meant to prevent technical problems, but more importantly to let the resulting output better display the structure -- and thus part of the content -- of the formula.
A badly broken up formula is harder to understand for both the reader and the author.
Consider the following example:
\begin{showlatex}{Badly broken formula}
It follows that
\begin{align*}
  aaaaaaaaa
  &=
  bbbbbbbbbbb
  =
  cccccccc
  \leq
  dddddd
  =
  eeeeeee
  \\
  &=
  eee
  \leq
  fffffff
  =
  gggggggg
  \leq
  hhhhhhhhh
  \\
  &<
  kkkkkkk
  =
  llll
  \leq
  mmmma
  =
  nnnnnnn
  =
  pp \,.
\end{align*}
\end{showlatex}
In such a case the breaking of the formula should be done in a consistent way.
There seem to be two sensible approaches for the above example, which we will now explain.

\subsubsection{Align everything}

One can align all occurring relation symbols:
\begin{showlatex}{Aligning all relation symbols}
It follows that
\begin{align*}
  aaaaaaaaa
  &= bbbbbbbbbbb \\
  &= cccccccc \\
  &\leq dddddd \\
  &= eeeeeee \\
  &= eee \\
  &\leq fffffff \\
  &= gggggggg \\
  &\leq hhhhhhhhh \\
  &< kkkkkkk \\
  &= llll \\
  &\leq mmmma \\
  &= nnnnnnn \\
  &= pp \,.
\end{align*}
\end{showlatex}
This approach has the advantage of being very consistent.
But it has the disadvantage of taking a lot of space.
It might also not reflect the structure of the formula particularly well, as this layout gives all (in)equalities the same importance.

\subsubsection{Align at inequalities}

On could align all the inequality symbols, to make it clear where these occur:
\begin{showlatex}{Aligning all inequalities}
It follows that
\begin{align*}
  aaaaaaaaa
  &= bbbbbbbbbbb
  = cccccccc
  \\
  &\leq
  dddddd
  = eeeeeee
  = eee
  \\
  &\leq
  fffffff
  = gggggggg
  \\
  &\leq
  hhhhhhhhh
  \\
  &<
  kkkkkkk
  = llll \\
  &\leq mmmma
  = nnnnnnn
  \\
  &= pp \,.
\end{align*}
\end{showlatex}
This layout emphasizes the importance of the inequalities, while relegating the equalities to a less important position.
Note that we have also aligned the first and last equality signs to make it clear where the manipulations begin and end.
If some other equalities are also particularly important (e.g.\ if they follows from some previously hard-earned proposition) then they too should be aligned

\index{line breaks!in math mode|)}

\section{Aligning nearly aligned formulas}
\index{aligning formulas|(}

Sometimes formulas turn out to look nearly aligned in the compiled output, even though this wasn’t planned.
But the formulas may still be non-aligned enough to look jarring.
In such a case it is often best to align these formulas.

Consider the following example:
\begin{showlatex}{Accidental jarringly non-aligned expressions}
\begin{gather*}
  KK^{-1} = 1 = K^{-1}K \,,
  \quad
  EF - FE = \frac{ K - K^{-1} }{ q - q^{-1} } \,,
  \\
  KE = q^2 EK \,,
  \quad
  KF = q^{-2} FK \,.
\end{gather*}
\end{showlatex}
Note that the first line of the output looks slightly shifted to the left when compared to the second line.
This impression vanishes when both lines are properly aligned, and instead gives rise to more coherent look and fell.
\begin{showlatex}{Intentional well-aligned expressions}
\begin{align*}
  KK^{-1} = 1 = K^{-1}K \,,
  \quad
  &EF - FE = \frac{ K - K^{-1} }{ q - q^{-1} } \,,
  \\
  KE = q^2 EK \,,
  \quad
  &KF = q^{-2} FK \,.
\end{align*}
\end{showlatex}

\index{aligning formulas|)}

\section{Proper spacing before multi-line display mode environments}
\label{spacing before multi-line}

If mathematical content is put into display mode then this content will not only be horizontally centered but also receive some vertical spacing\index{spacing!around display mode} around it to separate it from the surrounding text.
Observe in the following example that both above and below the formula there is an additional spacing of roughly (exactly?) one line:
\begin{showlatex}*{Vertical space around display mode}
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et
\[
  a = b \,.
\]
Nunc aliquet bibendum enim facilisis gravida. Nisl nunc mi ipsum faucibus vitae aliquet nec ullamcorper.
\end{showlatex}

If the line before a display mode environment is too short then this vertical spacing may be visually too large.
The one-line display environments~\inlinecode{{\tbs}[ {\tbs}]}\massindex[display math environment]{{\tbs}[ {\tbs}]}[\inlinecode] and~\envname{equation*}\massindex[display math environment]{equation*}[\envname] do therefore automatically adjust this spacing.
Observe that in the following example the distance between the first two text lines is far shorter than the distance between the second and third text lines.
\begin{showlatex}*{Adjusted spacing before one-line display mode}
Lorem
\[
  a = b
\]
 ipsum dolor sit amet, consectetur
\[
  a = b \,.
\]
ipsum elit.
\end{showlatex}
We can say more precisely that the additional spacing inserted by {\LaTeX} is either \comname{abovedisplayskip} or its shorter version~\comname{abovedisplayshortskip}.

The multi-line display mode environments (like~~\envname{align*}\massindex[display math environment, multi-line mathematics]{align*}[\envname]) don’t have this feature (for technical reasons).
Instead, it always uses the spacing~\comname{abovedisplayskip}.
This can lead to some unpleasant spacing:
\begin{showlatex}*{Unpleasant spacing around a multi-line environment}
text text text text text text text text text
\begin{alignat*}{1}
  x &= y_i
\shortintertext{text}
  x &= z_{i+1}
\end{alignat*}
text text text text text text text text
\end{showlatex}

The package~\packname{mathools} provides a partial fix to this problem:
By putting the command~\comname{SwapAboveDisplaySkip}\massindex[\piname{mathtools}, spacing!around display mode]{SwapAboveDisplaySkip}[\comname] at the beginning of a multi-line display mode environment we force {\LaTeX} to use the shorter spacing~\comname{abovedisplayshortskip}.
\begin{showlatex}*{Pleasant spacing around a multi-line environment}
text text text text text text text text text
\begin{alignat*}{1}
\SwapAboveDisplaySkip
  x &= y_i
\shortintertext{text}
  x &= z_{i+1}
\end{alignat*}
text text text text text text text text
\end{showlatex}

\section{Use the \envtitle{cases} environment}

Use the environment~\envname{cases}\massindex[multi-line mathematics]{cases}[\envname] for case distinctions:
\begin{showlatex}{Using \envname{cases}}
It follows that
\[
  A(x)
  =
  \begin{cases}
    x^2  & \text{if $x \leq 0$,} \\
    3x   & \text{if $x = 0$.}
  \end{cases}
\]
\end{showlatex}
In most cases one should actually use the environment~\envname{cases*}\massindex[multi-line mathematics]{cases*}[\envname], which ensures that the second column will be treated as text.
\begin{showlatex}{Using \envname{cases*}}
It follows that
\[
  A(x)
  =
  \begin{cases*}
    x^2  & if $x \leq 0$, \\
    3x   & if $x = 0$.
  \end{cases*}
\]
\end{showlatex}

The package~\packname{mathtools}\massindex[packages]{mathtools}[\packname] defines some more useful variants of the environment~\envname{cases}.
We refer to~\cite[3.4.3]{mathtools} for more information on this.

\section{Proper placement of the qed-symbol}

\index{qed-symbol|(}

The~\envname{proof}~environment\massindex[\piname{amsthm}]{proof}[\envname] automatically places a qed-symbol at its end.
\begin{showlatex}{Using the \envname{proof}~environment}
\begin{proof}
This is obviously a proof.
\end{proof}
\end{showlatex}
It can happen that this automatic placement of the qed-symbol gives a bad looking result.
The general rule is that the qed-symbol should not occur at the end of an otherwise empty line.
To fix such a bad placement one can use the command~\comname{qedhere}\massindex{qedhere}[\comname].
Let’s look at some specific examples of this problem:

\subsubsection{qed-symbol after lists}

If a proof consists of a list then the qed-symbol will by default be placed after the list, and thus in a new line.
\begin{showlatex}{Improper placement of the qed-symbol after a list~I}
\begin{proof}
  This proof consists of a list.
  \begin{enumerate}
    \item
      Some part of the proof.
    \item
      Another part of the proof.
  \end{enumerate}
\end{proof}
\end{showlatex}
This can be fixed by placing the command~\comname{qedhere} just before the list ends.
\begin{showlatex}{Proper placement of the qed-symbol after a list}
\begin{proof}
  This proof consists of a list.
  \begin{enumerate}
    \item
      Some part of the proof.
    \item
      Another part of the proof.
    \qedhere
  \end{enumerate}
\end{proof}
\end{showlatex}
But one has to be careful no to introduce a new line before the command~\comname{qedhere}.
\begin{showlatex}{Improper placement of the qed-symbol after a list~II}
\begin{proof}
  This proof consists of a list.
  \begin{enumerate}
    \item
      Some part of the proof.
    \item
      Another part of the proof.

    \qedhere
  \end{enumerate}
\end{proof}
\end{showlatex}

\subsubsection{qed-symbol after displaystyle}

If a proof ends with display~style mathematics then the qed-symbol will by default be placed after this display~mode.
\begin{showlatex}{Improper placement of the qed-symbol after display~mode~I}
\begin{proof}
  This proof ends with a some display~mode mathematics, namely
  \[
    \sin(x+y)
    =
    \cos(x)\sin(y) + \sin(x)\cos(y) \,.
  \]
\end{proof}
\end{showlatex}
There are at least two different approaches to fixing this situation:

Some authors prever to bring the qed-symbol to the height of the display environment, as done in the following example.
\begin{showlatex}{Somewhat proper placement of the qed-symbol after display~mode}
\begin{proof}
  This proof ends with a some display~mode mathematics, namely
  \[
    \sin(x+y)
    =
    \cos(x)\sin(y) + \sin(x)\cos(y) \,.
    \qedhere
  \]
\end{proof}
\end{showlatex}
But this approach does not work if the (last line) of the display environment has additional height.
Then the mathematical formula goes below the line on which the qed-symbol rests.
\begin{showlatex}{Improper placement of the qed-symbol after display~mode~II}
\begin{proof}
  This proof ends with a some display~mode mathematics, namely
  \[
    1
    + \frac{a}{b - a}
    =
    \frac{(b - a) + a}{b - a}
    =
    \frac{b}{b - a} \,.
    \qedhere
  \]
\end{proof}
\end{showlatex}
The author is the opinion that in such a cases there is no good placement for the qed-symbol.

Aside the placement of the qed-symbol there is another problem to ending a proof with a display~environment:
One of the functions of display~mode is to put an emphasis on the displayed mathematical content.
Putting such an emphasis directly before the end of a proof can lead to the proof missing a sense of closure.

This problem leads to the second solution for the placement of the qed-symbol.
Never end a proof with a display~environment.
This can be done by rewriting the end of proof, or by adding a closing sentence to it.
% TODO: Give an example

\subsubsection{qed-symbol at the end of a long text}

It can also happen that the qed-symbol is pushed to a new line if the previous line is completely filled with text.
(Although {\LaTeX} will actually try quite hard to prevent this from happening.)
If this happens then one should (slightly) rewrite the text to circumvent this problem.

\index{qed-symbol|)}
\index{qed-symbol!zzzz@\igobble |seealso{\comname{qedhere}}}

\section{Tagging and numbering}

A finer control over tags can be achieved via the commands~\comname{tag} and~\comname{notag}.

\subsection{Don’t autonumber all formulas}
\label{dont number all formulas}
\index{tagging and numbering}

Don’t indiscriminately number every occurring formula.
Instead, an equation should be numbered only if it will be referred to later on.
This numbering should then be done automatically by using a suitable environment like~\envname{equation},~\envname{gather},~\envname{align} or~\envname{alignat}.

\subsection{\comtitle{tag}}

With the command~\comname{tag}\massindex[tagging and numbering]{tag}[\comname] a custom tag can be set.
This is useful for marking selected equations by special symbols:
\begin{showlatex}{Using~\comname{tag} for marking a line}
Consider the equation
\begin{equation}
\label{important equation}
  2 + 2 = 5 \,.
  \tag{\ast}
\end{equation}
Note that \cref{important equation} can equivalently be expressed as~$5 = 2 + 2$.
\end{showlatex}
The argument of~\comname{tag} is in text mode, and the resulting tag is automatically enclosed in parentheses.
These parentheses can be removed by using the starred command~\comname{tag*} instead.

The command~\comname{tag} should not be used for regular numbering of equations.
It should be used to tag only certain (often a single) equations in a special way.

It can also be used to express that certain transformations have been used, as the following example demonstrates:
\begin{showlatex}{Using~\comname{tag} to explain steps}
It follows from the Chinese remainder theorem that
\begin{align*}
  \mathbb{R}[x] / ( x^3 + x^2 + x + 1 )
  &=
  \mathbb{R}[x] / ( (x^2 + 1) (x + 1) )
  \\
  &\cong
  \mathbb{R}[x] / ( x^2 + 1 ) \times \mathbb{R}[x] / ( x + 1 )
  \tag{CRT}
  \\
  &\cong
  \mathbb{C} \times \mathbb{R}
\end{align*}
\end{showlatex}

\subsection{\comtitle{notag}}

According to \cref{dont number all formulas} a formula should be numbered only if it needs to be referred to.
But if this formula occurs in a multi-line environment like~\envname{align} then all occuring lines will be numbered, even unwanted ones.
The prevent the numbering of the unrequired lines the command~\comname{notag}\massindex[tagging and numbering]{tag}[\comname] can then be used:
\begin{showlatex}*{Using~\comname{notag} to prevent selected line numbers}
\begin{align}
  a
  &= b \notag \\
  &= c \\
  &= d \notag \\
  &= e
\end{align}
\end{showlatex}

\section{Multi-line set descriptions}

Multi-line set descriptions of the form
\[
  \left\{
    (e_1, \dotsc, e_n)
  \,\middle|\,
    \begin{tabular}{@{}c@{}}
      $e_1, \dotsc, e_n \in R$ \\
      is a complete set of \\
      pairwise orthogonal \\
      idempotents
    \end{tabular}
  \right\}
\]
can be typeset by using a \envname{tabular} environment for the right hand side of the set description:
\begin{showlatex}*{Multi-line set descriptions with tabular}
\[
  \left\{
    x \in X
  \,\middle|\,
    \begin{tabular}{@{}c@{}}
      $x$ satisfies \\
      certain conditions
    \end{tabular}
  \right\}
\]
\end{showlatex}
Note that the entries of the environment~\envname{tabular} are automatically in text mode.
The argument~\inlinecode{\@\{\}} ensure that the environment~\envname{tabular} does not insert additional spacing\index{spacing!in math mode} to its left and right.