\section{Manual labelling of sharp-wave ripples}

In this section, we assess the amount of consensus between the scientists asked to label potential SWR events.

A popular measure to quantify the agreement between two labellers is \emph{Cohen's $\kappa$}. It measures the proportion of events that are given the same label by both labellers (denoted by $p_o$), corrected by the expected proportion of events that are given the same label by chance (denoted by $p_e$). $\kappa \in [-1, 1]$ is defined as:
\begin{equation}
\kappa = \frac{p_o - p_e}{1 - p_e},
\end{equation}
%
where $p_o$ and $p_e$ are estimated as in \cite{McHugh2012}.

\begin{figure}
\img[1.2]{labellers_A}
\captionn{Example of scientist decisions}{Time series are two representative channels of the labelled LFP data. Each cluster of one to five vertical lines marks a candidate event. The colored lines indicate which labellers marked this event as a true SWR event (colors correspond to \cref{fig:upset}). Grey lines indicate events that were marked by no-one as a true SWR event. See \cref{fig:labellers_B} for additional examples.}
\label{fig:labellers_A}
\end{figure}

\begin{figure}
    \begin{subfigure}{0.6\textwidth}
    \img{overlap}
    \end{subfigure}
    \begin{subfigure}{0.5\textwidth}
    \vspace{4em}
    \begin{tabular}{r|l l l l l}
    $N_\text{accepted}$ & FK   & FM   & CK   & DC  & JS  \\ \hline
    Common set          & 111  & 137  & 147  & 78  & 151 \\
    Personal set        & 112  & 101  & 123  & 92  & 111
    \end{tabular} \\[4em]
    \begin{tabular}{r|l l l l}
    $\kappa$ &  FM   & CK    & DC    & JS     \\ \hline
    FK       &  0.71 & 0.62  & 0.64  & 0.57   \\
    FM       &       & 0.76  & 0.45  & 0.67   \\
    CK       &       &       & 0.36  & 0.83   \\
    DC       &       &       &       & 0.34
    \end{tabular}
    \end{subfigure}
\captionn{Labeller agreement}{Left: Agreement between the neuroscientists (denoted by their initials) asked to label potential SWR events. Each row represents the set of events that were given the same label by the neuroscientists that have a colored marker in that row. (Visualised using \cite{Lex2014}). Top right: number of candidate events labelled as `true SWR events' by each neuroscientist, for both the set of events labelled by all neuroscientists (`Common set'), and the sets of events labelled only by one neuroscientist (`Personal set'). Each of these sets contained 200 candidate events in total. Bottom right: Cohen's $\kappa$ for each pair of neuroscientists.}
\label{fig:upset}
\end{figure}


\documentclass[]{report}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{bm}
\usepackage{graphicx}
\usepackage{listings}

\usepackage{hyperref}
\hypersetup{
    colorlinks=true,
    linkcolor=blue,
    filecolor=magenta,
    urlcolor=cyan,
}

\graphicspath{ {images/} }

\title{CSCI 567 HW \# 6}
\author{Mohmmad Suhail Ansari \\ USC ID: 8518586692\\e-mail: mohmmada@usc.edu}

\begin{document}

\maketitle

\paragraph{Sol. 1.1}
	Given
	\[ J = \frac{1}{N} \sum_{i=1}^N {(x_i - p_{i1} e_1 - p_{i2} e_2)}^T (x_i - p_{i1} e_1 - p_{i2} e_2) \]
	Taking the derivative w.r.t $p_{i2}$
	\[ \frac{\partial{J}}{\partial{p_{i2}}} = \frac{1}{N} 2  (- e_2^T) (x_i - p_{i1} e_1 - p_{i2} e_2) = 0\]
	\[ = -e_2^T x_i + p_{i1} e_2^T e_1 + p_{i2} e_2^T e_2 = 0\]

	Since, we know that $e_2^T e_1 = 0$ and $ {\|e_2\|}_2 = e_2^T e_2 = 1$, we get
	\[ = p_{i2} - e_2^T x_i = 0 \]
	\[ p_{i2} = e_2^T x_i \]

\paragraph{Sol. 1.2}
	Given
	\[ \tilde{J} = -e_2^T S e_2 + \lambda_2 {(e_2^T e_2 - 1)}  + \lambda_{12} {(e_2^T e_1 - 0)} \]
	Taking the derivative w.r.t $e_{2}$, we get
	\begin{equation}
		 \frac{\partial{\tilde{J}}}{\partial{e_2}} = -(S + S^T)e_2 + 2 \lambda_2 e_2 + \lambda_{12}e_1 = 0
	\end{equation}
	Since, $S$ is symmetric, i.e. $S = S^T$, we get
	\[ -2Se_2 + 2\lambda_2 e_2 + \lambda_{12} e_1 = 0\]
	multiplying with $e_1^T$ form left, we get
	\[ -2 e_1^T S e_2 + 2 \lambda_2 e_1^T e_2 + \lambda_{12} e_1^T e_1  = 0 \]
	since, $ e_1^T e_2  = 0$ and $e_1^T e_1 = 1$
	\[ -2 {(Se_1)}^T e_2 + \lambda_{12} = 0	\]
	We also know, that ${(Se_1)}^T e_2 = 0$ because $ e_1^T e_2  = 0$, hence
	\[ \lambda_{12} = 0\]
	and therefore from equation (1), we get
	\[ Se_2 = \lambda_2 e_2 \]
	and this proves that the value of $e_2$ which minimizes $\tilde{J}$ is given by the second largest eigen vector of $S$.

\paragraph{Sol. 1.3}
	Using \href{https://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.eig.html}{numpy.linalg.eig}, we get the following eigenvalues
	\[\lambda_1 = 1626.5264,\quad \lambda_2 = 128.9860,\quad \lambda_3 = 7.0974 \]
	and the following eigenvectors
	\[
		v_1 = \begin{bmatrix}
				0.2179\\
				0.4144\\
				0.8835
			\end{bmatrix}
	\]\[
		v_2 = \begin{bmatrix}
			-0.2466 \\
			-0.8525 \\
			0.4607
		\end{bmatrix}
	\]\[
		v_3 = \begin{bmatrix}
			0.9442 \\
			-0.3183\\
			-0.0835
		\end{bmatrix}
	\]

\paragraph{Sol. 1.4}
	Yes, we can omit the direction of the vector $v_3$, since $\lambda_1$ and $\lambda_2$ account for most the information. It can be shown that
	for $\lambda_1$, $\frac{\lambda_1}{\lambda_1 + \lambda_2 + \lambda_2} = 0.9227$, i.e. $\lambda_1$ is account for ~92.28\% of the information and for
	$\lambda_2$, $\frac{\lambda_2}{\lambda_1 + \lambda_2 + \lambda_2} = 0.0731$, i.e. $\lambda_2$ is account for ~7.32\% of the information, which makes $\lambda_3$
	account for only ~0.4\% for the information, and hence can be omitted.

\paragraph{Sol. 1.5}
	For $v_1$, since all the elements of the vector have the same sign, it suggest that all the attributes increase and decrease w.r.t to each other, i.e. for example if the bird has large length, then it will also have large wing span and a higher weight and vice versa. This makes sense since, a large bird will tend to have all three attributes larger than a small bird.

\paragraph{Sol. 2.1}
	To calculate $P(O;\theta)$, we first calculate the following
	\[ \alpha_1(1) = \pi_1 \times b_{1A}  = 0.6 \times 0.4 = 0.24 \]
	\[ \alpha_1(2) = \pi_2 \times b_{2A}  = 0.4 \times 0.2 = 0.08 \]

	\[ \alpha_2(1) = b_{1C} (\alpha_1 (1) a_{11} + \alpha_1(2) a_{21}) = 0.2 \times (0.24 \times 0.7 + 0.08 \times 0.4) = 0.04 \]
	\[ \alpha_2(2) = b_{2C} (\alpha_1 (1) a_{12} + \alpha_1(2) a_{22}) = 0.4 \times (0.24 \times 0.3 + 0.08 \times 0.6) = 0.048 \]

	\[ \alpha_3(1) = b_{1C} (\alpha_2 (1) a_{11} + \alpha_2(2) a_{21}) = 0.2 \times (0.04 \times 0.7 + 0.048 \times 0.4) = 0.00944 \]
	\[ \alpha_3(2) = b_{2C} (\alpha_2 (1) a_{12} + \alpha_2(2) a_{22}) = 0.4 \times (0.04 \times 0.3 + 0.048 \times 0.6) = 0.01632 \]

	\[ \alpha_4(1) = b_{1G} (\alpha_3 (1) a_{11} + \alpha_3 (2) a_{21}) = 0.3 \times (0.00944 \times 0.7 + 0.01632 \times 0.4) = 0.0039408 \]
	\[ \alpha_4(2) = b_{2G} (\alpha_3 (1) a_{12} + \alpha_3(2) a_{22}) = 0.1 \times (0.00944 \times 0.3 + 0.01632 \times 0.6) = 0.0012624 \]

	\[ \alpha_5(1) = b_{1T} (\alpha_4 (1) a_{11} + \alpha_4 (2) a_{21}) = 0.1 \times (0.0039408 \times 0.7 + 0.0012624 \times 0.4) = 0.000326352 \]
	\[ \alpha_4(2) = b_{2T} (\alpha_4 (1) a_{12} + \alpha_4(2) a_{22}) = 0.3 \times (0.0039408 \times 0.3 + 0.0012624 \times 0.6) = 0.000581904 \]

	\[ \alpha_6(1) = b_{1A} (\alpha_5 (1) a_{11} + \alpha_5 (2) a_{21}) = 0.4 \times (0.000326352 \times 0.7 + 0.000581904 \times 0.4) = 0.0001844832 \]
	\[ \alpha_6(2) = b_{2A} (\alpha_5 (1) a_{12} + \alpha_5 (2) a_{22}) = 0.2 \times (0.000326352 \times 0.3 + 0.000581904 \times 0.6) = 0.0000894096 \]
	Finally,
	\[ P(O;\theta) = \alpha_6(1) + \alpha_6(2) = 0.0001844832 + 0.0000894096 = 0.0002738928 \]

\paragraph{Sol. 2.2}
	To calculate $P(X_6 = S_i|O;\theta)$, we first calculate the following
	\[ \beta_6(1) = 1 \]
	\[ \beta_6(2) = 1 \]

	\[ \beta_5(1) = (b_{1A}a_{11}\beta_6(1) + b_{2A}a_{12}\beta_6(2)) =  0.34\]
	\[ \beta_5(2) = (b_{1A}a_{21}\beta_6(1) + b_{2A}a_{22}\beta_6(2)) =  0.28\]

	\[ \beta_4(1) = (b_{1T}a_{11}\beta_5(1) + b_{2T}a_{12}\beta_5(2)) =  0.049 \]
	\[ \beta_4(2) = (b_{1T}a_{21}\beta_5(1) + b_{2T}a_{22}\beta_5(2)) =  0.064 \]

	\[ \beta_3(1) = (b_{1G}a_{11}\beta_4(1) + b_{2G}a_{12}\beta_4(2)) =  0.01221\]
	\[ \beta_3(2) = (b_{1G}a_{21}\beta_4(1) + b_{2G}a_{22}\beta_4(2)) =  0.00972\]

	\[ \beta_2(1) = (b_{1C}a_{11}\beta_3(1) + b_{2C}a_{12}\beta_3(2)) =  0.0028758\]
	\[ \beta_2(2) = (b_{1C}a_{21}\beta_3(1) + b_{2C}a_{22}\beta_3(2)) =  0.0033096\]

	\[ \beta_1(1) = (b_{1C}a_{11}\beta_2(1) + b_{2C}a_{12}\beta_2(2)) =  0.000799764\]
	\[ \beta_1(2) = (b_{1C}a_{21}\beta_2(1) + b_{2C}a_{22}\beta_2(2)) =  0.001024368\]

	\[ P(X_6 = S_1 | O;\theta) = \frac{\alpha_6(S_1) \beta_6(S_1)}{\alpha_6(S_1) \beta_6(S_1) + \alpha_6(S_2) \beta_6(S_2)} \]
	\[  = \frac{0.0001844832 \times 1}{0.0001844832 \times 1 + 0.0000894096 \times 1}\]
	\[ = 0.67355987452 \]

	\[ P(X_6 = S_2 | O;\theta) = 1 - P(X_6 = S_1 | O;\theta) \]
	\[ = 1 - 0.67355987452 = 0.32644012548 \]

\paragraph{Sol. 2.3}
	\[ P(X_4 = S_1 | O;\theta) = \frac{\alpha_4(S_1) \beta_4(S_1)}{\alpha_4(S_1) \beta_4(S_1) + \alpha_4(S_2) \beta_4(S_2)} \]
	\[  = \frac{0.0039408 \times 0.049}{0.0039408 \times 0.049 + 0.0012624 \times 0.064}\]
	\[ = 0.70501743747 \]

	\[ P(X_4 = S_2 | O;\theta) = 1 - P(X_4 = S_1 | O;\theta) \]
	\[ = 1 - 0.70501743747 = 0.29498256253 \]

\paragraph{Sol. 2.4}
	\[ P(X_1 = S_1 | O;\theta) = \frac{0.24 \times 0.000799764}{0.24 \times 0.000799764 + 0.08 \times 0.001024368 } = 0.70079739226 \]
	\[ P(X_1 = S_2 | O;\theta) = 0.29920260773  \]

	\[ P(X_2 = S_1 | O;\theta) = \frac{0.04 \times 0.0028758}{0.04 \times 0.0028758 + 0.048 \times 0.0033096 } = 0.41998913443 \]
	\[ P(X_2 = S_2 | O;\theta) = 0.58001086556 \]

	\[ P(X_3 = S_1 | O;\theta) =  \frac{0.00944 \times 0.01221}{0.00944 \times 0.01221 + 0.01632 \times 0.00972 } = 0.42083033946 \]
	\[ P(X_3 = S_2 | O;\theta) = 0.57916966053\]

	\[ P(X_4 = S_1 | O;\theta) =  0.70501743747\]
	\[ P(X_4 = S_2 | O;\theta) = 0.29498256253  \]

	\[ P(X_5 = S_1 | O;\theta) =  \frac{0.000326352 \times 0.34}{0.000326352 \times 0.34 + 0.000581904 \times 0.28} = 0.40512083559 \]
	\[ P(X_5 = S_2 | O;\theta) = 0.5948791644 \]

	\[ P(X_6 = S_1 | O;\theta) = 0.67355987452 \]
	\[ P(X_6 = S_2 | O;\theta) = 0.32644012548 \]

	So, the most likely sequence is
	\[ S_1 S_2 S_2 S_1 S_2 S_1\]

\paragraph{Sol. 2.4}
	\[ P(O_7 = A | O; \theta) = P(X_6 = S_1| O; \theta) \times b_{1A} +  P(X_6 = S_2| O; \theta) \times b_{2A} \] \[ = 0.67355987452 \times 0.4 + 0.32644012548 \times 0.2 = 0.3347119749\]

	\[ P(O_7 = T | O; \theta) = P(X_6 = S_1| O; \theta) \times b_{1T} +  P(X_6 = S_2| O; \theta) \times b_{2T} \] \[ = 0.67355987452 \times 0.1 + 0.32644012548 \times 0.3 = 0.16528802509\]

	\[ P(O_7 = G | O; \theta) = P(X_6 = S_1| O; \theta) \times b_{1G} +  P(X_6 = S_2| O; \theta) \times b_{2G} \] \[ = 0.67355987452 \times 0.3 + 0.32644012548 \times 0.1 = 0.2347119749\]

	\[ P(O_7 = C | O; \theta) = P(X_6 = S_1| O; \theta) \times b_{1C} +  P(X_6 = S_2| O; \theta) \times b_{2C} \] \[ = 0.67355987452 \times 0.2 + 0.32644012548 \times 0.4 = 0.26528802509\]

	 \[ O_7 = argmax_O P(O|O; \theta) = A \]
\end{document}


\section{Carrier-Greenspan periodic solution}

Periodic solutions for flows on a sloping beach were proposed by Carrier and Greenspan~\cite{CG1958}. The solutions have been widely used to test the performance of numerical methods used to solve the shallow water equations~\cite{Johns1982,MR2012CG}.

This test can be described in dimensional and dimensionless equations. For our reference, please note that dimensional quantities shall be denoted by starred variables, while dimensionless quantities by unstarred variables for brevity of our analytical presentation. This notational convention is used only in this test.

The problem is set up as follows. Consider a one dimensional domain through the $x^*$-axis. Recall the shallow water equations
\begin{equation} \label{eq:mass_app}
h^*_{t^*} +\left(h^*u^*\right)_{x^*}=0\,,
\end{equation}
\begin{equation} \label{eq:mom_app}
\left(h^*u^*\right)_{t^*}+\left(h^*{u^*}^2+\frac{1}{2}g{h^*}^2\right)_{x^*}=-gh^* z^*_{x^*}\,.
\end{equation}
Here,
$x^*$ represents the one-dimensional domain,
$t^*$ is the time variable,
$u^*=u^*(x^*,t^*)$ represents the velocity,
$z^*=z^*(x^*)$ denotes the water bed topography (elevation),
$h^*=h^*(x^*,t^*)$ denotes the height (water depth), that is, the distance from the free surface to the water bed topography, and
$g$ is the acceleration due to gravity.
Now, consider the situation on a sloping beach. The topography changes linearly with $x^*$
\begin{equation}
z^*=(h_0^*/L^*)x^*-h_0^*\,,
\end{equation}
in which $h_0^*$ is the vertical distance from the origin $O$ to the topography at any time, and $L^*$ is the horizontal distance from the origin $O$ to the topography when the water is still. This implies that when the water is still: $z^*=-h^*$ over the spatial domain, $z^*=-h_0^*$ at $x^*=0$\,, and the position of the shoreline is $x^*=L^*$\,. More detailed descriptions are given by Mungkasi and Roberts~\cite{MR2012CG}.

The free surface or called stage is defined by $w^*:=h^*+z^*$\,.
Scaling the horizontal distance by $L^*$\,, the vertical distance by $h_0^*$\,, the time by $L^*/\sqrt{gh_0^*}$\,, and the velocity by $\sqrt{gh_0^*}$\,, the nonconservative dimensionless shallow water equations can be expressed as
\begin{equation} \label{eq:mass1_app}
w_t + \left[ \left( w+1-x  \right)u  \right]_{x} = 0\,,
\end{equation}
\begin{equation} \label{eq:mom1_app}
u_t + u u_x + w_{x} = 0\,.
\end{equation}
For smooth solutions, equations (\ref{eq:mass1_app}) and (\ref{eq:mom1_app}) are equivalent to the conservative dimensionless shallow water wave equations
\begin{equation}
h_t + \left( hu \right)_x = 0\,,
\end{equation}
\begin{equation}
\left(hu\right)_t + \left( hu^2 + \frac12 h^2  \right)_x = -h z_x\,.
\end{equation}

Carrier and Greenspan showed that
\begin{equation} \label{eq:w_johns_full}
w = - \frac12 u^2 + \mathcal{A} J_0 \left( \frac{4 \pi \sqrt{ w+1-x}}{T} \right)
\cos{\left(\frac{2 \pi \left( u+t \right)}{T}\right)}\,,
\end{equation}
\begin{equation} \label{eq:u_johns_full}
u = - \frac{\mathcal{A}J_1\left( \frac{4 \pi \sqrt{w+1-x}}{T}  \right)}{\sqrt{w+1-x}}
 \sin{\left( \frac{2 \pi \left(u+t \right)}{T}  \right)}\,
\end{equation}
satisfies the shallow water equations. This was verified by Johns~\cite{Johns1982} as well as Mungkasi and Roberts~\cite{MR2012CG}. Equations (\ref{eq:w_johns_full}) and (\ref{eq:u_johns_full}) are the Carrier--Greenspan periodic solutions for flows on a sloping beach, which are written in the dimensionless form. Obviously, this can be rescalled back to the dimensional form.

Because this solution is periodic, the initial condition can be set by substituting $t=0$ into the analytical solution (\ref{eq:w_johns_full}) and (\ref{eq:u_johns_full}).

\subsection{Results}

We consider a spatial domain given by the interval $[-50, 55050]$\,. The dimensional length is $L^*=50,000$\,, dimensional height $h_0^*=500$\,, and dimensional period $T^*=900$\,. At $x^*=0$ the dimensional amplitude is $\epsilon^*=1.0$\,. After four cycles, periodic motions are clear.

The following figures show the stage, $x$-momentum, and $x$-velocity at several instants of time through a cross-section of the domain. Perturbation at the zero point of the spatial domain is also shown.
We should see excellent agreement between the analytical and numerical solutions.

\begin{figure}
\begin{center}
\includegraphics[width=0.9\textwidth]{stage_plot.png}
\end{center}
\caption{Stage results}
\end{figure}

\begin{figure}
\begin{center}
\includegraphics[width=0.9\textwidth]{xmom_plot.png}
\end{center}
\caption{Xmomentum results}
\end{figure}

\begin{figure}
\begin{center}
\includegraphics[width=0.9\textwidth]{xvel_plot.png}
\end{center}
\caption{Xvelocity results}
\end{figure}

\begin{figure}
\begin{center}
\includegraphics[width=0.9\textwidth]{perturbation_at_origin.png}
\end{center}
\caption{Perturbation at the origin}
\end{figure}

\endinput


\section{Convergence of Sequences and Series}
\subsection{Sequence}
\begin{definition}
    Let $X$ be a set, then a sequence in $X$ is a function $f:\mathbb N\to X$.
\end{definition}
We denote the sequence in the form $a_n=f(n)$.
\begin{example}
    1. The constant sequence $f\equiv a$ for some constant $a\in X$.\\
    2. The sequence $f(n)=\sqrt{n^2+17}$ is a sequence in positive real numbers.\\
    3. Flip a coin.
    The sequence
    $$a_n=\begin{cases}
        1\text{, if the $n^{th}$ flip is head}\\
        0\text{, if it is tail.}
    \end{cases}$$
    Then $a_n$ is a sequence in $\{0,1\}$.\\
    4. With last example, we can define $b_1=0,b_{n+1}=b_n+a_n$ makes a sequence $b_n$ in $\mathbb Z$.
\end{example}
\begin{definition}
    If $(z_n)$ is a sequence of complex numbers, we say $z_n\to z$ (or $z_n$ converges to $z$) as $n\to\infty$ if
    $$\forall\epsilon>0,\exists N\in\mathbb N,\forall n>N,|z_n-z|<\epsilon$$
    Otherwise, we say $(z_n)$ diverges.
\end{definition}
Note that $N$ depends on $\epsilon$.
\begin{example}
    1. The constant sequence $z_n=z$ converges to $z$.\\
    2. The sequence $z_n=1/n$ converges to $0$.
    Given $\epsilon>0$ we can find $N$ such that $N>1/\epsilon$ since $\mathbb N$ has no upper bound, then whenever $n>N$, we have $|z_n-z|=|1/n|<1/N<\epsilon$.\\
    3. The sequence $z_n=n$ diverges.\\
    4. The sequence $z_n=(-1)^n$ diverges.
\end{example}
\begin{proposition}
    $z_n\to z$ iff:\\
    1. $z_n-z\to 0$.\\
    2. $|z_n-z|\to 0$.\\
    3. $\forall m\in N,\exists N\in\mathbb N, \forall n>N,|z_n-z|<1/m$.
\end{proposition}
\begin{proposition}
    Suppose $z_n$ and $w_n$ are sequences in $\mathbb C$.
    If $z_n\to z,w_n\to w$, then\\
    1. $z_n+w_n\to z+w$.\\
    2. $z_nw_n\to zw$.\\
    3. If $z\neq 0$, then $\exists N\in\mathbb N,\forall n>N,z_n\neq 0$ and for $n>N,1/z_n\to 1/z$.\\
    4. If $z_n=x_n+iy_n,z=x+iy$ with $x_n,x,y_n,y\in\mathbb R$, we have $z_n\to z\iff x_n\to x\land y_n\to y$.
\end{proposition}
\begin{proof}
    Trivial.
\end{proof}
\begin{example}
    1. $1/(n^2)=(1/n)(1/n)\to 0\cdot 0=0$.\\
    2. $1\pm1/n^2\to 1\pm0=1$.\\
    3. $1/(1\pm 1/n^2)\to 1/1=1$.\\
    4. $(n^2-n)/(n^2+n)=(1+1/n)^{-1}(1-1/n)\to 1\cdot 1=1$.
\end{example}
\begin{remark}
    2 implies that if $z_n\to z$ then $cz_n\to cz$.
    Similarly 2,3 together show that if $z_n\to z,w_n\to w\neq 0$, then $z_n/w_n$ is eventually well-defined and goes to $z/w$.
\end{remark}
\begin{corollary}[Uniqueness of Limit]
    If $z_n\to z$ and $z_n\to z'$, then $z=z'$
\end{corollary}
\begin{proof}
    By 1 and 2, $0\equiv z_n+(-1)z_n\to z+(-1)z'=z-z'$, but if $0\to w\neq 0$, then $|w|>0$, therefore there is some $n$ such that $|w|=|w-0|<|w|/2<|w|$, contradiction.
    So $z=z'$.
\end{proof}
We sometimes write
$$\lim_{n\to\infty}z_n=z$$
if $z_n\to z$.
We can write it so since limits are unique.
\begin{proposition}
    Suppose $x_n\to x,y_n\to y$ are real sequences, then if $\forall n,x_n\ge y_n$, then $x>y$.
\end{proposition}
Note that it is not true if we replace $\ge$ by $>$.
\begin{proof}
    It suffices to prove the case when $y_n\equiv 0$.\\
    If $x<0$, then there is a natural number $n$ such that $|x_n-x|<|x|$, but $|x_n-x|\ge|x|$, which is a contradiction.\\
    For the general case, just consider the sequence $x_n-y_n$.
\end{proof}
\begin{proposition}[Squeeze Rule]
    If $x_n,c_n,y_n$ are real sequences with $\forall n,x_n\ge c_n\ge y_n$ and $x_n\to c,y_n\to c$, then $c_n\to c$.
\end{proposition}
\begin{proof}
    Given $\epsilon>0$, pick $N_1$ such that $\forall n>N_1,|x_n-c|<\epsilon$ and $N_2$ such that $\forall n>N_2,|y_n-c|<\epsilon$, so $\forall n>\max\{N_1,N_2\}$. we have
    $$c+\epsilon>x_n\ge c_n\ge y_n>c-\epsilon$$
    so $|c_n-c|<\epsilon$.
\end{proof}
\begin{example}
    Since
    $$\frac{n^2-1}{n^2}\le\frac{n^2+\sin n}{n^2}\le\frac{n^2+1}{n^2}$$
    we have $(n^2+\sin n)/n^2\to 1$.
\end{example}
\begin{definition}
    Let $x_n$ be a sequence in $\mathbb R$.
    We say it is monotone increasing if $x_{n+1}\ge x_n$ for all $n\mathbb N$.
\end{definition}
\begin{definition}
    A sequence $x_n$ is bounded above if there is some constant $c\in\mathbb R$ such that $x_n\ge c$ for any $n$.
\end{definition}
\begin{theorem}[Monotone Sequence Theorem]
    A monotone increasing sequence in $\mathbb R$ that is bounded above converges.
\end{theorem}
Note that this theorem is false if we replace $\mathbb R$ by $\mathbb Q$
\begin{proof}
    Let $x_n$ be such a sequence and we let $c=\sup_{n\in\mathbb N}x_n$.
    $c$ exists since $x_n$ is bounded above.
    We shall show that $x_n\to c$.\\
    If $x_n$ does not tend to $c$, then there is some $\epsilon>0$ such that $\forall N\in\mathbb N,\exists n>N,c-x_n=|x_n-c|\ge\epsilon$.
    However, this would mean that for any $N\in\mathbb N$, we can choose such an $n$, then it gives $x_N\le x_n\le c-\epsilon$, so $c$ is not the least upper bound, contradiction.
\end{proof}
\begin{definition}
    Let $(x_n)$ be a sequence in $\mathbb R$, we say $x_n\to\infty$ if $\forall M\in\mathbb R,\exists N\in\mathbb N,\forall n>N,x_n>M$.
\end{definition}
Note that a sequence that tends to infinity diverges.
\begin{proposition}
    If the sequence $(x_n)$ is monotone increasing, then it either converges or tends to infinity.
\end{proposition}
\subsection{Series}
\begin{definition}
    We say the infinite series
    $$\sum_{i=0}^\infty c_i$$
    converges (diverges) if the sequence $s_n=\sum_{i=0}^nc_i$ converges (diverges).\\
    If $s_n$ converges to $s$, we write
    $$\sum_{i=0}^\infty c_i=s$$
\end{definition}
\begin{example}
    Consider the geometric series $\sum_{k=0}^\infty z^k$ where $|z|<1$.
    So $s_n=(1-z^{n+1})/(1-z)$.
    Note that $z_{n+1}\to 0$ as $n\to\infty$, hence $s_n\to 1/(1-z)$.
\end{example}
\begin{lemma}
    If the series sum $\sum_{i=0}^\infty c_i$ converges, then $c_i\to 0$ as $i\to\infty$.
\end{lemma}
Note that the converse of the statement is false.
\begin{proof}
    Trivial.
\end{proof}
\begin{lemma}
    Suppose $(a_n),(b_n)$ are sequences in $\mathbb C$,\\
    1. For any $K_1,K_2\in\mathbb N$, $\sum_{i=K_1}^\infty a_n$ cconverges iff $\sum_{i=K_2}^\infty a_n$ converges.\\
    2. Suppose $\sum_na_n\to a,\sum_nb_n\to b$, then $\sum_n(a_n+\lambda b_n)\to a+\lambda b$.
\end{lemma}
\begin{proof}
    Obvious.
\end{proof}
\begin{theorem}[Comparison Test]
    Suppose $(a_n),(b_n)$ are sequences in $\mathbb R$, and $a_n\ge b_n\ge 0$ for all $n$, then if the series $\sum_ia_i$ converges, so does $\sum_ib_i$, and $\sum_ia_i\ge\sum_ib_i\ge 0$.
\end{theorem}
\begin{proof}
    Indubitable.
    \footnote{The author is short of such sort of adjectives.}
\end{proof}
\begin{definition}
    Let $(c_n)$ be a complex sequence, we say $\sum_ic_i$ converges absolutely if $\sum_i|c_i|$ converges.
\end{definition}
\begin{lemma}
    If $(c_n)\in\mathbb R$, then if $\sum_{i}|c_i|$ converges, then $\sum_ic_i$ converges.
\end{lemma}
\begin{proof}
    Just sum up $c_n+|c_n|$ and use Comparison Test.
\end{proof}
\begin{theorem}
    If $(c_n)\in\mathbb C$ converges absolutely, then it converges.
\end{theorem}
\begin{proof}
    Split real and imaginary part and use Comparison Test.
\end{proof}
Alternatively one can also use triangle inequality.
\begin{theorem}[Strong Comparison Test]
    Suppose $(c_n)\in\mathbb C$ and $(a_n)\in\mathbb R_+$ with $a_n\ge |c_n|\ge 0$.
    Then if $\sum_na_n$ converges, then $\sum_nc_n$ converges absolutely.
\end{theorem}
\begin{proof}
    Analogous.
\end{proof}
\begin{theorem}[Ratio Test]
    Suppose $(c_n)\in\mathbb C$ such that
    $$[0,\infty]\ni r=\lim_{n\to\infty}\left|\frac{c_{n+1}}{c_n}\right|$$
    exists, then if $r<1$, $\sum_{n}c_n$ converges, and if $r>1$, $\sum_nc_n$ diverges.
\end{theorem}
If $r=1$, then both ways are possible.
\begin{example}
    $$\sum_{n=0}^\infty\frac{z^n}{n!}$$
    where $z$ is a complex number.
    This series converges absolutely since
    $$\lim_{n=\to\infty}\frac{|z|^{n+1}n!}{|z|^n(n+1)!}=\lim_{n\to\infty} \frac{|z|}{n+1}\to 0<1$$
    by Ratio Test.
    So we can really define $\exp(z)$.
\end{example}
\begin{proof}
    If $r>1$, then we can find $\eta$ such that $1<\eta<r$, and $\exists N\in\mathbb N,\forall n>N$ we have $|c_{n+1}|>\eta|c_n|$, therefore $|c_n|\to\infty$, hence the series diverges.\\
    If $r<1$, then we can find $\eta$ such that $r<\eta<1$ and $\exists N\in\mathbb N,\forall n>N,|c_{n+1}|\le\eta|c_n|$, so a comparison test with the geometric series shows the convergence.
\end{proof}
\begin{example}[Non-examples]
    Consider the harmonic series $\sum_n1/n$.
    The ratio test is useless here as $(n+1)/n\to 1$ as $n\to\infty$.
    The sequence of the terms converges as well, so we cannot get anything from there either.
    And in fact, it diverges.
    We can compare it with the sequence $1+1/2+1/4+1/4+1/8+1/8+1/8+1/8+\cdots$ to show that it is indeed actually diverges.
\end{example}
\begin{proposition}[Cauchy Condensation Test]
    Suppose $(a_n)\in\mathbb R$ and $a_n\ge a_{n+1}\ge 0$ for any $n$.
    Then $\sum_na_n$ converges iff $\sum_{k}2^ka_{2^k}$ converges.
\end{proposition}
I'd like to think that we can replace $2$ by any natural number $p\ge 2$.
\begin{example}
    $\sum_nn^{-p}$ converges iff $\sum_k2^k2^{-kp}=\sum_k(2^{1-p})^k$ converges iff $p>1$.
\end{example}
\begin{proof}
    Comparison.
\end{proof}
\begin{definition}
    Suppose $(x_n)$ is a sequence in some set $X$.
    A subsequence of $x_n$ is a sequence of the form $x_{n_k}$ where $(n_k)_{k\in\mathbb N}$ is strictly increasing.
\end{definition}
\begin{lemma}
    Suppose $c_n$ is a sequence in $\mathbb C$ and $c_{n_k}$ is a subsequence, then if $c_n\to c$, then $c_{n_k}\to c$
\end{lemma}
\begin{proof}
    Immediate.
\end{proof}
The converse is obviously not true.
\begin{lemma}
    If $(a_n)\in\mathbb R$ is monotone and a subsequence of it converges $a_{n_k}\to a$, then $a_n\to a$.
\end{lemma}
\begin{proof}
    Easy.
\end{proof}
\subsection{Bolzano-Weierstrass}
\begin{definition}
    Let $(c_n)$ be a sequence of complex numbers.
    We say $(c_n)$ is bounded if $\exists M>0,\forall n\in\mathbb N,|c_n|<M$.
\end{definition}
\begin{theorem}[Bolzano-Weierstrass]
    Any bounded sequence has a converging subsequence.
\end{theorem}
This is false in $\mathbb Q$ as one will expect.\\
We shall prove this by ``lion-hunting''.
The philosophy is like this:
Given an infinite sequence of lions in a rectangular zone.
We divide the rectangle by halving each side, then one of the four parts shall contain infinitely many lions.
Do this again on the region having infinitely many lions.
And do this again and again and again so there is a sequence of rectangles $A_0\subset A_1\subset A_2\subset\cdots$ such that each $A_n$ contains infinitely many lions and that the diameters of $A_n$ decreases (exponentially) to $0$ when $n\to\infty$.
So choosing a lion in each $A_n$ (such that its index is higher than the previous lion, possible since each $A_n$ is infinite) gives a subsequence of converging lions.
\begin{proof}
    Suffices to show the real case.
    Take $a_0=-M,b_0=M$, then $[a_0,b_0]$ contains infinitely many terms of the sequence.
    Once we have chosen $a_n,b_n$ such that $[a_n,b_n]$ contains infinitely many terms of the sequence, at least one of $[a_n,(a_n+b_n)/2],[(a_n+b_n)/2,b_n]$ must contain infinitely many terms of the sequence.
    For the former case we set $a_{n+1}=a_n,b_{n+1}=(a_n+b_n)/2$ and for the latter $a_{n+1}=(a_n+b_n)/2,b_{n+1}=b_n$.
    Then $b_{n+1}-a_{n+1}=(b_n-a_n)/2=2M/2^{n+1}\to 0$ as $n\to\infty$.
    Also both $b_n,a_n$ converges as monotone sequences.
    Then we can choose $x_{n_k}$ inductively:
    Choose $x_{n_0}\in [a_0,b_0]$, then once we have chosen $x_{n_k}$, we can choose $n_{k+1}$ by choosing one such that $x_{n_{k+1}}\in [a_{k+1},b_{k+1}]$ and $n_{k+1}>n_k$, which is possible as $[a_{k+1},b_{k+1}]$ contains infinitely many terms.
    So $x_{n_k}$ is squeezed by $a_k,b_k$ which converges to the same limit, hence this subsequence converges.
\end{proof}
\begin{definition}
    Let $(z_n)\in\mathbb C$ be a sequence in $\mathbb C$, we say $z_n$ is Cauchy if $\forall\epsilon>0,\exists N\in\mathbb N,\forall n,m>N,|x_n-x_m|<\epsilon$.
\end{definition}
\begin{proposition}
    A complex sequence converges if and only if it is Cauchy.
\end{proposition}
\begin{proof}
    The ``only if'' part is obvious.
    For the other direction, it is easy that Cauchy sequences are bounded and that if it has a subsequence that converges, then it converges to the same value.
    Then Bolzano-Weierstrass suffices.
\end{proof}
\begin{corollary}
    Let $(z_n)\in\mathbb C$, then $\sum_nz_n$ converges iff $\forall\epsilon>0,\exists N\in\mathbb N,\forall n>m>N$,
    $$\left|\sum_{k=m+1}^nz_k\right|<\epsilon$$
\end{corollary}
\begin{proof}
    Immediate.
\end{proof}
\begin{proposition}[Alternating Series Test]
    Suppose $(a_n)\in\mathbb R$ and $a_n\ge a_{n+1}\ge 0$ and $a_n\to 0$, then
    $$\sum_{n=0}^\infty(-1)^na_n$$
    converges.
\end{proposition}
\begin{example}
    So $\sum_n(-1)^n/n$ converges even if the harmonic series diverges.
\end{example}
\begin{proof}
    Let $s_n$ be the $n^{th}$ partial sum, then $s_{2n+1}=s_{2n-1}+a_{2n}-a_{2n+1}\ge s_{2n-1}$ is monotone increasing and bounded above by $a_0$, so it converges.
    Also $s_{2n}$ converges as well and we can squeeze it.
\end{proof}


% This is part of the TFTB Reference Manual.
% Copyright (C) 1996 CNRS (France) and Rice University (US).
% See the file refguide.tex for copying conditions.

\markright{tfrridt}
\section*{\hspace*{-1.6cm} tfrridt}

\vspace*{-.4cm}
\hspace*{-1.6cm}\rule[0in]{16.5cm}{.02cm}
\vspace*{.2cm}

{\bf \large \sf Purpose}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
Reduced Interference Distribution with triangular kernel.
\end{minipage}
\vspace*{.5cm}

{\bf \large \sf Synopsis}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
\begin{verbatim}
[tfr,t,f] = tfrridt(x)
[tfr,t,f] = tfrridt(x,t)
[tfr,t,f] = tfrridt(x,t,N)
[tfr,t,f] = tfrridt(x,t,N,g)
[tfr,t,f] = tfrridt(x,t,N,g,h)
[tfr,t,f] = tfrridt(x,t,N,g,h,trace)
\end{verbatim}
\end{minipage}
\vspace*{.5cm}

{\bf \large \sf Description}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
        Reduced Interference Distribution with a kernel based on the
        triangular (or Bartlett) window.  {\ty tfrridt} computes either the
        distribution of a discrete-time signal {\ty x}, or the cross
        distribution between two signals. This distribution has the
        following expression :
%\begin{multline*}
\begin{eqnarray*}
RIDT_x(t,\nu)&=&\int_{-\infty}^{+\infty}
h(\tau)\,R_x(t,\tau)\,e^{-j2\pi\nu\tau}\ d\tau\\
{\rm with}\quad
R_x(t,\tau)&=&
\int_{-\frac{|\tau|}{2}}^{+\frac{|\tau|}{2}}
\frac{2\,g(v)}{|\tau|}(1-\frac{2|v|}{|\tau|})\
x(t+v+\frac{\tau}{2}) x^*(t+v-\frac{\tau}{2})\,dv.
\end{eqnarray*}
%\end{multline*}

\hspace*{-.5cm}\begin{tabular*}{14cm}{p{1.5cm} p{8cm} c}
Name & Description & Default value\\
\hline
        {\ty x}     & signal if auto-RIDT, or {\ty [x1,x2]} if cross-RIDT ({\ty
			Nx=length(x)})\\
        {\ty t}     & time instant(s)          & {\ty (1:Nx)}\\
        {\ty N}     & number of frequency bins & {\ty Nx}\\
        {\ty g}     & time smoothing window, {\ty G(0)} being forced to {\ty 1}, where {\ty G(f)} is the Fourier transform of {\ty g(t)}
                                         & {\ty window(odd(N/10))}\\
        {\ty h}     & frequency smoothing window, {\ty h(0)} being forced to {\ty 1}
                                         & {\ty window(odd(N/4))}\\
        {\ty trace} & if nonzero, the progression of the algorithm is shown
                                         & {\ty 0}\\
     \hline {\ty tfr}   & time-frequency representation \\
        {\ty f}     & vector of normalized frequencies\\

\hline
\end{tabular*}
\vspace*{.2cm}

When called without output arguments, {\ty tfrridt} runs {\ty tfrqview}.
\end{minipage}

\newpage

{\bf \large \sf Example}
\begin{verbatim}
         sig=[fmlin(128,0.05,0.3)+fmlin(128,0.15,0.4)];
         g=window(31,'rect'); h=window(63,'rect');
         tfrridt(sig,1:128,128,g,h,0);
\end{verbatim}
\vspace*{.5cm}

{\bf \large \sf See Also}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
all the {\ty tfr*} functions.
\end{minipage}
\vspace*{.5cm}

{\bf \large \sf Reference}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
[1] J. Jeong, W. Williams ``Kernel Design for Reduced Interference
Distributions'' IEEE Trans. on Signal Proc., Vol. 40, No. 2, pp. 402-412,
Feb. 1992.
\end{minipage}


    \documentclass{article}
    \usepackage[utf8]{inputenc}
    \usepackage{amsmath}
    \begin{document}
 %Title
 \section{Collatz Sequence for \(n=10\) }
    %Collatz Function
    \[
        f(n)=
        \begin{cases}
        \frac{n}{2}, & n \mod 2=0
        \\
        3n+1, &n \mod 2=1
        \end{cases} \\
    \]
    % Path for given N
    \(\textbf{Path for f(10)}\\[3mm]f(n), n=10
 \\
 \Rightarrow \frac{10}{2}
 \\
 \Rightarrow n=5
 \\[3mm]
f(n), n=5
 \\
 \Rightarrow 3(5) + 1
 \\
 \Rightarrow n=16
 \\[3mm]
f(n), n=16
 \\
 \Rightarrow \frac{16}{2}
 \\
 \Rightarrow n=8
 \\[3mm]
f(n), n=8
 \\
 \Rightarrow \frac{8}{2}
 \\
 \Rightarrow n=4
 \\[3mm]
f(n), n=4
 \\
 \Rightarrow \frac{4}{2}
 \\
 \Rightarrow n=2
 \\[3mm]
f(n), n=2
 \\
 \Rightarrow \frac{2}{2}
 \\
 \Rightarrow n=1
 \\[3mm]

    \section{Credits}
    %Maybe add link to gitrepo and other shit, idk
    This is created using Collatzer (https://github.com/Z1aaan/Collatzer).
    Created By: Z1aaan

    README:
    A program created to visualize and simulate a user-given value for \textit{N}
    and see what happens when it is put under the Collatz function.
    \end{document}


% !TeX root = ../notes.tex

\section{Textbook Query Optimization}
Textbook query optimization involves techniques to perform a rough first optimization, which however is quite simple. There is a series of steps to translate raw SQL into logical and physical plans, each of them transforming input in a more optimal form.

The output is going to be \textit{executable}, but still to be improved by non-trivial methods.

\subsection{Algebra and tuples}
Plain relational algebra is not sufficient itself: it needs to be revisited ensuring \textbf{correctness} (producing the same result) within a formal model. The most relevant problem to tackle is \textbf{deciding whether two algebraic expressions are the same}, but this in difficult in practice.

For instance, performing a selection before a join might be correct (and faster) in case the considered criterion is equality, but can give a different result than selecting after an outer join.

To remedy this issue, it is possible to guarantee that two expressions are equivalent, not accepting false positing yet allowing false negatives.

A formal definition of \textbf{tuple} is an unordered mapping from attribute names to values of a domain. A schema consists in a set of attributes with domain $A(t)$.

Tuple operations are:
\begin{itemize}
	\item \textbf{Concatenation}, attaching one tuple to another regardless of ordering (union);
	\item \textbf{Projection}, producing a notation $t.a$ in which it is possible to access single values or multiple $t_{|\{a, b\}}$, getting a subset of the schema.
\end{itemize}

A set of tuples with the same schema forms a \textbf{relation}. Sets naturally do not comply with real data, since they not allow duplicates, but are used for simplicity.

In most cases, sets and bags can be used interchangeably, but the optimizer considers different semantics: logical algebra operates on \textit{bags}, physical algebra on \textit{streams} and sets are only considered after an \textit{explicit duplicate elimination}.

\begin{figure}
	\begin{minipage}{0.45\textwidth}
		\hspace{-2mm}
		\includegraphics[width=0.83\textwidth]{equivalences_1.png}
	\end{minipage}
	\begin{minipage}{0.5\textwidth}
		\hspace{-10mm}
		\includegraphics[width=1.2\textwidth]{equivalences_2.png}
	\end{minipage}
	\vspace{-15pt}
\end{figure}

Set operations are the classic ones of union, intersection and difference, yet are \textbf{subject to schema constraints}. On bags, operations are performed on frequencies.

There are also free variables, which first must be bounded to be evaluated: they are essentials for predicates and algebra expressions, such as dependent joins.

It is important to note that projection removes duplicates within sets, while keeping them in bags.

There are equivalences for selection and projection useful to derive whether a different ordering produces the same output. For instance, applying selection twice is the same as applying it once with two criteria plus an AND. Commutative property also holds.

\subsection{Canonical Query Translation}
The canonical query translation transforms SQL into \textbf{algebra expressions}. The first approach involves some restrictions: it assumes no duplicates without aggregation and set operations.

The first step is translating the FROM clause:
$$F = \begin{cases}
R_1 & k = 1 \\
((\dots (R_1 \times R_2) \times \dots) \times R_k)) & \text{else}
\end{cases}$$
In short all relations are joined through a cross product. The next step is translating the WHERE clause:
$$W = \begin{cases}
F & \text{there is no WHERE clause} \\
\sigma_p(F) & \text{otherwise}
\end{cases}$$
The SELECT clause is translated starting from the projection $a_1,\ \dots,\ a_n$ or $*$. The expression is constructed:
$$S = \begin{cases}
W & \text{if the projection is ALL} \\
\prod_{a_1,\ \dots,\ a_n}(W) & \text{otherwise}
\end{cases}$$
GROUP BY can also be translated, even though it is not part of the canonical translation. Let $g_1,\ \dots,\ g_n$ be the attributes in the clause and $agg$ the aggregations within SELECT:
$$G = \begin{cases}
W & \text{there is no GROUP BY clause} \\
\Gamma_{g_1,\ \dots,\ g_m:agg}(W) & \text{otherwise}
\end{cases}$$
HAVING is basically the same as WHERE, with the filter predicate on top of $G$.

\subsection{Logical Query Optimization}
Once obtained the relational algebra, equivalences span the \textit{potential search space} and new expressions are derived thanks to them. Of course equivalence can be applied both ways, hence it is relevant to decide which one works better, and conditions have to be checked as well. This, however, makes the search more expensive since there are plenty of alternatives.

To speed the process up, sometimes some equivalences are ignored, even the simplest ones (for instance when choosing the join algorithm).

Query plans can only be compared if there is a cost function, often needing details which are not available merely through relational algebra (what kind of join is being used): logical query optimization is still a \textbf{heuristic} and requires additional steps, since it is not enough to determine the runtime.

Most algorithms, therefore, use the following strategy:
\begin{itemize}
	\item Organization of equivalences into \textbf{groups};
	\item \textbf{Directing equivalences}, deciding the preferred side and rewriting rules to apply them sequentially to the initial expression, trying to reduce the size of intermediate results.
\end{itemize}
For example, a projection on the output of a join can be preferred to a join of a projection. It is important to keep in mind that tuples are being removed in the process, and this only applies in certain circumstances (regular expressions, high selectivity of join).

The rule of thumb is simply to eliminate the most tuples during the intermediate step, to then perform computationally expensive operation with the smallest amount of data.

To summarize, the phases are:
\begin{itemize}
	\item Breaking up conjunctive selection predicates, since simpler predicates can be moved around easier;
	\item Pushing selections down, reducing the number of tuples early;
	\item Introducing joins, which are cheaper than cross product (linear time);
	\item Determining join order, a usually NP-hard problem which is tackled with different approaches;
	\item Introducing and pushing down projections, removing redundant attributes.
\end{itemize}
Some SQL queries have limitations: selections sometimes cannot be pushed down, since there might be no join predicate between tables (cross product). Choosing a different join order allows further push down.

\subsection{Physical Query Optimization}
Physical query optimization adds execution information to the plan, allowing actual cost calculation and optimizing over data structures, access path and operator implementation.

Data may be sorted or materialized, introducing results which can be reused and deciding where to store them.

First of all, the access path is selected: lookup can be done through \textbf{index} or \textbf{table scan}, depending on the selectivity (fraction of the data satisfying the clause): in general, above 10\% a table scan is recommended.

Scanning a table might be efficient since tuples are stored adjacent in memory; using index, instead, involves traversing a tree multiple times starting from the root.

Sometimes it is useful to just store in cache the output of a view, but that also depends on the query plan: intermediate results should actually be reused.

\textbf{Operator selection} is replacing a logical operator with a physical one, according to semantic restrictions (most operators require equi-join).

A blockwise nested loop join is generally better than a natural join; sort merge join and hash join are better than both. In general, hash join is the best if not reusing sorts. This process must be performed for all operators: sort join requires ordered tuples, distributed databases need local data and there are multiple ways to model the properties (hashing).

Sort merge join might outperform the hash join if the amount of data is much larger than the available memory.

Materializing, on the other side, is quite relevant for nested loop joins: the first pass is expensive, but the afterwards ones are way cheaper, making it essential for multiple consumers.


\subsection{Definition of the methods}

\begin{Algorithm*}{subspace-iteration}{Orthogonal subspace iteration}

  Let $\mata\in\Cnn$, $\matx_0 \in \C^{n\times m}$.\\
  For $k=0,\ldots$ until convergence repeat
  \begin{itemize}
  \item $\matz_k = \mata \matx_k$.
  \item $\matq_k\matr_k = \matz_k$ (QR factorization)
  \item $\matx_{k+1} = \matq_k$
  \end{itemize}
\end{Algorithm*}

\begin{Algorithm*}{qr-iteration}{QR iteration}

  Let $\mata_1 = \mata\in\Cnn$.\\
  For $k=1,\ldots$ until convergence repeat
  \begin{itemize}
  \item $\matq_k\matr_k = \mata_k$ (QR factorization)
  \item $\mata_{k+1} = \matr_k\matq_k$
  \end{itemize}
\end{Algorithm*}

\subsection{Analysis}
\begin{Theorem*}{schur-canonical}{Schur canonical form}
  For every matrix $\mata\in\Cnn$ there are a unitary matrix
  $\matq\in\Cnn$ and an upper triangular matrix $\matr\in\Cnn$ such
  that
  \begin{gather}
    \mata = \matq \matr \matq^*.
  \end{gather}
  The diagonal entries of $\matr$ are the eigenvalues of $A$. The
  column vectors of $\matq$ are called \define{Schur vectors}.
\end{Theorem*}

\begin{Lemma}{schur-canonical-1}
  For any $k\le n$ the span of the Schur vectors
  $\vq_1,\dots,\vq_k$ is invariant under the action of $\mata$.

  For $\matq_k = (\vq_1\dots\vq_k)$ and $R_k$ the upper left $k\times k$ block of $\matr$, there holds
  \begin{gather}
    \mata\matq_k = \matq_k \matr_k.
  \end{gather}
\end{Lemma}

\begin{Lemma}{schur-canonical-2}
  The Schur vectors depend on the order chosen for the eigenvalues,
  and in case of geometric multiplicity, the eigenvectors,
  respectively. They are determined up to factors $e^{i\phi}$
\end{Lemma}

\begin{Theorem}{convergence-subspace-iteration}
  Let $\mata\in\Cnn$ and
  \begin{gather}
    \abs{\lambda_1} >
    \abs{\lambda_2}>\dots>\abs{\lambda_m}>\abs{\lambda}
  \end{gather}
  for all
  remaining eigenvalues $\lambda\in\sigma(\mata)$. Let
  $\matq = (\vq_1\dots\vq_m)$ be the Schur vectors associated with the
  first $m$ eigenvalues and $\esp{1,\dots,j}$ be the space spanned by
  the first $j$ eigenvectors and $P_j$ the orthogonal projector
  onto this space. Let the set of start vectors of the orthogonal subspace iteration
  $\matx_0 = (\vx_1\dots\vx_m)$ be chosen that
  \begin{gather}
    \operatorname{span}\{P_1 \vx_1,\dots,P_j\vx_j\} = \esp{1,\dots,j},\qquad j=1,\dots,m.
  \end{gather}
  Then, the $j$-th column of $\matx_k$ converges to $\vq_j$ for $j=1,\dots,m$ up to a factor $e^{i\phi}$.
\end{Theorem}

\begin{Lemma}{qr-1}
  The matrices $\mata_k$ of the QR-iteration have the following properties:
  \begin{enumerate}
  \item $\mata_{k+1} = \matq_k^*\mata_k\matq_k = \matq_k^*\dots\matq_1^*A\matq_1\dots\matq_k$.
  \item $\mata^k=\matq_1\dots\matq_k\matr_k\dots\matr_1$.
  \item If $\mata$ is normal, so is $\mata_k$ for any $k$.
  \item If $\mata$ is symmetric, so is $\mata_k$ for any $k$.
  \end{enumerate}
\end{Lemma}

\begin{Theorem}{convergence-qr-iteration}
Removed
\end{Theorem}

\subsection{Implementation issues}
\begin{intro}
  In each step of the QR-iteration, a QR-decomposition of the matrix
  is needed, which requires $\bigo(n^3)$ operations. Thus, the
  complexity of the iteration is highly unfavorable. The following
  discussion will provide us with means to reduce the complexity of
  the QR-decomposition to $\bigo(n^2)$, in the symmetric case even to
  $\bigo(n)$.
\end{intro}

\begin{Definition}{hessenberg}
  A matrix is in \define{Hessenberg form} or is a \define{Hessenberg
    matrix}, if all its entries below the first subdiagonal are zero. Visually,
  \begin{gather}
    H =
    \begin{pmatrix}
      *&*&*&*&*&*\\
      *&*&*&*&*&*\\
      0&*&*&*&*&*\\
      0&0&*&*&*&*\\
      0&0&0&*&*&*\\
      0&0&0&0&*&*
    \end{pmatrix}
  \end{gather}
  A symmetric or Hermitian Hessenberg matrix is \define{tridiagonal}.
\end{Definition}

\begin{Theorem}{Hessenberg-qr}
  The QR-decomposition of a Hessenberg matrix $\matH$ can be obtained
  by $n-1$ givens rotations. The matrix $\matr\matq$ is again in
  Hessenberg form. For a (complex) symmetric matrix $\matH$, the
  matrix $\matr\matq$ is even tridiagonal and (complex) symmetric.
\end{Theorem}

\begin{Corollary}{Hessenberg-qr}
  The complexity of each step of a QR-iteration for Hessenberg matrices is $\bigo(n^2)$. For tridiagonal (complex) symmetric matrices, it is $\bigo(n)$.
\end{Corollary}

\begin{Theorem}{Hessenberg-householder}
  Every matrix $\mata\in\Cnn$ is unitarily similar to a Hessenberg matrix $\matH$, that is,
  \begin{gather}
    \matH = \matq \mata \matq^*.
  \end{gather}
  The matrix $\matq$ can be obtained by $n-2$ \putindex{Householder
    reflections}.
\end{Theorem}

\begin{Algorithm*}{qr-method}{The QR-Method}
  Compute the spectrum of a matrix $\mata\in\Cnn$ by
  \begin{enumerate}
  \item Use $n-2$ Householder transformations to transform $\mata$ to
    Hessenberg form
    \begin{gather}
     \matH = \matq\mata\matq^*.
   \end{gather}
 \item QR-iteration: let $\matH_{0}=\matH$ and perform until convergence
   \begin{align}
     \Omega^{(k)}_{1,2}\times\dots\times\Omega^{(k)}_{n-1,n} \matr &= \matH_k\\
     \matH_{k+1} &= \matr \Omega^{(k)}_{1,2}\times\dots\times\Omega^{(k)}_{n-1,n}.
   \end{align}
 \item Store Householder vectors as well as $r$ and $c$ for each
   Givens rotation if the eigenvectors are desired in the end.
  \end{enumerate}
\end{Algorithm*}

\begin{Theorem}{hessenberg-qr-convergence}
    Let $\matH\in\Cnn$ be a Hessenberg matrix with eigenvalues such that
  \begin{gather}
    \abs{\lambda_1} >
    \abs{\lambda_2}>\dots>\abs{\lambda_n}.
  \end{gather}
  Then, the sequences of the QR-iteration admit the following estimates:
  \begin{align}
    \dist(\esp{1,\dots,j},\spann{\vq_1^{(k)},\dots,\vq_j^{(k)}}) &= \bigo \left(\abs*{\frac{\lambda_{j+1}}{\lambda_j}}^k\right),
    \\
    h_{j+1,j}^{(k)} &= \bigo \left(\abs*{\frac{\lambda_{j+1}}{\lambda_j}}^k\right)
                      .
  \end{align}
  Here, $h_{ij}^{(k)}$ are the entries of $\matH_k$.
\end{Theorem}

\begin{proof}
  See~\cite[Theorem 7.3-1]{GolubVanLoan83}.
\end{proof}

\subsection{Shifts and deflation}

\begin{intro}
  The goal of this section is the development and justification of a
  method which accelerates convergence of the QR-iteration and
  reducing the effort at the same time. It is based on shifts, like
  for the simple or inverse power method. But, shifts are much more
  powerful here, since we compute not only ``converging subspace'',
  but also its complement. The presentation follows
  mostly~\cite{GolubVanLoan83}.
\end{intro}

\begin{Theorem}{qr-reduction}
  Let the matrix $\matH^{(k)}\in\Cnn$ in the QR iteration be of the
  form
  \begin{gather}
    \matH^{(k)} =
    \begin{pmatrix}
      \matH_{11} & \mata_{12}\\0 & \matH_{22}
    \end{pmatrix}
  \end{gather}
  with Hessenberg matrices $\matH_{11}\in\C^{p\times p}$,
  $\matH_{22}\in \C^{n-p\times n-p}$ and an arbitrary matrix
  $\mata_{12}\in \C^{p\times n-p}$. Then, the matrix $\matq^{(k)}$
  decouples into two diagonal blocks and $\matH^{(k+1)}$ has the same
  form. Thus, the iteration decouples into two separate iterations.
\end{Theorem}

\begin{Definition}{hessenberg-unreduced}
  A Hessenberg matrix is called \define{unreduced} if all entries on
  the first subdiagonal are nonzero. It is called \define{reduced}
  otherwise.
\end{Definition}

\begin{Algorithm*}{shifted-qr-iteration}{QR iteration with shift}

  Let $\matH_1 = \matq_0^*\mata\matq_0\in\Cnn$.\\
  For $k=1,\ldots$ until convergence repeat
  \begin{itemize}
  \item $\matq_k\matr_k = \matH_k - \sigma\id$ (QR factorization)
  \item $\matH_{k+1} = \matr_k\matq_k + \sigma\id$
  \end{itemize}
\end{Algorithm*}

\begin{Lemma}{shifted-qr-convergence}
  The shifted QR-iteration admits the estimate
  \begin{gather}
    h_{j+1,j}^{(k)} = \bigo \left(\abs*{\frac{\lambda_{j+1}-\sigma}{\lambda_j-\sigma}}^k\right)
  \end{gather}
\end{Lemma}

\begin{Example*}{rayleigh-shift}{Rayleigh shift}
  The Rayleigh quotient for the smallest eigenvalue by magnitude
  converges to $h_{nn}$, as
  \begin{gather}
    \ve_n^* H^{(k)} \ve_n = h_{nn}^{(k)}
  \end{gather}
  and $\vq_n$ is orthogonal to all eigenvectors for eigenvalues of
  greater magnitude. Therefore, using $\sigma_k = h_{nn}^{(k)}$ seems
  a good idea, and often is. But it is not reliable, as in the example
  \begin{gather}
    H =
    \begin{pmatrix}
      0 & 1 \\ 1 & 0
    \end{pmatrix}.
  \end{gather}
\end{Example*}

\begin{Definition*}{wilkinson-shift}{Wilkinson shift}
  Let
  \begin{gather}
    \matm =
    \begin{pmatrix}
      h_{n-1,n-1}^{(k)}&h_{n-1,n}^{(k)}\\h_{n,n-1}^{(k)}&h_{nn}^{(k)}
    \end{pmatrix}.
  \end{gather}
  Then, for $\sigma_k$ use the eigenvalue of $\matm$ which is closer
  to $h_{nn}^{(k)}$.
\end{Definition*}

\begin{Remark}{wilkinson-shift}
  The Wilkinson shift is reliable and the $h_{n,n-1}$ and $h_{nn}$
  converge to zero and the smallest eigenvalue by magnitude,
  respectively. They converge at least quadratically and cubically in
  the symmetric case~\cite[Section 8.2]{GolubVanLoan83}.
\end{Remark}

\begin{Algorithm*}{qr-deflation}{Deflation}
  After each step of the shifted QR-iteration monitor the subdiagonal
  elements of $\matH^{(k)}$. Whenever
  \begin{gather}
    \abs{h_{j,j-1}} \le \eps \bigl(\abs{h_{j-1,j-1}}+\abs{h_{jj}}\bigr)
  \end{gather}
  set $h_{j,j-1}=0$.

  If this happens in the last row, consider $h_{nn}=\lambda_n$
  converged and proceed with a matrix of dimension $n-1\times n-1$.

  If this happens in the center of the matrix, proceed with both
  remaining diagonal blocks separately.
\end{Algorithm*}

\subsection{Methods in real arithmetic}

\begin{Theorem*}{real-schur-form}{The real Schur form}
  For every matrix $\mata\in \Rnn$ there is an orthogonal matrix
  $\matq\in\Rnn$ and a matrix $\matr\in\Rnn$ such that
  \begin{gather}
    \mata = \matq\matr\matq^*,
    \qquad
    \matr =
    \begin{pmatrix}
      R_{11} &* & *&*\\
      &R_{22}&*&*\\
      &&\ddots&*\\
      &&& R_{jj}
    \end{pmatrix},
  \end{gather}
  where the diagonal blocks are either of dimension one containing the
  real eigenvalues or of dimension 2 for complex conjugate eigenvalue
  pairs. The latter correspond to scaled rotation matrices with the
  according eigenvalue pair.
\end{Theorem*}

\begin{Remark}{francis-qr}
  Using double shifts, the QR-iteration can be made to converge to the
  real Schur form using double shifts in real arithmetic. This method
  is also known as the \define{Francis QR step}~\cite[Algorithm
  7.5-1]{GolubVanLoan83}.
\end{Remark}

\begin{Remark*}{real-symmetric-qr}{QR-Iteration for real, symmetric matrices}
  In this case, many things simplify
  \begin{enumerate}
  \item Hessenberg form is tridiagonal
  \item The Schur normal form is
    \begin{gather}
      \mata = \matq^T\matd\matq
    \end{gather}
    with real, diagonal matrix $\matd$
  \item QR-decomposition uses $\bigo(n)$ operations and $\matr$
    consists only of the main diagonal and one upper diagonal.
  \end{enumerate}
  Accumulating the matrix $\matq$ still needs $\bigo(n^2)$ operations
\end{Remark*}

\subsection{Singular Value Decomposition (SVD)}

\begin{Definition}{svd}
  The \define{singular value decomposition} (\define{SVD}) of a matrix $\mata\in\C^{m\times n}$ is a facorization
  \begin{gather}
    \mata = \matu\matsigma\matv^*
  \end{gather}
  with unitary matrices $\matu\in\C^{m\times m}$ and $\matv\in\Cnn$ as
  well as a real, diagonal matrix $\matsigma$ with diagonal entries
  \begin{gather}
    \sigma_1 \ge \sigma_2 \ge \dots \ge \sigma_p \ge 0,
  \end{gather}
  where $p = \min\{m,n\}$.
\end{Definition}

\begin{Theorem}{svd}
  Every matrix $\mata\in\C^{m\times n}$ admits a singular value
  decomposition. Every real matrix admits a singular value
  decomposition with orthogonal matrices $\matu$, $\matv$.
\end{Theorem}

\begin{Corollary}{svd-rank}
  Let $\mata = \matu\matsigma\matv^*$ be the SVD of $\mata$ with
  \begin{gather}
    \sigma_1 \ge \dots \ge \sigma_r > \sigma_{r+1} = \dots = \sigma_p = 0.
  \end{gather}
  Then, $\rank \mata = r$
\end{Corollary}

\begin{Corollary}{svd-inverse}
  If $\mata\in\Cnn$ is invertible, then
  \begin{gather}
    \mata^{-1} = \matv\matsigma^{-1}\matu^*,
  \end{gather}
  where
  \begin{gather}
    \matsigma^{-1} = \diag\left(\frac1{\sigma_1},\dots,\frac1{\sigma_n}\right).
  \end{gather}
\end{Corollary}

\begin{Remark}{svd-geometry}
  Let
  \begin{gather}
    E = \bigl\{ \vy\in \R^m \big| \vy=\mata\vx, \norm{\vx}_2 = 1 \bigr\},
  \end{gather}
  be the ellipsoid obtained by mapping the unit sphere though
  $\mata$. Then, the column vectors of $\matu$ and the singular values
  $\sigma_i$ are the directions and lengths of the semi-axes of this
  ellipsoid, respectively.
\end{Remark}

\begin{Lemma}{svd-ata}
  The singular values of $\mata$ are the square roots of the
  eigenvalues of $\mata^*\mata$ and of $\mata\mata^*$, respectively. For $m\ge n$ there holds
  \begin{align}
    \matv^*(\mata^*\mata)\matv &= \diag(\sigma_1^2,\dots,\sigma_n^2)
    &&\in \R^{n\times n}\\
    \matu^*(\mata\mata^*)\matu &= \diag(\sigma_1^2,\dots,\sigma_n^2, 0,\dots,0)
    &&\in \R^{m\times m}
  \end{align}
\end{Lemma}

\begin{Theorem*}{implicit-Q}{Implicit Q-theorem}
  Let $\mata\in\Cnn$ arbitrary, let $\matq,\matv\in\Cnn$ unitary such that
  \begin{gather}
    \matq^*\mata\matq = \matH,\qquad \matv^*\mata\matv = \matg,
  \end{gather}
  where $\matH$ and $\matg$ are Hessenberg matrices. Let $k$ denote
  the smallest integer such that $h_{k+1,k} = 0$, or $k=n$ if $\matH$
  is unreduced. Assume $\vv_1 = \vq_1$. Then,
  $\vv_j = e^{i_j\phi}\vq_j$ and $\abs{h_{j,j-1}} = \abs{g_{j,j-1}}$
  for $j=1,\dots,k$. if $k<n$, then $g_{k+1,k} = 0$.
\end{Theorem*}

\begin{proof}
  See \cite[Theorem 7.4-2]{GolubVanLoan83}.
\end{proof}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "main"
%%% End:


\subsubsection{USB3 Vision Interface}
\label{subsubsec:usb3_vision_interface}
% \todo[inline]{Citations are weird!, Table position? Leave it!}

The Baumer industrial camera VCXU-13C uses the USB3 Vision (U3V) interface with a maximum transfer rate of \SI{5}{Gbit/s} $\approx$ \SI{596}{MiB}.
The max. frame rate is \SI{222}{fps} and the total pixel count is equal to
\[
\text{Pixel}_\text{tot} = \text{Pixel}_\text{width} \cdot \text{Pixel}_\text{height} = \SI{1280}{px} \cdot \SI{1024}{px} = \SI{1310720}{px}.
\]

To calculate the required data rate in bytes per second, equation \ref{eq:data_rate} can be used.
Baumer transmits further information in addition to the actual image data (e.g. frame id, width and height).
This metadata is located in the so-called leader and can be neglected in the calculations due to its relatively small size \cite{baumer_gapi}.

\begin{equation}
  \text{Req. data rate} = \text{Pixel}_\text{tot} \cdot \text{Bytes per pixel} \cdot \text{Frame rate}
  \label{eq:data_rate}
\end{equation}

For this application a fixed frame rate of \SI{200}{fps} is sufficient and facilitates the calculations.

Table \ref{tab:data_rates} shows the required data rates for the Baumer pixel format \texttt{BayerRG8} and \texttt{BGR8}.
It is evident that due to the maximum transfer rate of \SI{596}{MiB} of the U3V interface, only the Baumer \texttt{BayerRG8} pixel format is suitable \cite{baumer_ic}.

\begin{table}[h]
  \caption{Required data rate for the two Baumer pixel formats \texttt{BayerRG8} and \texttt{BGR8}}
  \label{tab:data_rates}
  \centering
  \begin{tabular}{lll}
    \toprule
     & \textbf{\texttt{BayerRG8}} & \textbf{\texttt{BGR8}} \\
    \midrule
    \textbf{Bytes per pixel} & \SI{1}{B} & \SI{3}{B} \\
    \textbf{Frame rate} & \SI{200}{fps} & \SI{200}{fps} \\
    \textbf{Req. data rate} & \SI{250}{MiB/s} & \SI{750}{MiB/s} \\
    \bottomrule
  \end{tabular}
\end{table}


\section{Volume and Average Height}\label{sec:VolumeAvgHeight}

Consider a surface $f(x,y)$; you might temporarily think of this as
representing physical topography---a hilly landscape, perhaps. What is
the average height of the surface (or average
altitude of the landscape) over some region?

As with most such problems, we start by thinking about how we
might approximate the answer. Suppose the region is a rectangle,
$[a,b]\times[c,d]$. We can divide the rectangle into a grid, $m$
subdivisions in one direction and $n$ in the other, as indicated in
Figure~\ref{fig:rectangulargrid}. We pick $x$ values $x_0$,
$x_1$,\dots, $x_{m-1}$ in each subdivision in the $x$ direction, and
similarly in the $y$ direction.
At each of the points $(x_i,y_j)$ in one of the smaller rectangles in
the grid, we
compute the height of the surface: $f(x_i,y_j)$. Now the average
of these heights should be (depending on the fineness of the grid)
close to the average height of the surface:
\[\ds\frac{f(x_0,y_0)+f(x_1,y_0)+\cdots+f(x_0,y_1)+f(x_1,y_1)+\cdots+
f(x_{m-1},y_{n-1})}{mn}\]

As both $m$ and $n$ go to infinity, we expect this approximation to
converge to a fixed value, the actual average height of the
surface. For reasonably nice functions this does indeed happen.

\begin{figure}[H]
\centerline{
\vbox{\beginpicture
\normalgraphs
\setcoordinatesystem units <1.5truecm,1.5truecm>
\setplotarea x from 0 to 4.1, y from 0 to 3.1
\axis left ticks withvalues $c$ $y_1$ $y_2$ $y_3$ $d$ /
at 0.5 1 1.5 2 2.5 / /
\axis bottom ticks withvalues $a$ $x_1$ $x_2$ $x_3$ $x_4$ $x_5$ $b$ /
at 0.5 1 1.5 2 2.5 3 3.5 / /
\put {$\Delta x$} [t] <0pt,-3pt> at 2.75 0.5
\put {$\Delta y$} [l] <3pt,0pt> at 3.5 1.75
\putrule from 0.5 0.5 to 3.5 0.5
\putrule from 0.5 1 to 3.5 1
\putrule from 0.5 1.5 to 3.6 1.5
\putrule from 0.5 2 to 3.6 2
\putrule from 0.5 2.5 to 3.5 2.5
\putrule from 0.5 0.5 to 0.5 2.5
\putrule from 1 0.5 to 1 2.5
\putrule from 1.5 0.5 to 1.5 2.5
\putrule from 2 0.5 to 2 2.5
\putrule from 2.5 0.4 to 2.5 2.5
\putrule from 3 0.4 to 3 2.5
\putrule from 3.5 0.5 to 3.5 2.5
\endpicture}}
\caption{A rectangular subdivision of $[a,b]\times[c,d]$.}
\label{fig:rectangulargrid}
\end{figure}

Using sigma notation, we can rewrite the approximation:
\begin{align*}
\frac{1}{mn}\sum_{i=0}^{n-1}\sum_{j=0}^{m-1}f(x_j,y_i)
  &=\frac{1}{(b-a)(d-c)}\sum_{i=0}^{n-1}\sum_{j=0}^{m-1}f(x_j,y_i)\frac{b-a}{m}\frac{d-c}{n}	\\
  &=\frac{1}{(b-a)(d-c)}\sum_{i=0}^{n-1}\sum_{j=0}^{m-1}f(x_j,y_i)\Delta x\Delta y.
\end{align*}
The two parts of this product have useful meaning: $(b-a)(d-c)$ is of
course the area of the rectangle, and the double sum adds up $mn$
terms of the form $f(x_j,y_i)\Delta x\Delta y$, which is the height of
the surface at a point multiplied by the area of one of the small rectangles
into which we have divided the large rectangle. In short, each term
$f(x_j,y_i)\Delta x\Delta y$ is the volume of a tall, thin,
rectangular box, and is approximately the volume under the surface and
above one of the small rectangles; see Figure~\ref{fig:volumeapproximation}.
When we add all of these up, we get an
approximation to the volume under the surface and above the rectangle
$R=[a,b]\times[c,d]$. When we take the limit as $m$ and $n$ go to
infinity, the double sum becomes the actual volume under the surface,
which we divide by $(b-a)(d-c)$ to get the average height.

\begin{figure}[H]
\centerline{
\vbox{\beginpicture
\normalgraphs
\setcoordinatesystem units <3truecm,3truecm>
\setplotarea x from -1 to 1, y from 0 to 1
\put {\hbox{\epsfxsize8cm\epsfbox{images/double_int_approx_constr.eps}}} at 0 -0.15
\endpicture}}
\caption{Approximating the volume under a surface.}
\label{fig:volumeapproximation}
\end{figure}

Double sums like this come up in many applications, so in a way it is
the most important part of this example; dividing by $(b-a)(d-c)$ is a
simple extra step that allows the computation of an average. As we did
in the single variable case, we introduce a special notation for the
limit of such a double sum:
\[\lim_{m,n\to\infty} \sum_{i=0}^{n-1}\sum_{j=0}^{m-1}f(x_j,y_i)\Delta
  x\Delta y=\iint_R f(x,y)\,dx\,dy=\iint_R f(x,y)\,dA,\]
the \dfont{double integral}\index{double integral}
of $f$ over the region $R$. The notation $dA$ indicates a small bit of
area, without specifying any particular order for the variables $x$
and $y$; it is shorter and more ``generic'' than writing $dx\,dy$.
The average height of the surface in this notation is
\[\frac{1}{(b-a)(d-c)}\iint_R f(x,y)\,dA.\]

The next question, of course, is: How do we compute these double
integrals? You might think that we will need some two-dimensional
version of the Fundamental Theorem of Calculus, but as it turns out we
can get away with just the single variable version, applied twice.

Going back to the double sum, we can rewrite it to emphasize a
particular order in which we want to add the terms:
\[\sum_{i=0}^{n-1}\left(\sum_{j=0}^{m-1}f(x_j,y_i)\Delta x\right)\Delta y.\]
In the sum in parentheses, only the value of $x_j$ is changing; $y_i$
is temporarily constant. As $m$  goes to infinity, this sum has the
right form to turn into an integral:
\[\lim_{m\to\infty}\sum_{j=0}^{m-1}f(x_j,y_i)\Delta
  x = \int_a^b f(x,y_i)\,dx.\]
So after we take the limit as $m$ goes to infinity, the sum is
\[\sum_{i=0}^{n-1}\left(\int_a^b f(x,y_i)\,dx\right)\Delta y.\]
Of course, for different values of $y_i$ this integral has different
values; in other words, it is really a function applied to $y_i$:
\[G(y)=\int_a^b f(x,y)\,dx.\]
If we substitute back into the sum we get
\[\sum_{i=0}^{n-1} G(y_i)\Delta y.\]
This sum has a nice interpretation. The value $G(y_i)$ is the area of
a cross section of the region under the surface $f(x,y)$, namely, when
$y=y_i$. The quantity $G(y_i)\Delta y$ can be interpreted as the
volume of a solid with face area $G(y_i)$ and thickness $\Delta y$.
Think of the surface $f(x,y)$ as the top of a loaf of sliced
bread. Each slice has a cross-sectional area and a thickness;
$G(y_i)\Delta y$ corresponds to the volume of a single slice of
bread. Adding these up approximates the total volume of the loaf. (This is
very similar to the technique we used to compute volumes in
Section~\ref{sec:volume},
except that there we need the cross-sections to be in
some way ``the same''.) Figure~\ref{fig:volumeapproximationwithslices}
shows this ``sliced loaf''
approximation using the same surface as shown in
Figure~\ref{fig:volumeapproximation}.
Nicely enough, this sum looks just like the sort of sum that turns into an
integral, namely,
\begin{align*}
\lim_{n\to\infty}\sum_{i=0}^{n-1} G(y_i)\Delta y&=\int_c^d G(y)\,dy	\\
&=\int_c^d \int_a^b f(x,y)\,dx\,dy.
\end{align*}
Let's be clear about what this means: we first will compute the inner
integral, temporarily treating $y$ as a constant. We will do this by
finding an anti-derivative with respect to $x$, then substituting
$x=a$ and $x=b$ and subtracting, as usual. The result will be an
expression with no $x$ variable but some occurrences of $y$. Then the
outer integral will be an ordinary one-variable problem, with $y$ as
the variable.

\begin{figure}[H]
\centerline{
\vbox{\beginpicture
\normalgraphs
\setcoordinatesystem units <3truecm,3truecm>
\setplotarea x from -1 to 1, y from 0 to 1
\put {\hbox{\epsfxsize9cm\epsfbox{images/sliced_loaf_2.eps}}} at 0 -0.15
\endpicture}}
\caption{Approximating the volume under a surface with slices.}
\label{fig:volumeapproximationwithslices}
\end{figure}

\begin{example}{Volume Under Surface}{VolumeUnderSurface}
Figure~\ref{fig:volumeapproximation} shows the function
$\sin(xy)+6/5$ on $[0.5,3.5]\times[0.5,2.5]$. Find the volume under this surface.
\end{example}
\begin{solution}
The volume under this surface is
\[\int_{0.5}^{2.5}\int_{0.5}^{3.5} \sin(xy)+{6\over5}\,dx\,dy.\]
The inner integral is
\[\int_{0.5}^{3.5} \sin(xy)+{6\over5}\,dx=
\left.{-\cos(xy)\over y}+{6x\over5}\right|_{0.5}^{3.5}=
{-\cos(3.5y)\over y}+{\cos(0.5y)\over y}+{18\over5}.\]
Unfortunately, this gives a function for which we can't find a simple
anti-derivative. To complete the problem we could use Sage or similar
software to approximate the integral. Doing this gives a volume of
approximately $8.84$, so the average height is approximately
$8.84/6\approx 1.47$.
\end{solution}

Because addition and multiplication are commutative and associative,
we can rewrite the original double sum:
\[\sum_{i=0}^{n-1}\sum_{j=0}^{m-1}f(x_j,y_i)\Delta
  x\Delta y=\sum_{j=0}^{m-1}\sum_{i=0}^{n-1}f(x_j,y_i)\Delta
  y\Delta x.\]
Now if we repeat the development above, the inner sum turns into
an integral:
\[\lim_{n\to\infty}\sum_{i=0}^{n-1}f(x_j,y_i)\Delta
  y = \int_c^d f(x_j,y)\,dy,\]
and then the outer sum turns into an integral:
\[\lim_{m\to\infty}\sum_{j=0}^{m-1}\left(\int_c^d f(x_j,y)\,dy
\right)\Delta x = \int_a^b\int_c^d f(x,y)\,dy\,dx.\]
In other words, we can compute the integrals in either order, first
with respect to $x$ then $y$, or vice versa. Thinking of the loaf of
bread, this corresponds to slicing the loaf in a direction
perpendicular to the first.

We haven't really proved that the value of a double integral is equal
to the value of the corresponding two single integrals in either order
of integration, but provided the function is reasonably nice, this is
true; the result is called \dfont{Fubini's Theorem}.

\begin{example}{Compute Volume in Two Ways}{ComputeVolumeTwoWays}
We compute $\ds\iint_R 1+(x-1)^2+4y^2\,dA$, where
$R=[0,3]\times[0,2]$, in two ways.
\end{example}
\begin{solution}
First,
\begin{align*}
\int_0^3\int_0^2 1+(x-1)^2+4y^2\,dy\,dx
&=\int_0^3\left. y+(x-1)^2y+{4\over 3}y^3\right|_0^2\,dx	\\
&=\int_0^3 2+2(x-1)^2+{32\over 3}\,dx	\\
&=\left. 2x + {2\over 3}(x-1)^3+{32\over 3}x\right|_0^3	\\
&=6+{2\over 3}\cdot 8 + {32\over 3}\cdot3-(0-1\cdot{2\over3}+0)	\\
&=44.
\end{align*}
In the other order:
\begin{align*}
\int_0^2\int_0^3 1+(x-1)^2+4y^2\,dx\,dy
&=\int_0^2\left. x+{(x-1)^3\over3}+4y^2x\right|_0^3\,dy	\\
&=\int_0^2 3+{8\over3}+12y^2+{1\over3}\,dy	\\
&=\left. 3y+{8\over3}y+4y^3+{1\over3}y\right|_0^2	\\
&=6+{16\over3}+32+{2\over3}	\\
&=44.
\end{align*}
\end{solution}

In this example there is no particular reason to favor one direction
over the other; in some cases, one direction might be much easier than
the other, so it's usually worth considering the two different
possibilities.

Frequently we will be interested in a region that is not simply a
rectangle. Let's compute the volume under the surface $x+2y^2$ above
the region described by $0\le x\le1$ and $0\le y\le x^2$, shown in
Figure~\ref{fig:parabolicregion}.

\begin{figure}[H]
\hbox to \hsize{\hfill
\begin{tikzpicture}[baseline=0,x=2cm,y=2cm]
\draw (0,0) -- (1.1,0) ;
\draw (0,0) -- (0,1.1) ;
\foreach \x in {0,1} \draw (\x,0) -- (\x,-2pt) node[anchor=north] {$\x$};
\foreach \y in {0,1} \draw (0,\y) -- (-2pt,\y) node[anchor=east]
{$\y$};
%\gpad
\draw[color=black]  plot[id=fun,domain=0:1] function{x**2};
\draw (1,0) -- (1,1);
\fill[opacity=0.5,fill=red!20] (0,0) parabola (1,1) -- (1,0) -- (0,0);
\end{tikzpicture}\hfill}
\caption{A parabolic region of integration.}
\label{fig:parabolicregion}
\end{figure}

In principle there is nothing more difficult about this problem. If we
imagine the three-dimensional region under the surface and above the
parabolic region as an oddly shaped loaf of bread, we can still slice
it up, approximate the volume of each slice, and add these volumes
up. For example, if we slice perpendicular to the $x$ axis at $x_i$, the
thickness of a slice will be $\Delta x$ and the area of the slice will
be
\[\int_0^{x_i^2} x_i+2y^2\,dy.\]
When we add these up and take the limit as $\Delta x$ goes to 0, we
get the double integral
\begin{align*}
\int_0^1 \int_0^{x^2} x+2y^2\,dy\,dx
&=\int_0^1 \left.xy+{2\over3}y^3\right|_0^{x^2}\,dx	\\
&=\int_0^1 x^3+{2\over3}x^6\,dx	\\
&=\left. {x^4\over4}+{2\over21}x^7\right|_0^1	\\
&={1\over4}+{2\over21}={29\over84}.
\end{align*}
We could just as well slice the solid perpendicular to the $y$ axis,
in which case we get
\begin{align*}
\int_0^1 \int_{\sqrt y}^1 x+2y^2\,dx\,dy
&=\int_0^1 \left.{x^2\over2}+2y^2x\right|_{\sqrt y}^1 \,dy	\\
&=\int_0^1 {1\over2}+2y^2-{y\over2}-2y^2\sqrt y\,dy	\\
&=\left.{y\over2}+{2\over3}y^3-{y^2\over4}-{4\over7}y^{7/2}\right|_0^1	\\
&={1\over2}+{2\over3}-{1\over4}-{4\over7}={29\over84}.
\end{align*}
What is the average height of the surface over this region? As before,
it is the volume divided by the area of the base, but now we need to
use integration to compute the area of the base, since it is not a
simple rectangle. The area is
$$\int_0^1 x^2\,dx={1\over3},$$
so the average height is $29/28$.

\begin{example}{Volume of Region}{VolumeRegion}
Find the volume under the surface $\ds z=\sqrt{1-x^2}$ and above
the triangle formed by $y=x$, $x=1$, and the $x$-axis.
\end{example}
\begin{solution}
Let's consider the two possible ways to set this up:
$$\int_0^1 \int_0^x \sqrt{1-x^2}\,dy\,dx \qquad\hbox{or}\qquad
\int_0^1 \int_y^1 \sqrt{1-x^2}\,dx\,dy.
$$
Which appears easier? In the first, the first (inner) integral is
easy, because we need an anti-derivative with respect to $y$, and the
entire integrand $\ds\sqrt{1-x^2}$ is constant with respect to $y$. Of
course, the second integral may be more difficult. In the second, the
first integral is mildly unpleasant---a trig substitution. So let's
try the first one, since the first step is easy, and see where that
leaves us.
\[\int_0^1 \int_0^x \sqrt{1-x^2}\,dy\,dx=
\int_0^1 \left. y\sqrt{1-x^2}\right|_0^x\,dx=
\int_0^1 x\sqrt{1-x^2}\,dx.\]
This is quite easy, since the substitution $u=1-x^2$ works:
\[\int x\sqrt{1-x^2}\,dx=-{1\over 2}\int \sqrt u\,du
={1\over3}u^{3/2}=-{1\over3}(1-x^2)^{3/2}.\]
Then
\[\int_0^1 x\sqrt{1-x^2}\,dx=\left. -{1\over3}(1-x^2)^{3/2}\right|_0^1
={1\over3}.\]
This is a good example of how the order of integration can affect the
complexity of the problem. In this case it is possible to do the other
order, but it is a bit messier. In some cases one order may lead to a
very difficult or impossible integral; it's usually worth considering
both possibilities before going very far.
\end{solution}

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

\begin{enumialphparenastyle}

\begin{ex}
Compute $\ds \int_{0}^{2}\int_{0}^{4} 1+x \,dy\,dx$.
\begin{sol}
$16$
\end{sol}
\end{ex}

\begin{ex}
Compute $\ds \int_{-1}^{1}\int_{0}^{2} x+y\,dy\,dx$.
\begin{sol}
$4$
\end{sol}
\end{ex}

\begin{ex}
Compute $\ds \int_{1}^{2}\int_{0}^{y} xy \,dx\,dy$.
\begin{sol}
$15/8$
\end{sol}
\end{ex}

\begin{ex}
Compute $\ds \int_{0}^{1}\int_{y^2/2}^{\sqrt y} \,dx\,dy$.
\begin{sol}
$1/2$
\end{sol}
\end{ex}

\begin{ex}
Compute $\ds \int_{1}^{2}\int_{1}^{x} {x^2\over y^2}\,dy\,dx$.
\begin{sol}
$5/6$
\end{sol}
\end{ex}

\begin{ex}
Compute $\ds \int_{0}^{1}\int_{0}^{x^2} {y\over e^x}\,dy\,dx$.
\begin{sol}
$12-65/(2e)$.
\end{sol}
\end{ex}

\begin{ex}
Compute $\ds \int_{0}^{\sqrt{\pi/2}}\int_{0}^{x^2} x\cos y\,dy\,dx$.
\begin{sol}
$1/2$
\end{sol}
\end{ex}

\begin{ex}
Compute $\ds \int_{0}^{\pi/2}\int_{0}^{\cos\theta}r^2
(\cos\theta-r) \,dr\,d\theta$.
\begin{sol}
$\pi/64$
\end{sol}
\end{ex}

\begin{ex}
Compute: $\ds \int_0^1\int_{\sqrt{y}}^1
\sqrt{x^3+1}\,dx\,dy$.
\begin{sol}
$(2/9)2^{3/2}-(2/9)$
\end{sol}
\end{ex}

\begin{ex}
Compute: $\ds \int_0^1\int_{y^2}^1
y\sin(x^2)\,dx\,dy$.
\begin{sol}
$(1-\cos(1))/4$
\end{sol}
\end{ex}

\begin{ex}
Compute: $\ds \int_0^1 \int_{x^2}^1 x\sqrt{1+y^2}\,dy\,dx$
\begin{sol}
$(2\sqrt2-1)/6$
\end{sol}
\end{ex}

\begin{ex}
Compute: $\ds \int_0^1 \int_0^y
	  {2\over\sqrt{1-x^2}}\,dx\,dy$
\begin{sol}
$\pi-2$
\end{sol}
\end{ex}

\begin{ex}
Compute: $\ds \int_0^1 \int_{3y}^3
	  e^{x^2}\,dx\,dy$
\begin{sol}
$(e^9-1)/6$
\end{sol}
\end{ex}

\begin{ex}
Compute $\ds \int_{-1}^1\int_0^{1-x^2} x^2-\sqrt{y}\,dy\,dx$.
\begin{sol}
$\ds {4\over15}-{\pi\over4}$
\end{sol}
\end{ex}

\begin{ex}
Compute
$\ds \int_{0}^{\sqrt2/2}\int_{-\sqrt{1-2x^2}}^{\sqrt{1-2x^2}} x\,dy\,dx$.
\begin{sol}
$1/3$
\end{sol}
\end{ex}

\begin{ex}
Evaluate $\ds\iint x^2\,dA$ over the region in the first
quadrant bounded by the hyperbola $xy=16$ and the lines $y=x$, $y=0$, and
$x=8$.
\begin{sol}
$448$
\end{sol}
\end{ex}

\begin{ex}
Find the volume below $z=1-y$ above the region
$-1\le x\le 1$, $0\le y\le 1-x^2$.
\begin{sol}
$4/5$
\end{sol}
\end{ex}

\begin{ex}
Find the volume bounded by $z=x^2+y^2$ and $z=4$.
\begin{sol}
$8\pi$
\end{sol}
\end{ex}

\begin{ex}
Find the volume in the first octant
bounded by $y^2=4-x$ and $y=2z$.
\begin{sol}
$2$
\end{sol}
\end{ex}

\begin{ex}
Find the volume in the first octant
bounded by $y^2=4x$, $2x+y=4$, $z=y$,
and $y=0$.
\begin{sol}
$5/3$
\end{sol}
\end{ex}

\begin{ex}
Find the volume in the first octant
bounded by $x+y+z=9$, $2x+3y=18$, and $x+3y=9$.
\begin{sol}
$81/2$
\end{sol}
\end{ex}

\begin{ex}
Find the volume in the first octant
bounded by $x^2+y^2=a^2$ and $z=x+y$.
\begin{sol}
$2a^3/3$
\end{sol}
\end{ex}

\begin{ex}
Find the volume bounded by $4x^2+y^2=4z$ and $z=2$.
\begin{sol}
$4\pi$
\end{sol}
\end{ex}

\begin{ex}
Find the volume bounded by $z=x^2+y^2$ and $z=y$.
\begin{sol}
$\pi/32$
\end{sol}
\end{ex}

\begin{ex}
Find the volume under the surface $z=xy$ above the triangle
with vertices $(1,1,0)$, $(4,1,0)$, $(1,2,0)$.
\begin{sol}
$31/8$
\end{sol}
\end{ex}

\begin{ex}
Find the volume enclosed by $y=x^2$, $y=4$, $z=x^2$, $z=0$.
\begin{sol}
$128/15$
\end{sol}
\end{ex}

\begin{ex}
A swimming pool is circular with a 40 meter diameter.  The
depth is constant along east-west lines and increases linearly from 2
meters at the south end to 7 meters at the north end.  Find the volume
of the pool.
\begin{sol}
$1800\pi$ ${\rm m}^3$
\end{sol}
\end{ex}

\begin{ex}
Find the average value of $f(x,y)=e^y\sqrt{x+e^y}$ on the
    rectangle with vertices $(0,0)$, $(4,0$), $(4,1)$ and $(0,1)$.
\begin{sol}
$\ds{(e^2+8e+16)\over15}\sqrt{e+4}-{5\sqrt5\over3}-{e^{5/2}\over15}
+{1\over15}$
\end{sol}
\end{ex}

\begin{ex}
Figure~\ref{fig:colotemperatures} shows a temperature map
of Colorado.  Use the data to estimate the average temperature in the
state using 4, 16 and 25 subdivisions.  Give both an upper and lower
estimate.  Why do we like Colorado for this problem?  What other
state(s) might we like?

\begin{figure}[H]
\centerline{
\vbox{\beginpicture
\setcoordinatesystem units <2.5truecm,2.5truecm>
\setplotarea x from -1 to 1, y from 0 to 1
\put {\hbox{\epsfxsize7cm\epsfbox{images/weathermap.eps}}} at 0 0
\endpicture}}
\caption{Colorado temperatures.}
\label{fig:colotemperatures}
\end{figure}
\end{ex}

\begin{ex}
Three cylinders of radius 1 intersect at right angles at the
origin, as shown in Figure~\ref{fig:threecylinders}. Find the
volume contained inside all three cylinders.
\begin{sol}
$16-8\sqrt{2}$
\end{sol}

\begin{figure}[H]
\centerline{
\vbox{\beginpicture
\normalgraphs
\setcoordinatesystem units <2truecm,2truecm>
\setplotarea x from -1 to 1, y from 0 to 1
\put {\hbox{\epsfxsize6cm\epsfbox{images/three_cylinders.eps}}} at 0 0
\put {\hbox{\epsfxsize4.5cm\epsfbox{images/three_cylinders_inside.eps}}} at 4 0
\endpicture}}
\caption{Intersection of three cylinders.}
\label{fig:threecylinders}
\end{figure}
\end{ex}

\begin{ex}
Prove that if $f(x,y)$ is integrable and if $\ds g(x,y)=\int_a^x
    \int_b^y f(s,t) \; dt \; ds$ then $g_{xy}=g_{yx}=f(x,y)$.
\end{ex}

\begin{ex}
Reverse the order of integration on each of the following integrals
\begin{enumerate}
	\item $\ds\int_0^9 \int_0^{\sqrt{9-y}} f(x,y)\; dx \; dy$
	\item $\ds\int_1^2 \int_0^{\ln x} f(x,y) \; dy \; dx $
	\item $\ds\int_0^1 \int_{\arcsin y}^{\pi/2} f(x,y) \; dx \; dy$
	\item $\ds\int_0^1 \int_{4x}^{4} f(x,y) \; dy \; dx$
	\item $\ds\int_0^3 \int_{0}^{\sqrt{9-y^2}} f(x,y) \; dx \; dy$
\end{enumerate}
\end{ex}

\begin{ex}
What are the parallels between Fubini's
    Theorem and Clairaut's Theorem?
%% /Balof
\end{ex}

\end{enumialphparenastyle}


\chapter{Ramification theory}
We're very interested in how rational primes $p$ factor in a bigger number field $K$.
Some examples of this behavior: in $\ZZ[i]$ (which is a UFD!), we have factorizations
\begin{align*}
	(2) &= (1+i)^2 \\
	(3) &= (3) \\
	(5) &= (2+i)(2-i).
\end{align*}
In this chapter we'll learn more about how primes break down when they're thrown into bigger number fields.
Using weapons from Galois Theory, this will culminate in a proof of Quadratic Reciprocity.

\section{Ramified / inert / split primes}
\prototype{In $\ZZ[i]$, $2$ is ramified, $3$ is inert, and $5$ splits.}

Let $p$ be a rational prime, and toss it into $\OO_K$.
Thus we get a factorization into prime ideals
\[ p \cdot \OO_K = \kp_1^{e_1} \dots \kp_g^{e_g}. \]
We say that each $\kp_i$ is \vocab{above} $(p)$.\footnote{%
	Reminder that $p \cdot \OO_K$ and $(p)$ mean the same thing, and I'll use both interchangeably.}
Pictorially, you might draw this as follows:
\begin{diagram}
	K & \supset & \OO_K & \kp_i \\
	\dLine && \dLine & \dLine \\
	\QQ & \supset & \ZZ & (p)
\end{diagram}
Some names for various behavior that can happen:
\begin{itemize}
	\ii We say $p$ is \vocab{ramified} if $e_i > 1$ for some $i$.
	For example $2$ is ramified in $\ZZ[i]$.
	\ii We say $p$ is \vocab{inert} if $g=1$ and $e_1=1$; i.e. $(p)$ remains prime.
	For example $3$ is inert in $\ZZ[i]$.
	\ii We say $p$ is \vocab{split} if $g > 1$.
	For example $5$ is split in $\ZZ[i]$.
\end{itemize}
\begin{ques}
	More generally, for a prime $p$ in $\ZZ[i]$:
	\begin{itemize}
		\ii $p$ is ramified exactly when $p = 2$.
		\ii $p$ is inert exactly when $p \equiv 3 \pmod 4$.
		\ii $p$ is split exactly when $p \equiv 1 \pmod 4$.
	\end{itemize}
	Prove this.
\end{ques}

\section{Primes ramify if and only if they divide $\Delta_K$}
The most unusual case is ramification:
Just like we don't expect a randomly selected polynomial to have a double root,
we don't expect a randomly selected prime to be ramified.
In fact, the key to understanding ramification is the discriminant.

For the sake of discussion, let's suppose that $K$ is monogenic,
$\OO_K = \ZZ[\theta]$, where $\theta$ has minimal polynomial $f$.
Let $p$ be a rational prime we'd like to factor.
If $f$ factors as $f_1^{e_1} \dots f_g^{e_g}$, then we know that
the prime factorization of $(p)$ is given by
\[ p \cdot \OO_K = \prod_i \left( p, f_i(\theta) \right)^{e_i}. \]
In particular, $p$ ramifies exactly when \emph{$f$ has a double root mod $p$}!
To detect whether this happens, we look at the polynomial discriminant of $f$,
namely
\[ \Delta(f) = \prod_{i<j} (z_i - z_j)^2 \]
and see whether it is zero mod $p$ -- thus $p$ ramifies if and only if this is true.

It turns out that the na\"{\i}ve generalization to any number field
works if we replace $\Delta(f)$ by just the discriminant $\Delta_K$ of $K$;
(these are the same for monogenic $\OO_K$ by \Cref{prob:root_discriminant}).
That is,
\begin{theorem}
	[Discriminant detects ramification]
	Let $p$ be a rational prime and $K$ a number field.
	Then $p$ is ramified if and only if $p$ divides $\Delta_K$.
\end{theorem}
\begin{example}[Ramification in the Gaussian integers]
	Let $K = \QQ(i)$ so $\OO_K = \ZZ[i]$ and $\Delta_K = 4$.
	As predicted, the only prime ramifying in $\ZZ[i]$ is $2$,
	the only prime factor of $\Delta_K$.
\end{example}
In particular, only finitely many primes ramify.

\section{Inertial degrees}
\prototype{$(7)$ has inertial degree $2$ in $\ZZ[i]$ and $(2+i)$ has inertial degree $1$ in $\ZZ[i]$.}

Recall that we were able to define an ideal norm
$\Norm(\ka) = \left\lvert \OO_K / \ka \right\rvert$
measuring how ``roomy'' the ideal $\ka$ is.
For example, $(5)$ has ideal norm $5^2 = 25$ in $\ZZ[i]$, since
\[ \ZZ[i] / (5) \cong \left\{ a+bi \mid a,b \in \Zc5 \right\} \]
has $5^2 = 25$ elements.

Now, let's look at
\[ p \cdot \OO_K = \kp_1^{e_1} \dots \kp_g^{e_g} \]
in $\OO_K$, where $K$ has degree $n$.
Taking the ideal norms of both sides, we have that
\[ p^n = \Norm(\kp_1)^{e_1} \dots \Norm(\kp_g)^{e_g}. \]
We conclude that $\kp_i = p^{f_i}$ for some integer $f_i \ge 1$, and moreover that
\[ n = \sum_{i=1}^g e_i f_i. \]
\begin{definition}
	We say $f_i$ is the \vocab{inertial degree} of $\kp_i$,
	and $e_i$ is the \vocab{ramification index}.
\end{definition}
\begin{example}[Examples of inertial degrees]
	Work in $\ZZ[i]$, which is degree $2$.
	The inertial degree detects how ``spacy'' the
	given $\kp$ is when interpreted in $\OO_K$.
	\begin{enumerate}[(a)]
		\ii The prime $7 \cdot \ZZ[i]$ has inertial degree $2$.
		Indeed, $\ZZ[i]/ (7)$ has $7^2=49$ elements,
		those of the form $a+bi$ for $a$, $b$ modulo $7$.
		It gives ``two degrees'' of space.
		\ii Let $(5) = (2+i)(2-i)$.
		The inertial degrees of $(2+i)$ and $(2-i)$ are both $1$.
		Indeed, $\ZZ[i] / (2+i)$ only gives ``one degree'' of space,
		since each of its elements can be viewed as integers modulo $5$,
		and there are only $5^1=5$ elements.
	\end{enumerate}
	If you understand this, it should be intuitively clear
	why the sum of $e_i f_i$ should equal $n$.
\end{example}

\section{The magic of Galois extensions}
OK, that's all fine and well.
But something \emph{really magical} happens when we add the
additional hypothesis that $K/\QQ$ is \emph{Galois}:
all the inertial degrees and ramification degrees are equal.
We set about proving this.

Let $K/\QQ$ be Galois with $G = \Gal(K/\QQ)$.
Note that if $\kp \subseteq \OO_K$ is a prime above $p$,
then the image $\sigma\im(\kp)$ is also prime for any $\sigma \in G$
(since $\sigma$ is an automorphism!).
Moreover, since $p \in \kp$ and $\sigma$ fixes $\QQ$,
we know that $p \in \sigma\im(\kp)$ as well.

Thus, by the pointwise mapping, \textbf{the Galois group acts
on the prime ideals above a rational prime $p$}.
Picture:
\begin{center}
	\begin{asy}
		size(6cm);
		pair P = MP("p", (0,-2.8), dir(-90));
		pair A = MP("\mathfrak p_1", 0.8*dir(210), origin);
		pair B = MP("\mathfrak p_2", 0.5*dir(140), origin);
		pair C = MP("\mathfrak p_3", dir(70), origin);
		pair D = MP("\mathfrak p_4", 1.2*dir(-15), origin);
		pair E = MP("\mathfrak p_5", 1.4*dir(15), origin);
		pair F = MP("\mathfrak p_6", 1.5*dir(135), origin);
		draw(dir(-90)--P);
		draw(A--D, dashed, EndArrow, Margin(3,3));
		label("$\sigma$", A--D, dir(-90));
	\end{asy}
\end{center}

The notation $\sigma\im(\kp)$ is hideous in this context,
since we're really thinking of $\sigma$ as just doing a group action,
and so we give the shorthand:
\begin{abuse}
	Let $\sigma\kp$ be shorthand for $\sigma\im(\kp)$.
\end{abuse}

Since the $\sigma$'s are all bijections (they are automorphisms!),
it should come as no surprise that the prime ideals which are in the same
orbit are closely related.
But miraculously, it turns out there is only one orbit!
\begin{theorem}
	[Galois group acts transitively]
	Let $K/\QQ$ be Galois with $G = \Gal(K/\QQ)$.
	Let $\{\kp_i\}$ be the set of distinct prime ideals in
	the factorization of $p \cdot \OO_K$ (in $\OO_K$).

	Then $G$ acts transitively on the $\kp_i$:
	for every $i$ and $j$, we can find $\sigma$ such that $\sigma\kp_i = \kp_j$.
\end{theorem}
\begin{proof}
	Fairly slick.
	Suppose for contradiction that no $\sigma \in G$ sends $\kp_1$ to $\kp_2$, say.
	By the Chinese remainder theorem, we can find an $x \in \OO_K$ such that
	\begin{align*}
		x &\equiv 0 \pmod{\kp_1} \\
		x &\equiv 1 \pmod{\kp_i} \text{ for $i \ge 2$}
		% \pmod{(\sigma\inv)\im(\kp_2)} \text{ for $\sigma \in G$.}
	\end{align*}
	Then, compute the norm
	\[ \NK(x) = \prod_{\sigma \in \Gal(K/\QQ)} \sigma(x). \]
	Each $\sigma(x)$ is in $K$ because $K/\QQ$ is Galois!

	Since $\NK(x)$ is an integer and divisible by $\kp_1$,
	we should have that $\NK(x)$ is divisible by $p$.
	Thus it should be divisible by $\kp_2$ as well.
	But by the way we selected $x$, we have $x \notin \sigma\inv\kp_2$ for every $\sigma \in G$!
	So $\sigma(x) \notin \kp_2$ for any $\sigma$, which is a contradiction.
\end{proof}
\begin{theorem}[Inertial degree and ramification indices are all equal]
	Assume $K/\QQ$ is Galois.
	Then for any rational prime $p$ we have
	\[ p \cdot \OO_K = \left( \kp_1 \kp_2 \dots \kp_g \right)^e \]
	for some $e$, where the $\kp_i$ are distinct prime ideals
	with the same inertial degree $f$.
	Hence \[ [K:\QQ] = efg. \]
\end{theorem}
\begin{proof}
	To see that the inertial degrees are equal, note that each $\sigma$
	induces an isomorphism
	\[ \OO_K / \kp \cong \OO_K / \sigma(\kp). \]
	Because the action is transitive, all $f_i$ are equal.
	\begin{exercise}
		Using the fact that $\sigma \in \Gal(K/\QQ)$,
		show that \[ \sigma\im(p \cdot \OO_K) = p \cdot \sigma\im(\OO_K) = p \cdot \OO_K. \]
	\end{exercise}
	So for every $\sigma$, we have that
	$p \cdot \OO_K = \prod \kp_i^{e_i} = \prod (\sigma\kp_i)^{e_i}$.
	Since the action is transitive, all $e_i$ are equal.
\end{proof}

Let's see an illustration of this.
\begin{example}[Factoring $5$ in a Galois/non-Galois extension]
	Let $p = 5$ be a prime.
	\begin{enumerate}[(a)]
		\ii Let $E = \QQ(\cbrt2)$.
		One can show that $\OO_E = \ZZ[\cbrt2]$, so
		we use the Factoring Algorithm on the minimal polynomial $x^3-2$.
		Since $x^3-2 \equiv (x-3)(x^2+3x+9) \pmod 5$ is the irreducible factorization,
		we have that
		\[ (5) = (5,\cbrt2-3)(5, \cbrt4+3\cbrt2+9) \]
		which have inertial degrees $1$ and $2$, respectively.
		The fact that this is not uniform reflects that $E$ is not Galois.

		\ii Now let $K = \QQ(\cbrt2,\omega)$, which is the splitting
		field of $x^3-2$ over $\QQ$; now $K$ is Galois.
		It turns out that
		\[ \OO_K = \ZZ[\eps] \quad\text{where}\quad \eps \text { is a root of } t^6+3t^5-5t^3+3t+1. \]
		(this takes a lot of work to obtain, so we won't do it here).
		Modulo $5$ this has an irreducible factorization
		$(x^2+x+2)(x^2+3x+3)(x^2+4x+1) \pmod 5$,
		so by the Factorization Algorithm,
		\[ (5) = (5, \eps^2+\eps+2)(5, \eps^2+3\eps+3)(5, \eps^2+4\eps+1). \]
		This time all inertial degrees are $2$, as the theorem predicts for $K$ Galois.
	\end{enumerate}
\end{example}

\section{(Optional) Decomposition and inertia groups}
Let $p$ be a rational prime.
Thus
\[ p \cdot \OO_K = \left( \kp_1 \dots \kp_g \right)^e \]
and all the $\kp_i$ have inertial degree $f$.
Let $\kp$ denote a choice of the $\kp_i$.

We can look at both the fields $\OO_K / \kp$ and $\ZZ / p = \mathbb F_p$.
Naturally, since $\OO_K / \kp$ is a finite field we can view it as a field extension of $\OO_K$.
So we can get the diagram
\begin{diagram}
	K & \supset & \OO_K & \kp & & \OO_K / \kp \cong \FF_{p^f} \\
	\dLine && \dLine & \dLine & & \dLine \\
	\QQ & \supset & \ZZ & (p) & & \FF_p.
\end{diagram}
At the far right we have finite field extensions, which we know are \emph{really} well behaved.
So we ask:
\begin{quote}
	\itshape
	How are $\Gal\left( (\OO_K/\kp) / \FF_p \right)$
	and $\Gal(K/\QQ)$ related?
\end{quote}
Absurdly enough, there is an explicit answer:
\textbf{it's just the stabilizer of $\kp$, at least when
$p$ is unramified}.
\begin{definition}
	Let $D_\kp \subseteq \Gal(K/\QQ)$ be the stabilizer of $\kp$, that is
	\[ D_\kp \defeq \left\{ \sigma \in \Gal(K/\QQ) \mid \sigma\kp = \kp \right\}. \]
	We say $D_\kp$ is the \vocab{decomposition group} of $\kp$.
\end{definition}
Then, every $\sigma \in D_\kp$ induces an automorphism of $\OO_K / \kp$ by
\[ \alpha \mapsto \sigma(\alpha) \pmod\kp. \]
So there's a natural map
\[ D_\kp \taking\theta \Gal\left( (\OO_K/\kp) / \FF_p \right) \]
by declaring $\theta(\sigma)$ to just be ``$\sigma \pmod \kp$''.
The fact that $\sigma \in D_\kp$ (i.e.\ $\sigma$ fixes $\kp$)
ensures this map is well-defined.

\begin{theorem}[Decomposition group and Galois group]
	\label{thm:decomposition}
	Define $\theta$ as above. Then
	\begin{itemize}
		\ii $\theta$ is surjective, and
		\ii its kernel is a group of order $e$,
		the ramification index.
	\end{itemize}
	In particular, if $p$ is unramified then
	$D_\kp \cong \Gal\left( (\OO_K/\kp)/\FF_p \right)$.
\end{theorem}
(The proof is not hard, but a bit lengthy and in my opinion
not very enlightening.)

\begin{moral}
	If $p$ is unramified, then taking
	modulo $\kp$ gives
	$D_\kp \cong \Gal\left( (\OO_K/\kp) / \FF_p \right)$.
\end{moral}

But we know exactly what $\Gal\left( (\OO_K/\kp)/\FF_p \right)$ is!
We already have $ \OO_K / \kp \cong \FF_{p^f} $, and the Galois group is
\[
	\Gal\left( (\OO_K/\kp) / \FF_p \right)
	\cong \Gal\left( \FF_{p^f} / \FF_p \right)
	\cong \left< x \mapsto x^p \right>
	\cong \Zc f.
\]
So \[ D_\kp \cong \Zc f \] as well.

Let's now go back to
\[ D_\kp \taking\theta \Gal\left( (\OO_K/\kp) : \FF_p \right). \]
The kernel of $\theta$ is called the \vocab{inertia group}
and denoted $I_\kp \subseteq D_\kp$; it has order $e$.

This gives us a pretty cool sequence of subgroups
$\{1\} \subseteq I \subseteq D \subseteq G$
where $G$ is the Galois group (I'm dropping the $\kp$-subscripts now).
Let's look at the corresponding \emph{fixed fields} via the Fundamental theorem of Galois theory.
Picture:
\begin{diagram}
	\kp \subseteq \OO_K \subseteq & K & \rIsom & \{1\} \\
	& \dLine^{\text{Ramify}} & & \dLine_e \\
	& K^I & & I \\
	& \dLine^{\text{Inert}} & & \dLine_f \\
	& K^D & & D \\
	& \dLine^{\text{Split}} & & \dLine_g \\
	(p) \subseteq \ZZ \subseteq & \QQ & \rIsom & G
\end{diagram}
Something curious happens:
\begin{itemize}
	\ii When $(p)$ is lifted into $K^D$ it splits completely into $g$ unramified primes.
	Each of these has inertial degree $1$.
	\ii When the primes in $K^D$ are lifted to $K^I$, they remain inert, and now have
	inertial degree $f$.
	\ii When then lifted to $K$, they ramify with exponent $e$ (but don't split at all).
\end{itemize}
Picture:
In other words, the process of going from $1$ to $efg$
can be very nicely broken into the three steps above.
To draw this in the picture, we get
\begin{diagram}
	(p) & \rTo & \kp_1 \dots \kp_g & \rTo & \kp_1 \dots \kp_g & \rTo & (\kp_1 \dots \kp_g)^e \\
	\{f_i\}: && 1,\dots,1 && f,\dots,f && f,\dots,f \\
	\QQ & \hLine_{\text{Split}} & K^D & \hLine_{\text{Inert}} & K^I & \hLine_{\text{Ramify}} & K
\end{diagram}
In any case, in the ``typical'' case that there is no ramification,
we just have $K^I = K$.

\section{Tangential remark: more general Galois extensions}
All the discussion about Galois extensions
carries over if we replace $K/\QQ$ by some different Galois extension $K/F$.
Instead of a rational prime $p$ breaking down in $\OO_K$,
we would have a prime ideal $\kp$ of $F$ breaking down as
\[ \kp \cdot \OO_L = (\kP_1 \dots \kP_g)^e \]
in $\OO_L$ and then all results hold verbatim.
(The $\kP_i$ are primes in $L$ above $\kp$.)
Instead of $\FF_p$ we would have $\OO_F/\kp$.

The reason I choose to work with $F = \QQ$ is that capital Gothic $P$'s ($\kP$)
look \emph{really} terrifying.

\section\problemhead
\todo{more problems}
% Cyclic Galois groups?

\begin{dproblem}
	Prove that no rational prime $p$ can remain inert in
	$K = \QQ(\cbrt2, \omega)$, the splitting field of $x^3-2$.
	How does this generalize?
	\begin{hint}
		Show that no rational prime $p$ can remain
		inert if $\Gal(K/\QQ)$ is not cyclic.
		Indeed, if $p$ is inert then $D_p \cong \Gal(K/\QQ)$.
	\end{hint}
\end{dproblem}


%!TEX root =  ../main.tex

\subsection{Inside and Outside}

\objective{Graph and apply absolute value transformation}

\index{Absolute Value!Transformation}
Consider what applying absolute value does as a \emph{transformation}.
First of all, what would $y=|f(x)|$ do?  Obviously, this will prevent $y$ from ever being negative.
But what happens to those negative segments of the function?  They --- and they alone ---
are reflected over the $x$-axis.  This creates cusps at every turn, moments that will
be un-differentiable.

What if we try $f(|x|)$?  This will cause negative inputs to receive the output as if they were
positive.  Graphically, this means the left-half of our graph will be the mirror image of the
right half.

\index{Absolute Value!of motion, terms}
\subsection{Distance, Velocity, Jerk}
Absolute value was invented to describe distance.  The verbal question ``how far is $x_1$
from $x_2$?'' is best represented as $|x_1-x_2|$.  Typically, we have some function modeling
\textbf{position} and then we contrast that position with something else,
and are asking a question of  \textbf{distance}.  What would have to be true about
some graph to leave it susceptible to change
by the application of absolute values?  How can absolute value transformation produce
undifferentiable moments in a graph?

Another common function to deal with in physics is a speed graph.  This too can be negative,
which when converted to \textbf{velocity}, might make issues. Another source of difficulty
is when we disregard the sign of our input, effectively transforming into $f(|x|)$.  Thinking
graphically, how would the left side correspond to the right?  What kind of symmetry would
such a transformation enforce?  The derivative of speed is position.

Lastly, we will often consider acceleration.  Most processes effected by acceleration
do not care if it is positive or negative (deceleration).  The absolute value of the
acceleration is sometimes called  ``jerk'', though this term is not universally standardized.
The derivative of acceleration is speed.

Studies have begun to come out about higher orders, functions whose derivative is acceleration
(called snap, crackle, and pop), but these terms are so rarely needed that the names are trivial.