\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage[english]{babel}
\usepackage{amsthm}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{enumerate}

\author{Jayadev Naram}
\title{Number Theory and Cryptology}

\begin{document}
\maketitle

\part{Number Theory}

\newtheorem{theorem}{Theorem}
\newtheorem{corollary}{Corollary}[theorem]
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{mydef}{Definition}
\newtheorem*{remark}{Remark}
\newtheorem*{prop}{Proposition}
\newcommand{\leg}[2]{\big(\frac{#1}{#2}\big)}
\newcommand{\modp}[2]{#1(mod\;#2)}

\begin{mydef}[Binary Operation]
A binary operation on a set S is a function from $S\times{S}$ to $S$.
\\* Eg: $A:\mathbb{Z}\times{\mathbb{Z}} \to \mathbb{Z}$, i.e, $(a,b) \mapsto a+b$
\end{mydef}

\begin{mydef}[Domain]
A domain is triple $(D,+,\cdot), where\, \vert{D}\vert > 1 \;and\; + \;and\; \cdot \;are\; two\; operations\; on\; D\; such\; that\;{:}$
\begin{enumerate}[i)]
	\item $a+b = b+a \;and\; a\cdot{b} = b\cdot{a}, \forall\, a,b \in D$
	\item $(a+b)+c = a+(b+c) \;and\; (a\cdot{b})\cdot{c} = a\cdot(b\cdot{c}), \forall\, a,b,c \in D$
	\item $\exists\, 0,1 \in D, a+0 = a\;and\;a\cdot{1} = a, \forall a \in D$
	\item $a\cdot(b+c) = a\cdot{b} + a\cdot{c}, \forall\, a,b,c \in D$
	\item $\forall\,a\in D, \exists\,\;a^{\prime},\;a+a^{\prime} = 0$
	\item $a\cdot{b} = 0 \implies either\;a\, = 0\;or\;b= 0$
\end{enumerate}
Eg: $(\mathbb{Z},+,\cdot) \;and\; (\mathbb{R}[X],+,\cdot), where\; \mathbb{R}[X]$ is the Set of real polynomials
\end{mydef}

\begin{mydef}[Field]
If every non-zero elements of a domain D has an inverse, i.e, units are $D-\{0\}$, then D is called a field.
\end{mydef}

\section*{Division Algorithm}

\begin{theorem}
Let $a \in \mathbb{Z}$ and $b \in \mathbb{N}$. Then $\exists$ unique q,r $\in \mathbb{Z}$ such that $$a=bq+r,\, 0\,{\le}r<b$$
\end{theorem}

\begin{proof}
If $a=0$ (trivial). Let's prove for $a \in \mathbb{N}$ by induction.
If $a = 1$, take $r = 1$ and $q = 0$ (Base Case).
Assume the statement is true $\forall n \in \mathbb{N}, n < a$, then we prove the statement for a.
If $a \ge b\, then\; a-b<a$.
Then by induction, we have $$a-b = qb+r, \; 0\, \le r <b \implies a = (q+1)b+r$$
If $a<b$, then take $q = 0 \;and\;r=a$. Hence the theorem is proved for $a\in \mathbb{N}$ \\
Now let $a\in \mathbb{Z}_{-}$. Then $-a\in\mathbb{N}$.
\begin{align*}
&\exists\; \text{q and r,} -a = bq+r,\,  0\, \le r <b \\
&\implies a = (-q)b + (-r) \\
&\implies a = (-q-1)b + (b-r),\; where\;  0\, \le b- r <b
\end{align*}
This ends the existence proof.\\
Now we prove the uniqueness. Let $(q,r) and (q^{\prime},r^{\prime})$ be two pairs that satisfy the theorem. Then,
$$a=bq+r,\, 0\,{\le}r<b$$
$$a=bq^{\prime}+r^{\prime},\, 0\,{\le}r^{\prime}<b$$
WLOG, assume $r^{\prime}\ge r$, then
\begin{align*}
&\implies 0\;\le r^{\prime}-r<b \\
&\implies bq+r = bq^{\prime}+r^{\prime} \\
&\implies b(q-q^{\prime}) = r^{\prime}-r \\
&\implies b\,|\,(r^{\prime}-r) \\
&\implies r^{\prime}=r \;and\; q^{\prime}=q \qquad(\text{since }r^{\prime}-r<b)
\end{align*}
This completes the uniqueness proof.
\end{proof}

\begin{lemma}[Modified Division Algorithm]
Let $a \in \mathbb{Z}$ and $b \in \mathbb{N}$. Then $\exists$ unique q,r $\in \mathbb{Z}$ such that $$a=bq+r,\, \vert r\vert\le \frac{b}{2}$$
\end{lemma}

\begin{theorem}
Let $a(X),\,b(X) \in \mathbb{R}[X]$. Then $\exists$ q(X),r(X) $\in \mathbb{R}[X]$ such that $$a(X)=b(X)q(X)+r(X),\, either\; r(X) = 0 \;or\; deg(r(X))<deg(b(X))$$
\end{theorem}

\begin{proof}
Proof by induction on deg(a(X)). If $deg(a(X))<deg(b(X))$, then take $q(X) = 0 \;and\; r(X) = a(X)$. If $deg(b(X)) = 0,$ i.e, $b(X) = b_{0}$, then take $q(X) = {b_{0}}^{-1}a(X)$ and $r(X) = 0$.\\
Now assume $deg(b(X)) > 0$ and $deg(a(X))\ge deg(b(X))$ and also assume the theorem is true $\forall\; h(X) \in \mathbb{R}[X],\; deg(h(X))<deg(a(X))$. \\
Then if $deg(a(X)) = m \;and\; deg(b(X)) = n$,
\begin{align*}
&\implies a(X) = a_{0} + a_{1}X + \cdots + a_{m}X^{m}\\
& and\;\;\;\; b(X) = b_{0} + b_{1}X + \cdots + b_{n}X^{n},\qquad(m\ge n)
\end{align*}
Now consider the polynomial $g(X) = a(X)-{b_{n}}^{-1}a_{m}X^{m-n}b(X)$. It can be easily verified that $deg(g(X)) < m$. Then,
$$\exists\; q(X),r(X)\in \mathbb{R}[X], g(X) = b(X)q(X)+r(X),$$
\begin{flushright}
$where\; r(X) = 0\,\; or\; deg(r(X)) \le deg(b(X))$
\end{flushright}
\begin{align*}
&\implies a(X)-{b_{n}}^{-1}a_{m}X^{m-n}b(X) = b(X)q(X)+r(X) \\
&\implies a(X) = b(X)(q(X)+{b_{n}}^{-1}a_{m}X^{m-n})+r(X),
\end{align*}
\begin{flushright}
$where\;r(X) = 0\,\; or\; deg(r(X)) \le deg(b(X))) $
\end{flushright}
\end{proof}

\begin{mydef}[Unit]
The multiplicatively invertible elements in a domain are caleed units of a domain.
\\* Eg: Units in $\mathbb{Z} = \{\pm 1\}$ and Units in $\mathbb{R}[X] = \{c\mid c \in \mathbb{R}-\{0\}\}$
\end{mydef}

\begin{mydef}[Prime]
a is prime if $a = uv \implies$ either u or v is a unit, but not both.
\end{mydef}

\begin{mydef}[Associate]
b is an associate of a if $a\mid b \; and \; b\mid a$ or equivalently $a = ub$, where u is a unit.
\end{mydef}

\begin{theorem}
If x is a prime and u is a unit, then ux is also a prime.
\end{theorem}

\begin{proof}
Suppose $ux=st$. Since u is a unit, $x=(u^{-1}s)t$. But we know, x is a prime, then either of $u^{-1}s \; or \; t$ is a unit. If t is unit, proof is completed. Else $u^{-1}s$ must be a unit. We know that the product of two units is again a unit. So is $uu^{-1}s$, i.e, s is a unit.
\end{proof}

\begin{mydef}[Greatest Common Divisor]
d is said to be gcd of a and b if $d\mid a\;and\;d\mid b$ and every common divisor c of a and b must divide d, i.e, if $c\mid a\;and\;c\mid b$, then $c\mid d$. It is written as d = (a,b).
\end{mydef}

\begin{remark}
If d is a gcd a and b and then an associate of d is also a gcd of a and b, i.e, if u is a unit, then $d = (a,b) = ud$.
\end{remark}

\begin{mydef}
If a and b $\in\; \mathbb{Z}$, then we define $$a\mathbb{Z}+b\mathbb{Z} = \{ax+by\mid x, y\in\mathbb{Z}\}$$
\end{mydef}

\begin{remark}
It can be seen that $a,b \in a\mathbb{Z}+b\mathbb{Z}$ and if $s_{1} \;and\; s_{2} \in a\mathbb{Z}+b\mathbb{Z}$ then $s_{1}x+s_{2}y \in a\mathbb{Z}+b\mathbb{Z},\; \forall x,y \in\mathbb{Z}$. Therefore $a\mathbb{Z}+b\mathbb{Z} \cap \mathbb{N} \neq \emptyset$.
\end{remark}

\begin{theorem}
If a, b $\in \mathbb{Z}$, then $\exists d \in \mathbb{Z}, a\mathbb{Z}+b\mathbb{Z} = d\mathbb{Z}\text{, where }d = (a,b).$
\end{theorem}

\begin{proof}
We first prove the existence of such a d. Since $a\mathbb{Z}+b\mathbb{Z} \cap \mathbb{N} \neq \emptyset$, let d be is least natural number in $a\mathbb{Z}+b\mathbb{Z}$. Then $d\mathbb{Z} \subseteq a\mathbb{Z}+b\mathbb{Z}$. Now let $s \in a\mathbb{Z}+b\mathbb{Z}$, then by division algorithm on $\mathbb{Z},$
\begin{align*}
&\exists \,q,r \in \mathbb{Z}, s = qd+r, 0\,\le r < d. \\
&\implies r = s-qd \in \mathbb{Z} \\
&\implies r = 0,\, i.e,\; s = qd \\
&\implies a\mathbb{Z}+b\mathbb{Z} \subseteq d\mathbb{Z} \\
& \;\;Therefore,\; a\mathbb{Z}+b\mathbb{Z} = d\mathbb{Z}.
\end{align*}
Now we prove that $d  = (a,b)$. Since $a,b \in a\mathbb{Z}+b\mathbb{Z},\, d\mid a \;and\; d\mid b$. But $d \in a\mathbb{Z}+b\mathbb{Z}, \;so\; d=ax+by$ for some $x,y\in\mathbb{Z}$. Suppose $c\mid a\;and\;c\mid b$, then $a = a_{1}c \;and\; b=b_{1}c$. Then $d = c(xa_{1}+yb_{1})$, implies $c\mid d$.
\end{proof}

\begin{corollary}
If $a\mid bc\;and\;(a,b) = 1,\;then\;a\mid c$.
\end{corollary}

\begin{theorem}
$\mathbb{Z}$ is a UFD (Unique factorization Domain), i.e, every non-zero, non-unit can be written as product of primes and this factorzation is unique upto order and association, i.e, if n is a non-zero, non-unit in $\mathbb{Z}$, and $n = p_{1}p_{2}{\cdots}p_{r} = q_{1}q_{2}{\cdots}q_{s}$, where ${p_{i}}^{\prime}s$ and ${q_{i}}^{\prime}s$ are primes, then $r=s$ and every $p_{i}$ is an associate of some $q_{j}$ and vice versa.
\end{theorem}

\begin{proof}
The exitence of such factorization can be proved by using stroing induction for non-negative integers and using this result, we can multiply by a -1 (unit) and show it's true for negative integers as well. \\
Now, we prove the uniqueness by induction. Suppose n is a non-zero, non-unit.$$Suppose\;n = p_{1}p_{2}{\cdots}p_{r} = q_{1}q_{2}{\cdots}q_{s}.$$
If $r = 1\;(Base\;Case),\;then\; n = p_{1} = q_{1}q_{2}{\cdots}q_{s}$. But $p_{1}$ is a prime, therefore, $s=1\;and\;n=p_{1}=uq_{1},$ where u is a unit. Assume the statement is true $\forall\;a\in\mathbb{N},\;a<n$. Now we prove the statement for n. $$p_{r}\mid n,\;i.e,\; p_{r}\mid q_{1}(q_{2}{\cdots}q_{s}).$$
$$\text{If }(p_{r},q_{1})=1\implies p_{r}\mid q_{2}(q_{3}{\cdots}q_{s})$$
This way, we get some $q_{j}$ which is an associate of $p_{r}$. WLOG, we can assume $p_{r}$ is an associate of $q_{s}$, i.e, $up_{r} = q_{s}.$
\begin{align*}
&\implies p_{1}p_{2}{\cdots}p_{r} - uq_{1}q_{2}{\cdots}q_{s-1}p_{r}=0 \\
&\implies p_r(p_{2}{\cdots}p_{r-1} - uq_{1}q_{2}{\cdots}q_{s-1}) = 0 \\
&\implies p_{2}{\cdots}p_{r-1} = uq_{1}q_{2}{\cdots}q_{s-1} < n
\end{align*}
\end{proof}

\begin{mydef}[$\mathbb{Z}\lbrack\omega\rbrack$]
$\mathbb{Z}\lbrack\omega\rbrack = \{a+b\omega\mid a,b\in \mathbb{Z}\} \subset \mathbb{C}$, where $\omega  = \frac{-1\pm i\sqrt{3}}{2}$.
\begin{center}
and $N(\alpha) = {\alpha}\bar{\alpha}.$ \\
\end{center}
\end{mydef}

\begin{remark}
If $\alpha = a+b\omega,$ then
\begin{align*}
N(a+b\omega) &= (a+b\omega)(\overline{a+b\omega}) \\
&= (a+b\omega)(a+b{\omega}^{2}) \\
&= a^2 - ab + b^2 \\
&= \frac{(2a-b)^2+3{b^2}}{4}
\end{align*}
\end{remark}

\begin{remark}
The only element whose norm is 0 is 0.
\end{remark}

\begin{prop}
$\alpha \in \mathbb{Z}[\omega]$ is a unit iff $N(\alpha) = 1$.
\end{prop}

\begin{proof}
Suppose $N(\alpha) = 1$, then ${\alpha}\overline{\alpha} = 1$. Therefore $\alpha$ is a unit in $\mathbb{Z}[\omega]$. \\
Conversely, suppose $\alpha$ is a unit $\mathbb{Z}[\omega]$.
\begin{align*}
&\exists {\alpha}^{\prime} \in \mathbb{Z}[\omega], \alpha{\alpha}^{\prime} = 1 \\
&\implies N(\alpha{\alpha}^{\prime}) = 1 \\
&\implies N(\alpha)N({\alpha}^{\prime}) = 1 \\
&\implies N(\alpha)= 1 \qquad(since,\; N(\alpha) \in \mathbb{N}, \forall\; \alpha \in \mathbb{Z}[\omega]).
\end{align*}
\end{proof}

\begin{theorem}
The units in $\mathbb{Z}[\omega]$ are $\pm 1,\;\pm \omega,\;\pm {\omega}^{2}$.
\end{theorem}

\begin{theorem}
There is no element in $\mathbb{Z}[\omega]$ with norm 2.
\end{theorem}

\begin{theorem}
The only elements in $\mathbb{Z}[\omega]$ with norm 3 are $\pm \pi,\;\pm \pi\omega,\;\pm \pi{\omega}^{2}$, where $\pi = 1-\omega$.
\end{theorem}

\begin{theorem}
$\mathbb{Z}[\omega]$ is a Euclidean Domain, i.e, $$\forall \alpha, \beta \in \mathbb{Z}[\omega],\;\beta\neq 0,\;\exists \gamma,\delta \in \mathbb{Z}[\omega],\alpha=\beta\gamma+\delta, N(\delta)<N(\beta).$$
\end{theorem}

\begin{proof}
Let $\alpha = a+b\omega,\;\beta = c+d\omega,a,b,c,d\in\mathbb{Z}[\omega],\;\beta\neq 0$, then $c,d\neq 0$.
\begin{enumerate}[$\text{Case }$i)]
	\item Let $d=0$. Then by Modified Division Algorithm, we have
	\begin{align*}
	a &= cq_{1}+r_{1}, \qquad(q_{1},r_{1}\in\mathbb{Z}\;and\;\vert r_{1}\vert\le\frac{c}{2})\\
	b &= cq_{2}+r_{2}, \qquad(q_{2},r_{2}\in\mathbb{Z}\;and\;\vert r_{2}\vert\le\frac{c}{2})\\
	\implies \;\;\;\;\;\;\alpha &= a+b\omega = c(q_{1}+q_{2}\omega)+(r_{1}+r_{2}\omega) \\
	\implies N(\delta) &= N(r_{1}+r_{2}\omega) \\
	&= {r_{1}}^2-r_{1}r_{2}+{r_{2}}^2 \\
	&\le {\vert r_{1}\vert}^2+\vert r_{1}\vert\vert r_{2}\vert+{\vert r_{2}\vert}^2 \\
	&= \frac{c^2}{4} + \frac{c^2}{4} + \frac{c^2}{4} \\
	&= \frac{3c^2}{4} < c^2 = N(b) = N(\beta)
	\end{align*}
	\item If $d\neq 0,$ consider $\alpha^{\prime} = \alpha\overline{\beta},\;\beta^\prime = \beta\overline{\beta}$,\;then\;$\beta^\prime \in \mathbb{Z}$, then by Case i), $$\exists \gamma^\prime,\delta^\prime\in\mathbb{Z}[\omega],\alpha^\prime = \beta^\prime\gamma^\prime+\delta^\prime, N(\delta^\prime)<N(\beta^\prime)={(N(\beta))}^2.$$
