% This LaTeX was auto-generated from an M-file by MATLAB.
% To make changes, update the M-file and republish this document.

      \subsection{loss\_unicycle.m}

\begin{par}
Robotic unicycle loss function. The loss is $1-\exp(-0.5*a*d^2)$, where $a$ is a (positive) constant and $d^2$ is the squared difference between the current configuration of the unicycle and a target set point.
\end{par} \vspace{1em}
\begin{par}
The mean and the variance of the loss are computed by averaging over the Gaussian distribution of the state $p(x) = \mathcal N(m,s)$ with mean $m$ and covariance matrix $s$, plus cost.expl times the standard deviation of the loss (averaged wrt the same Gaussian), where the exploration paramater cost.expl defaults to zero.
\end{par} \vspace{1em}
\begin{par}
Negative values of the exploration parameter are used to encourage exploration and positive values avoid regions of uncertainty in the policy. Derivatives are computed when desired.
\end{par} \vspace{1em}
\begin{par}
The mean and the variance of the loss are computed by averaging over the Gaussian distribution of the state $p(x) = \mathcal N(m,s)$ with mean $m$ and covariance matrix $s$. Derivatives of these quantities are computed when desired.
\end{par} \vspace{1em}
\begin{verbatim}function [L, dLdm, dLds, S2] = loss\_unicycle(cost, m, s)\end{verbatim}
\begin{par}
\textbf{Input arguments:}
\end{par} \vspace{1em}
\begin{verbatim}cost            cost structure
  .p            parameters: [radius of wheel, length of rod]    [2 x  1 ]
  .width        array of widths of the cost (summed together)
  .expl         (optional) exploration parameter; default: 0
m               mean of state distribution                      [D x  1 ]
s               covariance matrix for the state distribution    [D x  D ]\end{verbatim}
\begin{par}
\textbf{Output arguments:}
\end{par} \vspace{1em}
\begin{verbatim}L     expected cost                                             [1 x  1 ]
dLdm  derivative of expected cost wrt. state mean vector        [1 x  D ]
dLds  derivative of expected cost wrt. state covariance matrix  [1 x D^2]
S2    variance of cost                                          [1 x  1 ]\end{verbatim}
\begin{par}
Copyright (C) 2008-2013 by Marc Deisenroth, Andrew McHutchon, Joe Hall, and Carl Edward Rasmussen.
\end{par} \vspace{1em}
\begin{par}
Last modified: 2013-03-26
\end{par} \vspace{1em}

\subsection*{High-Level Steps}

\begin{enumerate}
\setlength{\itemsep}{-1ex}
   \item Precomputations
   \item Define static penalty as distance from target setpoint
   \item Trigonometric augmentation
   \item Calculate loss
\end{enumerate}

\begin{lstlisting}
function [L, dLdm, dLds, S2] = loss_unicycle(cost, m, s)
\end{lstlisting}

\subsection*{Code}

\begin{lstlisting}
rw = cost.p(1); r = cost.p(2);
if isfield(cost,'width'); cw = cost.width; else cw = 1; end
if ~isfield(cost,'expl') || isempty(cost.expl); b = 0; else b =  cost.expl; end

I6 = 8;  I9 = 10;                           % coordinates of theta and psi
Ixc = 6; Iyc = 7;                               % coordinates of xc and yc

% 1. Some precomputations
D = size(s,2);                                           % state dimension
D0 = D + 2;             % state dimension (augmented with I6-I9 and I6+I9)
D1 = D0 + 8;                              % state dimension (with sin/cos)
L = 0; dLdm = zeros(1,D); dLds = zeros(1,D*D); S2 = 0;

P = [eye(D); zeros(2,D)]; P(D+1:end,I6) = [1;-1]; P(D+1:end,I9) = [1;1];

M = zeros(D1,1); M(1:D0) = P*m; S = zeros(D1); S(1:D0,1:D0) = P*s*P';
Mdm = [P; zeros(D1-D0,D)]; Sdm = zeros(D1*D1,D);
Mds = zeros(D1,D*D); Sds = kron(Mdm,Mdm);

