%!TEX root = thesis-kdyoung.tex

% \begin{savequote}%[45mm]
% ---The formulation of the problem is often more essential than its solution
% \qauthor{Albert Einstein}
% \end{savequote}

\chapter{Mixed-Integer Programming Formulation}
\label{chap:mip}
This chapter presents three possible mixed-integer programs to model
the \sua{2}.
We adapt the work of \authciteb{Esmaeilbeigi2016}, in which
three similar formulations were presented.
These programs will provide a benchmark against which
to test our solution methodology presented in the following
chapter.
\citeauthor{Esmaeilbeigi2016} gave possible valid inequalities
which could be added to these formulations, however these
additional constraints are as yet untested by the literature.

One of the research aims we have for our project is to test and compare
our solution methodology against those previously presented
in the literature.
However, due to the limited research that has
been done into the type-2 variant of the \sua{}, there is little
work to compare our approach against.
The only computational results available which are related to the
\sua{2} are by \authciteb{Yolmeh2012}.
For reasons detailed in Section \ref{sec:exp:data}, we chose to test
on a different set of data to that of \citeauthor{Yolmeh2012},
which meant that there is currently no computational
results that can be used to make a direct comparison.
It is possible to roughly judge the quality our results against those of \citeauthor{Yolmeh2012}
by comparing how our solution procedure performs on instances
of similar size.
However, to gain an exact measurement of the quality of different
approaches, we were motivated to implement and test the mixed-integer programs presented
in \citeauthor{Esmaeilbeigi2016}.

In order to capture a richer structure of the \sua{},
Table \ref{tab:mipParams} expands upon the notation that was defined earlier in
Section \ref{sec:intro:probDef}. The work of \citeauthor{Esmaeilbeigi2016}
provides the basis of the notation we formulated for our central thesis.
Our choice of big-$M$ value listed in Table \ref{tab:mipParams}
is calculated as the maximum possible cycle time value,
which is detailed later in Equation \ref{eq:mip:cUB}.

\begin{table}[tbp]
	\def\arraystretch{1.1}
	\centering
	\caption{Notation Summary}
	\vspace{2mm}
	\begin{tabular}{lp{0.8\textwidth}}
		\toprule
		Notation & Definition  \\\midrule\midrule
		$n$ & The number of tasks\\
		$m$ & The number of stations\\
		$V$ & Set of tasks, indexed by $i$, $j$ and $v$ \\
		$K$ & Set of stations, indexed by $k$ \\
		$E (E^*)$ & Set of direct (all) $(i,j)$ precedence relations\\
		$t_i$ & Processing time of task $i$ \\
		$t_{\text{sum}}$ & Sum of all processing times, $\sum_{i\in V}t_i$ \\
		$P_i (P_i^*)$ & Set of direct (all) predecessors of task $i \in V$ \\
		$F_i (F_i^*)$ & Set of direct (all) successors of task $i \in V$ \\
		$\phi_{ij}$ & Setup time of task $j \in V$ when performed immediately after task $i \in V$ in the forward station load\\
		$\beta_{ij}$ & Setup time of task $j \in V$ when performed immediately after task $i \in V$ in the backward station load\\
		$F_i^\phi$ & Set of tasks which may directly follow task $i \in V$ in the forward station load,
					$F_i^\phi=\{\:j\in V- (F_i^*-F_i)-P_i^*-\{i\}\:|\: FS_i\cap FS_j \neq \emptyset\:\}$\\
		$F_i^\beta$ & Set of tasks which may directly follow task $i \in V$ in the backward station load,
					$F_i^\beta=\{\:j\in V- F_i^*\:|\: FS_i\cap FS_j \neq \emptyset\:\}$.\\
		$P_i^\phi$ & Set of tasks which may directly precede task $i \in V$ in the forward station load,
					$P_i^\phi=\{\: j \in V \:|\: i \in F_j^\phi \:\}$ \\
		$P_i^\beta$ & Set of tasks which may directly precede task $i \in V$ in the backward station load,
					$P_i^\beta=\{\: j \in V \:|\: i \in F_j^\beta \:\}$ \\
		$FS_i$ & Set of stations which task $i \in V$ can be feasibly assigned\\
		$FT_k$ & Set of tasks which can be feasibly assigned to station $k \in K$\\
		$\bar{c}(\ul{c})$ & Upper (lower) bound on the cycle time \\
		$M$ & Sufficiently large ``big-$M$'' value, $M=t_{sum} + \beta_{n,1} + \sum_{i=1}^{n-1} \phi_{i,i+1} $ \\
		\bottomrule
	\end{tabular}
	\label{tab:mipParams}
\end{table}

To illustrate how this notation can define the input properties of the
\sua{2}, we return to the simple instance of the problem
in Example \ref{ex:mip:notationIllust} originally presented
in Chapter \ref{chap:intro}.
\begin{example}\label{ex:mip:notationIllust}
	Again, consider the instance of the \sua{2} as defined
	previously in Example \ref{ex:intro:simpleSetup},
	with setup times defined in Tables \ref{tab:intro:forwSetupTimes}
	and \ref{tab:intro:backSetupTimes} (see Chap. \ref{chap:intro}).
	This simple example has input parameters defined as follows,
	\begin{IEEEeqnarray}{rClCrCl}
		V &=& \{1,2,3,4=n\}, &\hspace{4mm}& K &=& \{1,2,3=m\}, \nonumber\\[\eqnv]
		E &=& \big\{(1,2),(2,3),(2,4)\big\}, & & t&=&(6,5,2,9), \nonumber\\[\eqnv]
		F_i^\phi &=& \big(\{2\},\{3,4\},\{4\},\{3\}\big), & & F_i^\beta&=&\big(\{1\},\{1,2\},\{1,2,3,4\},\{1,2,3,4\}\big), \nonumber\\[\eqnv]
		P_i^\phi &=& \big(\emptyset,\{1\},\{2,4\},\{2,3\}\big), & & P_i^\beta&=&\big(\{1,2,3,4\},\{2,3,4\},\{3,4\},\{3,4\}\big), \nonumber\\[\eqnv]
		FS_i &=& \big(K \:|\: i \in V\big) , & & FT_k&=& \big( V \:|\: k \in K\big). \nonumber
	\end{IEEEeqnarray}
	Note: for brevity we have omitted the parameters defined by the transitive closure
	operator, \ie $E^*$, $P_i^*$ and $F_i^*$.
	The big-$M$ value is calculated by
	\[
		M=t_{sum} + \beta_{n,1} + \sum_{i=1}^{n-1} \phi_{i,i+1} =22 + 3 + (0+3+3)= 31. \qed
	\]
\end{example}

\section{First Station-Based Formulation}
\label{sec:mip:fsbf}
The first mixed integer program
of the \sua{2} that we present is a station-based
formulation, and for brevity we denote
this formulation by \fsbf{2}.
The decisions of a station-based formulation are related to the stations
along the assembly line.

The definitions of the decision variables in this model
have been presented in Table \ref{tab:mip:fsbfVars}.
The primary decision is denoted by $c$, which is a continuous
variable indicating the cycle time of the assembly line.
The start times, $s_i$, of each task denote when a task
begins execution relative to the launch time of the station where
task $i$ is performed at.
The assignment decisions between the tasks and stations
are encoded by variables $x_{ik}$.
Binary variables $y_{ij}$ and $z_{ij}$ are true if task $j$
follows task $i$ in the forward and backward station load respectively.
% Binary variable $o_{ik}$ is straightforward so we refer the reader
% to Table \ref{tab:mip:fsbfVars}.
The continuous variable $w_i$ denotes the station number of task $i$.

\begin{table}[tbp]
	\def\arraystretch{1.1}
	\centering
	\caption{Decision variables of FSBF-2}
	\vspace{2mm}
	\begin{tabular}{lp{0.8\textwidth}}
		\toprule
		Variable & Definition \\\midrule\midrule
		$c$ & cycle time of the assembly line, continuous\\
		$s_i$ & start time of task $i\in V$, continuous \\
		$x_{ik}$ & 1 iff task $i \in V$ is assigned to station $k \in FS_i$ \\
		$y_{ij}$ & 1 iff task $i \in V$ is followed directly by task $j \in F_i^\phi$ in the forward station load \\
		$z_{ij}$ & 1 iff task $i \in V$ is the last task completed and task $j \in F_i^\beta$ is the first in the same station \\
		$o_{ik}$ & 1 iff task $i \in V$ is the last task processed on station $k \in FS_i$ \\
		$w_i$ & encodes the number of the station task $i\in V$ is assigned, continuous \\
		\bottomrule
	\end{tabular}
	\label{tab:mip:fsbfVars}
\end{table}

\begin{IEEEeqnarray}{rClCl}
	\IEEEeqnarraymulticol{3}{l}{\text{\fsbf{2}: Min } c} & \hspace{4mm} & \label{eq:mip:fsbf1}\\[\eqnv]
	\text{s.t. } & \hspace{4mm} & \sum_{k\in FS_i} x_{ik} = 1 & & \forall i \in V \label{eq:mip:fsbf3}\\[\eqnv]
	& & \sum_{k\in FS_i} k\cdot x_{ik} = w_i & & \forall i \in V \label{eq:mip:fsbf4}\\[\eqnv]
	& & \sum_{j\in F_i^\phi} y_{ij} + \sum_{j\in F_i^\beta}z_{ij} = 1 & & \forall i \in V \label{eq:mip:fsbf5}\\[\eqnv]
	& & \sum_{i\in P_j^\phi} y_{ij} + \sum_{i\in P_j^\beta}z_{ij} = 1 & & \forall j \in V \label{eq:mip:fsbf6}\\[\eqnv]
	& & o_{ik} \leq x_{ik} & & \forall i \in V,~ k \in FS_i \label{eq:mip:fsbf7}\\[\eqnv]
	& & \sum_{j\in F_i^\beta} z_{ij} \leq \sum_{k\in FS_i} o_{ik} & & \forall i \in V \label{eq:mip:fsbf8}\\[\eqnv]
	& & \sum_{i\in FT_k} o_{ik} \leq 1 & & \forall k \in K \label{eq:mip:fsbf9}\\[\eqnv]
	& & w_j - w_i \leq M\cdot(1-y_{ij}) & & \nonumber\\[\smolEqnv]
	& & \text{ and }~w_i - w_j \leq M\cdot(1-y_{ij}) & & \forall i \in V,~ j \in F_i^\phi \label{eq:mip:fsbf10}\\[\eqnv]
	& & w_j - w_i \leq M\cdot(1-z_{ij}) & & \nonumber\\[\smolEqnv]
	& & \text{ and }~w_i - w_j \leq M\cdot(1-z_{ij}) & & \forall i \in V,~ j \in F_i^\beta \label{eq:mip:fsbf11}\\[\eqnv]
	& & \sum_{i\in FT_k} t_i\cdot x_{ik}\leq c & & \forall k \in K \label{eq:mip:fsbf12}\\[\eqnv]
	& & s_i+t_i+\sum_{j\in F_i^{\beta}} \beta_{ij}\cdot z_{ij}\leq c & & \forall i \in V \label{eq:mip:fsbf13}\\[\eqnv]
	& & \sum_{i\in V}\sum_{j\in F_i^\beta}z_{ij}\geq m & &  \label{eq:mip:fsbf14}\\[\eqnv]
	& & s_i+c\cdot(w_i-w_j)+t_i+\phi_{ij}\cdot y_{ij} \leq s_j & & \forall (i,j) \in E \label{eq:mip:fsbf15}\\[\eqnv]
	& & s_i+(t_i+\phi_{ij})+(c+\phi_{ij})\cdot (y_{ij}-1) \leq s_j & & \forall i \in V,~ j \in F_i^\phi\setminus F_i \label{eq:mip:fsbf16}\\[\eqnv]
	& & \ul{c}\leq c \leq \bar{c} & &  \label{eq:mip:fsbf17}\\[\eqnv]
	& & s_i \geq 0 & & \forall i \in V \label{eq:mip:fsbf18}\\[\eqnv]
	& & x_{ik}\in\{0,1\} & & \forall i \in V,~ k \in FS_i \label{eq:mip:fsbf19}\\[\eqnv]
	& & y_{ij}\in\{0,1\} & & \forall i \in V,~ j \in F_i^\phi \label{eq:mip:fsbf20}\\[\eqnv]
	& & z_{ij}\in\{0,1\} & & \forall i \in V,~ j \in F_i^\beta \label{eq:mip:fsbf21}\\[\eqnv]
	& & o_{ik} \geq 0 & & \forall i \in V,~ k \in FS_i  \label{eq:mip:fsbf22}\\[\eqnv]
	& & w_i \in \{1,2,\ldots,m\} & & \forall i \in V \label{eq:mip:fsbf23}
\end{IEEEeqnarray}