Let $\delta = \alpha-\beta\gamma$, then $\delta\overline{\beta} = \alpha\overline{\beta}-\beta\overline{\beta}\gamma = \delta^\prime. N(\delta\beta^\prime) = N(\delta^\prime) < (N(\beta))^2$
\begin{align*}
&\implies N(\delta)N(\beta)<(N(\beta))^2 \\
&\implies N(\delta)<N(\beta).
\end{align*}
\end{enumerate}
\end{proof}

\begin{theorem}
If $\alpha, \beta \in \mathbb{Z}[\omega]$, then $\exists\;\delta \in \mathbb{Z}[\omega], \alpha\mathbb{Z}[\omega]+\beta\mathbb{Z}[\omega] = \delta\mathbb{Z}[\omega],\\ \text{ where }\delta = (\alpha,\beta).$
\end{theorem}

\begin{mydef}[]
If $a,b,m\in\mathbb{Z}$ and $m\neq0$, we say thata is congruent to b modulo m if  $m\mid b-a$. This relation is written $a\equiv b\;(m).$
\end{mydef}

\begin{mydef}[$\mathbb{Z}_{n},\;+_{n},\;\cdot_{n}$]
-FILL IN-
\end{mydef}

\begin{theorem}
If $a\in\mathbb{Z}_n-\{0\}$ is a unit iff $(a,n)=1.$
\end{theorem}

\begin{proof}
Let $a\in\mathbb{Z}_n-\{0\}$ be a unit. Then $\exists\,a^\prime\in\mathbb{Z}_n-\{0\}$, such that $a\cdot_n a^\prime=1$, i.e, $\exists\;q,\;aa^\prime=qn+1.$ $$\implies (a,n)=1.$$
Now let $(a,n)=1$, then $\exists\,u,v\in\mathbb{Z},\;au+nv=1$. By Division Algorithm, $\exists\,q,r,\;such\;that\;u=qn+r,\,r\in\mathbb{Z}_n.$
\begin{align*}
&\implies a(qn+r)+nv=1 \\
&\implies ar=n(-aq-v)+1 \\
&\implies a\cdot_n r=1 \qquad(Since,\;a,r\in\mathbb{Z}_n).
\end{align*}
Therefore, a is a unit in $\mathbb{Z}_n.$
\end{proof}

\begin{mydef}
We define $U_n$ to be the set of all units in $\mathbb{Z}_n$ and $\phi(n)$ to be the cardinality of $U_n$, where $\phi_n$ is called Euler totient function, i.e, $$U_n=\{a\in\mathbb{Z}_n-\{0\}\mid (a,n)=1\},\;\phi(n)=\vert U_n\vert.$$
We define $\phi(1)=1.$
\end{mydef}

\begin{remark}
If $n=p,$ p is prime, then every element is relatively prime to p, i.e, $U_p=\mathbb{Z}_p-\{0\}=\{1,2,\cdots,p-1\}$. And also $(\mathbb{Z}_p,+_p,\cdot_p)$ is a field. If $n=p^t,\;\phi(n)=p^{t-1}(p-1).$ If $n=pq,\;\phi(n)=(p-1)(q-1).$
\end{remark}

\begin{theorem}[Euler's Theorem]
If $(a,n)=1$, then $a^{\phi(n)}\equiv 1\,(mod\,n).$
\end{theorem}

\begin{proof}
Let's prove it for elements in $U_n$ first and then for any element in general. Let $U_n = \{a_1,a_2,\cdots,a_{\phi(n)}\}$ and let $a\in U_n.$ Then,
$$a\cdot_n U_n=\{a\cdot_n a_1,a\cdot_n a_2,\cdots,a\cdot_n a_{\phi(n)}\}\subseteq U_n$$
\textbf{Claim.} All elements of $a\cdot_n U_n$ are distinct, i.e, $a\cdot_n U_n = U_n$. \\
We prove this by contradiction. Assume, $a\cdot_n a_i = a\cdot_n a_j,\;such\;that\;i\neq j$. Then $a^{-1}\cdot_n a\cdot_n a_i = a^{-1}\cdot_n a\cdot_n a_j$, hence $a_i=a_j$. Therefore, $a\cdot_n U_n = U_n$.
\begin{align*}
&\implies \prod_{i=1}^{\phi(n)}a\cdot_n a_i = \prod_{j=1}^{\phi(n)}a_j \\
&\implies a^{\phi(n)}\Bigg(\prod_{i=1}^{\phi(n)}a_i\Bigg) = \prod_{j=1}^{\phi(n)}a_j \\
&\implies a^{\phi(n)}b = b,\;where\;b=\prod_{i=1}^{\phi(n)}a_i \in U_n \\
&\implies a^{\phi(n)} = 1\;in\;(\mathbb{Z}_{n},\;+_{n},\;\cdot_{n}).
\end{align*}
Now, let's prove the theorem for any $a\in\mathbb{Z},\;such\;that\;(a,n)=1.$
By Division Algorithm, $\exists\,q,r,\;such\;that\;a=qn+r,\,r\in\mathbb{Z}_n.$ Since $(a,n)=1$, we have $(r,n)=1$.
\begin{align*}
\implies a^\phi(n) &= (qn+r)^{\phi(n)} \\
&= r^{\phi(n)} +  \binom{\phi(n)}{1}(nq)+\cdots+(nq)^{\phi(n)} \\
&= r^{\phi(n)} +nk \\
\implies a^\phi(n)-1 &=r^{\phi(n)}-1+nk \\
But\;n\mid r^{\phi(n)}-&1,\;then\;n\mid a^{\phi(n)}-1\\
\implies a^{\phi(n)}&\equiv 1\,(mod\,n)
\end{align*}
\end{proof}
\textbf{Notation:} ${\mathbb{Z}_p}^x=\mathbb{Z}_p-\{0\}\;and\;{\mathbb{Z}_p}^{x^2}$ to be set of elements in ${\mathbb{Z}_p}^x$ which are square. Here p is a prime.

\begin{prop}
$\vert {\mathbb{Z}_p}^{x^2}\vert=\frac{p-1}{2}$, therefore $\exists\,u\in{\mathbb{Z}_p}^x$ which is a non-sqaure. Then $u{\mathbb{Z}_p}^{x^2}$ will be the set of all non-square in ${\mathbb{Z}_p}^x$.
\end{prop}

\begin{proof}
First, we prove that $\vert {\mathbb{Z}_p}^{x^2}\vert=\frac{p-1}{2}$. Consider the following mapping:
\begin{align*}
{\mathbb{Z}_p}^{x} &\mapsto {\mathbb{Z}_p}^{x^2} \\
x &\mapsto x^2 \\
\implies p-x &\mapsto (p-x)^2 = p^2-2px+x^2 = x^2 + pk \\
\implies p-x &\mapsto x^2\;in\;(\mathbb{Z}_p,+_p,\cdot_p)
\end{align*}
Therefore this mapping is a 2-1 mapping and hence $\vert {\mathbb{Z}_p}^{x^2}\vert=\frac{p-1}{2}$. There are $\frac{p-1}{2}$ non-sqaure elements in ${\mathbb{Z}_p}^x$. Let u be a non-square. Then consider the following mapping:
\begin{align*}
{\mathbb{Z}_p}^{x^2} &\mapsto u{\mathbb{Z}_p}^{x^2} \\
x^2 &\mapsto ux^2 \\
\end{align*}
We prove that this mapping is bijective. It is enough to show that all the elements in $u{\mathbb{Z}_p}^{x^2}$ are distinct and non-squares. Consider two elements $ux^2,uy^2\in u{\mathbb{Z}_p}^{x^2}$.
\begin{align*}
\text{If }ux^2&=uy^2 \\
\implies u^{-1}ux^2&=u^{-1}uy^2 \qquad(\text{since }\mathbb{Z}_p\text{ is a field})\\
\implies x^2&=y^2
\end{align*}
This shows that all the elements of $u{\mathbb{Z}_p}^{x^2}$. Now we show that elements of $u{\mathbb{Z}_p}^{x^2}$ are all the non-sqaure elements in ${\mathbb{Z}_p}^{x}$. Suppose some element in $u{\mathbb{Z}_p}^{x^2}$ is a square, i.e,
\begin{align*}
&\implies ux^2=y^2 \\
&\implies ux^2x^{-2}=y^2x^{-2} \\
&\implies u=(yx^{-1})^2 \in {\mathbb{Z}_p}^{x^2}
\end{align*}
But u is a non-square, which is a contradiction. Therefore, this mapping is not just a bijection, but none of the elements in one set belongs to other. Hence, $u{\mathbb{Z}_p}^{x^2}$ is the set of all non-squares in ${\mathbb{Z}_p}^x$.
\end{proof}

\begin{remark}
From the above proposition it can be concluded that $${\mathbb{Z}_p}^x=u{\mathbb{Z}_p}^{x^2}\oplus {\mathbb{Z}_p}^{x^2}.$$
\end{remark}

\begin{mydef}
We define a mapping such that,
\begin{align*}
\mathbb{Z}&\rightarrow\mathbb{Z}_n \\
x&\mapsto\bar{x},\bar{x}=\modp{x}{n}
\end{align*}
\end{mydef}

\begin{theorem}
The following properties hold for $x,\,y\in\mathbb{Z}$:
\begin{align*}
&i)\;\overline{x+y}=\bar{x}+_n\bar{y}
&ii)\;\overline{xy}=\bar{x}\cdot_n\bar{y}
\end{align*}
Define the following mapping from $\mathbb{Z}_{mn}\rightarrow\mathbb{Z}_n$ in the similar way as above. Then the following properties hold:
\begin{align*}
&i)\;\overline{x+_{mn}y}=\bar{x}+_n\bar{y}
&ii)\;\overline{x\cdot_{mn}y}=\bar{x}\cdot_n\bar{y}
\end{align*}
\end{theorem}

\begin{theorem}[Chinese Remainder Theorem]
Suppose $(m,n)=1$. Let $\bar{x}=\modp{x}{m}$ and $\bar{\bar{x}}=\modp{x}{n}$, $x\in\mathbb{Z}.$ Then the following mapping is bijection which preserves operation:
\begin{align*}
\mathbb{Z}_{mn} &\rightarrow\mathbb{Z}_m\times\mathbb{Z}_n \\
x&\mapsto(\bar{x},\bar{\bar{x}})
\end{align*}
\end{theorem}

\begin{proof}
Since the sets are finite, by pigeon hole principle, it is enough to show the mapping $f:\mathbb{Z}_{mn}\rightarrow\mathbb{Z}_m\times\mathbb{Z}_n,x\mapsto(\bar{x},\bar{\bar{x}})$ is onto for it to be bojective, i.e, if $\forall\;(u,v)\in\mathbb{Z}_m\times\mathbb{Z}_n\;\exists\;x\in\mathbb{Z}_{mn},$ such that $\bar{x}=\modp{x}{m}$ and $\bar{\bar{x}}=\modp{x}{n}$. \\
We will first show that $\exists\;x\in\mathbb{Z}$ satisfying the above conditions. Since $(m,n)=1,\;\exists\;M,N\in\mathbb{Z},Mm+nN=1.$ Let $x=mMv+nNu,$ then $x-u=mMv+(nN-1)u=mM(v-u)=mq\implies x=mq+u\implies \bar{x}=u.$ Similarly, $\bar{\bar{x}}=v.$ So $\exists\;x\in\mathbb{Z},\;\bar{x}=u,\bar{\bar{x}}=v$, then by division algortihm, $\exists\;r\in\mathbb{Z}_{mn},q,\;x=mnq+r.$ Notice that $u=\bar{x}=\overline{mnq+r}=\overline{mnq}+_m\bar{r}=\bar{r}$ and similarly $\bar{\bar{r}}=v.$ Therefore, $$\exists\;x\in\mathbb{Z},\;\bar{x}=\modp{x}{m}\;and\;\bar{\bar{x}}=\modp{x}{n}.$$
We know,
\begin{align*}
&i)\;\overline{x+_{mn}y}=\bar{x}+_m\bar{y} &ii)\;\overline{x\cdot_{mn}y}=\bar{x}\cdot_m\bar{y} \\
&i)\;\overline{\overline{x+_{mn}y}}=\bar{\bar{x}}+_n\bar{\bar{y}} &ii)\;\overline{\overline{x\cdot_{mn}y}}=\bar{\bar{x}}\cdot_n\bar{\bar{y}}
\end{align*}
\begin{align*}
f(x+_{mn}y)&=(\overline{x+_{mn}y},\overline{\overline{x+_{mn}y}})\\
&=(\bar{x}+_m\bar{y},\bar{\bar{x}}+_n\bar{\bar{y}})\\
&=(\bar{x},\bar{\bar{x}})+(\bar{y},\bar{\bar{y}})\\
&=f(x)+f(y).
\end{align*}
Similarly, $f(x\cdot_{mn}y)=f(x)\times f(y)$. Here addition(+) and mutliplication($\times$) are component-wise. Therefore, f is onto and hence bijective(???).
\end{proof}

\begin{theorem}
If $(m,n)=1,$ then $\phi(m)=\phi(m)\phi(n)$.
\end{theorem}

\begin{proof}
First we show that under this bijection mapping defined above the elements of $U_{mn}$ group map bijetively to the elements of $U_m\times U_n$, i.e,
\begin{enumerate}[i)]
\item if $x\in U_{mn},$ then $\bar{x}\in U_m\;and\;\bar{\bar{x}}\in U_n.$ If $x\in\mathbb{Z}_{mn},$ then $(x,mn)=1$, i.e, $ax+bmn=1$, this implies $(x,m)=1\;and\;(x,n)=1$, i.e, $\bar{x}\in\mathbb{Z}_m\;and\;\bar{\bar{x}}\in\mathbb{Z}_n$.
\item if $\bar{x}\in U_m\;and\;\bar{\bar{x}}\in U_n,$ then $x\in U_{mn}$. Then $\exists\;u\in\mathbb{Z}_m$ and $v\in\mathbb{Z}_n$, such that $\bar{x}\cdot_m u=1$ and $\bar{\bar{x}}\cdot_n v=1$. Since f is onto, $\exists\;y\in\mathbb{Z}_{mn},\;\bar{y}=u\;and\;\bar{\bar{y}}=v.$ Then $\overline{x\cdot_{mn}y}=\bar{x}\cdot_m\bar{y}=1$ and $\overline{\overline{x\cdot_{mn}y}}=\bar{\bar{x}}\cdot_m\bar{\bar{y}}=1$. Therefore, $f(x\cdot_{mn}y)=(\bar{1},\bar{\bar{1}})=f(1)$. Since f is one-to-one, $x\cdot_{mn}y=1,$ i.e, $x\in U_{mn}$.
\end{enumerate}
Therefore,
\begin{align*}
U_{mn}&\leftrightarrow U_m\times U_n\\
\implies |U_{mn}|&=|U_m||U_n|\\
\implies \phi(mn)&=\phi(m)\phi(n).
\end{align*}
\end{proof}

