\chapter{The $\alpha$-shapes approach}\label{chap:boundaries_alpha}
In the previous chapter, we presented a new ray tracing approach based on PS. We explained that, in order to compute the target intensity, it is necessary to know the boundaries of the regions in target PS with positive luminance. Ray tracing in PS requires tracing only the rays close to these boundaries. The rays traced can be seen as a point cloud in PS. To detect the shape formed by those rays, the $\alpha$-shapes approach is employed \cite{portegies2013fast}.\\ \indent
Methods based on $\alpha$-shapes are widely used to reconstruct an unknown shape formed by a set of finite data points \cite{guo1997surface}. $\alpha$-shapes is a very powerful tool to construct the shape of a point cloud. As the parameter $\alpha$ varies, we can obtain different $\alpha$-shapes from the point set itself to the convex
hull \cite{xu2003automatic}. The disadvantage of such method is that it can be very hard to choose the appropriate value of the parameter $\alpha$ and, in most cases it can be selected only by trial-and-error.\\ \indent
We developed a technique based on $\alpha$-shapes that gives a criterion to determine the value of the parameter $\alpha$, for which the boundaries are approximated well \cite{filosa2015new}.\\ \indent This chapter is organized as follows. An overview of the-state-of-the-art about $\alpha$-shape methods is provided in Section \ref{sec:alpha-shapes}; the technique used for computing the $\alpha$ value is explained in Section \ref{sec:Tir_alpha}; the results for two different kinds of total internal reflection (TIR)-collimators are given in Section \ref{sec:results-Tir-alpha}. Discussions and conclusions are provided in the last paragraph of this chapter.
\section{$\alpha$-shapes theory}\label{sec:alpha-shapes}
Given a finite set $\mbox{\insieme{U}} = \{\point{u}_1, \cdots, \point{u}_N\}\subset \mathbb{R}^2$ of points, $\alpha$-shapes are geometrical objects that give us an approximation of the shape formed by the point cloud. For now we do not further specify the notion of shape. A more precise definition will be provided later.\\ \indent
Before giving a formal definition, we explain an intuitive and nice interpretation of $\alpha$-shapes \cite{lucieer2004alpha}.
Let us think of a stracciatella ice-cream\footnote{Stracciatella ice cream is made with milk-based ice-cream and fine pieces of chocolate \cite{Wiki3}.}. If we desire to know the shape formed by the chocolate pieces we can start eating the ice cream using a spoon with a spherical scoop and try not to remove any piece of chocolate.
We will obtain a shape formed by arcs and points. % (see Figure \ref{fig:shape2d} for the two-dimensional case).
%\begin{figure}[t]\label{fig:shape2d}
%\begin{center}
%\includegraphics[width=\textwidth]{alpha_shape2D}
%\label{fig:shape}
%\caption{\textbf{Construction of $\alpha$-shapes.} The boundary of the shape (red line) formed by a set of points (blue dots) in $\mathbb{R}^2$ is detected for $\alpha = 1$ (left) and for $\alpha=2$ (right) \cite{sabel2017application}.}
%\label{fig:shape2d}
%\end{center}
%\end{figure}
Straightening the arcs to line segments we obtain broken lines which constitute the boundary of the so-called $\alpha$-shape of the point set $\mbox{\insieme{U}}$.
A very small spoon will allow us to eat the entire ice cream without eating any piece of chocolate, while with a larger spoon we are not able to eat any chunk of the ice cream without chocolate pieces. In this example, the chocolates pieces are the points of set $\mbox{\insieme{U}}$ and, the parameter $\alpha$ determines the radius of the carving spoon (the spherical spoon in two-dimension is simply a circle).\\ \indent
The formal definition of $\alpha$-shape was first given by Edelsbrunner, Kirkpatrick and Seidel in 1983 \cite{edelsbrunner1983shape}. They describe $\alpha$-shape as a generalization of the convex hull of a finite set of points in the plane. Let $\alpha$ be a non negative number $0\leq\alpha<\infty$.
If $\alpha = 0$ the shape degenerates to the point set $\mbox{\insieme{U}}$. On the other hand, when $\alpha\rightarrow\infty$ the $\alpha$-shape is simply the convex hull of \insieme{U}. If $0<\alpha<\infty$ the $\alpha$-shape is a polygon of \insieme{U} \cite{edelsbrunner1994three}. The construction of the $\alpha$-shape is closely related to the Delaunay triangulation of \insieme{U} \cite{mucke1993shapes}. Therefore, a formal definition of triangulation and Delaunay triangulation is now required. \\ \indent
Given a set $\mbox{\insieme{U}}$ of points not all aligned, let us consider the set \insieme{E} of all the straight-line segments whose endpoints are in \insieme{U}.
A triangulation \insieme{T} of $\mbox{\insieme{U}}$ is the subset of \insieme{E} with the maximum number of segments such that all the line segments of \insieme{T} intersect only at their endpoints \cite{lloyd1977triangulations}.
\\ \indent Before giving a more formal definition of triangulation, let us define a partition of a set $\mbox{\insieme{X}}\subset \mathbb{R}^2$ as a collection of the subsets which divide \insieme{X} into non-overlapping regions so that any point in \insieme{X} is located in only one region.
\begin{definition} \indent Let $\mbox{\insieme{P}}\subset \mathbb{R}^2$ be the convex hull of $\mbox{\insieme{U}}$ and $\mbox{\insieme{T}} = \{\mbox{\insieme{T}}_1, \cdots, \mbox{\insieme{T}}_\variabile{h}\}$ be a partition of $\mbox{\insieme{P}}$ into closed triangles, that is triangles that include their edges. Suppose that the following properties hold:
\begin{itemize}
\item[a)] $\mbox{\insieme{P}} = \bigcup_{\variabile{i} = 1}^{\variabile{h}}\mbox{\insieme{T}}_\variabile{i}$,
\item[b)] $\forall \mbox{ \insieme{T}}_\variabile{i}, \mbox{\insieme{T}}_\variabile{j} \in \mbox{\insieme{T}}$, $\mbox{\insieme{T}}_\variabile{i} \neq \mbox{\insieme{T}}_\variabile{j}$, and
\begin{equation*}
\textrm{int}(\mbox{\insieme{T}}_\variabile{i})\cap \textrm{int}(\mbox{\insieme{T}}_\variabile{j}) = \emptyset,
\end{equation*}
where $\textrm{int}(\mbox{\insieme{T}}) = \mbox{\insieme{T}}-\partial \mbox{\insieme{T}}$,
\end{itemize}
then $\mbox{\insieme{T}}$ is called a \textit{triangulation} of $\mbox{\insieme{P}}$ \cite{Numericalmethods}.
\end{definition}
The Delaunay triangulation $\mbox{\insieme{T}}^{\prime}$ of the point set \insieme{U} has the property that the circumcircle of any triangle of $\mbox{\insieme{T}}^{\prime}$ does not contain any point of \insieme{U}. This is called the Delaunay property. Next, a very commonly used algorithm to construct such triangulation is explained.\\ \indent
$\mbox{\insieme{T}}^\prime$ is constructed by modifying a general triangulation \insieme{T} such that every point satisfies the Delaunay property.
Therefore, every triangle that does not satisfy such property is flipped such that the new edge is part of the triangulation, see Figure \ref{fig:Delaunay}.
Given, for example, an arbitrary triangulation \insieme{T} in two-dimensions, for each edge $\overline{\point{ab}}$ in \insieme{T} which is not on the boundary of the convex hull the two triangles
$\Delta_{\point{abc}}$ and $\Delta_{\point{abd}}$ with the common edge $\overline{\point{ab}}$ are identified. Then, if either the circumcircle of triangle $\Delta_{\point{abc}}$ contains point \point{d} or the circumcircle of triangle $\Delta_{\point{abd}}$ contains point \point{c}, the edge $\overline{\point{ab}}$ cannot be included in the Delaunay triangulation and, therefore, $\overline{\point{ab}}$ is replaced by the edge $\overline{\point{cd}}$ such that the triangles $\Delta_{\point{acd}}$ and $\Delta_{\point{bcd}}$ are constructed. The new edge $\overline{\point{cd}}$ locally satisfies the Delaunay property and the triangles $\Delta_{\point{acd}}$ and  $\Delta_{\point{bcd}}$ are added to the Delaunay triangulation $\mbox{\insieme{T}}^\prime$.
\begin{figure}[t]\label{fig:Delaunay}
\begin{subfigure}[t]{0.48\textwidth}
\centering
\includegraphics[]{triangle_alpha_shapes}
\label{fig:shape}
\caption{\textbf{Not acceptable triangle.} The point \point{d} is inside the circle circumscribing the triangle $\Delta_{\point{abc}}$, therefore the edge $\overline{\point{ab}}$ cannot be included in the Delaunay triangulation.}
\end{subfigure}
\hfill
\begin{subfigure}[t]{0.48\textwidth}
\centering
\includegraphics[]{triangle_alpha_shapes_flipped}
\caption{\textbf{Acceptable triangle.} The flipped triangle $\Delta_{\point{acd}}$ satisfies the Delaunay property, thus it is included in the Delaunay triangulation.}
\end{subfigure}
\caption{Construction of the Delaunay triangulation in 2D.}
\label{fig:Delaunay}
\end{figure}
\\ \indent Several other algorithms have been developed to construct a Delaunay triangulation, see for example \cite{lee1980two, renka1997algorithm}.
 Given a point set $\mbox{\insieme{U}}$ and a triangulation \insieme{T}, it can be proved that the corresponding Delaunay triangulation $\mbox{\insieme{T}}^\prime$ is unique. Moreover, it maximizes the largest minimum angle among all possible triangulations of a point set $\mbox{\insieme{U}}$ \cite{press2007numerical}.
\\ \indent Alternatively, the Delaunay triangulation can be constructed as the dual of the Voronoi diagram \cite{fortune1992voronoi}. Let $\mbox{\insieme{X}}\subset\mathbb{R}^2$ be a metric space endowed with the Euclidean distance $d(\point{x}, \point{y})$ for $\point{x}, \point{y}\in \mbox{\insieme{X}}$. For \textit{almost}\footnote{Note the importance of the word \textit{almost}. Some points can have the same distance with two or more points of $\mbox{\insieme{U}}$.} every point $\point{x}\in \mathbb{R}^2$, there is a unique point that is the closest point to $\point{x}$. The Voronoi cell of a point $\point{u}_\variabile{i}\in \mbox{\insieme{U}}$ contains all points in $\mathbb{R}^2$ that are closest to $\point{u}_{\variabile{i}}$, see Figure \ref{fig:Voronoi}. The Voronoi diagram of $\mbox{\insieme{U}}\subset \mathbb{R}^2$ is defined as the set of all Voronoi cells \cite{cazals2005conformal}. A more formal definition of the Voronoi diagram is given in the following.
\begin{defn}
Let $\mbox{\insieme{U}}=\{\point{u}_1,\cdots,\point{u}_N\}$ be a set of points in $\mathbb{R}^2$. The Voronoi cell $\mbox{\insieme{V}}_\variabile{i}$ associated to point $\point{u}_\variabile{i}$ is defined as:
\begin{equation}
\mbox{\insieme{V}}_\variabile{i}=\{\point{x}\in \mathbb{R}^2\; | \;|\point{x}-\point{u}_\variabile{i}|<|\point{x}-\point{u}_\variabile{j}| \quad \forall \variabile{j}\neq \variabile{i} \}.
\end{equation}
The Voronoi diagram $\mbox{\insieme{V}}$ is defined as
\begin{equation}
\mbox{\insieme{V}} = \bigcup_{\variabile{i}=1}^N \mbox{\insieme{V}}_\variabile{i}
\end{equation}
 where $\mbox{\insieme{V}}_{\variabile{i}}\cap \mbox{\insieme{V}}_{\variabile{j}}= \emptyset$ for $\variabile{i}\neq\variabile{j}$.
