% ****** Start of file apssamp.tex ******
%
%   This file is part of the APS files in the REVTeX 4.1 distribution.
%   Version 4.1r of REVTeX, August 2010
%
%   Copyright (c) 2009, 2010 The American Physical Society.
%
%   See the REVTeX 4 README file for restrictions and more information.
%
% TeX'ing this file requires that you have AMS-LaTeX 2.0 installed
% as well as the rest of the prerequisites for REVTeX 4.1
%
% See the REVTeX 4 README file
% It also requires running BibTeX. The commands are as follows:
%
%  1)  latex apssamp.tex
%  2)  bibtex apssamp
%  3)  latex apssamp.tex
%  4)  latex apssamp.tex
%
\documentclass[%
 reprint,
%superscriptaddress,
%groupedaddress,
%unsortedaddress,
%runinaddress,
%frontmatterverbose,
%preprint,
%showpacs,preprintnumbers,
%nofootinbib,
%nobibnotes,
%bibnotes,
 amsmath,amssymb,
 aps,
%pra,
%prb,
%rmp,
%prstab,
%prstper,
%floatfix,
]{revtex4-1}

\usepackage{graphicx}% Include figure files
\usepackage{dcolumn}% Align table columns on decimal point
\usepackage{bm}% bold math
\usepackage{amsmath}% better dot placment
\usepackage{systeme}% systemes of equations
%\usepackage{hyperref}% add hypertext capabilities
%\usepackage[mathlines]{lineno}% Enable numbering of text and display math
%\linenumbers\relax % Commence numbering lines

%\usepackage[showframe,%Uncomment any one of the following lines to test
%%scale=0.7, marginratio={1:1, 2:3}, ignoreall,% default settings
%%text={7in,10in},centering,
%%margin=1.5in,
%%total={6.5in,8.75in}, top=1.2in, left=0.9in, includefoot,
%%height=10in,a5paper,hmargin={3cm,0.8in},
%]{geometry}

