\subsection{The Riemann-Hurwitz Theorem}
\begin{theorem}[Riemann-Hurwitz]
    Let $f:R\to S$ be any non-constant analytic map of compact Riemann surfaces, then
    $$\chi(R)=\deg(f)\chi(S)-\sum_{p\in R}(m_f(p)-1)$$
\end{theorem}
\begin{remark}
    As $R$ is compact, the sum only has finitely many nonzero terms.
\end{remark}
\begin{proof}[Sketch of proof]
    As in the proof of the valency theorem, each $q\in S$ has a ``power neighborhood'' $U$ where $f$ restricts to a union of power maps on $f^{-1}(U)$.
    By compactness, there is a finite open cover $\{U_1,\ldots,U_k\}$ of $S$ where each $U_i$ is a ``power neighbourhood'' of $f$.
    In particular, the number of branch points is finite.
    We can subdivide a triangulation on $S$ so that we can eventually reach a triangulation such that each triangle has at most $1$ branch point.
    We can further subdivide such that each branch point is a vertex.
    Continue to subdivide so that each triangle is contained in some $U_i$.
    Now the preimage of this eventual triangulation forms a triangulation of $R$.
    Let $n=\deg f$ and $V_R,E_R,F_R,V_S,E_S,F_S$ are exactly what you think they mean.
    Then, intuitively, $F_R=nF_S,E_R=nE_S$ while
    $$|f^{-1}(\{q\})|=n-\sum_{p\in f^{-1}(\{q\})}(m_f(p)-1)$$
    Summing up,
    $$V_R=nV_S-\sum_{q\in S}\sum_{p\in f^{-1}(\{q\})}(m_f(p)-1)=nV_S-\sum_{p\in R}(m_f(p)-1)$$
    which implies the identity.
\end{proof}


\section{Lighting}
\subsection{Preface}
One of the most important factors that make a scene realistic is how the light interacts with the objects in it.
Through time, rendering techniques progressed from various empirical models (e.g. Phong models), that required
tweaking from the artists in order to make results seem more realistic under different lighting setups, to physical based
algorithms (e.g. Microfacet models) that calculate the correct object lighting given the lighting environment and
its material properties. The Physical Based models are based on the laws of physics on how light interacts with
matter depending on a number of environmental variables.

\subsection{Background}
All the physical based rendering models try to solve a simplified version of the rendering equation~\cite{lighting:ref32}.
This equation relates the outgoing light in a particular direction $\omega_o$ to the incident light at a point $p$ on a surface:

\begin{equation}
\label{eq:rendeq}
L_o(p,\omega_o) = L_e(p, \omega_o) + \int_\Omega f_r(p,\omega_i,\omega_o)L_i(p,\omega_i)n\cdot\omega_i \ d\omega_i
\end{equation}

\noindent Breaking down the equation we get:

\begin{equation}
\underbrace{L_o(p,\omega_o)}_\text{Outgoing Light} = \underbrace{L_e(p, \omega_o)}_\text{Emitted Light} + \underbrace{\int_\Omega f_r(p,\omega_i,\omega_o)L_i(p,\omega_i)n\cdot\omega_i \ d\omega_i}_\text{Reflected Irradiance}
\end{equation}

According to the equation the \textit{outgoing light} in a particular direction from a point on a surface is the
sum of the \textit{emitted light} from that point and the contribution of all the incoming light from all directions in a
hemisphere above the point p (the orientation of the hemisphere is determined by the normal n).

\noindent Breaking down the integral we get:

\begin{equation}
\int_\Omega \underbrace{f_r(p,\omega_i,\omega_o)}_\text{BRDF}\underbrace{L_i(p,\omega_i)}_\text{Incoming Light}\underbrace{n\cdot\omega_i}_\text{Incident Flow}\ d\omega_i
\end{equation}

Where BRDF is a function that returns the ratio of the amount of light reflected in a particular direction $\omega_o$, to the
amount received from another direction $\omega_i$ (more details later), incoming light is the light arriving at the point p
from the direction $\omega_i$, and incident flow is an attenuation factor that makes the incoming light at $p$
dependent on the cosine of the angle between the normal $n$ and the incoming light direction $\omega_i$. Note that the
incoming light does not have to come from a light source (direct light), it may have been reflected or refracted from another
point int the scene (indirect light).

\subsection{Diffuse and Specular lighting}
When light hits a surface there are three possible outcomes. Light may be \textbf{absorbed} by the material, light may be
\textbf{transmitted} through to the other side or light may be \textbf{reflected} back. For dense objects, light scattered
back through the surface of incidence near the point of entry is said to be diffusely reflected (diffused). Moreover, surface
reflection is also called specular reflection. The Cook-Torrance model, that is used in the current implementation,
also makes this separation by describing the BRDF function as:

$$f_r = k_d f_{lambert} + k_s f_{cook-torrance}$$

Where $f_{lambert}$ describes the diffuse component, $f_{cook-torrance}$ describes the specular component, $k_d$ is the amount
of incoming radiance that gets diffused and $k_s$ is the amount of light that is specularly reflected. If a given material
exhibits a strong diffusive behaviour it have a high value for $k_d$, while if it behaves more like a mirror it will have high $k_s$.
For the diffuse BRDF has been choosen the Lambert's BRDF that is equal to:

$$f_{lambert} = \frac{c}{\pi}$$

where c is the surface colour. The specular Cook-Torrance BRDF is defined as:

$$f_{cook-torrance} = \frac{DFG}{(4 \omega_o \cdot n)(\omega_i \cdot n)}$$

Where D is the distribution function, F is the fresnel function and G is the geometry function.
The Cook-Torrance model falls into the category of the \textbf{Microfacet Models} which are based on the idea
that rough surfaces can be modelled as a collection of small microfacets, where each microfacet is assumed to be a very small perfect mirror,
and their distribution and orientation define how the light is reflected at a large scale.\cite{lighting:ref33}

\subsection{Material properties}
Materials are defined by the following properties:

\begin{enumerate}
    \item Base Color (Albedo), defines the color of diffused light
    \item Roughness, defines how rough is a surface. Rougher surfaces will show wider but dimmer specular reflections, while smoother
        surfaces will show brighter but sharper specular reflections.
    \item Metallic, defines if a material behaves like a dielectric or metal.
    \item Reflectivity, defines the percentage of light a surface reflects. Specifically defines how reflective a surface is when viewed
        head on.
    \item Emissive Color, defines emission light color for emissive materials.
\end{enumerate}

The separation between metals and dielectrics, also affects each light reflection type.
Metals are assumed to be fully specular materials with base color acting as their specular color and no diffuse color (black)
due to very quick absorption near the surface. In the dielectrics category on the other hand the base color acts as their diffuse
color and white as their specular color.

In the following figures we can see the effects of roughness and reflectivity values for each type of material with
roughness increasing from left to right and increasing from bottom to top:

\begin{figure}[h]
    \centering
    \includegraphics[scale=0.4,clip=true]{./image/pbr_dielectrics.png}
    \caption{Dielectrics}
\label{fig:pbrdielectrics}
\end{figure}

\begin{figure}[h]
    \centering
    \includegraphics[scale=0.4,clip=true]{./image/pbr_metals.png}
    \caption{Metals}
\label{fig:pbrmetals}
\end{figure}

\subsection{Environmental lighting}
As stated before, light arriving at a point may have been reflected or refracted from another point in the scene.
Offline algorithms may be able to capture that effect but in realtime applications this is computationally expensive.
So in order to emulate indirect lighting quite often many rendering systems resolve to approaches that take advantage of
preprocessing the surrounding environment of an object.

A technique called Image Base Lighting (IBL) is one solution that addresses that problem. First, an object's environment is
captured as images in a cubemap form\footnote{Cube mapping is a method of environment mapping that uses the six faces of a cube
as the map shape. The environment is projected onto the sides of a cube and stored as six square textures, or unfolded into six
regions of a single texture.} and then the rendering equation integrals are precalculated, treating each pixel in the
environment map as a point sample on the surrounding environment that contributes to the center of the cube map. The result
can also be stored in cubemaps that can be queried later and be used to calculate the environmental light contribution on a
fragment. This way, each texel of the new environment (pre-filterred radiance) map, corresponds to the pre-integrated radiance
from a given lobe of directions centered at the direction of the cubemap texel. The integrant includes the weighting according
to a BRDF matching the directional lobe, which corresponds to a particular roughness level.

\begin{figure}[h]
    \centering
    \includegraphics[scale=0.18,clip=true]{./image/envl_ref.png}
    \caption{Radiance Field}
\label{fig:envlref}
\end{figure}

The environmental diffuse and specular light contribution can be captured on an Irradiance Maps\footnote{The irradiance map that is
adjusted for specular reflections is also known as Prefiltered mipmaped radiance environment map (PMREM)}. The diffuse Irradiance map
can be generated by precalculating the BRDF for all the points in a hemisphere around an object according to the normal
direction, and similary the specular environment map can be generated by precalculating the BRDF for an area near the ideal reflection
vector. For the specular reflections, roughness is the main contributor on the tightness of the specular lobe, so with lower roughness
values a less blurry lookup map is needed, while with the higher roughness values a more blurry map must be used in order to make
the correct specular lobe. For this reason, multiple versions of increasing roughness radiance maps are created that are chosen
for their relevant roughness value. These maps can be stored as different mipmaps of the radiance cubemap in order to sample uniformingly.
A function that will choose the correct mipmap level according to an object roughness must be also created, that should be tweaked
for a specific cubemap resolution.

\begin{figure}[ht]
    \centering
    \includegraphics[scale=0.18,clip=true]{./image/envl_irr.png}
    \caption{Diffuse Irradiance Map}
\label{fig:envlirr}
\end{figure}

\begin{figure}[ht]
    \centering
    \includegraphics[scale=0.36,clip=true]{./image/envl_rad.png}
    \caption{Specular Irradiance Map}
\label{fig:envlrad}
\end{figure}

Note that still, this way only phong lobe shapes are possible to be matched, as the whole microfacet specular
BRDF is not evaluated for the environment, although more advanced approximations also exist\footnote{Split sum approximation is a great
example, where the part of the BRDF not included in the PMREM is calculated as a separate lookup table that can be used to make
specular lobes for the environment lighting that match the direct lighting model\cite{lighting:ref40}}.


\input{PreambleCommon}
\input{../WeekTitles}

\begin{document}
\setfont
\pagestyle{fancy}
\renewcommand{\Week}{9 }
\renewcommand{\WeekTitle}{\WeekTitleNine }

