%!TEX root = Calculus_I.tex
\chapter{Using the Derivative}
This chapter will cover

\section{Using First and Second Derivatives}
Recall from Chapter 2 that:
%
\begin{enumerate}
\item If $f^\prime > 0$ $\forall x \in [a, b]$, then $f(x)$ is \textbf{\textit{monotonically increasing}} on that interval\\
\item If $f^\prime < 0$ $\forall x \in [a, b]$, then $f(x)$ is \textbf{\textit{monotonically decreasing}} on that interval\\
\item If $f^{\prime\prime} > 0$ $\forall x \in [a, b]$, then $f(x)$ is \textbf{\textit{concave up}} on that interval\\
\item If $f^{\prime\prime} < 0$ $\forall x \in [a, b]$, then $f(x)$ is \textbf{\textit{concave down}} on that interval
\end{enumerate}
%
We can combine these principles along with the general derivation formulas we discussed in Chapter 3.

\vspace{0.1in}
Often, the \textbf{\textit{local maxima}} or \textbf{\textit{local minima}} are points of interest for a given function, if they exist. Suppose $p$ is a point in the domain of $f(x)$; $p \in [a, b]$. Then:
%
\begin{enumerate}
\item $f(x)$ has a \textbf{\textit{local minimum}} at $p$ if $f(p \pm \Delta) > f(p)$.
\item $f(x)$ has a \textbf{\textit{local maximum}} at $p$ if $f(p \pm \Delta) < f(p)$.
\end{enumerate}
%
These definitions of the \textbf{\textit{maxima}} and \textbf{\textit{minima}} are \textbf{\textit{local}} because these relationships do not provide any information outside of the near vicinity of point $p$.

\vspace{0.1in}
\textbf{\textit{Local maxima}} or \textbf{\textit{local minima}}, as well as other important points of a function, are commonly referred to as \textbf{\textit{critical points}}. These points are identified by the \textbf{\textit{derivative}}. For any function, $f(x)$, \textbf{\textit{critical points}} are located at points $p$ satisfying:
%
\begin{enumerate}
\item $f^\prime(p) = 0$\\
\item $f^\prime(p)$ is undefined
\end{enumerate}
%
The \textbf{\textit{critical values}} of a function are found by evaluating $f(p)$ at each \textbf{\textit{critical point}}, $p$. Note that not every \textbf{\textit{critical point}} is a \textbf{\textit{maximum}} or \textbf{\textit{minimum}}.

\vspace{0.1in}
Suppose we wanted to find all of the \textbf{\textit{extrema}} of a function. This can be done using both $f^\prime(x)$ and $f^{\prime\prime}
(x)$. When performing the \textbf{First-Derivative Test}, assume that $p$ is a \textbf{\textit{critical point}} in the domain of $f(x)$; $p \in [a, b]$. Then, assuming $x$ is increasing:
%
\begin{enumerate}
\item If $f^\prime(x)$ changes sign from negative to positive at $p$, then $p$ is a \textbf{\textit{local minimum}} of $f(x)$.\\
\item If $f^\prime(x)$ changes sign from positive to negative at $p$, then $p$ is a \textbf{\textit{local maximum}} of $f(x)$.
\end{enumerate}
%
The \textbf{Second-Derivative Test} also provides curvature information about the function. Again, assuming that $p$ is a \textbf{\textit{critical point}} in the domain of $f(x)$; $p \in [a, b]$:
%
\begin{enumerate}
\item If $f^\prime(p) = 0$ and $f^{\prime\prime}(p) > 0$, then $f$ has a \textbf{\textit{local minimum}} at $p$.\\
\item If $f^\prime(p) = 0$ and $f^{\prime\prime}(p) < 0$, then $f$ has a \textbf{\textit{local maximum}} at $p$.\\
\item If $f^\prime(p) = 0$ and $f^{\prime\prime}(p) = 0$, then the \textbf{Second-Derivative Test} does not result in a conclusion.
\end{enumerate}
%
\textbf{\textit{Inflection Points}} are defined at places that the function, $f$, changes \textbf{\textit{concavity}}. These occur when $f^{\prime\prime} = 0$ or $f^{\prime\prime}$ is undefined.

\begin{center}
\section*{\small Examples}
Coming soon$!^{\text{TM}}$
\end{center}

\section{Optimization}
Some functions have maximum or minimum values across its domain, meaning the functions do not grow or decay to $\pm\infty$. These maximum or minimum values are referred to as \textbf{\textit{extrema}} or \textbf{\textit{optimal values}}. Practical applications of finding these \textbf{\textit{extrema}} are to minimize weight of an airplane or maximize the profit of an investment. The \textbf{Extreme Value Theorem} is used to describe when \textbf{\textit{extrema}} exist:
\vspace{0.1in}
\textbf{Extreme Value Theorem}: If $f$ is \textbf{\textit{continuous}} on the closed interval, $a \leq x \leq b$, then $f$ has a \textbf{\textit{global maximum}} and \textbf{\textit{global minimum}} on that interval.

\vspace{0.1in}
The \textbf{\textit{critical points}} of the function are first found using the \textbf{First-Derivative Test}. Then, the function values are evaluated at each of the \textbf{\textit{critical points}} and the endpoints of the domain, $a$ and $b$.

\begin{center}
\section*{\small Examples}
Coming soon$!^{\text{TM}}$
\end{center}

\section{Families of Functions}
\textbf{\textit{Families of Functions}} are defined by functions that all have similar terms. An example is the set of quadratic functions, which follow the form of:
%
\begin{equation}
f(x) = ax^2 + bx + c
\end{equation}
%
where $a$, $b$, and $c$ are constants. Different combinations of $a$, $b$, and $c$ represent unique members of the \textbf{\textit{family of functions}}. Because there are an infinite amount of constant values, there are an infinite amount of quadratic functions. Different \textbf{\textit{families of functions}} are commonly used in mathematical modeling of some system or event. Some examples of these are probabalistic theory, kinematic motion, or population density.

\begin{center}
\section*{\small Examples}
Coming soon$!^{\text{TM}}$
\end{center}

\section{Optimization, Geometry, and Modeling}
The practice of \textbf{\textit{Optimization}} can be applied to a variety of problems. One common example of this in the field of geometry is minimizing or maximizing the surface area or volume of a given shape. To do this, a mathematical model of how surface area and volume change with other parameters is required. These \textbf{\textit{optimization}} problems often have constraints, such as maximum or minimum dimensions or the shape must have have at least a certain volume.

\vspace{0.1in}
A common technique to solve \textbf{\textit{optimization}} problems is to apply the \textbf{First-Derivative Test} to find the \textbf{\textit{critical points}} of a function; where $f^\prime(x) = 0$, and then testing the possible optimal solutions for the maximum or minimum function value.

\begin{center}
\section*{\small Examples}
Coming soon$!^{\text{TM}}$
\end{center}

\section{Applications to Marginality}
In the world of business, decisions are often made based on \textbf{\textit{revenue}} or \textbf{\textit{cost}} of a project or investment. The \textbf{\textit{derivative}} can be used to maximize \textbf{\textit{profit}}, the difference between \textbf{\textit{revenue}} or \textbf{\textit{cost}}:
%
\begin{equation}
\text{Profit} = \text{Revenue} - \text{Cost}
\end{equation}
%
\textbf{\textit{Cost}} represents the total cost of producing a quantity, $q$, of some good. The more goods are made, the higher the \textbf{\textit{cost}}, so $C(q)$ is an increasing function. Many \textbf{\textit{cost functions}}, $C(q)$, have \textbf{\textit{fixed costs}}, or costs that are incurred before the first good is produced. This represents things like facility cost or hardware/software or material investments to create the good.

\vspace{0.1in}
The \textbf{\textit{Revenue Function}}, $R(q)$, represents the total value for selling a good:
%
\begin{equation}
\text{Revenue} = \text{Price} * \text{Quantity}
\end{equation}
%
Sometimes, the price, $p$, of an item can depend on the quantity, $q$, sold. Accessing the \textbf{\textit{profit}} of producing the next good, based on the current \textbf{\textit{cost}} and \textbf{\textit{revenue}} is referred to as \textbf{\textit{Marginal Analysis}}. This is performed using the definition of the \textbf{\textit{derivative}}. The \textbf{\textit{Marginal Cost}} is given by:
%
\begin{equation}
\text{MC} = \frac{C(q+1) - C(q)}{(q+1) - q}
\end{equation}
%
\begin{equation}
\nonumber
\text{MC} = C^\prime(q) \approx C(q+1) - C(q)
\end{equation}
%
Likewise, \textbf{\textit{Marginal Revenue}} is given by:
%
\begin{equation}
\text{MR} = \frac{R(q+1) - R(q)}{(q+1) - q}
\end{equation}
%
\begin{equation}
\nonumber
\text{MR} = R^\prime(q) \approx R(q+1) - R(q)
\end{equation}
%
It can be shown that the maximum \textbf{\textit{profit}} occurs when:
%
\begin{equation}
\text{MR}= \text{MC}
\end{equation}

\begin{center}
\section*{\small Examples}
Coming soon$!^{\text{TM}}$
\end{center}

\section{Rates and Related Rates}
Because \textbf{\textit{derivatives}} represent rates of change, they can be used to represent the rates of different observable and quantifiable situations. This includes ideas like change in volume of a melting snowball, the distance an object travels over time due to changes in velocity and acceleration, the fluid level of a tank as it is expelling its contents, and so on.

\begin{center}
\section*{\small Examples}
Coming soon$!^{\text{TM}}$
\end{center}

\section{L'Hopital's Rule, Growth, and Dominance}
Suppose we want to evaluate the \textbf{\textit{limit}} of a function exactly of a quantity as it approaches a point where the function is not defined, such as:
%
\begin{equation}
\lim_{x \rightarrow 0} \frac{e^{2x} - 1}{x}
\end{equation}
%
This yields $\frac{0}{0}$, which does not make mathematical sense. Though the \textbf{\textit{limit}} can be approximated by taking values near the desired evaluation point, it can be calculated exactly by applying the principle of \textbf{\textit{local linearity}}.

\vspace{0.1in}
By replacing the numerator and denominator with two functions, $f(x)$ and $g(x)$, we can approximate the limit by the ratio of the corresponding $y$-values. Taking $f^\prime(x)$ and $g^\prime(x)$ as a tangent-line approximation, we see that:
%
\begin{equation}
\frac{f(x)}{g(x)} = \frac{e^{2x} - 1}{x} \approx \frac{2x}{x} = \frac{2}{1} = \frac{f^\prime(0)}{g^\prime(0)}
\end{equation}
Thus, we arrive at:
%
\begin{equation}
\lim_{x \rightarrow 0} \frac{e^{2x} - 1}{x} = 2
\end{equation}
%
This technique is referred to as \textbf{\textit{L'Hopital's Rule}}, which is formally defined as:
%

\vspace{0.1in}
\textbf{L'Hopital's Rule}: if $f$ and $g$ are \textbf{\textit{differentiable}}, $f(a) = g(a) = 0$, and $g^\prime(a) \neq 0$, then:
%
\begin{equation}
\lim_{x \rightarrow a} \frac{f(x)}{g(x)} = \frac{f^\prime(a)}{g^\prime(a)}
\end{equation}
%
This requires that the right-hand \textbf{\textit{limit}} exists. \textbf{\textit{L'Hopital's Rule}} also applies to limits that approach $\pm \infty$.

\begin{center}
\section*{\small Examples}
Coming soon$!^{\text{TM}}$
\end{center}

\section{Parametric Equations}
\textbf{\textit{Parametric Equations}} are commonly used to represent motion in the $xy$-plane because motion is a function of time. Thus, if $x = f(t)$ and $y = g(t)$ then at time $t$, the object can be located at point $\left(f(t), g(t)\right)$. The \textit{parameter} of the \textbf{\textit{parametric equations}} for this example is the value of time, $t$.

\vspace{0.1in}
For straight-line motion, the object is located at an initial point, $\left(x_0, y_0\right)$. Provided the $x$ and $y$ rates of change (\textbf{\textit{deriatives}}) are constant, they can be represented by the quantities $a = \frac{dx}{dt}$ and $b = \frac{dy}{dt}$. Thus, at time $t$, the object has coordinates $\left(x_0 + at, y_0 + bt\right)$. The slope of the line the object is following is $m = \frac{b}{a}$.

\vspace{0.1in}
The \textbf{\textit{speed}} of this object can be quantified as well. In one unit of time, $t$, the object will move $a$ units in the $x$-direction and $b$ units in the $y$-direction; provided $a$ and $b$, are constant. Applying the Pythagorean Theorem results in:
%
\begin{equation}
\text{Speed} = \frac{Distance}{Time} = \frac{\sqrt{a^2 + b^2}}{1} = \sqrt{a^2 + b^2}
\end{equation}
%
More generally, for an object along an arbitrary curve with time-varying speed, the \textbf{\textit{instantaneous speed}} is given by:
%
\begin{equation}
v = \sqrt{\left(\frac{dx}{dt}\right)^2 + \left(\frac{dy}{dt}\right)^2}
\end{equation}
%
where $v_x = \frac{dx}{dt}$ is the \textbf{\textit{instantaneous velocity}} in the $x$-direction and $v_y = \frac{dy}{dt}$ is the \textbf{\textit{instantaneous velocity}} in the $y$-direction. Recall that \textbf{\textit{velocity}} has both magnitude \textit{and} direction. This introduces the idea of \textbf{\textit{unit vectors}}, which have an identity magnitude (1) and are always oriented in the $x$- and $y$-directions. For traditional $xy$-coordinate frames, $i$, and $j$ are used to represent motion in $x$ and $y$, respectively. Thus, the
\textbf{\textit{velocity vector}} is written as:
%
\begin{equation}
\overrightarrow{v} = v_x \overrightarrow{i} + v_y \overrightarrow{j}
\end{equation}
%

