\documentclass[a4paper,10pt]{article}
\usepackage{mystyle}

\begin{document}

\section{Limits}

\begin{defn}[Limit]
	$a$ is the limit of a sequence ${(xn)}_{n \in \NN}$ if $\forall \eps > 0 \quad \exists N \in \NN: n > N \implies | x_n -a | < \eps$.
	We write
	\[ \lim_{n \to \infty} x_n = a \]
\end{defn}

\begin{prop}
	Let $a$ be the limit of the sequence $x_n$, then $a$ is unique.
\end{prop}

\begin{proof}
	Assume we have two limits $a$ and $b$, then $\forall \eps > 0
	\quad \exists N_1, N_2 \in \NN$ such that
	\[ n > N_1 \implies |x_n - a| < \eps \]
	\[ n > N_2 \implies |x_n - b| < \eps \]

	Let $N = \max\{N_1, N_2\}$, then if $n > N$
	\[ |x_n - a| < \eps, \quad |x_n - b| < \eps \]

	\begin{align*}
		|b-a| &= |(x_n - a) + (b - x_n)| \leq |x_n - a| + |x_n - b| \\
		      &\implies |b-a| < 2\eps \\
		      &\implies b - a = 0 \implies a = b
	\end{align*}
\end{proof}

\begin{thm}[Archimedian Property]
	Let $x,y \in \RR, \, x>0$ then $\exists n \in \NN$ such that $xn > y$.
\end{thm}

\begin{proof}
	Assume $nx \leq y, \, \forall n \in \NN$.

	Let $S_x = \{ nx \mid n \in \NN \}$. By assumption, $S_x$ is
	bounded, and so by the completeness of the real numbers, we know
	the supremum of this set exists. $y$ is the maximum of the set
	so it must be the supremum too.

	$x > 0 \implies y-x < y$ so $y-x$ is not an upper bound for $S_x$.
	\begin{align*}
		&\implies \exists s \in S_x : y-x < s = kx, \, k \in \NN \\
		&\implies y < (k+1)x = mx \in S_x
	\end{align*}
	which is a contradiction to our assumption.
\end{proof}

\begin{cor}
	\[ \forall y \in \RR, \, \exists N \in \NN : n > y \]
\end{cor}

\begin{prop}
	Let $x_n = \frac{1}{n}, \, n \in \NN$, then $\lim_{n \to \infty}
	x_n = 0$.
\end{prop}

\begin{proof}
	Let $\frac{1}{\eps} \in \RR$. By the Archimedian Property,
	$\exists N \in \NN : N > \frac{1}{\eps}, \, \forall \eps > 0$.

	Let $n>N$, then
	\[ |x_n - 0| = \frac{1}{n} < \frac{1}{N} < \frac{1}{1/\eps} < \eps \]
\end{proof}

\begin{prop}
	Assume $\lim_{n \to \infty} x_n = a$ and $\lim_{n \to \infty}
	y_n = b$, then:
	\begin{enumerate}
		\item
			$\lim (x_n + y_n) = a + b$
		\item
			$\lim (x_n \dot y_n) = a \dot b$
		\item
			Assuming $y_n \neq 0 \, \forall n$ and $b \neq 0$,
			$\lim(\frac{x_n}{y_n}) = \frac{a}{b}$
		\item
			If a sequence is constant i.e. $x_n = c \,
			\forall n$, then $\lim x_n = c$
	\end{enumerate}
\end{prop}

\begin{proof}
	4:
	\begin{align*}
		&|x_n - c| = 0, \, \forall n \in \NN \\
		\implies &|x_n - c| < \eps, \, \forall \eps > 0 \in \RR, n \in \NN \\
		\implies &c = \lim x_n
	\end{align*}

	1:
	For a given $\eps > 0, \, \exists N_1, N_2 \in \NN$ such that
	\[ n > N_1 \implies |x_n - a| < \frac{\eps}{2} \]
	\[ n > N_2 \implies |y_n - b| < \frac{\eps}{2} \]
	\[
		n > \max \{ N_1, N_2 \} \implies | (x_n + y_n) - (a+b)|
	\]
	\[
		\leq |x_n - a| + |y_n - b|
		< \frac{\eps}{2} + \frac{\eps}{2} = \eps
	\]

	3:
	\[ \exists N_1 : |x_n - a| < |a|/2 \]
	\[ |a| = |a + x_n - x_n| \leq |a - x_n| + |x_n| < |a|/2 + |x_n| \]
	\[ \implies |a|/2 < |x_n| \implies 1/|x_n| < 2/|a| \]
	\[ \exists N_2 : |x_n -a| < \frac{|a|^2}{2}\eps \]
	\begin{align*}
		n > \max{N_1, N_2} \implies \\
		|\frac{1}{x_n} - \frac{1}{a}| &= |\frac{a-x_n}{ax_n} \\
		&= \frac{1}{|a|}\frac{1}{|x_n|}|x_n - a| \\
		&< \frac{1}{|a|}\frac{2}{|a|}|x_n - a| \\
		&< \frac{2}{|a|^2}\frac{a^2}{2}\eps = \eps
	\end{align*}

\end{proof}

\begin{ex}
	\[
		\lim \frac{n^2 - 1}{n_2 + 1}
		= \lim \frac{1 - \frac{1}{n^2}}{1 + \frac{1}{n^2}}
	\]
	\[ \lim \frac{1}{n} = 0 \implies \lim \frac{1}{n^2} = 0 \]
	\[ \implies \lim \frac{n^2 - 1}{n_2 + 1} = 1 \]
\end{ex}

\begin{defn}[Divergence]
	A sequence $x_n$ diverges to $+\infty$ if $\forall A > 0, \,
	\exists N \in \NN : n > N \implies x_n > A$. We say
	\[ \lim_{n \to \infty} x_n = \infty \]

	Similarly, if $\forall A < 0, \, \exists N \in \NN : n > N
	\implies x_n - A$, then
	\[ \lim_{n \to \infty} x_n = -\infty \]
\end{defn}

\begin{defn}[Increasing]
	A sequence is called increasing if $\forall n \in \NN, \, x_n
	\leq x_{n+1}$.
\end{defn}

\begin{defn}[Decreasing]
	A sequence is called decreasing if $\forall n \in \NN, \, x_n
	\geq x_{n+1}$.
\end{defn}

\begin{defn}[Monotonic]
	A sequence is monotonic if it is increasing or descreasing.
\end{defn}

\begin{defn}[Bounded]
	A sequence is bounded if $\exists A > 0 : |x_n| \leq A \,
	\forall n \in \NN$.
\end{defn}

\begin{thm}[MonontonicConvergence]
	If $x_n$ is bounded and increasing, it converges to $\sup\{x_1,
	\ldots, x_n\}$. If it is bounded and decreasing then it
	converges to $\inf\{x_1, \ldots, x_n\}$.
\end{thm}

\begin{proof}
	Assume the sequence is increasing.\\

	The supremum, $a$, of the sequence exists because $\RR$ is
	complete and the sequence is bounded. Take $\eps > 0$, then $a
	- \eps < a$ so $a - \eps$ is not a upper bound for $\{x_1,
	\ldots, x_n\}$ and there is $N \in \NN$ such that $x_N > a - \eps$.

	For every $n > N$ we have $x_n \geq x_N$, due to the increasing
	nature of the sequence. Hence,
	\[
		a - \eps < x_n \leq a < a + \eps
		\implies a - \eps < x_n < a + \eps
		\implies | x_n - a | < \eps
	\]
	and $\lim x_n = \sup\{x_1, \ldots, x_n\}$.\\

	Assume now that the sequence is decreasing.\\

	Again by completeness and boundedness $a = \inf \{x_1, \ldots,
	x_n\}$ is well defined.
	Fix $\eps > 0$ then $a + \eps > a$ and so $a + \eps$ is not a
	lower bound for $\{x_1, \ldots, x_n\}$ and there is $N \in \NN$
	such that $x_N < a + \eps$.
	Since $x_n$ is decreasing, $n > N \implies x_n \leq x_N$, so
	\[
		a - \eps < a \leq x_n < a + \eps
		\implies | x_n - a | < \eps
	\]
	and $\lim x_n = \inf \{x_1, \ldots, x_n\}$.
\end{proof}

\begin{ex}
	Let $x_1 = 2$ and $x_{n+1} = 2 + \sqrt{x_n}$, so $x_2 = 2 +
	\sqrt{2}$, $x_3 = 2 + \sqrt{2 + \sqrt{2}}$, etc.

	We aim to show that $2 \leq x_n \leq 4 \quad \forall n \in \NN$.

	$n=1$ is clearly true, so we may assume true for $n=k$.

	\[
		2 \leq x_k \leq 4 \implies \sqrt{2} \leq \sqrt{x_k} \leq \sqrt{4} = 2
	\]
	\[
		\implies 2 + \sqrt{2} \leq 2 + \sqrt{x_k} \leq 4
	\]
	\[
		2 + \sqrt{2} > 2 \implies x_{k+1} > 2 \implies 2 \leq x_{k+1} \leq 4
	\]

	So $x_n$ is bounded and increasing hence $\lim_{n \to \infty} x_n = 4$.
\end{ex}

\begin{defn}[Cauchy Sequence]
	A sequence is Cauchy if
	\[
		\forall \eps > 0 \, \exists N \in \NN :
		| x_n - x_m | < \eps \forall n,m > N
	\]
\end{defn}

\begin{thm}
	Any Cauchy sequence is bounded.
\end{thm}

\begin{proof}
	Let $x_n$ be a Cauchy sequence. Take $\eps = 1$, then $\exists
	N \in \NN : |x_n - x_m| < 1 \quad \forall n,m > N$.

	If $m = N+1$ then $|x_n - x_{N+1}| < 1 \iff x_{N+1} - 1 < x_n < x_{N+1} + 1$.

	$A = \max\{x_1, x_2, \ldots, x_N, x_{N+1}+1\}$
	$B = \min\{x_1, x_2, \ldots, x_N, x_{N+1}-1\}$

	So $B \leq x_n \leq A \quad \forall n$, hence $x_n$ is bounded.
\end{proof}

\begin{prop}
	If $S$ and $T$ are bounded subsets of $\RR$ and $S \subseteq T$,
	then $\inf T \leq \inf S$.
\end{prop}

\begin{proof}
	Let $t = \inf T$ and $s = \inf S$.

	Assume $s<t$. By completeness, $t$ cannot be a lower bound of
	$S$, hence
	\begin{align*}
		\exists \alpha \in S : \alpha < t \\
		&\implies \alpha < \beta \forall \beta \in T \\
		&\implies \alpha \notin T \\
		&\implies S \nsubseteq T
	\end{align*}

	The contrapositive statement has been proved, hence the original
	statement is true as well.
\end{proof}

\begin{thm}
	A sequence is Cauchy iff it converges.
\end{thm}

\begin{proof}
	Assume that a sequence converges such that $\lim x_n = a$, then
	\[
		\forall \eps > 0 \exists N \in \NN :
		| x_n - a | < \frac{\eps}{2}, \quad
		| x_m - a | < \frac{\eps}{2}, \quad
		\forall n,m > N
	\]
	\begin{align*}
		|x_n - x_m| &= |(x_n -a) - (x_m -a)| \\
			    &\leq |x_n -a| + |x_m -a| \\
			    &< \frac{\eps}{2} + \frac{\eps}{2} = \eps
	\end{align*}

	Assume now that $x_n$ is Cauchy, then we know it is bounded and
	by completeness, $\inf\{x_1, \ldots, x_n, x_{n+1}\}$ is well
	defined.

	Let
	\begin{align*}
		b_1 &= \inf \{x_1, \ldots\} \\
		b_2 &= \inf \{x_2, \ldots\} \\
		b_n &= \inf \{x_n, \ldots\}
	\end{align*}
	then
	\[
		b_1 \leq b_2 \leq \cdots \leq b_n \leq \cdots
	\]
	and
	\[
		b_n \leq x_n \leq B
	\]
	where $B$ is an upper bound $\forall n \in \NN$.

	By the Monotonic Covergence Theorem, $a = \lim b_n$ exists, so
	\[
		\forall \eps > 0 \exists N_1 \in \NN :
		|b_n - a| < \frac{\eps}{3}, \quad \forall n > N_1
	\]
	\[
		b_n + \frac{\eps}{3} > b_n = \inf\{x_n\}
	\]
	\[
		\implies \exists m_0 > N_1 :
		b_n < x_{m_0} < b_n + \frac{\eps}{3}
	\]

	$x_n$ is Cauchy, hence
	\[
		\exists N_2 \in \NN :
		|x_n - x_m| < \frac{\eps}{3}, \quad
		\forall n,m > N_2
	\]

	Let $N = \max\{N_1, N_2\}$, then
	\begin{align*}
		|x_n - a| &\leq |(x_n - x_m) + (x_m - b_n) + (b_n - a)| \\
			  &\leq |x_n - x_m| + |x_m - b_n| + |b_n - a| \\
			  &< \frac{\eps}{3} + \frac{\eps}{3}
				+ \frac{\eps}{3} = \eps
	\end{align*}
\end{proof}

\begin{ex}[Harmonic Series]
	\[
		y_n = 1 + \frac{1}{2} + \cdots + \frac{1}{n}
	\]
	We claim that $y_n$ diverges.
	\begin{align*}
		y_{2n} - y{n} &=
		\left(1 + \frac{1}{2} + \cdots + \frac{1}{2n} \right)
		\left(1 + \frac{1}{2} + \cdots + \frac{1}{n} \right) \\
		&= \frac{1}{n+1} + \cdots + \frac{1}{2n}
		> \frac{1}{2n} + \cdots + \frac{1}{2n} = \frac{1}{2}
	\end{align*}

	If we set $\eps = \frac{1}{3}$ then for $m = 2n$
	\[
		|y_m - y_n| > \eps
	\]
	hence the sequence is not Cauchy and so it cannot converge.
\end{ex}

\begin{cor}
	Any convergent sequence is bounded.
\end{cor}

\begin{ex}
	\[
		x_n = {(-1)}^n
	\]
	\[
		x_n = \pm 1 \quad \forall n \in \NN
	\]
	hence $x_n$ is bounded yet it does not converge.
\end{ex}

\section{Subsequences}

\begin{prop}
	If $x_n$ is a convergent sequence with $\lim_{n \to \infty} x_n
	= a$, then any subsequence $x_{n_k}$ coverges and $\lim_{k \to
	\infty} x_{n_k} = a$.
\end{prop}

\begin{proof}
	$x_n$ coverges so
	\[
		\forall \eps > 0 \exists N : |x_n - a| < \eps \quad \forall n > N
	\]

	Since $x_{n_k}$ is a subsequence of $x_n$, $n_k \geq k$ hence
	\[
		| x_{n_k} -a | < \eps \quad \forall k > N
		\implies \lim x_{n_k} = a
	\]
\end{proof}

\begin{thm}[Bolzano-Weierstrass]
	Any bounded sequence has a convergent subsequence.
\end{thm}

\begin{proof}
	Let $x_n$ be a bounded sequence, then
	\[
		\exists I_1 = [a,b] : x_n \in I_1, \, \forall n \geq 1
	\]

	If $c_1$ is the midpoint of $I_1$, then either $[a,c_1]$ or
	$[c_1,b]$ contains infinitely many terms of $x_n$. We call this
	$I_2$.

	We can continue in this manner, forming a sequnce $I_n$.

	We can form a subsequence of $x_n$ by taking $x_{n_1} \in I_1$,
	$x_{n_2} \in I_2$, etc.\ such that $n_1 < n_2 < \cdots < n_k$.

	Since the interval sizes are arbitrarily small, we can take
	$\eps = b_n - a_n$, where $I_n = [a_n,b_n]$. Hence
	\[
		|x-y| < b_n - a_n = \eps, \quad \forall x,y \in I_n
	\]
	and $x_{n_k}$ is Cauchy so it must converge.
