\section*{Ex.2.3}
\subsection*{Is the identity function $f$ a universal hash function?}
No $f$ is not a universal hash function. By the definition of a universal hash function, $h$ must be a random variable with some distribution, which the identity function is not.


\documentclass[]{article}
\usepackage{amsmath}
\usepackage{bbm}

%opening
\title{Unbiased Pixel Cache Tracing}
\author{Johannes Jendersie}

\begin{document}

\maketitle

\section{Pixel Cache Projection}

For a randomly chosen cache with position $\mathbbm{c}_{i}$ and a random point on the light path $\mathbbm{p}_{i}$ the unbiased light transport is
\begin{equation}
	w = \frac{f_r(\overrightarrow{\mathbbm{p}_{i-1}\mathbbm{p}_i}, \overrightarrow{\mathbbm{p}_i\mathbbm{c}_i})
	f_r(\overrightarrow{\mathbbm{p}_i\mathbbm{c}_i}, \overrightarrow{\mathbbm{c}_i, \mathbbm{c}_{i-1}})}
	{p_{cf}(\overrightarrow{\mathbbm{p}_i\mathbbm{c}_i})/p_c(\overrightarrow{\mathbbm{p}_i\mathbbm{c}_i})}
	\label{eq:transport0}
\end{equation}
where the denominator $p_{cf}/p_c$ is the probability for the connection direction under the distribution of caches.
It depends on the global spherical distribution of pixel caches $p_c(\mathbbm{d})$ with respect to $\mathbbm{p}$ and on the distribution of directions from the BRDF $p_f(\mathbbm{d}) = f_r(\overrightarrow{\mathbbm{p}_{i-1}\mathbbm{p}_i}, \mathbbm{d})$.
The common probability is
\begin{equation}
	p_{cf}(\mathbbm{d}) = \frac{p_c(\mathbbm{d})p_f(\mathbbm{d})}{\int_\Omega\int_\Omega p_c(\mathbbm{d})p_f(\mathbbm{d}) d\mathbbm{d} d\mathbbm{d}}
\end{equation}
because both destributions are independent.
The integrals normalize the distribution function to 1 and are the only reason why $w$ depends on $p_c$.
Putting this back into equation \ref{eq:transport0} gives
\begin{equation}
w = f_r(\overrightarrow{\mathbbm{p}_{i-1}\mathbbm{p}_i}, \overrightarrow{\mathbbm{p}_i\mathbbm{c}_i})
	f_r(\overrightarrow{\mathbbm{p}_i\mathbbm{c}_i}, \overrightarrow{\mathbbm{c}_i, \mathbbm{c}_{i-1}})
	\frac{\int_\Omega\int_\Omega p_c(\mathbbm{d})p_f(\mathbbm{d}) d\mathbbm{d} d\mathbbm{d}}
	{p_f(\overrightarrow{\mathbbm{p}_i\mathbbm{c}_i})}
\end{equation}

To solve the integral we would need to iterate over all existing caches which is unfeasible.
However, making assumptions to $p_f$ can remove this ugly term.

\subsection{Diffuse case}

Setting $p_f$ to an uniform distribution on the hemisphere gives the following quotient.
\begin{align}
p_f(\mathbbm{d}) &= \frac{1}{2\pi}\\
q &= \int_\frac{\Omega}{2}\int_\frac{\Omega}{2} p_c(\mathbbm{d}) d\mathbbm{d} d\mathbbm{d}\\
&= 2\pi p_c\left(\in\frac{\Omega}{2}\right)
\end{align}
Problem: we need to integrate over $\frac{\Omega}{2}$! $p_f$ is 0 otherwise.
I.e. the inner integral is the probability that the cache is in the positive hemisphere $p_c(\in\frac{\Omega}{2})$ which is constant.
The constant integrated over the hemisphere just adds another $2\pi$.
%With the assumption that $p_f=1/2\pi$ for the entire sphere we can go further.
%This is allowed because we discard the path anyway and the further weight is irrelevant.
%In that case the inner integral resolves to 1 (as this is a property of the density function $p_c$).
%\begin{align}
%q &= \int_\Omega 1 d\mathbbm{d}\\
%&= 4\pi
%\end{align}

\end{document}


\documentclass{article}
\usepackage[utf8]{inputenc}

\title{MAT257 Notes}
\author{Jad Elkhaleq Ghalayini}
\date{October 22 2018}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{mathtools}
\usepackage{enumitem}
\usepackage{graphicx}
\usepackage{cancel}

\usepackage[margin=1in]{geometry}

\newtheorem{theorem}{Theorem}
\newtheorem{lemma}{Lemma}
\newtheorem{definition}{Definition}
\newtheorem*{corollary}{Corollary}
\newtheorem{exercise}{Exercise}

