The properties desired of CBC Casper broadly fall into two categories: 1) safety and 2) non-triviality. Definition of what these two things are. Some intuition on why these two things are important. We show below our incremental approach to proving these properties, starting from a definition of CBC Casper in terms of a state transition system with a reflexive and transitive reachability relation, also known in rewrite logic literature as a partial order and in modal logic literature as a KT4 Kripke model.
\subsection{Partial order}
\begin{lstlisting}
Class PartialOrder :=
{ A : Type;
	A_eq_dec : forall (a1 a2 : A), {a1 = a2} + {a1 <> a2};
	A_inhabited : exists (a0 : A), True;
	A_rel : A -> A -> Prop;
	A_rel_refl :> Reflexive A_rel;
	A_rel_trans :> Transitive A_rel;
}.
\end{lstlisting}
At this level, we are able to derive all of the safety properties desired of CBC Casper, namely:
\begin{lstlisting}
Theorem pair_common_futures '{CBC_protocol_eq}:
forall s1 s2 : pstate,
(equivocation_weight (state_union s1 s2) <= proj1_sig t)%R ->
exists s : pstate, pstate_rel s1 s /\ pstate_rel s2 s.

Theorem n_common_futures '{CBC_protocol_eq} :
forall ls : list pstate,
(equivocation_weight (fold_right state_union state0 (map (fun ps => proj1_sig ps) ls)) <= proj1_sig t)%R ->
exists ps : pstate, Forall (fun ps' => pstate_rel ps' ps) ls.

Theorem pair_consistency_prot '{CBC_protocol_eq} :
forall s1 s2 : pstate,
(equivocation_weight (state_union s1 s2) <= proj1_sig t)%R ->
forall P,
~ (decided P s1 /\ decided (not P) s2).

Theorem n_consistency_prot '{CBC_protocol_eq} :
forall ls : list pstate,
(equivocation_weight (fold_right state_union state0 (map (fun ps => proj1_sig ps) ls)) <= proj1_sig t)%R ->
state_consistency ls.

Theorem n_consistency_consensus '{CBC_protocol_eq} :
forall ls : list pstate,
(equivocation_weight (fold_right state_union state0 (map (fun ps => proj1_sig ps) ls)) <= proj1_sig t)%R ->
consensus_value_consistency ls.
\end{lstlisting}
These results correspond to Theorems X - Y in \cite{CBCfull}.
\newline

\subsection{Partial order with non-local confluence}
In order to prove non-triviality properties, we additionally require  that our partial order possess certain confluence properties. In fact, we find that non-triviality as defined in \cite{CBCfull} directly captures the notion of non-local confluence in state transition systems, defined abstractly as follows:

\begin{lstlisting}
Class PartialOrderNonLCish '{PartialOrder} :=
{ no_local_confluence_ish : exists (a a1 a2 : A),
														A_rel a a1 /\ A_rel a a2 /\
														~ exists (a' : A), A_rel a1 a' /\ A_rel a2 a';
}.
\end{lstlisting}

\subsection{Abstract protocol}
To provide a richer, protocol-specific language to describe our desired properties, we give an abstract type class from which we can generalize a partial order, but which contains information specific to consensus protocols, including types for validators, consensus values, states, and an abstract, total estimator function.
\begin{lstlisting}
Class CBC_protocol_eq :=
{
consensus_values : Type;
about_consensus_values : StrictlyComparable consensus_values;
validators : Type;
about_validators : StrictlyComparable validators;
weight : validators -> {r | (r > 0)%R};
t : {r | (r >= 0)%R};
suff_val : exists vs, NoDup vs /\ ((fold_right (fun v r => (proj1_sig (weight v) + r)%R) 0%R) vs > (proj1_sig t))%R;
state : Type;
about_state : StrictlyComparable state;
state0 : state;
state_eq : state -> state -> Prop;
state_union : state -> state -> state;
state_union_comm : forall s1 s2, state_eq (state_union s1 s2) (state_union s2 s1);
reach : state -> state -> Prop;
reach_refl : forall s, reach s s;
reach_trans : forall s1 s2 s3, reach s1 s2 -> reach s2 s3 -> reach s1 s3;
reach_union : forall s1 s2, reach s1 (state_union s1 s2);
reach_morphism : forall s1 s2 s3, reach s1 s2 -> state_eq s2 s3 -> reach s1 s3;
E : state -> consensus_values -> Prop;
estimator_total : forall s, exists c, E s c;
prot_state : state -> Prop;
about_state0 : prot_state state0;
equivocation_weight : state -> R;
equivocation_weight_compat : forall s1 s2, (equivocation_weight s1 <= equivocation_weight (state_union s2 s1))%R;
about_prot_state : forall s1 s2, prot_state s1 -> prot_state s2 ->
(equivocation_weight (state_union s1 s2) <= proj1_sig t)%R -> prot_state (state_union s1 s2);
}.
\end{lstlisting}

The plan here is to 1) say that our t

We prove that \verb|CBC_protocol_eq| can derive \verb|PartialOrder|.


\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage[margin=1in]{geometry}

\title{An inner product space of sampled fission sources}
\author{James Holloway and Jeremy Conlin}

\newtheorem{definition}{Definition}
\newtheorem{theorem}{Theorem}

\begin{document}

\maketitle

\section{Source vectors}

We need a vector space that can contain all possible sampled fission sources.  The purpose of this document is to define such a vector space.  A vector space provides a notion of scalar multiplication and a notion of vector addition, with certain properties.  The key properties are that addition is commutative and distributive, there is a zero vector, for every vector there is an additive inverse that when summed with that vector will yield zero, and the scalar multiplication is associative and distributive over vector addition.

\subsection{Source vectors}

First, we define a source point as a location in space combined with a weight, which can be positive, negative or zero.
\begin{definition} A \emph{source point} $s$ is a pair $s = (w, \mathbf{x})$ where $w \in \mathbf{R}\backslash0$ is a non-zero real number called the \emph{weight}, and $\mathbf{x} \in \Gamma$ is a point in a subset of 3-space, $\Gamma \subset \mathbf{R}^3$.  Note that the weight cannot be zero, but can be positive or negative.   We denote by $w(s)$ the weight of a source point, $s$, and by $\mathbf{x}(s)$ the location of the source point.
\end{definition}
We will need a notion of equality for source points; two source points are equal if they are at the same location and have equal weight.
\begin{definition}
Two source points, $s_1, s_2$, are equal, written as $s_1 = s_2$ if and only if $w(s_1) = w(s_2)$ as real numbers and $\mathbf{x}(s_1) = \mathbf{x}(s_2)$ as points in $\Gamma$.
\end{definition}
For convenience of notation it will be useful to define multiplication of a source point $s$ by a non-zero real number $\alpha$ as
\begin{definition}
For $\alpha \in \mathbf{R}\backslash 0$ and source point $s$, multiplication $\alpha s$ is defined as producing the new source point $\alpha s = (\alpha w(s), \mathbf{x}(s))$ at the same location, but with weight scaled by $\alpha$.
\end{definition}
Note that there is no meaning to adding source points together.  A notion of this sort will be the heart of defining a vector space whose elements are lists of source points. In addition, we will need a sense of scalar multiplication that includes multiplication by zero.

Now we define a collection,  $S$, of source points that can represent a fission source as a finite collection of source points.
\begin{definition}
Let $N$ be a non-negative integer.  A \emph{source} $S$ is a set of $N$ non-zero-weight source points, $S = \{s_1, s_2, \ldots, s_N\}$, $w(s_i) \ne 0$ $i = 1, 2, \ldots N$, with distinct locations $\mathbf{x}(s_i) \ne \mathbf{x}(s_j)$, $i \ne j$.  $N$ is called the number of source points, and will also be written as $N(S)$.
\end{definition}
It is very important that $S$ is a set, and so we do not allow repeated source point locations $\mathbf{x}(s_i) \ne \mathbf{x}(s_j)$. Note that sets are un-ordered (by definition), so the order of the source points does not matter; $\{s_1, s_2\}$ is the same source as $\{s_2, s_1\}$.  Note also that a source does not just have distinct source points, but rather has source points with distinct locations.

There is a very interesting source, namely the source with $0$ source points.  There is only one such source, since it is simply the empty set.  This special source is very important to defining a vector space of sources, and it is physically important too.
\begin{definition}
The unique source with no source points is called the \emph{zero source, and will be denoted $0$}.
\end{definition}

Let's discuss equality of any two source $S_1, S_2$.  This is simply the set equality imposed by our previous definition of equality of source points.   For $S_1$ and $S_2$ to be equal they must:
\begin{enumerate}
\item Have the same number of source points, $N = N(S_1) = N(S_2)$
\item If $N > 0$, then for each source point $s_1 \in S_1$ there must exist a source point $s_2 \in S_2$ (and there can be only one because $S_1$ and $S_2$ are sets) such that $s_1 = s_2$.
\end{enumerate}

We are finally ready to define a vector space of sources.
Let $\mathcal{S}$ be the set of all sources (including sources of any number of source points from zero on up).
We can give $\mathcal{S}$ a vector space structure by defining scalar multiplication (over the real numbers) and by defining the addition of sources, with the appropriate properties.  Let's define scalar multiplication first, basically as scaling the source weights by a scalar.
\begin{definition}
Let $S \in \mathcal{S}$, and $\alpha \in \mathbf{R}$ be a real number.  If $S$ has no source points, or if $\alpha = 0$, then $\alpha S = 0$.   Otherwise, with $M = N(S)$ the number of source points, there exist source points $s_i$, $i = 1, \ldots, M$ such that $S = \{s_1, \ldots, s_M\}$, and $\alpha S$ is defined to be the source $\alpha S = \{\alpha s_1, \ldots, \alpha s_M\}$.
\end{definition}
Note that $\alpha S$ is still a good source.  It's either a zero source, or else a set of non-zero-weight source points all at distinct locations.  Scalar multiplication is properly associative $(\alpha \beta) S = \alpha (\beta S)$, and that $\alpha = 1$ is the identity.  Note also that multiplication by a non-zero scalar does not change the number of source points, but multiplication by zero does.

Next we must define vector addition, and in doing so we wish to capture the physical notion of adding together two sources.
\begin{definition}
Given any two sources $S_1, S_2 \in \mathcal{S}$, the sum $S_1 + S_2 \in \mathcal{S}$ is defined as the source consisting of all source points from $S_1$ and $S_2$ that are at distinct locations, and for every pair of source points $s_1 \in S_1$ and $s_2 \in S_2$ that share a common location, $\mathbf{x}(s_1) = \mathbf{x}(s_2)$, the sum $S_1 + S_2$ will contain only the single source point $(w(s_1) + w(s_2), \mathbf{x}(s_1))$ at the same location but with weight equal to the sum total weight of the two originals.  If this combined weight is zero, there is no source point at that location, and that location is not included in the source points of the final sum vector.
\end{definition}
This definition is well posed; by construction $S_1 + S_2$ will contain a finite number of source points all of which are at distinct locations and none of which has zero weight.  Note that the number of source points in $S_1 + S_2$ will be between 0 and $N(S_1) + N(S_2)$, inclusive; the sum will have fewer source points if there were points in $S_1$ and $S_2$ at common locations, and will have no source points if every point in $S_1$ has a partner of opposite weight in $S_2$.

