\documentclass{article}
    % General document formatting
    \usepackage[margin=0.7in]{geometry}
    \usepackage[parfill]{parskip}
    \usepackage[utf8]{inputenc}
    \usepackage{mathrsfs}
    \usepackage{amsmath}
    \usepackage{amssymb}
    \usepackage{tikz}
    \usepackage{fancyhdr}
    \usepackage{multicol}

    \usetikzlibrary{positioning}

\pagestyle{fancy}
\fancyhf{}
\rhead{Edgar Jacob Rivera Rios - A01184125}

\renewcommand{\labelenumi}{\alph{enumi})}

\begin{document}
\section*{4.3.1}
Individuals filing federal income tax returns prior to March 31 received an average refund of \$1056. Consider the population of “last-minute” filers who mail their tax return during the last five days of the income tax period (typically April 10 to April 15).
\begin{enumerate}
  \item A researcher suggests that a reason individuals wait until the last five days is that on average these individuals receive lower refunds than do early filers. Develop appropriate hypotheses such that rejection of $H_{0}$ will support the researcher’s contention.
  \begin{align*}
    \text{Average of people that fill last days} &= \mu_{l}\\
    H_{0} &= \mu \leq \mu_{l}\\
    H_{\alpha} &= \mu > \mu_{l}\\
  \end{align*}

  \item For a sample of 400 individuals who filed a tax return between April 10 and 15, the sample mean refund was \$910. Based on prior experience a population standard deviation of $\sigma$ \$1600 may be assumed. What is the $p$-value?
  \begin{align*}
    \sigma &= 1600\\
    \mu &= 1056\\
    \bar{x} &= 910\\
    n &= 400\\
    \sigma_{\bar{x}} &= \frac{\sigma}{\sqrt{n}}\\
    \sigma_{\bar{x}} &= \frac{1600}{\sqrt{400}}\\
    \sigma_{\bar{x}} &= 80\\
    z &= \frac{\bar{x} - \mu}{\sigma_{\bar{x}}}\\
    z &= \frac{910 - 1056}{80}\\
    z &= -1.825\\
    p-value &= 0.0340005
  \end{align*}

  \item At $\alpha =.05$, what is your conclusion?

  As the p-value is less than $\alpha$, we can reject $H_{0}$, which means that $H_{\alpha}$ is true, which means that the individuals who mail the tax return in the last 5 days truly receive less than the ones who send them earlier

  \item Repeat the preceding hypothesis test using the critical value approach.

  \begin{align*}
    z &= -1.825\\
    \text{critical value} (z_{0}) &= -1.64485
  \end{align*}
  As $z < z_{0}$ we reject the null hypothesis

\end{enumerate}
\pagebreak

\section*{4.3.2}
In a study entitled How Undergraduate Students Use Credit Cards, it was reported that undergraduate students have a mean credit card balance of \$3173 (Sallie Mae, April 2009). This figure was an all-time high and had increased 44\% over the previous five years. Assume that a current study is being conducted to determine if it can be concluded that the mean credit card balance for undergraduate students has continued to increase compared to the April 2009 report. Based on previous studies, use a population standard deviation $\sigma =\$1000$.
\begin{enumerate}
  \item State the null and alternative hypotheses.
  \begin{align*}
    \text{Updated CC balance} &= \mu_{c}\\
    H_{0} &= \mu \geq \mu_{c}\\
    H_{\alpha} &= \mu < \mu_{c}\\
  \end{align*}

  \item What is the $p$-value for a sample of 180 undergraduate students with a sample mean credit card balance of \$3325?
  \begin{align*}
    \sigma &= 1000\\
    \mu &= 3173\\
    \bar{x} &= 3325\\
    n &= 180\\
    \sigma_{\bar{x}} &= \frac{\sigma}{\sqrt{n}}\\
    \sigma_{\bar{x}} &= \frac{1000}{\sqrt{180}}\\
    \sigma_{\bar{x}} &= 74.53559924999\\
    z &= \frac{\bar{x} - \mu}{\sigma_{\bar{x}}}\\
    z &= \frac{3325 - 3173}{74.53559924999}\\
    z &= 2.03929399547981\\
    p-value &= 0.020710347281277
  \end{align*}

  \item Using a .05 level of significance, what is your conclusion?

  As the $p$-value is smaller than .05, we can reject the null hypothesis, meaning that the credit card balance for undergraute students has continued rising

\end{enumerate}
\pagebreak

\section*{4.3.3}
Consider the following hypothesis test:
\begin{align*}
  H_{0}: \mu \leq 25\\
  H_{\alpha}: \mu > 25\\
\end{align*}
A sample of 40 provided a sample mean of 26.4. The population standard deviation is 6.
\begin{enumerate}
  \item Compute the value of the test statistic.

  \begin{align*}
    \sigma &= 6\\
    \mu &= 25\\
    \bar{x} &= 26.4\\
    n &= 40\\
    \sigma_{\bar{x}} &= \frac{\sigma}{\sqrt{n}}\\
    \sigma_{\bar{x}} &= \frac{6}{\sqrt{40}}\\
    \sigma_{\bar{x}} &= 0.9486832980505\\
    z &= \frac{\bar{x} - \mu}{\sigma_{\bar{x}}}\\
    z &= \frac{26.4 - 25}{0.9486832980505}\\
    z &= -1.47572957474524\\
  \end{align*}

  \item What is the $p$-value?
  \begin{align}
    p-value &= 0.070008251598585
  \end{align}

  \item At $\alpha = .01$, what is your conclusion?

  As the $p-value$ is greater than alpha,the null hypothesis cannot be rejected, so we don't have a definitive answer to this question

  \item What is the rejection rule using the critical value? What is your conclusion?

  The rejection rule is that $z < z_{0}$, but it this case $z =-1.47572957474524$ and $z_{0} = -2.32634787404084$, so the rejection does not hold

\end{enumerate}

\end{document}


\subsubsection{Explicit, Coupled, Advective-Dispersive Mass Transfer}\label{sec:adv_dif_mass_transfer}

The third Cauchy type mixed boundary condition defines a
solute flux along a boundary.  The fixed concentration flux Cauchy boundary
condition can be provided at the external boundary of any mass balance model.
For a vertically oriented system with advective velocity in the $\hat{k}$
direction,

    \begin{align}
      -D\frac{\partial C(z, t)}{\partial z}\Big|_{z \in \Gamma} &+ v_zC(z, t) = v_zC(t)
      \intertext{where}
      C(t) &= \mbox{ a known concentration function }[kg/m^{3}].\nonumber
    \end{align}

In the Degradation Rate and Mixed Cell models, the Cauchy boundary condition
can be selected to enforce coupled advective and dispersive flow,

\begin{align}
  J_{coupled} &= J_{adv} + J_{dis} \nonumber\\
  &= \theta vC(z,t_n) -\theta D\frac{\partial C}{\partial z}.
\end{align}

The resulting mass transfer into the Degradation Rate or Mixed Cell model is then,

\begin{align}
m_{jk}(t_n) &= A\Delta t \theta_k \left( v C(z,t_n)\Big|_{z=r_j} - D \frac{\partial C(z,t_n)}{\partial z}\Big|_{z=r_j} \right).
\end{align}


\subsection{Shared Secret Encryption}
\label{ssec:secret_enc}

As mentioned above, the shared secret from
$P_{i}$ to $P_{j}$ is $s_{i\to j} = f_{i}(j)$.
To encrypt this, we need to compute their shared secret:

\begin{equation}
    k_{ij} = \pk_{i}^{\sk_{j}} = \pk_{j}^{\sk_{i}} =
    g_{1}^{\sk_{i}\sk_{j}}.
\end{equation}

\noindent
Encryption and decryption are based on the idea of a one-time pad;
in particular, we use outputs of cryptographic hashes of
the $x$-coordinate of the shared secret along with the index
of the participant receiving the message as our ``one-time pad''.
This does not meet the technical definition of a one-time pad
as it is usually defined (one standard reference is~\cite{hac1996}),
but the idea is similar.
By including the index of the intended recipient in the hash function,
each symmetric encryption key is unique.
See Alg.~\ref{alg:enc_dec} for details.

\input{algs/encryption_and_decryption.tex}


\subsection{The Cholesky Decomposition}\label{sec:cholesky}
In \autoref{sec:simulatingvalues}, we utilised the Cholesky decomposition on the covariance matrix in the stochastic shadow fading part. The Cholesky decomposition is a decomposition algorithm for \gls{symmetric}, \gls{pd-matrix} into the product of a \gls{lt-matrix} and its \gls{conjugate-transpose}, and is primarily used for solving systems of linear equations~\cite{Press:2007:NRE:1403886}. In this Section, we present and describe the Cholesky decomposition, as well as the problems the decomposition creates for our computation time of the stochastic shadow fading part of the link model, as well as possible ways for us to optimise our usage of the Cholesky decomposition. \autoref{algo:cholesky} contains a pseudo code description of the Cholesky decomposition. \medbreak

\begin{algorithm}[H]
    \DontPrintSemicolon
    \KwResult{The Cholesky decomposition of the input matrix}
    \SetKwFunction{Cholesky}{Cholesky}
    \SetKwProg{Fn}{Function}{:}{}
    \Fn{\Cholesky{matrix, N}}{
        result $\leftarrow$ empty matrix of size N $\times$ N \;
        \For{n $\leftarrow$ 0, n < N}{
            \For{m $\leftarrow$ 0, m < n + 1}{
                sum $\leftarrow$ 0\;
                \For{i $\leftarrow$ 0, i < m}{
                    sum $\leftarrow$ sum + result$_{n,i} \cdot$ result$_{m,i}$\;
                }
                \If{n = m}{
                    \If{$\text{matrix}_{n,n} - \text{sum} \leq 0$}{
                        throw error; matrix is not positive-definite
                    }

                    results$_{n,m}$ $\leftarrow$ $\sqrt{\text{matrix}_{n,n} - \text{sum}}$\;
                }
                \Else{
                    results$_{n,m} \leftarrow \frac{1}{\text{result}_{m,m}} \cdot (\text{matrix}_{n,m} - sum)$\;
                }
            }
        }
        \KwRet result
    }
    \caption{Cholesky decomposition}
    \label{algo:cholesky}
\end{algorithm}
\medbreak
The first issue we have found with the Cholesky decomposition, or more specifically with the covariance matrix, is that the covariance matrix is not guaranteed to be a \gls{pd-matrix}. The covariance matrix is based on the relation between links in the network, which means that whether the matrix is positive-definite or not is entirely based on the network. To work around this, we have employed a tool called NearPD~\cite{website:nearPD} to transform our covariance matrix into a new matrix that has the positive-definite property, while minimising the Frobenius norm~\cite{website:frobieniusnorm} of the difference between the original and the new matrix. This significantly increases the time required to compute the link model, however, which leads us to our next major issue: The computational time required by the Cholesky decomposition itself. The computational time required for the NearPD tool and the Cholesky decomposition can be seen in \autoref{table:cholesky:spdtime}.\smallbreak

The Cholesky decomposition has a computational complexity of $O(n^3)$~\cite{Press:2007:NRE:1403886}. With a fully connected network of 1000 nodes, we would have a $\frac{1000(1000+1)}{2} - 1000 = 499500$ (\autoref{eq:lengthoflinks}) unique links, which means that our correlation (and covariance) matrix would be of size $499500 \times 499500$. This is a major issue, as we would like to be able to compute the link model in a relatively short amount of time. To combat this issue, we have two possible solutions: removing links with a distance over a certain threshold, and clustering nodes that are very close to each-other. We present the first solution in this section, and clustering in \autoref{sec:clustering}.

