In the original release of the \mf Drain (DRN) Package a drain is designed to simulate the effects of agricultural drains, springs, and other features that remove water from the aquifer at a rate proportional to the difference between the head in the aquifer and some fixed head or elevation, called the drain elevation, so long as the head in the aquifer is above that elevation. If, however, the aquifer head falls below the drain elevation, then the drain has no effect on the aquifer. The constant of proportionality is called the drain conductance.  This chapter describes a new feature of the \mf DRN Package that allows the drain conductance to gradually increase from zero to the user-specified value as a function of the simulated groundwater head.

\subsection{Theory}

The standard drain equation in \mf is

\begin{align}
	\label{eqn:bcdrnqout}
    	\mli{Qout}_{nb} = \begin{dcases}
    		0 &h_{n} \le \mli{HDRN}_{nb} \\
    		\mli{CDRN}_{nb} \left( h_{n} - \mli{HDRN}_{nb} \right) &h_{n} > \mli{HDRN}_{nb}
	\end{dcases} ,
\end{align}

\noindent where $\mli{Qout}_{nb}$ is the flow from the aquifer to drain $nb$ (\ulct), $\mli{CDRN}_{nb}$ is the drain conductance (\ulst), $\mli{HDRN}_{nb}$ is the drain elevation (\ul), and $h_{n}$ is the head in the cell containing the drain (\ul). Equation~\ref{eqn:bcdrnqout} rewritten in terms of flow from the drain into aquifer (\ulct), $\mli{QDRN}$, is

\begin{align}
	\label{eqn:bcdrnqdrn}
    	\mli{QDRN}_{nb} = \begin{dcases}
    		\mli{CDRN}_{nb} \left( \mli{HDRN}_{nb} - h_{n} \right) &h_{n} > \mli{HDRN}_{nb} \\
    		0 &h_{n} \le \mli{HDRN}_{nb}
	\end{dcases} .
\end{align}

The standard DRN Package has been modified to include the option to scale the drain conductance using either linear or cubic scaling. The modified form of the drain equation (eq.~\ref{eqn:bcdrnqdrn}) with linear scaling is

\begin{equation}
	\label{eqn:bcdrnqoutalt0}
	\mli{QDRN}_{nb} = F_{\mli{DRN}_{nb}} \mli{CDRN}_{nb} \left( \mli{ZDRN}_{nb} - h_{n} \right) ,
\end{equation}

\noindent where $\mli{ZDRN}_{nb}$ is the elevation at which drainage discharge begins (\ul), and $F_{\mli{DRN}_{nb}}$ is the linear scaling function (unitless), which accounts for how the conductance of the drain changes with changes in head. Conceptually, the drain conductance changes because the area through which flow occurs depends on the head. The area through which flow occurs increases as the head at the drain increases. The linear scaling function is defined as

\begin{equation}
	\begin{aligned}
		\label{eqn:bcdrnFC}
		F_{\mli{DRN}_{nb}} = \begin{dcases}
			0 &\phantom{0 <} \Delta h_{n,nb}^{r} \le 0 \\
			\Delta h_{n,nb}^{r}  &0 < \Delta h_{n,nb}^{r} < 1 \\
			1 &\phantom{0 <} \Delta h_{n,nb}^{r} \ge 1
		\end{dcases} ,
	\end{aligned}
\end{equation}

\noindent where $\mli{DDRN}_{nb}$ is the drainage depth (\ul) and the relative drain head difference (\ul) is defined as

\begin{equation}
	\label{eqn:reldrainheaddiff}
	\Delta h_{n,nb}^{r} \equiv \frac{h_n - \mli{ZDRN}_{nb}}{\left| \mli{DDRN}_{nb} \right|} .
\end{equation}

\noindent The elevation at which drainage begins, $\mli{ZDRN}_{nb}$ depends on whether the drainage depth, $\mli{DDRN}_{nb}$, is positive or negative and is calculated as

\begin{align}
	\label{eqn:bcdrnZDRN}
	\mli{ZDRN}_{nb} = \begin{dcases}
		\mli{HDRN}_{nb} - \left| \mli{DDRN}_{nb} \right| &\mli{DDRN}_{nb} < 0, \\
		\mli{HDRN}_{nb} &\mli{DDRN}_{nb} \ge 0
    	\end{dcases} .
\end{align}

\noindent If $\mli{DDRN}_{nb}$ is positive, $\mli{ZDRN}_{nb}$ is the drain elevation just as it is in the standard formulation. If $\mli{DDRN}_{nb}$ is negative, $\mli{ZDRN}_{nb}$ is the drain elevation plus the (negative) drainage depth. If $\mli{DDRN}_{nb}$ is zero, the standard formulation is used. The linear scaling function ($F_{\mli{DRN}_{nb}}$) is shown in figure~\ref{fig:drndischscalef}.

\begin{figure}[!ht]
	\begin{center}
	\includegraphics{./Figures/DischargeScaleFactor.pdf}
	\caption[Graphs showing combined linear and cubic scaling functions for drain discharge and the derivatives of the linear and cubic scaling fraction functions]{Graphs showing combined linear and cubic scaling functions for drain discharge and the derivatives of the linear and cubic scaling fraction functions. \textit{A}, linear and cubic fraction functions and \textit{B}, derivatives of the linear and cubic scaling functions with respect to the relative drain head difference $\left( \frac{h - \mli{ZDRN}}{\left| \mli{DDRN} \right|} \right)$. The derivative of the linear and cubic scaling functions with respect to $h$ is the product of the derivative with respect to $\frac{h - \mli{ZDRN}}{\left| \mli{DDRN} \right|}$ and $\frac{1}{\mli{DDRN}}$}
	\label{fig:drndischscalef}
	\end{center}
\end{figure}

The linear scaling function varies with head according to equations~\ref{eqn:bcdrnFC} and~\ref{eqn:reldrainheaddiff}. Differentiation of equation~\ref{eqn:bcdrnFC} with respect to $h_n$, with application of the chain rule to account for the variation of $ \Delta h_{n,nb}^{r}$ with respect to $h_n$ in equation~\ref{eqn:reldrainheaddiff}, gives

\begin{equation}
	\label{eqn:bcdrndFC}
	\begin{aligned}
		\frac{\partial F_{\mli{DRN}_{nb}}}{\partial h_n} = \begin{dcases}
			0 &\phantom{0 <} \Delta h_{n,nb}^{r} \le 0 \\
			\frac{1}{\left| \mli{DDRN}_{nb} \right|}  &0 < \Delta h_{n,nb}^{r} < 1 \\
			1 &\phantom{0 <} \Delta h_{n,nb}^{r} \ge 1
		\end{dcases} ,
	\end{aligned}
\end{equation}

\noindent which is discontinuous in the neighborhood of $h_n = \mli{ZDRN}_{nb}$ and $h_n = \mli{ZDRN}_{nb} - \left| \mli{DDRN}_{nb} \right| $. The derivative of the linear scaled drain discharge (eq.~\ref{eqn:bcdrnqoutalt0}) with respect to $h_n$ is

\begin{equation}
	\label{eqn:bcdrnNRlindqoutalt0}
	\frac{\partial \mli{QDRN}_{nb}}{\partial h_n} = -F_{\mli{DRN}_{nb}} \mli{CDRN}_{nb}  + \frac{\partial F_{\mli{DRN}_{nb}}}{\partial h_n} \mli{CDRN}_{nb} \left( \mli{ZDRN}_{nb} - h_{n} \right) .
\end{equation}

\noindent Substitution of equations ~\ref{eqn:bcdrnFC} and~\ref{eqn:bcdrndFC} into equation~\ref{eqn:bcdrnNRlindqoutalt0} results in

\begin{equation}
	\begin{aligned}
		\label{eqn:bcdrnNRlindQdh}
		\frac{\partial \mli{QDRN}_{nb}}{\partial h_n} = \begin{dcases}
			0 &\phantom{0 <} \Delta h_{n,nb}^{r} \le 0 \\
			- 2\, \mli{CDRN}_{nb} \, \Delta h_{n,nb}^{r}  &0 < \Delta h_{n,nb}^{r} < 1 \\
			-\mli{CDRN}_{nb} &\phantom{0 <} \Delta h_{n,nb}^{r} \ge 1
		\end{dcases} ,
	\end{aligned}
\end{equation}

\noindent which remains discontinuous in the vicinity of $\mli{ZDRN}_{nb}$.

When the Newton-Raphson formulation is used, discontinuous derivatives can cause non-convergence in the neighborhood of the discontinuity \citep{doi:10.1029/2006WR005195}. To ensure continuous drain discharge derivatives when the Newton-Raphson formulation is used, cubic smoothing of the linear relative drain head difference is used and equation~\ref{eqn:bcdrnqoutalt0} is modified to

\begin{equation}
	\label{eqn:bcdrnNRqoutalt0}
	\mli{QDRN}_{nb}^* = F_{\mli{DRN}_{nb}}^* \mli{CDRN}_{nb} \left( \mli{ZDRN}_{nb} - h_{n} \right) ,
\end{equation}

\noindent where where $F_{\mli{DRN}_{nb}}^*$ is the cubic scaling function. The cubic scaling function is defined as

\begin{equation}
	\begin{aligned}
		\label{eqn:bcdrnFCC}
		F_{\mli{DRN}_{nb}}^* = \begin{dcases}
			0 & \phantom{0 <} \Delta h_{n,nb}^{r} \le 0 \\
			- \left( \Delta h_{n,nb}^{r} \right)^3  + 2 \left( \Delta h_{n,nb}^{r} \right)^2  & 0 < \Delta h_{n,nb}^{r} < 1 \\
			1 & \phantom{0 <} \Delta h_{n,nb}^{r} \ge 1
		\end{dcases} ,
	\end{aligned}
\end{equation}

\noindent which is continuous in the vicinity of $h_n = \mli{ZDRN}_{nb}$ and $h_n = \mli{ZDRN}_{nb} - \left| \mli{DDRN}_{nb} \right| $ (fig.~\ref{fig:drndischscalef}\textit{A}). The derivative of equation~\ref{eqn:bcdrnFCC} with respect to $h_n$ is

\begin{equation}
	\begin{aligned}
		\label{eqn:bcdrndFCC}
		\frac{\partial F_{\mli{DRN}_{nb}}^*}{\partial h_n} = \begin{dcases}
			0 & \phantom{0 <} \Delta h_{n,nb}^{r} \le 0 \\
			-\frac{3}{\left| \mli{DDRN}_{nb} \right|} \left( \Delta h_{n,nb}^{r} \right)^2  + \\
			\phantom{-}\frac{4}{\left| \mli{DDRN}_{nb} \right|} \, \Delta h_{n,nb}^{r} & 0 < \Delta h_{n,nb}^{r} < 1 \\
			0 & \phantom{0 <} \Delta h_{n,nb}^{r} \ge 1
		\end{dcases} ,
	\end{aligned}
\end{equation}

\noindent which is discontinuous in the vicinity of $h_n = \mli{ZDRN}_{nb} - \left| \mli{DDRN}_{nb} \right| $ (fig.~\ref{fig:drndischscalef}\textit{B}). The derivative of the cubic scaled drain discharge (eq.~\ref{eqn:bcdrnNRqoutalt0}) with respect to $h$ is

\begin{equation}
	\label{eqn:bcdrnNRdqoutalt0}
	\frac{\partial \mli{QDRN}_{nb}^*}{\partial h} = -F_{\mli{DRN}_{nb}}^* \mli{CDRN}_{nb}  + \frac{\partial F_{\mli{DRN}_{nb}}^*}{\partial h} \mli{CDRN}_{nb} \left( \mli{ZDRN}_{nb} - h_{n} \right) .
\end{equation}

\noindent Substitution of equations~\ref{eqn:bcdrnFCC} and~\ref{eqn:bcdrndFCC} into equation~\ref{eqn:bcdrnNRdqoutalt0} results in