\vspace{0.1in}
For any given point $\left(x_0, y_0\right)$, a tangent line to the curve can be given parametrically by finding the straight-line motion through  $\left(x_0, y_0\right)$ with the same $x$ and $y$ velocities as an object moving along the curve.

\vspace{0.1in}
Sometimes, the curve is more interesting to observe than the object's motion through space and time. This representation is referred to as the \textit{parameterization} of the curve. This can be helpful to graph complicated curves.

\vspace{0.1in}
The slope and concavity of \textbf{\textit{parametric curves}} can be obtained via the \textbf{\textit{Chain Rule}} by thinking of the \textbf{\textit{parametric equations}} as functions of time. The slope is given by:
%
\begin{equation}
\frac{dy}{dx} = \frac{\frac{dy}{dt}}{\frac{dx}{dt}}
\end{equation}
%
The second \textbf{\textit{derivative}} can be found by a similar method. If $w$ is a \textbf{\textit{differentiable}} function of $x$, then:
%
\begin{equation}
\frac{dw}{dx} = \frac{\frac{dw}{dt}}{\frac{dx}{dt}}
\end{equation}
%
For $w = \frac{dy}{dx}$:
%
\begin{equation}
\frac{dw}{dx} = \frac{d}{dx}\left(\frac{dy}{dx}\right) = \frac{d^2y}{dx^2}
\end{equation}
%
So, by the \textbf{\textit{Chain Rule}}:
%
\begin{equation}
\frac{d^2y}{dx^2} = \frac{\frac{d}{dt}\left(\frac{dy}{dx}\right)}{\frac{dx}{dt}}
\end{equation}
%

\begin{center}
\section*{\small Examples}
Coming soon$!^{\text{TM}}$
\end{center}


As previously mentioned, radial distortion is only dependent on the distance between the origin and the point being considered.
Because of this, it can be thought of as a transformation which, according to some function $f$, moves the point to a different location on the line intersecting it and the origin.
More specifically, for a point $(r_u, \theta_u)$ in polar coordinates, $r_d = f(r_u), \theta_d = \theta_u$ is a radial distortion where $(r_d, \theta_d)$ represents the distorted polar coordinates of the point.

Brown's model for lens distortion~\cite{brown1966decentering} builds upon that presented by Conrady~\cite{conrady1919lens}, presenting a mathematical representation of barrel/pincushion and tangential distortion.
The rest of this report will consider a simplified version of this model in which there is no tangential distortion and is evaluated up to $r^5$ (and $r^8$ for its inverse).
This simplified version of Brown's radial distortion model is shown in Eq.~\ref{eq:brown-forward-radial-distortion}.
The approximation of its inverse is shown in Eq.~\ref{eq:brown-inverse-radial-distortion}.

\begin{equation}
    \label{eq:brown-forward-radial-distortion}
    r_d = f(r_u) = r_u + c_1 r_u^3 + c_2 r_u^5
\end{equation}

\begin{equation}
    \label{eq:brown-inverse-radial-distortion}
    r_u = f^{-1}(r_d) \approx \frac{c_1 r_d^2 + c_2 r_d^4 + c_1^2 r_d^4 + c_2^2 r_d^8 + 2 c_1 c_2 r_d^6}{1 + 4 c_1 r_d^2 + 6 c_2 r_d^4}
\end{equation}

Where $c_1$ and $c_2$ are per-lens constants that determine the extremity of the distortion.
These are usually calculated experimentally, however, for the static renders in this paper, they will be set to arbitrary values of $c_1 = 0.15$ and $c_2 = 0.10$.
While these are relatively extreme, they clearly exemplify the concepts being demonstrated.
Its worth noting that, due to the approximate nature of the inverse distortion function, the greater values of $c_1$ and $c_2$, the more $f^{-1}(f(r))$, $f(f^{-1}(r))$ and $r$ diverge.

For practical implementations, computers represent points using cartesian coordinates.
Because of this, any transformations first need to convert the coordinates to polar form.
Given a two dimensional cartesian point $p = (x, y)$, its polar form is $\left(r = \sqrt{x^2 + y^2} = \sqrt{p \cdot p}, \theta = \arctantwo{(y, x)}\right)$.
Given a two dimensional polar point $(r, \theta)$, its cartesian form is $\left(x = r \cos{\theta}, y = r \sin{\theta}\right)$.

By using the fact that only $r$ is varied during a radial distortion, an optimisation can be used whereby only $r$, or the required powers of $r$, are calculated, and the cartesian coordinates are simply scaled by that applied during the distortion (removing the need to calculate $\theta$, $\sin{\theta}$, and $\cos{\theta}$).
Applying this to Brown's model, for a point $(x_u, y_u)$,~\cite{mallon2004precise} calculates its distorted point $(x_d, y_d)$ as shown in Eq.~\ref{eq:inverse-radial-distortion}.

\begin{equation}
    \label{eq:inverse-radial-distortion}
    \binom{x_u}{y_u} = \binom{x_d}{y_d} \left(1 - f^{-1}\left(\sqrt{x_d^2 + y_d^2}\right)\right),
\end{equation}

All distortions are applied on top of the scene shown in Fig.~\ref{fig:no-distortion}.
Any intermediary render textures are $1024 \times 1024$ pixels and the output resolution is $500 \times 500$ pixels unless otherwise specified.

\begin{figure}[ht]
    \centering
    \includegraphics[width=0.4\textwidth]{figures/no-distortion}
    \caption{Untouched and undistorted 9-cube scene.}
    \label{fig:no-distortion}
\end{figure}

\subsection{Pixel-based Distortion Correction}\label{sec:pixel-based}

The pixel-based distortion correction method works by transforming UV positions in the texture map for each pixel (or fragment).
In a normal fragment shader, the graphics card provides texture UV positions (between $(0, 0)$ and $(1, 1)$) interpolated between those specified in the vertex shader.
A \texttt{sampler2D} is then used to retrieve the colour of the pixel in the texture corresponding to the UV position.
In order to perform distortion, the UV position being sampled can be changed.

Firstly, both the forward and inverse radial distortions functions require points to be normalised to between $(-1, -1)$ and $(1, 1)$.
For input UV position $p_{\texttt{UV}}$, this is achieved simply using $p_{\texttt{norm}} = 2 p_{\texttt{UV}} - 1$.
After the distortion transformation is completed, the normalised points are converted back to UV positions using $p_{\texttt{UV}}' = 0.5 (p_{\texttt{norm}}' + 1)$.

Somewhat non-trivially, to perform a radial barrel or pincushion distortion, the opposite equation must be used (Eq.~\ref{eq:brown-forward-radial-distortion} for barrel and Eq.~\ref{eq:brown-inverse-radial-distortion} for pincushion).
This is because sampling a different UV value to the current one is already the inverse operation.
Instead of imagining the current UV point $A$ being transformed to its distorted UV point $B$, its better to think of it as finding the UV point that should replace $A$, and calculating this to be $B$.
Hence, for barrel distortion, considering the point $A$, it needs to be replaced by a point $B$ further away from the origin (effectively distorting by compression).
In this case, $B$ is found by inverting the distortion that lands on $A$, therefore using Eq.~\ref{eq:brown-forward-radial-distortion} (and vice versa for pincushion distortion).

The fragment shader implementation simply performs this operation for each pixel/fragment in the image, and discards any fragments who's distorted UV positions lie outside the bounds of the texture.
The results of this and simulation of it running through a lens are shown in~\ref{fig:frag-based}.

\begin{figure}[ht]
    \centering

    \subfloat[Pre-distortion]{\includegraphics[width=0.225\textwidth]{figures/pixel-based}}
    \hfil
    \subfloat[Correction and distortion]{\includegraphics[width=0.225\textwidth]{figures/pixel-based-inverse}}

    \caption{Pixel-based pre-distortion and lens simulation.}
    \label{fig:frag-based}
\end{figure}

\subsection{Mesh-based Distortion Correction}\label{sec:mesh-based}

The mesh-based distortion correction method works by transforming the vertex positions of a mesh (instead of transforming texture UV positions).
In doing so, it looks to exploit the graphics rendering pipeline for a performance gain with minimal residual artifacts.

The meshes were produced in Blender by continually subdividing a plane (the $8 \times 8$ mesh is shown in Fig.~\ref{fig:blender-mesh8}).
An $n \times n$ mesh has $n^2$ quads, $2n^2$ triangles, or $(n + 1)^2 = n^2 + 2n + 1$ vertices.
As each face is subdivided into four subfaces, $n$ is a power of $2$.

\begin{figure}[ht]
    \centering
    \includegraphics[width=0.4\textwidth]{figures/blender-mesh8}
    \caption{$8 \times 8$ mesh in Blender.}
    \label{fig:blender-mesh8}
\end{figure}

In the vertex shader, vertices are first transformed from world-space to clip-space using its model-view-projection matrix.
Since clip-space is between $(-1, -1)$ and $(1, 1)$, it matches the domain of both Eq.~\ref{eq:brown-forward-radial-distortion} and Eq.~\ref{eq:brown-inverse-radial-distortion}.
This means that the respective radial transforms can be applied to the vertices in clip-space to distort their positions.

Opposite to the pixel-based method, applying pre-distortion correction to the output requires the inverse radial transform (Eq.~\ref{eq:brown-inverse-radial-distortion}), and simulating the distortion requires the forward radial transform (Eq.~\ref{eq:brown-forward-radial-distortion}).
This works by translating the vertices (as if they were distorted) to their pre-distorted positions.
As part of the rendering pipeline, during the fragment shader, each of these pre-distorted vertex positions are linearly interpolated by the graphics card to produce estimates for pre-distorted fragments/pixels.

In order to simulate a lens being applied to the pre-distorted image, as mentioned, the same technique is applied using the forward radial transform (Eq.~\ref{eq:brown-forward-radial-distortion}).
This then cancels out the pre-distortion to produce an image similar to that which was input ($f^{-1}(f(r)) = r$).

As an aside, similar results could be achieved by performing the same texture UV transformation in the vertex shader as that used by the pixel-based method in the fragment shader.
While this would cause the same distortion, as vertices remain on the edge, a mesh image of original size would be output, causing redundant additional distortion and calculations near the edge (instead of the black background).

Fig.~\ref{fig:mesh-based-distortion} shows the mesh-based method applied to three meshes ($8 \times 8$, $128 \times 128$, and $512 \times 512$) with correction only ($a$, $c$, and $e$) and correction with distortion ($b$, $d$, and $f$).
The main visible difference is between $8 \times 8$ and $128 \times 128$, where clear straight lines blend into curved lines.
There is also a clear difference in correction and distortion applied together, with the $8 \times 8$ mesh exhibiting clear artifacts towards the corners.
Between $128 \times 128$ and $512 \times 512$ there is no perceivable difference in output, implying that at this level the difference is sub-pixel.
This makes logical sense since the number of quads (and by extension vertices) exceed the number of pixels in the image.

\begin{figure}[ht]
    \centering

    \subfloat[$8 \times 8$ mesh, corrected]{\includegraphics[width=0.225\textwidth]{figures/mesh-based-correction8}}
    \hfil
    \subfloat[$8 \times 8$ mesh, corrected and distorted]{\includegraphics[width=0.225\textwidth]{figures/mesh-based-distortion8}}

    \subfloat[$128 \times 128$ mesh, corrected]{\includegraphics[width=0.225\textwidth]{figures/mesh-based-correction128}}
    \hfil
    \subfloat[$128 \times 128$ mesh, corrected and distorted]{\includegraphics[width=0.225\textwidth]{figures/mesh-based-distortion128}}

    \subfloat[$512 \times 512$ mesh, corrected]{\includegraphics[width=0.225\textwidth]{figures/mesh-based-correction512}}
    \hfil
    \subfloat[$512 \times 512$ mesh, corrected and distorted]{\includegraphics[width=0.225\textwidth]{figures/mesh-based-distortion512}}

    \caption{LCA pre-distortion and corrected LCA with distortion.}
    \label{fig:mesh-based-distortion}
\end{figure}

For benchmarking and profiling, on an i7 6700K with an RTX 2080 at $500 \times 500$ pixels, all three meshes performed the same (about 1.7ms per frame).
However, increasing the resolution to $8192 \times 8192$ yields more reliable variation on the frame rate and a clear performance drop as mesh complexity increases.
The profiling results are shown in Tab.~\ref{tab:benchmarking}.
These results, combined with the minimal visual difference between the more detailed meshes, show that a low-poly mesh is a good trade-off between frame rate and visual quality.