\begin{table}[H]
    \centering
    \begin{tabular}{|c|c|c|c|}
        \hline
        Nodes & Links & NearPD          & Cholesky                 \\\hline
        10    & 45    & 3 milliseconds  & \textless{}1 millisecond \\\hline
        20    & 190   & 88 milliseconds & 3 milliseconds           \\\hline
        30    & 435   & 1 seconds       & 34 milliseconds          \\\hline
        40    & 780   & 6 seconds       & 200 milliseconds         \\\hline
        50    & 1225  & 32 seconds      & 720 milliseconds         \\\hline
        60    & 1770  & 113 seconds     & 3 seconds                \\\hline
        70    & 2415  & 285 seconds     & 6 seconds                \\\hline
        80    & 3160  & 584 seconds     & 12 seconds               \\\hline
        90    & 4005  & 22 minutes      & 26 seconds               \\\hline
        100   & 4950  & 35 minutes      & 45 seconds               \\\hline
        110   & 5995  & 75 minutes      & 93 seconds               \\\hline
        120   & 7140  & 127 minutes     & 155 seconds              \\\hline
        130   & 8385  & 235 minutes     & 250 seconds              \\\hline
        140   & 9730  & Timeout         & \dots                    \\\hline%582 seconds              \\\hline
        %150   & 11175 & \dots           & 14 minutes               \\\hline
        %160   & 12720 & \dots           & 17 minutes               \\\hline
        %170   & 14365 & \dots           & 20 minutes               \\\hline
        %180   & 16110 & \dots           & 29 minutes               \\\hline
        %190   & 17955 & \dots           & 39 minutes               \\\hline
        %200   & 19900 & \dots           & 52 minutes               \\\hline
    \end{tabular}
    \caption{Computation time measurement for NearPD and Cholesky decomposition.}
    \label{table:cholesky:spdtime}
\end{table}

\autoref{table:cholesky:spdtime} shows time measurements from running the cholesky decomposition computation with different network topology sizes with the NearPD tool. The measurements shows that the NearPD tool is not fast enough, as at only 130 nodes with 8385 links the tool takes 235 minutes to compute the \gls{pd-matrix}. This is not fast enough and a better solution will have to be found. The cholesky decomposition computation speed is also problematic, as the goal is 1000 nodes and already at 120 nodes the decomposition takes more than 2 minutes.

\subsubsection{Distance Threshold}\label{sec:distancethreshold}
In \autoref{sec:linkmodel}, we saw that the distance dependent \gls{pathloss} has significantly more importance in the total \gls{pathloss} than the stochastic shadow fading part. In \autoref{eq:pathlossdetermG}, we see that the distance \gls{pathloss} is 92 \acrshort{dbm} for links of 100 meters, and 100.2 \acrshort{dbm} for the diagonal links of $141.42$ meters, and in \autoref{eq:pathlossfadingG}, we see (stochastic) \gls{pathloss} values between $-13.837$ and $6.413$. This means that, entirely based on the distance part of the link model, according to the formula for computing packet error probability in \autoref{sec:pep}, with a distance of 1000 meters, and a transmission power of 26 \acrshort{dbm}, we would have a link \gls{pathloss} of $147$ \acrshort{dbm} (disregarding the stochastic shadow fading part of the \gls{pathloss}), which in turn would mean that the \gls{rssi} on the receiving end of the link would be $26 - 147 = -121$, or equivalent to a packet loss probability of 99.999~\% (assuming the noise figure and thermal noise is the same as in \autoref{sec:pep}, and the packet size is 160 bits). Hence removing links that are far away can potentially reduce the size of the correlation matrix significantly. To show this, we ran an experiment, where we generated 1000 nodes with random locations in a $25 \times 25$ kilometre area, created links between these nodes based on their location, and iteratively scaled the maximum distance allowed between nodes (the distance threshold) and calculated the link path loss, based on the distance threshold. The results of this experiment can be seen in \autoref{table:cholesky:distance-threshold}. Recall that a fully connected network topology of 1000 nodes would have a total of 499500 links. With a distance threshold of 1000 meters, we get a total of 2346 links in our $25 \times 25$ kilometre area, which is a significant improvement as we would be able to compute the Cholesky decomposition in approximately six seconds (disregarding the positive-definiteness of the correlation matrix) according to \autoref{table:cholesky:spdtime}.

%The experiment consisted of generating 1000 random nodes in a 25 kilometre square area, create links from those nodes based on their location, iteratively scale the maximum allowed distance of the links (distance threshold) and calculate the link path loss. The results can be read in \autoref{table:cholesky:distance-threshold}.

\begin{table}[H]
    \centering
    \begin{tabular}{|c|c|c|}
        \hline
        Distance threshold & Links & Probability \\\hline % & Cholesky         \\\hline
        % 100 meters         & 182   & \\\hline % & 3 milliseconds   \\\hline
        % 125 meters         & 256   & \\\hline % & 7 milliseconds   \\\hline
        % 150 meters         & 360   & \\\hline % & 20 milliseconds  \\\hline
        % 175 meters         & 483   & \\\hline % & 46 milliseconds  \\\hline
        200 meters         & 92    & 0.001~\%    \\\hline % & 97 milliseconds  \\\hline
        % 225 meters         & 773   & \\\hline % & 185 milliseconds \\\hline
        250 meters         & 139   & 0.001~\%    \\\hline % & 333 milliseconds \\\hline
        % 275 meters         & 1157  & \\\hline % & 646 milliseconds \\\hline
        300 meters         & 208   & 0.001~\%    \\\hline % & 1 second         \\\hline
        % 325 meters         & 1589  & \\\hline % & 1 second         \\\hline
        350 meters         & 296   & 0.001~\%    \\\hline % & 2 seconds        \\\hline
        % 375 meters         & 2076  & \\\hline % & 4 seconds        \\\hline
        400 meters         & 394   & 0.001~\%    \\\hline % & 6 seconds        \\\hline
        % 425 meters         & 2642  & \\\hline % & 8 seconds        \\\hline
        450 meters         & 490   & 0.001~\%    \\\hline % & 11 seconds       \\\hline
        % 475 meters         & 3290  & \\\hline % & 15 seconds       \\\hline
        500 meters         & 592   & 6.000~\%        \\\hline % & 21 seconds       \\\hline
        % 525 meters         & 4035  & \\\hline % & 28 seconds       \\\hline
        550 meters         & 723   & 82.333~\%    \\\hline % & 37 seconds       \\\hline
        % 575 meters         & 4830  & \\\hline % & 49 seconds       \\\hline
        600 meters         & 846   & 99.999~\%   \\\hline % & 62 seconds       \\\hline
        % 625 meters         & 5723  & \\\hline % & 82 seconds       \\\hline
        650 meters         & 994   & 99.999~\%   \\\hline % & 142 seconds      \\\hline
        %675 meters         & 6656  & \\\hline % & 184 seconds      \\\hline
        700 meters         & 1164  & 99.999~\%   \\\hline % & 273 seconds      \\\hline
        % 725 meters         & 7608  & \\\hline % & 375 seconds      \\\hline
        750 meters         & 1349  & 99.999~\%   \\\hline % & 454 seconds      \\\hline
        % 775 meters               & 8696 \\\hline %  & 454 seconds      \\\hline
        800 meters         & 1507  & 99.999~\%   \\\hline %  & 580 seconds      \\\hline
        % 825 meters               & 9837 & \\\hline %  & 708 seconds      \\\hline
        850 meters         & 1714  & 99.999~\%   \\\hline % & 544 seconds      \\\hline
        % 875 meters               & 1101 & \\\hline %5 & 571 seconds      \\\hline
        900 meters         & 1910  & 99.999~\%   \\\hline % & 665 seconds      \\\hline
        % 925 meters               & 1225 & \\\hline %1 & 783 seconds      \\\hline
        950 meters         & 2102  & 99.999~\%   \\\hline % & 916 seconds      \\\hline
        % 975 meters               & 1356 & \\\hline %8 & 1058 seconds     \\\hline
        1000 meters        & 2346  & 99.999~\%   \\\hline % & 1214 seconds     \\\hline
    \end{tabular}
    \caption{Results from the distance threshold experiments.}
    \label{table:cholesky:distance-threshold}
\end{table}

% as the last row in \autoref{table:cholesky:distance-threshold} of 750m gives good results. More aggressive thresholds drastically reduces the total amount of links more, but has the potential of removing links that should have been there.

% 55 \log_{10} \left( d(l_1) \right) - 18

% \subsection{Cholesky decomposition}\label{sec:cholesky}
%In this section we present and describe the Cholesky decomposition, and the problem it creates for our computation time, and how we propose to optimise the decomposition algorithm for our particular needs.
%sec:simulatingvalues
% The cholesky decomposition or cholesky factorization is a matrix decomposition, of a positive-definite matrix, resulting in a lower triangular matrix and its conjugate transpose.

%In \autoref{sec:linkmodel} we utilise the Cholesky decomposition in \autoref{eq:pathlossstoch}. The Cholesky decomposition is a matrix decomposition, on a \gls{pd-matrix}. The decomposition results in a \gls{lt-matrix} and its \gls{conjugate-transpose}. The Cholesky decomposition is an expensive computation of cubic time complexity, as such we intend to speed up the algorithm. Furthermore since the decomposition requires an \gls{pd-matrix} to work, we choose to verify our auto-correlation matrix before decomposing it, to ensure that the decomposition will run correctly.

% is a decomposition of a Hermitian, positive-definite matrix into the product of a lower triangular matrix and its conjugate transpose,

% In \autoref{sec:linkmodel} we utilise the Cholesky decomposition in \autoref{eq:pathlossstoch}.
% In \autoref{sec:linkmodel} we utilise the Cholesky decomposition. The Cholesky decomposition is an expensive computation of cubic time complexity and, as such, it needs to be more efficient for our use case.

%Initially we propose to optimise the algorithm by changing the data structure from a matrix to an ordered map of key-value pairs. The keys will a tuple of links and the value will be the result of the auto-correlation function from \autoref{eq:pathlossautocorrelation}.\medbreak

%, where the pair will be sorted after the link with the largest id, will be the first element in the pair, eg. $l_1.id = 1$ and $l_2.id = 2$ then $key = (l_2, l_1)$. The map must be ordered since the cholesky decomposition uses previous calculated values, to calculate the next.

% shortly introduce cholesky

% our intended improvements


\chapter{Background and related work}

To give some background to our method this chapter first gives a brief overview of machine learning and neural networks. Then, a selection of face detection, recognition, and reconstruction methods are presented. More emphasis is given to facial reconstruction methods, especially those that have been developed recently and use neural networks and synthetic data in one way or another.

% Finally, selected parts of computer graphics theory are presented.

\section{Machine learning and neural networks}

If we have a parametric function $f(\bm{x};\bm{\theta}) = \bm{y}$, how should the parameters $\bm{\theta}$ be adjusted so that, with input $\bm{x}$, the output of the function matches $\bm{y}$ as closely as possible? Machine learning, and especially its most common form, supervised machine learning, can be used to solve this problem. In supervised learning, the parameters $\bm{\theta}$ are iteratively adjusted until the output of the function cannot be further made more accurate. To make these kinds of small adjustments, a number of training pairs $(\bm{x}_i,\bm{y}_i)$ are needed in addition to some metric that tells how well the function is performing. \cite{Goodfellow2016,LeCun2015}

The function performance can be measured by a separate loss function that tells how far the output of the function is from the desired values. In the case of fitting a simple line function to a collection of points, the loss function could be visualized as the sum of the distances of the points from the line. If the points lie exactly on the line, the loss becomes zero, and further away the points are from the line, the bigger the loss becomes. Especially with more complex functions, the loss could be visualized as a multi-dimensional hilly landscape. Lower values of the loss function are valleys, and higher values are hills. The lower it is possible to travel in this loss landscape, the better the function under optimization performs. With complex functions, the loss landscape has numerous valleys and hills. Even if it seems that the loss is now small, because you are at the bottom of a valley, it is very much possible that there exists another valley somewhere else that is even deeper and thus better. \cite{Goodfellow2016,LeCun2015}

To adjust the parameters of the function, an algorithm called gradient descent is used. The gradient of the loss function can be visualized as an arrow that, at any point in the loss landscape, points towards the direction of the greatest ascent. To travel towards lower loss function values, that is, towards the bottoms of the valleys, a step into the opposite direction of the gradient should be taken. When repeated enough many times, a loss function minimum is reached. The problem with gradient descent is that, after reaching a bottom of a valley, the algorithm cannot continue. This means that the optimization could get stuck into a local minimum even though much better minima would be available further away. \cite{Goodfellow2016,LeCun2015}

If a very accurate gradient is calculated using all the available training data, it is very much possible for the gradient descent algorithm to get stuck at a local minimum. The accurate gradient calculation is also time-consuming and usually needs the whole dataset to be kept in memory. This is not feasible for very large datasets. The \ac{SGD} algorithm solves this problem by calculating a less accurate gradient from a smaller, randomly selected, part of the training dataset. This small selection of training samples is called a minibatch. The more random gradient is faster to calculate, and because it is noisy, it will help the gradient descent algorithm to escape from local minima. \cite{Goodfellow2016,LeCun2015}