\newcommand{\reals}[0]{\mathbb{R}}
\newcommand{\nats}[0]{\mathbb{N}}
\newcommand{\ints}[0]{\mathbb{Z}}
\newcommand{\rationals}[0]{\mathbb{Q}}
\newcommand{\brac}[1]{\left(#1\right)}
\newcommand{\sbrac}[1]{\left[#1\right]}
\newcommand{\mc}[1]{\mathcal{#1}}
\newcommand{\eval}[3]{\left.#3\right|_{#1}^{#2}}
\newcommand{\ip}[2]{\left\langle#1,#2\right\rangle}
\newcommand{\prt}[2]{\frac{\partial #1}{\partial #2}}

\begin{document}

\maketitle

\section*{Tangent Space}
We begin with some examples:
\begin{enumerate}

  \item Consider an ellipsoid
  \[\frac{x^2}{a^2} + \frac{y^2}{b^2} + \frac{z^2}{c^2} = 1\]
  The tangent plane at a point \((x_0, y_0, z_0)\) of the ellipsoid is given by
  \[\frac{\cancel{2}x_0}{a^2}(x - x_0) + \frac{\cancel{2}y_0}{b^2}(y - y_0) + \frac{\cancel{2}z_0}{c^2}(z - z_0) = 0\]
  \[\frac{x_0}{a^2}x + \frac{y_0}{b^2}y + \frac{z_0}{c^2}z = 1\]

  \item Let \(f(x, y) = 0\) and \(g(x, y) = 0\) be curves in \(\reals^2\). Assume the curves are smooth and intersect at point \((a, b)\). The two curves are \underline{orthogonal} at \((a, b)\) if
  \[\prt{f}{x}\prt{g}{x} + \prt{f}{y}\prt{g}{y} = 0\]
  at \((a, b)\). The two curves are \underline{tangent} at \((a, b)\) if the gradients are proportional to each other, i.e.
  \[\prt{f}{x}\prt{g}{y} - \prt{f}{y}\prt{g}{x} = 0\]
  at \((a, b)\).

  \item Consider the family of parabolas
  \[y^2 - 2p(x + p/2) = 0\]
  Here, \(p\) is the parameter. These parabolas open along the \(x\) axis, either to the right or two the left, with \(x\) acting like a function of \(y\).

  This kind of family is what's called \textit{confocal}: these parabolas all have the same focus, 0. Of course, they don't all intersect, but all those which open to the right intersect all those which open to the left. Let's look at the intersections.

  If \(p_1 > 0\) and \(p_2 > 0\) then the two parabolas
  \[f(x, y) = y^2 - 2p_1(x + p_1/2) = 0, g(x, y) = y^2 - 2p_2(x + p_2/2) = 0\]
  intersect where
  \[f(x, y) = y^2 - 2p_1(x + p_1/2) = 0 = g(x, y) = y^2 - 2p_2(x + p_2/2)\]
  Let's show that all the intersections among all these possible parabolas are right angles.

  Let's try to compute the inner product of the gradients. That is, we want to show
  \[f_xg_x + f_yg_y = 0\]
  at an intersection point. So what's the product of the \(x\) derivatives? Well it'll just be
  \[f_xg_x = (-2p_1)(-2p_2) = 4p_1p_2\]
  And what's the product of the \(y\) derivatives?
  \[f_yg_y = (2y)(2y) = 4y^2\]
  So we want to see that
  \[f_xg_x + f_yg_y = 4p_1p_2 + 4y^2\]
  vanishes when both \(f\) and \(g\) are zero.

  If you take \(p_2\) times \(f\), and subtract \(p_1\) times \(g\), you get rid of the \(x\) terms:
  \[p_2f - p_1g = p_2y^2 - 2p_2p_1(x + p_1/2) - p_1y^2 + 2p_1p_2(x + p_2/2) = (p_1 - p_2)y^2 + (p_2 - p_1)p_1p_2\]
  So we have
  \[f_xg_x + f_yg_y = 4p_1p_2 + 4y^2 = 4\frac{p_2f - p_1g}{p_2 - p_1} = 0\]
  Since \(f = g = 0\) and \(p_2 - p_1 \neq 0\) at the intersection point.

\end{enumerate}
So these are some examples of computations with tangent spaces.

\section*{Proof of the Inverse Function Theorem}
The hypotheses of the inverse function theorem are as follows: \(f: \reals^n \to \reals^n\) is a \(\mc{C}^1\) function in a neighborhood of a point \(a \in \reals^n\) such that
\[\det f'(a) \neq 0\]
We have to show that there are open neighborhoods \(V\) of \(a\) and \(W\) of \(f(a)\) such that \(f: V \to W\) and has an inverse \(f^{-1}: W \to V\) which is differentiable. Remember of course that the inverse function theorem includes a very important formula for the inverse, but this is theminimum that we have to prove, since if we prove this we can get the formula for the inverse function just from the Chain Rule. We also already checked before that if we have the formula for the inverse, we can check from the formula that the inverse if \(\mc{C}^1\), or in the general case \(\mc{C}^r\).

One thing that in general it is good to do at the beginning, and it's something that in general we'll do at the beginning of a lot of things in this course, is that when you're given a problem which has a condition depending on the linear part of a function, for example, here, that the linear part is invertible, its always very simple to reduce to the case that the linear part is the identity. So let's just see here why we can make that assumption, that is, that we can assume without loss of generality that
\[f'(a) = I\]
So, why? Let
\[\lambda = f'(a)\]
If we know this, how can we use \(\lambda\) and \(f\) to find another function such that it's derivative at that point will be the identity? Quite simply, we can write, since \(\lambda\) is invertible,
\[g = \lambda^{-1} \circ f \implies g'(a) = (\lambda^{-1})Df + (\cancel{D\lambda^{-1}})f = \lambda^{-1}\lambda = I\]
by the Chain Rule. And if \(g\) has an inverse \(g^{-1}: W' \to V\) where \(W'\) is an open neighborhood of \(g(a)\), then \(f\) has an inverse \(f^{-1}: W \to V\) given by
\[f = \lambda \circ g \implies f^{-1} = g^{-1} \circ \lambda^{-1}\]
where \(W' = \lambda(W)\). So knowing the theorem for \(g\), it follows for \(f\).
So first we'll show the statement above, but we'll check that the inverse is merely continuous instead of differentiable.

Let's stop here, and hopefully do the entire proof next time.

\end{document}


\subsection{Synthetic Data}

First, tests were run on data generated in situ from a list of frequencies,
coefficients, and phase differences

\begin{align*}
    c &= \{c_1, c_2, \ldots, c_n\}\\
    \omega &= \{\omega_1, \omega_2, \ldots, \omega_n\}\\
    \phi &= \{\phi_1, \phi_2, \ldots, \phi_n\}\\
\end{align*}

which generate a 'generating' function for the discrete signal to follow

\begin{align*}
    f(x) = \sum_{i = 1}^{n} c_i \cdot sin(w_i x + \phi_i)
\end{align*}

implemented as a lambda-function in code

\begin{lstlisting}[language=C++]
auto f_gen = [w, c, p](int x){
                float sum = 0;
                for(int i = 0; i < wlist.size(); i++){
                    sum += c[i] * sin(w[i]*x + p[i]);
                }
                return 7.0*sum/float(std::accumulate(c.begin(), c.end(), 0));
            };

auto f = new signal[SIZE];

for(int x = 0; x < SIZE; x++){
    f[i] = signal(f_gen(2*x), f_gen(2*x + 1));
}
\end{lstlisting}

The normalising factor may be succinctly written as

\begin{align*}
    a_N = \frac{7.0}{\sum_{i = 1}^{n}c_i}~.
\end{align*}

It simply ensures that the cross correlation of the signals do not blow up and
that a single signal agnostic threshold value may be chosen as a metric for
matching. The factor of 7 scales the float value to the range (-7, 7) so as to
maintain reasonable resolution after conversion to integer type to save memory.

The signal is generated as even/odd pairs to facilitate pairwise storage
implemented by \nameref{sec:doobit}. The typename \texttt{signal} is an alias
for \texttt{doobit}, kept as such for modularity amongst storage backends, and
to possibly expand across architectures, saving major edits.

Playing with the described parameters, experiments were performed, and the
results have been tabulated in \autoref{fig:synthexp}. Since we look for a
global maxima of cross correlation, the phase is irrelevant to the results, but
for the sake of completeness, an indication has been provided where phase shifts
were tested. Several arbitrary values were tested, with the result being
uneaffected as expected.

All synthetic tests were performed with a resolution of 1ms, and 400 data points.

\begin{figure}[ht]
    \centering
    \def\arraystretch{1.5}
    \setlength\tabcolsep{2em}
    \begin{tabular}{c | c | c}
        NCC Threshold (t)   & Input frequencies and coefficients (kHz)      & Output (kHz)  \\ \hline
        0.1                 & 0.3, 0.5, 0.8                                 & 0.3, 0.5, 0.8 \\
        0.1                 & 0.3, 0.5, 0.8$^\dagger$                       & 0.3, 0.5, 0.8 \\
        0.1                 & 0.3, 0.5$^\dagger$, 0.8$^\dagger$             & 0.3, 0.5, 0.8 \\
        0.2                 & 0.3, 0.5$^\dagger$, 0.8$^\dagger$             & 0.3, 0.5, 0.8 \\
        0.2                 & 0.3, 0.5$^\dagger$, 0.8$^\dagger$             & 0.3, 0.5, 0.8 \\
        0.2                 & 0.3, 4$\times$ 0.5$^\dagger$, 0.8$^\dagger$   & 0.3, 0.5, 0.8 \\
        0.4                 & 0.3, 4$\times$ 0.5$^\dagger$, 0.8$^\dagger$   & 0.3, 0.5, 0.8 \\
        0.5                 & 0.3, 4$\times$ 0.5$^\dagger$, 0.8$^\dagger$   & 0.3, 0.5      \\
        0.6                 & 0.3, 4$\times$ 0.5$^\dagger$, 0.8$^\dagger$   & 0.5           \\
        0.8                 & 0.3, 4$\times$ 0.5$^\dagger$, 0.8$^\dagger$   & $\emptyset$   \\
        0.1                 & 0.3, 0.5, 0.35$^\dagger$                      & 0.3, 0.4, 0.5 \\
        0.2                 & 0.3, 0.5, 0.35$^\dagger$                      & 0.3, 0.5
    \end{tabular}
    \captionsetup{justification=centering}
    \caption{Experiments with varying synthetic inputs and thresholds.\\
    $\dagger.$ Phase shifted. Weight unit unless otherwise specified}
    \label{fig:synthexp}

\end{figure}

\subsection{Real Inputs}

Not having a microphone module for the Arduino, we resorted to passing a
recorded array to the device via MATLAB, opening its own can of worms by way of
buffer overflows and race conditions, as described in detail in
\autoref{subsec:buffinp}.

For currently unknown reasons, we had memory overflows with the same size of
real data as we ran synthetic tests for. Curiously, in this case, the Arduino
continuosly prints \texttt{'w'} on the Serial line. After much testing we have
linked this behaviour to memory overflow, but are yet to find resources
confirming it.

Reducing the size, however, allows us to perform some tests, limited by the data
transfer speed as well, with the serial line taking up to 30 seconds to transfer
all the data successfully, yet with significant packet loss. The best transfer
ratio we were able to obtain after fine tuning the transfer rates and timing was
400 packets received for 420 sent, but at this delicate parameter island, the
Arduino would, with roughly half probability, run out of memory. It may be
dependent on buffering of incoming packets. Buffering just a few more packets
may have been pushing it over the edge.

For generating required frequencies, the Android app
\href{https://play.google.com/store/apps/details?id=com.luxdelux.frequencygenerator}{Frequency
Sound Generator} was used.

All real tests were performed with a resolution of 1ms, and 200 data points.

\begin{figure}[ht]
    \centering
    \def\arraystretch{1.5}
    \setlength\tabcolsep{2em}
    \begin{tabular}{c | c | c}
        NCC Threshold (t)   & Input signals                                 & Output (kHz) \\ \hline
        0.1                 & 0.4 kHz (generated using a phone speaker)     & 0.2$^\ast$, 0.3, 0.4 \\
        0.1                 & 0.5 kHz (generated using a phone speaker)     & 0.3, 0.4, 0.5, 0.6$^\ast$ \\
        0.1                 & 0.6 kHz (generated using a phone speaker)     & 0.4, 0.5, 0.6, 0.7$^\ast$ \\
        0.2                 & 0.6 kHz (generated using a phone speaker)     & 0.4, 0.5, 0.6 \\
        0.3                 & 0.6 kHz (generated using a phone speaker)     & $\emptyset$ \\
        0.1                 & Speech ("Hello Hello")                        & 0.2$^\ast$, 0.3, 0.4, 0.5$^\ast$, 0.6 \\
        0.1                 & Middle C (261 Hz)                             & 0.3 \\
        0.1                 & Middle A (440 Hz)                             & 0.3, 0.4
    \end{tabular}
    \captionsetup{justification=centering}
    \caption{Experiments with real inputs and thresholds. $\ast$ indicating the frequency appeared sometimes during several test runs.}
    \label{fig:realexp}

\end{figure}
We further noted that the algorithm itself could give us better resolution with more frequencies. However, as the memory in our Arduino was quite strained, we ran into memory overflow issues with garbage data values being sent. Howver, this algorithm can be easily scaled up on better devices by adding more frequencies to test with.

\subsection{Proofs}

Screenshots for the code compiled and working may be seen in
\autoref{fig:pushkcompile} and \autoref{fig:sankacompile} respectively for the
group emmbers, and a photo each of the physical setups is found in
\autoref{fig:realsetup}. These, along with some more photos, and video
demonstrations, have been uploaded to the required shared directory, as well as
\href{https://drive.google.com/drive/folders/1jjNn4WWWuB7WvTU04xIGaotz5W_Lfz55?usp=sharing}{to
our own folder, linked here}.

\begin{figure}[pt]
    \centering
    \includegraphics[width=0.9\textwidth]{fig/pushkcompile1.png}

    \includegraphics[width=0.9\textwidth]{fig/pushkcompile2.png}
    \caption{Code compiled and running tests for Pushkar}
    \label{fig:pushkcompile}
\end{figure}

\begin{figure}[pt]
    \centering
    \includegraphics[width=0.9\textwidth]{fig/sankacompile1.png}

    \includegraphics[width=0.9\textwidth]{fig/sankacompile2.png}
    \caption{Code compiled and running tests for Sankalp}
    \label{fig:sankacompile}
\end{figure}

\begin{figure}[pt]
    \centering
    \includegraphics[width=0.9\textwidth]{fig/pushkreal.png}

    \includegraphics[width=0.9\textwidth]{fig/sankareal.png}
    \caption{Physical setup. Top, Pushkar; bottom, Sankalp.}
    \label{fig:realsetup}
\end{figure}


\section{PIC Implementation}

Implementing PIC into SINATRA was completed using the algorithm seen in Section \ref{sec:algorithm}. Two important factors were critical during implementation: robustness and efficiency. SINATRA is built to be able to simulate many different fluid scenarios chosen by the user. Therefore, the PIC portion should also have this generality. Therefore, even though the Finite Difference method requires equal cell sizes, the rest of the algorithms are based on the individual cells so that when the mesh is non-uniform the simulation could be updated with minimum work. It was also implemented in an efficient manner for the DSMC setup and algorithms found in SINATRA. It uses flattened arrays, built-in particle and cell index arrays, and a structure which fits the current parallelization within SINATRA. \textcolor{white}{Live Long and Prosper}

\subsection{Finite Difference}
\label{sec:finite_diff}

\indent Finite Difference was chosen as the method to discrete Poisson's equation. Finite difference is a simple and common method for discretizing the Poisson equation and is common in PIC codes. There are other algorithms which could be used, but Finite Difference allows for a relatively simple implementation. The derivation below is for a 7-point finite difference for the Poisson equation \cite{FD_GS,FDM}.

The derivation begins with the Poisson equation expanded out to each of the 3 directions.

\begin{equation}
    \label{eqn:poisson_expanded}
    \nabla^2 \phi = \frac{\partial^2 \phi}{\partial x^2} + \frac{\partial^2 \phi}{\partial y^2} + \frac{\partial^2 \phi}{\partial z^2} = - \frac{\rho}{\epsilon_0}
\end{equation}

% Make those
Next the Poisson equation can be discretized in the \(x \: \text{,} \: y \: \text{and} \: z\) directions as shown in Equations \ref{eqn:x_partial}, \ref{eqn:y_partial}, and \ref{eqn:z_partial}. The indices for \(x \: \text{,} \: y \: \text{and} \: z\) are given by \(i \: \text{,} \: j \: \text{and} \: k\) respectively \cite{FD_GS}.

\begin{equation}
    \label{eqn:x_partial}
    \frac{\partial^2 \phi}{\partial x^2}(x_i,y_i,z_i) \approx \frac{1}{h^2}(\phi(x_{i-1},y_j,z_k) - 2\phi(x_i,y_j,z_k) + \phi(x_{i+1},y_j,z_k)),
\end{equation}
\begin{equation}
    \label{eqn:y_partial}
    \frac{\partial^2 \phi}{\partial x^2}(x_i,y_i,z_i) \approx \frac{1}{h^2}(\phi(x_i,y_{j-1},z_k) - 2\phi(x_i,y_j,z_k) + \phi(x_i,y_{j+1},z_k)),
\end{equation}
\begin{equation}
    \label{eqn:z_partial}
    \frac{\partial^2 \phi}{\partial x^2}(x_i,y_i,z_i) \approx \frac{1}{h^2}(\phi(x_i,y_j,z_{k-1}) - 2\phi(x_i,y_j,z_k) + \phi(x_i,y_j,z_{k+1})),
\end{equation}

Substituting these into Equation \ref{eqn:poisson_expanded} gives Equation \ref{eqn:poisson_full} below.

\begin{align}\label{eqn:poisson_full}
    \frac{\phi(x_{i-1},y_j,z_k) - 2\phi(x_i,y_j,z_k) + \phi(x_{i+1},y_j,z_k)}{h^2} +& \nonumber \\
    \frac{\phi(x_i,y_{j-1},z_k) - 2\phi(x_i,y_j,z_k) + \phi(x_i,y_{j+1},z_k)}{h^2} +& \nonumber \\
    \frac{\phi(x_i,y_j,z_{k-1}) - 2\phi(x_i,y_j,z_k) + \phi(x_i,y_j,z_{k+1})}{h^2} =& - \frac{\rho_{ijk}}{\epsilon_0}
\end{align}
\(h\) = Cell side length \par

This builds a set of linear equations which is a common problem in linear algebra and therefore multiple methods exist to solve it. First, the matrix must be created. This matrix, also called a stencil, only needs to be created once per simulation because it is only dependent on the mesh. Equation \ref{eqn:stencil} shows the stencil for a 7-point mesh on a \(3\times3\times3\) node domain. \par

% Make this prettier  \ddots
\begin{equation}
\label{eqn:stencil}
A =
\begin{bmatrix}
S & I &  & I &  &  &  &  & \\
I & S &I  &  & I &  &  &  & \\
 & I & S &  &  &I  &  &  & \\
I &  &  & S & I &  &I &  & \\
 & I &  & I & S & I &  &I  & \\
 &  & I &  & I & S &  &  & I\\
 &  &  & I &  &  & S & I & \\
 &  &  &  & I &  & I & S & I\\
 &  &  &  &  & I &  & I & S
\end{bmatrix}
\end{equation}
where,
\begin{equation} \nonumber
S =
\begin{bmatrix}
-6 & 1 & \\
1 & -6 & 1\\
 & 1 & -6 \\
\end{bmatrix}
\ \text{and} \ I =
\begin{bmatrix}
1 &  & \\
 & 1 & \\
 & & 1 \\
\end{bmatrix}
\end{equation}

\indent Boundary conditions in a finite difference model can either be Neumann or Dirichlet. Neumann boundary conditions define the derivative at the boundary while Dirichlet define the value of the solution along the boundary. In this case, that means Neumann boundaries define the electric field while Dirichlet conditions define the value of the electric potential. For PIC simulations, it is best to use Neumann for open or uncharged boundaries while Dirichlet is best for objects like charged surfaces. SINATRA currently only includes Neumann boundary conditions; Dirichlet will be added when the boundary class is updated.\footnote{This current condition causes the stencil to be ill posed for a direct solver like Gauss-Seidel \cite{bvp_neumann}. This means that while it will converge upon an answer it is possible that it converged upon an inaccurate result and therefore will skew the results. For current results it has be observed to calculate relatively accurate solutions, however this should be explored in further work and users should be stopped from selecting this situation in the initial conditions.}Neumann boundaries have been implemented by editing the stencil when the node is along a boundary. At that point Equation \ref{eqn:neumann} is used to change the values of the stencil. An average is calculated between the direction of boundary and the perpendicular direction in order to keep the slip boundary along open walls. \par

\begin{equation}
    \label{eqn:neumann}
     A_{x_0,y_j,z_k} = \frac{-\phi(x_0,y_j,z_k) + \phi(x_1,y_j,z_k)}{2 h}
\end{equation}

\indent This stencil was implemented into SINATRA as a flattened 2D matrix which in itself is two flattened 3D matrices. The electric potential is stored as a flattened 3D matrix. The matrix items are selected through conversion functions that go from 3 dimensions to 1 dimension and vice versa, shown in Equations \ref{eqn:to1D} and \ref{eqn:to3D}. Figure \ref{fig:sparse} shows a MATLAB\textsuperscript{\textregistered} \textit{spy} command on SINATRA's stencil. The spy command shows which items in a matrix are non-zero. As seen by these figures the stencil is largely empty. Future work would include using a different data structure and access algorithm to reduce the wasted memory. \par

\begin{figure}
    \centering
  \begin{minipage}[b]{0.49\textwidth}
    \includegraphics[width=\textwidth]{figures/sparse_8.jpg}
  \end{minipage} %
  \begin{minipage}[b]{0.49\textwidth}
    \includegraphics[width=\textwidth]{figures/sparse_64.jpg}
  \end{minipage}
  \caption[SINATRA Sparse Stencil Matrix]{SINATRA Sparse Stencil Matrix\textmd{, shown through the MATLAB\textsuperscript{\textregistered} spy command (left) 8 Cells in the mesh (right) 64 cells in the mesh.}}
  \label{fig:sparse}
\end{figure}

\Needspace{5\baselineskip}
\begin{equation}
    \label{eqn:to1D}
    Index = i \times s^2 + j \times s + k
\end{equation}
\(Index\) = The index of the 1D array \\
\(i \: \text{,} \: j \: \text{and} \: k\) = The indices of the 3D array \\
\(s\) = The size of one dimension of the 3D array \par

\Needspace{8\baselineskip}
\begin{align}\label{eqn:to3D}
    i =& \frac{Index}{s^2} \nonumber \\
    j =& \Big(\frac{Index}{s}\Big) \: \% \: s \\
    k =& Index \: \% \: s \nonumber
\end{align}
Note: Values rounded down to closest whole number for use as indices \newline
\(\%\) = The modulus operator which gives the remainder of division \par

\indent At this stage, the simulation assumes equal cell sizes and refinement across the whole domain. However, the charge density summation is calculated cell by cell, and the electric field is distributed cell by cell. These allow future iterations to have unequal cell sizes. \par

\subsection{Gauss-Seidel}
\label{sec:gauss}

The Gauss-Seidel iterative method was chosen to be the linear algebra solver for SINATRA. It was chosen for its simplicity, universality, and robustness. It has one improvement over the simplest iterative method, Jacobi, in that it uses the information as it calculates it. It solves a version of the Equation \ref{eqn:linearset}.

\Needspace{8\baselineskip}
\begin{equation}
    \label{eqn:linearset}
    A\cdot x = b
\end{equation}
In terms of SINATRA, \\
\(A\) = Stencil - Matrix of coefficients\\
\(x\) = Electric Potential - The vector to be solved \\
\(b\) = Charge Density - The calculated vector \par

The Charge Density (\(b\)), as defined in Equation \ref{eqn:density}, includes the electron density. In SINATRA the electrons are approximated with a fluid assumption as shown in the Boltzmann relationship (\ref{eqn:e_density}). This thesis will not go into a derivation of the Gauss-Seidel method, but the equation to update the electric potential at each time-step is given by Equation \ref{eqn:gauss_seidel} \footnote{A derivation of the Gauss-Seidel method can be found in Reference \cite{Gauss_eqn}}.

\begin{equation}
    \label{eqn:gauss_seidel}
    x_i^{k+1} = \Big( b_i - \sum_{j=1}^{i-1} A_{i j} x_j^{k+1} - \sum_{j=i+1}^n A_{i j} x_j^{k} \Big) / a_{ii}
\end{equation}

\indent  It was discussed previously how to distribute the ion's charge, but not how to combine the ion charge and the electron charge. Therefore, by discretizing Poisson's Equation (\ref{eqn:poisson}), combining it with Equations \ref{eqn:density} and \ref{eqn:e_density} for the charge density, and putting it in matrix form (\ref{eqn:stencil}) we get Equation \ref{eqn:mixed}.

\Needspace{10\baselineskip}
\begin{equation}
    \label{eqn:mixed}
    A \cdot \phi = - \frac{e}{\epsilon_0} \Big[n_i - n_o \exp\Big(\frac{\phi - \phi_0}{k \: T_e}\Big)\Big]
\end{equation}
\(A\) = The sparse stencil \\
\(e\) = Charge on an electron \\
\(\epsilon_0\) = Permittivity of free space \\
\(n\) = Number density \\
\(\phi\) = Electric Potential \\
\(\phi_0\) = Initial Electric Potential \\
\(k\) = Boltzmann Constant \\
\(T_e\) = Temperature of the Electrons \par

\indent This shows that the \(b\) in Equation \ref{eqn:linearset} is the right hand side of the set of linear equations for the Gauss-Seidel solver. Importantly, note that both sides depend on the electric potential. It means that the right hand side must be recalculated at every time-step. \par


\section{Symbolic Execution in Presence of Unbounded Loops\label{sec-motor-control}}

Many programs targeting REDFIN share the distinctive feature of
the energy estimation program considered in section~\S\ref{sec-verification},
i.e. the existence of an  \emph{upper bound on execution time},
since their termination does not depend on input data.
However, other programs may have a loop
which is guarded by a termination condition that involves computation
considering the input parameters of the program, thus making the loop
\emph{unbounded}.

Presence of unbounded loops makes program verification by symbolic execution considerably
harder~\cite[p.~50:20]{SurveySymExec-CSUR18}, since the number of program execution
paths becomes infinite. In this section we consider an example
of a control program that drives a stepper motor and verify one of its essential
safety properties by formulating it as a~\emph{loop invariant} and ensuring that the
invariant holds for every possible state of the loop.

\subsection{Stepper Motor Control Program}

Stepper motors are often deployed as parts of antenna and solar panel pointing units
in space satellites. We consider a program for controlling a motor with
one degree of freedom. The control Algorithm~\ref{alg-motor} takes three input
parameters:
\begin{itemize}
\item $dist$ --- the distance to move the motor
\item $v_{max}$ --- the maximal permitted velocity
\item $a_{max}$ --- the maximal permitted acceleration
\end{itemize}

\noindent
and computes a series of
displacement and velocity values that will be used to move the motor. Since the algorithm is
designed for controlling a stepper motor, the calculations happen in~\emph{discrete time},
i.e. every iteration of the $while$ loop corresponds to a time interval; thus
the deceleration (i.e. braking) distance is computed as
\[
s_{decel} = a_{max} \cdot \frac{decel\_steps \cdot (decel\_steps + 1)}{2},
\]
where $decel\_steps = \frac{v}{a_{max}}$ is the number of decelerating iterations
needed for a full stop.

The conditional statement in line 9 decides whether
to accelerate, to keep the velocity, or to decelerate;
see Fig.~\ref{fig-motor} for example plots of velocity and distance travelled
against time. The spike at the bottom-right of the velocity plot illustrates
the edge case covered by the conditional statement on line 18: if the velocity
is zero, but the target distance has not yet been reached, the motor must be
moved further.

\begin{algorithm}[h]
  \begin{algorithmic}[1]
\Require {$dist$, $v_{max}$, $a_{max}$}
\State $s \gets 0$
\State $v \gets 0$
\While {$true$}
  % Compute deceleration distance based on current speed
  \State $decel\_steps\gets v / a_{max}$
  \algorithmiccomment{Compute deceleration distance}
  \State $s_{decel} \gets a_{max} \cdot decel\_steps \cdot (decel\_steps + 1) / 2$
  \algorithmiccomment{based on the current velocity\hspace{2.55mm}~}
  \If {$decel\_steps \cdot a_{max} \neq v$}
    \State $s_{decel} \gets s_{decel} + v$
  \EndIf
  \State {$v_{next} = min(v_{max}, dist, v + a_{max})$}
  \If {$s + s_{decel} + v_{next} \leq dist$}
    \State $v \gets v_{next}$ \algorithmiccomment{Accelerate}
  \ElsIf {$s + s_{decel} + v \leq dist$}
    \State $ v \gets v $     \algorithmiccomment{Keep velocity}
  \Else
     \algorithmiccomment{Decelerate}
    \If {$v > decel\_steps \cdot a_{max}$}
      \State $v \gets decel\_steps \cdot a_{max}$
    \Else
      \State $v \gets v - a_{max}$
      \EndIf
  \EndIf
  \If {$v = 0$}
    \If {$s \neq dist$}  \algorithmiccomment{Accelerate again to reach target}
      % didn't quite reach our target after deceleration
      % => accelerate again
      \State $v \gets min(dist - s, a_{max})$
    \Else
      % reached target
      \State $break$  \algorithmiccomment{Terminate execution}
      \EndIf
      \EndIf
  \State $s \gets s + v$
\EndWhile
\end{algorithmic}
\caption{Motor Control Algorithm.\label{alg-motor}}
\end{algorithm}

To deploy the Algorithm~\ref{alg-motor} to REDFIN, it has been manually
implemented in REDFIN assembly. The resulting assembly program comprises 85 lines of
code and closely mirrors the high-level pseudocode. Fig.~\ref{fig-sym-tree} shows
a fragment of the program's symbolic execution tree that corresponds to the
decision whether to accelerate, keep the velocity, or decelerate the motor.

The decision is performed by computing the resulting total distance
travelled from start to stop, based on the action taken in the current
time step.  First, the total distance is computed if the motor were to
accelerate for one more time step and then decelerate in the
subsequent time steps.  If the computed total distance is less than or
equal to $dist$, the decision to accelerate is committed.  Otherwise,
the algorithm checks whether the targeted distance can be met by
maintaining the current velocity for one more time step.  If even that
would cause an overshoot, the decision for immediately commencing
deceleration is taken. Fig.~\ref{fig-motor} illustrates this decision process by
plotting the velocity and distance over time for a specific simulation
run.

\subsection{Loop Invariant Verification}

In order to ensure that the motor will not introduce disturbances
and will not lead the whole unit out of its normal mode of operation, the velocity and
acceleration of the motor must be kept within safe limits. This verification condition
is motivated by the correctness requirements of the whole space satellite unit.

More formally, the verification condition means
that at any iteration $t$ of the loop the values of the
expressions $v^t$, velocity, and $\left| v^t_{next} - v^t \right|$,
acceleration, must never exceed the parameters $v_{max}$ and $a_{max}$, respectively.
This property is the loop invariant for the motor control program which
ensures that velocity and acceleration always stay within their safe bounds. We
formalise it as the following predicate that universally quantifies over the
program's inputs and the loop's state:

\vspace{1mm}
\begin{figure}[H]
\begin{tcolorbox}
\LARGE{
\[
  \forall\ v_{max}\ a_{max}\ t\ v^t\ v^t_{next},\ v^t \leq v_{max} \land \left| v^t_{next} - v^t \right| \leq a_{max}
\]}
\end{tcolorbox}
\caption{Motor control loop invariant.\label{fig-loop-invariant}}
\end{figure}

\noindent
We will verify the loop invariant by using the verification framework in the
\emph{branching} mode~\S\ref{sec-transformer}. While symbolic execution with
merging, which is implemented by the framework's \emph{merging} mode, allows
for intuitive formulation of properties for whole-program verification and
is very useful for verifying finite programs, as we have
reported in the section~\S\ref{sec-verification}, in the presence of branches
guarded by symbolic values, it suffers from \emph{symbolic non-termination};
thus, for verifying the loop invariant, we rely on the branching mode of
symbolic execution.

\begin{figure}
\centerline{\includegraphics[scale=0.50]{fig/motor_control_graph.pdf}}
  % \vspace{-13mm}
\caption{Velocity ($v$) and distance travelled ($s$) plotted against time ($t$)\label{fig-motor}}
\Description[Please consult the text of this section for the description]{}
\end{figure}

\noindent
We take the following generic approach:

\begin{itemize}
  \item Obtain the binary tree-shaped trace by symbolic execution in~\emph{branching} mode.
  \item Split the trace into linear paths, thus enumerating all possible
    execution scenarios.
  \item For every path perform the analysis:
    \begin{itemize}
      \item extract the relevant parts of the state from the~\emph{last} node in
        the path, i.e. symbolic expressions stored representing $s$,
        $v$ and $v_{next}$;
      \item extract the terminal~\emph{path condition} $\phi$
            from the~\emph{last} node in the path;
      \item construct a symbolic expression representing the~\emph{verification condition}
            $\psi$ (Fig.~\ref{fig-loop-invariant});
      \item verify the property in the given path by checking the following
        formula for satisfiability: $\phi \land \lnot\psi$, i.e. the path condition
        conjoined with negated verification condition.

    \end{itemize}
    \item  The property holds if and only if for every path the solver returns
           \textsc{Unsat}, i.e. there are no assignments of the variables which
           satisfy the~\emph{negation} of the property to check, considering the
           terminal path condition.
\end{itemize}

As illustrated by Fig.~\ref{fig-sym-tree}, every conditional jump instruction produces
two branches in the symbolic execution tree: the one
where the current path condition is conjoined with the jump's guard and the one where it is
conjoined with the guard's negation. However, if the resulting
conjunction is unsatisfiable, the corresponding branch need not to be explored
and can be safely pruned.
Thus the symbolic execution engine needs to call an SMT solver every
time a conditional jump is encountered to check if the path conditions of the branches
are satisfiable.

\begin{figure}
\centerline{\includegraphics[scale=0.4]{fig/sym-tree.pdf}}
\caption{Symbolic execution tree of a code fragment with conditional branching.\label{fig-sym-tree}}
\Description[Please consult the text of this section for the description]{}
\end{figure}

Checking satisfiability of path conditions is essential for mitigating the path explosion
problem. Precondition, when available, is assigned as the initial path condition and thus
will become a subterm of every formula submitted to the solver.
By identifying strong preconditions, we can drastically reduce the number of
satisfiable paths in the symbolic execution tree of a program that is being
verified, thereby significantly shortening verification times.

With the branch pruning optimisation in place and the parameters restricted
to $dist \in \mathopen[1, 200\mathclose]$, $v_{max} \in \mathopen[1, 30\mathclose]$ and
$a_{max} \in \mathopen[1, 30\mathclose]$, the verification of the loop invariant
takes around 40 minutes.


\newpage
\section{Center of Gravity Computation}

The Center of Gravity of the experiment has been calculated considering all the components' mass listed in Section \ref{components}.

\subsection{Code}

\lstinputlisting[language=Matlab]{appendix/code/centerofgravity/TUBULAR_COG.m}


\section{A Tutorial on Spectral Clustering}
\label{ch:ulrike07}

\textit{A Tutorial on Spectral Clustering} by Ulrike Von Luxburg. Professor of Computer Science, focus on ML and computational statistics, University of Hamburg.\\
Cited by 2597. \textit{Statistics and computing, 2007}.
\newline

\textbf{Main point} is that \begin{inparaenum}[\itshape a\upshape)]
\item spectral clustering obtains high-quality solution to general clustering problem.
\item graph Laplacian has important properties.
\item three different perspectives  are given as motivation and explanation: a graph cut point of view, a random walks point of view, and a pertubation theory point of view.
\end{inparaenum}

\subsection{graph Laplacian}
Spectral clustering algorithms attemp to find $k$ subsets of vertices which have the property that vertices within a cluster are similar to each other than to vertices in other clusters. In order to find sets, spectral clustering makes the unnormalized Laplacian $n \times n$ matrix $L$.

$L = D - W$ where $D$ is the diagonal degree matrix $d_{ii}$ is the detree of vertex $i$ defined as $d_{ii} = \sum_{j=1}^n$ and $W$ is the weighted adjacency matrix, such that $W_{ii} = 0$ and $w_{ij}$ is the weight of the edge between $v_i \in V$ and $v_j \in V$.

Note that we are assuming that the graph is undirect which makes $W$ and $L$ symmetric and the weight of an edge is a non-negative measure of similiarity between the two vertices. Higher weights imply greater similarity.

\subsubsection{Unnormalized $L$}
\begin{figure}[ht]
\begin{mdframed}
\begin{description}
\item[Property 1] \hfill \\
for every vector $f \in R^n$, we have $f^T L f = \frac{1}{2} \sum_{i,j=1}^{n} w_{i,j}(f_i - f_j)^2$.
\item[Property 2] \hfill \\
$L$ is symmetric and positive-definite.
\item[Property 3] \hfill \\
The smallest eigenvalue of $L$ is 0, and the corresponding eigenvector is the constant eigenvector 1.
\item[Property 4] \hfill \\
$L$ has $n$ non-negative, real-valued eigenvalues are $0 = \lambda_1 \leq \lambda_2 \leq \cdots \leq  \lambda_n$.
\end{description}
\end{mdframed}
\caption{Unnormalized $L$}
\end{figure}

\subsubsection{Normalized $L$}
Here is the definition of $L_{sym}$ and $L_{rw}$. \\
$\bullet$$L_{sym} = D^{-\frac{1}{2}} L D^{-\frac{1}{2}} = I - D^{-\frac{1}{2}} W D^{-\frac{1}{2}}$ \\
$\bullet$$L_{rw} = D^{-1} L = I - D^{-1} W$

\begin{figure}[ht]
\begin{mdframed}
\begin{description}
\item[Property 1] \hfill \\
for every vector $f \in R^n$, we have $f^T L_{sym} f = \frac{1}{2} \sum_{i,j=1}^{n} w_{i,j}(\frac{f_i}{\sqrt{d_{ii}}} - \frac{f_j}{\sqrt{d_{jj}}})^2$.
\item[Property 2] \hfill \\
$\lambda$ is an eigenvalue of $L_{rw}$ with eigenvector $u$ if and only if $\lambda$ is an eigenvalue of $L_{sym}$ with eigenvector $w = D^{\frac{1}{2}} u$.
\item[Property 3] \hfill \\
$\lambda$ is an eigenvalue of $L_{rw}$ with eigenvector $u$ if and only if $u$ solve the gereralized eigenvalue problem $L u = \lambda D u$.
\item[Property 4] \hfill \\
0 is an eigenvalue of $L_{rw}$ with the constant vector 1 as eigenvector.\\
0 is an eigenvalue of $L_{sym}$ with eigenvector $D^{\frac{1}{2}} 1 $.
\item[Property 5] \hfill \\
$L_{sym}$ and $L_{rw}$ are positive semi-definite and have $n$ non-negative, real-valued eigenvalues are $0 = \lambda_1 \leq  \lambda_2 \leq  \cdots \leq  \lambda_n$.
\end{description}
\end{mdframed}
\caption{Normalized $L$}
\end{figure}

\subsection{Approaches}
\begin{description}
\item[Graph cut] \hfill \\
$\bullet$ Apporaximate solution by finding eigenvector. \\
$\bullet$ There is no guarantee on quality of the solution.
\item[Random walk approach] \hfill \\
$\bullet$ Minimizing normalized cut is equivalent to minimizing the probability that a random walk on the graph.
\item[Perturbation theory approach] \hfill \\
$\bullet$ The larger the gap between $\lambda_{k}$ and $\lambda_{k+1}$, the closer the piecewise-constant eigenvectors will be.
\end{description}

\newpage

\subsection{Spectral clustering}
Normalized and unnormalized spectral clustering is similar.

\begin{figure}[ht]
\begin{mdframed}
\begin{enumerate}
\item[Input] : Similarity matrix $S \in R^{n \times n}$, number $k$ of clusters. \\
\item[Step 1] : Construct the unnormalized graph Laplacian $L = D - W$, using the weighted $n \times n$ adjacency matrix $W$, where $w_{ij}$ is a function of $S_{ij}$ that gives non-negative weights. \\
\item[Step 2] : Find the $k$ eigenvectors $u_1, \cdots, u_k$ corresponding to the smallest $k$ eigenvalues of $L$ \\
\item[Step 3] : Let $U = R^{n \times k}$ be the matrix containing the eigenvectors $u_i$ as columns, and let $y_i \in R^k$ be the $i$th row of $U$.\\
\item[Step 4] : Cluster the points $(y_i)_{i=1,\cdots,n}$ in $R^k$ using $k$-means clustering into clusters $C_1,\cdots,C_k$.\\
\item[Output] : clusters $A_1, \cdots, A_k$ where $A_k - {v_j|y_j \in C_i}$
\end{enumerate}
\end{mdframed}
\caption{Unnormalized spectral clustering}
\end{figure}

\begin{figure}[ht]
\begin{mdframed}
\begin{enumerate}
\item[Input] : Similarity matrix $S \in R^{n \times n}$, number $k$ of clusters. \\
\item[Step 1] : Construct the unnormalized graph Laplacian $L = D - W$, using the weighted $n \times n$ adjacency matrix $W$, where $w_{ij}$ is a function of $S_{ij}$ that gives non-negative weights. \\
\item[Step 2] : Find the $k$ eigenvectors $u_1, \cdots, u_k$ corresponding to the smallest $k$ eigenvalues that solve the generalized eigenvector problem $L u = \lambda D u$ \\
\item[Step 3] : Let $U = R^{n \times k}$ be the matrix containing the eigenvectors $u_i$ as columns, and let $y_i \in R^k$ be the $i$th row of $U$.\\
\item[Step 4] : Cluster the points $(y_i)_{i=1,\cdots,n}$ in $R^k$ using $k$-means clustering into clusters $C_1,\cdots,C_k$.\\
\item[Output] : clusters $A_1, \cdots, A_k$ where $A_k - {v_j|y_j \in C_i}$
\end{enumerate}
\end{mdframed}
\caption{Normalized spectral clustering according to Shi}
\end{figure}

\begin{figure}[ht]
\begin{mdframed}
\begin{enumerate}
\item[Input] : Similarity matrix $S \in R^{n \times n}$, number $k$ of clusters. \\
\item[Step 1] : Construct the normalized graph Laplacian $L_{sym} = I - D^{-\frac{1}{2}} S D^{-\frac{1}{2}}$. \\
\item[Step 2] : Find the $k$ eigenvectors $u_1, \cdots, u_k$ corresponding to the largest $k$ eigenvalues of $L_{sym}$. \\
\item[Step 3] : Let $U = R^{n \times k}$ be the matrix containing the eigenvectors $u_i$ as columns, and let $y_i \in R^k$ be the $i$th row of $U$.\\
\item[Step 4] : Let $T$ be the row-normalized $U$ matrix where $t_{ij} = \frac{u_{ij}}{(\sum_k u_{ik}^2)^2}$ and let $y_i \in R^k$ be the $i$th row of $T$.\\
\item[Step 5] : Cluster the points $(y_i)_{i=1,\cdots,n}$ in $R^k$ into clusters $C_1,\cdots,C_k$ via $k$-means clustering or any other algorithm that attempts to minimize distortion. \\
\item[Output] : clusters $A_1, \cdots, A_k$ where $A_k - {v_j|y_j \in C_i}$
\end{enumerate}
\end{mdframed}
\caption{Normalized spectral clustering according to Ng}
\end{figure}


\documentclass{article}
\usepackage{amsmath}
%opening
\title{Diffusion equations for clines at migration-selection balance}
\author{Erin Calfee}

\begin{document}

\maketitle

\section{For addition to Graham's pop gen textbook..}

Let $s(x)$ describe the selection coefficient over one-dimensional space, such at that allele 1 has fitness 1 and allele 2 has fitness $1+ s(x)$ at location $x$. Consider the change in frequency of allele 2, $\Delta q(x)$, at location $x$ in one short time step $\Delta t$. \\
We can calculate the change in allele frequency by adding the change due to migration and the change due to selection. This is an approximation supposing migration and selection happen simultaneously and independently, which is sufficiently accurate as long as selection is weak, such that we don't have to worry about the contribution of migrants who should have died from selection (i.e. $ms(x)$ is small):
\begin{align*}
\frac{\Delta q(x)}{\Delta t} = \frac{\Delta q_{m}(x)}{\Delta t} + \frac{\Delta q_{s}(x)}{\Delta t}
\end{align*}

We'll start with the contribution from migration. Assume $\Delta t$ is small enough that individuals can only disperse a short distance $\Delta x$ in this time. Let $m$ be the probability an individual migrates distance $\Delta x$, with equal probability to the left or right, and $1-m$ be the probability an individual doesn't migrate in $\Delta t$:

\begin{align*}
\frac{\Delta q_{m}(x)}{\Delta t} & = (1-m)q(x) + m(\frac{1}{2}q(x + \Delta x) + \frac{1}{2}q(x - \Delta x)) - q(x) \\
& = q(x) - q(x) + \frac{m}{2}\left(q(x + \Delta x) + q(x - \Delta x) - 2q(x)\right) \\
& = \frac{m}{2}\left((q(x + \Delta x) - q(x)) - (q(x) - q(x - \Delta x))\right)
\end{align*}
Somehow I'm missing a $\Delta x^{2}$ in the denominator. \newline
For the contribution of selection, we use the approximation that selection does not strongly distort mean population fitness, i.e. $\overline{w} \approx 1$:
\begin{align*}
\frac{\Delta q_{s}(x)}{\Delta t} = (1+s(x))(1-q(x))q(x)
\end{align*}
Summing these terms together and taking the limit as $\Delta x$ and $\Delta t$ go to zero, we get the classic diffusion equation for the description of a cline:
\begin{align}
\frac{dq(x)}{dt} = \frac{m}{2}\frac{d^{2}q(x)}{dx^2} + s(x)q(x)(1-q(x))
\end{align}
It could be that the cline is dynamic and one allele is on the path to fixation as it diffuses across space, but for the equilibrium case at migration-selection balance, $\frac{dq(x)}{dt} = 0$. If we make assumptions about the way selection pressures are distributed across the environment (e.g. a discrete environmental boundary where selection is $-s$ to the left of $x=0$ and $+s$ to the right), we can estimate the strength of selection $s$ by plugging in empirically-estimated migration rates, where $m$ is the variance of individual dispersal distances.
\end{document}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Copyright (c) 2003-2018 by The University of Queensland
% http://www.uq.edu.au
%
% Primary Business: Queensland, Australia
% Licensed under the Apache License, version 2.0
% http://www.apache.org/licenses/LICENSE-2.0
%
% Development until 2012 by Earth Systems Science Computational Center (ESSCC)
% Development 2012-2013 by School of Earth Sciences
% Development from 2014 by Centre for Geoscience Computing (GeoComp)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%!TEX root = user.tex
\chapter{The escript symbolic toolbox}
\label{CHAP:Symbolic}
\section{Introduction}
\escript builds on the existing Sympy~\cite{Sympy} symbolic maths library to
provide a \SYMBOL class with support for \escript Data objects. \SYMBOL objects
act as placeholders for a single mathematical symbol, such as \texttt{x}, or
for arbitrarily complex mathematical expressions such as
\texttt{c*x**4 + alpha*exp(x) - 2*sin(beta * x)}, where \texttt{alpha},
\texttt{beta}, \texttt{c}, and \texttt{x} are also symbols (the symbolic
``atoms" of the expression).
With the help of the \EVALUATOR class, these symbols and expressions can
be evaluated by substituting numeric values and/or \escript \Data objects
for the atoms. Escript's \SYMBOL class has a shape (and thus a rank) as well
as a dimensionality.
Symbols are useful to perform mathematical simplifications, compute
derivatives, take gradients and in the case of \escript describe PDEs.
As an example of how the symbolic toolbox can be used, consider the following
code extract.
\begin{python}
import esys.escript as es
u = es.Symbol('u')
p = 2*u**2 + 3*u + 1
p2 = es.sin(u)
p3 = p.diff(u)              # p3 = derivative of p with respect to u
evalu = es.Evaluator()
evalu.addExpression(p)
evalu.addExpression(p2)
evalu.addExpression(p3)
evalu.subs(u=2*es.symconstants.pi)
evaluated=evalu.evaluate()
print("p3 =", p3)            # The symbols can be printed, this line will print p3.
print(evaluated)
\end{python}
Running this code outputs:\\\texttt{p3 = 4*u + 3\\(1 + 6*pi + 8*pi**2, 0, 3 + 8*pi)}.\\
To get the numeric value of the expression we replace\\
\texttt{evalu.evaluate()}
\\with\\
\texttt{evalu.evaluate(evalf=True)}.\\
This results in
\texttt{(98.806, 0, 28.132)}.
The use of these Symbols becomes more interesting in the context of escript
when they are integrated with escript \Data objects.

\pagebreak
\section{NonlinearPDE}
The \NLPDE class in escript makes use of the escript \SYMBOL class and allows
for the solution of PDEs of the form:
\begin{equation}
-X_{ij,j} + Y_i = 0
\label{symbolic eq1}
\end{equation}
where $X$ and $Y$ are both functions of $u_{k,j}$ and $u_k$, and $u$ is the
unknown function implemented as a \SYMBOL. $\nabla\cdotp x$ denotes divergence
of $x$.
The \NLPDE class uses the \SYMBOL class to solve the nonlinear PDE given in
\eqn{symbolic eq1}.
The class incorporates Newton's method to find the zeroes of the left hand side
of \eqn{symbolic eq1} and as a consequence finding the $X$ and $Y$ which
satisfy \eqn{symbolic eq1}.
Consecutive updates are calculated until the equation is satisfied to the
desired level of accuracy. The solution to each update step involves solving a
linear PDE. The \NLPDE class uses $X$ and $Y$ to produce the coefficients of
the linear PDE for the update step. The linear PDE class given in
\Sec{SEC LinearPDE} is used to solve the linear PDEs from the update step.
The coefficients of the linear PDE to be solved are calculated as follows:
\begin{equation*}
 A_{ijkl} = \frac{\partial \text{X}_{ij}}{\partial u_{k,l}}, B_{ijkl} = \frac{\partial \text{X}_{ij}}{\partial u_{k}}, C_{ijkl} = \frac{\partial \text{Y}_{ij}}{\partial u_{k,l}}, D_{ijkl} = \frac{\partial \text{Y}}{\partial u_{k}}
\end{equation*}
\section{2D Plane Strain Problem}
The \NLPDE class can be used to solve a 2D plane strain problem. In continuous
media, stress is given by Lam\'e's \eqn{symbolic eq2}.
\begin{equation}
-\sigma_{ij,j}=f
\label{symbolic eq2}
\end{equation}
Hook's Law provides a relation between $\sigma$ and $\epsilon$ in the following form
\begin{equation}
\left[ \begin{array}{c}
\sigma_{00} \\
\sigma_{11} \\
\sigma_{01} \\
\end{array} \right] =
\left[ \begin{array}{ccc}
c_{00} & c_{01} & c_{05}\\
c_{01} & c_{11} & c_{15}\\
c_{05} & c_{15} & c_{55}\\
\end{array}\right]
\left[ \begin{array}{c}
\epsilon_{00} \\
\epsilon_{11} \\
2\epsilon_{10} \\
\end{array} \right]
\label{symbolic eq3}
\end{equation}
Where $\epsilon = symmetric(grad(u)) \text{ or } %
\epsilon_{ij}=\frac{1}{2}\left(\frac{\partial u_i}{\partial x_j} + %
{\frac{\partial u_j}{\partial x_i}}\right)$, $u$ is the unknown function and
$c_{ij}$ is the stiffness matrix. To fit this to the nonlinear PDE class'
standard form, X is set to $C \times symmetric(grad(u))$.
The following \PYTHON extract shows how an example 2D plane strain problem can
be set up.

\begin{python}
from esys.escript import *
from esys.finley import Rectangle
#set up domain and symbols
mydomain = Rectangle(l0=1.,l1=1.,n0=10, n1=10)
u = Symbol('u',(2,), dim=2)
q = Symbol('q', (2,2))
sigma = Symbol('sigma',(2,2))
theta = Symbol('theta')
# q is a rotation matrix represented by a Symbol. Values can be substituted for
# theta.
q[0,0]=cos(theta)
q[0,1]=-sin(theta)
q[1,0]=sin(theta)
q[1,1]=cos(theta)
# Theta gets substituted by pi/4 and masked to lie between .3 and .7 in the
# vertical direction. Using this masking means that when q is used it will apply
# only to the specified area of the domain.
x = Function(mydomain).getX()
q=q.subs(theta,(symconstants.pi/4)*whereNonNegative(x[1]-.30)*
    whereNegative(x[1]-.70))
# epsilon is defined in terms of u and has the rotation applied.
epsilon0 = symmetric(grad(u))
epsilon = matrixmult(matrixmult(q,epsilon0),q.transpose(1))
# For the purposes of demonstration, an arbitrary c with isotropic constraints
# is chosen here. In order to act as an isotropic material c is chosen such that
# c00 = c11 = c01+c1+2*c55
c00 = 10
c01 = 8; c11 = 10
c05 = 0; c15 = 0; c55 = 1
# sigma is defined in terms of epsilon
sigma[0,0] = c00*epsilon[0,0]+c01*epsilon[1,1]+c05*2*epsilon[1,0]
sigma[1,1] = c01*epsilon[0,0]+c11*epsilon[1,1]+c15*2*epsilon[1,0]
sigma[0,1] = c05*epsilon[0,0]+c15*epsilon[1,1]+c55*2*epsilon[1,0]
sigma[1,0] = sigma[0,1]
sigma0=matrixmult(matrixmult(q.transpose(1),sigma),q)
# set up boundary conditions
x=mydomain.getX()
gammaD=whereZero(x[1])*[1,1]
yconstraint = FunctionOnBoundary(mydomain).getX()[1]
# The nonlinear PDE is set up, the values are substituted in and the solution is
# calculated, y represents an external shearing force acting on the domain.
# In this case a force of magnitude 50 is acting in the x[0] direction.
p = NonlinearPDE(mydomain, u, debug=NonlinearPDE.DEBUG0)
p.setValue(X=sigma0,q=gammaD,y=[-50,0]*whereZero(yconstraint-1),r=[1,1])
v = p.getSolution(u=[0,0])
\end{python}
%\pagebreak
The way in which the rotation matrix q is set up demonstrates the seamless integration of escript symbols and \Data objects. A \SYMBOL is used to set up the matrix, the values for theta are then later substituted in. The example also demonstrates how the symbolic toolbox can be used as an aid to easily move from a mathematical equation to an escript data object which can be used to do numerical calculations.
Running the script calculates the unknown function u and assigns it to v. We can use v to calculate the stress and strain.
\begin{table}[!h]
\centering
\begin{tabular}{|c|c|c|}
  \hline
   & Anisotropic & Isotropic\\%\multicolumn{2}{|c|}{Isotopic} \\
  \hline
  No rotation & \includegraphics[scale=0.2]{0RotAniso} & \includegraphics[scale=0.2]{0RotIso}\\
  \hline
  60\textdegree\ rotation & \includegraphics[scale=0.2]{MidRotAniso} & \includegraphics[scale=0.2]{MidRotIso}\\
  \hline
  difference & \includegraphics[scale=0.2]{diffaniso} & \raisebox{2cm}{no difference}\\
  \hline
\end{tabular}
\caption{Displacement vectors calculated using \NLPDE}
\label{isovsaniso}
\end{table}
Table \ref{isovsaniso}, shows the result of running the above script under varying values of c and theta. Both isotropic and anisotropic cases are considered.  For the anisotropic case, $c_{ij}$ is chosen such that
$c00 = c11 = c01+c1+2*c55$ does not hold.
Two values of theta are also considered; one with a masked 60\textdegree
rotation in the middle and one with no rotation.
The last row of the table shows the difference between rotation in the middle
and no rotation. In the isotropic case it can be seen that there is no
difference in the output when the rotation is applied.
There is however, an obvious difference when the anisotropic case is considered.

\section{Classes}
A number of classes are associated with escript symbols. A detailed listing of the definitions and usage is provided below.
\subsection{Symbol class}
\begin{classdesc}{Symbol}{symbol \optional{, shape} \optional{, Dim}}
Defines a \SYMBOL object. The first argument \member{symbol} is a string given
to represent the \SYMBOL. The string typically matches the name of the object,
for instance \texttt{u=Symbol('u')}. Next optional \member{shape} argument
defines whether the \SYMBOL is a scalar, vector, matrix, or tensor and the
length or size of it. \member{dim} is used to define the dimensionality of the
object contained in the \SYMBOL.
For a \SYMBOL definition \texttt{u = Symbol('u',(10,), dim=2)}, the value of u
will be a vector with 10 components and the domain on which u will be used is
2-dimensional (this is relevant with operations such as \texttt{grad} where the
number of spatial dimensions is important).
\end{classdesc}
\subsubsection{Symbol class methods}
\begin{methoddesc}[Symbol]{atoms}{\optional{types}}
Returns the atoms that form the current \SYMBOL.
By default, only objects that are truly atomic and cannot be divided into
smaller pieces are returned: symbols, numbers, and number symbols like $e$ and
$pi$. It is possible to request atoms of any type via the \member{types}
argument, however.
\end{methoddesc}

\begin{methoddesc}[Symbol]{coeff}{x \optional{, expand=true}}
Returns the coefficient of the term \texttt{x} or $0$ if there is no \texttt{x}.
If \texttt{x} is a scalar \SYMBOL then \texttt{x} is searched in all components
of this \SYMBOL. Otherwise the shapes must match and the coefficients are
checked component by component. For example:
\begin{python}
     x=Symbol('x', (2,2))
     y=3*x
     print y.coeff(x)
     print y.coeff(x[1,1])
\end{python}
will print:
\begin{python}
     [[3 3]
      [3 3]]
     [[0 0]
      [0 3]]
\end{python}
\end{methoddesc}
\begin{methoddesc}[Symbol]{diff}{symbols}
Takes the derivative of the \SYMBOL object of which the method is called with
respect to the symbols specified in the argument \member{symbols}.
\end{methoddesc}
\begin{methoddesc}[Symbol]{evalf}{}
Applies the \texttt{sympy.evalf} operation on all elements of the \SYMBOL which
are of type or inherit from \texttt{sympy.Basic}.
\end{methoddesc}
\begin{methoddesc}[Symbol]{expand}{}
Applies the \texttt{sympy.expand} operation on all elements in this \SYMBOL.
\end{methoddesc}
%\begin{methoddesc}[Symbol]{getDataSubstitutions}{}
%end{methoddesc}
\begin{methoddesc}[Symbol]{getDim}{}
Returns the \SYMBOL's spatial dimensionality, or -1 if undefined.
\end{methoddesc}
\begin{methoddesc}[Symbol]{getRank}{}
Returns the \SYMBOL's rank which is equal to the length of the shape.
\end{methoddesc}
\begin{methoddesc}[Symbol]{getShape}{}
Returns the shape of this \SYMBOL.
\end{methoddesc}
\begin{methoddesc}[Symbol]{grad}{\optional{where=none}}
Returns the gradient of this \SYMBOL. The \SYMBOL must have a dimensionality
defined in order for \texttt{grad} to work. As with the normal escript
\texttt{grad} function a \FunctionSpace can be specified using the
\member{where} argument. The \FunctionSpace should be wrapped in a \SYMBOL.
To do this, set up a \SYMBOL and then use the \texttt{subs} function to
substitute in the \FunctionSpace.
\end{methoddesc}
\begin{methoddesc}[Symbol]{inverse}{}
Find the inverse of the \SYMBOL to which the function is applied. Inverse is only valid for square rank 2 symbols.
\end{methoddesc}
%\begin{methoddesc}[Symbol]{item}{}
%\end{methoddesc}
%\begin{methoddesc}[Symbol]{lambdarepr}{}
%test
%\end{methoddesc}
\begin{methoddesc}[Symbol]{simplify}{}
Applies the \texttt{sympy.simplify} operation on all elements in this \SYMBOL.
\end{methoddesc}
\begin{methoddesc}[Symbol]{subs}{old, new}
Substitutes or replaces a \SYMBOL specified in \member{old} with whatever is in
\member{new} for this \SYMBOL. Consider:
  \begin{python}
     import esys.escript as es
     u=es.Symbol("u")
     expr=2*u
     expr.subs(u,2)
\end{python}
This prints 4.
\end{methoddesc}
\begin{methoddesc}[Symbol]{trace}{axis_offset}
Returns the trace of the \SYMBOL object.
\end{methoddesc}

\subsection{Evaluator class}
The \EVALUATOR class is intended to have a group of expressions added to it,
substitutions can be made across all expressions and the expressions can then
all be evaluated.
\subsubsection{Evaluator class methods}
\begin{classdesc}{Evaluator}{\optional{expressions}}
An \EVALUATOR object is initiated via \texttt{Evaluator()} with an optional
argument of expressions to store.
\end{classdesc}
\begin{methoddesc}[Evaluator]{addExpression}{expression}
Adds an expression to this \EVALUATOR.
\end{methoddesc}
\begin{methoddesc}[Evaluator]{evaluate}{\optional{evalf=False}\optional{, args}}
Evaluates all expressions in this \EVALUATOR and returns the result as a tuple.
\member{evalf} can be set to \texttt{True} to call \texttt{evalf} on any sympy
symbols which may be part of the expression.
\member{args} can be provided to make any substitutions before the expression
is evaluated.
\end{methoddesc}
\begin{methoddesc}[Evaluator]{subs}{old,new}
Substitutes or replaces a \SYMBOL specified in \member{old} with whatever is
in \member{new} for all expressions in the \EVALUATOR.
\end{methoddesc}

\subsection{NonlinearPDE class}
\begin{classdesc}{NonlinearPDE}{domain, u}
Defines a general nonlinear, steady, second order PDE for an unknown function
\var{u} on a given domain defined through a \Domain object \var{domain}.
\var{u} is a \SYMBOL object.
The general form is -div(X) + Y = 0
\end{classdesc}
\iffalse
\begin{methoddesc}[NonlinearPDE]{concatenateRow}{}
test
\end{methoddesc}
\begin{methoddesc}[NonlinearPDE]{createCoefficient}{}
test
\end{methoddesc}
\begin{methoddesc}[NonlinearPDE]{getUnknownSymbol}{}
test
\end{methoddesc}
\begin{methoddesc}[NonlinearPDE]{getLinearSolverOptions}{}
test
\end{methoddesc}
\begin{methoddesc}[NonlinearPDE]{getLinearPDE}{}
test
\end{methoddesc}
\begin{methoddesc}[NonlinearPDE]{getNumSolutions}{}
test
\end{methoddesc}
\begin{methoddesc}[NonlinearPDE]{getShapeOfCoefficient}{}
test
\end{methoddesc}
\begin{methoddesc}[NonlinearPDE]{getCoefficient}{}
test
\end{methoddesc}
\begin{methoddesc}[NonlinearPDE]{getSensitivity}{}
test
\end{methoddesc}
\fi
\begin{methoddesc}[NonlinearPDE]{getSolution}{subs}
Returns the solution of the PDE. \member{subs} contatins the substitutions for
all symbols used in the coefficients including the initial value for the
unknown \texttt{u}.
\end{methoddesc}
\begin{methoddesc}[NonlinearPDE]{setOptions}{opts}
\begin{verbatim}
  Allows setting options for the nonlinear PDE.
  The supported options are:
    tolerance
        error tolerance for the Newton method
    iteration_steps_max
        maximum number of Newton iterations
    omega_min
        minimum relaxation factor
    atol
        solution norms less than atol are assumed to be atol.
        This can be useful if one of your solutions is expected to
        be zero.
    quadratic_convergence_limit
        if the norm of the Newton-Raphson correction is reduced by
        less than quadratic_convergence_limit between two iteration
        steps, quadratic convergence is assumed.
    simplified_newton_limit
        if the norm of the defect is reduced by less than
        simplified_newton_limit between two iteration steps and
        quadratic convergence is detected, the iteration switches to the
        simplified Newton-Raphson scheme.
\end{verbatim}

\end{methoddesc}
\begin{methoddesc}[NonlinearPDE]{setValue}{
\optional{X}\optional{, Y}
\optional{, y}
\optional{, q}\optional{, r}}
Assigns new values to coefficients. By default all values are assumed to be
zero\footnote{In fact, it is assumed they are not present by assigning the
value \code{escript.Data()}. This can be used by the solver library to reduce
computational costs.}.
If the new coefficient value is not a \Data object, it is converted into a
\Data object in the appropriate \FunctionSpace.
\end{methoddesc}

\subsection{Symconsts class}
Symconsts provides symbolic constants for use in symbolic expressions. These constants are preferred to floating point implementation as they can cancel perfectly when mathematical expressions are evaluated, avoiding numerical imprecision.
\begin{verbatim}
usage:
  symconsts.pi this provides a Symbol object

Available constants currently include:
  pi and e
\end{verbatim}


\subsubsection{Quantization}

\begin{frame}{Quantization}
    Quantization based on \textit{uWave} \cite{liu2009uwave}

    \begin{block}{Compressing}
        \begin{itemize}
            \item Recorded acceleration data were compressed to an average value for a window size of 50
                ms and a step length of 30 ms
        \end{itemize}
    \end{block}

    \begin{block}{Conversion}
        \begin{itemize}
            \item The compressed records are converted into 33 different levels
            \begin{center}
                \tiny
                \begin{tabular}{ll}
                    \textbf{Acceleration data ($a$) in $\frac{dm}{s^2}$} & \textbf{Converted value}\\
                    \hline
                    $a > 200$ & 16\\
                    $100 < a < 200$ & 11 to 15 (five levels linearly)\\
                    $0 < a < 100$ & 1 to 10 (ten levels linearly)\\
                    $a = 0$ & 0\\
                    $-100 < a < 0$ & -1 to - 10 (ten levels linearly)\\
                    $-200 < a < -100$ & -11 to - 15 (five levels linearly)\\
                    $a < -200$ & -16
                \end{tabular}
            \end{center}
        \end{itemize}
    \end{block}
\end{frame}

\begin{frame}{Quantization}{Example - raw acceleration data}
    \begin{center}
        \begin{tikzpicture}
            \pgfplotsset{every axis legend/.append style={
        		at={(0.5,1.03)},
        		anchor=south}}
            \begin{axis}[
                xmin=1,
                xmax=295,
                xlabel=time,
                ylabel=acceleration in $\frac{dm}{s^2}$,
                legend columns=4]
                \addplot[blue, ultra thick, mark=none] table[x=t, y=x] {../data/fig/quantization/raw.dat};
                \addlegendentry{x-axis}
                \addplot[red, ultra thick, mark=none] table[x=t, y=y] {../data/fig/quantization/raw.dat};
                \addlegendentry{y-axis}
                \addplot[green, ultra thick, mark=none] table[x=t, y=z] {../data/fig/quantization/raw.dat};
                \addlegendentry{z-axis}
            \end{axis}
        \end{tikzpicture}
    \end{center}
\end{frame}

\begin{frame}{Quantization}{Example - compressed acceleration data}
    \begin{center}
        \begin{tikzpicture}
            \pgfplotsset{every axis legend/.append style={
                at={(0.5,1.03)},
                anchor=south}}
            \begin{axis}[
                xmin=1,
                xmax=52,
                xlabel=time,
                ylabel=acceleration in $\frac{dm}{s^2}$,
                legend columns=4]
                \addplot[blue, ultra thick, mark=none] table[x=t, y=x] {../data/fig/quantization/compressed.dat};
                \addlegendentry{x-axis}
                \addplot[red, ultra thick, mark=none] table[x=t, y=y] {../data/fig/quantization/compressed.dat};
                \addlegendentry{y-axis}
                \addplot[green, ultra thick, mark=none] table[x=t, y=z] {../data/fig/quantization/compressed.dat};
                \addlegendentry{z-axis}
            \end{axis}
        \end{tikzpicture}
    \end{center}
\end{frame}

\begin{frame}{Quantization}{Example - compressed \& converted acceleration data}
    \begin{center}
        \begin{tikzpicture}
            \pgfplotsset{every axis legend/.append style={
                at={(0.5,1.03)},
                anchor=south}}
            \begin{axis}[
                xmin=1,
                xmax=52,
                xlabel=time,
                ylabel=converted acceleration,
                legend columns=4]
                \addplot[blue, ultra thick, mark=none] table[x=t, y=x] {../data/fig/quantization/converted.dat};
                \addlegendentry{x-axis}
                \addplot[red, ultra thick, mark=none] table[x=t, y=y] {../data/fig/quantization/converted.dat};
                \addlegendentry{y-axis}
                \addplot[green, ultra thick, mark=none] table[x=t, y=z] {../data/fig/quantization/converted.dat};
                \addlegendentry{z-axis}
            \end{axis}
        \end{tikzpicture}
    \end{center}
\end{frame}


\documentclass{beamer}

\input{../../shared_slides.tex}

% reference: https://indico.cern.ch/event/845380/attachments/1915103/3241592/Dvurechensky_lectures.pdf
% https://arxiv.org/abs/1803.00567

% Use this aswell
% https://www.math.ucdavis.edu/~qlxia/Research/monge.pdf

\title{Optimal Transport}
\date{\today}

\begin{document}
\maketitle
\frame{\tableofcontents}

\section{Monge Problem}%
\label{sec:}

\begin{frame}
  \frametitle{The Monge Problem (1781)}

  \begin{minipage}{0.5\textwidth}
    \begin{figure}[ht]
      \centering
      \includegraphics[width=\textwidth]{monge-sand.png}
      \caption{How to best move piles of sand to fill up holes of the same total volume?\label{fig:label} }
    \end{figure}
  \end{minipage}
  \hfill
  \begin{minipage}{0.45\textwidth}
    \begin{itemize}
      \item $X,Y$ are metric spaces
      \item $\B$ is a Borel $\sigma$-Algebra (open sets, countable union, \dots)
      \item $\mu$ is a probability measure
    \end{itemize}
  Given a cost $c: X\times Y \to \R_+$ find a \textbf{transport map} $T: X \to Y$ minimizing
  \begin{equation}
    M(\mu, \nu) = \inf_T \int_X c(x, Tx) \diff \mu(x)
  \end{equation}
  s.t.\ the \emph{mass} remains the same:
  \begin{equation}
    \mu(T^{-1}(B)) = \nu(B) \quad \forall B \in \B.
  \end{equation}
  \end{minipage}
\end{frame}

\begin{frame}
  \frametitle{Drawbacks of Monge formulation}
  \begin{block}{Example}
    $X=(x_1, \dots, x_m)$ and $Y=(y_1, \dots, y_n)$ and
    $\mu= \frac{1}{m}(\delta_{x_1}+ \dots + \delta_{x_m})$ and $\nu= \frac{1}{n}(\delta_{y_1}+ \dots + \delta_{y_m})$
  \end{block}
  \begin{minipage}{0.5\textwidth}
    \underline{If $m=n$}, then $c(\cdot, \cdot) = C \in \R^{m\times n}$ is just a (square) matrix and $T$ is a permutation $\sigma$
    \begin{equation}
      \begin{aligned}
        M(\mu, \nu) &= \min_T \frac{1}{n} \sum_{i} c(x_i, T(x_i)) \\
        &= \min_\sigma \frac{1}{n} \sum_{i} c(x_i, y_{\sigma(i)}) \\
        &= \min_\sigma \frac{1}{n} \sum_{i} C_{i, \sigma(i)} \\
      \end{aligned}
    \end{equation}
  \end{minipage}
  \hfill
  \begin{minipage}{0.4\textwidth}
    \underline{If $m \neq n$}, for example $\mu=\delta_{x_1}$ and $\nu= \frac12(\delta_{y_1}+ \delta_{y_2})$
    $\Rightarrow$ No $T$ exists.
    \vspace{3.5cm}
    \vfill
  \end{minipage}
\end{frame}

\begin{frame}
  \frametitle{Nonuniqueness of solutions}
  \begin{figure}[ht]
    \centering
    \includegraphics[scale=0.3]{book-shifting.png}
    \caption{\label{fig:label} }
  \end{figure}
\end{frame}

\section{Kantorovich formulation}%
\label{sec:}

\begin{frame}
  \frametitle{Kantorovich's relaxation (1940s)}
  % we allow for splitting the mass coming from one point (can be spread out)
  Before: \emph{Transport map}, $\forall x$ move all amount to some $y$.\\
  Now: \emph{Transport plan}, $\forall (x,y)$ how much to move from $x$ to $y$.

  \begin{block}{}
    Find $\gamma \in \P(X \times Y)$
    \begin{equation}
      \begin{aligned}
        K(\mu, \mu) &= \inf_{\gamma} \int_{X \times Y} c(x,y) \diff \gamma(x,y) \\
        \text{s.t.} & \gamma(A \times Y) = \mu(A) \\
                    & \gamma(X \times B) = \nu(B)
      \end{aligned}
    \end{equation}
    % Linear in \gamma
    for all $A \in \B(X)$ and $B \in \B(Y)$.
    Note: constraints say that $\mu$ and $\nu$ are the marginals of $\gamma$, i.e.\ $\gamma \in \Pi(\mu, \nu)$.
  \end{block}

\end{frame}

\begin{frame}
  \frametitle{Kantorovich's relaxation}
  Allows for many more settings.
  \begin{figure}[ht]
    \centering
    \includegraphics[scale=0.2]{Kantorovich-relaxation.png}
    \caption{\label{fig:label} }
  \end{figure}
\end{frame}

\begin{frame}
  \frametitle{}
  \begin{block}{Example (as before)}
    $X=(x_1, \dots, x_m)$ and $Y=(y_1, \dots, y_n)$ and
    $\mu= \frac{1}{m}(\delta_{x_1}+ \cdots + \delta_{x_m})$ and $\nu= \frac{1}{n}(\delta_{y_1}+ \cdots + \delta_{y_m})$
  \end{block}
  The problem reduces to
  \begin{equation}
    \min_{\gamma} \sum_{i,j} C_{i,j} \gamma_{i,j},
  \end{equation}
  where again $C_{i,j}$ is the cost of moving mass from $x_i$ to $y_j$
  Then the space of probability measures on $X\times Y$ is just the set of matrices ${[\gamma_{i,j}]}_{i=1,\dots,m, j=1,\dots,n}$
  such that
  \begin{equation}
    \sum_{i=1}^{m} \gamma_{i,j} = \frac{1}{n} \quad \text{and} \quad \sum_{j=1}^{n} \gamma_{i,j} = \frac{1}{m}
  \end{equation}
  called \textbf{bistochastic matrices}.
\end{frame}

\begin{frame}
  \frametitle{Doubly stochastic matrices and the Birkhoff polytope}
  If $m=n$, then
  \begin{equation}
    \sum_{i=1}^{m} \gamma_{i,j} = \frac{1}{n} = \sum_{j=1}^{n} \gamma_{i,j}
  \end{equation}
  which called \textbf{bistochastic matrices} (scaled by \frac{1}{n}).\\
  The set of all such matrices is called the \textbf{Birkhoff polytope}.

  \onslide<2->{%
    \begin{block}{Vertices of the Birkhoff polytope}
      Are given by the permutation matrices.
    \end{block}
  }
  \onslide<3->{%
    By linearity
  \begin{equation}
    \begin{aligned}
    &\min_{\gamma \in \Pi} \sum_{i,j} C_{i,j} \gamma_{i,j}\\
    &= \frac{1}{n}\min_{\gamma \in Perm}\sum_{i,j} C_{i,j} \gamma_{i,j}
    &= \frac{1}{n} \min_{\sigma}\sum_{i,j} C_{i,\sigma(i)}
    \end{aligned}
  \end{equation}
  }
\end{frame}

\begin{frame}
  \frametitle{}
  \begin{block}{Example (with non-uniform distribution)}
    Now $\mu = p = (p_1, \dots, p_m)$ and $\nu = q = (q_1, \dots, q_n)$
  \end{block}
  \begin{equation}
    \begin{aligned}
      K(\mu, \nu) &= \min_\gamma \sum_{}^{} C_{i,j} \gamma_{i,j} \\
      s.t. & \begin{cases}
        \sum_{i}^{} \gamma_{i,j} = q_j \\
        \sum_{j}^{} \gamma_{i,j} = p_i \\
        \gamma_{i,j} \ge 0
      \end{cases}
    \end{aligned}
  \end{equation}
  Linear optimization problem.
\end{frame}

% Write here about wasserstein distance?
% \section{Wasserstein distance}%
% \label{sec:}

\end{document}


\documentclass{article}
\usepackage{../allan-eason}

\title{Sample Document using allan-eason.sty}
\author{Allan Pan and Eason Shao}
\date{\today}

% abcdefghijklmnopqrstuvwxyz
% zyxwvutsrqpomnlkjihgfedcba

\begin{document}
    \maketitle
    \newpage
    \tableofcontents
    \newpage

    \section{maths}
        \url{https://mathxstudio.github.io/} \\
        % \href{https://mathxstudio.github.io/}{Allan's website} \\
        \techlnk{https://mathxstudio.github.io/} \\
        \defword{https://mathxstudio.github.io/} \\
        https://mathxstudio.github.io/
        \subsection{test subsection}
        Let \(n\geq 3\) be a positive integer. Let \(C_1,C_2,\ldots,C_n\) be unit circles in the plane, with centres \(O_1,O_2,\ldots,O_n\) respectively. If no line meets more than two of the circles, prove that:
        \[\sum\limits_{1\leq i<j\leq n}\dfrac{1}{O_iO_j}\leq \dfrac{(n-1)\pi}{4}.\]

        For brevity, let \(d_{ij}\) be the length of \(O_{ij}\) and let \(\angle(ijk)\) be shorthand for \(\angle O_iO_jO_k\) (or its measure in radians).
        \\
        First, we eliminate the circles completely and reduce the problem to angles using the following \textbf{\color{allanred} Lemma}:
        \\
        \begin{allanlemma}
            For any indicies \(i,j,m\) we have the inequalities
            \[\angle(imj)\geq \max\left(\dfrac{2}{d_{mi}}, \dfrac{2}{d_{mj}}\right)\quad \mathrm{and} \quad \pi -\angle(imj)\geq \max\left(\dfrac{2}{d_{mi}}, \dfrac{2}{d_{mj}}\right)\]
            \sublemma
                We first prove the former line. Consider the altitude from \(O_i\) to \(O_mO_j\). The altitude must have length at least \(2\), otherwise its perpendicular bisector passes intersects all of \(C_i, C_m, C_j\). Thus
                \[2\leq d_{mi}\sin\angle(imj)\leq \angle(imj)\]
                proving the first line. The seconf line follows by considering the external angle formed by lines \(O_mO_i\) and \(O_mO_j\) instead of the internal one.\(\square\)
        \end{allanlemma}
        \showlemma
        \begin{allanlemma}
            another test lemma.
            \sublemma
                proof of lemma.
        \end{allanlemma}
        \showlemma
        \\
        Our idea now is for any index \(m\) we will make an estimate on \(\displaystyle \sum_{\substack{1\leq i\leq n\\i\neq b} }{\dfrac{1}{d_{bi}}} \) for each index \(b\). If the centers formed a convex polygon, this would be much simpler, but because we do not have this assumption some more care is needed.
        \begin{allanclaim}
            Suppose \(O_a,O_b,O_c\) are consecutive verticies of the convex hull. Then
            \[\dfrac{n-1}{n-2}\dang(abc)\geq \dfrac{2}{d_{1b}}+\dfrac{2}{d_{2b}}+\ldots+\dfrac{2}{d_{nb}}\]
            where the term \(\displaystyle \dfrac{2}{d_{bb}}\) does not appear (obviously).
            \subclaim
                WLOG let's suppose \((a,b,c)=(2,1,n)\) and that ...
        \end{allanclaim}
        another line of text...
        \begin{allanfact}
            Describe your fact.
            \subfact
                Describe proof.
        \end{allanfact}
        another line of text...
        \begin{allantheorem}[Test theorem]
            Here is a theorem. Here is a theorem. Here is a theorem. Here is a theorem. Here is a theorem. Here is a theorem.
        \end{allantheorem}
        ...
        \\
        Now suppose there were \(r\) verticies in the convex hull. If we sum the first claim across all \(b\) on the hull, and the second across all \(b\) not on the hull (inside it), we get
        $$
        \begin{aligned}
            \sum\limits_{1\leq i< j\leq n} \dfrac{2}{d_{ij}}
            &= \dfrac{1}{2} \sum\limits_{b} \sum\limits_{i\neq b} \dfrac{2}{d_{bi}}
            \\
            &\leq \dfrac{1}{2}\cdot\dfrac{n-1}{n-2} ((r-2)\pi + (n-2)\pi)
            \\
            &= \dfrac{(n-1)\pi}{4}
        \end{aligned}
        $$
        as needed (with \((r-2)\pi\) being the sum of all angles in the hull.

        \begin{allanenvremark}
            This is the sixth and last problem of IMO 2002, and is a difficult one. Allan put it here to test the latest style file.
        \end{allanenvremark}

    \section{code}
        \begin{allanenvhypothesis}
            test hypothesis.
        \end{allanenvhypothesis}
        type some justifications.

        \begin{allanpy}
            allanpy
            \begin{lstlisting}
                # observation from the air

                %matplotlib inline
                import numpy as np
                import matplotlib.pyplot as plt

                v_car=5.611 # 20km/h on average in hk
                v_eye=16 # Hz
                alpha_lag=1.00
                v_reload=alpha_lag*v_eye

                pie=math.pi
                r_a=13000
                rho_car=0.001023
                delta_d_car=v_car/v_reload
                L_car=4.71769
                C_eye=40960000
                alpha_c1=1.00
                C1=C_eye*alpha_c1
                alpha_c2=0.90
                C2=C_eye*alpha_c2
                alpha_c3=0.80
                C3=C_eye*alpha_c3

                def alpha_clarity(x):
                    if x>0 and x<sep_point:
                        return float(0)
                    elif x>=sep_point and x<=1:
                        return $\displaystyle\left[ -\left( \frac{4096}{4095} \right) ^2 \right] \cdot \left[ \left( x-1 \right) ^2+1 \right]$
                    elif x>1:
                        return float(1)
                output=[0 for i in range(len(dataport))]
                for i in range(len(dataport)):
                    output[i]=alpha_clarity(dataport[i])
                dataport=np.arange(0,1.01,0.01)
                sep_point=1/4096
            \end{lstlisting}
            \begin{allaninpseudo}
                \[\mathrm{data}_i=4\pi \sqrt{r_{a}^{2}-\mathrm{datax}_{i}^{2}}\cdot \rho _{\mathrm{car}}\cdot \Delta d_{\mathrm{car}}\]
            \end{allaninpseudo}
        \end{allanpy}
        another line of text.
        \begin{lstlisting}
                # observation from the air

                %matplotlib inline
                import numpy as np
                import matplotlib.pyplot as plt

                v_car=5.611 # 20km/h on average in hk
                v_eye=16 # Hz
                alpha_lag=1.00
                v_reload=alpha_lag*v_eye

                pie=math.pi
                r_a=13000
                rho_car=0.001023
                delta_d_car=v_car/v_reload
                L_car=4.71769
                C_eye=40960000
                alpha_c1=1.00
                C1=C_eye*alpha_c1
                alpha_c2=0.90
                C2=C_eye*alpha_c2
                alpha_c3=0.80
                C3=C_eye*alpha_c3

                def alpha_clarity(x):
                    if x>0 and x<sep_point:
                        return float(0)
                    elif x>=sep_point and x<=1:
                        return float((-(4096/4095)**2)*((x-1)**2)+1)
                    elif x>1:
                        return float(1)
                output=[0 for i in range(len(dataport))]
                for i in range(len(dataport)):
                    output[i]=alpha_clarity(dataport[i])
                dataport=np.arange(0,1.01,0.01)
                sep_point=1/4096
        \end{lstlisting}
        \begin{codebox}
            \Procname{$\proc{Insertion-Sort}(A)$}
            \li \For $j \gets 2$ \To $\attrib{A}{length}$
            \li \Do
            $\id{key} \gets A[j]$
            \li \Comment Insert $A[j]$ into the sorted sequence
            $A[1 \twodots j-1]$.
            \li $i \gets j-1$
            \li \While $i > 0$ and $A[i] > \id{key}$
            \li \Do
            $A[i+1] \gets A[i]$
            \li $i \gets i-1$
            \End
            \li $A[i+1] \gets \id{key}$
            \End
        \end{codebox}
        \begin{codebox}
            \Procname{$\proc{Segments-Intersect}(p_1, p_2, p_3, p_4)$}
            \li $d_1 \gets \proc{Direction}(p_3, p_4, p_1)$
            \li $d_2 \gets \proc{Direction}(p_3, p_4, p_2)$
            \li $d_3 \gets \proc{Direction}(p_1, p_2, p_3)$
            \li $d_4 \gets \proc{Direction}(p_1, p_2, p_4)$
            \li \If $((d_1 > 0 \mbox{ and } d_2 < 0) \mbox{ or }
            (d_1 < 0 \mbox{ and } d_2 > 0))$ and
            \Indentmore
            \zi $((d_3 > 0 \mbox{ and } d_4 < 0) \mbox{ or }
            (d_3 < 0 \mbox{ and } d_4 > 0))$
            \End
            \li \Then \Return \const{true}
            \li \ElseIf $d_1 \isequal 0$ and $\proc{On-Segment}(p_3, p_4, p_1)$
            \li \Then \Return \const{true}
            \li \ElseIf $d_2 \isequal 0$ and $\proc{On-Segment}(p_3, p_4, p_2)$
            \li \Then \Return \const{true}
            \li \ElseIf $d_3 \isequal 0$ and $\proc{On-Segment}(p_1, p_2, p_3)$
            \li \Then \Return \const{true}
            \li \ElseIf $d_4 \isequal 0$ and $\proc{On-Segment}(p_1, p_2, p_4)$
            \li \Then \Return \const{true}
            \li \ElseNoIf \Return \const{false}
            \End
        \end{codebox}
    \section{colors}
        \color{allanblue} allanblue \color{black}
        \color{allanred} allanred \color{black}
        \color{allangreen} allangreen \color{black}
        \color{allanpurple} allanpurple \color{black}
        \color{allancyan} allancyan \color{black}
        \color{allanorange} allanorange \color{black}
        \color{allanyellow} allanyellow \color{black}
        \color{allandarkblue} allandarkblue \color{black}
    \section{cites}
        I love bibliography. \cite{bibliography}
    \begin{thebibliography}{30}
        \bibitem{bibliography} bibliography is important.
    \end{thebibliography}
\end{document}

example using allan-eason style/example using allan-eason style.tex