We want now to show that this definition of addition makes $\mathcal{S}$ into a vector space.
\begin{theorem}
With the scalar multiplication and addition just defined, $\mathcal{S}$ is a vector space.
\end{theorem}

\begin{proof}
Note first that the zero source is the additive identity element, $S + 0 = S$ because $0$ contains no source points.  Further, note that $-1 S$ is an additive inverse because every source point from $-1 S$ is at the same location as a source point in $S$, but the sum of the weights of these paired points will be zero.  Hence $-1 S + S = 0$.

Next, we must check the distributive property $\alpha (S_1 + S_2) = \alpha S_1 + \alpha S_2$.  If $\alpha$ is zero this is trivially true, and if either $S_1$ or $S_2$ is zero, it's also trivially true.  Otherwise, we must show that that $\alpha(S_1 + S_2)$ and $\alpha S_1 + \alpha S_2$ contain the same source points.  Scalar multiplication does not alter any source points locations, it just changes the source point weights; therefore for points in $S_1$ and $S_2$ at distinct points  $\alpha S_1 + \alpha S_2$ contains the same source points as $\alpha (S_1 + S_2)$.  For points in $S_1$ and $S_2$ at the same source locations there are two cases to consider: 1) either they sum to zero weight and are removed, or 2) they do not.  In the first case, corresponding points in $\alpha S_1 + \alpha S_2$ will also sum to zero (if $w(s_1) + w(s_2) = 0$ then $\alpha w(s_1) + \alpha w(s_2) = 0$), and in both  $\alpha (S_1 + S_2)$ and $\alpha S_1 + \alpha S_2$ the point in the sum will be eliminated.  In the second case, $\alpha(S_1 + S_2)$ will contain a point at the common location with weight $\alpha(w(s_1) + w(s_2)) = \alpha w(s_1) + \alpha w(s_2)$ and this is the same weight and location as a particle in $\alpha S_1 + \alpha S_2$.

Now we must show that $(\alpha + \beta) S = \alpha S + \beta S$.  If $\alpha + \beta = 0$ then both sides are the zero source, and the statement is true. Similarly if either $\alpha$ or $\beta$ is zero.  So we assume now that $\alpha \ne0$, $\beta \ne 0$, and $\alpha + \beta \ne 0$ and note that $(\alpha + \beta)S$ will contain the same source locations as $S$, and that $S$, $\alpha S$, $\beta S$ and hence $\alpha S + \beta S$ will also all contain the same source locations.  The weight for the source point $s$ in $S$ becomes $(\alpha + \beta) w(s)$ in $(\alpha + \beta) S$, and this same source point generates a point with weight $\alpha w(s) + \beta w(s) = (\alpha + \beta) w(s)$ in the sum $\alpha S + \beta S$.  This establishes that $(\alpha + \beta) S = \alpha S + \beta S$ in all cases.