Instead of using just one function as the target of the optimization, a composite of multiple functions, a network, can also be used. These networks are usually called neural networks because the functional parts are loosely inspired by neuroscience. A neural network has an input layer, any number of hidden layers, and an output layer. The adjacent layers can be fully connected to each other with distinct weights for each connection, and the values that flow through the network can be modified with activation functions. The hidden layers are called so because the training data does not give any desired output for them. Instead, when trained, the network is free to come up with its internal representations. The training of a neural network like this is enabled by the backpropagation algorithm. For it to work, one requirement is that the functions that compose the network are differentiable. If this is the case, the gradient calculated at the output layer can be backpropagated through the network, and all the parameters of the network can be adjusted accordingly. \cite{Goodfellow2016,LeCun2015}

The machine learning algorithms used today are largely the same as in the 1980s. The massive increase of computing power, storage space, and memory amounts in recent years has enabled the training of networks that were previously thought very hard to train. The modern general processing \acp{GPU} have been instrumental in making the training process faster, as their architecture suits the task very well. Some other smaller developments, e.g., using the \ac{RELU} activation function, have also played their part in making the training of deeper networks easier. Modern neural networks can have tens of millions of parameters, and the networks can be very deep with tens, if not hundreds, of layers. Deep learning, the name given to this new era of machine learning, reflects this fact. A good example of the deep learning advantage is the application of deep neural networks by \textcite{Krizhevsky2012} in the 2012 ImageNet competition. Their results were impressive, almost halving the error rate compared to the best competitors. \cite{Goodfellow2016,LeCun2015}

The functions that the network is composed of can also perform filtering using convolutions. These kinds of networks are called \acfp{CNN}. Convolutions can be though as small filters that are slid over the larger underlying signal and that produce a new, modified, signal. Convolutions are especially well suited for processing images. One of the first applications of \acp{CNN} was a method to recognize handwritten digits, developed by \textcite{Lecun1998}. Many convolutions, with differing filter values, can be applied to the same image. This allows the extraction of different features from the image. The hierarchical nature of the \acp{CNN} means that, at the first layers, the extracted features are edges, and then continuing deeper inside the network the features become parts, and parts become objects. The structure of \acp{CNN} has been directly inspired by the concepts in visual neuroscience. \cite{Goodfellow2016,LeCun2015}

An ordinary \ac{CNN} usually has fully connected layers at the end and will produce a one-dimensional vector as the final output \cite{Lecun1998}. In \acfp{FCNN}, the fully connected part at the end is replaced by a convolutional upsampling part \cite{Long2015}. This means that, for \acp{FCNN}, both the inputs and outputs can be images. \acp{FCNN} have been successfully used for dense image semantic segmentation. Semantic segmentation means understanding the image at the pixel level, i.e., giving each pixel of the input image a meaningful class \cite{Long2015,Ronneberger2015}. The training of \acp{FCNN} has been made easier with the introduction of the concept of skip connections. Skip connections connect the downsampling and upsampling parts of the \ac{FCNN} directly. Skip connections have been shown to help gradients propagate from the output towards the input side of the network, transfer high-frequency detail from the input side to output side, and help to avoid singularities in the loss landscape \cite{Ronneberger2015,Orhan2017,Goodfellow2016}.

Designing and training the neural networks, especially the deep ones, would be very time-consuming if the training code had to be reimplemented at low-level with every design iteration. To get fast enough training speeds, using \acp{GPU} is necessary. The training code has to be executable on \acp{GPU}, and efficient transfer of training data to the \acp{GPU} needs to be implemented. To help make the neural network design process, code generation for the \acp{GPU}, data transfer to the \acp{GPU}, and the training process easier, numerous software frameworks have been developed in recent years. Good examples of these software frameworks are \textcite{cntk}, \textcite{tensorflow}, and \textcite{pytorch}. With these frameworks, the neural network can be designed at high-level with the Python scripting language. Changes to the network topology can be made easily, and the framework will take care of converting the high-level network description into optimized low-level code that is ready to be run on \acp{GPU}. \cite{cntk,tensorflow,pytorch}

\section{Face detection and reconstruction}

The techniques for processing human faces in images are broadly classified into three categories by \textcite{Datta2015}: face detection, face recognition, and face reconstruction. Face detection tells us if there exists a human face in an image in the first place, and can give the approximate location and size of the face. Face recognition goes a step further as it can identify the actual person in the image. Face reconstruction does not necessarily have anything to do with identification but rather finding out the underlying facial geometry, i.e., the facial shape, of the human pictured.

\begin{figure}
    \centering
    \subfloat[\cite{Sakai1972}]{\label{fig:face_detection_1a}\includegraphics[height=.3\textwidth]{takeo}}\qquad\qquad
    \subfloat[]{\label{fig:face_detection_1b}\includegraphics[height=.3\textwidth]{eigenfaces}}
    \caption[Face detection 1]{One of the first face detection systems was developed in the early 1970s by \textcite{Sakai1972}. Their algorithm was based on analyzing slices of a binary image of the head as shown in \protect\subref{fig:face_detection_1a}. Face recognition based on eigenfaces, as proposed by \textcite{Turk1991}, was one of the first commercially viable methods for face recognition. Visualizations of eigenfaces are shown in \protect\subref{fig:face_detection_1b} (Copyright of AT\&T Laboratories Cambridge).}
    \label{fig:face_detection_1}
\end{figure}

One of the first automated face detection systems was developed in the early 1970s by \textcite{Sakai1972}. The input was a 5-bit grayscale image with a resolution of 140x208. The image was processed with an edge detecting filter and then thresholded to obtain a binary image containing contours of the face. See figure \ref{fig:face_detection_1a} for an example. The binary image was then scanned slice-by-slice from top to bottom. Each slice was analyzed while an elaborate state machine kept track whether the image contained a human or not. The face had to be centered in the image and could have only a small amount of tilt in any direction. This method did not work at all if the person in the image had glasses or a beard.

The method of using eigenfaces for recognizing humans from pictures was introduced by \textcite{Turk1991} in 1991. It was one of the first accurate and fast enough methods to be used commercially. The method captured the relevant variation in a collection of facial images, that is, the principal components of the distribution, into eigenvectors. The eigenvectors can be visualized as ghostly faces, eigenfaces, as is shown in figure \ref{fig:face_detection_1b}. Any picture of a human face could then be deconstructed into a linear combination of eigenvectors, and inversely, reconstructed with the same linear combination of the same eigenvectors. The face recognition could be done by comparing the weights of the linear combinations as the weights would be similar between two images of the same person.

\begin{figure}
    \centering
    \subfloat[\cite{Viola2001}]{\label{fig:face_detection_2a}\includegraphics[height=.28\textwidth]{viola}}\hfill
    \subfloat[ \cite{Osadchy2007}]{\label{fig:face_detection_2b}\includegraphics[height=.28\textwidth]{osadchy}}
    \caption[Face detection 2]{Face detection speed was greatly increased by a method introduced by \textcite{Viola2001}. The method was based on fast evaluation of rectangular filters and machine learning. Some filters are visualized in \protect\subref{fig:face_detection_2a}. Convolutional neural networks were used by \textcite{Osadchy2007} for fast face detection and pose estimation. \protect\subref{fig:face_detection_2b} shows how the algorithm performed on a somewhat difficult image.}
    \label{fig:face_detection_2}
\end{figure}

The speed of detecting faces and generating the bounding boxes around them was dramatically improved by \textcite{Viola2001} in 2001. Their proposed method was based on the idea of the integral image, rectangular feature filters, and machine learning. The integral image, or summed area tables, was an intermediate representation of the image that allowed rapid summation of pixels in arbitrary rectangular areas. The rectangular feature filters are illustrated in figure \ref{fig:face_detection_2a}. The filter calculated the sum of the pixels in the white area which was then subtracted from the sum of the black area. The filter could thus find intensity variations between arbitrary rectangular areas rapidly. Hundreds of thousands of possible combinations of these filters existed for a 24x24 pixel detection window, and machine learning was used to select few thousand of the most relevant filters for human face detection. Applying the resulting combination of filters to images was fast, and faces could be detected and tracked near real-time with contemporary hardware.

In 2007, \textcite{Osadchy2007} published a novel method for simultaneously detecting faces and estimating their poses in real-time using convolutional neural networks. Figure \ref{fig:face_detection_2b} shows how the network was able to detect multiple faces in one image including their yaw from left to right and in-plane rotation. Their network topology was similar to the one used by \textcite{Lecun1998} in 1998 to recognize hand-written digits. The training data consisted of real human faces which were manually annotated with the pose data. The method could do face detection and pose estimation at the same time quicker and more accurately than previous methods that did the tasks separately.

\begin{figure}
    \centering
    \subfloat[\cite{Blanz1999}]{\label{fig:face_reconstruction_1a}\includegraphics[height=.2\textwidth]{blanz}}

    \subfloat[\cite{Richardson2016a}]{\label{fig:face_reconstruction_1b}\includegraphics[width=\textwidth]{richardson1}}
    \caption[Face reconstruction 1]{Facial geometry and texture reconstruction using a 3D morphable model was proposed by \textcite{Blanz1999}. The steps of their algorithm are shown in \protect\subref{fig:face_reconstruction_1a}. \textcite{Richardson2016a} used a \ac{CNN} and an iteration process to fit the morphable model to the input image. The steps are shown in \protect\subref{fig:face_reconstruction_1b}.}
    \label{fig:face_reconstruction_1}
\end{figure}

In 1999, \textcite{Blanz1999} introduced a new technique to reconstruct human facial geometry from a single image using a 3D morphable face model. They started by scanning hundreds of real human faces with a 3D laser scanner. The scan produced accurate vertex positions and colors. The generated 3D face models were processed so that they all came into full correspondence with each other. Using \ac{PCA} decomposition, an average face model and a set of basis face vectors were created. \ac{PCA} was also done for the vertex color data. This allowed a parametric creation of 3D face models and their textures. The process of reconstructing the facial geometry from a single image started with a coarse manual alignment of the average face model over the image. An analysis-by-synthesis loop was then repeated where the face model was rendered over the image, and the factors of the principal components were adjusted until an optimization minimum was reached. Finally, additional detailed facial texture information was extracted from the image as the color \ac{PCA} components did not contain enough high-resolution data. Steps of this process are shown in figure \ref{fig:face_reconstruction_1a}.

More recently, in 2016, \textcite{Richardson2016a} proposed a method to extract 3D morphable model parameters from real-world images using a \ac{CNN}. The steps of their algorithm are visualized in figure \ref{fig:face_reconstruction_1b}. They started by aligning the average morphable face model over the image using another posing algorithm. The face was segmented out of the background and fed to the \ac{CNN} along with a rendered version of the current morphable face model. The \ac{CNN} was trained to output a correction term to the morphable model parameters to make the rendered face model match the segmented real face image better. The face model rendering and parameter correction calculation were repeated iteratively multiple times to increase the quality of the reconstruction. The \ac{CNN} was completely trained on synthetic data generated using the 3D morphable face model. Because fine facial details could not be extracted using this method, they were later captured with a separate shape-from-shading algorithm.

\textcite{Richardson2016} improved their previous method of \cite{Richardson2016a}. The problem with their previous method was that it needed separate initialization of the average face model over the input image and that the method could not extract fine details without an external algorithm. The improved algorithm could do both in one go using two connected networks, a \ac{CNN} and an \ac{FCNN}. The \ac{CNN} was applied iteratively to the input image and a coarse geometry using a 3D morphable model was recovered. A depth map was generated from this model, and the map was fed to the \ac{FCNN} along with the input image. The \ac{FCNN} was trained to do fine detail reconstruction, i.e., shape-from-shading, within the depth map in one shot without iteration. The \ac{CNN} was trained in a supervised manner with synthetic data rendered using the 3D morphable model, and the \ac{FCNN} was trained in an unsupervised manner using real-world images.

\textcite{Kim2017} introduced a method that could estimate a wide variety of 3D morphable model parameters from a real-world image in a single shot using a \ac{CNN}. The estimated parameters included facial pose, facial shape, facial expression, skin reflectance, and scene illumination. No iteration was needed to refine the results. Before feeding the real-world images to the network, their variety was reduced by segmenting the faces out of the backgrounds using facial landmark detection. The network was trained exclusively on synthetic training data derived from the 3D morphable model. To get the parameter distributions of the synthetic training data to better model the real-world distributions, a novel breeding method was introduced that used real-world facial images to update the training set. A network trained with the breeding outperformed a network trained only on synthetic data.