\begin{table}[ht]
    \centering
    \caption{Profiling results for three meshes at $8192 \times 8192$ resolution.}
    \label{tab:benchmarking}
    \begin{tabular}{c|cccc}
    \textbf{Mesh}     & \textbf{FPS} & \textbf{CPU (ms)} & \textbf{GPU (ms)} & \textbf{Total (ms)} \\ \hline
    $8 \times 8$      & $\sim135$    & $7.3$             & $0.3$           & $7.3$                 \\
    $128 \times 128$  & $\sim128$    & $7.8$             & $0.3$           & $7.9$                 \\
    $512 \times 512$  & $\sim104$    & $9.4$             & $0.3$           & $9.5$
    \end{tabular}
\end{table}

\subsection{Pixel-based vs Mesh-based Distortion Correction}\label{sec:pixel-vs-mesh-based}

Both techniques have advantages and drawbacks.

Firstly, pixel-based pre-distortion in the fragment shader involves calculating Eq.~\ref{eq:brown-forward-radial-distortion} for each fragment/pixel in the scene, including those which are eventually discarded.
On the other hand, mesh-based pre-distortion in the vertex shader involves calculating Eq.~\ref{eq:brown-inverse-radial-distortion} for each vertex in the mesh.
This means that, for a mesh with far less vertices than pixels being rendered to the screen, the mesh-based method performs far less direct distortion calculations than the pixel-based method, giving it better theoretical performance.

Secondly, while the pixel-based method produces the most accurate theoretical discrete output, in practice, the mesh-based method is able to produce indistinguishable results for a low number of vertices relative to pixels/fragments.

From an implementation efficiency standpoint, one drawback of the mesh-based method is that using Eq.\ref{eq:brown-inverse-radial-distortion} means it needs to perform more operations per vertex calculation than the pixel-based method does per pixel.
It does however have the added benefit that the input coordinates are already normalised to between $-1$ and $1$.
Additionally, since it only uses even powers of $r$, $r^2$ can be trivially calculated using $r^2 = x^2 + y^2 = p \cdot p$, removing the need for an expensive square root.

For the frame buffer resolution of $1024 \times 1024$ pixels used in the render targets, the fragment shader in the pixel-based method should perform $1024^2 = 1048576$ distortion calculations and up to $1048576$ texture lookups per frame (dependent on the number of discarded fragments).
On the other hand, the mesh-based method only performs $(n + 1)^2$ distortion calculations for an $n \times n$-quad mesh ($81$ calculations for $8 \times 8$, $16384$ for $128 \times 128$, and $262144$ for $512 \times 512$), but will still require up to $1048576$ texture lookups as the fragment shader samples the texture for each fragment in the buffer.

Finally, while not used in this report, the mesh-based method can be further refined by pre-baking its distorted vertex positions, removing the need to do any distortion calculations at runtime.


\subsection{Sample range}

\subsubsection{Minimum}

This is the smallest value in the sample.

\subsubsection{Maximum}

This is the largest value in the sample.

\subsubsection{Range}

This is the difference between the maximum and minimum.

\subsubsection{Median}

This is the value whereby 50\% of the sample can be found below the value.

\subsubsection{Percentiles}

The \(x\)th percentile is the value by which  \(x\%\) of the values can be found below it.

\subsubsection{Interquartile range}

This is the differnence between the \(25\)th percentile and the \(75\)th percentile.


\lab{The Finite Element Method in Two Dimensions}{The Finite Element Method in Two Dimensions}
\label{lab:finite_element_2d}

In Lab \ref{lab:finite_element} we discussed some of the basic details of how the Finite Element Method works.
We demonstrated how a grid can be refined around areas of interest to give a more accurate approximation to a desired solution.
One of the other great strengths of the finite element method is that, when it is used in two or more dimensions, it is easily applied to unusually shaped domains.
There are a wide variety of elements that are commonly used, but the simplest elements are triangles.
Triangles are often used because they allow us to define continuous piecewise linear basis functions on their interiors without much trouble.

\section*{Working With Triangle Meshes}
There are a variety of ways to store triangle meshes.
We will present a simple one here.
To store a mesh we need to store several pieces of information.
We need to store the points used in the mesh.
We also need to store which points are connected to make the triangles that are part of the mesh we are studying.
Later, when working with boundary conditions, we will also need to know which nodes have fixed values (or some other sort of boundary condition, as the case may be).

It is common to store this information in arrays, one containing the $x$ and $y$ coordinates of each node in each of its rows and another containing the indices of the nodes that form the vertices of each triangle.

The following is a short example that divides the unit square up into triangles and returns arrays of the desired form.

\begin{lstlisting}
import numpy as np

def triangles(n):
    ''' Generate the indices of the triangles for a triangular mesh
    on a square grid of points.
    'n' is expected to be the number of nodes on each edge. '''
    # Make the indices for a single row.
    row = np.empty((2 * (n - 1), 3), dtype=np.int32)
    row[::2,0] = row[1::2,0] = row[::2,1] = np.arange(n-1)
    row[1::2,0] += 1
    row[::2,1] += n
    row[1::2,1] = row[::2,1]
    row[::2,2] = row[1::2,2] = row[1::2,0]
    row[1::2,2] += n
    # Now use broadcasting to make the indices for the square.
    return (row + np.arange(0, n * (n-1), n)[:,None,None]).reshape((-1,3))
\end{lstlisting}

Matplotlib's \li{triplot} function can be used to plot triangulations.
To plot the mesh generated by the above function, we can do the following:
\begin{lstlisting}
from matplotlib import pyplot as plt
n=5
x = np.linspace(0, 1, n)
x, y = map(np.ravel, np.meshgrid(x, x))
t = triangles(n)
plt.triplot(x, y, t, color='b')
plt.scatter(x, y, color='b')
plt.show()
\end{lstlisting}
This mesh is shown in figure \ref{fig:fem2d_square_triangulation}.

\begin{figure}
\includegraphics[width=\textwidth]{square_triangulation.pdf}
\caption{A triangulation of a square grid.}
\label{fig:fem2d_square_triangulation}
\end{figure}

Matplotlib and Mayavi both include the functionality to plot 3d surfaces based on a triangulation and the values of a function at the nodes of the triangulation.
For example, we can plot a piecewise linear function that is one at a single vertex and zero at every other vertex using Mayavi using the \li{triangular_mesh} function included in \li{mlab}.
\begin{lstlisting}
from mayavi import mlab as ml
n=5
x = np.linspace(0, 1, n)
x, y = np.meshgrid(x, x)
t = triangle_mesh(n)
vals = np.zeros(x.size)
vals[n**2 // 2] = 1
ml.triangular_mesh(x.ravel(), y.ravel(), vals, t)
ml.show()
\end{lstlisting}
The output from this code is shown in Figure \ref{fig:fem2d_basis_functions}.
These functions are often called "hat functions," and are often used as the basis functions for 2d finite element analysis on domains that can be represented as meshes of triangles.
These are the two dimensional analogues of the piecewise linear functions shown in Figure \ref{fig:FEM_one_basis_function}.
You may recall that, in the one-dimensional case, we could represent any function that was continuous and linear over each element as a sum of these basis functions.
The same is true in this case.
We can represent any continuous function that is linear on each triangle in the triangulation as a sum of these basis functions.
Strictly speaking, if $f$ is a continuous function that is linear on each of the triangles in the triangulation, $p_i$ are the vertices of the trianglulation, and $\phi_i$ are the basis functions corresponding to each vertex, we may say
\[f(x) = \sum_{i} f(p_i) \phi_i(x, y)\]

\begin{figure}
\includegraphics[width=\textwidth]{hat_function.png}
\caption{A hat function in two dimensions. Compare with the one dimensional hat function shown in Figure \ref{fig:FEM_one_basis_function}.}
\label{fig:fem2d_basis_functions}
\end{figure}

We can plot funcitons like this on triangulations using matplotlib as well.
The following code will plot the same basis function in matplotlib
\begin{lstlisting}
from mpl_toolkits.mplot3d import Axes3D
n=6
x = np.linspace(0, 1, n)
x, y = map(np.ravel, np.meshgrid(x, x))
t = triangles(n)
vals = np.random.rand(x.size)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection='3d')
ax.plot_trisurf(x, y, vals, triangles=t)
plt.show()
\end{lstlisting}

\section*{Using Triangulations for Finite Element Analysis}

As was mentioned before, hat functions on triangulations are often used in Finite Element analysis.
When using the Finite element method, regardless of the number of dimensions, it is necessary to transform a problem to its weak formulation.
This allows the approximation of the action of a PDE on a domain via the computation of integrals.
To begin, we will consider the differential operator
\[-\nabla \cdot \left(a\left(x\right) \nabla u\left(x\right)\right) + b\left(x\right) \cdot \nabla u\left(x\right) + c\left(x\right) u\left(x\right) = d\left(x\right)\]
on a triangulated domain $\Omega$.
For simplicity, we will first consider the case where $u$ is assumed to have Neumann boundary conditions.
The weak formulation of this problem on a function space $V$ is to find a function $u$ such that $a\left(u, v\right) = l\left(v\right)$ for all $v \in V$ with $a$ and $l$ defined as the following integral operators.
\begin{align*}
a\left(u, v\right) &= \int_\Omega \left( a\left(x\right) \nabla u\left(x\right) \cdot \nabla v\left(x\right) + \left(b\left(x\right) \cdot \nabla u\left(x\right)\right) v\left(x\right) + c\left(x\right) u\left(x\right) v\left(x\right) \right) dx\\
l\left(v\right) &= \int_\Omega d\left(x\right) v\left(x\right) dx
\end{align*}

We will find an approximate solution to the PDE that lies in the space $V$ of functions that are continuous and linear on each of the triangles in the triangulation of $\Omega$.
Let $\phi_i$ be the hat function centered at the $i$'th vertex of the triangulation of $\Omega$.
The $\phi_i$ are a basis for $V$, so we may say that we seek coefficients $u_i$ such that for any set of coefficients $v_i$,
\[a\left(\sum_i u_i \phi_i , \sum_j v_j \phi_j \right) = l\left(\sum_j v_j \phi_j\right)\]
Since $a$ is linear in its second term and $l$ is linear, this is equivalent to finding coefficients $u_i$ such that
\[a\left(\sum_i u_i \phi_i , \phi_j \right) = l\left(\phi_j\right)\]
Since $a$ is also linear in its first term, this is equivalent to finding $u_i$ such that
\[\sum_i u_i a\left(\phi_i, \phi_j\right) = l\left(\phi_j\right)\]
This problem can be represented as a linear system $A u = \Phi$ where $\Phi_j = l\left(\phi_j\right)$ and $A_{j, i} = a\left(\phi_i, \phi_j\right)$.
Generally speaking, this problem can be solved by construct the matrix $A$, and the vector $\Phi$, and then solving the resulting system.

\begin{info}
The matrix $A$ in the linear system constructed here is often referred to as the ``stiffness matrix."
The vector $\Phi$ is commonly known as the ``load vector."
\end{info}

\begin{info}
Notice that $a\left(\phi_i, \phi_j\right)$ depends entirely on the area where $\phi_i$ and $\phi_j$ are both nonzero.
If the supports of the functions $\phi_i$ and $\phi_j$ do not overlap, $a\left(\phi_i, \phi_j\right) = 0$.
Since only neighboring hat functions yield nonzero terms in the sum, the matrix $A$ is usually sparse.
\end{info}

\subsection*{Constructing the Stiffness Matrix and Load Vector}
It is not always easiest to construct the stiffness matrix and load vector by considering the basis functions one at a time.


% !TEX root = ../main.tex

\section{Abstract data types}

To model a model we need to be able to model the data too. Similar to functional programming.

\subsection{Goal}
\begin{itemize}
  \item Describe abstract data types (ADTs)
  \item Be able to use ADTs in mCRL2
\end{itemize}

\subsection{Declaration of data type}

\textbf{sort} D;

It represents a non-empty domain. $D$ can have infinite elements.

The most basic data type are the booleans.

\textbf{sort} $\mathbb{B}$;

\textbf{cons} \emph{true}, \emph{false}: $\mathbb{B}$

Special semantic rule: The elements representing \emph{true} and \emph{false} of sort $\mathbb{B}$
(\emph{Bool}) must be different.

\begin{align*}
  \text{\textbf{sort} } & \mathbb{B}; \\
  \text{\textbf{cons} } & true, false: \mathbb{B}; \\
  \text{\textbf{map} } & not: \mathbb{B} \Rightarrow \mathbb{B}; \\
  & and, or: \mathbb{B} \times \mathbb{B} \Rightarrow \mathbb{B}; \\
  \text{\textbf{var} } & b: \mathbb{B} \\
  \text{\textbf{eqn} } & not(true) = false; \\
  & not(false) = true; \\
  & not(not(true)) = true; \\
  \text{\textbf{eqn} } & and(true, b) = b; \\
  & and(false, b) = false; \\
  & and(b, true) = b; \\
  & and(b, false) = false; \\
  \text{\textbf{eqn}} & or(true, b) = true; \\
  & or(false, b) = b; \\
  & or(b, true) = true; \\
  & or(b, false) = b; \\