% 2. Define static penalty as distance from target setpoint
Q = zeros(D+10);
C1 = [rw r/2 r/2];
Q([D+4 D+6 D+8],[D+4 D+6 D+8]) = 8*(C1'*C1);                          % dz
C2 = [1 -r];
Q([Ixc D+9],[Ixc D+9]) = 0.5*(C2'*C2);                                % dx
C3 = [1 -(r+rw)];
Q([Iyc D+3],[Iyc D+3]) = 0.5*(C3'*C3);                                % dy
Q(9,9) = (1/(4*pi))^2;                                    % yaw angle loss

target = zeros(D1,1); target([D+4 D+6 D+8 D+10]) = 1;    % target setpoint

% 3. Trigonometric augmentation
i = 1:D0; k = D0+1:D1;
[M(k) S(k,k) C mdm sdm Cdm mds sds Cds] = ...
  gTrig(M(i),S(i,i),[I6 D+1 D+2 I9]);
[S Mdm Mds Sdm Sds] = ...
  fillIn(S,C,mdm,sdm,Cdm,mds,sds,Cds,Mdm,Sdm,Mds,Sds,i,k,D1);

% 4. Calculate loss
for i = 1:length(cw)                    % scale mixture of immediate costs
  cost.z = target; cost.W = Q/cw(i)^2;
  [r rdM rdS s2 s2dM s2dS] = lossSat(cost, M, S);

  L = L + r; S2 = S2 + s2;
  dLdm = dLdm + rdM(:)'*Mdm + rdS(:)'*Sdm;
  dLds = dLds + rdM(:)'*Mds + rdS(:)'*Sds;

  if (b~=0 || ~isempty(b)) && abs(s2)>1e-12
    L = L + b*sqrt(s2);
    dLdm = dLdm + b/sqrt(s2) * ( s2dM(:)'*Mdm + s2dS(:)'*Sdm )/2;
    dLds = dLds + b/sqrt(s2) * ( s2dM(:)'*Mds + s2dS(:)'*Sds )/2;
  end
end

% normalize
n = length(cw); L = L/n; dLdm = dLdm/n; dLds = dLds/n; S2 = S2/n;

% Fill in covariance matrix...and derivatives ----------------------------
\end{lstlisting}

\begin{lstlisting}
function [S Mdm Mds Sdm Sds] = ...
  fillIn(S,C,mdm,sdm,Cdm,mds,sds,Cds,Mdm,Sdm,Mds,Sds,i,k,D)
X = reshape(1:D*D,[D D]); XT = X';                    % vectorized indices
I=0*X; I(i,i)=1; ii=X(I==1)'; I=0*X; I(k,k)=1; kk=X(I==1)';
I=0*X; I(i,k)=1; ik=X(I==1)'; ki=XT(I==1)';

Mdm(k,:)  = mdm*Mdm(i,:) + mds*Sdm(ii,:);                      % chainrule
Mds(k,:)  = mdm*Mds(i,:) + mds*Sds(ii,:);
Sdm(kk,:) = sdm*Mdm(i,:) + sds*Sdm(ii,:);
Sds(kk,:) = sdm*Mds(i,:) + sds*Sds(ii,:);
dCdm      = Cdm*Mdm(i,:) + Cds*Sdm(ii,:);
dCds      = Cdm*Mds(i,:) + Cds*Sds(ii,:);

S(i,k) = S(i,i)*C; S(k,i) = S(i,k)';                        % off-diagonal
SS = kron(eye(length(k)),S(i,i)); CC = kron(C',eye(length(i)));
Sdm(ik,:) = SS*dCdm + CC*Sdm(ii,:); Sdm(ki,:) = Sdm(ik,:);
Sds(ik,:) = SS*dCds + CC*Sds(ii,:); Sds(ki,:) = Sds(ik,:);
\end{lstlisting}


%%%% 2D Skewness Example %%%%%%
\subsection{Impact of Skewness on Accuracy}\label{ex:skewness}
In this example, we demonstrate the key point of this study: the magnitude of skewness between QoI maps impacts accuracy by orders of magnitude, and thus in optimizing the choice of a QoI map, it is in our interest to pursue the minimization of skewness.
This is especially true in problems where the number of random samples we are permitted to use is constrained by the computational cost of model evaluations.

%Thus, any map can be thought of as a piecewise-defined linear map, and the results we present in this example, while applying to all of $\pspace$ in these cases, can be applied solely to the support of each local linear approximation.
%Capturing the geometry of sets (improving accuracy) on each of these subdomains thus guarantees a desired result on the entirety of the domain.

To illustrate this point, we first define the linear maps:
\begin{equation}\label{eq:qmap2}
\qspace_S := \left \lbrace Q^{(s)} =  \mat{cc}{1 & 0 \\ \sqrt{s^2 - 1}& 1 } \right \rbrace_{s\in S},
\end{equation}
for $S=\set{1,2,4}$ because they allow us to control the global skewness (since it is equal to local skewness in a linear map) while preserving the measures of sets between $\pspace$ and $\dspace$.
More specifically, the support of the solution to the SIP associated with each QoI map has equal $\mu_\pspace$-measure, which isolates the impact of accuracy solely to the skewness of the QoI map.
% We show what the component row vectors of these maps in Figure~\ref{fig: skewmapvecs} and note the skewness is determined by the ratio of the magnitude of the black line to its projection onto the vertical axis (and each of these projects directly on to the unit vector).
The skewness of these maps is given by the index $s$, so $Q^{(1)}$ is $1$, the skewness of $Q^{(2)}$ is $2$, and $S_{Q^{(4)}} = 4$.

% The maps chosen for this example are expository ones that provide valuable insight despite their simplicity.
% For example, when solving many physics-based problems, local linear approximations are often used to simplify model evaluation and guide optimization procedures.

%
% \begin{figure}[h]
% 	\begin{minipage}{.3\textwidth}
% 		\includegraphics[width=\linewidth]{./images/vector_a.png}
% 	\end{minipage}
% 	\begin{minipage}{.3\textwidth}
% 		\includegraphics[width=\linewidth]{./images/vector_b.png}
% 	\end{minipage}
% 	\begin{minipage}{.3\textwidth}
% 		\includegraphics[width=\linewidth]{./images/vector_c.png}
% 	\end{minipage}
% \caption{(Left to right):  The component row-vectors of $Q^{(1)}$, $Q^{(2)}$, and $Q^{(4)}$. Our linear maps take $\RR^2$ to $\RR^2$ and can be visualized graphically as the component row-vectors of the matrices representing the transformation. The first row is highlighted in blue. The skewness is then simply equal to the reciprocal of the inverse sine of the angle between these vectors. }
% \label{fig: skewmapvecs}
% \end{figure}

\begin{figure}
\begin{table}[H]
\begin{tabular}{ c | c | c | c }
\nsamps & $\qoiA$ & $\qoiB$ & $\qoiC$\\ \hline \hline
$200$ & $2.02E-01$ & $2.49E-01$ & $3.37E-01$\\ \hline

$400$ & $1.75E-01$ & $2.16E-01$ & $2.58E-01$\\ \hline

$800$ & $1.19E-01$ & $1.45E-01$ & $1.81E-01$\\ \hline

$1600$ & $8.23E-02$ & $9.67E-02$ & $1.29E-01$\\ \hline

$3200$ & $6.10E-02$ & $7.15E-02$ & $9.22E-02$\\ \hline

$6400$ & $3.96E-02$ & $5.47E-02$ & $6.39E-02$\\ \hline
\end{tabular}
\end{table}

\includegraphics[width=0.45\linewidth]{./images/Plot-reg_BigN_40000_reg_M_1_rand_I_100000}

\caption{The results of $d^2_\text{TV}(\PP_{\pspace, \ndiscs, \nsamps}, \PP_{\pspace, \ndiscs, \bar{\nsamps}})$ for $\ndiscs = 1, \bar{\nsamps} = 40,000$, with $a, b, c = 1, 2, 4$ in two dimensions.}
\label{fig:M1_2d}
\end{figure}

The displaced horizontal lines and accompanying table in Figure~\ref{fig:M1_2d} demonstrate that skewness has a direct impact on the number of samples required to achieve a particular value for the Total Variation distance.
By comparing adjacent columns in the table, it appears that if the ratio of skewness between two maps is 2, then the more-skewed map will require approximately twice as many random samples to approximate the set on a a well-resolved discretization with the same error tolerance.
We observe that the measure induced by $Q^{(1)}$ requires fewer than half the number of samples to be as accurately resolved as $Q^{(2)}$ does.
The effect is even more pronounced when compared against $Q^{(4)}$.

The fact we can achieve the same accuracy with fewer model evaluations provides a strong motivation for minimizing skewness and reinforces the results from \cite{BPW_2015}.
There, it was demonstrated that a similar relationship existed in the number of samples required to remove error in inverse set approximations quantified by the $\mu_\pspace$-measure of the {\em symmetric difference} of the inverse sets.
Rather than study the support of the distributions as was the focus in \cite{BPW_2015}, we turn our attention to a formal measure of accuracy for the densities or measures involved in the solution to the SIP.
We have shown that the skewness has an impact not only on the symmetric difference error, but also on the approximation error as measured by Total Variation.


\documentclass[simplex.tex]{subfiles}
% NO NEED TO INPUT PREAMBLES HERE
% packages are inherited from simplex.tex; you can compile this on its own
\begin{document}
\subsection{ndreg}

The NeuroData Registration python module, \textit{ndreg}, uses \textit{Large Deformation Diffeomorphic Metric Mapping (LDDMM)} to register a template image $I_0$ to a target image $J_1$.
It does this by finding smooth invertible map $\varphi$ such that the \textit{matching term} $M(I_0 \circ \varphi^{-1}, J_1)$, a function whose value is small when $I_0 \circ \varphi^{-1}$ is aligned with $J_1$, is minimized.
We compared MI-LDDMM, LDDMM with a Mutual Information based matching term, to our previous SSD-LDDMM and Mask-LDDMM techniques.
SSD-LDDMM uses a Sum of Squared Differences matching term.
As it is based on image subtraction, it assumes that bright regions in $I_0$ align with bright regions in $J_1$.
Mask-LDDMM is SSD-LDDMM in which $I_0$ and $J_1$ are replaced with their respective binary brain mask images $M_0$ and $M_1$.
Since the masks only contain information on which voxels are inside the brain, only edge information is used by Mask-LDDMM.
\\
We placed fiducial landmarks in the corpus callosum and midbrain of four CLARITY template image and the \textit{Allen Reference Atlas (ARA)} target.
After registration corresponding CLARITY landmarks should be close to those of the ARA.
If not then the registration was inadequate.
Figure~\ref{fig:comparison} shows that MI-LDDMM registration performed better than the previous methods.

\begin{figure}[!h]
\begin{cframed}
\centering
 \begin{subfigure}{0.25\columnwidth}
  \includegraphics[width=\textwidth]{../../figs/ssdCoronal.png}
  \caption{SSD-LDDMM}
  \label{fig:clarityCoronalSSD}
 \end{subfigure}
 \begin{subfigure}{0.25\columnwidth}
  \includegraphics[width=\textwidth]{../../figs/maskCoronal.png}
  \caption{Mask-LDDMM}
  \label{fig:clarityCoronalMask}
 \end{subfigure}
 \begin{subfigure}{0.25\columnwidth}
  \includegraphics[width=\textwidth]{../../figs/miCoronal.png}
  \caption{MI-LDDMM}
  \label{fig:clarityCoronalMI}
 \end{subfigure}
 \begin{subfigure}{0.2\columnwidth}
  %\captionsetup{justification=centering} % Center caption
  \includegraphics[width=\textwidth]{../../figs/lmkError.png}
  \caption{Landmark Error}
  \label{fig:lmkError}
 \end{subfigure}
 \caption{Comparison of SSD-LDDMM (\subref{fig:clarityCoronalSSD}), Mask-LDDMM (\subref{fig:clarityCoronalMask}) and MI-LDDMM (\subref{fig:clarityCoronalMI}) registration for a CLARITY mouse brain.
  Panes (\subref{fig:clarityCoronalSSD}-\subref{fig:clarityCoronalMI}) have an ARA coronal slice on the left juxtaposed to the corresponding aligned CLARITY slice on the right.
  Green arrows point out that the corpus callosum is misaligned by SSD-LDDMM but aligned correctly by MI matching.
  Red arrows show that SSD-LDDMM distorts bright regions.
  Fiducial landmarks were placed throughout the corpus callosum, and midbrain of the acquired volumes.
  Pane (\subref{fig:lmkError}) compares mean errors between the deformed CLARITY and ARA landmarks after registration.
 }
 \label{fig:comparison}
\end{cframed}
\end{figure}

\clearpage
\end{document}


\section{Local vs.\ Global Solvers}

The problem of \emph{solving} a parity game $G = (V,V_0,V_1,E,\Omega)$ is, roughly speaking, to determine
which of the players has a winning strategy for that game. However, this does not take starting nodes into
account. In order to obtain a well-defined problem, this is refined in two ways.

The problem of solving the parity game $G$ \emph{globally} is to determine, for \emph{every} node $v \in V$
whether $v \in W_0$ or $v \in W_1$. The problem of solving $G$ \emph{locally} is to determine for a
\emph{given} $v \in V$ whether $v \in W_0$ or $v \in W_1$ holds. In addition, we require a \emph{solver}
to compute (partial) winning strategies in the following sense: a local solver should return one strategy
that is a winning strategy for player $i$ if $v \in W_i$ for the given input node $v$. A global solver
should return two strategies, one for each player s.t.\ player $i$ wins exactly on the nodes $v \in W_i$
if he/she plays according to the strategy computed for her.

Clearly, the local and global problem are interreducible, the global one solves the local one for free,
and the global one is solved by calling the local one $|V|$ many times. But neither of these indirect
methods is particularly clever. Thus, there are algorithms for the global, and other algorithms for the
local problem. We focus on the global problem here, but also use local solvers. In that case we expect
them to determine for \emph{at least} the given node $v$ to which winning set it belongs but possibly and
preferably also for other nodes. These methods can then be used to solve the global problem by calling
the local algorithm \emph{at most} $|V|$ many times.

Suppose $V = \{v_0,\ldots,v_n\}$. Then one can solve $G$ globally using a local solver $A$ as follows.
Start $A$ with starting node $v_0$. It will return two (possibly empty) winning sets $W'_0$ and $W'_1$.
In any case, we will have $W'_0 \cap W'_1 = \emptyset$, but not necessarily $W'_0 \cup W'_1 = V$. Let
$W' := V \setminus (\attr{}{0}{W'_0} \cup \attr{}{1}{W'_1})$. Then taking out all nodes in $W'$ from $G$
will result in a total parity game again by two applications of Lemma~\ref{lem:minusattr} and the fact
that $\attr{}{0}{W'_0} \cap \attr{}{1}{W'_1} = \emptyset$. Let $G'$ be this game. Then the winning positions
for player $i$ in $G$ are $\attr{}{i}{W'_i}$ plus the winning positions for him/her in $G'$. Since $G'$
will be smaller than $G$ this can be used iteratively or recursively, to compute the entire $W_0$ and $W_1$.

The winning strategies can equally be assembled to a winning strategy $\sigma$ for player $i$. Let
$\sigma'$ be player $i$'s winning strategy on the subgame $G'$, $\alpha$ be his/her attractor strategy
for reaching $W'_i$, and $\eta$ be the strategy that guarantees him/her to win on $W'_i$. Then define
\begin{displaymath}
\sigma(v) \enspace := \enspace
\begin{cases}
\eta(v) \enspace, &\mbox{if } v \in W'_i \\
\alpha(v) \enspace, &\mbox{if } v \in \attr{}{i}{W'_i} \setminus W'_i \\
\sigma'(v) \enspace, &\mbox{if } v \mbox{ is a node in } G' \\
\mbox{anything} & \mbox{otherwise}
\end{cases}
\end{displaymath}
The following theorem provides correctness of this construction.

\begin{proposition}
Let $G = (V,V_0,V_1,E,\Omega)$ be a parity game, let $W_i$ be the winning set of player $i$ and
$i \in \{0,1\}$. Player $i$ has a positional strategy $\sigma$ for $G$ s.t. $\sigma$ is a positional
winning strategy for $G$ starting in any node $v \in W_i$.
\end{proposition}

To see that this holds let $\sigma_v$ be positional winning strategies for $G$ starting in $v$ for each node
$v \in W_i$ (which have to exist by the former theorem). We assume all nodes $v \in W_i$ to be ordered
w.r.t.\ a well-ordering $<$. A positional strategy $\sigma$ winning from each node $v \in W_i$ can be
defined as follows:
\begin{displaymath}
\sigma: v \in W_i \cap V_i \mapsto \sigma_{\min_{<} \{u \in W_i \mid v \in dom(\sigma_u)\}} (v)
\end{displaymath}
To see that $\sigma$ indeed is a positional winning strategy for $G$ starting in any node $v \in W_i$ let
$v_0 v_1 \ldots$ be a $\sigma$-conforming play with $v_j \in W_i$ for all $j \in \Nat$. Since $<$ is a
well-ordering $\sigma$ finally simulates the strategy of $\sigma_u$ for some $u \in W_i$ and thus player
$i$ is wins this play.

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "main"
%%% End:


\chapter{Sparse Recurrent Neural Networks}\label{chap:experiments}
This chapter explains the experiments described in section \ref{section:proposed_approach}, but before that, we first describe our base model, against which we will compare the performance of our pruning experiment.

% -----------------------------------------------------------------------------------------------------------
% ----------------------------------------------- BASE MODEL ------------------------------------------------
% -----------------------------------------------------------------------------------------------------------

\section{Base model}\label{section:training_rnn}
Apart from the standard input and output layer, our base model is a regular RNN model followed by a linear layer and stacked upon an embedding layer, as shown in the following figure:

\begin{figure}[h]
	\centering
	\includegraphics[width=0.85\linewidth]{images/experiments/training_rnn.png}
	\caption[Training recurrent networks]%
	{Visualizing training of a recurrent network with 3 recurrent layers, each layer consisting of 50 neurons.}
	\label{fig:training_rnn}
\end{figure}

Our base model starts with an input layer that feeds data to the model. Since our data is in text format, we convert each character into its corresponding ASCII value. This conversion is visualized in figure \ref{fig:training_rnn} with a valid example Reber sequence, \textbf{BTSXE}. Furthermore, we are working with batches, so all the sequences in a batch must be of equal length. Therefore, we apply zero-padding to ensure all the sequences in a given batch are of the same length.

After the input layer, the second layer is an embedding layer that transforms each input character into a fixed-length vector as visualized in figure \ref{fig:input-to-embedding}. In our case, this embedding layer is of shape $(128, 50)$, where $128$ is the number of possible ASCII characters, and $50$ is the size of each embedding vector known as embedding dimensions. The value of an embedding dimension must be equal to the number of neurons in the next layer.

\begin{figure}[h]
	\centering
	\includegraphics[width=0.85\linewidth]{images/experiments/input-to-embedding.png}
	\caption[Generating embeddings of example input sequence]%
	{Generating embeddings of example Reber sequence after converting each character of this sequence into its corresponding ASCII value.}
	\label{fig:input-to-embedding}
\end{figure}

After the embedding layer, the subsequent three layers are recurrent, each layer containing $50$ neurons. These layers start with an initial hidden state of shape \textit{(batch size, hidden size)}, comprising all zeros. In LSTM, these layers begin with one initial hidden state and one initial cell state, both of similar shape and comprising of all zeros. Each neuron in recurrent layers is an individual RNN unit with either Tanh or ReLU nonlinearity in case of vanilla recurrent network, LSTM unit in case of LSTM recurrent network and GRU unit in case of GRU recurrent network as visualized in the following figure:

\begin{figure}[h]
	\centering
	\includegraphics[width=1.0\linewidth]{images/experiments/recurrent_layer.png}
	\caption[Visualization of a single recurrent layer]%
	{Visualizing a single recurrent layer from our model (fig. \ref{fig:training_rnn}). Each neuron is either an individual RNN unit (fig. \ref{fig:rnn_unit}) or an LSTM unit (fig. \ref{fig:lstm_unit}), or a GRU unit (fig. \ref{fig:gru_unit}).}
	\label{fig:recurrent_layer}
\end{figure}

These recurrent layers are followed by a linear layer that takes input from the third recurrent layer and applies the following linear transformation on the input:
\begin{equation}
\label{eq:linear}
    \hat{y} = xW^{T}+b
\end{equation}
where $\hat{y}$ is the output, $W$ represents weight matrices of shape $(output\_size=2, input\_size=50)$, $x$ is the input, and $b$ is bias of shape $(output\_size=2)$.

Since the third recurrent layer's hidden size is $50$, the linear layer's input is of size $50$, and output is of size two because our target has only two classes, $\{0, 1\}$, as shown in figure \ref{fig:output_layer}.

\begin{figure}[h]
	\centering
	\includegraphics[width=0.3\linewidth]{images/experiments/output_layer.png}
	\caption[Visualization of linear layer's connection to output layer]%
	{\textbf{Visualizing of linear layer's connection to output layer.} Each of the 50 neurons from the linear layer is densely connected to both the output layer neurons.}
	\label{fig:output_layer}
\end{figure}

For our example Reber sequence, the final output is in the form of a float array of size two, for instance, $[-0.8169, 1.3229]$. To transform it into $0$ or $1$, take the index of maximum value from this array. After getting an output, the model calculates the cross-entropy loss and optimizes it via backpropagation through time to a minimum value to achieve better performance.

We train this model on our training dataset of $18750$ sequences for $50$ epochs, and to check its performance, we evaluate it on the test dataset of $6250$ sequences. This training and evaluation employ the hyper-parameters shown in the following table.

\begin{table}[h]
	\centering
	\begin{tabular}{|c|c|}
	    \hline
		\textbf{Hyper-parameter} & \textbf{Value} \\
		\hline
		Learning rate & 0.001 \\
		Loss & Cross-Entropy Loss \\
		Optimizer & Adam \\
		Batch size & 32 \\
		\hline
	\end{tabular}
	\caption[Hyper-parameters used for training and evaluating the base model]{Hyper-parameters used for training and evaluating the base model.}
	\label{tab:hype_param_base}
\end{table}

Since we work with four different RNN variants, namely RNN with Tanh nonlinearity, RNN with ReLU nonlinearity, LSTM, and GRU, we also perform this training four times.

As we have already established, training a recurrent network is computationally expensive and time-consuming. Therefore all four models in our experiment are trained on a Tesla K80 GPU.

% -----------------------------------------------------------------------------------------------------------
% ----------------------------------------------- PRUNE RNNs ------------------------------------------------
% -----------------------------------------------------------------------------------------------------------

\newpage
\section{Pruned Recurrent Neural Networks}\label{section:pruned_rnn}

One of the ways to introduce sparsity in recurrent networks is to prune weight below a certain threshold. As shown in figure \ref{fig:rnn}, we have three different types of weights; input-to-hidden weights, hidden-to-hidden weights, and hidden-to-output weights. In our experiment, we individually and simultaneously prune input-to-hidden and hidden-to-hidden weights as envisioned in the following figure:

\begin{figure}[h]
  \begin{subfigure}{0.33\textwidth}
    \includegraphics[width=\linewidth]{images/experiments/both_prune.png}
    \caption{} \label{fig:both}
  \end{subfigure}%
  %\hspace*{\fill}   % maximize separation between the subfigures
  \begin{subfigure}{0.33\textwidth}
    \includegraphics[width=\linewidth]{images/experiments/i2h_prune.png}
    \caption{} \label{fig:i2h}
  \end{subfigure}%
  %\hspace*{\fill}   % maximizeseparation between the subfigures
  \begin{subfigure}{0.33\textwidth}
    \includegraphics[width=\linewidth]{images/experiments/h2h_prune.png}
    \caption{} \label{fig:h2h}
  \end{subfigure}

\caption[Simultaneous and individual pruning of i2h and h2h weights]{Simultaneously and individually pruning input-to-hidden and hidden-to-hidden weights. The red right arrow (\textcolor{red}{$\longrightarrow$}) resembles the pruning of hidden-to-hidden weight, while the red up-arrow (\textcolor{red}{$\big\uparrow$}) resemble the pruning of input-to-hidden weight. Fig. \ref{fig:both} illustrates pruning both, input-to-hidden and hidden-to-hidden weights, fig. \ref{fig:i2h} illustrates pruning only input-to-hidden weights, and fig. \ref{fig:h2h} illustrates pruning only hidden-to-hidden weights.} \label{fig:pruning}
\end{figure}

The threshold based on which we prune weights is calculated based on the percent of weights to prune. Therefore, for example, to prune $p=10\%$ of weights for a given layer, the threshold is the 10th percentile of all the \textbf{absolute} weights in that layer (code \ref{code:threshold}). In our experiment, as shown in figure \ref{fig:flowchart_pruning}, we go from percent $p=10$ to $100$ while incrementing $p$ by $10$ after each round.

Using this threshold value, we create a binary tensor called a mask, where $1$ corresponds to the \textbf{absolute} weight value above the threshold, and $0$ corresponds to the \textbf{absolute} weight value below the threshold (code \ref{code:mask}) in the corresponding weight matrix. Element-wise multiplication of this binary mask with the corresponding weight matrix zero-outs the absolute weight values below the threshold value.

To understand this binary mask generation and mask multiplication, consider the following example weight matrix of shape $(3, 3)$ with float values between $-1$ and $1$:

\[
\begin{bmatrix}
    -0.685 & 0.530 & -0.464 \\
    -0.534 & 0.828 &  0.045 \\
    -0.123 & 0.629 & -0.014
\end{bmatrix}
\]

This weight matrix corresponds to a linear layer with 3 neurons densely connected to the next linear layer with 3 neurons as shown in the below figure:

\begin{figure}[h]
	\centering
	\includegraphics[width=0.3\linewidth]{images/experiments/three_neuron.png}
	\caption[Two densely connected linear layers]%
	{Two densely connected linear layers, each layer containing 3 neurons. This visualization corresponds to the example weight matrix shown above.}
	\label{fig:three_neuron}
\end{figure}

To prune $10\%$ percent of weight values from the above matrix, we calculate the threshold by finding the 10th percentile of this matrix's \textbf{absolute} values, which is $0.0387$. Using this threshold, we create a binary mask of shape $(3, 3)$, where values at positions that correspond to the below threshold \textbf{absolute} values in the weight matrix are kept $0$ while other values are kept $1$ as shown below:

\[
\begin{bmatrix}
    -0.685 & 0.530 & -0.464 \\
    -0.534 & 0.828 &  0.045 \\
    -0.123 & 0.629 & \textcolor{red}{-0.014}
\end{bmatrix}
\longrightarrow
\begin{bmatrix}
    1 & 1 & 1 \\
    1 & 1 &  1 \\
    1 & 1 & \textcolor{red}{0}
\end{bmatrix}
\]

By performing element-wise multiplication of this binary mask with our weight matrix, we get a pruned weight matrix as shown below:

\[
\begin{bmatrix}
    -0.685 & 0.530 & -0.464 \\
    -0.534 & 0.828 &  0.045 \\
    -0.123 & 0.629 & \textcolor{red}{-0.014}
\end{bmatrix}
\circ
\begin{bmatrix}
    1 & 1 & 1 \\
    1 & 1 &  1 \\
    1 & 1 & \textcolor{red}{0}
\end{bmatrix}
=
\begin{bmatrix}
    -0.685 & 0.530 & -0.464 \\
    -0.534 & 0.828 &  0.045 \\
    -0.123 & 0.629 & \textcolor{red}{0}
\end{bmatrix}
\]

The value $0$ in this weight matrix represents the missing connections between the third neuron of the first and second linear layer. After applying the mask, the simple neural network shown in the figure \ref{fig:three_neuron} looks like below:

\begin{figure}[h]
	\centering
	\includegraphics[width=0.5\linewidth]{images/experiments/pruned_three_neuron.png}
	\caption[Two densely connected linear layers after pruning]%
	{Two densely connected linear layers containing 3 neurons show the absence of connection between each layer's third neuron after pruning. This visualization corresponds to the pruned weight matrix.}
	\label{fig:pruned_three_neuron}
\end{figure}

We follow a similar mask generation approach and element-wise multiplication for each corresponding weight matrix in each recurrent layer of our trained model. As shown above, this pruning process returns a pruned weight matrix for each corresponding weight matrix. After pruning, we evaluate pruned model's performance on the test dataset to see how such pruning affects an already trained model's overall accuracy.

Once we have the pruned model's performance, the next step is to retrain the same pruned model to estimate the number of epochs required to regain the accuracy. We use the same pruned weight matrices throughout the entire retraining course. This helps us understand how long it takes for a recurrent network to relearn and update weight matrices to make up for the lost connections.

These pruning and retraining steps are repeated three times per RNN variant (i.e., RNN-Tanh, RNN-ReLU, LSTM, and GRU), first time for pruning both, input-to-hidden and hidden-to-hidden weights, the second time for pruning only input-to-hidden weights, and the third time for pruning only hidden-to-hidden weights.

% -----------------------------------------------------------------------------------------------------------
% ----------------------------------------------- RA-ST RNNs ------------------------------------------------
% -----------------------------------------------------------------------------------------------------------

\newpage
\section{Randomly Structured Recurrent Neural Networks}\label{section:random_rnn}

The pruning method to induce sparsity in a recurrent network works by canceling connections between neurons of two separate layers. While in this section, we explain another method to induce sparsity by generating random structures that are already sparse. This method was introduced for Artificial Neural Network by Stier et al. in \cite{julian}, which we will revise to make it suitable for our goal.

The first step of this experiment is to generate random graphs irrespective of how they might correspond to a neural network. We generate $100$ connected Watts–Strogatz (\ref{subsubsection:wsmodel}) and Barabási–Albert (\ref{subsubsection:bamodel}) graphs using the graph generators provided by the networkx\footnote{NetworkX is an open-source Python library that empowers users to create and manipulate various graph structures.}. Each of these graphs contains nodes between $(10, 51)$, meaning the corresponding neural architecture will have $(10, 51)$ neurons.

The networkx graph generators return undirected graphs. Therefore, the next is to make them directed. Converting a random graph into a Directed Acyclic Graph is vital to make it easier to compute layer indexing.

After converting a random graph into a DAG, the next step is to compute layer indexing of all the nodes in this DAG. This helps us identify which node of this DAG belongs to which layer in the resulting neural network. The recursive algorithm to compute layer indexing of a node (similar to the one used by Stier et al. in \cite{julian}) is as below:

\begin{algorithm}
  \DontPrintSemicolon
  \caption[Compute layer indexing of nodes]%
  {Recursive function to compute layer indexing of nodes}
  \label{alg:layer_index}

    \KwIn{Graph $G$, Set of nodes $V$, Set of edges $E$}
    \KwOut{Layer index mapping for each node $v \in V$}

    \SetKwFunction{func}{LayerIndexOf}
    \SetKwProg{Fn}{Def}{:}{}

    \Fn{\func{$v$}}{
        $l \gets [-1]$ \tcp*{List $l$, initialized with an element $-1$}

        \ForEach{$s \in V | E_{s \rightarrow v}$}{
            $l$.append(LayerIndexOf($s$))
        }

        \textbf{return} (max($l$) + 1)
    }

    $indexes \gets dict()$ \tcp*{Initializing an empty dictionary, $indexes$}
    \ForEach{$v \in V$}{
        $indexes[v] \gets $ LayerIndexOf($v$)
    }
\end{algorithm}

This recursive function outputs layer indexing of all nodes in our DAG, based on which we generate our final neural architecture, which then we convert into a recurrent network by introducing hidden-to-hidden connections, i.e., recurrent connections.

Once we have our randomly structured recurrent network, the final step is to embed it between an embedding layer and a linear layer. The embedding layer transforms data from the input layer into a fixed-length vector and feeds them to our recurrent layer. The linear layer takes the last recurrent layer's output, applies a linear transformation shown in equation \ref{eq:linear}, and generates final outputs.

\subsection{From Random Graph to recurrent network: An example}

To begin, consider the following undirected graph containing 5 nodes:

\begin{figure}[h]
	\centering
	\includegraphics[width=0.45\linewidth]{images/experiments/undirected.png}
	\caption[Undirected graph containing $5$ nodes]%
	{An undirected graph containing $5$ nodes.}
	\label{fig:undirected}
\end{figure}

The next step is to convert this graph into a DAG. This conversion introduces directed edges from one node to another, as shown below:

\begin{figure}[h]
	\centering
	\includegraphics[width=0.45\linewidth]{images/experiments/directed.png}
	\caption[Directed graph containing $5$ nodes]%
	{A directed graph containing $5$ nodes.}
	\label{fig:directed}
\end{figure}

The next step is to compute layer indexing of all $5$ nodes in this DAG using algorithm \ref{alg:layer_index}. The following table shows the values of important variables in this algorithm and the final layer indexing:

\begin{table}[h]
	\centering
	\begin{tabular}{|c|c|c|c|}
	    \hline
		\textbf{Node $v$} & \textbf{Set of $s, \forall s \in V | E_{s \rightarrow v}$} & List $l$ & Layer index of node $v$\\
		\hline
		0 & \{\} & [-1] & 0\\
		1 & \{\} & [-1] & 0\\
		2 & \{0, 1\} & [-1, 0, 0] & 1\\
		3 & \{1, 2\} & [-1, 0, 1] & 2\\
		4 & \{2, 3\} & [-1, 1, 2] & 3\\
		\hline
	\end{tabular}
	\caption[Computing layer indexing of example DAG]{We first identify a set of all the parent nodes for a given node $v$, then append layer indexing of these parent nodes to the list $l$, and finally return $max(l) + 1$ as layer index of node $v$.}
	\label{tab:layer_index}
\end{table}

The next step is to create a neural architecture using this layer indexing:

\begin{figure}[h]
	\centering
	\includegraphics[width=0.25\linewidth]{images/experiments/neural_arc.png}
	\caption[Neural architecture generated using the layer indexing]%
	{A neural architecture of $4$ layers, generated using the layer indexing of nodes shown in table \ref{tab:layer_index}.}
	\label{fig:neural_arc}
\end{figure}

Once we have the neural architecture, as shown in figure \ref{fig:neural_arc}, the next step is to introduce recurrent connections. This will give us our randomly structured recurrent network.

Attaching this randomly structured recurrent network between an embedding layer and a linear layer, as shown in the below figure, gives us our final model:

\begin{figure}[h]
	\centering
	\includegraphics[width=0.8\linewidth]{images/experiments/recurrent_arc.png}
	\caption[Complete model with recurrent architecture generated from a random graph]%
	{A complete trainable model with an input and output layer, an embedding layer, a linear layer, and a recurrent architecture generated from a random graph.}
	\label{fig:recurrent_arc}
\end{figure}

This is the final trainable model that we train on our training dataset for $15$ epochs and evaluate on the test dataset.

Similarly, we generate $200$ random graphs with nodes between $(10, 51)$ for each RNN variant (i.e., RNN-Tanh, RNN-ReLU, LSTM, and GRU), convert them into a recurrent network, and create the full model for each randomly structured recurrent network, as shown in figure \ref{fig:recurrent_arc}. We train them on our train dataset for $15$ epochs with hyper-parameters shown in table \ref{tab:hype_param_base} except with batch size of $128$. Finally, we evaluate them on our test dataset and store the performance that is useful in the next part of the experiment.

\subsection{Identifying important graph properties}

While training random structure recurrent networks, we also record specific graph properties (mentioned in section \ref{subsection:properties}) of its base random graph. After training and evaluation are complete, we store its accuracy, creating a small dataset of $200$ rows.

Since the graph properties in our small dataset are in different ranges, we apply MinMax scaler on our dataset to scale each graph property between a range $(0, 1)$.

Next, we split this dataset into a train-test with a $0.9/0.1$ ratio. We train three different regressor algorithms: Bayesian ridge regressor, Random Forest regressor, and Ada Boost regressor, on the train set with graph properties as features and its corresponding accuracy as the target.

Then, we evaluate each regressor algorithm on the test set and report an R-squared value to understand how our data fit each regressor model.

This chapter described our experiments with Sparse RNN in detail. After getting familiarized with our experiments, we present our experiments' results and their corresponding visualizations in the next chapter.


\documentclass[a4paper,12pt]{article}

%PACKAGES
\usepackage[margin=2cm]{geometry}
\usepackage{latexsym,amsmath,amsfonts,amssymb}
\usepackage{graphicx,color}
\usepackage{pstricks,pst-node,pst-text,pst-3d}
\usepackage{verbatim}
\usepackage{hyperref}
\usepackage{float,placeins}
\usepackage{xcolor}
\hypersetup{
    colorlinks,
    linkcolor={reds!50!black},
    citecolor={blue!50!black},
    urlcolor={blue!50!black}
}
%USEFUL COMMANDS
\newcommand{\tr}[1]{\text{Tr}\big[#1\big]}
\newcommand{\ga}[1]{\Gamma\left(#1\right)}
\renewcommand{\d}[1]{\delta^d\Big(#1\Big)}
\newcommand{\ie}{\emph{i.e.}}
\newcommand{\ex}[1]{{\rm e}^{#1}} \def\ii{{\rm i}}
\newcommand{\Tr}{{\rm Tr}}
\newcommand{\sect}[1]{\setcounter{equation}{0}\section{#1}}
\renewcommand{\theequation}{\thesection.\arabic{equation}}

\begin{document}

\author{Liam Keegan\\ \href{mailto:liam@keegan.ch}{liam@keegan.ch}}
\title{$\mu$RHMC Code Documentation \\ \href{https://github.com/lkeegan/muRHMC}{github.com/lkeegan/muRHMC}}

\maketitle

\section{Introduction}
A simple RHMC code to simulate $n_f+n_f$ QCD with isospin chemical potential $\mu_u = -\mu_d \equiv \mu_I$, using unimproved staggered fermions and the Wilson SU(3) gauge action.

\section{QCD formulation}
\subsection{Gauge Action}
Wilson SU(3) plaquette lattice gauge action,
\begin{equation}
 S_g[U] = -\frac{\beta}{3} \sum_x \sum_{\mu<\nu} Re \Tr \left[ U_{\mu}(x)  U_{\nu}(x+\hat\mu)  U_{\mu}^{\dagger}(x+\hat\nu)  U_{\nu}^{\dagger}(x)\right]
\end{equation}
where $U_\mu(x)$ is a 3x3 complex matrix at the site $x$ in the direction $\mu$.

Defining the staple
\begin{equation}
 A_\mu(x) \equiv \sum_{\mu\neq\nu} \left\{
 U_{\nu}(x+\hat\mu) U_{\mu}^{\dagger}(x+\hat\nu)  U_{\nu}^{\dagger}(x) +
 U_{\nu}^{\dagger}(x+\hat\mu-\hat\nu) U_{\mu}^{\dagger}(x-\hat\nu)  U_{\nu}(x-\hat\nu) \right\}
\end{equation}
the terms in the action that depend on a given link $U_{\mu}(x)$ are then
\begin{equation}
 S_g^{local}[U_{\mu}(x)] = -\frac{\beta}{6} \Tr \left[ U_{\mu}(x) A_{\mu}(x) + A_{\mu}^{\dagger}(x)U_{\mu}^{\dagger}(x)  \right]
\end{equation}
which is related to the full action by
\begin{equation}
 S_g[U] = \frac{1}{12} \sum_{x} \sum_{\mu} S_g^{local}[U_{\mu}(x)]
\end{equation}

\subsection{Fermion Action}
The fermion action for $4+4$ staggered flavors with isospin chemical potential $\mu_u = -\mu_d = \mu_I$ and mass $m$ is given by
\begin{equation}
\label{eq:action_fermion}
 S_f[U] = \phi^{\dagger} [D(\mu_I,m) D^{\dagger}(\mu_I,m)]^{-1} \phi
\end{equation}
where $D(\mu_I,m)$ is the staggered lattice Dirac operator
\begin{align}
 [D(\mu_I,m)]_{xy} &= e^{\mu_I/2} U_{0}(x) \delta_{n,m-\hat0} - e^{-\mu_I/2} U_{0}^{\dagger}(x-\hat0) \delta_{n,m+\hat0} \\
 &+ \sum_{\mu=1}^{3} \eta_{\mu}(x) \left[ U_{\mu}(x) \delta_{n,m-\hat\mu} - U_{\mu}^{\dagger}(x-\hat\mu) \delta_{n,m+\hat\mu} \right]
\end{align}
and $\eta_{\mu}(x) = -1^{\sum_{\nu<\mu}x_{\nu}}$ are the space-dependent staggered equivalent of Dirac $\gamma$-matrices, i.e.
\begin{align}
 \eta_{0}(x) &= 1 \\
 \eta_{1}(x) &= -1^{x_0} \\
 \eta_{2}(x) &= -1^{x_0+x_1} \\
 \eta_{3}(x) &= -1^{x_0+x_1+x_2} \\
 \eta_{5}(x) &= -1^{x_0+x_1+x_2+x_3}
\end{align}
For $\mu_I=0$ this reduces to the standard $n_f=8$ staggered action, with the usual staggered $\gamma_5$--hermicity property $D^{\dagger} = \eta_5 D \eta_5 = -D$ for the massless case,
which allows us to write
\begin{equation}
D(0,m) D^{\dagger}(0,m) = (D(0,0) + m)(D(0,0) + m)^{\dagger} = D(0,0)D^{\dagger}(0,0) + m^2
\end{equation}
Moreover, if we split the pseudofermion field into even and odd sites we find a block diagonal operator
\begin{equation}
D(0,m) D(0,m)^{\dagger} \phi =
\begin{pmatrix}
D(0,0)D^{\dagger}(0,0) + m^2 & 0 \\
0 & D(0,0)D^{\dagger}(0,0) + m^2
\end{pmatrix}
\begin{pmatrix}
\phi^{even} \\
\phi^{odd}
\end{pmatrix}
\end{equation}
so we can find the determinant of just one of these blocks and square it, halving the size of the effective Dirac operator. Equivalently an LU preconditioning of the Dirac operator
applied to even and odd sites allows us to compute the inverse of the operator by inverting a hermitian operator applied to only even sites.

However, for non--zero $\mu_I$ the $\gamma_5$--hermicity property becomes instead $D(\mu_I)^{\dagger} = \eta_5 D(-\mu_I) \eta_5 = -D(-\mu_I)$. This means that the above matrix is
no longer block diagonal, but instead
\begin{equation}
D(\mu_I,m) [D(\mu_I,m)]^{\dagger} \phi =
\begin{pmatrix}
D(\mu_I,0)D^{\dagger}(\mu_I,0) + m^2 & m\hat D(\mu_I) \\
 m\hat D(\mu_I) & D(\mu_I,0)D^{\dagger}(\mu_I,0) + m^2
\end{pmatrix}
\begin{pmatrix}
\phi^{even} \\
\phi^{odd}
\end{pmatrix}
\end{equation}
where
\begin{equation}
 [\hat D(\mu_I)]_{xy} = \sinh(\mu_I/2)\left[U_0(x)\delta_{x,y-\hat0} + U_0(x-\hat0)\delta_{x,y+\hat0}\right]
\end{equation}
and the LU preconditioning results in a non-hermitian operator (which is ok but means BiCGstab instead of CG will need to be used for the inversion).

\section{HMC}
The hamiltonian for the HMC is
\begin{equation}
 S[P, U, \phi] = S_p[P] + S_g[U] + S_f[\phi, U]
\end{equation}
where we need to solve the classical equations of motion
\begin{align}
 \frac{dP_{\mu}(x)}{dt} &= -\frac{\partial}{\partial U_{\mu}(x)}\left(S_g[U] + S_f[\phi, U]\right) = -F_g(x,\mu) - F_f(x,\mu) \\
 \frac{dU_{\mu}(x)}{dt} &= P_{\mu}(x)
\end{align}
The following steps are required to update a gauge configuration $U$:
\begin{enumerate}
 \item Generate gaussian momenta $P$
 \item Generate $\chi$ with distribution $e^{-\chi^{\dagger}\chi}$, then set $\phi = D\chi$
 \item Integrate the force terms to generate $U', P'$
 \item Accept or reject $U'$ with probability $e^{S[P, U, \phi] - S[P', U', \phi]}$
\end{enumerate}

\subsection{Momenta}
The momenta $P_{\mu}(x)$ are defined as
\begin{equation}
 P_{\mu}(x) = \sum_{a=1}^8 p^a_{\mu}(x) T_a
\end{equation}
where $T_a$ are the generators of SU(3) and $p^a_{\mu}(x)$ are real numbers.
The action is given by
\begin{equation}
 S_p[P] = \sum_{x,\mu}\Tr\left\{\left[P_{\mu}(x)\right]^2\right\} = \frac{1}{2}\sum_{x,\mu}\sum_{a=1}^8 [p^a_{\mu}(x)]^2
\end{equation}
so we can generate momenta simply by sampling the numbers $p^a$ from the normal gaussian distribution $e^{-(p^a)^2/2}$.

The resulting matrices $P$ have $\Tr[P]=0$ and $\left\langle \Tr [P^2]\right\rangle = 4$.

\subsection{Pseudofermions}
The pseudofermion fields are 3-component complex vectors defined on each site of the lattice. To generate a field $\phi$ according to the distribution $e^{-S_f[U]}$ we
can first generate a complex vector $\chi$ with distribution $e^{-\chi^{\dagger}\chi}$, i.e. set each of
the six real numbers that make up the 3 complex components of $\chi$ to a random value $r$ sampled from the normal gaussian distribution $e^{-r^2/2}$.

Then set $\phi = D(\mu_I,m)\chi$.

\subsection{Gauge Force}
The gauge force is given by
\begin{equation}
F_g(x,\mu) = \sum F_g^a(x,\mu) T_a
\end{equation}
where
\begin{align}
F_g^a(x,\mu) &= \frac{\partial S_g[U]}{\partial U_{\mu}^{(a)}(x)} = \frac{\partial S_g^{local}[U_{\mu}(x)]}{\partial U_{\mu}^{(a)}(x)} \\
&= -\frac{\beta}{6} \frac{\partial }{\partial U_{\mu}^{(a)}(x)} \Tr \left[ U_{\mu}(x) A_{\mu}(x) + A_{\mu}^{\dagger}(x)U_{\mu}^{\dagger}(x)  \right] \\
&= \frac{i\beta}{6} \Tr \left[ T_a \left( U_{\mu}(x) A_{\mu}(x) - A_{\mu}^{\dagger}(x)U_{\mu}^{\dagger}(x)\right) \right]
\end{align}
But the quantity $(...)$ is traceless and anti-hermitian, i.e. can be written as $\sum_b c_b T_b$, so that using the trace identity $Tr[T_aT_b] = \tfrac{1}{2}\delta_{ab}$ we find
\begin{align}
F_g(x,\mu) &= \sum_{ab} \frac{i\beta}{6} \Tr \left[ T_a c_b T_b \right] T_a = \frac{i\beta}{12} \sum_a c_a T_a \\
&= \frac{i\beta}{12} \left[ U_{\mu}(x) A_{\mu}(x) - A_{\mu}^{\dagger}(x)U_{\mu}^{\dagger}(x) \right]
\end{align}

\subsection{Fermion Force}
The fermion force is given by
\begin{equation}
F_f(x,\mu) = \sum F_f^a(x,\mu) T_a
\end{equation}
where
\begin{align}
F_f^a(x,\mu) &= \frac{\partial S_f[U]}{\partial U_{\mu}^{(a)}(x)} = \phi^{\dagger} \frac{\partial }{\partial U_{\mu}^{(a)}(x)} [D(\mu_I,m) D^{\dagger}(\mu_I,m)]^{-1} \phi \\
&= \chi^{\dagger} \left[ \frac{\partial D(\mu_I,m)}{\partial U_{\mu}^{(a)}(x)} D^{\dagger}(\mu_I,m) +  D(\mu_I,m) \frac{\partial D^{\dagger}(\mu_I,m)}{\partial U_{\mu}^{(a)}(x)}  \right] \chi \\
&= -\chi^{\dagger} \frac{\partial D(\mu_I,m)}{\partial U_{\mu}^{(a)}(x)} \psi + h.c. \\
&= \Im m \left\{ \chi^{\dagger}(x) e^{\mu_I/2} T_a U_0(x)\psi(x+\hat0) + \chi^{\dagger}(x+\hat0) e^{-\mu_I/2} U_0^{\dagger}(x) T_a \psi(x) \right\} \nonumber \\
&+ \sum_{\mu=1}^{3} \eta_\mu(x) \Im m \left\{ \chi^{\dagger}(x) T_a U_\mu(x)\psi(x+\hat\mu) + \chi^{\dagger}(x+\hat\mu) U_\mu^{\dagger}(x) T_a \psi(x) \right\}
\end{align}
where
\begin{align}
\chi &\equiv [D(\mu_I,m) D^{\dagger}(\mu_I,m)]^{-1} \phi \\
\psi &\equiv -D^{\dagger}(\mu_I,m) \chi = D(-\mu_I, m) \chi
\end{align}

\subsection{Force Integration}
The integration of the force terms is done by alternating two discrete steps,
\begin{align}
  I_P(P, F, \epsilon): P & \leftarrow P - \epsilon F \\
  I_U(U, P, \epsilon): U & \leftarrow e^{i \epsilon P} U
\end{align}
which can be combined to form reversible integrators of different orders.
\subsection{Leapfrog}
The simplest integrator is the leapfrog, where an integration step of size $\epsilon$ is given by
\begin{itemize}
 \item $I_P(\epsilon/2)$
 \item $I_U(\epsilon)$
 \item $I_P(\epsilon/2)$
\end{itemize}
giving a second order integrator with one Dirac operator inversion required per step.

\subsection{OMF2}
One integration step of size $\epsilon$ is given by
\begin{itemize}
 \item $I_P(\lambda \epsilon)$
 \item $I_U(\epsilon/2)$
 \item $I_P((1-2\lambda) \epsilon)$
 \item $I_U(\epsilon/2)$
 \item $I_P(\lambda \epsilon)$
\end{itemize}
where $\lambda$ is a tunable parameter, e.g. $\lambda = 0.193$. This is also a second order integrator,
with $\sim10\times$ smaller errors than leapfrog, but requires two Dirac operator inversions per step.

\subsection{OMF4}
todo?

\section{Inverters}
\subsection{CG}
\subsection{BiCGstab}
\subsection{CG-Multishift}
\subsection{BiCGstab-Multishift}
\subsection{BiCGstab-Block}
\subsection{BiCGstab-Block-Multishift}

\section{Observables}
\subsection{Gauge Observables}
\subsubsection{Plaquette}
The plaquette is averaged over the whole lattice and normalised to 1,
\begin{equation}
 plaquette[U] = \frac{1}{L^4} \sum_x \frac{1}{6}\sum_{\mu<\nu} \frac{1}{3} Re \Tr \left[ U_{\mu}(x)  U_{\nu}(x+\hat\mu)  U_{\mu}^{\dagger}(x+\hat\nu)  U_{\nu}^{\dagger}(x)\right]
\end{equation}

\subsubsection{Polyakov Loop}
The polyakov loop is the product of links in the time direction, averaged over the spatial voume, normalised to 1,
\begin{equation}
 polyakov[U] = \frac{1}{L^3} \sum_x^{L^3} \frac{1}{3} Re \Tr \left[\prod_{t=0}^{T-1} U_{0}(x, t)\right]
\end{equation}

\subsection{Fermionic Observables}
Fermionic observables generally involve traces of the inverse of the Dirac operator, which can be estimated stochastically by
inverting gaussian noise vectors. For this purpose we define here $\phi$ as a gaussian noise vector with unit norm, $\phi^{\dagger}.\phi=1$ NB should this be 1 or 3??,
which we use as a source vector to find
\begin{equation}
 \chi = \left[D(\mu_I,m) D^{\dagger}(\mu_I,m)\right]^{-1} \phi = -\left[D(\mu_I,m) D(-\mu_I,-m)\right]^{-1} \phi
\end{equation}
and also
\begin{equation}
 \psi = -\left[D(\mu_I,m)\right]^{-1} \phi = D(-\mu_I,-m) \chi
\end{equation}
from which we can construct estimators of various fermionic observables.

\subsubsection{Quark condensate}
\begin{equation}
 condensate = -\left\langle \phi^{\dagger}.\psi \right\rangle
\end{equation}

\subsubsection{Pion susceptibility}
\begin{equation}
 susceptibility = \left\langle \phi^{\dagger}.\chi \right\rangle
\end{equation}

\subsubsection{Isospin density}
\begin{equation}
 density = 2 \Re e \left\langle \chi^{\dagger}.\left(\frac{\partial D(\mu_I,m)}{\partial\mu_I}\psi\right) \right\rangle
\end{equation}
where
\begin{equation}
 \left[\frac{\partial D(\mu_I,m)}{\partial\mu_I}\psi\right]_x =\frac{\mu_I}{4}\left[ e^{\mu_I/2} U_0(x) \psi(x+\hat0) + e^{-\mu_I/2} U^{\dagger}_0(x-\hat0) \psi(x-\hat0)\right]
\end{equation}

\newpage
\appendix
\section{SU(3) Matrix Algebra}

\subsection{Generators}
The generators of SU(3) are the set of traceless $3\times 3$ hermitian matrices $T_a$, where $a=1,2,\ldots,8$, with the properties
\begin{equation}
\tr{T_a}=0, \quad T_a^{\dagger}=T_a, \quad \tr{T_a T_b} = \tfrac{1}{2}\delta_{ab}
\end{equation}
\begin{equation}
[T_a,T_b] = T_a T_b - T_b T_a = f_{abc} T_c
\end{equation}
where $f_{abc}$ is real and antisymmetric in all indices. An SU(3) matrix $U$ can be written as
\begin{equation}
U = e^{i \omega_a T_a}
\end{equation}
where $\omega_a$ are real numbers.

\subsection{Differentiation}
Differentiation w.r.t an element of the algebra can be defined as
\begin{equation}
\frac{\partial F(U)}{\partial U^{(a)}} \equiv \frac{\partial}{\partial \omega} \left. F(e^{i \omega T_a} U) \right|_{\omega=0}
\end{equation}
which gives for SU(3) matrices $U, V, W,$
\begin{align}
\frac{\partial}{\partial U^{(a)}} \left( V U W \right) &= i V T_a U W \\
\frac{\partial}{\partial U^{(a)}} \left( V U^{\dagger} W \right) &= -i V U T_a W
\end{align}

\end{document}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Author: A. Herrera Poyatos
% Conclusion
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% !TEX root = ../main.tex

\chapter{Conclusion and future work}

\section{Conclusion}

In this work we have obtained novel results in the fields of cyclotomic polynomials and commutative monoids. We have also developed new algorithms for detecting Kronecker polynomials after studying the literature available on this topic. Finally, we have presented two packages for drawing graphs associated to numerical semigroups.

The original objectives have been successfully achieved. Moreover, we have reached new objectives that were not initially considered when we proposed this thesis. Our focus was originally limited to cyclotomic numerical semigroups but, as a side effect of our research on this topic, we have produced a series of new results and tools that are interesting on their own. This has lead to the development of four separate publications, one of which have already been accepted by a peer-reviewed journal in number theory.

\section{Future work}

The publication on exponent sequences of cyclotomic numerical semigroups (Chapter \ref{chap:ns-cyclotomic}) is still a draft. We expect to generalize our results on cyclotomic exponent sequences to arbitrary numerical semigroups. That is, we are getting rid of the assumption of the finiteness of the exponent sequence. We hope that after these changes, Chapter \ref{chap:ns-cyclotomic} is ready to be separately published soon.

Our work on cyclotomic numerical semigroups generates expectation on a possible proof of the main conjecture of this area, which states that cyclotomic numerical semigroups are complete intersections. Nonetheless, our results only reach the surface of this conjecture, which is expected to be a very deep result. Relating a property of the polynomial of a numerical semigroup with the cardinality of its minimal presentation would be a very important advance in the theory of numerical semigroups.

The study of algorithms to detect Kronecker polynomials has been more productive than we expected. We were able to come up with new proposals that perform better than the current algorithms of the state of the art. We are going to work more on these proposals with the expectations of writing a journal publication on this topic.

Finally, the GAP packages \texttt{dot-numericalsgps} and \texttt{FrancyMonoid} will be continuously improved. We will add new functionality as new graphs and similar structures arise in the theory of numerical semigroups.


\subsection{Amortized Analysis}

\begin{frame}{Dynamic Arrays}{Amortized Analysis}
  \vspace*{-1.0em}
  \begin{figure}[!t]%
    \def\FSAsize{16}\def\FSAelements{11}%
    \def\FSAcopy{0}\def\FSAdelete{0}\def\FSAinsert{0}%
    \def \FSAcopyarrow{1}%
    \def\FSAlabelsize{Size ${\color{MainA}s_i}$}%
    \def\FSAlabelcapacity{Capacity ${\color{MainA}c_i}$}%
    \input{Images/FixedSizeArray_Sizes.tikz}%
    \vspace*{-0.5em}%
    \caption{Static array with capacity $c_i$}%
    \label{fig:array_amortized analysis}%
  \end{figure}%
  \vspace*{-1.0em}
  \textbf{Notation:}%
  \begin{itemize}%
    \item
      We have $n$ instructions $O = \{O_1,\dots,O_n\}$
    \item
      The {\color{MainA}size} after operation $i$ is
      {\color{MainA}$s_i$},
      with ${\color{MainA}s_0} := 0$
    \item
      The {\color{MainA}capacity} after operation $i$ is
      {\color{MainA}$c_i$},
      with ${\color{MainA}c_0} := 0$
    \item
      The {\color{MainA}cost} of operation $i$ is
      ${\color{MainA}\mathrm{cost}(O_i)}$
      (previously named $T_i$)\\[0.5em]
      \begin{tabular}{lll}
        Reallocation: &
        ${\color{MainA}\mathrm{cost}(O_i)}
          \leq A \cdot {\color{MainA}s_i},$ & {}\\
        Insert / Delete (Update): &
        ${\color{MainA}\mathrm{cost}(O_i)}
          \leq A,$ & {}
      \end{tabular}
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dynamic Arrays}{Amortized Analysis - Example}
  \vspace*{-1.0em}
  \begin{table}[!h]
%    \caption{Dynamic Array with%
%      $C_\textbf{ext} = 3, \; C_\text{shr} = \frac{2}{3}$}%
    \label{tab:amortized_analysis_introduction}%
    \begin{tabular}{|l|l|l|l|l|c|}
      \bottomrule
      \multicolumn{3}{|c|}{Operation} &
      Size {\color{MainA}$s_i$} &
      Capactity {\color{MainA}$c_i$} &
      $\begin{array}{c}
        \text{Costs}\\
        {\color{MainA}\mathrm{cost}(O_i)}
      \end{array}$\\
      \toprule
      $O_1$ & append & {\color{MainA}realloc.} &
      $s_1$ & {\color{MainA}$c_1$} &
      ${\color{MainA}A \cdot s_1}$\\
      $O_2$ & append & {} &
      $s_2$ & $c_2 = c_1$ & $A\cdot 1$\\
      $O_3$ & append & {} &
      $s_3$ & $c_3 = c_1$ & $A\cdot 1$\\
      $O_4$ & remove & {} &
      $s_4$ & $c_4 = c_1$ & $A\cdot 1$\\
      \midrule
      $O_5$ & remove & {\color{MainA}realloc.} &
      $s_5$ & {\color{MainA}$c_5$} &
      ${\color{MainA}A \cdot s_5}$\\
      $O_6$ & append & {} &
      $s_6$ & $c_6 = c_5$ & $A\cdot 1$\\
      $O_7$ & remove & {} &
      $s_7$ & $c_7 = c_5$ & $A\cdot 1$\\
      $O_8$ & append & {} &
      $s_8$ & $c_8 = c_5$ & $A\cdot 1$\\
      \midrule
      $O_9$ & append & {\color{MainA}realloc.} &
      $s_9$ & {\color{MainA}$c_9$} &
      ${\color{MainA}A \cdot s_9}$\\
      $\dots$ & $\dots$ & $\dots$ & $\dots$ & $\dots$ & $\dots$\\
      $O_n$ & append & {} &
      $s_n$ & $c_n$ & $A\cdot 1$\\
      \bottomrule
    \end{tabular}
  \end{table}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dynamic Arrays}{Amortized Analysis - Example}
  \textbf{Implementation:}
  \begin{itemize}
    \item
      If {\color{MainA}$O_i$} is an \textit{append} operation and
      ${\color{MainA}s_{i-1}} = {\color{MainA}c_{i-1}}$:\\
      $\Rightarrow$ Resize array to
      ${\color{MainA}c_i}
        = \left\lfloor\frac{3}{2} {\color{MainA}s_i} \right\rfloor
        = \mathrm{floor}\left(\frac{3}{2}{\color{MainA}s_i}\right)$\\
      $\Rightarrow {\color{MainA}cost(O_i)}
        = A \cdot {\color{MainA}s_i}$
  \end{itemize}
  \begin{figure}[!h]
    \def\FSAsize{7}\def\FSAelements{7}%
    \def\FSAcopy{0}\def\FSAdelete{0}\def\FSAinsert{0}%
    \def\FSAcopyarrow{0}%
    \def\FSAlabelsize{${\color{MainA}s_{i-1}} = 7$}%
    \def\FSAlabelcapacity{%
      ${\color{MainA}c_{i-1}}%
        = {\color{MainA}s_{i-1}} = 7$}%
    \input{Images/FixedSizeArray_Sizes.tikz}%
    %
    \hspace*{0.5em}\raisebox{2em}{$\Rightarrow$}\hspace*{0.5em}%
    %
    \def\FSAsize{12}\def\FSAelements{0}%
    \def\FSAcopy{7}\def\FSAdelete{0}\def\FSAinsert{1}%
    \def\FSAlabelsize{${\color{MainA}s_i}
      = {\color{MainA}s_{i-1} + 1}=8$}%
    \def\FSAlabelcapacity{$12={\color{MainA}c_i}
      = \lfloor\frac{3}{2}{\color{MainA}s_i}\rfloor = 8$}%
    \input{Images/FixedSizeArray_Sizes.tikz}%
    \caption{\textit{Append} operation with reallocation}
    \label{fig:dynamic_fields:amortized_analysis:append}
  \end{figure}
\begin{itemize}
  \item[] \textbf{Result:} after operation we have $c_i=\frac{3}{2}\cdot s_i$
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dynamic Arrays}{Amortized Analysis - Example}
  \textbf{Implementation:}
  \begin{itemize}
    \item
    If {\color{MainA}$O_i$} is an \textit{remove} operation and
    ${\color{MainA}s_{i-1}}
      \leq \frac{1}{3} {\color{MainA}c_{i-1}}$:\\
    $\Rightarrow$ Resize array to
    ${\color{MainA}c_i}
      = \left\lfloor\frac{3}{2} {\color{MainA}s_i} \right\rfloor
      = \mathrm{floor}\left(\frac{3}{2} {\color{MainA}s_i}\right)$\\
    $\Rightarrow {\color{MainA}cost(O_i)}
      = A \cdot {\color{MainA}s_i}$
  \end{itemize}
  \begin{figure}[!h]
    \def\FSAsize{15}\def\FSAelements{5}%
    \def\FSAcopy{0}\def\FSAdelete{0}\def\FSAinsert{0}%
    \def\FSAcopyarrow{0}%
    \def\FSAlabelsize{${\color{MainA}s_{i-1}} = 5$}%
    \def\FSAlabelcapacity{%
      ${\color{MainA}c_{i-1}} = 15%
      \geq {3\cdot\color{MainA}s_{i-1}}$}%
    \input{Images/FixedSizeArray_Sizes.tikz}%
    %
    \hspace*{0.5em}\raisebox{2em}{$\Rightarrow$}\hspace*{0.5em}%
    %
    \def\FSAsize{6}\def\FSAelements{0}%
    \def\FSAcopy{4}\def\FSAdelete{1}\def\FSAinsert{0}%
    \def\FSAlabelsize{${\color{MainA}s_i}
      = {\color{MainA}s_{i-1}- 1} $}%
    \def\FSAlabelcapacity{$6={\color{MainA}c_i}
      = \frac{3}{2}{\color{MainA}s_i}$ = 4}%
    \input{Images/FixedSizeArray_Sizes.tikz}%
    \caption{\textit{Remove} operation with reallocation}
    \label{fig:dynamic_fields:amortized_analysis:remove}
  \end{figure}
  \begin{itemize}
  \item[] \textbf{Result:} after operation we have again $c_i=\frac{3}{2}\cdot s_i$
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dynamic Arrays}{Amortized Analysis - Proof}
  \textbf{Idea for proof:}
  \begin{itemize}
    \item
      Expensive are only operations where reallocations are necessary
    \item
      If we just reallocated, it takes some time until the next reallocation is required.
    \item \textbf{Assumption:}
      After a costly \textit{reallocation} of size $X$ we have at least $X$ operations of runtime $O(1)$
    \item
      \textbf{Then:} Total cost of {\color{MainA}$n$} operations is maximally {\color{MainA}$2 \cdot n$}
  \end{itemize}
  %% \[\Rightarrow {\color{MainA}\mathrm{cost}(O_i)} \dots
  %%   {\color{MainA}\mathrm{cost}(O_{i + n})} \leq 2 \, n \cdot C,
  %%   \hspace*{0.5em}n \in \mathbb{N}\]
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dynamic Arrays}{Amortized Analysis - Proof}
  \vspace*{-1.0em}%
  \begin{table}[!h]%
    \caption{Dynamic Array with %
      $C_\text{ext} = \frac{3}{2}$}%
    \label{tab:amortized_analysis_limit}%
    \vspace*{-0.2em}%
    \begin{tabular}{|l|l|l|l|l|c|@{}l}
\cline{1-6}%      \cmidrule[\heavyrulewidth]{1-5}\addlinespace[-\belowrulesep]
      \multicolumn{3}{|c|}{$\begin{array}{c}
          \text{Operation}\\
        \end{array}$} &
      $\begin{array}{c}
        \text{Size}\\
        {\color{MainA}s_i}
      \end{array}$ &
      $\begin{array}{c}
        \text{Capacity}\\
        {\color{MainA}c_i}
      \end{array}$ &
      $\begin{array}{c}
        \text{Costs}\\
        {\color{MainA}\mathrm{cost}(O_i)}
      \end{array}$\\
\cline{1-6}
      $O_1$ &app. & {\color{MainA}realloc.} &
      $s_1$ & {\color{MainA}$c_1=4$} &
      ${\color{MainA}C_1 \cdot s_1}$ &
      \raisebox{-0.5em}{\multirow{4}{*}{%
        $\left.\begin{array}{@{}l@{}}\\[4.0em]\end{array}\right\rbrace
        \begin{array}{@{}l@{}}
           \text{distance}\\
           4 \geq \left\lfloor\dfrac{{\color{MainA}s_1}}{2}\right\rfloor
        \end{array}$%
      }}\\
      $O_2$ &app.& {} & $s_2$ & $c_2 = c_1$ & $C_2$\\
      $O_3$ &app.& {} & $s_3$ & $c_3 = c_1$ & $C_2$\\
      $O_4$ &app.& {} & $s_4$ & $c_4 = c_1$ & $C_2$\\
\cline{1-6}%
      $O_5$ &app.& {\color{MainA}realloc.} &
      $s_5$ & {\color{MainA}$c_5 = \lfloor\frac{3}{2} s_5\rfloor=7$} &
      ${\color{MainA}C_1 \cdot s_5}$ &
      \raisebox{-0.5em}{\multirow{3}{*}{%
        $\left.\begin{array}{@{}l@{}}\\[3.05em]\end{array}\right\rbrace
        \begin{array}{@{}l@{}}
          \text{distance}\\
           3\geq\left\lfloor\dfrac{{\color{MainA}s_5}}{2}\right\rfloor
        \end{array}$%
      }}\\
      $O_6$ &app.& {} & $s_6$ & $c_6 = c_5$ & $C_2$\\
      $O_7$ &app.& {} & $s_7$ & $c_7 = c_5$ & $C_2$\\
      \cline{1-6}
      $O_8$ &app.& {\color{MainA}realloc.} &
      $s_8$ & {\color{MainA}$c_8 = \frac{3}{2} s_8=12$} &
      ${\color{MainA}C_1 \cdot s_8}$\\
      $\dots$ & $\dots$ & $\dots$ & $\dots$ & $\dots$& $\dots$\\
      \cline{1-6}
    \end{tabular}
  \end{table}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dynamic Arrays}{Amortized Analysis - Proof}
  \textbf{To show:}
  \begin{itemize}
    \item
      \textbf{Lemma:}
      If a \textit{reallocation} occurs at ${\color{MainA}O_i}$
      the nearest \textit{reallocation} is at ${\color{MainA}O_j}$
      with $j - i > \dfrac{{\color{MainA}s_i}}{2}$
    \item
      \textbf{Corollary:}
      ${\color{MainA}\mathrm{cost}(O_1)}+\dots
        +{\color{MainA}\mathrm{cost}(O_n)} \leq 4 \, A \cdot n$
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dynamic Arrays}{Proof: Worst Case Same Operation}
  \vspace*{-1.5em}
  \begin{table}[!h]
    \caption{Case 1: $\frac{1}{2} {\color{MainA}s_i}$ \textit{appends}}
    \label{tab:dynamic_fields:amortized_analysis:proof_add}
    \begin{tabularx}{\linewidth}{@{}r@{}c@{}lc@{}l}
      {} & {} & \multicolumn{1}{c}{Array} & Costs\\
      \cmidrule{1-4}
      {\color{MainA}$O_i$}: & {} &
      \def\FSAsize{9}\def\FSAelements{0}%
      \def\FSAcopy{5}\def\FSAdelete{0}\def\FSAinsert{1}%
      \def\FSAcopyarrow{1}%
      \def\FSAlabelsize{${\color{MainA}s_i}$}%
      \def\FSAlabelcapacity{${\color{MainA}c_i}$}%
      \raisebox{-2em}{\begin{adjustbox}{width=11.25em}%
        \input{Images/FixedSizeArray_Sizes.tikz}%
      \end{adjustbox}} &
      $\begin{array}{c}
        \text{{\color{MainA}reallocation}}\\
        {\color{MainA}A \cdot s_i}\;\text{(linear)}
      \end{array}$\\
      {\color{MainA}$O_{i+1}$}: & $\hspace*{0.5em}$ &
      \def\FSAsize{9}\def\FSAelements{6}%
      \def\FSAcopy{0}\def\FSAdelete{0}\def\FSAinsert{1}%
      \begin{adjustbox}{width=11.25em}%
        \input{Images/FixedSizeArray.tikz}%
      \end{adjustbox} &
      $A$ (constant) &
      \multirow{3}{*}{$\left.
          \begin{array}{@{}c@{}}\\[4.0em]\end{array}
        \right\rbrace
        \begin{array}{@{}c@{}}
          \dfrac{\color{MainA}s_i}{2}\\
          \text{times}
        \end{array}$}\\
      {\color{MainA}$O_{i+2}$}: & {} &
      \def\FSAsize{9}\def\FSAelements{7}%
      \def\FSAcopy{0}\def\FSAdelete{0}\def\FSAinsert{1}%
      \begin{adjustbox}{width=11.25em}%
        \input{Images/FixedSizeArray.tikz}%
      \end{adjustbox} &
      $A$ (constant)\\
      {\color{MainA}$O_{i+3}$}: & {} &
      \def\FSAsize{9}\def\FSAelements{8}%
      \def\FSAcopy{0}\def\FSAdelete{0}\def\FSAinsert{1}%
      \begin{adjustbox}{width=11.25em}%
        \input{Images/FixedSizeArray.tikz}%
      \end{adjustbox} &
      $A$ (constant)\\
      \cmidrule{1-4}
      {\color{MainA}$O_j$}: & {} &
      \def\FSAsize{11}\def\FSAelements{0}%
      \def\FSAcopy{9}\def\FSAdelete{0}\def\FSAinsert{1}%
      \begin{adjustbox}{width=13.75em}%
        \input{Images/FixedSizeArray.tikz}%
      \end{adjustbox}$\hspace*{0.25em}\cdots$ &
      $\begin{array}{c}
        \text{{\color{MainA}reallocation}}\\
        \llap{$A \cdot$} {\color{MainA}s_j}\;\text{(earliest realloc)}
      \end{array}$\\
    \end{tabularx}
  \end{table}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dynamic Arrays}{Amortized Analysis - Proof}
  \vspace*{-1.5em}
  \begin{table}[!h]
    \caption{Case 2: $\frac{1}{2} {\color{MainA}s_i}$ \textit{removes}}
    \label{tab:dynamic_fields:amortized_analysis:proof_remove}
    \begin{tabularx}{\linewidth}{@{}r@{}c@{}lc@{}l}
      {} & {} & \multicolumn{1}{c}{Array} & Costs\\
      \cmidrule{1-4}
      {\color{MainA}$O_i$}: & {} &
      \def\FSAsize{9}\def\FSAelements{0}%
      \def\FSAcopy{6}\def\FSAdelete{0}\def\FSAinsert{0}%
      \def\FSAcopyarrow{1}%
      \def\FSAlabelsize{${\color{MainA}s_i}$}%
      \def\FSAlabelcapacity{${\color{MainA}c_i}$}%
      \raisebox{-2em}{\begin{adjustbox}{width=11.25em}%
        \input{Images/FixedSizeArray_Sizes.tikz}%
      \end{adjustbox}} &
      $\begin{array}{c}
        \text{{\color{MainA}reallocation}}\\
        {\color{MainA}A \cdot s_i}\;\text{(linear)}
      \end{array}$\\
      {\color{MainA}$O_{i+1}$}: & $\hspace*{0.5em}$ &
      \def\FSAsize{9}\def\FSAelements{5}%
      \def\FSAcopy{0}\def\FSAdelete{1}\def\FSAinsert{0}%
      \begin{adjustbox}{width=11.25em}%
        \input{Images/FixedSizeArray.tikz}%
      \end{adjustbox} &
      $A$ (constant) &
      \multirow{3}{*}{$\left.
          \begin{array}{@{}c@{}}\\[3.0em]\end{array}
        \right\rbrace
        \begin{array}{@{}c@{}}
          \dfrac{\color{MainA}s_i}{2}\\
          \text{times}
        \end{array}$}\\
      {\color{MainA}$O_{i+2}$}: & {} &
      \def\FSAsize{9}\def\FSAelements{4}%
      \def\FSAcopy{0}\def\FSAdelete{1}\def\FSAinsert{0}%
      \begin{adjustbox}{width=11.25em}%
        \input{Images/FixedSizeArray.tikz}%
      \end{adjustbox} &
      $A$ (constant)\\
      {\color{MainA}$O_{i+3}$}: & {} &
      \def\FSAsize{9}\def\FSAelements{3}%
      \def\FSAcopy{0}\def\FSAdelete{1}\def\FSAinsert{0}%
      \begin{adjustbox}{width=11.25em}%
        \input{Images/FixedSizeArray.tikz}%
      \end{adjustbox} &
      $A$ (constant)\\
      \cmidrule{1-4}
      {\color{MainA}$O_j$}: & {} &
      \def\FSAsize{3}\def\FSAelements{0}%
      \def\FSAcopy{2}\def\FSAdelete{1}\def\FSAinsert{0}%
      \begin{adjustbox}{width=3.75em}%
        \input{Images/FixedSizeArray.tikz}%
      \end{adjustbox} &
      $\begin{array}{c}
        \text{{\color{MainA}reallocation}}\\
        {\color{MainA}A \cdot s_j}\;\text{(earliest reallocation)}
      \end{array}$\\
    \end{tabularx}
  \end{table}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dynamic Arrays}{Amortized Analysis}
  \textbf{Proof of lemma:}
  \begin{itemize}
  \item
    If a reallocation happens at {\color{MainA}$O_i$} and then again at {\color{MainA}$O_j$}, then {\color{MainA}$j - i\geq s_i/2$}
    \item
      After operation {\color{MainA}$O_i$} the capacity is
      \[
        {\color{MainA}c_i}
        = \left\lfloor\frac{3}{2}\cdot{\color{MainA}s_i}\right\rfloor
      \]
    \item
      Lets consider a operation {\color{MainA}$O_i$} to {\color{MainA}$O_k$} with
      $k -i \leq \dfrac{\color{MainA}s_i}{2}$:
      \begin{itemize}
        \item
          Case 1: Since the \textit{reallocation} we have inserted at
          maximum
          $\mathrm{floor}\left(
            \frac{1}{2} \cdot {\color{MainA}s_i}
          \right)$ elementsation
          \vspace{0.5em}\\
          \begin{tabularx}{\linewidth}{Xr}
            ${\color{MainA}s_k}
              \leq {\color{MainA}s_i} +
                \left\lfloor
                  \dfrac{\color{MainA}s_i}{2}
                  \vphantom{\dfrac{3}{2}}
                \right\rfloor
              = \left\lfloor\dfrac{3}{2} {\color{MainA}s_i}\right\rfloor
              = c_i$ &
              {\color{MainA}no reallocation needed}
          \end{tabularx}
      \end{itemize}
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dynamic Arrays}{Amortized Analysis}
  \textbf{Proof of lemma - continued:}
      \begin{itemize}
        \item
          Case 2: Since the \textit{reallocation} we have removed at
          maximum $\left\lfloor\frac{1}{2} {\color{MainA}s_i} \right\rfloor$
          elements
          \vspace{0.5em}\\
          \begin{tabularx}{\linewidth}{Xr}
           ${\color{MainA}s_k}
             \geq {\color{MainA}s_i} -
               \left\lfloor
                 \dfrac{\color{MainA}s_i}{2}
                 \vphantom{\dfrac{1}{2}}
               \right\rfloor
             = \left\lceil\dfrac{1}{2} {\color{MainA}s_i}\right\rceil$ &
           \multirow{2}{*}{\color{MainA}no reallocation needed}\\[1.0em]
           $\Rightarrow 3 \cdot {\color{MainA}s_k}
             \geq \left\lceil\dfrac{3}{2} {\color{MainA}s_i}\right\rceil
             \geq \left\lfloor\dfrac{3}{2} {\color{MainA}s_i}\right\rfloor
             = {\color{MainA}c_i}$
          \end{tabularx}
      \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dynamic Arrays}{Amortized Analysis - Proof of Corollary}
  \begin{block}{Corollary:}
    \begin{displaymath}
      \mathrm{cost}(O_1)+\dots+\mathrm{cost}(O_n) \leq 4 A \cdot n
    \end{displaymath}
  \end{block}
  \begin{itemize}
    \item
      Let the \textit{reallocations} be at operations
      \begin{math}
        {\color{MainA}\mathrm{cost}(O_{i_1})},\dots,
        {\color{MainA}\mathrm{cost}(O_{i_m})}
      \end{math}
    \item
      The {\color{MainA}cost} of all \textit{reallocations} are
      \begin{math}
        A \cdot \left(
          {\color{MainA}s_{i_1}}+\dots+{\color{MainA}s_{i_m}}
        \right)
      \end{math}
    \item
      With the lemma we know:
      \begin{displaymath}
        i_2-i_1 > \frac{\color{MainA}s_{i_1}}{2},\hspace{1.0em}
        i_3-i_2 > \frac{\color{MainA}s_{i_2}}{2},\hspace{1.0em}
        \dotsc,\hspace{1.0em}
        i_m-i_{m-1} > \frac{\color{MainA}s_{i_{m-1}}}{2}
      \end{displaymath}
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dynamic Arrays}{Amortized Analysis - Proof of Corollary}
  \begin{itemize}
    \item
      We can conclude that:
      \begin{align*}
        i_2-i_1 &> \frac{\color{MainA}s_{i_1}}{2} &
        \Rightarrow &&
        {\color{MainA}s_{i_1}} &< 2 (i_2 - i_1)\\
        i_3-i_2 &> \frac{\color{MainA}s_{i_2}}{2} &
        \Rightarrow &&
        {\color{MainA}s_{i-2}} &< 2 (i_3 - i_2)\\
        {} & {} & \vdots\;\;\\
        i_m-i_{m-1} &> \frac{\color{MainA}s_{i_{m-1}}}{2} &
        \Rightarrow &&
        {\color{MainA}s_{i_{m-1}}} &< 2 (i_m - i_{m-1})\\
        {} & {} & {} &&
        {\color{MainA}s_{i_m}} &\leq n
        \hspace*{3.0em}\text{(trivial)}
      \end{align*}
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dynamic Arrays}{Amortized Analysis - Proof of Corollary}
  \begin{itemize}
    \item
      The {\color{MainA}costs} of all reallocations are:
      \begin{align*}
        {\color{MainA}cost}(realloc.)
          & = A \cdot \left(
              {\color{MainA}s_{i_1}}+\dots+{\color{MainA}s_{i_m}}
            \right)\\
          & < A \cdot \left(
              2(i_2-i_1)+2(i_3-i_2)+\dots+2(i_m-i_{m-1}) + n
            \right)\\
          & \hspace{1.5em} = A \cdot \left(
              2 (i_m - i_1) + n
            \right)\\
          & \leq A \cdot \left(2n + n\right) = 3A \cdot n
      \end{align*}
    \item
      Additionally we have to consider the respective constant costs for
      a normal append or remove ({\color{MainA}$\leq A \cdot n$}) therefore in total {\color{MainA}$\leq 4 \cdot A \cdot n$}
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dynamic Arrays}{Amortized Analysis - Alternate Proof of Corollary}
  \vspace*{-1.5em}
  \begin{table}[!h]
    \caption{Case 1: $\frac{1}{2} {\color{MainA}s_i}$ \textit{appends}}
    \label{tab:dynamic_fields:amortized_analysis:proof_corollary_add}
    \begin{tabularx}{\linewidth}{@{}r@{}c@{}lc@{}l}
      {} & {} & \multicolumn{1}{c}{Array} & Costs\\
      \cmidrule{1-4}
      {\color{MainA}$O_i$}: & {} &
      \def\FSAsize{9}\def\FSAelements{0}%
      \def\FSAcopy{5}\def\FSAdelete{0}\def\FSAinsert{1}%
      \def\FSAcopyarrow{1}%
      \def\FSAlabelsize{${\color{MainA}s_i}$}%
      \def\FSAlabelcapacity{${\color{MainA}c_i}$}%
      \raisebox{-2em}{\begin{adjustbox}{width=11.25em}%
        \input{Images/FixedSizeArray_Sizes.tikz}%
      \end{adjustbox}} &
      $\begin{array}{c}
        \text{{\color{MainA}reallocation}}\\
        {\color{MainA}A \cdot s_i}\;\text{(linear)}
      \end{array}$\\
      {\color{MainA}$O_{i+1}$}: & $\hspace*{0.5em}$ &
      \def\FSAsize{9}\def\FSAelements{6}%
      \def\FSAcopy{0}\def\FSAdelete{0}\def\FSAinsert{1}%
      \begin{adjustbox}{width=11.25em}%
        \input{Images/FixedSizeArray.tikz}%
      \end{adjustbox} &
      $A$ (constant) &
      \multirow{3}{*}{$\left.
          \begin{array}{@{}c@{}}\\[4.0em]\end{array}
        \right\rbrace
        \begin{array}{@{}c@{}}
          \dfrac{\color{MainA}s_i}{2}\\
          \text{times}
        \end{array}$}\\
      {\color{MainA}$O_{i+2}$}: & {} &
      \def\FSAsize{9}\def\FSAelements{7}%
      \def\FSAcopy{0}\def\FSAdelete{0}\def\FSAinsert{1}%
      \begin{adjustbox}{width=11.25em}%
        \input{Images/FixedSizeArray.tikz}%
      \end{adjustbox} &
      $A$ (constant)\\
      {\color{MainA}$O_{i+3}$}: & {} &
      \def\FSAsize{9}\def\FSAelements{8}%
      \def\FSAcopy{0}\def\FSAdelete{0}\def\FSAinsert{1}%
      \begin{adjustbox}{width=11.25em}%
        \input{Images/FixedSizeArray.tikz}%
      \end{adjustbox} &
      $A$ (constant)\\
      \cmidrule{1-4}
      {\color{MainA}$O_j$}: & {} &
      \def\FSAsize{11}\def\FSAelements{0}%
      \def\FSAcopy{9}\def\FSAdelete{0}\def\FSAinsert{1}%
      \begin{adjustbox}{width=13.75em}%
        \input{Images/FixedSizeArray.tikz}%
      \end{adjustbox}$\hspace*{0.25em}\cdots$ &
      $\begin{array}{c}
        \text{{\color{MainA}reallocation}}\\
        {\color{MainA}A \cdot s_j}\\
        \text{(earliest realloc.)}
      \end{array}$\\
    \end{tabularx}
  \end{table}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dynamic Arrays}{Amortized Analysis - Alternate Proof of Corollary}
  \begin{itemize}
    \item
      Total costs of {\color{MainA}$A \cdot \frac{3}{2} \cdot s_i$}
      for {\color{MainA}$\frac{s_i}{2} + 1$} operations
    \item
      Cost per operation:
      \begin{displaymath}
        \frac{
          \frac{3}{2} A \cdot {\color{MainA}s_i}
        }{
          \frac{1}{2}{\color{MainA}s_i} + 1
        }
        \leq
        \frac{
          \frac{3}{2} A \cdot {\color{MainA}s_i}
        }{
          \frac{1}{2}{\color{MainA}s_i}
        }
        = 3 \cdot A = \text{const.}
      \end{displaymath}
    \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dynamic Arrays}{Amortized Analysis - Alternate Proof of Corollary}
  \begin{table}[!h]
    %\caption{Case 2: $\frac{1}{2} {\color{MainA}s_i}$ \textit{removes}}
    \label{tab:dynamic_fields:amortized_analysis:proof_corollary_remove}
    \begin{tabularx}{\linewidth}{@{}r@{}c@{}lc@{}l}
      {} & {} & \multicolumn{1}{c}{Array} & Costs\\
      \cmidrule{1-4}
      {\color{MainA}$O_i$}: & {} &
      \def\FSAsize{9}\def\FSAelements{0}%
      \def\FSAcopy{6}\def\FSAdelete{0}\def\FSAinsert{0}%
      \def\FSAcopyarrow{1}%
      \def\FSAlabelsize{${\color{MainA}s_i}$}%
      \def\FSAlabelcapacity{${\color{MainA}c_i}$}%
      \raisebox{-2em}{\begin{adjustbox}{width=11.25em}%
        \input{Images/FixedSizeArray_Sizes.tikz}%
      \end{adjustbox}} &
      $\begin{array}{c}
        \text{{\color{MainA}reallocation}}\\
        {\color{MainA}A \cdot s_i}\;\text{(linear)}
      \end{array}$\\
      {\color{MainA}$O_{i+1}$}: & $\hspace*{0.5em}$ &
      \def\FSAsize{9}\def\FSAelements{5}%
      \def\FSAcopy{0}\def\FSAdelete{1}\def\FSAinsert{0}%
      \begin{adjustbox}{width=11.25em}%
        \input{Images/FixedSizeArray.tikz}%
      \end{adjustbox} &
      $A$ (constant) &
      \multirow{3}{*}{$\left.
          \begin{array}{@{}c@{}}\\[3.0em]\end{array}
        \right\rbrace
        \begin{array}{@{}c@{}}
          \dfrac{\color{MainA}s_i}{2}\\
          \text{times}
        \end{array}$}\\
      {\color{MainA}$O_{i+2}$}: & {} &
      \def\FSAsize{9}\def\FSAelements{4}%
      \def\FSAcopy{0}\def\FSAdelete{1}\def\FSAinsert{0}%
      \begin{adjustbox}{width=11.25em}%
        \input{Images/FixedSizeArray.tikz}%
      \end{adjustbox} &
      $A$ (constant)\\
      {\color{MainA}$O_{i+3}$}: & {} &
      \def\FSAsize{9}\def\FSAelements{3}%
      \def\FSAcopy{0}\def\FSAdelete{1}\def\FSAinsert{0}%
      \begin{adjustbox}{width=11.25em}%
        \input{Images/FixedSizeArray.tikz}%
      \end{adjustbox} &
      $A$ (constant)\\
      \cmidrule{1-4}
      {\color{MainA}$O_j$}: & {} &
      \def\FSAsize{3}\def\FSAelements{0}%
      \def\FSAcopy{2}\def\FSAdelete{1}\def\FSAinsert{0}%
      \begin{adjustbox}{width=3.75em}%
        \input{Images/FixedSizeArray.tikz}%
      \end{adjustbox} &
      $\begin{array}{c}
        \text{{\color{MainA}reallocation}}\\
        {\color{MainA}A \cdot s_j}\;\text{(linear)}
      \end{array}$\\
    \end{tabularx}
  \end{table}
  \begin{itemize}
    \item Runtime analysis for local worst-case sequence
    \item Same total cost as previous slide
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

%% \begin{frame}{Dynamic Arrays}{Amortized Analysis - Alternate Proof of Corollary}
%%   \begin{itemize}
%%     \item
%%       For $\frac{\color{MainA}s_i}{2} + 1$ elements we have total
%%       {\color{MainA}costs} of:\\
%%       \hspace{1.5em}With $C_2 = C_1 = C \in \mathbb{R}$
%%       \begin{displaymath}
%%         C_2 \cdot \frac{\color{MainA}s_i}{2}
%%         + C_1 \cdot {\color{MainA}s_i}
%%         = \frac{3}{2} C \cdot {\color{MainA}s_i}
%%       \end{displaymath}
%%     \item
%%       Cost per operation:
%%       \begin{displaymath}
%%         \frac{
%%           \frac{3}{2} C \cdot {\color{MainA}s_i}
%%         }{
%%           \frac{1}{2}{\color{MainA}s_i} + 1
%%         }
%%         \leq
%%         \frac{
%%           \frac{3}{2} C \cdot {\color{MainA}s_i}
%%         }{
%%           \frac{1}{2}{\color{MainA}s_i}
%%         }
%%         = 3 C = \text{const.}
%%       \end{displaymath}
%%     \item
%%     Total costs:
%%     \begin{math}
%%       {\color{MainA}\mathrm{cost}(O_k)}+\dots+
%%       {\color{MainA}
%%         \mathrm{cost}(O_{k+\frac{1}{2}{\color{MainA}s_i} + 1})}
%%       \leq 3C \cdot n
%%     \end{math}
%%   \end{itemize}
%% \end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dynamic Arrays}{Amortized Analysis -
    Yet Another Proof of Corollary}
  \textbf{Bank account paradigm:}
  \begin{itemize}
    \item
      \textbf{Idea:}
       \enquote{Save first, spend later}
    \item
      For each operation we deposit some coins on an \enquote{bank account}\\
      $\Rightarrow$ We still have {\color{MainA}constant costs}
    \item
      When we have a {\color{MainA}linear operation} (reallocation)
      we pay with the coins from our \enquote{bank account}
    \item
      For the \enquote{double the size} strategy we have to pay
      two coins per operation
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dynamic Arrays}{Amortized Analysis -
    Yet Another Proof of Corollary}
  \begin{tabularx}{\linewidth}{@{}lclc}
    \textbf{Double the size:} &
    {\color{MainA}$\mathrm{cost}(O_i)$} &
    $\begin{array}{c}
      \text{deposit /}\\
      \text{withdraw}
    \end{array}$ &
     $\begin{array}{c}
       \text{account}\\
       \text{value}
     \end{array}$\\
    \def\FSAsize{1}\def\FSAelements{0}%
    \def\FSAcopy{0}\def\FSAdelete{0}\def\FSAinsert{1}%
    \input{Images/FixedSizeArray.tikz} &
    $O(1)$ & \hspace{1.5em}{\color{MainB}+2} & 2\\
    \def\FSAsize{2}\def\FSAelements{0}%
    \def\FSAcopy{1}\def\FSAdelete{0}\def\FSAinsert{1}%
    \input{Images/FixedSizeArray.tikz} &
    $O(1 + {\color{MainA}1})$ &
    \hspace{1.5em}{\color{MainB}+2} {\color{MainA}-1} &
    {\color{MainA}3}\\
    \def\FSAsize{4}\def\FSAelements{0}%
    \def\FSAcopy{2}\def\FSAdelete{0}\def\FSAinsert{1}%
    \input{Images/FixedSizeArray.tikz} &
    $O(1 + {\color{MainA}2})$ &
    \hspace{1.5em}{\color{MainB}+2} {\color{MainA}-2} &
    {\color{MainA}3}\\
    \def\FSAsize{4}\def\FSAelements{3}%
    \def\FSAcopy{0}\def\FSAdelete{0}\def\FSAinsert{1}%
    \input{Images/FixedSizeArray.tikz} &
    $O(1)$ & \hspace{1.5em}{\color{MainB}+2} & 5\\
    \def\FSAsize{8}\def\FSAelements{0}%
    \def\FSAcopy{4}\def\FSAdelete{0}\def\FSAinsert{1}%
    \input{Images/FixedSizeArray.tikz} &
    $O(1 + {\color{MainA}4})$ &
    \hspace{1.5em}{\color{MainB}+2} {\color{MainA}-4} &
    {\color{MainA}3}\\
    \def\FSAsize{8}\def\FSAelements{5}%
    \def\FSAcopy{0}\def\FSAdelete{0}\def\FSAinsert{1}%
    \input{Images/FixedSizeArray.tikz} &
    $O(1)$ & \hspace{1.5em}{\color{MainB}+2} & 5\\
    \def\FSAsize{8}\def\FSAelements{6}%
    \def\FSAcopy{0}\def\FSAdelete{0}\def\FSAinsert{1}%
    \input{Images/FixedSizeArray.tikz} &
    $O(1)$ & \hspace{1.5em}{\color{MainB}+2} & 7\\
    \def\FSAsize{8}\def\FSAelements{7}%
    \def\FSAcopy{0}\def\FSAdelete{0}\def\FSAinsert{1}%
    \input{Images/FixedSizeArray.tikz} &
    $O(1)$ & \hspace{1.5em}{\color{MainB}+2} & 9\\
    \def\FSAsize{10}\def\FSAelements{0}%
    \def\FSAcopy{8}\def\FSAdelete{0}\def\FSAinsert{1}%
    \input{Images/FixedSizeArray.tikz}%
    \hspace*{0.25em}$\cdots$ &
    $O(1 + {\color{MainA}8})$ &
    \hspace{1.5em}{\color{MainB}+2} {\color{MainA}-8} &
    {\color{MainA}3}\\
    \hspace*{1.5em}\dots & \dots & \hspace{1.5em}\dots & \dots
  \end{tabularx}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dynamic Arrays}{Amortized Analysis -
    Yet Another Proof of Corollary}
  \vspace{-1.5em}
  \begin{figure}[!h]%
    \def\FSAsize{14}\def\FSAelements{0}%
    \def\FSAcopy{7}\def\FSAdelete{0}\def\FSAinsert{1}%
    \def\FSAcopyarrow{1}%
    \def\FSAinsertarrow{1}%
    \def\FSAlabelsize{\raisebox{1.75em}{$\begin{array}{c}
        {\color{MainA}s_{i-1}}\\
        \text{old elements}
      \end{array}$}}%
    \def\FSAlabelinsertcapacity{new elements}%
    \def\FSAlabelcapacity{${\color{MainA}c_i}
      = 2 \cdot {\color{MainA}c_{i-1}}$}%
    \input{Images/FixedSizeArray_Copy.tikz}%
    \vspace{-0.5em}%
    \caption{Array after realloc. (insert) operation}
    \label{fig:dynamic_fields:amortized_analysis:yapoc_array}
  \end{figure}
  \textbf{Why do we need to deposit 2 coints per operation?}
  \begin{enumerate}
    \item
      Each newly inserted element has to be copied later\\
      (first coin)
    \item
      Due to the factor of two there is for each new element also an old
      one in the array that also has to be copied\\
      (second coin)
  \end{enumerate}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dynamic Arrays}{Amortized Analysis -
    Yet Another Proof of Corollary}
  \vspace{-2.0em}
  \begin{figure}[!h]%
    \def\FSAsize{14}\def\FSAelements{0}%
    \def\FSAcopy{7}\def\FSAdelete{1}\def\FSAinsert{0}%
    \def\FSAcopyarrow{1}%
    \def\FSAinsertarrow{1}%
    \def\FSAlabelsize{\raisebox{1.75em}{$\begin{array}{c}
        {\color{MainA}s_{i-1}-1}\\
        \text{old elements}
        \end{array}$}}%
    \def\FSAlabelinsertcapacity{removed elements}%
    \def\FSAlabelcapacity{${\color{MainA}c_i}
      = \frac{1}{2} \cdot {\color{MainA}c_{i-1}}$}%
    \input{Images/FixedSizeArray_Copy.tikz}%
    \vspace{-0.5em}%
    \caption{Array after realloc. (remove) operation}
    \label{fig:dynamic_fields:amortized_analysis:yapoc_array_shrink}
  \end{figure}
  \vspace*{-1.0em}
    \textbf{Shrinking strategy: If array 1/4 full shrink by half}
  \begin{itemize}
  \item
    How many coins do we need per \textit{remove} operation?
  \item
    \textbf{Worst case:}
    The previous remove operation triggered a \textit{reallocation}
  \item[$\Rightarrow$]
    Array is half full
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dynamic Arrays}{Amortized Analysis -
    Yet Another Proof of Corollary}
  \vspace{-2.0em}
  \begin{figure}[!h]%
    \def\FSAsize{14}\def\FSAelements{0}%
    \def\FSAcopy{7}\def\FSAdelete{1}\def\FSAinsert{0}%
    \def\FSAcopyarrow{1}%
    \def\FSAinsertarrow{1}%
    \def\FSAlabelsize{\raisebox{1.75em}{$\begin{array}{c}
        {\color{MainA}s_{i-1}-1}\\
        \text{old elements}
        \end{array}$}}%
    \def\FSAlabelinsertcapacity{removed elements}%
    \def\FSAlabelcapacity{${\color{MainA}c_i}
      = \frac{1}{2} \cdot {\color{MainA}c_{i-1}}$}%
    \input{Images/FixedSizeArray_Copy.tikz}%
    \vspace{-0.5em}%
    \caption{Array after realloc. (remove) operation}
    \label{fig:dynamic_fields:amortized_analysis:yapoc_array_shrink2}
  \end{figure}
  \vspace*{-1.0em}
  \textbf{Shrinking strategy: If array 1/4 full shrink by half}
  \begin{itemize}
    \item
      Array is half full
    \item
      The nearest \textit{reallocation} is after removing
      $\frac{1}{4} {\color{MainA}c_i}$ elements
    \item
      We have to copy $\frac{1}{4} {\color{MainA}c_i}$ elements
    \item[$\Rightarrow$]
      1 coin per operation is enough
  \end{itemize}
\end{frame}


\documentclass[a4paper]{article}
\usepackage{listings}
\usepackage{color}
\usepackage[margin=1in]{geometry}
\usepackage{graphicx}

\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}