The objective function (\ref{eq:mip:fsbf1}) minimizes the cycle time
(as with all programs presented in this chapter),
(\ref{eq:mip:fsbf3}--\ref{eq:mip:fsbf16}) define the main constraints
on this formulation and the last constraints (\ref{eq:mip:fsbf17}--\ref{eq:mip:fsbf23})
are non-functional defining the domains of our decision variables.
We now detail the purpose of each of the main constraints.

Constraint (\ref{eq:mip:fsbf3}) ensures that each task is assigned exactly one station,
(\ref{eq:mip:fsbf4}) encode the station numbers of the variables $w_i$,
constraints (\ref{eq:mip:fsbf5}) and (\ref{eq:mip:fsbf6}) enforce that
each task has exactly one follower and one predecessor respectively (in either
load direction),
(\ref{eq:mip:fsbf7}) requires that a task can only be the last task of a station
if it has been assigned to that station,
constraint (\ref{eq:mip:fsbf8}) ensures that the number of last tasks is at least
the number of backward setups,
(\ref{eq:mip:fsbf9}) enforces that each station has at maximum one last task.
The four inequalities of constraints (\ref{eq:mip:fsbf10}) and (\ref{eq:mip:fsbf11})
encode a disjunction using a big-$M$ value to ensure that only tasks in the
the same station can be a part of the same sequence.
Knapsack constraint (\ref{eq:mip:fsbf12}) strengthens the formulation by
requiring all tasks be completed by the cycle time,
(\ref{eq:mip:fsbf13}) enforces the backward setup times to bound the cycle time below,
(\ref{eq:mip:fsbf14}) ensures that there are at least $m$ backward setup times,
constraint (\ref{eq:mip:fsbf15}) requires that the precedence relations are satisfied
within stations and between stations using the cycle time as a big-$M$ value
and finally (\ref{eq:mip:fsbf16}) enforces that the forward setup times are satisfied
in the forward station load of each station.

