\input{../common/common.tex}

\title{Math notes - Groovy Numbers}
\author{Uwe Hoffmann}
\hypersetup{colorlinks, pdftitle={Math notes - Groovy Numbers}}

\begin{document}

\setcounter{chapter}{1}
\section*{Groovy Numbers}

\vspace{10 mm}
\begin{problem}
$x \in \mathbb{R}$ is said to be a groovy number iff $\exists\  n \in \mathbb{N}$ such that $x = \sqrt{n} + \sqrt{n + 1}$. Prove that if $x$ is groovy, then $\forall r \in \mathbb{N}:\ x^r$ is groovy.
\end{problem}

\subsection{Binomial Expansion}

In this section we explore a property of the binomial power expansion

\begin{equation*}
(a + b)^r = \sum_{k = 0}^r \binom{r}{k} a^{r - k} b^k
\end{equation*}

\noindent We define $\mathbb{N}_r = \{k \in \mathbb{N}_0: 0 \leq k \leq r\}$ and its partition into two subsets \mbox{$\mathbb{N}_r = \mathbb{E}_r \cup \mathbb{O}_r$}, with  \mbox{$\mathbb{E}_r = \{k \in \mathbb{N}_r: k = 2 u, u \in \mathbb{N}_0 \}$} and \\
\mbox{$\mathbb{O}_r = \{k \in \mathbb{N}_r: k  = 2 u + 1, u \in \mathbb{N}_0 \}$}. We then partition the binomial power expansion into two sums:

\begin{equation*}
(a + b)^r = \sum_{k = 0}^r \binom{r}{k} a^{r - k} b^k = \sum_{k \in \mathbb{E}_r}  \binom{r}{k} a^{r - k} b^k + \sum_{k \in \mathbb{O}_r}  \binom{r}{k} a^{r - k} b^k
\end{equation*}

\noindent Let

\begin{equation*}
E(a, b, r) = \sum_{k \in \mathbb{E}_r}  \binom{r}{k} a^{r - k} b^k \ \text{and} \ O(a, b, r) = \sum_{k \in \mathbb{O}_r}  \binom{r}{k} a^{r - k} b^k
\end{equation*}

\noindent Then

\begin{align*}
(a^2 - b^2)^r &= (a + b)^r (a - b)^r \\
&= (E(a, b, r) + O(a, b, r)) (E(a, -b, r) + O(a, -b, r))
\end{align*}

\noindent But

\begin{equation*}
E(a, -b, r) = E(a, b, r) \ \text{and}\  O(a, -b, r) = - O(a, b, r)
\end{equation*}

\noindent so

\begin{align*}
(a^2 - b^2)^r &= (a + b)^r (a - b)^r \\
&= (E(a, b, r) + O(a, b, r)) (E(a, -b, r) + O(a, -b, r)) \\
&= (E(a, b, r) + O(a, b, r)) (E(a, b, r) - O(a, b, r)) \\
&= E(a, b, r)^2 - O(a, b, r)^2
\end{align*}

\noindent We therefore proved

\begin{lem}\label{even_odd}
\begin{equation*}
(a^2 - b^2)^r = E(a, b, r)^2 - O(a, b, r)^2
\end{equation*}
\end{lem}

\subsection{Solution}

Using lemma \ref{even_odd} with $a = \sqrt{n}$ and $b = \sqrt{n + 1}$, we get

\begin{equation}
(-1)^r = E(\sqrt{n}, \sqrt{n + 1}, r)^2 - O(\sqrt{n}, \sqrt{n + 1}, r)^2\tag{L}
\end{equation}

\begin{lem}\label{squared}
\begin{gather*}
E(\sqrt{n}, \sqrt{n + 1}, r)^2 \in \mathbb{N}, \\
O(\sqrt{n}, \sqrt{n + 1}, r)^2 \in \mathbb{N}
\end{gather*}
\end{lem}

\begin{proof}

\noindent We will look at two cases: $r$ even and $r$ odd.

\noindent \textbf{Case 1.} For $r = 2 u$ even we have

\begin{align*}
E(\sqrt{n}, \sqrt{n + 1}, 2 u) &=  \sum_{k = 0}^u  \binom{2 u}{2 k} (\sqrt{n})^{2 u - 2 k} (\sqrt{n + 1})^{2 k} \\
&=  \sum_{k = 0}^u  \binom{2 u}{2 k} (\sqrt{n})^{2 (u - k)} (\sqrt{n + 1})^{2 k} \\
&=  \sum_{k = 0}^u  \binom{2 u}{2 k} n^{u - k} (n + 1)^k \\
\end{align*}

\noindent so $E(\sqrt{n}, \sqrt{n + 1}, r) \in \mathbb{N}$, and therefore $E(\sqrt{n}, \sqrt{n + 1}, r)^2 \in \mathbb{N}$.

\begin{align*}
O(\sqrt{n}, \sqrt{n + 1}, 2 u) &=  \sum_{k = 0}^{u - 1}  \binom{2 u}{2 k + 1} (\sqrt{n})^{2 u - 2 k - 1} (\sqrt{n + 1})^{2 k + 1} \\
&=  \frac{\sqrt{n + 1}}{\sqrt{n}} \sum_{k = 0}^{u - 1}  \binom{2 u}{2 k + 1} (\sqrt{n})^{2 (u - k)} (\sqrt{n + 1})^{2 k} \\
&=  \frac{\sqrt{n + 1}}{\sqrt{n}} \sum_{k = 0}^{u - 1}  \binom{2 u}{2 k + 1} n^{2 (u - k)} (n + 1)^{k} \\
&=  \sqrt{n (n + 1)} \sum_{k = 0}^{u - 1}  \binom{2 u}{2 k + 1} n^{2 (u - k) - 1} (n + 1)^{k} \\
\end{align*}

\noindent so $O(\sqrt{n}, \sqrt{n + 1}, r)^2 \in \mathbb{N}$.

\noindent \textbf{Case 2.}  $r = 2 u + 1$ is handled in a similar fashion by factoring out $\sqrt{n}$ and $\sqrt{n + 1}$ with the remainder $\in \mathbb{N}$.

\end{proof}

\noindent From lemma \ref{squared} and equation (L) it follows that  $E(\sqrt{n}, \sqrt{n + 1}, r)^2$ and $O(\sqrt{n}, \sqrt{n + 1}, r)^2$ are consecutive natural numbers. Let

\begin{equation*}
m = min(E(\sqrt{n}, \sqrt{n + 1}, r)^2, O(\sqrt{n}, \sqrt{n + 1}, r)^2) \in \mathbb{N}
\end{equation*}

\noindent Then

\begin{equation*}
x^r = (\sqrt{n} + \sqrt{n + 1})^r = \sqrt{m} + \sqrt{m + 1}
\end{equation*}

\end{document}


\chapter{Frequency Mismatch}
\section{Carrier frequency mismatch in single TX/RX}
Considering the transmitted In-phase signal $S_I(t)$, Quad-phase signal $S_Q(t)$ and the carrier frequency of
transmitter $f_{c1}$. The transmitted signal can be writted as $S(t)$

\begin{equation}
S(t) = S_I(t) \times cos(2\pi \times f_{c1} \times t ) + S_Q(t) \times sin(2\pi \times f_{c1} \times f)
\end{equation}

Ignoring the channel characteristic and absense of noice, the received signal $R(t) = S(t)$. If there is a
mismatch in carrier frequency, the receiver carrier frequency $f_{c2} \neq f_{c1}$. The received In-phase signal
can be expressed as \\
$r_I(t) = R(t) \times cos(2\pi \times f_{c2} \times t)$.

\begin{eqnarray}
r_I(t) = \left[S_I(t) \times cos(\omega_{c1}t) + S_Q(t) \times sin(\omega_{c1}t)\right] \times cos(\omega_{c2}t) \\
r_I(t) = S_I(t) \times cos(\omega_{c1}t) \times cos(\omega_{c2}t) + S_Q(t) \times sin(\omega_{c1}t) \times cos(\omega_{c2}t)
\end{eqnarray}

Where $\omega_{c1,2} = 2\pi\times f_{c1,2}$. Simplifying the equation by product to sum identities.
\begin{equation}
r_I(t) =
\tfrac{1}{2}S_I(t)\left[cos((\omega_{c1}-\omega_{c2})t) + cos((\omega_{c1}+\omega_{c2})t)\right] +
\tfrac{1}{2}S_Q(t)\left[sin((\omega_{c1}-\omega_{c2})t) + sin((\omega_{c1}+\omega_{c2})t)\right]
\end{equation}

By removing high frequency components ($\omega_{c1}+\omega_{c2}$). In-phase baseband signal $r_{IB}(t)$ can be expressed
\begin{equation}
r_{IB}(t) = \tfrac{1}{2}S_I(t)\times cos(\Delta\omega t) - \tfrac{1}{2}S_Q(t)\times sin(\Delta\omega t)
\end{equation}

Similarly, Quad-phase baseband signal $r_{QB}(t)$
\begin{equation}
r_{QB}(t) = \tfrac{1}{2}S_I(t)\times sin(\Delta\omega t) + \tfrac{1}{2}S_Q(t)\times cos(\Delta\omega t)
\end{equation}

where $\Delta\omega = \omega_{c2} - \omega_{c1}$. By Rearranging these queations, we can express the
following.
\[
\begin{bmatrix}
	r_{IB}(t) \\
	r_{QB}(t)
\end{bmatrix}
=
\begin{bmatrix}
	cos(\Delta\omega t) & -sin(\Delta\omega t) \\
	sin(\Delta\omega t) & cos(\Delta\omega t)
\end{bmatrix}
\times
\begin{bmatrix}
	{}^1/_2\cdot S_I(t)\\
	{}^1/_2\cdot S_Q(t)\\
\end{bmatrix}
\]
We can clearly see that the frequency mismatch is equivalent to the rotation
of complex coordinate with angular velocity $\Delta\omega$. Hense, by
measuring the rotation speed and direction, we can back calculate the
frequency offset between receiver and transmitter. In the O-QPSK modulation,
I/Q signal at any given time map to a unit circle on constellation.
Measuring both clockwise and counter clockwise rotation angle at fixed
interval tells the receiver's carrier frequency is leading or legging.
With this information, radio is able to self-compensate the frequency
mismatch and we called it \textit{Automatic Frequency Compensation} or AFC.

\begin{table}
	\centering
	\begin{tabular}{|c|c|c|} \hline
		\rowcolor[gray]{0}
		  {\sc {\color{white} AGC mode}}
		& {\sc {\color{white} AFC mode}}
		& {\sc {\color{white} ARR}}
		\\ \hline
		SFD-latch 	& Enable 	& 93.3\%	\\ \hline
		Continuous	& Enable	& 95.5\%	\\ \hline
		SFD-latch 	& Disable	& 94.5\%	\\ \hline
		Continuous	& Disable	& 95.1\%	\\ \hline
	\end{tabular}
    \caption{Acknowledgment reception rate (ARR) for two constructively
interfering transmitters with respect to different AGC modes and AFC mode.
We transmitted 10,000 ACKs per transmitter per experiment. With AFC disable,
carrier frequencies of TX and RX is off by 16.4KHz. In both case of AFC mode,
continuous AGC works better. Enabling the AFC worsen the reception rate for
SFD-Latch AGC simply because the baseband signal is attenuated by low-frequency
envelope.}
	\label{tab:ARR_versus_agc_afc_mode}
\end{table}

From table ~\ref{tab:ARR_versus_agc_afc_mode}, data shows that the ARR decrease
in SFD-Latch type AGC when AFC is enabled. Since the AFC tries to sompensate the
frequency difference between TX and RX, the duration of local minimum becomes
larger whereas no significant change on Continuous AGC mode. Continuous AGC with
AFC could potentially increase the reception rate in longer identical packet
collision.

\subsection{Automatic Frequency Compensation}
The 802.15.4 uses O-QOSK modulation. At any given of time, the in-phase and quad-phase
have only 8 combinations which is listed in table~\ref{tab:demux}. The key concept
is that the in-phase and quad-phase signal vector is always on the unit circle of
complex plane. Moreover, the signal vector is always rotating at angular velocity
$\omega = \tfrac{\pi/2}{0.5 \mu s}$. However, if the frequency of transmitter
and receiver doesn't match, the angular velocities toward clockwise and counter-clockwise
are different. Thus, by sampling the vector at constant rate and calculate angle difference
toward two different direction, we are able to tell the frequency is leading or lagging.
In usdr, we implemented a simple cordic core to measure the angle of input vector with
sampling rate $= 250 Ksps$. The delta angle should be $\pm\pi/4$ if frequencies are perfectly
matched. By comparing the delta angle with $\pm\pi/4$, radio is able to self-align
its carrier frequency to the incoming packet.
\begin{figure}[h]
\centering
	\includegraphics[width=0.6\columnwidth]{frequency_compensate/freq_compensate}
	\caption{}
	\label{fig:afc_freq_error}
\end{figure}

Figure~\ref{fig:afc_freq_error} shows capability of AFC to correct the frequency
mismatch. The original frequency mismatch is set to be 50 KHz. The figure shows how
much frequency mismatch after automatic frequency compensation within a packet length.
In these simulations, the packet lengths are set to be 127 and 64 bytes. The frequency
step size in both simulation is 300 Hz. Thus, if the RMS frequency error $<$ 300 Hz,
the AFC successfully compensated all frequency mismatch. We can clearly see that
for larger SNR, AFC works better. In addition, more opportunities to compensate the
frequency in longer packet. Hence, frequency error is smaller in 127 bytes packet.

\subsection{Demodulation with frequency mismatch}
\label{1tx1rx_mismatch}
Recall the demodulation equation $d(t) = S_Q(t)\times S_I(t-\tau) - S_Q(t-\tau)\times S_I(t)$.
Under the frequency mismatch scenario, the recieved $r_{IB}(t)$ and $r_{QB}(t)$ can be substituded by $S_I(t)$ and $S_Q(t)$.
$d_f(t)$ can be written in following:
\begin{align}
d_f(t) = &
\left[-\tfrac{1}{2}S_I(t)sin(\Delta\omega t)+\tfrac{1}{2}S_Q(t)cos(\Delta\omega t)\right] \times
\left[\tfrac{1}{2}S_I(t-\tau)cos(\Delta\omega(t-\tau))+\tfrac{1}{2}S_Q(t-\tau)sin(\Delta\omega(t-\tau))\right] \nonumber \\
&  -\left[-\tfrac{1}{2}S_I(t-\tau)sin(\Delta\omega(t-\tau))+\tfrac{1}{2}S_Q(t-\tau)cos(\Delta\omega(t-\tau))\right] \times
\left[\tfrac{1}{2}S_I(t)cos(\Delta\omega t) + \tfrac{1}{2}S_Q(t)sin(\Delta\omega t)\right]
\end{align}

Assuming that the $\Delta\omega\times\tau$ is small enough.
\begin{align}
cos(\Delta\omega(t-\tau))& \approx cos(\Delta\omega t) \\
sin(\Delta\omega(t-\tau))& \approx sin(\Delta\omega t)
\end{align}

