\section{Finite Markov Decision Processes}

\subsection{Exercise 3.1}
\subsubsection*{Q}
Devise three example tasks of your own that fit into the MDP framework, identifying for each its states, actions, and rewards. Make the three examples as \emph{different} from each other as possible. The framework is abstract and flexible and can be applied in many different ways. Stretch its limits in some way in at least one of your examples.

\subsubsection*{A}
\begin{enumerate}
    \item Simple example is a robot that hoovers a room. The state can be how much dust there is on the ground and where the robot is (including it's orientation). Actions can be to move and hoover. The reward can be the amount by which it reduces dust in the room on that action. This is Markov because all that is important from the previous state to the future is where the dust is left (maybe also how much).
    \item Outlandish example is a football coach. Actions are playing strategies. Rewards are goals. State is current score, team fitness, etc..
    \item financial trader. State is their current holdings on an asset. Reward is money from a trade. Actions are buy/sell. Maybe not markov because the environment may change predictably based on information from multiple steps ago.
\end{enumerate}

\subsection{Exercise 3.2}
\subsubsection*{Q}
Is the MDP framework adequate to usefully represent \emph{all} goal-directed learning tasks? Can you think of any clear exceptions?

\subsubsection*{A}
The main thing about the MDP is that Markov property. There are tasks where this does not hold. For instance, in Poker, the previous states will determine what is in the deck and what is not. This does not obey Markov property.

\subsection{Exercise 3.3}
\subsubsection*{Q}
Consider the problem of driving. You could define the actions in terms of the accelerator, steering wheel, and brake, that is, where your body meets the machine. Or you could define them farther out—say, where the rubber meets the road, considering your actions to be tire torques. Or you could define them farther in—say, where your brain meets your body, the actions being muscle twitches to control your limbs. Or you could go to a really high level and say that your actions are your choices of \emph{where} to drive. What is the right level, the right place to draw the line between agent and environment? On what basis is one location of the line to be preferred over another? Is there any fundamental reason for preferring one location over another, or is it a free choice?

\subsubsection*{A}

\begin{itemize}
    \item The natural distinction depends on the task. If the task is to go from one location to another, the actions might be in terms of directing the car and altering its speed.
    \item This also depends on what we consider to be the decision making part here. If we consider the decisions to be made by the brain of a human driving, then their physical body will form part of the environment -- if they break their leg then it will effect their goal.
    \item Actions have to be things that the agent can actually control. Take the example of an autonomous vehicle. One might consider that the agent has complete control over the car's break, accelerator and steering wheel. These operations would form the actions for the agent.
\end{itemize}

\subsection{Exercise 3.4}
\subsubsection*{Q}
Give a table analogous to the one in Example 3.3, but for $p(s', r| s, a)$. It should have columns $s, a, s', r$, and a row for every 4-tuple for which $p(s', r | s, a) > 0$.

\subsubsection*{A}
\begin{table}[h!]
\centering
\begin{tabular}{ll|ll|c}
    $s$ & $a$ & $s'$ & $r$ & $p(s', r | s, a)$ \\
    \hline
     \texttt{high}& \texttt{search}&  \texttt{high}&  $r_{\texttt{search}}$& $\alpha$  \\
     \texttt{high}& \texttt{search}&  \texttt{low}&   $r_{\texttt{search}}$& $1 - \alpha$ \\
     \texttt{high}& \texttt{wait}&  \texttt{high}&   $r_{\texttt{wait}}$&  $1$ \\
     \texttt{low}& \texttt{recharge}&   \texttt{high}&    $0$&   $1$\\
     \texttt{low}& \texttt{search}&  \texttt{high}&  $-3$& $1 - \beta$  \\
     \texttt{low}& \texttt{search}&  \texttt{low}&   $r_{\texttt{search}}$& $\beta $\\
     \texttt{low}& \texttt{wait}&  \texttt{low}&   $r_{\texttt{wait}}$&  $1$
\end{tabular}

\caption{Transition table}
\label{table:3.4}
\end{table}

\subsection{Exercise 3.5}
\subsubsection*{Q}
The equations in Section 3.1 are for the continuing case and need to be modified (very slightly) to apply to episodic tasks. Show that you know the modifications needed by giving the modified version of (3.3)

\subsubsection*{A}
\begin{equation}
    \sum_{s' \in \mathcal{S}^{+}} \sum_{r \in \mathcal{R}} p(s', r | s, a) = 1
\end{equation}

\subsection{Exercise 3.6}
\subsubsection*{Q}
Suppose you treated pole-balancing as an episodic task but also used discounting, with all rewards zero except for $-1$ upon failure. What then would the return be at each time? How does this return differ from that in the discounted, continuing formulation of this task?

\subsubsection*{A}
Note that the pole will fall eventually with probability 1
\begin{equation}
    G_t = - \gamma^{T - t}.
\end{equation}
Whereas in the continuing case the value is
\begin{equation}
    G_t = - \sum_{k \in \mathcal{K}} \gamma^{k - t},
\end{equation}
where $\mathcal{K}$ is the set of times after $t$ at which the pole falls over.

\subsection{Exercise 3.7}
\subsubsection*{Q}
Imagine that you are designing a robot to run a maze. You decide to give it a reward of $+1$ for escaping from the maze and a reward of zero at all other times. The task seems to break down naturally into episodes--the successive runs through the maze--so you decide to treat it as an episodic task, where the goal is to maximize expected total reward (3.7). After running the learning agent for a while, you find that it is showing no improvement in escaping from the maze. What is going wrong? Have you effectively communicated to the agent what you want it to achieve?

\subsubsection*{A}
If the agent keeps going randomly, it will reach the end of the maze with probability 1, so the value of $G$ under most strategies is $1$. What you actually want is for the thing to leave the maze as quickly as possible.\\

Note also that there are some instances in which the agent might just get stuck in a loop. You would have to impose another rule to put the agent into a terminal state here.

\subsection{Exercise 3.8}
\subsubsection*{Q}
Suppose $\gamma = 0.5$ and the following sequence of rewards is received $R_1=-1$, $R_2 =2$,$R_3 =6$,$R_4 =3$, and $R_5 =2$, with $T =5$. What are $G_0$, $G_1$, $\dots$, $G_5$? Hint: Work backwards.

\subsubsection*{A}
$G_0 = 2$, $G_1 = 3$, $G_2 = 2$, $G_3 = \frac12$, $G_4 = \frac18$, $G_5=0$.

\subsection{Exercise 3.9}
\subsubsection*{Q}
Suppose $\gamma = 0.9$ and the reward sequence is $R_1 = 2$ followed by an infinite sequence of $7$s. What are $G_1$ and $G_0$?
\subsubsection*{A}
\begin{align}
    G_1 &= 7 \frac{\gamma}{1 - \gamma} \\
    G_0 &= 2 + 7 \frac{\gamma}{1 - \gamma}
\end{align}

\subsection{Exercise 3.10}
\subsubsection*{Q}
Prove (3.10).

\subsubsection*{A}
Take $r \in \mathbb{C}$ with $|r| < 1$, then
\begin{align*}
    S_N &\doteq \sum_{i=0}^N r^i \\
    rS_N - S_N &= r^{N+1} - 1 \\
    S_N &= \frac{1 - r^{N+1}}{1 - r} \\
    S &\doteq \lim_{N \to \infty} S_N = \frac{1}{1 - r}
\end{align*}

\subsection{Exercise 3.11}
\subsubsection*{Q}
If the current state is $S_t$, and actions are selected according to stochastic policy $\pi$, then what is the expectation of $R_{t+1}$ in terms of $\pi$ and the four-argument function $p$ (3.2)?

\subsubsection*{A}
\begin{equation}
    \Epi{} [R_{t+1} | S_t = s] = \sum_{a} \pi(a | s) \sum_{s', r} r p(s', r | s, a)
\end{equation}

\subsection{Exercise 3.12}
\subsubsection*{Q}
The Bellman equation (3.14) must hold for each state for the value function $v_\pi$ shown in Figure 3.2 (right) of Example 3.5. Show numerically that this equation holds for the center state, valued at $+0.7$, with respect to its four neighbouring states, valued at $+2.3$, $+0.4$, $-0.4$, and $+0.7$. (These numbers are accurate only to one decimal place.)

\subsubsection*{A}
\begin{align*}
    v_\pi(\mathtt{center}) &\doteq \sum_a \pi(a | s) \sum_{s', r} p(s', r | s, a)[r + \gamma v_\pi(s')] \\
    & = \frac14 \times 0.9 \times \sum_{s'} v_\pi(s')\\
    & = \frac14 \times 0.9 \times 3.0 \\
    & = 0.675
\end{align*}

\subsection{Exercise 3.13}
\subsubsection*{Q}
What is the Bellman equation for action values, that is, for $q_\pi$? It must give the action value $q_\pi(s, a)$ in terms of the action values, $q_\pi(s',a')$, of possible successors to the state–action pair $(s,a)$. Hint: the backup diagram to the right corresponds to this equation. Show the sequence of equations analogous to (3.14), but for action values.

\subsubsection*{A}
\begin{align*}
    q_\pi &\doteq \Epi[G_t | S_t=s, A_t=a]\\
          &= \Epi[R_{t+1}| S_t=s, A_t=a] + \gamma \Epi[G_{t+1} |S_t = s, A_t = a] \\
          &= \sum_{s', r} p(s', r|s, a)r + \gamma \sum_{s', r} p(s', r|s, a) \sum_{a'}\pi(a'|s')\Epi[G_{t+1}| S_{t+1}=s', A_{t+1}=a'] \\
          &= \sum_{s', r} p(s', r| s, a) [r + \gamma \sum_{a'}\pi(a'|s')q_\pi(s', a')]
\end{align*}

\subsection{Exercise 3.14}
\subsubsection*{Q}
In the gridworld example, rewards are positive for goals, negative for running into the edge of the world, and zero the rest of the time. Are the signs of these rewards important, or only the intervals between them? Prove, using (3.8), that adding a constant $c$ to all the rewards adds a constant, $v_c$, to the values of all states, and thus does not affect the relative values of any states under any policies. What is $v_c$ in terms of $c$ and $\gamma$?

\subsubsection*{A}
We choose actions by relative (additive) values. Add $c$ to all states and
\begin{equation}
    G_t \mapsto G_t + \frac{c}{1 - \gamma}
\end{equation}
so relative values unchanged.\\

$v_c = \frac{c}{1 - \gamma}$

\subsection{Exercise 3.15}
\subsubsection*{Q}
Now consider adding a constant $c$ to all the rewards in an episodic task, such as maze running. Would this have any effect, or would it leave the task unchanged as in the continuing task above? Why or why not? Give an example.

\subsubsection*{A}
Let terminal time be $T$. In this case we have
\begin{equation}
    G_t \mapsto G_t + c\frac{1 - \gamma^T}{1 - \gamma},
\end{equation}
so if the agent can procrastinate termination, then all else being equal it will increase $v_\pi$.\\

Suppose that we have an episodic task with one state $S$ and two actions $A_0, A_1$. $A_0$ takes agent to terminal state with reward $1$, while $A_1$ takes agent back to $S$ with reward $0$. In this case the agent should terminate to maximise reward. \\

If we add $1$ to each reward then the return for doing $A_1$ forever is $\frac{1}{1-\gamma}$. Which can be bigger than $2$ if we choose a discount factor smaller than $\frac12$.

\subsection{Exercise 3.16}
\subsubsection*{Q}
\includegraphics[width=\textwidth]{\ProjectDir/data/exercise_questions/ex_3_16_question.png}

\subsubsection*{A}
\begin{align*}
    v_\pi(s) & = \Epi[q_\pi(S_t, A_t) |S_t = s, A_t = a] \\
          & = \sum_a \pi(a|s) q_\pi(s, a)
\end{align*}

\subsection{Exercise 3.17}
\subsubsection*{Q}
\includegraphics[width=\textwidth]{\ProjectDir/data/exercise_questions/ex_3_17_question.png}

\subsubsection*{A}
\begin{align*}
    q_\pi(s, a) & = \Epi[R_{t+1} + v_\pi(s') |S_t = s, A_t = a] \\
          & = \sum_{s', r} p(s', r|s, a) [r + v_\pi(s')]
\end{align*}

\subsection{Exercise 3.18}
\subsubsection*{Q}
Draw or describe the optimal state-value function for the golf example.

\subsubsection*{A}
(Using the pictures.) Optimal state value gives values according to \texttt{driver} when off the green, then according to \texttt{putter} on the green.\\

Optimal policy is to use \texttt{driver} when off green and \texttt{putter} when on green.

\subsection{Exercise 3.19}
\subsubsection*{Q}
 Draw or describe the contours of the optimal action-value function for putting, $q_*(s, \mathtt{putter})$, for the golf example.

\subsubsection*{A}
Should be the same as the value for the policy that always uses the \texttt{putter}.

\includegraphics[width=\textwidth]{\ProjectDir/data/exercise_output/ex_3_19/ex_3_19_answer.png}

\subsection{Exercise 3.20}
\subsubsection*{Q}
\includegraphics[width=\textwidth]{\ProjectDir/data/exercise_questions/ex_3_20_question.png}

\subsubsection*{A}
When $\gamma=0$, $v_{\pi_{\mathtt{left}}}$ is optimal. When $\gamma = 0.5$, they are both optimal. When $\gamma=0.9$, $v_{\pi_{\mathtt{right}}}$ is optimal.

\subsection{Exercise 3.21}
\subsubsection*{Q}
Give the Bellman equation for $q_*$ for the recycling robot.

\subsubsection*{A}
This is just writing out the equation below, filling in the values given in the robot example.
\begin{equation}
    q_*(s, a) = \sum_{s', r} p(s', r | s, a)[r + \gamma \max_{a'} q_*(s', a')]
\end{equation}

\subsection{Exercise 3.22}
\subsubsection*{Q}
Figure 3.5 gives the optimal value of the best state of the gridworld as $24.4$, to one decimal place. Use your knowledge of the optimal policy and (3.8) to express this value symbolically, and then to compute it to three decimal places.

\subsubsection*{A}
All actions tak the agent to $A'$ with reward $10$. We can see that $\gamma = 16.0 / 17.8 = 0.9$. This means that
\begin{equation}
    v = 10 + 16 \times 0.9 = 24.4.
\end{equation}
This is using the following framework
\begin{equation}
    v_*(s) = \max_{a}\sum_{s', r} p (s', r| s, a)[r + \gamma v_*(s')].
\end{equation}

\subsection{Exercise 3.23}
\subsubsection*{Q}
Give an equation for $v_*$ in terms of $q_*$

\subsubsection*{A}
\begin{equation}
    v_*(s) = \sum_a \pi^*(a | s) q_*(s, a)
\end{equation}

\subsection{Exercise 3.24}
\subsubsection*{Q}
Give an equation for $q_*$ in terms of $v_*$ and the world's dynamics $p(s', r| s, a)$.

\subsubsection*{A}
\begin{align}
    q_* (s, a) &= \mathbb{E} [ R_{t+1} + \gamma v_*(S_{t+1}) | S_t = s, A_t = a] \\
    &= \sum_{s', r} (r + \gamma v_*(s'))p(s', r | s, a)
\end{align}

\subsection{Exercise 3.25}
\subsubsection*{Q}
Give an equation for $\pi_*$ in terms of $q_*$.

\subsubsection*{A}
This is just any policy that acts greedily w.r.t. the optimal action-value function.
\begin{equation}
    \pi_* (a|s) = \frac{\mathds{1}\{a = \argmax_{a'} q_*(a', s)\}}{\sum_{a} \mathds{1}\{a = \argmax_{a'}q_*(a', s)\}}
\end{equation}

\subsection{Exercise 3.26}
\subsubsection*{Q}
Give an equation for $\pi_*$ in terms of $v_*$ and the world's dynamics $p(s', r| s, a)$.

\subsubsection*{A}
This is just the answer to 3.25 with the answer to 3.24 substituted in for $q_*$.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% UMB-CS114-2015F: Introduction to Programming in Java
% Copyright 2015 Pejman Ghorbanzade <pejman@ghorbanzade.com>
% Creative Commons Attribution-ShareAlike 4.0 International License
% More info: https://github.com/ghorbanzade/UMB-CS114-2015F
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\def \topDirectory {.}
\def \texDirectory {\topDirectory/src/main/tex}

\documentclass[12pt,letterpaper,twoside]{article}
\usepackage{\texDirectory/template/style/directives}
\usepackage{\texDirectory/template/style/assignment}
\input{\texDirectory/template/config}

\usepackage[school]{\texDirectory/template/style/pgf-umlcd/pgf-umlcd}

\begin{document}

\doc{title}{Final Practice Exam}
\doc{points}{20}

\prepare{header}

\section*{Question 1}

The following code snippets do not compile.
There are two distinct compilation problems in each code snippet.
You are expected to find and fix all errors so that given command-line argument will lead to expected output as indicated.

\begin{enumerate}[label=\textbf{(\alph*)}]

\item Execution Script \hfill Expected Output\\
\texttt{java HelloWorld Beacons} \hfill \texttt{Hello Beacons!}

\begin{lstlisting}
public class HelloWorld {
	public static void main(String args) {
		System.out.println("Hello" + args[0] "!");
	}
}
\end{lstlisting}

\item Execution Script \hfill Expected Output\\
\texttt{java Temperature 86} \hfill \texttt{30.00}

\begin{lstlisting}
public class Temperature {
	public static void main(String[] args) {
		double tempF = args[0];
		double tempC = 5 * (tempF - 32) / 9;
		System.out.printf("%.2d\n", tempC);
	}
}
\end{lstlisting}

\newpage

\item Execution Script \hfill Expected Output\\
\texttt{java Array 3} \hfill \texttt{6}

\begin{lstlisting}
public class Array {
	public static void main(String[] args) {
		sum = 0;
		int num = Integer.parseInt(args[0]);
		int[] array = new int[num];
		for (int i = 1; i <= num; i++) {
			array[i] = i;
			sum += i;
		}
		System.out.println(sum);
	}
}
\end{lstlisting}

\item Execution Script \hfill Expected Output\\
\texttt{java Random 5} \hfill integer $x$ where $0 \leq x < 5$

\begin{lstlisting}
public class Random {
	public static void main(String[] args) {
		int num = args[0];
		int random = getRandomNumber(num);
		System.out.println(random);
	}
	public static int getRandomNumber(num) {
		return (int) (Math.random() * num);
	}
}
\end{lstlisting}

\newpage

\item Execution Script \hfill Expected Output\\
\texttt{java CircleDemo 1} \hfill 3.14

\begin{lstlisting}
public class CircleDemo {
	public static void main(String[] args) {
		Circle circle = Circle(Double.parseDouble(args[0]));
		System.out.printf("%.2f\n", circle.getArea());
	}
}
public class Circle {
	private double radius;
	public Circle(double radius) {
		this.radius = radius;
	}
	public double getArea() {
		double area = Math.PI * Math.pow(this.radius, 2);
	}
}
\end{lstlisting}

\end{enumerate}

\section*{Question 2}

The following code snippet compiles and runs without error.

Determine the output of the following program when user gives number five as command line argument.
Formulate the output based on any integer number given as command line argument.
Support your answer by explaining how the program works.

\lstinputlisting[firstline=12]{\topDirectory/src/main/java/f01/Math.java}

\section*{Question 3}

A student has developed the following code snippet for his CS114 assignment.
The program asks for a number $n$ and prints a triangle in $n$ number of lines
using star characters as shown below.

\lstinputlisting[firstline=12]{\topDirectory/src/main/java/f01/Triangle1.java}

\begin{terminal}
$ javac Triangle1.java
$ java Triangle1
Enter number of rows: 4
*
**
***
****
\end{terminal}

Just before submitting, the student realized that the printed triangle was supposed to be right-aligned.
Since he is running out of time, he has asked for your help to fix the problem.
Rewrite the program such to produce the output as shown below.

\begin{terminal}
$ javac Triangle2.java
$ java Triangle2
Enter number of rows: 4
   *
  **
 ***
****
\end{terminal}

\section*{Question 4}

Write a static method with the following method signature that takes an integer $n$ and returns a two-dimensional array of size $n \times n$ whose elements are randomely generated integer numbers in range $[0, 10)$.

\begin{terminal}
public static int[][] generateMatrix(int size);
\end{terminal}

\section*{Question 5}

A group of students are making an object-oriented java program that can record grades of multiple students (no more than 10) and display their averages.
The team has agreed that their program will have two classes; \texttt{Grader.java} that contains the main method and \texttt{Student.java} from which student objects are instantiated.
So far the students wrote a sample \texttt{Grader.java}, shown below, to demonstrate how student objects should be used.

\lstinputlisting[firstline=12]{\topDirectory/src/main/java/f01/Grader.java}

They are now trying to develop the Student class and are asking for your help.

\begin{enumerate}
	\item Write a UML diagram for the \texttt{Student} class that can be used with \texttt{Grader.java} file.
	\item Develop the \texttt{Student.java} file that implements your UML diagram.
\end{enumerate}

\newpage

\section*{Question 6}

A software developer was assigned to develop an Employee class for a simple accounting software for a small-business company.
Employees in this company are either interns or full time and they are all paid on weekly basis.
Interns are paid \$20 per hour and full-time employees are paid \$2000 per week regardless of how many hours they work.

In a development meeting, he proposed the UML diagram shown in Figure \ref{fig1} for the Employee class.
Some developers, however, argued that this design is prone to major security threats and needs minor modification.

Briefly explain what you think is the issue with this design.
Rewrite the UML diagram to fix the issue.

\begin{figure}[H]
	\centering
	\begin{tikzpicture}
		\begin{class}[text width=9cm]{Employee}{0, 0}
			\attribute{+ name: String}
			\attribute{+ numHours: num}
			\attribute{+ isIntern: boolean}
			\operation{+ Employee(name: String, isIntern: boolean)}
			\operation{+ work(numHours: int): void}
			\operation{+ getSalary(): int}
		\end{class}
	\end{tikzpicture}
	\caption{Proposed UML Diagram for Employee class}\label{fig1}
\end{figure}

\end{document}


\newcommand{\of}{\texttt{.o}~}

\lab{Interfacing With Other Programming Languages Using Cython}{Interfacing With Other Programming Languages Using Cython}
\label{lab:cythonwrap}

\objective{Learn to interface with object files using Cython. This lab should be worked through on a machine that has already been configured to build Cython extensions using gcc or MinGW.}

There are several ways to interface with other languages from Python.
Calling code written in C or Fortran is often done for performance reasons.
There is also an easy built in interface for R, which is a statistical computing language.
Writing an interface for functions or objects that have been written in one language so that they are accessible in another language is called \emph{wrapping}.  For example, many of the linear algebra functions in NumPy wrap the corresponding function in LAPACK or BLAS.
This allows us to gain the speed of these very optimized libraries but use them in a nice, Pythonic way.

In this lab we will focus primarily on how Python can interact with C, C++, and Fortran.
Python can interface with these languages in a variety of ways.
There is a built-in library, ctypes, that allows you to interface with object files compiled from other languages.
There are also many other methods for interfacing with compiled languages like C, C++, and Fortran.
A particularly convenient way to do this is with Cython.
Since Cython already compiles as a C file, we can essentially use it to call functions from a C program.
This is done through the use of object files.

% Describe .o files and dll's
% This is mainly just to help them know what is going on.
\section*{Object Files and DLL's}
For C, C++, and Fortran, the desired functions or classes are compiled into an object file containing instructions in assembly that can be used by compilers of a variety of languages.
Object files typically use the \of extension.
An object file that can be used and modified by multiple programs is called a \emph{shared object} and generally use the \texttt{.so} extension.
When C code has been compiled with the proper headers, etc. to interface with Python, it uses the \texttt{.pyd} extension on Windows and the \texttt{.so} extension on Unix-based operating systems.
The two extensions are essentially the same, except that Python object files are distinguished from shared object files on Windows.
When you compile anything that uses functions from object files, you must tell your compiler to link to the contents of the object file.
Otherwise it will not be able to find any of the needed functions.
The part of the compiler that forms these links between the code is called the \emph{linker}.
If you do not give your compiler proper instructions for linking, it will raise a linking error.
The compiler must also be able to find the necessary header files for compiling the source code.
This can be done by including the proper directories in the system path or by passing the paths to the proper directories as arguments to the compiler at compile time.

Object files can be statically or dynamically linked.
When statically linked, the entire object file becomes part of the final program.
On the other hand, dynamic linking is just a reference to the object file.
The operating system will load any needed dynamically linked libraries at runtime.
Dynamically linked programs depend on the presence of the appropriate object files on the target system.
Since statically linked libraries are part of the program itself, there is no need to load any libraries at runtime.

These instructions will use the GNU C Compiler (gcc) to demonstrate how Python can interface with other languages.
Other compilers can be used, but some configuration may be necessary..
Python's distutils package is designed to avoid some of the difficulty that comes when interfacing with different compilers, and we will show some basic examples of how it is used here.
The best way to learn to interface with other languages is to work through simple examples.
\begin{info}
The examples in this lab are intended to provide simple templates that can be used later to wrap C and Fortran functions.
It will be good to read through and think about how each file works, but it will not be critical to understand every detail at this point.
\end{info}

\begin{warn}
When passing arrays as pointers to C, C++, and Fortran functions, be \emph{absolutely sure} that the array being passed is contiguous.
This means that the entries of the array are stored in \emph{adjacent} entries in memory.
Passing one of these functions a strided array will result in out of bounds memory accesses and could crash your computer.
When working with two dimensional arrays, C and C++ expect rows to be stored in contiguous blocks of memory.
Fortran expects columns to be stored in contiguous blocks of memory.

To avoid out of bounds memory accesses, be sure to pass in the size of the array as a separate parameter.
\end{warn}

\section*{Wrapping a C Function}
The following C function computes the solution to a tridiagonal system.
It requires pointers to four arrays.
Arrays \li{a} and \li{c} have length \li{n-1} and represent the first subdiagonal and first superdiagonal of a banded matrix with bandwidth 3.
Arrays \li{b} and \li{x} have length \li{n} and represent the main diagonal of the banded matrix and the right hand side of the system of equations.
\li{c} and \li{x} are modified in place to compute the solution of the system.
\li{c} is used to store temporary values and \li{x} is transformed into the solution of the system.

\lstinputlisting[style=fromfile, language=C, name=ctridiag.c]{./ctridiag/ctridiag.c}

This file can be compiled to a \li{.o} file so that \texttt{ctridiag} can be called by other programs.
To do this, we run the following command in a terminal:
\begin{lstlisting}[style=ShellInput]
gcc -fPIC -c ctridiag.c -o ctridiag.o
\end{lstlisting}
This calls the C-compiler gcc and tells it to compile the file \texttt{ctridiag.c} to an object file \texttt{ctridiag.o}.
The \texttt{-c} flag tells it to compile to an object file without linking as opposed to compiling to some sort of executable.
Those that are familiar with C may have noticed that we did not include a \li{main} function in our C file.
The \texttt{-c} flag is what prevents this from causing errors with the compiler.
The \texttt{-fPIC} option is required when building code for a shared object.

Once we have run this command the file has been compiled to a \of file, but the functions in the \of file cannot be used directly as python functions.
Although Python relies on C internally, it needs some special interfaces to interact with these \of files.
A general approach for accessing these external functions from Python is to define in some way what inputs each function takes.
We will show how this is done with Cython.
Since a Cython file compiles to a C file which is then compiled to a callable extension for Python, it can be used to interface with C code and other object files.
We will use the following approach:
\begin{enumerate}
\item Write a C header that specifies what the function is named and how it is called.
\item Write a Cython file that imports the header to its C file and defines a Python wrapper for C function to import.
\item Compile the Cython file to C code.
\item Compile the Cython file to a Python extension, giving it access to the object file we are accessing and giving the compiler access to the proper header files.
\end{enumerate}

An approach similar to this will work for some other types of \of files compiled from other languages as well.

The header file that defines how to interface with the C function is very simple.
This file will tell Cython how to interact with the object file once it has been loaded.

\lstinputlisting[style=fromfile, language=C, name=ctridiag.h]{./ctridiag/ctridiag.h}

The following Cython file defines a Python wrapper for the C function contained in the object file:

\lstinputlisting[style=fromfile, language=Python, name=cython_ctridiag.pyx]{./ctridiag/cython_ctridiag.pyx}

\begin{info}
This is a good example of why header files are used.
A C header file tells the C compiler how to interface with compiled C functions that it will link to later in compilation.
The compiler then allows for the use of the given function throughout the C file as if it had already been defined.

The Cython \li{cdef extern} declaration is used in the same way for the Cython compiler.
It essentially tells the Cython compiler, "When the C file generated from this .pyx file is compiled it will be linked against a C library that contains a C function that acts like this: ..."
Describing the C function this way to the Cython compiler allows it to check for undefined references and improper function use before building the C file.

It is possible to define Cython headers in much the same way as C headers.
These headers use the .pxd file extension and can be imported via the \li{cimport} statement in Cython.
.pxd files are called Cython declaration files.
When you use a \li{cimport} statement to import basic C functions or to interface with NumPy arrays and NumPy datatypes, you are really importing Cython declaration files that contain the proper signatures so that the Cython compiler can know how these functions are meant to behave.
Several different tools for automatically generating Cython declaration files from C and C++ headers are available online.
\end{info}

Now that the necessary source files are written, we can build the Python extension.
On Windows, using the compiler MinGW (a version of gcc for windows), the compilation can be performed by running the following setup file.

\lstinputlisting[style=fromfile, language=Python, name=ctridiag_setup_windows64.py]{./ctridiag/ctridiag_setup_windows64.py}

The following file works on Linux and Macintosh machines using gcc.

\lstinputlisting[style=fromfile, language=Python, name=ctridiag_setup_linux.py]{./ctridiag/ctridiag_setup_linux.py}

The distutils module in Python is designed to allow the compilation of Python extensions on different platforms with a single setup file.
The following setup file uses distutils to compile the Cython file, and may be run on Windows, Linux, or Macintosh-based computers.
This is a more standard way to build a Cython extension.

\lstinputlisting[style=fromfile, language=Python, name=ctridiag_setup_distutils.py]{./ctridiag/ctridiag_setup_distutils.py}

This setup file can be called via the command line with the following command:
\begin{lstlisting}[style=ShellInput]
python ctridiag_setup_distutils.py build_ext --inplace
\end{lstlisting}
The \texttt{--inplace} flag tells the script to compile the extension in the current directory.

Here is a simple script to test whether or not the compilation worked.

\lstinputlisting[style=fromfile, language=Python, name=ctridiag_test.py]{./ctridiag/ctridiag_test.py}

\begin{info}
It is a good idea to test whether or not these compilations work using test scripts instead of doing it manually or in an IPython notebook since you may run into trouble when trying to build an extension that replaces a file currently in use.
\end{info}

\section*{Wrapping a Fortran Function}
By making some small modifications to our original approach we can also use Cython to interface with functions that have been written in Fortran.
Here we will consider a simple Fortran implementation of the same function as before.
Here is that same algorithm implemented in Fortran.
Since this function does not return a value, it is called a subroutine in Fortran.
In this case we have used the \li{iso_c_binding} library in C to make the function accept pointers to native C types.
If we were wrapping a function or subroutine that we did not write ourselves we would have to define a Fortran function that uses the \li{iso_c_binding} library to accept pointers from C and then uses the values it receives to call the original function.
\lstinputlisting[style=fromfile, language=Fortran, name=ftridiag.f90]{./ftridiag/ftridiag.f90}

We will now write a header that tells C how to interface with the function we have just defined.
\begin{warn}
When interfacing between Fortran and C, you will have to pass pointers to \emph{all} the variables you send to the Fortran function as arguments.
Passing a variable directly will probably crash Python.
\end{warn}
Here is the file:
\lstinputlisting[style=fromfile, language=C, name=ftridiag.h]{./ftridiag/ftridiag.h}

To compile \li{ftridiag.f90} to an object file you can run the following command in your command line:
\begin{lstlisting}[style=ShellInput]
gfortran -fPIC -c ftridiag.f90 -o ftridiag.o
\end{lstlisting}
As before, if you are working on a Linux or Macintosh based machine you will probably need to include the \texttt{-fPIC} flag as well.

Aside from the small changes in the names used, the modified header file, and the use of gfortran instead of gcc, the rest of the compilation process is entirely the same.
The test script that verifies that the function works properly is the same except that it will import the function from the module \li{cython_ftridiag} instead.
The setup files specific to Windows and Linux, the setup file using distutils, and the test script are included with this lab.

\begin{warn}
When working with higher dimensional arrays Fortran differs from C in that the columns are stored in contiguous blocks of memory instead of the rows.
The default for NumPy and for C arrays is to have rows stored in contiguous blocks, but this varies, depending on what computation has already been done with the NumPy array.
This means that, whenever you need to interface with Fortran code that is meant to act on arrays with multiple dimensions, you \emph{must} be very careful about what is a row and what is a column.
When you pass a Fortran subroutine a pointer to an array it will, by default, assume that the entries of the array are arranged in Fortran order.
Many Fortran routines have options that allow you to specify whether to act on an array or its transpose, but you should still be very careful about this.
\end{warn}

\begin{info}
It may seem unusual that we have already provided a Fortran function that has been modified so that it is callable from C.
Though this is not pure Fortran, it is not necessarily an uncommon scenario.
Many larger Fortran libraries (for example, LAPACK) provide C wrappers for all their functions.
When wrapping a Fortran library, a C wrapper may already be available for use.
\end{info}

Here is a file for the next problem:
\lstinputlisting[style=fromfile, language=Fortran, name=fssor.f90]{./fssor/fssor.f90}

\begin{problem}
The Fortran subroutine \li{fssor} included with this lab uses a technique called Successive Over Relaxation to solve Laplace's equation on a rectangular domain discretized as a grid of equally spaced points.
It solves the same problem as the one involving Laplace's equation in Lab \ref{lab:NumPyArrays}.
It takes the following inputs:
\begin{itemize}
\item $U$ is expected to be a pointer to a Fortran ordered array of double precision numbers.
\item $m$ is a pointer to an integer storing the number of rows of $U$.
\item $n$ is a pointer to an integer storing the number of columns of $U$.
\item $\omega$ is a pointer to a double precision floating point value storing some parameter between $1$ and $2$.
The closer this value is to $2$, the faster the algorithm will converge, but if it is too close the algorithm may not converge at all.
For this lab just use $1.9$.
\item tol is a pointer to a floating point number storing a tolerance used to determine when the algorithm should terminate.
\item maxiters is a pointer to an integer storing the maximum allowable number of iterations.
\item info is a pointer to an integer.
This subroutine will set info to $0$ if it converged to a solution within the given tolerance.
It will set info to $1$ if it did not.
\end{itemize}

Write a corresponding C header \li{fssor.h}, Cython wrapper \li{cython_fssor.pyx}, and Cython setup file (using distutils) \li{fssor_setup_distutils.py} to wrap this subroutine for use in Python.
Call the wrapper function in your Cython file \li{cyssor}.

Fortran works with Fortran ordered arrays, but this algorithm does not need any particular orientation of axes.
Have your Cython wrapper swap the length of the axes when calling \li{fssor} if the array is C ordered.
If it is Fortran ordered, pass the lengths as they are.
Have the wrapper raise an error if the array $U$ is neither Fortran ordered nor C ordered.
Also have it raise an error if the algorithm failed to converge to within the desired tolerances (i.e. if the function sets info to 1).
Use keyword arguments to pass \li{fssor} a default tolerance of $10e^{-9}$ and a default maximum number of iterations to 10000.

Here is how you should be able to call this function:
\begin{lstlisting}
import numpy as np
from cython_fssor import cyssor
resolution = 501
U = np.zeros((resolution, resolution))
X = np.linspace(0, 1, resolution)
U[0] = np.sin(2 * np.pi * X)
U[-1] = - U[0]
cyssor(U, 1.9)
\end{lstlisting}

Generate a 3D plot of $U$ after it has been modified by the function \li{cyssor}.
Use equally spaced points between 0 and 1 as your $x$ and $y$ values.
\end{problem}

Here is some code for the next problem.
\lstinputlisting[style=fromfile, language=C, name=cssor.c]{./cssor/cssor.c}
\begin{problem}
(Optional)
The above C code is another implementation of the Successive Over Relaxation algorithm for solving Laplace's equation.
Wrap it so that it is callable from Python as well.
Produce the same plot as in the previous problem using the C version of this algorithm.
Notice that info is still passed as a pointer because the function needs to be able to modify it.
\end{problem}

\let\of\undefined


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
	\DeclareGraphicsExtensions{.png, .jpeg}
\usepackage[top=1in, bottom=1in, left=1in, right=1in]{geometry}

\title{Compiler Construction \\ PA03: A Lex Scanner for a Simple Calculator}
\author{Terence Henriod}
\date{\today}

\begin{document}

\clearpage            % All of
\maketitle            % this,
\thispagestyle{empty} % removes the page number from the title page

\begin{abstract}
\noindent This assignment requires you to write a \textit{flex}-style scanner for a simple four-function calculator.
\end{abstract}

\newpage
\section{The Assignment}
The scanner should return separate tokens for each of the following:
\begin{itemize}
  \item The `+' (addition) symbol [PLUS]
  \item The `-' (subtraction) symbol [MINUS]
  \item The `*' (multiplication) symbol [MULT]
  \item The `/' (division) symbol [DIV]
  \item The `(' and `)' (parentheses) [OPEN], [CLOSE]
  \item Positive integers (you need not worry about negative integers, nor about floating-point values) [INTEGER]
  \item The `;' (end-of-calculation) symbol [SEMI]
\end{itemize}
Whitespace such as spaces, tabs, and newlines should not be returned. All of the above tokens are predefined for you in a file called (calc.tab.h) which you should include, and you should use these symbolic values. For any other non-whitespace character, your scanner should return the symbolic value ERROR. Your scanner should terminate when end of file is reached, and should read all input from stdin.

Don't forget that you need to pass the parser the actual value of any integers you read in. yylval can be used for this purpose.

You will be given a parser and driver for this assignment in object form. Link this object (calc.tab.o) with the object created by compiling your flex output into a program called calc. The resulting program, when run, should function as a 4-function calculator. You are responsible for generating appropriate test cases for your scanner, and you may wish to create an alternate driver module to do so. (Makefile)

You may discuss this assignment with other students and work the problems together. However, your programs should be your own individual work. Remember that all assignments are to be turned in in class on the date due.

For this project I need you to link in these libraries -lfl, -lfl is for flex.

\section{The Code}
\subsection{calc.lex}
\begin{verbatim}
 /**
  * calc.lex
  *
  * This is a lex file for defining a tokenizer for a simple calculator.
  */

 /* DEFINITIONS */
%{
 #include <stdio.h>
 #include <errno.h>
 #include <limits.h>
 #include "calc.tab.h"

 #define STROL_ERROR -1
%}

ZERO_STRING   [0]
POSITIVE_INT  [1-9][0-9]*
WHITESPACE    [ \t\r\n]+

%%
 /* RULES */

{ZERO_STRING} {
  yylval = 0;
  return INTEGER;
}

{POSITIVE_INT} {
  int error = 0;
  yylval = extract_int(&error, yytext, yyleng);

  if (error != 0) {
    yyerror("The number (%s) is not properly formatted.", yytext);
    return ERROR;
  }

  return INTEGER;
}

[;] {return SEMI;}

[(] {return OPEN;}

[)] {return CLOSE;}

[+] {return PLUS;}

[-] {return MINUS;}

[*] {return MULT;}

[/] {return DIV;}

{WHITESPACE} {/* ignore */;}

. {/* unrecognized characters are errors */ return ERROR;}

%%
 /* USER CODE */

  int extract_int(int* error, char* yytext, const int yyleng) {
    char* end = yytext + yyleng;
    *error = 0;

    int number_value = strtol(yytext, &end, 10);

    if (errno == ERANGE) {
      // the documentation states that the returned value should
      // be LONG_[MIN|MAX] if there is a conversion failure, but I have found
      // that this is not the case. It seems that checking errno is the most
      // reliable method.
      *error = 1;
    }

    return number_value;
  }
\end{verbatim}

\subsection{test.sh}
\begin{verbatim}
#! /bin/bash

#
# A script for testing the Flex tokenizer.
#

clear; clear; clear;

num_passed=0
num_failed=0

function unit_test {
  expression=$1
  expected=$2

  echo "~~~~~~~~~~~~~~~~~~~~~~~"
  echo "CASE: "
  echo "$expression"
  echo ""

  result=$(echo $expression | bin/calc)

  if [[ $result = "" ]]; then
    result="FAIL"
  fi

  if [[ $result -eq $expected ]]; then
    message="passed"
    ((num_passed++))
  else
    message="FAILED"
    ((num_failed++))
  fi

  echo "expected: ""$expected"
  echo "got:      ""$result"
  echo ""
  echo "RESULT: "$message
  echo "~~~~~~~~~~~~~~~~~~~~~~~"
}

echo "========================"
echo "| Building the project |"
echo "========================"
make

echo ""
echo "================="
echo "| Running Tests |"
echo "================="

unit_test '1;'                            1
unit_test '(2);'                          2
unit_test '1 + 2;'                        3
unit_test '7- 3;'                         4
unit_test '1*5;'                          5
unit_test '12 /2;'                        6
unit_test '1+2 *3;'                       7
unit_test $'4\n+\n4\n;'                   8
unit_test '((2 + 1)* 3);'                 9
unit_test '100 / (2*5);'                  10
unit_test '4@+4;'                         "FAIL"
unit_test '99 / 0;'                       "FAIL"
unit_test '00 + 1;'                       "FAIL"
unit_test '1 + 01;'                       "FAIL"
unit_test '99999999999999999999999 + 1;'  "FAIL"

echo ""
echo "============================"
echo "PASSED: ""$num_passed"
echo "FAILED: ""$num_failed"

\end{verbatim}

\subsection{The Unit Test Output}
\begin{verbatim}
========================
| Building the project |
========================
flex -o /home/t/Desktop/CS660/PA03/code/out/calc.lex.yy.c /home/t/Desktop/CS660/PA03/code/src/calc.lex
cc -o /home/t/Desktop/CS660/PA03/code/out/calc.lex.yy.o -c /home/t/Desktop/CS660/PA03/code/out/calc.lex.yy.c -I/home/t/Desktop/CS660/PA03/code/out -L/usr/local/lib -I/home/t/Desktop/CS660/PA03/code/out -lfl
cc -o /home/t/Desktop/CS660/PA03/code/bin/calc /home/t/Desktop/CS660/PA03/code/out/calc.lex.yy.o /home/t/Desktop/CS660/PA03/code/out/calc.tab.o -L/usr/local/lib -I/home/t/Desktop/CS660/PA03/code/out -lfl

=================
| Running Tests |
=================
~~~~~~~~~~~~~~~~~~~~~~~
CASE:
1;

expected: 1
got:      1

RESULT: passed
~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~
CASE:
(2);

expected: 2
got:      2

RESULT: passed
~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~
CASE:
1 + 2;

expected: 3
got:      3

RESULT: passed
~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~
CASE:
7- 3;

expected: 4
got:      4

RESULT: passed
~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~
CASE:
1*5;

expected: 5
got:      5

RESULT: passed
~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~
CASE:
12 /2;

expected: 6
got:      6

RESULT: passed
~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~
CASE:
1+2 *3;

expected: 7
got:      7

RESULT: passed
~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~
CASE:
4
+
4
;

expected: 8
got:      8

RESULT: passed
~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~
CASE:
((2 + 1)* 3);

expected: 9
got:      9

RESULT: passed
~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~
CASE:
100 / (2*5);

expected: 10
got:      10

RESULT: passed
~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~
CASE:
4@+4;

expected: FAIL
got:      FAIL

RESULT: passed
~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~
CASE:
99 / 0;

expected: FAIL
got:      FAIL

RESULT: passed
~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~
CASE:
00 + 1;

expected: FAIL
got:      FAIL

RESULT: passed
~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~
CASE:
1 + 01;

expected: FAIL
got:      FAIL

RESULT: passed
~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~
CASE:
99999999999999999999999 + 1;

expected: FAIL
got:      FAIL

RESULT: passed
~~~~~~~~~~~~~~~~~~~~~~~

============================
PASSED: 15
FAILED: 0
\end{verbatim}

\end{document}


% !TEX TS-program = pdflatex
% !TEX encoding = UTF-8 Unicode

% This is a simple template for a LaTeX document using the "article" class.
% See "book", "report", "letter" for other types of document.

\documentclass[11pt]{article} % use larger type; default would be 10pt

\usepackage[utf8]{inputenc} % set input encoding (not needed with XeLaTeX)

%%% Examples of Article customizations
% These packages are optional, depending whether you want the features they provide.
% See the LaTeX Companion or other references for full information.

%%% PAGE DIMENSIONS
\usepackage{geometry} % to change the page dimensions
\geometry{a4paper} % or letterpaper (US) or a5paper or....
% \geometry{margin=2in} % for example, change the margins to 2 inches all round
% \geometry{landscape} % set up the page for landscape
%   read geometry.pdf for detailed page layout information

\usepackage{graphicx} % support the \includegraphics command and options

% \usepackage[parfill]{parskip} % Activate to begin paragraphs with an empty line rather than an indent

%%% PACKAGES
\usepackage{booktabs} % for much better looking tables
\usepackage{array} % for better arrays (eg matrices) in maths
\usepackage{paralist} % very flexible & customisable lists (eg. enumerate/itemize, etc.)
\usepackage{verbatim} % adds environment for commenting out blocks of text & for better verbatim
\usepackage{subfig} % make it possible to include more than one captioned figure/table in a single float
% These packages are all incorporated in the memoir class to one degree or another...
\usepackage{amsmath}
\usepackage{amssymb}
%%% HEADERS & FOOTERS
\usepackage{fancyhdr} % This should be set AFTER setting up the page geometry
\pagestyle{fancy} % options: empty , plain , fancy
\renewcommand{\headrulewidth}{0pt} % customise the layout...
\lhead{}\chead{}\rhead{}
\lfoot{}\cfoot{\thepage}\rfoot{}

%%% SECTION TITLE APPEARANCE
\usepackage{sectsty}
\allsectionsfont{\sffamily\mdseries\upshape} % (See the fntguide.pdf for font help)
% (This matches ConTeXt defaults)

%%% ToC (table of contents) APPEARANCE
\usepackage[nottoc,notlof,notlot]{tocbibind} % Put the bibliography in the ToC
\usepackage[titles,subfigure]{tocloft} % Alter the style of the Table of Contents
\renewcommand{\cftsecfont}{\rmfamily\mdseries\upshape}
\renewcommand{\cftsecpagefont}{\rmfamily\mdseries\upshape} % No bold!

%%% END Article customizations

%%% The "real" document content comes below...

\title{Homework based on Chapter 6\\
Computational Probability and Statistics \\
CIS 2033, Section 002}
\author{}
\date{} % Activate to display a given date or no date (if empty),
         % otherwise the current date is printed

\begin{document}
\maketitle

\section{Part 1 (Due: 9:00 AM, Friday, Feb. 20, 2015)}
\paragraph*{Question 1} Let $U$ follows a $U(0,1)$ distribution. Let $X$ follows a binomial distribution $Bin(3, 0.5)$.Describe how to simulate the outcome of $X$. (Hint: $X$ can take values from $k=\{0,1,2,3\}$, $p(k)={3 \choose k}(0.5)^k(0.5)^{3-k}$)
\paragraph*{Question 2} Somebody messed up the random number generator in your computer, instead of uniform random numbers it generates numbers with an $Par(3)$ distribution. Describe how to construct a $U(0,1)$ random variable $U$ from an $Par(3)$ distributed $X$.

\section{Part 2 (Due: 11:59 PM, Tuesday, 24, 2015)}

\paragraph*{Question 3} A random variable X has a $Exp(2)$ distribution. For details of the Exponential distribution see the notes. Describe how to construct $X$ from $U(0, 1)$ random variable.

\paragraph*{Question 3} A random variable X has a $Par(5)$ distribution. For details of the Exponential distribution see the notes. Describe how to construct $X$ from $U(0, 1)$ random variable.

\newpage
\textbf{Sample Questions}
\paragraph*{6.1} Let $U$ have a $U(0, 1)$ distribution. Describe how to simulate the outcome of a roll with a die using $U$.

\textbf{Answer}. Denote $X$ the random variable for the outcome of throwing a die. $X$ can take $k={1,2,3,4,5,6}$. The PMF of $X$ is $P(X=1) = P(X=2) = P(X=3) = P(X=4) = P(X=5) = P(X=6) = \frac{1}{6}; 0~$elsewhere. To simulate the PMF of $X$, is to define a partition on $\Omega$ of $U$ such that the area of each part corresponds to a probability of $X$.

\begin{table}[h!]
\renewcommand{\arraystretch}{1.5}
\centering
\caption{Simulation of rolling a die using $U(0, 1)$.}
\label{Ta:6.1a}
\begin{tabular}{|c|c|c|c|c|c|c|} \hline
Events of U & $0\leq U < \frac{1}{6}$ & $\frac{1}{6} \leq U < \frac{2}{6}$ &
$\frac{2}{6} \leq U < \frac{3}{6}$ & $\frac{3}{6} \leq U < \frac{4}{6}$ &
$\frac{4}{6} \leq U < \frac{5}{6}$ & $\frac{5}{6} \leq U \leq 1$ \\ \hline
Events of X & $X$=1 & $X$=2 & $X$=3 & $X$=4 & $X$=5 & $X$=6 \\ \hline
Equal probabilities & $\frac{1}{6}$ & $\frac{1}{6}$ & $\frac{1}{6}$ & $\frac{1}{6}$ & $\frac{1}{6}$ & $\frac{1}{6}$ \\ \hline
\end{tabular}
\end{table}

Notice: the second row of the table can be shuffled, which is also a valid simulation.

\paragraph*{6.6} Somebody messed up the random number generator in your computer, instead of uniform random numbers it generates numbers with an $Exp(2)$ distribution. Describe how to construct a $U(0,1)$ random variable $U$ from an $Exp(2)$ distributed $X$.

\textbf{Answer}.For the uniform distribution, $U(0, 1)$, the distribution function is $F_U(u) = u$ for $0 \leq u \leq 1$. For the exponential distribution, $Exp(2)$, the distribution function is $F_X(x) = 1 - e^{-2x}$ for $x \geq 0$. In order to construct a $U(0, 1)$ random variable $U$ from an $Exp(2)$ distributed $X$, we have to compute $F_U(u) = F_X(x)$. This means $u = 1-e^{-2x}$ for $x \geq 0$. Then, we can define the $U(0, 1)$ distributed random variable $U = F^{inv}(X) = 1-e^{-2X}$. If $U$ is a uniform distribution, then $1-U$ is also a uniform distribution, we can also construct $U = e^{-2X}$.

%\paragraph*{6.8} A random variable X has a $Par(3)$ distribution, so with distribution function $F$ with $F(x) = 0$ for $x < 1$, and $F(x) = 1-x^{-3}$ for $x \geq 1$. For details of the Pareto distribution see Section 5.4. Describe how to construct $X$ from $U(0, 1)$ random variable.
%
%For the $U(0, 1)$ random variable $U$, the distribution function is $F_U(u) = u$ for $0 \leq u \leq 1$. For the $Par(3)$ random variable $X$, the distribution function is $F_X(x) = 1 - \frac{1}{x^{3}}$, for $x \geq 1$. In order to construct a $Par(3)$ random variable $X$ from a $U(0, 1)$ distributed variable $U$, we have to compute $F_X(x) = F_U(u)$. This means $1 - \frac{1}{x^3} = u$. Then we can get $x = (1-u)^{-\frac{1}{3}}$, for $0 \leq u \leq 1$. Then, we can define the $Par(3)$ distributed random variable $X = (1 - U)^{-\frac{1}{3}}$. Since $Y = 1-U$ is also a $U(0, 1)$ distributed random variable is $U$ is a $U(0, 1)$ distributed random variable, we can also construct $X = U^{-\frac{1}{3}}$.
%
%\paragraph*{6.9} In Quick Exercise 6.1 we simulated a die by tossing three coins. Recall that we might need several attempts before succeeding.
%
%If tossing three coins, we could get eight possible outcomes, {HHH, HHT, HTH, THH, HTT, THT, TTH, TTT}. For a die, there should be six outcomes, {1, 2, 3, 4, 5, 6}. Thus, we could match
%
%\begin{table}[h!]
%\renewcommand{\arraystretch}{1.5}
%\centering
%\caption{Simulation of rolling a die using $U(0, 1)$.}
%\label{Ta:6.1a}
%\begin{tabular}{|c|c|c|c|c|c|c|} \hline
%three coins & HHH & HHT & HTH & THH & HTT & THT \\
%die & 1 & 2 & 3 & 4 & 5 & 6 \\ \hline
%\end{tabular}
%\end{table}
%If the outcome is HHT, or TTT, we toss those three coins again until we meet the other six outcomes.
%
%\subparagraph*{a.} What is the probability that we succeed on the first try ?
%
%The probability is $\frac{6}{8} = \frac{3}{4}$.
%
%\subparagraph*{b.} Let $N$ be the number of tries that we need. Determine the distribution of $N$.
%
%We know each trial is a $Ber(\frac{3}{4})$. We repeated it until succeed. We know $N \sim Geo(\frac{3}{4})$.
\end{document}


\lab{Getting Started with Git}{Setup and Git}
\label{appendix:setup}
\objective{
Computer code is delicate.
A rogue or clumsy programmer can easily damage a program with a simple spelling error.
Maintaining a working product is therefore a serious endeavor in software development, requiring checks, collaboration, and careful coordination.
\emph{Git} is a version control system that facilitates code development involving multiple contributors.
It is commonly used to manage large projects, especially open-source projects, but it can also be used for personal code storage and management.
\\ \indent Git is our tool of choice for submitting lab assignments and for receiving feedback.
In this appendix we introduce Bitbucket, a web-based hosting service for git, and give an overview of important git commands.
}

% TODO: Set up a BYU ACME TA account on Bitbucket with two repositories: one for the students to import, and one for the TA's to import.

\section*{Overview} % =========================================================

The main idea behind git is that a master copy of a program's code is kept online in a \emph{repository}.
All certified contributors can \emph{clone} the repository onto their machine, make changes to the code, then submit those changes to the online repository.

Git is different from other standard file-sharing services such as Google Drive in that it is specifically designed for managing code and other small files.
It is harder to use than Google Drive, but the precision and safeguards that git provides are well worth the investment of learning how to use it.

\subsection*{Installation} % --------------------------------------------------

Most Linux and Macintosh machines come with git pre-installed.
Download the appropriate installer at \url{http://git-scm.com/downloads}.
Git is underlying software that will be accessed through the command line, so the installation will not create a new visible application.
On Windows machines, however, it will also download a terminal-like interface just for git called \emph{git bash}.

\subsection*{Import the Template Repository} % --------------------------------

Many different companies have websites for hosting online git repositories.
We will use Atlassian Inc.'s \href{https://bitbucket.org/}{\emph{Bitbucket}} because it is free to set up a limited number of private repositories.
Other popular repository hosts include \href{https://github.com/}{GitHub}, \href{https://gitlab.com/}{GitLab}, and more.

There are two ways to set up a new repository: by importing an existing repository, or by starting from scratch.
We have a template repository for you to import, but most git hosts present easy instructions for creating and setting up an empty repository.

Closely follow these steps to import the template repository:

\begin{enumerate}
    \item Make an account at \url{https://bitbucket.org}.
    Write down your user name and password somewhere---you will need to type the password often.
    \item Click ``not now'' if it asks to set up your first repository.
    \item At the top, click ``Repositories,'' then ``Import Repository.''
    \item Enter the URL for the template repository:\\\url{https://bitbucket.org/byuacmeta/template}.
    \item Name the repository with the convention ``Vol\#A'' or ``Vol\#B,'' depending on the class number (``Vol'' as in ``Volume'').

    \begin{table}[H]
    \begin{tabular}{c|c}
        Class & Repository Name \\
        \hline
        Math 345 & Vol1A \\
        Math 321 & Vol2A \\
        Math 347 & Vol1B \\
        Math 323 & Vol2B \\
        Math 403 & Vol3A \\
        Math 437 & Vol4A \\
        Math 405 & Vol3B \\
        Math 439 & Vol4B
    \end{tabular}
    \end{table}

    Do \textbf{not} use spaces, change the capitalization, or do anything else to change the title.
    Please.

    \item Make sure the box for ``This is a private repository'' \textbf{is checked}.
    \item Click ``Advanced settings.''
    \item Write a short description for the repository.
    \item Under ``Forking,'' select ``No forks.''
    \item For ``Language,'' select ``Python.''
    \item Do \textbf{not} check the boxes for Issue tracking, Wiki, or HipChat.
    \item Press ``Import repository.''
\end{enumerate}

\subsection*{Clone the New Repository} % --------------------------------------

Now the new repository is set up in the cloud, but it is not yet connected to a computer.
On Bitbucket, go to the your new repository's page.
In the ``ACTIONS'' menu on the left, click ``Clone'' and copy the text that pops up. % (it should look like \li{git clone https://bitbucket.org/<username>/Vol1A} or similar).

On your computer, open a shell terminal (called \emph{Terminal} on Linux and Max and \emph{GitBash} on Windows).
Use \li{cd}, \li{ls}, and \li{pwd} to navigate to the directory where you would like to make a copy of the online repository (on the lab computers, this should be in the \texttt{myacmeshare/} directory).
Execute the command copied from the ``Clone'' button:
\li{git clone https://bitbucket.org/<username>/Vol1A}.

\begin{lstlisting}[language=bash]
$ pwd                               # Print the working directory.
/Users/Guest
$ ls                                # List the files in the current directory.
Desktop     Documents       Downloads       Pictures        Music
myacmeshare

$ cd myacmeshare                    # Change directories to 'myacmeshare/'.
$ pwd
/Users/Guest/myacmeshare
$ git clone https://bitbucket.org/<username>/Vol1A
Cloning into 'Vol1A'...
\end{lstlisting}

This command creates a folder (called \texttt{Vol1A/} in this case), places all files from the repository in that folder, and automatically connects the folder to the online repository via the alias ``origin.''
% You will probably be asked to provide your Bitbucket password to authenticate the cloning, since the repository is private.

\subsection*{Lab Submission Policies} % ---------------------------------------

Back on the repository's Bitbucket page, click the blue ``Send invitation'' button on the right side of the ``Overview'' page.
Enter your grader's email address or Bitbucket user name.
Select ``write'' priveleges for the grader.
Finally, click the ``Share'' button.

Since the grader now has access to your repository, they will be able to grade your assignments and submit feedback.
\textbf{Lab assignments should be named after the specifications file provided on the ACME website}, allowing the grader's automated test drivers to locate and test the appropriate file.
Feedback will be returned via \texttt{.txt} files in the \texttt{Feedback/} directory (the grader will create this folder in your repository if it does not exist).

Git is designed to store source code files, not large data files.
When a lab uses a large data set, download the data and put it in your repository folder, but \textbf{do not add or commit the data file}.
That way, you can use the data locally without pushing it up to the cloud.

Finally, please do not submit \emph{anything} other than the source code needed to run your solutions.
In addition, no Python file submission should ever execute any code in its main body.
Every file should only include import statements, function declarations, and class declarations.
Anything else (tests, etc.) must be placed in an \li{if __name__ == "__main__":} block at the end of the file so that it is not executed when the automated test driver imports the file for grading.

\section*{Using Git} % ========================================================

Git manages the history of a file system through a series of \emph{commits}, or checkpoints.
Use \li{git status} to see the files that have been changed since the last commit.
These changes are then moved to the \emph{staging area}, a list of files to save during the next commit, with \li{git add <filename(s)>}.
Save the changes in the staging area with \li{git commit -m "A brief message describing the changes"}.

All of these commands are done within the clone of the repository.
The clone must be manually synchronized with the online repository via two other git commands: \li{git pull origin master}, to pull updates from the web to the computer; and \li{git push origin master}, to push updates from the computer to the web.

A typical work session using git might look like the following.

\begin{lstlisting}[language=bash]
$ cd Desktop/Vol1A/
$ git pull origin master                           # Pull updates.
$ touch python_intro.py                            # Make changes.
$ git add python_intro.py                          # Track changes.
$ git commit -m "Made a file for the first lab."   # Commit changes.
$ git push origin master                           # Push updates.
\end{lstlisting}

With the full output from each of these commands, plus running \li{git status} a few times to check the staging area, the work session will be as follows.

\begin{lstlisting}[language=bash]
# Navigate to the clone of the repository.
$ cd Desktop/Vol1A

# Pull any updates from the online repository (such as TA feedback).
$ git pull origin master
From https://bitbucket.org/byuacmeta/template
 * branch            master     -> FETCH_HEAD
Already up-to-date.

# Work on the labs. For example, create a new file for the first lab.
$ touch python_intro.py
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
  (use "git add <file>..." to include <<in>> what will be committed)

    python_intro.py

nothing added to commit but untracked files present (use "git add" to track)

$ git add python_intro.py
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    new file:   python_intro.py

# Continued on the next page.
\end{lstlisting}

\newpage

\begin{lstlisting}
# Commit the changes to the repository with an informative message.
$ git commit -m "Made a file for the first lab"
[master fed9b34] Made a file <<for>> the first lab
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 python_intro.py

# Push the changes to the online repository.
$ git push origin master
Counting objects: 3, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 327 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://byuacmeta@bitbucket.org/byuacmeta/template.git
   5742a1b..fed9b34  master -> master

# The changes have been saved and the online repository updated.
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
\end{lstlisting}

Commit often and use informative messages to track your progress.

\begin{figure}[H]
\captionsetup[subfigure]{justification=centering}
\centering
\begin{subfigure}{.6\textwidth}
    \centering
    \includegraphics[width=\linewidth]{xkcd2.png}
    \caption{\url{https://xkcd.com/1296/}}
\end{subfigure}
\quad
\begin{subfigure}{.35\textwidth}
    \centering
    \includegraphics[width=\linewidth]{xkcd1.png}
    \caption{\url{https://xkcd.com/1597/}}
\end{subfigure}
\end{figure}

\newpage

\subsection*{Index of Git Commands} % -----------------------------------------

Execute \li{<<git help>>} for general help, or run \li{<<git help <command>>>} for instructions on a specific command.

\begin{table}[H]
\centering
\begin{tabular}{l|l}
    Command & Description \\ \hline
    \li{git status} & Display the staging area and untracked changes. \\
    \li{git pull origin master} & Pull down changes from the online repository.\\
    \li{git push origin master} & Push up changes to the online repository. \\
    \li{git add <filename(s)>} & Add a file or files to the staging area.\\
    \li{git add -u} & Add all of the modified, previously tracked files \\ & to the staging area. \\
    \li{git commit -m <<\"<message>\">>} & Save the changes in the staging area with a given \\ & message.\\
    \li{git checkout -- <filename>} & Revoke the changes made since the last commit \\ & on a file that is not in the staging area. \\
    \li{git reset -- <filename>} & Remove a file from the staging area. \\
    \li{git diff <filename>} & See the changes made on an unstaged file since \\ & the last commit. \\
    \li{git diff --cached <filename>} & See the changes made on a staged file since the \\ & last commit. \\
    \li{git log} & See the commit history (press \li{q} to exit). \\
\end{tabular}
\end{table}


\documentclass{article}
\usepackage{ amssymb }
\usepackage{amsmath}
\usepackage{mathtools}

\begin{document}
\title{Quiz3}
\author{Malte Wegener (4672194)}

\maketitle

\section{Question 1}
MPI(Message Passage Interface) is parallel Programming Model in which there is no global shared memory. Each Processor has its own private memory. If one Procesor needs data from a different Processor, a message has to be send containing the data. There are 4 modes of messaging in MPI.
\begin{itemize}
    \item Standard Send (MPI\_SEND). In this mode, the Implementation decides whether to use synchronous or buffered mode on the fly
    \item Synchronous Send (MPI\_SSEND). In this mode, the send halts the process until a mathcing recieve is available
    \item Buffered Send (MPI\_BSEND). In this mode, the message is passed into a buffer and then send if a matching recieve is available.
    \item Ready Send (MPI\_RSEND). In this mode the message is send regardless if there is a recieve available, but only suceeds if there is a recieve available. There is no reason to ever use this.
\end{itemize}

\section{Question 2}
As the serial execution time is $T^{\star}=n^3$ and $T_p(p) = \frac{n^3}{p}+p^{1.5}n$. The efficiency is given by $E_p = \frac{T^{\star}}{p*T_p(p)}$. In order to keep the efficiency equal when doubling the number of processors, n has to be increased.

\begin{align}
    \frac{1+p^{2.5}}{n_1^2} = \frac{1+(2p)^{2.5}}{n_2^2}\\
    \frac{n_2}{n_1} = \sqrt{\frac{1+(2p)^{2.5}}{1+p^{2.5}}}
\end{align}
Thus the increase in n has to follw the the previous relation when efficiency should stay the same. In the limit as $n \to \infty$ this approaches 2.387.

When keeping execution time constant when increasing n by increasing p the following relation emerges
\begin{align}
\frac{n_1^3}{p_1}+p_1^{1.5}n_1 = \frac{n_2^3}{p_2}+p_2^{1.5}n_2
\end{align}
However when p is increased this will decrease efficiency of the program.

\section{Question 3}
The data locality ratio is the ratio between the required time for computations between 2 communications and the time required for communication. A higher ratio is better for program performance as it indicates that more time is actually spent computing rather than communicating what to compute.

\end{document}


%!TEX root = forallxcam.tex

\chapter{Alternative proof systems}
In formulating my natural deduction system, I treated certain rules of natural deduction as \emph{basic}, and others as \emph{derived}. However, I could equally well have taken various different rules as basic or derived. I shall illustrate this point by considering some alternative treatments of disjunction, negation, and the quantifiers. I shall also explain why I have made the choices that I have.

\section{Alternative negation rules}
Some systems take the following rule as their basic negation introduction rule:
\begin{proof}
	\open
	\hypo[m]{a}{\meta{A}}
	\have[n-1]{b}{\meta{B}}
	\have[n]{nb}{\enot\meta{B}}
	\close
	\have[\ ]{}{\enot\meta{A}}\by{$\enot$I*}{a-nb}
\end{proof}
and the following as their basic negation elimination rule:
\begin{proof}
	\open
	\hypo[m]{na}{\enot\meta{A}}
	\have[n][-1]{b}{\meta{B}}
	\have{nb}{\enot\meta{B}}
	\close
	\have[\ ]{a}{\meta{A}}\by{$\enot$E*}{na-nb}
\end{proof}
Using these two rules, we could have derived all of the rules governing negation and contradiction that we have taken as basic (i.e.\ $\enot$I, $\enot$E, X and TND). Indeed, we could have avoided all use of the symbol `$\ered$' altogether. Negation would have had a single introduction and elimination rule, and would have behaved much more like the other connectives.

The resulting system would have had fewer rules than ours. So why did I chose to separate out contradiction, and to use an explicit rule TND?\footnote{P.D.\ Magnus's original version of this book went the other way.}

My first reason is that adding the symbol `$\ered$' to our natural deduction system makes proofs considerably easier to work with.

My second reason is that a lot of fascinating philosophical discussion has focussed on the acceptability or otherwise of \emph{ex falso quodlibet} (i.e.\ X) and \emph{tertium non datur} (i.e.\ TND). By treating these as separate rules in the proof system, we will be  in a better position to engage with that philosophical discussion. In particular: having invoked these rules explicitly, it will be much easier for us to know what a system which lacked these rules would look like.

\section{Alternative disjunction elimination}
Some systems take DS as their basic rule for disjunction elimination. They can then treat the $\eor$E rule as a derived rule, by using the following proof scheme:
	\begin{proof}
		\have[m]{ab}{\meta{A}\eor\meta{B}}
		\open
			\hypo[i]{a}{\meta{A}} {}
			\have[j]{c1}{\meta{C}}
		\close
		\open
			\hypo[k]{b}{\meta{B}}{}
			\have[l]{c2}{\meta{C}}
		\close
		\have[n]{aic}{\meta{A} \eif \meta{C}}\ci{a-c1}
		\have{bic}{\meta{B} \eif \meta{C}}\ci{b-c2}
		\open
		\hypo{c}{\meta{C}}
		\have{c1}{\meta{C}}\by{R}{c}
		\close
		\open
		\hypo{nc}{\enot \meta{C}}
		\open
			\hypo{a1}{\meta{A}}
			\have{c2}{\meta{C}}\ce{aic, a1}
			\have{nc1}{\ered}\ri{c2, nc}
		\close
		\have{na}{\enot\meta{A}}\ni{a1-nc1}
		\have{b}{\meta{B}}\ds{ab, na}
		\have{c3}{\meta{C}}\ce{bic, b}
	\close
	\have{con}{\meta{C}}\tnd{c-c1, nc-c3}
\end{proof}
So why did I choose to take $\eor$E as basic, rather than DS?\footnote{P.D.\ Magnus's original version of this book went the other way.} My reasoning is that DS involves the use of `$\enot$' in the statement of the rule. It is in some sense `cleaner' for our disjunction elimination rule to avoid mentioning \emph{other} connectives.

\section{Alternative quantification rules}
An alternative approach to the quantifiers is to take as basic the rules for $\forall$I and $\forall$E from \S\ref{s:BasicFOL}, and also two CQ rule which allow us to move from $\forall \meta{x} \enot \meta{A}$ to $\enot \exists \meta{x} \meta{A}$ and vice versa.\footnote{Warren Goldfarb follows this line in \emph{Deductive Logic}, 2003, Hackett Publishing Co.}

Taking only these rules as basic, we could have derived the  $\exists$I and $\exists$E rules provided in \S\ref{s:BasicFOL}. To derive the $\exists$I rule is fairly simple. Suppose $\meta{A}$ contains the name $\meta{c}$, and contains no instances of the variable $\meta{x}$, and that we want to do the following:
\begin{proof}
	\have[m]{a}{\meta{A}(\ldots \meta{c} \ldots \meta{c}\ldots)}
	\have[k]{c}{\exists \meta{x} \meta{A}(\ldots \meta{x} \ldots \meta{c}\ldots)}
\end{proof}
This is not yet permitted, since in this new system, we do not have the $\exists$I rule. We can, however, offer the following:
\begin{proof}
	\hypo[m]{a}{\meta{A}(\ldots \meta{c} \ldots \meta{c}\ldots)}
	\open
		\hypo{nEna}{\enot \exists \meta{x} \meta{A}(\ldots \meta{x} \ldots \meta{c}\ldots)}
		\have{Ana}{\forall \meta{x} \enot \meta{A}(\ldots \meta{x} \ldots \meta{c}\ldots)}\cq{nEna}
		\have{nAc}{\enot\meta{A}(\ldots \meta{c} \ldots \meta{c}\ldots)}\Ae{Ana}
		\have{red}{\ered}\ri{a, nAc}
	\close
	\have{nnEa}{\enot \enot \exists \meta{x} \meta{A}(\ldots \meta{x} \ldots \meta{c}\ldots)}\ni{nEna-red}
	\have{end}{\exists\meta{x} \meta{A}(\ldots \meta{x} \ldots \meta{c}\ldots)}\dne{nnEa}
\end{proof}\noindent
To derive the $\exists$E rule is rather more subtle. This is because the $\exists$E rule has an important constraint (as, indeed, does the $\forall$I rule), and we need to make sure that we are respecting it. So, suppose we are in a situation where we \emph{want} to do the following:
\begin{proof}
	\have[m]{ExA}{\exists \meta{x} \meta{A}(\ldots \meta{x} \ldots \meta{x}\ldots)}
	\open
		\hypo[i]{Ac}{\meta{A}(\ldots \meta{c} \ldots \meta{c}\ldots)}
		\have[j]{B}{\meta{B}}
	\close
	\have[k]{end}{\meta{B}}
\end{proof}\noindent
 where $\meta{c}$ does not occur in any undischarged assumptions, or in $\meta{B}$, or in $\exists \meta{x} \meta{A}(\ldots \meta{x} \ldots \meta{x}\ldots)$. Ordinarily, we would be allowed to use the $\exists$E rule; but we are not here assuming that we have access to this rule as a basic rule. Nevertheless, we could offer the following, more complicated derivation:

\begin{proof}
	\have[m]{ExA}{\exists \meta{x} \meta{A}(\ldots \meta{x} \ldots \meta{x}\ldots)}
	\open
		\hypo[i]{Ac}{\meta{A}(\ldots \meta{c} \ldots \meta{c}\ldots)}
		\have[j]{B}{\meta{B}}
	\close
	\have[k]{condi}{\meta{A}(\ldots \meta{c} \ldots \meta{c}\ldots) \eif \meta{B}}\ci{Ac-B}
	\open
		\hypo{nB}{\enot \meta{B}}
		\have{nAc}{\enot \meta{A}(\ldots \meta{c} \ldots \meta{c}\ldots)}\mt{condi, nB}
		\have{AxnA}{\forall \meta{x} \enot \meta{A}(\ldots \meta{x} \ldots \meta{x}\ldots)}\Ai{nAc}
		\have{nEA}{\enot \exists \meta{x} \meta{A}(\ldots \meta{x} \ldots \meta{x}\ldots)}\cq{AxnA}
		\have{red2}{\ered}\ri{ExA, nEA}
	\close
	\have{nnB}{\enot\enot\meta{B}}\ni{nB-red2}
	\have{end}{\meta{B}}\dne{nnB}
\end{proof}\noindent
We are permitted to use $\forall$I on line $k+3$ because $\meta{c}$ does not occur in any  undischarged assumptions or in $\meta{B}$. The entries on lines $k+4$ and $k+1$ contradict each other, because $\meta{c}$ does not occur in $\exists \meta{x} \meta{A}(\ldots \meta{x} \ldots \meta{x} \ldots)$.

Armed with these derived rules, we could now go on to derive the two remaining CQ rules, exactly as in \S\ref{s:DerivedFOL}.

So, why did I start with all of the quantifier rules as basic, and then derive the CQ rules?

My first reason is that it seems more intuitive to treat the quantifiers as on a par with one another, giving them their own basic rules for introduction and elimination.

My second reason relates to the discussion of alternative negation rules. In the derivations of the rules of $\exists$I and $\exists$E that I have offered in this section, I invoked DNE. This is a derived rule, whose derivation essentially depends upon the use of TND. But, as I mentioned earlier, TND is a contentious rule. So, if we want to move to a system which abandons TND, but which still allows us to use existential quantifiers, we shall want to take the introduction and elimination rules for the quantifiers as basic, and take the CQ rules as derived. (Indeed, in a system without TND, we shall be \emph{unable} to derive the CQ rule which moves from $\enot \forall \meta{x} \meta{A}$ to $\exists \meta{x} \enot \meta{A}$.)


\clearpage
\subsection{Allocating Memory} % (fold)
\label{sub:allocating_memory}

With dynamic memory management, one of the tasks you can perform is to request space from the heap. With this request the Operating System will locate available space and allocate this to you for use in your code. The only thing the Operating System really needs to know is how much space do you require? It can then search for a free space of that size, allocate this to you, and then give you a pointer to this newly allocated area of memory.

\begin{figure}[h]
   \centering
   \includegraphics[width=0.75\textwidth]{./topics/dynamic-memory/diagrams/AllocateMemory-Overview}
   \caption{When requesting a memory allocation you need to specify the size you want}
   \label{fig:allocate-memory-overview}
\end{figure}

\mynote{
\begin{itemize}
  \item Allocating memory is an \textbf{action} you can perform by calling appropriate Functions or Procedures offered to you by the Programming Language.
  \item Your request for memory must include an indicating of the amount of memory that you require.
  \item You also need to have a pointer that will refer to the memory you are allocated.
  \item It is possible that your request will be denied, this occurs when the computer has run out of memory to allocate.
\end{itemize}
}

\clearpage
\subsubsection{Explicitly allocating memory for a single value} % (fold)
\label{ssub:explicitly_allocating_memory}

If you want to store a single value on the heap you can ask to be allocated enough space for a single value. \fref{fig:allocate-memory} shows a Pointer (\texttt{p}) that points to an Integer value. If you want this value to be on the Heap you can ask to be allocated enough space to store an integer (4 bytes). The Operating System will then allocate you 4 bytes of space from the Heap, and give you the address of this space.

\begin{figure}[h]
   \centering
   \includegraphics[width=0.85\textwidth]{./topics/dynamic-memory/diagrams/AllocateMemory}
   \caption{You can ask to be allocated enough space to store one value}
   \label{fig:allocate-memory}
\end{figure}

\csection{
In C the \texttt{malloc} function from \texttt{stdlib.h} is used to allocate memory. With \texttt{malloc} you must specify the size of the memory you require in bytes. The \texttt{sizeof} function can be used to give you the size of the value you require. For the above example you would perform the following, with \texttt{sizeof(int)} giving you the number of bytes needed to store an \texttt{int} value: \csnipet{p = (int*) malloc(sizeof(int));}
}
\passection{
In Pascal the \texttt{New} procedure is used to allocate memory to a pointer. The \texttt{New} procedure is passed the pointer to allocate, and Pascal uses the information about what it points to, to determine how much space to allocate. For the above example you would perform the following, with \texttt{New} working out that you need to be allocated space for an Integer (the type of value that \texttt{p} points to): \passnipet{New(p);}
}

% subsubsection explicitly_allocating_memory (end)
\clearpage
\subsubsection{Explicitly allocating memory for an array} % (fold)
\label{ssub:explicitly_allocating_memory_for_an_array}

Storing single values on the Heap can be useful, but often you want to be able to allocate enough space for a number of values. Arrays on the Stack must be of a fixed length, so this dynamic allocation allows you to have \textbf{variable length arrays}.

\begin{figure}[h]
   \centering
   \includegraphics[width=0.75\textwidth]{./topics/dynamic-memory/diagrams/AllocateMemory-Array}
   \caption{You can ask to be allocated a number of values}
   \label{fig:allocate-memory-array}
\end{figure}

\csection{In C you can use \texttt{malloc} to allocate space for a number of element. Alternatively the \texttt{calloc} function from \texttt{stdlib.h} provides direct support for allocating space for an array. This function takes two parameter, the first takes the number of elements to allocation, the second the size of those elements. \texttt{calloc} also \emph{clears} its allocation, setting each byte allocated to 0. The following code allocates 4 integer values as shown in \fref{fig:allocate-memory-array}: \csnipet{p = (int*) malloc(4 * sizeof(int));} or using \texttt{calloc}, which would set all values to 0, you could use \csnipet{p = (int*) calloc(4, sizeof(int));} \newline
\newline
In C you can use the standard array access mechanisms with pointers to access subsequent elements. So \texttt{p[0]} is the value in the first element of the array dynamically allocated in \fref{fig:allocate-memory-array}, \texttt{p[1]} is the value of the second element, \texttt{p[2]} is the value of the third element, and so on. This relates back to Pointer arithmetic discussed in \sref{ssub:what_is_the_pointer_value_what_can_you_do_with_it_}.
}

\passection{Pascal has built in support for \textbf{dynamic arrays}. You can declare an array without a length, and then call \texttt{SetLength} to specify the size you want allocated. Behind the scenes Pascal will allocate the space for this array on the heap, and will take care of ensuring that it is cleaned up when you no longer have access to it. If you just want a block of memory to work with Pascal also offers a \texttt{GetMem} function.}

% subsubsection explicitly_allocating_memory_for_an_array (end)

\clearpage
\subsubsection{Changing the size of a dynamically allocated array} % (fold)
\label{ssub:changing_the_size_of_a_dynamically_allocated_array}

The advantage of dynamic memory allocation is that you can change your allocations. If you asked for an array of two values, you may later want to be able to expand that array to three or four elements. Alternatively, an array with twenty elements may have some data removed and be shrunk down to only 5 elements. All of this is possible with dynamic memory allocation. You can ask to have the memory you were allocated changed to a different size.

\begin{figure}[h]
   \centering
   \includegraphics[width=0.75\textwidth]{./topics/dynamic-memory/diagrams/AllocateMemory-Array-Resize}
   \caption{You can change the size of the allocation, growing or shrinking the number of element}
   \label{fig:allocate-memory-array-resize}
\end{figure}

\mynote{
\begin{itemize}
  \item These reallocations will keep the data that was in the array previously. Obviously if the new allocation is smaller than than existing one you will lose some values, but the others are kept.
  \item It is possible that the Operating System will need to move your new allocation, so if you change the size of an array you need to be careful if other pointers refer to elements in their old locations.
\end{itemize}
}

\csection{The \texttt{realloc} function from \texttt{stdlib.h} allows you to change the memory allocation of a pointer. The following C code performs the reallocation shown in \fref{fig:allocate-memory-array-resize}: \csnipet{p = (int*) realloc(p, 2 * sizeof(int));} }

\passection{Pascal \textbf{dynamic arrays} perform this task for you. The \texttt{SetLength} procedure allows you to change the number of elements allocated to a dynamic array as you see fit.}

% subsubsection changing_the_size_of_a_dynamically_allocated_array (end)

% subsection allocating_memory (end)