\fancyhead[LE,RO]{Week \Week}  % default, usually only for first page
\fancyfoot{}
\sectionbox{Week \#\Week: \WeekTitle}

\vspace{5mm}
\goals
\begin{itemize}
\item Take problems that can be modeled by differential equations,
  both first and second order, and give solutions both by hand and
  MATLAB
\item Examine case studies of differential equations applied to
  engineering problems and reproduce those solutions
\end{itemize}
\vspace{5mm}

%Falling body problem (maybe even the redbull stratos jump?)
%Electric Circuits
%Suspension cable bridge

\newpage

\topic{Application - Pendulum}
\subsection*{Application - Pendulum }
\begin{minipage}[t]{0.25\linewidth}
\vspace{0pt}
\begin{center}
\includegraphics[width=1.0\linewidth]{graphics/notes_09_pendulum_diagram}
\end{center}
\end{minipage}
\begin{minipage}[t]{0.70\linewidth}
\vspace{0pt}
\begin{align*}
  \mbox{Newton's }& \mbox{ Second Law: } \\
   m  L^2 \theta'' & = T_g + T_f  \\
  & = - m L g \sin(\theta) - (\mu L^2 m) \theta' \\[3ex]
  \mbox{Solving for $\theta''$: }\theta'' & = - \frac{g}{L} \sin(\theta) - \mu
  \theta'
\end{align*}
\end{minipage}

\problem Turn this single second-order DE into a pair of first-order
DEs.

\newpage
\problem Compare the system of differential equations we obtained to
the equations that define the motion of the damped spring/mass system.

\begin{minipage}[t]{0.45\linewidth}
\vspace{0pt}
\includegraphics[width=0.4\linewidth]{graphics/notes_08_hanging_mass}
\begin{align*}
  \frac{dw_1}{dt} & = w_2 \\
  \frac{dw_2}{dt} & = \left(\frac{1}{m}\right) (- k w_1-c w_2)
\end{align*}
\end{minipage}
\begin{minipage}[t]{0.45\linewidth}
\vspace{0pt}
\begin{center}
\includegraphics[width=0.2\linewidth]{graphics/notes_09_pendulum_diagram}
\end{center}
\begin{align*}
  \frac{d w_1}{dt} & = w_2 \\
  \frac{d w_2}{dt} & = -\frac{g}{L}\sin(w_1)  - \mu w_2
\end{align*}
\end{minipage}

\newpage

\problem Create a new MATLAB function file called \texttt{pendulumDE.m}.
Start with the first line
\begin{verbatim}
function dw_dt = pendulumDE(t, w, g, L, mu)
\end{verbatim}

In the body of the function, implement the system of differential
equations
\begin{align*}
  \frac{d w_1}{dt} & = w_2 \\
  \frac{d w_2}{dt} & = -\frac{g}{L}\sin(w_1)  - \mu w_2
\end{align*}
\vsc

\newpage

\problem Write a MATLAB script that simulates the motion of the
pendulum
using  \\
$g = 9.8$ m/s$^2$, $L$ = 2 m, $\mu = 0.1$, and \\
initial amplitude of 0.05 radians ($\approx 2.9$ degrees). \\
Generate a plot of the resulting angular position over time.

\newpage
\topic{Pendulum - Period of Swings}
\subsection*{Pendulum - Period of Swings}

Galileo famously noticed the consistent period of pendulum swings,
even if the amplitude of the swings was changed (so the actual
distance travelled was different).

\vspace{2.5in}

\problem Compare the periods of the pendulum swings, using a range of
initial angles from $\theta_0 = 0.05$ radians up to $\theta_0 = 0.25$
radians ($\approx 14$ degrees).

\newpage
However, it turns out that pendulums are {\bf not} perfectly
consistent in their period, due to the non-linear term
$\ds -\frac{g}{L} \sin(\theta)$ in one of the forces: as the
amplitudes get bigger, there is a gradual lengthening of the period.

\problem Compare the periods of the pendulum swings, using a range of
initial angles from $\theta_0 = 0.25$ radians up to $\theta_0 = \frac{\pi}{2}$
radians ($= 90$ degrees).

\newpage

\problem Use these observations to explain the designs you see for
pendulum-based clocks.

\newpage

\topic{Pendulum -  Including an Initial Velocity}
\subsection*{Pendulum -  Including an Initial Velocity}

\problem Write a new simulation script that starts the pendulum
swinging from $ \theta_0 = -\frac{\pi}{2}$, with no initial velocity.
Simulate the motion for this scenario and generate a graph of the
angle against time.

Use the  parameters $g = 9.8$ m/s$^2$, $L$ = 2 m, and $\mu = 0.1$. \\

\vsc

\newpage

If we add a high enough initial `kick', or initial velocity, it would
be possible to make the mass of the pendulum go ``over the top'', or
above the point of rotation.

\problem Sketch what the anglular position graph would look like for
this scenario.

\newpage

\problem If we keep the initial angle at $-\frac{\pi}{2}$ (pendulum
out horizontally), experiment with the MATLAB code to find the initial
velocity that will push the pendulum ``over the top''.

\newpage

\topic{Application - Lake Mixing Model}
\subsection{Application - Lake Mixing Model}
Consider a small lake that initially contains $10$ million litres of
fresh water.  Water containing an undesirable chemical flows into the
lake at the rate of $5$ million litres per year; the mixture in the
lake flows out at the same rate.  The concentration $c(t)$ of chemical
in the incoming water varies periodically with time according to the
expression $c(t) = 2 + \sin(2t) \; \text{g} \cdot \text{L}^{-1}$.

  \problem Construct a mathematical model of this flow process.

\newpage
\problem Use MATLAB and a differential equation solver to determine
the amount of chemical in the lake over time, assuming that the lake
started without any contamination.

\newpage

\topic{Application - Tailings Pond With Sediment}
\subsection*{Application - Tailings Pond With Sediment}
% Source: http://faculty.sfasu.edu/judsontw/ode/html/firstlook05.html
Consider a tailings pond, where the the inflow contains both an
environmentally sensitive chemical, and sediments that will settle out
of the water.

\begin{itemize}
\item The volume of the pond is 40,000 cubic meters.
\item Water is flowing in and out of the pond at a rate of 1,500 cubic
  meters per day.
\item The water flowing into the pond contains 2 g of toxic chemical
  per cubic meter.
\item The inflow water also contains 1\% sediments
\end{itemize}

\problem Sketch a diagram of this scenario.

\newpage

\problem Write a differential equation that describes the rate of
change of the concentration of the chemical in the water remaining in
the tailings pond.

\newpage

\problem Use MATLAB and a differential equation solver to determine
the concentration of chemical in the water part of the tailings pond,
assuming that the pond started without any contamination.

\newpage

\problem Comment on any mismatch between the model and the reality
that should be addressed to make the model more accurate.

\newpage
\topic{Application - Interconnected Tanks}
\section*{Application- Interconnected Tanks}

\noindent
Consider the tanks shown below, which shows water flowing between the
tanks, and the concentration of a salt solution coming in.  Within
each tank, the water/salt solution is kept well mixed.
\begin{center}
\includegraphics[width=0.7\linewidth]{graphics/notes_09_tanks1}
\end{center}

\begin{problem}
  If both tanks start with no salt, sketch what you expect will happen
  to the concentration within each tank over time.
\end{problem}

\newpage
\begin{minipage}[h]{0.5\linewidth}
\vspace{0pt}
\begin{problem}
Create a system of differential equations that dictate
how the two tank concentrations will evolve over time.
\end{problem}
\end{minipage} \hfill
\begin{minipage}[h]{0.45\linewidth}
\vspace{0pt}
\includegraphics[width=1.0\linewidth]{graphics/notes_09_tanks1}
\end{minipage}

\newpage
\begin{minipage}[t]{0.5\linewidth}
\vspace{0pt}
\problem
  Use MATLAB and a differential equation solver to predict the exact salt concentrations over time
in {\bf both tanks}.
% \begin{align*}
% \frac{dc_A}{dt}    & = \frac{-1}{10} c_A + 3; \\
% \frac{dc_B}{dt}    & = \frac{1}{20} c_A -\frac{1}{20} c_B
% \end{align*}
\end{minipage}
\begin{minipage}[t]{0.45\linewidth}
\vspace{0pt}
\includegraphics[width=1.0\linewidth]{graphics/notes_09_tanks1}
\end{minipage}

\newpage

\topic{Tank Model - Example 2}
\subsection*{Tank Model - Example 2}

Consider the more complicated tank arrangement shown below.
\begin{center}
\includegraphics[width=0.7\linewidth]{graphics/notes_09_tanks2}
\end{center}
\begin{problem}
Given that the initial concentrations are \\
$c_A(0) = 0 $ g/L and $c_B(0)  = 90$ g/L,  \\
sketch what you would predict for the concentration in each tank over time.
\end{problem}

\newpage
\begin{minipage}[h]{0.5\linewidth}
\vspace{0pt}
\begin{problem}
  Construct the differential equation for the salt concentration in
  each tank.
\end{problem}
\end{minipage} \hfill
\begin{minipage}[h]{0.45\linewidth}
\vspace{0pt}
\includegraphics[width=1.0\linewidth]{graphics/notes_09_tanks2}
\end{minipage}

%\newpage
%\hfill \includegraphics[width=0.5\linewidth]{graphics/notes_09_tanks2}

\newpage

\begin{minipage}[h]{0.5\linewidth}
  \vspace{0pt} \problem Use MATLAB and a differential equation solver
  to predict the salt concentrations over time by solving the system
  of differential equations
  \begin{align*}
    \frac{dc_A}{dt} & = -0.09 c_A + 0.02 c_B + 2.1 \\
    \frac{dc_B}{dt} & = 0.18 c_A - 0.18 c_B \\
  \end{align*}
\end{minipage} \hfill
\begin{minipage}[h]{0.45\linewidth}
\vspace{0pt}
\includegraphics[width=1.0\linewidth]{graphics/notes_09_tanks2}
\end{minipage}

\newpage

~\hfill \begin{minipage}[h]{0.45\linewidth}
\vspace{0pt}
\includegraphics[width=1.0\linewidth]{graphics/notes_09_tanks2}
\end{minipage}

\end{document}
%Confirmation of deflection:
%  http://www.efunda.com/formulae/solid_mechanics/beams/casestudy_display.cfm?case=cantilever_uniformload

\end{document}


\allowdisplaybreaks[4]
\section{Adaptive Noise Cancellation}
\subsection{Delay of ALE}
Due to the uncorrelation between interest signal and noise signal, the white noise can be eliminated by the delay version of the signal. The adaptive line enhancer (ALE) is used the delay of the noise-corrupted signal $s(n)$ to estimate the interest signal $\hat x(n)$. The optimal delay $\Delta$ can be calculated at the beginning of the Mean Squared Error.
\begin{align}
	\mathbb{E}\{(s(n)-\hat x(n))^2\}
	&=\mathbb{E}\{(x(n)+\eta(n)-\hat x(n))^2\}\notag\\
	&=\underbrace{\mathbb{E}\{\eta(n)^2\}}_{\eta^2}+\underbrace{\mathbb{E}\{(x(n)-\hat x(n))^2\}}_{\approx 0}+\underbrace{2\mathbb{E}\{(x(n)-\hat x(n))\eta(n)\}}_{minimize}
\end{align}
Therefore, for the optimal estimation, the MSE should be equal to the noise power. That is, only the last term should minimize.
\begin{align}
	\min_{\Delta} \mathbb{E}\left\{(x(n)-\hat x(n))\eta(n)\} \right\}
	&\Rightarrow \min_{\Delta} \mathbb{E}\left\{\hat x(n)\eta(n)\} \right \}\notag\\
	&=\min_{\Delta} \mathbb{E}\left\{v(n) + 0.5v(n-2)\mathbf w^T(n)\mathbf u(n)\right\}\notag\\
	&=\min_{\Delta} \mathbb{E}\left \{(v(n) + 0.5v(n-2))\sum_{i=0}^{M-1}\mathbf w^T(n)s(n-\Delta -i)\right\}\notag\\
	&=\min_{\Delta} \mathbb{E}\left \{(v(n) + 0.5v(n-2))\sum_{i=0}^{M-1}\mathbf w^T(n)(x(n-\Delta -i)+\eta(n-\Delta -i))\right\}
\end{align}
And due to uncorrelated of $x(n)$ and $v(n)$, equation above can be simplified to
\begin{align}
	&\min_{\Delta} \mathbb{E}\left \{(v(n) + 0.5v(n-2))\sum_{i=0}^{M-1}\mathbf w^T(n)(x(n-\Delta -i)+\eta(n-\Delta -i))\right\}\notag\\
	=&\min_{\Delta} \mathbb{E}\left \{(v(n) + 0.5v(n-2))\sum_{i=0}^{M-1}\mathbf w^T(n)\eta(n-\Delta -i)\right\}\notag\\
	=&\min_{\Delta} \mathbb{E}\left \{(v(n) + 0.5v(n-2))\sum_{i=0}^{M-1}\mathbf w^T(n)(v(n-\Delta -i)+v(n-\Delta-2 -i))\right\}\label{eq:MSe}\\
	\approx& 0 \to\Delta=2\notag
\end{align}
Therefore, observing Eq.\ref{eq:MSe}, the error will tend to zero only if the delay $\Delta$ is larger than 2. Since the time indexes of signal $v(n)$  are non-overlapping, resulting in uncorrelated. Fig.\ref{fig:2_3_a} depicts the effect of the delay $\Delta=1\sim4$ on the estimated signal $\hat x(n)$ with the fixed filter length $M=3$. The top row illustrated 100 realisations of $s(n)$, $\hat x(n)$, while the bottom row is the average signal of the estimation. When the delay $\Delta>3$, the noise signal (in yellow) is suppressed a lot, leading to a small MSE. Thus, the results prove the previous analysis.
\begin{figure}[htb]
    \centering
    \hspace{-0.4cm}
    \begin{subfigure}[b]{0.26\textwidth}
     \centering
     \includegraphics[width=\textwidth]{fig/23/23a1.eps}
    \end{subfigure}
    \hspace{-0.4cm}
    \begin{subfigure}[b]{0.26\textwidth}
     \centering
     \includegraphics[width=\textwidth]{fig/23/23a3.eps}
    \end{subfigure}
    \hspace{-0.4cm}
    \begin{subfigure}[b]{0.26\textwidth}
     \centering
     \includegraphics[width=\textwidth]{fig/23/23a5.eps}
    \end{subfigure}
    \hspace{-0.4cm}
    \begin{subfigure}[b]{0.26\textwidth}
     \centering
     \includegraphics[width=\textwidth]{fig/23/23a7.eps}
    \end{subfigure}
    \\
    \hspace{-0.4cm}
    \begin{subfigure}[b]{0.26\textwidth}
     \centering
     \includegraphics[width=\textwidth]{fig/23/23a2.eps}
    \end{subfigure}
    \hspace{-0.4cm}
    \begin{subfigure}[b]{0.26\textwidth}
     \centering
     \includegraphics[width=\textwidth]{fig/23/23a4.eps}
    \end{subfigure}
    \hspace{-0.4cm}
    \begin{subfigure}[b]{0.26\textwidth}
     \centering
     \includegraphics[width=\textwidth]{fig/23/23a6.eps}
    \end{subfigure}
    \hspace{-0.4cm}
    \begin{subfigure}[b]{0.26\textwidth}
     \centering
     \includegraphics[width=\textwidth]{fig/23/23a8.eps}
    \end{subfigure}
    \caption{ALE: Effect of $\Delta$ with fixed $M=3$}
    \label{fig:2_3_a}
\end{figure}
\subsection{Effects of $M$ and delay on MSPE }
In order to find the optimal delay and the filter length $M$, this experiment calculates the MPSE by varying $\Delta$ from 1 to 25 and $M$ from 1 to 20. As shown in Fig.\ref{fig:2_3_b1}, neglecting the inappropriate values of $\Delta\leq 2$ and $M=1$, the MPSE curves both keep a increasing tendency with the growing of the delay and filter order. However, the MSPE remains approximately flat with a minimum error in range 3 to 6. When the filter order is large than 6, the over-modelling problem will occur, causing the growth of the MSPE. Notice that the performances of $\Delta=3$ and $\Delta=5$ are nearly same.
\begin{figure}[htb]
    \centering
    \hspace{-0.4cm}
    \begin{subfigure}[b]{0.4\textwidth}
     \centering
     \includegraphics[width=\textwidth]{fig/23/23b2.eps}
    \end{subfigure}
    \hspace{1cm}
    \begin{subfigure}[b]{0.4\textwidth}
     \centering
     \includegraphics[width=\textwidth]{fig/23/23b1.eps}
    \end{subfigure}
    \caption{ALE: Effect of $\Delta$ and $M$ on MSPE}
    \label{fig:2_3_b1}
\end{figure}\\
Observing the effect on varying delay, the MSPE curves approximately keep same in the range of $\Delta\in[3,6]$, which are similar as the ones of varying order $M$. Afterwards, the MSPE gradually rises up to the maximum when $\Delta=25$. If the delay is set to large, there is a lag between the estimated signal $\hat x(n)$ and actual signal $x(n)$, resulting in the increasing MSPE. As shown in Fig.\ref{fig:2_3_b2}, the realisations of optimal parameters ($M=3$, $\Delta=3$) and large delay ($M=3$, $\Delta=25$) are plotted. There is an obvious shift of estimated signal which proved the analysis. Due to the computational cost with increasing order, the relatively optimal parameters are $M=3$ and $\Delta=3$.
\begin{figure}[htb]
    \centering
    \hspace{-0.4cm}
    \begin{subfigure}[t]{0.37\textwidth}
     \centering
     \includegraphics[width=\textwidth]{fig/23/23b3.eps}
    \end{subfigure}
    \hspace{0.4cm}
    \begin{subfigure}[t]{0.37\textwidth}
     \centering
     \includegraphics[width=\textwidth]{fig/23/23b4.eps}
    \end{subfigure}
    \caption{ALE: Realisations of increasing $\Delta$ with fixed $M$}
    \label{fig:2_3_b2}
\end{figure}
\subsection{ANC vs ALE}
The adaptive noise cancellation (ANC) applied different input signal $\mathbf u(n)$ with correlated noise signal $\epsilon(n)$ whose aim is to estimate the noise $\eta(n)$. Thus, the desired signal $\hat x(n)$ is obtained by subtraction. The correlated secondary signal is assumed as $\epsilon(n) = 0.7\eta(n) + 0.01$. Fig.\ref{fig:2_3_c} illustrated the performance of ANC and ALE. At the beginning of the estimation, the noise is quite large. With the increasing of time index, the noise is almost eliminated. Therefore, the MSPE of ANC is 0.1098 which is approximately one third of the ALE. Observing the average plot, the ANC estimation is equal to the actual sine wave after 200 samples. Thus, the ANC has a high performance than the ALE.
\begin{figure}[htb]
    \centering
    \hspace{-0.4cm}
    \begin{subfigure}[b]{0.33\textwidth}
     \centering
     \includegraphics[width=1.1\textwidth]{fig/23/23c1.eps}
    \end{subfigure}
    \hspace{-0.1cm}
    \begin{subfigure}[b]{0.33\textwidth}
     \centering
     \includegraphics[width=1.1\textwidth]{fig/23/23c2.eps}
    \end{subfigure}
    \hspace{-0.1cm}
    \begin{subfigure}[b]{0.33\textwidth}
     \centering
     \includegraphics[width=1.1\textwidth]{fig/23/23c3.eps}
    \end{subfigure}
    \caption{Performance of ANC and ALE}
    \label{fig:2_3_c}
\end{figure}
\subsection{ANC for EEG data}
In order to remove the strong component at $50Hz$, a noisy sine wave $\epsilon(n)$ with corresponding frequency is synthesised. Fig.\ref{fig:2_3_d1} shows the spectrogram of the original \texttt{POz} data with the rectangular window length of $2^{12}$ and 0.5 overlapping. There is an distinct line in yellow at $50Hz$ which should be removed.
\begin{figure}[htb]
    \centering
    \includegraphics[width=0.35\textwidth]{fig/23/23d1.eps}
    \caption{EEG: Original Spectrogram}
    \label{fig:2_3_d1}
\end{figure}\\
Fig.\ref{fig:2_3_d2} shows the effects ANC by learning rate $\mu$ and $M$. When increasing the filter order $M$, the noisy component is getting to be removed more effectively. A under-modelling with small order causes residual of $50Hz$ component, while over-modelling results in excessively elimination. As to the $\mu$, large learning rate will affect the component around $50Hz$, leading to the attenuation of interest signal.
\begin{figure}[htb]
    \centering
    \hspace{-0.4cm}
    \begin{subfigure}[b]{0.32\textwidth}
     \centering
     \includegraphics[width=\textwidth]{fig/23/23d2.eps}
    \end{subfigure}
    \hspace{-0.4cm}
    \begin{subfigure}[b]{0.32\textwidth}
     \centering
     \includegraphics[width=\textwidth]{fig/23/23d3.eps}
    \end{subfigure}
    \hspace{-0.4cm}
    \begin{subfigure}[b]{0.32\textwidth}
     \centering
     \includegraphics[width=\textwidth]{fig/23/23d4.eps}
    \end{subfigure}
    \\
    \hspace{-0.4cm}
    \begin{subfigure}[b]{0.32\textwidth}
     \centering
     \includegraphics[width=\textwidth]{fig/23/23d5.eps}
    \end{subfigure}
    \hspace{-0.4cm}
    \begin{subfigure}[b]{0.32\textwidth}
     \centering
     \includegraphics[width=\textwidth]{fig/23/23d6.eps}
    \end{subfigure}
    \hspace{-0.4cm}
    \begin{subfigure}[b]{0.32\textwidth}
     \centering
     \includegraphics[width=\textwidth]{fig/23/23d7.eps}
    \end{subfigure}
    \\
    \hspace{-0.4cm}
    \begin{subfigure}[b]{0.32\textwidth}
     \centering
     \includegraphics[width=\textwidth]{fig/23/23d8.eps}
    \end{subfigure}
    \hspace{-0.4cm}
    \begin{subfigure}[b]{0.32\textwidth}
     \centering
     \includegraphics[width=\textwidth]{fig/23/23d9.eps}
    \end{subfigure}
    \hspace{-0.4cm}
    \begin{subfigure}[b]{0.32\textwidth}
     \centering
     \includegraphics[width=\textwidth]{fig/23/23d10.eps}
    \end{subfigure}
    \caption{ANC POz Spectrogram: Effect of $M$ and $\mu$}
    \label{fig:2_3_d2}
\end{figure}\\
Fig.\ref{fig:2_3_d3} shows the periodograms of original and ANC data at $\mu=0.0001$ and $M=10$. Only the component at $50Hz$ is suppressed and others are nearly same compared with original signal.
\begin{figure}[htb]
    \centering
    \hspace{-0.4cm}
    \begin{subfigure}[b]{0.35\textwidth}
     \centering
     \includegraphics[width=\textwidth]{fig/23/23d11.eps}
    \end{subfigure}
    \hspace{-0.4cm}
    \begin{subfigure}[b]{0.35\textwidth}
     \centering
     \includegraphics[width=\textwidth]{fig/23/23d12.eps}
    \end{subfigure}
    \caption{Periodograms and Squared error of Original EEG and de-noising EEG}
    \label{fig:2_3_d3}
\end{figure}


\section{Model Description}
The radiation pressure module contains two different models for calculating the effects of solar radiation pressure on spacecraft state. Both methods of calculating solar radiation pressure have simple implementations in Basilisk using basic coefficients and assumptions. The methods of arriving at these coefficients can be complex and making the coefficients time-varying to improve accuracy can greatly increase complexity. The cannonball method used here essentially follows the mathematics described by Vallado\cite{vallado2001}.

\subsection{Radiation Pressure Model}
Radiation is modeled by using the solar flux at one astronomical unit and scaling by distance from the sun relative to 1 AU. The solar flux at one AU is taken as :
\begin{equation}
SF_{\mathrm{AU}} = 1372.5398    \bigg[\frac{W}{m^2}\bigg]
\end{equation}
\subsubsection{Cannonball Method}
The cannonball model assumes the spacecraft is a simple sphere.  It is the default SRP model when the {\tt RadiationPressure} module is invoked.  The radiation pressure at 1AU, $p_{SR}$, can be taken as the solar flux divided by the speed of light.
\begin{equation}
	p_{SR} = \frac{SF_{\mathrm{AU}}}{c} \bigg[\frac{N}{m^2}\bigg]
\end{equation}
Then, a ``scaling factor'' can be determined. This ``scaling factor" is equivalent to the magnitude of the solar radiation force divided by the distance between the spacecraft and the sun:
\begin{equation}
	\frac{|\mathbf{F}_{\textrm{radiation}}|}{|\mathbf{r}_{\textrm{sun}}|} = \frac{-c_{R}p_{SR}A_{\odot}{AU}^2}{|\mathbf{r}_{\textrm{sun}}|^3} \bigg[\frac{N}{m}\bigg]
\end{equation}
$\mathbf{r}_{\textrm{sun}}$ is the vector from the spacecraft to the sun in the spacecraft body frame and $c_R$ is the reflectivity. This factor is then multiplied by the position vector from the spacecraft to the sun to get the force on the spacecraft due to solar radiation pressure.
\begin{equation}
	{\mathbf{F}_{\textrm{radiation}}} = \frac{|\mathbf{F}_{\textrm{radiation}}|}{|\mathbf{r}_{\textrm{sun}}|}  \mathbf{r}_{\textrm{sun}} [N]
\end{equation}
The user must provide the coefficient of reflection and the equivalent area of the spacecraft to use this method.\\
\subsubsection{Table Look-up Method}
For the table look-up method, pre-determined values of torque and force acting on the spacecraft due to radiation pressure are given. It is required that these values be given at 1AU from the sun and with a corresponding direction vector from the spacecraft to the sun in the spacecraft body frame.\\\\
The look-up works by finding the direction vector in the given tables which most closely matches the current sun heading vector in the body frame. This is done by taking the maximum of the dot products of each lookup vector entry with the sun heading vector in the body frame. As a visual demonstration, \ref{fig:lookupMethod} shows that for some current sun heading amongst the body vector entries 1 through 8, the force and torque data corresponding to entry 8 would be chosen due to its proximity to the current sun heading.
\begin{figure}[H]
	\centerline{
		\includegraphics[height=0.5\textwidth, keepaspectratio]{Figures/lookupDiagram}}
	\caption{Visual Description of Table Look-up Method}
	\label{fig:lookupMethod}
\end{figure}

Then, the corresponding force and torque values are taken from the table and scaled according to the magnitude of the spacecraft-sun position vector:
\begin{equation}
{\mathbf{F}_{\textrm{radiation,scaled}}} = {\mathbf{F}_{\textrm{radiation}}}{\Big(  \frac{AU}{|\textbf{r}_{\textrm{sun}}|}  \Big)}^{2} [N]
\end{equation}
\begin{equation}
{\bm{\tau}_{\textrm{radiation,scaled}}} = {\bm{\tau}_{\textrm{radiation}}}{\Big(  \frac{AU}{|\textbf{r}_{\textrm{sun}}|}  \Big)}^{2} [Nm]
\end{equation}\\\\
Most important to the user of the table look-up method is the required input and format of data. Data must be recorded in XML format. As an example, see ../cube\_lookup.xml (in the radiation pressure folder). Additionally, a utility script called parseSRPLookup.py is provided there to read the XML input into numpy arrays. Experienced users are welcome to store their data in their own format and load it into equivalent numpy arrays as they see fit.\\\\
An example of using the provided python script to load data is shown in test\_radiationPressure.py. Note that this also requires import of the unitTestSupport library.\\
\subsubsection{Solar Eclipses}
Solar eclipses are are detected by the basilisk eclipse module. The effects of the eclipse are calculated into a shadow factor, $F_{\mathrm{s}}$, which is applied to the output forces and torques.
\begin{equation}
\mathbf{F}_{\mathrm{out}} = F_{\mathrm{s}}\mathbf{F}_{\mathrm{full\_sun}}
\end{equation}
\begin{equation}
\bm{\tau}_{\mathrm{out}} = F_{\mathrm{s}}\bm{\tau}_{\mathrm{full\_sun}}
\end{equation}


\problemname{Small Schedule}

%% Image URL: https://www.pexels.com/photo/bandwidth-close-up-computer-connection-1148820/
%% Image License: https://www.pexels.com/photo-license/

\illustration{0.33}{computer.jpg}{~}

Everybody is into cloud computing these days, so quite a few different business models are being experimented with. You are trying a very simple one: you sell time on your machines in one of two batches called {\em slots}. A customer can buy one second of CPU time or $Q$ seconds for some integer $Q$.

Each time slot a customer purchases must be completed on a single machine, but you get to decide how to allocate the purchased time slots between machines.

After coming back from a long vacation, you see that all of your machines are idle and a variety of orders have come in. To keep customers happy, you must decide how to distribute these requests between machines in a way that minimizes the time when the purchased time slots are finally all completed.

What is the smallest amount of time in which you can complete all of the purchased time slots?

\section*{Input}

The input consists of a single line containing four integers $Q$~($2 \leq Q \leq 1\,000$), which is the time needed to complete the longer batches, $M$~($1 \leq M \leq 1\,000\,000$), which is the number of machines owned by your company, $S$~($0 \leq S \leq 1\,000\,000$), which is the number of 1-second time slots purchased, and $L$~($0 \leq L \leq 1\,000\,000$), which is the number of $Q$-second time slots purchased.

\section*{Output}

Display the smallest amount of time in which you can complete all of the purchased time slots.


\documentclass[12pt]{article}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{pgfplots}
\usepackage{mathtools}
\usepackage{booktabs}
\usepackage{indentfirst}

\usetikzlibrary{angles, quotes}

\pgfplotsset{compat=newest}

\title{Complex Analysis}
\author{Linxuan Ma}

\begin{document}
	\maketitle

	\newcommand{\mo}[1]{\lvert #1 \rvert}
	\newcommand{\mos}[1]{\lvert #1 \rvert^2}
	\newcommand{\RR}{\mathbb{R}}
	\newcommand{\p}{\partial}

	\abstract{Convex analysis is the domain of mathematics that investigates functions of complex numbers. Despite its frequent occurrence in applied science, IB Mathematics seems to decide on not covering anything other than "haha $i^2$ is $-1$", so here's my attempt at creating notes for it after binging an entire semester of complex analysis in one night and getting sick immediately the next day due to sleep deprivation. Balanced IB life.}

	\section{Fundamentals}
	A complex number $z \in \mathbb{C}$ is a number of the form $$a + bi$$ where $a$ and $b$ are both real numbers.

	\subsection{Definitions}
	The imaginary number $i$ is defined as $\sqrt{-1}$. Due to its equivalence with $\sqrt{1}$, $i$ obeys all arithmetic laws that apply to root terms. Similarly, it coerces into $-1 \in \RR$ in the case of $i^2$.

	Due to the encapsulation of $z \in \mathbb{C}$ over real numbers $a$ and $b$, a complex function $f: \mathbb{C} \to \mathbb{C}$ is isomorphic to $g: \RR^2 \to \RR^2$. Trivially, by decomposition we obtain
	\begin{gather*}
		\Re: \mathbb{C} \to \RR \\
		\Im: \mathbb{C} \to \RR
	\end{gather*}
	corresponding to retrieving real components $a$ and $b$.

	Deriving from $z \in \mathbb{C}$'s correspondence to a vector $\vec{z} \in \RR$, the representation of a 2-dimensional coordinate can be concluded from $x$:

	\begin{equation*}
		\begin{bmatrix}
			\Re(z) \\ \Im(z)
		\end{bmatrix}
	\end{equation*}

	From such geometric representation, further vector-like properties can be defined (conjugate, modulus and argument) for $z \in \mathbb{C}$ with real part $a$ and imaginary part $b$:

	\begin{gather*}
		\overline{z} = a - bi \\
		\mo{z} = \sqrt{a^2 + b^2} \\
		\arg z = \arctan \frac{b}{a}
	\end{gather*}

	Note the isomorphism of the modulus-argument pair with the standard $a + bi$ form under the domain $-\pi < \theta \leq \pi$ for argument. In the remaining portion of this note, a complex number may take any of the following form:

	\begin{enumerate}
		\item Regular form: $a + bi$ where $a, b \in \RR$
		\item Tuple form (equivalent to $a + bi$): $(a, b)$
		\item Polar form (modulus $r$ and argument $\theta$): $r(cos \theta + i \sin \theta)$
	\end{enumerate}

	\subsection{Complex Arithmetic}

	Trivially, due to the nature of square roots:

	\begin{gather*}
		(a, b) + (c, d) = (a + c, b + d) \\
		(a, b) - (c, d) = (a - c, b - d) \\
		(a, b) * (c, d) = (ac - bd, ad + bc) \\
		\frac{(a, b)}{(c, d)} = \frac{(a, b)(c, -d)}{(c, d)(c, -d)}
	\end{gather*}

	Conjugation is distributive over addition, subtraction, multiplication and division:

	\begin{gather*}
		\overline{z + w} = \overline{z} * \overline{w} \\
		\overline{z - w} = \overline{z} * \overline{w} \\
		\overline{z * w} = \overline{z} * \overline{w} \\
		\overline{\left(\frac{z}{w}\right)} = \frac{\overline{z}}{\overline{w}}
	\end{gather*}

	It is clear that the complex conjugation $z \to \overline z$ is an automorphism of $\mathbb{C}$ (an isomorphic endofunctor).

	Trivially, $z * \overline z \in \RR$.

	Rules regarding the polar representation can be trivially obtained via substitution of definitions:

	\begin{gather*}
		\mo{z * w} = \mo{z} * \mo{w} \\
		\arg(z * w) = \arg z + \arg w
	\end{gather*}

	Modulus can be viewed as the distance of the represented point to the origin, and thereby the rule:

	\begin{gather*}
		\mo{z * w} \leq \mo{z} * \mo{w}
	\end{gather*}

	\subsection{Simple Applications}

	The following section explores rudimentary applications we can derive from the previously covered rules of complex numbers.

	\subsubsection{Magma of Sums of Squares Under Multiplication}

	Consider the number theory problem:
	\begin{center}
		Which integers are sums of two squares?
	\end{center}

	With complex numbers, we realize that the integers that are the sum of two squares form a magma under multiplication.
	\\\\
	\textbf{Proof.} There exists an rearrangement of terms in a product of sums of 2 squares such that: $$(a^2 + b^2)(c^2 + d^2) \to (ac - bd)^2 (ad + bc)^2$$
	The above transformation is derived from the complex number rules related to complex modulus:

	\begin{gather*}
		a^2 + b^2 = \mos{a + ib} \\
		c^2 + d^2 = \mos{c + id}
	\end{gather*}
	Therefore $(a^2 + b^2)(c^2 + d^2)$ can be written as:

	\begin{align*}
		  & (a^2 + b^2)(c^2 + d^2) \\
		= & \mos{(a + ib)(c + id)} \\
		= & \mos{ac - bd + i(ad + bc)} \\
		= & (ac - bd)^2 + (ad + bc)^2
	\end{align*}

	Extending from the proof above, consider two given sum of squares:

	\begin{gather*}
		5 = 1^2 + 2^2 \\
		13 = 2^2 + 3^2
	\end{gather*}

	By multiplication we obtain: $$5 * 13 = 65$$

	Note that $65$ can also be represented as different sums of two squares: $1^2 + 8^2 = 4^2 + 7^2 = 65$. By writing the constituent of $5$ and $13$ as complex modulus, we obtain:

	\begin{gather*}
		\mos{1 + 2i} = 5 \\
		\mos{2 + 3i} = 13
	\end{gather*}

	By multiplying $1 + 2i$ and $2 + 3i$ and conjugating one of them, we obtain:

	\begin{gather*}
		(1 + 2i)(2 + 3i) = -4 + 7i \\
		(1 + 2i)(2 - 3i) = 8 + i
	\end{gather*}
	corresponding to the other two solution $1^2 + 8^2 = 65$ and $4^2 + 7^2 = 65$.

	\subsubsection{Pythagorean Triples}

	Another application of complex arithmetic relates to the Pythagorean theorem, easily generating a set of Pythagorean triples from complex numbers. Consider the Pythagorean theorem of edge $a, b, c$ where $c \leq a + b$ in a right triangle: $$a^2 + b^2 = c^2$$

	Consider the modulus of an arbitrary complex number with integer real and imaginary parts, i.e. $a + bi$ where $a, b \in \mathbb{Z}$, then its modulus can be expressed as a single square root of an integer. Therefore, the modulus of the square of any $a + bi$ of integer coordinate is an integer:
	\begin{align*}
		\mo{a + bi} &= \sqrt{a^2 + b^2} \\
		(a + bi)^2 &= a^2 - b^2 + (2ab)i  \\
		\mo{(a + bi)^2} &= \mo{a + bi} * \mo{a + bi} \\
		& = a^2 + b^2
	\end{align*}

	Therefore, we obtain a generalized formula of the Pythagorean triples over the complex plane for any integer substitution of $a$ and $b$:
	\begin{align*}
		a &= a^2 - b^2 \\
		b &= 2ab \\
		c &= a^2 + b^2
	\end{align*}

	\subsubsection{Quaternions}

	A feasible expansion of the complex number system into $\RR^3$ has yet to be proposed. However, there exists a $\RR^4$ expansion, the quaternions, of the complex plane.

	In the quaternion system, a complex number $a + bi$ is expanded into $a + bi + cj + dk$, where the multiplication of $i$, $j$ and $k$ is not communicative:
	\begin{gather*}
		i^2 = j^2 = k^2 = ijk = -1 \\
		ij = k = -ji \\
		jk = i = -kj \\
		ki = j = -ik
	\end{gather*}

	Similar to finding the inverse of a complex number $a + bi$ (multiplying nominator and denominator by its conjugate):
	$$\frac{1}{a + bi} = \frac{a - bi}{a^2 + b^2}$$
	, there also exists an inverse for a quaternion:
	$$\overline{a + bi + cj + dk} = a - bi - cj - dk$$

	The product of a quaternion with its conjugate is:
	\begin{align*}
		z\overline{z} = a^2 + b^2 + c^2 + d^2
	\end{align*}

	Note that the cross terms (such as $bcij$ and $bcji$) in the above equation vanish due to the non-commutativity ($ij$ = $-ji$).

	The inverse of a quaternion can thus be found:
	$$\frac{1}{a + bi + cj + dk} = \frac{a - bi - cj - dk}{a^2 + b^2 + c^2 + d^2}$$

	All none-zero quaternions have inverses, similar to complex numbers.

	\section{Roots}

	Finding roots of complex numbers is significant in complex analysis. The square root of $x \in \RR$ can be represented as a complex number, such as:
	\begin{align*}
		\sqrt{10} &= \sqrt{10} + 0i \\
		\sqrt{-5} &= \sqrt{5}i
	\end{align*}

	Due to the complications of working with the cartesian form of complex numbers in multiplication and division, the majority of calculations below feature $z \in \mathbb{C}$ in the exponential form, namely $re^{i\theta}$. This trivially corresponds to the polar form $r(\cos \theta + i\sin \theta)$ as dicussed previously.

	The conversion from cartesian to polar can be roughly described as (neglecting the domain restrictions):
	\begin{align*}
		r &= \mo{z}
	\end{align*}

	\subsection{Geometric Interpretation of Multiplication}

	To better represent multiplication over the complex plane, we shall express multiplication in complex number's polar
	\begin{center}
		\begin{tikzpicture}
			\coordinate (o) at (0, 0);
			\coordinate (a) at (1, 0);
			\coordinate (b) at (2, 1);

			\draw[thin,gray!40] (-3, -3) grid (3, 3);
			\draw[->] (-3, 0)--(3, 0) node[right]{Real};
			\draw[->] (0, -3)--(0, 3) node[above]{Imaginary};
			\draw[line width=2pt,blue,-stealth](0, 0)--(2, 1) node[anchor=south west]{$a + bi$};

			\pic [draw, -, "$\theta$", angle eccentricity=2] {angle = a--o--b};
		\end{tikzpicture}
	\end{center}

	From the above illustration we can conclude that:
	\begin{align*}
		z &= a + bi \\
		r &= \mo{z} \\
		x &= r * \cos \theta \\
		y &= r * \sin \theta \\
		\frac{y}{x} &= \tan \theta
	\end{align*}

	Consider the set of complex numbers $S^1$ ($S$ for sphere, and $1$ for 1-dimensional) whose modulus is 1. This forms a group over multiplication, i.e:
	\begin{gather*}
		\forall z_1, z_2 \in S^1,\ \mo{z_1} = \mo{z_2} = 1 \\
		z_1 * z_2 = 1 \\
		\mo{z_1^{-1}} = 1
	\end{gather*}

	Note that every complex number can be written in $r * z_i$ for a specific $z_i \in M$. In other words, there exists a unique isomorphism between any non-zero complex number and a $z_i$ scaled by a constant $r$: $$a + bi \cong r * z_i \ \text{for}\ a, b, r \in \RR,\ z_i \in S^1$$

	$\mathbb{C}^*$ (non-zero complex number) is the product of $\RR_{>0} \times S^1$, and thus $$r_1 e^{i\theta_1} * r_2 e^{i\theta_2} = r_1r_2e^{i(\theta_1 + \theta_2)}$$
	can be easily deduced. This also hints complex number's proficiency at representing rotations of $\RR^2$ (and therefore the existence of quaternions to describe rotations in $\RR^3$).

	\textbf{Warning:} When multiplying complex numbers on a computer, care must be taken to map the polar angles to $(-\pi, \pi]$ when converting from cartesian form to polar form.

	\subsection{Square Root of $z \in \mathbb{C}$}

	With the aforementioned geometric representation of complex multiplication, $\sqrt{z}$ for $z \in \mathbb{C}$ can be trivially obtained as:
	\begin{gather*}
		\sqrt{z} = \sqrt{re^{i\theta}} = \sqrt{r}e^{i\frac{\theta}{2}}
	\end{gather*}

	There exists two solutions for the square root of a complex number. Therefore, in order to preserve continuity, we realize that $\sqrt{e^{i\pi}}$ and $\sqrt{e^{-i\pi}}$ should be equal, yet the continuity of the square root function insists that:
	\begin{align*}
		\sqrt{e^{i\pi}} &= i \\
		\sqrt{e^{-i\pi}} &= -i
	\end{align*}

	There exists no solution to this inequality; however, since we normally only consider the domain $(-\pi, \pi]$, this inconsistency is not \textit{that} significant.

	\subsection{$n$-th Root of $z \in \mathbb{C}$}

	Consider the complex number $3 + 4i$ and its fifth root. Its modulus is $5$, and therefore $\mo{\sqrt[5]{3 + 4i}} = 5$. Interestingly, there exists multiple solutions for $\arg \sqrt[5]{3 + 4i}$, as:
	$$\frac{\theta}{5},\ \frac{\theta + 2\pi}{5},\ \frac{\theta + 4\pi}{5},\ \dots$$
	are all valid solutions to $\arg \sqrt[5]{3 + 4i}$ (since the original $3 + 4i$ can also be represented with argument of $\theta$, $\theta + 2\pi$, $\theta + 4\pi$, etc). This behavior is distinct from taking an odd root of a real number, and therefore introduces complications.

	Interestingly, the argument of the fifth root of $3 + 4i$, when plotted on the complex plane, forms a pentagon:
	\begin{center}
	\begin{tikzpicture}
		\coordinate (o) at (0, 0);
		\coordinate (a) at (1, 0);
		\coordinate (b) at (2, 1);

		\draw[thin,gray!40] (-3, -3) grid (3, 3);
		\draw[->] (-3, 0)--(3, 0) node[right]{Real};
		\draw[->] (0, -3)--(0, 3) node[above]{Imaginary};

		\draw[red, very thick] (0, 0) circle [radius=1.380];
		\node[red] at (-1.5, 1.75) {$\mo{z} = \sqrt{5},\ z \in \mathbb{C}$};

		\draw[line width=2pt, blue, -stealth](0, 0)--(1.36, 0.254) node[anchor=south west]{$\sqrt{5}e^{i\frac{\theta}{5}}$};
		\draw[line width=2pt, blue, -stealth](0, 0)--(0.177, 1.369) node[anchor=south west]{$\sqrt{5}e^{i\frac{\theta + 2\pi}{5}}$};
		\draw[line width=2pt, blue, -stealth](0, 0)--(-1.247, 0.591) node[anchor=south east]{$\sqrt{5}e^{i\frac{\theta + 4\pi}{5}}$};
		\draw[line width=2pt, blue, -stealth](0, 0)--(-0.948, -1.003) node[anchor=north east]{$\sqrt{5}e^{i\frac{\theta + 6\pi}{5}}$};
		\draw[line width=2pt, blue, -stealth](0, 0)--(0.661, -1.211) node[anchor=north west]{$\sqrt{5}e^{i\frac{\theta + 8\pi}{5}}$};
	\end{tikzpicture}
	\end{center}

	This pattern generalizes across the $n$-th roots of a complex number, forming a regular $n$-sided polygon around the origin with radius of $\sqrt[n]{\mo{z}}$.

	Therefore, any $z \in \mathbb{C}$ has $n$-th root. Inductively, the polynomial $z^n - a$ always has a root.

	For more complicated polynomials there also exists a complex root,  but won't be proved in this section: $$z^n + a_{n-1}z^{n - 1} + a_{n - 2}^2z^{n - 2} + \dots + a_1z + a_0 = 0$$

	$\mathbb{C}$ is therefore "algebraically closed".

	Recall the formula:
	\begin{align*}
		\cos 2\theta &= \cos^2 \theta - sin^2 \theta \\
		\cos 3\theta &= \cos^3 \theta - 3\cos \theta \sin^2 \theta
	\end{align*}
	and so on. The above formula can be obtained by the binomial theorem:
	\begin{gather*}
		\cos(n\theta) + i\sin(n\theta) = (\cos \theta + i \sin\theta)^n
	\end{gather*}

	The above formula correspond to the the geometric interpretation of complex multiplication: addition of the arguments corresponds to multiplication of the numbers (given that modulus is $1$).

	The right hand side can be expanded via the binomial theorem, resulting in:
	\begin{gather*}
		\cos^n \theta + in\cos^{n - 1}\theta\sin\theta - \binom{n}{2} \cos^{n - 2}\theta \sin^2 \theta + \dots
	\end{gather*}
	which, after applying the above to $\Re$, will equal to the original $\cos(n\theta)$.

	\section{Exp, Log and Trigonometry}

	The exponential function can be defined as a power series that converges:
	$$\exp(z) = 1 + z + \frac{z^2}{2!} + \frac{2^3}{3!} + \dots$$

	If a series of complex numbers $a_0 + a_1 + a_2 + \dots$ is convergent, then $\Re(a_0) + \Re(a_1) + \dots$ and $\Im(a_0) + \Im(a_1) + \dots$ also converges due to $\mo{\Re(z)} \leq \mo{z}$ and $\mo{\Im(z)} \leq \mo{z}$.

	\subsection{The Exponential Function}
	Properties of the exponential function includes:
	\begin{gather*}
		\exp(z_1 + z_2) = \exp(z_1) * \exp(z_2)
	\end{gather*}
	which can be proven with:
	\begin{align*}
		\sum_{n=0}^\infty \frac{(z_1 + z_2)^n}{n!} &= \sum_{n, m=0}^\infty \frac{\binom{n}{m}z_1^m z_2^{n - m}}{n!} \\
		&= \sum_{n, m=0}^\infty \frac{z_1^m}{m!}*\frac{z_2^{n-m}}{(n-m)!} \\
		&= \sum_{n, m=0}^\infty \frac{z_1^m}{m!}*\frac{z_2^n}{n!} \\
		&= \exp(z_1) * \exp(z_2)
	\end{align*}

	Defining $z \in \mathbb{C}$ as $z = a + bi$, $e^z = e^{a + bi} = e^a*e^{bi}$. Note that the left section $e^a \in \RR$, while $e^{bi} \in \mathbb{C}$. We can rewrite this as:
	\begin{align*}
		\exp(bi) &= 1 + bi + \frac{b^2i^2}{2!} + \frac{b^3i^3}{3!} + \dots \\
		&= 1 + bi - \frac{b^2}{2!} - \frac{-b^3i}{3!} + \frac{b^4}{4!} + \dots
	\end{align*}

	Note that the series $1,\ -\frac{b^2}{2!},\ \frac{b^4}{4!},\ \dots$ is just the series for $\cos(b)$, while the series $bi,\ -\frac{b^3i}{3!},\ \frac{b^5i}{5!},\ \dots$ is the series for $i * \sin(b)$.

	Therefore, Euler's identity can be deduced from the above equation:
	\begin{align*}
		e^{i\theta} &= \cos \theta + i \sin \theta \\
		e^{i\pi} &= -1 \\
		e^{2i\pi} &= 1 \\
		e^{2ni\pi} &= 1,\ n \in \mathbb{Z}
	\end{align*}

 	Exponentials can also be viewed as a homomorphism of groups (note that the mapping is surjective but not injective): $$\exp(z_1 + z_2) = \exp(z_1)\exp(z_2)$$
 	from complex numbers under addition to complex numbers under multiplication (in polar coordinates): $$\mathbb{C} \xrightarrow{\exp} \mathbb{C}^*$$

	The above is satisfied if the modulus of the object being mapped is non-zero. In addition, observe that:
	\begin{align*}
		r(\cos \theta + i\sin \theta) &= r * \exp(i\theta) \\
		&= \exp(\ln r) * \exp(i\theta) \\
		&= \exp(\ln r + i\theta)
	\end{align*}
	therefore the homomorphism is satisfied for $r \neq 0$.

	Incidentally, there also exists a homomorphism of real numbers $\RR$ under addition to $\RR$ under multiplication that is injective yet not surjective (trivial).

	\subsection{The Logarithm Function}

	The $\ln$ function is the inverse of the exponential function e.g. solving for $a$ and $b$ in $\exp(a + bi) = z$ becomes $a + bi = \ln z$. Since the polar representation of $a + bi$ is $r(\cos \theta + i\sin \theta)$, it can be deduced that:
	\begin{align*}
		\exp(a) &= r = \mo{z} \\
		\theta &= \arg z
	\end{align*}

	Note that $\ln(z)$ is not unique (only defined up to multiples of $2\pi i$). The logarithm function can't be defined continuously for all non-zero complex numbers (due to the ambiguity in the argument of the complex number):
	$$z_1^{z_2} = \exp(z_2 * \ln(z_1))$$
	is only well-defined when:
	\begin{enumerate}
		\item $z_1 > 0$ and $z_1 \in \RR$ (trivial)
		\item $z_2 \in \mathbb{Z}$ (ambiguity is unimpactful as $z_1^{z_2} = 1$)
	\end{enumerate}

	\subsection{Trigonometric Functions}

	The $\cos$ function and $\sin$ function are just special cases for the exponential function:
	\begin{align*}
		e^{iz} &= \cos z + i\sin z \\
		e^{-iz} &= \cos z - i\sin z \\
		\frac{e^{iz} + e^{-iz}}{2} &= \cos z
	\end{align*}

	The same conclusion is arrived for defining $\cos$ as the power series derived from the definition (in series form) of the exponential function.

	Similarly: $$\frac{e^{iz} + e^{-iz}}{2} = \cos z$$

	In addition, identities of the trigonometric functions follow the identity of the exponential function:
	\begin{align*}
		\cos(\theta_1 + \theta_2) &= \cos \theta_1 \cos \theta_2 - \sin \theta_1 \sin \theta_2 \\
		\exp(z_1 + z_2) &= \exp(z_1) * \exp(z_2) \text{ (substitution)}
	\end{align*}

	Note that the $\sin$ and $\cos$ functions are defined over the entire complex plane due to its correspondence with Euler's identity.

	\subsection{Applications of Complex Trigonometry}

	Due to the flexibility of trigonometric functions in the complex plane, there are numerous applications of it. The following section lists a few simple and significant ones.

	\subsubsection{Differential Equation}

	Consider the linear differential equation: $$a\frac{d^2y}{dx^2} + b\frac{dy}{dx} + cy = 0$$
	With complex numbers, the above can be solved trivially:
	\begin{align*}
		y &= e^{\lambda x} \\
		a\lambda^2e^{\lambda x} + b\lambda e^{\lambda x} + ce^\lambda &= 0 \\
		a \lambda^2 + b\lambda + c &= 0
	\end{align*}

	$\lambda$ can be solved with a simple quadratic equation. Consider the example: $$\frac{d^2 y}{dx} + 2\frac{dy}{dx} + 2y = 0$$
	By implementing the concept above, we obtain:
	\begin{align*}
		\lambda^2 + 2\lambda + 2 &= 0 \\
		\lambda = -1 \pm \sqrt{1^2 - 2} &= -1 \pm i \\
		\exp((1 + i)x),\ \exp((1 - i)x) &= y
	\end{align*}

	\subsubsection{Fourier Series}

	With a periodic function $f$:
	\begin{gather*}
		f(x) = f(2\pi + x)
	\end{gather*}
	$f$ can be written as a sum of $sin$ and $cos$ functions (thus simplified with complex numbers):
	\begin{align*}
		f(x) &= \sum_{n > 0} a_n \sin(nx) + \sum_{n \geq 0} b_n \cos{nx} \\
		&= \sum_{n \in \mathbb{Z}} c_n e^{inx}
	\end{align*}

	\subsection{Tangent of $z$}

	The regular difinition for the trigonometric function $\tan$ is $\sin$ divided by $\cos$:
	\begin{gather*}
		\tan z = \frac{\sin z}{\cos z}
	\end{gather*}

	Due to Euler's indetity, $\tan$ can be represented as:
	\begin{gather*}
		\tan z = \frac{1}{i} * \frac{e^{iz} - e^{-iz}}{e^{iz} + e^{-iz}}
	\end{gather*}

	Except near the real axis, the tangent function is almost constant around $\tan z = i$.

	\section{Complex Derivatives}

	Complex derivatives refer to the result of differentiating a complex function.

	\subsection{Real Derivatives of Complex Functions}

	In real analysis, we recall real differentiation: $$f: \RR \to \RR$$

	$f$ is differentiable at $x_0 \in \RR$ if $f$ is approximately linear at $x_0$, i.e. $f(x) = f(x_0) + a(x - x_0)$ where $a$ is the slope at $x$. This is derived from the definition of derivatives.

	An alternative way of defining derivatives (or the more commonly known one) is: $$\lim_{dx \to 0} \frac{f(x) - f(x_0)}{dx}$$ where $dx = x - x_0$.

	Consider $w(z) = u(z) + iv(z)$ is a function of $z = x + yi$, where $u$, $v$, $x$ and $y$ are real values. $u$ and $v$ can be viewed as functions over two real values that resembles the way in which we deduce derivatives for real functions:
	\begin{equation*}
		\begin{bmatrix}
			u(x, y) \\ v(x, y)
		\end{bmatrix} =
		\begin{bmatrix}
			u(x_0, y_0) \\ v(x_0, y_0)
		\end{bmatrix} +
		\begin{bmatrix}
			a & b \\c & d
		\end{bmatrix}
		\begin{bmatrix}
			x - x_0 \\ y - y_0
		\end{bmatrix}  + \epsilon
	\end{equation*}
	where $\epsilon$ denotes the error value as the (small)  cost $$\lim_{(x, y)  \to (x_0, y_0)}\frac{\mo{\epsilon}}{\mo{(x, y) - (x_0, y_0)}} \to 0$$
	of such linear approximation.

	$w$ being differential at $z$ implies that $w$ can be approximated by the above linear function.

	The matrix of $a$, $b$, $c$ and $d$ can be rewritten as a matrix of partial derivatives:

	\begin{equation*}
		\begin{bmatrix}
			\frac{\p u}{\p x} & \frac{\p u}{\p y} \\
			\frac{\p v}{\p x} & \frac{\p v}{\p y}
		\end{bmatrix}
	\end{equation*}

	The above function for $w$ only defines the real differentiability of a function.

	\subsection{Complex Derivatives}

	Consider $w(z) = u(z) + iv(z)$ and $z = x + yi$ in respect of the complex plane: $$w(z) = w(z_0) + A(z - z_0) + \epsilon$$
	where $\epsilon$ is less than linear (same as above), $A \in \mathbb{C}$.

	The transformation by $A$ can be described in the matrix form:
	\begin{equation*}
		\begin{bmatrix}
			\Re(A) & -\Im(A) \\ \Im(A) & \Re(A)
		\end{bmatrix}
		\begin{bmatrix}
			x - x_0 \\ y - y_0
		\end{bmatrix}
	\end{equation*}
	or via direct definition: $$A(x + yi) = x\Re(A) - y\Im(A) + i(x\Im(A) +y\Re(A))$$

	$w$ is only differentiable as a complex function when $A$ is equivalent with the differentiation matrix of $w$ over real values:
	\begin{align*}
		\Re(A) &= \frac{\p u}{\p x} = \frac{\p v}{\p y} \\
		\Im(A) &= \frac{\p v}{\p x} = -\frac{\p u}{\p y}
	\end{align*}

	The above equation is referred to as the \textit{Cauchy-Riemann Equation}, the condition under which a real function is complex differentiable (if and only if).

	In terms of limits, $A$ can be represented as: $$A = \lim_{dz \to 0} \frac{w(z) - w(z_0)}{dz},\ dz = z-z_0$$

	\subsection{Holomorphic Functions}

	Suppose $w$ is a complex function of $z \in U \subseteq \mathbb{C}$ where $U$ is an open set, $w$ is \textit{holomorphic} if it is complex differentiable for all points on $U$ (therefore continuous and holds real derivatives that satisfies the Cauchy-Riemann equation). A holomorphic function is also infinitely differentiable. The concept of holomorphic function is crucial to complex analysis.

	Another representation of the Cauchy-Riemann equation is to define the partial derivatives to $z$ and $\overline{z}$ respectively, referred to as the \textit{Wirtinger Derivatives}. The derivatives are defined as:
	\begin{align*}
		\frac{\p}{\p z} &= \frac{1}{2}\left(\frac{\p}{\p x} - i\frac{\p}{\p y}\right) \\
		\frac{\p}{\p \overline{z}} &= \frac{1}{2}\left(\frac{\p}{\p x} + i\frac{\p}{\p y}\right)
	\end{align*}

	The reasoning behind the definition is that:
	\begin{align*}
		\frac{\p z}{\p z} = 1&,\ \frac{\p \overline{z}}{\p z} = 0 \\
		\frac{\p z}{\p \overline{z}} = 0&,\ \frac{\p \overline{z}}{\p \overline{z}} = 1
	\end{align*}

	Informally, holomorphic can be understood as a function that "depends on $z$ but not $\overline{z}$";  however, such statement is meaningless and merely provides an understanding as a function that depends on $z$ is not dependent on $\overline{z}$ (definition of complex conjugation) (more explanations on this in the succeeding sections).

	Suppose $f$ and $g$ are holomorphic on open set $U$, then $f + g$, $f - g$, $f * g$, $f / g$ and $f.g$ are all holomorphic.

	Therefore, the trigonometric functions as well as $\exp$ and $\ln$ are all differentiable over well-defined points. Surprisingly, $\frac{df}{dz}$ is holomorphic if $f$ is holomorphic. This is distinct from the properties of real functions.

	Some examples of functions that are not holomorphic: $\Re(z)$, $\Im(z)$, $\mo{z}$, $\mos{z}$, $\overline{z}$.

	Consider the function $f(z) = z^2$. $f$ is holomorphic in $\mathbb{C}$, and can be shown with the Cauchy-Riemann equation. We start by rewriting $f$:
	\begin{align*}
		f(z) &= z^2 = (x + yi)^2 \\
		&= x^2 + 2xyi - y^2 \\
		&= (x^2 + y^2) + i(2xy)
	\end{align*}

	Recall the Cauchy-Riemann equation:
	\begin{align*}
		f(z) &= u(x, y) + iz(x, y) \\
		\frac{\p u}{\p x} &= \frac{\p v}{\p y} \\
		\frac{\p u}{\p y} &= -\frac{\p v}{\p x}
	\end{align*}

	The resulting equation from transforming $f(z) = z^2$ can be pattern matched with the Cauchy-Riemann equation:
	\begin{align*}
		u(x, y) &= x^2 - y^2 \\
		v(x, y) &= 2xy
	\end{align*}

	Therefore:
	\begin{align*}
		\frac{\p u}{\p x} &= \frac{\p v}{\p y} = 2x \\
		\frac{\p u}{\p y} &= -2y \\
		\frac{\p v}{\p x} &= 2y
	\end{align*}
	The above shows that $f(z) = z^2$  is indeed holomorphic.

	\section{Harmonic Functions}

	Recall the definition of complex function $w(z) = u(z) + iv(z)$ where $z$ is complex. Given a function $u$ of $x,\ y \in \mathbb{C}$, there exists no way to find a holomorphic $w$ such that $u(z) = \Re(w(z))$. If $w$ is holomorphic, it satisfies the Cauchy-Riemann equation. Consider the equation after differentiating with respect to $x$:
	\begin{align*}
		\frac{\p}{\p x} \frac{\p u}{\p x} &= \frac{\p}{\p x} \frac{\p v}{\p y} \\
		\frac{\p^2 u}{\p x^2} &= \frac{\p^2 v}{\p x \p y} = \frac{-\p^2 u}{\p y^2}
	\end{align*}

	From the equation it can be concluded that:
	$$\frac{\p^2 u}{\p x^2} + \frac{\p^2 u}{\p y^2} = 0$$

	The above equation is referred to as the \emph{Laplace equation}, and functions that satisfy such equation is a \emph{harmonic equation}. The Laplace equation can be written in terms of the Laplacian operator: $$\nabla(u) = \frac{\p^2 u}{\p x^2} + \frac{\p^2 u}{\p y^2}$$

	To find all harmonic polynomials in the cartesian plane, we start by taking complex polynomials of $z$ and take their real and imaginary part:
	\begin{table}[ht]
	\centering
	\begin{tabular}[t]{lccccc}
	\toprule
		$$ & $1$ & $z$ & $z^2$ & $z^3$ & $\dots$ \\
	\midrule
		$\Re$ & $1$ & $x$ & $x^2 - y^2$ & $x^3 - 3xy^2$ & --\\
		$\Im$ & -- & -- & $2xy$ & $3x^2y - y^3$ & -- \\
	\bottomrule
	\end{tabular}
	\end{table}

	Any linear combination of harmonic polynomials is harmonic. The above table illustrates the basis of all harmonic polynomials.

	As another example, consider the holomorphic function $ze^z$, its real part is $e^x(x*\cos y - y*\sin y)$. From the above, we conclude that for any holomorphic function $w$, $\Re \circ w$ is a harmonic function.

	Sometimes a harmonic function $u$ is also the real part of some holomorphic function $w(z) = u(z) + iv(z)$. This depends on the open set $U \subseteq \mathbb{C}$. Notice the Cauchy-Riemann equation determines $v$ up to constant, as the partial derivative of $v$ with respect to $x$ and $y$ vanishes.

	Suppose real functions $f$ and $g$ each of the domain $x,\ y$. The equation:
	\begin{align*}
		\frac{\p v}{\p x} &= f \\
		\frac{\p v}{\p y} &=g
	\end{align*}
	can only be solved under the condition: $$\frac{\p f}{\p y} = \frac{\p g}{\p x}$$

	As an example, consider rectangle $U$ containing the origin $(0, 0)$, and $v(0, 0) = 0$.

\end{document}


\section{The Fundamental Group}
\subsection{Basic Ideas}
Fix a space $X$ and a point $x_0\in X$.
We consider loops based at $x_0$, i.e. maps $\gamma:I\to X$ with $\gamma(0)=\gamma(1)=x_0$.
\begin{example}
    We can take $X=\mathbb R^2\setminus{(0,0)}$ and $x_0\in X$.
    We can take $\gamma$ to be a loop that doesn't come near $(0,0)$ at all, or one that encloses it.
\end{example}
There can be many loops satisfying these conditions, of course, but is it necessary to consider all of them differently?
For example, we certainly wish to consider two loops to be the same if one can become the other by a ``small perturbation''.\\
To do this, we will define a notion of equivalence relationship characterising this ``equal after perturbation'' condition.
The fundamental group of $X$ at $x_0$, as a set, will be the set of equivalence classes of loops defined in this way.\\
But how would we make this a group?
Certainly, we want a group operation defined there.
Pick two loops starting and finishing at $x_0$, we want to define their product as the loop that first goes through one loop, then the other.
As one expect, this may or may not be commutative, but we do expect it to be associative and has the obvious identity and inverse.
More importantly, we do not yet know if it is well defined on the equivalence class of loops.
So we need some technicalities.
\subsection{Homotopy}
\begin{definition}
    Let $f_0,f_1:X\to Y$ be maps.
    A homotopy between $f_0$ and $f_1$ is a map $F:X\times I\to Y$ such that $F(x,0)=f_0(x)$ and $F(x,1)=f_1(x)$.
    We often write $f_t(x)=F(x,t)$ to represent the interpretation of $F$ as some kind of deformation.\\
    If such a map exists for $f_0,f_1$, we say $f_0$ is homotopic to $f_1$, written as $f_0\simeq_F f_1$ or simply $f_0\simeq f_1$.
\end{definition}
\begin{example}
    If $Y\subset\mathbb R^2$ is convex, then any maps $f_0,f_1:X\to Y$ are homotopic by taking $F(x,t)=tf_0(x)+(1-t)f_1(x)$.
\end{example}
\begin{definition}
    For $f_0,f_1:X\to Y$ and $f_0\simeq_F f_1$, if $Z\subset X$ has the property that $F(z,t)=f_0(z)=f_1(z)$ for any $z\in Z,t\in I$, then we say $f_0\simeq f_1$ relative to $Z$.
\end{definition}
\begin{lemma}
    Let $Z\subset X,Y$ be spaces.
    Then $\simeq$ relative to $Z$ is an equivalence relation on the set of continuous maps $X\to Y$
\end{lemma}
\begin{proof}
    Trivial but let's write it.
    $f_0\simeq f_0$ via $F(x,t)=f_0(x)$ so it is reflexive.
    If $f_0\simeq_F f_1$, then $f_1\simeq_{F'}f_0$ via $F^\prime(x,t)=F(x,1-t)$.\\
    If $f_0\simeq_{F_0}f_1$ and $f_1\simeq_{F_1}f_2$, then $f_0\simeq_F f_2$ via
    $$F(x,t)=\begin{cases}
        F_0(x,2t)\text{, for $t\in[0,1/2]$}\\
        F_1(x,2t-1)\text{, for $t\in[1/2,1]$}
    \end{cases}$$
    whose continuity is guaranteed by the gluing lemma.
\end{proof}
Recall that a map $f:X\to Y$ is a homeomorphism if it is a bijection and has continuous inverse (in addition to it being continuous itself).
We can extend this idea to characterise two spaces being ``homotopically the same''.
\begin{definition}
    A homotopy equivalence between spaces $X,Y$ is a map $f:X\to Y$ such that there exists a map $g:Y\to X$ such that $f\circ g\simeq \operatorname{id}_Y,g\circ f\simeq\operatorname{id}_X$.
    If such a map exists, we say $X$ and $Y$ are homotopy equivalent.
\end{definition}
Obviously homeomorphic spaces are homotopy equivalent, but the converse is not true as we shall see.
\begin{example}
    The letters `$\delta$' and `$o$', as topological spaces, are homotopy equivalent.
    But obviously they are not homeomorphic.
\end{example}
\begin{remark}
    All of the invariants of this course are homotopy invariants, as we will see.
\end{remark}
\begin{example}
    Let $\ast$ be the one-point space and $f:\mathbb R^n\to\ast$ the unique map and $g:\ast\to\mathbb R^n$ constantly $0$.
    Then $f\circ g=\operatorname{id}_\ast\simeq\operatorname{id}_\ast$.
    Now $g\circ f$ is the zero map, which is not the identity but is homotopically equivalent to the identity via $F(x,t)=tx$.
\end{example}
\begin{definition}
    If $X$ is homotopy equivalent to $\ast$, we say $X$ is contractible.
\end{definition}
\begin{example}
    Let $f:S^{n-1}\hookrightarrow\mathbb R^n\setminus\{0\}$be the inclusion and $g:\mathbb R^n\setminus\{0\}\to S^{n-1}$ be $g(x)=x/\|x\|$.
    Then $g\circ f=\operatorname{id}_{S^{n-1}}$.
    Although $f\circ g\neq\operatorname{id}_{\mathbb R^n\setminus\{0\}}$, we can consider
    $$F(x,t)=(1-t)x+t\frac{x}{\|x\|}$$
    which is a homotopy between $f\circ g$ and $\operatorname{id}_{\mathbb R^n\setminus\{0\}}$.
    Therefore $S^{n-1}\simeq \mathbb R^n\setminus\{0\}$.
\end{example}
\begin{definition}
    Let $f:X\to Y,g:Y\to X$ be maps.
    If $g\circ f=\operatorname{id}_X$, we say $X$ is a retract of $Y$ and $g$ is a retraction.\\
    If in addition that $f\circ g\simeq \operatorname{id}_Y$ relative to $f(X)$, then we say $X$ is a deformation retract of $Y$.
\end{definition}
\begin{lemma}
    Homotopy equivalences of spaces is an equivalence relation on spaces.
\end{lemma}
We got a bit imprecise here as we usually learnt equivalence relations on sets, but the collection of all topological spaces is a proper class.
Nevertheless, we can still simply check for reflexivity, symmetry and transitivity.
\begin{proof}
    Reflexivity and symmetry is obvious.
    For transitivity, suppose the maps shown below are homotopy equivalences:
    \[
        \begin{tikzcd}
            X \arrow[bend left]{r}{f} & Y \arrow[bend left]{l}{g} \arrow[bend left]{r}{f'} & Z \arrow[bend left]{l}{g'}
        \end{tikzcd}
    \]
    Then obviously we want to show $f'\circ f$ and $g\circ g'$ are homotopy inverses of each other.
    Suppose $g'\circ f'\simeq_{F'}\operatorname{id}_Y$, then the function
    $$(x,t)\mapsto g\circ F'(f(x),t)$$
    is a homotopy between $g\circ f$ and $g\circ (g'\circ f')\circ f=(g\circ g')\circ (f'\circ f)$.
    Therefore $(g\circ g')\circ (f'\circ f)\simeq g\circ f\simeq \operatorname{id}_X$.
    Using the exact same idea, $(f'\circ f)\circ (g\circ g')\simeq\operatorname{id}_Z$.
\end{proof}
\subsection{Loops and the Fundamental Group}
\begin{definition}
    Let $X$ be a space, a path in $X$ is a map $\gamma:I\to X$.
    It is a path from $x_0$ to $x_1$ ($x_0,x_1\in X$) if $\gamma(0)=x_0$ and $\gamma(1)=x_1$.\\
    A loop based at $x_0\in X$ is a path from $x_0$ to $x_0$.
\end{definition}
\begin{definition}
    Let $\gamma_0,\gamma_1$ be paths from $x_0$ to $x_1$.
    We say $\gamma_0$ and $\gamma_1$ are (path-)homotopic if they are homotopic relative to $\{0,1\}$.\\
    This has been shown to be an equivalence relation.
    We write $[\gamma]$ to denote the equivalence class containing $\gamma$ in the set of all paths from $x_0$ to $x_1$.
\end{definition}
\begin{definition}
    Let $X$ be a space and $x,y,z\in X$.
    Let $\gamma_1$ be a path from $x$ to $y$ and $\gamma_2$ a path from $y$ to $z$.\\
    1. The concatenation $\gamma_1\cdot \gamma_2$ of $\gamma_1$ and $\gamma_2$ is the path from $x$ to $z$ defined by
    $$(\gamma_1\cdot \gamma_2)(t)=\begin{cases}
        \gamma_1(2t)\text{, for $t\in [0,1/2]$}\\
        \gamma_2(2t-1)\text{, for $t\in [1/2,1]$}
    \end{cases}$$
    which is a proper path as it is continuous due to the gluing lemma.\\
    2. The constant path at $x$ is the constant function $c_x:t\mapsto x$.\\
    3. The inverse of a path $\gamma_1$ is a path $\bar\gamma_1$ from $y$ to $x$ defined by $\bar\gamma_1(t)=\gamma_1(1-t)$.
\end{definition}
\begin{theorem}\label{fund_group}
    Let $X$ be a space and $x_0\in X$.
    Write $\pi_1(X,x_0)$ to denote the set of homotopy classes of loops based at $x_0$.
    Then $\pi_1(X,x_0)$ is a group under the operation $[\gamma_1][\gamma_2]=[\gamma_1\cdot\gamma_2]$, with identity $[c_{x_0}]$ and $[\gamma]^{-1}=[\bar\gamma]$.
\end{theorem}
This group is called the fundamental group of $X$.
\begin{lemma}\label{fund_group_well_def}
    If $\gamma_0\simeq\gamma_1$ are paths to $y$ and $\delta_0\simeq \delta_1$ are paths from $y$, then $\gamma_0\cdot\delta_0\simeq\gamma_1\cdot\delta_1$ and $\bar\gamma_0\simeq\bar\gamma_1$.
\end{lemma}
\begin{proof}
    Suppose $\gamma_0\simeq_F\gamma_1,\delta_0\simeq_G\delta_1$.
    Define
    $$H(s,t)=\begin{cases}
        F(2s,t)\text{, for $s\in[0,1/2]$}\\
        G(2s-1,t)\text{, for $s\in[1/2,1]$}
    \end{cases}$$
    Then we immediately have $\gamma_0\cdot\delta_0\simeq_H\gamma_1\cdot\delta_1$.\\
    Now for the inverses, $F'(s,t)=F(1-s,t)$ gives $\bar\gamma_0\simeq_{F'}\bar\gamma_1$.
\end{proof}
\begin{lemma}\label{fund_group_ax}
    Let $x,y,z\in X$.
    If there are paths $\alpha$ from $w$ to $x$, $\beta$ from $x$ to $y$, $\gamma$ from $y$ to $z$.
    Then\\
    1. $(\alpha\cdot\beta)\cdot\gamma\simeq\alpha\cdot(\beta\cdot\gamma)$.\\
    2. $\alpha\cdot c_x\simeq c_x\cdot\alpha\simeq\alpha$.\\
    3. $\alpha\cdot\bar\alpha\simeq c_x$.
\end{lemma}
\begin{proof}
    Note that the composition of any path $\gamma$ and an order-preserving surjection $\phi I\to I$ is homotopic to the original path via $F(s,t)=\gamma(t\phi(s)+(1-t)s)$.
    This is called a reparameterisation.
    Now take $\phi$ to be the function
    $$\phi(t)=\begin{cases}
        t/2\text{, for $t\in[1,1/2]$}\\
        t-1/4\text{, for $t\in[1/2,3/4]$}\\
        2t-1\text{, for $t\in[3/4,1]$}
    \end{cases}$$
    Then, as one can check
    $$(\alpha\cdot\beta)\cdot\gamma\simeq((\alpha\cdot\beta)\cdot\gamma)\circ\phi=\alpha\cdot(\beta\cdot\gamma)$$
    To see $\alpha\cdot c_x\simeq\alpha$, just reparameterise $\alpha$ by
    $$t\mapsto\begin{cases}
        2t\text{, for $t\in [0,1/2]$}\\
        1\text{, for $t\in[1/2,1]$}
    \end{cases}$$
    The other side is analogous.\\
    Indeed $c_x\simeq_F\alpha\cdot\bar\alpha$ via
    $$F(s,t)=\begin{cases}
        \alpha(2s)\text{, for $s\in[0,t/2]$}\\
        \alpha(t)\text{, for $s\in[t/2,1-t/2]$}\\
        \alpha(2-2s)=\bar\alpha(2s-1)\text{, for $s\in[1-t/2,1]$}
    \end{cases}$$
    which can be verified to work.
\end{proof}
\begin{proof}[Proof of Theorem \ref{fund_group}]
    Combining Lemma \ref{fund_group_well_def} and Lemma \ref{fund_group_ax} shows the result immediately.
\end{proof}
\begin{example}
    Consider $X=\mathbb R^n$ and $x_0=0$, then for any loop $\gamma$ based at $x_0$ we have $\gamma\simeq c_{x_0}$ via the straightline homotopy $F(x,t)=(1-t)\gamma(x)+tx_0$.
    Therefore $\pi_1(X,x_0)$ is the trivial group.
\end{example}
Now, as we mentioned in the introduction, we still want a property of this algebraic invariant regarding maps between the relevant objects.
\begin{lemma}
    Let $f:X\to Y$ be a map, $x_0\in X$ and $y_0=f(x_0)$.
    Then there is an induced group homomorphism $f_\ast:\pi_1(X,x_0)\to\pi_1(Y,y_0)$ defined by $f_\ast([\gamma])=[f\circ \gamma]$.
    Further:\\
    1. If $f\simeq f'$ relative to $x_0$, then $f_\ast=f_\ast'$.\\
    2. if $g:Y\to Z$ with $g(y_0)=z_0$ is another map, then $g_\ast\circ f_\ast=(g\circ f)_\ast$.\\
    3. $(\operatorname{id}_X)_\ast=\operatorname{id}_{\pi_1(X,x_0)}$.
\end{lemma}
\begin{proof}
    $f_\ast$ is always well-defined as a function.
    Suppose $\gamma_1\simeq_F\gamma_2$, then we obviously have $f\circ \gamma_1\simeq_{f\circ F}f\circ \gamma_2$.\\
    To see it is a group homomorphism, we observe
    $$f\circ(\gamma_1\cdot\gamma_2)=(f\circ \gamma_1)\cdot(f\circ\gamma_2)\implies f_\ast([\gamma_1\cdot\gamma_2])=f_\ast([\gamma_1])\cdot f_\ast([\gamma_2])$$
    For 1, if $f\simeq_F f'$ relative to $x_0$, then $f\circ\gamma\simeq_{F'} f'\circ\gamma$ via $F'(s,t)=F(\gamma(s),t)$, so $f_\ast([\gamma])=[f\circ\gamma]=[f'\circ\gamma]=f_\ast'([\gamma])$.
    2 and 3 are completely obvious.
\end{proof}
One thing we are not satisfied:
We define the fundamental group with reference to a basepoint.
Is there a way to remove it, at least for path-connected space?
\begin{lemma}\label{indep_basepoint}
    Let $X$ be a space.
    A path $\alpha$ from $x_0$ to $x_1$ induces a group isomorphism $\alpha_\#:\pi_1(X,x_0)\to\pi_1(X,x_1)$ via $\alpha_\#([\gamma])=[\bar\alpha\cdot\gamma\cdot\alpha]$.
    Further:\\
    1. If $\alpha\simeq\alpha'$, then $\alpha_\#=\alpha_\#'$.\\
    2. $(c_{x_0})_\#=\operatorname{id}_{\pi_1(X,x_0)}$.\\
    3. If $\beta$ is a path from $x_1$ to $x_2$, then $(\alpha\cdot\beta)_\#=\beta_\#\circ\alpha_\#$.\\
    4. If $f:X\to Y$ is a map with $y_i=f(x_i)$, then
    $$(f\circ\alpha)_\#\circ f_\ast=f_\ast\circ\alpha_\#$$
\end{lemma}
In short, the path $\alpha_\#(\gamma)$ goes from $x_1$, walk through $\bar\alpha$ to $x_0$, do the loop, then go back to $x_1$ via $\alpha$.
\begin{proof}
    It is very easy to see that $\alpha$ is well-defined.
    To see it is a group homomorphism, observe that for loops $\gamma,\delta$ based at $x_0$, we have
    \begin{align*}
        \alpha_\#(\gamma)\cdot\alpha_\#(\delta)&\simeq(\bar\alpha\cdot\gamma\cdot\alpha)\cdot(\bar\alpha\cdot\delta\cdot\alpha)\\
        &\simeq\bar\alpha\cdot\gamma\cdot(\alpha\cdot\bar\alpha)\cdot\delta\cdot\alpha\\
        &\simeq\bar\alpha\cdot(\gamma\cdot\delta)\cdot \alpha\\
        &\simeq\alpha_\#(\gamma\cdot\delta)
    \end{align*}
    Note also that $\bar\alpha_\#$ has
    $$\bar\alpha_\#\circ\alpha_\#(\gamma)\simeq \alpha\cdot(\bar\alpha\cdot\gamma\cdot\alpha)\cdot\bar\alpha\simeq(\alpha\cdot\bar\alpha)\cdot\gamma\cdot(\alpha\cdot\bar\alpha)\simeq c_{x_0}\cdot\gamma\cdot c_{x_0}\simeq\gamma$$
    for any $\gamma$, so $\bar\alpha_\#$ is inverse to $\alpha_\#$, hence $\alpha_\#$ is indeed a group isomorphism.\\
    1,2,3 are completely trivial.
    For 4, we basically just want
    \[
        \begin{tikzcd}
            \pi_1(X,x_0)\arrow{r}{\alpha_\#}\arrow[swap]{d}{f_\ast}&\pi_1(X,x_1)\arrow{d}{f_\ast}\\
            \pi_1(Y,y_0)\arrow[swap]{r}{(f\circ\alpha)_\#}&\pi_1(Y,y_1)
        \end{tikzcd}
    \]
    to commute.
    To see this,
    \begin{align*}
        ((f\circ\alpha)_\#\circ f_\ast)([\gamma])&=(f\circ\alpha)_\#([f\circ\gamma])\\
        &=[(\overline{f\circ\alpha})\cdot(f\circ\gamma)\cdot (f\circ\alpha)]\\
        &=[f\circ(\bar\alpha\cdot\gamma\cdot\alpha)]\\
        &=f_\ast(\alpha_\#(\gamma))
    \end{align*}
    As we want.
\end{proof}
In particular, the fundamental group does not depend on the basepoint if the space is path-connected.
\begin{definition}
    If $X$ is a path-connected soace and $\pi_1(X,x_0)$ is trivial for some (hence every) $x_0\in X$, then we say $X$ is simply connected.
\end{definition}
\begin{lemma}\label{hom_commute_path}
    Let $x_0\in X_1$ and $f,g:X\to Y$ with $f\simeq_Fg$.
    Set $\alpha(t)=F(x_0,t)$ a path from $f(x_0)$ to $g(x_0)$.
    Then
    \[
        \begin{tikzcd}
            \pi_1(Y,f(x_0))\arrow{r}{\alpha_\#}&\pi_1(Y,g(x_0))\\
            \pi_1(X,x_0)\arrow{u}{f_\ast}\arrow[swap]{ur}{g_\ast}&
        \end{tikzcd}
    \]
    commutes.
\end{lemma}
\begin{proof}
    We need to check that for a loop $\gamma$ based at $x_0$, we have $\bar\alpha\cdot(f\circ\gamma)\cdot\alpha\simeq g\circ\gamma$.
    Consider $G:I\times I\to Y$ defined by $G(s,t)=F(\gamma(s),t)$.
    Let $a,b_1,b_2,b_3,b:I\to I^2$ be paths defined by
    $$a(t)=(t,1),b_1(t)=(0,1-t),b_2(t)=(t,0),b_3(t)=(1,t),b=b_1\cdot b_2\cdot b_3$$
    Easily $a\simeq b$.
    Then $(G\circ a)(s)=G(s,1)=F(\gamma(s),1)=(g\circ\gamma)(s)$, so $G\circ a=g\circ\gamma$.
    Calculation shows that $G\circ b_1=\bar\alpha, G\circ b_2=f\circ\gamma, G\circ b_3=\alpha$.
    Hence
    $$g\circ\gamma\simeq G\circ a\simeq G\circ b\simeq G\circ (b_1\cdot b_2\cdot b_3)\simeq (G\circ b_1)\cdot(G\circ b_2)\cdot(G\circ b_3)\simeq \bar\alpha\cdot(f\circ\gamma)\cdot\alpha$$
    As desired.
\end{proof}
\begin{theorem}\label{hom_eqv_iso}
    If $f:X\to Y$ is a homotopy equivalence, then for any $x_0\in X$,
    $$f_\ast:\pi_1(X,x_0)\to\pi_1(Y,f(x_0))$$
    is an isomorphism of groups.
\end{theorem}
\begin{proof}
    Let $g:Y\to X$ be an homotopy inverse of $f$.
    Then $\operatorname{id}_X\simeq_Fg\circ f$ and $\operatorname{id}_Y\simeq_Gf\circ g$ for some homotopy $F,G$.
    Let $\alpha(t)=F(x_0,t)$ be a path joining $\operatorname{id}_X(x_0)=x_0$ and $g(f(x_0))$, then by Lemma \ref{hom_commute_path} we have
    $$g_\ast\circ f_\ast=(g\circ f)_\ast=\alpha_\#\circ (\operatorname{id}_X)_\ast=\alpha_\#$$
    But $\alpha_\#$ is an isomorphism hence injective by Lemma \ref{indep_basepoint}, so in particular $f_\ast$ is an injection.
    By the same argument $\beta(t)=G(f(x_0),t)$ has $f_\ast\circ g_\ast=\beta_\#$ which is an isomorphism hence surjective, so $f_\ast$ is surjective.
    Therefore $f_\ast$ is bijective, hence an isomorphism.
\end{proof}
\begin{corollary}
    Contractible spaces are simply connected.
\end{corollary}
\begin{proof}
    By definition and Theorem \ref{hom_eqv_iso}.
\end{proof}


\documentclass[12pt]{article}
\usepackage{pylatex}
\usepackage{matlatex}
\usepackage{geometry}
\usepackage{amsmath}
\usepackage{pgf}
\usepackage{caption}
\usepackage{hyperref}
\usepackage{examples}

% portrait
\geometry{papersize={210mm,297mm},hmargin=2cm,tmargin=1.0cm,bmargin=1.5cm}
\parskip=8pt plus 4pt minus 2pt

% landscape
% \geometry{papersize={297mm,210mm},hmargin=2cm,tmargin=1.0cm,bmargin=1.5cm}
% \parskip=6pt plus 3pt minus 2pt

\begin{document}

\section*{A mixed Matlab-Python example}

This example demonstrates a cooperative effort where Matlab is used to do the analytic computations while Python is used to plot the data.

The example chosen here is to find and plot the solution to the boundary value problem defined by
\begin{align*}
   \frac{d^2y}{dx^2} + 2 \frac{dy}{dx} +10 y = 0\quad\quad\text{with }y(0)=3,\> y'(0)=0
\end{align*}

This example requires to passes, once for Matlab and once for Python (and in that order). This example can be run using

\begin{lstlisting}
   matlatex.sh -x -i mixed
   pylatex.sh  -x -i mixed
   pdflatex          mixed
\end{lstlisting}

Note that the last pair of commands could also be combined as {\small\tt pylatex.sh -i mixed}.

\subsection*{The Matlab code}

Here Matlab is used to first find the general solution of th differential equation. The boundary conitions are then imposed and finally a uniform sampling of the solution is written to a file for later use by Python and Matplotlib.

\begin{matlab}
   syms x y(x)
   % a second order ode
   ode = diff(y(x), x, x) + 2*diff(y(x),x) + 10*y(x) == 0;  % mat (ans.101,ode)
   ddx = diff(y,x);

   % find the general solution
   sol = dsolve(ode);                     % mat (ans.102,sol)

   % set initial conditions
   ics = [y(0) == 3, ddx(0) == 0];
   tmp = ics';                            % mat (ans.103,tmp)

   % find the particular solution
   sol = dsolve(ode,ics);                 % mat (ans.104,sol)
   ddx = diff(sol,x);                     % mat (ans.105,ddx)

   xvals = linspace (0.0,2.0*pi,300);
    f = vpa(subs(sol,x,xvals),10);
   df = vpa(subs(ddx,x,xvals),10);

   dlmwrite ('mixed.txt',[xvals;f;df]','delimiter',' ','precision','% .8e');

\end{matlab}

\clearpage

The general solution of the differential equation is
\begin{equation*}
   y(x) = \mat{ans.102}
\end{equation*}
while the particular solution satifying the boundary conditions is given by
\vspace{5pt}
\begin{align*}
   y(x) &= \mat{ans.104}
\end{align*}

\subsection*{The Python code}

This is a straighforward use of Matplotlib to plot two functions. The code reads the datafile created previously by Matlab and then calls Matplotlib to plot that data.

\begin{python}
   import numpy as np
   import matplotlib.pyplot as plt

   plt.matplotlib.rc('text', usetex = True)
   plt.matplotlib.rc('grid', linestyle = 'dotted')
   plt.matplotlib.rc('figure', figsize = (5.5,4.1)) # (width,height) inches

   x, y, dy = np.loadtxt ('mixed.txt', unpack=True)

   plt.plot (x,y)
   plt.plot (x,dy)

   plt.xlim (0.0,4.0)

   plt.legend(('$y(x)$', '$dy(x)/dx$'), loc = 0)
   plt.xlabel('$x$')
   plt.ylabel('$y(x),\> dy/dx$')
   plt.grid(True)
   plt.tight_layout(0.5)

   plt.savefig('mixed_fig.pdf')
\end{python}

\vspace{10pt}

\begin{minipage}{\textwidth}
   \centering
   \IfFileExists{mixed_fig.pdf}%
   {\includegraphics[width=0.75\textwidth]{mixed_fig.pdf}}{Failed to create pdf plot.}
   \captionof{figure}{The function and its derivative.}
\end{minipage}

\end{document}


\documentclass{article}
\usepackage{fullpage}
\usepackage{nopageno}
\usepackage{amsmath}
\allowdisplaybreaks
\begin{document}
\title{Homework 5}
\author{Jon Allen}
\date{October 2, 2013}
\maketitle
\section*{Section 3.1 Problems 19,20}
\subsection*{19}
Solve the logistic equation $\frac{\mathrm{d}y}{\mathrm{d}t}=\alpha y\left(1-\frac{1}{K}y\right)$ by viewing it as a Bernoulli equation.
\begin{align*}
\frac{\mathrm{d}y}{\mathrm{d}t}&=\alpha y-\frac{\alpha}{K}y^2\\
\frac{\mathrm{d}y}{\mathrm{d}t}-\alpha y&=-\frac{\alpha}{K}y^2 & w&=\frac{1}{y}\\
-\frac{1}{y^2}\frac{\mathrm{d}y}{\mathrm{d}t}+\alpha\frac{1}{y}&=\frac{\alpha}{K} & \frac{\mathrm{d}w}{\mathrm{d}t}&=-\frac{1}{y^2}\frac{\mathrm{d}y}{\mathrm{d}t}\\
\frac{\mathrm{d}w}{\mathrm{d}t}+\alpha w&=\frac{\alphae}{K} & \mu(t)&=e^{\int{\alpha\,\mathrm{d}t}}\\
\mu(t)w&=\int{\mu(t)\frac{\alpha}{K}\,\mathrm{d}t} & \mu(t)&=e^{\alpha t}\\
e^{\alpha t}w&=\frac{1}{K}\int{\alpha e^{\alpha t}\,\mathrm{d}t}=\frac{e^{\alpha t}+C}{K}\\
\frac{1}{y}&=\frac{1+Ce^{-\alpha t}}{K}&y&=\frac{K}{1+Ce^{-\alpha t}}
\end{align*}
\subsection*{20}
What is the limmiting population, $\displaystyle\lim_{t\to\infty}y(t)$, of the United states population using the result obtained in Example 3.1.5?
\begin{align*}
y(t)&=\frac{0.159}{0.00053+0.02947e^{-0.03t}}\\
\lim_{t\to\infty}y(t)&=\lim_{t\to\infty}\frac{0.159}{0.00053+0.02947e^{-0.03t}}\\
\lim_{t\to\infty}y(t)&=\frac{0.159}{0.00053+0.02947e^{-0.03\infty}}\\
\lim_{t\to\infty}y(t)&=\frac{0.159}{0.00053+0.02947e^{-\infty}}\\
\lim_{t\to\infty}y(t)&=\frac{0.159}{0.00053+0.02947\frac{1}{e^{\infty}}}\\
\lim_{t\to\infty}y(t)&=\frac{0.159}{0.00053+0.02947\cdot0}\\
\lim_{t\to\infty}y(t)&=300
\end{align*}
\section*{Section 3.2 Problems 7,8}
Use equation $T=(T_0-T_s)e^{kt}+T_s$
\subsection*{7}
A thermometer that reads 90$^\circ$F is placed in a room with temperature 70$^\circ$F. After 3 min, the thermometer reads 80$^\circ$F. What does the thermometer read after 5 min?
\begin{align*}
	T_s&=70\\
	T_0&=90\\
	T(3)&=80=(90-70)e^{3k}+70\\
	\frac{10}{20}&=e^{3k}\\
	\ln\frac{1}{2}&=3k\\
	k&=\frac{1}{3}\ln\frac{1}{2}\\
	T(5)&=20e^{\frac{5}{3}\ln\frac{1}{2}}+70\approx76.3^\circ\mathrm{F}
\end{align*}
\subsection*{8}
A thermometer is placed outdoors with temperature 80$^\circ$F. After 2 min, the thermometer reads 68$^\circ$F, and after 5 min, it reads 72$^\circ$F. What was the initial temperature reading of the thermometer?
\begin{align*}
	T_s&=80\\
	T(2)&=68\\
	T(5)&=72\\
	T(t)-T_s&=(T_0-T_s)e^{kt}\\
	\frac{T(t)-T_s}{e^{kt}}&=T_0-T_s\\
	\frac{T(t)-T_s}{e^{kt}}+T_s&=T_0\\
	\frac{T(2)-80}{e^{2k}}&=\frac{T(5)-80}{e^{5k}}=\frac{T(5)-80}{e^{2k}e^{3k}}\\
	68-80&=\frac{72-80}{e^{3k}}\\
	e^{3k}&=\frac{-8}{-12}\\
	k&=\frac{1}{3}\ln\frac{2}{3}\\
	T_0&=\frac{T(2)-80}{e^{\frac{2}{3}\ln\frac{2}{3}}}+80=\frac{68-80}{(2/3)^{2/3}}+80=-\frac{12}{(2/3)^{2/3}}+80\\
	T_0&\approx64.3^\circ\mathrm{F}
\end{align*}
\end{document}


% !TEX root = ./main.tex
\section{Stochastic reproduction and the Moran process}

Our approach to derive the equation that governs the time evolution of the
allele frequency starts from a discrete state space. As mentioned before, we
will be working in the context of the one-locus two-allele model. To capture the
stochastic nature of the reproduction of these organisms we will adopt the Moran
process, shown schematically in Fig.~\ref{fig01:moran}(A). The Moran process is
a stochastic process with a fixed population size $N$. Organisms in this
discrete population die with a characteristic rate $\gamma$. Which type of
organisms, $A$ or $a$, dies during a small time window $\Delta t$ depends only
on the relative abundance of each allele type. When the organism that dies it is
immediately replaced when any of the organisms in the population reproduces. The
probability of which organism gets to reproduce depends on the relative fitness
values of each allele. Furthermore, when the organism reproduces, there is an
intrinsic probability that the progeny mutates to the opposite allele. As we
will show below for the different evolutionary forces, all of these stochastic
events--type of organism that dies, type of organism that replaces the dead
organism, change of allele type due to mutation--are taken as multiplicative
factors in the rate of change of the population structure.

\begin{figure}[h!]
	\centering \includegraphics {../../fig/spread_the_butter/fig01_moran.pdf}
  \caption{\textbf{Schematic of the Moran process}. (A) In the Moran process the
  population size $N$ remains constant at all times. Organisms stochastically
  die at a rate $\gamma$ to be immediately replaced by another organism that is
  able to reproduce. For the composition of the population to change two things
  can happen: 1. An organism of the same type as the one that died reproduces
  --with a probability depending on the fitness--,and then this organism mutates
  to the opposite allele type, or 2. An organism of the opposite allele
  reproduces--also with a probability depending on the fitness--, and this
  organism does not mutate when doing so. (B) Schematic representation of the
  construction of the master equation for the number of organisms carrying
  allele $A$. On a small discrete time step $\Delta t$ the probability of
  transitioning to from a number $n$ to a number $n + 1$ or $n-1$ is given by
  $w^+(n)$ and $w^-(n)$, respectively. (C) In the limit of a large population we
  can approximate the probability mass function for the number of organisms
  $p(n, t)$ with a probability density function for the allele frequency $P(x,
  t)$.}
  \label{fig01:moran}
\end{figure}

\subsection{General master equation for the Moran process}

Given the discrete nature of the Moran process, our first task consists of
writing the probability mass function (PMF) for the number of organisms carrying
allele $A$. Fig~\ref{fig01:moran}(B) shows a schematic of how to build such
distribution. Focusing on a particular number of organisms $n$, during a
sufficiently small time window $\Delta t$ such that we can neglect changes
larger than one organism, we must take into account all of the inflow and
outflow of probability from this particular bin. Let us define $w^+(n)$ and
$w^-(n)$ to be the transition rates (in units of time$^{-1}$) of jumping from
bin $n$ to bin $n+1$ and from bin $n$ to bin $n-1$, respectively. Writing down
the checks and balances during this small time window allows us to predict the
probability of having a particular number of organisms. This is mathematically
written as
\begin{equation}
    p(n, t + \Delta t) =
    p(n, t)
    + \overbrace{w^+(n - 1)\Delta t p(n - 1, t)}^
    {n - 1 \rightarrow n}
    + \overbrace{w^-(n + 1)\Delta t p(n + 1, t)}^
    {n + 1 \rightarrow n}
    - \overbrace{w^-(n)\Delta t p(n, t)}^
    {n \rightarrow n - 1}
    - \overbrace{w^+(n)\Delta t p(n, t)}^
    {n \rightarrow n + 1}.
    \label{eq:master_n_discrete}
\end{equation}
It is in the definition of these rates $w^\pm(n)$ that we include the effects
that different evolutionary forces have on the probability of changing the
composition of the population as schematized in Fig.~\ref{fig01:moran}(A).

We can further simplify Eq.~\ref{eq:master_n_discrete} by sending the first term
on the right hand side to the left, dividing both sides by $\Delta t$, and
taking the limit $\Delta t \rightarrow 0$. This results in the so-called master
equation for the number of organisms carrying allele $A$
\begin{equation}
    \frac{dp(n, t)}{dt} =
    w^+(n - 1) p(n - 1, t)
    + w^-(n + 1) p(n + 1, t)
    - w^+(n) p(n, t)
    - w^-(n) p(n, t).
    \label{eq:master_n}
\end{equation}

\subsection{Fokker-Planck equation for allele frequency}

Part of the beauty and power of diffusion theory is that we can work with smooth
continuous equations that track the evolution of the population composition.
Therefore, rather than tracking the discrete number of organisms, we will work
instead with allele frequency. In the limit of a very large population--one of
the conditions for diffusion theory to apply--we can approximate the allele
frequency $x$ to be a continuous variable between 0 and 1. As schematized in
Fig.~\ref{fig01:moran}(C), we can bin the continuous probability density
function for the allele frequency $P(x, t)$ into small bins of width $\Delta x$.
The natural width of such bins is given by the minimum change that can happen in
the discrete language, i.e., $\Delta x \approx 1 / N$. With this definition in
hand we can then approximate the PMF of $n$ as
\begin{equation}
  p(n, t) \approx
  \overbrace{P(x, t)}^{\text{base}} \;
  \overbrace{\Delta x}^{\text{width}}.
\end{equation}
The transition rates $w^\pm(n)$, having units of time$^{-1}$, are not affected
by this approximation. In other words, the transition rate to jump from $n$ to
$n \pm 1$ is the exact same transition rate to jump from $x$ to $x \pm \Delta
x$. We then define the transition rates in allele frequency as
\begin{equation}
  W^\pm(x) \equiv w^\pm(n).
\end{equation}
Substituting these definitions we can rewrite Eq.~\ref{eq:master_n} as
\begin{equation}
\begin{split}
    \frac{dP(x, t) \Delta x}{dt} =
    &W^+(x - \Delta x) P(x - \Delta x, t) \Delta x
    + W^-(x + \Delta x) P(x + \Delta x, t) \Delta x\\
    &- W^+(x) P(x, t) \Delta x
    - W^-(x) P(x, t) \Delta x.
\end{split}
\end{equation}
Given that $\Delta x$ does not depend on the time $t$ we can take it out of the
derivative and simplify it from both sides, obtaining the master equation for
the time evolution of the probability density function of the allele frequency
\begin{equation}
    \frac{dP(x, t)}{dt} =
    W^+(x - \Delta x) P(x - \Delta x, t)
    + W^-(x + \Delta x) P(x + \Delta x, t)
    - W^+(x) P(x, t)
    - W^-(x) P(x, t).
    \label{eq:master_x}
\end{equation}
Given the continuous nature of Eq.~\ref{eq:master_x} we can Taylor expand the
terms involving $x \pm \Delta x$. The expansion of these terms up to second
order takes the form
\begin{equation}
  W^{\mp}(x \pm \Delta x) P(x \pm \Delta x) \approx
  W^\mp(x) P(x, t) \pm
  \frac{d}{dx} \left[W^\mp(x) P(x, t) \right] \Delta x
  + \frac{1}{2} \frac{d^2}{dx^2} \left[ W^\mp(x) P(x, t) \right] (\Delta x)^2
  + \mathcal{O}((\Delta x)^3)
\end{equation}
Substituting these expansions into Eq.\ref{eq:master_x} results in a partial
differential equation of the form
\begin{equation}
\begin{aligned}
  \frac{\partial}{\partial t} P(x, t) &=
  W^{+}(x) P(x, t)
  -\frac{\partial}{\partial x}
  \left[W^{+}(x) P(x, t)\right] \Delta x
  +\frac{1}{2} \frac{\partial^{2}}{\partial x^{2}}
  \left[W^{+}(x) P(x, t)\right](\Delta x)^{2} \\
  &+W^{-}(x) P(x, t)+
  \frac{\partial}{\partial x}
  \left[W^{-}(x) P(x, t)\right] \Delta x
  + \frac{1}{2} \frac{\partial^{2}}{\partial x^{2}}
  \left[W^{-(x)} P(x, t)\right](\Delta x)^{2} \\
  &-W^{+}(x, t) P(x, t) -W^{-}(x) P(x, t).
\end{aligned}
\end{equation}
Simplifying terms and using the linearity of the derivatives we can rewrite this
as
\begin{equation}
\frac{\partial}{\partial t} P(x, t)=
-\frac{\partial}{\partial x}
\left[\left(W^{+}(x)-W^{-}(x)\right) P(x, t)\right] \Delta x
+\frac{1}{2} \frac{\partial^{2}}{\partial x^{2}}
\left[\left(W^{+}(x)+W^{-}(x)\right) P(x, t)\right](\Delta x)^{2}
\end{equation}
Finally, recall that we define $\Delta x \equiv 1 / N$. Substituting this gives
us the partial differential equation in which we will implement the different
evolutionary forces via the transition rates
\begin{equation}
\frac{\partial}{\partial t} P(x, t)=
-\frac{1}{N}\frac{\partial}{\partial x}
\left[\left(W^{+}(x)-W^{-}(x)\right) P(x, t)\right]
+\frac{1}{2N^2} \frac{\partial^{2}}{\partial x^{2}}
\left[\left(W^{+}(x)+W^{-}(x)\right) P(x, t)\right]
\label{eq:pde_x_general}
\end{equation}

In the following section we will implement one-by-one the three evolutionary
forces via the definition of the transition rates.