\textcite{Tewari2017} were able to train an autoencoder \ac{CNN} completely unsupervised using only real-world images. No separately generated synthetic data was used. The \ac{CNN} was able to extract 3D morphable model parameters including facial pose, facial shape, facial expression, skin reflectance, and scene illumination in a single iteration. The method relied on a novel analytical and differentiable decoder layer that used the morphable model parameters to reconstruct the input image. It was possible to train the network in an unsupervised manner because the loss could be backpropagated through the decoder layer. With a method like this, the real-world distribution of the morphable model parameters could be captured the best, and the network could generalize well to real-world images.

\begin{figure}
    \centering
    \subfloat[\cite{Sela2017}]{\label{fig:face_reconstruction_2a}\includegraphics[height=.3\textwidth]{sela1}}\qquad\qquad
    \subfloat[\cite{Guler2016}]{\label{fig:face_reconstruction_2b}\includegraphics[height=.3\textwidth]{guler1}}

    \subfloat[\cite{Yu2017}]{\label{fig:face_reconstruction_2c}\includegraphics[height=.3\textwidth]{yu1}}
    \caption[Face reconstruction 2]{The method of \textcite{Sela2017} generated dense correspondence and depth map images. Their results are shown in \protect\subref{fig:face_reconstruction_2a}. \textcite{Guler2016} generated dense correspondences using quantized regression which resulted in outputs shown in \protect\subref{fig:face_reconstruction_2b}. \textcite{Yu2017} proposed a method that could estimate dense 2D pixel flow between the input image and a front-facing template mesh. Visualization of their method is shown in \protect\subref{fig:face_reconstruction_2c}.}
    \label{fig:face_reconstruction_2}
\end{figure}

After the work on this thesis had started, multiple papers were published that had similar ideas regarding \acp{CNN} and dense geometry generation. \textcite{Sela2017} proposed an \ac{FCNN} that could map a real-world human face image to a dense correspondence image and a dense depth map image. Examples of these images are shown in figure \ref{fig:face_reconstruction_2a}. In contrast to most of the previously mentioned methods, this method did not use a 3D morphable model in the reconstruction process. The correspondence and depth map images were then used with a separate algorithm to extract the 3D mesh of the face. The network was trained with synthetic data with a wide range of facial shapes, facial poses, facial materials, lighting conditions, and background textures. A surprising result was that even though the synthetic data was created with a limited generative model, the network could generalize beyond the limited scope of the training material.

\textcite{Guler2016} trained an \ac{FCNN} to estimate dense correspondences between image pixels and a 3D template mesh projected onto a 2D deformation-free space, or in other words, a UV space. Their approach was very similar to ours, but instead of plain regression, they used quantized regression. The mappings generated by this method are illustrated in figure \ref{fig:face_reconstruction_2b}. The training dataset was generated from real-world images that had existing facial landmark annotations. The dataset generation was done by first fitting the 3D template mesh over the image using the landmarks and then rasterizing the mesh using colors representing locations in the deformation-free space.

\textcite{Yu2017} used an \ac{FCNN} to generate dense facial correspondences between the input image and a 3D morphable face model. Two images were generated from the input image: a 2D flow image and a matchability mask image. The 2D flow image estimated the flow between the input image pixels and a synthetic rendering of an average frontal face. The matchability image indicated which correspondences were valid inside the average face. These images are visualized in figure \ref{fig:face_reconstruction_2c}. In the beginning, the network was trained with synthetic data generated from a 3D morphable model. Later, the network was refined using annotated real-world images. Simple rectangular occlusions were also added to the training data which made the model more robust against obstructions over the faces.

\begin{figure}
    \centering
    \includegraphics[height=.4\textwidth]{jackson1}
    \caption[Face reconstruction 3]{Results of the method proposed by \textcite{Jackson2017}. They used \acp{FCNN} to regress from the input image into a 3D volume directly.}
    \label{fig:face_reconstruction_3}
\end{figure}

In late 2017, \textcite{Jackson2017} published a novel method that used \acp{FCNN} for direct 2D-to-3D facial geometry reconstruction. No intermediate fitting steps were used. The training dataset consisted of real-world images and corresponding 3D binary volumes that modeled the faces in the input images. Inside the volume, if a voxel was inside the face, it was given a value of 1, and 0 otherwise. The 3D binary volumes were created using a 3D morphable model that was already fitted to the input images. The actual 3D facial geometry was recovered by generating the iso-surface of the regressed binary volume. Results of this method are shown in figure \ref{fig:face_reconstruction_3}.

\iffalse

\section{Computer graphics and rendering}

Computer graphics theory is a vast subject area. The relevant parts of it, for this thesis, are 3D mesh representation, texture mapping, surface reflectance models (\acsp{BRDF}), the light transport equation, path tracing, and antialiasing.

\begin{gather}
L_o(p, \omega_o) = L_e(p, \omega_o) + \int_\Omega f(p, \omega_o, \omega_i) L_i(p, \omega_i) |cos\theta_i| d\omega_i
\end{gather}

\begin{Verbatim}
- how 3D meshes are composed of triangles
- triangles have vertices that have 3D world coordinates and 2D UV coordinates
- UV coordinates are used for texture mapping
- rendering can be performed by shooting rays from the camera
- realistic images can be generated by solving the lighting equation
- lighting equation can be solved using path tracing
- light bouncing determined by surface reflectance models (BRDFs)
- reflectance models (BRDFs)
- smoothing edges using antialiasing
\end{Verbatim}

\begin{figure}[h]
    \includegraphics[width=\textwidth]{tex-mapping}
    \caption[Texture mapping]{An illustration of the texture mapping process. A 3D mesh consists of triangles. Each vertex of a triangle usually has at least a 3D world coordinate (red) and a 2D UV coordinate (green). UV coordinates map the triangle onto the 2D UV space of the texture. The color from the texture can be applied to the triangle when rendering.}
    \label{fig:tex_mapping_1}
\end{figure}

\fi


\subsection{Governing equations of the coarse-scale (continuum) model}
Consider the dynamic equilibrium of a naturally fractured rock mass $\Omega$. Let $\Gamma$ denote the boundary of $\Omega$ and let $\Gamma$ be divided into mutually exclusive sets $\Gamma_u$ and $\Gamma_t$.   The body contains a set of natural fractures denoted by $\Gamma_{cr}$ and is subjected to a body force $\mathbf{g}$.  Let material points in the the undeformed and the deformed configuration be denoted by $\mathbf{X}$ and $\mathbf{x}$, respectively. Let $\mathbf{u}\left(\mathbf{X}, t\right)=\mathbf{x}\left(\mathbf{X}, t\right)-\mathbf{X}$ denote the displacement of material point $\mathbf{x}$ at time $t$.    Equilibirum of $\Omega$ is governed by
\begin{equation}
\label{eqn:equil}
\rho_s \ddot{\mathbf{u}} =\nabla \cdot \boldsymbol{\sigma} +\mathbf{g},\:\forall \mathbf{x}\in\Omega, t\geq0,
\end{equation}
in which $\ddot{\mathbf{u}}=\ddot{\mathbf{u}}\left(\mathbf{x}, t\right)$ denotes the second partial derivative of the displacement field and $\rho_s$ is the density of the rock mass.


\input{../ex_template.tex}
\usepackage{setspace}

\title{BPP Exercise 11 - NumPy and Matplotlib}
\author{A. Hain, M. Nipshagen}
\date{18.06.2018, 10:00}

\makeatletter
\let\thetitle\@title
\let\theauthor\@author
\let\thedate\@date
\makeatother

\newcommand\itemsub[1]{
	\begin{itemize}
		\item #1
	\end{itemize}
}

% do not include solutions
% \renewcommand\sol[1]{}

\begin{document}

The deadline for this exercise sheet is \textbf{Monday, \thedate.}

\section*{Introductory Words}
Remember that you need proper documentation to pass
the homework. The documentation doesn't need to be \textit{perfect}, but
everything that needs a docstring, should have a docstring.\\

\noindent Please make sure that your libraries are up to date. For numpy your
version must be $\geq$ 1.13 and for matplotlib $\geq$ 2.2. You can check the
installed version either using pip: \verb|pip show numpy matplotlib| or
directly in Python:
\begin{python}
import numpy as np
import matplotlib as mpl

print("Numpy:", np.__version__)
print("Matplotlib:", mpl.__version__)
\end{python}
If you need to upgrade either, use conda or pip:
\verb|conda install numpy matplotlib| or \verb|pip install numpy matplotlib|.
This should install the latest version.

\section{Warm-Up}
\subsection{Number Stuff with NumPy}
Given the following ndarrays:
\begin{python}
import numpy as np

a = np.array([1, 2, 3])
b = np.array([-1, 2, -3])
c = np.arange(1, 10).reshape(3,3)
d = np.array([[1,2],[-2,-1],[5,5]])
\end{python}
calculate the following operations. Order matters.
\begin{itemize}
	\item The dot product of a and b
	\sol{\pythoninline{np.dot(a, b)}}
	\item The dot product of c and a
	\sol{\pythoninline{np.dot(c, a)}}
	\item The matrix multiplicative of c and d
	\sol{\pythoninline{np.matmul(c,d)}}
	\item The matrix multiplicative of the transpose of d and c
	\sol{\pythoninline{np.matmul(d.T, c.T)}}
	\item the overall mean of a and b together
	\sol{\pythoninline{np.mean([a,b])}}
\end{itemize}

\sol{\pythonexternal{solution/numpy_stuff.py}}

\subsection{Plot, plot away}
\subsubsection{Crossed out}
Plot the following functions from $-4$ to $8$ into one figure.
Use at least 100 points.\\
\emph{Note:} You only need to create \textit{one} ndarray for the x-coordinates.
\begin{itemize}
	\item $y_1 = 2x - 2$
	\item $y_2 = -2x + 6$
	\item $y_3 = 0.25(x - 2)^2 + 2$
	\item $y_4 = -0.25(x - 2)^2 + 2$
\end{itemize}