\end{align*}

\subsubsection{Induction/case distinction on Bool}
Can we prove $and(b, b) = b$ ?

As true and false are the constructors of $\mathbb{B}$ we only have to prove that
$and(true, true) = true$ and that $and(false, false) = false$.

Prove that $and(b, c) = and(c, b)$:

\begin{align*}
  \text{case } & b = true \\
  & and(true, c) = c \\
  & \text{case } c = true \\
  & & and(true, true) = and(true, true) \\
  & \text{case } c = false \\
  & & \underbrace{and(true, false)}_{false} = \underbrace{and(false, true)}_{false} \\
\end{align*}

\subsection{Specification numbers. Peano arithmetic}

\begin{align*}
  \text{\textbf{sort} } & Nat; \\
  \text{\textbf{cons} } & zero: Nat; \\
  & succ: Nat \rightarrow Nat; \\
  \text{\textbf{map }} & add: Nat \times Nat \rightarrow; \\
  \text{\textbf{var }} & n, m: Nat; \\
  \text{\textbf{eqn }} & add(zero, zero) = zero; \\
  & add(zero, succ(n)) = succ(n); \\
  & add(succ(n), zero) = succ(n); \\
  & add(succ(n), succ(m)) = succ(add(n, succ(m))); \\
  \text{\textbf{map }} & <: Nat \times Nat \rightarrow \mathbb{B}; \\
  \text{\textbf{eqn }} & n < zero = false; \\
  & zero < succ(n) = true; \\
  & succ(n) < succ(m) = n < m;
\end{align*}

\begin{align*}
  & zero & 0 \\
  & succ(zero) & 1 \\
  & succ(succ(zero)) & 2 \\
\end{align*}

\subsubsection{Induction on Natural numbers}
Prove a property of \emph{zero}

Prove a property for $succ(n)$, assuming that it has been proven for $n$.

Then the property has been prover for all natural numbers $n$.

\subsection{Specification of efficient natural numbers}

Peano arithmetic is too inefficient for system validation.

\begin{align*}
  \text{sort } & \mathbb{N}^+; \qquad \text{sort } Pos;
\end{align*}

No maximum number

\subsection{Natural numbers}

\begin{align*}
  \text{sort } & \mathbb{N}; \qquad \text{sort} Nat; \\
  \text{cons } & @c0: \mathbb{N}; \\
  & @cNat: \mathbb{N}^+ \rightarrow \mathbb{N}; \\
  \text{map } & \simeq: \mathbb{N} \times \mathbb{N} \rightarrow \mathbb{N}^+
\end{align*}

\subsection{Type conversion}

All the type conversions are automatically run into the tools.

Sometimes you'll have to explicitly cast some types, specially when downcasting

\subsection{Data type for lists}

You can have the empty list or the list with something appended in the front


\chapter{Some topological constructions}
In this short chapter we briefly describe some common spaces and constructions
in topology that we haven't yet discussed.

\section{Spheres}
Recall that
\[ S^n = \left\{ (x_0, \dots, x_n)
	\mid x_0^2 + \dots + x_n^2 = 1 \right\} \subset \RR^{n+1} \]
is the surface of an $n$-sphere while
\[ D^{n+1} = \left\{ (x_0, \dots, x_n)
	\mid x_0^2 + \dots + x_n^2 \le 1 \right\} \subset \RR^{n+1} \]
is the corresponding \emph{closed ball}
(So for example, $D^2$ is a disk in a plane while $S^1$ is the unit circle.)
\begin{exercise}
	Show that the open ball $D^n \setminus S^{n-1}$
	is homeomorphic to $\RR^n$.
\end{exercise}

In particular, $S^0$ consists of two points,
while $D^1$ can be thought of as the interval $[-1,1]$.

\begin{center}
	\begin{asy}
		size(8cm);
		draw(dir(0)--dir(180), blue);
		dot(dir(0), red+4);
		dot(dir(180), red+4);
		label("$S^0$", dir(0), dir(90), red);
		label("$D^1$", dir(0)--dir(180), blue);
		add(shift(-4,0)*CC());
		unitsize(2cm);
		filldraw(unitcircle, lightblue+opacity(0.2), red);
		label("$D^2$", origin, blue);
		label("$S^1$", dir(45), dir(45), red);
	\end{asy}
\end{center}

\section{Quotient topology}
\prototype{$D^n / S^{n-1} = S^n$, or the torus.}

Given a space $X$, we can \emph{identify} some of the points together
by any equivalence relation $\sim$;
for an $x \in X$ we denote its equivalence class by $[x]$.
Geometrically, this is the space achieved by welding together points
equivalent under $\sim$.

Formally,
\begin{definition}
	Let $X$ be a topological space, and $\sim$ an equivalence relation
	on the points of $X$.
	Then $X / {\sim}$ is the space whose
	\begin{itemize}
		\ii Points are equivalence classes of $X$, and
		\ii $U \subseteq X / {\sim}$ is open if and only if
		$\left\{ x \in X \text{ such that } [x] \in U  \right\}$
		is open in $X$.
	\end{itemize}
\end{definition}
As far as I can tell, this definition is mostly useless for intuition,
so here are some examples.

\begin{example}[Interval modulo endpoints]
	Suppose we take $D^1 = [-1, 1]$
	and quotient by the equivalence relation which identifies
	the endpoints $-1$ and $1$.
	(Formally, $x \sim y \iff (x=y) \text{ or } \{x,y\} = \{-1,1\}$.)
	In that case, we simply recover $S^1$:
	\begin{center}
		\begin{asy}
			size(8cm);
			draw(dir(0)--dir(180), blue);
			dot("$-1$", dir(0), dir(90), red+4);
			dot("$-1$", dir(180), dir(90), red+4);
			label("$D^1$", dir(0)--dir(180), blue);
			add(shift(-4,0)*CC());
			unitsize(2cm);
			draw(unitcircle, blue);
			label("$S^1 \approx D^1 / {\sim}$", dir(45), dir(45), blue);
			dot("$-1 \sim 1$", dir(90), dir(90), red);
		\end{asy}
	\end{center}
	Observe that a small open neighborhood around $-1 \sim 1$ in the quotient space
	corresponds to two half-intervals at $-1$ and $1$ in the original space $D^1$.
	This should convince you the definition we gave is the right one.
\end{example}

\begin{example}[More quotient spaces]
	Convince yourself that:
	\begin{itemize}
		\ii Generalizing the previous example, $D^n$ modulo its boundary $S^{n-1}$ is $S^n$.
		\ii Given a square $ABCD$, suppose we identify segments $AB$ and $DC$ together.
		Then we get a cylinder. (Think elementary school, when you would tape
		up pieces of paper together to get cylinders.)
		\ii In the previous example, if we also identify $BC$ and $DA$ together,
		then we get a torus. (Imagine taking our cylinder and putting the two
		circles at the end together.)
		\ii Let $X = \RR$, and let $x \sim y$ if $y -x \in \ZZ$.
		Then $X / {\sim}$ is $S^1$ as well.
	\end{itemize}
\end{example}

One special case that we did above:
\begin{definition}
	Let $A \subseteq X$.
	Consider the equivalence relation which identifies
	all the points of $A$ with each other
	while leaving all remaining points inequivalent.
	(In other words, $x \sim y$ if $x=y$ or $x,y \in A$.)
	Then the resulting quotient space is denoted $X/A$.
\end{definition}

So in this notation, \[ D^n / S^{n-1} = S^n. \]

\begin{abuse}
	Note that I'm deliberately being sloppy, and saying
	``$D^n / S^{n-1} = S^n$'' or ``$D^n / S^{n-1}$ \emph{is} $S^n$'',
	when I really ought to say ``$D^n / S^{n-1}$ is homeomorphic to $S^n$''.
	This is a general theme in mathematics:
	objects which are homoeomorphic/isomorphic/etc.\ are generally
	not carefully distinguished from each other.
\end{abuse}

\section{Product topology}
\prototype{$\RR \times \RR$ is $\RR^2$, $S^1 \times S^1$ is the torus.}

\begin{definition}
	Given topological spaces $X$ and $Y$,
	the \vocab{product topology} on $X \times Y$ is the space whose
	\begin{itemize}
		\ii Points are pairs $(x,y)$ with $x \in X$, $y \in Y$, and
		\ii Topology is given as follows: the \emph{basis} of
		the topology for $X \times Y$ is $U \times V$,
		for $U \subseteq X$ open and $V \subseteq Y$ open.
	\end{itemize}
\end{definition}

\begin{remark}
	It is not hard to show that, in fact,
	one need only consider basis elements for $U$ and $V$.
	That is to say,
	\[ \left\{ U \times V \mid
		U,V \text{ basis elements for } X,Y \right\} \]
	is also a basis for $X \times Y$.

	We really do need to fiddle with the basis:
	in $\RR \times \RR$, an open unit disk better be open,
	despite not being of the form $U \times V$.
\end{remark}

This does exactly what you think it would.
\begin{example}[The unit square]
	Let $X = [0,1]$ and consider $X \times X$.
	We of course expect this to be the unit square.
	Pictured below is an open set of $X \times X$ in the basis.
	\begin{center}
		\begin{asy}
		size(6cm);
		filldraw(unitsquare, opacity(0.2)+lightblue, black);

		pair B = (0,1);
		pair A = (1,0);
		fill(box(0.3*A+0.2*B,0.6*A+0.7*B), lightred+opacity(0.5));
		label("$U \times V$", (0.45,0.45), brown);

		draw(0.3*A--(0.3*A+B), heavygreen+dashed+1);
		draw(0.6*A--(0.6*A+B), heavygreen+dashed+1);
		draw(0.2*B--(0.2*B+A), heavycyan+dashed+1);
		draw(0.7*B--(0.7*B+A), heavycyan+dashed+1);

		draw( 0.3*A--0.6*A, heavygreen+2 );
		opendot( 0.3*A,  heavygreen+2);
		opendot( 0.6*A, heavygreen+2);
		label("$U$", 0.45*A, dir(-90), heavygreen);
		draw( 0.2*B--0.7*B, heavycyan+2 );
		opendot( 0.2*B, heavycyan+2);
		opendot( 0.7*B, heavycyan+2);
		label("$V$", 0.45*B, dir(180), heavycyan);
		\end{asy}
	\end{center}
\end{example}
\begin{exercise}
	Convince yourself this basis gives the same topology
	as the product metric on $X \times X$.
	So this is the ``right'' definition.
\end{exercise}

\begin{example}[More product spaces]
	\listhack
	\begin{enumerate}[(a)]
		\ii $\RR \times \RR$ is the Euclidean plane.
		\ii $S^1 \times [0,1]$ is a cylinder.
		\ii $S^1 \times S^1$ is a torus! (Why?)
	\end{enumerate}
\end{example}

\section{Disjoint union and wedge sum}
\prototype{$S^1 \vee S^1$ is the figure eight.}

The disjoint union of two spaces is geometrically exactly
what it sounds like: you just imagine the two spaces side by side.
For completeness, here is the formal definition.
\begin{definition}
	Let $X$ and $Y$ be two topological spaces.
	The \vocab{disjoint union}, denoted $X \amalg Y$, is defined by
	\begin{itemize}
		\ii The points are the disjoint union $X \amalg Y$, and
		\ii A subset $U \subseteq X \amalg Y$ is open if
		and only if $U \cap X$ and $U \cap Y$ are open.
	\end{itemize}
\end{definition}
\begin{exercise}
	Show that the disjoint union of two nonempty spaces is disconnected.
\end{exercise}

More interesting is the wedge sum, where two topological spaces $X$
and $Y$ are fused together only at a single base point.
\begin{definition}
	Let $X$ and $Y$ be topological spaces, and $x_0 \in X$ and $y_0 \in Y$
	be points.
	We define the equivalence relation $\sim$ by declaring $x_0 \sim y_0$ only.
	Then the \vocab{wedge sum} of two spaces is defined as
	\[ X \vee Y = (X \amalg Y) / {\sim}. \]
\end{definition}

\begin{example}
	[$S^1 \vee S^1$ is a figure eight]
	Let $X = S^1$ and $Y = S^1$,
	and let $x_0 \in X$ and $y_0 \in Y$ be any points.
	Then $X \vee Y$ is a ``figure eight'': it is two
	circles fused together at one point.
	\begin{center}
		\begin{asy}
			size(3cm);
			draw(shift(-1,0)*unitcircle);
			draw(shift(1,0)*unitcircle);
			dotfactor *= 1.4;
			dot(origin);
		\end{asy}
	\end{center}
\end{example}
\begin{abuse}
	We often don't mention $x_0$ and $y_0$ when they are understood
	(or irrelevant).  For example, from now on we will just
	write $S^1 \vee S^1$ for a figure eight.
\end{abuse}

\begin{remark}
	Annoyingly, in \LaTeX\ \verb+\wedge+ gives $\wedge$ instead
	of $\vee$ (which is \verb+\vee+).
	So this really should be called the ``vee product'', but too late.