\begin{theorem}
Some important facts from group theory that are used later.
\begin{enumerate}[i)]
\item If order of an element in the group is equal to order of the group, then the group is cyclic.
\item Let $(G,*)$ be a finite group and $(H,*)$ be a subgroup of G, then $|H|\bigm\vert|G|.$
\item In a  finite group the order of an element must divide the order of the group.
\item If $a\in G$, $(G,*)$ be a finite group, then $a^{|G|}=e.$
\item If $a\in G$, $(G,*)$ be a finite group and if $a^i=e,$ then $o(a)\mid i.$
\item If $a\in G$, $(G,*)$ be a finite group, then $o(a^i)=\frac{o(a)}{(i,o(a))}$ and $o(a^i)=o(a)$ iff $(i,o(a))=1.$ Therefore, there are $\phi(n)$ generators in a cyclic group of order n.
\end{enumerate}
\end{theorem}

\begin{lemma}
$\sum_{d\mid n}\phi(d)=n\;\forall\;n\in\mathbb{N}.$
\end{lemma}

\begin{proof}
Define $f(n)=\sum_{d\mid n}\phi(d)$. We show that if $(m,n)=1$, then $f(mn)=f(m)f(n).$ Let $m=p^{e_1}_1p^{e_2}_2\cdots p^{e_r}_r$ and $n=q^{e_1}_1q^{e_2}_2\cdots q^{e_s}_s$. If $d\mid mn$, then $d=(p^{l_1}_1p^{l_2}_2\cdots p^{l_r}_r)(q^{r_1}_1q^{r_2}_2\cdots q^{r_s}_s)=d_1d_2,$ such that $(d,m)=d_1$ and $(d,n)=d_2.$
Now,
\begin{align*}
f(mn)&=\sum_{d\mid mn}\phi(d) = \sum_{d_1\mid m,\;d_2\mid n}\phi(d_1d_2)= \sum_{d_1\mid m,\;d_2\mid n}\phi(d_1)\phi(d_2)\\
&=\sum_{d_1\mid m}\phi(d_1)\sum_{d_2\mid n}\phi(d_2)=f(m)f(n)
\end{align*}
Let n be a non-zero, such that $n>1$. Let $n=p^{e_1}_1p^{e_2}_2\cdots p^{e_r}_r$, where $p_i$'s are distinct primes.
\begin{align*}
f(p^{e_1}_1) &= \sum_{d\mid p^{e_1}_1}\phi(d) = \phi(1) + \phi(p_1) + \phi(p^2_1) + \cdots + \phi(p^{e_1}_1) \\
&= 1+p_1+\cdots+p^{e_1-1}_1(p_1-1) = p^{e_1}_1 \\
\text{Then, }f(n) &= f(p^{e_1}_1p^{e_2}_2\cdots p^{e_r}_r) = f(p^{e_1}_1)f(p^{e_2}_2\cdots p^{e_r}_r) \\
&= p^{e_1}_1p^{e_2}_2\cdots p^{e_r}_r = n = \sum_{d\mid n}\phi(d).
\end{align*}
\end{proof}

\begin{theorem}
Let $(\mathbb{F},+,\cdot)$ be a field and G a finite subgroup of $(\mathbb{F}-\{0\},\cdot)$, then G is a cyclic group.
\end{theorem}

\begin{proof}
Given that $G\subseteq (\mathbb{F}-\{0\},\cdot)$ is finite, i.e, $|G|<\infty$. Let $|G|=n.$ If $d\mid n$ define $G_d$ as the set containing all the elements in G of order d. We have $G=\coprod_{d\mid n}\,G_d$, then $|G|=\sum_{d\mid n}|G_d|$. If $G_d=\phi,$ then $|G_d|=0.$ Suppose $|G_d|\neq0,$ let $a\in G_d$, then $o(a)=d$. \\
Consider $H=\{1,a,a^2,\cdots,a^{d-1}\},\;a^d=1$. Then $X^d-1$ is a polynomial in $\mathbb{F}[X]$. Notice that all the elements of H are the roots of the polynomial and these are the only roots of $X^d-1\;in\;\mathbb{F}.$ Therefore, $G_d\subseteq H$. Notice that the number of elements in H of order d are $\phi(d)$, since $o(a^i)=o(a)=d$ iff $(i,d)=1$, then $|G_d|=\phi(d).$ Hence, $G_d = \phi(d)$ or $0$. \\
We know, $\sum_{d\mid n}\phi(d)=n$ and $n=\sum_{d\mid n}|G_d|$, hence $|G_d|$ is never 0, i.e, $G_d$ is never empty $\forall\;d\mid n$. In particular, $G_n\neq \phi$, therefore there exists an element of order n. Hence, G is cyclic.
\end{proof}

\begin{corollary}
$(\mathbb{Z}_p-\{0\},\cdot_p)$ is cyclic group in $F=(\mathbb{Z}_p-\{0\},+,\cdot_p)$.
\end{corollary}

\begin{mydef}[Legendre Symbol]
Let $c\in\mathbb{Z},\;p$ is an odd prime. Then we define:
\[
\leg{c}{p} =
     \begin{cases}
       \;\;\,0, &\quad\text{if }p\mid c\\
       \;\;\,1, &\quad\text{if }\exists\;x\in{Z},\,x^2\equiv c(mod\;p) \\
      -1, &\quad\text{otherwise}\\
     \end{cases}
\]
\end{mydef}

\begin{theorem}
The properties of Legendre Symbol are listed below:
\begin{enumerate}[i)]
\item If $a\equiv \modp{b}{p}$, then $\leg{a}{p}=\leg{b}{p}.$
\item $\leg{xy}{p}=\leg{x}{p}\leg{y}{p}.$
\item $\leg{a}{p}={\bar{a}}^{(\frac{p-1}{2})},\;where\;\bar{a}=\modp{a}{p}.$
\item
\end{enumerate}
\end{theorem}

\end{document}


\documentclass{article}

\usepackage[sans, stdmargin, noindent]{../../rajeev}
\usepackage{mathtools}
\usepackage{dirtytalk}

