\subsection{Terminology, notation, and the inverse problems}
We provide a summary of the notation, definitions, problem-formulation, and assumptions that reoccur throughout this work.
For more details on the original sources and derivations,  we refer the interested reader to \cite{BES12, BE13, BET+14, BJW18a, BWY20}.
To make comparisons more clear, we first introduce shared notation between the SIP and Bayesian inverse problems.

Let $u$ be the solution to a model, mathematically represented by $\M(u, \param) = 0$, where $\param$ represents a parameter into such a model, e.g. the permeability of the medium in the subsurface through which a contaminant is spreading.
Such parameters are often uncertain, and we begin the quantification of uncertainty by identifying the set of all physically plausible parameters denoted by $\pspace\subset\RR^\dimP$.
Since different choices of $\param \in \pspace$ often lead to different model solutions, we write $u\lam$ to make this dependence on the parameter space explicit.

In general, we cannot observe the entire solution $u(\param)$ due to practical limitations.
For example, one cannot observe air pressure at every point throughout a room, but one can perform experiments and take measurements to infer pressure at specific locations within the room.
Put more precisely, we are often limited in our ability to observe data related to some QoI that are mathematically defined as functionals of $u\lam$.
We let $\qoi$ denote the (potentially vector-valued) QoI map from the solution space of the model to the space of observable data.

Then, given $\param \in \pspace$, we obtain $u\lam$ and compute $\qoi(u\lam)$ to get the QoI predicted by the model.
The QoI map depends on $\param$ through the dependency of $u$ on $\param$, so we write $\qlam$ to simplify our notation.
We generally assume this map is at least piecewise-differentiable.
The data space $\dspace \subset \RR^\dimD$ is defined as the range of the QoI map $\qoi$, i.e.
\[
\dspace = \qoi(\pspace).
\]
In other words, we use $\dspace$ to denote the space of all physically plausible data for the QoI that the model can predict.

Let $\pborel$ and $\dborel$ denote (the Borel) $\sigma$-algebras on $\pspace$ and $\dspace$, respectively.
A $\sigma$-algebra is a collection of subsets representing the set of all measurable events, i.e., events for which it makes sense to assign a probability.
The map $\qoi$ between measurable spaces $(\pspace, \pborel)$ and $(\dspace, \dborel)$ is immediately measurable by the smoothness assumption.
Then, equipping $\pspace$ and $\dspace$ with (dominating) measures $\pmeas$ and $\dmeas$, respectively, is the final necessary component for constructing the Radon--Nikodym derivatives defining probability density functions (pdfs) from probability measures defined on the measure spaces $(\pspace, \pborel, \pmeas)$ and $(\dspace, \dborel, \dmeas)$.
In practice, $\pmeas$ and $\dmeas$ are often taken to be Lebesgue measures when $\pspace$ and $\dspace$ are finite-dimensional~\cite{BET+14, BJW18a}.
In general, these measure allow for the description of commonly known probability measures as familiar pdfs.

\subsection{Problem Formulation and Solution}
We begin with defining the types of forward and inverse problems considered in this thesis.

\begin{defn}[Stochastic Forward Problem (SFP)]\label{defn:forward-problem}
  Given a probability measure $\PP_\pspace$ on $(\pspace, \pborel)$, and QoI map $\qoi$, the \emph{stochastic forward problem} is to determine a measure, $\PP_\dspace$, on $(\dspace, \dborel)$ that satisfies
  \begin{equation}\label{eq:forward-problem}
    \PP_\dspace (E) = \PP_\pspace \left ( \qoi^{-1}(E) \right ), \; \forall \; E \in \dborel.
  \end{equation}
\end{defn}

\begin{defn}[Stochastic Inverse Problem (SIP)]\label{defn:inverse-problem}
  Given a probability measure, $\PP_\dspace$, on $(\dspace, \dborel)$ the \emph{stochastic inverse problem} is to determine a probability measure, $\PP_\pspace$, on $(\pspace, \pborel)$ satisfying
  \begin{equation}\label{eq:inverse-problem}
    \PP_\pspace (\qoi^{-1}(E)) = \PP_\dspace(E), \; \forall \; E \in \mathcal{B}_\dspace.
  \end{equation}

  \noindent Any probability measure $\PP_\pspace$ satisfying \eqref{eq:inverse-problem} is referred to as a \emph{consistent solution} to the inverse problem, and \eqref{eq:inverse-problem} is referred to as the \emph{consistency condition}.
  If $\PP_\pspace$ or $\PP_\dspace$ are absolutely continuous with respect to $\pmeas$ or $\dmeas$, respectively, then we write

  \begin{equation*}
    \pp_\pspace := \frac{d\PP_\pspace}{d\pmeas} \;\text{ or }\; \pp_\dspace := \frac{d\PP_\dspace}{d\dmeas}
  \end{equation*}
  to denote the Radon-Nikodym derivatives (i.e., pdfs) of $\PP_\pspace$ and $\PP_\dspace$, respectively.
  In such a case, we can rewrite \eqref{eq:forward-problem} and \eqref{eq:inverse-problem} using these pdfs. For example, here is a variant of \eqref{eq:inverse-problem} using these pdfs:

  \begin{equation*}
  \PP_\pspace (\qoi^{-1}(E)) = \int_{\qoi^{-1}(E)} \pp_\pspace \lam \, d\pmeas = \int_E \pp_\dspace \Q \, d\dmeas = \PP_\dspace(E), \; \forall \; E \in \mathcal{B}_\dspace.
  \end{equation*}
\end{defn}

\subsubsection{The Stochastic Inverse Problem (SIP)}

In measure-theoretic terms, $\PP_\dspace$ in Definition~\ref{defn:forward-problem} is a push-forward measure of $\PP_\pspace$, and in Definition~\ref{defn:inverse-problem}, $\PP_\pspace$ is a pull-back measure of $\PP_\dspace$.
From the perspective of a forward problem, we seek $\PP_\pspace$ such that its \emph{push-forward measure is equivalent to} $\PP_\dspace$.
In other words, \emph{the solution we seek to the inverse problem is constrained by a forward problem.}
Below, we formalize some of the vocabulary involved in the formulation and solution of the SIP.
We refine the concept of push-forward measures as solutions to the SFP mentioned in the introduction, formally introducing the requisite vocabulary of \emph{initial}, \emph{observed}, and \emph{predicted} densities.
This helps frame the SIP more clearly as the direct inversion of the SFP.

\begin{defn}[Observed Distribution]\label{defn:observed}
  When the measure $\PP_\dspace$ in \eqref{eq:inverse-problem} is defined by the quantitative characterization of uncertainty in the QoI data, it is referred to as the \emph{observed measure}, $\observedP$.
  If a dominating measure $\mu_\dspace$ exists on $(\dspace, \dborel)$, the \emph{observed density} $\observed$ is given by the Radon-Nikodym derivative of $\observedP$ with respect to the measure $\dmeas$.
\end{defn}

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

The map $\qoi$ impacts the structure of any solution to the SIP since the underlying data space $\dspace$ itself depends on $\qoi$.
In the event that the map $\qoi$ is a bijection, then the consistency condition \eqref{eq:inverse-problem} defines a unique measure $\PP_\pspace$ given the specification of an observed density.
However, there are many applications of interest where $\qoi$ fails to be a bijection, either due to differences in the dimensions of the parameter and data spaces, nonlinearities inherent in the model itself, or both.

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

Therefore, we do not generally expect that there is a unique $\mathbb{P}_\pspace$ solving the SIP in Definition~\ref{defn:inverse-problem}, but rather there is a class of pullback measures that solve the SIP.
In \cite{BET+14}, a disintegration theorem \citep{Chang_Pollard, Dellacherie_Meyer_book} along with an ansatz is used to establish the existence of solutions to the SIP that are unique up to the choice of ansatz.
An algorithm is provided in \cite{BET+14} for explicitly approximating pullback measures by applying a specified ansatz to approximations of contour events, i.e., approximations of $Q^{-1}(E_i)$ where $\set{E_i}_{i\in\mathcal{I}}$ is a partitioning of $\dspace$ according to some (finite) index set $\mathcal{I}$.
In \cite{BJW18a}, a density-based approach is presented that is computationally simpler to implement, and scales well with increasing parameter dimension
% The solution to the SIP presented there is a direct inversion of a SFP; we introduce the following definitions to connect the result to general forms presented in \ref{defn:forward-problem} and \ref{defn:inverse-problem}:
The density-based approach makes explicit use of a solution to the SFP in constructing a solution to the SIP.
We make use of the following definitions in this approach.

\begin{defn}[Initial Distribution]\label{defn:initial}
  When the measure $\PP_\pspace$ in \eqref{eq:forward-problem} is defined by the quantitative characterization of uncertainty in parameter variability before observations on QoI are taken into account, it is referred to as the initial measure $\initialP$.
  If a dominating measure $\mu_\pspace$ exists on $(\pspace, \pborel)$, the \emph{initial distribution} $\initial$ is given by the Radon-Nikodym derivative of $\initialP$ with respect to the measure $\pmeas$.
\end{defn}

To construct a density-based solution to the SIP, we first push-forward the initial density using the QoI map.
In other words, we first solve the SFP of \eqref{eq:forward-problem}.
We refer to the push-forward of the initial measure as the \emph{predicted measure} since it may be constructed before any observed data are known.
This also helps to distinguish it from the {\em observed} measure used in the formulation of the SIP.
To make this precise, we use the following:

\begin{defn}[Predicted Distribution]\label{defn:predicted}
  The push-forward density of $\initial$ under the map $\qoi$ is denoted as $\predicted$, and is referred to as the \emph{predicted distribution} (or density).
  It is given as the Radon-Nikodym derivative (with respect to $\dmeas$) of the push-forward probability measure \eqref{eq:forward-problem} given by
  \begin{equation}\label{eq:predicted}
    \predictedP (E) = \initialP \left ( \qoi^{-1}(E) \right ), \; \forall \; E \in \dborel.
  \end{equation}
\end{defn}

%%%%%%%%%%%%%%%%%%%
We now have all of the definitions required to summarize the density-based solution to the SIP, known as the \emph{updated density} as:
\begin{equation}\label{eq:updated-pdf}
	\updated(\param) := \initial(\param)\frac{\observed(Q(\param))}{\predicted(Q(\param))}.
\end{equation}

%%%%%%%%%%%%%%%%%%%
We refer the interested reader to \cite{BJW18a} for the theoretical and algorithmic details of implementing the solution to the SIP, though some are summarized in \ref{sec:properties}.
For now, we note that the solution in \eqref{eq:updated-pdf} is stable with respect to perturbations in the initial and observed probability measures, and that the solution given by \eqref{eq:updated-pdf} requires only the forward-problem construction of $\predicted$, since $\initial$ and $\observed$ are specified in the SIP.
Additional properties of $\updated$ are given in \ref{sec:properties} alongside the conditions for the existence and uniqueness of $\updated$ of the form given by \eqref{eq:updated-pdf}.
%%%%%%%%%%%%%%%%%%%

In order to ensure that $\updated$ is in fact a density, a predictability assumption is required \cite{BJW18a}.
A practical form of the predictability assumption is that there exists a constant $C>0$ such that $\observed(q)\leq C\predicted(q)$ for $\text{ a.e. } q\in\dspace$.
Conceptually, we interpret the predictability assumption as stating that we are able to predict the observed data.
This also helps to frame the special role of $\initial$ in the SIP compared to the role of the prior density used in the Bayesian inverse problem that is discussed below.
Specifically, $\initial$ allows us to perform (1) robust predictions, and (2) define a particular data-consistent solution.

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

\subsubsection{The Deterministic Inverse Problem (DIP)}
A typical Bayesian approach to an inverse problem focuses on first modeling epistemic uncertainties in data on a QoI obtained from a true, but unknown, parameter value, which we denote by $\paramref$.
This is in contrast to the SIP and its data-consistent solutions that are defined as pullback measures of an observed probability measure on the QoI.
To make the distinction between the two approaches more clear, we introduce the following two definitions to frame the problems addressed by the Bayesian framework:

\begin{defn}[Deterministic Forward Problem (DFP)]
  Given a space $\pspace$, and QoI map $\qoi$, the \emph{deterministic forward problem} is to determine the values, $\q \in \dspace$ that satisfy
  \begin{equation}
    \q = \qlam, \; \forall \; \param \in \pspace.
  \end{equation}
\end{defn}

\begin{defn}[Deterministic Inverse Problem (DIP) Under Uncertainty]
  Given a noisy datum (or data-vector) $d = \q + \xi$, $\q \in \dspace$, the \emph{deterministic inverse problem} is to determine the parameter $\param \in \pspace$ which minimizes
  \begin{equation}
    \norm{\qoi(\param) - d}
  \end{equation}
  where $\xi$ is a random variable (or vector) drawn from a distribution characterizing the uncertainty in observations due to measurement errors.
\end{defn}

In the above definition, $\xi$ is some unobservable perturbation to the true output, arising from epistemic uncertainty (e.g. the precision of available measurement equipment).
The Bayesian inversion framework  is perhaps the most popular approach in the UQ community for incorporating uncertainties in inverse solutions.
As mentioned in the introduction, the data-consistent framework developed in \cite{BJW18a, BJW18b, BWY20} is designed to quantify aleatoric sources of uncertainty while the typical Bayesian framework \citep{0266-5611-7-5-003,
 Kennedy_O_JRSSSB_2001, MNR07, CDS10, starktenorio,
 AlexanderianPetraStadlerEtAl14, Bui-ThanhGhattas14, Ernst2014,
 0266-5611-30-11-110301, ROM:CMW_2016, Stuart10,
 cockayneoatessullivangirolami} is designed to quantify epistemic sources of uncertainty.
These conceptual differences have significant impacts on the solutions to inverse problems formulated within these distinctive frameworks.
% To help build intuition about these differences, we summarize key details about the SIP and its solution before presenting an example that highlights differences in solutions.
We provide more details in Section~\ref{sec:compare} to further clarify these impacts for the reader.
%An example is then used to illustrate the differences, which is also helpful for building intuition.
Moreover, the details provided below play a vital role in Section~\ref{sec:estimation} where features of the data-consistent framework are used to motivate its extension to parameter estimation problems.

\FloatBarrier


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{fullpage}
\usepackage{amsmath}
\usepackage{amsmath}

%\usepackage{amsymbols}

\begin{document}
\section{Review}
Change of Bases:
$$
\mathcal{B} \{b_1, b_2, \dots b_n \} \longrightarrow B \{b_1|b_2\dots|b_n\}
$$
$$
\mathcal{C} \{c_1, c_2, \dots c_n \} \longrightarrow C \{c_1|c_2\dots|c_n\}
$$