The equation can be further simplified.
\begin{align}
d_f(t) \approx &
\left[-\tfrac{1}{2}S_I(t)sin(\Delta\omega t)+\tfrac{1}{2}S_Q(t)cos(\Delta\omega t)\right] \times
\left[\tfrac{1}{2}S_I(t-\tau)cos(\Delta\omega t)+\tfrac{1}{2}S_Q(t-\tau)sin(\Delta\omega t)\right] \nonumber \\
& -\left[-\tfrac{1}{2}S_I(t-\tau)sin(\Delta\omega t)+\tfrac{1}{2}S_Q(t-\tau)cos(\Delta\omega t)\right] \times
\left[\tfrac{1}{2}S_I(t)cos(\Delta\omega t) + \tfrac{1}{2}S_Q(t)sin(\Delta\omega t)\right]\\
\approx & \left[-\tfrac{1}{4}S_I(t)S_Q(t-\tau)sin^2(\Delta\omega t) + \tfrac{1}{4}S_I(t-\tau)S_Q(t)cos^2(\Delta\omega t)\right]\nonumber\\
& -\left[\tfrac{1}{4}S_I(t)S_Q(t-\tau)cos^2(\Delta\omega t) -\tfrac{1}{4}S_I(t-\tau)S_Q(t)sin^2(\Delta\omega t)\right]\\
\approx &\tfrac{1}{2}\left[S_Q(t)S_I(t-\tau) - S_Q(t-\tau)S_I(t)\right]
\end{align}

The decoded equation $d_f(t)$ is linear scaling of $d(t)$. The sign of $d_f(t)$ and $d(t)$ are identical. Therefore,
the decoder scheme still function if there is a mismatch between transmitter and receiver.

\clearpage
\section{Carrier frequency mismatch in double TX, single RX}
\label{sec:2tx1rx}
In this scenario, two TX node transmit the identical baseband signal simultaneously. Assuming the TX nodes have different
carrier frequency $f_{c1}$ and $f_{c2}$, and receiver's frequency is $f_{cr}$. We simply assume that $R(t) = S(t)$.
\begin{equation}
R(t) = S(t) = S_I(t)\left[cos(\omega_{c1}t) + cos(\omega_{c2}t)\right] + S_Q(t)\left[sin(\omega_{c1}t) + sin(\omega_{c2}t)\right]
\end{equation}

Simplifying the equation by sum-to-product identities.
\begin{align}
R(t)& = S_I(t)\left[2cos(\tfrac{\omega_{c1}+\omega_{c2}}{2}t)cos(\tfrac{\omega_{c1}-\omega_{c2}}{2}t)\right] +
S_Q(t)\left[2sin(\tfrac{\omega_{c1}+\omega_{c2}}{2}t)cos(\tfrac{\omega_{c1}-\omega_{c2}}{2}t)\right] \\
& = cos(\tfrac{\omega_{c1}-\omega_{c2}}{2}t)\left[
2S_I(t)cos(\tfrac{\omega_{c1}+\omega_{c2}}{2}t) + 2S_Q(t)sin(\tfrac{\omega_{c1}+\omega_{c2}}{2}t) \right]
\end{align}

The received In-phase signal $r_I(t)$ can be expressed as following:
\begin{equation}
r_I(t) = R(t) \times cos(\omega_{cr}t) \\
\end{equation}
\begin{align}
r_I(t) =  cos(\tfrac{\omega_{c1}-\omega_{c2}}{2}t)\{
&S_I(t)\left[cos(\tfrac{\omega_{c1}+\omega_{c2}-2\omega_{cr}}{2}t) + cos(\tfrac{\omega_{c1}+\omega_{c2}+2\omega_{cr}}{2}t)\right] + \nonumber \\
&S_Q(t)\left[sin(\tfrac{\omega_{c1}+\omega_{c2}-2\omega_{cr}}{2}t) + sin(\tfrac{\omega_{c1}+\omega_{c2}+2\omega_{cr}}{2}t)\right]
\}
\end{align}

Let $\Delta\omega_1 = (\omega_{c1} - \omega_{cr})$ and $\Delta\omega_2 = (\omega_{c2} - \omega_{cr})$. By removing
high frequency tems, $r_{IB}(t)$ can be simplified.
\begin{equation}
r_{IB}(t) = cos(\tfrac{\Delta\omega_1-\Delta\omega_2}{2}t)\left[
S_I(t)cos(\tfrac{\Delta\omega_1+\Delta\omega_2}{2}t) + S_Q(t)sin(\tfrac{\Delta\omega_1+\Delta\omega_2}{2}t)\right]
\end{equation}

Similarily, the received Quad-phase baseband signal $r_{QB}(t)$ can be derived from $R(t)\times sin(\omega_{cr}t)$.
\begin{equation}
r_{QB}(t) = cos(\tfrac{\Delta\omega_1-\Delta\omega_2}{2}t)\left[
-S_I(t)sin(\tfrac{\Delta\omega_1+\Delta\omega_2}{2}t) + S_Q(t)cos(\tfrac{\Delta\omega_1+\Delta\omega_2}{2}t)\right]
\end{equation}

From section~\ref{1tx1rx_mismatch}, we can conclude that if $(\Delta\omega_1-\Delta\omega_2)t$ is small enough, the
decoding equation just simply scaling by $cos^2((\Delta\omega_1-\Delta\omega_2)t)$, which is always greater than zero.
The sign of decoding equation remains the same as no frequency mismatch.

\section{Envelop Modeling}
\subsection{Theoretical Method}
\label{sec:envelop_modeling}
Considering a simplified model, there are two transmitters and one receiver with their carrier frequency
$f_{c1}$, $f_{c2}$ and $f_{cr}$ respectively. Assuming that $f_{cr} = f_{c1}$ and $S_Q(t) = 0$, the received
in-phase baseband signal can be expressed as:
\begin{equation}
r_I(t) = S_I(t)\times cos^2((\Delta\omega_2/2)\times t)
\label{eq:sec:envelop_modeling:1}
\end{equation}
Thus, from equation~\ref{eq:sec:envelop_modeling:1}, the low-frequency envelop can be observed intuitively.
The period $T$ of the envelope is equal to $\tfrac{1}{\Delta f_2/2}\times \tfrac{1}{2} = \tfrac{1}{\Delta f_2}$.
Where $\Delta f_2 = \tfrac{\Delta\omega_2}{2\pi}$.
We can define a threshold $\alpha$ which the receiver makes false decision once the received signal's amplitude
below $\alpha$. The time ($t$) which the signal's amplitude is queal to threshold ($\alpha$) can be expressed as:
\begin{eqnarray}
cos^2(2\pi\tfrac{\Delta\omega_2}{2}\times t) = \alpha \\
\tfrac{\Delta\omega_2 t}{2} = cos^{-1}(\sqrt{\alpha}) \\
t = \tfrac{cos^{-1}(\sqrt{\alpha})}{\Delta\omega_2/2} \\
t = \tfrac{cos^{-1}(\sqrt{\alpha})}{\pi\Delta f_2} \\
\end{eqnarray}

Thus, we defined $\gamma$, which is the proportion of signal below threshold ($\alpha$) to the entire
period ($T$) can be calculated as:
\begin{align}
\gamma 	&= \tfrac{T/2-t}{T/2}	\\
		&= \tfrac{\tfrac{1}{2\Delta f_2} - \tfrac{cos^{-1}(\sqrt{\alpha})}{\pi\Delta f_2}}{\tfrac{1}{2\Delta f_2}}	\\
		&= 1 - \tfrac{2}{\pi}cos^{-1}(\sqrt{\alpha})
		\label{eq:sec:envelop_modeling:alpha}
\end{align}

From equation~\ref{eq:sec:envelop_modeling:alpha}, we find that the proportion $\gamma$ is a function
of $\alpha$ only. In other words, the proportion remains the same no matter what $\Delta f_2$ is.
Thus, in this simplified model, assuming that all the false decoding comes from the amplitude
attenuation by $cos^2(\Delta\omega_2/2)$. The time duration $D$ while signal below threshold ($\alpha$)
can be expressed as:
\begin{align}
D 	&= \gamma \times T\\
	&= \left[1 - \tfrac{2}{\pi}cos^{-1}(\sqrt{\alpha})\right]\times\tfrac{1}{\Delta f_2} (s)
\end{align}

In 802.15.4, the period of each chip is 0.5$\mu$s and each byte occupies 32$\mu$s in time. Therefore, the
relationship between number of chips $n$ in each duration $D$ and total number of envelopes $m$ in
a given lengh packet $L$ bytes can be written as:
\begin{eqnarray}
n = D/0.5\mu s \\
m = L\times 32\mu s/T
\end{eqnarray}

\begin{table}
\begin{center}
	\begin{tabular}{c||c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c c}
			& 0& 1& 2& 3& 4& 5& 6& 7& 8& 9&10&11&12&13&14&15	\\ \hline\hline
		0	&  &14&15&14&16&13&15&13&31&17&16&17&15&18&16&18 	\\ \hline
		1	&14&  &13&16&14&15&13&15&17&31&18&15&17&16&18&16 	\\ \hline
		2	&15&13&  &13&15&14&16&14&16&18&31&18&16&17&15&17	\\ \hline
		3	&14&16&13&  &14&15&13&15&17&15&18&31&17&16&18&16	\\ \hline
		4	&16&14&15&14&  &13&15&13&15&17&16&17&31&18&16&18	\\ \hline
		5	&13&15&14&15&13&  &14&16&18&16&17&16&18&31&17&15	\\ \hline
		6	&15&13&16&13&15&14&  &14&16&18&15&18&16&17&31&17	\\ \hline
		7	&13&15&14&15&13&16&14&  &18&16&17&16&18&15&17&31	\\ \hline
		8	&31&17&16&17&15&18&16&18&  &14&15&14&16&13&15&13	\\ \hline
		9	&17&31&18&15&17&16&18&16&14&  &13&16&14&15&13&15	\\ \hline
		10	&16&18&31&18&16&17&15&17&15&13&  &13&15&14&16&14	\\ \hline
		11	&17&15&18&31&17&16&18&16&14&16&13&  &14&15&13&15	\\ \hline
		12	&15&17&16&17&31&18&16&18&16&14&15&14&  &13&15&13	\\ \hline
		13	&18&16&17&16&18&31&17&15&13&15&14&15&13&  &14&16	\\ \hline
		14	&16&18&15&18&16&17&31&17&15&13&16&13&15&14&  &14	\\ \hline
		15	&18&16&17&16&18&15&17&31&13&15&14&15&13&16&14&
	\end{tabular}
	\caption{Chip distance table. This table shows the distance between any two chip sequence
		in codebook. The minimum distance two sequences is 13 chips}
	\label{tab:chip_distance}
\end{center}
\end{table}

Given $\alpha, \Delta f_2$ and $L$, we can calculate $n, m$. For example, $\alpha = 0.05$,
$\Delta f_2 = 15$ KHz and $L = 127$ bytes. $n = 4.24, m = 60.96$. Decoding a 802.15.4 symbol
requires a distance calculation between a received sequence and 16 possible sequence in codebook.
From table~\ref{tab:chip_distance}, any chip sequence can be correctly decoded if the number of
bit errors is less than 13. Furthermore, we define a probability $p_{single}$ which is the
probability that a single bit error while the amplitude is below threshold $\alpha$. In the duration
$D$, the number of bit errors should less than 13 for symbol to be able to decode correctly. We derive
a probability $p_{correct}$, which is the probability that the symbol is able to be decoded correctly.
\begin{equation}
p_{correct} = \sum_{i=0}^{12} \dbinom{n}{i}\times(p_{single})^i\times(1-p_{single})^{n-i}
\end{equation}

Thus, the packet error rate ($PER$) and be can be expressed by $p_{correct}$ and $m$.
\begin{equation}
	PER = 1 - (p_{correct})^m
	\label{eq:sec:envelop_modeling:per}
\end{equation}

\begin{figure*}
\centering
	\centering
		\subfigure[Theoretical Modeling]{
			\includegraphics[width=0.45\columnwidth]{envelop_modeling/per_deltaf}
			\label{fig:envelop_modeling_the}
		}
		\subfigure[Simulated Modeling]{
			\includegraphics[width=0.45\columnwidth]{envelop_modeling_sim/per_deltaf_sim}
			\label{fig:envelop_modeling_sim}
		}
	\caption{Figure~\ref{fig:envelop_modeling_the} simulates the PER using equation~\ref{eq:sec:envelop_modeling:per}.
	Whereas the figure~\ref{fig:envelop_modeling_sim} uses 802.15.4 transceiver architectural model. The parameters
	for both plots are: $L = 127$ Bytes, $\alpha = 0.05$, $\Delta f_1 = 0, \Delta f_2$ from 10 to 20 KHz. we can
	find the trend that the PER drops while $\Delta f_2$ increases in both plots. Since the number of consecutive
	contaminated bits get shorter in larger frequency separation, the possibility for receiver to false decode a
	packet become smaller.}
	\label{fig:envelop_modeling}
\end{figure*}

Figure~\ref{fig:envelop_modeling_the} shows the simulation result of packet error rate (PER) versus
$\Delta f_2$. Parameters of this simulation are: $L = 127$ bytes, $\alpha = 0.05$, $p_{single} = 0.2$
and $\Delta f_2$ from 100 to 20 KHz. Since number of chips $n$, which below threshold $\alpha$ drops
while increasing the frequency. Even number of evnelops $m$ increases but the correct probability
$p_{correct}$ is close or equal to 1 (if $n$ $<$ chip distance, 13). In contrast, low frequency separation
dramatically deteriorates the $p_{correct}$. The PER is much worse even if the number of envelop $m$ is small.
However, the equations we derived above don't hold under a special case, which is the period of envelop ($T$)
is somehow greater than packet length. Imaging when $m \to \infty$, the PER should be 0 instead of 1. Thus, if the
condition $(1-\gamma)T + (6.5 \mu s(chip distance)/p_{single}) > L\times32 \mu s$ meets, the PER can be written as:
\begin{align}
	PER &= 1 - p_{correct} \\
		&= 1 - \tfrac{(1-\gamma)T - L\times32\mu s + 6.5\mu s/p_{single}}{T - L\times32 \mu s}
\end{align}
Where the $6.5 \mu s/p_{single}$ is the expection value of numbers of error chip, and
$(1-\gamma)T - L\times32 \mu s + 6.5 \mu s/p_{single}$ is the length for correct decoding. Namely,
if the packet starts within the envelop any moment from 0 $\sim$ numerator, the total error chips is less
than 13 (chip distance). The denominator is the total possible starting point.

