\section{Riemann Surfaces; Analytic Maps}
\subsection{Covering Maps}
In previous sections, we have seen the way of realising the complex logarithm by constructing functions $f,\pi:R\to\mathbb C$ satisfying the $\exp\circ f=\pi$.
That is, the diagram
\[
    \begin{tikzcd}
        R\arrow{r}{f}\arrow[swap]{dr}{\pi}&\mathbb C\arrow{d}{\exp}\\
        &\mathbb C_\star
    \end{tikzcd}
\]
Sadly, $\pi$ is not exactly a homeomorphism, but it is the next best thing.
\begin{definition}
    Let $\tilde{X},X$ be path-connected Hausdorff topological spaces.
    A covering map $\pi:\tilde{X}\to X$ is a local homeomorphism.
    That is, each $\tilde{x}\in\tilde{X}$ has an open neighbourhood $\tilde{U}$ such that $\pi|_{\tilde{U}}$ is a homoemorphism onto its image.
\end{definition}
\begin{definition}
    A covering map $\pi:\tilde{X}\to X$ is regular if for each $x\in X$ there is an open neighbourhood $U$ of $x$ and a discrete set $\Delta_x$ such that $\pi^{-1}(U)\cong U\times\Delta_x$ and the diagram
    \[
        \begin{tikzcd}
            \pi^{-1}(U)\arrow{r}{\cong}\arrow[swap]{dr}{\pi}&U\times\Delta_x\arrow{d}{(u,\delta)\mapsto u}\\
            &U
        \end{tikzcd}
    \]
    commutes.
\end{definition}
In particular, $\pi|_{\pi^{-1}(U)}$ must has image $U$.
A useful and non-confusing way of taking $U\times\Delta_x$ is to think of it as a disjoint union of copies of $U$.
\begin{example}
    1. The map $\pi:R\to\mathbb C$ we defined when treating $\log$ is a regular convering map as
    $$\pi^{-1}(U_{I(n)})=\coprod_{m\equiv n\pmod{4}}U_{I(m)}\cong U_{I(n)}\times\mathbb Z$$
    2. For each open interval $I\subset\mathbb R$, write
    $$\tilde{V}_I=\mathbb R+iI=\{x+iy:x\in\mathbb R,y\in I\}$$
    As long as the length of $I$ is at most $2\pi$, the exponential function restricts to a homeomorphism $\tilde{V}_I\to U_I$ with the obvious inverse obtained by taking a branch of $\log$.
    So
    $$\exp^{-1}(U_{I(n)})=\coprod_{m\equiv n\pmod{4}}\tilde{V}_{I(m)}\cong U_{I(n)}\times\mathbb Z$$
    which means $\exp:\mathbb C\to\mathbb C_\star$ is a covering map.\\
    3. (non-example) Consider $\pi:\mathbb D\to\mathbb C$ which is obviously a covering map and $z\in\mathbb T$ with a neighbourhood $U\ni z$, then $\pi^{-1}(U)=U\cap\mathbb D$.
    But then the image of $\pi|_{U\cap\mathbb D}$ is never $U$, so $\pi$ is not regular.\\
    4. The map $\pi:R_k\to\mathbb C_\star$ we constructed for $\sqrt[k]{\cdot}$ is also regular.
\end{example}


\documentclass{notes}

  \title{Hash functions}
  \author{ian.mcloughlin@gmit.ie}
  \date{\today}

\begin{document}

  \section*{Hash function}
  \[h:\{0,1\}^* \rightarrow \{0,1\}^n \qquad n \in \mathbb{N}_0 \] \\
  \textit{Compression:} arbitrary length to fixed length. \\[2mm]
  \textit{Ease of computation:} we know an efficient algorithm to perform \(h\).

  \section*{Collision}
  \[h(x_1) = h(x_2) \]

  \section*{Preimage resistance}
  Given \(y\) it's infeasible to find any \(x\) such that \(h(x) = y\).

  \section*{Second preimage resistance}
  Given \(x_1\) it's infeasible to find another \(x_2\) such that \(h(x_1) = h(x_2)\).

  \section*{Collision resistance}
  Infeasible to find \(x_1\) and \(x_2\) such that \( x_1 \neq x_2 \) and \(h(x_1) = h(x_2)\).

  \section*{One-way}
  Efficient algorithm to calculate \(f(x) = y\), no efficient algorithm to calculate \(f^{-1}(y) = x\).
  No one has proved one-way functions really exist.

  Not to be confused with not being one-to-one:

  \[\mathtt{rshift}(0011) = \mathtt{rshift}(0010) = 0001 \]

  Given \(y\), easy to find \(x\) such that \(\mathtt{rshift}(x) = y\).

  \section*{SHA256}
  \[f: \{0,1\}^{256} \times \{0,1\}^{512} \rightarrow \{0,1\}^{256}\]

  \section*{Padding}
  \[\mathtt{pad}(m) = M\]
  \begin{itemize}
    \item Append a 1 bit.
    \item Append 0 bits such that \(|M| \equiv_{512} 448\).
    \item Append \(|M|\), least significant bit on right.
  \end{itemize}

  Note padding with zeros or not padding would give easy collisions.

  \section*{Merkle-Damgrad}

  \begin{adjustbox}{max width={\textwidth},center}
    \begin{tikzpicture}[node distance=2cm]
      \node (h_x) at (0,0) {};
      \node (h_y) at (5.5,0) {};
      \begin{scope}[every node/.style={draw}]
        \node (h_0) at (1,0) {\(f\)};
        \node (h_1) at (2,0) {\(f\)};
        \node (h_2) at (3,0) {\(f\)};
        \node (h_n) at (4.5,0) {\(f\)};
      \end{scope}
      \begin{scope}[every node/.style={draw}]
        \node (m_1) at (1,1.3) {\(M_1\)};
        \node (m_2) at (2,1.3) {\(M_2\)};
        \node (m_3) at (3,1.3) {\(M_3\)};
        \node (m_n) at (4.5,1.3) {\(M_n\)};
			\end{scope}
			\begin{scope}[every edge/.style={draw=black,->}]
        \path (h_x) edge node[below] {\(H_0\)} (h_0);
        \path (h_0) edge node[below] {\(H_1\)} (h_1);
        \path (h_1) edge node[below] {\(H_2\)} (h_2);
        \path (h_n) edge node[below] {\(H_n\)} (h_y);

        \path (m_1) edge node[below] {} (h_0);
        \path (m_2) edge node[below] {} (h_1);
        \path (m_3) edge node[below] {} (h_2);
        \path (m_n) edge node[below] {} (h_n);
      \end{scope}
      \begin{scope}[every edge/.style={draw=black,thick,->,dashed}]
				\path (h_2) edge node[below] {} (h_n);
			\end{scope}
    \end{tikzpicture}
  \end{adjustbox}

  %\bibliography{bibliography}
\end{document}


\section{Solutions of TISE in One Dimension}
In one dimension, TISE restricts to
$$-\frac{\hbar^2}{2m}X^{\prime\prime}+UX=EX$$
where $E\in\mathbb R$.
\subsection{Infinte Potential Well}
Consider the potential
\footnote{Unorthodox, I know. I have stopped caring.}
$$U(x)=\begin{cases}
    0\text{, if $|x|\le a$}\\
    \infty\text{, if $|x|>a$}
\end{cases}$$
For $|x|>a$ we take the solution $X(x)=0$.
And we also want $X(\pm a)=0$ as we want $X$ to be continuous.
For $|x|\le a$, we are then aiming at the boundary value problem
$$-\frac{\hbar^2}{2m}X^{\prime\prime}=EX\iff X^{\prime\prime}+K^2X=0,K^2=\frac{2mE}{\hbar^2}\ge 0$$
subject to $X(\pm a)=0$.
This is known to have the general solution $X(x)=A\sin(Kx)+B\cos(Kx)$ for constants $A,B$.
The boundary conditions then require either $A=0$ and $K=n\pi/2a$ for $n=1,3,5,\ldots$ or $B=0$ and $K=n\pi/2a$ with $n=2,4,6,\ldots$.
So the allowed values of the energy would be
$$E_n=\frac{\hbar^2\pi^2}{8ma^2}n^2,n=1,2,3,\ldots$$
The lowest positive energy (aka ground state energy) is then $E_1=\hbar^2\pi^2/(8ma^2)$.
The solutions are
$$X_n(x)=\frac{1}{\sqrt{a}}\begin{cases}
    \cos(n\pi x/(2a))\text{, for $n=1,3,5,\ldots$}\\
    \sin(n\pi x/(2a))\text{, for $n=2,4,6,\ldots$}
\end{cases}$$
where the factor is obtained from the assumption that $X_n$ is normalised.
I am too lazy to plot the functions, but whoever read this are encouraged to plot a few of these states.
Without plotting, however, one can immediately realise that when $n$ is large, $X$ tends to fluctuate a lot.
The solution also allows us to draw the analogy between these solutions and standing waves with two endpoints fixed at $\pm a$.
Also, $X_n(-x)=(-1)^{n+1}X_n(x)$, so $X_n$ is either even or odd.
This is actually a general feature of the eigenfunctions of the Hamiltonian $\hat{H}$.
\begin{proposition}
    If $U$ is even and the energy spectrum is non-degenerate (i.e. we require $E_1<E_2<\cdots$), then each eigenfunction is either even or odd.
\end{proposition}
\begin{proof}
    If $U$ is even then TISE is reflection invariant, so $x\mapsto X(-x)$ is also a solution.
    But it also has energy $E$, so $X(-x)=AX(x)$ where $A$ is a constant.
    But then $X(x)=X(-(-x))=AX(-x)=A^2X(x)$, so $A=\pm 1$ as $X$ is not identically zero.
\end{proof}
\subsection{Finite Potential Well}
We consider the potential of the form
$$U(x)=\begin{cases}
    0\text{, if $|x|\le a$}\\
    U_0\text{, if $|x|>a$}
\end{cases}$$
where $U_0>0$.
We only consider the case $E<U_0$.
The other case will be dealt with later.
Also we only look for even $X$.
We define the nonnegative real quantities
$$K=\sqrt{\frac{2mE}{\hbar^2}},\kappa=\sqrt{\frac{2m(U_0-E)}{\hbar^2}}$$
For $|x|\le a$, we obtain the general solution $X(x)=A\sin(Kx)+B\cos(Kx)$ for $A,B$ constants.
We are looking for even states only, so we might as well write $X(x)=A\cos(Kx)$.
For $|x|>a$, we have $X^{\prime\prime}-\kappa^2X=0$ which has solution $X=Ce^{\kappa x}+De^{-\kappa x}$ for some constants $C,D$.
But the normalisation condition then tells us $C=0$ for $x>a$ and $D=0$ for $x<-a$.
Hence
$$X(x)=\begin{cases}
    Ce^{\kappa x}\text{, for $x<-a$}\\
    A\cos(Kx)\text{, for $|x|\le a$}\\
    De^{-\kappa x}\text{, for $x>a$}
\end{cases}$$
The continuity of $X$ and $X^\prime$ that we assumed then yield $K\tan(Ka)=\kappa$.
But the definition of $K$ and $\kappa$ yields $K\tan(Ka)=\kappa$.
We also know that $K^2+\kappa^2=2mU_0/\hbar^2$.
Define $\xi=Ka$ and $\eta=\kappa a$, then the two equations become
$$\begin{cases}
    \xi\tan\xi=\eta\\
    \eta^2+\eta^2=r_0^2
\end{cases}$$
where $r_0^2=2a^2mU_0/\hbar^2$.
By a plot, we know that there are only finitely many solutions $\{\xi_1,\ldots,\xi_p\}$ of $\xi$ with $(n-1)\pi\le\xi_n\le(n-1/2)\pi$ and the allowed energy are of the form
$$E_n=\frac{\hbar^2}{2ma^2}\xi_n^2,n=1,\ldots,r$$
As $U_0\to\infty$, we can get $r_0\to\infty$, so $p\to\infty$.
And the allowed energy would be
$$E_n=\frac{\hbar^2}{2ma^2}\xi_n^2=\frac{\hbar^2(2n-1)^2}{2ma^2}\pi^2$$
which is consistent with our result on infinite well.
Of course we can determine all the eigenfunctions as we have enough number of conditions on the breaking points and the normalisation condition.
One can also show by some calculation that the eigenfunctions also go to the eigenfunctions of the infinite well solution as $U_0\to\infty$.
\subsection{Free Particle}
If we require that
$$\int_{-\infty}^\infty |X(x)|^2\,\mathrm dx=N\in\mathbb R_+$$
then necessarily
$$\lim_{R\to\infty}\int_{|x|>R}|X(x)|^2\,\mathrm dx=0$$
Returning to our TISE $X^{\prime\prime}+K^2X(x)=0$ where $K=\sqrt{2mE/\hbar^2}$ which has solutions in the form $X_K(x)=Ae^{iKx}$.
This is obviously a continuous spectrum of solutions (corresponding to different values of $E$).
They gives the solutions $\psi_K(x,t)=X_k(x)e^{-i\hbar K^2t/(2m)}$.
But they are not in general normalisable!
Indeed,
$$\int_{\mathbb R}|\psi_K(x,t)|^2\,\mathrm dx=\int_{\mathbb R}|X_K(x,t)|^2\,\mathrm dx=|A|^2\int_{\mathbb R}\mathrm dx=\infty$$
unless $A=0$.
The workaround is to consider the integral superposition
$$\psi(x,t)=\int_{\mathbb R}A(K)\psi_K(x,t)\,\mathrm dK=\int_{\mathbb R}A(K)e^{iKx}e^{-i\hbar K^2t/(2m)}\,\mathrm dK$$
and choose $A$ that decreases fast enough as $K\to\infty$ so that $\psi$ is well-defined and normalisable.
A typical choice is the Gaussian wavepackage
$$A(K)=\exp\left( -\frac{\sigma}{2}(K-K_0)^2 \right)$$
where $\sigma>0$.
So using this we can write
$$\psi(x,t)=\int_{\mathbb R}\exp F(K)\,\mathrm dK$$
where
\begin{align*}
    F(K)&=-\frac{\sigma}{2}(K-K_0)^2+i\left(Kx-\frac{\hbar K^2}{2m}t\right)\\
    &=-\frac{1}{2}\left( \sigma+\frac{i\hbar t}{m} \right)K^2+(K_0\sigma+ix)K-\frac{\sigma}{2}K_0^2\\
    &=-\frac{\alpha}{2}\left( K-\frac{\beta}{\alpha} \right)^2+\frac{\beta^2}{2\alpha}+\delta,\alpha=\sigma+\frac{i\hbar t}{m},\beta=K_0\sigma+ix,\delta=-\frac{\sigma}{2}K_0^2