\end{proof}

\section{Functions}

\begin{defn}[Continuous]
	A function $f: (a,b) \to \RR$ is continuous at $c \in (a,b)$
	if for any sequence of real numbers $x_n \in (a,b)$ where $\lim
	x_n = c$, one has $\lim f(x_n) = f(c)$.

	Equivalently, we could say $\lim_{x \to c} f(x) = f(c)$.

\end{defn}

\begin{defn}[Function Limits]
	Functions can have left and right limits.

	Let $f: (a,c) \cup (c,b) \to \RR$.

	Consider $x \in (a,c)$, then $\lim_{x \to c^-} f(x) = L$ is the
	left limit of $f$ if:
	\[
		\forall \eps > 0 \exists \delta > 0 :
		c - \delta < x < c \implies L - \eps < f(x) < L
	\]

	Now consider $x \in (c,b)$, then $\lim_{x \to c^+} f(x) = L$ is
	the right limit of $f$ if:
	\[
		\forall \eps > 0 \exists \delta > 0 :
		a < x < a + \delta \implies L < f(x) < L + \eps
	\]

	Let $x \in (a,c) \cup (c,b)$, then $\lim_{x \to c} f(x) = L$ is
	the limit of $f$ if:
	\[
		\forall \eps > 0 \exists \delta > 0 :
		|x-c| < \delta \implies |f(x) - L| < \eps
	\]

	If the limit exists, then so do the left and right limits and they are
	all equal. Conversely, if the left and right limits exist and are equal,
	the limit exists and share this common value.
\end{defn}

\begin{thm}
	Let $f$ be a function defined on $U = (a,c) \cup (c,b)$, then
	$\lim_{x \to c}f(x)$ exists iff for all sequences $x_n \in U$
	with $\lim_{x \to \infty} x_n = c$, we have
	\[
		\lim_{n \to \infty} f(x_n) = \lim_{x \to c} f(x)
	\]
\end{thm}

\begin{proof}
	Assume that $\lim_{x \to c}f(x) = L$.

	\[
		\forall \eps > 0 \, \exists \delta :
		|x-c| < \delta \implies |f(x)-L| < \eps
	\]

	Let $x_n$ be a sequence such that $\lim_{n \to\infty}x_n = c$, then
	\[
		\forall \eps' > 0 \, \exists N \in \NN :
		|x_n - c| < \eps', \, \forall n > N
	\]

	Let $\eps' = \delta$, then
	\begin{align*}
		|x_n - c| < \delta \quad \forall n > N \\
		&\implies |f(x_n) - L| < \eps \quad \forall n > N \\
		&\implies \lim f(x_n) = L
	\end{align*}

	Assume now that $\lim x_n = L$.

	Assume that $\lim f(x) \neq L$, then
	\[
		\exists \eps > 0 \, \forall \delta > 0 : \exists x :
		|x-c| < \delta \implies |f(x) - L| \geq \eps
	\]

	Fix $\eps$ and let $\delta = 1$, then there exists $x_1$ such that:
	\[
		|x_1 - c| < 1 \implies |f(x_1) - L| \geq \eps
	\]

	Let $\delta = \frac{1}{2}$, then there exists $x_2$ such that:
	\[
		|x_2 - c| < \frac{1}{2} \implies |f(x_2) - L| \geq \eps
	\]

	Continuing in this way, we obtain a sequence $x_n$ such that
	$\lim f(x_n) \neq L$, which is an obvious contradiction.
\end{proof}

\begin{rem}
	Let $f:[a,b] \to \RR$ be a function. $f$ is continuous as $c \in
	(a,b)$ if $\lim_{x \to c} f(x) = f(c)$.
	$f$ is continuous as $a \in (a,b)$ if $\lim_{x \to a^-} f(x) =
	f(a)$.
	$f$ is continuous as $a \in (a,b)$ if $\lim_{x \to b^+} f(x) =
	f(b)$.
\end{rem}

\begin{defn}[Continuous]
	$f:[a,b] \to \RR$ is continuous at $c$ if
	\[
		\forall \eps > 0 \, \exists \delta > 0 :
		|x-c| < \delta \implies |f(x)-f(c)| < \eps
	\]
	$f$ is continuous if it is continuous at every point in its
	domain.
\end{defn}

\subsection{Open and Closed Subsets}

\begin{defn}[Open Subset]
	A subset $U \subset \RR$ is called open if
	\[
		\forall a \in U \, \exists \eps > 0 :
		(a-\eps, a+\eps) \subset U
	\]
\end{defn}

\begin{defn}[Preimage]
	Let $f:[a,b] \to \RR$ and $U \subset \RR$. The preimage of $U$
	is defined as follows:
	\[
		f^{-1}(U) = \{ x \in [a,b] \mid f(x) \in U \}
	\]
\end{defn}

\begin{ex}
	\[
		f(x) = x^2, \quad f:\RR \to \RR, \quad U = \{0,1\}
	\]
	\[
		f^{-1}(U) = \{ x \in \RR \mid x^2 \in U \}
		= \{ \pm 1, 0 \}
	\]
\end{ex}

\begin{defn}[Closed Subset]
	A subset $V \in \RR$ is closed if its compliment is open.
\end{defn}

\begin{prop}
	$V \subset \RR$ is closed iff for any convergent sequence of
	real numbers $x_n$ in $V$, the limit is also in $V$.
\end{prop}

\begin{proof}
	Let $V = [a,b]$ and $x_n$ be a sequence in $V$.

	If every $x_n$ has a limit in $V$ then the sequence $a +
	\frac{\eps}{n}$, which clearly converges to $a$, must be in $V$.
	Likewise, the sequence $b - \frac{\eps}{n}$, whose limit is
	clearly $b$, must have it's limit in $V$.

	Assuming $V$ is closed, assume also that there is a sequence
	$x_n$ whose limit $L \notin [a,b]$, then $L = a -
	\frac{\eps}{2}$ or $b + \frac{\eps}{2}$ for some $\eps > 0 \in
	RR$.

	\begin{align*}
		L &= a - \frac{\eps}{2} \\
		  &\implies \exists N \in \NN : |x_n - a + \frac{\eps}{2}|
		< \frac{\eps}{2}, \,\forall n > N \\
		&\implies |x_n - a| - |- \frac{\eps}{2}| < \frac{\eps}{2} \\
		&\implies |x_n - a| < \eps \\
		&\implies a = \lim x_n
	\end{align*}
	This is a contradiction because $a \in V$.

	\begin{align*}
		L &= b + \frac{\eps}{2} \\
		  &\implies \exists N \in \NN : |x_n - b - \frac{\eps}{2}|
		< \frac{\eps}{2}, \,\forall n > N \\
		&\implies |x_n - b| - |\frac{\eps}{2}| < \frac{\eps}{2} \\
		&\implies |x_n - b| < \eps \\
		&\implies b = \lim x_n
	\end{align*}
	This is a contradiction because $b \in V$.
\end{proof}

\subsection{Properties of Open and Closed Sets}

\begin{itemize}
	\item A finite union of closed subsets is closed
	\item Any intersetion of closed subsets is closed
	\item Any union of open subsets is open
	\item A finite intersection of open subsets is open
\end{itemize}

\begin{proof}
	Number 2. \\

	Let $[a,b]$ and $[c,d]$ be two closed subsets in $\RR$, with a
	nonempty intersection.  If either is a subset of the other, then
	we are done.  If not, then either $c>a$ and $d>b$, or $a>c$ and
	$b>d$, resulting in the intersection being either $[c,b]$ or
	$[a,d]$, both of which are clearly closed.
\end{proof}

\begin{thm}
	For a function $f:(a,b) \to \RR$, $f$ is continuous iff the
	preimage of an open set under $f$ is also open.
\end{thm}

\begin{proof}
	Assume that $f$ is continuous. \\

	Let $U \subset \RR$ be open and let $W$ be the preimage of $U$
	under $f$.  Let $c \in W$ such that $f(c) = d \in U$. Given that
	$U$ is open, ther is $\eps > 0$ such that $(d-\eps, d+\eps)
	\subset U$.

	By the coninuity of $f$, there must be a $\delta > 0$ such that
	$|f(x) - f(c)| < \eps$ when $|x-c| < \delta$. We now need to
	show that $(c-\delta, c+\delta) \subset W$.

	\[
		z \in (c-\delta, c+\delta) \implies |z-c| < \delta
		\implies |f(z)-f(c)| < \eps
	\]
	\[
		\implies |f(z) - f(d)| < \eps
		\implies f(z) \in (d-\eps, d+\eps) \subset U
		\implies z \in W
	\]

	So, $(c-\delta, c+\delta) \subset W$, hence
	\[
		\forall c \in W \exists \delta > 0 :
		(c-\delta, c+\delta) \subset W
	\]
	and $W$ is open. \\

	Assume the openness of sets is preserved under $f$. \\

	Fix $\eps > 0$ and let $x \in (a,b)$ such that $f(x) = d \in
	RR$.  Let $U = (d-\eps, d+\eps) \subset \RR$, then $W =
	f^{-1}(U)$ is open by assumption.

	\[
		\implies \exists \delta > 0 :
		(x-\delta, x+\delta) \subset W
	\]

	Let $y \in (x-\delta, x+\delta)$, then $|x-y| < \delta$ hence
	$f(y) \in (d-\eps, d+\eps)$ and $|f(y) - f(x)| < \eps$.

\end{proof}

\subsection{Properties of Continuous Functions}

\begin{enumerate}
	\item The identity map is continuous
	\item
		If $f,g:(a,b) \to \RR$ are continuous, then $f+g$, $f-g$,
		$f\cdot g$ are all continuous. If $g(x) \neq 0 \forall x
		\in (a,b)$, $f(x)/g(x)$ is also continuous.
	\item $f \circ g$ is continuous
	\item $f(\lim x_n) = \lim f(x_n)$
\end{enumerate}

\begin{proof}
	\begin{enumerate}
		\item
			Fix $\eps$ and take $\delta = \eps$, then
			\[ |x-y| < \delta \implies |x-y| < \eps \implies |f(x)-f(y)| < \eps \]

		\item Follows from preservation of open sets.

		\item
			Let $c = \lim x_n$, then
			\[ \exists \eps > 0, \, N \in \NN : |x_n - c| < \eps, \, \forall n > N \]
			By continuity,
			\[ \exists \delta > 0 : |x_n -c| < \delta \implies |f(x)-f(c)| < \delta \]
			\[
				\implies |f(x_n) - f(c)| < \delta, \, \forall n > N
				\implies \lim f(x_n) = f(c) = f(\lim x_n)
			\]

		\item
			Follows from 4 and the properties of limits, since $f$ is continuous at $c$ iff
			$x_n \to c \implies f(x_n) \to f(c)$.
	\end{enumerate}
\end{proof}

\begin{cor}
	Any polynomial is a continuous function.
\end{cor}

\subsection{Intermediate Value Theorem}

\begin{thm}[Sandwich]
	Let $a_n$ and $b_n$ be sequences such that $\lim a_n = \lim b_n
	= C$. Then, if $c_n$ is a sequence such that $a_n \leq c_n \leq
	b_n \, \forall n \in \NN$, then $\lim c_n = C$.
\end{thm}

\begin{proof}
	$|c_n -C| \leq |a_n - C|$ or $|c_n -C| \leq |b_n - C|$, i.e.\
	$c_n$ is closer to $C$ than either $a_n$ or $b_n$.

	$\forall \eps > 0 \exists N_1,N_2 \in \NN : n > N_1 \implies
	|a_n - C| < \eps, \, n > N_2 \implies |b_n - C| < \eps$

	Let $N = \max(N_1, N_2)$, then for all $n > N$:
	\[ |a_n - C| < \eps \quad |b_n - C| < \eps \]
	\[ \implies |c_n -C| < \eps \implies \lim c_n = C \]
\end{proof}

\begin{thm}[Intermediate Value Theorem]
	Let $f:[a,b] \to \RR$ be a continuous function. If $f(a) = A
	\leq C \leq B = f(b)$, then $\exists c \in [a,b] : f(c) = C$.
\end{thm}

\begin{proof}
	Consider $g(x) = f(x) - C$. We aim to show that $g(c) = 0$ for some $c \in [a,b]$.

	Assume $A \leq C \leq B$, then
	\[ g(a) = f(a) - C \leq 0 \]
	\[ g(b) - f(b) - C \geq 0 \]

	If $g(a) = 0$ or $g(b) = 0$, the we are done. Assume $g(a) < 0$ and $g(b) > 0$.

	We can construct segments $[a_k, b_k] \subset [a,b]$, inductively as follows:
	\[ [a_1, b_1] = [a,b] \]
	Assume $[a_n, b_n]$ exists, and let $m$ be the midpoint of this interval.
	\[ m = \frac{b_n + a_n}{2} \in [a_n, b_n] \subset [a,b] \]
	Either $g(m) \leq 0$ or $g(m) > 0$. If $g(m) \leq 0$, then
	\[ [a_{n+1}, b_{n+1}] = [m, b_n] \]
	but if $g(m) > 0$, then
	\[ [a_{n+1}, b_{n+1}] = [a_n, m] \]

	We now have two bounded monotonic sequnces, $a_n$ and $b_n$, so
	they have well-defined limits.

	We also have
	\[
		b_{n+1} - a_{n+1} = \frac{1}{2^n}(b-a)
		\implies \lim a_n = \lim b_n = c
	\]

	Note that $g(a_k) \leq 0$ and $g(b_k) \geq 0$ for every $k \NN$.
	\[ \implies \lim g(a_n) = g(\lim a_n) = g(c) \leq 0 \]
	similarly,
	\[ \implies \lim g(b_n) = g(\lim b_n) = g(c) \geq 0 \]
	\[ \implies g(c) = 0 \implies f(c) = C \]
\end{proof}

\begin{defn}[Bounded Function]
	A function $f:I \to \RR$, where $I = [a,b] \subset \RR$, is bounded if
	\[ \exists M \in \RR, \, m > 0 : |f(x)| \leq M \, \forall x \in I. \]
	Equivalently, we could say
	\[ \exists A, B \in \RR : A \leq f(x) \leq B \, \forall x \in I. \]
\end{defn}

\begin{thm}
	If $f:[a,b] \to \RR$ is continuous, then $f$ is bounded.
\end{thm}

\begin{proof}
	Assume that $f$ is unbounded on $I = [a,b]$. Let $m = \frac{b+a}{2}$
	and divide $I$ into two subintervals: $[a,m]$ and $[m,b]$; $f$ must be
	unbounded on one of these intervals.

	Denote this subinterval by $[a_1, b_1]$, then construct a
	sequence of subintervals inductively.

	It is clear that $\lim a_n = \lim b_n = c$.

	By assumption, $f$ is unbounded in each of these intervals, in other words,
	\[ \exists c_n \in [a_n, b_n] : f(c_n) > N \forall N \in \NN. \]

	This implies $\lim f(c_n)$ does not exist, but this contradicts
	the sandwich theorem.
\end{proof}

\begin{thm}[Weierstrass Extremal Value Theorem]o
	Let $f:[a,b] \to \RR$ be a continuous function, then
	\[ \exists c,d \in [a,b] : f(c) \leq f(x) \leq f(d) \forall x \in [a,b] \]
\end{thm}

