%These comments were left for any intrepid scholars who may examine this file after me

\documentclass[12pt]{article}
\title{A Generalization of The Chinese Remainder Theorem}
\author{Klaus Crusius}

\usepackage{amssymb,amsmath,amsthm}
\usepackage[pdftex]{graphicx}

\newtheorem{theorem}{Theorem}
\newtheorem{lemma}{Lemma}

\providecommand{\divides}[2]{#1 \, \lvert \, #2}
\providecommand{\refeq}[1]{(eq.#1)}
\def\1ton{1 \cdots n}
\providecommand{\for}[2]{\forall_{#1 \in #2} \;}
\providecommand{\forn}[1]{\for{#1}{\1ton}}
\providecommand{\set}[2]{\{ #1 \mid #2 \}}
\providecommand{\abs}[1]{\lvert#1\rvert}
\providecommand{\up}[1]{u_p\left(#1\right)}
\providecommand{\gcdc}[1]{\gcd\left(#1\right)}
\providecommand{\lcmc}[1]{\lcm\left(#1\right)}
\providecommand{\minc}[1]{\min\left(#1\right)}
\providecommand{\maxc}[1]{\max\left(#1\right)}
\def\ie{i.e.\,}
\def\lt{<}
\def\coloneqq{:=}
\def\Z{\mathbb{Z}}
\def\N{\mathbb{N}}
\DeclareMathOperator*{\modf}{mod}
\DeclareMathOperator{\lcm}{lcm}

\begin{document}
\maketitle
\newpage
\tableofcontents
\newpage
\abstract{
\indent It is well known, that the Chinese Remainder Theorem is valid under the condition of mutual co-prime multiple modules. This paper gives a generalization to the case of non-co-prime modules. The constructive proof allows to derive an efficient algorithm, which can be easily parallelized.
}

\noindent \section{Theorems and Proofs}

\indent It is well known that the following Theorem is valid.

\begin{theorem}{Chinese Remainder Theorem}
	\label{theorem:CRT}

	Given $n \ge 1$ and a set of mutual co-prime positive integers $m_i$ and corresponding remainders $ a_i $ with $ 0 \le a_i \lt m_i$ for $ i = 1,2,\dotsc n$. Then there exists exactly one $x$ with $ 0 \le x \lt m_1 m_2 \dotsm m_n$ which solves the equations
	$ x \equiv a_i  \mod  m_i$ for all $ i = \1ton$. \cite[ch.~4.3.2, p.~286]{Knuth2}

\end{theorem}

This theorem becomes invalid, if we drop the condition of mutual co-primeness. For example there is no solution for  $x \equiv 0 \mod 20 \text{; }x \equiv 1 \mod 50$, while for $x \equiv 1 \mod 20 \text{; } x \equiv 11 \mod 50$ we have $10$ solutions $\{61, 161, \dotsc, 961\}$.

At first, we will proof a necessary condition on the remainders, if a solution is to exist.

\begin{theorem}{Necessary condition on remainders}
	\label{theorem:nec}

	Let $m_1, m_2, \dotsc, m_n$ be positive and $x, a_1, a_2, \dotsc, a_n$ be integers, which solve the equations
	\begin{equation}
	\label{eq:xam}\tag{\ref{theorem:nec}.1}
	 \forn{i} x \equiv a_i \mod m_i.
	 \end{equation}
	Then we have

	\begin{equation}
	\label{eq:amij}\tag{\ref{theorem:nec}.2}
	 \forn{i, j} a_i \equiv a_j \mod \gcd\left(m_i, m_j\right)
	 \end{equation}
\end{theorem}

\begin{proof}
	From \eqref{eq:xam} and because $\divides{\gcd\left(m_i, m_j\right)}{m_i}$ we conclude, that $x \equiv a_i \mod \gcd\left(m_i, m_j\right)$ for all $i, j$. By eliminating $x$ for each pair of $i,j$ the assertion follows immediately.
\end{proof}

We will give a generalization of Theorem \ref{theorem:CRT}, which replaces the co-primeness condition on $ m_i$ by the necessary condition \eqref{eq:xam}. We restrict in a first step to the case $ n = 2$ and prove the following:

\begin{theorem}{Generalized Chinese Remainder Theorem - two modules}
	\label{theorem:CRT2}

	Let $ p, q, a, b \in \Z$ integers with $0 \le a \lt p$ and $ 0 \le b \lt q$. If
\begin{equation}
\label{eq:abpq}\tag{\ref{theorem:CRT2}.1}
 a \equiv b \mod \gcd\left(p, q\right),
\end{equation}
then there exists a unique $ x \in \Z $ with

\begin{equation}
\label{eq:xapbq}\tag{\ref{theorem:CRT2}.2}
x \equiv a \mod p \; and \; x \equiv b \mod q.
\end{equation}

\begin{equation}
\label{eq:xltlcm}\tag{\ref{theorem:CRT2}.3}
 0 \le x \lt \lcm\left(p, q\right) \text{ and }
\end{equation}

The solution is given by formula
\begin{equation}
\label{eq:xab}\tag{\ref{theorem:CRT2}.4}
\begin{split}
x &= a + p \ \modf\left( u \left(\frac{b - a}{c}\right), \; \frac{q}{c} \right)\\
\text{ with } c &= \gcd\left(p, q\right) \text{ and } u = \; {\left(\frac{p}{c}\right)}^{-1} \mod \frac{q}{c}.\notag
\end{split}
\end{equation}

\end{theorem}

\begin{proof}

	\em Uniqueness: \em Assume $x$ and $y$ solve equations \eqref{eq:xapbq}. Then by subtracting we obtain $x \equiv y \mod p$ and $x \equiv y \mod q$. Then $x \equiv y \mod \lcm\left(p, q\right)$ by equation \eqref{lemma.b} of Lemma \ref{lemma}. Because of \eqref{eq:xltlcm} $x = y$.

	 \em Construction of solution: \em We give a closed formula for an x  solving \eqref{eq:xapbq}  and  \eqref{eq:xltlcm} under condition \eqref{eq:abpq}.

	 Let $c \coloneqq  \gcd\left(p, q\right)$. We can write $a = a_2 + c a_1$ and $b = a_2 + c b_1$ with $0 \le a_2 \lt c$, because $a \equiv b \mod c$. The equations become
	 $x = a_2 + c a_1 + c p_1 r$ and  $x = a_2 + c b_1 + c q_1$. Here $p_1 \coloneqq p / c$ and $q_1 \coloneqq q / c$. $p_1$ and $q_1$ are co-prime. By introducing a new variable $y$, substituting
	 \begin{align}
	 \label{eq:xy}x &= c y + a_2 \text{, and dividing by $c$, we obtain}\\
	 \label{eq:ya}y &= a_1 + p_1 r \text{ and } y = b_1 + q_1 s.
	 \end{align}

	  Theorem \ref{theorem:CRT} asserts the existence and uniqueness of $y$ with $0 \le y \lt p_1 q_1$. We try to calculate $y$, $r$, and $s$.

	 There is a unique inverse $u$ of $p_1$ modulo $q_1$, \ie $u p_1 = 1 + q_1 v$ with $0 \le u \lt q_1$, which can be calculated by a the Extended Euclid's algorithm \cite[ch.~4.5.2, Theorem X, ~p.342]{Knuth2}. We subtract equations \eqref{eq:ya} and multiply with $u$ to obtain
	 \begin{align*}
	 u \left(b_1 - a_1\right) &= u p_1 r - u q_1 s\\
	  &= r + q_1 v r - u q_1 s\\
	  &= r + \left(v r - u s\right) q_1 \text{, hence}\\
	 p_1 r &= p_1 \left[u \left(b_1 - a_1\right)\right] + \left(u s - v r\right) p_1 q_1,\\
	 \intertext{thus \eqref{eq:ya} becomes}
		y &= a_1 + p_1 \left[u \left(b_1 - a_1\right)\right] + \left(u s - v r\right) p_1 q_1.
	 \end{align*}
	 If we perform the calculation of $u \left(b_1 - a_1\right)$ modulo $q_1$, we get $u \left(b_1 - a_1\right) = \modf\left(u \left(b_1 - a_1\right), q_1\right) + k q_1$ for some $k$, to obtain finally the solution in terms of $y$:
	 \begin{equation*}
	 \label{eq:ysol}y = a_1 + p_1 \modf\left( u \left(b_1 - a_1\right), q_1\right) + \left(u s - v r + k\right) p_1 q_1.
	 \end{equation*}
	 Because $0 \le a_1 \lt p_1$ and $0 \le \modf\left(\cdot, q_1\right) \le q_1-1$, we have
	 \begin{equation*}
	 0 \le a_1 + p_1 \modf\left(u \left(b_1 - a_1\right), q_1\right) \le a_1 + p_1 \left(q_1 - 1\right) \lt p_1 q_1.
	 \end{equation*}

	 Therefore
	 \begin{equation*}
	   	y = a_1 + p_1 \modf\left(u \left(b_1 - a_1\right), q_1\right)
	 \end{equation*}
	 is the unique solution of \eqref{eq:ya}, with $0 \le y \lt p_1 q_1$. Re-substituting $x$ in \eqref{eq:xy} gives $x = a_2 + c a_1 + p \modf\left(u \left(b_1 - a_1\right), q_1\right)$ and using the original values
	 \begin{equation}
	 \tag{\ref{eq:xab}}
	 \begin{split}
		 x &= a + p \modf\left( u \left(\left(b - a\right) / c\right), q/c \right)\\
		\text{ with } c &= \gcd\left(p, q\right) \text{ and } u = \modf\left(p/c, q/c\right).\notag
		\end{split}
	 \end{equation}
	 We claim that x of \eqref{eq:xab} is the unique solution of \eqref{eq:xapbq} and \eqref{eq:xltlcm}. First part of \eqref{eq:xapbq} is obvious. For the second we have to prove $a + p \left(u \left(b - a\right)/c - k q / c\right) \equiv b \mod q$. That is equivalent to $a - b + p_1 u \left(b - a\right) - p_1 k q \equiv 0 \mod q$. Since $p_1 u = 1 + q_1 v$,  that reduces further to
	 $a - b + b - a + q_1 v \left(b - a\right) \equiv 0 \mod q$, or $q v \left(b_1 - a_1\right) \equiv 0 \mod q$, which is valid.

	 To prove \eqref{eq:xltlcm}, we use $0 \le a \lt p \text{ and } 0 \le \modf\left(\cdot, q/c\right) \le q/c - 1$ to conclude $0 \le x \lt p + p\left(q/c-1\right) = p q / c = \lcm\left(p, q\right)$.

\end{proof}

We can now formulate the main theorem of this article.
\begin{theorem}{Generalized Chinese Remainder Theorem}
	\label{theorem:CRTG}

	Let $m_1, m_2, \dotsc , m_n$ be positive and $a_1, a_2, \dotsc , a_n$ be integers with $0 \le a_i \lt m_i$ satisfying for all $i, j \in \{\1ton\} \text{ the conditions }$ $$a_i \equiv a_j \mod \gcd\left(m_i, m_j\right)$$

	Then there is exactly one integer $x \text{ with } 0 \le x \lt \lcm\left(m_i \mid i \in \{\1ton\} \right)$, which satisfies
	 $$x \equiv a_i \mod m_i \text{ for } i \in \{\1ton\} \ .$$

\end{theorem}

\begin{proof}$\\$
	For the purpose of this proof, we define $\lcm_I \coloneqq \lcmc{\set{m_i}{i \in I}}$

	The theorem is valid independent of the chosen finite index set. So we can write $m_i \text{ for } i \in I \text{ with } \abs{I} \lt \infty$ without changing the proof.

	If $n = 1$ the assertion is trivially true with $x = a_1$.\\
	If $n > 1$ we conduct a proof by induction on n.

	Assume, the assertion of the theorem was true for all index sets $I \text{ with } \abs{I} \lt n$. Then we can derive the assertion using previous Theorem \ref{theorem:CRT2}. We split the complete index set into two non-empty subsets $I, J \ne \emptyset \text{ with } I \cup J = \{\1ton\}$. Because of the induction assumption, for $K \in \{I, J\}$ there is a $x_K$ with
	\begin{equation}
	\label{eq:indco} 0 \le x_K \lt \lcm_K \text{ and } \forall_{i \in K}\; x_K \equiv a_i \mod m_i.
	\end{equation}
	We want to apply Theorem \ref{theorem:CRT2} with $a = x_I, b = x_J, p = \lcm_I, q = \lcm_J$. The necessary condition \eqref{eq:abpq} reads now $$x_I \equiv x_J \mod \gcd\left(\lcm_I, \lcm_J\right).$$
	Because of \eqref{eq:indco} $\forall_{i \in I} \forall_{j \in J} \; x_I \equiv a_i \mod \gcd\left(m_i, m_j\right) \text{ and } x_J \equiv a_j \mod \gcd\left(m_i, m_j\right)$, using conclusion \eqref{lemma.a} of Lemma \ref{lemma}.\\
	Hence $\forall_{i \in I} \forall_{j \in J}\; x_I - x_J \equiv a_i - a_j \equiv 0 \mod \gcd\left(m_i, m_j\right)$, which is equivalent by Lemma \ref{lemma} \eqref{lemma.b} to
	$$ x_I \equiv x_J \mod \lcm\left(\set{\gcd\left(m_i, m_j\right)}{i \in I, j \in J}\right).$$
	Then the necessary condition follows, because of Lemma \ref{lemma} \eqref{lemma.c} and \eqref{lemma.a}.

	Theorem \ref{theorem:CRT2} delivers a unique $0 \le x \lt \lcmc{\lcm_I, \lcm_J}$ with
	$ x \equiv x_I \mod \lcm_I \ \land \ x \equiv x_J \mod \lcm_J$. Because of Lemma \ref{lemma} \eqref{lemma.a} and $\divides{m_i}{\lcm_I}$ we have $ \forall_{i \in I}\; x \equiv x_I \mod m_i$. So $ x \equiv a_i \mod m_i$ because of \eqref{eq:indco}. The same is true $ \forall_{i \in J}$.

\end{proof}

The proofs need some auxiliary facts from elementary number theory, which are noted in the following:

\begin{lemma}\label{lemma}
	In all statements below let
	\begin{align*}
	&x, y, a, u \in \Z, I, J \text{ finite index sets, and } \for{i}{I \cup J} m_i \in \N \\
	&\lcm_I \coloneqq \lcm\left(\set{m_i}{i \in I}\right)
	\end{align*}
	 then

	\begin{align}
		\label{lemma.a}\tag{L1}
		x \equiv y \mod u &\implies \forall_{\divides{a}{u}} \; x \equiv y \mod a\\
		\label{lemma.b}\tag{L2}
		\for{i}{I} x \equiv y \mod m_i	&\iff x \equiv y \mod \lcm_I\\  \label{lemma.c}\tag{L3}
		\lcm\left(\lcm_I, \lcm_j\right) & \quad = \; \lcm_{I \cup J}\\
		\label{lemma.d}\tag{L4}
		\gcd\left(\lcm_I, \lcm_J\right) \text{ divides }
		&\lcm\left(\set{\gcd\left(m_i, m_j\right)}{i \in I, j \in J}\right)
	\end{align}

\end{lemma}

\begin{proof}$\\$
		\eqref{lemma.a}: If $u = k a \text{ and } x = y + v u \text{ for some } k, v \in \Z$, then $x = y + \left(v k\right) a$, hence $x \equiv y \mod a$.

		\eqref{lemma.b}: $\impliedby$ is clear because $\for{i}{I} \divides{m_i}{\lcm_I}$ and \eqref{lemma.a}.

		$\implies$: To see that we assume $x - y = k \mod \lcm_I$ with $0 \le k \lt \lcm_I$ and show, that $k = 0$.
		 Because $\forall_i \divides{m_i}{\lcm_I}$, we have $x - y = k + \lcm_I u = k + m_i u_i$ for some $u, u_i$. Because $\forall_i \; x – y \equiv 0 \mod m_i$, $\exists_{v_i} \; x - y = m_i v_i$, hence $k = m_i \left(v_i - u_i\right)$. That means $k$ is a multiple of all $m_i$, hence of $\lcm_I$, by the definition of $\lcm$. The only $k$ with $0 \le k \lt \lcm_I$ is $k = 0$.

		 \eqref{lemma.c}$ "\ge"$ : because $\lcm\left(\lcm_I, \lcm_J\right) = \lcm_I k_I \text{ and } lcm_I = m_i k_{iI}$ for some $k_I, k_{iI} \for{i}{I} $, we have $\lcm\left(\lcm_I, \lcm_J\right) = m_i k_I k_iI$, that means the left-hand side is a multiple of $m_i \for{i}{I}$. Accordingly, it is a multiple of $m_j \for{j}{J}$. Then, by definition of $\lcm$ it is $\ge \lcm_{I\cup J}$.

		 $ "\le"$ : $\lcm_{I \cup J}$ is a multiple of $m_i \for{i}{I}$, hence of $\lcm_I$ by definition of $\lcm_I$; accordingly also of $\lcm_J$. Then it is also a multiple of $\lcm\left(lcm_I, lcm_J\right)$. So the right-hand side is $\ge$ the left-hand side.

		 \eqref{lemma.d}: We make use of the Fundamental Theorem of Arithmetic \cite[chapter 1.2.4, exercise ~21]{Knuth1}, which proves the unique prime-factorization of the natural numbers. For each number $n \in \N$ and each prime number $p$ there is a unique exponent $\up{n} \in \N \cup \{0\}$, such that $$ n = \prod_{p \text{ prime}} p^{\up{n}}.$$ where only a finite amount of the $u_p\left(n\right) \ne 0$. Then we have
		 \begin{align*}
			 \gcdc{m, n} &= \prod_{p \text{ prime}} p^{\minc{\up{m}, \up{n}}}\\
			 \lcmc{m, n} &= \prod_{p \text{ prime}} p^{\maxc{\up{m}, \up{n}}}\\
		 \end{align*}
		 or for each prime $p$
		 \begin{align*}
			 \divides{m}{n} &\iff \forall_p \; \up{m} \le \up{n}\\
			 \up{\gcdc{m, n}} &= \minc{\up{m}, \up{n}}\\
			 \up{\lcmc{m, n}} &= \maxc{\up{m}, \up{n}}\\
		\end{align*}
		Then \eqref{lemma.d} ( we set $u_{pi} \coloneqq \up{m_i}$ ) is equivalent to
		\begin{equation}\label{eq:minmax}
		\begin{split}
			\forall_p &\minc{\maxc{\set{u_{pi}}{i \in I}}, \maxc{\set{u_{pj}}{j \in J}}}\\
			\le &\maxc{\set{\minc{u_{pi}, u_{pj}}}{i \in I, j \in J}}
		\end{split}
		\end{equation}
		There is an $i_{max} \in I$ with $ u_{pi_{max}} = \maxc{\set{u_{pi}}{i \in I}}$; as well as an $j_{max} \in J$. Inserting these into the left-hand side of \eqref{eq:minmax} gives
		\begin{align*}
			\minc{u_{pi_{max}}, u_{pj_{max}}} \le \maxc{\set{\minc{u_{pi}, u_{pj}}}{i \in I, j \in J}}
		\end{align*}
		which is obviously true for all prime numbers $p$.
\end{proof}

\section{Algorithms}

From Theorem \ref{theorem:CRT2} we can straightforward derive the following procedure:

\newtheorem{algo}{Algorithm}
\begin{algo}$\\$
	\noindent procedure crt2(a, b, p, q)\\
	Input: a, b, p, q: integers p, q $>$ 0\\
	Output: x, lcm: solution, least common multiple of p and q\\
	Errors: fail if a $\ne b \mod \gcdc{p, q}\\$
	External: gcdx: calculate greatest common divisor\\
	                and inverse of co-prime pair\\
	\begin{align*}
	&c, u \coloneqq gcdx(p, q)\\
	&p_1, q_1 \coloneqq p / c, q / c\\
	&u \coloneqq \modf(u, q_1)\\
	&if \; mod(b -a, c) \ne 0 \; Error("remainders' condition")\\
	&bac \coloneqq (b - a) / c\\
	&x \coloneqq a + p * mod(u * bac, q_1)\\
	&lcm \coloneqq p * q_1\\
	&return \; x, lcm
	\end{align*}
\end{algo}

Theorem \ref{theorem:CRTG} provides some freedom in partitioning the original set. If $n = 1$ we return the trivial solution or we apply Algorithm 1. Otherwise, we split $ \{\1ton\} $ two partitions and apply Theorem \ref{theorem:CRTG}.
\begin{algo}$\\$
	\noindent procedure crtg(a, m)\\
	Input: a, m: integer vectors of same lengths, m $>$ 0\\
	Output: x, lcm: solution, least common multiple of m\\
	Errors: fail if $a_i \ne a_j \mod \gcdc{m_i, m_j} for \ any \ i, \ j$\\
	External: crt2: see above\\
	\begin{align*}
	&n \coloneqq length(a) \\
	&x_I, lcm_I \coloneqq 1,\ 1\\
	&for \ i \coloneqq 1 \dots n\\
	&\ \ x_I, lcm_I \coloneqq crt2(x_I, a[i], lcm_I, m[i])\\
	&end\\
	&return \; x_I, lcm_I\\
	\end{align*}
\end{algo}

\newpage

\begin{thebibliography}{4}

	\bibitem{Knuth1}
	 Donald E. Knuth,
	 \emph{The Art of Computer Programming - Volume 1}
	 Addison-Wesley, New York, 3rd edition,  1998.

	 \bibitem{Knuth2}
	 Donald E. Knuth,
	 \emph{The Art of Computer Programming - Volume 2}
	 Addison-Wesley, New York, 3rd edition,  1998.

\end{thebibliography}

\end{document}


\chapter{Final model}
	\section{Blending IDM and MOBIL}
		Until this point all simulations were run in a one lane imaginary road. However to be able to represent real traffic situations multi lane roads have to be considered as mentioned in the introduction. In a road with multiple lanes IDM can describe the longitudinal and MOBIL the transversal motions. For a starting point let us revisit the model used previously:
		\begin{equation}
			\dot{\textbf{y}}=
			\begin{pmatrix}
			v_0\\
			0\\
			f_1(v_{2})\\
			f_2(x_{2}, v_{2},x_{1}, v_{1})\\
			f_1(v_{3})\\
			f_2(x_{3}, v_{3},x_{2}, v_{2})\\
			\vdots\\
			f_1(v_{\rm n-1})\\
			f_2(x_{n-1}, v_{n-1},x_{n-2}, v_{n-2})\\
			f_1(v_{\rm n})\\
			f_2(x_{n}, v_{n},x_{n-1}, v_{n-1})
			\end{pmatrix}\,.
			\label{eq:n_ode_math_revisit}
		\end{equation}
		As Eq.~\eqref{eq:n_ode_math_revisit} shows there is a criterion on the car indexing. Namely n-1-th vehicle is always the leader of n-th car. Every car is stored in an array called \textit{definition array} with their identifiers, current and target lanes, initial positions and velocities, and parameters. An example of this definition array can be seen in Table~\ref{tab:definition_array}.
		\begin{table}
			\begin{center}
				\begin{tabular}{ |c|c|c|c|c|c| }
					\hline
					Id & Current lane & Target lane & Initial position & Initial velocity& IDM params\\
					$[-]$ & $[-]$ & $[-]$ & $[m]$ & $[m/s]$ & $[-]$\\
					\hline
					1 & 1 & 0 & 100 & 0 & ...\\
					2 & 2 & 0 & 50 & 0 & ...\\
					\vdots & \vdots & \vdots & \vdots & \vdots & \vdots\\
					n - 1 & 1 & 0 & 200 & 0 & ...\\
					n & 1 & 0 & 0 & & ...\\
					\hline
				\end{tabular}
			\end{center}
			\caption{Definition array example}
			\label{tab:definition_array}
		\end{table}
		Since there were no lane changes until this point, the indexing condition held. However this criterion cannot be satisfied where changing lanes are allowed. Instead of this condition a new function is introduced. The suggested method is capable of calculating which car is the leader of an other vehicle. So from now on the simulator relies on this function instead of the order of car models in the definition array.
		\subsection*{Determining the leader vehicle}
		The previously mentioned function does the following steps
		\begin{itemize}
			\item assembles and sorts a car model array with the required data by current vehicle position,
			\item searches for cars in the same lane,
			\item finds the row of the car by id,
			\item returns the row above (index - 1) if exists or nothing if there is no row above.
		\end{itemize}
		Let us call this function $g$. This $g$ function has one parameter, the id of the car whose leader vehicle is searched for. If there is leader than it returns exactly the same values as the IDM expects as parameters, namely the leading car's position and velocity.
		So Eq.~\eqref{eq:n_ode_math_revisit} modifies as follows:
		\begin{equation}
			\dot{\textbf{y}}=
			\begin{pmatrix}
			f_1(v_1)\\
			f_2(x_{1}, v_{1},g(\id_1))\\
			f_1(v_2)\\
			f_2(x_{2}, v_{2},g(\id_2))\\
			f_1(v_2))\\
			f_2(x_{3}, v_{3},g(\id_3))\\
			\vdots\\
			f_1(v_{n-1}))\\
			f_2(x_{n-1}, v_{n-1},g(\id_{n-1}))\\
			f_1(v_n))\\
			f_2(x_{n}, v_{n},g(\id_{n}))
			\end{pmatrix}\,.
			\label{eq:n_ode_math_with_find_leading}
		\end{equation}
		As mentioned previously function $g$ can only return the expected parameters by $f$ if there is a leader car. This makes sense since someone has to be the very first in every single time step. Consequently that cars longitudinal model has to be changed to handle that there is no car in front of it. It is handled by leaving the follower behavior out of the Eq.~\eqref{eq:aidm}. So it will not need the leader vehicle's parameters. This means that the leader cars in their lanes will always be in free acceleration state. That is coinciding with reality.
		\subsection*{Implementation of lane changing}
		The original n-car solver only calculated and stored the \textbf{y} vector which contained the position and velocity values at each time step. Since lane changes came into picture that would not be enough. Consequently, two more vectors needs to be stored. One for the current lane (named $\vcl$) and one for the target lane (named $\vtl$) which can keep track of the lane changes. $\vcl$ will store the lane numbers of the vehicle's current lane. $\vtl$ will contain the target lane identifier when a car is changing lanes, otherwise it will have zero value. Figure~\ref{fig:stage1ofchanginglanes}, \ref{fig:stage2ofchanginglanes}, \ref{fig:stage3ofchanginglanes} show the stages of an example lane change.
		\begin{figure}
			\begin{center}
				\begin{minipage}{.65\textwidth}
					\includegraphics[width=\textwidth]{common/stages_of_lane_changes_1}
				\end{minipage}\quad
				\begin{minipage}{.3\textwidth}
					\begin{tabular}{ |c|c|c|c| }
						\hline
						Id & $\vcl$ & $\vtl$ & ... \\
						$[-]$ & $[-]$ & $[-]$ & ...\\
						\hline
						$\vdots$ & $\vdots$ & $\vdots$ & \vdots\\
						$C$ & $2$ & $0$ & ...\\
						$\vdots$ & $\vdots$ & $\vdots$ & \vdots\\
						\hline
					\end{tabular}
				\end{minipage}
			\end{center}
			\caption{Stage I of changing lanes. Vehicle C is considering a lane change.}
			\label{fig:stage1ofchanginglanes}
		\end{figure}
		\begin{figure}
			\begin{center}
				\begin{minipage}{.65\textwidth}
					\includegraphics[width=\textwidth]{common/stages_of_lane_changes_2}
				\end{minipage}\quad
				\begin{minipage}{.3\textwidth}
					\begin{tabular}{ |c|c|c|c| }
						\hline
						Id & $\vcl$ & $\vtl$ & ... \\
						$[-]$ & $[-]$ & $[-]$ & ...\\
						\hline
						$\vdots$ & $\vdots$ & $\vdots$ & \vdots\\
						$C$ & $2$ & $1$ & ...\\
						$\vdots$ & $\vdots$ & $\vdots$ & \vdots\\
						\hline
					\end{tabular}
				\end{minipage}
			\end{center}
			\caption{Stage II of changing lanes. Vehicle C changing lanes from lane 2 to lane 1. The row of vehicle C contains 2 in the current lane vector and 1 in the target lane vector.}
			\label{fig:stage2ofchanginglanes}
		\end{figure}
		\begin{figure}
			\begin{center}
				\begin{minipage}{.65\textwidth}
					\includegraphics[width=\textwidth]{common/stages_of_lane_changes_3}
				\end{minipage}\quad
				\begin{minipage}{.3\textwidth}
					\begin{tabular}{ |c|c|c|c| }
						\hline
						Id & $\vcl$ & $\vtl$ & ... \\
						$[-]$ & $[-]$ & $[-]$ & ...\\
						\hline
						$\vdots$ & $\vdots$ & $\vdots$ & \vdots\\
						$C$ & $1$ & $0$ & ...\\
						$\vdots$ & $\vdots$ & $\vdots$ & \vdots\\
						\hline
					\end{tabular}
				\end{minipage}
			\end{center}
			\caption{Stage III of changing lanes. Vehicle C has performed its lane change so the current lane vector will hold a value of 1 at the row of vehicle C, corresponding row of target lane vector will reset to zero.}
			\label{fig:stage3ofchanginglanes}
		\end{figure}
		\subsection*{Decision making}
		The updated model with vectors $\vcl$ and $\vtl$ is able to keep track of a multi lane traffic with lane changes. However the algorithm to fill those vectors correctly is not discussed yet. A lane change has multiple aspects like when does the driver wants to change or is it possible to change lanes safely. Fortunately these two questions can be answered by simply implementing the algorithm of MOBIL.

		From the implementation point of view in every time step MOBIL calculates that a driver should change lanes or not. If a driver should change lanes based on the local traffic situation then the lane change starts by filling the corresponding row in the vector $\vtl$. For now the duration of a lane change is neglected, so the lane change will end in the next time step which means that the car's current lane value will be the target lane value and target lane resets to zero. It is worth mentioning that a lane changing car occupies both its current and target lane. So it will be the leader car in both current and target lane's immediate following cars.
	\section{Simulation of a traffic light situation}
		Unfortunately \textsc{Matlab}'s built in ode45 solver cannot be used anymore since the lane changing algorithm needs to run in the inner scope of the solver, namely in the main loop which is not defined publicly. So the model described in the previous section has been implemented using EE method.  It is worth mentioning that both algorithms' (IDM and MOBIL) primary focus were simulating highway traffic. However in this document urban traffic was investigated. The task is to test the simulator through interesting cases like a red traffic light with 10 waiting vehicles and see the outcome after the traffic light turns into green.

		\subsection*{Initial conditions}
		The initial conditions can be found in Table~\ref{tab:case_1_definition_array}. The arrangement of the cars can be seen in Figure~\ref{fig:red_light_situation}. The front of the first car of each lane has been placed at zero position. Then every vehicle has been behind its leader by its traffic jam distance. The initial velocity of all cars is zero since they are waiting the traffic light turn to green. The IDM parameters of the simulated cars have been varied between realistic values. Also for the first time a bus has been introduced to traffic. From the model point of view a bus is only different from a regular car by its length and some of its IDM parameters like the maximum acceleration or duration of a lane change.

		Until now the simulator was running until a specific time. It would be nice to run until when all of the cars have reached a specific distance. For example a traffic light situation is interesting for the first 100 meters. A while loop has been implemented into the simulator which is checking that every car has reached the target line and stops the simulation when all of them have done. Figure~\ref{fig:traffic_light_init} shows the start position of the simulation and the target distance. Figure~\ref{fig:traffic_light_end} represents the end of the simulation when the last car has reached the target line.
		\begin{table}
			\begin{center}
				\begin{tabular}{ |c|c|c|c|c|c| }
					\hline
					Id & Current lane & Target lane & Initial position & Initial velocity& IDM params\\
					$[-]$ & $[-]$ & $[-]$ & $[m]$ & $[m/s]$ & $[-]$\\
					\hline
					1 & 1 & 0 & 0 & 0 & ..., L = 4.2, ... \\
					2 & 2 & 0 & 0 & 0 & ..., L = 4.4, ... \\
					3 & 1 & 0 & -5.4 & 0 & ..., L = 20, ... \\
					4 & 2 & 0 & -5.5 & 0 & ..., L = 4.1, ... \\
					5 & 1 & 0 & -26.4 & 0 & ..., L = 4.3, ... \\
					6 & 2 & 0 & -10.9 & 0 & ..., L = 4.5, ... \\
					7 & 1 & 0 & -31.9 & 0 & ..., L = 4.6, ... \\
					8 & 2 & 0 & -16.5 & 0 & ..., L = 4.2, ... \\
					9 & 1 & 0 & -37.7 & 0 & ..., L = 4.3, ... \\
					10 & 2 & 0 & -21.7 & 0 & ..., L = 4.4, ... \\
					\hline
				\end{tabular}
			\end{center}
			\caption{Initial conditions of Case 1}
			\label{tab:case_1_definition_array}
		\end{table}
		\begin{figure}
			\centering
			\includegraphics[width=\textwidth]{common/traffic_light_case_1}
			\caption{Red light situation with 10 vehicles}
			\label{fig:red_light_situation}
		\end{figure}
		\begin{figure}
			\centering
			\includegraphics[width=\textwidth]{common/traffic_light_init}
			\caption{Initial positions}
			\label{fig:traffic_light_init}
		\end{figure}
		\begin{figure}
			\centering
			\includegraphics[width=\textwidth]{common/traffic_light_end}
			\caption{End positions}
			\label{fig:traffic_light_end}
		\end{figure}
		\begin{figure}
			\centering
			\includegraphics[width=0.8\textwidth]{eemobil/simh_case1}
			\caption{Red light traffic. Headway of the vehicles respect to their position in lanes}
			\label{fig:red_light_situationh}
		\end{figure}
		\begin{figure}
			\centering
			\includegraphics[width=0.8\textwidth]{eemobil/simv_case1}
			\caption{Red light traffic. Velocity of the vehicles respect to their position in lanes}
			\label{fig:red_light_situationv}
		\end{figure}
		\begin{figure}
			\centering
			\includegraphics[width=0.8\textwidth]{eemobil/simcc_case1}
			\caption{Red light traffic. Number of cars in lanes }
			\label{fig:red_light_situationcc}
		\end{figure}
		\subsection*{Summary of the result}
		The traffic situation was successfully simulated. However post processing of the result turned out to be kind of a challenging task. The usage of the previous types of figures does not give enough information about the traffic. An excellent way of showing the result would be animating the vehicles and create a video from that. Unfortunately it cannot be included in the document here for obvious reasons however the QR code of the \href{https://youtu.be/wSFh_gOpIFU}{\underline{link}} can be found in the appendices. To make it easy to understand the motions of the vehicles here, the headway and velocity figures have been divided into two parts. One figure shows the headway of the cars in one lane the other figure shows the other lane. When a vehicle changes lanes its curve breaks and disappears from one figure and continues on the other.
		The headway and velocity diagrams can be seen in Figure~\ref{fig:red_light_situationh} and \ref{fig:red_light_situationv}. Notice that the first cars of each lanes have zero headway since there is no vehicle in front of them. Another figure was made to emphasize the lane changes during the simulation. Figure~\ref{fig:red_light_situationcc} shows the number of vehicles in each lane. The total time until the last car has reached the target line was \casetime{1}seconds in our investigated case.
		\section{Effect of the time step size on solutions}
		A numerical solution should be independent from the time step. Thus a time step independence analysis has been carried out. What it means practically is that one particular simulation should be run multiple times with decreasing step sizes. If the solution is converged than it can be said that the model is step size independent. The default value of the step size was 0.01 second. The simulations were run with the time step values defined in Table~\ref{tab:timestepsizes}.
		Using the same parameters and initial conditions as before, the analysis has been performed. As it can be seen the first car has been examined. The position and velocity of the car can be seen in Figure~\ref{fig:tsip} and \ref{fig:tsip}. These plots are almost identical for all time step size. Figure~\ref{fig:tsit} shows that the elapsed time is converging to approximately 28.5s. Figures~\ref{fig:tsipr} and \ref{fig:tsivr} show that for $\Delta \rm{t}\leq 0.0125$s the relative errors are below 1\%, so it is safe to say that the default $\Delta \rm{t}=0.01$s time step is fine to use.
		\begin{table}
			\begin{center}
				\begin{tabular}{ |c||c|c|c|c|c|c|c|}
					\hline
					&1. & 2. & 3. & 4. & 5. & 6. & 7.\\
					\hline
					$\Delta$t[s]& $0.4$ & $0.2$ & $0.1$ & $0.05$ & $0.025$ & $0.0125$ & $0.00625$\\
					\hline
				\end{tabular}
			\end{center}
			\caption{Step sizes}
			\label{tab:timestepsizes}
		\end{table}
		\begin{figure}
			\centering
			\begin{minipage}{.5\textwidth}
				\centering
				\includegraphics{eemobil/timestepi_p}
				\caption{Position of car 1}
				\label{fig:tsip}
			\end{minipage}\hfill
			\begin{minipage}{.5\textwidth}
				\centering
				\includegraphics{eemobil/timestepi_v}
				\caption{Velocity of car 1}
				\label{fig:tsiv}
			\end{minipage}
		\end{figure}
		\begin{figure}
			\centering
			\includegraphics{eemobil/timestepi_t}
			\caption{Time step independence. Elapsed time for 100 meters for car 1}
			\label{fig:tsit}
		\end{figure}
		\begin{figure}
			\centering
			\begin{minipage}{.5\textwidth}
				\centering
				\includegraphics{eemobil/timestepi_relative_p}
				\caption{Relative error of position}
				\label{fig:tsipr}
			\end{minipage}\hfill
			\begin{minipage}{.5\textwidth}
				\centering
				\includegraphics{eemobil/timestepi_relative_v}
				\caption{Relative error of velocity}
				\label{fig:tsivr}
			\end{minipage}
		\end{figure}
		\section{Simulator improvements}
		The simulator works as expected. It is capable of simulating a multi lane traffic with lane changes. But there are some human driver behaviors that can be modeled slightly better, thus making the simulator more realistic.
		\subsection{Improvement I}
		As mentioned previously the duration of a lane change is not modeled or implemented yet. However it must be implemented since it probably has a great impact on the overall traffic outcome. It makes the simulator a bit more reasonable too. The actual duration of the lane change should be defined as a driver specific parameter in seconds. A typical value is 2 to 4 seconds. After a lane change has started (by filling the corresponding row of the target lane vector) the simulator is checking the elapsed time in every time step for the lane changing vehicle. The simulator will end the lane change when the elapsed time has exceeded the lane change duration. To see the effect of this improvement the same configuration was simulated with the same initial conditions as before, only the newly introduced lane change duration parameters were added. See Table~\ref{tab:lane_change_duration}.
		\begin{table}
			\begin{center}
				\begin{tabular}{ |c|c|c| }
					\hline
					Id & ... & Lane change duration\\
					$[-]$ & & [s]\\
					\hline
					1 & ... & 2.0\\
					2 & ... & 2.5\\
					3 & ... & 4.0\\
					4 & ... & 3.4\\
					5 & ... & 2.9\\
					6 & ... & 3.3\\
					7 & ... & 4.0\\
					8 & ... & 3.2\\
					9 & ... & 2.3\\
					1 & ... & 3.9\\
					\hline
				\end{tabular}
			\end{center}
			\caption{Initial conditions of Case 2}
			\label{tab:lane_change_duration}
		\end{table}

		Results of the simulation can be seen in Figure~\ref{fig:red_light_situationh_impr1}, \ref{fig:red_light_situationv_impr1} and \ref{fig:red_light_situationcc_impr1}. After implementing this improvement the overall duration has increased to \casetime{2}seconds. It can be seen clearly that the number of lane changes has increased. At the end however the number of cars in each lane has not changed.
		\begin{figure}
			\centering
			\includegraphics[width=0.8\textwidth]{eemobil/simh_case2}
			\caption{Red light traffic. Headway of the vehicles respect to their position in lanes}
			\label{fig:red_light_situationh_impr1}
		\end{figure}
		\begin{figure}
			\centering
			\includegraphics[width=0.8\textwidth]{eemobil/simv_case2}
			\caption{Red light traffic. Velocity of the vehicles respect to their position in lanes}
			\label{fig:red_light_situationv_impr1}
		\end{figure}
		\begin{figure}
			\centering
			\includegraphics[width=0.8\textwidth]{eemobil/simcc_case2}
			\caption{Red light traffic. Number of cars in lanes }
			\label{fig:red_light_situationcc_impr1}
		\end{figure}
		\subsection{Improvement II}
		Currently the acceleration value of a vehicle during a lane change is calculated by its leader only. However in a real situation a driver changes lane because he would like to accelerate. So instead of calculating the acceleration based on the current leader only, both the current and the future leader accelerations should be taken into account. A reasonable approximation could be that the car's current acceleration is the weighted average of its current and future lane acceleration value. The mathematical expression of this improvement would look like:
		\begin{equation}
			a=a_{\rm c} \cdot \left(1-\frac{t_{\rm e}}{t_{\rm d}}\right) + a_{\rm f}\cdot \frac{t_{\rm e}}{t_{\rm d}}\,,
			\label{eq:weighted_average}
		\end{equation}
		where $a_{\rm c}$ is the acceleration considering only the current leader vehicle, $a_{\rm f}$ is the acceleration considering only the future leader vehicle. $t_e$ and $t_d$ are the elapsed and total time of the lane change. Unfortunately this improvement only makes sense if the previously defined lane changing duration is active as well. Based on Eq.~\eqref{eq:weighted_average} the improvement has been implemented in the simulator. This feature has been added with a toggle if one wishes to turn it off in the future.

		Results of the simulation can be seen in Figure~\ref{fig:red_light_situationh_impr2}, \ref{fig:red_light_situationv_impr2} and \ref{fig:red_light_situationcc_impr2}. After implementing this improvement the overall duration has increased to \casetime{3}seconds. Both the headway and velocity figures are similar to the previous ones. The difference is marginal. But the lane change figure has changed. The first part of the figures are identical but second part has changed. The last lane change could not happen because of this improvement.
		\begin{figure}[ht]
			\centering
			\includegraphics[width=0.8\textwidth]{eemobil/simh_case3}
			\caption{Red light traffic. Headway of the vehicles respect to their position in lanes}
			\label{fig:red_light_situationh_impr2}
		\end{figure}
		\begin{figure}
			\centering
			\includegraphics[width=0.8\textwidth]{eemobil/simv_case3}
			\caption{Red light traffic. Velocity of the vehicles respect to their position in lanes}
			\label{fig:red_light_situationv_impr2}
		\end{figure}
		\begin{figure}
			\centering
			\includegraphics[width=0.8\textwidth]{eemobil/simcc_case3}
			\caption{Red light traffic. Number of cars in lanes }
			\label{fig:red_light_situationcc_impr2}
		\end{figure}
		\subsection{Improvement III}
		The other area which can be developed is related to the attention of the drivers to the road. The model currently assumes that everyone is paying attention to the driving always. This is not the case in real life at all. There are several factors which can distract the driver's attention. Some of these activities could be eating, interacting with radio or gps, looking at a roadside object or messaging on the phone. These activities most likely to have an effect on the traffic so it is worth to implement. The model will assume that if a driver is distracted with something then it cannot take any attention to its longitudinal or its transversal motions. So the car will go ahead with the same speed and zero acceleration when it is nobody ahead and even if its leader is braking. The actual implementation is quite simple. A new array is introduced which will contain the time intervals. These represent the time when a driver is not paying attention to driving. Whenever a driver is not paying attention the simulator will keep its speed.

		 Another problem is that driving - as discussed earlier - consists two parts, longitudinal and transversal motions. Probably drivers have to divide their attention and focus on only one at a time. It seems right to say that everyone is capable of paying attention to longitudinal and transversal motions at the same time when the speed is moderated. However when a driver is accelerating or decelerating above or below a certain point he will not care about changing lanes. This can be expressed with the  following inequality and can be included in the lane changing algorithm:
		\begin{equation}
			|a|<a_{\rm p,tr}\,.
			\label{eq:attention_acceleration}
		\end{equation}
		If Eq.~\eqref{eq:attention_acceleration} holds then the driver is capable of paying attention to lane changing otherwise he cannot. The current acceleration value is represented by $|a|$ and $a_{\rm p,tr}$ is threshold value which is typically between 0 and $\amax$ for a human driver.

		Results of the simulation can be seen in Figure~\ref{fig:red_light_situationh_impr3}, \ref{fig:red_light_situationv_impr3} and \ref{fig:red_light_situationcc_impr3}. After implementing this improvement the overall duration has increased to \casetime{4}seconds. With this improvement the headway and velocity figures have changed significantly. The time intervals when a driver is not paying attention to driving can be seen in Figure~\ref{fig:red_light_situationv_impr3}. The horizontal parts of a curve (where $\dot{v}=0$) show that a driver is probably distracted from driving.
		\begin{figure}
			\centering
			\includegraphics[width=0.8\textwidth]{eemobil/simh_case4}
			\caption{Red light traffic. Headway of the vehicles respect to their position in lanes}
			\label{fig:red_light_situationh_impr3}
		\end{figure}
		\begin{figure}
			\centering
			\includegraphics[width=0.8\textwidth]{eemobil/simv_case4}
			\caption{Red light traffic. Velocity of the vehicles respect to their position in lanes}
			\label{fig:red_light_situationv_impr3}
		\end{figure}
		\begin{figure}
			\centering
			\includegraphics[width=0.8\textwidth]{eemobil/simcc_case3}
			\caption{Red light traffic. Number of cars in lanes }
			\label{fig:red_light_situationcc_impr3}
		\end{figure}


\chapter{Evaluation Data Generation}
\label{chap:appendixevaldatagen}
The aim of this appendix chapter is to provide some further information and insight about how we generated the data for our evaluation graphs and how we plotted them discussed in chapter 5. For this purpose we first explain how we discretized the input data in order to compute the BRDF values. This is followed by discussing the algorithm how we plotted our graphs as illustrated in algorithm $\ref{alg:evalmatlab}$. \\

In our evaluation Java program (as mentioned in section $\ref{sec:evalprecomp}$) we fixed the incident light angle $\theta_i$$\footnote{In practice, we set the $\theta_i$ equal to $75 \degree$.}$. This allows us to remove the argument $\theta_i$ from our BRDF function. Our java program computes each $BRDF$ function at a given discrete wavelength-viewing-angle grid, denoted by $[\Lambda, \Theta]$. The wavelength space $\Lambda = [\lambda_{min}, \lambda_{max}]$ and the viewing angle range $\Theta = [\alpha_{min}, \alpha_{max}]$ of our free parameter $\theta_r$ are discretized using equidistant steps. The step sizes, denoted by $(\lambda_{step}, \alpha_{step})$, are provided as input arguments for our Java evaluation program. \\

Next, let us have a closer look at how our discrete $[\Lambda, \Theta]$ grid is constructed. The wavelength space $\Lambda$, which is ranging from $\lambda_{min}$ to $\lambda_{max}$, is discretized like the following:

\begin{equation}
\Lambda = \{\lambda = \lambda_{min} + k \cdot \lambda_{step} | k \in \{0,..,steps_{\lambda}-1\}\}
\label{eq:lambdaspacesetup}
\end{equation}

where $steps_{\lambda} = \ceil{\frac{\lambda_{max}-\lambda_{min}}{\lambda_{step}}}$. We similarly discretize the viewing angle space $\Theta$ by setting a minimal and maximal viewing-angle boundary $\alpha_{min}$ and $\alpha_{max}$. Then $\ceil{\frac{\alpha_{max} - \alpha_{min}}{\alpha_{step}}}$ is the number of angle $steps_{\alpha}$. And thus, our $\Theta$ space it defined like the following:

\begin{equation}
\Theta = \{\alpha = \alpha_{min} + k \cdot \alpha_{step} | k \in \{0,..,steps_{\alpha}-1\}\}
\label{eq:thetaspacesetup}
\end{equation}

Then, every BRDF java function is applied to the grid $[\Lambda, \Theta]$ and the resulting spectral response is stored in a matrix
\begin{equation}
R = \{BRDF(\lambda_i, \theta_{r}^{j}) | i \in Index(\Lambda), \quad j \in Index(\Theta)\}
\label{eq:responsematrix}
\end{equation}

The generation process of the evaluation plots, which we discuss in section $\ref{sec:virtualtestbench}$, is described in algorithm $\ref{alg:evalmatlab}$. This algorithm takes the matrix R from equation $\ref{eq:responsematrix}$ as input argument. For the maximal reflectance of our methods at any wavelength, it computes the corresponding peak viewing angles and compares it to the angle resulting from the grating equation.

\begin{algorithm}[H]
\caption{BRDF Evaluation Graph Plotter}
\begin{table}[H]
  \begin{tabular}{@{}lll@{}}
    \textbf{Input:} & $R$ Matrix with $BRDF$ intensity values of $(\Lambda, \Theta)$ grid \\
    & $\Lambda$ discretized wavelength space used to compute $R$ \\
    & $(\alpha_{min})$ minimum value of viewing angle space $\Theta$ \\
    & $(\alpha_{step})$ discretization level of viewing angle space $\Theta$ \\
    & $d$ estimated periodicity of height field \\
    & $\theta_i$ fixed incident angle \\
    \textbf{Procedure:} & $getMaxIntensGridPointsOf(matrix,r):$ get the column-index of the largest  \\
    & \quad \quad \quad \quad \quad \quad \quad \quad \quad \quad \quad \quad \quad \quad \quad \quad \quad \quad intensity value in the row $matrix(r,*)$\\
    &$plotPoint(x,y)$: draw a point at $(x,y)$ \\
    \textbf{Output:} & Evaluation Plot of given BRDF model applied on given height field \\
  \end{tabular}
\end{table}
\setlength{\fboxrule}{0pt}
\begin{boxedminipage}{1.0\textwidth}
  \begin{algorithmic}[1]
    \ForAll{$\lambda_k \in \Lambda$}
      \State $\widetilde{\alpha} = getMaxIntensGridPointsOf(R, \lambda_k)$
      \Comment{$\widetilde{\alpha}$ $\equiv$ index viewing angle of max. R}
      \State $\widetilde{\alpha}_{r_k} = \alpha_{min} + \alpha_{step} \cdot \widetilde{\alpha}$
      \State $\theta_{r_k} = asin\left( \frac{\lambda}{d} - sin(\theta_i) \right)$
      \State $plotPoint(\lambda_k, \widetilde{\alpha}_{r_k})$
      \Comment{graph resulting from our BRDF model}
      \State $plotPoint(\lambda_k, \theta_{r_k})$
      \Comment{graph resulting from grating equation}
    \EndFor
  \end{algorithmic}
  \end{boxedminipage}
  \vskip1.5pt
\label{alg:evalmatlab}
\end{algorithm}

Algorithm $\ref{alg:evalmatlab}$ iterates over the wavelength space $\Lambda$ and generates our evaluation plots. For any wavelength it computes the viewing angle with maximum reflectance and the angle resulting from the grating equation as defined in equation $\ref{eq:gratingeq}$. Both angles are then plotted for the current wavelength in the current iteration. In the next section we will discuss the generated evaluation plots. The reason for discussing this algorithm is to offer the reader a complete discussion about the whole evaluation process.


\section{New Results}

\begin{itemize}
\item We show a new result for the relativistic normal form factor
      for hydrogenic atoms in equation~\ref{eq:nff-relativistic}.
\item We show that for low Z atoms (non-relativistic limit)
      the relativistic normal form factor reduces to the non
      relativistic normal form factor for hydrogen in equation~\ref{eq:reduction}.
\item We compare and look at the differences between the new relativistic
      form factor and the non relativistic theory~\ref{eq:nff-nonrelativistic}
      and an alternate theory (Hubbell) and graphically
      show the differences in figures~\ref{fig:delta-theory}
      and~\ref{fig:hubbell-comparison}

\item We show semi-analytic results for the first order relativistic
photoabsorption amplitude for photons polarised in both the $x$ and $y$
directions for the all poles approach (equations~\ref{eq:A1-X}
and~\ref{eq:A1-Y})
and for the electric dipole approximation (equation~\ref{eq:A1-Dipole}).

\item We show a new complete analytic result for the first order
photoabsorption amplitude in the forward scattering direction
(equation~\ref{eq:A1-Forward}).

\item We show a result for the second order S-matrix amplitude for the
      all poles (equation~\ref{eq:A2}) and electric dipole
      approximation (equation ~\ref{eq:A2-Dipole}).

\item We present a result in figure~\ref{fig:compare-poles} for the normal form factor $f''$
      in both the all poles approach and the electric dipole approximation
      over a range of energies.

\item We compare the results obtained for $f''$ near an absorption edge for
      hydrogen with other theories (see figure~\ref{fig:fpp-compare}).

\item We show results for angular the angular distribution of ejected
      photoelectrons at selected energies (see figure~\ref{fig:angular-plots}).

\end{itemize}

\section{Further Work In This Area}
    The work presented in this thesis can be expanded in a number of different
    ways. Firstly, the integration techniques used in the S-matrix theory can
    be improved to hopefully provide more accurate results.
    Secondly, the first order photoionisation amplitudes can be used
    to calculate a first order photo-electric cross section which
    can then be used to calculate $f''$, which may then be compared
    to the results obtained with the second order S-matrix theory.
    Thirdly, the cross section components due to bound-bound transitions,
    and other effect such as Delbr\"uck and Compton scattering and pair
    production should be added to the existing photoionisation
    cross section.

    Longer term research in the area of relativistic atomic form factors
    can include calculations for molecular hydrogen, bound hydrogen, and a study
    of the multiple scattering processes which give rise to XAFS (X-Ray
    Anomalous Fine Structure).
    Additionally, the development form factor calculations for many electron
    atoms ranging from Helium to Uranium making use of numerical
    relativistic Dirac-Hartree-Fock wave-functions is also of great interest.


\vsssub
\subsubsection{~$S_{bot}$: \showex\ bottom friction} \label{sec:BT4}
\vsssub

\opthead{BT4}{Crest model}{F. Ardhuin}

\noindent
A more realistic parameterization for sandy bottoms is based on the eddy
viscosity model by \cite{art:GM79} and a roughness parameterization that
includes the formation of ripples and transition to sheet flow. The
parameterization of \cite{tol:JPO94}, was adjusted by \cite{art:Aea03a} to
field measurements from the DUCK'94 and SHOWEX experiments on the North
Carolina continental shelf. The parameterization has been adapted to \ws\ by
also including a sub-grid parameterization for the variability of the water
depth, as given by \cite{tol:CE95}. This parameterization is activated by the
switch BT4.

The source term can be written as

%------------------------%
% SHOWEX bottom friction %
%------------------------%
% eq:SHOWEX_bot

\begin{equation}
\cS_{bot}(k,\theta) = - f_e u_b \: \frac{\sigma^2}{2 g \sinh^2(kd)} \: N(k,\theta)
\: , \label{eq:SHOWEX_bot}
\end{equation}

\noindent
where $f_e$ is a dissipation factor that is a function of the r.m.s. bottom orbital
displacement amplitude $a_b$ and the Nikuradse roughness length $k_N$, and
$u_b$ is the r.m.s. of the bottom orbital
velocity.

The present bed roughness parameterization
(\ref{eq:SHOWEX_kr})--(\ref{eq:SHOWEX_krr}) contains seven empirical
coefficients listed in Table \ref{tab:BT4}.

\begin{table} \begin{center}
\begin{tabular}{|l|c|c|c|c|c|} \hline \hline
Par.         &  WWATCH var.           & namelist &  SHOWEX  &  \cite{tol:JPO94} \\
\hline
  $A_1$ &  RIPFAC1                    & BT4 & 0.4     & 1.5    \\
  $A_2$ &  RIPFAC2                    & BT4 & -2.5    & -2.5   \\
  $A_3$ &  RIPFAC3                    & BT4 & 1.2     & 1.2     \\
  $A_4$ &  RIPFAC4                    & BT4 & 0.05    & 0.0     \\
  $\sigma_d$ &  SIGDEPTH              & BT4 & 0.05    & user-defined  \\
  $A_5$ &  BOTROUGHMIN                & BT4 & 0.01    & 0.0     \\
  $A_6$ &  BOTROUGHFAC                & BT4 & 1.00    & 0.0     \\
\hline
\end{tabular} \end{center}
\caption{Parameter values for the SHOWEX bottom friction (default values) and the original
  parameter values used by \cite{tol:JPO94}. Source term
  parameters can be modified via the BT4 namelist. Please
  note that the name of the variables only apply to the namelists. In the source
  term module the seven variables are contained in the array SBTCX. } \label{tab:BT4}
\botline
\end{table}

The roughness $k_{N}$ is decomposed in a ripple roughness $k_{r}$ and
a sheet flow roughness $k_{s}$,
\begin{eqnarray}
k_{r} &=&a_{b}\times A_{1}\left( \frac{\psi }{\psi _{c}}\right) ^{A_{2}}, \label{eq:SHOWEX_kr}\\
k_{s} &=&0.57\frac{u_{b}^{2.8}}{\left[ g\left( s-1\right) \right] ^{1.4}}%
\frac{a_{b}^{-0.4}}{\left( 2\pi \right) ^{2}}\label{eq:SHOWEX_ks}.
\end{eqnarray}
In Eqs. (\ref{eq:SHOWEX_kr}) and (\ref{eq:SHOWEX_ks}) $A_1$ and $A_2$ are empirical constants, $s$ is the
sediment specific density, $\psi$ is the Shields number determined from $u_b$
and the median sand grain diameter $D_{50}$,
\begin{equation}
\psi =f_{w}^{\prime }u_{b}^{2}/\left[g\left( s-1\right) D_{50}\right],
\end{equation}
with $f_{w}^{\prime }$ the friction factor of sand grains (determined in the
same way as $f_e$ with $D_{50}$ instead of $k_r$ as the bottom roughness), and
$\psi _{c}$ is the critical Shields number for the initiation of sediment
motion under sinusoidal waves on a flat bed.  We use an analytical fit
\citep{bk:Soul97}
\begin{eqnarray}
\psi _{c} &=&\frac{0.3}{1+1.2D_{*}}+0.055\left[ 1-\exp \left(
-0.02D_{*}\right) \right]\label{Soulsby_psic} , \\
D_{*} &=&D_{50}\left[ \frac{g\left( s-1\right) }{\nu ^{2}}\right] ^{1/3},
\end{eqnarray}
where $\nu $ is the kinematic viscosity of water.

When the wave motion is not strong enough to generate vortex ripples, i.e.
for values of the Shields number less than a threshold $\psi _{{\rm rr}}$,
$k_{N}$ is given by a relic ripple roughness $k_{{\rm rr}}$. The threshold is
\begin{equation}
\psi _{{\rm rr}}=A_{3}\psi _{c}.
\end{equation}
Below this threshold, $k_{N}$ is given by
\begin{equation}
k_{{\rm rr}}=\max \left\{ A_5 {\rm m,} A_6 D_{50}, A_{4}a_{b}\right\}
{\rm for\ }\psi <\psi _{{\rm rr}}.\label{eq:SHOWEX_krr}
\end{equation}


\subsection{Stochastic evolution}

We can describe processes by their evolution.

\(p(x_t|x_{t-1}...)\)


\chapter{Logistic Regression}

In the workflow above we have used logistic regression, a popular machine learning method. It is often used in text mining for its speed and predictive performance. How does it work?

\marginnote{This visualization is called a nomogram. It displays scores (votes) of each attribute for the selected target value at the top left.}It lets the words vote. For example, the word 'fox' in the text votes for the tale being an animal tale. So do cats and birds and wolves, but not so strongly (lines in the visualization are much shorter). We can see that the word 'fox' is the best clue for the text being an animal tale.

\vspace{-0.2cm}
\begin{figure*}[h]
  \centering
  \includegraphics[width=0.8\linewidth]{workflow.png}%
  \caption{$\;$}
\end{figure*}
\vspace{-0.3cm}

The word 'little' votes for the opposition. So does 'came' (see how they have zeros at the right end of the scale?). The more little things there are in a story, the less likely it is an animal tale.

\marginnote{In Nomogram, you can interactively observe the model's classification. Drag the blue dots left or right so that the accumulated sum of points (Total) is as high as possible.}Each word gives a score. If there are 29 foxes in the text, then the model will give it 3 points for being an animal tale. And if there are no words 'little', it will give it an additional 4.5 points.

Of course the real method is a bit more complicated - it tries to find appropriate vote weights and thresholds. But this involves some linear algebra and other scarier-than-wolves words, so let's not stroll down this path.

\vspace{-0.2cm}
\begin{figure*}[h]
  \centering
  \includegraphics[width=0.8\linewidth]{nomogram.png}%
  \caption{$\;$}
\end{figure*}
\vspace{-0.3cm}