\newpage
\section{Decision Trees}\label{s:decision-trees}

Decision trees are tree-like structures in which each internal node represents a ``test" on an attribute, each branch represents the outcome of the specific test, and each leaf node represents a class label (decision taken after computing all attributes).
More specifically, decision trees depict models of decisions and their possible results.
They are also a simple way to display an algorithm that only contains conditional control statements.
Starting from the root node and following the paths to leaves, each path represents classification rules.

For instance, the attribute ``Gender" would have two edges leaving it, one for ``Male" and
one for ``Female".
Each edge could point to a new attribute (for example ``Age Groups"), and so on.
The leaves of the tree contain the expected class value for transactions matching the path from the root to that leaf.
Given a decision tree, one can predict the class of a new transaction just by following the ``correct" path, which will result in a class label.
He\myslash She can predict the class of a transaction by viewing only the non-class attributes (\textit{i.e.} the ``constructed" decision tree).

For instance, two friends wish to decide if they will go to play tennis outside, depending the weather conditions.
They also remember the previous days that some weather conditions did not allow them to play.
Thus, they decide to create a decision tree to assist them.
First they define the class attribute to be ``Play Tennis", and three more attributes that will help them decide: ``Outlook" (``Sunny", ``Overcast", ``Rainy"), ``Windy" (``High", ``Low") and ``Humidity" (``High", ``Low").
Then they construct a decision tree like figure \ref{f:outlook} that corresponds to the data that the have collect from the past days.

\begin{figure}[th]
  \centering
  \includegraphics[width=0.8\linewidth]{figures/outlook.pdf}
  \caption{Play-Tennis decision tree example}\label{f:outlook}
\end{figure}

From figure \ref{f:outlook}, the two friends are able to extract a set of rules, listed in Code \ref{sc:outlook-rules}, that will help them decide if they should go play tennis given the weather conditions.

{
\begin{minted}[xleftmargin=21pt, framesep=3mm, frame=single, linenos, tabsize=2, breaklines, breaksymbolleft=, fontsize=\footnotesize]{bash}
IF (Outlook = Sunny) AND (Windy = Low) THEN Play=Yes

IF (Outlook = Sunny) AND (Windy = High) THEN Play=No

IF (Outlook = Overcast) THEN Play=Yes

IF (Outlook = Rainy) AND (Humidity = High) THEN Play=No

IF (Outlook = Rainy) AND (Humidity = Low) THEN Play=Yes
\end{minted}
\captionof{lstlisting}{Rules for decision tree from figure \ref{f:outlook}}
\label{sc:outlook-rules}
}

Of course, remembering the weather conditions of more days in the past and if they had played tennis those days -- having an extended dataset -- could probably result to more specific rules.
Decision tree rules, help covering all possible future decisions\hyp transactions and can be used to classify them.
There are many useful examples and it has become evident why this type of learning has become so popular.
The algorithms according to which those tress are generated vary and lead to possible different trees.

One famous algorithm for decision tree construction is ID3 (Iterative Dichotomiser 3).
ID3 is the precursor to the C4.5 algorithm, and is typically used in the machine learning and natural language processing domains.
Both algorithms build decision trees from a set of training data, using the concept of information entropy, however, C4.5 results in better classification utilizing a more efficient splitting criterion.

\subsection{Textbook ID3}\label{s:id3}

The ID3 algorithm was first introduced in \cite{quinlan1986induction} and assumes that each attribute is categorical, such as the aforementioned attribute ``Age Groups" which is separated in four disjoint categories; ``Infant", ``Adolescent", ``Child" and ``Adult".

ID3 algorithm constructs the classification tree top-down in a recursive fashion.
The idea is to find the \textit{best} attribute that classifies the transactions.
At start, the algorithm searches and chooses the best attribute for the root node, and consecutively the remaining transactions are partitioned by it.
On each partition, ID3 is then recursively called.

The \textit{best} attribute is defined as the attribute that has the smallest entropy -- or in other words, the best information gain.
Let $T$ be a set of transactions, $C$ the class attribute and $A$ some non-class attribute.
On each iteration, ID3 iterates through every unused attribute $A$ of the dataset and calculates the entropy $H_C(T)$ (equation \ref{eq:entropy} and algorithm \ref{a:id3-entropy-simple}) for every subset $T$ that results from splitting the dataset on attribute $A$.

\import{./}{algorithms/entropy_textbook.tex}

\begin{equation}\label{eq:entropy}
  H_C(T) = \sum_{i=1}^{l} - \frac{\mid T(c_i) \mid}{\mid T \mid} log{\frac{\mid T(c_i) \mid}{\mid T \mid}}
\end{equation}

The idea is to identify the class of a transaction $t$, given that the value of $A$ has been obtained.
Let $A$ obtain values $a_1, \dots, a_m$ and let $T(a_j)$ be the transactions obtaining value $a_j$ for $A$.
Then, the conditional information of $T$ given $A$, is given from equation \ref{eq:TgivenA}.

\import{./}{algorithms/gain_textbook.tex}

\import{./}{algorithms/best_textbook.tex}

\begin{equation}\label{eq:TgivenA}
  H_C(T \mid A) = \sum_{j=1}^{m} - \frac{\mid T(a_j) \mid}{\mid T \mid} H_C(T(a_j))
\end{equation}

\begin{equation}\label{eq:gain}
  Gain(A) = H_C(T) - H_C(T \mid A)
\end{equation}

Finally, the set $T$ is then split by the selected attribute $A$ (best-attribute algorithm \ref{a:id3-best-simple}) that has the maximum gain (equation \ref{eq:gain} and algorithm \ref{a:id3-gain-simple}) -- or equivalently minimum $H_C(T \mid A)$ -- to produce subsets of the data.
The algorithm continues to recurse on each subset, considering only attributes never selected before.

The \f{AllExamplesSame} (see algorithm \ref{a:id3-same-simple}) procedure is used to determine if all transactions are of the same class, by checking the value of $classAttribute$ for each transaction.
It returns $true$ if they are, $false$ otherwise.

\import{./}{algorithms/allsame_textbook.tex}

The \f{MostCommonLabel} (algorithm \ref{a:id3-most-common-label-simple}) procedure returns the class label that is most common in all available transactions.

\import{./}{algorithms/most_common_label_textbook.tex}

The recursive \f{ID3} algorithm (see algorithm \ref{a:id3-simple} shown below) has the following structure with three halting conditions.
\begin{enumerate}
  \item If the set of remaining attributes is empty, then the algorithm returns the label that is most common in all transactions as a leaf (lines 2-3).
  \item If all transactions are of the same class, then the algorithm returns this class as a leaf (lines 4-5).
  \item If none of the above conditions hold, then the algorithm finds the best splitting attribute (using algorithm \ref{a:id3-best-simple}) and makes a branch for every possible value of that attribute (lines 7-19).
\end{enumerate}

\import{./}{algorithms/id3_textbook.tex}

\subsection{Privacy Preserving ID3}\label{s:pp-id3}

In the previous subsection we described the ID3 algorithm which operates on public data.
We should now describe the privacy\hyp preserving version of the same algorithm, where all the transactions (\textit{examples}) from which the algorithm builds the tree are private data.

First of all, a key difference from the textbook algorithm is the lack of ability to maintain subsets of the transactions based on private conditions.
In particular, as you can see in line 11 of algorithm \ref{a:id3-simple}, we wish to keep a subset of all $examples$ that have a certain value in the $bestAttribute$
 column.

As we cannot have conditional statements on private data, the only thing we can do is to apply the oblivious selection technique described in section \ref{s:secrec}.
Thus, we are not able to keep only the subset of rows that satisfy our condition.
A solution to this problem is to define $subset$ as a copy of the $examples$ array.
We keep a vector $eq$ (see line 17 of algorithm \ref{a:id3-pp}) of equal length with $examples$.
The value of $eq[i]$ is $\enc{1}$ if $examples[bestAttribute]$ is equal to $v_i$, or $\enc{0}$ otherwise.
Since the values of $eq$ are encrypted, we perform a vector multiplication between $eq$ and $examples$ so the result will be identical to $examples$ for ever row $i$ that the condition holds ($eq[i]$ is equal to $\enc{1}$), and $\enc{0}$ otherwise.
Then we add a dummy row $[\enc{-1}, \enc{-1}, \dots, \enc{-1}]$ (a row of all $\enc{-1}$, denoted as just $\enc{-1}$ for simplicity) to all rows that don't satisfy the condition (using multiplication with $\enc{1} - eq$).
The resulting array has all rows from $examples$ that satisfy the condition, and rows full of $\enc{-1}$ wherever the condition does not hold.

For that reason, all the algorithms listed below operate on these kinds of ``sets''.
In algorithm \ref{a:id3-count-positives-pp} we describe the \f{CountPositives} procedure which returns the (encrypted) number of rows that are not the dummy $[\enc{-1}, \enc{-1}, \dots, \enc{-1}]$ row.
The \f{CountPositives} procedure is equivalent to the call of $Length$ on sets with public data.

\import{./}{algorithms/count_positives.tex}

The \f{AllExamplesSame} procedure described in algorithm \ref{a:id3-same-pp} returns the same result as that in algorithm \ref{a:id3-same-simple}.
The way this is achieved is by counting the number of examples\myslash transactions that are of class $c_i$, for each possible class $c_i$.
The private variable $res$ is initialized to $\enc{0}$, and gets increased if any of those counts is equal to the total number of examples, \textit{i.e.} all examples are of that class.
If $res$ is greater than $\enc{0}$ (equal to $\enc{1}$) then all the examples have the same class.

\import{./}{algorithms/allsame_pp.tex}

Similarly to the \f{AllExamplesSame} procedure, the \f{MostCommonLabel} procedure also maintains the transaction counts for each possible class $c_i$.
Using the technique described in algorithm \ref{a:max}, we obliviously keep the maximum count and the class label corresponding to that count, which is returned.

\import{./}{algorithms/most_common_label_pp.tex}

The \f{Best} procedure obliviously chooses the attribute that has the greatest information gain.
This attribute is considered to be the best to split the dataset on an will be included in an output tree node.
That is why this attribute can be handled as public data (note the usage of the \f{Declassify} operator in line 10) after it has been privately computed.

\import{./}{algorithms/best_pp.tex}

As in algorithms \ref{a:id3-entropy-simple} and \ref{a:id3-gain-simple}, procedures \f{Entropy} and \f{InformationGain} (algorithms \ref{a:id3-entropy-pp}, \ref{a:id3-gain-pp}) compute the entropy of a set of transactions, and an attribute's information gain respectively.
The key differences with the previously described textbook algorithms include the usage of \f{CountPositives} procedure instead of \f{Length}, due to the difference in the sets involved in the computation, and the modified $log_2$ function that handles zero input.
Also, note that the subsets (algorithm \ref{a:id3-gain-pp}, line 5) are also constructed with the technique described in the beginning of this section.

\import{./}{algorithms/entropy_pp.tex}
\import{./}{algorithms/gain_pp.tex}

The complete \f{ID3} procedure is described in algorithm \ref{a:id3-pp}.
In lines 6-11 of the algorithm we retrieve class label that is same to all examples ad return it.
In order to do that, we cannot just take a random's example value in the $classAttribute$ column.
We should make sure that this is not a dummy ($\enc{-1}$) example (line 8).
The rest of the algorithm is quite similar to the textbook one.
The differences mainly include the subsets handling. (creation, counting, etc.)

\import{./}{algorithms/id3_pp.tex}

\subsubsection{Privacy Assessment}\label{s:id3-privacy-assessment}

In privacy-preserving algorithms, all intermediate values should remain private.
In many cases, intermediate values may reveal some patterns, and in general sensitive information about the private inputs.

However, in the case of ID3, some of these intermediate values are part of the output and eventually will be revealed.
For instance, the attributes chosen in each node of the tree will be revealed in the final results.
Thus, there is no reason to trying to protect them during the protocol execution.
In the privacy\hyp preserving ID3, as in all algorithms in this thesis, we explicitly define which values are private (and thus encrypted), and which are public.

As stated in \cite{lindell2000privacy}, although the name of the attribute with the highest information gain is revealed, nothing is learned of the actual $H_C(T \mid A)$ values themselves.
This observation holds true for all such cases, since neither the information gain nor any other intermediate values which are not explicitly denoted as public can be leaked.

For an algorithm to remain private, the requirement is that any information is learned by the algorithm, can also be learned directly from the public input and output \cite{lindell2000privacy}.

As mentioned in section \ref{s:secrec} the \f{Declassify} operator publishes a private value.
Consecutively, we reason about the selection of public and private data in the described algorithms and also about the usage of the \f{Declassify} operator.

First of all, we consider the case that all involved parties have the same schema for their data.
Thus, attribute names are known to all parties an should not be considered private information.

Lindell and Pinkas have constructed a protocol in \cite{lindell2000privacy} for privately computing ID3 that works in the two\hyp party setting.
This two\hyp party protocol is proven to be private.
However that algorithm does not consider the case in where the set of transactions $T$ is empty. Algorithm \ref{a:id3-pp} is a generalization to the multi\hyp party setting from the 2\hyp party protocol that is described in \cite{lindell2000privacy}.
The algorithm also addresses the empty transaction set case.

The ID3 algorithm (see algorithm \ref{a:id3-pp}) can terminate (\textit{i.e.} return a value) in three different cases.
Each time, the algorithm's output is a node of the resulting tree.
That is why, everything that the algorithm returns in any of the three cases should be considered as public data (see \f{Declassify} operator in lines 9, 11 and public $branches$ variable in line 14).

\textbf{Empty attribute set:}
The first termination channel of the algorithm is when the set of remaining attributes is empty, \textit{i.e.} when there are no attributes left for the algorithm to check..
This information is publicly known since the remaining attributes can be derived from the output tree.
In this case, the algorithm returns the most common label of the remaining transactions.
Here, we use the \f{Declassify} operator to publish that label and add it as a leaf to the tree.

\textbf{All examples same:}
The second way that algorithm \ref{a:id3-pp} can terminate, is when all transactions of the dataset are of the same class.
We use the \f{Declassify} operator to determine that information (\textit{i.e.} if all the dataset is of the same class).

\textbf{After recursive calls:}
Finally, if none of the above cases terminate the execution of ID3, the algorithm will recursively add either a leaf or a sub\hyp tree for each possible value of the selected attribute, and then return.
In order to determine whether to add a leaf or a sub\hyp tree, we publish the information that there are no transactions left in the subset of the dataset that corresponds to a particular value of the selected attribute.

By observing the output tree, as well as the public input, one can make the following observations.
Wherever there is a leaf node with class $c_i$, one can tell if the set of remaining attributes is empty, simply by observing the path from the root of the tree.
If all attributes have been used then the set is empty, otherwise it is not.
If the attribute set is not empty, we can deduce that one of two things holds.
Either all transactions that belong to the current path are of the same class ($c_i$), or that there are no transactions at all in that path.
These exact information is also published by algorithm \ref{a:id3-pp}, with the only difference being that the information published by the algorithm distinguishes between when all transactions are of the same class and the corner case of having no transactions for a particular path of the tree.


\documentclass[11pt]{article}
\usepackage[margin=1in]{geometry}
% Math packages.
\usepackage{amsmath,amssymb,amstext,amsfonts}
% Add figures.
\usepackage{graphicx}

% Metadata
\author{FCM Student}
\title{Example Solution Report for Example Assignment}

\begin{document}

\maketitle

\section{Executive Summary}

In this report, we consider the big-$O$ order of accuracy for the forward, backward, and central finite difference approximations to the derivative of a function. Analytic derivations are confirmed empirically by a method of best-fit line under logarithmic transformation of the absolute error. Results are obtained and discussed for two functions, $f_1(x) = \sin(x)$ and $f_2(x) = \exp(-0.5x^2)$.

\section{Statement of the Problem}

The numerical approximation of derivatives is important in scientific- and mathematical-computing applications, e.g., in the numerical approximations to ODEs. Finite difference methods, which often depend on some parameter, $h$, can be used for these approximations, and the accuracy and convergence properties of the approximation often depends on the value of this parameter. We analyze these properties for the forward, backward, and central difference approximations, respectively:

\begin{enumerate}
    \item $\displaystyle F_h[f](x) = \frac{f(x+h) - f(x)}{h}$
    \item $\displaystyle B_h[f](x) = \frac{f(x) - f(x-h)}{h}$
    \item $\displaystyle C_h[f](x) = \frac{f(x+h) - f(x-h)}{2h}$
\end{enumerate}

\section{Description of the Mathematics}

\subsection{Determining the Order of Accuracy}

A finite difference approximation to the true derivative at some point, $x$, is often $O(h^p)$ for some positive integer, $p$, i.e., the approximation is within $Ch^p$ to the true derivative whenever $0 < h < \delta$ for some $C$ and appropriately chosen $\delta$. We derive the value, $p$, for forward, backward, and central difference methods in this section. Throughout, we assume that the function, $f(x)$, can be expanded as necessary using Taylor's theorem.

\subsubsection{Analytic Derivation of Accuracy Order}
\label{subsection:accuracy-order-derivation}
First,
$$
    f(t) = f(x) + f'(x)(t-x) + \frac{f''(\xi)}{2}(x-h)^2
$$
by Taylor expansion to second-order, so that
$$
    f(x+h) = f(x) + f'(x)h + \frac{f''(\xi)}{2}h^2
$$
and finally,
$$
    \frac{f(x+h)-f(x)}{h} = f'(x) + \frac{f''(\xi)}{2}h = f'(x) + O(h)
$$
Thus, $F_h[f](x)$ is $O(h)$. Similarly, evaluating the second-order expansion at $x-h$, we obtain,
$$
    \frac{f(x)-f(x-h)}{h} = f'(x) - \frac{f''(\xi)}{2}h = f'(x) + O(h)
$$
Thus, $B_h[f](x)$ is $O(h)$. Finally, by taking Taylor expansion to third-order,
$$
    f(t) = f(x) + f'(x)(t-x) + \frac{f''(x)}{2}(t-x)^2 + \frac{f^{(3)}(\mu)}{6}(t-x)^3
$$
we then obtain,
$$
    f(x+h)-f(x-h) = 2f'(x)h  + \frac{f^{(3)}(\mu)}{3}h^3
$$
Thus, $C_h[f](x)$ is $O(h^2)$.

\subsubsection{Method for Empirical Estimation of Accuracy Order\protect\footnote{The method described in this section is equivalent to the ``normal equation" approach. Ideally, a more numerically stable method should be used to solve this linear least-squares problem, e.g., the $QR$ decomposition.}}

\label{subsection:accuracy-estimate}

In this section, we describe a method for estimating the order of the accuracy of a numerical approximation by finding the slope of the best-fit line in log-space. Suppose we use some difference method, $D_h[f](x)$, for approximating the derivative, $f'(x)$.

We consider some fixed number of $h$ values, say $h_1, h_2, \ldots, h_m$, and for each, we measure the absolute error,
$$
    a_i := |D_{h_i}[f](x)-f'(x)|
$$
Assuming $a \approx Ch^p$ for some $C$ and $p$, we have that $\log(a) \approx \log(C) + p\log(h)$. We make the transformations, $u_i = \log(h_i)$, $v_i = \log(a_i)$, and denote $b=\log(C)$. The best parameters for the line, $a = b + pu$, are determined in the least-squares sense, i.e., those that minimize:
$$
    L(b,p) := \sum_{i=1}^m \left(b+pu_i - v_i\right)^2
$$
By taking partial derivatives, and setting to zero, we obtain a linear system:
$$
    \begin{bmatrix}
        m          & \sum_i u_i \\
        \sum_i u_i & \sum_i u_i^2
    \end{bmatrix}
    \begin{bmatrix} b \\ p \end{bmatrix}
    =
    \begin{bmatrix} \sum_i v_i \\ \sum_i u_iv_i \end{bmatrix}
$$
Solving the system for $b$ and $p$, yields the parameters of the best-fit line,
$$
    \hat{b} = \frac{\left(\sum_i u_i^2\right) \left(\sum_i v_i\right) - \left(\sum_i u_i\right)\left(\sum_i u_iv_i\right)}{m\sum_i u_i^2 - \left(\sum_i u_i \right)^2}
$$
and
$$
    \hat{p} = \frac{m\left(\sum_i u_iv_i\right) - \left(\sum_i u_i\right)\left(\sum_i v_i\right)}{m\sum_i u_i^2 - \left(\sum_i u_i \right)^2}
$$
Therefore, $\hat{p}$, yields an empirical estimate of the order of the accuracy of the finite difference approximation to the derivative.

\section{Description of the Algorithms and Implementation}

The finite difference methods each are translated directly into respective functions in \texttt{C++}, which accept three arguments: (1) a function, $f$, (2) a value, $x$, and (3) a value, $h$. For example, if a function, \texttt{func}, is previously defined, then \texttt{fdiff::forward(func, 1.2, 1e-3)} returns the forward difference approximation to the derivative of \texttt{func} at $x=1.2$ with $h=10^{-3}$.

\section{Description of the Experimental Design and Results}

For each finite difference method, function, and $x$-value, the order of the accuracy is first estimated by plotting, in logscale, the obtained absolute error against increasing values of $h$, and visually estimate the slope of the resulting line. We also quantify the slope, employing the method described in Section~\ref{subsection:accuracy-estimate}. Specifically, we choose $h_i = 10^{-1-6(i-1)/(m-1)}$, $i=1,\ldots, m$. This yields $m$ values of $h$ that are equally spaced after a $\log_{10}$ transformation, such that $h_1 = 10^{-1}$ and $h_m = 10^{-7}$. We choose $m=25$.

In Figures~\ref{fig:func1-results}~and~\ref{fig:func2-results}, the absolute error for both required functions, $f_1$ and $f_2$, and the required values of $x$, is plotted against increasing values of $h$. For both functions, we visually estimate in Figures~\ref{fig:func1-results}~and~\ref{fig:func2-results} that the forward and backward difference methods yield a slope of approximately 1. We observe, however, that for the central difference method, for both functions and for values of $h$ less than approximately $10^{-5}$ that the absolute error begins to increase and behave more erratically due to floating-point rounding error.

In Tables~\ref{tab:order-estimates-f1}~and~\ref{tab:order-estimates-f2}, the visually estimated slopes match the estimated values from the method of Section~\ref{subsection:accuracy-estimate}. We find values of approximately 1 (the expected analytic slope) for the forward and backward difference methods for both functions, $f_1$ and $f_2$, and each $x$-value tested, which agrees with the analytic order derived. We find that the estimated order of accuracy for the central difference method is skewed away from $2$ (the expected analytic slope for this method) when values of $h$ below $10^{-5}$. We observe (also in Tables~\ref{tab:order-estimates-f1}~and~\ref{tab:order-estimates-f2}) that by excluding these cases where numerical round-off becomes an issue, that we obtain order of accuracy estimates that agree with the analytically derived order of accuracy for the central difference method.

\section{Conclusions}

Our results illustrate the big-$O$ accuracy of the forward, backward, and central finite difference approximations to the derivative of a function. We found that numerical rounding-error affected the accuracy of the  central difference approximation for values of $h$ below $10^{-5}$. We suspect that similar findings would be obtained for the forward and backward difference methods as well, for smaller values of $h$ than those discussed in this report. This raises the question of how the value of $h$ should be chosen for a given function and finite difference method. Ideally, $h$ would be chosen in such manner so that the difference approximation maintains some acceptable level of error against the true derivative for all desired values of $x$. Another topic not considered in this report is how boundaries should be handled. In some circumstances, it may not be possible to sample a function beyond some point (or set of points), thus rendering some finite difference unsuitable for certain points. Is it possible, for example, to use the higher-order, central difference method in interior regions, and switch to some other second-order finite difference method not discussed here on the boundaries? Answers to such questions and related ones could be studied further in the future or found perhaps in the literature.

\section{Tables and Figures}

\begin{figure}[!ht]
    \centering
    \includegraphics[width=\textwidth]{../figures/f1.pdf}
    \caption{Error results for $f_1(x) = \sin(x)$ for the three difference methods, shown in log scale.}
    \label{fig:func1-results}
\end{figure}

\begin{figure}[!hb]
    \centering
    \includegraphics[width=\textwidth]{../figures/f2.pdf}
    \caption{Error results for $f_2(x) = \exp(-0.5x^2)$ for the three difference methods, shown in log scale.}
    \label{fig:func2-results}
\end{figure}

\begin{table}[!ht]
    \centering
    \begin{tabular}{|c|c|c|c|c|}
        \hline
                & Forward & Backward & Central & Central ($< 10^{-5}$ excluded) \\
        \hline
        $x=1.0$ & 1.0008  & 0.9995   & 1.3607  & 1.9944 \\
        \hline
        $x=2.0$ & 0.9997  & 1.0006   & 1.3454  & 2.0067 \\
        \hline
    \end{tabular}
    \caption{Order of accuracy estimates ($\hat{p}$) by method of Section~\ref{subsection:accuracy-estimate} for $f_1(x) = \sin(x)$.}
    \label{tab:order-estimates-f1}

    \vspace{0.5in}

    \begin{tabular}{|c|c|c|c|c|}
        \hline
                & Forward & Backward & Central & Central ($< 10^{-5}$ excluded) \\
        \hline
        $x=1.1$ & 1.0089 & 0.9853   & 1.3629  & 2.0021  \\
        \hline
        $x=2.1$ & 0.9984 & 1.0014   & 1.2977  & 1.9916  \\
        \hline
    \end{tabular}
    \caption{Order of accuracy estimates ($\hat{p}$) by method of Section~\ref{subsection:accuracy-estimate} for $f_2(x) = \exp(-0.5x^2)$.}
    \label{tab:order-estimates-f2}
\end{table}

\end{document}


\subsection{Marshallian demand}

We can write a demand function:

\(x_i=f(I, \mathbf p)\)

We can derive this from the first-order conditions of a specific utility function.


\section{Model and semantics}

\ralf{What also needs to be done here: Define uPred and its later function; define black later; define the resource CMRA}

The semantics closely follows the ideas laid out in~\cite{catlogic}.
We just repeat some of the most important definitions here.

An \emph{ordered family of equivalence relations} (o.f.e.\@) is a pair
$(X,(\nequiv{n})_{n\in\mathbb{N}})$, with $X$ a set, and each $\nequiv{n}$
an equivalence relation over $X$ satisfying
\begin{itemize}
	\item $\All x,x'. x \nequiv{0} x',$
	\item $\All x,x',n. x \nequiv{n+1} x' \implies x \nequiv{n} x',$
	\item $\All x,x'. (\All n. x\nequiv{n} x') \implies x = x'.$
\end{itemize}
\a
Let $(X,(\nequivset{n}{X})_{n\in\mathbb{N}})$ and
$(Y,(\nequivset{n}{Y})_{n\in\mathbb{N}})$ be o.f.e.'s. A function $f:
X\to Y$ is \emph{non-expansive} if,   for all $x$, $x'$ and $n$,
\[
x \nequivset{n}{X} x' \implies
fx \nequivset{n}{Y} f x'.
\]
Let $(X,(\nequiv{n})_{n\in\mathbb{N}})$ be an o.f.e.
A sequence $(x_i)_{i\in\mathbb{N}}$ of elements in $X$ is a
\emph{chain} (aka \emph{Cauchy sequence}) if
\[
\All k. \Exists n. \All i,j\geq n. x_i \nequiv{k} x_j.
\]
A \emph{limit} of a chain $(x_i)_{i\in\mathbb{N}}$ is an element
$x\in X$ such that
\[
\All n. \Exists k. \All i\geq k. x_i \nequiv{n} x.
\]
An o.f.e.\ $(X,(\nequiv{n})_{n\in\mathbb{N}})$ is \emph{complete}
if all chains have a limit.
A complete o.f.e.\ is called a c.o.f.e.\ (pronounced ``coffee'').
When the family of equivalence relations is clear from context we
simply
write $X$ for a c.o.f.e.\ $(X,(\nequiv{n})_{n\in\mathbb{N}})$.

Let $\cal U$ be the category of c.o.f.e.'s and nonexpansive maps.

Products and function spaces are defined as follows.
For c.o.f.e.'s $(X,(\nequivset{n}{X})_{n\in\mathbb{N}})$ and
$(Y,(\nequivset{n}{Y})_{n\in\mathbb{N}})$, their product
is
$(X\times Y, (\nequiv{n})_{n\in\mathbb{N}}),$
where
\[
(x,y) \nequiv{n} (x',y') \iff
x \nequiv{n} x' \land
y \nequiv{n} y'.
\]
The function space is
\[
(\{\, f : X\to Y \mid f \text{ is non-expansive}\,\}, (\nequiv{n})_{n\in\mathbb{N}}),
\]
where
\[
f \nequiv{n} g \iff
\All x. f(x)  \nequiv{n}  g(x).
\]

For a c.o.f.e.\ $(X,(\nequiv{n}_{n\in\mathbb{N}}))$,
$\latert (X,(\nequiv{n}_{n\in\mathbb{N}}))$ is the c.o.f.e.\@
$(X,(\nequivB{n}_{n\in\mathbb{N}}))$,  where
\[
x \nequivB{n} x' \iff \begin{cases}
\top	&\IF n=0 \\
x \nequiv{n-1} x' &\IF n>0
\end{cases}
\]

(Sidenote: $\latert$ extends to a functor on $\cal U$ by the identity
action on morphisms).

\subsection{Semantic structures: propositions}
\ralf{This needs to be synced with the Coq development again.}

\[
\begin{array}[t]{rcl}
%  \protStatus &::=& \enabled \ALT \disabled \\[0.4em]
\textdom{Res} &\eqdef&
\{\, \rs = (\pres, \ghostRes) \mid
\pres \in \textdom{State} \uplus \{\munit\} \land \ghostRes \in \mcarp{\monoid} \,\} \\[0.5em]
(\pres, \ghostRes) \rtimes
(\pres', \ghostRes') &\eqdef&
\begin{cases}
(\pres, \ghostRes \mtimes \ghostRes')  & \mbox{if $\pres' = \munit$ and $\ghostRes \mtimes \ghostRes' \neq \mzero$} \\
(\pres', \ghostRes \mtimes \ghostRes') & \mbox{if $\pres = \munit$ and $\ghostRes \mtimes \ghostRes' \neq \mzero$}
\end{cases}
\\[0.5em]
%
\rs \leq \rs' & \eqdef &
\Exists \rs''. \rs' = \rs \rtimes \rs''\\[1em]
%
\UPred(\textdom{Res}) &\eqdef&
\{\, p \subseteq \mathbb{N} \times \textdom{Res} \mid
\All (k,\rs) \in p.
\All j\leq k.
\All \rs' \geq \rs.
(j,\rs')\in p \,\}\\[0.5em]
\restr{p}{k} &\eqdef&
\{\, (j, \rs) \in p \mid j < k \,\}\\[0.5em]
p \nequiv{n} q & \eqdef & \restr{p}{n} = \restr{q}{n}\\[1em]
%
\textdom{PreProp} & \cong  &
\latert\big( \textdom{World} \monra \UPred(\textdom{Res})
\big)\\[0.5em]
%
\textdom{World} & \eqdef &
\mathbb{N} \fpfn \textdom{PreProp}\\[0.5em]
%
w \nequiv{n} w' & \eqdef &
n = 0 \lor
\bigl(\dom(w) = \dom(w') \land \All i\in\dom(w). w(i) \nequiv{n} w'(i)\bigr)
\\[0.5em]
%
w \leq w' & \eqdef &
\dom(w) \subseteq \dom(w') \land \All i \in \dom(w). w(i) = w'(i)
\\[0.5em]
%
\textdom{Prop} & \eqdef & \textdom{World} \monra \UPred(\textdom{Res})
\end{array}
\]

For $p,q\in\UPred(\textdom{Res})$ with $p \nequiv{n} q$ defined
as above, $\UPred(\textdom{Res})$ is a
c.o.f.e.

$\textdom{Prop}$ is a c.o.f.e., which exists by America and Rutten's theorem~\cite{America-Rutten:JCSS89}.
We do not need to consider how the object is constructed.
We only need the isomorphism, given by maps
\begin{align*}
	\wIso &: \latert \bigl(World \monra \UPred(\textdom{Res})\bigr) \to \textdom{PreProp} \\
	\wIso^{-1} &: \textdom{PreProp} \to \latert \bigl(World \monra \UPred(\textdom{Res})\bigr)
\end{align*}
which are inverses to each other.
Note: this is an isomorphism in $\cal U$, i.e., $\wIso$ and
$\wIso^{-1}$ are both non-expansive.

$\textdom{World}$ is a c.o.f.e.\ with the family of equivalence
relations defined as shown above.

\subsection{Semantic structures: types and environments}

For a set $X$, write $\Delta X$ for the discrete c.o.f.e.\ with $x \nequiv{n}
x'$ iff $n = 0$ or $x = x'$
\[
\begin{array}[t]{@{}l@{\ }c@{\ }l@{}}
\Sem{\textsort{Unit}} &\eqdef& \Delta \{ \star \} \\
\Sem{\textsort{InvName}} &\eqdef& \Delta \mathbb{N}  \\
\Sem{\textsort{InvMask}} &\eqdef& \Delta \pset{\mathbb{N}} \\
\Sem{\textsort{Monoid}} &\eqdef& \Delta |\monoid|
\end{array}
\qquad\qquad
\begin{array}[t]{@{}l@{\ }c@{\ }l@{}}
\Sem{\textsort{Val}} &\eqdef& \Delta \textdom{Val} \\
\Sem{\textsort{Exp}} &\eqdef& \Delta \textdom{Exp} \\
\Sem{\textsort{Ectx}} &\eqdef& \Delta \textdom{Ectx} \\
\Sem{\textsort{State}} &\eqdef& \Delta \textdom{State} \\
\end{array}
\qquad\qquad
\begin{array}[t]{@{}l@{\ }c@{\ }l@{}}
\Sem{\sort \times \sort'} &\eqdef& \Sem{\sort} \times \Sem{\sort} \\
\Sem{\sort \to \sort'} &\eqdef& \Sem{\sort} \to \Sem{\sort} \\
\Sem{\Prop} &\eqdef& \textdom{Prop} \\
\end{array}
\]

The balance of our signature $\Sig$ is interpreted as follows.
For each base type $\type$ not covered by the preceding table, we pick an object $X_\type$ in $\cal U$ and define
\[
\Sem{\type} \eqdef X_\type
\]
For each function symbol $\sigfn : \type_1, \dots, \type_n \to \type_{n+1} \in \SigFn$, we pick an arrow $\Sem{\sigfn} : \Sem{\type_1} \times \dots \times \Sem{\type_n} \to \Sem{\type_{n+1}}$ in $\cal U$.

An environment $\vctx$ is interpreted as the set of
maps $\rho$, with $\dom(\rho) = \dom(\vctx)$ and
$\rho(x)\in\Sem{\vctx(x)}$,
and
$\rho\nequiv{n} \rho' \iff n=0 \lor \bigl(\dom(\rho)=\dom(\rho') \land
\All x\in\dom(\rho). \rho(x) \nequiv{n} \rho'(x)\bigr)$.

\ralf{Re-check all the following definitions with the Coq development.}
%\typedsection{Validity}{valid : \pset{\textdom{Prop}} \in Sets}
%
%\begin{align*}
%valid(p) &\iff \All n \in \mathbb{N}. \All \rs \in \textdom{Res}. \All W \in \textdom{World}. (n, \rs) \in p(W)
%\end{align*}

\typedsection{Later modality}{\later : \textdom{Prop} \to \textdom{Prop} \in {\cal U}}

\begin{align*}
	\later p &\eqdef \Lam W. \{\, (n + 1, r) \mid (n, r) \in p(W) \,\} \cup \{\, (0, r) \mid r \in \textdom{Res} \,\}
\end{align*}
\begin{lem}
	$\later{}$ is well-defined: $\later {p}$ is a valid proposition (this amounts to showing non-expansiveness), and $\later{}$ itself is a \emph{contractive} map.
\end{lem}

\typedsection{Always modality}{\always{} : \textdom{Prop} \to \textdom{Prop} \in {\cal U}}

\begin{align*}
	\always{p} \eqdef \Lam W. \{\, (n, r) \mid (n, \munit) \in p(W) \,\}
\end{align*}
\begin{lem}
	$\always{}$ is well-defined: $\always{p}$ is a valid proposition (this amounts to showing non-expansiveness), and $\always{}$ itself is a non-expansive map.
\end{lem}

% PDS: p \Rightarrow q not defined.
%\begin{lem}\label{lem:always-impl-valid}
%\begin{align*}
%&\forall p, q \in \textdom{Prop}.~\\
%&\qquad
%  (\forall n \in \mathbb{N}.~\forall \rs \in \textdom{Res}.~\forall W \in \textdom{World}.~(n, \rs) \in p(W) \Rightarrow (n, \rs) \in q(W)) \Leftrightarrow~valid(\always{(p \Rightarrow q)})
%\end{align*}
%\end{lem}

\typedsection{Invariant definition}{inv : \Delta(\mathbb{N}) \times \textdom{Prop} \to \textdom{Prop} \in {\cal U}}
\begin{align*}
	\mathit{inv}(\iota, p) &\eqdef \Lam W. \{\, (n, r) \mid \iota\in\dom(W) \land W(\iota) \nequiv{n+1}_{\textdom{PreProp}} \wIso(p) \,\}
\end{align*}
\begin{lem}
	$\mathit{inv}$ is well-defined: $\mathit{inv}(\iota, p)$ is a valid proposition (this amounts to showing non-expansiveness), and $\mathit{inv}$ itself is a non-expansive map.
\end{lem}

\typedsection{World satisfaction}{\wsat{-}{-}{-}{-} :
	\textdom{State} \times
	\pset{\mathbb{N}} \times
	\textdom{Res} \times
	\textdom{World} \to \psetdown{\mathbb{N}} \in {\cal U}}
\ralf{Make this Dave-compatible: Explicitly compose all the things in $s$}
\begin{align*}
	\wsat{\state}{\mask}{\rs}{W} &=
	\begin{aligned}[t]
		\{\, n + 1 \in \mathbb{N} \mid &\Exists  \rsB:\mathbb{N} \fpfn \textdom{Res}. (\rs \rtimes \rsB).\pres = \state \land{}\\
		&\quad \All \iota \in \dom(W). \iota \in \dom(W) \leftrightarrow \iota \in \dom(\rsB) \land {}\\
		&\quad\quad \iota \in \mask \ra (n, \rsB(\iota)) \in \wIso^{-1}(W(\iota))(W) \,\} \cup \{ 0 \}
	\end{aligned}
\end{align*}
\begin{lem}\label{lem:fullsat-nonexpansive}
	$\wsat{-}{-}{-}{-}$ is well-defined: It maps into $\psetdown{\mathbb{N}}$. (There is no need for it to be a non-expansive map, it doesn't itself live in $\cal U$.)
\end{lem}

\begin{lem}\label{lem:fullsat-weaken-mask}
	\begin{align*}
		\MoveEqLeft
		\All \state \in \Delta(\textdom{State}).
		\All \mask_1, \mask_2 \in \Delta(\pset{\mathbb{N}}).
		\All \rs, \rsB \in \Delta(\textdom{Res}).
		\All W \in \textdom{World}. \\&
		\mask_1 \subseteq \mask_2 \implies (\wsat{\state}{\mask_2}{\rs}{W}) \subseteq (\wsat{\state}{\mask_1}{\rs}{W})
	\end{align*}
\end{lem}

\begin{lem}\label{lem:nequal_ext_world}
	\begin{align*}
		&
		\All n \in \mathbb{N}.
		\All W_1, W_1', W_2 \in \textdom{World}.
		W_1 \nequiv{n} W_2 \land W_1 \leq W_1' \implies \Exists W_2' \in \textdom{World}. W_1' \nequiv{n} W_2' \land W_2 \leq W_2'
	\end{align*}
\end{lem}

\typedsection{Timeless}{\textit{timeless} : \textdom{Prop} \to \textdom{Prop}}

\begin{align*}
	\textit{timeless}(p) \eqdef
	\begin{aligned}[t]
		\Lam W.
		\{\, (n, r) &\mid \All W' \geq W. \All k \leq n. \All r' \in \textdom{Res}. \\
		&\qquad
		k > 0 \land (k - 1, r') \in p(W') \implies (k, r') \in p(W') \,\}
	\end{aligned}
\end{align*}

\begin{lem}
	\textit{timeless} is well-defined: \textit{timeless}(p) is a valid proposition, and \textit{timeless} itself is a non-expansive map.
\end{lem}

% PDS: \Ra undefined.
%\begin{lem}
%\begin{align*}
%&
%  \All p \in \textdom{Prop}.
%  \All \mask \in \pset{\mathbb{N}}.
%valid(\textit{timeless}(p) \Ra (\later p \vs[\mask][\mask] p))
%\end{align*}
%\end{lem}

\typedsection{View-shift}{\mathit{vs} : \Delta(\pset{\mathbb{N}}) \times \Delta(\pset{\mathbb{N}}) \times \textdom{Prop} \to \textdom{Prop} \in {\cal U}}
\begin{align*}
	\mathit{vs}_{\mask_1}^{\mask_2}(q) &= \Lam W.
	\begin{aligned}[t]
		\{\, (n, \rs) &\mid \All W_F \geq W. \All \rs_F, \mask_F, \state. \All k \leq n.\\
		&\qquad
		k \in (\wsat{\state}{\mask_1 \cup \mask_F}{\rs \rtimes \rs_F}{W_F}) \land k > 0 \land \mask_F \sep (\mask_1 \cup \mask_2) \implies{} \\
		&\qquad
		\Exists W' \geq W_F. \Exists \rs'. k \in (\wsat{\state}{\mask_2 \cup \mask_F}{\rs' \rtimes \rs_F}{W'}) \land (k, \rs') \in q(W')
		\,\}
	\end{aligned}
\end{align*}
\begin{lem}
	$\mathit{vs}$ is well-defined: $\mathit{vs}_{\mask_1}^{\mask_2}(q)$ is a valid proposition, and $\mathit{vs}$ is a non-expansive map.
\end{lem}

%\begin{lem}\label{lem:prim_view_shift_trans}
%\begin{align*}
%\MoveEqLeft
%  \All \mask_1, \mask_2, \mask_3 \in \Delta(\pset{\mathbb{N}}).
%  \All p, q \in \textdom{Prop}. \All W \in \textdom{World}.
%  \All n \in \mathbb{N}.\\
%&
%  \mask_2 \subseteq \mask_1 \cup \mask_3 \land
%  \bigl(\All W' \geq W. \All r \in \textdom{Res}. \All k \leq n. (k, r) \in p(W') \implies (k, r) \in vs_{\mask_2}^{\mask_3}(q)(W')\bigr) \\
%&\qquad
%  {}\implies \All r \in \textdom{Res}. (n, r) \in vs_{\mask_1}^{\mask_2}(p)(W) \implies (n, r) \in vs_{\mask_1}^{\mask_3}(q)(W)
%\end{align*}
%\end{lem}

% PDS: E_1 ==>> E_2 undefined.
%\begin{lem}
%\begin{align*}
%&
%  \forall \mask_1, \mask_2, \mask_3 \in \Delta(\pset{\mathbb{N}}).~
%  \forall p_1, p_2, p_3 \in \textdom{Prop}.~\\
%&\qquad
%  \mask_2 \subseteq \mask_1 \cup \mask_3 \Rightarrow
%  valid(((p_1 \vs[\mask_1][\mask_2] p_2) \land (p_2 \vs[\mask_2][\mask_3] p_3)) \Rightarrow (p_1 \vs[\mask_1][\mask_3] p_3))
%\end{align*}
%\end{lem}

%\begin{lem}
%\begin{align*}
%\MoveEqLeft
%  \All \iota \in \mathbb{N}.
%  \All p \in \textdom{Prop}.
%  \All W \in \textdom{World}.
%  \All \rs \in \textdom{Res}.
%  \All n \in \mathbb{N}. \\
%&
%  (n, \rs) \in inv(\iota, p)(W) \implies (n, \rs) \in vs_{\{ \iota \}}^{\emptyset}(\later p)(W)
%\end{align*}
%\end{lem}

% PDS: * undefined.
%\begin{lem}
%\begin{align*}
%&
%  \forall \iota \in \mathbb{N}.~
%  \forall p \in \textdom{Prop}.~
%  \forall W \in \textdom{World}.~
%  \forall \rs \in \textdom{Res}.~
%  \forall n \in \mathbb{N}.~\\
%&\qquad
%  (n, \rs) \in (inv(\iota, p) * \later p)(W) \Rightarrow (n, \rs) \in vs^{\{ \iota \}}_{\emptyset}(\top)(W)
%\end{align*}
%\end{lem}

% \begin{lem}
% \begin{align*}
% &
%   \forall \mask_1, \mask_2 \in \Delta(\pset{\mathbb{N}}).~
%   valid(\bot \vs[\mask_1][\mask_2] \bot)
% \end{align*}
% \end{lem}

% PDS: E_1 ==>> E_2 undefined.
%\begin{lem}
%\begin{align*}
%&
%  \forall p, q \in \textdom{Prop}.~
%  \forall \mask \in \pset{\mathbb{N}}.~
%valid(\always{(p \Rightarrow q)} \Rightarrow (p \vs[\mask][\mask] q))
%\end{align*}
%\end{lem}

% PDS: E # E' and E_1 ==>> E_2 undefined.
%\begin{lem}
%\begin{align*}
%&
%  \forall p_1, p_2, p_3 \in \textdom{Prop}.~
%  \forall \mask_1, \mask_2, \mask \in \pset{\mathbb{N}}.~
%valid(\mask \sep \mask_1 \Ra \mask \sep \mask_2 \Ra (p_1 \vs[\mask_1][\mask_2] p_2) \Rightarrow (p_1 * p_3 \vs[\mask_1 \cup \mask][\mask_2 \cup \mask] p_2 * p_3))
%\end{align*}
%\end{lem}

\typedsection{Weakest precondition}{\mathit{wp} : \Delta(\pset{\mathbb{N}}) \times \Delta(\textdom{Exp}) \times (\Delta(\textdom{Val}) \to \textdom{Prop}) \to \textdom{Prop} \in {\cal U}}

% \begin{align*}
% 	\mathit{wp}_\mask(\expr, q) &\eqdef \Lam W.
% 	\begin{aligned}[t]
% 		\{\, (n, \rs) &\mid \All W_F \geq W; k \leq n; \rs_F; \state; \mask_F \sep \mask. k > 0 \land k \in (\wsat{\state}{\mask \cup \mask_F}{\rs \rtimes \rs_F}{W_F}) \implies{}\\
% 		&\qquad
% 		(\expr \in \textdom{Val} \implies \Exists W' \geq W_F. \Exists \rs'. \\
% 		&\qquad\qquad
% 		k \in (\wsat{\state}{\mask \cup \mask_F}{\rs' \rtimes \rs_F}{W'}) \land (k, \rs') \in q(\expr)(W'))~\land \\
% 		&\qquad
% 		(\All\ectx,\expr_0,\expr'_0,\state'. \expr = \ectx[\expr_0] \land \cfg{\state}{\expr_0} \step \cfg{\state'}{\expr'_0} \implies \Exists W' \geq W_F. \Exists \rs'. \\
% 		&\qquad\qquad
% 		k - 1 \in (\wsat{\state'}{\mask \cup \mask_F}{\rs' \rtimes \rs_F}{W'}) \land (k-1, \rs') \in wp_\mask(\ectx[\expr_0'], q)(W'))~\land \\
% 		&\qquad
% 		(\All\ectx,\expr'. \expr = \ectx[\fork{\expr'}] \implies \Exists W' \geq W_F. \Exists \rs', \rs_1', \rs_2'. \\
% 		&\qquad\qquad
% 		k - 1 \in (\wsat{\state}{\mask \cup \mask_F}{\rs' \rtimes \rs_F}{W'}) \land \rs' = \rs_1' \rtimes \rs_2'~\land \\
% 		&\qquad\qquad
% 		(k-1, \rs_1') \in \mathit{wp}_\mask(\ectx[\textsf{fRet}], q)(W') \land
% 		(k-1, \rs_2') \in \mathit{wp}_\top(\expr', \Lam\any. \top)(W'))
% 		\,\}
% 	\end{aligned}
% \end{align*}
\begin{lem}
	$\mathit{wp}$ is well-defined: $\mathit{wp}_{\mask}(\expr, q)$ is a valid proposition, and $\mathit{wp}$ is a non-expansive map. Besides, the dependency on the recursive occurrence is contractive, so $\mathit{wp}$ has a fixed-point.
\end{lem}

\begin{lem}
	$\mathit{wp}$ on values and non-mask-changing $\mathit{vs}$ agree:
	\[ \mathit{wp}_\mask(\val, q) = \mathit{vs}_{\mask}^{\mask}(q \: \val)  \]
\end{lem}

\typedsection{Interpretation of terms}{\Sem{\vctx \proves \term : \sort} : \Sem{\vctx} \to \Sem{\sort} \in {\cal U}}

%A term $\vctx \proves \term : \sort$ is interpreted as a non-expansive map from $\Sem{\vctx}$ to $\Sem{\sort}$.

\begin{align*}
	\Sem{\vctx \proves x : \sort}_\gamma &= \gamma(x) \\
	\Sem{\vctx \proves \sigfn(\term_1, \dots, \term_n) : \type_{n+1}}_\gamma &= \Sem{\sigfn}(\Sem{\vctx \proves \term_1 : \type_1}_\gamma, \dots, \Sem{\vctx \proves \term_n : \type_n}_\gamma) \ \WHEN \sigfn : \type_1, \dots, \type_n \to \type_{n+1} \in \SigFn \\
	\Sem{\vctx \proves \Lam x. \term : \sort \to \sort'}_\gamma &=
	\Lam v : \Sem{\sort}. \Sem{\vctx, x : \sort \proves \term : \sort'}_{\gamma[x \mapsto v]} \\
	\Sem{\vctx \proves \term~\termB : \sort'}_\gamma &=
	\Sem{\vctx \proves \term : \sort \to \sort'}_\gamma(\Sem{\vctx \proves \termB : \sort}_\gamma) \\
	\Sem{\vctx \proves \unitval : \unitsort}_\gamma &= \star \\
	\Sem{\vctx \proves (\term_1, \term_2) : \sort_1 \times \sort_2}_\gamma &= (\Sem{\vctx \proves \term_1 : \sort_1}_\gamma, \Sem{\vctx \proves \term_2 : \sort_2}_\gamma) \\
	\Sem{\vctx \proves \pi_i~\term : \sort_1}_\gamma &= \pi_i(\Sem{\vctx \proves \term : \sort_1 \times \sort_2}_\gamma)
\end{align*}
%
\begin{align*}
	\Sem{\vctx \proves \mzero : \textsort{Monoid}}_\gamma &= \mzero \\
	\Sem{\vctx \proves \munit : \textsort{Monoid}}_\gamma &= \munit \\
	\Sem{\vctx \proves \melt \mtimes \meltB : \textsort{Monoid}}_\gamma &=
	\Sem{\vctx \proves \melt : \textsort{Monoid}}_\gamma \mtimes \Sem{\vctx \proves \meltB : \textsort{Monoid}}_\gamma
\end{align*}
%
\begin{align*}
	\Sem{\vctx \proves t =_\sort u : \Prop}_\gamma &=
	\Lam W. \{\, (n, r) \mid \Sem{\vctx \proves t : \sort}_\gamma \nequiv{n+1} \Sem{\vctx \proves u : \sort}_\gamma \,\} \\
	\Sem{\vctx \proves \FALSE : \Prop}_\gamma &= \Lam W. \emptyset \\
	\Sem{\vctx \proves \TRUE : \Prop}_\gamma &= \Lam W. \mathbb{N} \times \textdom{Res} \\
	\Sem{\vctx \proves P \land Q : \Prop}_\gamma &=
	\Lam W. \Sem{\vctx \proves P : \Prop}_\gamma(W) \cap \Sem{\vctx \proves Q : \Prop}_\gamma(W) \\
	\Sem{\vctx \proves P \lor Q : \Prop}_\gamma &=
	\Lam W. \Sem{\vctx \proves P : \Prop}_\gamma(W) \cup \Sem{\vctx \proves Q : \Prop}_\gamma(W) \\
	\Sem{\vctx \proves P \Ra Q : \Prop}_\gamma &=
	\Lam W. \begin{aligned}[t]
		\{\, (n, r) &\mid \All n' \leq n. \All W' \geq W. \All r' \geq r. \\
		&\qquad
		(n', r') \in \Sem{\vctx \proves P : \Prop}_\gamma(W')~ \\
		&\qquad
		\implies (n', r') \in \Sem{\vctx \proves Q : \Prop}_\gamma(W') \,\}
	\end{aligned} \\
	\Sem{\vctx \proves \All x : \sort. P : \Prop}_\gamma &=
	\Lam W. \{\, (n, r) \mid \All v \in \Sem{\sort}. (n, r) \in \Sem{\vctx, x : \sort \proves P : \Prop}_{\gamma[x \mapsto v]}(W) \,\} \\
	\Sem{\vctx \proves \Exists x : \sort. P : \Prop}_\gamma &=
	\Lam W. \{\, (n, r) \mid \Exists v \in \Sem{\sort}. (n, r) \in \Sem{\vctx, x : \sort \proves P : \Prop}_{\gamma[x \mapsto v]}(W) \,\}
\end{align*}
%
\begin{align*}
	\Sem{\vctx \proves \always{\prop} : \Prop}_\gamma &= \always{\Sem{\vctx \proves \prop : \Prop}_\gamma} \\
	\Sem{\vctx \proves \later{\prop} : \Prop}_\gamma &= \later \Sem{\vctx \proves \prop : \Prop}_\gamma\\
	\Sem{\vctx \proves \MU x. \pred : \sort \to \Prop}_\gamma &=
	\mathit{fix}(\Lam v : \Sem{\sort \to \Prop}. \Sem{\vctx, x : \sort \to \Prop \proves \pred : \sort \to \Prop}_{\gamma[x \mapsto v]}) \\
	\Sem{\vctx \proves \prop * \propB : \Prop}_\gamma &=
	\begin{aligned}[t]
		\Lam W. \{\, (n, r) &\mid \Exists r_1, r_2. r = r_1 \bullet r_2 \land{} \\
		&\qquad
		(n, r_1) \in \Sem{\vctx \proves \prop : \Prop}_\gamma \land{} \\
		&\qquad
		(n, r_2) \in \Sem{\vctx \proves \propB : \Prop}_\gamma \,\}
	\end{aligned} \\
	\Sem{\vctx \proves \prop \wand \propB : \Prop}_\gamma &=
	\begin{aligned}[t]
		\Lam W. \{\, (n, r) &\mid \All n' \leq n. \All W' \geq W. \All r'. \\
		&\qquad
		(n', r') \in \Sem{\vctx \proves \prop : \Prop}_\gamma(W') \land r \sep r' \\
		&\qquad
		\implies (n', r \bullet r') \in \Sem{\vctx \proves \propB : \Prop}_\gamma(W')
		\}
	\end{aligned} \\
	\Sem{\vctx \proves \knowInv{\iname}{\prop} : \Prop}_\gamma &=
	inv(\Sem{\vctx \proves \iname : \textsort{InvName}}_\gamma, \Sem{\vctx \proves \prop : \Prop}_\gamma) \\
	\Sem{\vctx \proves \ownGGhost{\melt} : \Prop}_\gamma &=
	\Lam W. \{\, (n, \rs) \mid \rs.\ghostRes \geq \Sem{\vctx \proves \melt : \textsort{Monoid}}_\gamma \,\} \\
	\Sem{\vctx \proves \ownPhys{\state} : \Prop}_\gamma &=
	\Lam W. \{\, (n, \rs) \mid \rs.\pres = \Sem{\vctx \proves \state : \textsort{State}}_\gamma \,\}
\end{align*}
%
\begin{align*}
	\Sem{\vctx \proves \pvsA{\prop}{\mask_1}{\mask_2} : \Prop}_\gamma &=
	\textdom{vs}^{\Sem{\vctx \proves \mask_2 : \textsort{InvMask}}_\gamma}_{\Sem{\vctx \proves \mask_1 : \textsort{InvMask}}_\gamma}(\Sem{\vctx \proves \prop : \Prop}_\gamma) \\
	\Sem{\vctx \proves \dynA{\expr}{\pred}{\mask} : \Prop}_\gamma &=
	\textdom{wp}_{\Sem{\vctx \proves \mask : \textsort{InvMask}}_\gamma}(\Sem{\vctx \proves \expr : \textsort{Exp}}_\gamma, \Sem{\vctx \proves \pred : \textsort{Val} \to \Prop}_\gamma) \\
	\Sem{\vctx \proves \wtt{\timeless{\prop}}{\Prop}}_\gamma &=
	\textdom{timeless}(\Sem{\vctx \proves \prop : \Prop}_\gamma)
\end{align*}

\typedsection{Interpretation of entailment}{\Sem{\vctx \mid \pfctx \proves \prop} : 2 \in \mathit{Sets}}

\[
\Sem{\vctx \mid \pfctx \proves \propB} \eqdef
\begin{aligned}[t]
\MoveEqLeft
\forall n \in \mathbb{N}.\;
\forall W \in \textdom{World}.\;
\forall \rs \in \textdom{Res}.\;
\forall \gamma \in \Sem{\vctx},\;
\\&
\bigl(\All \propB \in \pfctx. (n, \rs) \in \Sem{\vctx \proves \propB : \Prop}_\gamma(W)\bigr)
\implies (n, \rs) \in \Sem{\vctx \proves \prop : \Prop}_\gamma(W)
\end{aligned}
\]

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "iris"
%%% End:


\section{Alpha-beta search}

\begin{enumerate}
    \item Perform the MiniMax algorithm on the tree in Figure 1, i.e. put a value to each node. Circle the move the root player should do.
      \begin{figure}[!ht]
      \begin{framed}
	  \centering
	  \includegraphics[width=\linewidth]{tree.png}
	  \caption{MiniMax}
      \end{framed}
      \end{figure}
      \FloatBarrier
    \item Perform the Alpha-Beta algorithm on the tree in Figure 2. At each non terminal node, put the successive values of $\alpha$ and $\beta$. Cross out the arcs reaching non visited nodes. Assume a left-to-right node expansion.
      \begin{figure}[!ht]
      \begin{framed}
	  \centering
	  \includegraphics[width=\linewidth]{tree.png}
	  \caption{Alpha-Beta, left-to-right expansion}
      \end{framed}
      \end{figure}
      \FloatBarrier
    \item Do the same, assuming a right-to-left node expansion instead (Figure 3).
      \begin{figure}[!ht]
      \begin{framed}
	  \centering
	  \includegraphics[width=\linewidth]{tree.png}
	  \caption{Alpha-Beta, right-to-left expansion}
      \end{framed}
      \end{figure}
      \FloatBarrier
    \item Can the nodes be ordered in such a way that Alpha-Beta pruning can cut off more branches (in a left-to-right node expansion)? If no, explain why; if yes, give the new ordering and the resulting new pruning.
      \begin{figure}[!ht]
      \begin{framed}
        The nodes can be ordered in such a way that Alpha-Beta pruning can
        cut off more branches, as shown in the following tree:

        \bigskip
        \bigskip
	  \centering
	  \includegraphics[width=\linewidth]{my_tree.png}
      \bigskip
      \bigskip
	  \caption{Alpha-Beta, left-to-right expansion on ordered tree}
      \end{framed}
      \end{figure}
      \FloatBarrier
\end{enumerate}

\section{Avalam}
    \subsection{A basic alpha beta agent}
    \subsection{Comparison of two evaluation functions}
    \begin{enumerate}
    \item[6.] Launch a game where one of the agents is the basic agent described before against another agent where the basic evaluate method has been replaced such that it returns directly the result of Board.get\_score instead of -1, 0 or 1. Watch the replay of the match. What do you observe? Does one of the agent clearly overcomes the other one? Explain why there is such a difference.
      \begin{framed}
          When the new version of basic agent is Player 1, it clearly
          overcomes the old version because even with a depth of 2, the
          differences between the values evaluated are  much more relevant.
          \newline

          When the new version of basic agent is Player 2, the old basic
          agent wins because the improvement does not overcome the advantage
          from being Player 1. The first moves aren't very good but it gets
          better and better as the game enfolds catching back nearly all
          its delay at the end, losing by only 1 point. \newline
      \end{framed}
\end{enumerate}

\subsection{Evaluation function}

    \begin{enumerate}
        \item[7.] Describe precisely your evaluation function.
        \begin{framed}
            In our evaluation fonction, we first start by separating the board positions in three types. If the position is empty we ignore it. If the position is filled a tower of height five, we add (or substract depending on the color of the tower) a fixed score X to the total score of the board. Finally for every positions that doesn't fit the two previous criteria we call the towerScore method. \newline

            In this towerScore fonction, we calculate the score that should be allowed to the tower depending to various factor. First, if the tower is isolated, we return a fixed score Y such as X > Y, allowing our algorithm to prefer state with more tower of height five than others. The difference is of course not too large so that the algorithm doesn't sacrifice two isolated tower for a tower of height five. \newline

            If the tower isn't isolated, the algorithm will then determine if the tower is in a simple snapback state wich we define as follow : If a tower of height one or two doesn't have an ally of the same color on which it could jump on and if for every ennemy it could jump on the ennemy can either form a tower of height five or an isolated tower with his next move, then the tower is in a simple snapback state. \newline

            Of course some pure snapback states are not contained in this description but it was necessary for the evaluation method to run as fast as possible so we kept only the most common and obvious definition of a snapback to speed it up. The other snapack states will still usually be found as the depth increases, but this gives us a safety margin for lower depth computation. \newline

            Finally if the tower is neither isolated nor in a snapback state, we return a given score depending on the height of the tower at hand. This score is such that a tower of height 1 is worth more than a tower of height 2 which is worth more than a tower of height three or four. Doing so allows our algorithm to reduce the opponent's possibilites if an occasion to do so arises. \newline

            When our algorithm has computed the score for each position of the board, adding or substracting it depending on the color, we simply return this score as the evaluation of the board. \newline
        \end{framed}
    \end{enumerate}

\subsection{Successors function}

\begin{enumerate}
    \item[8.] Give an upper and a lower bound on the branching factor for a search tree on the Avalam game. Justify your answer.
    \begin{framed}
        For an algorithm that doens't drop branches, we can easily differ that the branching factor will be lower as the game enfolds because every step consume a tower thus the number of action possible keeps decreasing. We can thus easily affirm that the branching factor will be comprised between 292 which is the branching factor of a brand new game and 2 which is the lowest branching factor that could happen for the last move when only two movable towers are left next to each other. The branching factor is thus comprised in the domain $[2, 292]$
    \end{framed}
    \item[9.] How does the branching factor evolve after a move has been performed? Explain.
    \begin{framed}
        As explained in the previous answer, it will decrease at each step but the quantity by which it decreases is variable as it depends on the neighbours of the moved tower.
    \end{framed}
    \item[10.] Can you think of states that might be ignored? What do you loose if you ignore successors? Explain.
    \begin{framed}
        There is quite a number of states that could potentially be ignored but doing so might not always be safe as it could drop with it potential for better states. For example, a move that can be safely dropped is when two tower which fill each other are next to each other, we can safely ignore states where the current player give a five tower to the ennemy by making an ennemy tower jump on his tower. \newline

        Similarly we can drop moves where the current player isolate a tower for the ennemy but it might not be safe as giving that tower to the ennemy may allows us to make more points later on. Dropping tower is thus a dangerous but worthy gamble.
    \end{framed}
    \item[11.] Describe your successors function.
    \begin{framed}
        Our successor function is quite simple, we simply call the get\_sorted\_action() method which does the same as the original get\_actions() except that it sorts the actions it returns so that the most interesting ones tend to be yield towards the beginning. It will also drops extremely useless possibilities like a one surrounded by seven or more ones other ones.

        It then plays those action on a clone of the old board and yields the resulting new boards in succession
    \end{framed}
\end{enumerate}

\subsection{Cut-off function}

\begin{enumerate}
    \item[12.] The \verb#cutoff# method receives an argument called depth . Explain precisely what is called the \textit{depth} in the \verb#minimax.py# implementation.
    \begin{framed}
        The depth argument is the current depth at which the minmax function is in the search tree. Thus the root will be at depth 0, its branches will be at the two and so forth and so on.
    \end{framed}
    \item[13.] Explain why it might be useful (for the Avalam contest) to cut off the search for another reason than the depth. Would it be interesting to consider a changing cutoff function (i.e. it changes according to the advancement of the game).
    \begin{framed}
        It might be usefull if our algorithm takes too much time to compute a given depth, interrupting the execution and allowing for it to recompute at a lower depth may be wiser than keep going on and wasting lots of time credit. \newline

        Changing the depth depending on the game state would be an excellent decision (which we also implemented) as the time to compute a given depth will be reduced as the game enfolds. As such a depth 2 which took 6 seconds at the beginning of the game might take only half a second at step 20. Implementing a changing cutoff function is thus a must have for the contest as seeing deeper allows an algorithm to perform better results.
    \end{framed}
    \item[14.] Describe your cut-off function.
    \begin{framed}
        Our cutoff function will use a precomputed maximum depth and a precomputed maximum time to run and proceeds as follow :

        \begin{itemize}
            \item A computation with a \textbf{depth lower than 3} will always wait to finish and will never be interrupted due to a lack of time as we consider that we need at least that to play a decent move.
            \item If the \textbf{depth is bigger than 2 and we aren't in lack of time}, cutoff will only return true if the current depth equals the maximum allowed depth that we precomputed earlier.
            \item If the depth is \textbf{bigger than 2 but we exceeded our decided time limit} for this step, our algorithm will process the rest of the tree in depth 2 to avoid wasting too much time. We infered after much reflexion that it would be better to play a good move where we have fully seen the outcome rather than playing blind and see what happens but it is also better to play blind than to play what we know is a bad move. Thus this seemed to be the best solution and this works normally well with our algorithm as we reorder the subtree in such a way that the most interesting actions will be tested toward the beginning.
        \end{itemize}
    \end{framed}
\end{enumerate}

\subsection{A Smart Alpha-Beta Agent}

\begin{enumerate}
    \item[15.] Upload your agent to the INGInious \textit{Assignment 3 - Avalam: Super Agent} task. Your agent will play a match against a simple alpha-beta player. Each agent will have a time credit of 2 . 5 minutes. If you win against this agent, you get all the points for the question. If you succeed to perform a draw, you get half of the points and if get beaten, you don’t get any point. Don’t forget to include all the needed files inside the archive you upload.
    \begin{figure}[!ht]
    \begin{framed}
        \centering
        \includegraphics[width=\linewidth]{test_super_agent.png}
        \caption{INGInious result on super agent}
    \end{framed}
    \end{figure}
\end{enumerate}

\subsection{Contest}

\begin{enumerate}
    \item[16.] Upload your agent to the INGInious \textit{Assignment 3 - Avalam: Contest Agent} task. Your agent will play a match against a basic alpha-beta player. Each agent will have a time credit of 2 . 5 minutes (for the real contest, your agent will have a time credit of 20 minutes per match). If you win against this agent, you get all the points for the question. If you succeed to perform a draw, you get half of the points and if get beaten, you don’t get any point. Don’t forget to include all the needed files inside the archive you upload.
        \begin{figure}[!ht]
        \begin{framed}
            \centering
            \includegraphics[width=\linewidth]{test_contest_agent.png}
            \caption{INGInious result on contest agent}
        \end{framed}
        \end{figure}
    \item[17.] Describe concisely your super tough agent in your report. \textbf{Your description shouldn’t be longer than one A4 paper page; if it is the same agent than the one described in earlier sections, just state it, no need to re-explain.}
    \begin{framed}
        Our super tough contest performs the same behaviour we described previously in this report. There is however a feature in our code that we didn't have the occasion to mention previously. \newline

        When our board execute the minimax function, it saves at each steps the combinaison of moves that lead to that given state to avoid visiting symmetric boards.
    \end{framed}
\end{enumerate}


\section{The \removecopyii algorithm (number of copied elements)}
\label{sec:removecopyii}

In this section we improve the contract of \specref{removecopy}
by formally specifying the number~\inl{\\result} of elements copied by \removecopy.

The number of copied elements equals of course the number of elements
in the input range \inl{a[0..n-1]} that are different from~\inl{v}.
One can formally describe this number by relying on the logic function~\logicref{Count}.

\begin{lstlisting}[style=acsl-block]

  logic integer
  CountNotEqual(value_type* a, integer n, value_type v) =  n - Count(a, n, v);
\end{lstlisting}

In fact, we have used this kind of definition in earlier version of this document.
We have found it, however, worthwhile to provide a separate definition of \CountNotEqual
and express the relationship with \Count as a lemma.
This definition is shown in the Listings~\ref{logic:CountNotEqual-1}
and~\ref{logic:CountNotEqual-2}.

\begin{logic}[hbt]
\begin{minipage}{\textwidth}
\lstinputlisting[linerange={1-34}, style=acsl-block, frame=single]{Source/CountNotEqual.acsl}
\end{minipage}
\caption{\Label{logic:CountNotEqual-1} The logic function \CountNotEqual (1)}
\input{Listings/CountNotEqual.acsl.labels.tex}
\input{Listings/CountNotEqual.acsl.index.tex}
\end{logic}

\clearpage

The  above mentioned relationship with \logicref{Count} is expressed as
lemma \logicref{CountNotEqualCount} in the following listing.

\begin{logic}[hbt]
\begin{minipage}{\textwidth}
\lstinputlisting[linerange={35-60}, style=acsl-block, frame=single]{Source/CountNotEqual.acsl}
\end{minipage}
\caption{\Label{logic:CountNotEqual-2} The logic function \CountNotEqual (2)}
\end{logic}

\FloatBarrier

\subsection{Formal specification of \removecopyii}

We extend our formal specification by using \logicref{CountNotEqual}
and add the new postcondition \inl{size}, which states that the returning value
of \removecopyii equals \CountNotEqual.
The following listing shows the formal specification of \specref{removecopyii}.

\input{Listings/remove_copy2.h.tex}

\subsection{Implementation of \removecopyii}

The following listing shows the implementation of our
extended of \removecopyii.
Here we added the loop invariant \inl{size} which corresponds to the
postcondition in \specref{removecopyii}.
In order to ensure that the loop invariant \inl{size} can be verified
we have added the assertions \inl{size} and \inl{unchanged}.

\input{Listings/remove_copy2.c.tex}

While we now can precisely speak of the number of copied elements,
it is still not possible to say something about the exact relationship between
the elements of range~\inl{a[0..n-1]} and range~\inl{b[0..n-1]}.
We will address this question the contract of \specref{removecopyiii}.

\clearpage


\subsection{Plots}\label{subsec:plots}

\begin{figure}[H]
\begin{center}
%Here begins the 3d plot
\begin{tikzpicture}
\begin{axis}
\addplot3[
    surf,
]
{exp(-x^2-y^2)*x};
\end{axis}
\end{tikzpicture}
\end{center}
\caption{Eigens erstellter Plot mit dem Paket libz und pgfplots}
\end{figure}

\begin{figure}[H]
\begin{center}
\begin{tikzpicture}
\begin{axis}[
    axis lines = left,
    xlabel = $x$,
    ylabel = {$f(x)$},
]
%Below the red parabola is defined
\addplot [
    domain=-10:10,
    samples=100,
    color=red,
]
{x^2 - 2*x - 1};
\addlegendentry{$x^2 - 2x - 1$}
%Here the blue parabloa is defined
\addplot [
    domain=-10:10,
    samples=100,
    color=blue,
    ]
    {x^2 + 2*x + 1};
\addlegendentry{$x^2 + 2x + 1$}

\end{axis}
\end{tikzpicture}
\end{center}
\caption{Eigens erstellter Plot mit dem Paket libz und pgfplots}
\end{figure}


\chapter{Python Programs}

Since Matlab requires a licence, we have also included Python versions of some of the Matlab programs. These programs have been tested in Python 2.7 (which can be obtained from \url{http://python.org/}), they also require Matlplotlib (version 1.10, which can be obtained from \url{http://matplotlib.sourceforge.net/index.html}), Mayavi (\url{http://github.enthought.com/mayavi/mayavi/index.html}) and numpy (\url{http://numpy.scipy.org/}). These programs have been tested primarily with the Enthought Python distribution.

\lstinputlisting[style=python_style,language=Python,label=lst:instability,caption={A Python program to demonstrate instability of different time-stepping methods.  Compare this to the Matlab implementation in listing \ref{lst:MatlabInstability}.}]{./PythonPrograms/Programs/PythonCode/Simple_ODE_Example_of_Unstable_FE.py}

\lstinputlisting[style=python_style,language=Python,label=lst:HeatFE1dPython,caption={A Python program to solve the heat equation using forward Euler time-stepping.  Compare this to the Matlab implementation in listing \ref{lst:HeatFE1dMatlab}.}]{./PythonPrograms/Programs/PythonCode/Heat_Eq_1D_Spectral_FE.py}

\lstinputlisting[style=python_style,language=Python,label=lst:HeatBE1dPython,caption={A Python program to solve the heat equation using backward Euler time-stepping.  Compare this to the Matlab implementation in listing \ref{lst:HeatBE1dMatlab}.}]{./PythonPrograms/Programs/PythonCode/Heat_Eq_1D_Spectral_BE.py}

\lstinputlisting[style=python_style,language=Python,label=lst:AllenCahnIE2dPython,caption={A Python program to solve the 2D Allen Cahn equation using implicit explicit time-stepping.  Compare this to the Matlab implementation in listing \ref{lst:AllenCahnIE2dMatlab}.}]{./PythonPrograms/Programs/PythonCode/Allen_Cahn_2D_Spectral_IE.py}

\lstinputlisting[style=python_style,language=Python,label=lst:BEFPiterPython,caption={A Python program to demonstrate fixed-point iteration. Compare this to the Matlab implementation in listing \ref{lst:BEFPiterMatlab}.}]{./PythonPrograms/Programs/PythonCode/BackwardEulerFixedPoint.py}

\lstinputlisting[style=python_style,language=Python,label=lst:BENewIterPython,caption={A Python program to demonstrate Newton iteration. Compare this to the Matlab implementation in listing \ref{lst:BENewIterMatlab}.}]{./PythonPrograms/Programs/PythonCode/BackwardEulerNewtonIteration.py}

\lstinputlisting[style=python_style,language=Python,label=lst:OdeStrangPython,caption={A Python program which uses Strang splitting to solve an ODE. Compare this to the Matlab implementation in listing \ref{lst:OdeStrangMatlab}.}]{./PythonPrograms/Programs/PythonCode/ODEsplittingStrang.py}

\lstinputlisting[style=python_style,language=Python,label=lst:NlsSplit1DPython,caption={A Python program which uses Strang splitting to solve the one-dimensional nonlinear Schr\"{o}dinger equation. Compare this to the Matlab implementation in listing \ref{lst:NlsSplit1DMatlab}.}]{./PythonPrograms/Programs/PythonCode/NLSsplitting1D.py}

\lstinputlisting[style=python_style,language=Python,label=lst:NlsSplit2DPython,caption={A Python program which uses Strang splitting to solve the two-dimensional nonlinear Schr\"{o}dinger equation. Compare this to the Matlab implementation in listing \ref{lst:NlsSplit2DMatlab}.}]{./PythonPrograms/Programs/PythonCode/NLSsplitting2D.py}

\lstinputlisting[style=python_style,language=Python,label=lst:NlsSplit3DPython,caption={A Python program which uses Strang splitting to solve the three-dimensional nonlinear Schr\"{o}dinger equation. Compare this to the Matlab implementation in listing \ref{lst:NlsSplit3DMatlab}.}]{./PythonPrograms/Programs/PythonCode/NLSsplitting3D.py}

\lstinputlisting[style=python_style,language=Python,label=lst:Ns2DPython,caption={A Python program which finds a numerical solution to the 2D Navier-Stokes equation. Compare this to the Matlab implementation in listing \ref{lst:Ns2DMatlab}.}]{./PythonPrograms/Programs/PythonCode/NavierStokes2DFFTCn.py}

\lstinputlisting[style=python_style,language=Python,label=lst:Kg1DPython,caption={A Python program to solve the one-dimensional Klein Gordon equation \eqref{eq:KleinGordon} using the time discretization in eq.\ \eqref{eq:KgImEx}. Compare this to the Matlab implementation in listing \ref{lst:MatKg1D}.}]{./PythonPrograms/Programs/PythonCode/KleinGordon1D.py}

\lstinputlisting[style=python_style,language=Python,label=lst:Kg1Dimp,caption={A Python program to solve the one-dimensional Klein Gordon equation \eqref{eq:KleinGordon} using the time discretization in eq.\ \eqref{eq:KgImp}. Compare this to the Matlab implementation in listing \ref{lst:MatKg1Dimp}.}]{./PythonPrograms/Programs/PythonCode/KleinGordon1Dimp.py}

\lstinputlisting[style=python_style,language=Python,label=lst:Kg2DPython,caption={A Python program to solve the two-dimensional Klein Gordon equation \eqref{eq:KleinGordon} using the time discretization in eq.\ \eqref{eq:KgImp}. Compare this to the Matlab implementation in listing \ref{lst:MatKg2D}.}]{./PythonPrograms/Programs/PythonCode/KleinGordonImp2Db.py}

\lstinputlisting[style=python_style,language=Python,label=lst:Kg2DPython,caption={A Python program to solve the two-dimensional Klein Gordon equation \eqref{eq:KleinGordon} using the time discretization in eq.\ \eqref{eq:KgImp}. Compare this to the Matlab implementation in listing \ref{lst:MatKg2D}.}]{./PythonPrograms/Programs/PythonCode/KleinGordonImp2Db.py}

\lstinputlisting[style=python_style,language=Python,label=lst:Kg2DPython,caption={A Python program to solve the two-dimensional Klein Gordon equation \eqref{eq:KleinGordon} using the time discretization in eq.\ \eqref{eq:KgImp}. Compare this to the Matlab implementation in listing \ref{lst:MatKg2D}.}]{./PythonPrograms/Programs/PythonCode/KleinGordonImp2Db.py}

\lstinputlisting[style=python_style,language=Python,label=lst:Kg3DPython,caption={A Python program to solve the three-dimensional Klein Gordon equation \eqref{eq:KleinGordon} using the time discretization in eq.\ \eqref{eq:KgImEx}. Compare this to the Matlab implementation in listing \ref{lst:MatKg3D}.}]{./PythonPrograms/Programs/PythonCode/KleinGordonImp3D.py}


\chapter{Mathematical Background}\label{math}
\section{Introduction}
Discontinuous Galerkin finite element method is an amalgamation of the element based Galerkin schemes and
the finite volume method. It is a compact, higher-order accurate scheme where the solution can be
discontinuous between two elements.
The DG scheme requires the physical domain ($\Omega$) discretized with $Ne$ finite elements (written as $\Omega^k$).
The finite element cells are assumed to be one of the following types.
\begin{enumerate}
	\item Hexahedral ($8$ edges, $8$ nodes, $6$ faces)
	\item Tetrahedral ($6$ edges, $4$ nodes, $4$ faces)
	\item Prism ($9$ edges, $6$ nodes, $5$ faces)
	\item Pyramid ($8$ edges, $5$ nodes, $5$ faces)
\end{enumerate}
The solution is assumed to be continuous (along with continuous derivatives depending on the governing equations) inside
the element. However, the solution can be discontinuous at the edges and faces. The value of the conserved variable (and
the numerical fluxes) at the faces and edges are found out by solving the Riemann problem which arises because of the
discontinuity.

The DG method essentially starts with the `variational' formulation of the problem. Consider a
differential equation given by,
\begin{equation}\label{diffeqn}
	\mathbf{L}(u) = f(u),  u\in{H^1(\Omega)}
\end{equation}
(and suitable boundary conditions, not mentioned here).
For example, $\mathbf{L} = \frac{d}{dx}$ and $f(u) = u$ results in,
\begin{equation}\label{diffeqn_ex1}
	\frac{du}{dx} = u
\end{equation}
We will be using the general notation given by equation \ref{diffeqn} henceforth.
Then, if $u^k$ is the approximate solution inside of the element, then instead of the solving equation \ref{diffeqn}
with $u^k$, we solve the following variational formulation of equation \ref{diffeqn}.
\begin{equation}\label{diffeqnvr}
	\int_{\Omega_k} \phi \mathbf{L}(u^k) d\Omega = 	\int_{\Omega_k} \phi f(u^k) d\Omega
\end{equation}
$\phi$ is the test function. The approximate solution $u^k$ is obtained by polynomial approximation of $u$ inside $\Omega^k$.
A modal interpolation (basis functions consisting of modes, i.e. frequencies) or a nodal interpolation (basis functions
consist of the cardinal polynomials such as the Lagrange polynomials) can be used for this.
In the DG methods, the basis functions serve as the test functions.
The integrations in equation \ref{diffeqn_ex1} are solved numerically using quadrature formulas.
The resulting algebraic system is solved iteratively (or ODE is solved numerically if time dependant).
This process is repeated till convergence (for steady state
problems) or till the final time is reached (for transient problems).
It is easier to solve the problem on a reference cell (canonical element) and then map the solution on to the physical cell.
This requires a mapping to be defined between the reference cell and the physical cell.
Based on this general process, the following mathematical concepts are revised in this chapter.
\begin{enumerate}
	\item Mapping functions (Jacobian ($\mathbf{J}$) of transformation, inverse Jacobian ($\mathbf{J}^{-1}$) and ($|\mathbf{J}|$))
	\item Modal and nodal basis functions
	\item Polynomial interpolation and differentiation
	\item Numerical quadrature (integration)
	\item Cell matrices (Vandermonde matrix, Mass matrix, differentiation matrix and flux matrix)
	\item Other mathematical functions
\end{enumerate}
The discussion is kept brief and concise and only those concepts concerned with MEAN-DG are discussed.
The user is encouraged to go through the references for in-depth theory of DG schemes.

\begin{note}
	We use the c++ style indexing. i.e. indices start at $0$ (not $1$).
\end{note}

\section{Mapping on the Reference Element}
Corresponding to each physical cell, there exists a canonical (reference) element.
The physical cell is expressed in terms of the physical coordinate system $(x,y,z)$. The reference coordinate system is
written in terms of  $(r,s,t)$.
Thus, each of the physical coordinates can be written in terms of the $(r,s,t)$ coordinate system. In the code, we
describe the physical coordinate system as $XYZ$, and the reference system as $RST$.
The mapping from the physical coordinate system to the reference coordinate system is characterized by the Jacobian of
transformation $\mathbf{J}$.
In this section, we describe the Jacobian of transformation for the four types of the cells.

\subsection{Hexahedral cells}
Hexahedral, or Hex in short, cells are mapped to a cube in 3D. The Hex cell has $8$ nodes given by the array,
$\left((x_0,y_0,z_0), (x_1, y_1, z_1), ... ,(x_7,y_7,z_7)\right)$.
We define the linear shape functions on the reference element as follows:
\begin{equation}\label{shape0}
	N_0 = \frac{1}{8} (1-r) (1-s) (1-t), \hspace{5mm} \{r,s,t \in V | \forall v\in V \subset  {\mathbb{R}} , ~ v \in [-1,1]\}
\end{equation}
\begin{equation}\label{shape1}
	N_1 = \frac{1}{8} (1+r) (1-s) (1-t)
\end{equation}
\begin{equation}\label{shape2}
	N_2 = \frac{1}{8} (1+r) (1+s) (1-t)
\end{equation}
\begin{equation}\label{shape3}
	N_3 = \frac{1}{8} (1-r) (1+s) (1-t)
\end{equation}
\begin{equation}\label{shape4}
	N_4 = \frac{1}{8} (1-r) (1-s) (1+t)
\end{equation}
\begin{equation}\label{shape5}
	N_5 = \frac{1}{8} (1+r) (1-s) (1+t)
\end{equation}
\begin{equation}\label{shape6}
	N_6 = \frac{1}{8} (1+r) (1+s) (1+t)
\end{equation}
\begin{equation}\label{shape7}
	N_7 = \frac{1}{8} (1-r) (1+s) (1+t)
\end{equation}

The mapping then is simply given as,

\begin{equation}\label{mapx}
	x(r,s,t) = \sum_{i=0}^7 N_i x_i
\end{equation}

\begin{equation}\label{mapy}
	y(r,s,t) = \sum_0^7 N_i y_i
\end{equation}

\begin{equation}\label{mapz}
	z(r,s,t) = \sum_0^7 N_i z_i
\end{equation}

\begin{definition}\label{Jacobian}
	The Jacobian of transformation ($\mathbf{J}$) is defined as :
	\begin{equation}\label{J}
		{\setstretch{2}
		\mathbf{J} = \left[\begin{array}{c c c}
			\frac{\partial x}{\partial r} & \frac{\partial x}{\partial s} &  \frac{\partial x}{\partial t} \\
			\frac{\partial y}{\partial r} & \frac{\partial y}{\partial s} &  \frac{\partial y}{\partial t} \\
			\frac{\partial z}{\partial r} & \frac{\partial z}{\partial s} &  \frac{\partial z}{\partial t} \\
												                   \end{array} \right]
			= \left[\begin{array}{c c c}
				J_{00} & J_{01}  & J_{02}   \\
				J_{10} & J_{11}  & J_{12}   \\
				J_{20} & J_{21}  & J_{22}   \\
												                   \end{array} \right]
	        }
	\end{equation}
\end{definition}
The partial derivatives in equation \ref{J} can be easily computed from equations \ref{shape0}-\ref{shape7} and \ref{mapx}, \ref{mapy}, \ref{mapz}.
In the code, the above implementation is found in Cell.cpp.
\begin{verbatim}
Cell::calculateJacobian3DTensor(double r,double s,double t)
\end{verbatim}
\begin{note}
	The first name indicates the name of the class (Cell), the second symbol (::) indicates the scope (in this case, indicating
	'belongs to Cell' the third name is the name of the class method (calculateJacobian3DTensor) and terms in the bracket being
	arguments passed to this function.
\end{note}
It is to be noted that the name `Tensor' in the above function indicates that the functional space for a Hex cell is
obtained by a tensor product (outer product) of 1D functional spaces.

The inverse of the Jacobian is found out by the standard process of inversion of a matrix.

\begin{equation}\label{JInv}
	{\setstretch{2}
	\mathbf{J^{-1}} = \left[\begin{array}{c c c}
		\frac{\partial r}{\partial x} & \frac{\partial r}{\partial y} &  \frac{\partial r}{\partial z} \\
		\frac{\partial s}{\partial x} & \frac{\partial s}{\partial y} &  \frac{\partial s}{\partial z} \\
		\frac{\partial t}{\partial x} & \frac{\partial t}{\partial y} &  \frac{\partial t}{\partial z} \\
											                   \end{array} \right]
        }
\end{equation}
These are given as:
\begin{equation}\label{metricTerms00}
	\frac{\partial r}{\partial x} = \frac{1}{|{\mathbf{J}}|} \left( J_{22}J_{11} - J_{21}J_{12}\right)
\end{equation}
\begin{equation}\label{metricTerms01}
	\frac{\partial r}{\partial y} = \frac{-1}{|{\mathbf{J}}|} \left( J_{22}J_{01} - J_{21}J_{02}\right)
\end{equation}
\begin{equation}\label{metricTerms02}
	\frac{\partial r}{\partial z} = \frac{1}{|{\mathbf{J}}|} \left( J_{12}J_{01} - J_{11}J_{02}\right)
\end{equation}
\begin{equation}\label{metricTerms10}
	\frac{\partial s}{\partial x} = \frac{-1}{|{\mathbf{J}}|} \left( J_{22}J_{10} - J_{20}J_{12}\right)
\end{equation}
\begin{equation}\label{metricTerms11}
	\frac{\partial s}{\partial y} = \frac{1}{|{\mathbf{J}}|} \left( J_{22}J_{00} - J_{20}J_{02}\right)
\end{equation}
\begin{equation}\label{metricTerms12}
	\frac{\partial s}{\partial z} = \frac{-1}{|{\mathbf{J}}|} \left( J_{12}J_{00} - J_{10}J_{02}\right)
\end{equation}
\begin{equation}\label{metricTerms20}
	\frac{\partial t}{\partial x} = \frac{1}{|{\mathbf{J}}|} \left( J_{21}J_{10} - J_{20}J_{11}\right)
\end{equation}
\begin{equation}\label{metricTerms21}
	\frac{\partial t}{\partial y} = \frac{-1}{|{\mathbf{J}}|} \left( J_{21}J_{00} - J_{20}J_{01}\right)
\end{equation}
\begin{equation}\label{metricTerms22}
	\frac{\partial t}{\partial z} = \frac{1}{|{\mathbf{J}}|} \left( J_{11}J_{00} - J_{10}J_{01}\right)
\end{equation}
where, the determinant of the Jacobian $|{\mathbf{J}}|$ is calculated as:
\begin{equation}\label{detJ}
	|{\mathbf{J}}| = J_{00} (J_{22}J_{11} - J_{21}J_{12}) - J_{01} (J_{22}J_{10} - J_{20}J_{12})  + J_{02} (J_{21}J_{10} - J_{20}J_{11})
\end{equation}
The inverse of Jacobian is implemented in
\begin{verbatim}
Cell::calculateInverseJacobianMatrix3DTensor(arguments)
\end{verbatim}

\begin{note}
	The inverse of Jacobian is called as {\bf dRST\_by\_dXYZ} in the code. Each cell stores these matrices for all the quadrature
	points within that cell. The metric terms given by \ref{metricTerms00}--\ref{metricTerms22}
	are used in computations involving derivatives as seen in the subsequent sections and chapters.
\end{note}
The inverse Jacobian is stored for all the quadrature points for all the cells. It is an attribute of the cell
(i.e. Cell::dRST\_by\_dXYZ) and has dimensions $nQuad \times 3 \times 3$.

{\bf Use of the Jacobian}
Consider the following integration:
\begin{equation}\label{J_example}
	I = \int_{\Omega^k} \phi(x,y,z) d\Omega^k
\end{equation}
The integration can be written as,
\begin{equation}
	I = \int_{\Omega^s} \phi(r,s,t) |\mathbf{J}| d\Omega^s
\end{equation}
where, $\Omega^s$ is a reference element and $\mathbf{J}$ is the Jacobian matrix.
The main benefit of performing the above conversion is that, the function $\phi(r,s,t)$ needs to be computed only once
for the standard element, each cell will have its own Jacobian matrix, so number of computations reduce drastically.

In the current code, the vertices and faces are numbered in a standardized fashion as shown in Fig.\ref{RC}.

\begin{figure}[h]
	\begin{minipage}[t]{0.5 \textwidth}
		\begin{center}{\includegraphics[width=0.9\textwidth]{RefCellFace.pdf}}
		\end{center}
	\end{minipage}%
	\begin{minipage}[t]{0.5 \textwidth}
		\begin{center}{\includegraphics[width=0.9\textwidth]{RefCellVertex.pdf}}
		\end{center}
	\end{minipage}%
	\caption{\label{RC}a. Reference cell and faces relative numbering \hspace{2mm} b. Reference cell and vertices numbering}
\end{figure}

The corresponding implementation can be found in:
\begin{verbatim}
Cell::orderDefiningPoints()
\end{verbatim}

\section{Mapping onto the Reference Face}
DG method involves evaluation of surface integrations in order to find the numerical flux at the cell-interface.
The surface integrations in turn require mapping of a given arbitrary surface on the reference surface.
The parameterization of a plane surface requires two parameters $r$ and $s$.

\subsection{Quadrilateral Faces}
For a quadrilateral faces, the parameters assume the following values: $(r,s) \in [-1,1]$.
However, the given surface can be arbitrarily oriented in the space, and thus requires $(x,y,z)$ position
to be completely defined.
We thus need to orient the surface such as one of the variables becomes redundant. This means rotating
the surface such as to make it parallel to $xy$ plane, for example.
Thus, each surface needs to be re-oriented in space such that the face normal $\hat{n}$ becomes
parallel to the $z$ axis. After the we can map the rotated face onto the standard element.
Let $\theta_z$ be the angle made by the face normal with the $z$ axis and $\theta_x$ be the angle made by
the face normal with the $x$ axis.
The rotation matrix rotates the face through $\theta_x$ first (counterclockwise) and the through $\theta_z$
counterclockwise. The resulting rotation matrix $R$ is given as:
\begin{equation}\label{rotMat1}
	R = <R_z, R_x>
\end{equation}
where,
\begin{equation}\label{rotMat}
	{\setstretch{1.5}
	R_x =
	 \left[\begin{array}{c c c}
				cos(\theta_x) & sin(\theta_x)  & 0  \\
			       -sin(\theta_x) & cos(\theta_x)  & 0  \\
				       0      &       0        & 1  \\
         \end{array} \right];
\hspace{5mm}
	R_z =
	 \left[\begin{array}{c c c}
				cos(\theta_z)   & 0 & -sin(\theta_z) \\
				       0        & 1 &       0       \\
			        sin(\theta_z)   & 0 & cos(\theta_z) \\
         \end{array} \right];
 }
 \end{equation}
 and $<a,b>$ indicates the dot product between matrices $a$ and $b$.
 Matrix $R^{-1}$ indicates the reverse transformation.
 The rotation matrix is calculated in,
 \begin{verbatim}
 Face::calculateRotationMatrixParallelToXY()
 \end{verbatim}
 and the inverse matrix is computed in,
 \begin{verbatim}
 Face::calculateInverseRotationMatrix()
 \end{verbatim}
 In order to describe the procedure to compute the Jacobian of transformation ($|{\mathbf{J}^f}|$) for face,
 we use the following nomencleture:
 \begin{itemize}
	 \item $(x,y,z)$: physical coordinate system (actual coordinates)
	 \item $(x', y', z')$: Rotated coordinates such that the face is $||$ to the $xy$ plane
	 \item $(r,s)$: Reference coordinates such that $r,s \in [-1,1]$
 \end{itemize}
 There are two ways the Jacobian can be computed.

 \subsubsection{Method 1}
 First, rotate each vertex of the face using $R$.
 i.e.
 \begin{equation}
	 {\setstretch{1.5}
	 \left[\begin{array}{c}
			  x'_i  \\
			  y'_i  \\
			  z'_i \\
	  \end{array} \right]
	  =
	  R\left(
	 \left[\begin{array}{c}
			  x_i  \\
			  y_i  \\
			  z_i \\
	  \end{array} \right]
	  \right), \hspace{5mm} i \in \{0,1,2,3\}
  }
 \end{equation}
 Each of the rotated points is then mapped to the standard element.
 After rotation of the face, $\frac{\partial z'}{\partial r} = 0, \frac{\partial z'}{\partial s}=0$ since
 $z'$ remains constant.
The mapping from the rotated face $f'$ to the standard face $f^s$ can be defined through the shape functions.
\begin{equation}\label{shape0f}
	N_0 = \frac{1}{4} (1-r) (1-s) , \hspace{5mm} \{r,s \in V | \forall v \in V \subset {\mathbb{R}}  , ~ v \in [-1,1]\}
\end{equation}
\begin{equation}\label{shape1f}
	N_1 = \frac{1}{4} (1+r) (1-s)
\end{equation}
\begin{equation}\label{shape2f}
	N_2 = \frac{1}{4} (1+r) (1+s)
\end{equation}
\begin{equation}\label{shape3f}
	N_3 = \frac{1}{4} (1-r) (1+s)
\end{equation}
Then,
\begin{equation}\label{faceMap}
	x' = \sum_{i=0}^3 N_i x'_i, \hspace{4mm}y' = \sum_{i=0}^3 N_i y'_i,
\end{equation}
Equation\ref{faceMap} can be used to find partial derivatives of $x'$ and $y'$ with respect to $r$ and $s$.
The required face Jacobian is:
 \begin{equation}\label{method1J}
	 {\setstretch{1.5}
	 \mathbf{J}^f =
	 \left[\begin{array}{c c}
			 \frac{\partial x}{\partial r}  & \frac{\partial x}{\partial s}  \\
			 \frac{\partial y}{\partial r}  & \frac{\partial y}{\partial s}  \\
			 \frac{\partial z}{\partial r}  & \frac{\partial z}{\partial s}  \\
         \end{array} \right]
 }
 \end{equation}
 which can be written as,
 \begin{equation}\label{method1J}
	 {\setstretch{1.5}
	 \mathbf{J}^f =
	 \left[\begin{array}{c}
			  x  \\
			  y  \\
			  z  \\
	  \end{array} \right].
	  \left[\begin{array}{c c}
		  \frac{\partial}{\partial r} &  \frac{\partial}{\partial s}\\
		  \end{array} \right]
		  = R^{-1}
	 \left[\begin{array}{c}
			  x'  \\
			  y'  \\
			  z'  \\
	  \end{array} \right].
	  \left[\begin{array}{c c}
		  \frac{\partial}{\partial r} &  \frac{\partial}{\partial s}\\
		  \end{array} \right]
 }
 \end{equation}
i.e.
\begin{equation}\label{method1J1}
	\mathbf{J}^f = R^{-1} \mathbf{J}^{f'}
\end{equation}
where,
\begin{equation}\label{method1J'}
        {\setstretch{1.5}
		\mathbf{J}^{f'} =
        \left[\begin{array}{c c}
       		 \frac{\partial x'}{\partial r}  & \frac{\partial x'}{\partial s}  \\
       		 \frac{\partial y'}{\partial r}  & \frac{\partial y'}{\partial s}  \\
				0		 &             0		   \\
        \end{array} \right]
}
\end{equation}
The procedure is summarized as,
\begin{enumerate}
        \item Rotate the face so as to make it parallel to the $xy$ plane.
	\item Find the Jacobian of transformation from the rotated face to the standard face ($\mathbf{J}^{f'}$).
	\item Find the Jacobian of the face as given by equation \ref{method1J1}.
	\item $|\mathbf{J}^{f}|$ is given as $\sqrt{|\left(\mathbf{J}^{f}\right)^{T}\left(\mathbf{J}^{f}\right)|}$
\end{enumerate}

\subsubsection{Method 2}
In this method, we parametarize the surface $x,y,z$ with parameters $r,s$.
Let $$X = x\hat{i} + y\hat{j} + z\hat{k}$$
We use the standard shape functions to map the physical element to parameters $r,s$.
i.e.
\begin{equation}\label{faceMap1}
	x = \sum_{i=0}^3 N_i x_i, \hspace{4mm}y = \sum_{i=0}^3 N_i y_i, \hspace{4mm}z = \sum_{i=0}^3 N_i z_i,
\end{equation}
where same shape functions are used as earlier.
\begin{note}
In order to find the vertex mapping of a randomly oriented face with the standard
element, it would be advisable to first rotate the face to make it parallel to the $xy$ plane and then
find the vertex mapping. Then find the shape function and proceed with equation \ref{faceMap1}.
However, we do not use the coordinates of the rotated face here.
\end{note}
After that, we find $\frac{\partial X}{\partial r}$ and $\frac{\partial X}{\partial s}$.
Then $|\mathbf{J}^{f}|$  is simply $\left|\left| \frac{\partial X}{\partial r} \times \frac{\partial X}{\partial s}\right|\right|$
Both of these methods are implemented in
\begin{verbatim}
	Face::calculateJacobianQuadFace(double r, double s)
\end{verbatim}
Any given quadrature point $(r,s)$ can be mapped into $(x,y,z)$ space using the two operations:
\begin{enumerate}
	\item First find mapping from $(r,s)$ to $(x',y',z')$ using equations \ref{shape0f}-\ref{shape3f} and
		\ref{faceMap}.
	\item Then map from $(x',y',z')$ to physical coordinates using $R^{-1}$.
\end{enumerate}
The global positioning of all the quadrature points on each face is
stored in array
\begin{verbatim}
	Face::quadPointsGlobalLocation
\end{verbatim}
Index $i$ runs over the surface quadrature points and index $j$ runs over the $x,y,z$ axes.
\begin{note}
	Some of the volume quadrature points coincide with the surface quadrature points. Function \begin{verbatim}DG::mapFaceQuadPointsToCellQuadPoints() \end{verbatim} performs the mapping by direct comparison of global positioning of surface and volume quadrature points.
\end{note}
\noindent
The mapping of surface to volume quadrature points is stored in arrays \newline Face::mapOwnerQuadPoints[~] and Face::mapNeighbourQuadPoints[~].

Thus, if we are finding a $2^{nd}$-order polynomial for a face, then it should have $9$ quadrature points (QP).
The corresponding cell will meanwhile have $27$  quadrature points, $9$ of which will coincide with the face QP.
Then if
\begin{verbatim}
	Face::mapOwnerQuadPoints = [3, 2, 21, 14, ... , 7]
\end{verbatim}
Then, it means the $0^{th}$ quad point on face is same as the $3^{rd}$ quad point of owner cell and so on.

\begin{note}
	In the improved version of the code, the face-vertex numbering is standardized, such that the vertex numbering follows the right-hand rule.
	i.e. if the curled fingers indicate the sequence of vertices, then the stretched thumb points in the direction of hte face-normal.
	In that case, the rotation matrices are not required. i.e. both method 1 and method 2 are obsolete.
	Refer:
	\begin{verbatim}
	Face::orderDefiningPoints()
	\end{verbatim}
\end{note}

\section{Modal and Nodal Basis Functions}
Basis functions are the independant dimensions on which any function can be projected. In this chapter, we present a few
modal and nodal basis functions.

\subsection{Modal Basis Functions}
These are the set of basis functions, where each dimension corresponds to a `mode' or frequency. A simple example is
a set of monomials defined on the interval $[a,b]$
\begin{note}
This is just an example, in reality $x$ could be valid over $(-\infty, \infty)$
\end{note}
\begin{equation}
	V_{mono}^K = \{1, x, x^2, x^3, ..., x^{K-1}\} ,\hspace{4mm} x\in C^0[a,b]
\end{equation}
A polynomial up to order $K-1$ can be represented exactly using $K$ modal functions.
Thus, any general polynomial can be written as
\begin{equation}
	P^K(x) = \sum_{i=0}^{i=K-1} \alpha_{i} x^{i}, ,\hspace{4mm} x\in C^0[a,b]
\end{equation}
The monomials is not a `good' choice of the basis functions as we will see later, thus we need a set of basis functions
where each of the functions is `orthogonal' (or better `orthonormal') to every other functions.
One such important set of orthogonal functions
is called as `Jacobi' polynomials. A special case of `Jacobi' polynomials is `Legendre' polynomials.
In our code, we have extensively used Legendre polynomials.
A general $i^{th}$  modal basis function is indicated by symbol $\psi_i$.
Other popular example of modal basis functions is a set of Chebyshev polynomials.
\begin{figure}[H]
	\begin{center}
		\includegraphics[width=0.8\textwidth]{Legendre.pdf}
		\caption{\label{Legendre} First $9$ Legendre polynomials}
	\end{center}
\end{figure}
Fig.\ref{Legendre} shows first $9$ Legendre polynomials. Observe that the `frequency' of the polynomials increases  (for increasing
count) indicating higher modes. Fig.\ref{Cheb} shows Chebyshev polynomials.
\begin{figure}[H]
	\begin{center}
		\includegraphics[width=0.8\textwidth]{Chebyshev.pdf}
		\caption{\label{Cheb} First $9$ Chebyshev polynomials}
	\end{center}
\end{figure}
\noindent The $1$D modal functions are implemented in:
\begin{verbatim}
FunctionalSpace::monomials1D(args),
FunctionalSpace::legendrePolynomial1D(args),
FunctionalSpace::chebyshevPolynomial1D(args) and
FunctionalSpace::jacobiPolynomial1D(args)
\end{verbatim}
It is indeed difficult to comprehend how the polynomials shown in Fig.\ref{Legendre} and Fig.\ref{Cheb} be `orthogonal'.
The main reason for this is that we try to stick to our understanding of the $3$D space and the notion of
orthogonality that comes with it, however, the polynomial space is essentially $\infty$ dimensional space. For
this space, the definition of orthogonality depends on the definition of the Inner-Product and `metric' of that space.
For $L^2$ space, the orthogonality is defined as,
\begin{equation}
	\int_{\Omega} \psi_i \psi_j d\Omega = \alpha \delta_{ij}, \hspace{4mm}\forall \psi_i, \psi_j \in L^2\{\Omega\} ~\& ~\alpha \in \mathbb{R}
\end{equation}
where, $\delta_{ij}$ is a `Kronecker Delta' which means $\delta_{ij} = 1 $ if $i=j$ and $0$ otherwise.
The functions $\psi_i$ and $\psi_j$ are orthonormal if $\alpha = 1$.

\subsection{Nodal Basis Functions}
The `nodal' basis functions are the set of functions which are `Cardinal' in nature. What that means is, at the
interpolation point, the basis function will have value $1$ and for all other collocation points, the value of the
basis function will be zero. Thus, the interpolated value of the function agrees perfectly at the collocation points
but may have some error at other places.
The general symbol used for nodal basis functions is $\phi$.
The interpolation itself is given as,
\begin{equation}
	f^N(x) = \sum_{i=0}^{N-1} f(x_i) \phi_i(x), \hspace{4mm} x \in C^0[a,b]
\end{equation}
and, $\phi_i(x_j) = \delta_{ij}$.
We use Lagrange polynomials as the nodal basis functions. The polynomials in $1$D are give as,
\begin{equation}
	\phi_i(x) = \prod_{j=0 , j \ne i}^{N-1} \frac{(x-x_j)}{(x_i - x_j)}
\end{equation}
it is clear that at $x=x_i$, $\phi_i(x_i) = 1$.
One crucial point that needs to be taken care of is selection of the collocation points $\left[x_0, x_1, ..., x_{N-1}\right]$.
It is seen that the equi-spaced arrangement of collocation points gives poor interpolation for very high orders of
accuracies. Therefore we take these points as roots of orthonormal modal polynomials.
Popular choices for collocation points are Gauss-Legendre (LG) collocation points, Legendre-Gauss-Lobatto (LGL) points
or Chebyshev points. An important distinction between LG and LGL points is that, LGL points include the `end-points'
whereas LG points include only the internal points. For DG methods, this doesn't have a significant difference since
solving a Riemann problem at the face remains unchanged.

\subsection{Multidimensional Interpolation}
For number of dimensions greater than $1$, the interpolating polynomials are obtained depending on the type of the
element we wish to interpolate on. For hex elements, these are obtained using tensor product of $1$D polynomials.
i.e.
\begin{equation}
	\phi(x,y,z) = \phi_{1D}(x)  \phi_{1D}(y)  \phi_{1D}(z)
\end{equation}
Whereas, for other types of elements, special multidimensional polynomials are used. However, the concept of
modal and nodal basis functions remains essentially the same irrespective of the number of dimensions.


\section{Results}

The hyperparameters of the \acrshort{ESN} used in Figure \ref{fig:ESN1} and
Figure \ref{fig:ESN2} were randomly assigned and therefore not optimized. In
spite of this, preliminary predictions track reasonably well with grid demand.
The current iteration shows a potentially
misleading relationship between accuracy and training length. By inspection,
Figure \ref{fig:ESN2} is more accurate but the comparison is unfair because
the two ESNs are predicting different time periods.
We also conducted a single grid search for the optimal combination of
spectral radius ($\rho$) and noise injection (for regularization of reservoir
neurons), shown in Figure \ref{fig:gridsearch}, following the recommendations from
\cite{lukosevicius_practical_2012}.

\begin{figure}[h]
  \centering
  \includegraphics[width=\columnwidth]{scaled_esn_network2.png}
  \caption{A simple ESN with a prediction of 100 hours into the future after
  training on 1000 hours of historical data.}
  \label{fig:ESN1}
\end{figure}
\begin{figure}[h]
  \centering
  \includegraphics[width=\columnwidth]{scaled_esn_network.png}
  \caption{A simple ESN with a prediction of 100 hours into the future. After
  training on 3500 hours of historical data.}
  \label{fig:ESN2}
\end{figure}
\begin{figure}[h]
  \centering
  \includegraphics[width=0.8\columnwidth]{noise_spectral_radius.png}
  \caption{A grid search over a range of spectral radii and noise levels. The
  optimal set minimizes the mean squared error.}
  \label{fig:gridsearch}
\end{figure}