\chapter{Simulation}
% put these two lines after every \chapter{} command
\vspace{-2em}
\minitoc
To implement and research various FDIR systems on satellites an simulation of satellite dynamics and kinematics is developed. The focus of this thesis is on small satellites and more specifically cubesats. For the simulation of the ADCS of the satellite \cite{auret2012design, JansevanVuuren2015, Jordaan2016} were referenced during the development of the satellite simulation. The simulation was developed in Python to simulate the dynamics and kinematics during a satellite orbit. The faults for the subsystems are also developed within the simulation and will be discussed within this chapter.

\section{Attitude Determination and Control System}

For the mission of the specific satellite in this document the main operational goal of the Attitude Determination and Control System (ADCS) on this specific satellite mission is to control the payload to point towards the centre of the earth.

\subsection{Coordinate Frames}
The coordinate frames in aerospace is a fundamental part of the ADCS. To determine the orientation and position of an object, it should be relative to a fixed frame. Consequently, the Earth inertial coordinate (EIC) frame is the fixed frame from which every other frame is relative to.

A coordinate frame consists of three orthogonal vectors which is commonly referred to as x, y, and z. The axis of the coordinate frame is appropriately named as X-axis, Y-axis and Z-axis as seen in Figure... A vector (r) within the current coordinate frame can thus be expressed as
\begin{equation}
\overrightarrow{r} = x\overrightarrow{i} + y\overrightarrow{j} + z\overrightarrow{k}
\end{equation}
where the magnitude of $\overrightarrow{r}$ is denoted as $|\overrightarrow{r}|$ and is equal to
\begin{equation}
|\overrightarrow{r}| = \sqrt{x^2 + y^2 + z^2}.
\end{equation}

The Earth-centered coordinate frames are dived into two, namely the EIC and earth fixed coordinate (EFC) frame. EFC is fixed to the earth and rotates with it. This frame is important with respect to where the satellite position is with regards to position's on earth, such as the ground station. It is also import for the modelling of the geomagnetic fields.

% Insert a figure of the earth coordinate frames here

The EIC is defined as the Z-axis pointing towards the north pole, the X-axis pointing towards the Vernal Equinox, $\Upsilon$, and the Y-axis completing the orthogonal set. The EFC is a copy of the EIC, with the Z-axis being identical, however the EFC rotates with the earth. The EFC in relation to the EIC can be expressed by a single angle of rotation, which is the Greenwich Hour Angle (GHA), $\alpha_G$. With the knowledge of $t$ --- the elapsed time since $t_0$, $w_E$ --- the angular rate of the earth, and $\alpha_{G,0}$ --- the GHA at $t = t_0$, $\alpha_G$ can be calculated as
\begin{equation}
\alpha_G = w_Et + \alpha_{G,0}
\end{equation}
To transform a vector from one coordinate frame to another, a transformation matrix, $\boldsymbol{A}$, is required. For example vector $\overrightarrow{r}_{EFC}$ can be transformed to $\overrightarrow{r}_{EIC}$ with
\begin{equation}
\overrightarrow{r}_{EIC} = \boldsymbol{A}^{EIC}_{EFC}\overrightarrow{r}_{EFC}
\end{equation}
with $\boldsymbol{A}^{EIC}_{EFC}$ being the EFC-to-EIC transformation matrix. Due to the definition of both coordinate frames, $\boldsymbol{A}^{EIC}_{EFC}$ can be defined .

\begin{equation}
\boldsymbol{A}^{EIC}_{EFC} =
\begin{bmatrix}
	cos(\alpha_G) & -sin(\alpha_G) & 0\\
	sin(\alpha_G) & cos(\alpha_G) & 0 \\
	0 & 0 & 1
\end{bmatrix}
\end{equation}

To determine the satellite position, satellite coordinate frames must be used. Three satellite-centred coordinate frames are used, namely the inertial-reference coordinate frame (the satellite does not rotate), the orbit-referenced coordinate (ORC) frame and the satellite body coordinate (SBC) frame. The IRC frame is only acknowledged, since it is the frame that is fixed (as it does not rotate around the centre of the satellite), however it changes position with the orbit of the satellite. This frame is not used to determine the position of the satellite and will not be referenced for the remainder of this document.

The ORC frame changes location as the satellite moves, however the Z-axis is always pointing towards the centre of the earth, with the Y-axis being the anti-normal and the X-axis completing the orthogonal set. To transform a vector from the EIC frame to the ORC frame the unit position vector, $\overrightarrow{r}_{sat}$ and the unit velocity vector, $\overrightarrow{v}_{sat}$ in EIC \cite{Chen_ground-target}.

\begin{equation}
	\boldsymbol{A}^{ORC}_{EIC} =
	\begin{bmatrix}
		\hat{u} & \hat{v} & \hat{w}\\
	\end{bmatrix}^T
\end{equation}
where
\begin{equation}
\hat{w} = -\frac{r_{sat}}{||r_{sat}||}
\end{equation}
\begin{equation}
\hat{v} = -\frac{r_{sat} \times v_{sat}}{||r_{sat} \times v_{sat}||}
\end{equation}
\begin{equation}
\hat{u} = \hat{v} \times \hat{w}
\end{equation}

The SBC frame is the frame fixed to the satellite and it is the relative rotation of the satellite in relation to the ORC. Thus for the mission of this satellite it is required that the SBC and ORC frames coincide. For the transformation of a vector from the ORC to SBC frame, the direct cosine matrix (DCM) also referred to as $\boldsymbol{A}$ or $\boldsymbol{A}^{SBC}_{ORC}$ is used. For the remainder of the document the DCM will be referred to as $\boldsymbol{A}^{SBC}_{ORC}$ to avoid any confusion. The calculation of this transformation matrix will be discussed in $\S$\ref{subsection_quaternions}.

% Insert a figure of the satellite coordinate frames here

\subsection{Attitude}
\label{subsection_quaternions}
To determine the attitude of an object, a model must be used to determine the rotation of an object in three dimensions. For this the visual and intuitive example of the Euler angles exist. Euler angles are the rotation of an object around three orthogonal axis, that change orientation with the rotation of the object. The three axes, denoted by X, Y and Z rotate with the object as depicted in Figure~\ref{Figure-Euler_angles}.

