\chapter{Analytic Approach}
	\section{Basic concepts}
		Here we'll define some basic concepts about norms and metrics.
		\begin{defn}
			Let $X$ be a non-empty set, a function $d\colon  X \times X \to \R_{\geq 0}$ is a \textit{metric} if, for every $x, y, z \in X$, we have:
			\begin{enumerate}
				\item $d(x, y) = 0 \iff x = y$;
				\item $d(x, y) = d(y, x)$;
				\item $d(x, y) \leq d(x, z) + d(z, y)$.
			\end{enumerate}
		\end{defn}
		\begin{defn}
			\label{defn:field-norm}
			Let $F$ be a field, a function $\norm{\ }\colon F \to \R_{\geq 0}$ is a \textit{field norm}\footnote{Although usually the term ``field norm'' has a different definition in field theory, we choose to use this terminology, to distinguish between norms on fields and norms on vectorial spaces.}(or an \textit{absolute value}) if, for every $x, y \in F$, we have:
			\begin{enumerate}
				\item $\norm{x} = 0 \iff x = 0$;
				\item $\norm{x \cdot y} = \norm{x} \cdot \norm{y}$;
				\item $\norm{x + y} \leq \norm{x} + \norm{y}$.
			\end{enumerate}
		\end{defn}
		\begin{defn}
			Let $V$ be a vector space over the field $F$, which has its norm $\norm{\ }_F$. A function $\norm{\ }\colon V \to \R_{\geq 0}$ is a \textit{norm} if, for every $v, w \in V, \alpha \in F$, we have:
			\begin{enumerate}
				\item $\norm{v} = 0 \iff v = 0$;
				\item $\norm{\alpha \cdot v} = \norm{\alpha}_F \cdot \norm{v}$;
				\item $\norm{v + w} \leq \norm{v} + \norm{w}$.
			\end{enumerate}
		\end{defn}
		Beginning from a (field) norm $\norm{\ }$ there's a natural metric defined as $d(x, y) = \norm{x - y}$.
	\section{Metrics on $\Q$}
		The metric we normally equip $\Q$ with is the euclidean one, which comes from the usual absolute value $\abs{\ }$ (denoted also by $\abs{\ }_\infty$).
		\begin{defn}
			Let $p$ a fixed prime. We can define a function $\ord\colon \Z \to \N \cup \{+\infty \}$ as follows:
			\begin{equation*}
				\ord a :=
				\begin{cases*}
					+\infty, & \text{if $a = 0$;} \\
					n_a, & \text{otherwise;}
				\end{cases*}
			\end{equation*}
			where $n_a \in \N$ is such that $p^{n_a} | a$ and $p^{n_a + 1} \nmid a$. It's easy to prove that $\ord ab = \ord a + \ord b$ (using the usual convention $\infty + n = n + \infty =+\infty$).
		\end{defn}
		We can extend this function to $\Q$:
		\begin{equation*}
			\ord \left( \frac{a}{b} \right) :=
			\begin{cases*}
				+\infty, & \text{if $\frac{a}{b} = 0$;} \\
				\ord a - \ord b, & \text{otherwise;}
			\end{cases*}.
		\end{equation*}
		This is of course well defined: $\ord \left( \tfrac{ac}{bc} \right) = \ord ac - \ord bc = \ord a - \ord b = \ord \left( \tfrac{a}{b} \right)$.
		\begin{prop}
	 		$\emph{ord}_p\colon  \Q \to \Z \cup \{+\infty\}$ is a discrete valuation.
		\end{prop}
		\begin{proof}
			We have to prove the following properties:
			\begin{itemize}
				\item $\ord x = +\infty \iff x = 0$;
				\item $\ord xy = \ord x + \ord y$;
				\item $\ord (x + y) \geq \min \{\ord x, \ord y \}$.
			\end{itemize}
			The first two properties are quite easy, to see why the third one is true it's sufficient to write
			\[
				x = \frac{a}{b} = p ^ {\ord x} \cdot \frac{a'}{b'}, \qquad y = \frac{c}{d} = p^{\ord y} \cdot \frac{c'}{d'}
			\]
			with $a', b', c', d'$ coprime with $p$. Then
			\begin{equation*}
				x + y = p^{\min \{\ord x,\,\ord y \}} \cdot q \qquad (q \in \Q).
			\end{equation*}
			Applying property $2.$ from \cref{defn:field-norm} we obtain
			\begin{gather*}
				\ord(x + y) = \ord \left(p^{\min \{\ord x,\,\ord y \}} \cdot q\right) \geq \min \{\ord x, \ord y \}.\qedhere
			\end{gather*}
		\end{proof}

		Using these functions we can define a field norm $\pabs{\ }\colon \Q \to \Q$ as follows:
		\begin{equation*}
			\pabs{x} =
			\begin{cases*}
				p ^ {-\ord x}, & if $x \neq 0$; \\
				0, & otherwise;
			\end{cases*}.
		\end{equation*}
		\begin{prop}
			\label{prop:padic-nonarchimedean}
			$\pabs{\ }$ is a field norm on $\Q$.
		\end{prop}
		\begin{proof}
			Property $1.$ is obvious. \newline
			To prove $2.$, given $x, y \in \Q^{\times}$ we know that $\ord xy = \ord x + \ord y$ so
			\begin{gather*}
				\pabs{xy} = p^{- \ord xy} = p^{-\ord x - \ord y} = p^{-\ord x} \cdot p^{-\ord y} = \pabs{x} \cdot \pabs{y}.
			\end{gather*}
			To prove $3.$ let $x, y \in \Q^{\times}$; $\ord(x + y) \geq \min \{\ord x, \ord y\}$ so
			\begin{gather*}
				\pabs{x + y} = p^{-\ord(x + y)} \leq p^{- \min \{\ord x,\, \ord y \} } = p ^ {\max \{-\ord x,\,-\ord y\} } \\
				= \max \left\{p ^ {-\ord x}, p^{-\ord y} \right\} = \max \left\{\pabs{x}, \pabs{y} \right\} \leq \pabs{x} + \pabs{y}.
			\end{gather*}
			We actually proved a stronger inequality than $3.$, which is one of the key ingredients of \padic analysis.
		\end{proof}
		\begin{defn}
			A norm on $X$ is called \textit{non-Archimedean} if $\norm{x + y} \leq \max \left\{\norm{x}, \norm{y} \right\}$ holds for every $x, y \in X$.
		\end{defn}
		If $\norm{\ }$ is a non-Archimedean norm on $X$, it's immediate to see that
		\begin{gather*}
			\norm{n\cdot x} \leq \norm{x} \text{ for every $n \in \N, x \in X$ }
		\end{gather*}
		which explains the name. We have already proved that $\pabs{\ }$ is a non-Archimedean norm on $\Q$ in \cref{prop:padic-nonarchimedean}.
		\begin{prop}
			If $\norm{\ }$ is a non-Archimedean norm on $X$ then
			\[
				\norm{x} \neq \norm{y} \implies \norm{x + y} = \max \left\{\norm{x}, \norm{y} \right\}.
			\]
		\end{prop}
		\begin{proof}
			We can assume that $\norm{x} < \norm{y}$. Then
			\begin{gather*}
				\norm{y} = \norm{(x + y) - x} \leq \max \left\{\norm{x+y}, \norm{x}\right\} \leq \norm{y}
			\end{gather*}
			but since $\norm{x} < \norm{y}$ we must have $\norm{x+y} = \norm{y}$.
		\end{proof}
		\begin{defn}
			If $(X, d)$ is a metric space, a sequence $(a_n)_{n \in \N}$ is a \textit{Cauchy sequence} if $\forall \varepsilon > 0$ $\exists n_0 \in \N$ such that $n, m > n_0 \implies d(a_n, a_m) < \varepsilon$.
		\end{defn}
		\begin{defn}
			Two metrics $d_1, d_2$ on $X$ are \textit{equivalent} if every Cauchy sequence for $d_1$ is Cauchy for $d_2$ and vice-versa. Two norms are equivalent if they induce equivalent metrics.
		\end{defn}
		Now we present a technical lemma we're going to need.
		\begin{lemma}
			\label{lemma:equivalent-norm}
			If $\alpha \in (0, 1]$ the function on $\Q$ defined by $x \mapsto \abs{x}^\alpha$ is a norm equivalent to $\abs{\ }_\infty$.
		\end{lemma}
		\begin{proof}
			First of all we show $\abs{\ }^\alpha$ is actually a norm; property $1.$ and $2.$ are easily verified. To prove $3.$ we have to show that $\abs{x + y}^\alpha \leq \abs{x}^\alpha + \abs{y}^\alpha$ for every $x, y \in \Q^{\times}$. We can assume $0 < x < y$ and, dividing both sides by $\abs{y}^\alpha$, we just need to prove $(1 + t)^\alpha \leq 1 + t^\alpha$ for $t \in [0, 1]$. This easily follows studying the first derivative of $[0, 1] \ni t \mapsto 1 + t^\alpha - (1 + t)^\alpha$ (always non negative if $0 \leq \alpha \leq 1$).\newline
			The equivalence of the two norms is easy to see if we use the above definition: let $(a_n)_n$ be Cauchy for $\abs{\ }$; fixed $\varepsilon > 0$ we can find $n_0 \in \N$ such that $n, m > n_0 \implies \abs{a_n - a_m} < \varepsilon ^ \frac{1}{\alpha}$ i.e. $\abs{a_n - a_m}^\alpha < \varepsilon$ $\forall n, m > n_0$ so $(a_n)_n$ is also Cauchy for $\abs{\ }^\alpha$ (and vice-versa).
		\end{proof}
		Generalizing a little bit the previous lemma we can prove that if $\norm{\ }_1$ and $\norm{\ }_2$ are two field norms on $F$ which satisfy $\norm{x}_1 = \norm{x}_2^\alpha$ $\forall x \in F$ for a fixed $\alpha > 0$ then they're equivalent. For example, instead of defining $\pabs{\ }$ using $p ^ {- \ord a}$, we could have used $\rho \in (0, 1)$ in place of $1/p$ and we would have obtained an equivalent norm because $p^ {-\ord a} = \left(\rho ^ {\ord a}\right)^{-\log_\rho p}$.
		\begin{defn}
			The norm $\norm{\ }$ such that $\norm{x} = 1 - \delta_0^x$ is called \textit{trivial}.
		\end{defn}
		Finally we can prove the main theorem of this section.
		\begin{thm}[Ostrowski]
			Every non-trivial norm $\norm{\ }$ on $\Q$ is equivalent to $\pabs{\ }$ for some prime $p \in \N$ or for $p = \infty$.
		\end{thm}
		\begin{proof}
			We distinguish two cases.\newline
			\textit{Case} (1). There exists a positive integer $n$ such that $\norm{n} > 1$. Let $n_0$ be the minimum among those (for every field norm $\norm{\pm 1} = 1$ so $n_0 > 1$). Since $\norm{n_0} > 1$ there exists $\alpha = \log_{n_0}\norm{n_0} > 0$ such that $\norm{n_0} = n_0^\alpha$. Now if $n \in \N^{\times}$ then, using base $n_0$, we can write
			\begin{equation*}
				n = a_0 + a_1n + \dots + a_sn_0^s, \qquad a_i \in \{0, 1, \dots, n_0-1\}, a_s \neq 0.
			\end{equation*}
			Then, since norms are subadditive and multiplicative
			\begin{gather*}
				\norm{n} \leq \norm{a_0} + \norm{a_1n_0} + \dots + \norm{a_sn_0^s} = \\
				= \norm{a_0} + \norm{a_1}n_0^\alpha + \dots + \norm{a_s}n_0^{s\alpha}.
			\end{gather*}
			Being $n_0$ the minimum positive integer with $\norm{n_0} > 1$ we have $\norm{a_i} \leq 1$ so
			\begin{gather*}
				\norm{n} \leq 1 + n_0^\alpha + \dots + n_0^{s\alpha} \leq n_0^{s\alpha}(1 + n_0^{-\alpha} + \dots + n_0^{-s\alpha}) \leq
	 			n^\alpha \left[\sum_{i=0}^{\infty} n_0^{-i\alpha} \right].
			\end{gather*}
			The last inequality is true because $n \geq n_0^s$. The series at the right side is a geometric one which converges to a certain $C < +\infty$ (since $0 < \frac{1}{n_0} < 1$). Now we have obtained
			\begin{equation*}
				\norm{n} \leq Cn^\alpha.
			\end{equation*}
			Using $n^N$, for some large $N \in \N$, in place of $N$ in the last inequality, and then extracting $N$th roots, leads us to
			\begin{equation*}
				\norm{n} \leq \sqrt[N]{C}n^\alpha.
			\end{equation*}
			Letting $N \to +\infty$ we get $\norm{n} \leq n^\alpha$ (obviously this is valid for every $n \in \N$). To get the other verse of the inequality, using $n$ written as above, we have $n_0^{s+1} > n \geq n_0^s$. Using reverse triangular inequality and the one we obtained above, we get
			\begin{equation*}
				\norm{n} \geq \norm{n_0^{s+1}} - \norm{n_0^{s+1} - n} \geq n_0^{(s+1)\alpha} - \left(n_0^{s+1} - n\right)^\alpha.
			\end{equation*}
			Since $n > n_0^s$
			\begin{equation*}
				\norm{n} \geq n_0^{(s+1)\alpha} - \left(n_0^{s+1} - n_0^s\right)^\alpha = n_0^{(s+1)\alpha} \left[1 - \left(1 - \frac{1}{n_0} \right) ^ \alpha \right] \geq C'n^\alpha
			\end{equation*}
			with $C' := \left[1 - \left(1 - \frac{1}{n_0} \right) ^ \alpha \right]$ that doesn't depend on $n$. As before, using $n^N$ and taking $N$th roots and letting $N \to +\infty$ gives $\norm{n} \geq n^\alpha$.
			So we proved that $\norm{n} = n^\alpha$ for every $n \in \N$. Using property $2.$ of norms and $\norm{-1} = 1$ we get $\norm{q} = \abs{q}^\alpha$ for every $q \in \Q$. Now, using \cref{lemma:equivalent-norm}, we conclude that $\norm{\ }$ is equivalent to $\abs{\ }_\infty$.\newline
			\textit{Case} (2). For every $n \in \N$, $\norm{n} \leq 1$. Since $\norm{\ }$ is non-trivial by hypothesis we can find the minimum $\N \ni n_0 > 1$ such that $\norm{n_0} < 1$. Easily $n_0$ is a prime number: if not, $n_0 = a \cdot b$ with $1 < a,b < n_0$ and $1 > \norm{n_0} = \norm{ab} = \norm{a}\norm{b}$ so at least one from $\norm{a}$ and $\norm{b}$ must be strictly less than $1$, absurd because $a, b < n_0$ and $n_0$ is the minimum positive integer with this property. \newline
			Let $p = n_0$ and we claim that if $q$ is a different prime from $p$ $\norm{q} = 1$. If this is not true then $\norm{q} < 1$ and we can find some large $N \in \N$ such that $\norm{p^N}, \norm{q^N} < 1/2$. Since $p^N$ and $q^N$ are coprime, from Bézout identity there are $n, m \in \Z$ such that $np^N + mq^N = 1$, but this leads to a contradiction:
			\begin{equation*}
				1 = \norm{1} = \norm{np^N + mq^N} \leq \norm{n}\norm{p^N} + \norm{m}\norm{q^N} \leq \norm{p^N} + \norm{q^N} < \frac{1}{2} + \frac{1}{2} < 1.
			\end{equation*}
			Now, given $n \in N$ we can factorize it in a unique way into prime divisors $n = p_1^{b_1} \cdots p_r^{b_r}$. At most one from the $p_i$-s is equal to $p$ so if, wlog, $p_1 = p$ then $b_1 = \ord n$ and $\norm{p_i} = 1$ if $i>1$ so
			\begin{equation*}
				\norm{n} = \norm{p_1^{b_1} \cdot \dots \cdot p_r^{b_r}} = \norm{p_1}^{b_1}\cdot \dots \cdot \norm{p_r}^{b_r} = \norm{p}^{\ord n}.
			\end{equation*}
			Letting $\rho := \norm{p} \in (0, 1)$ we obtain $\norm{a} = \rho ^ {\ord a}$ for $a \in \N^{\times}$. Using property $2.$ of norms we can show this holds also if $a \in \Q^{\times}$. We conclude that $\norm{\ }$ is equivalent to $\pabs{\ }$, using the general version of \cref{lemma:equivalent-norm}.
		\end{proof}
		The standard topology of $\Q$, induced by the euclidean metric, is very different from the \padic topology, induced by the \padic ultrametric. With the former, $\Z \subset \Q$ is a discrete set while, in \padic environment, $\Z$ isn't discrete: $0$ is an accumulation point, $\lim_{n \to +\infty}p^n = 0$. There are also some interesting algebraic properties, which we haven't in the standard topology, for example the one described in the following lemma.
		\begin{lemma}
			For every $r > 0$ the set $B_{<r}(0) \cap \Z = \{x \in \Z \mid \pabs{x} < r\}$ is an ideal of the ring $\Z$, in the \padic topology.
		\end{lemma}
		\begin{proof}
			It's clear that we can only consider the case $r = p^k$ with $k \in \Z$. If $k \geq 0$ the property is trivial since $\Z \subseteq B_{\leq 1}(0) = \{x \in \Q \mid \pabs{x} \leq 1 \}$. Let's consider $x, y \in \Z \cap B_{< p^k}(0)$, i.e. $\pabs{x}, \pabs{y} < p^k$. We must show that $\pabs{x - y} < p^k$ and that for every $z \in \Z$ we have $z \cdot x \in B_{<p^k}(0) \cap \Z$. For the first property we have
			\begin{gather*}
				\pabs{x - y} \leq \max \left\{\pabs{x}, \pabs{-y} \right\} = \max \left\{\pabs{x}, \pabs{y} \right\} < p^k
			\end{gather*}
			and for the second one, recalling that $\Z \ni z \implies \pabs{z} \leq 1$, we have
			\begin{gather*}
				\pabs{z \cdot x} = \pabs{z} \cdot \pabs{x} \leq \pabs{x} < p^k. \qedhere
			\end{gather*}
		\end{proof}

		These non-Archimedean norms $\pabs{\ }$ have some very strange properties, far from our intuition (which is based on euclidean norms). We'll explore them in detail in \cref{section:ultrametrics}.
	\section{Construction of $\Qp$}
		\label{section:construction-Qp}
		\begin{defn}
			A metric space $(X, d)$ is \textit{complete} if every Cauchy sequence in $X$ converges to some element in $X$.
		\end{defn}
		\begin{defn}
			If $(X, d)$ is a metric space, $(\overline{X}, \overline{d})$ is its completion if it is a complete metric space which contains $X$ as a dense subspace and satisfies this universal property: if $Y$ is a complete metric space and $f\colon X \to Y$ is uniformly continuous then there exists a unique $f'\colon \overline{X} \to Y$ such that $f'$ is uniformly continuous and $f'|_X = f$.
		\end{defn}
		It's clear from the definition that the completion of a space is unique up to isometry.
		\begin{prop}
			\label{prop:Q-not-complete}
			$(\Q, \pabs{\ })$ is not complete.
		\end{prop}
		\begin{proof}
			This proof will heavily rely on the definition of $\Zp$ proposed in \cref{section:Zp}, and on Hensel's lemma (\cref{thm:hensel-lemma}). Obviously we don't need any result depending on this statement to  build $\Zp$ and prove the Hensel's lemma (in other words: this proof does not create any logical loop).\newline
	 		We have to show that there exists a Cauchy sequence in $(\Q, \pabs{\ })$ which has no limit in $\Q$. To do this, we'll use a polynomial $P(X) \in \Z[X] \subset \Zp[X]$ which has no roots in $\Q$ but admits a root in $\Z/p\Z$. We'll then use Hensel's lemma to obtain $\xi \in \Zp$ such that $P(\xi) = 0$. We'll then have a Cauchy sequence in $\Z \subset \Q$ (we can consider truncated sums of $\xi$) which converges to $\xi \notin \Q$. Let's distinguish four cases.
	 		\begin{itemize}
	 			\item $p = 2$: \newline
	 				  Let's consider the polynomial $P(X) = X^3 - 7 \in \Z[X]$: obviously there are no rational roots of $P$ but $x_0 = 1$ is such that $P(x_0) \equiv 0 \mod 2$. We immediately see that $P'(X) = 3X^2$ so $2 \nmid 3 = P'(1)$ and, applying Hensel's lemma, we infer there is a unique $\xi \in \Z_2$ such that $P(\xi) = 0$.
	 			\item $p = 3$: \newline
		 			  Let's consider the polynomial $P(X) = X^2 - 7 \in \Z[X]$: obviously there are no rational roots of $P$ but $x_0 = 1$ is such that $P(x_0) \equiv 0 \mod 3$. We immediately see that $P'(1) = 2 \not\equiv 0 \mod 3$ and, applying Hensel's lemma, we infer there is a unique $\xi \in \Z_3$ such that $P(\xi) = 0$.
	 			\item $p \equiv 1 \mod 4$: \newline
	 				  Let's consider the polynomial $P(X) = X^2 - (p+1) \in \Z[X]$. We observe that $P$ has no rational roots; writing $p+1 = 4k + 2$ we immediately see that $p+1$ is not a perfect square, because $2 \mid p+1$ but $4 \nmid p+1$. Clearly, $p+1$ can't either be a square of some rational number: if it were, then we would have
	 				  \[
	 				      p+1 = \left( \frac{a}{b} \right) ^2 \implies b^2\cdot (p+1) = a^2
	 				  \]
	 				  which is an absurd, since $p+1$ is not a perfect square. So $P$ has no roots in $\Q$, but we easily see that $P(1) \equiv 0 \mod p$ and $P'(1) = 2 \not\equiv 0 \mod p$. Applying Hensel's lemma we find $\xi \in \Zp$ such that $P(\xi) = 0$.
	 			\item $p \equiv 3 \mod 4$: \newline
	 				  Let's consider $\left(\frac{p-1}{2}\right)^2 \equiv 4^{-1} \mod p$ and let $t \in \{0, \dots, p-1\}$ such that $4t \equiv 1 \mod p$. Obviously $0 \neq t$ is a quadratic residue in $\Z/p\Z$; we claim that $\sqrt{t} \notin \Q$. We just need to show that $t$ is not a perfect square (then we can use the same reasoning of the previous point). First of all, with a little abuse of notation, we observe that
	 				  \[
	 				  	  \Fp^2 = \Set{ x^2 | 0 \leq x \leq \frac{p-1}{2}, x \in \N}.
	 				  \]
	 				  Since the only perfect squares less than $p$ are exactly $\Set{x^2 | 0 \leq x \leq \lfloor{\sqrt{p}}\rfloor, x \in \N}$ and $\frac{p-1}{2} \geq \sqrt{p}$ for $p \geq 7$, we infer that $t$ cannot be a perfect square. \newline
	 				  Now we can consider the polynomial $P(X) = X^2 - t \in \Z[X]$: we know that it has no rational root but $x_0 = -2^{-1}$ is a root of $P$ in $\Z/p\Z$, by construction. Obviously $P'(X) = 2X$ so $P'(x_0) = 2x_0 \not\equiv 0 \mod p$. Then we can apply Hensel's lemma and obtain $\xi \in \Zp$ such that $P(\xi) = 0$.\qedhere
	 		\end{itemize}
		\end{proof}
		Actually, if $p \neq 2$, there is an easier way to prove $(\Q, \pabs{\ })$ is not complete, using the Cauchy sequence $(a^{p^n})_{n \in \N}$, where $a \in \{1, \dots, p-2\}$. The proof can be found at
		\cite[3]{thorne:teichmuller}. Anyway the proof we gave is indeed a nice application of the Hensel's lemma.

		The goal of this section is to build $\Qp$, the completion field of $(\Q, \pabs{\ })$ with $p$ a fixed prime. The building process is analogue to the construction of $\R$, the completion of $(\Q, \abs{\ }_\infty)$ and it's actually the ``standard'' way to complete a metric space. This process is actually necessary, because $(\Q, \pabs{\ })$ is not complete, so it's a very unfriendly setting to perform analysis.
		\begin{defn}
			Let $\mathcal{S} := \Set{ (a_n)_{n \in \N} \subseteq \Q | (a_n)_{n \in \N} \text{ Cauchy for } \pabs{\ }}$. Then
			\begin{equation*}
				\Qp := \nicefrac{\mathcal{S}}{\sim}
			\end{equation*}
			where $\sim$ is a relation on $\mathcal{S}$: $(a_n)_n \sim (b_n)_n$ if $\pabs{a_i - b_i} \to 0$ as $i \to +\infty$.
		\end{defn}
		\begin{prop}
			$\Qp$ is well defined and there's a natural sum and product on $\Qp$ which makes $(\Qp, +, \cdot)$ a field.
		\end{prop}
		\begin{proof}
			$\Qp$ is well defined, in the sense that $\sim$ is an equivalence relation on $\mathcal{S}$ (easy to verify). First of all we can immerge $\Q$ in $\mathcal{S}$ (and then in $\Qp$) sending $x$ to $\{x\}$, the constant sequence (it's immediate that $\{x'\} \sim \{x\} \iff x = x'$ so this is really an immersion). From now on we'll do a little abuse of notation, not to result too pedantic: $0$ will denote both $\{0\}$ (the constant sequence) and $[\{0\}]$ (its equivalence class), context will clarify which is the right meaning. \newline
			% Sum and product
			There's a natural extension of the classical sum and product on $\Q$ to $\Qp$, which makes it a field. Let $a, b \in \Qp$, we define $a + b := [(a_n + b_n)_n]$ where $(a_n)_n, (b_n)_n$ are two representatives of $a$ and $b$ respectively. It is easy to see that this is well defined: the sum of two Cauchy is still a Cauchy and $a + b$ doesn't depend on the choice of the representatives. Similarly the product of $a \cdot b := [(a_n \cdot b_n)_n]$ is well defined: product of two Cauchy is Cauchy and given $(a'_n)_n \sim (a_n)_n$ and $(b'_n)_n \sim (b_n)_n$ we have
			\begin{gather*}
				0 \leq \lim_{i \to +\infty} \pabs{a_ib_i - a'_ib'_i} = \lim_{i \to +\infty} \pabs{a_i(b_i - b'_i) + b'_i(a_i - a'_i)} \leq  \\
				\leq \lim_{i \to +\infty} \pabs{a_i}\pabs{b_i -b'_i} + \lim_{i \to +\infty} \pabs{b'_i}\pabs{a_i - a'_i} = 0
			\end{gather*}
			where we used that if $(a_n)_n$ is Cauchy then it is bounded in norm. Then the definition doesn't depend on the choice of the representatives (the first claim above can be proved in the exact same way).\newline
			It's easy to see $(\Qp, +)$ is a group, because $0$ is the neutral element and additive inverses are defined in the trivial way. To see that also $(\Qp^\times, \cdot)$ is a group let's first note that every sequence $(a_n)_n$ is equivalent to $(a'_n)_n$ where $a'_i = p^i$ if $a_i = 0$ and $a'_i = a_i$ otherwise. Associativity holds and the neutral element is $1 = [\{1\}] \neq 0$. The only non-trivial property to prove is the existence of multiplicative inverses: if $a \neq 0$ then if $a = [(a_n)_n]$ (where $(a_n)_n$ is chosen without zeros) then $1/a = [(1/a_n)_n]$. We have to show that $(1/a_n)_n$ is Cauchy: let $N \in \N$ large enough such that $\exists \varepsilon > 0$ and $\pabs{a_n} > \varepsilon$ $\forall n > N$ (see proof of \cref{prop:padic-is-norm}) and that $n, m > N \implies \pabs{a_n - a_m} < \varepsilon^3$; if $n, m > N$ we obtain
			\begin{equation*}
				\pabs{\frac{1}{a_n} - \frac{1}{a_m}} = \pabs{\frac{a_m - a_n}{a_na_m}} = \frac{1}{\pabs{a_na_m}}\pabs{a_m - a_n} \leq \frac{1}{\varepsilon^2} \varepsilon^3 = \varepsilon.
			\end{equation*}
			Using the same exact technique we can prove that $1/a$ is well defined, i.e. if $(a_n)_n$ and $(a'_n)_n$ are both non-zero representatives of $a$ then $(1/a_n)_n \sim (1/a'_n)_n$. Obviously this product is abelian. It is also easy to prove that distributivity holds, i.e. given $a, b, c \in \Qp$ $a \cdot (b + c) = a\cdot b + a \cdot c$ (it's sufficient to note that $a \cdot (b + c) = [(a_n \cdot (b_n + c_n))_n] = [(a_n \cdot b_n + a_n \cdot c_n)_n]$). So we have finally proved that $(\Qp, +, \cdot)$ is a field, containing $\Q$ as a subfield (the immersion defined at the beginning is in-fact a ring morphism between $\Q$ and $\Qp$, representing the natural identification of $\Q$ in $\Qp$).
		\end{proof}

		% Norm Extension
		We have then to extend the norm $\pabs{\ }$ to $\Qp$: if $a \in \Qp$ then $\pabs{a} := \lim_{i \to +\infty} \pabs{a_i}$ where $(a_n)_n$ is any representative of $a$.
		\begin{prop}
			\label{prop:padic-is-norm}
			$\pabs{\ }$ is a norm on $\Qp$.
		\end{prop}
		\begin{proof}
			First of all we prove that, chosen a representative $(a_n)_n$ of $a$, $\exists \lim_{i \to +\infty} \pabs{a_i}$. We have two cases:
			\begin{enumerate}
				\item if $a = 0$, by definition, $\lim_{i \to +\infty}\pabs{a_i} = 0$;
				\item if $a \neq 0$ then $(a_n)_n \nsim 0$ so $\exists \varepsilon > 0$ and for every $N \in \N$ there exists $i_N > N$ such that $\pabs{a_{i_N}} > \varepsilon$. Since $(a_n)_n$ is Cauchy, choosing $N$ large enough such that $\pabs{a_i - a_j} < \varepsilon$ $\forall i,j > N$ we have that $\pabs{a_i - a_{i_N}} < \varepsilon$ $\forall i>N$. Using the isosceles triangle principle we get $\pabs{a_i} = \pabs{a_{i_N}}$ for every $i > N$, so trivially the limit exists.
			\end{enumerate}
			Now we prove that this is well defined, i.e. $\pabs{a}$ doesn't depend on the choice of the representative of $a$. Let $(a_n)_n, (b_n)_n$ two representatives of $a$, then $(a_n)_n \sim (b_n)_n$ which means $\lim_{i \to +\infty} \pabs{a_i - b_i} = 0$. Now by the reverse triangular inequality
			\begin{equation*}
				0 \leq \lim_{i \to +\infty} \abs{\pabs{a_i} - \pabs{b_i} } \leq \lim_{i \to +\infty} \pabs{a_i - b_i} = 0 \implies \lim_{i \to +\infty} \pabs{a_i} = \lim_{i \to +\infty} \pabs{b_i}.
			\end{equation*}
			The property $1.$ of norms is proved in the case  above. Property $2.$ and $3.$ immediately follows from the ones of $\pabs{\ }$ on $\Q$ and basic limit rules.
		\end{proof}
		\begin{prop}
			$(\Qp, \pabs{\ })$ is complete.
		\end{prop}
		\begin{proof}
			We have to prove that if $(a_n)_n$ is a Cauchy sequence in $\Qp$ for $\pabs{\ }$ then there exists $a \in \Qp$ such that $a = \lim_{i \to +\infty} a_i$. Let $a_n = [(a_{n,m})_{m \in \N}]$ where $(a_{n,m})_m$ is a Cauchy sequence in $\Q$. Let $N_j \in \N$ such that $\forall$ $n, m > N_j$ $\pabs{a_{j, m} - a_{j, n}} < 1/j$. Now, choosing $k_j > \max \{N_j, k_{j-1} \}$, we claim that $[(a_{n, k_n})_n] \in \Qp$ is the limit of the sequence at the beginning. First of all we prove that $(a_{n, k_n})_n$ is a Cauchy sequence in $\Q$:
			\begin{gather*}
				\pabs{a_{n, k_n} - a_{m, k_m}} = \pabs{ a_{n, k_n} - a_{n, j} + a_{n, j} - a_{m, j} + a_{m, j} - a_{m, k_m} } \leq  \\
				\leq \max \left\{\pabs{a_{n, k_n} - a_{n, j} }, \pabs{a_{n, j} - a_{m, j}}, \pabs{a_{m, j} - a_{m, k_m} } \right\}.
			\end{gather*}
			Choosing a large enough $j \in \N$ we obtain $\pabs{a_{n, k_n} - a_{n, j} } < 1/n$ and $\pabs{a_{m, j} - a_{m, k_m} } < 1/m$. Since $(a_n)_n \subseteq \Qp$ is a Cauchy sequence, for every $\varepsilon > 0$ $\exists N \in N$ such that $n, m > N \implies \pabs{a_n - a_m} < \varepsilon$, meaning $\lim_{j \to +\infty} \pabs{a_{n, j} - a_{m, j}} < \varepsilon$. From here we can see that $\exists N' \in \N$ such that $j > N' \implies \pabs{a_{n, j} - a_{m, j} } < \varepsilon$ so we can also control the other term above. We proved that $(a_{n, k_n})_n$ is a Cauchy sequence. \newline
			Now we show that its equivalence class, let it be $a \in \Qp$, is actually the limit of $(a_n)_n$, i.e.
			\[
				0 = \lim_{n \to +\infty} \pabs{a_n - a} = \lim_{n \to +\infty} \left( \lim_{j \to +\infty} \pabs{a_{n, j} - a_{j, k_j} } \right).
			\]
			Fixed $\varepsilon > 0$ we know that $\exists N \in \N$ such that $n, m > N \implies \pabs{a_{n, k_n} - a_{m, k_m} } < \varepsilon$. Choosing $\N \ni n > \max \{N, 1/\varepsilon \}$ we have
			\begin{equation*}
				\pabs{a_{n, j} - a_{j, k_j} } \leq \max \left\{\pabs{a_{n, j} - a_{n, k_n}}, \pabs{a_{n, k_n} - a_{j, k_j} } \right\}.
			\end{equation*}
			If $j > \max\{k_n, N\}$ then $\pabs{a_{n, j} - a_{n, k_n}} < 1/n < \varepsilon$ and $\pabs{a_{n, k_n} - a_{j, k_j} } < \varepsilon$ so
			\[
				\lim_{j \to +\infty} \pabs{a_{n, j} - a_{j, k_j} } \leq \varepsilon.
			\]
			Thesis easily follows from the fact that $\varepsilon$ is arbitrary.
		\end{proof}
		\begin{prop}
			\label{prop:Q-dense-in-Qp}
			$\Q$ is dense in $\Qp$.
		\end{prop}
		\begin{proof}
			Chosen $a = [(a_n)_n] \in \Qp$ and $\varepsilon > 0$ we know that $\exists N \in \N$ such that $n > m > N \implies \pabs{a_n - a_m} < \varepsilon$. Now, fixed $n \in \N$ we have that $a_n \in \Q$ is identified with $a' = \{a_n\} \in \Qp$, the equivalence class of the constant sequence $(a_n, a_n, a_n, \dots)$. Now $\pabs{a - a'} = \lim_{j \to +\infty} \pabs{a_j - a_n}$ which is clearly no bigger than $\varepsilon$ (we can consider $j > N$).
		\end{proof}
		Up to now we have proved that $(\Qp, \pabs{\ })$ is actually the completion of $(\Q, \pabs{\ })$. Obviously we're not going to work using this abstract construction, thanks to the following result.\newline
		First we'll need a technical lemma.
		\begin{lemma}
			\label{lemma:integer-representation-Qp}
			If $x \in \Q$ and $\pabs{x} \leq 1$ then $\forall i \in \N$ $\exists \alpha \in \Z$ such that $\pabs{\alpha - x} \leq p^{-i}$. The integer $\alpha$ can be chosen in $\{0, 1, \dots, p^i-1\}$.
		\end{lemma}
		\begin{proof}
			Let $x = a/b$ written in lowest terms. The fact that $\pabs{x} \leq 1$ means exactly $p \nmid b$ so, since $p$ is a prime number, $p^i$ and $b$ are coprime; thanks to Bézout identity $\exists m, n \in \Z$ $mb + np^i = 1$. Letting $\Z \ni \alpha := am$ we get
			\begin{equation*}
				\pabs{\alpha - x} = \pabs{am - \frac{a}{b}} = \pabs{\frac{a}{b}}\pabs{mb - 1} \leq \pabs{mb - 1} = \pabs{np^i} = \frac{\pabs{n}}{p^i} \leq \frac{1}{p^i}
			\end{equation*}
			since $\pabs{x} = \pabs{a/b} \leq 1$ and $\pabs{n} \leq 1$ if $n \in \Z$. Adding the right multiple of $p^i$ to $\alpha$ we can get an integer between $0$ and $p^i - 1$ still satisfying the above inequality.
		\end{proof}
		\begin{thm}
			\label{thm:representation-Qp}
			Every $a \in \Qp$ with $\pabs{a} \leq 1$ has exactly one representative $(a_i)_{i \in \N}$ such that for every $i \in \N$:
			\begin{enumerate}
				\item $a_i \in \{0, 1, \dots, p^{i+1}-1\}$;
				\item $a_i \equiv a_{i+1} \mod p^{i+1}$.
			\end{enumerate}
		\end{thm}
		\begin{proof}
			We first prove uniqueness: let $(a'_i)_i$ a different sequence satisfying \textit{1}. and \textit{2.} If $a_{i_0} \neq a'_{i_0}$ then $a_{i_0} \not\equiv a'_{i_0} \mod p^{i_0 + 1}$ since they are both between $0$ and $p^{i_0 + 1}$. Now if $i \geq i_0$ we have
			\[
				a_i \equiv a_{i_0} \not\equiv a'_{i_0} \equiv a'_i \mod p^{i_0 + 1} \implies \pabs{a_i - a'_i} > \frac{1}{p^{i_0 + 1}},
			\]
			meaning $(a'_i)_i \nsim (a_i)_i$.\newline
			Now we prove existence. Let $(b_i)_i$ be any of the representatives of $a$ and let $N(j) \in \N$ such that $n, m \geq N(j) \implies \pabs{b_n - b_m} \leq p^{-j-1}$ for every $j \in \N$. We can choose the sequence $(N(j))_{j \in \N} \subseteq \N$ strictly increasing with $j$, in particular with $N(j) > \max \{j, N(j-1)\}$. We immediately note that if $i \geq N(0)$ then $\pabs{b_i} \leq 1$ because for every $j \geq N(0)$
			\begin{equation*}
				\pabs{b_i} \leq \max\left\{\pabs{b_j}, \pabs{b_i - b_j} \right\} \leq \max\left\{\pabs{b_j}, \frac{1}{p}\right\}
			\end{equation*}
			and $\lim_{j \to +\infty} \pabs{b_j} = \pabs{a} \leq 1$. Using \cref{lemma:integer-representation-Qp} we can find $a_j \in \Z$ such that $0 \leq a_j < p^{j+1}$ and $\pabs{a_j - b_{N(j)} } \leq 1/p^{j+1}$, because $\pabs{b_{N(j)}} \leq 1$. We'll show that $(a_n)_n$ is the desired sequence. Obviously it's Cauchy because
			\begin{equation*}
				\pabs{a_n - a_m} \leq \max\left\{\pabs{a_n - b_{N(n)} }, \pabs{b_{N(n)} - b_{N(m)}}, \pabs{b_{N(m)} - a_m}  \right\}
			\end{equation*}
			and, choosing $n, m$ large enough, we can control all those three terms. Property \textit{1}. is already verified by construction so we have only to prove that $a_{j+1} \equiv a_j \mod p^{j+1}$ and that $(a_n)_n \sim (b_n)_n$. The former follows from
			\begin{gather*}
				\pabs{a_{j+1} - a_j} \leq \max\left\{ \pabs{a_{j+1} - b_{N(j+1)} }, \pabs{b_{N(j+1)} - b_{N(j)}}, \pabs{b_{N(j)} - a_j} \right\} \leq \\
				\leq \max\left\{\frac{1}{p^{j+2}}, \frac{1}{p^{j+1}}, \frac{1}{p^{j+1}} \right\} \leq \frac{1}{p^{j+1}}.
			\end{gather*}
			To prove the latter, for every $j$, if $i > N(j)$ we have
			\begin{gather*}
				\pabs{a_i - b_i} \leq \max\left\{\pabs{a_i - a_j}, \pabs{a_j - b_{N(j)}}, \pabs{b_{N(j)} - b_i} \right\} \leq \\
				\leq \max\left\{\frac{1}{p^{j+1}},  \frac{1}{p^{j+1}}, \frac{1}{p^{j+1}} \right\} = \frac{1}{p^{j+1}}
			\end{gather*}
			because $a_i \equiv a_{i+1} \equiv a_{i+2} \equiv \dots \equiv a_j \mod p^{i+1}$. So $\lim_{j \to +\infty} \pabs{a_j - b_j} = 0$, i.e. $(a_i)_i \sim (b_i)_i$.
		\end{proof}
		So we have a ``canonical'' representative for every $a \in \Qp$ with $\pabs{a} \leq 1$, let it be $(a_n)_n$. Since $a_i \in \{0, 1, \dots, p^{i+1} - 1\}$ we can write it using base $p$, i.e.,
		\begin{equation*}
			a_i = b_0 + b_1p + b_2p^2 + \dots + b_ip^i
		\end{equation*}
		where $b_i \in \{0, 1, \dots, p-1\}$. Property \textit{2}. of \cref{thm:representation-Qp} tells us exactly that
		\begin{equation*}
			a_{i+1} = b_0 + b_1p + b_2p^2 + \dots + b_ip^i + b_{i+1}p^{i+1}
		\end{equation*}
		i.e. the first $i+1$ digits (from $b_0$ to $b_i$) are the same, because $\pabs{a_{i+1} - a_i} \leq 1/p^{i+1}$. So we can write, just as a notation,
		\begin{equation*}
			a = \sum_{i=0}^{+\infty} b_ip^i = b_0 + b_1p + b_2p^2 + \dots
		\end{equation*}
		the so called \padic expansion of $a$. It's easy to see that $\pabs{a} = p^{-k}$ where $k$ is the minimum integer such that $b_k \neq 0$ ($k = +\infty$ if $a=0$). This notation makes sense only if $\pabs{a} \leq 1$ but it can be used for every element of $\Qp$ with a little refinement: let $a' \in \Qp$ with $\pabs{a'} = p^m > 1$ ($m \in \N^{\times}$); then $\pabs{p^{m}a'} = \pabs{p^m}\pabs{a'} = 1$ so we can expand it like before
		\begin{equation*}
			p^ma' = \sum_{i=0}^{+\infty} b_ip^i
		\end{equation*}
		and multiplying both sides by $p^{-m}$ we obtain
		\begin{equation*}
			a' = p^{-m} \sum_{i=0}^{+\infty} b_ip^i = \sum_{i=0}^{+\infty} b_ip^{i-m} = \frac{b_0}{p^m} + \frac{b_1}{p^{m-1}} + \dots + \frac{b_{m-1}}{p} + b_m + b_{m+1}p + \dots
		\end{equation*}
		which can be thought as a \padic expansion with a finite number of decimal digits. So we have a unique canonical way to write every element of $\Qp$, which is way more practical than the abstract description. For example it is now easy to actually perform arithmetic operations: sum, difference, multiplication and division can be done applying the exact same algorithm that we use to perform them between integers, except that now we have to proceed with infinite digits (and actions like ``carrying'' or ``borrowing'' work from left to right).
		\begin{defn}
			Given $a, b \in \Qp$ and $n \in \N^{\times}$ we say that $a \equiv b \mod p^n$ if $\pabs{a - b} \leq 1/p^n$.
		\end{defn}
		It's easy to check that if $a, b \in \Z$ this definition is exactly the old-fashioned congruence.
		\begin{defn}
			$\Zp := \Set{x \in \Qp | \pabs{x} \leq 1}$ is called the set of \emph{\padic integers}.
		\end{defn}
		It's easy to verify that $\Zp$ is a subring of $\Qp$ (immediate from properties of $\pabs{\ }$). Its invertible elements are exactly
		\[
			\Zp^{\times} = \Set{x \in \Zp | \frac{1}{x}\in \Zp} = \Set{x \in \Zp | x \not \equiv 0 \mod p} = \Set{x \in \Zp | \pabs{x} = 1}.
		\]
		We can now justify our initial notations, which is actually a ``real'' equality and not just a way to write things, thanks to the following lemma.
		\begin{lemma}
			Let $(c_i)_i \subseteq \Qp$ such that $\lim_{i \to +\infty} c_i = 0$. Then the series
			\begin{equation*}
				\sum_{i=0}^{+\infty} c_i
			\end{equation*}
			converges in $\Qp$.
		\end{lemma}
		\begin{proof}
			We need to show that the sequence of partial sums converge, i.e. $(S_n)_n \subseteq \Qp$ has limit, where $S_n := c_0 + c_1 + \dots + c_n$. Since $\Qp$ is complete it's sufficient to prove $(S_n)_n$ is Cauchy. Fixed $\varepsilon > 0$ $\exists N \in \N$ such that $i > N \implies \pabs{c_i} < \varepsilon$; so if $n, m > N$ we have
			\begin{equation*}
				\pabs{S_n - S_m} = \pabs{c_{n+1} + \dots + c_m} \leq \max\left\{\pabs{c_{n+1}}, \dots, \pabs{c_m} \right\} < \varepsilon
			\end{equation*}
			so $(S_n)_n$ is Cauchy.
		\end{proof}
		We can use this lemma with $c_i = b_ip^{i-m}$, for $m \in \N$ and $b_i \in \{0, 1, \dots, p^i - 1\}$, because $\pabs{c_i} = \pabs{b_i}\pabs{p^{i-m}} \leq 1 \cdot p^{m - i} \to 0$ as $i \to +\infty$. We conclude that every \padic expansion
		\[
			\sum_{i=0}^{+\infty} b_ip^{i-m}
		\]
		actually converges to some element in $\Qp$ (and, clearly, our notation is coherent). This lemma is also a much cleaner results on series: they converge if and only if the general term approaches zero, unlike in $(\R, \abs{\ }_\infty)$ where there are divergent series like $1 + \frac{1}{2} + \frac{1}{3} + \dots = \sum_{n=1}^{+\infty} 1/n$. There is a very nice result about \padic expansions: while the writing of rational numbers using base 10 is not unique ($0.99999\ldots = 1$), in $\Qp$ \padic expansions are unique, i.e. if two expansions have different digits they converge to totally different numbers.
		\begin{lemma}
			\label{lemma:Q-in-Qp}
			Given $a = p^k \sum_{i=0}^{+\infty} a_ip^i \in \Qp$, its \padic expansion is periodic, i.e. $\exists r, N \in \N$ such that $a_i = a_{i+r}$ for every $i > N$, if and only if $a \in \Q$.
		\end{lemma}
		\begin{proof}
	 	To see that every periodic \padic number is rational we can write
			\begin{equation*}
			a = \sum_{i=-k}^{+\infty} a_ip^i = (a_{-k}p^{-k} + \dots + a_{m-1}p^{m-1}) + p^m\sum_{i=0}^{+\infty} (b_0 + b_1p + \dots + b_{n-1}p^{n-1})p^{in}
			\end{equation*}
			with the obvious meaning: $\Q \ni q := a_{-k}p^{-k} + \dots + a_{m-1}p^{m-1}$ is the anti-period and $(b_0, \dots, b_{n-1})$ is the period. It is an easy calculation to verify that if $\alpha \in \N^{\times}$
			\begin{equation*}
			\sum_{i=0}^{+\infty} p^{i\alpha} = \frac{1}{1 - p^\alpha}.
			\end{equation*}
			Using this identity we get
			\begin{equation*}
				a = q + (b_0 + b_1p + \dots + b_{n-1}p^{n-1})\cdot p^m \cdot \sum_{i=0}^{+\infty} p^{in} = q + (b_0 + b_1p + \dots + b_{n-1}p^{n-1})\cdot\frac{p^m}{1 - p^n}
			\end{equation*}
			which is clearly in $\Q$.\newline
			To prove that every $q \in \Q$ has a periodic \padic expansion we'll need a little more work. First of all let's note that if $a \in \Qp$ admits a periodic representation also $-a$ admits one: given
			\begin{equation*}
				a = \sum_{i=-k}^{+\infty} a_ip^i = (a_{-k}p^{-k} + \dots + a_{m-1}p^{m-1}) + p^m\sum_{i=0}^{+\infty} (b_0 + b_1p + \dots + b_{n-1}p^{n-1})p^{in}
			\end{equation*}
			we have
			\begin{gather*}
				-a = (p - a_{-k})p^{-k} + (p - 1 - a_{-k+1})p^{-k+1} +  \dots + (p - 1 - a_{m-1})p^{m-1} +\\
				+ p^m\sum_{i=0}^{+\infty} \left[(p - 1 - b_0) + (p - 1 - b_1)p + \dots + (p - 1 - b_{n-1})p^{n-1}\right]p^{in}
			\end{gather*}
			i.e. the period is $(p-1-b_0, p-1-b_1, \dots, p-1-b_n)$ (the relation above is true if $a$ admits a non zero anti-period, but it's almost the same if it does not).\newline
			Now let $\Q \ni a/b = p^k\cdot(t/s)$ with $p \nmid ts$. We'll show that $t/s$ admits a periodic expansion (then we can conclude immediately). Since $p$ is prime $p \nmid ts \implies p \nmid s$ so $p$ and $s$ are coprime and, thanks to Euler's theorem, $1 - p^{\phi(s)} = \alpha s$ with $\alpha \in \Z_{\leq0}$ (where $\phi$ is the Euler's totient function). So we have
			\begin{equation*}
				\frac{t}{s} = \frac{\alpha t}{1 - p^{\phi(s)} } = \alpha t \cdot \left(\frac{1}{1 - p^{\phi(s)}} \right).
			\end{equation*}
			Now it's sufficient to prove that $\abs{\alpha t}/(1 - p^{\phi(s)})$ is periodic (because sign doesn't matter). As said before we know that $(1 - p^{\phi(s)})^{-1} = \sum_{i=0}^{+\infty} p ^ {\phi(s)i}$ and that $\abs{\alpha t} \in \N$ has a finite \padic expansion (i.e. definitively zero). It's easy to see that also their product is periodic.
		\end{proof}
		Using this characterization of $\Q$ in $\Qp$ we can give another proof (a posteriori) of the non-completeness of $(\Q, \pabs{\ })$. Obviously, this proof is much easier than the proof of \cref{prop:Q-not-complete}, because it already uses the structure of $\Qp$.
		\begin{prop}
			$(\Q, \pabs{\ })$ is not complete.
		\end{prop}
		\begin{proof}
			Using the density of $\Q$ in $\Qp$, proved in \cref{prop:Q-dense-in-Qp}, we just need to find some element $e \in \Qp \setminus \Q$, because then we'll have a Cauchy sequence in $(\Q, \pabs{\ })$ which doesn't converge to any rational. Thanks to \cref{lemma:Q-in-Qp} we know that every element of $\Q$ corresponds to a periodic expansion in $\Qp$ and vice-versa, so $e$ can be every infinite \padic expansion which is not periodic, like for example
			\begin{equation*}
				e = 1 + p^2 + p^4 + p^8 + \dots = \sum_{i=0}^{+\infty} p^{2^i}.\qedhere
			\end{equation*}
		\end{proof}


\lab{Algorithms}{Limitations of Floating Point Computation}{Limitations of Floating Point Computation}
\label{lab:breakfloat}
\objective{Understand the limitations of floating point numbers and introduce numerical stability.}

\section*{Introduction}

In the last lab we discussed the structure and flexibility of floating point numbers.
Floating point numbers are a remarkably versatile tool for performing computations that roughly approximate operations on real numbers.
In this lab we will see how the imperfections of floating point numbers can lead to all kinds of trouble.
There are a variety of common errors involved with floating point computations.
In order to safely use floating point computations it is necessary to know what their limitations are.

We will start by considering a simple example in Cython:

\begin{lstlisting}
cimport numpy as np
def breakfloat(int n):
    cdef np.float32_t a = 2<<26
    cdef int i
    actual = float(a) + n
    for i in range(n):
        a += 1
    return a, actual
\end{lstlisting}

When we run this code for \li{n=2**26} we see that the actual answer is double the answer returned by adding 1 repeatedly.
This is because the floating point number is large enough that adding 1 to it is, after rounding, equivalent to adding 0.
This is effect is mitigated substantially by the use of double precision floating point numbers, but it is still a worry.

\begin{problem}
consider the cython function defined below
\begin{lstlisting}
from numpy cimport ndarray as ar
cimport numpy as np
import numpy as np
from numpy.random import rand
def sumrand(int size, int number):
    # takes the sum of <number> of random arrays
    # with number of elements <size>
    cdef np.float32_t tot=0.
    cdef ar[np.float32_t] A = np.empty(size, dtype=np.float32)
    cdef int i, j
    for i in xrange(number):
        A[:] = rand(size).astype(np.float32)
        for j in xrange(size):
            tot += A[j]
    return tot
\end{lstlisting}
The function call \li{breakfloat2(10**6, 10**3)} returns 16777216.0.
The answer should obviously be around 500000000.
Why is there such a large error?
How could you fix the function so that it would give the right result?
\end{problem}

\section*{Various Computational Considerations}

There are a wide variety of issues that must be considered when using floating point numbers.
The first thing to notice is that floating point numbers are not actually stored as decimal representations.
When you define a decimal number, your computer does the best it can to \emph{approximate} the decimal number given.
For example, when you give your computer a value of .1, the value it actually stores is 0.1000000000000000055511151231257827021181583404541015625 which is usually close enough to .1 that the difference goes unnoticed.
This is, in a sense, a sort of round off error.
There is usually always a certain amount of error involved in each floating point computation.
The floating point operations on modern computers are designed to minimize such error, but it will always exist due to the limitations of floating point representation.
The way that computers implement floating point arithmetic does imply that there is a certain bound on the error introduced in each operation.
The bound that is most common is called "machine precision" or "machine epsilon."
Given two precisely represented floating point numbers the Fundamental Axiom of Floating Point Arithmetic states that there is an $\epsilon$ with $|\epsilon | \leq \epsilon_{machine}$ such that $x * y = (x y) (1+\epsilon)$ where $*$ denotes a floating point operation between the two numbers.
For single precision floating point numbers $\epsilon_{machine} = 2^{-24}$.
For double precision it is $2^{-53}$.
These errors are extremely small, but a poorly written algorithm can result in massive accumulation of error and incorrect results.
These sort of problems can pop up in all kinds of places.
You must also keep in mind that rounding occurs after each operation.
These two effects can combine to give unexpected results, for example
\begin{lstlisting}
>>> 3. * .1 - .3
5.551115123125783e-17
>>> 3. * .1 == .3
False
\end{lstlisting}
These sorts of effects can combine to give somewhat astonishing results, for example, all of the following should be equivalent.
They should all evaluate to 0.
\begin{lstlisting}
>>> n = 19000001
>>> (5*n)**2 - (4*n)**2 - (3*n)**2 # exact result using integers
0
>>> (5.*n)**2 - (4*n)**2 - (3*n)**2 # added a single decimal point
1.0
>>> (5*n)**2 - (4.*n)**2 - (3*n)**2 # added a single decimal point somewhere else
-1.0
>>> (5.*n)**2 - ((4*n)**2 + (3*n)**2) # added a decimal point and some parenthesis
2.0
\end{lstlisting}
Notice how four expressions that should, in theory, be equivalent returned four \emph{different} answers.
The change caused by the inclusion of parenthesis is particularly troublesome because it means that floating point operations are only \emph{approximately} associative.
This is an example of what is called catastrophic cancellation.
Catastrophic cancellation occurs when we perform an operation on two floating point numbers that are already subject to rounding error.
it is especially noticeable when subtracting numbers that are approximately equal.
Because different methods of computation may yield slightly different results it is imperative that equality tests and comparisons using floating point numbers be carried out with certain tolerances.
It is all too tempting to do some floating point computation that you would expect to give a result of 0 and then to test your result with something like
\begin{lstlisting}
if result == expected:
    print "it worked"
\end{lstlisting}
and then try to debug a perfectly good bit of code because the way it does its computations gives a result that is minutely different than what you expect.
A test like this should be replaced with something roughly along the lines of
\begin{lstlisting}
tolerance = 1E-6
if abs(result - expected) < tolerance:
    print "it worked"
\end{lstlisting}
For a specific example, consider the square root function given in the previous lab for 32 bit floating point numbers.
The square root given is not a perfect match to the square root given by the built in square root function, but it can compute the proper value so that all but the last bit is accurate.
To test whether or not the algorithm converges for randomly generated numbers between 0 and 1 we may be tempted to do the following
\begin{lstlisting}
A = rand(10000)
(pysqrtacc32(A, 2) == np.sqrt(A)).all()
\end{lstlisting}
Which would return \li{False} since some of the values returned by our \emph{algorithm} may differ from the built in square root by an extremely small amount regardless of how many iterations we run.
We can test for accuracy properly using something like
\begin{lstlisting}
A = rand(10000)
(np.absolute(pysqrtacc32(A, 2) - np.sqrt(A)) < 2E-7).all()
\end{lstlisting}
Which returns \li{True}.

\begin{problem}
One example where it is common to subtract numbers that are nearly equal is in the computation of numerical derivatives.
Write a simple approximation to the derivative of a function $f$ using the formula $\frac{f(x+h) - f(x)}{h}$.
Check how accurate it is for varying sizes of \li{h} by testing it on the \li{sin} function included in Python's math library with \li{x = 1}.
Use Python's \li{cos} function as the exact value of the derivative.
What do you notice about accuracy as \li{h} becomes smaller and smaller?
\end{problem}

\begin{figure}
\includegraphics[width=\textwidth]{lnseries.pdf}
\caption{Floating point computation of $\frac{\ln \left(x+1\right)}{x}$ shown with a corresponding series approximation.}
\label{floatbreak:lnseries}
\end{figure}

\begin{problem}
Sometimes clever numerical methods can be used to avoid some of the issues involved in floating point computations.
The value for $\frac{\ln \left(x+1\right)}{x}$ can be approximated by the series
\[
\sum_{n=1}^{\infty} \frac{\left( -1 \right)^{n+1}}{n} x^n
\]
where $\| x \| \leq 1$.
Use this series to obtain a more accurate approximation of $\frac{\ln \left(x+1\right)}{x}$ for extremely small floating point numbers.
Using the polynomial you have constructed plot the exact value and the approximation to the function $ln \left( 10^{-x} + 1 \right)10^{x}$.
Plot the exact value on $[-5, 30]$.
Plot the approximation on $[1, 30]$.
Your plot should look roughly like the plot shown in Figure \ref{floatbreak:lnseries}.
\end{problem}

Another limitation of floating point numbers is that they are subject to overflow.
Floating point numbers do have restricted values set aside for values of $\infty$, $-\infty$, and \li{Nan}, so you can often tell when overflow has occured, but it is best to avoid extremely large or extremely small floating point values in either case.
For a simple example of overflow, we will perform a computational demonstration that the $p$-norms approach the $\infty$-norm as $p \rightarrow \infty$.
\begin{lstlisting}
def pnorm(A, p):
    return (A**p).sum()**(1./p)
\end{lstlisting}
If you haven't seen p-norms before, the main thing you need to understand here is that as $p \rightarrow \infty$, \li{pnorm(A, p)} should come arbitrarily close to \li{np.absolute(A).max()}.
Testing this computationally, we see that, for a randomly generated array of values between 0 and 1, we get the plot shown in Figure \ref{pnorm_convergence} as $p \rightarrow \infty$.

\begin{figure}
\includegraphics[width=\textwidth]{pnorm_convergence.pdf}
\caption{The convergence of the $p$-norms to the $\infty$-norm for a randomly generated vector of 100 floating point values in $(1,2)$.
Notice how the $p$-norms disappear when $p$ is roughly 1000.
This is because values of floating point infinity are introduced into the computation.
This is especially troublesome because the array is nearly at the middle of the range of possible floating point values.
If the maximum were less than 1, the norm would suddenly drop to zero instead of jumping to $\infty$.}
\label{pnorm_convergence}
\end{figure}

For floating point numbers, overflow results in a value of $\infty$ or $-\infty$.
Operations that cannot be well defined (like $\infty - \infty$) return a value of \li{NAN}, which stands for "Not A Number."
When dealing with machine integers, for example integers that are stored in arrays or integers in programming languages other than Python, there are no specific values for things likt $\infty$.
The operations can be performed, but the result will be incorrect.
To understand this, first consider an unsigned integer.
Unsigned integers are always considered to be positive.
Unsigned integers have no sign, so each bit is considered a binary digit.
In an eight bit unsigned integer there are 256 possible values.
They are used to represent the numbers 0 through 255.
When we pass 255, the numbers cycle back around to 0 again, so, for example:
\begin{lstlisting}
>>> np.arange(250, 260, dtype=np.uint8)
array([250, 251, 252, 253, 254, 255,   0,   1,   2,   3], dtype=uint8)
>>> np.arange(250, 260, dtype=np.uint8) * 2
array([244, 246, 248, 250, 252, 254,   0,   2,   4,   6], dtype=uint8)
>>> np.arange(250, 260, dtype=np.uint8)**2
array([36, 25, 16,  9,  4,  1,  0,  1,  4,  9], dtype=uint8)
\end{lstlisting}
As you can see, the operations can be understood as operations in $\mathbb{Z} / 256 \mathbb{Z}$, which has operations equivalent to those in $\mathbb{Z}$ only some of the time.
These sorts of issues can come up in computations involving integers or even typecasts to integers.

% this is another possibly useful problem, but it can be removed if necessary
\begin{problem}
Your friend writes the following Cython code to search for Pythagorean Triples.
\begin{lstlisting}
from libc.math cimport sqrt
def find_triple(double start, int num):
    cdef int i, j
    cdef double temp
    for i in xrange(num):
        for j in xrange(i, num):
            temp = sqrt((start+i)**2 + (start+j)**2)
            if temp == int(temp):
                print int(start+i), int(start+j), int(temp)
\end{lstlisting}
By running \li{find_triple(600000000, 10000)}, he claims to have found the triples

600001807  600004043  848532274

600001924  600004742  848532851

600004561  600009787  848538283

600004738  600007261  848536622

{\setlength{\parindent}{0cm}
What is wrong with his code and how should it be fixed?
Be sure to watch out for any integer overflow along the way.
}
\end{problem}

\section*{Some Examples of Computation Error}

There have been several historical examples where incorrect numerical computing has caused terrible disasters.

Our first example involves the explosion of the Ariane 5 rocket in 1996.
The Ariane 5 rocket was launched by the European Space Agency.
Roughly 7 billion dollars were spent in its development.
The rocket and its cargo were, in themselves, worth an estimated 500 million dollars.
At an altitude of about 3700 meters the rocket veered off of its intended path and exploded.
After some investigation it was discovered that the error was caused by the conversion of an excessively large 64 bit floating point number to a 16 bit signed integer.
To see how this could cause problems, consider the following example:
\begin{lstlisting}
>>> from numpy.random import rand
>>> A = rand(10) * 10**5
>>> A.astype(np.int16) # incorrect cast, overflow has occured
array([  6691,  24236, -28341,   4104,  18063, -19080,  -7500,  -4954,
        25898,  11995], dtype=int16)
>>> A.astype(np.int64) # what the values should be
array([26802, 64425,  1339, 55901, 51665, 11992, 90368, 87717,    25, 38894], dtype=int64)
\end{lstlisting}
Keep in mind that a simple error like this caused hundreds of millions of dollars of damage.

The Vancouver Stock Exchange is another example of a relatively simple propagation of error.
When the exchange opened, the index to track its value was initialized at 1000 in January of 1982.
Around 3000 times per day the index was updated and then truncated to three decimals of precision.
After 22 months the computed value was 524.881 when the actual value should have been 1009.811.
When it was discovered that the computation of the index was diminishing it's value, the index was reset to its actual value.

Another example which we will not discuss here was the sinking of the Sleipner A offshore platform in 1991.
That disaster was due to improper finite element analysis.
The total loss was estimated at 700 million dollars.

The final example we will consider here is the failure of the Patriot Missile Defense system at Dharan, Saudi Arabia during the gulf war.
In this disaster the patriot missile system failed to fire on an incoming missile.
The missile struck a barracks, killed 28 people, and injured around 100 others.
This failure was due to improper use of floating point numbers!
The system clock was incremented using every .1 seconds using integers, but the time was then converted back to float by multiplying by .1 for use in some floating point computations.
After 100 hours of operation the roundoff error in the floating point representation of .1 became significant enough that it prevented the system from properly targeting an incoming missile.

\begin{problem}
In the floating point format used for the Patriot missile systems in the gulf war the closest floating point number to .1 was $\frac{209715}{2097152}$.
Calculate the error accumulated over 100 hours.
If a missile travels at 1676 meters per second, how far will it be able to travel in that amount of time?
\end{problem}


\lab{Applications}{Wavelet Denoising and Compression}{Denoising and Compression}

\objective{This lab presents the two-dimensional Discrete Wavelet Transform
as well as related applications in image denoising and compression.}

\section*{The two-dimensional Discrete Wavelet Transform}
In the previous lab, we explored wavelet analysis using the Haar wavelet and
the discrete wavelet transform. Our discussion focused on one-dimensional
discrete signals, but it is not difficult to extend the same ideas into the
realm of two-dimensional arrays. As you know, a digital image can be represented
as a matrix of pixel values (for simplicity we will consider grayscale images of
size $2^n \times 2^n$ in this lab). We can perform the wavelet decomposition of
an image in much that same way as with one-dimensional signals. We once again
calculate detail and approximation coefficients using an iterative filter
bank, but now we generate four arrays of coefficients at each iteration as opposed to
just two. In essence, we perform the one-dimensional wavelet transform first on each
row, and then on each column of the matrix. Given an input matrix of size $2^n \times
2^n$, after operating on the rows, we have two matrices of size $2^n \times 2^{n-1}$,
since each row has been downsampled by a factor of 2. Then for each of these two
intermediate matrices, operate on each column, yielding a total of four matrices of
size $2^{n-1} \times 2^{n-1}$. Recall the issues associated with the function
\li{sp.signal.fftconvolve} discussed in the previous lab. Figure \ref{fig:2dwt}
gives a graphical depiction of one iteration of the algorithm.

\begin{figure}[t]
    \includegraphics[width=0.8\textwidth]{2dwt.jpg}
    \caption{The 2-dimensional discrete wavelet transform.}
    \label{fig:2dwt}
\end{figure}

We initialize $LL_0$ to be the
original image matrix, and we terminate once the length of the rows or columns
is less than the length of the filters. We end up with a list of wavelet
coefficients, starting with the final approximation frame $LL_n$ followed by
collections of detail coefficients $(LH_n,HL_n,HH_n)$, $(LH_{n-1},HL_{n-1},HH_{n-1})$,
$\ldots$, $(LH_1,HL_1,HH_1)$. Note that at each iteration we operate first on the
rows (convolve with the filter, then downsample), and the we operate on the columns
of the resulting matrices (\emph{not} the original matrix). The size of the output
matrices have been reduced by a factor of two in both dimensions. As with the
one-dimensional algorithm, to reconstruct the image from the coefficients, we simply
reverse the process by upsampling, convolving, and adding (first the columns, then
the rows). Here is some sample code for one iteration of the transform and the inverse.

\begin{lstlisting}
def dwt2_pass(image,lo_d,hi_d):
	temp = sp.zeros([image.shape[0], image.shape[1]/2])
	LL = sp.zeros([image.shape[0]/2, image.shape[1]/2])
    LH = sp.zeros([image.shape[0]/2, image.shape[1]/2])
	for i in xrange(image.shape[0]):
		temp[i] = sp.signal.fftconvolve(image[i], lo_d, mode='full')[1::2]
	for i in xrange(image.shape[1]/2):
		LL[:,i] = sp.signal.fftconvolve(temp[:,i],lo_d,mode='full')[1::2]
        LH[:,i] = sp.signal.fftconvolve(temp[:,i],hi_d,mode='full')[1::2]
	HL = sp.zeros([image.shape[0]/2, image.shape[1]/2])
    HH = sp.zeros([image.shape[0]/2, image.shape[1]/2])
	for i in xrange(image.shape[0]):
		temp[i] = sp.signal.fftconvolve(image[i], hi_d, mode='full')[1::2]
	for i in xrange(image.shape[1]/2):
		HL[:,i] = sp.signal.fftconvolve(temp[:,i],lo_d,mode='full')[1::2]
        HH[:,i] = sp.signal.fftconvolve(temp[:,i],hi_d,mode='full')[1::2] 	return [LL,LH,HL,HH]

def idwt2_pass(coeffs, lo_r, hi_r):
	LL, LH, HL, HH = coeffs
    n = LL.shape[0]
	temp1 = sp.zeros([2*n,n])
	temp2 = sp.zeros([2*n,n])
	up1 = sp.zeros(2*n)
	up2 = sp.zeros(2*n)
	for i in xrange(n):
		up1[1::2] = HH[:,i]
		up2[1::2] = HL[:,i]
		temp1[:,i] = fftconvolve(up1, hi_r)[1:] + fftconvolve(up2, lo_r)[1:]
		up1[1::2] = LH[:,i]
		up2[1::2] = LL[:,i]
		temp2[:,i] = fftconvolve(up1, hi_r)[1:] + fftconvolve(up2, lo_r)[1:]
	result = sp.zeros([2*n,2*n])
	for i in xrange(2*n):
		up1[1::2] = temp1[i]
		up2[1::2] = temp2[i]
		result[i] = fftconvolve(up1, hi_r)[1:] + fftconvolve(up2, lo_r)[1:]
	return result
\end{lstlisting}

\begin{problem}
Build off of the sample code to fully implement the two-dimensional discrete
wavelet transform as described above.
As in the last lab, the input to your decomposition function should consist of
three arrays: the input image, the low-pass filter, and the high-pass filter.
To increase the flexibility, also include an integer parameter that determines
the level of the decomposition, i.e. how many iterations to perform. Have the
default value of this parameter cause the function to compute the complete
decomposition.
You should return a list of the following form: $[LL_n,[LH_n,HL_n,HH_n],\ldots
,[LH_1,HL_1,HH_1]]$. The reconstruction function should take as input a list
of that same form, as well as the reconstruction low-pass and high-pass filters,
and should return the reconstructed image.
\end{problem}

These wavelet coefficients are very useful in a variety of image processing
tasks. They allow us to analyze and manipulate images in terms of both their
frequency and spatial properties, and at differing levels of resolution.
Furthermore, wavelet bases often have the remarkable ability to represent
images in a very \textit{sparse} manner -- that is, most of the image
information is captured by a small subset of the wavelet coefficients.
In the remainder of this lab, we will see how the discrete wavelet transform
plays a role in edge detection, noise removal, and compression.

\section*{Edge Detection}
It is often useful to identify the edges of objects and figures
represented in images. The edge information can be used to classify images
and group them with other similar images (this is part of a field called
\textit{computer vision}), to segment the image into component parts, to
sharpen blurry images, to filter out unnecessary details of the image,
and so forth. Of course, our human eyes are very adept at recognizing edges,
but enabling a computer to do the same is much more difficult. An edge can
be thought of as a discontinuity in the image or a region of high contrast
in either color or brightness. We can therefore leverage the high-frequency
detail coefficients of the wavelet transform to detect the edges. Once again
using the Haar Wavelet and its associated filters, execute the following
code:
\begin{lstlisting}
import numpy as np
from scipy.misc import imread
from matplotlib import pyplot as plt
from matplotlib import cm

# Get the standard Lenna image for processing
lenna = np.array(imread("Lenna.png",flatten=True),dtype=np.float32)

# Use your 2-dimensional DWT function to calculate one level of coefficients
coeffs = dwt2(lenna,lo_d,hi_d,1)

# Now visualize the original image together with the result
top = np.vstack([coeffs[0]/2.0,np.absolute(coeffs[1][0])])
bottom = np.vstack([np.absolute(coeffs[1][1]),np.absolute(coeffs[1][2])])
plt.imshow(np.hstack([lenna,np.hstack([top,bottom])),cmap=cm.Greys_r)
plt.show()
\end{lstlisting}

Note that the approximation coefficients are very close to the original
image, while the detail coefficients are much more sparse, and roughly
capture the edges in the image. In particular, the upper right coefficients
emphasize the vertical edges, the lower left coefficients emphasize the
horizontal edges, and the lower right coefficients emphasize the diagonal
edges.

\begin{problem}
Now zero out the approximation coefficients and use your inverse DWT
function to recreate the image. Plot its absolute value. This image is
a fairly good representation of the edges. If we add this to the original
image, we can increase the contrast at the edges (that is, make the dark
side darker, and the light side lighter). Do this, and plot the original
image side-by-side with the sharpened image. What do you notice? There
are many image-sharpening techniques, and those based on wavelets
are more sophisticated than what we have done here, but this gives the
basic idea.
\end{problem}

\section*{Noise Removal}
Noise in an image can be defined as unwanted visual artifacts that
obscure the true image. Images can acquire noise from a variety of
sources, including the camera, transmission, and image processing
algorithms. Noise can be completely random and incoherent (as in
Figure \ref{fig:incoherent}), or it can be coherent and display
visual patterns (Figure \ref{fig:coherent}). In this section, we will
focus on reducing a particular type of random noise in images, called
\textit{Gaussian white noise}.

\begin{figure}[t]
\minipage{0.49\textwidth}
    \includegraphics[width=\linewidth]{phantom_random.pdf}
    \caption{The Phantom image with incoherent noise}
    \label{fig:incoherent}
\endminipage\hfill
\minipage{0.49\textwidth}
    \includegraphics[width=\linewidth]{phantom_coherent.pdf}
    \caption{The Phantom image with coherent noise}
    \label{fig:coherent}
\endminipage
\end{figure}

An image that is distorted by Gaussian white noise is one in which
every pixel has been perturbed by a small amount, such that the
perturbations are normally distributed. We can easily add such noise
to an image using the \li{np.random.normal} function.

Given an image with Gaussian white noise, how do we go about reducing
the noise level? Our approach will be based on the idea of thresholding.
As discussed earlier, images are often sparse in the wavelet basis,
particularly in the high-frequency details. The Gaussian noise, however,
is very high frequency, and thus its wavelet transform will be
concentrated in high-frequency wavelet coefficients (of magnitude
roughly proportional to the variance of the noise). We can therefore
reduce the noise while preserving the true image by shrinking the
detail coefficients via hard or soft thresholding.

Given a positive threshold value $\tau$, hard thresholding sets
every wavelet coefficient whose magnitude is less than $\tau$ to
zero, while leaving the remaining coefficients untouched. Soft
thresholding also zeros out all coefficients of magnitude less than
$\tau$, but in addition maps every other coefficient $\beta$ to
$\beta - \tau$ if $\beta > 0$ or $\beta + \tau$ if $\beta < 0$.

Once the coefficients have been thresholded, we take the inverse
wavelet transform to recover the denoised image. The threshold
value is generally a function of the variance of the noise,
and in real situations, we do not know what this variance is. In fact,
noise variance estimation in images is a research area in its own
right, but this goes beyond the scope of this lab, and so we will
assume that we already have a decent estimate of the variance.

\begin{problem}
Write functions that implement the hard and soft thresholding
techniques. The inputs should be a list of wavelet coefficients
in standard form, as well as the threshold value. The output
should be the thresholded wavelet coefficients (also in
standard form). Remember that we only want to threshold the
detail coefficients, and not the approximation coefficients.
You should therefore leave the first entry of the input
coefficient list unchanged.
\end{problem}
\begin{problem}
Create a noisy version of the Lenna image by adding Gaussian
white noise of mean 0 and standard deviation $\sigma = 20$. Compute four
levels of the wavelet coefficients, and input these into your
thresholding functions (with $\tau = 3\sigma$ for the hard threshold,
and $\tau = 3\sigma/2$ for the soft threshold). Reconstruct the
two denoised images, and then plot these together alongside the
noisy image. What do you notice? How does lowering or raising the
threshold affect the reconstructed images?
\end{problem}

\section*{Image Compression}
We now turn to the problem of image compression. Explicitly saving
the value of every pixel in an image can be very costly in both
storage and transmission, and numerous image compression techniques
have been developed over the years to deal with this problem.
Transform methods have long played an important role in these
techniques; the popular JPEG image compression standard is based on
the discrete cosine transform. Starting from the early 1990's, much
research has gone into compression methods using the discrete wavelet
transform, and to great success. The JPEG2000 compression standard
and the FBI Fingerprint Image database, along with other systems,
take the wavelet approach.

The general framework for compression is fairly straightforward. First,
the image to be compressed undergoes some form of preprocessing (this
can include subtracting out its mean, tiling the image, or perhaps
nothing at all). Next, the wavelet coefficients are computed using some
specially constructed wavelet (JPEG2000 uses the either the
Cohen-Daubechies-Feauveau 9/7 or 5/3 wavelet) and then \textit{quantized},
 a process that we will explain shortly. The quantized coefficients are
 then grouped in a particular way and passed through an entropy encoder
 (such as Huffman coding, run length coding, or arithmetic coding). This
 coding step comes from the realm of information theory, and we will not
 worry about it in this lab. What you have left is a compact stream of bits
 that can then be saved or transmitted much more efficiently than the
 original image. All of the above steps are invertible, allowing us to
 reconstruct the image from the bitstream.

 The step in this process that we will focus on is quantization. Put simply,
 quantization is a process whereby the coefficients are converted into
 integers. If the coefficients are floating-point numbers, then this
 process introduces some loss of precision, and we call this \textit{
 lossy compression}. In the situation where all the coefficients are already
 integers, it is possible to compress the image without any loss of precision,
 and this is called \textit{lossless compression}. Quantization can be
 performed in a variety of ways, and we will explore one particular method
 called a uniform null-zone quantizer. Given a coefficient $x$, we assign
 to it an integer $q$ given by
\begin{equation*}
q =
 \begin{cases}
   \lceil x / \delta - t/2 \rceil, &  x \geq 0\\
   \lfloor x / \delta + t/2 \rfloor & x \leq 0
 \end{cases}
\end{equation*}
where $1 \leq t \leq 2$ and $\delta > 0$ are adjustable parameters.
The inverse process, called de-quantization, consists of recovering
the coefficient $y$ from the quantized value $q$ via the equation
\begin{equation*}
 y =
  \begin{cases}
   (q - 1/2 + t/2)\delta & q > 0\\
   (q + 1/2 - t/2)\delta & q < 0\\
   0,                    & q = 0
  \end{cases}
 \end{equation*}
What we are essentially doing is mapping all wavelet coefficients that
fall in the interval $[-\delta,\delta]$ to 0 and all wavelet coefficients
in the interval $[j\delta,(j+1)\delta]$ to $(2j+1)\delta/2$ for integers
$j \geq 1$ and $j \leq -2$. This greatly reduces the number of distinct
coefficient values (indeed, most of the coefficients are mapped to
zero), allowing us, at the cost of some precision, to store less
information. The larger we choose $\delta$, the more compression we
can achieve, albeit with a correspondingly larger loss in precision.
\begin{problem}
Write quantize and dequantize functions based on the discussion above.
In both cases, the inputs should be a list of wavelet coefficients in
standard form, the $\delta$ parameter, and the $t$ parameter with default
value of 2. The functions should return the altered list of wavelet
coefficients.

For the Lenna image, calculate the wavelet coefficients and then
quantize and de-quantize the coefficients, and reconstruct the image.
Do this for a few different values of $\delta$ (with $t=2$), and observe how
the image is distorted. Keep in mind that we are using the Haar wavelet,
which is definitely not well-suited for compression, and so the
distortion will be greater than tolerable in real-life settings.
\end{problem}


\subsection{Introduction}

\begin{frame}
  \frametitle{Integer Difference Logics (\Idl)}

  The \tatoms of \Idl consists of arithmetic
  constraints of the form
  $$
  x - y \leq c
  $$
  where $x, y$ are variables and $c$ is a numerical constant.
  The domain of $x,y,c$ is that of the integers
  \vfill
  \pause
  Notice that the following translations hold
  \begin{itemize}
    \item $x - y \geq c  \quad\quad\Longrightarrow\quad\quad  y - x \leq -c$
    \item $x - y < c     \quad\quad\Longrightarrow\quad\quad  x - y \leq  c-1$
    \item $x - y > c     \quad\quad\Longrightarrow\quad\quad  y - x \leq -c-1$
    \item $x - y = c     \quad\quad\Longrightarrow\quad\quad (x - y \leq c) \wedge (x - y \geq c)$
    \item $x - y \not= c \quad\quad\Longrightarrow\quad\quad (x - y < c)    \vee   (x - y > c)$
  \end{itemize}

\end{frame}

\begin{frame}
  \frametitle{Integer Difference Logic (\Idl)}

  \Rdl is similar to \Idl, but it is defined on the rationals. However
  an \Rdl formula can be reduced to an equisatisfiable \Idl formula
  \vfill
  \pause
  \Idl/\Rdl can be used to encode a large variety of verification probems
  \begin{itemize}
    \item scheduling
    \item TSP
    \item ASP
    \item timed-automata
    \item sorting algorithms
  \end{itemize}
  \vfill
  Also, the worst-case complexity of solving a conjunction of \Idl constraints is $O(m + n \log n)$

\end{frame}


\chapter{Simple Examples}\label{simpleexampleschapter}

\subsection{Peano Arithmetic}\label{peanosection}

This section introduces \emph{Peano representation} of numbers (technically, \emph{Peano numerals}).  The advantage of this representation is that we can use \scheme|==| both to construct and to match against numbers.

The Peano representation of zero is \scheme|`z|, while the immediate successor to a Peano number \mbox{\scheme|n|} is represented as \mbox{\scheme|`(s ,n)|}.  For example, one is the immediate successor of zero---the Peano representation of one is therefore \mbox{\scheme|`(s z)|}.  Two is the immediate successor of one, so the Peano representation of two is \mbox{\scheme|`(s (s z))|}.

Typographically, we indicate a Peano number using corner brackets---for example, \mbox{\scheme|(peano 3)|} for \mbox{\scheme|`(s (s (s z)))|}. We represent \mbox{\scheme|`(s ,x)|} as \mbox{\scheme|(peano+ x 1)|}, \mbox{\scheme|`(s (s ,x))|} as \mbox{\scheme|(peano+ x 2)|}, and so forth, where \scheme|x| is a variable or a reified variable (that is, a symbol).

Here is \scheme|pluso|, which adds two Peano numbers.

\begin{schemedisplay}
(define pluso
  (lambda (n m sum)
    (conde
      ((== (peano 0) n) (== m sum))
      ((exist (x y)
         (== (peano+ x 1) n)
         (== (peano+ y 1) sum)
         (pluso x m y))))))
\end{schemedisplay}

\newpage
\noindent \scheme|pluso| allows us to find all pairs of numbers that sum to six.

\begin{schemedisplay}
(run* (q)
  (exist (n m)
    (pluso n m (peano 6))
    (== `(,n ,m) q))) $\Rightarrow$
\end{schemedisplay}
\nspace
\begin{schemeresponse}
((,(peano 0) ,(peano 6))
 (,(peano 1) ,(peano 5))
 (,(peano 2) ,(peano 4))
 (,(peano 3) ,(peano 3))
 (,(peano 4) ,(peano 2))
 (,(peano 5) ,(peano 1))
 (,(peano 6) ,(peano 0)))
\end{schemeresponse}

Let us define \scheme|minuso| using \scheme|pluso|, and use it to find ten pairs of numbers whose difference is six.

\begin{schemedisplay}
(define minuso
  (lambda (n m k)
    (pluso m k n)))

(run10 (q)
  (exist (n m)
    (minuso n m (peano 6))
    (== `(,n ,m) q))) $\Rightarrow$
\end{schemedisplay}
\nspace
\begin{schemeresponse}
((,(peano 6) ,(peano 0))
 (,(peano 7) ,(peano 1))
 (,(peano 8) ,(peano 2))
 (,(peano 9) ,(peano 3))
 (,(peano 10) ,(peano 4))
 (,(peano 11) ,(peano 5))
 (,(peano 12) ,(peano 6))
 (,(peano 13) ,(peano 7))
 (,(peano 14) ,(peano 8))
 (,(peano 15) ,(peano 9)))
\end{schemeresponse}

We have chosen to have subtraction of a larger number from a smaller number
fail, rather than be zero.

\begin{schemedisplay}
(run* (q) (minuso (peano 5) (peano 6) q)) $\Rightarrow$ `()
\end{schemedisplay}

We will also need \scheme|eveno| and \scheme|positiveo| in several examples below.

\begin{schemedisplay}
(define eveno
  (lambda (n)
    (conde
      ((== (peano 0) n))
      ((exist (m)
         (== (peano+ m 2) n)
         (eveno m))))))

(define positiveo
  (lambda (n)
    (exist (m)
      (== (peano+ m 1) n))))
\end{schemedisplay}

\scheme|eveno| and \scheme|positiveo| ensure that their
arguments represent even and positive Peano numbers, respectively.

\begin{schemedisplay}
(run4 (q) (eveno q)) $\Rightarrow$ `(,(peano 0) ,(peano 2) ,(peano 4) ,(peano 6))

(run* (q) (positiveo q)) $\Rightarrow$ `(,(peano+ '_.0 1))
\end{schemedisplay}

\noindent The value \begin{schemebox}(peano+ '_.0 1)\end{schemebox} shows that
$n + 1$ is positive for every number $n$.


\section{The Coordinate System}\label{sec:3Dcoordinatesystem}

Throughout the text thus far we have focused investigating functions of the form $y=f(x)$, with
one independent and one dependent variable. Such functions can be
represented in two dimensions, using two numerical axes that allow us
to identify every point in the plane with two numbers. We now shift our focus to three-dimensional space; to identify every point in three
dimensions we require three numerical values. The obvious way to make
this association is to add one new axis, perpendicular to the $x$ and
$y$ axes we already understand. We could, for example, add a third
axis, the $z$ axis, with the positive $z$ axis coming straight out of
the page, and the negative $z$ axis going out the back of the
page. This is difficult to work with on a printed page, so more often
we draw a view of the three axes from an angle:

\begin{figure}[H]
$$\vbox{\beginpicture
\normalgraphs
\setcoordinatesystem units <7truemm,7truemm>
\setplotarea x from 0 to 5, y from 0 to 5
\axis left /
\axis bottom /
\plot 0 0 -3 -3 /
\put {$x$} [l] <4pt,0pt> at 5 0
\put {$y$} [b] <0pt,4pt> at 0 5
\put {$z$} [tr] <-4pt,-4pt> at -3 -3
\endpicture
}$$
\end{figure}

You must then imagine that the $z$ axis is perpendicular to the other
two. Just as we have investigated functions of the form $y=f(x)$ in
two dimensions, we will investigate three dimensions largely by
considering functions; now the functions will (typically) have the
form $z=f(x,y)$. Due to the fact that we are used to having the result of a
function graphed in the vertical direction, it is somewhat easier to
maintain that convention in three dimensions. To accomplish this, we
normally rotate the axes so that $z$ points up; the result is then:

\begin{figure}[H]
$$\vbox{\beginpicture
\normalgraphs
\setcoordinatesystem units <6truemm,6truemm>
\setplotarea x from 0 to 5, y from 0 to 6
\axis left /
\axis bottom /
\plot 0 0 -3 -3 /
\setdashes
\plot 4 0 3 -1 /
\plot 3 -1 -1 -1 /
\plot 3 -1 3 4 /
\setdots
\plot 0 5 4 5 3 4 -1 4 0 5 /
\plot -1 4 -1 -1 /
\plot 4 5 4 0 /
\put {$\bullet$} at 3 4
\put {$(2,4,5)$} [tr] <-4pt,-4pt> at 3 4
\put {$y$} [l] <4pt,0pt> at 5 0
\put {$z$} [b] <0pt,4pt> at 0 6
\put {$x$} [tr] <-4pt,-4pt> at -3 -3
\put {$4$} [b] <0pt,3pt> at 4 0
\put {$2$} [br] <-2pt,2pt> at -1 -1
\put {$5$} [r] <-3pt,0pt> at 0 5
\endpicture
}$$
\end{figure}

Note that if you imagine looking down from above, along the $z$ axis,
the positive $z$ axis will come straight toward you, the positive $y$
axis will point up, and the positive $x$ axis will point to your
right, as usual. Any point in space is identified by providing the
three coordinates of the point, as shown; naturally, we list the
coordinates in the order $(x,y,z)$. One useful way to think of
this is to use the $x$ and $y$ coordinates to identify a point in the
$x$-$y$ plane, then move straight up (or down) a distance given by
the $z$ coordinate.

It is now fairly simple to understand some ``shapes'' in three
dimensions that correspond to simple conditions on the coordinates. In
two dimensions the equation $x=1$ describes the vertical line through
$(1,0)$. In three dimensions, it still describes all points with
$x$-coordinate 1, but this is now a plane, as in
Figure~\ref{fig:plane}.

\begin{figure}[H]
\centerline{
\vbox{\beginpicture
\normalgraphs
\setcoordinatesystem units <3truecm,3truecm>
\setplotarea x from 0 to 1.1, y from 0 to 1.1
\put {\hbox{\epsfxsize7cm\epsfbox{images/plane.eps}}} at 0 0
\endpicture}}
\caption{The plane $x=1$. \label{fig:plane}}
\end{figure}

Recall the very useful distance formula in two dimensions which comes directly from the Pythagorean Theorem: the
distance between points $(x_1,y_1)$ and $(x_2,y_2)$ is
$\sqrt{(x_1-x_2)^2+(y_1-y_2)^2}$\index{distance formula}. What is the distance between two points
$(x_1,y_1,z_1)$ and $(x_2,y_2,z_2)$ in three dimensions?
Geometrically, we want the length of the long diagonal labelled $c$ in
the ``box'' in Figure~\ref{fig:3d distance formula}. Since $a$,
$b$, $c$ form a right triangle, $a^2+b^2=c^2$. $b$ is the vertical
distance between $(x_1,y_1,z_1)$ and $(x_2,y_2,z_2)$, so
$b=|z_1-z_2|$.  The length $a$ runs parallel to the $x$-$y$ plane, so
it is simply the distance between $(x_1,y_1)$ and $(x_2,y_2)$, that
is, $a^2=(x_1-x_2)^2+(y_1-y_2)^2$. Now we see that
$c^2=(x_1-x_2)^2+(y_1-y_2)^2+(z_1-z_2)^2$ and
$c=\sqrt{(x_1-x_2)^2+(y_1-y_2)^2+(z_1-z_2)^2}$.

It is sometimes useful to give names to points, for example we might
let $P_1=(x_1,y_1,z_1)$, or more concisely we might refer to the point
$P_1(x_1,y_1,z_1)$, and subsequently use just $P_1$. Distance between
two points in either two or three dimensions is sometimes denoted by
$d$, so for example the formula for the distance between $P_1(x_1,y_1,z_1)$
and $P_2(x_2,y_2,z_2)$ might be expressed as
$$d(P_1,P_2)=\sqrt{(x_1-x_2)^2+(y_1-y_2)^2+(z_1-z_2)^2}.$$

\begin{formulabox}[Distance]
The distance between points  $P_1(x_1,y_1)$ and $P_2(x_2,y_2)$ in two dimensions is
\[
d(P_1,P_2) = \sqrt{(x_1-x_2)^2+(y_1-y_2)^2}
\]

\medskip

The distance between points $P_1(x_1,y_1,z_1)$ and $P_2(x_2,y_2,z_2)$ in three dimensions is \[
d(P_1,P_2)=\sqrt{(x_1-x_2)^2+(y_1-y_2)^2+(z_1-z_2)^2}
\]
\end{formulabox}

\begin{figure}[H]
\centerline{
\vbox{\beginpicture
\normalgraphs
\setcoordinatesystem units <7truemm,7truemm>
\setplotarea x from 0 to 7, y from 0 to 6
\axis left /
\axis bottom /
\plot 0 0 -3 -3 /
\setdashes
\plot 5 2 4 1 /
\plot 4 1 1 1 1 5 4 5 5 6 5 2 /
\plot 4 1 4 5 /
\plot 5 6 2 6 1 5 /
\setdots
\plot 2 6 2 2 1 1 /
\plot 2 2 5 2 /
\setsolid
\plot 4 1 2 2 4 5 /
\put {$a$} [tr] <-2pt,-2pt> at 3 1.5
\put {$b$} [l] <2pt,0pt> at 4 3
\put {$c$} [br] <-2pt,2pt> at 3 3.5
%\put {$(2,4,5)$} [l] <4pt,0pt> at 3 4
\put {$y$} [l] <4pt,0pt> at 7 0
\put {$z$} [b] <0pt,4pt> at 0 6
\put {$x$} [tr] <-4pt,-4pt> at -3 -3
\endpicture}}
\caption{Distance in three dimensions. \label{fig:3d distance formula}}
\end{figure}

In two dimensions, the distance formula immediately gives us the
equation of a circle: the circle of radius $r$ and center at $(h,k)$
consists of all points $(x,y)$ at distance $r$ from $(h,k)$, so the
equation is $r=\sqrt{(x-h)^2+(y-k)^2}$ or $r^2=(x-h)^2+(y-k)^2$. Now
we can get the similar equation $r^2=(x-h)^2+(y-k)^2+(z-l)^2$, which
describes all points $(x,y,z)$ at distance $r$ from $(h,k,l)$, namely,
the sphere with radius $r$ and center $(h,k,l)$.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Opensolutionfile{solutions}[ex]
\section*{Exercises for \ref{sec:3Dcoordinatesystem}}

\begin{enumialphparenastyle}

\begin{ex}
Sketch the location of the points $(1,1,0)$, $(2,3,-1)$,
and $(-1,2,3)$ on a single set of axes.
\end{ex}

\begin{ex}
Describe geometrically the set of points $(x,y,z)$ that
satisfy $z=4$.
\end{ex}

\begin{ex}
Describe geometrically the set of points $(x,y,z)$ that
satisfy $y=-3$.
\end{ex}

\begin{ex}
Describe geometrically the set of points $(x,y,z)$ that
satisfy $x+y=2$.
\end{ex}

\begin{ex}
The equation $x+y+z=1$ describes some collection of points
in $\ds \R^3$. Describe and sketch the points that satisfy $x+y+z=1$ and
are in the $x$-$y$ plane, in the $x$-$z$ plane, and in the
$y$-$z$ plane.
\end{ex}

\begin{ex}
Find the lengths of the sides of the triangle with
vertices $(1,0,1)$, $(2,2,-1)$, and $(-3,2,-2)$.
\begin{sol}
$3$, $\sqrt{26}$, $\sqrt{29}$
\end{sol}
\end{ex}

\begin{ex}
Find the lengths of the sides of the triangle with
vertices $(2,2,3)$, $(8,6,5)$, and $(-1,0,2)$. Why do the results tell
you that this isn't really a triangle?
\begin{sol}
$\sqrt{14}$, $2\sqrt{14}$, $3\sqrt{14}$.
\end{sol}
\end{ex}

\begin{ex}
Find an equation of the sphere with center at $(1,1,1)$ and
radius 2.
\begin{sol}
$(x-1)^2+(y-1)^2+(z-1)^2=4$.
\end{sol}
\end{ex}

\begin{ex}
Find an equation of the sphere with center at $(2,-1,3)$ and
radius 5.
\begin{sol}
$(x-2)^2+(y+1)^2+(z-3)^2=25$.
\end{sol}
\end{ex}

\begin{ex}
Find an equation of the sphere with center $(3,-2,1)$ and
that goes through the point $(4,2,5)$.
\end{ex}

\begin{ex}
Find an equation of the sphere with center at $(2,1,-1)$ and
radius 4. Find an equation for the intersection of this sphere with
the $y$-$z$ plane; describe this intersection geometrically.
\begin{sol}
$(x-2)^2+(y-1)^2+(z+1)^2=16$,
$(y-1)^2+(z+1)^2=12$
\end{sol}
\end{ex}

\begin{ex}
Consider the sphere of radius 5 centered at $(2,3,4)$.  What is
the intersection of this sphere with each of the coordinate planes?
\end{ex}

\begin{ex}
Show that for all values of $\theta$ and $\phi$, the point
$(a\sin\phi\cos\theta,a\sin\phi\sin\theta,a\cos\phi)$ lies on the
sphere given by $x^2+y^2+z^2=a^2$.
\end{ex}

\begin{ex}
Prove that the midpoint of the line segment connecting
$(x_1,y_1,z_1)$ to $(x_2,y_2,z_2)$ is at
$\ds\left({x_1+x_2\over 2},{y_1+y_2\over 2},{z_1+z_2\over 2}\right)$.
\end{ex}

\begin{ex}
Any three points $P_1(x_1,y_1,z_1)$, $P_2(x_2,y_2,z_2)$,
$P_3(x_3,y_3,z_3)$, lie in a plane and form a triangle.  The \dfont{triangle inequality}
says that $ d(P_1,P_3)\le d(P_1,P_2)+d(P_2,P_3)$.  Prove the triangle inequality
using either algebra (messy) or the law of cosines (less messy).
\end{ex}

\begin{ex}
Is it possible for a plane to intersect a sphere in exactly two
points?  Exactly one point? Explain.
\end{ex}

\end{enumialphparenastyle}