\lstset{frame=tb,
	language=C++,
	aboveskip=3mm,
	belowskip=3mm,
	showstringspaces=false,
	columns=flexible,
	basicstyle={\small\ttfamily},
	numbers=none,
	numberstyle=\tiny\color{gray},
	keywordstyle=\color{blue},
	commentstyle=\color{dkgreen},
	stringstyle=\color{mauve},
	breaklines=true,
	breakatwhitespace=true,
	tabsize=4
}

\begin{document}
	\title{NOI 2016 Reference Sheet}
	\author{Compiled by Sun Yudong}
	\date{\today}
	\maketitle

	\begin{center}
		{\Huge \textbf{Don't Panic}}
	\end{center}

	\section{Sorts}
	\subsection{Merge Sort}
	\begin{lstlisting}
#include <iterator>
#include <algorithm> // for std::inplace_merge
#include <functional> // for std::less

template<typename RandomAccessIterator, typename Order>
void mergesort(RandomAccessIterator first, RandomAccessIterator last, Order order)
{
	if (last - first > 1)
	{
		RandomAccessIterator middle = first + (last - first) / 2;
		mergesort(first, middle, order);
		mergesort(middle, last, order);
		std::inplace_merge(first, middle, last, order);
	}
}

template<typename RandomAccessIterator>
void mergesort(RandomAccessIterator first, RandomAccessIterator last)
{
	mergesort(first, last, std::less<typename std::iterator_traits<RandomAccessIterator>::value_type>());
}
	\end{lstlisting}

	\subsection{Sort using Algorithm Package}
	Using arrays:
	\begin{lstlisting}
// sort() Example using arrays.
// By Zereo 04/22/13
#include <iostream>
#include <algorithm>

using namespace std;

const int SIZE = 7;

int main()
{
	int intArray[SIZE] = {5, 3, 32, -1, 1, 104, 53};

	//Now we call the sort function
	sort(intArray, intArray + SIZE);

	//Alternatively in C++11
	sort(begin(intArray), end(intArray));

	cout << "Sorted Array looks like this." << endl;
	for (size_t i = 0; i != SIZE; ++i)
	cout << intArray[i] << " ";

	return 0;
}
	\end{lstlisting}

	And using vectors:
	\begin{lstlisting}
// Vector Sorting Example.
// By Zereo 04/22/13
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>

using namespace std;

int main()
{
	// Warning this type of initialization requires a C++11 Compiler
	vector<int> intVec = {56, 32, -43, 23, 12, 93, 132, -154};
	vector<string> stringVec = {"John", "Bob", "Joe", "Zack", "Randy"};

	// Sorting the int vector
	sort(intVec.begin(), intVec.end());

	for (vector<int>::size_type i = 0; i != intVec.size(); ++i)
	cout << intVec[i] << " ";

	cout << endl;

	// Sorting the string vector
	sort(stringVec.begin(), stringVec.end());

	// Ranged Based loops. This requires a C++11 Compiler also
	// If you don't have a C++11 Compiler you can use a standard
	// for loop to print your vector.
	for (string &s : stringVec)
	cout << s << " ";

	return 0;
}
	\end{lstlisting}

	\section{Greatest Common Divisor}
	\begin{lstlisting}
unsigned int gcd(unsigned int u, unsigned int v)
{
	int shift;

	/* GCD(0,v) == v; GCD(u,0) == u, GCD(0,0) == 0 */
	if (u == 0) return v;
	if (v == 0) return u;

	/* Let shift := lg K, where K is the greatest power of 2 dividing both u and v. */
	for (shift = 0; ((u | v) & 1) == 0; ++shift) {
		u >>= 1;
		v >>= 1;
	}

	while ((u & 1) == 0) {
		u >>= 1;
	}

	/* From here on, u is always odd. */
	do {
		/* remove all factors of 2 in v -- they are not common */
		/*   note: v is not zero, so while will terminate */
		while ((v & 1) == 0)  /* Loop X */ {
			v >>= 1;
		}

		/* Now u and v are both odd. Swap if necessary so u <= v,
		then set v = v - u (which is even). For bignums, the
		swapping is just pointer movement, and the subtraction
		can be done in-place. */

		if (u > v) {
			unsigned int t = v; v = u; u = t;}	// Swap u and v.
			v = v - u;                       	// Here v >= u.
	} while (v != 0);

	/* restore common factors of 2 */
	return u << shift;
}
	\end{lstlisting}

	\section{deque}
	\begin{lstlisting}
deque();                   						//default constructor
deque<int> dq1;									//produces empty deque of ints

deque(const deque& d ); 						//copy constructor
deque<int> dq2(dq1);     						//creates a copy of dq1 in dq2
deque<int> dq2(dq1.begin(), dq1.end());
deque<int> dq2(dq1.rbegin(), dq1.rend());		//create a new deque, reversing dq1 by using reverse iterators

deque(size_type number, const T& value=T() );   //initialization construction
deque<double> dq3(10);     						//deque of doubles with 10 elements set to default (0.0)
deque<double> dq4(5, 8.1); 						//deque of doubles with 5 elements set to 8.1

deque(iterator start, iterator end );   		//interator constructor
double array1[]={0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0};
deque<double> dq5(array1,array1+4);      		//constructs deque with elements 0-3 from array1

/*----------*/

#include <stdexcept>	//required for defining and catching standard exceptions

deque<double> dq(5, 8.1);

for (int i=0; i<=5; ++i) {
	cout << "Element "<< i << " accessed with [] : "<< dq[i] << endl;
}

try {
	cout << "Element " << i << " accessed with at(i) :" << dq.at(i) << endl;
} catch (out_of_range&) {
	cout << "**out of range exception accessing element " << i << " with at() **" << endl;
}

/*---------*/

dq.at(2)=9.5				/* to change the element */
dq.size()					/* Size of deque */
(dq.empty())				/* Is deque empty? Return true if it is*/
dq.max_size()				/* Maximum size of deque */
dq.push_back(i)				/* Add elements to the back */
dq.push_front(i)			/* Add elements to the front */
dq.pop_front()				/* Remove first element */
dq.pop_back()				/* Remove last element */

/* display to screen */
for (int i=0; i<(signed)dq.size(); ++i) cout << dq.at(i);

/* insert array into deque */
int array[]= { 0, 1, 2, 3, 4 };
dq.insert(dq.begin()+2, array, array+5);

dq.insert(dq.begin()+4, 3, 0);		/* insert 3 copies of 0s */
dq.erase(dq.end()-2);				/* erase the next-to-last element */
	/* note: end() returns and iterator to the point just past the end of the queue. */
dq.erase(dq.begin(),dq.begin()+3);	/* erase the first three elements */

/* ----- */

//  declare two deques and intialize the second to contain 10 zeros
deque<int> dq1;
deque<int> dq2(10,0);

dq1.assign(10,5); 	/* assign 10 copies of the int 5 to deque 1 */
dq1.resize(15,10); 	/* resize deque 1, trailing elems truncated, new elems initialized to 10 */
dq1.swap(dq2);		/* swap contents of the deques */
dq1.clear();		/* clears and empties dq1, size will return 0 */

/* iterate through a deque */
for (deque<int>::iterator itr = dq.begin(); itr != dq.end(); ++itr)

	\end{lstlisting}

	\section{Vectors}
	\begin{lstlisting}
/* initializing a vector */
	vector<int> v { 34,23 };
	vector<int> v[1000005];

	vector<int> v(2);
	v = { 34,23 };

	vector<char> array;
	array.reserve(10) // If the vector already has room for the required number of elements, reserve() does nothing. In other words, reserve() will grow the allocated storage of the vector, if necessary, but will never shrink it.

/* using a vector */
v.push_back(1) 			//adds the element 1 to the end of the vector
v.capacity()			//vector capacity
v.size()				//actually how many elements there are
v.resize(6, 1);			//similar to deque

double a[] = {1, 2, 3, 4, 5};
mean(a, 5)
/* is equal to */
vector<double> a; //and you push the necessary elements
mean(&a[0], 5)

/* inserting array into vector */
double p[] = {1, 2, 3, 4, 5};
vector<double> a(p, p+5);

/* interating */
vector<double>::const_iterator i;
	for(i=a.begin(); i!=a.end(); ++i){
	std::cout<<(*i)<<std::endl;
}

	\end{lstlisting}

	\section{AARGHHHH}
	\begin{lstlisting}
/* always begin with the right bits */
#include <bits/stdc++.h>

/* reading/writing from/to standard input */
scanf("%d", &n);
printf("%d\n", n);

/* reading/writing to filestreams */
ifstream fin("file1.txt");
ofstream fout("output.txt");
!stream1.eof()					//check if end of file

/* initializing an array */
long long int x[1000] = {x1, x2, ....};

/* getting the whole line */
string name;
cin.getline (name, [chars], [delim]); /* or */
getline(cin, name);
	\end{lstlisting}

	\vspace{5mm}

	\begin{center}
		Here marks the end... Good luck! :D \\
		\vspace{5mm}
		{\Huge \textbf{Don't Panic}}
	\end{center}

	\begin{figure}[ht!]
		\includegraphics[width=\textwidth]{d5CDK.png}
	\end{figure}

\end{document}