\pagestyle{fancy}
\rhead{\today}
\lhead{Math 291H HW \#1}

\begin{document}

\begin{center}
    \Large \textbf{Math 291H Homework \#1}
\end{center}
\begin{center}
    \Large Rajeev Atla
\end{center}

Honors Pledge Statement: \say{The writeup of this submission is my own work alone.}

\problem{1.1}

\begin{align*}
  \pars{3, -1} &= s \pars{2, 1} + t \pars{1, 3} \\
  \pars{3, -1} &= \pars{2s+t, s+3t} \\
\end{align*}

We turn this into a system of equations.
$$
\begin{cases}
  2s + t = 3 \\
  s+3t = -1 \\
\end{cases}
$$

Solving, we find that \boxed{s=2, t=-1}.

\problem{1.4}

$$\norm{\bm{x}} = \sqrt{4^2 + 7^2 + \pars{-4}^2 + 1^2 + 2^2 + \pars{-2}^2} =  \boxed{\sqrt{15}}$$

$$\norm{\bm{y}} = \sqrt{2^2 + 1^2 + 2^2 + 2^2 + \pars{-1}^2 + \pars{-1}^2} = \boxed{\sqrt{11}}$$

\begin{align*}
  \cos \theta &= \frac{\bm{x} \cdot \bm{y}}{\norm{\bm{x}} \norm{\bm{y}}} \\
              &= \frac{4 \cdot 2 + 7 \cdot 1 + \pars{-4} \cdot 2 + 1 \cdot \pars{-1} + \pars{-2} \cdot \pars{-1}}{\sqrt{15} \cdot \sqrt{11}} \\
              &= \frac{8}{\sqrt{165}} \\
              &= \frac{8 \sqrt{165}}{165} \\
  \theta &= \boxed{\arccos{\frac{8 \sqrt{165}}{165}}} \\
\end{align*}

\problem{1.5}

$$\norm{\bm{x}} = \sqrt{4^2 + 7^2 + 4^2} =  \boxed{9}$$

$$\norm{\bm{y}} = \sqrt{2^2 + 1^2 + 2^2} =  \boxed{3}$$

\begin{align*}
  \cos \theta &= \frac{4 \cdot 2 + 7 \cdot 1 + 4 \cdot 2}{9 \cdot 3} \\
              &= \frac{23}{27} \\
  \theta &= \boxed{\arccos \frac{23}{27}} \\
\end{align*}

\problem{1.7}

\subsection*{a}

\begin{align*}
  \bm{u}_1 \cdot \bm{u}_2 &= \frac{1}{81} \pars{1 \cdot 8 + \pars{-4} \cdot 4 + \pars{-8} \cdot \pars{-1}} = 0 \\
  \bm{u}_2 \cdot \bm{u}_3 &= \frac{1}{81} \pars{8 \cdot 4 + 4 \cdot \pars{-7} + \pars{-1} \cdot 4} = 0 \\
  \bm{u}_1 \cdot \bm{u}_3 &= \frac{1}{81} \pars{1 \cdot 4 + \pars{-4} \cdot \pars{-7} + \pars{-8} \cdot 4} = 0 \\
\end{align*}

Therefore, $\set{\bm{u}_1, \bm{u}_2, \bm{u}_3}$ \emph{is} an orthonormal basis of $\RR^3$.

\begin{align*}
  \bm{u}_1 \times \bm{u}_2 &= \frac{1}{81} \pars{1, -4, -8} \times \pars{8, 4, -1} \\
                           &= \frac{1}{81} \pars{36, -63, 36} \\
                           &= \frac{1}{9} \pars{4, -7, 4} \\
                           &= \bm{u}_3 \\
\end{align*}
Since $\bm{u}_1 \times \bm{u}_2 = \bm{u}_3$, $\set{\bm{u}_1, \bm{u}_2, \bm{u}_3}$ is a \emph{right-handed} orthonormal basis of $\RR^3$.

\subsection*{b}

\begin{align*}
  y_1 \bm{u}_1 + y_2 \bm{u}_2 + y_3 \bm{u}_3 &= \pars{10, 11, -11} \\
  y_1 \pars{1, -4, -8} + y_2 \pars{8, 4, -1} + y_3 \pars{4, -7, 4} &= \pars{90, 99, -99} \\
  \pars{y_1 + 8 y_2 + 4 y_3, -4 y_1 + 4 y_2 - 7 y_3, -8 y_1 - y_2 + 4y_3} &= \pars{90, 99, -99} \\
\end{align*}

We can solve this system to find \boxed{y_1 = 6, y_2 = 15, y_3 = -9}.

\begin{align*}
  \norm{\pars{y_1, y_2, y_3}} &= \sqrt{6^2 + 15^2 + \pars{-9}^2} \\
                              &= \boxed{3 \sqrt{38}} \\
  \norm{\pars{10, 11, -11}} &= \sqrt{10^2 + 11^2 + \pars{-11}^2} \\
  &= \boxed{3 \sqrt{38}} \\
\end{align*}

\problem{1.14}

\subsection*{a}

Let $\bm{v}_1$ be the vector that passes through $\bm{a}_1$ and $\bm{a}_2$.
Let $\bm{v}_2$ be the vector that passes through $\bm{a}_2$ and $\bm{a}_3$.
Let $\bm{v}_3$ be the vector that passes through $\bm{b}_1$ and $\bm{b}_2$.
Let $\bm{v}_4$ be the vector that passes through $\bm{b}_2$ and $\bm{b}_3$.

\begin{align*}
  \bm{v}_1 &= \pars{-2, 0, -4} \\
  \bm{v}_2 &= \pars{3, -5, 1} \\
  \bm{v}_3 &= \pars{0, -1, 1} \\
  \bm{v}_4 &= \pars{-1, 1, 0} \\
\end{align*}

Since $\bm{v}_1, \bm{v}_2$ lie in the plane $P_1$, their cross product $\bm{n}_1$ is perpendicular to $P_1$.
Likewise for $\bm{v}_3, \bm{v}_4, P_2, \text{and }\bm{n}_2$, respectively.

\begin{align*}
  \bm{n}_1 &= \pars{20, -10, 10} \\
  \bm{n}_2 &= \pars{-1, -1, -1} \\
\end{align*}

We substitute into the standard form equation for a plane:

\begin{align*}
  P_1 : \bm{n}_1 \cdot \bm{r} + d_1 &= 0 \\
  P_2 : \bm{n}_2 \cdot \bm{r} + d_2 &= 0 \\
\end{align*}

Substituting $\bm{a}_1$ and $\bm{b}_1$, respectively, we find that $d_1 = -10$ and $d_2 = 3$.
After simplifying,

\begin{align*}
  P_1 &: \boxed{2x - y + z - 1 = 0} \\
  P_2 &: \boxed{x + y + z - 3 = 0} \\
\end{align*}

\subsection*{b}

Adding and subtracting the two equations, respectively

$$
\begin{cases}
  x - 2y + 2 = 0 \Longleftrightarrow y = \frac{1}{2} x + 1 \\
  3x + 2z - 4 = 0 \Longleftrightarrow z = - \frac{3}{2} x + 2 \\
\end{cases}
$$

Letting $t \in \RR$, we can write

\begin{align*}
  \bm{x} \pars{t} &= \pars{t, \frac{1}{2} t + 1, - \frac{3}{2} t + 2} \\
  \bm{x} \pars{t} &= \boxed{\pars{0, 1, 2} + t \pars{0, \frac{1}{2}, - \frac{3}{2}}} \\
\end{align*}

This is of the form $\bm{x} \pars{t} = \bm{x}_0 + t \bm{v}$.
To find the distance, we must first normalize $\bm{v}$.

\begin{align*}
  \bm{u} &= \frac{\bm{v}}{\norm{\bm{v}}} \\
  &= \frac{\sqrt{10}}{5} \pars{0, \frac{1}{2}, - \frac{3}{2}} \\
\end{align*}

Suppose the point on the line closest to $\bm{a}_1$ is $\bm{p}$.
The shortest distance is then

\begin{align*}
  \norm{\bm{p} - \bm{a}_1}^2 &= \norm{\bm{x}_0 - \bm{a}_1}^2 - \norm{\pars{\bm{x}_0 - \bm{a}_1} \cdot \bm{u}}^2 \\
                             &= \norm{\pars{0, 1, 2} - \pars{1, 2, 1}}^2 - \frac{10}{25} \abs{\pars{\pars{0, 1, 2} - \pars{1, 2, 1}} \cdot \pars{0, \frac{1}{2}, - \frac{3}{2}}}^2 \\
                             &= \norm{\pars{-1, -1, 1}}^2 - \frac{2}{5} \abs{\pars{-1, -1, 1} \cdot \pars{0, \frac{1}{2}, - \frac{3}{2}}} \\
                             &= 3 - \frac{2}{5} \pars{2} \\
                             &= \frac{13}{5} \\
  \norm{\bm{p} - \bm{a}_1} &= \boxed{\sqrt{\frac{13}{5}}} \\
\end{align*}

\subsection*{c}

\begin{align*}
  \bm{x} &= \bm{b}_1 + t \bm{a} \\
\end{align*}

Suppose the line is at $\bm{b}_1$ when $t=0$.
In addition, at $t=1$, suppose the line is at $\bm{b}_2$.
Then, $\bm{a} = \bm{b}_2 - \bm{b}_1 = \pars{0, -1, 1}$.
Therefore,

$$
\bm{x} = \pars{1, 1, 0} + t \pars{0, -1, 1}
$$

The vector equation of the line is therefore

\begin{align*}
  \bm{a} \times \pars{\bm{x} - \bm{b}_1} &= 0 \\
  \pars{0, -1, 1} \times \pars{\bm{x} - \pars{1, 1, 0}} &= 0 \\
  \pars{0, -1, 1} \times \pars{x - 1, y - 1, z} &= 0 \\
  \pars{-y-z+1, x-1, x-1} &=0 \\
\end{align*}

Clearly, $x=1$ from the $\bm{e}_2$ and $\bm{e}_3$ components of this equation.
In addition, $y + z = 1$ from the $\bm{e}_1$ component.

The equation for $P_1$ is $2x-y+z=1$.
Substituting $x=1$, we have

$$
\begin{cases}
  y + z = 1 \\
  y - z = 1 \\
\end{cases}
$$

Solving, we find $y=1$ and $z=0$.
Finally, the point of intersection is $\boxed{\pars{1, 1, 0}}$.

\problem{1.15}
Let the orthonormal basis be composed of vectors $\bm{a}, \bm{b}, \bm{c}$.
We let
$$\bm{c} := \frac{\bm{v}}{\norm{\bm{v}}} = \frac{1}{\sqrt{26}} \pars{1, 4, 3}$$

In addition, we define
$$
\bm{w} := \pars{-4, 1, 0}
$$

Note that $\bm{w}$ and $\bm{c}$ are orthogonal.
We normalize and let the resultant unit vector be $\bm{a}$.

$$
\bm{a} := \frac{1}{\sqrt{17}} \pars{-4, 1, 0}
$$

To make $\bm{b}$ orthogonal to the other two vectors, we can compute the final vector:

\begin{align*}
  \bm{y} & := \bm{v} \times \bm{w} \\
         &= \pars{-3, -12, 15} \\
  \bm{b} &= \frac{\bm{y}}{\norm{\bm{y}}} \\
  \bm{b} &= \frac{1}{\sqrt{378}} \pars{-3, -12, 15} \\
\end{align*}

\problem{1.16}

Let the orthonormal basis be $\bm{u}_1, \bm{u}_2, \bm{u}_3$.

\begin{align*}
  \bm{u}_1 &= \frac{\bm{a}}{\norm{\bm{a}}} \\
           &= \frac{1}{\sqrt{26}} \pars{1, 4, 3} \\
  \bm{u}_2 &= \frac{\bm{b}}{\norm{\bm{b}}} \\
           &= \frac{1}{\sqrt{14}} \pars{3, 2, 1} \\
  \bm{c} &= \bm{a} \times \bm{b} \\
           &= \pars{-2, 8, -10} \\
  \bm{u}_3 &= \frac{\bm{c}}{\norm{\bm{c}}} \\
  &= \frac{1}{\sqrt{168}} \pars{-2, 8, -10} \\
\end{align*}

\problem{1.17}

\subsection*{a}

Let $\pars{s, t} = \pars{0, 0}, \pars{0, 1}, \pars{1, 0}$ correspond to $\bm{p}_1, \bm{p}_2, \bm{p}_3$, respectively.
We see that $\bm{x}_0 = \bm{p}_1 = \pars{-2, 0, 2}$.
Further, we also see that $$\bm{v}_1 = \bm{p}_2 - \bm{p}_1 = \pars{3, -2, 0}$$ and $$\bm{v}_2 = \bm{p}_3 - \bm{p}_1 = \pars{5, -1, -4}$$

$$
\bm{x} \pars{s, t} = \pars{-2, 0, 2} + s \pars{3, -2, 0} + t \pars{5, -1, -4}
$$

\subsection*{b}

Let $u=0$ at $\bm{x}_0$, so $\bm{z}_0 = \bm{x}_0 = \boxed{\pars{1, 4, -2}}$.
Letting $u=1$ at $\bm{x}_1$ lets us see that $\bm{w} = \bm{z}_1 - \bm{z}_0 = \boxed{\pars{-1, -7, 3}}$.

\subsection*{c}
We can compute the normal vector by

\begin{align*}
  \bm{n} &= \bm{v}_1 \times \bm{v}_2 \\
         &= \pars{3, -2, 0} \times \pars{5, -1, -4} \\
         &= \pars{8, 12, 7} \\
\end{align*}

We know that $\bm{n} \cdot \bm{x} + d = 0$ is the general vector equation for the plane.
Substituting $\bm{x} = \bm{p}_1$, we see that $d=2$.
We can then expand,
$$
\boxed{8x + 12y + 7z + 2 =0}
$$

\subsection*{d}

The general vector equation for a line is
\begin{align*}
  \bm{w} \times \pars{\bm{z} - \bm{z}_0} &= 0 \\
  \pars{-1, -7, 3} \times \pars{\pars{x, y, z} - \pars{1, 4, -2}} &= 0 \\
  \pars{-1, -7, 3} \times \pars{x - 1, y - 4, z + 2} &= 0 \\
  \pars{-2 - 3y - 7z, -1+3x+z, -3 + 7x - y} &= 0 \\
\end{align*}

We therefore have the system,

$$
\begin{cases}
  3y + 7z = -2 \\
  3x + z = 1 \\
  7x - y = 3 \\
\end{cases}
$$

\subsection*{e}
Solving for $y$ in the last equation,

$$
y = 7x - 3
$$

Substituting into the equation for the plane,

$$
\begin{cases}
  92x + 7z + 2 = 0 \\
  3x + z = 1 \\
\end{cases}
$$

Solving and resubstituting, we find $\boxed{\pars{\frac{24}{71}, - \frac{24}{71}, - \frac{10}{71}}}$.

\subsection*{f}

We must first normalize $\bm{w}$.

\begin{align*}
  \bm{u} &= \frac{\bm{w}}{\norm{\bm{w}}} \\
  &= \frac{1}{\sqrt{59}} \pars{-1, -7, 3} \\
\end{align*}

Suppose the point on the line closest to $\bm{p}_1$ is $\bm{q}$.

\begin{align*}
  \norm{\bm{p}_1 - \bm{q}} &= \norm{\pars{\bm{x}_0 - \bm{p}_1} \times \bm{u}} \\
                           &= \frac{1}{\sqrt{59}} \norm{\pars{\pars{1, 4, -2} - \pars{-1, -3, 0}} \times \pars{-1, -7, 3}} \\
                           &= \frac{1}{\sqrt{59}} \norm{\pars{2, 7, -2} \times \pars{-1, -7, 3}} \\
                           &= \frac{1}{\sqrt{59}} \norm{\pars{7, -4, -7}} \\
                           &= \boxed{\sqrt{\frac{114}{59}}} \\
\end{align*}

\subsection*{g}

We must first normalize $\bm{n}$.

\begin{align*}
  \bm{u} &= \frac{\bm{n}}{\norm{\bm{n}}} \\
  &= \frac{1}{\sqrt{257}} \pars{8, 12, 7} \\
\end{align*}

The distance is then

\begin{align*}
  \abs{\pars{\bm{x}_0 - \bm{z}_0} \cdot \bm{u}} &= \frac{1}{\sqrt{257}} \abs{\pars{-2, 0, 2} - \pars{1, 4, -2} \cdot \pars{8, 12, 7}} \\
                                                &= \frac{1}{\sqrt{257}} \abs{\pars{-3, -4, 4} \cdot \pars{8, 12, 7}} \\
                                                &= \frac{1}{\sqrt{257}} \abs{-44} \\
                                                &= \boxed{\frac{44}{\sqrt{257}}} \\
\end{align*}

\end{document}


%!TEX root = ../Thesis.tex
\chapter{Implicitly Restarted Arnoldi's Method\label{ch:IRAM} }
A good restart vector is one where the undesired region of the spectrum of our linear operator is suppressed while the desired region is enhanced.  This is done by zeroing out the Ritz vectors associated with the Ritz values from the undesired region of the spectrum.  This is done explicitly in \fref{eq:ExplicitRestartVector} and repeated here for clarity;
\begin{equation}
    \hat{v} = c_1y_1 + \cdots + c_jy_j + 0\,y_{j+1} + \cdots + 0\,y_n;
    \label{eq:RestartVector}
\end{equation}
In addition to saving computational expense the improved restarts \emph{implicitly} restart Arnoldi's method with an improved restart vector and is thus called an implicit restart or implicitly restarted Arnoldi's method (IRAM).  To see how this is done, a brief discussion of the \QR algorithm will first be presented and then IRAM will be shown.

\section*{\QR Algorithm} \label{sec:QRAlgorithm}
A matrix $A \in \mathbb{R}^{n \times n}$can be decomposed into two matrices
\begin{equation}
    A = \QR
    \label{eq:QRDecomposition}
\end{equation}
where $Q, R \in \mathbb{C}^{n \times n}$ with $Q$ unitary (orthonormal columns) and $R$ upper triangular.  The \QR decomposition (\fref{eq:QRDecomposition}) is unique \citep[see][Chapter 3, pg. 204]{Watkins:2002Funda-0} for nonsingular matrices and $R$ with positive main diagonal entries.  The \QR factors can be recombined in reverse order to form a new matrix
\begin{equation}
    \hat{A} = RQ.
\end{equation}

The decomposition of a matrix and the recombination of the factors is typically performed iteratively.  A simple change of notation makes this illustration clear
\begin{subequations}
    \label{eq:QRIteration}
    \begin{align}
        A_{n-1} &= Q_nR_n \label{eq:QRFactIter} \\
        A_{n} &= R_nQ_n. \label{eq:QRRecombineIter}
    \end{align}
\end{subequations}
Using this notation $A_0 = A$.  The decomposition of $A_{n-1}$ and formation of $A_n$ constitutes one \QR iteration.

We can see from \fref{eq:QRFactIter} that $R_n = Q_n^*A_{n-1}$.  Substituting this in \fref{eq:QRRecombineIter} we obtain an alternative form for $A_n$
\begin{equation}
    A_{n} = Q_n^*A_{n-1}Q_n.
    \label{eq:QRIterationAlternate}
\end{equation}

\section*{Shifted \QR Iteration} \label{sec:ShiftedQR}
The \QR iteration can be shifted by subtracting the identity matrix multiplied by some scalar shift.  The factors in the shifted \QR iteration can be recombined similarly to the non-shifted counterpart
\begin{subequations}
    \label{eq:ShiftedQRIteration}
\begin{gather}
    A_{n-1} - \nu_n I = Q_nR_n \label{eq:ShiftedQRDecomposition} \\
    A_n = R_nQ_n + \nu_nI \label{eq:ShiftedQRRecombine}
\end{gather}
\end{subequations}
where $Q_n$ and $R_n$ are the same as in the non-shifted \QR iteration, $I$ is the identity matrix, and $\nu_n$ is the shift being applied.  Similarly to the non-shifted \QR iteration, we can form $A_n$ alternatively.  To see this first note from \fref{eq:ShiftedQRDecomposition} that \[R_n = Q_n^*A_{n-1} - \nu_nQ_n^*.\]  Substituting this into \fref{eq:ShiftedQRRecombine} we obtain
\begin{equation}
    \begin{split}
        A_n &= \left[ Q_n^*A_{n-1} - \nu_nQ_n^* \right]Q_n + \nu_nI \\
         &= Q_n^*A_{n-1}Q_n - \nu_nQ_n^*Q_n + \nu_nI \\
         &= Q_n^*A_{n-1}Q_n - \nu_nI + \nu_nI \\
         A_n &= Q_n^*A_{n-1}Q_n
    \end{split}
    \label{eq:ShiftedQRIterationAlternative}
\end{equation}
Equation \eqref{eq:ShiftedQRIterationAlternative} generalizes the alternative form of $A_m$ from \fref{eq:QRIterationAlternate} to the shifted \QR algorithm.

Now that the basic \QR algorithm has been given we will proceed to to develop some identities that will be of use to us in analyzing implicit restarts for Arnoldi's method.
\begin{lem}
    Let $Q_n$, $A_n$ be defined by equations \eqref{eq:ShiftedQRIteration} and let
\begin{equation}
    \hat{Q}_n \equiv Q_1Q_2\cdots Q_n,
    \label{eq:QhatDefined}
\end{equation}
then
\begin{align}
    A_n &= \hat{Q}_n^*A\hat{Q}_n  \label{eq:QRRecombined} \\
    \hat{Q}_nA_n &= A\hat{Q}_n. \label{eq:QnAn}
\end{align}
\label{lem:QRRecombined}
\end{lem}

\begin{proof}
    For $n=1$, we know from \fref{eq:ShiftedQRIteration}
    \begin{equation}
        A_1 = Q_1^*A_0Q_1 = \hat{Q}_1^*A \hat{Q}_1.
    \end{equation}
    For $n=2$, after a second iteration, we have
    \begin{equation}
        \begin{split}
            A_2 &= Q_2^*A_1Q_2 \\
             &= Q_2^*\left( Q_1^*A Q_1 \right)Q_2 \\
             &= \hat{Q}_2^*A \hat{Q}_2
        \end{split}
    \end{equation}
    where $\hat{Q}_2 = Q_1Q_2$.  We can prove this in general by induction on $n$:
    \begin{equation}
        \begin{split}
            A_n &= Q_n^*A_{n-1}Q_n \\
            &= Q_n^*\left( \hat{Q}_{n-1}^*A_{n-2} \hat{Q}_{n-1} \right)Q_n \\
             &= Q_n^*Q_{n-1}^*\cdots Q_1 A Q_1\cdots Q_{n-1}Q_n \\
             &= \hat{Q}_n^*A \hat{Q}_n
        \end{split}
    \end{equation}
    where $\hat{Q}_n = Q_1Q_2\cdots Q_n$, as defined in \fref{eq:QhatDefined}.  Equation \eqref{eq:QnAn} immediately follows from \fref{eq:QRRecombined} since $\hat{Q}_n$ is a unitary matrix.
\end{proof}

\begin{lem}
    Let $Q_n$, $A_n$ be defined by \fref{eq:ShiftedQRDecomposition} and let $\left\{\nu_1,\ldots, \nu_m\right\}$ be the shifts, then
    \begin{equation}
        \left(A-\nu_nI\right)\hat{Q}_n = \hat{Q}_n\left(A_n - \nu_nI\right),
    \end{equation}
    where $\hat{Q}_n \equiv Q_1Q_2\cdots Q_n$.
\end{lem}

\begin{proof}
    Using \fref{eq:QnAn} this becomes trivial:
    \begin{equation}
        \begin{split}
            \left(A-\nu_nI\right)\hat{Q}_n &= A\hat{Q}_n - \nu_n\hat{Q}_n \\
             &= \hat{Q}_nA_n - \nu_n\hat{Q}_n \\
             \left(A-\nu_nI\right)\hat{Q}_n &= \hat{Q}_n\left( A_n - \nu_nI \right).
        \end{split}
    \end{equation}
\end{proof}

With these two lemmas we now proceed to the theorem that is important to showing how IRAM restarts with the ideal restart vector.
\begin{thm} \label{thm:QRPolynomial}
    Let $\hat{Q}_n$ be defined as in \fref{eq:QhatDefined} and
    \begin{equation}
        \hat{R}_n \equiv R_nR_{n-1}\cdots R_1
        \label{eq:RhatDefined}
    \end{equation}
    and using $\left\{\nu_1,\nu_2,\ldots\nu_n\right\}$ as the shifts of a shifted \QR algorithm, then
\begin{equation}
    \poly{j}{A} = \hat{Q}_j\hat{R}_j,
    \label{eq:QRPolynomial}
\end{equation}
    where $\p_j$ is a polynomial of degree $j$ with zeros $\nu_1,\ldots,\nu_j$
    \begin{equation}
        \poly{j}{z} = \left(z-\nu_1I\right)\left(z-\nu_2I\right)\cdots\left(z-\nu_jI\right).
        \label{eq:Polynomial}
    \end{equation}
\end{thm}
\begin{proof}
    For $n=1$ \fref{eq:QRPolynomial} is true by definition of the shifted \QR decomposition given in \fref{eq:ShiftedQRDecomposition}
    \begin{equation}
        \left(A - \nu_1I\right) = Q_1R_1 = \hat{Q}_1\hat{R}_1.
    \end{equation}
    In general we can prove this by induction
    \begin{equation}
        \begin{split}
            \left(A-\nu_nI\right)\left[\left(A-\nu_{n-1}I\right)\cdots\left(A-\nu_1I\right)\right] &= \left(A-\nu_nI\right)\left[\hat{Q}_{n-1}\hat{R}_{n-1}\right] \\
            &=\left[A\hat{Q}_{n-1} - \nu_n\hat{Q}_{n-1}\right]\hat{R}_{n-1} \\
            &=\left[\hat{Q}_{n-1}A_{n-1} - \nu_n\hat{Q}_{n-1}\right]\hat{R}_{n-1} \\
            &=\hat{Q}_{n-1}\left(A_{n-1} - \nu_nI\right)\hat{R}_{n-1} \\
            &=\hat{Q}_{n-1}\left(Q_nR_n\right)\hat{R}_{n-1} \\
            \left(A-\nu_nI\right)\left[\left(A-\nu_{n-1}I\right)\cdots\left(A-\nu_1I\right)\right] &=\hat{Q}_n\hat{R}_n.
        \end{split}
    \end{equation}
\end{proof}

Equation \eqref{eq:QRPolynomial} shows that the product of the unitary ($\hat{Q}_n$) and upper triangular ($\hat{R}_n$) matrices from the \QR algorithm are equivalent to a polynomial of $A$ of degree $n$ with the shifts as the zeros of the polynomial.  This is an important point in IRAM.

\section*{Updating Arnoldi Factorization by Shifted \QR Iterations}
To show how IRAM uses the \QR algorithm we begin with the Arnoldi factorization first introduced in \fref{eq:ArnoldiFactorization}
\begin{equation}
    A V_m = V_mH_m + v_{m+1}h_{m+1,m}e_m^T.
    \label{eq:ArnoldiFactorizationIRAM}
\end{equation}
Implicitly restarted Arnoldi's method performs shifted \QR iterations on $H_m$ using the eigenvalues estimates from the undesired region of the spectrum of $A$.  For Monte Carlo reactor analysis the eigenvalues largest in magnitude are of particular interest however, the following treatment is independent of what region of the spectrum is desired.

We note that the Arnoldi factorization in \fref{eq:ArnoldiFactorizationIRAM} is shown after $m$ Arnoldi iterations.  For this discussion we assume that $k$ eigenvalues are desired and $m=k+j$ iterations are performed in each restart where $k \sim j$.  After $m$ Arnoldi iterations we have $m$ Ritz values---eigenvalue estimates---of $A$; $k$ of them are the eigenvalues of interest and the other $j$ values are used as shifts for the shifted \QR algorithm.

IRAM performs $j$ iterations of the shifted \QR algorithm on $H_m$ using the undesired Ritz values as described previously.  After $j$ iterations we obtain
\begin{equation}
    \hat{H}_j = \hat{Q}_j^*H_m\hat{Q}_j
    \label{eq:Hhat}
\end{equation}
where $\hat{Q}_j$ is defined in \fref{eq:QhatDefined}.  Because $H_m$ is upper Hessenberg, we can show that $Q_i$ is upper Hessenberg and $\hat{Q}_j$ is properly $j$-Hessenberg.

\begin{thm}
    Let $j$ be a non-negative integer.  A matrix $H$ is called $j$-Hessenberg if $h_{rc} = 0$ whenever $(r-c)>j$.  An Hessenberg matrix is said to be properly $j$-Hessenberg if $h_{rc} \neq 0$ whenever $\left(r-c\right) = j$,
    \begin{equation}
        h_{rc} = \begin{cases}
            0 & r-c > j \\
            x & \text{otherwise}.
        \end{cases}
        \label{eq:hrcDefined}
    \end{equation}
    Then the product of a properly $j$-Hessenberg matrix ($H_j$) and a properly $k$-Hessenberg matrix $H_k$ is properly $(j+k)$-Hessenberg ($H_{j+k}$).
    \label{thm:jkHessenberg}
\end{thm}
\begin{proof}
    The $r$th row of a properly $j$-Hessenberg matrix has $\max\left[r-j-1,0\right]$ leading zeros.  The $c$th column of a properly $k$-Hessenberg matrix has $\max\left[m-\left(c+k\right),0\right]$ trailing zeros. The elements of $H_{j+k}$, $h_{rc}$, are zero if the sum of the leading zeros of the $r$th row of $H_j$ is greater than or equal to the difference of the size of the matrix $m$ and the trailing zeros of the $c$th column of $H_k$; \mbox{$h_{rc} = 0$} if \mbox{$\left( r-j-1 \right) \geq m-\left[m-\left(c+k\right)\right]$}.  Simplifying we obtain \mbox{$\left( r-j-1 \right) \geq \left(c+k\right)$} or equivalently
    \begin{equation}
        h_{rc} = \begin{cases}
            0 & r-c > \left(j+k\right) \\
            x & \text{otherwise}.
        \end{cases}
        \label{eq:hrcProved}
    \end{equation}
    We see that \fref{eq:hrcProved} is equivalent to \fref{eq:hrcDefined} for a $\left(j+k\right)$-Hessenberg matrix.
\end{proof}

When performing the \QR decomposition on an upper Hessenberg matrix we obtain
\begin{equation}
    H_m = Q_1R_1
    \label{eq:HQR}
\end{equation}
with $Q_1$ and $R_1$ defined in \fref{eq:QRDecomposition}.  This can be re-written as
\begin{equation}
    Q_1 = H_mR_1^{-1}
\end{equation}
where we note that the inverse of an upper triangular matrix is an upper triangular matrix.  An upper triangular matrix is properly $0$-Hessenberg and $H_m$ is $1$-Hessenberg so we can apply \fref{thm:jkHessenberg} to show that $Q_1$ is properly $1$-Hessenberg.

\begin{col}
    Let $\hat{Q}_n \in \mathbb{C}^{m \times m}$ be the combined unitary matrix resulting from $n$ iterations of the \QR iteration on an upper Hessenberg matrix, $H$.
    \begin{comment}
        Define a \emph{properly $j$-Hessenberg} $B$ if $b_{ik} = 0$ whenever $\left(i-k\right)>j$ and when $b_{ij} \neq 0$ whenever $\left(i-k\right)=j$.
    \end{comment}
    Then $\hat{Q}_n$ is a properly $n$-Hessenberg matrix and that the row vector $e_m^T\hat{Q}_n$ has $m-n-1$ leading zeros.
    \label{thm:QRHessenberg}
\end{col}

\begin{proof}
    We know that the unitary matrix from the \QR decomposition of an upper Hessenberg matrix is also upper Hessenberg.  $\hat{Q}_n$ is the product of $n$ upper Hessenberg matrices, therefore $\hat{Q}_n$ is properly $n$-Hessenberg.

    The elements of product $e_m^T\hat{Q}_n$ are non-zero when the element of the $m$th or last row of $\hat{Q}_n$ is also non-zero.  The last row of $\hat{Q}_n$ has $m-n-1$ leading zeros as given in \fref{thm:jkHessenberg}.
\end{proof}

Let's return to the Arnoldi method.  Solving \fref{eq:Hhat} for $H_m$ and substituting into \fref{eq:ArnoldiFactorizationIRAM} we obtain
\begin{align}
    A V_m &= V_m\left(\hat{Q}_j\hat{H}_j\hat{Q}_j^*\right) + v_{m+1}h_{m+1,m}e_m^T. \\
    \intertext{Now operate on the right by $\hat{Q}_j$,}
    \begin{split}
        A V_m\hat{Q}_j &= V_m\hat{Q}_j\hat{H}_j\hat{Q}_j^*\hat{Q}_j + v_{m+1}h_{m+1,m}e_m^T\hat{Q}_j \\
        A \hat{V}_m &= \hat{V}_m\hat{H}_m + v_{m+1}h_{m+1,m}e_m^T\hat{Q}_j, \label{eq:UpdatedIRAM}
    \end{split}
\end{align}
where
\begin{equation}
    \hat{V}_m = V_m\hat{Q}_j.
\end{equation}

The row vector \mbox{$e_m^T\hat{Q}_j$} in \fref{eq:UpdatedIRAM} has \mbox{$\left( m-j-1 \right)$} leading zeros according to \fref{thm:QRHessenberg}.  If we drop the first $j$ columns of \fref{eq:UpdatedIRAM} we obtain
\begin{equation}
    \begin{split}
        A \hat{V}_k &= \hat{V}_{k+1}\hat{H}_{k+1,k} + v_{m+1}h_{m+1,m}\beta e_k^T \\
        &= \hat{V}_k\hat{H}_k + \left( \check{v}_{k+1}\check{h}_{k+1,k} + v_{m+1}h_{m+1,m}\beta \right) e_k^T
    \end{split}
    \label{eq:IRAMcheck}
\end{equation}
where $\beta e_k^T$ is the first $k+1$ columns of $e_m^T\hat{Q}_j$ and we have defined
\begin{align}
    \hat{v}_{k+1} &= \gamma\left(\check{v}_{m+1}\check{h}_{m+1,m} + v_{m+1}h_{m+1,m}\beta \right)
\end{align}
where $\gamma$ is chosen to normalize $\hat{v}_{k+1}$, $\|\hat{v}_{k+1}\|_2 = 1$.  If we let $\hat{h}_{k+1,k} = 1/\gamma$ then \fref{eq:IRAMcheck} becomes
\begin{equation}
    A \hat{V}_k = \hat{V}_k\hat{H}_k + \hat{v}_{k+1}\hat{h}_{k+1,k} e_k^T
    \label{eq:IRAMhat}
\end{equation}
which is exactly like \fref{eq:ArnoldiFactorizationIRAM} except we have added some hats on some symbols and replaced $m$ with $k$.  It can be shown \citep[see][]{Watkins:2002Funda-0} that the Arnoldi vectors contained as the columns of $\hat{V}_k$ are exactly those that would have been generated by explicitly starting with $\hat{v}_1$.  Thus with IRAM we don't have have to start at the beginning of a restart, therefore we can jump right in at the $k$th iteration of the restart.  After $j$ additional steps we will have a Krylov subspace of size $m$, exactly as we would have had after $m$ iterations in explicit Arnoldi.

Being able to jump into the middle of an Arnoldi restart can save considerable computational expense by reducing the number applications of the linear operator A required.  (The application of the linear operator in Monte Carlo particle transport is responsible for > 80\% of the computational runtime.)  In short, IRAM exchanges Arnoldi iterations for shifted \QR iterations.  Shifted \QR iterations are faster than Arnoldi iterations because $H_m$ is small.

\section*{How Implicit Restarts Suppresses Unwanted Eigenvalue Information}
We have just shown that implicit restarts can be computationally more efficient.  Here we show that implicit restarts are mathematically equivalent to restarting with a linear combination of the Ritz vectors from the desired region of the spectrum of $A$ \citep[see][Chapter~5,~pg.~456]{Watkins:2002Funda-0}.

\begin{thm}\label{thm:ArnoldiPolynomial}
    Suppose we have the Arnoldi factorization
    \begin{equation}
        AV_m = V_mH_m + v_{m+1}h_{m+1,m}e_m^T
    \end{equation}
    and let $\p_j$ be a polynomial of degree $j < m$ as shown in \fref{eq:Polynomial}.  Then
    \begin{equation}
        \poly{j}{A}V_m = V_m\poly{j}{H_m} + E_j,
        \label{eq:PolyAPolyHDefined}
    \end{equation}
    where $E_j \in \mathbb{C}^{n \times m}$ is identically zero, except in the last $j$ columns.
\end{thm}

\begin{proof}
For $m=1$ we can just apply a shift $\nu_1$ to the Arnoldi factorization, \fref{eq:ArnoldiFactorizationIRAM},
\begin{equation}
    \left(A-\nu_1 I\right)V_m = V_m\left(H_m - \nu_1 I\right) + E_1
    \label{eq:ArnoldiPolynomialDegree1}
\end{equation}
where $E_1 = h_{m+1,m}v_{m+1}e_m^T$.  We can see that $E_1$ is zero except for the last column by the product $ v_{m+1}e_m^T$.  $\left(A-\nu_1 I\right)$ and $\left(H_m - \nu_1 I\right)$ are polynomials of $A$ and $H_m$ respectively with degree $j=1$, both with root $\nu_1$.

Assuming this theorem holds for polynomials of degree $j-1$ (we have just shown this to be true for $j-1=1$) we can now show it is valid for polynomials of degree $j$.  We know:
\begin{equation}
        \left(A-\nu_{j-1}I\right)\cdots\left(A-\nu_1I\right)V_m = V_m\left(H_m-\nu_{j-1}I\right)\cdots\left(H_m-\nu_1I\right) + E_{j-1}
\end{equation}
operate on the left by $\left( A-\nu_jI \right)$
\begin{multline}
    \left(A-\nu_jI\right)\left(A-\nu_{j-1}I\right)\cdots\left(A-\nu_1I\right)V_m =  \\
    \left(A-\nu_jI\right)V_m\left(H_m-\nu_{j-1}I\right)\cdots\left(H_m-\nu_1I\right) + \left(A-\nu_jI\right)E_{j-1}.
\end{multline}
Substituting $\nu_j$ for $\nu_1$ in \fref{eq:ArnoldiPolynomialDegree1} we see that
\begin{equation}
    \left(A-\nu_j I\right)V_m = V_m\left(H_m - \nu_j I\right) E_1
    \label{eq:ArnoldiPolynomialDegreej}
\end{equation}
which can be inserted into the previous equation
\begin{align}
    \lefteqn{\left(A-\nu_jI\right)\cdots\left(A-\nu_1I\right)V_m} \qquad \nonumber \\
    \begin{split}
    &= \left[ \left(A-\nu_jI\right)V_m \right]\left(H_m-\nu_{j-1}I\right)\cdots\left(H_m-\nu_1I\right) + \left(A-\nu_jI\right)E_{j-1} \\
     &= \left[ V_m\left(H_m-\nu_jI\right)+E_1 \right]\left(H_m-\nu_{j-1}I\right)\cdots\left(H_m-\nu_1I\right) + \left(A-\nu_jI\right)E_{j-1} \\
     &= V_m\left(H_m-\nu_jI\right)\cdots\left(H_m-\nu_1I\right) + E_1\left(H_m - \nu_{j-1}I\right)\cdots\left(H_m - \nu_1I\right) \\
     &\qquad + \left(A-\nu_jI\right)E_{j-1} \\
     &= V_m\left(H_m-\nu_jI\right)\cdots\left(H_m-\nu_1I\right) + E_j
     \label{eq:PolyAPolyHProof}
    \end{split}
\end{align}
where
\begin{equation}
    \label{eq:Ej}
    E_j = E_1\left(H_m - \nu_{j-1}I\right)\cdots\left(H_m - \nu_1I\right) + \left(A-\nu_jI\right)E_{j-1}.
\end{equation}
We can simplify \fref{eq:PolyAPolyHProof} further and write
\begin{equation}
    \poly{j}{A}V_m = V_m\poly{j}{H_m} + E_j,
    \label{eq:PolyAPolyHProved}
\end{equation}
where \[\poly{j}{A} = \left(A-\nu_jI\right)\cdots\left(A-\nu_1I\right)\] is a polynomial of degree $j$ on $A$ and \[\poly{j}{H_m} = \left(H_m-\nu_jI\right)\cdots\left(H_m-\nu_1I\right)\] is a polynomial of degree $j$ on $H_m$.  Equation \eqref{eq:PolyAPolyHProved} is exactly what we want to prove \fref{eq:PolyAPolyHDefined}.

We know that the columns of $E_{j-1}$ are zero except the last $j-1$ columns.  We can see by inspection that the second term on the right hand side of \fref{eq:Ej} has the same structure.  From \fref{thm:jkHessenberg} we know that the product $\left(H_m - \nu_{j-1}I\right)\cdots\left(H_m - \nu_1I\right)$ is properly $j$-Hessenberg.  Multiply this by $E_1$ on the left and we obtain a zero matrix except in the last $j$ columns.  The right hand side of \fref{eq:Ej} is therefore as we expected.
\end{proof}

Now that we have the necessary mathematical basis to understand what happens with an implicit Arnoldi restart we can investigate how IRAM generates it's starting vector.    In IRAM the shifts $\nu_1$, $\nu_2$, \ldots, $\nu_j$ are chosen from the region of the eigenvalue spectrum that is to be suppressed.  These shifts are then used in $j$ iterations of the shifted \QR algorithm on $H_m$ resulting in $\hat{H}_j = \hat{Q}_j^*H_m\hat{Q}_j$ where $\hat{Q}_j$ is the combined unitary factor in the \QR factorization
\begin{equation}
    \poly{j}{H_m} = \hat{Q}_j\hat{R}_j
    \label{eq:HmPolynomial}
\end{equation}
where $j$ indicates the number of shifted \QR iterations performed and $\poly{j}{z}$ is defined in \fref{eq:Polynomial}.  When we apply \fref{thm:ArnoldiPolynomial} and substitute \fref{eq:HmPolynomial} we obtain
\begin{equation}
    \poly{j}{A}V_m = V_m\hat{Q}_j\hat{R}_m + E_j.
    \label{eq:PolyArnoldiFactAllColumns}
\end{equation}
The Arnoldi factorization is uniquely defined by the starting vector and the linear operator.  If we multiply \fref{eq:PolyArnoldiFactAllColumns} by $e_1$ we get
\begin{equation}
    \poly{j}{A}V_me_1 = V_m\hat{Q}_j\hat{R}_je_1 + E_je_1.
    \label{eq:PolyArnoldiFactColumn1}
\end{equation}
$E_je_1 = 0$ because the first column of $E_j$ is zero.  Since $\hat{R}_j$ is upper triangular \mbox{$\hat{R}_je_1 = r_{11}e_1 = \alpha e_1$} and the first term on the right hand side is \mbox{$\alpha V_m\hat{Q}_m = \alpha \hat{v}_1$}.  The left hand side of \fref{eq:PolyArnoldiFactColumn1} is just a polynomial of degree $j$ of $A$ multiplied by $v_1$ the starting vector for this Arnoldi process.  From \fref{eq:PolyArnoldiFactColumn1} we can deduce
\begin{equation}
    \hat{v}_1 = \frac{1}{\alpha}\poly{j}{A}v_1.
    \label{eq:IRAMRestartVector}
\end{equation}

Let's return to the starting vector for an Arnoldi procedure.  We can write the vector as a linear combination of a set of basis vectors
\begin{equation}
    v_1 = c_1x_1 + c_2x_2 + \cdots + c_nx_n,
\end{equation}
where the $x_i$'s are the basis vectors and the $c_i$'s are some expansion coefficients.  If we choose the eigenvectors of our linear operator $A$ as our basis vectors then things become interesting with respect to IRAM.  To see this, multiply $v_1$ by a polynomial of degree $1$ of $A$
\begin{equation}
    \begin{split}
        \poly{1}{A}v_1 &= \left(A-\nu_1I\right)\left(c_1x_1 + c_2x_2 + \cdots + c_nx_n\right) \\
         &= c_1\left(Ax_1 - \nu_1x_1\right) + \cdots c_n\left(Ax_n - \nu_1x_n\right)
    \end{split}
        \label{eq:PolyStartingVectorTerm1}
\end{equation}
where we remember the $x_i$'s are the eigenvectors of $A$ and that $\nu_1$ is the first undesired eigenvalue, $\lambda_{k+1}$.  Equation \eqref{eq:PolyStartingVectorTerm1} becomes
\begin{multline}
    \poly{1}{A}v_1 = c_1\left(\lambda_1x_1 - \nu_1x_1\right) + \cdots \\
    +c_{k+1}\underbrace{\left(\lambda_{k+1}x_{k+1} - \nu_1x_{k+1}\right)}_{=0} + \\
    \cdots + c_n\left(\lambda_nx_n - \nu_1x_n\right)
\end{multline}
where the vector $x_{k+1}$ has been completely eliminated.

If we include all the terms of the polynomial $\poly{j}{A}$ then all $j$ eigenvectors from the undesired region of the spectrum are removed from our restart vector
\begin{multline}
    \poly{j}{A}v_1 = c_1\left(\lambda_1x_1 - \nu_1x_1\right)\left(\lambda_1x_1 - \nu_2x_1\right)\cdots\left(\lambda_1x_1 - \nu_jx_1\right) + \cdots \\
    + c_k\left(\lambda_kx_k - \nu_kx_k\right)\cdots\left(\lambda_kx_k - \nu_kx_k\right) + 0.
\end{multline}
We can plug this result into \fref{eq:IRAMRestartVector} and get our optimal restart vector as described in \fref{eq:RestartVector}.

Thus we see that implicitly restarted Arnoldi's method is mathematically equivalent to explicitly restarted Arnoldi's method but can skip into the restart after $k$ iterations.  IRAM can save computational expense by trading expensive applications of the linear operator for cheap shifted \QR iterations.

%\section*{Monte Carlo Implementation} \label{sec:MCIRAM}


\subsection{Dynamic Time Warping}
\label{dtw}

The question of how to take the difference between the demand curve and
the production curve is an important one. The na\"ive option is to simply
take the $L_1$ norm of the difference between these two time series, as
seen in Equation \ref{delta-l1}.  However, since the $g(t, \Theta)$ computed
from a simulation is expensive, any operation that can meaningfully
exacerbate the difference between time series helps drive down the number
of optimization iterations.

Dynamic time warping is just such a mechanism. It computes
a distance between any two time series which compounds the separation
between the two. Additionally, the time series are not required to be of the
same length, though for optimization purposes there is no reason for them
not to be. DTW gives a measure of the amount that one time series would need to
be warped to become the other time series. It is, therefore, a holistic
measure that operates over all times. Dynamic time warping
is more fully covered in \cite{muller}.  However, an
optimization-relevant introduction is given here.

For the time series $f$ and $g$, there are three parts to dynamic time
warping. The first is the distance $d$, which will be minimized. The second
is a cost matrix $C$ that helps compute $d$ by indicating how far a point
on $f$ is from another point on $g$. Thirdly, the warp path $u$ is the
minimal cost curve through the $C$ matrix from the fist point in time to
the last. The DTW distance can thus be interpreted as the
total cost of traveling the warp path.

The first step in computing a dynamic time warp distance is to
assemble the cost matrix. Say that the demand time series $f$ has
length $A$ indexed by $a$, and the production time series $g$ has
length $B$ indexed by $b$. For the optimization problem here, $A$ and $B$
are in practice both equal to $T$.  However, it is useful to have $a$ and
$b$ index the two time series separately. Now denote an $A\times B$ matrix
$\Delta L$ as the $L_1$ norm of the difference between $f$ and $g$:
\begin{equation}
\label{delta-l1}
\Delta L_{a,b} = \left|f(a) - g(b, \Theta)\right|_1
\end{equation}
Since $\Delta L$ uses the $L_1$ norm, $f$ and $g$ may return vector
data. This enables multi-objective optimization. However it is recommended
that the components of the $f$ and $g$ are weighted, normalized, or otherwise
directly comparable.

The cost matrix $C$ may now be defined as the $A\times B$ sized matrix
which follows the recursion relations seen in Equation \ref{cost-matrix}.
\begin{equation}
\label{cost-matrix}
\begin{split}
C_{1,1} & = \Delta L_{1,1}\\
C_{1,b+1} & = \Delta L_{1,b} + C_{1,b}\\
C_{a+1,1} & = \Delta L_{a,1} + C_{a,1}\\
C_{a+1,b+1} & = \Delta L_{a,b} + \min\left[C_{a,b}, C_{a+1,b}, C_{a,b+1}\right]
\end{split}
\end{equation}
The boundary conditions above are the same as setting an infinite cost to
any $a \le 0$ or $b \le 0$. The cost matrix $C$ has the same units as the
demand curve. However, the scale of $C$ is
larger than the demand,
except for in the fiducial case. This is because the cost matrix compounds the
minimum value of previous entries.

Knowing a cost matrix, the warp path can be computed by traversing the
matrix backwards from the $(A, B)$ corner to the $(1, 1)$ corner.
If the length of the warp is $I$ indexed by $i$, the warp path itself
can be thought of as a sequence of coordinate points $u_i$. For a given
point $u_i$ in the warp path, the previous point $u_{i-1}$ may be found by
picking the minimum cost point among the locations one column over $(a,b-1)$,
one row over $(a-1,b)$, and one previous diagonal element to $(a-1,b-1)$.
Equation \ref{warp-path} expresses this mathematically.
\begin{equation}
\label{warp-path}
u_{i-1} = \argmin\left[C_{a-1,b-1}, C_{a-1,b}, C_{a,b-1}\right]
\end{equation}
The maximum possible length of $u$ is thus $\max(I) = A + B$.
The minimum possible length, though, is $\min(I) = \sqrt{A^2 + B^2}$.

The dynamic time warping distance distance $d$ can now be stated as the
cost of the final entry of the warp path normalized by the maximum possible
length of the warp path.
\begin{equation}
\label{d-calc-ab}
d(f, g) = \frac{C_{A,B}}{A + B}
\end{equation}
However, because the demand curve and the production curve
are often defined on the same time grid, $d$ can be further
reduced to the following:
\begin{equation}
\label{d-calc}
d(f, g) = \frac{C_{T,T}}{2T}
\end{equation}
Therefore, $d$ has the same units as the demand curve, production curve,
and cost matrix.

As an example, take a 1\% growth that starts with 90 GWe in the year
2016 as the demand curve. Then consider a production curve that
under-produces the demand by 5\% for 25 years before switching to
over-producing this curve by 5\% for the next 25 years.
Figure \ref{cost-demand-to-production} shows the dynamic time warping
cost matrix between these two time series as a heat map.

\begin{figure}[htb]
\centering
\includegraphics[width=0.9\textwidth]{cost-demand-to-production.eps}
\caption{Heat map of the cost matrix between a 1\% growth demand curve and
a production curve the under produces by 5\% for the first 25 years and then
over produces for the second 25 years.
The warp path $u$ is superimposed as the white curve on top of the
cost matrix.}
\label{cost-demand-to-production}
\end{figure}

Additionally, the warp path between the example demand and production
curves is presented as the white curve on top of the heat map in
Figure \ref{cost-demand-to-production}.
Recognize that $u$ is monotonic along both time axes. Furthermore, the precise
path of $u$ minimizes the cost matrix at every step. Regions of increased
cost in the cost matrix can be seen to repel the warp path. The
distance $d$ between the demand and production curves here happens
to be 0.756 GWe.

Dynamic time warping distance can therefore be used as an objective function
to minimize for any demand and production curves. However, using full
simulations to find $g(t, \Theta)$ remains expensive, even though DTW itself
is computationally cheap. Therefore, a mechanism to reduce the overhead
from production curve evaluation is needed.


\chapter{Milestone 4}
\section{Force-Derivation from the Lenard Jones Potential}

\begin{comment}
1. Put the python code here
2. write the base equations (lj + f)
3. write the result form the python code
4. write some text
5. c++ code that results from this?
\end{comment}
%% equations
\begin{equation}
	\overrightarrow{f_{k}} = \sum_{i}^{}\frac{\partial V}{\partial r_{ik}} \hat{r_{ik}}
\end{equation}

\begin{equation}
	V(r) = 4\epsilon\bigg[\Big(\frac{\sigma}{r}\Big)^{12}- \Big(\frac{\sigma}{r}\Big)^{6} \bigg]
\end{equation}

\begin{equation}
	\frac{\partial V}{\partial r_{ik}} = 4\epsilon\Bigg(\frac{6\sigma^{6}}{r_{ik}^{6}} - \frac{12\sigma^{12}}{r_{ik}^{13}} \Bigg)
\end{equation}

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{4}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{k+kn}{import} \PY{n+nn}{sympy} \PY{k}{as} \PY{n+nn}{sp}
\PY{k+kn}{import} \PY{n+nn}{warnings}
\PY{n}{warnings}\PY{o}{.}\PY{n}{filterwarnings}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{ignore}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}
\PY{n}{sp}\PY{o}{.}\PY{n}{init\PYZus{}printing}\PY{p}{(}\PY{p}{)}
\PY{n}{eps} \PY{o}{=} \PY{n}{sp}\PY{o}{.}\PY{n}{Symbol}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{e}\PY{l+s+s2}{\PYZdq{}}\PY{p}{)}
\PY{n}{sig} \PY{o}{=} \PY{n}{sp}\PY{o}{.}\PY{n}{Symbol}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{s}\PY{l+s+s2}{\PYZdq{}}\PY{p}{)}
\PY{n}{rad} \PY{o}{=} \PY{n}{sp}\PY{o}{.}\PY{n}{Symbol}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{r}\PY{l+s+s2}{\PYZdq{}}\PY{p}{)}
\PY{n}{energyRad} \PY{o}{=} \PY{l+m+mi}{4} \PY{o}{*} \PY{n}{eps} \PY{o}{*} \PY{p}{(}\PY{p}{(}\PY{n}{sig}\PY{o}{/}\PY{n}{rad}\PY{p}{)}\PY{o}{*}\PY{o}{*}\PY{l+m+mi}{12} \PY{o}{\PYZhy{}} \PY{p}{(}\PY{n}{sig}\PY{o}{/}\PY{n}{rad}\PY{p}{)}\PY{o}{*}\PY{o}{*}\PY{l+m+mi}{6}\PY{p}{)}
\PY{n}{energyRad}\PY{o}{.}\PY{n}{diff}\PY{p}{(}\PY{n}{rad}\PY{p}{)}
\end{Verbatim}
\end{tcolorbox}
\prompt{Out}{outcolor}{4}{}

    $\displaystyle 4 e \left(\frac{6 s^{6}}{r^{7}} - \frac{12 s^{12}}{r^{13}}\right)$

