\documentclass[
  xhtml,%
  use filename%
]{internet}

\usepackage{tutorial}
\usepackage{hyperref}

\title{Do It Again \dots\ But Better}
\date{\today}
\begin{document}
\maketitle

\section{Introduction}

One thing that computers excel at is repetition.
Ask a computer to write out ``I must not let my dog eat my homework.'' a hundred times and it won't bat an eyelid\begin{marginpar}If it had eyelids, that is.\end{marginpar}.

There are a variety of ways of getting a computer to do something several times.
Which is right depends on exactly what you want to achieve.
We'll use an example where we draw lots of rectangles on the screen.

The one method that is almost never right is to just write out everything yourself.

\section{For Loops}

The most straightforward is the \verb+for+ loop.
Let's suppose we want to draw a rectangle \(20\) times.
The \verb+for+ loop starts like this:

\begin{verbatim}
for k=1,20 do
\end{verbatim}

\noindent Then we put whatever it is we want to repeat, in this case the command to draw a rectangle.
To indicate where we want the repetition to end, we simply write \verb+end+.
So our \verb+for+ loop will look like:

\begin{verbatim}
for k=1,20 do
  rect(20,20,100,100)
end
\end{verbatim}

\noindent and our whole code should now look like:

\begin{verbatim}
function setup()
  print("hello world")
end

function draw()
  background(40,40,50)
  fill(150,200,30)
  stroke(200,30,150)
  strokeWidth(10)
  for k=1,20 do
    rect(20,20,100,100)
  end
end
\end{verbatim}

Now run this code.
You shouldn't notice any difference from the original code.
That's because you are drawing the \emph{same} rectangle \(20\) times.
This is rarely what you want to do, more often you will want to do \emph{almost} the same thing several times with little changes between each time.

Again, there are a number of ways to achieve this.
In a \verb+for+ loop, we have a special variable which counts which time through the repetition we are on.
In our example, this variable is \verb+k+ (we get to choose the name: if we wrote \verb+for n=1,25 do+ then it would be \verb+n+).
We can use this inside the loop to change what happens.
For example, to space out the rectangles we could do:

\begin{verbatim}
for k=1,20 do
  rect(20*k,20,100,100)
end
\end{verbatim}

Full code is now:

\begin{verbatim}
function setup()
  print("hello world")
end

function draw()
  background(40,40,50)
  fill(150,200,30)
  stroke(200,30,150)
  strokeWidth(10)
  for k=1,20 do
    rect(20*k,20,100,100)
  end
end
\end{verbatim}

Now we should see something different.
The first rectangle is drawn with lower left corner at \((20,20)\), the second is at \((40,20)\), the third at \((60,20)\) and so on.
We can do more if we like:

\begin{verbatim}
for k=1,20 do
  rect(20*k,20+10*k,100-k,100-k)
end
\end{verbatim}

We can also be a bit more adventurous with how we create the loop.
We don't have to start at \(1\).
It might be more natural to start at, say, \(5\):

\begin{verbatim}
for k=5,20 do
  rect(20*k,20+10*k,100-k,100-k)
end
\end{verbatim}