\end{defn}
For the definition of Voronoi diagram in higher dimensions see \cite{brown1979voronoi}.
\begin{figure}[t]
\centering
\includegraphics[width = 0.7 \textwidth]{Delanay_Voronoi1}
\caption{\textbf{Relationship between the Delaunay triangulation and the Voronoi Diagram \cite{Wiki4}.} The black line segments are the boundaries of the Delaunay triangulation, the red line segments constitutes the boundaries of the Voronoi diagram.}
\label{fig:Voronoi}
\end{figure}
\\ \indent The Delaunay triangulation triangulates the convex hull of \insieme{U} and, therefore it does not constitute a suitable method for reconstructing the contour formed by a point cloud. The $\alpha$-shape method was developed to solve such problem \cite{edelsbrunner2010alpha, guo1997surface}. Starting from the Delaunay triangulation $\mbox{\insieme{T}}^\prime$ of a point set \insieme{U}, the corresponding $\alpha$-shape of $V$ is formed by the only triangles of $\mbox{\insieme{T}}^\prime$ that satisfy the so-called "$\alpha$-test" which is briefly explained.
For each triangle, we calculate the circumradius, i.e., the radius of the circumcircle. If this radius is larger than $\alpha$ the triangle is removed from the shape. The choice of the parameter $\alpha$ is highly significant in the $\alpha$-shapes procedure and, it has to be selected such that the desired approximation of the shape formed by the points of $V$ is obtained.\\ \indent
%Therefore, $\alpha$ is closely related to the radius of the circumcircles. A possible strategy is to find the radius of the greater empty circumcircle. Thus $\alpha$ can be selected according to the density $\delta$ of the point sets $V$
%with $C$ a constant and $\delta$:
%\begin{equation}
%\delta=\frac{N}{A}\; ,
%\end{equation}
%where $N$ is the number of points in $V$ and $A$ is the area of the convex hull of $V$.
%%Note that, if we scale the dimensions of the
%The value of $\alpha$ can be chosen, for instance, inversely proportional to $\sqrt{\delta}$:
%\begin{equation}
%\alpha=C\frac{1}{\sqrt{\delta}}\;,
%\end{equation}
%Note that, while $\delta$ is given for a fixed point set $V$, the value of $C$ needs to be determined by numerical simulations.\\ \indent
To summarize, the $\alpha$-shape construction can be outlined as follows:
\begin{enumerate}
\item Construct a Delaunay triangulation\footnote{In the simulations we present in this chapter the Matlab function \textit{Delaunay} is used \cite{matlab_delaunay}.} $\mbox{\insieme{T}}^\prime$ of the point cloud $\mbox{\insieme{U}}$;
\item For every triangle $\mbox{\insieme{T}}^\prime(i)\in \mbox{\insieme{T}}^\prime$ calculate its circumradius $r(i)$;
\item If $r(i)\leq\alpha$ keep the triangle $\mbox{\insieme{T}}^\prime(i)$ in the triangulation;
\item If $r(i)>\alpha$ remove the triangle from the triangulation;
\item Select from the new triangulation obtained those sides that belong to only one triangle, the so-called \textit{free boundary edges}\footnote{In the simulations we present in this chapter the Matlab function \textit{freeBoundary} is used \cite{matlab_free}.}. By definition, the free boundary edges are not a common edge of any two triangles.
\end{enumerate}
%% Insert algorithm
%\begin{algorithm}[h]
%\caption{$\alpha$-shape reconstruction}\label{alg:alphashapes}
%\begin{algorithmic}[1]
%\Procedure{$\alpha$-shape}{$V$, $\alpha$}
%\State Construct a Delaunay triangulation of the point cloud $V$
%\State$T^\prime\gets$ all triangles of the Delaunay triangulation
%\State For every
%\State Calculates the radius of the circle circumscribed by the vertices of every triangle
%\State $r(i) \gets$
%\State For {every triangle in $T^\prime$}
%\State $(\variabile{q}_1^4, \variabile{p}_1^4) \gets \mbox{left and upper corner of source PS: (-\variabile{a}, 1)} $
%\For{$ \variabile{k}= 1 \to 4 $}
%\State Trace the ray with initial coordinates $(\variabile{q}_1^\variabile{k}, \variabile{p}_1^{\variabile{k}})$ in \set{S}{}{};
%\State Calculate the corresponding path $\Pi^{\variabile{k}}$;
%\State $\mbox{Ray}.\variabile{q}\gets [\mbox{Ray}.\variabile{q}, \variabile{q}_1^\variabile{k}]$;
%\State $\mbox{Ray}.\variabile{p}\gets [\mbox{Ray}.\variabile{p}, \variabile{p}_1^\variabile{k}]$;
%\State Store the corresponding path $\Pi^{\variabile{k}}$.
%\State $\mbox{Ray}.\Pi\gets [\mbox{Ray}.\Pi, \Pi^{\variabile{k}}]$;
%\EndFor
%\State VL $\gets [1, 2, 4]$ \Comment{VL = vertices of the left triangle}
%\State VR $\gets [2,3, 4]$   \Comment{VR = vertices of the right triangle}
%\State \Call{Left Triangle}{VL, Ray, $\varepsilon_{\variabile{q}_1}^{\textrm{min}}, \varepsilon_{\variabile{q}_1}^{\textrm{max}}, \varepsilon_{\variabile{p}_1}^{\textrm{min}}, \varepsilon_{\variabile{p}_1}^{\textrm{max}}$}\Comment{Refine the left triangle}
%\State \Call{Right Triangle}{VR, Ray, $\varepsilon_{\variabile{q}_1}^{\textrm{min}}, \varepsilon_{\variabile{q}_1}^{\textrm{max}}, \varepsilon_{\variabile{p}_1}^{\textrm{min}}, \varepsilon_{\variabile{p}_1}^{\textrm{max}}$} \Comment{Refine the right triangle} \\
%\Return ;
%\EndProcedure
%\end{algorithmic}
%\end{algorithm}
%\\
%Let us define a Voronoi diagram in a metric space.
%The simplest case that we can have is the two-dimensional case that is the case where $X=\mathbb{R}^2$.
%The tuple $\mathcal{S}=\{1,\cdots,n\}\subset \mathbb{R}^2$ is now a set of points. The Voronoi diagram of $\mathcal{S}$ is a subsection of $\mathbb{R}^2$ such that every other region around a point $p\in \mathcal{S}$ contains all points that are closer to $p$ than to every point in $\mathcal{S}$. A triangulation of the point set $\mathcal{S}$ is a set of edges $\mathcal{E}$ whose extremes are points of $\mathcal{S}$ such that the faces of each triangle are bounded by three edges and any edge that is not in $\mathcal{E}$ intersects one of the existing edges. The Delaunay triangulation is the dual graph of the Voronoi diagram: it consists of vertices (the points in $\mathcal{S}$) and it has an edge between two vertices if the two corresponding faces share an edge.
$\alpha$-shapes provide a nice mathematical definition of the \textit{shape} of
a set of points. In two dimensions, $\alpha$-shapes gives the contour of the point cloud which is approximated by a family of line segments.
Although they are a powerful tool for determining the shape of a point cloud, there exist shapes that are not described well by classical $ \alpha $-shapes. Indeed, for some point sets there is no value of $\alpha$ that gives a good approximation of the contour formed by the point cloud. Usually the parameter $\alpha$ is determined according to the density of the point cloud, therefore, it can be difficult to obtain a good approximation of a shape formed by a non-uniform point set. Furthermore, the $\alpha$-shape method does not work well when the shape we need to approximate has a sharp turn or a joint, this case will be clarified in Section \ref{sec:results-Tir-alpha} with an example.
%In this case $\alpha$-shapes often give a "webbed-foot" appearance at such joints since they improperly connect the adjacent surfaces.
\\ \indent There are several ways to determine the value of $\alpha$ \cite{mandal1997selection}; in the next section we provide a technique that exploits the conservation of \'{e}tendue in PS.
%The first step of this method is to make a triangulation of the point cloud.
%Then the key idea is to compute somehow the point-density of each point and use this to get an approximation of the point density of a triangle. In this way one can reduce the $\alpha$-value in areas where the triangle's point density (see Equation \ref{delta_t} for the definition) is higher than average in such a way that is possible to obtain a finer level of detail for areas that have an higher density.
%More precisely, each point $ \textbf{p}\in \mathcal{S} $ has a local point density defined as
%\begin{equation}
%\delta (\textbf{p})= \sum_{\textbf{q}\in \mathcal{S}}\Big( 1-\frac{\textrm{d}(q,p)}{\lambda}\Big) \qquad \forall \textbf{q} \mbox{\;\;such that\;\;} \textrm{d}(\textbf{p},\textbf{q})<\lambda\,,
%\end{equation}
%where $ \lambda $ is the constant radius of the local neighborhood and $\textrm{d}(\textbf{x},\textbf{y})$ is the Euclidean distance.
%When local density is larger than the average, that is when
%\begin{equation}
%\delta (\textbf{p}) >\frac{1}{| \mathcal{S} |}\sum_{\textbf{q}\in \mathcal{S}}\delta (\textbf{q})
%\end{equation}
%we know some properties about the region surrounding $\textbf{p}$.
%For instance, if the point set is uniformly distributed then it is possible to find areas with a high-density in the case where there are two closely separated surfaces.  In point sets of non-uniform distribution, high densities are found when the surface presents a joint discontinuity. The algorithm developed by Teichmann and Capps is structured as follow.
%After computing density information for each point they make a triangulation of the point set. Then they calculate the average density  $\delta(t)$ for each triangle $\Delta_{abc}$ defined as:
%\begin{equation}
%\delta(t)=\frac{\delta(a)+\delta(b)+\delta(c)}{3 \mu}\,,
%\label{delta_t}
%\end{equation}
%where $\mu$ is the global average density of the entire point set $\mathcal{S}$.
%If $\delta(t)$ is greater than $1$ the density of the point cloud is higher. Hence is necessary to define another value of $\alpha$:
%\begin{equation}
%\alpha^{\;\prime} = \frac{\alpha}{\delta(t)^\sigma}
%\end{equation} where $\sigma$ is a value that is adjusted by the user.
%If  $\delta$ is less than $1$ the $\alpha$-value is not modified.
%In this way it is possible to have a finer precision on the shape formed by the point set where the density is higher than the average density. Hence it is possible to distinguish two separated objects with different density.
% We want to determine the boundaries in phase space
% Jorg
% It is useful to understand whether the approximation is correct
\section{Determination of $\alpha$ using \'{e}tendue conservation} \label{sec:Tir_alpha}
As mentioned in Section \ref{sec:PSconcept}, in two-dimensions \'{e}tendue can be seen as an area in PS.
Therefore, given an optical system with a source $\point{S} = [-\variabile{a}, \variabile{a}]$, the \'{e}tendue at the source coincides with the area of source PS, and it is given by:
\begin{equation}\label{eq:etenduesource1}
U = 4\n_1 \variabile{a} \sin(\myangle_1^{\textrm{max}})\,,
\end{equation}
 where $\variabile{a}$ is the half length of the source, $\n_1$ the index of refraction of the medium in which the \point{S} is located and $\myangle_1^{\textrm{max}}$ is the maximum value of the angle that the rays make with the normal $\boldsymbol{\nu}_1$ of the source.\\ \indent
For some optical systems, all the rays emitted by the source arrive at the target, for some others there are also rays that can end at other detectors which are located outside the system.
Indicating with \set{R}{$1$}{}$(\Pi)$ the regions in source PS formed by the rays that reach the target following path $\Pi$ and with \set{R}{}{}$(\Pi)$ the corresponding regions at the target, the \'{e}tendue $U_1$ at the source restricted to rays that arrive at the target is given by:
\begin{subequations}
\begin{align}
\label{eq:etendueintegralsource}
U\big(\mbox{\set{R}{$1$}{}}(\Pi)\big) = {\int\!\!\int}_{\textup{R}_1(\Pi)} \textrm{d}\variabile{q}\,\textrm{d}\variabile{p}.\\\label{eq:etenduesumsource}
U_1 = \sum_\Pi{U\big(\mbox{\set{R}{$1$}{}}(\Pi)\big)},
\end{align}
\end{subequations}
where $U\big(\mbox{\set{R}{$1$}{}}(\Pi)\big)$ is the contribution to the \'{e}tendue given by the rays inside
\set{R}{$1$}{}$(\Pi)$ in source PS and the sum is over all possible paths $\Pi$ from the source to the target.
Similarly, the \'{e}tendue at the target of the rays emitted by the source is:
\begin{subequations}
\begin{align}
\label{eq:etendueintegraltarget}
U\big(\mbox{\set{R}{}{}}(\Pi)\big) = {\int\!\!\int}_{\textup{R}(\Pi)} \textrm{d}\variabile{q}\,\textrm{d}\variabile{p}.\\ \label{eq:etenduesumtarget}
U_\textrm{t}= \sum_\Pi{U\big(\mbox{\set{R}{}{}}(\Pi)\big)},
\end{align}
\end{subequations}
Note that, since both the source and the target are located in air ($\n_1=1$), in (\ref{eq:etendueintegralsource}) and (\ref{eq:etendueintegraltarget}), and from now on, we omit writing the index of refraction $\n_1$.
\\ \indent In order to determine the value of $\alpha$ in the $\alpha$-shape procedure that approximates the boundaries $\partial$\set{R}{}{}$(\Pi)$ accurately, we use \'{e}tendue conservation ($U_{\textrm{t}}= U_1$). The $\alpha$-shapes method is applied to every region \set{R}{}{}$(\Pi)$ for a range of values of $\alpha$;
   for each value an approximation of the boundaries $\partial$\set{R}{}{}$(\Pi)$ is obtained and
   the intersection points $\variabile{q}^{\textrm{\,max}}(\Pi,\variabile{p})$ and $\variabile{q}^{\textrm{\,min}}(\Pi,\variabile{p})$ between $\partial$\set{R}{}{}$(\Pi)$
and the horizontal lines $\variabile{p}=\textrm{const}$, with $\variabile{p}\in[-1,1]$, are computed for every path $\Pi$.
Therefore, Equation (\ref{eq:etendueintegraltarget}) becomes:
\begin{equation}\label{eq:etenduetarg}
 U\big(\mbox{\set{R}{}{}}(\Pi)\big)= \int_{-1}^{1}{\Big(\variabile{q}^{\textrm{\,max}}(\Pi,\variabile{p})-\variabile{q}^{\textrm{\,min}}(\Pi,\variabile{p})\Big)} \,\textrm{d}\variabile{p}.
\end{equation} In case more than two intersection points between the line $\variabile{p}=\textrm{const}$ and $\partial$\set{R}{}{}$(\Pi)$ occur, the previous equation needs to be generalized. Suppose that $\variabile{r}$ intersection points $\big(\variabile{q}^{\,\variabile{i}}(\Pi,\variabile{p}), \variabile{p}\big)_{\variabile{i} = 1, \cdots, \variabile{r}}$ are found.
Ordering their $\variabile{q}$-coordinates in ascending order, the target \'{e}tendue is calculated by:
\begin{equation}\label{eq:etenduetarg1}
 U\big(\mbox{\set{R}{}{}}(\Pi)\big) = \sum_{\variabile{i} = 1}^{\variabile{m}}\int_{-1}^{1}
{\Big(\variabile{q}^{\,2\variabile{i}}(\Pi,\variabile{p})}-{\variabile{q}^{\,2\variabile{i}-1} (\Pi, \variabile{p}) \Big)}\,\textrm{d}\variabile{p}\,,
\end{equation}
where $\variabile{m}$ is the integer part of $\variabile{r}/2$.
The integrals in (\ref{eq:etenduetarg}) and (\ref{eq:etenduetarg1}) are calculated discretizing the interval $[-1, 1]$
   into $\nbin=100$ sub-intervals of equal length, the so-called bins, and using the trapezoidal rule.
\\ \indent Matching the \'{e}tendue at the source $U_1$ with the \'{e}tendue at the target $U_{\textrm{t}}$, a unique value $\alpha_{\textrm{c}}$ of $\alpha$ is determined. Implementing the $\alpha$-shapes procedure with $\alpha = \alpha_\textrm{c}$, an approximation of the boundaries $\partial$\set{R}{}{}$(\Pi)$ is found and the intensity at the target can be calculated.\\ \indent If two intersection points between $\variabile{p}=\textrm{const}$ and $\partial$\set{R}{}{}$(\Pi)$ are found the target intensity is calculated using ($\ref{eta2}$). If more than two-intersection points are found we use the generalized equation:
\begin{equation}
I_{\textrm{PS}}(\variabile{p}) = \sum_{\Pi, \variabile{i} }\int_{\variabile{q}^{\,2\variabile{i}-1}(\Pi, \variabile{p})}^{\variabile{q}^{\,2\variabile{i}}( \Pi, \variabile{p})}L(\variabile{q}, \variabile{p})\textrm{d}\variabile{q} =
 \sum_{\Pi, \variabile{i}}\big(\variabile{q}^{\,2\variabile{i}}(\Pi, \variabile{p})-
\variabile{q}^{\,2\variabile{i}-1}( \Pi, \variabile{p})\big)\,,
\label{eq:Ips}
\end{equation}
where $\variabile{q}^{\,2\variabile{i}}( \Pi, \variabile{p})>\variabile{q}^{\,2\variabile{i}-1}( \Pi, \variabile{p})$, the summation over $\Pi$ is for all the paths $\Pi$ for which the intersection $\variabile{p} = \textrm{const}$ and \set{R}{}{}$(\Pi)$ is not empty, and the summation over $\variabile{i}$ is for $\variabile{i} = 1,2, \cdots, \variabile{m}$. The second equation holds as we assume $L(\variabile{q}, \variabile{p})=1$.\\ \indent
To clarify our idea we apply the method to two different optical systems, the results are presented next.
%\\ \indent Let us consider the two-faceted cup introduced in Chapter \ref{chap:raytracing} and depicted in Figure \ref{fig:cup}. The half length of the source is $\variabile{a}=2$, the maximum angle is $\myangle_1^{\textrm{max}} = \pi/2$ and \point{S} is located in air, i.e., $\n_1=1$. Therefore, the total \'{e}tendue is $U=8$. For the two-faceted cup all the rays emitted by \point{S} arrive to \point{T}, hence from \'{e}tendue conservation we obtain that $U_{\textrm{t}} =U_1 = 8$.
%For some optical systems, not all the rays emitted by the source arrive to the target.
% Explain the idea: use etendue conservation
% Do the example for the two faceted cup
% Explain the TIR collimator
\section{Results for a TIR-collimator}\label{sec:results-Tir-alpha}
We apply the $\alpha$-shapes method to the set of points in target PS obtained by using PS ray tracing.
In this chapter, the procedure is applied to two different kinds of total internal reflection (TIR)-collimators.\\ \indent
Let us first describe the TIR-collimator depicted in Figure \ref{fig:tir}. It is an optical system symmetric with respect to the $z$-axis, it consists of a lens (central curve), two broken lines adjacent to the lens,
two curved lines on each side and a top formed by a horizontal segment. The lens (line $2$) and the broken lines, formed by a collection of three segments (lines $3, 4, \mbox{ and } 5$ and $9, 10 \mbox{ and } 11$), are refractive line segments while the curved lines (labeled with $6$ and $8$) are designed in such a way that light is totally internal reflected (which explains the name TIR).
The light source $\point{S}$ (line $1$) and the target $\point{T}$ (line $12$) are two straight line segments normal to the optical axis.
The source $\point{S}= [-2,2]$ is located at a height $\variabile{z}_{1} = 0.3$ from the $\variabile{x}$-axis.
 The target $\point{T}= [-9.7, 9.7]$ is parallel to the source and is located at a height $ \variabile{z}= 8.2$. Both \point{S} and \point{T} are located in air ($\n_1=1$).
The volume inside the collimator is filled with a material with index of refraction $\n_2=1.5$ (e.g. glass).
The collimator is surrounded by two vertical lines (lines $13$ and $15$) and two horizontal lines ($12$ and $14$) that receive the light emitted from the source; among these the one at the top (line $12$) is assumed to be the target, and it is located at a small distance from the top (line $7$).
\begin{figure}[t]
  \begin{center}
  \includegraphics[width=0.7\textwidth]{TIR}
  \end{center}
  \caption{\textbf{Shape of the TIR-collimator.} Each line of the system is labeled with a number.
   The shape of the collimator is shown in blue.
   Three detectors depicted with green lines (surfaces $13$, $14$, and $15$) are located at the left, the right and the bottom of the optical system. The source (line $1$) and the target (line $12$) are depicted in red.}
%The sagitta of the lens is approximately $1.17$.}
  \label{fig:tir}