\subsection{Architectural Method}
From section~\ref{sec:2tx1rx}, we derived the in-phase and quad-phase signal if the frequency present
in two transmitters and one receiver. We build a complete models of uSDR's transceiver.
Figure~\ref{fig:envelop_modeling_sim_IQ} shows the received in-phase and quad-phase signal with
$\Delta f_2 = 15$ KHz. Thus, the period $T$ of envelops is $\tfrac{1}{\Delta f_2} = 66.67 \mu$s.
\begin{figure}[h]
\centering
	\includegraphics[width=0.98\columnwidth]{envelop_modeling_sim/IQ}
	\caption{This figure shows the received in-phase and quad-phase baseband signals in simulation. The
	parameters of this simulation are: $\Delta f_1 = 0, \Delta f_2 = 15$ KHz. We can see that the period
	of envelop $T = \tfrac{200 \mu s}{3} = 66.67 \mu s = \tfrac{1}{15 KHz} = \tfrac{1}{\Delta f_2}$}
	\label{fig:envelop_modeling_sim_IQ}
\end{figure}

In the architectural based simulation, we add Gaussian noise to the signal and setting the signal to noise
ration (SNR) to 15dB. In addition to that, we set a threshold, which similar to $\alpha$. If the amplitude
less than $\alpha$, instead of original signal, we randomly generate the noise bounded by $\alpha$.
From figure~\ref{fig:envelop_modeling_sim}, we can see trend of PER from architectural simulation is similar
to the theoretical method. However, in architectural simulation, the PER never goes to 0 even the $\Delta f$
is large enough. The resaon is the AWGN present and the random noise we added. The random noise wee add not
only exist on low-frequency envelop but the entire signal. Namely, every chip in the sequence is affected by
the noise. Thus, the architectural method has less "effective" chip distance.


% !TeX root = ../main.tex
% Add the above to each chapter to make compiling the PDF easier in some editors.

\chapter{Summary of Notation}

\begin{fullwidth}
We follow these general rules: \begin{itemize}[noitemsep]
    % \item uppercase italic for constants $N$
    \item lowercase italic for indices $i$ and scalar variables $a$
    % \item lowercase italic bold for vectors $\vx$
    \item uppercase italic bold for matrices $\mM$
    % \item uppercase italic for random variables $X$
    % \item uppercase bold for random vectors $\rX$
    \item uppercase italic for sets $A$
    % \item uppercase calligraphy for spaces (usually infinite sets) $\spA$
\end{itemize}

\emptyparagraph\begin{longtable}{p{2cm}l}
    $\defeq$ & equality by definition \\
    $\N$ & set of natural numbers $\{1, 2, \dots\}$ \\
    $\NZ$ & set of natural numbers, including $0$, $\N \cup \{0\}$ \\
    $[n]$ & set of natural numbers from $1$ to $n$, $\{1, 2, \dots, n-1, n\}$ \\
    $\Z$ & set of integers $\{\dots, -2, 1, 0, 1, 2, \dots\}$ \\
    $\R$ & set of real numbers \\
    $\C = \R^2$ & set of complex numbers \\
    $A \cupdot B$ & disjoint union of sets $A$ and $B$ \\
    $f : A \to B$ & function $f$ from elements of set $A$ to elements of set $B$ \\
    $\divides{a}{b}$ & $a$ is a divisor of $b$ \\
\end{longtable}

\section*{\smallcaps{Groups}}
\begin{longtable}{p{2cm}l}
    $U \subgroup G$ & $U$ is a subgroup of $G$ \\
    $\gen{M} \subgroup G$ & for $M \subseteq G$, the subgroup generated by $M$ \\
    $\gen{a}$ & the cyclic group $\gen{\{a\}}$ \\
    $o(a)$ & order of element $a$, $|\gen{a}|$ \\
    $aU$ & left coset of $a \in G$ and $U \subgroup G$, $\{a \cdot u \mid u \in U\}$ \\
    $Ua$ & right coset of $a \in G$ and $U \subgroup G$, $\{u \cdot a \mid u \in U\}$ \\
    $[G:U]$ & index of $U \subgroup G$ in $G$, $|\{aU \mid a \in G\}|$ \\
    $\ker \varphi$ & kernel of homomorphism $\varphi : G \to H$, $\{a \in G \mid \varphi(a) = e_H\} \subseteq G$ \\
    $\im \varphi$ & image of homomorphism $\varphi : G \to H$, $\{\varphi(a) \mid a \in G\} \subseteq H$ \\
    $G \cong H$ & $G$ and $H$ are isomorphic \\
    $N \normal G$ & $N$ is a normal subgroup of $G$ \\
    $\rep{a} = a N$ & the (left) coset of some $a \in G$ in the context of the quotient group $\Quot{G}{N}$ \\
    \addlinespace
    $\GL{n}{K}$ & \emph{general linear group} over invertible linear maps $\mA \in K^{n \times n}$, $\det{\mA} \neq 0$ \\
    $\SL{n}{K}$ & \emph{special linear group} over volume-preserving linear maps $\mA \in K^{n \times n}$, $\det{\mA} = 1$ \\
    $S_n$ & \emph{symmetric group} over bijections on $[n]$ (so-called permutations) \\
    $A_n$ & \emph{alternating group} over bijections $\sigma$ on $[n]$ with positive sign, $\sgn{\sigma} = 1$ \\
    $Z(G)$ & \emph{center} of group $G$, $\{a \in G \mid \forall x \in G.\ ax = xa\} \normal G$ \\
    $\Aut{G}$ & \emph{automorphism group} over automorphisms on $G$ \\
    $\Inn{G}$ & \emph{inner automorphism group} over inner automorphisms on $G$ \\
    $\Quot{G}{N}$ & for some $N \normal G$, \emph{quotient group} $G$ modulo $N$ over (left) cosets of $N$ \\
    $n\Z = \gen{n}$ & subgroup $n\Z \subgroup \Z$ of multiples of $n \in \Z$ \\
    $Z_n = \Quot{\Z}{n\Z}$ & quotient group $\Z$ modulo $n\Z$, $\gen{\rep{1}} = \{\rep{0}, \rep{1}, \dots, \rep{n-1}\}$ \\
    $\Out{G}$ & \makecell[tl]{\emph{outer automorphism group} over cosets of the inner automorphism group and \\ outer automorphisms on $G$, $\Quot{\Aut{G}}{\Inn{G}}$} \\
    \addlinespace
    $i_g$ & \emph{inner automorphism} of some conjugating element $g \in G$, $i_g : G \to G, x \to g \cdot x \cdot \inv{g}$ \\
\end{longtable}
\end{fullwidth}


\section{Quaternions\label{quaternions}}
\subsection{The need for Quaternions}
Besides its position, each rigid body in 3D space may have an orientation, in which there are
three degrees of freedom (three independent axes to rotate about).
While the position of a body can be neatly represented using Cartesian coordinates, there is
no obvious best way of describing an orientation. The most common schemes describe
it in terms of a rotation operation which transforms a vector in the body's local
coordinates into world coordinates (or \textsl{vice versa}). But again, there are various different
approaches to representing this rotation, all of which have advantages and disadvantages.

\emph{Euler angles} are probably the most intuitive representation of a 3D rotation, describing
it as a series of three rotations about different axes. These axes are fixed by convention, so it
suffices to specify the three angles of rotation. However, this scheme has a number of
drawbacks~\cite{Saunders:PhD,Shoemake:85}: amongst other things, it is possible that rotation
about one of the axes freezes during an animation (``Gimbal lock'').

\emph{Rotation matrices} are commonly used because they are well understood and
allow efficient combination with other linear transformations (scaling
and shearing~-- also translation if homogeneous coordinates are employed). However, ODEs over
rotation matrices are difficult to implement correctly, since this representation uses nine
numbers (a $3\times3$ matrix) to represent three degrees of freedom, thus introducing six
additional side conditions which must be maintained. Not doing so causes skew through numerical
drift~\cite{Saunders:PhD}.

\emph{Quaternions}~\cite{Shoemake:85,Eberly:01,MathWorld:Quaternion} are a popular alternative
to the two previous sche\-mes, and they are used extensively in this project.

\subsection{Definition and properties}
Mathematically, quaternions can be regarded as numbers with one real part and three
distinct imaginary parts:
\begin{equation}
\q{q} = q_w + q_x\qi + q_y\qj + q_z\qk
\end{equation}
where $q_w$, $q_x$, $q_y$ and $q_z$ are real numbers and \qi{}, \qj{} and \qk{} satisfy
\begin{equation}
\qi^2 = \qj^2 = \qk^2 = \qi\qj\qk = -1.
\end{equation}
From this follows that
$\qi\qj = -\qj\qi = \qk$ and
$\qj\qk = -\qk\qj = \qi$ and
$\qk\qi = -\qi\qk = \qj$.
Note that multiplication is not commutative.

We will also need the conjugate and the inverse of a quaternion. In analogy to
complex numbers, these are given respectively by
\begin{eqnarray}
\overline{\q{q}} & = & q_w - q_x\qi - q_y\qj - q_z\qk \label{quatConjugate}\\
\q{q}^{-1} & = & \frac{\overline{\q{q}}}{\norm{\q{q}}^2} \label{quatInverse}\\
\mathrm{where}\quad\quad \norm{q_w + q_x\qi + q_y\qj + q_z\qk} &=&
    \sqrt{q_w^2 + q_x^2 + q_y^2 + q_z^2} \label{quatMagnitude}
\end{eqnarray}

Sometimes we will need to relate a 3D vector to a quaternion with zero real part.
For a given vector $\ve{u} = (u_1, u_2, u_3)^T$ we define the corresponding
quaternion to be
\begin{equation}
\label{vectorToQuat}
\tilde{\ve{u}} = u_1\qi + u_2\qj + u_3\qk.
\end{equation}

The complex constants \qi{}, \qj{} and \qk{} are required for the algebra only, therefore we can
represent a quaternion as four numbers $(q_w,q_x,q_y,q_z)$. It turns out that a quaternion
with unit magnitude ($\norm{\q{q}} = 1$) neatly represents an arbitrary rotation in
3D space, similarly to the way that an ordinary complex number represents a rotation in the 2D
Argand diagram. The condition $\norm{\q{q}} = 1$ reduces the number of degrees of freedom to
three, as required.

Every unit quaternion represents a rotation of angle $\theta$ about an arbitrary axis.
If the axis passes through the origin and has a direction given by the vector
\ve{a} with $\norm{\ve{a}} = 1$, the quaternion describing this rotation is
\begin{equation}
\label{quatRotation}
\q{q} = \cos\left(\frac{\theta}{2}\right) + \tilde{\ve{a}} \sin\left(\frac{\theta}{2}\right).
\end{equation}
It can easily be verified that this quaternion always has unit magnitude. It shall be assumed
throughout this project that the rotation thus described is clockwise (as seen when looking in
the direction of the vector $\ve{a}$) in a right-hand coordinate system, i.e.\ that it is
given by the ``right-hand rule''.

Two rotations can be concatenated by multiplying their quaternions together. The order in which
these rotations are applied is significant, and quaternion multiplication is not commutative,
so the semantics match. The operations are, however, associative. The quaternion product is
obtained simply by multiplying out the components, observing the rules for multiplying \qi{},
\qj{} and \qk{}:
\begin{eqnarray}
\lefteqn{(p_w + p_x\qi + p_y\qj + p_z\qk)(q_w + q_x\qi + q_y\qj + q_z\qk) = } \nonumber\\*
&& (p_w q_w - p_x q_x - p_y q_y - p_z q_z) +
   (p_w q_x + p_x q_w + p_y q_z - p_z q_y) \,\qi + \nonumber\\*
&& (p_w q_y + p_y q_w + p_z q_x - p_x q_z) \,\qj +
   (p_w q_z + p_z q_w + p_x q_y - p_y q_x) \,\qk \label{quatProduct}
\end{eqnarray}

To rotate a vector $\ve{v} = (v_1, v_2, v_3)^T$ by a quaternion \q{q}, we first
convert it into its corresponding quaternion $\tilde{\ve{v}}$ as defined in
equation~\ref{vectorToQuat} and then calculate the quaternion product
\begin{equation}
\label{quatTransform}
\tilde{\ve{v}}' = \q{q}\tilde{\ve{v}}\q{q}^{-1}
\end{equation}

If we expand this formula, we find that the real part of the result is always zero, and
that the rotated vector $\ve{v}' = (v_1', v_2', v_3')^T$ corresponds
to $\tilde{\ve{v}}'$ (i.e.\ $\ve{v}'$ is contained in the three complex parts of
the quaternion product).

Some authors, notably Shoemake~\cite{Shoemake:85}, choose to define the product in
equation~\ref{quatTransform} in
the reverse order. The choice is a matter of convention, since it merely changes the effect
of this operation from being a clockwise to a counter-clockwise rotation. I chose the clockwise
convention because it is consistent with the usual definition of the angular velocity vector
in physics.

Observe that under this convention, if \q{q} is itself a product of quaternions
$\q{q} = \q{q}_n \q{q}_{n-1} \cdots \q{q}_1$, the result is that of first applying the
$\q{q}_1$ rotation, then $\q{q}_2$ etc. In other words, the rotations in a quaternion product
are applied from right to left. To verify that this is the case, the identity
$\overline{\q{p}\, \q{q}} = \q{\overline{q}}\; \q{\overline{p}}$
is useful~\cite{MathWorld:Quaternion}.

\subsection{Quaternion integration}

We have seen that given the torques on a body, a numerical ODE solver can treat each component
of the vector separately to obtain the new angular momentum. Now that we are representing
orientation as a quaternion, how do we compute the change in orientation given the body's angular
velocity?

The instantaneous rate of change of a quaternion \q{q} over time is
usually~\cite{BaraffWitkin:97,Eberly:04,Saunders:PhD} given as
\begin{equation}
\label{quatRateOfChange}
\dot{\q{q}}(t) = \frac{1}{2}\tilde{\ve{\omega}}(t)\q{q}(t)
\end{equation}
where $\tilde{\ve{\omega}}$ is the quaternion corresponding to the angular velocity
vector $\ve{\omega}$. The quaternion $\dot{\q{q}}$ can be fed into an ODE solver which can handle
its four components separately. However, when this is done it is observed that the new orientation
$\q{q}'$ no longer has unit magnitude. This is an inherent property of the definition in
equation~\ref{quatRateOfChange}, and not, as sometimes claimed~\cite{Eberly:04}, merely a matter
of numerical round-off (proof in appendix~\ref{quatIntegrationMagnitude}). Usually this problem
is `solved' by renormalizing the quaternion:
\begin{equation}
\q{q}(t + h) = \frac{\q{q}'}{\norm{\q{q}'}} \quad\quad\mathrm{where}\quad
    \q{q}' = \q{q}(t) + h\dot{\q{q}}(t)
\end{equation}
(using Euler's method for clarity; $\q{q}'$ would be appropriately redefined when using e.g.\ RK4).
This `solution' seemed quite \textsl{ad hoc} to me, and I demonstrated that it returns
erroneous results if the angular velocity is large (see appendix~\ref{quatNormalization}).
I derive an exact algorithm for quaternion integration in appendix~\ref{quatProofs}. Such
an algorithm may be important in aerospace applications, as the NASA patent~\cite{NASA:00}
suggests.