\end{align*}
Therefore,
\begin{align*}
    \psi(x,t)(x,t)&=\exp\left( \frac{\beta^2}{2\alpha}+\delta \right)\int_{-\infty}^\infty\exp\left( -\frac{\alpha}{2}\left( K-\frac{\beta}{\alpha} \right)^2 \right)\,\mathrm dK\\
    &=\exp\left( \frac{\beta^2}{2\alpha}+\delta \right)\int_{-\infty-i\nu}^{\infty-i\nu}\exp\left( -\frac{\alpha}{2}\tilde{K}^2 \right)\,\mathrm d\tilde{K},\tilde{K}=K-\frac{\beta}{\alpha},\nu=\operatorname{Im}\frac{\beta}{\alpha}\\
    &=\sqrt{\frac{2\pi}{\alpha}}\exp\left( \frac{\beta^2}{2\alpha}+\delta \right)
\end{align*}
I know the last step is staggering.
It's ok, you are stong enough to handle these traumas in your life.
Anyways, putting everything back gives
$$\psi(x,t)=\sqrt{\frac{2\pi}{\alpha}}\exp\left( -\frac{\sigma(x-\hbar K_0t/m)^2}{2(\sigma^2+\hbar^2t^2/m^2)} \right)$$
And (with the help of God) we can obtain the probability density
$$\rho(x,t)=\frac{C}{\sqrt{\sigma^2+\hbar^2t^2/m^2}}\exp\left( -\frac{\sigma(x-\hbar K_0t/m)^2}{\sigma^2+\hbar^2t^2/m^2} \right)$$
where $C$ is a normalisation constant, which can be determined by more calculation:
$$\int_{-\infty}^\infty\rho(x,t)\,\mathrm dx=1\implies C=\sqrt{\frac{\sigma}{\pi}}$$
Now, by a little calculation we obtain the mean $\langle x\rangle=\hbar K_0t/m$.
We write $v=\hbar K_0/m$ as an analogy of velocity.
The standard deviation is
$$\Delta x=\sqrt{\langle x^2\rangle-\langle x\rangle^2}=\sqrt{\frac{1}{2}\left( \sigma+\frac{\hbar^2t^2}{m^2\sigma} \right)}$$
which increases as $t\to\infty$, therefore the distribution is more spreaded out as $t\to\infty$.\\
Back to $\psi_K(x,t)=Ae^{iKx}e^{-i\hbar K^2t/(2m)}$, which as one can verify has standard deviation $\Delta x=\infty$ on position and $\Delta p=0$ on momentum since the momentum is just $p=\hbar K$.
These whole $\Delta x,\Delta p$ business will be dealt with later when we get to Heisenberg's Uncertainty Principle.
In fact, it is possible to show that the Gaussian wavepackage minimises uncertainty.
Now $\rho_K(x,t)=|A(K)|^2$ is then interpreted as the constant average density of a particle.
The probability current is
$$J_K(x,t)=-\frac{i\hbar}{2m}\left( \psi_K^*\frac{\partial\psi_K}{\partial x}-\psi_K\frac{\partial\psi_K^*}{\partial x} \right)=|A|^2\frac{\hbar K}{m}=|A|^2\frac{p}{m}$$
This is basically the product of the average density and velocity, which can be interpreted as the average flux of particles.
In some way, we are interpreting the state as a beam of particles where $A$ relates to the beam density.
\subsection{Scattering States}
Consider $U_0>0$ and
$$U(x)=\begin{cases}
    U_0\text{, for $x\in (0,a)$}\\
    0\text{, otherwise}
\end{cases}$$
Define
$$R=\lim_{t\to\infty}\int_{-\infty}^0|\psi(x,t)|^2\,\mathrm dx,T=\lim_{t\to\infty}\int_0^\infty|\psi(x,t)^2|\,\mathrm dx$$
If we interpret the system as a wave moving towards $x=0$ being scattered (partially reflected and partially transmitted), then $R$ can be seens as the reflection probability and $T$ the transmission probability.
If $\psi$ is normalised, then we have $R+T=1$.
Take the solutions $\psi_K(x,t)=X_K(x)e^{-i\hbar K^2t/(2m)}$ where $X_K(x)=Ae^{iKx}$.
\subsubsection{Scattering off a Potential Step}
Take $a\to\infty$ gives the potential
$$U(x)=\begin{cases}
    0\text{, for $x\le 0$}\\
    U_0\text{, for $x>0$}
\end{cases}$$
Restate the TISE
$$-\frac{\hbar^2}{2m}X^{\prime\prime}+UX=EX$$
First, we consider the case $E>U_0$.
For $x\le 0$, if we throw in $K=\sqrt{2mE/\hbar^2}$ the system gives
$$X_K(x)=X_K^{(+)}(x)+X_K^{(-)}(x),X_K^{(+)}(x)=Ae^{iKx},X_K^{(-)}(x)=Be^{-iKx}$$
where $A,B$ are constants.
Then we interpret $X_K^{(+)}$ as the beam of incident particles from $x=-\infty$ with $p=\hbar K$ and probability current $J_K^{(+)}=|A|^2\hbar K/m$.
The $X_K^{(-)}$ is correspondingly interpreted as the beam of reflected particles going towards $x=-\infty$ with $p=-\hbar k$ and $J_K^{(-)}=-|B|^2\hbar K/m$.
Now for $x>0$ we have $X^{\prime\prime}+\tilde{K}^2X=0$ where $\tilde{K}=\sqrt{2m(E-U_0)/\hbar^2}$ which is well-defined as we assumed $U_0<E$.
This yield the solutions
$$X_{\tilde{K}}=X_{\tilde{K}}^{(+)}+X_{\tilde{K}}^{(+)},X_{\tilde{K}}^{(+)}(x)=Ce^{i\tilde{K}x},X_{\tilde{K}}^{(-)}(x)=De^{-i\tilde{K}x}$$
Again $X_{\tilde{K}}^{(+)}$ is the beam of transmitted particles moving towards $x=+\infty$ with $p=\hbar\tilde{K}$ and $X_{\tilde{K}}^{(-)}$ is the beam if incident particles from $x=+\infty$ with $p=-\hbar \tilde{K}$.
We only want to consider the scattering problem, so we can set $D=0$.
Continuity of $X$ at $x=0$ gives $A+B=C$ and continuity of $X^\prime$ at $x=0$ gives $iKA-iKB=i\tilde{K}C$, therefore
$$B=\frac{K-\tilde{K}}{K+\tilde{K}}A,C=\frac{2K}{K+\tilde{K}}A$$
So the incident, reflective and transmitted probability current are
$$J_{\rm inc}=\frac{\hbar K}{m}|A|^2,J_{\rm ref}=\frac{\hbar K}{m}\left( \frac{K-\tilde{K}}{K+\tilde{K}} \right)^2|A|^2,J_{\rm tr}=\frac{\hbar \tilde{K}}{m}\frac{4K^2}{(K+\tilde{K})^2}|A|^2$$
Hence
$$R=\frac{J_{\rm ref}}{J_{\rm inc}}=\left( \frac{K-\tilde{K}}{K+\tilde{K}} \right)^2,T=\frac{J_{\rm tr}}{J_{\rm inc}}=\frac{4K\tilde{K}}{(K+\tilde{K})^2}$$
Easy to verify that $R+T=1$ and when $E\to \infty$ we have $K-\tilde{K}\to 0$ which implies $R\to 0,T\to 1$, which corresponds to the classical case.\\
For the case $E<U_0$, the equations become
$$\begin{cases}
    X^{\prime\prime}+K^2X=0\implies X=Ae^{iKx}+Be^{-iKx}\text{, for $x\le 0$}\\
    X^{\prime\prime}-\kappa^2X=0\implies X=Ge^{\kappa x}+Fe^{-\kappa x}\text{, for $x>0$}
\end{cases}$$
where as you expect,
$$K=\sqrt{\frac{2mE}{\hbar^2}},\kappa=\sqrt{\frac{2m(U_0-E)}{\hbar^2}}$$
We have $G=0$ because we expect the wavefunction to not blow up at $+\infty$.
Hence
$$X(x)=\begin{cases}
    Ae^{iKx}+B^{-iKx}\text{, for $x\le 0$}\\
    Fe^{-\kappa x}\text{, for $x>0$}
\end{cases}$$
Then the assumed continuity of $X,X^\prime$ at $0$ gives
$$B=\frac{iK+\kappa}{iK-\kappa}A,F=\frac{2iK}{iK-\kappa}A$$
So
$$j_{\rm tr}(x)=0,j_{\rm inc}=\frac{\hbar K}{m}|A|^2,j_{\rm ref}=\frac{\hbar K}{m}|B|^2=\frac{\hbar K}{m}|A|^2$$
Hence $R=1$ and $T=0$ just as we expect it.
\subsubsection{Scattering off a Potential Barrier}
Consider $a\in (0,\infty)$, so
$$U(x)=\begin{cases}
    U_0\text{, for $x\in (0,a)$}\\
    0\text{, otherwise}
\end{cases}$$
We deal with the $E\le U_0$ case first.
We define
$$K=\sqrt{\frac{2mE}{\hbar^2}},\bar{K}=\sqrt{\frac{2m(U_0-E)}{\hbar^2}}$$
Therefore
$$\begin{cases}
    X^{\prime\prime}(x)-\bar{K}^2X(x)=0\text{, for $x\in (0,a)$}\\
    X^{\prime\prime}(x)+K^2X(x)=0\text{, otherwise}
\end{cases}$$
which gives the general (normalised) solution
$$X(x)=\begin{cases}
    e^{iKx}+Ae^{-iKx}\text{, for $x\le 0$}\\
    Be^{-\bar{K}x}+Ce^{\bar{K}x}\text{, for $x\in (0,a)$}\\
    De^{iKx}+Fe^{-iKx}\text{, for $x\ge a$}
\end{cases}$$
We can set $F=0$ since we only consider the case where the incident beam is from $x=-\infty$.
By continuity of $X,X^\prime$ again we get
$$D=\frac{-4i\bar{K}K}{(\bar{K}-iK)^2e^{(\bar{K}+iK)a}-(\bar{K}+iK)^2e^{-(\bar{K}-iK)a}}$$
In classical dynamics, the particle can never pass the barrier.
But our result just shows that this is not in general the case, since
$$T=\frac{j_{\rm tr}}{j_{\rm inc}}=\frac{\hbar K|D|^2/m}{\hbar K/m}=|D|^2=\frac{4K^2\bar{K}^2}{(K^2+\bar{K}^2)^2\sinh^2(\bar{K}a)+4K^2\bar{K}^2}\neq 0$$
In particular, for $\bar{K}a>>1$, either $a>>1$ or $U_0>>E$ and
$$T\sim\frac{16K^2\bar{K}^2}{(K^2+\bar{K}^2)^2}e^{-2\bar{K}a}$$
So if the barrier is very wide or very tall, $T$ goes exponentially to $0$.
When $E<U_0$ but a beam of particles is transmitted, this phenomenon is called quantum tunneling.
\subsubsection{Physical Examples of Quantum Tunneling}
\begin{example}[Cold Emission]
    When light irradiates a metal surface, electrons are emitted when $\hbar\omega>W$ where $\omega$ is the angular frequency of photons and $W$ is a constant (called the work function) associated with the metal.
    An electron would be facing some sort of triangular barrier.
    In addition to the potential step at $x=a$ because of the work function $W$, suppose $\xi$ is the external electric field, then the resulting potential induced by the Lorentz force is $V_{\rm Lorentz}(x)=-e\xi(x-a)$.
    Their superposition would be a triangular barrier with cliff at $x=a$.
    So even for an electron with energy less than $W$, it can go to the part of the potential where it is low enough via quantum tunneling and excite from there.
    This is knowns as cold emission in photoelectric effect.
\end{example}
\begin{example}[Radioactive Decay]
    Consider the decay $\text{N}_{Z}^A\to \text{M}_{Z-2}^{A-4}+\text{He}_2^4$.
    For the $\alpha$ particle $\text{He}_2^4$, we want to know the potential field it is in.
    Indeed, if we let $r$ to be the distant between this $\alpha$ particle and the center of the nucleus, then $U(r)$ will begin negative near $0$ because of the attractive nuclear force and eventually go up positive again after $r$ gets large enough since the repulsive Coulumb force comes in play.
    Eventually, it goes down (but keep positive) and tends to $0$ as $r\to\infty$.
    This is known as the Gamow model.
    The curve certainly looks like a potential barrier.
    If we let $T$ be the transmission coefficient, then the half-life of this decay is actually proportional to $T^{-1}$.
\end{example}
\subsection{The Harmonic Oscillator}
The potential of the harmonic oscillator can be written as $U(x)=Kx^2/2=m\omega^2x^2/2$ where $K>0, \omega=\sqrt{k/m}>0$.
In classical mechanics, Newton's Second Law gives the equation $\ddot{x}=-\omega^2x$ which has solutions $x=A\sin(\omega t)+B\cos(\omega t)$ where $A,B$ are constants.
So the particle oscillates about $x=0$ with period $2\pi/\omega$.\\
In quantum mechanics, this gives the TISE
$$-\frac{\hbar^2}{2m}\frac{\mathrm d^2X}{\mathrm dx^2}+\frac{1}{2}m\omega^2x^2X(x)=EX(x)$$
where we expect to find a discrete set of normalisable eigenfunctions by looking at the shape of the potential.
As seen earlier, we can guarantee to find eigenfunctions that are either even or odd.
Write $\xi^2=m\omega x^2/\hbar$ and $\mathcal E=2E/(\hbar\omega)$, then this change of variables yield
$$-\frac{\mathrm d^2X}{\mathrm d\xi^2}+\xi^2X(\xi)=\mathcal EX(\xi)$$
For $\mathcal E=1$ it reduces to $-X^{\prime\prime}(\xi)+\xi^2X=X$.
Educated guess reveals that $X_0=A\exp(-\xi^2/2)$ is a family of solutions which is normalisable when $A\neq 0$.
This gives one pair of eigenvalue $E_0=\hbar\omega/2$ and eigenfunction $X_0(x)=A\exp(-m\omega x^2/(2\hbar))$ for $A\neq 0$.\\
For the general case, set $X(\xi)=f(\xi)\exp(-\xi^2/2)$, which transforms the equation into
$$-\frac{\mathrm d^2f}{\mathrm d\xi^2}+2\xi\frac{\mathrm df}{\mathrm d\xi}+(1-\mathcal E)f=0$$
Now we are desperate, so the obvious thing to do is to plug in the power series solution $f(\xi)=\sum_na_n\xi^n$ which gives
$$0=\sum_{n=0}^\infty [(n+1)(n+2)a_{n+2}-2na_n+(\mathcal E-1)a_n]\xi^n$$
which gives the recurrence
$$a_{n+2}=\frac{2n-\mathcal E+1}{(n+1)(n+2)}a_n$$
The recurrence steps by $2$, so it has even or odd solutions.
Here we need to prove an important claim
\begin{claim}
    Suppose the series does not terminate then $X(\xi)=f(\xi)\exp(-\xi^2/2)$ is not normalisable.
\end{claim}
\begin{proof}
    $a_{n+2}/a_n\sim 2/n$ as $n\to\infty$, which means that $f(\xi)\sim\exp(\xi^2)$, hence $X$ is not normalisable.
