%!TEX root = ../notes.tex
\section{March 8, 2022}
\subsection{Legendre Symbol \emph{continued}}
\begin{example}\label{example:legendre-symbol}
    Determine if $219$ is a quadratic residue mod $383$ (we note that $383$ is a prime).
    \begin{align*}
        \lege{219}{383} & = \lege{3}{383}\cdot\lege{73}{383}    \\
        \intertext{We now flip the Legendre Symbols using quadratic reciprocity:}
                        & = -\lege{383}{3} \cdot \lege{383}{73} \\
                        & = -\lege{2}{3} \cdot \lege{18}{73}    \\
                        & = 1\cdot \lege{18}{73}                \\
                        & = \lege{2}{73}\cdot\lege{9}{73}       \\
                        & = \lege{2}{73} = \boxed{1}
    \end{align*}
\end{example}
\begin{remark*}
    We must factor the top argument before beginning to flip using quadratic reciprocity.
\end{remark*}

\subsection{Proof of Quadratic Reciprocity}

Recall \cref{thm:qr}:
\begin{theorem*}[Law of Quadratic Reciprocity]
    Let $p, q\in\ZZ_+$ be distinct odd positive primes. Then
    \begin{equation*}
        \lege{p}{q}\lege{q}{p} = (-1)^{\frac{p-1}{2}\frac{q-1}{2}}
    \end{equation*}
    In other words,
    \[\lege{p}{q} = \lege{q}{p}\]
    if and only if at least one of $p, q$ is congruent to $1$ mod $4$.
\end{theorem*}