(The numbers also don't \emph{have} to be whole numbers, but it works best if they are.)

We also don't have to go up in \(1\)s.
If we put a third number in the first line then that is the \emph{step value}.
For example,

\begin{verbatim}
for k=5,20,5 do
  rect(20*k,20+10*k,100-k,100-k)
end
\end{verbatim}

\noindent goes up in \(5\)s.
So it will do four repetitions with \(k=5\), \(10\), \(15\), and ending at \(20\).

Lastly, we could use a variable in place of any of the numbers.
So if we've already defined, say, \(n=20\) we could write:

\begin{verbatim}
for k=5,n do
  rect(20*k,20+10*k,100-k,100-k)
end
\end{verbatim}

In conclusion, \verb+for+ loops are best for when the change from one repetition to the next is regular and where we know in advance how many times we want to repeat the code.

\section{While Loops}

A \verb+while+ loop is a slightly more free-form version of a \verb+for+ loop.
Suppose we want to draw rectangles all the way across the screen.
The width of the screen is contained in a variable \verb+WIDTH+ so we could work out how many rectangles are needed to get across and then draw the right number using a \verb+for+ loop.
Much easier is to say ``Carry on drawing until we reach the edge of the screen.''.
For this, we use a \verb+while+ loop.

In a \verb+while+ loop, we need a \emph{test} that we can check each time round.
While it remains true, the loop keeps repeating.
At some point, the condition should fail and then the loop stops.

Let's use a similar example.
We're drawing rectangles across the screen starting at \(20\) and continuing until we reach the right-hand edge.
Before the loop starts, we need to define our starting point.
Then the loop starts.
Thus we start with:

\begin{verbatim}
x = 20
while x < WIDTH do
\end{verbatim}

\noindent As with everything in Lua, the loop ends with \verb+end+.

Inside the loop we put our drawing command.
We also change the value of \verb+x+ to match what we want to do.

\begin{verbatim}
x = 20
while x < WIDTH do
  rect(x,20+10,100,100)
  x = x + 20
end
\end{verbatim}

It is important that the loop does eventually end.
So we do have to ensure that \verb+x+ eventually will be bigger than \verb+WIDTH+.

Our full code is:

\begin{verbatim}
function setup()
  print("hello world")
end

function draw()
  background(40,40,50)
  fill(150,200,30)
  stroke(200,30,150)
  strokeWidth(10)
  x = 20
  while x < WIDTH do
    rect(x,20,100,100)
    x = x + 20
  end
end
\end{verbatim}

Again, we can be more adventurous and change more than just the \(x\) coordinate of the rectangles.

In conclusion, \verb+while+ loops are best for when the change from one repetition to the next is regular and where we don't necessarily know in advance how many times we want to repeat the code but know when we want to stop.

\section{Repeating From a List}

The last way for repeating stuff is the most flexible.
This method involves \emph{lists}.

\subsection{Lists}

Suppose we want to draw some rectangles at a variety of places on the screen but there is no particular structure to the places.
In fact, let's suppose that we want to draw a rectangle whenever the user clicks on the screen and we want to place the rectangle at that place.
To do this, we need to maintain a \emph{list} of the places of those rectangles.

Lists are a common feature of programming languages.
There are a few different words used to refer to them, the most common are \emph{arrays} and \emph{tables}.
In Lua, they are called \emph{tables}.

We create an empty list like this:

\begin{verbatim}
rectangles = {}
\end{verbatim}

Let's put this in the \verb+setup+ function, so our code should look like:

\begin{verbatim}
function setup()
  print("hello world")
  rectangles = {}
end

function draw()
  background(40,40,50)
  fill(150,200,30)
  stroke(200,30,150)
  strokeWidth(10)
  rect(20,20,100,100)
end
\end{verbatim}

Every time the user clicks on the screen, we need to add the coordinates of where they touch to this list.
The code to add to a list is the function \verb+table.insert()+.
We use it as follows:

\begin{verbatim}
table.insert(rectangles,coordinate(x,y))
\end{verbatim}

\noindent So the first thing is the table itself, the second is what we want to add to it.
LuaCanvas contains a special command \verb+coordinate+ for referring to coordinates.
The \verb+x+ and \verb+y+ are the \(x\) and \(y\) values of the coordinate.
We want to get these from the user's mouse click.
We get the information from mouse clicks by a special function called \verb+touched+.
Our code so far is:

\begin{verbatim}
function setup()
  print("hello world")
  rectangles = {}
end

function draw()
  background(40,40,50)
  fill(150,200,30)
  stroke(200,30,150)
  strokeWidth(10)
  rect(20,20,100,100)
end

function touched(t)
  if t.state == ENDED then
    table.insert(rectangles,coordinate(t.x,t.y))
  end
end
\end{verbatim}

We'll look at what's going on in the \verb+touched+ function in more detail in another tutorial.
In brief, the variable \verb+t+ contains information about each mouse click.
A mouse ``event'' comprises three stages: the mouse button being pressed down, then the mouse might move, before finally the mouse button is released.
We figure out which is happening via the \verb+t.state+ variable.
We're only going to add our coordinate when the mouse button is released.
The coordinates of the mouse when the button is released are contained in \verb+t.x+ and \verb+t.y+ so we use those to add to our list.

\subsection{Doing the Repetition}

Every time you click on the screen you add a coordinate to the list.
Our list is growing, but at the moment we're not doing a lot with it.
We want to draw a rectangle for each coordinate.

This involves a special kind of loop.
It's a bit like a \verb+for+ loop, and indeed it starts with the word \verb+for+.
Its starting code is:

\begin{verbatim}
for k,v in ipairs(rectangles) do
\end{verbatim}

\noindent and by now you should know how to end the loop!

What this does is the following.
It goes through the list of coordinates contained in the table \verb+rectangles+ and executes the code each time.
Inside the loop, the variable \verb+v+ is set to each coordinate in turn.
The variable \verb+k+ keeps track of the number of that coordinate in the list.

So if our list consisted of the coordinates \((20,30)\), \((10,50)\), and \((60,100)\) then our code in the loop would be run three times.
The first time then we would have \verb+k+ as \verb+1+ and \verb+v+ as \verb+coordinate(20,30)+.
The second time we would have \verb+k+ as \verb+2+ and \verb+v+ as \verb+coordinate(10,50)+.
So inside the loop we can use \verb+k+ and \verb+v+.
For example,

\begin{verbatim}
for k,v in ipairs(rectangles) do
  rect(v,100)
end
\end{verbatim}

Note that the \verb+rect+ is quite flexible.
You can give it the coordinates and size as in \verb+rect(20,20,100,100)+ but if the first bit is a \verb+coordinate+ then it understands that that coordinate should be used as the \(x\) and \(y\).
Also, if you miss out the height it will assume you want it to be a square.

Our full code is now:

\begin{verbatim}
function setup()
  print("hello world")
  parameter.watch("Number of rectangles","#rectangles")
  rectangles = {}
end

function draw()
  background(40,40,50)
  fill(150,200,30)
  stroke(200,30,150)
  strokeWidth(10)
  for k,v in ipairs(rectangles) do
    rect(v,100)
  end
end

function touched(t)
  if t.state == ENDED then
    table.insert(rectangles,coordinate(t.x,t.y))
  end
end
\end{verbatim}

\subsection{Going Further}

We can get more intricate.
Lists can contain anything, including other lists.
So as well as our coordinate we can add some other information.
Let's define a random colour function:

\begin{verbatim}
function randomColour()
  local r = math.random(255)
  local g = math.random(255)
  local b = math.random(255)
  return colour(r,g,b)
end
\end{verbatim}

\noindent and change our \verb+touched+ function to:

\begin{verbatim}
function touched(t)
  if t.state == ENDED then
    table.insert(rectangles,{coordinate(t.x,t.y),colour = randomColour(),size = 100})
  end
end
\end{verbatim}

Now we need to use this information when drawing the rectangles.
In our loop, \verb+v+ refers to the current item in the \verb+rectangles+ loop.
This is now itself a list.
We need to get out the coordinate, the colour, and the size from this list.

Let's look at how the list is constructed:

\begin{verbatim}
{coordinate(t.x,t.y),colour = randomColour(),size = 100}
\end{verbatim}

For the sake of this discussion, we'll give this table a name:

\begin{verbatim}
tbl = {coordinate(t.x,t.y),colour = randomColour(),size = 100}
\end{verbatim}

Lists (that is, \emph{tables}) in Lua are quite versatile.
They have an ordered part and an unordered part.
We get at the pieces in the ordered part by their position.
To get the pieces in the unordered part, we need \emph{keys}.

In this list, we have a bit of both.
The \verb+size = 100+ stores the value \verb+100+ in the \emph{unordered} part.
The \verb+size+ is the key.
To get this value back, we write \verb+tbl.size+.
So that starts with the table name (in this case \verb+tbl+), then a dot, then the key name (\verb+size+).
An alternative notation is \verb+tbl["size"]+ (this is the only way if the key has, for example, spaces in it).

To get the ordered part, we use square brackets.
As the value \verb+coordinate(t.x,t.y)+ is by itself (i.e.\ it doesn't have a key), it goes in the ordered part of the table.
It's the first item, so it has positive \(1\).
To get it back, we write \verb+tbl[1]+.

So to draw this rectangle, we can write:

\begin{verbatim}
tbl = {coordinate(t.x,t.y),colour = randomColour(),size = 100}
fill(tbl.colour)
rect(tbl[1],tbl.size)
\end{verbatim}

Putting this into our code, we have:

\begin{verbatim}
function setup()
  print("hello world")
  parameter.watch("Number of rectangles","#rectangles")
  rectangles = {}
end

function draw()
  background(40,40,50)
  fill(150,200,30)
  stroke(200,30,150)
  strokeWidth(10)
  for k,v in ipairs(rectangles) do
    fill(v.colour)
    rect(v[1],v.size)
  end
end

function touched(t)
  if t.state == ENDED then
    table.insert(rectangles,{coordinate(t.x,t.y),colour = randomColour(),size = 100})
  end
end

function randomColour()
  local r = math.random(255)
  local g = math.random(255)
  local b = math.random(255)
  return colour(r,g,b)
end
\end{verbatim}

\subsection{Removing From a List}

As a finale, let's adjust the size of the rectangles and remove them when their size reaches zero.
To reduce the size of each rectangle, as we draw it we also subtract one from its size:

\begin{verbatim}
v.size = v.size - 1
\end{verbatim}

Then we have to check if the size is \(0\) and remove it from the table.
Now, it's a bad idea to alter a list when we're in the middle of a loop, so what we do is make a new list containing those rectangles we want to remove from our existing list.
Then when we've finished drawing our rectangles we go through this new list and remove the corresponding rectangles from our original list.

\begin{verbatim}
del = {}
for k,v in ipairs(rectangles) do
  fill(v.colour)
  rect(v[1],v.size)
  v.size = v.size - 1
  if v.size == 0 then
    table.insert(del,1,k)
  end
end
\end{verbatim}

So we start by making a new empty table, \verb+del+.
Then during our list we reduce the size by \(1\) and then test to see if the size is now \(0\).
If it is, we add the \emph{position} in the list of this rectangle to our list to delete.
Before, the \verb+table.insert+ command looked like \verb+table.insert(tbl,item)+.
In this case, it looks like \verb+table.insert(tbl,position,item)+.
The first version adds the item to the end of the list.
The new version adds it in the specified position, shifting the rest of the list downwards.
In this case we're using position \(1\) so it adds the new item at the front of the list.

The reason for doing this is to do with how we delete things from our list.
The removal code is:

\begin{verbatim}
for k,v in ipairs(del) do
  table.remove(rectangles,v)
end
\end{verbatim}

This goes through the \verb+del+ list.
Remember that this contains the rectangles that we want to remove.
So each time through this list, \verb+v+ is the \emph{position} in the \verb+rectangles+ list of a rectangle that we want to get rid of.
The \verb+table.remove(rectangles,v)+ command gets rid of the \verb+v+th item in the \verb+rectangles+ list.
But there's an added extra: when it removes an item from the middle of the list then all the items after it get shifted up.

So suppose we have the list:

\begin{verbatim}
list = {"a","b","c","d","e"}
\end{verbatim}

\noindent and we want to remove the \verb+"b"+ and \verb+"d"+ items.
These are in positions \(2\) and \(4\).
If we do:

\begin{verbatim}
table.remove(list,2)
table.remove(list,4)
\end{verbatim}

We will actually end up with \verb+{"a","c","d"}+.
This is because after removing the \verb+"b"+ from position \(2\) then everything shifts up so our list is now:

\begin{verbatim}
list = {"a","c","d","e"}
\end{verbatim}

\noindent and \verb+"e"+ is now in position \(4\).
So the next line removes \verb+"e"+, not \verb+"d"+ as intended.

The solution is to work backwards through the list.
If we do:

\begin{verbatim}
table.remove(list,4)
table.remove(list,2)
\end{verbatim}

Then after the first removal we have:

\begin{verbatim}
list = {"a","b","c","e"}
\end{verbatim}

\noindent and although the \verb+"e"+ has moved up, the \verb+"b"+ is still in position \(2\) where it started.
So the second removal does remove \verb+"b"+ as intended.

This is why we add the positions to the front of the \verb+del+ list so that at the end they are in descending order.

(In actual fact, unless you are extremely fast clicking the mouse then there will only ever be one rectangle removed at a time.
But the general principle is important.)

Our final code is:

\begin{verbatim}
function setup()
  print("hello world")
  parameter.watch("Number of rectangles","#rectangles")
  rectangles = {}
end

function draw()
  background(40,40,50)
  fill(150,200,30)
  stroke(200,30,150)
  strokeWidth(10)
  del = {}
  for k,v in ipairs(rectangles) do
    fill(v.colour)
    rect(v[1],v.size)
    v.size = v.size - 1
    if v.size == 0 then
      table.insert(del,1,k)
    end
  end
  for k,v in ipairs(del) do
    table.remove(rectangles,v)
  end
end

function touched(t)
  if t.state == ENDED then
    table.insert(rectangles,{coordinate(t.x,t.y),colour = randomColour(),size = 100})
  end
end

function randomColour()
  local r = math.random(255)
  local g = math.random(255)
  local b = math.random(255)
  return colour(r,g,b)
end
\end{verbatim}

\subsection{Do It Yourself \dots\ But Better}

You could try adding more information to each rectangle when it is created.
Try making the size random as well.
You could make the outline a random colour and random width.
It could also decrease in size at a random rate.

Note: the \verb+math.random+ function has three modes:

\begin{itemize}
\item \verb+math.random()+ returns a random number between \(0\) and \(1\).
\item \verb+math.random(b)+ returns a random integer between \(1\) and \(b\).
\item \verb+math.random(a,b)+ returns a random integer between \(a\) and \(b\).
\end{itemize}

You could also replace the \verb+rect+ command by \verb+ellipse+ to make circles instead of squares.

Another possibility would be to add squares (or circles) on \emph{every} mouse event.
To do this, remove the line \verb+if t.state == ENDED then+ and the corresponding \verb+end+.
Then if you drag the mouse across the canvas you will get a trail of shapes that gradually shrink.

\end{document}


% !TEX encoding = UTF-8 Unicode
% !TEX spellcheck = en_US
% !TEX root = ../../../ICMA2020.tex
\subsection{Signal Processing}
\label{subsec:SignalProcessing}
All experiments described in Sec.\,\ref{subsec:ExperimentScenario} are carried out multiple times with the same set of parameters. Over $N_\text{r}=50$ repetitions of each experiment the mean of the joint angular positions $\overline{\boldsymbol{q}}(t_p)$ and the measured torques $\overline{\boldsymbol{\tau}}(t_p)$ are calculated for every time step $t_p$.

In the case of the \textsc{Fourier} series the frequency spectrum of the excitation is known, which allows for additional filtering of the position measurements:
The discrete \textsc{Fourier} transform of the mean position measurements $\overline{\boldsymbol{q}}$ is calculated and only the first $n_\mathrm{h}+1$ \textsc{Fourier} coefficients are retained. These correspond to the offset, the base frequency and the $n_\mathrm{h}-1$ harmonics. Utilizing this filter technique yields nearly noise-free estimates of the joint angular positions.
A possible loss of information is tolerated in favor of reduced noise.
The same filter technique is used in \cite{Olsen.2002} and \cite{Stueckelmaier.}.
Because of the averaging of the measurement data and the additional filtering for the \textsc{Fourier} series the derivatives for the joint angular velocities and accelerations can be calculated numerically for all experiments.

%Since periodic \textsc{Fourier} series are used as excitation trajectories, no leakage errors are introduced due to the allowed settling time of the system.

To take into account the different signal-to-noise ratios in each joint, the WLS method is used for parameter estimation. The measurement points are weighted using the inverse of the covariance matrix of the measured torque. The noise of the torque measurements in all joints $j$ in every time step $t_p$ can be estimated using the equation for the sample variance
\begin{equation}\label{eq:var_tau}
	\sigma^2_{j,p} = \frac{1}{N_\text{r}-1} \sum\limits_{k=1}^{N_\text{r}} (\tau_{k,j} (t_p) - \overline{\tau}_j(t_p))^2,
\end{equation}
where $k$ refers to the repetitions of the experiment.
The matrix $\boldsymbol{W}$ is then defined as:
\begin{equation}\label{eq:WLS_Gew}
	\boldsymbol{W} = \mathrm{diag}(\boldsymbol{W}_1^{-1}, \boldsymbol{W}_2^{-1}, \hdots, \boldsymbol{W}_6^{-1})
\end{equation}
	with
\begin{equation}
	\boldsymbol{W}_j = \mathrm{diag}(\sigma^2_{j,1}, \sigma^2_{j,2}, \hdots, \sigma^2_{j,p}).
\end{equation}
Since the measurements are assumed to be independent, $\boldsymbol{W}$ is a diagonal matrix.
All measurements used for this evaluation were taken when the robot was in the controlled state.


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