\begin{proof}
	$\{f(x) \mid x \in [a,b] \}$ is bounded, so $m = \inf \{f(x)\}$ exists.

	$m+\eps$ is not a lower bound, hence $\exists y : f(y) < m+\eps$.

	If $\eps = 1/n$, then denote with $y_n$ the corresponding $y$.

	\[
		m \leq f(y_n) < m + \frac{1}{n}
		\implies |f(y_n) -m| < \frac{1}{n} \, \forall n \in \NN
	\]
	\[ \implies \lim f(y_n) = m \]

	$y_n \in [a,b] \forall n \in \NN$, hence $y_n$ is bounded and
	has a convergent subsequence.

	\[
		\lim y_{n_k} = c \implies f(\lim y_{n_k}) = f(c)
		= \lim f(y_{n_k}) = m
	\]
	\[ \implies m = f(c) \implies f(x) \geq f(c) \forall x \in [a,b] \]

	Similarly, $M = \sup \{f(x)\}$, hence $M-\eps$ is not an upper bound and
	$\exists y \in (a,b) : M \geq f(y) > M-\eps$.
	\[ \implies |f(y) - M| < \eps \]

	Let $\eps = \frac{1}{n}$ for $y_n$, then $\lim f(y_n) = M$.
	Therefore there is a subsequence $y_{n_k}$ such that $\lim y_{n_k} = d$.

	\[ \implies f(\lim y_{n_k}) = f(d) = \lim f(y_{n_k}) = M \]
	\[ \implies f(d) = M \implies f(x) \leq f(d) \forall x \in [a,b] \]

	The extrema are reached at least once, simply because the limits
	have to exist within the bounded subsets in which their
	sequences are contained.
\end{proof}

\subsection{Applications of IVT}

\begin{itemize}
	\item If $f:[a,b] \to \RR$ is continuous, with $f(a) < 0$ and
		$f(b) > 0$, then $\exists c \in (a,b) : f(c) = 0$
	\item Let $f:[0,1] \to (0,1)$ be continuous, then
		$\exists c \in (0,1): f(c) = c$
\end{itemize}

\begin{proof}
	Let $g(x) = x - f(x)$, then $g(0) = -f(0) < 0$ and $g(1) = 1 - f(1) > 0$,
	since $f(x) \in (0,1)$.

	\[ \implies \exists c \in (0,1) : g(c) = 0 \implies f(c) = c \]
\end{proof}

\subsection{Differentiable Functions}

\begin{defn}[Differentiable]
	A function $f$ is differentiable at $c$ if the following limit exists:
	\[ \lim_{h \to 0} \frac{f(c+h) - f(c)}{h} \]
	$f$ is called differentiable if it is differentiable at any point in it's domain.
\end{defn}

\begin{lemma}
	$f$ differentiable at $c$ implies $f$ continuous at $c$.
\end{lemma}

\begin{proof}
	Let $f$ be differentialble at $c$.
	\[
		\implies f'(c) = \lim_{h \to 0} \frac{f(c+h) - f(c)}{h}
		= \lim_{x \to c} \frac{f(x) - f(c)}{x-c}
	\]
	\begin{align*}
		\lim_{x \to c} f(x) - f(c)
		&= \lim_{x \to c}(x-c) \lim_{x \to c} \frac{f(x) - f(c)}{x-c} \\
		&= 0
	\end{align*}
	\[
		\implies \lim_{x \to c} f(x) = f(c)
	\]
	hence $f$ is continuous at $c$.
\end{proof}

\begin{rem}
	Not all continuous functions are differentiable, e.g.\ $f(x) = |x|$.
\end{rem}

\begin{lemma}[Product Rule]
	$(f(x)g(x))' = f(x)g'(x) + f'(x)g(x)$.
\end{lemma}

\begin{proof}
	\begin{align*}
		&\lim_{h \to 0} \frac{f(x+h)g(x+h) - f(x)g(x)}{h} \\
		&= \lim_{h \to 0} \frac{f(x+h)g(x+h) - f(x+h)g(x) + f(x+h)g(x) - f(x)g(x)}{h} \\
		&= \lim_{h \to 0} \frac{f(x+h)(g(x+h) - g(x))}{h}
		+ \lim_{h \to 0} \frac{g(x)(f(x+h) - f(x))}{h} \\
		&= \lim_{h \to 0} f(x+h) \lim_{h \to 0} \frac{g(x+h) - g(x)}{h}
		+ g(x) \lim_{h \to 0} \frac{f(x+h) - f(x)}{h} \\
		&= f(x)g'(x) + g(x)f'(x)
	\end{align*}
\end{proof}