\section{Modules}
\subsection{Definition and Examples}
\begin{definition}
    A module over a ring $R$ (an $R$-module) is a triple $(M,+,\cdot)$ where $(M,+,0)$ for some $0\in M$ is an abelian group and $\cdot:R\times M\to M$ (called scalar multiplication) satisfies, for any $r,r_1,r_2\in R,m,m_1,m_2\in M$:\\
    1. $(r_1+r_2)\cdot m=r_1\cdot m+r_2\cdot m$.\\
    2. $r\cdot (m_1+m_2)=r\cdot m_1+r\cdot m_2$.\\
    3. $r_1\cdot(r_2\cdot m)=(r_1r_2)\cdot m$.\\
    4. $1\cdot m=m$.
\end{definition}
The function $\cdot$ is called the scalar multiplication and is omitted from writing sometimes.
\begin{remark}
    To show something is a module, we also need to check closure (that is $+,\cdot$ are well-defined).
\end{remark}
\begin{example}
    1. If $R$ is a field, then an $R$-module $M$ is a vector space over $R$.\\
    2. A $\mathbb Z$-module is precisely the same as an abelian group as the scalar multiplication can be uniquely defined by $n\cdot a=a+\cdots +a$ for $n$ many copies of $a$.\\
    3. Consider the ring $R=\mathbb F[X]$ for a field $\mathbb F$ and $V$ a vector space over $\mathbb F$.
    Consider $\alpha:V\to V$ an endomorphism.
    We can make $V$ an $R$-module over the scalar multiplication $\mathbb F[X]\times V\to V$ by $(f,v)\mapsto f(\alpha)(v)$.
    Note that different choice of $\alpha$ makes $V$ a different module.
    We sometimes write this as $V_\alpha$.
\end{example}
There are some general construction methods to produce a module.
\begin{example}
    1. For any ring $R$, $R^n$ is an $R$-module by $r\cdot(r_1,\ldots,r_n)=(rr_1,\ldots,rr_n)$ for $r,r_i\in R$.
    In particular, when $n=1$, $R$ itself is an $R$-module.\\
    2. If $I$ is an ideal, then $I$ is an $R$-module by $r\cdot i=ri$ for $r\in R,i\in I$.\\
    3. If $I$ is an ideal, then $R/I$ is an $R$-module by $r\cdot(s+I)=rs+I$ for $r,s\in R$.\\
    4. If $\phi:R\to S$ is a ring homomorphism, then any $S$-module $M$ is also an $M$-module by $r\cdot m=\phi(r)\cdot m$ for $r\in R,m\in M$.
    In particular, if $R\le S$, then any $S$-module can be viewed as an $R$-module.
\end{example}
\begin{definition}
    Let $M$ be an $R$-module, a subset $N\subset M$ is called a $R$-submodule of $M$, written as $N\le M$, if $(N,+)\le (M,+)$ and for any $r\in R,n\in N$, we have $r\cdot n\in N$.
\end{definition}
\begin{example}
    1. Any $R$-submodule of $R$ is an ideal.\\
    2. When $R$ is a field, then an $R$-module is a vector space, then a submodule is a vector subspace.
\end{example}
\begin{definition}
    If $N$ is a $R$-submodule of $M$, we can form the quotient $M/N$ by taking the quotient group under addition.
    We can make it as an $R$-module by specifying the scalar multiplication $r\cdot (m+N)=r\cdot m+N$.
\end{definition}
We can check easily that the scalar multiplication defined in this way is well-defined and makes $M/N$ an $R$-module.
\subsection{Homomorphisms}
\begin{definition}
    Let $M,N$ be $R$-modules, then a function $f:M\to N$ is a homomorphism of $R$-modules (or $R$-module map) if $f$ is a homomorphism of groups under addition and $\forall r\in R,m\in M,f(r\cdot m)=r\cdot f(m)$.\\
    A bijective homomorphism is called an isomorphism, and two $R$-modules $M,N$ are called isomorphic (written as $M\cong N$) if there is an isomorphism between them.
\end{definition}
\begin{example}
    When $R$ is a field, a homomorphism of $R$-modules is a linear map.
\end{example}
\begin{theorem}[(First) Isomorphism Theorem for Modules]
    Suppose $M,N$ are $R$-modules and $f:M\to N$ is a homomorphism of $R$-modules, then $\ker f\le M,f(M)\le N$ and $M/\ker f\cong f(M)$.
\end{theorem}
\begin{proof}
    Similar to before.
\end{proof}
\begin{theorem}[Second Isomorphism Theorem]
    Let $A,B$ be $R$-submodules of an $R$=module $M$, then $A+B=\{a+b:a\in A,b\in B\}\le M$ and $A\cap B\le M$.
    Moreover, $A/(A\cap B)\cong (A+B)/B$.
\end{theorem}
\begin{proof}
    Use the First Isomorphism Theorem.
\end{proof}
To motivate the Third Isomorphism Theorem, we note that for $R$-modules $N\le M$, we have the correspondance between the submodules of $M/N$ and the submodules of $M$ containing $N$.
\begin{theorem}[Third Isomorphism Theorem]
    Suppose $N\le L\le M$ are $R$-modules, then $M/L\cong (M/N)/(L/N)$.
\end{theorem}
\begin{proof}
    Same.
\end{proof}
In partricular, these are all true for vector spaces by taking the ring to be a field.
One can compare these results to familiar results in linear algebra (e.g. the First Isomorphism Theorem implies the Rank-Nullity Theorem).
\subsection{Finitely Generated Modules}
\begin{definition}
    Let $M$ be an $R$-module, and $m\in M$, then the submodule $Rm$ generated by $m$ is the smallest $R$-submodule of $M$ containing $m$, i.e. $Rm=\{r\cdot m:r\in R\}$.
\end{definition}
\begin{definition}
    Let $M$ be an $R$-module.
    $M$ is called cyclic if $M=Rm$ for some $m\in M$.
    $M$ is finitely generated if $\exists m_1,\ldots,m_n\in M$ such that $Rm_1+\cdots Rm_n=M$.
\end{definition}
\begin{lemma}
    An $R$-module $M$ is cyclic iff $M$ is isomorphic as an $R$-module to $R/I$ for some $I\unlhd R$.
\end{lemma}
\begin{proof}
    If $M$ is cyclic, write $M=Rm$, then there is a surjective $R$-module homomorphism $R\to M$ by $r\mapsto r\cdot m$ so the claim follows by the First Isomorphism Theorem.\\
    Conversely If $M\cong R/I$, then $M\cong R/I=R(1+I)$.
\end{proof}
\begin{lemma}
    An $R$-module $M$ is finitely generated iff there exists a surjective $R$-module homomorphism from $f:R^n\to M$ for some $n$.
\end{lemma}
\begin{proof}
    If $M$ is finitely generated, then $M=Rm_1+\cdots +Rm_n$ where $m_i\in M$, so we can take $f(r_1,\ldots,r_n)=r_1m_1+\cdots +r_nm_n$.\\
    Conversely, if such a map $f$ exists, then $M=Rf(e_1)+\cdots+Rf(e_n)$, then $e_i$ has $1$ in $i^{th}$ entry and $0$ in $j^{th}$ entry for any $j\neq i$.
\end{proof}
\begin{corollary}
    The quotient of a finitely generated $R$-module is a finitely generated $R$-module.
\end{corollary}
\begin{proof}
    Obvious from the preceding lemma.
\end{proof}
\begin{remark}
    A submodule of a finitely generated $R$-module needs not be finitely generated.
    For example, we can take a non-Noetherian ring $R$ itself as an $R$-module and consider a non-finitely generated ideal of it.
\end{remark}
\begin{lemma}
    Let $R$ be an integral domain, then every $R$-submodule of a cyclic $R$-module is cyclic iff $R$ is a PID.
\end{lemma}
\begin{proof}
    $R$ itself is a cyclic $R$-module, so if all $R$-submodules of it are cyclic, then all of its ideals are generated by one element, so $R$ is a PID.\\
    Conversely, if $R$ is a PID and $M$ is a cyclic $R$-module, so $M\cong R/I$ for $I\unlhd R$, so the $R$-submodules of $M$ are in the form $J/I$ for $I\subset J\unlhd R$.
    Now since $R$ is a PID, $J$ is principal, so $J/I$ is cyclic.
\end{proof}
\begin{theorem}
    Let $R$ be a PID, and $M$ an $R$-module.
    Suppose $M$ is generated by $n$ elements, then any $R$-submodule $N$ of $M$ can also be generated by at most $n$ elements.
\end{theorem}
\begin{proof}
    $n=1$ is the preceding lemma.
    For general $n$, we proceed by induction.
    Suppose $M=Rx_1+\cdots Rx_n$.
    Let $M_i=Rx_1+\cdots Rx_i$ and $0=M_0\le M_1\le\cdots\le M_n=M$.
    So we have
    $$0=M_0\cap N\le M_1\cap N\le\cdots\le M_n\cap N=N$$
    Then the $R$-module map $M_i\cap N\to M_i/M_{i-1}$ by $m\mapsto m+M_{i-1}$ has kernel $M_{i-1}\cap N$.
    Hence
    $$(M_i\cap N)/(M_{i-1}\cap N)\cong M'\le M_i/M_{i-1}$$
    But $M_i/M_{i-1}$ is cyclic by hypothesis, so by preceding lemma, $(M_i\cap N)/(M_{i-1}\cap N)$ is also cyclic and is generated by $y_i+M_{i-1}\cap N$ where $y_i\in M_i\cap N$.
    Therefore $M_i=M_{i-1}\cap N+Ry_i$.
    It follows that $M_i\cap N=Ry_1+\cdots +Ry_i$.
    In particular, $N=M_n\cap N=Ry_1+\cdots+Ry_n$, so $N$ is generated by $n$ elements.
\end{proof}
\begin{example}
    Take $R=\mathbb Z$, then we know that any subgroup of $\mathbb Z^n$ can be generated by $n$ elements.
\end{example}


\subsection{Curl}
\noindent
In 2D, we define the curl of a vector field $\vec{F}(x,y) = \langle P(x,y), Q(x,y) \rangle$ as
\begin{equation*}
	\nabla \times \vec{F} = Q_x - P_y
\end{equation*}
Note that the result is a scalar for this 2D case.\\
If $Q_x > 0$, then the field line accelerate upwards and to the left together: a counter-clockwise rotation.\\
If $P_y > 0$, then the field lines accelerate upwards and to the left together: a clockwise rotation.\\
So, $\nabla \times \vec{F}$ tells us the net counter-clockwise rotation at a point.\\

\noindent
We use the same del operator we introduced in divergence for curl, so $\nabla \times \vec{F} = \left< \frac{\partial}{\partial x}, \frac{\partial}{\partial y},\ldots \right> \times \vec{F}$. This means that for 3D and higher dimensions, the curl is a vector that is perpendicular to the plane of net rotation.\\

\noindent
For example, let's compute the curl of $\vec{F}(x,y) = \langle -y, x \rangle$.\\
\begin{equation*}
	\frac{\partial}{\partial x}(x) - \frac{\partial}{\partial y}(-y) = 1-(-1) = 2
\end{equation*}

\input{./vectorAnalysis/curlConservativeVFs}
\input{./vectorAnalysis/divergenceOfCurl}


\lab{Optimization with Scipy}{scipy.optimize}
\label{lab:Optimization1}
\objective{The Optimize package in Scipy provides highly optimized and versatile methods for solving fundamental optimization problems.
In this lab we introduce the syntax and variety of \li{scipy.optimize} as a foundation for unconstrained numerical optimization.}

Numerical optimization is one of the most common modern applications of mathematics.
Many mathematical problems can be rewritten in terms of optimization, and unless the problem is both simple and small, usually cannot be solved analytically, and must be approximated by numerical computation.

To assist with this, the \li{scipy.optimize} package has several functions for minimizing, root finding, and curve fitting, of which we will introduce the most essential.