\end{figure}
\\ \indent
Using PS ray tracing explained in Section \ref{sec:PS_raytracing} with parameters $\varepsilon_{\variabile{q}_1}^{\textrm{max}} = 0.05/4$, $ \varepsilon_{\variabile{p}_1}^\textrm{max} = 0.05/8, $ $\varepsilon_{\variabile{q}_1}^\textrm{min} = 0.8/4$ and $\varepsilon_{\variabile{p}_1}^\textrm{min} = 0.8/8$, around $1.67 \cdot 10^4$ rays are traced (see Table \ref{tab:table}). The rays distribution at the source PS is shown in Figure \ref{fig:sourcePS}, where we depicted the rays that follow the same path with the same color. Seven different paths are found. The yellow rays follow path $\Pi_1 = (1, 2, 7, 12)$; the green rays follow path $\Pi_2 ~= ~(1, 4, 6, 7, 12)$; the red rays follow path $\Pi_3 = (1, 10, 8, 7, 12)$; the magenta rays follow path $\Pi_4= (1, 3, 7, 12)$ and the blue rays follow path $\Pi_5= (1, 11, 7, 12)$. The rays located inside the white areas correspond to rays that do not reach the target, they follow either path $\Pi_6 = (1, 4, 7, 6, 15)$ or path $\Pi_7 = (1,10,7,8,13)$ and they do not give any contribution to the target intensity.
\begin{figure}[t]
\label{fig:sourcePS}
  \begin{center}
  \includegraphics[width=0.8\textwidth]{source1}
  \end{center}
  \caption{\textbf{Distribution of the rays on \point{S}{}{}}. Around $1.67 \cdot 10^4$ rays are traced using the triangulation refinement with parameters:
  $\varepsilon_\variabile{q}^\textrm{max} = 0.8/4 ,$ $ \varepsilon_{\variabile{p}}^\textrm{max} = 0.08/8, $ $\varepsilon_{\variabile{q}}^\textrm{min} = 0.05/4, \varepsilon_\variabile{p}^\textrm{min} = 0.05/8$. Rays that belong to the same region are depicted with the same color. The rays located inside the white areas do not reach the target. The boundaries of the two white regions are approximated by triangles depicted with black lines.}
 \label{fig:sourcePS}
\end{figure}
Note that, given two adjacent paths, the corresponding regions in \set{S}{}{} have usually a common boundary.
Since for this system not all the rays emitted by the source arrive at the target, $U_{\textrm{t}}$ needs to be compared to the \'{e}tendue $U_1$ at the source given by only those rays that reach the target (the rays that follow paths $\Pi_6$ and
$\Pi_7$ are discarded). To this purpose, $U_1$ is calculated by removing from the total area $U$ of \set{S}{}{} those areas occupied by the regions formed by the rays that hit the left or the right detector (white regions in Figure $\ref{fig:sourcePS}$).  For the TIR collimator in Figure \ref{fig:tir}, $U$ is obtained from (\ref{eq:etenduesource1}), the source \'{e}tendue $U_1$ corresponding to the area covered by the rays that arrive at the target can be approximated by:
 \begin{equation}\label{eq:Usource}
 U_{1} = U-2A_{T},
 \end{equation}
 where $U=8$ and $A_{T}$ is the approximated area of the white triangles in Figure \ref{fig:sourcePS} surrounded by the black lines.\\ \indent  Next, $U_{\textrm{t}}$ is calculated several times from (\ref{eq:etenduetarg1}) where every time the boundaries $\partial$\set{R}{}{}$(\Pi)$ are obtained by using $\alpha$-shapes for a different value of $\alpha$.
