\chapter{Arithmetic Expressions}

\label{chapt:arithmeticexpressions}

\section{Mindtuning for Arithmetic Expressions}

While \Kleene{} is designed primarily for creating and manipulating
finite-state networks, it does support arithmetic expressions, variables
that hold arithmetic values, functions that return arithmetic values,
etc.  Variables with an arithmetic value are marked with a \# sigil,
functions that return an arithmetic value with \verb!#^!, etc.  Wherever a
simple integer or float can appear in \Kleene{} syntax, including
numbered iterations like \verb!a{2,4}! and weights like \verb!<0.1>!, an
arbitrarily complex arithmetic expression can appear, e.g.\@
\verb!a{2, #maxlength - 1}! and \verb!<#defaultweight + .01>!.\footnote{Internally,
\Kleene{} stores arithmetic values as either a Long or a Double object.}

The arguments passed to a function could be any mix of regular
expressions and arithmetic expressions, and one of the biggest challenges
during \Kleene{} design and development was the distinguishing and proper
tokenization/parsing of the two separate expression types.  For example,
both use the plus sign as an operator, but in arithmetic expressions it
is a binary infix operator of fairly low precedence, e.g.\@  \verb!2+3!,
while in regular expressions it is a unary postfix operator of fairly
high precedence, e.g.\@ \verb!ab+c!.  Distinguishing the two
expression types by inventing new operators---either for regular
expressions or for arithmetic expressions---was judged to be
completely unacceptable; and forcing users to surround regular
expressions, or arithmetic expressions, with some kind of explicit
delimiters, such as the Perl slashes /\ldots/, was deemed inelegant
and undesirable.

The solution adopted was to define a systematic set of sigils starting
with \verb!$! for network-value variables and functions, and \verb!#! for
arithmetic-value variables and functions.  Parser lookahead distinguishes
\verb!#a+#b! as an arithmetic expression, involving addition, from
\verb!$a+$b!, which is a regular expression indicating the concatenation
of one or more iterations of network \verb!$a! with network \verb!$b!.
Once the sigil system is mastered, users can, in almost all cases, simply
type familiar regular and arithmetic expressions in appropriate places.

The remaining problematic cases are expressions like \verb!2! and
\verb!2+3!, which start with digits.  Are they arithmetic expressions,
having a integer or float value, or regular expressions, having a network
value?  The \Kleene{} solution is to treat bare digits by default as
arithmetic expressions.  To be interpreted as literal characters, and
therefore regular expressions, digits must be literalized in the usual
\Kleene{} ways:

\begin{itemize}
\item
Using the prefix backslash literalizer:  \verb!\2!
\item
Surrounding them with double quotes: \verb!"2"!, or
\item
Putting them in square-bracketed expressions: \verb![2]! \verb![^2]! \verb![0-9]! \verb![^0-9]!
\end{itemize}

\section{Primary Arithmetic Expressions}

The primary arithmetic expressions are the following:

\vspace{.5cm}

\renewcommand\tabcolsep{1.25mm}

\noindent
\begin{tabular}{|p{5.5cm}|l|}
\hline
\verb!0 1 42!  & decimal integers\\
\hline
\verb!0x1 0X1 0x1A 0X23ABC! & hex integers\\
\hline
\verb!2.5 .234 103.! & decimal floats\\
\hline
\verb!#n #count #foo! & names of variables denoting an arithmetic value\\
\hline
\verb!#^abs(#numexp)! \verb!#^arcCount($regexp)!
\verb!#^stateCount($regexp)! \verb!#^myfunction(!\emph{args}\verb!)! & names of functions returning an arithmetic value\\
%\hline
%\verb!#@arr[1]! & arithmetic array reference\\
\hline
\end{tabular}

\vspace{.5cm}

\noindent
Note that \Kleene{} does not support octal (base 8) representations of integers.

\section{Arithmetic Expression Operators}

The following regular expression operators are available,
listed from high to low precedence.

\vspace{0.5cm}

\noindent
\begin{tabular}{|l|l|l|l|}
\hline
( ) &  parenthetical grouping & circumfix &\\
\hline
\verb!+ -! & positive, negative & prefix &\\
\hline
\verb!* / %! & multiply, divide, mod & infix & left assoc.\\
\hline
\verb!+ -!  & add/subtract & infix & left assoc.\\
\hline
\verb/< <= >= == != >/ & Boolean comparisons & infix & left assoc.\\
\hline
\verb/!/ & Boolean NOT  & prefix & \\
\hline
\verb!&&! & Boolean AND & infix & left assoc.\\
\hline
\verb!||! & Boolean OR  & infix & left assoc.\\
\hline
\end{tabular}

\vspace{0.5cm}

\noindent
The result of boolean operations is always 1 (true) or 0 (false).  There
is no separate boolean type in Kleene.

\section{Arithmetic Functions}

The following pre-defined arithmetic-valued functions are provided.

\vspace{0.5cm}

\noindent
\begin{tabular}{|l|}
\hline
\verb!#^pathCount($fst)! \\
\verb!#^stateCount($fst)! \\
\verb!#^arcCount($fst)! \\
\verb!#^arity($fst)! \\
\hline
\verb!#^abs(#num)! \\
\verb!#^ceil(#num)! \\
\verb!#^floor(#num)! \\
\verb!#^round(#num)! \\
\verb!#^log(#num)! \\
\verb!#^prob2c(#prob)! \\
\verb!#^pct2c(#pct)! \\
\hline
\verb!#^long(#num)! or \verb!#^int(#num)! \\
\verb!#^double(#num)! or \verb!#^float(#num)! \\
\verb!#^rint(#num)! \\
\hline
\end{tabular}

\vspace{0.5cm}

In Kleene there is no difference between \verb!#^long(#num)! and
\verb!#^int(#num)!; both are stored as long integers.\footnote{Long
integers are stored internally as Java Long objects.}  Similarly, there
is no difference between \verb!#^double(#num)! and \verb!#^float(#num)!;
both are stored as double values.\footnote{Double values are stored
internally as Java Double objects.}

The \verb!#^log(#num)! function returns the natural logarithm of the
argument.
The \verb!#^prob2c(#prob)! takes an argument that represents a probability,
where probabilities range from 1.0 (certain) to 0.0 (impossible), and
returns a cost weight suitable for the Tropical Semiring.\footnote{In
	the Tropical Semiring, the weights are \emph{costs}.  For any
probability \emph{p}, the corresponding cost is $-$log(p).  A probability
of 1.0 corresponds to a cost of 0.0, and a probability of 0.0
corresponds to an infinite cost.}  The \verb!#^prob2c(#prob)! function might
be used in examples like the following,