Next we must show the commutative property $S_1 + S_2 = S_2 + S_1$.   The commutative property is obvious if either source is zero, so we now focus on the non-zero case.  If $s \in S_1 + S_2$ then there exists a point $s'$ in either $S_1$ or $S_2$ or both, such that $\mathbf{x}(s) = \mathbf{x}{s'}$.  Suppose this point $s'$ appears only in $S_1$; then $s = s'$ and this point is also in $S_2 + S_1$.  Similarly if the point $s'$ appears only in $S_2$.  Finally if there is an $s' \in S_1$ and $s'' \in S_3$ such that $\mathbf{x}(s) = \mathbf{x}(s') = \mathbf{x}(s'')$ then $w(s) = w(s') + w(s'') = w(s'') + w(s')$ and this point also appears in $S_2 + S_1$.  Thus, addition is commutative.

Finally, we must show that addition is associative, $(S_1 + S_2) + S_3 = S_1 + (S_2 + S+3)$.  The thinking that leads to this is identical to that showing that addition is commutative.  It does not matter in what order we collect source points into the sum, and if multiple source points share a common location it does not matter in what order we add up their weights.
\end{proof}

\subsection{Mapping sources to functions}

Let $S \in \mathcal{S}$ be a source vector.  We can, in a non-unique way, map this vector to a function $q(x)$ over $\Gamma$.  Let $h(\mathbf{x}, \mathbf{y})$ be any non-negative function (a kernel) from $\Gamma \times \Gamma \to \mathcal{R}$ with the property
\begin{equation}
1 = \int_{\Gamma} h(\mathbf{x}, \mathbf{y}) \, d\mathbf{x} \,.
\end{equation}
Let $\{s_1, \ldots, s_M\}$ be the source points in $S$.  Then
\begin{equation}
Q(S, \mathbf{x}) = \sum_{i=1}^M w(s_i) h(x, \mathbf{x}(s_i))
\end{equation}
is a physical representation of the source as a function of space.   Normally we would also want the function $h$ to be zero outside of $\Gamma$ (so for example, there is no source outside $\Gamma$).  Obviously we define $Q(0, \mathbf{x})$ as the zero function.  Most importantly,
\begin{equation}
Q(\alpha S, \mathbf{x}) = \sum_{i=1}^M \alpha w(s_i) h(x, \mathbf{x}(s_i)) = \alpha Q(s, \mathbf{x})
\end{equation}
so the mapping from $\mathcal{S}$ to the space of functions is linear over scalar multiplication, and indeed, scalar multiplication in $\mathcal{S}$ maps to scalar multiplication of functions.  (Exercise for the reader: show that the mapping is a vector space isomorphism, that is, the vector addition of vectors in $\mathcal{S}$ maps to the vector addition of functions.  This is needed in order to have a well defined inner product below.)

This construction is fairly general.  Note for example that if we want a histogram (in 1-D) then we can define a set of bins and
\begin{equation}
h(x,y) = \begin{cases}
1/\Delta & \text{if $x$ and $y$ are in the same bin}\\
0 & \text{otherwise}
\end{cases}
\end{equation}
where $\Delta$ is the width of the bin in which $x$ and $y$ lie.

Note: It might have been easier to start here and work out the properties needed to add vectors in $\mathcal{S}$ in order to create this vector space isomorphism.

\subsection{An inner product space}

Finally, we want to discuss equipping $\mathcal{S}$ with an inner product; with this in hand $\mathcal{S}$ becomes an inner product space and we can define a norm and orthogonality.  We can also carry out the Arnoldi process, although we should note that $\mathcal{S}$ will be an infinite dimensional inner product space (a Hilbert space), and we don't know much about Arnoldi for infinite dimensional problems.

There is no unique way to attach an inner product to $\mathcal{S}$, but a general approach is to map $\mathcal{S}$ to a space of functions, and use the natural $L^2$ inner product.  So we pick kernel functions $h$ as the the previous section and define
\begin{equation}
\langle S_1, S_2 \rangle = \int_{\Gamma} Q(S_1, \mathbf{x}) Q(S_2, \mathbf{x}) \, d\mathbf{x}
\end{equation}
This inner product is symmetric, and linear in each argument because mapping $\mathcal{S}$ to the space of finite expansions in kernels is a vector space isomorphism.

The inner product is positive-definite
\begin{equation}
\langle S, S \rangle = \int_{\Gamma} Q(S, \mathbf{x}) Q(S, \mathbf{x}) \, d\mathbf{x} \geq 0
\end{equation}
with equality if and only if $S = 0$.

\section{Practical issues}

Because $\mathbf{S}$ is an inner product space, all the steps in Arnoldi can be done by working on source vectors $S \in \mathcal{S}$.   Doing this in practice is problematic because the vectors will contain more and more source points as we orthogonalize.  However, maybe we do not need to explicitly construct vectors of the form $S_1 + \beta S_2$.  Rather we simply need to track of $S_1$, $\beta$, and $S_2$.   A simple data structure can keep this formation.   Then we need a way to sample from the vector $S_1 + \beta S_2$ without explicitly constructing it.  Is there a way to do this?

This is important, because explicitly adding two vectors $S_1$ and $S_2$ could be a very expensive process: to accomplish it we must find any points in the two vectors that share a single source location (of course the probability of this is so small we might neglect it).

\end{document}


\documentclass[letterpaper,12pt]{article}

\usepackage[margin=1in]{geometry}
\usepackage{amsmath,amsthm,amsfonts,amssymb}
\usepackage{mathtools}
\usepackage{algorithm,algpseudocode}
\usepackage{hyperref}
\usepackage{graphicx}

% \newcommand{\Mod}[1]{\ (\mathrm{mod}\ #1)}

\theoremstyle{remark}
\newtheorem{claim}{Claim}

\DeclarePairedDelimiter\abs{\lvert}{\rvert}
\DeclarePairedDelimiter\floor{\lfloor}{\rfloor}
\DeclarePairedDelimiter\ceiling{\lceil}{\rceil}

\begin{document}

\title{Brief description of the algorithms}
\date{\today}
\author{Vaibhav Sinha}
\maketitle

\section{Discrete Log Problem}

In this problem we are given $g, y$ and $n$ and we have to find an $x$ such that $g^x \equiv y \pmod n$. We use this $\equiv$ and = alterchangebaly in the report and the meaning is clear from the context.

First notice a few things:
\begin{enumerate}
    \item If $g = 0$ or $1$ then all powers $x$ would only produce $0$ and $1$ respectively. We can always output $1$ in this case. Also observe that without loss of generality, $g < n$. So in the rest of the report we will only consider $g \in \{2 \dots n-1\}$.
    \item When $n$ is prime, then $y \neq 0$, as $y = 0$ can not have any solution. So in such cases, $y \in [n-1]$ and $x \in [n-1]$. Notice that one can equivalently take the $x \in \{0, \dots n-2\}$ as $g^0 \equiv g^{n-1} \equiv 1 \pmod g$.
\end{enumerate}

We now describe a few algorithms to solve the discrete log problem. Observe that none of these algorithms are truly polynomial time. No efficient algorithm are known for solving Discrete Log Problem in general setting. We implement these algorithms (and some of these only work) modulo a prime as Diffie-Hellman uses a prime modulus.

\subsection{Brute Force Algorithm}
In this algorithm we simply try all possible $x$'s. This takes $O(n)$ time.

\subsection{Baby-Step Giant-Step Algorithm}

This algorithm trades off space to improve the running time over brute force. The idea is to write $x = i\lceil n \rceil + j$ for $0 \le i,j \le \lceil n \rceil$. For simplicity let $m = \lceil n \rceil$. First compute $g^j$ for all $0 \le j \le m$. Then we compute $y\cdot g^{-im}$ for each $0 \le i \le m$. Notice that both can be done in $O(\sqrt n )$ time. If we could find a collision $y\cdot g^{-im} = g^j$, then $x = im + j$. To find this collision efficiently we store all $g^j$ (for all $0 \le j \le m$) by hash-sets and when we compute $y\cdot g^{-im}$ we simply check if that value is already present in the set. This finds the collision in $O(\sqrt{n})$ time (as we need to check if the value is present in the set only $O(\sqrt{n})$ times). Overall this algorithm uses $O(\sqrt{n})$ space and time.

\subsection{Pollard's $\rho$ algorithm}

This is a randomized algorithm that works in $O(\sqrt{n})$ expected time and with $O(1)$ space requirement. We descirbe the algorithm for prime modulus. All the operations described in this section work mod $p$.

The idea is to divide $\{1, \dots n-1\}$, which are the possible $x$s into three sets, say, $S_0, S_1$ and $S_2$ of roughly equal size. In practice we do it by taking $x \pmod 3$ ie. $x \in S_i$, where $i = x \pmod 3$. We do a random walk starting at $x_0 = g^{a_0} y^{b_0}$ where $a_0 = b_0 = 0$. To generate $x_{i+1}$ from $x_i$ we follow the following rule:

\begin{equation*}
    x_{i+1} =
    \begin{dcases}
        x_i^2 \text{ if } x_i \in S_0 \\
        yx_i \text{ if } x_i \in S_1 \\
        gx_i \text{ if } x_i \in S_2
    \end{dcases}
\end{equation*}

Corresponding to these $x_i$'s we also maintain the $a_i, b_i$ such that $x_i = g^{a_i} y^{b_i}$. Now suppose we find $i$ and $j$, $i \neq j$, such that $x_i = x_j$, or $g^{a_i} y^{b_i} = g^{a_j} y^{b_j}$. Then $g^{a_i - a_j} = y^{b_j - b_i} \pmod n$. Substituing $y = g^x$, we get the equation $a_i - a_j = x (b_j - b_i)$. We then solve this using a linear congrence solver to obtain $x$.

So all we need to so now is to find $i$ and $j$, $i \neq j$, such that $x_i = x_j$. This is done using the classic Floyd's Hare and Tortoise algorithm (by which the algorithm gets the $\rho$ in its name). Essentially there are two pointers, one that jumps two steps and another that jumps only one. If there are $i$ and $j$, such that $x_i = x_j$, then these pointers would meet at the same point, giving us $i$ and $j$.

\subsection{Pohlig Hellman algorithm}

Let $g^x = y \pmod n$. Given $g$, $y$ and $n$ where $g$ is the generator $\mathbb{Z}^*_{n}$ we need to find $x$. We are also given the factorization of $n-1$ as,
\begin{equation*}
    n-1 = p_1^{e_1} p_2^{e_2} \cdots p_k^{e_k}
\end{equation*}
where $p$'s are distinct primes. Notice that $1 \le y \le n-1$ and $0 \le x \le n-2$ (as $g^0 = g^{n-1} = 1$).

The idea of Pohlig Hellman is to first compute $x \pmod {p_i^{e_i}}$ for each $i \in [k]$. As all $p_i^{e_i}$ are mututally coprime we can recover $x \pmod {n-1}$ by Chinese Remainder Theorem. We show how to compute $x \pmod {p_i^{e_i}}$. We now drop the indices from $p_i$ and $e_i$ for simplicity (and refer to them as $p$ and $e$).

Consider the expansion of $x$,
\begin{equation*}
    x = \sum_{0 \le i \le e-1} x_i p^i + sp^e
\end{equation*}

Observe,
\begin{equation*}
    y^{\frac{n-1}{p}} \equiv g^{\left(x_0 \frac{n-1}{p} + k(n-1)\right)} \equiv g^{x_0 \frac{n-1}{p}} \pmod n
\end{equation*}
where $k$ is some integer.
This follows because of $g$ is a generator and so $g^{n-1} \pmod n = 1$. So now we can recover $x_0$ by using Baby-Step Giant-Step algorithm (as $y^{\frac{n-1}{p}}$ is the $x_0$th power of $g^{\frac{n-1}{p}} \pmod n$).

This idea extends to find $x_i$. Assume that we have computed $x_0 \dots x_{i-1}$. Now we compute,
\begin{equation*}
    y\cdot g^{-(\sum_{0\le j \le i-1} x_j p^j)} = g^{(\sum_{i \le j \le e-1} x_j p^j) + sp^e}
\end{equation*}
Call this $y'$. Then notice that (using similar simplification as above),
\begin{equation*}
    (y')^{\frac{n-1}{p^{(i+1)}}} \equiv g^{x_i \frac{n-1}{p}} \pmod n
\end{equation*}
Again we use Baby-Step Giant-Step algorithm to compute $x_j$. We continue this to extract each $x_i$ until $x_{e-1}$ using which we create $x \pmod {p^e}$. This algorithm is exactly implemented in the code.

\section{Man in the middle attack on the Diffie-Hellman Protocol}

Assume that there is an attacker Eve who wants to evesdrop on Alice and Bob's conversation or worse even to modify their messages. Here's how Eve attacks using the `Man In The Middle' attack if Alice and Bob use Diffie-Hellman Protocol.

Eve obtains the public $g$ and $p$, the generator and modulus, that Alice and Bob have decided to use. Now according to protocol Alice chooses $a$ and sends $g^a$ and Bob chooses $b$ and sends $g^b$. Eve herself chooses an $e$ and generates $g^e$. She sends $g^e$ to Alice and Bob claiming that she is Bob and Alice respectively. She receives both Alice and Bob's $g^a$ and $g^b$ which they had sent.

Now Alice receives $g^e$ and believes that this message is from Bob. So she creates the key $g^{ae}$. Similarly, Bob receives $g^e$ and believes that this message is from Alice. So he creates the key $g^{be}$. Eve computes both $g^{ae}$ and $g^{be}$. Notice now that Eve has both the keys and has Alice and Bob convinced that they are talking to each other.

Now when Alice sends a message encrypted by the key $g^{ae}$, Eve decrypts it, reads (and possibly modifies) it, reencrypts the message using $g^{be}$ and sends it to Bob who successfully decrypts it. The message recieved is what Eve sent him and not Alice but Bob believes that the message is from Alice. Eve similarly intercepts messages from Bob to Alice. Thus Alice and Bob think that they are communicating with each other without realizing that there is a `man' in the middle, Eve, who is evesdropping and possibly modifing their messages.

This attack happens because there is no way for Alice and Bob to authenticate that they are talking to the persons they think they are. This vulenrability is fixed by using digital signatures (a mechanism for authentication) and other such protocols.

\end{document}


\section{Autoregressive Models}

We begin our study with the autoregressive generative models. As before, we assume we are given access to a dataset $\mathcal{D}$  of $n$-dimensional datapoints $\mathbf{x}$. For simplicity, we assume the datapoints are binary, i.e.,  $\mathbf{x} \in \{0,1\}^n$.

\section{Representation}

By the chain rule of probability, we can factorize the joint distribution over the $n$-dimensions as:
\[
\begin{equation}
p(\mathbf{x}) = \prod\limits_{i=1}^{n}p(x_i \vert x_1, x_2, \ldots, x_{i-1}) = \prod\limits_{i=1}^{n} p(x_i \vert \mathbf{x}_{<i})
\end{equation}
\label{eq:chain_rule}
\]
where $\mathbf{x}_{<i}=[x_1, x_2, \ldots, x_{i-1}]$ denotes the vector of random variables with index less than $i$. If we allow for every conditional $p(x_i \vert \mathbf{x}_{<i})$ to be specified in a tabular form, then such a representation is fully general and can represent any possible distribution over $n$ random variables. However, the space complexity for such a representation grows exponentially with $n$.

To see why, let us consider the conditional for the last dimension, given by $p(x_n \vert \mathbf{x}_{<n})$. In order to fully specify this conditional, we need to specify a probability for $2^{n-1}$ configurations of the variables $x_1, x_2, \ldots, x_{n-1}$.  Since the probabilities should sum to 1, the total number of parameters for specifying this conditional is given by $2^{n-1} -1$. Hence, a tabular representation for the conditionals is impractical for learning the joint distribution in (\ref{eq:chain_rule}) .

In an \textit{autoregressive generative model}, the conditionals are specified as parameterized functions with a fixed number of parameters. That is, we assume the conditional distributions $p(x_i \vert \mathbf{x}_{<i})$ to correspond to a Bernoulli random variable and learn a function that maps the preceeding random variables $x_1, x_2, \ldots, x_{i-1}$ to the mean of this distribution. Hence, we have:
\[
p_{\theta_i}(x_i \vert \mathbf{x}_{<i}) = \mathrm{Bern}(f_i(x_1, x_2, \ldots, x_{i-1}))
\]
where $\theta_i$ denotes the set of parameters used to specify the mean function $f_i: \{0,1\}^{i-1}\rightarrow [0,1]$.  The term \textit{autoregressive} originates from the literature on time-series models where observations from the previous time-steps are used to predict the value at the current time step. Here, we are predicting the distribution for the $i$-th random variable using the values of the preceeding random variables in the sequence $x_1, x_2, \ldots, x_n$.

The number of parameters of an autoregressive generative model are given by $\sum_{i=1}^n \vert \theta_i \vert$. As we shall see in the examples below, the number of parameters are much fewer than the tabular setting considered previously. Unlike the tabular setting however, an autoregressive generative model cannot represent all possible distributions. Its expressiveness is limited by the fact that we are limiting the conditional distributions to correspond to a Bernoulli random variable with a restricted class of parameterized functions specifying the mean.

In the simplest case, we can specify the function as a linear combination of the input elements followed by a sigmoid non-linearity (to restrict the output to lie between 0 and 1). This gives us the formulation of a \textit{fully-visible sigmoid belief network} (FVSBN):
\[
f_i(x_1, x_2, \ldots, x_{i-1}) =\sigma(\alpha^{(i)}_0 + \alpha^{(i)}_1 x_1 + \ldots + \alpha^{(i)}_{i-1} x_{i-1})
\]
where $\sigma$ denotes the sigmoid function and $\theta_i=\{\alpha^{(i)}_0,\alpha^{(i)}_1, \ldots, \alpha^{(i)}_{i-1}\}$ denote the parameters of the mean function. The conditional for variable $i$ requires $i$
 parameters, and hence the total number of parameters in the model is given by $\sum_{i=1}^ni= O(n^2)$.  Note that the number of parameters are much fewer than the exponential parameters required in the tabular case.

A natural way to increase the expressiveness of an autoregressive generative model is to use more flexible parameterizations for the mean function e.g., multi-layer perceptrons (MLP). In the case of 1-hidden layer neural networks, the mean function for variable $i$ can be expressed as:
\[
\mathbf{h}_i = \sigma(A_i \mathbf{x_{<i}} + \mathbf{c}_i)\\
f_i(x_1, x_2, \ldots, x_{i-1}) =\sigma(\boldsymbol{\alpha}^{(i)}\mathbf{h}_i +b_i )
\]
where $\mathbf{h}_i \in \mathbb{R}^d$ denotes the hidden layer activations for the MLP and$\theta_i = \{A_i \in \mathbb{R}^{d\times (i-1)},  \mathbf{c}_i \in \mathbb{R}^d, \boldsymbol{\alpha}^{(i)}\in \mathbb{R}^d, b_i \in \mathbb{R}\}$ are the set of parameters for the mean function $\mu_i(\cdot)$.  The total number of parameters in this model is dominated by the matrices $A_i$ and given by $O(n^2 d)$.

The Neural Autoregressive Density Estimation (NADE) provides an efficient MLP parameterization that shares parameters used for evaluating the hidden layer activations.
\[
\mathbf{h}_i = \sigma(W_{., <i} \mathbf{x_{<i}} + \mathbf{c})\\
f_i(x_1, x_2, \ldots, x_{i-1}) =\sigma(\boldsymbol{\alpha}^{(i)}\mathbf{h}_i +b_i )
\]
where $\theta=\{W\in \mathbb{R}^{d\times n}, \mathbf{c} \in \mathbb{R}^d, \{\boldsymbol{\alpha}^{(i)}\in \mathbb{R}^d\}^n_{i=1}, \{b_i \in \mathbb{R}\}^n_{i=1}\}$is the full set of parameters for the mean functions $f_1(\cdot), f_2(\cdot), \ldots, f_n(\cdot)$. The weight matrix $W$ and the bias vector $\mathbf{c}$ are shared across the conditionals. Sharing parameters offers two benefits:
\begin{enumerate}
\item The total number of parameters from $O(n^2 d)$ to $O(nd)$ [readers are encouraged to check!].
\item The hidden unit activations can be evaluated in $O(nd)$ time via the following recursive strategy:
\[
\mathbf{h}_i = \sigma(\mathbf{a}_i)\\
\mathbf{a}_{i+1} = \mathbf{a}_{i} + W[., i]x_i
\]
with the base case given by $\mathbf{a}_1=\mathbf{c}$.
\end{enumerate}

\section{Learning and inference}

Recall that learning a generative model involves optimizing the closeness between the data and model distributions. One commonly used notion of closeness in the KL divergence between the data and the model distributions.

$$
\begin{align*}
\min_{\theta\in \mathcal{M}}d_{KL}(p_{\mathrm{data}}, p_{\theta}) &= \mathbb{E}_{\mathbf{x} \sim p_{\mathrm{data}} }\left[\log p_{\mathrm{data}}(\mathbf{x}) - \log p_{\theta}(\mathbf{x})\right].
\end{align*}
$$
Before moving any further, we make two comments about the KL divergence. First, we note that the KL divergence between any two distributions is asymmetric. As we navigate through this chapter, the reader is encouraged to think what could go wrong if we decided to optimize the reverse KL divergence instead. Secondly, the KL divergences heavily penalizes model distribution $p_\theta$ which place little mass on any datapoint that has a non-zero probability under $p_{\mathrm{data}}$. In the extreme case, if the density $p_\theta(\mathbf{x})$ evaluates to zero for a datapoint sampled from $p_{\mathrm{data}}$, the objective evaluates to $+\infty$.

Since $p_{\mathrm{data}}$ does not depend on $\theta$, we can equivalently recover the optimal parameters via maximizing likelihood estimation:

$$
\begin{align*}
\max_{\theta\in \mathcal{M}}\mathbb{E}_{\mathbf{x} \sim p_{\mathrm{data}} }\left[\log p_{\theta}(\mathbf{x})\right].
\end{align*}
$$
Here, $\log p_{\theta}(\mathbf{x})$ is referred to as the log-likelihood of the datapoint $\mathbf{x}$ with respect to the model distribution $p_\theta$.

To approximate the expectation over the unknown $p_{\mathrm{data}}$, we make an assumption: points in the dataset $\mathcal{D}$ are sampled i.i.d. from $p_{\mathrm{data}}$. This allows us to obtain an unbiased Monte Carlo estimate of the objective:

$$
\begin{align}
\max_{\theta\in \mathcal{M}}\frac{1}{\vert D \vert} \sum_{\mathbf{x} \in\mathcal{D} }\log p_{\theta}(\mathbf{x}) = \mathcal{L}(\theta \vert \mathcal{D}).
\end{align}
\label{eq:mle}
\tag{2}
$$

The maximum likelihood estimation (MLE) objective has an intuitive interpretation: pick the model parameters $\theta \in \mathcal{M}$ that maximize the log-probability of the observed datapoints in $\mathcal{D}$.

In practice, we optimize the MLE objective using mini-batch gradient ascent. The algorithm operates in iterations. At every iteration $t$, we sample a mini-batch $\mathcal{B}_t$  of datapoints sampled randomly from the dataset ($\vert \mathcal{B}_t\vert < \vert \mathcal{D} \vert$) and compute gradients of the objective evaluated for the mini-batch. These parameters at iteration $t+1$ are then given via the following update rule:
\[
\theta^{(t+1)} = \theta^{(t)} + r_t \nabla_\theta\mathcal{L}(\theta^{(t)} \vert \mathcal{B}_t)
\]
where $\theta^{(t+1)}$ and $\theta^{(t)}$ are the parameters at iterations $t+1$ and $t$ respectively, and $r_t$ is the learning rate at iteration $t$.  Typically, we only specify the initial learning rate $r_1$ and update the rate based on a schedule.  [Variants](http://cs231n.github.io/optimization-1/) of stochastic gradient ascent, such as RMS prop and Adam, employ modified update rules that work slightly better in practice.

From a practical standpoint, we must think about how to choose hyperaparameters (such as the initial learning rate) and a stopping criteria for the gradient descent. For both these questions, we follow the standard practice in machine learning of monitoring the objective on a validation dataset. Consequently, we choose the hyperparameters with the best performance on the validation dataset and stop updating the parameters when the validation log-likelihoods stop improving.

Now that we have a well-defined objective and optimization procedure, the only remaining task is to evaluate the objective in the context of an autoregressive generative model. To this end, we substitute the factorization of the joint distribution in Eq.$~\ref{eq:chain_rule}$
in the MLE objective in Eq.$~\ref{eq:mle}$ to get:
\[
\max_{\theta \in \mathcal{M}}\frac{1}{\vert D \vert} \sum_{\mathbf{x} \in\mathcal{D} }\sum_{i=1}^n\log p_{\theta_i}(x_i \vert \mathbf{x}_{<i})
\]where $\theta = \{\theta_1, \theta_2, \ldots, \theta_n\}$ now denotes the collective set of parameters for the conditionals.

Inference in an autoregressive model is straightforward. For density estimation of an arbitrary point $\mathbf{x}$, we simply evaluate the log-conditionals $\log p_{\theta_i}(x_i \vert \mathbf{x}_{<i})$ for each $i$ and add these up to obtain the log-likelihood assigned by the model to $\mathbf{x}$. Since we know conditioning vector $\mathbf{x}$, each of the conditionals can be evaluated in parallel. Hence, density estimation is efficient on modern hardware.

Sampling from an autoregressive model is a sequential procedure. Here, we first sample $x_1$, then we sample $x_2$ conditioned on the sampled $x_1$, followed by $x_3$ conditioned on both $x_1$ and $x_2$ and so on until we sample $x_n$ conditioned on the previously sampled $\mathbf{x}_{<n}$. For applications requiring real-time generation of high-dimensional data such as audio synthesis, the sequential sampling can be an expensive process.

TODO: add NADE samples figure

Finally, an autoregressive model does not directly learn unsupervised representations of the data. In the next few set of lectures, we will look at latent variable models (e.g., variational autoencoders) which explicitly learn latent representations of the data.

TODO: Autoregressive generative models based on Autoencoders, RNNs, and CNNs.
MADE, Char-RNN, Pixel-CNN, Wavenet


\chapter{Background}\label{chap:background}

The explanations of the fundamental elements of reinforcement learning are based on the book Reinforcement Learning:
An Introduction\cite{Sutton1998} from Richard S. Sutton and Andrew G. Barto.

\section{Markov Decision Processes}
The Markov Decision Process (MPD)  is the mathematical framework of Reinforcement Learning and
is defined as a tuple < $\mathcal{S, A, R, T}$ >
\begin{itemize}
\item $\mathcal{S}$ is a set of states, s $\in \mathcal{R}^{n}$
\item $\mathcal{A}$ is a set of actions, a $ \in \mathcal{R}^{n}$
\item $\mathcal{R}$ is the reward
\item $\mathcal{T}$ is the transition probability function
\end{itemize}
Every state in a Markov Decision Process needs to satisfy the Markov Property,
which means that "The future is independent of the past given the present".
The definition is given by a state reward pair
\begin{equation}
\mathcal{P}(S_{t+1}, R_{t+1} | S_{t}, R_{t}) =  \mathcal{P}(S_{t+1}, R_{t+1} | S_{t}, R_{t}, ..., S_{0}, R_{0})
\end{equation}
Unfortunately for most real-world problems this assumption is violated. This is also the case for the
state transition function, which is defined as
\begin{equation}
  \mathcal{P}(s_{t+1}| s_{t}, a_{t}) =  \mathcal{P}(S_{t+1} = s^{'}| S_{t} = s ,  A_{t} = a)
\end{equation}
 The goal of the agent is to find the policy that maximizes the total reward $(R_{t})$.
In the discounted case is given as
\begin{equation}
  R_t = \sum^{\infty}_{k = 0} \gamma^k r_{t+k+1}
\end{equation}
with $\gamma$ as the discount factor to exponentially discounts future rewards.
The policy is a function, which in the deterministic case maps states to actions.
While a stochastic policy $\pi(a|s)$ has a certain probability of selecting an action \textit{a} in state \textit{s}.
For every MDP there is always at least one optimal policy $\pi$.
In case the transition probability is given, the \textit{dynamic programming} based \textit{value iteration} Algorithm is one way to compute policy $\pi$.
This Algorithm is build upon the concept of a value function.
\subsection{Value Function}
The value function $V^{\pi}$ : $\mathcal{S}$ $\rightarrow$ $\mathcal{R}$, represents the expeceted total reward in a given state when following the policy $\pi$.
It is defined as
 \begin{equation}
  \begin{aligned}
    V^{\pi}(s) &= \mathbb{E}_{\pi} [R_t | s_t = s] \\
    &= \mathbb{E}_{\pi} [\sum^{\infty}_{k=0} \gamma^{k} r_{t+k+1} | s_t = s] \label{MDP:eq1} \\
    &= \sum_{a \in \mathcal{A}} \pi(s, a) \sum_{s' \in \mathcal{S}} \mathcal{T}^{a}_{ss'}[\mathcal{R}^{a}_{ss'} + \gamma V^\pi(s')]
  \end{aligned}
\end{equation}
In each Iteration \textit{i} of the \textit{value iteration} Algorithm the value function is updated by the following equation
\begin{equation}
  V_{i+1}(s) := \max_a \Big\{ \sum_{s', r} P(s',r| s,a) (r + \gamma V_i(s')) \Big\}
\end{equation}
This update is repeated until both sides of the equation are equal.
\subsection{Q-value Function}
In many real-world problems the transition function is unknown and computing the value function is not feasible, because
it would require evaluating all possible actions in a single state at every time step.
To evaluate every action in a state the Q-value function $ \mathcal{Q}: \mathcal{S} \times \mathcal{A} \rightarrow \mathcal{R} $
\begin{align}
  Q^{\pi}(s, a) &= \textsc{E}_\pi[R_t | s_t = s, a_t = a] \\
  &= \textsc{E}_\pi [\sum^\infty_{k=0} \gamma^k r_{t+k+1} | s_t = s, a_t = a]
\end{align}
It represents the expected reward of the agent if the action a is chosen in state s at time step t when following the current policy $\pi$.
The Bellman Optimality Equation is one way to update the Q-value Function
\begin{equation}
  Q(s_t, a_t) \leftarrow Q(s_t, a_t) + \alpha[r_{t+1} + \gamma \max_a Q(s_{t+1}, a) - Q(s_t, a_t)]
\end{equation}
where $ \alpha$ is the \textit{learning rate} and $\gamma$ weights between short and longterm reward.

\subsection{Q Learning}
This update equation is used to find an optimal policy in the Q-Learning Algorithm repesened in Algorithm \ref{alg:q-learning}
\input{figures/background/Q-learning.tex}
The Algorithm is based on the concept of Temporal Difference Learning.
It uses the combined ideas of Dynamic Programming (DP) and Monte Carlo (MC) methods.
Like in Dynamic Programming the old estimate to update the new estimate. Unlike MC it uses a bootstraped estimate.

Q-Learning is an off-policy Algorithm, it uses different policies for interacting with the environment and updating the Q-value Function.

\section{Neural Network}
In complex environments, the state space is continuous, which makes it infeasible to store all state-action pairs in a table.
Advances in machine learning especially in deep learning make it possible to approximate the Q function.
Basic elements of Deep learning are artificial neural networks which are inspired by the human brain.
Neural network have a structure of input layer connected to hidden layers followed by the output layer.
A fully connected network has many hidden layers represented as nodes. The nodes between different layers are connected by weights.
The values of the weights can be trained iteratively by using an optimization technique like stochastic gradient descent and backpropagation.
This simple architecture could be seen as matrix multiplications which makes it a linear function.
By adding nonlinear activation functions it is able to approximate more complex nonlinear functions.
\begin{figure}[h]
  \centering
  \includegraphics[width=0.5\textwidth]{figures/background/nn.png}
  \caption{Deep Neural Network with several hidden layers}
  \label{fig:tab-training}
\end{figure}

\section{Soft Q Learning}

Soft Q learning uses the maximum entropy to augment the standard RL objective
by maximizing also the entropy of the policy.

\begin{equation}
  \pi^* = \underset{\pi}{\mathrm{argmax}} \sum_{t=0}^{T} E_{(s_t, a_t) \sim \tau_{\pi}}[\gamma^t (r(s_t, a_t) + \alpha \mathcal{H}(\pi(.|s_t))]
\end{equation}

The entropy of the polciy $\pi$ is represented by $\mathcal{H}(\pi(.|s_t))]$ and is computed with $-log\pi(.|s_t)$.
This new objectiv leads to a stochasic policy and an implicit exploration.
In most cases it is difficult to find a good $\alpha$, for this nt. Haarnoja et al. (2018) proposed to adjust $\alpha$  dynamically by taking a
gradient step with respect to the loss

\begin{equation}\label{eq:entropy_temp}
J(\alpha)= \mathbb{E}_{\mathcal{D}, \pi}
\left[
    \log \alpha \cdot  (
        -\log \pi( a_{t} | s_{t} )
        -
        \mathcal{H}_T
    )
\right],
\end{equation}

\section{Actor-Critic Concept}

The Algoritem used in this work is based on Soft Q Learning approch and the Actor-Critic Concept.

The Actor is a Neural Network to approximate our policy function p(s) in continuous action spaces.
This is necessary because with infinite actions the max operation for updating the Q-value function is not feasible. The probably simplest way to update the
actor weights to force the actor to select actions that have a higher Q-value is by using the critic to evaluate the actions.

A simple way is to minimize the policy gradient loss:

\begin{equation}
  \nabla_{\theta} \mathcal{L}_{\theta}(\mathcal{D}) = - \mathbb{E}_{s \sim \mathcal{D}} [\nabla_{\theta} log \pi_{\theta}(a|s) \cdot Q_{\phi}(s,a)]
\end{equation}

The Critic is used to evaluate the action of the actor. It is a neural network that approximates the Q-value function and it can be trained by minimixing the mean squard error loss
of the training data-set $\mathcal{D}$ represented by the replay buffer.

\begin{equation}
  \mathcal{L}_{\phi}(\mathcal{D}) =  \mathbb{E}_{s,a, r, s^{\prime} \sim \mathcal{D}} [Q_{\phi}(s,a) - \underbrace{( r + \gamma \: \underset{a^{\prime}}{max} \:  Q_{\phi^{\prime}}(s^{\prime},a^{\prime})}_{TD - target}]^{2}
\end{equation}


\section{Physics}
\subsection{Position, Velocity, \& Acceleration}
Since we know by the FTC that integration is the opposite of differentiation, we can also interpret integrals in a similar physical sense as derivatives.
\begin{table}[H]
	\begin{center}
		\begin{tabular}{ l l }
			$\begin{aligned}\int{v(t)\d{t}}=x(t) + C\end{aligned}$ & $\begin{aligned}\int{a(t)\d{t}}=v(t)\end{aligned}$
		\end{tabular}
	\end{center}
\end{table}

\begin{example}
	A particle starts at $t=0$ with an initial velocity of 5m/s and accelerates for 8 seconds.
	It's acceleration is given by $a(t)=2.4t$ m/s$^2$.
	What is the particle's velocity after the 8 seconds pass?
	What is the particle's displacement after the 8 seconds pass?
\end{example}
\begin{answer}
	We can integrate acceleration to get velocity.
	\begin{equation*}
		v(t) = \int{2.4t\d{t}} = 1.2t^2 + C.
	\end{equation*}

	We know that $v(0)=5$m/s so we can solve\footnote{When we solve for $C$ like this, we're solving what's called an "inital value problem," which we'll do more of when talking about differential equations.} for $C$.
	\begin{align*}
		1.2(0) + C &= 5 \\
		C &= 5 \\
		v(t) = 1.2t^2 + 5.
	\end{align*}

	So, at $t=8$, $v(8) = 1.2(8)^2 + 5 = 81.8$m/s.
	We can now integrate velocity to get displacement
	\begin{equation*}
		x(t) = \int{1.2t^2 + 5 \d{t}} = 0.4t^3 + 5t + C.
	\end{equation*}

	We know that $x(0)=0$m, so $C=0$.
	\begin{equation*}
		x(t) = 0.4t^3 + 5t.
	\end{equation*}

	So, at $t=8$, $x(t) = 0.4(8)^3 + 5(8) = 244.8$m.
	We could have also tackled this problem with definite integrals.
	\begin{align*}
		\Delta x = \int_{0}^{8}{v(t)\d{t}} &= 244.8\text{m} \implies \text{Net Displacement} = x_0 + \Delta x = 244.8\text{m} \\
		\Delta v = \int_{0}^{8}{a(t)\d{t}} &= 76.8\text{m/s} \implies \text{Net Velocity} = v_0 + \Delta v = 81.8\text{m/s}
	\end{align*}
\end{answer}

\subsection{Work}
Work is defined as
\begin{equation*}
	W = Fd
\end{equation*}
where $F$ is force and $d$ is displacement.
The force applied by stretching or compressing a spring beyond its natural length is given by Hooke's Law
\begin{equation*}
	F = kx
\end{equation*}
where $k$ is some spring constant and $x$ is the displacement beyond the spring's natural length.
We can apply ideas as integrals representing net change to find the work needed to compress or stretch a spring.

\begin{example}
	It takes 10N of force to stretch a spring 2m beyond its natural length.
	How much work is done stretching the spring 4m beyond its natural length?
\end{example}
\begin{answer}
	We can use the first bit of information to get the spring constant.
	\begin{align*}
		F &= kx \\
		10\text{N} &= k(2\text{m}) \\
		k &= 5\text{N/m}.
	\end{align*}

	So, $F(x)=5x\text{N}$.
	If we stretch the spring by $\Delta x$, the work done over this interval is approximately $\Delta W = F(x)\Delta x= 5x\Delta x$.
	In the limit, $\d{W} = 5x\d{x}$.
	Integrating both sides from $x=0$ to $x=4$,
	\begin{equation*}
		W = \int_{0}^{4}{\d{W}} = \int_{0}^{4}{5x\d{x}} = 40\text{Nm}.
	\end{equation*}
\end{answer}

We can even bring in other concepts to these problems, like related rates.
\begin{example}
	An inverted conical tank with a height of 10ft and a base radius of 5ft is filled to within 2ft of the top with a liquid with a density of 57lbs/ft$^3$.
	How much work does it take to fill the remaining 2ft of the tank with liquid, assuming you only have to pump the liquid to the current liquid level in the tank?
\end{example}
\begin{answer}
	Let $V$ be the volume of the tank and $x$ the height of the liquid.
	Imagine we pump in some liquid that changes the height of the liquid in the tank by $\Delta x$.
	Then
	\begin{align*}
		\Delta V &= \pi r^2 \Delta x \\
		\d{V} &= \pi r^2 \d{x}.
	\end{align*}

	Since the height of the tank is 10ft and the base radius 5ft, the radius of the liquid level will always be half the liquid depth.
	\begin{align*}
		r &= x/2 \\
		\d{V} &= \pi (x/2)^2 \d{x}.
	\end{align*}

	Since weight in pounds is already a unit of force, we can multiply $\d{V}$ by the density of the liquid to get $F$.
	\begin{equation*}
		F = 57\pi(x/2)^2 \d{x}.
	\end{equation*}

	The displacement of the liquid is the current height of the liquid $x$, getting us $\d{W}$.
	\begin{align*}
		\d{W} &= 57\pi x(x/2)^2 \d{x} \\
		W &= \int_{8}^{10}{57\pi x(x/2)^2 \d{x}} \\
		&= 21033\pi \text{ft lbs}.
	\end{align*}
\end{answer}


\section*{Annex A: Wavelet transform  using the Fourier Transform}
\addcontentsline{toc}{section}{Appendix A: Wavelet transform  using the Fourier Transform}

We start with the set of scalar products $c_0(k)=<f(x),\phi(x-k)>$. If
$\phi(x)$ has a cut-off frequency $\nu_c\le {1\over 2}$
(\cite{starck:sta94_3,starck:sta94_4,starck:book98}),
the data are
correctly sampled. The data at resolution $j=1$ are:
\begin{eqnarray}
c_1(k)=<f(x),\frac{1}{2}\phi(\frac{x}{2}-k)>
\end{eqnarray}
and we can  compute the set $c_{1}(k)$ from $c_0(k)$ with a discrete
filter $\hat h(\nu)$:
\begin{eqnarray}
\hat h(\nu)= \left\{
  \begin{array}{ll}
  {\hat{\phi}(2\nu)\over \hat{\phi}(\nu)} & \mbox{if } \mid \nu \mid < \nu_c \\
0 & \mbox{if } \nu_c  \leq \mid \nu \mid < {1\over 2}
  \end{array}
  \right.
\end{eqnarray}
and
\begin{eqnarray}
\forall \nu, \forall n \mbox{    } & \hat h(\nu + n) = \hat h(\nu)
\end{eqnarray}
where $n$ is an integer.
So:
\begin{eqnarray}
\hat{c}_{j+1}(\nu)=\hat{c}_{j}(\nu)\hat{h}(2^{j}\nu)
\end{eqnarray}
The cut-off frequency is reduced by a factor $2$ at each step, allowing  a
reduction of the number of samples by this factor.

The wavelet coefficients at scale $j+1$ are:
\begin{eqnarray}
w_{j+1}(k)=<f(x),2^{-(j+1)}\psi(2^{-(j+1)}x-k)>
\end{eqnarray}
and they can be computed directly from $c_j(k)$ by:
\begin{eqnarray}
\hat{w}_{j+1}(\nu)=\hat{c}_{j}(\nu)\hat g(2^{j}\nu)
\end{eqnarray}
where $g$ is the following  discrete filter:
\begin{eqnarray}
\hat g(\nu)= \left\{
  \begin{array}{ll}
  {\hat{\psi}(2\nu)\over \hat{\phi}(\nu)} & \mbox{if } \mid \nu \mid < \nu_c \\
1 & \mbox{if } \nu_c  \leq \mid \nu \mid < {1\over 2}
  \end{array}
  \right.
\end{eqnarray}
and
\begin{eqnarray}
\forall \nu, \forall n \mbox{    } & \hat g(\nu + n) = \hat g(\nu)
\end{eqnarray}

The frequency band is also reduced by a factor $2$ at each step.
Applying the sampling theorem, we can build a pyramid  of
\index{pyramid}
 $N+{N\over 2}+\ldots+1=2N$ elements.
For an image analysis the number of elements is ${4\over 3}N^2$. The
overdetermination is not very high.

The B-spline functions are compact in direct space. They
correspond to the autoconvolution of a square function. In
Fourier space we have:
\index{Fourier transform}
\begin{eqnarray}
\hat B_l(\nu)=({\sin\pi\nu\over\pi\nu})^{l+1}
\end{eqnarray}
$B_3(x)$ is a set of $4$ polynomials of degree $3$.
We choose the scaling function $\phi(\nu)$ which has a
$B_3(x)$ profile in Fourier space:
\begin{eqnarray}
\hat{\phi}(\nu)={3\over 2}B_3(4\nu)
\end{eqnarray}
In direct space we get:
\begin{eqnarray}
\phi(x)={3\over 8}[{\sin{\pi x\over 4}\over {\pi x\over
4}}]^4
\end{eqnarray}
This function is quite similar to a Gaussian and converges
rapidly to $0$. For 2-dimensions the scaling function is defined by
$\hat \phi(u,v) = {3\over 2}B_3(4r)$, with $r = \sqrt(u^2+v^2)$.
This is an isotropic function.

The wavelet transform algorithm with $n_p$ scales is the following:
\index{wavelet transform}
\begin{enumerate}
\item Start with a $B_3$-spline scaling function and derive $\psi$, $h$ and
$g$ numerically.
\item Compute the corresponding FFT image.
Name the resulting complex array $T_0$.
\item Set $j$ to $0$. Iterate:
\item Multiply  $T_j$ by $\hat g(2^ju,2^jv)$. We get the complex array
$W_{j+1}$. The inverse FFT
gives the wavelet coefficients at scale $2^j$;
\item Multiply  $T_j$ by $\hat h(2^ju,2^jv)$. We get the array
$T_{j+1}$. Its inverse FFT gives the image at scale $2^{j+1}$.
The frequency band is reduced by a factor $2$.
\item Increment $j$.
\item If $j \leq  n_p$, go back to 4.
\item The set $\{w_1, w_2, \dots, w_{n_p}, c_{n_p}\}$ describes the
wavelet transform.
\end{enumerate}
If the wavelet is the difference between two resolutions, i.e.
\begin{eqnarray}
\hat \psi(2\nu) = \hat \phi(\nu) - \hat \phi(2\nu)
\end{eqnarray}
and:
\begin{eqnarray}
\hat g(\nu) = 1 - \hat h(\nu)
\end{eqnarray}
then the wavelet coefficients $\hat w_j(\nu)$ can be computed by
$\hat c_{j-1}(\nu) - \hat c_j(\nu)$.

\subsubsection*{Reconstruction.}
If the wavelet is the difference between two resolutions,
an evident reconstruction for a wavelet transform
${\cal W} = \{w_1,\dots, w_{n_p}, c_{n_p}\}$ is:
\begin{eqnarray}
\hat c_0(\nu) = \hat c_{n_p}(\nu) + \sum_j \hat w_j(\nu)
\end{eqnarray}
But this is a particular case, and other alternative wavelet functions can be
chosen. The reconstruction can be made step-by-step, starting from
the lowest resolution. At each scale, we have the relations:
\begin{eqnarray}
\hat c_{j+1} = \hat h(2^j \nu) \hat c_j(\nu) \\
\hat w_{j+1} = \hat g(2^j \nu) \hat c_j(\nu)
\end{eqnarray}
We look for $c_j$ knowing $c_{j+1}$, $w_{j+1}$, $h$ and $g$.
We restore $\hat c_j(\nu)$ based on a least mean square estimator:
\begin{eqnarray}
\hat p_h(2^j\nu) \mid \hat c_{j+1}(\nu)-\hat h(2^j\nu)\hat c_j(\nu) \mid^2 +
\nonumber \\
\hat p_g(2^j\nu) \mid \hat w_{j+1}(\nu)-\hat g(2^j\nu)\hat c_j(\nu) \mid^2
\end{eqnarray}
is to be minimum. $\hat p_h(\nu)$ and $\hat p_g(\nu)$ are weight
functions which permit a general solution to the
restoration of $\hat c_j(\nu)$. From the derivation of $\hat c_j(\nu)$  we get:
\begin{eqnarray}
\hat{c}_{j}(\nu)=\hat{c}_{j+1}(\nu) \hat{\tilde h}(2^{j}\nu)
                +\hat{w}_{j+1}(\nu) \hat{\tilde g}(2^{j}\nu)
\label{restauration}
\end{eqnarray}
where the conjugate filters have the expression:
\begin{eqnarray}
\hat{\tilde h}(\nu) & = {\hat{p}_h(\nu) \hat{h}^*(\nu)\over \hat{p}_h(\nu)
\mid \hat{h}(\nu)\mid^2 + \hat{p}_g(\nu)\mid \hat{g}(\nu)\mid^2} \label{eqnht} \\
\hat{\tilde g}(\nu) & = {\hat{p}_g(\nu) \hat{g}^*(\nu)\over \hat p_h(\nu)
\mid \hat{h}(\nu)\mid^2 + \hat{p}_g(\nu)\mid \hat{g}(\nu)\mid^2}
\label{eqngt}
\end{eqnarray}

In this analysis, the
Shannon sampling condition is always respected and no aliasing
exists.

The denominator is reduced if we choose:
\[\hat{g}(\nu) = \sqrt{1 - \mid\hat{h}(\nu)\mid^2}\]
This corresponds to the case where the wavelet is the difference between
the square of two resolutions:
\begin{eqnarray}
\mid \hat \psi(2\nu)\mid^2  \ = \ \mid \hat \phi(\nu)\mid^2  - \mid  \hat
\phi(2\nu)\mid^2
\end{eqnarray}

% \begin{figure}[htb]
% \centerline{
% \hbox{
% \psfig{figure=ch1_diff_uv_phi_psi.ps,bbllx=0.5cm,bblly=13.5cm,bburx=20.5cm,bbury=27cm,height=6cm,width=14.cm,clip=}
% }}
% \caption{On the left, the interpolation function $\hat{\phi}$ and, on the
% right, the wavelet  $\hat{\psi}$.}
% \label{fig_diff_uv_phi_psi}
% \end{figure}

% \begin{figure*}[htb]
% \centerline{
% \hbox{
% \psfig{figure=ch1_diff_uv_ht_gt.ps,bbllx=0.5cm,bblly=13.5cm,bburx=20.5cm,bbury=27cm,height=5cm,width=14.5cm,clip=}
% }}
% \caption{On the left, the filter $\hat{\tilde{h}}$, and on the right the
% filter $\hat{\tilde{g}}$.}
% \label{fig_diff_uv_ht_gt}
% \end{figure*}

% In Fig.\ \ref{fig_diff_uv_phi_psi} the chosen scaling function
% derived from a B-spline of degree
% 3, and its resulting wavelet function, are plotted in frequency space.

The reconstruction algorithm is:
\begin{enumerate}
\item Compute  the FFT of the image at the low resolution.
\item Set $j$ to $n_p$. Iterate:
\item Compute the FFT of the wavelet coefficients at scale $j$.
\item Multiply  the wavelet coefficients $\hat{w}_j$ by $\hat{\tilde{g}}$.
\item Multiply   the image at the lower resolution $\hat{c}_j$ by
$\hat{\tilde{h}}$.
\item The inverse Fourier transform of the addition of
$\hat{w}_j\hat{\tilde{g}}$ and $\hat{c}_j\hat{\tilde{h}}$ gives the
image $c_{j-1}$.
\item Set $j = j - 1$ and return to 3.
\end{enumerate}
\index{Fourier transform}

The use of a scaling function with a cut-off frequency
allows a reduction of sampling at each scale, and limits the
computing time and the memory size.


\begin{document}
	\chapter{Integration}
	\section{Reduction Formul\ae}
	Integrating using a reduction formula is in essence repeating integration by parts over and over again.\\

	 We can think of the process of finding a reduction formula for a given integral as a \emph{recursive approach} to integration by parts. By listing all the iterations of $\textstyle\int u\od vx\,dx= uv-\int v\od ux\,dx$, more specifically the $\textstyle\int v\od ux\,dx$ part in terms of $I_n$ where $n$ is the \emph{iterative index}, or the \textit{step number}, if you will.\\

	As expected, finding this recursively valid form is not as direct, and thus, the exponent has to be \textit{split} in such a way that trigonometric identities can be used.
	\begin{example}
		If $I_n = \int \cos^n x \, dx$ show that $I_n = \frac{1}{n} \sin\cos^{n-1}x + \frac{n-1}{n}\cdot I_{n-2}$. Hence find $\int \cos^5x\, dx$.
	\end{example}

	\begin{align*}
		I_n                  & = \int \cos^n x \, dx                                                       \\
		& = \int \cos x \cdot \cos^{n-1}x\, dx                                        \\
		%&\quad \text{Integrating by parts: }\\
		\therefore \quad I_n & = \cos^{n-1}x\sin x + (n-1)\int\cos^{n-2}x\sin^2x\,dx                       \\
		& = \cos^{n-1}x\sin x + (n-1)\int\cos^{n-2}x(1-\cos^2x)\,dx                   \\
		& = \cos^{n-1}x\sin x + (n-1)\int\cos^{n-2}x \, dx \,-\, (n-1)\int\cos^nx\,dx \\
		& = \cos^{n-1}x\sin x + (n-1)I_{n-2} - (n-1)I_n                               \\
		I_n + (n-1)I_n       & = \cos^{n-1}x\sin x + (n-1)I_{n-2}                                          \\
		\implies  nI_n       & = \cos^{n-1}x\sin x + (n-1)I_{n-2}                                          \\
		\implies I_n         & = \frac1n\cos^{n-1}x\sin x + \left(\frac{n-1}n\right)I_{n-2}                \\
	\end{align*}

	\begin{align*}
		\int \cos^5 x \, dx   =\,      & I_5                                                                                \\
		& I_5 = \frac{1}{5} \cos^4x\sin x + \frac45I_3                                                                        \\
		& I_3 = \frac{1}{5} \cos^4x\sin x + \frac{4}{5}I_1                                                                    \\
		& I_1 = \int \cos x \, dx = \sin x + k                                                                                \\
		\therefore \quad \int \cos^5 x & = \frac{1}{5}\cos^4x \sin x + \frac{4}{5}\left(\frac{1}{3}\cos^2x\sin x + \frac{2}{3}\left(\sin x + k\right)\right) \\
		& =\frac{1}{5}\cos^2x\cdot\sin x + \frac4{15} \cos^2 x \cdot \sin x + \frac{8}{15}\sin x + c   \qed
	\end{align*}
		\hrulefill
	\begin{example}
		If $I_n = \int \tan^n\theta \, d\theta$, find a reduction formula for $I_n$ and use it to evaluate $\int_0^{\frac\pi4} \tan^6\theta\,d\theta$.
	\end{example}

	\begin{equation*}
		\begin{split}
			I_n &= \int \tan^n\theta \, d\theta\\
			&= \int \tan^2\theta \tan^{n-2}\theta\, d\theta\\
			&= \int (\sec^2\theta - 1) \tan^{n-2}\theta\, d\theta\\
			&= \int \sec^2\theta\tan^{n-2}\theta\,d\theta - \int \underbrace{\tan^{n-2}\theta\,d\theta}\\
			&= \frac{\tan^{n-1}\theta}{n-1} - \underbrace{I_{n-2}}
		\end{split}
		\qquad\qquad\qquad
		\begin{split}
			\int_0^{\frac\pi4}\tan^6\theta\,d\theta &= I_6\bigg|_0^{\frac\pi4}\\
			I_6 &= \frac{tan^5\theta}{5} - I_4\\
			I_4 &= \frac{\tan^3\theta}{3} - I_2\\
			I_2 &= \tan\theta - I_0\\
			I_0 &= \int 1 \, d\theta = \theta + k
		\end{split}
	\end{equation*}
	\begin{align*}
		\therefore \int_0^\frac\pi4  \tan^6\theta \, d\theta & =  \frac{\tan^6\theta}5 - \frac{\tan^3\theta}{3} + \tan\theta - \theta\bigg|_0^\frac\pi4 \\
		& = \frac15 - \frac13 + 1 - \frac{\pi}{4}                                                  \\
		& = \frac{13}{15} - \frac{\pi}{4}   \qed
	\end{align*}
		\hrulefill\newpage

	\begin{example}
		Establish a reduction formula that could be used to find $\int x^ne^x \, dx$ and use it to find $\int x^4e^4$.
	\end{example}

	\begin{equation*}
		\begin{split}
			\text{Let } I_n &= \int x^ne^x \, dx \\
			\text{Let } u 	&= x^n    \qquad   \od{v}{x} = e^x \\
			\od{u}{x}     	&= nx^{n-1} \qquad  v = e^x         \\
			\therefore \quad I_n	&= x^ne^x - n \int x^{n-1}e^x \, dx\qquad\\
			&= x^ne^x - n\, I_{n-1}
		\end{split}
		\begin{split}
			&\int x^4e^x = I_4\\
			&I_4 = x^4e^x - 4I_3\\
			&I_3 = x^3e^x - 3I_2\\
			&I_2 = x^2e^x - 2I_1\\
			&I_1 = xe^x - I_0\\
			&I_0 = e^x + k
		\end{split}
	\end{equation*}
	\begin{align*}
		\therefore \quad I_4 & = x^4e^x -4(x^3e^x - 3(x^2e^x - 2(xe^x - e^x + k))) \\
		& = x^4e^x -4x^3e^x + 12x^2e^x - 24xe^x + 24e^x + c   \qed
	\end{align*}
		\hrulefill
	\begin{example}
		Establish a reduction formula which can be used to evaluate $\int x^n \sin x \, dx$.
	\end{example}

	\begin{align*}
		\text{Let } I_n &= \int x^n \cdot \sin x\\
						&= -x^n\cos x + \int nx^{n-1}\cos x\,dx\\
						&= -x^n\cos x + n\left(x^{n-1}\sin x - \int (n-1)x^{n-2}\sin x\,dx\right)\\
						&= -x^n\cos x + n\left(x^{n-1}\sin x - (n-1) \int x^{-2} \cdot x^n\sin x\,dx\right)\\
						&= -x^n\cos x + n\left(x^{n-1}\sin x - (n-1) \underbrace{x^{n-2}\sin x\,dx}\right)\\
						\therefore \quad I_n &= -x^n\cos x + n\left(x^{n-1}\sin x - (n-1)I_{n-2}\right)\\
						&= -x^n\cos x + nx^{n-1}\sin x - n(n-1)I_{n-2} \qed
	\end{align*}
	\hrulefill\newpage

	\begin{example}
		Establish a reduction formula to find $\int \csc^nx \, dx$. Hence find $\int csc^5x \, dx$
	\end{example}

	\begin{align*}
		\text{Let } I_n &= \int \csc^nx \, dx\\
		&= \int \csc^2x \cdot \csc^{n-2}x \, dx\\
		\text{Let } u & = \csc^{x-2} x              \qquad \od{v}{x} = \csc^2x \, dx \\
		\od{u}{x}     & = -(n-2)\csc^{n-2}\cot x  v \qquad = -\cot x                 \\
		\therefore \int \csc^nx \, dx &= -\cot x \cdot \csc^{n-2}x  - (n-2)\int \csc^{n-2}x\cot^2x \, dx\\
		I_n &= 	-\cot x \cdot \csc^{n-2}x - (n-2)\int \csc^{n-2}x\left(\csc^2x - 1\right) \, dx\\
		&= 		-\cot x \cdot \csc^{n-2}x - (n-2)\int \csc^{n}x\,dx + (n-2)\int\csc^{n-2}xdx\\
		&=-\cot x \cdot \csc^{n-2}x  - (n-2)\, I_n + (n-2)I_{n-2}\\
		I_n + nI_n -2I_n &= -\cot x\cdot \csc^{n-2}x  + (n-2)I_{n-2}\\
		(n-1)I_n &= -\cot x\cdot \csc^{n-2}x  + (n-2)I_{n-2}\\
		I_n &= \frac{-1}{n-1}-\cot x\cdot \csc^{n-2}x + \frac{n-2}{n-1}I_{n-2}\\
		&= \left(1-\frac{1}{n-1}\right)I_{n-2}-\frac{\cot x\csc^{n-2}x}{n-1}\qed
	\end{align*}
	\begin{example}
		Show that if $I_n - \int_0^\pi x^n\sin x\,dx$, then $I_n = \pi^n - n(n-1)\,I_n-1$. Hence evaluate $\int_0^\pi\sin x\,dx$
	\end{example}
	\begin{align*}
		I_n &=\left[-x^n\cos x\right]_0^\pi + n\int_0^\pi x^{n-1} \cos x \, dx\\
		&=\pi^n + n\int_0^\pi x^{n-1} \cos x \, dx\\
		&= \pi^n \int_0^\pi \\
		\text {Let } u &=
	\end{align*}
	\begin{example}
		Show that, if $I_n = \int_0^1 x^n e^{x^3} \, dx$, then $I_n =\frac{e}{3} - \frac{n-2}{3} \cdot I_{n-3}$
	\end{example}

	\begin{align*}
		I_n &= \int_0^1 x^n e^{x^3} \, dx\\
		&= \int_0^1 x^{n-2}x^2e^{x^3} \, dx\\
		\therefore \quad I_n &= \left[\frac{x^{n-2}e^{x^3}}3\right]_0^1 - \frac{n-2}3 \int_0^1 x^{n-3}{e^{x^3}} \, dx\\
		&= \frac{e}3 - \frac{n-2}3 \cdot I_{n-3}
	\end{align*}

	\begin{example}
		Show that, if $I_n= \int_0^1 x^n(1+x^5)^4 \, dx$, then $I_n = \frac1{n+21} \left[32-(n-4)\cdot I_{n-5}\right]$
	\end{example}

	\begin{align*}
		I_n                 & = \int_0^1 x^n(1+x^5)^4 \, dx                                                                           \\
		& = x^{n-4}x^4(1+x^5)^4\,dx                                                                               \\
		& =\left[x^{n-4} \frac{(1+x^5)^5}{25}\right]_0^1 - \frac{n-4}{25} \int x^{n-5}(1+x^5)^5\,dx               \\
		& = \frac{32}{25}  -\frac{n-4}{25} \int_0^1x^n-5(1+x^5)(1+x^5)^4\,dx                                      \\
		& = \frac{32}{25} - \frac{n-5}{25}\int_0^1x^{n-5}(1+x^5)^4\, dx - \frac{n-4}{25}\int_0^1x^n(1+x^5)^4\, dx \\
		& = \frac{32}{25} - \left(\frac{n-4}{25}\right)I_{n-5} - \left(\frac{n-4}{25}\right)I_n                   \\
		25I_n               & = 32 - (n-4)I_{n-5} - \left(\frac{n-4}{25}\right)\,I_n                                                  \\
		25I_n + nI_n - 4I_n & = 32-(n-4)\,I_{n-5}\\
		(n+21)I_n &= 32-(n-4)I_{n-5}\\
		I_n &= \frac{1}{n+21}\left(32-(n-4)I_{n-5}\right)
	\end{align*}
	\hrulefill
	\newpage
	\begin{example}
		Given $I_n = \int_0^1 (1+x^2)^{-n}\,dx$, show that $2n\,I_{n+1} = 2^{-n} + (2n-1)\,I_n$
	\end{example}
	\begin{align*}
		I_n                   & = \int_0^1 (1+x^2)^{-n}\,dx                                             \\
		& = \int_0^1  (1+x^2)^{-n}\cdot 1 \,dx                                    \\
		\therefore \quad  I_n & = -2nx^2(1+x^2)^{-(n+1)}\bigg|_0^1 + 2n \int_0^1x^2(1+x^2)^{-n-1}\,dx   \\
						   	  & =2^{-n} + 2n\int_0^1(x^2+1-1)(1+x^2)^{-(n+1)}\,dx                       \\
						      & =2^{-n} + 2n \int_0^1(1+x^2)^{-2}\,dx - 2n\int_0^1 (1+x^2)^{-(n+1)}\,dx \\
							  & = 2^{-n} + 2n\,I_n - 2n\,I_{n+1}                                        \\
           	      2n\,I_{n+1} & = 2^{-n} + (2n-1)\,I_n
	\end{align*}
\end{document}


\documentclass{article}
\usepackage{latexsym}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}

\setlength{\bigskipamount}{8em}
\setlength{\parindent}{3em}
\setlength{\parskip}{1em}

\newtheorem{theorem}{Theorem}
\newtheorem{question}{Q.}
\newtheorem{answer}{A.}

\begin{document}
\author{Dave Neary}
\title{Introduction to Modular Arithmetic}

\maketitle

\section{Modular arithmetic}

Let's start with a question: $3.141592653589793238462643383279$ is the value of
$\pi$ to 30 decimal places. Is the number 3141592653589793238462643383279 the
square of an integer?

Rather than attempting to answer ths question directly, let's do some exploration
of squares of integers, to see if we can find some common characteristics.

\begin{table}[htb]
\begin{tabular}{|c|c|c|}
\hline
	$n$ & $n^2$ & rem($\frac{n^2}{4}$) \\
\hline
	1 &  1 & 1 \\
	2 &  4 & 0 \\
	3 &  9 & 1 \\
	4 & 16 & 0 \\
	5 & 25 & 1 \\
	6 & 36 & 0 \\
	7 & 49 & 1 \\
\hline
\end{tabular}
\end{table}

Interestingly, it seems like every even square has a remainder of 0 when we divide
by 4, and every odd square has a remainder of 1 (and in fact, appears to be 1 more than a multiple of 8).
We can prove that this is the case in general quite easily:

\[ n = 2k \implies n^2 = 4k^2 \]
\[ n = 2k+1 \implies n^2 = 4k^2 + 4k + 1 = 4(k^2+k) + 1 \]

We can now return to our original question - a simple initial test for whether a
number is a square of an integer is to check its remainder when divided by 4. And
we only need to look at the last two digits to check because $100n+m =4(25n)+m$ -
so we can ignore everything before the last 2 digits. In our example,
$79 = 4\times 19 + 3$, so the number at the start of this section is \textbf{not}
the square of an integer.

\subsection{Working with remainders}

This example gives a glimpse of something called modular arithmetic - sometimes,
we can draw conclusions related to a problem by looking only at the remainders
when divided by a number. In terms of notation, we say that $a \equiv b \pmod{n}$
when $a = m\cdot n+b$ for some integer $m$. In the example above, we can write:
$a^2 \equiv 0 \text{ or } 1 \pmod{4} \text{ for all } a\in \mathbb{Z}$.

There are a few operations that hold for all numbers $\pmod{n}$:

Remainders are additive and multiplicative:
\begin{eqnarray*}
	(a\pmod{n}) + (b\pmod{n}) & = & (a+b)\pmod{n} \\
	(a\pmod{n}) \cdot (b\pmod{n}) & = & (a\cdot b) \pmod{n}
\end{eqnarray*}

So we can tell that if $a=76$ and $b=42$, when we multiply them together, the
remainder when we divide the result by 5 will be $76 \pmod{5} \times 42 \pmod{5}
= 1\times 2 \pmod{5}$. We say that $a \equiv b \pmod{n}$ ($a$ is congruent to $b$
mod $n$) if $n|(a-b)$ - that is, if we subtract one number from another, and they
have the same remainder when divided by $n$, then their difference is a multiple
of $n$.

Just the basics of modular arithmetic allow us to address a whole range of problems already.

\begin{question}Prove that $6\cdot 4^n - 6$ is divsible by 9 for all $n$.\end{question}
\begin{proof}
	Let's look at the values of $6 \cdot 4^n \pmod{9}$ for different
values of $n$.

\begin{table}[htb]
\begin{tabular}{|c|c|c|}
\hline
	$n$ & $4^n \pmod{9}$ & $6\cdot 4^n \pmod{n}$\\
\hline
	0 & 1 & 6 \\
	1 & 4 & 6 \\
	2 & 7 & 6 \\
	3 & 1 & 6 \\
\hline
\end{tabular}
\end{table}

Clearly, $4^n$ cycles through the values 1, 4, 7 for all $n$, and each of these multiplied
by $6$ gives a remainder of 6 when divided by 9. Another way of putting this is that
$4^n \equiv 1 +3k \pmod{9}$ for $n\equiv k \pmod{3}$, and since $6\cdot 3 = 18 \equiv 0 \pmod{9}$
$6\times (1+3k) \equiv 6 \pmod{9}$ for all $n$.
\end{proof}

\begin{question}What are the last two digits base 10 of $6^{19}$?\end{question}

\begin{answer} This is an intimidating looking question, but modular arithmetic offers us a
powerful tool to simplify things. $6^2 = 36 \pmod{100}$, $6^3 = 216 \equiv 16 \pmod{100}$,
$6^4 \equiv 96 \pmod{100} \equiv -4 \pmod{100}$, $(6^4)^2 = 6^8 \equiv (-4^2) = 16 \pmod{100}$
So we have $6^8 \equiv 6^3 \pmod{100}$ But now we have:
\begin{eqnarray*}
	6^{19} & =&  (6^8)^2\cdot 6^3 \\
               & \equiv & (6^3)^3 \pmod{100} \\
	       & \equiv & 6^{9} \pmod{100} \\
	       & \equiv & 6^8\cdot 6 \pmod{100} \\
	       & \equiv & 6^4 \pmod{100} \\
	       & \equiv & 96 \pmod{100}
\end{eqnarray*}
\end{answer}

You can use modular arithmetic to prove common divisibility tricks.

\begin{question}Prove that 9 divides a number if and only if it divides the sum of its digits.\end{question}
\vspace*{\bigskipamount}

\begin{question}Prove that a number is divisible by 8 if its last 3 digits are divisible by 8.\end{question}
\vspace*{\bigskipamount}

\begin{question}Prove that a number is divisible by 11 if the sum of its even digits minus the
sum of its odd digits is divisible by 11.\end{question}
\vspace*{\bigskipamount}

\subsection{Fermat's Little Theorem}

Fermat made an interesting observation when working with remainders modulo a prime number. If you
repeatedly multiplied any number not divisible by a prime number $p$, and took just the remainder
$\mod (p)$, that $p$ would eventually divide $a^k - 1$ for some number, and that $k$ always divided
evenly into $p-1$. Equivalently, for any number not divisible by $p$, $a^{p-1} \equiv 1 \pmod{p}$.
More generally, $a^p \equiv a \pmod{p}$ for all $a\in \mathbb{Z}$

Let's work through an example to see how it works with $p=7$:

\begin{table}[htb]
\begin{tabular}{|c|c|c|c|c|c|c| }
\hline
	$a$ & $a^2$ & $a^3$ & $a^4$ & $a^5$ & $a^6$ \\
\hline
	1 & 1 & 1 & 1 & 1 & 1 \\
	2 & 4 & 1 & 2 & 4 & 1 \\
	3 & 2 & 6 & 4 & 5 & 1 \\
	4 & 2 & 1 & 4 & 2 & 1 \\
	5 & 4 & 6 & 2 & 3 & 1 \\
	6 & 1 & 6 & 1 & 6 & 1 \\
\hline
\end{tabular}
\end{table}

To show how we get the table entries, let's look at row 5 for example:
\begin{eqnarray*}
	5^2 &=& 25 \equiv 4 \pmod{7}\\
	5^3 &=& 5 \times 4 \equiv 6 \pmod{7} \\
	5^4 &=& 5 \times 6 \equiv 2 \pmod{7} \\
	5^5 &=& 5 \times 2 \equiv 3 \pmod{7} \\
	5^6 &=& 5 \times 3 \equiv 1 \pmod{7}
\end{eqnarray*}

If you look at how many times you need to multiply a number by itself to get back to 1, you can
see that for 1, the cycle length is 1, for $6\equiv-1 \pmod{7}$ it is 2, for 2 and 4 it is 3,
and for 3 and 5, it is 6. In all cases the cycle length divides $p-1$.

In many equations where we want to prove that solutions are or are not possible for equations
including prime numbers, we can do so using Fermat's Little Theorem and modular arithmetic.

\begin{question}
What is the value of $2001^{2002} \pmod{2003}$?
\end{question}

\begin{answer}
	There are often questions like this with year numbers in the question - it is a good idea to
	know if the current year (or one more than the current year) has some interesting property.
	In this case, let's check whether 2003 is a prime. To do so, we need to check whether it
	is divisible by any prime number less than $\sqrt{2003} \approx 44$.

	We can quickly see with basic divisibility tricks that it is not divisible by 2 (last
	digit is odd), 3 (sum of digits is not a multiple of 3), 5 (last digit is not 0 or 5), 11
	(summing the alternating digits does not give the same number mod 11). So we only have to
	check divisibility by 7, 13, 17, 19, 23, 29, 31, 37, 41, and 43 to verify if 2003 is prime.
	I will leave that as an excercise.

	If 2003 is prime, then by Fermat's Little Theorem, $a^2002 \equiv 1 \pmod{2003}$.
\end{answer}

\begin{question}
	What is the remainder when you divide $4^{87}$ by 17?
\end{question}
\begin{answer}
	We know by Fermat's Little Theorem that $4^{16} \equiv 1 \pmod{17}$.
	Then:
	\begin{eqnarray*}
		4^{87} &=& (4^{16})^5 \times 4^7 \pmod{17} \\
		&=& (4^2)^3 \times 4 \pmod{17} \\
		&=& (-1)^3 \times 4 \pmod{17} \\
		&=& -4 \pmod{17} = 13 \pmod{17}
	\end{eqnarray*}
\end{answer}

\begin{question}Find $6^{1000} \pmod{23}$. (AOPS)\end{question}
\vspace*{\bigskipamount}

\begin{question}What are the last two digits of $7^{9999}$? (MATHCOUNTS 1986)\end{question}
\vspace*{\bigskipamount}

\end{document}