%An accurate approximation of $\partial$\set{R}{}{}$(\Pi)$ gives a value of $U_{\textrm{t}}$ close to the exact \'{e}tendue.
%Matching $U_1$ with all the approximations of $U_{\textrm{t}}$ we find the best value $\alpha_c$ of $\alpha$ that approximates $\partial$\set{R}{}{}$(\Pi)$ and, therefore, $U_{\textrm{t}}$.
\\ \indent To clarify this concept, in Figure \ref{fig:etendueTS} we provide an example where the source \'{e}tendue $U_1$ and target \'{e}tendue $U_{\textrm{t}}$ are computed from a set of around $1.67\cdot 10^4$ rays. The approximated source \'{e}tendue $U_1\approx 7.77$ is depicted with the red line. The blue line shows how the \'{e}tendue at the target changes as a function of $\alpha$. The smallest difference $\Delta U = |U_1-U_{\textrm{t}}|$ is obtained using $\alpha = \alpha_\textrm{c} = 0.08$.
 \begin{figure}[t]
  \begin{center}
  \includegraphics[width=0.7 \textwidth]{etendue_alpha_shapes}
  \end{center}
  \caption{\textbf{\'{E}tendue for the TIR-collimator.} $U_\textrm{t}$ is computed for a range of values of $\alpha$. $U_1 \approx 7.77$. The green dot indicates the value of $\alpha_\textrm{c} = 0.08$ which gives a good approximation of the boundaries $\partial$\set{R}{}{}$(\Pi)$ at the target.
   Around $1.67 \cdot 10^4$ rays have been traced using PS ray tracing.
  }
  \label{fig:etendueTS}
\end{figure}
In Figure \ref{fig:targetPS} we show the boundaries
$\partial$\set{R}{}{}$(\Pi)$ in target PS obtained tracing $1.67\cdot10^4$ rays and using $\alpha_\textrm{c}=0.08$.
  \begin{figure}[t]
  \begin{center}
  \includegraphics[width=0.7\textwidth]{target_alpha_shapes}
  \end{center}
  \caption{\textbf{Target PS representation.} A set of $1.67 \cdot 10^4$ rays are traced.
  Rays that follow the same path are depicted with the same color. The choice of the colors is consistent with Figure $\ref{fig:sourcePS}$. The boundaries $\partial$\set{R}{}{}$(\Pi)$ are computed through the $\alpha$-shapes method with $\alpha = \alpha_\textrm{c} = 0.08$.}
  \label{fig:targetPS}
\end{figure}
The target intensity $I_{\textrm{PS}}(\variabile{p})$ for $\variabile{p}\in[-1,1]$ is obtained from Equation (\ref{eta2}). \\ \indent
To validate our method we compare the PS intensity with the QMC intensity.
To this purpose a partitioning $P_2:-1=\variabile{p}_{0}<\variabile{p}_1<\cdots<\variabile{p}_{\nbin}=1$ of the interval $[-1,1]$ into $\nbin=100$ bins is considered.
The averaged and normalized PS intensity $\hat{I}_{\textrm{PS}}$ is calculated for every
$\big(\variabile{p}^{\variabile{h}+1/2} = \frac{1}{2}(\variabile{p}^{\variabile{h}+1}+ \variabile{p}^{\variabile{h}})\big)_{\variabile{h}=0, \cdots, \nbin-1}$ dividing the PS averaged intensity by the total \'{e}tendue:
\begin{equation}\label{eq:normalized_PS_intensity}
\hat{I}_{\textrm{PS}}(\variabile{p}^{\variabile{h}+1/2}) = \frac{1}{U_{\textrm{t}}}\int_{\variabile{p}_{\variabile{h}}}^{\variabile{p}_{\variabile{h}+1}} I_{\textrm{PS}}(\variabile{p})\textrm{d}\variabile{p}.
\end{equation}
The averaged and normalized QMC intensity $\big(\hat{I}_{\textrm{QMC}}(\variabile{p}^{\variabile{h}+1/2})\big)_{\variabile{h} = 0, \cdots, \nbin-1}$ is given by
\begin{equation}\label{eq:normalized_MC_intensity}
\hat{I}_{\textrm{QMC}}(\variabile{p}^{\variabile{h}+1/2}) = \frac{\nrays[\variabile{p}^{\variabile{h}},\variabile{p}^{\variabile{h}+1})}{\nrays[-1,1)}
\qquad \mbox{ for } \variabile{p}\in[\variabile{p}^{\variabile{h}}, \variabile{p}^{\variabile{h}+1}).
\end{equation}
Both approximate intensities $\hat{I}_{\textrm{A}} (\textrm{A} = \textrm{PS}, \textrm{QMC})$ are compared to an intensity $\hat{I}_{\textrm{ref}}$ taken as a reference.

For some optical systems, there is an explicit solution for the target intensity, but this is not the case of the TIR-collimator.
Therefore, a QMC simulation with $10^7$ rays is used to obtain the averaged normalized intensity $\hat{I}_{\textrm{ref}}$.
The intensity profile $\hat{I}_{\textrm{PS}}
$ obtained using PS ray tracing with $8.3\cdot 10^4$ rays and $\alpha= \alpha_\textrm{c} = 0.06$ is depicted in Figure \ref{fig:intensityMCPS} with a red line.
$\hat{I}_{\textrm{PS}}$ is hardly distinguishable from $\hat{I}_{\textrm{ref}}$ which is indicated with the dashed and blue line in Figure $\ref{fig:intensityMCPS}$.\\ \indent
  \begin{figure}[h]
    \centering
    \includegraphics[width=0.7\textwidth]{intensity_alpha_shapes}
\caption{\textbf{Target intensity profile.}
The exact intensity is computed using the QMC method for a set of $10^7$ rays. For the PS intensity a set of $8.3\cdot 10^4$
rays is considered and $\alpha_\textrm{c} = 0.06$ is chosen to compute the boundaries $\partial$\set{R}{}{}$(\Pi)$.}
  \label{fig:intensityMCPS}
\end{figure}
Finally, we calculate the error between $\hat{I}_{\textrm{A}}$ and $\hat{I}_{\textrm{ref}}$, defined as:
\begin{equation}\label{eq:error}
\mbox{error} = \frac{\sum_{\variabile{h}= 1}^{\nbin}| \hat{I}_{\textrm{A}}(\variabile{p}^{\variabile{h}+1/2}) - \hat{I}_{\textrm{ref}}(\variabile{p}^{\variabile{h}+1/2})|}{\nbin}.
\end{equation}
The QMC and PS intensities are calculated several times increasing the number of rays to improve the accuracy.
Table \ref{tab:table} and \ref{tab:table2} describe how the number of rays traced affects the error.
In Table \ref{tab:table} the correlation between $\alpha_\textrm{c}$ and the number of rays is evident.
%determined by the values of $\varepsilon^{\textrm{min}}_\variabile{p}$, $\varepsilon^{\textrm{min}}_{\variabile{q}}$,
%$\varepsilon^{\textrm{max}}_{\variabile{p}}$ and $\varepsilon^{\textrm{max}}_{\variabile{q}}$.
Note that increasing the number of rays the value of $\alpha_\textrm{c}$ and the corresponding error decrease.
\begin{table}[htbp] \label{tab:table}
\centering
\caption{\bf Errors of the PS intensity}
\begin{tabular}{lllllll}
 \hline  Number \\ of rays\;  & $\varepsilon^{\textrm{max}}_{\variabile{q}} $  & $\varepsilon^{\textrm{min}}_{\variabile{q}} $   \;     & $\varepsilon^{\textrm{max}}_{\variabile{p}}$\;
  & $\varepsilon^{\textrm{min}}_\variabile{p}$\; & $\alpha_\textrm{c}$  & PS error \\
  \hline
 $3\,339$ & $0.8$  & $0.05$  & $0.8/2$  & $0.05/2$ & $0.14$ & $1.47\cdot10^{-3}$ \\
$7\,567$  & $0.8/2$  & $0.05/2$  & $0.8/4$  & $0.05/4$ & $0.10$ & $3.01\cdot 10^{-4}$  \\
$16\,755$  & $0.8/4$  & $0.05/4$  & $0.8/8$  & $0.05/8$ & $0.08$ & $8.60\cdot 10^{-5}$ \\
 $83\,005$ & $0.8/16$  & $0.05/16$  & $0.8/32$  & $0.05/32$ & $0.06$ & $1.31\cdot 10^{-5}$ \\
 \hline
 \end{tabular}
 \label{tab:table}
 \end{table}
\\ \indent In Table \ref{tab:table2} the numerical results of QMC ray tracing are reported.
Increasing the number of rays traced, the error gradually decreases.
\begin{table}[htbp]
\centering
\caption{\bf Error of the QMC intensity}
\begin{tabular}{ll} \hline   Number of rays\; & QMC error\\
 \hline $10^3$  & $1.65\cdot10^{-3}$ \\
$10^4$  & $3.96\cdot 10^{-4}$  \\
 $10^5$  & $6.36\cdot 10^{-4}$ \\
$10^6$  & $1.02\cdot 10^{-5}$ \\
 \hline
 \end{tabular}
 \label{tab:table2}
 \end{table}
\noindent In Figure $\ref{fig:error}$, the results listed in Table $\ref{tab:table}$ and Table $\ref{tab:table2}$ are shown. The red line depicts the convergence of the PS error and the blue line indicates the QMC error.
\begin{figure}[t]
  \begin{center}
  \includegraphics[width=0.7\textwidth]{error_alpha_shapes_vs_qmc_curved}
  \end{center}
  \caption{\textbf{PS and QMC errors as a function of the number of rays}
  The horizontal dotted line shows that an error equal to $1.31\cdot  10^{-5}$ can be obtained tracing almost $10$ times fewer rays in phase space.}
  \label{fig:error}
\end{figure}
%Note from Figure \ref{fig:error} that the error for the QMC method decreases as $\frac{1}{\sqrt{\nrays}}$, while for the PS simulation the speed of convergence is much higher.\\ \indent
We need to emphasize that the convergence of the error of PS ray tracing for increasing $\nrays$ may change according to the design of the optical system.
This is because the approximation of the boundaries in PS depends on the accuracy of the $\alpha$-shapes method.
The $\alpha$-shapes procedure is unable to properly detect the boundaries of regions with a sharp turn if not enough points are given
\cite{teichmann1998surface}. Indeed, on the one hand a low density requires a large value of $\alpha$ to accept the triangles in a region, on the other hand,
 choosing $\alpha$ too large, the shape of the region could be destroyed. Increasing $\alpha$ more triangles are kept and triangles inside the regions \set{R}{}{}$(\Pi)$ could be taken into account creating holes in \set{R}{}{}$(\Pi)$.
\begin{figure}[t]
\centering
\begin{subfigure}{.48\textwidth}
  \centering
  \includegraphics[width=\textwidth]{boundaries_alpha1}
  \caption{Boundaries approximation obtained using the $\alpha$-shapes method with $\alpha_\textrm{c} = 0.3$ (black lines).}
\end{subfigure}
\hfill
\begin{subfigure}{.48\textwidth}
  \centering
  \includegraphics[width=\textwidth]{boundaries_alpha2}
  \caption{Boundaries approximation obtained using the $\alpha$-shapes method with $\alpha_\textrm{c} = 0.31$ (black lines).}
\end{subfigure}
\caption{\textbf{Approximated boundaries at the target PS.} Tracing $3339$ rays and using $\alpha$-shapes, the boundaries cannot be approximated well.
A small change of the parameter $\alpha$ leads to a completely different approximation of the boundaries.}
\label{fig:Tir1}
\end{figure}
Figure $\ref{fig:Tir1}$ clarifies this concept showing that the region formed by rays that hit the lens is hard to approximate when there is a small number of rays inside the region. Consequently either a region bigger than the area covered by the rays is obtained or some triangles which are not part of the boundaries are included in the triangulation. This results in an inaccurate intensity (either too high or to low). To obtain a good approximation of the boundaries of these kinds of patches more rays have to be traced. The PS error decreases very fast increasing the number of rays (see Table
 \ref{tab:table} and Figure \ref{fig:error}).
 \\\indent To show how the error plot changes according to the regularity of the shape of the regions $\partial$\set{R}{}{}$(\Pi)$, we consider another example of a TIR-collimator.
 Figure $\ref{fig:Tir1}$ shows that the hardest region to approximate is given by those rays that follow path $\Pi_1 ~=~ (1,2,7,12)$.
 We therefore consider a TIR-collimator with a flatter lens and with the target located at a smaller distance to the top (see Figure $\ref{fig:analyticlens}$).
The source $\point{S}= [-2,2]$ (surface number $1$) is located in air at a height $\variabile{z}_1 = 0.3$ from the $x$-axis.
       The target $\point{T}= [-9.7, 9.7]$ (surface $12$) is parallel to the source and is located in air at a height $ \variabile{z}= 7.85$.
       The shape of the collimator is shown as a blue line.
       Three detectors depicted with green lines (surfaces $13$, $14$, and $15$) are located at the left, the right and the bottom of the optical system.
 \\ \indent Tracing around $3\cdot10^3$ rays using PS ray tracing, we obtain the target rays distribution shown in Figure $\ref{fig:Tir2}$.
Compared with the distribution in Figure \ref{fig:targetPS}, we note that the extremities at top and bottom of the region formed by the rays that hit the lens are less pronounced.
Moreover a target located very close to the top makes the shape of that region less stretched along the $\variabile{q}$-axis.
Therefore, it is expected that the $\alpha$-shapes method performs better in this case.
\begin{figure}[t]
  \begin{center}
  \includegraphics[width=0.7\textwidth]{tir_analytic2}
   \end{center}
    \caption{\textbf{Shape of the TIR-collimator.} Each line of the system is labeled with a number.
       $n_1 = 1$ is the refraction index of the medium (air) where the source and the target are located, and
       $n_2 = 1.5 $ the refraction index of the medium (glass) inside the optical system.}
%The sagitta of the lens is equal to $0.6$.}
 \label{fig:analyticlens}