Both a lower and upper bound on the cycle time is required as input for
this model, so these are calculated as follows,
\begin{align}
	\ul{c} &=\max\left\{\: t_{max}',\:\left\lceil \frac{t_{sum}}{m} \right\rceil \:\right\} \label{eq:mip:cLB}\\
	\bar{c} &=t_{sum} + \beta_{n,1} + \sum_{i=1}^{n-1} \phi_{i,i+1}, \label{eq:mip:cUB}
\end{align}
where $t_{max}'=\max_{i\in V}(t_i+\beta_{ii})$.
The theoretical lower bound given by (\ref{eq:mip:cLB})
is the larger value between the maximum possible processing of any singular task
in a station and the equal fractional distribution of all processing times across
all $m$ stations.

We note that the upper bound on the cycle time, given by (\ref{eq:mip:cUB}), need only be the cycle
time of some feasible solution.
The precedence graph is numbered topologically,
thus a precedence-feasible sequencing
of all tasks within a single station is 1,2,\ldots,n.
The upper bound on the cycle time is trivially
the sum of all processing times together with the setup times
resulting from the above sequencing.
As this assignment and sequence is valid, the upper bound is feasible.

The number of variables and constraints are both bounded by $\mathcal{O}(n^2)$.
We note here that this formulation of the \sua{2} requires a number
of disjunctive constraints, which can have an
undesirable impact on the ability of MIP solvers to
handle the problem.
This is due to MIPs needing to formulate disjunctive
constraints with a big-$M$ value which leads to weak
linear relaxations.
Constraints (\ref{eq:mip:fsbf10}), (\ref{eq:mip:fsbf11}),
(\ref{eq:mip:fsbf15}) and (\ref{eq:mip:fsbf16}) all encode disjunctions.

\section{Second Station-Based Formulation}
\label{sec:mip:ssbf}
The next formulation is abbreviated as \ssbf{}
and in this case the number of
variables is bounded by $\mathcal{O}(n^3)$.
The decision variables in this model are similar
to \fsbf{2} except for a few differences which
are presented in Table \ref{sec:mip:ssbf}.

\begin{table}[tbp]
	\def\arraystretch{1.1}
	\centering
	\caption{Decision variables of SSBF-2}
	\vspace{2mm}
	\begin{tabular}{lp{0.8\textwidth}}
		\toprule
		Variable & Definition \\\midrule\midrule
		$g_{ijk}$ & 1 iff $i\in V$ is followed directly by task $j \in F_i^\phi$ in the forward station load on station $k\in FS_i$ \\
		$h_{ijk}$ & 1 iff $i\in V$ is the last task of station $k\in FS_i$ and $j\in F_i^\beta$ is the first \\
		$r_i$ & ordering variable which represents the priority of task $i\in V$ in a station's sequence of tasks\\
		\bottomrule
	\end{tabular}
	\label{tab:mip:ssbfVars}
\end{table}

\begin{IEEEeqnarray}{rClCl}
	\IEEEeqnarraymulticol{3}{l}{\text{\ssbf{2}: Min } c} & \hspace{4mm} & \label{eq:mip:ssbf1}\\[\eqnv]
	\text{s.t. } & \hspace{1mm} & \IEEEeqnarraymulticol{3}{l}{(\ref{eq:mip:fsbf5}),~(\ref{eq:mip:fsbf6}),~(\ref{eq:mip:fsbf17}),~(\ref{eq:mip:fsbf19}),~(\ref{eq:mip:fsbf23})} \nonumber\\[\eqnv]
	& & \sum_{j\in FT_k \cap F_i^\phi}g_{ijk} +\sum_{j\in FT_k \cap F_i^\beta} h_{ijk} = x_{ik} & & \forall i \in V,~ k\in FS_i \label{eq:mip:ssbf3}\\[\eqnv]
	& & \sum_{i\in FT_k \cap P_j^\phi}g_{ijk} +\sum_{i\in FT_k \cap P_j^\beta} h_{ijk} = x_{ik} & & \forall j \in V,~ k\in FS_j \label{eq:mip:ssbf4}\\[\eqnv]
	& & \sum_{i\in FT_k} \sum_{j\in FT_k \cap F_i^\beta} h_{ijk} = 1 & & \forall k\in K \label{eq:mip:ssbf5}\\[\eqnv]
	& & (n-|F_i^*|-|P_j^*|)\cdot\left( \sum_{k\in FS_i \cap FS_j} g_{ijk} -1 \right) & & \nonumber\\[\smolEqnv]
	& & ~~+r_i +1 \leq r_j & & \forall i\in V,~ j\in F_i^\phi \label{eq:mip:ssbf6}\\[\eqnv]
	& & r_i +1 \leq r_j & & \forall (i,j) \in E \label{eq:mip:ssbf7}\\[\eqnv]
	& & w_i \leq w_j & & \forall (i,j) \in E \label{eq:mip:ssbf8}\\[\eqnv]
	& & \sum_{i\in FT_k} t_i \cdot x_{ik} +\sum_{i\in FT_k} \sum_{j\in FT_k \cap F_i^\phi} \phi_{ij} g_{ijk} & &  \nonumber\\[\smolEqnv]
	& & ~~+\sum_{i\in FT_k} \sum_{j\in FT_k \cap F_i^\beta} \beta_{ij}h_{ijk} \leq c & & \forall k \in K \label{eq:mip:ssbf9}\\[\eqnv]
	& & \sum_{i\in FT_k \setminus \{j\}} x_{ik} \leq (n-m+1)\cdot(1-h_{ijk}) & & \forall k \in K,~ j\in FT_k \label{eq:mip:ssbf10}\\[\eqnv]
	& & g_{ijk} \in \{0,1\} & & \forall k \in K,~ i\in FT_k,~ j\in FT_k \cap F_i^\phi \nonumber\\[\smolEqnv]
	& & & & \label{eq:mip:ssbf11}\\[\eqnv]
	& & h_{ijk} \in \{0,1\} & & \forall k \in K,~ i\in FT_k,~ j\in FT_k \cap F_i^\beta \nonumber\\[\smolEqnv]
	& & & & \label{eq:mip:ssbf12}\\[\eqnv]
	& & |P_i^*|+1 \leq r_i \leq n-|F_i^*| & & \forall i \in V \label{eq:mip:ssbf13}
\end{IEEEeqnarray}

Constraints (\ref{eq:mip:ssbf3}--\ref{eq:mip:ssbf4}) ensure each task has exactly one
successor and one predecessor in the cyclic sequence of the assigned station and
each cycle is forced to contain exactly one backward setup by constraint (\ref{eq:mip:ssbf5}).
Constraint set (\ref{eq:mip:ssbf6}--\ref{eq:mip:ssbf7}) establish the
precedence relations between the tasks within each station.
We note here that (\ref{eq:mip:ssbf6}) is a disjunctive constraint
and becomes inactive when tasks $i$ and $j$ are assigned
to different stations, \ie $g_{ijk}=0$.
Due to this, constraint (\ref{eq:mip:ssbf8}) is added to
ensure the precedence relations are satisfied between stations.
A knapsack constraint (\ref{eq:mip:ssbf9}) is again included
to strengthen the formulation.
If a task is its own successor, \ie $h_{jjk}=1$, then
constraint (\ref{eq:mip:ssbf10}) requires that no other tasks are
assigned the same station as task $j$.
Constraints (\ref{eq:mip:ssbf11}--\ref{eq:mip:ssbf13}) are the non-functional
constraints defining the new decision variables domains.

This model needs to use two disjunctions to achieve
a correct representation of the problem.
These disjunctive constraints are
(\ref{eq:mip:ssbf6}) and (\ref{eq:mip:ssbf10}).

\section{Scheduling-Based Formulation}
\label{sec:mip:scbf}
We now demonstrate that the \sua{} can be formulated
exclusively as a scheduling problem, namely as the
\scbf{}.
Taking this approach means we must again choose
a new way of encoding the decisions of the problem.
This leads us to the new decision variable $q_{ij}$
which takes the value 1 if and only if task $i\in V$ is processed
before task $j \in F_i^\phi$ along the assembly line.
Together with $q_{ij}$ we again use decision variables
$s_i$, $y_{ij}$ and $z_{ij}$ as defined previously.
Variable $r_i$ is also reused however now its value
represents the position of task $i$ in the full schedule of
tasks, rather than within a single cyclic sequence of
a station.

\begin{IEEEeqnarray}{rClCl}
	\IEEEeqnarraymulticol{3}{l}{\text{\scbf{2}: Min } c} & \hspace{4mm} & \label{eq:mip:scbf1}\\[\eqnv]
	\text{s.t. } & \hspace{1mm} & \IEEEeqnarraymulticol{3}{l}{(\ref{eq:mip:fsbf3}),~(\ref{eq:mip:fsbf4}),~(\ref{eq:mip:fsbf17}),~(\ref{eq:mip:fsbf18}),~(\ref{eq:mip:fsbf20}),~(\ref{eq:mip:fsbf21}),~(\ref{eq:mip:ssbf7}),~(\ref{eq:mip:ssbf13})} \nonumber\\[\smolEqnv]
	& & q_{ij} + q_{ji} = 1 & & \forall i \in V,~ j \in V\setminus (P_i^*\cup F_i^*) \text{ with } i<j \nonumber\\[\smolEqnv]
	& & & & \label{eq:mip:scbf2}\\[\eqnv]
	& & r_i + 1 + (n-|F_i^*|-|P_j^*|) & & \nonumber\\[\smolEqnv]
	& & ~~\times (q_{ij}-1) \leq r_j & & \forall i \in V,~ j \in V\setminus (P_i^*\cup F_i^*) \text{ with } i\neq j \nonumber\\[\smolEqnv]
	& & & & \label{eq:mip:scbf3}\\[\eqnv]
	& & r_j -1 + (n-|F_j^*|-|P_j^*|-1) & & \nonumber\\[\smolEqnv]
	& & ~~\times (y_{ij}-1) \leq r_i & & \forall i\in V,~ j\in F_i^\phi \label{eq:mip:scbf4}\\[\eqnv]
	& & y_{ij} \leq q_{ij} & & \forall i\in V,~ j\in F_i^\phi\setminus F_i^* \label{eq:mip:scbf5}\\[\eqnv]
	& & z_{ji} \leq q_{ij} & & \forall i\in V,~ j\in P_i^\beta\setminus F_i^*\text{ with }i\neq j \label{eq:mip:scbf6}\\[\eqnv]
	& & s_i + (t_i + \phi_{ij}) & & \nonumber\\[\smolEqnv]
	& & ~~+(c+\phi_{ij})\cdot(y_{ij}-1)\leq s_j & & \forall i \in V,~ j\in F_i^\phi \label{eq:mip:scbf7}\\[\eqnv]
	& & \sum_{i\in V}\sum_{j\in F_i^\beta}z_{ij} = m & &  \label{eq:mip:scbf8}\\[\eqnv]
	& & q_{ij} \in \{0,1\} & & \forall i\in V,~ j\in V\setminus (P_i^* \cup F_i^*) \text{ with } i \neq j \nonumber\\[\smolEqnv]
	& & & & \label{eq:mip:scbf9}
\end{IEEEeqnarray}

Constraint (\ref{eq:mip:scbf2}) establishes an ordering
between two tasks $i$ and $j$ which are not related
by the precedence graph.
The transitive relation between the $q_{ij}$ variables
is enforced by (\ref{eq:mip:scbf3}) together with (\ref{eq:mip:ssbf7}) and (\ref{eq:mip:scbf2}).
Explicitly, the combination of these constraints guarantee that for
three tasks $i$, $j$ and $v$, we have the following,
\[
	[q_{ij}=1]\wedge[q_{jv}=1] \Rightarrow [q_{iv}=1].
\]
Constraints (\ref{eq:mip:scbf4}--\ref{eq:mip:scbf5}) with (\ref{eq:mip:scbf3})
enforce the following implication,
\[
	[y_{ij}] \Rightarrow [r_j=r_i +1].
\]
Further to this, the conjunction of these constraints also encodes
the forward setup times.
Constraint (\ref{eq:mip:scbf6}) ensures that backward setups
are accounted for in the schedule defined by the $q_{ij}$
variables.

This formulation requires disjunctive constraints
(\ref{eq:mip:scbf3}), (\ref{eq:mip:scbf4}) and (\ref{eq:mip:scbf7}).

\section{Valid Inequalities}
\label{sec:mip:validIneqs}
In \authciteb{Esmaeilbeigi2016} some
valid inequalities are proposed to improve the formulations
of the type-2 version of \sua{}.
As we mentioned previously, none of these valid inequalities were tested in their
paper as their concern was with the type-1 version of the problem.

The following two inequalities can be added to both \fsbf{2}
and \scbf{2}
\begin{IEEEeqnarray}{lCl}
	y_{ij} + y_{ji} \leq 1 & \hspace{4mm} & \forall i \in V, j \in (F_i^\phi \cap P_i^\phi) \label{eq:mip:valIneq1}\\[\eqnv]
	w_{ii} + \sum_{j\in F_i^\beta}w_{ij} + \sum_{j\in P_i^\beta}w_{ji} \leq 1 & \hspace{4mm} & \forall i \in V \label{eq:mip:valIneq2}
\end{IEEEeqnarray}

Inequality (\ref{eq:mip:valIneq1}) states task $i$ cannot be before and
after another task $j$ in the forward station load.
Equivalently, inequality (\ref{eq:mip:valIneq2}) does the same for
the backward station load.
\citeauthor{Esmaeilbeigi2016} note that (\ref{eq:mip:valIneq1})
dominates (\ref{eq:mip:valIneq2}) and thus only the
first inequality was tested in the relevant formulations.

Let us denote the total setup time that occurs throughout all stations
by $S$.
The idle time of a station is the difference
between the line's cycle time and the station's workload; let the
total idle time across all stations be denoted by $I$.
So the total amount of time used by the assembly line
can be calculated by summing all processing times with the total
setup time and the total idle time.
This results in the following relation $t_{sum}+S+I=m\cdot c$.
This can be formulated as a valid inequality to tighten the
linear relaxation of the MIPs as follows
\begin{IEEEeqnarray}{lCl}
	t_{sum}+\sum_{i\in V} \sum_{j\in F_i^\phi}\phi_{ij}\cdot y_{ij}+\sum_{i\in V} \sum_{j\in F_i^\beta}\beta_{ij}\cdot z_{ij} \leq m\cdot c. \label{eq:mip:valIneq3}
\end{IEEEeqnarray}
Here we interpret the left-hand side of inequality (\ref{eq:mip:valIneq3})
as a lower bound on the line capacity and the right-hand side
as the tightest possible upper-bound.
Once again, this inequality cannot be added to \ssbf{2} due to
(\ref{eq:mip:valIneq3}) requiring the decision variables $y_{ij}$
and $z_{ij}$.

\section{Summary}
\label{sec:mip:summary}
% The three mixed-integer programs presented in this chapter
% have been untested by the literature.
When testing the viability of a Benders decomposition
approach to a problem,
it is common practice to compare the computational results against
the best known mixed-integer programming formulation of the full problem.
Although, due to the \sua{2} being relatively under-studied,
no computational results of an exact solution
approach are available.
As such, we will test the MIPs presented here in order to receive benchmark results.
These results will allow us to judge the quality of our Benders decomposition method,
which will be detailed in the next chapter.


% !TEX TS-program = xelatex
% !TEX encoding = UTF-8
% !Mode:: "TeX:UTF-8"

\documentclass[onecolumn,oneside]{SUSTechHomework}

\usepackage{indentfirst}
\setlength{\parindent}{2em}

\author{Yubin Hu}
\sid{11712121}
\title{Assignment 4}
\coursecode{CS403}
\coursename{Cryptography and Network Security}

\begin{document}
  \maketitle

  \section{}

    \subsection{}

    $H_a$ is necessarily collison-resistant.

    Here we use the contradiction method to prove.
    Suppose $H_a$ is not necessarily collison-resistant, there is a PPT algorithm A which find a collison with non-negligible probability($x \neq y,~H_a(x) = H_a(y)$).
    So we have $H_1^{s_1}(x)||H_2^{s_2}(x)=H_1^{s_1}(y)||H_2^{s_2}(y)$, and we got $H_1(x) = H_1(y)$ and $H_2(x) = H_2(y)$, which means $H_1 \mbox{and} H_2$ are not collison-resistant.
    This conflicts with assumptions.

    \subsection{}

    $H_b$ is not necessarily collison-resistant.

    Suppose we have $H_1$ be collison-resistant and $H_2(x) = 0$ for all $x$.
    So we got $\forall x, H_b(x) = H_1(0)||0$, and in this case, $H_b$ is not necessarily collison-resistant.

    \subsection{}

    $H_c$ is necessarily collison-resistant.

    $H_c$ is necessarily collison-resistant.
    Suppose $H_c$ is not necessarily collison-resistant, there is a PPT algorithm A which find a collison with non-negligible probability($x \neq y,~H_c(x) = H_c(y)$).
    So we have $H_1(H_2(x)||x)||H_2(H_1(x)||x) = H_1(H_2(y)||y)||H_2(H_1(y)||y)$, and we got $H_1(H_2(x)||x) = H_1(H_2(y)||y)$ and $H_2(H_1(x)||x) = H_2(H_1(y)||y)$.
    Also we know that $x \neq y$, so $H_1(x)||x \neq H_1(y)||y$ and $H_2(x)||x \neq H_2(y)||y$, , which means $H_1 \mbox{and} H_2$ are not collison-resistant.
    This conflicts with assumptions.

  \section{}

    \subsection{}

    Suppose that we have the compression function $h: {0,1}^{2n} \rightarrow {0, 1}^n$ and the length of $k$ is $n$.

    1. An arbitary message $m$ with length $n$ to ask the oracle. Let $t = Mac(m) = H(k||m) = h(h(k||IV)||m)$.

    2. $Mac(m||t) = h()t||t$, since $Mac(m||t)=H(k||m||t) = h(h(h(k||IV)||m)||t)$ and $t = h(h(k||IV))$

    Thus this PPT algorithm has a winning probability 1.
    This is not a secure MAC.

    \subsection{}

    If $H$ is modeled as a random oracle, $F_k(M) =H(k||m) is PRF$.
    And it is MAC security.

    \subsection{}

    The random oracles are more soundness then the normal oracles.
    So the consequences are only available for the random oracles, not for all the cases.

\section{}

  \subsection{}

  There are the set of quadratic residue (QR) called $G$. $G \subseteq Z_n^*$.
  Suppose we have $y_1,y_2 \in G$, then $\exists x_1,x_2 \in Z_n^*$, $y_1 = x_1^2,y_2=x_2^2$.
  So $y_1y_2 = x_1^2x_2^2 = x_1x_2x_1x_2$, it is closure.
  And $1=1^2$, $1 \in G$, it is identity.
  Now we have $y \in G$, $\exists x,x^{-1} in G_n^*$, so that $y=x^2,xx^{-1}=1$.
  $y^{-1} = {x^2}^{-1} = (x^{-1})^2$ and $y^{-1} \in G$, $yy^{-1} = 1$.
  Therefore, the set of QRs is a subgroup of $Z_n^*$.

  \subsection{}

  \begin{itemize}
    \item \emph{if part} Suppose we have $y \in Z_p^*$, $log_g(y) = 2k$, then $g^{2k} = y, y = (g^k)^2$ and $y$ is a QR.
    \item \emph{only if part} Support that $y$ is a QR, then $\exists x \in Z_p^*$ such that $y = x^2$. Let $log_g(x) = i$, then we got $x = g^i$ and $y = g^{2i}$.
    \item $log_g(y)$ is the min number over all the $2i \mbox{mod} (p-1)$.
    \item $p$ is a prime, $p - 1 = 1$ or $p$ is a even number. So $log_g(y)$ is even number.
  \end{itemize}

\section{}

Suppose we have $Z_n$ has a generator $g$.
$gcd(g, N) = 1$ and for $\forall y \in Z_N$.
We know that in $Z_N$, $g^x = xg \mbox{mod} N$.
Thus $x = g^{-1}y$ and We can obtain x through the extended Euclidean algorithm.

\section{}

  \subsection{}

  $S={1,4,9,16,8,2,15,13,13,15,2,8,16,9,4,1}={1,2,4,8,9,13,15,16}$, the size is 8.

  \subsection{}

  $x \in Z_{17}^*$ is a generator if and only if $gcd(x, \varphi(17))=1$. So the number of generator is $\varphi(\varphi(17))=\varphi(16)=8$

  \subsection{}

  $g^{ab} \in S$,
  $ab \mbox{mod} 2 = 0$, 17 is prime.
  $Pr[g^{ab} \in S]=1-Pr[ab~is~odd]=1-\frac{1}{4}=\frac{3}{4}$

\section{}

Suppose $x$ is a random element of $Z_N^*$ and $y=x^2$, we have a algorithm $A$ to get $y's$ square root $z$.
if $z = \pm x$, we choose another $x$ and do it again, until $z \neq \pm x$.
So we got 5 square root of $y: \pm x \pm z$.

$a = x^2 (mod N) = z^2 (mod N)$, $x^2-z^2=(x+z)(x-z) = kN$, $x+z \neq 0$, $x-z \neq 0$, then $k \neq 0$
So $k=1$ and $N=(x+z)(x-z)$

\section{}

For arbitrary message $m$, we first choose an arbitrary $k \neq 0,1$.
Asking the signing oracle to sign $m' = mk^e$ mod $N$(here $e$ is public key in the scheme of signature).
Then we have the $Sign(m') = m^{fd}$ mod $N=(mk^e)^d$ mod $N=m^d \times k^{ed}$ mod $N=m^d$ mod N.
Therefore, message m cannot be queried to the signing oracle.

\section{}

Suppose $G$ is a group with generator $g$, and $h_1, h_2, h_3$ are the element of G.
$h_1 = g^x$, $h_2=g^y$.
We define $DH_h(h_1,h_2)=DH_g(g^x, g^y)=g^{xy}$
The discrete algorithm problem is to compute $log_gh$
The CDH problem is to compute $DH_g(h_1,h_2)$
The DDH problem is distinguish $DH_g(h_1,h_2)$ frome a uniform element of $G$.
DDH > CDH > DLog

\section{}

  \subsection{}

  EI Gamal encryption scheme is not secure against the chosen ciphertext atatch.
  CCA-secure schemes are not malleable.

  \subsection{}
  The El Gamal signature scheme using hash-then-sign paradigm is secure against the chosen plaintext attack.

  \subsection{}
  Yes. we forge a signature for any given message m by asking the signing oracle.

\end{document}


\documentclass{article}
\usepackage{lmodern}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage[T1]{fontenc}
\usepackage{fancyhdr}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\usepackage{tabularx}
\usepackage{multirow}
\pagestyle{fancy}
\lhead{Anirudhan J Rajagopalan --- ajr619 --- N18824115}

\begin{document}

\title{Web Search Engines --- Homework 3}
\date{March 28, 2016}
\author{Anirudhan J Rajagopalan\\ N18824115\\ ajr619}
\maketitle

\newpage

\section{Problem 1}

The decision of the two judges can be summarized as:
\begin{center}
  \begin{tabular}{cc|c|c|}
    \cline{3-4}
    & & \multicolumn{2}{c|}{Judge 2} \\ \cline{3-4}
    & & Yes & No \\ \cline{1-4}
    \multicolumn{1}{|c}{\multirow{2}{*}{Judge 1}} &
    \multicolumn{1}{|c|}{Yes} & 2 & 4 \\ \cline{2-4}
    \multicolumn{1}{|c}{} &
    \multicolumn{1}{|c|}{No} & 4 & 2 \\ \cline{1-4}
  \end{tabular}
\end{center}

For the above condition, we get
\begin{align*}
  P(A) = \frac{2 + 2}{12}
\end{align*}

\begin{align*}
  P(nonrelevant) = \frac{6 + 6}{12 + 12} = 0.5
\end{align*}

\begin{align*}
  P(relevant) = \frac{6 + 6}{12 + 12} = 0.5
\end{align*}

\begin{align*}
  P(E) = & {P(NR)}^{2} + {P(R)}^{2} \\
  =& 0.5^{2} + 0.5^{2} \\
  =& 0.5
\end{align*}

\subsection{$kappa$ statistic}

\begin{align*}
  \kappa =& \frac{P(A) - P(E)}{1 - P(E)} \\
  =& \frac{0.33 - 0.5}{1 - 0.5} \\
  =& -0.36
\end{align*}

\subsection{Relevant only if both judges agree}
\#Relevant documents retrieved = 1 \\
\#retrieved documents = 5 \\
\#relevant documents = 4 \\
\subsubsection{Precision}
\begin{align*}
  Precision =& \frac{\#relevant documents retrieved}{\#retrieved documents} \\
  =& \frac{1}{5} \\
  =& 0.2
\end{align*}

\subsubsection{Recall}
\begin{align*}
  Recall =& \frac{\#relevant documents retrieved}{\#relevant documents}\\
  =& \frac{1}{4} \\
  =& 0.25
\end{align*}

\subsubsection{$F_{1}$ --- score}
\begin{align*}
  F_{1} =& \frac{2PR}{P+R} \\
  =& \frac{2*0.2*0.25}{0.2+0.25} \\
  =& 0.222
\end{align*}

\subsection{Relevant if either judge thinks relevant}
\#Relevant documents retrieved = 5 \\
\#retrieved documents = 5 \\
\#relevant documents = 10 \\

\subsubsection{Precision}
\begin{align*}
  Precision =& \frac{\#relevant documents retrieved}{\#retrieved documents} \\
  =& \frac{5}{5} \\
  =& 1
\end{align*}

\subsubsection{Recall}
\begin{align*}
  Recall =& \frac{\#relevant documents retrieved}{\#relevant documents}\\
  =& \frac{5}{10} \\
  =& 0.5
\end{align*}

\subsubsection{$F_{1}$ --- score}
\begin{align*}
  F_{1} =& \frac{2PR}{P+R} \\
  =& \frac{2 * 1 * 0.5}{1 + 0.5} \\
  =& 0.667
\end{align*}

\section{Problem 2}
\subsection{How to evaluate an image search engine}
Evaluating any search engine involves defining what ``relevancy'' is with respect to the particular audience the search engine is designed to serve.
The relevancy of a search engine might be dependent on how the input is provided.  Incase of image search, the search can be done by text or by providing images.

\subsubsection{Image input}
When a search is based on image input, the returned images should be similar to the input image.  This will depend on various descriptors of an image such as brightness, contrast, colors and so on.

Suppose we upload an image of a patch of tiger skin, the first relevant answer can be a sofa which has stripes similar to that of a tiger.  This result is acceptable because the sofa's pattern matches the input image's pattern.

\subsubsection{Text input}
When an image search is triggered by textual input, the result should be an image which best describes the textual content given by the user.
The most important way in which we can check for relevancy is to verify if objects representing each and every search term is present in the result images.

Suppose an user searches for ``Monkey swinging from a tree''.  The first result should have a monkey, and a tree and possibily the monkey swinging.  The second result can have a moneky, a tree and a swing and so on.

Suppose, the user searches for ``tiger skin'', the first relevant answer should be an image which shows tiger skin and not a sofa which is covered by a cloth similar to tiger skin.

\subsection{How evaluating image search differs from textual search}
As discussed above, evaluating image search differs from textual search mainly because of the type of inputs that can be provided to the system.

Also the conecpt of relevancy can get confusing when we deal with images \& text, especially when the input is an image.  An image search for ``August Rush'' can return the posters of movie \textit{August rush} which is quite relevant for people looking for the movie.  But it can also return images of a festival which happens in August and attracts huge amount of people.  This may or maynot be relevant to the user who tries to search for images using textual content.

\section{Problem 3}
Given a relation between Gold standard and custom clustering as follows:
\begin{center}
  \begin{tabular}{llll}
      & A & B & C \\
    X & 10 & 40 & 20 \\
    Y & 50 & 10 & 20 \\
  \end{tabular}
\end{center}

\subsection{Purity of cluster}

For the given example
\begin{align*}
  \Omega = & {X, Y} \\
  \mathbb{C} = & {A, B, C}
\end{align*}

The purity is given by
\begin{align*}
  purity(\Omega, \mathbb{C}) = & \frac{1}{N} \sum_{k} \max_{j} |\omega_{k} \cap C_{j}|
\end{align*}

We interpret $\omega_{k}$ as the set of documents in $\omega_{k}$ and $ c_{j} $ as the set of documents in $c_{j}$.

Therefore the purity for the given cluster combination is given by

\begin{align*}
  purity(\Omega, \mathbb{C}) =& \frac{1}{150} ( 40 + 50 )\\
  =& \frac{90}{150}\\
  =& 0.6
\end{align*}

\subsection{Computation for TP, TN, FP, FN, P, R, $F_{\beta}$ }
\subsubsection{TP --- True Positives}
Pairs $ \alpha -  \beta $ which are in the same cluster in both the gold standard and in the algorithmic clustering.

The True Positives are given by
\begin{align*}
  TP =& \frac{10 * 9}{2} + \frac{40 * 39}{2} + \frac{20 * 19}{2} + \frac{50 * 49}{2} + \frac{10 * 9}{2} + \frac{20 * 19}{2} \\
  =& 20*39 + 25*49 + 20*19 + 10*9 \\
  =& 2475
\end{align*}

\subsubsection{FP --- False Positives}
Pairs $ \alpha -  \beta $ which are in the same cluster in the algorithmic cluster but different clusters in the gold standard.

The False Positives are given by
\begin{align*}
  FP =& 10*40 + 10*20 + 40*20 + 50*10 + 50*20 + 10*20 \\
  =& 400 + 200 + 800 + 500 + 1000 + 200 \\
  =& 3100
\end{align*}

\subsubsection{FN --- False Negatives}
Pairs $ \alpha -  \beta $ which are in the same cluster in the gold standard, but different algorithmic clusters.

The False Negatives are given by
\begin{align*}
  FN =& 10*50 + 40*10 + 20*20 \\
  =& 500 + 400 + 400 \\
  =& 1300
\end{align*}

\subsubsection{TN --- True Negatives}
Pairs $ \alpha -  \beta $ that are in different clusters in both the gold standard and the algorithmic clustering.

The True Negatives are given by
\begin{align*}
  TN =& 10*10 + 10*20 + 40*50 + 40*20 + 20*50 + 20*10 \\
  =& 100 + 200 + 2000 + 800 + 1000 + 200 \\
  =& 4300
\end{align*}

\subsection{Rand index}
\begin{align*}
  RI = & \frac{TP + TN}{TP + FP + FN + TN} \\
  =& \frac{6775}{11175} \\
  =& 0.606
\end{align*}

\subsection{Precision}
\begin{align*}
  Precision =& \frac{TP}{TP+FP} \\
  =& \frac{2475}{2475 + 3100}\\
  =& \frac{2475}{5575} \\
  =& 0.4439
\end{align*}

\subsection{Recall}
\begin{align*}
  Recall =& \frac{TP}{TP + FN} \\
  =& \frac{2475}{2475 + 1300} \\
  =& \frac{2475}{3775} \\
  =& 0.6556
\end{align*}

\subsection{F score when $\beta$=1}
\begin{align*}
  F_{\beta} =& \frac{(\beta^{2} + 1) * P * R}{\beta^{2}P + R} \\
  =& \frac{2PR}{P+R} \\
  =& \frac{2 * 0.4439 * 0.6556}{0.4439 + 0.6556} \\
  =& \frac{0.5820}{1.0995} \\
  =& 0.5293
\end{align*}

\end{document}


\newpage
\subsection{Scalar AES Acceleration}
\label{sec:scalar:aes}

This section details proposals for acceleration of
the AES block cipher \cite{nist:fips:197} within a scalar RISC-V core,
obeying the two-read-one-write constraint on general purpose register
file accesses.
Supporting material, including rationale and a design space exploration for
these instructions can be found in \cite{cryptoeprint:2020:930}.

\subsubsection{RV32 Instructions}
\label{sec:scalar:aes:rv32}

\begin{bytefield}[bitwidth={1.05em},endianness={big}]{32}
\bitheader{0-31} \\
\encaesthreetwoesmi
\encaesthreetwoesi
\encaesthreetwodsmi
\encaesthreetwodsi
\end{bytefield}

\begin{cryptoisa}
aes32esi     rt, rs2, bs // Encrypt: SubBytes
aes32esmi    rt, rs2, bs // Encrypt: SubBytes & MixColumns
aes32dsi     rt, rs2, bs // Decrypt: SubBytes
aes32dsmi    rt, rs2, bs // Decrypt: SubBytes & MixColumns
\end{cryptoisa}

These instructions are a very lightweight proposal, derived from
\cite{MJS:LWAES:20}.
They are designed to enable a partial T-Table based implementation
of AES in hardware, where the SubBytes, ShiftRows and MixColumns
transformations are all rolled into a single instruction, with the
per-byte results then accumulated.
The {\tt bs} immediate operand is a 2-bit {\em Byte Select}, and indicates
which byte of the input word is operated on.
RISC-V Sail model code for each instruction is found in figure
\ref{fig:sail:aes:rv32}.
Note that the instructions source their destination register from
bits $19:15$ of the encoding, rather than the usual $11:7$.
This is because the instructions are designed to be used such that
the destination register is always the same as {\tt rs1}.
See Appendix \ref{sec:scalar:encodings} for more information.

These instructions use the Equivalent Inverse Cipher
construction \cite[Section 5.3.5]{nist:fips:197}.
This affects the computation of the KeySchedule, as shown in
\cite[Figure 15]{nist:fips:197}.

\begin{figure}[h]
\lstinputlisting[language=sail,firstline=55,lastline=71]{../extern/sail-riscv/model/riscv_insts_kext_rv32.sail}
\caption{RISC-V Sail model specification for the lightweight AES instructions targeting the
RV32 base architecture.}
\label{fig:sail:aes:rv32}
\end{figure}

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

\newpage
\subsubsection{RV64 Instructions}
\label{sec:scalar:aes:rv64}

\begin{bytefield}[bitwidth={1.05em},endianness={big}]{32}
\bitheader{0-31} \\
\encaessixfourksonei
\encaessixfourkstwo
\encaessixfourim
\encaessixfouresm
\encaessixfoures
\encaessixfourdsm
\encaessixfourds
\end{bytefield}

\begin{cryptoisa}
aes64ks1i  rd, rs1, rcon // KeySchedule: SubBytes, Rotate, Round Const
aes64ks2   rd, rs1, rs2  // KeySchedule: XOR summation
aes64im    rd, rs1       // KeySchedule: InvMixColumns for Decrypt
aes64esm   rd, rs1, rs2  // Round:    ShiftRows,    SubBytes,    MixColumns
aes64es    rd, rs1, rs2  // Round:    ShiftRows,    SubBytes
aes64dsm   rd, rs1, rs2  // Round: InvShiftRows, InvSubBytes, InvMixColumns
aes64ds    rd, rs1, rs2  // Round: InvShiftRows, InvSubBytes
\end{cryptoisa}

These instructions are for RV64 only.
They implement the SubBytes, ShiftRows and MixColumns transformations of AES.
Each round instruction takes two 64-bit registers as input, representing
the 128-bit state of the AES cipher, and outputs one 64-bit
result, i.e. half of the next round state.
The byte mapping of input register values to AES state and output register
values is shown in \figref{aes:rv64:mapping}.
RISC-V Sail model code for the instructions is illustrated in
\figref{pesudo:aes:rv64}.

\begin{itemize}

\item
The
\mnemonic{aes64ks1i}/\mnemonic{aes64ks2}
instructions are used in the encrypt KeySchedule.
\mnemonic{aes64ks1i} implements the rotation, SubBytes and Round Constant
addition steps.
\mnemonic{aes64ks2} implements the remaining {\tt xor} operations.

\item
The
\mnemonic{aes64im}
instruction applies the inverse MixColumns
transformation to two columns of the state array, packed into a single
64-bit register.
It is used to create the inverse cipher KeySchedule, according to
the equivalent inverse cipher construction in
\cite[Page 23, Section 5.3.5]{nist:fips:197}.

\item
The \mnemonic{aes64esm}/\mnemonic{aes64dsm} instructions perform the
(Inverse) SubBytes, ShiftRows and MixColumns Transformations.

\item
The \mnemonic{aes64es}/\mnemonic{aes64ds} instructions perform the
(Inverse) SubBytes and ShiftRows Transformations.
They are used for the last round only.

\item
Computing the next round state uses two instructions.
The high or low 8 bytes of the next state are selected by swapping the order
of the source registers.
The following code snippet shows one round of the AES block encryption.
{\tt t0} and {\tt t1} hold the current round state.
{\tt t2} and {\tt t3} hold the next round state.
\begin{lstlisting}
aes64esm t2, t0, t1 // ShiftRows, SubBytes, MixColumns bytes 0..7
aes64esm t3, t1, t0 // "          "         "          "     8..15
\end{lstlisting}
\end{itemize}

This proposal requires $6$ instructions per AES round:
$2$ \mnemonic{ld} instructions to load the round key,
$2$ \mnemonic{xor} to add the round key to the current state
and
$2$ of the relevant AES encrypt/decrypt instructions to perform the
    SubBytes, ShiftRows and MixColumns round functions.
An un-rolled AES-128 block encryption with an offline KeySchedule
hence requires $69$ instructions in total.

These instructions are amenable to macro-op fusion.
The recommended sequences are:
\begin{lstlisting}[language=pseudo]
aes64esm rd1, rs1, rs2 // Different destination registers,
aes64esm rd2, rs2, rs1 // identical source registers with swapped order.
\end{lstlisting}
This is similar to the recommended \mnemonic{mulh}, \mnemonic{mul}
sequence in the M extension to compute a full $32*32->64$ bit
multiplication result \cite[Section 7.1]{riscv:spec:user}.

Unlike the $32$-bit AES instructions, the $64$-bit variants
{\em do not} use the Equivalent Inverse Cipher
construction \cite[Section 5.3.5]{nist:fips:197}.

\begin{figure}[h]
\centering
\includegraphics[width=0.8\textwidth]{diagrams/aes-rv64-state.png}
\caption{
Mapping of AES state between input and output registers for the round
instructions.
{\tt Rout1} is given by \mnemonic{aes64esm rd, rs1, rs2},
and
{\tt Rout2}          by \mnemonic{aes64esm rd, rs2, rs1}.
The {\tt [Inv]ShiftRows} blocks show how to select the relevant $8$ bytes
for further processing from the concatenation {\tt rs2 || \tt rs1}.
}
\label{fig:aes:rv64:mapping}
\end{figure}

\begin{figure}[h!]
\lstinputlisting[language=sail,firstline=64,lastline=105]{../extern/sail-riscv/model/riscv_insts_kext_rv64.sail}
\caption{
RISC-V Sail model specification for the RV64 AES instructions.
}
\label{fig:pesudo:aes:rv64}
\end{figure}


\documentclass[12pt]{cdblatex}
\usepackage{fancyhdr}
\usepackage{footer}

\begin{document}

% ============================================================================================
\section*{Example 10 The determinant of the metric}

\CdbSetup{action=hide}

\begin{cadabra}
   {a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u#}::Indices(position=independent).

   \nabla{#}::Derivative.

   d{#}::KroneckerDelta.

   R_{a b c d}::RiemannTensor.

   def check (objA,objB):

       tmp := @(objA) - @(objB).

       distribute     (tmp)
       sort_product   (tmp)
       rename_dummies (tmp)
       canonicalise   (tmp)

       return tmp

   import cdblib

   # ---------------------------------------------
   check000A = cdblib.get ('check000','expected/example-10.json')
   check001A = cdblib.get ('check001','expected/example-10.json')
   check002A = cdblib.get ('check002','expected/example-10.json')

   # ---------------------------------------------
   check000B = cdblib.get ('check000','output/example-10.json')
   check001B = cdblib.get ('check001','output/example-10.json')
   check002B = cdblib.get ('check002','output/example-10.json')

   # ---------------------------------------------
   diff000 = check (check000A,check000B)   # cdb (ex-10.diff000,diff000)
   diff001 = check (check001A,check001B)   # cdb (ex-10.diff001,diff001)
   diff002 = check (check002A,check002B)   # cdb (ex-10.diff002,diff002)

\end{cadabra}

% \clearpage

\begin{dgroup*}
   \Dmath*{\cdb*{ex-10.diff000}}
   \Dmath*{\cdb*{ex-10.diff001}}
   \Dmath*{\cdb*{ex-10.diff002}}
\end{dgroup*}

\end{document}


\documentclass[11pt]{amsart}
\usepackage{geometry}                % See geometry.pdf to learn the layout options. There are lots.
\geometry{letterpaper}                   % ... or a4paper or a5paper or ...
%\geometry{landscape}                % Activate for for rotated page geometry
%\usepackage[parfill]{parskip}    % Activate to begin paragraphs with an empty line rather than an indent
\usepackage{graphicx}
\usepackage{amssymb}
\usepackage{epstopdf}
\usepackage{dirtytalk}
\usepackage{fuzz}

\DeclareGraphicsRule{.tif}{png}{.png}{`convert #1 `dirname #1`/`basename #1 .tif`.png}

\title{Notes on Entropy}
\author{Arthur Ryman}
\date{\today}                                           % Activate to display a given date or no date

\begin{document}

\begin{abstract}
The concept of entropy crops up in many seemingly unrelated areas, ranging from physics to information theory to
machine learning.
This article summarizes the mathematical foundations of entropy, providing precise definitions and proofs.
\end{abstract}

\maketitle

\section{Introduction}

Carnot discovered the concept of entropy while analyzing the maximum theoretical efficiency of heat engines.
Boltzman gave entropy a statistical definition in terms of the number of microscopic states available to a macroscopic state.
Shannon showed how entropy was relevant to the rate at which information could be transmitted over a communications channel.
Bekenstein proposed that black holes have entropy and Hawking defined their temperature and predicted that they would radiate.
And today, machine learning algorithms regularly use the concepts of entropy and information gain to analyze large datasets.

This is certainly a fascinating and important circle of ideas!
Unfortunately, it is difficult to find clear explanations of these concepts.
Typical textbooks on thermodynamics do not lay out the material using precise mathematical language.
My goal in this article is to collect together all the related definitions and give them a precise mathematical treatment.

The target audience for this article is me since I am personally dissatisfied with my grasp of the subject.
However, if I manage to explain this material clearly and simply enough, then it may be of interest to the machine learning community.

\section{Microscopic and Macroscopic States}

When Carnot discovered entropy, the atomic theory of matter was not widely accepted as fact by the scientific community.
Carnot analyzed heat engines using their macroscopic properties, such as temperature, volume, and pressure.
When the atomic theory was being developed, physicists sought to derive the macroscopic properties of matter from
the statistical properties of their microscopic constituents.
This approach was called {\it statistical mechanics} or, more generally, {\it statistical physics}.
In fact, the success of statistical mechanics in explaining thermodynamics was viewed as strong supporting evidence for the
validity of the atomic theory of matter.

\subsection{Boltzman Entropy}

Although the thermodynamic definition of entropy came first,
I am going to start with the statistical definition since it explains the thermodynamic definition and
it applies more directly to machine learning.

The concept of entropy arises when the we forget information about the states of a system.
Consider a system and let $A$ be the set of states that the system can be in.
For example, consider a classical system that consists of $N$ identical point particles each mass $m$ moving in a box of fixed volume
that has perfectly reflecting walls.
The state of this system is determined by the position and momentum of each particle.
We refer to $A$ as the set of {\it microscopic states} of the system or, more briefly, its {\it microstates}.

Suppose further that $B$ is a related set of more coarse-grained states referred to as the {\it macroscopic states} or {\it macrostates}.
Every macrostate corresponds to one or more microstates.
Continuing with the example of particles in a box, the macrostate might be defined simply by the system's temperature.

Let the correspondence between the microstates and macrostates be given by a {\it forgetful} mapping $f$ of $A$ onto $B$:
\begin{equation}
	f: A \fun B
\end{equation}
For particles in a box, the temperature is their average kinetic energy

The Boltzman definition of entropy applies directly to the case where for each macrostate $b$ there is a finite number $\Omega_f(b)$ of microstates
that map to $b$ under $f$:
\begin{equation}
	\Omega_f : B \fun \nat_1
\end{equation}
where
\begin{equation}
	 \Omega_f(b) = \# \{~ a: A | f(a) = b ~\}
\end{equation}
In this case the entropy $S_f(b)$ of the macrostate $b$ is defined to be:
\begin{equation}
	S_f : B \fun \mathbb{R}
\end{equation}
where
\begin{equation}
	S_f(b) = k \ln \Omega_f(b)
\end{equation}

In the system of particles in a box there are an infinite number of microstates for each macrostates so we'll need to adapt Boltzman's definition of entropy a little.
Before doing that, let's examine the situation for finite system.

\subsection{The Entropy of Finite Systems}

Consider a particle that can exist in one of some finite number $d$ of states.
Let $\Phi$ denote the set these single-particle states:
\begin{equation}
	\# \Phi = d
\end{equation}

Let $\phi_1, \ldots, \phi_d$ denote the $d$ states in $\Phi$:
\begin{equation}
	\Phi = \{ \phi_1, \ldots, \phi_d \}
\end{equation}

Now consider a system of $N$ of these particles.
Each microstate of this system is given by an ordered $N$-tuple of single-particle states:
\begin{equation}
	A = \Phi^N
\end{equation}

Recall the definition of the Kronecker $\delta$-function:
\begin{equation}
	\delta(x,y) =
		\left\{
			\begin{array}{ll}
			1	&	\mbox{if $x = y$} \\
			0	&	\mbox{if $x \neq y$}
			\end{array}
		 \right.
\end{equation}

Given an $N$-particle microstate $a = (a_1, \ldots, a_N)$  and a single-particle state $\phi \in \Phi$ let $n(a, \phi)$ be the number of particles in
$a$ that are in the single-particle state $\phi$:
\begin{equation}
	n : A  \cross \Phi \fun \nat
\end{equation}
where
\begin{equation}
	n(a, \phi) = \sum_{i=1}^N \delta(a_i, \phi)
\end{equation}
The number of particles in a single-particle state is referred to as the {\it occupancy number} of that state.
Note that for any multi-particle state $a$ we have:
\begin{equation}
	\sum_{j=1}^d n(a, \phi_j) = N
\end{equation}

Define the macrostates of the system to be the set of all possible $N$-particle occupancy number assignments $b$ on $\Phi$:
\begin{equation}
	B = \{~ b: \Phi \fun \nat | \sum_{j=1}^d b(\phi_j) = N ~\}
\end{equation}

The forgetful mapping $f : A \fun B$ sends an $N$-particle state to its occupancy numbers on $\Phi$:
\begin{equation}
	f(a)(\phi) = n(a, \phi)
\end{equation}

TO DO: Compute the entropy of a state b in B. Use Stirling's formula to approximate it. Show how Shannon's entropy emerges.

%\subsection{}

\end{document}


\XtoCBlock{TDSystemO2}
\label{block:TDSystemO2}
\begin{figure}[H]\includegraphics{TDSystemO2}\end{figure}

\begin{XtoCtabular}{Inports}
In1 & Input \#1\tabularnewline
\hline
In2 & Input \#2\tabularnewline
\hline
\end{XtoCtabular}

\begin{XtoCtabular}{Outports}
Out1 & Output \#1\tabularnewline
\hline
Out2 & Output \#2\tabularnewline
\hline
\end{XtoCtabular}

\begin{XtoCtabular}{Mask Parameters}
A & State matrix A\tabularnewline
\hline
B & Input matrix B\tabularnewline
\hline
\end{XtoCtabular}

\subsubsection*{Description:}
2nd order time discrete system with two inputs and two outputs.

% include optional documentation file
\InputIfFileExists{\XcHomePath/Library/Control/Doc/TDSystemO2_Info.tex}{\vspace{1ex}}{}

\subsubsection*{Implementations:}
\begin{tabular}{l l}
\textbf{FiP8} & 8 Bit Fixed Point Implementation\tabularnewline
\textbf{FiP16} & 16 Bit Fixed Point Implementation\tabularnewline
\textbf{FiP32} & 32 Bit Fixed Point Implementation\tabularnewline
\textbf{Float32} & 32 Bit Floating Point Implementation\tabularnewline
\textbf{Float64} & 64 Bit Floating Point Implementation\tabularnewline
\end{tabular}

\XtoCImplementation{FiP8}
\index{Block ID!3360}
\nopagebreak[0]
% Implementation details
\begin{tabular}{l l}
\textbf{Name} & FiP8 \tabularnewline
\textbf{ID} & 3360 \tabularnewline
\textbf{Revision} & 1 \tabularnewline
\textbf{C filename} & TDSystemO2\_FiP8.c \tabularnewline
\textbf{H filename} & TDSystemO2\_FiP8.h \tabularnewline
\end{tabular}
\vspace{1ex}

8 Bit Fixed Point Implementation

\begin{XtoCtabular}{Controller Parameters}
a11 & Coefficient a11\tabularnewline
\hline
a12 & Coefficient a12\tabularnewline
\hline
a21 & Coefficient a21\tabularnewline
\hline
a22 & Coefficient a22\tabularnewline
\hline
b11 & Coefficient b11\tabularnewline
\hline
b12 & Coefficient b12\tabularnewline
\hline
b21 & Coefficient b21\tabularnewline
\hline
b22 & Coefficient b22\tabularnewline
\hline
sfra11 & Shift factor for coefficient a11\tabularnewline
\hline
sfra12 & Shift factor for coefficient a12\tabularnewline
\hline
sfra21 & Shift factor for coefficient a21\tabularnewline
\hline
sfra22 & Shift factor for coefficient a22\tabularnewline
\hline
sfrb11 & Shift factor for coefficient b11\tabularnewline
\hline
sfrb12 & Shift factor for coefficient b12\tabularnewline
\hline
sfrb21 & Shift factor for coefficient b21\tabularnewline
\hline
sfrb22 & Shift factor for coefficient b22\tabularnewline
\hline
x1 & State x1\tabularnewline
\hline
x2 & State x2\tabularnewline
\hline
\end{XtoCtabular}

% Implementation data structure
\XtoCDataStruct{Data Structure:}
\begin{lstlisting}
typedef struct {
     uint16        ID;
     int8          *In1;
     int8          *In2;
     int8          Out1;
     int8          Out2;
     int8          a11;
     int8          a12;
     int8          a21;
     int8          a22;
     int8          b11;
     int8          b12;
     int8          b21;
     int8          b22;
     uint8         sfra11;
     uint8         sfra12;
     uint8         sfra21;
     uint8         sfra22;
     uint8         sfrb11;
     uint8         sfrb12;
     uint8         sfrb21;
     uint8         sfrb22;
     int8          x1;
     int8          x2;
} TDSYSTEMO2_FIP8;
\end{lstlisting}

\ifdefined \AddTestReports
\InputIfFileExists{\XcHomePath/Library/Control/Doc/Test_TDSystemO2_FiP8.tex}{}{}
\fi
\XtoCImplementation{FiP16}
\index{Block ID!3361}
\nopagebreak[0]
% Implementation details
\begin{tabular}{l l}
\textbf{Name} & FiP16 \tabularnewline
\textbf{ID} & 3361 \tabularnewline
\textbf{Revision} & 1 \tabularnewline
\textbf{C filename} & TDSystemO2\_FiP16.c \tabularnewline
\textbf{H filename} & TDSystemO2\_FiP16.h \tabularnewline
\end{tabular}
\vspace{1ex}

16 Bit Fixed Point Implementation

\begin{XtoCtabular}{Controller Parameters}
a11 & Coefficient a11\tabularnewline
\hline
a12 & Coefficient a12\tabularnewline
\hline
a21 & Coefficient a21\tabularnewline
\hline
a22 & Coefficient a22\tabularnewline
\hline
b11 & Coefficient b11\tabularnewline
\hline
b12 & Coefficient b12\tabularnewline
\hline
b21 & Coefficient b21\tabularnewline
\hline
b22 & Coefficient b22\tabularnewline
\hline
sfra11 & Shift factor for coefficient a11\tabularnewline
\hline
sfra12 & Shift factor for coefficient a12\tabularnewline
\hline
sfra21 & Shift factor for coefficient a21\tabularnewline
\hline
sfra22 & Shift factor for coefficient a22\tabularnewline
\hline
sfrb11 & Shift factor for coefficient b11\tabularnewline
\hline
sfrb12 & Shift factor for coefficient b12\tabularnewline
\hline
sfrb21 & Shift factor for coefficient b21\tabularnewline
\hline
sfrb22 & Shift factor for coefficient b22\tabularnewline
\hline
x1 & State x1\tabularnewline
\hline
x2 & State x2\tabularnewline
\hline
\end{XtoCtabular}

% Implementation data structure
\XtoCDataStruct{Data Structure:}
\begin{lstlisting}
typedef struct {
     uint16        ID;
     int16         *In1;
     int16         *In2;
     int16         Out1;
     int16         Out2;
     int16         a11;
     int16         a12;
     int16         a21;
     int16         a22;
     int16         b11;
     int16         b12;
     int16         b21;
     int16         b22;
     uint8         sfra11;
     uint8         sfra12;
     uint8         sfra21;
     uint8         sfra22;
     uint8         sfrb11;
     uint8         sfrb12;
     uint8         sfrb21;
     uint8         sfrb22;
     int16         x1;
     int16         x2;
} TDSYSTEMO2_FIP16;
\end{lstlisting}

\ifdefined \AddTestReports
\InputIfFileExists{\XcHomePath/Library/Control/Doc/Test_TDSystemO2_FiP16.tex}{}{}
\fi
\XtoCImplementation{FiP32}
\index{Block ID!3362}
\nopagebreak[0]
% Implementation details
\begin{tabular}{l l}
\textbf{Name} & FiP32 \tabularnewline
\textbf{ID} & 3362 \tabularnewline
\textbf{Revision} & 1 \tabularnewline
\textbf{C filename} & TDSystemO2\_FiP32.c \tabularnewline
\textbf{H filename} & TDSystemO2\_FiP32.h \tabularnewline
\end{tabular}
\vspace{1ex}

32 Bit Fixed Point Implementation

\begin{XtoCtabular}{Controller Parameters}
a11 & Coefficient a11\tabularnewline
\hline
a12 & Coefficient a12\tabularnewline
\hline
a21 & Coefficient a21\tabularnewline
\hline
a22 & Coefficient a22\tabularnewline
\hline
b11 & Coefficient b11\tabularnewline
\hline
b12 & Coefficient b12\tabularnewline
\hline
b21 & Coefficient b21\tabularnewline
\hline
b22 & Coefficient b22\tabularnewline
\hline
sfra11 & Shift factor for coefficient a11\tabularnewline
\hline
sfra12 & Shift factor for coefficient a12\tabularnewline
\hline
sfra21 & Shift factor for coefficient a21\tabularnewline
\hline
sfra22 & Shift factor for coefficient a22\tabularnewline
\hline
sfrb11 & Shift factor for coefficient b11\tabularnewline
\hline
sfrb12 & Shift factor for coefficient b12\tabularnewline
\hline
sfrb21 & Shift factor for coefficient b21\tabularnewline
\hline
sfrb22 & Shift factor for coefficient b22\tabularnewline
\hline
x1 & State x1\tabularnewline
\hline
x2 & State x2\tabularnewline
\hline
\end{XtoCtabular}

% Implementation data structure
\XtoCDataStruct{Data Structure:}
\begin{lstlisting}
typedef struct {
     uint16        ID;
     int32         *In1;
     int32         *In2;
     int32         Out1;
     int32         Out2;
     int32         a11;
     int32         a12;
     int32         a21;
     int32         a22;
     int32         b11;
     int32         b12;
     int32         b21;
     int32         b22;
     uint8         sfra11;
     uint8         sfra12;
     uint8         sfra21;
     uint8         sfra22;
     uint8         sfrb11;
     uint8         sfrb12;
     uint8         sfrb21;
     uint8         sfrb22;
     int32         x1;
     int32         x2;
} TDSYSTEMO2_FIP32;
\end{lstlisting}

\ifdefined \AddTestReports
\InputIfFileExists{\XcHomePath/Library/Control/Doc/Test_TDSystemO2_FiP32.tex}{}{}
\fi
\XtoCImplementation{Float32}
\index{Block ID!3363}
\nopagebreak[0]
% Implementation details
\begin{tabular}{l l}
\textbf{Name} & Float32 \tabularnewline
\textbf{ID} & 3363 \tabularnewline
\textbf{Revision} & 0.1 \tabularnewline
\textbf{C filename} & TDSystemO2\_Float32.c \tabularnewline
\textbf{H filename} & TDSystemO2\_Float32.h \tabularnewline
\end{tabular}
\vspace{1ex}

32 Bit Floating Point Implementation

\begin{XtoCtabular}{Controller Parameters}
a11 & Coefficient a11\tabularnewline
\hline
a12 & Coefficient a12\tabularnewline
\hline
a21 & Coefficient a21\tabularnewline
\hline
a22 & Coefficient a22\tabularnewline
\hline
b11 & Coefficient b11\tabularnewline
\hline
b12 & Coefficient b12\tabularnewline
\hline
b21 & Coefficient b21\tabularnewline
\hline
b22 & Coefficient b22\tabularnewline
\hline
x1 & State x1\tabularnewline
\hline
x2 & State x2\tabularnewline
\hline
\end{XtoCtabular}

% Implementation data structure
\XtoCDataStruct{Data Structure:}
\begin{lstlisting}
typedef struct {
     uint16        ID;
     float32       *In1;
     float32       *In2;
     float32       Out1;
     float32       Out2;
     float32       a11;
     float32       a12;
     float32       a21;
     float32       a22;
     float32       b11;
     float32       b12;
     float32       b21;
     float32       b22;
     float32       x1;
     float32       x2;
} TDSYSTEMO2_FLOAT32;
\end{lstlisting}

\ifdefined \AddTestReports
\InputIfFileExists{\XcHomePath/Library/Control/Doc/Test_TDSystemO2_Float32.tex}{}{}
\fi
\XtoCImplementation{Float64}
\index{Block ID!3364}
\nopagebreak[0]
% Implementation details
\begin{tabular}{l l}
\textbf{Name} & Float64 \tabularnewline
\textbf{ID} & 3364 \tabularnewline
\textbf{Revision} & 0.1 \tabularnewline
\textbf{C filename} & TDSystemO2\_Float64.c \tabularnewline
\textbf{H filename} & TDSystemO2\_Float64.h \tabularnewline
\end{tabular}
\vspace{1ex}

64 Bit Floating Point Implementation

\begin{XtoCtabular}{Controller Parameters}
a11 & Coefficient a11\tabularnewline
\hline
a12 & Coefficient a12\tabularnewline
\hline
a21 & Coefficient a21\tabularnewline
\hline
a22 & Coefficient a22\tabularnewline
\hline
b11 & Coefficient b11\tabularnewline
\hline
b12 & Coefficient b12\tabularnewline
\hline
b21 & Coefficient b21\tabularnewline
\hline
b22 & Coefficient b22\tabularnewline
\hline
x1 & State x1\tabularnewline
\hline
x2 & State x2\tabularnewline
\hline
\end{XtoCtabular}

% Implementation data structure
\XtoCDataStruct{Data Structure:}
\begin{lstlisting}
typedef struct {
     uint16        ID;
     float64       *In1;
     float64       *In2;
     float64       Out1;
     float64       Out2;
     float64       a11;
     float64       a12;
     float64       a21;
     float64       a22;
     float64       b11;
     float64       b12;
     float64       b21;
     float64       b22;
     float64       x1;
     float64       x2;
} TDSYSTEMO2_FLOAT64;
\end{lstlisting}

\ifdefined \AddTestReports
\InputIfFileExists{\XcHomePath/Library/Control/Doc/Test_TDSystemO2_Float64.tex}{}{}
\fi


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{float}
\usepackage{geometry}
\usepackage{mathtools}
%\geometry{margin=1.0in}
\usepackage{tabularx,ragged2e,booktabs,caption}
\usepackage{algorithm}
\usepackage{algorithmicx}
\usepackage{algpseudocode}
\usepackage{comment}
\usepackage{listings}

\newcommand{\bbm}{\begin{bmatrix}}
\newcommand{\ebm}{\end{bmatrix}}
\newcommand\tab[1][1cm]{\hspace*{#1}}

\title{Raptor BSR Guide}

\begin{document}
\maketitle

\section*{General Format}

\begin{itemize}
    \item Assume a matrix of size $m \times n$ and blocks of
          size $bm \times bn$ \\
          where $m \text{ mod } bm = 0  $ and $n \text{ mod } bn = 0$
    \item Defined by 3 arrays
    \begin{itemize}
        \item \textbf{row\_ptr}:
        \begin{itemize}
            \item size: $\frac{m}{bm} + 1$
            \item position i contains the starting index of the ith block row in the cols array
            (block row referring to the block matrix - right matrix in example)
        \end{itemize}
        \item \textbf{cols}:
        \begin{itemize}
            \item size: number of non-zero blocks
            \item position i contains the column index of the ith block within the block matrix
            (right matrix in example)
        \end{itemize}
        \item \textbf{values}:
        \begin{itemize}
            \item size: number of non-zero blocks * number of elements within
                        a block
            \item contains the non-zero entries for the blocks in the order listed in cols, \\
            there is a new block every $bm \times bn$ entries, \\
            each block is stored in a flattened row-major order
        \end{itemize}
    \end{itemize}

    \item Example
    \begin{equation*}
        \setcounter{MaxMatrixCols}{15}
        \bbm
        1 & 0 & 6 & 7 & * & * & * & * & 1 & 0 & * & * \\
        2 & 1 & 8 & 2 & * & * & * & * & 0 & 1 & * & * \\
        * & * & 1 & 4 & * & * & 2 & 0 & * & * & * & * \\
        * & * & 5 & 1 & * & * & 0 & 0 & * & * & * & * \\
        * & * & 4 & 3 & 7 & 2 & * & * & * & * & 3 & 0 \\
        * & * & 0 & 0 & 0 & 0 & * & * & * & * & 1 & 0 \\
        * & * & 1 & 0 & * & * & 1 & 0 & 6 & 7 & * & * \\
        * & * & 0 & 1 & * & * & 2 & 1 & 8 & 2 & * & * \\
        2 & 0 & * & * & * & * & * & * & 1 & 4 & * & * \\
        0 & 0 & * & * & * & * & * & * & 5 & 1 & * & * \\
        * & * & * & * & 3 & 0 & * & * & 4 & 3 & 7 & 2 \\
        * & * & * & * & 1 & 0 & * & * & 0 & 0 & 0 & 0
        \ebm \rightarrow
        \bbm
        A & B & * & * & C & * \\
        * & D & * & E & * & * \\
        * & F & G & * & * & H \\
        * & I & * & J & K & * \\
        L & * & * & * & M & * \\
        * & * & N & * & O & P
        \ebm
    \end{equation*}
    \begin{itemize}
        \item Assuming 2 x 2 non-zero blocks
        \item row\_ptr = \{ 0, 3, 5, 8, 11, 13, 16 \}
        \item cols = \{ 0, 1, 4, 1, 3, 1, 2, 5, 1, 3, 4, 0, 4, 2, 4, 5 \}
        \item values = \{ 1, 0, 2, 1, 6, 7, 8, 2, 1, 0, 0, 1,
                          1, 4, 5, 1, 2, 0, 0, 0, 4, 3, 0, 0,
                          7, 2, 0, 0, \\
                \tab \tab 3, 0, 1, 0, 1, 0, 0, 1, 1, 0, 2, 1,
                          6, 7, 8, 2, 2, 0, 0, 0, 1, 4, 5, 1,
                          3, 0, 1, 0, \\
                \tab \tab 4, 3, 0, 0, 7, 2, 0, 0\}
    \end{itemize}
\end{itemize}

\section*{BSRMatrix Class}

\subsection*{Attributes}
    \begin{itemize}
        \item \textbf{n\_rows} : int \\
            \tab    number of rows in matrix
        \item \textbf{n\_cols} : int \\
            \tab    number of columns in matrix
        \item \textbf{b\_rows} : int \\
            \tab    number of rows in a block
        \item \textbf{b\_cols} : int \\
            \tab    number of columns in a block
        \item \textbf{n\_blocks} : int \\
            \tab    number of non-zero blocks in matrix
        \item \textbf{b\_size} : int \\
            \tab    number of elements in a block
        \item \textbf{nnz} : int \\
            \tab    total number of non-zeros in the matrix
        \item \textbf{idx1} : std::vector \textless int \textgreater \\
            \tab    refers to row\_ptr array given in General Format
        \item \textbf{idx2} : std::vector \textless int \textgreater \\
            \tab    refers to cols array given in General Format
        \item \textbf{vals} : std::vector \textless double \textgreater \\
            \tab    refers to data array given in General Format
    \end{itemize}

\subsection*{Constructors (found in matrix.hpp)}
    \begin{itemize}
        \item \textbf{BSRMatrix(int \_nrows, int \_ncols, int \_brows, int \_bcols, \\
                \tab int \_nblocks=0, int \_nnz=0) : Matrix(\_nrows, \_ncols)}

                    Creates an empty BSRMatrix with \_nrows rows and \_ncols columns. \\
                    Each block is \_brows rows by \_bcols columns. \\
                    If \_brows does not divide \_nrows evenly or \_bcols does not
                    divide \_ncols evenly, it will throw an error.

        \item \textbf{BSRMatrix(int \_nrows, int \_ncols, int \_brows, int \_bcols, \\
                \tab double *\_data) : Matrix(\_nrows, \_ncols)}

                    Creates a BSRMatrix from the dense array \_data. \\
                    \_data is in block order where each block is flattened in row-wise
                    order. \\
                    This is used for testing.

        \item \textbf{BSRMatrix(int \_nrows, int \_ncols, int \_brows, int \_bcols, \\
                \tab std::vector \textless int \textgreater\& rowptr,
                     std::vector \textless int \textgreater\& cols, \\
                \tab std::vector \textless double \textgreater\& data) :
                     Matrix(\_nrows, \_ncols)}

                    Creates a BSRMatrix with the given rowptr, cols, and data array as the
                    idx1, idx2, and vals arrays.

        \item \textbf{BSRMatrix(const COOMatrix* A, int \_brows, int \_bcols)}

                    Creates a BSRMatrix from a COOMatrix* with blocks of size
                    \_brows by \_bcols.

        \item \textbf{BSRMatrix(const CSRMatrix* A, int \_brows, int \_bcols)}

                    Creates a BSRMatrix from a CSRMatrix* with blocks of size
                    \_brows by \_bcols.
    \end{itemize}

\subsection*{Methods (found in matrix.hpp and matrix.cpp)}
    \begin{itemize}
        \item \textbf{print()}

            Prints the non-zero blocks of the matrix in block order.

        \item \textbf{block\_print(int row, int num\_blocks\_prev, int col)}

            Helper function for print(). It prints a given block. \\
            row : the row of the block in the block matrix \\
            col : the column of the block in the block matrix \\
            num\_blocks\_prev : the number of blocks previously printed - used to \\
                \tab calculate the offset for where this block is in the vals array

        \item \textbf{to\_dense()}

            returns : std::vector \textless double \textgreater \\
            Converts the block matrix to a dense array. \\
            This is used for testing.

        \item \textbf{add\_block(int row, int col,
            std::vector \textless double \textgreater\& values)}

            Adds the block given in values to the BSRMatrix at position (row, col)
            in the block matrix. Blocks do not need to be added in any order.\\
            row : the row of the block in the block matrix \\
            col : the column of the block in the block matrix \\
            values : the data for the block given in row-major order

        \item \textbf{copy(const COOMatrix* A)}

            Copies the given COOMatrix*.

        \item \textbf{copy(const CSRMatrix* A)}

            Copies the given CSRMatrix*.

        \item \textbf{copy(const BSRMatrix* A)}

            Copies the given BSRMatrix*.

        \item \textbf{mult(std::vector \textless double \textgreater\& x,
                std::vector \textless double \textgreater\& b)}

            Performs standard multiplicaion of the BSRMatrix with x and stores
            the solution in b.

        \item \textbf{mult\_T(std::vector \textless double \textgreater\& x,
                std::vector \textless double \textgreater\& b)}

            Performs standard multiplication of the transpose of the BSRMatrix
            with x and stores the solution in b.

        \item \textbf{mult\_append(std::vector \textless double \textgreater\& x,
                std::vector \textless double \textgreater\& b)}

            Helper function for mult().

        \item \textbf{block\_mult( int row, int num\_blocks\_prev, int col, \\
            \tab std::vector \textless double \textgreater\& x,
                 std::vector \textless double \textgreater\& b)}

            Block helper function for mult() that performs multiplication on the
            block at (row, col) in the block matrix with x and stores the solution in b.

        \item \textbf{mult\_append\_T(std::vector \textless double \textgreater\& x,
                std::vector \textless double \textgreater\& b)}

            Helper function for mult\_T().

        \item \textbf{block\_mult\_T( int row, int num\_blocks\_prev, int col,  \\
            \tab std::vector \textless double \textgreater\& x,
                 std::vector \textless double \textgreater\& b)}

            Block helper function for mult\_T() that performs transpose multiplication
            on the block at (row, col) in the block matrix with x and stores the
            solution in b.

        \item \textbf{mult\_append\_neg(std::vector \textless double \textgreater\& x,
                std::vector \textless double \textgreater\& b)}

            Helper function to subtract BSRMatrix times x from b.

        \item \textbf{block\_mult\_neg( int row, int num\_blocks\_prev, int col \\
            \tab std::vector \textless double \textgreater\& x,
                 std::vector \textless double \textgreater\& b)}

            Block helper function for mult\_append\_neg() that multiplies the block
            at (row, col) in the block matrix by x and subtracts the result from b.

        \item \textbf{mult\_append\_neg\_T(std::vector \textless double \textgreater\& x,
                std::vector \textless double \textgreater\& b)}

            Helper function that subtracts the transpose of the BSRMatrix times x
            from b.

        \item \textbf{block\_mult\_neg\_T( int row, int num\_blocks\_prev, int col, \\
            \tab std::vector \textless double \textgreater\& x,
                 std::vector \textless double \textgreater\& b)}

            Block helper function for mult\_neg\_T().

        \item \textbf{residual(const std::vector \textless double \textgreater\& x,
                const std::vector \textless double \textgreater\& b, \\
                \tab std::vector \textless double \textgreater\& r)}

            Calculates the residual of the BSRMatrix times x and b, then stores the
            solution in r.

        \item \textbf{block\_res( int row, int num\_blocks\_prev, int col, \\
            \tab const std::vector \textless double \textgreater\& x,
                 std::vector \textless double \textgreater\& r)}

            Block helper function for residual() that calculates the residual on the
            block at (row, col) in the block matrix. Stores the solution in r.

        \item \textbf{format()}

            Returns 'BSR' as the format

        \item \textbf{row\_ptr()}

            Returns a reference to idx1 - std::vector \textless int \textgreater\&

        \item \textbf{cols()}

            Returns a reference to idx2 - std::vector \textless int \textgreater\&

        \item \textbf{data()}

            Returns a reference to vals - std::vector \textless double \textgreater\&

        \item \textbf{block\_rows()}

            Returns b\_rows - number of rows in block - int.

        \item \textbf{block\_cols()}

            Returns b\_cols - number of columns in block - int.

        \item \textbf{block\_size()}

            Returns b\_size - number of values in a non-zero block - int.

        \item \textbf{num\_blocks()}

            Returns n\_blocks - the number of non-zero blocks in the matrix - int.
    \end{itemize}

\subsection*{Test Files}
\begin{itemize}
    \item raptor/raptor/core/tests/test\_bsr\_matrix.cpp
    \item raptor/raptor/util/tests/test\_bsr\_spmv\_aniso.cpp
    \item raptor/raptor/util/tests/test\_bsr\_spmv\_laplacian.cpp
    \item raptor/raptor/util/tests/test\_bsr\_spmv\_random.cpp
\end{itemize}

\subsection*{Usage Example}

Create the following matrix with 2x2 blocks 2 different ways:

\begin{equation*}
    \bbm 1 & 0 & 6 & 7 & * & * \\
         2 & 1 & 8 & 2 & * & * \\
         * & * & 1 & 4 & * & * \\
         * & * & 5 & 1 & * & * \\
         * & * & 4 & 3 & 7 & 2 \\
         * & * & 0 & 0 & 0 & 0 \ebm
\end{equation*}

\begin{lstlisting}[language=C,label=lst:bsr_mat1,caption=Creating a BSRMatrix]
std::vector<int> row_ptr = {0, 2, 3, 5};
std::vector<int> cols = {0, 1, 1, 1, 2};
std::vector<double> vals = {1., 0., 2., 1., 6., 7., 8., 2., 1., 4.,
                            5., 1., 4., 3., 0., 0., 7., 2., 0., 0.}

BSRMatrix A(6, 6, 2, 2, row_ptr, cols, vals);
\end{lstlisting}

\begin{lstlisting}[language=C,label=lst:bsr_mat1,caption=Creating a BSRMatrix]
BSRMatrix A(6, 6, 2, 2);

std::vector<double> block1 = {1., 0., 2., 1.};
std::vector<double> block2 = {6., 7., 8., 2.};
std::vector<double> block3 = {1., 4., 5., 1.};
std::vector<double> block4 = {4., 3., 0., 0.};
std::vector<double> block5 = {7., 2., 0., 0.};
A.add_block(0, 0, block1);
A.add_block(0, 1, block2);
A.add_block(1, 1, block3);
A.add_block(2, 1, block4);
A.add_block(2, 2, block5);
\end{lstlisting}

Mutiplication with the above BSRMatrix:

\begin{lstlisting}[language=C,label=lst:bsr_mat1,caption=Multiplication with a BSRMatrix]
Vector x(A->n_rows);
Vector b(A->n_rows);

x.set_const_value(1.0);
A.mult(x, b); // The solution to A * x = b is now stored in b

\end{lstlisting}

\section*{ParBSRMatrix Class}

\subsection*{Attributes}
\begin{itemize}
    \item \textbf{global\_num\_rows} : index\_t \\
        \tab    global number of rows in the matrix

    \item \textbf{global\_num\_cols} : index\_t \\
        \tab    global number of columns in the matrix

    \item \textbf{local\_nnz} : int \\
        \tab    number of non-zeros on the process

    \item \textbf{local\_num\_rows} : int \\
        \tab    number of rows of the global matrix stored on the process

    \item \textbf{on\_proc\_num\_cols} : int \\
        \tab    number of columns in the on\_proc matrix

    \item \textbf{off\_proc\_num\_cols} : int \\
        \tab    number of columns in the off\_proc matrix

    \item \textbf{comm} : ParComm* \\
        \tab    parallel communicator for communicating off\_proc

    \item \textbf{tap\_comm} : ParComm* \\
        \tab    parallel communicator for topology aware communication

    \item \textbf{b\_rows} : int \\
        \tab    number of rows in a block

    \item \textbf{b\_cols} : int \\
        \tab    number of columns in a block

    \item \textbf{b\_size} : int \\
        \tab    number of elements in a block

    \item \textbf{on\_proc} : BSRMatrix* \\
        \tab    diagonal matrix for on process computation

    \item \textbf{off\_proc} : BSRMatrix* \\
        \tab    matrix containing all off-diagonal columns for off process \\
        \tab    communication and computation
\end{itemize}

\subsection*{Constructors}
\begin{itemize}
    \item \textbf{ParBSRMatrix() : ParMatrix()}

        Creates an empty ParBSRMatrix.

    \item \textbf{ParBSRMatrix(index\_t glob\_rows, index\_t glob\_cols,
                    int \_brows, int \_bcols, int blocks\_per\_row=5) :
                    ParMatrix(glob\_rows, glob\_cols, \_brows, \_bcols)}

        Creates an empty ParBSRMatrix with glob\_rows and glob\_cols for
        rows and columns. The blocks are of size \_brows by \_bcols.

        * RECOMMENDED CONSTRUCTOR

    \item \textbf{ParBSRMatrix(Partition* part, int \_brows, int \_bcols,
                    int blocks\_per\_row = 5) : ParMatrix(part)}

        Creates a ParBSRMatrix with the given partition and blocks of size
        \_brows by \_bcols. - not tested

    \item \textbf{ParBSRMatrix(Partition* part, BSRMatrix* \_on\_proc,
                    BSRMatrix* \_off\_proc) : ParMatrix(part)}

        Creates a ParBSRMatrix with the given partition and on and off
        process matrices. - not tested

    \item \textbf{ParBSRMatrix*(Partition* part, index\_t glob\_rows, index\_t glob\_cols,
                    int local\_rows, int \_brows, int \_bcols, int on\_proc\_cols,
                    int off\_proc\_cols, int blocks\_per\_row = 5) :
                    ParMatrix(part, glob\_rows, glob\_cols, local\_rows, on\_proc\_cols)}

        Creates a ParBSRMatrix with the given partition, global rows and global columns,
        local rows, blocks of size \_brows by \_bcols, and the given number of
        on and off process columns. - not tested
\end{itemize}

\subsection*{Methods}
\begin{itemize}
    \item \textbf{add\_block(int global\_coarse\_row, int global\_col\_coarse,
                    std::vector \textless double \textgreater \& data)}

        Adds the block at (global\_coarse\_row, global\_col\_coarse) to the appropriate
        process and inserts data into the on or off process matrix accordingly.

        global\_row\_coarse : the row of the block in the global block matrix \\
        global\_col\_coarse : the column of the block in the global block matrix \\
        data : the data for the block given in row-major order

    \item \textbf{mult(ParVector\& x, ParVector\& b)}

        Multiplies the ParBSRMatrix by x and stores the result in b.

    \item \textbf{tap\_mult(ParVector\& x, ParVector\& b)}

        Topology aware multiplication - has not been tested.

    \item \textbf{mult\_T(ParVector\& x, ParVector\& b)}

        Multiplication by transpose of ParBSRMatrix - has not been tested.

    \item \textbf{tap\_mult\_T(ParVector\& x, ParVector\& b)}

        Topology aware transpose multiplication - has not been tested.
\end{itemize}

\subsection*{Updates}
\begin{itemize}
    \item \textbf{ParMatrix(index\_t glob\_rows, index\_t glob\_cols, int \_brows, int\_bcols)}

    * in par\_matrix.hpp

    Creates a ParMatrix corresponding to a ParBSRMatrix. This constructor makes sure to
    call the appropriate Partition() constructor so that the blocks do not get
    split across processes.

    \item \textbf{Partition(index\_t \_global\_num\_rows, index\_t \_global\_num\_cols,
                    index\_t \_brows, index\_t \_bcols, Topology* \_topology = NULL)}

    * in partition.hpp

    Partitions the matrix such that the blocks do not get split across processes.

    \item \textbf{finalize(bool create\_comm, int b\_cols = 0)}

    * in par\_matrix.cpp

    Creates the communicators for a given ParMatrix based on the off process column map.
    If b\_cols is not zero, then the matrix is a ParBSRMatrix, meaning that
    expand\_off\_proc() should be called.

    This method MUST be called after creating a ParBSRMatrix to ensure that the
    communicators are created correctly.

    \item \textbf{expand\_off\_proc(int b\_cols)}

    * in par\_matrix.cpp

    The condense\_off\_proc() method condenses the off process column map to only include
    the columns with non-zero entries. Because of the storage format of the
    BSRMatrix, this condenses the off process column map to include the block
    columns from the block matrix - this function corrects that. It expands the
    off process column map to include all of the global columns that correspond
    to a given non-zero block.
\end{itemize}

\subsection*{Test Files}
\begin{itemize}
    \item raptor/raptor/core/tests/test\_par\_bsr.cpp
    \item raptor/raptor/util/tests/test\_par\_bsr\_spmv.cpp
\end{itemize}

\subsection*{Usage Example}

To create a ParBSRMatrix for the matrix given in the original example:

\begin{lstlisting}[language=C,label=lst:bsr_mat1,caption=Creating a ParBSRMatrix]
// Create an empty ParBSRMatrix
ParBSRMatrix* A = new ParBSRMatrix(12, 12, 2, 2);

// Then add each block - they do not need to be added in any particular order
std::vector<std::vector<double>> blocks1 = {{1,0,2,1}, {6,7,8,2}, {1,4,5,1},
                                           {4,3,0,0}, {7,2,0,0}};
std::vector<std::vector<double>> blocks2 = {{1,0,0,1}, {2,0,0,0}, {3,0,1,0}};

A->add_block(0, 0, blocks1[0]);
A->add_block(3, 3, blocks1[0]);
A->add_block(0, 1, blocks1[1]);
A->add_block(3, 4, blocks1[1]);
A->add_block(1, 1, blocks1[2]);
A->add_block(4, 4, blocks1[2]);
A->add_block(2, 1, blocks1[3]);
A->add_block(5, 4, blocks1[3]);
A->add_block(2, 2, blocks1[4]);
A->add_block(5, 5, blocks1[4]);

A->add_block(0, 4, blocks2[0]);
A->add_block(3, 1, blocks2[0]);
A->add_block(1, 3, blocks2[1]);
A->add_block(4, 0, blocks2[1]);
A->add_block(2, 5, blocks2[2]);
A->add_block(5, 2, blocks2[2]);

// Call finalize to create on and off process maps
// and to create communicators
// You MUST include true and number of columns in a block
// to ensure that the communicators are created correctly
A->finalize(true, 2);

\end{lstlisting}

Multiplication with the ParBSRMatrix* given above:

\begin{lstlisting}[language=C,label=lst:bsr_mat1,caption=Creating a BSRMatrix]
ParVector x(A->global_num_cols, A->on_proc_num_cols, A->partition->first_local_col);
ParVector b(A->global_num_rows, A->local_num_rows, A->partition->first_local_row);

x.set_const_value(1.0);
A->mult(x, b); // The solution to A * x = b is now stored in b

\end{lstlisting}

\end{document}


\section{Calculating Ordinary Least Squares (OLS) estimators}


%Suggested order of slides:

%1 slides-regression-losses
%2 slides-regression-linearmodel
%3 slides-regression-polynomials

\subsection{Loss Functions for Regression}
\includepdf[pages=-]{../slides-pdf/slides-regression-losses.pdf}

\subsection{Linear Regression Models}
\includepdf[pages=-]{../slides-pdf/slides-regression-linearmodel.pdf}

\subsection{Polynomial Regression Models}
\includepdf[pages=-]{../slides-pdf/slides-regression-polynomials.pdf}


\documentclass{ximera}
\input{../preamble}
\title{The Integral Test}
%%%%%\author{Philip T. Gressman}

\begin{document}
\begin{abstract}
  We study the integral test for infinite series and related concepts.
\end{abstract}
\maketitle

\section*{(Video) Calculus: Single Variable}
\youtube{TNcr4MAKR1s}

\section*{Online Texts}
\begin{itemize}
\item \link[OpenStax II 5.3: The Integral Test]{https://openstax.org/books/calculus-volume-2/pages/5-3-the-divergence-and-integral-tests}
\item \link[Ximera OSU: The Integral Test]{https://ximera.osu.edu/mooculus/calculus2/integralTest/titlePage} and \link[Remainders for the Integral Test]{https://ximera.osu.edu/mooculus/calculus2/remainders/digInRemaindersIntegralTest}
\item \link[Community Calculus 11.3: The Integral Test]{https://www.whitman.edu/mathematics/calculus_online/section11.03.html}
\end{itemize}

\section*{Examples}

\begin{example}

\end{example}

\begin{example}

\end{example}

\end{document}


\documentclass[a4paper]{article}
\input{style/head.tex}

\usepackage{listings}
\usepackage{multirow}

\newcommand{\yourname}{Schweizerschule Mexiko, CDMX} % replace YOURNAME with your name
\newcommand{\yournetid}{M.Sc. William Bombardelli} % replace YOURNETID with your NetID
\newcommand{\youremail}{wbombardellis@gmail.com} % replace YOUREMAIL with your email
\newcommand{\assignmentnumber}{8} % replace X with assignment number
\newcommand{\coursename}{Java - While} % replace X with assignment number

\begin{document}

	\input{style/header.tex}

	\section{Exercises}
	\begin{enumerate}
		\item Read the following solutions for the Fibonacci problem.
		\begin{lstlisting}[language=Java, basicstyle=\scriptsize]
public static void main(String[] args) {
	int prev;
	int num = 0;
	int next = 1;

	while (num <= 1000) {
		System.out.println(num);

		prev = num;
		num = next;
		next = prev + num;
	}

	System.out.println("=====================");
	////////// Alternative solution \\\\\\\\\\\\\

	int previousPrevious = 0;
	int previous = 1;
	int n = previousPrevious + previous;

	System.out.println(previousPrevious);
	System.out.println(previous);

	while(n <= 1000) {
		System.out.println(n);

		previousPrevious = previous;
		previous = n;
		n = previousPrevious + previous;
	}
}
		\end{lstlisting}
		Now complete the following table with the values of the variables $prev$, $num$ and $next$ at each iteration.
		\begin{table}[h!]
			\centering
			\begin{tabular}{| c | c | c | c |}
				\hline
				Iteration & $prev$ & $num$ & $next$ \\
				\hline
				1 & & & \\
				2 & & & \\
				3 & & & \\
				4 & & & \\
				5 & & & \\
				6 & & & \\
				7 & & & \\
				\hline
			\end{tabular}
		\end{table}
		And, finally, finish your own solution, if you have not done that yet.
		\item Write a program that reads the grades of ten students and print at the end the greatest note, the least note, the average note (arithmetic mean) and how many students passed ($grade \ge 7$).
		\item Write a program that tells whether a number is prime or not. A prime number is a natural number greater than 1 that is only divisible by 1 and by itself.
	\end{enumerate}

	\section{References}
	\begin{itemize}
		\item W3C Tutorial:
		\begin{itemize}
			\item \url{https://www.w3schools.com/java/java_while_loop.asp}
		\end{itemize}
		\item Extra Exercises: \url{https://www.w3schools.com/java/exercise.asp}
		\begin{itemize}
			\item Java Loops
		\end{itemize}
		\item Solutions: \url{https://github.com/wbombardellis/java-unterricht/tree/master/Programme/08}
	\end{itemize}
\end{document}