Diagnolization:
$$
A \in C^{n \times n}
$$

$A$ is diagnolizable if $\exists$ $X \in C^{n\times n}$ duch that $det (X) \neq 0$

$v \in C^{n \times n}$

$ [v] = B[v]_B$

$[v]_B = B^{-1}[v]$

$[v]_C = C^{-1}B[v]_B$

$A = X\Lambda X^{-1}$ , $\Lambda = diag(\lambda_1, \lambda_2, \dots \lambda_n)$
$\Lambda = X^{-1}AX$

Diagnolization is like change of basis

$$
A[v] = X\Lambda X^{-1}[v]/[v]_X = [\Lambda v]_X/[\Lambda v]
$$

\textbf{Claim: }If spectrum of $A$, $card(\sigma(A)) = n i.e.(\lambda_i \neq \lambda_j, i \neq j)$ then $A$ is diagonizable(non-defective)

\textbf{Proof:} Do induction on \# of eigen vector

$\lambda_1 \neq \lambda_2$
$c_1\vec{X}_1 + c_2\vec{X}_2 = 0$; $c_1,c_2 \neq 0$

$c_1A\vec{X}_1 + c_2A\vec{X}_2 =0$

$c_1\lambda_1\vec{X}_1 + c_2\lambda_2\vec{X}_2 =0$

$-c_1\lambda_2\vec{X}_1 + c_2\lambda_2\vec{X}_2 =0$

 $c_1(\lambda_1-\lambda_2)\vec{X}_1=0$ $->$ Contradiction

 Assume truth for $k=1$

 $\sum_{i=1}^k c_i \vec{X}_i = 0$ $c_i is not zero$

 In particular, at least one of $c_i$ is not zero

 \begin{align*}
\sum_{i}c_i\lambda_i X_i &= 0\\
0 &= \lambda_k 0 - 0\\
&= \lambda_k \sum c_iX_o - \sum_i c_i \lambda_i X_i\\
&= \sum_i c_i(\lambda_k - \lambda_i)X_i\\ contradiction
\end{align*}

$X_i$ are independent, hence follows.

\section{Interpretation}
Assume $A \in C^{n \times n} \text{ and } \vec{b} \in C^n$ ; $det(A) \neq 0$

$AX = \vec{b}$

Best case:
\begin{itemize}
\item $A$ is diagnolizable
\item $A$ is triangular(upper/lower)
\end{itemize}

Properties:
\begin{itemize}
\item Production of 2 upper triangle is upper triangular
\item Inverse of non singular upper triangular is upper triangular
\end{itemize}

Argument of (2):
\begin{align*}
SX &= I\\
[SX_1, SX_2, \dots, SX_n] &= [e_1,e_2, \dots e_n] (Std basis)\\
S\vec{X}_i &= \vec{e}_i
\end{align*}

Elementary row operations

\begin{itemize}
\item R1 Multiply on RHS by a constant
\item R2 Exchanging two rows
\item R3 add non zero multiple of one to another
\end{itemize}

(R2)Permutation matrix: exactly one 1 in row or column -> Not lower or upper traingular. But holds for R1,R3

LU decomposition $[L_1, L_2,L3]A = U$ $\hat{L}A = U$
$A = LU$
$L=\hat{L}^{-1}$

$AX =b -> Ly= b -> UX=y$

Two factorization:
1. $A = X\Lambda X^{-1}$ when $X$ is non-defective
2. $A = LU$, when $A$ is square and not of permutation type.

\section{Norms}

Absolute value:
\begin{align*}
|a| &\geq 0 \\
|a| &=0 -> a=0,\\
|ab| & = |a||b|\\
|x+y| \leq |x|+|y|
\end{align*}

Norm:
\begin{itemize}
\item Norm is  mapping $||. || : V -> R$ $V over C$
\item ||0||: V ->R
\item $||\vec{v}|| \geq 0, ||v|| = 0$ iff $v=0$
\item $||c\vec{v}|| = |c|||\vec{v}||$
\item $||\vec{v}+\vec{w}|| \leq ||\vec{v}|| + ||\vec{w}||$
\end{itemize}

$p$ norm:
$p \leq \infty$

$||\vec{v}||_p = \sum (|v_i|^p)^{1/p}$

$||v||_{\infty} = max|V_i|$ $1 \leq i \leq n$

\textbf{Clasim:} $||v||_p$ is a norm
\end{document}


\subsubsection{Adaptive Naive Bayes Classifier} \label{sec:anbc} ANBC \cite{14} is a supervised learning algorithm that can be used to classify any type of N-dimensional signal. It is based on simple probabilistic classifier called Naive Bayes classifier. It fundamentally works by fitting an N-dimensional Gaussian distribution to each class during the training phase. New gestures can then be recognized in the prediction phase by finding the gesture that results.

ANBC like Naive Bayes classifier makes a number of basic assumptions with input data that all the variables in the data are independent. However, despite these naive assumptions, Naive Bayes Classifiers have proved successful in many real-world classification problems \cite{15}. It has also been shown in a study that the Naive Bayes Classifier not only performs well with completely independent features, but also with functionally dependent features.

ANBC algorithm is based on Bayes theory and gives the likelihood of event $A$ occurring, given the observation of event $B$. In the equation \ref{eq:grt:bayes}, $P(A)$ represents the prior probability of event $A$ occurring and $P(B)$ is a normalizing factor to ensure that all the posterior probabilities sum to 1.

\input{chapter/equations/grt-bayes}

\paragraph*{Training} The weighting coefficient adds an important feature for the ANBC algorithm as it enables one general classifier to be trained with multidimensional inputs, even if a number of inputs are only relevant for one particular gesture. For example, if it is used to recognize hand gestures, the weighting coefficients would enable the classifier to recognize both left and right hand gestures independently, without the position of the left hand affecting the classification of a right-handed gesture. For example, hand gesture recognition using x,y,z position of palms of Left and Right hand will have 6 dimensional sample. In this case left hand gestures will have weights {1,1,1,0,0,0}, right hand gestures will have weights {0,0,0,1,1,1} and both hand gestures will have weights {1,1,1,1,1,1}.

Using the weighted Gaussian model, the ANBC algorithm requires $G(3N)$ parameters, assuming that each of the $G$ gestures require specific values for the N-dimensional $ \mu_{k} , \sigma_{k}^{2} $ and $ \phi_{k} $ vectors where $ \mu_{k} , \sigma_{k}^{2},\phi_{k}$ are mean, variance and weighting coefficients. Assuming that $ \phi_{k} $ is set by the user, $ \mu_{k} $ and $\sigma_{k}^{2} $ values can easily be calculated in a supervised learning scenario by grouping the input training data $X$ into a matrix containing $M$ training examples each with $N$ dimensions, into their corresponding classes. The values for $ \mu$ and $\sigma^{2} $of each dimension $n$ for each class $k$ can then be estimated by computing the mean and variance of the grouped training data for each of the respective classes \cite{14}.

\input{chapter/equations/grt-gauss}

After the Gaussian models have been trained for each of the $G$ classes, an unknown N-dimensional vector $x$ can be classified as one of the $G$ classes using the maximum a posterior probability estimate (MAP). MAP estimate classifies $x$ as the $k$-th class that results in the maximum a posterior probability given by the equation \ref{eq:grt:gauss}

\input{chapter/equations/grt-threshold}

\paragraph*{Rejection Threshold} Using equation \ref{eq:grt:threshold}, an unknown N-dimensional vector $x$ can be classified as one of the $G$ classes from a trained ANBC model. If $x$ actually comes from an unknown distribution that has not been modeled by one of the trained classes then, it will be incorrectly classified against the $k$ th gesture that gives the maximum likelihood value. A rejection threshold must therefore be calculated for each of the $G$ gestures to enable the algorithm to classify any of the $G$ gestures from a continuous stream of data that also contains non-gestural data \cite{14}.

\paragraph*{Online Training} One key element of ANBC is that it can easily be made adaptive. Adding an adaptive online training phase to the common two-phase (training and prediction) provides some significant advantages for the recognition gestures. During online training phase the algorithm will not only perform real-time predictions on the continuous stream of input data, but it will also continue to train and refine the models for each gesture. This enables the user to initially train the algorithm with a low number of training samples and during the adaptive online training phase, the algorithm can continue to train and refine the initial models, creating a more robust model as the number of training samples increases.

\paragraph*{Pros / Cons} ANBC works well for the classification of static gestures and non-temporal pattern recognition. However, the main limitation of the ANBC is that, it does not work well when the data you want to classify, is not linearly separable because it uses a Gaussian distribution to represent each class. Also when ANBC is working with online training enabled, a small number of incorrectly labeled training examples can create a loose model that becomes less effective at each update step and ultimately lead to a poor performance and accuracy.

\subsubsection{Gesture Recognition Toolkit (GRT)} \label{sec:grt} GRT is a cross-platform open-source C++ library designed and developed mainly by Nicholas Gillian at MIT Media Lab to make real-time machine learning and gesture recognition \cite{16}. Emphasis is placed on the ease of use with a consistent, minimalist design that promotes accessibility while supporting flexibility and customization for advanced users. The toolkit features a broad range of classification and regression algorithms, and has extensive support for building real-time systems. GRT includes algorithms for signal processing, feature extraction and automatic gesture spotting.

In this thesis, we attempt to take advantage of GRT as framework to carry out most of the tasks involved in hand gesture recognition. Figure \ref{fg:grt:pipeline} shows that GRT provides the full fledge pipeline to build a real-time gesture recognition system.

\input{chapter/figures/grt-pipeline}

\paragraph*{Pipeline} GRT provides an API to reduce the need for boilerplate code to perform common functionality, such as passing data between algorithms or to per-process data sets. GRT uses an object-oriented modular architecture and it is built around a set of core modules and a gesture-recognition pipeline. The input to both the modules and pipeline consists of an N-dimensional double-precision vector, making the toolkit flexible to any type of input signal. The algorithms can be used as stand-alone classes; alternatively a gesture recognition pipeline can be used to chain modules together to create a more sophisticated gesture recognition system. Modularity of GRT pipeline offers developers opportunities to work on each stages of gesture recognition independently. Additionally, pipeline can be stored and loaded dynamically so that an compiled application can work in many different configurations.

\paragraph*{ClassificationData} Accurate labeling of dataset is very critical for machine learning problems. The toolkit thus contains an extensive support for recording, labeling and managing supervised and unsupervised datasets for classification, regression and time series analysis. \textit{ClassificationData} is the data structure used for supervised learning problems and for most of the non temporal classification algorithms.

GRT allows us to store and load the training data in GRT format or Comma Separated Values (CSV). Since the training datasets are stored in human readable format, it enables us to add more samples which are collected separately or remove false data from the training dataset.

\paragraph*{TrainingDataRecordingTimer} Important part of the training phase is recording positive samples of modeled hand gestures. Hence, GRT provides a feature called \textit{TrainingDataRecordingTimer} that sets recording and preparation time in milliseconds. Once it is started by calling \textit{startRecording(prepationTime, recordTime)} method, it waits for given preparation time before it actually starts to store the data. This feature helps the trainer get into the right pose before samples are added to the training data and as well as train all the gestures for the same time duration.

\paragraph*{Algorithms} GRT features a broad range of machine-learning algorithms such as AdaBoost, Decision Trees, Dynamic Time Warping (DTW), Hidden Markov Models (HMM), K-Nearest Neighbor (KNN), Linear and Logistic Regression, Adaptive Naive Bayes (ANBC), Multilayer Perceptrons (MLP), Random Forests and Support Vector Machines (SVM) \cite{16}.

\paragraph*{Null Rejection} Another important feature of GRT is Null Rejections threshold. It means that algorithms can automatically spot the difference between trained gestures and unintended gestures that can happen when the user moves the hand in freely. It can be enabled by the method \textit{enableNullRejection(true)} and the range of the null rejection region can be set by this method \textit{setNullRejectionCoeff(double nullRejectionCoeff)} of the classifier. Algorithm such as the ANBC and N-Dimensional DTW, learn rejection thresholds from the training data, which are then used to automatically recognize valid gestures from a continuous stream of real-time data.

\input{chapter/figures/grt-null}

Figure \ref{fg:grt:null} shows that the decision boundaries computed by training six of classification algorithms on an example dataset with 3 classes. After training each classifier, each point in the two-dimensional feature space is colored by the likelihood of the predicted class label (red for class 1, green for class 2, blue for class 3). The top row shows the predictions of each classifier with null rejection disabled. The bottom row shows the predictions of each classifier with null rejection enabled with a coefficient of 3.0. Rejected points are colored white. Note that both the decision boundaries and null-rejection regions are different for each of the classifiers. This results from the several learning and prediction algorithms used by each classifier.

\paragraph*{Scaling Normalization} Real-time classification faces normalization problems when the range of training data differ from prediction input. To solve this problems, there are few solutions such as Z-score Standardization and Feature Scaling. GRT presents a simple solution called as Minimum-Maximum scaling.

Min-Max scaling rescales the range in [0, 1] or [-1, 1]. Selecting the target range depends on the nature of the data. Classifiers \textit{enableScaling(true)} method scales input vector between the default min-max range that is from 0 to 1. The cost of having this bounded range is that model will end up with smaller standard deviations, which can suppress the effect of outliers. Equation \ref{eq:grt:scaling} shows how Min-Max scaling is done.

\input{chapter/equations/grt-scaling}

\paragraph*{Pre/Post Processing Modules} In many real-world scenarios, the input to a classification algorithm must be preprocessed and have salient features extracted. GRT therefore supports a wide range of pre/post-processing modules such as Moving Average Filter, Class Label Filter and Class Label Change Filter, embedded feature extraction algorithms such as AdaBoost, dimensionality reduction techniques such as Principal Component Analysis (PCA) and unsupervised quantizers such as K-Means Quantizer, Self-Organizing Map Quantizer.

There will not be any need of preprocessing modules in this project since raw data received from depth sensor is processed by NiTE framework. However, post-processing modules such as Class Label Filter and Class Label Change Filter may be needed for a reasons that depth sensor samples 30 frames per second, therefore 30 input samples per second are supplied to the classifier for prediction and the output must be triggered once for every gesture.

\input{chapter/figures/grt-label-filter}

\paragraph*{Class Label Filter} It is a useful post-processing module which can remove erroneous or sporadic prediction spikes that may be made by a classifier on a continuous input stream of data. Figure \ref{fg:grt:label} that the classifier correctly outputs the predicted class label of 1 for a large majority of the time that a user is performing gesture 1. However, may be due to sensor noise or false samples in the training data, the classifier outputs the class label of 2. In this instance the class label filter can be used to remove these sporadic prediction values with the output of the class label filter in this instance being 1.