\end{figure}
 \begin{figure}[t]
  \begin{center}
       \includegraphics[width=0.7\textwidth]{boundaries_alpha3}
   \end{center}
        \caption{\textbf{Target phase space for the TIR-collimator depicted in
        Figure \ref{fig:analyticlens}.} The black line depicts the best approximation of $\partial$\set{R}{}{}$(\Pi)$ for $3\cdot 10^3$ rays.
The $\alpha$-shapes method gives an accurate approximation of the boundaries for $\alpha_\textrm{c} = 0.9$.}
  \label{fig:Tir2}
\end{figure}
\\ \indent PS and QMC ray tracing are implemented for the TIR-collimator in Figure \ref{fig:analyticlens}. The approximated intensities $\hat{I}_{\textrm{A}}$ $(\textrm{A} = \textrm{PS}, \textrm{QMC})$ are compared to the reference intensity $\hat{I}_{\textrm{ref}}$ (QMC ray tracing with $10^7$ rays).
PS error is depicted with the red line and, QMC error is depicted with the blue line. \\ \indent
% Lets indicate with $\big(\hat{I}_{\textrm{A}, n}\big)_{n \in \mathbb{N}}$ the sequence of the approximated intensity. Every term of the sequence is calculated by increasing the number of rays, for example $\hat{I}_{\textrm{A}, 1}$ is computed tracing $\nrays = 10$ rays,   $\hat{I}_{\textrm{A}, 2}$ is computed tracing $\nrays = 100$ rays, and so on. The speed or rate of convergence of the approximate intensity $\big(\hat{I}_{\textrm{A}, n}\big)$ describes how quickly the terms of sequence $\big(\hat{I}_{\textrm{A}, n}\big)$ converge to the reference intensity $\hat{I}_{\textrm{ref}}$ by increasing the number of rays. Suppose $m$ is a real number, the sequence $\big(\hat{I}_{\textrm{A}, n}\big)$ converges to $\hat{I}_{\textrm{ref}}$ if
%\begin{equation}
%\lim_{\nrays\rightarrow \infty} \frac{|\hat{I}_{\textrm{A}, n+1}-\hat{I}_{\textrm{ref}}|}{|\hat{I}_{\textrm{A}, n}-\hat{I}_{\textrm{ref}}|^m} = \mu
%\end{equation}
%and $\mu$ is called the rate of convergence and $m$ is the order of convergence. For $m\in[0,1]$ we have the linear convergence, for $m=2$ the quadratic convergence, etc.
%\\ \indent
Numerical results show that, for the TIR-collimator in Figure \ref{fig:analyticlens}, the number of rays needed to achieve an error of the order of $10^{-5}$ is reduced using PS ray tracing compared with QMC ray tracing. Furthermore, for a relatively small number of rays, the $\alpha$-shapes method performs better for a TIR-collimator with a flatter lens.
\begin{figure}[t]
 \begin{center}
   \includegraphics[width=\textwidth]{error_alpha_shapes_vs_qmc_flat}
    \end{center}
     \caption{\textbf{PS and QMC errors.}}
%The red line depicts the error using the $\alpha$-shapes method to compute the boundaries.
%The blue line shows the error between the Monte Carlo intensity and the exact intensity.
%     The dashed black line represents a straight line with slope $-\frac{1}{2}$.
%   The dashed blue line represents a straight line with slope $-1$.}
 \label{fig:error2}
\end{figure}
\section{Conclusion}
The aim of this chapter was to detect the boundaries of the regions formed by the rays traced using $\alpha$-shapes.\\
\indent First, we reported some theory about $\alpha$-shapes which are commonly used to approximate the shape formed by a point cloud.
These methods depend on a parameter $\alpha$ that, in most cases, can be determined only by simulations.
\\ \indent Using \'{e}tendue conservation, we developed a new approach to detect the value of $\alpha$ that better approximates the boundaries in target PS.
We applied $\alpha$-shapes to two different kinds of TIR-collimators. The target PS intensity was computed for both systems several times increasing every time the number of rays traced. Finally, the corresponding errors between the approximated intensities and a reference intensity was calculated. We observed that PS ray tracing allows tracing far less rays compared to QMC ray tracing. Numerical results show that using PS ray tracing the desired accuracy can be achieved reducing significantly the number of rays traced.\\ \indent
However, we observed that the error convergence for PS ray tracing strongly depends on the design of the optical system (shapes of the region in target PS). Indeed, the accuracy of the intensity is related to the precision of the $\alpha$-shape, that is, to the choice of the parameter value of $\alpha$. For more complicated shapes in PS, more rays need to be traced for a good boundaries reconstruction.\\ \indent
In order to remove the dependence of PS ray tracing on the parameter $\alpha$, we will construct another procedure to detect the boundaries of the regions in target PS.
The new technique is based on the triangulation refinement explained in Section \ref{sec:PS_raytracing}. The details are explained in the next chapter and numerical results are reported for several optical systems.


\documentclass[12pt]{article}

\title{Math Foundations}
\author{Abel Doñate}
\date{}

\usepackage{amsmath}
\usepackage{hyperref}
\usepackage{graphicx}
\graphicspath{ {./images/} }
\usepackage{wrapfig}

\setcounter{page}{0}

%Geometry
\usepackage{geometry}
\geometry{a4paper, margin=1in}

\begin{document}

\maketitle
\tableofcontents
\newpage

\section{Useful techniques to solve problems}
    \subsection{The Well ordered principle and Descent to infinity}
    The well ordering principle tells us that we cannot have a sequence of non-negative integers such that $a_1>a_2>...$.
    This is useful to apply in descent to infinite proofs. \\ \\
    Suppose you have a problem in which you are given a condition and you have to check what numbers satisfy the condition. In this case you can apply descent to infinite:
    \begin{itemize}
        \item[1)] Find a condition that guarantees you that a set of numbers satisfy the condition if and only if another set of numbers that are strictly less then the original set satisfy the condition.
        \item[2)] By the Well ordering principle, reason that it is impossible to keep descending infinitely many times , so the sequence must stop somewhere.
        \item[3)] If we can apply it many times, that means that there is no set of numbers that satisfy the condition (or only does the trivial ones like $(0,0,0)$).
    \end{itemize}
    A good example is to find the triples $(a, b, c)$ that satisfy $a^3 + 2b^3 = 4c^3$.\\
    Because the right hand side (RHS) is even, then $a$ must be even $\implies a=2x$. \\
    Dividing by $2$ the equation is $4x^3 + b^3 = 2c^3$ \\
    Because RHS is even, then $b$ must be even $\implies b=2y$. \\
    Dividing by $2$ the equation is $2x^3 + 4y^3 = c^3$ \\
    Because LHS is even, then $c$ must be even $\implies c=2z$. \\
    Dividing by $2$ the equation is $x^3 + 2y^3 = 4z^3$ \\

    We observe that we are left with the original case. That tells us that $(a, b, c)$ only holds if $(a/2, b/2, c/2)$ also holds. Applying descent to infinity, we assure that there is no solution except the trivial one $(0,0,0)$

    \subsection{Mathematical induction}
    We can prove lots of types of problem by this simple principle. \\

    Suppose we have to prove that a given condition holds for every natural number. Then one way to prove it is to check if holds for $k=1$, and then prove that if it holds for $k$, then, it must hold for $k+1$. \\

    The sketch of the steps is the following:
    \begin{itemize}
        \item[1)] \textbf{Base case.} First we prove that the condition holds for $k=1$ (or the first element of the set you are given).
        \item[2)] \textbf{Induction statement.} Now we prove that if $k$ holds, then $k+1$ also holds.
        \item[3)] \textbf{Proof.} Finally you reason that the problem has been solved by the principle of mathematical induction
    \end{itemize}

    \subsection{Double counting}
    Sometimes it is useful to exploit different perspectives of a problem. If your task is to prove an equivalence, perhaps the easiest way to do it is double counting, or compute the expression in two different ways, but showing that both are equal.