%%

\section{Different Time Steps}
%plots for different timesteps
\begin{comment}
- ploted for different timesteps to see diffrent results
- all were stable expect the 0.03 which crashed the program while returning (SIGABRT)
- discuss the curves
- in the end choose the 0.01 timestep which in my Opinion was the best tradeoff between
	accuracy and time spent simulating
- maybe also just plot them in one but will have tho smooth it out in this case
\end{comment}
\begin{figure}[!h]
	\begin{center}
		\includegraphics[scale=1]{Figure/plot_001.png}
	\end{center}
	\caption[Simulation]{Simulation with a time step of 0.001}
	\label{Plot001}
\end{figure}
\begin{figure}[!h]
	\begin{center}
		\includegraphics[scale= 1]{Figure/plot_005.png}
	\end{center}
	\caption[Simulation]{Simulation with a time step of 0.005}
	\label{Plot005}
\end{figure}
\begin{figure}[!h]
	\begin{center}
		\includegraphics[scale=1]{Figure/plot_01.png}
	\end{center}
	\caption[Simulation]{Simulation with a time step of 0.01}
	\label{Plot01}
\end{figure}
\begin{figure}[!h]
	\begin{center}
		\includegraphics[scale= 1]{Figure/plot_02.png}
	\end{center}
	\caption[Simulation]{Simulation with a time step of 0.02 }
	\label{Plot02}
