\documentclass{article}
\usepackage[dvipsnames]{xcolor} % for colorful text
\usepackage{tikz}             % for graphics
\usepackage{amsmath, amssymb} % for math symbols
\usepackage{hyperref}         % for creating links
\hypersetup{ colorlinks=true, linkcolor=blue}
\numberwithin{equation}{section}
\pagenumbering{roman}
\title{Thermal Physics}
\begin{document}

\maketitle
\tableofcontents
\pagebreak

\section{Boyle's Temperature}
At Boyle's temperature, $T_b$, $Z \to 1$\\
Using Van der Waals' equation,
$$P\left(1+\frac{a}{PV^2}\right) V\left(1-\frac{b}{V}\right) = RT$$
$$Z = \left(1+\frac{a}{PV^2}\right)^{-1} \left(1-\frac{b}{V}\right)^{-1}$$
$$Z = \left(1 - \frac{a}{PV^2}\right) \left(1+\frac{b}{V}\right)$$
$$Z = 1 + \frac{b}{V} - \frac{a}{PV^2} - \frac{ab}{PV^3}$$
$\because$ ab $\ll$ $V^3$,
at $T_B$, Z $\to$ 1,
$$\therefore \frac{b}{V} = \frac{a}{PV^2}$$
Using PV=RT,
$$b = \frac{a}{RT_b}$$
\begin{equation}
  \boxed{\textcolor{Mulberry}{T_b = \frac{a}{Rb}}} \label{eq:b1}
\end{equation}

\section{Critical Coefficients}
Using Van der waals' equation,
$$\left(P+\frac{a}{V^2}\right) \left(V-b\right) = RT$$
\begin{equation}
  P = \frac{RT}{\left(V-b\right)} - \frac{a}{V^2} \label{eq:c1}
\end{equation}
differentianting \eqref{eq:c1} w.r.t. V,
\begin{equation}
  \frac{dP}{dV} = - \frac{RT}{\left(V-b\right)^2} + \frac{2a}{V^3} \label{eq:c2}
\end{equation}
differentiating \eqref{eq:c2} w.r.t. V,
\begin{equation}
  \frac{d^2P}{dV^2} = \frac{2RT}{\left(V-b\right)^3} - \frac{6a}{V^4} \label{eq:c3}
\end{equation}
At the critical point,
$$\frac{dP}{dV} = 0  ,  \frac{d^2P}{dV^2} = 0$$
$\therefore$ \eqref{eq:c2} becomes,
\begin{equation}
  \frac{2a}{V_c^3} = \frac{RT_c}{\left(V_c-b\right)^2} \label{eq:c4}
\end{equation}
and \eqref{eq:c3} becomes,
\begin{equation}
  \frac{6a}{V_c^4} = \frac{2RT_c}{\left(V_c-b\right)^3} \label{eq:c5}
\end{equation}
Dividing \eqref{eq:c4} by \eqref{eq:c5},
$$\frac{V_c}{3} = \frac{\left(V_c-b\right)}{2}$$
$$2V_c = 3V_c - 3b$$
\subsection{Critical volume}
\begin{equation}
  \boxed{V_c = 3b} \label{eq:c6}
\end{equation}
Using \eqref{eq:c6} in \eqref{eq:c4},
$$\frac{2a}{27b^3} = \frac{RT_c}{4b^2}$$
\subsection{Critical temperature}
\begin{equation}
  \boxed{T_c = \frac{8a}{27Rb}} \label{eq:c7}
\end{equation}
Using \eqref{eq:c6} and \eqref{eq:c7} in \eqref{eq:c1},
$$P_c = \frac{4Ra}{27Rb^2} - \frac{a}{9b^2}$$
\subsection{Critical pressure}
\begin{equation}
  \boxed{P_c = \frac{a}{27b^2}} \label{eq:c8}
\end{equation}
\subsection{Critical compressibility factor}
$$Z = \frac{P_c V_c}{R T_c} = \frac{3}{8}$$
\subsection{Critical coefficient}
Critical coefficient is defined as the inverse of critical compressibility factor, i.e.,
$$K = \frac{1}{Z} = \frac{8}{3}$$

\section{Temperature of Inversion}
\begin{tikzpicture}
  % Outer Box
  \draw (-1,-2) rectangle (6.5,1.5);
  %Container 1
  \draw (1,0) node[align=center] {$P_1$,$V_1$,$T_1$};
  \draw[thick,->,blue] (0,-1) node[fill=white] {Container I} -- ((0.2,-0.2);
  \draw (0,-0.5) rectangle (2,0.5);
  %Porous Plug
  \filldraw[fill=brown] (1.9,-0.5) rectangle (2,0.5);
  \draw[thick,->] (1.5,-1.5) node[fill=white] {Porous Plug} -- (1.95,0);
  %Container 2
  \draw (3,0) node[align=center] {$P_2$,$V_2$,$T_2$};
  \draw[thick,->,blue] (5,-1) node[fill=white] {Container II} -- (3.5,-0.5);
  \draw (2,-1) rectangle (4,1);
\end{tikzpicture}
\\
When the temperature of incoming gas($T_1$) in Joule-Thompson effect is, then the temperature of the outcoming gas($T_2$) is,
\begin{itemize}
\item $T_2 > T_1$ when $T_1 > T_i$
\item $T_2 = T_1$ when $T_1 = T_i$
\item $T_2 < T_1$ when $T_1 < T_i$
\end{itemize}
Work done in overcoming intermolecular attractions,
\begin{align*}
  w &= \int_{V_1}^{V_2} P dV\\
  &= \int_{V_1}^{V_2} \frac{a}{V^2} dV\\
  &= - \frac{a}{V_2} + \frac{a}{V_1}
\end{align*}
Total work done by the gas,
$$W = (P_2 V_2 - P_1 V_1) + w$$
\begin{equation}
  W = (P_2 V_2 - P_1 V_1) + \left(\frac{a}{V_1} - \frac{a}{V_2}\right) \label{eq:i1}
\end{equation}
from Van der waals' equation, we know that
$$\left(P+\frac{a}{V^2}\right)(V-b) = RT$$
$$PV = RT + bP - \frac{a}{V} + \frac{ab}{V^2}$$
$$\because ab \ll V^2$$
\begin{equation}
  PV = RT + bP - \frac{a}{V} \label{eq:i2}
\end{equation}
Using \eqref{eq:i2} in \eqref{eq:i1},
$$W = RT + bP_2 - \frac{a}{V_2} - RT - bP_1 + \frac{a}{V_1} - \frac{a}{V_2} + \frac{a}{V_1}$$
\begin{equation}
  W = (P_2 - P_1)b + 2a \left(\frac{1}{V_1} - \frac{1}{V_2}\right) \label{eq:i3}
\end{equation}
Using $PV=RT$,
\begin{equation}
  V_1 = \frac{RT}{P_1} , V_2 = \frac{RT}{P_2} \label{eq:i4}
\end{equation}
Using \eqref{eq:i4} in \eqref{eq:i3},
$$W = (P_2 - P_1)b + \frac{2a}{RT}(P_1 - P_2)$$
$$W = (P_1 - P_2) \left[ \frac{2a}{RT} - b \right]$$
W = 0 when $T=T_i$\\
\[ \therefore \boxed{T_i = \frac{2a}{Rb}} \label{eq:i5} \tag{3.5} \]

\section{Relationship between $T_b$,$T_c$ and $T_i$}
Using \eqref{eq:b1}, \eqref{eq:c7} and \eqref{eq:i5},
$$T_c < T_b < T_i$$

\section{Maxwell's Relations}
From first law of thermodynamics,
\begin{equation}
  dU = TdS - PdV \label{eq:m1}
\end{equation}
we can write
$$T = \frac{\partial U}{\partial S} , P = -\frac{\partial U}{\partial T} $$
we can write U, S and V as functions of x and y where x and y are any of P, V, S or T,
\begin{equation}
  dU = \frac{\partial U}{\partial x}dx + \frac{\partial U}{\partial y}dy \label{eq:m2}
\end{equation}
\begin{equation}
  dS = \frac{\partial S}{\partial x}dx + \frac{\partial S}{\partial y}dy \label{eq:m3}
\end{equation}
\begin{equation}
  dV = \frac{\partial V}{\partial x}dx + \frac{\partial V}{\partial y}dy \label{eq:m4}
\end{equation}

using \eqref{eq:m2}, \eqref{eq:m3} and \eqref{eq:m4} in \eqref{eq:m1},
\begin{equation}
\frac{\partial U}{\partial x}dx + \frac{\partial U}{\partial y}dy = T\left[\frac{\partial S}{\partial x}dx + \frac{\partial S}{\partial y}dy \right] - P\left[\frac{\partial V}{\partial x}dx + \frac{\partial V}{\partial y}dy\right] \label{eq:m5}
\end{equation}

equating $dx$ terms of \eqref{eq:m5},
\begin{equation}
  \frac{\partial U}{\partial x} = T\frac{\partial S}{\partial x} - P\frac{\partial V}{\partial x} \label{eq:m6}
\end{equation}
differentiating \eqref{eq:m6} w.r.t. $y$,
\begin{equation}
  \frac{\partial^2 U}{\partial y \partial x} = \left(\frac{\partial T}{\partial y}\right)_x \left(\frac{\partial S}{\partial x}\right)_y + T\frac{\partial^2 S}{\partial y \partial x} - \left(\frac{\partial P}{\partial y}\right)_x \left(\frac{\partial V}{\partial x}\right)_y - P\frac{\partial^2 V}{\partial y \partial x} \label{eq:m7}
\end{equation}

equating $dy$ terms of \eqref{eq:m5},
\begin{equation}
  \frac{\partial U}{\partial y} = T\frac{\partial S}{\partial y} - P\frac{\partial V}{\partial y} \label{eq:m8}
\end{equation}
differentiating \eqref{eq:m8} w.r.t. $x$,
\begin{equation}
  \frac{\partial^2 U}{\partial x \partial y} = \left(\frac{\partial T}{\partial x}\right)_y \left(\frac{\partial S}{\partial y}\right)_x + T\frac{\partial^2 S}{\partial x \partial y} - \left(\frac{\partial P}{\partial x}\right)_y \left(\frac{\partial V}{\partial y}\right)_x - P\frac{\partial^2 V}{\partial x \partial y} \label{eq:m9}
\end{equation}

for a continuous function, F we have,
$$\frac{\partial^2 F}{\partial x \partial y}=\frac{\partial^2 F}{\partial y \partial x}$$
$\therefore$ from \eqref{eq:m7} and \eqref{eq:m9},
\begin{equation}
  \left(\frac{\partial T}{\partial y}\right)_x \left(\frac{\partial S}{\partial x}\right)_y - \left(\frac{\partial P}{\partial y}\right)_x \left(\frac{\partial V}{\partial x}\right)_y = \left(\frac{\partial T}{\partial x}\right)_y \left(\frac{\partial S}{\partial y}\right)_x - \left(\frac{\partial P}{\partial x}\right)_y \left(\frac{\partial V}{\partial y}\right)_x \label{eq:m10}
\end{equation}

\subsection{Equation I}
Using $x=P$ and $y=V$ in \eqref{eq:m10},
$$ \left(\frac{\partial T}{\partial V}\right)_P \left(\frac{\partial S}{\partial P}\right)_V - \left(\frac{\partial P}{\partial V}\right)_P \left(\frac{\partial V}{\partial P}\right)_V = \left(\frac{\partial T}{\partial P}\right)_V \left(\frac{\partial S}{\partial V}\right)_P - \left(\frac{\partial P}{\partial P}\right)_V \left(\frac{\partial V}{\partial V}\right)_P $$
$$\boxed{\left(\frac{\partial T}{\partial P}\right)_V \left(\frac{\partial S}{\partial V}\right)_P - \left(\frac{\partial T}{\partial V}\right)_P \left(\frac{\partial S}{\partial P}\right)_V = 1}$$

\subsection{Equation II}
Using $x=P$ and $y=S$ in \eqref{eq:m10},
$$ \left(\frac{\partial T}{\partial S}\right)_P \left(\frac{\partial S}{\partial P}\right)_S - \left(\frac{\partial P}{\partial S}\right)_P \left(\frac{\partial V}{\partial P}\right)_S = \left(\frac{\partial T}{\partial P}\right)_S \left(\frac{\partial S}{\partial S}\right)_P - \left(\frac{\partial P}{\partial P}\right)_S \left(\frac{\partial V}{\partial S}\right)_P$$
$$\boxed{\left(\frac{\partial T}{\partial P}\right)_S = \left(\frac{\partial V}{\partial S}\right)_P}$$

\subsection{Equation III}
Using $x=P$ and $y=T$ in \eqref{eq:m10},
$$\left(\frac{\partial T}{\partial T}\right)_P \left(\frac{\partial S}{\partial P}\right)_T - \left(\frac{\partial P}{\partial T}\right)_P \left(\frac{\partial V}{\partial P}\right)_T = \left(\frac{\partial T}{\partial P}\right)_T \left(\frac{\partial S}{\partial T}\right)_P - \left(\frac{\partial P}{\partial P}\right)_T \left(\frac{\partial V}{\partial T}\right)_P$$
$$\boxed{\left(\frac{\partial S}{\partial P}\right)_T = - \left(\frac{\partial V}{\partial T}\right)_P}$$

\subsection{Equation IV}
Using $x=V$ and $y=S$ in \eqref{eq:m10},
$$\left(\frac{\partial T}{\partial S}\right)_V \left(\frac{\partial S}{\partial V}\right)_S - \left(\frac{\partial P}{\partial S}\right)_V \left(\frac{\partial V}{\partial V}\right)_S = \left(\frac{\partial T}{\partial V}\right)_S \left(\frac{\partial S}{\partial S}\right)_V - \left(\frac{\partial P}{\partial V}\right)_S \left(\frac{\partial V}{\partial S}\right)_V$$
$$\boxed{\left(\frac{\partial P}{\partial S}\right)_V = - \left(\frac{\partial T}{\partial V}\right)_S}$$

\subsection{Equation V}
Using $x=V$ and $y=T$ in \eqref{eq:m10},
$$\left(\frac{\partial T}{\partial T}\right)_V \left(\frac{\partial S}{\partial V}\right)_T - \left(\frac{\partial P}{\partial T}\right)_V \left(\frac{\partial V}{\partial V}\right)_T = \left(\frac{\partial T}{\partial V}\right)_T \left(\frac{\partial S}{\partial T}\right)_V - \left(\frac{\partial P}{\partial V}\right)_T \left(\frac{\partial V}{\partial T}\right)_V$$
$$\boxed{\left(\frac{\partial S}{\partial V}\right)_T = \left(\frac{\partial P}{\partial T}\right)_V}$$

\subsection{Equation VI}
Using $x=S$ and $y=T$ in \eqref{eq:m10},
$$\left(\frac{\partial T}{\partial T}\right)_S \left(\frac{\partial S}{\partial S}\right)_T - \left(\frac{\partial P}{\partial T}\right)_S \left(\frac{\partial V}{\partial S}\right)_T = \left(\frac{\partial T}{\partial S}\right)_T \left(\frac{\partial S}{\partial T}\right)_S - \left(\frac{\partial P}{\partial S}\right)_T \left(\frac{\partial V}{\partial T}\right)_S$$
$$\boxed{\left(\frac{\partial P}{\partial T}\right)_S \left(\frac{\partial V}{\partial S}\right)_T - \left(\frac{\partial P}{\partial S}\right)_T \left(\frac{\partial V}{\partial T}\right)_S = 1}$$

\section{Statistical Mechanics}
\subsection{Fundamental Postulates of Statistical Mechanics}
\begin{itemize}
\item Any gas under consideration may be considered to be composed of large number of molecules which are constantly in motion and behave like very small elastic spheres.
\item All the cells in phase space are of equal size.
\item All accessible microstates corresponding to possible macrostates are equally probable.
\item The equilibrium state of a gas corresponds to the macrostate of maximum probability.
\item The total number of molecules is constant.
\item The total energy of the system is constant.
\end{itemize}

\subsection{Types of particles}
\subsubsection{Maxwell-Boltzmann Statistics}
Used to describe identical, distinguishable particles.
\subsubsection{Fermi-Dirac Statistics}
Used to describe identical, non-distinguishable particles with half-integral spins, i.e., $\frac{1}{2}$,$\frac{3}{2}$,etc...\\
Examples include electron, positron.
\subsubsection{Bose-Einstein Statistics}
Used to describe identical, non-distinguishable particles with integral spins, i.e., 0,1,2,etc...\\
Examples include photon, W boson.

\section{Maxwell-Boltzmann Energy Distribution Law}
Suppose the particles are distributed among $k$ energy levels $E_1,E_2,\dots,E_i,\dots,E_k$. Let $n_1,n_2,\dots,n_i,\dots,n_k$ be the number of independent quantum states associated with the energy levels. According to the basic statistical postulates
\begin{equation}
  N = n_1 + n_2 + \dots + n_i + \dots + n_k = \sum_i n_i \label{eq:mb1}
\end{equation}
and since the force of interation between particles are negligible, we have
\begin{equation}
  U = n_1 E_1 + n_2 E_2 + \dots + n_i E_i + \dots + n_k E_k \label{eq:mb2}
\end{equation}
Taking differential of equation \eqref{eq:mb1} and \eqref{eq:mb2}, we have
\begin{equation}
  dN = \sum_i dn_i = 0 \label{eq:mb3}
\end{equation}
and
\begin{equation}
  dU = \sum_i E_i dn_i = 0 \label{eq:mb4}
\end{equation}
\subsection{Maxwell-Boltzmann Statistical Count}
The number of ways of choosing $n_1$ particles for the first energy level($E_1$) is, thus, given by
$$x_1 = \frac{N!}{n_1!(N-n_1)!} = {}^NC_{n_1}$$
For the second state $E_2$, only $(N-n_1)$ particles are available and $n_2$ particles are in state $E_2$. The number of independent ways of choosing $n_2$ particles for the second energy level is
$$x_2 = \frac{(N-n_1)!}{n_2! (N-n_1-n_2)!} = {}^{N-n_1}C_{n_2}$$
Similarly for all energy levels. For the $k^{th}$ energy level we have
$$x_k = \frac{(N-n_1-n_2-\dots n_{k-1})!}{n_k! (N-n_1-n_2-\dots n_k)!} = {}^{(N-n_1-n_2-\dots n_{k-1})} C_{n_k}$$
Since these numbers are independent, the number of distinct ways of obtaining the distribution of particles among energy levels is given by
$$x = x_1 x_2 x_3 \dots x_i \dots x_k$$
\begin{equation}
  x = \frac{N!}{n_1! n_2! \dots n_k!} \label{eq:mb5}
\end{equation}
Now our next job is to find the number of independent ways in which the particles in the various energy level can be distributed among the quantum states in the respective levels. For example, in the energy level, containing $n_i$ particles, there are $g_i$ quantum states. Each of the $n_i$ particles can be in any one of the quantum states. Hence there are $g_i^{n_i}$ ways of arranging $n$ particles among $g_i$ quantum states available in the $i^{th}$ energy level.
$$W = \frac{N!}{n_1! n_2! \dots n_k!} \times g_1^{n_1} \times g_2^{n_2} \times \dots g_k^{n_k}$$
\begin{equation}
  W = N! \prod_{i=1}^{k} \left[\frac{g_i^{n_i}}{n_i!}\right] \label{eq:mb6}
\end{equation}
\subsection{Condition for most probable distribution}
The most probable distribution of the particles among the energy states in equilibrium is that for which the probability of occurence is maximum, i.e. for which \textbf{W is maximum}. For mathematical convenience, we consider the condition for maximum value of $\log W$. The condition for maximum value of $\log W$ is
\begin{equation}
  d(\log W) = 0 \label{eq:mb7}
\end{equation}
where W is a function of $n_1,n_2,\dots,n_k$.\\
Differentiating equation \eqref{eq:mb6}, we get
\begin{align*}
  d(\log W) &= \frac{\partial \log W}{\partial n_1}dn_1 + \frac{\partial \log W}{\partial n_2}dn_2 + \dots + \frac{\partial \log W}{\partial n_k}dn_k\\
  &= \sum_{i=1}^{k} \frac{\partial \log W}{\partial n_i}dn_i
\end{align*}
Applying the condition from equation \eqref{eq:mb7}, we get
\begin{equation}
  \sum_{i=1}^{k} \frac{\partial \log W}{\partial n_i}dn_i = 0 \label{eq:mb8}
\end{equation}
The solution of this equation is subject to the condition laid down by \eqref{eq:mb3} and \eqref{eq:mb4}. These conditions are introduced into equation \eqref{eq:mb8} by using the method of Lagrange's undetermined multipliers.\\
Let $\alpha$ and $\beta$ be these multipliers independent of $n_i$'s. We multiply \eqref{eq:mb3} by $-\alpha$ and \eqref{eq:mb4} by $-\beta$ and adding these equations to \eqref{eq:mb8}, we have
\begin{equation}
  \sum_{i=1}^{k}\left(\frac{\partial \log W}{\partial n_i} - \alpha - \beta E_i\right)dn_i \label{eq:mb9}
\end{equation}
\subsection{M-B Energy Distribution Law in General Form}
Taking logarithms of \eqref{eq:mb6}, we have
\begin{align*}
  \log W &= \log N! + \log \left[\prod_i \left(\frac{g_i^{n_i}}{n_i!}\right)\right]\\
  &= \log N! + \sum_{i=1}^{k} \left(n_i \log g_i - \log n_i!\right)
\end{align*}
Using sterlings formula, $\log x! = x(\log x - 1)$
\begin{equation}
  \log W = N \log N - N + \sum_{i=1}^{k}(n_i \log g_i - n_i \log n_i + n_i) \label{eq:mb10}
\end{equation}
Differentiating \eqref{eq:mb10} w.r.t. $n_i$,
$$\frac{d(\log W)}{dn_i} = \sum\left[\log g_i - \log n_i\right]\delta n_i$$
\begin{equation}
  \frac{d(\log W)}{dn_i} = \sum\left[-\log\left(\frac{n_i}{g_i}\right)\right]\label{eq:mb11}
\end{equation}
Substituting \eqref{eq:mb11} in \eqref{eq:mb9},
$$\sum\left[-\log\left(\frac{n_i}{g_i}\right) - \alpha - \beta E_i\right]\delta n_i = 0$$
$$\log\left(\frac{n_i}{g_i}\right) = -\alpha -\beta E_i$$
$$n_i = g_i e^{-\alpha-\beta E_i}$$
\begin{equation}
  n_i = g_i e^{-\alpha} e^{-\beta E_i} \label{eq:mb12}
\end{equation}
where $i=1,2,3,\dots k$\\
This equation is known as \textbf{Maxwell-Boltzmann energy distribution law} in the general form.\\
The value of $\beta$ is evaluated to be
$$\beta = \frac{1}{kT}$$
Substituting in \eqref{eq:mb12} we get
$$n_i = g_i e^{-\alpha} e^{-\frac{E_i}{kT}}$$
where $k$ is the Boltzmann  constant and T is the absolute temperature. The quantity $e^{-\frac{E_i}{kT}}$ is known as the \textbf{Boltzmann factor}.
From the fundamental postulates of statistical mechanics, we write,N, the total number of particles as
\begin{align*}
  N &= \sum_i n_i\\
  &= \sum_i g_i e^{-\alpha} e^{\frac{-E_i}{kT}}
\end{align*}
\begin{equation}
  e^{-\alpha} = \frac{N}{\sum_i g_i e^{\frac{-E_i}{kT}}} \label{eq:mb13}
\end{equation}
The quantity $\sum_i g_i e^{\frac{-E_i}{kT}}$ is the sum over all the states of the system and is called the \textbf{partition function} of the system denoted by Z. Thus
$$Z = \sum_i g_i e^{\frac{-E_i}{kT}}$$
Substituting in \eqref{eq:mb12}, we have
$$n_i = \frac{N}{Z} g_i e^{\frac{-E_i}{kT}}$$
\subsection{Maxwell-Boltzmann Speed Distribution Law}
The Maxwell-Boltzmann equation for distribution of energy among the molecules of an ideal gas is given by
$$n(E) dE = \frac{2\pi N}{(\pi kT)^{3/2}} E^{\frac{1}{2}} e^{\frac{-E}{kT}}dE$$

\end{document}


\chapter{Type Systems}

A \textbf{Type System}\cite{typesystems} for any programming language is a set of rules that associate a property called "\textit{Type}" with the basic grammatical constructs of the language like variables, constants, statements and commands. It is the computer programming equivalent of type theory which uses type safety and type validation to prevent bugs, compiler runtime errors and compiler optimizations. It is very basic for any programming language to have types to actually perform any of \underline{Higher Order Logic} on the host machine. Note that a type system can work only if the program is run. In other words it is absolutely insignificant to check a program if it is not run or atleast compiled to be run.\\

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Dependent Types }
\textbf{Dependent Types} \cite{dependent_types} is the most important kind of Types whose definition depends on a value. It is the conceptual overlap between computer's Type System and Type theory as it allows evaluation by induction. In functional Programming languages, dependent types increases expressivity of a defined type. It's examples include dependent functions and dependent pairs. Introduction of new variable or new values may completely change the overall behaviour. Thus the Structual Type checking would be used and not nominal (defined below). It is considered to be of two types from mathematical point of view : \\

\begin{itemize}
	\item{
	\subsection{ $\Pi$-Type}
	It is called \textbf{Dependent Product Type} where given a type space $\Omega$, we define a family of types B:A$\rightarrow$ $\Omega$ that says every term a:A has a type B(a):$\Omega$. It explains that there is not a fixed co-domain for functions of this type. The name 'pi-type' comes from the idea that these may be viewed as a Cartesian product of types. Pi-types can also be understood as models of universal quantifiers. For an example let's say a function gives tuple of order n for a given natural number input n. Clearly the terms in that tuple does not depend on the value of n. Here also, the Dependent product nature is preserved.\\
	}
	\item{
	\subsection{ $\Sigma$-Type}
	It is called \textbf{Dependent Sum Type} and is \underline{categorically dual} to dependent product type where given a type space $\Omega$, if we have a family of types B:A$\rightarrow$ $\Omega$ then there must exist a dependent pair type. It explains that there should be fixed codomain for any function. \\
	}
\end{itemize}

They use mathematical operators ($\rightarrow$) and binders ($\forall$) to produce complex types. The higher order \textbf{Dependently Typed Polymorphic lambda calculus} also called \textbf{Calculus of Inductive Constructions} is the foundational basis of all Proof Assistants.

\section{Flow-Sensitive Types}
Such Type systems determine the type of a variable by controlflow. The type of a variable may change in any of the program's method. The type inference (defined below) is used for checking these type systems.

\section{Latent Types}
Latent typing refers to a type system where types are associated with values and not variables.

\section{Refinement Types}
These are a set of some preconditions that define the behaviour of the function or variable. These can be understood as the return types of most of functions which may dpend on some 'if/else' conditions.

\section{Substructural Types}
This is the type system defined depending on the use of a variable. For exmaple, a variable may be used once or many times. It has \textbf{Linear}, \textbf{Affine}, \textbf{Relevant} and \textbf{Ordered} as subcategories. The Linear system allows the use of object exactly once, Affine system allows use atmost once, Relevant system allows usage any number of times and Order system allows using objects only once in an order.

\section{Unique Types}
A Unique type guarantees that an object is used in a single-threaded way only with maximum one reference to it. It mantains the referntial transparency and improves the efficieny of functional languages.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Type Safety \& Memory Safety }
Type safety refers to type equivalence and behavioural equivalence of all language constructs for a given program written in any programming language. Memory safety on the other hand refers to leakage and corruption of memory by any program. example : stack overflow.\\

%% Major Categories in Type Systems %%
Some Type checking techniques to ensure type safety are :

\begin{itemize}
\item {\section*{Static vs Dynamic}

A \textbf{Static} type system refers to compilation time type checking. The source code's type correctness is verified first and then the executable is generated. A \textbf{Dynamic} type system checks type correctness during the runtime. This is the case when bindings and linking are created so that data passed on from one of the program to other is correct type-wise. Together both create polymorphic type checking where a single interface or a single Type check-pass is capable of doing both kind of type checks and is very portable to use. \\}

\item {
\section*{Nominal vs Structural}
In a \textbf{Nominal} or \textbf{Nominative} type system, the equivalence of data types is determined by explicit declarations or name of types. For example, in C programming language, two "\textit{struct} types with different names in the same translation unit are never considered compatible, even if they have identical field declarations. In \textbf{Structural} type system, the inner structure of a data type are evaluated  to show data equivalence and type safety. }

\item{
\section*{Manifest vs Inferred}
\textbf{Manifest} Typing is explicit identification by the software programmer of the type of each variable being declared. For example: if variable X is going to store integers then its type must be declared as integer. Type \textbf{Inference} refers to the automatic detection of the data type of an expression in a programming language. For example, user may write a float point decimal without having declared that variable as a float point. The compiler technology should be able to detect what program is trying to do and perform further actions accordingly. This is also referred to as \textbf{Typeless Typeing} where the type is later introduced depending on data passed.\\ }

\item{
\section*{Duck Type-ing}
This type system got it's name from on of the most common computer engineering practices which says if something behaves like a duck, quacks like aduck then it must be duck. In other words the behaviour determines the data type. For example, adding a float type integer to a double type integer, the answer should have a double type because it is surely to have characteristics of a double type integer.\\}

\end{itemize}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Unified Type System}
In the object oriented programming (OOP) context, the abstraction and scope of methods deals with it's derivation from a base class. similar logic can be extended to class objects whose properties and methods depends on a base class. A \textbf{Unified Type System} states that all the Types of objects are derived from a single root type. Thus that object is capable of performing operations approved by the root type. Example of such a programming language is C\# developed by Microsoft.  In C\# the concepts of encapsulation and methods have been decoupled from the reference requirement so that a type can support methods and encapsulation without being a \textbf{Reference type}. A reference type is also an OOP concept that says all instances of class objects are by reference. \\


\section{Other Coordinate Systems}\label{sec:3Dothercoordsystem}

Coordinate systems are tools that let us use algebraic methods to
understand geometry. While the \dfont{rectangular} (also called
\dfont{Cartesian}) coordinates that we
have been discussing are the most common, some problems are easier to
analyze in alternate coordinate systems.\index{Cartesian coordinates}

A coordinate system is a scheme that allows us to identify any point
in the plane or in three-dimensional space by a set of numbers. In
rectangular coordinates these numbers are interpreted, roughly
speaking, as the lengths of the sides of a rectangular ``box.''

In two dimensions you may already be familiar with an alternative,
called \dfont{polar coordinates}\index{polar coordinates}. In this system, each
point in the plane is identified by a pair of numbers $(r,\theta)$.
The number $\theta$ measures the counter-clockwise angle between the positive
$x$-axis and a vector with tail at the origin and head at the
point, as shown in Figure~\ref{fig:polar coordinates}; the number
$r$ measures the distance from the origin to the
point. Either of these may be negative; a negative $\theta$ indicates
the angle is measured clockwise from the positive
$x$-axis instead of counter-clockwise, and a negative $r$ indicates
the point at distance $|r|$ in the opposite of the direction given by
$\theta$.

The relationship between polar and rectangular coordinates is given by\index{polar coordinates!convert to rectangular}
\begin{align*}
x = r \cos \theta \\
y = r \sin \theta
\end{align*}
and
\begin{align*}
r = \sqrt{x^2 + y^2} \\
\tan \theta = \frac{y}{x}
\end{align*}

\begin{example}{Rectangular to Polar Coordinates}{}
Convert the point $(x,y) = (1, \sqrt3)$ into polar coordinates.
\end{example}

\begin{solution}
First calculate $r$:
\[
r = \sqrt{x^2 + y^2} = \sqrt{ 1 + 3} = 2
\]
Now find $\theta$ such that $\tan \theta = \frac{\sqrt{3}}{1}$. The required $\theta$ is $\frac{\pi}{3}$.

The polar coordinates are $(2, \frac{\pi}{3})$.
\end{solution}

Figure~\ref{fig:polar coordinates} also shows the point with
rectangular coordinates $(1,\sqrt3)$ and polar coordinates
$(2,\pi/3)$, 2 units from the origin and $\pi/3$ radians from the
positive $x$-axis.

\begin{figure}[H]
\centerline{
\vbox{\beginpicture
\normalgraphs
\setcoordinatesystem units <15truemm,15truemm>
\setplotarea x from 0 to 1.3, y from 0 to 2
\axis left /
\axis bottom /
\arrow <4pt> [0.35, 1] from 0 0 to 1 1.73
\put {$x$} [l] <3pt,0pt> at 1.3 0
\put {$y$} [b] <0pt,3pt> at 0 2
\put {$(r,\theta)$} [l] <3pt,0pt> at 1 1.73
\put {$r$} [br] <-2pt,2pt> at 0.5 0.86
\put {$\theta$} [bl] <2pt,0pt> at 0.173 0.1
\arrow <4pt> [0.35,1] from 0.5 -0.2 to 0 -0.2
\arrow <4pt> [0.35,1] from 0.5 -0.2 to 1 -0.2
\put {$r\cos\theta$} at 0.5 -0.4
\put {$r\sin\theta$} [l] <3pt,0pt> at 1 0.86
%\put {$\ds{\pi\over 3}$} [bl] <2pt,2pt> at 0.2 0
\circulararc 60 degrees from 0.2 0 center at 0 0
\setdashes
\plot 1 0 1 1.73 /
\setsolid
\setcoordinatesystem units <15truemm,15truemm> point at -3 0
\setplotarea x from 0 to 1.3, y from 0 to 2
\axis left /
\axis bottom /
\arrow <4pt> [0.35, 1] from 0 0 to 1 1.73
\put {$x$} [l] <3pt,0pt> at 1.3 0
\put {$y$} [b] <0pt,3pt> at 0 2
\put {$(2,\pi/3)$} [l] <3pt,0pt> at 1 1.73
\put {$\pi/3$} [bl] <2pt,0pt> at 0.173 0.1
\circulararc 60 degrees from 0.2 0 center at 0 0
\setdashes
\endpicture}}
\caption{Polar coordinates: the general case and
the point with rectangular coordinates $(1,\sqrt3)$. \label{fig:polar coordinates}}
\end{figure}

We can extend polar coordinates to three dimensions simply by adding a
$z$ coordinate; this is called \dfont{cylindrical coordinates}\index{cylindrical coordinates}.
Each point in three-dimensional space is represented by three
coordinates $(r,\theta,z)$ in the obvious way: this point is $z$ units
above or below the point $(r,\theta)$ in the $x$-$y$ plane,
as shown in Figure~\ref{fig:cylindrical coordinates}. The point
with rectangular coordinates $(1,\sqrt3, 3)$ and cylindrical
coordinates $(2,\pi/3,3)$ is also indicated
in Figure~\ref{fig:cylindrical coordinates}.

\begin{figure}[H]
\centerline{
\vbox{\beginpicture
\normalgraphs
\setcoordinatesystem units <10truemm,10truemm>
\setplotarea x from 0 to 2, y from -1 to 3.3
\axis bottom shiftedto y=0 /
\putrule from 0 0 to 0 3.3
\plot 0 0 -1 -1 /
\arrow <4pt> [0.35,1] from 0 0 to 1.5  -0.7
\put {$(r,\theta,z)$} [l] <3pt,0pt> at 1.5 2.3
\put {$\bullet$} at 1.5 2.3
\put {$z$} [b] <0pt,3pt> at 0 3.3
\put {$x$} [tr] <-3pt,-3pt> at -1 -1
\put {$y$} [l] <3pt,0pt> at 2 0
\put {$\theta$} [t] <0pt,-3pt> at 0 -0.3
\put {$z$} [l] <3pt,0pt> at 1.5 1
\put {$r$} [tr] <-2pt,-2pt> at 1 -0.467
\circulararc 110 degrees from -0.2 -0.2 center at 0 0
\setdashes
\plot 1.5 -0.7 1.5 2.3 /
\setcoordinatesystem units <10truemm,10truemm> point at -5 0
\setplotarea x from 0 to 2, y from -1 to 3.3
\axis bottom shiftedto y=0 /
\putrule from 0 0 to 0 3.3
\plot 0 0 -1 -1 /
\arrow <4pt> [0.35, 1] from 0 0 to 1.5 -0.7
\put {$(2,\pi/3,3)$} [l] <3pt,0pt> at 1.5 2.3
\put {$\bullet$} at 1.5 2.3
\put {$z$} [b] <0pt,3pt> at 0 3.3
\put {$x$} [tr] <-3pt,-3pt> at -1 -1
\put {$y$} [l] <3pt,0pt> at 2 0
\put {$\pi/3$} [t] <0pt,-3pt> at 0 -0.3
\circulararc 110 degrees from -0.2 -0.2 center at 0 0
%\put {$\ds{\pi\over 3}$} [bl] <2pt,2pt> at 0.2 0
\setdashes
\plot 1.5 -0.7 1.5 2.3 /
\endpicture}}
\caption{Cylindrical coordinates: the general case
and the point with rectangular coordinates $(1,\sqrt3, 3)$. \label{fig:cylindrical coordinates}}
\end{figure}

Some figures with relatively complicated equations in rectangular
coordinates will be represented by simpler equations in cylindrical
coordinates. For example, the cylinder in Figure~\ref{fig:cylinder}
has equation $x^2+y^2=4$ in rectangular coordinates, but equation
$r=2$ in cylindrical coordinates.

\begin{figure}[H]
\centerline{
\vbox{\beginpicture
\normalgraphs
\setcoordinatesystem units <3truecm,3truecm>
\setplotarea x from 0 to 1.1, y from 0 to 1.1
\put {\hbox{\epsfxsize7cm\epsfbox{images/cylinder.eps}}} at 0 0
\endpicture}}
\caption{The cylinder $r=2$. \label{fig:cylinder}}
\end{figure}

Given a point $(r,\theta)$ in polar coordinates, it is easy
to see  (as in Figure~\ref{fig:polar coordinates}) that
the rectangular coordinates of the same point are
$(r\cos\theta,r\sin\theta)$, and so the point $(r,\theta,z)$ in
cylindrical coordinates is $(r\cos\theta,r\sin\theta,z)$ in
rectangular coordinates. This means it is usually easy to convert any
equation from rectangular to cylindrical coordinates: simply substitute\index{cylindrical coordinates!convert to rectangular}
\begin{align*}
  x&=r\cos\theta	\\
  y&=r\sin\theta
\end{align*}
and leave $z$ alone.
For example,
starting with $x^2+y^2=4$ and substituting $x=r\cos\theta$,
$y=r\sin\theta$ gives
\begin{align*}
  r^2\cos^2\theta+r^2\sin^2\theta&=4	\\
  r^2(\cos^2\theta+\sin^2\theta)&=4	\\
  r^2&=4	\\
  r&=2.
\end{align*}
Of course, it's easy to see directly that this defines a cylinder as
mentioned above.

Cylindrical coordinates are an obvious extension of polar coordinates
to three dimensions, but the use of the $z$ coordinate means they are
not as closely analogous to polar coordinates as another standard
coordinate system. In polar coordinates, we identify a point by a
direction and distance from the origin; in three dimensions we can do
the same thing, in a variety of ways. The question is: how do we
represent a direction? One way is to give the angle of rotation,
$\theta$, from the positive $x$ axis, just as in cylindrical
coordinates, and also an angle of rotation, $\phi$, from the positive
$z$ axis. Roughly speaking, $\theta$ is like longitude and $\phi$ is
like latitude. (Earth longitude is measured as a positive or negative
angle from the prime meridian, and is always between 0 and 180
degrees, east or west; $\theta$ can be any positive or negative angle,
and we use radians except in informal circumstances.
Earth latitude is measured north or south
from the equator; $\phi$ is measured from the north pole down.) This
system is called \dfont{spherical coordinates}\index{spherical coordinates};
the coordinates are listed in the order
$(\rho,\theta,\phi)$, where $\rho$ is the distance from the
origin, and like $r$ in polar and cylindrical coordinates it may be negative.
The general case and an
example are pictured in Figure~\ref{fig:spherical coordinates}; the
length marked $r$ is the $r$ of cylindrical coordinates.

\begin{figure}[H]
\centerline{
\vbox{\beginpicture
\normalgraphs
\setcoordinatesystem units <10truemm,10truemm>
\setplotarea x from 0 to 2, y from -1 to 3.3
\axis bottom shiftedto y=0 /
\putrule from 0 0 to 0 3.3
\plot 0 0 -1 -1 /
\arrow <4pt> [0.35, 1] from 0 0 to 1.5 -0.7
\arrow <4pt> [0.35, 1] from 0 0 to 1.5 2.3
\put {$(\rho,\theta,\phi)$} [l] <3pt,0pt> at 1.5 2.3
\put {$z$} [b] <0pt,3pt> at 0 3.3
\put {$x$} [tr] <-3pt,-3pt> at -1 -1
\put {$y$} [l] <3pt,0pt> at 2 0
\put {$\theta$} [t] <0pt,-3pt> at 0 -0.3
\put {$\phi$} [bl] <2pt,2pt> at 0 0.4
\put {$\rho$} [tl] <2pt,2pt> at 0.75 1.15
\put {$r$} [tr] <-2pt,-2pt> at 1 -0.467
\circulararc 110 degrees from -0.2 -0.2 center at 0 0
\circulararc -32 degrees from 0 0.4  center at 0 0
\setdashes
\plot 1.5 -0.7 1.5 2.3 /
\setsolid
\setcoordinatesystem units <10truemm,10truemm> point at -5 0
\setplotarea x from 0 to 2, y from -1 to 3.3
\axis bottom shiftedto y=0 /
\putrule from 0 0 to 0 3.3
\plot 0 0 -1 -1 /
\arrow <4pt> [0.35, 1] from 0 0 to 1.5 -0.7
\arrow <4pt> [0.35, 1] from 0 0 to 1.5 2.3
\put {$(\sqrt{13},\pi/3,\arctan(2/3))$} [l] <3pt,0pt> at 1.5 2.3
\put {$z$} [b] <0pt,3pt> at 0 3.3
\put {$x$} [tr] <-3pt,-3pt> at -1 -1
\put {$y$} [l] <3pt,0pt> at 2 0
\put {$\pi/3$} [t] <0pt,-3pt> at 0 -0.3
\circulararc 110 degrees from -0.2 -0.2 center at 0 0
\circulararc -32 degrees from 0 0.4  center at 0 0
%\put {$\ds{\pi\over 3}$} [bl] <2pt,2pt> at 0.2 0
\setdashes
\plot 1.5 -0.7 1.5 2.3 /
\endpicture}}
\caption{Spherical coordinates: the general case and the point
with rectangular coordinates $(1,\sqrt3 , 3)$. \label{fig:spherical coordinates}}
\end{figure}

As with cylindrical coordinates, we can easily convert equations
in rectangular coordinates to the equivalent in spherical coordinates,
though it is a bit more difficult to discover the proper substitutions.
Figure~\ref{fig:rectangular to spherical coordinates} shows
the typical point in spherical coordinates from
Figure~\ref{fig:spherical coordinates},
viewed now so that the arrow marked $r$ in the original graph appears as
the horizontal ``axis'' in the left hand graph. From this diagram it
is easy to see that the $z$ coordinate is $\rho\cos\phi$, and that
$r=\rho\sin\phi$, as shown. Thus, in converting from rectangular to
spherical coordinates we will replace $z$ by $\rho\cos\phi$. To see
the substitutions for $x$ and $y$ we now view the same point from
above, as shown in the right hand graph. The hypotenuse of the
triangle in the right hand graph
is $r=\rho\sin\phi$, so the sides of the triangle, as shown,
are $x=r\cos\theta=\rho\sin\phi\cos\theta$ and
$y=r\sin\theta=\rho\sin\phi\sin\theta$. Therefore to convert from rectangular to spherical coordinates,
we make these substitutions:\index{spherical coordinates!convert to rectangular}
\begin{align*}
  x&=\rho\sin\phi\cos\theta	\\
  y&=\rho\sin\phi\sin\theta	\\
  z&=\rho\cos\phi.
\end{align*}

\begin{figure}[H]
\centerline{
\vbox{\beginpicture
\normalgraphs
\setcoordinatesystem units <10truemm,10truemm>
\setplotarea x from 0 to 2.3, y from -1 to 3.3
\axis bottom shiftedto y=0 /
\putrule from 0 0 to 0 3.3
\plot 0 0 2 3 /
\put {$(\rho,\theta,\phi)$} [l] <3pt,0pt> at 2 3
\put {$z$} [b] <0pt,3pt> at 0 3.3
\put {$\phi$} [bl] <2pt,2pt> at 0 0.4
\put {$\rho$} [tl] <2pt,2pt> at 1 1.5
\circulararc -32 degrees from 0 0.4  center at 0 0
\put {$\rho\sin\phi$} [t] <0pt,-3pt> at 1 0
\arrow <4pt> [0.35,1] from 0.3 -0.3 to 0 -0.3
\arrow <4pt> [0.35,1] from 1.7 -0.3 to 2 -0.3
\put {$\rho\sin\phi$} at 1 3
\arrow <4pt> [0.35,1] from 0.3 3 to 0 3
\arrow <4pt> [0.35,1] from 1.7 3 to 2 3
\put {$\rho\cos\phi$} [r] <-3pt,0pt> at 0 1.5
\arrow <4pt> [0.35,1] from -0.3 1.8 to -0.3 3
\arrow <4pt> [0.35,1] from -0.3 1.2 to -0.3 0
\setdashes
\plot 2 0 2 3 /
\setsolid
\setcoordinatesystem units <17truemm,17truemm> point at -3 0
\setplotarea x from 0 to 1.3, y from 0 to 2
\axis bottom shiftedto y=0 /
\axis left /
\plot 0 0 1 1.728 /
\arrow <4pt> [0.35, 1] from 0 0 to 1 1.728
%\put {\rotstart{-60 rotate}$\rho\sin\phi$\rotfinish} <3pt,-3pt> at 0.5 0.86
\put {$x$} [l] <3pt,0pt> at 1.3 0
\put {$y$} [b] <0pt,3pt> at 0 2
\put {$\theta$} [bl] <2pt,2pt> at 0.346 0.2
\put {$\rho\sin\phi\cos\theta$} at 0.5 -0.4
\arrow <4pt> [0.35,1] from 0.5 -0.1 to 1 -0.1
\arrow <4pt> [0.35,1] from 0.5 -0.1 to 0 -0.1
\put {$\rho\sin\phi\sin\theta$} [l] <3pt,0pt> at 1 0.85
\arrow <4pt> [0.35,1] from 1.3 1.1 to 1.3 1.728
\arrow <4pt> [0.35,1] from 1.3 0.5 to 1.3 0
\circulararc 60 degrees from 0.4 0 center at 0 0
\setdashes
\plot 1 0 1 1.728 /
\endpicture}}
\caption{Converting from rectangular to spherical coordinates. \label{fig:rectangular to spherical coordinates}}
\end{figure}

As the cylinder had a simple equation in cylindrical coordinates, so
does the sphere in spherical coordinates.

\begin{example}{}{}
Find an equation for the sphere of radius $2$ in spherical coordinates.
\end{example}

\begin{solution}
If we start with the
Cartesian equation of the sphere and substitute, we get the spherical
equation:
\begin{align*}
  x^2+y^2+z^2&=2^2	\\
  \rho^2\sin^2\phi\cos^2\theta+
     \rho^2\sin^2\phi\sin^2\theta+\rho^2\cos^2\phi&=2^2	\\
  \rho^2\sin^2\phi(\cos^2\theta+\sin^2\theta)+\rho^2\cos^2\phi&=2^2	\\
  \rho^2\sin^2\phi+\rho^2\cos^2\phi&=2^2	\\
  \rho^2(\sin^2\phi+\cos^2\phi)&=2^2	\\
  \rho^2&=2^2	\\
  \rho&=2
\end{align*}
Therefore, in spherical coordinates, a sphere of radius $2$ is expressed $\rho=2$.
\end{solution}

Although not as simple as with cylindrical coordinates, we can use spherical coordinates to describe the equation of a cylinder.

\begin{example}{Cylinder Equation in Spherical Coordinates}{}
Find an equation for the cylinder $x^2+y^2=4$ in spherical
coordinates.
\end{example}
\begin{solution}
Proceeding as in the previous example:
\begin{align*}
  x^2+y^2&=4	\\
  \rho^2\sin^2\phi\cos^2\theta+
     \rho^2\sin^2\phi\sin^2\theta=4	\\
  \rho^2\sin^2\phi(\cos^2\theta+\sin^2\theta)&=4	\\
  \rho^2\sin^2\phi&=4	\\
  \rho\sin\phi&=2	\\
  \rho&={2\over\sin\phi}
\end{align*}
\end{solution}

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

\begin{enumialphparenastyle}

\begin{ex}
Convert the following points in rectangular coordinates to
cylindrical and spherical coordinates:
\begin{enumerate}
\item $(1,1,1)$
\item $(7,-7,5)$
\item $(\cos(1),\sin(1),1)$
\item $(0,0,-\pi)$
\end{enumerate}
\begin{sol}
\begin{enumerate}
	\item	$\ds (\sqrt2,\pi/4,1)$, $\ds (\sqrt3,\pi/4,\arccos(1/\sqrt3))$
	\item	$\ds (7\sqrt2,7\pi/4,5)$, $\ds (\sqrt{123},7\pi/4,\arccos(5/\sqrt{123})$
	\item	$(1,1,1)$, $\ds (\sqrt2,1,\pi/4)$
	\item	$(0,0,-\pi)$, $(\pi,0,\pi)$
\end{enumerate}
\end{sol}
\end{ex}

\begin{ex}
Find an equation for the sphere $x^2+y^2+z^2=4$ in
cylindrical coordinates.
\begin{sol}
	$r^2+z^2=4$
\end{sol}
\end{ex}

\begin{ex}
Find an equation for the $y$-$z$ plane in cylindrical
coordinates.
\begin{sol}
	$r\cos\theta=0$
\end{sol}
\end{ex}

\begin{ex}
Find an equation equivalent to $x^2+y^2+2z^2+2z-5=0$ in
cylindrical coordinates.
\begin{sol}
	$r^2+2z^2+2z-5=0$
\end{sol}
\end{ex}

\begin{ex}
Suppose the curve $z=e^{-x^2}$ in the $x$-$z$ plane is
rotated around the $z$ axis. Find an equation for the resulting
surface in cylindrical coordinates.
\begin{sol}
	$z=e^{-r^2}$
\end{sol}
\end{ex}

\begin{ex}\label{ex:rotate z=x z axis cylindrical}
Suppose the curve $z=x$ in the $x$-$z$ plane is
rotated around the $z$ axis. Find an equation for the resulting
surface in cylindrical coordinates.
\begin{sol}
	$z=r$
\end{sol}
\end{ex}

\begin{ex}
Find an equation for the plane $y=0$ in
spherical coordinates.
\begin{sol}
	$\sin\theta=0$
\end{sol}
\end{ex}

\begin{ex}
Find an equation for the plane $z=1$ in
spherical coordinates.
\begin{sol}
	$1=\rho\cos\phi$
\end{sol}
\end{ex}

\begin{ex}
Find an equation for the sphere with radius 1 and center at
$(0,1,0)$ in spherical coordinates.
\begin{sol}
	$\rho=2\sin\theta\sin\phi$.
\end{sol}
\end{ex}

\begin{ex}
Find an equation for the cylinder $x^2+y^2=4$ in
spherical coordinates.
\begin{sol}
	$\rho\sin\phi=2$
\end{sol}
\end{ex}

\begin{ex}\label{ex:rotate z=x z axis spherical}
Suppose the curve $z=x$ in the $x$-$z$ plane is
rotated around the $z$ axis. Find an equation for the resulting
surface in spherical coordinates.
\begin{sol}
	$\cos\phi=1/\sqrt2$
\end{sol}
\end{ex}

\begin{ex}
Plot the polar equations $r=\sin(\theta)$ and $r=\cos(\theta)$
and comment on their similarities.  (If you get stuck on how to plot
these, you can multiply both sides of each equation by $r$ and convert
back to rectangular coordinates).
\end{ex}

\begin{ex}
Extend Exercises~\ref{ex:rotate z=x z axis cylindrical}
and \ref{ex:rotate z=x z axis spherical} by rotating the curve $z=mx$
around the $z$ axis and converting to both cylindrical and spherical
coordinates.
\begin{sol}
	$z=mr$; $\cot\phi=m$ if $m\neq0$, $\phi=0$ if $m=0$
\end{sol}
\end{ex}

\begin{ex}
Convert the spherical formula $\rho=\sin \theta \sin \phi$ to
rectangular coordinates and describe the surface defined by the
formula (Hint: Multiply both sides by $\rho$.)
\begin{sol}
	A sphere with radius $1/2$, center at $(0,1/2,0)$
\end{sol}
\end{ex}

\begin{ex}
We can describe points in the first octant by $x >0$, $y>0$ and
$z>0$.  Give similar inequalities for the first octant in cylindrical
and spherical coordinates.
\begin{sol}
	$0<\theta<\pi/2$, $0<\phi<\pi/2$, $\rho>0$;
$0<\theta<\pi/2$, $r>0$, $z>0$
\end{sol}
\end{ex}

\end{enumialphparenastyle}


\section{Simulation-Based Inference} \label{sec:sbi}
% standard bayesian approach and introducing SBI
The goal of Bayesian SED modeling, and probabilistic inference more
broadly, is to infer the posterior probability distributions
$p(\btheta\given\bfi{x})$ of galaxy properties, $\btheta$, given observations,
$\bfi{x}$.
For a specific $\btheta$ and $\bfi{x}$, we typically evaluate the posterior
using Bayes' rule,
$p(\btheta\given\bfi{x}) \propto p(\btheta)~p(\bfi{x}\given\btheta)$, where
$p(\btheta)$ denotes the prior distribution and $p(\bfi{x}\given\btheta)$ the
likelihood, which is typically assumed to have a Gaussian functional form:
\beq
\label{eq:likelihood}
    \ln p(\bfi{x}\given\btheta) = -\frac{1}{2}\left(\bfi{x} - m(\btheta)\right)^T {\bf C}^{-1}
    \left(\bfi{x} - m(\btheta)\right).
\eeq
$m(\btheta)$ is the theoretical model, in our case a galaxy SED model from SPS.
${\bf C}$ is the covariance matrix of the observations.
In practice, off-diagonal terms are often ignored and measured uncertainties
are used as estimates of the diagonal terms.

% overview of SBI and mention of ABC
Simulation-based inference (SBI; also known as ``likelihood-free'' inference)
offers an alternative that requires no assumptions about the form of the
likelihood.
Instead, SBI uses a generative model, \emph{i.e.} a simulation $F$, to generate
mock data $\bfi{x}'$ given parameters $\btheta'$: $F(\btheta') = \bfi{x}'$.
It uses a large number of simulated pairs $(\btheta', \bfi{x}')$ to directly estimate
either the posterior  $p(\btheta\given \bfi{x})$, the likelihood
$p(\bfi{x}\given \btheta)$, or the joint distribution of the parameters and data $p(\btheta, \bfi{x})$.
SBI has already been successfully applied to a number of Bayesian parameter
inference problems in astronomy~\citep[\emph{e.g.}][]{cameron2012, weyant2013,
hahn2017b, kacprzak2018, alsing2018, wong2020, huppenkothen2021, zhang2021}
and in physics~\citep[\emph{e.g.}][]{brehmer2019, cranmer2020}.

%One simple and pedagogical example of SBI is Approximate Bayesian Computation~\citep[ABC;][]{rubin1984, pritchard1999, beaumont2002}, which uses a rejection sampling framework to estimate the posterior.  First, parameter values are sampled from the prior: $\btheta'\sim p(\btheta)$.  The forward model, $F$, is then run on $\btheta'$ to generate simulated data $F(\btheta') = \bfi{x}'$.  If the simulated $\bfi{x}'$ is `close' to the observed $\bfi{x}$, usually based on a threshold on some distance criterion $\rho(\bfi{x}', \bfi{x}) < \epsilon$, $\btheta'$ is kept.  Otherwise, $\btheta'$ is rejected.  This process is repeated until there are enough samples to estimate the posterior.  The estimated posterior from ABC can be written as $p(\btheta \given \rho(F(\btheta), \bfi{x}) < \epsilon)$.  In the case where $\epsilon\rightarrow 0$, the conditional statement is equivalent to the condition $F(\btheta) = \bfi{x}$; thus, the estimated ABC posterior is  equivalent to the true posterior: $p(\btheta \given \rho(F(\btheta), \bfi{x}) < \epsilon\rightarrow 0) \equiv p(\btheta \given \bfi{x})$.

\subsection{Amortized Neural Posterior Estimation} \label{sec:flow}
SBI provides another a critical advantage over MCMC inference methods --- it
enables \emph{amortized inference}.
For SED modeling using MCMC, each galaxy requires >$10^5$ model evaluations to
accurately estimate $p(\btheta \given \bfi{x})$~(\citealt{hahn2022},
Kwon~\etal~in prep.).
Moreover, model evaluations for calculating the posterior of one galaxy cannot
be used for another.
This makes MCMC approaches for SED modeling of upcoming surveys computationally
infeasible.

With density estimation SBI, we require a large number (${\sim}10^6$) of model
evaluations only initially to train a neural density estimator (NDE), a neural
network with parameters $\bphi$ that is trained to estimate the density
$p_\phi(\btheta \given \bfi{x}')$.
If the training covers the entire or the practically relevant portions of the
$\btheta$ and $\bfi{x}$ spaces, we can evaluate
$p_\phi(\btheta\given\bfi{x}_i)$ for each galaxy $i$ with minimal computational
cost.
The inference is therefore amortized and no additional model evaluations are
needed to generate the posterior for each galaxy.
This technique is called  Amortized Neural Posterior Estimation (ANPE)
and has recently been applied to a broad range of astronomical applications
from analyzing gravitational waves~\citep[\emph{e.g.}][]{wong2020,dax2021} to
binary microlensing lensing~\citep{zhang2021}.
For SED modeling, the choice in favor of using ANPE is easy: the entire upfront
cost for ANPE model evaluations would only yield posteriors of tens of galaxies
with MCMC.

ANPE makes two important assumptions.
First, the simulator $F$ is capable of generating mock data $\bfi{x}'$ that is
practically indistinguishable from the observations.
In terms of the expected signal, $m$ in Eq.~\ref{eq:likelihood}, this is the
same requirement as any probabilistic modeling approach.
But unlike likelihood-based evaluations, such as conventional MCMC, data
generated for SBI need to include all relevant noise terms as well.
We address both aspects in Sections \ref{sec:training} and \ref{sec:forward-model}.
Second, ANPE assumes that the NDE is well trained:
$p_\phi(\btheta \given \bfi{x}')$ is a good approximation of
$p(\btheta \given \bfi{x}')$, and therefore of $p(\btheta \given \bfi{x})$.
We assess this in Section~\ref{sec:results}.

ANPE commonly employs so-called ``normalizing flows''~\citep{tabak2010,
tabak2013} as density estimators.
Normalizing flow models use an invertible bijective transformation, $f$, to map
a complex target distribution to a simple base distribution, $\pi(\bfi{z})$, that is
fast to evaluate.
For ANPE, the target distribution is $p(\btheta \given \bfi{x})$ and the
$\pi(\bfi{z})$ is typically a simple multivariate Gaussian, or mixture of Gaussians.
The transformation $f: \bfi{z} \rightarrow \btheta$ must be invertible and have a
tractable Jacobian.
This is so that we can evaluate the target distribution from $\pi(\bfi{z})$ by
a change of variable:
\begin{equation} \label{eq:normflow}
    p(\btheta \given \bfi{x}) = \pi(\bfi{z}) \Bigl|{\rm det} \left(\frac{\partial
    f^{-1}}{\partial \btheta} \right)\Bigr|.
\end{equation}
Since the base distribution is easy to evaluate, we can also easily evaluate
the target distribution.
A neural network is trained to obtain $f$ and the collection of its parameters
form $\bphi$.
The network typically consists of a series of simple transforms (\emph{e.g.}
shift and scale transforms) that are each invertible and whose Jacobians are
easily calculated.
By stringing together many such transforms, $f$ provides an extremely flexible
mapping from the base distribution.
%Rather than a single complicated transformation, the network is typically restricted to a series of simple transforms that are each invertible and whose Jacobians are easily calculated.

Many different normalizing flow models are now available in the
literature~\citep[\emph{e.g.}][]{germain2015, durkan2019}.
In this work, we use Masked Autoregressive
Flow~\citep[MAF;][]{papamakarios2017}.
The autoregressive design~\citep{uria2016} of MAF is particularly well-suited
for modeling conditional probability distributions such as the posterior.
Autoregressive models exploit chain rule to expand a joint probability of a set
of random variables as products of one-dimensional conditional
probabilities: $p(\bfi{x}) = \prod_i p(x_i\given x_{1:i-1})$.
They then use neural networks to describe each conditional probability,
$p(x_i\given x_{1:i-1})$.
In this context, we can add a conditional variable $y$ on both sides of the
equation, $p(\bfi{x}\given \bfi{y}) = \prod_i p(x_i\given x_{1:i-1}, \bfi{y})$, so that the
autoregressive model describes a conditional probability $p(\bfi{x}\given \bfi{y})$.
One drawback of autoregressive models is their sensitivity to the ordering of
the variables.
Masked Autoencoder for Distribution Estimation~\citep[MADE;][]{germain2015}
models address this limitation using binary masks to impose the autoregressive
dependence and by permutating the order of the conditioning variables.
A MAF model is built by stacking multiple MADE models.
Hence, it has the autoregressive structure of MADE but with more flexibility to
describe complex probability distributions.
In practice, we use the MAF implementation in the $\mathtt{sbi}$ Python
package\footnote{\url{https://github.com/mackelab/sbi/}}~\citep{greenberg2019,
tejero-cantero2020}.


\documentclass[]{article}
\usepackage{amsmath}
\usepackage{graphicx}

\begin{document}

In this post, I will briefly review the basic theory about ordinary linear regression using frequentist and Bayesian estimation methods. This will lay the terrain for a later post about Gaussian Processes. This post closely follows the presentation in Murphy (2012).

\section{Basics of Linear Regression}

Linear regression used to fit a model that is linear in the parameters to a data set $\mathcal{D}=\{\boldsymbol{X},\boldsymbol{y}\}$, where $\boldsymbol{X}$ represents the independent variables and $\boldsymbol{y}$ the dependent variable, and has the form

$p(y|\boldsymbol{x}, \boldsymbol{\beta}) = \mathcal{N}(y|\boldsymbol{\beta}^T\boldsymbol{x}, \sigma_{\epsilon}^2)$

where $\boldsymbol{\beta}$ is a vector of model parameters and $\sigma_{\epsilon}^2$ is the model error variance. The unusual notation for the normal distribution $\mathcal{N}$ should be read as "a normal distribution of $\boldsymbol{y}$ with (or given) mean $\boldsymbol{\beta}^T\boldsymbol{x}$ and variance $\sigma_{\epsilon}^2$." The traditional method of estimating the parameters of a linear regression model is by using the frequentist method of maximum likelihood estimation (MLE). MLE provides point estimates $\boldsymbol{\hat{\beta}}$ for each of the regression model parameters $\boldsymbol{\beta}$ by maximizing the likelihood function with respect to $\boldsymbol{\beta}$ and $\sigma_{\epsilon}^2$ by solving the minimization problem

$\boldsymbol{\hat{\beta}} = arg\,\underset{\boldsymbol{\beta}}{max}\;\mathcal{L}(\boldsymbol{\beta})$

where the likelihood function $\mathcal{L}(\boldsymbol{\beta})$ is defined as

$\mathcal{L}(\boldsymbol{\beta})=p(\mathcal{D}|\boldsymbol{\beta})={\displaystyle \prod_{i=1}^{N} p(y_i|\boldsymbol{x}_i, \boldsymbol{\beta}, \mu, \sigma_{\epsilon}^2)} = \mathcal{N}(\boldsymbol{y}|\mu + \boldsymbol{X}\boldsymbol{\beta},\sigma_{\epsilon}^2\boldsymbol{I})$

where $\mu$ is the mean of the model error ($\mu=0$ hereafter), $N$ is the number of points in the data set, $\boldsymbol{I}$ is an identity matrix, and $\boldsymbol{x}$ is a vector of values of the independent variables for an individual data point of matrix $\boldsymbol{X}$. If a non-linear function shape is sought, linear regression can be used to fit a model linear in the parameters over a function $\phi(\cdot)$ of the data. This procedure is called basis function expansion and has a likelihood function of the form

$\mathcal{L}(\boldsymbol{\beta}) = \mathcal{N}(\boldsymbol{y}|\mu + \phi(\boldsymbol{X})\boldsymbol{\beta},\sigma_{\epsilon}^2\boldsymbol{I})$

where $\phi(\boldsymbol{x})$ can have, for example, the form

$\phi(\boldsymbol{x})=[1, x_1, x_1^2]$

for fitting a parabola to a one-dimensional $\boldsymbol{X}$ over $\boldsymbol{y}$. When minimizing the squared residuals we get to the famous Ordinary Least Squares regression. Linear regression can be further developed, for example, into Ridge Regression to better handle multicollinearity by introducing bias to the parameter estimates, and into Kernel Ridge regression to implicitly add non-linear terms to the model. These formulations are beyond the scope of this post.

What is important to notice is that the standard approaches for linear regression described here, although able to fit linear and non-linear functions, do not provide much insight into the model errors. That is when Bayesian methods come to play.

\section{Bayesian Linear Regression}

In Bayesian Linear regression (and in any Bayesian approach), the parameters $\boldsymbol{\beta}$ are treated themselves as random variables. This allows for the consideration of model uncertainty, given that now instead of having the best point estimates for $\boldsymbol{\beta}$ we have their full distributions from which to sample models – a sample of  $\boldsymbol{\beta}$ corresponds to a model. The distribution of parameters for $\boldsymbol{\beta}$, $P(\boldsymbol{\beta}|\mathcal{D})$, called the parameter posterior distribution, is calculated by multiplying the likelihood function used in MLE by a prior distribution for the parameters. A prior distribution is assumed from knowledge prior to analyzing new data. In Bayesian Linear Regression, a Gaussian distribution is commonly assumed for the parameters. For example, the prior on $\boldsymbol{\beta}$ can be $\mathcal{N}(\boldsymbol{\beta_0},\boldsymbol{V}_0)$ for algebraic simplicity. The parameter posterior distribution assuming a known  then has the form

$p(\boldsymbol{\beta}|\mathcal{D}, \sigma^2) =\frac{p(\mathcal{D}|\boldsymbol{\beta}, \sigma^2)p(\boldsymbol{\beta})}{p(\mathcal{D})} \propto p(\mathcal{D}|\boldsymbol{\beta}, \sigma^2)p(\boldsymbol{\beta})$  ,        given $p(\mathcal{D})$ is a constant depending only on the data.

We now have an expression from which to derive our parameter posterior for our linear model from which to sample $\boldsymbol{\beta}$. If the likelihood and the prior are Gaussian, the parameter posterior will also be a Gaussian, given by

$p(\boldsymbol{\beta}|\mathcal{D}, \sigma^2) \propto \mathcal{N}(\boldsymbol{\beta}|\boldsymbol{\beta}_0, \boldsymbol{V}_0)\mathcal{N}(\boldsymbol{y}|\boldsymbol{X\beta},\sigma^2\boldsymbol{I})=\mathcal{N}(\boldsymbol{\beta}|\boldsymbol{\beta}_N,\boldsymbol{V}_N)$
where
$\boldsymbol{V}_N = \sigma^2(\sigma^2\boldsymbol{V}_0^{-1}+\boldsymbol{X}^T\boldsymbol{X})^{-1}$
$\boldsymbol{\beta}_N=\boldsymbol{V}_N\boldsymbol{V}_0^{-1}\boldsymbol{\beta}_0 + \frac{1}{\sigma^2}\boldsymbol{V}_N\boldsymbol{X}^T\boldsymbol{y}$

If we calculate the parameter posterior (distribution over parameters $\boldsymbol{\beta}$) for a simple linear model $f(x) = \beta_0 + \beta_1x$ for a data set $\mathcal{D}$ in which $\boldsymbol{X}$ and $\boldsymbol{y}$ are approximately linearly related given some noise $\sigma_{\epsilon}^2$, we can use it to sample values for $\boldsymbol{\beta}$. This is equivalent to sampling linear models $f(x)$ for data set $\mathcal{D}$. As the number of data points increase, the variability of the sampled models should decrease, as in the figure below

\begin{figure}
	\centering
	\includegraphics[width=0.9\linewidth]{linear_model.png}
	\label{fig:linearmodel}
\end{figure}

This is all interesting but the parameter posterior per se is not of much use. We can, however, use the parameter posterior to find the posterior predictive distribution, which can be used to both get point estimates of $y$ and the associated error. This is done by multiplying the likelihood by the parameter posterior and marginalizing the result over $\boldsymbol{\beta}$. This is equivalent to performing infinite sampling of blue lines in the example before to form density functions around a point estimate of $\mu_{y_*}=f(\boldsymbol{x}_*)$, with $(\boldsymbol{x}_*,y_*)$ denoting a new point that is not in $\mathcal{D}$. If the likelihood and the parameter posterior are Gaussian, the posterior predictive then takes the form below and will also be Gaussian (in Bayesian parlance, this means that the Gaussian distribution is conjugate to itself)!

$\begin{aligned}p(y_*|x_*,\mathcal{D}, \sigma_{\epsilon}^2) &= \int_{\boldsymbol{\beta}}p(y_*|x_*,\mathcal{D}, \sigma_{\epsilon}^2,\boldsymbol{\beta})p(\boldsymbol{\beta}|\mathcal{D}, \sigma)d\boldsymbol{\beta}\\

&=\mathcal{N}(\boldsymbol{\beta}_N^T, \sigma_N^2(\boldsymbol{x}_*))\end{aligned}$

where

$\sigma_N^2(\boldsymbol{x}_*) = \sigma_{\epsilon}^2 + x^{*T}\boldsymbol{V}_Nx_*$

or, to put it simply,

$\boldsymbol{y}_* = f(\boldsymbol{x}_*) = \boldsymbol{\beta}_N(\boldsymbol{x}_*) \pm \sigma_N^2(\boldsymbol{x}_*)$

The posterior predictive, meaning final linear model and associated errors (the parameter uncertainty equivalent of frequentist statistics confidence intervals) are shown below

\begin{figure}
	\centering
	\includegraphics[width=0.9\linewidth]{linear_regression.png}
	\label{fig:linearregression}
\end{figure}

If, instead of having a data set $\mathcal{D}$ in which $\boldsymbol{X}$ and $\boldsymbol{y}$ are related approximately according to a 4th order polynomial, we use a $\phi(\cdot)$ function to artificially create more random variables (or features, in machine learning parlance) corresponding to the non-linear terms of the polynomial function. Our function $\phi(\cdot)$ would be $\phi(x) = [1, x, x^2, x^3, x^4]$ and the resulting $X$ would therefore have five columns instead of two ($[1, x]$), so now the task is to find $\boldsymbol{\beta}=[\beta_0, \beta_1, \beta_2, \beta_3, \beta_4]$. Following the same logic as before for $X'=\phi(X)$ and $x_*'=\phi(x_*)$, where prime denotes the new set random variable $x$ from function $\phi(\cdot)$, we get the following plots

\begin{figure}
	\centering
	\includegraphics[width=0.9\linewidth]{poly_model.png}
	\caption{}
	\label{fig:polymodel}
\end{figure}

\begin{figure}
	\centering
	\includegraphics[width=0.9\linewidth]{poly_regression.png}
	\caption{}
	\label{fig:polyregression}
\end{figure}

This looks great, but there is a problem: what if we do not know the functional form of the model we are supposed to fit (e.g. a simple linear function or a 4th order polynomial)? This is often the case, such as when modeling the reliability of a water reservoir system contingent on stored volumes, inflows and evaporation rates, or when modeling topography based on samples surveyed points (we do not have detailed elevation information about the terrain, e.g. a DEM file). Gaussian Processes (GPs) provide a way of going around this difficulty.

\section{References}

Murphy, Kevin P., 2012. Machine Learning: A Probabilistic Perspective. The MIT Press.

\end{document}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Toolset}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Unary Math}
\label{sec:unarymath}
Us humans are used to representing numbers in decimal, 0, 1, 9 for the ten
first numbers, followed by 10 to 19, 20 to 29... 90 to 99, then 100 and so
on; yet at early childhood we use our fingers to represent numbers, depending
on the culture we open or close as many fingers as the number we want to
represent.

Here in \sed* we will thus represent unary numbers by means of a string
composed of a single character repeated as many times as the number being
represented.  \sed* support for transformations based on Regular Expressions
makes working with them quite easy, as we will see here.

\begin{description}
	\item[Addition: $x^ny^m\mapsto x^{n+m}$:]
		\begin{verbatim}
			s/y/x/g
		\end{verbatim}

	\item[Duplication: $x^n\mapsto x^{2n}$:]
		\begin{verbatim} s/xx*/&&/ \end{verbatim}

	\item[Assignment: $x^n\mapsto x^ny^m$:]

		This can be achieved by an intermediate representation containing a pipe in
		the middle of a duplication: $x^n\texttt{|}x^n$, that way each of the
		{\tt x}s after the pipe can be converted one by one into {\tt y}s.  At the
		i-th iteration we will have:
		$x^ny^{i-1}\texttt{|}xx^{n-i} \mapsto x^ny^i\texttt{|}x^{n-i}$.

		\begin{Verbatim}
			s/x*/&|&/; :a; s/|x/y|/; ta; s/|//
		\end{Verbatim}

	\item[Multiplication: $x^ny^m\mapsto x^{n(m+1)}$:]

		Since $n\cdot m=n(m-1)+n$, this can be solved by consuming one {\tt y}
		at a time by repeating the substitution $x^ny^m \equiv x^nyy^{m-1}
		\mapsto	x^ny^{m-1}x^n$ while there are {\tt y} availables.  The last
		substitution will then be $x^nyx^{n(m-1)} \mapsto x^nx^nx^{n(m-1)}
		\equiv x^{n(m+1)}$:

		\begin{Verbatim}
			:a; s/\(x*\)y\(y*\)/\1\2\1/; ta
		\end{Verbatim}

		To calculate $n\cdot m$ instead of $n(m+1)$, we can either delete one
		{\tt y} if we know there is at least one (\verb|s/y//|); or we can place
		a separator between the {\tt x}s and the {\tt y}s, then at the iteration
		we will have $x^n\texttt{|yx}^{n(m-1)} \mapsto x^n\texttt{|x}^{nm}$:

		\begin{Verbatim}
			s/x*/&|/
			:a; s/\(x*\)|y\(y*\)/\1|\2\1/; ta
			s/x*|//
		\end{Verbatim}

	\item[Factorization: $\texttt{x}^n \mapsto
		\texttt{|x}^{p_1}\texttt{|x}^{p_2}\texttt{|}
		\ldots\texttt{|x}^{p_k}\texttt{|}$:]

		We can use the following code to compute the prime numbers
		$p_1, p_2\ldots p_n$, where $n=\prod_{i=1}^k p_i$.
		Additionally this prime factors will be sorted
		decreasingly, assuring a unique decomposition.

		\begin{Verbatim}
			s/x*/|&|/; :a
			  s/|\(xxx*\)\(\1\1*\)|/|\1<x>\2|/
			  s/\(x*\)<\(x*\)>\1/\1<\2x>/
			  s/<\(x*\)>|/|\1|/
			ta
		\end{Verbatim}

		\begin{enumerate}
			\item On the first line we wrap the {\tt x}s between pipes just to
				make processing simple, it is not required though.

			\item Then for a block of length $n$ this is when we find a
				divisor $d>1$ of $n$, the backreference is placed twice to ensure
				$d<n$.  Since the algorithm for matching regular expressions is
				greedy, \verb|\1| will match the largest divisor smaller than $n$,
				meaning that $n/d$ is the smallest prime that divides $n$.

				The block $\texttt{|x}^n\texttt{|}$ will be converted into
				$\texttt{|x}^d\texttt{<x>x}^{n-d}\texttt{|}$.

			\item The next substitution gradually substracts $d$ from the
				``number'' after the \verb|>| symbol, adding one before it.
				In other words we just divided $n$ over $d$ using repeated
				substraction.
				So after applying this rule as many times as possible, we obtain:
				$\texttt{|x}^d\texttt{<x}^{n/d}\texttt{>|}$.

			\item And the final rule, applicable only when the division has
				completed, is to convert it back to the ``pipe separated
				unary format'' so that we can proceed to find newer factors:
				$ \texttt{|x}^n\texttt{|} \mapsto \ldots
				\mapsto \texttt{|x}^d\texttt{|x}^{n/d}\texttt{|}$
		\end{enumerate}

	\item[Logarithm: $x^n\mapsto y^{\lfloor\log_2 n\rfloor}$:]

		The idea here is to divide by 2 repeatedly until {\tt xx} can no longer
		be found.  Note that we divide by 2 rounding down by substituting a
		single (final) {\tt x} in the pattern at line 2 to an empty string.
		There may be a remaining {\tt x} which we remove at the end.

		% sedcode logarithm:
		\begin{Verbatim}
			:a; /xx/ {
			  s/x\(x\|\)/\1/g
			  s/^/y/
			ba }
			s/x//
		\end{Verbatim}

	\item[Minimum: $\texttt{x}^n\texttt{|x}^m \mapsto
		\texttt{x}^{\min\{n,m\}}$:]

		% sedcode minimum:
		Calculating the minimum between $n$ and $m$ is performed by
		substituting them leaving only the longest common prefix.  Such a
		transformation is simply \verb!s/\(x*\)x*|\1x*/\1/!.

	\item[Maximum: $\texttt{x}^n\texttt{|x}^m \mapsto
		\texttt{x}^{\max\{n,m\}}$:]

		To fully comprehend how the implementation presented here
		we need to embrace the fact that \sed* uses a greedy algorithm for
		substitutions. In other words whenever our regular
		expression is the concatenation of two or more quantified parts, it
		will try to match as much as possible in the first part, then as
		much as possible in the second and so on, as long the regex matches.

		When looking at the implementation for minimum we can see that the
		only difference is that we are capturing and keeping the second and
		third occurrences of $\texttt{x}^*$:

		% sedcode maximum:
		\begin{Verbatim}
			s/\(x*\)\(x*\)|\1\(x*\)/\1\2\3/
		\end{Verbatim}

		So, since
		\begin{itemize}
			\item \verb|\1| captures $\min\{n,m\}$ number of \verb|x|s,
			\item \verb|\2| captures $n-\min\{n,m\}$ number of \verb|x|s, and
			\item \verb|\3| captures $m-\min\{n,m\}$ number of \verb|x|s;
		\end{itemize}
		then for \verb|\1\2\3|: $n+m-\min\{n,m\}=\max\{n,m\}$.

		Another possibility, though way less elegant, is to abuse the fact
		that we can assume the greedy behavior for the alternative operator
		``\verb!\|!'' as well.  If $n<=m$ then \verb|\1\3| will match $n +
		(m-n)=m$, and if $n>m$, then the second alternative will be selected
		leaving \verb|\3| empty:
		\begin{Verbatim}
			s/\(x*\)|\(\1\(x*\)\|x*\)/\1\3/
		\end{Verbatim}

	\item[Fibonacci: $\texttt{x}^n\mapsto \texttt{x}^{\fib n}$:]

		Fibonacci is usually defined recursively by $\fib n = \fib (n-1) +
		\fib (n-2)$ with the base case $\fib n = n$ when $n<2$.  So in the
		implementation presented here we first handle the base case first by
		not adding pipes (which disables all substitutions on the following
		lines).

		The recursive case is handling by having our pattern space with the
		format $\texttt{x}^{n-i}\texttt{|x}^{\fib i-1}\texttt{|x}^{\fib i}$,
		and applying the inductive substitution $(a,b)\mapsto (b,a+b)$
		which increases by 1 the value at $i$:
		$\texttt{x}^{n-i}\texttt{|x}^{\fib i-1}\texttt{|x}^{\fib i}$
		$\mapsto$
		$\texttt{x}^{n-i-1}\texttt{|x}^{\fib i}\texttt{|x}^{\fib i+1}$.

		When $i$ reaches $n$, that is when there are no more {\tt x}s before
		the first pipe, we will have $\texttt{|x}^{\fib n-1}\texttt{|x}^{\fib n}$,
		so at that point we just leave the text after the last pipe:

		% sedcode fibonacci:
		\begin{Verbatim}
			/xx/ s/x$/||x/
			:a; s/x|\(x*\)|\(x*\)/|\2|\1\2/; ta
			s/.*|//
		\end{Verbatim}

\end{description}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Binary Math}

\begin{description}
	\item[XOR: $a_n\ldots a_1 a_0\texttt{|}b_m\ldots b_1 b_0 \mapsto
		c_{\max\{n, m\}}\ldots c_1 c_0$ where $(c_i)=(a_i)\oplus(b_i)$:]$\,$

		Bit-wise XOR between $(a_i)$ and $(b_i)$, assuming as many leading zeros
		as needed.

		% sedcode bitwise-xor:
		\begin{Verbatim}
			s/$/|/
			:a; s/^|\(.*\)|/\1/; s/||//
			  s/0|\(.*\)\(.\)|/|\1|\2/
			  s/1|\(.*\)0|/|\1|1/
			  s/1|\(.*\)1|/|\1|0/; ta
			s/^0*//; s/^$/0/
		\end{Verbatim}

		\begin{enumerate}
			\item We start by adding a pipe to the end.
				We will store the result after that pipe.
			\item At the start of each iteration denoted by the label \verb|:a| we
				detect the cases where there are no more binary digits left in any
				of the operands.

				In the following 3 lines we will be processing
				one bit at a time starting from the right (least significant bit).
			\item The rule that we apply here $0\oplus x = x$.  We just maintain the
				least significant bit of the second number.
			\item Then we consider $1\oplus 0=1$.
			\item And the other case $1\oplus 1=0$.
			\item Finally we just remove leading zeroes and place a zero in place of
				an empty string.
		\end{enumerate}
\end{description}

\todo{Write about binary addition.}

\todo{Write about truncation.}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Lookup Tables}

Many of the most complex programs written in \sed* use the power of
backreferences in order to do translations which otherwise could be very
complex, here we will focus on two very practical use cases, converting from
decimal numbers to unary and viceversa.

\subsubsection{Decimal to Unary}

Let's say we want to convert the digits of the number {\tt 42} to unary:
{\tt |xxxx|xx}, we can use the following short program composed of just three
lines:

\begin{Verbatim}
	s/./|0123456789&!/g
	s/\(.\)[0-9]*\1//g
	:a; s/|!/|/g; s/.!/!x/g; ta
\end{Verbatim}

\begin{enumerate}
	\item with the first substitution we would obtain the seemingly useless
		string: {\tt |01234567894!|01234567892!}, yet there is an interesting
		pattern going on.
	\item If for each block we were to remove the duplicated numbers and
		everything in between, we would obtain {\tt |0123!|01!}, and that's
		what the second line does.

		Note that the number of digits left matches the corresponding digit
		that we had before.  That's because we inserted all the digits in order.

	\item Finally to obtain the {\tt x}s, we can iterate the exclamation marks
		to the left substituting the digits by {\tt x}s until they reach the
		pipes (when we remove them).
\end{enumerate}

\subsubsection{Unary to Decimal}
\todo{Copiar desde la cuadernola la explicaci\'on de las reverse lookup tables.}

\subsubsection{Generic Substitutions}
\todo{Escribir c\'omo se podr\'{\i}a usar una lookup table en general para
hacer una substitución arbitraria.}
% the example usually seen for "sed lookup tables" is that of substituting
% numbers via 0zero1one2two3three4four5five6six7seven8eight9nine, there has
% to be another simple example.
%   Maybe converting from hexa to binary requiring only two commands.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Debugging}

When developing programs in \sed*, more often than not we end up with mountains
of completely unreadable line noise which at a certain point, sometimes even
way before to be completed, leaves us in a state of absolute confusion.  Is at
this point that we really need to understand what's going on.

One of the simplest ways to try to understand the code, is to print the content
of the pattern space, in the most simple form we just place the {\tt p} command
whenever we want to, however if we want to print it at several places and
specially if there are newlines inside the pattern space, we might want to use
something like this instead:

\begin{Verbatim}
	... lots of code ...
	s/.*/At xyz: <<&>>/p;  s/[^<]*<<//;  s/>>$//
	... lots of code ...
\end{Verbatim}

Note that by using the previous code snippet we ``set to true'' the state of
whether a substitution was applied at that point.  If we don't care about its
state then we might just use that code, if not we have to maintain it by
basically duplicating our code around an ``if then else'':

\begin{Verbatim}
	... lots of code ...
	tTxyz; s/.*/At xyz (F): <<&>>/p; s/[^<]*<<//; s/>>$//; tFxyz
	:Txyz; s/.*/At xyz (T): <<&>>/p; s/[^<]*<<//; s/>>$//; :Fxyz
	... lots of code ...
\end{Verbatim}

This works because if the flag was true, it will jump to the {\tt (T)} part,
with these new substitutions restoring the flag to true.
Oterwise the conditional jump to {\tt Fxyz} will reset it to false
before continuing.

If the hold space has to be printed as well, then one simple solution is
logging the pattern space, followed by the {\tt x} command, that logging
the hold space and finally switching back the spaces with the {\tt x}
command once again.

\todo{add more tools}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Jump Tables}

\todo{complete jump tables}


\documentclass[a4paper, 11pt]{article}
\usepackage{fullpage, amssymb, amsmath, graphicx}

\newcommand{\mytitle}{Cholesky Decomposition of 2D Kernel}

\begin{document}
\noindent
\large\textbf{\mytitle} \\ \\ Tyler Gordon \\
\normalsize \today
\ \ \hrulefill
\section{}
In the 1D case, the cholesky decomposition algorithm for a covariance kernel represented by
a semiseparable matrix with the form
\begin{equation}
K = A + \text{tril}(UV^T) + \text{triu}(VU^T)
\end{equation}
where U and V are both N x J matrices and A is an N x N diagonal matrix is given in in section
5.1 of Foreman-Mackey et al, 2017 (FM17).
In the 2D case, the covariance matrix is given by
\begin{equation}
K = A' + \text{tril}(U'V'^T) + \text{triu}(V'U'^T)
\end{equation}
where $U' = U\otimes Q$, $V' = V\otimes I_M$, and $A' = (A - \sigma_n) \otimes Q + \sigma_{nM+i}$
where $Q_{i,j}$ specifies the covariance between
coordinates $x_i$ and $x_j$ in the second dimension, and $I_M$ is the identity matrix with $M$ corresponding
to the size of $Q$. Because $U'$ and $V'$ for $U$ and $V$ does not alter the semiseperable property of the
matrices, the decomposition can proceed as in the 1D case with the following alterations to equations 43-45 of
FM17:
\begin{align}
\tilde{U}_{(n-1)M+p, (2j-1)M+q} = a_j\cos(d_jt_n) + b_j\sin(d_jt_n)Q_{p,q} \\
\tilde{U}_{(n-1)M+p, (2j)M+q} = a_j\cos(d_jt_n) + b_j\sin(d_jt_n)Q_{p,q} \\
\tilde{V}_{(n-1)M+p, (2j-1)M+q} = a_j\sin(d_jt_n) - b_j\cos(d_jt_n)\delta_{p, q} \\
\tilde{V}_{(n-1)M+p, (2j)M+q} = a_j\sin(d_jt_n) - b_j\cos(d_jt_n)\delta_{p, q} \\
A_{(n-1)M+p, (n-1)M+p} = \sigma_{(n-1)M+p} + Q_{p, p}\sum_{j=1}^J a_j
\end{align}
where $p, q = 1, 2...M$.
\begin{equation}
	\phi_{(n-1)M+p, (2j-1)M+q} = \phi_{(n-1)M+p, (2j)M+q} =
	 \begin{cases}
		e^{-c_j(t_n-t_{n-1})} & p = 1 \\
		1 & p > 1
	\end{cases}
\end{equation}
Still with the constraint that
\begin{equation}
	\phi_{1, (2j-1)M+q} = \phi_{1, (2j)M+q} = 0
\end{equation}
The algorithm for computing the cholesky decomposition is unaltered from equation 46 of FM17.

\end{document}


\section{Convolutional Neural Networks}

In the field of machine learning, especially in the deep learning sector, a CNN is a deep learning algorithm. It can take an input image and reproduce an identification of the object with a certain probability. The special feature of a CNN is that it is able to learn filters of different types (e.g. horizontal lines, vertical lines, etc.). A convolutional neural network has a characteristic structure in terms of its layers. It is structured in convolutional, pooling, and fully connected layers. The arrangement of alternating convolutional and pooling layers allows a more accurate and complex analysis of the image. The first layers focus on shapes and colors, while later layers contribute to the identification of more complex details for the recognition of the overall image.

\subsection{Convolutional Layer}
The convolutional layer is the key component of a CNN. It contains a certain set of filters, also called kernels. The parameters of the filter are learned over the course of the training. The filter interacts with the image and convolves it. From this convolution, an activation map is created which is calculated from the dot product between each element of the filter and the input. The weights in the filter are maintained as the filter moves across the image. However, these weights adjust during backpropagation and the associated gradient descent to achieve the most accurate results.

\begin{figure}[htb]
    \centering
    \includegraphics[width=7cm]{images/conv_layer.jpg}
    \caption{Convolutional Layer}
    \label{fig:convLayer}
\end{figure}

\subsection{Pooling Layer}
Since a great increase in dimension occurs through the use of a convolutional layer, a dimension reduction is required in the next step to reduce the number of parameters of a CNN. This is achieved by the pooling layer. It has the advantage that the computational cost decreases drastically. Also, unnecessary details are omitted, which is helpful for the later image identification.The most commonly used pooling methods are maximum and average pooling. In figure \ref{fig:poolingLayer}, you can see how a maximum pooling is performed, leading to a dimension reduction.

\begin{figure}[htb]
    \centering
    \includegraphics[width=7cm]{images/maxpooling.jpg}
    \caption{Max Pooling Layer}
    \label{fig:poolingLayer}
\end{figure}

\subsection{Fully-Connected Layer}
Since the individual pixel values of the input image are not directly connected to the output layer, a fully-connected layer is required which is directly connected to the output layer. The layer does the classification using the collected features from the previous layers. At the end, a softmax or Sigmoid function (dependent on the problem) is applied, which outputs a classification using probabilities between 0 and 1.

\begin{figure}[htb]
    \centering
    \includegraphics[width=10cm]{images/cnn_turtle_pooling.png}
    \caption{Turtle Image using CNN}
    \label{fig:turtleCNN}
\end{figure}


\chapter{Massive Spin-1 Particles}
\section{Polarisations}
In chapter 12, we saw that for massless spin-1 particles, the equations of motion (Maxwell's equations) can be expressed in covariant form by \eqref{eq:MaxwellCovariant}:
\begin{equation*}
j^\mu = \partial^2 A^\mu - \partial^\mu \partial_\nu A^\nu.
\end{equation*}
The term $\partial^2 A^\mu$ may be expanded as $(\pdv[2]{t} - \nabla^2)A^\mu = (-E^2 + p^2)A^\mu$. Now we note that for on-shell massless particles, the combination $-E^2+p^2=0$. Therefore, we replace this combination with the analogous term for massive particles, $(-E^2+p^2+m^2)A^\mu = \partial^2A^\mu + m^2A^\mu$,
\begin{equation}
\partial^2A^\mu + m^2A^\mu - \partial^\mu\partial_\nu A^\nu = j^\mu \label{eq:proca}
\end{equation}
where $j^\mu$ is zero for a free massive spin-1 particle and non-zero when it is a virtual propagator. This is the Proca equation.

Differentiating with $\partial_\mu$,
\begin{equation}
\partial_\mu \partial^\nu \partial_\nu A^\mu + m^2 \partial_\mu A^\mu - \partial_\mu \partial^\mu \partial_\nu A^\nu = \partial_\mu j^\mu = 0
\end{equation}
where the final equality $\partial_\mu j^\mu = 0$ follows from continuity of the 4-current. Now the first and third terms are equal, as can be seen from index contraction (or swap $\mu \leftrightarrow \nu$ and commute $\partial_\mu$ and $\partial_\nu$ in one term), so we identify
\begin{equation}
m^2 \partial_\mu A^\mu = 0
\end{equation}
which is nothing but the Lorenz gauge condition \eqref{eq:LorentzGauge} for $m^2 \neq 0$. Therefore, massless spin-1 fields automatically satisfy the Lorenz gauge and we cannot `gauge away' any degrees of freedom as we did in the massless case.

The field may be expressed in its polarisation states as
\begin{equation}
A^\mu = \epsilon^\mu_i e^{-ipx}
\end{equation}
with possible spatial polarisations
\begin{align*}
\epsilon_1 = \mqty(0\\1\\0\\0)\,, \quad  \epsilon_2 = \mqty(0\\0\\1\\0)\,, \quad \epsilon_3 = \mqty(0\\0\\0\\1).
\end{align*}

Given the field now has some mass, we are free to consider its rest frame where $p^\mu = \mqty(m,\,0,\,0,\,0)^T$. Therefore the condition $p_\mu \epsilon_i^\mu = 0$ in the rest frame.

\subsection{Transformation of the polarisation vector}
Now consider a particle with momentum $p$ along the $z$-axis, without loss of generality, or equivalently a boost of $-p$ from the rest frame. The 4-momentum is $p^\mu = \mqty(E,\,0,\,0,\,p)^T$. It is clear that polarisations $\epsilon_1$ and $\epsilon_2$ will remain the same since the field is perpendicular to the boost. To determine how $\epsilon_3$ transforms we use the Lorenz gauge condition,
\begin{align}
0=\partial_\mu A^\mu &= \partial_\mu \left( \epsilon_3^\mu e^{-ipx} \right)\nonumber \\
&= \epsilon_3^\mu (-ip_\mu) e^{-ipx} = 0 \nonumber \\
\Rightarrow \quad p_\mu \epsilon_3^\mu
\end{align}
Now the covariant (index-down) form of the 4-momentum is $p_\mu = (E,\,0,\,0,\,-p)$, so the above equation is satisfied by
\begin{equation}
\epsilon_3^\mu(p) = \frac{1}{m}\mqty(p\\0\\0\\E) = \frac{1}{m}\mqty(p\\0\\0\\\sqrt{p^2+m^2})
\end{equation}
where the $1/m$ factor normalises the polarisation vector for free (on-shell) vector bosons.

\section{Completeness relation}
For free massive vector bosons the completeness relation is given by the sum of outer products,
\begin{align}
\sum_i \epsilon_i \epsilon_i^\dagger &= \mqty(0\\1\\0\\0)\mqty(0,\,1,\,0,\,0) + \mqty(0\\0\\1\\0)\mqty(0,\,0,\,1,\,0) + \frac{1}{m^2}\mqty(p\\0\\0\\E)\mqty(p,\,0,\,0,\,E) \\
&= \mqty(\dmat[0]{\frac{p^2}{m^2},1,1,\frac{E^2}{m^2}}) \\
&= -g^{\mu\nu} + \frac{p^\mu p^\nu}{m^2}.
\end{align}
Verifying the last equality,
\begin{equation}
-g^{00} + \frac{p^0 p^0}{m^2} = -1 + \frac{E^2}{m^2} = \frac{-m^2+E^2}{m^2} = \frac{p^2}{m^2}
\end{equation}
and
\begin{equation}
-g^{33} + \frac{p^3 p^3}{m^2} = 1 + \frac{p^2}{m^2} = \frac{m^2+p^2}{m^2} = \frac{E^2}{m^2}.
\end{equation}

\section{Virtual vector bosons}
\subsection{Polarisations}
For virtual massive vector bosons there is no longer the constraint that $E^2-p^2=m^2$, so a timelike polarisation is ostensibly allowed. However, imposing the Lorenz gauge condition removes this degree of freedom, just as in the massless case. Now the virtual 4-momentum is $q^\mu = \mqty(\nu,\vec{q})^T$. Therefore,
\begin{equation}
-Q^2 = q_\mu q^\mu = \nu^2 - \vec{q}\cdot\vec{q},
\end{equation}
where $Q^2-m^2$ is the \emph{virtuality} of the particle. So when travelling along the $z$-axis, $q_z=\sqrt{\nu^2+Q^2}$, giving
\begin{equation}
q^\mu(\nu) = \mqty(\nu\\0\\0\\\sqrt{\nu^2+Q^2}).
\end{equation}
Therefore, following the procedure above, the polarisation states of a virtual vector boson are
\begin{equation}
\epsilon_1 = \mqty(0\\1\\0\\0)\,, \quad  \epsilon_2 = \mqty(0\\0\\1\\0)\,, \quad \epsilon_3 = \frac{1}{Q^2}\mqty(\sqrt{\nu^2+Q^2}\\0\\0\\\nu).
\end{equation}

\subsection{Propagator}
For a massive spin-1 field, we had the Proca equation \eqref{eq:proca},
\begin{equation*}
\partial^2A^\mu + m^2A^\mu - \partial^\mu\partial_\nu A^\nu = j^\mu
\end{equation*}
and from differentiating,
\begin{equation}
m^2 \partial_\mu A^\mu = \partial_\mu j^\mu.
\end{equation}
Substituting into the Proca equation,
\begin{align}
(\partial^2 + m^2)A^\mu &- \frac{1}{m^2}\partial^\mu \partial_\nu j^\nu = j^\mu \nonumber \\
\Rightarrow \quad (\partial^2 + m^2)A^\mu &= \frac{1}{m^2}\partial^\mu \partial_\nu j^\nu + j^\mu \nonumber \\
&= \frac{1}{m^2}\partial^\mu \partial^\nu j_\nu + g^{\mu\nu}j_\nu \nonumber \\
&= \left( g^{\mu\nu} + \frac{1}{m^2} \partial^\mu \partial^\nu \right)j_\nu.
\end{align}
Now using that $q^\mu = i\partial^\mu$, $\partial^\mu \partial^\nu = -q^\mu q^\nu$,
\begin{equation}
(\partial^2 + m^2)A^\mu = \left( g^{\mu\nu} - \frac{q^\mu q^\nu}{m^2} \right)j_\nu
\end{equation}
from which we identify the virtual vector boson propagator
\begin{equation}\boxed{
\mathcal{G}(q^\mu) = \frac{i}{-q^2+m^2} \left( g^{\mu\nu} - \frac{q^\mu q^\nu}{m^2} \right)
}.
\end{equation}

\subsubsection{Decay}
In an s-channel process, the propagating particle is seen to decay with a characteristic lifetime
\begin{equation}
\Gamma = \frac{4\pi}{m^2} \frac{1}{32\pi^2} \abs{T_{fi}^2} p_f.
\end{equation}
We therefore modify the wavefunction such that $\psi^\dagger \psi \sim e^{-\Gamma t}$,
\begin{equation}
\psi \sim e^{-imt -\frac{\Gamma}{2}t}.
\end{equation}
By analogy, we replace $im \rightarrow im + \frac{\Gamma}{2}$ (equivalently $m \rightarrow m - \frac{i\Gamma}{2}$) such that $m^2 \rightarrow (m - \frac{i\Gamma}{2}) \approx m^2 - i\gamma m$ where the approximation holds for $\Gamma \ll m$ (true for real-world examples),
\begin{equation}
\mathcal{G}_\text{decay}(q^\mu) = \frac{i}{-q^2+m^2 - i\Gamma m} \left( g^{\mu\nu} - \frac{q^\mu q^\nu}{m^2 - i\Gamma m} \right).
\end{equation}


\section{Graph Creation}

\subsection{Node Pruning}

We remove all nodes from the graph corresponding to labels with fewer than $t_{seg}$ voxels (Section 3.1).
Figure \ref{fig:node-pruning} shows the results of varying $t_{seg}$ on two different quantities for the Kasthuri training volume.
The blue line indicates the number of nodes remaining in the graph.
The rate of node reduction decreases for larger thresholds since there are fewer labels of larger size.
The green line shows the percent of voxels with a label pruned from the graph.
Ideally this number is low since we want to remove small segments which do not contribute much to the overall volume.
This ``volume lost" grows at an increasing rate as the larger segments are removed.
Based on these curves we set $t_{seg} = 20,000$ voxels.
With this threshold, we prune over half of the labels and only lose $1.5\%$ of the total volume.

\subsection{Edge Pruning}

There are two parameters in the two-pass edge pruning algorithm, $t_{low}$ and $t_{high}$ (Section 3.2).
The resulting graph should have a high percentage of the original split errors still present while keeping the number of total edges low.
We perform a search over several thresholds from $40\textrm{nm} \leq t_{low} \leq 300 \textrm{nm}$ and $300 \textrm{nm} \leq t_{high} \leq 800 \textrm{nm}$ on the Kasthuri training dataset to find the parameter with the highest retention of split errors while keeping the total number of edges less than $6 \times$ the number of split errors.

\begin{figure}
	\begin{center}
		\includegraphics[width=0.95\linewidth]{./figures/node-threshold.png}
		\caption{The number of remaining nodes after increasing the threshold (blue) and the number of voxels with this label as a percent of the total volume (green). }
		\label{fig:node-pruning}
	\end{center}
\end{figure}


% !TEX root = ../main.tex

\newpage
\appendix
\section{Appendix} \label{sec:Appendix}

\subsection{Transformation to the QIF model} \label{app:TransformationToQIF}
We prove that the transformation \eqref{eq:QIFtransformation} holds from the \QIF model \eqref{eq:QIFmodel} to the Theta model \eqref{eq:thetaneuron}.
\begin{align*}
V &\equiv \tan \left( \frac{\theta}{2} \right) \quad \longrightarrow \quad
\frac{\mathop{d V}}{\mathop{d t}} = \frac{1}{2 \cos ^{2}\left(\frac{\theta}{2}\right)} \frac{d \theta}{ \mathop{d t}}
\end{align*}
Insert into $\frac{\mathop{d V}}{\mathop{d t}}= V^2 + I$:
\begin{align*}
\frac{\mathop{d \theta}}{\mathop{d t}} &= 2\left(\cos ^{2}\left(\frac{\theta}{2}\right) \cdot \tan ^{2}\left(\frac{\theta}{2}\right)+\cos ^{2}\left(\frac{\theta}{2}\right) \cdot I \right) = 2\left(\sin ^{2}\left(\frac{\theta}{2}\right)+\cos ^{2}\left(\frac{\theta}{2}\right) \cdot I \right)
\end{align*}
Using $\cos ^{2}\left(\frac{\theta}{2}\right) = \frac{1+\cos \left(\frac{\theta}{2}\right)}{2}$ and $\sin ^{2}\left(\frac{\theta}{2}\right)=\frac{1-\cos \left(\frac{\theta}{2}\right)}{2}$:
\begin{align*}
\dot{\theta} &=2\left(\frac{1-\cos \theta}{2}+\left(\frac{1+\cos \theta}{2}\right) \cdot I \right) =(1-\cos \theta)+(1+\cos \theta) \cdot I
\end{align*}
This proves that the transformation \eqref{eq:QIFtransformation} is correct.

\subsection{Solutions to the QIF model} \label{app:ThetaModelSolutions}
Depending on the value of $I$, we can distinguish multiple solutions  \cite{Perez2020}. In all cases we can integrate through the separation of variables. Solutions are bound to start at $V(t_0)$, right after a spike has occured at $t=t_0$.

\subsubsection{Solving for \texorpdfstring{$I < 0$}{TEXT}}
%Change variables: $I = -\tilde{I}^2$. Separate variables as $\frac{\mathop{dv}}{v^2 - \tilde{I}^2} = \mathop{dt}$ and integrate from $t = 0$:
\begin{align*}
\int_{V(t_0)}^{V(t)} \frac{\mathop{dv}}{v^2 - \tilde{I}^2} &= \int_{V(t_0)}^{V(t)} \frac{\mathop{dv}}{(v+\tilde{I})(v-\tilde{I})}
= \frac{1}{2 \tilde{I}} \int_{V(t_0)}^{V(t)} \frac{\mathop{dv}}{v-\tilde{I}}-\frac{1}{2 \tilde{I}} \int_{V(t_0)}^{V(t)} \frac{\mathop{dv}}{v+\tilde{I}} \\
%&= \frac{1}{2 \tilde{I}} \log (v-\tilde{I}) -\frac{1}{2 \tilde{I}} \log (v+\tilde{I})\Big \rvert_{V(t_0)}^{V(t)}
&= \frac{1}{2 \tilde{I}} \log \left(1-\frac{2 \tilde{I}}{v+\tilde{I}}\right) \Big \rvert_{V(t_0)}^{V(t)}
= \int_{t_0}^t \mathop{d\tau} = t - t_0 \\
V(t) &= \lim_{V(t_0) \rightarrow -\infty} \frac{2 \sqrt{-I}}{1 - \left(1-\frac{2 \sqrt{-I}}{V(t_0)+\sqrt{-I}}\right)\cdot e^{2 (t - t_0)\sqrt{-I}}}-\sqrt{-I}\\
&= \frac{2 \sqrt{-I}}{1 - e^{2 (t - t_0) \sqrt{-I}}}-\sqrt{-I}
\end{align*}

\subsubsection{Solving for \texorpdfstring{$I = 0$}{TEXT}}
\begin{align*}
\int_{V(t_0)}^{V(t)} \frac{\mathop{dv}}{v^2} &= \frac{1}{v}\Big\rvert_{V(t_0)}^{V(t)} = - \frac{1}{V(t)} + \frac{1}{V(t_0)} = \int_{t_0}^t \mathop{d\tau} = t -t_0 \\
V(t) &= \lim_{V(t_0) \rightarrow - \infty} \frac{V(t_0)}{1-V(t_0)(t - t_0)} \underset{\frac{\infty}{\infty}}{\overset{\mathrm{H}}{=}} \frac{-1}{t - t_0}
\end{align*}

\subsubsection{Solving for \texorpdfstring{$I > 0$}{TEXT}}
%Separate the variables as $\frac{\mathop{dv}}{v^2 + I} = \mathop{dt}$ and integrate from $t=0$:
\begin{align*}
\int_{V(t_0)}^{V(t)} \frac{\mathop{dv}}{v^2 + I} &= \int_{V(t_0)}^{V(t)} \frac{I}{\left(\frac{v}{\sqrt{I}}\right)^2 + 1} \mathop{dv}
\overset{x = \frac{v}{\sqrt{I}}}{\underset{\mathop{dx} = \frac{dv}{\sqrt{I}}}{=}}
%\quad \longrightarrow \quad x = \frac{v}{\sqrt{I}} \quad \mathop{dx} = \frac{\mathop{dv}}{\sqrt{I}} \\
\int_{\frac{V(t_0)}{\sqrt{I}}}^{\frac{V(t)}{\sqrt{I}}} \frac{I}{x^2 + 1} \mathop{dx}= \frac{1}{\sqrt{I}} \arctan(x) \Big \rvert_{\frac{V(t_0)}{\sqrt{I}}}^{\frac{V(t)}{\sqrt{I}}} \\
&= \frac{1}{\sqrt{I}} \left( \arctan \left( \frac{V(t)}{\sqrt{I}} \right) - \arctan \left( \frac{V(t_0)}{\sqrt{I}} \right) \right) =
\int_{t_0}^t \mathop{d\tau} = t - t_0 \\
V(t) &= \lim_{V(t_0) \rightarrow -\infty} \sqrt{I} \cdot \tan \left( (t - t_0) \sqrt{I} + \arctan \left( \frac{V(t_0)}{\sqrt{I}} \right) \right) = \sqrt{I} \cdot \tan \left( (t - t_0) \sqrt{I} - \frac{\pi}{2} \right) \\
&=  -\sqrt{I} \cdot \cot \left( (t - t_0) \sqrt{I} \right)
\end{align*}

\subsection{Frequency response of the Theta model} \label{app:ThetaModelFrequencyResponse}
What is the relation between spiking period and input current? The integral is solved like before, but now with the conditions of the spike:
\begin{align*}
T - t_0&= \lim_{a \rightarrow \infty} \int_{-a}^{a} \frac{I}{\left(\frac{v}{\sqrt{I}}\right)^2 + 1} \mathop{dv}
\overset{x = \frac{v}{\sqrt{I}}}{\underset{\mathop{dx} = \frac{dv}{\sqrt{I}}}{=}}
\lim_{a \rightarrow \infty} \int_{\frac{-a}{\sqrt{I}}}^{\frac{a}{\sqrt{I}}} \frac{I}{x^2 + 1} \mathop{dx}
= \lim_{a \rightarrow \infty} \frac{1}{\sqrt{I}} \arctan(x) \Big \rvert_{\frac{-a}{\sqrt{I}}}^{\frac{a}{\sqrt{I}}} \\
&= \frac{1}{\sqrt{I}} \left( \frac{\pi}{2} - \left( - \frac{\pi}{2} \right) \right)
= \frac{\pi}{\sqrt{I}}
\end{align*}
So the spiking frequency is proportional to $\sqrt{I}$.

\subsection{Phase response of the Theta model} \label{app:ThetaModelPhaseResponse}
Here we will find an exact expression for the \PRC. Starting from the analysis in \ref{app:ThetaModelFrequencyResponse}, we first look for an expression for $T_{\phi}$. We can resume our normal integration at $\phi$ with $V \left( \phi \right) + \varepsilon$:
\begin{align*}
T_{\phi}=\frac{1}{\sqrt{I}}\left(\frac{\pi}{2}-\arctan \left(\frac{V(\phi)+\varepsilon}{\sqrt{I}}\right)\right)+\phi
\end{align*}
As $V(\phi)$ is given by \eqref{eq:ThetaNeuronModelSolutionPeriodic}, we can find:
\begin{align*}
T_{\phi} = \frac{1}{\sqrt{I}}\left(\frac{\pi}{2} - \arctan \left(\frac{\varepsilon}{\sqrt{I}} - \cot \left(\phi\sqrt{I})\right)\right)\right)+\phi
\end{align*}
The \PRC is then simply:
\begin{align*}
\PRC(\phi, \varepsilon) = \frac{1}{\sqrt{I}}\left(\frac{\pi}{2} + \arctan \left(\frac{\varepsilon}{\sqrt{I}} - \cot \left(\phi \sqrt{I})\right)\right)\right) - \phi
\end{align*}

\subsection{Newton-Raphson root iteration} \label{app:NewtonRaphson}
We define the equilibria $\boldsymbol{x^\ast} \in \R^n$ of a multivariate function $\boldsymbol{f}(\boldsymbol{x}) : \R^n \rightarrow \R^n$ with $\boldsymbol{f}(\boldsymbol{x}) = \boldsymbol{0}$. Expanding $\boldsymbol{f}$ as a Taylor series, we obtain:
\begin{align*}
f_i(\boldsymbol{x} + \delta \boldsymbol{x}) =f_{i}(\boldsymbol{x}) + \sum_{j=1}^{n} \frac{\partial f_{i}(\boldsymbol{x})}{\partial x_{j}} \delta x_{j}+O\left(\delta \boldsymbol{x}^{2}\right) \approx f_{i}(\boldsymbol{x})+\sum_{j=1}^{n} \frac{\partial f_{i}(\boldsymbol{x})}{\partial x_{j}} \delta x_{j}, \qquad (i=1, \cdots, n)
\end{align*}
We can also write this in vector notation, by setting $\boldsymbol{J}(\boldsymbol{x}) = \nabla \boldsymbol{f}(\boldsymbol{x}) = \frac{d}{d\boldsymbol{x}} \boldsymbol{f}(\boldsymbol{x}) \in \R^{n \times n}$
\begin{align*}
\boldsymbol{f}(\boldsymbol{x}+\delta \boldsymbol{x}) &\approx\left[\begin{array}{c}f_{1}(\boldsymbol{x}) \\ \vdots \\ f_{N}(\boldsymbol{x})\end{array}\right]
+ \left[\begin{array}{ccc}\frac{\partial f_{1}}{\partial x_{1}} & \cdots & \frac{\partial f_{1}}{\partial x_{N}} \\ \vdots & \ddots & \vdots \\ \frac{\partial f_{N}}{\partial x_{1}} & \cdots & \frac{\partial f_{N}}{\partial x_{N}}\end{array}\right]
\left[\begin{array}{c}\delta x_{1} \\ \vdots \\ \delta x_{N}\end{array}\right]
=\boldsymbol{f}(\boldsymbol{x})+\boldsymbol{J}(\boldsymbol{x}) \delta \boldsymbol{x}
\end{align*}
By assuming $\boldsymbol{f}(\boldsymbol{x}+\delta \boldsymbol{x}) = 0$ we can find that $\delta \boldsymbol{x} = -\boldsymbol{J}^{-1}( \boldsymbol{x}) \boldsymbol{f}(\boldsymbol{x})$ so that $\boldsymbol{x} + \delta \boldsymbol{x} =  \boldsymbol{x} - \boldsymbol{J}^{-1} (\boldsymbol{x}) \boldsymbol{f}(\boldsymbol{x})$. This expression converges to $\boldsymbol{x^\ast}$. When the equations are nonlinear, the equations converge to the real root as $\boldsymbol{x}_k =  \boldsymbol{x}_k - \boldsymbol{J}^{-1} ( \boldsymbol{x}_k)\boldsymbol{f}(\boldsymbol{x}_k)$.

\subsection{Jacobian of the Ott-Antonsen manifold}
Starting from \eqref{eq:OttAntonsenSystemFull}, we separate the real and imaginary parts of $z(\k, t)$ in $x_{\k} = x(\k, t)$ and $y_{\k} = y(\k, t)$:
\begin{align*}
\frac{\partial z_{\k}}{\partial t} &= -\frac{\ic}{2} \cdot \left( x_{\k}^2 + \ic 2x_{\k}y_{\k} - y_{\k}^2 - 2x_{\k} - \ic 2y_{\k} +1\right) + \frac{1}{2} \cdot \left( x_{\k}^2 + \ic 2x_{\k}y_{\k} - y_{\k}^2 + 2x_{\k} + \ic 2y_{\k} +1 \right) \cdot I_{\k}\\
I_{\k} &= -\sigma_{\k} + \ic \eta_{0, \k} + \ic H_{2_{\k}} \\
H_{2_{\k}} &= \frac{\kappa}{\kmean} \sum_{\kacc \in \K} P\left(\kacc\right) \: a\left(\kacc \rightarrow \k\right) \cdot \left( 1 + \frac{x_{\k}^2}{3} - \frac{4}{3} x_{\k} \right)
\end{align*}
Taking the dynamics per real and imaginary value yields:
\begin{align*}
\frac{\partial x_{\k}}{\partial t}
&=f_{\k} \left(x_{\k}, y_{\k} \right) \\
&=(x_{\k}-1) y_{\k}-\frac{(x_{\k}+1)^{2}-y_{\k}^{2}}{2} \sigma_{\k} + (x_{\k}+1) y_{\k} \left[\eta_{0}+ H_{2_{\k}}\right] \\
\frac{\partial x_{\k}}{\partial t}
&= g_{\k} \left(y_{\k}, y_{\k} \right) \\
&=-\frac{(x_{\k}-1)^{2}-y_{\k}^{2}}{2}-(x_{\k}+1) y_{\k} \sigma_{\k} + \frac{(x_{\k}+1)^{2}-y_{\k}^{2}}{2} \cdot \left[\eta_{0} + H_{2_{\k}} \right]
\end{align*}
And the Jacobian is found from the partial derivatives of $f$ and $g$:
\begin{align*}
\frac{\partial f_{\k}}{\partial x_{\k}} &= y_{\k}-(x_{\k}+1) \sigma_{\k}-y_{\k} \left[\eta_{0} + \kappa \cdot H_{2_{\k}} \right] + (x_{\k}+1) y_{\k} \frac{\partial H_{2_{\k}}}{\partial x_{\k}}\\
\frac{\partial f_{\k}}{\partial y_{\k}} &= (x_{\k}-1)+y_{\k} \sigma_{\k} + (x_{\k}+1) \left[\eta_{0} + H_{2_{\k}} \right] \\
\frac{\partial g_{\k}}{\partial x_{\k}} &= -(x_{\k} - 1) - y_{\k}  \sigma_{\k} + (x_{\k} + 1) \left[\eta_{0} + H_{2_{\k}} \right] + \left( \frac{(x_{\k} + 1)^2 - y_{\k}^2}{2} \right) \frac{\partial H_{2_{\k}}}{\partial x_{\k}} \\
\frac{\partial g_{\k}}{\partial y_{\k}} &= y_{\k} - (x_{\k} + 1) \sigma_{\k} - y_{\k} \left[\eta_{0} + H_{2_{\k}} \right] \\
\frac{\partial H_{2_{\k}}}{\partial x_{\k}} &= \frac{\kappa}{\langle k\rangle} P\left(\k\right) \: a\left(\k \rightarrow \k\right) (x_{\k}-2) \frac{2}{3} \\
\frac{\partial H_{2_{\k}}}{\partial y_{\k}} &= 0
\end{align*}

And the off-diagonal elements, the nodes represented by degree $\kacc$:
\begin{align*}
\frac{\partial f_{\k}}{\partial x_{\kacc}} &= (x_{\k}+1) y_{\k} \frac{\partial H_{2_{\k}}}{\partial x_{\kacc}}\\
\frac{\partial f_{\k}}{\partial y_{\kacc}} &= 0 \\
\frac{\partial g_{\k}}{\partial x_{\kacc}} &= \left( \frac{(x_{\k} + 1)^2 - y_{\k}^2}{2} \right) \frac{\partial H_{2_{\k}}}{\partial x_{\kacc}} \\
\frac{\partial g_{\k}}{\partial y_{\kacc}} &= 0\\
\frac{\partial H_{2_{\k}}}{\partial x_{\kacc}} &= \frac{\kappa}{\langle k\rangle} P\left(\kacc\right) \: a\left(\kacc \rightarrow \k\right)(x_{\kacc}-2) \frac{2}{3} \\
\frac{\partial H_{2_{\k}}}{\partial y_{\kacc}} &= 0
\end{align*}

\label{LastPage}~