\begin{lemma}[Quotient Rule]
	\[ \left( \frac{f}{g} \right)' = \frac{f'g - g'f}{g^2} \]
\end{lemma}

\begin{proof}
	\begin{align*}
		&\lim_{h \to 0} \left(\frac{f(x+h)}{g(x+h)} - \frac{f(x)}{g(x)}\right)/h \\
		&= \lim_{h \to 0} \frac{f(x+h)g(x) - f(x)g(x+h)}{hg(x)g(x+h)} \\
		&= \lim_{h \to 0} \frac{f(x+h)g(x) - f(x)g(x) - f(x)g(x+h) + f(x)g(x)}{hg(x)g(x+h)} \\
		&= \lim_{h \to 0} \left(
			g(x)\frac{f(x+h) - f(x)}{h} - f(x)\frac{g(x+h) - g(x)}{h}
		\right)/g(x)g(x+h)
	\end{align*}
\end{proof}

\begin{lemma}
	$f$ is differentiable at $c$ iff $\exists A \in \RR$ and a continuous funtion $\alpha$ such that
	\[ f(x) = f(c) + A(x-c) + \alpha(x)(x-c) \]
	where $\lim_{x \to c} \alpha(x) = 0$.
	If this is so, then $A = f'(c)$.
\end{lemma}

\begin{proof}
	Assume $A$ and $\alpha$ exist.
	\[ f(c+h) = f(c) + Ah + \alpha(c+h)h \]
	\[
		\lim_{h \to 0} \frac{f(c+h) - f(c)}{h}
		= \lim_{h \to 0} \frac{Ah + h\alpha(c+h)}{h} = A
	\]
	\[ \implies f'(c) = A\]
	(since $\alpha(c) = 0$).

	Assume $f$ differentiable at $c$.

	Let $f'(c) = A = \lim_{h \to 0}\frac{f(c+h) - f(c)}{h}$.
	Let
	\[
		\alpha(x) =
		\begin{cases}
			\frac{f(x) - f(c)}{x-c} - A, &x \neq c \\
			0, &x = c
		\end{cases}
	\]
	\[ f(x) = f(c) + (x-c)A + (x-c)\alpha(x) \]
	Need to show that $\lim_{x \to c} \alpha(x) = 0$.

	\begin{align*}
		\lim_{x \to c} \alpha(x) &= \lim_{x \to c}\frac{f(x)-f(c)}{x-c} - f'(c) \\
		x = c+h &\implies \lim_{h \to 0} \frac{f(c+h)-f(c)}{h} - f'(c) \\
			&= 0
	\end{align*}
\end{proof}

\begin{lemma}[Chain Rule]
	Let $f:(a,b) \to (c,d) \subset \RR$ and $g:(c,d) \to \RR$, where
	$f$ is differentiable at $x_0 \in (a,b)$ and $g$ is
	differentiable at $y_0 = f(x_0)$. Then, $\phi(x) = g(f(x))$ is
	differentiable at $x_0$, and
	\[ \phi'(x_0) = g'(f(x_0))f'(x_0). \]
\end{lemma}

\begin{proof}
	Using the previous lemma, we know that:
	\[ f(x) = f(x_0) + A(x-x_0) + \alpha(x)(x-x_0) \]
	where
	\[ A = f'(x_0), \, \lim_{x \to x_0} \alpha(x) = 0 \]

	Similarly,
	\[ g(y) = g(y_0) + B(y-y_0) + \beta(y)(y-y_0) \]
	where
	\[ B = g'(y_0), \, \lim_{y \to y_0} \beta(y) = 0 \]

	Let $y = f(x)$ and $y_0 = f(x_0)$, then
	\[ g(f(x)) = g(f(x_0)) + B(f(x)-f(x_0)) + \beta(f(x))(f(x) - f(x_0)) \]
	\[
		\implies \phi(x) = \phi(x_0) + B(A(x-x_0) + (x-x_0)\alpha(x))
		+ \beta(f(x))(A(x-x_0) + (x-x_0)\alpha(x))
	\]
	\[ \phi(x_0) + BA(x-x_0) + \gamma(x)(x-x_0) \]
	where
	\[ \gamma(x) = B\alpha(x) + A\beta(f(x)) + \alpha(x)\beta(f(x)) \]

	$\lim_{x \to x_0} \gamma(x) = 0$ since $\lim_{x \to x_0} \alpha(x) = \lim_{x \to x_0} \beta(f(x)) = 0$.

	\[ \implies BA = \phi'(x_0) \]
	\[ \implies \phi'(x_0) = g'(f(x_0))f'(x_0) \]
\end{proof}

\begin{defn}[Local Maximum]
	Let $f:[a,b] \to \RR$. $x_0$ is a local maximum of $f$ if $\exists \delta > 0$ such that
	\[ f(x) \leq f(x_0) \forall x \in (x_0 - \delta, x_0 + \delta) \subset [a,b]. \]
\end{defn}

\begin{thm}[Fermat]
	Assume $f:[a,b] \to \RR$ is continuous, and differentiable at
	$x_0$. If $x_0$ is a local maximum, then $f'(x_0) = 0$.
\end{thm}

\begin{proof}
	Let $\delta > 0$ and $U = (x_0 - \delta, x_0 + \delta) \subset [a,b]$, where $x_0$ is a local max.

	Take $h > 0$ such that $x_0 + h \in U$, then
	\[
		\lim_{h \to 0} \frac{f(x_0 + h) - f(x_0)}{h} \leq 0
	\]

	We may also take $h < 0$ such that $x_0 + h \in U$, and
	\[
		\lim_{h \to 0} \frac{f(x_0 + h) - f(x_0)}{h} \geq 0
	\]

	\[ \implies f'(x_0) = 0 \]
\end{proof}

\begin{defn}[Critical Point]
	A point $x_0 \in (a,b)$ of $f:[a,b] \to \RR$ iff $f'(x_0) = 0$.
	Note that any local maxiumum or minimum is a critical point.
\end{defn}

\begin{cor}
	$\max f(x) = \max \{ f(A), f(B), f(x_1), \ldots, f(x_n) \}$, where
	$x_1, \ldots, x_n$ are critical points of $f$.
\end{cor}

\begin{ex}
	\[ f:[0,2] \to \RR, \quad f(x) = 1 + 5x - x^5 \]
	\begin{align*}
		f'(x) = 5 - 5x^4 &= 0 \\
		x^4 &= 1 \\
		x &= \pm 1
	\end{align*}

	$-1 \not \in [0,2]$ so $1$ is the only critical point we care about.

	We now check the values of the bounds and critical points:
	\[ f(1) = 1 \]
	\[ f(0) = 1 \]
	\[ f(2) = -21 \]
	hence, $\max f(x) = 1$ (on $[0,2]$).
\end{ex}

\begin{thm}[Rolle's Theorem]
	Let $f:[a,b] \to \RR$ be a continuous funtion, which is
	differentiable on $(a,b)$. If $f(a) = f(b)$, then there is $c
	\in (a,b)$ such that $f'(c) = 0$.
\end{thm}

\begin{proof}
	We know there are points $x_0$ and $x_1$ in $[a,b]$ such that
	$f(x_0) \leq f(x) \leq f(x_1) \forall x \in [a,b]$. (By taking
	the max and min of the endpoints and critical points).

	If $x_1$ and $x_0$ are the endpoints of the interval $[a,b]$,
	then the function must be constant, so $f'(x) = 0 \forall x \in
	[a,b]$.

	Assume that $x_1$ is within $(a,b)$, then $x_1$ is a local
	maximum and, by Fermat's Theorem, is a critical point. Hence
	$f'(x_1) = 0$.
\end{proof}

\begin{thm}[Mean Value Theorem (Lagrange)]
	Let $f:[a,b] \to \RR$ be a continuous function, which is
	differentiable on $(a,b)$, then:
	\[ \exists c \in (a,b) : f(b) - f(a) = f'(c)(b-a) \]
\end{thm}

\begin{proof}
	Consider a function
	\[ g(x) = f(x) - \frac{f(b) - f(a)}{b-a}x \]
	\begin{align*}
		g(b) - g(a) &= f(b) - \frac{f(b) - f(a)}{b-a}b
		- f(a) + \frac{f(b) - f(a)}{b-a}a \\
		&= \left( f(b) - f(a) \right)
		\left(1 - \frac{b}{b-a} + \frac{a}{b-a} \right) \\
		&= 0
	\end{align*}

	By Rolle's theorem, there is $c \in (a,b)$ such that $g'(c) = 0$.

	\[ \implies f'(c) = \frac{f(b) - f(a)}{b-a} \]
\end{proof}

\begin{cor}
	Let $f:[a,b] \to \RR$ be a continuous function, which is differentiable in $(a,b)$, then:
	\begin{enumerate}
		\item If $f'(x) > 0 \forall x \in (a,b)$, then f is strictly increasing.
		\item If $f'(x) < 0 \forall x \in (a,b)$, then f is strictly decreasing.
		\item If $f'(x) = 0 \forall x \in (a,b)$, then f is constant.
	\end{enumerate}
\end{cor}

\begin{proof}
	\begin{enumerate}
		\item Take $[c,d] \subset [a,b]$ and $x_0 \in (c,d)$, then by MVT we have
			\[ f(d) - f(c) = f'(x_0)(d-c) > 0 \implies f(d) > f(c) \]
		\item Similar to 1.
		\item Take $c,d \in [a,b]$, then $\exists x_0 \in (c,d)$ such that
			\[ f(d) - f(c) = f'(x_0)(d-c) = 0 \implies f(d) = f(c) \]
	\end{enumerate}
\end{proof}

\begin{ex}
	\[ f(x) = \frac{1}{3}x^3 - 3x^2 + 8x - 5 \]

	\[ f'(x) = x^2 - 6x + 8 = (x-4)(x-2) \]
	hence $x=4,2$ are critical points.

	\[ f'(3) = 9 - 18 + 8 = -1 \implies f'(x) < 0 \forall x \in (2,4) \]

	\[ f'(x) > 0 \forall x \in \RR \setminus [2,4] \]

	So $f(x)$ is increasing in $(2,4)$ and decreasing in $\RR \setminus [2,4]$.
\end{ex}

\begin{thm}[Cauchy MVT Generalisation]
	Assume $f,g:[a,b] \to \RR$ are continous and differentiable in $(a,b)$, then
	\[ \exists c \in (a,b) : (g(b) - g(a))f'(c) = (f(b) - f(a))g'(c) \]
\end{thm}

\begin{proof}
	If $g(b) = g(a)$ then by Rolle's Theorem, $\exists c \in (a,b) : g'(c) = 0$, and we are done.

	Assume $g(a) \neq g(b)$.

	Let $h(x) = (g(b) - g(a))f(x) - (f(b) - f(a))g(x)$.

	\begin{align*}
		h(b) - h(a) &= f(b)g(b) - f(b)g(a) - f(b)g(b) + f(a)g(b) \\
		&- f(a)g(b) + f(a)g(a) + f(b)g(a) - f(a)g(a) \\
		&= 0
	\end{align*}

	\[ \implies \exists c \in (a,b) : h'(c) = 0 \]

	\[ h'(x) = (g(b) - g(a))f'(x) - (f(b) - f(a))g'(x) \]
	\[ h'(c) = 0 \implies (g(b) - g(a))f'(c) = (f(b) - f(a))g'(c) \]
\end{proof}

\begin{rem}
	Setting $g$ to the identity map yields the standard MVT\@.
\end{rem}

\begin{lemma}[L'hopital Rule]
	Assume $f$ and $g$ are differentiable in $(a,b)$ and $c \in (a,b) : f(c) = g(c) = 0$.
	Moreover, let $g(x), g'(x) \neq 0 \forall x \neq c$. Then
	\[ \lim_{x \to c} \frac{f(x)}{g(x)} = \lim_{x \to c} \frac{f'(x)}{g'(x)} \]
\end{lemma}

\begin{proof}
	Let $x_n \in (c,b) : \lim_{n \to \infty} x_n = c$.

	By Cauchy MVT, $\exists y_n \in (c, x_n)$ such that
	\[ (g(x_n) - g(c))f'(y_n) = (f(x_n) - f(c))g'(y_n) \]

	By assumption, $f(c) = g(c) = 0$, so
	\[
		g(x_n)f'(y_n) = f(x_n)g'(y_n)
		\implies
		\frac{f'(y_n)}{g'(y_n)} = \frac{f(x_n)}{g(x_n)}
	\]

	\[ x_n \to c \implies y_n \to c \]

	\[
		\implies
		\lim_{n \to \infty}\frac{f(x_n)}{g(x_n)}
		= \lim_{n \to \infty}\frac{f'(y_n)}{g'(y_n)}
		= \lim_{x \to c^+}\frac{f'(x)}{g'(x)}
	\]

	We can use a similar method for $x_n \in (a,c)$, hence
	\[
		\lim_{x \to c^-}\frac{f(x)}{g(x)}
		= \lim_{x \to c^+}\frac{f(x)}{g(x)}
		= \lim_{x \to c}\frac{f'(x)}{g'(x)}
	\]
\end{proof}

\begin{ex}
	\[ \lim_{x \to 0} \frac{x - \sin x}{x^3} \]

	$x \to 0 \implies f(x), g(x) \to 0$, so we can apply l'hopital.

	\[
		\lim_{x \to 0} \frac{x - \sin x}{x^3}
		= \lim_{x \to 0} \frac{1 - \cos x}{3x^2}
	\]

	$x \to 0 \implies f(x), g(x) \to 0$, so we can apply l'hopital again.

	\[
		\lim_{x \to 0} \frac{1 - \cos x}{3x^2}
		= \lim_{x \to 0} \frac{\sin x}{6x}
		= \lim_{x \to 0} \frac{\cos x}{6}
		= \frac{1}{6}
	\]

\end{ex}

\section{Riemann Integration}

Denote by $B_{[a,b]}$ the set of all bounded functions on $[a,b]$, and
by $C_{[a,b]}$ the set of all continuous functions on $[a,b]$. Note that
$C_{[a,b]} \subset B_{[a,b]}$.

\begin{defn}[Partition]
	A partition of the interval $[a,b]$ is a (strictly increasing)
	sequence of numbers, $P := \{x_0, \ldots, x_n\}$, where $x_0 = a$
	and $x_n = b$.
\end{defn}

\begin{defn}[Partition width]
	The width of a partition $P$ is given
	\[ ||P|| = \max\{x_{i+1} - x_i\}.\]
\end{defn}

\begin{defn}[Refinement]
	If $P$ and $P'$ are partitions of $[a,b]$, then if $P \subset
	P'$, we say that $P'$ is a refinement of $P$.
\end{defn}

\begin{rem}
	If $P'$ is a refinement of a partition $P$, then $||P'|| \leq ||P||$.
\end{rem}

\begin{defn}[Riemann Sums]
	Let $f:[a,b] \to \RR$ be a bounded function and $P$ a partition of $[a,b]$.

	The lower Riemann sum of $f$ with respect to $P$ is
	\[ L(f,P) = \sum_{i=0}^{n-1} m_i (x_{i+1} - x_i) \]
	where $m_i = \inf_{x_i \leq x \leq x_{i+1}} \{f(x)\}$.

	The upper Riemann sum of $f$ with respect to $P$ is
	\[ U(f,P) = \sum_{i=0}^{n-1} M_i (x_{i+1} - x_i) \]
	where $M_i = \sup_{x_i \leq x \leq x_{i+1}} \{f(x)\}$.

	$m_i$ and $M_i$ are well defined as a result of the boundedness of $f$.
\end{defn}

\begin{rem}
	\[ L(f,P) \leq U(f,P) \]
\end{rem}

\begin{lemma}
	Assume $P'$ is a refinement of a partition $P$, then for a bounded function $f$
	\[ L(f,P) \leq L(f,P'), \quad U(f,P') \leq U(f,P). \]
\end{lemma}

\begin{proof}
	Consider $P = \{x_0, \ldots, x_n\}$ and $P' = \{x_0, x', x_1, \ldots, x_n\}$.

	\[ L(f,P) = m_0(x_1 - x_0) + \cdots + m_{n-1}(x_n - x_{n-1}) \]
	\[ L(f,P') = m_0'(x' - x_0) + m_0''(x_1 - x') + \cdots + m_{n-1}(x_n - x_{n-1}) \]

	It is clear that $m_0' \geq m_0$ and $m_0'' \geq m_0$, since $m_0 \in f([x_0, x'])$
	or $m_0 \in f([x', x_1])$ or bothe (if $m_0 = f(x')$).

	\begin{align*}
		L(f,P) - L(f,P') &= m_0(x_1 - x_0) - m_0(x' - x_0) - m_0''(x_1 - x') \\
				 &\leq m_0(x_1 - x_0 - x' + x_0 - x_1 + x') \\
		   		 &= 0
	\end{align*}
	\[ \implies L(f,P) \leq L(f,P') \]

	Similarly, $M_0'' \leq M_0$ and $M_0' \leq M_0$.
	\begin{align*}
		U(f,P) - U(f,P') &= M_0(x_1 - x_0) - M_0'(x' - x_0) + M_0''(x_1 - x') \\
				 &\geq 0
	\end{align*}
	\[ \implies U(f,P) \geq U(f,P') \]
\end{proof}

\begin{lemma}
	If $P$ and $Q$ are partitions of $[a,b]$, then
	\[ L(f, P) \leq U(f, Q) \]
\end{lemma}

\begin{proof}
	Let $R = P \cup Q$ It is clear that $R$ is a partition of
	$[a,b]$ and a refinement of both $P$ and $Q$.

	We know that $L(f,R) \leq U(f,R)$ and from the previous lemma:
	\[ L(f,P) \leq L(f,R) \quad U(f,R) \leq U(f,Q) \]

	\[ \implies L(f,P) \leq U(f,Q) \]
\end{proof}

\begin{defn}[Lower Integral]
	The lower integral of the function $f:[a,b] \to \RR$ is
	\[ (L) \int_a^b f(x) dx = \sup \{L(f,P)\} \]
	where $P$ is any partition of $[a,b]$.
\end{defn}

\begin{defn}[Upper Integral]
	The upper integral of the function $f:[a,b] \to \RR$ is
	\[ (U) \int_a^b f(x) dx = \inf \{U(f,P)\} \]
	where $P$ is any partition of $[a,b]$.
\end{defn}

\begin{defn}[Upper Integral]
	A function $f:[a,b] \to \RR$ is called Riemann Integrable if
	\[ (L) \int_a^b f(x) dx = (U) \int_a^b f(x) dx \]

	This common value is called the Riemann Integral of $f$ and is simply denoted
	\[ \int_a^b f(x) dx \]
\end{defn}

\begin{rem}
	\begin{itemize}
		\item The set of all Riemann Integrable funciton is denoted $R_{[a,b]}$
		\item $R_{[a,b]} \subset B_{[a,b]}$
	\end{itemize}
\end{rem}

\begin{defn}[Uniform Continuity]
	$f$ is uniformly continuous if
	\[ \forall \eps > 0 \exists \delta > 0 : |x-y| < \delta \implies |f(x) - f(y)| < \eps \]
\end{defn}

\begin{rem}
	The difference between regular and uniform continuity is that
	uniform continuity depends only on $\eps$, whereas regular
	continuity depends on a point in the domain of $f$.
\end{rem}

\begin{thm}
	If $f:[a,b] \to \RR$ is continuous, then $f$ is uniformly continuous.
\end{thm}

\begin{proof}
	Assume $f$ is not uniformly continuous, then:
	\[ \exists \eps > 0 \forall \delta > 0 : \exists x, y : |x-y| < delta \implies |f(x)-f(y)| \geq \eps \]

	Fix $\eps$ and take $\delta = \frac{1}{n}$
	\[ \exists x_n, y_n \in [a,b] : |x_n - y_n| < \frac{1}{n} \implies |f(x_n)-f(y_n)| \geq \eps \]

	We have two bounded sequences which, by the Bolzano-Wieirstrass theorem, have convergent subsequences.
	\[ x_n, \ldots, x_{m_l} \to c \in [a,b] \]
	\[ y_n, \ldots, y_{m_l} \to d \in [a,b] \]

	\[ |x_{m_l} - y_{m_l}| < \frac{1}{n} \implies c = d \]
	\[ f(x_{m_l})- f(y_{m_l}) \geq \eps \]

	\[ \implies \lim_{l \to \infty} |f(x_{m_l}) - f(y_{m_l})| \geq \eps \]
	\[ \implies | \lim f(x_{m_l}) - \lim f(y_{m_l}) | \geq \eps \]
	\[ \implies | f(\lim x_{m_l}) - f(\lim f_{m_l}) | \geq \eps \]
	\[ \implies |f(c) - f(d)| \geq \eps \]

	This is a contradiction because $c=d$ and $\eps > 0$.
\end{proof}

\begin{thm}
	A function $f:[a,b] \to \RR$ is integrable iff
	\[ \forall \eps > 0 \exists \delta : ||P|| < \delta \implies U(f,P) - L(f,P) < \eps \]
\end{thm}

\begin{proof}
	$f$ integrable
	\begin{align*}
		&\implies \inf \{ U(f,P) \} = \sup \{ L(f,P) \} \\
		&\implies \lim_{||P|| \to 0} U(f,P) = \lim_{||P|| \to 0} L(f,P) \\
		&\implies (\forall \eps > 0 \exists \delta : ||P|| < \delta \implies U(f,P) - L(f,P) < \eps)
	\end{align*}
	This follows from the result that $L(f,P) \leq L(f,P')$ and $U(f,P) \geq U(f,P')$.
\end{proof}

\begin{thm}
	Any continuous function $f:[a,b] \to \RR$ is integrable.
\end{thm}

\begin{proof}
	Continuity implies uniform continuity, hence
	\[
		\forall \eps > 0 \exists \delta : |x-y| < \delta
		\implies |f(x)-f(y)| < \frac{\eps}{2(b-a)}
	\]

	Let $P$ be a partition of $[a,b]$ such that $||P|| < \delta$.

	\begin{align*}
		U(f,P) - L(f,P)
		&= \sum_{i=0}^{n-1} M_i (x_{i+1} - x_i) - \sum_{i=0}^{n-1} m_i (x_{i+1} - x_i) \\
		&= \sum_{i=0}^{n-1} (M_i - m_i)(x_{i+1} - x_i) \\
		&\leq \sum_{i=0}^{n-1} \frac{\eps}{2(b-a)} (x_{i+1} - x_i)
	\end{align*}

	since
	\[
		||P|| < \delta \implies x_{i+1} - x_i < \delta
		\implies M_i - m_i < \frac{\eps}{2(b-a)}
	\]

	\begin{align*}
		\sum_{i=0}^{n-1} x_{i+1} - x_i
		&= (x_1 - x_0 + x_2 - x_1 + \cdots + x_n - x_{n-1}) \\
		&= x_n - x_0 \\
		&= b - a
	\end{align*}

	\[
		\implies U(f,P) - L(f,P) \leq \frac{\eps(b-a)}{2(b-a)}
		= \frac{\eps}{2} < \eps
	\]

	It follows from the previous theorem that $f$ must be integrable.

\end{proof}

\begin{defn}[Primtive]
	A primitive of a function $f:[a,b] \to \RR$ is a function $F:[a,b] \to \RR$, where
	\[ F'(x) = f(x). \]
\end{defn}

\begin{rem}
	If $F(x)$ is a primative of $f(x)$, then $F(x) + c$ is also a primitive of $f(x)$.
	Furthermore, if $F_1$ and $F_2$ are primitives of $f$, then $F_1 = F_2 + c$.
\end{rem}

\begin{thm}[Fundamental Theorem of Calculus]
	If $F$ is a primitve of $f:[a,b] \to \RR$, then
	\[ \int_a^b f(x) dx = F(b) - F(a) \]
\end{thm}

\begin{proof}
	Let $P$ be a partition of $[a,b]$.

	Using MVT, we have:
	\[ F(x_{i+1}) - F(x_i) = F'(y_i)(x_{i+1} - x_i) \]
	for some $y_i \in [x_i, x_{i+1}]$.

	\[ m_i \leq f(y_i) = F'(y_i) \leq M_i \]
	\begin{align*}
		&\implies L(f,P) \leq \sum_{i=0}^{n-1} f(y_i)(x_{i+1}-x_i) \leq U(f,P) \\
		&\implies L(f,P) \leq \sum_{i=0}^{n-1} F(x_{i+1}) - F(x_i) \leq U(f,P) \\
		&\implies L(f,P) \leq F(b) - F(a) \leq U(f,P) \\
		&\implies \int_a^b f(x) dx = F(b) - F(a)
	\end{align*}
\end{proof}

\subsection{Properties of Integrals}

Let $f,g : [a,b] \to \RR$ be integrable, then
\begin{enumerate}
	\item
		$\alpha f + \beta g$ is integrable and
		\[ \int_a^b \alpha f + \beta g = \alpha \int_a^b f + \beta \int_a^b g \]
	\item
		\[ \int_a^b f = \int_a^c f + \int_c^b f, \quad \forall c \in [a,b] \]
	\item
		if $|f(x)| \leq M$,
		\[ \left| \int_a^b f \right| \leq M |b-a| \]
\end{enumerate}

\begin{proof}
	\begin{enumerate}
		\item
			Let $h = \alpha f + \beta g$, and let $F,G$ be primitives of $f,g$.
			Let $H = \alpha F + \beta G + c$, then $H' = \alpha f + \beta g$.

			\begin{align*}
				\int_a^b h &= H(b) - H(a) \\
					   &= \alpha F(b) - \alpha F(a)
						+ \beta G(b) - \beta G(a) \\
					   &= \alpha \int_a^b f + \beta \int_a^b g
			\end{align*}
		\item
			\begin{align*}
				\int_a^c f + \int_c^b f &= F(c) - F(a) + F(b) - F(c) \\
							&= F(b) - F(a) \\
				   			&= \int_a^b f
			\end{align*}
		\item
			Let $P = \{a,b\}$ be a partition of $[a,b]$, then
			\begin{align*}
				|f(x)| \leq M &\implies \sup\{f(x)\} = M_i = M \\
					      &\implies U(f,P) = M(b-a)
			\end{align*}
			Let  $P' = \{a,c,b\}$ be a refinement of $P$, then
			\[ U(f,P') \leq U(f,P) \leq M(b-a) \]
			\[ \int_a^b f \leq \inf\{U(f,P)\} \implies \int_a^b f \leq M(b-a) \]
	\end{enumerate}
\end{proof}

\begin{thm}
	Let $f:[a,b] \to \RR$ be an integrable function. Consider the function
	\[ F(x) = \int_a^x f(t) dt \]
	then $F:[a,b] \to \RR$ is continuous. Futhermore, if $f$ is
	continuous at $c \in (a,b)$, then $F$ is differentiable at $c$
	and $F'(c) = f(c)$.
\end{thm}

\begin{proof}
	Take $c \in (a,b)$. We need to show that
	\[ \lim_{x \to c} F(x) = F(c) \]
	or
	\[ \lim_{h \to o} F(c+h) = F(c). \]

	\begin{align*}
		|F(c+h) - F(c)| &= \left| \int_a^{c+h} f(t) - \int_a^c f(t) \right| \\
				&= \left| \int_a^c f(t) + \int_c^{c+h} f(t) - \int_a^c f(t) \right| \\
			 	&= \left| \int_c^{c+h} f(t) \right|
	\end{align*}

	$f$ is bounded, so
	\[ \exists M : |f(x)| \leq M \]
	\[ \implies | F(c+h) - F(c) | \leq M(c+h-c) = Mh \]

	\[ \lim_{h \to 0} Mh = 0 \implies \lim_{h \to 0} F(c+h) = F(c) \]
	hence $F$ is continuous at $c \in [a,b]$.

	We now aim to prove that $F'(c) = f(c)$, or
	\[ \lim_{h \to 0} \frac{F(c+h) - F(c)}{h} = f(c) \]

	\begin{align*}
		\left|\frac{1}{h} \int_a^{c+h} f(t) - \frac{1}{h} \int_a^c f(t) - f(c) \right| \\
		&= \left| \frac{1}{h} \int_c^{c+h} f(t) - f(c) \right| \\
		&= \left| \frac{1}{h} \int_c^{c+h} f(t) - \frac{h}{h} f(c) \right| \\
		&= \left| \frac{1}{h} \int_c^{c+h} f(t) - \frac{1}{h} \int_c^{c+h} f(c) \right| \\
		&= \left| \frac{1}{h} \int_c^{c+h} (f(t) - f(c)) \right|
	\end{align*}

	By assumption, $f$ is continuous, hence
	\begin{align*}
		&\implies \forall \eps > 0 \exists \delta > 0 : |t-c| \implies |f(t)-f(c)| < \eps \\
		&\implies \frac{1}{h} \int_c^{c+h} f(t) - f(c) \leq \frac{\eps(c+h-c)}{h} = \eps \\
		&\implies \lim_{h \to 0} \frac{F(c+h) - F(c)}{h} = f(c) \\
		&\implies F'(c) = f(c)
	\end{align*}
\end{proof}

\end{document}


\lab{Algorithms}{Modified Gram-Schmidt (QR)}{QR decomposition}
\label{lab:QRdecomp}
\objective{Use Gram-Schmidt algorithm and orthonormal transformations to perform QR decomposition.}

The QR decomposition is used to represent any invertible square matrix as the matrix product of an orthogonal ($A^T A = I$) matrix, $Q$, and an upper triangular matrix, $R$.
This decomposition is useful in computing least squares and is part of a common method for finding eigenvalues (the QR algorithm).
We can succinctly state the QR decomposition in the following theorem.
\begin{theorem}
Let $A$ be an $m\times n$ matrix of rank $n$.  Then $A$ can be
factored into a product $Q R$, where $Q$ is an $m\times n$ matrix
with orthonormal columns and $R$ is a nonsingular $n \times n$ upper
triangular matrix.
\end{theorem}

\section*{Computing the QR Decomposition}
There are many methods for computing the QR factorization.
This lab will focus on the method that uses the Gram-Schmidt algorithm for orthogonalizing
a full-rank matrix.
We use this algorithm to create $Q$.
Let $\{\x_i\}_{i=1}^n$ be a basis for the inner product space $V$.
Let \[ \q_1 = \frac{\x_1}{\norm{\x_1}}\] and define $\q_2,\q_3,\ldots,\q_n$ recursively by
$$
\q_{k} = \frac{\x_k - \p_{k-1}}{\|\x_k - \p_{k-1}\|}, \,\,\,\, k=2,\ldots,n,
$$
where
$$
\p_{k-1} = \sum_{i=1}^{k-1} \langle \q_i, \x_k\rangle \q_i,
$$
and $\p_0 = 0$.
Then the set $\{\q_i\}_{i=1}^n$ is an orthonormal basis for $V$.

For the above algorithm, let $r_{j k} = \langle \q_j, \x_k\rangle$ when $j < k$ and
$r_{kk} = \|\x_k-\p_{k-1}\|$.
This can be written as
\begin{align*}
\x_1 &= r_{1 1} \q_1\\
\x_2 &= r_{1 2} \q_1 + r_{2 2} \q_2\\
\vdots \:\: &= \quad \vdots\\
\x_n &= r_{1 n} \q_1 + r_{2 n} \q_2 + \ldots + r_{n n} \q_{n},
\end{align*}
or in matrix form as
\[
[\x_1 \hspace{5mm} \x_2 \hspace{5mm} \cdots \hspace{5mm} \x_n]
=
[\q_1 \hspace{5mm} \q_2 \hspace{5mm} \cdots \hspace{5mm} \q_n]
\begin{bmatrix}
r_{1 1} & r_{1 2} & \cdots & r_{1 n}\\
0 & r_{2 2} & \cdots & r_{2 n}\\
\vdots & \vdots & \ddots & \vdots\\
0 & 0 & \cdots & r_{n n}
\end{bmatrix}.
\]
Hence if our original basis vectors $\{\x_i\}_{i=1}^n$ correspond to column
vectors of a matrix $A$, we can likewise write the resulting
orthonormal basis $\{\q_i\}_{i=1}^n$ as a matrix $Q$ of column
vectors.  Then we have that $A = Q R$, where $R$ is the above
nonsingular upper-triangular $n\times n$ matrix.
Numerically, the Gram Schmidt process can have problems due to
finite precision arithmetic.

In some cases, rounding errors may cause the resulting basis to fail to be orthonormal. To combat this, we consider the Modified Gram-Schmidt which can be used to carry out a slightly revised algorithm.  To do this, we first compute $\q_1$ as before.  We then project it out of each of the remaining original vectors
$\x_2,\x_3,\ldots,\x_n$ via
\[
\x_k := \x_k - \langle \q_1,\x_{k} \rangle \q_1,\quad k=2,\ldots,n.
\]
Then we compute $\q_2$ to be the unit vector of $\x_2$, that is,
\[
\q_2 = \frac{\x_2}{\|\x_2\|}.
\]
We repeat by projecting out $\q_2$ from the remaining vectors
$\x_3,\x_4,\ldots,\x_n$, and then continuing this process until
all $\q_i$ are obtained.

\begin{algorithm}
\caption{Modified Gram-Schmidt}
\label{Alg:MGS}
\begin{algorithmic}[1]
\Procedure{Modified Gram-Schmidt}{$A$}
\State $m, n \gets \text{shape} \left( A \right)$
\State $Q \gets \text{copy} \left( A \right)$
\State $Q \gets \text{zeros}((n,n))$
\For{$0 \leq i < n$}
    \State $R_{i,i} \gets \norm{Q_{:,i}}$
    \State $Q_{:,i} \gets Q_{:,i}/R_{i,i}$
    \For{$i+1 \leq j < n$}
        \State $R_{i,j} \gets Q_{:,j}Q_{:,i}$
        \State $Q_{:,j} \gets Q_{:,j}-R_{i,j}Q_{:,i}$
	\EndFor
\EndFor
\State \pseudoli{return} $Q, R$
\EndProcedure
\end{algorithmic}
\end{algorithm}

\begin{problem}
\label{prob:QR}
Write your own implementation of the QR decomposition. Write a function \li{QR} that accepts as input a square matrix $A$ of full rank, and computes the QR decomposition, returning the matrices $Q$ and $R$ (which should be the same shape as $A$). Be sure to use the numerically stable Modified Gram-Schmidt algorithm. Also assume there are no zeros on the main diagonal.

You can test that you have the right decomposition by verifying that $QR=A$ and $Q^T Q = I$. While this is true most of the time, it may not always be true due to roundoff.
\end{problem}

\begin{problem}
The QR decomposition gives a really nice way to calculate the magnitude of the determinant of a square matrix of full rank. Write a function \li{QRDet} that accepts a square matrix of full rank as input, and returns the magnitude of the determinant of that matrix. Use your QR decomposition to perform this
calculation.

You may check your work by comparing your results to those produced by \li{scipy.linalg.det}.
\end{problem}

\section*{QR Decomposition in SciPy}
The linear algebra library in SciPy wraps the very efficient algorithms of LAPACK to calculate the QR decomposition.
In addition to being much faster, SciPy's QR decomposition is also much more general and can decompose non-square matrices.

\begin{lstlisting}
>>> import numpy as np
>>> from scipy import linalg as la
>>> A = np.random.rand(4,3)
>>> Q, R = la.qr(A)
>>> Q.dot(R) == A                      # there are False entries
>>> np.allclose(Q.dot(R), A)           # A = QR
>>> np.allclose(Q.T.dot(Q), np.eye(4)) # Q is indeed, orthogonal
\end{lstlisting}

In order to interpret the results correctly, we need to understand that the computer has limited precision (especially with floating point numbers).
This is why \li{Q.dot(R)} is not exactly equal to \li{A}. However, we can see that the matrix product of $QR$ is very close to $A$ using the \li{allclose} method. This verifies that the product of $Q$ and $R$ is indeed $A$. Also note that $Q^T Q = I$, which implies that the column vectors of $Q$ are orthonormal.

\section*{Solving Least Squares Problems}
The QR decomposition can only be used to solve the linear least squares problem if $A$ is full rank. If $A$ is less than full rank, then we have to calculate the least squares solution more creatively.

%MATLAB's least squares backslash operator is based off the QR decomposition.
%SciPy uses the SVD to solve least squares problems because, although it is slower, the algorithm is more numerically stable.

For large or ill-conditioned problems, the QR decomposition provides a nice method for computing least squares solutions of over-determined matrices.
Consider the least squares problem $Ax=b$. We can approximate the solution with $\widehat x = (A^T A)^{-1}A^T b$.
Alternatively, we write the linear system as
\[ Q R x = b. \]
We then multiply both sides by $Q^T$, yielding
\[ R x = Q^T b. \]
Then $\widehat x = R^{-1} Q^T b$.

However, we can avoid calculating the inverse of $R$ (inverting a matrix is \emph{very expensive} computationally).
Since $R$ is a triangular matrix, we have a triangular system that we can solve much more efficiently.
SciPy includes a solver for triangular systems, \li{linalg.solve_triangular()}.
We approximate $x$ by solving the triangular system $Rx = Q^T b$.

\begin{problem}
Write a function \li{LeastSquares} that will accept a linear system (a matrix $A$ and a vector $b$) and solve the least squares problem.
Your function should rely on the SciPy's QR decomposition and triangular system solver.  Assume that $A$ is full rank.
You may test your function against the output of SciPy's least squares function, \li{linalg.lstsq()}.
\end{problem}

\section*{Orthonormal transformations}
Recall that a matrix $Q$ is \emph{unitary} if $Q^\mathsf{H} Q = I$ or, for real matrices, $Q^T Q = I$.
For the real case we say that such a matrix is \emph{orthonormal}.

Unitary transformations have the very desirable property of being numerically stable. The number $\kappa(A) = \norm{A} \norm{A^{-1}}$ is called the \emph{condition number} of $A$. We'll discuss condition number more in another lab; for now, all you need to know is that if $\kappa(A)$ is small, then calculations involving $A$ are less susceptible to numerical errors.

For the induced 2-norm, it holds that $\norm{Q}=1$ when $Q$ is unitary.
The Cauchy-Schwarz inequality

\centerline{$\norm{AB} \leq \norm{A} \norm{B}$}
also holds for this norm, and so it follows that

\centerline{$\kappa(A) = \norm{A} \norm{A^{-1}} \geq \norm{A A^{-1}} = \norm{I} = 1$.}

Note that if $Q$ is unitary, $Q^{-1} = Q^\mathsf{H}$ and $Q^\mathsf{H}$ is also unitary, so $\kappa(Q) = \norm{Q} \norm{Q^\mathsf{H}} = 1$. This means that orthonormal matrices have the smallest possible condition number.

Any orthogonal matrix $Q$ can be described as a reflection, a rotation, or some combination of the two.
If $det(Q) = 1$, then $Q$ is a rotation.
If $det(Q) = -1$, then $Q$  is a reflection or a composition of a reflection and a rotation. Let's explore these two types of unitary transformations and some of their applications. We will focus on the real case to simplify matters.

\section*{Householder reflections}
A Householder reflection is a linear transformation $P: \mathbb{R}^n \rightarrow \mathbb{R}^n$ that reflects a vector $x$ about a hyperplane.
See figure \ref{fig:Householder_reflector}.
Recall that a hyperplane can be defined by a unit vector $v$ which is orthogonal to the hyperplane.
As shown in figure \ref{fig:Householder_reflector}, $x - \langle v,x \rangle v$ is the projection of $x$ onto the hyperplane orthogonal to $v$.
However, to reflect \emph{across} the hyperplane, we must move twice as far; that is, $Px = x - 2\langle v,x \rangle v$.
This can be written $Px = x - 2v(v^\mathsf{H} x)$, so $P$ has matrix representation $P = I - 2v v^\mathsf{H}$.
Note that $P^\mathsf{H} P = I$; thus $P$ is orthonormal.

\begin{figure}
\includegraphics[width= \textwidth]{fig1}
\caption{Householder reflector}
\label{fig:Householder_reflector}
\end{figure}

\subsection*{Householder triangularization}
Consider the problem of computing the $QR$ decomposition of a matrix $A$.
You've already learned the Gram-Schmidt and the Modified Gram-Schmidt algorithms for this problem.
The $QR$ decomposition can also be computed by applying a series of Householder reflections.
Gram-Schmidt and Modified Gram-Schmidt make $A$ \emph{orthonormal} using a series of transformations stored in an \emph{upper triangular} matrix.
On the other hand, we can use Householder reflections to make $A$ \emph{triangular} by a series of \emph{orthonormal} transformations.

Let's demonstrate this method on a $4 \times 3$ matrix $A$.
First we find an orthonormal transformation $Q_1$ that maps the first column of A into the span of $e_1$
(where $e_1$ is the vector where the first element is one and the remainder of the elements are zeros).

\def\mc#1{\multicolumn{1}{c|}{#1}}
\begin{equation*}
\begin{pmatrix}
* & * & * \\
* & * & * \\
* & * & * \\
* & * & *
\end{pmatrix}
\underrightarrow{Q_1}
\begin{pmatrix}

* & * & * & \\ \cline{2-3}
\mc{0} & * & \mc{*}& \\
\mc{0} & * & \mc{*} & \\
\mc{0}& * & \mc{*} & \\ \cline{2-3}
\end{pmatrix}
\end{equation*}
Let $A_2$ be the boxed submatrix of $A$.
Now find an orthonormal transformation $Q_2$ that maps the first column of $A_2$ into the span of $e_2$.

\begin{equation*}
\begin{pmatrix}
* & * \\
* & * \\
* & *
\end{pmatrix}
\underrightarrow{Q_2}
\begin{pmatrix}
* & * \\
0 & * \\
0 & *
\end{pmatrix}
\end{equation*}
Similarly, $ \begin{pmatrix} * \\ * \end{pmatrix} \underrightarrow{Q_3} \begin{pmatrix} * \\ 0 \end{pmatrix} $.
(Technically $Q_2$ and $Q_3$ act on the whole matrix and not just on the submatrices, so that $Q_i: \mathbb{R}^n \rightarrow \mathbb{R}^n$ for all $i$.
$Q_2$ leaves the first row and the first column alone, and $Q_3$ leaves the first two rows and the first two columns alone.)
Then $Q_3 Q_2 Q_1 A =$

\begin{equation*}
Q_3 Q_2 Q_1
\begin{pmatrix}
* & * & * \\
* & * & * \\
* & * & * \\
* & * & *
\end{pmatrix}
= Q_3 Q_2
\begin{pmatrix}
* & * & * \\
0 & * & * \\
0 & * & * \\
0 & * & *
\end{pmatrix}
= Q_3
\begin{pmatrix}
* & * & * \\
0 & * & * \\
0 & 0 & * \\
0 & 0 & *
\end{pmatrix}
=
\begin{pmatrix}
* & * & * \\
0 & * & * \\
0 & 0 & * \\
0 & 0 & 0
\end{pmatrix}
\end{equation*}

We've accomplished our goal, which was to triangularize $A$ using orthonormal transformations.
But how do we find the $Q_i$ that do what we want? The answer lies in using Householder reflections.

To find $Q_1$, we first identify an appropriate hyperplane to reflect $x$ into the span of $e_1$.
It turns out there are two hyperplanes that will work, as shown in figure \ref{fig:two reflectors}.
(In the complex case, there are infinitely many such hyperplanes.)
Between the two, the one that reflects $x$ further will be more numerically stable.
This is the hyperplane perpendicular to $v = sign(x_1)\norm{x}_2 e_1 + x$.

To see how this works, let $x$ be the first column of the submatrix that we want to project onto the span of $e_1$.
In order for this to be a unitary operation, this will need to preserve the norm of $x$.
This means that $\left( I - 2 v v^\mathsf{H} \right) x = \pm \norm{x} e_1$, or, in other words,

\[ 2 v v^\mathsf{H} x =
\begin{pmatrix}
x_1 \pm \norm{x} \\
x_2 \\
x_3 \\
\vdots \\
x_n
\end{pmatrix}\]

Let $u$ be the vector on the right hand side of this expression.
It can be shown that the vector  $\frac{u}{\norm{u}}$ is the proper choice for $v$.
%We will show that the vector $\frac{u}{\norm{u}}$ is the proper choice for $v$.
%Notice that:
%
%\[\norm{u}^2 = \norm{x}^2 \pm 2 \norm{x} x_1 + x_1^2 + x_2 + \dots + x_n^2 = 2 \norm{x}^2 \pm 2 \norm{x} x_1 \]
%
%and that
%
%\[\norm{x}^2 \pm \norm{x} x_1 = u^\mathsf{H} x \]
%
%So we have
%
%\begin{align*}
%2 v v^\mathsf{H} x &= 2 u \frac{\norm{x}^2 \pm x_1 \norm{x}}{\norm{u}^2} \\
%		&= 2 u \frac{u^\mathsf{H} x}{\norm{u}^2} \\
%		&= 2 \frac{u}{\norm{u}} \left( \frac{u}{\norm{u}} \right)^\mathsf{H} x
%\end{align*}
%
%So $\frac{u}{\norm{u}}$ is a proper choice of $v$ that will project $x$ into the span of $e_1$.

This whole process is summarized in Algorithm \ref{Alg:Householder}.

\begin{figure}
\includegraphics[width= \textwidth]{fig2}
\caption{two reflectors}
\label{fig:two reflectors}
\end{figure}

\begin{algorithm}
\caption{Householder triangularization}
\label{Alg:Householder}
\begin{algorithmic}[1]
\Procedure{Householder}{$A$}
\State $m, n \gets \text{shape} \left( A \right)$
\State $R \gets \text{copy} \left( A \right)$
\State $Q \gets I_m$
\For{$0 \leq k < n-1$}
    \State $v_k \gets \text{copy} \left( R_{k:,k} \right)$
    \State $v_{k_0} \gets v_{k_0} + \text{sign} \left( v_{k_0} \right) \norm{v_k}$
    \State $v_k \gets v_k / \norm{v_k}$
    \State $R_{k:,k:} \gets R_{k:,k:} - 2 v_k \left( v_k^\mathsf{H} R_{k:,k:} \right)$
    \State $Q_{k:} \gets Q_{k:} - 2 v_k \left( v_k^\mathsf{H} Q_{k:} \right)$
\EndFor
\State \pseudoli{return} $Q^\mathsf{H}, R$
\EndProcedure
\end{algorithmic}
\end{algorithm}

To see how we are operating on the matrices $A$ and $Q$, consider the way each orthonormal transformation defined by the $v_k$ operates blockwise on each matrix.
The matrix form of each operation on $A$ and $Q$ can be represented in block form like this:

\[
\begin{pmatrix}
I & 0 \\
0 & I - 2 v_k v_k^\mathsf{H}
\end{pmatrix}
\]

Notice that a block matrix of this form operates only on entries that lie in the rows from $k$ onward.
Consider what happens when we left-multiply a $m \times n$ matrix by a block matrix of this form.
We obtain the following:

\[
\begin{pmatrix}
I & 0 \\
0 & I - 2 v_k v_k^\mathsf{H}
\end{pmatrix}
\cdot
\begin{pmatrix}
A[:k,:k] & A[:k,k:] \\
A[k:,:k] & A[k:,k:]
\end{pmatrix}
\]
\[
=
\begin{pmatrix}
A[:k,:k] & A[:k,k:] \\
A[k:,:k] - 2 v_k v_k^\mathsf{H} A[k:,:k] & A[k:,k:] - 2 v_k v_k^\mathsf{H} A[k:,k:]
\end{pmatrix}
\]

And, when we consider right multiplication by the same block matrix, we see that it fixes the first $k-1$ columns as below.

\[
\begin{pmatrix}
A[:k,:k] & A[:k,k:] \\
A[k:,:k] & A[k:,k:]
\end{pmatrix}
\cdot
\begin{pmatrix}
I & 0 \\
0 & I - 2 v_k v_k^\mathsf{H}
\end{pmatrix}
=
\begin{pmatrix}
A[:k,:k] & A[:k,k:] - 2  A[:k,k:] v_k v_k^\mathsf{H} \\
A[k:,:k] & A[k:,k:] - 2 A[k:,k:] v_k v_k^\mathsf{H}
\end{pmatrix}
\]

When we are iterating through the columns of $R$ and zeroing out the entries below the main diagonal we are able to safely ignore all the entries that lie in columns we have already processed because they are already zero.

This algorithm returns orthonormal $Q$ and upper triangular $R$ satisfying $A = QR$.nNotice that we did not explicitly construct each orthonormal reflector matrix. We applied the changes we needed to each portion of the array that needed to be changed. Doing the operations in this way allows us to avoid unnecessarily increasing the computational complexity of the algorithm.
A few other clever optimizations can still be applied, but they will not change the overall complexity of the algorithm.

%It should now be clear how it was that we computed $R$ using this algorithm.
%$Q$ is computed in much the same way.
%Since each of the orthonormal operations is self-inverse (i.e. idempotent), $Q$ can be computed by applying the these operations to the identity in reverse order.
%In other words, you could make an identity matrix and then for $k$ such that $n-2 \geq k > -1$ , do $I[k:,k:] -= 2 v_k v_k^\mathsf{H}$
%In our computation, it may be more convenient to simply apply the operations to an identity matrix as we go, just like we are doing to $R$, then take the transpose at the end to invert $Q$.
%This way we do not have to store the $v_k$ as we go.
%There is one key difference, when applying these operations to $Q$ we cannot ignore columns we have already processed because they are not necessarily zero.
%It is interesting to note that we can use the $v_k$ to behave like $Q$ or $Q^{-1}$ depending on the order in which we apply them.
%Such an approach does not require the computation of $Q$ or $Q^{-1}$ at all.

Another important thing to notice is that an outer product is needed to compute $v_k \left( v_k^\mathsf{H} A[k:,k:] \right)$, not an inner product.
Make sure this is accounted for when you write the code to run this algorithm. You can either make the vectors $v_k$ column vectors (two dimensional with a single column) instead of just one-dimensional arrays, or you can use the built in function \li{np.outer} in the appropriate location.

\begin{problem}
\label{prob:HouseholderQR}
Write a function \li{householder} that accepts an array $A$ as input, and performs
the algorithm described above to compute the QR decomposition of $A$. Return the
matrices $Q$ and $R$.

It is simple to check that your code works: multiply the two output matrices
of your function, and check that the result matches the original input matrix.
\end{problem}

\subsection*{Stability of the Householder QR algorithm}
We will now examine the stability of the Householder QR algorithm.
We will use SciPy's built in QR factorization which uses Householder reflections internally.

Try the following.

\begin{lstlisting}
>>> Q, X = la.qr(np.random.rand(500,500)) # create a random orthonormal matrix:
>>> R = np.triu(np.random.rand(500,500)) # create a random upper triangular matrix
>>> A = np.dot(Q,R) # Q and R are the exact QR decomposition of A
>>> Q1, R1 = la.qr(A) # compute QR decomposition of A
\end{lstlisting}

Observe:

\begin{lstlisting}
>>> la.norm(Q1-Q)/la.norm(Q) # check error in Q
0.282842955725
>>> la.norm(R1-R)/la.norm(R) # check error in R
0.0428922016647
\end{lstlisting}

This is terrible!
This algorithm works in $16$ decimal points of precision, but $Q_1$ and $R_1$ are only accurate to $0$ and $1$ decimal points, respectively.
We've lost $16$ decimal points of precision!

Don't lose hope.
Check how close the product $Q_1 R_1$ is to $A$.
\begin{lstlisting}
>>> A1 = Q1.dot(R1)
>>> np.absolute(A1 - A).max()
3.9968028886505635e-15
\end{lstlisting}
We've now recovered $15$ digits of accuracy.
Considering the error relative to the norm of $A$ (using the 2-norm for matrices), we see that this relative error is even smaller.
\begin{lstlisting}
>>> la.norm(A1 - A, ord=2) / la.norm(A, ord=2)
8.8655568331889288e-16
\end{lstlisting}
The errors in $Q_1$ and $R_1$ were somehow ``correlated," so that they canceled out in the product.
The errors in $Q_1$ and $R_1$ are called \emph{forward errors}.
The error in $A_1$ is the \emph{backward error}.

In fact, the large errors in \li{Q1} and \li{R1} were not because the algorithm was bad, it was because $A$ was poorly conditioned.
The condition number for randomly generated upper triangular matrices is generally very high, and this was the case here.
This has, in turn, made the condition number of $A$ extremely large.

Try the following to compute the condition number of $A$.
In this case the condition number of $A$ and $R$ are computed to be different, though, in theory, they should be exactly the same.
\begin{lstlisting}
>>> from numpy.linalg import cond
>>> cond(A)
4.1426075832870472e+18
>>> cond(R)
3.1767577244363792e+19
\end{lstlisting}

Householder QR factorization is more numerically stable than Gram-Schmidt or even Modified Gram-Schmidt (MGS).
However, MGS is still useful for some types of iterative methods because it finds the orthonormal basis one vector at a time instead of all at once (for an example see Lab \ref{lab:EigSolve}).

\subsection*{Upper Hessenberg Form}
An upper Hessenberg matrix is a square matrix with zeros below the first subdiagonal.
Every  $n \times n$ matrix $A$ can be written $A = Q^THQ$ where $Q$ is orthonormal and $H$ is an upper Hessenberg matrix, called the Hessenberg form of $A$.

The Hessenberg decomposition can be computed using Householder reflections in a process very similar to Householder triangularization.
Let's demonstrate this process on a $5 \times 5$ matrix $A$.
Note that $A=Q^THQ$ is equivalent to $QAQ^T = H$. Our strategy is to multiply $A$ on the right and left by a series of orthonormal matrices until it is in Hessenberg form.
If we use the same $Q_1$ as in the first step of the Householder algorithm, then with $Q_1 A$ we introduce zeros in the first column of $A$.
However, since we now have to multiply $Q_1 A$ on the left by $Q_1^T$, all those zeros are destroyed, as demonstrated below.
In order to zero out the entire first column we must choose $Q_1$ appropriately so it does not fix the first row. When we apply the same operation on the right, this ruins the column that we just zeroed out.
(Although this process may seem futile now, it actually does tend to decrease the size of the subdiagonal entries.
If we repeat over and over again, the subdiagonal entries will often converge to zero. That's the idea behind the $QR$ algorithm in Lab \ref{lab:EigSolve}.)
\[
\begin{array}{ccccc}
\begin{pmatrix}
* & * & * & * & * \\
* & * & * & * & * \\
* & * & * & * & * \\
* & * & * & * & * \\
* & * & * & * & *
\end{pmatrix}
&\underrightarrow{Q_1 \cdot }&
\begin{pmatrix}
* & * & * & * & * \\
0 & * & * & * & * \\
0 & * & * & * & * \\
0 & * & * & * & * \\
0 & * & * & * & *
\end{pmatrix}
&\underrightarrow{\cdot Q_1^T }&
\begin{pmatrix}
* & * & * & * & * \\
* & * & * & * & * \\
* & * & * & * & * \\
* & * & * & * & * \\
* & * & * & * & *
\end{pmatrix}
\\
A & & Q_1A & & Q_1 A Q_1^T
  \end{array}
\]
Instead, let's try starting with a different $Q_1$ that leaves the \emph{first} row alone and reflects the \emph{rest} of the rows into the span of $e_2$. This means that $Q_1^T$ leaves the first column alone.
\[
\begin{array}{ccccc}
\begin{pmatrix}
* & * & * & * & * \\
* & * & * & * & * \\
* & * & * & * & * \\
* & * & * & * & * \\
* & * & * & * & *
\end{pmatrix}
&\underrightarrow{Q_1 \cdot }&
\begin{pmatrix}
* & * & * & * & * \\
* & * & * & * & * \\
0 & * & * & * & * \\
0 & * & * & * & * \\
0 & * & * & * & *
\end{pmatrix}
&\underrightarrow{\cdot Q_1^T }&
\begin{pmatrix}
* & * & * & * & * \\
* & * & * & * & * \\
0 & * & * & * & * \\
0 & * & * & * & * \\
0 & * & * & * & *
\end{pmatrix}
\\
A & & Q_1A & & Q_1 A Q_1^T
  \end{array}
\]
We now iterate through the matrix until we obtain
\begin{equation*}
Q_3 Q_2 Q_1 A Q_1^T Q_2 ^T Q_3^T =
\begin{pmatrix}
* & * & * & * & * \\
* & * & * & * & * \\
0 & * & * & * & * \\
0 & 0 & * & * & * \\
0 & 0 & 0 & * & *
\end{pmatrix}
\end{equation*}

This is even more convenient when we are working with Hermitian matrices.
In that case, the matrices applied on the left zero out everything below the first subdiagonal and the matrices applied on the right zero out everything above the first superdiagonal, leaving us with a tridiagonal matrix.
There are remarkably efficient ways to solve systems involving tridiagonal matrices, so this is especially convenient.

The pseudocode for computing the Hessenberg form of a matrix is shown in Algorithm \ref{Alg:Hessenberg}.
The exact inner workings of this algorithm are similar to the inner workings of Algorithm \ref{Alg:Householder}.

\begin{algorithm}
\caption{Reduction to Hessenberg Form}
\label{Alg:Hessenberg}
\begin{algorithmic}[1]
\Procedure{Hessenberg}{$G,u,l,p$}
\State $m, n \gets \text{shape}(A)$
\State $H \gets \text{copy}(A)$
\State $Q \gets I_m$
\For{$0 \leq k < n-2$}
    \State $v_k \gets H_{k+1:, k}$
    \State $v_{k_0} \gets v_{k_0} + \text{sign}(v_{k_0}) \norm{v_k}$
    \State $v_k \gets v_k/norm{v_k}$
    \State $H_{k+1:,k:} \gets H_{k+1:,k:} - 2v_k(v_k^\mathsf{H} H_{k+1:,k:})$
    \State $H_{:,k+1:} \gets H_{:,k+1:} - 2(H_{:,k+1:} v_k) v_k^\mathsf{H}$
    \State $Q_{k+1:} \gets Q_{k+1:} - 2v_k(v_k^\mathsf{H} Q_{k+1:})$
\EndFor
\State \pseudoli{return} $Q, R$
\EndProcedure
\end{algorithmic}
\end{algorithm}

\begin{problem}
\label{prob:hessenberg}
Write a function \li{hessenberg} that computes the Hessenberg form of a real-valued
input matrix $A$. The function should return $Q$ and $H$ satisfying $A = Q^THQ$,
where $Q$ is orthonormal and $H$ has zeros below the first subdiagonal.

The code for this algorithm will be fairly similar to the code for the QR factorization using Householder reflections.
This factorization technique will be used later on in Lab \ref{lab:EigSolve}.
Notice what happens when you compute the Hessenberg factorization of a Hermitian matrix.
\end{problem}

%Sources: http://www.cs.unc.edu/~krishnas/eigen/node5.html
% http://en.wikipedia.org/wiki/Givens_rotation
%http://en.wikipedia.org/wiki/QR_decomposition
%	Note the Operation count: Householder is 2/3 n^3, MGS is 2 n^3
%http://en.wikipedia.org/wiki/QR_algorithm
%Applied Numerical methods using MATLAB by Yang has some code written for this
%http://www.math.kent.edu/~reichel/courses/intr.num.comp.2/lecture21/evmeth.pdf
%	These are eigenvalue algorithms explained carefully
%http://en.wikipedia.org/wiki/Householder_transformation
%Numerical Linear Algebra, by Lloyd N. Trefethen and David Bau III, Chapters 10 and 16


%% ps1_q2.tex
\section{Two small categories}

Objects: 1, 2

Morphisms:

\begin{align*}
1 \rightarrow 1 &: \{id_1\}  \\
2 \rightarrow 2 &: \{id_2\}  \\
1 \rightarrow 2 &: \{f\}     \\
2 \rightarrow 1 &: \emptyset
\end{align*}

Composition:
\begin{align*}
  id_1 \circ id_1 &= id_1 \\
  id_2 \circ id_2 &= id_2 \\
     f \circ id_1 &= f \\
     id_2 \circ f &= f
\end{align*}

Right unit: $f \circ id_1 = f$
Left unit: $id_2 \circ f = f$

Associativity: as we only compose with identity, the associativity is trivial,

$$
   id_2 \circ f \circ  id_1 = (id_2 \circ f) \circ  id_1 = id_2 \circ  (f \circ id_1) = f
$$


\documentclass[main.tex]{subfiles}
\begin{document}

\section{Sheet 2}

\subsection{Constant acceleration}

\subsubsection{Coordinate velocity}

We are given the position as a function of time,
\begin{equation} \label{eq:constant-acceleration}
  x(t) = \frac{\sqrt{1 + \kappa^2 t^2} -1 }{\kappa }\,,
\end{equation}
%
and we can directly compute its derivative
%
\begin{equation} \label{eq:constant-acceleration-velocity}
  v(t) = \dv{x}{t} =
  \frac{\kappa t}{\sqrt{\kappa^2 t^2  + 1} }=\frac{1}{\sqrt{\frac{1}{\kappa^2 t^2}+1 }}\,.
\end{equation}

It is clear from the expression that \(\abs{v} < 1\) for all times, while \(v\) approaches 1 at positive temporal infinity and \(-1\) at negative temporal infinity.

\begin{figure}[h]
    \centering
    \includegraphics[width=\textwidth]{figures/velocity.pdf}
    \caption{Velocity as a function of coordinate time \(t\)}
    \label{fig:velocity-constant-acceleration}
\end{figure}

\subsubsection{Components of the 4-velocity}

The Lorentz factor \(\gamma \) is given by
%
\begin{equation} \label{eq:constant-acceleration-gamma}
  \gamma = \frac{1}{\sqrt{1-v^2}}
  = \frac{1}{\sqrt{1 - \frac{\kappa^{2} t^{2}}{\kappa^{2} t^{2} + 1} }}
  = \sqrt{\kappa^2 t^2 + 1} \,,
\end{equation}
%
therefore the four-velocity is given by:
%
\begin{equation}
  u^{\mu } =
  \begin{bmatrix}
  \gamma  \\
  \gamma v \\
  0 \\
  0
  \end{bmatrix}
  =
  \begin{bmatrix}
    \sqrt{\kappa^2 t^2 + 1}  \\
    \kappa t \\
    0 \\
    0
  \end{bmatrix}\,.
\end{equation}

\subsubsection{Proper time}

The relation between coordinate and proper time is given by the definition of the first component of the four-velocity: \(u^{0} = \dv*{t}{\tau} = \gamma \), therefore \(\dd{\tau } = \dd{t} / \gamma \).
Integrating this relation we get:
%
\begin{equation}
    \tau = \int_0^{\tau} \dd{\tau'}
    = \int_0^{t} \frac{\dd[]{t'} }{\gamma(t')}
    = \displaystyle \frac{\operatorname{arcsinh}{\left(\kappa t \right)}}{\kappa}\,,
\end{equation}
%
where the constant of integration is selected by imposing \(t = 0 \iff \tau = 0\).
Notice that, as we would expect, when expanding up to first order near \(t = \tau = 0 \) we have \(t \sim \tau \), since in that region the velocity is much less than unity.

The inverse relation is given by \(t = \sinh (\kappa \tau ) / \kappa \). Using this, we can write:
%
\begin{equation}
  x (t(\tau ))  =\frac{\cosh{\left(\kappa \tau \right)} - 1}{\kappa}\,.
\end{equation}

\subsubsection{Four-acceleration}

Now, we wish to compute the four-acceleration. There are many ways to approach this: an easy one is to simply find the explicit expression \(u^{\mu } (\tau )\) and to differentiate it. The expression we get is:
%
\begin{equation}
    a^{\mu } = \dv{}{\tau } u^{\mu } = \dv{}{\tau }
  \begin{bmatrix}
    \sqrt{\sinh^{2}{\left(\kappa \tau \right)} + 1}
        \\
    \frac{\sqrt{\kappa^{2} t^{2} + 1} \sinh{\left(\kappa \tau \right)}}{\sqrt{\sinh^{2}{\left(\kappa \tau \right)} + 1}} \\
    0 \\
    0
  \end{bmatrix}
  =
  \begin{bmatrix}
    \frac{\sqrt{2} \kappa \sinh{\left(2 \kappa \tau \right)}}{2 \sqrt{\cosh{\left(2 \kappa \tau \right)} + 1}}
    \\
    \kappa \cosh \left(\kappa \tau \right)\\
    0 \\
    0
  \end{bmatrix}
\,,
\end{equation}
%
which is a bit unwieldy but it can be used to check two important facts: \(a^{\mu } a_{\mu } = \const \) and \(a^{\mu }u_{\mu } = 0\). The first of the two is:
%
\begin{equation}
    a^{\mu }a_{\mu } = -(a_0 )^2 + (a_1 )^2 =
    \kappa^{2} \cosh^{2}{\left(\kappa \tau \right)} - \frac{\kappa^{2} \sinh^{2}{\left(2 \kappa \tau \right)}}{2 \left(\cosh{\left(2 \kappa \tau \right)} + 1\right)}
    = \kappa^2
\,,
\end{equation}
%
which tells us that the constant acceleration \(\sqrt{a^{\mu }a_{\mu }} = \kappa \).

Also, we verify the orthogonality to the four-velocity:
%
\begin{equation}
  a^{\mu }u_{\mu } =
  - \frac{\sqrt{2} \kappa \sqrt{\sinh^{2}{\left(\kappa \tau \right)} + 1} \sinh{\left(2 \kappa \tau \right)}}{2 \sqrt{\cosh{\left(2 \kappa \tau \right)} + 1}} + \kappa \sinh{\left(\kappa \tau \right)} \cosh{\left(\kappa \tau \right)}
  = 0
\,.
\end{equation}

\subsubsection{Local velocity \& acceleration}

We can apply a Lorentz boost corresponding to this velocity:
it will be given by the matrix:
%
\begin{equation}
  \left[\begin{array}{cccc}
  \gamma  & -v \gamma  & 0 & 0 \\
  -v \gamma  & \gamma  & 0 & 0 \\
  0 & 0 & 1 & 0 \\
  0 & 0 & 0 & 1
  \end{array}\right]
\,,
\end{equation}
%
where \(v\) and \(\gamma \) are those found before.
Without doing any calculations we could already say that the transformed velocity will be equal to the time-like unit vector, while the acceleration will be equal to \(\kappa \) times the unit \(x\)-directed vector.

The velocity becomes:
%
\begin{equation}
  \qty(u^{\mu})' =
  \left[\begin{array}{cccc}
    \sqrt{\kappa^2 t^2 + 1}  & -\kappa t  & 0 & 0 \\
    -\kappa t  & \sqrt{\kappa^2 t^2 + 1}  & 0 & 0 \\
    0 & 0 & 1 & 0 \\
    0 & 0 & 0 & 1
    \end{array}\right]
    \begin{bmatrix}
      \sqrt{\kappa^2 t^2 + 1}  \\
      \kappa t \\
      0 \\
      0
    \end{bmatrix}
  =
  \begin{bmatrix}
  1 \\
  0 \\
  0 \\
  0
  \end{bmatrix}
\,,
\end{equation}
%
as we expected.

The acceleration instead becomes:
%
\begin{equation}
  \qty(a^{\mu})' =
  \left[\begin{array}{cccc}
    \sqrt{\kappa^2 t^2 + 1}  & -\kappa t  & 0 & 0 \\
    -\kappa t  & \sqrt{\kappa^2 t^2 + 1}  & 0 & 0 \\
    0 & 0 & 1 & 0 \\
    0 & 0 & 0 & 1
    \end{array}\right]
    \begin{bmatrix}
      \frac{\sqrt{2} \kappa \sinh{\left(2 \kappa \tau \right)}}{2 \sqrt{\cosh{\left(2 \kappa \tau \right)} + 1}}
      \\
      \kappa \cosh \left(\kappa \tau \right)\\
      0 \\
      0
    \end{bmatrix}
  =
  \begin{bmatrix}
  0 \\
  \kappa  \\
  0 \\
  0
  \end{bmatrix}
\,,
\end{equation}
%
At small speeds the Lorentz boost matrix reduces to the identity matrix: this implies $kt\simeq k\tau\simeq 0$. In this case we obtain the same results of the rest frame of the particle for both acceleration and speed.
\subsection{Fixed target collision}

\subsubsection{Center of mass momenta}

In the CoM frame, the momenta of the two protons are respectively \((E_p, \pm p, 0,0)^\top = m_p (\gamma, \pm v, 0, 0)\), where \(E_p^2 = m_p^2 + p^2\).
The total CoM energy is \(-(p^\mu _A + p^\mu _B )^2 = 2 m_p^2\).

\subsubsection{Center of mass velocity}

The momentum of particle \(B\) will be given by \(p^{\mu } = m_p u^{\mu } = (m_p \gamma , m_p \gamma v, 0, 0)^\top\). Therefore, \(\gamma v = p / m_p\). Solving this we get:
%
\begin{equation}
  v = \frac{p}{m_p} \sqrt{\frac{1}{(p/m_p)^{2} + 1}} = \frac{p}{E_p}
\,,
\end{equation}
%

\subsubsection{Lab frame momenta}

The momentum of particle \(B\) in its own rest frame will just be \((m_p, 0, 0, 0)^\top\).
The momentum of particle \(A\) instead will be given by a boost in the \(x\) direction with velocity \(-v\):
%
\begin{equation}
  (p_A^\mu) _{\text{lab}} =
  \left[\begin{array}{cccc}
  \gamma  & v \gamma  & 0 & 0 \\
  v \gamma  & \gamma  & 0 & 0 \\
  0 & 0 & 1 & 0 \\
  0 & 0 & 0 & 1
  \end{array}\right]
  \left[\begin{array}{c}
  E_p  \\
  p \\
  0 \\
  0
  \end{array}\right]
  = \left[\begin{array}{c}
    \gamma E_p + v \gamma p  \\
    v \gamma E_p + \gamma p \\
    0 \\
    0
    \end{array}\right]
  = \left[\begin{array}{c}
    m_p \gamma^2 (1+v^2)  \\
    2 \gamma p\\
    0 \\
    0
    \end{array}\right]
\,,
\end{equation}
%

\subsection{Weak field gravitational time dilation}

\subsubsection{Time dilation expression}

It is more intuitive geometrically to deal with a pulse sent from \(A\) to \(B\), for which we expect the time dilation to work in the opposite sense:
%
\begin{equation}
  \Delta t_B = \Delta t_A \qty(1 + gh)
\,,
\end{equation}
%
up to first order in \(gh\) and \(g\Delta t_A\), since \((1+gh )(1-gh) = 1-(gh)^2 = 1\) to first order in \(gh\).
Alternatively, one can just map \(g \rightarrow -g\) to recover the time contraction for pulses sent in the other direction.

We know that the paths of the observers are two curves of constant acceleration: we know their explicit expression from equation \eqref{eq:constant-acceleration}, and additionally we assume that they are separated by a space interval \(h\):
%
\begin{subequations}
\begin{align}
  x_A(t) &= \frac{\sqrt{1 + (gt)^2} -1}{g} \\
  x_B(t) &= \frac{\sqrt{1 + (gt)^2} -1}{g} + h
  \,.
\end{align}
\end{subequations}

At \(t=0\) Alice sends a pulse, which then reaches Bob at a time \(t_1\). After a time \(\Delta t_A\), she sends another, which then reaches Bob at a time \(t_2 \).
Right now, we are referring to all times as measured in the rest frame of Alice at \(t=0\).
These times can be found by imposing that the space and time separation between the events of the pulse being sent and received are equal, since it travels at light speed: the equations which represent this are \(x_B(t_1) = t_1\) and \(x_B(t_2 ) -x_A (\Delta t_A) = t_2 - \Delta t_A\). Substituting the expressions for the positions:
%
\begin{subequations}
\begin{align}
  t_1 &= \frac{\sqrt{1+(g t_1 )^2} -1}{g} +h \\
  t_2 - \Delta t_A &= \frac{\sqrt{1+(gt_2 )^2} -1}{g} + h - \qty(\frac{\sqrt{1+(g \Delta t_A )^2} -1}{g})\,.
\end{align}
\end{subequations}

Now, it is just a matter of calculation to solve these equations, expand up to first order in the adimensional parameters \(gh\) and \(g \Delta t_A\) and one recovers the desidered expression for  \(\Delta t_B = t_2 - t_1\).

\begin{figure}[h]
  \centering
  \includegraphics[width=\textwidth]{figures/Visualization_of_accelerational_time_dilation.eps}
  \caption{Visualization of the beams, in the frame where the rocket is stationary as the first beam is being sent. The two curves intersecting the space axis are the tip and tail of the spaceship; the beams being sent from the tail are events \(C\) and \(D\), while their reception at the tip are events \(A\) and \(B\). Event \(E\) is just calculated as \(B-A\), to make comparisons with \(D\) easier. \(H\) and \(G\) are computed by tracing the dotted line of points which have the same spacetime interval from the origin as points \(D\) and \(E\) respectively, and selecting its intersection with the temporal axis: this effectively means finding the proper time separation between the two beams being sent/received.}
  \label{fig:beam-visualization}
\end{figure}

There is one more consideration to make though: what about the Lorentz time dilation for Bob? This it actually a \emph{second order effect}.

\begin{claim}
The time interval measured by Bob in his frame at \(t \sim t_1\) is the same as the one measured in the rest frame of Alice at \(t=0\) up to first order in \(gh\) and \(g \Delta t_B\).
\end{claim}

\begin{proof}
We perform a Lorentz boost to the velocity of Bob at \(t=t_1 \): this is given by equation \eqref{eq:constant-acceleration-velocity}, and is equal to:
%
\begin{equation}
  v = \frac{gt }{\sqrt{(gt)^2 + 1}}
\,,
\end{equation}
%
with a Lorentz factor of \(\gamma = \sqrt{(gt)^2 +1} \) (see equation \eqref{eq:constant-acceleration-gamma}).

The temporal separation between the two events is \(\Delta t_B\), while the spatial separation is \(\Delta x_B \approx  v \Delta t_B\) to first order. The boost, in the \((t, x)\) plane, looks like:
%
\begin{equation}
  \left[\begin{array}{c}
  \Delta t_B \\
  \Delta x_B
  \end{array}\right]'
  =
  \left[\begin{array}{cc}
  \gamma  & -v \gamma  \\
  -v \gamma  & \gamma
  \end{array}\right]
  \left[\begin{array}{c}
  \Delta t_B \\
  \Delta x_B
  \end{array}\right]
  =
  \left[\begin{array}{c}
  \Delta t_B \qty(\sqrt{(gt)^2+1} - (gt)^2/\sqrt{(gt)^2+1}) \\
  -gt \Delta t_B + \sqrt{(gt)^2+1} gt \Delta t / \sqrt{(gt)^2+1}
  \end{array}\right]
\,,
\end{equation}
%
therefore as we would expect the spatial separation is eliminated, while expanding the factor multiplying the temporal one near \(gt = 0\) we get:
%
\begin{equation}
  \sqrt{(gt)^2+1} - (gt)^2/\sqrt{(gt)^2+1} = 1+O((gt)^2)
\,,
\end{equation}
%
which proves our result.
\end{proof}

\subsubsection{Gravitational time dilation}

By the equivalence principle, the effects measured in a uniformly accelerating frame at \(g\) are the same as those measured in a gravitational field with constant acceleration \(g\).
The gravitational field in such a frame is given by \(\Phi  = gh\), where \(h\) is the height (with arbitrary zero point): the result follows.

\subsubsection{Twins and gravitation}

The gravitational time dilation difference, in absolute value, is given by:
%
\begin{equation}
  \Delta t = t _{\text{elapsed}} \frac{g \Delta h}{c^2}
  \approx \SI{1}{yr} \frac{\SI{10}{m/s^2} \times \SI{100}{m}}{(\SI{3e8}{m/s})^2} \approx \SI{3.5e-7}{s}
\,.
\end{equation}

We are asked what is the age of the twin on the ground as measured by the twin who is higher up: this is analogous to the situation considered in the first section of this problem; the twin higher up will measure the twin lower down to be older, specifically if \(\text{age}_{\text{up}} = \SI{1}{yr}\), then the observer up in the palace will measure the age of the twin at ground level as:
%
\begin{equation}
  \text{age}_{\text{down}} = \SI{1}{yr} + \SI{3.5e-7}{s} \approx (1+\num{1e-14}) \text{age}_{\text{up}}
\,.
\end{equation}
%

\end{document}


Calculation:
\begin{equation*}
	Out = \begin{cases}0 & Num=0, Den = 0\\ maxVal & Num > 0, Den=0\\ minVal & Num < 0, Den =0\\ \frac{Num}{Den} & \text{otherwise} \end{cases}
\end{equation*}
\paragraph{Note:}\textit{maxVal} and \textit{minVal} refer to the maximum/minimum representable value of the implementation.


\documentclass{article}

\usepackage{latexsym}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{amsthm}
\usepackage{hyperref}

\newcommand*{\thead}[1]{\multicolumn{1}{|c|}{\bfseries #1}}

\newtheorem{theorem}{Theorem}
\newtheorem{question}{Question}

\begin{document}

\title{Euclid's Algorithm and B\'ezout's Identity}
\author{Dave Neary}

\maketitle

\section{Introduction}

When working with large numbers, we will often want to calculate their greatest common divisor.
In particular, it is often important to know if two numbers are co-prime, that is, if they have a
greatest common divisor of 1. A fast algorithm for this, which dates from ancient times, is
Euclid's Algorithm.

For math competitions, it will sometimes be useful to be able to find a linear combination of two
numbers with integer solutions, especially when working in modular arithmetic. Based on Euclid's
algorithm, we will also show how to create such a linear combination using B\'ezout's identity.

\section{Euclid's Algorithm}

The Greatest Common Divisor of two integers $a,b$ is the largest positive integer $k$ which
evenly divides both $a$ and $b$. That is, we can write $a=km$, $b=kn$ for $m,n \in \mathbb{Z}$ and
$\gcd(m,n) = 1$. For smaller numbers, we will typically do this by finding the prime decomposition
of the two numbers, and identifying prime numbers which are common factors of both numbers.

However, this is impractical for larger numbers. Thankfully, Euclid's algorithm gives us a simple
method to systematically find the GCD of any two natural numbers.

\begin{theorem}
Given two positive integers $a,b \in \mathbb{Z}^+$, we can find unique non-negative integers
$q,r \in \mathbb{Z}^+$ (quotient and remainder) such that:

\[ a = qb + r \]

with $0\leq r < a$
\end{theorem}

That is, we can always divide $b$ into $a$ to get a quotient and a remainder. We can even weaken the
requirement that $a$ and $b$ are positive integers, if we allow negative values of $q$ and we can find a
positive $r < |b|$. For example, if we use this with $a=89, b=17$, we get $q=5, r=4$, and
$89 = 5 \times 17 + 4$. What makes this into an algorithm to calculate the GCD is the following result:

\begin{theorem}
Given positive integers $a,b$ with $a>b$, and non-negative integers $q,r$ such that $a=qb+r$:
If $r>0$ then $\gcd(a,b) = \gcd(b,r)$.
\end{theorem}

\begin{proof}
Let $k = \gcd(a,b)$. Then $a=km, b=kn, \gcd(n,m) = 1$.

We are given:
\begin{eqnarray*}
	 a &=& qb+r \\
	\implies r &=& a-qb \\
	\implies r &=& k(m-qn)
\end{eqnarray*}
So $r$ is a multiple of $k$.

Also, if $gcd(b,r)=j > k$ then $qb = jm, r=jn$ for some $m,n$, and then:
\[ a = qb+r = j(m+n) \]
so $a$ is also a multiple of $j$ and $k$ is not the GCD of $a,b$, which is a contradiction.

Therefore, $\gcd(a,b) = \gcd(b,r)$.
\end{proof}

This gives us a way to repeat this operation until $r$ eventually reaches $0$, at which case the $r$
in the prior step is the GCD.

Let's work through an example to see it in action.

\begin{question}
Find the GCD of 1128\ and 33.
\end{question}
\begin{proof}[Answer]
	\begin{eqnarray*}
		1128 &=& 34\times 33 + 6 \\
		33 &=& 5\times 6 + 3 \\
		6 &=& 2 \times 3 + 0
	\end{eqnarray*}
	So the GCD of 1128 and 33 is 3 (the last non-zero remainder).
\end{proof}

\begin{question} Find the GCD of 6540 and 1206. \end{question}
\vspace*{\bigskipamount}

\section{Bézout's identity}

Given $a,b \in \mathbb{Z}, a, b \neq 0$, we can find $n,m \in \mathbb{Z}$ such that:
\[ \gcd(a,b) = ma + nb \]
In fact when both $a$ and $b$ are not equal to 0, we can find infinitely many such pairs $(m,n)$.

In particular, if $\gcd(a,b)=1$, then for all integers $k$, we can find $n,m\in \mathbb{Z}$ such that:
\[ k = am + bn \]

And further, if $\gcd(a,b)=k$, there are no solutions to the equation $am+bn=j$ if $j$ is not a
multiple of $k$.

The way that we use Euclid's algorithm to generate this identity is that we start at the last step,
and rearrange everything to be in terms of $\gcd(a,b)$, and then at each stel we replace the remainder
term with $a-qb$.

Let's work through an example to see how it works:

\begin{question}
	Find an integer solution to:
	\[ 267x + 112y = 3 \]
\end{question}
\begin{proof}[Answer]
	Let's start by calculating the GCD of 267 and 112 using Euclid's algorithm, and also
	noting the form $r = a-qb$ at each step:
	\begin{align*}
		267 &= 2 \times 112 + 43 & 43 &= 267 - 2 \times 112 \\
		112 &= 2 \times 43 + 26 & 26 &= 112 - 2 \times 43\\
		43 &= 1 \times 26 + 17 & 17 &= 43 - 1 \times 26 \\
		26 &= 1 \times 17 + 9 & 9 &= 26 - 1 \times 17 \\
		17 &= 1 \times 9 + 8 & 8 &= 17 - 1 \times 9 \\
		9 &= 1 \times 8 + 1 & 1 &= 9 - 1 \times 8 \\
		8 &= 8 \times 1 + 0 &&
	\end{align*}

	So the GCD of 267 and 112 is 1.

	Now we run through the algorithm backwards, isolating the remainder term:
	\begin{eqnarray*}
		1 &=&  9 - 1 \times 8  \\
		1 &=&  9 - 1 \times (17 - 1 \times 9) = 2 \times 9 -1 \times 17  \\
		1 &=&  2 \times (26 - 1 \times 17) - 1 \times 17 = 2 \times 26 - 3 \times 17  \\
		1 &=&  2 \times 26 - 3 \times (43 - 1 \times 26) = 5 \times 26 - 3 \times 43  \\
		1 &=&  5 \times (112 - 2 \times 43) - 3 \times 43 = 5 \times 112 -13 \times 43 \\
		1 &=&  5 \times 112 - 13 \times (267 - 2 \times 112) = 31 \times 112 - 13 \times 267
	\end{eqnarray*}

	Now we have a general solution $1 = 31\times 112 - 13\times 267$, and we can generate other
	solutions to the same equation by adding and subtracting multiples of $112 \times 267$ as follows:
	\[ 1 = (31-267k) \times 112 + (112k - 13) \times 267 \]

	And we can get the general solution to the question asked by multiplying every term by 3:
	\[ 3 = (93-801k) \times 112 + (336k - 39) \times 267 \]

	which gives solutions for any $k\in \mathbb{Z}$.

\end{proof}

Questions of this type often arise with different notation or ways of framing the question. Here are
another could of examples, and some exercises:

\begin{question}
	Detective John McLain and Zeus, a New York small business owner in the wrong place at the wrong
	time, are directed by the mysterious terrorist Simon to a fountain in Central Park, where they
	find an armed bomb. They are given an enigma to solve to defuse the bomb.

	Given a 3 gallon jug and a 5 gallon jug, how can they measure out exactly 4 gallons to put
	on the scale and defuse the bomb?
\end{question}

\begin{proof}[Answer]
	We can now recognize this as a simple application of B\'ezout's Identity. We want to find integers
	$a, b$ such that $5a + 3b = 4$. It is straightforward to find values of $a$ and $b$ that work,
	but we will run through the algorithm to find the general solution.

	\begin{align*}
		5 &= 1 \times 3 + 2 & 2 &= 5 - 3  \\
		3 &= 1 \times 2 + 1 & 1 &= 3 - 2 \\
		& & 1 &= 3 - (5 - 3) = 2(3) - 5
	\end{align*}

	Our base solution is:
	\[ 3(2) + 5(-1) = 1\]
	and multiplying across by 4, ww get
	\[ 3(8) + 5(-4) = 4\]

	Adding and subtracting $15k$ to obtain the general solution gives us:
	\[ 3(8-5k) + 5(3k-4) = 4\]

	Now the smallest solution in terms of steps is at $k=2$, $2\times 5 - 2\times 3 = 4$. So we have
	to fill up the five gallon drum twice, and empty out the 3 gallon drum twice.

	\begin{figure}[ht!]
	\includegraphics{mathologer.png}
		\caption{\href{https://www.youtube.com/watch?v=0Oef3MHYEC0}{Mathologer
		visual demonstration of this problem in action}}
	\end{figure}

	\vspace{1em}

	\begin{tabular}{|l|c|c|r|}
		\hline
		\thead{Step} & \thead{5 Gallon Jug} & \thead{3 Gallon jug}\\
		\hline
		Fill 5G jug from fountain & 5 & 0 \\
		Fill 3G jug from 5G jug & 2 & 3 \\
		Empty 3G jug & 2 & 0 \\
		Empty 5G jug into 3G jug & 0 & 2 \\
		Fill 5G jug from fountain & 5 & 2 \\
		Top up 3G jug from 5G jug & 4 & 3 \\
		\hline
		\multicolumn{3}{|c|}{\bfseries Weigh 5G jug} \\
		\hline
	\end{tabular}

	\vspace{1em}

	Now - given the relationship to B\'ezout's identity, can you find
	another solution which involves filling the 3G jug from the fountain?

\end{proof}

\begin{question}
What is the inverse of 10 modulo 17?
\end{question}

\begin{proof}[Answer]
	The multiplicative inverse of $k \pmod{n}$ in modular arithmetic is an integer which, when you
	multiply it by $k$, gives a result of $1 \pmod{n}$

	In other words, we need to find a number $m$ such that:
	\[ 10m + 17n = 1 \]

	We will use Euclid's algorithm to get the GCD of 10 and 17 (which is obviously 1):
	\begin{align*}
		17 &= 10 + 7 & 7 &= 17 - 10 \\
		10 &= 7 + 3  & 3 &= 10 - 7 \\
		7 &= 2 \times 3 + 1 & 1 &= 7 - 2 \times 3 \\
		&& 1 &= 7 - 2 \times (10 - 7) \\
		&& 1 &= 3 \times 7 - 2 \times 10 \\
		&& 1 &= 3 \times (17 - 10) - 2 \times 10 \\
		&& 1 &= 3 \times 17 - 5 \times 10 \\
	\end{align*}

	We have an identity, with a general term:
	\[ 1  = (3 - 10k) \times 17 + (17k-5) \times 10 \]
	with the smallest positive multiple of 10 which works being $k=1$, $17-5 = 12$
	And it's easy to verify that $1 = 12\times10 - 7\times17$.
\end{proof}

\begin{question}Find a solution in integers to the Diophantine equation $61x + 23y = 1$\end{question}
\vspace*{\bigskipamount}

\begin{question}Find a natural number $k$ such that $(573k+4)/719$ is also a natural
number.\end{question}
\vspace*{\bigskipamount}

\begin{question}Find the smallest positive integer that ends in 2010, and is divisible by 2011.\end{question}
\vspace*{\bigskipamount}

\end{document}


\section{Residual error model}
\label{sec:residualErrorModel}
\label{maths:error_model}
\label{maths:combined-err-model}

In this section we consider different forms of the residual error, i.e. about $g$ in the term
\begin{eqnarray}
g(x_{ij}, \psi_{i}, \xi) \epsilon_{ij} \nonumber
\end{eqnarray}
of the eq.\ref{eq:nlmeModel} with $\epsilon_{ij} \sim N(0, 1)$, i.e. a standardised random variable.
The residual errors are part of the \textit{Observations Model}, see section \ref{sec:eg1-obs-model} for detailed discussion.
The following table contains some of the models which can be implemented in \pharmml:

\begin{table}[htdp]
\begin{center}
\begin{tabular}{l c c}
Model name & $g$ & $\xi$ \\
\hline \hline
Constant error model & $a$ & $a$ \\
Proportional error model & $bf$ & $b$ \\
Combined error model & $a + bf$ & $a,b$ \\
Alternative combined error model 1& $\sqrt{a^2 + b^2f^2}$ & $a,b$ \\
Alternative combined error model 2 & $a + bf^c$ & $a, b, c$
\end{tabular}
\end{center}
\caption{Examples of residual error models which can be implemented in \pharmml.}
\label{tab:residualModels}
\end{table}%


\subsection{Metric tensors}

A metric tensor assigns a bilinear form to each point on the manifold.

We can then take two vectors in the tangent space and return a scalar.