\end{figure}
\begin{figure}[!h]
	\begin{center}
		\includegraphics[scale= 1]{Figure/plot_03.png}
	\end{center}
	\caption[Simulation]{Simulation with a time step of 0.03 }
	\label{Plot03}
\end{figure}
\begin{figure}[!h]
	\begin{center}
		\includegraphics[scale= 1]{Figure/plot_04.png}
	\end{center}
	\caption[Simulation]{Simulation with a time step of 0.04 }
	\label{Plot04}
\end{figure}
%Snapshots of the simulation (5)
\section{Simulation Snapshots}
\begin{comment}
- not sure if i just use 2
- background kinda bad?!
\end{comment}

Reworked the graphs allready in the Discrition
\begin{figure}[!h]
	\begin{center}
		\includegraphics[scale= 1]{Figure/1Image.png}
	\end{center}
	\caption[Simulation]{Simulation }
	\label{Simulation1}
\end{figure}
\begin{figure}[!h]
	\begin{center}
		\includegraphics[scale=1]{Figure/2Image.png}
	\end{center}
	\caption[Simulation]{Simulation}
	\label{Simulation2}
\end{figure}
\begin{figure}[!h]
	\begin{center}
		\includegraphics[scale= 1]{Figure/3Image.png}
	\end{center}
	\caption[Simulation]{Simulation }
	\label{Simulation3}