\begin{equation}
	\begin{aligned}
		\label{eqn:bcdrndQdh}
		\frac{\partial \mli{QDRN}_{nb}^*}{\partial h_n} = \begin{dcases}
			0 & \phantom{0 <} \Delta h_{n,nb}^{r} \le 0 \\
			-\mli{CDRN}_{nb} \Biggl[ - \left( \Delta h_{n,nb}^{r} \right)^3  + 2 \left( \Delta h_{n,nb}^{r} \right)^2 \Biggr] + \\
			\phantom{-} \mli{CDRN}_{nb} \Biggl[- \frac{3}{\mli{DDRN}_{nb}} \left( \Delta h_{n,nb}^{r} \right)^2  + \Biggr. \\
			\Biggl. \phantom{-\mli{CDRN}_{nb} \Biggl[- } \frac{4}{\mli{DDRN}_{nb}} \left( \Delta h_{n,nb}^{r} \right) \Biggr] \left( \mli{ZDRN}_{nb} - h_{n} \right) & 0 < \Delta h_{n,nb}^{r} < 1 \\
			-\mli{CDRN}_{nb} & \phantom{0 <} \Delta h_{n,nb}^{r} \ge 1
		\end{dcases} ,
	\end{aligned}
\end{equation}

\noindent which is continuous in the vicinity of $h_n = \mli{ZDRN}_{nb}$ and $h_n = \mli{ZDRN}_{nb} - \left| \mli{DDRN}_{nb} \right| $.

\subsection{Example Drain Discharge Scaling Calculations}

An example of the differences between the standard, linearly-scaled, and cubicly-scaled drainage discharge are shown in figure~\ref{fig:drndischdiff}. In this example the elevation that drainage discharge starts ($\mli{ZDRN}$) is set at $\frac{\mli{DDRN}}{2}$ below land surface elevation and drainage discharge is equal for all approaches at $\mli{ZDRN} + \mli{DDRN}$ (fig.~\ref{fig:drndischdiff}\textit{A}). In this conceptual problem, the groundwater head linearly increases from a value less than $\mli{ZDRN}$ to greater than $\mli{ZDRN + DDN}$ during the simulation, which is presented as the head difference ($h - \mli{ZDRN}$) divided by the drainage depth in figure~\ref{fig:drndischdiff}\textit{B}. The drainage discharge that results from the linear increase in groundwater head using the original-, linear-, and cubic-scaling increases with time as is shown in figure~\ref{fig:drndischdiff}\textit{C} and shows the continuous nature of the linear- and cubic scaled drainage discharge and that all three approaches result in the same drain discharge rate when the relative drain head difference is greater than or equal to one. Figure~\ref{fig:drndischdiff}\textit{D} shows that the cumulative drain discharge for the original drain formulation is 170 to 200 $L^3$ greater than the cubic- and linear-scaled drainage discharge, respectively.

\begin{figure}[!ht]
	\begin{center}
	\includegraphics{./Figures/DRNDischargeDifferences.pdf}
	\caption[Graphs showing a conceptual model cell using scaled drain discharge and the relation between the groundwater level and drain discharge]{Graphs showing a conceptual model cell using scaled drain discharge and the relation between the groundwater level and drain discharge. \textit{A}, Conceptual model cell containing a drain cell where drainage discharge starts $\frac{\mli{DDRN}}{2}$ below land surface and is equal $\frac{\mli{DDRN}}{2}$ above land surface for all drain scaling approaches, \textit{B}, conceptual linear groundwater level increases, which are presented as a relative drain head difference, with fractional simulation time, \textit{C}, calculated original, linear, and cubic scaled drainage discharge rates resulting from conceptual groundwater level increases, and \textit{D}, calculated original, linear, and cubic scaled drainage cumulative discharge resulting from conceptual groundwater level increases}
	\label{fig:drndischdiff}
	\end{center}
\end{figure}

\subsection{Incorporation of the modified Drain (DRN) Package into the CVFD Groundwater Flow Equation}

To prepare the CVFD equation for solution using the standard formulation, it is convenient to rearrange the discretized groundwater flow equation for a cell so that all terms containing heads at the end of the current time step are grouped on the left-hand side of the equation, and all terms that are independent of head at the end of the current time step are on the right-hand side of equation 6--1 in \cite{modflow6gwf}. Refer to \cite{modflow6gwf} for additional information on how the groundwater flow equation is formulated in \mf.

\subsubsection{Standard Formulation}

According to the sign convention in \mf, $\mli{QDRN}_{nb}$ in the groundwater flow equation is defined as a head-dependent flow out of cell $n$, and corresponding terms must be added to the left and right sides of equation~6--1 in \cite{modflow6gwf} for each cell containing a drain. This is accomplished in the modified Drain Package by adding the head-dependent term and the known term in equation~\ref{eqn:bcdrnqoutalt0} to the left- and right-side of equation~6--1 in \cite{modflow6gwf}. The contribution of the modified drain equations to the left-hand side and right-hand sides of the groundwater flow equation are

\begin{equation}
	\label{eqn:qdrnstd}
	\begin{aligned}
		A_{n,n} &\leftarrow A_{n,n} - F_{\mli{DRN}_{nb}} \mli{CDRN}_{nb}   \\
		b_n &\leftarrow b_n - F_{\mli{DRN}_{nb}} \mli{CDRN}_{nb} ZDRN_{nb} ,
	\end{aligned}
\end{equation}

\noindent where $A_{n,n}$ is the diagonal of the coefficient matrix for cell $n$ and $b_n$ is the right-hand side of the groundwater flow equation for cell $n$. In the case where drainage discharge is not scaled, $F_{\mli{DRN}_{nb}}$ is zero when $h_n$ is less than or equal to $HDRN_{nb}$ and one when $h_n$ is greater than $HDRN_{nb}$, which results in identical behavior as the original drain package formulation.

\subsubsection{Newton-Raphson Formulation}

The modified Newton-Raphson form of equation~\ref{eqn:bcdrnNRqoutalt0} solved in terms of $h$ instead of $\Delta h$  and incorporated into the Newton-Raphson form of the groundwater flow equation \citep[eq. 2--26]{modflow6gwf} is

\begin{equation}
	\label{eqn:nrQout}
	\frac{\partial \mli{QDRN}_{nb}^*}{\partial h_n}  h^k_n  = - \mli{QDRN}_{nb}^* +
	\frac{\partial \mli{QDRN}_{nb}^*}{\partial h_n}  h^{k-1}_n,
\end{equation}

\noindent where $h^k_n$ is the head at the end of the current non-linear (picard) iteration and $h^{k-1}_n$ is the head at the start of the current non-linear iteration. Substitution of equations~\ref{eqn:bcdrnNRqoutalt0} and~\ref{eqn:bcdrnNRdqoutalt0} into equation~\ref{eqn:nrQout} results in

\begin{equation}
	\label{eqn:nrQout01}
	\begin{aligned}
		A_{n,n} \leftarrow & A_{n,n} - F_{\mli{DRN}_{nb}}^* \mli{CDRN}_{nb}  + \frac{\partial F_{\mli{DRN}_{nb}}^*}{\partial h} \mli{CDRN}_{nb} \left( \mli{ZDRN}_{nb} - h_{n} \right) \\
		b_n \leftarrow & b_n - F_{\mli{DRN}_{nb}}^* \mli{CDRN}_{nb} \left( \mli{ZDRN}_{nb} - h_{n}^{k-1} \right) \\
		& \phantom{b_n} + \Biggl[ -F_{\mli{DRN}_{nb}}^* \mli{CDRN}_{nb} + \frac{\partial F_{\mli{DRN}_{nb}}^*}{\partial h} \mli{CDRN}_{nb} \left( \mli{ZDRN}_{nb} - h_{n} \right) \Biggr] h^{k-1}_n .
	\end{aligned}
\end{equation}

\noindent Simplifying the right-hand side contribution in equation~\ref{eqn:nrQout01} results in

\begin{equation}
	\label{eqn:nrQout02}
	b_n \leftarrow b_n - F_{\mli{DRN}_{nb}}^* \mli{CDRN}_{nb} \mli{ZDRN}_{nb} + \Biggl[ \frac{\partial F_{\mli{DRN}_{nb}}^*}{\partial h} \mli{CDRN}_{nb} \left( \mli{ZDRN}_{nb} - h_{n}  \right) \Biggr] h^{k-1}_n .
\end{equation}

\noindent The $F_{\mli{DRN}_{nb}}^* \mli{CDRN}_{nb}$ term in equation~\ref{eqn:nrQout01} and the $F_{\mli{DRN}_{nb}}^* \mli{CDRN}_{nb} \mli{ZDRN}_{nb}$ term in equation~\ref{eqn:nrQout02} are subtracted from the diagonal of the coefficient matrix and the right-hand side of the groundwater-flow equation, respectively, during the standard formulation step for the drain package. The Newton-Raphson formulation for the modified drain package is completed by augmenting the coefficient matrix with the derivative term in equation~\ref{eqn:nrQout01} and adding the second term in equation~\ref{eqn:nrQout02} (the product of a derivative term and the head at the start of the current iteration) to the right-hand side of the groundwater-flow equation.

\subsection{Use of Drain Discharge Scaling}

A few examples of how the modified drain package can be used to simulate drainage discharge consistent with other \mf packages are given below.

\paragraph{The original drain package formulation with a specified drainage depth value}

Specifying $\mli{DDRN}$ to be 0 results in a drain that behaves the same as the original DRN Package (eq.~\ref{eqn:bcdrnqout}). Using a combination of $\mli{DDRN}$ values set to zero and non-zero values will result in drains with 0 values behaving like the original drain package and others using the scaled drainage discharge approach.

\paragraph{Groundwater seepage from the Unsaturated Zone Flow Package}

The drain package can be used as an alternative to the groundwater seepage option in the Unsaturated Zone Flow (UZF) Package. Specifying a positive $\mli{DDRN}$ value and $\mli{HDRN}_{nb}$ to be $\frac{\mli{DDRN}}{2}$ below the single values specified with the standard DRN Package approach (for example, setting $\mli{DDRN}$ to be at land surface) results in a formulation that is equivalent to the groundwater seepage option available in the UZF Package in \mf \citep{modflow6gwf}. To be consistent with the UZF package, the drain conductance should be calculated as

\begin{equation}
	\label{eqn:uzfcond}
	\mli{CDRN}_{nb} = \frac{K_{v_{nb}} A_n}{\mli{DDRN}_{nb}} ,
\end{equation}

\noindent where $K_{v_{nb}}$ is vertical hydraulic conductivity (\ult) and $A_n$ is the horizontal area of cell $n$ (\uls).


\providecommand{\main}{..}
\documentclass[\main/main.tex]{subfiles}
\begin{document}

\chapter{The classifier}

\section{Training the classifier}

\subsection{Convert documents to vector representation}
Given a set of training class-labeled elements \(T\), we convert the documents, treated as \textit{bag of elements}, to enumeration-sorted frequency vectors: the most common words in the language are dropped before elaborating the documents.

Given a document composed of \(n=\#d\) elements \(d = \crl{e_1, e_2, \ldots, e_n}\), first we define \(d_\neq \subseteq d\) as the set of distinct elements in \(d\). Then, we map to every distinct element its normalized cardinality in the set \(d\):
\[
	z(e_i) = \frac{\arity{e_j \in d: e_j=e_i}}{n}
\]
Then, we proceed to map to a common enumeration the elements, using as full-set of elements the entire training set elements, so that \(\forall e_i, e_j \in T, \exists! i, j \in \N: i=j \Leftrightarrow e_i=e_j \).

\subsection{Choosing representative points}
Given an arbitrary percentage of points \(p \in \sqr{0,1}\) and an arbitrary distance in percentage \(\alpha \in \sqr{0,1}\), for each class of points \(C_j \in T\) we choose using \(k\)-Means:
\[
	k=\ceil{\#C_j\cdot p^2}
\]
This way the centroids surely distribute among the different points, following their density. If the points are, in truth, a unique cluster, the centroids will distribute themselves in an uniform fashion.

Then, we select the most distant points in every cluster in a number equal to \(\floor{\arity{p \in C_j: p \in Q_i}\cdot p}\). We move each point \(p\) of an amount proportional to the distance from the point to its centroid \(c_i\):\(\alpha \cdot L^2\rnd{p, c_i}\) towards its centroid.

\subsubsection{Using a metric to choose k}
For the high dimensionality and number of the vectors, iterating multiple times \textit{KMeans} searching for an optimal \(k\) following any given metric has an high time cost. For this reason, an attempt using \textbf{PCA} to reduce the dimensionality and predict the number of clusters in high dimensionality space using a density metric was made.

\paragraph*{The density was defined as follows:}
\[
	\bar{\rho}_{jk} = \frac{1}{k} \sum_{i=1}^k \rho_{{jk}_i} = \frac{1}{k} \sum_{i=1}^k \rnd{\frac{\arity{\bmv \in C_{r_j}:\bmv \in Q_i}}{\#C_{r_j}}}^k \cdot \frac{1}{r_{Q_i}^2} \qquad r_{Q_i}^2 = \begin{cases}
		\frac{1}{n} \sum_{h=1}^{n} {(\bmc_i - \bmp_h)}^2 & \text{if \(n \neq 0\)} \\
		1                                                & \text{else}
	\end{cases}
\]
Where \(r_{Q_i}\) is the approximated radius of the cluster \(Qk_i\), using the farthest \(n\) frontier points \(p_f\).

\paragraph*{Does the prediction hold?}
While the metric on two dimensions reduction seemed to work, when iterated on increasingly larger number of dimensions, with the exception of strongly clustered data, it did not much better than a random selection.

As follows, with 2D PCA density metrics the heuristic seems to be successful to identify the number of clusters \(k\) necessary to describe the given classes.
\begin{figure}
	\begin{subfigure}{0.24\textwidth}
		\includegraphics[width=\textwidth]{multi-cluster}
		\caption{Data with multi-clusters, 2D PCA, \(k=6\)}
	\end{subfigure}
	~
	\begin{subfigure}{0.24\textwidth}
		\includegraphics[width=\textwidth]{multi-cluster-pca-score}
		\caption{Density for data multi-clusters, 2D PCA, density/k}
	\end{subfigure}
	~
	\begin{subfigure}{0.24\textwidth}
		\includegraphics[width=\textwidth]{one-cluster}
		\caption{Data with single-cluster, 2D PCA, \(k=1\)}
	\end{subfigure}
	~
	\begin{subfigure}{0.24\textwidth}
		\includegraphics[width=\textwidth]{one-cluster-pca}
		\caption{Density for data single-cluster, 2D PCA, density/k}
	\end{subfigure}
\end{figure}

The prediction on any given PCA reduction however is not valid for different dimension numbers, on multi-clusters classes.

Any number of clusters \(k\) high enough (\(k>5\)) is no more precise than a density metric. With increasing number of clusters, as shown below, it becomes increasingly unreliable.

\begin{figure}
	\begin{subfigure}{0.49\textwidth}
		\includegraphics[width=\textwidth]{one-cluster-pca-k}
		\caption{Best value of \(k\) for increasing dimensions in PCA reduction, one-clusters data.}
	\end{subfigure}
	~
	\begin{subfigure}{0.49\textwidth}
		\includegraphics[width=\textwidth]{multi-cluster-pca-k}
		\caption{Best value of \(k\) for increasing dimensions in PCA reduction, multi-clusters data.}
	\end{subfigure}
\end{figure}

\subsection{Completing the classifier}
The classifier model is now finished, comprised of every class-labeled representative point.

\section{Classifying a document}
To classify a given a document \(d\) we proceed as follows:
\begin{enumerate}
	\item Convert the document \(d\) to a zipf representative vector, using the common enumeration: \(\bmv = z(d)\).
	\item The document is then classified with the same label as the closest representative point in the classifier model.
\end{enumerate}

\end{document}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CS624: Analysis of Algorithms
% Copyright 2015 Pejman Ghorbanzade <pejman@ghorbanzade.com>
% Creative Commons Attribution-ShareAlike 4.0 International License
% More info: https://github.com/ghorbanzade/beacon
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section*{Question 3}

Give an example of a directed graph $G = (V, E)$, a source vertex $s \in V$, and a set of tree edges $E_\pi \subseteq E$ such that for each vertex $v \in V$, the unique simple path in the graph $(V, E_\pi)$ from $s$ to $v$ is a shortest path in $G$, yet the set of edges $E_\pi$ cannot be produced by running BFS on $G$, no matter how the vertices are ordered in each adjacency list.

\subsection*{Solution}

Figure \ref{fig31} provides an example of a graph $G = (V, E)$ in which $E_\pi$ can be chosen as either $\{(a,b), (a,c), (b,d), (c,d)\}$ or $\{(a,b), (a,c), (b,e), (c,d)\}$.
Taking node $a$ as the source vertex, $E_\pi$ will immediately ensure a unique shortest path from $a$ to all other nodes.

\begin{figure}[H]\centering
\tikzstyle{vertex}=[circle,draw,minimum size=0.7cm]
  \begin{tikzpicture}
    \node[vertex] (1) {a};
    \node[vertex] (2) [above right = 0.5cm and 1.5cm of 1] {b};
    \node[vertex] (3) [below right = 0.5cm and 1.5cm of 1] {c};
    \node[vertex] (4) [right = 1.5cm of 2] {d};
    \node[vertex] (5) [right = 1.5cm of 3] {e};
    \path[draw,thick,->]
	(2) edge (5)
	(3) edge (4)
	;
    \path[draw,ultra thick,->]
    (1) edge (2)
    (1) edge (3)
    (2) edge (4)
    (3) edge (5)
    ;
  \end{tikzpicture}
\caption{Directed graph $G$}\label{fig31}
\end{figure}

Applying the breadth-first algorithm to graph $G$ starting from vertex $a$, will result into the Figure \ref{fig:sfig3-1} after one step (visiting vertex $a$).
At step 2, one of nodes $b$ or $c$ must be visited.
Due to symmetry of the graph which ensures no loss of generality, suppose node $b$ will be visited.
Since both nodes $b$ and $c$ are connected to nodes $d$ and $e$, any such visit will put nodes $d$ and $e$ in queue, adding edges $(b,d)$ and $(b,e)$ to the set of tree edges.
In next step of algorithm, vertex $c$ will be visited.
However, since all vertices connected to $c$ are already placed in queue, the visit will not affect the set of tree edges.
Hence, full implementation of the BFS algorithm will result in Figure \ref{fig:sfig3-2}.

\begin{figure}[H]\centering
\tikzstyle{vertex}=[circle,draw,minimum size=0.7cm]
\tikzstyle{not visited}=[]
\tikzstyle{visited}=[fill=gray!50]
\tikzstyle{in queue}=[fill=gray!20]
\tikzstyle{label}=[]
\tikzstyle{ultra thick}=[line width=0.6mm]
  \begin{subfigure}{0.49\textwidth}\centering
    \begin{tikzpicture}
    \node[vertex,visited] (1) {a};
    \node[vertex,in queue] (2) [above right = 0.5cm and 1.5cm of 1] {b};
    \node[vertex,in queue] (3) [below right = 0.5cm and 1.5cm of 1] {c};
    \node[vertex] (4) [right = 1.5cm of 2] {d};
    \node[vertex] (5) [right = 1.5cm of 3] {e};
    \path[draw,thick,->]
	(2) edge (5)
	(3) edge (4)
	;
    \path[draw,ultra thick,->]
    (1) edge (2)
    (1) edge (3)
    (2) edge (4)
    (3) edge (5)
    ;
    \end{tikzpicture}
    \caption{}
    \label{fig:sfig3-1}
  \end{subfigure}
  \begin{subfigure}{0.49\textwidth}\centering
    \begin{tikzpicture}
    \node[vertex,visited] (1) {a};
    \node[vertex,visited] (2) [above right = 0.5cm and 1.5cm of 1] {b};
    \node[vertex,visited] (3) [below right = 0.5cm and 1.5cm of 1] {c};
    \node[vertex,visited] (4) [right = 1.5cm of 2] {d};
    \node[vertex,visited] (5) [right = 1.5cm of 3] {e};
    \path[draw,thick,->]
	(3) edge (4)
	(3) edge (5)
	;
    \path[draw,ultra thick,->]
    (1) edge (2)
    (1) edge (3)
    (2) edge (4)
    (2) edge (5)
    ;
    \end{tikzpicture}
    \caption{}
    \label{fig:sfig3-2}
  \end{subfigure}
\caption{Implementation of BFS on $G$ starting from $a$ at two different steps of the algorithm}\label{fig32}
\end{figure}

Therefore, regardless of the order of adjacency lists, no implementation of the breadth-first search algorithm will generate $E_\pi$ as set of tree edges.


\section{1-D DP}

%%%%%%%%%%
\begin{frame}{1-D DP}
  \begin{exampleblock}{Weighted interval/class scheduling \pno{2.2.20}}
    \begin{itemize}
      \item $\mathcal{C} = \set{c_{1}, c_{2}, \cdots, c_{n}}$
      \item $c_{i}$: $\langle g_{i}, s_{i}, f_{i} \rangle$
      \item choosing non-conflicting classes to maximize your grades
    \end{itemize}

    \fignocaption{width = 0.40\textwidth}{fig/weighted-interval.png}
  \end{exampleblock}
\end{frame}
%%%%%%%%%%
\begin{frame}{1-D DP}
  \begin{block}{Solution.}
    \begin{itemize}
      \item subproblem $G[i]$: the maximal grades obtained from $\set{c_{1}, c_{2}, \cdots, c_{i}}$
      \item goal: $G[n]$
    \end{itemize}
  \end{block}
\end{frame}
%%%%%%%%%%
\begin{frame}{1-D DP}
  \begin{block}{Solution.}
    \begin{itemize}
      \item question: choose $c_{i}$ or not in $G[i]$? % {\footnotesize \textcolor{red}{(binary chioce)}}
      \item recurrence:
	\[
	  G[i] = \max \set{G[i-1], G[j] + g_{i}}
	\]

	$c_{j}$: the last class which does not conflict with $c_{i}$
      \item initialization:
	\[
	  G[0] = 0
	\]
    \end{itemize}
    \uncover<2->{
      \begin{center}
	\textcolor{red}{sort $\mathcal{C}$ by finishing time.}
      \end{center}
    }
  \end{block}
\end{frame}
%%%%%%%%%%
\begin{frame}{1-D DP}
  \begin{alertblock}{Why is ordering necessary?}
    \fignocaption{width = 0.50\textwidth}{fig/weighted-interval-unordered.pdf}

    \[
      G[7] = \max \set{G[6], G[\set{1,3,5}] + g_{7}}
    \]

    \begin{center}
      subproblems changed: all $O(2^{n})$ subsets
    \end{center}
  \end{alertblock}
\end{frame}
%%%%%%%%%%
\begin{frame}{1-D DP}
  \begin{alertblock}{Sorting by starting time?}
    \fignocaption{width = 0.50\textwidth}{fig/weighted-interval-starting-time-ordered.pdf}

    \[
      G[6] = \max \set{G[5], G[\set{2,3}] + g_{6}}
    \]

    \begin{center}
      subproblems changed: all $O(2^{n})$ subsets
    \end{center}
  \end{alertblock}
\end{frame}
%%%%%%%%%%
\begin{frame}{1-D DP}
  \begin{exampleblock}{Maximal sum subarray \pno{2.2.3, 2.2.13, \textcolor{red}{Google Interview Problem}}}
    \begin{itemize}
      \item array $A[1 \cdots n], a_{i} >=< 0$
      \item to find (the sum of) an MS in $A$
	\begin{itemize}
	  \item special case: $\text{mss} = 0$ if all negative
	\end{itemize}
    \end{itemize}

    \[
      A[-2,1 ,-3,4,-1,2,1,-5,4] \Rightarrow [4,-1,2,1]
    \]
  \end{exampleblock}

  \pause
  \begin{alertblock}{Trial and error.}
    \begin{itemize}
      \item try subproblem $\text{MSS}[i]$: the sum of the MS (\text{MS}[i]) in $A[1 \cdots i]$
      \item goal: $\text{mss} = \text{MSS}[n]$
      \item question: Is $a_{i} \in \text{MS}[i]$?
      \item recurrence:
	\[
	  \text{MSS}[i] = \max \set{\text{MSS}[i-1], \textcolor{red}{???}}
	\]
    \end{itemize}
  \end{alertblock}
\end{frame}
%%%%%%%%%%
\begin{frame}{1-D DP}
  \begin{block}{Solution.}
    \begin{itemize}
      \item subproblem $\text{MSS}[i]$: the sum of the MS \textcolor{red}{\it ending with} $a_{i}$ or 0
      \item goal: $\text{mss} = \max_{1 \le i \le n} \text{MSS}[i]$
      \item<2-> question: where does the $\text{MS}[i]$ start?
      \item<2-> recurrence:
	\[
	  \text{MSS}[i] = \max \set{\text{MSS}[i-1] + a_{i}, 0} \text{ \textcolor{red}{(prove it!)}}
	\]
      \item<3-> initialization: $\text{MSS}[0] = 0$
    \end{itemize}

    % \begin{displaymath}
    %   \text{MSS}[i] = \left\{ \begin{array}{ll}
    %     0 & i = 0 \\
    %     \max \set{\text{MSS}[i-1] + a_{i}, 0} & i > 0
    %   \end{array} \right.
    % \end{displaymath}
  \end{block}
\end{frame}
%%%%%%%%%%
\begin{frame}[fragile]{1-D DP}
  \begin{block}{Code.}
    \begin{verbatim}
      MSS[0] = 0
      For i = 1 to n
        MSS[i] = max{MSS[i-1] + A[i], 0}
      return max_{i = 1 to n} MSS[i]
    \end{verbatim}
  \end{block}

  \begin{block}{Simpler code.}
    \begin{verbatim}
      mss = 0
      MSS = 0
      For i = 1 to n
        MSS = max{MSS + A[i], 0}
        mss = max{mss, MSS}
      return mss
    \end{verbatim}
  \end{block}
\end{frame}
%%%%%%%%%%
\begin{frame}{1-D DP}
  \begin{exampleblock}{Reconstructing string \pno{2.2.14}}
    \begin{itemize}
      \item string $S[1 \cdots n]$
      \item dict for \emph{lookup}:
	\begin{displaymath}
	  \text{dict}(w) = \left\{ \begin{array}{ll}
	    \text{true} & \textrm{if } w \textrm{ is a valid word}\\
	    \text{false} & \textrm{o.w.}
	  \end{array} \right.
	\end{displaymath}
      \item Is $S[1 \cdots n]$ valid (reconstructed as a sequence of valid words)?
    \end{itemize}
  \end{exampleblock}

  \begin{block}{Solution.}
    \begin{itemize}
      \item subproblem $V[i]$: is $S[1 \cdots i]$ valid?
      \item goal: $V[n]$
    \end{itemize}
  \end{block}
\end{frame}
%%%%%%%%%%
\begin{frame}{1-D DP}
  \begin{block}{Solution.}
    \begin{itemize}
      \item question: where does the last word start? % {\footnotesize \textcolor{red}{(multi-way choices)}}
      \item recurrence:
	\[
	  V[i] = \bigvee_{j = 1 \ldots i} (V[j-1] \land \text{dict}(S[j \cdots i]))
	\]
      \item initialization: $V[0] = \text{true}$
    \end{itemize}
  \end{block}
\end{frame}
%%%%%%%%%%
\begin{frame}<beamer:0>[fragile]{1-D DP}
    \begin{displaymath}
      V[i] = \left\{ \begin{array}{ll}
	0 & i = 0 \\
	\bigvee_{j = 1 \ldots i} (V[j-1] \land \text{dict}(S[j \cdots i])) & 1 \le i \le n
      \end{array} \right.
    \end{displaymath}

  \begin{block}{Code.}
    \begin{verbatim}
    V[0] = true
    For i = 1 to n
      For j = 1 to i
        V[i] = V[j-1] and Dict(S[j...i])
    return V[n]
    \end{verbatim}
  \end{block}
\end{frame}
%%%%%%%%%%
\begin{frame}<beamer:0>{1-D DP}
  \begin{exampleblock}{A trip through hotels \pno{2.2.21}}
    \begin{itemize}
      \item hotel sequence (distance): $a_{0} = 0, a_{1}, \cdots, a_{n}$
      \item stop at only hotels
      \item cost: $(200 - x)^{2}$
      \item to minimize overall cost
    \end{itemize}
  \end{exampleblock}

  \begin{block}{Solution.}
    \begin{itemize}
      \item $C[i]$: minimum cost when the destination is $a_{i}$
      \item goal: $C[n]$
      \item question: what is the last but one hotel $a_{j}$ to stop in the optimal solution?
      \item recurrence: $C[i] = \min_{0 \le j < i} \set{C[j] + (200 - (a_{i} - a_{j}))^{2}}$
      \item initialization: $C[0] = 0$
    \end{itemize}
  \end{block}
\end{frame}
%%%%%%%%%%


\section{Verification of Distribution over Monoid Concatenation}~\label{sec:monoid:distribution}
In this section we prove distribution over monoid concatenation.
%
Concretely, if a function @f :: i -> m@
from some input @i@ to a monoid @m@,
distributes on the input
@f (i1 stringMappend i2) == (f i1) mappend (f i2)@,
then @f@ distributes over monoid concatenation:
@f i == mconcat (map f (chunk n i))@.
%
For the above property to hold, the input @i@
should satisfy certain properties,
for example, the input should define the methods
@stringMappend@ and @chunk@.
%
First~\S~\ref{subsec:rstring} we defined Refined Strings
as an example of proper inputs, and then~\S~\ref{subsec:distribution}
we prove distribution over Monoid Concatenation on Refined Strings.

\subsection{Refined Strings}\label{subsec:rstring}
We represent the input as a Refined String
defined at a library @RString.hs@
with implementation irrelevant for the proofs.
%
\NV{introduce RString here is good, but implementation and constact time indexing seems irrelevant here}
\begin{code}
data RString
\end{code}
%
@RString.hs@
defines the monoid operators @stringMempty@ and @(stringMappend)@
that satisfy the monoid laws.
%
Moreover, it defines string manipulation operators
that come refined via various theorems.
%
For example, the library exposes the
operators @takeStr@ and @dropStr@
\begin{code}
takeStr :: i:Nat -> xs:{RString | i <= stringLen xs }
  -> {v:RString | stringLen v == i }
dropStr :: i:Nat -> xs:{RString | i <= stringLen xs }
  -> {v:RString | stringLen v == stringLen xs - i}
\end{code}
%
and asserts that appending a list split via @take@ and @drop@
exactly reconstructs the list.
\begin{code}
concatTakeDrop
  :: i:Nat -> xs:{RString | i <= stringLen xs}
  -> {xs == takeStr i xs stringMappend dropStr i xs }
\end{code}
%
The operators defined in @RString@ are already reflected in the logic.
%
Thus we use them to further define operators
that we reflect and use in the theorems.
%
For example, @takeStr@ and @dropStr@ are used to define
@chunkStr@
\begin{code}
chunkStr i xs
  | i <= 1 ||stringLen xs <= i
  = C xs N
  | otherwise
  = C (takeStr i xs) (chunkString i (dropStr i xs))
\end{code}
%
We reflect @chunkStr@ into logic and use it to define and prove
distribution over monoid concatenation.

\subsection{Distribution over Monoid Concatenation on Refined Strings}\label{subsec:distribution}
%
We use the refinement type specification for @monoidDirstibution@
to specify and prove  that for each monoid @m@,
if the function @f:RString -> m@ distributes over @RString@
then it distributes over monoids.
%
Note that the assumption of the theorem,
is expressed in the specifications as a functional argument
that makes the proper assumptions for @f@.
%
To prove distribution over monoids for a specific @f@,
we need to invoke @monoidDirstibution@
on @f@ and a proper proof argument that @f@ distributes over @RString@.
%
\begin{theorem}[Monoid Distribution]\label{theorem:monoid:distribution}
If @m@ is a monoid, then each function @f:RString -> m@
that distributes over @RString@, distributes over monoid concatenation.
%
\begin{code}
monoidDirstibution
  :: f:(RString -> m)
  -> (x1:_ -> x2:_ -> {f (x1 stringMappend x2) == (f x1) <> (f x2)} )
  -> is:RString
  -> n:Int
  -> {f is == mconcat (map f (chunkStr n is))}
\end{code}
\end{theorem}

\begin{proof}
The proof proceeds by induction on the length of the input.
%
\begin{code}
monoidDirstibution f thm is n
  | stringLen is <= n || n <= 1
  =   mconcat (map f (chunkStr n is))
  ==. mconcat (map f (C is N))
  ==. mconcat (f is `C` map f N)
  ==. f is <> mconcat N
  ==. f is <> mempty
  ==. f is ? idRight (f is)
  *** QED
monoidDirstibutionf f thm is n
  =   mconcat (map f (chunkStr n is))
  ==. mconcat (map f (C takeIs (chunkStr n dropIs)))
  ==. mconcat (f takeIs `C` map f (chunkStr n dropIs))
  ==. f takeIs <> f dropIs
       ? monoidDirstibution f thm dropIs n
  ==. f (takeIs <+> dropIs)
       ? thm takeIs dropIs
  ==. f is
       ? concatTakeDrop n is
  *** QED
  where
    dropIs = dropStr n is
    takeIs = takeStr n is
\end{code}
%
In the base case we use rewriting and right identity on the monoid @f is@.
%
In the inductive case,
we use the inductive hypothesis on the input @dropIs = dropStr n is@,
that is provably smaller than @is@ as @n > 1@.
%
Then, by our assumption argument @thm takeIs dropIs@
we get basic distribution of @f@, that is
@f takeIs <> f dropIs == f (takeIs stringMappend dropIs)@.
%
Finally, we merge @takeIs stringMappend dropIs@ to @is@
using the property @concatTakeDrop@ take is exported by the @RString@
library.
\qed\end{proof}


\chapter{Theory}\label{cha:theory}
This chapter presents the literature used in order to answer the research questions.
It starts off with defining line voxelization and the three algorithms used.
Then the optimal scanline is presented together with other voxelization techniques.
Finally, some error metrics are presented, one of which is used as part of the final results.

\section{Line Voxelization}
\textit{Line voxelization} is a way of generating voxels based on a 3D line.
This is needed for the optimal scanline method to work, as this is how the scanlines are generated.
In this thesis, line voxelization defines all voxels in a grid being touched by a line.
This is similar to how lines are drawn to a screen in 2D.
An example of line voxelization can be seen in \figref{fig:line-voxelization}.
To determine which voxels are touching the line, three algorithms are introduced in the upcoming sections.

\input{fig/line-voxelization.fig}

\newpage

\subsection{Real Line Voxelization}
\call{voxeltraversal} proposed a method of ray marching voxels in a uniform grid.
This can also be used to voxelize lines and will further be called \textit{Real Line Voxelization} (RLV).
The basis of the algorithm is the line equation
$$p = p_0 + vt,$$
where $p$ is a position on the line, $p_0$ is the start position of the line, $v$ is the direction of the line (normalized) and $t$ is how much the direction is scaled.

When the algorithm starts, it initializes $t$ as
$$t_x = \frac{p_{1,x} - p_{0,x}}{v_x},$$
where $p_{1,x}$ is the x-position of the next voxel in the x-axis, as seen in \figref{fig:next-voxel}.
This is calculated similarly for each component of $t$.
Each iteration of the algorithm evaluates $t_{min}$ as $min(t_x,t_y,t_z)$.
Let's assume $t_{min} = t_x$.
This means the next voxel is in the x-direction.
Again, from \figref{fig:next-voxel}, $p_{1,x}$ is closer to $p_0$ compared to $p_{1,y}$ and therefore has a smaller $t$ value.
This means the next voxel would be placed to the right of $p_0$.
The $t$ for the next iteration is calculated by subtracting all of its components by $t_{min}$.

Finally assign
$$t_x = \frac{|p_{i,x} - p_{i-1,x}|}{v_x} = \frac{1}{v_x},$$
where $p_{i,x}$ is the x-position of the $i$'th voxel in the x-direction.
Here the grid is assumed to have a voxel size of 1, meaning $|p_{i,x} - p_{i-1,x}| = 1$.
After this the iteration is restarted.
The iteration is then terminated when the sum of all $t_{min}$ exceeds the length of the line.

\input{fig/next-voxel.fig}

\newpage

\subsection{Integer Line Voxelization}
\textit{Integer Line Voxelization} (ILV) follows the same structure as RLV except it avoids the floating-point arithmetics and divisions.
The changes needed to avoid floating-points are adapted from \call{scanline-voxelization}.

Having RLV as a basis, ILV requires three changes.
Firstly, the initial $t$ is calculated from the center of the start voxel, which means
$$p_{1,x} - p_{0,x} = \frac{1}{2},$$
and therefore
\begin{equation*}
\left\{
\begin{aligned}
    t_x &= \frac{1}{2 \Delta X} \\
    t_y &= \frac{1}{2 \Delta Y} \\
    t_z &= \frac{1}{2 \Delta Z}
\end{aligned}
\right.
,
\end{equation*}
where $\Delta X$ denotes the length of the line in the x-axis.
$\Delta Y$ and $\Delta Z$ are defined similarly.

Secondly, since only the relative sizes of $t$'s components are needed, the equation can therefore be multiplied with
$$2 \Delta X\Delta Y\Delta Z,$$
to avoid fractions.
As such, the integer version of $t_x$ is denoted $T_x$ and can be described as
\begin{equation}\label{eq:Tx}
  T_x = \Delta Y\Delta Z.
\end{equation}
$T_y$ and $T_z$ are described similarly.

Lastly, the iteration of the algorithm follows RLV, replacing $t$ with $T$, but the assignment to $t_x$ in the end is instead
$$T_x = 2 \Delta Y\Delta Z,$$
if $T_{min} = T_x$.
This follows \equref{eq:Tx}, but multiplied with 2.
The reason is that before $T$ was calculated to traverse half a voxel, multiplying it by 2 then traverses a whole voxel.

\subsection{3D Bresenham Algorithm}
The 3D version of the \textit{Bresenham} algorithm presented by \call{3d-bresenham} follows the original by \call{bresenham} rather nicely.
The 3D algorithm starts off with a few assumptions about the line,
$\Delta X \ge \Delta Y \ge 0$ and $\Delta X \ge \Delta Z \ge 0$, these are defined the same as in the previous section.

In the original Bresenham an error in the y-axis is initialized to
$$e_y = 2 \Delta Y - \Delta X.$$
Then for each iteration, if the current $x$ is greater than $X_1$, terminate the iteration.
Otherwise set the current voxel and increment $x$ by one.
Following that, check if the error of the line is greater than 0.
If it is, increase $y$ by one and decrease the error, otherwise increase the error.
Then restart the loop.
The change in the error is defined as

\begin{equation*}
\left\{
\begin{aligned}
  e'_y &= e_y + 2 (\Delta Y - \Delta X) &, e_y \ge 0\\
  e'_y &= e_y + 2 \Delta Y &, e_y < 0 \\
\end{aligned}
\right.
.
\end{equation*}

To extend the algorithm to 3D, \citeauthor{3d-bresenham} added another error, $e_z$, which kept track of when $z$ should increase.
This works the same way as $e_y$ and is evaluated after $e_y$.
A pseudo code of the algorithm can be found in \appref{app:3dbresen}.

\section{Model Voxelization}
As mentioned in the introduction, voxelization is a way of turning a 3D model or scene into voxel data.
This can be done in a wide range of ways and is still being researched today.
Four such techniques, two of which are based on the optimal scanline, are presented in the following sections.

\subsection{Floating-Point Optimal Scanline}\label{sss:vox_optscan}
One of the more recent works in voxelization include work done by \call{scanline-voxelization}.
This algorithm, called optimal scanline, uses line voxelization as its core concept.
All its calculations are done for each triangle of the model.
The algorithm first sorts the vertices of a triangle based on its most dominant axis.
This axis is defined as the x-, y- or z-axis which most aligns with the triangle's normal.
It can be determined by choosing the axis with the greatest absolute value of the normal's components.
That is, choose the axis which matches $max(|n_x|, |n_y|, |n_z|)$, where $n$ is the normal of the triangle.
The dominant axis will further be assumed to be the z-axis.

With the vertices of the triangle sorted, it performs line voxelizations between each of the vertices using RLV.
This results in all the edges of the triangle being voxelized.

In order to fill the interior of the triangle, it splits the edges into slices in the z-axis.
Where each edge has the same integer z-value.
Then, for each slice, perform 2D line voxelization between the edges of the triangle.
This is shown in \figref{fig:real-optimal-scanline}.
The figure also shows that not every edge voxel needs to contain a scanline endpoint.
That is where the optimal keyword of the algorithm comes in.
The authors propose a theorem stating that, there exists a distance, $l$, where there cannot exist a voxel between two parallel lines.
This distance can be seen in \figref{fig:scanline-distance}.
Intuitively, the distance can be seen to be the length of the voxel's diagonal projected onto the scanline direction, $d_{sl}$.
Furthermore, the length, $l$, can be calculated as $|d_{sl,x}| + |d_{sl,y}|$.
To prove this the scanline can be classified into four cases, all the combination of the signs of $d_{sl}$. The result is the four following equations:
\begin{equation*}
\left\{
\begin{aligned}
  l &= ((0,1) - (1,0)) \cdot d_{sl}\hspace{1cm},d_{sl,x} < 0 < d_{sl,y}\\
  l &= ((1,0) - (0,1)) \cdot d_{sl}\hspace{1cm},d_{sl,y} < 0 < d_{sl,x}\\
  l &= ((0,0) - (1,1)) \cdot d_{sl}\hspace{1cm},d_{sl,x}, d_{sl,y} < 0\\
  l &= ((1,1) - (0,0)) \cdot d_{sl}\hspace{1cm},0 < d_{sl,x}, d_{sl,y}\\
\end{aligned}
\right.
\end{equation*}
The values $(0,0)$, $(1,1)$, $(1,0)$ and $(0,1)$ are the corners of the voxel.
They are used to calculate the diagonal that aligns with $d_{sl}$.
Calculating the dot product of each of the equations results in $l = |d_{sl,x}| + |d_{sl,y}|$.

\input{fig/real-optimal-scanline.fig}
\input{fig/scanline-distance.fig}

\newpage

\subsection{Integer Optimal Scanline}\label{ss:integer-optimal-scanline}
The way integer voxelization of the optimal scanline is handled is a bit different from the floating-point one.
Since the scanline direction and the scanline length are both floating-points, these cannot be used.
Instead, it makes use of an iterative approach, where it steps through the edge voxels of the triangle until the voxel is too far away from the previous scanline.
How exactly it was derived can be found in~\cite{scanline-voxelization}, but the final theory is presented below (assuming z is the most dominant axis).

To start off, two boundary variables are defined, called $C_{lower}$ and $C_{upper}$.
These are defined as
\begin{equation*}
  \left\{
    \begin{aligned}
      C_{lower} &= \Delta Y X^a - \Delta X Y^a - |\Delta X| - |\Delta Y|\\
      C_{upper} &= \Delta Y X^a - \Delta X Y^a + |\Delta X| + |\Delta Y|
    \end{aligned}
  \right.,
\end{equation*}
where ($X^a$, $Y^a$) and ($X^b$, $Y^b$) are the endpoints of the previous scanline and $\Delta X = X^b - X^a$, $\Delta Y = Y^b - Y^a$.
Each iteration, the algorithm steps the edge voxelization by one voxel and calculate a variable called $C_k$ to be
$$ C_k = \Delta Y X_k - \Delta X Y_k, $$
where ($X_k$, $Y_k$) is the k'th voxel on the edge, with k=0 being the previous scanline endpoint.
If this variable is outside the boundaries, $C_{lower}$ and $C_{upper}$, the voxel is too far away from the previous scanline and $(X_{k-1},Y_{k-1})$ is chosen as the next scanline endpoint.
This iteration is performed separately for both the edges.
As a triangle has three edges and not two, the voxels on the edge from $v_1$ to $v_2$ are merged with the voxels on the edge from $v_2$ to $v_3$.
Those two edges are therefore counted as a single edge.

\subsection{Rasterization}
Another technique used for surface voxelization utilizes the GPU rasterizer in order to optimize the voxel generation.
One such technique was presented by \call{octree-voxelization}.
The basic idea is that, for every triangle in the model, find the triangle's most dominant axis and render it from that direction.
In practice, this means swapping the dominant axis with the z-axis in a vertex shader.
The triangle is then sent to the rasterizer which outputs fragments of the triangle.
The voxel coordinate is then simply the position of the fragment and its depth value.
Then the axes are swapped back from before in order to place it correctly in the scene.
Finally it writes the coordinate to a 3D texture which stores the voxel data.
Note that all rasterization is done in a framebuffer with the same width and height as the voxel grid resolution.
There are however problems with the algorithm creating holes in the voxelization in some instances.
It can be resolved by using a technique called conservative rasterization.
This involves marking all fragments which are touched by the triangle.

% \subsection{Triangle-Box Intersection}
% Triangle-box intersection is another method of voxelization that was proposed by \call{SAT-voxelization}.
% The main idea of the method is, as its name implies, performing intersection tests between the voxels and the triangles.
% In order to minimize the amount of tests, a few optimizations are done.
% The first of which is classifying the type of triangle it is, be it a 1D line, a 2D triangle or a 3D triangle.
% These are further classified to their respective dominant axis.
% All the triangles of the model are then sorted so that they are grouped in their classes.
% This makes it so that most of the GPU threads are performing the same tasks without branch divergence.
% Another optimization is that a triangle has at most three voxel thickness in the most dominant axis.
% This could reduce the amount of tests needed significantly.

\newpage

\subsection{Depth Buffer}
One way of performing solid voxelization is to make use of the depth buffer \cite{depth-buffer}.
This is done by rendering the entire model from six different directions, positive x, y and z, and negative x, y and z.
In these rendering steps, only the depth buffer is needed.
Then the voxelization is defined as all voxels which are within all the depth buffers.

One way to do this, is to iterate through the entire grid and marking all voxels which are within the buffers as occupied.
This is however rather slow for higher resolutions, as the complexity grows cubically.
Another way would be to choose one axis, let's say the z-axis, and iterate through its buffer x- and y-coordinates.
Then for each coordinate, there is a minimum and maximum z-position which is determined by the two depth buffers in the z-axis.
This means the algorithm only needs to iterate through these two values instead of all the z-values.
As most models do not fill the entire voxel grid, this reduces the runtime of the algorithm in the average case.

One obvious problem of the algorithm is that it needs to render the object six times to voxelize it.
For convex shapes, this can be reduced to two, as the shape can be fully described by rendering it from the front and back.

\section{Error Analysis}\label{s:error}
Calculating the approximation errors can be done using several methods.
The choice of method depends on the use case.
This thesis presents two different methods to analyze the error.
These are relative error, the one used in~\cite{scanline-voxelization}, and the Jaccard distance.

\subsection{Relative Error}
The \textit{relative error} can be described as
\begin{equation*}
  e_{re} = \frac{|x - y|}{|x|} = \Big|\frac{x - y}{x}\Big|,
\end{equation*}
where $x$ is the actual value and $y$ is the approximation.
This was the method used by \call{scanline-voxelization}, where they set $x$ to the total voxel count of the floating-point version and $y$ to the total voxel count of the integer version.
A problem with this is that if a voxel is moved to another location, the error would be considered 0.
This can be seen in \figref{fig:relative-error}, where the relative error of the voxel count is 0.
Therefore, a different error metric is required in order to describe this error.

\input{fig/relative-error.fig}

\newpage

\subsection{Jaccard Distance}\label{ss:mae}
The \textit{Jaccard distance} is an error metric which aims to measure differences in sets.
It is derived from  the Jaccard similarity presented in \cite{jaccard}, where it instead measures how similar two sets are.
The Jaccard similarity is defined as the size of the intersection divided by the size of the union of the two sets.
This results in a value between zero and one, which can be interpreted as a percentage of how similar two sets are.
The Jaccard distance is defined as one subtracted by the Jaccard similarity.
It can also be defined as the symmetric difference divided by the union of the two sets.
Both of which are equivalent.


% Copyright 2017-2018 Markus J. Pflaum, licensed under CC BY-NC-ND 4.0
% main author:
%   Markus J. Pflaum
%
\section{Summability}
\label{sec:summability}
\begin{definition}
Assume to be given a locally convex topological vector space $\tvsV$ over the field $\fldK$ of real or complex numbers.
Let $(v_i)_{i \in I}$ be a family of elements of $\tvsV$. Let $\mathscr{F} (I)$ be the
set of finite subsets of $I$ and note that it is filtered by set-theoretic inclusion.
The family  $(v_i)_{i \in I}$ then gives rise to the net
$\Big( \sum_{i \in J} v_i \Big)_{J \in \mathscr{F}(I)}$. One calls the family  $(v_i)_{i \in I}$
\emph{summable} to an element $v \in \tvsV$ if the net
$\Big( \sum_{i \in J} v_i \Big)_{J \in \mathscr{F}(I)}$ converges to $v$. In other words this means that
for every convex zero neighborhood $U \subset \tvsV$ and $\varepsilon >0$ there exists an element
$J_{U,\varepsilon} \in \mathscr{F} (I)$ such that for all finite sets $J$ with $ J_{U,\varepsilon} \subset J \subset I$
\[
   p_U \left( v - \sum_{i \in J} v_i \right) < \varepsilon \ .
\]
As before, $p_U$ denotes here the gauge of $U$.
If $\tvsV$ is Hausdorff, the limit $v$ of a summable family  $(v_i)_{i \in I}$ is
uniquely determined, and one  writes in this situation
\[
   v = \sum_{i \in I} v_i \ .
\]
We denote the space of summable families in $\tvsV$ over the given index set $I$ by
$\ell^1 (I ,\tvsV)$. For $E=\C$ we just write $\ell^1 (I )$ instead of $\ell^1 (I ,\C)$. If in addition the index set
coincides with $\N$, we briefly denote $\ell^1 (\N )$ by $\ell^1$.
\end{definition}

\begin{proposition}[Cauchy criterion for summability]
  Let $\tvsV$ be a complete locally convex topological vector space.
  A family $(v_i)_{i \in I}$ of elements of $\tvsV$ then is summable
  to some $v\in \tvsV$ if and only if it satisfies the following Cauchy condition:
  \begin{axiomlist}[\hspace{1pt}]
  \item[\textup{\sffamily (C)}]
    For every convex zero neighborhood $U \subset \tvsV$ and $\varepsilon >0$ there exists an element
    $J_{U,\varepsilon} \in \mathscr{F} (I)$ such that for all $K \in \mathscr{F} (I)$ with  $ K \cap J_{U,\varepsilon} = \emptyset$
    the relation
    \[
      p_U \left( \sum_{i \in K} v_i \right) < \varepsilon
    \]
    holds true.
  \end{axiomlist}

\end{proposition}
\begin{proof}
  By completeness of $\tvsV$ it suffices to verify that the net $\Big( \sum_{i \in J} v_i \Big)_{J \in \mathscr{F}(I)}$ is a Cauchy net
  if and only if condition \textup{\sffamily (C)} is satisfied. Recall that one calls
  $\Big( \sum_{i \in J} v_i \Big)_{J \in \mathscr{F}(I)}$ a Cauchy net if for every convex zero neighborhood $U \subset \tvsV$
  all $\varepsilon >0$ there exists an element $J_{U,\varepsilon} \in \mathscr{F} (I)$ such that for all $J,J' \in \mathscr{F} (I)$
  containing  $ J_{U,\varepsilon}$ as a subset the relation
  \[
     p_U \left( \sum_{i \in J} v_i - \sum_{i\in J'} v_i \right) < \varepsilon
  \]
  holds true.
  But that is clearly equivalent to condition \textup{\sffamily (C)}.
\end{proof}

\para Several other notions of summability have been introduced in the analysis and functional analysis literature.
These are mainly either used to establish summability criteria or are used in the study of
topological tensor products and nuclearity of locally convex topological vector spaces, see \cite{GroPTTEN,PieNLCS}.
In the following we define these further notions of summability and study their properties.
The symbol $\tvsV$  hereby always stands for a locally convex \tvs, $I$ always denotes a nonempty
index set, and $\mathscr{F} (I)$ the set of its finite subsets.

\begin{definition}
  A family   $(v_i)_{i \in I}$ in $\tvsV$ is called \emph{weakly summable} to $v \in \tvsV$ if for
  every continuous linear form $\alpha : \tvsV \to \fldK$ the net
  $\Big( \sum_{i \in J} \alpha( v_i ) \Big)_{J \in \mathscr{F}(I)}$ converges in $\fldK$ to $\alpha ( v )$.
  In other words this means that for every $\alpha \in \tvsV'$ and $\varepsilon >0$ there exists a finite set
  $J_{\alpha,\varepsilon} \subset I$ such that for all finite sets $J$ with $J_{\alpha,\varepsilon} \subset  J \subset I $
  \[
    \left| \alpha (v) -  \sum_{j\in J} \alpha( v_i )   \right|< \varepsilon \ .
  \]
  The set of all weakly summable families in $\tvsV$ with index set $I$ is denoted $\ell^1 [I, \tvsV]$.
\end{definition}

\begin{definition}
  A family  $(v_i)_{i \in I}$ in $\tvsV$ is called \emph{absolutely summable} if for every circled convex
  zero neighborhood $U \subset \tvsV$ there exists some $C\geq 0$ such that
  \[
    \sum_{i \in J} p_U \left( v_i\right) \leq C \quad \text{for all } J \in \mathscr{F} (I) \ .
  \]
  We denote the set of all absolutely summable families  in $\tvsV$ by  $\ell^1 \{ I, \tvsV \}$.
\end{definition}

\begin{proposition}
  A family $(v_i)_{i \in I} \subset \tvsV$ is absolutely summable if and only if for every element $U$
  of a basis of circled convex zero neighborhoods there exists a $C\geq 0$ such that
  \[
    \sum_{i \in J} p_U \left( v_i\right) \leq C \quad \text{for all } J \in \mathscr{F} (I) \ .
  \]
\end{proposition}

\begin{proof}

\end{proof}

\begin{definition}
 A family $(v_i)_{i \in I}$ in $\tvsV$ is called \emph{totally summable} if there exists a bounded
 absolutely convex subset $B\subset \tvsV$  and a $C\geq 0$ such that
 \[
   \sum_{i \in J} p_B \left( v_i\right) \leq C  \quad \text{for all } J \in \mathscr{F} (I) \ .
 \]
 We write $\ell^1 \langle I, \tvsV \rangle$ for the set of all totally summable families in $\tvsV$.
\end{definition}

\subsec{Summable families of complex numbers}

\begin{lemma}[cf.~{\cite[Lem.~1.1.2]{PieNLCS}}]
  Let $(z_i)_{i\in I}$ be a family of  complex numbers for which there exists a positive real number $C > 0$
  such that
  \[
     \left|\sum_{i\in J} z_i \right| \leq C \quad \text{for all } J \in \mathscr{F} (I) \ .
  \]
  Then one has the estimate
   \[
     \sum_{i\in J}\left| z_i \right| \leq 4 C \quad \text{for all } J \in \mathscr{F} (I) \ .
  \]
\end{lemma}

\begin{proof}
  We assume first that all $z_i$ are real. Then let $I^+$ the set of all indices $i\in I$ such that $z_i \geq 0$,
  and $I^-$ the set of all  $i\in I$ such that $z_i < 0$. Then, for all finite $J\subset I$
  \[
     \sum_{i\in J} \left| z_i \right| =  \sum_{i\in J\cap I^+}  \left| z_i  \right| +  \sum_{i\in J\cap I^-}  \left| z_i  \right|
    =  \left| \sum_{i\in J\cap I^+}  z_i  \right| +  \left| \sum_{i\in J\cap I^-}  z_i  \right| \leq 2  C \ .
  \]
  In the general case decompose $z_i$ into real and imaginary parts $x_i = \Re z_i$ and   $y_i = \Im z_i$.
  By the triangle inequality one obtains for all finite $J\subset I$
  \[
    \sum_{i\in J} \left| z_i \right| \leq \sum_{i\in J} \left| x_i \right| + \sum_{i\in J} \left| y_i \right| \leq 4 C \ .
  \]
\end{proof}

\begin{proposition}
\label{thm:summability-criteria-family-complex-numbers}
  For a  family $(z_i)_{i\in I}$ of complex numbers the following are equivalent.
  \begin{romanlist}
  \item The family $(z_i)_{i\in I}$ is summable.
  \item The family $(\left| z_i \right| )_{i\in I}$ is summable.
  \item The family $(z_i)_{i\in I}$ is absolutely summable.

  \item There exists some $C > 0$ such that
        $\sum_{i\in J}\left| z_i \right| \leq C$ for all $J\in \mathscr{F} (I)$.
  \end{romanlist}
  In case that  one hence all of the conditions are fulfilled, the estimate
  \[
     \left|  \sum_{i\in I} z_i \right|  \leq \sum_{i\in I}\left| z_i \right|
  \]
  holds true.
\end{proposition}

\begin{proof}
  Assume that  $(z_i)_{i\in I}$ is absolutely summable. Since $\C$ is normed with norm given by the absolut value
  this just means that there exists some $C > 0$ such that
  $\sum_{i\in J}\left| z_i \right| \leq C$ for all $J\in \mathscr{F} (I)$. Hence the supremum
  $c = \sup \left\{ \sum_{i\in J}\left| z_i \right| \mid J \in \mathscr{F} (I)\right\}$ exists and is $\leq C$.
  For given $\varepsilon >0$ choose $J_\varepsilon \in \mathscr{F} (I)$ such that
  \[
        c- \varepsilon \leq \sum_{i\in J_\varepsilon}\left| z_i \right| \leq c \ .
  \]
  Then one has for all $K\in \mathscr{F} (I)$ with $K\cap J_\varepsilon = \emptyset$
  \[
     \left| \sum_{i\in K} z_i \right| \leq  \sum_{i\in K} \left| z_i \right| \leq \varepsilon \ .
  \]
  Hence $\left( \sum_{i\in J} z_i \right)_{J \in \mathscr{F} (I)}$ is a Cauchy net, so has to
  converges  by completeness of $\C$. This proves summability of   $(z_i)_{i\in I}$.

  Vice versa, assume now that   $(z_i)_{i\in I}$ is summable. Then
  $\left( \sum_{i\in J} z_i \right)_{J \in \mathscr{F} (I)}$ is a Cauchy net. Hence there exists an element
  $J_1 \in  \mathscr{F} (I)$ such that for all $K\in  \mathscr{F} (I)$ with $K \cap J_1 = \emptyset$
  the inequality
  \[
      \left| \sum_{i\in K} z_i \right| < 1
  \]
  holds true. Let $C = \sum_{i\in J_1} \left| z_i\right|$. Then one has for all $J\in  \mathscr{F} (I)$
  \[
       \left| \sum_{i\in J} z_i \right| \leq  \left| \sum_{i\in J\setminus J_1} z_i \right| +
       \left| \sum_{i\in J\cap J_1} z_i \right| \leq 1 + C \ .
  \]
  By the preceding lemma the set of partial sums $ \sum_{i\in J} \left| z_i \right|$, where $J$ runs through the
  finite subsets of $I$, is then bounded by $4 + 4C$, hence  $(z_i)_{i\in I}$ is absolutely summable.
\end{proof}

\subsec{Summability in Banach spaces}

\begin{proposition}\label{thm:summability-criteria-family--normed-vector-space}
  Let $\banachV$ be a normed vector space.
  For a  family $(v_i)_{i\in I}$ of elements in $\tvsV$ the following are equivalent:
  \begin{romanlist}
  \item\label{ite:absolute-summable-family-normed-vector-space} The family $(v_i)_{i\in I}$ is absolutely summable.
  \item\label{ite:norm-summable-family-normed-vector-space} The family $(\left\| v_i \right\| )_{i\in I}$ is summable.
  \item\label{ite:cauchy-summable-family-normed-vector-space} There exists some $C > 0$ such that
        $\sum_{i\in J}\left\| v_i \right\| \leq C$ for all $J\in \mathscr{F} (I)$.
  \end{romanlist}
  If $\banachV$ is even a Banach space, these conditions are all equivalent to
  \begin{romanlist}
  \setcounter{enumi}{3}
  \item The family $(v_i)_{i\in I}$ is summable.
  \end{romanlist}
\end{proposition}
\begin{proof}
  \ref{ite:norm-summable-family-normed-vector-space} and \ref{ite:cauchy-summable-family-normed-vector-space}
  are equivalent by \Cref{thm:summability-criteria-family-complex-numbers}
  Assume now that \ref{ite:absolute-summable-family-normed-vector-space} holds true.
\end{proof}

\textbf{to do:} Carl Neumann series

\subsec{Properties of and relations between the various summability types}

\begin{theorem}
  Let $I$ be a non-empty index set. Then the spaces $\ell^1 (I, \tvsV )$ of summable families,
  $\ell^1 [I, \tvsV ]$ of weakly summable families, $\ell^1 \{I, \tvsV \}$ of absolutely summable families and
  $\ell^1 \langle I, \tvsV \rangle$ of totally summable families in $E$ are all subvector spaces of
  the product vector space $E^I = \Pi_{i\in I} E$.   Furthermore one has the following chain of inclusions:
  \[
     \ell^1 \langle I, \tvsV \rangle\subset\ell^1 \{I, \tvsV \}   \quad\text{and} \quad \ell^1 (I, \tvsV) \subset \ell^1 [I, \tvsV ] \ .
  \]
  If $E$ is complete, then one even has
   \[
        \ell^1 \{I, \tvsV \}   \subset \ell^1 (I, \tvsV)
   \]
\end{theorem}

\begin{proof}
    Now let $(v_i)$ be a summable family and $\alpha : \tvsV \to \fldK$ a continuous linear form.

 Let $U$ be an absolutely convex zero neighborhood. Then $U$ absorbes $B$, so there exists $r>0$ such that
 $B \subset rU$. Hence
\end{proof}


\section{Correctness of Parallel String Matching}\label{sec:stringmatching}

In \S~\ref{sec:parallelization} we showed that any monoid morphism
whose domain is chunkable can be parallelized. We now apply that
result to parallelize string matching. We start by observing that
strings are a chunkable monoid. %\NV add monoid methods
We then turn string matching for a
given target into a monoid morphism from a string to a suitable
monoid, @SM target@, defined in
\S~\ref{subsec:stringmatcher}.
Finally, in \S~\ref{subsec:parallel-string-matching}, we parallelize string matching
by a simple use of the
parallel morphism function of \S~\ref{subsec:both-levels}.

\ignore{
  In this section we apply the Correctness of Parallelization Theorem~\ref{theorem:two-level}
to a string matching function @toSM@
that is a morphism between strings and the indices where a target string appears,
to get correctness of parallelization of @toSM@.

We define @toSM :: RString -> SM target@
from a Refined String data type @RString@
to a dependently typed string maching data type @SM target@
where @target@ represents the substring to be matched.
%
To apply Theorem~\ref{theorem:two-level} on @toSM@ we need to discharge three proof obligations.
%
\begin{itemize}
\item @RString@ is a chunkable monoid (\S~\ref{subsec:refinedstrings}),
\item @SM target@ is a monoid (\S~\ref{subsec:stringmatcher}), and
\item @toSM@ is a morphism between @RString@ and @SM target@ (\S~\ref{subsec:smmorphism}).
\end{itemize}
%
With these proof obligations discharged we conclude (\S~\ref{subsec:parallel-string-matching})
correctness of parallel string matching.
}

\subsection{Refined Strings are Chunkable Monoids}\label{subsec:refinedstrings}
\input{text/stringmatcher/rstring-is-monoid}

\subsection{String Matching Monoid}\label{subsec:stringmatcher}
\input{text/stringmatcher/string-matcher-is-monoid}

\subsection{String Matching Monoid Morphism}\label{subsec:smmorphism}
\input{text/stringmatcher/string-matching-morphism}

\subsection{Parallel String Matching}\label{subsec:parallel-string-matching}
\input{text/stringmatcher/all-together}


%\NeedsTeXFormat{LaTeX2e}[1995/06/01]
\input{preamble}

\newcommand{\version}{Draft: 18 June 2017}

\lhead{\scriptsize Barnes \& Soule}
\chead{\scriptsize}
\rhead{\scriptsize \thepage}

\lfoot{\scriptsize Nagadan}
\cfoot{\scriptsize}
\rfoot{\scriptsize \version}

\usepackage{color}
\definecolor{red}{RGB}{255,0,0}

\newcommand{\phit}{\phi_t}
\newcommand{\phis}{\phi_s}

%******************************************************************************
\begin{document}
%******************************************************************************

%==============================================================================
% Title block information
%==============================================================================
\title{Nagadan: Identification Influential Data using a Quadratic Discharge Potential Model}
\author{
Dr. Randal J. Barnes\\
Department of Civil, Environmental, and Geo- Engineering\\
University of Minnesota
\and
Richard Soule\\
Source Water Protection\\
Minnesota Department of Health\\
}
\date{\version}
\maketitle
\thispagestyle{plain}

%==============================================================================
\section{Introduction}
%==============================================================================

%==============================================================================
\section{Geohydrologic Descriptors}
%==============================================================================
The underlying model for the regional flow is given by a quadratic function.
%
\begin{equation}\label{2.1}
    \Phi(x,y) = A x^2 + B y^2 + C xy + Dx + Ey + F
\end{equation}
%
The six model parameters, $\left\{ A, B, C, D, E, F \right\}$, are organized into a single column vector and modeled as stochastic variables.
%
\begin{equation}\label{2.2}
    \mat{P} = \begin{bmatrix} A \\ B \\ C \\ D \\ E \\ F \end{bmatrix}
\end{equation}
%

\newpage
%------------------------------------------------
\subsection{Regional recharge}
%------------------------------------------------
The regional recharge is given by
%
\begin{equation}\label{2.3}
    N = -2 \left( A + B \right)
\end{equation}
%
\begin{equation}\label{2.4}
    \ev{N} = -2 \left( \ev{A} + \ev{B} \right)
\end{equation}
%
\begin{equation}\label{2.5}
    \var{N} = 4 \left( \var{A} + \var{B} + 2\cov{A,B} \right)
\end{equation}

%------------------------------------------------
\subsection{Regional Discharge Components}
%------------------------------------------------
The regional discharge components, $Q_x$ and $Q_y$, are given by
%
\begin{align}
    Q_x &= -D    \label{2.6}\\
    Q_y &= -E    \label{2.7}
\end{align}
%
\begin{align}
    \ev{Q_x} &= -\ev{D} \label{2.8}\\
    \ev{Q_y} &= -\ev{E} \label{2.9}
\end{align}
%
\begin{align}
    \var{Q_x} &= \var{D} \label{2.10}\\
    \var{Q_y} &= \var{E} \label{2.11}\\
    \cov{Q_x,Q_y} &= \cov{D,E} \label{2.12}
\end{align}

%------------------------------------------------
\subsection{First-order second-moment analysis}
%------------------------------------------------
Let the function $g(x,y)$ be twice differentiable over the domain of interest.
%
\begin{equation} \label{8.1}
    \ev{g(X,Y)} \approx g(\mu_X,\mu_Y) + \frac{1}{2} \left[
        \ppderiv{g}{x} \, \sigma_X^2 +
        \ppderiv{g}{y} \, \sigma_Y^2 +
        2 \pqderiv{g}{x}{y} \, \sigma_{XY} \right]
\end{equation}
%
\begin{equation} \label{8.2}
    \var{g(X,Y)} \approx
        \left[ \pderiv{g}{x} \right]^2 \sigma_X^2 +
        \left[ \pderiv{g}{y} \right]^2 \sigma_Y^2 +
        2 \left[ \pderiv{g}{x} \cdot \pderiv{g}{y} \right] \sigma_{XY}
\end{equation}
%
where all of the partial derivatives are evaluated at $(\mu_X,\mu_Y)$.

\newpage
%------------------------------------------------
\subsection{Regional Discharge Magnitude}
%------------------------------------------------
The regional discharge magnitude, $T$, is given by
%
\begin{align}
    S &= Q_x^2 + Q_y^2 \label{2.13}\\
    T &= \sqrt{S} \label{2.14}
\end{align}
%
\begin{align}
    \pderiv{T}{Q_x} &= \frac{Q_x}{T} \label{2.15}\\
    \pderiv{T}{Q_y} &= \frac{Q_y}{T} \label{2.16}\\
    \ppderiv{T}{Q_x} &= \frac{Q_y^2}{T^3} \label{2.17}\\
    \ppderiv{T}{Q_y} &= \frac{Q_x^2}{T^3} \label{2.18}\\
    \pqderiv{T}{Q_x}{Q_y} &= - \frac{Q_x Q_y}{T^3} \label{2.19}
\end{align}

\begin{equation}\label{2.20}
    \ev{T} \approx T + \frac{1}{2} \left[
        \ppderiv{T}{Q_x} \, \var{Q_x} +
        \ppderiv{T}{Q_y} \, \var{Q_y} +
        2 \pqderiv{T}{Q_x}{Q_y} \, \cov{Q_x,Q_y} \right]
\end{equation}
%
\begin{equation}\label{2.21}
    \var{T} \approx
        \left[ \pderiv{T}{Q_x} \right]^2 \var{Q_x} +
        \left[ \pderiv{T}{Q_y} \right]^2 \var{Q_y} +
        2 \left[ \pderiv{T}{Q_x} \cdot \pderiv{T}{Q_y} \right] \cov{Q_x,Q_y}
\end{equation}

%------------------------------------------------
\subsection{Regional Discharge Direction}
%------------------------------------------------
The regional discharge magnitude, $U$, is given by
%
\begin{align}
    S &= Q_x^2 + Q_y^2 \label{2.22}\\
    U &= \text{atan2}\left(Q_y, Q_x\right) \label{2.23}
\end{align}
%
\begin{align}
    \pderiv{U}{Q_x} &= -\frac{Q_y}{S} \label{2.24}\\
    \pderiv{U}{Q_y} &=  \frac{Q_x}{S} \label{2.25}\\
    \ppderiv{U}{Q_x} &=  2 \frac{Q_x Q_y}{S^2} \label{2.26}\\
    \ppderiv{U}{Q_y} &= -2 \frac{Q_x Q_y}{S^2} \label{2.27}\\
    \pqderiv{U}{Q_x}{Q_y} &= \frac{Q_y^2 - Q_x^2}{S^2} \label{2.28}
\end{align}

\begin{equation}\label{2.29}
    \ev{U} \approx U + \frac{1}{2} \left[
        \ppderiv{U}{Q_x} \, \var{Q_x} +
        \ppderiv{U}{Q_y} \, \var{Q_y} +
        2 \pqderiv{U}{Q_x}{Q_y} \, \cov{Q_x,Q_y} \right]
\end{equation}
%
\begin{equation}\label{2.30}
    \var{U} \approx
        \left[ \pderiv{U}{Q_x} \right]^2 \var{Q_x} +
        \left[ \pderiv{U}{Q_y} \right]^2 \var{Q_y} +
        2 \left[ \pderiv{U}{Q_x} \cdot \pderiv{U}{Q_y} \right] \cov{Q_x,Q_y}
\end{equation}

%******************************************************************************
\end{document}
%******************************************************************************


\documentclass{article}

\usepackage{hyperref}
\usepackage{amsmath}
\usepackage{bm}
\usepackage{amssymb}
\usepackage{amsfonts}
\usepackage{amstext}
\usepackage{graphicx}

\title{\textbf{Six DOF Aircraft Simulator}}

\begin{document}

\maketitle

\section{Introduction}
The goal of this project is to implement a six degrees of freedom (DOF), nonlinear simulation for fixed-wing aircraft.
The first iteration of this project is using a linear aircraft model. The second iteration will be using a nonlinear model.

\section{Aircraft Model}
The linear longitudinal and lateral models for a conventional fixed-wing aircraft could be written as follows \cite{Nelson}.

\begin{equation} \label{Eq:Linear_Long_sys}
    \begin{bmatrix}
    \dot{u} \\
    \dot{w} \\
    \dot{q} \\
    \dot{\theta} \\
    \end{bmatrix}
    =
    \begin{bmatrix}
    X_u & X_w & 0 & -g \cos{\theta_0}\\
    Z_u & Z_w & u_0 & -g \sin{\theta_0}\\
    M_u & M_w & M_q & 0\\
    0 & 0 & 1 & 0 \\
    \end{bmatrix}
    \begin{bmatrix}
    u \\
    w \\
    q \\
    \theta \\
    \end{bmatrix}
    +
    \begin{bmatrix}
    X_{\delta_e} & X_{\delta_t} \\
    Z_{\delta_e} & 0 \\
    M_{\delta_e} & M_{\delta_t} \\
    0 & 0\\
    \end{bmatrix}
    \begin{bmatrix}
    {\delta_e} \\
    {\delta_t}
    \end{bmatrix}
\end{equation}

\begin{equation} \label{Eq:Linearized_sys}
    \begin{bmatrix}
    \dot{v} \\
    \dot{p} \\
    \dot{r} \\
    \dot{\phi} \\
    \dot{\psi} \\
    \end{bmatrix}
    =
    \begin{bmatrix}
    Y_v & Y_p & -(u_0 - Y_r) & g \cos{\theta_0} & 0\\
    \mathcal{L}_v & \mathcal{L}_p & \mathcal{L}_r & 0 & 0\\
    N_v & N_p & N_r & 0 & 0\\
    0 & 1 & 0 & 0 & 0\\
    0 & 0 & \sec{\theta_0} & 0 & 0\\
    \end{bmatrix}
    \begin{bmatrix}
    v\\
    p\\
    r\\
    \phi\\
    \psi\\
    \end{bmatrix}
    +
    \begin{bmatrix}
    0 & Y_{\delta_r} \\
    \mathcal{L}_{\delta_a} & \mathcal{L}_{\delta_r} \\
    N_{\delta_a} & N_{\delta_r} \\
    0 & 0\\
    0 & 0\\
    \end{bmatrix}
    \begin{bmatrix}
    {\delta_a} \\
    {\delta_r}
    \end{bmatrix}
\end{equation}

In this project we will consider the linear model of the aircraft "DELTA" given in \cite[PP. 561--563]{Mclean}
whose parameters are given as follows (at $U_0 = 75~ m/s$ and $\theta_0 = 2.7 ^\circ$)
\begin{equation}\label{Eq:DELTA_Params}
    \begin{split}
        m &= 300000 kg\\
        X_u &= -0.02\\
        X_w &= 0.1\\
        Z_u &= -0.23\\
        Z_w &= -0.634\\
        M_u &= -2.55*10^{-5}\\
        M_w &= -0.005\\
        M_q &= -0.61\\
        Y_v &= -0.078\\
        Y_p &= 0\\
        Y_r &= 0\\
        \mathcal{L}_v &= -0.086\\
        \mathcal{L}_p &= -1.0758\\
        \mathcal{L}_r &= 0.6334\\
        N_v &= 0.0037\\
        N_p &= -0.1121\\
        N_r &= -0.2569\\
        X_{\delta_e} &= 0.14\\
        Z_{\delta_e} &= -2.9\\
        M_{\delta_e} &= -0.64\\
        X_{\delta_t} &= 1.56\\
        M_{\delta_t} &= 0.0054\\
        Y_{\delta_r} &= 0.0065\\
        \mathcal{L}_{\delta_a} &= 0.46\\
        \mathcal{L}_{\delta_r} &= 0.1\\
        N_{\delta_a} &= 0.05\\
        N_{\delta_r} &= -0.21\\
    \end{split}
\end{equation}
where $\delta_t$ is considered to be from the trim thrust.
As such, $\delta_t$ is allowed the between $1$ and $-0.56$ \cite{Hassan2016_JAST}.

\section{Algorithm Structure}

\section{Results}
In this simulation example, the aircraft is subject to both elevator and aileron sinusoidal inputs
 with an amplitude of $5$ degrees and a frequency of $\pi$ rad/s. Figures \ref{Fig:Translational_Velocities},
 \ref{Fig:Rotational_Velocities} and \ref{Fig:Attitude} show the resulting translational velocities,
 rotational velocities and attitude angles, respectively.
\begin{figure}[h]
    \centering
    \includegraphics[width=0.9\textwidth]{sim_results_uvw.png}
    \caption{Translational velocities.}
    \label{Fig:Translational_Velocities}
\end{figure}
\begin{figure}[h]
    \centering
    \includegraphics[width=0.9\textwidth]{sim_results_pqr.png}
    \caption{Rotational velocities.}
    \label{Fig:Rotational_Velocities}
\end{figure}
\begin{figure}[h]
    \centering
    \includegraphics[width=0.9\textwidth]{sim_results_phi_theta_psi.png}
    \caption{Attitude angles.}
    \label{Fig:Attitude}
\end{figure}

\bibliographystyle{unsrt}
\bibliography{ref}
\end{document}


%\section*{Appendices}
\addcontentsline{toc}{section}{Appendices}
\renewcommand{\thesubsection}{\Alph{subsection}}
\label{sec:annexe}
Recall that $r_{x_i}$ is the distance between a BS at the origin and one given device $x_i$, $\theta$ is the SINR threshold for capture effect. $H_{x_i}$ and $\exp(G_{x_i})$ are respectively random variable accounting for Rayleigh fading and lognormal shadowing.
\begin{align}
p_{s} \left( r_{x_i} \right)
%&= \mathbb{P}\left\lbrace \frac{P_r}{I^{'}} \geq \theta \right\rbrace \nonumber\\
& =\mathbb{P}\left\lbrace \frac{H_{x_i} \exp(G_{x_i}) r_{x_i}^{-\gamma}}{\sum_{x_j \in \Phi_{m}} H_{, x_j} \exp(G_{x_j}) r_{x_j}^{-\gamma}}  \geq \theta \right\rbrace \nonumber\\
%& =\mathbb{P} \left\lbrace \frac{H_{x_i}  r_{x_i}^{-\gamma}}{\sum_{x_j \in \Phi_{m}} H_{x_j} \exp(G_{x_j}-G_{x_i}) r_{x_j}^{-\gamma}} \geq \theta \right\rbrace \nonumber
\end{align}
Let $I=\sum_{x_j \in \Phi_{m}} H_{x_j} \exp(G_{x_j}) r_{x_j}^{-\gamma}$, which is the cumulative interference suffered for device $x_i$. Thus, we have:
%Let $Z_{3}= \exp(G_{x_j}-G_{x_i})$, $H = Z_1 Z_3$. $I$ be
\begin{align}
\label{eq:def_ps}
p_{s}\left( r \right)  &= Pr \left\lbrace H_{x_i} \geq I \theta\exp(-G_{x_i})r ^{\gamma}  \right\rbrace  \nonumber\\
&=\mathbb{E}_{G_{x_i}}\left\lbrace  \mathbb{E}_{I} \left[ \exp(-\theta \exp(-g_{x_i}) r^{\gamma}  I ) \vert G_{x_i} = g_{x_i}\right]\right\rbrace   \nonumber\\
&= \mathbb{E}_{G_{x_i}} \left[ \mathcal{L}_{I}\left\lbrace \theta \exp(-G_{x_i}) r^{\gamma}\right\rbrace \right]
\end{align}
It is observed that the $p_s\left( r \right)$ is actually the expectation (w.r.t $G_{x_i}$) of a conditional Laplace Transform of cumulative interference $I$ at point $\theta \exp(-G_{x_i})  r_{x_i}^{\gamma}$.
\begin{align}
\label{eq:interferece-laplace-transform}
\mathcal{L}_{I}\left( s \right)  &= \mathbb{E}\left[ \exp(-sI)\right] \nonumber\\
&= \mathbb{E}\left[ \exp(-s\sum_{x_j \in \Phi_m} H_{x_j} \exp(G_{x_j})  r_{x_j}^{-\gamma})\right] \nonumber \\
&= \mathbb{E}_{\Phi_m} \left\lbrace \prod_{x_j\in \Phi_m} \mathbb{E}\left[\exp(-sH_{x_j} \exp(G_{x_j})  r_{x_j}^{-\gamma})\right] \right\rbrace
\end{align}

Formula (\ref{eq:interferece-laplace-transform}) is actually the Probability Generating Functional (PGFL) of a Poisson point process.
Applying Campell's theorem,
%\qsong{Here I give the wikipedia link for this theorem, in the final version, I will give a reference of one book: https://en.wikipedia.org/wiki/Campbell's_theorem_(probability)}
\begin{align}
\label{eq:laplace_trans_I}
\mathcal{L}_{I}\left( s \right) &=\exp\left\lbrace -\lambda_m \pi \mathbb{E}\left[ H^{\frac{2}{\gamma}}\right]\mathbb{E}\left[ \exp(\frac{2}{\gamma}G_{x_j})\right] \Gamma(1-\frac{2}{\gamma})s^{\frac{2}{\gamma}}  \right\rbrace
\end{align}
This result has been derived in~\cite[eq.3.20]{haenggi2009interference}.

\begin{align}
\label{eq:lognormal_moment}
\mathbb{E}\left[ \exp(\frac{2}{\gamma}G_{x_j})\right] &= \exp \left\lbrace \left( \frac{\sqrt{2}\beta\sigma}{\gamma}\right) ^2\right\rbrace
\end{align}
\begin{align}
\label{eq:expo_moment}
\mathbb{E}\left[ H^{\frac{2}{\gamma}} \right] &= \Gamma(1+\frac{2}{\gamma})
\end{align}

Substituting $(\ref{eq:expo_moment})(\ref{eq:lognormal_moment})$ into $(\ref{eq:laplace_trans_I})$, we have:
\begin{align}
\mathcal{L}_{I}\left( s \right) &= \exp\left\lbrace -p\lambda_m \pi \Gamma(1+\frac{2}{\gamma}) \Gamma(1-\frac{2}{\gamma}) \exp \left( \frac{\sqrt{2}\beta\sigma}{\gamma}\right) ^2 s^{\frac{2}{\gamma}}  \right\rbrace
\end{align}
Let $s=\theta \exp(-G) r^{\gamma}$, thus
\begin{align}
	\mathcal{L}_{I}\left\lbrace \theta \exp(-G) r^{\gamma}\right\rbrace = \exp\left\lbrace -A r^2\exp(-\frac{2}{\gamma}G)\right\rbrace ,
\end{align}
where $A=p\lambda_m\pi \Gamma(1+\frac{2}{\gamma}) \Gamma(1-\frac{2}{\gamma}) \exp \left( \frac{\sqrt{2}\beta\sigma}{\gamma}\right) ^2 \theta^{\frac{2}{\gamma}}$.

We continue to process formula $(\ref{eq:def_ps})$. It remains to calculate the expectation with respect to $G$:
\begin{align}
	\label{eq:def_ps_2}
	p_{s}(r) &= \mathbb{E}_{G}\left[ \exp(-A r^2 \exp(-\frac{2}{\gamma}G)) \right] \nonumber\\
	&=\int_{-\infty}^{+\infty} \exp(-A r^2 e^{-\frac{2}{\gamma}x})\frac{1}{\sqrt{2\pi}\sigma} \exp(-\frac{x^2}{2\sigma^2}) dx \nonumber\\
	&=\int_{-\infty}^{+\infty} \exp(-A r^2 e^{x})\frac{1}{\sqrt{2\pi}\frac{2}{\gamma}\sigma} \exp(-\frac{x^2}{2(\frac{2}{\gamma})^2\sigma^2}) dx
\end{align}
Formula $(\ref{eq:def_ps_2})$ is actually the value of Laplace Transform for one log-normal random variable $X \sim LN(0, \frac{2}{\gamma}\sigma)$ at point $Ar^2$.
\begin{align}
	p_{s}(r) &= \mathbb{E}\left[ \exp(-Ar^2X)\right] \nonumber\\
	&= \mathcal{L}_{X} \left[ Ar^2\right]
\end{align}
Now we will not consider to get the closed-form expression for $p_{s}(r)$, because in the following the form will simplify the analysis.
%现在我们先不考虑：获取P_s的解析解，因为在下一步计算会更方便
%A closed form expression of the Laplace transform of the lognormal distribution does not
%exist. According to reference~\cite{asmussen2016laplace}, the Laplace transform of a log-normal random variable can be accurately approximated as follows:
%\begin{align}
%\label{eq:laplace-transform-lognormal-form-1}
%\mathcal{L} \left\lbrace X \right\rbrace \left( s \right)
%&= \frac{\exp(-\frac{W(s \sigma_{X}^2 e^{\mu_{X}} )^2 + 2W(s \sigma_{X}^2 e^{\mu_{X}})}{2\sigma_{X}^2})}{\sqrt{1 + W(s \sigma_{X}^2 e^{\mu_{X}})}},
%\end{align}
%where $W\left( \cdot \right)$ is the Lambert W function~\cite{corless1996lambertw}, which is defined as the solution in principal branch of the
%equation $W\left(x\right) e^{W \left( x\right) }= x$.

We want to know the outage probability $P_{f1}$ over an infinite plane. Our strategy is to start with outage probability $P_{f1}(R)$ over a finite circle region with radius $R$, then extend to an infinite area. We still assume one device is located at the origin of the circle region with radius $R$. The spatial density of base station is $\pi R^2 \lambda_b$. The number of base station $N$ is thus:
\begin{align}
	\mathbb{P}\left[ N=n\right] = e^{-\pi R^2 \lambda_b}\frac{\left( \pi R^2 \lambda_b\right) ^n}{n!}
\end{align}
Since the base station is uniformly distributed in the circle region, the probability density function of distance between a given base station and the device $r$ at the origin is as follows:
\begin{align}
	f\left(r\right) = \frac{2r}{R^2}, r \in \left[ 0, R\right]
\end{align}

Thus, the outage probability $P_f\left( R\right) $ conditioned on base station $N$ is as follows:
\begin{align}
	P_{f1}\left( R \vert N= n\right ) &=  \prod_{i=1}^{n}\int_{0}^{R}(1-p_{s}(r_i))f(r_i) dr_i \nonumber\\
	&= \left[ 1-\int_{0}^{R} p_s\left(r\right)f\left( r\right)dr \right]^n
\end{align}

Next step, we decondition for  $P_{f1}\left( R \vert N= n\right )$, we have the expression $P_{f1}\left( R \right )$, which is actually the probability generating function (PGF) of Poisson distribution with intensity $\pi R^2 \lambda_b$:
\begin{align}
		P_{f1}\left( R\right ) &= \mathbb{E}\left[ P_f\left( R \vert N= n\right )  \right] \nonumber\\
		&= \exp(-\pi R^2 \lambda_b \int_{0}^{R} p_{s}\left( r \right)f\left( r\right)dr  ) \\
		&= \exp(-\pi R^2 \lambda_b \int_{0}^{R} \mathbb{E}\left[ e^{-Ar^2X} \right]  \frac{2r}{R^2}dr) \\
		&= \exp(-\pi R^2 \lambda_b \mathbb{E}\left[\int_{0}^{R} e^{-Ar^2X} \frac{2r}{R^2}dr \right] ) \\
		&= \exp(-\pi R^2 \lambda_b \mathbb{E}\left[\int_{0}^{R} e^{-Ar^2X} \frac{2r}{R^2}dr \right] ) \\
		&= \exp(-\pi\lambda_b \mathbb{E}\left[\int_{0}^{R^2} e^{-ArX} dr \right] ) \\
		&= \exp( \pi\lambda_b \mathbb{E}\left[ \frac{e^{-ArX} }{AX} \vert_{0}^{R^2}\right] ) \\
		&= \exp( \pi\lambda_b \mathbb{E}\left[ \frac{e^{-AR^2X} }{AX} - \frac{1}{AX}\right] )
\end{align}
By extending $R$ to $+\infty$, we get the outage probability over infinite plane:
\begin{align}
	\label{eq:analytical_result_approach_1}
	P_f &=  \exp( \pi\lambda_b A \mathbb{E}\left[ {X^{-1}}\right] ) \\
	&= \exp(-\frac{1}{\frac{p\lambda_m}{\lambda_b}\Gamma(1+\frac{2}{\gamma}) \Gamma(1-\frac{2}{\gamma})\theta^{\frac{2}{\gamma}}} )
\end{align}
We surprisingly observe that with BS reception diversity, the limit case, the outage probability has the same formula with the case where just Rayleigh fading is considered. But in practical system, we still observe that shadowing has improved the outage probability, since the wireless network cannot be infinite and the population is always limited.

As a comparison reference, now we consider probability $P_{f2}$ over an infinite plane using traditional method: the device attach the nearest BS.
The distance to the nearest base station, we denote this distance as $r$. Its PDF is:
\begin{align}
f\left( r\right)  = 2 \pi \lambda_b  r \exp(-\lambda_b \pi r^2), r \in \left[ 0, +\infty\right]
\end{align}
\begin{align}
P_{f2}\left( R \right) &= \mathbb{E}\left[ 1-p_{s}\left(r\right) \right]  \nonumber\\
&= 1-\int_{0}^{R}   p_{s}\left(r\right)  f\left( r\right) dr \nonumber\\
&= 1-\int_{0}^{R} \mathbb{E}\left[ \exp(-A r^2 X)\right]  2 \pi \lambda_b  r \exp(-\lambda_b \pi r^2) dr \nonumber\\
&= 1-  \pi \lambda_b \mathbb{E}\left[ \int_{0}^{R} \exp\left\lbrace -\left( AX+\lambda_b \pi\right)  r^2 \right\rbrace    dr^2 \right] \nonumber\\
&= 1 + \frac{\pi \lambda_b \exp\left\lbrace -\left( AX+\lambda_b \pi\right)  r^2 \right\rbrace }{AX+\lambda_b \pi}\vert_{0}^{R}
\end{align}
Similarly, by extending $R$ to $+\infty$, we have $P_{f2}$:
\begin{align}
P_{f2} &= 1 - \mathbb{E}\left[ \frac{1}{\frac{AX}{\pi\lambda_b}+1} \right] \nonumber
\end{align}
For the simplicity  of writing in the following, let $B= A \left( \pi \lambda \right) ^{-1}$. And we will focus on the term $\mathbb{E}\left[ \frac{1}{BX+1} \right]$. Thus,
\begin{align}
\label{eq:mean_bx+1_step1}
&\mathbb{E}\left[ \frac{1}{BX+1} \right] =  \int_{0}^{+\infty} \frac{1}{\left( Bx+1\right) \sqrt{2\pi}\sigma_X x} \exp(-\frac{(\ln(x))^2}{2\sigma_X^2})dx\nonumber\\
&= \int_{-\infty}^{+\infty} \frac{1}{ Be^t+1} \cdot \frac{1}{\sqrt{2\pi} \sigma_X} \exp\left\lbrace -\frac{t^2}{2 \sigma_X^2}\right\rbrace dt \nonumber\\
&= \int_{-\infty}^{+\infty} \frac{1}{1+e^{-(t-\ln(B))}} \cdot \frac{1}{\sqrt{2\pi} \sigma_X} \exp\left\lbrace -\frac{t^2}{2 \sigma_X^2}\right\rbrace dt \nonumber\\
&= \int_{-\infty}^{+\infty} \frac{1}{1+\exp{-\left( \frac{t-\frac{\ln(B)}{\sigma_X}}{\frac{1}{\sigma_X}}\right) }} \cdot \frac{1}{\sqrt{2\pi}} \exp\left\lbrace -\frac{t^2}{2}\right\rbrace dt \nonumber\\
& = \int_{-\infty}^{+\infty} \frac{\phi\left( t \right) }{1+\exp{-\left( \frac{t-\frac{\ln(B)}{\sigma_X}}{\frac{1}{\sigma_X}}\right) }} dt,
\end{align}
where $\phi\left( t \right)$ is the PDF of standard normal distribution. According to~\cite{crooks2009logistic}, the logistic function can be approximately by an error function:
\begin{align}
\label{eq:logistical_erf_fun_appro}
\frac{1}{1 + \exp(-\frac{x}{\alpha})} &\approx \frac{1}{2} + \frac{1}{2}\erf(\frac{\sqrt{\pi}}{4\alpha}x)，
\end{align}
where $\alpha$ is the parameter of logistic function. Applying $(\ref{eq:logistical_erf_fun_appro})$ for the denominator of integrand in $(\ref{eq:mean_bx+1_step1})$ , we have:
\begin{align}
\frac{1}{1+\exp{-\left( \frac{t-\frac{\ln(B)}{\sigma_X}}{\frac{1}{\sigma_X}}\right) }} &\approx \frac{1}{2} + \frac{1}{2}\erf(\frac{\sqrt{\pi}\sigma}{4}\left( t-\frac{\ln(B)}{\sigma_X}\right) ) \nonumber\\
&= \Phi\left( \sqrt{\frac{\pi}{8}}\sigma\left( t-\frac{\ln(B)}{\sigma_X}\right) \right)
\end{align}
Therefore:
\begin{align}
&\mathbb{E}\left[ \frac{1}{BX+1} \right] = \int_{-\infty}^{+\infty} \Phi\left( \sqrt{\frac{\pi}{8}}\sigma_X\left( t-\frac{\ln(B)}{\sigma_X}\right) \right) \phi\left( t \right) dt \nonumber\\
&= Pr \left\lbrace X_1 \leq \sqrt{\frac{\pi}{8}}\sigma_X\left( X_2-\frac{\ln(B)}{\sigma}\right) \right\rbrace \nonumber\\
&= Pr \left\lbrace X_1 - \sqrt{\frac{\pi}{8}}\sigma_X X_2 \leq -\frac{\ln(B)}{2} \sqrt{\frac{\pi}{2}} \right\rbrace
\end{align}
where $X_1, X_2$ are independent standard normal random variable. Obviously, $X_1 - \sqrt{\frac{\pi}{8}}\sigma_X X_2 \sim \mathcal{N}\left( 0,  1+ \frac{\pi}{8}\sigma_X^2\right) $. Hence,
\begin{align}
\label{eq:mean_bx+1_step2}
\mathbb{E}\left[ \frac{1}{BX+1} \right] &= Pr \left\lbrace \frac{X_1 - \sqrt{\frac{\pi}{8}}\sigma_X X_2}{\sqrt{1+ \frac{\pi}{8}\sigma_X^2}} \leq -\frac{\ln(B)}{\sqrt{\frac{8}{\pi}+ \sigma_X^2}}\right\rbrace  \nonumber\\
&= \Phi\left( -\left( \frac{8}{\pi}+ \sigma_X^2\right) ^{-\frac{1}{2}}\ln(B) \right) \nonumber\\
&= \frac{1}{2} + \frac{1}{2} \erf \left\lbrace -\left( \frac{16}{\pi}+ 2\sigma_X^2\right) ^{-\frac{1}{2}}\ln(B)\right\rbrace
\end{align}
Still, applying $(\ref{eq:logistical_erf_fun_appro})$ for $(\ref{eq:mean_bx+1_step2})$, we have:
% 实际上此图可以删除
%\begin{figure}
%	\centering
%	\includegraphics[width=0.7\linewidth]{Figures/erf_approx}
%	\caption{The approximation of the logistic function by error function. The absolute difference between the two functions is always less than 0.02.}
%	\label{fig:erf_approx}
%\end{figure}
\begin{align}
\label{eq:analytical_result_approach_2}
P_{f2} &= 1 - \frac{1}{1 + \exp\left\lbrace \left( 1 +\frac{\pi \sigma_X^2}{8} \right)^{-\frac{1}{2}} \ln(B) \right\rbrace} \nonumber\\
&= 1-\frac{1}{1 + B^{\left( 1 +\frac{\pi \sigma_X^2}{8} \right)^{-\frac{1}{2}}}},
\end{align}
where $B= \frac{p\lambda_m}{\lambda_b}\Gamma(1+\frac{2}{\gamma}) \Gamma(1-\frac{2}{\gamma}) \exp \left( \frac{\sqrt{2}\beta\sigma}{\gamma}\right) ^2 \theta^{\frac{2}{\gamma}}$.