\section{Inequalities}
    \subsection{Means inequalities}
    We have the chain of inequalities $HM\leq GM\leq AM\leq QM$:
    \[\frac{1}{\frac{1}{a}+\frac{1}{b}+\frac{1}{c}}\leq \sqrt[3]{abc}\leq \frac{a+b+c}{3}\leq \sqrt{\frac{a^2+b^2+c^2}{3}} \]
    The equality holds when $a=b=c$.

    \subsection{Cauchy's inequality}
    \[\vec{a}\cdot \vec{b}\leq |\vec{a}||\vec{b}| \implies \left(\sum{a_ib_i}\right)^2 \leq \left(\sum{a_i^2}\right)\left(\sum{b_i^2}\right) \]

    \subsection{Chebyshev's inequality}
    If we have two ordered vectors (both increasing or decreasing):
    \[n\sum{a_ib_i}\geq \left(\sum{a_i}\right)\left(\sum{b_i}\right)\geq n\left(\sum{a_ib_{n+1-i}}\right)\]

    \subsection{Jensen's inequality}
    Let $f$ be a convex function (i.e. $f''\geq 0$). Then:
    \[f\left(\sum{\alpha_ix_i}\right) \leq \sum{\alpha_i f(x_i)} \ \ \ \ \ where \ \ \sum{\alpha_i}=1\]

\section{Combinatorial numbers}
    The number of ways to choose k elements in a set of n elements is \textit{n choose k}:
    \[\binom{x}{k}=\frac{n!}{k!(n-k)!}\]
    Some of the properties of combinatorial numbers are:
    \begin{align}
        (x+y)^n &= \sum{\binom{n}{k}x^ny^{n-k}} \\
        \binom{n+m}{k} &= \sum{\binom{n}{i}\binom{m}{k-i}} \ \ \ \text{(Vandermonde's identity)}
    \end{align}

    \begin{wrapfigure}{r}{0.23\textwidth}
        \centering
        \includegraphics[width=0.23\textwidth]{pascal}
    \end{wrapfigure}

    These numbers appear also in Pascal's triangle. The ith element of the nth row (begining both with 0) is $\binom{n}{i}$.\\ \\
    An extension of \textit{n choose k} is the following:
    \[\binom{n}{k_1 \cdots k_m}=\frac{n!}{k_1!\cdots k_m!}\]
    and it appears in the multinomial theorem:
    \[(x_1+\cdots +x_m)^n=\sum_{k_1+ \cdots +k_m=n}{\binom{n}{k_1 \cdots k_m}x_1^{k_1}\cdots x_m^{k_m}}\]

\section{Sets}
    \subsection{Operations and De Morgan's Law}
    Main operations
    \begin{itemize}
    	\item Union. $A \cup B$
    	\item Intersection $A \cap B$
    	\item Difference $A \setminus B$ or $A-B$
    	\item Symmetric difference $A \bigtriangleup B = (A\cup B)-(A\cap B)$
    	\item Complement. $\bar{A}$
    \end{itemize}
    \textbf{De Morgan's Law:}
    \begin{itemize}
    	\item $\overline{A \cup B}=\bar{A}\cap \bar{B}$
    	\item $\overline{A\cap B}=\bar{A}\cup \bar{B}$
    \end{itemize}

	\subsection{Canonical decomposition}
	Every function $f$ can be decomposed in a surjective, bijective and injective functions. 		For instance: $f=i \circ b \circ \pi$.
	\begin{itemize}
		\item $\pi$ maps to its equivalence class
		\item $b$ maps to the function of every equivalence class
		\item $i$ maps to itself
    \end{itemize}
    \section{Algebraic structures}
	\subsection{Group}
	A group is a set with one binary operation $(S,\star)$ that must satisfy:
	\begin{itemize}
		\item Closure under $\star$
		\item Associative $(a\star b)\star c=a\star (b\star c)$
		\item Identity element $e$ such that $e\star a=a\star e=a$
		\item Inverse element. Every $a$ has an inverse $a^{-1}$
	\end{itemize}

	If the group is commutative $a\star b= b\star a$, then is called \textbf{Abelian}. \\

	Examples of abelian group are $\mathbf{Z}_n$ (group whose elements are $0, 1, \cdots, n-1$, and the operation is the sum modulo n), or the group or rotations of a regular polygon. \\

	An example of non-abelian group is the group of rotations of a cube. The proof is left to the reader.

	\subsection{Ring}
	A Ring is a set with two binary operations $(S,\star, \top)$ that must satisfy:
	\begin{itemize}
		\item $(S, \star)$ is an abelian group
		\item Distributive $a\top (b\star c)= (a\top b) \star (a\top c)$
		\item Identity elements $e_\star, e_\top$
	\end{itemize}

	If $e_\star \neq e_\top$ and $a\top b\neq e_\star$, then is called an \textbf{Integral domain}.\\

	One example of a Ring is $\mathbf{Z}$ with usual sum and product.\\

	$\mathbf{Z}_p$ with $p$ prime is an example of an integral domain. $\mathbf{Z}_6$, because 6 is not prime, is not an integral domain, as $2\times 3=0=e_+$.

	\subsection{Field}
	A field is a ring with no inverse for $e_\star$ under $\top$.\\

	The most common example is $\mathbf{R}$. Observe that the element $0=e_+$ has no inverse under multiplication.

\section{Stereographic projection}
    \begin{wrapfigure}{r}{0.30\textwidth}
        \centering
        \includegraphics[width=0.30\textwidth]{stereo1}
    \end{wrapfigure}

    The stereographic projection maps every point in a sphere except the upper pole to the $XY$ plane. On the other hand we can take the inverse stereographic projection, that maps every point in the plane to the sphere (except the upper pole). \\

    This map is achieved by drawing a line from the upper pole to the point in the sphere and taking the intersection with the $XY$ plane. \\

    If we define the coordinates of the plane as $x, y$ as usual and the coordinates of the sphere as $\alpha, \beta, \gamma$, we can derive the following equations:
    \[F(\alpha, \beta, \gamma)=\left(\frac{\alpha}{1-\gamma}, \frac{\beta}{1-\gamma}\right) \ \ \ \
    F^{-1}(x, y)=\left(\frac{2x}{x^2+y^2+1}, \frac{2y}{x^2+y^2+1}, \frac{x^2+y^2-1}{x^2+y^2+1}\right)\]

\end{document}


\section{Background}

\subsection{The original multi-head attention
mechanism}

The scaled dot-product attention proposed by
\citet{vaswani2017attention} transforms the vectorial embedding $\vec{Y}_i$ of a token as a
 function of a sequence of other embedding $\vec{X}_j$. Where
 $\vec{Y}_i$ and $\vec{X}_j$ are all vectors of size $D$. A key
$\vec{K}_j$ and value $\vec{V}_j$ are attributed to each vector
$\vec{X}_j$, and query $\vec{Q}_i$ is attributed to $\vec{Y}_i$.
 The vectors are obtained by linear projection from dimension $D$ to $d$ using three matrices of learnable parameters. The new vector
 $\vec{y}_i'$ is a weighted sum of the $\vec{V}_j$. The weights are
 scores of matching between the query $\vec{Q}_i$ and the keys
$\vec{K}_j$, calculated as the dot product between the two vectors.
They are also \emph{softmaxed} to sum up to 1. The transformation of $L_Q$
vectors $\vec{Y}_i$ as a function of $L_K$ vectors $\vec{X}_j$ can
 be efficiently computed with matrix multiplications:

\begin{equation}
A = \mathrm{softmax}\left(\frac{Q \times K^T}{\sqrt{d}}\right) \times V
\end{equation}

With $Q$ a matrix of shape $(L_Q, d)$, $K$ a matrix of shape
 $(L_K, d)$ and $V$ a matrix of shape $(L_K, d)$. The $\sqrt{d}$
at the denominator is a scaling factor used to avoid saturation in the
exponential terms of the softmax function.

The multi-head attention performs $h$ different projections into spaces
 of dimension $d = D/h$. The resulting vector $\vec{Y}_i'$ is the
concatenation of the $h$ vectors $\vec{y}_i'$ obtained. Thus the
embedding dimension is preserved. Using multiple heads was found
beneficial by the authors over using a single head of dimension
$d = D$.

During training, the cross entropy of the $n^{th}$ predicted token is
 calculated assuming all previous tokens have been generated correctly.
 This enables to parallelize training completely without need for
recurrence. However as the $n^{th}$ token should not depend of the
following tokens, the cells in the upper right corner of the score
matrix are set to $-\infty$ such that after the softmax they are equal
to 0, and the rows still sums up to 1.

\subsection{Improving Transformer scalability with sequence
length}

The original attention mechanism requires the computation of a score
matrix $Q \times K^T$ of shape $(L_Q, L_K)$, with complexity
$O(L_QdL_K)$. If the query and key sequence lengths are multiplied by
two, then the memory used and computation time are multiplied by 4. To
improve the scalability of the transformer with sequence length, several axis of research have been explored.

\citet{kitaev2020reformer} proposed
the Reformer's architecture, which uses an hash-bucketting algorithm to
reduce the complexity of the original multi head attention operation
from $O(L^2)$ to $O(L\log(L))$.

\citet{dai2019transformerxl} proposed the
Transformer-XL's architecture, which cuts the sequence in segments of
length L. The model predicts each stage of the current segment as a
function of the previous and current segment. All the segments are
computed sequentially with a recurrence mechanism. The complexity is linear
with sequence length, but the computation cannot be completely
 parallelized due to the recurrence mechanism, although more than a RNN,
as segments can be computed in one go.

Other publications explored using a sparse attention matrix, such as the
 Longformer by \citet{beltagy2020longformer} and the Big Bird model by
 \citet{zaheer2021big}. As each
token attends to a fixed number of all other tokens, the scalability is
improved. These sparse attention models however require custom
operations implemented in CUDA.

Some other works propose to modify the attention mechanism to be
 compatible with linear complexity. The Linformer by
\citet{wang2020linformer} projects the
 key and values onto a smaller sequence length dimension with matrix
 multiplication. It cannot however generalize to sequences longer than
 during training, as the weights of the projection for such tokens would
 be undefined.

\citet{shen2020efficient} proposed to
 replace the softmax attention score.
 $A = \mathrm{softmax}\left(\frac{Q \times K^T}{\sqrt{d}}\right) \times V$ is
changed into $A = \rho(Q) \times \rho(K)^T \times V$. With $\rho$
the softmax function along the embedding dimension. Thanks to matrix
multiplication commutativity, the order of the operations can be chosen.
If $Q$, $K$ and $V$ are of shape $(L_Q, d)$, $(L_K, d)$ and
$(L_K, d)$ respectively, the complexity of
$(\phi(Q) \times \phi(K)^T) \times V$ is $O(L_Q \times d \times L_K)$ whereas the complexity of
$\phi(Q) \times (\phi(K)^T \times V)$ is
$O\left(max(L_Q, L_k) \times d^2 \right)$. The right-side-first
operation is linear in complexity with sequence length. The shape of the
intermediate result matrix is also changed, allowing to scale the better
in memory requirements as well. The original \emph{softmaxed} attention score
matrix was giving rows of positive scores that sum to 1. With this
change the elements of the score matrix remain positive as $\phi(Q)$
and $\phi(K)^T$ are matrices of positive values, but the rows of the
score matrix does not sum up to 1. This work also does not give a linear complexity formulation for masked attention. If the right-side-first
scheme is adopted, the attention score matrix
$\phi(Q) \times \phi(K)^T$ is never explicitly computed, and can't be
masked.

Building on this idea of commutative attention function proposed by
\citep{shen2020efficient}, \citet{katharopoulos2020transformers} introduced their kernerlized attention function as:

\begin{equation}
A = \frac{\phi(Q) \times \phi(K)^T}{\sum_j \left( \phi(Q) \times \phi(K)^T \right)} \times V
\end{equation}

The function $\phi$ is applied element-wise and can be any positive
function, for example $\phi(x) = elu(x) + 1$. This attention is
row-wise normalized so that all rows of the score matrix are sets of
positive weights adding up to one. This preserves the objective of the
original softmaxed attention scores, while allowing to perform
operations in an optimal order.

The Performer by \citet{choromanski2021rethinking} exploits the same idea of a kernelized attention introduced
by \citet{katharopoulos2020transformers}, with an algorithm that betters approximate softmaxed attention. Most
importantly they also give in annex a prefix sum algorithm to perform
operations in the right-side-first order while giving the same result as
masked left-side-first operation.

Although the author did not specify how to implement it, the only
 implementations we found of this operation requires custom CUDA code. In
 this work we will give an implementation of the right-side-first masked
operation, with usual functions from neural network frameworks, that
remains linear in complexity.

\subsection{Alternatives to absolute positional
encoding}

The original multi-head attention operation introduced by
\citet{vaswani2017attention} was
intrinsically invariant by token order permutation. As token position was
an important information for sequence to sequence models, they encoded
the global position of each token in their embedding. Since then, some
modified attention mechanisms, that depend on relative tokens position,
have been proposed.

\citet{shaw2018selfattention} explored
modifying the attention mechanism so that it depends on the relative
distance between tokens. A second score matrix that is function of the
query and the query/key relative distance is added to the original score
matrix that only depends on query/key vector representation. $A = softmax\left(\frac{Q \times K^T}{\sqrt{d}}\right) \times V$ becomes
$A = \left(\frac{Q \times K^T + S_{rel}}{\sqrt{d}}\right) \times V$ with $S_{rel}$ of shape $(L_Q, L_K)$ defined as
${S_{rel}}_{ij} = \vec{Q_i} \cdotp \vec{RP}_{clip(i-j, -k, k)}$. Where $k$ is the attention horizon length and $\vec{RP}_n$ is one of
$2k+1$ relative positional embedding, vectors of size $d$.
\citet{shaw2018selfattention} and
\citet{huang2018music} observed that introducing this attention scheme improved performances.
The naive calculation of this term however has a complexity of
$O(L_QL_Kd)$. No algorithm was provided to linearize the complexity.

More recently \citet{liutkus2021relative} gives a stochastic positional encoding that is linear in
complexity with regards to sequence length. However the implementation
is complex and its stochastic nature requires that the operations be
repeated several times in parallel.

\citet{horn2021translational} noted that
the term $S^{rel} \times V$ can be computed with linear complexity for
the case where $RP_{-k} = RP_{k}$. However this is restraining as the
model can't make the difference between tokens before the attention
horizon or after.

In this work we will show that the computation of $S^{rel} \times V$
can also be done with linear complexity, without concession.

\endinput


%% This is a simple example of various ScriptTex features

% A single-line comment starts with % and extends until the end of the line

% ScriptTex "understands" a language largely  inspired by tex/latex
% however only a small subset of the tex/latex functionalities
% are supported

%{
A multiline comment start with %{  and ends  with the next }%.
And multiline comments can be nested of course.
}%

\document{article}   % also  report, book  and maybe others later (class system ?)

\title{Simple recursive functions in Python\\
An example of some ScripTex features}

\author{Frédéric Peschanski}  %% remark : ScriptTex only supports UTF-8 encodings

\date{\today}

= Introduction =
%{ the same as :
\section{Basic functions}
in article mode
}%

We describe a few simple _recursive_ functions in the [Python|http://www.python.org] language. % _word_ translates to \emph{word}
% a wiki-like syntax for hyperlinks is supported

We see three *classical* functions : %% *word* translates to \strong{word}  and then \textbf{word} by default
  - the factorial function %% at least two spaces leading the '-' symbol
  - the fibonacci function
  - the ackermann function
%{ this translates to
\begin{itemize}
\item the factorial function
\item the fibonacci function
\item the ackermann function
\end{itemize}
%}

This allows to show the following features.
  1. the factorial highlights:
    a. a simple recursive scheme  % at least 2 chars more
    b. a tail-call variant with a single accumulator
    c. an imperative variant
      i) using a while loop  % this is the final nesting, one must use the latex syntax for deeper nesting
      ii) using a range iterator
  2. the fibonacci highlights:
    - a more complex recursive scheme
    - the use of two accumulators for the tail-call version %  an embedded itemize
%{ this translates to
\begin{enumerate}
\item the factorial hightlights
\begin{enumerate}
\item a simple recursive scheme

... etc.

\end{enumerate}
\end{enumerate}
%}

== Factorial ==
%{
The same as :
\subsection{Factorial}
in article mode
}%
The factorial function is often denoted $n! = 1 \times \ldots \times n$
% ScriptTex is interfaced with MathJax for HTML/CSS/JS output of embedded maths
% it relies on pdflatex or luatex for the pdf output

A more constructive version is:

\[
\left \{ \begin[t]{array}{l}
0!=1 \\
n! = n \times (n-1)! \text{ for } n>0
\end{array} \right.
\] % both display math and inline math are supported

This can be almost directly translated to python code :

\begin{code}[language=python] % a code block, support for various programming languages is planned
def fact(n):
  if n = 0:
    return 1
  else:
    return n * fact(n-1)
\end{code}

The name of the function is `fact` and its definition is quite simple. % the `word` sequence is translated to
%  \verbUwordU in Latex mode, where the U symbol is choosen fresh.  Not that line feeds are disallowed.

% the langage by default can be set
\set[defaultcodelanguage=python]

An tail-call version is as follows\footnote{The python interpreter does not eliminate tail calls, but the notion remains interesting anyway.}:

% default language is python now
\begin{code}[coderef=factit]
def fact(n):
  def factit(n,acc):
    if n = 0:
      return acc
   else:
      return factit(n-1,n*acc)
  # body of the function
  return factit(n,1)
\end{code}

\begin{evalcode} % a block of code can be evaluated and its (by default, standard) output printed to ScriptTex
# insertion of code blocks
\coderef[factit]
print("fact(4)={0}".format(fact(4))')
\end{evalcode} % the result of an evalcode must be executable in isolation


\mychapter{6}{Lesson 6} %181012

\section{Computationally secure encryption}

Having a better idea of what can and can't be accomplished in the cryptographic world, by means of theorems and proofs, we can focus now on the goal of defining a cryptographic system that meets our requirements. In this lesson, we focus specifically on the secrecy-oriented schemes, thus dealing with encryption and decryption of messages.

The requirements of a ``good'' encryption scheme are collectively called those of \emph{computationally secure encryption}: the characterizing requirement is to design a task, or routine, that is \emph{computationally hard} for an attacker to revert.
In detail: this task usually involves a secret key\footnotemark, and is accomplished in polynomial time, and any attacker who wishes to revert it has no efficient means of doing it without knowing such key. Other properties include:

\footnotetext{This is the case for symmetric-key schemes, though many other kinds exist: some involving ``public'' keys, some others not having any key at all}

\begin{enumerate}
    \item \label{prop:owk} \emph{one-way}ness with respect to the encryption key: given $c = \Enc(k, m)$, it should be hard to recover $k$
    \item \label{prop:owm} \emph{one-way}ness with respect to the original message: given $c = \Enc(k, m)$, it should be hard to recover $m$
    \item \label{prop:nol} In a stricter sense: no information whatsoever must ``leak'' from the message
\end{enumerate}

To start visualizing these concepts, let $\Pi = (\Enc, \Dec)$ be a secrecy scheme, and consider the game depicted in figure \ref{cryptogame:otindist}  where the adversary ``wins'' the game when the challenger outputs 1.

\begin{cryptogame}
    {otindist}
    {$\cryptog{ind}(\lambda, b)$}
    {ind}

    \send{}{$m_0, m_1 \in \M$}{}

    \receive{\shortstack[l]{
        $k \pickUAR \K$ \\
        $b \pickUAR \binary$ \\
        $c \pickUAR \Enc(k, m_b)$ }}
    {$c$}{}

    \cseqdelay

    \send{}{$b'$}{\textsc{Output 1 iff} $b = b'$}

\end{cryptogame}

\begin{definition}
    The scheme $\Pi$ is said to be \emph{computationally one-time secure} iff:
    \[
        \forall \adversary \in \ppt \implies \cryptog{ind}(\lambda, 0) \compindist \cryptog{ind}(\lambda, 1)\footnotemark
    \]
    or, rephrased in probability terms:
    \[
        \forall \adversary \in \ppt \implies \lvert\Pr[\cryptog{ind}(\lambda, 0) = 1] - \Pr[\cryptog{ind}(\lambda, 1) = 1]\rvert \in \negl(\lambda) \qedhere
    \]
\end{definition}
\footnotetext{$\cryptog{ind}$ refers to the indistinguishability of the messages sent by \adversary{} during the game}

This last definition shows how such a scheme is compliant with the three properties exposed beforehand. In particular:

\begin{enumerate}
    \item \emph{It is hard to recover the key}. If not, then an adversary \adversary{} can efficiently recover the key and use it to decrypt the ciphertext, which in turn enables him to perfectly distinguish $m_{0}$ from $m_{1}$ on any instance;
    \item \emph{It is hard to recover the message}. This is analogous, and even more obvious than the preceding point. Nevertheless, this is a necessary condition for a secrecy scheme to be ``good'', and it mustn't be forgotten;
    \item \emph{No information about the message whatsoever may leak from the ciphertext}. This may seem subtler than the previous point, but it warrants caution. Observe how an adversary \adversary{}, if it has the ability to extract even a tiny bit of information of the original message from the ciphertext, then it is actually able to make an educated guess on which message was encrypted in the first place, putting him at an advantage. This leads the probabilities described in the definition to be sensibly more unbalanced than negligible, forfeiting the desired secrecy.
\end{enumerate}

By extension, we may ask ourselves what scheme may or may not be \emph{computationally two-time secure}. For instance, let $\Pi_{\oplus} = (\Enc, \Dec)$ be a secrecy scheme using a \prg{} $G : \binary^\lambda \to \binary^n$, structured as follows:

\begin{itemize}
    \item $\K = \binary^\lambda$, $\M = \C= \binary^n$
    \item $\Enc(k, m) = G(k) \oplus m$
    \item $\Dec(k, c) = c \oplus G(k) = m$
\end{itemize}

To be two-time secure means that, even if an adversary \adversary{} gets hold of a valid plaintext-ciphertext couple $(\overline{m}, \overline{c})$, he is unable to decrypt any future ciphertexts\footnotemark, apart from the obvious $\overline{c}$. However, observe that \adversary{} is now able to extract valuable information for decrypting future ciphertexts:

\footnotetext{This example models a technique called \emph{Chosen Plaintext Attack}, which will be discussed in depth later}

\[
    \overline{c} = \Enc(k, \overline{m}) = G(k) \oplus \overline{m} \implies \overline{c} \oplus \overline{m} = G(k)
\]
so now, for any second ciphertext \adversary{} receives, he can \emph{mimic} the decryption routine, and efficiently uncover the underlying plaintext. This proves that $\Pi_\oplus$ is not two time-secure; nevertheless, it is still one-time secure:

\begin{theorem}
    If $G$ is a \prg, then $\Pi_\oplus$ is computationally one-time secure
\end{theorem}

\begin{proof}
    This proof is another example that showcases the use of hybrid games. Recalling the one-time security definition, we need to show that:
    \[
        \forall \adversary \in \ppt \implies \cryptog{ind}[\Pi_\oplus](\lambda, 0) \compindist \cryptog{ind}[\Pi_\oplus](\lambda, 1)
    \]
    Consider the hybrid game in figure \ref{cryptogame:xorothybrid}, where the original encryption routine is changed to use a completely random value, instead of using $G(k)$\footnotemark. As an exercise, compare it with the original one-time secure definition in figure \ref{cryptogame:otindist}, to check that it perfectly matches.

    \footnotetext{The observant student may recognize that this modification yields exactly the ``one-time pad'' secrecy scheme discussed in lesson 1}

    \begin{cryptogame}
        {xorothybrid}
        {$\hybridg{}[\Pi_\oplus](\lambda, b)$}
        {r-ind}

        \send{$m_0 \neq m_1 \in \binary^l$}{$m_0, m_1$}{}

        \receive{\shortstack[l]{
            $r \pickUAR \binary^l$ \\   % R \sim \unifdist{\binary^l}
            $b \pickUAR \binary$ \\     % B \sim \unifdist{\binary}
            $c = r \oplus m_b$            % C = R \oplus m_B
        }}
        {$c$}{}

        \cseqdelay

        \send{}{$b'$}{\textsc{Output 1 iff} $b' = b$}

    \end{cryptogame}

    The proof begins by affirming that:
    \begin{claim}
        \[
            \forall \adversary \in \ppt \implies \hybridg{}[\Pi_\oplus](\lambda, 0) \equiv \hybridg{}[\Pi_\oplus](\lambda, 1)
        \]
    \end{claim}

    To prove it, notice that $r$ is chosen uniformly at random, and independently of $b$. Thus, no matter how the messages $m_0$ and $m_1$ are structured, $r$ will effectively make the chosen message completely unrecognizable. In formal terms, let $B$ and $R$ be the random variables for \challenger{}'s picks of $b$ and $r$ respectively; then:
    \begin{align*} % AP190906: Totally original, need a peer review here
        & \left| \Pr(B = 0 \knowing C = c) - \Pr(B = 1 \knowing C \evaluatesto c) \right| & \\
        =& \left| \Pr(B = 0 \knowing R \oplus m_B \evaluatesto c) - \Pr(B = 1 \knowing R \oplus m_B \evaluatesto c) \right| & \text{(C definition)} \\
        =& \mathrlap{\frac{\left| \Pr(R \oplus m_B = c \knowing B \evaluatesto 0) \Pr(B = 0) - \Pr(R \oplus m_B = c \knowing B \evaluatesto 1) \Pr(B = 1) \right|}{\Pr(R \oplus m_B = c)}} & \\
        & & \text{(Bayes' theorem)} \\
        =& \frac{\left| \Pr(R \oplus m_0 = c) \Pr(B = 0) - \Pr(R \oplus m_1 = c) \Pr(B = 1) \right|}{\Pr(R \oplus m_B = c)} & \text{(Cond. collapse)} \\
        =& \frac{\left| \oneover{2^l} \half - \oneover{2^l} \half \right|}{\Pr(R \oplus m_B = c)} = 0 & \\
    \end{align*}

    Having proven that \adversary's success is equivalent to straight guessing in $\hybridg{}[\Pi_\oplus]$, we now relate the hybrid game to the original one, affirming that:

    \begin{claim}
        \[
            \forall \adversary \in \ppt,\, \forall b \in \binary \implies \hybridg{}[\Pi_\oplus](\lambda, b) \compindist \cryptog{ind}[\Pi_\oplus](\lambda, b) \qedhere
        \]
    \end{claim}

    The proof proceeds by reduction as depicted in figure \ref{cryptoredux:xorotprg}, by assuming the existence of a distinguisher $\textsf{D}^{\textsc{ind}}$ for $c = G(k) \oplus m_{b}$ and $c = r \oplus m_{b}$, and using it to break $G$'s pseudo-random generation property.

    \begin{cryptoredux}
        {xorotprg}
        {Reducing to breaking a \prg}
        {prg}
        {G-ind}[2]

        \receive{\shortstack[l]{
            $k \pickUAR \binary^\lambda$ \\
            $x_0 \pickUAR G(k)$ \\
            $x_1 \pickUAR \binary^l$ \\
            $b \pickUAR \binary$
        }}{$x_b$}{}

        \return{}{$m_0, m_1$}{}

        \invoke{\shortstack[l]{
            $\beta \pickUAR \binary$ \\
            $c = x_b \oplus m_\beta$
        }}{$c$}{}

        \return{}{$\beta'$}{}

        \cseqdelay
        \send{$b' = \begin{cases}
            0 &\textsc{iff } \beta' = \beta \\
            1 &\textsc{else}
        \end{cases}$
        }{$b'$}{\textsc{Output 1 iff} $b' = b$}

    \end{cryptoredux}

    Again, notice how the games of indistinguishability and pseudo-random generation are reliably reproduced on their respective sides. This construct's value centers on how $\distinguisher^\textsc{ind}$ will perform in its own challenge:
    \begin{itemize}
        \item if $x_b$ is a random value, then $\distinguisher^\textsc{ind}$ will perform as depicted in the hybrid game, thus giving right or wrong answers at random;
        \item if b is the result of $G$, then $\distinguisher^\textsc{ind}$ has a better chance in finding the right answer by its own design;
    \end{itemize}
    From these observations, especially the second point, the adversary \adversary{} has a better chance of winning the \prg{} game by asserting that $x_b$ comes from $G$ whenever $\distinguisher^\textsc{ind}$ makes a correct guess; conversely, \adversary{} will preferably declare that $x_b$ is truly random whenever $\distinguisher^\textsc{ind}$ fails a guess, as the probability of the latter getting fooled by a random value is sensibly greater than by a pseudo-random one.

    Either way, by the existence of $\distinguisher{}^\textsc{ind}$, \adversary{} gains an edge in efficiently recognizing $G$, which cannot happen by $G$'s definition; the claim is proven. The theorem's proof can be completed by putting the pieces together, as is usual in the hybrid argument:
    \[
        \cryptog{ind}[\Pi_\oplus](\lambda, 0) \compindist
        \hybridg{}[\Pi_\oplus](\lambda, 0) \equiv
        \hybridg{}[\Pi_\oplus](\lambda, 1) \compindist
        \cryptog{ind}[\Pi_\oplus](\lambda, 1)
    \]

    which finally states that $\cryptog{ind}[\Pi_\oplus](\lambda, 0) \compindist \cryptog{ind}[\Pi_\oplus](\lambda, 1)$.
\end{proof}

\section{Pseudorandom functions}

\textsc{Prg}s are used in practice as a stepping stone for building \emph{pseudo-random functions}, \prf{} henceforth, which are the principal construct in several cryptographic schemes. Before formally introducing what a \prf{} is, we begin instead by defining what a \emph{truly random function} is:

\begin{definition}
    A random function $R : \binary^n \to \binary^l$ is a function that, depending on what is known about its previous applications:

    \begin{itemize}
        \item if $x$ is ``fresh'' (in formal terms, $R$ has never been applied to $x$ beforehand), then a value $y$ is chosen \uar{} from $R$'s codomain, and it is permanently associated as the image of $x$ in $R$\footnote{This property is also called \emph{lazy sampling}.};

        \item if $x$ is not fresh, then $R(x)$ is directly returned instead. \qedhere
    \end{itemize}
\end{definition}

It should be noted that, if such functions are to be implemented in computers, they would occupy too much space in memory. Suppose all the possible outputs of $R$ have been generated and stored as an array in memory; then its total size in bits will be $l \cdot 2^n$:

\[
    \color{black!50}
    \overbracket[0.5pt][5pt]{\;\color{black}\framebox[4em][c]{0010...}\;}^{l \text{ bits}}_1\;
    \overbracket[0.5pt][5pt]{\;\color{black}\framebox[4em][c]{1110...}\;}^{l \text{ bits}}_2\;
    \overbracket[0.5pt][5pt]{\;\color{black}\framebox[4em][c]{0011...}\;}^{l \text{ bits}}_3\;\;
    \color{black!25}
    \framebox[2em][c]{\rule{0pt}{1.5ex}...}\;
    \framebox[2em][c]{\rule{0pt}{1.5ex}...}\;
    \framebox[2em][c]{\rule{0pt}{1.5ex}...}\;
    \framebox[2em][c]{\rule{0pt}{1.5ex}...}\;\;
    \color{black!50}
    \overbracket[0.5pt][5pt]{\;\color{black}\framebox[4em][c]{1110...}\;}^{l \text{ bits}}_{2^n}
\]

Such a function becomes cumbersome and difficult to maintain in practice; therefore, it is desirable to find a kind of function which looks as a random function possible, but does not require to be wholly memorized, while not forgetting to maintain poly-time complexity. Pseudo-randomness comes to the rescue here:

\begin{definition}
    Let $f$ be a function, then it is deemed pseudo-random (therefore, $f$ is a \prf{}) iff it is computationally indistinguishable from a true random function.
\end{definition}

In detail, \prf{}s are actually designed as function families $f_k$\footnotemark, where $k$ is a parameter that indexes the functions inside the family. To model the \prf{}s' indistinguishability from random functions, let $F \in \binary^\lambda \to (\binary^{n(\lambda)} \to \binary^{l(\lambda)} )$, usually denoted simply by $f_k$, be a \prf{}, and define $\mathfrak{R}(n, l)$ to be the domain that collects the random functions from $\binary^{n(\lambda)}$ to $\binary^{l(\lambda)}$.

\footnotetext{Does this remind you of something else? If not, look back in lesson 2 and 3.}

Consider the indistinguishability game drawn in figure \ref{cryptogame:prf}; although one may thing that \prg{}s and \prf{}s aren't much different, the game tells a different story, which is best put by an introductory paragraph about \prf{}s in their Wikipedia page:

\begin{quotation}
    ``Pseudorandom functions are not to be confused with pseudorandom generators (\prg{}s). The guarantee of a \prg{} is that a single output appears random if the input was chosen at random. On the other hand, the guarantee of a \prf{} is that all its outputs appear random, regardless of how the corresponding inputs were chosen, as long as the function was drawn at random from the \prf{} family.''\footnote{\linkicon \href
        {https://en.wikipedia.org/wiki/Pseudorandom_function_family}
        {\textsf{Pseudorandom function family --- Wikipedia}}}
\end{quotation}

\begin{cryptogame}
    {prf}
    {The \prf{} indistinguishability game}
    {prf}

    \cseqchallenger{\shortstack[l]{
        $k \pickUAR \binary^\lambda$ \\
        $R \pickUAR \mathfrak{R}(n, l)$ \\
        $b \pickUAR \binary$
    }}

    \cseqdelay
    \cseqbeginloop

    \send{}{$x$}{}

    \receive{\shortstack[l]{
        $y_0 = f_k(x)$ \\
        $y_1 = R(x)$
    }}{$y_b$}{}

    \cseqendloop
    \cseqdelay

    \send{}{$b'$}{\textsc{Output 1 iff} $b = b'$}

\end{cryptogame}

In this game, \adversary{} is allowed to make multiple queries to $\challenger^\prf$, as opposed to the \prg{} indistinguishability game where it can perform just one query before making its guess. To reiterate the \prf{} definition in terms of this game:

\begin{definition}
    A function family $F = f_k$ is a \prf{} iff:
    \[
        \cryptog{prf}[f_k](\lambda, 0) \compindist \cryptog{prf}[f_k](\lambda, 1) \qedhere
    \]
\end{definition}

\begin{exercise}
    Prove the following statements:
    \begin{itemize}
        \item No \prg{} is secure against unbounded attackers;
        \item No \prf{} is secure against unbounded attackers.
    \end{itemize}

\end{exercise}

\subsection{\textsc{Ggm}-tree}

This section is dedicated to a concrete example of a \prf{} which is built from the ground up using a \prg. This construct has been designed from Oded Goldreich, Shafi Goldwasser and Silvio Micali, and its structure is akin to a binary tree, hence its name: \emph{\ggm-tree}.

\begin{construction}
    Let $G \in \binary^\lambda \to \binary^{2\lambda}$ be a \prg{} such that it doubles the length of its argument, and denote the images' first and second halves as $G_0(k)$ and $G_1(k)$ respectively, so that:
    \[
        k \mapsto (G_0(k), G_1(k))
    \]

    Since the principal mechanism makes use of the halves being the same length of the argument, in the same spirit, we will denote the action of using one half of an image of $G$ as argument of $G$ itself in a shorter fashion, as demonstrated in the following example:
    \[
        G_a(G_b(G_c(k))) =: G_{abc}(k)
    \]

    This leads to the final step: let $f_k$ be a function family, where $k \in \binary^\lambda$, such that:
    \[
        f_k(r) = G_r(k)
    \]

    This is our candidate \prf. To visualize it, consider the tree structure depicted in figure \ref{fig:ggmtree}: at each level of the tree, a single bit of $r$ is used to decide which half of $G$'s image will be used in the next level. For example, $f_k(01 \dots 10)$ would evaluate as $G_0(G_1( \dots G_1(G_0(k))))$.

    \begin{figure}
        \centering
        \begin{tikzpicture}[
            level 1/.style={sibling distance=16em},
            level 2/.style={sibling distance=8em},
            level 3/.style={sibling distance=4em}]

            \node{$k$}
                child{ node {$G_0(k)$}
                    child{ node {$G_{00}(k)$}
                        child{ node{$G_{000}(k)$} node[below]{$\vdots$}}
                        child{ node{$G_{001}(k)$} node[below]{$\vdots$}}
                    }
                    child{ node {$G_{01}(k)$}
                        child{ node{$G_{010}(k)$} node[below]{$\vdots$}}
                        child{ node{$G_{011}(k)$} node[below]{$\vdots$}}
                    }
                }
                child{ node {$G_1(k)$}
                    child{ node {$G_{10}(k)$}
                        child{ node{$G_{100}(k)$} node[below]{$\vdots$}}
                        child{ node{$G_{101}(k)$} node[below]{$\vdots$}}
                    }
                    child{ node {$G_{11}(k)$}
                        child{ node{$G_{110}(k)$} node[below]{$\vdots$}}
                        child{ node{$G_{111}(k)$} node[below]{$\vdots$}}
                    }
                };
        \end{tikzpicture}
        \caption{The \textsc{ggm}-tree for $G$}
        \label{fig:ggmtree}
    \end{figure}

\end{construction}

The proof of \prf-ness will be discussed in the next lesson.


%
% CMPT 310: Artificial Intelligence - A Course Overview
% Section: Game Playing and Adversarial Search
%
% Author: Jeffrey Leung
%

\section{Game Playing and Adversarial Search}
	\label{sec:game-playing-adversarial-search}
\begin{easylist}

& Game playing: Scenario where a game with discrete states is played with two players, each seeking to maximize their position which minimizes the opponent's position
	&& E.g. Chess, checkers
	&& \textbf{Heuristic/evaluation function:} Estimate of the optimality of a game state
	&& \textbf{Terminal state:} Game state which has no further states
	&& Example diagram of game state evaluations in a tree: See figure~\ref{fig:game-tree}

\begin{figure}[!htb]
	\caption{Game Tree (Minimizer first)}
	\label{fig:game-tree}
	\centering

	\begin{forest}
		for tree={
			draw,
			circle,
			minimum size=1cm,
			line width=0,
			align=center
		},
		[0
			[2
				[2]
				[-3]
				[-14]
			]
			[14
				[14]
				[17]
				[20]
			]
			[0
				[-12]
				[-18]
				[0]
			]
		]
	\end{forest}
\end{figure}

& \textbf{Minimax algorithm:} Decision-making rule which minimizes the potential loss for a worst-case scenario, given two entities who are alternatively trying to maximize and minimize a score
	&& Performs the equivalent of a depth-first search
	&& Each alternating level of the decision tree is minimized or maximized from the values in the levels below it
	&& Complete: Yes, as long as the tree is finite

& \textbf{$\alpha / \beta$ pruning:} Keeping the maximum/minimum searched values at a given level so far to eliminate nodes which will not exceed a minimum/maximum
	&& $\alpha$: Lower bound on the potential value of a maximum node
	&& $\beta$: Upper bound on the potential value of a minimum node
	&& \href{http://inst.eecs.berkeley.edu/~cs61b/fa14/ta-materials/apps/ab_tree_practice/}{Resource for practice}
	&& See figure~\ref{fig:game-tree-ab} for an example of pruned nodes (notated by dashed edges)

\begin{figure}[!htb]
	\caption{Game Tree with $\alpha / \beta$ Pruning (Minimizer first)}
	\label{fig:game-tree-ab}
	\centering

	\begin{forest}
		for tree={
			draw,
			circle,
			minimum size=1cm,
			line width=0,
			align=center
		},
		[0
			[2
				[2]
				[-3]
				[-14]
			]
			[14
				[14]
				[17, edge={densely dashed}]
				[20, edge={densely dashed}]
			]
			[0
				[-12]
				[-18]
				[0]
			]
		]
	\end{forest}
\end{figure}

& Time, space, and optimal complexity of algorithms: See figure~\ref{fig:algos-complexity}

\end{easylist}
\clearpage