\subsubsection{Markers all around}
From NumPy's random module use the normal function to create 1000 randomly
drawn numbers. Draw from a normal distribution with center (loc) 0 and a
standard deviation (scale) of 5. Then plot those numbers into a histogram,
with 25 bins, and a bar width of 0.9.\\
Have a look at the numpy documentation for
\href{https://docs.scipy.org/doc/numpy/reference/generated/numpy.random.normal.html#numpy.random.normal}{random.normal}.

\subsubsection{Who is that plot?!}
In the accompanying .zip archive you can find the \verb|plot_me| module.
Import the \verb|get_x_y| function from it. The function will return two
numpy ndarrays in the form \verb|x, y|, which are x and y coordinate pairs.
Plot those pairs using a scatter plot, with a scale (\verb|s|) of 0.5.\\
\emph{Note:} The function can take a few seconds to compute.

\sol{\pythonexternal{solution/plot_plot_away.py}}

\section{The great Plotting}
\subsection{Wavy Waves}
Using your new gained knowledge, recreate the figure in the image
below \ref{fig:sines}.\\
The graphs are plotted from $-2\pi$ to $2\pi$. You do not have to modify
the ticks on the x-axis. If you want to recreate those as well, look into
\href{https://matplotlib.org/api/_as_gen/matplotlib.pyplot.rc.html}{the rc doc},
which allows you to set all kind of formatting. Especially interesting is the
\texttt{usetex} boolean property of \texttt{text}. Also see
\href{https://matplotlib.org/users/usetex.html}{this explanation}.

\begin{figure}[h!]
	\makebox[\textwidth][c]{\includegraphics[width=1.5\textwidth]{sines}}
	\caption{Three sine waves with different properties}
	\label{fig:sines}
\end{figure}

\subsection{Bonus: All alive.}
Let's bring this plot to life! Animate the sine waves in such a way, that
the top left sine wave moves to the left and the top right sine wave moves
to the right. Wave-y!
\subsection{Bonus-Bonus: Wave the wave}
Now that we got that animation up and going, let's animate the bottom sine
wave as well. This time not just left and right, but up and down as well.
Move the whole sine wave on a sine wave. So that the whole curve shifts to
the left as well as up and down as if it was moving along another sine wave.\\
This is a tricky one.

\sol{\pythonexternal{solution/the_great_plotting.py}}

\end{document}


\documentclass[notitlepage]{problem-solving}

\usepackage{mathtools}

\author{Matt McCarthy}
\date{June 2016}
\title{Generating Monoids from Categories}

\begin{document}

\maketitle

\begin{thm*}
	Let $C$ be a category and let $A$ be an object in $C$.
	Then $C[A,A]$ forms a monoid under arrow composition.
\end{thm*}

\section{Background}

\begin{definition}[Category]
	A \textit{category}, $C$ consists of the following.
	\begin{enumerate}
		\item A class of \textit{objects}, denoted $Obj(C)$.
		\item A class of \textit{arrows}, denoted $Arr(C)$.
		Each arrow $f\in Arr(C)$ has a source object $A\in Obj(C)$, a target object $B\in Obj(C)$, and is denoted $f:A\rightarrow B$.
		We denote the class of all arrows going from $A\in Obj(C)$ to $B\in Obj(C)$ as $C[A,B]$.
		\item A partial composition $\circ:Arr(C)\times Arr(C)\rightarrow Arr(C)$ such that for any $f:A\rightarrow B,g:B\rightarrow D$, $gf:A\rightarrow D\in Arr(C)$.
	\end{enumerate}
	Furthermore, the following axioms must hold.
	\begin{enumerate}
		\item For all $f:A\rightarrow B,g:B\rightarrow D,h:D\rightarrow E\in Arr(C)$, $h(gf) = (hg)f$.
		\item For all $A\in Obj(C)$, there exists an $id_A\in C[A,A]$ such that for all arrows $f:X\rightarrow A$, $g:A\rightarrow Y$ $id_A\, f =f$ and $g\, id_A = g$.
	\end{enumerate}
\end{definition}

\begin{definition}[Monoid]
	Let $M$ be a set, and let $*:M^2\rightarrow M$ be a binary operation.
	Then $(M,*)$ forms a \textit{monoid} if all of the following are satisfied.
	\begin{enumerate}
		\item For all $a,b,c\in M$, $a*(b*c)=(a*b)*c$.
		\item There exists an $e\in M$ such that for all $a\in M$, $e*a=a*e=a$.
	\end{enumerate}
\end{definition}

\section{Solution}

\begin{thm}
	Let $C$ be a category and let $A$ be an object in $C$.
	Then $C[A,A]$ forms a monoid under arrow composition.
\end{thm}
\begin{proof}
	Let $f,g\in C[A,A]$.
	Then
	\[
		A \xrightarrow{f} A \xrightarrow{g} A
	\]
	and thus
	\[
		A \xrightarrow{fg} A.
	\]
	Therefore, arrow composition forms a binary operation on $C[A,A]$.

	Next, we claim that $id_A$ is the identity for $C[A,A]$ with respect to arrow composition.
	Let $f\in C[A,A]$.
	Then, by definition, we know that $f\, id_A = id_A\, f = f$.
	Thus, $id_A$ is the identity for $C[A,A]$ with respect to arrow composition.

	Lastly, we must show that arrow composition is associative for all arrows in $C[A,A]$.
	Let $f,g,h\in C[A,A]$.
	Consider $f(gh)$.
	\[
		A \xrightarrow{f(gh)} A = A\xrightarrow{gh} A \xrightarrow{f} = A\xrightarrow{h} A\xrightarrow{g} A\xrightarrow{f} A
	\]
	Now consider $(fg)h$.
	\[
		A \xrightarrow{(fg)h} A = A\xrightarrow{h} A\xrightarrow{fg} A = A\xrightarrow{h} A\xrightarrow{g} A\xrightarrow{f}A
	\]
	Therefore, $(fg)h=f(gh)$ and $C[A,A]$ is associative with respect to arrow composition.
	Thus, $C[A,A]$ forms a monoid under arrow composition.
\end{proof}

\end{document}


\section{Optics}
    \subsection{Traveling Waves}
    Let's first just consider a sine wave. We see that $y=sin(x)$. Let's say that the y-axis is the vertical displacement vertically, and x-axis is the horizontal displacement. The amplitude is $y_0$ and $-y_0$. Our original equation needs a few fixes.
    \begin{align*}
        y&=y_0sin(x)\\
        \shortintertext{Because you cannot take the sin of something in meters, we need to change this to be an angle. Pme full oscilation coresponds to $2\pi rad$ or $\lambda$ distance}\\
        y&=y_0sin\left(2\pi\frac{x}{\lambda}\right)\\
        \shortintertext{Sometimes we use the parameter called the wave number:}\\
        k&=\frac{2\pi}{\lambda}\\
        y&=y_0sin(kx)
    \end{align*}
    What if also the wave moves to the right at speed $v$ (figure 6.16)? Let's take advantage of inertial frames. Let the prime axis ($x'$ and $y'$) travel \underline{with} the wave. In the prime frame, $v'=0$. We can write $y'=y_0'sin(kx')$. Now we want to get $y$ based on $y'$. We can see that $y_0=y'_0$. What about $x$ and $x'$?
    \begin{align*}
        \Delta x&=v\Delta t=v(t-0)\\
        x-x'&=vt\\
        \shortintertext{Both sides of this equation provide a positive displacement. And now to get $y$ from $y'$, we will sub out x' for x.}
        y'&=y_0sin(kx')\\
        &=y_0sin(kx')\\
        y&=y_0sin(k(x-vt))\\
        y&=y_0sin(kx-kvt)\\
        \shortintertext{Let's look at $kv$}\\
        \to kv&=\frac{2\pi}{\lambda}v\\
        &=\frac{2\pi}{\lambda}\frac{\lambda}{T}\\
        &=\frac{2\pi}{T}\\
        y&=y_0sin\left(\frac{2\pi}{\lambda}x-\frac{2\pi}{T}t\right)\\
        \shortintertext{Notice that $\frac{2\pi}{T}=2\pi f=\omega$ Where $\omega$ is the angular frequency.}\\
        y&=y_0sin(kx-\omega t)\\
        \shortintertext{Recall from 1D kinematics: $x=x_0+v_0t+\frac{1}{2}a_xt^2$}
        \shortintertext{We will add a phase constant $\Phi_0$}\\
        \to y(x,t)&=y_0sin(kx-wt+\phi_0)
        \shortintertext{The total phase is:}\\
        \phi&=kx-\omega t+\phi_0\\
    \end{align*}
    \subsubsection{Example 1}
    The following $\vec{E}$ and $\vec{B}$ fields satisfy this set of four equations (figure 7.1)
    \begin{align*}
        \vec{E}&=E_0sin(kz-\omega t)\hat{i}\\
        \vec{B}1&=B_0sin(kz-\omega t)\hat{j}\\
        \shortintertext{The pointing vector tells us the direction and magnitude of this combined electromagnetic wave. We must cross $\vec{B}$ and $\vec{E}$ in order to get the correct direction. In this case we are going to to $\vec{E}\times\vec{B}$}\\
        \vec{S}&\approx\vec{E}\times\vec{B}\\
        \shortintertext{In order to make this an equality we must add $\frac{1}{\mu_0}$.}\\
        \alignedbox{\vec{S}}{=\frac{1}{\mu_0}\vec{E}\times\vec{B}}\\
        \shortintertext{This means that electric fields do not need a median in order to move. Now let's figure out how fast this electromagnetic wave moves. We recently saw in class that $kv=\omega$. For this case we do find that $v=\frac{\omega}{k}=\frac{1}{\sqrt{\mu_0\epsilon_0}}$. It was immediately noticed that light traveled at exactly this value. This is denoted as:}\\
        c&=\frac{1}{\sqrt{\mu_0\epsilon_0}}\approx3\times10^8\frac{m}{s}\\
        \shortintertext{It turns out that light is an electromagnetic wave! Optics is the applied study of electromagnetic waves.}
    \end{align*}

    \subsection{Electromagnetic Spectrum}
    For light,
    \begin{align*}
        c=\frac{\omega}{k}=\frac{2\pi f}{\frac{2\pi}{\lambda}}\\
        c=\lambda f\\
        \shortintertext{Notice that we can change the wavelength or the frequency but the product must result in c. This gives us a spectrum or wavelengths we can choose.}
    \end{align*}
    In terms of wavelength, humans can see from violet $(\sim400nm)$ up to red $(\sim600nm)$.  In terms of frequency, red is about $1.21\times10^34$ and violet is around $7.86\times10^31$

    \subsection{Creating EM waves}
    Figure 7.2. I flip the switch up and change the capacitor to $Q_0=cv_0$. Then I flip the switch to exclude the battery. The new circuit just excludes the battery. The charges are going to flow cw through the inductor, which rejects the current. The charges are going to equilibriate and the current is going to weaken. This is an oscilating current.
    \begin{align*}
        \omega&=\frac{1}{\sqrt{lc}}\\
        f&=\frac{1}{2\pi}\frac{1}{\sqrt{lc}}\\
    \end{align*}

    \subsection{Pointing Vectors}
    Recall that was saw that we can make an EM wave with an LC oscillator circuit. The energy stored in an electric field in a capacitor is:
    \begin{equation*}
        U_E=\frac{1}{2}CV^2
    \end{equation*}
    The energy density can be written more generally as, where $u_E$ is the energy density and $U_B$ is the potential energy:
    \begin{equation*}
        u_E=\frac{U_E}{volume}=\frac{1}{2}\epsilon_0E^2
    \end{equation*}
    The energy stored in a magnetic field in an inductor is:
    \begin{equation*}
        U_B=\frac{1}{2}LI^2
    \end{equation*}
    The energy density can be writted more generally as:
    \begin{equation*}
        u_B=\frac{U_B}{volume}=\frac{1}{2}\frac{1}{\mu_0}B^2
    \end{equation*}
    Consider figure 7.1 again. On the EM wave, the energy density is:
    \begin{align*}
        u&=u_E+u_B\\
        u&=\frac{1}{2}\epsilon_0E^2+\frac{1}{2}\frac{1}{\mu_0}B^2\\
        u&=\frac{1}{2}\epsilon_0(cB)^2+\frac{1}{2}\frac{1}{\mu_0}B^2\\
        \shortintertext{Recall that $c=\frac{1}{\sqrt{\epsilon_0\mu_0}}$}\\
        u&=\frac{1}{2}\epsilon_0\frac{1}{\epsilon_0}B^2+\frac{1}{2}\frac{1}{\mu_0}B^2\\
        u&=\frac{1}{\mu_0}B^2\\
        \shortintertext{Equivalently, because $B=\frac{E}{c}$:}\\
        \to u&=\frac{1}{\mu_0}\frac{E^2}{c^2}\\
        &=\frac{1}{\mu_0}\epsilon_0\mu_0E^2\\
        \alignedbox{u}{=\epsilon_0E^2}\\
        &\text{Energy density in EM wave}
    \end{align*}
    An electromagnetic wave transports energy in its EM fields. This energy transport is called the pointing vector $\vec{S}$.
    \begin{align*}
        S&=\frac{energyfluxthroughareaintime\Delta t}{A\Delta t}\\
        &=\frac{1}{\mu_0}EB\\
        \alignedbox{\text{In Vector Form, }\vec{s}}{=\frac{1}{\mu_0}\vec{E}\times\vec{B}}
    \end{align*}
    $u(z,t)$ is the wave's energy density, so if i want the energy transport in time, we must multiply it by the volume to get energy.
    \begin{align*}
        u*volume&=u*A\\
        S&=\frac{uAc\Delta t}{A\Delta t}=uc\\
        &=\epsilon_0E^2c=\epsilon_0(cE)(E)
    \end{align*}
    When we see light, our eye is detecting the EM waves in the pointing vector. Our brain arranges out the fast oscillations. So essentially, the intensity of light we see is the time-average pointing vector. Intensity is script i:
    \begin{align*}
        i&=S_{avg}=\left(\frac{1}{\mu_0}EB\right)_{avg}\\
        &=\left(\frac{1}{\mu_0}E\frac{E}{c}\right)_{avg}\\
        &=\frac{1}{\mu_0c}(E^2)_{avg}\\
        &\neq\frac{1}{\mu_0}(E_{avg})^2\\
        E&=E_0sin(kz-\omega t)\\
        E_{avg}&=0\\
        (E^2)_{avg}&=E_0^2(sin^2(kz-\omega t))_{avg}=\frac{1}{2}E_0^2\\
        i&=\frac{1}{\mu_0c}\frac{E_0^2}{2}\\
        \shortintertext{We can also write:}\\
        \sqrt{(E^2)}_{avg}&=E_{rms}\\
        E_{rms}&=\frac{E_0}{\sqrt{2}}
    \end{align*}
    More on intensity. The general definition of intensity is:
    \begin{equation*}
        \mathscr{I}=\frac{P}{A}
    \end{equation*}
    Light has no mass, and there is no mass in electromagnetic fields. Although it doesn't have math, it had momentum and pressure. Radiation pressure $(P_{rad}$):
    % First PRAD below is power radiation, after \frac{F_{rad}c}{A} P_rad changes to rad pressure
    \begin{align*}
        P_{rad}&=\frac{dW}{d}=\frac{\vec{F_rad}\cdot d\vec{z}}{dt}=\frac{F_{rad}dz}{dt}\\
        P_{rad}&=F_{rad}c\\
        \mathscr{I}&=\frac{P}{A}\\
        &=\frac{F_{rad}c}{A}\\
        &=\mathscr{P}_{rad}c\\
        \to P_{rad}&=\frac{\mathscr{I}}{c}\\
    \end{align*}
    Note: $F=ma$ does not apply here, but Newton's second law, $\vec{F}_{net}=\frac{d}{dt}\vec{p}$. For a massive object,
    \begin{align*}
        \vec{p}&=m\vec{v}\\
        \vec{F}_{net}&=\frac{d}{dt}(m\vec{v})\\
        &=m\frac{d}{dt}\vec{v}\\
        &=m\vec{a}\\
    \end{align*}
    Here, we do not have mass in the EM wave. We cannot use $\vec{F}=m\vec{a}$ in any calculations.\newline\newline
    Let's consider an EM wave that is incident on a surface. How much force will be exerted on the surface when the wave hits it? This depends on whether the wave is absorbed or reflected.
    \begin{align*}
        \vec{F}&=\frac{\Delta \vec{p}}{\Delta t}\\
        \shortintertext{If the wave is reflected, how must $\Delta\vec{p}$ look? Let's call the initial momentum $\vec{p}_0$.}\\
        \Delta\vec{p}&=\vec{p}_+-\vec{p}_0\\
        &=-2\vec{p}_0\\
        \shortintertext{By contrast, during an absorbtion,}
        \Delta\vec{p}&=\vec{p}_f-\vec{p}_i\\
        \Delta\vec{p}&=-\vec{p}_0\\
    \end{align*}
    With this, consider:
    \begin{align*}
        \mathscr{P}_{rad}&=\frac{\mathscr{I}}{c}\\
        \frac{F_{rad}}{A}&=\frac{\mathscr{I}}{c}\\
        \alignedbox{F_{rad}}{=\frac{\mathscr{I}A}{c}=\frac{S_{avg}A}{c}\text{ Absorbtion}}\\
        \shortintertext{For reflection we can just update the derivation:}\\
        \alignedbox{F_{rad}}{=2\frac{\mathscr{I}A}{c}=2\frac{S_{avg}A}{c}\text{Reflection}}\\
    \end{align*}
    If you would like to use EM waves to exert a force on something, it is twice as efficient to have the wave reflect instead of absorb. For example, a solar sail. Consider a satalite in space that has a solar sail that is being pushed by the electromagnetic waves of the sun. The sunlight will exert a force with reflection. Keep in mind that $A$ is the sail's cross sectional area.
    \begin{align*}
        F_{rad}&=2\frac{\mathscr{I}A}{c}\\
        \shortintertext{This is exerted on the sail}\\
        2\frac{\mathscr{I}A}{c}&=ma\\
        a&=\frac{2\mathscr{I}A}{mc}\\
    \end{align*}

    \subsection{Polarization of Light}
    With a normal electromagnetic wave, $\vec{E}=E\hat{i}=E_0sin(kz-\omega t+\mathscr{P}_0)\vec{i}$ and $\vec{B}=B\hat{j}=B_0sin(kz-\omega t+\mathscr{P}_0)\hat{j}$. When such a wave $\left(\vec{S}=\frac{1}{\mu_0}\vec{E}\times\vec{B}\right)$ reaches a surface, that surface can polarize the light. Light from the sun and lightbulds are unpolarized. A \underline{polaroid} is a thin material that polarizes light. It does so by building long parallel chains of molecules. When sunlight reflects of the road, it is largely polarized in plane with the road. So windshields are polarized vertically to block thin horizontal glare.
    \subsubsection{Example 1}
    This is an intensity example. A $18W$ light bulb is 1m away from a tennis ball (diameter of 12cm). How much energy has the tennis ball absorbed. Assume that the ball absorbs 70 percent of incident energy (figure 7.3). Let's assume the light bulb is isotropic (the same in all directions). Let's assume theres no reflection off the table for simplicity. How do we determine how much light goes towards the ball? First lets determine which variables cover which units. r is going to be the distance from the ball, and $r_{ball}$ is the radius of the ball. $P_{light} = 18W$, $r=1m$, $r_{ball}=6cm=0.06m$, $t=1hr=3600s$. More generally, let's say that $P_{light}=P_{src}=18W$. First we need to determine the amount of power incident on the ball ("absorbed") is $P_{inc}=P_{src}\frac{a_{ball}}{a_{shell}}$. This is where $a_{ball}$ is the cross sectional area and $a_{shell}$ is the surface area of the sphere.
    \begin{align*}
        P_{inc}&=P_{src}\frac{a_{ball}}{a_{shell}}\\
        &=P_{src}\frac{\pi r_{ball}^2}{4\pi r^2}\\
        \alignedbox{P_{inc}}{=\frac{1}{4}P_{src}\frac{r_{ball}^2}{r^2}}\\
        \shortintertext{This is the power incident on the tennis ball, but how intense is the light at the tennis ball?}\\
        \mathscr{I}_{attheball}=\frac{P_{src}}{A_{shellattheball}}=\frac{P_{src}}{4\pi r^2}\\
        \shortintertext{Notice that $P_{inc}=\mathscr{I}_{src}a_{ball}$. The energy absorbed in one hour is $\mathscr{E}=P_{inc}t$}\\
        \mathscr{E}&=\frac{0.7}{4}P_{src}\frac{r_{ball}^2}{r^2}t\\
        \mathscr{E}&=\frac{0.7}{4}(18W)\frac{(0.6m)^2}{(1m)^2}(3600s)\\
        \alignedbox{\mathscr{E}}{=40.8J}\\
    \end{align*}

    \subsection{Geometric or Ray Optics}
    Reflection: consider a bullet. If we shoot a gun such that it hits the floor, it will bounce back (reflect off the surface) at a 90 degree angle from the angle it came in at. This is due to Newton's second law, every action must have an equal and opposite reaction (figure 7.4).
    \begin{align*}
        \vec{F}&=m\vec{a}\\
        \vec{F}&=\frac{\Delta\vec{p}}{\Delta t}\\
    \end{align*}
    The law of reflection states that $\theta_1=\theta_1'$. Let's now consider light incident on a still surface of water (figure 7.5). The law of refraction is known as snell's law:
    \begin{align*}
        n_1sin\theta_1&=n_2sin\theta_2\\
        \shortintertext{n is the index of refraction. The value of n depends on the medium}\\
        n&=\frac{c}{v}\\
        \shortintertext{For gasses a good approximation for n is 1. This also applies in a vaccum}\\
        n_{water}&=1.333\\
        n_{diamonds}&=2.42\\
    \end{align*}
    The incident angle at which light will totally internally reflect is called the critical angle $(\theta_c)$. Now we are going to figure out what $\theta_c$ is.
    \begin{align*}
        n_1sin\theta_c&=n_2sin90^o\\
        \theta_c&=arcsin\frac{n_2}{n_1}\\
        \shortintertext{For diamond to air,}\\
        \theta_c&=arcsin\frac{1}{2.42}\\
        &=24.4\si{\degree}\\
    \end{align*}
    A great application of this idea is fiber optic cables. For wavelets in Refraction we will wait some time $(\Delta t)$ for the wavelength to travel into the water. Keep in mind that light travels faster in air than in water.

    \subsection{Thin Lens Refraction}
    Look at figure 7.7. Here are the rules for ray diagrams (thin lenses):
    \begin{itemize}
        \item A raythrough the center of the lenz from the object goes straight through the lens.
        \item Parallel rays go through the focal point.
    \end{itemize}
    The crossing point from any two rays gives the image location. Real images are inverted. The thin lens equation is the following:
    \begin{equation*}
        \frac{1}{f}=\frac{1}{d_0}+\frac{1}{d_i}
    \end{equation*}
    For this problem, let's find $d_i$.
    \begin{align*}
        \frac{1}{f}-\frac{1}{d_0}&=\frac{1}{d_i}\\
        d_i&=\left(\frac{1}{f}-\frac{1}{d_0}\right)^{-1}\\
        &=\left(\frac{1}{f}+\frac{1}{-d_0}\right)^{-1}\\
        &=\frac{f(-d_0)}{f+(-d_0)}\\
        d_i&=\frac{fd_0}{d_0-f}\\
        &=\frac{(15cm)(46cm)}{46cm-15cm}=22.3cm\\
    \end{align*}
    With my ray diagram, I get 21.5cm, which is pretty close to 22.3cm
    \subsubsection{Example 1}
    Here $f=15cm$ and $d_0=10cm$. This is figure 7.8. This gives is a virtual image. Virtual images are upright and cannot be seen/projected on a screen. Again, $\frac{1}{f}=\frac{1}{d_0}+\frac{1}{d_i}$ gives:
    \begin{align*}
        d_i&=\frac{fd_0}{d_0-f}\\
        d_i&=\frac{(15cm)(10)}{(10cm)-(15cm)}=-30cm
    \end{align*}
    By hand on the board, we got -33cm. With a pen on gridpaper, we should be within 0.5cm.
    \newline\newline
    The ratio of image height to object (with a minus sign) is called magnification.
    \begin{equation*}
        m=\frac{h_i}{h_0}=\frac{-d_i}{d_0}
    \end{equation*}
    Here we have $m=\frac{h_i}{h_0}=\frac{37cm}{11.5cm}=3.2$ also, if we calculate using $d_i$ and $d_0$, $m=-\frac{-33cm}{10cm}=3.3$
    \newline\newline
    Diverging lens ray diagram (figure 7.9). Must treat f as negative.
    \begin{align*}
        d_i=\frac{fd_0}{d_0-f}=\frac{(-15cm)(47cm)}{47cm-(-15cm)}
    \end{align*}
    I measure -11.4cm. This matches exactly. Now to calculate the magnification:
    \begin{align*}
        m&=\frac{h_i}{h_0}=\frac{-d_i}{d_0}\\
        &=\frac{2.5cm}{10cm}\text{ or }-\frac{-11.4cm}{47cm}\\
        &= 0.25
    \end{align*}

    \subsection{Wave Interference}
    Let's recall from last class that we treated light as particle-like. For example, ray tracing. However at times, light acts like a wave. For context, let's consider water waves. Both waterwaves and light waves have interference. Let's think about an interferometer (figure 7.10). The interference equation is the following:
    \begin{equation*}
        differenceinpathlength=integernumberof\lambda s
    \end{equation*}
    For our laser interferometer, this equation will give constructive interference.
    \begin{equation*}
        2d_1-2d_2=m\lambda
    \end{equation*}
    Where $m$ is the integer from above. Now for destructive interference:
    \begin{align*}
        2d_1-2d_2=m\lambda+\frac{1}{2}\lambda\\
        2d_1-2d_2=\left(m+\frac{1}{2}\right)\lambda
    \end{align*}
    let's discuss more of what happens at an interface. Consider again light refracting from air to water. We know that $n_{air}=1$ and $n_{water}=1.33$. $n=\frac{c}{v}$. In waves, $v=\lambda f$. If in air or a vacuum, $c=\lambda f$, but in water $v=\lambda f$. Either $\lambda$ or $f$ needs to change because of the change in medium.
    \begin{align*}
        \frac{c}{n}&=\frac{\lambda f}{n}\\
        \shortintertext{Do we want $\frac{c}{n}=\frac{\lambda}{n}f$ or $\frac{c}{n}=\lambda\frac{f}{n}$?}\\
        \shortintertext{We cannot have the second option because frequency \underline{must} be constant across the boundary. There is no way for the frequency to change as it goes through an interface. If this were the case then the frequency would increase when it comes back in contact with air.}
    \end{align*}
    \subsubsection{Example 2}
    This involes thin film interference. On a soap bubble there are many different colors that reflect off of the bubble. We are going to assume that the soapy water has $n= 1.4$. Some of the light will reflect and some of the light will refract and then reflect off the inner surface. Recall that the interference equation is $difinpathlength=integernumberof\lambda s$.
    \begin{align*}
        2t&=m\lambda\\
        \shortintertext{This is on the right track but an important detail is missing.}
    \end{align*}
    In order to determine what this important detail thats missing is, we must consider first a wave phase shift. With a heavy rope knotted with a light rope, we can see that the wave from the rope is refracted, not reflected.\newline\newline
    The following is true for light:
    \begin{itemize}
        \item From high n to low n, there is no phase change upon reflection.
        \item From low n to high n, there is a $180\si{\degree}$ or $\pi rad$ or flip in phase, or $\frac{1}{2}\lambda$
        \item For refraction/transmission, there is never a phase change.
    \end{itemize}
    Earlier, we found $2t=m\lambda$. But we must still account for phase flips for reflections.
    \begin{align*}
        difinpathlength&=integernumberof\lambda s\\
        2t+\frac{1}{2}\lambda&=m\lambda\\
        \shortintertext{Because the changeing of the color of the light occurs within the soapy bubble, we are going to use the wavelength of light in soapy water on both sides of our equation.}
        \to 2t+\frac{1}{2}\frac{\lambda_0}{n_{s.w.}}&=m\frac{\lambda_0}{n_{s.w.}}\\
        2t&=\left(m-\frac{1}{2}\right)\frac{\lambda_0}{n_{s.w.}}\text{ constructive}\\
        2t&=\left(m-\frac{1}{2}\right)\frac{\lambda_0}{n_{s.w.}}+\frac{1}{2}\frac{\lambda_0}{n_{s.w.}}\\
        2t&=m\frac{\lambda_0}{n_{s.w.}}\text{ destructive}
        \shortintertext{Both the constructive and destructive are for $m=1,2,3,4,$.}
    \end{align*}

    \subsection{Slit Interference}
    For a single slit diffraction, $\frac{a}{2}sin\theta=m\lambda$ for construction, $m=0,1,2$. For destructino, it is $asin\theta=m\lambda$, $m=0,1,2$. For double slit interference, the contructive equation is $dsin\theta=m\lambda$ for $m=0,1,2$. Destructive is $dsin\theta=\left(m+\frac{1}{2}\right)\lambda$. Notice that $sin\theta=\frac{y_m}{\sqrt{y_m^2+D^2}}$.

    \subsection{Crystallography}
    Also called Xr-ray diffraction or bragg diffration. The following is a basic crystal (figure 7.11).
    \begin{align*}
        differenceinpathlength&=intnum\lambda\\
        \alignedbox{2dsin\theta}{=m\lambda}
    \end{align*}
    \subsection{Electron Diffraction}
    Consider a single-slit setup. When shooting electrons at the slit, we also see an interference pattern. Electrons, then must also have wave characteristics. deBroglie:
    \begin{equation*}
        \lambda_c=\frac{h}{p}
    \end{equation*}
    Where $h$ is the Planchs constant.

    \subsection{Special Relativity}
    \begin{itemize}
        \item 1860s Maxwell unifies electricity and magnetism.
        \item Light is an electromagnetic wave
        \item Waves require a medium
        \item In 1877, Nichelsen and mosley try to measure the luminous ether, the medium in which light must travel. They measured no change.
    \end{itemize}
    Einstein came up with two postulates to try to understand everythign that doesn't make sense (1905). The Principle of Relativity: The laws of physice are the same in all inertial (non-accelerating) frames. Invariance of c. Signals don't arrive instantaneously but propogate. Thus, there must be a maximum universal speed.
    \subsubsection{Example}
    Let's look at a light clock (figure 7.12). The left side of the figure is when the light clock is at rest. We can derive that $t_0=\frac{2L_0}{c}$. The right side of the figure is when the light clock is at speed. We know that the light is moving at $\sqrt{c^2 + u^2}$. Now let's put this into terms of light:
    \begin{align*}
        t&=\frac{2\sqrt{L_0^2+\left(\frac{ut}{2}\right)^2}}{\sqrt{c^2+u^2}}\\
        &=\frac{2\sqrt{\left(\frac{ct}{2}\right)^2+\left(\frac{ut}{2}\right)^2}}{\sqrt{c^2+u^2}}\\
        &=\frac{2\sqrt{\left(\frac{1}{2}\right)^2\left[(ct)^2+(ut)^2\right]}}{\sqrt{c^2+u^2}}\\
        t&=\frac{t\sqrt{c^2+u^2}}{\sqrt{c^2+u^2}}\\
        t&=t\\
        \shortintertext{However, this is \underline{NOT} reality. From a special relativity (SR) approach things are different. Light always travels at c in \underline{all} inertial reference frames. We can use the same figure as before but we much do a different analysis. Becuase the speed of light is always c, our analysis triangles from before does not work. The way the velocity vectors combine is differnet.}\\
        t&=\frac{2\sqrt{L_0^2+\left(\frac{ut}{2}\right)^2}}{c}\\
        t&=\frac{2\left(\frac{ct_0}{2}\right)^2\left(\frac{ut}{2}\right)^2}{c}\\
        (ct)^2&=(ct_0)^2+(ut)^2\\
        (ct)^2-(ut)^2&=(ct_0)^2\\
        t^2(c^2-u^2)&=(ct_0)^2\\
        t^2&=\frac{(ct_0)^2}{c^2-u^2}\\
        t&=\frac{ct_0}{\sqrt{c^2-u^2}}\\
        \alignedbox{t}{=t_0\frac{1}{\sqrt{1-\frac{u^2}{c^2}}}}
        \shortintertext{This is called Time Dilation. Time panes move slowly for moving objects.}
    \end{align*}
    Now let's say the clokc ticks at rest with a time of $t_0=1sec$. In order to make the clock tick one percent slower, that is, $t=1.01sec$, the clock must move at a speed of $42,000,000\frac{m}{s}$!
\newpage


\subsection{Frechet spaces}\label{subsec:frechet_spaces}

\begin{definition}\label{def:frechet_space}\mcite[1.8 (f)]{Rudin1991Functional}
  An \term{F-space} is a \hyperref[thm:uniform_space_completion]{complete} \hyperref[def:metric_topology]{metrizable} \hyperref[def:topological_vector_space]{topological vector space}. We can assume that an F-space is a tuple \( (\mscrX, \rho) \), where \( \rho \) is a \hyperref[def:complete_metric_space]{complete} \hyperref[def:translation_invariant_metric]{translation-invariant} \hyperref[def:metric_space]{metric}.

  A \term{Frechet space} is a \hyperref[def:locally_convex_space]{locally convex} F-space.
\end{definition}


% \documentclass[10pt]{article}
% \usepackage{amsmath}
% \usepackage{amssymb}
% \usepackage[margin=1in]{geometry}
% \setlength\parindent{0pt}
%
% \begin{document}
% \noindent

\section{Dispersion of Matter Waves}
An equation which is closely related to the wave equation is the Schrodinger equation:

$$i\frac{\partial\psi(x,t)}{\partial t}=-\frac{1}{2m}\frac{\partial^{2}\psi(x,t)}{\partial x^{2}},$$

where $m$ is a positive real number. Here $\psi(x,t)$ is a \it wavefunction \rm that describes the movement of a quantum mechanical matter wave with mass $m$. Much like the ordinary wave equation, the Schrodinger equation also admits traveling wave solutions of the form

$$\psi_{p}(x,t)=e^{ip\left(x-\frac{pt}{2m}\right)},$$

where $p\in\mathbb{R}$ is an arbitrary real number.

\begin{enumerate}[label=\alph*)]
\item (3 pts) What is the dispersion relation for the wave solutions of the Schrodinger equation? Give a brief sketch.

\item (2 pts) What is the phase velocity of these traveling waves?

\item (3 pts) Let us consider beats formed using a superposition of different wave solutions with similar frequencies. Let $p_{0}$ be a given real number. Consider a superposition of the form

$$\xi(x,t)=\psi_{p_{0}-\Delta p}(x,t)+\psi_{p_{0}}(x,t)+\psi_{p_{0}+\Delta p}(x,t),$$

where $\Delta p$ is some small number such that $\Delta p\ll p_{0}$. To linear order in $\Delta p$, show that we have

$$\xi(x,t)=\left(2\cos\left[\left(x-\frac{p_{0}}{m}t\right)\Delta p\right]+1\right)\psi_{p_{0}}(x,t).$$
\\

The previous result shows that, just like in the classical phenomenon of beats, a superposition of traveling matter waves with similar frequencies will give you an “envelope” function that modulates $\psi_{k_{0}}$. A reasonable definition of the group velocity in this case would be the velocity of the envelope function.

\item (2 pts) What is the group velocity of the superposition? Does this match up with what you expected from your knowledge of group velocities for classical waves?
\\

As an aside, the values of p actually correspond to the momentum of the quantum mechanical wave. With this interpretation, you may find your expression for the group velocity to be very familiar.

% \item (2 pts) Now consider these waves to be trapped in a box from $x=0$ to $x=L$. Inside the box, a right-moving wave will bounce off the wall to become a left-moving wave and vice versa. The waves inside the box must therefore be a general combination of a left-moving wave and a right-moving wave which will form a standing wave:
%
% $$f(x,t)=Ae^{ip\left(x-\frac{pt}{2m}\right)}+Be^{-ip\left(x+\frac{pt}{2m}\right)}.$$
%
% Since the particle cannot exit the box, quantum mechanics requires that the above superposition go to zero at the boundaries, i.e., $f(x=0,t)=0$ and $f(x=L,t)=0$. Up to an overall constant, find all the linear combinations which form valid quantum mechanical standing waves. What are the allowed values of $p$?
\end{enumerate}

 % Notice that the act of putting the waves into a box severely restricts the possible values of $p$ the standing wave is allowed to have. The previously unrestricted value of $p$ is now reduced to a discrete set of values. Thus we say that the act of restricting the wave \it quantizes \rm its momentum (and hence also energy). This is a very generic phenomenon in quantum mechanics.
%\end{document}


%!TEX root = main.tex
\paragraph{Fast multipole method}

The fast multipole method (\fmm) is an algorithm that can reduce the quadratic time and space complexity of such matrix-vector multiplication down to $\mathcal{O}(N)$.
In the context of \fmm, $\{\mathbf{r}_i\}$ and $\{\mathbf{r}_j'\}$ in Equation \ref{eq:nbody_sum} are often referred to as the set of targets and sources respectively, with $\{q_j\}$ representing the source densities (charges).
The goal of \fmm is to efficiently compute the potential at $N$ targets $\{s_i\}$ induced by all $N$ sources and the kernel function $g$.
Following the common notations in the literature, we use $\mathbf{x}_i$ and $\mathbf{y}_j$, instead of $\mathbf{r}_i$ and $\mathbf{r}_j'$, to denote targets and sources respectively in this subsection.

The \fmm algorithm builds upon two fundamental ideas: (1) approximating the far-range interactions between distant clusters of sources and targets using low-rank methods, while computing the near-range interactions exactly, and (2) partitioning the domain using a tree structure to maximize the far-range portion in the computation.

To construct the octree, we first create a cube that encloses all sources/targets and then recursively subdivide the domain until each cube at the finest level only contains a constant number of points.
Figure \ref{fig:near_far_decomp} depicts a 3-level quadtree.
The potentials of targets in node $B$ consist of three contributions: the influence from sources in the near-field of $B$: $\mathcal{N}(B)$, in the interaction list of $B$: $\mathcal{I}(B)$, and in the rest of the domain.
$B$'s near-field includes $B$ and its neighbors, where the interactions are computed exactly.
The remaining domain is in $\mathcal{F}(B)$, $B$'s far-field.
In $\mathcal{F}(B)$, the nodes that are the children of $B$'s parent's neighbors but are not adjacent to $B$ compose $\mathcal{I}(B)$, the interaction list of $B$, whose contributions to $B$ are approximated by low-rank methods.
The contributions from the rest of the far-field are approximated at coarser levels via $B$'s ancestors.

The classic \fmm \cite{greengard1987fast, cheng1999fast} relies on truncated analytical expansions to approximate far-field interactions, whereas its kernel-independent variant \cite{ying2004kernel} uses equivalent densities (charges) instead.
In \kifmm, each node is associated with upward and downward equivalent densities (see Figure \ref{fig:multipole} and \ref{fig:local}), the analog of multipole and local expansions in the analytical \fmm.
The upward equivalent densities $q^{B,u}$ are used to approximate the influence of sources in $B$ on targets in $\mathcal{F}(B)$;
the downward equivalent densities $q^{B,d}$ are used to approximate the influence of sources in $\mathcal{F}(B)$ on sources in $B$.
To find these densities, we match the potential of equivalent densities to the potential of actual sources at the check surfaces:
%
\begin{align}\label{eq:multipole_local}
    \sum_{\mathbf{y}_{j} \in B} g\left(\mathbf{x}_{i}^{B,u}, \mathbf{y}_{j}\right) q_{j} &= \sum_{j} g\left(\mathbf{x}_{i}^{B,u}, \mathbf{y}^{B,u}_{j}\right) q^{B,u}_{j}, \quad \forall i  \nonumber \\
    \sum_{\mathbf{y}_{j} \in \mathcal{F}(B)} g\left(\mathbf{x}_{i}^{B,d}, \mathbf{y}_{j}\right) q_{j} &= \sum_{j} g\left(\mathbf{x}_{i}^{B,d}, \mathbf{y}^{B,d}_{j}\right) q^{B,d}_{j}, \quad \forall i
\end{align}
%
We then solve the linear systems for $\{q^{B,u}_{j}\}$ and $\{q^{B,d}_{j}\}$.
Here, $\mathbf{x}_{i}^{B}$ and $\mathbf{y}_{j}^{B}$ denote the discretization points of the check surface and equivalent surface of $B$ respectively.

The algorithm also defines the following operators:
%
\begin{itemize}
    \item particle-to-multipole (P2M): For a leaf node $B$, compute $B$'s upward equivalent densities, \ie multipole expansion, from the sources in $B$. (Figure \ref{fig:multipole})
    \item multipole-to-multipole (M2M): For a non-leaf node $B$, evaluate $B$'s multipole expansion based on the multipole expansions of all $B$'s children. (Figure \ref{fig:translations} left)
    \item multipole-to-local (M2L): For a node $B$, evaluate $B$'s downward equivalent densities, \ie local expansion, by using the multipole expansions of all nodes in $\mathcal{I}(B)$. (Figure \ref{fig:translations} middle)
    \item local-to-local (L2L): For a non-leaf node $B$, add the contribution of $B$'s local expansion to the local expansions of $B$'s children. (Figure \ref{fig:translations} right)
    \item local-to-particle (L2P): For a leaf node $B$, evaluate $B$'s local expansion at the locations of targets in $B$.
    This step adds all far-field contribution to the potentials of targets in $B$.
    \item particle-to-particle (P2P): For a leaf node $B$, evaluate the potential induced by all sources in $\mathcal{N}(B)$ directly.
\end{itemize}
%
As indicated by the arrows in Figure \ref{fig:translations}, translation operators in \kifmm share the same procedure: (1) evaluating the potentials on the check surface, and (2) solving the equation arising from matching the potentials for the equivalent densities.

Figure \ref{fig:fmm_sketch} outlines the complete \fmm algorithm.
During the upward pass, we compute P2M at all leaf nodes and perform M2M in post-order tree traversal.
Next, we compute M2L for all nodes.
Finally, we compute L2L in pre-order tree traversal, and perform L2P and P2P at all leaf nodes during the downward pass.

\begin{figure*}
\centering
    \subfloat[][A 3-level quadtree.]{\includegraphics[width=0.35\textwidth]{near_far_decomposition.pdf}
        \label{fig:near_far_decomp}}
    \subfloat[][Sketch of FMM algorithm using a binary tree.]{\includegraphics[width=0.6\textwidth]{fmm_sketch.pdf}
        \label{fig:fmm_sketch}}\\
    \subfloat[][]{\includegraphics[width=0.4\textwidth]{multipole_expansion.pdf}
        \label{fig:multipole}}
   \subfloat[][]{\includegraphics[width=0.4\textwidth]{local_expansion.pdf}
        \label{fig:local}}\\
\subfloat[][]{\includegraphics[width=\textwidth]{translations.pdf}
        \label{fig:translations}}
    \caption{Illustrations of the \fmm algorithm.
    \textbf{c},\textbf{d}, Multipole and local expansion in \kifmm.
    \textbf{e}, M2M (left), M2L (middle) and L2L (right) operators in \kifmm. Node $C$ is the parent of $B$, and node $A$ is in the interaction list of $B$.
    }
\end{figure*}

The original Exafmm \cite{yokota2012tuned,yokota2013fmm} implements the classical \fmm based on dual tree traversal and focuses on low-accuracy optimizations.
Recently, Exafmm received a major update to adopt \kifmm due to its great extensibility.
Its current generation, Exafmm-t \cite{Wang2021}, offers highly optimized \kifmm operators, allows pre-computing and caching invariant matrices and more importantly, provides a high-level Python interface to reach a broader audience.


\documentclass{beamer}

\input{beamer-config.tex}

\input{required-packages.tex}

\input{font-config.tex}

\input{custom-commands.tex}

\input{math-ops.tex}

\input{tikz-config.tex}

\input{theorem-config.tex}

\title{
    \Subtitle{COMP0005 Algorithms}  \\
    {\huge\itshape Graphs}                  \\
}
\author{Jieyou Xu}
\date{\today}

\begin{document}
\frame{\titlepage}

\section{Introduction to Graphs}

\begin{frame}{Undirected Simple Graph}
    An \Keyword{undirected simple graph} $G$ is a two-tuple
    \begin{equation}
        G = (V, E)
    \end{equation}
    Where
    \begin{enumerate}
        \item $V$ is the set of \Keyword{vertices} (or \Keyword{nodes}, \Keyword{points})
        \item $E$ is the set of \Keyword{edges} (or \Keyword{links}) where each \Keyword{edge} connects two \Keyword{vertices}
        \begin{itemize}
            \item Not allowing \textit{self-loops}:
            \begin{equation}
                E \subseteq \set{(x, y) \mid (x, y) \in V^2 \land x \ne y}
            \end{equation}
            \item Allowing \textit{self-loops}:
            \begin{equation}
                E \subseteq \set{(x, y) \mid (x, y) \in V^2}
            \end{equation}
        \end{itemize}
    \end{enumerate}
\end{frame}

\begin{frame}
    No \textit{self-loops}
    \begin{figure}[H]
        \centering
        \begin{equation*}
            \psmatrix[colsep=2em, rowsep=1em, mnode=circle, linewidth=0.5pt]
                x & y
                \ncline{-}{1, 1}{1, 2}
            \endpsmatrix
        \end{equation*}
    \end{figure}

    Allowing \textit{self-loops}
    \begin{figure}[H]
        \centering
        \begin{equation*}
            \psmatrix[colsep=2em, rowsep=1em, mnode=circle, linewidth=0.5pt]
                x & y
                \ncline{-}{1, 1}{1, 2}
                \nccircle{-}{1, 2}{1em}
            \endpsmatrix
        \end{equation*}
    \end{figure}
\end{frame}

\begin{frame}{Directed Simple Graph}
    A \Keyword{directed simple graph} $G$ is a \textit{graph} in which \textit{edges} have orientation
    \begin{equation}
        G = (V, A)
    \end{equation}
    Where
    \begin{enumerate}
        \item $V$ is the set of \Keyword{vertices} (or \Keyword{nodes}, \Keyword{points})
        \item $A$ is the set of \Keyword{directed edges} where each \Keyword{edge} connects two vertices with a \Keyword{direction}
    \end{enumerate}
\end{frame}

\begin{frame}{Directed Edge}
    In a \Keyword{directed simple graph}, each \Keyword{edge} $(x, y)$ connects \textit{vertex} $x \to y$.
    \begin{figure}[H]
        \centering
        \begin{equation*}
            \psmatrix[colsep=2em, rowsep=1em, mnode=circle, linewidth=0.5pt]
                x & y
                \ncline{->}{1, 1}{1, 2}
            \endpsmatrix
        \end{equation*}
    \end{figure}
    For the \Keyword{directed edge} $(x, y)$ from $x \to y$
    \begin{itemize}
        \item $x$ is the \Keyword{tail} of the edge
        \item $y$ is the \Keyword{head} of the edge
    \end{itemize}

    The \Keyword{edge} $(y, x)$ is the \Keyword{inverted edge} of $(x, y)$
    \begin{figure}[H]
        \centering
        \begin{equation*}
            \psmatrix[colsep=2em, rowsep=1em, mnode=circle, linewidth=0.5pt]
                x & y
                \ncline{->}{1, 2}{1, 1}
            \endpsmatrix
        \end{equation*}
    \end{figure}
\end{frame}

\begin{frame}{Directed Edge}
    It is also possible for a \textit{loop} (or \textit{cycle}) to form between nodes
    \begin{figure}[H]
        \centering
        \begin{equation*}
            \psmatrix[colsep=2em, rowsep=1em, mnode=circle, linewidth=0.5pt]
                x & y
                \ncarc[arcangle=45]{->}{1, 1}{1, 2}
                \ncarc[arcangle=45]{->}{1, 2}{1, 1}
            \endpsmatrix
        \end{equation*}
    \end{figure}
\end{frame}

\end{document}


\chapter{Adaptive Resonance Theory}
ART networks are self-organizing networks that have been able to solve the \emph{stability-plasticity} dilemma. ART able to \emph{switch modes} between plastic and stable without damage to previous learned weight values.

\begin{description}
\item[Stability-plasticity dilemma] not able to learn new information on top of old
\item[Plasticity] for the integration of new knowledge
\item[Stability] to prevent the forgetting of previous knowledge
\end{description}

\section{ART-1}
Designed to cluster and recognize binary patterns only.
\begin{figure}[!h]
\centering
\includegraphics[width=8cm]{chapter10_1}
\end{figure}

\noindent The feed-forward weight matrix is:
$$\mathbf{W} = [\mathbf{w}_1 \mathbf{w}_2 \ldots \mathbf{w}_M]^{T}$$
The feedback weight vector is:
$$\mathbf{V} = [\mathbf{v}_1 \mathbf{v}_2 \ldots \mathbf{v}_N]^{T}$$
The feed-forward weight is proportional to the corresponding feedback weight value:
$$w_{ji} \propto v_{ij}$$
\begin{center} where $v_{ij} \in {0,1}$ and $w_{ji} \in [0,1]$\end{center}
Output layer is a \emph{winner-take-all} layer

\subsubsection{Control Signals of the Network}
$$
control1 =
\begin{cases}
0 & comparison\ mode \\
1 & input\ mode
\end{cases}
$$

$$
control2 =
\begin{cases}
0 & failed\ vigilance\ test \\
1 & satisfied\ vigilance\ test
\end{cases}
$$

\subsubsection{Vigilance Test}
Similarity between input pattern $\mathbf{X}$ and feedback pattern $\mathbf{V}_j$:
$$s_j = \frac{|\mathbf{X} \cap \mathbf{V}_j|}{| \mathbf{X} |}$$
Vigilance test is satisfied if $s_j$ is greater than vigilance value $\rho$

\section{ART-1 Algorithm}
\subsubsection{Step 1: Initialization}
Initialize vigilance threshold $\rho$, feed-forward weights $w_{ij}$, and feedback weights $v_{j1}$:
$$0 \le \rho \le 1$$
$$w_{ij} = \frac{1}{1+N}$$
$$v_{ji} = 1$$
\subsubsection{Step 2: The total synaptic input}
\begin{equation*}
\begin{split}
\mathbf{u}_j &= \sum_{i=1}^{n} w_{ji} x_i \\
&= \frac{|\mathbf{V}_j \cap \mathbf{X}|}{0.5 + |\mathbf{V}_j|} \\
& \propto | \mathbf{V}_j \cap \mathbf{X}_i |
\end{split}
\end{equation*}
\begin{center}Since $w_{ji} \propto v_{ij}$ \end{center}

\subsubsection{Step 3: The winner $m$}
$$m = arg\!\max_{j=1...M} u_j\ for\ y_j \ne 0$$
The output
$$
y_j =
\begin{cases}
1 & j = m \\
0 & j \ne m
\end{cases}
$$
\subsubsection{Step 4: Calculate similarity}
$$s_j = \frac{|\mathbf{X} \cap \mathbf{V}_m |}{| \mathbf{X} |}$$
If $s \ge \rho$, go to step 5. \\
Else if the top-later has any active nodes left, go to step 6. \\
Else, go to step 5 to create a new cluster.
\subsubsection{Step 5: Update the weights of winning node}
$$\mathbf{V}_m^{new} = \mathbf{V}_m \cap \mathbf{X}$$
$$\mathbf{w}_m^{new} = \frac{\mathbf{V}_m^{new}}{0.5 + | \mathbf{V}_m^{new} |}$$
Go to step 2.
\subsubsection{Step 6: Continue search}
Setting output $y_m = 0$. \\
Go to step 3 to find a new winner $m$