Class Label Filter module is controlled through two parameters: the minimum count value and buffer size value. The minimum count sets the minimum number of label values that must be present in the buffer to be output by the Class Label Filter. The size of the class labels buffer is set by the buffer size parameter. If there is more than one type of class label in the buffer then the class label with the maximum number of instances will be output. If the maximum number of instances for any class label in the buffer is less than the minimum count parameter then the Class Label Filter will output the default null rejection class label of 0.

\input{chapter/figures/grt-label-change-filter}

\paragraph*{Class Label Change Filter} It is one of the useful post-processing module that triggers when the predicted output of a classifier changes. Figure \ref{fg:grt:label:change}shows that, if the output stream of a classifier is {1,1,1,1,2,2,2,2,3,3}, then the output of the filter would be {1,0,0,0,2,0,0,0,3,0}. This module is useful to trigger a gesture once, if the user is gesticulating the same gesture for longer time duration. If the user intends to trigger the same gesture again, then hand position must be changed to another such as pointing the hand towards the ground, and gesticulate the gesture again.

\paragraph*{GUI} Figure \ref{fg:grt:gui} shows GRT-GUI which is an application that provides an easy-to-use graphical interface developed in C++ to setup and configure a gesture recognition pipeline that can be used for classification, regression, or time-series analysis. Data and control commands are streamed in and out of this application as Open Sound Control (OSC) packets via UDP . Therefore, it acts as a standalone application to record, label, save, load and test the training data and performs a real-time prediction for the incoming data, send output to another application.

\input{chapter/figures/grt-gui}


% !TEX root = hott_intro.tex

\section{The fundamental theorem of identity types}\label{chap:fundamental}
\sectionmark{The fundamental theorem}