\end{remark}

\section{CW complexes}
Using this construction, we can start building some spaces.
One common way to do so is using a so-called \vocab{CW complex}.
Intuitively, a CW complex is built as follows:
\begin{itemize}
	\ii Start with a set of points $X^0$.
	\ii Define $X^1$ by taking some line segments (copies of $D^1$)
	and fusing the endpoints (copies of $S^0$) onto $X^0$.
	\ii Define $X^2$ by taking copies of $D^2$ (a disk)
	and welding its boundary (a copy of $S^1$) onto $X^1$.
	\ii Repeat inductively up until a finite stage $n$;
	we say $X$ is \vocab{$n$-dimensional}.
\end{itemize}
The resulting space $X$ is the CW-complex.
The set $X^k$ is called the \vocab{$k$-skeleton} of $X$.
Each $D^k$ is called a \vocab{$k$-cell}; it is customary to
denote it by $e_\alpha^k$ where $\alpha$ is some index.
We say that $X$ is \vocab{finite} if only finitely many cells were used.
\begin{abuse}
	Technically, most sources (like \cite{ref:hatcher}) allow one to
	construct infinite-dimensional CW complexes.
	We will not encounter any such spaces in the Napkin.
\end{abuse}

\begin{example}
	[$D^2$ with $2+2+1$ and $1+1+1$ cells]
	\listhack
	\begin{enumerate}[(a)]
	\ii First, we start with $X^0$ having two points $e_a^0$ and $e_b^0$.
	Then, we join them with two $1$-cells $D^1$ (green),
	call them $e_c^1$ and $e_d^1$.
	The endpoints of each $1$-cell (the copy of $S^0$) get identified
	with distinct points of $X^0$; hence $X^1 \cong S^1$.
	Finally, we take a single $2$-cell $e^2$ (yellow) and weld it in,
	with its boundary fitting into the copy of $S^1$ that we just drew.
	This gives the figure on the left.

	\ii In fact, one can do this using just $1+1+1=3$ cells.
	Start with $X^0$ having a single point $e^0$.
	Then, use a single $1$-cell $e^1$, fusing its two endpoints
	into the single point of $X^0$.
	Then, one can fit in a copy of $S^1$ as before,
	giving $D^2$ as on the right.
	\end{enumerate}
	\begin{center}
		\begin{asy}
			size(4cm);
			filldraw(unitcircle, opacity(0.2)+yellow, heavygreen);
			dotfactor *= 1.4;
			dot(dir(90), blue);
			dot(dir(-90), blue);
			label("$e_a^0$", dir(90), dir(90), blue);
			label("$e_b^0$", dir(-90), dir(-90), blue);
			label("$e_c^1$", dir(0), dir(0), heavygreen);
			label("$e_d^1$", dir(180), dir(180), heavygreen);
			label("$e^2$", origin, origin);
		\end{asy}
		\qquad
		\begin{asy}
			size(4cm);
			filldraw(unitcircle, opacity(0.2)+yellow, heavygreen);
			dotfactor *= 1.4;
			dot(dir(90), blue);
			label("$e^0$", dir(90), dir(90), blue);
			label("$e^1$", dir(-90), dir(-90), heavygreen);
			label("$e^2$", origin, origin);
		\end{asy}
	\end{center}
\end{example}

\begin{example}
	[$S^n$ as a CW complex]
	\listhack
	\begin{enumerate}[(a)]
		\ii One can obtain $S^n$ (for $n \ge 1$) with just two cells.
		Namely, take a single point $e^0$ for $X^0$, and to obtain $S^n$
		take $D^n$ and weld its entire boundary into $e^0$.

		We already saw this example in the beginning with $n=2$,
		when we saw that the sphere $S^2$ was the result when we fuse
		the boundary of a disk $D^2$ together.

		\ii Alternatively, one can do a ``hemisphere'' construction,
		by constructing $S^n$ inductively using two cells in each dimension.
		So $S^0$ consists of two points, then $S^1$ is obtained
		by joining these two points by two segments ($1$-cells),
		and $S^2$ is obtained by gluing two hemispheres (each a $2$-cell)
		with $S^1$ as its equator.
	\end{enumerate}
\end{example}

\begin{definition}
	Formally, for each $k$-cell $e^k_\alpha$ we want to add to $X^k$,
	we take its boundary $S^{k-1}_\alpha$ and weld it onto
	$X^{k-1}$ via an \vocab{attaching map} $S^{k-1}_\alpha \to X^{k-1}$.
	Then
	\[ X^k = X^{k-1} \amalg \left(\coprod_\alpha e^k_\alpha\right) / {\sim} \]
	where $\sim$ identifies each boundary point of $e^k_\alpha$
	with its image in $X^{k-1}$.
\end{definition}

\section{The torus, Klein bottle, $\RP^n$, $\CP^n$}
\label{sec:top_spaces}
We now present four of the most import examples of CW complexes.

\subsection{The torus}
The \vocab{torus} can be formed by taking
a square and identifying the opposite edges in the same direction:
if you walk off the right edge, you re-appear at the corresponding
point in on the left edge.
(Think \emph{Asteroids} from Atari!)

\begin{center}
	\begin{asy}
		size(2cm);
		fill(unitsquare, yellow+opacity(0.2));
		pair C = (0,0);
		pair B = (1,0);
		pair A = (1,1);
		pair D = (0,1);
		draw(A--B, red, MidArrow);
		draw(B--C, blue, MidArrow);
		draw(D--C, red, MidArrow);
		draw(A--D, blue, MidArrow);
	\end{asy}
\end{center}

Thus the torus is $(\RR/\ZZ)^2 \cong S^1 \times S^1$.

Note that all four corners get identified together to a single point.  One
can realize the torus in $3$-space by treating the square as a sheet of paper,
taping together the left and right (red) edges to form a cylinder,
then bending the cylinder and fusing the top and bottom (blue) edges
to form the torus.
\begin{center}
	\includegraphics[width=0.8\textwidth]{media/Projection_color_torus.jpg}
	\\ \scriptsize Image from \cite{img:torus}
\end{center}

The torus can be realized as a CW complex with
\begin{itemize}
	\ii A $0$-skeleton consisting of a single point,
	\ii A $1$-skeleton consisting of two $1$-cells $e^1_a$, $e^1_b$, and
	\begin{center}
		\begin{asy}
			unitsize(1cm);
			draw(shift(-1,0)*unitcircle, blue, MidArrow);
			draw(shift(1,0)*rotate(180)*unitcircle, red, MidArrow);
			label("$e^1_a$", 2*dir(180), dir(180), blue);
			label("$e^1_b$", 2*dir(0), dir(0), red);
			dotfactor *= 1.4;
			dot("$e^0$", origin, dir(0));
		\end{asy}
	\end{center}
	\ii A $2$-skeleton with a single $2$-cell $e^2$,
	whose circumference is divided into four parts,
	and welded onto the $1$-skeleton ``via $aba\inv b \inv$''.
	This means: wrap a quarter of the circumference around $e^1_a$,
	then another quarter around $e^1_b$,
	then the third quarter around $e^1_a$ but in the opposite direction,
	and the fourth quarter around $e^1_b$ again in the opposite direction as before.
	\begin{center}
		\begin{asy}
			size(3cm);
			fill(unitcircle, yellow+opacity(0.2));
			defaultpen(linewidth(1));
			draw(arc(origin, 1, 45, 135), blue, MidArrow);
			draw(arc(origin, 1, 315, 225), blue, MidArrow);
			draw(arc(origin, 1, 135, 225), red, MidArrow);
			draw(arc(origin, 1, 45, -45), red, MidArrow);
			label("$e^2$", origin, origin);
		\end{asy}
	\end{center}
\end{itemize}
We say that $aba\inv b\inv$ is the \vocab{attaching word};
this shorthand will be convenient later on.

\subsection{The Klein bottle}
The \vocab{Klein bottle} is defined similarly to
the torus, except one pair of edges is identified in the opposite manner,
as shown.

\begin{center}
	\begin{asy}
		size(2cm);
		fill(unitsquare, yellow+opacity(0.2));
		pair C = (0,0);
		pair B = (1,0);
		pair A = (1,1);
		pair D = (0,1);
		draw(A--B, red, MidArrow);
		draw(C--B, blue, MidArrow);
		draw(D--C, red, MidArrow);
		draw(A--D, blue, MidArrow);
	\end{asy}
\end{center}

Unlike the torus one cannot realize this in $3$-space
without self-intersecting. One can tape together the red edges
as before to get a cylinder, but to then fuse the resulting blue
circles in opposite directions is not possible in 3D.
Nevertheless, we often draw a picture in 3-dimensional space
in which we tacitly allow the cylinder to intersect itself.

\begin{center}
	\begin{minipage}[c]{0.5\textwidth}
	\includegraphics[width=\textwidth]{media/klein-fold.png}
	\end{minipage}
	\quad
	\begin{minipage}[c]{0.3\textwidth}
	\includegraphics[width=\textwidth]{media/KleinBottle-01.png}
	\end{minipage}
	\par \scriptsize Image from \cite{img:kleinfold,img:kleinbottle}
\end{center}

Like the torus, the Klein bottle is realized as a CW complex with
\begin{itemize}
	\ii One $0$-cell,
	\ii Two $1$-cells $e^1_a$ and $e^1_b$, and
	\ii A single $2$-cell attached this time via the word $abab\inv$.
\end{itemize}

\subsection{Real projective space}
Let's start with $n=2$.
The space $\RP^2$ is obtained if we reverse both directions of
the square from before, as shown.

\begin{center}
	\begin{asy}
		size(2cm);
		fill(unitsquare, yellow+opacity(0.2));
		pair C = (0,0);
		pair B = (1,0);
		pair A = (1,1);
		pair D = (0,1);
		draw(B--A, red, MidArrow);
		draw(C--B, blue, MidArrow);
		draw(D--C, red, MidArrow);
		draw(A--D, blue, MidArrow);
	\end{asy}
\end{center}

However, once we do this the fact that the original
polygon is a square is kind of irrelevant;
we can combine a red and blue edge to get the single purple edge.
Equivalently, one can think of this as a circle with half
its circumference identified with the other half:

\begin{center}
	\begin{asy}
		size(3cm);
		dotfactor *= 2;
		fill(unitcircle, opacity(0.2)+yellow);
		draw(dir(-90)..dir(0)..dir(90), purple, MidArrow);
		draw(dir(90)..dir(180)..dir(-90), purple, MidArrow);
		dot(dir(90));
		dot(dir(-90));
		label("$\mathbb{RP}^2$", origin, origin);
	\end{asy}
	\qquad
	\begin{asy}
		size(3cm);
		dotfactor *= 2;
		draw(dir(-90)..dir(0)..dir(90));
		draw(dir(90)..dir(180)..dir(-90), dashed);
		fill(unitcircle, yellow+opacity(0.2));
		dot(dir(90));
		opendot(dir(-90));
		label("$\mathbb{RP}^2$", origin, origin);
	\end{asy}
\end{center}

The resulting space should be familiar to those of you who do
projective (Euclidean) geometry.
Indeed, there are several possible geometric interpretations:
\begin{itemize}
	\ii One can think of $\RP^2$ as the set of lines through the
	origin in $\RR^3$, with each line being a point in $\RP^2$.

	Of course, we can identify each line with a point on the unit sphere $S^2$,
	except for the property that two antipodal points actually
	correspond to the same line, so that $\RP^2$ can be almost thought
	of as ``half a sphere''. Flattening it gives the picture above.

	\ii Imagine $\RR^2$, except augmented with ``points at infinity''.
	This means that we add some points ``infinitely far away'',
	one for each possible direction of a line.
	Thus in $\RP^2$, any two lines indeed intersect
	(at a Euclidean point if they are not parallel, and at a point
	at infinity if they do).

	This gives an interpretation of $\RP^2$,
	where the boundary represents the \emph{line at infinity}
	through all of the points at infinity.
	Here we have used the fact that $\RR^2$
	and interior of $D^2$ are homeomorphic.
\end{itemize}
\begin{exercise}
	Observe that these formulations are equivalent
	by considering the plane $z=1$ in $\RR^3$,
	and intersecting each line in the first formulation with this plane.
\end{exercise}

We can also express $\RP^2$ using coordinates:
it is the set of triples $(x : y : z)$ of real numbers not all zero
up to scaling, meaning that
\[ (x : y : z) = (\lambda x : \lambda y : \lambda z) \]
for any $\lambda \neq 0$.
Using the ``lines through the origin in $\RR^3$'' interpretation
makes it clear why this coordinate system gives the right space.
The points at infinity are those with $z = 0$,
and any point with $z \neq 0$ gives a Cartesian point since
\[ (x : y : z) = \left( \frac xz : \frac yz : 1 \right) \]
hence we can think of it as the Cartesian point $(\frac xz, \frac yz)$.

In this way we can actually define \vocab{real-projective $n$-space},
$\RP^n$ for any $n$, as either
\begin{enumerate}[(i)]
	\ii The set of lines through the origin in $\RR^{n+1}$,
	\ii Using $n+1$ coordinates as above, or
	\ii As $\RR^n$ augmented with points at infinity,
	which themselves form a copy of $\RP^{n-1}$.