\begin{figure}[h!tb]
\label{Figure-Euler_angles}
\begin{tikzpicture}[scale=2.5,tdplot_main_coords]
	%\tdplotsetmaincoords{70}{110}
	% Set origin of main (body) coordinate system
	\coordinate (O) at (0,0,0);

	% Draw main coordinate system
	\draw[red, ,->] (0,0,0) -- (1,0,0) node[anchor=north east]{$x_{\mathcal{I}}$};
	\draw[red, ,->] (0,0,0) -- (0,1,0) node[anchor=north west]{$y_{\mathcal{I}}$};
	\draw[red, ,->] (0,0,0) -- (0,0,1) node[anchor=south]{$z_{\mathcal{I}}$};

	% Intermediate frame 1
	\tdplotsetrotatedcoords{-60}{0}{0}
	\draw[tdplot_rotated_coords,->, blue] (0,0,0) -- (1,0,0) node[anchor=north east]{$x'$};
	\draw[tdplot_rotated_coords,->, blue] (0,0,0) -- (0,1,0) node[anchor=west]{$y'$};
	\draw[tdplot_rotated_coords,->, blue] (0,0,0) -- (0,0,1) node[anchor=west]{$z'$};

	\tdplotsetrotatedthetaplanecoords{90}
	%draw theta arc and label
	%\tdplotdrawarc[tdplot_rotated_coords,->,color=blue]{(0,0,0)}{0.5}{0}{350}{anchor=south west,color=gray}{$\alpha$}
	\tdplotdrawarc[tdplot_rotated_coords,->,color=gray]{(0,0,0)}{0.5}{80}{90}{anchor=south west,color=gray}{$\alpha$}
	\tdplotdrawarc[tdplot_rotated_coords,->,color=gray]{(0,0,0)}{0.5}{170}{180}{anchor=south west,color=gray,  yshift = -15 pt}{$\alpha$}

	%% Intermediate frame 2
	\tdplotsetrotatedcoords{-60}{15}{0}
	\draw[,tdplot_rotated_coords,->, green] (0,0,0) -- (1,0,0) node[anchor=
	north]{};
	\draw[,tdplot_rotated_coords,->, green] (0,0,0) -- (0,1,0)
	node[anchor=west]{$y''$};
	\draw[,tdplot_rotated_coords,->, green] (0,0,0) -- (0,0,1)
	node[anchor=south]{$z''$};
	\tdplotsetrotatedthetaplanecoords{60}
	%draw theta arc and label
	%\tdplotdrawarc[tdplot_rotated_coords,->,color=green]{(0,0,0)}{0.5}{0}{350}{anchor=north,color=gray}{$\beta$}
	\tdplotdrawarc[tdplot_rotated_coords,->,color=gray]{(0,0,0)}{0.5}{80}{90}{anchor=north,color=gray}{$\beta$}
	\tdplotdrawarc[tdplot_rotated_coords,->,color=gray]{(0,0,0)}{0.5}{310}{320}{anchor=south west,color=gray}{$\beta$}
	%
	% Rotate to final frame
	\tdplotsetrotatedcoords{-60}{15}{45}
	\draw[thick,tdplot_rotated_coords,->, cyan] (0,0,0) -- (1,0,0)
	node[anchor=west]{$x_{\mathcal{B}}$, \textcolor{green}{$x''$}};
	\draw[thick,tdplot_rotated_coords,->, cyan] (0,0,0) -- (0,1,0) node[anchor=west]{$y_{\mathcal{B}}$};
	\draw[thick,tdplot_rotated_coords,->, cyan] (0,0,0) -- (0,0,1) node[anchor=south]{$z_{\mathcal{B}}$};

	\tdplotsetrotatedthetaplanecoords{30}
	%draw theta arc and label
	%\tdplotdrawarc[tdplot_rotated_coords,->,color=cyan]{(0,0,0)}{0.5}{0}{350}{anchor=north,color=gray}{$\gamma$}
	\tdplotdrawarc[tdplot_rotated_coords,->,color=gray]{(0,0,0)}{0.5}{215}{225}{anchor=north,color=gray, yshift = 15pt}{$\gamma$}
	\tdplotdrawarc[tdplot_rotated_coords,->,color=gray]{(0,0,0)}{0.5}{328}{338}{anchor=south west,color=gray}{$\gamma$}

\end{tikzpicture}
\end{figure}

Euler angles and DCM will be acknowledged and explained why they are not applicable.
In some literature the first and last quaternion are swapped.

\subsection{Satellite Kinematics and Dynamics}

\subsection{Rungka-kutta}

\section{Environment}
\subsection{Earth Orbit}
Earth orbit according to sgp4 and also the placement of the earth sensor.

Show plot of 3D earth orbit...

\subsection{Sun}
The calculations for the sun position and also the placement of the coarse and fine sun sensor.

Show graph of sun plot...

\subsection{Geomagnetic field}

\begin{equation}
\label{Eq-Geomagnetic_field}
V(r_s,\theta, \lambda) = R_E \sum_{n=1}^{k}\left(\frac{R_E}{r_s}^{n+1}\right)\sum_{m=0}^{n}\left(g_n^mcos(m\lambda) + h_n^msin(m\lambda)\right)P_n^m(\theta)
\end{equation}

Show graph of geomagnetic plot...

\section{Sensor models}
\subsection{Position of Sensors and Field of View}

\subsection{Noise}

\section{Disturbance models}
\subsection{Gravity Gradient}

\subsection{Aerodynamic Disturbance}
\cite{Steyn2014}

\subsection{Wheel Imbalance}

\section{Attitude Determination}
In this section discuss the Kalman filter.

\subsection{Kalman Filter}
\cite{Jones2017}

\section{Attitude Control}
Magnetic control during detumbling
Reaction wheel control during normal operation

\section{Constellations}
Explain the design of the satellite constellations and the algorithms to run communicate between satellites.

\begin{algorithm}

	\SetKwInOut{Input}{Input}
	\SetKwInOut{Output}{Output}

	\SetKwData{Left}{left}
	\SetKwData{This}{this}
	\SetKwData{Up}{up}
	\SetKwFunction{Union}{Union}
	\SetKwFunction{FindCompress}{FindCompress}

	\Indm
	\Input{Description of the input to the algorithm.}
	\Output{Description of the output from the algorithm.}
	\Indp

	\BlankLine

	\emph{Initialize hyperparameters}\;
	Get initial data from satellite\\
	Update positions of each satellite\\
	\For{$i\leftarrow 1$ \KwTo $N$}{
		Determine $k$-nearest satellites to $satellite_i$ from the positions\\
		Select data from nearest satellites\\
		Send nearest satellites predictions and data to $satellite_i$\\
		Retrieve data from $satellite_i$ and fault predictions of $k$-nearest satellites\\
	}
	Update position of $satellite_i$\\
	Update data of $satellite_i$

	\caption[Do not end short caption with full-stop]{Algorithm example}
	\label{alg}

\end{algorithm}

\section{Typical Faults}
For the simulation of the satellite and the induced faults to train and test various anomaly detection methodologies a database of typical faults is required. \textcite{tafazoli2009study} made a study of the percentage of failure per subsystem.

\subsection{Probability of Fault Occurence}
The occurrence of a fault depends on the reliability of that equipment. \textcite{Guo2014} studied the reliability of small satellites and calculated the parameters for the Weibull distribution based on real data. To model the probability of a fault to occur the probability density function is used \cite{Jones2017}.

This probability however is small and for the training of the system the data is too sparse for the computational abilities of any regular PC. Thus the probability of a failure during training is fixed to $1/1000000$ to produce the data required for the anomaly detection with a million test samples.

\subsection{Set of faults}
A set of typical faults for the ADCS is shown in Table~\ref{ADCS fault table}.

\newpage
\begin{sidewaystable}[]
	\label{ADCS fault table}
	\begin{tabular}{|l|c|l|l|l|l|}
		\hline
		\multicolumn{6}{|c|}{\textbf{Internal Faults}} \\ \hline
		\textbf{Fault classes} &
		\multicolumn{1}{l|}{\textbf{\begin{tabular}[c]{@{}l@{}}Failure rate \\ per hour\end{tabular}}} &
		\textbf{Fault causes} &
		\textbf{References} &
		\textbf{Possible effect} &
		\textbf{Possible permutations} \\ \hline
		\multirow{4}{*}{Reaction wheels} &
		\multicolumn{1}{l|}{\multirow{4}{*}{2.5E-7 \cite{Spilhaus1987}}} &
		\begin{tabular}[c]{@{}l@{}}Reaction wheel electronics \\ fail\end{tabular} &
		\cite{allen2012satellite} \cite{Jacklin2019} &
		\begin{tabular}[c]{@{}l@{}}Does not respond \\ to control inputs\end{tabular} &
		\begin{tabular}[c]{@{}l@{}}Momentum remains the same \\ or decreases slightly due to \\ friction\end{tabular} \\ \cline{3-6}
		&
		\multicolumn{1}{l|}{} &
		Overheated reaction wheel &
		\cite{Wintoft} &
		Decrease in speed &
		1\% of initial speed per second \\ \cline{3-6}
		&
		\multicolumn{1}{l|}{} &
		\begin{tabular}[c]{@{}l@{}}Catastrophic failure (cause \\ unknown)\end{tabular} &
		\cite{Choi2011} &
		Stops rotating &
		0 \\ \cline{3-6}
		&
		\multicolumn{1}{l|}{} &
		\begin{tabular}[c]{@{}l@{}}Increase in rotation speed \\ (Unknown cause)\end{tabular} &
		\begin{tabular}[c]{@{}l@{}}Gerhard Janse \\ van Vuuren\end{tabular} &
		\begin{tabular}[c]{@{}l@{}}Wheel speed \\ increases\end{tabular} &
		\begin{tabular}[c]{@{}l@{}}Between 90-100\% of maximum \\ wheel speed\end{tabular} \\ \hline
		Magnetorquers &
		\multicolumn{1}{l|}{ADCS fault table8.15E-9 \cite{Spilhaus1987}} &
		Polarities are inverted &
		\cite{Crowell2011} &
		Incorrect rotation &
		\\ \hline
		\multirow{2}{*}{Magnetometers} &
		\multicolumn{1}{l|}{\multirow{2}{*}{8.15E-9 \cite{Spilhaus1987}}} &
		Unknown &
		\begin{tabular}[c]{@{}l@{}}Gerhard Janse \\ van Vuuren\end{tabular} &
		Stops reacting &
		\begin{tabular}[c]{@{}l@{}}Provides no feedback or the \\ output remains constant\end{tabular} \\ \cline{3-6}
		&
		\multicolumn{1}{l|}{} &
		\begin{tabular}[c]{@{}l@{}}Magnetometers and magne-\\ torquers interfered with \\ each other\end{tabular} &
		\cite{Jacklin2019} &
		\begin{tabular}[c]{@{}l@{}}Noise on magneto-\\ meters and noise \\ on control of mag-\\ netorquers\end{tabular} &
		\begin{tabular}[c]{@{}l@{}}Between x3 and x5 times the \\ normal noise magnitude \\ Guassian distribution\end{tabular} \\ \hline
		Earth Sensor &
		- &
		Unknown &
		\cite{Robertson2019} &
		\begin{tabular}[c]{@{}l@{}}Noisy Earth Sensor \\ effected pointing \\ accuracy\end{tabular} &
		\begin{tabular}[c]{@{}l@{}}Between x5 and x10 times the \\ normal sensor noise based on \\ Guassian distribution\end{tabular} \\ \hline
		\multirow{2}{*}{Sun sensor} &
		\multirow{2}{*}{-} &
		\begin{tabular}[c]{@{}l@{}}Cross-wired during instal-\\ lation\end{tabular} &
		\cite{Crowell2011} &
		\begin{tabular}[c]{@{}l@{}}Erroneous \\ measurements\end{tabular} &
		Uniform random values \\ \cline{3-6}
		&
		&
		Unknown &
		\cite{Jacklin2019} &
		Sun sensor fails &
		output is 0 \\ \hline
		Star tracker &
		- &
		\begin{tabular}[c]{@{}l@{}}Shutter on star tracker is \\ closed\end{tabular} &
		\cite{Crowell2011} &
		Star tracker fails &
		output is 0 \\ \hline
		Overall control &
		- &
		\begin{tabular}[c]{@{}l@{}}Incorrect control law or \\ variation \\ thereof\end{tabular} &
		\begin{tabular}[c]{@{}l@{}}Gerhard Janse \\ van Vuuren\end{tabular} &
		\begin{tabular}[c]{@{}l@{}}Angular velocity \\ suddenly increases \\ or decreases or \\ oscillation results\end{tabular} &
		\begin{tabular}[c]{@{}l@{}}Increase to 75 - 100\% \\ Decrease to 0 - 25\%\\ Oscillates\end{tabular} \\ \hline
		\multirow{3}{*}{\begin{tabular}[c]{@{}l@{}}Common data \\ transmission errors\end{tabular}} &
		\multirow{3}{*}{-} &
		Sign flip &
		\cite{Crowell2011} &
		Processor-based &
		\begin{tabular}[c]{@{}l@{}}Processor outputs and/or \\ inputs experience a sign flip\end{tabular} \\ \cline{3-6}
		&
		&
		Bit flip &
		N/A &
		&
		\begin{tabular}[c]{@{}l@{}}Processor outputs and/or \\ inputs experience a bit flip flip\end{tabular} \\ \cline{3-6}
		&
		&
		Insertion of zeros &
		\cite{Jacklin2019} &
		&
		\begin{tabular}[c]{@{}l@{}}Processor outputs and/or inputs \\ experience an insertion of a zero\end{tabular} \\ \hline
		\begin{tabular}[c]{@{}l@{}}Possible sensors \\ errors\end{tabular} &
		- &
		Unknown &
		N/A &
		High sensor noise &
		\begin{tabular}[c]{@{}l@{}}Between x5 and x10 times the \\ normal sensor noise based on \\ Guassian distribution\end{tabular} \\ \hline
	\end{tabular}
\end{sidewaystable}

\newpage


\section{Scannerless parsing of streams}

In this section, we present a technique to write
descent\hyp{}recursive parsers based on \emph{streams}, a lazy data
structure available to \OCaml by means of \CamlpF, which is
distributed separately. We will not explain how to install \CamlpF and
we refer the reader to any textbook on compilers for the basics on
context\hyp{}free grammars and, in particular, LL($k$) grammars.

Our technique assumes that we already have an LL(1) grammar, and we
wish to implement a parser to recognise the language it generates. In
passing, it enables also a style called \emph{scannerless parsing},
meaning that both the lexer, also called scanner, and the parser can
be expressed with the same feature, namely streams.

We will use the following formalism to express context\hyp{}free
grammars. A grammar is a set of production rules, each one associating
a non\hyp{}terminal symbol to a possibly empty sequence of terminals
and non\hyp{}terminals. These rules can be conceived as rewrite rules
whereby the non\hyp{}terminal symbol, on the left\hyp{}hand side, is
rewritten into a series of symbols in the right\hyp{}hand side, and
that is why we separate both sides with a rightwards
arrow. Non\hyp{}terminals are names whose first letter is capitalised,
whereas terminals are named whose first letter is in lowercase. A word
is a sequence of non\hyp{}terminals. To avoid leaving blanks on the
page, we will note~\(\varepsilon\) the empty word. A Greek letter in
lowercase represents a possibly empty concatenation of terminals and
non\hyp{}terminals, in general, which means that the series may, in
particular, be a word. We will write \(A \stackrel{*}{\Rightarrow}
\alpha\) the relationship `$A$~derives~$\alpha$'. Consider for example
the following ambiguous, context\hyp{}free grammar for Boolean
expressions:
\begin{equation*}
B \rightarrow B \, \textsf{||} \, B \; \mid \; B \, \textsf{\&\&} \, B
\; \mid \; \textsf{!}B \; \mid \; \textsf{(}B\textsf{)}
\; \mid \; \textsf{true} \; \mid \; \textsf{false}
\end{equation*}
We will augment the formalism with rational operators, in order to
have a more compact notation: \(\alpha^*\), \(\alpha^+\),
\([\alpha]\), \(\{ A \, a \dots \}^*\) and \(\{ A \, a \dots
\}^+\). Each occurrence of these operators can always be replaced by a
non\hyp{}terminal whose defining rule is shown in
\fig~\vref{fig:bnf_rel_op}.
\begin{figure}
\centering
\(
\begin{array}{llc}
\toprule
  \multicolumn{1}{c}{\text{Notation}}
& \multicolumn{1}{c}{\text{Definition}}
& \multicolumn{1}{c}{\text{Constraint}}\\
\midrule
  X \rightarrow \alpha^*
& X \rightarrow \alpha X \mid \varepsilon
& \neg(\alpha \stackrel{*}{\Rightarrow} \varepsilon)\\
  X \rightarrow \alpha^+
& X \rightarrow \alpha \alpha^*
& \neg(\alpha \stackrel{*}{\Rightarrow} \varepsilon)\\
  X \rightarrow [\alpha]
& X \rightarrow \alpha \mid \varepsilon
& \neg(\alpha \stackrel{*}{\Rightarrow} \varepsilon)\\
  X \rightarrow \{A \, a \ldots\}^*
& X \rightarrow \varepsilon \mid A \, (a A)^*
& \neg(A \stackrel{*}{\Rightarrow} \varepsilon)\\
  X \rightarrow \{A \, a \ldots\}^+
& X \rightarrow A \, (a A)^*
& \neg(A \stackrel{*}{\Rightarrow} \varepsilon)\\
\bottomrule
\end{array}\)
\caption{Rational operators for context\hyp{}free grammars
\label{fig:bnf_rel_op}}
\end{figure}
These rules are LL(1) and it is suitable now that we define that
property. LL(1) grammars are context\hyp{}free grammars which can be
used to recognise languages in a top\hyp{}down way, with one token of
look\hyp{}ahead. By `top\hyp{}down', we actually mean rightwards in
the sense of the rules: Left to right scanning of the input, building
a Leftmost derivation (according to the production rules) with One
token of look\hyp{}ahead. In order to define them formally, we need
first to define a couple of functions. Let us note~\(\mathcal{N}\) the
set of the non\hyp{}terminals and~\(\Sigma\) the set of the terminal
symbols.

\paragraph{The function \emph{First}}

The function `First', noted~\(\mathcal{P}\), maps each
non\hyp{}terminal to the set of terminals that start any derivation
from it. We have, for all non\hyp{}terminal~\(A\),
\begin{equation*}
\first{A} := \{ x \in \Sigma \mid A \stackrel{*}{\Rightarrow} x
\alpha\}.
\end{equation*}

\paragraph{The function \emph{Follow}}

The function `Follow', noted~\(\mathcal{S}\), maps each
non\hyp{}terminal to the set of terminals that may follow each
occurrences of the non\hyp{}terminal. Formally, for all
non\hyp{}terminal~\(A\),
\begin{equation*}
\follow{A} := \{x \in \Sigma \mid \exists B \in \mathcal{N}.B
\stackrel{*}{\Rightarrow} \alpha A x \beta\}.
\end{equation*}

\paragraph{The LL(1) property}

We can now formally define LL(1) grammars as a set of production rules
\(A \rightarrow \alpha_1 \mid \alpha_2 \mid \ldots \mid \alpha_n\)
satisfying
\begin{gather}
\neg(A \stackrel{*}{\Rightarrow} A \alpha);\label{P1}\\
\bigcap_{i=1}^{n} \first{\alpha_i} = \varnothing;\label{P2}\\
\alpha_i \stackrel{*}{\Rightarrow} \varepsilon \quad \text{implies} \quad
\first{\alpha_j} \cap \follow{A} = \varnothing, \quad \text{for all}
\; j.\label{P3}
\end{gather}
The first constraint states that no rule can be left\hyp{}recursive,
neither directly, nor indirectly; the second affirms that no two words
produced by the same non\hyp{}terminal can start with the same
terminal; the third and last states that, if a word produced by a
non\hyp{}terminal~\(A\) derives the empty word (it might derive
non\hyp{}empty words as well), then the words derived from~\(A\)
should not start with a terminal following~\(A\) in the grammar.

These constraints can be extended to the rational operators we
introduced in \fig~\vref{fig:bnf_rel_op}, and the whole new definition
can be given an algorithmic style in \fig~\vref{fig:first_ext}.
\begin{figure}[!t]
\centering
\begin{framed}
\begin{align*}
\first{\varepsilon} &= \{\varepsilon\},\\
\first{x \gamma} &= \{x\},\\
\first{B \gamma} &= \first{B},\\
\first{[\beta] \gamma} &= \first{\beta} \cup \first{\gamma},\\
\first{\{B \; b \ldots\}^* \gamma} &= \first{B} \cup \first{\gamma},\\
\first{\{B \; b \ldots\}^+ \gamma} &= \first{B},\\
\first{\beta^*\gamma} &= \first{\beta} \cup \first{\gamma},\\
\first{\beta^+\gamma} &= \first{\beta},\\
\first{A} &= \cup_{i=1}^{n}\first{\alpha_i}, \quad \text{if} \; A
\rightarrow \alpha_1 \mid \alpha_2 \mid \ldots \mid \alpha_n.
\end{align*}
\end{framed}
\caption{Extended definition of function First\label{fig:first_ext}}
\end{figure}
Note that we extended the definition of~\(\mathcal{P}\)
to~\(\varepsilon\), even though it is not a non\hyp{}terminal,
and~\(\gamma\) may derive~\(\varepsilon\) (it is \emph{nullable}). As
for~\(\mathcal{S}\), the redefinition is seen in
\fig~\vref{fig:follow_ext}.
\begin{figure}
\centering
\(\begin{array}{@{}ll@{}}
\toprule
\multicolumn{1}{c}{\text{Rule}} & \multicolumn{1}{c}{\text{Constraint}}\\
\midrule
  X \rightarrow \ldots \mid \alpha A B \beta
& \first{B} \subseteq \follow{A};\\
  X \rightarrow \ldots \mid \alpha A \{B \; b \dots\}^+ \beta
& \first{B} \subseteq \follow{A};\\
  X \rightarrow \ldots \mid \alpha A [\beta] \gamma
& \first{\beta} \cup (\first{\gamma} \setminus
  \{\varepsilon\}) \cup \follow{X} \subseteq \follow{A},\\
& \text{if} \; \gamma \stackrel{*}{\Rightarrow} \varepsilon, \;
  \text{else} \; \first{\beta} \cup \first{\gamma} \subseteq \follow{A};\\
  X \rightarrow \ldots \mid \alpha A \beta^* \gamma
& \first{\beta} \cup (\first{\gamma} \setminus
  \{\varepsilon\}) \cup \follow{X} \subseteq \follow{A},\\
& \text{if} \; \gamma \stackrel{*}{\Rightarrow} \varepsilon, \;
  \text{else} \; \first{\beta} \cup \first{\gamma} \subseteq \follow{A};\\
  X \rightarrow \ldots \mid \alpha A
& \follow{X} \subseteq \follow{A};\\
  X \rightarrow \ldots \mid \alpha A x \beta
& x \in \follow{A};\\
  X \rightarrow \ldots \mid \alpha A \beta^+ \gamma
& \first{\beta} \subseteq \follow{A};\\
  X \rightarrow \ldots \mid \alpha A \{B \; b \ldots \}^* \beta
& \first{B} \cup (\first{\beta} \setminus \{\varepsilon\}) \cup
  \follow{X} \subseteq \follow{A},\\
& \text{if} \; \beta \stackrel{*}{\Rightarrow} \varepsilon,
  \text{else} \; \first{B} \cup \first{\beta} \subseteq \follow{A};\\
  X \rightarrow \ldots \mid \alpha \{A \; a \dots\}^* \beta
& \{a\} \cup (\first{\beta} \setminus \{\varepsilon\}) \cup \follow{X}
  \subseteq \follow{A},\\
& \text{if} \; \beta \stackrel{*}{\Rightarrow} \varepsilon,
  \text{else} \; \{a\} \cup \first{\beta} \subseteq \follow{A};\\
  X \rightarrow \ldots \mid \alpha \{A \; a \dots\}^+ \beta
& \{a\} \cup (\first{\beta} \setminus \{\varepsilon\}) \cup \follow{X}
  \subseteq \follow{A},\\
& \text{if} \; \beta \stackrel{*}{\Rightarrow} \varepsilon,
  \text{else} \; \{a\} \cup \first{\beta} \subseteq \follow{A}.\\
\bottomrule
\end{array}\)
\caption{Extended definition of function Follow\label{fig:follow_ext}}
\end{figure}
Any place where it is possible, we replace~\(A\) by \([A]\), \(A^*\)
or~\(A^+\) --~the first replacement is mandatory for the
implementation technique we propose. Notice as well that the
algorithmic definition of~\(\mathcal{S}\) consists in a collection of
inclusion constraints \(\ldots \subseteq \follow{A}\), whose smallest
solution is then~\(\follow{A}\). (Contrast this with the definition
of~\(\mathcal{P}\).)

For reasons that will become apparent later, we will impose that our
grammars do not contain explicitly the empty word, therefore, in the
case of the rational expressions, the LL(1) property~\vref{P3} takes
the shape
\begin{equation*}
\begin{array}{ll}
\toprule
\multicolumn{1}{c}{\text{Rule}} & \multicolumn{1}{c}{\text{Constraint}}\\
\midrule
  X \rightarrow \alpha^*
& \first{\alpha} \cup \follow{X} = \varnothing\\
  X \rightarrow \alpha^+
& \first{\alpha} \cup \follow{X} = \varnothing\\
  X \rightarrow [\alpha]
& \first{\alpha} \cup \follow{X} = \varnothing\\
  X \rightarrow \{A \; a \ldots\}^*
& (\first{A} \cup \{a\}) \cap \follow{X} = \varnothing\\
  X \rightarrow \{A \; a \ldots\}^+
& \{a\} \cap \follow{X} = \varnothing\\
\bottomrule
\end{array}
\end{equation*}

\paragraph{Stream constraints}

The method we propose in this section relies on LL(1) grammars with
additional constraints due to the semantics of streams; it will allow
us to specify error messages only in the rules that may fail to parse
the input, so they can be tailored, without knowledge of the lexical
context. Higher\hyp{}order parser will be used to implement rational
operators, so the functions matching stream patterns will be
isomorphic to the constrained grammar, greatly improving the
maintenance of the program.

Let us begin by the sort of constraints arising from the use of
streams for parsing. Simply put, we cannot implement as is the
production rules which are left\hyp{}recursive:
\begin{equation*}
A \rightarrow X \, B \mid C, \quad \text{where} \; X
\stackrel{*}{\Rightarrow} A.
\end{equation*}
Indeed, since each non\hyp{}terminal will be transformed into a
function matching a stream, the parser for~\(A\) will try to match a
lexeme by calling itself, recursively, which yields
non\hyp{}termination. Another issue arises in the case
\begin{equation*}
A \rightarrow X \, B \mid C, \quad \text{where} \; X
\stackrel{*}{\Rightarrow} \varepsilon.
\end{equation*}
If~\(B\) fails, the exception \texttt{Stream.Failure} raised by the
parser associated with~\(B\) becomes an exception
\texttt{Stream.Error} in the parser associated with~\(A\)
because~\(B\) is not at the head of a rule, which interrupts the
syntax analysis, event though~\(C\) might have succeeded. It is clear
that there was never any danger in pursuing the analysis since no
lexeme had been consumed from the stream by~\(X\). This limitation is
justified by a simpler semantics for streams and can be overcome by
rewriting the LL(1) grammar. Therefore, after making sure that the
grammar is not left\hyp{}recursive, we must transform it according to
the following table:
\begin{equation*}
\begin{array}{@{}lcl@{}}
\toprule
  X \rightarrow [\alpha] \beta
& \textit{becomes}
& X \rightarrow \alpha \beta \mid \beta\\
  X \rightarrow \alpha^* \beta
& \textit{becomes}
& X \rightarrow \alpha^+ \beta \mid \beta\\
  X \rightarrow \{A \; a \ldots\}^* \beta
& \textit{becomes}
& X \rightarrow \{A \; a \ldots\}^+ \beta \mid \beta\\
\bottomrule
\end{array}
\end{equation*}
Note that we assume that the language does not contain the empty word.

These constraints are relevant to our analysis method and it is
perhaps suitable now to explain why parsers on streams are good tools
in general, despite the constraints we just have mentioned. First, it
is wrong to believe that these allow us to only analyse LL(1)
grammars. Let us consider the famous case of the `dangling else'. Let
the grammar
\begin{equation*}
\begin{array}{@{}ccl@{}}
S & \rightarrow & \textsf{if} \; \textit{BoolExpr} \;
                  \textsf{then} \; S \, S'
                  \mid \textit{OtherInstr}\\
S' & \rightarrow & \textsf{else} \; S \mid \varepsilon
\end{array}
\end{equation*}
This grammar defines a conditional construct and it yields a
shift/reduce conflict in parsers generated by tools like \textsc{Yacc}
when the lexical right\hyp{}context is \textsf{else}. That conflict
boils down to decide whether a \textsf{else} clause should be
associated to the last \textsf{if} or not, as in the sentence
\begin{equation*}
\textsf{if} \; b_1 \; \textsf{then} \; \textsf{if} \; b_2 \;
\textsf{then} \; i_1 \; \textsf{else} \; i_2.
\end{equation*}
The usual way to resolve this matter is to systematically associate a
\textsf{else} to the last \textsf{if} encountered, from left to
right. In \OCaml with parsers on streams, the same effect is simply
achieved by writing the pattern matching corresponding to
\(\textsf{else} \; S\) first, so it is tried before~\(\varepsilon\).

In fact, \OCaml with parsers on streams can recognise contextual
languages, thanks to higher\hyp{}order functions. Let us consider the
language \(\{wcw \,\mid\, w \in (a+b)^*\}\), where \(a\), \(b\)
and~\(c\) are terminal symbols. This is a contextual language because,
in order to recognise the second occurrence of~\(w\), we need to
somehow `remember' (here goes the context) the first occurrence. In
\OCaml, we recognise the prefix~\(w\), then we dynamically build a
parser for it, then, after reading~\(c\), we apply this previously
constructed parser to the stream to find the suffix~\(w\). First, let
us write the parser for~\(w\):
\begin{alltt}
\textbf{let} \textbf{rec} wd = \textbf{parser}
  [< ''a'; w=wd >] \(\rightarrow\) (\textbf{parser} [< ''a' >] \(\rightarrow\) "a")::w
| [< ''b'; w=wd >] \(\rightarrow\) (\textbf{parser} [< ''b' >] \(\rightarrow\) "b")::w
|             [<>] \(\rightarrow\) []
\end{alltt}
The type of \texttt{wd} is \topout{char stream $\rightarrow$ (char
  Stream.t $\rightarrow$ string) list}. The higher\hyp{}order parser,
which takes as an argument the list of parsers generated by
\texttt{wd}, and applies them to the current stream is defined as
follows:
\begin{alltt}
\textbf{let} \textbf{rec} wu = \textbf{function}
  p::pl \(\rightarrow\) (\textbf{parser} [< x=p; w=wu pl >] \(\rightarrow\) x^w)
|    [] \(\rightarrow\) (\textbf{parser} [<>] \(\rightarrow\) "")
\end{alltt}
whose type is \topout{($\alpha$ Stream.t $\rightarrow$ string) list
  $\rightarrow$ $\alpha$ Stream.t $\rightarrow$ string}. Finally, the
parser for our toy language is
\begin{alltt}
\textbf{let} wcw = \textbf{parser} [< pl=wd; ''c'; w=wu pl >] \(\rightarrow\) w
\end{alltt}
whose type is \topout{char Stream.t $\rightarrow$ string}. Then, we
can use it as follows:

\bigskip

\noindent\topin{wcw (Stream.of\_string "abaacabaa")}

\noindent\topout{-~:~string = "abaa"}

\paragraph{Error handling}

Since we rewrite any given LL(1) grammar so that the empty word does
not occur, we can use the stream pattern \texttt{[<>]} or \texttt{[< s
    >]} to detect failures, but, first, let us define a type with two
constant constructors which are passed to all parsers, denoting, in
case of failure, whether the analysis should stop with an error
message (abort), or resume (fail):
\begin{alltt}
\textbf{type} mode = Abort | Fail
\end{alltt}
To allow for the partial application of the parsers, we put the
argument of type \texttt{mode} in first position, and the general
shape of the parsers is
\begin{alltt}
\textbf{let} my\_parser mode = \textbf{parser}
  [< ... >] \(\rightarrow\) ...
| ...
| [< ... >] \(\rightarrow\) ...
|   [< s >] \(\rightarrow\) \textbf{match} mode \textbf{with}
                 Fail  \(\rightarrow\) raise Stream.Failure
               | Abort \(\rightarrow\) syntax\_error \emph{message} s
\end{alltt}
where \texttt{syntax\_error} is a function which prints the error
message about the first lexeme at the head of the stream~\texttt{s}
and stops the execution, for example by raising the predefined
exception \texttt{(Stream.Error s)}. We call \texttt{(my\_parser
  Fail)} in head of a stream pattern, and \texttt{(my\_parser Abort)}
in tail of a stream pattern. We will later see how to optimise this
general pattern, for instance, in case we know that
\texttt{my\_parser} can never stop the parsing because it never occurs
in tail of a stream pattern. We understand now why we take care of
rewriting the grammar so it does not produce the empty word: the
stream pattern \texttt{[< s >]} can then be used for error handling.

\paragraph{Parser combinators}

The rational operators we have seen above are implemented by means of
higher\hyp{}order parsers: their first parameter is the parser to
apply, the second is the parsing mode (of type \texttt{mode} above)
and the last is the stream to parse. This order makes it possible to
partially evaluate a rational operator on its first argument and use
the resulting first\hyp{}order parser as an argument to another
higher\hyp{}order parser, giving rise to \emph{parser combinators}.
\begin{itemize}

  \item \(X \rightarrow \alpha^*\)\\ The definition of~\(\alpha^*\),
    called the \emph{Kleene star}, is \(X \rightarrow \alpha X \mid
    \varepsilon\). Its \OCaml implementation will evaluate in the list
    of the parsed lexemes. As seen earlier, for the LL(1) property to
    hold, the lexeme following \(\alpha^*\) in the grammar must not be
    recognisable by~\(\alpha\). Moreover, we impose that~\(\alpha\)
    does not derive the empty word~\(\varepsilon\). The general shape
    of the implementation is then:
\begin{alltt}
\textbf{let rec} star p m = \textbf{parser} [< x=p Fail; y=star p m >] \!\(\rightarrow\)\! x::y
                        |                       [<>] \!\(\rightarrow\) \![]
\end{alltt}
    Note that the mode~\texttt{m} is not used because the call
    to~\texttt{p} must always be allowed to fail, with possibility to
    resume parsing.

  \item \(X \rightarrow \alpha^+\)\\
     The definition of this operator is \(X \rightarrow \alpha
     \alpha^*\). Its \OCaml implementation relies on \texttt{star} and
     evaluates in the list of the parsed lexemes:
\begin{alltt}
\textbf{let} plus p m = \textbf{parser} [< x=p m; y=star p m >] \(\rightarrow\) x::y
\end{alltt}
     Recall that the mode~\texttt{m} in \texttt{star} is not used
     because \texttt{p} is called there with \texttt{Fail}. This
     entails that, in general, the call \texttt{(plus p Abort)} implies
     the calls \texttt{(p Abort)} and \texttt{(p Fail)}.

  \item \(X \rightarrow [\alpha]\)\\
    The definition of this operator is \(X \rightarrow \alpha \mid
    \varepsilon\). We have the following straightforward
    implementation:
\begin{alltt}
\textbf{let} opt p m = \textbf{parser} [< x=p Fail >] \(\rightarrow\) [x] | [<>] \(\rightarrow\) []
\end{alltt}
    Just as with the operator~\(\alpha^*\), the parser~\texttt{p} must
    always be allowed to fail, hence the mode~\texttt{m} is
    useless. An alternative implementation could evaluate in a value
    of the predefined type \texttt{option}:
\begin{alltt}
\textbf{let} opt p m = \textbf{parser} [< x=p Fail >] \!\(\rightarrow\)\! Some x | [<>] \!\(\rightarrow\)\! None
\end{alltt}

  \item \(X \rightarrow \{A \; a \ldots\}^+\)\\
    The definition of this operator is \(X \rightarrow A \, (a
    A)^*\). If~\texttt{p} is the parser for~\(A\) and~\texttt{a}
    denotes~\(a\), the implementation is as follows:
\begin{alltt}
\textbf{let} list\_plus p a m =
  \textbf{let} aux m = \textbf{parser} [< 'b \textbf{when} a=b; c=p Abort >] \(\rightarrow\) a::c
\textbf{in parser} [< x=p m; y=star aux m >] \(\rightarrow\) x::y
\end{alltt}
    Note that the call \texttt{(list\_plus p a Fail)} entails the
    calls \texttt{(p Fail)} and \texttt{(p Abort)}.

  \item \(X \rightarrow \{A \; a \ldots\}^*\)\\
    The definition of this
    operator is \(X \rightarrow \varepsilon \mid A \, (a A)^*\), hence
\begin{alltt}
\textbf{let} list\_star p a m =
  \textbf{let} aux m = \textbf{parser} [< 'b \textbf{when} a=b; c=p Abort >] \(\rightarrow\) a::c
\textbf{in parser} [< x=p Fail; y=star aux m >] \(\rightarrow\) x::y | [<>] \(\rightarrow\) []
\end{alltt}
    Note the two calls \texttt{(p Fail)} and \texttt{(p Abort)} and
    how the mode~\texttt{m} is useless.

\end{itemize}

\paragraph{Optimisation}

All parsers have the same type, in particular they all take a mode as
an argument. This mode can sometimes be useless when the parser in
question is always at the head of a stream pattern or always in the
tail: if the former, this is equivalent to always have the
\texttt{Fail} mode, whereas, in the latter, it is always
\texttt{Abort}. We can partially evaluate these parsers accordingly
and we can do the same with the parser combinators implementing the
rational operators. The pros are twofold: we create fewer closures at
run\hyp{}time and we eliminate useless code; the cons are also
twofold: we need another analysis of the grammar and we lose the
possibility to combine arbitrarily our parsers because their types may
vary. The parser combinators are modified as follows.
\begin{itemize}

  \item \(X \rightarrow \alpha^*\)\\ We have seen above that the
    parsing mode is irrelevant, so we can remove it and assume that,
    if no lexeme can be recognised, the parser~\texttt{p} may fail by
    raising \texttt{Stream.Failure}, but not abort by raising
    \texttt{Stream.Error} or an exception defined by the
    programmer. We have the new definition
\begin{alltt}
\textbf{let rec} star p = \textbf{parser} [< x=p; y=star p >] \(\rightarrow\) x::y
                      |                [<>] \(\rightarrow\) []
\end{alltt}

  \item \(X \rightarrow \alpha^+\)\\ We must retain the parsing mode
    for~\texttt{plus} because it needs to be passed along to the
    parser~\texttt{p}. According to the optimisation of~\(\alpha^*\)
    above, we evaluate partially now~\texttt{p} with the
    mode~\texttt{Fail}:
\begin{alltt}
\textbf{let} plus p m = \textbf{parser} [< x=p m; y=star (p Fail) >] \(\rightarrow\) x::y
\end{alltt}

  \item \(X \rightarrow [\alpha]\)\\
    We remove the parsing mode, as with~\texttt{star} above:
\begin{alltt}
\textbf{let} opt p = \textbf{parser} [< x=p >] \(\rightarrow\) [x] | [<>] \(\rightarrow\) []
\end{alltt}

  \item \(X \rightarrow \{A \; a \ldots\}^+\)\\ We keep the parsing
    mode, as with~\texttt{plus} above:
\begin{alltt}
\textbf{let} list\_plus p a m =
  \textbf{let} aux = \textbf{parser} [< 'b \textbf{when} a=b; c=p Abort >] \(\rightarrow\) a::c
\textbf{in parser} [< x=p m; y=star aux >] \(\rightarrow\) x::y
\end{alltt}

  \item \(X \rightarrow \{A \; a \ldots\}^*\)\\ We remove the parsing
    mode, as with~\texttt{star} above:
\begin{alltt}
\textbf{let} list\_star p a =
  \textbf{let} aux = \textbf{parser} [< 'b \textbf{when} a=b; c=p Abort >] \(\rightarrow\) a::c
\textbf{in parser} [< x=p Fail; y=star aux >] \(\rightarrow\) x::y | [<>] \(\rightarrow\) []
\end{alltt}

\end{itemize}
These optimisations give rise to three kinds of parsers:
\begin{enumerate*}

  \item \emph{passing parsers}, which may fail by raising
    \texttt{Stream.Failure};

  \item \emph{blocking parsers}, which may stop parsing, for example
    by raising \texttt{Stream.Error};

  \item \emph{mixed parsers}, which may or may not stop parsing,
    depending on the call context.

\end{enumerate*}
The grammar must be analysed to categorise all the non\hyp{}terminals
with respect to these three kinds. First, we ignore the rational
operators. If a non\hyp{}terminal always appear at the start of a
production, the corresponding parser is passing; if it always occur
inside a production, the parser is blocking; otherwise, it is
mixed. Second, we assume that the non\hyp{}terminals appearing only
inside rational operators are passing. Third, we consider the rational
operators:
\begin{itemize}

  \item \(\alpha^*\)\\ We distinguish the first word of~\(\alpha\):
    it is a non\hyp{}terminal whose parser was deemed blocking, then
    it becomes mixed. For each following non\hyp{}terminal, if their
    parser was passing, it becomes mixed.

  \item \(\alpha^+\)\\ Let us leave aside the first word
    of~\(\alpha\). For each subsequent non\hyp{}terminal, if the
    corresponding parser was passing, it becomes mixed. We now
    distinguish depending on the position of~\(\alpha^+\) in the
    grammar. If it occurs at the start of a production and if the
    parser of the first word of~\(\alpha\) was blocking, then it
    becomes mixed. If~\(\alpha^+\) does not occur at the start of a
    production and if the parser of the first word of~\(\alpha\)
    was passing, then it becomes mixed.

  \item \([\alpha]\)\\ Same as~\(\alpha^*\).

  \item \(\{A \; a \ldots\}^*\)\\ The parser of~\(A\) becomes (or
    remains) mixed.

  \item \(\{A \; a \ldots\}^+\)\\ Same as~\(\alpha^+\).

\end{itemize}
All mixed parsers require a parameter for the parsing mode. Some
passing or blocking parsers may also require such a parameter only for
typing reasons, for example if it is passed itself as an argument to
the parser combinators \texttt{plus} or \texttt{list\_plus}. The
blocking parsers have the shape
\begin{alltt}
\textbf{let} my\_parser \emph{mode\(\sp{\text{opt}}\)} = \textbf{parser}
  [< ... >] \(\rightarrow\) ...
| ...
| [< ... >] \(\rightarrow\) ...
|   [< s >] \(\rightarrow\) syntax\_error \emph{message} s
\end{alltt}
Note that \texttt{\emph{mode\(^{\text{opt}}\)}} means that the
parameter \texttt{mode} may be required for typing reasons. The
passing parsers have the shape
\begin{alltt}
\textbf{let} my\_parser \emph{mode\(\sp{\text{opt}}\)} = \textbf{parser}
  [< ... >] \(\rightarrow\) ...
| ...
| [< ... >] \(\rightarrow\) ...
\end{alltt}
Note that there is no pattern \texttt{[<>]} or \texttt{[< s >]}
because the semantics of stream matching would naturally see an
exception \texttt{Stream.Failure} raised if the first lexeme in the
stream matches no pattern. The mixed parsers have the shape we gave
earlier, before considering any optimisations.

\paragraph{An example}

Let us consider an example based on a real formal language called
\emph{Abstract Syntax Notation One} (ASN.1)~\citep{Larmouth_1999,
  Dubuisson_2001}, notoriously difficult to
parse~\citep{Rinderknecht_1995}. To keep things simple, we will only
parse the header of a module, not the assignments it contains.

The name of the scanned file containing the module is set in a global
reference:
\begin{alltt}
\textbf{let} file = ref ""
\end{alltt}
and a location in that file is a pair of integer coordinates (line,
column):
\begin{alltt}
\textbf{type} loc = int \(\times\) int
\end{alltt}
The keywords here are a small subset of all ASN.1 keywords:
\begin{alltt}
\textbf{type} kwd = ALL | AUTOMATIC | BEGIN | DEFINITIONS
| END | EXPLICIT | EXPORTS | EXTENSIBILITY | FROM
| IMPLICIT | IMPLIED | IMPORTS | TAGS
\end{alltt}
Symbols are also in small number in our presentation:
\begin{alltt}
\textbf{type} sym =
  Assign | Lbrace | Rbrace | Comma | Lparen | Rparen | SemiColon
\end{alltt}
To understand what these data constructors stand for, here is the
function that provides their concrete syntax:
\begin{alltt}
\textbf{let} string\_of\_sym = \textbf{function}
  Assign \(\rightarrow\) "::=" | Lbrace \(\rightarrow\) "{" | Rbrace \(\rightarrow\) "}" | Comma \(\rightarrow\) ","
| Lparen \(\rightarrow\) "(" | Rparen \(\rightarrow\) ")" | SemiColon \(\rightarrow\) ";"
\end{alltt}
The concrete syntax of keywords is obvious, but we need a function
nonetheless:
\begin{alltt}
\textbf{let} string\_of\_kwd = \textbf{function}
  ALL \(\rightarrow\) "ALL" | AUTOMATIC \(\rightarrow\) "AUTOMATIC" | BEGIN \(\rightarrow\) "BEGIN"
| DEFINITIONS \(\rightarrow\) "DEFINITIONS" | END \(\rightarrow\) "END" | TAGS \(\rightarrow\) "TAGS"
| EXPLICIT \(\rightarrow\) "EXPLICIT" | EXPORTS \(\rightarrow\) "EXPORTS" | FROM \(\rightarrow\) "FROM"
| EXTENSIBILITY \(\rightarrow\) "EXTENSIBILITY" | IMPLICIT \(\rightarrow\) "IMPLICIT"
| IMPLIED \(\rightarrow\) "IMPLIED" | IMPORTS \(\rightarrow\) "IMPORTS"
\end{alltt}
The type for the tokens is
\begin{alltt}
\textbf{type} token =
  Low    \textbf{of} (loc \(\times\) string)  (* Lowercase-starting identifier *)
| Up     \textbf{of} (loc \(\times\) string)  (* Uppercase-starting identifier *)
| ModRef \textbf{of} (loc \(\times\) string)  (* Module reference *)
| Nat    \textbf{of} (loc \(\times\) string)  (* Natural number *)
| Str    \textbf{of} (loc \(\times\) string)  (* String *)
| Kwd    \textbf{of} (loc \(\times\) kwd)     (* Keyword *)
| Sym    \textbf{of} (loc \(\times\) sym)     (* Symbol *)
| EOF    \textbf{of} loc              (* End of file (virtual token) *)
\end{alltt}
The only parser combinator we need are the following:
\begin{alltt}
\textbf{let} opt p = \textbf{parser} [< x=p >] \(\rightarrow\) Some x | [<>] \(\rightarrow\) None

\textbf{let rec} star p = \textbf{parser} [< x=p; y=star p >] \(\rightarrow\) x::y
                      |                [<>] \(\rightarrow\) []
\end{alltt}
Note that the constraints and optimisations above apply. We also need
\texttt{plus}, which requires parsing modes:
\begin{alltt}
\textbf{type} mode = Fail | Abort

\textbf{let} plus p m = \textbf{parser} [< x=p m; y=star (p Fail) >] \(\rightarrow\) x::y
\end{alltt}
At the head of a stream pattern, we shall use \texttt{(plus p Fail)},
otherwise \texttt{(plus p Abort)}. We need an exception to signal
syntax errors, and a way to print them:
\begin{alltt}
\textbf{exception} Error \textbf{of} (loc \(\times\) string)

\textbf{let} get\_loc = \textbf{function}
  Low a | Up a | ModRef a | Str a | Nat a -> fst a
| Sym (loc,\_) | Kwd (loc,\_) | EOF loc \(\rightarrow\) loc

\textbf{let} stop token msg = raise (Error (get\_loc token, msg))

\textbf{let} check msg = \textbf{function}
   Fail \(\rightarrow\) raise Stream.Failure
| Abort \(\rightarrow\) \textbf{parser} [< 't >] \(\rightarrow\) stop t msg
\end{alltt}
The function \texttt{check} performs the action corresponding to the
parsing mode. We continue with the definitions of auxiliary parsers
dedicated to tokens to be recognised in pattern tails, so we must stop
parsing if they are not found:
\begin{alltt}
\textbf{let} modref = \textbf{parser}
  [< 'ModRef \_ >] \(\rightarrow\) ()
| [< 't >] \(\rightarrow\) stop t "Module reference expected."

\textbf{let} kwd k = \textbf{parser}
  [< 'Kwd (\_,k') \textbf{when} k=k' >] \(\rightarrow\) ()
| [< 't >] \(\rightarrow\) stop t ("Keyword "^ string\_of\_kwd k ^ " expected.")

\textbf{let} sym s = \textbf{parser}
  [< 'Sym (\_,s') \textbf{when} s=s' >] \(\rightarrow\) ()
| [< 't >] \(\rightarrow\) stop t ("Symbol " ^ string\_of\_sym s ^ " expected.")

\textbf{let} nat = \textbf{parser} [< 'Nat \_ >] \(\rightarrow\) ()
               | [< 't >] \(\rightarrow\) stop t "Natural number expected."
\end{alltt}
Finally, we can define the parser for ASN.1 modules:
\begin{alltt}
\textbf{let rec} moduleDefinition = \textbf{parser}
  [< \_=moduleIdentifier;
     ()=kwd DEFINITIONS;
     \_=opt tagDefault;
     \_=opt extensionDefault;
     ()=sym Assign;
     ()=kwd BEGIN;
     s=moduleSuffix >] \(\rightarrow\) s

\textbf{and} moduleIdentifier = \textbf{parser}
  [< 'ModRef \_; \_=opt definitiveIdentification >] \(\rightarrow\) ()
| [< 't >] \(\rightarrow\) stop t "Module identifier expected"

\textbf{and} definitiveIdentification = \textbf{parser}
  [< 'Sym (\_,Lbrace); \_=plus definitiveObjIdComponent Abort;
     ()=sym Rbrace; \_=opt iriValue >] \(\rightarrow\) ()

\textbf{and} iriValue = \textbf{parser} [< 'Str \_ >] \(\rightarrow\) ()

\textbf{and} definitiveObjIdComponent mode = \textbf{parser}
  [< 'Nat \_ >] \(\rightarrow\) ()
| [< 'Low \_; \_=opt num >] \(\rightarrow\) ()
| [< s >] \(\rightarrow\) check "OID component expected." mode s

\textbf{and} num = \textbf{parser}
  [< 'Sym (\_,Lparen); ()=nat; ()=sym Rparen >] \(\rightarrow\) ()

\textbf{and} tagDefault = \textbf{parser}
  [< 'Kwd (\_,EXPLICIT);  ()=kwd TAGS >] \(\rightarrow\) ()
| [< 'Kwd (\_,IMPLICIT);  ()=kwd TAGS >] \(\rightarrow\) ()
| [< 'Kwd (\_,AUTOMATIC); ()=kwd TAGS >] \(\rightarrow\) ()

\textbf{and} extensionDefault = \textbf{parser}
  [< 'Kwd (\_,EXTENSIBILITY); ()=kwd IMPLIED >] \(\rightarrow\) ()

\textbf{and} moduleSuffix = \textbf{parser}
  [< 'Kwd (\_,EXPORTS); \_=opt exports; ()=sym SemiColon;
     \_=opt imports; a=assignmentList >] \(\rightarrow\) a
|     [< \_=imports; a=assignmentList >] \(\rightarrow\) a
|                [< a=assignmentList >] \(\rightarrow\) a

\textbf{and} exports = \textbf{parser} [< \_=symbolList Fail >] \(\rightarrow\) ()
                   |      [< 'Kwd (\_,ALL) >] \(\rightarrow\) ()

\textbf{and} imports = \textbf{parser}
  [< 'Kwd (\_,IMPORTS); \_=opt imp1; ()=sym SemiColon >] \(\rightarrow\) ()

\textbf{and} imp1 = \textbf{parser} [< \_=symbolList Fail; \_=from >] \(\rightarrow\) ()

\textbf{and} from = \textbf{parser} [< ()=kwd FROM; \_=modref; \_=opt imp2 >] \(\rightarrow\) ()

\textbf{and} imp2 = \textbf{parser}
  [< \_=obj; \_=opt imp1 >] \(\rightarrow\) ()
| [< 'Up \_; \_=opt braces; \_=opt moreSymbols; \_=from >] \(\rightarrow\) ()
| [< 'Low \_; \_=opt imp3 >] \(\rightarrow\) ()

\textbf{and} obj = \textbf{parser}
  [< 'Sym (\_,Lbrace); \_=plus objIdComponents Abort;
     ()=sym Rbrace >] \(\rightarrow\) ()

\textbf{and} braces = \textbf{parser} [< 'Sym (\_,Lbrace); ()=sym Rbrace >] \(\rightarrow\) ()

\textbf{and} moreSymbols = \textbf{parser}
  [< 'Sym (\_,Comma); \_=symbolList Abort >] \(\rightarrow\) ()

\textbf{and} imp3 = \textbf{parser}
  [< \_=braces; \_=opt moreSymbols; \_=from >] \(\rightarrow\) ()
|               [< \_=moreSymbols; \_=from >] \(\rightarrow\) ()
| [< 'Kwd (\_,FROM); \_=modref; \_=opt imp2 >] \(\rightarrow\) ()
|                              [< \_=imp1 >] \(\rightarrow\) ()

\textbf{and} objIdComponents mode = \textbf{parser}
                            [< \_=obj >] \(\rightarrow\) ()
| [< \_=definitiveObjIdComponent Fail >] \(\rightarrow\) ()
| [< s >] \(\rightarrow\) check "OID component expected." mode s

\textbf{and} symbolList mode = \textbf{parser}
  [< \_=symbol; \_=opt moreSymbols >] \(\rightarrow\) ()
| [< s >] \(\rightarrow\) check "Reference expected." mode s

\textbf{and} symbol = \textbf{parser} [< \_=reference; \_=opt braces >] \(\rightarrow\) ()

\textbf{and} reference = \textbf{parser} [< 'Up \_ >] \(\rightarrow\) () | [< 'Low \_ >] \(\rightarrow\) ()

\textbf{and} assignmentList = \textbf{parser}
  [< 'Kwd(\_,END); s >] \(\rightarrow\) after\_END s
|  [< 'EOF \_ \textbf{as} eof >] \(\rightarrow\) stop eof "Keyword END expected."
|          [< 't; s >] \(\rightarrow\) [< 't; assignmentList s >]

\textbf{and} after\_END = \textbf{parser}
  [< 'EOF \_ \textbf{as} eof >] \(\rightarrow\) [< 'eof >]
|            [< 't >] \(\rightarrow\) stop t "End of file expected."
\end{alltt}
Note that \texttt{objIdComponents} and \texttt{symbolList} retained
their parsing mode parameter because the former is used as an argument
to the parser combinator \texttt{plus}, and the latter is used both at
the head (passing parser) and in the tail (blocking parser) of stream
patterns. As explained earlier, the parsers have been derived from the
standard grammar, which has been transformed into LL(1) form first,
then the restrictions for streams were applied, for instance, not
having a parser combinator for the empty word at the head of a
pattern.

The next step would be building up symbol tables for imported and
exported definitions in the semantic actions, that is, on the
right\hyp{}hand side of the arrows in parsers, and then write another
parser to recognise the tokens currently passed along by
\texttt{assignmentList} (third pattern), that is, the actual contents
of the ASN.1 modules.


%----------------------------------------------------------------------------------------
%	PACKAGES AND OTHER DOCUMENT CONFIGURATIONS
%----------------------------------------------------------------------------------------

\documentclass[11pt]{article}
\usepackage{hyperref}
\input{structure.tex} % Include the file specifying the document structure and custom commands

%----------------------------------------------------------------------------------------
%	ASSIGNMENT INFORMATION
%----------------------------------------------------------------------------------------

% Required
\newcommand{\assignmentQuestionName}{} % The word to be used as a prefix to question numbers; example alternatives: Problem, Exercise
\newcommand{\assignmentClass}{Zero to ASIC} % Course/class
\newcommand{\assignmentTitle}{Analogue Design} % Assignment title or name
\newcommand{\assignmentAuthorName}{Thomas Parry} % Student name

% Optional (comment lines to remove)
%\newcommand{\assignmentClassInstructor}{Jones 10:30am} % Intructor name/time/description
%\newcommand{\assignmentDueDate}{Monday,\ January\ 24,\ 2019} % Due date

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

\begin{document}

%----------------------------------------------------------------------------------------
%	TITLE PAGE
%----------------------------------------------------------------------------------------

\maketitle % Print the title page

\thispagestyle{empty} % Suppress headers and footers on the title page

\newpage

\section{Fundamentals of the MOSFET}

The MOSFET (Metal-Oxide Semiconductor Field Effect Transistor) is the fundamental building block of modern IC design.

\begin{figure}[h]
\centering{
\includegraphics[scale=1.00]{diagrams/nmos.pdf}
}
\end{figure}

The MOSFET is a \textit{transconductance} device which translates a voltage between it's gate and source terminals to a current between the drain and source - assuming the device is biased correctly.

\subsection{The Saturation Region}

Most devices in a circuit will be operating in saturation where the following description of the current is given for a square-law device:

\begin{equation}
I_D = \frac{1}{2} \mu_n C_{ox} \frac{W}{L} {\left( V_{GS} - V_{TH} \right)}^2
\end{equation}

This can be simplified to:

\begin{equation}
I_D = K_n \frac{W}{L} {V_{OD}}^2
\end{equation}

\begin{equation}
given \; that: \;\;\; V_{DS} > V_{OD}
\end{equation}

The condition is very important, if you want your FET to work as expected you must ensure you have sufficient $V_{DS}$ headroom.  The value $V_{OD}$ is also more commonly called $V_{D_{SAT}}$ and is defined as the minimum drain voltage required to keep the device in saturation. Note that in sub-micron processes (like Skywater 130 nm) the statement that $V_{D_{SAT}} = V_{OD}$ is not fully accurate due to second order effects from device scaling.

\subsection{The Triode Region}

When there is insufficient drain-source voltage (by design or error) the device will work in the triode region where it approximates a voltage controlled resistor:

\begin{equation}
I_D = \mu_n C_{ox} \frac{W}{L} \left( (V_{GS} - V_{TH}) V_{DS} - \frac{V_{DS}^2}{2} \right)
\end{equation}

\subsection{The Cutoff Region}

When there is insufficient voltage across the gate-source terminals the device is 'off':

\begin{equation}
I_D = 0 \;\;\;\; given \; that: \;\; V_{GS} << V_{TH}
\end{equation}

\answer{Always know the region you expect your FET to operate and check that you have it correctly biased or all other assumptions are broken.}

\section{Basics of Transconductance Efficiency}

The transconductance is the amount of current in the drain per gate-source voltage.

\begin{equation}
i_d = g_m \cdot v_{gs}
\end{equation}

This simple diagram shows the most important concept of the FET: a voltage at the input is converted to an output current. This output current will later be converted by an impedance to another voltage.

\begin{figure}[h]
\centering{
\includegraphics[scale=1.00]{diagrams/nmos_smallsignal.pdf}
}
\end{figure}

The transconductance can be expressed as a function of the bias current, giving the $g_m / I_d $ value. This value is related to how inverted the channel of the FET is - a measure of how 'turned on' the FET is. The measure of channel inversion is how many minority charge carriers have been attracted to the conductive channel between the source and drain.

\subsection{Transconductance Efficiency and Overdrive Voltage}

There is a relation to the overdrive voltage mentioned above, $V_{OD}$. The higher the $V_{OD}$, the more the FET is 'turned on', and therefore the more the FET channel is inverted, which leads to a \textit{lower} $g_m / I_d$. Conversly, a lower $V_{OD}$ turns on the FET less, and therefore the FET channel is less inverted, which leads to a \textit{higher} $g_m / I_d$.

\subsection{Realistic Values of Transconductance Efficiency}

There is a range of transconductance efficiencies that can be obtained in standard CMOS processes. It is reasonable to assume a maximum transconductance efficiency of 20 in design equations, although in some specific cases values of 25 can be reached. Typically a device will not be used below a transconductance efficiency of 4.

\subsection{Why Use Transconductance Efficiency?}

While transconductance efficiency, $g_m/I_d$, feels very abstract at first, its use lies in two factors.

\begin{enumerate}
  \item It gives a strong indication of the channel inversion.
  \item It allows quick calculation of circuit currents and other conditions.
\end{enumerate}

For example, if I have determined I will use a FET in weak inversion and I need $1 \; mS$ of transconductance, then I very simply know the required current of $50 \; \mu A$.

\answer{But how do we know what transconductance we should use for a given FET? We'll discuss this in the next section where I will talk about the relation of matching and noise to transconductance.}

\textbf{Further Reading (clickable)}

\hspace{1cm} \href{http://web02.gonzaga.edu/faculty/talarico/EE406/documents/gmid.pdf}{gm/ID - Based Design}

\hspace{1cm} \href{http://web.eecs.utk.edu/~bblalock/ece532/ece532_pres_ekv_bsim.pdf}{An Introduction to the EKV Model and a comparison of EKV to BSIM}

\end{document}


% !TEX root = ../main.tex
\chapter{Sentiment Analysis}
\label{ch:sentimentAnalysis}

Semantic orientation describes the strength and polarity of words and phrases used in text.
Extraction of subjectivity and polarity of the text from the semantic orientation of its words and phrases is called sentiment analysis.
The huge volume of text-based interactions on social media makes it difficult
to read and organize them, and manually analyze their sentiment.
Focusing on single instances of interaction also makes it difficult to extract the overall sentiment,
necessitating a solution that performs and summarizes the analysis automatically, fast, and gives human-comprehensible results~\cite{Sarlan2014}.

\section{Methods}
\label{sec:methods_sa}

The informal language used in statuses on Twitter, and the limited length, poses an additional difficulty
to analyzing their sentiment.
Previous research showed supervised approaches to be superior to lexicon-based approaches~\cite{Sarlan2014}.
Since labeled data is available from the Sanders dataset~\cref{sec:theSandersDataset},
this thesis will focus on supervised approaches.
The dataset was preprocessed using the preprocessing and tokenization functions introduced in~\cref{sec:preprocessingAndTokenization} (without stopwords removal),
and the following methods evaluated.

\subsection{Naive Bayes}
\label{subsec:naivebayes}

The naive Bayes algorithm works by first expressing the probability of a label given the features,
in this case the sentiment given the bag of words that is the status, with Bayes rule:

\begin{equation}
    P(label|features) = \frac{P(label)*P(features|label)}{P(features)}
\end{equation}

The algorithm then naively assumes that all features are independent, giving:

\begin{equation}
    P(label|features) = \frac{P(label)*P(feature 1|label)*...*P(feature n|label)}{P(features)}
\end{equation}

The denominator $P(features)$ is not explicitly calculated.
Instead, all numerators are calculated and the denominator is chosen such that all $P(label|features)$ sum to 1~\cite{nltkDocs}.
\\
The features of each status are extracted as a vector of every distinct word in the dataset,
together with $True$ if the word can be found in the status, or $False$ if not.
The Python-code achieving this encoding can be seen in~\cref{code:extract_features}.

\begin{figure}
    \caption{Encoding a status as a vector}
    \label{code:extract_features}
    % @formatter:off
    \begin{minted}{python}
        # Contains every word in the dictionary and whether the status contains it.
        features = {}
        # Using the previously created dictionary
        for word in dictionary:
            features['contains(\%s)' \% word] = (word in document_words)
    \end{minted}
    % @formatter:on
\end{figure}

NLTK's implementation of this algorithm is used~\cite{nltkDocs}.
This implementation assumes the distribution of each feature $P(feature_i|label)$ to be multinomial,
making it a multinomial naive Bayes, which has shown the best performance in previous work~\cite{Go2009}.
The data from the Sanders dataset created in~\cref{sec:theSandersDataset} was shuffled and split into 10\% test data and 90\% training data,
which still gives accurate test results without removing too much from the little available training data.
This classifier achieved an accuracy of 75\%.
In addition to accuracy, the performance measures precision, recall and F-measure, were computed for this classifier.
Let $p_t$ be the number of statuses from a class that were actually labeled as being in that class,
and $p_f$ be the number of statuses from other classes that were falsely labeled as being in that class.
Similarly, let $n_t$ be the number of statuses that were correctly labeled as $not$ being in that class,
and $n_f$ be the number of statuses were labeled as not being in that class although they are.
The definitions for precision, recall and F-measure are then defined as follows:

\begin{equation}
    Precision = \frac{p_t}{p_t + p_f}
\end{equation}
\begin{equation}
    Recall = \frac{p_t}{p_t + n_f}
\end{equation}
\begin{equation}
    F-Measure = \frac{2 \times Precision \times Recall}{Precision + Recall}
\end{equation}

Intuitively, precision and recall can be understood as the percentage of positives that are real positives,
and the percentage of real positives that were labeled as positives, respectively.
The F-measure is their harmonic mean.
These measures, for each sentiment-class, can be seen in~\cref{tab:mnb_results}.
Class distribution and absolute counts for each class can be seen in~\cref{tab:naive_bayes_results}.

\begin{table}
    \centering
    \fontsize{8.5}{9}\selectfont
    \caption{Sentiment classification results using multinomial naive Bayes}
    \label{fig:sentiment_results}
    \begin{subtable}{.48\textwidth}
        \caption{Absolute counts and percentages}
        \label{tab:naive_bayes_results}
        \centering
        \begin{tabular}{lllll} %
            \toprule
            & \multicolumn{2}{c}{Test Data} & \multicolumn{2}{c}{Classification Result}\\
            \cmidrule{2-3}
            \cmidrule{4-5}
            Sentiment
            & Count
            & Percentage
            & Count
            & Percentage
            \\\midrule
            neutral & 206 & 47\%  & 266 & 61\%
            \\\midrule
            irrelevant & 146 & 33\%  & 124 & 28\%
            \\\midrule
            negative & 42 & 9\%   & 30 & 6\%
            \\\midrule
            positive & 41 & 9\%   & 15 & 3\%
            \\\bottomrule
        \end{tabular}
    \end{subtable}%
    \hfill
    \begin{subtable}{.48\textwidth}
        \caption{F-measure, precision and recall}
        \label{tab:mnb_results}
        \centering
        \begin{tabular}{llll} %
            \toprule
            & \multicolumn{3}{c}{Measure}\\
            \cmidrule{2-4}
            Sentiment
            & F-measure
            & Precision
            & Recall
            \\\midrule
            neutral & 0.769 & 0.747 & 0.792
            \\\midrule
            irrelevant & 0.896 & 0.902 & 0.890
            \\\midrule
            negative & 0.504 & 0.464 & 0.553
            \\\midrule
            positive & 0.318 & 0.440 & 0.250
            \\\bottomrule
        \end{tabular}
    \end{subtable}
\end{table}

When the dictionary created from the sample stream was used to extract the features as shown in~\cref{code:extract_features},
the accuracy dropped to 62\%, indicating that the Sanders dataset, and the dictionary created from it, is highly topical.
However, the dictionary created from the Sanders dataset will be used in this thesis.

\subsection{NLTK Sentiment Analyzer}
\label{subsec:nltksentimentanalyzer}

The NLTK sentiment analyzer package provides a framework for building classification-pipelines with easily interchangeable components
(like choosing which methods to use for training, or which feature extractors to use).
It also builds its own dictionary, which means the dictionary created in~\cref{sec:preprocessingAndTokenization} doesn't need to be used.

First, the previous approach was reconstructed in this framework, and successfully validated to give the same accuracy.
Then, it was expanded by marking all negated words as negated using the \texttt{mark\_negation} utility function of the NLTK.
They were then added to the dictionary in their negated form.
\\
This yielded no performance increase: again, an accuracy of 75\% was achieved.
\\
Inspection revealed that only 22 words were added to the dictionary in negated form.
Since the tokenization function described in~\cref{sec:preprocessingAndTokenization} filters tokens of length shorter then 3,
thereby also filtering, for example, the word "no", another test was conducted with a modified tokenization function that doesn't filter out short words.
\\
This also yielded no performance increase.
\\
Only two more distinct words were found in their negated form and added to the dictionary,
increasing the dictionary size of the modified tokenization function by 24, from 4132 to 4156.
\\
While recognizing contextual polarity has shown improvements in accuracy in a more general case~\cite{Hoffmann2005},
these results indicate that marking negated words does not have the same positive effect on the special case presented by Twitter statuses.

\subsection{VADER}
\label{subsec:vader}

VADER (\textbf{V}alence \textbf{A}ware \textbf{D}ictionary for s\textbf{E}ntiment \textbf{R}easoning)
is a gold standard of lexical features together with five general rules embodying grammatical and syntactical conventions
that were devised in a human-centered approach.
It is specifically designed for micro-blog-like environments such as Twitter~\cite{Hutto2014}.

NLTK's implementation of VADER returns scores between 0 and 1 for positivity, negativity and neutrality,
as well as a compound score between -1 and 1, where the direction represents polarity and the magnitude represents subjectivity.
A test was conducted involving only the statuses labeled "positive", "negative" or "neutral",
to avoid the ambiguity of the "irrelevant"-label.
The optimal threshold for which a status will be classified as neutral was found
at an absolute value for the compound score of 0.8, giving an accuracy of 68\%.

\begin{equation}
    class(x) =
    \begin{cases}
        neutral & \text{for } |compound\_sentiment(x)| < 0.8 \\
        positive & \text{for } compound\_sentiment(x) \geq 0.8 \\
        negative & \text{for } compound\_sentiment(x) \leq -0.8 \\
    \end{cases}
\end{equation}

Still, this accuracy remained below that of the naive Bayes approach at 74\% when using the same filtered subset of statuses.
Detailed results can be seem in~\cref{tab:vader_results}.

\begin{table}[t]
    \begin{minipage}[t]{.4\textwidth }
        \caption{F-measure, precision and recall of VADER}
        \label{tab:vader_results}
        \vspace{1.5mm} % Lining up the tables to make it bit prettier
        \resizebox{\textwidth}{!}{%
        \begin{tabular}{llll} %
            \toprule
            Sentiment
            & F-measure
            & Precision
            & Recall
            \\\midrule
            neutral & 0.806 & 0.689 & 0.971
            \\\midrule
            negative & 0.098 & 0.742 & 0.052
            \\\midrule
            positive & 0.171 & 0.448 & 0.105
            \\\bottomrule
        \end{tabular}}
    \end{minipage}%
    \hfill
    \begin{minipage}[t]{.55\textwidth}
        \captionof{figure}{NLTK's VADER demonstration}
        \label{code:vader_demo}
        % @formatter:off
        \begin{minted}{python}
            import nltk.sentiment.util
            nltk.sentiment.util.demo_vader_tweets()

            Loaded 1 tweets
            (...)
            Loaded 5000 tweets

            Accuracy: 0.861
            F-measure [neg]: 0.8524416135881105
            F-measure [pos]: 0.8686200378071833
            Precision [neg]: 0.9083710407239819
            Precision [pos]: 0.8234767025089605
            Recall [neg]: 0.803
            Recall [pos]: 0.919
        \end{minted}
        % @formatter:on
    \end{minipage}
\end{table}

The NLTK also provides a function that demonstrates the use of VADER on the dataset provided by the NLTK.
Interestingly, as seen in~\cref{code:vader_demo}, the performance on that dataset is better than on the Sanders-dataset.
However, as described in~\cref{sec:theSandersDataset}, it is not known how the sample dataset in the NLTK was collected and labeled,
which is why these results are disregarded.

\subsection{TextBlob}
\label{subsec:textblob}

TextBlob is a simple text-processing library written in Python.
It provides similar functionality than the NLTK described in~\cref{subsec:nltk},
but is simpler and less extensive.
Among other, it offers sentiment analysis~\cite{textblobDocs}.
TextBlob returns a polarity score between -1 and 1 as well as a subjectivity score between 0 and 1.
As in the previous subsection, the Sanders dataset is used for evaluation, disregarding statuses labeled as irrelevant.
A scatter plot of the polarity and subjectivity score on the axes and label represented as the color can be seen in~\cref{fig:textblob}.
The plot clearly shows the algorithms incapability of distinguishing subjective from neutral statuses,
which is why a quantitative accuracy test was conducted using only tweets labeled as negative or positive.

\begin{equation}
    class(x) =
    \begin{cases}
        positive & \text{for } compound\_sentiment(x) \geq 0 \\
        negative & \text{for } compound\_sentiment(x) < 0\\
    \end{cases}
\end{equation}

The TextBlob's sentiment analysis algorithm achieved an accuracy of 64\% on this filtered dataset,
whereas the current front-runner, the multinomial naive bayes approach,
achieved an accuracy of 72\% on the same filtered subset of statuses.

\subsection{Google Cloud Platform}
\label{subsec:googlecloud}

The natural language API by the Google Cloud Platform offers sentiment analysis as a service.
For any document, provided in a supported language, the API returns a magnitude starting from 0, with an undocumented upper bound
(although the highest observed was 4.1) and a score between -1 and 1.
Magnitude and subjectivity as well as score and polarity are similarly described in the their respective documentations~\cite{gcloudDocs}\cite{textblobDocs},
which is why they are assumed have the same meaning.

The API is billed on a per-request basis, so all statuses from the Sanders dataset were classified once and persisted in a CSV-file.
Again, statuses labeled as irrelevant were filtered out to avoid ambiguity.
A scatter plot of the score and magnitude on the axes and label represented as the color can be seen in~\cref{fig:gcloud}.

\begin{figure}
    \centering
    \caption{Sentiment classification results on a scatter plot}
    \label{fig:sentiment_results_scatter}
    \begin{subfigure}{.5\textwidth}
        \centering
        \caption{TextBlob}
        \label{fig:textblob}
        \includegraphics[width=\textwidth]{../figures/textblob.pdf}
    \end{subfigure}%
    \begin{subfigure}{.5\textwidth}
        \centering
        \caption{Google Cloud natural language API}
        \label{fig:gcloud}
        \includegraphics[width=\textwidth]{../figures/gcloud.pdf}
    \end{subfigure}
\end{figure}

As for the VADER-classifier in~\cref{subsec:vader}, these values are mapped to the labels from the dataset by
choosing an optimal threshold for which a status will be classified as neutral.
Statuses labeled as irrelevant were, again, discarded.
The optimal magnitude-threshold was found at a magnitude of 0.9, giving an accuracy of 49\%,
which is just marginally better than guessing neutral, with 47\% of statuses being labeled neutral.

\begin{equation}
    class(x) =
    \begin{cases}
        neutral & \text{for } |compound\_sentiment(x)| < 0.9 \\
        positive & \text{for } compound\_sentiment(x) \geq 0.9 \\
        negative & \text{for } compound\_sentiment(x) \leq -0.9 \\
    \end{cases}
\end{equation}

\section{Comparison and Conclusion}
\label{sec:comparison}

When comparing the results of the different methods presented, it is surprising to see the simplest,
a multinomial naive Bayes, turned out to be the most accurate.
The summarized accuracy results can be seen in~\cref{tab:sa_results}.

This indicates that short text statuses on micro-blogs like Twitter present a special challenge to sentiment analysis
that cannot be solved with current approaches.
Additions to existing algorithms, like marking negated words, that showed improved results with regular texts,
showed no improvement to the multinomial naive Bayes classifier when working with Twitter statuses.
Furthermore, even methods specifically designed for this special challenge, like VADER,
were outperformed by a multinomial naive Bayes classifier.

However, the results of these tests need to be handled carefully.
As explained in~\cref{sec:theSandersDataset},
and also indicated by the performance discrepancies between different dictionaries in~\cref{subsec:naivebayes},
the dataset used is highly topical.

The sample stream dataset introduced in~\cref{sec:streamingSampleDataset} was then classified using the multinomial naive bayes classifier from~\cref{subsec:naivebayes}.
When comparing the sentiment distribution of this dataset, seen in~\cref{fig:sample_sentiment},
to that of the Sanders dataset, seen in~\cref{fig:sanders_sentiment},
one can see that it seems to be more opinionated,
containing fewer statuses labeled as "irrelevant" in favor of positive and negative tweets.

This indicates that the dataset is not only highly topical, but also not perfectly representative in terms of sentiment.
Still, the multinomial naive Bayes classifier was saved and will be used in the upcoming chapters.

\begin{table}[t]
    \begin{minipage}[t]{.5\textwidth }
        \captionof{table}{Sentiment classification accuracy comparison}
        \label{tab:sa_results}
        \centering
        \resizebox{\textwidth}{!}{%
        \begin{tabular}{lll} %
            \toprule
            Method
            & Statuses used
            & Accuracy
            \\\midrule
            & \textit{all} & 75\%
            \\\cmidrule{2-3}
            Naive Bayes & positive, negative, neutral & 74\%
            \\\cmidrule{2-3}
            & positive, negative & 72\%
            \\\midrule
            NLTK Sentiment Analyzer & \textit{all} & 75\%
            \\\midrule
            VADER & positive, negative, neutral & 68\%
            \\\midrule
            TextBlob & positive, negative & 64\%
            \\\midrule
            Google Cloud Platform & positive, negative, neutral & 49\%
            \\\bottomrule
        \end{tabular}}
    \end{minipage}%
    \hfill
    \begin{minipage}[t]{.46\textwidth}
        \centering
        \captionof{figure}{Sample stream dataset label distribution}
        \label{fig:sample_sentiment}
        \includegraphics[width=\textwidth]{../figures/sample_sentiment.pdf}
    \end{minipage}
\end{table}


\section{Muth--Lucas--Pischke and \cite{reis:inattentive}} \label{sec:Comparisons}

Now that our calibrations and results have been presented, we are in position to make some quantitative comparisons of our model to two principal alternatives to habit formation (or our model) for explaining excess smoothness in consumption growth, by Pischke and by Reis.

\subsection{Muth--Lucas--Pischke}
The longest-standing rival to habit formation as an explanation of consumption sluggishness is what we will call the Muth--Lucas--Pischke (henceforth, MLP) framework.  The idea is not that agents are inattentive, but instead that they have imperfect information on which they perform an optimal signal extraction problem.

\cite{muthOptimal}'s agents could observe only the level of their income, but not the split between its permanent and transitory components.  He derived the optimal (mean-squared-error-minimizing) method for estimating the level of permanent income from the observed signal about the level of actual income.  \cite{lucas:imperfectInfo} applied the same mathematical toolkit to solve a model in which firms are assumed to be unable to distinguish idiosyncratic from aggregate shocks.  \cite{pischkeMicroMacro} combines the ideas of Muth and Lucas and applies the result to micro consumption data: His consumers have no ability at all to perceive whether income shocks that hit them are aggregate or idiosyncratic, transitory or permanent.  They see only their income, and perform signal extraction on it.

Pischke calibrates his model with micro data in which he calculates that transitory shocks vastly outweigh permanent shocks.\footnote{Pischke's estimates constructed from the {\it Survey of Income and Program Participation} are rather different from the magnitudes of transitory and permanent shocks estimated in the extensive literature---mostly subsequent to Pischke's paper---cited in our calibration section above.}  So, when a shock arrives, consumers always interpret it as being almost entirely transitory and change their consumption by little.  However, macroeconometricians have long known that {\it aggregate} income shocks are close to permanent.  When an aggregate permanent shock comes along, Pischkian consumers spend very little of it, confounding the aggregate permanent shock's effect on their income with the mainly transitory idiosyncratic shocks that account for most of the total variation in their income.  This misperception causes sluggishness in {\it aggregate} consumption dynamics in response to aggregate shocks.

In its assumption that consumers fail to perceive aggregate shocks immediately and fully, Pischke's model resembles ours.  However, few papers in the subsequent literature have followed Pischke in making the assumption that households have no idea, when an idiosyncratic income shock occurs, whether it is transitory or permanent.  Especially in the last decade or so, the literature instead has almost always assumed that consumers can perfectly perceive the transitory and permanent components of their income; \hyperlink{Why-Consumers-See-Individual-Shocks}{see our defense of this assumption above}.

Granting our choice to assume that consumers correctly perceive the events that are idiosyncratic to them (job changes, lottery winnings, etc), there is still a potential role for application of the MLP framework:  Instead of assuming sticky expectations, we could instead have assumed that consumers perform a signal extraction exercise on \textit{only} the aggregate component of their income, because they cannot perceive the transitory/permanent split for the (tiny) part of their income change that reflects aggregate macroeconomic developments.

In principle, such confusion could generate excess smoothness; for a detailed description of the mechanism, \hyperlink{MuthLucasPischke}{see} online Appendix~\ref{appendix:Muth}.  But, defining the signal-to-noise ratio $\varphi=\sigma^2_{\Psi}/\sigma^2_{\Theta}$, Muth's derivations imply that the optimal updating coefficient is:
 \input{/Volumes/Data/Papers/cAndCwithStickyE/cAndCwithStickyE-AEJM/Equations/muthOptimal.tex}
\newcommand{\PischkePi}{0.83}
\newcommand{\PischkePiCancel}{0.17}
 \newcommand{\fromFile}{false}
 \newcommand{\FileOrNot}{\ifthenelse{\boolean{\fromFile}}}
Plugging our calibrations of $\sigma^2_{\Psi}$ and $\sigma^2_{\Theta}$ from section \ref{sec:calibration} into \eqref{eq:muthOptimal}, the model yields a predicted value of \FileOrNot{$1-\Pi \approx \PischkePiCancel$}{$(1-\Pi) \approx \PischkePiCancel $}---very far below the approximately $0.6$ estimate from \cite{hrsHabit} and even farther below our estimate of roughly $0.7$--$0.8$ for U.S.\ data.  This reflects the well-known fact that aggregate income is hard to distinguish from a random walk; if it were perceived to be a perfect random walk with no transitory component at all, the serial correlation in its growth would be zero.  So, in practice, allowing signal extraction with respect to the aggregate data is not a path to explaining excess smoothness.

\subsection{\cite{reis:inattentive}}

Leaving aside our earlier criticisms of its fidelity to microeconomic evidence, the model of \cite{reis:inattentive} has a further disadvantage relative to any of the other three stories (habits, MLP, or our model) with respect to aggregate dynamics. In Reis's model consumers update their information on a regular schedule---under a plausible calibration of the model, once a year. One implication of the model is that the change in consumption at the next reset is unpredictable; this implies that aggregate consumption growth would be unpredictable at any horizon beyond, say, a year.\footnote{In contrast, our model exhibits significant predictability beyond one year. The value of $\chi$ in the `horse-race' regression for the SOE economy is 0.66 when the right hand side is lagged by one quarter (see Table~\ref{tPESOEsim}). Adding an extra one and two years' lag to the right hand side sees $\chi$ decline approximately as an AR(1), to 0.20 and 0.06 respectively.}  But, macroeconomists felt compelled to incorporate sluggishness into macroeconomic models in large part to explain the fact that consumption growth is forecastable over extended periods---empirical impulse response functions indicate that a macroeconomically substantial component of the adjustment to shocks takes place well beyond the one year horizon.  A calibration of the Reis model in which consumers update once a year therefore fails to solve a large part of the original problem (of medium-term predictability).


\section{Implementation}
Our implementation was done in Matlab code. The structure of the Warm Chorus algorithm is quite modular; figure~\ref{fig:struct}, which details the structure of the algortihm, shows that there are at least six distinct processing blocks.
\begin{figure}[ht]
\centering
\includegraphics[width= 9cm]{Structure.png}
\caption{Block diagram of the warm chorus algorithm. \cite{dudas}}
\label{fig:struct}
\end{figure}

The flow of the algorithm begins with a multi-tap delay line, which divides the input signal into eight differently delayed paths. The first path is not delayed, as it corresponds to the lead player in the orchestra section. There is also an uneffected path on the left side of the structure, which is only used in the frequency domain processing for some minor corrections.

These eight paths then process the signal with harmonising structures, filters and other common digital signal processing tools. The most important processing block is the harmoniser, which will be explained separately, followed by brief explanations of the other processing blocks.


\title{(An introduction to )MultiSymplectic Geometry and Classical Field systems}
\author{
	International Ph.D. Program in "Science" \\
	"Differential geometry and applications to modern physics" \\
	%
	\begin{minipage}[c]{0.5\textwidth}
		\vspace{2em}
		\centering
		Department of Mathematics and Physics\\
		Universit\'a Cattolica del Sacro Cuore\\
		via Musei 41, 25121 Brescia, \underline{Italy}
	\end{minipage}
	\hfill
	\begin{minipage}[c]{0.5\textwidth}
		\vspace{2em}
		\centering
		Department of Mathematics\\
		KU Leuven\\
		Celestijnenlaan 200B, 3001 Leuven, \underline{Belgium}
	\end{minipage}
}

\documentclass[a4paper,12pt,fleqn]{article}  %Per La Stampa
\usepackage[a4paper, margin=3cm]{geometry}

\usepackage{amsmath}
\usepackage{amsfonts}
%\usepackage[multisym, geomec, basic, diffgeo]{./Math-Symbols-List/toninus-math-symbols}
\usepackage[subpreambles=true]{standalone}
\usepackage{commath}

\usepackage[italian,english]{babel}
\usepackage[utf8]{inputenc}

\usepackage{graphicx}
\usepackage{hyperref}
\usepackage{tabularx}

\usepackage{Latex-Theorem/theoremtemplate}

\providecommand{\oast}{\ensuremath{\:\!%
 {\raisebox{0.1ex}{$\scriptscriptstyle \bigcirc$} \hspace{-0.5em} \ast}}}
\providecommand{\ostar}{\ensuremath{\:\!%
 {\raisebox{0.05ex}{$\scriptscriptstyle \bigcirc$} \hspace{-0.5em} \star}}}
\providecommand{\smoast}{\ensuremath{%
 {\raisebox{-0.25ex}{$\textstyle \circ$} \hspace{-0.42em}
 {\scriptscriptstyle \ast}}}}
\providecommand{\smostar}{\ensuremath{%
 {\raisebox{-0.25ex}{$\textstyle \circ$} \hspace{-0.42em}
  \raisebox{0.05ex}{$\scriptscriptstyle \star$}}}}

\begin{document}
\maketitle

$$
\ast
\star
\oast
\ostar
\smoast
\smostar
J^\smostar
J^\smoast
%J^\ostar
$$

\begin{abstract}
$n$-plectic structures (also called \emph{multisymplectic}) are a rather straightforward generalization of symplectic ones where closed non-degenerate $n+1$-forms take the place of  $2$-forms.
\\
Just as one can associate a symplectic manifold to an ordinary classical mechanical system (e.g. a single point-like particle constrained to some manifold), it is possible to associate a multisymplectic manifold to any classical field systems (e.g. a continuous medium like a filament or a membrane).
\\
The aim of this talk is to give an account on the multisymplectic framework for (I-order) classical fields theories trying to comparing it with another object that plays a significant role in the mathematical description of classical fields called \emph{Covariant phase space}.
\\
Being the latter a sort of "$\infty$-dimensional manifold" (namely a mapping space), we will draw from this picture the idea that multisymplectic geometry could be seen as a tool that allows us to treat such formal object in a finite-dimensional setting.

\end{abstract}

\section{Preliminiaries}

%What's geometric Mechanics
\subsection{A glance at the geometric approach to classical mechanics}
The context in which we are going to work is \emph{Geometric Mechanics}.
\\
Roughly speaking, geometric mechanics is a branch of (applied) mathematics that employs differential geometry to the description of physical systems. Unlike analytical mechanics, the focus is on encoding the mechanical properties of a physical system regardless of the reference frame (coordinate system).
\\
Let us briefly recall what are the most important ingredients of an ordinary classical\footnote{Here "ordinary" stands for finite degrees of freedom and "classical" stands for non-quantum and non-relativistic.} system.

\begin{center}
  %\includestandalone{Pictures/Figure_ordinary_landscape}
\end{center}

\begin{itemize}
	\item The starting point is the \emph{Configuration space}, that is a smooth manifolds $Q$ containing all possible spatial displacements of a system.\\
	E.g. for the simple planar pendulum $Q= S^1$, for a double pendulum $Q =\mathbb{T}^2$
	\item A point in $Q$ is a statical configuration, that is opposed to the elements of the \emph{space of kinematics configuration}
	$$ \mathcal{C} = C^\infty(\mathbb{R},Q)$$
	which is the collection of all trajectories admitted by the constraints.
	\item The tangent bundle $TQ$ takes the name of \emph{space of generalized velocities}
	\item The cotangent bundle $T^\ast Q$ is called \emph{phase space}. Such space carries a natural symplectic form $\omega$ (\emph{Poincare\'} 2-form) defined in term of the tautological 1-form $$\omega= \text{d} \theta$$
\end{itemize}
%
All of this encode the geometry of the kinematics. Fixing a point in the previous two spaces is tantamount to give a complete set of initial data determining unambiguously the evolution of the system.
\\
Such point carries more information that the simple spatial displacement and are interpreted as the \emph{physical state} of the system.
%
\begin{itemize}
\item Dynamics is encoded by two smooth functions:
	\\
	$L:TQ\rightarrow \mathbb{R}$ in the \emph{Lagrangian} framework
	\\
	$H:T^\ast Q \rightarrow \mathbb{R}$ in the \emph{Hamiltonian} framework
	\\
	Generally the two framework are not equivalent.
\item From the Lagrangian $L$ or the Hamiltonian $H$ one can obtain the so-called \emph{Action functional} $S:\mathcal{C}\rightarrow \mathbb{R}$ which yields the equation of motion through the d'Alambert principle.
\end{itemize}
%
The goal of this talk is to describe the analogue of this framework in the case of classical field systems.

%What's a Classical Field
\subsection{Modelling a classical field theory}
A classical field theory is a physical theory that predicts how "physical fields" evolves according to their interaction with the matter and themselves.
\\
The term "classical" is commonly reserved to theories treating electro-magnetism and gravitation. Here it is meant as non-quantum mechanics of physical fields or continuous systems.

Roughly, a physical field can be thought as the assignment of a physical quantity at each point of space and time.

Classicamente: DOF come numero di misure necessarie per fissare univocamente una configurazione.

\begin{tabularx}{\textwidth}{c X c c}
	System & Prototype & DoF & Configuration space (locally)\\
	Ordinary classical mech system & point-particle on a  $q$-dim manifold &
	$q$ finite (discrete) & $\mathbb{R}^n = Hom_{set}(\{1,\ldots n\}, \mathbb{R})$ \\
	1-dim. field & collection of points on a line each one parametrized by a point on the manifold $Q$ & $q$ for each point on the continuous line (infinite and continuous) &
	...
%	\\
%	n dimensional field & $(\mathbb{R}^m)^{\mathbb{R}^n} = C^\infty (\mathbb{R}^n, \mathbb{R}^m)$ & ...
\end{tabularx}

A questo punto si dovrebbe Essere chiaro il punto di partenza:
\\

\subsection{Multisymplectic geometry}

\section{Multymplectic approach to classical field theories}
Tabella -Dictionary

\subsection{From the configuration Space to the configuration Bundle}
Recall that a physical field has to be thought as the assignment of a quantity to every point of a base manifold (a "spacetime" most of the time).
\\
It is reasonable that the object needed to encode the kinematics of our field system ougth to be a smooth bundle, that would be the proper language in order to formulate the notion of smooth physical quantity.
\\
\begin{definition}[Configuration bundle]
 	\begin{displaymath}
 		E = \left( \pi_{M,E} : E\rightarrow M \right)
 	\end{displaymath}
Smooth fiber bundle $E\sim(x^\mu,q^A)$ with typical fiber $Q\sim(q^A)$ over a smooth, oriented and connected base manifold $M\sim(x^\mu)$.
\end{definition}
This definition may seem a bit too bare, but has to be kept in mind that we are describing a structure suitable for many different models.
\\
The collection $\Gamma(E)$ of all smooth sections has to be understood as the set of kinematics configurations of the system and the typical fiber $Q$ can be tought as the configuration space of possible values of the field in a fixed point.

\subsection{From the space of generalized velocities to the I Jet bundle}
The collection of all field configurations together with their partial derivatives up to $r$-th order can be represented by the space of the sections of the $r$-th jet bundle.
\\
In what follows we shall only need the first order Jet bundle. This restriction reflect the fact that almost all known examples of field theories are governed by second order PDE and derived from Lagrangian depending only on first order partial derivative.
Also, it is also reasonable to develop the theory starting from this point directly extending the ordinary geometric mechanics theory.

\begin{definition}[I Jet bundle]
Affine bundle $J^1 E \rightarrow E$, with fibers over a point $y\in E$ given by
\begin{displaymath}
	\left( J^1 E\right)_y = \left\lbrace \gamma \in \text{L}(T_{\pi(y)}M, T_y E) \; \vert \: T\pi \circ \gamma = \text{id}_{T_{\pi(x)} M} \right\rbrace
\end{displaymath}
where $T$ is the tangent functor, $L$ is the space of linear maps and $\pi$ is the bundle projection of $E$ over $M$.
\\
This fiber is an affine subspace of $\text{L}(T_{\pi(y)}M, T_y E)$ modelled on the following (difference) vector space:
\begin{displaymath}
	\vec{\left( J^1 E\right)_y} = \left\lbrace z \in \text{L}(T_{\pi(y)}M, T_y E) \; \vert \: T\pi \circ z = 0 \right\rbrace
	\cong \text{L}(T_{\pi(y)}M, T_y E)
	\cong T_x^\ast M \otimes V_y E
\end{displaymath}
\end{definition}

\subsection{Dualization of Jet bundles}

\subsection{Lagrangian Dynamics}

\subsection{Hamiltonian Dynamics}

\subsection{Legendre transformation}

\subsection{examples}

\section{Spunti}

\subsection{Link to the Covariant Approach}

\subsection{Open problems?}
\begin{itemize}
	\item We have a momap on ms mfd and a momap on mapping space (as diffeological space) does the two match?
	\item We have a canonical m-s structure on J.. and a canonical poisson structure con cov phase space (Peierls bracket) does the two match? \\
		Yes! this is actually the result of \cite{forgeromero}. At pages 396-397 they show how to transgress the multisymplectic form to a symplectic form on Sol.\\
		In theorem 4 pag 403 they show that this form coincide with the one by Peierls constructed a' la Dewitt.

	\item there are other notion of moment map on the multisymplectic setting. For instance how the definition in section 4 of \cite{Gotay1998a} is related to the one in \cite{Ryvkin2018}?

Marco ha fatto un'osservazione interessante: la densità hamiltoniana come definita da Forger-Romero non sembra a priori collegata a nessun elemento delle "L-infty algebra" usata nei suoi lavori. Questo e' in contrasto con il parallelismo che si vorrebbe fare tra algebra di poisson per i sistemi tipo di particella e L-infity algebra per i sistemi tipo campo.

Anche Forger e Romero indicano come che uno dei grandi difetti del formalismo multi-simplettico il fatto che non sia chiaro quale sia la corretta struttura algebrica degli osservabili. Questo problema non è di poco conto visto che la maggior parte degli schemi di quantizzazione partono dal quantizzare l'algebra degli osservabili.

Marco ha parlato con Gotay: dice che la forma ms per i campi è molto particolare (esatta e definita su un particolare fibrato) e che lo studio della geo multisymp a livello molto generale potrebbe alla fine non essere centrale o utile per quanto riguarda la meccanica.

\end{itemize}

%------------------------------------------------------------------------------------------------
% Bibliography (BibTex)
% https://arxiv.org/hypertex/bibstyles/
%------------------------------------------------------------------------------------------------
			\nocite{*}
			\bibliographystyle{ieeetr}
			\bibliography{biblio}
%------------------------------------------------------------------------------------------------

\end{document}