\index{fundamental theorem of identity types|(}
\index{characterization of identity type!fundamental theorem of identity types|(}
For many types it is useful to have a characterization of their identity types. For example, we have used a characterization of the identity types of the fibers of a map in order to conclude that any equivalence is a contractible map. The fundamental theorem of identity types is our main tool to carry out such characterizations, and with the fundamental theorem it becomes a routine task to characterize an identity type whenever that is of interest.

In our first application of the fundamental theorem of identity types we show that any equivalence is an embedding. Embeddings are maps that induce equivalences on identity types, i.e., they are the homotopical analogue of injective maps. In our second application we characterize the identity types of coproducts.

Throughout the rest of this book we will encounter many more occasions to characterize identity types. For example, we will show in \cref{thm:eq_nat} that the identity type of the natural numbers is equivalent to its observational equality, and we will show in \cref{thm:eq-circle} that the loop space of the circle is equivalent to $\Z$.

In order to prove the fundamental theorem of identity types, we first prove the basic fact that a family of maps is a family of equivalences if and only if it induces an equivalence on total spaces.

\subsection{Families of equivalences}

\index{family of equivalences|(}
\begin{defn}
Consider a family of maps
\begin{equation*}
f : \prd{x:A}B(x)\to C(x).
\end{equation*}
We define the map\index{total(f)@{$\tot{f}$}}
\begin{equation*}
\tot{f}:\sm{x:A}B(x)\to\sm{x:A}C(x)
\end{equation*}
by $\lam{(x,y)}(x,f(x,y))$.
\end{defn}

\begin{lem}\label{lem:fib_total}
  For any family of maps $f:\prd{x:A}B(x)\to C(x)$ and any $t:\sm{x:A}C(x)$,
  there is an equivalence\index{fiber!of total(f)@{of $\tot{f}$}}\index{total(f)@{$\tot{f}$}!fiber}
  \begin{equation*}
    \eqv{\fib{\tot{f}}{t}}{\fib{f(\proj 1(t))}{\proj 2(t)}}.
  \end{equation*}
\end{lem}

\begin{proof}
  For any $p:\fib{\tot{f}}{t}$ we define $\varphi(t,p):\fib{\proj 1(t)}{\proj 2(t)}$ by $\Sigma$-induction on $p$. Therefore it suffices to define $\varphi(t,(s,\alpha)):\fib{\proj 1(t)}{\proj 2 (t)}$ for any $s:\sm{x:A}B(x)$ and $\alpha:\tot{f}(s)=t$. Now we proceed by path induction on $\alpha$, so it suffices to define $\varphi(\tot{f}(s),(s,\refl{})):\fib{f(\proj 1(\tot{f}(s)))}{\proj 2(\tot{f}(s))}$. Finally, we use $\Sigma$-induction on $s$ once more, so it suffices to define
  \begin{equation*}
    \varphi((x,f(x,y)),((x,y),\refl{})):\fib{f(x)}{f(x,y)}.
  \end{equation*}
  Now we take as our definition
  \begin{equation*}
    \varphi((x,f(x,y)),((x,y),\refl{}))\defeq(y,\refl{}).
  \end{equation*}

  For the proof that this map is an equivalence we construct a map
  \begin{equation*}
    \psi(t) : \fib{f(\proj 1(t))}{\proj 2(t)}\to\fib{\tot{f}}{t}
  \end{equation*}
  equipped with homotopies $G(t):\varphi(t)\circ\psi(t)\htpy\idfunc$ and $H(t):\psi(t)\circ\varphi(t)\htpy\idfunc$. In each of these definitions we use $\Sigma$-induction and path induction all the way through, until an obvious choice of definition becomes apparent. We define $\psi(t)$, $G(t)$, and $H(t)$ as follows:
  \begin{align*}
    \psi((x,f(x,y)),(y,\refl{})) & \defeq ((x,y),\refl{}) \\
    G((x,f(x,y)),(y,\refl{})) & \defeq \refl{} \\
    H((x,f(x,y)),((x,y),\refl{})) & \defeq \refl{}.\qedhere
  \end{align*}
\end{proof}

\begin{thm}\label{thm:fib_equiv}
  Let $f:\prd{x:A}B(x)\to C(x)$ be a family of maps. The following are equivalent:
  \index{is an equivalence!total(f) of family of equivalences@{$\tot{f}$ of family of equivalences}}
  \index{total(f)@{$\tot{f}$}!of family of equivalences is an equivalence}\index{is family of equivalences!if total(f) is an equivalence@{iff $\tot{f}$ is an equivalence}}
\begin{enumerate}
\item For each $x:A$, the map $f(x)$ is an equivalence. In this case we say that $f$ is a \define{family of equivalences}.
\item The map $\tot{f}:\sm{x:A}B(x)\to\sm{x:A}C(x)$ is an equivalence.
\end{enumerate}
\end{thm}

\begin{proof}
By \cref{thm:equiv_contr,thm:contr_equiv} it suffices to show that $f(x)$ is a contractible map for each $x:A$, if and only if $\tot{f}$ is a contractible map. Thus, we will show that $\fib{f(x)}{c}$ is contractible if and only if $\fib{\tot{f}}{x,c}$ is contractible, for each $x:A$ and $c:C(x)$. However, by \cref{lem:fib_total} these types are equivalent, so the result follows by \cref{ex:contr_equiv}.
\end{proof}

Now consider the situation where we have a map $f:A\to B$, and a family $C$ over $B$. Then we have the map
\begin{equation*}
  \lam{(x,z)}(f(x),z):\sm{x:A}C(f(x))\to\sm{y:B}C(y).
\end{equation*}
We claim that this map is an equivalence when $f$ is an equivalence. The technique to prove this claim is the same as the technique we used in \cref{thm:fib_equiv}: first we note that the fibers are equivalent to the fibers of $f$, and then we use the fact that a map is an equivalence if and only if its fibers are contractible to finish the proof.

The converse of the following lemma does not hold. Why not?

\begin{lem}\label{lem:total-equiv-base-equiv}
  Consider an equivalence $e:A\simeq B$, and let $C$ be a type family over $B$. Then the map
  \begin{equation*}
    \sigma_f(C) \defeq\lam{(x,z)}(f(x),z):\sm{x:A}C(f(x))\to\sm{y:B}C(y)
  \end{equation*}
  is an equivalence.
\end{lem}

\begin{proof}
  We claim that for each $t:\sm{y:B}C(y)$ there is an equivalence
  \begin{equation*}
    \fib{\sigma_f(C)}{t}\simeq \fib{f}{\proj 1(t)}.
  \end{equation*}
  We obtain such an equivalence by constructing the following functions and homotopies:
  \begin{align*}
    \varphi(t) & : \fib{\sigma_f(C)}{t}\to\fib{f}{\proj 1 (t)} & \varphi((f(x),z),((x,z),\refl{})) & \defeq (x,\refl{}) \\
    \psi(t) & : \fib{f}{\proj 1(t)} \to\fib{\sigma_f(C)}{t} & \psi((f(x),z),(x,\refl{})) & \defeq ((x,z),\refl{}) \\
    G(t) & : \varphi(t)\circ\psi(t)\htpy\idfunc & G((f(x),z),(x,\refl{})) & \defeq \refl{} \\
    H(t) & : \psi(t)\circ\varphi(t)\htpy\idfunc & H((f(x),z),((x,z),\refl{})) & \defeq \refl{}.
  \end{align*}
  Now the claim follows, since we see that $\varphi$ is a contractible map if and only if $f$ is a contractible map.
\end{proof}

We now combine \cref{thm:fib_equiv,lem:total-equiv-base-equiv}.

\begin{defn}
  Consider a map $f:A\to B$ and a family of maps
  \begin{equation*}
    g:\prd{x:A}C(x)\to D(f(x)),
  \end{equation*}
  where $C$ is a type family over $A$, and $D$ is a type family over $B$. In this situation we also say that $g$ is a \define{family of maps over $f$}. Then we define\index{total f(g)@{$\tot[f]{g}$}}
  \begin{equation*}
    \tot[f]{g}:\sm{x:A}C(x)\to\sm{y:B}D(y)
  \end{equation*}
  by $\tot[f]{g}(x,z)\defeq (f(x),g(x,z))$.
\end{defn}

\begin{thm}\label{thm:equiv-toto}
  Suppose that $g$ is a family of maps over $f$, and suppose that $f$ is an equivalence. Then the following are equivalent:
  \begin{enumerate}
  \item The family of maps $g$ over $f$ is a family of equivalences.
  \item The map $\tot[f]{g}$ is an equivalence.
  \end{enumerate}
\end{thm}

\begin{proof}
  Note that we have a commuting triangle
  \begin{equation*}
    \begin{tikzcd}[column sep=0]
      \sm{x:A}C(x) \arrow[rr,"{\tot[f]{g}}"] \arrow[dr,swap,"\tot{g}"]& & \sm{y:B}D(y) \\
      & \sm{x:A}D(f(x)) \arrow[ur,swap,"{\lam{(x,z)}(f(x),z)}"]
    \end{tikzcd}
  \end{equation*}
  By the assumption that $f$ is an equivalence, it follows that the map $\sm{x:A}D(f(x))\to \sm{y:B}D(y)$ is an equivalence. Therefore it follows that $\tot[f]{g}$ is an equivalence if and only if $\tot{g}$ is an equivalence. Now the claim follows, since $\tot{g}$ is an equivalence if and only if $g$ if a family of equivalences.
\end{proof}
\index{family of equivalences|)}

\subsection{The fundamental theorem}

\index{identity system|(}
Many types come equipped with a reflexive relation that possesses a similar
structure as the identity type. The observational equality on the natural
numbers is such an example. We have see that it is a reflexive, symmetric, and
transitive relation, and moreover it is contained in any other reflexive
relation. Thus, it is natural to ask whether observational equality on the natural numbers is equivalent to the identity type.

The fundamental theorem of identity types (\cref{thm:id_fundamental}) is a general theorem that can be used to answer such questions. It describes a necessary and sufficient condition on a type family $B$ over a type $A$ equipped with a point $a:A$, for there to be a family of equivalences $\prd{x:A}(a=x)\simeq B(x)$. In other words, it tells us when a family $B$ is a characterization of the identity type of $A$.

Before we state the fundamental theorem of identity types we introduce the notion of \emph{identity systems}. Those are families $B$ over a $A$ that satisfy an induction principle that is similar to the path induction principle, where the `computation rule' is stated with an identification.

\begin{defn}
  Let $A$ be a type equipped with a term $a:A$. A \define{(unary) identity system} on $A$ at $a$ consists of a type family $B$ over $A$ equipped with $b:B(a)$, such that for any family of types $P(x,y)$ indexed by $x:A$ and $y:B(x)$,
  the function
  \begin{equation*}
    h\mapsto h(a,b):\Big(\prd{x:A}\prd{y:B(x)}P(x,y)\Big)\to P(a,b)
  \end{equation*}
  has a section.
\end{defn}

The most important implication in the fundamental theorem is that (ii) implies (i). Occasionally we will also use the third equivalent statement. We note that the fundamental theorem also appears as Theorem 5.8.4 in \cite{hottbook}.

\begin{thm}\label{thm:id_fundamental}
Let $A$ be a type with $a:A$, and let $B$ be be a type family over $A$ with $b:B(a)$.
Then  the following are logically equivalent for any family of maps
\begin{equation*}
  f:\prd{x:A}(a=x)\to B(x).
\end{equation*}
\begin{enumerate}
\item The family of maps $f$ is a family of equivalences.
\item The total space\index{is contractible!total space of an identity system}
\begin{equation*}
\sm{x:A}B(x)
\end{equation*}
is contractible.
\item The family $B$ is an identity system.
\end{enumerate}
In particular the canonical family of maps
\begin{equation*}
\pathind_a(b):\prd{x:A} (a=x)\to B(x)
\end{equation*}
is a family of equivalences if and only if $\sm{x:A}B(x)$ is contractible.
\end{thm}

\begin{proof}
  First we show that (i) and (ii) are equivalent.
  By \cref{thm:fib_equiv} it follows that the family of maps $f$ is a family of equivalences if and only if it induces an equivalence
  \begin{equation*}
    \eqv{\Big(\sm{x:A}a=x\Big)}{\Big(\sm{x:A}B(x)\Big)}
  \end{equation*}
  on total spaces. We have that $\sm{x:A}a=x$ is contractible. Now it follows by \cref{ex:contr_equiv}, applied in the case
  \begin{equation*}
    \begin{tikzcd}[column sep=3em]
      \sm{x:A}a=x \arrow[rr,"\tot{f}"] \arrow[dr,swap,"\eqvsym"] & & \sm{x:A}B(x) \arrow[dl] \\
      & \unit & \phantom{\sm{x:A}a=x}
    \end{tikzcd}
  \end{equation*}
  that $\tot{f}$ is an equivalence if and only if $\sm{x:A}B(x)$ is contractible.

  Now we show that (ii) and (iii) are equivalent. Note that we have the following commuting triangle
  \begin{equation*}
    \begin{tikzcd}[column sep=0]
      \prd{t:\sm{x:A}B(x)}P(t) \arrow[rr,"\evpair"] \arrow[dr,swap,"{\evpt(a,b)}"] & & \prd{x:A}\prd{y:B(x)}P(x,y) \arrow[dl,"{\lam{h}h(a,b)}"] \\
      \phantom{\prd{x:A}\prd{y:B(x)}P(x,y)} & P(a,b)
    \end{tikzcd}
  \end{equation*}
  In this diagram the top map has a section. Therefore it follows by \cref{ex:3_for_2} that the left map has a section if and only if the right map has a section. Notice that the left map has a section for all $P$ if and only if $\sm{x:A}B(x)$ satisfies singleton induction, which is by \cref{thm:contractible} equivalent to $\sm{x:A}B(x)$ being contractible.
\end{proof}
\index{identity system|)}

\subsection{Embeddings}
\index{embedding|(}
As an application of the fundamental theorem we show that equivalences are embeddings. The notion of embedding is the homotopical analogue of the set theoretic notion of injective map.

\begin{defn}
An \define{embedding} is a map $f:A\to B$\index{is an embedding} satisfying the property that\index{is an equivalence!action on paths of an embedding}
\begin{equation*}
\apfunc{f}:(\id{x}{y})\to(\id{f(x)}{f(y)})
\end{equation*}
is an equivalence for every $x,y:A$. We write $\isemb(f)$\index{is-emb(f)@{$\isemb(f)$}} for the type of witnesses that $f$ is an embedding.
\end{defn}

Another way of phrasing the following statement is that equivalent types have equivalent identity types.

\begin{thm}
\label{cor:emb_equiv}
Any equivalence is an embedding.\index{is an embedding!equivalence}\index{equivalence!is an embedding}
\end{thm}

\begin{proof}
Let $e:\eqv{A}{B}$ be an equivalence, and let $x:A$. Our goal is to show that
\begin{equation*}
\apfunc{e} : (\id{x}{y})\to (\id{e(x)}{e(y)})
\end{equation*}
is an equivalence for every $y:A$. By \cref{thm:id_fundamental} it suffices to show that
\begin{equation*}
\sm{y:A}e(x)=e(y)
\end{equation*}
is contractible for every $y:A$. Now observe that there is an equivalence
\begin{samepage}
\begin{align*}
\sm{y:A}e(x)=e(y) & \eqvsym \sm{y:A}e(y)=e(x) \\
& \jdeq \fib{e}{e(x)}
\end{align*}
\end{samepage}
by \cref{thm:fib_equiv}, since for each $y:A$ the map
\begin{equation*}
\invfunc : (e(x)=e(y))\to (e(y)= e(x))
\end{equation*}
is an equivalence by \cref{ex:equiv_grpd_ops}.
The fiber $\fib{e}{e(x)}$ is contractible by \cref{thm:contr_equiv}, so it follows by \cref{ex:contr_equiv} that the type $\sm{y:A}e(x)=e(y)$ is indeed contractible.
\end{proof}
\index{embedding|)}

\subsection{Disjointness of coproducts}

\index{disjointness of coproducts|(}
\index{characterization of identity type!coproduct|(}
\index{identity type!coproduct|(}
\index{coproduct!identity type|(}
\index{coproduct!disjointness|(}
To give a second application of the fundamental theorem of identity types, we characterize the identity types of coproducts. Our goal in this section is to prove the following theorem.

\begin{thm}\label{thm:id-coprod-compute}
Let $A$ and $B$ be types. Then there are equivalences
\begin{align*}
(\inl(x)=\inl(x')) & \eqvsym (x = x')\\
(\inl(x)=\inr(y')) & \eqvsym \emptyt \\
(\inr(y)=\inl(x')) & \eqvsym \emptyt \\
(\inr(y)=\inr(y')) & \eqvsym (y=y')
\end{align*}
for any $x,x':A$ and $y,y':B$.
\end{thm}

In order to prove \cref{thm:id-coprod-compute}, we first define
a binary relation $\Eqcoprod_{A,B}$ on the coproduct $A+B$.

\begin{defn}
Let $A$ and $B$ be types. We define
\begin{equation*}
\Eqcoprod_{A,B} : (A+B)\to (A+B)\to\UU
\end{equation*}
by double induction on the coproduct, postulating
\begin{align*}
\Eqcoprod_{A,B}(\inl(x),\inl(x')) & \defeq (x=x') \\
\Eqcoprod_{A,B}(\inl(x),\inr(y')) & \defeq \emptyt \\
\Eqcoprod_{A,B}(\inr(y),\inl(x')) & \defeq \emptyt \\
\Eqcoprod_{A,B}(\inr(y),\inr(y')) & \defeq (y=y')
\end{align*}
The relation $\Eqcoprod_{A,B}$ is also called the \define{observational equality of coproducts}\index{observational equality!of coproducts}.
\end{defn}

\begin{lem}
The observational equality relation $\Eqcoprod_{A,B}$ on $A+B$ is reflexive, and therefore there is a map
\begin{equation*}
\Eqcoprodeq:\prd{s,t:A+B} (s=t)\to \Eqcoprod_{A,B}(s,t)
\end{equation*}
\end{lem}

\begin{constr}
The reflexivity term $\rho$ is constructed by induction on $t:A+B$, using
\begin{align*}
\rho(\inl(x))\defeq \refl{\inl(x)}  & : \Eqcoprod_{A,B}(\inl(x)) \\
\rho(\inr(y))\defeq \refl{\inr(y)} & : \Eqcoprod_{A,B}(\inr(y)).\qedhere
\end{align*}
\end{constr}

To show that $\Eqcoprodeq$ is a family of equivalences, we will use the fundamental theorem, \cref{thm:id_fundamental}. Moreover, we will use the functoriality of coproducts (established in \cref{ex:coproduct_functor}), and the fact that any total space over a coproduct is again a coproduct:
\begin{align*}
\sm{t:A+B}P(t) & \eqvsym \Big(\sm{x:A}P(\inl(x))\Big)+\Big(\sm{y:B}P(\inr(y))\Big)
\end{align*}
All of these equivalences are straightforward to construct, so we leave them as an exercise to the reader.

\begin{lem}\label{lem:is-contr-total-eq-coprod}
For any $s:A+B$ the total space
\begin{equation*}
\sm{t:A+B}\Eqcoprod_{A,B}(s,t)
\end{equation*}
is contractible.
\end{lem}

\begin{proof}
We will do the proof by induction on $s$. The two cases are similar, so we only show that the total space
\begin{equation*}
\sm{t:A+B}\Eqcoprod_{A,B}(\inl(x),t)
\end{equation*}
is contractible. Note that we have equivalences
\begin{samepage}
\begin{align*}
& \sm{t:A+B}\Eqcoprod_{A,B}(\inl(x),t) \\
& \eqvsym \Big(\sm{x':A}\Eqcoprod_{A,B}(\inl(x),\inl(x'))\Big)+\Big(\sm{y':B}\Eqcoprod_{A,B}(\inl(x),\inr(y'))\Big) \\
& \eqvsym \Big(\sm{x':A}x=x'\Big)+\Big(\sm{y':B}\emptyt\Big) \\
& \eqvsym \Big(\sm{x':A}x=x'\Big)+\emptyt \\
& \eqvsym \sm{x':A}x=x'.
\end{align*}%
\end{samepage}%
In the last two equivalences we used \cref{ex:unit-laws-coprod}. This shows that the total space is contractible, since the latter type is contractible by \cref{thm:total_path}.
\end{proof}

\begin{proof}[Proof of \cref{thm:id-coprod-compute}]
The proof is now concluded with an application of \cref{thm:id_fundamental}, using \cref{lem:is-contr-total-eq-coprod}.
\end{proof}
\index{disjointness of coproducts|)}
\index{characterization of identity type!coproduct|)}
\index{identity type!coproduct|)}
\index{coproduct!identity type|)}
\index{coproduct!disjointness|)}

\begin{exercises}
  \exercise
  \begin{subexenum}
  \item \label{ex:is-emb-empty}Show that the map $\emptyt\to A$ is an embedding for every type $A$.\index{is an embedding!0 to A@{$\emptyt\to A$}}
  \item \label{ex:is-emb-inl-inr}Show that $\inl:A\to A+B$ and $\inr:B\to A+B$ are embeddings for any two types $A$ and $B$.
    \index{is an embedding!inl (for coproducts)@{$\inl$ (for coproducts)}}
    \index{is an embedding!inr (for coproducts)@{$\inr$ (for coproducts)}}
    \index{inl@{$\inl$}!is an embedding}
    \index{inr@{$\inr$}!is an embedding}
  \end{subexenum}
  \exercise Consider an equivalence $e:A\simeq B$. Construct an equivalence
  \begin{equation*}
    (e(x)=y)\simeq(x=e^{-1}(y))
  \end{equation*}
  for every $x:A$ and $y:B$.
  \exercise Show that\index{embedding!closed under homotopies}
  \begin{equation*}
    (f\htpy g)\to (\isemb(f)\leftrightarrow\isemb(g))
  \end{equation*}
  for any $f,g:A\to B$.
  \exercise \label{ex:emb_triangle}Consider a commuting triangle
  \begin{equation*}
    \begin{tikzcd}[column sep=tiny]
      A \arrow[rr,"h"] \arrow[dr,swap,"f"] & & B \arrow[dl,"g"] \\
      & X
    \end{tikzcd}
  \end{equation*}
  with $H:f\htpy g\circ h$.
  \begin{subexenum}
  \item Suppose that $g$ is an embedding. Show that $f$ is an embedding if and only if $h$ is an embedding.\index{is an embedding!composite of embeddings}\index{is an embedding!right factor of embedding if left factor is an embedding}
  \item Suppose that $h$ is an equivalence. Show that $f$ is an embedding if and only if $g$ is an embedding.\index{is an embedding!left factor of embedding if right factor is an equivalence}
  \end{subexenum}
  \exercise \label{ex:is-equiv-is-equiv-functor-coprod}Consider two maps $f:A\to A'$ and $g:B \to B'$.
  \begin{subexenum}
  \item Show that if the map
    \begin{equation*}
      f+g:(A+B)\to (A'+B')
    \end{equation*}
    is an equivalence, then so are both $f$ and $g$ (this is the converse of \cref{ex:coproduct_functor_equivalence}).
  \item \label{ex:is-emb-coprod}Show that $f+g$ is an embedding if and only if both $f$ and $g$ are embeddings.
  \end{subexenum}
  \exercise \label{ex:htpy_total}
  \begin{subexenum}
  \item Let $f,g:\prd{x:A}B(x)\to C(x)$ be two families of maps. Show that
    \begin{equation*}
      \Big(\prd{x:A}f(x)\htpy g(x)\Big)\to \Big(\tot{f}\htpy \tot{g}\Big).
    \end{equation*}
  \item Let $f:\prd{x:A}B(x)\to C(x)$ and let $g:\prd{x:A}C(x)\to D(x)$. Show that
    \begin{equation*}
      \tot{\lam{x}g(x)\circ f(x)}\htpy \tot{g}\circ\tot{f}.
    \end{equation*}
  \item For any family $B$ over $A$, show that
    \begin{equation*}
      \tot{\lam{x}\idfunc[B(x)]}\htpy\idfunc.
    \end{equation*}
  \end{subexenum}
  \exercise \label{ex:id_fundamental_retr}Let $a:A$, and let $B$ be a type family over $A$.
  \begin{subexenum}
  \item Use \cref{ex:htpy_total,ex:contr_retr} to show that if each $B(x)$ is a retract of $\id{a}{x}$, then $B(x)$ is equivalent to $\id{a}{x}$ for every $x:A$.
    \index{fundamental theorem of identity types!formulation with retractions}
  \item Conclude that for any family of maps
    \index{fundamental theorem of identity types!formulation with sections}
    \begin{equation*}
      f : \prd{x:A} (a=x) \to B(x),
    \end{equation*}
    if each $f(x)$ has a section, then $f$ is a family of equivalences.
  \end{subexenum}
  \exercise Use \cref{ex:id_fundamental_retr} to show that for any map $f:A\to B$, if
  \begin{equation*}
    \apfunc{f} : (x=y) \to (f(x)=f(y))
  \end{equation*}
  has a section for each $x,y:A$, then $f$ is an embedding.\index{is an embedding!if the action on paths have sections}
  \exercise \label{ex:path-split}We say that a map $f:A\to B$ is \define{path-split}\index{path-split} if $f$ has a section, and for each $x,y:A$ the map
  \begin{equation*}
    \apfunc{f}(x,y):(x=y)\to (f(x)=f(y))
  \end{equation*}
  also has a section. We write $\pathsplit(f)$\index{path-split(f)@{$\pathsplit(f)$}} for the type
  \begin{equation*}
    \sections(f)\times\prd{x,y:A}\sections(\apfunc{f}(x,y)).
  \end{equation*}
  Show that for any map $f:A\to B$ the following are equivalent:
  \begin{enumerate}
  \item The map $f$ is an equivalence.
  \item The map $f$ is path-split.
  \end{enumerate}
  \exercise \label{ex:fiber_trans}Consider a triangle
  \begin{equation*}
    \begin{tikzcd}[column sep=small]
      A \arrow[rr,"h"] \arrow[dr,swap,"f"] & & B \arrow[dl,"g"] \\
      & X
    \end{tikzcd}
  \end{equation*}
  with a homotopy $H:f\htpy g\circ h$ witnessing that the triangle commutes.
  \begin{subexenum}
  \item Construct a family of maps
    \begin{equation*}
      \fibtriangle(h,H):\prd{x:X}\fib{f}{x}\to\fib{g}{x},
    \end{equation*}
    for which the square
    \begin{equation*}
      \begin{tikzcd}[column sep=8em]
        \sm{x:X}\fib{f}{x} \arrow[r,"\tot{\fibtriangle(h,H)}"] \arrow[d] & \sm{x:X}\fib{g}{x} \arrow[d] \\
        A \arrow[r,swap,"h"] & B
      \end{tikzcd}
    \end{equation*}
    commutes, where the vertical maps are as constructed in \cref{ex:fib_replacement}.
  \item Show that $h$ is an equivalence if and only if $\fibtriangle(h,H)$ is a family of equivalences.
  \end{subexenum}
\end{exercises}
\index{fundamental theorem of identity types|)}
\index{characterization of identity type!fundamental theorem of identity types|)}

\endinput

  \begin{comment}
    \exercise \label{ex:eqv_sigma_mv}Consider a map
    \begin{equation*}
      f:A \to \sm{y:B}C(y).
    \end{equation*}
    \begin{subexenum}
    \item Construct a family of maps
      \begin{equation*}
        f':\prd{y:B} \fib{\proj 1\circ f}{y}\to C(y).
      \end{equation*}
    \item Construct an equivalence
      \begin{equation*}
        \eqv{\fib{f'(b)}{c}}{\fib{f}{(b,c)}}
      \end{equation*}
      for every $(b,c):\sm{y:B}C(y)$.
    \item Conclude that the following are equivalent:
      \begin{enumerate}
      \item $f$ is an equivalence.
      \item $f'$ is a family of equivalences.
      \end{enumerate}
    \end{subexenum}
    \exercise \label{ex:coh_intro}Consider a type $A$ with base point $a:A$, and let $B$ be a type family on $A$ that implies the identity type, i.e., there is a term
    \begin{equation*}
      \alpha : \prd{x:A} B(x)\to (a=x).
    \end{equation*}
    Show that the \define{coherence reduction map}
    \begin{equation*}
      \cohreduction : \Big(\sm{y:B(a)}\alpha(a,y)=\refl{a}\Big) \to \Big(\sm{x:A}B(x)\Big)
    \end{equation*}
    defined by $\lam{(y,q)}(a,y)$ is an equivalence.
  \end{comment}


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

\section{Sampling from a small population}

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

\begin{frame}
\frametitle{Sampling with replacement}

When sampling \hl{with replacement}, you put back what you just drew.

\pause

\begin{itemize}

\item Imagine you have a bag with 5 red, 3 blue and 2 orange chips in it. What is the probability that the first chip you draw is blue?
\begin{center}
5 \textcolor{red}{$\CIRCLE$}~, 3 \textcolor{blue}{$\CIRCLE$}~, 2 \textcolor{orange}{$\CIRCLE$}
\end{center}

\pause

\[ Prob(1^{st} \text{ chip } B) = \frac{3}{5 + 3 + 2} = \frac{3}{10} = 0.3 \]

\pause

\item Suppose you did indeed pull a blue chip in the first draw. If drawing with replacement, what is the probability of drawing a blue chip in the second draw?

\pause

\begin{center}
$1^{st}$ draw: 5 \textcolor{red}{$\CIRCLE$}~, 3 \textcolor{blue}{$\CIRCLE$}~, 2 \textcolor{orange}{$\CIRCLE$} \\

\pause

$2^{nd}$ draw: 5 \textcolor{red}{$\CIRCLE$}~, 3 \textcolor{blue}{$\CIRCLE$}~, 2 \textcolor{orange}{$\CIRCLE$}
\end{center}

\pause

\[ Prob(2^{nd} \text{ chip } B | 1^{st} \text{ chip } B) = \frac{3}{10} = 0.3 \]

\end{itemize}

\end{frame}

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

\begin{frame}
\frametitle{Sampling with replacement (cont.)}

\begin{itemize}

\item Suppose you actually pulled an orange chip in the first draw. If drawing with replacement, what is the probability of drawing a blue chip in the second draw?

\pause

\begin{center}
$1^{st}$ draw: 5 \textcolor{red}{$\CIRCLE$}~, 3 \textcolor{blue}{$\CIRCLE$}~, 2 \textcolor{orange}{$\CIRCLE$} \\
\pause
$2^{nd}$ draw: 5 \textcolor{red}{$\CIRCLE$}~, 3 \textcolor{blue}{$\CIRCLE$}~, 2 \textcolor{orange}{$\CIRCLE$}
\end{center}
\pause
\[ Prob(2^{nd} \text{ chip } B | 1^{st} \text{ chip } O) = \frac{3}{10} = 0.3 \]

\pause
\item If drawing with replacement, what is the probability of drawing two blue chips in a row?
\begin{center}

\pause
$1^{st}$ draw: 5 \textcolor{red}{$\CIRCLE$}~, 3 \textcolor{blue}{$\CIRCLE$}~, 2 \textcolor{orange}{$\CIRCLE$} \\
$2^{nd}$ draw: 5 \textcolor{red}{$\CIRCLE$}~, 3 \textcolor{blue}{$\CIRCLE$}~, 2 \textcolor{orange}{$\CIRCLE$}
\end{center}
\pause
\[ Prob(1^{st} \text{ chip } B) \cdot Prob(2^{nd} \text{ chip } B | 1^{st} \text{ chip } B) = 0.3 \times 0.3 \]
\[ = 0.3^2 = 0.09 \]

\end{itemize}

\end{frame}

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

\begin{frame}
\frametitle{Sampling with replacement (cont.)}

\begin{itemize}

\item When drawing with replacement, probability of the second chip being blue does not depend on the color of the first chip since whatever we draw in the first draw gets put back in the bag.
\[ Prob(B | B) = Prob(B | O) \]

\item In addition, this probability is equal to the probability of drawing a blue chip in the first draw, since the composition of the bag never changes when sampling with replacement.
\[ Prob(B | B) = Prob(B) \]

\item \hl{When drawing with replacement, draws are independent.}

\end{itemize}

\end{frame}

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

\begin{frame}
\frametitle{Sampling without replacement}

When drawing \hl{without replacement} you do not put back what you just drew.

\begin{itemize}

\pause

\item Suppose you pulled a blue chip in the first draw. If drawing without replacement, what is the probability of drawing a blue chip in the second draw?
\pause
\begin{center}
$1^{st}$ draw: 5 \textcolor{red}{$\CIRCLE$}~, 3 \textcolor{blue}{$\CIRCLE$}~, 2 \textcolor{orange}{$\CIRCLE$} \\
\pause
$2^{nd}$ draw: 5 \textcolor{red}{$\CIRCLE$}~, 2 \textcolor{blue}{$\CIRCLE$}~, 2 \textcolor{orange}{$\CIRCLE$}
\end{center}
\pause
\[ Prob(2^{nd} \text{ chip } B | 1^{st} \text{ chip } B) = \frac{2}{9} = 0.22 \]

\pause

\item If drawing without replacement, what is the probability of drawing two blue chips in a row?
\begin{center}

\pause
$1^{st}$ draw: 5 \textcolor{red}{$\CIRCLE$}~, 3 \textcolor{blue}{$\CIRCLE$}~, 2 \textcolor{orange}{$\CIRCLE$} \\
$2^{nd}$ draw: 5 \textcolor{red}{$\CIRCLE$}~, 2 \textcolor{blue}{$\CIRCLE$}~, 2 \textcolor{orange}{$\CIRCLE$}
\end{center}
\pause
\[ Prob(1^{st} \text{ chip } B) \cdot Prob(2^{nd} \text{ chip } B | 1^{st} \text{ chip } B)  = 0.3 \times 0.22 \]
\[ = 0.066 \]

\end{itemize}

\end{frame}

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

\begin{frame}
\frametitle{Sampling without replacement (cont.)}

\begin{itemize}

\item When drawing without replacement, the probability of the second chip being blue given the first was blue is not equal to the probability of drawing a blue chip in the first draw since the composition of the bag changes with the outcome of the first draw.
\[ Prob(B | B) \ne Prob(B) \]

\pause

\item \hl{When drawing without replacement, draws are not independent.}

\pause

\item This is especially important to take note of when the sample sizes are small. If we were dealing with, say, 10,000 chips in a (giant) bag, taking out one chip of any color would not have as big an impact on the probabilities in the second draw.

\end{itemize}

\end{frame}

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

\begin{frame}
\frametitle{Practice}

\pq{In most card games cards are dealt without replacement. What is the probability of being dealt an ace and then a 3? Choose the closest answer.}

\twocol{0.3}{0.6}{
\begin{enumerate}[(a)]
\item 0.0045
\item 0.0059
\solnMult{0.0060}
\item 0.1553
\end{enumerate}
}
{
\soln{
\pause
\[ P(ace~then~3) = \frac{4}{52} \times \frac{4}{51} \approx 0.0060 \]
}}

\end{frame}

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


\subsection{Operations}

\begin{frame}[t]{Prime fields}{Operations}
	When the field $GF(p)$ is a prime field we encounter that:

	\begin{itemize}
		\item All nonzero elements of $GF(p)$ have an inverse.
		\item Arithmetic in $GF(p)$ is done modulo $p$.
	\end{itemize}

\end{frame}

\begin{frame}[t]{Prime fields}{Operations example}
	Given $GF(5)$ and $a, b \in GF(5)$
	\medskip

	\begin{columns}
		\begin{column}{0.5\textwidth}
			\centering $+(a, b) = a + b \mod 5$
			\begin{table}[]
				\begin{tabular}{c|ccccc}
					\textbf{+} & \textbf{0} & \textbf{1} & \textbf{2} & \textbf{3} & \textbf{4} \\ \hline
					\textbf{0} & 0          & 1          & 2          & 3          & 4          \\
					\textbf{1} & 1          & 2          & 3          & 4          & 0          \\
					\textbf{2} & 2          & 3          & 4          & 0          & 1          \\
					\textbf{3} & 3          & 4          & 0          & 1          & 2          \\
					\textbf{4} & 4          & 0          & 1          & 2          & 3
				\end{tabular}
			\end{table}
		\end{column}
		\begin{column}{0.5\textwidth}  %%<--- here
			\centering $\times(a, b) = a * b \mod 5$
			\begin{table}[]
				\begin{tabular}{c|ccccc}
					\textbf{$\times$} & \textbf{0} & \textbf{1} & \textbf{2} & \textbf{3} & \textbf{4} \\ \hline
					\textbf{0} & 0          & 0          & 0          & 0          & 0          \\
					\textbf{1} & 0          & 1          & 2          & 3          & 4          \\
					\textbf{2} & 0          & 2          & 4          & 1          & 3          \\
					\textbf{3} & 0          & 3          & 1          & 4          & 2          \\
					\textbf{4} & 0          & 4          & 3          & 2          & 1
				\end{tabular}
			\end{table}
		\end{column}
	\end{columns}
\end{frame}

\begin{frame}[t]{Prime fields}{Additive Inverse}
	Given $GF(5)$ and $a, b \in GF(5)$
	\medskip

	\begin{columns}
		\begin{column}{0.5\textwidth}
			\centering $+(a, b) = a + b \mod 5$
			\begin{table}[]
				\begin{tabular}{c|ccccc}
					\textbf{+} & \textbf{0} & \textbf{1} & \textbf{2} & \textbf{3} & \textbf{4} \\ \hline
					\textbf{0} & 0          & 1          & 2          & 3          & 4          \\
					\textbf{1} & 1          & 2          & 3          & 4          & 0          \\
					\textbf{2} & 2          & 3          & 4          & 0          & 1          \\
					\textbf{3} & 3          & 4          & 0          & 1          & 2          \\
					\textbf{4} & 4          & 0          & 1          & 2          & 3
				\end{tabular}
			\end{table}
		\end{column}
		\begin{column}{0.5\textwidth}  %%<--- here
			\centering $-a$
			\begin{table}[]
				\begin{tabular}{c|cc}
					-0  & = & 1 \\ \hline
					-1 & = & 4 \\
					-2  & = & 3 \\
					-3  & = & 2 \\
					-4  & = & 1
				\end{tabular}
			\end{table}
		\end{column}
	\end{columns}
\end{frame}

\begin{frame}[t]{Prime fields}{Multiplicative inverse}
	Given $GF(5)$ and $a, b \in GF(5)$
	\medskip

	\begin{columns}
		\begin{column}{0.5\textwidth}
			\centering $\times(a, b) = a * b \mod 5$
			\begin{table}[]
				\begin{tabular}{c|ccccc}
					\textbf{$\times$} & \textbf{0} & \textbf{1} & \textbf{2} & \textbf{3} & \textbf{4} \\ \hline
					\textbf{0} & 0          & 0          & 0          & 0          & 0          \\
					\textbf{1} & 0          & 1          & 2          & 3          & 4          \\
					\textbf{2} & 0          & 2          & 4          & 1          & 3          \\
					\textbf{3} & 0          & 3          & 1          & 4          & 2          \\
					\textbf{4} & 0          & 4          & 3          & 2          & 1
				\end{tabular}
			\end{table}
		\end{column}
		\begin{column}{0.5\textwidth}  %%<--- here
			\centering $a^{-1}$
			\begin{table}[]
				\begin{tabular}{c|cc}
					$0^{-1}$  & = & Doesn't exist \\ \hline
					$1^{-1}$  & = & 1 \\
					$2^{-1}$  & = & 3 \\
					$3^{-1}$  & = & 2 \\
					$4^{-1}$  & = & 4
				\end{tabular}
			\end{table}
		\end{column}
	\end{columns}
\end{frame}

\begin{frame}[t]{Prime fields}{GF(2)}
	Given $GF(2)$ and $a, b \in GF(2)$
	\bigskip
	\medskip
	\begin{columns}
		\begin{column}{0.5\textwidth}
			\centering $\oplus(a, b) = a + b \mod 2$
			\begin{table}[]
				\begin{tabular}{c|cc}
					\textbf{$\oplus$} & \textbf{0} & \textbf{1} \\ \hline
					\textbf{0} & 0         & 1           \\
					\textbf{1} &1          & 0           \\

				\end{tabular}
			\end{table}
		\end{column}
		\begin{column}{0.5\textwidth}  %%<--- here
			\centering $\wedge(a, b) = a * b \mod 2$
			\begin{table}[]
				\begin{tabular}{c|cc}
					\textbf{$\wedge$} & \textbf{0} & \textbf{1} \\ \hline
					\textbf{0} & 0          & 0                 \\
					\textbf{1} & 0          & 1                 \\

				\end{tabular}
			\end{table}
		\end{column}
	\end{columns}
\end{frame}

\begin{frame}[t]{Extension Fields}{$GF(2^m)$}
	When m is different than 1. The operations regarding our fields change. In the case of the AES, and for convenient reasons, m is equal to 8.

	A field $F$ defined as $GF(2^m)$ will be called an extension field.

	Before writing the definition of the multiplication operation over $G(2^8)$ let's introduce the notion of polynomials with coefficients in $G(2^8)$.

\end{frame}

\begin{frame}[t]{Polynomials with coefficients in $G(2^8)$}{Definition}
	In the \textbf{AES} algorithm will be presented as the concatenation of its individual bit values (0 or 1) between braces in the order $\{ b_7, b_6, b_5, b_4, b_3, b_2, b_1, b_0 \}$. As we have 8 bits, we can store it exactly in 1 byte of memory. Let's write these elements using a polynomial representation: \bigskip

	$b_7x^7 + b_6x^6 + b_5x^5 + b_4x^4 + b_3x^3 + b_2x^2 + b_1x^1 + b_0 = \sum_{i=0}^{7}b_ix^i$. \\[10pt]

	For example, $\{01100011\}$ identifies the specific finite field element $x^6 + x^5 + x + 1$.

\end{frame}

\begin{frame}[t]{Polynomials with coefficients in $G(2^8)$}{Representation}
	It is also convenient to denote byte values using hexadecimal notation with each of two groups of four bits being denoted by a single character as followed: \\[5pt]

	\begin{columns}
		\tiny
		\setlength{\tabcolsep}{3pt}
		\begin{column}{0.5\textwidth}
			\begin{table}[]
				\begin{tabular}{c|c}
					\textbf{Bit Pattern} & \textbf{Character} \\ \hline
					\textbf{0000} & 0                  \\
					\textbf{0001} & 1                  \\
					\textbf{0010} & 2                  \\
					\textbf{0011} & 3                  \\
					\textbf{0100} & 4                  \\
					\textbf{0101} & 5                  \\
					\textbf{0110} & 6                  \\
					\textbf{0111} & 7                  \\
				\end{tabular}
			\end{table}
		\end{column}

		\begin{column}{0.5\textwidth}  %%<--- here
			\begin{table}[]
				\begin{tabular}{c|c}
					\textbf{Bit Pattern} & \textbf{Character} \\ \hline
					\textbf{1000} & 8                  \\
					\textbf{1001} & 9                  \\
					\textbf{1010} & A                  \\
					\textbf{1011} & B                  \\
					\textbf{1100} & C                  \\
					\textbf{1101} & D                  \\
					\textbf{1110} & E                  \\
					\textbf{1111} & F                  \\

				\end{tabular}
			\end{table}
		\end{column}
	\end{columns}

	\centering Figure 1. Hexadecimal representation of bit patterns.
\end{frame}

\begin{frame}[t]{Polynomials with coefficients in $G(2^8)$}{Example}

	For example, the following expressions are equivalent to one another:
	\medskip

	(polynomial notation);
	$(x^6 + x^4 + x^2 + x + 1) + (x^7 + x + 1) = x^7 + x^6 + x^4 + x^2$

	\bigskip

	(binary notation); \\
	$\{01010111\} \wedge \{10000011\} = \{11010100\}$

	\bigskip

	(hexadecimal notation);	\\
	$\{57\} \wedge \{83\} = \{D4\}$

\end{frame}

\begin{frame}[t]{Polynomials with coefficients in $G(2^8)$}{Multiplication}
	In the polynomial representation, multiplication in $G(2^8)$ (denoted as $bullet$) corresponds to:

	\medskip

	Let $a(x), b(x) \in GF(2^8)$

	\begin{enumerate}[i.]
		\item Compute $a(x) * b(x)$ as in normal polynomial calculus.
		\item Apply the modulo using an \textbf{irreducible polynomial} $m(x)$ of degree $8$.

	\end{enumerate}
	\begin{itemize}
		\item A polynomial is irreducible if its only divisors are $1$ and itself.
		\item Doing this, we ensure that $a(x) \bullet b(x) \in GF(2^8)$.
		\item In the AES, $m(x) = x^8 + x^4 + x^3 + x + 1$
	\end{itemize}

\end{frame}

\begin{frame}[t]{Polynomials with coefficients in $G(2^8)$}{Multiplication example}

	For example, $\{57\} \bullet \{83\} = \{C1\}$, because
	\medskip

	\begin{center}
		\small
		\begin{tabular}{ccl}\
			$(x^6 + x^4 + x^2 + x + 1)(x^7 + x + 1)$ & = &  $x^{13} + x^{11} + x^9 + x^8 + x^7 + x^7 + $ \\
			& & $ x^5 + x^3 + x^2 + x + x^6 + x^4 + x^2 $ \\
			& & $+ x + 1$ \\
			& = & $x^{13} + x^{11} + x^9 + x^8 + x^6 + x^5 + $ \\
			& & $ x^4 + x^3 + 1$\\

	\end{tabular}\end{center}

	and

	\begin{center}
		\small
			$x^{13} + x^{11} + x^9 + x^8 + x^6 + x^5 + x^4 + x^3 + 1 \text{ modulo} (x^8 + x^4 + x^3 + x + 1) $ \\
			$ = x^{7} + x^6 +1 $ \\

	\end{center}
\end{frame}

\begin{frame}[t]{Polynomials with coefficients in $G(2^8)$}{The xtime operation}
	Multiplying the binary polynomial previously defined with the polynomial $x$ results in \\
	\medskip

	\centering{ $b_7x^8 + b_6x^7 + b_5x^6 + b_4x^5 + b_3x^4 + b_2x^3 + b_1x^2 + b_0x$.}

	\medskip

	\begin{itemize}
		\item $x \bullet b(x)$ is obtained by reducing the above result modulo $m(x)$.
		\item If $b_7 = 0$, the result is already in reduced form.
		\item If $b_7=1$, the reduction is accomplished by subtracting the polynomial $m(x)$.
		\item It follows  that multiplication by $x$ ($\{02\}$) can be implemented at the byte level as a left shift and a subsequent conditional bit wise XOR with $\{1B\}$.
	\end{itemize}

\end{frame}

\begin{frame}[t]{Polynomials with coefficients in $G(2^8)$}{Other Example}

	For example, $\{57\} \bullet \{13\} = \{FE\}$, because
	\medskip

	\begin{center}
		\small
		\begin{tabular}{ccccccl}

			$\{57\}$ & $\bullet$ & $\{02\}$ & = &  $xtime(\{57\})$ & = & $\{AE\}$ \\
			$\{57\}$ & $\bullet$ & $\{04\}$ & = &  $xtime(\{AE\})$ & = & $\{47\}$ \\
			$\{57\}$ & $\bullet$ & $\{08\}$ & = &  $xtime(\{47\})$ & = & $\{8E\}$ \\
			$\{57\}$ & $\bullet$ & $\{10\}$ & = &  $xtime(\{8E\})$ & = & $\{07\}$ \\

	\end{tabular}\end{center}

	thus,

	\begin{center}
		\small
		\begin{tabular}{ccl}\
			$\{57\} \bullet \{13\}$ & = &  $\{57\} \bullet (\{01\} \oplus \{02\} \oplus \{10\})$ \\
			& = & $\{57\} \oplus \{AE\} \oplus \{07\}$ \\
			& = &  $FE$ \\

	\end{tabular}\end{center}
\end{frame}

\begin{frame}[t]{Polynomials with coefficients in $G(2^8)$}{Inversion in $GF(2^8)$}
	For any non-zero binary polynomial $b(x)$ of degree less than 8, the multiplicative inverse of $b(x)$, denoted $b^{-1}(x)$, can be found as follows: the extended Euclidean algorithm is used to compute polynomials $a(x)$ and $c(x)$ such that \\
	\medskip

	\centering{ $b(x)a(x) + m(x)c(x) = 1$}

	\medskip

	\begin{flushleft}
		Hence, $a(x) \bullet b(x) \mod m(x) = 1$, which means
	\end{flushleft}

	\centering{ $b^{-1}(x) = a(x) \mod  m(x)$.}

	\medskip
	\begin{flushleft}
		Moreover, for any $a(X) \text{,} b(x) \text{ and } c(x)$ in the field, it holds that
	\end{flushleft}

	\centering{ $a(x) \bullet (b(x)+c(x)) = a(x) \bullet b(x) + a(x) \bullet c(x)$}

\end{frame}

\section{The SubBytes and InvSubBytes Operations}

\subsection{The SubBytes}

\begin{frame}[t]{SubBytes}{Definition}
	\begin{itemize}
		\item Non-linear byte substitution that operates independently on each byte of the State.
		\item Constructed by composing two transformations
			\begin{enumerate}
				\item Take the multiplicative inverse in the finite field GF($2^8$); the element $\{00\}$ is mapped to itself.
				\item Apply the following affine transformation (over GF(2)):
				\begin{center}
					$b_i^{'}= b_i \oplus b_{(i+4)mod8} \oplus b_{(i+5)mod8} \oplus b_{(i+6)mod8} \oplus b_{(i+7)mod8} \oplus c_i$
				\end{center}
				for $0 \leq i < 8$, where $b_i$ is the $i^{th}$ bit of the byte, and $c_i$ is the $i^{th}$ bit of a byte. $c$ with the value $\{63\}$ or $\{01100011\}$
			\end{enumerate}
		\item Is invertible.
	\end{itemize}

\end{frame}

\begin{frame}[t]{SubBytes}{The Affine transformation}

	In matrix form, the affine transformation element of the S-box can be expressed as:

	\[
	\begin{bmatrix}
	b_0^{'} \\
	b_1^{'} \\
	b_2^{'} \\
	b_3^{'} \\
	b_4^{'} \\
	b_5^{'} \\
	b_6^{'} \\
	b_7^{'} \\
	\end{bmatrix}
	=
	\begin{bmatrix}
	1 & 0 & 0 & 0 & 1 & 1 & 1 & 1\\
	1 & 1 & 0 & 0 & 0 & 1 & 1 & 1\\
	1 & 1 & 1 & 0 & 0 & 0 & 1 & 1\\
	1 & 1 & 1 & 1 & 0 & 0 & 0 & 1\\
	1 & 1 & 1 & 1 & 1 & 0 & 0 & 0\\
	0 & 1 & 1 & 1 & 1 & 1 & 0 & 0\\
	0 & 0 & 1 & 1 & 1 & 1 & 1 & 0\\
	0 & 0 & 0 & 1 & 1 & 1 & 1 & 1\\

	\end{bmatrix}
	\begin{bmatrix}
	b_0 \\
	b_1 \\
	b_2 \\
	b_3 \\
	b_4 \\
	b_5 \\
	b_6 \\
	b_7 \\
	\end{bmatrix}
	+
	\begin{bmatrix}
	1 \\
	1 \\
	0 \\
	0 \\
	0 \\
	1 \\
	1 \\
	0 \\
	\end{bmatrix}
	\]

\end{frame}

\begin{frame}[t]{SubBytes}{S-Box Figure}

	The following figure illustrates the effect of the \textbf{SubBytes()} transformation on the State:

	\medspace

	\begin{center}
		\includegraphics[scale=0.8]{images/s_box}
	\end{center}

\end{frame}

\begin{frame}[t]{Sbox}
	\small
	The S-box used in the \textbf{SubBytes()} transformation is presented in hexadecimal for as in figure. \\
	For example, if $s_{1,1} = {53}$, then the substitution value would be determined by the intersection of the row with index '5' and the column with index '3'. This would result in $s^{'}_{1,1}$ having a value of ${ED}$.

	\begin{table}[]
		\tiny
		\setlength{\tabcolsep}{2pt}
		\begin{tabular}{c|cccccccccccccccc}
			& 0    &    1 &    2 &    3 &    4 &    5 &    6 &    7 &    8 &    9 &    A &    B &  C &   D  &  E &  F \\ \hline
			0 & 0x63 & 0x7C & 0x77 & 0x7B & 0xF2 & 0x6B & 0x6F & 0xC5 & 0x30 & 0x01 & 0x67 & 0x2B & 0xFE & 0xD7 & 0xAB & 0x76 \\
			1 & 0xCA & 0x82 & 0xC9 & 0x7D & 0xFA & 0x59 & 0x47 & 0xF0 & 0xAD & 0xD4 & 0xA2 & 0xAF & 0x9C & 0xA4 & 0x72 & 0xC0 \\
			2 & 0xB7 & 0xFD & 0x93 & 0x26 & 0x36 & 0x3F & 0xF7 & 0xCC & 0x34 & 0xA5 & 0xE5 & 0xF1 & 0x71 & 0xD8 & 0x31 & 0x15 \\
			3 & 0x04 & 0xC7 & 0x23 & 0xC3 & 0x18 & 0x96 & 0x05 & 0x9A & 0x07 & 0x12 & 0x80 & 0xE2 & 0xEB & 0x27 & 0xB2 & 0x75 \\
			4 & 0x09 & 0x83 & 0x2C & 0x1A & 0x1B & 0x6E & 0x5A & 0xA0 & 0x52 & 0x3B & 0xD6 & 0xB3 & 0x29 & 0xE3 & 0x2F & 0x84 \\
			5 & 0x53 & 0xD1 & 0x00 & 0xED & 0x20 & 0xFC & 0xB1 & 0x5B & 0x6A & 0xCB & 0xBE & 0x39 & 0x4A & 0x4C & 0x58 & 0xCF \\
			6 & 0xD0 & 0xEF & 0xAA & 0xFB & 0x43 & 0x4D & 0x33 & 0x85 & 0x45 & 0xF9 & 0x02 & 0x7F & 0x50 & 0x3C & 0x9F & 0xA8 \\
			7 & 0x51 & 0xA3 & 0x40 & 0x8F & 0x92 & 0x9D & 0x38 & 0xF5 & 0xBC & 0xB6 & 0xDA & 0x21 & 0x10 & 0xFF & 0xF3 & 0xD2 \\
			8 & 0xCD & 0x0C & 0x13 & 0xEC & 0x5F & 0x97 & 0x44 & 0x17 & 0xC4 & 0xA7 & 0x7E & 0x3D & 0x64 & 0x5D & 0x19 & 0x73 \\
			9 & 0x60 & 0x81 & 0x4F & 0xDC & 0x22 & 0x2A & 0x90 & 0x88 & 0x46 & 0xEE & 0xB8 & 0x14 & 0xDE & 0x5E & 0x0B & 0xDB \\
			A & 0xE0 & 0x32 & 0x3A & 0x0A & 0x49 & 0x06 & 0x24 & 0x5C & 0xC2 & 0xD3 & 0xAC & 0x62 & 0x91 & 0x95 & 0xE4 & 0x79 \\
			B & 0xE7 & 0xC8 & 0x37 & 0x6D & 0x8D & 0xD5 & 0x4E & 0xA9 & 0x6C & 0x56 & 0xF4 & 0xEA & 0x65 & 0x7A & 0xAE & 0x08 \\
			C & 0xBA & 0x78 & 0x25 & 0x2E & 0x1C & 0xA6 & 0xB4 & 0xC6 & 0xE8 & 0xDD & 0x74 & 0x1F & 0x4B & 0xBD & 0x8B & 0x8A \\
			D & 0x70 & 0x3E & 0xB5 & 0x66 & 0x48 & 0x03 & 0xF6 & 0x0E & 0x61 & 0x35 & 0x57 & 0xB9 & 0x86 & 0xC1 & 0x1D & 0x9E \\
			E & 0xE1 & 0xF8 & 0x98 & 0x11 & 0x69 & 0xD9 & 0x8E & 0x94 & 0x9B & 0x1E & 0x87 & 0xE9 & 0xCE & 0x55 & 0x28 & 0xDF \\
			F & 0x8C & 0xA1 & 0x89 & 0x0D & 0xBF & 0xE6 & 0x42 & 0x68 & 0x41 & 0x99 & 0x2D & 0x0F & 0xB0 & 0x54 & 0xBB & 0x16
		\end{tabular}
	\end{table}
\end{frame}

\subsection{The InvSubBytes}

\begin{frame}[t]{InvSubBytes}{Definition}
	\begin{itemize}
		\item The S-Box can be inversed by applying the inverse of each operation in reverse order.
		\item In the S-Box computation we first applied the Galois Field inverse and then the affine function.
		\item We will apply the inverse of the affine function (which is also affine) and then the inverse of the Galois field which is the function itself.
		\item The derivations of the inverse of the affine function are vaguely specified in the bibliography consulted.
		\item As in the SubBytes operation, we will try to build a lookup table.
	\end{itemize}

\end{frame}

\begin{frame}[t]{InvSubBytes}{The inverse Affine transformation}

	In matrix form, the affine transformation element of the S-box can be expressed as:

	\[
	\begin{bmatrix}
	b_0^{'} \\
	b_1^{'} \\
	b_2^{'} \\
	b_3^{'} \\
	b_4^{'} \\
	b_5^{'} \\
	b_6^{'} \\
	b_7^{'} \\
	\end{bmatrix}
	=
	\begin{bmatrix}
	0 & 0 & 1 & 0 & 0 & 1 & 0 & 1\\
	1 & 0 & 0 & 1 & 0 & 0 & 1 & 0\\
	0 & 1 & 0 & 0 & 1 & 0 & 0 & 1\\
	1 & 0 & 1 & 0 & 0 & 1 & 0 & 0\\
	0 & 1 & 0 & 1 & 0 & 0 & 1 & 0\\
	0 & 0 & 1 & 0 & 1 & 0 & 0 & 1\\
	1 & 0 & 0 & 1 & 0 & 1 & 0 & 0\\
	0 & 1 & 0 & 0 & 1 & 0 & 1 & 0\\

	\end{bmatrix}
	\begin{bmatrix}
	b_0 \\
	b_1 \\
	b_2 \\
	b_3 \\
	b_4 \\
	b_5 \\
	b_6 \\
	b_7 \\
	\end{bmatrix}
	+
	\begin{bmatrix}
	1 \\
	0 \\
	1 \\
	0 \\
	0 \\
	0 \\
	0 \\
	0 \\
	\end{bmatrix}
	\]

\end{frame}

\begin{frame}[t]{InvSbox}
	\small
	The Inv-S-box used in the \textbf{InvSubBytes()} transformation is presented in hexadecimal for as in figure. \\
	For example, if $s_{1,1} = {53}$, then the substitution value would be determined by the intersection of the row with index '5' and the column with index '3'. This would result in $s^{'}_{1,1}$ having a value of ${50}$.

	\begin{table}[]
		\tiny
		\setlength{\tabcolsep}{2pt}
		\begin{tabular}{c|cccccccccccccccc}
			& 0    &    1 &    2 &    3 &    4 &    5 &    6 &    7 &    8 &    9 &    A &    B &  C &   D  &  E &  F \\ \hline
			0 & 0x52 & 0x09 & 0x6A & 0xD5 & 0x30 & 0x36 & 0xA5 & 0x38 & 0xBF & 0x40 & 0xA3 & 0x9E & 0x81 & 0xF3 & 0xD7 & 0xFB \\
			1 & 0x7C & 0xE3 & 0x39 & 0x82 & 0x9B & 0x2F & 0xFF & 0x87 & 0x34 & 0x8E & 0x43 & 0x44 & 0xC4 & 0xDE & 0xE9 & 0xCB \\
			2 & 0x54 & 0x7B & 0x94 & 0x32 & 0xA6 & 0xC2 & 0x23 & 0x3D & 0xEE & 0x4C & 0x95 & 0x0B & 0x42 & 0xFA & 0xC3 & 0x4E \\
			3 & 0x08 & 0x2E & 0xA1 & 0x66 & 0x28 & 0xD9 & 0x24 & 0xB2 & 0x76 & 0x5B & 0xA2 & 0x49 & 0x6D & 0x8B & 0xD1 & 0x25 \\
			4 & 0x72 & 0xF8 & 0xF6 & 0x64 & 0x86 & 0x68 & 0x98 & 0x16 & 0xD4 & 0xA4 & 0x5C & 0xCC & 0x5D & 0x65 & 0xB6 & 0x92 \\
			5 & 0x6C & 0x70 & 0x48 & 0x50 & 0xFD & 0xED & 0xB9 & 0xDA & 0x5E & 0x15 & 0x46 & 0x57 & 0xA7 & 0x8D & 0x9D & 0x84 \\
			6 & 0x90 & 0xD8 & 0xAB & 0x00 & 0x8C & 0xBC & 0xD3 & 0x0A & 0xF7 & 0xE4 & 0x58 & 0x05 & 0xB8 & 0xB3 & 0x45 & 0x06 \\
			7 & 0xD0 & 0x2C & 0x1E & 0x8F & 0xCA & 0x3F & 0x0F & 0x02 & 0xC1 & 0xAF & 0xBD & 0x03 & 0x01 & 0x13 & 0x8A & 0x6B \\
			8 & 0x3A & 0x91 & 0x11 & 0x41 & 0x4F & 0x67 & 0xDC & 0xEA & 0x97 & 0xF2 & 0xCF & 0xCE & 0xF0 & 0xB4 & 0xE6 & 0x73 \\
			9 & 0x96 & 0xAC & 0x74 & 0x22 & 0xE7 & 0xAD & 0x35 & 0x85 & 0xE2 & 0xF9 & 0x37 & 0xE8 & 0x1C & 0x75 & 0xDF & 0x6E \\
			A & 0x47 & 0xF1 & 0x1A & 0x71 & 0x1D & 0x29 & 0xC5 & 0x89 & 0x6F & 0xB7 & 0x62 & 0x0E & 0xAA & 0x18 & 0xBE & 0x1B \\
			B & 0xFC & 0x56 & 0x3E & 0x4B & 0xC6 & 0xD2 & 0x79 & 0x20 & 0x9A & 0xDB & 0xC0 & 0xFE & 0x78 & 0xCD & 0x5A & 0xF4 \\
			C & 0x1F & 0xDD & 0xA8 & 0x33 & 0x88 & 0x07 & 0xC7 & 0x31 & 0xB1 & 0x12 & 0x10 & 0x59 & 0x27 & 0x80 & 0xEC & 0x5F \\
			D & 0x60 & 0x51 & 0x7F & 0xA9 & 0x19 & 0xB5 & 0x4A & 0x0D & 0x2D & 0xE5 & 0x7A & 0x9F & 0x93 & 0xC9 & 0x9C & 0xEF \\
			E & 0xA0 & 0xE0 & 0x3B & 0x4D & 0xAE & 0x2A & 0xF5 & 0xB0 & 0xC8 & 0xEB & 0xBB & 0x3C & 0x83 & 0x53 & 0x99 & 0x61 \\
			F & 0x17 & 0x2B & 0x04 & 0x7E & 0xBA & 0x77 & 0xD6 & 0x26 & 0xE1 & 0x69 & 0x14 & 0x63 & 0x55 & 0x21 & 0x0C & 0x7D
		\end{tabular}
	\end{table}
\end{frame}


\documentclass{article}

\title{ICPC Notebook}
\author{pedroteosousa}
\date{}

\usepackage[utf8]{inputenc}
\usepackage[hidelinks]{hyperref}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{multicol}
\usepackage[a4paper, margin=0.5in]{geometry}

\lstset{
	tabsize=4,
	basicstyle={\small\ttfamily},
	showstringspaces=false,
	commentstyle=\color{black},
	keywordstyle=\color{blue},
	stringstyle=\color{red}
}

\begin{document}
%\begin{multicols}{2}

\maketitle
\tableofcontents

\section{Geometry}
\subsection{Basic}
\begin{lstlisting}[language=C++]
struct point {
    coord x, y;
    point(): x(0), y(0) {}
    point(coord a, coord b): x(a), y(b) {}

    inline point operator+ (point o) { return {x + o.x, y + o.y}; }
    inline point operator- (point o) { return {x - o.x, y - o.y}; }
    inline point operator* (coord o) { return {x * o, y * o}; }
    inline point operator/ (coord o) { return {x / o, y / o}; }

    inline coord operator* (point o) { return x * o.x + y * o.y; }
    inline coord operator^ (point o) { return x * o.y - y * o.x; }

    inline bool operator< (point o) { return make_pair(x, y) < make_pair(o.x, o.y); }

    inline bool operator== (point o) { return abs(x-o.x) < eps && abs(y-o.y) < eps; }

    // cw angle
    inline double ang(point o) {
        point p = *this;
        return atan2(-(p ^ o), p * o);
    }

    inline coord sqr() { return x * x + y * y; }
    inline double len() { return sqrt(sqr()); }

    // rotate cw
    inline point rot90() { return {y, -x}; }
    inline point rotate(double a) { return {cos(a)*x + sin(a)*y, -sin(a)*x + cos(a)*y}; }

    inline int ccw(point o) { coord a = (*this) ^ o; return (eps < a) - (a < -eps); }
    inline int dir(point o) { coord a = (*this) * o; return (eps < a) - (a < -eps); }

    bool in_seg(point a, point b) {
        point p = *this;
        return (p-a).ccw(b-a) == 0 && (p-a).dir(p-b) <= 0;
    }
    double dist_line(point a, point b) {
        point p = *this;
        return (b-a).sqr() <= eps ? (p-a).sqr() : double(abs((a-p) ^ (b-p))) / (b-a).len();
    }
    double dist_seg(point a, point b) {
        point p = *this;
        return (p-a).dir(p-b) <= 0 ? dist_line(a, b) : min((p-a).len(), (p-b).len());
    }
};

struct line {
    point p; coord c;
    line() {}
    line(point s, point e): p((s-e).rot90()), c(p*s) {}
    point inter(line o) {
        if (p.ccw(o.p) == 0) throw 1;
        coord d = (p ^ o.p);
        return point((c * o.p.y - p.y * o.c) / d, (o.c * p.x - o.p.x * c) / d);
    }
};

bool inter_seg(point a, point b, point c, point d) {
    if (a.in_seg(c, d) || b.in_seg(c, d) || c.in_seg(a, b) || d.in_seg(a, b)) return true;
    return ((c-a).ccw(b-a) * (d-a).ccw(b-a) == -1 && (a-c).ccw(d-c) * (b-c).ccw(d-c) == -1);
}\end{lstlisting}
\subsection{Convex Hull}
\begin{lstlisting}[language=C++]
double side(point a, point b, point c) {
	return (a^b) + (b^c) + (c^a);
}

vector<point> convex_hull(vector<point> p) {
	int n = p.size(), k = 0;
	if (n == 1) return p;
	vector<point> hull(2*n);

	sort(p.begin(), p.end());

	for(int i=0; i<n; i++) {
		// use <= when including collinear points
		while(k>=2 && (side(hull[k-2], hull[k-1], p[i]) < 0))
			k--;
		hull[k++] = p[i];
	}

	for(int i=n-2,t=k+1; i>=0; i--) {
		while(k>=t && (side(hull[k-2], hull[k-1], p[i]) < 0))
			k--;
		hull[k++] = p[i];
	}

	hull.resize(k-1);
	return hull;
}
\end{lstlisting}
\section{Graph Algorithms}
\subsection{Tarjan}
\begin{lstlisting}[language=C++]
const int inf = 1791791791;

vector<int> adj[N];

// time complexity: O(V+E)
stack<int> ts;
int tme = 0, ncomp = 0, low[N], seen[N];
int comp[N]; // nodes in the same scc have the same color
int scc_dfs(int n) {
	seen[n] = low[n] = ++tme;
	ts.push(n);
	for (auto a : adj[n]) {
		if (seen[a] == 0)
			scc_dfs(a);
		low[n] = min(low[n], low[a]);
	}
	if (low[n] == seen[n]) {
		int node;
		do {
			node = ts.top(); ts.pop();
			comp[node] = ncomp;
			low[node] = inf;
		} while (n != node && ts.size());
		ncomp++;
	}
	return low[n];
}\end{lstlisting}
\subsection{Lowest Common Ancestor}
\begin{lstlisting}[language=C++]
const int N = 1e6 + 5;
const int L = 20;

vector<int> adj[N];
int prof[N], p[N][L+5];

void dfs(int v, int h = 1) {
	prof[v] = h;
	if (h == 1) p[v][0] = v;
	for (auto u : adj[v])
		if (prof[u] == 0) {
			p[u][0] = v;
			dfs(u, h+1);
		}
}

void init(int n) {
	for (int i = 1; i <= L; i++)
		for (int j = 1; j < n; j++)
			p[j][i] = p[p[j][i-1]][i-1];
}

int lca(int u, int v) {
	if (prof[u] < prof[v]) swap(u, v);
	for (int i = L; i >= 0; i--)
		if (prof[p[u][i]] >= prof[v])
			u = p[u][i];
	for (int i = L; i >= 0; i--)
		if (p[u][i] != p[v][i]) {
			u = p[u][i];
			v = p[v][i];
		}
	while (u != v) {
		u = p[u][0];
		v = p[v][0];
	}
	return u;
}
\end{lstlisting}
\subsection{Centroid}
\begin{lstlisting}[language=C++]
vector<int> adj[N], centroid[N];
int sze[N];

int dfs(int v, int p = 0) {
    sze[v] = 1;
    for (int u: adj[v])
        if (u != p && sze[u] != -1) sze[v] += dfs(u, v);
    return sze[v];
}

// returns root of centroid tree
int build(int v) {
    int n = dfs(v, v);
    int w = v;
    do {
        v = w;
        for (int u: adj[v])
            if (sze[u] != -1 && sze[u] < sze[v] && 2 * sze[u] >= n)
                w = u;
    } while (v != w);
    sze[v] = -1;
    for (int u: adj[v])
        if (sze[u] != -1)
            centroid[v].push_back(build(u));
    return v;
}\end{lstlisting}
\section{Flow}
\subsection{Dinic's Algorithm}
\begin{lstlisting}[language=C++]
struct dinic {
	struct edge {
		int from, to;
		ll c, f;
	};
	vector<edge> edges;
	vector<int> adj[N];

	void addEdge(int i, int j, ll c) {
		edges.push_back({i, j, c, 0}); adj[i].push_back(edges.size() - 1);
		edges.push_back({j, i, 0, 0}); adj[j].push_back(edges.size() - 1);
	}

	int turn, seen[N], dist[N], st[N];
	bool bfs (int s, int t) {
		seen[t] = ++turn;
		dist[t] = 0;
		queue<int> q({t});
		while (q.size()) {
			int u = q.front(); q.pop();
			st[u] = 0;
			for (auto e : adj[u]) {
				int v = edges[e].to;
				if (seen[v] != turn && edges[e^1].c != edges[e^1].f) {
					seen[v] = turn;
					dist[v] = dist[u] + 1;
					q.push(v);
				}
			}
		}
		return seen[s] == turn;
	}

	ll dfs(int s, int t, ll f) {
		if (s == t || f == 0)
			return f;
		for (int &i = st[s]; i < adj[s].size(); i++) {
			int e = adj[s][i], v = edges[e].to;
			if (seen[v] == turn && dist[v] + 1 == dist[s] && edges[e].c > edges[e].f) {
				if (ll nf = dfs(v, t, min(f, edges[e].c - edges[e].f))) {
					edges[e].f += nf;
					edges[e^1].f -= nf;
					return nf;
				}
			}
		}
		return 0ll;
	}

	ll max_flow(int s, int t) {
		ll resp = 0ll;
		while (bfs(s, t))
			while (ll val = dfs(s, t, inf))
				resp += val;
		return resp;
	}
};
\end{lstlisting}
\subsection{Min Cost}
\begin{lstlisting}[language=C++]
typedef long long ll;
const ll inf = 1e12;

struct min_cost {
	struct edge {
		int from, to;
		ll cp, fl, cs;
	};
	vector<edge> edges;
	vector<int> adj[N];

	void addEdge(int i, int j, ll cp, ll cs) {
		edges.push_back({i, j, cp, 0, cs}); adj[i].push_back(edges.size() - 1);
		edges.push_back({j, i, 0, 0, -cs}); adj[j].push_back(edges.size() - 1);
	}

	ll seen[N], dist[N], pai[N], cost, flow;
	int turn;
	ll spfa(int s, int t) {
		turn++;
		queue<int> q; q.push(s);
		for (int i = 0; i < N; i++) dist[i] = inf;
		dist[s] = 0;
		seen[s] = turn;
		while (q.size()) {
			int u = q.front(); q.pop();
			seen[u] = 0;
			for (auto e : adj[u]) {
				int v = edges[e].to;
				if (edges[e].cp > edges[e].fl && dist[u] + edges[e].cs < dist[v]) {
					dist[v] = dist[u] + edges[e].cs;
					pai[v] = e ^ 1;
					if (seen[v] < turn) {
						seen[v] = turn;
						q.push(v);
					}
				}
			}
		}
		if (dist[t] == inf) return 0;
		ll nfl = inf;
		for (int u = t; u != s; u = edges[pai[u]].to)
			nfl = min(nfl, edges[pai[u] ^ 1].cp - edges[pai[u] ^ 1].fl);
		cost += dist[t] * nfl;
		for (int u = t; u != s; u = edges[pai[u]].to) {
			edges[pai[u]].fl -= nfl;
			edges[pai[u] ^ 1].fl += nfl;
		}
		return nfl;
	}

	void mncost(int s, int t) {
		cost = flow = 0;
		while (ll fl = spfa(s, t))
			flow += fl;
	}
};
\end{lstlisting}
\section{Data Structures}
\subsection{Trie}
\begin{lstlisting}[language=C++]
struct trie {
	struct node {
		int to[A], freq, end;
	};
	struct node t[N];
	int sz = 0;
	int offset = 'a';

	// init trie
	void init() {
		memset(t, 0, sizeof(struct node));
	}

	// insert string
	void insert(char *s, int p = 0) {
		t[p].freq++;
		if (*s == 0) {
			t[p].end++;
			return;
		}
		if (t[p].to[*s - offset] == 0)
			t[p].to[*s - offset] = ++sz;
		insert(s+1, t[p].to[*s - offset]);
	}

	// check if string is on trie
	int find(char *s, int p = 0) {
		if (*s == 0)
			return t[p].end;
		if (t[p].to[*s - offset] == 0)
			return false;
		return find(s+1, t[p].to[*s - offset]);
	}

	// count the number of strings that have this prefix
	int count(char *s, int p = 0) {
		if (*s == 0)
			return t[p].freq;
		if (t[p].to[*s - offset] == 0)
			return 0;
		return count(s+1, t[p].to[*s - offset]);
	}

	// erase a string
	int erase(char *s, int p = 0) {
		if (*s == 0 && t[p].end) {
			--t[p].end;
			return --t[p].freq;
		}
		if ((*s == 0 && t[p].end == 0) || t[p].to[*s - offset] == 0)
			return -1;
		int count = erase(s+1, t[p].to[*s - offset]);
		if (count == 0)
			t[p].to[*s - offset] = 0;
		if (count == -1)
			return -1;
		return --t[p].freq;
	}
};
\end{lstlisting}
\subsection{Binary Indexed Tree}
\begin{lstlisting}[language=C++]
int b[N];

int update(int p, int val, int n) {
	for(;p < n; p += p & -p) b[p] += val;
}

int getsum(int p) {
	int sum = 0;
	for(; p != 0; p -= p & -p) {
		sum += b[p];
	}
	return sum;
}
\end{lstlisting}
\subsection{Lazy Segment Tree}
\begin{lstlisting}[language=C++]
typedef long long ll;

const ll N = 1e5 + 5;
const ll inf = 1791791791;

struct seg_tree {
	ll seg[4*N];
	ll lazy[4*N];

	seg_tree() {
		memset(seg, 0, sizeof(seg));
		memset(lazy, 0, sizeof(lazy));
	}

	void do_lazy(ll root, ll left, ll right) {
		seg[root] += lazy[root];
		if (left != right) {
			lazy[2*root+1] += lazy[root];
			lazy[2*root+2] += lazy[root];
		}
		lazy[root] = 0;
	}

	// sum update
	ll update(ll l, ll r, ll val, ll left = 0, ll right = N-1, ll root = 0) {
		do_lazy(root, left, right);
		if (r < left || l > right) return seg[root];
		if (left >= l && right <= r) {
			lazy[root] += val;
			do_lazy(root, left, right);
			return seg[root];
		}
		ll update_left = update(l, r, val, left, (left+right)/2, 2*root+1);
		ll update_right = update(l, r, val, (left+right)/2+1, right, 2*root+2);
		return seg[root] = min(update_left, update_right);
	}

	ll query(ll l, ll r, ll left = 0, ll right = N-1, int root = 0) {
		do_lazy(root, left, right);
		if (r < left || l > right)
			return inf;
		if (left >= l && right <= r) return seg[root];
		ll query_left = query(l, r, left, (left+right)/2, 2*root+1);
		ll query_right = query(l, r, (left+right)/2+1, right, 2*root+2);
		return min(query_left, query_right);
	}
};
\end{lstlisting}
\subsection{Union Find}
\begin{lstlisting}[language=C++]
int p[N], w[N];

void init() {
	for (int i = 0; i < N; i++)
		w[p[i] = i] = 1;
}

int find(int x) {
	return p[x] = (x == p[x] ? x : find(p[x]));
}

void join(int a, int b) {
	if ((a = find(a)) == (b = find(b))) return;
	if (w[a] < w[b]) swap(a, b);
	w[a] += w[b];
	p[b] = a;
}\end{lstlisting}
\section{Mathematics}
\subsection{Matrix}
\begin{lstlisting}[language=C++]
template <int n> struct matrix {
	long long mat[n][n];
	matrix () {
		memset (mat, 0, sizeof (mat));
	}
	matrix (long long temp[n][n]) {
		memcpy (mat, temp, sizeof (mat));
	}
	void identity() {
		memset (mat, 0, sizeof (mat));
		for (int i=0;i<n;i++)
			mat[i][i] = 1;
	}
	matrix<n> mul (const matrix<n> &a, long long m) const {
		matrix<n> temp;
		for (int i=0; i<n; i++)
			for (int j=0; j<n; j++)
				for (int k=0; k<n; k++) {
					temp.mat[i][j] += (mat[i][k]*a.mat[k][j])%m;
					temp.mat[i][j] %= m;
				}
		return temp;
	}
	matrix<n> operator% (long long m) {
		matrix<n> temp(mat);
		for (int i=0; i<n; i++)
			for (int j=0; j<n; j++)
				temp.mat[i][j] %= m;
		return temp;
	}
	matrix<n> pow(long long e, long long m) {
		matrix<n> temp;
		if (e == 0) {
			temp.identity();
			return temp%m;
		}
		if (e == 1) {
			memcpy (temp.mat, mat, sizeof (temp.mat));
			return temp%m;
		}
		temp = pow(e/2, m);
		if (e % 2 == 0)
			return (temp.mul(temp, m))%m;
		else
			return (((temp.mul(temp, m))%m)*pow(1, m))%m;
	}
};
\end{lstlisting}
\subsection{Fast Fourier Transform}
\begin{lstlisting}[language=C++]
typedef complex<double> cpx;
const double pi = acos(-1.0);
// DFT if type = 1, IDFT if type = -1
// If you are multiplying, remember to let EACH vector with n >= sum of degrees of both polys
// n is required to be a power of 2
void FFT(vector<cpx> &v, vector<cpx> &ans, int n, int type, int p[]) { // p[n]
	assert(!(n & (n - 1))); int i, sz, o; p[0] = 0;
	for(i = 1; i < n; i++) p[i] = (p[i >> 1] >> 1) | ((i & 1)? (n >> 1) : 0);
	for(i = 0; i < n; i++) ans[i] = v[p[i]];
	for(sz = 1; sz < n; sz <<= 1) {
		const cpx wn(cos(type * pi / sz), sin(type * pi / sz));
		for(o = 0; o < n; o += (sz << 1)) {
			cpx w = 1;
			for(i = 0; i < sz; i++) {
				const cpx u = ans[o + i], t = w * ans[o + sz + i];
				ans[o + i] = u + t;
				ans[o + i + sz] = u - t;
				w *= wn;
			}
		}
	}
	if(type == -1) for(i = 0; i < n; i++) ans[i] /= n;
}\end{lstlisting}
\subsection{Extended Euclidean Algorithm}
\begin{lstlisting}[language=C++]
// x * a + y * b = gcd(a, b)
ll ext(ll a, ll b, ll &x, ll &y) {
	if (a == 0) {
		x = 0;
		y = 1;
		return b;
	}
	ll x1, y1;
	ll gcd = ext(b%a, a, x1, y1);

	x = y1 - (b/a)*x1;
	y = x1;

	return gcd;
}\end{lstlisting}
\subsection{Rabin-Miller Primality Test}
\begin{lstlisting}[language=C++]
long long llrand(long long mn, long long mx) {
	long long p = rand();
	p <<= 32ll;
	p += rand();
	return p%(mx-mn+1ll)+mn;
}

long long mul_mod(long long a, long long b, long long m) {
	long long x = 0, y = a%m;
	while (b) {
		if (b % 2)
			x = (x+y)%m;
		y = (2*y)%m;
		b >>= 1;
	}
	return x%m;
}

long long exp_mod(long long e, long long n, long long m) {
	if (n == 0)
		return 1ll;
	long long temp = exp_mod(e, n/2, m);
	if (n & 1)
		return mul_mod(mul_mod(temp, temp, m), e, m);
	else
		return mul_mod(temp, temp, m);
}

// complexity: O(t*log2^3(p))
bool isProbablyPrime(long long p, long long t=64) {
	if (p <= 1) return false;
	if (p <= 3) return true;
	srand(time(NULL));
	long long r = 0, d = p-1;
	while (d % 2 == 0) {
		r++;
		d >>= 1;
	}
	while (t--) {
		long long a = llrand(2, p-2);
		a = exp_mod(a, d, p);
		if (a == 1 || a == p-1) continue;
		for (int i=0; i<r-1; i++) {
			a = mul_mod(a, a, p);
			if (a == 1) return false;
			if (a == p-1) break;
		}
		if (a != p-1) return false;
	}
	return true;
}
\end{lstlisting}
\section{Strings}
\subsection{Z function}
\begin{lstlisting}[language=C++]
int z[N];

void Z(string s) {
	int n = s.size();
	int m = -1;
	for (int i = 1; i < n; i++) {
		z[i] = 0;
		if (m != -1 && m + z[m] >= i)
			z[i] = min(m + z[m] - i, z[i-m]);
		while (i + z[i] < n && s[i+z[i]] == s[z[i]])
			z[i]++;
		if (m == -1 || i + z[i] > m + z[m])
			m = i;
	}
}
\end{lstlisting}
\subsection{Knuth–Morris–Pratt Algorithm}
\begin{lstlisting}[language=C++]
int kmp[N];

void build(string p) {
	int n = p.size(), k = -1;
	kmp[0] = k;
	for (int i = 1; i < n+1; i++) {
		while (k >= 0 && p[k] != p[i-1]) k = kmp[k];
		kmp[i] = ++k;
	}
}

vector<int> match(string p, string s) {
	int n = s.size(), m = p.size(), j = 0;
	vector<int> matches;
	for (int i = 1; i < n+1; i++) {
		while (j >= 0 && p[j] != s[i-1]) j = kmp[j];
		if (++j == m) {
			matches.push_back(i-j+1);
			j = kmp[j];
		}
	}
	return matches;
}
\end{lstlisting}
\section{Miscellaneous}
\subsection{vim settings}
\begin{lstlisting}[language=]
set ai si noet ts=4 sw=4 sta sm nu rnu
inoremap <NL> <ESC>o
nnoremap <NL> o
inoremap <C-up> <C-o>:m-2<CR>
inoremap <C-down> <C-o>:m+1<CR>
nnoremap <C-up> :m-2<CR>
nnoremap <C-down> :m+1<CR>
vnoremap <C-up> :m-2<CR>gv
vnoremap <C-down> :m'>+1<CR>gv
syntax on
colors evening
highlight Normal ctermbg=none "No background
highlight nonText ctermbg=none
\end{lstlisting}

%\end{multicols}
\end{document}