% Personal definitions
\newcommand{\dvec}[1]{\dot{\vec{#1}}}
\newcommand{\grad}{\vec{\nabla}}
\newcommand{\intV}[1]{\int_{-\infty}^{\infty} #1 d^3x}
\newcommand{\intVdot}[1]{\int_{-\infty}^{\infty} #1 d^3\dot{x}}
\newcommand{\intVVdot}[1]{\int_{-\infty}^{\infty}\int_{-\infty}^{\infty} #1 d^3xd^3\dot{x}}

\begin{document}

\title{A Derivation of Fluid Mechanics}% Force line breaks with \

\author{L. Siemens}
\email{lsiemens@uvic.ca}

\date{\today}

\begin{abstract}
I derive a single PDE that describes the dynamics of fluids in state space. Then the similarity between that PDE and equations of fluid mechanics is demonstrated by using it to deriving a set of three equations analogous to the mass, internal energy and Navier-Stokes equations.  Finally I demonstrate that for a fluid with particles following the Maxwell-Boltzmann distribution the set of analogous equations reduces to the equations of inviscid flow.
\end{abstract}

\maketitle

\section{Introduction}

Fluids consist of a large number of interacting particles, presumably the fluid mechanics observed at a macroscopic level is a result of the interactions occurring at a microscopic level. If the dynamics of each particle was know it should be possible in principle to determine the macroscopic dynamics of the fluid from the collective motion of the particles.

\section{State space dynamics}

Assuming the dynamics a particle is defined by the acceleration and that the acceleration of the particles is given by the potential $\phi$ such that $\vec{a} = \vec{\nabla}\cdot\phi$, then the equations of motion become
\[
\frac{d}{dt}\begin{pmatrix} \vec{x} \\ \dvec{x} \end{pmatrix}=\begin{pmatrix} \dvec{x} \\ -\grad\phi \end{pmatrix}
\]

For a system of $n$ particles where the $i^{\text{th}}$ particle is located at $\vec{x}_i$, the potential can in principle depend on the location of all of the particles, such that the potential for the $i^{\text{th}}$ particle is $\phi_i = \phi({\vec{x}_i, \vec{x}_1, \vec{x}_2, ... \vec{x}_i ... , \vec{x}_{n-1}, \vec{x}_n})$. Then for this system the equations of motion for each particle is given by the system of equations
\begin{equation}
\frac{d}{dt}\begin{pmatrix} \vec{x}_i \\ \dvec{x}_i \end{pmatrix}=\begin{pmatrix} \dvec{x}_i \\ -\partial_{\vec{x}_i}\phi_i \end{pmatrix}
\label{discrete_system_dynamics}
\end{equation}

Moving over to a state space description of the system, the state space has six coordinates given by the orthogonal coordinate vectors $\vec{x}$ and $\dvec{x}$. Given a time dependent density distribution defined over state space $\sigma=\sigma(\vec{x}, \dvec{x}, t)$, such that the total mass at the time $t$ is $M(t)=\intVVdot{\sigma(\vec{x}, \dvec{x}, t)}$. The system of discrete particles described by equation \eqref{discrete_system_dynamics} then be described by the distribution
\[
\sigma(\vec{x}, \dvec{x}, t) = m\sum^n_{i=0}\delta(\vec{x} - \vec{x}_i)\cdot\delta(\dvec{x} - \dvec{x}_i)
\]
where $m$ is the mass of the particles, and $\delta(\vec{x})$ is the Dirac delta distribution in 3-space. For this system the six component state space velocity is $\vec{v}=\left\langle\dvec{x}, -\grad\phi(\vec{x}, \rho, t)\right\rangle$, where the potential $\phi$ is defined as $\phi(\vec{x}_i, \rho, t)=\phi_i$ and $\rho(\vec{x}, t)=\intVdot{\sigma(\vec{x}, \dvec{x}, t)}$ . Assuming the number of particles in the distribution is constant, then $\frac{dM}{dt}=0$. Since there are no sources or sinks for the particles, the density distribution is constrained by the continuity equation
\[
\frac{d}{dt}\int_{V}\sigma(\vec{x}, \dvec{x}, t)d^3xd^3\dot{x}+\oint_{\partial V}\sigma(\vec{x}, \dvec{x}, t)\vec{v}\cdot d\vec{a}=0
\]
where $V$ is an arbitrary volume in state space, $\partial V$ is the surface of the arbitrary volume, $\vec{v}$ is the state space velocity and $d\vec{a}$ is a surface element in state space. Rearranging the terms and using the divergence theorem the continuity equation can be rewritten in the form
\[
\int_V\left(\partial_t\sigma + \grad\cdot\left(\sigma\vec{v}\right)\right)d^3xd^3\dot{x}=0
\]

Since the integral equals zero over any arbitrary volume $V$, then the integrand must be zero
\[
\partial_t \sigma + \grad\cdot\left(\sigma\vec{v}\right)=0
\]

Finally the independence of $\vec{x}$ and $\dvec{x}$ can be used to rewrite the continuity equation in the final form, in this case it is written using Einstein notation
\begin{equation}
\partial_t \sigma + \dot{x}_i\partial_{x_i}\sigma-\left(\partial_{x_i}\phi\right)\partial_{\dot{x}_i}\sigma=0
\label{state_space_continuity}
\end{equation}

While the derivation of equation \eqref{state_space_continuity} was motivated using a discrete collection of particles the equation is not restricted to systems of discrete particles. As long as the dynamics in state space is determined by $\vec{v}=\left\langle\dvec{x}, \grad\phi(\vec{x}, \rho, t)\right\rangle$ and there are no sources or sinks for the state space density, then any state space density function or distribution is described by equation \eqref{state_space_continuity}.

Assuming the motion of individual particles, in a fluid described by fluid mechanics, is described by equation \eqref{discrete_system_dynamics} and assuming that the number and mass of the particles is invariant, then the dynamics of the state space distribution is described by equation \eqref{state_space_continuity}. If these assumptions are true for any fluid, then equation \eqref{state_space_continuity} must be capable of reproducing the behavior of fluid mechanics.

\section{Fluid mechanics mass equation: Conservation of mass}
The state space density function $\sigma$ is defined such that $M=\intVVdot{\sigma(\vec{x}, \dvec{x}, t)}$. Define the mass density $\rho$ as $\rho(\vec{x}, t)=\intVdot{\sigma(\vec{x}, \dvec{x}, t)}$. Also define the bulk velocity $\vec{u}$ as $u_i=\frac{1}{\rho(\vec{x}, t)}\intVdot{x_i\sigma(\vec{x}, \dvec{x}, t)}=\intVdot{x_i\frac{\sigma}{\rho}}$. The integral of equation \eqref{state_space_continuity} over velocity space is
\[
\intVdot{\left(\partial_t \sigma + \dot{x}_i\partial_{x_i}\sigma-\left(\partial_{x_i}\phi\right)\partial_{\dot{x}_i}\sigma\right)}=0
\]

Splitting up the integral, pulling out factors and operations that are independent of $\dot{x}_i$ puts the equation into a form where some terms can be evaluated. Then applying the divergence theorem and evaluating the simplified integrals results in the equation
\[
\partial_t\rho + \partial_{x_i}\left(u_i\rho\right)-\left(\partial_{x_i}\phi\right)\oint{\sigma da_i}=0
\]
where $da_i$ is the $i^{\text{th}}$ component of the surface element, and $\oint{\sigma da_i}$ is the surface integral over all of velocity space. Assuming $\sigma(\vec{x}, \dvec{x}, t)$ drops to zero faster than $\lvert\dvec{x}\rvert^2$ then the surface integral converges to zero. Given the surface integral does converge to zero, the resulting equation is $\partial_t\rho + \partial_{x_i}\left(u_i\rho\right)=0$. When written in vector notation it becomes
\begin{equation}
\partial_t\rho + \grad\cdot\left(\vec{u}\rho\right)=0
\label{conservation_of_mass}
\end{equation}
which is the conservation of mass equation from fluid mechanics.

\section{Conservation of momentum}
To get an equation for the conservation of momentum, multiply equation \eqref{state_space_continuity} by $\dvec{x}$ before integrating over velocity space.
\[
\intVdot{\dot{x}_i\left(\partial_t \sigma + \dot{x}_j\partial_{x_j}\sigma-\left(\partial_{x_j}\phi\right)\partial_{\dot{x}_j}\sigma\right)}=0
\]

After simplifying the equation, applying the product rule and divergence theorem it can be written in the form
\[
\begin{split}
& \partial_t\left(u_i\rho\right) + \partial_{x_j}\left(\intVdot{\dot{x}_i\dot{x}_j\sigma}\right) \\ & - \left(\partial_{x_j}\phi\right)\oint\dot{x}_i\sigma da_j + \left(\partial_{x_j}\phi\right)\rho\delta_{i j}=0
\end{split}
\]
where $\oint\dot{x}_i\sigma da_j$ is a surface integral over all of velocity space and $\delta_{ij}$ is the Kronecker delta function. Assuming $\sigma(\vec{x}, \dvec{x}, t)$ drops to zero faster than $\lvert\dvec{x}\rvert^3$ then the surface integral converges to zero. Given the surface integral does converge to zero
\begin{equation}
\partial_t\left(u_i\rho\right) + \partial_{x_j}\left(\intVdot{\dot{x}_i\dot{x}_j\sigma}\right) + \rho\partial_{x_i}\phi=0
\label{incomplete_conservation_of_momentum}
\end{equation}

\subsection{Integrating the velocity tensor product}
The velocity tensor product term is $\intVdot{\dot{x}_i\dot{x}_j\sigma}$. Since the bulk velocity is defined as $u_i=\intVdot{\dot{x}_i\frac{\sigma}{\rho}}$ let
\[
\begin{split}
& \intVdot{\dot{x}_i\dot{x}_j\sigma}=\intVdot{\dot{x}_i\frac{\sigma}{\rho}}\intVdot{\dot{x}_j\sigma} \\ & - \intVdot{\dot{x}_i\frac{\sigma}{\rho}}\intVdot{\dot{x}_j\sigma} + \intVdot{\dot{x}_i\dot{x}_j\sigma}
\end{split}
\]

Evaluating the first term and combining the other two terms produces the equation
\[
\begin{split}
& \intVdot{\dot{x}_i\dot{x}_j\sigma}=\rho u_i u_j \\ & + \frac{1}{2}\left(\intVdot{\left(\dot{x}_i - u_i\right)\dot{x}_j\sigma} + \intVdot{\left(\dot{x}_j - u_j\right)\dot{x}_i\sigma}\right)
\end{split}
\]

If we define a symmetric tensor $A$ such that $A_{ij} = -\frac{1}{2}\left(\intVdot{\left(\dot{x}_i - u_i\right)\dot{x}_j\sigma} + \intVdot{\left(\dot{x}_j - u_j\right)\dot{x}_i\sigma}\right)$, then the integral of the velocity tensor product simplifies to the equation
\[
\intVdot{\dot{x}_i\dot{x}_j\sigma}=\rho u_i u_j - A_{ij}
\]

Let $p=-\frac{1}{3}A_{ii}$ and define the traceless tensor $B$ such that $B_{ij}=A_{ij}-\frac{1}{3}A_{kk}\delta_{ij}$, so the velocity tensor product term can be written as
\[
\intVdot{\dot{x}_i\dot{x}_j\sigma}=\rho u_i u_j + p\delta_{ij} - B_{ij}
\]

\subsection{Fluid mechanics momentum equation: Navier-Stokes equations}
Substituting the integral of the velocity tensor product into equation \eqref{incomplete_conservation_of_momentum}
\[
\partial_t\left(u_i\rho\right) + \partial_{x_j}\left(\rho u_i u_j + p\delta_{ij}-B_{ij}\right) + \rho\partial_{x_i}\phi=0
\]

Expand the partial derivatives involving the bulk velocity and canceling terms leading with $u_i$ by substituting in equation \eqref{conservation_of_mass}. Then the equation can then be written in vector notation as
\begin{equation}
\rho\left(\partial_t \vec{u} + \vec{u}\cdot\grad\vec{u}\right) = - \grad p + \grad B - \rho\grad\phi
\label{conservation_of_momentum}
\end{equation}

Assuming that for a reasonable model of a fluid the tensor $A_{ij}$ evaluates to be the total stress tensor $\sigma_{ij}$, then equation \eqref{conservation_of_momentum} is the Navier-Stokes equations.

\pagebreak % ---------------------------------------------------------------

\section{Conservation of energy}
The total energy of the system is $E_{\text{tot}}=\intVVdot{\left(\frac{1}{2}\dot{x}_i^2 + \phi(\vec{x}, \rho)\right)\sigma(\vec{x}, \dvec{x}, t)}$. Let us define $\vec{v} = \dvec{x} - \vec{u}$, so then $\dot{x}_i^2 = v_i^2 + 2\dot{x}_i u_i - u_i^2$. In order to derive an equation for the conservation of energy equation in position space, equation \eqref{state_space_continuity} must be multiplied by $\frac{1}{2}\dot{x}_i^2 + \phi$ before integrating. To make the derivation of the conservation of energy simpler we can use the linearity of integration to break the conservation of energy equation into a kinetic energy component and a potential energy component.

\subsection{The kinetic energy component}
Multiplying equation \eqref{state_space_continuity} by $\frac{1}{2}\dot{x}_i^2$ then integrating over velocity space, the resulting relation is
\[
\intVdot{\frac{1}{2}\dot{x}_i^2\left(\partial_t \sigma + \dot{x}_j\partial_{x_j}\sigma-\left(\partial_{x_j}\phi\right)\partial_{\dot{x}_j}\sigma\right)}=0
\]

After simplifying the equation, applying the product rule and divergence theorem, it can be written in the form
\[
\begin{split}
& \partial_t\left(\intVdot{\frac{1}{2}\dot{x}_i^2\sigma}\right) + \partial_{x_j}\left(\intVdot{\frac{1}{2}\dot{x}_i^2\dot{x}_j\sigma}\right) \\ & -  \left(\partial_{x_j}\phi\right)\left(\oint\left(\frac{1}{2}\dot{x}_i^2\sigma\right)da_j - \intVdot{\sigma\dot{x}_j}\right)=0
\end{split}
\]
where $\oint\left(\frac{1}{2}\dot{x}_i^2\sigma\right)da_j$ is a surface integral over all of velocity space. Assuming $\sigma(\vec{x}, \dvec{x}, t)$ drops to zero faster than $\lvert\dvec{x}\rvert^4$ then the surface integral converges to zero. Evaluating integrals and rearranging the equation given the surface integral does converge to zero, results in the equation
\[
\partial_t\left(\intVdot{\frac{1}{2}\dot{x}_i^2\sigma}\right) + \partial_{x_j}\left(\intVdot{\frac{1}{2}\dot{x}_i^2\dot{x}_j\sigma}\right) + u_j\rho\partial_{x_j}\phi=0
\]

After expanding the equation by using the definition of $v_i$ and rearranging, use the result $\intVdot{\dot{x}_i\dot{x}_j\sigma}=\rho u_i u_j + p\delta_{ij} - B_{ij}$ from the momentum equation derivation to write the equation in the form
\begin{equation}
\begin{split}
& \partial_t\left(\intVdot{\frac{1}{2}v_i^2\sigma} + \frac{1}{2}u_i^2\rho\right) + u_j\rho\partial_{x_j}\phi \\ & + \partial_{x_j}\left(\intVdot{\frac{1}{2}v_i^2\dot{x}_j\sigma} + \frac{1}{2}u_i^2 u_j\rho + p u_j - u_i B_{ij}\right)=0
\end{split}
\label{incomplete_conservation_of_energy_kinetic}
\end{equation}

\subsection{The potential energy component}
Multiplying equation \eqref{state_space_continuity} by $\phi$ and then integrating over velocity space, the resulting relation is
\[
\intVdot{\phi\left(\partial_t \sigma + \dot{x}_j\partial_{x_j}\sigma-\left(\partial_{x_j}\phi\right)\partial_{\dot{x}_j}\sigma\right)}=0
\]

After rearranging and simplifying use the result that $\intVdot{\partial_{\dot{x}_j}\sigma}=\oint\sigma da_j = 0$, from the derivation for the conservation of mass equation, to get
\[
\phi\partial_t\rho + \phi\partial_{x_j}\left(u_j\rho\right)=0
\]

Applying the product rule produces the form
\begin{equation}
\partial_t\left(\phi\rho\right) - \rho\partial_t\phi + \partial_{x_j}\left(u_j\rho\phi\right) - u_j\rho\partial_{x_j}\phi=0
\label{incomplete_conservation_of_energy_potential}
\end{equation}

\subsection{Total energy density dynamics}
The potential $\phi$ can be split into two components, the internal potential $\phi_{\text{in}}$ and a potential due to the external environment $\phi_{\text{ext}}$, such that $\phi = \phi_{\text{in}} + \phi_{\text{ext}}$. Let us define two energy densities, the internal energy density $e$ and the external energy density $\eta$, such that $e(\vec{x}, t)=\intVdot{\left(\frac{1}{2}v_i^2 + \phi_{\text{in}}\right)\sigma}$ and $\eta(\vec{x}, t)=\intVdot{\left(\frac{1}{2}u_i^2 + \phi_{\text{ext}}\right)\sigma}=\left(\frac{1}{2}u_i^2 + \phi_{\text{ext}}\right)\rho$. From these definitions the total energy is then $E_{\text{tot}}=\intV{\left(e + \eta\right)}$. Adding the two energy components from equation \eqref{incomplete_conservation_of_energy_kinetic} and equation \eqref{incomplete_conservation_of_energy_potential} produces the equation
\[
\begin{split}
& \partial_t\left(\intVdot{\frac{1}{2}v_i^2\sigma} + \frac{1}{2}u_i^2\rho\right) + u_j\rho\partial_{x_j}\phi \\ & + \partial_{x_j}\left(\intVdot{\frac{1}{2}v_i^2\dot{x}_j\sigma} + \frac{1}{2}u_i^2 u_j\rho + p u_j - u_i B_{ij}\right) \\ & + \partial_t\left(\phi\rho\right) - \rho\partial_t\phi + \partial_{x_j}\left(u_j\rho\phi\right) - u_j\rho\partial_{x_j}\phi=0
\end{split}
\]

After simplifying and splitting $\phi$ into its two components and evaluating terms using the energy density definitions the equation can be written as
\[
\begin{split}
& \partial_t\left(e + \eta\right)  - \rho\partial_t\phi + \partial_{x_j}\left(\intVdot{\frac{1}{2}v_i^2\dot{x}_j\sigma} \right. \\ & \left. \vphantom{\intVdot{.}} + u_j\phi_{\text{in}}\rho + \eta u_j + p u_j - u_i B_{ij}\right)=0
\end{split}
\]

Using the definition of $v_i$ and applying the definition internal energy density the relation is
\[
\begin{split}
& \partial_t\left(e + \eta\right) + \partial_{x_j}\left(e u_j + \eta u_j\right) \\ & + \partial_{x_j}\left(\intVdot{\frac{1}{2}v_i^2 v_j\sigma} + p u_j - u_i B_{ij}\right) - \rho\partial_t\phi=0
\end{split}
\]

Defining the vector $\vec{C}$ as $C_j = \intVdot{\frac{1}{2}v_i^2 v_j\sigma}$ and using it to simplify the equation results in the form
\begin{equation}
\begin{split}
& \partial_t\left(e + \eta\right) + \partial_{x_j}\left(e u_j + \eta u_j\right) \\ & + \partial_{x_j}\left(C_j + p u_j - u_i B_{ij}\right) - \rho\partial_t\phi=0
\end{split}
\label{incomplete_conservation_of_energy}
\end{equation}

Equation \eqref{incomplete_conservation_of_energy} describes the dynamics of the total energy density. Using equation \eqref{conservation_of_momentum} an equation for the external energy density can be derived. Which when combined with equation \eqref{incomplete_conservation_of_energy} will produce an equation for the internal energy density.

\subsection{External energy density dynamics}
To generate an equation for the external energy density, let us multiply equation \eqref{conservation_of_momentum} by $u_i$.
\[
u_i\left(\rho\partial_t u_i + \rho u_j\partial_{x_j}u_i + \partial_{x_i}p - \partial_{x_j}B_{ij} + \rho\partial_{x_i}\phi\right)=0
\]

Rearranging and applying the product rule, the equation becomes
\[
\begin{split}
& \partial_t\left(\frac{1}{2}u_i^2\rho\right) - \frac{1}{2}u_i^2\partial_t\rho + \partial_{x_j}\left(\frac{1}{2}u_i^2\rho u_j\right) - \frac{1}{2}u_i^2\partial_{x_j}\left(\rho u_j\right) \\ & + u_i\left(\partial_{x_i}p - \partial_{x_j}B_{ij} + \rho\partial_{x_i}\phi\right)=0
\end{split}
\]

After collecting terms of $\frac{1}{2}u_i^2$, adding a potential energy component $\partial_t\left(\rho\phi_{\text{ext}} - \rho\phi_{\text{ext}}\right) + \partial_{x_j}\left(u_j\rho\phi_{\text{ext}} - u_j\rho\phi_{\text{ext}}\right)=0$, applying the product rule and using the definition of $\eta$ the equation can put in the form
\[
\begin{split}
& \partial_t\eta - \rho\partial_t\phi_{\text{ext}} - \left(\frac{1}{2}u_i^2 + \phi_{\text{ext}}\right)\left(\partial_t\rho + \partial_{x_j}\left(\rho u_j\right)\right)+ \partial_{x_j}\left(\eta u_j\right) \\ & - u_j\rho\partial_{x_j}\phi_{\text{ext}} + u_i\left(\partial_{x_i}p - \partial_{x_j}B_{ij} + \rho\partial_{x_i}\phi\right)=0
\end{split}
\]

Canceling terms by substituting in equation \eqref{conservation_of_mass} and relabeling contracted indices results in the equation
\begin{equation}
\begin{split}
& \partial_t\eta + \partial_{x_j}\left(\eta u_j\right) - \rho\partial_t\phi_{\text{ext}} \\ & + u_j\partial_{x_j}p - u_i\partial_{x_j}B_{ij} + u_j\rho\partial_{x_j}\phi_{\text{in}}=0
\end{split}
\label{incomplete_conservation_of_energy_external}
\end{equation}

\subsection{Fluid mechanics energy equation: Internal energy equation}
To remove the external energy density from equation \eqref{incomplete_conservation_of_energy} subtract equation \eqref{incomplete_conservation_of_energy_external} from it, resulting in the equation
\[
\begin{split}
& \partial_t\left(e + \eta\right) + \partial_{x_j}\left(e u_j + \eta u_j\right) - \rho\partial_t\phi \\ & + \partial_{x_j}\left(C_j + p u_j - u_i B_{ij}\right) \\ & - \partial_t\eta - \partial_{x_j}\left(\eta u_j\right) + \rho\partial_t\phi_{\text{ext}} \\ & - u_j\partial_{x_j}p + u_i\partial_{x_j}B_{ij} - u_j\rho\partial_{x_j}\phi_{\text{in}}=0
\end{split}
\]

After applying the product rule and simplifying, the equation can be written as
\[
\begin{split}
& \partial_te + \partial_{x_j}\left(e u_j\right) - \rho\partial_t\phi_{\text{in}} - u_j\rho\partial_{x_j}\phi_{\text{in}} \\ & + \partial_{x_j}C_j + p\partial_{x_j}u_j - B_{ij}\partial_{x_j}u_i=0
\end{split}
\]
rearranging the equation and putting it in vector notation, the equation then can be written in the from
\begin{equation}
\begin{split}
& \partial_te + \grad\cdot\left(e \vec{u}\right) = \rho\partial_t\phi_{\text{in}} + \rho\vec{u}\cdot\grad\phi_{\text{in}} \\ & - \grad\cdot\vec{C} - p\grad\cdot\vec{u} + B\cdot\grad\vec{u}
\end{split}
\label{conservation_of_energy}
\end{equation}

Assuming that for a reasonable model of a fluid the tensor $A_{ij}$ evaluates to be the total stress tensor $\sigma_{ij}$ and the vector $C_j$ reduce a term including the gradient of the temperature, then equation \eqref{conservation_of_energy} is a form of the fluid mechanics internal energy equation.
\linebreak %-----------------------------------------------------------

\section{Fluid model: Maxwell-Boltzmann distribution}
Assuming the speed of particles in a fluid follow a Maxwell-Boltzmann distribution, then the velocity distribution of the fluid is a normalized Gaussian distribution in three dimensional velocity space. For a fluid element with a non-zero bulk velocity assume that the velocity distribution is simply a normalized Gaussian with a non-zero mean value. Given these assumptions the velocity distribution at any point is $\sigma(\vec{x}, \dvec{x}) / \rho(\vec{x}) = \left(\frac{m}{2\pi kT}\right)^{3/2}e^{-\frac{m\left(x_i - u_i\right)^2}{2kT}}$, where $m$ is the mass of the particles the fluid is made from, $k$ is Boltzmann's constant and $T=T(\vec{x}, t)$ is the thermodynamic temperature. Also assume that the potential $\phi$ only explicitly depends on $\vec{x}$ and $\rho$. So in this model $\phi=\phi(\vec{x}, \rho)$ and the state space density distribution is,
\begin{equation}
\sigma(\vec{x}, \dvec{x}) = \rho(\vec{x})\left(\frac{m}{2\pi kT}\right)^{3/2}e^{-\frac{m\left(x_i - u_i\right)^2}{2kT}}
\label{distribution_maxwell_boltzmann}
\end{equation}

As defined earlier the tensor $A$ is
\[
A_{ij} = -\frac{1}{2}\left(\intVdot{\left(\dot{x}_i - u_i\right)\dot{x}_j\sigma} + \intVdot{\left(\dot{x}_j - u_j\right)\dot{x}_i\sigma}\right)
\]

When $i$ is not equal to $j$ the integral is $A_{ij} = 0$, when $i$ is equal to $j$ the integral is $A_{ij} = -kT\rho/m$, so the full tensor is $A_{ij}=-kT\frac{\rho}{m}\delta_{ij}$. Using the ideal gas law $PV=NkT$, since $\rho/m=N/V$ where $N$ is the number of particles and $V$ is the volume, then $A_{ij} = -P\delta_{ij}$, $p=-\frac{1}{3}A_{ii}=P$ and $B_{ij}=A_{ij}-\frac{1}{3}A_{kk}\delta_{ij} = 0$.

The vector $\vec{C}$ is defined as $C_j = \intVdot{\frac{1}{2}v_i^2 v_j\sigma}$, evaluating the integral results in the equation $C_j = 0$. So given the state space density function \eqref{distribution_maxwell_boltzmann} the equations of fluid mechanics derived from equation \eqref{state_space_continuity} are
\[
\partial_t\rho + \grad\cdot\left(\vec{u}\rho\right)=0
\]

\[
\rho\left(\partial_t \vec{u} + \vec{u}\cdot\grad\vec{u}\right) = - \grad P - \rho\grad\phi
\]

\[
\partial_te + \grad\cdot\left(e \vec{u}\right) = \rho\vec{u}\cdot\grad\phi_{\text{in}} - P\grad\cdot\vec{u}
\]

These equations are equal to the conservation of mass, internal energy density and Navier-Stokes equations in the case of inviscid flow.

\end{document}
%
% ****** End of file apssamp.tex ******


\documentclass[11pt,a4paper]{article}
\usepackage[left=1.1in,right=1.1in,bottom=1.3in,top=1.1in]{geometry}
% Acepta tildes y eñes
\usepackage[utf8]{inputenc}
% Para traducir los chapter a capítulos
\usepackage[spanish,mexico,english]{babel}
\usepackage{graphicx,amsfonts,amsmath,hyperref,tabularx}
\usepackage[x11names,table]{xcolor}
%Para hacer tablas
\usepackage{tabularx}
\usepackage[labelfont=bf,justification=centering]{caption}

\raggedbottom % Los párrafos los mantiene juntos
\usepackage{fancyhdr}
\usepackage{setspace}

\usepackage{listings}
\lstdefinestyle{customc}{basicstyle=\ttfamily,columns=fullflexible }
\lstset{style=customc}

\setstretch{1.3} % Line spacing of 1.3

\begin{document}

\title{Hazel - practical example}
\author{Carlos José Díaz Baso}
\maketitle
%genera doble hoja en blanco
%\cleardoublepage

\section{The reference system}

\begin{figure}[ht!]
\centering
\includegraphics[width=0.4\textwidth]{f1.pdf}
\includegraphics[width=0.5\textwidth]{fig2a.pdf}
\caption{Left) Hazel reference system, rigth) diagram indicating the position of the LOS vector.}
\label{fig:figure1}
\end{figure}

As we can see in Fig. \ref{fig:figure1}, the angle $\chi$ is measured from $X$ to $Y$, and $\theta$ is measured from $Z$ to $\Omega$. We can choose the angle $\chi$ in order to simplify the equations. If we choose $\chi= 0$, then $\Omega$ is between $Z$ and $X$. This configuration only happens when $X$ is radial and  points towards the disk center (DC). However, if we choose $\chi= 180$, then $\Omega$ is between $Z$ and $-X$. This configuration only happens when $X$ is radial and  points away from the disk center (DC). In the rigth panel we see where is $\Omega$ if we choose the value of $\chi$.

The consecuences of choosing one option are: the reference system itself and the equations to find all the posible solutions.

\clearpage
\section{Example 1.}
We have the following data from our observation and we choose $\chi=$180d:
\begin{itemize}
\item Position [arcsec]: $x=-300.0$; $y=-200.0$ \& Q$>$0: N-S
\end{itemize}

We can calculate the angle from the equator:
\begin{lstlisting}
alpha = np.arctan(y/x)*180./(np.pi) = 33.7d
\end{lstlisting}
Then we can calculate the heliocentric angle:
\begin{lstlisting}
theta = np.arcsin(np.sqrt(x**2.+y**2.)/960.)*180/np.pi = 22.1d
\end{lstlisting}
Now we can calculate $\gamma$ (must be measured from X to Y, anticlockwise). In Fig. \ref{fig:example1} (right) you have two solutions: the purple and the blue one:
\begin{lstlisting}
Gamma(purple) = 360-(90+33.7)=236.3d    or     Gamma(blue) =(90-33.7) = 56.3
\end{lstlisting}
Q$>$0 is a line, not  a direction. Gamma is defined [0,180º] (look the Hazel GUI) so in principle you can choose the direction of Q$>$0 which makes Gamma  inside the range. In order to check the result, you can execute the 3D plot to visualize the result (Fig. \ref{fig:example1}).

\begin{figure}[ht!]
\centering
\includegraphics[width=0.35\textwidth]{example1A.pdf}
\includegraphics[width=0.55\textwidth]{example1B.pdf}
\includegraphics[width=0.55\textwidth]{example1_3D.pdf}
\caption{Hazel reference system}
\label{fig:example1}
\end{figure}
\clearpage
Then, the angles for this observation are:
\begin{lstlisting}
theta_OBS = 22.1d
chi_OBS = 180.0d
Gamma_OBS =(90-33.7) = 56.3
\end{lstlisting}

\section{Example 2.}
We have the same observation but we choose $\chi=$0d. Now, $\gamma$ is again measured from $X$, and it is the same as before (Fig. \ref{fig:example2}).
\begin{lstlisting}
Gamma(blue) =(90-33.7) = 56.3
\end{lstlisting}

\begin{figure}[ht!]
\centering
\includegraphics[width=0.35\textwidth]{example2A.pdf}
\includegraphics[width=0.55\textwidth]{example2B.pdf}
\includegraphics[width=0.55\textwidth]{example2_3D.pdf}
\caption{Hazel reference system}
\label{fig:example2}
\end{figure}

Then, the angles for this observation are:
\begin{lstlisting}
theta_OBS = 22.1d
chi_OBS = 0.0d
Gamma_OBS =(90-33.7) = 56.3
\end{lstlisting}

\end{document}


\documentclass[a4paper,11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[margin=1cm]{geometry}
\usepackage{amsthm}
%\usepackage{fullpage}

\newtheorem{definition}{Definition}
\newtheorem{lemma}{Lemma}
\newtheorem{theorem}{Theorem}
\newtheorem{notation}{Notation}

\newcommand{\Inv}{\mathit{Inv}}
\newcommand{\app}{\mathit{\,++\,}}
\newcommand{\Default}{\mathit{Default}}
\newcommand{\pc}{\mathit{pc}}
\newcommand{\ex}{\mathit{ex}}
\newcommand{\ar}{\mathit{ar}}
\newcommand{\WP}{\mathit{WP}}

\newcommand{\sect}[1]{\noindent\parbox{\textwidth}{#1}}
\newcommand{\sep}{\hspace{-1.5707965cm}\rule{\paperwidth}{1pt}}

\newcommand{\UNSHIFT}{\mathit{unshift}}
\newcommand{\SHIFT}{\mathit{shift}}
\newcommand{\Dom}{\mathit{Dom}}
\newcommand{\SELECT}{\mbox{SELECT}}
\newcommand{\Stack}{\mathrm{s}}
\newcommand{\TRUE}{\mathit{tt}}
\newcommand{\FALSE}{\mathit{ff}}
\newcommand{\IF}{\mathrm{IF}}
\newcommand{\ghost}[1]{{\it #1}^g}
\newcommand{\LockSet}{\mathsf{Lck}}

\begin{document}
\pagestyle{empty}

\sep

\begin{notation}[$\Inv$]
$\Inv$ will from now on denote the visible state invariant of the program in question.
\end{notation}

\sep

\begin{notation}[$\Default$]
$\Default$ denotes an assertion specifying that all variables have their default values. ($\Default$ is true at the initial configuration.)
\end{notation}

\sep

\begin{definition}[Calling / Returning / Visible Configuration]
~\\[-5mm]
\begin{itemize}
\item A configuration $C=(h, (c, m, \pc, s, l)::R)$ is \emph{calling} if $C \rightarrow C'$ for some $C' = (h', (c', m', \pc', s', l')::(h, (c, m, \pc, s, l))::R)$.
\item A configuration $C=(h, a::R)$ is \emph{returning} if $C \rightarrow (h', R)$.

  (Returning normally / exceptionally depending on shape of $a$ but I currently don't think it ever matters.)

%\item A configuration $C=(h, (o)::R)$ is \emph{returning exceptionally} if $C \rightarrow C'$ for some $C' = (h', R)$.
\item A configuration is \emph{visible} if it is calling or returning.
\end{itemize}
\end{definition}

\sep

\begin{definition}[Annotated Method / Program]
An \emph{annotated method} is an ordinary method with an assertion attached to each instruction.

An \emph{annotated program} is an ordinary program (whose methods are annotated) together with an $\Inv$.
\end{definition}

\sep

\begin{definition}[$\WP$]\label{def:wp}
See figure \ref{fig:wp}. Note that the figure suggests that any instruction may have an exceptional outcome. I think that this this would be quite heavy in the proofs and that we (at least in this formalization) should restrict it to {\tt athrow} and {\tt invoke}.
\begin{figure}
\[ \WP_M(L) = \WP_M^N(L) \land \WP_M^E(L) \quad\mbox{where~}M = (I, H, A)\]

\[
\begin{array}{ll}
\hline
I_L & \WP_M^N(L) \\ \hline
\mbox{\tt aload}\ n        & \UNSHIFT(A_{L+1}[l_n/\Stack_0]) \\[1pt]
\mbox{\tt astore}\ n       & (\SHIFT(A_{L+1}))\wedge \Stack_0 = l_n \\[1pt]
\mbox{\tt athrow}          & \TRUE \\[1pt]
\mbox{\tt goto}\ L'        & A_{L'}  \\[1pt]
\mbox{\tt if\_icmpeq}~L'   & \IF(\Stack_0=\Stack_1,\SHIFT^2(A_{L'}),\SHIFT^2(A_{L+1}))\\[1pt]
\mbox{\tt ifeq}\ L'        & \IF(\Stack_0=0,\SHIFT(A_{L'}), \SHIFT(A_{L+1})) \\[1pt]
\mbox{\tt getstatic}\ c.f  & \left\{\begin{array}{@{}l}
                                    c' \in \LockSet \land \UNSHIFT (A_{L+1}[c.f/\Stack_0]) \textrm{if $\Gamma_\mathrm{L}(c') = (F,a)$ and $(c.f) \in F$}\\
                                    \forall x. \UNSHIFT(A_{L+1}[x/\Stack_0]) \ \ \ \mbox{otherwise}
                                    \end{array}\right.\\[1pt]
\mbox{\tt putstatic}\ c.f  & \left\{\begin{array}{@{}l}
                                    c' \in \LockSet \land \SHIFT(A_{L+1})[\Stack_0/c.f] \textrm{if $\Gamma_\mathrm{L}(c') = (F,a)$ and $(c.f) \in F$}\\
                                    \SHIFT(A_{L+1})[\Stack_0/c.f] \ \ \ \mbox{otherwise}
                                    \end{array}\right.\\[1pt]
\mbox{\tt ldc}\ v          & \UNSHIFT(A_{L+1}[v/\Stack_0]) \\[1pt]
\mbox{\tt ldc\ c}          & \left\{\begin{array}{@{}l}
                                    \FALSE \ \ \ \ \textrm{if $c \in \Dom(\Gamma_L) \land I_{L+1} \neq \mbox{\tt monitor*}$}\\
                                    \UNSHIFT(A_{L+1}[c/\Stack_0]) \ \ \ \ \textrm{otherwise}
                                    \end{array}\right.\\[1pt]
\mbox{\tt monitorenter}    & \left\{\begin{array}{@{}l}
                                    \lnot(s_0 \in \LockSet)\> \land \forall\mathbf{x}.(a \Rightarrow \SHIFT(A_{L+1})[\LockSet\cup\{s_0\}/\LockSet])[\mathbf{x}/F] \\
                                    \hspace*{1cm}\textrm{if $I_{L-1} = \mbox{\tt LDC\ $c$}$ and $(c, (F, a)) \in \Gamma_L$}\\
                                    \SHIFT(A_{L+1})[\LockSet \cup \{s_0\}/\LockSet] \ \ \ \textrm{otherwise}
                                    \end{array}\right.\\[1pt]
\mbox{\tt monitorexit}     & \left\{\begin{array}{@{}l}
                                    a \land (\forall \mathbf{x}. \SHIFT(A_{L+1})[\mathbf{x}/F][\LockSet\!\setminus\!\{s_0\}/\LockSet]) \\
                                    \hspace*{1cm} \textrm{if $I_{L-1} = \mbox{\tt LDC\ $c$}$ and $(c, (F, a)) \in \Gamma_L$}\\
                                    A_{L+1}[\LockSet \setminus\{s_0\}/\LockSet] \ \ \  \textrm{otherwise}
                                    \end{array}\right.\\[1pt]
\begin{array}{@{}l@{}}
\mbox{\tt invoke-} \\
\mbox{\tt virtual}\ c.m
\end{array}                & \left\{\begin{array}{@{}ll}
                                    A_{L+1} & \mbox{if $c.m$ is a non-client-reentrant API method} \\
                                    Inv    & \textrm{if for each successor label $L'$, $A_{L'} = Inv$} \\
                                    \FALSE & \mbox{otherwise}
                                    \end{array}\right.\\[1pt]
\multicolumn{2}{l}{\mbox{\tt invokestatic System.exit}\quad \TRUE}\\[1pt]

\multicolumn{2}{l}{\langle \ghost{\bf x} := a_1 \rightarrow {\bf e}_1 \mid \ldots \mid a_n \rightarrow {\bf e}_n \rangle} \\
 & \begin{array}{@{}l@{}l@{}}\SELECT(&\langle a_1, \ldots, a_n\rangle,\\ & \langle A_{L+1}
[{\bf e}_1/\ghost{{\bf x}}], \ldots, A_{L+1}[{\bf e}_n/\ghost{{\bf x}}]\rangle,\FALSE)\end{array} \\

\textrm{other}             & \left\{\begin{array}{@{}ll}
                                    Inv   & \textrm{if for each successor label $L'$, $A_{L'} = Inv$} \\
                                    \FALSE & \mbox{otherwise}
                                    \end{array}\right. \\ \hline
\end{array}
\]

\[
\begin{array}{l@{~=~}l}
\WP_M^E(L) & \WP_M^E(L, H) \\
\WP_M^E(L, \epsilon) & Inv \\
\WP_M^E(L, (b, e, L', \mathit{Throwable}) :: H') & A_{L'} \quad \mbox{if $b \leq L < e$} \\
\WP_M^E(L, (b, e, L', c) :: H') & A_{L'} \land \WP_M^E(L, H')
\end{array}
\]

\caption{\label{fig:wp} Specification of the $\WP_M$ function}
\end{figure}
\end{definition}

\sep

\begin{definition}[Valid Execution]
An execution $E = C_0C_1\ldots$ of an annotated program is \emph{valid} if for each $i: 0 \leq i~(\leq |E|)$ the following hold
\begin{itemize}
\item If $C_i$ is visible (calling or returning), then $\Inv$ holds.
\item If $C_i$ is of the form $(h, (c, m, \pc, s, l)::R)$, then $\| A_{\pc} \| C_i$ holds.
\end{itemize}
where $A$ denotes the array of assertions in method $c.m$.
\end{definition}

\sep

\begin{definition}[Global Validity]
A program $P$ is \emph{globally valid} if all possible executions of $P$ are valid.
\end{definition}

\sep

\sect{\begin{definition}[Local Validity (Method)]\label{def:local_validity_method}
An annotated method $M = (I, H, A)$ is \emph{locally valid} if
\begin{enumerate}
\item $\Inv \Rightarrow A_0$
\item $A_L \Rightarrow \WP_M(L)$ for $L$ s.t. $0 \leq L \le |I|$
\end{enumerate}

\noindent\framebox[\linewidth]{\parbox{\linewidth}{Important note 1 (which we might have overlooked in original defs): The assertion at an execption handle target label may not mention anything but $\Inv$ that depends on the heap, since $\Inv$ is the only guarantee we can fulfill about the heap after having made a call to another method.}}

\noindent\framebox[\linewidth]{\parbox{\linewidth}{Important note 2 (which we definately overlooked in original defs): The assertion at an execption handle target label may not mention the stack since JLS }}
\end{definition}
}

\sep

\begin{definition}[Local Validity (Program)]\label{def:local_validity_program}
An annotated program is locally valid if
\begin{enumerate}
\item All its methods are locally valid
\item $\Default \Rightarrow Inv$
\end{enumerate}
\end{definition}

\sep

\begin{lemma}[Invoke Only Caller]\label{lem:invoke_only_caller}
If a configuration $(h, (c, m, \pc, s, l)::R)$ is calling, then the instruction at $\pc$ in $c.m$ is an invoke instruction.
\end{lemma}
\begin{proof} By case analysis of the possible transitions. {\bf (Partly formalized. Huge proof.)}\end{proof}

\sep

\newpage

\begin{lemma}[$\WP$ correct (normal)]\label{lem:wp_correct_normal}
Let $C = (h, (c, m, \pc, s, l)::R)$ and $C' = (h', (c', m', \pc', s', l')::R')$.\\
If $C \rightarrow C'$, then $\| \WP_{c.m}(\pc) \| C \Leftrightarrow \| A_{\pc'} \| C'$.
\end{lemma}

\begin{proof}
Case analysis on the instruction at $c.m[pc]$.

\begin{itemize}
\item {\tt aload} $n$:
  We know that
  \begin{itemize}
  \item $C' = (h, (c, m, \pc+1, l(n) :: s, l)::R)$
  \item $\WP_{c.m}(\pc) = \UNSHIFT(A_{\pc+1}[l_n/\Stack_0])$
  \end{itemize}

  Left to show: $ \| \UNSHIFT(A_{\pc+1}[l_n/\Stack_0]) \| C \Leftrightarrow \| A_{\pc+1} \| C' $

  Proceeding with structural induction on $A_{\pc+1}$.

  \begin{itemize}
  \item $\TRUE$: Applying substitution and $\UNSHIFT$ yields $\|\TRUE\|C \Leftrightarrow \| \TRUE \| C'$, which equals $\TRUE \Leftrightarrow \TRUE$.
  \item $\FALSE$: Applying substitution and $\UNSHIFT$ yields $\|\FALSE\|C \Leftrightarrow \| \FALSE \| C'$, which equals $\FALSE \Leftrightarrow \FALSE$.
  \item $A_1 \land A_2$:
    \begin{enumerate}
    \item IH$_1$: $ \| \UNSHIFT(A_1[l_n/\Stack_0]) \| C \Leftrightarrow \| A_1 \| C'$
    \item IH$_2$: $ \| \UNSHIFT(A_2[l_n/\Stack_0]) \| C \Leftrightarrow \| A_2 \| C'$
    \item Applying substitution yields $\| \UNSHIFT(A_1[l_n/\Stack_0] \land A_2[l_n/\Stack_0]) \| C \Leftrightarrow \| A_1 \land A_2 \| C'$
    \item Applying $\UNSHIFT$ yields $\| \UNSHIFT(A_1[l_n/\Stack_0]) \land \UNSHIFT(A_2[l_n/\Stack_0]) \| C \Leftrightarrow \| A_1 \land A_2 \| C'$
    \item Which equals $ \| \UNSHIFT(A_1[l_n/\Stack_0]) \| C$ and $\| \UNSHIFT(A_2[l_n/\Stack_0]) \| C \Leftrightarrow \| A_1 \land A_2 \| C'$
    \item By IH$_1$ and IH$_2$, we know $ \| A_1 \| C'$ and $\| A_2 \| C' \Leftrightarrow \| A_1 \land A_2 \| C'$
    \item Which equals, $\| A_1 \land A_2 \| C' \Leftrightarrow \| A_1 \land A_2 \| C'$.
    \end{enumerate}
  \item $\neg A_1$:
    \begin{enumerate}
    \item IH: $ \| \UNSHIFT(A_1[l_n/\Stack_0]) \| C \Leftrightarrow \| A_1 \| C'$
    \item Applying substitution yields $ \| \UNSHIFT(\neg (A_1[l_n/\Stack_0])) \| C \Leftrightarrow \| \neg A_1 \| C'$
    \item Applying $\UNSHIFT$ yields $ \| \neg \UNSHIFT(A_1[l_n/\Stack_0]) \| C \Leftrightarrow \| \neg A_1 \| C'$
    \item Which equals $ \neg \| \UNSHIFT(A_1[l_n/\Stack_0]) \| C \Leftrightarrow \| \neg A_1 \| C'$
    \item Rewriting rhs yields $ \neg \| \UNSHIFT(A_1[l_n/\Stack_0]) \| C \Leftrightarrow \neg \| A_1 \| C'$
    \item By lemma ? it is sufficient to show $\| \UNSHIFT(A_1[l_n/\Stack_0]) \| C \Leftrightarrow \| A_1 \| C'$
    \item Which equals the IH.
    \end{enumerate}
  \item $A_1 \lor A_2$, $\IF(A_1, e_1, e_2)$ Similarly as above.
  \item $e_1 \leq e_2$.
    \begin{enumerate}
    \item We need an auxillary lemma for expressions: $\forall e, \| \UNSHIFT(e[l_n/\Stack_0]) \| C = \| e \| C' $

      Proceed with structural induction on $e$.
      \begin{itemize}
      \item $v$: $\| \UNSHIFT(v[l_n/\Stack_0]) \| C = \| \UNSHIFT(v) \| C = \| v \| C = v = \| v \| C'$
      \item $\Stack_k$: Case split on $k = 0$
        \begin{itemize}
        \item $k = 0$:    $\| \UNSHIFT(\Stack_0[l_n/\Stack_0]) \| C = \| \UNSHIFT(l_n) \| C = \| l_n \| C = ls(n) = \| l_n \| C'$
        \item $k \neq 0$: $\| \UNSHIFT(\Stack_k[l_n/\Stack_0]) \| C = \| \UNSHIFT(s_k) \| C = \| \Stack_{k-1} \| C = s(n-1) = (l(n)::s)(n) = \| \Stack_k \| C'$
        \end{itemize}
      \item $e_1 + e_2$:
        \begin{enumerate}
        \item $\| \UNSHIFT((e_1 + e_2)[l_n/\Stack_0]) \| C$
        \item Applying substitution yields $\| \UNSHIFT(e_1[l_n/\Stack_0] + e_2[l_n/\Stack_0]) \| C$
        \item Applying $\UNSHIFT$ yields $\| \UNSHIFT(e_1[l_n/\Stack_0]) + \UNSHIFT(e_2[l_n/\Stack_0]) \| C$
        \item Which equals $\| \UNSHIFT(e_1[l_n/\Stack_0]) \| C + \| \UNSHIFT(e_2[l_n/\Stack_0]) \| C$
        \item By IH, we know that this equals $\| e_1 \| C' + \| e_2 \| C'$
        \item Which equals $\| e_1 + e_2 \| C'$
        \end{enumerate}
      \item $e_1.f$:
        \begin{enumerate}
        \item $\| \UNSHIFT(e_1.f[l_n/\Stack_0]) \| C$
        \item Applying substitution yields $\| \UNSHIFT(e_1[l_n/\Stack_0].f) \| C$
        \item Applying $\UNSHIFT$ yields $\| \UNSHIFT(e_1[l_n/\Stack_0])).f \| C$
        \item Which equals $(\| \UNSHIFT(e_1[l_n/\Stack_0]) \| C)(f)$
        \item By IH, we know that this equals $(\| e_1 \| C')(f)$
        \item Which equals $\| e_1.f \| C'$
        \end{enumerate}

      \item $c.f$, $l_k$, $e_1 \rightarrow e_2 \mid e_3$, $\bot$: treated similarly as above.
      \end{itemize}
    \item Applying substitution yields $ \| \UNSHIFT(e_1[l_n/\Stack_0] \leq e_2[l_n/\Stack_0]) \| C \Leftrightarrow \| e_1 \leq e_2 \| C'$.
    \item Applying $\UNSHIFT$ yields $ \| \UNSHIFT(e_1[l_n/\Stack_0]) \leq \UNSHIFT(e_2[l_n/\Stack_0]) \| C \Leftrightarrow \| e_1 \leq e_2 \| C'$.
    \item Which equals $ \| \UNSHIFT(e_1[l_n/\Stack_0]) \| C \leq \| \UNSHIFT(e_2[l_n/\Stack_0]) \| C \Leftrightarrow \| e_1 \leq e_2 \| C'$.
    \item By lemma above, this equals $ \| e_1 \| C' \leq \| e_2 \| C' \Leftrightarrow \| e_1 \leq e_2 \| C'$.
    \item Which equals $ \| e_1 \leq e_2 \| C' \Leftrightarrow \| e_1 \leq e_2 \| C'$.
    \end{enumerate}
  \end{itemize}

\item {\tt astore} $n$
\item {\tt athrow}
\item {\tt dup}
\item {\tt getfield} $f$
\item {\tt getstatic} $c''$ $f$
\item {\tt goto} $L'$
\item {\tt iadd}
\item {\tt iconst} $i$
\item {\tt ifeq} $L'$
\item {\tt invoke} $c''$ $m''$
\item {\tt putstatic} $c''$ $f$
\item {\tt ldc} $v$
\item {\tt exit}
\item {\tt ret}
\item {\tt nop}
\end{itemize}
\end{proof}

\sep

\sect{\begin{lemma}[At most one exceptional frame]\label{lem:at_most_one_exc_arec}
There can be at most one exceptional frame on the current activation record stack.
\end{lemma}
\begin{proof} Induction on the length of the execution.
\emph{Base case:} An initial configuration has no exceptional frame. \emph{Inductive Step:} Case study of the transition rules.
\end{proof}}

\sep

\sect{\begin{lemma}[$\WP$ correct (exceptional)]\label{lem:wp_correct_exceptional}
If
\begin{enumerate}
  \item $C = (h, (o)::(c, m, \pc, s, l)::R)$,
  \item $C' = (h', (c', m', \pc', s', l')::R')$,
  \item $C \rightarrow C'$, and
  \item $\|\WP_{c.m}(\pc) \| (h'', (c, m, \pc, s, l)::R)$ for some $h''$
\end{enumerate}
then $\| A_{\pc'} \| C'$
\end{lemma}
\begin{proof}(Sketch)
\begin{enumerate}
%\item We know that the exception was thrown as a consequence of the instruction at $c.m[\pc]$ directly ({\tt athrow}, $C = C''$) or indirectly ({\tt invoke}, $C \neq C''$). (Needs to be shown.)
%\item In either way, we know that $c = c'$, $m = m'$, $s = \epsilon$ and $l = l'$.
\item By the transition rules, we know that $\pc'$ must be the target label of an exception handler.
\item From def \ref{def:local_validity_method} we know that the only way $A_{\pc'}$ references the heap is by $\Inv$.
\item From def \ref{def:local_validity_method} we know that $A_{\pc'}$ does not mention the stack.
\item From def of $\WP^E$ we know that $A_{\pc'}$ held at $C''$ and that nothing $A_{\pc'}$ mentiones has changed since then.
\end{enumerate}
\end{proof}}

\sep

\newpage

%\sect{\begin{lemma}[At most one push to ar-stack]\label{lem:most_one_ar_push}
%If $(h, R) \rightarrow (h', R'++R)$, then $R'$ is of the shape $nil$ or $a$ (single activation record). (Or, in words, a transition adds \emph{at most one} activation record to the activation record stack.)
%\end{lemma}
%\paragraph{Proof} Case analysis of possible transitions.
%}

\sect{\begin{lemma}[Activation Record Suffixes]\label{lem:ar_suffixes}
%If $(h, R) \rightarrow (h', R')$, and $R''$ is a proper suffix of $R'$, then $R''$ is a suffix of $R$.
If $(h, R) \rightarrow (h', a::R')$ then $R'$ is a suffix of $R$.
\end{lemma}
\begin{proof} Case analysis of possible transitions.\end{proof}
}

\sep

\sect{\begin{lemma}[All activation records in current ar-stack has been on top]\label{lem:all_suffs_in_ex}
If $\ex = (h_0, R_0)\ldots (h,R)$ is an execution of $P$, then for all non-empty suffixes $R''$ of $R$, there exists a heap $h''$ such that $(h'', R'')$ is a configuration in $\ex$.
\end{lemma}
\begin{proof} By induction on the structure of an execution.
\begin{itemize}
\item Base Case: $\ex = (h_0, R_0)$ (an initial configuration)
  \begin{enumerate}
  \item Assume $\ex$ is an execution of $P$.
  \item Since $(h_0, R_0)$ must be an initial configuration, we have $R_0 = \ar :: nil$.
  \item The only non-empty suffix $R''$ of $\ar::nil$ is $\ar::nil$.
  \item Thus there exists an $h''$, namely $h_0$ such that $(h'', R'') \in \ex$.
  \end{enumerate}
\item Inductive Step: $\ex = \ex'(h,R)(h', R')$
  \begin{enumerate}
  \item Assume $\ex$ is an execution of $P$.
  \item Assume $R''$ is a non-empty suffix of $R'$.
  \item We do a case split on the type of suffix:
    \begin{itemize}
      \item $R' = R''$ ($R''$ is a ``non-proper'' suffix):
        \begin{enumerate}
        \item There exists an $h''$, namely $h'$ such that $(h'', R'') \in \ex(h,R)(h', R')$
        \end{enumerate}
      \item $R' = a_0::...::a_k::R''$ ($R''$ is a proper suffix):
        \begin{enumerate}
        \item Since $\ex(h,R)(h',R')$ is an execution, we have $(h, R) \rightarrow (h', R')$.
        \item By lemma \ref{lem:ar_suffixes} we know that $R''$ is a suffix of $R$.
        \item By IH we know that this lemma holds for $\ex(h, R)$.
        \item Therefor there exists a $h''$ such that $(h'', R'') \in \ex (h, R)$
        \item Thus there exists a $h''$ such that $(h'', R'') \in \ex (h, R) (h', R')$
        \end{enumerate}
    \end{itemize}

  \end{enumerate}
\end{itemize}
\end{proof}
}

\sep

\sect{
\begin{theorem}[Local Validity implies Global Validity]
  For any annotated program $P$, if $P$ is locally valid, then $P$ is globally valid.
\end{theorem}

\begin{proof}
\begin{enumerate}
\item Assume that $P$ is locally valid.
\item Pick an arbitrary execution $\ex$ of $P$ and show that it is valid.
\item Proceed by structural induction on $\ex$. (Note that executions are not empty.)
  \begin{description}
  \item[Base Case:] $\ex$ can be written as $C_0$ where $C_0$ denotes an initial configuration.

    \begin{enumerate}
    \item By definition, $\Default$ holds in $C_0$.
    \item By def \ref{def:local_validity_program}.2 $\Inv$ holds in $C_i$.
    \item To show that $\ex$ is a valid execution, we show:
      \begin{itemize}
      \item If $C_0$ is visible (calling or returning), then $\Inv$ holds.

        \begin{enumerate}
        \item That $\Inv$ holds in $C_0$ is established above.
        \end{enumerate}

      \item If $C_0$ is of the form $(h, (c, m, \pc, s, l)::R)$, then $\| A_{\pc} \| C_i$ holds.

        \begin{enumerate}
        \item That $\Inv$ holds in $C_0$ is established above.
        \item By def \ref{def:local_validity_method}.1 $\|A_0\|C_i$ holds.
        \end{enumerate}

      \end{itemize}
    \end{enumerate}

  \item[Inductive Case:] $\ex$ can be written as $\ex'C_i$ where $\ex'$ is a valid execution.

    According to def of valid execution, we need to show that
    \begin{itemize}
      \item If $C_i$ is of the form $(h', (c', m', \pc', s', l')::R')$, then $\| A_{\pc'} \| C_i$ holds.

      Note that by IH, $\ex'$ is a valid execution and thus of length at least 1. We split proof into two cases:
      \begin{itemize}
      \item Previous configuration is normal: $C_{i-1} = (h, (c, m, \pc, s, l)::R)$

        \begin{enumerate}
          \item By IH we know that $\| A_{\pc} \| C_{i-1}$ holds.
          \item By local validity we therefor know $\| \WP_{c.m}(\pc) \|C_{i-1}$.
          \item By lemma \ref{lem:wp_correct_normal}, $\| A_{\pc'} \| C_i$ holds.
        \end{enumerate}

      \item Previous configuration is exceptional: $C_{i-1} = (h, (o)::R)$

        \begin{enumerate}
          \item First note that $R$ may not be nil since we do have a successor.
          \item By lemma \ref{lem:at_most_one_exc_arec} we know that $R$ can be written on the form $(c, m, \pc, s, l)::R''$.
          \item Let $C''$ denote the configuration at which $(c, m, \pc, s, l)$ was the top of the ar-stack. (Some such configuration exists according to lem \ref{lem:all_suffs_in_ex}.)
          \item By IH we know that $\| A_{\pc} \| C''$ hold.
          \item By local validity we thus know that $\| \WP_{c.m}(\pc) \| C''$ hold.
          \item By lemma \ref{lem:wp_correct_exceptional}, $\| A_{\pc'} \| C_i$ holds.
        \end{enumerate}

      \end{itemize}

    \item If $C_i$ is calling, then $\Inv$ holds.
      \begin{enumerate}
      \item Due to lemma \ref{lem:invoke_only_caller}, we know that the current instruction is an invoke.
      \item Since $\WP$ of an invoke instruction is $\Inv$ we know by def \ref{def:local_validity_method}.2 that the current assertion implies $\Inv$.
      \item By the same reasoning as above, we know that the current assertion holds, thus $\Inv$ holds.
      \end{enumerate}
    \item If $C_i$ is returning, then $\Inv$ holds.

      (Sketch) Should be ensured by local validity + def of $\WP$

      \begin{itemize}
      \item Normal return: $\WP^N$
      \item Exceptional return: $\WP^E$
      \end{itemize}

    \end{itemize}

  \end{description}
\end{enumerate}
\end{proof}
}

\newpage
\section{EXISTS\_EXEC\_WITH\_EQ\_GUS}
\newcommand{\ssus}{\mathit{ssus}}
\newcommand{\gus}{\mathit{gus}}
\newcommand{\sst}{\mathit{sst}}
\renewcommand{\ss}{\mathit{ss}}
\newcommand{\gv}{\mathit{gv}}
\newcommand{\pref}{\mathit{pref}}

\begin{lemma}[before\_gu\_precedes\_before\_ssu]\label{lem:before_gu_precedes_before_ssu}
An instruction that causes a sec-state-update of the shape {\tt contract after c m} is always preceded by an instruction on the form {\tt ghost\_update (contract after c m)}.
\end{lemma}
\begin{proof}
TBD.
\end{proof}

\begin{lemma}[gu\_after\_follows\_ssu\_after]\label{lem:gu_after_follows_ssu_after}
An instruction causing a sec-state-update of the shape {\tt contract after c m} may be followed by an instruction on the form {\tt ghost\_update (contract after c m)}.
\end{lemma}
\begin{proof}
TBD.
\end{proof}

\begin{lemma}[after\_su\_precedes\_after\_gu]\label{lem:after_su_precedes_after_gu}
An instruction on the form {\tt ghost\_update (contract before c m)} is always preceded by an instruction that causes a {\tt before c m}-event (which in turn gives rise to a sec-state-update of the shape {\tt contract before c m}).
\end{lemma}
\begin{proof}
TBD.
\end{proof}

\begin{lemma}[exec\_tail\_cases]\label{lem:exec_tail_cases}
For each execution $E$ of a ghost inlined program, that ends in a configuration $C$, we have:
\\\begin{tabular}{llll}
1: & (1a) $C = $ {\tt before-ghost-update gu}      & and (1b) {\tt (ssus $E$) ++ gu = gus $E$}   & OR \\
2: & (2a) $C = $ {\tt before-sec-state-update}     & and (2b) {\tt ssus $E$ = gus $E$}           & OR \\
3: & (3a) $C = $ {\tt after-sec-state-update ssu}  & and (3b) {\tt ssus $E$ = (gus $E$) ++ ssu}  & OR \\
4: & (4a) $C = $ {\tt after-ghost-update gu}       & and (4b) {\tt ssus $E$ = gus $E$}           & OR \\
5: & (5a) $C = $ {\tt none of the above}           & and (5b) {\tt ssus $E$ = gus $E$}           &
\end{tabular}
\end{lemma}

\begin{proof}
This can be shown by induction over the length of $E$. The base case is trivial. Case 2 should follow from lemma \ref{lem:before_gu_precedes_before_ssu}. Case 4 should follow from lemma \ref{lem:after_su_precedes_after_gu}.
\end{proof}

\begin{lemma}[EXISTS\_EXEC\_WITH\_EQ\_GUS]\label{lem:exists_exec_with_eq_gus}
For all executions $E$ of a ghost inlined program, there exists an extended execution $E'$ of $E$, such that the ghost state updates of $E'$ ($\gus(E')$) equal the security state updates of $E$ ($\ssus(E)$).
\end{lemma}

\begin{proof}
  \begin{enumerate}
  \item Pick an arbitrary $E$ (and show $\exists E', \ssus(E) = \gus(E')$.
  \item The case in which $E = \mathit{nil}$ is trivial, thus we assume that $E = E0 \app C$.
  \item Thus we have left to show $\exists E', \ssus(E0 \app C) = \gus(E')$.
  \item We do a case-split on the type of $C$ according to lemma \ref{lem:exec_tail_cases} above.

    \begin{enumerate}
    \item[Case 1:]
      \begin{enumerate}
      \item We choose $E0$ as our $E'$ and show that $\ssus(E0 \app C) = \gus(E0)$.
      \item (1b) gives $(\ssus E0 \app C) \app \mathit{gu} = \gus(e0 \app c)$.
      \item Using (1a) we rewrite it as $(\ssus E0 \app C) = \gus(e0)$.
      \end{enumerate}
    \item[Case 2:]
      \begin{enumerate}
      \item We choose $E0 \app C$ as our $E'$.
      \item $\ssus(E0 \app C) = \gus(E0 \app C)$ follows from (2b).
      \end{enumerate}
    \item[Case 3:]
      \begin{enumerate}
      \item According to (3a) the configuration $C$ points at a {\tt ret}-instruction.
      \item Let $C'$ be the configuration that follows after executing the {\tt ret} from $C$.
      \item We choose $E0 \app C \app C'$ as our $E'$ and show that $\ssus(E0 \app C) = \gus (E0 \app C \app C')$.
      \item According to (3b) we have $\ssus(E0 \app C) = \gus(E0 \app C) \app \mathit{ssu}$.
      \item According to lemma \ref{lem:gu_after_follows_ssu_after}, $C'$ points at the instruction {\tt ghost\_update ssu}.
      \item Thus we have $\ssus(E0 \app C) = \gus(E0 \app C \app C')$.
      \end{enumerate}
    \item[Case 4:]
      \begin{enumerate}
      \item We choose $E0 \app C$ as our $E'$.
      \item $\ssus(E0 \app C) = \gus(E0 \app C)$ follows from (4b).
      \end{enumerate}
    \item[Case 5:]
      \begin{enumerate}
      \item We choose $E0 \app C$ as our $E'$.
      \item $\ssus(E0 \app C) = \gus(E0 \app C)$ follows from (4b).
      \end{enumerate}
    \end{enumerate}
  \end{enumerate}
\end{proof}

\sep

\newpage
\section{SST\_SUBSET\_GVS}

\begin{lemma}[EXISTS\_SSUS\_PREFIX]\label{lem:exists_ssus_prefix}
For all executions $E$, if $\sst$ is the security state trace of the sec-state updates $\ssus(E)$ and $\ss \in \sst$, then there exists a prefix $\ssus_\pref$ of $\ssus(E)$ such that $\ssus_\pref$ takes the initial sec-state to $\ss$.
\end{lemma}
\begin{proof}
I'm guessing: By induction on the length of $\ssus$. Try it out in Coq.
\end{proof}

\sep

\begin{lemma}[GUS\_PREF\_IMPLIES\_EXEC\_PREF]\label{lem:gus_pref_implies_exec_pref}
For all executions $E$, If $\gus_\pref$ is a prefix of $\gus(E)$, then there exists a prefix $E_\pref$, such that $\gus_\pref = \gus(E_\pref)$.
\end{lemma}
\begin{proof}
Inversion on the prefix-predicate + induction. I have a Coq-proof outline for this.
\end{proof}

\sep

\begin{lemma}[SS\_AFTER\_EQUALS\_GV\_LAST]\label{lem:ss_after_equals_gv_last}
For all executions on the form $E$, security update lists $\ssus$ % Not neccesarily ssus of E!
and security states $\ss$, if $\ssus$ takes the initial security state to $\ss$, and $\ssus = gus(E)$, then $E$ is on the form $E' \app (\ss, \_, \_)$.
\end{lemma}
\begin{proof}
I'm guessing: Induction on $\ssus$. Try it out in Coq.

EDIT: Seems to work out!
\end{proof}

\sep

\begin{lemma}[SSUS\_EQ\_GUS\_IMPLIES\_SST\_SUBSET\_GVS]\label{lem:ssus_eq_gus_implies_sst_subset_gvs}
For all executions $E$ and $E'$ of a program, such that $\ssus(E) = \gus(E')$, then $\sst(E) \subseteq \{ \gv \mid (gv, \_, \_) \in E'\}$.
\end{lemma}

\begin{proof}
\begin{enumerate}
\item Pick an arbitrary $\ss \in \sst(E)$ (and then show that $(\ss, \_, \_) \in E'$)
\item Let $\pref$ denote the prefix of $\ssus(E)$ which takes the initial sec-state to $ss$. (Such prefix exists due to lemma \ref{lem:exists_ssus_prefix}).
\item Since $\ssus(E) = \gus(E')$, $\pref$ is also a prefix of $\gus(E')$.
\item By lemma \ref{lem:gus_pref_implies_exec_pref}, there exists a prefix $E'_\pref$ of $E'$, such that $\gus(E'_\pref) = \pref$.
\item Since $\pref$ takes the initial sec-state to $\ss$, lemma \ref{lem:ss_after_equals_gv_last} says that $E'_\pref$ ends with $(\ss, \_, \_)$.
\item Since $(\ss, \_, \_) \in E'_\pref$ we have $(\ss, \_, \_) \in E'$
\end{enumerate}
\end{proof}

\sep

\begin{lemma}[SST\_SUBSET\_GVS]
For all ghost inlined programs $P^g = {\cal I}^g(P, \cal C)$, executions $E^g$ of $P^g$, let $\sst$ denote the security automaton trace (under $\cal C$), then there exists an execution $E'^g$ of $P^g$ such that $\sst \subseteq \{ \gv \mid (gv, \_, \_) \in E'^g \}$.
\end{lemma}

\begin{proof}
\begin{enumerate}
\item Let $E'^g$ be the extended execution of $E^g$ such that $\gus(E'^g) = \ssus(E^g)$. (Such execution exists by lemma \ref{lem:exists_exec_with_eq_gus}.
\item By lemma \ref{lem:ssus_eq_gus_implies_sst_subset_gvs}, $\sst \subseteq \{ \gv \mid (gv, \_, \_) \in E'^g \}$.
\end{enumerate}
\end{proof}

\sep

\end{document}


\documentclass[11pt,a4paper]{report}
\usepackage{amsmath,amsfonts,amssymb,amsthm,epsfig,epstopdf,titling,url,array}
\usepackage{enumitem}
\usepackage{changepage}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{listings}
\usepackage{color}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{hyperref}
\hypersetup{
	colorlinks=true,
	linkcolor=blue,
	filecolor=magenta,
	urlcolor=cyan,
}
\urlstyle{same}
\theoremstyle{plain}
\newtheorem{thm}{Theorem}[section]
\newtheorem{lem}[thm]{Lemma}
\newtheorem{prop}[thm]{Proposition}
\newtheorem*{cor}{Corollary}
\theoremstyle{definition}
\newtheorem{defn}{Definition}[section]
\newtheorem{conj}{Conjecture}[section]
\newtheorem{exmp}{Example}[section]
\newtheorem{exercise}{Exercise}[section]
\theoremstyle{remark}
\newtheorem*{rem}{Remark}
\newtheorem*{note}{Note}
\def\changemargin#1#2{\list{}{\rightmargin#2\leftmargin#1}\item[]}
\let\endchangemargin=\endlist

\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}

\lstdefinestyle{mystyle}{
	backgroundcolor=\color{backcolour},
	commentstyle=\color{codegreen},
	keywordstyle=\color{magenta},
	numberstyle=\tiny\color{codegray},
	stringstyle=\color{codepurple},
	basicstyle=\footnotesize,
	breakatwhitespace=false,
	breaklines=true,
	captionpos=b,
	keepspaces=true,
	numbers=left,
	numbersep=5pt,
	showspaces=false,
	showstringspaces=false,
	showtabs=false,
	tabsize=2
}

\lstset{style=mystyle}
\begin{document}

\section*{Problem}
Find the smallest number that is divisible by all of the digits 2, ..., 9.
\\\\
This problem is from \href{https://www.morningbrew.com} {The Morning Brew}.

\section*{Solution} Our strategy will be to keep track of the required
prime factors and their highest exponents.  Suppose that $x$ is divisible by
each number in \\ $D = \{2, ..., 9\}.$  Then for each $d$ in $D$, $x$'s prime
factorization must include all of the primes in $d$'s prime factorization with
equal or greater exponents.  \\ Starting with 2, that adds $2^1$ to the required
primes in $x$.  Next is 3, where we add a similar term for 3.  Continuing, 4 ups
the required exponent on 2 to 2, 5 adds $5^1$, 6 is covered (already have 2 and
3 required), 7 adds $7^1$, 8 ups the exponent on 2 to 3,  and 9 ups the exponent
on 3 to 2.  Putting this all together, to be divisible by each of the digits,
$x$ must be at least $$ 2^{3}3^{2}5^{1}7^{1} = 2520 $$

\end{document}


\documentclass[english,10pt,a4paper,twocolumn,colorscheme=green]{orarticle}
\usepackage{hyperref}
\usepackage{bbm}

\usepackage{listings}
\lstset{
	language=C++,
	commentstyle=\color{darkgreen}\textit,
	frame=single,
	tabsize=4,
	keywordstyle=\color{blue}\bfseries,
	stringstyle=\color[gray]{0.4},
	basicstyle=\lst@ifdisplaystyle\scriptsize\else\ttfamily\fi,
	morekeywords={uint16, uint32, Vec2, Vec3}
}

\begin{document}

	\titlehead{\begin{minipage}{0.5\linewidth}
		\begin{center}
		Otto-von-Guericke-University Magdeburg\\
		Faculty of Computer Science\\
		ISG - Computational Visualistics
		\end{center}
	\end{minipage}}
%	\subject{File Format Documentation}
	\title{Fitting Minimal Bounding Ellipsoids}
	\authors{Johannes Jendersie}
	\abstract{Abstract}{
		Building a bounding volume hierarchy with axis aligned ellipsoids can be done with different fitting strategies. One could search the Löwner-John ellipsoid for the convex hull of the geometry in a subtree. This is complicated and relatively slow depending on the build method. I purpose an approximative and iterative solution which uses two operations: fit an ellipsoid with known center and a single point and fit an ellipsoid to two given ellipsoids.
	}
	\thispagestyle{empty}
	\maketitle

	% ************************************************************************ %
	\section{Minimal Ellipsoid}
	There are multiple representations of the ellipsoid: implicit (also in matrix form) and parametric.
	\begin{align}
	\left\lVert\frac{\mathbbm{x}-\mathbbm{c}}{\mathbbm{a}}\right\rVert &= 1\\
	(\mathbbm{x}-\mathbbm{c})^T diag(\mathbbm{a}) (\mathbbm{x}-\mathbbm{c}) &= 1\\
	\mathbbm{a}\cdot\begin{pmatrix}
		\cos(u) \cos(v)\\
		\cos(u) \sin(v)\\
		\sin(u)
	\end{pmatrix} &= \mathbbm{x}
	\end{align}
	where $\mathbbm{c}$ is the center and $\mathbbm{a}$ is the vector of radii. Each vector operation inclusive the division is component-wise.

	The minimal ellipsoid is usually defined as the ellipsoid of minimal volume:
	\begin{align}
		V = \frac{4}{3}\pi \prod_{i=1}^d \mathbbm{a}_i
	\end{align}
	In degenerated cases where some of the radii are 0, which may happen if fitting ellipsoids to axis aligned triangles, the volume becomes 0 too. In that case the minimal ellipses (smallest) area is searched.

	% ************************************************************************ %
	\section{Point and Center Known}

	Finding a minimal ellipsoid with a known center $\mathbbm{a}$ to contain a point is equivalent to find the minimal ellipsoid to a symmetric box with that point as one corner and the same center.

	\begin{figure}[h]
	\includegraphics[width=\linewidth]{img/ellipsoidpointfitting}
	\caption{Finding the best ellipsoid for a point is equivalent to that of a symmetric box. On the right side the space is rescaled such that the ellipsoid becomes a sphere.}
	\end{figure}
	The optimal bounding sphere for a cube with side length $s$ has $r = s\frac{\sqrt{3}}{2}$. The $\sqrt{3}$ comes from the euclidean length from the center to one corner $\sqrt{3\cdot\frac{s^2}{2^2}}$. In the general case for $d$ dimensions the factor is $\frac{\sqrt{d}}{2}$. In case of an ellipsoid the only difference is that the space must be scaled first which happens when use a vector of three side lengths $2\lvert\mathbbm{x}-\mathbbm{c}\rvert$ instead of $s$. In the degenerated case where one or more of that lengths are 0 the factor shrinks.
	\begin{align}
		\mathbbm{a} = \lvert\mathbbm{x}-\mathbbm{c}\rvert\cdot\sqrt{\sum_{i=1}^d (\mathbbm{x}_i-\mathbbm{c}_i) \neq 0}
	\end{align}

	\textbf{Proof} There are infinite many ellipsoid through the given point, which can be computed as:
	\begin{align*}
		\left\{\mathbbm{a}_0, \mathbbm{a}_1, \sqrt{\frac{(\mathbbm{x}_2-\mathbbm{c}_2)^2}{1-\frac{\mathbbm{x}_0-\mathbbm{c}_0}{\mathbbm{a}_0}^2 - \frac{\mathbbm{x}_1-\mathbbm{c}_1}{\mathbbm{a}_1}^2}}\right\}
	\end{align*}
	where $\mathbbm{a}_0, \mathbbm{a}_1$ can be chosen freely.
	W.l.o.g. an ellipsoid with $\mathbbm{a}_0 - \epsilon$ which again contains the point $\mathbbm{x}$ has a larger volume than the proposed solution.

	\begin{align*}
		V(\mathbbm{a}) &< V(\mathbbm{a}')\\
		s_i &= \lvert \mathbbm{x}_i-\mathbbm{c}_i \rvert\\
		\mathbbm{a}_0 \cdot\mathbbm{a}_1 \cdot\mathbbm{a}_2 &< (\mathbbm{a}_0-\epsilon) \cdot\mathbbm{a}_1 \cdot\sqrt{\frac{s_2^2}{1-\frac{s_0}{\mathbbm{a}_0-\epsilon}^2 - \frac{s_1}{\mathbbm{a}_1}^2}}\\
		3\cdot s_0\cdot s_2 &< (\mathbbm{a}_0-\epsilon) \cdot\sqrt{\frac{s_2^2}{1-\frac{s_0}{\mathbbm{a}_0-\epsilon}^2 - \frac{1}{\sqrt{3}}}}\\
		9\cdot s_0^2 &< (\sqrt{3}\cdot s_0-\epsilon)^2 \cdot\frac{1}{1-\frac{s_0}{\sqrt{3}s_0-\epsilon}^2 - \frac{1}{\sqrt{3}}}\\
		9-\frac{9}{\sqrt{3}}\cdot s_0^2 &- 9 \cdot \frac{s_0^4}{(\sqrt{3}s_0-\epsilon)^2} < (\sqrt{3}\cdot s_0-\epsilon)^2\\
	%	(\sqrt{3}\cdot s_0-\epsilon)^2 &\cdot (1-\frac{1}{\sqrt{3}}\cdot s_0^2) - s_0^4 < \frac{(\sqrt{3}\cdot s_0-\epsilon)^4}{9}\\
	\end{align*}

	Well I don't see how to simplify this... but testing it seemed ok.

\end{document}


\documentclass[a4paper,12pt]{article} % This defines the style of your paper

\usepackage[top = 2.5cm, bottom = 2.5cm, left = 2.5cm, right = 2.5cm]{geometry}
\usepackage[utf8]{inputenc} %utf8 % lettere accentate da tastiera
\usepackage[english]{babel} % lingua del documento
\usepackage[T1]{fontenc} % codifica dei font

\usepackage{multirow} % Multirow is for tables with multiple rows within one
%cell.
\usepackage{booktabs} % For even nicer tables.

\usepackage{graphicx}

\usepackage{setspace}
\setlength{\parindent}{0in}

\usepackage{float}

\usepackage{fancyhdr}

\usepackage{caption}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage{color}

\usepackage[hidelinks]{hyperref}
\usepackage{csquotes}
\usepackage{subfigure}

\newcommand{\footlabel}[2]{%
	\addtocounter{footnote}{1}%
	\footnotetext[\thefootnote]{%
		\addtocounter{footnote}{-1}%
		\refstepcounter{footnote}\label{#1}%
		#2%
	}%
	$^{\ref{#1}}$%
}

\newcommand{\footref}[1]{%
	$^{\ref{#1}}$%
}

\pagestyle{fancy}

\setlength\parindent{24pt}

\fancyhf{}

\lhead{\footnotesize Deep Learning Lab: Assignment 2}

\rhead{\footnotesize Giorgia Adorni}

\cfoot{\footnotesize \thepage}

\begin{document}

	\thispagestyle{empty}
	\noindent{
	\begin{tabular}{p{15cm}}
		{\large \bf Deep Learning Lab} \\
		Università della Svizzera Italiana \\ Faculty of Informatics \\ \today  \\
		\hline
		\\
	\end{tabular}

	\vspace*{0.3cm}

	\begin{center}
		{\Large \bf Assignment 2: Convolutional Neural Network}
		\vspace{2mm}

		{\bf Giorgia Adorni (giorgia.adorni@usi.ch)}

	\end{center}
}
	\vspace{0.4cm}

	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

	\section{Introduction}
	The scope of this project is to implement a convolutional neural network to
	classify the images in the CIFAR-10 dataset.

	First of all, the original training set has been shuffled and divided into
	train and validation sets, with $49000$ and $1000$ images respectively. A
	seed has been used to reproduce the same sample split and use them in the
	different models. Instead, the test set provided contains $10000$ images.

	A certain preprocessing has been applied to the data. The pixel values of
	each sample, initially comprised between 0 and 255, have been rescaled
	between 0 and 1. To represent the class assignments, which were integers
	between 0 and 9, three binary assignment matrices have been created, one
	for each set of data.

	The architecture of the convolutional neural network follows the
	instructions provided, as well as the hyper-parameter values for the models
	presented in Sections \ref{section:model0} and \ref{section:dropout}.
	In the training phase, mini-es were used. In particular, each
	epoch splits the training set in different samples of data.

	All the models were implemented using \texttt{TensorFlow} and trained on an
	NVIDIA Tesla V100-PCIE-16GB GPU.

	Note: Since mini-batches are used during the training of the models, the
	loss and the accuracy of all samples are averaged, obtaining a less noisy
	estimate.

	\section{Performance of the initial model}
	\label{section:model0}
	In Table \ref{tab:model0} is summarised the architecture of the network
	used in the first experiment.

	\begin{figure}[H]
		\centering

		\begin{tabular}{cccccccc}
		\toprule
		\textbf{conv1} & \textbf{conv2} & \textbf{mpool1} & \textbf{conv3} &
		\textbf{conv4} & \textbf{mpool1} &   \textbf{fc} &
		\textbf{softmax} \\
		\midrule
		3$\times$3,  32 & 3$\times$3, 32 & 2$\times$2 &3$\times$3, 64 &
		3$\times$3, 64  & 2$\times$2  & 512 & 10\\
		s. 2$\times$2 &   s. 2$\times$2 &   s. 1$\times$1 & s. 1$\times$1  & s.
		2$\times$2 & s. 2$\times$2 && \\
		p. same & p. same & p. same  & p. same & p. same & p. same &&\\
		\bottomrule
		\end{tabular}
		\captionof{table}{Network architecture}
		\label{tab:model0}
	\end{figure}

	The model is trained for $50$ epochs and \texttt{Adam} is used as
	optimiser with learning rate $0,001$.
	As loss function, the Softmax Cross Entropy with Logits is used since the
	model is a multi-class classifier. Moreover, once per epoch, is documented
	the classification accuracy on both the train and validation set.
	The performance is shown in Figure \ref{fig:model0-performance}.

	\begin{figure}[H]
		\begin{minipage}[c]{.49\textwidth}
			\centering
			\includegraphics[width=\linewidth]{../src/out/img/1-Accuracy.png}
			\caption*{(a)}
		\end{minipage}
		~
		\begin{minipage}[c]{.49\textwidth}
			\centering
			\includegraphics[width=\linewidth]{../src/out/img/1-Loss.png}
			\caption*{(b)}
		\end{minipage}
		\caption{Training and validation curves in the initial model}
		\label{fig:model0-performance}
	\end{figure}

	As can be seen in Figure (a), the training accuracy rapidly grows up to
	$100\%$, while the validation accuracy remains stable at $70\%$.
	The final accuracy on the valid set is $70.30\%$.
	For what concerns the loss, it is clear that the model overfits the data.
	The training loss is close to $0$ while the validation one
	diverges.

	\begin{table}[H]
		\centering
		\begin{tabular}{l@{\hspace{.5cm}}cc|cc|c}
			\toprule
			& \multicolumn{2}{c}{\textbf{Accuracy}} &
			\multicolumn{2}{c}{\textbf{Loss}} & \multirow{2}*{\textbf{Train
					Time}} \\
			& Train & Validation
			& Train & Validation	& 						 		\\
			\midrule
			\textbf{Model 1} & 98.99\% & 70.30\%  & 0.05 & 4.20 & 406 sec \\

			\bottomrule
		\end{tabular}
		\captionof{table}{Model performances}
		\label{tab:performace-m1}
	\end{table}

	For this reason, in Section \ref{section:dropout} is presented a new model
	that has the aim of improving these results.

	\section{Regularisation of the model with dropout}
	\label{section:dropout}
	The model proposed in this section involve the use of a model
	regularisation technique, that is the addition of a dropout layer after
	each max-pooling and fully-connected layer. In particular, during the
	training phase, the probability to keep each neuron is set to $0.5$,
	while in the validation set should be $1$.

	The architecture of the new network is presented in Table \ref{tab:model1}.

	\begin{figure}[H]
		\centering

		\begin{tabular}{cccccccc}
			\toprule
			\textbf{conv1} & \textbf{conv2} & \textbf{mpool1} &
			\textbf{conv3} &
			\textbf{conv4} & \textbf{mpool2} &   \textbf{fc} &
			\textbf{softmax} \\
			\midrule
			3$\times$3,  32 & 3$\times$3, 32 & 2$\times$2 &3$\times$3, 64 &
			3$\times$3, 64  & 2$\times$2  & 512 & 10\\
			s. 2$\times$2 &   s. 2$\times$2 &   s. 1$\times$1 & s. 1$\times$1
			& s.
			2$\times$2 & s. 2$\times$2 && \\
			p. same & p. same & p. same  & p. same & p. same & p. same &&\\
			 &  & dropout  &  &  & dropout & dropout & \\
			\bottomrule
		\end{tabular}
		\captionof{table}{Network architecture}
		\label{tab:model1}
	\end{figure}

	The actual performance is shown in Figure \ref{fig:model1-performance}.
	The performances legitimately improved as expected after the application of
	the dropout.

	Even if the performance on the training worsen respect to the previous
	model, since the regularisation is applied only on this set, the
	performance on the validation increased by $9.90\%$. Now its value is
	$80.20\%$, significantly better compared to the previous model.

	\begin{figure}[htb]
		\begin{minipage}[c]{.49\textwidth}
			\centering
			\includegraphics[width=\linewidth]{../src/out/img/2-Accuracy.png}
			\caption*{(a)}
		\end{minipage}
		~
		\begin{minipage}[c]{.49\textwidth}
			\centering
			\includegraphics[width=\linewidth]{../src/out/img/2-Loss.png}
			\caption*{(b)}
		\end{minipage}
		\caption{Training and validation curves in the regularised model}
		\label{fig:model1-performance}
	\end{figure}

	Instead, observing the loss curves, after the $30\mathrm{th}$ epochs it is
	possible to see some signs of overfitting.

	\begin{table}[htb]
		\centering
		\begin{tabular}{l@{\hspace{.5cm}}cc|cc|c}
			\toprule
			& \multicolumn{2}{c}{\textbf{Accuracy}} &
			\multicolumn{2}{c}{\textbf{Loss}} & \multirow{2}*{\textbf{Train
					Time}} \\
			& Train & Validation & Train & Validation & \\
			\midrule
			\textbf{Model 2} & 79.32\% & 80.20\%  & 0.60 & 0.61 & 415 sec \\
			\bottomrule
		\end{tabular}
		\captionof{table}{Model performances}
		\label{tab:performace-m2}
	\end{table}

	In the following section will be attempted some additional experiments by
	modifying the model's hyperparameters.

	\section{Hyperparameter settings}
	\label{section:hyperparam}

	In this section will be discussed $6$ different additional configurations
	for the hyperparameters of the network, to improve the validation accuracy.
	In particular, will be documented the performances according to the
	modification of the following hyperparameters: learning rate, mini-batch
	size, dropout and number of epochs. In this analysis, only the models with
	the best performances will be included. In Table \ref{tab:param1} are
	documented the different hyperparameter set for the models.

	\begin{table}[htb]
		\centering
		\begin{tabular}{l@{\hspace{.5cm}}ccc}
			\toprule
			& \textbf{learning rate} & \textbf{batch size} & \textbf{dropout}
			\\
			\midrule
			\textbf{Model 1}  & {1e-3} & {32}  &  -  \\
			\textbf{Model 2}  & {1e-3} & {32}  & 0.5 \\
			\textbf{Model 3}  & {1e-4} & {32}  & 0.5 \\
			\textbf{Model 4}  & {1e-3} & {128} & 0.6 \\
			\textbf{Model 5}  & {1e-3} & {128} & 0.5 \\
			\textbf{Model 6}  & {1e-4} & {128} & 0.5 \\
			%\textbf{Model 7}  & {1e-4} & {256} & 0.5 \\
			%\textbf{Model 8}  & {1e-3} & {256} & 0.5 \\
			\textbf{Model 9}  & {1e-3} & {128} & 0.25 - 0.5\\
			\bottomrule
		\end{tabular}
		\captionof{table}{Model hyperparameters}
		\label{tab:param1}
	\end{table}

	All the models are trained for 50 epochs.
	The other configurations tested, also those that include the modification
	of other hyperparameters, for example using the Gradient Descent as
	optimiser or adding the decay rate and other parameters to Adam optimizer
	will not be presented in this report due to their mediocre results.
	\newline

	The first experiment performed simply consists in the reduction of the
	learning rate from $0.001$ down to $0.0001$.

	In Figure \ref{fig:model3-performance} are visualised the performances of
	the model. The accuracy, which now measures $80.80\%$, increased only by
	$0.60\%$ compared to the previous model. Furthermore, it is visible a
	distance between the training and validation curves.
	The loss curve rapidly decreases towards $0$, and there are no signs of
	overfitting.

	\begin{figure}[htb]
		\begin{minipage}[c]{.49\textwidth}
			\centering
			\includegraphics[width=\linewidth]{../src/out/img/3-Accuracy.png}
			\caption*{(a)}
		\end{minipage}
		~
		\begin{minipage}[c]{.49\textwidth}
			\centering
			\includegraphics[width=\linewidth]{../src/out/img/3-Loss.png}
			\caption*{(b)}
		\end{minipage}
		\caption{Training and validation curves in model 3}
		\label{fig:model3-performance}
	\end{figure}

	In Section \ref{subsection:epochs} will be discussed a further
	modification to this model that consists of increasing the number of
	epochs, since the trend of the curve seems to be growing.
	\newline

	In Model 4, the training has been carried out using $128$ samples
	for batch instead of $32$ and the learning rate has been restored to its
	original value of $0.001$.
	Furthermore, the dropout value is updated: the probability to keep each
	neuron is increased to $0.6$.

	\begin{figure}[htb]
		\begin{minipage}[c]{.49\textwidth}
			\centering
			\includegraphics[width=\linewidth]{../src/out/img/4-Accuracy.png}
			\caption*{(a)}
		\end{minipage}
		~
		\begin{minipage}[c]{.49\textwidth}
			\centering
			\includegraphics[width=\linewidth]{../src/out/img/4-Loss.png}
			\caption*{(b)}
		\end{minipage}
		\caption{Training and validation curves in model 4}
		\label{fig:model4-performance}
	\end{figure}

	In this case, the performance is very similar to the previous experiment,
	even if slightly worse. The validation accuracy is reduced to
	$79.4\%$ despite the validation loss is still the same, as can be seen in
	Figure \ref*{fig:model5-performance}.
	\newline

	In Model 5, the number of samples for batches and the learning rate are
	kept
	equal to the previous experiment, while the dropout has been restored to
	its original value.
	The current validation accuracy has begun to rise again, reaching $81.4\%$
	and improving the performance of model 3. However, as it can be seen in
	Figure \ref{fig:model4-performance}, the validation curve, initially
	promising, after the $30\mathrm{th}$ epoch it falls below the training
	curve.

	\begin{figure}[htb]
		\begin{minipage}[c]{.49\textwidth}
			\centering
			\includegraphics[width=\linewidth]{../src/out/img/5-Accuracy.png}
			\caption*{(a)}
		\end{minipage}
		~
		\begin{minipage}[c]{.49\textwidth}
			\centering
			\includegraphics[width=\linewidth]{../src/out/img/5-Loss.png}
			\caption*{(b)}
		\end{minipage}
		\caption{Training and validation curves in model 5}
		\label{fig:model5-performance}
	\end{figure}

	For the next two experiments, it was decided to set the learning rate to
	the previous value of $0.0001$ and restore the dropout to his original
	value of $0.5$. Instead, the number of examples in the batch for model 6
	has been kept equal to the previous experiments, that is $128$ and increase
	up to $256$ for model 7.

	Unfortunately, in both cases, the performance has deteriorated compared to
	all the previous performances. The first model has a validation accuracy of
	$73.50\%$, while the second fell to $68.40\%$, that is the worst result
	measured so far.
	\newline

	For this reason, it was decided to retrain the model keeping the learning
	rate value at $0.001$ and the number of examples per batch at $256$.
	As shown in Figure \ref{fig:model8-performance}, the final validation
	accuracy is $80.00\%$, which is a nice result.
	\newline

	\begin{figure}[htb]
		\begin{minipage}[c]{.49\textwidth}
			\centering
			\includegraphics[width=\linewidth]{../src/out/img/8-Accuracy.png}
			\caption*{(a)}
		\end{minipage}
		~
		\begin{minipage}[c]{.49\textwidth}
			\centering
			\includegraphics[width=\linewidth]{../src/out/img/8-Loss.png}
			\caption*{(b)}
		\end{minipage}
		\caption{Training and validation curves in model 8}
		\label{fig:model8-performance}
	\end{figure}

	In the last experiment, a further attempted modification foresees in the
	use
	of different dropouts based on its application after a max-pooling or a
	fully connected layer. In particular, the rate of the dropout is set to
	$0.25$ after the max-pooling layers and kept to $0.5$ after the fully
	connected layer.

	\begin{figure}[htb]
		\begin{minipage}[c]{.49\textwidth}
			\centering
			\includegraphics[width=\linewidth]{../src/out/img/9-Accuracy.png}
			\caption*{(a)}
		\end{minipage}
		~
		\begin{minipage}[c]{.49\textwidth}
			\centering
			\includegraphics[width=\linewidth]{../src/out/img/9-Loss.png}
			\caption*{(b)}
		\end{minipage}
		\caption{Training and validation curves in model 9}
		\label{fig:model9-performance}
	\end{figure}
	The performances of the model are shown in Figure
	\ref{fig:model9-performance}. The validation accuracy obtained, that is
	$80.5\%$, is among the best three.
	\newline

	In Table \ref{tab:performace1} are summarised the performance of the
	presented models. Among the documented model, the best achieved are model
	5, model 3 and model 2.
	In the following section will be presented the performances of some
	models after the increase of training epochs.

	\begin{table}[htb]
		\centering
		\begin{tabular}{l@{\hspace{.5cm}}cc|cc|c}
			\toprule
			& \multicolumn{2}{c}{\textbf{Accuracy}} &
			\multicolumn{2}{c}{\textbf{Loss}} & \multirow{2}*{\textbf{Train
					Time}} \\
			& Train & Validation
			& Train & Validation	& 						 		\\
			\midrule
			\textbf{Model 1} & 98.99\% & 70.30\%  & 0.05 & 4.20 & 406 sec \\
			\textbf{Model 2} & 79.32\% & {80.20\%}  & 0.60 & 0.61 & 415
			sec \\
			\textbf{Model 3} & 77.70\% & \textbf{80.80\%}  & 0.63 & 0.61 & 445
			sec \\
			\textbf{Model 4} & 76.75\% & 79.40\%  & 0.65 & 0.61 & 148 sec \\
			\textbf{Model 5} & 84.39\% & \textbf{81.40\%}  & 0.44 & 0.57 & 185
			sec \\
			\textbf{Model 6} & 71.03\% & 73.50\%  & 0.82 & 0.77 & 149 sec \\
			\textbf{Model 7} & 65.37\% & 68.40\%  & 0.98 & 0.88 & 119 sec \\
			\textbf{Model 8} & 83.60\% & {80.00\%}  & 0.46 & 0.59 & 120
			sec \\
			\textbf{Model 9} & 93.91\% & \textbf{80.50\%}  & 0.18 & 0.73 & 155
			sec \\
			\bottomrule
		\end{tabular}
		\captionof{table}{Model performances}
		\label{tab:performace1}
	\end{table}

	\subsection{Modification of the number of hidden units}
	\label{subsection:hidden units}

	In this section will be discussed modification to the current best model,
	which is the model 5, that consists in an increase of the number of
	hidden units of the fully connected layer from $512$ up to $1024$.

	Since the learning curve of the network, shown in the previous section,
	seems to be underfitting, increasing the complexity of the network should
	be a good idea. The new performances are shown in Figure
	\ref{fig:model10-performance}.

	\begin{figure}[htb]
		\begin{minipage}[c]{.49\textwidth}
			\centering
			\includegraphics[width=\linewidth]{../src/out/img/10-Accuracy.png}
			\caption*{(a)}
		\end{minipage}
		~
		\begin{minipage}[c]{.49\textwidth}
			\centering
			\includegraphics[width=\linewidth]{../src/out/img/10-Loss.png}
			\caption*{(b)}
		\end{minipage}
		\caption{Training and validation curves in model 10}
		\label{fig:model10-performance}
	\end{figure}

	The previous validation accuracy, that was $81.40\%$, increases by
	$0.90\%$, reaching $82.3\%$.

	\begin{table}[htb]
		\centering
		\begin{tabular}{l@{\hspace{.5cm}}cc|cc|c}
			\toprule
			& \multicolumn{2}{c}{\textbf{Accuracy}} &
			\multicolumn{2}{c}{\textbf{Loss}} & \multirow{2}*{\textbf{Train
					Time}} \\
			& Train & Validation
			& Train & Validation	& 						 		\\
			\midrule
			\textbf{Model 10} & 87.47\% & \textbf{82.30\%}  & 0.35 & 0.54 & 161
			sec \\
			\bottomrule
		\end{tabular}
		\captionof{table}{Model 10 performances}
		\label{tab:performace10}
	\end{table}

	Further modifications to this model will be discussed in Section
	\ref{subsection:batchnorm}.

	\subsection{Increasing the number of epochs}
	\label{subsection:epochs}
	In this section are analysed the performances of the previous models
	trained for a greater number of epochs.

	These experiments have been carried out only on models 3, 4, 5, 8 and 9
	since they shown better accuracy values.

	The models are trained on GPU which allows training without worsening the
	time too much.
	\newline

	The first experiment were performed on model 3, which curves are shown in
	Figure \ref{fig:model3b-performance}.

	\begin{figure}[htb]
		\begin{minipage}[c]{.49\textwidth}
			\centering
			\includegraphics[width=\linewidth]{../src/out/img/3b-Accuracy.png}
			\caption*{(a)}
		\end{minipage}
		~
		\begin{minipage}[c]{.49\textwidth}
			\centering
			\includegraphics[width=\linewidth]{../src/out/img/3b-Loss.png}
			\caption*{(b)}
		\end{minipage}
		\caption{Training and validation curves in model 3b}
		\label{fig:model3b-performance}
	\end{figure}

 	Compared to the previous, this model has improved the validation accuracy
 	of $4.9\%$, reaching $85.7\%$, which is the highest result achieved so far.
	\newline

	The following experiments was carried out on model 4.

	\begin{figure}[H]
		\begin{minipage}[c]{.49\textwidth}
			\centering
			\includegraphics[width=\linewidth]{../src/out/img/4b-Accuracy.png}
			\caption*{(a)}
		\end{minipage}
		~
		\begin{minipage}[c]{.49\textwidth}
			\centering
			\includegraphics[width=\linewidth]{../src/out/img/4b-Loss.png}
			\caption*{(b)}
		\end{minipage}
		\caption{Training and validation curves in model 4b}
		\label{fig:model4b-performance}
	\end{figure}

	The results obtained, shown in Figure \ref{fig:model4b-performance}, are
	worse than those obtained from the previous model. The validation accuracy
	is fallen to $81.60\%$. In this case, the validation accuracy obtained
	is more or less the same as the one achieved with only $50$ epochs.
	\newline

	Continuing, the results for the model 5b are shown in Figure
	\ref{fig:model5b-performance}. Despite the performances of the model on
	$50$ epochs were the best, this model obtains exactly the same results,
	hence it is no longer the best among the proposed models.

	\begin{figure}[htb]
		\begin{minipage}[c]{.49\textwidth}
			\centering
			\includegraphics[width=\linewidth]{../src/out/img/5b-Accuracy.png}
			\caption*{(a)}
		\end{minipage}
		~
		\begin{minipage}[c]{.49\textwidth}
			\centering
			\includegraphics[width=\linewidth]{../src/out/img/5b-Loss.png}
			\caption*{(b)}
		\end{minipage}
		\caption{Training and validation curves in model 5b}
		\label{fig:model5b-performance}
	\end{figure}

	In fact, looking at the two curves, it is clearly visible that after few
	epochs the model stabilizes and stops improving its performance.
	This result was predictable since the learning curve seemed to have already
	stabilized before $50$ epochs.
	\newline

	The next experiment was carried out on model 8, which presented a further
	increasing equal to $0.40\%$ of the performance respect to the last
	experiment.

	\begin{figure}[htb]
		\begin{minipage}[c]{.49\textwidth}
			\centering
			\includegraphics[width=\linewidth]{../src/out/img/8b-Accuracy.png}
			\caption*{(a)}
		\end{minipage}
		~
		\begin{minipage}[c]{.49\textwidth}
			\centering
			\includegraphics[width=\linewidth]{../src/out/img/8b-Loss.png}
			\caption*{(b)}
		\end{minipage}
		\caption{Training and validation curves in model 8b}
		\label{fig:model8b-performance}
	\end{figure}

	The situation presented, in particular, the trend of the train and
	validation accuracy curve, shown in Figure \ref{fig:model8b-performance},
	is very similar to the one which occurred with model 5. This is because,
	after a certain epoch, both the model have stopped or significantly slowed
	down the learning.
	\newline

	The last experiment was performed on model 9. Despite the performance is
	better than the latest, it does not reveal a significant increase in
	performance compared to the latest and, above all, respect to model 3b.

	\begin{figure}[htb]
		\begin{minipage}[c]{.49\textwidth}
			\centering
			\includegraphics[width=\linewidth]{../src/out/img/9b-Accuracy.png}
			\caption*{(a)}
		\end{minipage}
		~
		\begin{minipage}[c]{.49\textwidth}
			\centering
			\includegraphics[width=\linewidth]{../src/out/img/9b-Loss.png}
			\caption*{(b)}
		\end{minipage}
		\caption{Training and validation curves in model 9b}
		\label{fig:model9b-performance}
	\end{figure}

	The current validation accuracy is equal to $82.20\%$.
	The curves are displayed in Figure \ref{fig:model9b-performance}.
	\newline

	In Table \ref{tab:performace2} are summarised the performance of the
	presented models. Among these, the bests are the model 3b,
	model 8b and model 9b.

	\begin{table}[H]
		\centering
		\begin{tabular}{l@{\hspace{.5cm}}cc|cc|c}
			\toprule
			& \multicolumn{2}{c}{\textbf{Accuracy}} &
			\multicolumn{2}{c}{\textbf{Loss}} & \multirow{2}*{\textbf{Train
					Time}} \\
			& Train & Validation
			& Train & Validation	& 						 		\\
			\midrule

			\textbf{Model 3} & 77.70\% & {80.80\%}  & 0.63 & 0.61 & 445
			sec \\
			\textbf{Model 3b} & 93.47\% & \textbf{85.70\%}  & 0.18 & 0.55 &
			2974 sec \\
			\textbf{Model 4} & 76.75\% & 79.40\%  & 0.65 & 0.61 & 148 sec \\
			\textbf{Model 4b}  & 84.60\% & \textbf{81.60\%}  & 0.44 & 0.56 &
			1040 sec \\
			\textbf{Model 5} & 84.39\% & {81.40\%}  & 0.44 & 0.57 & 185
			sec \\
			\textbf{Model 5b} & 91.91\% & \textbf{81.40\%}  & 0.24 & 0.63 & 893
			sec \\
			\textbf{Model 8} & 83.60\% & {80.00\%}  & 0.46 & 0.59 & 120
			sec \\
			\textbf{Model 8b} & 93.40\% & \textbf{84.90\%}  & 0.19 & 0.57 & 714
			sec \\
			\textbf{Model 9} & 93.91\% & {80.50\%}  & 0.18 & 0.73 & 155
			sec \\
			\textbf{Model 9b} & 97.55\% & \textbf{82.20\%}  & 0.08 & 0.96 & 931
			sec \\
			\bottomrule
		\end{tabular}
		\captionof{table}{Model performances after increasing the number of
		epochs}
		\label{tab:performace2}
	\end{table}

	The following section will present a further attempt to improve
	performances that consists in the addition of batch normalisation to the
	best models.

	\subsection{Batch normalisation}
	\label{subsection:batchnorm}

	In this section are analysed the performances of the models 3c, 5c, 8c, 9c
	and 10c, after the addition of batch normalisation.
	The main purpose of this change is to improve the performance and the
	stability of the network.
	The transform is introduced immediately before each ReLU non-linearity, in
	order to normalise the layer inputs
	\footlabel{note1}{Ioffe, S. and Szegedy, C., 2015. Batch normalization:
	Accelerating deep network training by reducing internal covariate shift.
	arXiv preprint arXiv:1502.03167.}.
	%\footref{note1}.

	This experiment has been carried out only on five models, which are the
	ones that have obtained better performances, and for 300 epochs.
	\newline

	The first experiment is carried out on model 3b, that is the one with the
	best performances so far.

	\begin{figure}[htb]
		\begin{minipage}[c]{.49\textwidth}
			\centering
			\includegraphics[width=\linewidth]{../src/out/img/3c-Accuracy.png}
			\caption*{(a)}
		\end{minipage}
		~
		\begin{minipage}[c]{.49\textwidth}
			\centering
			\includegraphics[width=\linewidth]{../src/out/img/3c-Loss.png}
			\caption*{(b)}
		\end{minipage}
		\caption{Training and validation curves in model 3c (with batch
			normalisation)}
		\label{fig:model3c-performance}
	\end{figure}

	The learning curves are displayed in Figure \ref{fig:model3c-performance}.
	The validation accuracy is slightly worst compared to that of the model
	without batch normalisation. In fact, the current one is decreased by
	$0.20\%$.
	\newline

	The next experiment is performed on model 5b. In this case, the batch
	normalisation increases the validation accuracy of the previous model of
	$5.3\%$, reaching the new best results of $86.5\%$, as shown in Figure
	\ref{fig:model5c-performance}.

	\begin{figure}[htb]
		\begin{minipage}[c]{.49\textwidth}
			\centering
			\includegraphics[width=\linewidth]{../src/out/img/5c-Accuracy.png}
			\caption*{(a)}
		\end{minipage}
		~
		\begin{minipage}[c]{.49\textwidth}
			\centering
			\includegraphics[width=\linewidth]{../src/out/img/5c-Loss.png}
			\caption*{(b)}
		\end{minipage}
		\caption{Training and validation curves in model 5c (with batch
			normalisation)}
		\label{fig:model5c-performance}
	\end{figure}

	Probably, the reason why model 5c has better performance than 3c is that
	batch normalisation works better with a higher learning rate.
	\newline

	The experiment performed on model 8c has achieved lower result respect to
	model 5c even if further increasing the validation accuracy of $0.20\%$ of
	the model without batch normalisation.
	The performances are shown in Figure \ref{fig:model8c-performance}.
	\newline

	\begin{figure}[htb]
		\begin{minipage}[c]{.49\textwidth}
			\centering
			\includegraphics[width=\linewidth]{../src/out/img/8c-Accuracy.png}
			\caption*{(a)}
		\end{minipage}
		~
		\begin{minipage}[c]{.49\textwidth}
			\centering
			\includegraphics[width=\linewidth]{../src/out/img/8c-Loss.png}
			\caption*{(b)}
		\end{minipage}
		\caption{Training and validation curves in model 8c (with batch
			normalisation)}
		\label{fig:model8c-performance}
	\end{figure}

	The model 9c, which performances are shown in Figure
	\ref{fig:model9c-performance}, performs slightly better compared to the
	previous model. Moreover, the validation accuracy increases by $3.00\%$
	respect to the model without batch normalisation.

	\begin{figure}[H]
		\begin{minipage}[c]{.49\textwidth}
			\centering
			\includegraphics[width=\linewidth]{../src/out/img/9c-Accuracy.png}
			\caption*{(a)}
		\end{minipage}
		~
		\begin{minipage}[c]{.49\textwidth}
			\centering
			\includegraphics[width=\linewidth]{../src/out/img/9c-Loss.png}
			\caption*{(b)}
		\end{minipage}
		\caption{Training and validation curves in model 9c (with batch
			normalisation)}
		\label{fig:model9c-performance}
	\end{figure}

	The last experiment performed has achieved a good result, in particular
	reaching a validation accuracy of $85.70\%$. Despite these results, the
	model does not improve the performance of model 5c.
	The performances are shown in Figure \ref{fig:model10c-performance}.
	\newline

	\begin{figure}[htb]
		\begin{minipage}[c]{.49\textwidth}
			\centering
			\includegraphics[width=\linewidth]{../src/out/img/10c-Accuracy.png}
			\caption*{(a)}
		\end{minipage}
		~
		\begin{minipage}[c]{.49\textwidth}
			\centering
			\includegraphics[width=\linewidth]{../src/out/img/10c-Loss.png}
			\caption*{(b)}
		\end{minipage}
		\caption{Training and validation curves in model 10c (with batch
			normalisation)}
		\label{fig:model10c-performance}
	\end{figure}

	The following table summarises the latest results obtained, highlighting
	model 5c is the best of all the experiments presented.

	\begin{table}[H]
		\centering
		\begin{tabular}{l@{\hspace{.5cm}}cc|cc|c}
			\toprule
			& \multicolumn{2}{c}{\textbf{Accuracy}} &
			\multicolumn{2}{c}{\textbf{Loss}} & \multirow{2}*{\textbf{Train
					Time}} \\
			& Train & Validation
			& Train & Validation	& 						 		\\
			\midrule
			\textbf{Model 3c} & 87.15\% & {85.50\%}  & 0.37 & 0.46 & 4254 sec \\
			\textbf{Model 5c} & 94.20\% & \textbf{86.50\%}  & 0.17 & 0.50 &
			1599 sec \\
			\textbf{Model 8c} & 94.87\% & {85.10\%}  & 0.14 & 0.57 & 1215 sec \\
			\textbf{Model 9c} & 94.74\% & {85.20\%}  & 0.15 & 0.57 & 279
			sec \\
			\textbf{Model 10c} & 96.67\% & {85.70\%}  & 0.10 & 0.60 & 1768
			sec \\
			\bottomrule
		\end{tabular}
		\captionof{table}{Model performances after the batch normalisation}
		\label{tab:performace3}
	\end{table}

	\section{Test set accuracy}
	\label{section:finalmodel}
	The test set accuracy was measured on the model 5c, that was the one which
	presents the best performance. As can be seen in Figure
	\ref{fig:model5c-test-performance} and summarised in Table
	\ref{tab:performace-m5c}, also on the test the performances of the model
	are high. The test accuracy reached $85.36\%$, just $1.14\%$ worse than the
	validation, and the test loss is $0.50$.

	\begin{figure}[htb]
		\begin{minipage}[c]{.49\textwidth}
			\centering
			\includegraphics[width=\linewidth]{../src/out/img/5c-test-Accuracy.png}
			\caption*{(a)}
		\end{minipage}
		~
		\begin{minipage}[c]{.49\textwidth}
			\centering
			\includegraphics[width=\linewidth]{../src/out/img/5c-test-Loss.png}
			\caption*{(b)}
		\end{minipage}
		\caption{Test curves of model 5c}
		\label{fig:model5c-test-performance}
	\end{figure}

	\begin{table}[H]
		\centering
		\begin{tabular}{l@{\hspace{.5cm}}ccc|ccc}
			\toprule
			& \multicolumn{3}{c}{\textbf{Accuracy}} &
			\multicolumn{3}{c}{\textbf{Loss}} \\
			& Train & Validation & Test	& Train & Validation & Test \\
			\midrule
			\textbf{Model 5c} & 94.20\% & {86.50\%} & \textbf{85.36\%} & 1.03 &
			0.47 & \textbf{0.50} \\
			\bottomrule
		\end{tabular}
		\captionof{table}{Test set performances of the chosen model}
		\label{tab:performace-m5c}
	\end{table}

\end{document}