\end{figure}
\begin{figure}[!h]
	\begin{center}
		\includegraphics[scale= 1]{Figure/4Image.png}
	\end{center}
	\caption[Simulation]{Simulation }
	\label{Simulation4}
\end{figure}
\begin{figure}[!h]
	\begin{center}
		\includegraphics[scale= 1]{/home/cm/CLionProjects/MDCode/AData//totalEnergyDrift.png}
	\end{center}
	\caption[Simulation]{Simulation }
	\label{Simulation5}
\end{figure}


\chapter{Curvilinear Coordinates}
\noindent
You should already know about Cartesian $(x,y)$ coordinates and polar $(r,\theta)$ coordinates in 2D. Cartesian extends to 3D as $(x,y,z)$, but there are multiple ways to extend polar coordinates into 3D.\\

\input{./curvilinearCoordinates/reviewPolarCoordinates}
\input{./curvilinearCoordinates/cylindricalCoordinates}
\input{./curvilinearCoordinates/sphericalCoordinates}


\lab{Visualizing Complex-valued Functions}{Visualizing Complex-valued Functions}
\objective{Functions that map from the complex plane into the complex plane are difficult to fully visualize because the domain and range are both $2$-dimensional.
However, such functions can be visualized at the expense of partial information.
In this lab we present methods for analyzing complex-valued functions visually, including locating their zeros and poles in the complex plane.
We recommend completing the exercises in a Jupyter Notebook.}

\section*{Representations of Complex Numbers} % ===============================

A complex number $z = x+iy$ can be written in \emph{polar coordinates} as $z = re^{i\theta}$ where
\begin{itemize}
\item $r = |z| = \sqrt{x^2+y^2}$ is the \emph{magnitude} of $z$, and
\item $\theta = \arg(z) = \arctan(y/x)$ is the \emph{argument} of $z$, the angle in radians between $z$ and $0$.
\end{itemize}
Conversely, Euler's formula is the relation $re^{i\theta} = r\cos(\theta) + ir\sin(\theta)$.
Then setting $re^{i\theta}=x+iy$ and equating real and imaginary parts yields the equations $x=r\cos(\theta)$ and $y=r\sin(\theta)$.

\begin{figure}[H]
\begin{tikzpicture}[dot/.style={circle,fill=black,minimum size=3pt,
    inner sep=0pt,outer sep=-1pt}, >=stealth', thick, xscale=1.4]

\draw[-](-.5,0)--(3,0);
\draw[-](0,-.5)--(0,3);

\draw[-, dashed, gray, anchor=east](2.6,2.3)--(0,2.3);
\node[draw=none]()at(-.3,2.3){$iy$};
\draw[-, dashed, gray, anchor=north ](2.6,2.3)--(2.6,0);
\node[draw=none]()at(2.6,-.3){$x$};

\draw[gray](.75,0) arc (0:45:.7 and .7);

\draw[-](0,0)--(2.6,2.3);
\node[dot,draw](point)at(2.6,2.3){};
\node[draw=none]()at(2.8,2.5){$z$};
\node[draw=none]()at(.9,.35){$\theta$};

\draw [decorate,decoration={brace,amplitude=10pt},rotate=-45, gray] (0,0) --(.2,3.45);
\node[draw=none]()at(1.05,1.65){$r$};

\end{tikzpicture}
\caption{The complex number $z$ can be represented in Cartesian coordinates as $z = x+iy$ and in polar coordinates as $z = re^{i\theta}$, when $\theta$ is in radians.}
\label{fig:polar_coords}
\end{figure}

NumPy makes it easy to work with complex numbers and convert between coordinate systems.
The function \li{np.angle()} returns the argument $\theta$ of a complex number (between $-\pi$ and $\pi$) and \li{np.<<abs>>()} (or \li{np.absolute()}) returns the magnitude $r$.
These functions also operate element-wise on NumPy arrays.

\begin{lstlisting}
>>> import numpy as np

>>> z = 2 - 2*1j                    # 1j is the imaginary unit i = sqrt(-1).
>>> r, theta = np.<<abs>>(z), np.angle(z)
>>> print(r, theta)                 # The angle is between -pi and pi.
2.82842712475 -0.785398163397

# Check that z = r * e^(i*theta)
>>> np.isclose(z, r*np.exp(1j*theta))
<<True>>

# These function also work on entire arrays.
>>> np.<<abs>>(np.arange(5) + 2j*np.arange(5))
array([ 0.        ,  2.23606798,  4.47213595,  6.70820393,  8.94427191])
\end{lstlisting}

\section*{Complex Functions} % ================================================

A function $f: \mathbb{C} \rightarrow \mathbb{C}$ is called a \emph{complex-valued function}.
Visualizing $f$ is difficult because $\mathbb{C}$ has 2 real dimensions, so the graph of $f$ should be 4-dimensional.
However, since it is possible to visualize 3-dimensional objects, $f$ can be visualized by ignoring one dimension.
There are two main strategies for doing this: assign a color to each point $z\in\mathbb{C}$ corresponding to either the argument $\theta$ of $f(z)$, or to the magnitude $r$ of $f(z)$.
The graph that uses the argument is called a \emph{complex color wheel graph}.
Figure \ref{fig:complex-identity-angle-mag} displays the identity function $f(z) = z$ using these two methods.

\begin{figure}[H] % f(z) = z
\captionsetup[subfigure]{justification=centering}
\centering
\begin{subfigure}{.49\textwidth}
    \centering
    \includegraphics[width=\textwidth]{figures/identity_angle.png}
\end{subfigure}
%
\begin{subfigure}{.49\textwidth}
    \centering
    \includegraphics[width=\textwidth]{figures/identity_magnitude.png}
\end{subfigure}
\caption{The identity function $f: \mathbb{C} \rightarrow \mathbb{C}$ defined by $f(z)=z$. On the left, the color at each point $z$ represents the angle $\theta = \arg(f(z))$. As $\theta$ goes from $-\pi$ to $\pi$, the colors cycle smoothly counterclockwise from white to blue to red and back to white (this colormap is called \li{"twilight"}). On the right, the color represents the magnitude $r = |f(z)|$. The further a point is from the origin, the greater its magnitude (the colormap is the default, \li{"viridis"}).
}
\label{fig:complex-identity-angle-mag}
\end{figure}

The plots in Figure \ref{fig:complex-identity-angle-mag} use Cartesian coordinates in the domain and polar coordinates in the codomain.
The procedure for plotting in this way is fairly simple.
Begin by creating a grid of complex numbers: create the real and imaginary parts separately, then use \li{np.meshgrid()} to turn them into a single array of complex numbers.
Pass this array to the function $f$, compute the angle and argument of the resulting array, and plot them using \li{plt.pcolormesh()}.
The following code sets up the complex domain grid.

\begin{lstlisting}
>>> x = np.linspace(-1, 1, 400)     # Real domain.
>>> y = np.linspace(-1, 1, 400)     # Imaginary domain.
>>> X, Y = np.meshgrid(x, y)        # Make grid matrices.
>>> Z = X + 1j*Y                    # Combine the grids into a complex array.
\end{lstlisting}

Visualizing the argument and the magnitude separately provides different perspectives of the function $f$.
The angle plot is generally more useful for visualizing function behavior, though the magnitude plot often makes it easy to spot important points such as zeros and poles.
% Use both of these plots together to get as much information as possible about the function.

\begin{figure}[H] % f(z) = sqrt(z^2 - 1)
\captionsetup[subfigure]{justification=centering}
\centering
\begin{subfigure}{.49\textwidth}
    \centering
    \includegraphics[width=\textwidth]{figures/discontinuity_angle.png}
\end{subfigure}
%
\begin{subfigure}{.49\textwidth}
    \centering
    \includegraphics[width=\textwidth]{figures/discontinuity_magnitude.png}
\end{subfigure}
\caption{Plots of $f(z) = \sqrt{z^2+1}$ on $\{x+iy \mid x,y \in [-3,3]\}$.
Notice how a discontinuity is clearly visible in the angle plot on the left, but disappears from the magnitude plot on the right.}
\label{fig:complex-discontinuity}
\end{figure}

\begin{problem}
Write a function that accepts a function $f:\mathbb{C}\rightarrow\mathbb{C}$,  bounds $[r_{\text{min}},r_{\text{max}},i_{\text{min}},i_{\text{max}}]$ for the domain, an integer \li{res} that determines the resolution of the plot, and a string to set the figure title.
Plot $\arg(f(z))$ and $|f(z)|$ on an equally-spaced \li{res}$\times$\li{res} grid over the domain $\{x + iy \mid x \in [r_{\text{min}},r_{\text{max}}],\: y \in [i_{\text{min}},i_{\text{max}}]\}$ in separate subplots.
\begin{enumerate}
\item For $\arg(f(z))$, set the \li{plt.pcolormesh()} keyword arguments \li{vmin} and \li{vmax} to $-\pi$ and $\pi$, respectively.
This forces the color spectrum to work well with \li{np.angle()}.
Use the colormap \li{"twilight"}, which starts and ends white, so that the color is the same for $-\pi$ and $\pi$.

\item For $|f(z)|$, set \li{norm=matplotlib.colors.LogNorm()} in \li{plt.pcolormesh()} so that the color scale is logarithmic.
Use a sequential colormap like \li{"viridis"} or \li{"magma"}.

\item Set the aspect ratio to \li{"equal"} in each plot.
Give each subplot a title, and set the overall figure title with the given input string.
\end{enumerate}

Use your function to visualize $f(z) = z$ on $\{x+iy \mid x,y \in [-1,1]\}$ and $f(z) = \sqrt{z^2+1}$ on $\{x+iy \mid x,y \in [-3,3]\}$.
Compare the resulting plots to Figures \ref{fig:complex-identity-angle-mag} and \ref{fig:complex-discontinuity}, respectively.
\label{prob:complex-plotting-function}
\end{problem}

\section*{Interpreting Complex Plots} % =======================================

Plots of a complex function can be used to quickly identify important points in the function's domain.

\subsection*{Zeros} % ---------------------------------------------------------

A complex number $z_0$ is called a \emph{zero} of the complex-valued function $f$ if $f(z_0) = 0$.
% $z_0$ is also called a \emph{root} of the equation $f(z) = 0$.
The \emph{mutliplicity} or \emph{order} of $z_0$ is the largest integer $n$ such that $f$ can be written as $f(z) = (z - z_0)^n g(z)$ where $g(z_0) \ne 0$.
% In this case, $z_0$ is called a \emph{zero of order $n$} of $f$.
In other words, $f$ has a zero of order $n$ at $z_0$ if the Taylor series of $f$ centered at $z_0$ can be written as
\[
f(z) = \sum_{k=n}^{\infty} a_k(z-z_0)^k, \qquad  a_n \neq 0.
\]

Angle and magnitude plots make it easy to locate a function's zeros and to determine their multiplicities.

\begin{problem} % Plot zeros.
Use your function from Problem \ref{prob:complex-plotting-function} to plot the following functions on the domain $\{x+iy \mid x,y \in [-1,1]\}$.
\begin{itemize}
\item $f(z) = z^n$ for $n=2,3,4$.
\item $f(z) = z^3 - iz^4 - 3z^6$.
Compare the resulting plots to Figure \ref{fig:complex-zeros}.
\end{itemize}
Use a Markdown cell to write a sentence or two about how the zeros of a function and their multiplicity appear in angle and magnitude plots.
\label{prob:plot-complex-zeros}
\end{problem}

% In other words, $f(z) = a_n(z-z_0)^n + a_{n+1}(z-z_0)^{n+1} + \ldots$.
% This explains why we can estimate the order of a zero by counting the number of times the colors circle a point (see Figure \ref{fig:complex-zeros}).

Problem \ref{prob:plot-complex-zeros} shows that in an angle plot of $f(z) = z^n$, the colors cycle $n$ times counterclockwise around 0.
This is explained by looking at $z^n$ in polar coordinates,
\[
z^n = (re^{i \theta})^n = r^n e^{i(n\theta)}.
\]
Multiplying $\theta$ by a number greater than $1$ compresses the graph along the ``$\theta$-axis'' by a factor of $n$.
In other words, the output angle repeats itself $n$ times in one cycle of $\theta$.
This is similar to taking a scalar-valued function $f:\mathbb{R}\rightarrow\mathbb{R}$ and replacing $f(x)$ with $f(nx)$.

Problem \ref{prob:plot-complex-zeros} also shows that the plot of $f(z) = z^3 - iz^4 - 3z^6$ looks very similar to the plot of $f(z) = z^3$ near the origin.
This is because when $z$ is close to the origin, $z^4$ and $z^6$ are much smaller in magnitude than $z^3$, and so the behavior of $z^3$ dominates the function.
In terms of the Taylor series centered at $z_0 = 0$, the quantity $|z-z_0|^{n+k}$ is much smaller than $|z-z_0|^n$ for $z$ close to $z_0$, and so the function behaves similar to $a_n(z-z_0)^n$.

\begin{figure}[H] % f(z) = z^3 - iz^4 - 3z^6
\captionsetup[subfigure]{justification=centering}
\centering
\begin{subfigure}{.49\textwidth}
    \centering
    \includegraphics[width=\textwidth]{figures/zeros_angle.png}
\end{subfigure}
%
\begin{subfigure}{.49\textwidth}
    \centering
    \includegraphics[width=\textwidth]{figures/zeros_magnitude.png}
\end{subfigure}
\caption{The angle plot of $f(z)=z^3 - iz^4 - 3z^6$ on $\{x+iy \mid x,y \in [-1,1]\}$. The angle plot shows that $f(z)$ has a zero of order $3$ at the origin and $3$ distinct zeros of order $1$ scattered around the origin.
The magnitude plot makes it easier to pinpoint the location of the zeros.}
% and shows that the basin around the origin is a little wider than the basins around the other zeros.}
\label{fig:complex-zeros}
\end{figure}

\subsection*{Poles} % ---------------------------------------------------------

A complex number $z_0$ is called a \emph{pole} of the complex-valued function $f$ if $f$ can be written as $f(z) = g(z) / (z - z_0)$ where $g(z_0) \ne 0$.
From this definition it is easy to see that $\lim_{z\rightarrow z_0}|f(z)| = \infty$, but knowing that $\lim_{z\rightarrow z_1}|f(z)| = \infty$ is not enough information to conclude that $z_1$ is a pole of $f$.

The \emph{order} of $z_0$ is the largest integer $n$ such that $f$ can be written as $f(z) = g(z) / (z - z_0)^n$ with $g(z_0) \ne 0$.
In other words, $f$ has a pole of order $n$ at $z_0$ if its Laurent series on a punctured neighborhood of $z_0$ can be written as
\[
f(z) = \sum_{k=-n}^\infty a_k(z-z_0)^k  \qquad, a_{-n} \neq 0.
\]

\begin{problem} % Plot poles.
Plot the following functions on domains that show all of its zeros and/or poles.
\begin{itemize}
\item $f(z) = z^{-n}$ for $n=1,2,3$.
\item $f(z) = z^2+iz^{-1}+z^{-3}$.
\end{itemize}
Use a Markdown cell to write a sentence or two about how the poles of a function appear in angle and magnitude plots.
How can you tell the multiplicity of the poles from the plot?
\label{prob:plot-complex-poles}
\end{problem}

Problem \ref{prob:plot-complex-poles} shows that in angle plot of $z^{-1}$, the colors cycle $n$ times clockwise around $0$, as opposed to the counter-clockwise rotations seen around roots.
Again, this can be explained by looking at the polar representation,
\[
z^{-n} = (re^{i \theta})^{-n} = r^{-n} e^{i(-n\theta)}.
\]
The minus sign on the $\theta$ reverses the direction of the colors, and the $n$ makes them cycle $n$ times.

From Problem \ref{prob:plot-complex-poles} it is also clear that $f(z) = z^2+iz^{-1}+z^{-3}$ behaves similarly to $z^{-3}$ for $z$ near the pole at $z_0 = 0$.
Since $|z-z_0|^{-n+k}$ is much smaller than $|z-z_0|^{-n}$ when $|z-z_0|$ is small, near $z_0$ the function behaves like $a_{-n}(z-z_0)^{-n}$.
This is why the order of a pole can be estimated by counting the number of times the colors circle a point in the clockwise direction.

\subsection*{Counting Zeros and Poles} % --------------------------------------

The \emph{Fundamental Theorem of Algebra} states that a polynomial $f$ with highest degree $n$ has exactly $n$ zeros, counting multiplicity.
For example, $f(z) = z^2 + 1$ has two zeros, and $f(z) = (z-i)^3$ has three zeros, all at $z_0 = i$ (that is, $z_0=i$ is a zero with multiplicity $3$).

The number of poles of function can also be apparent if it can be written as a quotient of polynomials.
For example, $f(z) = z / (z+i)(z-i)^2$ has one zeros and three poles, counting multiplicity.

\begin{problem}
Plot the following functions and count the number and order of their zeros and poles.
Adjust the bounds of each plot until you have found all zeros and poles.
\begin{itemize}
\item $f(z) = -4z^5 + 2z^4 - 2z^3 - 4z^2 + 4z - 4$
\item $f(z) = z^7 + 6z^6 - 131z^5 - 419z^4 + 4906z^3 - 131z^2 - 420z + 4900$
\item $f(z) = \frac{16z^4 + 32z^3 + 32z^2 + 16z + 4}{16z^4 - 16z^3 + 5z^2}$
\end{itemize}
\end{problem}

It is usually fairly easy to see how many zeros or poles a polynomial or quotient of polynomials has.
However, it can be much more difficult to know how many zeros or poles a different function may or may not have without visualizing it.

\begin{problem}
\label{prob:findpz}
Plot the following functions on the domain $\{x+iy\mid x,y\in[-8,8]\}$.
Explain carefully in a Markdown cell what each graph reveals about the function and why the function behaves that way.
\begin{itemize}
\item $f(z) = e^z$
\item $f(z) = \tan(z)$
\end{itemize}
(Hint: use the polar coordinate representation to mathematically examine the magnitude and angle of each function.)
\end{problem}

\subsection*{Essential Poles} % -----------------------------------------------

A complex-valued function $f$ has an \emph{essential pole} at $z_0$ if its Laurent series in a punctured neighborhood of $z_0$ requires infinitely many terms with negative exponents.
For example,
\[
e^{1/z} = \sum_{k=0}^{\infty}\frac{1}{n! z^n} = 1+\frac{1}{z}+\frac{1}{2}\frac{1}{z^2}+\frac{1}{6}\frac{1}{z^3}+\cdots.
\]
An essential pole can be thought of as a pole of order $\infty$.
Therefore, in an angle plot the colors cycle infinitely many times around an essential pole.

\begin{figure}[H] % f(z) = e^(1/z)
\includegraphics[width=.7\textwidth]{figures/poles_angle.png}
\caption{Angle plot of $f(z) = e^{1/z}$ on the domain $\{x+iy \mid x,y \in [-1,1]\}$.
The colors circle clockwise around the origin because it is a pole, not a zero.
Because the pole is essential, the colors repeat infinitely many times.}
\label{fig:complex-essential-pole}
\end{figure}

\begin{warn}
Often, color plots like the ones presented in this lab can be deceptive because of a bad choice of domain.
Be careful to validate your observations mathematically.
\end{warn}

\begin{problem}
For each of the following functions, plot the function on $\{x+iy\mid x,y\in[-1,1]\}$ and describe what this view of the plot seems to imply about the function.
Then plot the function on a domain that allows you to see the true nature of the roots and poles and describe how it is different from what the original plot implied.
Use Markdown cells to write your answers.
\begin{itemize}
\item $f(z) = 100z^2 + z$
\item $f(z) = \sin\left(\frac{1}{100z}\right)$.
\end{itemize}
(Hint: zoom way in.)
\end{problem}

\begin{comment} % This part is confusing and not in the book.
\subsection*{Multi-Valued Functions} % ----------------------------------------

Every complex number has two complex square zeros, since if $w^2=z$, then also $(-w)^2=z$.
If $z$ is not zero, these zeros are distinct.

Over the nonnegative real numbers, it is possible to define a continuous square root function.
However, it is not possible to define a continuous square root function over any open set of the complex numbers that contains 0.
This is intuitive after graphing $\sqrt{z}$ on the complex plane.

\begin{problem}
Plot the following functions and explain why they look the way that they do.
\begin{itemize}
\item $f(z) = \sqrt{z}$.
Use \li{np.sqrt()} to take the square root.
\item $f(z) = -\sqrt{z}$.
\end{itemize}
\end{problem}

% Just as raising $z$ to a positive integer compresses the $\theta$-axis, making the color wheel repeat itself $n$ times around 0, raising $z$ to a fractional power \emph{stretches} the $\theta$-axis, so that only one $n$th of the color wheel appears around 0.
% The colors at the ends of this $n$th-slice are not the same, but they appear next to each other in the plot of $z^{-n}$.
% This discontinuity will appear in every neighborhood of the origin.
%
% If your domain does not contain the origin, it is possible to define a continuous root function by picking one of the zeros.

\section*{Multi-Valued Functions} % -------------------------------------------

Another important topic in Complex Analysis is the study of multiple valued functions.
These functions arise as we consider the inverses of functions that are not strictly one to one on the complex plane.
A classic example is $\sqrt{x}$, which may take two values for every nonzero point of the complex plane.

In the Real numbers we worked with functions like this by simply restricting their output on a certain domain.
We can do a similar thing in the Complex plane.
Loosely speaking, such a restriction is called a branch.
Computationally we restrict the output to a single portion of the actual possible values of the multifunction.
Inverse functions that have multiple values like this are called \emph{multi-valued functions} or \emph{multifunctions}.
NumPy automatically restricts the output of multi-valued functions. So to get other cuts you have to modify the function to get the cut you want, like multiplying the output of the $\sqrt{x}$ by $-1$.
Figure \ref{fig:sqrt} shows the two cuts surfaces for $\sqrt{z}$ in the complex plane.

These are some very basic examples.
Another simple example is $\ln\left(z\right)$, which has a single value for the real part and infinitely many possible values for its imaginary part.
This is because for any complex $z\neq 0$, we have $e^z=e^{z+2n\pi}$ where $n$ is any integer.
\end{comment}

\begin{comment}
\begin{problem}
Write two functions, both accepting a natural number $n$.
Have one function plot the Riemann surface for the real part $f(z)=\sqrt[n]{z}$ and the other plot the imaginary part.

Hint: Convert $z$ in $f(z)=\sqrt[n]{z}$ to polar form as $z=re^{\theta + 2k\pi}$
If you then plug this into $\sqrt[n]{z}$ the function takes the form
\[f(z)=\sqrt[n]{r} e^{i \frac{\theta + 2 \pi k}{n}}\]
Notice that here $f(z)$ has distinct values for $k = 0, 1, \dots, n-1$ (a total of $n$ different values). Each value of $k$ corresponds to a different branch, which you can plot as $n$ separate surfaces.

If you use just one surface to plot each branch you will get erroneous vertical lines from jump discontinuities. Split each branch into two surfaces to get rid of these lines. You can investigate where the discontinuities occur by first plotting it as just one surface. The discontinuities happen at the same place for all $n$;
\end{problem}
\end{comment}

\begin{comment} % Integration. This has been orphaned.
\section*{Contour Integrals in the Complex Plane}

From multivariable calculus, you may recall that an integral may be taken along a path.
This is very similar to what can be done in the complex plane.
Consider the function $f(z)$ on the complex plane.
Let $z=x+iy$.
Let $u$ and $v$ be the real and imaginary parts of $f$ respectively.
We integrate $f$ along some contour $C$ in the complex plane, beginning at $z=a$ and ending at $z=b$.
This integral may be written
\[\int_c f(z)dz\]
Parameterizing $z$, we have
\[\int_a^b f\left( c\left(t\right)\right) c'\left(t\right) dt\]
Expanding into real and imaginary parts (where $c\left(t\right) = x\left(t\right) + i y\left(t\right)$), we have
\[\int_a^b \left(u \left(c \left(t\right)\right) x'\left(t\right)-v\left(c \left(t\right)\right) y'\left(t\right)\right) dt + i \int_a^b\left(v \left(c \left(t\right)\right)x'\left(t\right)+u\left(c \left(t\right)\right) y'\left(t\right)\right) dt\]
We have now written this complex integral as the sum of two real valued integrals in $\mathbb{R}$.
Note that this implies that $\int_C f(z) dz$ may depend on the contour we choose and not just on the endpoints $a$ and $b$.

\begin{problem}
Write a function which takes a complex function $f(z)$, a contour parameterization $c(t)$ of a contour $c$, and the integration bounds on $t$ and returns the integral of $f$ along the contour $c$.
Use the numerical integration function \li{sympy.mpmath.quad} and the numerical derivative function \li{sympy.mpmath.diff} included in mpmath (which is, in turn, included as a submodule of sympy).
These functions already work for complex numbers.
To do something similar with the integration routines in SciPy, we would have to separate the function into real and imaginary parts, as is shown above.

Using the function you just defined, integrate the following functions along the following contours
\begin{itemize}
\item $\bar{z}$ counterclockwise along the unit ball starting and ending at $1$
\item $\bar{z}$ along a straight line from $0$ to $1+i$
\item $\bar{z}$ along the real axis from $0$ to $1$, then along the line from $1$ to $1+i$
\item $\bar{z}$ along the unit ball centered at $i$ from $0$ to $1+i$
\item $e^z$ counterclockwise along the unit ball starting and ending at $1$
\item $e^z$ along a straight line from $0$ to $1+i$
\item $e^z$ along the real axis from $0$ to $1$, then along the line from $1$ to $1+i$
\item $e^z$ along the unit ball centered at $i$ from $0$ to $1+i$
\end{itemize}
\end{problem}

Notice that, for a holomorphic function on a simply connected domain, the integrals from one point to another are not path dependent for any contours that lie within the domain.
An immediate consequence of the theorem is that for a complex function $f$, holomorphic on a simply connected domain $D$, and a contour $C$ lying entirely within $D$ which begins and ends at some point $a\in D$,
\[\int_C f(z)dz=0\]

The quadrature algorithms used in many of the integration algorithms work along a straight line between the integration bounds in the complex plane, so for holomorphic functions we should be able to use the integration function we wrote earlier.
For example, integrating $e^z$ from $-1-i$ to $1+i$ can be done numerically like this:
\begin{lstlisting}
from sympy import mpmath as mp
mp.quad(lambda z: mp.exp(z), (complex(-1, -1), complex(1, 1)))
\end{lstlisting}

\section*{The Cauchy Integral Formula}

Another major theorem in complex analysis is called Cauchy's Integral Formula (not to be confused with Cauchy's Integral Theorem).
It states that for a domain $D$ in the complex plane, containing some contour $C$ and the interior of $C$, for any $z_0$ in the interior of $C$,
\[f(z_0)=\frac{1}{2\pi i} \int_C \frac{f(z)}{z-z_0} dz\]

With more work, this theorem can be used to show that any function $f$ holomorphic on some domain $D$ is also infinitely differentiable on that domain.
In fact, the $n$th derivative of $f$ is given by the formula
\[f^{(n)}(z_0) = \frac{n!}{2\pi i} \int_C \frac{f(z)}{(z-z_0)^{n+1}} dz\]
This result is also important because it allows us to relate the value of $f$ on the inside of a contour to the value of $f$ on the contour itself.
In other words, the values of $f$ inside the contour depend only on the values of $f$ along the contour itself.
A related theorem (the Morera theorem) states that if some function $f$ is continuous on a domain $D$ and for every contour beginning and ending at the same point, the formula $\int_C f(z) dz = 0$ holds, then $f$ is holomorphic on $D$.

\begin{problem}
Using Cauchy's Integral Formula, write a python function which returns a callable function which evaluates a complex function $f$ along the interior of a contour $C$.
It should accept a callable function for the parameterization of $C$, a callable function for the values of $f$ along $C$, and the bounds on the parameter used.
Assume in your function that $C$ begins and ends at the same point and that $f$ also begins and ends at the same value (so that $f$ is continuous along $C$)
Try it out on simple functions like $e^x$ with complex values and compare what you get with what calling the functions normally gives you.
\end{problem}

Notice that in Cauchy's Integral Formula, we are integrating along a contour that begins and ends at the same point.
The function is also holomorphic at every point except $z_0$. At $z_0$ the integrand is undefined and has a singularity.
This integral around a singularity has some useful properties.
We will discuss these properties later on.

\end{comment}

\begin{comment}
\section*{Appendix}
It is possible to visualize the argument and the modulus of the output of a complex function $f(z)$.
One way to do so is to assign the modulus to a \emph{lightness} of color.
For example, suppose we have a complex number with argument 0, so it will map to red in the color plots described above.
If its modulus is very small, then we can map it to a blackish red, and if its modulus is large, we can map it to a whitish red.
With this extra rule, our complex plots will still be very much the same, except that zeros will look like black dots and poles will look like white dots (see Figure \ref{fig:example} for an example).

The code below implements the map we just described.
Be warned that this implementation does not scale well.
For example, if you try to plot a complex function whose outputs are all very small in modulus, the entire plot will appear black.

\begin{lstlisting}
import numpy as np
import matplotlib.pyplot as plt
from colorsys import hls_to_rgb

def colorize(z):
    '''
    Map a complex number to a color (or hue) and lightness.

    INPUT:
    z - an array of complex numbers in rectangular coordinates

    OUTPUT:
    If z is an n x m array, return an n x m x 3 array whose third axis encodes
    (hue, lightness, saturation) tuples for each entry in z. This new array can
    be plotted by plt.imshow().
    '''

    zy=np.flipud(z)
    r = np.abs(zy)
    arg = np.angle(zy)

    # Define hue (h), lightness (l), and saturation (s)
    # Saturation is constant in our visualizations
    h = (arg + np.pi)  / (2 * np.pi) + 0.5
    l = 1.0 - 1.0/(1.0 + r**0.3)
    s = 0.8

    # Convert the HLS values to RGB values.
    # This operation returns a tuple of shape (3,n,m).
    c = np.vectorize(hls_to_rgb) (h,l,s)

    # Convert c to an array and change the shape to (n,m,3)
    c = np.array(c)
    c = c.swapaxes(0,2)
    c = c.swapaxes(0,1)
    return c
\end{lstlisting}

The following code uses the \li{colorize()} function to plot  $\frac{z^2 - 1}{z}$. The output is Figure \ref{fig:example}.

\begin{lstlisting}
>>> f = lambda z :  (z**2-1)/z
>>> x = np.linspace(-.5, 1.5, 401)
>>> y = np.linspace(-1, 1, 401)
>>> X,Y = np.meshgrid(x,y)
>>> Z=f(X+Y*1j)
>>> Zc=colorize(Z)
>>> plt.imshow(Zc, extent=(-.5, 1.5, -1, 1))
>>> plt.show()
\end{lstlisting}

\begin{figure}
\includegraphics[width=.7\textwidth]{figures/example.png}
\caption{Plot of the function $\frac{z^2 - 1}{z}$ created with \li{colorize()}.
Notice that the zero at 1 is a black dot and the pole at 0 is a white dot.}
\label{fig:example}
\end{figure}
\end{comment}