%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                 %
%  GEANT manual in LaTeX form                              %
%                                                                 %
%  Michel Goossens (for translation into LaTeX)                   %
%  Version 1.00                                                   %
%  Last Mod. Jan 24 1991  1300   MG + IB                          %
%                                                                 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Origin{L.Urb\'{a}n}
\Version{Geant 3.16}\Routid{PHYS431}
\Submitted{12.03.82}  \Revised{16.12.93}
\Makehead{Ionisation processes for heavy ions}

\section{Subroutines}

For a description of the subroutines used to calculate the energy
loss tables for protons, see {\tt [PHYS430]}.

\Shubr{GTHION}{}

\Rind{GTHION} transports heavy ions ($A,Z > 1$, particle
type 8). For the moment the
only discrete processes activated are $\delta$-ray and \v{C}erenkov
photon generation.

\section{Method}

The mean energy loss of the heavy ions can be expressed as:
\begin{equation}
\Delta E = Q^{2} S_{p} \left ( \frac{T}{A} \right ) \rho t
\end{equation}
where
\begin{DLtt}{MMMMMMMMM}
\item[$Q$] charge of the ion;
\item[$\displaystyle S_{p} \left ( \frac{T}{A} \right )$]
energy loss by the a proton with kinetic energy $T_{p} = T/A$;
\item[$T,A$] kinetic energy and mass number of the ion;
\item[$\rho$] density of the medium;
\item[$t$] step length
\end{DLtt}

This formula is used to calculate the ionisation loss for all the charged
hadrons.

In the case of ions, the charge of the ion makes the problem more difficult.
Electron-exchange processes with the atoms change the
charge as the ion traverses the medium. The main features of this
process can be summarised as follows:
\begin{itemize}
\item $Q$ loses the dependence on the initial ion charge after a
very short initial step, usually $\ll 1\mu$ for condensed media;
\item $Q$ does depend on the velocity of the ion;
\item even in the case of constant velocity, $Q$ fluctuates around
a mean value;
\item $Q$ features a small dependence on the medium, especially for
small values of $T$.
\end{itemize}
\begin{table}
\begin{centering}
\begin{tabular}{|c|c|c|}
\hline
T/A & He ions & Pb ions \\
MeV & error (\%) & error (\%) \\
\hline
10 & 3 &  1 \\
5 & 4 & 3 \\
3 & 10 & 4 \\
\hline
\end{tabular}
\caption{Comparison of the measured and calculated $\Delta$E for 10 MeV/A
ions in Pb}
\end{centering}
\label{tb:phys431-1}
\end{table}

If we want to calculate the energy loss of the ion, we need a good
parametrisation for the quantity $Q=Q_{eff}$. We give here a
relatively simple parametrisation~\cite{bib-ANZI,bib-ANZ1}:
\begin{eqnarray}
\label{eq:phys431-2}
Q_{eff} & = & Z_{I} \left [ 1- \left ( 1.034 - 0.1777 e^{-0.08114\;Z_{I}}
\right ) e^{-v} \right ] \\
v & = & 121.4139 \frac{\beta}{Z_{I}^{2/3}} + 0.0378
\sin \left ( 190.7165 \frac{\beta}{Z_{I}^{2/3}} \right ) \nonumber
\end{eqnarray}
where $\beta$ is the velocity and $Z_{I}$ the atomic number of the ion
(i.e. the charge of the bare nucleus).

\begin{table}
\begin{centering}
\begin{tabular}{|r|rr|rr|r|r|rr|rr|r|r|}
\hline
\multicolumn{1}{|c|}{T (MeV)} & \multicolumn{6}{c|}{417 $\mu$g cm$^{-2}$}
& \multicolumn{6}{c|}{110 $\mu$g cm$^{-2}$} \\[0.1cm]
& \multicolumn{4}{c|}{MC (keV)} & \multicolumn{2}{c|}{data (keV)} &
\multicolumn{4}{c|}{MC (keV)} & \multicolumn{2}{c|}{data (keV)} \\
& $\Delta$E & dif & FWHM & dif & $\Delta$E & FWHM &
$\Delta$E & dif & FWHM & dif & $\Delta$E & FWHM \\
\hline
4.88  & &  & & & & & 785 & 8 & 59 & -23 & 725 & 77 \\
9.85 & 3600 & 14 & 138 & -33 & 3150 & 206 & 816 & 8 & 67 &  -26 & 756 & 91 \\
19.79 & 3090 & 3 & 142 & -35 & 2990 & 218 & 719 & 3 & 70 & -32 & 699 & 103 \\
29.27 & 2660 & 1 & 141 & -31 & 2630 & 204 & & & & & & \\
29.75 & & & & & & & 620 & 4 & 69 & -26 & 598 & 93 \\
39.70 & 2320 & 1 & 138 & -28 & 2300 & 191 & 550 & 4 & 68 & -24 & 528 & 90 \\
\hline
\end{tabular}
\caption{Comparison of the measured and calculated $\Delta$E and FWHM
for O ions in Al; errors are in percent.}
\end{centering}
\label{tb:phys431-2}
\end{table}

It can be seen that (\ref{eq:phys431-2}) neglects the (small) medium
dependence of $Q_{eff}$. For very high energies ($\beta \rightarrow 1$)
$Q_{eff} \rightarrow Z_{I}$. For very low energies ($T/A \sim$ few keV)
the formula breaks down. $Q_{eff}$ can even become negative for $T/A <
20$ keV and $Z_{I} > 20$. However this is not a serious source of
error when calculating $\Delta E$, since in this case the range of
the ion is very small, and it can almost be said that it stops
{\it immediately}.

The calculation of the energy loss straggling (fluctuations) differs
from that of {\it normal} charged hadrons. For the charged hadrons
the fluctuations come from the statistical nature of the
projectile-atom interactions; for the ions there is another process which
broadens the energy loss distribution: the fluctuation of the charge.
For heavier ions this process dominates the energy loss straggling
for $T/A \leq 10$ MeV.

\begin{figure}[hbt]
     \centering
     \epsfig{file=eps/phys431-1.eps,width=14cm}
     \caption{Stopping powers in Carbon}
     \label{fg:phys431-1}
\end{figure}

The heavy ions are in the {\it Gaussian regime} (see {\tt [PHYS332]})
of the collisional fluctuations even in the case of very thin absorbers.
If $T/A$ is not too high, the $\sigma^{2}$ of the distribution:
\begin{equation}
\label{eq:phys431-3}
\sigma^{2}_{coll} = D \frac{Q_{eff}^{2} Z}{A} \rho \; t \; m
\left [ 1 + \frac{T_{A}}{M_{u}} + \frac{1}{2} \left ( \frac{T_{A}}{M_{u}}
\right ) ^{2} \right ]
\end{equation}
where
\begin{DLtt}{MMMMMMMM}
\item[$D$] $\displaystyle 0.307 \frac{\mbox{MeV cm$^{2}$}}{\mbox{g}}$;
\item[$T_{A}$] $\displaystyle \frac{T}{A}$;
\item[$M_{u}$] atomic mass unit;
\item[$Z$] atomic number of the medium;
\item[$A$] mass number of the medium.
\end{DLtt}

\begin{table}
\begin{centering}
\begin{tabular}{|r|rr|r|rr|r|rr|r|}
\hline
& \multicolumn{9}{c|}{Pb ions in gas (energies in MeV)} \\
& \multicolumn{3}{c|}{N$_{2}$} &
\multicolumn{3}{c|}{Ar} &
\multicolumn{3}{c|}{Xe} \\
& \multicolumn{2}{c}{MonteCarlo} & \multicolumn{1}{c|}{data} &
\multicolumn{2}{c}{MonteCarlo} & \multicolumn{1}{c|}{data} &
\multicolumn{2}{c}{MonteCarlo} & \multicolumn{1}{c|}{data} \\
t (cm) &
$\Delta$E & FWHM & FWHM & $\Delta$E & FWHM & FWHM & $\Delta$E & FWHM & FWHM \\
\hline
0.2 & 25.9 & 1.15 & 1.1 & 25.4 & 1.30 & 1.4 & 51.1 & 2.26 & 2.5 \\
0.4 & 53.6 & 1.62 & 1.5 & 52.4 & 1.84 & 1.9 & 107.0 & 3.22 & 3.3 \\
0.8 & 112.0 & 2.25 & 2.0 & 111.0 & 2.59 & 2.6 & 236.0 & 4.51 &  \\
1.2 & 162.0 & 2.37 & 2.0 & 168.0 & 2.95 & 2.8 & & & \\
\hline
\end{tabular}
\caption{Comparison of the measured and calculated and FWHM
for 1.4 MeV/A Pb ions in gas.}
\end{centering}
\label{tb:phys431-3}
\end{table}

Analysing the experimental straggling data it is possible to find that
the electron-exchange charge fluctuations can be described by a Gaussian
with width:
\begin{equation}
\label{eq:phys431-4}
\sigma^{2}_{ch} = D \frac{Q_{eff}^{2} Z}{A} \rho \; t \; m \frac{C}{2}
\left ( 1 - \frac{Q_{eff}}{Z_{I}} \right )
\end{equation}
where the parameter $C \sim 2.5$ has been derived
from the experimental straggling data.

If $Q_{eff} \rightarrow Z_{I}$, which is the case for high energy
heavy ions and for few MeV/A He ions, then $\sigma^{2}_{ch} \rightarrow 0$.

Comparing equations (\ref{eq:phys431-3}) and (\ref{eq:phys431-4}) it can be
seen that for heavy ions and for $T/A \ll M_{u}$ $\sigma^{2}_{ch}
> \sigma^{2}_{coll}$. The total energy loss fluctuation can be
described by a Gaussian distribution with:
\begin{equation}
\sigma^{2} = \sigma^{2}_{ch} + \sigma^{2}_{coll}
\end{equation}

The mean energy loss and energy loss fluctuation calculation is performed
in the routine \Rind{GTHION}, making use of the proton energy loss tables.

{\bf Note:} The Gaussian fluctuation gives too broad a distribution for
high energy in the case of thin absorbers. A correction has been introduced
in \Rind{GTHION} which cures this discrepancy. In the absence of high
energy straggling data for ions, the correction has been tuned using
high energy $\pi$ energy loss data, where the $\pi$ has been tracked
by \Rind{GTHION}.

\section{Comparison with data}

\subsection{Mean energy loss and range}

A test has been made against the distributions reported in~\cite{bib-HEIN}
for He ions at 1 and 10 MeV/A and for O and Pb ions at 1, 10 and
100 MeV/A in carbon and the results were found
to be correct within $5\%$ both for the energy loss and the range.

\begin{figure}[hbt]
     \centering
     \epsfig{file=eps/phys431-2.eps,width=14cm}
     \caption{Stopping powers in Lead}
     \label{fg:phys431-2}
\end{figure}

Another comparison has been made with the tables of~\cite{bib-HUBE}
for 10 MeV/A ions in lead, with
the results shown in table~\ref{tb:phys431-1}.
Another comparison with the same tables is shown in Figs.~\ref{fg:phys431-1}
and \ref{fg:phys431-2}.

A comparison with the tables in~\cite{bib-ANZI,bib-ANZ1} for T/A $<$ 1 MeV/A
gives  an error on $(dE/dx) \leq 10-20\%$.

\begin{table}
\begin{centering}
\begin{tabular}{|r|rr|r|}
\hline
\multicolumn{4}{|c|}{He ions in Ar (energies in keV)} \\
& \multicolumn{2}{c}{MonteCarlo} & \multicolumn{1}{c|}{data} \\
t (cm) &
$\Delta$E & FWHM & FWHM \\
\hline
0.5 & 444 & 38 & $\sim$40 \\
1.0 & 913 & 53 & $\sim$60 \\
2.0 & 1940 & 78 & $\sim$80 \\
\hline
\end{tabular}
\caption{Comparison of the measured and calculated and FWHM
for 1.4 MeV/A He ions in gas.}
\end{centering}
\label{tb:phys431-4}
\end{table}

\subsection{Comparison of FWHM}

A comparison has been made with
low energy data ($0.3 \leq T/A \leq 2.5$ MeV/A) in very
thin absorbers (1,7 $\mu$m, 0.4 $\mu$m). Data are taken from
the tables in~\cite{bib-TSCH} for
energy loss of O ions in Al, and the
results are reported in table~\ref{tb:phys431-2}. The quoted experimental
error for these data is $1\%$ statistical + $5\%$ systematic for
417 $\mu$g cm$^{-2}$ data and $2\%$ statistical + $10\%$ systematic for
110 $\mu$g cm$^{-2}$ data.

Plots of energy deposition distributions of 1.4 MeV/A lead and helium
ions in gases can be found in~\cite{bib-GEIS}.
From these we have measured the FWHM and compared them with the ones given
by the MonteCarlo. The results can be seen in table~\ref{tb:phys431-3}
and ~\ref{tb:phys431-4}. It
should be noted that the absorber thickness of, for example, Ar compares
with that of the preceding case (0.2 cm Ar = 356 $\mu$g cm$^{-2}$ and
1.2 cm Ar = 2136 $\mu$g cm$^{-2}$).

Similar comparison has been made for C ions of energies 17.1 and 39.4 MeV in
isobuthane~\cite{bib-SCHM} and the agreement between data and {\tt GEANT}
for FWHM is within 30\%.


%!TEX root = ../dissertation.tex
\begin{savequote}[75mm]
Science may be described as the art of systematic over-simplification.
\qauthor{Karl Popper}
\end{savequote}

\chapter{Previous Work}
\label{chap:previous_work}
\newthought{Several works have addressed tasks that involve CV and NLP}, such as object detection based on natural language expressions \cite{hu_natural_2015}\cite{guadarrama_understanding_2016}, image captioning \cite{hendricks_deep_2015}\cite{gan_semantic_2016}\cite{johnson_densecap:_2016}\cite{yang_review_2016} and visual question answering (VQA) \cite{goyal_making_2016}\cite{zhu_visual7w:_2015}\cite{krishna_visual_2016}\cite{agrawal_vqa:_2015}\cite{teney_graph-structured_2016}. Nevertheless, the standard way of processing both types of information jointly has never been clearly established, since visual and linguistic data have properties that make them fundamentally different, \textit{i.e.} the former has spatial meaning and no sequentiality while the latter does not contemplate space but has a sequential nature. Hence, each work in this sub-field has proposed a particular way of addressing each task.

On this chapter, we intend to describe three model architectures that mix both language referring expressions and vision inputs to produce a segmentation mask. While two of them \cite{hu2016segmentation} \cite{liu2017segmentation} try to solve the task by merging language and vision representations by defining a new, synthetic entity that describes the relation between both, the third one \cite{li2017cvpr} tr aims to map one domain to the other \textit{i.e.,} map language features to the visual filter space to accomplish object tracking on videos based on referring expressions.

\section{Segmentation from Natural Language Expressions}
On this work \cite{hu2016segmentation}, the authors process visual and natural language information through separate neural networks: a CNN extracts visual features from the image while an LSTM is applied to the natural language query. Strided convolutions and pooling operations in the CNN progressively downsample the feature maps to a low resolution output while producing a large receptive field size for neurons in the final layers.

Thus, the authors use a modified pre-trained VGG-16 \cite{DBLP:journals/corr/SimonyanZ14a} as a feature extractor whose last three fully-connected layers were replaced by three convolutional layers. Given an image of height \textit{H} and width \textit{W},  the output of such a fully convolutional network (FCN) is a spatial feature map of dimensions $h$ and $w$  with $D$ channels at each spatial location. The strided convolutions and pooling operations, while reducing the size of the image  to $h = \frac{H}{32}$ and $w = \frac{W}{32}$, also provide the units of the final response map with a large receptive field. At each spatial location, $\ell^2$-normalization is applied in search for a \textit{more robust feature representation}.

Additionally, to explicitly model localization in the network (\textit{i.e.} help the network reason about queries containing localization information), relative coordinates are concatenated at each spatial location in the feature map obtained by the CNN, thus obtaining a final visual feature map, with $D + 8$ channels.

On the other hand, language information is processed by tokenizing each word with a Word Embedding (WE) and passing each of these through a Long Short-Term Memory (LSTM) cell until the end of the sentence is reached. The last hidden state of the LSTM, which is expected to hold an overall representation of the expression's concept, is $\ell^2$-normalized and concatenated along the channel axis at \textit{each spatial location} of the feature representation that was obtained from the FCN module. If the hidden state of the LSTM is of length $L$, the concatenation of the linguistic and visual representation is of shape $(L + D + 8) \times h \times w$. Merging of visual and natural language information is done by concatenating the output of the LSTM to the visual feature map at each spatial location.

Finally, two $1\times1$ convolution layers with ReLU nonlinearities in between are applied for final classification. The ground truth mask is downsampled to have the same dimensions as the output, so that they can be directly compared. The loss function is defined as the average over the per-pixel weighed logistic regression loss, where weights for foreground and background counteracting the class imbalance are empirically determined to produce faster convergence.

This low resolution architecture is trained with standard backpropagation and Stochastic Gradient Descent (SGD) with momentum until convergence.  Training is thus done in two stages: a low resolution stage and a high resolution stage that trains a deconvolution layer to upsample the low resolution output with deconvolution to yield the final segmentation mask. The deconvolution layer is initialized with weights from bilinear interpolation.

\section{Recurrent Multimodal Interaction}
In \cite{liu2017segmentation}, the authors argue that segmenting the image based only on a final, memorized representation of the sentence does not fully take advantage of the sequential nature of language. This is done by not only including spatial coordinates as extra-channels for the image representation (as in the previous work by \cite{hu2016segmentation}) but also including the linguistic information at \textit{each} time step. This way, the sequential property of natural language is claimed to be exploited, and an analogy can be made so that image segmentation is understood as a sequential process.

The initial processing for the image itself is similar to that in \cite{hu2016segmentation}: the output of a FCN is $\ell^2$-normalized along the channel-axis and mesh grids representing spatial information are concatenated. Again, the FCN reduces the size of the input image and delivers a large receptive field for the units in the final layers ($h = \frac{H}{32}$ and $w = \frac{W}{32}$, in this case).

Consequently, they propose to perform segmentation multiple times in the pipeline. Such multimodal representation is obtained by concatenating the visual features at every spatial location of the visual representation with the hidden state of the LSTM. The segmentation mask is obtained by applying a multimodal LSTM (mLSTM) to the multimodal representation and then performing regular convolution to combine the channels that were produced by the mLSTM. The definition of the mLSTM is a convolutional LSTM that shares weights both across spatial location and time step, and is implemented as a $1\times1$ convolution that merges all these types of information.

The sequential property of natural language is thus exploited, and an analogy can be made so that image segmentation is understood as a sequential process. It should be mentioned that the hidden state of this mLSTM is not one-dimensional, like that of a conventional LSTM, but rather three dimensional, resembling an image.

At the final time step, when the last hidden state of the mLSTM is generated, a convolution is applied to it, yielding an output with only one channel. In the training process, although the network's output is a downsized version of its input, no upsampling is performed. The loss is defined exactly as in \cite{hu2016segmentation} and the network is trained with backpropagation, Adam optimizer with a fixed learning rate of $0.00025$, batch size of $1$ and weight decay to $0.0005$.

At test time, bilinear upsampling is performed to the net's output to produce a mask with the same dimensions of the ground-truth mask. Additionally, as a way of making the output less coarse, a Dense Conditional Random Field (DenseCRF) \cite{DBLP:journals/corr/abs-1210-5644} is applied for refinement.

\section{Tracking by Natural Language Specification}
In \cite{li2017cvpr}, the main task is not precisely segmentation based on a natural language, but object tracking in a video sequences. One of the usual approaches for specifying the target to be tracked in a video, is to mark it with a bounding box in the first frame. However, this approach has the issue that, for the duration of the video, the object may change much of its appearance and location, rendering the initial bounding box useless in some cases. Their main idea is about providing an alternative to this approach, by noting that \textit{(i)} the semantic meaning of the object being marked does not vary, for the duration of the video, as much as the appearance, and \textit{(ii)} this semantic meaning may be more enclosed by a linguistic expression.

This way of understanding the task brings the two problems close from the CV perspective. However, the approach taken in \cite{li2017cvpr} is substantially different, since visual and linguistic information is never merged \textit{per se} (as in the previous two methods), but rather the linguistic information is mapped to a space in which it can be thought of as having visual meaning.

The visual input is processed by a modified VGG-16 whose final three fully connected layers were replaced by convolutional ones, meaning that the network yields a feature map as in the previous methods. The linguistic input consisting of $T$ words is represented, word by word, with a WE, to which an LSTM is recursively applied. The main contribution of this approach is employing a single layer perceptron (SLP) with sigmoid activation function to the last hidden state of the LSTM ($h_T$), generating a \textit{dynamic convolutional visual filter}, $f$. Essentially, $f$ is a vector that is produced as a function of the last hidden state of the LSTM, and its dimensions are such that it can be thought of as filter for a 2D convolution that is to be performed on the feature map produced by the modified VGG. It is denominated as a \textit{dynamically} generated filter because it is specialized and fine-tuned for the semantics of the visual specification \cite{li2017cvpr}.

The main idea is that the filter produced as a function of an expression, will produce a strong response to the elements being referred to in such expression, and a weak response to those \textit{not} being referred to. This response is thought of as \textit{scores} for the referring expression, so that a segmentation can be output.

Once the filter is generated, convolution is applied to the feature map, and a response map is obtained. After this, a deconvolution layer of stride 32 is applied to produce a response map of the same size of the input image.

% For an example of a full page figure, see Fig.~\ref{fig:myFullPageFigure}.

%% Requires fltpage2 package
%%
% \begin{FPfigure}
% \includegraphics[width=\textwidth]{figures/fullpage}
% \caption[Short figure name.]{This is a full page figure using the FPfigure command. It takes up the whole page and the caption appears on the preceding page. Its useful for large figures. Harvard's rules about full page figures are tricky, but you don't have to worry about it because we took care of it for you. For example, the full figure is supposed to have a title in the same style as the caption but without the actual caption. The caption is supposed to appear alone on the preceding page with no other text. You do't have to worry about any of that. We have modified the fltpage package to make it work. This is a lengthy caption and it clearly would not fit on the same page as the figure. Note that you should only use the FPfigure command in instances where the figure really is too large. If the figure is small enough to fit by the caption than it does not produce the desired effect. Good luck with your thesis. I have to keep writing this to make the caption really long. LaTex is a lot of fun. You will enjoy working with it. Good luck on your post doctoral life! I am looking forward to mine. \label{fig:myFullPageFigure}}
% \end{FPfigure}
% \afterpage{\clearpage}


\documentclass{beamer}
\usefonttheme[onlymath]{serif}
\usepackage[english]{babel}							%For internationalization
\usepackage[utf8]{inputenc}							%For character encoding
\usepackage{amsmath}								%For mathematical typesetting
\usepackage{amssymb}								%For mathematical typesetting
\usepackage{graphicx}								%For handling graphics
\usepackage{listings}

\newcommand{\be}{\begin{equation}}
\newcommand{\bea}{\begin{equation*}}
\newcommand{\ben}[1]{\begin{equation}\label{#1}}
\newcommand{\ee}{\end{equation}}
\newcommand{\eea}{\end{equation*}}
\newcommand{\aq}{\overset{\sim}{q}}

\title
{An Introduction to Discontinuous Galerkin Methods}
\subtitle{Module 3A: To Higher-Orders - Solution Approximation}
\author[Bevan] % (optional, for multiple authors)
{J.~Bevan}
\institute[UMass Lowell]
{
  Department of Mechanical Engineering, Grad Student\\
  University of Massachusetts at Lowell
}
\date[Fall 2014]
{}
\subject{Discontinuous Galerkin}

\begin{document}
\frame{\titlepage}
\frame{\frametitle{Module 3A: To Higher-Orders - Solution Approximation}\tableofcontents}

%NEW SECTION
\section{Weak Form- Revisited}
\subsection{Approximation Space}
\frame{\frametitle{\textbf{\secname}: \subsecname}
\be \int_{\Omega} \frac{\partial q}{\partial t} \phi_j \,dx + \int_{\Omega} \frac{\partial f(q)}{\partial x} \phi_j \,dx = 0 \ee
\begin{itemize}
\item Let's revisit the weak form in greater detail, we glossed over some important details.
\item To get a flexible method, we'd like to be able to generate an arbitrary order solution approximation. How do we do this in code?
\item We glossed over the construction of the bases, we can think in terms of nodal or modal
\be \aq(x) = \sum_{i=0}^M a_i \psi_i(x) \ee
\end{itemize}
}

\subsection{$L^2$ Projection: Minimize norm}
\frame{\frametitle{\subsecname}
\begin{itemize}
\item Where does the weak form come from? Approximate sol'n doesn't satisfy the PDE exactly, left with some residual
\be \frac{\partial \aq}{\partial t} \,dx + \frac{\partial f(\aq)}{\partial x}\,dx = R(x) \ee
\item A projection finds the "closest" fit on a vector space compared to the original. An orthogonal projection does just this.
\item Minimizes the $L^2$ norm wrt the space projected upon
\end{itemize}
}

\subsection{The Galerkin Condition}
\frame{\frametitle{\subsecname}
\begin{itemize}
\item An orthogonal projection means that the difference between the original and the projection should be orthogonal to the space projected upon, so for each basis $\phi \in V_h$
\be \int (f-f_h) \, \phi \,dx =0 = \int R(x) \phi \,dx\ee
\item The projected space doesn't have to be the same space as the solution approximation, but if we do choose them to be the same we have the Bubnov-Galerkin approach
\item We could choose the two spaces to be different, this is a Petrov-Galerkin approach. As an example, a weight space of delta functions yields a collocation method
\end{itemize}
}

%NEW SECTION
\section{Arbitrary Order Basis: Monomials?}
\frame{\frametitle{\textbf{\secname}}
\begin{itemize}
\item Let's continue with the choice of polynomials for our basis functions.
\item The easy and obvious choice is the monomials $\psi_i(x) = a_i x^i$ so the arbitrary order solution approximation is
\be \aq(x) = \sum_{i=0}^M a_i \psi_i(x)= \sum_{i=0}^M a_i x^i  \ee
\item Can you think of why this a poor choice for a vector basis? Remember a basis needs to be complete and linearly independent. Let's see two separate ways why this is a poor choice.
\end{itemize}
}

\subsection{Linear Dependence}
\frame{\frametitle{\subsecname}
\begin{itemize}
\item Normalize the basis wrt to the $L^2$ norm $\vert g \vert = \sqrt{\int g^2}$ so $\bar{\psi}_i=\sqrt{2i+1}x^i$
\item Consider the inner product of two adjacent basis functions
\be \int \bar{\psi}_{i-1} \bar{\psi}_{i} \,dx = \sqrt{1-\frac{1}{4i^2}} \ee
\item Linearly independent vectors are orthogonal, that is their inner product is zero. But for our monomials, even moderate values of $i$ give an inner product of close to 1
\item Very nearly dependent basis vectors mean small perturbations can lead to trouble finding the correct solution
\end{itemize}
}

\subsection{The ill-conditioned Hilbert matrix}
\frame{\frametitle{\subsecname}
\begin{itemize}
\item The alternate way to consider this is to look at the mass matrix for a monomial basis
\item $1$
\item $x$
\item $x^2$
\item $x^3$
\item When we integrate from 0 to 1 it is easy to see the pattern of values, this is a Hilbert matrix
\item It is ill-conditioned, let's see why this is a problem in Matlab
\end{itemize}
}

%NEW SECTION
\section{[Recall] Lagrange Interpolation}
\frame{\frametitle{\textbf{\secname}}
\begin{itemize}
\item Instead of a monomial basis, what if we were to use a Lagrange basis (commonly used for interpolation)
\item Recall, the Lagrange basis is zero at all interp nodes except the basis node, which it is 1 at
\item It has the form
\be L_q(x) = \prod_{p=1,p\neq q}^{Np} \frac{x-x_p}{x_q-x_p} \ee
\item We can compactly represent this as
\textit{nn=elim(nd(1:N)',nd(1:N)',[1 3 2]);\\
Lag= @(x,nv) prod(bsxfun(@rdivide,bsxfun(@minus,x,nn(nv,:,:)),...\\
bsxfun(@minus,nd(nv),nn(nv,:,:))),3)}
\end{itemize}
}

%NEW SECTION
\section{Lagrange Spatial Approximation}
\frame{\frametitle{\textbf{\secname}}
\begin{itemize}
\item We can now represent our arbitrary order sol'n approximation
\be \aq(x) = \sum_{i=0}^M a_i \psi_i(x)= \sum_{i=0}^M q(x_i) L_i(x)  \ee
\item Where we must choose a collection of points to use as our interpolation nodes
\end{itemize}
}

%NEW SECTION
\section{Interpolation Points: Equispaced?}
\frame{\frametitle{\textbf{\secname}}
\begin{itemize}
\item It would seem logical that equispaced points are a good choice. They evenly cover the domain and are simple to calculate positions for.
\item Indeed these points work well for low $Np$, so one would expect the accuracy to increase as $Np$ increases
\item Let's see how it behaves at large $Np$
\end{itemize}
}

%NEW SECTION
\section{Runge's Phenomenon}
\frame{\frametitle{\textbf{\secname}}
\begin{itemize}
\item The divergence of the interpolation is a result of Runge's phenomenon
\item Why this fails is explainable in terms of two relations: the Bernstein and Markov Inequalities. For a polynomial $P^N \in [-1, 1]$
\be |P^{\prime}(x)| \leq \frac{N}{\sqrt{1-x^2}}\| P \| _{\infty} \ee
\item The singularity in the Bernstein inequality can be bounded by the Markov inequality
\be \| P^{\prime} \| {\infty} \leq N^2 \| P \|_ {\infty} \ee
\item With the overall being
\be |P^{\prime}(x)| \leq \min \left(\frac{N}{\sqrt{1-x^2}},N^2\right)\| P \|_ {\infty} \ee

\end{itemize}
}

%NEW SECTION
\section{Interpolation Points: Legendre Roots}
\frame{\frametitle{\textbf{\secname}}
\begin{itemize}
\item The overall inequality implies we'd like quadratic spacing near the element boundaries.
\item Though it may seem arbitrary, the roots of the Legendre polynomials have a favorable spacing that matches out needs
\item They avoid Runge's phenomenon, and have several other very nice features we shall see in the next module
\begin{figure}
\centering
\includegraphics[width=2in]{LegPolys.PNG}
\end{figure}
\centering
\tiny http://en.wikipedia.org/wiki/File:Legendrepolynomials6.svg
\end{itemize}
}

\end{document}


% 20201005
\documentclass[env.tex]{subfiles}

\newcommand{\aq}{aquatic}
\newcommand{\te}{terrestrial}
\newcommand{\mL}{mixed layer}
\newcommand{\hg}{homogeneous}
\newcommand{\thl}{theoretical}
\newcommand{\sP}{subarctic Pacific}
\newcommand{\sgs}{Sargasso Sea}
\newcommand{\nA}{N. Atlantic}
\newcommand{\ds}{density}
\newcommand{\pl}{population size}
\newcommand{\mC}{molar Concentration}
\newcommand{\ct}{continuous}
\newcommand{\y}{yes}
\newcommand{\n}{no}
\newcommand{\ft}{fluctuation}
\newcommand{\sd}{steady}

\begin{document}
\subsection{Features comparison between models}

\begin{table}[H]
    \centering
    \caption[Model features comparison]{Table of features comparison (16 features) between model in this project with aquatic slab models (22 models) and two terrestrial nutrient cycle models}
    \label{modComp}
\end{table}

\begin{landscape}
\begin{longtable}{p{2cm}|p{.05\linewidth}p{.07\linewidth}p{.1\linewidth}p{.1\linewidth}p{.13\linewidth}p{.06\linewidth}p{.14\linewidth}p{.1\linewidth}}\hline
    model & year & system & structure & condition & location & element & unit & time-frame \\\hline
    this study & 2020 & \aq & CPB & \mL & \thl & C & \ds & \ct \\
    \parencite{evans1985model} & 1985 & \aq & NPZ & \mL & \sP & n.a. & \pl & spring \\
    \parencite{frost1987grazing} & 1987 & \aq & NP(Z) & \mL & \sP & n.a. & \pl & \ct \\
    \parencite{fasham1990nitrogen} & 1990 & \aq & 2NPZDB(DOM) & \hg & \sgs & N & \mC & \ct \\
    \parencite{robinson1993simulated} & 1993 & \aq & P2Z & \mL & upwell Pacific & C & \ds & by-year \\
    \parencite{fasham1995use} & 1995 & \aq & 2NPZDB(DOM) & - & \sP & - & - \\
    \parencite{matear1995parameter} & 1995 & \aq & 2NP2ZDB(DOM) & \mL & \sP & n.a. & \pl & \ct \\
    \parencite{hurtt1996pelagic} & 1996 & \aq & 2NPR & \mL & \sgs & N & \mC & \ct \\
    \parencite{xiao1996relative} & 1996 & \te & CNP & dual cycles & \thl & C, N & \ds & \ct \\
    \parencite{popova1997chaotic} & 1997 & \aq & NPZD & \mL & \thl & N & \mC & \ct \\
    \parencite{anderson1998modelling} & 1998 & \aq & 2NPZDB(DOM) & slab & Engligh Channel & N & \mC & \ct \\
    \parencite{spitz1998data} & 1998 & \aq & 2NPZDB(DOM) & \mL & \sgs & N & \mC & \ct \\
    \parencite{fennel2001testing} & 2001 & \aq & NPZD & \mL & \sgs & N & \mC & \ct \\
    \parencite{natvik2001weak} & 2001 & \aq & NPZ & - & Flemish Cap & - & - & - \\
    \parencite{schartau2001parameter} & 2001 & \aq & NPZ & \mL & \sgs & N & \mC & \ct \\
    \parencite{spitz2001configuring} & 2001 & \aq & 2NPZDB(DOM) & \mL & \sgs & N & \mC & \ct \\
    \parencite{ito2002simulation} & 2002 & \te & CP & \hg & \thl & C & \ds & no \\
    \parencite{hemmings2004split} & 2004 & \aq & NPZ & - & \nA & - & - & - \\
    \parencite{onitsuka2005differences} & 2005 & \aq & NPZD, 2N2P3Z(DOM) & slab & Japan sea & N & \mC & \ct \\
    \parencite{findlay2006modelling} & 2006 & \aq & NP & \mL & \thl & hypo & \mC & \ct \\
    \parencite{mitra2007accounting} & 2007 & \aq & 2NPZDB(DOM) & \hg & \nA & N & \mC & short \\
    \parencite{mitra2009closure} & 2009 & \aq & 2NPZDB(DOM) & \hg & \nA & N & \mC & \ct \\
    \parencite{llebot2010role} & 2010 & \aq & 2N2PD(DOM) & \hg & Mediterranean Bay & N & \mC & \ct \\
    \parencite{kidston2013phytoplankton} & 2013 & \aq & NPZD & \mL & Southern Ocean & N & \mC & \ct \\
    \parencite{anderson2015empower} & 2015 & \aq & NPZD & \mL & \thl & N & \mC & \ct \\
\hline\end{longtable}

\begin{longtable}{p{2cm}|p{.07\linewidth}p{.1\linewidth}p{.1\linewidth}p{.1\linewidth}p{.1\linewidth}p{.1\linewidth}p{.1\linewidth}p{.1\linewidth}}\hline
    model & thermocline & density-depend motality & trophic levels (main level + decomposer) & explicit players & sunlight & system setting & respiration & resource allocation \\\hline
    this study & \n & \phy & 1+1 & \y & \n & \ft & \y & \y \\
    \parencite{evans1985model} & \y & \n & 1+0 & \y & \y & \sd & \n & \n \\
    \parencite{frost1987grazing} & \y & \n & 2+0 & \n & \y & \sd & \n & \n \\
    \parencite{fasham1990nitrogen} & \y & \n & 2+1 & \y & \n & \sd & \n & \n \\
    \parencite{robinson1993simulated} & \y & unknown & 3+0 & no equations & \y & \ft & \n & \n \\
    \parencite{fasham1995use} & - & - & - & model 03 & - & - & \n & \n \\
    \parencite{matear1995parameter} & \y & \n & 2+0 & \y & \y & \ft & \n & \n \\
    \parencite{hurtt1996pelagic} & \y & \n & 1+0 & model 03 & \n & \sd & \n & \n \\
    \parencite{xiao1996relative} & \n & \n & 1+1 & \y & \y & \sd & \y & \y \\
    \parencite{popova1997chaotic} & \y & \n & 2+0 & \y & \y & \ft & \n & \n \\
    \parencite{anderson1998modelling} & \y & \n & 2+1 & \n & \y & \ft & \n & \n \\
    \parencite{spitz1998data} & \y & \n & 2+1 & \y & \y & \ft & \n & \n \\
    \parencite{fennel2001testing} & \y & \n & 2+0 & \y & \y & \ft & \n & \n \\
    \parencite{natvik2001weak} & - & - & - & no equations & - & - & \n & \n \\
    \parencite{schartau2001parameter} & \y & \n & 2+0 & \n & \y & \ft & \n & \n \\
    \parencite{spitz2001configuring} & \y & \n & 2+1 & model 11 & \n & \ft & \n & \n \\
    \parencite{ito2002simulation} & \n & \n & 1+0 & \y & \n & \sd & \n & \n \\
    \parencite{hemmings2004split} & - & - & - & statistical & - & - & \n & \n \\
    \parencite{onitsuka2005differences} & \y & \n & 2+0 & \y & \n & \ft & \n & \n \\
    \parencite{findlay2006modelling} & \y & \n & 1+0 & \n & \n & \ft & \n & \n \\
    \parencite{mitra2007accounting} & \n & \n & 2+1 & \n & \n & perturbation & \n & \n \\
    \parencite{mitra2009closure} & \n & \n & 2+1 & \y & \n & cannibalism & \n & \n \\
    \parencite{llebot2010role} & \n & \n & 2+0 & \y & \y & \sd & \n & \n \\
    \parencite{kidston2013phytoplankton} & \y & \phy & 2+0 & \y & \y & \sd & \n & \n \\
    \parencite{anderson2015empower} & \y & \phy & 2+1 & \y & \y & \ft & \n & \n \\
\hline\end{longtable}
\end{landscape}

\end{document}


\section{Basic Analysis}
\label{891_2:sec:anal}

To quantify uncertainties in our full spectrum galaxy fits we will
test different methods of fitting spectra and interpreting the
results. In this section we describe the basic, high-level methodology
that is common to all fitting methods. We also detail preparatory
analysis that is required before these methods can be applied to our
data, namely determining the velocity of each aperture and correcting
for Balmer emission.

%+++++++++++++++++++++++++++
%% {\bf MAB: Itemize (not in bullets, in sentences, clauses) the detailed
%%   preparatory analysis you will be describing. ``running the data
%%   through full spectrum fitting'' should be re-worded.

%------------------------
%% You should also have some preliminary discussion about why you are
%% laying the basics on the ``SSP fitting methodology.'' (I would call it
%% SPS not SSP, i.e., your SPS involves adding together SSPs.) For
%% example, there are the issues of degeneracies between age, metallicity
%% and extinction. There is also the issue of assumptions about the SFH
%% on $\tau_L$ and $Z_L$ for which the data cannot offer resolution,
%% i.e., the data does not allow you to know about the detailed SFH for
%% the older stellar populations. In other words, what is going to lead
%% you ultimately to the DFK approach. Foreshadow this.}

\subsection{SSP fitting methodology}
\label{891_2:sec:SSP_method}

Our fitting method involves minimizing differences between measured
NGC 891 spectra and synthetic galaxy spectra constructed from the
superposition of simple stellar populations (SSPs, see
\S\ref{891_2:sec:SSP_sets}). Fits are performed with
MPFITFUN\footnote{available at \url{http://purl.com/net/mpfit}}
\citep{Markwardt09}, an IDL implementation of the MINPACK Levenberg
Marquardt minimization algorithm \citep{More78}. The model galaxy
spectra are defined as
\begin{equation}
\label{891_2:eq:SSP_galaxy}
g(\lambda) = R(\lambda,\tau_V) \sum_{i,j} M(\Delta t_i,Z_j)
f(\lambda,\Delta t_i, Z_j),
\end{equation}
where $f(\lambda, \Delta t_i, Z_j)$ is the spectrum of an SSP with
metallicity $Z_j$ formed over time interval $\Delta t_i$, $M(\Delta
t_i,Z_j)$ quantifies the strength of the contribution of each SSP, and
$R(\lambda, \tau_V)$ governs the total extinction in the galaxy. All of our
SSPs are normalized to \val{1}{\sol{M}} so the strength of each SSP is
simply
\begin{equation}
\label{891_2:eq:fit_weight}
M(\Delta t_i,Z_j) = \int_{\Delta t_i} \psi(t, Z_j) dt,
\end{equation}
where $\psi(t, Z_j)$ describes the mass of stars formed at time $t$
with metallicity $Z_j$ (i.e., the star formation rate). In other
words, $M(\Delta t_i,Z_j)$ is the total mass in stars formed during
the time interval, $\Delta t_i$, associated with a particular SSP. The
extinction, $R(\lambda, \tau_V)$, is parametrized by a single
variable, $\tau_V$ and, importantly, does not depend on SSP age
($t_i$) or metallicity ($Z_j$). In \S\ref{891_2:sec:extinction} we
discuss the details of $R(\lambda, \tau_V)$ and the limitations of
this simplification. During fitting the free parameters are the SSP
weights ($M(\Delta t_i,Z_j)$) a single extinction term (\tauV), and the
systematic velocity.

%+++++++++++++++++++++++++++++
%% {\bf MAB: You should reference Levenberg Marquardt but I do not think
%%   you need to describe your coding language or the implementation in
%%   MPFITFUN. Concerning equation (1), you need to mention here what is
%%   being assumed given the placement of R. You can discuss it more
%%   later (in 3.3), but you should at least note the implications here
%%   and point to that later discussion here.}

For each aperture our primary values of interest are the age,
metallicity, and extinction. We measure extinction directly as $A_V =
1.086\ \tauV$, but age and metallicity depend on assumptions made
during fitting (i.e., priors in the SSP library) and during subsequent
analysis.

As shown in Equation \ref{891_2:eq:fit_weight} the fit weights describe the
integral of a particular star formation history (SFH), but it is
useful to reduce a SFH down to single values for age and
metallicity. To do this we define a pair of light-weighted
quantities. The mean, light-weighted age is
\begin{equation}
\label{891_2:eq:MLWA}
\tau_L = \frac{\sum_{i,j} W_{L,i,j}\times t_i}{\sum_{i,j} W_{L,i,j}}
\end{equation}
where $t_i$ is the age associated with each SSP and $W_L$ is the
``light weight'', defined as
\begin{equation}
\label{891_2:eq:light_weight}
W_{L,i,j} = M(\Delta t_i,Z_j)\sum_k S(\lambda_k) R(\lambda_k, \tau_V)
f(\lambda_k, \Delta t_i, Z_j),
\end{equation}
where $S(\lambda_k)$ defines a bandpass over which the light weight is
computed. We set $S(\lambda_k)$ to be flat over \val{5450}{\AA}
$\leq\lambda\leq$ \val{5550}{\AA} and zero everywhere else. The choice
of $t_i$ depends on assumptions about the SFH and in this work we make
the simple assumption that star formation is constant across the
lifetime of each SSP ($\Delta t_i$) so that $t_i$ is simply the
midpoint of each $\Delta t_i$. In \S\ref{891_2:sec:sys_err} we discuss
and quantify the systematics that arise from such an assumption. We
note that Equation \ref{891_2:eq:light_weight} also describes a
conversion from mass-weights (i.e., $M(\Delta t_i, Z_j)$) to
light-weights that is simply a constant that is independent of the
specific mix of SSPs (but does depend on the overall extinction).

Similarly to $\tau_L$ we also define the mean, light-weighted
metallicity as
\begin{equation}
\label{891_2:eq:MLWZ}
Z_L = \frac{\sum_{i,j} W_{L,i,j}\times Z_j}{\sum_{i,j} W_{L,i,j}}.
\end{equation}

%+++++++++++++++++++++++
%% {\bf MAB: In eqn (4) move M outside of the sum since it does not
%%   depend on wavelength to clearly indicate that the factor between the
%%   light weight and the mass (weight) is a contant factor that depends
%%   on age and metallicity for a given band-pass. And say as much.}

\subsection{Velocities}
\label{891_2:sec:vel}

The systemic velocities of our model galaxies do not depend on the
shape of the spectra (i.e., the SSP mix) and thus we can reduce the
number of free parameters in our SSP fits by measuring the velocities
once and then keeping them fixed for all subsequent analysis. The
velocity values we use are a combination of velocity estimates from
two sources: the ``fit'' velocities that result from SSP fitting, and
velocities measured from the centers of bright emission lines.

%++++++++++++++++++++++
%% {\bf MAB: Do you mean ``systematic velocities'' or do you mean
%%   ``systemic velocities'' or ``line of sight velocities'' ? At the end
%%   of the paragraph enumerate in words what the following two
%%   subsubsections are.}

\subsubsection{Fit velocities}
During full spectrum fitting the velocity can be a free parameter of
the model, and this fit provides the first estimate of the velocity of
each aperture. The first step is to run a fully unconstrained fit
where all SSP weights, the velocity, and the extinction are allowed to
vary (see \S\ref{891_2:sec:SSP_method}). This produces a ``best fit''
only in the sense that the resulting model spectrum matches the data
well in a $\chi^2$ sense; to determine velocities the details of the
astrophysical assumptions behind each model are unimportant. The
reported fit uncertainties on these velocities is on the order of
\val{\asim10}{\kms}, depending on \GP fiber size (see Chapter
\ref{chap:891_1}), which is slightly better that 10\% of the
instrumental resolution (again, depending on fiber size).

%++++++++++++++++++
%% {\bf MAB: Discuss the centroiding accuracy in terms of the
%%   instrumental resolution.}

Once the ``best fit'' spectrum is determined we run a second iteration
of the fitting routine with the SSP weights and extinction fixed to
the best fit values. This keeps the shape of the model spectrum the
same and causes $\chi^2$ minimization to be driven only by the
velocity offset between the model and the data. When fitting these
velocities we used only the range of wavelengths that lie within the
region spanned by arc lamp lines ($\val{4100}{\AA} \leq\lambda\leq
\val{6600}{\AA}$). This avoids wavelengths that could be affected by
extrapolation of the wavelength solution (see Chapter
\ref{chap:891_1}). In the majority of apertures this ``refined''
velocity was within \val{\asim 10}{\kms} of the original fit velocity,
which is similar to the precision of the fits and indicates that this
second iteration is perhaps not necessary.

%+++++++++++++++=
%% {\bf MAB: How important is this iteration? Again, compare amplitude of
%%   the change in the iteration to the instrumental resolution.
%% Refer to Paper-I concerning the wavelengths spanned by arcs (at the end of
%% the last sentence of the par.
%% }

The results of these fits produce what we call the ``fit
velocities''. The final fit values were found to be stable to a wide
(\val{\asim 100}{\kms}) range of starting velocity values. These
velocity fits are driven mainly by regions of the spectrum that
deviate significantly from the continuum level, which in our case are
the Balmer absorption series, the MgI absorption band, and the G
band. Many apertures show moderate to strong \Ha emission, but we
masked this feature (and [OIII]) during fits because our model SSPs do
not have nebular emission. For this reason the $\chi^2$ velocities can
be though of as the ``stellar'' velocity values.

%% {\bf MAB: Do you mask the upper Balmer series, including H-beta? I
%%   would have thought this would be important, and I thought you did.}

%% Comment for MAB: we actually didn't mask \HB. In retrospect maybe we
%% should have, but it is a rare aperture indeed that has strong enough
%% \HB emission to really mess with the residuals. Besides, the other
%% strong absorption features are certainly enough to simply fit a
%% centroid.

\begin{figure*}[t]
  \centering
  \includegraphics[width=\textwidth]{891_2/figs/Swat_vel.pdf}
  \caption[Comparison of measure stellar and gas velocities to HI
    velocity data]{\fixspacing\label{891_2:fig:Swat_vel}Comparison of
    HI velocity envelope (from \citet{Swaters97}) to \emph{left:}
    measured H$\alpha$ centroids, \emph{middle:} best fit velocities
    from SSP models, and \emph{right:} best fit stellar velocities
    with a constant offset to minimize median difference between gas
    and stars.}
\end{figure*}

\subsubsection{Emission line velocities}

As a check on the velocities found above we also measure the velocity
of nebular emission lines, namely \Ha. Centroids were computed using
the IRAF task FITPROFS which allows us to deblend the \Ha, [NII]
complex. We focus only on the \Ha velocities, which are then compared
to known HI velocities from \citet{Swaters97}, as shown in the left
panel of Figure \ref{891_2:fig:Swat_vel}. We call the velocities measured in
\Ha ``gas velocities''.

%+++++++++++++++=
%% {\bf MAB: While I am ok with us comparing to Swaters+97 in Figure 1,
%%   are you taking into account in all of this analysis the trend in the
%%   HI tangential speed with height found in Oosterloo+07? This is
%%   essential I think. Last sentence are you measuring \Ha velocities or
%%   \Ha+[NII] velocities?}

The center panel of Figure \ref{891_2:fig:Swat_vel} shows the
``stellar velocities'' compared to known HI velocities and our
measured \Ha velocities. Clearly there is a systematic offset between
the stellar and gas velocities and we find that this offset is
constant in magnitude and sign across both sides of the galaxy. This
fact rules out an offset caused by asymmetric drift
\citep{Binney87}. Furthermore, many of the stellar velocities are
unphysical in the sense that they lie outside of the HI envelope. We
therefore conclude that this offset is caused by systematic
measurement uncertainty (likely arising from uncertainty in our
wavelength solution) and compute a constant velocity ``correction''
that is applied to the stellar velocities. This offset is computed to
minimize the median difference between stellar and gas
velocities. While we do expect the stellar tangential speed to lag
behind the gas the fact that we measure both sides of the galaxy
means, the median difference between gas and stars should be, to first
order, zero.

%+++++++++++++++++++++
%% {\bf MAB: I made a few edits in the above par. As part of or before
%%   the sentence beginning ``Clearly, ...'', add a description of what
%%   you see in Figure 1 that is clearly unphysical, i.e., the negative
%%   velocities in the middle panel below the gas. More broadly, discuss
%%   first the distribubtion of the ionized gas velocity w.r.t. the
%%   envelop from HI. This envelop should be noted in the text and the
%%   figure caption. Explain why we might expect the ionized gas to lie
%%   below the HI envelop, i.e., we are measuring a centroid. What should
%%   the reader make of the few points in the upper part of the left
%%   panel that are above the HI envelope? Are these bad measuremets? Add
%%   error bars?

%%   Define AD -- you can reference Binney \& Tremaine.

%%   Discuss what the soures of possible uncertainty could be in the
%%   stellar velocities. Is this a problem with the wavelength solution
%%   -- something that you come to in the next paragraph, or something
%%   else? Did you check that the wavelength solution and the BC03
%%   templates were either both in vacuum or air?

%---------------------
%%   Lastly, concerning the use of the median, yes, maybe to to zeroth
%%   order the median difference should be zero, but not to a \kms
%%   level. The median difference is going to depend on at least the
%%   number of points on the positive and negative sides of the galazy to
%%   zeroth order. To first order it will depend on the radial
%%   distribution particularly near the center where the projected
%%   velocity is small. Can you address this?}

\begin{figure}[t]
  \centering
  \includegraphics[width=\columnwidth]{891_2/figs/SG_diff.pdf}
  \caption[Offset between stellar and gas
    velocities]{\fixspacing\label{891_2:fig:SG_offset}Difference
    between stellar and gas velocities after applying a constant
    \val{74}{\kms} offset to the stars. A median of 1 corresponds to
    no average offset between the two population \emph{across both
      sides of the galaxy}.}
\end{figure}

Figure \ref{891_2:fig:SG_offset} shows the relation between stellar
and gas velocities after applying an offset of \val{74}{\kms}. This
offset minimizes the global difference between gas and stellar
velocities across both sides of the galaxy. Based on these results,
the final velocity of each aperture is fixed as the velocity measured
during $\chi^2$ fitting (``stellar velocity'') plus a constant offset
of \val{74}{\kms}. The right hand panel of Figure
\ref{891_2:fig:Swat_vel} shows the final velocities used. We note that
the systematic correction of \val{74}{\kms} is within the total
uncertainty in our velocity as dictated by uncertainties in the
wavelength solution (\val{120}{\kms}, see Chapter \ref{chap:891_1}).

%+++++++++++++++++++=
%% {\bf MAB: The standard is km s$^{-1}$ not \kms. At the end here you
%%   want to discuss the difference between the stars and gas in the left
%%   and right panels compared to each other and the HI envelop. For
%%   example, the asymmetry in the stars is not seen in the gas.}

\subsection{Extinction Model}
\label{891_2:sec:extinction}

During all steps of full spectral fitting
(\S\ref{891_2:sec:SSP_method}) we assume the extinction model of
\citet{Charlot00}, which is described by
\begin{equation}
R(\lambda) = e^{-\tauV \left(\lambda/\val{5500}{\AA}\right)^{-0.7}}.
\end{equation}
This model has a steeper slope than a simple foreground dust screen,
but the authors show it accurately reproduces the ratio of
far-infrared to ultraviolet luminosities, the ratio of \Ha to \HB
luminosities, the \Ha equivalent width, and the ultraviolet spectral
slope of nearby starburst galaxies. We retain the functional
dependence of $\tau \propto \lambda^{-0.7}$ and define two
normalizations, one for gas emission and one for stellar
continuum. Thus, the total optical depth to a particular species is
either
\begin{equation}
\tau_{{\rm cont}}(\lambda) = \tauV \left(\frac{\lambda}{\val{5500}{\AA}}\right)^{-0.7}
\end{equation}
or
\begin{equation}
\label{891_2:eq:tauVB}
\tau_{{\rm em}}(\lambda) = \tauVB \left(\frac{\lambda}{\val{5500}{\AA}}\right)^{-0.7},
\end{equation}
where \tauV and \tauVB are the normalization factors for stellar continuum and
gas emission, respectively.

As described in \S\ref{891_2:sec:SSP_method}, \tauV is a free
parameter of the SSP models that essentially measures the slope of the
continuum. To measure \tauVB (which we need to compute emission
corrections a la \S\ref{891_2:sec:emission_corr}) we use the ratio of
measured \Ha/\HB flux and compare it to the expected value of 2.86
from \citet{Osterbrock89}, assuming case B recombination in a
\val{10^4}{K} gas. Under these assumptions, and given the extinction
law in equation \ref{891_2:eq:tauVB}, we compute
\begin{equation}
\label{891_2:eq:Balmer_extinction}
\tauVB = 4.84 \ln \left(\frac{\Ha/\HB}{2.86}\right).
\end{equation}

The \Ha and \HB line fluxes were measured using the IRAF task FITPROFS
which allows us to deblend the \Ha/[NII] emission feature. Before
fitting the emission lines a ``best fit'' galaxy model is
subtracted. As in \S\ref{891_2:sec:vel} the detailed astrophysics of the
best fit model are unimportant; we only require that the model
accurately match the stellar continuum so that it can be subtracted
from the data. Once the continuum is subtracted the data are smoothed
with a 3 pixel (\val{\asim 350}{\kms}) wide moving average. To
mitigate the impact of bad continuum fits the centroid fitting window
is constrained to be only $\sim 4\sigma$ wide, where $\sigma$ is the
average spectral width (standard deviation) of bright sky emission
lines.

%++++++++++++++++++==
%% {\bf MAB: In the first sentence above do you mean SSP or SPS? I think
%%   the latter. Concerning the 600 \kms wide moving average, is this
%%   equivalent to a boxcar of xx pixels? If so, you might say ``the data
%%   are smoothed with a 600 km s$^{-1}$ (XX pixel) wide moving average,
%%   or boxcar.'' Again, modify \kms to km s$^{-1}$. However, I am not
%%   sure I understand the need for this smoothing and how it reduces the
%%   impact of bad continuum fits. Why? It seems to me you will make the
%%   NII+Ha blending worse which also sounds like it is a problem.}

The relative strength of the two [NII] lines in the \Ha/[NII] complex
is governed by quantum mechanics and we expect the flux ratio
[NII]$_{6585}$/[NII]$_{6549} = 3$. When measuring the \Ha/[NII] lines,
however, we often find [NII]$_{6585}$/[NII]$_{6549} > 3$, which
indicates that the line fit is attributing some of the [NII]$_{6549}$
flux to \Ha and causing the \Ha flux to be overestimated.  This is
likely due to an incorrect de-blending of the \Ha and [NII]$_{6549}$
lines.  We correct for this effect by using the flux of [NII]$_{6585}$
as a ``calibration'' flux (reasonable given its isolation, away from
blending issues of \Ha and [NII]$_{6549}$) and define an \Ha
correction,
\begin{equation}
  \label{891_2:eq:balmer_corr}
  \Delta\Ha = \mathrm{[NII]}_{6549} - \frac{\mathrm{[NII]}_{6585}}{3},
\end{equation}
which is added to the measured \Ha flux.

As mentioned above, measuring \Ha and \HB emission requires first correcting
for \Ha and \HB absorption present in the stellar continuum. In practice the
entire process is iterative because fitting SSPs accurately requires accurate
emission corrections (\S\ref{891_2:sec:emission_corr}), which, in turn, requires
accurate extinction values. We find that the measured Balmer fluxes
and derived emission corrections are stable after only a single
iteration.

%+++++++++++++++
%% {\bf MAB: I would like to understand the issue with the imperfect
%%   continuum subtraction (in the sentence ``This is likely due...'')
%%   and how it alters the NII ratio. In this paragraph, leading up to
%%   the motivation for eqn 10 you want to say that the concept here is
%%   to trust the 6585 NII line because of its greater wavelength
%%   separation from Ha. However, you also need to address wy you think
%%   the continuum subtraction is better for 6585, i.e., why for some
%%   reason the baseline for this line isn't what is causing the problems
%%   with the ratio not equalling 3.

%-----------------
%%   Further, you need to provide the
%%   amplitude of the correction which I expect will vary significatly
%%   with Ha line-strength and height (NII/Ha changes). So I'd like to
%%   know the \% correction to the Ha flux in the mean (and/or median),
%%   and the trends with height, e.g., numbers for 0-0.4 kpc, 0.4-1 kpc,
%%   $>$1 kpc.}

\subsection{Emission Corrections}
\label{891_2:sec:emission_corr}

Many of our apertures show moderate to strong Balmer emission that is
not present in the SSP libraries underlying our galaxy models and
therefore must be subtracted before accurate analyses can be made. To
do this we construct a model spectrum containing only emission from
\Ha, \HB, \Hg, \Hd, and \He.

The basic idea is to first construct a model galaxy produced from fits
where the cores of the Balmer lines were masked. These fits will be
good first guesses of the stellar continuum in the absence Balmer
emission. We then use these fits to remove the stellar continuum and
isolate this emission. Then the two strongest emission lines (\Ha and
\HB) can be used to construct a model containing emission from the
higher-order lines.  Based on the results of \citet{Osterbrock89}, and
assuming case B recombination in a \val{10^4}{K} gas, the ratios of
fluxes for the Balmer series is \mbox{1 : 0.350 : 0.164 : 0.087 :
  0.056} (\Ha : \HB : \Hg : \Hd : \He), thus if we know the flux in a
single Balmer line we can construct a model containing all of these
Balmer lines. In our emission models the width of each line is
dictated by the instrumental resolution measured at the line location
(see Chapter \ref{chap:891_1}), with apertures made from different
sized \GP fibers having different widths.

%++++++++++++++++++++=
%% {\bf MAB: First sentence: I think you mean SPS models (that use
%%   SSPs). I'd like you to explain the notion behind the approach in an
%%   explicit way. The notion is an important idea. The kernel of it is
%%   that Hydrogen Balmer Case B emission strength decreases sigificantly
%%   with higher order, e.g., by more than a factor of 10 between
%%   H$\alpha$ and H$\delta$. In contrast, the equivalent width of the
%%   Hydrogen Balmer absorption in stellar photospheres does not display
%%   this trend, and indeed H$\alpha$ absorption EW is weaker than
%%   H$\beta$. This contrast can be used to our advantage in an iterative
%%   scheme that first masks the cores of the Balmer absorption lines
%%   where the emission is present to provide a first-order estimate the
%%   continuuum. Once subtracted, only the two strongest emission lines
%%   are used to estimate the normalization of the emission and the
%%   attenuation, from which the remaining line-emission is estimated.}

\begin{figure}
  \centering
  \includegraphics[width=\columnwidth]{891_2/figs/TauV_balm.pdf}
  \caption[Height dependence of
    \tauVB]{\fixspacing\label{891_2:fig:tauVB_height}Measured values
    of \tauVB as a function of distance from the midplane. The error
    bars show the uncertainties reported by IRAF from 100 Monte Carlo
    noise iterations. The black shows a linear fit described in
    Equation \ref{891_2:eq:tauVbalm}.}
\end{figure}

This idealized Balmer emission model is then extincted using Equation
\ref{891_2:eq:Balmer_extinction} based on the value of \tauVB as measured in
section \ref{891_2:sec:extinction}. To minimize the impact of aberrant
extinction calculations we compute a height-dependent \tauVB by
fitting a line to the data from all apertures (and thus from all
radii), as shown in Figure \ref{891_2:fig:tauVB_height}. From these data it
is clear that the radial dependence to the vertical trend in \tauVB is
minimal so our grouping of all radii is valid. We find for NGC 891
\begin{equation}
\label{891_2:eq:tauVbalm}
  \tauVB (z) = -0.91 z + 4.15,
\end{equation}
which allows us to compute the extinction in our Balmer emission
models at any height above the midplane.

%----------------------
%% {\bf MAB: Concerning Figure 3: Yuck. I can't tell if the line is a
%%   good fit to the data and whether there is a radial dependence. (You
%%   should say ``a radial dependence to the vertical trend.'') Clearly
%%   we see a radial trend in the stellar absorption later so this result
%%   is somewhat puzzling without some thought and
%%   interpretation. Regarding the figure itself, I'd like to see a
%%   version w/o the error bars. The error bars must be wrong because
%%   they are far larger than the scatter. I would also like to see
%%   moving averages for all pointings as well as for individual
%%   pointings.}

\begin{figure}
  \centering
  \includegraphics[width=\columnwidth]{891_2/figs/emission_comp_zoom.pdf}
  \caption[Spectra before and after Balmer emission
    correction]{\fixspacing\label{891_2:fig:emission_comp}An example
    of Balmer emission correction. The red and blue spectra show an
    aperture with strong Balmer emission (aperture P4.6) before and
    after the Balmer emission model described in
    \S\ref{891_2:sec:emission_corr} is applied.}
\end{figure}

Finally, the emission model is scaled so the \Ha flux matches the \Ha
flux measured in each individual aperture. The data spectra are then
corrected by subtracting the corresponding emission model. Figure
\ref{891_2:fig:emission_comp} shows an example of a spectrum with strong \HB
emission before and after subtracting the Balmer emission model.

In many apertures the blending of the \Ha/[NII] complex causes
imperfect subtraction in this region (as it should, our emission model
does not contain [NII] emission), and in subsequent analysis we do not
fit (i.e., mask) data within \val{\pm 500}{\kms} of \Ha (which
includes both [NII] lines). In addition to \Ha/[NII] we mask other
strong sources of nebular emission ([OIII]$_{4959}$, [OIII]$_{5007}$,
and S2) and sky lines with strong residuals ([OI]$_{6300}$, NaD, and
OI$_{5577}$).

%++++++++++++++++===
%% {\bf MAB: Itemize the lines that you mask -- all of them -- and give
%%   equivalent pixels to 500 \kms at the wavelength extrama. For Figure
%%   4 I can't see anything in this figure so I recommend you show
%%   blow-ups in wave for the Balmer lines from $\alpha$ to $\epsilon$.}

As mentioned above, the best-fit, extinction-value, emission-model
analysis steps are interconnected and at least one iteration is
required to find the ``final'' values.


% BEGIN LICENSE BLOCK
% Version: CMPL 1.1
%
% The contents of this file are subject to the Cisco-style Mozilla Public
% License Version 1.1 (the "License"); you may not use this file except
% in compliance with the License.  You may obtain a copy of the License
% at www.eclipse-clp.org/license.
%
% Software distributed under the License is distributed on an "AS IS"
% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See
% the License for the specific language governing rights and limitations
% under the License.
%
% The Original Code is  The ECLiPSe Constraint Logic Programming System.
% The Initial Developer of the Original Code is  Cisco Systems, Inc.
% Portions created by the Initial Developer are
% Copyright (C) 2006 Cisco Systems, Inc.  All Rights Reserved.
%
% Contributor(s):
%
% END LICENSE BLOCK

\chapter{The Eplex Library}
\label{chapeplex}
%HEVEA\cutdef[1]{section}

\section{Introduction}

The eplex library allows an external Mathematical Programming solver to be
used by {\eclipse}. It is designed to allow the external solver to be seen
as another solver for {\eclipse}, possibly in co-operation with
the existing `native' solvers of {\eclipse} such as the {\it ic\/} solver. It is
not specific to a given external solver, with the differences between
different solvers (largely) hidden from the user, so that the user can
write the same code and it will run on the different solvers.

The exact types of problems that can be solved (and methods to solve them)
are solver dependent, but currently linear programming, mixed integer
programming and quadratic programming problems can be solved.

The rest of this chapter is organised as follows: the remainder of this
introduction gives a very brief description of Mathematical Programming, which
can be skipped if the reader is familiar with the concepts.
Section~\ref{mpmodelling} demonstrates the modelling of an MP problem, and the
following section discusses some of the more advanced features of the library
that are useful for hybrid techniques.

\subsection{What is Mathematical Programming?}
\label{whatmp}

\index{Mathematical Programming (MP)}
\index{Linear Programming (LP)}
\index{Mixed Integer Programming (MIP)}
\index{optimisation (numerical)}
\index{objective function}

Mathematical Programming (MP) (also known as numerical optimisation) is the study of optimisation using
mathematical/numerical techniques. A problem is modelled
by a set of simultaneous equations: an
objective function that is to be minimised or maximised, subject to a set of
constraints on the problem variables, expressed as equalities and
inequalities.

Many subclasses of MP problems have found important practical
applications. In particular, Linear Programming (LP)
problems and Mixed Integer Programming (MIP) problems are perhaps the most
important. LP problems have both a linear objective function and linear
constraints.  MIP problems are LP problems where some or all of the
variables are constrained to take on only integer values.

It is beyond the scope of this chapter to cover MP in
any more detail. However, for most usages of the eplex library, the user
need not know the details of MP -- it can be treated as a black-box
solver.

\See{For more information on Mathematical Programming, you can read a
textbook on the subject such as H.~P.~Williams'  {\it Model Building in
Mathematical Programming} \cite{Williams99}.}

\quickref{Classification of MP problems}{

\begin{itemize}
\item Linear Programming (LP) problems: linear constraints and objective
function, continuous variables.
\item Mixed Integer Programming (MIP) problems: LP problems with some or
all variables restricted to taking integral values.
\end{itemize}

}

\subsection{Why interface to Mathematical Programming solvers?}
\label{whymp}

Much research effort has been devoted to developing efficient ways of
solving the various subclasses of MP problems for over 50 years. The
external solvers are state-of-the-art implementations of some of these
techniques. The eplex library allows the user to model an MP problem in
ECLiPSe, and then solve the problem using the best available MP tools.

In addition, the eplex library allows for the user to write programs that
combines MP's global algorithmic solving techniques with the local
propagation techniques of Constraint Logic
Programming.

\subsection{Example formulation of an MP Problem}
\begin{figure}
\begin{center}
\resizebox{0.5\textwidth}{!}{\includegraphics{tpprob.ps}}
\end{center}
\caption{An Example MP Problem}
\label{tpprob}
\end{figure}

Figure~\ref{tpprob} shows an example of an MP problem.
It is a transportation problem where
several plants (1-3) have varying product producing capacities that must
be transported to various clients (A-D), each requiring various amounts of
the product. The per-unit cost of transporting the product to
the clients also varies. The problem is to minimise the transportation
cost whilst satisfying the demands of the clients.

To formulate the problem, we define the amount of product transported from
a plant $N$ to a client $p$ as the variable $Np$, e.g.\ $A1$
represents the cost of transporting to plant $A$ from client $1$. There are
two kinds of constraints:

\begin{itemize}
\item The amount of product delivered from all the plants to a client must be equal to the
client's demand, e.g.\ for client A, which can recieve products from plants
1-3: \( A1 + A2 + A3 = 21 \)

\item The amount of product sent by a plant must not be more than its
capacity, e.g. for plant 1, which can send products to plants A-D:
\(A1 + B1 + C1 + D1 \leq 50 \)

\end{itemize}

The objective is to minimise the transportation cost, thus the objective
function is to minimise the combined costs of transporting the product to
all 4 clients from the 3 plants.

Putting everything
together, we have the following formulation of the problem:

Objective function:
{\small
\[
\min (10A1 + 7A2 + 200A3 + 8B1 + 5B2 + 10B3 + 5C1 + 5C2 + 8C3 + 9D1 + 3D2 + 7D3)
\]
}

Constraints:

\begin{eqnarray*}
A1 + A2 + A3 & = & 21\\
B1 + B2 + B3 & = & 40\\
C1 + C2 + C3 & = & 34\\
D1 + D2 + D3 & = & 10\\
A1 + B1 + C1 + D1 & \leq & 50\\
A2 + B2 + C2 + D2 & \leq & 30\\
A3 + B3 + C3 + D3 & \leq & 40\\
\end{eqnarray*}

\section{How to load the library}

To use the library, you must have an MP solver that eplex can use. This
can be an open-source solver such as the COIN-OR project's CLP/CBC solvers,
which is distributed with ECLiPSe, or a commercial solver such as
XPRESS-MP or CPLEX. Your {\eclipse} should be configured to load in a
`default' solver if there is more than one available.

\See{See the library
manual's Eplex chapter for details for how to install the solver.}

When configured properly, the library can be loaded with the directive:

\begin{code}
:- lib(eplex).
\end{code}

\noindent
This will load the library with the default external MP solver.

You probably need a valid license in order to use an external commercial solver.

\section{Modelling MP problems in {\eclipse}}
\label{mpmodelling}

\subsection{Eplex instance}

The simplest way to model an eplex problem is through an {\it eplex
instance}. Abstractly, it can be viewed as a solver module that is
dedicated to one MP problem. MP constraints can be posted to the instance
and the problem solved with respect to an objective function by the
external solver.

Declaratively, an eplex instance can be
seen as a compound constraint consisting of all the variables and
constraints of its eplex problem. Like normal constraints, different eplex
instances can share variables, although the individual MP constraints in
an eplex instance do not necessarily have to be consistent with those in
another.

\quickref{Eplex Instance}{

An {\bf eplex instance} represents a single MP problem in a
module. Constraints for the problem are posted to the module. The
problem is solved with respect to an objective function.
}

\subsection{Example modelling of an MP problem in {\eclipse}}

\index{modelling, LP problem}
The following code models (and solves) the transportation problem of
Figure~\ref{tpprob}, using an eplex instance:

{\small
\begin{code}
:- lib(eplex).

:- eplex_instance(prob).  % a. declare an eplex instance

main1(Cost, Vars) :-
        % b. create the problem variables and set their range
        Vars = [A1,A2,A3,B1,B2,B3,C1,C2,C3,D1,D2,D3],
        prob: (Vars \$:: 0.0..1.0Inf),

        % c. post the constraints for the problem to the eplex instance
        prob: (A1 + A2 + A3 \$= 21),
        prob: (B1 + B2 + B3 \$= 40),
        prob: (C1 + C2 + C3 \$= 34),
        prob: (D1 + D2 + D3 \$= 10),

        prob: (A1 + B1 + C1 + D1 \$=< 50),
        prob: (A2 + B2 + C2 + D2 \$=< 30),
        prob: (A3 + B3 + C3 + D3 \$=< 40),

        % d. set up the external solver with the objective function
        prob: eplex_solver_setup(min(
                10*A1 + 7*A2 + 200*A3 +
                 8*B1 + 5*B2 + 10*B3 +
                 5*C1 + 5*C2 +  8*C3 +
                 9*D1 + 3*D2 +  7*D3)),

        %------------------------------- End of Modelling code

        prob: eplex_solve(Cost).  % e. Solve problem using external solver

\end{code}
}

To use an eplex instance, it must first be declared with \verb'eplex_instance/1'.
This is usually done with a directive, as in line \verb'a'.
Once
declared, an eplex instance can be referred to using its name like a module
qualifier.

We first create the problem
variables and set their range to be non-negative, as is conventional in MP.
Note that the bounds are posted to our eplex instance, using \verb'$::/2'.
\Note{The default bounds for variables is -1.0Inf..1.0Inf. Bounds posted to
  an eplex instance are specific to that eplex instance.}

Next, we set up the MP constraints for the
problem by posting them to the eplex instance.
The MP constraints accepted by eplex are the arithmetic equalities and
inequalities:
\verb'$=/2', \verb'$=</2' and \verb'$>=/2'.
\Note{The arithmetic constraints can be linear expressions on both
sides. The restriction to linear
expressions originates from the external solver.}
\index{\$=/2!eplex}
\index{\$>=/2!eplex}
\index{\$=</2!eplex}
\index{eplex\_solver\_setup/1}
\index{eplex\_instance/1}
\index{eplex\_solve/1}

We need to setup the external solver with the eplex instance, so
that the problem can be solved by the external solver. This is done by
\verb'eplex_solver_setup/1', with the objective
function given as the argument, enclosed by either \verb'min(...)' or \verb'max(...)'. In this case, we are minimising.
Note that
generally the setup of the solver and the posting of the MP constraints can be
done in any order.

Having set up the problem, we can solve it
by calling \verb'eplex_solve/1' in line \verb'e'.

When an instance gets solved, the external solver takes into account all
constraints posted to that instance, the current variable bounds for the
problem variables, and the objective specified during setup.

In this case, there is an optimal solution of 710.0:

\begin{quote}\begin{verbatim}
?-  main1(Cost, Vars).

Cost = 710.0
Vars = [A1{0.0 .. 1e+20 @ 0.0}, A2{0.0 .. 1e+20 @ 21.0}, ....]
\end{verbatim}\end{quote}
Note that the problem variables are not
instantiated by the solver. However, the `solution' values, i.e.\ the
values that the variable are given by the solver, are available in the
eplex attribute. The eplex attribute is shown as \verb'Lo..Hi @ Sol' where
\verb'Lo' is the lower bound, \verb'Hi' the upper bound, and \verb'Sol' the
solution value for the variable (e.g., \verb'A2' has the solution value of
21.0 in the example above). Note also that the external solver may not
allow very large floats, hence \verb'1e+20', this external solver's
representation of infinity, is the upper bound of the
variables, even though we specified \verb'1.0Inf' in our code.

One reason the problem variables are not assigned their solution values is
so that the eplex problem can be solved again, after it has been modified.
A problem can be modified by the addition of more constraints, and/or
changes in the bounds of the problem variables.

\subsection{Getting more solution information from the solver}

The solution values of the problem variables can be obtained by
\verb'eplex_var_get/3'. The example program in the
previous section can be modified to return the solution values:

{\small
\begin{code}
main2(Cost, Vars) :-
        ....  % same as previous example up to line e
        prob: eplex_solve(Cost),  % e. Solve problem using external solver
        (foreach(V, Vars) do
            % f. set the problem variables to their solution values
            prob: eplex_var_get(V, typed_solution, V)
        ).
\end{code}}
\index{eplex_var_get/3}

In line \verb'f', \verb'eplex_var_get/3' is used to obtain the solution
value for a problem variable. The second argument, set to \verb'typed_solution',
 specifies that we want the solution value for the variable to be returned.
Here, we instantiate the problem variable itself to the solution value
with the third argument:

\begin{quote}\begin{verbatim}
?- main2(Cost, Vars).

Cost = 710.0
Vars = [0.0, 21.0, 0.0, 16.0, 9.0, 15.0, 34.0, 0.0, 0.0, 0.0, 0.0, 10.0]
\end{verbatim}\end{quote}

Note that, in general, an MP problem can have many optimal solutions, i.e.\
different solutions which give the optimal value for the objective function.
As a result, the above
instantiations for \verb'Vars' might not be what is returned by the solver
used.

\subsection{Adding integrality constraints}
\index{modelling, MIP}

In general, a problem variable is not restricted to taking integer
values. However, for some problems, there may be a requirement that some or
all of the variable values be strictly integral (for example, in the previous
transportation problem, it may be that only whole units of the
products can be transported; also variables may often be used to model
booleans by allowing them to take on the values of 0 or 1 only).
This can be specified  by
posting an additional \verb'integers/1' constraint on the
variables.

\index{integers/1!eplex}
Consider the example problem again, where it so happens that the
optimal value for the objective function can be satisfied with integral
values for the variables. To show the differences
that imposing integer constraints might make, we add the constraint that
client A must receive an equal amount of products from plants 1 and 2. Now
the problem (without the integer constraints) can be written as:

{\small
\begin{code}
:- lib(eplex).

:- eplex_instance(prob).

main3(Cost, Vars) :-
        Vars = [A1,A2,A3,B1,B2,B3,C1,C2,C3,D1,D2,D3],
        prob: (Vars \$:: 0.0..1.0Inf),
        prob: (A1 + A2 + A3 \$= 21),
        prob: (B1 + B2 + B3 \$= 40),
        prob: (C1 + C2 + C3 \$= 34),
        prob: (D1 + D2 + D3 \$= 10),

        prob: (A1 + B1 + C1 + D1 \$=< 50),
        prob: (A2 + B2 + C2 + D2 \$=< 30),
        prob: (A3 + B3 + C3 + D3 \$=< 40),

        prob: eplex_solver_setup(min(
                10*A1 + 7*A2 + 200*A3 +
                 8*B1 + 5*B2 + 10*B3 +
                 5*C1 + 5*C2 +  8*C3 +
                 9*D1 + 3*D2 +  7*D3)),

        prob: (A1 \$= A2), % g. the new constraint, added after setup

        %------------------------------- End of Modelling code

        prob: eplex_solve(Cost),
        (foreach(V, Vars) do
            prob: eplex_var_get(V, typed_solution, V)
        ).
\end{code}}

In this example, the new constraint in line \verb'g' is imposed after the
solver setup. In fact it can be imposed anytime before
\verb'eplex_solve(Cost)' is called.

This problem also has an optimal \verb'Cost' of 710, the same as the original
problem. However, the solution values are not integral:

\begin{quote}\begin{verbatim}
?- main3(Cost, Vars).

Cost = 710.0
Vars = [10.5, 10.5, 0.0, 5.5, 19.5, 15.0, 34.0, 0.0, 0.0, 0.0, 0.0, 10.0]
\end{verbatim}\end{quote}

Now, to impose the constraints that only whole units of the products can be
transported, we modify the program as follows:

{\small
\begin{code}
main4(Cost, Vars) :-
        Vars = [A1,A2,A3,B1,B2,B3,C1,C2,C3,D1,D2,D3],
        prob: (Vars \$:: 0.0..1.0Inf),
        prob: integers(Vars),  % h. impose the integrality constraint
        ....% Rest is the same as main3
\end{code}}

In line \verb'h', we added the \verb'integers/1' constraint. This imposes
the integrality constraint on \verb'Vars' for the eplex instance
\verb'prob'. Now,
the external solver will only assign integer solution values to the
variables in the list. \Note{In fact, with the integer
constraints, the problem is solved
as a MIP problem rather than an LP problem, which involves different
(and generally computationally more expensive) techniques. This difference
is hidden from the eplex user.}

Running this program, we get:

\begin{quote}\begin{verbatim}

?- main4(Cost,Vars).

Cost = 898.0
Vars = [10, 10, 1, 6, 20, 14, 34, 0, 0, 0, 0, 10]

\end{verbatim}\end{quote}

In this case, \verb'A1' and \verb'A2' are now integers. In fact, notice
that all the values returned are now integers rather than floats. This is
because the  \verb'typed_solution' option of \verb'eplex_var_get/3'
returns the solution values taking into account if the variables have been
declared as integers for the eplex instance.
\Note{Posting an integers/1 constraint to an eplex instance only
  inform the external solver to treat those variables as integers (in fact
  the external solver will still represent the variables as floats, but
  will only assign intergral solution values to them), but does
  not constrain the variable itself to be of type integer.}

\quickref{Modelling an MP Problem}{
\begin{itemize}
\item Declare an eplex instance using {\bf eplex_instance(+Instance)}.
\item Post the constraints ({\bf \$=/2, \$>=/2, \$=</2, integers/1,
  \$::/2}) for the problem to the eplex instance.
\item Setup the solver with the objective function using
\newline{\bf Instance: eplex_solver_setup(+ObjFunc)}.
\end{itemize}

}

\section{Repeated Solving of an Eplex Problem}

Part of the power of using the eplex library comes from being able to solve
an eplex problem repeatedly after modification.
For example, we can solve the original transportation
problem, add the extra constraint, and resolve the problem.
Remember that as \verb'eplex_solve/1' instantiates its argument, we
need to use a new variable for each call:

{\small
\begin{code}
        .... % setup the constraints for the original problem as before
        prob: (A3 + B3 + C3 + D3 =< 40),

        prob: eplex_solver_setup(min(....)), % as before

        prob: eplex_solve(Cost1),     % h. solve original problem
        prob: (A1 \$= A2),
        prob: eplex_solve(Cost2),     % i. solve modified problem
        .....
\end{code}}

Note that posted constraints behave logically: they are added to an eplex
instance when posted, and removed when they are backtracked over.

In the examples so far, the solver has been invoked explicitly.
However, the solver can also behave like a normal constraint, i.e.\ it is
automatically invoked when certain conditions are met.

As an example, we implement the standard branch-and-bound method of
solving a MIP problem, using the external solver as an LP solver only.
Firstly we outline how this can be
implemented with the facilities we have already encountered. We then show
how this can be improved usin more advanced features of \texttt{lib(eplex)}.

With the branch-and-bound approach, a search-tree is formed, and at each node a
`relaxed' version of the MIP problem is solved as an LP problem.
Starting at the root, the problem solved is the original MIP problem, but
without any of the integrality constraints:
{\small
\begin{code}
:- eplex_instance(mip).

main5(Cost, Vars) :-
      % set up variables and constraints, but no integers/1 constraints
      ....
      % assume minimise for simplicity
      mip: eplex_solver_setup(min(Obj)),
      mip: eplex_solve(RelaxedCost),
      mip: (Cost \$>= RelaxedCost),  % RelaxedCost is lower bound
\end{code}}
\begin{figure}
\begin{center}
\resizebox{0.35\textwidth}{!}{\includegraphics{mipnode.ps}}
\end{center}
\caption{Labelling a variable at a MIP tree node}
\label{mipnode}
\end{figure}
In general, this initial LP solution contains non-integer assignments
to integer variables. The objective value of this LP is a lower bound on
the actual MIP objective value.
The task of the search is to find integer assignments
for the integer variables that optimises the objective function.
Each node of the
search-tree solves the problem with extra bound constraints on these
variables. At each node, a particular variable is `labelled' as shown in
Figure~\ref{mipnode}. The integer variable in this case has been assigned
the non-integer value of 4.2. In the subsequent nodes of the tree, we
consider two  alternate problems, which creates two branches in the search. In one
problem, we impose the bound constraint $X \leq 4$, and in the other, $X
\geq 5$: these are the two nearest integer values to 4.2. In each branch,
the problem is  solved
again as an LP problem with its new bound for the variable:

{\small
\begin{code}
branching(IntVars) :-
      ....
      % for each integer variable X which violates the integer constraint
      mip: eplex_var_get(X, solution, XVal),
      ...
      Split is floor(XVal),
      % choice: branch on the two ranges for X
      (mip: (X \$=< Split) ; mip: (X \$>= Split + 1)),
      mip: eplex_solve(RelaxedCost),
      ...% repeat until there are no integer violations
\end{code}}

A choice-point for the two alternative branchings is created in the above
code, the problem is solved with one of the branchings (\verb'X $=< Split').
The program then proceeds to further labelling of the variables. The
alternative branch is left to be tried on backtracking.

Eventually, if the problem has a solution, all the integer
variables will be `labelled' with integer values, resulting in a solution to the
MIP problem. However, this will generally not be optimal, and so the program
needs to backtrack into the tree to search for a better solution by trying
the other branches for the variables, using the
existing solution value as a bound.
This `branch-and-bound' search technique is implemented in {\tt
lib(branch_and_bound)}.

\quickref{Reminder: use {\eclipse} libraries!}{Remember that {\eclipse} provides
libraries that make some programming tasks much easier. There is no
need to write your own code when you can use what is
provided by an {\eclipse} library.}

\begin{sloppypar}
In the code, the external solver is invoked explicitly at every node. This
however may not be necessary as the imposed bound may already be satisfied.
As stated at the start of this section, the invocation of the solver could be done in a
data-driven way, more like a normal constraint.
This is done with \verb'eplex_solver_setup/4':
\verb'eplex_solver_setup(+Obj,-ObjVal,+Options,+Trigs)', a more
powerful version of \verb'eplex_solver_setup/1' for setting up a
solver. The \verb'Trigs' argument specifies a list of `trigger
modes' for triggering the solver. \See{See the {\eclipse} reference manual for a
complete  description of the predicate.}
\end{sloppypar}

\index{eplex\_solver\_setup/4}
For our example, we add a bound constraint at each node to exclude a
fractional solution value for a variable. The criterion we want to use is
to invoke the solver only if this old solution value is excluded by the new
bounds (otherwise the external solver will solve the same problem
redundantly). This is done by
specifying \verb'deviating_bounds' in the trigger modes.
The full code that
implements a MIP solution for the
example transportation problem is given below:
{\small
\begin{code}
:- lib(eplex).
:- lib(branch_and_bound).

:- eplex_instance(mip).

main6(Cost, Vars) :-
        % b. create the problem variables and set their range
        Vars = [A1,A2,A3,B1,B2,B3,C1,C2,C3,D1,D2,D3],
        mip: (Vars :: 0.0..1.0Inf),

        % c. post the constraints for the problem to the eplex instance
        mip: (A1 + A2 + A3 \$= 21),
        mip: (B1 + B2 + B3 \$= 40),
        mip: (C1 + C2 + C3 \$= 34),
        mip: (D1 + D2 + D3 \$= 10),

        mip: (A1 + B1 + C1 + D1 \$=< 50),
        mip: (A2 + B2 + C2 + D2 \$=< 30),
        mip: (A3 + B3 + C3 + D3 \$=< 40),
        mip: (A1 \$= A2),

        % j. post the objective function as a constraint
        ObjFunc = 10*A1 + 7*A2 + 200*A3 +
                   8*B1 + 5*B2 + 10*B3 +
                   5*C1 + 5*C2 +  8*C3 +
                   9*D1 + 3*D2 +  7*D3,
        mip: (ObjFunc  \$= Cost),

        % k. this is a more flexible method for setting up a solver.
        %    [deviating_bounds] specifies that the external solver should be
        %    invoked when any solution value is outside the variable bounds
        mip: eplex_solver_setup(min(ObjFunc), Cost, [], [deviating_bounds]),

        % l. Use the branch_and_bound library to do the branch and bound
        bb_min(( branching(Vars),
                 mip: eplex_get(cost, Cost)
                 (foreach(V, Vars) do mip: eplex_var_get(V,solution,V))
               ), Cost, _).

branching(IntVars) :-
        % Find a variable X which does not have an integer solution value
        (integer_violation(IntVars, X, XVal) ->
            % m. try the closer integer range first
            Split is round(XVal),
            (Split > XVal ->
                (mip: (X \$>= Split) ;  mip: (X \$=< Split - 1))
            ;
                (mip: (X \$=< Split) ; mip:  (X \$>= Split + 1))
            ),
            branching(IntVars)
        ;
            % cannot find any integer violations; found a solution
            true
        ).

% returns Var with solution value Val which violates the integer constraint
integer_violation([X|Xs], Var, Val) :-
        mip: eplex_var_get(X, solution, RelaxedSol),
        % m. we are dealing with floats here, so need some `margin' for a
        %    float value to be considered integer (1e-5 on either side)
        (abs( RelaxedSol - round(RelaxedSol) ) >= 1e-5 ->
            Var = X, Val = RelaxedSol
        ;
            integer_violation(Xs, Var, Val)
        ).

\end{code}}

The setup of the solver is done in line \verb'k', with the use of the
\verb'deviating_bounds' trigger mode. There are no explicit calls to trigger the
solver -- it is triggered automatically. In addition, the first call to
\verb'eplex_solve/1' for an initial solution
is also not required, because when trigger modes are specified, then
by default, \verb'eplex_solver_setup/4' will invoke the solver once the
problem is setup.

\begin{sloppypar}
Besides the \verb'deviating_bounds'
trigger condition, the other argument of interest in our use of
\verb'eplex_solver_setup/4' is the second argument,
the objective value of the problem (\verb'Cost' in the example):
recall that this was returned previously by \verb'eplex_solve/1'.
Unlike in \verb'eplex_solve/1', the variable is {\it not\/}
instantiated when the solver returns. Instead, one of the bounds (lower
bound in the case of minimise) is updated
to the optimal value, reflecting the range the objective value can take,
from suboptimal to the `best' value at optimal. The variable is therefore
made a problem variable by posting of the objective as a constraint in line
\verb'j'. This informs the external solver needs to be informed
that the \verb'Cost' variable is the objective value.

\end{sloppypar}

In line \verb'm', the branch choice is created by the posting of the bound
constraint, which may trigger the external solver. Here, we use a
simple heuristic to decide which of the two branches to try first: the
branch with the integer range closer to the relaxed solution value.
For example, in the situation of Figure~\ref{mipnode}, the branch with
\verb'X $=< 4' is tried first since the solution value of 4.2 is
closer to 4 than 5.

By using {\it lib(branch_and_bound)\/}'s \verb'bb_min/3' predicate in \verb'm',
there is no need to explicitly write our own branch-and-bound routine. However,
this predicate requires the cost variable to be instantiated, so we call
\verb'eplex_get(cost, Cost)' to instantiate \verb'Cost' at the end of
each labelling of the variables. We also get the solution values for the
variables, so that the branch-and-bound routine will remember it.
The final value returned in \verb'Cost' (and {\tt Vars} for the solution
values)
is the optimal value after the branch-and-bound search, i.e.\ the
optimal value for the MIP problem.

\quickref{More advanced modelling in eplex}{
\begin{itemize}
\item Use {\bf
Instance:eplex_solver_setup(+Obj,-ObjVal,+Opts,+Trigs)} to
set up an external solver state for instance Instance. Trigs specifies a
list of trigger conditions to automatically trigger the external solver.
\item {\bf Instance:eplex_var_get(+Var,+What,-Value)} can be used to obtain
information for the variable {\bf Var} in the eplex instance.
\item {\bf Instance:eplex_get(+Item, -Value)} can be used to  retrieve
information about the eplex instance's solver state.
\end{itemize}
}
\index{eplex\_get/2}

Of course, in practice, we do not write our own MIP solver, but
use the MIP solver provided with the external solvers instead. These
solvers are highly optimised and tightly coupled to their own LP solvers.
The techniques of solving relaxed subproblems described here are however
very useful for combining the external solver with other solvers in a
hybrid fashion. \See{See chapter~\ref{chaphybrid} for more details on hybrid techniques.}

\section{Exercise}

A company produces two types of products T1 and T2, which requires the
following resources to produce each unit of the product:

\vspace{3mm}
\begin{center}
\begin{tabular}{|r||r|r|}\hline
Resource & T1 & T2\\ \hline
Labour (hours) & 9 & 6\\ \hline
Pumps (units) & 1 & 1\\ \hline
Tubing (m) & 12 & 16\\ \hline
\end{tabular}
\end{center}
\vspace{3mm}

The amount of profit per unit of products are:

\begin{description}
\item[T1] \pounds350
\item[T2] \pounds300
\end{description}

They have the following resources available: 1566 hours of labour, 200
pumps, and 2880 metres of tubing.

\begin{enumerate}
\item Write a program to maximise the profit for the company, using eplex
as a black box solver.   Write a predicate that returns the profit and the
    values for T1 and T2.

\item What program change is required to answer this question:
    What profit can be achieved if exactly 150 units of T1 are required?

\item What would the profit be if fractional numbers of refrigerators could
    be produced?

\item Rewrite the program from (1) without optimize/2, using
    eplex_solver_setup/1, eplex_solve/1, and eplex_var_get/3.

\item In the program from (4), remove the integrality constraints (so that eplex
    only sees an LP problem).  Solve the integer problem by interleaving
    solving of the LP problem with a rounding heuristic:

\begin{itemize}
    \item solve the continuous relaxation
    \item round the solution for T1 to the nearest integer and instantiate it
Initially just return the maximum profit value.
    \item re-solve the new continuous relaxation
    \item round the solution for T2 to the nearest integer and instantiate it
    \item re-solve the new continuous relaxation
\end{itemize}

    What is the result in terms of T1, T2 and Profit?

\item Rewrite the program from (5) using eplex_solver_setup/4 and automatic
    triggering of the solver instead of explicit calls to eplex_solve/1.
    The solver should be triggered whenever variables get instantiated.
\end{enumerate}

%HEVEA\cutend