\subsection{Stochastic process}

When describing how a quantity evolves over time with a non-deterministic
trajectory we often use what is commonly known as stochastic processes. For
example imagine we are keeping track of a random variable $X$ that changes over
time. This stochastic variable $X$ could describe things such as count of a
specific chemical species, average price of an asset over a period of time,
frequency of a particular allele on a population, etc. To describe how this
variable $X$ changes over time we could in principle list an infinite number of
functions
\begin{equation}
  X(t) = f(X, t).
\end{equation}
This is what we call a stochastic process, i.e. the time evolution of a random
variable $X$. What we are doing here is considered notation abuse by the more
scrupulous mathematicians. Usually people prefer to define a random process as
$Y_X(t) \equiv f(X, t)$ to specify that we are studying how a random variable
$X$ evolves over time $t$ without compromising the use of $X$ as the sole
random variable, but I personally find that notation too strict. Most of our
efforts in these notes will go into describing the probability distribution of
such processes $P(x, t)$, i.e. the probability of our particular random process
taking the specific value $X(t) = x(t)$. More on that later.

Let's clarify all these subtle but important notation differences: The random
variable $X$ is a mapping from a random event to the real line; in other words,
for a particular quantity that we want to describe we assign a number to
represent the event that happened. A useful example is to think about it as
getting to observe a specific face once we roll a die, and assigning a number
from 1 to 6 to represent the face we observed. The stochastic process $X(t)$
describes the values that our random variable $X$ can take over time. Following
up with the die example this could be rolling a die every so often and keeping
track of the numbers that we obtain. This process $X(t)$ represents the
\textbf{ensemble} of possible realizations $x(t)$. In other words, as shown
schematically in \fref{fig_ensemble_realization} $X(t)$ describes all possible
values that the quantity $X$ can take over time, and $x(t)$ describes a
specific trajectory that happened to occur for a specific realization of the
process.

\begin{figure}[h!]
	\centering \includegraphics[width=0.5\textwidth]
  {../../fig/drift_langevin/02_02_01_ensemble_realization.jpeg}
	\caption{\textbf{Schematic description of a stochastic process}. The shaded
	region describes the stochastic process $X(t)$, i.e. all the possible values
	that the random variable $X$ can take over time. The particular realization
	$x(t)$ describes a specific trajectory that the random variable $X$ took in
	one particular case.}
  \label{fig_ensemble_realization}
\end{figure}

From the intuition presented in \fref{fig_ensemble_realization} we understand
that all possible trajectories that our stochastic process can take are
contained in the function $X(t)$. But some particular paths $x(t)$ are more
likely to happen than others. This is what the language of probability theory
allows us to compute! In principle we are interested in writing how likely it
is for our process to take a specific value $x(t)$. This is described by the
probability function $P(x, t)$ which will be at the center of our endeavors for
these notes. Once we define our stochastic process we might want to compute a
moment of the distribution. Recall that for a continuous random variable $X$
with probability density function $P_X(x)$, a moment is defined as
\begin{equation}
  \ee{X^m} \equiv \int dx \; x^m P_X(x),
\end{equation}
where the integral is taken over all the values that $X$ can take. Notice that
our notation for the distribution $P_X(x)$ highlights that we are only
considering values that $X$ can take, no time involved so far. For our case
in which the random variable can change over time what we have to do is settle
for a specific time point $t^*$ and compute the average at that time point
\begin{equation}
  \ee{X(t^*)^m} \equiv \int dx \; x^m P(x, t^*).
  \label{eq_process_moment}
\end{equation}
Notice that this is still not making a statement about the time evolution of
our variable $X$, but rather committing to a specific time point $t^*$ and then
computing the $m\tth$ moment at that time point. An intuitive way to think
about this is to take a frequentist approach for probability. We imagine that
we get to observe many many individual trajectories $\{x_1(t), \ldots
x_n(t)\}$, as schematically shown in \fref{fig_average_langevin} then what
\eref{eq_process_moment} is doing is computing the moment at a specific time
point $t^*$ highlighted also in \fref{fig_average_langevin}.

\begin{figure}[h!]
	\centering \includegraphics[width=0.5\textwidth]
  {../../fig/drift_langevin/02_02_02_average_langevin.jpeg}
	\caption{\textbf{Computing a moment of a distribution $\ee{X(t)}$}. Multiple
  realizations of the stochastic process $\{x_1(t), \ldots x_n(t)\}$ are
  highlighted. In the limit where we have many many of these trajectories we
  can then compute a moment of the distribution $P(x, t^*)$ for a specific time
  point $t^*$. \mrm{need better caption}}
  \label{fig_average_langevin}
\end{figure}

There is no reason why we have to limit ourselves to a single time point $t^*$.
In particular there are interesting quantities such as the autocorrelation
function $\kappa(t_1, t_2)$ that analyze how two different time points $t_1$
and $t_2$ covary with each other. This quantity is defined as
\begin{equation}
  \kappa(t_1, t_2) = \ee{\left(X(t_1) - \ee{X(t_1)}\right)
                         \left(X(t_2) - \ee{X(t_2)}\right)}.
\end{equation}
\mrm{Not sure if I will need to include this if we don't ge to compute moments
that refer to multiple time points.}

Having understood conceptually what stochastic process are and what they allow
us to do we are ready to see their practical implementation. The stochastic
process literature is rich and very formal, but it can be daunting to grasp the
basics of it. For our purposes we will take a more pragmatic approach rather
than diving into the intricacies of the formalism. Our fundamental equation to
model stochastic processes will be the Langevin equation.

\subsection{The Langevin equation}\label{sec_langevin_intro}

Since Robert Brown systematically described the jiggling of particles of
organic and inorganic origin under the microscope, physicists of the caliber of
Einstein and Smoluchowski tried to describe the phenomena. The first success
came from Einstein himself who in 1905 published a microscopic description of
Brownian motion at the time where the atomic structure of matter was still up
in the air. In 1908 only three years later, motivated by this theoretical
predictions, Jean Baptiste Perrin experimentally confirmed Einstein's theory to
unambiguously settle the controversy behind the existence of atoms. That same
year Paul Langevin published a seminal paper in which he proposed an
``infinitely simpler'' formulation of Brownian dynamics according to himself.
In doing so Langevin made use of what we know nowadays as a stochastic
differential equation. Ever since, the study of microscopic particles jiggling
under thermal forces has had enormous consequences in our understanding of the
structure of matter, and has motivated the development of the mathematical
formalism of stochastic calculus with great impact in physics, economics, and
as we are concerned here biological evolution.

The mathematical object that Langevin proposed that now carries his name is
different form the standard ordinary differential equations in the sense that
it includes a stochastic term. More specifically for a 1D system the Langevin
equation that describes the dynamics of $x(t)$ is of the form
\begin{equation}
  \dt{x} = A(x, t) + B(x, t) \xi(t),
  \label{eq_langevin}
\end{equation}
where $A(x, t)$ is the directional deterministic field that could depend on the
variable $x$ itself as well as time, $B(x, t)$ is the scale associated with the
stochasticity of the process and $\xi(t)$ is the random variable associated
with the dynamics of $x(t)$. Without the second term on the right-hand side we
have a regular differential equation for which there is most likely a method to
solve it given that $A(x, t)$ is a well behaved function. The solution of
\eref{eq_langevin} without this second term would be of the form
\begin{equation}
  x(t) = x_o + \int_{t_o}^t dt\; A(x, t),
\end{equation}
where $x_o$ is the initial condition, i.e. $x(t = t_o) = x_o$. This solution
would be a smooth deterministic trajectory for $x(t)$. The extra element that
makes \eref{eq_langevin} special is the stochastic term $B(x, t) \xi(t)$.
$B(x, t)$ is simply a coefficient that determines the amplitude of the noise,
i.e. it sets the scale for how noisy the stochastic process $x(t)$ must be. But
the true innovative feature of \eref{eq_langevin} is $\xi(t)$, sometimes called
white noise. This term is itself a random variable, meaning that it is defined
by a probability distribution $P(\xi, t)$. The reason this term is called white
noise is because the noise is assumed to have mean zero, i.e. fluctuations that
randomly increase the value of $x(t)$ cancel with fluctuations that tend to
decrease $x(t)$ on average. This is mathematically expressed as
\begin{equation}
  \ee{\xi(t)} = 0.
