\section{Galois correspondence of covering spaces}
For this section, fix a Galois cover $p: Y \longrightarrow X$.

\begin{lemma}
\label{lemma:compositionOfCovers}
  If $p$ factors through a space $Z$ as
  \begin{equation*}
    \begin{tikzcd}
      Y \ar[rd, "p_1"] \ar[dd, "p"']\\
        & Z \ar[ld,"p_2"] \\
      X
    \end{tikzcd}
  \end{equation*}
  and if $p_2$ is a cover, then so is $p_1$.
  In this case, we say that $Z$ is a cover \emph{lying between $Y$ and $X$.}
\end{lemma}
\begin{qbox}
  Prove Lemma \ref{lemma:compositionOfCovers} by drawing pictures.
\end{qbox}

\begin{lemma}
\label{lemma:lemma2}
  If $p_2:Z \rightarrow X$ be a cover. Let $f$, $g$ be simplicial maps $Y \rightarrow Z$ that fit into a commutative diagram
  \begin{equation*}
    \begin{tikzcd}
      Y \ar[rr, shift left, "f"] \ar[rr, shift right, "g"']  \ar[rd, "p"']
      & & Z \ar[ld,"p_2"] \\
      & X
    \end{tikzcd}
  \end{equation*}
  If $f(y_0) = g(y_0)$ for some vertex $y_0$ in $Y$ then $f = g$.
\end{lemma}
\begin{qbox}
  Prove Lemma \ref{lemma:lemma2} for vertices i.e. show that $f(y) = g(y)$ for all vertices $y$ in $Y$. To do this, take a path $\gamma$ in $Y$ from $y_0$ to $y$, push it down to $X$ via $p$ and lift it up to $Z$ via $p_2$. Use induction on the length of $\gamma$.
\end{qbox}

\begin{proposition}
  If $Z$ is a cover lying between $Y$ and $X$, then $p_1: Y \rightarrow Z$ is a Galois cover.
\end{proposition}
\begin{proof}
  We have already shown that $p_1$ is a cover in Lemma \ref{lemma:compositionOfCovers}.
  In order to show that it is Galois we will use the criterion of Theorem \ref{theorem:GaloisCriterion}:
  ``$p_1$ is Galois if and only if $\Gal(Y|Z)$ acts freely, transitively on all the fibers.''
  First note that every deck transformation of $Y \rightarrow Z$ is also a deck transformation of $Y \rightarrow X$
  \begin{equation*}
    \begin{tikzcd}
      Y \ar[rr, "\varphi"] \ar[rd, "p_2"] \ar[rdd, "p"']
      & & Y \ar[ld,"p_2"'] \ar[ldd, "p"]\\
      & Z \ar[d] \\
      & X
    \end{tikzcd}
  \end{equation*}
  Hence $\Gal(Y|Z)$ is a subgroup of $\Gal(Y|X)$.
  We want to show that $\Gal(Y|Z)$ acts freely, transitively on the fibers $p_2^{-1}(Z)$.
  The freeness is by Theorem \ref{theorem:freenessGaloisAction} so we only need to prove transitivity.

  Pick an arbitrary element $z$ in $Z$ and let $y_1$ and $y_2$ be elements in the fiber $p_2^{-1}(z)$.
  As $Y \rightarrow X$ is Galois, there is a deck transformation $\varphi \in \Gal(Y|X)$ such that $\varphi(y_1) = y_2$.

  \textbf{Claim:} $\varphi$ is in $\Gal(Y|Z)$.

  To prove this claim, apply Lemma \ref{lemma:lemma2} to the following commutative diagram
  \begin{equation*}
    \begin{tikzcd}
      Y \ar[rr, shift left, "p_1"] \ar[rr, shift right, "p_1 \circ \varphi"']  \ar[rd, "p"']
      & & Z \ar[ld,"p_2"] \\
      & X
    \end{tikzcd}
  \end{equation*}
  Check that we can apply lemma 2: 1) $\varphi$ is a deck transformation of $p$, hence the diagram commutes. 2) $p_1(y_1) = p_1 \circ \varphi(y_1) = z$.
  By Lemma \ref{lemma:lemma2}, $p_1 = p_1 \circ \varphi$ i.e. $\varphi$ is a deck transformation in $\Gal(Y|Z)$.
  Hence, the action of $\Gal(Y|Z)$ on the fibers of $p_2$ is transitive.
\end{proof}

\begin{proposition}
  If $Z$ is a cover lying between $Y$ and $X$, and $p_2:Z \rightarrow X$ is Galois, then $\Gal(Y|Z)$ is a normal subgroup of $\Gal(Y|X)$, and\footnote{This is equivalent to saying that the following sequence is exact. \begin{equation*}
    1 \rightarrow \Gal(Y|Z) \rightarrow \Gal(Y|X) \rightarrow \Gal(Z|X) \rightarrow 1
  \end{equation*}}
    \begin{align*}
      \Gal(Z|X) \cong \Gal(Y|X) / \Gal(Y|Z).
    \end{align*}
\end{proposition}
\begin{proof}
  Let $\varphi \in G$ be a deck transformation in $\Gal(Y|X)$. We will first show that $\varphi$ descends to a deck transformation in $\Gal(Z|X)$ i.e. there is a $\psi$ that fits in the commutative diagram
  \begin{equation*}
    \begin{tikzcd}
      Y \ar[rrrr, "\varphi"] \ar[rd, "p_1"']
      & & & &
      Y  \ar[ld, "p_1"]\\
      & Z \ar[rr, shift left, "\psi"] \ar[rd,"p_2"']
      & & Z \ar[ld,"p_2"] \\
      & &  X
    \end{tikzcd}
  \end{equation*}
  To do this, pick an element $y \in Y$ and let $ z = p_1(y)$.
  Because $Z \rightarrow X$ is Galois, there is a unique deck transformation $\psi \in \Gal(Z|X)$ which sends $p_1(y)$ to $p_1(\varphi(y))$.
  We need to show that $p_1 \circ \varphi = \psi \circ p_1$.
  For this, apply Lemma \ref{lemma:lemma2} to the following commutative diagram
  \begin{equation*}
    \begin{tikzcd}
      Y \ar[rr, shift left, "\psi \circ p_1"] \ar[rr, shift right, "p_1 \circ \varphi"']  \ar[rd, "p"']
      & & Z \ar[ld,"p_2"] \\
      & X
    \end{tikzcd}
  \end{equation*}
  \begin{qbox}
    Finish the above argument. Then show that $\varphi \mapsto \psi$ defines a group homomorphsism $\Gal(Y|X) \rightarrow \Gal(Z|X)$ with kernel $\Gal(Y|Z)$ thereby completing the proof.
  \end{qbox}
\end{proof}

\begin{proposition}
  If $Z$ is a cover lying between $Y$ and $X$ and $\Gal(Y|Z)$ is a normal subgroup of $\Gal(Y|X)$, then $p_2:Z \rightarrow X$ is Galois.
\end{proposition}
  \begin{qbox}
    Using $Z \cong Y/H$, $X \cong Y/G$, argue that $X \cong Z/(G/H)$. Use this to prove the Proposition.
  \end{qbox}

\begin{theorem}[Galois Correspondence of Covering Spaces]
  There is a 1-1 correspondence between the subgroups of $\Gal(Y|X)$ and covers of $X$ that lie between $Y$ and $X$, given by the following maps
  \begin{align*}
    \{\mbox{ subgroups of $\Gal(Y|X)$ } \} &\longleftrightarrow  \{\mbox{ covers lying between $Y$ and $X$ } \}\\
    H &\longmapsto Y/H \\
    \Gal(Y|Z) & \longmapsfrom Z
  \end{align*}
  Under this correspondence, the normal subgroups of $\Gal(X|Y)$ correspond to Galois covers of $X$.
\end{theorem}
\begin{qbox}
  We have proved all the parts of this theorem. Write down the proof explicitly and see how the various pieces fit together.
\end{qbox}

\begin{remark}
  There is a subtle point here about what the correspondence is between. On the right hand side, the objects are not spaces $Z$ but the triangles of the form \begin{equation*}
    \begin{tikzcd}
      Y \ar[rd, "p_1"] \ar[dd, "p"']\\
        & Z \ar[ld,"p_2"] \\
      X
    \end{tikzcd}
  \end{equation*}
\end{remark}

\begin{qbox}
  Explicitly right down the Galois correspondence for
  \begin{enumerate}
    \item The covers of $S^1 \vee S^1$ in Figure \ref{fig:CoveringsOfS1S1}.
    \item The cover $\bbr^1 \rightarrow S^1$.
  \end{enumerate}
\end{qbox}

\iffalse
\section*{Review the following for tomorrow}
For tomorrow's class, please review/lookup \emph{presentation of a group}. The following statements should make sense to you
\begin{align*}
  \bbz &= \langle a \rangle \\
  \bbz/n &= \langle a | a^n \rangle \\
  \bbz \times \bbz &= \langle a, b | ab a^{-1} b^{-1} \rangle \\
  D_{2n} &= \langle r, s | r^n, s^2, (rs)^2 \rangle && \mbox{ where $D_{2n}$ is the Dihedral group.}
\end{align*}

\section{Lifting properties of covering spaces}

We have a complete description of what the covers lying between $X$ and $Y$ are.

\begin{theorem}
  \label{theorem:liftingOfCovers}
  Consider covering maps
  \begin{equation*}
    \begin{tikzcd}
      & X \ar[d,"p"] \\
      Z \ar[r, "p'"']& Y
    \end{tikzcd}
  \end{equation*}
  along with vertices $x \in X, y \in Y, z \in Z$ with $p(x) = z = p'(y)$. There exists a map $p'':Z \rightarrow X$ which factors $p'$ with $p'(z) = y$ if and only if
  \begin{align*}
    p'_*\pi_1(Z,z) \le p_* \pi_1(X,x).
  \end{align*}
  In this case, the lift $p''$ is unique.
\end{theorem}

  \begin{corollary}
    A simply connected space has no non-trivial (connected) covers.
  \end{corollary}

  A map between spaces $p:X \rightarrow Y$ naturally induces a map between fundamental groups
  \begin{equation*}
    p_*: \pi_1(X,x) \rightarrow \pi_1(Y,y)
  \end{equation*}

  \fi


\chapter{Background Independence in Quantum Gravity}\label{chap:BGindependence}
Before we come to an end, we briefly want to discuss the issue of background independence in the context of quantum gravity and emphasize that our calculations in the background field approximation have to be treated very carefully. The formulas presented in the scope of this chapter are taken from \cite{PawlowskiNPgaugeLecture}.\\
Throughout this work we used a linear split of the metric  into some background $\bar{g}_{\mu\nu}$ and a fluctuation field $h_{\mu\nu}$. This changes nothing about the fact, that in general, the flowing action $\Gamma_k = \Gamma_k[\bar{g} + h]$ is still a functional of the full metric $g$, but from a computational point of view it may be easier accessible, since we can expand it in powers of the fluctuations on the given background, i.\,e.
\begin{equation}
	\Gamma_k\left[\bar{g} + h\right] = \Gamma_k\left[\bar{g}\right] +  \Gamma_k^{(0,1)}\left[\bar{g}\right]\cdot h + \frac{1}{2}\Gamma_k^{(0,2)}\left[\bar{g}\right]\cdot h^2 +\mathcal{O}(h^3),
\label{eqn:vertex_expansion}
\end{equation}
where we introduced the shorthand notation $\Gamma_k^{(n,m)} = \frac{\delta^{n+m}\Gammak}{\delta^n\bar{g}_{\mu\nu}\delta^mh_{\mu\nu}}$ to distinguish derivatives w.\,r.\,t. the background and the fluctuation field. But since this split is only a mathematical trick to be able to perform calculations in a more efficient way, there should be some relation between the correlations of the fluctuation field and those of the background field.
This idea is encoded in the \textit{Nielsen identities}, given by
\begin{equation}
	 \mathrm{NI}=\frac{\delta \Gamma}{\delta \bar{g}_{\mu \nu}}-\frac{\delta \Gamma}{\delta h_{\mu \nu}}-\left\langle\left[\frac{\delta}{\delta \bar{g}_{\mu \nu}}-\frac{\delta}{\delta \hat{h}_{\mu \nu}}\right]\left(\mathcal{S}_{\mathrm{gf}}+\mathcal{S}_{\mathrm{gh}}\right)\right\rangle= 0.
\end{equation}
Here $h_{\mu\nu}=\langle\hat{h}_{\mu\nu}\rangle$. The difference between the background derivatives and the fluctuation derivatives is connected to derivatives of the gauge fixing sector. At finite $k$ the regulator, which plays a crucial role in our approach to the subject, introduces another background dependence. This leads us to the \textit{modified Nielsen identities}:
\begin{equation}
	\mathrm{mNI}=\mathrm{NI}-\frac{1}{2} \operatorname{Tr}\left[\frac{\delta}{\delta\sqrt{g}} \frac{\delta \sqrt{\bar{g}} R_{k}[\bar{g}]}{\delta \bar{g}_{\mu \nu}} G_{k}\right]=0.
\end{equation}
In background field approximation we assume $\frac{\delta\Gammak}{\delta h}\approx\frac{\delta\Gammak}{\delta \bar{g}}$, this violates the Nielsen identities and therefore background independence is lost. This may seem contradictory at first sight, since in background field approximation we only deal with a single metric. Nevertheless, up until now, the background field approximation is somehow the standard approach to calculations in this area.
As already mentioned in the introduction, more recently some progress has been made in surmounting the background field approximation. Results based on vertex expansions in powers of the fluctuation field, such as in (\ref{eqn:vertex_expansion}), were used to determine the flow of the couplings by computation of the flow of the $n$-point functions. Details can be found in \cite{ChristiansenLitimPawlowskiReichert2018,MeibohmPawlowskiReichert2015}. The results obtained from this approach differ to some extend strictly from the results found in computations in background field approximations. For future projects, one should keep these problems concerning the background field approximation in mind.


\documentclass[12pt, a4]{article}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage[a4paper, total={16cm, 22cm}]{geometry}

\usepackage{graphicx}
\graphicspath{ {./screenshots/} }

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\lhead{Business Intelligence Lab}
\rhead{Mini Project}
\rfoot{\thepage}

\usepackage{hyperref}
\usepackage[section]{placeins}

\title{
  \vspace{-1.0cm}
  \textbf{Logistic Regression with Rust}
}
\author{
  Arpit Bhat\\
  \textit{501808}
  \and
  Archit Bhonsle\\
  \textit{501810}
  \and
  Shalmali Kulkarni\\
  \textit{501835}
}
\date{}

\begin{document}

\maketitle
\thispagestyle{fancy}

\section{Problem Statement}

We aim to create a desktop application using GTK, a toolkit to create GUI apps
that guides use through the a simplified version of the machine learning pipeline. The algorithm we'll be using is logistic regression which is used in
various simple classification tasks.

\section{Dataset}

The dataset we've chosen is reduced version of a larger dataset with 76
attributes found here:
\url{https://archive.ics.uci.edu/ml/datasets/Heart+Disease}.
This reduced dataset has 14 attributes (described in detail in table
\ref{table:attributes}) and one target variable which indicates whether heart
disease is present or not. The reduced dataset has numeric, discrete,
continuous and binary attributes all of which can be represented as numbers
which in our case are 64-bit floating point values. This is an assumption that helps us optimize our model and skip certain steps like mapping nominal attributes to unique numbers.

\begin{table}[h]
\centering
\begin{tabular}{| l | l |}
  \hline
  Column   & Attribute                                            \\
  \hline
  age      & Age in years                                         \\
  sex      & Sex of person: 0 is female, 1 is male                \\
  cp       & Chest pain type                                      \\
  trestbps & Resting blood pressure (in mm Hg)                    \\
  chol     & Serum cholesterol in mg/dl                           \\
  fbs      & 1 if fasting blood sugar $>$ 120 mg/dl else 0        \\
  restecg  & Resting electrocardiograph results                   \\
  thalch   & Maximum heart rate achieved (in mm Hg)               \\
  exang    & 1 if exercise induced angina else 0                  \\
  oldpeak  & ST depression induced by exercise relative to rest   \\
  slope    & The slope of the peak exercise ST segment            \\
  ca       & Number of major vessels (0-3) colored by fluoroscope \\
  thal     & 3 = normal; 6 = fixed defect; 7 = reversable defect  \\
  target   & 1 if heart disease is present 0 otherwise            \\
  \hline
\end{tabular}
\caption{Attributes in the heart dataset}
\label{table:attributes}
\end{table}

\section{Model}

The model we use here is
\href{https://en.wikipedia.org/wiki/Logistic_regression}{logistic regression}.
It is a model used for binary classification problems like the heart disease
problem we are tackling here. A fundamental part of this model is the sigmoid function which maps an arbitary number of real values to a probability between 0 and 1:

\[ \frac{1}{1 + e^{-x}} \]

The cost function we use is:

\[ J(\theta) = \frac{1}{m} \sum_{i = 1}^{m}
  \left(
    y^{(i)}\log\left(h_{\theta}(x^{(i)})\right) +
    \left(1 - y^{(i)}\right)\log\left(1 - h_{\theta}(x^{(i)})\right)
  \right)
\]

And thus in gradient descent, which is basically applying this repeatedly:

\[
  \theta_{j} := \theta_{j} -
  \alpha \frac{\delta}{\delta\theta_{j}} J(\theta)
\]

After resolving the derivative using calculus the equation becomes:

\[
  \theta_{j} := \theta_{j} -
  \frac{\alpha}{m} \sum_{i = 1}^{m} x_{j}^{(i)}
  \left(h_{\theta}(x^{(i)}) - y^{(i)} \right)
\]

\

\section{Creating the model and testing}

The core code for the model is encapsulated inside the following
\texttt{forward\_backward} function:

\begin{verbatim}
fn forward_backward(
    weights: &Array2<f64>,
    bias: &f64,
    x_train: &Array2<f64>,
    y_train: &Array2<f64>,
) -> (f64, Array2<f64>, f64) {
    // forward
    let y_head: Array2<f64> = sigmoid(weights.t().dot(x_train)
        .mapv(|x| x + bias));
    let loss = (y_train * &(y_head.mapv(|z| z.ln()))
        + &((y_train.mapv(|z| 1. - z)) * &y_head.mapv(|z| (1. - z).ln())))
        .mapv(|z| -1. * z);
    let cost = loss.sum() / x_train.ncols() as f64;

    // backward
    let d_weights = (x_train.dot(&(&y_head - y_train).t()))
        .mapv(|z| z / x_train.ncols() as f64);
    let d_bias = (&y_head - y_train).sum() / x_train.ncols() as f64;

    (cost, d_weights, d_bias)
}
\end{verbatim}

Here, \texttt{y\_head} is $h_{\theta}(x^{(i)})$ or the hypothesis. \texttt{loss}
is $J(\theta)$ and \texttt{cost} is the average loss. This comprises the
``forward'' step. In the ``backward'' step we calculate the change in the weights,
\texttt{d\_weights} and the change in bias as \texttt{d\_bias}.

These values are then used by the \texttt{update} function which tweaks the
weights and the biases for the specified \texttt{iterations}. Thus, the model
is trained.

\begin{verbatim}
fn update(
    weights: Array2<f64>,
    bias: f64,
    x_train: Array2<f64>,
    y_train: Array2<f64>,
    learning_rate: f64,
    iterations: usize,
) -> (Vec<f64>, Array2<f64>, f64) {
    let mut costs: Vec<f64> = Vec::new();
    let mut weights = weights;
    let mut bias = bias;

    for _ in 0..iterations {
        let (cost, d_weight, d_bias) = forward_backward(
            &weights, &bias, &x_train, &y_train);
        weights -= &d_weight.mapv(|x| learning_rate * x);
        bias -= learning_rate * d_bias;

        costs.push(cost);
    }

    (costs, weights, bias)
}
\end{verbatim}

\section{Using the application}

The application has three pages. Each page has represents one section of the
machine learning pipeline.

\begin{enumerate}
  \item{In the first step, as shown in figure \ref{fig:choose}, we select our
        dataset. We can we review it in the tabular view and inspect it's
        various attributes.}
  \item{After that we perform preprocessing on this data. As one may observe
        in the tabular view some attributes go in the high 100s while some
        are simple binary values, 0 and 1. If we give this data to the model
        it may cause problems. To counter this we normalize the data. This is
        demonstrated in figure \ref{fig:preprocessing}.}
  \item{In the third step, as shown in the figure \ref{fig:model}, we actually
        train our model. We specify the learning rate and the number of
        iterations. Once trained a graph of the model's cost as the it goes
        through various iterations is graphed. Based on this we can evaluate
        the model. Once trained we can test the model against the testing data.
        Then the various metrics like accuracy, precision, recall and f1-score
        are shown.}
\end{enumerate}

\begin{figure}[h]
\centering
\includegraphics[width=120mm]{choose}
\caption{Choose Page}
\label{fig:choose}
\end{figure}

\begin{figure}[h]
\centering
\includegraphics[width=120mm]{preprocessing}
\caption{Preproccessing Page}
\label{fig:preprocessing}
\end{figure}

\begin{figure}[h]
\centering
\includegraphics[width=120mm]{model}
\caption{Model Page}
\label{fig:model}
\end{figure}

\section{Analyzing the results}

The results of various values of the hyperparameters, learning rate and
iterations are show in table \ref{table:hyperparams}.

\begin{table}[h]
  \centering
  \begin{tabular}{| p{2cm} | p{2cm} || p{2cm} | p{2cm} | p{2cm} | p{2cm} |}
    \hline
    LR & Iterations & Accuracy & Precision & Recall & F1-Score \\
    \hline
    0.1 & 100  & 79.1\% & 90.2\% & 76.7\% & 82.9\%  \\
    0.1 & 1000 & 80.2\% & 90.2\% & 78.0\% & 83.6\%  \\
    1   & 100  & 80.1\% & 90.2\% & 78.0\% & 83.6\%  \\
    1   & 1000 & 83.5\% & 92.2\% & 81.0\% & 86.2\%  \\
    10  & 100  & 74.2\% & 68.6\% & 83.3\% & 75.3\%  \\
    10  & 1000 & 76.9\% & 70.6\% & 85.7\% & 77.4\%  \\
    \hline
  \end{tabular}
\caption{Metrics for various hyperparameters}
\label{table:hyperparams}
\end{table}

As we can see, the model with the learning rate of 1 and 1000 iterations
performs the best. The learning rate of 0.1 is too slow and it takes 1000 to
match the performance of the (1, 1000) model. The learning rate of 10 is too
fast which is also apparent from it's cost vs iterations graph. Ideally, we'd
like a mechanism that reduces the learning rate whenever the cost stops
decreasing. This is known as an ``adaptive learning rate''.

\section{Setup}

Regardless of the operating system you're on you might need a dataset to test
this on. The app only supports datasets with numeric columns so other datasets
might crash the app. To download \texttt{heart.csv} used in this report, click
on
\href{https://downgit.github.io/#/home?url=https://github.com/ArchitBhonsle/gtk-rs-experiment/blob/main/data/heart.csv}{this} link.

\subsection{Windows}

\begin{enumerate}
  \item{Go
        \href {https://downgit.github.io/#/home?url=https://github.com/ArchitBhonsle/gtk-rs-experiment/tree/main/windows-package}
        {here}.}
  \item{In the downloaded folder double click on the
        \texttt{gtk-rs-experiment.exe}.}
\end{enumerate}

\subsection{Linux}

This may or may not work based on the libraries installed on your system.

\begin{enumerate}
  \item{Go to \href{https://downgit.github.io/#/home?url=https://github.com/ArchitBhonsle/gtk-rs-experiment/blob/main/linux-executable}{this}
        link.}
  \item{Open the location of the downloaded executable in the file explorer and
        right click and open a terminal there.}
  \item{Enter the command \texttt{chmod +x ./linux-executable} followed by
        \texttt{./linux-executable}.}
\end{enumerate}

\end{document}


\documentclass[a4paper,11pt]{amsbook}
\usepackage{amsmath}
\usepackage{../HBSuerDemir}
\usepackage{fullpage}
\pagestyle{headings}
\usepackage{fancyhdr}

\begin{document}
\hPage{b2p2/480}

of the field is tangent to the curve at that point.\\
$$DE: \displaystyle\frac{dx}{P}=\frac{dy}{0}$$ or\\
$$\displaystyle O(x,y)dx-P(x,y)dy=0$$
\paragraph{}
The orthogonal trajectories of the vector lines are called\\
equipotential curves of the field with $DE$. $Pdx+0dy=0$.\\
\begin{exmp}
Find the family of vector lines (stream lines)\\
of the vector field.\\
$F=2xyi-(x^{2}-y^{2})j$\\
\\
and the equipotential curves.
\end{exmp}
\begin{hSolution}
$$\displaystyle\frac{dx}{2xy}=\frac{dy}{-(x^{2}-y^{2})}$$\\
$$\Longrightarrow (x^{2}-y^{2})dx+2xydy=0$$\\
$$GS: x^{2}+y^{2}=cx$$ (circles)\\
\paragraph{}
Then the DE of the equipotential curves will be\\
$$(x^{2}-y^{2})dy-2xydx=0$$\\
with solution\\
$$\displaystyle x^{2}+y^{2}=cy$$\\

\end{hSolution}
\textbf{Exercises(6.3)}\\
\\
36. Find the $DE$ of the family of parabolas having the origin as\\
focus and $x=-p$ as directerix.\\
\\
37.Find the curve having length of subnormal equal to 3 and\\
 passing through the point $(1,4)$.\\
\\
38.Find the equation in polar coordinates of the curves such\\
that the tangent of the angle $\psi$ between the radius vector\\
\end{document}


\documentclass{article}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amssymb}
\usepackage{algorithm}
\usepackage{graphicx}
\usepackage[noend]{algpseudocode}
\usepackage{url}

\newlength\tindent
\setlength{\tindent}{\parindent}
\setlength{\parindent}{0pt}
\renewcommand{\indent}{\hspace*{\tindent}}

\newtheorem{thm}{Theorem}
\newtheorem{cor}{Corollary}[thm]
\newtheorem{lemma}{Lemma}[thm]

\title{Job Scheduling Problem}
\author{Daniel Braithwaite}

\begin{document}
	\pagenumbering{gobble}
	\maketitle
	\newpage
  	\pagenumbering{arabic}

	\section{Deterministic Algorithm}
		\subsection{Approximation Ratio}
			Say we have $m$ computers, we start by assuming our scheduler has been given $m$ simple jobs, assuming our algorithm wants to be optimal and considering that we cant see what is coming next we will assign one of these simple jobs to each of the $m$ machines. Now that it has scheduled the simple jobs we give it a single complex job. No matter which machine we assign the complex job to we end up with a total cost of 15 minutes where we could have a total cost of 10 minutes.\newline

			This gives us an approximation ratio of $\frac{15}{10} = 1.5$.

			\begin{lemma}
				No deterministic algorithm online algorithm for scheduling jobs can have a better ratio than 1.5
			\end{lemma}

			\begin{proof}
				We assume that our algorithm is always making the optimal choice when given a job. We clearly will want to assign incoming jobs to computers that don't already have a job on them. So we see we will assign the $m$ simple jobs to each of the $m$ computers. Given that we don't know about incoming jobs we cant arrange the tasks differently if the last one to be received is a complex job. So this leaves us with a solution with a cost of 15. Giving us a ratio of 1.5
			\end{proof}

		\subsection{Algorithm}
			We want to design an algorithm with the best approximation ratio of 1.5. We can see how we want to do this by referencing the proof in the previous section. We just want to assign the incoming job to the machine with the least total time as this will give us the best distribution across the machines.\newline

			We only have one input for the algorithm which is the machines. Assume we have a method that gives us the next job $getNextJob()$, and a method that tells us if we have any jobs left $haveJobsLeft()$

			\begin{algorithm}
				\begin{algorithmic}[1]
					\Procedure{deterministicJobSchedule}{$m$}
						\While{$haveJobsLeft()$}
							\State $j \gets getNextJob()$
							\State $i \gets$ machine with smallest queue
							\State assign j to $m[i]$
						\EndWhile
					\EndProcedure
				\end{algorithmic}
			\end{algorithm}

		\subsection{Complexity}
			Say we have $n$ jobs, then the outer loop will repeat $n$ times. Along with this we see that finding the machine with the smallest queue requires us to iterate through all the machines which will take $m$ steps where $m$ is the number of machines we have. Giving us a total complexity of

			\begin{align*}
				\theta(nm)
			\end{align*}

		\subsection{Testing}
			When testing this algorithm I generated worst case input to graph so that we could see the algorithm had an approximation ratio of 1.5. I also tested the program on randomly generated data with three different numbers of machines (5, 10, 20) so we could see that the algorithm never exceeded the approximation ratio of 1.5. As we can see in the graph below both these things hold and that the approximation ratio is as expected.

			\begin{figure}[h]
				\vspace{3mm}
				\begin{center}
					\includegraphics[scale=0.4]{Approx.png}
				\end{center}
			\end{figure}

	\section{Probabilistic Algorithm}
		\subsection{Algorithm}
			For this algorithm I am randomly assigning simple tasks and deterministic assigning complex tasks. This was because since complex tasks are bigger randomly assigning them will have more chance of making an issue where as simple tasks in the wrong place wont put out the solution by much. Assuming we are given a random distribution of simple and complex job this will also give us a significant speed increase from the deterministic algorithm\newline

			We will assume we have a method that gives us the next job $getNextJob()$, and a method that tells us if we have any jobs left $haveJobsLeft()$. Along with $uniformRandomInt(m)$, which returns a random number between 0 and m (not including m)
			\begin{algorithm}
				\begin{algorithmic}[1]
					\Procedure{probilisticJobSchedule}{$m$}
						\While{$haveJobsLeft()$}
							\State $j \gets getNextJob()$

							\If{$j$ is simple}
								\State $i \gets uniformRandomInt(m)$
							\Else
								\State $i \gets$ machine with smallest queue
							\EndIf

							\State assign j to $m[i]$
						\EndWhile
					\EndProcedure
				\end{algorithmic}
			\end{algorithm}

		\subsection{Approximation Ratio}
			We see that the approximation ratio for our probabilistic algorithm is the same as that for the deterministic algorithm by the following reasoning using the same worst case scenario as before.\newline

			If we get given $m$ simple jobs then given that we are uniformly distributing them across the machines when we get given the complex job the only thing to do is put it on a computer with a simple job and get a total cost of 15. We know that the optimal solution is 10. Giving us the approximation ratio of 1.5.

		\subsection{Complexity}
			The loop for this algorithm will repeat $n$ times where we have $n$ jobs. The probabilistic part of this algorithm will have a constant time complexity. And the deterministic part will have a complexity of $m$ where $m$ is the number of machines.

			\begin{align*}
				\theta(nm)
			\end{align*}

		\subsection{Testing}
			I tested this algorithm on random input and for each used three different numbers of machines. The solutions start of being wildly incorrect but as the input size grows the algorithm gets more accurate. The graph also shows that almost always having 5 machines will gives you better results than having 10 or 20. We can observer in the graph below that the approximation ratio is as we expected. We see that practically all of the plotted data falls below 1.5 and the few about can be explained by the fact that the expected ratio was calculated using the expected output of the algorithm, the fact that the algorithm is randomized means that we will sometimes get an output that is way off expected.

			\begin{figure}[h]
				\vspace{3mm}
				\begin{center}
					\includegraphics[scale=0.4]{Prob.png}
				\end{center}
			\end{figure}

			\break

	\section{Comparison}
		\subsection{Approximation Ratios}
			We can graph the approximation ratios of the two algorithms to see that deterministic algorithm out performs the probabilistic but not by a lot.

			\begin{figure}[h]
				\vspace{3mm}
				\begin{center}
					\includegraphics[scale=0.4]{CompareRatios.png}
				\end{center}
			\end{figure}

			\break

		\subsection{Complexity}
			If we compare the order cost of both the algorithms we see that the probabilistic algorithm and deterministic algorithms have the same order cost but if we examine the graph below we see that the probabilistic algorithmic out performs the deterministic one.

			\begin{figure}[h]
				\vspace{3mm}
				\begin{center}
					\includegraphics[scale=0.4]{CompareComplexity.png}
				\end{center}
			\end{figure}

			\break

		\subsection{Summary}
			While the deterministic algorithm gives a consistently good solution most of the time it is only slightly better if not the same as the probabilistic algorithm. Along with this we see that the probabilistic algorithm is considerably faster than the deterministic one.\newline

			The key difference between the two is that the probabilistic one will randomly assign the simple tasks to machines, this hopefully causes enough variance in the solution so that we can avoid arriving at a sub optimal solution. By only randomly assigning the simple tasks we are introducing variation but also minimizing risk. If a simple task is randomly assigned to a bad position then the offset caused by that isn't as much as if we assigned a complex task to a bad position.

	\section{Alternative Solutions Considered}
		\subsection{Probabilistic Algorithm}
			One possible solution I considered was rather than only randomly assigning simple tasks, just randomly assign any task but only $\alpha$ percent of the time. However in testing this didn't perform as optimally as the solution given in this report.

	\section{Further Improvements}
		\subsection{Deterministic Algorithm}
			A simple way to improve the effective of the deterministic algorithm would be to use a heap to store the computers. This would give is $log(m)$ time instead of $m$ time (where $m$ is the number of machines)

\end{document}


\chapter{Foundations of Image Analysis for Motion Estimation}\label{chap:found_mot_est}

\section{What are keypoints?}
According to Trucco \& Verri (1998) \cite{book} a local keypoint is defined as a local, meaningful, detectable part of an image. In the following, the term keypoint will be used for local keypoint. A keypoint is a local feature of an image, a part with some properties that differentiates it from other parts of the image. By meaningful, they mean that the feature is associated to interesting scene elements such as sharp intensity variations created by the contours of the objects in the scene. To be detectable they state that location algorithms must exist, if not, a keypoint would be of no use.

\section{Detecting Keypoints}
There is a wide variety of keypoint detectors, each with their own way of finding keypoints. There are multiple types of keypoint detectors, some focus on corner detection like Harris, FAST and Shi-Tomasi. SIFT and SURF on the other hand are scale-space detectors. The advantage of corner detectors is that they are quite invariant to view changes, on the other hand, scale changes pose a problem. Scale-space detectors try to detect keypoints on different scales of the image to find scale invariant keypoints

\section{Keypoint Descriptors}
The detection of keypoints is not enough. We need a way to compare these keypoints or rather the image patches around a keypoint and its hypothetical counterpart in the second frame. To do this, we can compare the color or gray values directly, or we can transform them into keypoint descriptors and compare these.\bigskip

Once again, there are lots of different keypoint descriptors that can be divided in continuous and binary keypoint descriptors. A continuous keypoint descriptor is nothing more than a high-dimensional real-valued vector describing the surroundings of the keypoint. While a binary keypoint descriptor is an vector of bits. The use of bits has the advantage that Hamming distance can be used to compare descriptors, which is very efficient. Also, storing binary values is cheaper than real values (using floating point).

An important feature of keypoint descriptors is their robustness, SIFT and SURF (continuous descriptors) for instance are robust to illumination, rotation and scale changes. BRIEF (binary descriptor) on the other hand is only robust to illumination, so illumination and scale changes are a problem when using BRIEF. ORB (binary descriptor) tried to eliminate this shortcoming of BRIEF and is invariant to illumination and rotation.

\section{Oriented FAST and rotated BRIEF (ORB)}\label{sec:orb}
ORB is based on a combination of the FAST keypoint detector and the BRIEF keypoint descriptor. With ORB, Rublee et al. (2011) \cite{6126544} didn't just develop a combination of FAST and BRIEF, but enhanced it with extra features to make it rotation invariant and resistant to noise while maintaining the focus on speed.

\subsection{FAST Keypoint Orientation (oFAST)}
Features from Accelerated Segment Test or FAST, proposed by Rosten \& Drummond \cite{10.1007/11744023_34} is a keypoint detector developed with real-time applications in mind. It has thus, as the name suggest, good speed performance. However, the problem with FAST is that there is no orientation component. This is the first thing added by Rublee et al. (2011) \cite{6126544}.\bigskip

\subsubsection{Features from Accelerated Segment Test (FAST)}
To detect if pixel $p$ is a corner, the pixels on circle with radius $r$ around $p$ are considered, see Figure \ref{fig:fastcircle}. In ORB, $r$ equals 9, which is called FAST-9. The intensity of $p$ is $I_p$. To decide if $p$ is a corner, FAST uses a threshold value $t$. If there exists a set of $n$ (usually 12) contiguous pixels on the circle that are either all brighter than $I_p + t$ or all darker than $I_p - t$, then $p$ is considered a corner.\bigskip

\begin{figure}
    \centering
    \includegraphics[width=1\textwidth]{figures/fast_circle.jpg}
    \caption{Fast with radius 3}
    \captionsource{Source: \cite{10.1007/11744023_34}}
    \label{fig:fastcircle}
\end{figure}

To make this process even faster, a high-speed test was introduced to eliminate a large number of non-corners. This high-speed test checks only four pixels, the ones on top, bottom, left and right. In Figure \ref{fig:fastcircle} these are pixels 1, 5, 9 and 13. At least three out of four of these pixels have to be either all brighter than $I_p + t$ or all darker than $I_p - t$. If this is not the case, there is no possibility for $p$ to be a corner. If this is the case, the full test will decide whether the $p$ is a corner.\bigskip

The problem with FAST is that it doesn't produce a value to indicate how much of a corner a certain pixel is. To cope with this shortcoming, ORB uses a Harris corner measure \cite{Harris1988ACC} to sort the keypoints detected by the FAST detector. After using FAST with a low threshold value (to ensure at least $N$ keypoints are detected), it sorts the corners based on the Harris measure, leaving only the top $N$ points. As FAST doesn't produce multi-scale features, a scale pyramid is used to calculate FAST features at each level in the pyramid.

\subsubsection{Orientation using Intensity Centroid}
ORB uses Intensity Centroid \cite{ROSIN1999291}, a measure of corner orientation. It uses geometric moments to determine the corner orientation. Rosin defines the momentum as:
\begin{equation}
    m_{pq} = \sum_{x,y} x^p y^q I(x, y),
\end{equation}
the centroid can then be found by:
\begin{equation}
    C = (\frac{m_{10}}{m_{00}},\frac{m_{01}}{m_{00}}).
\end{equation}
Placing $O$ at the center of the corner, a vector $\Vec{OC}$ can be created, the corner orientation is then:
\begin{equation}
    \theta = \mathrm{atan2}(m_{01}, m_{10})
\end{equation}
where $\mathrm{atan2}(x, y)$ or 2-argument arctangent is defined as the angle in the Euclidean plane, given in radians, between the positive x axis and the ray to the point $(x, y) \neq (0, 0)$ \cite{unknown-author-2022}. \autoref{fig:atan2} shows a graph of this function.\bigskip

\begin{figure}
    \centering
    \includegraphics[width=1\textwidth]{figures/atan2.png}
    \caption{Graph of $\mathrm{atan2}(x,y)$ over $y/x$}
    \label{fig:atan2}
\end{figure}

ORB improves the rotation invariance even more by making sure that the moments are computed with $x$ and $y$ remaining within a circular region of radius $r$. \cite{6126544} found the patch size to be a fit value for $r$. This means $x$ and $y$ run from $[-r, r]$. $\mid C\mid$ approaching 0 makes the measure become unstable, but this is rarely the case for FAST corners \cite{6126544}.

\subsection{Rotation-Aware BRIEF (rBRIEF)}
We now have keypoints and their orientation, the next step is to compute the descriptor using BRIEF. As stated before, BRIEF is not invariant to rotation, which is why ORB introduced a modification: Rotation-Aware BRIEF.

\subsubsection{Binary Robust Independent Elementary Features (BRIEF)}
BRIEF \cite{10.1007/978-3-642-15561-1_56} uses a small number of pairwise comparisons to classify patches from which it makes a bit vector. They defined the test $\tau$ on patch $\boldsymbol{p}$ like this:
\begin{equation}
    \tau(\boldsymbol{p};\boldsymbol{x},\boldsymbol{y}) := \left\{\begin{array}{ll}
         1\quad : \boldsymbol{p}(\boldsymbol{x}) < \boldsymbol{p}(\boldsymbol{y})\\
         0\quad : \boldsymbol{p}(\boldsymbol{x}) \geq \boldsymbol{p}(\boldsymbol{y})
    \end{array} \right.,
\end{equation}
with $\boldsymbol{p}(\boldsymbol{x})$ the pixel intensity in a smoothed version of $\boldsymbol{p}$ at $\boldsymbol{x} = (u, v)^T$. The BRIEF descriptor is then defined as the $n$-dimensional bitstring
\begin{equation}
    f_n(\boldsymbol{p}) := \sum_{1\leq i\leq n} 2^{i-1}\tau(\boldsymbol{p};\boldsymbol{x},\boldsymbol{y}) .
\end{equation}

Relying on the experiments of \cite{10.1007/978-3-642-15561-1_56}, ORB will use $n = 256$, a Gaussian distribution around the center of the patch and smooth the image using an integral image where each test point is a $5 \times 5$ subwindow of a $31 \times 31$ pixel patch. Based on experimental results in \cite{10.1007/978-3-642-15561-1_56} and their own these showed to be performing well.

\subsubsection{Steered BRIEF}
As stated before, BRIEF is not robust when it comes to in-plane rotations. Even small rotations over a few degrees drops the amount of inliers significantly \cite{6126544}. ORB tackles this problem by introducing Steered BRIEF which steers the BRIEF descriptor according to the orientation of the keypoint. \bigskip

To get to the steered BRIEF operator, a $2\times n$ matrix is defined as follows, $n$ being the amount of binary tests:
\begin{equation}
    \MS = \begin{pmatrix}
    x_1,...,x_n \\
    y_1,...,y_n
    \end{pmatrix}
\end{equation}

The steered version $\MS_\theta$ of $\MS$ is constructed using the rotation matrix $\MR_\theta$, $\theta$ being the orientation of the patch:
\begin{equation}
    \MS_\theta = \MR_\theta \MS
\end{equation}
The steered BRIEF operator is now
\begin{equation}
    g_n(\boldsymbol{p},\theta):=f_n(\boldsymbol{p})\mid(x_i,y_i)\in \MS_\theta
\end{equation}

A lookup table is constructed for values of $\theta = 2k\pi/30, k \in \mathbb{N}_0$. If the keypoint orientation $\theta$ is consistent across views, the correct set of points $\MS_\theta$ will be used to compute the descriptor.

\subsubsection{rBRIEF}
However, the benefit of rotational invariance comes at a price. Rublee et al. (2011) \cite{6126544} noticed a loss of variance and high correlation among the binary tests. To cope with these shortcomings, they developed a learning method to choose a good subset of binary tests. The goal is to have high variance of the bit feature and means close to 0.5, as well as being uncorrelated. To do this, they look at all binary tests.\bigskip

\cite{6126544} proposes the following greedy algorithm to get a set of uncorrelated tests with a mean close to 0.5:
\begin{enumerate}
    \item Run each test  against all training patches.\smallskip
    \item Order the tests by their distance from a mean of 0.5, forming the vector $\vt$.\smallskip
    \item Greedy search:\smallskip
    \begin{enumerate}
        \item Put the first test into the result vector $\vr$ and remove it from $\vt$.\smallskip
        \item Take the next test from $\vt$, and compare it against all tests in $\vr$. If its absolute correlation is greater than a threshold, discard it; else add it to $\vr$.\smallskip
        \item Repeat the previous step until there are 256 tests in R. If there are fewer than 256, raise the threshold and try again.\smallskip
    \end{enumerate}
\end{enumerate}

They showed that this is a good method to ensure high diversity and low correlation between the bit features.\bigskip

Rublee et al.\cite{6126544} is the perfect paper for a more detailed explanation of ORB, along with an evaluation and comparison with SURF and SIFT.

\section{Matching Keypoints}
As of now, we have a method to find good keypoints in an image and a way to describe the keypoint, a descriptor. To make point-to-point correspondences between the keypoints, we need a way to match them. There are multiple ways to do this.

\subsection{Brute force}
When searching for matches using brute force, all possible keypoints of the second frame are matched with a keypoint from the first set. The best match is then selected for that specific keypoint. There are several distance measures to check how good a certain match is, e.g. L1, L2, Hamming. This is repeated for every keypoint of the first frame until they are all matched with a keypoint from the second frame.

\subsection{Fast Library for Approximate Nearest Neighbors (FLANN)}
FLANN is a library in C++ used for finding the nearest neighbour in a high dimensional space. When looking at a descriptor as a vector in a high dimensional space, finding its nearest neighbour is as good as finding the most similar descriptor and thus finding a good match between keypoints.


\subsection{Stochastic Environment Model}\label{subsec:stochasticEnvModel}
Model-based agents require a concrete implementation of a model of their world, in order to maintain their internal state \cite[p.~50]{AIAMA}. The agent's internal state can be thought of as its opinion of what state the world might be in, and its model describes how it believes the world changes over time \cite{AIAMA}. Chapter \ref{chapter:Background} outlines the background behind potential models of the stochastic world, including both how internal state can be represented and how internal state can be updated.\par
Hidden Markov Models and Dynamic Bayesian Networks were identified as suitable models, due to the fact that they provide a succinct and flexible representation of hidden stochastic world state, as well as efficient online state estimation updating algorithms. We chose to use a Dynamic Bayesian Network (DBN), due to the fact that it can use conditional independence relations between variables to reduce the number of probabilities needed to be calculated to perform accurate inference \cite[p.~63]{KollerPGM}. DBNs also facilitate the incorporation of extra variables with arbitrary conditional independence relations. This was desirable, since we plan future work to extend the model to include extra variables, such as battery level, once a basic implementation was shown to work as intended.

The 2-Time Slice DBN shown in Figure \ref{fig:FirstDBNUsed} describes the agent's world model. A first-order Markov assumption is made, where the current state only depends on the state directly preceding it. Grey coloured variables are \textit{hidden state variables} and are assumed to not be directly observable. Green coloured variables are \textit{control actions} taken by the agent and the peach coloured variables are \textit{evidence variables}, which are assumed to be observable and depend on the hidden world state.
%The agent's location and the search status hidden state variables are mainly included for technical reasons
\note{maybe re-position figure so text wraps}
\begin{figure}
    \centering
    \includegraphics[width = 0.9\linewidth]{Chapters/MultiAgentTargetDetection/Figs/DBNs/DBNWithMultipleObservable.PNG}
     \caption{First Version of the DBN representing the agent world model}
    \label{fig:FirstDBNUsed}
\end{figure}
A number of conditional probabilities are specified here in order to describe the factored joint distribution of the world model fully. The full tables are omitted as they are sparse. For brevity, we use the abbreviation Loc for Location.

%\begin{figure}[H]
\scriptsize
\begin{equation}\label{eqn:EvidenceVarsProbs}
    %\centering
    p(SensorReading_t | AgentLoc_{t}, TargetLoc_{t})  =
    \begin{cases}
    \alpha \quad \text{ if } SensorReading_t=1 \text{ and } TargetLoc_t \neq AgentLoc_t
    \\
    1-\beta \quad \text{ if } SensorReading_t=1 \text{ and } TargetLoc_t = AgentLoc_t
    \\
    \beta \quad \text{ if } SensorReading_t=0 \text{ and } TargetLoc_t = AgentLoc_t
    \\
    1-\alpha \quad \text{ if } SensorReading_t=0 \text{ and } TargetLoc_t \neq AgentLoc_t
    \end{cases}
    %
\end{equation}
\begin{center}
    \normalsize
    Conditional Probability Distribution for the $Evidence$ Variable.
\end{center}
%\caption{Conditional Probability Distribution for the $Evidence$ Variable}
%\end{figure}

%\begin{figure}[H]
\scriptsize
\begin{equation}\label{eqn:TargetLocProbs}
    p(TargetLoc_{t} | TargetLoc_{t-1}) =
    \begin{cases}
    1 \quad \text{ if } TargetLoc_{t}=TargetLoc_{t-1}
    %agent returns correct target Loc.}
    \\
    0 \quad \text { otherwise. }
    \end{cases}
\end{equation}
%\caption{Conditional Probability Distribution for the $TargetLocation$ Variable}
%\end{figure}
\begin{center}
\normalsize
    Conditional Probability Distribution for the $TargetLocation$ Variable
\end{center}

%\begin{figure}[H]
\scriptsize
%\begin{equation}\label{eqn:SearchStatus}
    \begin{gather}\label{eqn:SearchStatus}
        p(SearchStatus_t | SearchStatus_{t-1}, Action_{t-1}) = \\
        \begin{cases}
        1 \quad \text{ if } SearchStatus_t = terminated\_x_i \text{ and } SearchStatus_{t-1} = terminated\_x_i
        \\
        1 \quad \text{ if } Action_t = terminate\_x_i \text{ and } SearchStatus_{t-1}=ongoing \text{ and } SearchStatus_{t} = terminated\_x_i
        \\
        1 \quad \text{ if } Action_t = move\_x_i \text{ and } SearchStatus_{t-1}=ongoing \text{ and } SearchStatus_t=ongoing
        %agent returns correct target location.}
        \\
        0 \quad \text { otherwise. }
        \end{cases}
    \end{gather}
%\end{equation}
%\caption{Conditional Probability Distribution for the $SearchStatus$ Variable}
%\end{figure}
\begin{center}
    \normalsize
    Conditional Probability Distribution for the $SearchStatus$ Variable
\end{center}

%\begin{figure}[H]
\scriptsize
    \begin{equation}\label{eqn:AgentLocation}
        p(AgentLoc_t | AgentLoc_{t-1}, Action_{t}) =
        \begin{cases}
        1 \quad \text{ if } Action_t = move\_x_i \text{ and } AgentLoc_t = x_i
        \\
        1 \quad \text{ if } Action_t = terminate\_x_i \text{ and } AgentLoc_t = AgentLoc_{t-1}
        \\
        0 \quad \text{otherwise}
        \end{cases}
    \end{equation}
%\caption{Conditional Probability Distribution for the $AgentLocation$ Variable}
%\end{figure}
\begin{center}
    \normalsize
    Conditional Probability Distribution for the $AgentLocation$ Variable
\end{center}

\normalsize
The semantics of the equations listed above is given here:
\begin{enumerate}
    \item Equation \ref{eqn:EvidenceVarsProbs} uses two parameters, $\alpha$ and $\beta$, to represent the probability of making a false positive and false negative sensor reading, respectively. These are assumed to have been calculated by using pre-calibrated values of the sensor. This model is does not stipulate any restrictions on the sensor other than that it must return a reading indicating that the target is present or not.
    \item  Equation \ref{eqn:TargetLocProbs} simply states that the location of the target does not change, even though it is hidden. This could be modified to allow for mobile target detection in the case of a non-stationary target by introducing non-unity probabilities in a transition matrix for different values of $TargetLoc_t$ and $TargetLoc_{t-1}$.
    \item The first line of Equation \ref{eqn:SearchStatus} states that once the agent terminates the search, it remains over. The second line states that if the agent requests an ongoing search to terminate, then it does so deterministically. The third line states that if the agent chooses to move in an ongoing search, then the search remains ongoing.
    \item Equation \ref{eqn:AgentLocation} states that the agent moves to new locations deterministically. It also indicates that should the agent choose to terminate the search, then the environment enters a terminal state, indicating that the search is over.
\end{enumerate}

Note that despite the fact that some hidden state variables cannot be observed directly, it is still possible to infer their value exactly based on their starting state. For example, the position of the agent is a deterministic function of its actions and previous position. %These variables could have been treated as variables that are internal to the agent rather than part of the hidden world state and could have been omitted for the sake of clarity.
%many authors
%\note{need to outline why I have included them - mainly because evidence probability depends on agent location as well as source location}

%\subsection{Analysis of Stochastic Environment Model}
%\note{talk about evolution of belief given a sequence of null observations}


\chapter{Examples for Cyclic Sextic Fields}
\label{chap:chap-seven}
In this chapter, we will give some examples for cyclic sextic field, using our results or methods given by chapter \ref{chap:chap-six} to a series of  problems: discriminant, integral basis, decomposition of prime, unit group and class number etc. For the first example in cyclotomic field, there exists a complete theory for that. But as a cyclic sextic number field, using the methods given by chapter \ref{chap:chap-six}, we also got the structures of it. More precisely, we discover two subfields of it, and then calculate the discriminant which is equal to the result in cyclotomic fields' theorem. Then we also give the prime decomposition of it based on our theorems in section \ref{sec:primdsex}. Also, we calculate the class number using the general theory. To solve the second field in section \ref{sec:rcsfexam}, we use almost all preliminaries we mentioned before. We first determine the Galois group of it, then we calculate the polynomial discriminant of it, based on the discriminant formula, then we find two subfields of it. After that, we also find its integral basis and get the prime decompositions. Also, we have tried to calculate the class number of it using general theory, and get the same result as M\"{a}ki did. The third section is another example given by A Bremner and B Spearman\citep{bremner2010cyclic} with sextic trinomial, but unfortunately, we haven't got the final results.

\section{7-th Cyclotomic Field}
\subsection{Cyclotomic Fields}
Let $\zeta_n$ denote a fixed primitive $n^{\text{th}}$ root of unity, and let $\mathbb{Q}(\zeta_n)$ be the number field generated by all the $n^{\text{th}}$ root of unity. The field $\mathbb{Q}(\zeta_n)$ is called the $n^{\text{th}}$ \textbf{cyclotomic field}. A following result is very import theorem.
\begin{theorem}
Let $\phi(n)$ denote the (Euler) totient of $n$, then $\mathbb{Q}(\zeta_n)$ is an Abelian extension of $\mathbb{Q}$ of degree $\phi(n)$. More precisely, there is an isomorphism:
\begin{eqnarray*}
(\mathbb{Z}/n)^{\times}&\rightarrow & \operatorname{Gal}(\mathbb{Q}(\zeta_n)/\mathbb{Q})\\
\bar{a}&\mapsto & \sigma_a,
\end{eqnarray*}
where $\sigma_a(\zeta_n)=\zeta_n^a$
\end{theorem}
Since a sub-extension of an Abelian extension is also Abelian, cyclotomic fields and their subfields already give us an abundant supply of Abelian extensions of $\mathbb{Q}$.
More formally, we have the
\begin{theorem}[Kronecker-Weber]\label{thm:kronecker}
Every finite abelian extension of $\mathbb{Q}$ is contained in a cyclotomic field.
\end{theorem}

\subsection{7-th Cyclotomic Field}
Now let us consider the simplest cyclic sextic field, a 7-th cyclotomic field. As we all know, $K_6=\mathbb{Q}(\zeta_7)$ is generated by $\zeta_7$, which is a root of unity, and its minimal polynomial is $$f(x)=x^6+x^5+x^4+x^3+x^2+x+1.$$

First of all, we would know its structure. As we known in Section \ref{sec:unitccsf}, this field is a CM-field, i.e. a totally imaginary extension of a totally complex cyclic cubic field. Write as before, we have $K_6=\mathbb{Q}(\sqrt{m},\theta)$, where $m$ is a negative squarefree integer. Now we need to find the minimal polynomials for $\sqrt{m}$ and $\theta$.

Note that $\zeta_7^i$ is complex conjugate to $\zeta_7^{-i}$, so let $\xi=\zeta_7+\frac{1}{\zeta_7}$, then $\xi$ is a real number\footnote{One can find that $\xi=2\cos\frac{2\pi}{7}$.}. Since $\zeta_7$ is a root of the minimal polynomial, then we have $$\zeta_7+\frac{1}{\zeta_7}+\zeta_7^2+\frac{1}{\zeta_7^2}+\zeta_7^3+\frac{1}{\zeta_7^3}+1=0,$$ Substitute $\xi$ into the equation, we have \begin{equation}\label{eqn:xi}
g(\xi)=\xi^3+2\xi^2-2\xi-1=0
\end{equation}
Let $x=\xi-\frac{2}{3}$, we can change it into the standard form, i.e. $$x^3-x^2-2x+\frac{13}{27},$$
where we can get $e=7,u=-1,v=1$, hence, the cyclic cubic subfield defined by $\xi$, where $\xi$ is a solution of equation \ref{eqn:xi}. i.e. $K^+=K_3=\mathbb{Q}(\xi)$.

From theorem \ref{thm:ccpoly}, we got the discriminant of $K_3$ is $49=f_3^2$, and since $v=1$, we got that $(1,\xi,\xi^2)$ is a power integral basis of $K_3$.

On the other hand, note that the automorphism $\sigma_2:\zeta_7\rightarrow\zeta_7^2$ generates the subgroup of order 3. Thus consider $\omega=\zeta_7+\zeta_7^2+\zeta_7^4$.
From the properties of root of unity, we have following two equations: $$(\zeta_7+\zeta_7^2+ \zeta_7^4)+(\zeta_7^3+\zeta_7^6+\zeta_7^{12})=-1,$$
$$(\zeta_7+\zeta_7^2+ \zeta_7^4)(\zeta_7^3+\zeta_7^6+\zeta_7^{12})=3-1=2$$

Then $\omega$ satisfies a quadratic equation: $$h(x)=x^2+x+2=0,$$ hence $\mathbb{Q}(\omega)$ is a quadratic subfield, and $K_2=\mathbb{Q}(\sqrt{-7})$. Then the discriminant of $K_2$ is $-7=f_2$. From theorem \ref{thm:sexdisccom}, we have the discriminant of $K_6$ is $d(K_6)=(-7)\times(7)^2\times(7)^2=-7^5$.

A famous result for discriminant of $\mathbb{Q}(\zeta_p)$, where $p$ is a prime, is that $\operatorname{Disc}(\mathbb{Q}(\zeta_p))=(-1)^{(p-1)/2}p^{p-2}$. In this field, we have $\operatorname{Disc}(\mathbb{Q}(\zeta_7))=(-1)^{3}7^{7-2}=-7^5$, which coincides our result.

To sum up, we got that $K_6=\mathbb{Q}(\xi,\sqrt{-7})$. As for the prime decomposition, the discriminant has only one prime divisor, namely $7$, hence only $7$ is ramified in $K_6$. what's more, since $7$ is common factor of $f_2$ and $f_3$, hence it is totally ramified. In fact, since $K_6$ is monogenic, we have $(x+6)^6 (\operatorname{mod} 7)$. For other primes, we just need to consider the decomposition in subfields, the using the result in section \ref{sec:primdsex}. For example, consider $p=37$, note that $$\left(\frac{-7}{37}\right)=(-1)^{(37-1)/2}\left(\frac{7}{37}\right)=(-1)^{(7-1)(37-1)/4}\left(\frac{37}{7}\right)=-\left(\frac{2}{7}\right)=1$$ Hence $37$ is ramified in $K_2$. On the other hand, similarly, consider we just need to verify that $49^{(37-1)/3}\equiv 1 (\operatorname{mod }37)$ or not. However, $49^{(37-1)/3}\equiv 12^{12} \equiv (-4)^6\equiv (-10)^2\equiv 26\not\equiv 1 (\operatorname{mod} 37)$, hence $37$ is inert in $K_2$. Hence, $37=\wp_1\wp_2$.

As for the unit group of $K_6$, we first refer a theorem  \citep{Xianke2006ANT} which is a corollary of theorem \ref{thm:unitcomplexsec} as follows:
\begin{theorem}
Let $m=p^s$, where $p$ is an odd prime, $s\in\mathbb{N^*}$, then $K=\mathbb{Q}(\zeta_m)$ has the same system of fundamental units to $K^+=\mathbb{Q}(\zeta_m+\zeta_m^{-1})$.
\end{theorem}

So from Dirichrlet Theorem \ref{thm:Dirichlet}, $U(K_6)=\mu(K_6)\times \mathbb{Z}^2$, where $\mu(K_6)=\langle\zeta_7\rangle$ is the root of unity of $K$ and the fundamental units are the same to $K_3$. The fundamental units in $K_3$ are $$(-1+\xi+\xi^2,2-\xi^2)=(1+\zeta_7+\zeta_7^2+\zeta_7^{-1}+\zeta_7^{-2},-\zeta_7^{-2}-\zeta_7^{2}).$$

As for the class number, firstly, we compute the Minkowski's bound, and we get $$C(K_6)=\left(\frac{4}{\pi}\right)^3\frac{6!}{6^6}\sqrt{|-7^5|}=4.13$$
Hence $Cl(K_6)=\langle[\wp]|\wp|2\text{ or }3\rangle$

now we consider the decomposition of $2$ and $3$. since $\left(\frac{-7}{2}\right)=1$, hence it splits in $K_2$. On the other hand, $x^3+x^2-2x-1\equiv x^3+x^2+1 (\operatorname{mod} 2)$, i.e. it's inert in $K_3$, hence $2=\wp_1\wp_2$\footnote{Here we haven't use cubic reciprocity, since there is no definition for $p=2$(in fact it only define in $p=3k+1$, like quadratic reciprocity defined in $p=2k+1$).}. In fact, $\wp_1=(2,1+\xi+\xi^3),\wp_2=(2,1+\xi^2+\xi^3)$, since $1+\xi^2+\xi^3$ is conjugate to $1+\xi+\xi^3$, hence $[\wp_1]=[\wp_2]=1$.

For $p=3$, it's easy to see that $\left(\frac{-7}{3}\right)=-1$ and $x^3+x^2-2x-1$ is irreducible in $\mathbb{F}_3$, hence $p=3$ is inert in $K_6$. To sum up, we have the class number is $1$.

\section{A Real Cyclic Sextic Field}\label{sec:rcsfexam}
Consider the sextic field $K_6$ generated by a root of $$f(x)=x^6-x^5-6x^4+6x^3+8x^2-8x+1.$$ First note that $f(x)$ is irreducible over $\mathbb{Q}$, since $f(x)\equiv x^6+x^5+1 (\operatorname{mod} 2)$. In order to verify the Galois group of the splitting field of $f$, we should first compute the resolvent polynomials. Let $p_1=x_1+x_2$, $p_2=x_1+x_2+x_3$, then use the numerical method, we have the approximate root of $f$ are: $$(r_1,r_2,r_3,r_4,r_5,r_6)=(-1.97766,-1.46610,0.14946,0.73068,1.65248,1.91115)$$
Then, \begin{eqnarray*}
R_{p_1,f}(y)&=&\prod_{p_{1i}\in orb(p)}(y-p_{1i}(r_1,\dots,r_6))\\
&=& x^{15}-5x^{14}-14x^{13}+98x^{12}+7x^{11}-567x^{10}+280x^9+1404x^8\\
&&-818x^7-1596x^6+735x^5+700x^4-203x^3-77x^2+11x+1\\
&=&(x^3-x^2-2 x+1)(x^6-2 x^5-10 x^4+6 x^3+30 x^2+17 x+1)\\
&&(x^6-2 x^5-10 x^4+27 x^3-12 x^2-4 x+1)
\end{eqnarray*}

So we have $(3,6^2)$ cycle, from table \ref{tab:allorbits}, we have that the Galois group of $f$ is either $C_6$ or $D_6$. Then we calculate $R_{p_2,f}$,
\begin{eqnarray*}
R_{p_2,f}(y)&=&\prod_{p_{2i}\in orb(p)}(y-p_{2i}(r_1,\dots,r_6))\\
&=& x^{20} + 2 x^{19} - 106 x^{18} - 600 x^{17} - 593 x^{16} + 3252 x^{15} - 6530 x^{14}\\
&& -2589 x^{13} + 4875 x^{12} - 675 x^{11} - 1759 x^{10} + 3349 x^9 + 5376 x^8 + 1260 x^7 \\
&&+ 1188 x^6 + 865 x^5 + 316 x^4 + 77 x^3 + 23 x^2 + 11 x + 1
\\
&=&(x^2 - x + 1) (x^6 - 12 x^5 + 3 x^4 + 7 x^3 + 6 x^2 - 2 x + 1) \\
&&(x^6 + 8 x^5 + 25 x^4 + 2 x^3 + 5 x^2 + 2 x + 1) \\
&&(x^6 + 7 x^5 - 8 x^4 + 4 x^3 + 27 x^2 + 12 x + 1)
\end{eqnarray*}
So we have $(2,6^3)$ cycle, from table \ref{tab:allorbits}, we have that the Galois group of $f$ is $C_6$, also we know $K_6$ is totally real.

The polynomial discriminant of $f$ could be given by formula \ref{prop:disctri}, in fact, $\operatorname{Disc}(f)=453789=3^3\times7^5$
Note that, $d(K_6)=f_2f_3^2f_6^2$ by theorem \ref{thm:sexdisc}, since $f_6=\operatorname{lcm}(f_2,f_3)$, hence we have $f_2^3|d(K_6)$ and $f_3^4|d(K_6)$. The relation between $d(K_6)$ and $\operatorname{Disc}(f)$ is showed in lemma \ref{lem:discpoly-field}, i.e. $\operatorname{Disc}(f)=a^2d(K_6)$, here we have $$3^3\times 7^5=a^2 f_2f_3^2f_6^2,$$ from the requirements we mentioned above, we have $$a^2f_3^4|3^3\times7^5,$$ this force $f_3=7$, and $a=1$ or $a=3$. Then $a^2f_2^3f_3^2|3^3\times7^5$, i.e. $$a^2f_2^3|3^3\times7^3.$$ If $a=3$, then $f_2=7$, and for this case $d(K_6)=7^5$, and we get $3^3d(K_6)=\operatorname{Disc}(f)$. An contradiction! Hence, $a=1$, and we could get $f_2=3\times7$.

To sum up, we have $f_2=21,f_3=7$. So we can immediately get the quadratic subfield is $\mathbb{Q}(\sqrt{21})$, with minimal polynomial $g(x)=x^2-x-5$, and integer ring $\mathbb{Z}\left[\frac{1+\sqrt{21}}{2}\right]$. \footnote{In fact, since $\phi(21)=\phi(3)\times\phi(7)=12$ and $6|12$, actually this cyclic sextic field is a subfield of the cyclotomic field $\mathbb{Q}(\zeta_21)$.}  As for the cyclic cubic subfield, since $e=f_3=7$, and $v=1$, hence we have the minimal polynomial of $\theta$, $h(x)=x^3-x^2-2x+\frac{13}{27}$ with power integral $(1,\theta,\theta^2)$. To sum up, $$K_6=\mathbb{Q}(\theta,\sqrt{21})$$

Since $d(K_6)=\operatorname{Disc}(f)$, hence $K_6$ has a power integral basis, wlog, let $r$ be a root of $f$, then $(1,r,r^2,\dots,r^5)$ is a integral basis. On the other hand, we have another integral basis given by theorem \ref{thm:intbasis_sex}.

The prime decomposition is quite easy, $7$ is totally ramified in $K_6$, since it is ramified in both two subfield. In fact, $f(x)\equiv (x+1)^6 (\operatorname{mod} 7)$. Hence $7O_{K_6}=\wp^6$, where $\wp=(7,r+1)$. $3$ is ramified in $K_2$, and inert in $K_3$, hence $2O_{K_6}=\wp'^2$. The other prime's situation could be solved through theorem \ref{thm:csf-unramified}.

As for the class number, we first compute the Minkowski's bound, $$C_{K_{6}}=(4/\pi)^{0}\frac{6!}{6^6}\sqrt{453789}=10.4,$$ now we should consider $p=2,3,5,7$. we can find that $2$ is inert in $O_{K_6}$. $7$ is totally ramified with $\wp=(7,r+1)$, while $N(r+1)=7$\footnote{calculate through $f(x-1)$}, $(r+1)\subset(7)$, hence $\wp=(r+1)$ is a principal ideal.

As for $p=3$, we have $f(x)\equiv(2+x+x^2+x^3)^2 (\operatorname{mod} 3)$, hence $\wp'=(3,2+r+r^2+r^3)$, $N(2+r+r^2+r^3)=3$ hence $\wp'=(2+r+r^2+r^3)$. Similar result for $p=5$. Finally, we get $h(K_6)=1$.

\section{Sextic Trinomials}
As expected, increasing the degree of a polynomial will increase its complexity. In the case of sextics, however, the number of possible Galois groups jumps up to sixteen. We can once again find a list of these groups in Cohen's book and we expect $S_6$ to be the most frequently occurring group.

It is much more preferable to work with sextic trinomials rather than general sextics. Again, we can reduce the possible unique forms of these trinomials to only $x^6+ax+b,x^6+ax^2+b,x^6+ax^3+b$. Note that the last of these three forms can be simplified to a quadratic in $x^3$.

It has already been shown by A Bremner and B Spearman \citep{bremner2010cyclic} that up to scaling, there exists a single, unique sextic trinomial with Galois group isomorphic to $C_6$; which was given as
$$f(x)=x^6+133x+209$$

Now we focus on this example, i.e. $K_6$ is the splitting field of $f(x)$. First of all, since $f(x)\equiv x^6+x+1(\operatorname{mod} 2)$, hence $f(x)$ is irreducible over $\mathbb{Q}$.  Then, we can compute the discriminant of the polynomial, from formula \ref{for:nxpxq}, we have $$\operatorname{Disc}(f)=(-1)^{\frac{4\times5}{2}}\cdot 5^5\cdot 133^6+(-1)^{\frac{6\times5}{2}}\cdot 6^6\cdot 209^5=-19^5\times83^2\times277^2.$$

We have $d(K_6)=f_2f_3^2f_6^2$ by theorem \ref{thm:sexdisc}, since $f_6=\operatorname{lcm}(f_2,f_3)$, hence we have $f_2^3|d(K_6)$ and $f_3^4|d(K_6)$. The relation between $d(K_6)$ and $\operatorname{Disc}(f)$ is showed in lemma \ref{lem:discpoly-field}, i.e. $\operatorname{Disc}(f)=a^2d(K_6)$. Whatever, $f_3^4|d(K_6)$ force that $f_3=19$ and $-19|f_2$. There are several possible cases for $d(K_6)$:
\begin{equation*}
\left\{ \begin{array}{l}
f_3=19,f_2=-19;\\
f_3=19,f_2=-19\times 83;\\
f_3=19,f_2=-19\times 277;\\
f_3=19,f_2=-19\times 83\times 277.
\end{array} \right.
\end{equation*}
To recognize the case for the field generated by a root of $f$, we need another method. However, this example is still under solving.


%!TEX root = ../thesis.tex
%*******************************************************************************
%*********************************** Second Chapter *****************************
%*******************************************************************************

\chapter{Formulations of the Relativistic Hydrodynamics}  %Title of the Second Chapter

\ifpdf
    \graphicspath{{Chapter2/Figs/PDF/}{Chapter2/Figs/}}
\else
    \graphicspath{{Chapter2/Figs/}}
\fi

%********************************** %First Section  **************************************
\section{Relativistic Hydrodynamics} %Section - 2.1
In the previous chapter, we explored the formulation of the left hand side of the Einstein equations.
In this chapter, we focus on the right hand side of the equations,
specifically the 3+1 treatment of perfect fluid in conservative form \cite{marti1991numerical,rezzolla2013relativistic}.

\subsection{Perfect fluid}
\label{section2.1.1}
In this section, we consider relativistic \textit{perfect fluid} (i.e. no viscous effects and heat fluxes).
The energy-momentum tensor of perfect fluid is given by
\begin{align}
    T^{\mu\nu} = \rho h u^\mu u^\nu + p g^{\mu\nu},
\end{align}
where $\rho$ is the rest-mass density,
$u^\mu$ is the fluid 4-velocity,
$h = 1 + \epsilon + \frac{p}{\rho}$ is the enthalpy,
$p$ is the pressure,
and $\epsilon$ is the specific internal energy.\\
The \textit{Lorentz factor} $W$ of the fluid with respects to the Eulerian observers is given by
the projection of fluid 4-velocity $u^\mu$ to $n^\mu$
\begin{align}
    W \coloneqq - n_{\mu} u^\mu = \alpha u^t.
\end{align}
Thus, the spatial 4-velocity of a fluid measured by Eulerian observers is
\begin{align}
    v^\mu \coloneqq \frac{\gamma_{\nu}{}^{\mu} u^\nu} {W},
\end{align}
or in component form
\begin{align}
    v^t &= 0, & v^i &=  \frac{u^i}{W} + \frac{\beta^i}{\alpha}, \\
    v_t &= \beta_i v^i, & v_i &= \frac{u_i}{W}.
\end{align}
As a result, the \textit{Lorentz factor} can also be written as
\begin{align}
    W = \frac{1}{\sqrt{1-v_i v^i}}.
\end{align}
The dynamics of fluid are governed by the equations of rest-mass conservation and energy momentum conservation.
The system is closed by the equation of state (EOS) of the fluid,
which reflects the thermodynamic properties of the fluid at a macroscopic level.
Here, we consider two common analytic equation of state as follows:
\begin{enumerate}
    \item Ideal-gas
    \begin{align}
        p = (\Gamma-1)\rho \epsilon,
    \end{align}
    where $\Gamma$ is the polytropic index of the gas.
    \item Polytropic EOS
    \begin{align}
        p = K\rho^\Gamma,
    \end{align}
    where $\Gamma$ again is the polytropic index the gas, and $K$ is a constant of proportionality.
\end{enumerate}

\subsection{Rest-mass conservation}
\label{section2.1.2}
The general-relativistic conservation of rest-mass is given by
\begin{align}
    \nabla_\mu J^\mu &= 0, & J^\mu &\coloneqq \rho u^\mu,
\end{align}
where $J^\mu$ is the rest-mass current density.
Under 3+1 decomposition, the rest-mass conservation can be written as
\begin{align}\label{eq:mass_cons}
    \partial_t \left(\sqrt{\gamma}D\right) + \partial_i \left[\sqrt{\gamma} D \left(\alpha v^i - \beta^i \right) \right] = 0,
\end{align}
where
\begin{align}
    D \coloneqq \rho \alpha u^t = \rho W,
\end{align}
is the conserved quantity.

\subsection{Energy and momentum conservation}
\label{section2.1.3}
Other than the conservation of rest-mass, the energy and momentum conservation of the fluid must also be satisfied
\begin{align}
    \nabla_\nu T^{\mu\nu} = 0.
\end{align}
Following the 3+1 decomposition of stress-energy tensor introduced in equation (\ref{eq:T_decompose}),
the fluid quantities in the case of perfect fluid become
\begin{align}
    S^{\mu\nu} &= \rho h W^2 v^\mu v^\nu + p \gamma^{\mu\nu}, \\
    S^\mu &= \rho h W^2 v^\mu, \\
    E &= \rho h W^2 - p.
\end{align}
The conservation of energy and momentum hence leads to
\begin{align}
    &\partial_t \left( \sqrt{\gamma} S_j \right) + \partial_i \left[\sqrt{\gamma} \left(\alpha S^i{}_j - \beta^i S_j\right) \right]
    = \sqrt{\gamma} \left(\frac{1}{2} \alpha S^{ik} \partial_j \gamma_{ik} + S_i \partial_j \beta^i - E \partial_j \alpha\right), \label{eq:mom_cons} \\
    &\partial_t \left( \sqrt{\gamma} E \right) + \partial_i \left[ \sqrt{\gamma} \left(\alpha S^i - \beta^i E \right) \right]
    = \sqrt{\gamma} \left(\alpha K_{ij} S^{ij} - S^i \partial_i \alpha \right). \label{eq:E_cons}
\end{align}
Equation (\ref{eq:E_cons}) can also be written as
\begin{align}
    &\partial_t \left( \sqrt{\gamma} \tau \right) + \partial_i \left[\sqrt{\gamma} \left(\alpha\left(S^i - D v^i \right) - \beta^i \tau \right)\right]
    = \sqrt{\gamma} \left(\alpha K_{ij} S^{ij} - S^i \partial_i \alpha \right). \label{eq:tau_cons},
\end{align}
to obtain better numerical accuracy,
where the conserved quantity $\tau$ is defined as
\begin{align}
    \tau \coloneqq E - D.
\end{align}

\subsection{Conservative form of hydrodynamics equations}
\label{section2.1.4}
Here, we summarize \cref{eq:mass_cons,eq:mom_cons,eq:tau_cons} and express in conservative form
\begin{align}
    \partial_t \left(\sqrt{\gamma} \mathbf{Q} \right) \partial_i \left(\sqrt{\gamma} \mathbf{F}^i \right) = \sqrt{\gamma} \mathbf{S},
\end{align}
where the conserved variables $\mathbf{Q}$, flux terms $\mathbf{F}^i$ and source terms $\mathbf{S}$ are
\begin{align}
    & \mathbf{Q} =
    \begin{pmatrix}
    D \\
    S_j \\
    \tau
    \end{pmatrix}
    =
    \begin{pmatrix}
    \rho W \\
    \rho h W^2 v_j \\
    \rho h W^2 - p - D
    \end{pmatrix},\\
    & \mathbf{F}^i =
    \begin{pmatrix}
    D\left(\alpha v^i -\beta^i \right) \\
    S_j \left( \alpha v^i - \beta^i \right) + \alpha \delta^i{}_j p \right) \\
    \tau left( \alpha v^i - \beta^i \right) + \alpha p v^i \right)
    \end{pmatrix},\\
    & \mathbf{S} =
    \begin{pmatrix}
    0 \\
    \frac{1}{2} \alpha S^{ik} \partial_j \gamma_{ik} + S_i \partial_j \beta^i - E \partial_j \alpha \\
    \alpha K_{ij} S^{ij} - S^i \partial_i \alpha
    \end{pmatrix},
\end{align}
which is commonly known as the "\textit{Valencia formulation}" of the relativistic-hydrodynamics equatons. \\
The characteristic speeds of such system is given by
\begin{align}\label{eq:valencia}
    \lambda_0 &= \alpha v^i - \beta^i \qquad \text{(triple eigenvalue)},\\
    \lambda_{\pm} &= \frac{\alpha}{1-v^2 c_s^2} \left\{ v^i \left(1-c_s^2 \right)
    \pm c_s^2 \sqrt{\left(1-v^2 \right) \left[\gamma^ii \left(1- v^2 c_s^2 \right) - {v^i}^2\left(1-c_s^2 \right) \right]} \right\},
\end{align}
where $\lambda_{\pm}$ measures the propagation speeds of the \textit{acoustic waves} of the system,
and $\lambda_0$ corresponds to the propagation speed of \textit{matter waves}.

\subsection{Conserved to Primitive variables conversion}
\label{section2.1.5}
It is not trivial to convert the conserved variables $\left(D,S_i,\tau\right)$ introduced in section \ref{section2.1.4}
to primitive variables $\left(\rho, W, v^i, p\right)$ in relativistic hydrodynamics.
One must solve non-linear equations numerically.
In \texttt{Gmunu}, we adopted the routine used in \cite{galeazzi2013implementation}.
The implementation details are included here:
\begin{Step}
    \item Calculate the rescaled variables and some useful relations which are fixed during the iterations
    \begin{align}
        S &\coloneqq \sqrt{S_i S^i}, \\
        r &\coloneqq \frac{S}{D}, & q &\coloneqq \frac{\tau}{D}, & k &\coloneqq \frac{S}{\tau + D}.
    \end{align}
    \item Determine the bounds of the root
    \begin{align}
        z_- &\coloneqq \frac{k/2}{\sqrt{1-k^2/4}}, & z_+ &\coloneqq \frac{k}{\sqrt{1-k^2}}.
    \end{align}
    \item In the inverval $\left[z_-, z_+ \right]$, we solve
    \begin{align}\label{eq:cons2prim_root}
        f(z) = z - \frac{r}{\hat{h}(z)},
    \end{align}
    where
    \begin{align}
        \hat{h}(z) &= \left(1+\hat{\epsilon}(z) \right) \left( 1+ \hat{a}(z) \right),
    \end{align}
    \begin{align}
        \hat{\epsilon}(z) &= \hat{W}(z)q - zr + \frac{z^2}{1+\hat{W}(z)},
        & \hat{a}(z) &= \frac{\hat{p}(z)}{\hat{\rho}(z)\left( 1 + \hat{\epsilon}(z) \right) },
    \end{align}
    \begin{align}
        \hat{p}(z) &= p\left(\hat{\rho}(z), \hat{\epsilon}(z) \right),
        & \hat{\rho}(z) &= \frac{D}{\hat{W}(z)},
        & \hat{W}(z) &= \sqrt{1+z^2}.
    \end{align}
    Equation (\ref{eq:cons2prim_root}) is solved using Illinois algorithm in \texttt{Gmunu}.
    Note that during the iterations, we enforce the density $\rho$ and the specific energy $\epsilon$ fail into the validity region of the EOS,
    i.e., we evalue the updated $\rho$ and $\epsilon$ with
    $\hat{\rho}(z) = \max\left(\min\left(\rho_\max{},\hat{\rho}\right),\rho_\min{}\right)$ and
    $\hat{\epsilon}(z) = \max\left(\min\left(\epsilon_\max{},\hat{\epsilon}\right),\epsilon_\min{}\right)$.
    \item With the root $z_0$ of \cref{eq:cons2prim_root},
    we can then work out the primitive variables $\left(\rho, \epsilon, p\right)$ respectively with the equations used in step 3.
    For the velocity $v^i$, it can be obtained with $z$ by
    \begin{align}
        \hat{v}^i(z) = \frac{S^i/D}{\hat{h}(z)\hat{W}(z)}.
    \end{align}
\end{Step}

%********************************** %Second Section  *************************************
\section{The reference-metric formalism} %Section - 2.2
\label{section2.2}
The reference-metric formalism, a generalization of the "\textit{Valencia}" formulation, was original proposed by Montero \cite{montero2014general} in 2014 for general relativistic hydrodynamics (GRHD)
and later extended to ideal magnetohydrodynamics.
With the conformal decomposition introduced in section \ref{section1.3.1},
the determinant of the spatial metric can be rewritten as
\begin{align}\label{eq:reference_metric_1}
    \begin{split}
    \sqrt{\gamma} &= \psi^6 \sqrt{\tilde{\gamma}} \\
    &= \sqrt{\hat{\gamma}} \psi^6 \sqrt{\tilde{\gamma}/\hat{\gamma}},
    \end{split}
\end{align}
where $\hat{\gamma}_{ij}$ is the \textit{time-independent reference metric}
which can be chosen to fit the mesh grid in curvilinear coordinate.
Therefore, the "\textit{Valencia}" formulation in equation (\ref{eq:valencia}) can be generalized as
\begin{align}
    \partial_t \mathbf{q} + \hat{\nabla}_i \mathbf{f}^i = \mathbf{s},
\end{align}
where $\hat{\nabla}_i$ is the corvariant derivatives sociated with the time-independent reference metric $\hat{\gamma}_{ij}$,
$\left(\mathbf{q},\mathbf{f}^i,\mathbf{s}\right)$ are the $\textit{conformally rescaled}$ conserved variables,
fluxes and source terms respectively.\\
Comparing to the "\textit{Valencia}" formulation,
the hydrodynamical equations in reference-metric approach are numerically more accurate in curvilinear coordinates in multi-dimensional simulation,
especially when symmetry is imposed.
Equation (\ref{eq:reference_metric_1}) can be further written as
\begin{align}
    \partial_t \mathbf{q} + \frac{1}{\sqrt{\hat{\gamma}}}\partial_j \left(\sqrt{\hat{\gamma}} \mathbf{f}^j \right) = \mathbf{s} + \mathbf{s}_{geom},
\end{align}
where $\mathbf{s}_{geom}$ are the \textit{geometrical} source terms
which contain the 3-Christoffel symbols $\hat{\Gamma}^i{}_{jk}$ associated with the reference metric $\hat{\gamma}_{ij}$. \\
Note that the momentum conservation in this expression satisfy to \textit{machine precision} rather than to the level of truncation error
due to the fact that the geometrical source terms $\mathbf{s}_{geom}$ are identically vanishing
for the components associated with ignorable coordinates in the metric.

\subsection{General relativistic hydrodynamics (GRHD) equations}
\label{section2.2.1}
The equations of relativistic hydrodynamics in reference-metric approach is given by
\begin{align}
    \partial_t \left( q_{D} \right) + \frac{1}{\sqrt{\hat{\gamma}}} \partial_j \left[ \sqrt{\hat{\gamma}} \left( f_{D} \right)^j \right]
    &= 0, \\
    \partial_t \left( q_{S_i} \right) + \frac{1}{\sqrt{\hat{\gamma}}} \partial_j \left[ \sqrt{\hat{\gamma}} \left( f_{S_i} \right)^j \right]
    &= s_{S_i} + \hat{\Gamma}^l{}_{ik}\left( f_{S_l} \right)^k, \\
    \partial_t \left( q_{\tau} \right) + \frac{1}{\sqrt{\hat{\gamma}}} \partial_j \left[ \sqrt{\hat{\gamma}} \left( f_{\tau} \right)^j \right]
    &= s_{\tau}, \\
\end{align}
The conformally rescaled conserved quantities $\mathbf{q}$ are
\begin{align}
    q_D & \coloneqq \psi^6 \sqrt{\tilde{\gamma}/\hat{\gamma}} D = \psi^6 \sqrt{\tilde{\gamma}/\hat{\gamma}} \left( \rho W \right), \\
    q_{S_i} & \coloneqq \psi^6 \sqrt{\tilde{\gamma}/\hat{\gamma}} S_i = \psi^6 \sqrt{\tilde{\gamma}/\hat{\gamma}} \left( \rho h W^2 v_i \right), \\
    q_\tau & \coloneqq \psi^6 \sqrt{\tilde{\gamma}/\hat{\gamma}} \tau = \psi^6 \sqrt{\tilde{\gamma}/\hat{\gamma}} \left( \rho h W^2 - p - D \right).
\end{align}
The flux terms $\mathbf{f}^i$ are
\begin{align}
    \left(f_D \right)^i & \coloneqq \psi^6 \sqrt{\tilde{\gamma}/\hat{\gamma}} \left( D \hat{v}^i \right), \\
    \left(f_{S_j} \right)^i & \coloneqq \psi^6 \sqrt{\tilde{\gamma}/\hat{\gamma}} \left(S_j \hat{v}^i + \delta^i{}_j \alpha p \right), \\
    \left(f_\tau \right)^i & \coloneqq \psi^6 \sqrt{\tilde{\gamma}/\hat{\gamma}} \left(\tau \hat{v}^i + \alpha p v^i \right),
\end{align}
where $\hat{v}^i \coloneqq \alpha v^i -\beta^i$.\\
Finally, the source terms $\mathbf{s}$ are
\begin{align}
    s_D &= 0, \\
    s_{S_i} &= \alpha \psi^6 \sqrt{\tilde{\gamma}/\hat{\gamma}}
    \left[ - T^{00} \alpha\partial_i \alpha + T^0{}_k \hat{\nabla}_i \beta^k
    + \frac{1}{2} \left( T^{00} \beta^j \beta^k + 2 T^{0j} \beta^k + T^{jk} \right) \hat{\nabla}_i \gamma_{jk} \right], \\
    s_\tau &= \alpha \psi^6 \sqrt{\tilde{\gamma}/\hat{\gamma}}
    \left[ T^{00} \left(K_{ij}\beta^i \beta^j - \beta^k \partial_k \alpha \right)
    + 2 T^{0j} \left( 2 K_{jk} \beta^k - \partial_j \alpha \right)
    + T^{ij} K_{ij} \right].
\end{align}


\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath,amsfonts,tikz,hyperref}
\usepackage{fontenc}
\usepackage{graphicx}
\usepackage[euler-digits]{eulervm}
\title{FinElt Quick Start Guide}
\author{William McLean}
\date{\today}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\maketitle
To use FinElt you need to install \href{www.julialan.org}{Julia}~and
\href{www.geuz.org/gmsh/}{Gmsh}~\cite{Gmsh}.  Versions for Windows or
OS~X can be downloaded from the project websites.  Under Linux, you
might be able to use your distribution's package manager.  For
example, I develop FinElt on \href{www.fedoraproject.org}{Fedora~31},
and can simply do
\begin{verbatim}
# dnf install julia gmsh
\end{verbatim}
At the time of writing (January 2020), this command installs Julia~1.2.0 and
Gmsh~4.4.1.

Whatever platform you are using, once Julia is installed you can
easily set up FinElt:
\begin{verbatim}
julia> Pkg.clone("https://github.com/billmclean/FinElt.jl")
\end{verbatim}
This guide will walk you through two examples.  The first is a
``hello world'' problem, that explains the basic usage of FinElt.
The second is more elaborate and shows more of the capabilities of
the package.  The source code for both problems is in the
\texttt{examples/keyhole} directory of the package on github.
You should copy this directory to a convenient location.

After working through these two examples, I recommend that you
download and study the
\href{http://www.geuz.org/gmsh/doc/texinfo/gmsh.pdf}{Gmsh manual}.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section*{First Example}

Consider the keyhole-shaped region~$\Omega$ shown in
Figure~\ref{fig: simple bvp}, with boundary~$\Gamma=\partial\Omega$.
We seek the solution~$u$ of the Poisson problem
\begin{equation}\label{eq:  simple bvp}
\begin{aligned}
-\nabla^2 u&=4&&\text{in $\Omega$,}\\
u&=0&&\text{on $\Gamma$,}
\end{aligned}
\end{equation}
where $\nabla^2u=\nabla\cdot(\nabla u)=u_{xx}+u_{yy}$ denotes the
Laplacian of~$u$. Recall that the Sobolev space~$H^1(\Omega)$
consists of the functions in~$L_2(\Omega)$ whose first-order, weak
partial derivatives also belong to~$L_2(\Omega)$, and that
\[
H^1_0(\Omega)=\{\,v\in H^1(\Omega):\text{$u=0$ on~$\partial\Omega$}
	\,\}.
\]
The first Green identity asserts that
\begin{equation}\label{eq: first Green}
\int_\Omega\nabla u\cdot\nabla v=\int_\Omega(-\nabla^2 u)v
	+\int_{\partial\Omega}\frac{\partial u}{\partial n}\,v,
\end{equation}
where $\boldsymbol{n}$ denotes the outward unit normal to~$\Omega$,
and consequently we say that $u\in H^1_0(\Omega)$ is a \emph{weak
solution} of the boundary-value problem~\eqref{eq:  simple bvp} if
\begin{equation}\label{eq: weak bvp}
\int_\Omega\nabla u\cdot\nabla v=\int_\Omega 4v
	\quad\text{for all $v\in H^1_0(\Omega)$.}
\end{equation}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{figure}
\caption{A simple boundary-value problem.}
\label{fig: simple bvp}
\begin{center}
\begin{tikzpicture}[scale=1.5]
\draw[blue, thick] (-1,0) -- (-1,-2) -- (1,-2) -- (1,0);
\draw[blue, thick] (1,0) arc
[radius=sqrt(2), start angle=-45, end angle=225];
\node at (0,0.5) {$-\nabla^2u=4$};
\node[above,right] at (1,2) {$u=0$};
\end{tikzpicture}
\end{center}
\end{figure}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{figure}
\caption{Reading the geometry description file into Gmsh}
\label{fig: open file}
\begin{center}
\includegraphics[scale=0.3]{images/open_geo_file.png}
\end{center}
\end{figure}

\begin{figure}
\caption{The Gmsh options window.}
\label{fig: options}
\begin{center}
\includegraphics[scale=0.4]{images/options.png}
\end{center}
\end{figure}

\begin{figure}
\caption{A triangulation of $\Omega$.}
\label{fig: triangulation}
\begin{center}
\includegraphics[scale=0.3]{images/triangulation.png}
\end{center}
\end{figure}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\paragraph{Geometry description.}
To compute a numerical approximation to this weak solution, we start
by triangulating $\Omega$.  Use a text editor to read the
geometry description file~\verb!keyhole.geo! and observe how the
domain~$\Omega$ is defined by first specifying the coordinates of
points around its boundary, and then specifying how these points are
connected by line segments or circular arcs whose union is~$\Gamma$.
Notice that Gmsh thinks of $\Omega$ as a surface in 3D (lying in the
plane~$z=0$).

Start Gmsh and use the \verb!File! menu to open
\verb!keyhole.geo! as shown in Figure~\ref{fig: open file}.
From the \verb!Tools! menu, open the \verb!Options! window, select
\verb!Geometry! in its left pane, and tick the checkbox to display
\verb!Point labels!; see Figure~\ref{fig: options}.  The main Gmsh
window should now display the keyhole domain~$\Omega$ and show the
locations of the points defined in \verb!keyhole.geo!.

\paragraph{Mesh generation.}
To generate a triangulation, click on the \verb!+! next to
the \verb!Mesh! label in the left pane of the main Gmsh window and
then click on \verb!2D!.  The result should look something like
Figure~\ref{fig: triangulation}.  From the \verb!File! menu, select
the \verb!Export ...! option to create a text file
\verb!keyhole.msh!.

\paragraph{Important:} FinElt does not support more recent Gmsh file
formats, so when the \verb!MSH Options!  window opens you must select
\verb!Version 2 ASCII! from the drop-down menu.

\paragraph{Finite element solution.}
Let $\Omega_h$ denote the triangulated domain, where, in the customary
way, $h$ is the maximum diameter of the triangles.  Since $\Omega_h$
is a polygon it can only approximate $\Omega$, whose boundary
$\Gamma$ contains circular arcs.  We introduce the finite element
space~$\mathcal{S}_h$ made up of the functions
$v:\Omega_h\to\mathbb{R}$ that are continuous, piecewise-linear and
vanish on~$\partial\Omega_h$.  The finite element
solution~$u_h\in\mathcal{S}_h$ is then defined by requiring that
\begin{equation}\label{eq: finite elt problem}
\int_{\Omega_h}\nabla u_h\cdot\nabla v=\int_{\Omega_h} 4v
	\quad\text{for all $v\in\mathcal{S}_h$,}
\end{equation}
which is a finite-dimensional approximation to the variational
problem~\eqref{eq: weak bvp}.

\paragraph{Julia script.}
Use a text editor to read the file \verb!keyhole.jl!.  This
Julia script begins by reading the mesh data file \verb!keyhole.msh!
and creating a data structure called \verb!mesh! that stores (amongst
other things) the coordinates of the vertices of the triangulation as
well as the connectivity matrix that specifies which points make up
each triangle.  Look in the file
\verb!src/FinElt/Gmsh.jl! to see how this data structure is defined via
the \verb!Mesh! type.

The script then creates a second data structure~\verb!vp! that
describes the variational problem; look in \verb!FinElt/FEM.jl!.  We
specify that an essential (that is, Dirichlet) boundary condition
applies over all of~$\Gamma$.  (Zero boundary values are assumed by
default.)  Next, we specify the bilinear form on the LHS
of~\eqref{eq: finite elt problem}, and the linear functional on the
RHS.

Once \verb!vp! is completely specified, the script generates the
(sparse) linear system for the degrees of freedom, that is, the
values of~$u_h$ at the interior nodes of the triangulation.  This
linear system is solved using the \verb!\! operator and the full
nodal vector~\verb!u! is constructed by including the values at both
the free and fixed nodes (the latter being all zeros).  As output,
the script writes a postprocessing file \verb!keyhole.pos!.

\paragraph{Output and visualization.}
Execute the script in the usual way:
\begin{verbatim}
julia> include("keyhole.jl")
\end{verbatim}
To visualize the solution, return to the Gmsh main window.  From
the \verb!File! menu, select \verb!Open! and choose
\verb!keyhole.pos!; the result should look like
Figure~\ref{fig: postprocess}.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{figure}
\caption{View after merging the postprocessing file.}
\label{fig: postprocess}
\begin{center}
\includegraphics[scale=0.4]{images/postprocess.png}
\end{center}
\end{figure}

\begin{figure}
\caption{The Warp plugin.}
\label{fig: Warp}
\begin{center}
\includegraphics[scale=0.4]{images/warp.png}
\end{center}
\end{figure}

\begin{figure}
\caption{View after warping the surface.}
\label{fig: warped surface}
\begin{center}
\includegraphics[scale=0.4]{images/warped_surface.png}
\end{center}
\end{figure}

\begin{figure}
\caption{View after using the mouse to rotate the surface in 3D.}
\label{fig: 3D view}
\begin{center}
\includegraphics[scale=0.4]{images/3Dview.png}
\end{center}
\end{figure}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

In the left pane of the Gmsh window, under \verb!Post-processing!,
are two check boxes labelled \verb!u!~and \verb!u_warp!.
Uncheck the second of these; the display should now look more
reasonable.  However, because Gmsh is really a 3D mesh generator,
when solving a 2D problem we have to use something
of a kludge to obtain a surface plot of the solution.  From the
\verb!Tools! menu, open the \verb!Plugins! window,
scroll to the bottom of its left pane and select \verb!Warp!.  In the
right pane, change \verb!View! from \verb!-1! to \verb!0!, and change
\verb!OtherView! from~\verb!-1! to~\verb!1!; see Figure~\ref{fig:
Warp}.  After clicking the \verb!Run! button, you should see something
like Figure~\ref{fig: warped surface}.  By left-clicking and dragging
the display, you can rotate the surface to obtain a view like the one
in Figure~\ref{fig: 3D view}.

\paragraph{Exercise.}
Solve the problem again using a finer mesh.  Hint: select \verb!Mesh!
in the left pane of the \verb!Options! window, choose the
\verb!General! tab and adjust the \verb!Min/Max element size!.  You
might also want to go to the \verb!Visibility! tab and
uncheck the \verb!Surface edges! box
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section*{Second Example}
Suppose now that $\Omega$ is the keyhole-shaped region with an
interior circular hole shown in Figure~\ref{fig: complicated bvp}.
We want to find $u$ satisfying $-\nabla\cdot(a\nabla u)=f$
in~$\Omega$, where the coefficient $a(x)$ and source term~$f(x)$ are
piecewise constant: $a=1$~and $f=1$ in the part of~$\Omega$ that is
shaded light blue, whereas $a=10$~and $f=4$ in the part shaded
light green. In addition, we impose Dirichlet boundary conditions on
the parts of~$\Gamma=\partial\Omega$ coloured dark blue or black, and
Neumann conditions on the part coloured violet or red.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{figure}
\caption{A more complicated boundary-value problem.}
\label{fig: complicated bvp}
\begin{center}
\begin{tikzpicture}[scale=1.5]
\path[fill=blue!10] (-1,0) -- (-1,-2) -- (1,-2) -- (1,0)
arc [radius=sqrt(2), start angle=-45, end angle=225];
\draw[blue,thick] (1,0)
arc [radius=sqrt(2), start angle=-45, end angle=225];
\draw[red,thick,fill=white] (0,1) circle [radius=sqrt(2)/2];
\draw[green!40,fill=green!40] (-0.5,-0.5) -- (-0.5,-1.5) --
(0.5,-1.25) -- (0.5, -0.75) -- (-0.5,-0.5);
\draw[black,thick] (-1,-2) -- (1,-2);
\node[above,blue] at (0,2.4142) {$u=-|\boldsymbol{x}|/2$};
\node[below] at (0,-2) {$u=0$};
\draw[violet,ultra thick] (-1,0) -- (-1,-2);
\node[left,violet] at (-1.0,-1.0)
{$\dfrac{\partial u}{\partial n}=-1$};
\draw[violet,ultra thick] (1,0) -- (1,-2);
\node[right,violet] at (1.0,-1.0)
{$\dfrac{\partial u}{\partial n}=-1$};
\node[red] at (0,1) {$\dfrac{\partial u}{\partial n}=0$};
\node at (-4,2) {$-\nabla\cdot(a\nabla u)=f$};
\draw[blue!10,fill=blue!10] (-4.5,1) -- (-3.5,1) -- (-3.5,0)
-- (-4.5,0) -- (-4.5,1);
\node at (-4,0.75) {$a=1$};
\node at (-4,0.25) {$f=1$};
\draw[green!40,fill=green!40] (-4.5,-1) -- (-3.5,-1) -- (-3.5,-2)
-- (-4.5,-2) -- (-4.5,-1);
\node at (-4,-1.25) {$a=10$};
\node at (-4,-1.75) {$f=4$};
\end{tikzpicture}
\end{center}
\end{figure}

\begin{figure}
\caption{The computed solution for the second example.}
\label{fig: second soln}
\begin{center}
\includegraphics[scale=0.4]{images/complic_keyhole.png}
\end{center}
\end{figure}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\paragraph{Weak formulation.}
Define the trial set~$\mathcal{S}$ and test space~$\mathcal{T}$ by
\begin{align*}
\mathcal{S}&=\{\,v\in H^1(\Omega):
\text{$v=-|x|/2$ for $x\in\text{DarkBlue}$, $v=0$ for
$x\in\text{Black}$}\,\},\\
\mathcal{T}&=\{\,u\in H^1(\Omega):
\text{$v=0$ for $x\in\text{DarkBlue}\cup\text{Black}$}\,\}.
\end{align*}
Based on the first Green identity~\eqref{eq: first Green}, the weak
solution~$u\in\mathcal{S}$ satisfies
\begin{multline*}
\int_{\text{LightBlue}}\nabla u\cdot\nabla v
	+10\int_{\text{LightGreen}}\nabla u\cdot\nabla v\\
	=\int_{\text{LightBlue}}v+\int_{\text{LightGreen}}4v
	-\int_{\text{Violet}}v
\quad\text{for all $v\in\mathcal{T}$.}
\end{multline*}

\paragraph{Finite element approximation.}
We approximate $\Omega$ by a triangulated polygon~$\Omega_h$ and
define a finite dimensional trial set~$\mathcal{S}_h$ that consists
of all continuous, piecewise-linear functions~$v$ such that
\[
v(\boldsymbol{x})=\begin{cases}
-|\boldsymbol{x}|/2,&
\text{$\boldsymbol{x}$ is a node in~$\text{DarkBlue}_h$,}\\
0,&\text{$\boldsymbol{x}$ is a node in~$\text{Black}_h$,}\\
\end{cases}
\]
where $\text{DarkBlue}_h$~and $\text{Black}_h$ are the parts
of the boundary of~$\Omega_h$ corresponding to $\text{DarkBlue}$~and
$\text{Black}$.  The finite dimensional test
space~$\mathcal{T}_h$ is the set of~$\mathcal{S}_h$ consisting of
those~$v$ that vanish on~$\text{DarkBlue}_h$ and
on~$\text{Black}_h$.  The finite element
solution~$u_h\in\mathcal{S}_h$ is then defined by
\begin{multline*}
\int_{\text{LightBlue}_h}\nabla u_h\cdot\nabla v
	+10\int_{\text{LightGreen}_h}\nabla u_h\cdot\nabla v\\
	=\int_{\text{LightBlue}_h}v+\int_{\text{LightGreen}_h}4v
	-\int_{\text{Violet}_h}v
\quad\text{for all $v\in\mathcal{T}_h$.}
\end{multline*}

\paragraph{Computing and visualizing the solution.}
Use a text editor to look at the geometry description
in \verb!complic_keyhole.geo!, and then look at the source code in
\verb!complic_keyhole.jl!.  Notice that the Julia script runs Gmsh
twice, using the latter's command-line interface.  (The
\verb!-format msh22! option is needed to generate the older file format.)
The first call performs the mesh generation, creating the file
\verb!complic_keyhole.msh!, and the second runs the Gmsh
script \verb!complic_keyhole.script! to display a surface plot of the
solution.  Thus, the whole process requires only the single command
\begin{verbatim}
julia> include("complic_keyhole.jl")
\end{verbatim}
to produce the output shown in Figure~\ref{fig: second soln}.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{thebibliography}{0}
\bibitem{Gmsh}
C.~Geuzaine and J.-F.~Remacle. Gmsh: a three-dimensional finite
element mesh generator with built-in pre- and post-processing
facilities. \emph{International Journal for Numerical Methods in
Engineering} 79(11), pp.~1309--1331, 2009.
\end{thebibliography}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{document}