\documentclass{amsart}

\usepackage{amssymb}
\usepackage{amsmath}

\title{PDE: Assignment 3}
\author{Mark Ditsworth}

\begin{document}
	\maketitle

	\section{Problem 1}
	Consider a three-component vector field $\mathbf{u(x)}$ on some finite volume domain $\Omega \in \mathbb{R}^3$. Define the inner product of two vectors $\mathbf{u}$ and $\mathbf{v}$ by the volume integral: $\langle\mathbf{u},\mathbf{v}\rangle = \int_{\Omega} \overline{\mathbf{u}} \cdot \mathbf{v}$. Consider the curl operator $\triangledown \times$.
	\\
	\subsection{Part 1}
	Derive the identity: $\triangledown \cdot (\mathbf{u} \times \mathbf{v}) = (\triangledown \times \mathbf{u}) \cdot \mathbf{v} - \mathbf{u} \cdot (\triangledown \times \mathbf{v})$
	\\\\
	\[ \triangledown \cdot (\mathbf{u} \times \mathbf{v})=
	   \triangledown \cdot
	   \begin{bmatrix}
		   u_2v_3 - u_3v_2\\
		   u_3v_1 - u_1v_3\\
		   u_1v_2 - u_2v_1
	   \end{bmatrix}=
	   \frac{\partial}{\partial x}(u_2v_3 - u_3v_2) + \frac{\partial}{\partial y}(u_3v_1 - u_1v_3) +
	   \frac{\partial}{\partial z}(u_1v_2 - u_2v_1)
	\]\\

	\[ (\triangledown \times \mathbf{u}) \cdot \mathbf{v} - \mathbf{u} \cdot (\triangledown \times \mathbf{v}) =
	\begin{bmatrix}
		\frac{\partial u_3}{\partial y} - \frac{\partial u_2}{\partial z} &
		\frac{\partial u_1}{\partial z} - \frac{\partial u_3}{\partial x} &
		\frac{\partial u_2}{\partial x} - \frac{\partial u_1}{\partial y}
	\end{bmatrix} \cdot \mathbf{v} - \mathbf{u} \cdot
	\begin{bmatrix}
		\frac{\partial v_3}{\partial y} - \frac{\partial v_2}{\partial z} \\
		\frac{\partial v_1}{\partial z} - \frac{\partial v_3}{\partial x} \\
		\frac{\partial v_2}{\partial x} - \frac{\partial v_1}{\partial y}
	\end{bmatrix}
	\]\\

	\[ = \frac{\partial u_3}{\partial y}v_1 - \frac{\partial u_2}{\partial z}v_1 +
	\frac{\partial u_1}{\partial z}v_2 - \frac{\partial u_3}{\partial x}v_2 +
	\frac{\partial u_2}{\partial x}v_3 - \frac{\partial u_1}{\partial y}v_3 -
	\frac{\partial v_3}{\partial y}u_1 + \frac{\partial v_2}{\partial z}u_1 -
	\frac{\partial v_1}{\partial z}u_2 + \frac{\partial v_3}{\partial x}u_2 -
	\frac{\partial v_2}{\partial x}u_3 + \frac{\partial v_1}{\partial y}u_3
	\]
	\\

	\[ = \frac{\partial u_2}{\partial x}v_3 + \frac{\partial v_3}{\partial x}u_2 - \left( \frac{\partial u_3}{\partial x}v_2 +  \frac{\partial v_2}{\partial x}u_3\right) + \frac{\partial u_3}{\partial y}v_1 + \frac{\partial v_1}{\partial y}u_3 - \left( \frac{\partial u_1}{\partial y}v_3 + \frac{\partial v_3}{\partial y}u_1 \right) + \frac{\partial u_1}{\partial z}v_2 + \frac{\partial v_2}{\partial z}u_1 \]\\\[+ \left( \frac{\partial u_2}{\partial z}v_1 + \frac{\partial v_1}{\partial z}u_2\right)
	\]\\
	by the chain rule,\\
	\[ =\frac{\partial}{\partial x}(u_2v_3 - u_3v_2) + \frac{\partial}{\partial y}(u_3v_1 - u_1v_3) +
	\frac{\partial}{\partial z}(u_1v_2 - u_2v_1)
	\]\\

	\[ \therefore \triangledown \cdot (\mathbf{u} \times \mathbf{v}) = (\triangledown \times \mathbf{u}) \cdot \mathbf{v} - \mathbf{u} \cdot (\triangledown \times \mathbf{v})
	\]
	\\\\
	\subsection{Part 2}
	Show that $\langle\mathbf{u},\triangledown \times \mathbf{v}\rangle = \langle\triangledown \times \mathbf{u},\mathbf{v}\rangle + \iint_{\partial \Omega}
	\mathbf{w} \cdot dS$ for some $\mathbf{w}$.
	\\\\
	From Part 1, $\overline{\mathbf{u}} \cdot (\triangledown \times \mathbf{v}) = (\triangledown \times \overline{\mathbf{u}}) \cdot \mathbf{v} - \triangledown \cdot (\overline{\mathbf{u}} \times \mathbf{v})$
	\\\\
	\[ \int_{\Omega}\overline{\mathbf{u}} \cdot (\triangledown \times \mathbf{v}) = \int_{\Omega}(\triangledown \times \overline{\mathbf{u}}) \cdot \mathbf{v} - \int_{\Omega} \triangledown \cdot (\overline{\mathbf{u}} \times \mathbf{v})
	\]\\
	From the definition of the inner product,
	\\
	\[\langle\mathbf{u},\triangledown \times \mathbf{v}\rangle = \langle\triangledown \times \mathbf{u},\mathbf{v}\rangle - \int_{\Omega} \triangledown \cdot (\overline{\mathbf{u}} \times \mathbf{v})
	\]\\
	Using the divergence theorem,
	\\
	\[\langle\mathbf{u},\triangledown \times \mathbf{v}\rangle = \langle\triangledown \times \mathbf{u},\mathbf{v}\rangle + \iint_{\partial \Omega} (\overline{\mathbf{u}} \times \mathbf{v}) \cdot dS.
	\]
	\\
	Thus, $\mathbf{w} = \overline{\mathbf{u}} \times \mathbf{v}$, and we are done.
	\\
	\subsection{Part 3}
	Give a possible boundary condition on the vector space such that $\triangledown\times$ is self-adjoint with this inner product.
	\\\\
	If $\langle \mathbf{u} ,\triangledown\times \mathbf{v} \rangle = \langle \triangledown\times \mathbf{u}, \mathbf{v} \rangle$ then $\triangledown\times$ is self-adjoint. For this to be true,\\ $\iint_{\partial\Omega} (\overline{\mathbf{u}}\times \mathbf{v})\cdot dS = 0$.
	\\

	\noindent
	Thus, with the boundary condition $\overline{\mathbf{u}} \times \mathbf{v} = 0$, the above equality will be true and $\triangledown\times$ will be self-adjoint.
	\\
	\subsection{Part 4}
	Show that $\triangledown\times\triangledown\times$ is self-adjoint for this inner product under \textit{either} some boundary condition on $\mathbf{u}$ or some boundary condition on the \textit{derivatives} of $\mathbf{u}$. Is it positive or negative definite or semi-definite?
	\\\\
	If $\langle \mathbf{u},\triangledown\times\triangledown\times\mathbf{v}\rangle = \langle \triangledown\times\triangledown\times\mathbf{u},\mathbf{v}\rangle$ then $\triangledown\times\triangledown\times$ is self-adjoint.
	\\
	\[
	\langle \mathbf{u},\triangledown\times\triangledown\times \mathbf{v}\rangle = \langle \triangledown\times\mathbf{u},\triangledown\times\mathbf{v}\rangle + \iint_{\partial\Omega} (\overline{\mathbf{u}} \times \triangledown\times \mathbf{v}) \cdot dS
	\]
	\[
	\langle\mathbf{u},\triangledown\times\triangledown\times \mathbf{v}\rangle = \langle \triangledown\times\triangledown\times\mathbf{u}, \mathbf{v}\rangle + \iint_{\partial\Omega}(\triangledown\times\overline{\mathbf{u}}\times\mathbf{v})\cdot dS + \iint_{\partial\Omega} (\overline{\mathbf{u}} \times \triangledown\times \mathbf{v}) \cdot dS
	\]\\
	If $\mathbf{u}\times \mathbf{n}|_{\partial\Omega} = 0$ or $\triangledown\times\mathbf{u}\times\mathbf{n}|_{\partial\Omega}=0$, then $\triangledown\times\triangledown\times$ is self-adjoint.
	\\\\
	$\langle\mathbf{u},\triangledown\times\triangledown\times\mathbf{u}\rangle = \langle\triangledown\times\mathbf{\overline{u}},\triangledown\times\mathbf{u}\rangle = \int_{\Omega}|\triangledown\times\mathbf{u}|^2 \geq 0$, thus it is positive semi-definite.
	\subsection{Part 5}
	Two of Maxwell's equations are $\triangledown\times \mathbf{E} = -\frac{\partial\mathbf{B}}{\partial t}$ and $\triangledown\times\mathbf{B} = \frac{1}{c^2}\frac{\partial\mathbf{E}}{\partial t}$. Take the curl of both sides of the first equation to obtain a PDE in $\mathbf{E}$ alone. Suppose that $\Omega$ is the interior of a hollow metal container, where the boundary conditions are that $\mathbf{E}$ is perpendicular to the metal at the surface. Combining these facts with the previous parts, explain why one would expect \textit{oscillating} solutions to Maxwell's equations.
	\\\\
	\[
	\triangledown\times\triangledown\times\mathbf{E} = \triangledown\times -\frac{\partial \mathbf{B}}{\partial t} = -\frac{1}{c^2}\frac{\partial^2 \mathbf{E}}{\partial t^2}
	\]
	\[
	\frac{\partial^2\mathbf{E}}{\partial t^2} =  \hat{A}\mathbf{E}
	\]
	\noindent
	where $\hat{A}=-c^2 \triangledown\times\triangledown\times$. Since $\mathbf{E}$ is normal to the surface, Part 4 gives that $\hat{A}$ is self-adjoint and negative semi-definite. Thus, the PDE is a Hyperbolic equation with eigenvalues $\lambda\leq0$, and thus, oscillating solutions.
	\\\\\\
	\section{Problem 2}
	Solve for the 2-D eigenfunctions of $\triangledown^2$ in an annular region $\Omega$ that \textit{does not contain the origin}, so that you will need both $J_m$ and $Y_m$ solutions to Bessel's equation. The separation of variables $u(r,\Theta) = \rho(r)\tau(\Theta)$ leads to functions $\tau(\Theta)$ spanned by $\sin(m\Theta)$ and $\cos(m\Theta)$ and functions $\rho(r)$ that satisfy Bessel's equation. That is, the eigenfunctions are of the form:
	\[ u(r,\Theta) = [\alpha J_m(kr) + \beta Y_m(kr)] \times [A\cos(m\Theta) + B\sin(m\Theta) \]
	for arbitrary constants $A$ and $B$, integers $m = 1,2,\dots$, and constants $\alpha$, $\beta$ and $k$, which must be determined.

	\noindent
	The boundary conditions are Neumann boundary condition $\frac{\partial u}{\partial r} = 0$ at R1 and R2.
	\\
	\subsection{Part 1}
	Using the boundary conditions, write two equations for $\alpha$, $\beta$, and $k$, of the form $E\left(
	\begin{matrix}
		\alpha \\ \beta
	\end{matrix}
	\right) = 0$ for some $2\times 2$ matrix $E$. This only has a solution when the determinant is 0. Use this fact to obtain a single equation for $k$ of the form $f_m(k) =0$ for some function $f_m$ that depends on $m$. In terms of $k$, write down a possible expression for $\alpha$, $\beta$.
	\\\\
	\[
		\frac{\partial u}{\partial r} = \alpha J_m'(kr) + \beta Y_m'(kr) = 0|_{r=R_1,R_2}
	\]
	\\
	\[
	E\left[
	\begin{matrix}
	\alpha \\ \beta
	\end{matrix}
	\right] =
	\left[\begin{matrix}
		J_m'(kR_1) & Y_m'(kR_1)\\
		J_m'(kR_2) & Y_m'(kR_2)
	\end{matrix}\right]
	\left[
	\begin{matrix}
		\alpha \\ \beta
	\end{matrix}\right] = \mathbf{0}
	\]
	\\
	\[
	f_m(k) = \det E = J_m'(kR_1)Y_m'(kR_2) - J_m'(kR_2)Y_m'(kR_1) = 0
	\]
	\\
	\[
	\frac{\partial u}{\partial r}|_{r=R_1} = \frac{\partial u}{\partial r}|_{r=R_2} = 0
	\]
	\\
	\[
	\alpha J_m'(kR_1)+\beta Y_m'(kR_1) = \alpha J_m'(kR_2)+\beta Y_m'(kR_2) = 0
	\]
	\\
	\[
	\beta = \alpha \frac{J_m'(kR_1)}{Y_m'(kR_1)} = \alpha \frac{J_m'(kR_2)}{Y_m'(kR_2)}
	\]
	\\
	\subsection{Part 2}
	Assuming $R1 = 1, R2=2$, plot $f_m(k)$ vs $k \in [0,20]$ for $m=0,1,2$.
	\\
	\textit{see notebook.}
	\\
	\subsection{Part 3}
	For $m=0$, find the first three (smallest $k>0$)  solutions to $f_0(k) = 0$.
	\\
	\textit{see notebook.}
	\\
	\subsection{Part 4}
	Since $\triangledown^2$ is self-adjoint, the eigenfunctions must be orthogonal. Check that the solutions from Part 3 are orthogonal.
	\\
	\textit{see notebook.}
	\\
	\subsection{Part 5}
	Let the operator $\hat{A}$ now be $c(r)\triangledown^2$ with $c(r)=2$ for $r<R_1$ and $c(r)=1$ for $r\geq R_1$. Impose Dirichlet boundary conditions $u(R_2)=0$. What is the form of the eigenfunctions? If we solve for eigenfunctions $\hat{A}u = \lambda u$, with $u$ finite everywhere, what conditions must $u$ satisfy at $r=R_1$ for $\hat{A}u$ to be well-defined and finite? Write down a condition $f_m(k)=0$ that must be satisfied in order for the above equation to have a solution. The roots of this function give the eigenvalues.
	\\\\
	The eigenfunctions are of the form:
	\[
	u(r,\theta) = [A\cos(m\theta) + B\sin(m\theta)] \times \left\lbrace
	\begin{matrix}
		\alpha J_m(k_1r) & r<R_1\\
		\beta J_m(k_2r) + \gamma Y_m(k_2r) & r>R_1
	\end{matrix}\right.
	\]
	with $k_1 = k_2/\sqrt{2}$.\\

	If $\hat{A}$ is to be well defined and finite, then
	\[
	\alpha J_m(kR_1/\sqrt{2}) = \beta J_m(kR_1)+\gamma Y_m(kR_1)
	\]
	and
	\[
	\alpha J_m'(kR_1/\sqrt{2}) = \beta J_m'(kR_1) + \gamma Y_m'(kR_1)
	\]
	paired with the Dirichlet boundary conditions, this gives the system,
	\[
	\left[
	\begin{matrix}
		-J_m(kR_1/\sqrt{2}) & J_m(kR_1) & Y_m(kR_1)\\
		-J_m'(kR_1/\sqrt{2}) & J_m'(kR_1) & Y_m(kR_1)\\
		0 & J_m(kR_2) & Y_m(kR_2)
	\end{matrix}\right]
	\left[
	\begin{matrix}
		\alpha \\ \beta \\ \gamma
	\end{matrix}
	\right]= E_m(k)
	\left[
	\begin{matrix}
		\alpha \\ \beta \\ \gamma]
	\end{matrix}
	\right] = \mathbf{0}
	\]
	$f_m(k)$ is thus $\det E_m(k)$.

	\section{Problem 3}
	The Bessel functions $u(x) = J_m(kx)$, solve the eigenproblem:
	\[
	\hat{A}u = u'' + \frac{u'}{r} - \frac{m^2}{r^2}u = -k^2 = \lambda u
	\]
	on $[0,R]$ where $u(R)=0$ and $u(0)=0$ for $m>0$.
	\\
	\subsection{Part 1}
	Show that $\hat{A}$ is of the form of a Sturm-Louville operator and is therefor self-adjoint for a particular inner product.
	\\
	\subsection{Part 2}
	Show that $\hat{A}$ is negative definite.
	\\
	\subsection{Part 3}
	Write a center-difference discretization of the operator $\hat{A}$ for $u_n = u(n\Delta x)$ with $m=1,\dots,R \Delta x = \frac{R}{N+1}$.
	\\
	\subsection{Part 4}
	In Julia, for the matrix approximation $A$ of $\hat{A}$ for $m=1$ (with $N=100$, $R=1$). Compare its smallest-magnitude eigenfunction to $J_1(k_{1,1}r/R)$ where $k_{1,1}$ is the first root of $J_1$.

\end{document}


\section{Directional Derivatives}
\noindent
We already saw partial derivatives in the $x$, $y$, and $z$ directions.
However, we can take the derivative coming from other directions.
These are called directional derivatives, $D_{\hat{u}}{f}$, where $\hat{u}$ is the direction.
\begin{equation*}
	D_{\hat{u}}{f} = \lim_{h \to 0}{\frac{f(\vec{p_0} + h\hat{u})}{h}}
\end{equation*}
\noindent
If $\hat{u} = \langle a, b \rangle$,
\begin{equation*}
	D_{\hat{u}}{f} = \lim_{h \to 0}{\frac{f(x+ah, y+bh) - f(x,y)}{h}}.
\end{equation*}
Note that
\begin{equation*}
	D_{\hat{i}}{f} = \lim_{h \to 0}{\frac{f(x+h, y) - f(x, y)}{h}} = f_x \text{ and } D_{\hat{j}}{f} = f_y.
\end{equation*}
Let's look at $D_{\hat{u}}{f}$.
\begin{align*}
	D_{\hat{u}}{f} &= \lim_{h \to 0}{\frac{f(x+ah, y+bh) - f(x+ah, y) + f(x+ah, y) - f(x, y)}{h}} \\
	&= b\lim_{h \to 0}{\frac{f(x+ah, y+bh)-f(x+ah, y)}{bh}} + a\lim_{h \to 0}{\frac{f(x+ah, y) - f(x, y)}{ah}} \\
	&= b\frac{\partial f}{\partial y} + a\frac{\partial f}{\partial x} \\
	&= af_x + bf_y.
\end{align*}
So,
\begin{equation*}
	D_{\hat{u}}{f} =\nabla f \cdot \hat{u}.
\end{equation*}


\lab{B-Splines}{B-Splines}
Though B\'{e}zier curves are good for a variety of things, they have certain limitations.
\begin{itemize}
\item As the number of control points increases it becomes very expensive to compute the points on the curve.
\item Changes in the placement of a single control point affect the shape of the entire curve.
\item Since a change in any control point affects the entire curve, it is necessary to recompute the entire curve to account for a change in a single control point.
\item As the number of points increases, individual points have progressively less affect on the portions of the curve that lie nearest to them.
\end{itemize}

B-Splines are an ideal way to answer these limitations.
A B-Spline is somewhat similar to a piecewise B\'{e}zier curve.

\section*{B-Spline Basis Functions}

In the previous lab we introduced a way to represent B\'{e}zier curves as a linear combination of Bernstein Polynomials.
We discussed the convenient property that the coefficient of each Bernstein polynomial for the B\'{e}zier curve formed from a given control point is simply the control point itself.
This is a useful property.

\begin{figure}
\includegraphics[width=\textwidth]{bernstein_basis}
\caption{5th degree Bernstein basis functions}
\end{figure}

B-splines are a generalization of B\'{e}zier curves which allow us to use piecewise basis functions.
We usually choose basis functions that are zero on most of the domain so that, at any given parameter value, we only have to consider a few of the basis functions to compute the value of a spline
Choosing basis functions that are zero on significant portions of the domain makes it so that we do not have to recompute the entire curve when we change only one control point.
It also makes it so that the curve is easier to manipulate, since local changes in control points only change a portion of the spline curve.

We would like to be able to do this without loosing the useful properties of B\'{e}zier curves.
Using basis functions of any sort guarantees that each point of the curve will be a linear combination of the control points.
It would be best if we could make it so that this curve has the convex hull property (i.e. that it lies within the area bounded by the outermost control points).
B\'{e}zier curves also allow easy computation of their derivatives, so we would hope that B-Splines would allow this as well.
One of the largest constraints we need is that the curve we are forming be continuous with a given number of continuous derivatives.
The construction of the B-Spline Basis functions takes all of these factors into account.

There are a number of ways the B-Spline basis functions can be defined.
A common approach is to use recursion.
Let $t = \lbrace t_0, t_1, ... , t_l \rbrace$ be a nondecreasing sequence of real numbers.
$t$ is called the knot vector.
The $t_i$ are called the knots.
Note that each $t_i$ is not necessarily distinct.
$N_{i,k}(x)$, the $i$'th B-Spline basis function of degree $k$ is defined as:

\begin{equation*}
N_{i,0}(x) =
\begin{cases}
1 & \text{if } t_i \leq x < t_{i+1} \\
0 & \text{otherwise}
\end{cases}
\end{equation*}

\begin{equation*}
N_{i,k}(x) = \frac{x - t_i}{t_{i+k} - t_i} N_{i,k-1}(x) + \frac{t_{i + k + 1} - x}{t_{i + k + 1} - t_{i + 1}} N_{i+1,k-1}(x)
\end{equation*}

This is known as the Cox-De Boor recursion formula.
This algorithm is the De Boor algorithm.
When implemented properly, this algorithm is both fast and numerically stable.
Some simple basis functions are shown in Figure \ref{fig:bspline_basis}.

\begin{figure}
\includegraphics[width=\textwidth]{bspline_basis.pdf}
\caption{B-spline basis functions that are only nonzero on a portion of the given interval.}
\label{fig:bspline_basis}
\end{figure}

Finding the point of a spline at a given parameter value $x$ is done in the same way it was using the Bernstein basis for B-splines.
Where, $P_i$ are the control points, $N_{i,k}$ are the basis functions of degree $k$, and there are $n$ control points , we have that the spline $B$ of degree $k$ can be written
\[B\left(x\right) = \sum_{i=0}^n P_i N_{i,k}\left(x\right)\]

Notice that we defined $t$ to be nondecreasing, which means some $t_i$ may be repeated.
When programming this algorithm as it is currently written, be careful to avoid division by zero.
Whenever we get the indeterminate form $\frac{0}{0}$ or any other sort of division by $0$, we must replace it with zero.

Also note that $N_{i,0}$ is a step function which is zero except on $[t_i, t_{i+1})$.
The other basis functions are piecewise polynomials of degree $k$ that are only nonzero on $[t_{i-k}, t_{i+1+k})$.
This is nice because computation of a set of basis functions requires only a knot vector $t$ and a degree $k$, so we can predefine basis functions before we know the actual positions of the control points.

\begin{warn}
When performing the computation of each of the terms in the De Boor Algorithm, you should very careful about division by $0$.
Whenever division by $0$ occurs in either term, you should replace the value for \emph{that specific term} by $0$.
This should be done for each term independently so that, if you are using NumPy's float types, a value of \li{inf} or \li{nan} does not propagate through the recursion in the algorithm.
You can test to see if a floating point number has a value of \li{nan} or \li{inf} using the functions \li{math.isnan} and \li{math.isinf} or \li{numpy.isnan} and \li{numpy.isinf}.
This can also be done by checking to see if the denominator is $0$ before performing division.
\end{warn}

\begin{problem}
Use the De Boor algorithm to write a recursive python function to evaluate a b-spline basis function for some $x$ between the maximum and minimum of a given knot vector $t$.
\end{problem}

Upon considering the computation involved in the previous problem, we see that this recursion, particularly for higher order splines, involves a massive amount of repetitive calculation.
Many of the performance costs can be avoided by rewriting the algorithm using explicit loops and avoiding unnecessary computations.

\begin{comment}
% These comments and this problem could be one way to expand this lab later on.
% Depending on how long the lab is, we will probably want to give them pseudocode for this version of the algorithm.

First, notice that for splines of order $2$ and higher, we actually compute the values of some splines multiple times.
A simple way to avoid this is to figure out which of the 0-order splines we will actually use in our computation, compute them all, then compute all the needed splines of order 1, 2, etc.

There is also some redundant computation in the computation of the coefficients used at each stage of the recursion.
This can be eliminated by using good control structure and a temporary variable.

We will label the left and right coefficients in the formula $L$ and $R$ respectively, so we have $L(i, k, x) = \frac{x - t_i}{t_{i + k} - t_i}$ and $R(i, p, u) = \frac{t_{i + k + 1} - x}{t_{i + k + 1} - t_{i + 1}}$.
Notice that $L(i + 1, k, x) = 1 - R(i, k, x)$.
We can eliminate much of the duplicate computation by computing the new left hand side coefficient the iteration before we actually need it.
This avoids nearly all the repeated computation.

\begin{problem}
Write a function that uses loops instead of recursion to compute the values for all the b-spline basis functions of a given power $k$ for a given array of $t$ values.
Return the answer as a two dimensional array with the results for each polynomial stored in each of the rows of the array.
\end{problem}

It is worth noting that you can remove further excess computation when evaluating a single function and even further when evaluating a single function at a single point.
This can be done by figuring out in advance which of the $N_{i,k}$ will be nonzero and only iterating over those terms.
This approach may or may not be faster depending on the form of the problem.

\end{comment}

Scipy has some built in functions and a built in class for B-splines.
They are all part of the scipy.interpolate package.

As of SciPy 14, \li{scipy.interpolate} also includes the \li{BPoly} class that allows for easy computation of the basis functions for a B-spline.

Several of the functions built in to SciPy are wrappers around the Fortran package FITPACK.
One such example is the function \li{scipy.interpolate.splev}.
This function is used to evaluate a spline with knot vector $t$, basis function coefficients $c$, and degree $k$ at a set of points $x$.
The calling convention for this function is \li{splev(x, (t, c, k))}.
If $c$ is a multi-dimensional array, the function is applied to each row of $c$.
This is equivalent to using each column of $c$ as a control point in a higher dimension.

The package \li{scipy.interpolate} also includes several routines designed for easy interpolation with b-splines.
It also has routines designed for integration and differentiation of B-splines.

\begin{problem}
Use \li{scipy.interpolate.splev} to generate a plot of a b-spline with control points in $\mathbb{R}^2$ at equally spaced points along the edge of a circle.
Make your control points start and end at the point $\left(1, 0\right)$.
Generate the same plot with your own implementation of the De Boor's algorithm.

Use the following knot vector (where $m$ some positive integer and $k$ is the degree of the desired spline):
\begin{lstlisting}
t = np.array([0]*(k) + range(m) + [m]*(k+1))
\end{lstlisting}
This knot vector will give you $k + m$ different nonzero basis functions on the interval $\left[0, m\right]$.
You will have to include an extra column at the end of your array of control points since this knot vector gives an extra basis function that is equal to $0$.
\end{problem}


\subsection{Residual blocks}

In a node we have:

\(a_{ij}=\sigma_{ij}(W_{ij}a_{i-1})\)

That is, the value of a node, is the activation on the sum of the weights of the previous layer.

Residual block however look further back that one layer. They include the full data from an older layer (without weights)

\(a_{ij}=\sigma_{ij}(W_{ij}a_{i-1}+a_k)\)


\subsection{Vector spaces}

A vector space is a group with additional structure.

The operation for each element is shown as addition. So we can say:

\(\forall u,v \in V [u+v \in V]\)

To this we add scalars, from a field \(F\). We write this as multiplication.

\(\forall f \in F \forall v \in V [fv \in V]\)

\subsubsection{Subspace}

A subspace is a subset of \(V\) which still acts as a vector space. In practice, this means fewer dimensions.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CS624: Analysis of Algorithms
% Copyright 2015 Pejman Ghorbanzade <pejman@ghorbanzade.com>
% Creative Commons Attribution-ShareAlike 4.0 International License
% More info: https://github.com/ghorbanzade/beacon
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section*{Question 3}

Prove that the keys in any subtree form a contiguous sequence.

\subsection*{Solution}

Suppose tree $T'$ with root $r'$ is a subtree of the tree $T$ with root $r$ such that $r'$ is a child of $r$.
We claim that the keys in the subtree $T'$ construct a contiguous sequence.
In other words, sorted sequence of all keys of subtree $T'$ is itself a subsequence of sorted sequence of keys of tree $T$.

Proof is given by contradiction.
Assume that our initial claim is false.
This means there is an element $e$ in tree $T$ such that $n_1.key < e.key < n_2.key$ where $n_1$ and $n_2$ are two keys of subtree $T'$ and such that $e \notin T'$.

Following the assumption that $r'$ is a child of $r$, $r'$ is either the left child or the right child.
We now show that existence of the node $e$ in tree $T$ violates basic definition of the binary search tree.

\begin{itemize}[itemsep=1mm] \parskip=0pt \parsep=0pt
\item If $r'$ is the left child of $r$, $T'$ is a left subtree of $r$.
Therefore, definition of the binary search tree enforces that keys of all nodes in $T'$ are less than key of $r$.
Since $e \notin T'$, $e$ is either $r$ or in the right subtree of $r$.
\begin{itemize}[itemsep=1mm] \parskip=0pt \parsep=0pt
  \item[] If $e = r$, then there is a node $n_2$ such that $r.key < n_2.key$.
  \item[] If $e$ is in the right subtree, this means $e.key > r.key$.
In this case, $e.key < n_2.key$ leads to $r.key < n_2.key$.
\end{itemize}
And both cases violate the definition stating key of every node in left subtree $T'$ is less than $r.key$.
\item If $r'$ is the right child of $r$, $T'$ is a right subtree of $r$.
Therefore, definition of the binary search tree enforces that keys of all nodes in $T'$ are bigger than key of $r$.
Since $e \notin T'$, $e$ is either $r$ or in the left subtree of $r$.
\begin{itemize}[itemsep=1mm] \parskip=0pt \parsep=0pt
  \item[] If $e = r$, then there is a node $n_1$ such that $n_1.key < r.key$.
  \item[] If $e$ is in the left subtree, this means $e.key < r.key$.
In this case, $n_1.key < e.key$ leads to $n_1 < r.key$.
\end{itemize}
And both cases violate the definition stating keys of nodes in right subtree $T'$ are bigger than $r.key$.
\end{itemize}
Threfore, our initial assumption is false and the claim holds for all cases.


\documentclass[11pt, a4paper]{article}

\usepackage{amsmath}
\usepackage{booktabs}

\begin{document}

\title{NAIVE BAYES CLASSIFIER}
\date{}
\maketitle

Supervised learning algorithm based on the application of Bayes\textsc{\char13} theorem with the
``naive'' assumption of independence between every pair of features.

\section{Bayes' Theorem}

\begin{align*}
	\mathbf{P}(A\cap B) = \mathbf{P}(A|B) \times \mathbf{P}(B)                   \\
	= \mathbf{P}(B|A) \times \mathbf{P}(A)                                       \\
	                                                                             \\
	\mathbf{P}(A|B) = \frac{\mathbf{P}(B|A) \times \mathbf{P}(A)}{\mathbf{P}(B)}
\end{align*}

\section{Derivation}

\begin{align*}
	\mathbf{P}(\mathbf{Y}=y_k\ |\ x_1,\ x_2,\ ...\ x_p) = \frac{\mathbf{P}(x_1,\ x_2,\ ...\ x_p|\mathbf{Y}=y_k) \times \mathbf{P}(\mathbf{Y}=y_k)}{\mathbf{P}(x_1,\ x_2,\ ...\ x_p)} \\
\end{align*}

Choose $y_k$ which maximizes $\mathbf{P}(\mathbf{Y}=y_k | x_1,\ x_2,\ ...\ x_p)$.

\begin{align*}
	\mathbf{Y} = \operatorname*{argmax}_{y_k}\left\{
	\begin{array}{ll}
	\frac{\mathbf{P}(x_1,\ x_2,\ ...\ x_p | \mathbf{Y}=y_k) \times \mathbf{P}(\mathbf{Y=y_k)}}{\mathbf{P}(x_1,\ x_2,\ ...\ x_p)} \\
	\end{array}
	\right.                                                                                \\
\end{align*}

Since $\mathbf{P}(x_1,\ x_2,\ ...\ x_p)$ is constant for all $y_k$, the above can be written as follows:

\begin{align*}
	\mathbf{Y} = \operatorname*{argmax}_{y_k}\left\{
	\begin{array}{ll}
	\mathbf{P}(x_1,\ x_2,\ ...\ x_p | \mathbf{Y}=y_k) \times \mathbf{P}(\mathbf{Y=y_k)} \\
	\end{array}
	\right.                                                                             \\
\end{align*}

The ``naive'' assumption that all $\mathbf{X_1},\ \mathbf{X_2},\ ...\ \mathbf{X_p}$ are independent allows for the following simplification.

\begin{align*}
	\mathbf{Y} = \operatorname*{argmax}_{y_k}\left\{
	\begin{array}{ll}
	\prod_{i} \mathbf{P}(x_i|\mathbf{Y}=y_k) \times \mathbf{P}(\mathbf{Y=y_k)} \\
	\end{array}
	\right.                                                                    \\
\end{align*}

All the terms on the right are easy to calculate.

\section{Example}

\begin{table}[h!]
	\centering
	\caption{Training data}
	\label{tab:table1}
	\begin{tabular}{c|cccc|c}
		\toprule
		\textbf{Sr.} & \textbf{Outlook} & \textbf{Temperature} & \textbf{Humidity} & \textbf{Windy} & \textbf{PlayGolf} \\
		\midrule
		1            & Rainy            & Hot                  & High              & False          & No                \\
		2            & Rainy            & Hot                  & High              & True           & No                \\
		3            & Overcast         & Hot                  & High              & False          & Yes               \\
		4            & Sunny            & Mild                 & High              & False          & Yes               \\
		5            & Sunny            & Cool                 & Normal            & False          & Yes               \\
		6            & Sunny            & Cool                 & Normal            & True           & No                \\
		7            & Overcast         & Cool                 & Normal            & True           & Yes               \\
		8            & Rainy            & Mild                 & High              & False          & No                \\
		9            & Rainy            & Cool                 & Normal            & False          & Yes               \\
		10           & Sunny            & Mild                 & Normal            & False          & Yes               \\
		11           & Rainy            & Mild                 & Normal            & True           & Yes               \\
		12           & Overcast         & Mild                 & High              & True           & Yes               \\
		13           & Overcast         & Hot                  & Normal            & False          & Yes               \\
		14           & Sunny            & Mild                 & High              & True           & No                \\
		\bottomrule
	\end{tabular}
\end{table}

\noindent The following tables are calculated.

\begin{table}[h!]
	\centering
	\label{tab:table2}
	\begin{tabular}{c|cc}
		\toprule
		\textbf{Outlook} & \textbf{Yes} & \textbf{No} \\
		\midrule
		Rainy            & 2            & 3           \\
		Overcast         & 4            & 0           \\
		Sunny            & 3            & 2           \\
	\end{tabular}
	\hspace{2em}
	\vspace{2em}
	\label{tab:table3}
	\begin{tabular}{c|cc}
		\toprule
		\textbf{Temperature} & \textbf{Yes} & \textbf{No} \\
		\midrule
		Hot                  & 2            & 2           \\
		Mild                 & 4            & 2           \\
		Cool                 & 3            & 1           \\
	\end{tabular}
	\label{tab:table4}
	\begin{tabular}{c|cc}
		\toprule
		\textbf{Humidity} & \textbf{Yes} & \textbf{No} \\
		\midrule
		High              & 3            & 4           \\
		Normal            & 6            & 1           \\
	\end{tabular}
	\hspace{2em}
	\label{tab:table5}
	\begin{tabular}{c|cc}
		\toprule
		\textbf{Windy} & \textbf{Yes} & \textbf{No} \\
		\midrule
		False          & 6            & 2           \\
		True           & 3            & 3           \\
	\end{tabular}
\end{table}

If $X = (Sunny, Hot, Normal, False)$, then

\begin{align*}
	\mathbf{PlayGolf} = \operatorname*{argmax}_{y_k}\left\{
	\begin{array}{ll}
	\mathbf{P}(\textbf{Outlook}=Sunny|\textbf{PlayGolf}=Yes)         \\
	\times\mathbf{P}(\textbf{Temprature}=Hot|\textbf{PlayGolf}=Yes)  \\
	\times\mathbf{P}(\textbf{Humidity}=Normal|\textbf{PlayGolf}=Yes) \\
	\times\mathbf{P}(\textbf{Windy}=False|\textbf{PlayGolf}=Yes)     \\
	\times\mathbf{P}(\textbf{PlayGolf}=Yes)                          \\
	                                                                 \\
	\mathbf{P}(\textbf{Outlook}=Sunny|\textbf{PlayGolf}=No)          \\
	\times\mathbf{P}(\textbf{Temprature}=Hot|\textbf{PlayGolf}=No)   \\
	\times\mathbf{P}(\textbf{Humidity}=Normal|\textbf{PlayGolf}=No)  \\
	\times\mathbf{P}(\textbf{Windy}=False|\textbf{PlayGolf}=No)      \\
	\times\mathbf{P}(\textbf{PlayGolf}=No)                           \\
	\end{array}
	\right.
\end{align*}

\begin{align*}
	\mathbf{PlayGolf} = \operatorname*{argmax}_{y_k}\left\{
	\begin{array}{ll}
	3/9                                                     \\
	\times2/9                                               \\
	\times6/9                                               \\
	\times6/9                                               \\
	\times9/14                                              \\
	                                                        \\
	2/5                                                     \\
	\times2/5                                               \\
	\times1/5                                               \\
	\times2/5                                               \\
	\times5/14                                              \\
	\end{array}
	\right.
\end{align*}

\begin{align*}
	\mathbf{PlayGolf} = \operatorname*{argmax}_{y_k}\left\{
	\begin{array}{ll}
	0.0212
	                                                        \\
	0.0046                                                  \\
	\end{array}
	\right.
\end{align*}

Therefore, $\textbf{PlayGolf}=Yes$ because $0.0212 > 0.0046$.

\section{Extensions}

\begin{itemize}
	\item For a continuous input feature, assumption regarding the distribution needs to be made. Examples: \textbf{Gaussian}, \textbf{Multinomial} and \textbf{Bernoulli}.
	\item Smoothing may be required to prevent the multiplication from being zero when one probability term is zero.
\end{itemize}

\section{Comments}
\begin{itemize}
	\item Due to independence assumption, naive Bayes' classifiers often perform good even with less training data.
	\item Main applications include \textbf{spam filtering} and \textbf{document classification}.
	\item Extremely fast in both training and prediction.
	\item Often fail to produce a good estimate of the correct class probabilities but make the correct classification if the correct class is more probable than any other class.
\end{itemize}
\end{document}


\paragraph{\underline{Hypothesis tests for goodness-of-fit}} \hspace{0pt}

\vspace{10pt} \noindent \textbf{\em Pearson chi-square test}
$X$ i.i.d. $F=?$, $F_0$ is dist. of interest, $H_0: F=F_0$

$d_i= \frac{N_i - np_i}{\sqrt{np_i}}$,
$p_i = P_{F_0}(X \in S_i) > 0$

\[ T = \Sigma_{i=1}^r d_i^2 \rightarrow_{n \rightarrow \infty} \chi^2_{r-1-t} \]

\vspace{10pt} \noindent \textbf{\em Kolmogorov test} assumes that the sample comes from a continuous distribution.

\[ T = \sup_{x \in \mathbb{R}}{ \left| \hat{F}_n(x) - F_0(x) \right| } \]

\[ T^+ = \max_{i}{ \left( \frac{i}{n} - \hat{F}_0(x_{i:n}) \right) } \]

\[ T^- = \max_{i}{ \left( \hat{F}_0(x_{i:n}) - \frac{i-1}{n} \right) } \]

where $i \in \mathbb{N}$, $i \in [1,n]$ and $x_{i:n}$ is the $i$th sample element taken from sorted sample

\[ T = \max{ \left\{ T^+, T^- \right\} } \]

\paragraph{\underline{Hypothesis tests for normality}} \hspace{0pt}

\vspace{10pt} \noindent \textbf{\em Shapiro-Wilk test}
\begin{gather*}
W = \frac{ \left( \sum_{i=1}^{n} a_i x_{i:n} \right)^2 }{ \sum_{i=1}^{n} \left( x_i - \bar{x} \right)^2 } \\
a_i = \frac{m^T V^{-1}}{ \sqrt{ m^T V^{-1} V^{-1} m } }
\end{gather*}

where $m$ is a vector of expected values of sorted sample from $\distnormal(0,1)$, and $V$ is a covariance matrix of $m$.

Null hypothesis is rejected when $W$ is too small. This test is designed for small sample sizes, $n < 50$.

\paragraph{\underline{Hypothesis tests for independence}} \hspace{0pt}

\vspace{10pt} \noindent \textbf{\em Wilcoxon signed-rank test}
\begin{gather*}
W = \frac{1}{2}\Sigma_{i=1}^n \bar{r}(Z_i)+\frac{n(n + 1)}{4}
\end{gather*}


\iffalse
Noether's theorem says that continuous symmetries of physical systems gives rise to conservation laws. In this class we'll see some examples of low dimensional Lie groups and how they give rise to various phenomenon in physics like time dilation and length contraction in special relativity, spin states of electrons.

Keywords: bilinear forms, signature, SO(2), SO(3), Spin, SO(1,3), Minkowski space and relativity, Noether's theorem, Lie groups.

Prereqs: Linear algebra, Group theory
Homework: Recommended
\fi

\input{../preamble}

\DeclareMathOperator{\aut}{Aut}

\begin{document}
\title{Linear Groups}
\author{Apurva Nakade}
\thispagestyle{fancy}
\maketitle

Any mathematical object naturally defines a group $\aut(X)$, the group of \textbf{automorphisms} or symmetries of $X$ which are maps $X \rightarrow X$ preserving {some structure} on $X$. Our main examples for $X$ will be vector spaces over $\R$ or $\C$ (with  additional structures) in which case the automorphisms groups are called \textbf{linear groups}. We'll make the structures successively more rigid thereby specializing to smaller and smaller linear groups.

\section*{Symmetries of $\R^1$}
Symmetries of $\R^1$ are transformations $f: \R^1 \rightarrow \R^1$ which preserve some structure. The simplest structure $\R^1$ has is that of a set. The symmetries of $\R^1$ thought of as a set are bijective maps $f: \R^1 \rightarrow \R^1$. This group is too big and lacks any interesting structure. $\R^1$ is also a topological space. Symmetries of the topological space $\R^1$ are continuous maps $f:\R^1 \rightarrow \R^1$ which are isomorphisms.

From an algebraic point of view $\R^1$ is a vector space over $\R$. Symmetries of the vector space $\R^1$ are linear transformations $f:\R^1 \rightarrow \R^1$ which are isomorphisms. A linear transformation $f$ is simply multiplication by a scalar $c \in \R$ and such a transformation is an isomorphism if $c \neq 0$. So the set of symmetries of the vector space $\R^1$ is isomorphic to the group of non-zero reals $\R^{\times}$. This group is usually denoted by $GL_1(\R)$.

Inside $\R^{\times}$ is the subgroup of positive reals $\R _ {> 0}$. These are the orientation preserving transformations of $\R^1$ (see Section \ref{sec:orientation}). Another structure that $\R^1$ has is that of distance. Multiplication by a scalar $c$ preserves distances iff $c = \pm 1$, and the set $\{ -1, +1 \}$ is isomorphic to $\Z/2$. Finally only the identity map preserves both the orientation and the inner product and so the corresponding automorphism group is trivial.

\begin{center}
	\begin{tabular}{ll}
		Structures on $\R^1$ & Automorphism Groups   \\\hline
		Set                  & Bijections            \\
		Topological space    & Continuous bijections \\
		Vector space         & $\R^{\times}$         \\
		Orientation          & $\R_{>0}$             \\
		Metric               & $\Z/2$                \\
		Orientation + Metric & trivial group
	\end{tabular}
\end{center}

\section{General linear groups}

Denote the set of $n \times n$ matrices with entries in $\R$ (resp. $\C$) by $M_{n \times n}(\R)$ (resp. $M_{n \times n}(\C)$).

For the vector space $\R^n$ the automorphism group is called the \textbf{general linear group} $GL_n(\R)$ i.e. $GL_n(\R)$ is the group of invertible $n \times n$ matrices with real entries. Consider an $n \times n$ matrix $A \in GL_n(\R)$ and let $e_1, e_2, \ldots , e_n$ be the columns of $A$. Because $M$ is invertible $e_1, e_2, \ldots , e_n$ form a basis for $\R^n$. Conversely any basis $e_1, e_2, \ldots , e_n$ gives an $n \times n$ invertible matrix.
\begin{align}
	GL_n(\R)
	  & = \{ n \times n \mbox{ invertible matrices with entries in } \R \}             \\
	  & = \{ A \in M_{n \times n}(\R) : \det A \neq 0\}                                \\
	  & = \{ (e_1 , e_2 , \cdots , e_n) : (e_i)_{i=1}^n \mbox{ is a basis for } \R^n\}
\end{align}

\section{Orientation}
\label{sec:orientation}
The determinant of an invertible matrix is non-zero. Further the determinant satisfies the property that for two $n\times n$ matrices $A$, $B$ we have $\det (AB) = \det A \cdot \det B$. Hence we have a group homomorphism
\begin{align}
	\det : GL_n(\R) \rightarrow \R^{\times}
\end{align}
The preimage of positive reals $\R_{>0}$ under this map is denoted by $GL_n^+(\R) := \det^{-1}(\R_{>0})$. It is easy to see that this is a subgroup of $GL_n(\R)$. We can decompose $GL_n(\R)$ into cosets
\begin{align}
	GL_n(\R) = {\det} ^{-1}(\R_{>0}) \sqcup {\det} ^{-1}(\R_{<0})
\end{align}
This breaks up the set of all bases of $\R^n$ into two. These are called the \textbf{orientation classes} of bases of $\R^n$. The bases in the orientation class containing the standard basis are said to have the \textbf{standard orientation} and the bases which are not in this orientation class are said to have the \textbf{reverse orientation}.

The matrices in $GL_n^+(\R)$ are called \textbf{orientation preserving} matrices. In the language of automorphisms $GL_n^+(\R)$ is the automorphism group of $\R^n$ with a chosen orientation class.

\section{Special linear groups}

Sitting inside $GL_n(\R)$ is the subgroup of $n\times n$ matrices with determinant 1 denoted by $SL_n(\R)$, called the \textbf{special linear group}.
\begin{align}
	SL_n(\R) = \{ A \in M_{n \times n}(\R) : \det A = 1 \}
\end{align}
We can realize $SL_n(\R)$ as volume preserving automorphisms of $\R^n$ i.e. if $A \in SL_n(\R)$ and $X$ is a subset of $\R^n$ then the volume of $X$ is the same as the volume of $AX$. This is because if $X$ is a `parallelogram' in $\R^n$ then the volume of $X$ is exactly the absolute value of the determinant of $X$, and any other shape can be approximated by `parallelograms'.

The converse is almost true. A volume preserving automorphism of $\R^n$ should have determinant $\pm 1$, hence $SL_n(\R)$ is the group of automorphism of $\R^n$ which preserve both {volume} and {orientation}.

\section{Orthogonal groups}
On $\R^n$ we have the natural notion of distance and we can look at linear autmorphisms of $\R^n$ which preserve distances. We know from Euclidean geometry that any transformation that preserves distances also preserves angles. Angles and lengths in $\R^n$ can be measured using the dot product, this turns out to be the structure that is easiest to manipulate.

Denote by $(\cdot)$ the standard dot product between vectors in $\R^n$ defined as: for $ x = [x_1, x_2, \cdots, x_n]^T$ and $ y = [y_1, y_2, \cdots, y_n]^T$ the dot product is
\begin{align}
	x \cdot  y := \sum \limits _{i=1}^n x_i y_i =  x ^ T  y
\end{align}
Recall that the length of the vectors is given by $\norm{ x}^2 = { x.  x}$ and we can \emph{define} the angle between the vectors $ x$ and $ y$ by $\theta := \cos^{-1}\dfrac{ x \cdot  y}{\norm { x} \norm{ y}}$. The group of linear transformations of $\R^n$ which preserve the standard dot product is called the \textbf{orthogonal group} denoted $O(n)$.
\begin{align}
	O(n) := \{ A : \R^n \rightarrow \R^n : \innerp{A  x}{A  y} = \innerp{ x}{ y} \mbox{ for all }  x,  y \in \R^n\}
\end{align}

We can simplify the condition $\innerp{M  x}{A  y} = \innerp{ x}{ y}$ as follows:
\begin{alignat}{4}
	  &   & \innerp{M  x}{M  y}
	& = \innerp{ x}{ y}   \qquad \qquad\\
	\Longleftrightarrow \qquad
	  &   &
	(A  x)^T (A  y)
	& =  x ^ T  y         \qquad \qquad\\
	\Longleftrightarrow  \qquad
	  &   &
	x ^ T A^T A y
	& =  x ^ T  y         \qquad \qquad\\
	\Longleftrightarrow \qquad
	  &   &
	x ^ T (A^T A- I_{n}) y
	& = 0 \qquad \qquad
\end{alignat}
where $I _ {n}$ denotes the identity matrix of size $n \times n$. For an orthogonal matrix $A$ we want this to be true for all $ x,  y \in \R^n$. This is equivalent to requiring $A^T A - I_{n} = 0$ (Exercise \ref{thm:linAlgProb}) which gives an alternate definition of $O(n)$.

For an orthogonal matrix $A \in O(n)$ if $e_1, e_2, \cdots, e_n$ are the columns of $A$ then $e_i \cdot e_j = 0$ if $i \neq j$ and $e_i \cdot e_i = 1$, such a basis is called an \textbf{orthonormal basis} of $\R^n$.

\begin{align}
	O(n)
	  & = \{ A \in M_{n \times n}(\R) : \mbox{ for all } x, y \in \R^n \mbox{ we have }\innerp{x}{y} = \innerp{Ax}{Ay}	\} \\
	  & = \{ A \in M_{n \times n}(\R) : A^T A = I_{n}\}                                                                   \\
	  & = \{ (e_1 , e_2 , \cdots , e_n) : (e_i)_{i=1}^n \mbox{ is an orthonormal basis for } \R^n\}
\end{align}

\subsection{Special orthogonal groups}
The intersection $O(n) \cap GL_n^+(\R)$ is called the \textbf{special orthogonal group} denoted $SO(n)$, these are the matrices which preserve the standard inner product and orientation. As we'll see later for small positive integers $n$ the groups $SO(n)$ have nice descriptions.

\section{Complex matrix groups}
Everything that we did above can be generalized for vector spaces over complex numbers. We have natural generalizations of the vector spaces $GL_n(\C)$ and $SL_n(\C)$.

The generalization of $O(n)$ on the other hand needs a little tweaking. On complex vector spaces instead of a regular inner product $(.)$ we have a \textbf{hermitian inner product} defined by
\begin{align}
	\innerp{ x}{ y} & = \conj{x_1}{y_1} + \conj{x_2}{y_2} + \cdots + \conj{x_n}{y_n}
\end{align}
Matrices which preserve this structure are called \textbf{unitary matrices} and the columns of these form an orthonormal basis under the hermitian inner product.
\begin{align}
	U(n)
	  & = \{ A \in M_{n \times n}(\C) : \mbox{ for all } x, y \in \C^n \mbox{ we have }\innerp{x}{y} = \innerp{Ax}{Ay}	\} \\
	  & = \{ A \in M_{n \times n}(\C) : A^* A = I_{n}\}                                                                   \\
	  & = \{ (e_1 , e_2 , \cdots , e_n) : (e_i)_{i=1}^n \mbox{ is an orthonormal basis for } \C^n\}
\end{align}
where $A^*$ is the complex conjugate of transpose of $A$. There is also the \textbf{special unitary group} $SU(n)$ defined to be the group of unitary matrices with determinant 1 (see \ref{thm:detProb}).

We have a natural inclusion $\R^n \rightarrow \C^n$ induced by $\R \subseteq \C$. This induces a group homomorphism $\Phi: GL_n(\R) \hookrightarrow GL_n(\C)$. Furthermore the restriction of the standard hermitian bilinear on $\C^n$ to $\R^n$ is the standard inner product which implies that $\Phi^{-1}(U(n)) \cong O(n)$ and $\Phi^{-1}(SU(n)) \cong SO(n)$. There are also maps going in the other direction, see Exercise \\

To summarize we have the following hierarchy of matrices
\begin{align*}
	\xymatrix{
	SO(n) \ar@{^{(}->}[r] \ar@{^{(}->}[dr] & SL(n) \ar@{^{(}->}[r] & GL_n^+(\R) \ar@{^{(}->}[r] & GL_n(\R) \\
	& O(n) \ar@{^{(}->}[urr]
	}
	&&
	\xymatrix{
	SU(n) \ar@{^{(}->}[r] & U(n) \ar@{^{(}->}[r] & GL_n(\C)
	}
\end{align*}

\section{Exercises}

\begin{exercise}
	\label{thm:linAlgProb}
	For $A \in M_{n \times n}(\R)$ show that if $ x^T A  y = 0$ for all vectors $ x,  y \in \R^n$ then $A = 0$.
\end{exercise}

\begin{exercise}
	\label{thm:detProb}
	Find the images of $O(n)$, $SO(n)$, $GL_n(\C)$, $U(n)$ under the determinant map.
\end{exercise}

\begin{exercise}
	Does it make sense to define $GL_n^+(\C)$?
\end{exercise}

\begin{exercise}
	Identify the following quotients:
	\begin{enumerate}
		\item $GL_n(\R)/GL_n^+(\R)$
		\item $GL_n(\R)/SL_n(\R)$
		\item $GL^+_n(\R)/SL_n(\R)$
		\item $O(n)/SO(n)$
		\item $U(n)/SU(n)$
	\end{enumerate}
\end{exercise}

\begin{exercise}
	Find the possible eigenvalues for matrices in $O(n)$ and $U(n)$.
\end{exercise}

\begin{exercise}$\:$
	\begin{enumerate}
		\item Because $\C^n$ can be thought of as a vector space over $\R$ of dimension $2n$ and every $\C$ linear transformation is also $\R$ linear, we can define a map $\Psi:GL_{n}(\C) \rightarrow GL_{2n}(\R)$. Explicitly describe the map $\Psi$.
		\item Is $\Psi$ a group homomorphism?
		\item Describe the map $\Psi \circ \Phi$.
	\end{enumerate}

\end{exercise}

\begin{exercise}
	Is $O(n)$ a normal subgroup of $GL_n(\R)$? Read about the \textbf{QR-decomposition} of matrices and identify the set $GL_n(\R)/O(n)$. What structure(s) does this set have?
\end{exercise}

\begin{exercise}
	$M_{n \times n}(\R)$ is naturally isomorphic to $\R^{(n^2)}$, similarly for $\C$, so we can talk about connectedness of its subsets.
	\begin{enumerate}
		\item Show that $GL_n(\R)$ and $O(n)$ are not connected.
		\item Can you think of a way of showing that $GL_n^+(\R)$, $SO(n)$ and $GL_n(\C)$ are connected?
	\end{enumerate}
\end{exercise}

\end{document}

\iffalse
$SO(n)$ is precisely the kernel of the determinant map $\det : O(n) \rightarrow \{ -1, +1\}$ and so we have an isomorphism
\begin{align}
	O(n) / SO(n) \cong \Z/2
\end{align}

\raisebox{10pt}{\dbend} We think of elements of $SO(n)$ as rotations of $\R^n$ and the elements of the coset $O(n) \setminus SO(n)$ as reflections of $\R^n$. When $n>3$ these words do not agree with our standard notion of rotation as there are multiple axes around which rotations can occur independently.
\fi

\iffalse
As $\det A^T = \det A$ for orthogonal matrices $A \in O(n)$ we get
\begin{align}
	(\det A )^2 = \det(A^2) & = \det(A^T A) = \det (I_n) = 1 \\
	\implies \qquad \det A  & = \pm 1
\end{align}
This is a very useful property and we will exploit this to understand elements of $O(n)$ later.
\fi

03 Symmetries of Spaces/01 Linear Groups.tex


\section{Stochastic Collocation with Gamma Distribution}
Associated external model: \texttt{poly\_scgpc\_gamma.py}

Recall that the \textit{Gamma} distribution has the probability density function
\begin{align}
  f(x) = \frac{x^{\alpha-1}e^{-x/\beta}}{\beta^\alpha\Gamma\left(\alpha\right)}, \alpha > 0, \beta > 0
\end{align}

The following two polynomials are used to compute the analytic statistical moments:
\textit{Gamma} distribution:
\begin{align}
  u_1(x, y) = x + y \\
  u_2(x, y) = x^2 + y^2
\end{align}
where $x$ and $y$ are two mutually independent \textit{Gamma} variates, i.e.
\begin{align}
  x \thicksim \Gamma\left(\alpha_{1},\beta_{1}\right) \notag \\
  y \thicksim \Gamma\left(\alpha_{2},\beta_{2}\right) \notag
\end{align}

\subsection{Mean and Variance}
The first two statistical moments of $u_1(x,y)$ and $u_2{x,y}$ are:
\begin{align}
  \expv{u_1(x,y)} &= \int_{0}^\infty dxdyP\left(x,y\right)u_1\left(x,y\right), \notag \\
  &= \int_{0}^\infty dxdy \Gamma\left(\alpha_{1},\beta_{1}\right) \Gamma\left(\alpha_{2},\beta_{2}\right) u_1\left(x,y\right), \notag \\
  &= \frac{\alpha_{1}}{\beta_{1}} + \frac{\alpha_{2}}{\beta_{2}}
\end{align}
\begin{align}
  \expv{u_2(x,y)} &= \int_{0}^\infty dxdyP\left(x,y\right)u_2\left(x,y\right), \notag \\
  &= \int_{0}^\infty dxdy \Gamma\left(\alpha_{1},\beta_{1}\right) \Gamma\left(\alpha_{2},\beta_{2}\right) u_2\left(x,y\right), \notag \\
  &= \frac{\left(\alpha_{1} + 1\right)\alpha_1}{\beta_{1}^2} + \frac{\left(\alpha_{2}+1\right)\alpha_2}{\beta_{2}^2}
\end{align}

\begin{align}
  \text{var}[u_1(x,y)] &= \int_{0}^\infty dxdyP\left(x,y\right)\left[u_1\left(x,y\right) - \expv{u_1(x,y)}\right]^2, \notag \\
  &= \frac{\alpha_{1}}{\beta_{1}^2} + \frac{\alpha_{2}}{\beta_{2}^2}
\end{align}
\begin{align}
  \text{var}[u_2(x,y)] &= \int_{0}^\infty dxdyP\left(x,y\right)\left[u_2\left(x,y\right) - \expv{u_2(x,y)}\right]^2, \notag \\
  &= \frac{\left(4\alpha_{1} + 6.0\right)\left(\alpha_{1} + 1\right)\alpha_1}{\beta_{1}^4} + \frac{\left(4\alpha_{2} + 6.0\right)\left(\alpha_{2}+1\right)\alpha_2}{\beta_{2}^4}
\end{align}

\subsection{numeric values}
Some numeric values for the mean and variance are listed below for given distributions:
\begin{align}
  x \thicksim \Gamma\left(11, 5\right) \notag \\
  y \thicksim \Gamma\left(2, 0.8\right) \notag
\end{align}
\begin{table}[h!]
  \centering
  \begin{tabular}{c|c|c}
    $Function$ & mean & variance \\ \hline
    $u_1$ & 4.7    & 3.565 \\
    $u_2$ & 14.655 & 215.638125 \\
  \end{tabular}
\end{table}


\section{The Second Variation}
\subsection{The Legendre Condition}
By the Euler-Lagrange equation we can only find the stationary point(s) of a functional, but we do not know about whether they are a minimum, maximum or a saddle point.
When we are in $\mathbb R^n$, a (partial) solution to this problem is to look at the second (or higher) derivative, which inspires us to consider higher order variations.\\
Consider the functional
$$F[y]=\int_\alpha^\beta f(x,y,y^\prime)\,\mathrm dx$$
We now want to expand further to the second order term of $\epsilon$, that is, for $\eta$ such that $\eta(\alpha)=\eta(\beta)=0$ and $y$ a stationary point of $F$,
$$F[y+\epsilon\eta]-F[y]=\frac{\epsilon^2}{2}\int_\alpha^\beta\left( \eta^2\frac{\partial^2f}{\partial y^2}+(\eta^\prime)^2\frac{\partial^2f}{\partial(y^\prime)^2}+2\eta\eta^\prime\frac{\partial^2f}{\partial y\partial y^\prime} \right)\,\mathrm dx+O(\epsilon^3)$$
The integrand (as a functional on $\eta$) is called the second variation:
$$\delta^2 F[y](\eta)=\frac{1}{2}\int_\alpha^\beta\left( \eta^2\frac{\partial^2f}{\partial y^2}+(\eta^\prime)^2\frac{\partial^2f}{\partial(y^\prime)^2}+2\eta\eta^\prime\frac{\partial^2f}{\partial y\partial y^\prime} \right)\,\mathrm dx$$
By a simple integration by part argument on the last term and using the boundary condition on $\eta$, we can further simplify the expression to get
$$\delta^2F[y](\eta)=\frac{1}{2}\int_\alpha^\beta(Q\eta^2+P(\eta^\prime)^2)\,\mathrm dx,Q=\frac{\partial^2f}{\partial y^2}-\frac{\mathrm d}{\mathrm dx}\left( \frac{\partial^2 f}{\partial y\partial y^\prime}\right),P=\frac{\partial^2f}{\partial (y^\prime)^2}$$
The way we got the expression of $\delta^2F[y]$ then hinted that $y$ being a minimiser relates greatly to $\delta^2F[y]$ being positive.
\begin{proposition}
    If $y$ is a solution to the Euler-Lagrange equation of $F$ and $Q\eta^2+P(\eta^\prime)^2>0$ for any nonzero (sufficiently smooth) function $\eta$ that vanishes at $\alpha,\beta$, then $y$ is a local minimiser of $F$.
\end{proposition}
\begin{example}
    For the geodesics on the plane, we already know that the solutions to the Euler-Lagrange equation are striaght line segments.
    We have $f=\sqrt{1+(y^\prime)^2}$, so
    $$P=\frac{\partial}{\partial y^\prime}\left( \frac{y^\prime}{\sqrt{1+(y^\prime)^2}} \right)=\frac{1}{(1+(y^\prime)^2)^{3/2}},Q=0$$
    But then $P$ is always positive and $\eta^\prime$ is nonzero since $\eta$ is nonconstant (if it is constant then it is zero by the boundary condition), so by the preceding proposition any stationary point of it is a local minimiser.
\end{example}
An analogous computation proves a similar result for geodesics on the sphere.
\begin{proposition}
    If $y_0(x)$ is a local minimum, then
    $$P=\left.\frac{\partial^2 f}{\partial (y^\prime)^2}\right|_{y_0}=0$$
\end{proposition}
This is known as the Legendre condition.
We will sketch a proof of it.
\begin{proof}[Sketch of proof]
    Assume there is some $x_0$ such that $P(x_0,y_0,y_0^\prime)<0$.
    We can easily constuct a function $\eta$ such that $|\eta|<\epsilon$ for some small $\epsilon>0$ but $\eta$ fluctuates so rapid around $x_0$ such that $|\eta^\prime|$ is big around $x_0$, then the $Q\eta^2$ term would contribute very little to the integral but $P(\eta^\prime)^2$ contributes a more significant and positive value, which shall yield a contradiction.
\end{proof}
Note that the Legendre condition is only necessary but not sufficient.
An obvious sufficient condition is $P>0,Q\ge 0$ at $y_0$.
\begin{example}[The Brachistochrone Problem]
    We have
    $$f=\sqrt{\frac{1+(y^\prime)^2}{-y}}$$
    Which we already know has the cycloid as a stationary point.
    To see it is actually a minimiser, we compute
    $$P=\frac{1}{(1+(y^\prime)^2)^{3/2}\sqrt{-y}}>0,Q=\frac{1}{2\sqrt{1+(y^\prime)^2}y^2\sqrt{-y}}>0$$
    so the cycloid is indeed a local minimiser.
\end{example}
\subsection{Associated Eigenvalue Problem}
Rewrite the integrand of the second variation in the form
$$Q\eta^2+P(\eta^\prime)^2=Q\eta^2+\frac{\mathrm d}{\mathrm dx}(P\eta\eta^\prime)-\eta\frac{\mathrm d}{\mathrm dx}(P\eta^\prime)$$
Integrating by part again yields another formula for the second variation.
$$\delta^2F[y](\eta)=\frac{1}{2}\int_\alpha^\beta\eta\left( -\frac{\mathrm d}{\mathrm dx}(P\eta^\prime)+ Q\eta\right)\,\mathrm dx$$
But the thing in the bracket is precisely the Sturm-Liouville operator $\mathcal L$ in Example \ref{sturm-liouville} with $\rho=P$ and $\sigma=Q$ applied to $\eta$.
So if $\mathcal L(\eta)=-\omega^2\eta$ for some $\eta$ satisfying the boundary conditions and $\omega\in\mathbb R$, then $\delta^2F[y_0]<0$ if $\eta$ is not constantly zero, therefore $y_0$ is not a minimiser.
Observe here that $\mathcal L$ can have nonpositive eigenvalues (as shown in the example below) even if $P>0$, so this would mean that the Legendre condition is not sufficient.
\begin{example}
    Consider the functional
    $$F[y]=\int_0^\beta((y^\prime)^2-y^2)\,\mathrm dx$$
    subject to the initial conditions $y(0)=y(\beta)=0$ and $\beta$ is not an integer multiple of $\pi$.
    The Euler-Lagrange equation transforms to $y^{\prime\prime}+y=0$, which solves to $y\equiv 0$ due to our initial condition.
    Now
    $$\delta^2F[0](\eta)=\frac{1}{2}\int_0^\beta((\eta^\prime)^2-\eta^2)\,\mathrm dx$$
    So $P=1>0$, so the Legendre condition is satisfied but the corresponding Sturm-Liouville operator is $\mathcal L(\eta)=-\eta^{\prime\prime}-\eta$.
    But if $\beta>\pi$, then there is a solution $\omega$ satisfying the condition $(\pi/\beta)^2=1-\omega^2$, which makes the function $\eta(x)=\sin(\pi x/\beta)$ satisfy $\mathcal L(\eta)=-\omega^2\eta$, hence $0$ is not a local minimiser even if it satisfies the Legendre condition.
\end{example}
The example shows that $P>0$ alone may not guarantee local minimisation when the interval $[\alpha,\beta]$ is ``too large''.
We will make it precise in a moment.
\subsection{The Jacobi Condition}
Legendre tried and failed to prove that $P>0$ is also a sufficient condition for a local minimiser -- which is because it is not, as seen in the last example.
Jacobi, on the other hand, improved upon Legendre's idea.
Assuming that the Legendre condition holds, then let $\phi=\phi(x)$ be any differentiable function on $[\alpha,\beta]$, then
$$0=\int_\alpha^\beta (\phi\eta^2)^\prime\,\mathrm dx=\int_\alpha^\beta(\phi^\prime\eta^2+2\eta\eta^\prime\phi)\,\mathrm dx$$
So we can modify the expression of the second variation by
$$\delta^2F[y](\eta)=\frac{1}{2}\int_\alpha^\beta(P(\eta^\prime)^2+2\phi\eta\eta^\prime+(Q+\phi^\prime)\eta^2)\,\mathrm dx$$
Completing the square,
$$\delta^2F[y](\eta)=\frac{1}{2}\int_\alpha^\beta \left( P\left( \eta^\prime+\frac{\phi}{P}\eta \right)^2 +\left( Q+\phi^\prime-\frac{\phi^2}{P} \right)\eta^2\right)\,\mathrm dx$$
Hence, if we can find a solution $\phi$ to the differential equation $\phi^2=P(Q+\phi^\prime)$, then the positivity of the integrand is proved, hence we have a local minimum.
This ODE is called the Ricatti equation.\\
We can transform the Ricatti equation into a linear second-order ODE by setting $\phi=-Pu^\prime/u$ for some $u$ nonzero anywhere on $[\alpha,\beta]$, so
$$-(Pu^\prime)^\prime+Qu=0$$
this is called the Jacobi accessory equation.
But this is exactly the kernel problem of $\mathcal L$ with the restriction that the solution vanishes nowhere on $[\alpha,\beta]$.


\documentclass[notitlepage]{simple}

\usepackage{float}

\author{Matt McCarthy}
\title{Euler's Formula and the Complex Unit Circle}
\date{March 2016}

\usepackage{pgf,tikz}
\usepackage{mathrsfs}
\usetikzlibrary{arrows}

\definecolor{qqwuqq}{rgb}{0.,0.39215686274509803,0.}
\definecolor{xdxdff}{rgb}{0.49019607843137253,0.49019607843137253,1.}
\definecolor{qqqqff}{rgb}{0.,0.,1.}

\begin{document}

\maketitle

We want to show the following.

\begin{thm*}
	Let $x\in\RR$ and $i$ be the positive root of $x^2+1$.
	Then $e^{ix} = \cos x + i \sin x$.
\end{thm*}

\begin{thm*}
	The set $U = \set{z\in\CC | |z|=1}$ forms a group under complex multiplication.
\end{thm*}

\section{Complex Numbers}

We begin by defining the complex numbers.

\begin{definition}
	The set of \textit{complex numbers} is the set $\CC=\set{a+bi|a,b\in\RR}$ where $i^2=-1$.
	Complex addition is defined component wise, that is $(a+bi)+(c+di)=(a+c)+(b+d)i$ and complex multiplication is defined as
	\[
		(a+bi)(c+di) = (ac -bd) + (ad + bc)i.
	\]
\end{definition}

Suppose $z\in\CC$, then there exist $a,b\in\RR$ such that $z=a+bi$.
We can plot this on a plane as follows.

\begin{figure}[H]
	\centering
	\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1.5cm,y=1.5cm]
	\draw[->,color=black] (-0.5,0.) -- (2.5,0.);
	\foreach \x in {,1.,2.}
	\draw[shift={(\x,0)},color=black] (0pt,2pt) -- (0pt,-2pt);
	\draw[color=black] (2.6,-0.15) node [anchor=south west] {$x$};
	\draw[->,color=black] (0.,-0.5) -- (0.,2.5);
	\foreach \y in {,1.,2.}
	\draw[shift={(0,\y)},color=black] (2pt,0pt) -- (-2pt,0pt);
	\draw[color=black] (-0.2,2.8) node [anchor=west] {$iy$};
	\clip(-0.5,-0.5) rectangle (2.5,2.5);
	\draw[color=qqwuqq,fill=qqwuqq,fill opacity=0.1] (2.,0.22277768022923544) -- (1.7772223197707646,0.22277768022923547) -- (1.7772223197707646,0.) -- (2.,0.) -- cycle;
	\draw [shift={(0.,0.)},color=qqwuqq,fill=qqwuqq,fill opacity=0.1] (0,0) -- (0.:0.3150552167742013) arc (0.:45.:0.3150552167742013) -- cycle;
	\draw (0.,0.)-- (2.,2.);
	\draw (2.,2.)-- (2.,0.);
	\draw (0.,0.)-- (2.,0.);
	\begin{scriptsize}
	\draw [fill=qqqqff] (2.,2.) circle (2.5pt);
	\draw[color=qqqqff] (2.0719752655925316,2.191528364493368) node {$z$};
	\draw [fill=qqqqff] (0.,0.) circle (2.5pt);
	\draw[color=qqqqff] (-0.15,-0.15) node {$0$};
	\draw[color=black] (0.8012525579365867,1.109838786901943) node {$r$};
	\draw [fill=xdxdff] (2.,0.) circle (2.5pt);
	\draw[color=xdxdff] (2.2,0.185676817697619) node {$a$};
	\draw[color=black] (2.1664918306247922,0.9838167001922624) node {$b$};
	\draw [fill=xdxdff] (0.,2.) circle (2.5pt);
	\draw[color=xdxdff] (-0.22792781685913763,2.023498915547127) node {$b$};
	\draw[color=black] (1.0322930502376675,-0.19238944243142264) node {$a$};
	\draw[color=qqwuqq] (0.4,0.15) node {$\theta$};
	\end{scriptsize}
	\end{tikzpicture}
\end{figure}

When we plot it, we get a triangle with vertices $0,a,z$.
If we consider the line $az$, we see that it is parallel to the $iy$ axis, which is in turn perpendicular to the $x$ axis.
Thus, $\triangle 0az$ is right and $r=\sqrt{a^2+b^2}$ by Pythagorean theorem.
We also define $|z|=\sqrt{a^2+b^2}$, or the Euclidean distance between $z$ and $0$.

Furthermore, consider the angle $\theta$.
If we use the change of variables $a=r\cos\theta$ and $b=r\sin\theta$, we get that $\theta = \arctan(b/a)$.
Thus, we can write $z=a+bi$ as $r(\cos\theta+i\sin\theta)$, which gives us a polar representation of $z$.
Moreover, we call $r$ the \textit{modulus} of $z$ and $\theta$ the \textit{argument} of $z$; note that both the modulus and argument are real numbers.

\section{Euler's Formula}

In the 1740's Leonhard Euler noted that
\[
	e^{ix}=\cos x + i\sin x.
\]
We provide a proof of that.
\begin{thm}
	Let $x\in\RR$ and $i$ be the positive root of $x^2+1$.
	Then $e^{ix} = \cos x + i \sin x$.
\end{thm}
\begin{proof}
	We know $e^{ix}$ is a complex number thus, $e^{ix}=r\cdot(\cos \theta+ i\sin \theta)$, where $r=r(x)$ and $\theta = \theta(x)$.
	Therefore
	\[
		\frac{d}{dx} e^{ix} = \frac{d}{dx}\paren{r\cdot\paren{\cos \theta+ i\sin \theta}}
	\]
	and
	\[
		-r\sin\theta + ir\cos\theta =
		ie^{ix} =
		\paren{\cos \theta+ i\sin \theta}\frac{dr}{dx} + r\cdot\paren{-\sin \theta+ i\cos \theta}\frac{d\theta}{dx}.
	\]
	Thus, when we match real and imaginary parts, we get the following linear system of equations.
	\begin{align*}
		\cos \theta\frac{dr}{dx} - r\sin\theta\frac{d\theta}{dx} &= -r\sin\theta\\
		\sin \theta\frac{dr}{dx} + r\cos\theta\frac{d\theta}{dx} &=  r\cos\theta
	\end{align*}
	Solving the system yields the differential equations $dr/dx = 0$ and $d\theta/dx = 1$.

	Consider $e^{i0}$.
	\[
		r(0)(\cos(\theta(0))+ i\sin(\theta(0)))=e^{i0}=1=1(\cos 0 + i\sin 0)
	\]
	Matching terms gives us, $r(0)=1$ and $\theta(0)=0$.
	When combined with our differential equations, this yields the following initial value problems.
	\[
		\begin{cases}
			\frac{dr}{dx}=0\\
			r(0)=1
		\end{cases}
		\hspace{4em}
		\begin{cases}
			\frac{d\theta}{dx}=1\\
			\theta(0)=0
		\end{cases}
	\]
	The solutions to these initial value problems are $r(x)=1$ and $\theta(x)=x$ respectively.
	Thus,
	\[
		e^{ix}=\cos x + i\sin x.
	\]
\end{proof}

We can quickly extend this result to any complex number.

\begin{corollary}
	For any $z\in\CC$, $z=re^{i\theta}$ where $\theta=\arg z$ and $r=|z|$.
\end{corollary}
\begin{proof}
	Since $z\in\CC$, $z=r(\cos \theta + i\sin \theta)$ where $\theta = \arg z$ and $r=|z|$.
	Thus by Euler's formula,
	\[
		z=r(\cos \theta + i\sin \theta) = re^{i\theta}.
	\]
\end{proof}

\section{The Unit Circle}

Consider $e^{i\theta}$ where $\theta$ is real.
We know that $\abs{e^{i\theta}}=1$ by the corollary to Euler's formula.
Thus $e^{i\theta}$ lies on the unit circle of the complex plane.
\begin{definition}
	The unit circle of the complex plane is the subset of $\CC$ whose elements all have a modulus of one.
	\[
		U = \set{z\in\CC | |z|=1}.
	\]
\end{definition}
Conversely, all points on the unit circle also have the form $e^{i\theta}$ for some $\theta\in[0,2\pi)$.

Our goal is to show that $U$ is a group under complex multiplication, but before we do so, let us consider $\CC$.
Most notably, $\CC$ is a field, which implies that $\CC\setminus\set{0}$ is an abelian group under complex multiplication.
Thus, in order to show that $(U,\cdot)$ forms a group, it is sufficient to show that $U$ is a \textit{subgroup} of $\CC\setminus\set{0}$.
In short, a subset of a group is a subgroup if it is also a group under the group operation in its own right.
Additionally, we have a quick test to determine whether or not a subset is a subgroup.

\begin{proposition}[One step test]
	Let $(G,*)$ be a group and $H$ a non empty subset of $G$.
	Then $H$ is a subgroup of $G$ if for all $a,b\in H$,
	\[
		a*b^{-1}\in H.
	\]
\end{proposition}

With the Euler's formula and the one step subgroup test in our toolbox, we are now ready to show that $U$ is a group.

\begin{thm}
	The set $U = \set{z\in\CC | |z|=1}$ forms a group under complex multiplication.
\end{thm}
\begin{proof}
	We know that $|1|=1$, therefore $1\in U$ and $U$ is nonempty.
	Furthermore, $U\subset\CC\setminus\set{0}$ by definition of $U$.

	Proceed via one step subgroup test.
	Let $z,w\in U$.
	Therefore $|z|=|w|=1$ and $z=e^{i\theta}$ and $w=e^{i\phi}$ where $\theta,\phi\in\RR$ by corollary to Euler's formula.

	Consider $zw^{-1}$.
	\[
		zw^{-1} = e^{i\theta}e^{-i\phi} = e^{i\theta - i\phi} = e^{i(\theta -\phi)}
	\]
	We know that for any $x\in\RR$, $|e^{ix}|=1$ therefore $|zw^{-1}|=|e^{i(\theta - \phi)}|=1$ and $zw^{-1}\in U$.
	Thus, $U$ is a subgroup of $\CC\setminus\set{0}$ under multiplication by the one step subgroup test.
	Therefore $(U,\cdot)$ is a group.
\end{proof}

\end{document}


\subsection{Translation}
\noindent
Let $a$ be a constant.
\begin{equation*}
	\Laplace{e^{at}f(t)} = \int_{0}^{\infty}{e^{at}f(t)e^{-st} \mathrm{d}t} = \int_{0}^{\infty}{f(t)e^{-(s-a)t} \mathrm{d}t} = \mathcal{L}\left\{f(t)\right\}\left(s-a\right)
\end{equation*}
So,
\begin{equation*}
	\Laplace{e^{at}f(t)} = \mathcal{L}\left\{f(t)\right\}\left(s-a\right)
\end{equation*}
This illustrates how multiplying by $e^{at}$ in the $t$ domain corresponds to a translation by $a$ in the $s$ domain.


\lab{Algorithms}{Optimization}{Optimization Package 2}
\label{lab:Optimization 2}
\objective{Introduce some of the basic optimization functions available in the cvxopt package}

You can learn about more about cvxopt at

\url{http://abel.ee.ucla.edu/cvxopt/documentation/}.

\section*{Linear Programs}

Cvxopt has linear program solver and can implement integer programming through the Gnu Linear Programming Kit, glpk.

Consider the following transportation problem:
A piano company needs to transport thirteen pianos from their three  supply centers (1,2, and 3) to two demand centers (4 and 5). Transporting a piano from a supply center to a demand center incurs a cost, listed in the table below. The company want to minimize shipping costs for the pianos. How many pianos should each supply center send each demand center?

\begin{table}[h]
\centering
\begin{tabular}{|c|c|}
Supply Center & Number of pianos available\\
\hline
1 & 7\\
2 & 2\\
3 & 4\\
\end{tabular}

\caption{Number of pianos available at each supply center}
\end{table}

\begin{table}[h]
\centering
\begin{tabular}{|c|c|}
Demand Center & Number of pianos needed\\
\hline
4 & 5\\
5 & 8\\
\end{tabular}

\caption{Number of pianos needed at each demand center}
\end{table}

\begin{table}[h]
\centering
\begin{tabular}{|c|c|c|c|}
Supply Center & Demand Center & Cost of transportation & Number of pianos\\
\hline
1 & 4 & 4 & p\\
1 & 5 & 7 & q\\
2 & 4 & 6 & r\\
2 & 5 & 8 & s\\
3 & 4 & 8 & t\\
3 & 5 & 9 & u\\
\end{tabular}
\caption{Cost of transporting one piano from supply center to demand center}
\end{table}

The variables $p,q,r,s,t,$ and $u$ must be nonnegative and satisfy the following three supply and two demand constraints:

\begin{align}
p +& q  &    &    &    &   &=& 7\\
   &    & r +& s  &    &   &=& 2\\
   &    &    &    & t +& u &=& 4\\
p +&    & r +&    & t  &   &=& 5\\
   & q +&    & s +&    & u &=& 8\\
\end{align}

The objective function is the number of pianos shipped from each location multiplied by the cost.

\begin{center}
$4p + 7q + 6r + 8s + 8t + 9u$
\end{center}

We can solve this program using cvxopt, which uses the notation

\begin{lstlisting}[mathescape]
min $c^Tx$
subject to $Gx +s = h$
	   $Ax    = b$
	   $s     \geq 0$
\end{lstlisting}

Cvxopt also solves the dual problem

\begin{lstlisting}[mathescape]
min $-h^Tz - b^Ty$
subject to $G^Tz +A^Ty + c = 0$
	   $z     \geq 0$
\end{lstlisting}

All entries must be floats, and each array must be converted to a matrix type, unique to cvxopt. Numpy arrays can be converted to matrix by

\begin{lstlisting}
>>> a = numpy.array([[11,2],[2,5]])
>>> A = matrix(a)
\end{lstlisting}

There a several ways to solve the linear program.

\section*{Example}
Here, $G$ and $h$ constrain the variables to be non-negative. The entries are negative because cvxopt uses the format $Gx \leq h$. $A$ and $b$ represent the supply and demand constraints. Cvxopt.solvers returns a dictionary with a lot of information, including slack, iterations, dual and primal information. All we really care about right now are the values of $x$ and the objective value.

\begin{lstlisting}
: from cvxopt import matrix, solvers
: c = matrix([4., 7., 6., 8., 8., 9])
: G = matrix([ [-1., 0., 0., 0., 0., 0.],
             [0., -1., 0., 0., 0., 0.],
             [0., 0., -1., 0., 0., 0.],
             [0., 0., 0., -1., 0., 0.],
             [0., 0., 0., 0., -1., 0.],
             [0., 0., 0., 0., 0., -1.] ])

: h = matrix([ 0., 0., 0., 0., 0., 0.,])
: A = matrix([ [1., 0., 0., 1., 0.],
             [1., 0., 0., 0., 1.],
             [0., 1., 0., 1., 0.],
             [0., 1., 0., 0., 1.],
             [0., 0., 1., 1., 0.],
             [0., 0., 1., 0., 1.] ])
: b = matrix([7., 2., 4., 5., 8])
: sol = solvers.lp(c,G,h,A,b)
: print sol['x']
: print sol['primal objective']
\end{lstlisting}

\textbf{Problem 1}

Run the example. What happens?

We can fix the problem by including the supply demand constraints in the $G$ matrix.

\section*{Example}
Why are all of the terms in $G$ and $h$ non-positive?

\begin{lstlisting}
: from cvxopt import matrix, solvers
: G = matrix([ [-1., 0., 0., -1., 0.,  -1., 0., 0., 0., 0., 0.],
             [-1., 0., 0., 0., -1.,  0., -1., 0., 0., 0., 0.],
             [0., -1., 0., -1., 0.,  0., 0., -1., 0., 0., 0.],
             [0., -1., 0., 0., -1.,  0., 0., 0., -1., 0., 0.],
             [0., 0., -1., -1., 0.,  0., 0., 0., 0., -1., 0.],
             [0., 0., -1., 0., -1.,  0., 0., 0., 0., 0., -1.] ])

: h = matrix([-7., -2., -4., -5., -8.,  0., 0., 0., 0., 0., 0.,])
: c = matrix([4., 7., 6., 8., 8., 9])
: sol = solvers.lp(c,G,h)
: print sol['x']
: print sol['primal objective']
\end{lstlisting}

Another method is to use an integer linear program. Cvxopt is configured to work with  Gnu, which does have an integer linear program. It will work with either of the methods above.

\textbf{Example}

glpk.ilp returns a tuple. The first entry describes the optimality of the result, while the second gives the $x$ values.

\begin{lstlisting}
: from cvxopt import matrix, solvers, glpk
: G = matrix([ [-1., 0., 0., -1., 0.,  -1., 0., 0., 0., 0., 0.],
             [-1., 0., 0., 0., -1.,  0., -1., 0., 0., 0., 0.],
             [0., -1., 0., -1., 0.,  0., 0., -1., 0., 0., 0.],
             [0., -1., 0., 0., -1.,  0., 0., 0., -1., 0., 0.],
             [0., 0., -1., -1., 0.,  0., 0., 0., 0., -1., 0.],
             [0., 0., -1., 0., -1.,  0., 0., 0., 0., 0., -1.] ])

: h = matrix([-7., -2., -4., -5., -8.,  0., 0., 0., 0., 0., 0.,])
: o = matrix([4., 7., 6., 8., 8., 9])
: sol = glpk.ilp(o,G,h)
: print sol[1]
\end{lstlisting}

or
\begin{lstlisting}
: from cvxopt import matrix, solvers, glpk
: G = matrix([ [-1., 0., 0., 0., 0., 0.],
             [0., -1., 0., 0., 0., 0.],
             [0., 0., -1., 0., 0., 0.],
             [0., 0., 0., -1., 0., 0.],
             [0., 0., 0., 0., -1., 0.],
             [0., 0., 0., 0., 0., -1.] ])

: h = matrix([ 0., 0., 0., 0., 0., 0.,])
: o = matrix([4., 7., 6., 8., 8., 9])
: A = matrix([ [1., 0., 0., 1., 0.],
             [1., 0., 0., 0., 1.],
             [0., 1., 0., 1., 0.],
             [0., 1., 0., 0., 1.],
             [0., 0., 1., 1., 0.],
             [0., 0., 1., 0., 1.] ])
: b = matrix([7., 2., 4., 5., 8])
: sol = glpk.ilp(o,G,h,A,b)
: print sol[1]
\end{lstlisting}

\textbf{Problem 2}
Choose one of these methods and compare the optimal values for the integer linear program to the result you received above.

\textbf{Problem 3}
Create the dual problem for the linear program and solve.
Compare your answer to the dual value cvxopt returned.

\section*{Programmming}

The quadratic programming setup is similar to the linear programming. However, it does not have to be constrained. $G, h, A$, and $b$ are optional

\begin{lstlisting}[mathescape]
minimize $\frac{1}{2}x^TQx + p^Tx$
subject to $Gx\leq h$
	$Ax = b$.
\end{lstlisting}

\section*{Example}
Find the minimum of
\begin{equation}
f(x,y) = 2x^2 +2xy + y^2 +x -y
\end{equation}
\begin{lstlisting}
: Q = matrix([ [4., 2.], [2., 2.] ])
: p = matrix([1., -1.])
: G = matrix([[1., 0.], [0., -1.]])
: h = matrix([0.,0.])
: sol=solvers.qp(Q, p, G, h)
: print(sol['x'])
: print sol['primal objective']
\end{lstlisting}

\textbf{Problem 4}
Find the minimizer and minimum of
\begin{equation}
f(x) = \frac{1}{2}x^tQx - x^Tp
\end{equation}
where

\begin{center}
$Q =
\begin{bmatrix}
3 & 0 & 1\\
0 & 4 & 2\\
1 & 2 & 3\\
\end{bmatrix}
$
and $p =
\begin{bmatrix}
3\\
0\\
1\\
\end{bmatrix}
$
\end{center}


\section{The natural numbers}

The archetypal example of an inductive type is the type $\N$ of \emph{natural numbers}.
The type of \define{natural numbers}\index{natural numbers|see N@{$\N$}} is defined to be a closed type $\nat$\index{N@{$\N$}} equipped with closed terms for a \define{zero term}\index{zero term} $\zeroN:\N$ and a \define{successor function}\index{successor function!of N@{of $\N$}}\index{function!successor on N@{successor on $\N$}} $\succN:\N\to\N$.

The rules we postulate for the type of natural numbers again come in four sets:
\begin{enumerate}
\item The formation rule, which asserts that the type $\N$ can be formed.
\item The introduction rules, which provide the zero element and the successor function.
\item The elimination rule. This rule is the type theoretic analogue of the induction principle for $\N$.
\item The computation rules, which assert that any application of the elimination rule behaves as expected on the constructors $\zeroN$ and $\succN$ of $\N$.
\end{enumerate}
\begin{rmk}
  We annotate the terms $\zeroN$ and $\succN$ of type $\N$ with their type in the subscript, as a reminder that $\zeroN$ and $\succN$ are declared to be terms of type $\N$, and not of any other type. In the next chapter we will introduce the type $\Z$ of the integers, on which we can also define a zero term $\zeroZ$, and a successor function $\succZ$. These should be distinguished from the terms $\zeroN$ and $\succN$. In general, we will make sure that every term is given a unique name. In libraries of mathematics formalized in a computer proof assistant it is also the case that every type must be given a unique name.
\end{rmk}

\subsection{The formal specification of the type of natural numbers}
\subsubsection{The formation rule of $\N$}
The type $\N$ is formed by the $\N$-formation rule
\begin{prooftree}
  \AxiomC{}
  \RightLabel{$\N$-form}
  \UnaryInfC{$\vdash \N~\mathrm{type}$.}
\end{prooftree}

\subsubsection{The introduction rules of $\N$}
The introduction rules for $\N$ introduce the zero term and the successor function

\bigskip
\begin{minipage}{.45\textwidth}
  \begin{prooftree}
    \AxiomC{}
    \UnaryInfC{$\vdash \zeroN:\N$}
  \end{prooftree}
\end{minipage}
\begin{minipage}{.45\textwidth}
  \begin{prooftree}
    \AxiomC{}
    \UnaryInfC{$\vdash \succN:\N\to\N$}
  \end{prooftree}
\end{minipage}

\subsubsection{The elimination rule of $\N$}
To prove properties about the natural numbers, we postulate an \emph{induction principle}\index{induction principle!of N@{of $\N$}} for $\N$. In dependent type theory, however, the induction principle for the natural numbers provides a way to construct \emph{dependent functions} of types depending on the natural numbers.

The induction principle for $\N$ states what one has to do in order to construct a dependent function of type $\prd{n:\N}P(n)$, for any given type family $P$ over $\N$. Just like for the usual induction principle of the natural numbers, there are two things to be constructed: first one has to construct $p_0:P(\zeroN)$, and the second task is to construct a function of type $P(n)\to P(\succN(n))$ for all $n:\N$.

Therefore the induction principle for $\N$ is as follows:
\begin{prooftree}
  \def\fCenter{\Gamma}
  \Axiom$\fCenter, n:\N\vdash P(n)~\mathrm{type}$
  \noLine
  \UnaryInf$\fCenter\ \vdash p_0:P(\zeroN)$
  \noLine
  \UnaryInf$\fCenter\ \vdash p_S:\prd{n:\N}P(n)\to P(\succN(n))$
  \RightLabel{$\N{-}\mathrm{Ind}$}
  \UnaryInf$\fCenter\ \vdash \ind{\N}(p_0,p_S):\prd{n:\N} P(n)$
\end{prooftree}

\begin{rmk}
  The induction principle of $\N$ might also be formulated as an inference rule
  \begin{prooftree}
    \AxiomC{$\Gamma,n:\N\vdash P(n)~\mathrm{type}$}
    \UnaryInfC{$\Gamma\vdash \mathsf{ind}_{\N} : P(\zeroN)\to \Big(\Big(\prd{n:\N}P(n)\to P(\succN(n))\Big)\to \prd{n:\N}P(n)\Big)$}
  \end{prooftree}
  To see that indeed we get such a function from the induction principle, we note that the induction principle is stated to hold in an \emph{arbitrary} context $\Gamma$. In particular, by weakening and the variable rule we have the following well-formed terms:
  \begin{align*}
    \Gamma,~p_0:P(\zeroN),~p_S:\prd{n:\N}P(n)\to P(\succN(n)) & \vdash p_0 : P(\zeroN) \\
    \Gamma,~p_0:P(\zeroN),~p_S:\prd{n:\N}P(n)\to P(\succN(n)) & \vdash p_S : \prd{n:\N}P(n)\to P(\succN(n)).
  \end{align*}
  Therefore, the induction principle of $\N$ provides us with a term
  \begin{equation*}
    \Gamma,~p_0:P(\zeroN),~p_S:\prd{n:\N}P(n)\to P(\succN(n)) \vdash \mathsf{ind}_\N(p_0,p_S) : \prd{n:\N}P(n).
  \end{equation*}
  By $\lambda$-abstraction we now obtain a function
  \begin{equation*}
    \mathsf{ind}_\N : P(\zeroN)\to \Big(\Big(\prd{n:\N}P(n)\to P(\succN(n))\Big) \to \prd{n:\N}P(n)\Big)
  \end{equation*}
  in context $\Gamma$. Therefore we see that it does not really matter whether we present the induction principle of $\N$ in a more verbose way as an inference rule with the base case and the inductive step as hypotheses, or as a function taking variables for the base case and the inductive step as arguments.
\end{rmk}

\subsubsection{The computation rules of $\N$}
Furthermore we require that the dependent function $\ind{\N}(P,p_0,p_S)$ behaves as expected when it is applied to $\zeroN$ or a successor, i.e., with the same hypotheses as for the induction principle we postulate the \define{computation rules}\index{computation rules!of N@{of $\N$}} for $\N$
\begin{prooftree}
    \def\fCenter{\Gamma}
  \Axiom$\fCenter, n:\N\vdash P(n)~\mathrm{type}$
  \noLine
  \UnaryInf$\fCenter\ \vdash p_0:P(\zeroN)$
  \noLine
  \UnaryInf$\fCenter\ \vdash p_S:\prd{n:\N}P(n)\to P(\succN(n))$
  \RightLabel{$\N{-}\mathrm{Ind}$}
  \UnaryInf$\fCenter\ \vdash \ind{\N}(p_0,p_S,\zeroN)\jdeq p_0 : P(\zeroN)$
\end{prooftree}
and similarly, with the same hypotheses as for the computation rule for the base case,
\begin{prooftree}
\AxiomC{$\cdots$}
%\RightLabel{$\N{-}\mathrm{Comp}(\succN)$}
\UnaryInfC{$\Gamma, n:\N \vdash  \ind{\N}(p_0,p_S,\succN(n))\jdeq p_S(n,\ind{\N}(p_0,p_S,n)) : P(\succN(n))$}
\end{prooftree}
This completes the formal specification of $\N$.

\subsection{Addition on the natural numbers}
Using the induction principle of $\N$ we can perform many familiar constructions.
For instance, we can define the \define{addition operation}\index{addition!on N@{on $\N$}}\index{function!addition on N@{addition on $\N$}} by induction on $\N$.

\begin{defn}
  We define a function
  \begin{equation*}
    \addN:\N\to (\N\to\N)
  \end{equation*}
  satisfying $\addN(\zeroN,n)\jdeq n$ and $\addN(\succN(m),n)\jdeq\succN(\addN(m,n))$. Usually we will write $n+m$ for $\addN(n,m)$.
\end{defn}

\begin{proof}[Informal construction]
Informally, the definition of addition is as follows. By induction it suffices to construct a function $\mathsf{add\usc{}}\zeroN : \N\to\N$, and a function
\begin{align*}
\mathsf{add\usc{}}\succN(n,f):\N\to\N,
\end{align*}
for every $n:\N$ and every $f:\N\to\N$.

The function $\mathsf{add\usc{}}\zeroN:\N\to\N$ is of course taken to be $\idfunc[\N]$, since the result of adding $0$ to $n$ should be $n$.

Given $n:\N$ and a function $f:\N\to\N$ we define $\mathsf{add\usc{}}\succN(n,f)\defeq \succN\circ f$. The idea is that if $f$ represents adding $m$, then $\mathsf{add\usc{}}\succN(n,f)$ should be adding one more than $f$ did.
\end{proof}

\begin{proof}[Formal derivation]
The derivation for the construction of $\mathsf{add\usc{}}\succN$ looks as follows:
\begin{prooftree}
  \AxiomC{}
  \UnaryInfC{$\succN:\N^\N$}
  \AxiomC{}
  \UnaryInfC{$\vdash\N~\mathrm{type}$}
  \AxiomC{}
  \UnaryInfC{$\vdash\N~\mathrm{type}$}
  \AxiomC{}
  \UnaryInfC{$\vdash\N~\mathrm{type}$}
  \TrinaryInfC{$\vdash \mathsf{comp}:\N^\N\to (\N^\N\to \N^\N)$}
  \UnaryInfC{$g:\N\to\N\vdash \mathsf{comp}(g):\N^\N\to\N^\N$}
  \BinaryInfC{$\vdash \mathsf{comp}(\succN):\N^\N\to\N^\N$}
  \UnaryInfC{$n:\N\vdash \mathsf{comp}(\succN):\N^\N\to\N^\N$}
  \UnaryInfC{$\vdash \mathsf{add\usc{}}\succN\defeq \lam{n}\mathsf{comp}(\succN):\N\to (\N^\N \to \N^\N)$}
%\BinaryInfC{$\vdash\addN:\ind{\N}(add_0,add_S):\N\to \N^\N$}
\end{prooftree}
We combine this derivation with the induction principle of $\N$ to complete the construction of addition:
\begin{prooftree}
  \AxiomC{$\vdots$}
  \UnaryInfC{$n:\N\vdash \N^\N~\mathrm{type}$}
  \AxiomC{$\vdots$}
  \UnaryInfC{$\vdash \mathsf{add\usc{}}\zeroN\defeq \idfunc[\N]:\N^\N$}
  \AxiomC{$\vdots$}
  \UnaryInfC{$\vdash \mathsf{add\usc{}}\succN:\N\to (\N^\N \to \N^\N)$}
  \TrinaryInfC{$\vdash\addN\jdeq\ind{\N}(\mathsf{add\usc{}}\zeroN,\mathsf{add\usc{}}\succN):\N\to \N^\N$}
\end{prooftree}
The asserted judgmental equalities then hold by the computation rules for $\N$.
\end{proof}

\begin{rmk}
  When we define a function $f:\prd{n:\N} P(n)$, we will often do so just by indicating its definition on $\zeroN$ and its definition on $\succN(n)$, by writing
  \begin{align*}
    f(\zeroN) & \defeq p_0 \\
    f(\succN(n)) & \defeq p_S(n,f(n)).
  \end{align*}
  For example, the definition of addition on the natural numbers could be given as
  \begin{align*}
    \addN(\zeroN,n) & \defeq n \\
    \addN(\succN(m),n) & \defeq \succN(\addN(m,n)).
  \end{align*}
  This way of defining a function is called \emph{pattern matching}. A more formal inductive argument can be obtained from a definition by pattern matching if it is possible to obtain from the expression $p_S(n,f(n))$ a general dependent function
  \begin{equation*}
    p_S : \prd{n:\N} P(n)\to P(\succN(n)).
  \end{equation*}
  In practice this is usually the case. Computer proof assistants such as Agda have sofisticated algorithms to allow for definitions by pattern matching.
\end{rmk}

\begin{rmk}
The rules that we provided so far are not sufficient to also conclude that $n+\zeroN\jdeq n$ and $n+ \succN(m)\jdeq \succN(n+m)$. However, once we have introduced the \emph{identity type} in \cref{chap:identity} we will nevertheless be able to \emph{identify} $n+\zeroN$ with $n$, and $n+ \succN(m)$ with $\succN(n+m)$. See \cref{ex:semi-ring-laws-N}.
\end{rmk}

\begin{exercises}
  \item Define the binary \define{min} and \define{max} functions $\min_\N,\max_\N:\N\to(\N\to\N)$.\index{minimum function}\index{maximum function}\index{function!min}\index{function!max}
  \item Define the \define{multiplication}\index{multiplication!on N@{on $\N$}}\index{function!multiplication on N@{multiplication on $\N$}} operation $\mathsf{mul}_\N :\N\to(\N\to\N)$.
  \item Define the \define{power}\index{power function on N@{power function on $\N$}}\index{function!power function on N@{power function on $\N$}} operation $n,m\mapsto m^n$ of type $\N\to (\N\to \N)$.
  \item Define the \define{factorial}\index{factorial function}\index{function!factorial function} function $n\mapsto n!$.
  \item Define the \define{binomial coefficient}\index{binomial coefficients} $\binom{n}{k}$ for any $n,k:\N$, making sure that $\binom{n}{k}\jdeq 0$ when $n<k$.
  \item Define the \define{Fibonacci sequence}\index{Fibonacci sequence} $0,1,1,2,3,5,8,13,\ldots$ as a function $F:\N\to\N$.
\end{exercises}