\begin{Verbatim}[fontsize=\small]
$fsm = a b ( c <#^prob2c(0.1)> | d <#^prob2c(0.9)> ) ;
\end{Verbatim}

\noindent
where the path containing \emph{c} has 0.1 probability, and the path containing
\emph{d} has 0.9 probability.  The \verb!#^pct2c(#pct)! is similar but allows you
to express the weights as percentages, e.g.\ 10\% vs.\ 90\%.  The following example is equivalent to the
one just above.

\begin{Verbatim}[fontsize=\small]
$fsm = a b ( c <#^pct2c(10)> | d <#^pct2c(90)> ) ;
\end{Verbatim}

[KRB:  explain why the \verb!#^pathCount()! is not completely accurate.
Explain the other functions..]

\section{Boolean Functions}

\subsection{Special Case of Arithmetic Functions}

Boolean functions are special cases of arithmetic functions that return 1 for true and 0 for false.  They
are typically used in if-else expressions or in loops for testing qualities of finite-state machines.
The predefined boolean functions are

\vspace{0.5cm}

\noindent
\begin{tabular}{|l|}
\hline
\verb!#^isAcceptor($fst)! \\
\verb!#^isTransducer($fst)! \\
\verb!#^isRtn($fst)! \\
\hline
\verb!#^isWeighted($fst)! \\
\verb!#^isIDeterministic($fst)! or \verb!#^isUDeterministic($fst)!\\
\verb!#^isODeterministic($fst)! or \verb!#^isLDeterministic($fst)!\\
\verb!#^isCyclic($fst)!\\
\verb!#^isIBounded($fst)! or \verb!#^isUBounded($fst)!\\
\verb!#^isOBounded($fst)! or \verb!#^isLBounded($fst)!\\
\verb!#^containsOther($fst)! \\
\verb!#^hasClosedAlphabet($fst)! \\
\verb!#^isEpsilonFree($fst)! \\
\hline
\verb!#^isEmptyLanguage($fst)! \\
\verb!#^isEmptyStringLanguage($fst)! \\
\verb!#^isString($fst)! or \verb!#^isSingleStringLanguage($fst)!\\
\verb!#^containsEmptyString($fst)! \\
\verb!#^isUniversalLanguage($fst)! \\
\hline
\end{tabular}

\vspace{0.5cm}
\noindent
and others will no doubt be defined as they become needed.

The \verb!#^isAcceptor($fst)! function returns true if and only if the
argument network is semantically an
acceptor.  In OpenFst, where all arc labels \emph{i}:\emph{o} in a finite-state machine
are two-level, with an input label \emph{i} and an output label \emph{o}, an acceptor is a special case of a
machine wherein each arc-label mapping is an identity mapping.\footnote{If the finite-state machine contains an arc labeled
\acro{other\_nonid:other\_nonid}, then it will look like an acceptor to the OpenFst library, but in Kleene such
a network is semantically and in practice a transducer; if \verb!#^isAcceptor()! is called on such
a network the return value is 0 (false).  Conversely, if
\verb!#^isTransducer()! is called on any network that
contains an arc labeled \acro{other\_nonid:other\_nonid}, the return value is 1 (true).}

The \verb!#^isTransducer($fst)! function returns true if and only if the argument network is semantically a
non-identity transducer.  Thus if the machine contains any arc label like a:b, where the upper label is
different from the lower label, \verb!#^isTransducer! will return true.\footnote{If the argument contains an
arc labeled
\acro{other\_nonid:other\_nonid},
then it will look like an identity relation to OpenFst itself, but in
Kleene this is semantically a non-identity transducer and \verb!#^isTransducer! will return 0 (false).}

The \verb!#^isWeighted($fst)! function returns true if and only if the argument
finite-state machine contains any arc
with a non-neutral weight value.

The \verb!#^isIDeterministic($fst)! function returns true if and only if the finite-state machine is \emph{input}-side
deterministic, where ``input'' here is taken in the OpenFst sense of the upper side.
Similarly, the function \verb!#^isODeterministic($fst)! returns true if and only if the
network is \emph{output}-side deterministic, in the OpenFst sense.\footnote{The spellings IDeterministic and ODeterministic are
used inside the OpenFst library.}

The \verb!#^isCyclic($fst)! function returns true if and only if the finite-state machine has cycles, also
known as loops.

The \verb!#^isIBounded($fst)! function returns true if and only if the finite-state machine has no input side
epsilon loops.

The \verb!$^isOBounded($fst)! function returns true if and only if the finite-state machine has no output side
epsilon loops.

The \verb!#^containsOther($fst)! function returns true if and only if the network
contains at least one \acro{other\_id} or \acro{other\_nonid} label.
Conversely,
\verb!#^hasClosedAlphabet($fst)! returns true if and only if the
alphabet of the network is closed, i.e.\@ the network does not contain any
\acro{other\_id} or \acro{other\_nonid} labels.  Thus
\verb!#^hasClosedAlphabet($fst)! is equivalent to
\verb@!#^containsOther($fst)@.

The \verb!#^isEpsilonFree($fst)! function returns true if and only if the network contains no
\acro{epsilon:epsilon} arcs,
i.e.\@ no double-sided epsilon labels.  Because Kleene routinely runs epsilon-removal
on all networks, which removes such double-sided epsilon arcs, unless such removal is
explicitly turned off, there is seldom any practical need to call
\verb!#^isEpsilonFree($fst)!.

The \verb!#^isEmptyLanguage($fst)! function returns true if and only if the network encodes the empty
language, i.e.\@ the language that contains no strings at all, not even the empty string.

The \verb!#^isEmptyStringLanguage($fst)! function returns true if and only if the network encodes the
empty-string language, i.e.\@ the language that contains only the empty string.

The \verb!#^isString($fst)! function, aliased as \verb!#^isSingleStringLanguage($fst)!, returns true if
and only if the network encodes a language of exactly one string.

The \verb!#^containsEmptyString($fst)! function returns true if and only if the network encodes a
language that contains the empty string.

The \verb!#^isUniversalLanguage($fst)! function returns true if and only if the network encodes the
universal language, i.e.\@ the language that contains all possible strings.  [KRB, 2012-08-19:  This
function will not be fully reliable until a ``compact sigma'' function is implemented.]

\subsection{Assert and Require Statements}

The \verb!assert #numexp, $regexp ;! statement has a required arithmetic first
argument, which is analyzed as a boolean, and an optional second
regular-expression argument, which must denote a single-string language.  If
the first argument analyzes as true, Kleene takes no further action and control simply passes to the next
statement.  But if the first argument analyzes as false, a runtime exception is thrown and
the single-string second argument, if present, is used as the exception
message.  The \verb!assert #numexp, $regexp ;! statement is typically used
for testing.

\begin{Verbatim}[fontsize=\small]
$fst = a - a ;
assert #^isEmptyLanguage($fst),
        "Should denote the empty language." ;

$fst2 = a* ;
assert #^containsEmptyString($fst2),
        "Should contain the empty string." ;
\end{Verbatim}

In complete parallel, the \verb!require #numexp, $regexp ;! statement also has a required arithmetic first
argument, which is analyzed as a boolean, and an optional second
regular-expression argument, which must denote a single-string language.  If
the first argument analyzes as true, Kleene takes no further action and control simply passes to the next
statement.  But if the first argument analyzes as false, a runtime exception is thrown and
the single-string second argument, if present, is used as the exception
message.  The \verb!require #numexp, $regexp ;! statement is typically used
in user-defined functions to impose semantic restrictions on the
arguments.\footnote{The need for separate \verb!assert #numexp, $regexp;!
and \verb!require #numexp, $regexp ;! arguments could be debated.  One
possibility would be to implement a global switch that would cause all
\verb!assert #numexp, $regexp ;! statements, but not
\verb!require #numexp, $regexp ;! statements, to be ignored at runtime.}

\begin{Verbatim}[fontsize=\small]
#^myIntersect($a, $b) {
	require #^isAcceptor($a) && #^isAcceptor($b),
    "The arguments to #^myIntersect must denote an acceptor." ;

	return $a & $b ;
}
\end{Verbatim}


\documentclass[12pt,twoside]{article}
\usepackage{mathtools} % for DeclareParedDelimiter
\usepackage{listings}
\usepackage{graphicx} % Required for including images
\newcommand{\reporttitle}{Foundations of Machine Learning}
\newcommand{\reportauthor}{Lionel Ngoupeyou Tondji}
\newcommand{\reporttype}{Assignment: Foundations of Machine Learning (AMMI)}
\newcommand\myeq{\mathrel{\overset{\makebox[0pt]{\mbox{\normalfont\tiny\sffamily iid}}}{=}}}

\begin{document}
% front page

% Last modification: 2016-09-29 (Marc Deisenroth)
\begin{titlepage}
\newcommand{\HRule}{\rule{\linewidth}{0.5mm}} % Defines a new command for the horizontal lines, change thickness here

%----------------------------------------------------------------------------------------
%	LOGO SECTION
%----------------------------------------------------------------------------------------

\begin{center}
\includegraphics[height = 2cm]{qla}\hspace{4mm}
\includegraphics[height=2cm]{aims-rwanda}
\end{center}

\begin{center} % Center remainder of the page

%----------------------------------------------------------------------------------------
%	HEADING SECTIONS
%----------------------------------------------------------------------------------------
\textsc{\LARGE \reporttype}\\[1.5cm]
\textsc{\Large African Institute for Mathematical Sciences}\\[0.5cm]
\textsc{\large Quantum Leap Africa}\\[0.5cm]
%----------------------------------------------------------------------------------------
%	TITLE SECTION
%----------------------------------------------------------------------------------------

\HRule \\[0.4cm]
{ \huge \bfseries \reporttitle}\\ % Title of your document
\HRule \\[1.5cm]
\end{center}
%----------------------------------------------------------------------------------------
%	AUTHOR SECTION
%----------------------------------------------------------------------------------------

\begin{minipage}{0.4\hsize}
 \begin{flushleft} \large
 \textit{Author: Lionel Ngoupeyou Tondji}\\
 \end{flushleft}
  \vspace{2cm}
 \makeatletter
 Date: \@date
\end{minipage}
\vfill % Fill the rest of the page with whitespace

\makeatother

\end{titlepage}

\section*{Density Estimation with Gaussian Mixture Models}

\subsection*{Task 1}

See the python file.

\subsection*{Task 2}

You are given a two-dimensional dataset of about 30 geolocations (latitude/longitude) of birthplaces. Model the data as well as possible. Describe your approach, justify your choices and interpret your results.

First of all we are trying to plot the two-dimensional dataset, to see how they look like.
\begin{center}
	\begin{figure}
		\includegraphics[scale=0.7]{1.png}
		\caption{visualizing our data set}
	\end{figure}
\end{center}

The next task that we perform is to normalize our data set, So we will reduce by the mean and subtract by the standard deviation in order to have numerical stability.
We obtain the following graph :

\begin{center}
	\begin{figure}
		\includegraphics[scale=0.7]{2.png}
		\caption{normalized Data set}
	\end{figure}
\end{center}

We create a function that help us visualize the locations and shapes of the Gaussian Mixture Model clusters by drawing ellipses based on the Gaussian Mixture Model output:

The first one we consider 2 clusters :
\begin{center}
	\begin{figure}
		\includegraphics[scale=0.7]{4.png}
		\caption{Ellipses based on the 2 Gaussian Mixture Model output}
	\end{figure}
\end{center}

The second one we consider 3 clusters :
\begin{center}
	\begin{figure}
		\includegraphics[scale=0.7]{3.png}
		\caption{Ellipses based on the 3 Gaussian Mixture Model output}
	\end{figure}
\end{center}

The third one we consider 4 clusters :
\begin{center}
	\begin{figure}
		\includegraphics[scale=0.7]{5.png}
		\caption{Ellipses based on the 4 Gaussian Mixture Model output}
	\end{figure}
\end{center}

We take the same data and transform it, we recognize that these transformed clusters are non-circular, and thus circular clusters would be a poor fit.

we can use the Gaussian Mixture Model approach to fit our stretched dataset; allowing for a full covariance the model will fit even very oblong, stretched-out clusters:

The first one we consider 3 clusters :
\begin{center}
	\begin{figure}
		\includegraphics[scale=0.7]{6.png}
		\caption{Ellipses based on the 3 Gaussian Mixture Model output where we apply the transformation}
	\end{figure}
\end{center}

The second one we consider 4 clusters :
\begin{center}
	\begin{figure}
		\includegraphics[scale=0.7]{7.png}
		\caption{Ellipses based on the 4 Gaussian Mixture Model output where we apply the transformation}
	\end{figure}
\end{center}

We can see  in the graph that if we consider just 2 mixture model , we have collision with the two ellipses but a good value for $k = 3$ from this value we can see that the ellipses are well drawn. Also for $k = 4$ it's perform very well and this can be view as over-fitting.
\end{document}


\newpage
\section{Classification Models} \label{sec:impl/clf_models}

All classification models detailed below were implemented in Python using toolsets provided by the Pytorch Deep Learning Library \cite{paszke2019}. This allows for higher abstraction when designing model architectures without having to manually implement backpropagation, optimizers, activation functions, and other standard machine learning concepts. Leaving these implementations to the experts allows for GPU accelerated training optimized for computing efficiency.

While some models could be trained on a mere laptop CPU, all the deeper architectures were trained using a cloud computing instance provided by Amazon Web Services. The instance used had four Intel Xeon Cascade Lake P-8259L processors, 16GiB memory, and an Nvidia T4 Tensor Core GPU with 16GiB graphics memory.

\subsection{Architecture}

The architecture of a neural network defines the hypothesis space in which predictions may be made from a given input. Deeper networks can encapsulate more complex dataset features at the increased risk of overfitting features that do not generalize well outside the training data. For this thesis, the selection of model architectures is inspired by a review of deep learning of \acrshort{tsc} algorithms presented by \textcite{fawaz2018}. Although an overview is provided in the following subsections, a comprehensive list of all model architectures can be studied in appendix \ref{app:arch_details}.

All models aim to be fully end-to-end and able to classify multivariate time series of undefined dimensions. Input time series are multivariate in their data channels (e.g., pupillary, coordinate, and \acrshort{ebr}) and temporal.
The dimensions of the input data are therefore defined by segment length and the number of data channels.
% Their dimension is therefore defined by its length in time, sampling frequency, and number of data channels.

\newpage
\subsubsection{\acrlong{mlp}}

\begin{figure}[h]
    \centering
    \includegraphics[width=\textwidth]{figures/impl_MLP.png}
    \caption{\acrlong{mlp} architecture.}
    \source{\textcite{wang2016}}
    \label{fig:impl_MLP}
\end{figure}

First up is the traditional and well-known \acrfull{mlp}. Being an integral part of deep learning, it is a natural inclusion as a baseline by which other architectures may be compared. This particular implementation, first proposed for \acrshort{tsc} by \textcite{wang2016}, is presented in figure \ref{fig:impl_MLP}. It is composed of four layers where all neurons are connected to activations from neurons of the previous layer. The three hidden layers between input and output are each composed of 500 neurons with \acrshort{relu} as the activation function. Between all layers is a dropout operation with dropout rates ranging from 10\% to 30\%. The input layer takes all samples in one time segment from all data channels. With input segments of length 256, the layers of this model contain $1,015,504$ parameters. Outputs from the last hidden layer are passed through a softmax function, such that the model output is a probability distribution over all classes that sum to one.

Since all layers in an \acrshort{mlp} are fully connected, any temporal invariance between time-consecutive samples in the data is lost. This is because all input neurons to every layer are weighted independently from one another, such that the time dimension from input samples is essentially ignored. Since temporal invariance is an important factor for \acrshort{tsc}, the \acrshort{mlp} is likely a sub-optimal alternative to other deep learning architectures.

\newpage
\subsubsection{\acrlong{fcn}}

\begin{figure}[h]
    \centering
    \includegraphics[width=\textwidth]{figures/impl_FCN.png}
    \caption{\acrlong{fcn} architecture.}
    \source{\textcite{wang2016}}
    \label{fig:impl_FCN}
\end{figure}

The second architecture presented is a form for \acrshort{cnn}, as described in section \ref{sec:bt/DNN/CNN}. Like the \acrshort{mlp}, the \acrfull{fcn} implementation was proposed by \textcite{wang2016}, and is presented in figure \ref{fig:impl_FCN}. It is composed of three convolutional blocks, where each block is appended with a batch normalization layer which is passed through a \acrshort{relu} activation function. Layers have 128, 256, and 128 convolutional filters, with filter lengths of eight, five, and three, respectively. Even though this architecture has the same layer depth as the \acrshort{mlp}, the shared parameter- and sparse connection features of \acrshort{cnn}s allows for fewer total parameters, at $268,292$. Output from the last layer is fed into a global average pooling layer that collapses the entire time dimension to a single value for every filter. These values are subsequently passed onto a fully connected classifier activated by a softmax function. Similar to the \acrshort{mlp}, the output is, therefore, a probability distribution that sums to one.

% Since the learned parameters of a \acrshort{cnn} is merely the internal weights of all convolutional filters, temporal invariance is achieved because

Contrary to a \acrshort{mlp} architecture, the convolutional nature of the \acrshort{fcn} allow for temporal invariance. With this, the resulting model can recognize distinctive features in time. Another advantage of the \acrshort{fcn} is invariance in the total number of parameters. Since the global average pooling layer collapses the time dimension, parameters are only dependent on the number of layers and filters, which is constant. This invariance is a consequence of the shared parameters property of \acrshort{cnn}s. It allows for a transfer learning approach since the convolutional filter weights will learn features independent of segment length and temporal position.

\newpage
\subsubsection{\acrlong{mcdcnn}}

\begin{figure}[h]
    \centering
    \includegraphics[width=\textwidth]{figures/impl_MCDCNN.png}
    \caption{\acrlong{mcdcnn} architecture.}
    \label{fig:impl_MCDCNN}
\end{figure}

The \acrfull{mcdcnn} is in some ways a blend of a \acrshort{mlp} and a \acrshort{fcn}. It was originally proposed and validated by \textcite{zheng2014}, and was expected to perform especially well on multivariate time series datasets. As is illustrated in figure \ref{fig:impl_MCDCNN}, it consists of two convolutional stages with eight filters of length five. Following the convolutions is a \acrshort{relu} activation function and a max-pooling operation. Uniquely, these convolutional stages are all applied in parallel before their outputs are flattened and passed to a fully connected layer with 732 neurons, again with \acrshort{relu} as its activation function. This model contains slightly more parameters than the \acrshort{fcn}, at $378,632$. Output from the fully connected layer is equal to the number of classes to be classified and is activated by a softmax function such that these class probabilities sum to one.

Separating input channels in individual parallel convolutions may have both advantages and disadvantages. On the one hand, the \acrshort{mcdcnn} may be able to generate features that might otherwise have been lost from the cross-channel convolutions of the \acrshort{fcn}. However, on the other hand, there might be interesting relationships across channels that may not be captured with this approach.

\newpage
\subsubsection{Residual Network}

\begin{figure}[h]
    \begin{subfigure}[b]{\textwidth}
        \includegraphics[width=\textwidth]{figures/impl_ResNet11.png}
        \caption{\acrlong{resnet11} architecture.}
        \source{\textcite{wang2016}}
        \label{fig:impl_ResNet11}
    \end{subfigure}
    \begin{subfigure}[b]{\textwidth}
        \includegraphics[width=\textwidth]{figures/impl_ResNet18.png}
        \caption{\acrlong{resnet18} architecture.}
        \label{fig:impl_ResNet18}
    \end{subfigure}
    \caption{Model architectures of \acrshort{resnet11} and \acrshort{resnet18}. Three dimensional layers represent convolutions, numbered by filter count. Characteristic to ResNet architectures is the "skip-connections" represented by arrows skipping every two- or three convolutional blocks.}
\end{figure}

The two final models presented in this thesis are based on an architecture made famous for computer vision by \textcite{he2015}, known as Residual Networks. At the time, a complication that was known as "the vanishing gradient problem"  plagued most architectures of any considerable depth. It was caused due to the iterative training approach, where weights of the network are updated according to the gradient of the error function with respect to each weight. For every added layer, these values are multiplied, eventually approaching zero. In the worst case, this may slow and even halt the training process entirely \cite{basodi2020}. Residual Networks solve this problem by introducing \textit{skip connections} that allow for gradients to flow past convolutions from start to end. This architecture won several image classification competitions after its inception in 2015, and skip connections have been employed in many other state-of-the-art model architectures since.

In the original paper, \textcite{he2015} introduced five distinct ResNet architectures, ranging from 18 to 152 layers in depth. However, to avoid comparing models with over an order of magnitude of difference in depth, the architecture used here will be limited to an \acrfull{resnet18}. This architecture is illustrated in figure \ref{fig:impl_ResNet18} and employs eight \textit{ResNet blocks}, each including two convolutional layers followed by a batch normalization layer activated by \acrshort{relu}. The input to every ResNet block is saved and added to its output, which constitutes a skip connection. Internal convolutional layers use an increasing amount of filters over the depth of the network, from 64 to 512 filters. Just like the \acrshort{fcn}, the outputs from the convolutional filters are sent through a global average pooling which collapses their time dimension. Finally, a fully-connected layer activated by softmax ensures a probability distribution over classes that sums to one.

A modification to the original ResNet architectures was proposed by \textcite{wang2016}. This version, an \acrfull{resnet11}, has proven to be accurate on \acrshort{tsc}, and is presented in figure \ref{fig:impl_ResNet11}. Contrary to \acrshort{resnet18}, this version consists of three larger ResNet blocks, each with three convolutional layers employing 64, 128, and 128 filters. Otherwise, the input, output, and skip connections of this architecture are similar to that of \acrshort{resnet18}.

Both ResNet architectures sport the greatest of parameters of all convolutional models detailed and developed in this thesis. Their complexity is expected to influence the training process significantly. \acrshort{resnet11} has $522,756$ parameters, while the deeper and more complex \acrshort{resnet18} sports as much as $4,190,084$.

% Both ResNet versions possess similar advantages as the \acrshort{fcn}, having a number of parameters which is invariant of the size of its inputs.

% \newpage
% \subsection{Hyperparameters and Training}

% In addition to comparing the intrinsically different architectures detailed above, the classifications each model produces are highly influenced by the parameters with which they are trained. Some mostly affected training efficiency, while others had a clear effect on either classification accuracy or tendency to overfit the training data.

% \begin{itemize}
%     \item Segment length
%     \item Time series augmentation
%     \item Blink rate attention window
%     \item Time series smoothing window
%     \item ? Batch size
%     \item ? Learning rate
%     \item ? Dropout
%     \item Batch Normalization
% \end{itemize}


\documentclass {article}

\usepackage[letterpaper]{geometry}
\usepackage{amsthm, amsmath, amssymb, stmaryrd}
\usepackage{mathpartir}

\newtheorem{theorem}{Theorem}[section]
\newtheorem{lemma}[theorem]{Lemma}

\title {Verifier WLP Definitions}
\author {Jenna Wise, Johannes Bader, Jonathan Aldrich, \'{E}ric Tanter}
\date {\today}

%% Commands
\newcommand{\lcar}{\left<}
\newcommand{\rcar}{\right>}
\newcommand{\true}{\text{true}}
\newcommand{\eif}[3]{if \ ( #1 ) \ \{ #2 \} \ else \ \{#3\}}
\newcommand{\fphi}{\widehat{\phi}}
\newcommand{\tphi}{\widetilde{\phi}}
\newcommand{\acc}[1]{\text{acc}(#1)}
\newcommand{\imp}{\Rightarrow}
\newcommand{\timp}{\ \widetilde{\Rightarrow}\ }
\newcommand{\maximp}[2]{\underset{\Rightarrow}{\text{max}}\left\{#1 \mid #2\right\}}
\newcommand{\pre}{\text{pre}(C,m)}
\newcommand{\presub}{\text{pre}(C,m)\left[z/this, \overline{x_i/\text{params}(C,m)_i}\right]}
\newcommand{\post}{\text{post}(C,m)}
\newcommand{\postsub}{\text{post}(C,m)\left[z/this, \overline{x_i/\text{old}(\text{params}(C,m)_i)},y/result\right]}

\newcommand{\wlp}[2]{\text{WLP}(#1,#2)}
\newcommand{\twlp}[2]{\widetilde{\text{WLP}}(#1,#2)}
\newcommand{\swlp}[2]{\text{sWLP}(#1,#2)}
\newcommand{\swlpi}[2]{\text{sWLP}_i(#1,#2)}

% uppercase word defs
\newcommand{\satdef}{\textsc{SatFormula}}
\newcommand{\implsdef}{\textsc{ImplStatic}}
\newcommand{\implgdef}{\textsc{ImplGrad}}
\newcommand{\tformuladef}{\widetilde{\textsc{F}}\textsc{ormula}}
\newcommand{\formuladef}{\textsc{Formula}}

\begin{document}

\maketitle

%\begin{figure*}[ht!]
%\begin{flalign*}
%%\spc{skip}{\phi} = \phi
%%\and
%%\spc{s_1;s_2}{\phi} = \spc{s_2}{\spc{s_1}{\phi}}
%%\and
%%\spc{T\ x}{\exists \overline{x}.\theta \wedge \psi} = \exists \overline{x}.(\theta \wedge x = \text{defaultValue}(T)) \wedge \psi
%%\and
%%\spc{x := e}{\exists \overline{x}.\theta \wedge \psi} = \exists \overline{x},x' . ([x'/x]\theta \wedge x = [x'/x]e) \wedge [x'/x]\psi
%%\and
%%\spc{x := y.f}{\exists \overline{x}.\theta \wedge (\psi \ast y.f \mapsto e)} =  \exists \overline{x},x' . ([x'/x]\theta \wedge x = [x'/x]e) \wedge [x'/x](\psi \ast y.f \mapsto e)
%%\and
%%\spc{x.f := y}{\exists \overline{x}.\theta \wedge (\psi \ast x.f \mapsto e)} = \exists \overline{x}.\theta \wedge (\psi \ast x.f \mapsto y)\\
%%\and
%%\spc{x := new\ C}{\exists \overline{x}.\theta \wedge \psi} = \exists \overline{x},x' . [x'/x]\theta \wedge ([x'/x]\psi\ \overline{\ast\ x.f_i \mapsto \text{defaultValue}(T_i)})\ \text{where fields}(C)=\overline{T_i\ f_i} \\
%%\and
%%\spc{y := z.m(\overline{x})}{\exists \overline{y}.\theta \wedge \psi} = \begin{cases} \exists \overline{y}, y' . ([y'/y]\theta_{frame} \wedge \theta_Q) \wedge ([y'/y]\psi_{frame} \ast \psi_Q) & \text{if}\ \exists \overline{y}.(\theta \wedge \psi) \imp \exists \overline{y}.(\theta_{frame} \wedge \theta_P) \wedge (\psi_{frame} \ast \psi_P) \\ undefined & \text{otherwise} \end{cases}
%%\\ \text{where}\ \left[z/this,\overline{x_i/\text{params}(m)_i}\right]\text{pre(m)}=\theta_P \wedge \psi_P\ \text{and}\ \left[z/this, y/result, \overline{x_i/\text{old}(\text{params}(m)_i)}\right]\text{post(m)}=\theta_Q \wedge \psi_Q
%%\and
%%\spc{assert\ \phi_a}{\phi} = \begin{cases} \phi & \text{if}\ \phi \imp \phi_a \\ undefined & \text{otherwise} \end{cases}
%%% WLP
%\wlp{skip}{\phi} &= \phi & \\
%\wlp{s_1;s_2}{\phi} &= \wlp{s_1}{\wlp{s_2}{\phi}}& \\
%\wlp{T\ x}{\phi} &= \phi &\\
%\wlp{x := e}{\phi} &= [e/x]\phi &\\
%\wlp{x := y.f}{\theta \wedge \psi} &= [e/x]\theta \wedge ([e/x]\psi \ast y.f \rightarrow e) &\\
%\wlp{x.f := y}{\theta \wedge \psi} &= \maximp{\theta' \wedge \psi'}{\theta' \wedge (\psi' \ast x.f \rightarrow y) \imp \theta \wedge \psi} &\\
%\wlp{x := new\ C}{\phi} &= &\\
%\wlp{y := z.m(\overline{x})}{\phi} &= &\\
%\wlp{assert\ \phi}{\phi} &=  &\\
%\end{flalign*}
%%\caption{Weakest Liberal Precondition Calculus}
%%\label{wp-rules}
%\end{figure*}

\section{Weakest liberal precondition calculus definitions over self-framed non-gradual formulas}
\hspace{0.5cm}

$\wlp{skip}{\fphi} = \fphi  $

\vspace{0.5cm}

$\wlp{s_1;s_2}{\fphi} = \wlp{s_1}{\wlp{s_2}{\fphi}} $

\vspace{0.5cm}

%$\wlp{T\ x}{\fphi} = \fphi\left[\text{defaultValue}(T)/x\right] $ -- NEEDS TO CHANGE

%\vspace{0.5cm}

$\wlp{T\ x}{\fphi} = \fphi$

$\wlp{x := e}{\fphi} =\maximp{\fphi'}{\fphi' \imp \fphi[e/x] \quad \wedge \quad \fphi' \imp \acc{e}} $

\vspace{0.5cm}

$\wlp{\eif{x \odot y}{s_1}{s_2}}{\fphi} = $
%\underset{\Rightarrow}{\text{max}} \Big{\{} \fphi' \mid \fphi' \imp \text{acc}(x) \quad \wedge \quad \fphi' \imp \text{acc}(y) \quad \wedge \quad$

%\indent  \hspace{4cm} $\fphi' \imp \left(\left(x \odot y \imp \wlp{s_1}{\fphi}\right) \quad \wedge \quad \left(\neg(x \odot y) \imp \wlp{s_2}{\fphi}\right)\right) \Big{\}}$

\vspace{0.5cm}

$\wlp{x.f := y}{\fphi} = \acc{x.f} \ \ast \ \maximp{\fphi'}{\fphi' \ast \acc{x.f} \ast (x.f = y) \imp \fphi \ \wedge \ \fphi' * \acc{x.f} \in \satdef} $

\vspace{0.5cm}

$\wlp{x := new\ C}{\fphi} = \maximp{\fphi'}{ \fphi' \ast \overline{\acc{x.f_i}} \imp \fphi}$

%\overline{\acc{x.f_i} \ast (x.f_i = \text{defaultValue}(T_i))}

\indent  \hspace{4cm} where fields$(C) = \overline{T_i \ f_i}$

\vspace{0.5cm}

$\wlp{y := z.m(\overline{x})}{\fphi} = undefined$

\vspace{0.5cm}

$\wlp{y := z.m_C(\overline{x})}{\fphi} = \underset{\Rightarrow}{\text{max}} \Big{\{} \fphi' \mid y \not \in \text{FV}(\fphi') \quad \wedge \quad \fphi' \imp (z \neq null) \ \ast \ \text{pre}(C,m)\left[z/this, \overline{x_i/\text{params}(C,m)_i}\right] $
\indent \hspace{4cm} $ \wedge \quad \fphi' \ast \text{post}(C,m)\left[z/this, \overline{x_i/\text{old}(\text{params}(C,m)_i)}, y/result \right] \imp \fphi \Big{\}}$

\vspace{0.5cm}

$\wlp{assert\ \phi_a}{\fphi} = \maximp{\fphi'}{\fphi' \imp \fphi \quad \wedge \quad \fphi' \imp \phi_a} $

\vspace{0.5cm}

$\wlp{release \ \phi_a}{\fphi} = $

\vspace{0.5cm}

$\wlp{hold \ \phi_a \ \{s\}}{\fphi} = $

\vspace{0.5cm}

\noindent \textbf{Note:}

\textbf{Dynamic method calls.} Dynamic method calls are left undefined, because we are not verifying programs with dynamic dispatch at this time (all method calls should be static method calls). They are included in the grammar for future implementation.

\textbf{If \& Release \& hold.} Definitions coming soon.

\textbf{Predicates in the logic.} Although the grammar allows for abstract predicate families, we do not support them yet. Therefore, we assume formulas look like:

$$ \phi ::= \true \mid e \odot e \mid acc(e.f) \mid \phi \ast \phi $$

\section{Algorithmic WLP calculus definitions over self-framed non-gradual formulas}
\hspace{0.5cm}

\textbf{Note:}

It may be helpful to check that the $\wlp{s}{\fphi}$ is well-formed and/or self-framed for some of the more complicated rules, which may be buggy in implementation.

\vspace{0.5cm}

\textbf{Note:}

We do not always calculate the correct WLP due to a limitation in the expressiveness of our specification language. Concrete formulas which do not contain enough information to determine whether two or more objects alias, wrongly assume that those two or more objects do not alias. This is because our current specification language is missing a logical OR or a conditional construct, which would allow us to write self-framed concrete formulas that capture the unknown aliasing between two or more objects. A conditional construct will be added and the definitions below adjusted in the near future.
% the weakest liberal precondition for assert, because concrete formulas cannot support the true weakest liberal precondition. Concrete formulas need to support logical OR. This issue occurs when aliasing constructs are missing from certain concrete formulas; in these cases, there is no way to tell whether accessibility predicates should be conjoined or one of them removed due to aliasing.

\vspace{0.5cm}

$\wlp{skip}{\fphi} = \fphi  $

\vspace{0.5cm}

$\wlp{s_1;s_2}{\fphi} = \wlp{s_1}{\wlp{s_2}{\fphi}} $

\vspace{0.5cm}

$\wlp{T\ x}{\fphi} = \begin{cases}
  \fphi & if\ x \not\in \text{FV}(\fphi) \\
  undefined & otherwise
\end{cases}$

$\wlp{x := e}{\fphi} =
	\begin{cases}
	 \fphi[e/x] & if \ \fphi[e/x] \imp \fphi_{acc(e)} \\
	 \fphi_{acc} \ast \vert \fphi[e/x] \vert & otherwise
	\end{cases} $

where $e_i$ is an object dereferenced in $e$ (ex. $y \neq null$ and $y.f \neq null$ if $e = y.f.f$), $\mid \phi \mid$ means the formula $\phi$ without accessibility predicates, $\fphi_{acc(e)}$ is the formula of conjoined accessibility predicates necessary to frame $e = e$, and $\fphi_{acc}$ is the formula of conjoined accessibility predicates necessary to frame $\vert \fphi[e/x] \vert$ and $e = e$ ($\fphi_{acc}$ can be computed in a similar fashion as in the assert rule just with $\phi_a$ replaced by $e = e$ and $\fphi$ replaced by $\fphi[e/x]$).

Check that $\wlp{x := e}{\fphi} \ast x = e \imp \fphi$ and that $\wlp{x := e}{\fphi}$ is satisfiable.

\textbf{Important cases to consider:}

$\fphi = acc(y.f) \ast acc(y.f.f) \ast x = y.f.f$, $e=y.f.f$

$\fphi = acc(y.f) \ast y.f = z \ast x > 0$, $e=y.f.f$

\vspace{0.5cm}

$\wlp{\eif{x \odot y}{s_1}{s_2}}{\fphi} = $

\vspace{0.5cm}

$\wlp{x.f := y}{\fphi} =
	\begin{cases}
	 \fphi[y/x.f] \ast x & if \ \fphi[y/x.f] \imp acc(x.f) \\
	 acc(x.f) \ast \fphi[y/x.f] & otherwise
	\end{cases}$

Check that $\wlp{x.f := y}{\fphi} \ast x.f = y \imp \fphi$ and that $\wlp{x.f := y}{\fphi}$ is satisfiable.

\textbf{Important cases to consider:}

$\fphi = acc(x.f) \ast x.f = p \ast x.f = q \ast a = b $

$\fphi = acc(x.f) \ast acc(x.f.f) \ast x = y $

\vspace{0.5cm}

$\wlp{x := new\ C}{\fphi} =
	\begin{cases}
	 \fphi \div x & if \ (\fphi \div x) \ast \overline{x \neq e_i} \ast \overline{acc(x.f_i)} \imp \fphi \\
	 undefined & otherwise
	\end{cases}$

where fields$(C) = \overline{T_i \ f_i}$, $\fphi \div x$ means to transitively expand (in-)equalities ($\odot$) and then remove conjunctive terms containing $x$, and $\overline{x \neq e_i}$ are conjunctive terms in $\fphi$.

Check $\wlp{x := new\ C}{\fphi}$ is satisfiable.

\textbf{Important cases to consider:}

$\fphi = x \neq null \ast acc(x.f)$

$\fphi = x \neq null \ast acc(x.f) \ast x.f = 1 \ast x.f = y$ --- should fail, bad postcondition

$\fphi = x \neq null \ast acc(x.f) \ast x = y \ast x = z$ --- should fail, bad postcondition

$\fphi = x \neq null \ast acc(x.f) \ast x = y \ast y = z$ --- should fail, bad postcondition

$\fphi = x \neq null \ast acc(x.f) \ast x \neq y \ast y = z$

$\fphi = x \neq null \ast acc(x.f) \ast acc(x.f.f) \ast x.f.f \neq y$ --- should fail, bad postcondition

$\fphi = x \neq null \ast acc(y.f) \ast x = y$ --- should fail, bad postcondition

$\fphi = x \neq null \ast acc(x.f) \ast y > x.f \ast x.f > z \ast r \geq x.f \ast x.f \geq s$ --- should fail, bad postcondition

\textbf{Note:}

$x := new \ C$ creates a fresh object and assigns it to $x$ without setting default values to the object's fields; therefore, postconditions cannot say anything about the value of $x$ other than it does not equal other values (no aliasing with $x$) and they cannot say anything about the values of the fields of $x$.

\vspace{0.5cm}

$\wlp{y := z.m(\overline{x})}{\fphi} = undefined$

\vspace{0.5cm}

$\wlp{y := z.m_C(\overline{x})}{\fphi} = \fphi \ \div \ y \overline{\ \div \ (e.f)_i} \ast z \neq null \ast \presub$

%\fphi_{acc} \ \ast \mid \fphi \ \div \ y \overline{\ \div \ (x.f)_i} \mid \ \ast \ z \neq null \ \ast \ \left\vert \text{pre}(C,m)\left[z/this, \overline{x_i/\text{params}(C,m)_i}\right] \right\vert$

where $\phi \div x$ is defined differently than for the allocation rule above. $\phi \div x$ means to transitively expand (in-)equalities and comparisons and then remove conjunctive terms containing $x$, but removal of conjuctive terms containing $x$ as an object dereference should replace $x$ with an alias, if an alias exists. If an alias does not exist, then the term is removed as usual.

$(e.f)_i$ and $\phi \overline{\ \div \ (e.f)_i}$ are defined as follows:

$(e.f)_i$ is a field access used in the substituted postcondition of $m$. It comes from a list of field accesses used in the substituted postcondition of $m$ that is determined in the following way:
\begin{enumerate}
%\item Remove conjunctive terms containing field accesses in $\vert \fphi \div y \vert$, call this resulting formula $\vert \fphi \div y \vert'$
\item Determine the list of field accesses occurring in $\postsub$
	\begin{itemize}
	\item This list should be determined by starting with the ones in the accessibility predicates and then adding their duplicates due to aliasing to the list. If the static part of $\postsub$ is not self-framed, as will be the case in the WLP definition of method calls over gradual formulas, then start with the ones in the accessibility predicates and add the ones which occur in the non-accessibility part of $\postsub$ that are not already in the list. Finally, their duplicates due to aliasing should be added to the list.
	\end{itemize}
\end{enumerate}
%determined from the accessibility predicates in the substituted precondition of $m$ (including duplicates due to aliasing),

$\phi \overline{\ \div \ (e.f)_i}$ removes dependencies on the field accesses in the removal list computed above in the following way:
\begin{enumerate}
\item Transitively expand (in-)equalities and comparisons in $\phi$, so $\phi$ is now modified in this way
\item Determine the list of aliases to each variable or field access dereferenced to a final field value in $\phi$ using $\phi$
\item Use the list of aliases to each variable or field access dereferenced to a final field value in $\phi$ to replace any variable or field access in $\phi$ that aliases to a top-level variable being dereferenced in a field access (ie. $x$ in $x.f$ and $x.f.g$) in the list of removal that the $(e.f)_i$s come from. When substituting in multi-level field accesses (ie. $x.f.g.h$), the top-level object dereferences take precedence (ie. $x$ should be substituted if possible; if not possible for $x$, then $x.f$ should be next and so forth).
\item Remove conjunctive terms containing the $(e.f)_i$s from $\phi$
	\begin{itemize}
	\item Removal for all conjunctive terms is straightforward (remove the term containing it) except for conjunctive terms which contain the $(e.f)_i$s as an object being dereferenced. In this case, each $(e.f)_i$ should be substituted with an available alias in $\phi$; if no alias exists, then remove the term completely. The available alias should not be used if it exists in the list for removal that the $(e.f)_i$s come from or if the substitution of the alias results in a field access which contains or may be itself a field access that exists in the list for removal that the $(e.f)_i$s come from.
	\item The order in which the $(e.f)_i$s are removed from $\phi$ should be from the smallest to largest in length of dereferencing, ie. $x.f$ is removed before $x.f.h$ which is removed before $x.f.h.g$ and so forth; $(e.f)_i$s with lengths of two are removed before $(e.f)_i$s with lengths of three and so forth.
	\end{itemize}
\end{enumerate}

%and $\fphi_{acc}$ is a special formula of accessibility predicates that is described below (this description is similar to how computation for $\fphi_{acc}$ is done for the assert rule following this rule, so some details are left out here):

%\begin{enumerate}
%\item Start with a list of the accessibility predicates in $\fphi \div y \overline{\ \div \ (x.f)_i}$ and
%
%$\text{pre}(C,m)\left[z/this, \overline{x_i/\text{params}(C,m)_i}\right]$
%%\item Remove any accessibility predicate containing $y$
%%\item Add to the list the accessibility predicates in $\text{pre}(C,m)\left[z/this, \overline{x_i/\text{params}(C,m)_i}\right]$
%\item Add aliasing information from the actual conjoined accessibility predicates in $\fphi \div y \overline{\ \div \ (x.f)_i}$ and
%
%$\text{pre}(C,m)\left[z/this, \overline{x_i/\text{params}(C,m)_i}\right]$ to the non-accessibility parts of $\fphi \div y \overline{\ \div \ (x.f)_i}$ and
%
%$\text{pre}(C,m)\left[z/this, \overline{x_i/\text{params}(C,m)_i}\right]$ respectively. In other words, the accessibility predicates in $\fphi \div y \overline{\ \div \ (x.f)_i}$ $\left(\text{pre}(C,m)\left[z/this, \overline{x_i/\text{params}(C,m)_i}\right]\right)$ separated by the separating conjunction indicate that if the fields ultimately being accessed within two or more of them are the same, then the objects being dereferenced in those field accesses do not alias, so add this information to the non-accessibility part of $\fphi \div y \overline{\ \div \ (x.f)_i}$ $\left(\text{pre}(C,m)\left[z/this, \overline{x_i/\text{params}(C,m)_i}\right]\right)$ to not lose information
%\item Determine the list of aliases and the list of non-aliases to each variable or field access dereferenced to a final field value in the current list of accessibility predicates using
%
%$\mid \fphi \div y \overline{\ \div \ (x.f)_i} \mid \ast \left\vert \text{pre}(C,m)\left[z/this, \overline{x_i/\text{params}(C,m)_i}\right] \right\vert$
%\item Remove duplicate accessibility predicates, including accessibility predicates which are duplicate due to aliasing (the list of aliases and list of non-aliases for each variable and field access dereferenced to a final field value in an acc predicate should help)
%	\begin{enumerate}
%	\item If it is unknown whether two objects alias due to missing or conflicting information in $\fphi \div y \overline{\ \div \ (x.f)_i}$ and $\text{pre}(C,m)\left[z/this, \overline{x_i/\text{params}(C,m)_i}\right]$, then assume (for now) that they do not alias
%	\end{enumerate}
%\item $\fphi_{acc}$ is the list of non-duplicated accessibility predicates conjoined with the separating conjunction
%\end{enumerate}

Also, check that

$\wlp{y := z.m_C(\overline{x})}{\fphi} \overline{\ \div \ (e.f)_i} \div y \ast \postsub$

\hspace{1cm} $\imp \fphi$,

and
%$\fphi'_{acc} \ \ast \ \left\vert \wlp{y := z.m_C(\overline{x})}{\fphi} \overline{\div (x.f)_i} \right\vert \ \ast \ \left\vert \text{post}(C,m)\left[z/this, \overline{x_i/\text{old}(\text{params}(C,m)_i)}, y/result \right] \right\vert$

%\hspace{1cm} $\imp \fphi$, and

$\wlp{y := z.m_C(\overline{x})}{\fphi}$ is satisfiable.

%(\textbf{Note:} Can add extra check of $\wlp{y := z.m_C(\overline{x})}{\fphi} \imp z \neq null \ \ast \ \text{pre}(C,m)\left[z/this, \overline{x_i/\text{params}(C,m)_i}\right]$ to be safe).

where $\phi \div x$ is defined as above, $(e.f)_i$ is defined as a field access used in the substituted precondition of $m$ and coming from a list of field accesses used in the substituted precondition of $m$ computed in the same way as above except for replacing $\postsub$ with $\presub$, and $\phi \overline{\ \div \ (e.f)_i}$ is defined as below:

\begin{enumerate}
\item Transitively expand (in-)equalities and comparisons in $\phi$, so $\phi$ is now modified in this way
\item Remove conjunctive terms containing the $(e.f)_i$s as a whole from $\phi$. Do not remove conjunctive terms that contain an $(e.f)_i$ as an object being dereferenced
\end{enumerate}

%and $\fphi'_{acc}$ is determined in a similar fashion as describe for $\fphi_{acc}$ by replacing $\fphi \div y \overline{\ \div \ (x.f)_i}$ with $\wlp{y := z.m_C(\overline{x})}{\fphi} \overline{\div (x.f)_i}$ and $\text{pre}(C,m)\left[z/this, \overline{x_i/\text{params}(C,m)_i}\right]$ with $\text{post}(C,m)\left[z/this, \overline{x_i/\text{old}(\text{params}(C,m)_i)}, y/result \right]$.

\textbf{Important cases to consider:}

TBD -- have them just need to type them up
%What if $y = x_i$ for some argument $x_i$, $z$ could also be an argument, and $y = z$
%$\fphi = $
%
%POST $= acc(result.f) \ast acc(result.f.g) \ast x = result.f \ast z = result$

\textbf{Note:}

This rule assumes only $old(id)$ can show up in the postcondition of a method. $old(.)$ of anything else is not allowed, ie. $old(e.f)$ (for now).

\vspace{0.5cm}

$\wlp{assert\ \phi_a}{\fphi} = \fphi_{acc} \ \ast \mid \phi_a \mid \ast \mid \fphi \mid$

where $\mid \phi \mid$ means the formula $\phi$ without accessibility predicates and $\fphi_{acc}$ is the self-framed formula which contains the accessibility predicates that frame $\mid \phi_a \mid \ast \mid \fphi \mid$ and, in general, the accessibility predicates in $\phi_a$ and $\fphi$ that are not duplicated (even with respect to aliasing).

Also, check $\wlp{assert\ \phi_a}{\fphi} \imp \phi_a$, $\wlp{assert\ \phi_a}{\fphi} \imp \fphi$, and $\wlp{assert\ \phi_a}{\fphi}$ is satisfiable.

\textbf{Advice on how to compute $\fphi_{acc}$:}
\begin{enumerate}
% \item Transitively expand equalities in $\mid \phi_a \mid \ast \mid \fphi \mid$
\item Start with a list of the accessibility predicates in $\phi_a$ and $\fphi$
\item Add accessibility predicates to this list for self-framing $\phi_a$, as some may be missing since $\phi_a$ is not self-framed
	\begin{enumerate}
	\item Duplicates (including those due to aliasing) are fine, because they will be removed later
	\item But, not including obvious duplicates will improve performance
	\end{enumerate}
\item Add aliasing information from the actual conjoined accessibility predicates in $\phi_a$ and $\fphi$ to the non-accessibility parts of $\phi_a$ and $\fphi$ respectively. In other words, the accessibility predicates in $\phi_a$ ($\fphi$) separated by the separating conjunction indicate that if the fields ultimately being accessed within two or more of them are the same, then the objects being dereferenced in those field accesses do not alias, so add this information to the non-accessibility part of $\phi_a$ ($\fphi$) to not lose information
	\begin{enumerate}
	\item $acc(x.f) \ast acc(y.f) \ast x.f = 2$ implies $acc(x.f) \ast acc(y.f) \ast x.f = 2 \ast x \neq y$
	\item $acc(x.f) \ast acc(x.f.g) \ast acc(y.g) \ast acc(y.g.f) \ast x.f = 2$ implies \\ $acc(x.f) \ast acc(x.f.g) \ast acc(y.g) \ast acc(y.g.f) * x.f = 2 \ast x \neq y.g \ast y \neq x.f$
	\end{enumerate}
\item Determine the list of aliases and the list of non-aliases to each variable or field access dereferenced to a final field value in the current list of accessibility predicates using $\mid \phi_a \mid \ast \mid \fphi \mid$
	\begin{enumerate}
	\item Keep in mind that if $x.f \neq y.f$ then $x \neq y$ and that $x.f \neq y.f$ can be determined in many different ways, including through $x.f = 3 * y.f = 4$, so the SMT solver should be helpful and necessary for this
	\end{enumerate}
% Scan the conjunctive terms in $\mid \phi_a \mid \ast \mid \fphi \mid$ for field accesses
\item Remove duplicate accessibility predicates, including accessibility predicates which are duplicate due to aliasing (the list of aliases and list of non-aliases for each variable and field access dereferenced to a final field value in an acc predicate should help)
	\begin{enumerate}
	\item If it is unknown whether two objects alias due to missing or conflicting information in $\phi_a$ and $\fphi$, then assume (for now) that they do not alias
	\end{enumerate}
% For each field access, check if an accessibility predicate exists for it in $\fphi_{acc}$ (including if an aliased accessibility predicate exists for it; the list of aliases for each variable should help)
	% \begin{itemize}
	% \item If so, continue scanning for field accesses
	% \item If not, add the accessibility predicate for the field access to $\fphi_{acc}$ using the separating conjunction
	% \end{itemize}
\item $\fphi_{acc}$ is the list of non-duplicated accessibility predicates conjoined with the separating conjunction in an appropriate order (ie. $acc(x.f)$ comes before $acc(x.f.g)$)
\end{enumerate}

\textbf{Important cases to consider:}

$\mid \phi_a \mid \ast \mid \fphi \mid \ = x \neq null \ast x.f = 1 \ast x.f = y.f \ast y.f \neq p \ast y.f.f > 1$

$\mid \phi_a \mid \ast \mid \fphi \mid \ = x = y \ast x = z \ast x.f = 8 \ast y.f > 4$

$\mid \phi_a \mid \ast \mid \fphi \mid \ = x = y \ast y = z \ast z.f + 1 \leq 10 \ast \true$

$\mid \phi_a \mid \ast \mid \fphi \mid \ = x.f.f \neq y \ast x \neq p \ast p = q \ast x.f > y \ast y > z$

$\mid \phi_a \mid \ast \mid \fphi \mid \ = y > x.f \ast x.f > z \ast r \geq x.f \ast x.f \geq s$

$\phi_a = acc(x.f) \ast y = 2$ and $\fphi = acc(z.f) \ast z.f = 4$

$\phi_a = acc(x.f) \ast x.f = 4$ and $\fphi = acc(z.f) \ast z.f = 4$

\textbf{Assumptions:}

We assume that objects are only referred to in formulas through variables or as field accesses, variables or field accesses which refer to objects are not used in binary operations ($\oplus$), and variables or field accesses which refer to objects are not used in comparison operators other than $\neq$ and $=$.

%	\begin{cases}
%	 \fphi \ast \phi_a & if \ fp_s(\fphi') \cap fp_s(\phi_a') = \emptyset \\
%	 \overline{acc((x.f)_i)} \ast \fphi - F \ast \phi_a - F & otherwise
%	\end{cases}$
%
%where $\lcar x,f \rcar_i \in F = fp_s(\fphi') \cap fp_s(\phi_a')$ and $\phi - F$ means to remove the accessibility predicates which apply to elements of $F$.
%
%Also, $\fphi'$ and $\phi_a'$ are the transitively expanded versions of the corresponding formula with additional accessibility predicates added as necessary due to aliasing (see important cases below).
%
%\textbf{Important cases to consider:}
%
%$\fphi = acc(x.f) \ast x = p \ast p = q \ast a = b $
%
%--- becomes $\fphi' = acc(x.f) \ast x = p \ast p = q \ast x = q \ast a = b $
%
%--- and finally $\fphi' = acc(x.f) \ast acc(p.f) \ast acc(q.f) \ast x = p \ast p = q \ast x = q \ast a = b $
%
%$\fphi = acc(x.f) \ast x = y $ --- results in $\fphi' = acc(x.f) \ast acc(y.f) \ast x = y $

\vspace{0.5cm}

$\wlp{release \ \phi_a}{\fphi} = $

\vspace{0.5cm}

$\wlp{hold \ \phi_a \ \{s\}}{\fphi} = $

\section{Algorithmic WLP calculus definitions over gradual formulas}
\hspace{0.5cm}

$\twlp{s}{\fphi} = \wlp{s}{\fphi} $ where $s$ is not a call statement

\vspace{0.5cm}

$\twlp{skip}{? \ast \phi} = \ ? \ast \phi$

\vspace{0.5cm}

$\twlp{s_1;s_2}{? \ast \phi} = \twlp{s_1}{\twlp{s_2}{? \ast \phi}} $

\vspace{0.5cm}

% IF use this then add neq stuff to it - see static rule
%$\twlp{T \ x := e}{? \ast \phi} = \ ? \ast \phi[e/x] \ast e = e$
%
%\textbf{Important cases to consider:}
%
%$? \ast \phi = \ ? \ast p = q \ast y.f = 2$ --- no mention of $e$ or $x$ for substitution; need extra $\ast \ e = e$ because of this case
%
%\textbf{OR}

$\twlp{T \ x := e}{? \ast \phi} = \ ? \ast \wlp{T \ x :=e}{\phi}$
%	 \begin{cases}
%	 ? \ast \phi[e/x] & if \ \phi[e/x] \imp acc(e) \\
%	 ? \ast acc(e) \ast \phi[e/x] & otherwise
%	\end{cases}$

Check that $\twlp{T \ x := e}{? \ast \phi} \ast x = e \timp ? \ast \phi$ and that $\twlp{T \ x := e}{? \ast \phi}$ is satisfiable.

\textbf{Note:} The use of WLP here is fine even though $\phi$ is not self-framed; it will still compute the correct static part for $\twlp{T \ x := e}{? \ast \phi}$.

\textbf{Important cases to consider:}

Similar to the ones for the non-gradual version; replacing $\fphi$ with $\phi$ and appending ? to the front of $\phi$ on either side of the = sign.

\vspace{0.5cm}

%$\twlp{\eif{x \odot y}{s_1}{s_2}}{? \ast \phi} = $
%
%\vspace{0.5cm}

$\twlp{x.f := y}{? \ast \phi} =
	\begin{cases}
	 ? \ast \phi[y/x.f] & if \ \phi[y/x.f] \imp acc(x.f) \\
	 ? \ast acc(x.f) \ast \phi[y/x.f] & otherwise
	\end{cases}$

Check that $\twlp{x.f := y}{? \ast \phi} \ast x.f = y \timp \ ? \ast \phi$ and that $\twlp{x.f := y}{? \ast \phi}$ is satisfiable.

\textbf{Important cases to consider:}

$? \ast \phi = \ ? \ast acc(x.f) \ast acc(x.f.f) \ast x = y $

\vspace{0.5cm}

$\twlp{x := new\ C}{? \ast \phi} =
	\begin{cases}
	 ? \ast \phi \div x & if \ (\phi \div x) \ast \overline{x \neq e_i} \ast \overline{acc(x.f_i)} \imp \phi \\
	 undefined & otherwise
	\end{cases}$

where fields$(C) = \overline{T_i \ f_i}$, $\phi \div x$ means to transitively expand (in-)equalities ($\odot$) and then remove conjunctive terms containing $x$, and $\overline{x \neq e_i}$ are conjunctive terms in $\phi$.

Check that $\twlp{x := new\ C}{? \ast \phi} \ast x \neq null \ast \overline{x \neq e_i} \ast \overline{acc(x.f_i)} \timp ? \ast \phi$ and that $\twlp{x := new\ C}{? \ast \phi}$ is satisfiable.

\textbf{Important cases to consider:}

Similar to the ones for the non-gradual version; replacing $\fphi$ with $\phi$ and appending ? to the front of $\phi$ on either side of the = sign.

\vspace{0.5cm}

$\twlp{y := z.m(\overline{x})}{\tphi} = undefined$

\vspace{0.5cm}

$\twlp{y := z.m_C(\overline{x})}{\tphi} =
	\begin{cases}
		\wlp{y := z.m_C(\overline{x})}{\fphi} & if \ \tphi = \fphi, \ \pre = \fphi_p, \\
		& \hspace{0.5cm} \post = \fphi_q \\
		& \hspace{0.5cm} for \ \fphi, \ \fphi_p, \ \fphi_q \in \formuladef \\
	? \ast \wlp{y := z.m_C(\overline{x})}{\text{static}(\tphi)} & if \ \tphi = \ ? \ast \phi, \ \pre = \fphi_p, \\
		& \hspace{0.5cm} \post = \fphi_q \\
		& \hspace{0.5cm} for \ \phi, \ \fphi_p, \ \fphi_q \in \formuladef \\
	? \ast \wlp{y := z.m_C(\overline{x})}{\text{static}(\tphi)} & if \ \pre = \ ? \ast \phi_p,\\
	where \ \text{static}(\pre) \ is \ used	& \hspace{0.5cm} \post = \fphi_q \\
	for \ \pre \ in \ \text{WLP} \ comp & \hspace{0.5cm} for \ \phi_p, \ \fphi_q \in \formuladef \\
	? \ast z \neq null \ast \text{static}(\pre)[...]& if \ \post = \ ? \ast \phi_q \\
	 & \hspace{0.5cm} for \ \phi_q \in \formuladef
	\end{cases}
$

where $\text{static}(\pre)[...] = \text{static}(\pre)\left[z/this, \overline{x_i/\text{params}(C,m)_i}\right]$, and $\wlp{y := z.m_C(\overline{x})}{\fphi}$ is designed to handle non-self-framed formulas, $\phi$, as well as self-framed formulas, $\fphi$.

Check that:

\begin{itemize}
\item $\wlp{y := z.m_C(\overline{x})}{\fphi} \overline{\ \div \ (e.f)_i} \div y \ \ast $

\hspace{0.5cm} $ \postsub \imp \fphi$

if $\tphi = \fphi$, $\pre = \fphi_p$, and $\post = \fphi_q$ for $\fphi, \ \fphi_p, \ \fphi_q \in \formuladef$

where $\phi \div x$, $(e.f)_i$, and $\phi \overline{\ \div \ (e.f)_i}$ are defined for this check as they were above (for the non-gradual weakest liberal precondition method call definition check defined previously)

\item $? \ast \text{static}(\twlp{y := z.m_C(\overline{x})}{\tphi}) \overline{\ \div \ (e.f)_i} \div y \ \ast$

\hspace{0.5cm} $\text{static}(\post)\left[z/this, \overline{x_i/\text{old}(\text{params}(C,m)_i)},y/result\right] \timp \tphi$

if $\pre = \ ? \ast \phi_p$, for $\phi_p \in \formuladef$

where $\phi \div x$ and $\phi \overline{\ \div \ (e.f)_i}$ are defined for this check as they were above (for the non-gradual weakest liberal precondition method call definition check defined previously), and $(e.f)_i$ is defined as a field access used in $\text{static}(\twlp{y := z.m_C(\overline{x})}{\tphi}) \div y$ and coming from a list of field accesses used in $\text{static}(\twlp{y := z.m_C(\overline{x})}{\tphi}) \div y$ computed in the same way as the above check except for replacing $\presub$ with \\ $\text{static}(\twlp{y := z.m_C(\overline{x})}{\tphi}) \div y$

\item $? \ast \text{static}(\twlp{y := z.m_C(\overline{x})}{\tphi}) \overline{\ \div \ (e.f)_i} \div y \ \ast$

\hspace{0.5cm} $\text{static}(\post)\left[z/this, \overline{x_i/\text{old}(\text{params}(C,m)_i)},y/result\right] \timp \tphi$

if $\pre = \fphi_p$, for $\fphi_p \in \formuladef$

where $\phi \div x$, $(e.f)_i$, and $\phi \overline{\ \div \ (e.f)_i}$ are defined for this check as they were above (for the non-gradual weakest liberal precondition method call definition check defined previously)
\end{itemize}

Also, check that
$\twlp{y := z.m_C(\overline{x})}{\tphi}$ is satisfiable.

\textbf{Important cases to consider:}

TBD

\vspace{0.5cm}

$\twlp{assert\ \phi_a}{? \ast \phi} = \ ? \ast \wlp{assert\ \phi_a}{\phi}$

Check that $\twlp{assert\ \phi_a}{? \ast \phi} \timp ? \ast \phi$, $\twlp{assert\ \phi_a}{? \ast \phi} \timp \phi_a$, and that

\noindent $\twlp{assert\ \phi_a}{? \ast \phi}$ is satisfiable.

\textbf{Note:}

Since $\phi$ is not self-framed, then the resulting $\wlp{assert\ \phi_a}{\phi}$ will not necessarily be self-framed. This is okay due to ? accounting for the lack of self-framing and since $\wlp{assert\ \phi_a}{\phi}$ does not lose any information from $\phi_a$ or $\phi$ (in fact, it gains information with respect to $\phi_a$). So, allow this call to $\wlp{assert\ \phi_a}{\fphi}$ even though $\phi$ is not self-framed.

\textbf{Important cases to consider:}

Similar to the ones for the non-gradual version; replacing $\fphi$ with $\phi$.

\vspace{0.5cm}
%
%$\twlp{release \ \phi_a}{? \ast \phi} = $
%
%\vspace{0.5cm}
%
%$\twlp{hold \ \phi_a \ \{s\}}{? \ast \phi} = $
%
%\vspace{0.5cm}

\textbf{Note:} The static parts of gradual formulas do not need to be self-framed unless the gradual formula is completely precise (completely static). The imprecision can account for the framing.

\section{Gradual formula implication and satisfiability}

\subsection{Gradual formula implication implementation advice}

\[ \inferrule*[right=ImplStatic]
   {\fphi_1 \imp static(\tphi_2) }
   {\fphi_1 \timp \tphi_2}
\]

\[
\inferrule*[right=ImplGrad]
   {\fphi \in \satdef \\ \fphi \imp \phi_1 \\ \fphi \imp static(\tphi_2)}
   {? \ast \phi_1 \timp \tphi_2}
\]

$\implsdef$ should be implemented directly for determining gradual formula implication. $\implgdef$ should be implemented by checking $\fphi$ is satisfiable, is self-framed, implies $\phi_1$, and implies $static(\tphi_2)$ and following this advice for determining $\fphi$ (determining $\fphi$ is similar to computing $\wlp{assert\ \phi_a}{\fphi}$, so some of the detailed advice is left out since it is mentioned above):

\begin{enumerate}
\item Start with a list of the accessibility predicates in $\phi_1$ and $static(\tphi_2)$
\item Add accessibility predicates to this list for self-framing $\phi_1$ and $static(\tphi_2)$, as some may be missing since $\phi_1$ and $static(\tphi_2)$ is/may not be self-framed
	\begin{enumerate}
	\item Duplicates (including those due to aliasing) are fine, because they will be removed later
	\item But, not including obvious duplicates will improve performance
	\end{enumerate}
\item Add aliasing information from the actual conjoined accessibility predicates in $\phi_1$ and $static(\tphi_2)$ to the non-accessibility parts of $\phi_1$ and $static(\tphi_2)$ respectively. In other words, the accessibility predicates in $\phi_1$ ($static(\tphi_2)$) separated by the separating conjunction indicate that if the fields ultimately being accessed within two or more of them are the same, then the objects being dereferenced in those field accesses do not alias, so add this information to the non-accessibility part of $\phi_1$ ($static(\tphi_2)$) to not lose information
\item Determine the list of aliases and the list of non-aliases to each variable or field access dereferenced to a final field value in the current list of accessibility predicates using $\mid \phi_1 \mid \ast \mid static(\tphi_2) \mid$
\item Remove duplicate accessibility predicates, including accessibility predicates which are duplicate due to aliasing (the list of aliases and list of non-aliases for each variable and field access dereferenced to a final field value in an acc predicate should help)
	\begin{enumerate}
	\item If it is unknown whether two objects alias due to missing or conflicting information in $\phi_1$ and $static(\tphi_2)$, then assume (for now) that they do not alias
	\end{enumerate}
\item Then $\fphi = \fphi_{acc} \ \ast \mid \phi_1 \mid \ast \mid static(\tphi_2) \mid$, where $\fphi_{acc}$ is the list of non-duplicated accessibility predicates conjoined with the separating conjunction in an appropriate order (ie. $acc(x.f)$ comes before $acc(x.f.g)$)
\end{enumerate}

\subsection{Gradual formula satisfiability implementation advice}
For $\tphi$, if $\tphi = \fphi$, then check satisfiability as usually for fully-precise formulas. If $\tphi = \ ? \ast \phi$, then $? \ast \phi$ is satisfiable iff $\phi$ is satisfiable. Also, there is no need to modify $\phi$ to be self-framed before checking satisfiability. Any attempt to modify $\phi$ to be self-framed would not affect satisfiability, as this modification should ensure the resulting formula is satisfiable if $\phi$ is satisfiable by relying on existing information in $\phi$. If $\phi$ was unsatisfiable, then this modification should not be made, since it relies on information in $\phi$ to be correct. But, if it was made it would not make the resulting formula satisfiable.

% Any additions to $\phi$ from $?$ should not affect satisfiability, as the additions should be made only if the resulting formula is satisfiable if $\phi$ was satisfiable, and if $\phi$ was unsatisfiable, then it would not become satisfiable with the addition of more information from $?$.

In general, it is recommended to check self-framing and satisfiability separately, ie. a formula can be satisfiable and not self-framed.

\end{document}


\chapter{Fringe field compensation using solenoids} \label{app-C}

In this appendix, we illuminate the finding from \cite{Holmes2018} that in an otherwise linear lattice with equal tunes, nonlinear fringe fields tend to eliminate any cross-plane correlations in the beam, but that this effect can be mitigated by solenoid magnetic fields and/or the beam's electric field.

To demonstrate this, a Danilov distribution matched to a linearized version of the SNS ring was generated. Fringe fields were turned on and the distribution was tracked without space charge. Fig.~\ref{fig:fringe_a} shows the turn-by-turn evolution.

There is nonlinear coupling between the horizontal and vertical motion, and the final distribution is a superposition of rotating and counter-rotating modes. We take this to be due to the difference resonance $\nu_x - \nu_y \approx 0$. In Fig.~\ref{fig:fringe_b}, a solenoid magnet is added to the ring. The cross-plane correlations are now mostly maintained. One explanation for this phenomenon is that since the solenoid is a coupled element, it splits the tunes of the system — $\nu_{x, y} \rightarrow \nu_{1, 2}$ with $\nu_1 \ne \nu_2$ — so the difference resonance is avoided.

In Fig.~\ref{fig:fringe_c}, the simulation is repeated with the inclusion of space charge instead of the solenoid magnet. An intensity of $10^{14}$ is used and the bunch length is equal to the ring length. Since the beam's electric field also introduces linear coupled forces, space charge has a stabilizing effect like that of the solenoid magnetic field.

It is recommended, however, that solenoid magnets be added to the SNS ring to produce a Danilov-like distribution using the elliptical painting method. The difficulty is that fringe fields seem to dominate at the beginning of injection when the transverse displacement is maximum and before a round beam has been formed. Additionally, without a solenoid in the ring, the quality of the final distribution is very sensitive to the difference in horizontal and vertical tunes. With a solenoid in the ring, elliptical trajectories at the injection point are produced for a wide range of original horizontal and vertical tunes.

\begin{figure}[!p]
    \centering
    \includegraphics[width=\textwidth]{Images/chapter3/fringe.png}
    \caption{Danilov distribution tracked in the SNS ring. Space charge \textit{is not} included. Fringe fields are the only nonlinear external effect.}
    \label{fig:fringe_a}
    \vspace*{3cm}
\end{figure}

\begin{figure}[!p]
    \centering
    \includegraphics[width=\textwidth]{Images/chapter3/fringe_solenoid.png}
    \caption{Danilov distribution tracked in the SNS ring with a solenoid added to the ring. Space charge \textit{is not} included. Fringe fields are the only nonlinear external effect.}
    \label{fig:fringe_b}
    \vspace*{3cm}
\end{figure}

\begin{figure}[!p]
    \centering
    \includegraphics[width=\textwidth]{Images/chapter3/fringe_spacecharge.png}
    \caption{Danilov distribution tracked in the SNS ring. Space charge \textit{is} included. Fringe fields are the only nonlinear external effect.}
    \label{fig:fringe_c}
    \vspace*{3cm}
\end{figure}


\chapter{Conclusion}
\section{Summary}
The aim of this thesis was to simulate structural colors produced by a directional light source upon diffraction by quasyperiodic nanostructures in nature. Our goal also included to use explicitly measured nano-scaled patches to simulate these structural colors at interactive rates. For this purpose we developed a BRDF model based on J. Stam's work described in his paper about diffraction shaders. Furthermore, we implemented our model as a rendering program.

Rendering the effect of diffraction for a measured nanostructure, described as a discrete height field was previously not done and may therefore be considered as a novelty. \\

Our BRDF model is based on a Taylor series approximation and a Gaussian windowing approach, formulated in a way such that many computationally expensive terms$\footnote{Such as computing different powers of the two dimensional Fourier transformation of the given height field.}$ in our model can be precomputed. These precomputed terms can be accessed by performing a lookup during the shading stages, which reduce the overall runtime complexity of our shader. Nevertheless, our basic implementation (FLSS) is not interactive for most of the present GPU platforms. Therefore, we also devised some optimization into my NMM shading approach, to render scenes interactively. We also formulated a completely different approach, the PQ shading approach, which performs sinc-interpolation, assuming the given height field patch is periodically distributed on a surface. Since the height field of natural diffraction gratings is only a quasiperiodic function, the PQ approach does not appropriately model the reality and thus does not produce reliable results. In addition, we evaluated the quality of all our shading approaches applied on different surface gratings. Finally, we produced some renderings BRDF maps and renderings on a snake mesh using all our shading approaches applied using different surface gratings. \\

For future work we could think of extending our model in a way such that it can handle multilayer diffraction grating. This would allow us to simulate structural colors due to multilayer interference wave-effects, which can be observed on wings of certain butterfly species. \\

This thesis contributed also to a follow up paper which discusses a fast implementation of the FLSS shading approach. This paper also contains a complete comparison with Stam's Shading approach. For further information, please have a look at the paper $\cite{daljitpaper}$.

\section{Personal Experiences}
I always knew that eventually I will write a thesis in the field of computer graphics. After having attended the $\emph{Computer Graphics}$ class held by Mr. Zwicker, I was certain that I will definitely write my thesis at the Computer Graphics Group at the University of Bern. Since I already acquired a Minor degree in Mathematics I and am very interested in topics like numerics, differential equations and differential geometry, I asked for a thesis subject involving some Mathematics. I have to admit that, after having read J. Stam's paper about diffraction shaders at the very beginning of my thesis, I thought this topic would exceed my knowledge in Mathematics and Physics. And it actually did, but I decided not to give up. \\

During working on my thesis I learnt new concepts in Physics relating to wave-interference, diffraction of waves, diffraction gratings and deepened my knowledge of wave theory for light. Regarding Mathematics I learned quite a lot about the different kind of Fourier transforms, about Windowing techniques for signal processing and about BRDF models. I felt it as a satisfactory experience to use and apply all the knowledge which I have acquired during the time as a bachelor student. Furthermore, this thesis gave me the opportunity to program quite a lot. Hence I could strengthen my programming skills in Java and OpenGL's shading language GLSL. Altogether it was a rewarding experience for me to write a Bachelor thesis at the Computer Graphics Group.

\section{Acknowledgment}
First, I would like to thank Mr. Zwicker for giving me the opportunity to write a Bachelor thesis at the Computer Graphics Group at the University of Bern. \\

Foremost, I would like to express my sincere gratitude to my advisor Mr. Daljit Singh Dhillon for his continuous support of my study, his patience, motivation, enthusiasm, and knowledge. His guidance and active support helped me quite a lot while deriving our BRDF model, developing it and evaluating the shaders and while writing this thesis. \\

Last but not least, I would like to thank my mother, Manuela Single and my brother Patrik Single and also to my close friend, Radischa Iyadurai for supporting me morally throughout during this thesis.


\documentclass[11pt]{article}

\usepackage{amsmath,amssymb,amsthm}
\usepackage{hyperref}
\usepackage{mathpartir}
\usepackage[barr]{xy}
\usepackage{stmaryrd}
\usepackage{supertabular}
\usepackage{geometry}
\usepackage{fullpage}
\usepackage{mdframed}

\newtheorem{theorem}{Theorem}
\newtheorem{lemma}{Lemma}
\newtheorem{definition}{Definition}
\newtheorem{example}{Example}

\newcommand{\interp}[1]{[\negthinspace[#1]\negthinspace]}
\newcommand\myeq{\mathrel{\overset{\makebox[0pt]{\mbox{\normalfont\tiny\sffamily def}}}{=}}}

\title{Monoidal-Annex}
\author{Preston Keel \and Dr. Harley Eades}
\begin{document}
\maketitle
\tableofcontents

\section{Monoidal Categories}
\label{sec:monoidal_categories}
\input{Monoidal_Category}

\section{Symmetric Monoidal Categories}
\label{sec:symmetric_monoidal_categories}
\input{Symmetric_Monoidal_Categories}

\section{Monoidal Double Category}
\label{sec:monoidal_double_category}
\input{Monoidal_Double_Cat}

\section{The Double Category of (co)Lax Monoidal Functors}
\label{sec:the_double_category_of_colax_monoidal_functors}
\input{mono-double-cat}
% section the_double_category_of_(co)lax_monoidal_functors (end)

\section{Monoidal Monads}
\label{sec:monoidal_monads}
\input{Monoidal_Monad}

\bibliography{references}
\bibliographystyle{plain}
\end{document}


%
% Theorems and the like
%
\newtheorem{theorem}{Theorem}
%\newtheorem{theorem}{Theorem}[chapter]
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{conjecture}[theorem]{Conjecture}
\newtheorem{definition}[theorem]{Definition}
\newtheorem{property}[theorem]{Property}
\newtheorem{observation}[theorem]{Observation}
\newcounter{excount}
\newcommand{\exercise}{\par\medskip\noindent\addtocounter{excount}{1}{\mbox{\bf Exercise\ \theexcount\quad}}}
%
% Slides
%
\newcounter{slipage}
\newcommand{\Beginslide}[1]{\par\noindent\addtocounter{slipage}{1}{\bf #1}\hfil(\theslipage)\break\vskip-5mm}
\newcommand{\Endslide}{\clearpage}
%
% Pictures
%
\setlength{\unitlength}{1 true mm}
%
% Lines
%
\newcommand{\bigline}{\hrule width\hsize height 1mm depth 0mm}
\newcommand{\linie}{\vrule width\hsize height 1mm depth 0mm}
%
% Semantics
%
\newcommand{\id}[1]{{\it #1}}
\def\Dom{\mathop{\rm Dom}\nolimits}
\def\boxml#1{\hbox{\tt #1}}
\def\boxit#1{\hbox{\it #1\/}}
\newcommand{\ts}{\vdash}
\newcommand{\dts}{\models}
\newcommand{\ra}{\Rightarrow}
\newcommand{\ar}[1]{\mathop{\vbox{\ialign{##\crcr % right arrow with
                                                  % argument on top
       \noalign{\kern 2pt\nointerlineskip}\crcr
       $\hfil\scriptstyle\kern 6pt{#1}\kern 6pt\hfil$\crcr
       \noalign{\kern-2pt\nointerlineskip}\crcr
       \rightarrowfill\crcr}}}\nolimits}
\newcommand{\rulesec}[2]{\subsubsection*{{\bf#1}\hfill\fbox{#2}}}
\def\emptymap{\{\}}
\long\def\proof{\noindent\hangindent=10mm\hangafter=1{\bf Proof\  }}
%
% Displays of ML code
%
\newenvironment{newmltabbing}{\begingroup\catcode`\~=\active\begin{tabbing}}{\end{tabbing}\endgroup}
\newenvironment{mltabbing}{\begingroup\catcode`\_=\active\catcode`\#=\active\catcode`\~=\active\begin{tabbing}}{\end{tabbing}\endgroup}

{\catcode`\_=\active \def_{\_}%
\catcode`\#=\active \def#{\#}%
\catcode`\~=\active \def~{$\sim$}}
%
%  Refereeing comments   (\nc is just short for \nextcomment)
%
\def\nextcomment#1{\par\smallskip\noindent{\bf#1:\quad}}
\def\nc#1{\nextcomment{#1}}
%