\end{proof}
Therefore, for a normalisable solution to exist, $\mathcal E$ must be an odd positive integer, so we get the eigenvalues and eigenfunctions
$$E_N=\left( N+\frac{1}{2} \right)\hbar\omega,X_N(x)=f_N\left( \sqrt{\frac{m\omega}{\hbar}} \right)\exp\left( -\frac{m\omega x^2}{2\hbar} \right)$$
where $f_N$ are the respective polynomials that yield from the series solution as the choice of $\mathcal E$ makes it terminate.
These are called Hermite polynomials which, as one can verify, satisfies $f_N(-\xi)=(-1)^Nf_N(\xi)$ and
$$f_N(\xi)=(-1)^n\exp(\xi^2)\frac{\mathrm d^n}{\mathrm d\xi^n}\exp(-\xi^2)$$
We've got nothing better to do so why not calculate some of them.
\begin{center}
    \begin{tabular}{c|c|c|c}
        $N$&$f_N(\xi)$&$E_N$&$X_N(\xi)$\\ \hline
        $0$&$1$&$\hbar\omega/2$&$\exp(-\xi^2/2)$\\
        $1$&$\xi$&$3\hbar\omega/2$&$\xi\exp(-\xi^2/2)$\\
        $2$&$1-2\xi^2$&$5\hbar\omega/2$&$(1-2\xi^2)\exp(-\xi^2/2)$\\
        $3$&$\xi-2\xi^3/3$&$7\hbar\omega/2$&$(\xi-2\xi^3/3)\exp(-\xi^2/2)$
    \end{tabular}
\end{center}


\chapter{Coding of integers}

Coding of integers is important prerequisite to data compression. Some of these codes are unable to directly encode $0$ and none of these codes are able to directly encode negative numbers. Generally, these codes can be classified as uniquely decodable and non-uniquely decodable.

\section{Unary codes}
Encoding\sidenote{$\alpha$-code is uniquely decodable (prefix) code with variable length. It does not have the ability to encode value $0$ or negative numbers directly, usually shift of values is introduced to handle such cases, for example encoding $x+1$ or $x+k, k\geq1$.}:

\subsection{$\alpha$-code}
$$\alpha(x)=0^{x-1}1$$
\subsection{$\alpha'$-code}
This variant has $0$ and $1$ swapped.\sidenote{This variant of $\alpha$-code is primarily used in Golomb Codes.}
$$\alpha'(x)=1^{x-1}0$$
\section{Binary codes}
Encoding\sidenote{$\beta$-codes are non-uniquely decodable codes with variable length. Fixed length binary codes are also widely used, they are uniquely decodable, but alsways encode to the same number of bits and they have a maximum value. $\beta'$-code has leading $1$ removed.}:
\subsection{$\beta$-code}
$$\beta(0)=0$$
$$\beta(1)=1$$
$$\beta(2i+j)=\beta(i)\beta(j)$$
\subsection{$\beta'$-code}
$$\beta'(x) = \beta(x)-10^{|\beta(x)|-1}$$

\section{Ternary codes}
These codes introduce a special symbol $\#$ for marking end of each keyword. \sidenote{$\tau$-codes are uniquely decodable (prefix) codes with variable length.}

\noindent
Encoding:

\subsection{$\tau$-code}
$$\tau(x) = \beta(x)\#$$
\subsection{$\tau'$-code}
$$\tau'(x) = \beta'(x)\#$$

\section{Elias codes}
Encoding\sidenote{$\gamma$-codes are uniquely decodable codes with variable length. Uniquely decodable $\alpha$-code is combined with non-uniquely decodable $\beta$-code. The difference between $\gamma'$-code and $\gamma$-code is that $\gamma$-code is interleaved. $\delta$-code is similar to $\gamma'$-code, but $\alpha$-code is replaced with $\gamma$-code.}:

\subsection{$\gamma'$-code}
$$\gamma'(x)=\alpha(|\beta(x)|)\beta'(x)$$

\subsection{$\gamma$-code}
$$y=\alpha(|\beta(x)|), z=\beta'(x)$$
$$\gamma(x)=y_1,z_1,y_2,z_2,\ldots,z_{|\beta(x)|-1},y_{|\beta(x)|}$$

\subsection{$\delta$-code}
$$\gamma(x)=\gamma(|\beta(x)|)\beta'(x)$$

\noindent
The $\omega$-codes have recursive prefix. The value is encoded by $\beta$-code followed with $0$. If the encoded $\beta$-code value is longer than $k, k \geq 2$ then a prefix is recursively prolonged by the prepending the value of $\beta$-code of the length of the previous part encoded by $\beta'$-code (length of $\beta$-code $-1$). If the length of $\beta$-code is shorted then $k$ then zerozes are prepended to make it of length $k$.

\noindent
Encoding\sidenote{$\omega$-codes are uniquely decodable codes with variable length. They do not have the ability to encode value $0$ or negative numbers directly}

\subsection{$\omega$-code ($k=2$)}
$$\omega(1) = 0^{k-1} = 0$$
$$\omega(x)= \text{<prefix>}\beta(x)0$$

\subsection{$\omega'$-code ($k=3$)}
$$\omega(1) = 0^{k-1} = 00$$
$$\omega(x)= \text{<prefix>}\beta(x)0$$

\section{Ternary Comma Code}
It is a simple code based on base 3 number representation and comma (c), which gives the following encoding:

\begin{table}
\begin{tabular}{|c|c|c|c|}\hline
    0 & 1 & 2 & c\\\hline
    00 & 01 & 10 & 11\\\hline
\end{tabular}
\caption{Ternary Comma Code}
\end{table}

\section{Fibbonacci Code}
Binary number is representation where each position corresponts to $2^i, i \in \mathbb{N}_0$. These powers of $2$ can be raplaced with numbers from Fibbonacci sequence, excluding the initial $1$:
$$1, 2, 3, 5, 8, 13, 21, 34\ldots$$.

This gives us multiple representations of each number as the two consecutive numbers can always be replaced with the next one, i.e. $11$ has the same meaning as $100$, for example:
$$1*5+1*3=1*8+0*5+0*3.$$
Because of this, each number can be represented using this Fibbonacci representation as a sequeance of $0$ and $1$, where no two consecutive $1$ are present ($11$). When such number is reversed and additional $1$ is appended, it leads to a code, where each additional $1$ marks the end of the keyword.

\section{Golomb Code}
$GC(x,m)$ is Golomb Code of $x$ in modulo $m$.

\noindent
Quotient:
$$q=\floor{x/m}.$$
Reminder:
$$r=x-qm.$$
Coeficient:
$$c=\ceil{\log_2 m}.$$

\noindent
Reminder table $R$:\\
\begin{tabular}{r l l l l}
    $2^c-m$ & values of & $r$ & $\beta$-code encoded by $c-1$ & bits,\\
    $2m-2^c$ & values of & $r+2^c-m$ & $\beta$-code encoded by $c$ & bits.
\end{tabular}

\noindent
Encoding:
$$GC(x,m)=\alpha'(q+1)R(r).$$

\subsection{Examples of reminder tables}
$R$ for $m=3$:

\begin{table}
\begin{tabular}{|c||c|c|}\hline
    0 & 1 & 2\\\hline
    0 & 10 & 11\\\hline
\end{tabular}
\caption{Golomb Code $R$ for $m=3$}
\end{table}

\noindent
$R$ for $m=4$:

\begin{table}
\begin{tabular}{||c|c|c|c|}\hline
    0 & 1 & 2 & 3\\\hline
    00 & 01 & 10 & 11\\\hline
\end{tabular}
\caption{Golomb Code $R$ for $m=4$}
\end{table}

\noindent
$R$ for $m=5$:

\begin{table}
\begin{tabular}{|c|c|c||c|c|}\hline
    0 & 1 & 2 & 3 & 4\\\hline
    00 & 01 & 10 & 110 & 111\\\hline
\end{tabular}
\caption{Golomb Code $R$ for $m=5$}
\end{table}

\noindent
$R$ for $m=6$:

\begin{table}
\begin{tabular}{|c|c||c|c|c|c|}\hline
    0 & 1 & 2 & 3 & 4 & 5\\\hline
    00 & 01 & 100 & 101 & 110 & 111\\\hline
\end{tabular}
\caption{Golomb Code $R$ for $m=6$}
\end{table}

\noindent
$R$ for $m=7$:

\begin{table}
\begin{tabular}{|c||c|c|c|c|c|c|}\hline
    0 & 1 & 2 & 3 & 4 & 5 & 6\\\hline
    00 & 010 & 011 & 100 & 101 & 110 & 111\\\hline
\end{tabular}
\caption{Golomb Code $R$ for $m=7$}
\end{table}

\noindent
$R$ for $m=8$:

\begin{table}
\begin{tabular}{||c|c|c|c|c|c|c|c|}\hline
    0 & 1 & 2 & 3 & 4 & 5 & 6 & 7\\\hline
    000 & 001 & 010 & 011 & 100 & 101 & 110 & 111\\\hline
\end{tabular}
\caption{Golomb Code $R$ for $m=8$}
\end{table}

\noindent
$R$ for $m=14$:

\begin{table*}
\begin{tabular}{|c|c||c|c|c|c|c|c|c|c|c|c|c|c|}\hline
    0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & 13\\\hline
    000 & 001 & 0100 & 0101 & 0110 & 0111 & 1000 & 1001 & 1010 & 1011 & 1100 & 1101 & 1110 & 1111\\\hline
\end{tabular}
\\
\caption{Golomb Code $R$ for $m=14$}
\end{table*}

\section{Rice Code}
$RC(x,m) = GC(x,m)$ if $m = 2^k, k \in \mathbb{N}_0$.

%\section{Properties of codes}

%Universal, Asymptotically optimal

%\section{Examples}

%\begin{table*}
%\begin{tabular}{|c|r|r|r|r|r|r|r|r|r|r|r|r|r|}\hline
%    x & $\alpha$-code & $\beta$-code & $\beta'$-code & $\gamma'$-code & $\gamma$-code & $\delta$-code & $\omega$-code & $\omega'$-code \\\hline
%    0  & -         & $0$      & -       & $\blue1$      & $\blue1$      & $\blue1$      & & \\
%    1  & $1$       & $1$      & $\varepsilon$ & & & & & \\
%    2  & $01$      & $10$     & $0$     & $\blue01\red0$     & $0$     & $0$     & & & \\
%    3  & $001$     & $11$     & $1$     & $\blue01\red1$     & $1$     & $1$     & & \\
%    4  & $0^31$    & $100$    & $00$    & $\blue001\red00$    & $00$    & $00$    & & \\
%    5  & $0^41$    & $101$    & $01$    & $\blue001\red01$    & $01$    & $01$    & & \\
%    6  & $0^51$    & $110$    & $10$    & $\blue001\red10$    & $10$    & $10$    & & \\
%    7  & $0^61$    & $110$    & $11$    & $\blue001\red10$    & $10$    & $10$    & & \\
%    8  & $0^71$    & $1000$   & $000$   & $\blue0001\red000$   & $000$   & $000$   & & \\
%    9  & $0^81$    & $1001$   & $001$   & $\blue0001\red001$   & $001$   & $001$   & & \\
%    10 & $0^91$    & $1010$   & $010$   & $\blue0001\red010$   & $010$   & $010$   & & \\
%    11 & $0^{10}1$ & $1011$   & $011$   & $\blue0001\red011$   & $011$   & $011$   & & \\
%    12 & $0^{11}1$ & $1100$   & $100$   & $\blue0001\red100$   & $100$   & $100$   & & \\
%    13 & $0^{12}1$ & $1101$   & $101$   & $\blue0001\red101$   & $101$   & $101$   & & \\
%    14 & $0^{13}1$ & $1110$   & $110$   & $\blue0001\red110$   & $110$   & $110$   & & \\
%    15 & $0^{14}1$ & $1111$   & $111$   & $\blue0001\red111$   & $111$   & $111$   & & \\
%    16 & $0^{15}1$ & $10000$  & $0000$  & $\blue00001\red0000$  & $0000$  & $0000$  & & \\
%    23 & $0^{22}1$ & $10111$  & $0111$  & $\blue00001\red0111$  & $0111$  & $0111$  & & \\
%    24 & $0^{23}1$ & $11000$  & $1000$  & $\blue00001\red1000$  & $1000$  & $1000$  & & \\
%    31 & $0^{30}1$ & $11111$  & $1111$  & $\blue00001\red1111$  & $1111$  & $1111$  & & \\
%    32 & $0^{31}1$ & $100000$ & $00000$ & $\blue000001\red00000$ & $00000$ & $00000$ & & \\\hline
%\end{tabular}
%\\
%\caption{Examples of coding of integers}
%\end{table*}


\section{Computational Power of the Equipment}

\lettrine[nindent=0em,lines=3]{I}n this chapter we present an analysis on the features of the available equipment for our experiments and final implementation of the system. We will make emphasis on the number of operations per second (FLOPS) that our equipment can handle.

Floating Point Operations per Second (FLOPS) is a measure of computer performance, useful in fields of scientific computations that require floating-point calculations. For such cases it is a more accurate measure than measuring instructions per second \cite{flops_wikipedia}. The FLOPS value can be used to determine the power for both the CPU and GPU.

The amount of Giga FLOPS (GFLOPS) for each node/computer can be calculated by using the following equation \cite{peak_theoretical_performance}.

\begin{equation}
GFLOPS = \textit{CPU speed in GHz} \times \textit{number of CPU cores} \times \textit{CPU instruction per cycle} \times \textit{number of CPUs per node}
\label{flops_eq_cpu}
\end{equation}

For the case of Graphics Processing Units (GPUs), the amount of flops the GPU can support can be computed by \cite{gpu_flops}

\begin{equation}
FLOPS = 2 \times \textit{number of parallel GPU processing cores} \times \textit{peak clock speed in MHz}
\label{flops_eq_gpu}
\end{equation}

The computer used for our experiments has an Intel Core i7-7700K Processor; with a CPU speed of 4.2 GHz average and 4.5 GHz overclocked, 4 cores \cite{cpu_tech_specs}, and 16 IPC \cite{determine_number_flops_cpu}.  Using these values and eq. \eqref{flops_eq_cpu}, the FLOPS range for the CPU is: 268.8 GFLOPS to 288 GFLOPS.

The computer used for our experiments also comes with a dedicated GPU. The GPU used in our system is a NVDIA GeForce GTX 1080Ti with 11GB GDDR5X; with a boost clock of 1582 MHz and 3584 NVIDIA CUDA Cores \cite{gpu_tech_specs}. Using the given values and eq. \eqref{flops_eq_gpu} the FLOPS value for the GPU is 11339.776 GFLOPS which is approximately 11,300 GFLOPS.

By combining the FLOPs value for both the CPU and GPU, the total FLOPs range of the server is 11608.576 GFLOPS to 11627.776 GFLOPS. This can be converted to FLOPS/month by multiplying the given range by $2592000$, using the assumption that a month is 30 days. This gives a final FLOPS/month of $3.00894 \times 10^{19}$ FLOPS/month to $3.01392 \times 10^{19}$ FLOPS/month.


\section{Spherical Harmonics}
Since $V\to 0$ at $r\to\infty$, then $a_{lm}=0$.
Impose boundary condition at $r=a$.
\begin{align}
    \sum_{l=0}^{n}\sum_{m=-l}^{+l}
    \frac{b_{lm}}{a^{l + 1}} Y_{lm}\left( \theta, \phi \right)
    =
    V_0 \left( \theta, \phi \right)
\end{align}
note
\begin{align}
    V\left( \theta, \phi \right)
    &=
    \sum_{l,m}
    \left( a_{lm}
    r^l
    +
    \frac{b_{lm}}{r^{l + 1}}\right)
    Y_{lm}\left( \theta, \phi \right)
\end{align}
Use orthogonality of spherical harmonics to find $b_{lm}$,
\begin{align}
    \int d\Omega \, Y_{l'm'}^* \left( \theta,\phi \right)
    \left\{
    \sum_{l,m}
    \frac{b_{lm}}{a^{l + 1}}
    Y_{lm\left( \theta, \phi \right)}
    \right\}
    &=
    \int d\Omega\,
    Y_{l'm'}^*\left( \theta, \phi \right)
    V_0\left( \theta, \phi \right)
\end{align}
so
\begin{align}
    b_{lm}
    &=
    a^{l + 1}
    \int d\Omega\,
    Y_{lm}^*\left( \theta, \phi \right)
    V_0\left( \theta, \phi \right)
\end{align}
so finally
\begin{align}
    V\left( r, \theta, \phi \right)
    &=
    \sum_{l=0}^{\infty}
    \int_{m=-l}^{+l}
    \left( \frac{a}{r} \right)^{l + 1}
    Y_{lm}\left( \theta, \phi \right)
    \int d\Omega'
    Y_{lm}^*\left( \theta', \phi' \right)
    V_0\left( \theta', \phi' \right)
\end{align}
I'll show you your formula sheet before the exam.
Usually you just express the answer in terms of Legendre polynomials.
Usually you're going to leave your answer in terms of the $Y_{lm}$ except where
there is spherical symmetry and you can write it in terms of Legendre
polynomials.

You can either not bother with $Y_{lm}$ and use Legendre polynomials,
or just use spherical harmonics and reduce it to the Legendre polynomials.
Remember the relation between the spherical harmonics and the Legendre
polynomials.

Let's do another example.
\begin{example}
    Find the Dirichlet Green function for a sphere of radius $b$ centered at
    origin.
    \begin{align}
        \nabla^2 G\left( \vec{r}, \vec{r}' \right)
        &=
        -4\pi \delta^{3}\left( \vec{r} - \vec{r}' \right)
    \end{align}
    such that $G\left( \vec{r}, \vec{r}' \right) = 0$
    at $\left|\vec{r}\right| = b$.
\end{example}
This is a very standard problem.
I have a charge here somewhere in the sphere and I want to find the charge here.
It's Laplace's equation except for that one point,
so we look for a solution like that.

Awa from $\vec{r}=\vec{r}'$,
the Green function satisfies Laplace's equation
\begin{align}
    G\left( \vec{r}, \vec{r}' \right)
    &=
    \sum_{l=0}
    \sum_{m=-l}^{l}
    g_{lm}\left( r \right)
    Y_{l}^{\,m}\left( \theta, \phi \right)
\end{align}
So we look for a separable solution.
Now take this and plug it in to the definition of the Green function.

And then yo get
\begin{align}
    \sum_{l,m}
    \left\{
    \frac{d^2 g_{lm}}{dr^2}
    +
    \frac{2}{r}
    \frac{dg_{lm}}{dr}
    -
    \frac{l(l + 1)}{r^2}g_{lm}
    \right\}
    Y_{l}^{\,m}\left( \theta, \phi \right)
    &=
    -4\pi \delta^3 \left( \vec{r} - \vec{r}' \right)
\end{align}
Then we multiply both sides by ${Y^*}_{l'}^{\, m'}\left( \theta, \phi \right)$
and integrate over $\Omega$.
\begin{align}
    \frac{d^2 g_{l'm'}}{dr^2}
    +
    \frac{2}{r} \frac{dg_{l'm'}}{dr}
    -
    \frac{l'\left( l' + 1 \right)}{r^2} g_{l'm'}
    &=
    -4\pi
    \int d\Omega\,
    {Y^*}_{l'}^{\,m'}\left( \theta, \phi \right)
    \underbrace{\delta^3\left( \vec{r} - \vec{r}' \right)}_{
    \frac{\delta\left( r - r' \right)}{r^2}
    \delta\left( \cos\theta - \cos\theta' \right)
    \delta\left( \phi - \phi' \right)
    }\\
    &=
    -\frac{4\pi}{r^2}
    {Y^*}_{l'}^{\,m'}\left( \theta, \phi \right)
\end{align}
This is much simpler because this is just an ODE,
so like the 1D problem we solve.

Away from $r=r'$,
\begin{align}
    g_{lm}\left( r \right)
    &=
    A_{lm} r^l + B_{lm} \frac{1}{r^{l + 1}}
\end{align}
So it's just like Laplace's equation.
The only difference is that you have a sphere and a delta function source
somewhere here.
This source is at $\vec{r}'$.
So we have Laplace's equation everywhere except $\vec{r}'$.
So we're going to get one value of the $A_{lm}$ for $r<r'$
and another value for $r>r'$.
So we are solving Laplace's equation for $r<r'$ and then for $r>r'$
and then we stick the two solutions together.
This is similar to what we did when we were solving for the Greens function for
the box,
where we had a point charge in the middle of the box and we had to divide the
box into two pieces,
which we solved and matched the distributions,
did using a more careful sophisticated way using the convergence theorem,
but here I'm doing it much quicker,
as the two are mathematically equivalent,
and you won't have time in an exam to do all those divergence theorems.
So this is equivalent to what we did.
Here we are breaking the sphere into two pieces,
one $r<r'$ and $r>r'$,
just done much more directly than the box,
and this is much faster,
because very quickly it gets reduced to a 1D ODE.

So in general,
$A_{lm}$ and $B_{lm}$ will be different for $r<r'$
and $r>r'$.
This $1/r^{l+1}$ blows up when $r=0$,
so since $g_{lm}(r)$ must be regular at $r=0$,
we have
\begin{align}
    g_{lm}^{<}\left( r \right)
    &=
    A_{lm}^{<} r^l
\end{align}
which is the solution for $r < r'$.
Also, we need that
\begin{align}
    g_{lm}\left( r \right) = 0
\end{align}
at $r=b$,
so
\begin{align}
    g_{lm}^{>}\left( r \right)
    &=
    A_{lm}^{>}
    \left(
    r^l
    -
    \frac{b^{2l + 1}}{r^{l + 1}}
    \right)
\end{align}
for $r>r'$.
The solutions are different for $r<r'$ and $r>r'$,
which is similar to the solution for the box
where the solution was different above and below the box
and you had to stick the solutions together at the boundary.
So now let's stitch the solutions together.

Since solutions must be continuous at $r=r'$,
\begin{align}
    g_{lm}^{<}\left( r' \right)
    &=
    g_{lm}^{>}\left( r' \right)
\end{align}
so you get this condition
\begin{align}
    A_{lm}^{<}
    &=
    A_{lm}^{>}
    \left(
    1
    -
    \frac{b^{2l + 1}}{r'^{2l + 1}}
    \right)
\end{align}
And then integrate the ODE on both sides over $r$
from $r=r' - \epsilon$ to $r=r' + \epsilon$,
which is the region that contains the delta function.
Then you get
\begin{align}
    \left\frac{dg_{lm}}{dr}\right|_{r' + \epsilon}
    -
    \left\frac{dg_{lm}}{dr}\right|_{r' - \epsilon}
    &=
    -\frac{4\pi}{r'^2}
    {Y^*}_{l}^{m}\left( \theta', \phi' \right)
\end{align}
So this is the second condition.
And this is a lot messier.
\begin{align}
    A_{lm}^{>}
    \left\{
    l {r'}^{l - 1}
    +
    \left( l + 1 \right)
    \frac{b^{2l + 1}}{ {r'}^{l + 2}}
    \right\}
    -
    A_{lm}^{,}
    \left\{
    l {r'}^{l - 1}
    \right\}
    &=
    - \frac{4\pi}{ {r'}^2 }
    {Y^*}_{m}^{\, l} \left( \theta', \phi' \right)
\end{align}
And then you do some algebra,
and the final answer is
\begin{align}
    G\left( \vec{r}, \vec{r}' \right)
    &=
    \sum_{l,m}
    \frac{4\pi}{\left( 2l + 1 \right)}
    r_{<}^{l}
    r_{>}^{l}
    \left\{
    \frac{1}{r_{>}^{2l + 1}}
    -
    \frac{1}{b^{2l + 1}}
    \right\}
    {Y^*}_{l}^{m}\left( \theta', \phi' \right)
    Y_{l}^{\;m}\left( \theta, \phi \right)
\end{align}

\section{Cylindrical Coordinates}
\begin{example}
    Consider an infinitely long cylinder of radius $b$
    that is aligned so its axis is along the $z$-axis.
    The potential on surface is independent of $z$
    and given by $V_0(\phi)$.

    Find the potential inside the cylinder.
\end{example}
So the cylinder has radius $b$ and you're given the potential $V_0(\phi$ on the
boundary surface.
The problem is to solve for $\nabla^2 V=0$.

Once again we're going to use separation of variables.
\begin{align}
    \frac{1}{\rho}
    \frac{\partial}{\partial \rho}
    \left(
    \rho
    \frac{\partial V}{\partial \rho}
    \right)
    +
    \frac{1}{\rho^2}
    \frac{\partial^2 V}{\partial \phi^2}
    +
    \frac{\partial^2 V}{\partial z^2}
    &=
    0
\end{align}
Note it should be independent of $z$.
So we get
\begin{align}
    \frac{\partial^2 V}{\partial \rho^2}
    +
    \frac{1}{\rho}
    \frac{\partial V}{\partial \rho}
    +
    \frac{1}{\rho^2}
    \frac{\partial^2 V}{\partial \phi^2}
    &=
    0
\end{align}
Using separation of variables,
\begin{align}
    V\left( \rho, \phi \right)
    &=
    R\left( \rho \right)
    \Phi\left( \phi \right)
\end{align}
so the equation becomes separable
\begin{align}
    \frac{\rho^2}{R}
    \left\{
    \frac{d^2 R}{d\rho^2}
    +
    \frac{1}{\rho}
    \frac{dR}{d\rho}
    \right\}
    &=
    -\frac{1}{\Phi}
    \frac{d^2 \Phi}{d\phi^2}
    =\nu^2
\end{align}
where $\nu$ is a constant.
So we have the ODE
\begin{align}
    \frac{d^2\Phi}{d\phi^2}
    +
    \nu^2 \Phi
    &= 0
\end{align}
which has the solution
\begin{align}
    \Phi
    &=
    A \sin\left( \nu \phi \right)
    +
    B \cos\left( \nu \phi \right)
\end{align}
If $\phi$ runs from $0$ to $2\pi$,
$\Phi(\phi) = \Phi\left( \phi + 2\pi \right)$,
which means $\nu$ must be an integer.

Now for the other equation.
\begin{align}
    \frac{d^2 R}{d\rho^2}
    +
    \frac{1}{\rho}
    \frac{dR}{d\rho}
    -
    \frac{\nu^2}{\rho^2}R
    &=
    0
\end{align}
So the solution should be something like $R\sim \rho^\nu$
and $R\sim \rho^{-\nu}$.
There will be a homework problem with power law but imaginary power,
but for this problem it's not relevant now.
But what if $\nu=0$ so there are no two independent solutions?
Well in the case $\nu=0$,
then we have the special case
$R\left( \rho \right) =$ constant and $R\left( \rho \right) = \log \rho$.

Now we can expand out the solution
\begin{align}
    V\left( \rho, \phi \right)
    &=
    a_0 + b_0 \log\rho
    +
    \sum_{n=1}^{\infty}
    a_n \rho^n \sin\left( n\phi + \alpha_n \right)
    +
    \sum_{n=1}^{\infty} b_n \frac{1}{\rho^n}
    \cos\left( n \phi + \beta_n \right)
\end{align}
and this is the general solution.
Note that the $\frac{1}{\rho^n}$ blows up at the origin,
so we must have $b_n=0$ for our problem.
Suppose we were looking for solutions outside the cylinder,
then in that case it wouldn't be.
But for our case,
$b_n=0$.
In fact, $b_0$ is also $0$ because the logarithm also blows up at $\rho=0$.

So then for our particular problem,
\begin{align}
    V\left( \rho, \phi \right)
    &=
    a_0
    +
    \sum_{n=1}^{\infty}
    a_n \rho^n \sin\left( n\phi + \alpha_n \right)
\end{align}
Now we have to determine these coefficients.
What we use to determine these coefficients is that we know
this is equal to $V_0(\phi)$ at $\rho = b$,
so it's kind of like a Fourier series with the sum of trigonometric functions.
First let's rewrite this as the standards Fourier series
\begin{align}
    V\left( \rho, \phi \right)
    &=
    \frac{1}{2} c_0
    +
    \sum_{n=1}^{\infty}
    \rho^n\left\{
    c_n \cos\left( n\phi \right)
    +
    d_n \sin\left( n\phi \right)
    \right\}
\end{align}
so it's been expanded in sine and cosine term.s.
At the surface of the cylinder $\rho=b$,
\begin{align}
    V\left( b, \phi \right)
    &=
    V_0
    +
    \frac{1}{2} b_0
    \sum_{n=1}^{\infty}
    b^n
    \left\{
    c_n \cos\left( n\phi \right)
    + d_n \sin\left( n\phi \right)
    \right\}
\end{align}
and now this really is exactly the form of a Fourier series,
so now you can invert it to find the $c_n$ and $d_n$ coefficients.
You can use the standard formula for Fourier series.
\begin{align}
    c_n &=
    \frac{1}{\pi}
    \frac{1}{b^n}
    \int_{0}^{2\pi}
    d\phi'\,
    V_0\left( \phi' \right)
    \cos\left( n\phi' \right)\\
    d_n &=
    \frac{1}{\pi}
    \frac{1}{b^n}
    \int_{0}^{2\pi}
    d\phi'
    V_0\left( \phi' \right)
    \sin\left( n\phi' \right)
\end{align}
and it turns out with a page of algebra and mathematical tricks,
it turns out you can resum this.
Go through the notes to see the trick,
and there is a homework problem where you have to use this trick.
The steps themselves are not very difficult,
just follow the in the notes.


%-----------------------------------------------------------------------------------------------
\section{General Solution}

    Owing to Definition~\ref{def:polyekthetic} being very broad, the focus  now  shifts  towards
    the more applicable Definition~\ref{def:polyekthetic.exponent}, i.e., in finding expressions
    for constant-property polyekthetic exponents $k_{ij\ell}$.

    %---------------------------------------------------------------------------------------
    \subsection{In Partial Differential Notation}

    Let property $\ell\!:\!\ell(i, j)$; then,  the  total  differential  of  $\ell$  is,  for  a
    const-$\ell$ process:
    %
    \begin{equation}
        d\ell = \parxyz\ell ij\,di + \parxyz\ell ji\,dj = 0
        \label{eq:polyek.dl}
    \end{equation}
    %
    \noindent which must be recast into an ODE  whose  solution,  \emph{at  least  locally},  is
    Eq.~(\ref{eq:polyekthetic}). Isolating $di$:
    %
    \begin{equation}
        di = -\parxyz\ell ji \parxyz i\ell j\,dj,
        \label{eq:polyek.di}
    \end{equation}
    %
    \noindent which can be arranged as:
    %
    \begin{equation}
        \frac{di}{i} = \frac{-j}{i} \parxyz\ell ji \parxyz i\ell j \frac{dj}{j}.
        \label{eq:polyek.ode}
    \end{equation}

    The polyekthetic exponent, $k_{ij\ell}$, is therefore:
    %
    \begin{equation}
        k_{ij\ell} = \frac{j}{i} \parxyz\ell ji \parxyz i\ell j.
        \label{eq:polyek.k.raw}
    \end{equation}

    Using the cyclic relationship:
    %
    \begin{equation}
        -1 = \parxyz ji\ell \parxyz\ell ji \parxyz i\ell j,
        \label{eq:cyclic}
    \end{equation}
    %
    \noindent the polyekthetic exponent can be rewritten as:
    %
    \begin{equation}
        k_{ij\ell} = \frac{-j}{i} \parxyz ij\ell.
        \label{eq:polyek.k}
    \end{equation}

    Eq.~(\ref{eq:polyek.k}) is thus the general solution for $k_{ij\ell}$.

    %---------------------------------------------------------------------------------------
    \subsection{In Terms of Bridgman's Relations}

    Perhaps the easiest way of expressing any polyekthetic  exponent  $k_{ij\ell}$  (for  simple
    properties $\ell$) in terms of quantities measurable in laboratory is  by  rewriting  it  in
    terms of Bridgman's relations~\cite{2006-BejanA-Wiley}, which are expressed in  terms  of  a
    peculiar notation.
    %
    \begin{equation}
        \frac{\bri i\ell}{\bri j\ell} \equiv \parxyz ij\ell.
        \label{eq:Bridgman}
    \end{equation}

    Please note that the $\equiv$ sign on Eq.~(\ref{eq:Bridgman}) indicates  the  definition  of
    the \emph{ratio} between shown Bridgman's primitives $\bri i\ell$ and $\bri j\ell$ in  terms
    of $\inlxyz ij\ell$, rather than the other way around.

    Bridgman's relations are tabulated, and can be found on reference~\cite{2006-BejanA-Wiley}.

    The general expression for $k_{ij\ell}$, using Bridgman's relations notation, is therefore:
    %
    \begin{equation}
        k_{ij\ell} = \frac{-j}{i}\frac{\bri i\ell}{\bri j\ell}.
        \label{eq:k.bri}
    \end{equation}

    %---------------------------------------------------------------------------------------
    \subsection{Properties of Polyekthetic Exponents}

    \begin{theorem}\label{the:k.recip}
        if  $k_{ij\ell}$  is  a  constant-property  process  polyekthetic  exponent,  then   the
        constant-property process polyekthetic exponent
        %
        \begin{equation}
            k_{ji\ell} = \frac{1}{k_{ij\ell}}.
            \label{eq:k.recipr}
        \end{equation}
    \end{theorem}

    \begin{proof}
        Let  $k_{ij\ell}$  be  a  constant-property   process   polyekthetic   exponent,   then,
        Eq.~(\ref{eq:polyek.k}) holds.

        Applying Eq.~(\ref{eq:polyek.k}) for a $k_{ji\ell}$, i.e.,  with  swapped  $i$  and  $j$
        property indices, gives :
        %
        \begin{align}
            k_{ji\ell} &= \frac{-i}{j} \parxyz ji\ell
                       & \rightharpoondown\\
            k_{ji\ell} &= \left[\frac{-j}{i} \parxyz ij\ell\right]^{-1}
                       & \rightharpoondown\\
            k_{ji\ell} &= \left(k_{ij\ell}\right)^{-1} = \frac{1}{k_{ij\ell}},
        \end{align}
        %
        \noindent thus proving the theorem.
    \end{proof}

    \begin{theorem}\label{the:k.consti}
        All constant-property process polyekthetic exponents $k_{iji} = 0$.
    \end{theorem}

    \begin{proof}
        Let  $k_{ij\ell}$  be  a  constant-property   process   polyekthetic   exponent,   then,
        Eq.~(\ref{eq:polyek.k}) holds.

        Applying  Eq.~(\ref{eq:polyek.k})  for  a  $k_{iji}$,  i.e.,   obtaining   a   const-$i$
        polyekthetic exponent, gives
        %
        \begin{align}
            k_{iji} &= \frac{-j}{i} \parxyz iji
                    & \rightharpoondown\\
            k_{iji} &= 0,
        \end{align}
        %
        \noindent by properties of partial derivatives, thus proving the theorem.
    \end{proof}

    \begin{theorem}\label{the:k.constj}
        All constant-property process polyekthetic exponents $k_{ijj} \to \pm\infty$.
    \end{theorem}

    \begin{proof}
        From Theorem~\ref{the:k.recip}, one has
        %
        \begin{equation}
            k_{ijj} = 1 / k_{jij},
        \end{equation}
        %
        \noindent which, from Theorem~\ref{the:k.consti} which establishes $k_{jij} = 0$,  leads
        to
        %
        \begin{equation}
            k_{ijj} = 1 / 0 \to \pm\infty,
        \end{equation}
        %
        \noindent thus proving the theorem.
    \end{proof}

    Moreover, from Theorems~\ref{the:k.consti} and~\ref{the:k.constj}, whenever property indices
    are repeated, the corresponding polyekthetic exponent $k$ is a trivial one.

    If properties $i$, $j$, and $\ell$, $i \neq j  \neq  \ell  \neq  i$,  are  chosen  from  the
    ``usual'' $\{P, T, v, u, h, s, a, g\}$ engineering thermodynamics  intensive  property  set,
    there will be $8!/5! = 336$ combinations of non-trivial $k_{ij\ell}$. However,  due  to  the
    reciprocity  property  of  Theorem~\ref{the:k.recip},  there  will  be  $168$   non-trivial,
    non-reciprocal $k_{ij\ell}$ required to easily deduce the remaining ones.

%-----------------------------------------------------------------------------------------------


%!TEX root = ../notes.tex
\section{February 1, 2022}
\todo{Happy Lunar New Year! \faHandPeaceO}

\emph{(Thanks Qinan and Andrew for allowing me to shamelessly copy their notes.)}

\subsection{Divisibility and Factorization}
We start with some commonly used notation:
\begin{definition}[Divisibility]
    We use $a\mid b$ to mean ``$a$ divides $b$'' and $a\nmid b$ to mean ``$a$ does not divide $b$''.
\end{definition}

Now for a series of definitions:

\begin{definition}[Primality]
    A positive integer $p\geq 2$ is said to be \ul{prime} if its only positive divisors are $1$ and $p$.
\end{definition}

\begin{definition}[Positive Integers]
    $\ZZ_+$ will denote the \ul{positive integers}.
\end{definition}

\begin{definition}[Order]
    For a nonzero $n\in \ZZ$ and a prime $p$, there is a nonnegative integer $a$ such that $p^a\mid n$ but $p^{a + 1}\nmid n$. This number $a$ is called the \ul{order} of $n$ at $p$, denoted by $\ord_p n$.

    For $n=0$, we set $\ord_p 0 = \infty$. We also have $\ord_p n = 0\Leftrightarrow p\nmid n$.
\end{definition}

We prove a lemma as warm-up:
\begin{lemma}[Existence of Factorization]
    Every nonzero integer can be written as a product of primes.

    \emph{We make an exception for $-1$. The empty product is $1$ so $1$ is fine.}
\end{lemma}
\begin{proof}
    Suppose for the sake of contradiction otherwise, that some nonzero integer can be written as a product of primes. Let $N$ be the smallest integer greater than $2$ that cannot be written as a product of primes.

    $N$ had better not be a prime number itself (since then it would be a product of itself). Then we can write $N = a\cdot b$ where $1 < a, b < N$.

    Since we took $N$ as the least such number that cannot be written as a product of primes, $a$ and $b$ which are less than $N$ can be written as a product of primes. Then $N$ is a product of primes since $a$ and $b$ individually are. This is a contradiction! Thus it had better be the case that \emph{every} nonzero integer can be written as a product of primes.
\end{proof}

This is the theorem we will eventually work toward proving:

\begin{theorem}[Unique Factorization]\label{thm:unique-factorization}
    Every nonzero integer $n$ yields a \emph{unique} prime factorization
    \begin{equation*}
        n = (-1)^\varepsilon\cdot \prod_{p}p^{a(p)}, a(p)\geq 0
    \end{equation*}
    where $\varepsilon = 0$ or $1$, and $\varepsilon, a(p)$ are uniquely determined by $n$. Moreover, we note that $a(p) = \ord_p n$.
\end{theorem}

\subsection{Euclidean and Principal Ideal Domains}
Before this proof, we first recall a conclusion from Math 1530:
\begin{lemma}[Division Lemma]\label{division-lemma}
    If $a, b\in \ZZ$ and $b>0$, then there exists $q, r\in \ZZ$ such that
    \[a = bq + r\]
    with $0\leq r < b$.
\end{lemma}
\begin{proof}
    Consider the set
    \[S = \{a - xb \mid x\in \ZZ\}\]
    We note that $S$ contains \emph{some} positive elements. Let $r = a - qb$ be the least nonnegative element of $S$.

    We claim that $0\leq r < b$. Suppose for the sake of contradiction otherwise, then $r = a - qb \geq b$ gives $a - qb - b \leq 0$ and $a - (q+1)b\leq 0$. Which is a contradiction since we took $r$ to be a the least nonnegative element in $S$ and we've found such smaller element $a - (q+1)b$.

    Then it had better be that $0\leq r < b$ for some $r, q\in\ZZ$.
\end{proof}
\begin{corollary}\label{cor:z-euclidean}
    $\ZZ$ is a Euclidean domain, with a Euclidean function given by \cref{division-lemma}.
\end{corollary}

\begin{definition}[Euclidean Domain]
    Let $R$ be an integral domain. $R$ is a \ul{Euclidean domain} if there exists a function $\lambda: R\setminus \{0\} \to \NN$ such that if $a, b\in R$ with $b\neq 0$, then there exists some $c, d\in R$ with the property that $a = cb + d$ with $d=0$ or $\lambda(d) < \lambda(b)$.
\end{definition}
\begin{example}
    $\ZZ$ is a Euclidean domain with $\lambda$ function given in \cref{division-lemma}.

    $R[x]$ for field $R$ is also a Euclidean domain, with $\lambda = \deg$.

\end{example}

\begin{proposition}
    If $R$ is a Euclidean domain, then $R$ is a principal ideal domain. That is, if $I\subseteq R$ is an ideal, then $\exists a\in R$ such that $I = Ra = \{ra\mid r\in R\}$.
\end{proposition}
\begin{proof}
    Assume WLOG that $I$ is not the trivial ideal $I\neq (0)$. Let $0\neq a\in I$ such that $\lambda(a)\leq \lambda(b) \forall b\in I, b\neq 0$.

    We claim that $I = (a) = Ra$.

    We know that $Ra\subseteq I$ since $I$ is an ideal. Let $b\in I$. Then $\exists c, d\in R$ such that $b = ca + d$ where $d = 0$ or $\lambda(d) < \lambda(a)$. Now we have $d = b-ca\in I$, so we can't have $\lambda(d) < \lambda(a)$. Thus $d = 0$, so $b=ca\in Ra$.

    Hence we have $I\subseteq Ra$. Together, we conclude that $I = Ra$.
\end{proof}

\begin{definition}[Principal Ideals, PIDs]
    If $I = (a)$ for some $a\in I$, then $I$ is said to be a \ul{principal ideal}.

    $R$ is a \ul{principal ideal domain} (PID) if every ideal of $R$ is principal.
\end{definition}

Here are some important properties of PIDs:
\begin{enumerate}
    \item Nonunit irreducible elements are exactly the prime elements in $R$.

          \recall $p\in R$ is \ul{irreducible} if $a\mid p\Rightarrow a$ is either a unit or an associate of $p$.

          $p\in R$ is \ul{prime} if $p\mid ab\Rightarrow p\mid a$ or $p\mid b$ and $p$ is a nonzero, nonunit of $R$.

    \item GCDs always exist in PIDs.
\end{enumerate}

\subsection{Unique Prime Factorization}
We're nearly ready to prove unique factorization, after a lemma:
\begin{lemma}\label{additive-orders}
    Suppose $p$ is a prime, and $a, b\in Z$. Then $\ord_p(ab) = \ord_p a + \ord_p b$.
\end{lemma}
\begin{proof}
    WLOG, assume $a, b\neq 0$. We let
    \begin{align*}
        \alpha & = \ord_p a \\
        \beta  & = \ord_p b
    \end{align*}
    Then we have
    \begin{align*}
        a & = p^\alpha\cdot c \text{ where }p\nmid c \\
        b & = p^\beta\cdot d \text{ where }p\nmid d
    \end{align*}
    Thus, $ab = p^{\alpha + \beta}\cdot cd$. We have that $p\nmid cd$ since $p\nmid c$ and $p\nmid d$ (we rely on the fact that if $p$ is irreducible, $p$ is prime). Thus we have that $\ord_p (ab) = \alpha + \beta$.
\end{proof}

\begin{proof}
    (of \cref{thm:unique-factorization}, that $\ZZ$ is a UFD). Recall that for a nonzero $n\in \ZZ$, we write
    \[n = (-1)^\varepsilon \prod_{p}p^{a(p)}, \text{where }\varepsilon = 0\text{ or }1\text{ and }a(p)\geq 0\]
    Given a positive prime $q$, we take $\ord_q$ of both sides. By \cref{additive-orders}, this yields
    \[\ord_q n = \varepsilon\cdot \ord_q (-1) + \sigma_p a(p)\ord_q(p)\]
    Since we have that $\ord_q(-1) = 0$ and $\ord_q(p) = 0, \forall p\neq q$, we've uniquely determined $a(q)$ since $\ord_q(n) = a(q)$. That is, $a(q)$ is \emph{uniquely determined} for all primes $q$. So $n$ has a \emph{unique} prime factorization.
\end{proof}

\subsection{Greatest Common Divisors}
\begin{definition}
    Let $R$ be an integral domain. Then $d\in R$ is said to be a $\gcd$ of two elements $a, b$ if
    \begin{enumerate}[i)]
        \item $d\mid a$ and $d\mid b$,
        \item if $d'\mid a$ and $d'\mid b$, then $d'\mid d$.
    \end{enumerate}
\end{definition}
\begin{remark*}
    An aside for ring theory enthusiasts: $\gcd$ domains are a class of rings mroe general than PIDs or UFDs.
\end{remark*}
We will denote $(a, b)$ as the $\gcd$ of $a$ and $b$.

\textbf{Caution, however!} $\gcd$'s are only unique up to units.

\begin{example*}
    $-5$ and $5$ are both $\gcd$s of $-5$ and $10$ since $-1$ is a unit.
\end{example*}

We will make the convention that the $\gcd$ of 2 integers is the positive $\gcd$, that is, $(-5, 10) = 5$.

An edge case is that $\gcd(0, 0)=0$.


\section{Model Description}

The gravity effector module is responsible for calculating the effects of gravity from a body on a spacecraft. A spherical harmonics model and implementation is developed and described below. The iterative methods used for the software algorithms are also described. Finally, the results of the code unit tests are presented and discussed.

\subsection{Mathematical model}

\subsubsection{Relative Gravitational Dynamics Formulation}

The gravity effector module is critical to the propagation of spacecraft orbits in Basilisk. In order to increase the accuracy of spacecraft trajectories, a relative gravitational acceleration formulation can be used. Relative dynamics keep the acceleration, velocity, and distance magnitudes small, allowing more bits of a double variable to be used for accuracy, rather than magnitude. This additional accuracy is compounded via integration. This relative formulation is enforced when a user sets any planet in a multi-planet environment to have \verb|isCentralBody = True|.

If no planets in a simulation are set as the central body, then an absolute formulation of gravitational acceleration is used. In the absolute formulation, acceleration of a spacecraft due to each massive body is summed directly to calculate the resultant acceleration of the spacecraft.

\begin{equation}
	\ddot{\bm{r}}_{B/N, \mathrm{grav}} = \sum_{i = 1}^{n} \ddot{\bm{r}}_{B/N, i}
\end{equation}
where the accelerations on the right hand side are the acceleration due to the $i^{\mathrm{th}}$ planet which is being modeled as a gravity body. In this absolute mode, spacecraft position and velocity are integrated with respect to the inertial origin, typically solar system barycenter.

In the relative formulation, the acceleration of the spacecraft is calculated \textit{relative to} the central body. This is done by calculating the acceleration of the central body and subtracting it from the acceleration of the spacecraft.

\begin{equation}
	\ddot{\bm{r}}_{B/C, \mathrm{grav}} =\ddot{\bm{r}}_{B/N, \mathrm{grav}} - \ddot{\bm{r}}_{C/N, \mathrm{grav}}
\end{equation}

where $C$ is the central body. In this case, other accelerations of the central body (due to solar radiation pressure, for instance) are ignored.  For relative dynamics, the Basilisk dynamics integrator uses only \textit{relative} acceleration to calculate \textit{relative} position and velocity. The gravity module then accounts for this and modifies the spacecraft position and velocity by the central body's position and velocity after each timestep.

The above relative formulation leads to some questions regarding the accuracy of the dynamics integration. First, if acceleration due to gravity is being handled in a relative form, but accelerations due to external foces are handled absolutely, does Basilisk always produce the correct absolute position and velocity? Second, if dynamic state effectors such as hinged rigid bodies are using the gravitational acceleration that the spacecraft receives from the gravity module, are their states being integrated correctly?

Absolute accelerations (i.e. due to thrust) being integrated alongside the relative gravitational acceleration is handled easily due to the linearity of integration. In the absolute dynamics formulation there is:

\begin{equation}
	\ddot{\bm{r}}_{B/N} = \ddot{\bm{r}}_{B/N, \mathrm{grav}}  + \ddot{\bm{r}}_{B/N, \mathrm{thrust}} + \ddot{\bm{r}}_{B/N, \mathrm{SRP}} + \dots
	\label{eq:absGrav}
\end{equation}
and each term can be integrated separately on the right side so that
\begin{equation}
      \bm{r}_{B/N} = \int \int \ddot{\bm{r}}_{B/N, \mathrm{grav}} \mathrm{dtdt} +   \int \int \ddot{\bm{r}}_{B/N, \mathrm{thrust}} \mathrm{dtdt} +  \int \int \ddot{\bm{r}}_{B/N, \mathrm{SRP}}\mathrm{dt dt} +  \dots
\end{equation}
In the derivation that follows, the double integral to position is used, but the logic holds for the first integral to velocity as well. Now, because accelerations also add linearly,
\begin{equation}
\ddot{\bm{r}}_{B/N} = \ddot{\bm{r}}_{B/C} + \ddot{\bm{r}}_{C/N} = \ddot{\bm{r}}_{B/C, \mathrm{grav}} +  \ddot{\bm{r}}_{C/N, \mathrm{grav}}  + \ddot{\bm{r}}_{B/N, \mathrm{thrust}} + \ddot{\bm{r}}_{B/N, \mathrm{SRP}} + \dots
\end{equation}
which differs from Eq. \ref{eq:absGrav} in the gravitational acceleration of the spacecraft being split at the acceleration of the central body. Applying the integrals:
\begin{equation}
\bm{r}_{B/N} = \bm{r}_{B/C} + \bm{r}_{C/N} = \int \int \ddot{\bm{r}}_{B/C, \mathrm{grav}} \mathrm{dt dt} + \bm{r}_{C/N}  +   \int \int \ddot{\bm{r}}_{B/N, \mathrm{thrust}} \mathrm{dt dt} +  \int \int \ddot{\bm{r}}_{B/N, \mathrm{SRP}}\mathrm{dt dt} +  \dots
\end{equation}
where $\ddot{\bm{r}}_{C/N}$ is deliberately double integrated to $\bm{r}_{C/N}$ to show that it can be removed from both sides and $\bm{r}_{B/C}$ can be evaluated using relative gravitation acceleration combined with absolute accelerations due to external forces:
\begin{equation}
\bm{r}_{B/C}= \int \int \ddot{\bm{r}}_{B/C, \mathrm{grav}} \mathrm{dt dt} +   \int \int \ddot{\bm{r}}_{B/N, \mathrm{thrust}} \mathrm{dt dt} +  \int \int \ddot{\bm{r}}_{B/N, \mathrm{SRP}}\mathrm{dt dt} +  \dots
\end{equation}
Once that is done, it is clear that the absolute position can be found by simply adding the position of the central body to the relative position just found:
\begin{equation}
	\bm{r}_{B/N} = \bm{r}_{B/C} + \bm{r}_{C/N}
\end{equation}
This is how absolute position and velocity are found in Basilisk when using a relative dynamics formulation: the relative dynamics are integrated and the position and velocity of the central body are added afterward. The position and velocity of the central body are not integrated by Basilisk, but found from Spice.

Dynamic state effectors connected to the spacecraft hub can use the relative gravitational acceleration in their calculation for much the same reason. Effector positions and velocities are always integrated relative to the spacecraft. In fact, the absolute position and velocity of an effector is rarely, if ever, calculated or used. This is explains why a hinged body experiencing a relative acceleration does not quickly fall behind the spacecraft which is known to be moving along a course experiencing absolute gravitational acceleration. Additionally, because the effector is "pulled along" with the spacecraft when the spacecraft position is modified by the central body position, the effector sees the effect of absolute gravitational acceleration as well.

For intricacies related to using absolute vs relative dynamics, see the user manual at the end of this document.

\subsubsection{Gravity models}

Gravity models are usually based on solutions of the Laplace equation ($\nabla^2 U(\mathbf{\bar r}) = 0$). It is very important to state that this equation only models a gravity potential outside a body. For computing a potential inside a body the Poisson equation is used instead.

The spherical harmonic potential is a solution of the Laplace equation using orthogonal spherical harmonics. It can be derived solving the Laplace equation in spherical coordinates, using the separation of variables technique and solving a Sturm-Liouville problem. In this work, the solution will be found using another technique, which essentially follows Vallado's book\cite{vallado2013}.

For each element of mass $d m_\text{Q}$ the potential can be written as
\begin{equation}
\D U(\mathbf{\bar r}) = G \frac{\D m_\text{Q}}{\rho_\text{Q}}
\end{equation}

where $\rho_\text{Q}$ is the distance between the element of mass and the position vector $\mathbf{\bar r}$ where the potential is computed. This position vector is usually given in a body-fixed frame. The relation between the position vector $\mathbf{\bar r}$, the position of the element of mass $\mathbf{\bar r_\text{Q}}$ and $\rho_\text{Q}$ can be given using the cosine theorem and the angle $\alpha$ between the two position vectors, as can be appreciated in Figure \ref{fig:spher_harm}.

\begin{figure}
	\centering
	\includegraphics[width=0.3\textwidth]{Figures/spherical_harmonics.png}
	\caption{Geometry of the Spherical Harmonics Representation.}\label{fig:spher_harm}
\end{figure}

\begin{equation}
\rho_\text{Q} = \sqrt{r^2 + r_\text{Q}^2 - 2 r r_\text{Q} \cos(\alpha)} = r \sqrt{1 - 2 \frac{r_\text{Q}}{r} \cos(\alpha) + \bigg(\frac{r_\text{Q}}{r}\bigg)^2} = r \sqrt{1 - 2 \gamma \cos(\alpha) + \gamma^2}
\end{equation}
where $\gamma = r_\text{Q}/r$.

The potential can be obtained by integrating $dU$ through the whole body.
\begin{equation}
U(\mathbf{\bar r}) = G \int_{body} \frac{\D m_\text{Q}}{r \sqrt{1 - 2 \gamma \cos(\alpha) + \gamma^2}}
\end{equation}

If the potential is computed outside the body, $\gamma$ will always be less than 1, and the inverse of the square root can be approximated using the Legendre polynomials $P_l[\beta]$\cite{vallado2013}. Even though this derivation does not use the Laplace equation, it still assumes that the potential is computed outside the body.

The Legendre polynomials can be written as
\begin{equation}
P_l[\beta] = \frac{1}{2^l l!} \frac{d^l}{d \beta^l} (\beta^2 - 1)^l
\end{equation}

The potential is
\begin{equation}
U(\mathbf{\bar r}) = \frac{G}{r} \int_{body} \sum_{l=0}^\infty \gamma^l P_l[\cos(\alpha)] \D m_\text{Q}
\end{equation}

The angle $\alpha$ must be integrated. However, the cosine of the angle $\alpha$ can be decomposed using the geocentric latitude and the longitude associated to vectors $\mathbf{\bar r}$ and $\mathbf{\bar r_\text{Q}}$. These angles will be called $(\phi, \lambda)$ and $(\phi_\text{Q}, \lambda_\text{Q})$ respectively. Using the addition theorem it is possible to write\cite{vallado2013}.
\begin{equation}
P_l[cos(\alpha)] = P_l[\sin(\phi_\text{Q})] P_l[\sin(\phi)] + 2 \sum_{m=1}^l \frac{(l-m)!}{(l+m)!} (a_{l,m} a'_{l,m} + b_{l,m} b'_{l,m})
\end{equation}

where
\begin{align}
	a_{l,m} &= P_{l,m}[\sin(\phi_\text{Q})] \cos(m \lambda_\text{Q})\\
	b_{l,m} &= P_{l,m}[\sin(\phi_\text{Q})] \sin(m \lambda_\text{Q})\\
	a'_{l,m} &= P_{l,m}[\sin(\phi)] \cos(m \lambda)\\
	b'_{l,m} &= P_{l,m}[\sin(\phi)] \sin(m \lambda)
\end{align}

where $P_{l,m}[x]$ are the associated Legendre functions. "$l$" is called degree and "$m$", order. The polynomials can be computed as
\begin{equation}
P_{l,m}[\beta] = (1 - \beta^2)^\frac{m}{2} \frac{d^m}{d \beta^m} P_l[\beta]\label{eq:legendre}
\end{equation}

As can be seen, $a_{l,m}$ and $b_{l,m}$ must be integrated, but $a'_{l,m}$ and $a'_{l,m}$ can be taken outside the integral.
Therefore, it is possible to define
\begin{align}
	C'_{l,m} &= \int_{body} (2 -\delta_m) r_\text{Q}^l \frac{(l-m)!}{(l+m)!} a_{l,m} \D m_\text{Q}\\
	S'_{l,m} &= \int_{body} (2 -\delta_m) r_\text{Q}^l \frac{(l-m)!}{(l+m)!} b_{l,m} \D m_\text{Q}
\end{align}
where $\delta_m$ is the Kronecker delta.

Then
\begin{equation}
U(\mathbf{\bar r}) = \frac{G}{r} \sum_{l=0}^\infty C'_{l,0} \frac{P_l[\sin(\phi)]}{r^l} + \frac{G}{r} \sum_{l=0}^\infty \sum_{m=1}^l \frac{P_{l,m}[\sin(\phi)]}{r^l} \big[C'_{l,m} \cos(m \lambda) + S'_{l,m} \sin(m \lambda)]
\end{equation}

Non-dimensional coefficients $C_{l,m}$ and $S_{l,m}$ are usually used
\begin{align}
	C'_{l,m} &= C_{l,m} R_{\text{ref}}^l m_\text{Q}\\
	S'_{l,m} &= _\text{CoM}S_{l,m} R_{\text{ref}}^l m_\text{Q}
\end{align}
where $m_\text{Q}$ is the total mass of the body and $R_{\text{ref}}$ is a reference radius. If the coefficients $C_{l,m}$ and $S_{l,m}$ are given, the reference radius must be specified. Usually, the reference is chosen as the maximum radius or the mean radius\cite{scheeres2012}.

The potential is then
\begin{equation}
U(\mathbf{\bar r}) = \frac{\mu}{r} \sum_{l=0}^\infty C_{l,0} \bigg(\frac{R_{\text{ref}}}{r}\bigg)^l P_l[\sin(\phi)] + \frac{\mu}{r} \sum_{l=0}^\infty \sum_{m=1}^l \bigg(\frac{R_{\text{ref}}}{r}\bigg)^l P_{l,m}[\sin(\phi)] \big[C_{l,m} \cos(m \lambda) + S_{l,m} \sin(m \lambda)\big]
\end{equation}

Since $P_l[x] = P_{l,0}[x]$ the potential can be written in a more compact way
\begin{equation}
U(\mathbf{\bar r}) = \frac{\mu}{r} \sum_{l=0}^\infty \sum_{m=0}^l \bigg(\frac{R_{\text{ref}}}{r}\bigg)^l P_{l,m}[\sin(\phi)] \big[C_{l,m} \cos(m \lambda) + S_{l,m} \sin(m \lambda)\big]
\end{equation}

Some coefficients have a very interesting interpretation.
\begin{align}
	C_{0,0} &= 1\\
	S_{l,0} &= 0 \quad \forall l \geq 0\\
	C_{1,0} &= \frac{Z_{\text{CoM}}}{R_{\text{ref}}}\\
	C_{1,1} &= \frac{X_{\text{CoM}}}{R_{\text{ref}}}\\
	S_{1,1} &= \frac{Y_{\text{CoM}}}{R_{\text{ref}}}
\end{align}

where $[X_\text{CoM}, Y_\text{CoM}, Z_\text{CoM}]$ represents the center of mass of the celestial body. Therefore, if the origin of the coordinate system coincides with the center of mass, all these coefficients are identically zero. Similarly, the second order coefficients are related to the second order moments (moments of inertia).

Finally, the coefficients and Legendre polynomials are usually normalized to avoid computational issues. The factor $N_{l,m}$ is called the normalization factor
\begin{equation}
N_{l,m} = \sqrt{\frac{(l-m)! (2 -\delta_m) (2 l +1)}{(l+m)!}}
\end{equation}

The normalized coefficients are
\begin{align}
	\bar C_{l,m} &= \frac{C_{l,m}}{N_{l,m}}\\
	\bar S_{l,m} &= \frac{S_{l,m}}{N_{l,m}}
\end{align}

The normalized associated Legendre functions are
\begin{equation}
\bar P_{l,m}[x] = P_{l,m}[x] N_{l,m}
\end{equation}

The potential may be written as
\begin{equation}
U(\mathbf{\bar r}) = \frac{\mu}{r} \sum_{l=0}^\infty \sum_{m=0}^l \bigg(\frac{R_{\text{ref}}}{r}\bigg)^l \bar P_{l,m}[\sin(\phi)] \big[\bar C_{l,m} \cos(m \lambda) + \bar S_{l,m} \sin(m \lambda)\big]
\end{equation}

\subsubsection{Pines' Representation of Spherical Harmonics Gravity}

There are many ways to algorithmically compute the potential and its first and secondary derivatives. One of such algorithms is the one proposed by Pines\cite{pines1973}.

The spherical harmonics representation as it was presented has a singularity at the poles for the gravity field. The Pines' formulation avoids this problem and is more numerically stable for high degree and high order terms.

Unfortunately, this formulation does not contain the normalization factor which is necessary if the coefficients are normalized. In a paper written by Lundberg and Schutz\cite{lundberg1988}, a normalized representation of the Pines' formulation is given, but it contains an approximation.

For this work, and in order to code the spherical harmonics formulation, a formulation similar to Pines' using the Lundberg-Schutz paper will be derived. However, no approximations will be used. Therefore, the algorithm will be developed here without using the exact formulations given in those papers. For the sake of brevity, not every single derivation will be carried out, but it is possible to get the results following the expressions obtained in this section.

In the Pines' formulation the radius and the director cosines are used as coordinates. The potential will be given as $U[r, s, t, u]$, where
\begin{align}
	r &= \sqrt{x^2+y^2+z^2}\\
	s &= \frac{x}{r}\\
	t &= \frac{y}{r}\\
	u &= \frac{z}{r}
\end{align}

For a function of these coordinates, the dependance will be given using square brackets (e.g. $f[r,s,t,u]$).

Since $u = \sin(\phi) = \cos(90^\circ - \phi)$, it is possible to write
\begin{equation}
P_{l,m}[\sin(\phi)] = P_{l,m}[u]
\end{equation}

The derived Legendre functions $A_{l,m}[u]$ are defined such that
\begin{equation}
P_{l,m}[u] = (1 - u^2)^\frac{m}{2} A_{l,m}[u]
\end{equation}

From the definition of $P_{l,m}$ (Equation \refeq{eq:legendre}), it is possible to write
\begin{equation}
A_{l,m}[u] = \frac{d^m}{d u^m} P_l[u] = \frac{1}{2^l l!} \frac{d^{l+m}}{d u^{l+m}} (u^2 - 1)^l\label{eq:der_leg}
\end{equation}

The term $(1 - u^2)^\frac{m}{2}$ can be written as $(1 - \sin^2(\phi))^\frac{m}{2} = |\cos(\phi)|^m = \cos^m(\phi)$.

If the complex number $\xi$ is defined such that ($j$ is the imaginary unit)
\begin{equation}
\xi = \cos(\phi) \cos(\lambda) + j \cos(\phi) \sin(\lambda) = \frac{x}{r} + j \frac{y}{r} = s + j t
\end{equation}

it is possible to write
\begin{equation}
\xi^m = \cos^m(\phi) e^{j m \lambda} = (s + j t)^m
\end{equation}

The following sequences may be defined
\begin{align}
	R_m[s,t] &= Re\{\xi^m\}\\
	I_m[s,t] &= Im\{\xi^m\}
\end{align}

Putting all together, it is possible to write
\begin{equation}
U(\mathbf{\bar r}) = \frac{\mu}{r} \sum_{l=0}^\infty \sum_{m=0}^l \bigg(\frac{R_{\text{ref}}}{r}\bigg)^l A_{l,m}[u] \{C_{l,m} R_m[s,t] + S_{l,m} I_m[s,t]\}
\end{equation}

In order to normalize the coefficients ($\bar C_{l,m}$ and $\bar S_{l,m}$) and the derived Legendre functions ($\bar A_{l,m} = N_{l,m} A_{l,m}$), each term is divided an multiplied by the normalization factor $N_{l,m}$. Then
\begin{equation}
U(\mathbf{\bar r}) = \frac{\mu}{r} \sum_{l=0}^\infty \sum_{m=0}^l \bigg(\frac{R_{\text{ref}}}{r}\bigg)^l \bar A_{l,m}[u] \{\bar C_{l,m} R_m[s,t] + \bar S_{l,m} I_m[s,t]\}
\end{equation}

The sets $D_{l,m}[s,t]$, $E_{l,m}[s,t]$, and $F_{l,m}[s,t]$, are defined as
\begin{align}
	D_{l,m}[s,t] &= \bar C_{l,m} R_m[s,t] + \bar S_{l,m} I_m[s,t]\\
	E_{l,m}[s,t] &= \bar C_{l,m} R_{m-1}[s,t] + \bar S_{l,m} I_{m-1}[s,t]\\
	F_{l,m}[s,t] &= \bar S_{l,m} R_{m-1}[s,t] - \bar C_{l,m} I_{m-1}[s,t]
\end{align}

The value $\rho_l[r]$ is also defined as
\begin{equation}
\rho_l[r] = \frac{\mu}{r} \bigg(\frac{R_{\text{ref}}}{r}\bigg)^l
\end{equation}

The gravity potential may be finally computed as
\begin{equation}
U(\mathbf{\bar r}) = \sum_{l=0}^\infty \sum_{m=0}^l \rho_l[r] \bar A_{l,m}[u] D_{l,m}[s,t]
\end{equation}

This is the final expression that will be used to compute the gravity potential.

\subsubsection{Recursion Formulas}

Several recursion formulas are needed in order to algorithmically implement the Pines' formulation. They will be given without proof, but they are easily derived using the definitions above.
\begin{itemize}

\item{Recursion formula for $\rho_l[r]$}

Initial condition: $\rho_0[r] = \frac{\mu}{r}$
\begin{equation}
\rho_l[r] = \rho \cdot \rho_{l-1}[r]
\end{equation}
where $\rho = R_{\text{ref}}/r$.

\item{Recursion formula for $R_m[s,t]$}

Initial condition: $R_0[s,t] = 1$
\begin{equation}
R_m[s,t] = s R_{m-1}[s,t] - t I_{m-1}[s,t]
\end{equation}

\item{Recursion formula for $I_m[s,t]$}

Initial condition: $I_0[s,t] = 0$
\begin{equation}
I_m[s,t] = s I_{m-1}[s,t] + t R_{m-1}[s,t]
\end{equation}

\item{Recursion formula for $\bar A_{l,m}[u]$}

From Equation \eqref{eq:der_leg}, it is possible to see that
\begin{align}
	A_{l,l}[u] &= (2 l -1) A_{l-1,l-1}[u]\label{eq:All}\\
	A_{l,l-1}[u] &= u A_{l,l}[u]\label{eq:All_1}
\end{align}
\end{itemize}
There are several recursion formulas for computing Legendre polynomials $A_{l,m}[u]$, for $m < l-1$. The following formula, which is stable for high degrees\cite{lundberg1988}, will be used:
\begin{equation}
A_{l,m}[u] = \frac{1}{l-m} ((2 l -1) u A_{l-1,m}[u] - (l+m-1) A_{l-2,m}[u])\label{eq:Alm}
\end{equation}

Using Equations \eqref{eq:All}, \eqref{eq:All_1}, and \eqref{eq:Alm}, and the definition $\bar A_{l,m}[u] = N_{l,m} A_{l,m}[u]$, the following recursion formulas can be derived.

Initial condition: $\bar A_{0,0}[u] = 1$

The diagonal terms are computed as
\begin{equation}
\bar A_{l,l}[u] = \sqrt{\frac{(2 l - 1) (2 - \delta_l)}{(2 l) (2 - \delta_{l-1})}} \bar A_{l-1,l-1}[u]
\end{equation}

The low diagonal terms are then calculated as
\begin{equation}
\bar A_{l,l-1}[u] = u \sqrt{\frac{(2 l) (2 - \delta_{l-1})}{2 - \delta_l}} \bar A_{l,l}[u]
\end{equation}

Finally, for $l \geq (m+2)$, $N1_{l,m}$ and $N2_{l,m}$ are defined such that
\begin{align}
	N1_{l,m} &= \sqrt{\frac{(2 l + 1) (2 l - 1)}{(l - m) (l + m)}}\\
	N2_{l,m} &= \sqrt{\frac{(l + m - 1) (2 l + 1) (l - m -1)}{(l - m) (l + m) (2 l - 3)}}
\end{align}

and $\bar A_{l,m}[u]$ computed using

\begin{equation}
\bar A_{l,m}[u] = u N1_{l,m} \bar A_{l-1,m}[u] - N2_{l,m} \bar A_{l-2,m}[u]
\end{equation}

\subsubsection{Derivatives}

The first order derivatives of many of the values given are necessary to compute the gravity field (second order derivatives are needed if the Hessian is to be computed).

It is easy to show that
\begin{align}
	\frac{\partial D_{l,m}}{\partial s}[s,t] &= m E_{l,m}[s,t]\\
	\frac{\partial D_{l,m}}{\partial t}[s,t] &= m F_{l,m}[s,t]
\end{align}

\begin{equation}
\frac{d \rho_l}{d r}[r] = -\frac{(l+1)}{R_{\text{ref}}} \rho_{l+1}[r]
\end{equation}

\begin{align}
	\frac{\partial R_m}{\partial s}[s,t] &= m R_{m-1}[s,t]\\
	\frac{\partial R_m}{\partial t}[s,t] &= -m I_{m-1}[s,t]\\
	\frac{\partial I_m}{\partial s}[s,t] &= m I_{m-1}[s,t]\\
	\frac{\partial I_m}{\partial t}[s,t] &= m R_{m-1}[s,t]
\end{align}

\begin{equation}
\frac{d \bar A_{l,m}}{d u}[u] = \frac{N_{l,m}}{N_{l,m+1}} \bar A_{l,m+1}[u]
\end{equation}

The gravity field can be computed using all the equations given. However, the gradient of the potential is needed. As a change of variables was realized, the chain rule must be applied. In order to avoid filling up pages with math derivations, the results will be given. With patience, the following results can be obtained applying the chain rule and using all the derivatives given.

The gravity field can be computed as
\begin{equation}
\mathbf{\bar g} = (a_1[r,s,t,u] + s \cdot a_4[r,s,t,u]) \mathbf{\hat i} + (a_2[r,s,t,u] + t \cdot a_4[r,s,t,u]) \mathbf{\hat j} + (a_3[r,s,t,u] + u \cdot a_4[r,s,t,u]) \mathbf{\hat k}
\end{equation}

where
\begin{align}
	a_1[r,s,t,u] &= \sum_{l=0}^\infty \sum_{m=0}^l \frac{\rho_{l+1}[r]}{R_{\text{ref}}} m \bar A_{l,m}[u] E_{l,m}[s,t]\\
	a_2[r,s,t,u] &= \sum_{l=0}^\infty \sum_{m=0}^l \frac{\rho_{l+1}[r]}{R_{\text{ref}}} m \bar A_{l,m}[u] F_{l,m}[s,t]\\
	a_3[r,s,t,u] &= \sum_{l=0}^\infty \sum_{m=0}^l \frac{\rho_{l+1}[r]}{R_{\text{ref}}} m \frac{N_{l,m}}{N_{l,m+1}} \bar A_{l,m+1}[u] D_{l,m}[s,t]\\
	a_4[r,s,t,u] &= \sum_{l=0}^\infty \sum_{m=0}^l \frac{\rho_{l+1}[r]}{R_{\text{ref}}} m \frac{N_{l,m}}{N_{l+1,m+1}} \bar A_{l+1,m+1}[u] D_{l,m}[s,t]
\end{align}

In order to avoid computing factorials, it is easy to see that
\begin{align}
	\frac{N_{l,m}}{N_{l,m+1}} &= \sqrt{\frac{(l-m) (2-\delta_m)(l+m+1)}{2- \delta_{m+1}}}\\
	\frac{N_{l,m}}{N_{l+1,m+1}} &= \sqrt{\frac{(l+m+2)(l+m+1)(2l+1)(2-\delta_m)}{(2l+3)(2-\delta_{m+1})}}
\end{align}

Using all these expressions, the potential and the gravity field can be computed.

\subsubsection{Simple Gravity}
"Simple Gravity", or gravitational potential and acceleration without taking spherical harmonics into account, is equivalent to using only the $0\sup{th}$ term of the spherical harmonics equations. This is the equation that is used in basics physics courses and is most often used in Basilisk simulations. It assumes the gravitational body to be a point mass:

\begin{equation}
U(\mathbf{\bar r}) = G \frac{ m_\text{Q}}{\rho_\text{Q}}
\end{equation}


\documentclass{article}
\usepackage[letterpaper, margin=1in]{geometry}
\usepackage[version=4]{mhchem}
\usepackage{amsmath}
\usepackage{systeme,mathtools}
\usepackage{url}
\setcounter{MaxMatrixCols}{20}

\let\oldquote\quote
\let\endoldquote\endquote
\renewenvironment{quote}[2][]
{\if\relax\detokenize{#1}\relax
	\def\quoteauthor{#2}%
	\else
	\def\quoteauthor{#2~---~#1}%
	\fi
	\oldquote}
{\par\nobreak\smallskip\hfill(\quoteauthor)%
	\endoldquote\addvspace{\bigskipamount}}
\begin{document}
	\title{(Ab)Using Linear Algebra to Balance Chemical Reaction Equations \\\& \\  A Love Letter to Mathematics}
	\author{Liang Wang}
	\date{\today}
	\maketitle

	\section{Mechanical Processes Should be Mechanical}
	Balancing chemical reaction equations is often taught as an imprecise process that involves lots of idiosyncratic tricks. While trying to balance them by hand is usually doable when the reaction is relatively simple, the process becomes less straightforward when more complex reactions are presented.

	However, balancing should be a fairly boring and mechanical process, and \emph{mechanical processes should be mechanical}. Using linear algebra, we could balance any arbitrary reactions without the need for tricks, instead, the process becomes simple and repeatable.

	\section{Abusing Linear Algebra to do Simple Math}
	The goal of balancing chemical equations is to find the appropriate coefficients such that the number of each molecule is the same for the reactants and the products. The word `balancing' should remind you of solving equations, where equality is preserved as long as the operation is done to both sides of equations.

	Indeed, balancing chemical equations is just solving systems of linear equations. Solving a system of linear equations by substitution is often tedious and error-prone. Fortunately, linear algebra provides us with simpler ways of solving such problems.

	\subsection{Homogeneous Systems}
	In linear algebra, a homogeneous system refers to a equation with the form $A\vec{x} = \vec{0}$, where $A$ is a $m \times n$ matrix and $\vec{0}$ is the zero vector. When $m = n$, i.e.\ the matrix $A$ is square, the system has infinite non-trivial solutions if  $\mathrm{det}(A) \neq 0$. When the matrix is rank-deficient, there are infinite non-trivial solutions.

	\section{Simple Example}
	The following\footnote{Taken from \url{http://myweb.astate.edu/mdraganj/BalanceEqn.html}} is an example of an unbalanced chemical equation that might require so time to solve.
	\begin{center}
		\ce{S + HNO3 -> H2SO4 + NO2 + H2O}
	\end{center}
	The complexity lies in that the number of reactants does not equate to the number of products. We shall see how linear algebra simplifies the process.

	We assign each $x_i$ to each compound that appears in the equation from left to right. Hence we have the following system of equations.
	\begin{equation*}
	\ce{$x_1$S + $x_2$HNO3 -> $x_3$H2SO4 + $x_4$NO2 + $x_5$H2O} \implies
	\left\{
	\quad
	\begin{aligned}
	x_1  &= x_3 &&\text{Sulphur}\\
	x_2  &= 2x_3 + 2x_5 &&\text{Hydrogen}\\
	x_2  &= x_4 &&\text{Nitrogen}\\
	3x_2 &= 4x_3 + 2x_4 + x_5 &&\text{Oxygen} \\
	\end{aligned}
	\right.
	\end{equation*}
	Which is equivalent to the following matrix.
	\begin{equation*}
	\begin{bmatrix}
	1 &  0 & -1 &  0 &  0 \\
	0 &  1 & -2 &  0 & -2 \\
	0 &  1 &  0 & -1 &  0 \\
	0 &  3 & -4 & -2 & -1 \\

	\end{bmatrix}
	\end{equation*}
	Finding the solution set entails finding the null space basis vectors. After row operations, we get the row reduced echelon form to be.

	\begin{equation*}
	\begin{bmatrix}
	1 &	0	&0&	0	&-0.5 \\
	0 &	1	&0&	0	&-3 \\
	0 &	0	&1&	0	&-0.5 \\
	0&	0	&0&	1	&-3 \\

	\end{bmatrix}
	\end{equation*}
	Meaning that there is only one free variable, $x_5$.
	\begin{equation*}
	\begin{split}
	\begin{bmatrix}
	x_1 \\
	x_2 \\
	x_3 \\
	x_4 \\
	x_5 \\
	\end{bmatrix} =
	\begin{bmatrix}
	0.5x_5 \\
	3x_5 \\
	0.5x_3 \\
	3x_5 \\
	x_5 \\
	\end{bmatrix} =
	x_5
	\begin{bmatrix}
	0.5 \\
	3 \\
	0.5 \\
	3 \\
	1\\
	\end{bmatrix}
	\end{split}
	\end{equation*}
	Since we want integer solutions, and $0.5x_5$ is a entry for our basis vector, we pick $x_5 = 2$ to get the smallest possible positive solution.
	$$ \mathrm{solution} =
	\begin{bmatrix}
	1 \\
	6 \\
	1 \\
	6 \\
	2 \\
	\end{bmatrix}
	$$
	Meaning, \ce{S + 6HNO3 -> H2SO4 + 6NO2 + 2H2O}. We see this is indeed the desired solution.

	\section{Complex Example}
	One can say the previous example is still relatively easy and can be attempted without the use of matrices. The section provides a complex example\footnote{Taken from \url{https://www.chembuddy.com/?left=balancing-stoichiometry-questions&right=balancing-questions}} where doing it by hand is borderline torture.
	\begin{center}
		\ce{K4[Fe(SCN)6] + K2Cr2O7 + H2SO4 -> Fe2(SO4)3 + Cr2(SO4)3 + CO2 + H2O + K2SO4 + KNO3}
	\end{center}
	As usual, we assign $x_i$ to be the coefficient of each compounds from left to right. We obtain the following system of equations.
	\begin{equation*}
	\left\{
	\quad
	\begin{aligned}
	4x_1 + 2x_2 - 2x_8  - x_9&= 0 &&\text{Potassium}\\
	x_1 - 2x_4          &= 0 &&\text{Iron}\\
	6x_1 + x_3- 3x_4 - 3x_5 - x_8  &= 0 &&\text{Sulphur}\\
	6x_1 - x_6          &= 0 &&\text{Carbon} \\
	6x_1 - x_9          &= 0 &&\text{Nitrogen}\\
	2x_2 - 2x_5         &= 0 &&\text{Chromium}\\
	7x_2 + 4x_3 - 12x_4 - 12x_5 - 2x_6 -x_7 - 4x_8 - 3x_9 &= 0 &&\text{Oxygen} \\
	2x_3 - 2x_7 &= 0 && \text{Hydrogen} \\
	\end{aligned}
	\right.
	\end{equation*}
	Converting it to matrix form gives us the following.
	$$
	\begin{bmatrix}
	4   &       2  &        0    &      0   &       0   &       0   &       0 &        -2  &        -1 \\
	1   &       0  &        0    &     -2   &       0   &       0   &       0 &         0  &        0\\
	6   &       0  &        1    &     -3   &      -3   &       0   &       0 &        -1  &        0\\
	6   &       0  &        0    &      0   &       0   &      -1   &       0 &         0  &        0\\
	6   &       0  &        0    &      0   &       0   &       0   &       0 &         0  &       -1\\
	0   &       2  &        0    &      0   &      -2   &       0   &       0 &         0  &        0\\
	0   &       7  &        4    &    -12   &     -12   &      -2   &      -1 &        -4  &       -3\\
	0   &       0  &        2    &      0   &       0   &       0   &      -2 &         0  &        0\\
	\end{bmatrix}
	$$
	We simplify the matrix to its row reduced echelon form.
	$$
	\begin{bmatrix}
	1   &       0     &     0   &       0  &        0    &      0   &       0    &      0  &     -1/6\\
	0   &       1     &     0   &       0  &        0    &      0   &       0    &      0  &   -97/36\\
	0   &       0     &     1   &       0  &        0    &      0   &       0    &      0  &  -355/36\\
	0   &       0     &     0   &       1  &        0    &      0   &       0    &      0  &    -1/12\\
	0   &       0     &     0   &       0  &        1    &      0   &       0    &      0  &   -97/36\\
	0   &       0     &     0   &       0  &        0    &      1   &       0    &      0  &       -1\\
	0   &       0     &     0   &       0  &        0    &      0   &       1    &      0  &  -355/36\\
	0   &       0     &     0   &       0  &        0    &      0   &       0    &      1  &   -91/36\\
	\end{bmatrix}
	$$
	We write the null space basis vector in terms of our one free variable, $x_9$.
	\begin{equation*}
	\begin{bmatrix}
	x_1 \\
	x_2 \\
	x_3 \\
	x_4 \\
	x_5 \\
	x_6 \\
	x_7 \\
	x_8 \\
	x_9 \\
	\end{bmatrix}
	=
	x_9
	\begin{bmatrix}
	1/6 \\
	97/36 \\
	355/36 \\
	1/12\\
	97/36\\
	1\\
	355/36\\
	91/36\\
	1 \\
	\end{bmatrix}
	\end{equation*}
	We pick $x_9$ to the be the inverse of the least common factor, $\mathrm{lcm}\left(\dfrac{1}{6},\: \dfrac{97}{36},\: \dfrac{355}{36},\: \dfrac{1}{12},\: \dfrac{97}{36},\: 1,\: \dfrac{355}{36},\: \dfrac{91}{36},\: 1\right)^{-1} = 36$. We obtain the solution set of:
	\begin{equation*}
	\mathrm{solution} =
	\begin{bmatrix}
	6 \\
	97 \\
	355 \\
	3 \\
	97 \\
	36 \\
	355 \\
	91 \\
	36 \\
	\end{bmatrix}
	\end{equation*}
	Henceforth, the complex reaction is as follows:
	\begin{center}
		\ce{6 K4[Fe(SCN)6] + 97 K2Cr2O7 + 355 H2SO4 -> 3 Fe2(SO4)3 + 97 Cr2(SO4)3 + 36 CO2 + 355 H2O + 91 K2SO4 + 36 KNO3}
	\end{center}
	Feel free to verify the result using the \emph{magic of internet}.

	\section{A Love Letter to Mathematics}
	\begin{quote}{Henri Poincaré}
		The scientist does not study nature because it is useful to do so. He studies it because he takes pleasure in it, and he takes pleasure in it because it is beautiful. If nature were not beautiful it would not be worth knowing, and life would not be worth living
	\end{quote}

	\begin{quote}{Richard Dawkins}
		After sleeping through a hundred million centuries we have finally opened our eyes on a sumptuous planet, sparkling with colour, bountiful with life. Within decades we must close our eyes again. Isn’t it a noble, an enlightened way of spending our brief time in the sun, to work at understanding the universe and how we have come to wake up in it?
	\end{quote}

	The world is a wonderful place filled with wonders, it is the noblest pursuit to try to understand it. One may prefer traditional science in an attempt to directly understand the world, one may find philosophy intriguing in understanding humanity. As someone majoring in software, I have always found mathematics to be the beauty that unlocks the understanding of the world.

	Mathematics has the interesting duality of both being immensely useful while desperately trying to be abstract and devoid of the real world. G.H.\ Hardy, the famous English mathematician that worked with the great Srinivasa Ramanujan---famously depicted in the film \textit{The Man Who Knew Infinity} once said:
	\begin{quote}{G.H.\ Hardy}
		We have concluded that trivial mathematics is, on the whole, useful, and that the real mathematics, on the whole, is not.
	\end{quote}

	Most mathematics, such as Group Theory, Category Theory, and Topology try to be as abstract as possible. Yet they all have proved to be critical tools in understanding large software. The concept of a monoid seems completely out of touch with reality until you realize it's everywhere in programming languages.

	Embrace mathematics both for its abstract beauty and the applications, for which it is the language of both the abstract world and the world we live in.
\end{document}


\subsection{Neural Network}

Neural Network (NN) is a classifier that tries to replicate the structure of the human thought.

Being the purpose of the paper to compare the performance of SVM vs NN, our second approach was to implement a Neural Network with a Multi-layer Perceptron Classifier \cite{nn-mlp-classifier}.

Just as in our SVM flow, we initially divide our data in two subsets:

\begin{itemize}
\item Training subset:  \(80\%\)
\item Testing subset:  \(20\%\).
\end{itemize}

Similar to what was done in SVM, we used HOG as our feature extractor. Hence, the specific implementation we used has 3 layers: \(Input Layer \rightarrow Hidden Layer \rightarrow Output Layer\), with the follow specification:

\begin{itemize}
\item Input Layer: \(128\) neurons (the respective number of features extracted by HOG);
\item Hidden Layer: \(10\) neurons (as default value, the optimized number of neurons will be specified ahead);
\item Output Layer: \(1\) neuron.
\end{itemize}

Furthermore, we used \textit{RELU} as the activation function, the loss function we used was \textit{Log-Loss Function}, using the \textit{LBFGS} weight optimizer (we decide to use \textit{LBFGS}, because, according to Scikit-Learn documentation \cite{nn-mlp-classifier}, it can converge faster and perform better, given smaller datasets, which is our case), and has the default learning rate, we used \( \alpha = 0.001 \), which will be optimized further ahead. To summarize we can see the initial configuration of our Neural Network in table \ref{table:nn-initial-configuration}.

\begin{table}[htbp]
\centering
\caption{NN - Initial Configuration}
\begin{tabular}{ |c|c| }
 \hline
 \textbf{Train Data} & 80\% \\
 \hline
 \textbf{Test Data} & 20\% \\
 \hline
 \textbf{Input Layer} & \(128\) neurons \\
 \hline
 \textbf{Hidden Layer} & [10, 20, 30, 40, 50, 60, 70] neurons, \(default = 10\) \\
 \hline
 \textbf{Output Layer} & \(1\) neuron \\
 \hline
 \textbf{Activation Function} & \textit{RELU} \\
 \hline
 \textbf{Loss Function} & \textit{Log-Loss Function}, via \textit{LBFGS} weight optimizer \\
 \hline
 \textbf{Learning Rate (\(\alpha\))} & [0.001, 0.01, 0.05, 0.1, 0.5, 1, 10], \(default = 0.001\) \\
 \hline
 \textbf{Number of Iterations} & [100, 200, 300, ..., 900, 1000], \(default = 100\) \\
 \hline
\end{tabular}
\label{table:nn-initial-configuration}
\end{table}

As done in the previous section, the SVM aproach \ref{svm}, we optimized the hyper-parameters of the NN model to enable its best performance. Just like in SVM, we did this by applying K-Fold Cross Validation, with \(K = 5\). The hyper-parameters we decided to vary, and consequently optimize, were as follows:

\begin{itemize}
\item Hidden Layer size (the number of neurons in the Hidden Layer);
\item Learning rate (\(\alpha\));
\item Number of iterations.
\end{itemize}

After validating every combination, as we can see in figure \ref{fig:nn-cross-validation}, our model performed the best when:

\begin{itemize}
\item \( HiddenLayerSize = 60 \) neurons;
\item \( \alpha = 1 \);
\item \( NumberOfIterations = 700 \).
\end{itemize}

\begin{figure}[htbp]
\centerline{\includegraphics[width=1\linewidth]{images/nn_cross_validation.png}}
\caption{NN - Cross-Validation using multiple-value combinations}
\label{fig:nn-cross-validation}
\end{figure}

Based on the Cross Validation data, we created a Neural Network with a Multi-layer Perceptron Classifier, and a final configuration that can be seen in table \ref{table:nn-final-configuration}.

\begin{table}[htbp]
\centering
\caption{NN - Final Configuration}
\begin{tabular}{ |c|c| }
 \hline
 \textbf{Train Data} & 80\% \\
 \hline
 \textbf{Test Data} & 20\% \\
 \hline
 \textbf{Input Layer} & \(128\) neurons \\
 \hline
 \textbf{Hidden Layer} & \(60\) neurons \\
 \hline
 \textbf{Output Layer} & \(1\) neuron \\
 \hline
 \textbf{Activation Function} & \textit{RELU} \\
 \hline
 \textbf{Loss Function} & \textit{Log-Loss Function}, via \textit{LBFGS} \\
 \hline
 \textbf{Learning Rate (\(\alpha\))} & \(\alpha = 1\) \\
 \hline
 \textbf{Number of Iterations} & 700 \\
 \hline
\end{tabular}
\label{table:nn-final-configuration}
\end{table}

Finally, and as we did in our SVM approach, to ensure that the model didn't end up in a local minimum, and that our model was actually learning we run the all process 50 times and calculated the mean values to both train and test phases.