\end{enumerate}

As a possibly helpful example, we give all three pictures of $\RP^1$.
\begin{example}
	[Real projective $1$-Space]
	$\RP^1$ can be thought of as $S^1$ modulo the relation
	the antipodal points are identified.
	Projecting onto a tangent line, we see that we get
	a copy of $\RR$ plus a single point at infinity, corresponding
	to the parallel line (drawn in cyan below).
	\begin{center}
		\begin{asy}
			size(7cm);
			filldraw(unitcircle, lightblue+opacity(0.2), heavyblue+opacity(0.4));
			label("$S^1$", dir(225), dir(225), lightblue);
			dot("$\vec 0$", origin, dir(45));
			pair X1 = (-2.1,1);
			pair X2 = (1.9,1);
			draw(X1--X2, heavyred, Arrows);
			dot("$0$", (0,1), dir(90), heavyred);
			dot("$1$", (1,1), dir(90), heavyred);
			pair P = extension( (0,1), (1,1), dir(250), dir(70) );
			dot("$0.36$", P, dir(90), heavyred);
			label("$\mathbb R$", X2, dir(105), heavyred);

			path L(pair A, pair B, real a=0.6, real b=a)
				{ return (a*(A-B)+A)--(b*(B-A)+B); }
			draw(L(dir(130),-dir(130),0.2,0.2), gray);
			draw(L(dir(250),-dir(250),0.2,0.2), gray);
			draw(L(dir(-20),-dir(-20),0.2,0.2), gray);
			draw(L(dir(0), -dir(0), 0.4,0.4), heavycyan+1);
		\end{asy}
	\end{center}

	Thus, the points of $\RP^1$ have two forms:
	\begin{itemize}
		\ii $(x:1)$, which we think of as $x \in \RR$ (in dark red above), and
		\ii $(1:0)$, which we think of as $1/0 = \infty$,
		corresponding to the cyan line above.
	\end{itemize}
	So, we can literally write
	\[ \RP^1 = \RR \cup \{\infty\}. \]
	Note that $\RP^1$ is also the boundary of $\RP^2$.
	In fact, note also that topologically we have
	\[ \RP^1 \cong S^1 \]
	since it is the ``real line with endpoints fused together''.
	\begin{center}
		\begin{asy}
			size(2cm);
			draw(unitcircle, heavyred);
			dot("$\infty$", dir(90), dir(90), heavycyan);
			dot("$0$", dir(-90), dir(-90), heavyred);
		\end{asy}
	\end{center}
\end{example}

Since $\RP^n$ is just ``$\RR^n$ (or $D^n$) with $\RP^{n-1}$ as its boundary'',
we can construct $\RP^n$ as a CW complex inductively.
Note that $\RP^n$ thus consists of \textbf{one cell in each dimension}.

\begin{example}[$\RP^n$ as a cell complex]
	\listhack
	\begin{enumerate}[(a)]
		\ii $\RP^0$ is a single point.
		\ii $\RP^1 \cong S^1$ is a circle, which as a CW complex
		is a $0$-cell plus a $1$-cell.
		\ii $\RP^2$ can be formed by taking a $2$-cell
		and wrapping its perimeter twice around a copy of $\RP^1$.
	\end{enumerate}
\end{example}

\subsection{Complex projective space}
The \vocab{complex projective space} $\CP^n$ is
defined like $\RP^n$ with coordinates, i.e.\
\[ (z_0 : z_1 : \dots : z_n) \]
under scaling; this time $z_i$ are complex.
As before, $\CP^n$ can be thought of as $\CC^n$ augmented
with some points at infinity (corresponding to $\CP^{n-1}$).
\begin{example}
	[Complex projective space]
	\listhack
	\begin{enumerate}[(a)]
		\ii $\CP^0$ is a single point.
		\ii $\CP^1$ is $\CC$ plus a single point at infinity
		(``complex infinity'' if you will).
		That means as before we can think of $\CP^1$ as
		\[ \CP^1 = \CC \cup \{\infty\}. \]
		So, imagine taking the complex plane and then adding
		a single point to encompass the entire boundary.
		The result is just sphere $S^2$.
	\end{enumerate}
	Here is a picture of $\CP^1$ with its coordinate system,
	the \vocab{Riemann sphere}.
	\begin{center}
		\includegraphics[width=0.9\textwidth]{media/earth.pdf}
	\end{center}
\end{example}

\begin{remark}
	[For Euclidean geometers]
	You may recognize that while $\RP^2$ is the setting for projective geometry,
	inversion about a circle is done in $\CP^1$ instead.
	When one does an inversion sending generalized circles to generalized
	circles, there is only one point at infinity:
	this is why we work in $\CP^n$.
\end{remark}

Like $\RP^n$, $\CP^n$ is a CW complex, built inductively
by taking $\CC^n$ and welding its boundary onto $\CP^{n-1}$
The difference is that as topological spaces,
\[ \CC^n \cong \RR^{2n} \cong D^{2n}. \]
Thus, we attach the cells $D^0$, $D^2$, $D^4$ and so on
inductively to construct $\CP^n$.
Thus we see that
\begin{moral}
	$\CP^n$ consists of one cell in each \emph{even} dimension.
\end{moral}

\section\problemhead
\begin{problem}
	Show that a space $X$ is Hausdorff if and only if the diagonal
	$\{(x,x) \mid x \in X\}$ is closed in the product space $X \times X$.
\end{problem}
\begin{problem}
	Realize the following spaces as CW complexes:
	\begin{enumerate}[(a)]
		\ii M\"obius strip.
		\ii $\RR$.
		\ii $\RR^n$.
	\end{enumerate}
\end{problem}
\begin{dproblem}
	Show that a finite CW complex is compact.
	\begin{hint}
		Prove and use the fact that a quotients of compact spaces remain compact.
	\end{hint}
\end{dproblem}


\section*{Exercises}

\begin{ex}
  Can a column vector ever be equal to a row vector?

  \begin{sol}
    Yes, a $1\times 1$-matrix is both a row vector and a column
    vector.  However, a column vector of dimension $2$ or greater can
    never be equal to a row vector, because one is an $n\times
    1$-matrix and the other is a $1\times n$-matrix.
  \end{sol}
\end{ex}

\begin{ex}
  Find scalars $x,y,z$ such that the following two matrices are equal.
  \begin{equation*}
    \begin{mymatrix}{rr}
      x & -1 \\
      2 & 4
    \end{mymatrix}
    \quad\mbox{and}\quad
    \begin{mymatrix}{rr}
      2 & y \\
      z & 4
    \end{mymatrix}.
  \end{equation*}
  \begin{sol}
    $x=2$, $y=-1$, $z=2$.
  \end{sol}
\end{ex}

\begin{ex}
  What are the dimensions of the following matrices?
  \begin{equation*}
    A = \begin{mymatrix}{rrr}
      1 & -2 & 0 \\
      4 & 3 & 2
    \end{mymatrix},
    \quad
    B = \begin{mymatrix}{rrr}
      3 & 4 & 1 \\
      1 & 3 & 1 \\
      6 & 2 & 2
    \end{mymatrix},
    \quad
    C = \begin{mymatrix}{rrr}
      1 & 0 \\
      4 & 0 \\
      2 & 0 \\
      0 & 0
    \end{mymatrix}.
  \end{equation*}
  \begin{sol}
    $2\times 3$, $3\times 3$, $4\times 2$.
  \end{sol}
\end{ex}

\begin{ex}
  What is the $(2,3)$-entry of the matrix $\begin{mymatrix}{rrr}
    1 & 2 & 1 \\
    -4 & 4 & 7 \\
    6 & -5 & 3
  \end{mymatrix}$?
  \begin{sol}
    $7$.
  \end{sol}
\end{ex}


%\section{1.10 Exercise 10. An open mapping theorem}
\textit{Suppose that X and Y are topological vector spaces,
%
  $\dim Y < \infty$,
%
$\Lambda : X \to Y$ is linear, and $\Lambda(X) = Y$.
%
  \begin{enumerate}
    \item{
      Prove that $\Lambda$ is an open mapping.}
    \item{
      Assume, in addition, that the null space of $\Lambda$ is closed,
      and prove that $\Lambda$ is continuous.
    }
  \end{enumerate}
  %
}
%
\begin{proof}
%We discard the trivial case $\dim Y = 0$ then henceforth assume that $\dim Y$
%has positive dimension $n$. \\\\
%
Discard the trivial case $\Lambda = 0$ then assume that %
$\dim Y = n$ %
for some positive $n$. %
Let $e$ range over a base of $B$ of $Y$. %
Pick $W$ an arbitrary neighborhood of the origin: %
There so exists $V$ %
a balanced neighborhood of the origin such that
%
  \begin{align}
    \label{definition of v}
    \underset{
      \text{Put } V \text{ exactly } n \text{ time(s)}
    }{
      \underbrace{V+\cdots +V}
    }\subset W,
  \end{align}
%
since addition is continuous. %
Moreover, for each $e$,
there exists $x_e$ in $X$ such that
%
  $\Lambda(x_e)=e$,
%
simply because $\Lambda$ is onto. So,
%
  \begin{align}\label{1_10_sum}
    y = \sum_{e} y_e \cdot \Lambda x_e,
  \end{align}
%
given any element $y=\sum_{e}y_e\cdot e$ of $Y$. %
As a finite set, $\set{x_e}{e\in B}$ is bounded: %
In particular, there exists  a positive scalar $s$ such that
%
  \begin{align}
    \forall e\in B,  x_e \in s \cdot V.
  \end{align}
%
Combining this with (\ref{1_10_sum}) shows that
%
  \begin{align}
    \label{y in sum of lambda V}
    y \in \sum_e y_e \cdot s \cdot \Lambda (V).
  \end{align}
%
We now come back to (\ref{definition of v}) and so conclude that %
%
  \begin{align}
    y \in \sum_e \Lambda (V) \subset \Lambda(W)
  \end{align}
%
whether $\magnitude{y_e} < 1/s$; which proves (a).\\\\
%
%
To prove (b), assume that the null space
%
  $\singleton{\Lambda = 0}$ %
%
is closed and let $f, \pi$ be as in Exercise 1.9,  %
%
  $\singleton{\Lambda = 0}$ %
%
playing the role of $N$.
%%
% Isomorphism:
%  \begin{align}
%    & X \to                    X/N   \to                  Y . \\
%    & x \overset{\pi}{\mapsto} \pi x  \overset{f}{\mapsto} \Lambda x \nonumber
%  \end{align}
%
Since $\Lambda$ is onto, the first isomorphism theorem (see Exercise 1.9)
asserts that
%
  $f$ is an isomorphism of $X/N$ onto $Y$.
%
Consequently,
%
  \begin{align}
    \dim X/N= n.
  \end{align}
%
$f$ is then an homeomorphism of
%
  $X/N\equiv \C^{n}$
%
onto $Y$; see \citeresultFA{1.21}.
We have thus established that $f$ is continuous: So is $\Lambda = f\circ \pi$.
\end{proof}


\subsection{Independent increments}

The changes in any non-overlapping time increments are independent.

Formally:

\(t_0<t_1<t_2<...<t_m\)

With \(X_t\)

\(X_{t_1}-X_{t_0}\) is indepentent from \(X_{t_2}-X{t_1}\) etc.

\subsection{Wiener processes}

A Wiener process is a process \(W_t\) with independent increments, which:
+ Is continuous
+ Has normally distributed increments.

Can be constructed as limit of random walk.
Can also be constructed as integral of Gaussian noise?


\lab{Line Sweep and Voronoi Diagrams}{Line Sweep}
\label{lab:Linesweep}

\objective{Learn about and implement a basic line sweep algorithm and Introduce Voronoid Diagrams and Delaunay Triangulations and discuss their applications}

\section*{General Line Sweep Algorithms}

Line sweep algorithms are a significant group of algorithms with a variety of applications.
In the strictest sense, line sweep algorithms typically use a priority queue and binary tree to lower the temporal complexity associated with certain tasks.
Some notable examples include Fortune's Algorithm for calculating Voronoi diagrams and Andrew's Algorithm for computing the convex hull of a set of points.
In this lab, we will explore a basic line sweep algorithm that allows efficient computation of the Closest Pair problem.
Given a set of points, find the distance between the closest pair of points.