\begin{proof}[Proof of Quadratic Reciprocity (\cref{thm:qr}), using Gauss's Lemma (\cref{lemma:gauss-lemma})]
    \textsc{wlog}, let
    \begin{align*}
        P & = \left\{1, 2, \dots, \frac{p-1}{2}\right\},\quad N=-P, \\
        Q & = \left\{1, 2, \dots, \frac{q-1}{2}\right\}
    \end{align*}
    We write $\tilde P, \tilde N$ for $P\pmod{p}$ and $N\pmod{p}$ respectively, so that Gauss's lemma gives
    \[\lege{q}{p} = (-1)^\mu, \quad\text{where }\mu = |q\tilde P\cap\tilde N|\]
    In other words, $\mu$ is exactly the number of $x\in P$ such that $qx\equiv n\pmod{p}$ for some $n\in N$, and hence the number of $x\in P$ such that for $y\in \ZZ$, \[-\frac{p}{2} < qx - py < 0.\]
    We now specify more precisely which $y$ can possibly satisfy this condition. Solving these inequalities for $y$ gives
    \begin{align*}
        \frac{qx}{p} < y < \frac{qx}{p}+\frac{1}{2}.
    \end{align*}
    \otoh, since $x\leq \frac{p-1}{2}$ $\forall x\in P$, this gives
    \begin{align*}
        y < \frac{qx}{p}+\frac{1}{2} & \leq \frac{q(p-1)}{2p}+\frac{1}{2} \\
                                     & < \frac{q+1}{2}.
    \end{align*}
    Thus $0 < y < \frac{q+1}{2}$, which means that
    \[y\in Q = \left\{1, 2, \dots, \frac{q-1}{2}\right\}.\]
    We've shown that $\mu$ is the number of points $(x, y)\in P\times Q$ such that
    \[\frac{p}{2} < qx - py < 0.\]

    Switching $p$ and $q$, we also have
    \[\lege{p}{q} = (-1)^\eta\]
    where $\eta$ is the number of pairs
    \[(y, x)\in Q\times P\]
    such that
    \[-\frac{q}{2} < py - qx < 0\]
    which is exactly the number of pairs
    \[(x, y)\in P\times Q\]
    satisfying
    \[0 < qx - py < \frac{q}{2}\]
    (reflecting the inequality over $0$).

    We note that
    \[\lege{p}{q}\lege{q}{p} = (-1)^\mu (-1)^\eta = (-1)^{\mu+\eta}\]
    so all that remains is counting $\mu$ and $\eta$. And we have that $\mu + \eta$ is the number of ordered pairs $(x, y)\in P\times Q$ such that either
    \[-\frac{p}{2} < qx - py < 0 \text{ or } 0 < qx - py < \frac{q}{2}\]
    Noting that $qx - py\neq 0$ since $x$ and $y$ are from $P$ and $Q$ respectively, hence we can reduce this to
    \[-\frac{p}{2} < qx - py < \frac{q}{2}.\]
    Graphically, we are looking at:
    \begin{center}
        \includegraphics[width=0.8\textwidth]{images/qr-diagram.png}
    \end{center}

    where $\mu + \eta$ is the number of lattice points in the shaded region.

    If $\alpha$ is the number of lattice points in $A$ and $\beta$ the number of lattice points in $B$. Then
    \[\mu + \eta = \frac{p-1}{2}\frac{q-1}{2} - (\alpha + \beta)\]
    We show that $\alpha = \beta$ so that $\alpha + \beta \equiv 0\pmod{2}$.

    Let $\rho$ be the rotation given by rotating the rectangle about its center leaves it invariant.
    \[\rho(x, y) = \left(\frac{p+1}{2}-x, \frac{q+1}{2} - y\right)\]
    Quick check that
    \[qx - py < \frac{-p}{2} \Leftrightarrow qx' - py' > \frac{q}{2}\]

    Since $\rho$ maps lattice points to lattice points, then $\alpha = \beta$ which concludes the proof with a little extra handiwork.
\end{proof}

\subsection{Jacobi Symbol}
The Jacobi symbol generalizes the Legendre symbol.
\begin{definition}[Jacobi Symbol]
    Let $b$ be an odd positive integer and let $a\in\ZZ$. Write
    $b = p_1p_2\cdots p_m$, where $p_i$ are (not necessarily distinct) primes. Then we write
    \[\lege{a}{b} = \lege{a}{p_1}\lege{a}{p_2}\cdots \lege{a}{p_m}\]
    is called the \ul{Jacobi symbol}.
\end{definition}
We note some basic properties that the Jacobi symbol is totally multiplicative (on top and bottom!):
\begin{align*}
    \lege{a_1a_2}{b} & = \lege{a_1}{b}\lege{a_2}{b} \\
    \lege{a}{b_1b_2} & = \lege{a}{b_1}\lege{a}{b_2}
\end{align*}
\begin{remark*}
    Note that they're multiplicative \emph{fixing} either top or bottom. That is, they don't multiply like fractions.
\end{remark*}

\textbf{Warning!} $\lege{a}{b} = 1$ does not imply that $a$ is a quadratic residue modulo $b$ (since we could have $-1$'s from the factorization cancel out).

However, $\lege{a}{b} = -1$ \emph{does} imply that $a$ is a non-residue modulo $b$. (it is a non-residue mod at least one of prime factors of $b$).

\begin{example}
    \[\lege{2}{15} = \lege{2}{3}\lege{2}{5} = (-1)(-1) = 1\]
    but $2$ is not a quadratic residue modulo $15$.
\end{example}
\begin{proposition}[5.2.2 of Text]\label{prop:5.2.2}
    We have the following properties about the Jacobi symbol:
    \begin{enumerate}[(a)]
        \item \[\lege{-1}{b} = (-1)^{\frac{b-1}{2}}\]
        \item \[\lege{2}{b} = (-1)^{\frac{b^2 - 1}{8}}\]
        \item If $a, b\in\ZZ_+$, then
              \[\lege{a}{b}\lege{b}{a} = (-1)^{\frac{a-1}{2}\frac{b-1}{2}}\]
    \end{enumerate}
\end{proposition}


%  -----------------------------------------------------------------------------
%  Author         : Bimalka Piyaruwan Thalagala
%  GitHub         : https://github.com/bimalka98
%  Date Created   : 01.09.2020
%  Last Modified  : 06.03.2020
%  -----------------------------------------------------------------------------

\documentclass[a4paper,11pt]{article}%,twocolumn
\input{settings/packages}
\input{settings/page}
\input{settings/macros}
\usepackage[ framed, numbered]{matlab-prettifier}%framed,%
\usepackage{listings}
\usepackage{physics}
\usepackage{pdfpages}
\usepackage[toc,page]{appendix}

\begin{document}
\input{content/title_page}

\pagebreak

\tableofcontents
\listoffigures
\vfill
\begin{center}
	\textbf{\textit{*PDF is clickable}}
\end{center}

\textit{\textbf{Note:}}\\
\textit{All the materials and executable {\tt MATLAB R2018a} Live Script related to the project can also be found at \url{https://github.com/bimalka98/Digital-Signal-Processing}}
\pagebreak

\section{Question 1 and Question 2}
Refer the Appendix for the Code. For the generation of the AWGN, MATLAB's {\tt normrnd($\mu$,$\sigma$,[1, samples])} function which returns random samples from a normal distribution with mean $\mu$ and standard deviation $\sigma$ was used.\\

\vspace{10cm}
\begin{center}
	\textit{this page was intentionally left blank}
\end{center}

\pagebreak

\section{Question 3}
Plot of the sequence of Received signal and observations of the impact of the variance of noise on it by varying $\sigma^2=1$.

\begin{figure}[!h]
	\centering
	\includegraphics[scale=0.45]{figures/q3f1}
	\caption{Sequence of the Received signal when $\sigma^2 = 1$}
\end{figure}

\begin{figure}[!h]
	\centering
	\includegraphics[scale=0.45]{figures/q3f2}
	\caption{Impact of the variance of noise on the Received signal}
\end{figure}

As illustrated in the above figure when the variance of the noise increases the range of the values the received sample can  be in increases (observe the change in the range of Y axis). This essentially increases the bit errors at the receiving end.

\pagebreak
\section{Question 4}
Sketching and comparing the sequence of Y(signal recovered through threshold) with the transmitted signal.
\begin{figure}[!h]
	\centering
	\includegraphics[scale=0.4]{figures/q4f1}
	\caption{Sequence of Y when the number of samples is 1000}
\end{figure}

Most of the transmitted bits have been identified correctly by the receiver after the thresholding(perfectly aligned curves). But some of the bits have been identified incorrectly after the thresholding. Change of signal levels due to the unwanted noise added during the process of transmission is the reason.

\section{Question 5}
Repeating the above steps for a sequence of length L = 100,000.
\begin{figure}[!h]
	\centering
	\includegraphics[scale=0.4]{figures/q5f1}
	\caption{Sequence of Y when the number of samples is 100,000}
\end{figure}

The same observations made regarding the previous figure is applicable to this scenario as well. But probability of error has a significant reduction when transmitting a huge number of bits.

\pagebreak
\subsection{Histogram of the received sequence taking	the no of bins as 10}
\begin{figure}[!h]
	\centering
	\includegraphics[scale=0.45]{figures/q5f2}
	\caption{Histogram of the received sequence using the user defined function: bins = 10}
\end{figure}

\begin{figure}[!h]
	\centering
	\includegraphics[scale=0.45]{figures/q5f3}
	\caption{Histogram of the received sequence using the MATLAB's built-in {\tt hist()} fucntion: bins = 10}
\end{figure}

\textbf{Comparison} : Histograms generated using the custom user defined function and the built-in function are almost the same. The reason for the difference in frequencies can be identified as the range differences of bins considered in the two algorithms. Code of the user defined fucntion can be found at the end of the Appendix.

\pagebreak
\subsection{Impact when the number of bins is changed from 10 to 100}

\begin{figure}[!h]
	\centering
	\includegraphics[scale=0.45]{figures/q5f4}
	\caption{Histogram of the received sequence using the user defined function: bins =100}
\end{figure}

\begin{figure}[!h]
	\centering
	\includegraphics[scale=0.45]{figures/q5f5}
	\caption{Histogram of the received sequence using the MATLAB's built-in {\tt hist()} fucntion: bins = 100}
\end{figure}
\textbf{Impact}: When the number of bins is changed from 10 to 100, shape of the normal distribution becomes clearly visible and differences between two histograms become negligible as the ranges of the bins become more similar.

\pagebreak
\subsection{Conditional PDFs when number of bins = 100 and A = 1}

Dependent axis represents the normalized frequencies calculated as follows. And therefore following figures illustrates the approximation for the required Probability Density Functions.
\[
Normalized ~Freq = \frac{Frequency}{Total~Number~of~Samples \times Width~of~a~Bin}
\]

\begin{figure}[!h]
	\centering
	\includegraphics[scale=0.45]{figures/q5f6}
	\caption{$f_{R|S}(r|S=A)$ when A = 1: Mean $\approx$ 1}
\end{figure}

\begin{figure}[!h]
	\centering
	\includegraphics[scale=0.45]{figures/q5f7}
	\caption{$f_{R|S}(r|S= -A)$ when A =1: Mean $\approx$ -1}
\end{figure}

Observe that when the distribution is conditioned on S, the mean of the distribution shifts along the amplitude axis towards the S.

\pagebreak
\subsection{Impact of A on the Conditional PDFs}

As illustrated in the following figures, when the distribution is conditioned on S =A , the mean of the distribution shifts along the amplitude axis towards the S = A.  But the variance of the distribution remains the same as it is only affected by the AWGN.

\begin{figure}[!h]
	\centering
	\includegraphics[scale=0.45]{figures/q5f8}
	\caption{Impact of A on the Conditional PDFs}
\end{figure}

\subsection{Expected Values of the distributions when A = 1}

The expected value of a continuous Random variable $X$ which has a PDF of $f_X(x)$ is given as,
\[
E[X] = \int_{-\infty}^{+\infty} x.f_X(x) ~dx
\]
The same formula can be redefined for discrete case as follows where $\Delta x$ represents the width of a bin which is equal in our cases.
\[
E[X] = \sum_{i = 1}^{N} x_i.P(x_i).\Delta x
\]

Therefore the requires expected values can be easily found using the {\tt Expected(x, px, bin\_width)} function defined in the appendix. The required parameters of that function can be found using the {\tt myHistogram(valueArray,numberOfBins, Normalized)} function. \\

\begin{tabular}[!h]{l c r}
	Expected value $E[R]$& = & -0.00073493\\
Expected value $E[R|S=A]$ &=& 1.0004\\
Expected value $E[R|S=-A]$& =& -1.0011\\

\end{tabular}

\pagebreak
\subsection{Marginal PDF of R $f_R(r)$}
Dependent axis represents the normalized frequencies calculated as follows as mentioned previously. Therefore, the following figure illustrates the approximation for the required Marginal Probability Density Functions of the continuous Random Variable R.
\[
Normalized ~Freq = \frac{Frequency}{Total~Number~of~Samples \times Width~of~a~Bin}
\]

Expected value of the Random Variable  ($E[R]$ = -0.00073493) reaches zero as the probabilities Pr(D = 0) = Pr(D = 1) = 1/2 are equal.

\begin{figure}[!h]
	\centering
	\includegraphics[scale=0.45]{figures/q5f9}
	\caption{Marginal PDF of R $f_R(r)$}
\end{figure}

\pagebreak
\section{Question 6: Effect of Interference}

Interference does nothing other than increasing the variance of the  distribution. When comparing the following figures with the figures of the previous section it is clearly visible. As the variance increases, to keep the area under the graph  a constant, the peak of the graph lowers. Therefore following two observations can be made when additional interference is added to the transmitted signal.
\begin{itemize}
	\item Variance of the distribution increases. Which eventually increases the probability of error.
	\item Peak value of the distribution decreases to keep the area under the curve constant.
\end{itemize}

These observations are common to all the figures and subsections of this section.

\subsection{Conditional PDFs when number of bins = 100, A = 1 and under additional interference}

\begin{figure}[!h]
	\centering
	\includegraphics[scale=0.38]{figures/q6f1}
	\caption{$f_{R|S}(r|S=A)$ when interference is added}
\end{figure}

\begin{figure}[!h]
	\centering
	\includegraphics[scale=0.38]{figures/q6f2}
	\caption{$f_{R|S}(r|S= -A)$  when interference is added}
\end{figure}

\pagebreak
\subsection{Impact of A on the Conditional PDFs under additional interference}
\begin{figure}[!h]
	\centering
	\includegraphics[scale=0.45]{figures/q6f3}
	\caption{Impact of A on the Conditional PDFs under additional interference}
\end{figure}

\subsection{Expected Values of the distributions and under additional interference}

As previously described interference affects only to the variance of the distribution. Therefore the Expected Values remains almost the same.\\

\begin{tabular}[!h]{l c r}
	Expected value $E[R]$& = & -0.0012806\\
	Expected value $E[R|S=A]$ &=& 0.99798\\
	Expected value $E[R|S=-A]$& =& -0.99949\\

\end{tabular}

\subsection{Marginal PDF of R $f_R(r)$ and under additional interference}
\begin{figure}[!h]
	\centering
	\includegraphics[scale=0.38]{figures/q6f4}
	\caption{Marginal PDF of R $f_R(r)$ under additional interference}
\end{figure}

\section{Question 7: Effect of Scaling when the number of bins = 100 and A = 1 and $\sigma^2 = 1$}

Effect of scaling of the pulse sequence(S), on the conditional probability distributions has the same effect as changing the signal level `A'. Therefore the figures and the subsections will be the same as that of Question 5.\\

Therefore only the behavior of Marginal distribution of R(received signal) is illustrated here. As it was not considered in earlier sections.

\begin{figure}[!h]
	\centering
	\includegraphics[scale=0.45]{figures/q7f1}
	\caption{Effect of Scaling on the Marginal PDF of R $f_R(r)$}
\end{figure}

The first sub-figure of the above figure shows the instance without any scaling. when the scale is increased, the distribution tends to divide in to two separate peaks centered around $+\alpha$ and $-\alpha$ as expected due to the lower variance than the scaling factor.

\begin{appendices}
	\section{Matlab Code for the Simulation}
	\includepdf[pages=-]{code/code.pdf}
\end{appendices}

%---------------------------------------------------------------------------
\end{document}
-


\subsection{Summary of Curve Sketching}\label{subsec:SummaryCurveSketching}

The following is a guideline for sketching a curve $y=f(x)$ by hand. Each item may not be relevant to the function in question, but utilizing this guideline will provide all information needed to make a detailed sketch of the function.

\begin{formulabox}[Guideline for Curve Sketching]
\begin{enumerate}
	\item	Domain of the function
	\item	$x$- and $y$-Intercepts
	\item	Symmetry
	\item	Vertical and Horizontal Asymptotes
	\item	Intervals of Increase/Decrease, and Local Extrema
	\item	Concavity and Points of Inflection
	\item	Sketch the Graph
\end{enumerate}
\end{formulabox}

\begin{example}{Graph Sketching}{graphsketching}
Sketch the graph of $y=f(x)$ where $\ds{f(x)=\frac{2x^2}{x^2-1}}$
\end{example}

\begin{solution}
\begin{enumerate}
\item  The domain is $\{x: x^2-1 \neq 0\}= \{x: x \neq \pm 1\}=(- \infty,-1) \cup (-1,1) \cup (1, \infty)$
\item  There is an $x$-intercept at $x=0$. The $y$ intercept is $y=0$.
\item  $f(-x)=f(x)$, so $f$ is an even function (symmetric about $y$-axis)
\item  $\ds{ \lim_{x \to \pm \infty} \frac{2x^2}{x^2-1} =\lim_{x \to \pm \infty} \frac{2}{1-1/x^2} = 2 }$, so $y=2$ is a horizontal asymptote.

Now the denominator is 0 at $x =\pm1$, so we compute:
\[\lim_{x \to 1^+} \frac{2x^2}{x^2-1} = + \infty, \;   \lim_{x \to 1^-} \frac{2x^2}{x^2-1} = - \infty, \;
\lim_{x \to -1^+} \frac{2x^2}{x^2-1} = - \infty, \;   \lim_{x \to -1^-} \frac{2x^2}{x^2-1} = + \infty.  \]
So the lines $x=1$ and $x=-1$ are vertical asymptotes.

\item  For critical values we take the derivative:
$$f'(x) = \frac{4x(x^2-1)-2x^2 \cdot 2x}{(x^2-1)^2} = \frac{-4x}{(x^2-1)^2}.$$
Note that $f'(x)=0$ when $x=0$ (the top is zero).
Also, $f'(x)=DNE$ when $x=\pm 1$ (the bottom is zero).
As $x=\pm 1$ is \ifont{not} in the domain of $f(x)$, the only critical number is $x=0$ (recall that to be a critical number we need it to be in the domain of the original function).

Drawing a number line and including \ifont{all} of the split points of $f'(x)$ we have:
$$\includegraphics[width=3.5in]{images/graphex1}$$
Thus $f$ is increasing on $(-\infty, -1)\cup(-1,0)$ and decreasing on $(0,1)\cup(1, \infty)$.

By the first derivative test, $x=0$ is a local max.

\item  For possible inflection points we take the second derivative:
$$f''(x)= \frac{12x^2+4}{(x^2-1)^3}$$
The top is never zero.
Also, the bottom is only zero when $x=\pm 1$ (neither of which are in the domain of $f(x)$).
Thus, there are no possible inflection points to consider.

Drawing a number line and including \ifont{all} of the split points of $f''(x)$ we have:
$$\includegraphics[width=2.25in]{images/graphex2}$$
Hence $f$ is concave up on $(- \infty,-1)\cup(1, \infty)$, concave down on $(-1,1)$.

\item  We put this information together and sketch the graph.

We combine some of this information on a single number line to see what \ifont{shape} the graph has on certain intervals:
$$\includegraphics[width=3.0in]{images/graphex3}$$
Note that there is a horizontal asymptote at $y=2$ and that the curve has $x$-int of $x=0$ and $y$-int of $y=0$.
Therefore, a sketch of $f(x)$ is as follows:
$$\includegraphics[width=3.5in]{images/graphex4}$$
\end{enumerate}
\end{solution}


\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}