\end{equation}
The second reason this term is called white noise is because the noise at time
$t$ is independent of the noise at any other point in time $t'$. This is
sometimes referred as delta-correlated noise because the way we express this
lack of time correlation between time points is through a $\delta$-function as
\begin{equation}
  \ee{\xi(t) \xi(t')} = 2D \delta(t - t'),
  \label{eq_noise_delta}
\end{equation}
where the prefactor $2D$ is put to resemble the origins of this equation where
$D$ represented the diffusion coefficient of the Brownian particle. What
\eref{eq_noise_delta} is saying is that only when $t = t'$ the noise
autocorrelation function takes the magnitude $2D$ - that's what the
$\delta$-function role is. In other words, independently of what the noise
magnitude was at time $t'$, the noise at time $t$ is sampled from a
distribution with mean $\ee{\xi(t)} = 0$ and variance $\ee{\xi(t)^2} = 2D$.

Since we are characterizing the noise via only its first two moments, i.e. the
noise and the variance, we take the distribution of $\xi(t)$ to be Gaussian.
This assumption is justified because of the central limit theorem that tells us
that the distribution of the sum of many independent random variables tends to
a Gaussian distribution. That means the the probability density function of
$\xi(t)$ is of the form
\begin{equation}
  P(\xi(t)) = {1 \over \sqrt{4 \pi D}}
  \E^{- {\xi(t)^2 \over 4D}}.
\end{equation}
To get a better feeling for how this equation works let's work through a very
simple example.

\subsubsection{Biased random walk}
\mrm{For now I took a ``synthetic'' example not directly connected to what we
will do in population genetics. The nice thing about this example is that can
be analitically solved very easily, so it illustrates how one works with
Langevin equations. But I might consider using a more relevant example in
further iterations if this feels too out of place.}

In order to see what we can do with this type of mathematical object let's
define the a very simple Langevin equation that includes both a directional
term and the stochastic term. For this we will imagine a small particle
performing a random walk in 1D. We will follow the particle's position $x(t)$
as it is subject to a restoration force and a constant noise term. What this
means is that the dynamics of $x(t)$ are given by
\begin{equation}
  \dt{x} = -\gamma x(t) + \xi(t),
  \label{eq_random_walk_langevin}
\end{equation}
where we set $A(x, t) = -\gamma x(t)$ and $B(x, t) = 1$. If $\xi(t)$ was a
regular function \eref{eq_random_walk_langevin} would be a non-homogeneous
linear differential equation. To solve this equation we could use for example
the integrating factor method. For this we would rewrite
\eref{eq_random_walk_langevin} as
\begin{equation}
  \dt{x} + \gamma x(t) = \xi(t).
\end{equation}
The integrating factor $M(t) $is then given by
\begin{equation}
  M(t) = \E^{\int \gamma \; dt}.
\end{equation}
The integral in the exponent of the integrating factor is a kind of integral
known as an integral function. The reason being that it gives the primitive
argument itself (in this case $t$) without an integration constant. In other
words, the integral should be thought as
\begin{equation}
  \int \gamma \; dt = \int_0^t \gamma ds,
\end{equation}
where the fundamental theorem of calculus tells us that this gives
\begin{equation}
  \int_0^t \gamma ds = \gamma t,
\end{equation}
regardless of the integration lower limit. Multiplying both sides of the
equation by $M(t)$ then gives
\begin{equation}
  \E^{\gamma t} \left[ \dt{x} + \gamma x(t) \right] =
  \E^{\gamma t} \xi(t).
\end{equation}
The integration factor allows us to rewrite the left-hand side as
\begin{equation}
  \dt{}\left[ x(t) \E^{\gamma t} \right] =
  \E^{\gamma t} \xi(t).
\end{equation}
We can now integrate both sides of the equation from 0 to $t$ to obtain
\begin{equation}
  \int_0^t ds\; {d \over ds}\left[ x(s) \E^{\gamma s} \right] =
  \int_0^t ds \; \E^{\gamma s} \xi(s).
\end{equation}
For the left-hand side we can use the fundamental theorem of calculus to cancel
the integral with the derivative, obtaining
\begin{equation}
  \left. x(s)\E^{\gamma s} \right\vert_{0}^{t} =
  \int_0^t ds \; \E^{\gamma s} \xi(s).
\end{equation}
Substituting the integration limits and solving for $x(t)$ results in
\begin{equation}
  x(t) = x_o \E^{-\gamma t} +
  \int_0^t ds \; \E^{-\gamma (t - s)} \xi(s).
  \label{eq_random_walk_sol}
\end{equation}
For the second term on the right-hand side we cannot give a close-form
analytical solution given the probabilistic nature of $\xi(t)$. But what we can
do is describe the statistical properties of the trajectories. For example
let's compute the average position of the random walker $\ee{x(t)}$
\begin{equation}
  \ee{x(t)} = \ee{x_o \E^{-\gamma t} +
  \int_0^t ds \; \E^{-\gamma (t - s)} \xi(s)}.
\end{equation}
Using the linearity of expected values this gives
\begin{equation}
  \ee{x(t)} = x_o \E^{-\gamma t} +
  \int_0^t ds \; \E^{-\gamma (t - s)} \ee{\xi(s)}.
\end{equation}
Since we defined the mean noise $\ee{\xi(t)} = 0$ this results in
\begin{equation}
  \ee{x(t)} = x_o \E^{-\gamma t}.
  \label{eq_walker_mean}
\end{equation}
From this result we can see that the memory of the initial position decays
exponentially with a typical time-scale of $\tau = {1 \over \gamma}$.

To compute the second moment $\ee{x(t)^2}$ we need to square
\eref{eq_random_walk_sol}. This results in
\begin{equation}
  x(t)^2 =
  \left( x_o \E^{-\gamma t} +
  \int_0^t ds \; \E^{-\gamma (t - s)} \xi(s) \right)^2.
\end{equation}
Expanding the binomial on the right-hand side gives
\begin{equation}
   x(t)^2 = x_o^2 \E^{-2\gamma t} +
   2 x_o \E^{-\gamma t}
   \left( \int_0^t ds\; \E^{-\gamma (t - s)} \xi(s) \right) +
   \left( \int_0^t ds \; \E^{-\gamma(t - s)} \xi(s) \right)^2.
\end{equation}
We can rewrite the squared integral term as a double integral of the form
\begin{equation}
   \left( \int_0^t ds \; \E^{-\gamma(t - s)} \xi(s) \right)^2 =
   \int_0^t ds \; \int_0^t dz\; \E^{-\gamma (t - s)} \E^{-\gamma (t - z)}
   \xi(s) \xi(z).
\end{equation}
Using this result and taking the expected value results in
\begin{equation}
  \ee{x(t)^2} = \ee{
   x_o^2 \E^{-2\gamma t} +
   2 x_o \E^{-\gamma t}
   \left( \int_0^t ds\; \E^{-\gamma (t - s)} \xi(s) \right) +
   \int_0^t ds \; \int_0^t dz\; \E^{-\gamma (t - s)} \E^{-\gamma (t - z)}
   \xi(s) \xi(z)
  }.
\end{equation}
We can again use the linearity of expected value. The first term on the
right-hand side involves no random variable $\xi(t)$, so it comes out of the
expected value operator. The second term involves a term $\ee{\xi(s)}$ which we
defined to be zero, so this term will cancel. For the last term we have a term
of the form $\ee{\xi(s)\xi(z)}$ - we defined this to be $2D \delta (s - z)$.
Let's substitute these results
\begin{equation}
  \ee{x(t)^2} =
   x_o^2 \E^{-2\gamma t} +
   \int_0^t ds\; \int_0^t dz\; \E^{-\gamma (t-s)} \E^{-\gamma (t-z)}
   (2D \delta(s - t)).
\end{equation}
Now that we have this $\delta$-function in place we can evaluate the integral
over the values of $z$. The $\delta$-function means that only when $z = s$ this
term contributes; in practice this means that the result from the integral over
$z$ is to substitute all values of $z$ for $s$. This looks like
\begin{equation}
  \ee{x(t)^2} =
   x_o^2 \E^{-2\gamma t} +
   2D \int_0^t ds \; \E^{-\gamma (t-s)} \E^{-\gamma (t-s)}.
\end{equation}
Factorizing the terms with exponent $t$ and taking them out of the integral
results in
\begin{equation}
   \ee{x(t)^2} =
   x_o^2 \E^{-2\gamma t} +
   2D \E^{-2 \gamma t} \int_0^t ds\; \E^{2 \gamma s}.
\end{equation}
Evaluating the integral results in
\begin{equation}
   \ee{x(t)^2} =
   x_o^2 \E^{-2\gamma t} +
   {2D \E^{-2\gamma t} \over 2 \gamma}\left( \E^{2 \gamma t} - 1 \right).
\end{equation}
This can be simplified to
\begin{equation}
   \ee{x(t)^2} =
   x_o^2 \E^{-2\gamma t} +
   {D \over \gamma} \left( 1 - \E^{-2 \gamma t} \right).
\end{equation}

Now that we have the first and the second moment we can compute the variance of
our random walk. Recall that the variance is given by
\begin{equation}
  \sigma^2(x(t)) = \ee{x(t)^2} - \ee{x(t)}^2.
  \label{eq_walker_var}
\end{equation}
Using the results we derived results in
\begin{equation}
  \sigma^2(x(t)) = x_o^2 \E^{-2\gamma t} +
   {D \over \gamma} \left( 1 - \E^{-2 \gamma t} \right) -
   x_o^2 \E^{-2\gamma t} =
   {D \over \gamma} \left( 1 - \E^{-2 \gamma t} \right).
\end{equation}
In the limit where $t \rightarrow \infty$ we the find that the variance
converges to $\sigma^2(x) = {D \over \gamma}$. Let's now test these results
with numerical integration of the Langevin equation.

\subsubsection{Numerical integration of Langevin equations}

In order to test the validity of our analytical results we can generate several
realizations of the trajectories, i.e. a list of $\{x_1(t), x_2(t), \ldots \}$
that follow the dynamics defined by \eref{eq_random_walk_langevin} to then
compute from such realizations the mean and variance. The simplest method to
numerically integrate the Langevin equation is to write it as a discrete
difference equation. This means we rewrite \eref{eq_random_walk_langevin} as
\begin{equation}
  {x(t + \Dt) + x(t) \over \Dt} = -\gamma x(t) + \xi(t).
\end{equation}
Then if we solve for $x(t + \Dt)$ we find our recipe to numerically integrate
the equation. This is
\begin{equation}
  x(t + \Dt) = x(t) - \gamma x(t) \Dt + \xi(t) \Dt.
  \label{eq_discrete_walker}
\end{equation}
So for a given small time interval $\Dt$ that is significantly smaller compared
to the relevant time scale set by $\gamma$ and $D$ we can use Euler's method to
integrate the dynamics. The difference is that at each time step we can
generate a pseudo-random number that follows the statistical properties that we
defined for $\xi(t)$.

\fref{fig_langevin_euler} shows the result of this numerical procedure. The
light gray lines show different realizations of the dynamics generated with
\eref{eq_discrete_walker}, the black lines are the resulting mean $\pm$
standard deviation of the trajectories, while the shaded blue are shows the
same mean $\pm$ standard deviation as computed from our analytical results in
\eref{eq_walker_mean} and \eref{eq_walker_var}. The agreement is strikingly
good, and the more realizations of the dynamics we were to include in our
computation, the better the agreement would be.

\begin{figure}[h!]
	\centering \includegraphics[width=0.5\textwidth]
  {../../fig/drift_langevin/02_02_03_langevin_euler.png}
	\caption{\textbf{Numerical integration of the Langevin equation}. Comparison of simulation-based results with analytical results. Random trajectories that evolve according to \eref{eq_random_walk_langevin} were generated. From these the mean and standard deviation of the trajectories were computed (black lines). Analytical results for the mean $\pm$ standard deviation are shown as shaded blue area. For these simulations $x_o= 2$, $\gamma = 1$, $D = 1.5$, and $\Dt = 0.001$}
  \label{fig_langevin_euler}
\end{figure}

Having introduced the concept of random processes and the simple but yet
powerful Langevin equation our goal will now be to work out the mathematical
description of the full time evolution of the probability distribution of
possible realizations of the random process $P(x, t)$. Our goal will be to
derive the famous partial differential equation known as the Fokker-Plank
equation that exactly describes this time evolution.


\subsection{Elementary row operations}

Some operations to a matrix can be reversed to arrive at the original matrix. Trivially, multiplying by the identity matrix is reversible.

Similarly, some operations are not reversible. Such as multiplying by the empty matrix.

All matrix operations which can be reversed are combinations of \(3\) elementary row operations. These are:
Swapping rows

\(T_{12}=\begin{bmatrix}0& 1&...&0\\1 & 0&...&0\\...&...&...&...\\0&0&...&1\end{bmatrix}\)

Multiplying rows by a vector

\(D_2(m)=\begin{bmatrix}1& 0&...&0\\0 & m&...&0\\...&...&...&...\\0&0&...&1\end{bmatrix}\)

Adding rows to other rows

\(L_{12}(m)=\begin{bmatrix}1& 0&...&0\\m & 1&...&0\\...&...&...&...\\0&0&...&1\end{bmatrix}\)


\section{Sets}

\begin{frame}{Set: collection of objects}
  \begin{itemize}
    \setlength\itemsep{4mm}
    \item Denoted by capital letters: $A,B,X$
    \item Objects in a set are called elements.
    \item Elements are denoted by lower case letters: $a,b,x$
    \item Curly braces around elements: $A = \{a_0,a_1,a_2\}$
  \end{itemize}
  \vspace{3mm}
  \begin{exampleblock}{Examples}
      \begin{flalign*}
      A &= \{ 1, 2, 3 \} \\
      B &= \{ p \mid p \ \textrm{is a prime number} \}
      \end{flalign*}
  \end{exampleblock}
\end{frame}

\begin{frame}{No order and no count}
  \begin{alertblock}{A set doesn’t maintain an order of its elements:}
      \begin{flalign*}
        & \{1,2,3\} \  = \  \{1,3,2\} \  = \  \{2,1,3\} \  = \   \{2,3,1\}\\
        & = \  \{3,1,2\} \  = \  \{3,2,1\}
      \end{flalign*}
    \end{alertblock}
    \vspace{3mm}
    \begin{alertblock}{An object is either in the set or not:}
      \begin{flalign*}
        & \{1,2,2,3\} \  = \   \{1,2,3\}
      \end{flalign*}
  \end{alertblock}
\end{frame}

\begin{frame}[fragile]{Question: is $1.0$ an element of $\mathbb{Z}$?}

  To a programmer, the answer is likely no, since $1.0$ is a floating-point number and not an integer.
  Compilers will sometimes give errors if you give $1.0$ where an integer is expected.

  \begin{minted}[frame=lines]{python}
IPython 6.1.0 -- An enhanced Interactive Python.
Type '?' for help.
In [1]: i = 1.0
In [2]: a = [1,4,9,16]
In [3]: a[i]
TypeError: list indices must be integers or slices,
not float
In [4]: a[int(i)]
Out[4]: 4
  \end{minted}
\end{frame}

\begin{frame}[fragile]{Question: is $1.0$ an element of $\mathbb{Z}$?}
  Most mathematicians, on the other hand, will likely say $1.0$ is an integer.
  To them, $1$ and $1.0$ are just different representations of the same point on a number line.
  They think of $\mathbb{Z}$ as a subset of the real numbers $\mathbb{R}$.
  Every number on the usual number line is a real number, including $\pi$, $1.5$ and $10$.

  \begin{center}
    \includegraphics[width=0.8\textwidth]{img/numberline.png}
  \end{center}

  There's not a lot we can do, other than to provide some context when we consider such sets.
  The discussion can get philosophical, especially in discussions about symbols and semantics.
  Again, this is an important concept in the theory of computation.
\end{frame}

\begin{frame}{Sets containing sets}
  \begin{alertblock}{Subsets}
    \setlength\itemsep{0mm}
    \belowdisplayskip=0pt
    $A$ is a subset of $B$ if all the elements of $A$ are in $B$.
    \begin{flalign*}
      A=\{1,2,3,4\} \qquad   B=\{2,3\} \qquad B \subset A
    \end{flalign*}
  \end{alertblock}

  \begin{alertblock}{Powersets}
    Some sets contain other sets as elements.
    The powerset of a set is the set containing all subsets of it:
    \begin{flalign*}
      A &= \{1,2,3\} \\
      \mathcal{P}(A) &= \{\{\},\{1\},\{2\},\{3\},\{1,2\},\{1,3\},\{2,3\},\{1,2,3\}\}
    \end{flalign*}
    Note $A$ contains 3 elements and $\mathcal{P}(A)$ contains $2^3=8$.
  \end{alertblock}
\end{frame}

\begin{frame}[fragile]{Famous sets}
  \begin{description}[123]
    \setlength\itemsep{5mm}
    \item[$\mathbb{N}$] -- the natural numbers $\{ 1, 2, 3, \ldots \}$.
    \item[$\mathbb{N}_0$] -- the natural numbers with zero $\{ 0, 1, 2, 3, \ldots \}$.
    \item[$\mathbb{Z}$] -- the integers $\{ \ldots, -2, -1, 0, 1, 2, \ldots \}$.
    \item[$\mathbb{Q}$] -- the rational numbers $\{ \frac{m}{n} \mid m, n \in \mathbb{Z} \}$.
    \item[$\mathbb{R}$] -- the \href{https://en.wikipedia.org/wiki/Real\_number\#Definition}{real numbers}.
    \item[$\mathbb{C}$] -- the complex numbers $\{ a + bi \mid a, b \in \mathbb{R}, i^2 = -1 \}$.
  \end{description}
\end{frame}

\begin{frame}[fragile]{Sizes of sets}

  \redmath{A = \{a,b,c\} \  \Rightarrow \  \vert A \vert = 3}

  \begin{itemize}
    \setlength\itemsep{4mm}
    \item Number of elements is denoted with vertical lines.
    \item The set of all prime numbers is an \emph{infinite} set.
    \item Infinite sets can still have a notion of size.
    \item $\mathbb{R}$ \href{https://en.wikipedia.org/wiki/Cantor\%27s\_diagonal\_argument}{is bigger than} $\mathbb{N}$ even though they're both infinite -- important consequences for computation.
  \end{itemize}
\end{frame}

\begin{frame}{Operations on sets}
  \redmath{A = \{1,2,3\} \qquad  B = \{2,3,4\}}
  \vspace{4mm}
  \begin{description}[Intersection:]
    \setlength\itemsep{6mm}
    \item[Union:] $A \cup B = \{1,2,3,4\}$, in $A$ \textbf{or} $B$.
    \item[Intersection:] $A \cap B = \{2,3\}$, in $A$ \textbf{and} $B$.
    \item[Difference:] $A \setminus B = \{1\}$, in $A$ \textbf{not} $B$.
  \end{description}
\end{frame}

\section{Tuples}

\begin{frame}{Tuples: finite list of elements taken from sets}
  \redmath{t = (2,1,1) \qquad t \in \mathbb{N} \times \mathbb{N} \times \mathbb{N} \qquad |t| = 3}
  \begin{itemize}
    \setlength\itemsep{2mm}
    \item Round brackets denote tuples, and $t$ is a $3$-tuple or a triple.
    \item Tuples have order, and can repeat elements.
    \item Sometimes we omit the brackets and commas: $t = 211$.
    \item $\mathbb{N} \times \mathbb{N} \times \mathbb{N}$ is sometimes shortened to $\mathbb{N}^3$.
    \item The first $\mathbb{N}$ means the first element comes from $\mathbb{N}$.
    \item The second $\mathbb{N}$ means the second element comes from $\mathbb{N}$, etc.
    \item Note that there is a single empty tuple: $()$.
  \end{itemize}
\end{frame}

\begin{frame}[fragile]{Cartesian products of sets}
  \redmath{A = \{1,2,3\} \qquad B = \{x,y\}}
  \redmath{A \times B = \{(1,x),(2,x),(3,x),(1,y),(2,y),(3,y)\}}

  \begin{itemize}
    \item $A \times B$ is called the cartesian product of $A$ and $B$ -- the set of tuples with first element from $A$ and second from $B$.
    \item $\mathbb{R} \times \mathbb{R} = \mathbb{R}^2$ is the usual 2D plane where we draw plots.
    \item Can extend to any length of tuple: $\mathbb{R}^3$ is the 3D plane.
  \end{itemize}

  \begin{center}
    \resizebox{20mm}{20mm}{%
      \begin{tikzpicture}
        \begin{axis}
          \addplot[color=red]{exp(x)};
        \end{axis}
      \end{tikzpicture}
    }
    \hspace{5mm}
    \resizebox{20mm}{20mm}{%
      \begin{tikzpicture}
        \begin{axis}[grid=both]
          \addplot3[surf,shader=faceted] {x*y};
        \end{axis}
      \end{tikzpicture}
    }
  \end{center}
\end{frame}

\begin{frame}[fragile]{Multisets}
  \redmath{A = \{a,b,x \}}
  \redmath{M = \{(a, 2), (b, 10), (x,5) \}}
  \begin{itemize}
    \setlength\itemsep{3mm}
    \item We can use sets and tuples to define other data structures.
    \item A multiset over a set $A$ is a subset of $A \times \mathbb{N}$ such that every element of $A$ appears exactly once as the first element in a tuple.
  \end{itemize}
\end{frame}


%!TEX root = ../notes.tex
\section{March 21, 2022}
\subsection{Elliptic Curve Elgamal}
As usual, we have some public knowledge, private key, and public key. The idea  is to replace multiplication in $\FF_p^\times$ with addition on $E$.
\begin{mdframed}
    \ul{Public Knowledge}: \\
    $p$ --- prime. \\
    $E$ --- elliptic curve over $\FF_p$. \\
    $P \in E(\FF_p)$ --- point.

    \ul{Private Key}: \\
    $n$ --- private key.

    \ul{Public Key}: \\
    $Q = n\cdot P$ --- public key.

    \ul{Encrpytion}: \\
    Bob has a message $M\in E(\FF_p)$.
    \begin{enumerate}
        \item Choose random $k$.
        \item Compute
              \begin{align*}
                  C_1 & = k\cdot P     \\
                  C_2 & = M + k\cdot Q
              \end{align*}
              Send $(C_1, C_2)$ to Alice.
    \end{enumerate}

    \ul{Decryption}: \\
    Alice will compute
    \[C_2 - n\cdot C_1 = M + k\cdot Q - nk\cdot P = M\]
\end{mdframed}

We now implement this:

\begin{lstlisting}
from ec import ext_gcd, add, minus, multiply

# Private key for Alice
n = randrange(q)

# Public key for Alice
Q = multiply(P, n, a, p)

def e(Q, M):
    """Encryption Function"""
    k = randrange(q)
    c1 = multiply(P, k, a, p)
    c2 = add(M, multiply(Q, k, a, p), a, p)
    return (c1, c2)

def d(n, C):
    """Decryption Function"""
    c1, c2 = C
    return add(c2, minus(multiply(c1, n, a, p), p), a, p)
\end{lstlisting}

The expansion factor is $2$. Even if we think of putting our message into only the $x$ coordinate, the $y$ coordinate is determined by the $x$ coordinate so the factor is still $2$.

\subsection{Elliptic Curve DSA}
Since we have Elgamal, we can also have DSA with Elliptic Curves.
\begin{mdframed}
    \ul{Public Knowledge \& Public Key}: \\
    \emph{Same as Elgamal} \\
    $p$ --- prime. \\
    $E$ --- elliptic curve over $\FF_p$. \\
    $Q = n\cdot P$ --- public key.

    \ul{Private Key}: \\
    $n$ --- private key.

    \ul{Signing}: \\
    Alice has document $d\in \ZZ/q\ZZ$.
    \begin{enumerate}
        \item Choose random $k$.
        \item Compute $(x, y) = k\cdot P$.
              \begin{align*}
                  s_1 & = x                               \\
                  s_2 & = (d + ns_1)\cdot k^{-1} \pmod{q}
              \end{align*}
    \end{enumerate}

    \ul{Verification}: \\
    We can verify the signature as follows:
    \begin{align*}
        v_1 & = d\cdot s_2^{-1} \pmod{q} \\
        v_2 & = s_1s_2^{-1} \pmod{q}
    \end{align*}
    \begin{align*}
        v_1\cdot P + v_2\cdot Q & = ds_2^{-1}\cdot P + s_1s_2^{-1} k\cdot P \\
                                & = (d + s_1 n)s_2^{-1} P                   \\
                                & = k P
    \end{align*}
    $(\text{$x$-coord of }v_1P + v_2Q) = s_1$ check to verify signature.
\end{mdframed}

Again, we can implement this:
\begin{lstlisting}
from ec import *

def sign(n, d):
    """Signing document d with private key n"""
    k = randrange(q)
    x, _ = multiply(P, k, a, p)
    s1 = x
    s2 = ((d + n * s1) * ext_gcd(k, q)[0]) % q
    return (s1, s2)

def verify(Q, d, s):
    """Verifies document d's signature s with public key Q"""
    s1, s2 = s
    v1 = (d * ext_gcd(s2, q)[0]) % q
    v2 = (s1 * ext_gcd(s2, q)[0]) % q
    return add(multiply(P, v1, a, p), multiply(Q, v2, a, p), a, p)[0] == s1
\end{lstlisting}

\begin{remark}
    The specific numbers used in lecture is the elliptic curve used in the Bitcoin blockchain. Being able to forge signatures in this elliptic curve is to topple the Bitcoin market.
\end{remark}


\section*{Exercise 26.2-2}
\subsubsection*{What is the flow and cut}
Let the cut be given as in the exercise, then the flow across the cut (and all other cuts) in figure~26.1(b) is
\begin{align*}
f(S,T) &= f(\{s, v_2, v_4\},\{v_1, v_3,t\}) = \sum_{u \in S}\sum_{v \in T} f(u,v) - \sum_{u \in S}\sum_{v \in T} f(v,u) \\
       &= 11 + 1 + 7 + 4 - 4 = 19
\end{align*}
The capacity is
$$
C(S,T) = \sum_{u \in S}\sum_{v \in T} c(u,v) = 16 + 4 + 7 + 4 =31
$$


%!TEX root = da2020-02.tex

\Chapter{2}{Graph-Theoretic Foundations}

\noindent
The study of distributed algorithms is closely related to graphs: we will interpret a computer network as a graph, and we will study computational problems related to this graph. In this section we will give a summary of the graph-theoretic concepts that we will use.

\section{Terminology}

A \emph{simple undirected graph} is a pair $G = (V,E)$, where $V$ is the set of \emph{nodes} (\emph{vertices}) and $E$ is the set of \emph{edges}. Each edge $e \in E$ is a 2-subset of nodes, that is, $e = \{u,v\}$ where $u \in V$, $v \in V$, and $u \ne v$. Unless otherwise mentioned, we assume that $V$ is a non-empty finite set; it follows that $E$ is a finite set. Usually, we will draw graphs using circles and lines\mydash each circle represents a node, and a line that connects two nodes represents an edge.

\subsection{Adjacency}

If $e = \{u,v\} \in E$, we say that node $u$ is \emph{adjacent} to $v$, nodes $u$ and $v$ are \emph{neighbors}, node $u$ is \emph{incident} to $e$, and edge $e$ is also \emph{incident} to $u$. If $e_1, e_2 \in E$, $e_1 \ne e_2$, and $e_1 \cap e_2 \ne \emptyset$ (i.e., $e_1$ and $e_2$ are distinct edges that share an endpoint), we say that $e_1$ is \emph{adjacent} to $e_2$.
\begin{figure}
    \centering
    \includegraphics[page=\PGraph]{figs.pdf}
    \caption{Node $u$ is adjacent to node $v$. Nodes $u$ and $v$ are incident to edge $e$. Edge $e_1$ is adjacent to edge $e_2$.}\label{fig:graph}
\end{figure}

The \emph{degree} of a node $v \in V$ in graph $G$ is \[
    \deg_G(v) = \bigl|\bigSet{ u \in V : \{u,v\} \in E }\bigr|.
\]
That is, $v$ has $\deg_G(v)$ neighbors; it is adjacent to $\deg_G(v)$ nodes and incident to $\deg_G(v)$ edges. A node $v \in V$ is \emph{isolated} if $\deg_G(v) = 0$. Graph $G$ is \emph{\Reg{k}} if $\deg_G(v) = k$ for each $v \in V$.

\subsection{Subgraphs}

Let $G = (V,E)$ and $H = (V_2,E_2)$ be two graphs. If $V_2 \subseteq V$ and $E_2 \subseteq E$, we say that $H$ is a \emph{subgraph} of $G$. If $V_2 = V$, we say that $H$ is a \emph{spanning subgraph} of $G$.

If $V_2 \subseteq V$ and $E_2 = \Set{ \{u,v\} \in E : u \in V_2,\ v \in V_2 }$, we say that $H = (V_2,E_2)$ is an \emph{induced subgraph}; more specifically, $H$ is the subgraph of $G$ induced by the set of nodes $V_2$.

If $E_2 \subseteq E$ and $V_2 = \bigcup E_2$, we say that $H$ is an \emph{edge-induced subgraph}; more specifically, $H$ is the subgraph of $G$ induced by the set of edges $E_2$.

\subsection{Walks}

\begin{figure}
    \centering
    \includegraphics[page=\PWalk]{figs.pdf}
    \caption{
        (a)~A walk of length~5 from $s$ to $t$.
        (b)~A non-backtracking walk.
        (c)~A path of length~4.
        (d)~A path of length~2; this is a shortest path and hence $\dist_G(s,t) = 2$.
    }\label{fig:walk}
\end{figure}

\begin{figure}
    \centering
    \includegraphics[page=\PCycle]{figs.pdf}
    \caption{
        (a)~A cycle of length~6.
        (b)~A cycle of length~3; this is a shortest cycle and hence the girth of the graph is~$3$.
    }\label{fig:cycle}
\end{figure}

A \emph{walk} of length $\ell$ from node $v_0$ to node $v_\ell$ is an alternating sequence \[w = (v_0, e_1, v_1, e_2, v_2, \dotsc, e_\ell, v_\ell)\] where $v_i \in V$, $e_i \in E$, and $e_i = \{ v_{i-1}, v_i \}$ for all $i$; see Figure~\ref{fig:walk}. The walk is \emph{empty} if $\ell = 0$. We say that walk~$w$ \emph{visits} the nodes $v_0, v_1, \dotsc, v_\ell$, and it \emph{traverses} the edges $e_1, e_2, \dotsc, e_\ell$. In general, a walk may visit the same node more than once and it may traverse the same edge more than once. A \emph{non-backtracking walk} does not traverse the same edge twice consecutively, that is, $e_{i-1} \ne e_i$ for all $i$. A \emph{path} is a walk that visits each node at most once, that is, $v_i \ne v_j$ for all $0 \le i < j \le \ell$. A walk is \emph{closed} if $v_0 = v_\ell$. A \emph{cycle} is a non-empty closed walk with $v_i \ne v_j$ and $e_i \ne e_j$ for all $1 \le i < j \le \ell$; see Figure~\ref{fig:cycle}. Note that the length of a cycle is at least~$3$.

\subsection{Connectivity and Distances}\label{ssec:graphs-conn}

For each graph $G = (V,E)$, we can define a relation $\leadsto$ on $V$ as follows: $u \leadsto v$ if there is a walk from $u$ to $v$. Clearly $\leadsto$ is an equivalence relation. Let $C \subseteq V$ be an equivalence class; the subgraph induced by $C$ is called a \emph{connected component} of $G$.

If $u$ and $v$ are in the same connected component, there is at least one \emph{shortest path} from $u$ to $v$, that is, a path from $u$ to $v$ of the smallest possible length. Let $\ell$ be the length of a shortest path from $u$ to $v$; we define that the \emph{distance} between $u$ and $v$ in $G$ is $\dist_G(u,v) = \ell$. If $u$ and $v$ are not in the same connected component, we define $\dist_G(u,v) = \infty$. Note that $\dist_G(u,u) = 0$ for any node $u$.

\begin{figure}
    \centering
    \includegraphics[page=\PNeighbourhood]{figs.pdf}
    \caption{Neighborhoods.}\label{fig:neighborhood}
\end{figure}
For each node $v$ and for a non-negative integer $r$, we define the \emph{radius-$r$ neighborhood} of $v$ as follows (see Figure~\ref{fig:neighborhood}):
\[
    \ball_G(v, r) = \Set{ u \in V : \dist_G(u,v) \le r }.
\]

A graph is \emph{connected} if it consists of one connected component. The \emph{diameter} of graph $G$, in notation $\diam(G)$, is the length of a longest shortest path, that is, the maximum of $\dist_G(u,v)$ over all $u, v \in V$; we have $\diam(G) = \infty$ if the graph is not connected.

The \emph{girth} of graph $G$ is the length of a shortest cycle in $G$. If the graph does not have any cycles, we define that the girth is $\infty$; in that case we say that $G$ is \emph{acyclic}.

A \emph{tree} is a connected, acyclic graph. If $T = (V,E)$ is a tree and $u,v \in V$, then there exists precisely one path from $u$ to $v$. An acyclic graph is also known as a \emph{forest}\mydash in a forest each connected component is a tree. A \emph{pseudotree} has at most one cycle, and in a \emph{pseudoforest} each connected component is a pseudotree.

A \emph{path graph} is a graph that consists of one path, and a \emph{cycle graph} is a graph that consists of one cycle. Put otherwise, a path graph is a tree in which all nodes have degree at most $2$, and a cycle graph is a \Reg{2} pseudotree. Note that any graph of maximum degree $2$ consists of disjoint paths and cycles, and any \Reg{2} graph consists of disjoint cycles.

\subsection{Isomorphism}

An \emph{isomorphism} from graph $G_1 = (V_1,E_1)$ to graph $G_2 = (V_2,E_2)$ is a bijection $f\colon V_1 \to V_2$ that preserves adjacency: $\{u,v\} \in E_1$ if and only if $\{f(u),f(v)\} \in E_2$. If an isomorphism from $G_1$ to $G_2$ exists, we say that $G_1$ and $G_2$ are isomorphic.

If $G_1$ and $G_2$ are isomorphic, they have the same structure; informally, $G_2$ can be constructed by renaming the nodes of $G_1$ and vice versa.

\section{Packing and Covering}\label{sec:packingcovering}

A subset of nodes $X \subseteq V$ is
\begin{enumerate}
    \item an \emph{independent set} if each edge has at most one endpoint in $X$, that is, $|e \cap X| \le 1$ for all $e \in E$,
    \item a \emph{vertex cover} if each edge has at least one endpoint in $X$, that is, $e \cap X \ne \emptyset$ for all $e \in E$,
    \item a \emph{dominating set} if each node $v \notin X$ has at least one neighbor in $X$, that is, $\ball_G(v,1) \cap X \ne \emptyset$ for all $v \in V$.
\end{enumerate}
A subset of edges $X \subseteq E$ is
\begin{enumerate}[resume]
    \item a \emph{matching} if each node has at most one incident edge in $X$, that is, $\{t,u\} \in X$ and $\{t,v\} \in X$ implies $u = v$,
    \item an \emph{edge cover} if each node has at least one incident edge in $X$, that is, $\bigcup X = V$,
    \item an \emph{edge dominating set} if each edge $e \notin X$ has at least one neighbor in $X$, that is, $e \cap \bigl(\bigcup X\bigr) \ne \emptyset$ for all $e \in E$.
\end{enumerate}
See Figure~\ref{fig:packingcovering} for illustrations.
\begin{figure}
    \centering
    \includegraphics[page=\PPackingCovering]{figs.pdf}
    \caption{Packing and covering problems; see Section~\ref{sec:packingcovering}.}\label{fig:packingcovering}
\end{figure}

Independent sets and matchings are examples of \emph{packing problems}\mydash intuitively, we have to ``pack'' elements into set $X$ while avoiding conflicts. Packing problems are \emph{maximization problems}. Typically, it is trivial to find a feasible solution (for example, an empty set), but it is more challenging to find a large solution.

Vertex covers, edge covers, dominating sets, and edge dominating sets are examples of \emph{covering problems}\mydash intuitively, we have to find a set $X$ that ``covers'' the relevant parts of the graph. Covering problems are \emph{minimization problems}. Typically, it is trivial to find a feasible solution if it exists (for example, the set of all nodes or all edges), but it is more challenging to find a small solution.

The following terms are commonly used in the context of maximization problems; it is important not to confuse them:
\begin{enumerate}
    \item \textbf{\emph{maximal}}: a maximal solution is not a proper subset of another feasible solution,
    \item \textbf{\emph{maximum}}: a maximum solution is a solution of the largest possible cardinality.
\end{enumerate}
Similarly, in the context of minimization problems, analogous terms are used:
\begin{enumerate}
    \item \textbf{\emph{minimal}}: a minimal solution is not a proper superset of another feasible solution,
    \item \textbf{\emph{minimum}}: a minimum solution is a solution of the smallest possible cardinality.
\end{enumerate}
Using this convention, we can define the terms \emph{maximal independent set}, \emph{maximum independent set}, \emph{maximal matching}, \emph{maximum matching}, \emph{minimal vertex cover}, \emph{minimum vertex cover}, etc.

For example, Figure~\ref{fig:packingcovering}a shows a maximal independent set: it is not possible to greedily extend the set by adding another element. However, it is not a maximum independent set: there exists an independent set of size $3$. Figure~\ref{fig:packingcovering}d shows a matching, but it is not a maximal matching, and therefore it is not a maximum matching either.

Typically, maximal and minimal solutions are easy to find\mydash you can apply a greedy algorithm. However, maximum and minimum solutions can be very difficult to find\mydash many of these problems are NP-hard optimization problems.

A \emph{minimum maximal matching} is precisely what the name suggests: it is a maximal matching of the smallest possible cardinality. We can define a \emph{minimum maximal independent set}, etc., in an analogous manner.

\section{Labelings and Partitions}\label{sec:partitions}

We will often encounter functions of the form \[f\colon V \to \{1,2,\dotsc,k\}.\] There are two interpretations that are often helpful:
\begin{enumerate}[label=(\roman*)]
    \item Function $f$ assigns a \emph{label} $f(v)$ to each node $v \in V$. Depending on the context, the labels can be interpreted as colors, time slots, etc.
    \item Function $f$ is a \emph{partition} of $V$. More specifically, $f$ defines a partition $V = V_1 \cup V_2 \cup \dotsb \cup V_k$ where $V_i = f^{-1}(i) = \Set{ v \in V : f(v) = i }$.
\end{enumerate}
Similarly, we can study a function of the form \[f\colon E \to \{1,2,\dotsc,k\}\] and interpret it either as a labeling of edges or as a partition of~$E$.

Many graph problems are related to such functions. We say that a function $f\colon V \to \{1,2,\dotsc,k\}$ is
\begin{enumerate}
    \item a \emph{proper vertex coloring} if $f^{-1}(i)$ is an independent set for each~$i$,
    \item a \emph{weak coloring} if each non-isolated node $u$ has a neighbor $v$ with $f(u) \ne f(v)$,
    \item a \emph{domatic partition} if $f^{-1}(i)$ is a dominating set for each~$i$.
\end{enumerate}
A function $f\colon E \to \{1,2,\dotsc,k\}$ is
\begin{enumerate}[resume]
    \item a \emph{proper edge coloring} if $f^{-1}(i)$ is a matching for each~$i$,
    \item an \emph{edge domatic partition} if $f^{-1}(i)$ is an edge dominating set for each~$i$.
\end{enumerate}
See Figure~\ref{fig:partitions} for illustrations.
\begin{figure}
    \centering
    \includegraphics[page=\PPartitions]{figs.pdf}
    \caption{Partition problems; see Section~\ref{sec:partitions}.}\label{fig:partitions}
\end{figure}

Usually, the term \emph{coloring} refers to a proper vertex coloring, and the term \emph{edge coloring} refers to a proper edge coloring. The value of $k$ is the \emph{size} of the coloring or the \emph{number of colors}. We will use the term \emph{$k$-coloring} to refer to a proper vertex coloring with $k$ colors; the term \emph{$k$-edge coloring} is defined in an analogous manner.

A graph that admits a $2$-coloring is a \emph{bipartite graph}. Equivalently, a bipartite graph is a graph that does not have an odd cycle.

Graph coloring is typically interpreted as a minimization problem. It is easy to find a proper vertex coloring or a proper edge coloring if we can use arbitrarily many colors; however, it is difficult to find an \emph{optimal} coloring that uses the smallest possible number of colors.

On the other hand, domatic partitions are a maximization problem. It is trivial to find a domatic partition of size $1$; however, it is difficult to find an \emph{optimal} domatic partition with the largest possible number of disjoint dominating sets.

\section{Factors and Factorizations}

Let $G = (V,E)$ be a graph, let $X \subseteq E$ be a set of edges, and let $H = (U,X)$ be the subgraph of $G$ induced by $X$. We say that $X$ is a \emph{$d$-factor} of $G$ if $U = V$ and $\deg_H(v) = d$ for each $v \in V$.

Equivalently, $X$ is a $d$-factor if $X$ induces a spanning \Reg{d} subgraph of $G$. Put otherwise, $X$ is a $d$-factor if each node $v \in V$ is incident to exactly $d$ edges of $X$.

A function $f\colon E \to \{1,2,\dotsc,k\}$ is a \emph{\Fact{d}} of $G$ if $f^{-1}(i)$ is a $d$-factor for each $i$. See Figure~\ref{fig:factorization} for examples.
\begin{figure}
    \centering
    \includegraphics[page=\PFactorisation]{figs.pdf}
    \caption{
        (a)~A \Fact{1} of a \Reg{3} graph.
        (b)~A \Fact{2} of a \Reg{4} graph.
    }\label{fig:factorization}
\end{figure}

We make the following observations:
\begin{enumerate}
    \item A $1$-factor is a maximum matching. If a $1$-factor exists, a maximum matching is a $1$-factor.
    \item A \Fact{1} is an edge coloring.
    \item The subgraph induced by a $2$-factor consists of disjoint cycles.
\end{enumerate}
A $1$-factor is also known as a \emph{perfect matching}.

\section{Approximations}

So far we have encountered a number of maximization problems and minimization problems. More formally, the definition of a maximization problem consists of two parts: a set of \emph{feasible solutions} $\calS$ and an \emph{objective function} $g\colon \calS \to \RR$. In a maximization problem, the goal is to find a feasible solution $X \in \calS$ that maximizes $g(X)$. A minimization problem is analogous: the goal is to find a feasible solution $X \in \calS$ that minimizes $g(X)$.

For example, the problem of finding a maximum matching for a graph $G$ is of this form. The set of feasible solutions $\calS$ consists of all matchings in $G$, and we simply define $g(M) = |M|$ for each matching $M \in \calS$.

As another example, the problem of finding an optimal coloring is a minimization problem. The set of feasible solutions $\calS$ consists of all proper vertex colorings, and $g(f)$ is the number of colors in $f \in \calS$.

Often, it is infeasible or impossible to find an optimal solution; hence we resort to approximations. Given a maximization problem $(\calS,g)$, we say that a solution $X$ is an \emph{\Apx{\alpha}} if $X \in \calS$, and we have $\alpha g(X) \ge g(Y)$ for all $Y \in \calS$. That is, $X$ is a feasible solution, and the size of $X$ is within factor $\alpha$ of the optimum.

Similarly, if $(\calS,g)$ is a minimization problem, we say that a solution $X$ is an \Apx{\alpha} if $X \in \calS$, and we have $g(X) \le \alpha g(Y)$ for all $Y \in \calS$. That is, $X$ is a feasible solution, and the size of $X$ is within factor $\alpha$ of the optimum.

Note that we follow the convention that the approximation ratio $\alpha$ is always at least $1$, both in the case of minimization problems and maximization problems. Other conventions are also used in the literature.

\section{Directed Graphs and Orientations}

Unless otherwise mentioned, all graphs that we encounter are undirected. However, we will occasionally need to refer to so-called orientations, and hence we need to introduce some terminology related to directed graphs.

A \emph{directed graph} is a pair $G = (V,E)$, where $V$ is the set of nodes and $E$ is the set of \emph{directed edges}. Each edge $e \in E$ is a pair of nodes, that is, $e = (u,v)$ where $u, v \in V$. Put otherwise, $E \subseteq V \times V$.

Intuitively, an edge $(u,v)$ is an ``arrow'' that points from node $u$ to node $v$; it is an \emph{outgoing edge} for $u$ and an \emph{incoming edge} for $v$. The \emph{outdegree} of a node $v \in V$, in notation $\outdegree_G(v)$, is the number of outgoing edges, and the \emph{indegree} of the node, $\indegree_G(v)$, is the number of incoming edges.

Now let $G = (V,E)$ be a graph and let $H = (V,E')$ be a directed graph with the same set of nodes. We say that $H$ is an \emph{orientation} of $G$ if the following holds:
\begin{enumerate}
    \item For each $\{u,v\} \in E$ we have either $(u,v) \in E'$ or $(v,u) \in E'$, but not both.
    \item For each $(u,v) \in E'$ we have $\{u,v\} \in E$.
\end{enumerate}
Put otherwise, in an orientation of $G$ we have simply chosen an arbitrary direction for each undirected edge of $G$. It follows that \[\indegree_H(v) + \outdegree_H(v) = \deg_G(v)\] for all $v \in V$.

\section{Quiz}

Construct a simple undirected graph $G = (V,E)$ with the following property: If you take any set $X$ that is a maximal independent set of $G$, then $X$ is not a minimum dominating set of $G$.

Present the graph in the set formalism by listing the sets of nodes and edges. For example, a cycle on three nodes can be encoded as $V = \{1,2,3\}$ and $E = \{ \{1,2\}, \{2,3\}, \{3,1\} \}$.

\section{Exercises}

\begin{ex}[independence and vertex covers]
    Let $I \subseteq V$ and define $C = V \setminus I$. Show that
    \begin{subex}
        \item if $I$ is an independent set then $C$ is a vertex cover and vice versa,
        \item if $I$ is a maximal independent set then $C$ is a minimal vertex cover and vice versa,
        \item if $I$ is a maximum independent set then $C$ is a minimum vertex cover and vice versa,
        \item it is possible that $C$ is a \Apx{2} of minimum vertex cover but $I$ is not a \Apx{2} of maximum independent set,
        \item it is possible that $I$ is a \Apx{2} of maximum independent set but $C$ is not a \Apx{2} of minimum vertex cover.
    \end{subex}
\end{ex}

\begin{ex}[matchings]
    Show that
    \begin{subex}
        \item any maximal matching is a \Apx{2} of a maximum matching,
        \item any maximal matching is a \Apx{2} of a minimum maximal matching,
        \item a maximal independent set is not necessarily a \Apx{2} of maximum independent set,
        \item a maximal independent set is not necessarily a \Apx{2} of minimum maximal independent set.
    \end{subex}
\end{ex}

\begin{ex}[matchings and vertex covers]\label{ex:mmvc}
    Let $M$ be a maximal matching, and let $C = \bigcup M$, i.e., $C$ consists of all endpoints of matched edges. Show that
    \begin{subex}
        \item $C$ is a \Apx{2} of a minimum vertex cover,
        \item $C$ is not necessarily a \Apx{1.999} of a minimum vertex cover.
    \end{subex}
    Would you be able to improve the approximation ratio if $M$ was a minimum maximal matching?
\end{ex}

\begin{ex}[independence and domination]
    Show that
    \begin{subex}
        \item a maximal independent set is a minimal dominating set,
        \item a minimal dominating set is not necessarily a maximal independent set,
        \item a minimum maximal independent set is not necessarily a minimum dominating set.
    \end{subex}
\end{ex}

\begin{ex}[graph colorings and partitions]
    Show that
    \begin{subex}
        \item a weak $2$-coloring always exists,
        \item a domatic partition of size $2$ does not necessarily exist,
        \item if a domatic partition of size $2$ exists, then a weak $2$-coloring is a domatic partition of size $2$,
        \item a weak $2$-coloring is not necessarily a domatic partition of size $2$.
    \end{subex}
    Show that there are \Reg{2} graphs with the following properties:
    \begin{subex}[resume]
        \item any $3$-coloring is a domatic partition of size $3$,
        \item no $3$-coloring is a domatic partition of size $3$.
    \end{subex}
    Assume that $G$ is a graph of maximum degree $\Delta$; show that
    \begin{subex}[resume]
        \item there exists a \Dpocol,
        \item a $\Delta$-coloring does not necessarily exist.
    \end{subex}
\end{ex}

\begin{ex}[isomorphism]
    Construct non-empty \Reg{3} connected graphs $G$ and $H$ such that $G$ and $H$ have the same number of nodes and $G$ and $H$ are \emph{not} isomorphic. Just giving a construction is not sufficient\mydash you have to \emph{prove} that $G$ and $H$ are not isomorphic.
\end{ex}

\begin{exs}[matchings and edge domination]\label{ex:mmeds}
    Show that
    \begin{subex}
        \item a maximal matching is a minimal edge dominating set,
        \item a minimal edge dominating set is not necessarily a maximal matching,
        \item a minimum maximal matching is a minimum edge dominating set,
        \item any maximal matching is a \Apx{2} of a minimum edge dominating set.
    \end{subex}
    \hint{Assume that $D$ is an edge dominating set; show that you can construct a maximal matching $M$ with $|M| \le |D|$.}
\end{exs}

\begin{exs}[Petersen 1891]\label{ex:2fact}
    Show that any \Reg{2d} graph $G = (V,E)$ has an orientation $H = (V,E')$ such that \[\indegree_H(v) = \outdegree_H(v) = d\] for all $v \in V$. Show that any \Reg{2d} graph has a \Fact{2}.
\end{exs}

\section{Bibliographic Notes}

The connection between maximal matchings and approximations of vertex covers (Exercise~\ref{ex:mmvc}) is commonly attributed to Gavril and Yannakakis\mydash see, e.g., Papadimitriou and Steiglitz \cite{papadimitriou98combinatorial}. The connection between minimum maximal matchings and minimum edge dominating sets (Exercise~\ref{ex:mmeds}) is due to Allan and Laskar~\cite{allan78domination} and Yannakakis and Gavril~\cite{yannakakis80edge}. Exercise~\ref{ex:2fact} is a 120-year-old result due to Petersen~\cite{petersen1891dietheorie}. The definition of a weak coloring is from Naor and Stockmeyer~\cite{naor95what}.

Diestel's book~\cite{diestel05graph} is a good source for graph-theoretic background, and Vazirani's book~\cite{vazirani01approximation} provides further information on approximation algorithms.


\vspace{-7mm}
This chapter gives routines for computing rotation matrices that operate on the expansion coefficients of spherical harmonics and spherical wave functions in order to rotate fields or view fields from a rotated frame.  The rotation matrices work the same for any of the wave functions in Chapter \ref{chap:wavefunctions}.

\section{Euler Rotation}
3D rotation of a Cartesian reference frame can be accomplished with three Euler angles $(\alpha,\beta,\gamma)$, where the sequence rotates the frame successively about one of its axes. The angles can take any value of radians. The most common rotation sequence is denoted Z-X'-Z'', sometimes just called ZXZ. This consists of a right-handed rotation of $\alpha$ about the $z$ axis, followed by a right-handed rotation of $\beta$ about the $x$ axis of the rotated frame (X'), followed by a right-handed rotation of $\gamma$ about the $z$ axis of the rotated frame (Z''). This is represented in a rotation matrix as:
\ea{
\bb{R}_{ZX'Z''}(\alpha,\beta,\gamma) &=& \bb{R}_{Z}(\alpha)\bb{R}_{X}(\beta)\bb{R}_{Z}(\gamma) \\
\ &= &
 \thbth{\cos(\alpha)}{-\sin(\alpha)}{0} {\sin(\alpha)}{ \cos(\alpha)}{ 0} {0}{ 0}{ 1}\thbth{1}{ 0}{ 0}{0 }{\cos(\beta)}{ -\sin(\beta)}{0}{ \sin(\beta) }{\cos(\beta)}\thbth{\cos(\gamma)}{ -\sin(\gamma)}{ 0} {\sin(\gamma)}{ \cos(\gamma) }{0}  {0 }{0 }{1} \label{rot}
}
Each matrix is a right-handed rotation about the axes of the unrotated, or fixed, global frame. The reason the matrices are applied in what seems like reverse order, and applied about the axes of the fixed frame, is because each new matrix stacks the rotations in front of it. This creates the effect that each rotation was applied about the axes of the rotated frame in the order $(\alpha,\beta,\gamma)$.

\begin{figure}[H]
   \centering
   \includegraphics[width=6.5in]{Rotation/Figures/ZXZ}
   \caption{ZX'Z'' rotation sequence $(\alpha,\beta,\gamma) = (\pi/6, \pi/5, \pi/4)$.  First pane shows the rotation of $\alpha$ about the $z$ axis from the original Cartesian frame (black dotted) to the new frame (red/blue/green). Second pane shows the second rotation of $\beta$ about the $x$ axis of the previously rotated frame. Third pane shows the final rotation of $\gamma$ about the $z$ axis of the previously rotated frame.}
   \label{fig1}
\end{figure}

A point $\bb{x} = [x, y, z]^t$ that in the global frame is rotated to the point $\bb{x}' = [x',y',z']^t$ as
\begin{equation}
\bb{x}' = \bb{R}\bb{x}
\end{equation}

\noindent In other words, the point is rotated through the global frame as though you are standing in the global frame. We call this a forward rotation. $\bb{R}$ is unitary, so the inverse is equal to the transpose, and
\begin{equation}
\bb{x} = \bb{R}^t\bb{x}'
\end{equation}

The inverse rotation brings the point back, and applies the Euler angles in the reverse order and reverse direction. Given a forward set of angles $(\alpha,\beta,\gamma)$, applying the inverse is equivalent to fixing a point in the global frame and seeing it as you ride along with the rotating frame. We call this the inverse rotation.

Determining the ZXZ Euler angles from a rotation matrix is done with
\begin{eqnarray}
\alpha & =& \arctan \dfrac{R_{13}}{-R_{23}} \\
\beta & =& \arctan \dfrac{\sqrt{R_{13}^2 + R_{23}^2} }{R_{33}}\\
\gamma &=& \arctan \dfrac{R_{31}}{R_{32}}
\end{eqnarray}

\noindent where $R_{ij}$ are the matrix entries of $\bb{R}$.

The rotation matrix and Euler angle conversions are given in the routines \texttt{euler2rot} and \texttt{rot2euler}. A plotting function,  \texttt{plotrot}, not copied here, will plot the axes of the rotated frame given $\bb{R}$.

{\footnotesize
\VerbatimInput{\code/Rotation/euler2rot.m}
}

{\footnotesize
\VerbatimInput{\code/Rotation/rot2euler.m}
}

%{\footnotesize
%\VerbatimInput{\code/Rotation/plotrot.m}
%}

\clearpage
\section{Rotation Addition Theorem}

The rotation addition theorem for spherical harmonics is given by
\begin{equation}
Y_{lm}(\theta,\phi) = \sum_{p=-l}^{l} D_{lmp}(\alpha,\beta,\gamma) Y_{lp}(\theta',\phi')
\end{equation}

\noindent where $(\alpha,\beta,\gamma)$ are the three Euler angles describing a ZXZ rotation from unprimed to primed coordinate systems. $D_{lmp}$ is the rotation matrix for spherical harmonics. Because the coordinate $r$ and the gradient are invariant under rotation, the rotation addition theorem for scalar and vector wave functions have identical forms \cite{edmonds1996angular,hansen1988spherical,stein1961addition,dufva2008unified},
\begin{equation}
\psi_{lm}(r,\theta,\phi) = \sum_{p=-l}^{l} D_{lmp}(\alpha,\beta,\gamma) \psi_{lp}(r',\theta',\phi')
\end{equation}
\begin{equation}
\bb{M}_{lm}(r,\theta,\phi) = \sum_{p=-l}^{l} D_{lmp}(\alpha,\beta,\gamma)\bb{M}_{lp}'(r',\theta',\phi')
\end{equation}

This means that the same $D_{lmp}$ can be used to rotate any of following: spherical harmonics, scalar spherical wave functions, vector spherical harmonics, or vector spherical wave functions. $D_{lmp}$ is a square block diagonal matrix with blocks that span $m$ and $p$ for a given $l$, zero otherwise.  Specifically,
\begin{equation}
D_{lmp}(\alpha,\beta,\gamma) = e^{im\alpha}d_{lmp}(\beta)e^{ip\gamma} \label{dlmpsep}
\end{equation}

The rotation matrix is inherently separable in $(\alpha,\beta,\gamma)$, where $\alpha$, and $\gamma$ are diagonal. The matrix $d_{lmp}(\beta)$, called "little-d", is block diagonal and can be computed itself. This is advantageous when precomputing rotation matrices over many combinations of Euler angles. The matrix $d_{lmp}(\beta)$ is given by
\begin{eqnarray}
d_{lmp}(\beta) &=&  i^{m-p}\sqrt{\dfrac{(l+p)!(l-p)!}{(l+m)!(l-m)!}} \sum_s (-1)^{s}  {l+m \choose l+p-s} {l-m \choose s}  \left(\cos\frac{\beta}{2}\right)^{2l+p-m-2s}\left(\sin\frac{\beta}{2}\right)^{m-p+2s}  \label{dlmpdirect}
\end{eqnarray}

The sum is over all $s$ for positive arguments of the binomial coefficients. There is a version used in quantum mechanics, \cite{wigner2012group}, based on a ZYZ rotation that yields a real-valued $d_{lmp}(\beta)$ matrix. This forms the basis of a recursion algorithm in Section \ref{littled}. The mapping from complex ZXZ  $d_{lmp}(\beta)$ to the purely real ZYZ is, \cite{littledconversion},
\eq{ d_{lmp}^{ZYZ}(\beta)   =  (-i)^{p-m} d_{lmp}^{ZXZ}(\beta)   \label{zyzzxzconv} }
%\eq{ d_{lmp}^{ZYZ}(\beta)   = i^{p-m} (-1)^{p-m} d_{lmp}^{ZXZ}(\beta)   \label{zyzzxzconv} }

%
%\begin{eqnarray}
%d_{lmp}(\beta) &=&  \sqrt{\frac{(l+p)!(l-p)!}{(l+m)!(l-m)!}}\sum_u {l+m \choose l-p-u}{l-m \choose u} \nonumber \\
%\ & \ & \cdot (-1)^{l-p-u}\left(\cos\frac{\beta}{2}\right)^{m+p+2u}\left(\sin\frac{\beta}{2}\right)^{2l-m-p-2u}
%\end{eqnarray}
%
%The sum is over all $u$ for positive arguments of the binomial coefficients.

%From Wikipedia ("Wigner-D Matrix"), the rotation matrix is given as
%\eq{D_{lmp}(\alpha\beta\gamma) = e^{-im\alpha}d_{lmp}(\beta)e^{-ip\gamma}}
%
%with
%\begin{eqnarray}
%d_{lmp}(\beta) &=&  \sqrt{(l+m)!(l-m)!(l+p)!(l-p)!} \sum_s  \dfrac{(-1)^{m-p+s}}{(l+p-s)!s!(m-p+s)!(l-m-s)!} \nonumber \\
%\ & \ & \cdot  \left(\cos\frac{\beta}{2}\right)^{2l+p-m-2s}\left(\sin\frac{\beta}{2}\right)^{m-p+2s}
%\end{eqnarray}

%\begin{eqnarray}
%d_{m'm}^j(\beta) &=&  \sqrt{(j+m')!(j-m')!(j+m)!(j-m)!} \sum_s  \dfrac{(-1)^{m'-m+s}}{(j+m-s)!s!(m'-m+s)!(j-m'-s)!} \nonumber \\
%\ & \ & \cdot  \left(\cos\frac{\beta}{2}\right)^{2j+m-m'-2s}\left(\sin\frac{\beta}{2}\right)^{m'-m+2s}
%\end{eqnarray}
%
%This is for a ZYZ rotation, which results in the purely real $d_{lmp}$.  For ZXZ Euler rotations, the $(-1)^{m-p+s}$ is replaced with $(-1)^s i^{p-m}$ and $s$ runs from $[0,l+p]$ while for valid factorials.  However, I found that $(-1)^s i^{m-p}$ and multiplying the diagonal matrices in reverse (conjugate transpose) as $e^{im\gamma}d_{lmp}(\beta)e^{ip\alpha}$ matched the values given by the recursion algorithm above. Therefore, the transform between the purely real and complex  $d_{lmp}(\beta)$ is
%\eq{ d_{lmp}(\beta) \leftarrow i^{m-p} (-1)^{m-p} d_{lmp}(\beta)}
%

%\begin{eqnarray}
%d_{lmp}(\beta) &=&  \sqrt{(l+m)!(l-m)!(j+p)!(j-p)!} \sum_s  \dfrac{(-1)^{m-p+s}}{(l+p-s)!s!(m-p+s)!(l-m-s)!} \nonumber \\
%\ & \ & \cdot  \left(\cos\frac{\beta}{2}\right)^{2l+p-m-2s}\left(\sin\frac{\beta}{2}\right)^{m-p+2s}  \\
%\ &=&  \sqrt{\dfrac{(l+p)!(l-p)!}{(l+m)!(l-m)!}} \sum_s  \dfrac{(-1)^{m-p+s} (l+m)!(l-m)! }{(l+p-s)!s!(m-p+s)!(l-m-s)!}   \left(\cos\frac{\beta}{2}\right)^{2l+p-m-2s}\left(\sin\frac{\beta}{2}\right)^{m-p+2s}  \\
%\ &=&  \sqrt{\dfrac{(l+p)!(l-p)!}{(l+m)!(l-m)!}} \sum_s (-1)^{m-p+s}  {l+m \choose l+p-s} {l-m \choose s}  \left(\cos\frac{\beta}{2}\right)^{2l+p-m-2s}\left(\sin\frac{\beta}{2}\right)^{m-p+2s}
%\end{eqnarray}

\subsection{Field Rotations}

Given a field expansion in an unprimed system,
\begin{equation}
\bb{E}(\bb{r}) = \sum_{lm}a_{lm}\bb{M}_{lm}(k,\br)+b_{lm}\bb{N}_{lm}(k,\br)
\end{equation}

the expansion coefficients in the primed system are found by substituting the addition theorem

\begin{equation}
\bb{E}'(\bb{r}') = \sum_{lp}a_{lp}'\bb{M}_{lp}'(k,\br')+b_{lp}'\bb{N}_{lp}'(k,\br')
\end{equation}

\noindent where

\begin{equation}
a_{lp}' = \sum_m a_{lm}D_{lmp}, \quad \quad
b_{lp}' = \sum_m b_{lm}D_{lmp}
\end{equation}

In matrix form, if $\bb{a}$ are the outgoing wave coefficients in the $i$ frame, and $\bb{a}'$ are those in the rotated $i'$ frame, then
\begin{equation}
\bb{a}' = \bb{D}_i\bb{a}
\end{equation}

\noindent where $\bb{D}_i$ is the rotation matrix, and $(\alpha,\beta,\gamma)$ describe the rotation from $i$ to $i'$.  In other words, the application of $\bb{D}_i$ is an inverse rotation on the field (the coefficients $\bb{a}'$ describe the same field but viewed from the rotated frame). $\bb{D}_i$ is unitary, so the inverse is the conjugate transpose: $\bb{D}_i^{-1} = \bb{D}^*_i$.  This is the forward rotation, where the field itself rotates, to follow the Euler angles, as seen from the originating frame.

%, and the inverse relation is
%
%\begin{equation}
%\bb{a} = \bb{D}^*_i\bb{a}'
%\end{equation}

\subsection{Properties}

Here we give several properties of the rotation matrix. The transpose satisfies the following relation
\eq{D_{lmp}(\alpha,\beta,\gamma) = D_{lpm}^*(-\gamma+\pi,\beta,-\alpha+\pi) }
The following relation exists for conjugated matrix elements
\eq{D_{l,-m,-p} = (-1)^{m+p}D_{lmp}^*}

The following identity exists between the $d_{lmp}(\beta)$ and the Legendre polynomials
\ea{d_{l00}(\beta) &=& P_l(\cos\beta) }

The rotation matrix is orthogonal over the three Euler angles as
\eq{\int_{0}^{2\pi} \int_{0}^{\pi} \int_{0}^{2\pi} D_{l'm'p'}^*(\alpha,\beta,\gamma) D_{lmp}(\alpha,\beta,\gamma) d\alpha \sin\beta d\beta d\gamma
= \dfrac{8 \pi^2}{2 l + 1} \delta_{l'l}\delta_{m'm}\delta_{p'p}}

where
\eq{\int_{0}^{2\pi}  d\alpha \int_{0}^{\pi} \sin\beta d\beta  \int_{0}^{2\pi} d\gamma
= 8 \pi^2}

The same integral computed over a matrix element is
\ea{I_{lmp} &=& \int_{0}^{2\pi} \int_{0}^{\pi} \int_{0}^{2\pi} D_{lmp}(\alpha,\beta,\gamma) d\alpha \sin\beta d\beta d\gamma \\
\ &=& 4\pi^2 \delta_{m,0}  \delta_{p,0} \int_{0}^{\pi}d_{lmp}(\beta) \sin\beta d\beta  \\
\ &=& 4\pi^2 \delta_{m,0}  \delta_{p,0}\int_{0}^{\pi}P_l(\cos\beta) \sin\beta d\beta  \\
\ &=& 8 \pi^2 \delta_{l,0}\delta_{m,0}  \delta_{p,0} }

For $\alpha = 0$, $\gamma = 0$, the sum over the diagonal elements of the blocks satisfy
\eq{\sum_{m=-l}^l D_{lmm}(\beta) = \sum_{m=-l}^l d_{lmm}(\beta) = \dfrac{\sin\left(\dfrac{(2 l+1)}{2}\beta\right)}{\sin\left(\dfrac{\beta}{2}\right)}}

which is the normalized Dirichlet kernel.

\clearpage
\newpage

\section{Computation of $D_{lmp}$}

Direct computation of the rotation matrix using \eqref{dlmpsep} and \eqref{dlmpdirect} is not advised because of the need to compute the binomial coefficients. Instead, a fast recursion algorithm for computing the full $D_{lmp}$ is given in \cite{choi1999rapid}. This begins with a rotation matrix defined
\begin{equation}
(\hat{x},\hat{y},\hat{z}) = (x,y,z)\thbth{R_{xx}}{R_{xy}}{R_{xz}}{R_{yx}}{R_{yy}}{R_{yz}}{R_{zx}}{R_{zy}}{R_{zz}} = (x,y,z)\bb{R}
\end{equation}

which is opposite to the rotation matrix in \eqref{rot}. Next, \cite{choi1999rapid} defines the following matrices
\begin{equation}
\bb{D} = \bb{F} + i\bb{G}
\end{equation}
\begin{equation}
\bb{F} = \thbth{(R_{yy}+R_{xx})/2}{R_{xz}/\sqrt{2}}{(R_{yy}-R_{xx})/2}{R_{zx}/\sqrt{2}}{R_{zz}}{-R_{zx}/\sqrt{2}}{(R_{yy}-R_{xx})/2}{-R_{xz}/\sqrt{2}}{(R_{yy}+R_{xx})/2}
\end{equation}
\begin{equation}
\bb{G} = \thbth{(R_{yx}-R_{xy})/2}{R_{yz}/\sqrt{2}}{-(R_{yx}+R_{xy})/2}{-R_{zy}/\sqrt{2}}{0}{-R_{zy}/\sqrt{2}}{(R_{yx}+R_{xy})/2}{R_{yz}/\sqrt{2}}{(R_{xy}-R_{yx})/2}
\end{equation}

\noindent which serves as the initial condition
\eq{D_{1mp} = \bb{D}}
%\begin{eqnarray}
%D_{lmp} &=& F_{lmp} + iG_{lmp} \\
%\bb{D}_{1mp} &=& \bb{D}
%\end{eqnarray}

The recursion equations are given as follows:
\begin{enumerate}
\item For $(-l+1) \leq p \leq (l-1)$:
\begin{equation}
D_{lmp} = a_{lmp}D_{100}D_{l-1,mp}+b_{lmp}D_{110}D_{l-1,m-1,p}+b_{l,-m,p}D_{1,-10}D_{l-1,m+1,p}
\end{equation}
\begin{eqnarray}
a_{lmp} &=& \sqrt{\frac{(l+m)(l-m)}{(l+p)(l-p)}} \\
b_{lmp} &=& \sqrt{\frac{(l+m)(l+m-1)}{2(l+p)(l-p)}}
\end{eqnarray}
$p = \pm l$ is not covered, $a_{lmp} = 0$ for $m=\pm l$, and $b_{lmp} = 0$ for both $m=-l$ and $m=-l+1$.

\item For $-l \leq p \leq (l-2)$:
\begin{eqnarray}
D_{lmp} &=& c_{lm,-p}D_{10,-1}D_{l-1,m,p+1}+d_{lm,-p}D_{11,-1}D_{l-1,m-1,p+1} \nonumber \\
\ & \ & +d_{l,-m,-p}D_{1,-1,-1}D_{l-1,m+1,p+1}
\end{eqnarray}
\begin{eqnarray}
c_{lmp} &=& \sqrt{\frac{2(l+m)(l-m)}{(l+p)(l+p-1)}} \\
d_{lmp} &=& \sqrt{\frac{(l+m)(l+m-1)}{(l+p)(l+p-1)}}
\end{eqnarray}
$p = \pm l$ and $p=(l-1)$ are not covered, $c_{lmp} = 0$ for $m=\pm l$, and $d_{lmp} = 0$ for both $m=-l$ and $m=-l+1$.

\item For $(-l+1) \leq p \leq l$:
\begin{eqnarray}
D_{lmp} &= &c_{lm,p}D_{101}D_{l-1,m,p-1}+d_{lm,p}D_{111}D_{l-1,m-1,p-1} \nonumber \\
\ & \ & +d_{l,-m,p}D_{1,-1,1}D_{l-1,m+1,p-1}
\end{eqnarray}
$p = -l$ and $p = -l+1$ are not covered, and $c_{lmp}$ and $d_{lmp}$ are the same as above.
\end{enumerate}

This algorithm is the basis for two routines that follow. The first routine computes \texttt{Dlmp} on a full square matrix, which has mostly zero elements. The second routine is specialized to compute this on a sparse 1D array to same memory.

\subsection{Full $D_{lmp}$ Matrix}

The routine \texttt{Dlmp} returns the rotation matrix in full, including zeros, starting with $l=1$. As mentioned, this describes an inverse rotation. Use the string switch \texttt{'mono'} to include the monopole term. The algorithm above uses a rotation matrix that is transposed from our convention, so we transpose it on input to keep the routine consistent. There are helper functions to compute the coefficients: \texttt{a\_lmp}, \texttt{b\_lmp}, \texttt{c\_lmp}, \texttt{d\_lmp}.  Figure \ref{fig1} shows the magnitude of $D_{lmp}$ for different $\beta$ ($\alpha$, $\gamma$ do not affect the magnitude).

\begin{figure}[H]
   \centering
   \includegraphics[width=4in]{Rotation/Figures/Dlmp}
   \caption{$D_{lmp}$ magnitude (color scale [0, 1]) for varying $\beta$, $\alpha = \gamma = 0$.}
   \label{fig1}
\end{figure}

{\footnotesize
\VerbatimInput{\code/Rotation/Dlmp.m}
}

\newpage

\subsection{Sparse $D_{lmp}$ Matrix}

The rotation matrix that is the output of \texttt{Dlmp} should not be used directly for matrix-vector multiplication because it contains unnecessary zeros. The quick fix is to immediately transform it to a sparse matrix:

\begin{verbatim}
      D = sparse(Dlmp(L,alpha,beta,gamma));
\end{verbatim}

If several different rotation matrices are needed, they will likely have different numbers of non-zero elements, which can lead to problems with preallocation. Furthermore, the routine \texttt{Dlmp} works on a large, mostly zero, 2D matrix. Therefore, we have a modified version of the routine \texttt{Dlmp}, which recurses on a sparse matrix stored as a 1D array. The modifications follow.

Because the rotation matrix is square block diagonal, the maximum number of non-zero elements (excluding the monopole) is found by counting the elements in blocks that are sized $2l+1$ on a side up to a maximum degree harmonic $L$:
\begin{eqnarray}
N_L &=& \sum_{l=1}^L (2l + 1)^2  \\
\ &=& 4 \sum_{l=1}^L l^2 + 4 \sum_{l=1}^L l + \sum_{l=1}^L 1 \\
%\ &=& 4 \left(\dfrac{L^3}{3} + \dfrac{L^2}{2} + \dfrac{L}{6}\right) + 4\left(\dfrac{L^2 + L}{2}\right) + L \\
\ &=& \dfrac{4}{3} L^3 + 4L^2 + \dfrac{11}{3}L
\end{eqnarray}

Add 1 for the monopole.  Next, we map the 2D row/column linear indices to a 1D index of the sparse matrix. Let the row linear index for the $(l,m)$ harmonic in the full matrix be $l^2 + l + m$ and let the column linear index of the $(l,p)$ harmonic in the full matrix be $l^2 + l + p$.  Counting column-major (top-down, left-right), the linear index for the $(l,m,p)$ element in the sparse array is
\begin{equation}
I(l,m,p) = N_{l-1} + (2l+1)(l+p) + (l+m+1)
\end{equation}

\noindent The term $N_{l-1}$ counts the number of elements for all full blocks less than $l$, the second term counts the number of elements in full columns of the current block less than $p$, the last term counts the rows up to $m$ in the current column.  Again, add 1 for the monopole.

The routine \texttt{DlmpSparse} returns the three column arrays containing the row index, column index, and matrix entries of $D_{lmp}$. It is the same as \texttt{Dlmp} except computed on a 1D array.  Use the string switch \texttt{'mono'} for the monopole.  The two helper functions \texttt{NDlmpSparse} and \texttt{indDlmpSparse} return the number of elements and index for the sparse rotation matrix. A further refinement could use conjugate symmetry to only store half of the matrix plus the main diagonal, but that then requires specialized routines for matrix-vector multiplication. Finally, the helper functions as written create a bottleneck, so a faster version, \texttt{DlmpSparseFast}, is included but not copied here. This uses inline indexing and is about five times faster for moderate values of $L$.

{\footnotesize
\VerbatimInput{\code/Rotation/DlmpSparse.m}
}

{\footnotesize
\VerbatimInput{\code/Rotation/NDlmpSparse.m}
}

{\footnotesize
\VerbatimInput{\code/Rotation/indDlmpSparse.m}
}

\newpage

\section{Computation of $d_{lmp}(\beta)$}
\label{littled}
This section contains routines for the computation of "little-d", $d_{lmp}(\beta)$. This rotation matrix should be used with \eqref{dlmpsep} to precompute and loop over combinations of Euler angles.  Four routines are provided. The first is a direct computation for cross-checking, but should not be used in practice because computing the factorials in the binomial coefficients is not accurate. The second is based on a fast recursion of the purely real ZYZ $d_{lmp}(\beta)$, computed on the full matrix, then converted to complex ZXZ $d_{lmp}(\beta)$.  The third routine is the same as the second but faster. The last routine is the same as the second but computed on the sparse matrix using the sparse indexing tools developed before.

\subsection{Direct Computation}

The routine \texttt{dlmpBetaDirect} computes the ZXZ complex $d_{lmp}(\beta)$ directly from \eqref{dlmpdirect}. This is good to maybe $L = 20$ due to direct computation of the factorials. This is used to validate the recursive algorithms.

{\footnotesize
\VerbatimInput{\code/Rotation/dlmpBetaDirect.m}
}

\subsection{Recursion Algorithm}

A fast computation of $d_{lmp}(\beta)$ is based on the recursive algorithm in \cite{gimbutas2009fast}, which gives an algorithm for the purely real ZYZ matrix. The recursion equations for real $d_{lmp}(\beta)$ are
\ea{d_{lmp} &=& \cos^2 (x) A_{lmp} d_{l-1,m-1,p-1} - 2 \sin (x)\cos (x) B_{lmp} d_{l-1,m-1,p} + \sin^2 (x) C_{lmp} d_{l-1,m-1,p+1} \label{dlmprec1} \\
 d_{lmp} &=& \sin^2 (x)  D_{lmp} d_{l-1,m+1,p-1}+2 \sin (x) \cos (x)  E_{lmp} d_{l-1,m+1,p} + \cos^2 (x)  F_{lmp} d_{l-1,m+1,p+1} \label{dlmprec2} \\
 d_{lmp} &=& \sin (x) \cos (x)  G_{lmp} d_{l-1,m,p-1}+(\cos^2 (x)  - \sin^2 (x) ) H_{lmp} d_{l-1,m,p} -\sin (x)\cos (x)  I_{lmp} d_{l-1,m,p+1} \nonumber \label{dlmprec3} \\}

\noindent where $x = \beta/2$. The coefficients can be written as an outer product
\eq{\thbth{A_{lmp}^2}{B_{lmp}^2}{C_{lmp}^2}{D_{lmp}^2}{E_{lmp}^2}{F_{lmp}^2}{G_{lmp}^2}{H_{lmp}^2}{I_{lmp}^2} =  \thrcol{\dfrac{1}{(l+m)(l+m-1)} }{\dfrac{1}{(l-m)(l-m-1)} }{\dfrac{1}{(l-m) (l+m)} } \thrrow{(l+p)(l+p-1)}{(l+p)(l-p)}{(l-p)(l-p-1) } }

%A_{lmp} &=& \sqrt{\dfrac{(l+p)(l+p-1) }{(l+m)(l+m-1) } } \\
%B_{lmp} &=& \sqrt{\dfrac{(l+p)(l-p) }{(l+m)(l+m-1) } } \\
%C_{lmp} &=& \sqrt{\dfrac{(l-p)(l-p-1) }{(l+m)(l+m-1) } } \\
%D_{lmp} &=& \sqrt{\dfrac{(l+p)(l+p-1) }{(l-m)(l-m-1) } }  \\
%E_{lmp} &=& \sqrt{\dfrac{(l+p)(l-p) }{(l-m)(l-m-1) } }  \\
%F_{lmp} &=& \sqrt{\dfrac{(l-p)(l-p-1) }{(l-m)(l-m-1) } }  \\
%G_{lmp} &=& \sqrt{\dfrac{(l+p)(l+p-1) }{(l-m) (l+m)} }  \\
%H_{lmp} &=&\sqrt{\dfrac{(l+p)(l-p) }{(l-m)(l+m) } }   \\
%I_{lmp} &=&  \sqrt{\dfrac{(l-p)(l-p-1) }{(l-m)(l+m) } }  }

%
%\ea{d_{n}^{m',m} &=& \cos^2 (x) A d_{n-1}^{m'-1,m-1} - 2 \sin (x)\cos (x) B d_{n-1}^{m'-1,m} + \sin^2 (x) C d_{n-1}^{m'-1,m+1} \label{dlmprec1} \\
% d_{n}^{m',m} &=& \sin^2 (x)  D d_{n-1}^{m'+1,m-1}+2 \sin (x) \cos (x)  E d_{n-1}^{m'+1,m} + \cos^2 (x)  F d_{n-1}^{m'+1,m+1} \label{dlmprec2} \\
% d_{n}^{m',m} &=& \sin (x) \cos (x)  G d_{n-1}^{m',m-1}+(\cos^2 (x)  - \sin^2 (x) ) H d_{n-1}^{m',m} -\sin (x)\cos (x)  I d_{n-1}^{m',m+1} \nonumber \label{dlmprec3} \\}
%\ea{x &=& \beta/2\\
%A_{nm'm} &=& \sqrt{\dfrac{(n+m)(n+m-1) }{(n+m')(n+m'-1) } } \\
%B_{nm'm} &=& \sqrt{\dfrac{(n+m)(n-m) }{(n+m')(n+m'-1) } } \\
%C_{nm'm} &=& \sqrt{\dfrac{(n-m)(n-m-1) }{(n+m')(n+m'-1) } } \\
%D_{nm'm} &=& \sqrt{\dfrac{(n+m)(n+m-1) }{(n-m')(n-m'-1) } }  \\
%E_{nm'm} &=& \sqrt{\dfrac{(n+m)(n-m) }{(n-m')(n-m'-1) } }  \\
%F_{nm'm} &=& \sqrt{\dfrac{(n-m)(n-m-1) }{(n-m')(n-m'-1) } }  \\
%G_{nm'm} &=& \sqrt{\dfrac{(n+m)(n+m-1) }{(n-m') (n+m')} }  \\
%H_{nm'm} &=&\sqrt{\dfrac{(n+m)(n-m) }{(n-m')(n+m') } }   \\
%I_{nm'm} &=&  \sqrt{\dfrac{(n-m)(n-m-1) }{(n-m')(n+m') } }  }
%
%In the paper, the numerator for $I$ is $(n-m+1)$, which is incorrect. I found $(n-m-1)$ to be correct, which also preserves the symmetry, so that these can be written as an outer product as
%\eq{\thbth{A^2}{B^2}{C^2}{D^2}{E^2}{F^2}{G^2}{H^2}{I^2} =  \thrcol{\dfrac{1}{(n+m')(n+m'-1)} }{\dfrac{1}{(n-m')(n-m'-1)} }{\dfrac{1}{(n-m') (n+m')} } \thrrow{(n+m)(n+m-1)}{(n+m)(n-m)}{(n-m)(n-m-1) } }

The recursion algorithm first computes \eqref{dlmprec1} for $m=l$ and all $\pm p$, then \eqref{dlmprec2} for $m=-l$ all $p$, and finally \eqref{dlmprec3} for $m=(-l+1),...,(l-1)$ all $p$.  When a coefficient is zero, the corresponding term at $l-1$ does not exist and can be ignored.  With the conversion to complex using \eqref{zyzzxzconv}, this recursion matches the direct compuation and matches the full \texttt{Dlmp} for $(\alpha,\beta,\gamma) = (0,\beta,0)$.  Note, in \cite{gimbutas2009fast}, the numerator of $I_{lmp}$ has $(l-p+1)$, which is incorrect. It should be $(l-p-1)$, so that it preserves the symmetry of the coefficients.

The routine \texttt{dlmpBeta} is a straight implementation of \eqref{dlmprec1}, \eqref{dlmprec2}, \eqref{dlmprec3} on the full matrix (with zeros) with conversion to complex.  Use \texttt{'mono'} to include the monopole. A second version of this routine, \texttt{dlmpBetaFast}, which is not copied here but included in the library, is a faster implementation that uses inline indexing, inline coefficient computation, and vectorized real to complex conversion. Still on the full matrix, it runs about six times faster.

{\footnotesize
\VerbatimInput{\code/Rotation/dlmpBeta.m}
}

\subsection{Sparse Recursive}
Like the full $D_{lmp}$, $d_{lmp}(\beta)$ can be computed as a sparse matrix on a 1D array with proper bookkeeping to avoid having to create and index a large matrix of zeros.  We previously solved the problem of indexing the sparse rotation matrix.  \texttt{dlmpBetaSparse} is a version of \texttt{dlmpBeta} that returns the row, column and matrix entries for sparse complex ZXZ $d_{lmp}(\beta)$.

{\footnotesize
\VerbatimInput{\code/Rotation/dlmpBetaSparse.m}
}

%
%\subsubsection{Log-recurrence for $d_{lmp}(\beta)$}
%We'll derive a recurrence based on the log-transform to compute each matrix element directly. This means including the factorials out front so that they cancel with the factorials in the sum.
%
%\begin{eqnarray}
%d_{lmp}(\beta) &=&  \sum_s (-1)^s i^{m-p}  \dfrac{\sqrt{(l+p)!(l-p)!(l+m)!(l-m)! }}{(l+p-s)!(m-p+s)! s! (l-m-s)!}  \left(\cos\frac{\beta}{2}\right)^{2l+p-m-2s}\left(\sin\frac{\beta}{2}\right)^{m-p+2s}
%\end{eqnarray}
%
%or
%\begin{eqnarray}
%d_{lmp}(\beta) &=& i^{m-p}  \sum_s (-1)^s  e^{\ln a_s}
%\end{eqnarray}
%where
%\eq{a_s = c d_{s}  C_{s} S_{s}  }
%
%\ea{c &=& \sqrt{(l+p)!(l-p)!(l+m)!(l-m)! } \\
%d_{s} &=& \dfrac{1}{(l+p-s)!(m-p+s)! s! (l-m-s)!} \\
%C_{s} &=& \left(\cos\frac{\beta}{2}\right)^{2l+p-m-2s}\\
%S_{s} &=& \left(\sin\frac{\beta}{2}\right)^{m-p+2s} }
%
%Taking the natural log
%\eq{\ln a_s = \ln c + \ln d_{s} + \ln C_{s} + \ln S_{s}  }


\chapter{Transformations}

\section{How transforming a vector transforms its coordinates}

We can imagine rotating a vector by 30 degrees,
but how do the coordinates change?

Let \(f : V \to V\) be a vector transformation.

Let \(T : V \to V\) be a linear function.

Let \(U : \Real^n \to \Real^n\) be a linear function.

Let \(v:V\) be a vector.

Let \(e:\Real^n \to V\) be a basis.

If \(v\) changes to \(T(v)\), then the \(e\)-coordinates change from \(x\) to \(U(x)\).

Let \(x = (x_1,\ldots,x_n)\).

We can explode \(U : \Real^n \to \Real^n\) into \(n\) functions
\(U_1,\ldots,U_n\), each of type \(\Real^n \to \Real\),
so that we can write \(U\) out as
\Formula{
    U(x) = (U_1(x), \ldots, U_n(x))
}

\(T\) is a vector transformation.
\(U\) is the coordinate transformation that corresponds to \(T\).

Let \(E\) be the basis.

Remember that we can write a vector as \(v = E(x)\).

Suppose that we transform a vector from \(v\) to \(T(v)\).

Thus \(T(v) = T(E(x)) = E(U(x))\).

Thus \(T \circ E = E \circ U\).

\section{Rotation in two-dimensional orthonormal basis}

The vector \(a\) rotated by \(t\) radians counterclockwise is the vector \(b\) having the same length
but such that the angle from \(a\) to \(b\) is \(t\).

Let \(\{i,j\}\) be an orthonormal basis.

It can be geometrically shown that
the result of rotating the vector \(xi+yj\) by \(a\) radians
is the vector \(x'i+y'j\) where
\begin{align}
    x' &= \cos(a) \cdot x - \sin(a) \cdot y
    \\ y' &= \sin(a) \cdot x + \cos(a) \cdot y
\end{align}
which we can write as the matrix equation
\Formula{
    \Matrix{x' \\ y'}
    = \Matrix{
        \cos(a) & -\sin(a)
        \\ \sin(a) & \cos(a)
    }
    \Matrix{x \\ y}
}
for which we can also write \( \text{rotate}(a, xi + yj) = x'i + y'j \).
We can also write \( \text{rotate}(e, a, (x,y)) = (x',y')\).
We can also write \( \text{rotate}(e, a, X) = R(a) \cdot X\).

If the basis is orthonormal, then we can define the rotation matrix
\Formula{
    R(a) = \Matrix{
        \cos a & -\sin a
        \\ \sin a & \cos a
    }
}
so that we can write the rotation as matrix multiplication
\( X' = R(a) \cdot X \).

Thus changing the vector \(v\) to \(\text{rotate}(a,v)\)
changes the \(e\)-coordinate tuple \(x\) to \(R(a) \cdot x\).

See also Wikipedia%
\footnote{\url{https://en.wikipedia.org/wiki/Rotation_(mathematics)\#Two_dimensions}}%
\footnote{\url{https://en.wikipedia.org/wiki/Rotation_matrix\#In_two_dimensions}}%
.

\section{Transforming the basis}

We have just rotated a vector.
What if we rotate the basis (the coordinate axes) instead?

See also Wikipedia%
\footnote{\url{https://en.wikipedia.org/wiki/Active_and_passive_transformation}}%
\footnote{\url{https://en.wikipedia.org/wiki/Change_of_basis}}%
\footnote{\url{https://en.wikipedia.org/wiki/Rotation_of_axes}}%
.

\section{Change of basis}

Let \( e : \Real^n \to V \) be a basis.

For example, if \(e(x,y) = xi + yj\).

Let \(t\) be rotation of 90 degrees counterclockwise.

We can think of a change of basis as an invertible function \( t : \Real^n \to \Real^n \).
Then, we can change the basis from \(e\) to \(e \circ t\).

We have a vector \(v : V\).
Its coordinate tuple under basis \(e\) is \(x\).
Its coordinate tuple under basis \(e \circ t\) is \(x'\).
\begin{align*}
    v &= v
    \\ e(x) &= (e \circ t)(x')
    \\ e(x) &= e(t(x'))
    \\ x &= t(x')
    \\ t^{-1}(x) &= x'
\end{align*}

Thus a vector is contravariant.

\section{Coordinateless and coordinateful description of scalar fields}

Let \(f : V \to \Real\) be a scalar field.
We call \(f\) coordinateless because \(f\) does not use any coordinates.

A coordinateful description of \(f\) under basis \(e : \Real^n \to V\) is
another function \(g : \Real^n \to \Real\)
such that \(g = f \circ e\).

How do we describe derivatives coordinatelessly?

\section{Keeping a function while changing basis: contravariance and covariance}

Let there be two functions \( p : \Real \to V \) and \( \phi : V \to \Real \).
Observe how their types mirror each other.
We will explain how \( p \) is contravariant and \( \phi \) is covariant.

An example of \(p\) is a parametric curve.

An example of \( \phi \) is a scalar field such as a temperature field,
which maps each point in space to the temperature at that point.
Another example is a height map,
which maps each point in space to the height of the terrain at that point.

Let \( p_e, p_f : \Real \to \Real^n \) be coordinateful descriptions of \( p \).
Let \(\phi_e : \Real^n \to \Real \) describe \(\phi\) using the basis \(e\).
Let \(\phi_f : \Real^n \to \Real \) describe \(\phi\) using the basis \(f\).
The subscript denotes the basis.
\begin{align*}
    e(p_e(t)) &= p(t)
    \\ f(p_f(t)) &= p(t)
    \\ \phi(v) &= \phi_e(e^{-1}(v))
    \\ \phi(v) &= \phi_f(f^{-1}(v))
\end{align*}

Even though \(p_e\) and \(p_f\) are different functions,
they describe the same coordinateless function \(p\), only with different bases.
We want to change the basis from \(e\) to \(f\), but we want \(p_f\) to describe \(p\).

We write \(f \equiv g\) to mean that \(f\) gives the same result as \(g\) for all parameters.
We have discussed this in \SectionRef{sec:function-equivalence}.
Thus, we can tidy up the equations as
\begin{align*}
    e \circ p_e &\equiv p
    \\ f \circ p_f &\equiv p
    \\ \phi &\equiv \phi_e \circ e^{-1}
    \\ \phi &\equiv \phi_f \circ f^{-1}
\end{align*}
Observe the inverses.

Let \( m : \Real^n \to \Real^n \) be the function that changes the basis from \( e \) to \( f \).
This means \( f \equiv e \circ m \).
(The other possibility \(m \circ e\) does not make sense because the types conflict.)

Then, observe how we have to change the coordinates to keep \(p\) and \(\phi\) the same:
\begin{align*}
    p &\equiv p
    &
    \phi &\equiv \phi
    \\
    e \circ p_e &\equiv f \circ p_f
    &
    \phi_e \circ e^{-1} &\equiv \phi_f \circ f^{-1}
    \\
    e \circ p_e &\equiv (e \circ m) \circ p_{e \circ m}
    &
    \phi_e \circ e^{-1} &\equiv \phi_{e \circ m} \circ (e \circ m)^{-1}
    \\
    e \circ p_e &\equiv e \circ m \circ p_{e \circ m}
    &
    \phi_e \circ e^{-1} &\equiv \phi_{e \circ m} \circ m^{-1} \circ e^{-1}
    \\
    p_e &\equiv m \circ p_{e \circ m}
    &
    \phi_e &\equiv \phi_{e \circ m} \circ m^{-1}
    \\
    m^{-1} \circ p_e &\equiv p_{e \circ m}
    &
    \phi_e \circ m &\equiv \phi_{e \circ m}
\end{align*}

Therefore, summarizing, we get
\begin{align}
    m^{-1} \circ p_e &\equiv p_{e \circ m}
    \\ \phi_e \circ m &\equiv \phi_{e \circ m}
\end{align}

We say that \(p\) is contravariant because of the \(m^{-1}\).

We say that \(\phi\) is covariant because of the \(m\).

A vector is contravariant.

Functions of type \( \Real \to V \) are contravariant.

Functions of type \( V \to \Real \) are covariant.

\section{Einstein notation?}

\paragraph{Subscripts and superscripts for coordinate tuple components?}

A vector is \(v = e(x^1,\ldots,x^n)\).

A parametric curve is \(p(t) = e((p_e(t))^1, \ldots, (p_e(t))^n)\).
We lift it to \(p = E(q^1, \ldots, q^n)\).

A covector is \(\phi(v) = \phi_e(x_1,\ldots,x_n)\).

We overload the superscript, which unfortunately has been used to mean raising a number to a power.
Don't confuse contravariant tuple component notation \(x^3\)
with power notation \(2^3 = 2 \times 2 \times 2\).


\section{Math II - Combinatorics}
  \subsection{Lucas Theorem}
    Compute $\binom{n}{k} \bmod{p}$ in $O(p + \log_p n)$ time, where $p$ is a prime.
    \code{math/combs/lucas.cpp}
  \subsection{Granville's Theorem}
    Compute $\binom{n}{k} \bmod{m}$ (for any $m$) in $O(m^2 \log^2 n)$ time.
    \code{math/combs/granville.py}
  \subsection{Derangements}
    Compute the number of permutations with $n$ elements such that no element is at their original position:
    \[
    D(n) = (n-1) \left( D(n-1) + D(n-2) \right) =  n D(n-1) + (-1)^n
    \]
    % \subsection{Next Permutation}
    % \subsection{Next Combination}
    % \subsection{Next Arrangement}
    % \subsection{Next Partition}
    \subsection{Factoradics}
    Convert a permutation of $n$ items to factoradics and vice versa in $O(n \log n)$.
    \code{math/combs/factoradics.cpp}
  \subsection{$k$th Permutation}
    Get the next $k$th permutation of $n$ items, if exists, using factoradics. All values should be from $0$ to $n-1$. Use factoradics methods as discussed above.
    \code{math/combs/kth-permutation.cpp}
  \subsection{Catalan Numbers}
    \[
        C_n = \frac{1}{n+1}\binom{2n}{n} =  \binom{2n}{n}-\binom{2n}{n+1}
    \]
    \begin{enumerate}
      \item The number of non-crossing partitions of an $n$-element set
      \item The number of expressions with $n$ pairs of parentheses
      \item The number of ways $n+1$ factors can be parenthesized
      \item The number of full binary trees with $n+1$ leaves
      \item The number of monotonic lattice paths of an $n \times n$ grid (5-SAT problem)
      \item The number of triangulations of a convex polygon with $n+2$ sides (non-rotational)
      \item The number of permutations $\{1, \ldots, n\}$ without a 3-term increasing subsequence
      \item The number of ways to form a mountain range with $n$ ups and $n$ downs
    \end{enumerate}
  \subsection{Stirling Numbers}
    $s_1$: Count the number of permutations of $n$ elements with $k$ disjoint cycles

    $s_2$: Count the ways to partition a set of $n$ elements into $k$ nonempty subsets
    \[
    s_1(n,k) = \begin{cases}
        1 & n = k = 0 \\
        s_1(n-1,k-1) - (n-1) s_1(n-1,k) & n,k>0 \\
        0 & \text{elsewhere}
    \end{cases}
    \]
    \[
    s_2(n,k) = \begin{cases}
        1 & n = k = 0 \\
        s_2(n-1,k-1) + k s_2(n-1,k) & n,k>0 \\
        0 & \text{elsewhere}
    \end{cases}
    \]
  \subsection{Partition Function}
    Pregenerate the number of partitions of positive integer $n$ with $n$ positive addends.
    \[
    p(n,k) = \begin{cases}
        1 & n = k = 0 \\
        0 & n < k \\
        p(n-1,k-1) + p(n-k,k) & n \ge k
    \end{cases}
    \]