\section*{Na\"ive Implementation}

The obvious way of doing this is to simply compute the distance between each point and each of the other points.
Roughly speaking, this requires some constant multiple of $n^2$ computations where $n$ is the number of points.
This can be coded as follows:
\begin{lstlisting}
def reallybadmindist(X):
    r = ((X[0]-X[1])**2).sum()**.5
    for i in range(len(X)):
        for j in range(len(X)):
            if i != j:
                r = min(r,((X[i]-X[j])**2).sum()**.5)
    return r
\end{lstlisting}
Since distance is symmetrical, we can compare each point with the points that follow it in the list we are given (which will reduce
the total number of distances to be computed).
This is a slightly better implementation:
\begin{lstlisting}
def multidist(p0,p1):
    l=len(p0)
    return sum([(p0[i]-p1[i])**2 for i in range(l)])**.5
def badmindist(X):
    l=len(X)
    r=multidist(X[0],X[1])
    for i in xrange(l):
        for j in xrange(i+1,l):
            d=multidist(X[i],X[j])
            if d<r:
                r=d
    return r
\end{lstlisting}

Since, on average, we iterate through about half of the list for each point added, this algorithm will have complexity $O(n^2)$ (we are only scaling by a constant factor).
It is faster than the truly na\"ive method, but essentially just as inefficient.
For very small numbers of points, this algorithm works, but it quickly becomes inefficient as we increase the number of points processed.
What we would like to do is go from $O(n^2)$ to something better.
To do this, we have to think about the problem differently.
This can be done by using one of the key techniques of computational geometry-a line sweep algorithm.

\section*{A Simplified Line Sweep Algorithm}

The concept behind a line sweep algorithm is breaking a global problem into a sequence of smaller problems that can be solved locally.
We sweep the domain space with a line.
This line divides the domain space into an explored region and an unexplored region.
As the line sweeps, events occur, be it whether the line encounters a point in the data set
or some other type of event.
We compute the solution at the very beginning of the line sweep and as new events happen, we compute the change that these events have on our current solution.
We only need to consider the events in the explored region or on the sweep line as they are the only events that affect the current solution.
Sweep line algorithms typically exhibit a temporal complexity of $O(n \log n)$, which is far better than $O(n^2)$.

Line sweep algorithms are good for doing all sorts of proximity based computations.
In the example above, we took advantage of the fact that distance was symmetric ($d(a,b)=d(b,a)$).
In a line sweep version of this algorithm, we also take advantage of the fact that any points further than the current smallest minimum distance along a given axis do not need to be considered.
This is really just an application of the triangle inequality.
The secret to the line sweep solution for the closest pair problem is to actually have two sweep lines with a small distance between them.
As we process the points in our list, we can decrease the distance between the lines so that we only process points that could possibly give a smaller distance than the smallest distance we have already encountered.
By only processing the points between our two sweep lines, we avoid processing many points needlessly.

For a general line sweep algorithm, it is common to use a priority queue to order the items that need to be processed.
A good priority queue can be found in the library \li{Queue} that is included in Python.
In this case all we need to do is sort the points by $X$ value.
This means it will be easier to make a sorted copy of the array using Numpy's built in functions.
Something like \li{X=Y.take(Y[:,0].argsort(),axis=0)} or \li{Y[Y[:,0].argsort()]} will do this for you.

We track the points in the area between the two sweep lines with a list.
Each time we advance the sweep lines, we update this list so it always contains the points between the sweep lines (adding or removing points where necessary).
A point stays in the list if its distance from the leading sweep line is less than the current minimum distance.
Conceptually, this is how we place the second sweep line.

Assuming we have already ordered all our points by $x$ coordinate, we can perform the line sweep algorithm as follows:

\vspace{5mm}
\begin{compactenum}[1.]
\item
Process the first two points and add them to the active list.
Set current minimum distance to the distance between these two points.
\item
Get the next point in the priority queue.
\item
Update the active list so that it contains only the points between the two sweep lines.
\item
Compute the distance between the current point and all the points in the active list.
\item
Compare the smallest distance found with the current minimum distance.
Change the current minimum distance if needed.
\item
Repeat steps 2 to 5 for all remaining points in the priority queue
\end{compactenum}
\vspace{5mm}

When the algorithm ends, the current minimum distance will be the distance between the closest pair of points.

This algorithm can be illustrated as follows:
\begin{figure}[H]
\includegraphics[width = .8\textwidth]{simple0.pdf}
\caption{After processing the first two points, we process the third point and change the current minimum distance accordingly.}
\end{figure}

\begin{figure}[H]
\includegraphics[width = .8\textwidth]{simple1.pdf}
\caption{This change in the minimum thus far is reflected in how we form the actives list for the next point we process.
We remove the points from the top of the queue that are too far away in the $x$ direction to have a distance less than the current minimum.
We then process anything that is left.}
\end{figure}

\begin{figure}[H]
\includegraphics[width = .8\textwidth]{simple5.pdf}
\caption{A few points further in we actually hit the minimum distance.}
\end{figure}

\begin{figure}[H]
\includegraphics[width = .8\textwidth]{simple7.pdf}
\caption{After iterating over all the points in the set, we have the final minimum distance desired.}
\end{figure}

This process is outlined in Algorithm \ref{alg:linesweep_simple}.
\begin{algorithm}
\begin{algorithmic}[1]
\Procedure{MindistSimple}{$Y, \text{metric}$}
	\LineComment{Make sorted copy of $Y$ to perform linesweep.}
	\State $X \gets Y \text{ sorted along first coordinate}$
	\LineComment{Initialize the smallest distance as the distance between the first points.}
	\State $r \gets metric\left(X\left[0\right], X\left[1\right]\right)$
	\LineComment{Use indices to track which points are active.}
	\State $l \gets 0$
	\LineComment{This is all that is needed for the first two points.}
	\LineComment{Iterate over the remaining points.}
	\For{$i=2$, $j<k$}
		\LineComment{Update $l$ to not include points that are too far away.}
		\While{$X\left[l, 0\right] + r < X\left[i, 0\right]$}
			\State $l += 1$
		\EndWhile
		\LineComment{If there are any points to process, process them.}
		\If{$l < i$}
			\State $r \gets \text{min}\left(r, \text{metric}\left(X\left[i\right], X\left[l:i\right]\right)\right)$
		\EndIf
	\EndFor
	\State \pseudoli{return} $r$
\EndProcedure
\end{algorithmic}
\caption{A Simplified Line Sweep Algorithm}
\label{alg:linesweep_simple}
\end{algorithm}

\begin{problem}
Write a Python function that implements the above algorithm.
Have your code accept a metric function as a parameter.
This metric function should take a point and an array of points as arguments and return an array of distances from the point to each of the points in the array.
Test your function's speed.
How does it scale as you increase the number of points?
How does it scale as you increase the number of dimensions?
\end{problem}

\section*{A Line Sweep Algorithm}

You may have noticed that we are not exploiting all the symmetry of the problem in the previous algorithm.
It would likely speed things up if we could also slice away the points that have $y$ values that could not possibly yield the minimum distance.
The simplest way to do this in this case is to take advantage of some algebraic symmetry.
As we compute the distance between two points, we have to calculate the difference between each of the coordinates of the points in question.
If at any time the difference has an absolute value greater than the minimum distance we have encountered thus far, we know we don't need to finish processing the point.
This is, theoretically, like slicing along the other axes in order to further reduce the ``active'' list of points.
A similar result could be obtained by using a list, binary tree, or some other data structure to keep the list of active points sorted by $y$ value, but in this case the insertion and deletion operations involved are more costly.

This algorithm can be illustrated as follows:

Note: in this case we sweep along the $x$ axis from right to left.

\begin{figure}[H]
\includegraphics[width = .8\textwidth]{sweep0.pdf}
\caption{In processing the first point, we find find that we must reduce the radius.
Notice that we only need to consider the points that lie in the box formed by the red an blue lines.}
\end{figure}

\begin{figure}[H]
\includegraphics[width = .8\textwidth]{sweep13.pdf}
\caption{This is where we actually hit the minimum distance.}
\end{figure}

\begin{figure}[H]
\includegraphics[width = .8\textwidth]{sweep22.pdf}
\caption{After processing through all the points, we are guaranteed to have hit the minimum distance already, so our minimum distance thus far is our final return value.}
\end{figure}

This process is outlined in Algorithm \ref{alg:linesweep_full}.
\begin{algorithm}
\begin{algorithmic}[1]
\Procedure{Mindist}{$Y, \text{metric}$}
	\LineComment{Make sorted copy of $Y$ to perform linesweep.}
	\State $X \gets Y \text{ sorted along first coordinate}$
	\LineComment{Store number of dimensions.}
	\State $dim \gets Y.\text{shape}\left[1\right]$
	\LineComment{Initialize the smallest distance as the distance between the first points.}
	\State $r \gets metric\left(X\left[0\right], X\left[1\right]\right)$
	\LineComment{Use indices to track which points are active.}
	\State $l \gets 0$
	\LineComment{This is all that is needed for the first two points.}
	\LineComment{Iterate over the remaining points.}
	\For{$i=2$, $j<k$}
		\LineComment{Update $l$ to not include points that are too far away.}
		\While{$X\left[l, 0\right] + r < X\left[i, 0\right]$}
			\State $l += 1$
		\EndWhile
		\LineComment{If there are any points to process, process them.}
		\For{$k=l$, $i$}
			\LineComment{Initialize a flag to store whether or not to process a point.}
			\State $proc \gets \text{True}$
			\LineComment{Initialize partially computed distance to $0$.}
			\State $d \gets 0.$
			\LineComment{Compute differences in each dimension.}
			\For{$j=1$, $dim$}
				\State $dif \gets \left|X\left[k,j\right] - \left[i, j\right]\right|$
				\LineComment{Stop computation if point is too far away.}
				\If{$r < dif$}
					\LineComment{Set flag to avoid further computations for this point.}
					\State $proc \gets \text{False}$
					\State \pseudoli{break}
				\EndIf
				\LineComment{If the point still needs to be processed,}
				\LineComment{add in the difference squared.}
				\State $d \gets d + dif^2$
			\EndFor
			\LineComment{If the point still has not been rejected, compute the distance.}
			\LineComment{Update the minimum distance if needed.}
			\If{$proc$}
				\State $dif \gets X\left[k,0\right] - X\left[i,0\right]$
				\State $r \gets \text{min}\left(r, \sqrt{d + dif^2}\right)$
			\EndIf
		\EndFor
	\EndFor
	\State \pseudoli{return} $r$
\EndProcedure
\end{algorithmic}
\caption{A Line Sweep Algorithm}
\label{alg:linesweep_full}
\end{algorithm}

\begin{problem}
Implement the algorithm above in Python.
Time it against the naive implementation at the beginning of this lab and against the simplified version you coded above.
The new version should be faster, but speed depends heavily on how you have implemented it, so you may have some optimization left to do.
Time how long it takes your new function to process ten million points in two dimensions.
Using benchmarks from the first implementation we gave you, estimate how long (in years) it would take that function to process that many points.
How many times faster is the good implementation?
\end{problem}

Since this version of the algorithm depends so heavily on array lookups, it will be much faster if it is implemented in Cython.

\section*{Voronoi Diagrams}

In this section we will discuss some applications of Voronoi Diagrams.
They are an application of the line sweep algorithm in that they can be computed using a line sweep algorithm called Fortune's Algorithm.
In the abstract sense, a Voronoi diagram is a partition of a plane into regions that lie closest to different points.
The easiest way to understand this is to look at some examples.
Figure \ref{voronoi_ex_1} is a voronoi diagram generated from 100 random points with $x$ and $y$ values between 0 and 1.
Notice how each point has a small  cell around it that is made up of the points that lie closest to it.

\begin{figure}
\includegraphics[width=\textwidth]{voronoi_example_1.pdf}
\caption{A Voronoi diagram of the square $[0,1]\times [0,1]$ for 100 randomly generated points.}
\label{voronoi_ex_1}
\end{figure}

One possible way to compute this sort of diagram would be by brute force, for example:
\begin{lstlisting}
import numpy as np
from numpy.random import rand
from matplotlib import pyplot as plt

def pnorm(pt, X, p=2):
    # Take the p-norm distance between a point 'pt'
    # and an array of points 'X'.
    if p == "inf":
        return np.absolute(pt - X).max(axis=-1)
    return (np.absolute(pt - X)**p).sum(axis=-1)**(1./p)

def brute_force_voronoi(n, res, p=2, filename=None):
    # Generates a grid of points and tests to find the nearest
    # neighbor for each of them.
    pts = rand(n, 2)
    X = np.linspace(0, 1, res)
    # Make an array to store the indices of the nearest points.
    indices = np.zeros((res, res))
    for i in xrange(res):
        for j in xrange(res):
            indices[i, j] = pnorm(np.array([X[j], X[i]]), pts, p).argmin()
    # Make a colorplot of the results.
    X, Y = np.meshgrid(X, X, copy=False)
    plt.pcolormesh(X, Y, indices)
    plt.scatter(pts[:,0], pts[:,1])
    plt.xlim((0,1))
    plt.ylim((0,1))
    plt.show()

brute_force_voronoi(10, 401)
\end{lstlisting}
This algorithm is good because it can work regardless of the metric space we are using, but it is terribly slow.
Figure \ref{voronoi_1norm} shows a similar diagram using the 1-norm and figure \ref{voronoi_supnorm} shows a diagram generated using the supremum norm.
It is linear in the number of points added and linear in the number of pixels used to represent the diagram.
This can be a terrible limitation, but if you are not working in a well behaved metric space, this may be the simplest approach.
\begin{figure}
\begin{minipage}[b]{0.45\linewidth}
\centering
\includegraphics[width=\textwidth]{voronoi_1norm.png}
\caption{Voronoi diagram in the 1-norm}
\label{voronoi_1norm}
\end{minipage}
\hspace{0.5cm}
\begin{minipage}[b]{0.45\linewidth}
\centering
\includegraphics[width=\textwidth]{voronoi_supnorm.png}
\caption{Voronoi diagram in the supremum norm}
\label{voronoi_supnorm}
\end{minipage}
\end{figure}

It is also possible to make voronoi diagrams based off of shapes and lines as well as points.

There are several data structures used in the field of computational geometry that can store the exact edges of diagrams like this.
We would prefer to not have to worry about sampling the domain in this way.
There are a variety of algorithms that can compute Voronoi diagrams in $\mathcal{O}\left( n \log\left(n\right)\right)$ time (where $n$ is the number of points).
Fortune's algorithm is a linesweep algorithm that can do this.

The Qhull library is commonly used to compute voronoi diagrams, and delaunay triangulations.
SciPy includes a wrapper of the Qhull library in the \li{scipy.spatial} module.
It currently only supports 2 dimensional voronoi diagrams under the Euclidean norm.
It allows you to generate a voronoi diagram from a set of points, add points to a voronoi diagram, find the nearest point to any given point, and plot a voronoi diagram.

A plot similar to the one we just generated can be made using SciPy like this:
\begin{lstlisting}
import numpy as np
from numpy.random import rand
import scipy.spatial as st
from matplotlib import pyplot as plt
G = rand(100,2)
G = st.Voronoi(G)
st.voronoi_plot_2d(G)
plt.xlim((0, 1))
plt.ylim((0, 1))
plt.show()
\end{lstlisting}
Notice how much clearer the plot is and how much faster it is generated.
Try running the last bit of code for 1000 points.
Though this is a little slower than it was for 10 points, this is a graph that would not display well if we had been using the brute force method.

Table \ref{voronoi_attributes} shows the different attributes of the \li{Voronoi} object.
For more details, see \url{http://docs.scipy.org/doc/scipy-dev/reference/spatial.html}
\begin{table}[h!]
\begin{center}
	\begin{tabular}{|l|p{12cm}|}
    \hline

    \li{points} & coordinates of input points (centers of cells)\\

    \li{vertices} & vertices of graph\\

    \li{ridge_points} & indices of the input points between which each ridge on the diagram lies\\

    \li{ridge_vertices} & indices of the vertices at the end of each ridge\\

    \li{regions} & indices of the vertices corresponding to each voronoi cell\\

    \li{point_region} & indices of the regions corresponding to each input point\\

    \hline

    \end{tabular}
\end{center}
\caption{Various summarizing functions}
\label{voronoi_attributes}
\end{table}

Unfortunately, the \li{Voronoi} objects do not allow us to find out, given the coordinates of a new point, which of our original input points lies the closest to it.
To do something like that we would need to use a different algorithm or data structure.
One possible way of solving such a problem is to us a KD tree.
KD trees are discussed in further detail in Volume II.
Scipy includes built in KD trees in both Python and C.
The version in C is generally faster.
You can make a KD tree like this
\begin{lstlisting}
import numpy as np
from numpy.random import rand
import scipy.spatial as st
A = rand(1000,2)
kd = st.cKDTree(A)
\end{lstlisting}
In this case we used the \li{cKDTree} object.
That is a KDTree implemented in C.
\li{KDTree} is a Python-based version that is also included in scipy.
To find the nearest neighbor of a point \li{pt} and how far away it is we can now use the following line of code:
\begin{lstlisting}
kd.query(pt)
\end{lstlisting}
This returns a tuple containing the distance of the given point to the nearest point in \li{A} and the index of that nearest neighbor in \li{A}.
For exmaple, if you put in:
\begin{lstlisting}
kd.query(A[27])
\end{lstlisting}
It will return
\begin{lstlisting}
(0.0,27)
\end{lstlisting}
Since \li{A[27]} would be the nearest point at a distance of \li{0.0}.
\section*{Applications of Voronoi Diagrams}

Though the computation of a Voronoi Diagram does not provide a quick way to run nearest neighbor queries, there are still several applications for Voronoi Diagrams.
The first, and probably most obvious, is the representation of data.
If you want to look for visual patterns in data, a Voronoi Diagram can be very useful.
One historically significant example was the containment of the London Cholera outbreak of 1854.
The English Mathematician John Snow plotted where the cholera outbreaks had all happened and after some consideration, noticed that they were all relatively close to a certain water pump in that portion of the city.
Upon noticing this, he plotted the voronoi cell of that particular water pump and proposed that the cholera outbreak was linked to contaminated water.
He recommended that the pump handle be removed so that people would have to use other pumps in the city.
Once the contaminated pump was shut down, the cholera outbreak was stopped.

Voronoi diagrams can also be used to solve problems involving the points furthest from those already on the graph.
Examples include determining where to drill next when searching for oil, or where to put a new branch of a major company.

The file \li{edge_intersections.py} included with this lab contains a helper function, \li{edge_intersections}, that will be helpful for the rest of the lab.
It takes a Voronoi diagram object as input as well as two tuples (xlims and ylims) representing the limits of a rectangle.
It returns an array containing the intersections of the edges of the voronoi diagram with the boundaries of the rectangular region.
It also returns two lists of pairs of indices.
In the first list of indices, the first index in each pair corresponds to one of the vertices of the Voronoi diagram and the second index corresponds to one of the intersections found by this function.
The second list of indices accounts for the case that no endpoint of an edge intersecting the region lies within the region.
In the second list of indices, both indices of each pair correspond to intersections with the boundaries of the region.

\begin{problem}
\label{FurthestPoints}
Write a function that, given a list of points, \li{pts}, and a rectangular region of the plain and an integer \li{n}, finds the \li{n} points in the region that lie farthest from all the points in \li{pts}.
Do this by creating a list of tuples with the first element as the distance to the nearest neighbor in \li{pts} and the second element of each tuple the canidate point.
You should use a KD tree to compute the distance to the nearest neighbor.
Add to your list of canidate tuples the veritices of the Voronoi diagram, the intersection of the edges given by the helper function, and the corners of the region.
The library \li{heapq} has a nice function \li{nlargest(n,Q)} that will sort your list \li{Q} by the first element in your tuples and return sorted the largest \li{n} elements.
\end{problem}

Another possible application is navigation through a field of obstacles.
This can be done by creating a Voronoi diagram where each obstacle is represented as a point.
This diagram can then be represented as a graph where the vertices of the Voronoi diagram are considered to be the nodes.
Using a shortest path algorithm you can find the shortest path between the two sides that would keep you as far from the obstacles as possible.

\begin{comment}
\begin{problem}
Given a voronoi diagram object, write a function that uses NetworkX to find the shortest path along the edges diagram between any two given vertices.
Have this function take a threshold value to limit which ridges are included.
Do not include a ridge in the adjacency matrix if the distance from the ridge to the center points of the Voronoi cells is less than the threshold.
You can find this distance by measuring the distance between each pair of points referenced in the \li{ridge_points} attribute of the Voronoi object and then dividing by two.
Note: the infinite ridges of the graph have $-1$ listed as one of the indices in the \li{ridge_vertices} attribute.
Make sure you do not include these ridges.

Depending on what is needed from the algorithm, you could modify the weights of the graph to give preference to edges that pass farther from the obstacles.
A fuller solution of this problem would also account for the bounds of the region.
That could be done with the helper function included with this lab.
\end{problem}
\end{comment}

Another possible application of voronoi diagrams is in the estimation of total rainfall, size of ore deposits, or other similar problems.
One simple way to do this is to take a weighted average of all known measurements where each measurement is given the weight corresponding to the size of its voronoi cell.

\begin{comment}
% This problem is cool, but it would probably make the lab too long
% I'll leave it here in case we want to add it later

\begin{problem}
\label{AverageRainfall}
Write a function that, given a square region and measurement values at different nodes, computes the weighted average of the measurements over the region.
Weight each measurement according to the area of the voronoi cell of each node.

Hint: You can find the area of each voroni cell by considering the triangles formed between its vertices and its center point. One way to compute the area of a triangle given the coordinates of its vertices is $A = \sqrt{s\left(s-a\right) \left(s-b\right) \left(s-c\right)}$ where $s = \frac{a+b+c}{2}$ and $a$, $b$, and $c$ are the lengths of the edges of the triangle.
This is known as Herron's Formula.
\end{problem}

\end{comment}

\section*{Delaunay Triangulation}

A concept related to Voronoi diagrams is that of the Delaunay Triangulation.
Delaunay Triangulations also have a wide variety of applications.
One such application is the automatic division of a region into triangles for use in finite element analysis for the numerical solution of partial differential equations.
In general, the delaunay triangulation divides the smallest convex region containing all the given points (the convex hull) into triangles that obey certain rules.

You can make a Delaunay Triangulation from a list of points and plot it like this:
\begin{lstlisting}
A = rand(100, 2)
D = st.Delaunay(A)
plt.triplot(A[:,0] ,A[:,1], D.simplices)
plt.show()
\end{lstlisting}

\begin{problem}
Use a Delaunay triangulation to write a function that breaks up the unit square into right triangles.
Have the only argument to your function be the number of nodes you want along each edge of the square.
Plot your results.
\end{problem}

%other possible applications:
%Use Delaunay Triangulation to tesselate a 3d surface.
%Use tesselation to rerun ore/rainfall problem and compare results.


\section*{Ex.34.3-2}
\subsection*{Show that the $\leq_P$ relation is a transitive relation on languages}

We want to show, that if $L_1\leq_P L_2$ and $L_2\leq_P L_3$, then $L_1\leq_P L_3$.
\\
Since $L_1\leq_P L_2$ and $L_2\leq_P L_3$, then there exists polynomial-time computable reduction functions $f_A$ and $f_B$, where $f_A$ reduces $L_1$ to $L_2$, and $f_B$ reduces $L_2$ to $L_3$.
\\
A polynomial-time computable reduction function $f_C$ can simply use first $f_A$ and then $f_B$, to reduce $L_1$ to $L_3$, meaning we must have $L_1\leq_P L_3$.


\subsection{Parameter identification problem with simultaneous equations}

\subsubsection{Identification terminology}

A system is under-identified if there are not enough estimators for all structural parameters.

A system is exactly identified if there are the same number of estimators as structural parameters.

A system is over-identified if there are more estimators than structural parameters.

In general we have in our structural form:

\(\sum^n_i\beta_{ij}y_i=\sum^m_i\gamma_{ij}x_i+\epsilon_j\)

This is a system with \(n\) endogeneous variables and \(m\) exogeneous variables.

We can write this in matrix form.

\(B\mathbf y =\Gamma \mathbf{x} + \mathbf{\epsilon}\)

We can use this to get:

\(\mathbf{y} =B^{-1}\Gamma \mathbf{x} + B^{-1}\mathbf{ \epsilon}\)

We estimate by placing restrictions on \(\Gamma\).

\subsubsection{Strucutral models}

If our data generating process is:

\(Q=\alpha + \beta P +\epsilon \)

We can estimate \(\alpha \)and \(\beta \) through measuring \(P\) and \(Q\).

If, however the data generating process involves simulataneous equations, we can have:

\(Q=\alpha_1 + \beta_1 P + \epsilon_1 \)

\(Q=\alpha_2 + \beta_2 P + \epsilon_2 \)

\subsubsection{Reduced form}

We can reduce this:

\(\alpha_1 + \beta_1 P + \epsilon_1 =\alpha_2 + \beta_2 P + \epsilon_2 \)

\((\alpha_1 -\alpha_2 )+ (\beta_1 -\beta_2 )P + (\epsilon_1 -\epsilon_2 )=0\)

\(P =\dfrac{\alpha_2-\alpha_1 }{\beta_1-\beta_2}+\dfrac{\epsilon_2-\epsilon_1 }{\beta_1-\beta_2}\)

We can rewrite this as:

\(P=\pi_1 + \tau_1 \)

Similarly we can reduce for \(Q\):

\(Q =\dfrac{\alpha_2\beta_1-\alpha_1\beta_2 }{\beta_1-\beta_2}+\dfrac{\beta_1\epsilon_2 -\beta_2\epsilon_1}{\beta_1-\beta_2}\)

\(Q= \pi_2 + \tau_2\)

\subsubsection{We can't directly estimate structural models}

If \(P\) is correlated with \(epsilon_1\) or \(\epsilon_2\) then our estimates for \(\beta_1\) and \(\beta_2\) will be biased.

This also affects \(Q\).

From the reduced forms we can see that \(P\) will be correlated, due to simultaneity.

\subsubsection{The identification problem}

We can estimate \(\pi_1 \) and \(\pi_2\), but this does not allow us to identify any of the structural parameters.

We have \(2\) estimators, but \(4\) parameters.

This is the identification problem.


\subsection{Principle of explosion}

If axioms contradict each other then it is possible to derive anything. That is:

$P\land \neg P\vdash Q$

We can prove this. If \(P\) and \(\neg P\) are true, then the following is also true:

\(P\lor Q\)

We can then use \(P\lor Q\) and \(\neg P\) to imply \(Q\).

This works for any proposition \(Q\), including \(\neg Q\).

As we can derive \(Q\) and \(\neg Q\), our axioms are not consistent.