You can learn about all of the functions at \url{http://docs.scipy.org/doc/scipy/reference/optimize.html}.

\section*{Local Minimization} % ===============================================

First we will test out a few of the minimization algorithms on the Rosenbrock function, which is defined as
\[
f(x,y) = (1-x)^2 + 100(y-x^2)^2.
\]
The Rosenbrock function is commonly used when evaluating the performance of an optimization algorithm.
Reasons for this include the fact that its minimizer \li{x = np.array([1., 1.])} is found in  curved valley, and so minimizing the function is non-trivial.
See Figure \ref{opt:rosenbrock}.
The Rosenbrock function is included in the optimize package (as \li{rosen}), as well as its gradient (\li{rosen_der}) and its hessian (\li{rosen_hess}).

\begin{figure}
\includegraphics[width=\textwidth]{Rosenbrock.pdf}
\caption{$f(x,y) = (1-x)^2 + 100(y-x^2)^2$}
\label{opt:rosenbrock}
\end{figure}

We will use the \li{minimize()} function and test some of its algorithms (specified by the keyword argument ``method" -- see the documentation page for \li{minimize()}).
For each algorithm, you need to pass in a callable function object for the Rosenbrock function, as well as a NumPy array giving the initial guess.
For some algorithms, you will additionally need to pass in the jacobian or hessian.
You may recognize some of these algorithms, and several of them will be discussed in greater detail later. For this lab, you do not need to understand how they work, just
how to use them.

% Give an example and explain how to read the output
As an example, we'll minimize the Rosenbrock with the Newton-CG method.
This method often performs better by including the optional hessian as an argument, which we will do here.
\begin{lstlisting}
>>> import numpy as np
>>> from scipy import optimize as opt
>>> x0 = np.array([4., -2.5])
>>> opt.minimize(opt.rosen, x0, method='Newton-CG', hess=opt.rosen_hess,
													jac=opt.rosen_der)
     fun: 1.1496545381999877e-15
     jac: array([  1.12295570e-05,  -5.63744647e-06])
 message: 'Optimization terminated successfully.'
    nfev: 45
    nhev: 34
     nit: 34
    njev: 78
  status: 0
 success: True
       x: array([ 0.99999997,  0.99999993])
\end{lstlisting}
As the online documentation indicates, \li{opt.minimize()} returns an object of type \li{opt.optimize.OptimizeResult}.

The printed output gives you information on the performance of the algorithm.
The most relevant output for this lab include

\li{fun: 1.1496545381999877e-15}, the obtained minimum;

\li{nit: 96}, the number of iterations the algorithm took to complete;

\li{success: True}, whether the algorithm converged or not;

\li{x: array([ 0.99999997,  0.99999993])}, the obtained minimizer.

Each of these outputs can be accessed either by indexing \li{OptimizeResult} object like a dictionary (\li{result['nit']}), or as attributes of a class (\li{result.nit}).
We recommend access by indexing, as this is consistent with other optimization packages in Python.

The online documenation for \li{scipy.optimize.minimize()} includes other optional parameters available to users, for example, to set a tolerance of convergence.
In some methods, the derivative may be optional, while it may be necessary in others.
While we do not cover all possible parameters in this lab, they should be explored as needed for specific applications.

\begin{problem} % Minimize the Rosenbrock function.
Use the \li{opt.minimize()} function to find the minimum of the Rosenbrock function.
Test Nelder-Mead, CG, and BFGS, starting each with the initial guess \li{x_0 = np.array([4., -2.5])}.
For each method, print whether it converged, and if so, print how many iterations it took.

Note that the hessian argument is not needed for these paticular methods.
\end{problem}

Each of these three algorithms will be explored in great detail later in Volume 2: Nelder-Mead is a variation of the Simplex algorithm, CG is a variant of the Conjugate Gradient algorithm, and BFGS is a quasi-Newton method developed by Broyden, Fletcher, Goldfarb, and Shanno.

The \li{minimize()} function can use various algorithms, each of which is best for certain problems.
Which algorithm one uses depends on the specific nature of one's problem.

It is also important to note that in many optimization applications, very little is known about the function to optimize.
These functions are often called \emph{blackbox functions}.
For example, one may be asked in the airline industry to analyze and optimize certain properties involving a segment of airplane wing.
Perhaps expert engineers have designed extremely robust and complicated software to model this wing segment given certain inputs, but this function is so complicated that nobody except the experts dares to try parcing it.
Briefly said, you simply don't want to understand it; nobody wants to understand it.

Fortunately, one can still optimize effectively in such situations by wisely selecting a correct algorithm.
However, because so little is known about the blackbox function, one must wisely select an appropriate minimization method and follow the specifications of the problem exactly.

\begin{problem} % Minimize a 'blackbox' function.
Minimize the \li{blackbox()} function in the \li{blackbox_function} module.
You don't need to know the source code or how it works in order to minimize it.
Simply select the appropriate method of \li{scipy.optimize.minimize()} for this problem, without passing your method a derivative.
You may need to test several methods and determine which is most appropriate.

The function \li{blackbox()} returns a certain measure of a piecewise-linear curve between two fixed points: the origin, and the point \li{(40,30)}.
This function accepts a one-dimensional \li{ndarray} of length \li{m} of y-values, where \li{m} is the number of points of the piecewise curve excluding endpoints.
These points are spaced evenly along the x-axis, so only the y-values of each point are passed into \li{blackbox()}.

Once you have selected a method, select an initial point with the following code:

\begin{lstlisting}
y_initial = 30*np.random.random_sample(18)
\end{lstlisting}

Then plot your initial curve and minimizing curve together on the same plot, including endpoints.
Note that this will require padding your array of internal y-values with the y-values of the endpoints, so that you plot a total of 20 points for each curve.
\end{problem}

\section*{Global Minimization via Basin Hopping} % ============================

In the realm of optimization, convex functions are the most well-behaved, as any local minimum is a global minimum.
However, in practice one must frequently deal with non-convex functions, and sometimes we need pick the global minimum out of many local minima.

For example, consider the function
%This is the crazy function that we came up with to stump the algorithms
\[
z = r^2 (1+ \sin^2(4r)),
\]
where
\[
r = \sqrt{(x+1)^2 + y^2}.
\]
Essentially, this is a wavy crater offset from the origin by 1 along the $x$ axis (see Figure \ref{opt:multimin}).
\begin{figure}
\includegraphics[width=\textwidth]{ManyMinima.pdf}
\caption{$z = r^2 (1+ 2\sin^2(4r))$}
\label{opt:multimin}
\end{figure}
The presence of many local minima proves to be a difficulty for the minimization algorithms.

For example, if we try using the Nelder-Mead method as previously, with an initial point of \li{x0 = np.array([-2, -2])}, the algorithm fails to find the global minimum, and instead comes to rest on a local minimum.
\begin{lstlisting}
>>> def multimin(x):
>>>     r = np.sqrt((x[0]+1)**2 + x[1]**2)
>>>     return r**2 *(1+ np.sin(4*r)**2)
>>>
>>> x0 = np.array([-2, -2])
>>> res = opt.minimize(multimin, x0, method='Nelder-Mead')
 final_simplex: (array([[-2.11758025, -2.04313668], [-2.11748198, -2.04319043],
       [-2.11751491, -2.04317242]]), array([ 5.48816866,
       												5.48816866,  5.48816866]))
           fun: 5.488168656962328
       message: 'Optimization terminated successfully.'
          nfev: 84
           nit: 44
        status: 0
       success: True
             x: array([-2.11758025, -2.04313668])
>>> print res['x']
[-2.11758025, -2.04313668]
>>> print res['fun']
5.488168656962328
>>> print multimin([-1,0])
0.0
\end{lstlisting}

However, SciPy does have some tools to help us with these problems.
Specifically, we can use the \li{opt.basinhopping()} function.

The \li{opt.basinhopping()} function uses the same minimizing algorithms (in fact, you can tell it whatever minimizing algorithm you can pass to \li{opt.minimize()}).
However, once it settles on a minimum, it hops randomly to a new point in the domain (depending on how we set the ``hopping" distance) that hopefully lies outside of the valley
or basin belonging to the current local minimum.
It then searches for the minimum from this new starting point, and if it finds a better minimizer, it repeats the hopping process from this new minimizer.
Thus, the \li{opt.basinhopping()} function has multiple chances to escape a local basin and find the correct global minimum.

\emph{Only Scipy Version 0.12+ has \li{opt.basinhopping}. In earlier versions, such as 0.11, you won't find it.}

\begin{problem} % Basinhopping
Explore the documentation for the \li{opt.basinhopping()} function online or via IPython, and use it to find the global minimum of our \li{multimin()} function with \li{x0 = np.array([-2, -2])}.
Call it using the same \li{Nelder-Mead} algorithm with \li{opt.basinhopping(multimin, x0, stepsize=0.5, minimizer_kwargs=\{'method':'nelder-mead'\})}.
Try it first with \li{stepsize=0.5} and then with \li{stepsize=0.2}.

Plot the multimin function using the following code:
\begin{lstlisting}
xdomain = np.linspace(-3.5,1.5,70)
ydomain = np.linspace(-2.5,2.5,60)
X,Y = np.meshgrid(xdomain,ydomain)
Z = multimin((X,Y))
fig = plt.figure()
ax1 = fig.add_subplot(111, projection='3d')
ax1.plot_wireframe(X, Y, Z, linewidth=.5, color='c')
\end{lstlisting}

Plot the initial point and minima by adapting the following line:
\begin{lstlisting}
ax1.scatter(x_value, y_value, z_value)
\end{lstlisting}

Why doesn't the alogrithm find the global minimum with \li{stepsize=0.2}?
Print your answer to this question, and return the true global minimum.
\end{problem}

\section*{Root Finding} % =====================================================

The \li{optimize} package also has functions useful in root-finding.
The next example, taken from the online documentation, solves the following nonlinear system of equations using \li{opt.root}.

\[
\begin{bmatrix}
	x_{0} + 1/2 ( x_{0} - x_{1} )^{3} - 1 \\
	1/2(x_{1}-x_{0})^{3} + x_{1}
\end{bmatrix} =
\begin{bmatrix}
	0 \\
	0
\end{bmatrix}.
\]

\begin{lstlisting}
>>> def func(x):
>>>     return [x[0] + 0.5 * (x[0] - x[1])**3 -1.0,
>>>             0.5 * ( x[1] - x[0])**3 + x[1]]
>>> def jac(x):
>>>     return np.array([[1 + 1.5 * (x[0] - x[1])**2,
>>>                     -1.5 * (x[0] - x[1])**2],
>>>                     [-1.5 * (x[1] - x[0])**2,
>>>                     1 + 1.5 * (x[1] - x[0])**2]])
>>> sol = opt.root(func, [0, 0], jac=jac, method='hybr')
>>> print sol.x
[ 0.8411639  0.1588361]
>>> print func(sol.x)
[-1.1102230246251565e-16, 0.0]
\end{lstlisting}

\begin{problem}
Find the roots of the system
\[
\begin{bmatrix}
	-x+y+z \\
	1+x^3-y^2+z^3\\
	-2-x^2+y^2+z^2
\end{bmatrix} =
\begin{bmatrix}
	0 \\
	0 \\
	0
\end{bmatrix} .
\]
Return the values of $x,y,z$ as an array.
\end{problem}

As with \li{opt.minimize()}, \li{opt.root()} has more than one algorithm for root finding.
Here we have used the \li{hybr} method.
There are also several algorithms for scalar root finding. See the online documentation for more.

\section*{Curve Fitting} % ====================================================

%we still have curve fitting
%least squares programming -- but it seems that it's already been done in Volume One

SciPy also has methods for curve fitting wrapped by the \li{opt.curve_fit()} function.
Just pass it data and a function to be fit.
The function should take in the independent variable as its first argument and values for the fitting parameters as subsequent arguments.
Examine the following example from the online documentation.
\begin{lstlisting}
>>> import numpy as np
>>> import scipy.optimize as opt

>>> #the function with which to create the data and later fit it
>>> def func(x,a,b,c):
>>>     return a*np.exp(-b*x) + c

>>> #create perturbed data
>>> x = np.linspace(0,4,50)
>>> y = func(x,2.5,1.3,0.5)
>>> yn = y + 0.2*np.random.normal(size=len(x));

>>> #perform the fit
>>> popt, pcov = opt.curve_fit(func,x,yn)
\end{lstlisting}
The variable \li{popt} now contains the fitted parameters and \li{pcov} gives the covariance of the fit.
See Figure \ref{opt:curve_fit} for a plot of the data and the fitted curve.

One of the most fundamental phenomena in the physical and engineering sciences is turbulent convection, wherein an unstable density gradient induces a fluid to move chaotically (basically, hot air rises).
This problem is so important that experiments and numerical simulations have been pushed to their limits in the past several decades to determine the qualitative nature of the fluid's motion under an extreme forcing (think of boiling a pot of water, but instead at temperatures akin to the interior of the sun).
The strength of the forcing (amount of the enforced temperature gradient) is measured by the non-dimensional Rayleigh number $R$.
Of paticular interest is to determine how well the chaotic turbulent flow transports the heat from the hot bottom to the cold top, as measured by the Nusselt number $\nu$.
One of the primary goals of experiments, simulations, and analysis is to determine how the Nusselt number $\nu$ depends on the Rayleigh number $R$, i.e. if the bottom of the pot of water is heated more strongly, how much faster does the boiling water transport heat to the top?

It is often generically believed that the Nusselt number obeys a power law of the form $\nu = cR^\beta$, where $\beta \le 1/2$.
Through some mild assumptions on the temperature, we can construct an eigenvalue problem that we solve numerically for a variety of Rayleigh numbers, thus obtaining an upper bound on the Nusselt number as $\nu \le cR^\beta$.  With our physical specifications of the problem, we may predict $\beta < 1/2$.

\begin{figure}
\includegraphics[width=\textwidth]{curve_fit.pdf}
\caption{Example of perturbed data graphed with the resulting curve using the fitted parameters: $a=2.72$,  $b=1.31$, and $c=0.45$.}
\label{opt:curve_fit}
\end{figure}

\begin{problem} % Convection curve fitting.
Use \li{opt.curve_fit()} to fit a curve to data obtained from numerical simulations of convection.
The data are in the file \li{convection.npy}.
The first column is $R$, the Rayleigh number, and the second column is $\nu$, the Nusselt number.
With the convection equation
\[
\nu = cR^\beta,
\]
use \li{opt.curve_fit()} to find a fit to the data using $c$ and $\beta$ as the fitting parameters.
See Figure \ref{opt:ConvectionFit} for a plot of the data along with a fitted curve.
Though it may be difficult to see in the figure, the first four points skew the data, and do not help us determine the appropriate long-term values of $c$ and $\beta$.
Thus, do not use the first four points when fitting a curve to the data, but include them in the plot.

Just so that you know that you are getting realistic values, $c$ should be around $.1$, and $\beta$ should be less than $1/2$.
Return your values for $c$ and $\beta$ in a NumPy array of length 2.
\end{problem}

\begin{figure}
\includegraphics[width=\textwidth]{ConvectionFit.pdf}
\caption{The black points are the data from \li{convection.npy} plotted as a scatter plot. The blue line is a fitted curve.}
\label{opt:ConvectionFit}
\end{figure}

The \li{scipy.optimize} package has many other useful functions, and is a good first resource when confronting a numerical optimization problem.
See the online documentation for further details.


\section{Universal coefficient theorem (and $\Hom$, adjointness)}
On Wednesday, we'll talk about the K\"{u}nneth theorem, and later we'll talk about the K\"{u}nneth theorem. We've been talking about tensor products of $R$-modules, but we can do something that's more natural in a way. That's the notion of $\Hom_R(M,N)$, which is the collection of $R$-linear homomorphisms between $R$-modules $M$ and $N$. This is actually itself an $R$-module. It's an abelian group, first of all, because you can add morphisms. How does $r\in R$ act on $f\in \Hom_R(M,N)$? Just define $(rf)(x)=f\cdot f(x)$. You should check that this does actually define an $R$-module homomorphism. (This is trivial.) If $R$ isn't commutative I guess there'd be an action of $Z(R)$ on $\Hom_R(M,N)$. In particular, $\Hom_R(M,-):\mathbf{Mod}_R\to\mathbf{Mod}_R$.
\begin{remark}
You're supposed to technically write $\underline{\Hom}_R(M,N)$ to mean $\Hom_R(M,N)$ with the structure of an $R$-module. But in these notes, I will not do this.
\end{remark}
I wanted to bring this up because it relates to tensor products in a beautiful way. Consider $\Hom_R(M\otimes_R N,L)$. This is the collection of $R$-bilinear maps $M\times N\to L$. I claim that $\Hom_R(M\otimes_R N,L)\cong\Hom_R(M,\Hom_R(N,L))$. The way this works is the following. Suppose $f:M\otimes_R N\to L$. Define $\widehat{f}:M\to\Hom_R(N,L)$ via $\widehat{f}:m\mapsto(n\mapsto f(m\otimes_R n))$. This is a special case of the notion of an adjoint functor, introduced by Dan Kan, who was actually here at MIT. This is a big part of category theory.
\begin{prop}
Let $\cI$ be a direct set, and let $M:\cI\to\mathbf{Mod}_R$ be a $\cI$-directed system of $R$-modules. There is a natural isomorphism $(\varinjlim_I M_i)\otimes_R N\cong \varinjlim_I (M_i\otimes_R N)$. It's very technical but it might be very useful. Maybe in the homework for example \emph{;-)}
\end{prop}
\begin{proof}
Consider $\Hom_R((\varinjlim_I M_i)\otimes_R N,L)\cong\Hom_R(\varinjlim_I M_i,\Hom_R(N,L))$. That's cool, because this is the same thing as $\Map_{\Fun(\cI,\mathbf{Mod}_R)}(\{M_i\},c_{\Hom_R(N,L)})$ because $\{M_i\}$ is a $\cI$-directed system. Now, this is the same as $\Map_{\Fun(\cI,\mathbf{Mod}_R)}(\{M_i\otimes_R N\},c_{L})$ by the Hom-tensor adjunction. We can unspool this to see that this is $\Hom_R(\varinjlim_\cI (M_i\otimes_R N),L)$. Now conclude via the Yoneda lemma (which we haven't discussed yet, but I think is a homework problem). Basically this states that $\Map_\cc(X,-)$ determines $X$.
\end{proof}
We'll talk a lot more about $\Hom$ and adjunctions, but not today.

Here's the question I want to talk about today. Suppose that I'm given $ H_\ast(X;\Z)$. Does it determine $ H_\ast(X;\Z/2\Z)$? Consider $\RP^2\to S^2$. In homology with coefficients in $\Z$, in dimension $2$, this map must induce $0$. But in $\Z/2\Z$-coefficients, in dimension $2$, this map gives an isomorphism. I could have considered reduced homology. This shows that there's not a functorial relationship between $ H_\ast(X;\Z)$ and $ H_\ast(X;\Z/2\Z)$. So how \emph{do} we go between different coefficients? That's the mystery.

Let $R$ be a commutative ring. Let $M$ be a $R$-module. I want to think about some chain complex $C_\bullet$ of $R$-modules. It could be the singular complex of a space, but it doesn't have to be. I'm going to forget to write $\otimes_R$ now; I'll just write $\otimes$. I can consider $ H_n(C_\bullet)\otimes M$, or $ H_n(C_\bullet\otimes M)$. The latter thing gives homology with coefficients in $M$. How can we compare these two? I claim that I can construct a map $\alpha: H_n(C_\bullet)\otimes M\to H_n(C_\bullet\otimes M)$. Recall the exact sequence $0\to B_n\to Z_n(C_\bullet)\to H_n\to 0$ that defines homology. So I get an exact sequence $B_n\otimes M\to Z_n(C_\bullet)\otimes M\to H_n(C_\bullet)\otimes M\to 0$. And there's a surjection $Z_n(C_\bullet)(C\otimes M)\to H_n(C_\bullet\otimes M)$. I have to tell you where $x\otimes m\in Z_n(C_\bullet)\otimes M$ goes. I'll send it to $x\otimes m\in Z_n(C_\bullet)(C_\bullet\otimes M)$. I claim that this is a cycle, because $d(x\otimes m)=(dx)\otimes m$. But $x\in\ker d$, so this is zero, and thus $x\otimes m\in Z_n(C_\bullet)(C_\bullet\otimes M)$. Does it descend to a map in homology? We want to check that $B_n\otimes M\to Z_n(C_\bullet)(C_\bullet\otimes M)$ is zero. Suppose $y\in C_{n+1}$. Then $dy\in C_n$. Where does $dy\otimes x$ go? Send it to $d(y\otimes x)\in B_n(C_\bullet\otimes M)$. And this maps to zero.

The problem is that $\alpha$ is not always an isomorphism. But it is if $M$ is free, say $M=R\langle S\rangle$. That's because then $C_\bullet\otimes M\cong\bigoplus_S C_\bullet$. Now there's a little lemma that nobody tells you about, because it's obvious, but here it is anyway:
\begin{lemma}
Suppose I have a collection of exact sequences of $R$-modules $A_i\to B_i\to C_i$. Then $\bigoplus A_i\to \bigoplus B_i\to \bigoplus C_i$ is short exact, i.e., $\bigoplus$ is an exact functor.
\end{lemma}
\begin{proof}
The composition is obviously zero. If $(b_i)\in\bigoplus B_i$ maps to $0$, then by exactness, there are $a_i$ that map to $b_i$, and we assume that if some $b_i=0$, then $a_i=0$.
\end{proof}
This in particular implies that $ H(\bigoplus C_i)\cong\bigoplus H(C_i)$.

Consider a free resolution of $M$. Assume $R$ is a PID, so that $M$ has a free resolution of the form $0\to F_1\to F_0\to M\to 0$. Thus we get a chain complex $C_\bullet\otimes F_1\to C_\bullet\otimes F_0\to C_\bullet M\to 0$. Now I get a lexseq in $\Tor$, namely $\cdots\to\Tor^R_1(C_\bullet,M)\to C_\bullet\otimes F_1\to C_\bullet\otimes F_0\to C_\bullet M\to 0$.

Now I'm going to make a second assumption. Suppose $C_n$ is a free $R$-module for all $n$. At least that $\Tor^R_1(C_n,M)=0$. This shouldn't bother you at all, because the chain complexes that we need satisfy this condition. In particular, we have a sexseq $0\to C_\bullet\otimes F_1\to C_\bullet\otimes F_0\to C_\bullet M\to 0$. What happens then? We get a lexseq. I want to give an unspliced form of this (huge diagram coming up!).
\begin{equation*}
\xymatrix{0\ar[d]\ar@{=}[r] & 0\ar[d]\\
\coker( H_n(C_\bullet\otimes F_1)\to H_n(C_\bullet\otimes F_0))\ar[d]\ar@{=}[r] & H_n(C_\bullet)\otimes M\ar[d]^\alpha\\
 H_n(C_\bullet\otimes M)\ar[d]^\partial\ar@{=}[r] & H_n(C_\bullet\otimes M)\ar[d]^\partial\\
\ker( H_{n-1}(C_\bullet\otimes F_1)\to H_{n-1}(C_\bullet\otimes F_0))\ar@{=}[r]\ar[d] & \Tor^R_{1}( H_{n-1}(C_\bullet),M)\ar[d]\\
0\ar@{=}[r] & 0}
\end{equation*}
Because $\ker( H_{n-1}(C_\bullet\otimes F_1)\to H_{n-1}(C_\bullet\otimes F_0))=\ker( H_{n-1}(C_\bullet)\otimes F_1\to H_{n-1}(C_\bullet)\otimes F_0)$ and $\coker( H_n(C_\bullet)\otimes F_1\to H_n(C_\bullet)\otimes F_0)$. Also, $\ker( H_{n-1}(C_\bullet\otimes F_1)\to H_{n-1}(C_\bullet\otimes F_0))=\Tor^R_{1}( H_{n-1}(C_\bullet),M)$ because of the lexseq $0\to \Tor^R_1( H_{n-1}(C_\bullet),M)\to H_{n-1}(C_\bullet)\otimes F_1\to H_{n-1}(C_\bullet)\otimes F_0\to H_{n-1}(C_\bullet)\otimes M\to 0$.

Thus we have a sexseq, which gives the universal coefficient theorem:
\begin{theorem}[Universal Coefficient Theorem]
If $R$ is a PID and $C_n$ is free for all $n$, then there is a natural sexseq of $R$-modules:
\begin{equation*}
0\to H_n(C_\bullet)\otimes M\xrightarrow{\alpha} H_n(C_\bullet\otimes M)\xrightarrow{\partial}\Tor^R_1( H_{n-1}(C_\bullet),M)\to 0
\end{equation*}
A further fact that we won't prove is that this splits as a sexseq of $R$-modules, but not naturally.
\end{theorem}
\begin{example}
Consider $ H_2(\RP^2;\Z/2\Z)\cong\Z/2\Z$, and we can consider $ H_2(\RP^2;\Z)\otimes\Z/2\Z=0$. So by the UCT, this must come from $\Tor^\Z_1( H_1(\RP^2;\Z),\Z/2\Z)\cong\Z/2\Z$. So $\partial$ is an isomorphism. This explains the mystery that we began with.
\end{example}
\begin{remark}
Suppose $R$ is not a PID. For example, consider what we worked with before, e.g., $R=k[e]/(e^2)$, and let $M=k$ (so $e$ acts as $0$). Consider the chain complex $C_\bullet:\cdots\to R\xrightarrow{e}R\xrightarrow{e} R\xrightarrow{e}R\to 0$. This is actually the free resolution of $k$ that we found before. In particular, $ H_n(C_\bullet)=\begin{cases}k & n=0 \\ 0 & n\neq 0\end{cases}$. What is $C_\bullet\otimes_R k$? It's exactly $\cdots\to k\xrightarrow{0}k\xrightarrow{0} k\xrightarrow{0}k\to 0$. So $ H_n(C_\bullet\otimes_R k)=\begin{cases}k & n>0 \\ 0 & n<0\end{cases}=\Tor^R_n(k,k)$. The two homologies are super different. The excess $\Tor$'s are accounted for via spectral sequences, which you'll see when you take 18.906.
\end{remark}
The next step is to consider the homology of products.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Independence of events and independence of random variables %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Independence of Events and Random Variables}

\begin{theorem}{6.3}{Monotone Class Theorem}

    Let $\Pi$ be a $\pi$-system contained in a $\lambda$-system $\Lambda$. Then $\sigma(\Pi)$ is contained in $\Lambda$.

\end{theorem}

\begin{proposition}{6.4}{Extending $\pi$-System Independence}

    Let $C_1$ and $C_2$ be two \emph{independent} $\pi$-systems, i.e.

        \begin{align*}
            P(A \cap B) = P(A)P(B) \quad \forall A \in C_1, B \in C_2,
        \end{align*}

    then the \SigmaAlgebra s $\sigma(C_1)$ and $\sigma(C_2)$ are also independent.

\end{proposition}

\begin{theorem}{6.7}{Fubini-Tonelli Theorem}

    Let $(\Omega_i, \CalF_i, \mu_i)$, for $i = 1,2$, be measure spaces and $(\Omega, \CalF, \mu)$ be the product measure space of the two, i.e. $\Omega = \Omega_1 \times \Omega_2$, $\CalF = \CalF_i \otimes \CalF_2$ and $\mu = \mu_1 \otimes \mu_2$. Let $f: \Omega \to \overline{\mathbb{R}}$ be a \emph{non-negative} $\CalF$-measurable function. If $\mu_i$, for $i = 1,2$, are \emph{finite measures} on $\Omega_i$, for $i = 1,2$, respectively, then the following iterated integrals are well-defined and:

        \begin{align*}
            \int_{\Omega_1 \times \Omega_2} f \, d\mu_1 \otimes \mu_2 &= \int_{\Omega_1} \int_{\Omega_2} f \, d\mu_2 d\mu_1 = \\ &= \int_{\Omega_2} \int_{\Omega_1} f \, d\mu_1 d\mu_2.
        \end{align*}

    Furthermore, this statement holds for $\CalF$-measurable functions if:

        \begin{align*}
            \int_{\Omega_1 \times \Omega_2} |f| \, d\mu_1 \otimes \mu_2 < \infty.
        \end{align*}

\end{theorem}

\begin{lemma}{6.9}{Borel-Cantelli (Full)}

    Let $(A_n)_{n=1}^{\infty}$ be a sequence of sets and set

        \begin{align*}
            A \coloneqq \limsup_{n \to \infty} A_n \coloneqq \bigcap_{n=1}^{\infty}\bigcup_{k=n}^{\infty} A_k,
        \end{align*}

    then the following statements holds:

        \begin{enumerate}[(i)]
            \setlength{\parskip}{0em}
            \item If $\sum_{n=1}^{\infty} \mu(A_n) < \infty$, then $\mu(A) = 0$.
            \item If all $A_n$ are \emph{jointly independent} and $\sum_{n=1}^{\infty} P(A_n) = \infty$, then $P(A) = 1$.
        \end{enumerate}

    \Hint (i) provided in general case. (ii) Prove $P((\limsup_{n \to \infty} A_n)^C) = 1$, define $B_n = \bigcap_{k=n}^{\infty} A_k^C$ and show that for a given $P(B_n) = P(\lim_{m \to \infty} \bigcap_{k=n}^{m} A_k) = 0$ using independence and observation that $1 - P(A) \leq e^{-P(A)}$. Finally, use \emph{sub}-$\sigma$-additivity for $P(\bigcup_{n=1}^{\infty} B_n)$. \emph{Do not} attempt to argue through increasing sequences.

\end{lemma}

\begin{exercise}{}{Pulling Sum Through Variance}

    Let $(X_i)_{i=1}^{\infty}$ be a sequence of \emph{pairwise independent} random variables. Assume that $EX_i^2 < \infty$ for $i=1,2,\hdots,n$, then

        \begin{align*}
            \mathrm{Var}\left(\sum_{i=1}^n X_i\right) = \sum_{i=1}^n \mathrm{Var}\left(X_i\right).
        \end{align*}

\end{exercise}


\section{Double Integrals}
\noindent
Similar to how the limit of a Riemann Sum, the sum of the areas of small rectangles, is the area underneath a curve, we can find the volume underneath a surface by summing the volumes of small rectangular prisms.\\

[INSERT IMAGE]

\noindent
In 2D (single variabe): $\Delta x = \frac{b-a}{n}$, $\int_{a}^{b}{f(x)\mathrm{d}x} = \lim_{n \to \infty}{\sum_{i = 0}^{n-1}{f(a+i\Delta x)\Delta a}}$.\\
In 3D: $\Delta x = \frac{b-a}{n}$, $\Delta y = \frac{d-c}{n}$,
\begin{equation*}
	\int_{c}^{d}{\int_{a}^{b}{f(x,y)\mathrm{d}x}\mathrm{d}y} = \lim_{m \to \infty}{\sum_{j = 0}^{m-1}{\left(\lim_{n \to \infty}{\sum_{i = 0}^{n-1}{f(a+i\Delta a, c+j\Delta y)\Delta x}}\right)\Delta y}}
\end{equation*}
\input{./multipleIntegrals/domainRegionsFubinisTheorem}
\input{./multipleIntegrals/averageValues}
\input{./multipleIntegrals/volumeBetweenSurfaces}
\input{./multipleIntegrals/planeLaminas}


\section{Geometry}
  \code{geom/compgeom.cpp}
	\subsection{Dots and Cross Products}
    \code{geom/dot-cross.cpp}
  \subsection{Angles and Rotations}
    \code{geom/angles-rots.cpp}
	\subsection{Spherical Coordinates}
    \[
        \begin{array}{cc}
            x = r \cos \theta \cos \phi & r = \sqrt{x^2 + y^2 + z^2} \\
            y = r \cos \theta \sin \phi & \theta = \cos^{-1} x/r \\
            z = r \sin \theta & \phi = \mathrm{atan2}(y,x)
        \end{array}
    \]
	\subsection{Point Projection}
    \code{geom/pt-proj.cpp}
	\subsection{Great Circle Distance}
    \code{geom/great-circle.cpp}
	\subsection{Point/Line/Plane Distances}
    \code{geom/dists.cpp}
	\subsection{Intersections}
    \subsubsection{Line-Segment Intersection}
      Get intersection points of 2D lines/segments $\overline{ab}$ and $\overline{cd}$.
      \code{geom/line-seg-isect.cpp}
    \subsubsection{Circle-Line Intersection}
      Get intersection points of circle at center $c$, radius $r$, and line $\overline{ab}$.
      \code{geom/circ-line-isect.cpp}
    \subsubsection{Circle-Circle Intersection}
      \code{geom/circ-circ-isect.cpp}
	\subsection{Areas}
    \subsubsection{Polygon Area}
      Find the area of any 2D polygon given as points in $O(n)$.
      \code{geom/poly-area.cpp}
    \subsubsection{Triangle Area}
      Find the area of a triangle using only their lengths. Lengths must be valid.
      \code{geom/tri-area.cpp}
    \subsubsection{Cyclic Quadrilateral Area}
      Find the area of a cyclic quadrilateral using only their lengths. A quadrilateral is
      cyclic if its inner angles sum up to $360^\circ$.
      \code{geom/cyc-quad-area.cpp}
  \subsection{Polygon Centroid}
    Get the centroid/center of mass of a polygon in $O(m)$.
    \code{geom/poly-centroid.cpp}
  \subsection{Convex Hull}
    \subsubsection{2D Convex Hull}
      Get the convex hull of a set of points using Graham-Andrew's scan. This sorts the
      points at $O(n \log n)$, then performs the Monotonic Chain Algorithm at $O(n)$.
      \code{geom/convex-hull.cpp}
    \subsubsection{3D Convex Hull}
      Currently $O(N^2)$, but can be optimized to a randomized $O(N\log{N})$ using the Clarkson-Shor algorithm.
      Sauce: \href{https://codeforces.com/blog/entry/81768}{Efficient 3D Convex Hull Tutorial on CF}.
      \code{geom/convex-hull-3d.cpp}
  \subsectionRed{Delaunay Triangulation}
    Simply map each point $(x,y)$ to $(x,y,x^2+y^2)$, find the 3d convex hull, and drop the 3rd dimension.
  \subsection{Point in Polygon}
    Check if a point is strictly inside (or on the border) of a polygon in $O(n)$.
    \code{geom/pt-in-poly.cpp}
  \subsection{Cut Polygon by a Line}
    Cut polygon by line $\overline{ab}$ to its left in $O(n)$, such that $\angle abp$ is counter-clockwise.
    \code{geom/cut-poly.cpp}
  \subsection{Triangle Centers}
    \code{geom/tri-centers.cpp}
  \subsection{Convex Polygon Intersection}
    Get the intersection of two convex polygons in $O(n^2)$.
    \code{geom/convex-poly-isect.cpp}
  \subsection{Pick's Theorem for Lattice Points}
    Count points with integer coordinates inside and on the boundary of a polygon in
    $O(n)$ using Pick's theorem: $\text{Area} = I + B/2 - 1$.
    \code{geom/picks.cpp}
  \subsection{Minimum Enclosing Circle}
    Get the minimum bounding ball that encloses a set of points (2D or 3D) in $\Theta{n}$.
    \code{geom/min-enclosing-circ.cpp}
  \subsection{Shamos Algorithm}
    Solve for the polygon diameter in $O(n \log n)$.
    \code{geom/shamos.cpp}
  \subsection{$k$D Tree}
    Get the $k$-nearest neighbors of a point within pruned radius in $O(k \log k \log n)$.
    \code{geom/kd-tree.cpp}
  \subsection{Line Sweep (Closest Pair)}
    Get the closest pair distance of a set of points in $O(n \log n)$ by sweeping a line and
    keeping a bounded rectangle. Modifiable for other metrics such as Minkowski and
    Manhattan distance. For external point queries, see $k$D Tree.
    \code{geom/closest-pair.cpp}
  \subsection{Line upper/lower envelope}
    To find the upper/lower envelope of a collection of lines $a_i+b_i x$,
    plot the points $(b_i,a_i)$, add the point $(0,\pm \infty)$ (depending
    on if upper/lower envelope is desired), and then find the convex hull.
  \subsection{Formulas}
    Let $a = (a_x, a_y)$ and $b = (b_x, b_y)$ be two-dimensional vectors.
    \begin{itemize}
      \item $a\cdot b = |a||b|\cos{\theta}$, where $\theta$ is the angle
        between $a$ and $b$.
      \item $a\times b = |a||b|\sin{\theta}$, where $\theta$ is the
        signed angle between $a$ and $b$.
      \item $a\times b$ is equal to the area of the parallelogram with
        two of its sides formed by $a$ and $b$. Half of that is the
        area of the triangle formed by $a$ and $b$.
      \item The line going through $a$ and $b$ is $Ax+By=C$ where $A=b_y-a_y$, $B=a_x-b_x$, $C=Aa_x+Ba_y$.
      \item Two lines $A_1x+B_1y=C_1$, $A_2x+B_2y=C_2$ are parallel iff.\ $D=A_1B_2-A_2B_1$ is zero. Otherwise their unique intersection is $(B_2C_1-B_1C_2,A_1C_2-A_2C_1)/D$.
      \item \textbf{Euler's formula:} $V - E + F = 2$
      \item Side lengths $a,b,c$ can form a triangle iff.\ $a+b>c$, $b+c>a$ and $a+c>b$.
      \item Sum of internal angles of a regular convex $n$-gon is $(n-2)\pi$.
      \item \textbf{Law of sines:} $\frac{a}{\sin A} = \frac{b}{\sin B} = \frac{c}{\sin C}$
      \item \textbf{Law of cosines:} $b^2 = a^2 + c^2 - 2ac\cos B$
      \item Internal tangents of circles $(c_1,r_1), (c_2,r_2)$ intersect at $(c_1r_2+c_2r_1)/(r_1+r_2)$, external intersect at $(c_1r_2-c_2r_1)/(r_1+r_2)$.
    \end{itemize}


\section{Model}
\label{sec:model}

The model computes the sequence of tags $t_1, ..., t_n$ (also denoted as $t_1^n$) with the highest probability given the observed sequence of words
$w_1, ..., w_n$:
\begin{equation}
    t_1, ..., t_n = \argmax_{t_1, ..., t_n} P(t_1^n | w_1^n).
    \label{eq:model_original}
\end{equation}

Applying the Bayes' Law, we obtain:
\begin{equation*}
    t_1, ..., t_n = \argmax_{t_1, ..., t_n} \frac{P(w_1^n | t_1^n) P(t_1^n)}{P(w_1^n)};
\end{equation*}

since the denominator does not depend on $t_1, ..., t_n$, we can drop it:
\begin{equation}
    t_1, ..., t_n = \argmax_{t_1, ..., t_n} P(w_1^n | t_1^n) P(t_1^n).
    \label{eq:model_compact}
\end{equation}

We assume that the probability of a word only depends on its own tag, and not on the tags of the other words in sentence.
Thus, we have that:
\begin{equation}
    P(w_1^n | t_1^n) \approx \prod_{i=1}^n P(w_i | t_i).
    \label{eq:model_first_term}
\end{equation}

Each $P(w_i | t_i)$ can be easily computed from the training data using maximum likelihood as:
\begin{equation*}
    P(w_i | t_i) = \frac{C(w_i, t_i)}{C(t_i)},
\end{equation*}
where $C(t_i, w_i)$ denotes the number of times that word $w_i$ has tag $t_i$
and $C(t_i)$ denotes the frequency of tag $t_i$.

The term $P(t_1^n)$ represent the probability of a tag given all its predecessor in the sentence.
To compute the term, we introduce a Markov assumption: the probability of a tag in the sentence
depends only on the $m$ previous tags:
\begin{equation}
    P(t_1, ..., t_n) = \prod_{i=m}^n P(t_i | t_{i-m}, ... t_{i-1}),
    \label{eq:model_second_term}
\end{equation}
where $t_0$ denotes the beginning of the sentence.
A Markov assumption of order $m$ corresponds to a $m+1$ n-gram language model.

Each term $P(t_i | t_{i-1})$ can be computed using maximum likelihood as follows:
\begin{equation*}
    P(t_i | t_{i-m}, ..., t_{i-1}) = \frac{C(t_{i-m}, ..., t_{i})}{C(t_{i-m}, ..., t_{i-1})}.
\end{equation*}

Using \cref{eq:model_compact}, (\ref{eq:model_first_term}) and (\ref{eq:model_second_term}), we can rewrite the model in \cref{eq:model_original} as:
\begin{equation*}
    t_1^n = \argmax_{t_1, ..., t_n} \prod_{i=1}^n P(w_i | t_i) \prod_{i=m}^n P(t_i | t_{i-m}^{i-1}).
\end{equation*}


\section{Arc Length and Curvature}\label{sec:arc length 3D}

Sometimes it is useful to compute the length of a curve in space; for
example, if the curve represents the path of a moving object, the
length of the curve between two points may be the distance traveled by
the object between two times.

Recall that if the curve is given by the vector function $\bf r$ then
the vector $\Delta {\bf r}=
{\bf r}(t+\Delta t)-{\bf r}(t)$ points from one position
on the curve to another, as depicted in Figure~\ref{fig:vector derivative}.
If the points are close together, the length of
$\Delta {\bf r}$ is close to the length of the
curve between the two points. If we add up the lengths of many such
tiny vectors, placed head to tail along a segment of the curve, we get
an approximation to the length of the curve over that segment. In the
limit, as usual, this sum turns into an integral that computes precisely
the length of the curve.
First, note that
$$|\Delta {\bf r}|={|\Delta {\bf r}|\over \Delta t}\,\Delta t\approx
|{\bf r}'(t)|\,\Delta t,$$
when $\Delta t$ is small.
Then the length of the curve between
${\bf r}(a)$ and ${\bf r}(b)$ is
$$\lim_{n\to\infty} \sum_{i=0}^{n-1}|\Delta {\bf r}|
=\lim_{n\to\infty} \sum_{i=0}^{n-1} {|\Delta {\bf r}|\over \Delta t}\,\Delta t
=\lim_{n\to\infty} \sum_{i=0}^{n-1} |{\bf r}'(t)|\,\Delta t=
\int_a^b|{\bf r}'(t)|\,dt.$$
(Well, sometimes. This works if between $a$ and $b$ the segment of curve
is traced out exactly once.)

\begin{example}{}{}
Find the length of one turn of the helix
${\bf r}=\langle \cos t, \sin t, t\rangle$ (see Figure~\ref{fig:helixes}).
\end{example}
\begin{solution}
We compute ${\bf r}'=\langle -\sin t, \cos t, 1\rangle$ and
$|{\bf r}'|=\sqrt{\sin^2 t+\cos^2 t+1}=\sqrt2$, so the length is
$$\int_0^{2\pi} \sqrt2\,dt = 2\sqrt2\pi.$$
\end{solution}

\begin{example}{Length of a Curve}{}
Suppose $y=\ln x$; what is the length of this curve between
$x=1$ and $x=\sqrt3$?
\end{example}
\begin{solution}
Although this problem does not appear to involve vectors or three
dimensions, we can interpret it in those terms: let
${\bf r}(t)=\langle t,\ln t,0\rangle$. This vector function traces
out precisely $y=\ln x$ in the $x$-$y$ plane. Then
${\bf r}'(t)=\langle 1,1/t,0\rangle$ and
$\ds |{\bf r}'(t)|=\sqrt{1+1/t^2}$ and the desired length is
$$\int_1^{\sqrt3} \sqrt{1+{1\over
    t^2}}\,dt=2-\sqrt2+\ln(\sqrt2+1)-{1\over2}\ln3.$$
(This integral is a bit tricky, but requires only methods we have
learned.)
\end{solution}

Notice that there is nothing special about $y=\ln x$, except that the
resulting integral can be computed. In general, given any $y=f(x)$, we
can think of this as the vector function
${\bf r}(t)=\langle t,f(t),0\rangle$. Then
${\bf r}'(t)=\langle 1,f'(t),0\rangle$ and
$\ds |{\bf r}'(t)|=\sqrt{1+(f')^2}$. The length of the curve $y=f(x)$
between $a$ and $b$ is thus
$$\int_a^b \sqrt{1+(f'(x))^2}\,dx.$$
Unfortunately, such integrals are often impossible to do exactly and
must be approximated.

One useful application of arc length is the \dfont{arc length parameterization}.
A vector function ${\bf r}(t)$ gives the position of a point in terms of the
parameter $t$, which is often time, but need not be. Suppose $s$ is
the distance along the curve from some fixed starting point; if we use
$s$ for the variable, we get ${\bf r}(s)$, the position in space in
terms of distance along the curve. We might still imagine that the
curve represents the position of a moving object; now we get the
position of the object as a function of how far the object has
traveled.

\begin{example}{}{}
Suppose ${\bf r}(t)=\langle \cos t,\sin t,0\rangle$. We know
that this curve is a circle of radius 1. While $t$ might represent
time, it can also in this case represent the usual angle between the
positive $x$-axis and ${\bf r}(t)$. The distance along the circle from
$(1,0,0)$ to $(\cos t,\sin t,0)$ is also $t$---this is the
definition of radian measure. Thus, in this case $s=t$ and
${\bf r}(s)=\langle \cos s,\sin s,0\rangle$.
\end{example}

\begin{example}{}{}
Suppose ${\bf r}(t)=\langle \cos t,\sin t,t\rangle$. We know
that this curve is a helix. The distance along the helix from $(1,0,0)$
to $(\cos t,\sin t,t)$ is
$$s=\int_0^t |{\bf r}'(u)|\,du=\int_0^t \sqrt{\cos^2u+\sin^2u+1}\,du=
\int_0^t \sqrt{2}\,du=\sqrt2t.$$
Thus, the value of $t$ that gets us distance $s$ along the helix is
$t=s/\sqrt2$, and so the same curve is given by $\hat{\bf r}(s)=
\langle \cos(s/\sqrt2),\sin(s/\sqrt2),s/\sqrt2\rangle$.
\end{example}

In general, if we have a vector function ${\bf r}(t)$, to convert it
to a vector function in terms of arc length we compute
$$s=\int_a^t |{\bf r}'(u)|\,du=f(t),$$
solve $s=f(t)$ for $t$, getting $t=g(s)$, and substitute this back
into ${\bf r}(t)$ to get $\hat{\bf r}(s)={\bf r}(g(s))$.

Suppose that $t$ is time.
By the Fundamental Theorem of Calculus, if we start with
arc length
$$s(t)=\int_a^t |{\bf r}'(u)|\,du$$
and take the derivative, we get
$$s'(t)=|{\bf r}'(t)|.$$
Here $s'(t)$ is the rate at which the arc length is changing, and
we have seen that $|{\bf r}'(t)|$ is the speed of a moving object;
these are of course the same.

Suppose that ${\bf r}(s)$ is given in terms of arc length; what is
$|{\bf r}'(s)|$? It is the rate at which arc length is changing
\emph{relative to arc length}; it must be 1! In the case of the helix, for
example, the arc length parameterization is $\langle
\cos(s/\sqrt2),\sin(s/\sqrt2),s/\sqrt2\rangle$, the derivative is
$\langle -\sin(s/\sqrt2)/\sqrt2,\cos(s/\sqrt2)/\sqrt2,1/\sqrt2\rangle$,
and the length of this is
$$\sqrt{{\sin^2(s/\sqrt2)\over2}+{\cos^2(s/\sqrt2)\over2}+{1\over2}}=
\sqrt{{1\over2}+{1\over2}}=1.$$
So in general, ${\bf r}'$ is a unit tangent vector.

Given a curve ${\bf r}(t)$, we would like to be able to measure, at
various points, how sharply curved it is. Clearly this is related to
how ``fast'' a tangent vector is changing direction, so a first guess
might be that we can measure curvature with $|{\bf r}''(t)|$. A little
thought shows that this is flawed; if we think of $t$ as time, for
example, we could be tracing out the curve more or less quickly
as time passes. The second derivative $|{\bf r}''(t)|$ incorporates
this notion of time, so it depends not simply on the geometric
properties of the curve but on how quickly we move along the curve.

\begin{example}{}{}
Consider ${\bf r}(t)=\langle \cos t,\sin t,0\rangle$ and
${\bf s}(t)=\langle \cos 2t,\sin 2t,0\rangle$. Both of these vector
functions represent the unit circle in the $x$-$y$ plane, but if $t$
is interpreted as time, the second describes an object moving twice as
fast as the first. Computing the second derivatives, we find
$|{\bf r}''(t)|=1$, $|{\bf s}''(t)|=4$.
\end{example}

To remove the dependence on time, we use the arc length
parameterization. If a curve is given by ${\bf r}(s)$, then the first
derivative ${\bf r}'(s)$ is a unit vector, that is,
${\bf r}'(s)={\bf T}(s)$. We now compute the second derivative
${\bf r}''(s)={\bf T}'(s)$ and use  $|{\bf T}'(s)|$ as the
``official'' measure of
\dfont{curvature}, usually denoted $\kappa$.

\begin{example}{}{}
We have seen that the arc length parameterization of a
particular helix is ${\bf r}(s)=
\langle \cos(s/\sqrt2),\sin(s/\sqrt2),s/\sqrt2\rangle$.
Computing the second derivative gives
${\bf r}''(s)=
\langle -\cos(s/\sqrt2)/2,-\sin(s/\sqrt2)/2,0\rangle$ with length $1/2$.
\end{example}

What if we are given a curve as a vector function ${\bf r}(t)$, where
$t$ is not arc length? We have seen that arc length can be difficult
to compute; fortunately, we do not need to convert to the arc length
parameterization to compute curvature. Instead, let us imagine that we have
done this, so we have found $t=g(s)$ and then formed
$\hat{\bf r}(s)={\bf r}(g(s))$. The first derivative $\hat{\bf r}'(s)$
is a unit tangent vector, so it is the same as the unit tangent vector
${\bf T}(t)={\bf T}(g(s))$. Taking the derivative of this we get
$${d\over ds}{\bf T}(g(s))= {\bf T}'(g(s)) g'(s)={\bf T}'(t){dt\over
  ds}.$$
The curvature is the length of this vector:
$$\kappa = |{\bf T}'(t)||{dt\over ds}|={|{\bf T}'(t)|\over|ds/dt|}=
{|{\bf T}'(t)|\over|{\bf r}'(t)|}.$$
(Recall that we have seen that $ds/dt=|{\bf r}'(t)|$.) Thus we can
compute the curvature by computing only derivatives with respect to
$t$; we do not need to do the conversion to arc length.

\begin{example}{}{}
Returning to the helix, suppose we start with the parameterization
${\bf r}(t)=\langle \cos t,\sin t,t\rangle$. Then
${\bf r}'(t)=\langle -\sin t,\cos t,1\rangle$,
$|{\bf r}'(t)|=\sqrt2$, and ${\bf T}(t)=\langle -\sin t,\cos
t,1\rangle/\sqrt2$. Then
${\bf T}'(t)=\langle -\cos t,-\sin t,0\rangle/\sqrt2$ and
$|{\bf T}'(t)|=1/\sqrt2$. Finally, $\kappa=1/\sqrt2/\sqrt2=1/2$,
as before.
\end{example}

\begin{example}
Consider this circle of radius $a$:
${\bf r}(t)=\langle a\cos t,a\sin t,1\rangle$. Then
${\bf r}'(t)=\langle -a\sin t,a\cos t,0\rangle$,
$|{\bf r}'(t)|=a$, and ${\bf T}(t)=\langle -a\sin t,a\cos
t,0\rangle/a$. Now
${\bf T}'(t)=\langle -a\cos t,-a\sin t,0\rangle/a$ and
$|{\bf T}'(t)|=1$. Finally, $\kappa=1/a$:
the curvature of a circle is everywhere the inverse of the radius. It
is sometimes useful to think of curvature as describing what circle a
curve most resembles at a point. The curvature of the helix in the
previous example is $1/2$; this means that a small piece of the helix
looks very much like a circle of radius $2$, as shown in
Figure~\ref{fig:osculating circle}.
\end{example}

\begin{figure}[H]
\centerline{
\vbox{\beginpicture
\normalgraphs
\setcoordinatesystem units <3truecm,3truecm>
\setplotarea x from 0 to 1.1, y from 0 to 1.1
\put {\hbox{\epsfxsize5cm\epsfbox{images/osculating_circle.eps}}} at 0 0
\endpicture}}
\caption{A circle with the same curvature as the helix. \label{fig:osculating circle}}
\end{figure}

\begin{example}{}{}
Consider ${\bf r}(t)=
\langle \cos t,\sin
t,\cos 2t\rangle$, as shown in Figure~\ref{fig:roller coaster}.
${\bf r}'(t)=
\langle -\sin t,\cos
t,-2\sin (2t)\rangle$ and $|{\bf r}'(t)|=
\sqrt{1+4\sin^2(2t)}$, so
$${\bf T}(t)=\left\langle {-\sin t\over \sqrt{1+4\sin^2(2t)}},
{\cos t\over \sqrt{1+4\sin^2(2t)}},
{-2\sin 2t\over \sqrt{1+4\sin^2(2t)}}\right\rangle.$$
Computing the derivative of this and then the length of the resulting
vector is possible but unpleasant.
\end{example}

Fortunately, there is an alternate formula for the
curvature that is often simpler than the one we have:
$$\kappa = {|{\bf r}'(t)\times{\bf r}''(t)|\over|{\bf r}'(t)|^3}.$$

\begin{example}{}{}
Returning to the previous example, we compute the second derivative
${\bf r}''(t)=
\langle -\cos t,-\sin t,-4\cos(2t)\rangle$. Then the cross product
${\bf r}'(t)\times{\bf r}''(t)$ is
$$\langle -4\cos t\cos 2t-2\sin t\sin 2t,
2\cos t\sin 2t-4\sin t \cos2t,1\rangle.$$ Computing the length of this
vector and dividing by $|{\bf r}'(t)|^3$ is still a bit tedious.
With the aid of a computer we get
$$\kappa = {\sqrt{48\cos^4 t - 48\cos^2 t + 17}\over
(-16\cos^4 t +16\cos^2t+1)^{3/2}}.$$
Graphing this we get

\begin{figure}[H]
\centerline{
\vbox{\beginpicture
\normalgraphs
\setcoordinatesystem units <10truemm,10truemm>
\setplotarea x from 0 to 6.28, y from 0 to 4.2
\axis left ticks length <2pt> withvalues {$2$} {$4$} / at 2 4 / /
\axis bottom ticks length <2pt> withvalues {$\pi\over2$} {$\pi$}
{$3\pi\over2$} {$2\pi$} / at 1.57 3.14 4.71 6.28 / /
\plot
0.000 4.123 0.031 4.022 0.063 3.742 0.094 3.343 0.126 2.894
0.157 2.451 0.188 2.048 0.220 1.699 0.251 1.408 0.283 1.169
0.314 0.975 0.346 0.819 0.377 0.692 0.408 0.590 0.440 0.507
0.471 0.439 0.503 0.384 0.534 0.340 0.565 0.303 0.597 0.273
0.628 0.250 0.660 0.231 0.691 0.217 0.723 0.208 0.754 0.202
0.785 0.200 0.817 0.202 0.848 0.208 0.880 0.217 0.911 0.231
0.942 0.250 0.974 0.273 1.005 0.303 1.037 0.340 1.068 0.384
1.100 0.439 1.131 0.507 1.162 0.590 1.194 0.692 1.225 0.819
1.257 0.975 1.288 1.169 1.319 1.408 1.351 1.699 1.382 2.048
1.414 2.451 1.445 2.894 1.477 3.343 1.508 3.742 1.539 4.022
1.571 4.123 1.602 4.022 1.634 3.742 1.665 3.343 1.696 2.894
1.728 2.451 1.759 2.048 1.791 1.699 1.822 1.408 1.854 1.169
1.885 0.975 1.916 0.819 1.948 0.692 1.979 0.590 2.011 0.507
2.042 0.439 2.073 0.384 2.105 0.340 2.136 0.303 2.168 0.273
2.199 0.250 2.231 0.231 2.262 0.217 2.293 0.208 2.325 0.202
2.356 0.200 2.388 0.202 2.419 0.208 2.450 0.217 2.482 0.231
2.513 0.250 2.545 0.273 2.576 0.303 2.608 0.340 2.639 0.384
2.670 0.439 2.702 0.507 2.733 0.590 2.765 0.692 2.796 0.819
2.827 0.975 2.859 1.169 2.890 1.408 2.922 1.699 2.953 2.048
2.985 2.451 3.016 2.894 3.047 3.343 3.079 3.742 3.110 4.022
3.142 4.123 3.173 4.022 3.204 3.742 3.236 3.343 3.267 2.894
3.299 2.451 3.330 2.048 3.362 1.699 3.393 1.408 3.424 1.169
3.456 0.975 3.487 0.819 3.519 0.692 3.550 0.590 3.581 0.507
3.613 0.439 3.644 0.384 3.676 0.340 3.707 0.303 3.738 0.273
3.770 0.250 3.801 0.231 3.833 0.217 3.864 0.208 3.896 0.202
3.927 0.200 3.958 0.202 3.990 0.208 4.021 0.217 4.053 0.231
4.084 0.250 4.115 0.273 4.147 0.303 4.178 0.340 4.210 0.384
4.241 0.439 4.273 0.507 4.304 0.590 4.335 0.692 4.367 0.819
4.398 0.975 4.430 1.169 4.461 1.408 4.492 1.699 4.524 2.048
4.555 2.451 4.587 2.894 4.618 3.343 4.650 3.742 4.681 4.022
4.712 4.123 4.744 4.022 4.775 3.742 4.807 3.343 4.838 2.894
4.869 2.451 4.901 2.048 4.932 1.699 4.964 1.408 4.995 1.169
5.027 0.975 5.058 0.819 5.089 0.692 5.121 0.590 5.152 0.507
5.184 0.439 5.215 0.384 5.246 0.340 5.278 0.303 5.309 0.273
5.341 0.250 5.372 0.231 5.404 0.217 5.435 0.208 5.466 0.202
5.498 0.200 5.529 0.202 5.561 0.208 5.592 0.217 5.623 0.231
5.655 0.250 5.686 0.273 5.718 0.303 5.749 0.340 5.781 0.384
5.812 0.439 5.843 0.507 5.875 0.590 5.906 0.692 5.938 0.819
5.969 0.975 6.000 1.169 6.032 1.408 6.063 1.699 6.095 2.048
6.126 2.451 6.158 2.894 6.189 3.343 6.220 3.742 6.252 4.022
6.283 4.123 /
\endpicture}}
\end{figure}

Compare this to Figure~\ref{fig:roller coaster}. The highest
curvature occurs where the curve has its highest and lowest points,
and indeed in the picture these appear to be the most sharply curved
portions of the curve, while the curve is almost a straight line
midway between those points.
\end{example}

Let's see why this alternate formula is correct. Starting with the
definition of ${\bf T}$,
${\bf r}'=|{\bf r}'|{\bf T}$ so by the product rule
${\bf r}''=|{\bf r}'|'{\bf T}+|{\bf r}'|{\bf T}'$. Then by
Theorem~\ref{thm:cross product properties} the cross product is
\begin{align*}
{\bf r}'\times{\bf r}''&=|{\bf r}'|{\bf T}\times|{\bf r}'|'{\bf T}+
|{\bf r}'|{\bf T}\times|{\bf r}'|{\bf T}'	\\
&=|{\bf r}'||{\bf r}'|'({\bf T}\times{\bf T})+|{\bf r}'|^2
({\bf T}\times{\bf T}')	\\
&=|{\bf r}'|^2({\bf T}\times{\bf T}')
\end{align*}
because ${\bf T}\times{\bf T}={\bf 0}$, since ${\bf T}$ is
parallel to itself. Then
\begin{align*}
|{\bf r}'\times{\bf r}''|&=|{\bf r}'|^2|{\bf T}\times{\bf T}'|	\\
&=|{\bf r}'|^2|{\bf T}||{\bf T}'|\sin\theta	\\
&=|{\bf r}'|^2|{\bf T}'|
\end{align*}
using Exercise~\ref{ex:derivative is perpendicular} in
Section~\ref{sec:calculus with vector functions}
to see that $\theta=\pi/2$. Dividing both sides by
$|{\bf r}'|^3$ then gives the desired formula.

We used the fact here that ${\bf T}'$ is perpendicular to ${\bf T}$;
the vector ${\bf N}={\bf T}'/|{\bf T}'|$ is thus a unit vector
perpendicular to ${\bf T}$, called the \dfont{unit normal}
to the curve. Occasionally of use is the \dfont{unit binormal}
${\bf B}={\bf T}\times{\bf N}$, a
unit vector perpendicular to both ${\bf T}$ and ${\bf N}$.

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

\begin{enumialphparenastyle}

\begin{ex}
Find the length of $\langle 3\cos t,2t,3\sin t\rangle$,
$t\in[0,2\pi]$.
\begin{sol}
	$2\pi\sqrt{13}$
\end{sol}
\end{ex}

\begin{ex}
Find the length of $\langle t^2,2,t^3\rangle$, $t\in[0,1]$.
\begin{sol}
	$(-8+13\sqrt{13})/27$
\end{sol}
\end{ex}

\begin{ex}
Find the length of $\langle t^2,\sin t,\cos t\rangle$, $t\in[0,1]$.
\begin{sol}
	$\sqrt5/2+\ln(\sqrt5+2)/4$
\end{sol}
\end{ex}

\begin{ex}
Find the length of the curve $y=x^{3/2}$, $x\in[1,9]$.
\begin{sol}
	$(85\sqrt{85}-13\sqrt{13})/27$
\end{sol}
\end{ex}

\begin{ex}
Set up an integral to compute the length of
$\langle \cos t, \sin t, e^t\rangle$, $t\in[0,5]$. (It is tedious but
not too difficult to compute this integral.)
\begin{sol}
	$\int_0^5 \sqrt{1+e^{2t}}\,dt$
\end{sol}
\end{ex}

\begin{ex}
Find the curvature of $\langle t,t^2,t\rangle$.
\begin{sol}
	$2\sqrt2/(2+4t^2)^{3/2}$
\end{sol}
\end{ex}

\begin{ex}
Find the curvature of $\langle t,t^2,t^2\rangle$.
\begin{sol}
	$2\sqrt2/(1+8t^2)^{3/2}$
\end{sol}
\end{ex}

\begin{ex}
Find the curvature of $\langle t,t^2,t^3\rangle$.
\begin{sol}
	$2\sqrt{1+9t^2+9t^4}/(1+4t^2+9t^4)^{3/2}$
\end{sol}
\end{ex}

\begin{ex}
Find the curvature of $y=x^4$ at $(1,1)$.
\begin{sol}
	$12\sqrt{17}/289$
\end{sol}
\end{ex}

\end{enumialphparenastyle}