\documentclass[main]{subfiles}
\begin{document}
\section{Poisson Regression}
\label{sec:poisson_regression}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\subsection{}

Transform the poisson distribution function
\begin{align*}
	p(y;\lambda)
		&=
			e^{-\lambda}
			\frac {(\lambda)^y} {y!} 			\\
		&=
			\frac {1} {y!}
			\exp (
				y \log \lambda - \lambda
			)
\end{align*}

Compare it to the general equation that defines the exponential family
\begin{center}
\begin{math}
	p(y;\lambda) =
		b(y) \exp ( \eta \, y - a(\eta) )
\end{math}
\end{center}

We get
\begin{align*}
	a(\eta) 	&=
			\lambda = e^{\eta} 					\\
	b(y)		&=
			\frac {1} {y!}						\\
	\eta 		&=
			\log \lambda						\\
	T(y)		&=
			y
\end{align*}

\subsection{}

The mean value of a random variable that abides by the Poisson distribution is
$\lambda$. Thus the \textbf{canonical response function} is
\begin{center}
\begin{math}
	g(\eta) = E[y;\lambda] = \lambda = e^{\eta}
\end{math}
\end{center}

\subsection{}
skipped

\subsection{}
Given $T(y) = y$, the general equation of exponential distributions is
simplified to
\begin{align*}
	p(y|\eta) =
		b(y) \exp ( \eta \, y - a(\eta) )
\end{align*}

With the course assumption $\eta = \v{\theta}\cdot\v{x}$, the log-likelihood
function can be written as
\begin{align*}
	l(\theta)
		&=
			\log p(y^{(i)}|x^{(i)};\theta) 		\\
		&=
			\log b(y) +
			(\v{\theta}\cdot\v{x}^{(i)}) y - a(\v{\theta}\cdot\v{x}^{(i)})
\end{align*}

Given a data point $(x^{(i)}, y^{(i)})$, the gradient of the log-likelihood with
respect to $\theta$ is
\begin{align*}
	\pd{l(\theta)}{\theta_j}
		=
			x^{(i)}_j (
				y - a^{\p}(\eta)
			)
\end{align*}

By observation, it concludes this proof if
\begin{align*}
	a^{\p}(\eta) = g(\eta)
\end{align*}

It can be proved. First let's define a function of $\eta$
\begin{align*}
	I(\eta)
		&=
			\int
			p(y;\eta)
			dy 									\\
		&=
			\int
			b(y) \exp ( \eta \, y - a(\eta) )
			dy
\end{align*}

$I(\eta)$ is just the sum of the probabilities of all possible states, which
must be one, i.e.
\begin{center}
\begin{math}
	I(\eta) = 1
\end{math}
\end{center}

Take the derivative with respect to $\eta$
\begin{align*}
	I^{\p}(\eta)
		&=
			\int
			\pd {} {\eta}
			b(y) \exp ( \eta \, y - a(\eta) )
			dy 									\\
		&=
			\int
			y \, b(y) \exp ( \eta \, y - a(\eta) )
			dy 									\\
		&-
			a^{\p}(\eta)
			\int
			b(y) \exp ( \eta \, y - a(\eta) )
			dy 									\\
		&=
			E[y]
			- a^{\p}(\eta) I(\eta)				\\
		&=
			g(\eta)
			- a^{\p}(\eta) 						\\
		&=
			0
\end{align*}

The last step used the fact that the derivative of a constant number is zero.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{document}


\documentclass[english]{../thermomemo/thermomemo}
% NOTE: You must pass either norsk or english as an option!
\usepackage[utf8]{inputenc}

\title{Introduction to thermodynamics}
\author{Morten Hammer, Geir Skaugen and Eskil Aursand}

\usepackage[normalem]{ulem}

\usepackage{hyperref}
\usepackage{color}
\usepackage{amsfonts}

\definecolor{midnightblue}{RGB}{35,35,132}
\definecolor{urlblue}{RGB}{70,130,180}

\definecolor{shadecolor}{gray}{0.9}

\hypersetup{
    colorlinks=true,
    linkcolor=midnightblue,
    urlcolor=urlblue,
    citecolor=midnightblue,
    linktoc=page
}

\usepackage{amsmath}
\usepackage{hyperref}
\usepackage{framed}
\usepackage{siunitx,mhchem,todonotes}
\newcommand{\pone}[3]{\frac{\partial #1}{\partial #2}\bigg|_{#3}}% partial
                                % derivative with information of
                                % constant variables
\newcommand*{\vektor}[1]{\boldsymbol{#1}}%
\newcommand{\dd}[1]{\mathrm{d}{#1}}

\DeclareMathOperator*{\argmin}{arg\,min }

\usepackage[activate={true,nocompatibility},final,kerning=true,tracking=true,spacing=true,stretch=10,shrink=10]{microtype}
\microtypecontext{spacing=nonfrench}
\SetExtraKerning[unit=space]
    {encoding={*}, family={bch}, series={*}, size={footnotesize,small,normalsize}}
    {\textendash={400,400}, % en-dash, add more space around it
     "28={ ,150}, % left bracket, add space from right
     "29={150, }, % right bracket, add space from left
     \textquotedblleft={ ,150}, % left quotation mark, space from right
     \textquotedblright={150, }} % right quotation mark, space from left
\SetTracking{encoding={*}, shape=sc}{0}

\begin{document}
\frontmatter

\tableofcontents

\section{Introduction}
This document is ment as an introduction to thermodynamics for users
and programmers in thermopack.

In addition to this memo, there exsist two memos written to document
thermopack. These can be generated running pdflatex in the thermopack
documentation folder ./doc/memo/.

Otherwise the thermodynamics book by Callen \cite{callen85} and by
Michelsen \cite{michelsen07} is recommended. Much of the work done on
flash algorithms are based on the work by Michelsen
\cite{michelsen82a,michelsen82b,michelsen99}.

\section{Thermodynamic potentials}
\subsection{Internal energy}
As a starting point, one assumes a fundamental equation for the internal energy ($U$), with the extensive variables entropy ($S$), volume ($V$)
and molar numbers ($\vektor{n})$ as independent variables:
\begin{equation}
  U = U(S,V,\vektor{n})
  \label{}
\end{equation}
Under the assumption that $U$ is a \textit{first order homogeneous function} of the extensive variables, one may use
\textit{Euler's theroem of homogeneous functions} to show that:
\begin{align}
  U(S,V,\vektor{n}) &= S \pone{U}{S}{V,\vektor{n}}
  +V \pone{U}{V}{S,\vektor{n}}
  +\sum_i n_i \pone{U}{n_i}{S,V,n_j} \nonumber \\
  &= ST - VP + \sum_i n_i \mu_i
  \label{eq:U_euler}
\end{align}
using the definitions
\begin{equation}
  T \equiv \pone{U}{S}{V,\vektor{n}}, \quad P \equiv \pone{U}{V}{S,\vektor{n}}, \quad \mu_i \equiv \pone{U}{n_i}{S,V,n_j}.
  \label{}
\end{equation}
The differential of $U$ given its homogeneous property \eqref{eq:U_euler} is
\begin{equation}
  \dd{U} = S\dd{T} + T\dd{S} - V\dd{P} - P\dd{V} + \sum_i n_i \dd{\mu_i} + \sum_i \mu_i \dd{n_i},
  \label{}
\end{equation}
while generally the total (exact) differential of \eqref{eq:U_euler}, given its independent variables, is
\begin{equation}
  \dd{U} = T\dd{S} - P\dd{V} + \sum_i \mu_i \dd{n_i},
  \label{eq:U_totaldiff}
\end{equation}
which combine into the \textit{Gibbs-Duhem} relation for the intensive properties:
\begin{equation}
  S\dd{T} - V\dd{P} + \sum_i n_i \dd{\mu_i} = 0.
  \label{eq:gibbsduhem}
\end{equation}

\subsection{Helmholtz energy}
The Helmholtz energy is defined as
\begin{equation}
  A(T,V,\vektor{n}) \equiv U - TS,
  \label{eq:helmholtz_def}
\end{equation}
and by using \eqref{eq:U_totaldiff}, one may show that
\begin{align}
  \dd{A} = -S\dd{T} - P\dd{V} + \sum_i \mu_i \dd{n_i},
  \label{}
\end{align}
and one may then see that
\begin{equation}
  S = -\pone{A}{T}{V,\vektor{n}}, \quad P = -\pone{A}{V}{T,\vektor{n}}, \quad \mu_i = \pone{A}{n_i}{T,V,n_j}.
  \label{eq:A_differentials}
\end{equation}

\subsection{Gibbs energy}
The Gibbs energy is defined as
\begin{equation}
  G(T,P,\vektor{n}) \equiv U + PV - TS,
  \label{eq:gibbs_def}
\end{equation}
and by using \eqref{eq:U_totaldiff}, one may show that
\begin{align}
  \dd{G} = -S\dd{T} + V\dd{P} + \sum_i \mu_i \dd{n_i}.
  \label{eq:dG}
\end{align}
and one may then see that
\begin{equation}
  S = -\pone{G}{T}{P,\vektor{n}}, \quad V = \pone{G}{P}{T,\vektor{n}}, \quad \mu_i = \pone{G}{n_i}{T,P,n_j}.
  \label{}
\end{equation}

Combining \eqref{eq:gibbsduhem} with \eqref{eq:dG} leads to
\begin{align}
  \dd{G} = \sum_i \mu_i \dd{n_i} + \sum_i n_i \dd{\mu_i},
  \label{}
\end{align}
which can be recognized as a total differential for the alternative expression for G:
\begin{equation}
  G = \sum_i \mu_i n_i.
  \label{eq:G_alt}
\end{equation}

\subsection{Enthalpy}
The Enthalpy is defined as
\begin{equation}
  H(S,P,\vektor{n}) \equiv U + PV,
  \label{}
\end{equation}
and by using \eqref{eq:U_totaldiff}, one may show that
\begin{align}
  \dd{H} = T\dd{S} + V\dd{P} + \sum_i \mu_i \dd{n_i}.
  \label{eq:dH}
\end{align}
and one may then see that
\begin{equation}
  T = \pone{H}{S}{P,\vektor{n}}, \quad V = \pone{H}{P}{S,\vektor{n}}, \quad \mu_i = \pone{H}{n_i}{S,P,n_j}.
  \label{}
\end{equation}

\section{Equilibrium in multi-phase systems with given (T,P)}
\subsection{Systems with given (T,P)}
\label{sec:eq_givenTP}
To characterize the equilibrium state, let us image that a small perturbation from equilibrium happens, and then focus on
the spontaneous process ($\delta$) leading back to the equilibrium state.
For an isolated system, the second law of thermodynamics states that all spontaneous changes towards the equilibrium
state involve an increase in entropy:
\begin{equation}
  \delta S_\text{tot} \geq 0
  \label{eq:secondlaw}
\end{equation}
where $\delta$ indicates that it is not a change between different equilibrium states, as $\mathrm{d}$ is used to indicate.
We are however not interested in an isolated system, but rather a system with given temperature and pressure. This may be though of as a sub-system of a total
system, the latter satisfying \eqref{eq:secondlaw}. The other part of the total system is the \textit{reservoir}, which holds the given temperature and
pressure, and is large enough to force our system to keep the same values. Energy and volume (but not particles)
may be freely exchanged between the system (no subscript) and
the reservour (subscript r), in order to find the equilibrium of the total system. As entropy is an extensive quantity, the total entropy (which must increase) is
a sum of the entropy of the system and en entropy of the reservoir:
\begin{align}
  \delta S  + \delta S_\text{r} = \delta S_\text{tot} \geq 0.
  \label{eq:secondlaw_composite}
\end{align}
Defining $\delta Q$ as the heat transferred into the system, the change of entropy in the reservoir is given by
\begin{align}
  \delta S_\text{r} = - \frac{\delta Q}{T} = - \left( \frac{1}{T}\delta U + \frac{P}{T} \delta V \right),
  \label{}
\end{align}
by using the first and second laws of thermodynamics. The implication of \eqref{eq:secondlaw_composite} is then that
\begin{align}
  T \delta S \geq \delta U +  P \delta V.
  \label{eq:TdS}
\end{align}
The change in Gibbs energy at constant temperature and pressure is
\begin{align}
  \delta G &= \delta \left( U + PV - TS \right) \nonumber\\
  &= \delta U + P \delta V + V \delta P - T \delta S - S \delta T \nonumber\\
  &= \delta U + P \delta V - T \delta S,
  \label{}
\end{align}
which combined with \eqref{eq:TdS} shows that
\begin{equation}
  \delta G \leq 0.
  \label{}
\end{equation}
In words, this shows that a process which increases the entropy of the total system must decrease the Gibbs energy of a sub-system, given that the rest of the
total system behaves like a $T,P$ reservoir. Given that this process characterizes returns from perturbations around the equilibrium state, the
conclusion must be:
\begin{framed}
  \noindent
  For a closed system of given temperature and pressure, the equilibrium condition is that its Gibbs energy ($G$) is at a minimum.
\end{framed}

\subsection{Multi-phase systems}
\label{sec:eq_multiphase}
So far the internal workings of the system have been ignored. Now we want to take into account that the system may consist of multiple phases,
which essentially are sub-systems which are free to exchange energy, volume and particles.
As shown in Sec.~\ref{sec:eq_givenTP}, the condition for equilibrium is that the Gibbs energy for the entire system is at an extremum with respect to perturbation.
Let us assume that the perturbation is sufficiently small that the chemical potentials to not change, \textit{i.e.} $\delta \mu_i \approx 0$. This may also be seen as
assuming that the perturbation is so small that $\delta G \approx \dd{G}$, where the latter is given by \eqref{eq:dG}.
For constant $T$ and $P$ this means that
\begin{align}
  \delta G = \sum_j \delta G_j = \sum_j \sum_i \mu_i^{(j)} \delta n_i^{(j)} = 0,
  \label{eq:dG_multiphase}
\end{align}
where $j$ indicates the phase, and $i$ indicates the component. The conservation of matter, given no chemical reactions,
gives that any change in the mole number of a component in an arbitrary phase $\alpha$, must come from a net opposite change in the same
component in the other phases:
\begin{equation}
  \delta n_i^{(\alpha)}  = - \sum_{j\neq \alpha} \delta n_i^{(j)}.
  \label{eq:matter_cons}
\end{equation}
Combining \eqref{eq:dG_multiphase} and \eqref{eq:matter_cons} leads to
\begin{equation}
  \sum_{j\neq \alpha} \sum_i \left( \mu_i^{(j)} - \mu_i^{(\alpha)} \right) \delta n_i^{(j)} = 0.
  \label{}
\end{equation}
Given that the above must be true for any variation of the independent mole numbers, this means that
\begin{equation}
  \mu_i^{(\alpha)} = \mu_i^{(j)} \quad \forall \quad  i,j
  \label{}
\end{equation}
which in words is stated as:
\begin{framed}
  \noindent
  For a multi-phase system of given temperature and pressure, the equilibrium condition is that for each component, the chemical potential
  is equal in all phases.
\end{framed}

Additionally, there is a stability criterion for each phase, stated as:
\begin{framed}
  \noindent
  For a multi-phase system of given temperature and pressure, the condition for phase stability is that the change in total Gibbs energy associated with
  the formation of a new infinitesimal phase within an existing phase is non-negative, for \textit{any} composition of the new phase.
  This is called the \textit{tangent plane condition}.
\end{framed}
The change in Gibbs energy with the formation of a new phase of $\delta n$ moles and composition $w_i$, from an existing phase of composition $z_i$, is
\begin{align}
  \delta G
  &= \sum_i \mu_i(\vektor{z})\left( - w_i \delta n \right) + \sum_i \mu_i(\vektor{w})\left( + w_i \delta n \right) \nonumber\\
  &= \delta n \sum_i w_i \left( \mu_i(\vektor{w}) - \mu_i(\vektor{z}) \right),
  \label{}
\end{align}
which must be non-negative for any composition $\vektor{w}$, for the existing phase composition $\vektor{z}$:
\begin{align}
  \sum_i w_i \left( \mu_i(\vektor{w}) - \mu_i(\vektor{z}) \right) \geq 0.
  \label{eq:tangentplane}
\end{align}
When checking the stability of equilibrium phases, the chemical potential of each component is equal between the phases, and thus the left hand side of
\eqref{eq:tangentplane} is identical for each phase.

\section{Equilibrium in terms of an equation of state}
\subsection{The equation of state}
An \textit{equation of state} is often given as an explicit pressure equation, such as
\begin{equation}
  P = P (T,V,\vektor{n}).
  \label{}
\end{equation}
However, many equations of state is expressed as a Helmholtz energy
function,
\begin{equation}
  A = A(T,V,\vektor{n}).
  \label{}
\end{equation}
which is useful because all thermodynamical properties
may be derived by differentiating it.

The Helmholtz function of new multi--parameter GERG equations of state
\cite{kunz07,span96} and the older cubic equations of state is represented as a sum of
an ideal contribution, and a departure (residual) contribution:
\begin{equation}
\label{eq:helmholtz}
A(T,V,\vektor{n}) = A^{\text{ig}}(T,V,\vektor{n}) + A^{\text{r}}(T,V,\vektor{n})
\end{equation}

Often a reduced (dimensionless) form of the residual Helmholtz energy is used,
\begin{equation}
\label{eq:reduced_helmholtz}
F(T,V,\vektor{n}) = a \equiv  \frac{A^\text{r}(T,V,\vektor{n})}{nRT}
\end{equation}

\subsection{The residual Helmholtz energy}

The relationship between pressure and the Helmholtz energy two forms is:
\begin{equation}
  P(T,V,\vektor{n}) = -\pone{A}{V}{T,\vektor{n}}
  = \frac{nRT}{V} - \pone{A^{\text{r}}(T,V,\vektor{n})}{V}{T,\vektor{n}}.
  \label{}
\end{equation}
An expression for $A^\text{r}$ may be found by integrating to the limit of infinite volume at constant temperature and mole numbers:
\begin{equation}
  A^\text{r}(T,V,\vektor{n})
  = A^\text{r}(T,\infty,\vektor{n}) + \int_\infty^V \left[ P^\text{ig}(T,V^\prime,\vektor{n}) - P(T,V^\prime,\vektor{n}) \right]\dd{V^\prime},
  \label{}
\end{equation}
which when using that real fluids behave as an ideal gas in the limit of zero pressure ($V \to \infty$) leads to the expression:
\begin{framed}
\begin{align}
  A^\text{r}(T,V,\vektor{n})
  = \int^\infty_V \left[ P(T,V^\prime,\vektor{n}) - \frac{nRT}{V^\prime} \right]\dd{V^\prime}
  \label{eq:helmholtz_int}
\end{align}
\end{framed}

Be aware that residual quantities will depend on which set of variables they are represented with. As an example, let's show
how this works out for the residual Helmholtz energy:
From the state $(T,V,P,\vektor{n})$ by definition being a real state, we know that
\begin{equation}
  A(T,V,\vektor{n}) = A(T,P,\vektor{n}).
  \label{eq:Aequal}
\end{equation}
While the value of $A$ is independent of representation, the distribution of it between the ideal
contribution and the residual is not. This is so because
the state $(T,V,P,\vektor{n})$ is not in
general a valid state of an ideal gas, so
generally we have that
\begin{equation}
  A^\text{ig}(T,V,\vektor{n}) \neq A^\text{ig}(T,P,\vektor{n}).
  \label{}
\end{equation}

Using an arbitrary valid ideal gas state at temperature $T$, $(T,V_0,P_0,\vektor{n})$, we
have by definition of this state that
\begin{equation}
  A^\text{ig}(T,P_0,\vektor{n}) = A^\text{ig}(T,V_0,\vektor{n})
  \label{eq:Aequal_ideal}
\end{equation}

Combining \eqref{eq:helmholtz}, \eqref{eq:Aequal}, \eqref{eq:Aequal_ideal} and the ideal gas law
leads to the following calculation
for the difference in residuals between variable representations:
\begin{align}
  A^\text{r}(T,V,\vektor{n}) - A^\text{r}(T,P,\vektor{n})
  &= A^\text{ig}(T,P,\vektor{n}) - A^\text{ig}(T,V,\vektor{n})
  \nonumber \\
  &=\left[A^\text{ig}(T,P,\vektor{n}) - A^\text{ig}(T,P_0,\vektor{n})\right]
  -
  \left[A^\text{ig}(T,V,\vektor{n}) - A^\text{ig}(T,V_0,\vektor{n})\right]
  \nonumber\\
  &=\int_{P_0}^P
  \pone{A^\text{ig}}{P}{T,\vektor{n}}
  \dd{P}
  -
  \int_{V_0}^V
  \pone{A^\text{ig}}{V}{T,\vektor{n}}
  \dd{V}
  \nonumber\\
  &= nRT \ln\left( \frac{PV}{P_0 V_0} \right)
  = nRT \ln\left( \frac{PV}{nRT} \right)
  = nRT \ln\left( Z \right),
  \label{}
\end{align}
where $Z$ is the compressibility factor, defined by
\begin{equation}
  Z \equiv \frac{PV}{nRT} = 1 - \frac{V}{nRT} \pone{A^\text{r}(T,V,\vektor{n})}{V}{T,\vektor{n}} .
  \label{eq:Z_def}
\end{equation}

Thus we have the following expression for the residual Helmholtz free energy expressed through the
variables $(T,P,\vektor{n})$:
\begin{equation}
\label{eq:helmholtz_int_TPN}
A^{\text{r}}(T,P,\vektor{n})  = \int_V^\infty \left(P(T,V^\prime,\vektor{n}) - \frac{nRT}{V^\prime}\right) \dd{V^\prime}
-nRT \ln\left( Z \right)
\end{equation}

\subsection{Fugacity}
For a pure ideal gas, the change in chemical potential corresponding to a change in pressure, at constant temperature, is give by
\begin{equation}
  \frac{P}{P_0} = \exp\left( \frac{\mu^\text{ig}(T,P) - \mu^\text{ig}(T,P_0) }{RT} \right).
  \label{}
\end{equation}
Real fluids do not obey this relation, but one may define an ``effective pressure'' called
\textit{fugacity} ($f$) such that the following relation holds:
\begin{equation}
  \frac{f(T,P)}{P_0} = \exp\left( \frac{\mu(T,P) - \mu^\text{ig}(T,P_0) }{RT} \right).
  \label{}
\end{equation}
The reference pressure is arbitrary, so one may set $P_0=P$ to make an expression only in terms of the deviation from ideal gas:
\begin{equation}
  \frac{f(T,P)}{P} = \exp\left( \frac{\mu(T,P) - \mu^\text{ig}(T,P) }{RT} \right),
  \label{}
\end{equation}
which shows that for ideal gases, $f(T,P)=P$.

\subsubsection{Ideal gas mixtures}
An ideal gas mixture is defined as a mixture which has the following expression for the Helmholtz free energy:
\begin{equation}
  A^\text{ig}(T,V,\vektor{n}) \equiv \sum_i n_i \left(
  \mu_i^\text{ig}(T,P_0) + RT\ln \frac{n_i RT}{P_0 V} - RT
  \right)
  \label{eq:def_idealmix}
\end{equation}
where $n_i$ is the mole number of component $i$, and
$\mu^\text{ig}_i(T,P)$ is the chemical potential of component $i$ in its pure form
at the given temperature and pressure. This definition is consistent with the ideal gas law for the mixture as a whole,
$PV=nRT$, where $n=\sum n_i$.
From \eqref{eq:def_idealmix} one may find the Gibbs free energy of the
ideal gas mixture as
\begin{align}
  G^\text{ig}(T,P,\vektor{n})
  &= A^\text{ig}(T,V,\vektor{n}) + \left( PV \right)^\text{ig} = A^\text{ig}(T,V,\vektor{n}) + nRT \nonumber\\
  &= \sum_i n_i \left(
  \mu_i^\text{ig}(T,P_0) + RT\ln \frac{x_i P}{P_0}
  \right)
  \label{eq:G_idealmix}
\end{align}
where $x_i \equiv n_i/n$ is the \textit{mole fraction} of component $i$.

From \eqref{eq:def_idealmix} and \eqref{eq:G_idealmix}
one may find the chemical potential, in terms of $V$ or $P$, of component $i$ in the ideal mixture as
\begin{align}
  \mu_i^\text{ig}(T,V,\vektor{n}) &\equiv \pone{A^\text{ig}}{n_i}{T,V} = \mu_i^\text{ig}(T,P_0) + RT\ln \frac{n_i RT}{P_0 V} \nonumber \\
  \mu_i^\text{ig}(T,P,\vektor{n}) &\equiv \pone{G^\text{ig}}{n_i}{T,P} = \mu_i^\text{ig}(T,P_0) + RT\ln \frac{x_i P}{P_0},
  \label{eq:mu_ig}
\end{align}
which are equal if the state $(T,V,P,\vektor{n})$ is a valid ideal gas state ($PV=nRT$), as expected. Combining the two
equations in \eqref{eq:mu_ig}, while setting $P_0=P$, leads the the following useful expression:
\begin{equation}
  \mu_i^\text{ig}(T,V,\vektor{n}) - \mu_i^\text{ig}(T,P,\vektor{n})
  =-RT\ln \left( Z \right)
  \label{eq:mu_ig_PVdiff}
\end{equation}
where if $(T,V,P,\vektor{n})$ is a valid state for an ideal gas,
$Z=1$, and the right hand side reduces to zero as expected.

The last equation in \eqref{eq:mu_ig} may be rearranged to form the inspiration for the definition of
real mixture fugacity in the next section:
\begin{equation}
  \frac{P_i}{P_0} = \exp\left( \frac{\mu^\text{ig}_i(T,P,\vektor{n}) - \mu^\text{ig}_i(T,P_0)}{RT} \right)
  \label{eq:p_mu_ideal}
\end{equation}
where $P_i=x_iP$ is the \textit{partial pressure} of component $i$. For $P_0 = P$ this reduces to
\begin{equation}
  \mu^\text{ig}_i(T,P,\vektor{n}) - \mu^\text{ig}_i(T,P) = RT\ln x_i
  \label{eq:mu_and_xi_idealmix}
\end{equation}

\subsubsection{Real mixtures}
\label{sec:realmixtures}
Similar to the case of pure components, one may define an ``effective partial pressure'' of a component in a mixture, the
component fugacity ($f_i$), such that the following relation similar to \eqref{eq:p_mu_ideal} holds:
\begin{equation}
  \frac{f_i(T,P,\vektor{n})}{P_0} \equiv \exp\left( \frac{\mu_i(T,P,\vektor{n}) - \mu_i^\text{ig}(T,P_0)}{RT} \right),
  \label{eq:fug_def_mix}
\end{equation}
where one may again set $P_0=P$ to state it in terms of deviations from a pure ideal gas:
\begin{equation}
  \frac{f_i(T,P,\vektor{n})}{P} \equiv \exp\left( \frac{\mu_i(T,P,\vektor{n}) - \mu_i^\text{ig}(T,P)}{RT} \right),
  \label{eq:fug_def_mix_P0eqP}
\end{equation}

The chemical potential of a component in a real mixture may be decomposed into an ideal gas part and a residual part:
\begin{align}
  \mu_i(T,V,\vektor{n}) &= \mu_i^\text{ig}(T,V,\vektor{n}) + \mu_i^\text{r}(T,V,\vektor{n}) \nonumber\\
  \mu_i(T,P,\vektor{n}) &= \mu_i^\text{ig}(T,P,\vektor{n}) + \mu_i^\text{r}(T,P,\vektor{n})
  \label{}
\end{align}
which may be combined with \eqref{eq:mu_ig_PVdiff} to show that
\begin{equation}
  \mu_i^\text{r}(T,V,\vektor{n}) - \mu_i^\text{r}(T,P,\vektor{n})
  =RT\ln \left( Z \right)
  \label{eq:mu_r_PVdiff}
\end{equation}

Combining \eqref{eq:fug_def_mix_P0eqP} and \eqref{eq:mu_and_xi_idealmix}, one finds that
\begin{equation}
  \frac{f_i(T,P,\vektor{n})}{x_i P} \equiv
  \exp\left( \frac{\mu_i(T,P,\vektor{n}) - \mu_i^\text{ig}(T,P,\vektor{n})}{RT}
  \right)
  \label{eq:fugcoeff_vs_mu}
\end{equation}

The \textit{fugacity coefficient} may now be defined, calculated using \eqref{eq:fugcoeff_vs_mu}
and \eqref{eq:mu_r_PVdiff} as follows:
\begin{align}
  \phi_i(T,P,\vektor{n}) \equiv \frac{f_i(T,P,\vektor{n})}{x_i P} &=
  \exp\left( \frac{\mu_i(T,P,\vektor{n}) - \mu_i^\text{ig}(T,P,\vektor{n})}{RT}\right)
  \nonumber\\
  &= \exp\left( \frac{\mu_i^\text{r}(T,P,\vektor{n})}{RT} \right)
  \nonumber\\
  &= \exp\left( \frac{\mu_i^\text{r}(T,V,\vektor{n})}{RT} - \ln\left( Z \right) \right)
  \nonumber\\
  &= \exp\left( \frac{1}{RT}\pone{A^\text{r}(T,V,\vektor{n})}{n_i}{T,V,n_j} - \ln\left( Z \right) \right)
  \label{eq:fugcoeff}
\end{align}
where $A^\text{r}(T,V,\vektor{n})$ may be found from \eqref{eq:helmholtz_int}.

As shown in Sec.~\ref{sec:eq_multiphase}, the condition of phase equilibrium is that for each component,
the chemical potential must be equal in all phases. Using \eqref{eq:fug_def_mix_P0eqP}, and the fact that
$\mu_i^\text{ig}(T,P)$ is independent of phase, the condition may be restated in the more useful form:
\begin{framed}
  \noindent
  For a multi-phase system of given temperature and pressure, the equilibrium condition is that for each component $i$, the quantity
  $x_i \phi_i$ is equal in all phases. The fugacity coefficient may be calculated from the equation of state through
  \eqref{eq:fugcoeff} and \eqref{eq:Z_def}.
\end{framed}

\section{TP Flash}

\subsection{General equations of phase equilibrium and stability}
As shown in Sec.~\ref{sec:eq_multiphase} and~\ref{sec:realmixtures}, the criterion for equilibrium in a multi-phase system at given temperature
and pressure is that for each component, the fugacity is equal in all phases.
\begin{equation}
  f_i^{(\alpha)} = f_i^{(j)} \quad \forall \quad i,j,\alpha
  \label{}
\end{equation}

Additionally, there is a phase stability criterion, stated as the \textit{tangent plane condition} in Sec.~\ref{sec:eq_multiphase}.
Let us define a \textit{tangent plane distance function} for the composition $\vektor{z}$, a function of a trial composition $\vektor{w}$:
\begin{equation}
  \mathit{TPD}_z(\vektor{w}) \equiv \sum_{i} w_i \left( \mu_i(\vektor{w}) - \mu_i(\vektor{z}) \right).
  \label{}
\end{equation}
It is often conventient to use a
\textit{reduced tangent plane distance function}
\begin{align}
  \mathit{tpd}_z(\vektor{w}) &\equiv \frac{\mathit{TPD}(\vektor{w})}{RT}
  \nonumber\\
  &= \sum_{i} w_i \left[ \ln w_i + \ln \phi_i(\vektor{w}) - \ln z_i - \ln \phi_i(\vektor{z}) \right]
  \nonumber\\
  &= \sum_{i} w_i \left[ \ln w_i + \ln \phi_i(\vektor{w}) - d_i(\vektor{z}) \right],
  \label{}
\end{align}
where $d_i \equiv \ln z_i + \ln \phi_i(\vektor{z}) $

The phase stability criterion is then
\begin{equation}
  \mathit{tpd}_z(\vektor{w}) \geq 0 \quad \text{for any valid composition $\vektor{w}$}
  \label{}
\end{equation}
and this may be shown to be both a necessary and sufficient condition for phase stability \cite{michelsen07}.
In practice, ensuring that $\mathit{tpd}_z(\vektor{w})$ is non-negative everywhere in compositional space is done by finding its
minima, and then checking if it is negative at these locations. Finding the minima of $\mathit{tpd}_z(\vektor{w})$, subject to the constraint of
$\sum w_i = 1$, may be solved using the \textit{method of Lagrange multipliers}.

Modified (unconstrained) formulation:
\begin{align}
  \mathit{tm}_z(\vektor{W}) &= 1 + \sum_{i} W_i \left[ \ln W_i + \ln \phi_i(\vektor{W}) - d_i(\vektor{z}) -1\right]
  \nonumber\\
  &= \left( 1 - W_T + W_T \ln W_T \right) + W_T \mathit{tpd}_z(\vektor{w})
  \label{eq:tm}
\end{align}
where $W_i = w_i W_T$ ($W_T \equiv \sum W_i $) are mole numbers, not composition, and are thus not constrained to sum to one.
At stationary points of $\mathit{tm}_z(\vektor{W})$ with respect to $W_i$:
\begin{equation}
  \ln W_i^\text{SP} + \ln \phi_i(\vektor{W}^\text{SP}) - d_i(\vektor{z}) = 0 \quad \forall \quad i
  \label{eq:tm_stationary}
\end{equation}
\begin{equation}
  \mathit{tm}_z(\vektor{W}^\text{SP}) = 1 - W_T^\text{SP}
  \label{}
\end{equation}

It may be shown~\cite[Ch.~9]{michelsen07} that the stationary points of
$\mathit{tm}_z(\vektor{W})$ are also valid stationary points for the
constrained function $\mathit{tpd}_z(\vektor{w})$, and
$\mathit{tpd}_z(\vektor{w}^\text{SP})$ is positive at these minima if and only
if $W_T^\text{SP} \leq 1$, \textit{i.e.} if
$\mathit{tm}_z(\vektor{W}^\text{SP}) \geq 0$. The procedure to check stability
can then be formulated as:
\begin{framed}
\begin{enumerate}
  \item Find the stationary points of $\mathit{tm}_z(\vektor{W})$ with respect to $\vektor{W}$, \textit{i.e} solutions of \eqref{eq:tm_stationary}.
  \item Check if $W_T^\text{SP} \leq 1$ at all the stationary points found. If not, the mixture $\vektor{z}$ is unstable.
\end{enumerate}
\end{framed}
Eq.~\eqref{eq:tm_stationary} may be solved by \textit{successive substitution}
or \textit{Newton's method}. As discussed by Michelsen \cite{michelsen07},
successive substitution will converge in most case but may suffer from a low
rate of convergence, giving merit to the use of Newton's method.

A composition $\vektor{z}$ is metastable as long as the Hessian matrix
$H(\vektor{z})$
of $\mathit{tm}_z(\vektor{W})$ is positive definite.
If not, the state is \textit{intrinsically unstable}.
The limit between the two cases is the
\textit{stability limit / spinodal line}.

How to uncover all negative minima of $\mathit{tm}_z(\vektor{W})$ (if any)?
This cannot be done with total certainty, but one may use some clever initial
estimates to perform a finite set of searches which are likely to uncover
them.

\subsection{Tangent plane stability analysis}

Tangent plane stability analysis serves two purposes:
\begin{itemize}
  \item Verifies that a single phase state is stable.
  \item If the single phase state is not stable, gives an improved K-factor guess.
\end{itemize}
As shown, a phase of composition $\vektor{z}$ is stable at the specified temperature and pressure \textit{if and only if}
$tpd_z(\vektor{w}) \geq 0$ for any trial phase composition $\vektor{w}$. This could checked by inspecting the minima of
$\mathit{tm}_z(\vektor{W})$ \eqref{eq:tm} and checking the criterion
$W_T^\text{SP} \leq 1$.

The original composition is \textit{intrinsically unstable} if the Hessian
\begin{equation}
  H_{ij} \equiv \frac{\partial^2 \mathit{tm}_z}{\partial W_i \partial W_j}
  = \frac{\delta_{ij}}{W_i} + \frac{\partial \ln \phi_i}{\partial W_j}
  \label{}
\end{equation}
has any negative eigenvalues at the trivial stationary point at $\vektor{W}=\vektor{z}$. If it has no negative eigenvalues at
this point, the mixture is either stable ($\mathit{tm}_z(\vektor{W}) > 0$ at all its minima) or
\textit{metastable} ($\mathit{tm}_z(\vektor{W}) < 0$ at some minimum where $\vektor{W}\neq \vektor{z}$).

By using \eqref{eq:tm_stationary}, minima of $\mathit{tm}_z(\vektor{W})$ may be found by successive substitution:
\begin{equation}
  \ln W_i^{k+1} = d_i(\vektor{z}) - \ln \phi_i \left( \vektor{W}^k \right),
  \label{}
\end{equation}
which may be shown to only converge to minima, not other stationary points. Slow convergence may be remedied by higher order
methods.
Outcomes of stability analysis:
\begin{itemize}
  \item A composition $\vektor{W}$ where $\mathit{tm}_z(\vektor{W}) < 0$ is found. Continue the flash calculation with $\vektor{w}$
    as a new phase.
  \item All attempts converge to either the trivial solution of a positive minimum. The composition $\vektor{z}$ is thus stable.
\end{itemize}

\subsection{Mixture reduced molar Gibbs energy}
By combining \eqref{eq:G_alt}, \eqref{eq:fugcoeff_vs_mu} and \eqref{eq:mu_and_xi_idealmix}, one may show that
the Gibbs energy of a multi-phase mixture is given by
\begin{align}
  G &= \sum_j \sum_i n_i^{(j)} \mu_i^{(j)} \nonumber\\
  &= \sum_j\sum_i n_i^{(j)} \left[ \mu_i^\text{ig}(T,P,\vektor{n}) + RT\ln \phi_i^{(j)}(T,P,\vektor{n})\right] \nonumber\\
  &= \sum_j\sum_i n_i^{(j)} \left[ \mu_i^\text{ig}(T,P) + RT\ln x_i^{(j)} + RT\ln \phi_i^{(j)}(T,P,\vektor{n})\right] \nonumber\\
  &= \sum_j\sum_i n_i^{(j)} \mu_i^\text{ig}(T,P) +  \sum_j \sum_i n_i^{(j)} \left[ RT\ln x_i^{(j)} + RT\ln \phi_i^{(j)}(T,P,\vektor{n})\right] \nonumber\\
  &= \sum_i \left( \sum_j n_i^{(j)} \right) \mu_i^\text{ig}(T,P) +
      \sum_j \sum_i n_i^{(j)} \left[ RT\ln x_i^{(j)} + RT\ln \phi_i^{(j)}(T,P,\vektor{n})\right] \nonumber\\
  &= \sum_i  n_i^{(\text{tot})}  \mu_i^\text{ig}(T,P) +
      \sum_j \sum_i n_i^{(j)} \left[ RT\ln x_i^{(j)} + RT\ln \phi_i^{(j)}(T,P,\vektor{n})\right]
  \label{eq:G_mix}
\end{align}

When wanting to compare the total Gibbs energy of different phase configurations possible from the same overall composition,
the first term in \eqref{eq:G_mix} will cancel due to being identical in all configurations. It is thus not necessary to consider
it further for these purposes. Denoting this term by $G_0 \equiv \sum_i  n_i^{(\text{tot})}  \mu_i^\text{ig}(T,P)$, one may
define a reduced molar Gibbs energy useful for comparing different phase configurations from the same feed by
\begin{align}
  g &\equiv \frac{G - G_0 }{nRT} \nonumber\\
    &= \sum_i \sum_j \beta_j  x_i^{(j)} \left[ \ln x_i^{(j)} + \ln \phi_i^{(j)}(T,P,\vektor{n})\right],
  \label{eq:g_func}
\end{align}
where $\beta_j$ is the fraction of moles in phase $j$.

\subsection{Two-phase flash}
Consider a mixture of $C$ components, of overall composition $\vektor{z}$, and define the liquid and vapor compositions as
$\vektor{x}$ and $\vektor{y}$, respectively. When indices are used instead of vectors, the index $i$ indicates the component.
It is assumed that we have a thermodynamic model for the mixture, capable of calculating the fugacity of any component, given a set of
temperature, pressure and composition.

The equilibrium condition is
\begin{equation}
  f_i^l = f_i^v \quad \forall \quad i
  \label{eq:eq_twophase}
\end{equation}

Define $\beta$ as the overall mole fraction of the vapor phase, giving the material balance:
\begin{equation}
  \beta y_i + (1-\beta)x_i = z_i \quad \forall \quad i
  \label{eq:materialbalance}
\end{equation}

The mole fractions must sum to unity, a condition which may be transformed to the convenient form:
\begin{equation}
  \sum_{i=1}^C \left( x_i - y_i \right) = 0.
  \label{eq:mole_frac_sum}
\end{equation}
The above equations yield $2C + 1$ relations, while the number of variables ($\vektor{x}$, $\vektor{y}$, $T$, $P$, $\beta$) are $2C + 3$.
This shows that two additional pieces of information are needed, such as specifying $(T,P)$.

\textit{Equilibrium factors}:
\begin{equation}
  K_i \equiv \frac{y_i}{x_i}
  \label{eq:K_factors}
\end{equation}

Combining \eqref{eq:K_factors} and \eqref{eq:materialbalance}
\begin{equation}
  x_i = \frac{z_i}{1 - \beta + \beta K_i}, \quad\quad y_i = K_i x_i = \frac{K_i z_i}{1 - \beta + \beta K_i}
  \label{eq:xy_and_Kbeta}
\end{equation}

At equilibrium, where \eqref{eq:eq_twophase} is satisfied, we have that
\begin{equation}
  K_i \equiv \frac{y_i}{x_i} = \frac{\phi_i^l}{\phi_i^v}
  \label{eq:K_eq}
\end{equation}

Combining \eqref{eq:mole_frac_sum} with \eqref{eq:xy_and_Kbeta} leads to the \textit{Rachford-Rice} equation:
\begin{equation}
  g(\beta) \equiv \sum_{i=1}^C \left( x_i - y_i \right) = \sum_{i=1}^C \frac{z_i \left( K_i -1 \right)}{1 -\beta + \beta K_i} = 0
  \label{eq:rachford_rice}
\end{equation}
\begin{framed}
Two-phase TP flash by successive substitution:
\begin{enumerate}
  \item Use current (or initial) $\vektor{K}$ in the Rachford-Rice equation \eqref{eq:rachford_rice}.
  \item Solve the Rachford-Rice equation for a value of $\beta$ \textit{e.g.} by Newton's method.
  \item Find compositions $x_i$ and $y_i$ corresponding to the above values of $K_i$ and $\beta$, through \eqref{eq:xy_and_Kbeta}.
  \item Find fugacity coefficients from the equation of state given the above compositions, using \textit{e.g.} \eqref{eq:fugcoeff}.
  \item Find a new equilibrium factor $\vektor{K}$ using the above fugacity coefficients and the equilibrium assunption \eqref{eq:K_eq}.
  \item Go to 1 if $\vektor{K}$ has not converged yet.
\end{enumerate}
\end{framed}
Equal fugacities may also be satisfied at ``false solutions'', which are critical points of the Gibbs energy, but not minima.
Fortunately, it may be shown that successive substitution can only converge to minima of the Gibbs energy~\cite{michelsen07}.
Convergence will be slow at high pressures or close to the mixture critical point.

Initial values for $\vektor{K}$:
Wilson's approximation:
\begin{equation}
  \ln K_i = \ln \left( \frac{P_{c_i}}{P} \right) + 5.373(1+\gamma_i) \left( 1-\frac{T_{c_i}}{T} \right),
  \label{eq:wilson_K}
\end{equation}
where $P_{c_i}$, $T_{c_i}$ and $\gamma_{i}$ is the critical temperature, critical pressure and acentric factor, respectively,
of component $i$.

Single phase solution?\\
Convergence at $\beta <0$ or $\beta>0$, or convergence to the trivial solution $\vektor{x} = \vektor{y}$.\\

Flash strategy:
\begin{enumerate}
  \item Find Wilson K-factors \eqref{eq:wilson_K} to use as initial estimates.
  \item Do three steps of successive substitution. Check for conditions:
    \begin{itemize}
      \item Total Gibbs energy of the resulting liquid-vapor system is lower than in the feed (assuming single phase). This verifies the presence of
              multiple phases. Continue iteration, perhaps with higher order methods.
      \item If not the above, check  if $\mathit{tpd}_z(\vektor{x})$ or $\mathit{tpd}_z(\vektor{y})$ is negative. If the former, set
        $K_i = \phi_i^x / \phi_i^z$. If the latter, set $K_i = \phi_i^z / \phi_i^y$. Continue iteration.
    \end{itemize}
  \item If neither of the above conditions were met after three iterations, do tangent plane stability analysis.
\end{enumerate}
If at any time $\beta$ exceeds its bounds, it is very likely that the specification corresponds to a single phase state.
To be certain, perform tangent plane stability analysis.

Initializing $\vektor{W}$ for tangent plane analysis:
Investigate both ``liquid-like'' and ``vapor-like'' trial compositions, through
Wilson K-factors \eqref{eq:wilson_K}. If the feed itself may with confidence be said to be ``liquid-like'' or ``vapor-like'',
one may to save time only investigate the trial composition of the opposite kind.

\subsubsection{Solution by minimization}
When formulating an equilibrium calculation as a minimization problem, it
may be useful to use \textit{molar amounts} as the independent variables,
defined by
\begin{align}
  v_i &\equiv \beta y_i\nonumber\\
  l_i &\equiv (1-\beta) x_i,
  \label{}
\end{align}
such that
\begin{equation}
  v_i + l_i = z_i,
  \label{}
\end{equation}
where $\vektor{z}$ is now molar amounts, but numerically identical to the feed composition.

\begin{equation}
  y_i = \frac{v_i}{\sum v_i}, \quad x_i = \frac{l_i}{\sum l_i}
  \label{}
\end{equation}

\begin{equation}
  \beta_L \equiv 1-\beta
  \label{}
\end{equation}

\begin{equation}
  \beta = \sum v_i
  \label{}
\end{equation}

\begin{equation}
  \beta_L = \sum l_i
  \label{}
\end{equation}

The minimization problem to be solved is then
\begin{equation}
  \vektor{v} = \underset{\vektor{v} \in \mathbb{R}^C}{\argmin}\left\{g(\vektor{v},\vektor{l}(\vektor{v}))\right\}, \quad\text{with }
  \vektor{l} = \vektor{z}-\vektor{v}
  \label{}
\end{equation}
\textit{e.g.}\ by Newton's method of optimization (ref appendix).

By using \eqref{eq:g_func} for the case of two-phase systems:
\begin{equation}
  g = \sum_i \left( v_i \left[ \ln y_i + \ln \phi_i^v \right] + l_i \left[ \ln x_i + \ln \phi_i^l \right] \right)
  \label{eq:g_twophase}
\end{equation}

\begin{align}
  \frac{\partial g}{\partial v_j}
  =& \ln y_j - \ln x_j + \ln \phi_j^v - \ln \phi_j^l \nonumber\\
  &+ 1 - \underbrace{\sum_i y_i}_{=1} + \underbrace{\sum_i v_i \frac{\partial \ln \phi_i^v}{\partial v_j}}_{=0}\nonumber\\
  &- 1 + \underbrace{\sum_i x_i}_{=1} - \underbrace{\sum_i l_i \frac{\partial \ln \phi_i^l}{\partial l_j}}_{=0} \nonumber\\
  =& \ln y_j - \ln x_j + \underbrace{\ln \phi_j^v - \ln \phi_j^l}_{\text{From EoS}},
  \label{}
\end{align}
where the sums involving derivatives are zero due to \textit{Euler's theroem} for homogeneous functions, as explained in
1.8 of~\cite{michelsen07}.

\begin{align}
  \frac{\partial^2 g}{\partial v_i \partial v_j}
  &= \frac{\partial \ln \phi_j^v}{\partial v_i} - \frac{\partial \ln \phi_j^l}{\partial v_i}
  - \left( \frac{\beta + \beta_L}{\beta \beta_L} \right)
  + \delta_{i,j} \left( \frac{z_i}{x_i y_i \beta \beta_L} \right) \nonumber\\
  &=
  \frac{1}{\beta \beta_L} \left[
  \beta_L \underbrace{\left( \sum_i v_i\right)  \frac{\partial \ln \phi_j^v}{\partial v_i} }_\text{From EoS}
  +  \beta \underbrace{\left( \sum_i l_i\right)  \frac{\partial \ln \phi_j^l}{\partial l_i}}_\text{From EoS}
  - 1
  + \delta_{i,j} \left( \frac{z_i}{x_i y_i} \right)
  \right]\nonumber\\
  \label{}
\end{align}

The function $g$ in \eqref{eq:g_twophase} should at the located minimum be smaller than the smallest possible value
for the a single phase of feed composition:
\begin{equation}
  g_\text{feed} = \min\left\{ \sum_i  z_i \left[ \ln z_i + \ln \phi_i(\vektor{z}) \right]   \right\},
  \label{}
\end{equation}
which is either a liquid or a vapor state, depending on which has the lowest Gibbs energy.

\section{Cubic equations of state}
The general cubic equation of state has the form
\begin{equation}
P = \frac{RT}{v -b}- \frac{\alpha a}{(v-m_1 b)(v-m_2 b)},
\label{eq:gencubic}
\end{equation}
where $m_1$ and $m_2$ are dimensionless constants defining the SRK, PR and Van der
Waals equation of state, $v=V/n~(\SI{}{\meter^3\per\mol})$ is the specific volume. The
parameters $a~(\SI{}{\meter^3\joule\per \mol^2})$ and $b~(\SI{}{\meter^3\per\mol})$ typically
depend on the composition and the dimensionless quantity
$\alpha$ is a function of temperature.

\begin{shaded}
  \noindent
\textit{Exercise: Find the reduced residual Helmholtz energy function for a general
cubic equation of state \eqref{eq:gencubic}.}

\noindent
\textit{Solution}:
\begin{equation}
  A^\text{r}(T,V,N) = n \left[ RT\ln \left( \frac{v}{v-b} \right) +
  \frac{\alpha a}{\left( m_1-m_2 \right)b} \ln\left( \frac{v-m_1 b}{v-m_2 b} \right) \right]
  \label{}
\end{equation}

\end{shaded}

\subsection{Mixing rules}
Mixing rules are various ways of letting $a$ and $b$ in \eqref{eq:gencubic}
depend on composition.

\subsubsection{Classic mixing rule}
Parameters: $a_{ii}$, $k_{ij}$ and $b_i$.
\begin{equation}
  a = \sum_i x_i \sum_j x_j a_{ij}
  \label{}
\end{equation}

\begin{equation}
  a_{ij} = a_{ji} = \sqrt{a_{ii} a_{jj}} \left( 1-k_{ij} \right)
  \label{}
\end{equation}

\begin{equation}
  b = \sum_i x_i b_i
  \label{}
\end{equation}

\subsubsection{Huron-Vidal mixing rule}

\section{Explicit Helmholtz equations of state}
Based on an explicit expression for the reduced dimensionless Helmholtz energy \eqref{eq:reduced_helmholtz}. This includes
equations such as GERG2004/2008 and the Span-Wagner equation of state for \ce{CO2}.

\begin{equation}
  a(\delta,\tau,\vektor{x}) = a^\text{ig}(\rho,T,\vektor{x}) + a^\text{r}(\delta,\tau,\vektor{x}),
  \label{}
\end{equation}
where $\delta$ is the \textit{reduced mixture density} and $\tau$ is the \textit{reduced mixture temperature}, which are both generally
dependent on the composition:
\begin{equation}
  \delta = \frac{\rho}{\rho_r(\vektor{x})}, \quad \quad \tau = \frac{T}{T_r(\vektor{x})}.
  \label{}
\end{equation}

\section{Ideal gas Helmholtz energy}
The equation of state does not provide the energies.
In order to find the total Helmholtz energy, and not just the residual,
the ideal gas contribution $A^\text{ig}$ must be found.

An ideal gas is defined by two equations. One the equation of state
\begin{equation}
  P^\text{ig} = \frac{nRT}{V}
  \label{eq:idealgaslaw}
\end{equation}
and the second is the equation for the internal energy
\begin{equation}
  U^\text{ig}(T,n) = n c_v(T) T
  \label{eq:U_ideal}
\end{equation}
which simply states that the internal energy depends on the temperature and the number of particles only. The function $c_v$
is the \textit{heat capacity at constant volume}:
\begin{equation}
  C_v = n c_v \equiv \pone{Q}{T}{V} = \pone{U}{T}{V}.
  \label{}
\end{equation}
Additionally there is the \textit{heat capacity at constant pressure}:
\begin{equation}
  C_p = n c_p \equiv \pone{Q}{T}{P} = \pone{H}{T}{P},
  \label{}
\end{equation}
where it was used that $\dd{H} = \delta Q + V\dd{P}$.
For an ideal gas, the enthalpy is
\begin{align}
  H^\text{ig}(T,n) &\equiv U^\text{ig} + p^\text{ig}V
  \nonumber\\
  &= U^\text{ig}(T,n) + nRT
  \nonumber\\
  &= n\left( c_v + R \right)T
  \label{}
\end{align}
which shows that for an ideal gas
\begin{equation}
  c_p = c_v + R.
  \label{}
\end{equation}

Change in ideal gas enthalpy at a constant number of particles:
\begin{align}
  H^\text{ig}(T,n) - H^\text{ig}_0 &= \int_{T_0}^T \pone{H}{T}{P} \dd{T}
  \nonumber\\
   &= n \int_{T_0}^T c_p \dd{T}
  \label{}
\end{align}

Change in ideal gas entropy at a constant number of particles:
\begin{align}
  S^\text{ig}(T,V,n) - S^\text{ig}_0
  &= \int_{T_0}^T \pone{S}{T}{V} \dd{T} + \int_{V_0}^V \pone{S}{V}{T} \dd{V}
    \nonumber\\
    &=\int_{T_0}^T \frac{n c_v(T)}{T} \dd{T}
    + \int_{V_0}^V \pone{P}{T}{V} \dd{V}
    \nonumber\\
    &=n\int_{T_0}^T \frac{c_p(T) - R}{T} \dd{T}
    + nR\int_{V_0}^V \frac{1}{V} \dd{V}
    \nonumber\\
    &=n\int_{T_0}^T \frac{c_p(T) - R}{T} \dd{T}
    + nR\ln \left( \frac{V}{V_0} \right),
  \label{}
\end{align}
using
\begin{equation}
  \pone{S}{T}{V} = \pone{S}{U}{V} \pone{U}{T}{V} = \frac{1}{T} \pone{U}{T}{V} = \frac{n c_v}{T}
  \label{}
\end{equation}
and the \textit{Maxwell relation}
\begin{equation}
\label{eq:dsrel}
\pone{S}{V}{T,\vektor{n}} = \pone{P}{T}{V,\vektor{n}}.
\end{equation}

The total Helmholtz energy is not provided through the cubic equation of state, only the residual part.
The ideal gas part is usually specified as
algebraic temperature functions for the heat capacity at constant pressure, $c_p(T)$, provided
for each component in the mixture. The total Helmholtz energy may be written as
\begin{align}
  A &\equiv U - TS \nonumber\\
  &=U-TS+PV-PV \nonumber\\
  &= H - TS - PV,
  \label{}
\end{align}
which means that the Helmholtz energy of a pure ideal gas may be expressed as:
\begin{align}
  A^\text{ig}_i(T,V,n_i) &= H^\text{ig} - TS^\text{ig} - nRT
  \nonumber\\
  &= \Delta H_i^\text{ig} + H_{0,i}^\text{ig}
  - T\left(  \Delta S_i^\text{ig} + S_{0,i}^\text{ig}  \right)  - n_i RT
  \nonumber\\
  &=  n_i \int_{T_0}^T c_{p,i}^\text{ig}(T) \dd{T} + H_{0,i}^\text{ig}
  -  n_i T \left[ \int_{T_0}^T \frac{c_{p,i}^\text{ig}(T) - R}{T} \dd{T}
    + R \ln \left( \frac{V}{V_0} \right) \right]
     - T S_{0,i}^\text{ig} - nRT.
  \label{}
\end{align}

Mixing carries with it another entropy contribution, even in the case of ideal mixtures, \textit{i.e.} with no interactions. The entropy change
from ideal mixing at constant pressure and temperature is:
\begin{align}
  \Delta S^\text{ig}_\text{mix} &= -nR \sum_i x_i \ln x_i
  \nonumber\\
  &= -R \sum_i n_i \ln \left( \frac{n_i}{n} \right),
  \label{}
\end{align}
which means that the total Helmholtz energy of an ideal mixture is given by:
\begin{align}
  A^\text{ig}(T,V,\vektor{n}) &=  \sum_i A_i^\text{ig}(T,V,n_i) - T \Delta S^\text{ig}_\text{mix}
  \label{}
\end{align}
In terms of the reduced Helmholtz energy, this becomes:
\begin{align}
  a^\text{ig}(T,V,\vektor{x}) &=  \sum_i x_i a_i^\text{ig}(T,V) + \sum_i x_i \ln x_i,
  \label{}
\end{align}
where
\begin{equation}
  a_i^\text{ig}(T,V) \equiv \frac{A^\text{ig}_i(T,V,n_i)}{n_i RT}.
  \label{}
\end{equation}

\appendix

\section{Newton's method for root finding}
\section{Newton's method for optimization (Modified Newton)}
\section{Accelerated direct substitution (Dominant Eigenvalue Method)}

\begin{thebibliography}{9}

\bibitem{callen85}
  Herbert B. Callen,
  \emph{Thermodynamics and an Introduction to Thermostatistics}.
  second edition,
  John Wiley \& Sons Inc,
  1985.

\bibitem{kunz07}
  O. Kunz, R. Klimeck, W. Wagner, M. Jaeschke,
  \emph{The GERG-2004: Wide-Range Equation of State for Natural Gases and Other Mixtures}.
  GERG TM15 Report (http://www.gerg.eu/publications/tm.htm),
  2007.

\bibitem{michelsen82a}
  Michael L. Michelsen,
  \emph{The isothermal flash problem. Part I. stability}.
  Fluid Phase Equilibria,
  9(1)
  pp. 1-19,
  1982.

\bibitem{michelsen82b}
  Michael L. Michelsen,
  \emph{The isothermal flash problem. Part II. phase-split calculation}.
  Fluid Phase Equilibria,
  9(1)
  pp. 21-40,
  1982.

\bibitem{michelsen99}
  Michael L. Michelsen and J{\o}rgen M. Mollerup,
  \emph{State function based flash specifications}.
  Fluid Phase Equilibria,
  158-160
  pp. 617-626,
  1999.

\bibitem{michelsen07}
  Michael L. Michelsen and J{\o}rgen M. Mollerup,
  \emph{Thermodynamic models: Fundamentals \& computational aspects}.
  second edition
  Tie-Line Publication,
  2007.

\bibitem{span96}
  Roland Span and Wolfgang Wagner,
  \emph{A New Equation of State for Carbon Dioxide Covering the Fluid Region
  from the Triple-Point Temperature to 1100 {K} at Pressures up to 800 {MPa}}.
  J. Phys. Chem. Ref. Data,
  25(6)
  pp. 1509-1596
  1996.

\end{thebibliography}

\end{document}


\documentclass[../main.tex]{}
\begin{document}

\subsection{Clarke Wright Saving Algorithm}
The Clarke and Wright algorithm \cite{clarke_wright} is one of the earliest, and most popular heuristic algorithm for the VRP due to its speed, simplicity, and ease of adjustment to handle various constraints in real-life applications. Clarke Wright saving algorithm works equally well for both directed and undirected problems. The algorithm is built on a basic simple idea: maximize saving cost within a route, hence minimize total cost. Consider a depot D and n demand points. Suppose that initially the solution to the VRP consists of using n vehides and dispatching one vehicle to each one of the n demand points. The total cost of this solution is:
\[ C = 2\sum\limits_{i=1}^n d(D, i)\]
where d(i, j) is a function calculate distance between point i and , j, and C is total cost. To get a better solution, we now can combine 2 customers served by single vehicle on a single trip, the total distance we can save after combined is:
\begin{eqnarray}
s(i, j) & = & 2d(D, i) + 2d(D, j) \nonumber \\
&& -\: [d(D, i) + d(D, j) + d(i, j)] \nonumber \\
& = & d(D, i) + d(D, j) - d(i, j)
\end{eqnarray}•
\begin{algorithm}
\caption{Clarke Wright saving alorithm}\label{alg:clarke_wright}
\textbf{Step 1: Savings computation}
\flushleft
\begin{itemize}
\item Compute $s_{ij} \forall(i, j) \subset E$.
\item Sort $s_{ij}$
\end{itemize}
\textbf{Step 2:  Route Extension}
\flushleft
\begin{itemize}
\item for each route $(0, i, ..., j, 0)$
\item Determine the first saving ${s_{ki}}$ or ${s_{jl}}$ that can feasibly be used to merge the current route with another route ending with ${(k,0)}$ or starting with ${(0,l)}$.
\item Implement the merge and repeat this operation to the current route.
\item If not feasible merge exists, consider the next route and reapply the same operations.
\item Stop when not route merge is feasible.
\end{itemize}
\end{algorithm}

\subsection{Ruin and Recreate Algorithms}

Ruin and recreate framework is introduced in \cite{ref:rr_break}. By performing ruin and recreation on current solutions frequently enough, the better solutions will be obtained. A set of random vehicle routes are created and their total vehicle distance is calculated. The Ruin and Recreate approach is applied frequently to obtain another set of vehicle routes that have the least of total vehicle distance.

This approach starts with creating a set of initial vehicle routes: a random customer is selected, and combined with the depot location to create a base vector. The base vector starts to rotate clockwise with the depot location at the center and customers on the way are added to the route. If the route is over the vehicle capacity, a new route is created, and so on. Total vehicle distance is calculated for the initial routes.
Next, some customers from each initial route are removed by different ruin strategies and then added back to the same route in different order or different route using greedy insertion recreation strategy by trying all possible locations to pick the least total vehicle distance.
 There are three ruin strategies are used to remove customers from existing solution: random ruin, sequential ruin, and least customer ruin. Random strategy removes customers randomly from all customers of all routes, which creates the effect of diversifying the removing process. Sequential strategy removes customers sequentially in a selected route. The least customer strategy removes all customers in the route that has the least customers.
Greedy insertion strategy is used for the recreation of the solution by adding the removed customers back. This approach is to insert each removed customer to the cheapest possible route.

\begin{algorithm}
\caption{Ruin and recreate framework}\label{alg:RR}
\flushleft
\begin{itemize}
\item create intial solution
\item do {
\flushleft
\begin{itemize}
\item choose ruin and recreate strategy
\item ruin current solution
\item recreate ruined solution
\item if new solution better than current solution
\item then accept new solution
\item else accept new solution with probaility $e^{\frac{\Delta C}{T}}$
\end{itemize}
\item } while stop condition is not meet
\item return best solution
\end{itemize}
\end{algorithm}

\end{document}


%intro

\section{Introduction}
We are presenting a brief summory of basic concepts of modern algebraic geometry. In particular, we will discuss standard constructions and expand our learnings to a more abstract scheme theoretic approach.
\subsection{Requirements}
The reader only needs a minimum understanding in linear and abstract algebra. However, although some definitions will be missed we try to include as many as possible.\\
\subsection{Basics}
\indent We denote by $R$ an arbitrary ring with one (unital), unless mentioned otherwise, always commutative. A field is denoted by $k$ and its algebraic closure with $\overline{k}$, special fields and rings are denoted following \cite{Bour}. The polynomial ring for a given ring $R$ is denoted by $R[X] = R[X_1\ldots X_n]$ and the affine module $\mathbb{A}^n_R = (R^n, P(R^n), \phi)$.
\subsubsection{Zero sets and the Zariski topology}
Given a field $k$ and its polynomial ring $k[X] = k[X_1\ldots X_n]$ and a nonempty set of polynomials $F \subset R[X]$, the zero set of $F$, $Z(F)$, is defined:
$$\left\{x \in \kaffn : f(x) = 0 \ \forall f \in F\right\}.$$
We may rewrite:
$$\begin{array}{rcl}
Z(F) &=& \{x : f(x) = 0 \forall f \in F\}\\&&\\
&=& \bigcap_{f \in F} \{x : f(x) = 0\}.\\
\end{array}$$
In other words, $Z(F)$ is simply the intersection of common zeros of  each polynomial in $F$. Therefore, we could have simply defined zero sets for polynomials and then show that zero sets of $F$ are the intersections of the zeros of its elements.\\
\indent We remark that $Z(F)$ may be empty if no common zero is present (e.g. $f = X, g = X - 1 \in k[X]$). The zero set of the constant polynomial $1 \in k[X]$ is therefore empty and the zero set of $0 \in k[X]$ is the whole affine space.\\
\paragraph{Zeros and their topology}
By the above definition, we get that the set of all zeros is closed under arbitrary intersection. Furthermore, the empty set and the whole affine space are present in $\cldk := \{X \subset \kaffn : X \ \mathrm{zero~set}\}$. Now, let us check for two polynomials $f, g \in k[X]$:
$$\begin{array}{rcl}
Z(f) \cup Z(g) &=& \{x : f(x) = 0\} \cup \{x : g(x) = 0\}\\&&\\
&=& \{x : f(x) = 0 \vee g(x) = 0\}\\&&\\
&=& \{x : f(x) \cdot g(x) = 0\}\\&&\\
&=& Z(f g)\\
\end{array}$$
This shows $\cldk$ is closed under finite union and making it a closed topology (i.e. generated by closed sets) over $\kaffn$.\\
\paragraph{Principal open sets and their topology}
\indent Now, for a given $f \in k[X]$ we call the set
$$D(f) := \{x \in \kaffn : f(x) \neq 0\}$$
a principal open set of $\kaffn$. It is obviously the complement of a zero set. Now, for any nonempty set $F$ of polynomials we define the open set
$$D(F) := \bigcup_{f \in F} D(f).$$
Again, we remark that $D(F)$ may be empty or the full affine space:
$$D(F) = \kaffn \Leftrightarrow F \subset k^\times,\ D(F) = \emptyset \Leftrightarrow F = \{0\}.$$
As above, we need to check for closeness under intersection. However, as we defined our subset system via the complements of a topology generated by closed sets we get a topology generated by open sets. Therefore:
$$\opnk := \left\{U \subset \kaffn : U^c := \kaffn\backslash U \in \cldk\right\}$$
is an open topology on $\kaffn$.
\begin{defi}
The topology $\tau \subset \mathcal{P}(\kaffn)$ generated by both $\cldk$ and $\opnk$ is called the Zariski topology on $\kaffn$.
\end{defi}
\subsubsection{Affine schemes and the Zariski topology}
Given a unital commutative $k$ algebra $R$ and the class of all $R$-submodules of $R$, $\mathrm{Mod}_R(R)$, (i.e. its ideals)
$$\specr := \{\prm \in \mathrm{Mod}_R(R) : \prm \ \mathrm{prime}\},$$
the set of all prime ideals, is called the spectrum of $R$.
\paragraph{Pairing of points and ideals}
Given a $P = (P_1,\ldots,P_n) \in \kaffn$, the ideal
$$\mxx_P = \left<X_1 - P_1,\ldots,X_n - P_n\right>$$
is maximal in every polynomial ring $k[X]$ over some field $k$. As every maximal ideal in a unital ring is prime we get a unique subset of $\specr$:
$$\max (R) = \{\mxx \in \specr : \mxx \ \mathrm{maximal}\},$$
i.e. the set of maximal ideals in $R$.
\begin{prop}
Let $k = \overline{k}$ be a field and $R = k[X]$. The pairing
$$\kaffn \longrightarrow \max(R),\ P \longmapsto \mxx_P$$
is a bijection.
\end{prop}
\bmk This proposition is a rephrasing of Hilberts Nullstellensatz. Thus, the interested reader will find a proof in the standard literature as \cite{Hart}. In analogy to the Zariski topology on $\kaffn$ we define for some family of polynomials $F$:
$$V(F) = \{\prm \in \speck : f \in \prm,\ \forall f \in F\} = \bigcap_{f \in F} \{\prm : f \in \prm\}.$$
Accordingly, we call for some polynomial $f \in k[X]$ the set $U(f) = \{\prm : f \notin \prm\} = V(f)^c$ a prinicipal open set in $\speck$. Again, we get for some family $F$ of polynomials:
$$
\begin{array}{rclcl}
U(F) &:=& V(F)^c &=& \speck \backslash V(F)\\&&&&\\ &=& \speck \backslash \left(\bigcap_{f \in F} V(f)\right) &=& \bigcup_{f \in F} \left(\speck \backslash V(f)\right)\\&&&&\\ &=& \bigcup_{f \in F} V(f)^c &=& \bigcup_{f \in F} U(f).\\
\end{array}
$$
With this equivalences in place, we get the following proposition
\begin{prop}
The sets $\kaffn$ and $\X := \speck$ are isomorphic in the category of topological spaces with corresponding topologies $\cldk$ and $\opnk$, as well as
$$\mathcal{F}(\X) = \{V(F) : F \subset k[X]\}\ \mathrm{and}\ \mathcal{O}(\X) = \{V(F)^c : F \subset k[X]\},$$
respectively. Again, $V(F)^c := \speck \backslash V(F)$.
\end{prop}
\bws We simply use our bijective pairing above:
$$\kaffn \ni P = (P_1,\ldots,P_n) \longmapsto \mxx_P = \left<X_1 - P_1,\ldots,X_n - P_n\right> \in X.$$
Furthermore, we expand
$$\begin{array}{rclcl}
\cldk \ni Z(F) &=& \bigcap_{f \in F} Z(f) \longmapsto V(F) &=& \bigcap_{f \in F} V(f) \in \mathcal{F}(\X)\\&&&&\\
\opnk \ni D(F) &=& \bigcup_{f \in F} D(f) \longmapsto U(F) &=& \bigcup_{f \in F} U(f) \in \mathcal{O}(\X)\\
\end{array}$$


\documentclass[]{article}
\usepackage{amsmath, amssymb, graphicx, float, dsfont}

%opening
\title{Notes for "Statistics Informed Decisions using Data"}
\author{Delbert Yip}

\begin{document}

\maketitle

\begin{abstract}
	These notes covers several chapters from "Statistics Informed Decisions using Data," focusing on practical applications.
\end{abstract}

\section{Probability Distributions}
\textbf{Poisson distribution}
A random variable $X$, the number of successes in a fixed interval, follows a Poisson process if:
\begin{itemize}
	\item The probability of 2 or more successes in a sufficiently small subinterval, is 0.
	\item The probability of success is the same for any two intervals of equal length.
	\item The number of successes in any interval is independent of the number of successes in any other intervals, as long as said intervals do not overlap.
\end{itemize}

A Poisson process is described by:
\begin{equation}
P(x) = \frac{(\lambda t)^x}{x!} \exp(-\lambda t), \quad x=0,1,...n
\end{equation}

Where $\lambda$ is the average number of occurrences of the event in an interval of length 1.

\section{Inference}
\subsection{Hypothesis Tests}
We try to find support for the \textbf{alternative hypothesis}, $H_1$. The \textbf{null hypothesis}, $H_0$, is assumed true until refuted by evidence, and assumes no change/difference/etc. Three ways to set up null and alternative hypotheses:
\begin{enumerate}
	\item Two-tailed test: equal versus not equal
	\begin{align*}
		H_0 &= x \\
		H_1 &\neq x
	\end{align*}

	\item Left-tailed test: equal versus less than
	\begin{align*}
	H_0 &= x \\
	H_1 &< x
	\end{align*}

	\item Right-tailed test: equal versus greater than
	\begin{align*}
	H_0 &= x \\
	H_1 &> x
	\end{align*}
\end{enumerate}

$H_0$ is always a statement of equality. Left- and right-tailed tests are a.k.a one-tailed tests. There are two types of errors in hypothesis testing: Type I (false negative, $H_0$ is rejected when $H_0$ is actually true) and Type II (false positive, $H_0$ is not rejected while $H_1$ is true.) Importantly, the \textbf{level of significance, $\alpha$} is the \textit{probability of making a Type I error/false negative}.The  choice of $\alpha$ depends on the consequences of making a Type I error, or false negative. On the other hand, $\beta$ is the probability of making a Type II error/false positive. There is an inverse relation between $\alpha$ and $\beta$: when $\alpha$ is small, $\beta$ is large.

\end{document}


\documentclass[english]{../spicker}

\usepackage{amsmath}

\usepackage{graphicx}
\usepackage{tabularx, multirow}

\usepackage[style=numeric, backend=bibtex]{biblatex}
\addbibresource{or.bib}

\title{Operations Research}
\author{Patrick Gustav Blaneck}

\begin{document}
\maketitle
\tableofcontents
\newpage

%\setcounter{section}{1}

\section{Linear Programming}

\begin{defi}{Linear Programming}
    \emph{Linear Programming} is the problem of optimizing (maximizing or minimizing) a \emph{linear objective function} subject to a set of \emph{linear functional constraints}.

    \textbf{Given:} $A \in \R^{m\times n}, b \in \R^m, c\in R^n$

    \textbf{Find:} $x^* \in \R^n$ where $x^* = \arg\max\{c^Tx \mid Ax \leq b\}$
\end{defi}

\begin{bonus}{Linear Programming Solvers}
    Software that solves linear programs - \emph{linear programming solvers} - also generate lots of important auxiliary information (as well as the optimum):
    \begin{itemize}
        \item sensitivity analysis
        \item shadow prices
        \item alternative optima
        \item \ldots
    \end{itemize}
\end{bonus}

\begin{theo}{Ellipsoid Method}
    A LP of dimension $n$ can be solved in $\bigo(L^2 \cdot n^6)$ time \cite{khachiyan1979}, where $L =$ \# bits in the input.
\end{theo}

\begin{theo}{Interior Point Method}
    A LP of dimension $n$ can be solved in a \emph{numerically stable} way in $\bigo(L^2 \cdot n^{3.5})$ time \cite{karmarkar1984}.
\end{theo}

\begin{defi}{Integer Linear Programs (ILP)}
    \textbf{Given:} $A \in \R^{m\times n}, b \in \R^m, c\in R^n$

    \textbf{Find:} $\underline{x^*\in\Z^n}$ where $x^* = \arg\max\{c^Tx \mid Ax \leq b\}$
\end{defi}

\begin{example}{Integer Linear Program for \textsc{Vertex Cover}}
    \fbox{
        \parbox{0.95\textwidth}{
            \underline{\textsc{Vertex Cover}}

            \textbf{Given:} Graph $G = (V, E)$\\
            \textbf{Find:} \textsc{Vertex Cover}, i.e. $V' \subseteq V$ such that every edge has at least one endpoint in $V'$.
        }
    }

    \textbf{Integer Linear Program:}

    For $v\in V$, let $x_v \in \{0, 1\}$.

    Goal: minimize $\sum_{v\in V}x_v$.

    Constraints: for every edge $uv \in E$, we require $x_u + x_v \geq 1$.
\end{example}

\printbibliography
\end{document}


\section{Skipping}
\label{sec:skipping}

Here we envisage how to copy a stack while ignoring a given
item. [\emph{The progression is very slow on purpose.}]

\paragraph{First occurrence}
\label{def:linear_search}
\index{stack!skipping an item!first occurrence}

Let us suppose that \(\fun{sfst}(s,x)\)\index{sfst@\fun{sfst/2}|(}
(\emph{skip the first occurrence}) evaluates in a stack identical
to~\(s\) but without the first occurrence of~\(x\), starting from the
top. In particular, if~\(x\) is absent in~\(s\), then the value of the
call is identical to~\(s\). This is our
\emph{specification}.\index{specification} For instance, we expect the
following evaluations:\label{sfst_ex}
\begin{equation*}
\begin{array}{r@{\;}l@{\;}l}
\fun{sfst}(\el,3) & \twoheadrightarrow & \el;\\
\fun{sfst}(\el,\el) & \twoheadrightarrow & \el;\\
\fun{sfst}([3,\el],[5,2]) & \twoheadrightarrow & [3,\el];\\
\fun{sfst}([\el,[1,2],4,\el,4],4) & \twoheadrightarrow &
  [\el,[1,2],\el,4];\\
\fun{sfst}([4,[1,2],\el,\el,4],\el)
 & \twoheadrightarrow & [4,[1,2],\el,4].
\end{array}
\end{equation*}

\paragraph{First attempt}

Let us try a direct approach. In particular, at this point, it is
important \emph{not} to seek a definition in tail form. Tail form must
be considered as an optimisation and early optimisation is opening
Pandora's jar. The first idea that may come to mind is to define an
auxiliary function \fun{mem/2} such that the call \(\fun{mem}(s,x)\)
checks whether a given item \(x\)~is in a given stack~\(s\), because
that notion of membership is implicit in the wording of the
specification. But two problems then arise. Firstly, what would be the
result of such a function? Secondly, what would be the additional cost
for using it? For the sake of the argument, let us follow this track
and find out how where it leads. A stack can either be empty or not,
so let us make two rules:
\begin{equation*}
\begin{array}{r@{\;}c@{\;}l}
\fun{mem}(\el,x) & \rightarrow & \fbcode{CCCCC}\,;\\
\fun{mem}(\cons{y}{s},x) & \rightarrow & \fbcode{CCCCC}\,.
\end{array}
\end{equation*}
Note that we introduced a variable~\(y\), distinct from
variable~\(x\). \emph{Two different variables may or may not denote
  the same value, but two occurrences of the same variable always
  denote the same value.} Had we written instead
\begin{equation*}
\begin{array}{r@{\;}c@{\;}l}
\fun{mem}(\el,x) & \rightarrow & \fbcode{CCCCC}\,;\\
\fun{mem}(\cons{\underline{x}}{s},x) & \rightarrow & \fbcode{CCCCC}\,.
\end{array}
\end{equation*}
one case would be missing, namely when the top of the stack is not the
item sought for, for instance, \(\fun{mem}(3,[4])\) would fail due to
a match failure. Now, what is the first right\hyp{}hand side? The
first pattern matches only if the stack is empty. In particular, this
means that the item is not in the stack, since, by definition, an
empty stack is a stack containing no item. How do we express that?
Since the original problem is silent on the matter, it is said to be
\emph{underspecified}.\index{underspecification} We may think that
zero would be a token of choice to denote the absence of the item in
the stack:
\begin{equation*}
\begin{array}{r@{\;}c@{\;}l}
\fun{mem}(\el,x) & \rightarrow & 0;\\
\fun{mem}(\cons{y}{s},x) & \rightarrow & \fbcode{CCCCC}\,.
\end{array}
\end{equation*}
But this would be a mistake because there is no natural and necessary
relationship between the concept of emptiness and the number
zero. Zero is best understood algebraically as the number noted~\(0\)
such that \(0 + n = n + 0 = n\), for any number~\(n\). Then, let us
try the empty stack:
\begin{equation*}
\begin{array}{r@{\;}c@{\;}l}
\fun{mem}(\el,x) & \rightarrow & \el;\\
\fun{mem}(\cons{y}{s},x) & \rightarrow & \fbcode{CCCCC}\,.
\end{array}
\end{equation*}
The next step is to find a way to actually compare the value of~\(x\)
to the value of~\(y\). We can use the rule above about variables: two
occurrences of the same variable mean that they hold the same
value. Therefore
\begin{equation*}
\begin{array}{r@{\;}c@{\;}l}
\fun{mem}(\el,x) & \rightarrow & \el;\\
\fun{mem}(\cons{x}{s},x) & \rightarrow & \fbcode{CCCCC}\,.
\end{array}
\end{equation*}
was not so bad, after all? Indeed, but we know now that a case is
missing, so let us add it at the end, where \(x \neq y\):
\begin{equation*}
\begin{array}{r@{\;}c@{\;}l}
\fun{mem}(\el,x) & \rightarrow & \el;\\
\fun{mem}(\cons{x}{s},x) & \rightarrow & \fbcode{CCCCC};\\
\underline{\fun{mem}(\cons{y}{s},x)} & \rightarrow & \fbcode{CCCCC}.
\end{array}
\end{equation*}
Now, what is the second right\hyp{}hand side? It is evaluated if the
item we were looking for is present at the top of the stack. How do we
express that? We may think of ending with the item itself, the
rationale being that if the result is the empty stack, then the item
is not in the input stack, otherwise the result is the item itself:
\begin{equation*}
\begin{array}{r@{\;}c@{\;}l}
\fun{mem}(\el,x) & \rightarrow & \el;\\
\fun{mem}(\cons{x}{s},x) & \rightarrow & x;\\
\fun{mem}(\cons{y}{s},x) & \rightarrow & \fbcode{CCCCC}\,.
\end{array}
\end{equation*}
The last right\hyp{}hand side is easier to guess since it deals with
the case where the top of the stack (\(y\)) is not the item we seek
(\(x\)), so a recursive call which ignores~\(y\) should come to mind:
\begin{equation*}
\begin{array}{r@{\;}c@{\;}l}
\fun{mem}(\el,x) & \rightarrow & \el;\\
\fun{mem}(\cons{x}{s},x) & \rightarrow & x;\\
\fun{mem}(\cons{y}{s},x) & \rightarrow & \fun{mem}(s,x).
\end{array}
\end{equation*}

Some tests would increase the confidence that this definition is
correct\index{soundness} and complete\index{completeness} with respect
to the specification. Let us label the rules first:
\begin{equation*}
\begin{array}{r@{\;}c@{\;}l}
\fun{mem}(\el,x)         & \smashedrightarrow{\zeta}  & \el;\\
\fun{mem}(\cons{x}{s},x) & \smashedrightarrow{\eta}   & x;\\
\fun{mem}(\cons{y}{s},x) & \smashedrightarrow{\theta} &
\fun{mem}(s,x).
\end{array}
\end{equation*}
Then we could try the following cases:
\begin{equation*}
\begin{array}{r@{\;}l@{\;}l@{\;}l@{\;}l}
\fun{mem}(\el,3) & \smashedrightarrow{\zeta} & \el,\\
\fun{mem}([1],3) & \smashedrightarrow{\theta} & \fun{mem}(\el,3)
& \smashedrightarrow{\zeta} & \el,\\
\fun{mem}([1,3,2],3) & \smashedrightarrow{\theta} & \fun{mem}([3,2],3)
& \smashedrightarrow{\eta} & 3.
\end{array}
\end{equation*}
The code seems to work: item~\(x\) is in stack~\(s\) if the result
is~\(x\), otherwise it is~\(\el\). However, this function is not
correct.\index{soundness} The hidden and flawed assumption is ``items
can not be stacks'', in spite of the counter\hyp{}examples given at
the beginning for illustrating the expected behaviour of
\fun{sfst/2}. In particular, an item can be the empty stack and this
situation leads to an ambiguity with our definition of \fun{mem/2}:
\begin{equation*}
  \fun{mem}(\el,\el) \smashedrightarrow{\zeta} \el \xleftarrow{\smash{\eta}} \fun{mem}([\el],\el).
\end{equation*}
It is impossible to discriminate the two cases, the first one meaning
absence of the item and the second presence, because they both end
with the empty stack. In fact, we should have distinguished two data
constructors to denote the outcomes ``the item was found'' and ``the
item was not found''. For example,
\begin{equation*}
\begin{array}{r@{\;}l@{\;}l}
\fun{mem}(\el,x)         & \smashedrightarrow{\zeta}  & \fun{false}();\\
\fun{mem}(\cons{x}{s},x) & \smashedrightarrow{\eta}   & \fun{true}();\\
\fun{mem}(\cons{y}{s},x) & \smashedrightarrow{\theta} & \fun{mem}(s,x).
\end{array}
\end{equation*}
But, for now, let us backtrack and ask ourselves again whether using
\fun{mem/2} is really a good idea.

\paragraph{Better approach}

Let us suppose that the input stack contains the item at the
bottom. Using \fun{mem/2} to find it leads to a complete traversal of
the input stack. Then another traversal from the beginning (the top of
the stack) is needed to copy the stack without its last item, so, in
total, two complete traversals are performed.

A better idea consists in \emph{interleaving} these two passes into
one because the problem stems from the fact that \fun{mem/2} forgets
about the items which are not the item of interest, thus, when it is
found or known to be absent, there is no way to build a copy to make
the result. By interleaving, we mean that during the traversal, the
concepts of membership and of copying are combined, instead of being
used sequentially as two function calls. A similar situation was
encountered in the design of a function reversing a stack:
\fun{rev\(_0\)/1}, which calls \fun{cat/2}, is much slower than
\fun{rev/1}, which uses an auxiliary stack.

Here, our algorithm consists in memorising all visited items and, if
the item is not found, the resulting stack is rebuilt from them; if
found, the result is built from them \emph{and} the remaining,
unvisited, items. There are usually two ways to keep visited items:
either in an accumulative parameter, called \emph{accumulator}, or in
the context of recursive calls. At this point, it is important to
recall a cardinal guideline: Do not try first to design a definition
in tail form, but opt instead for a direct approach. In some simple
cases, a direct approach may actually be in tail form, but the point
is methodological: a priori ignore all concerns about tail
forms. Accordingly, let us use the context of a recursive call to
record the visited items. A stack being either empty or not, it is
natural to start with
\begin{equation*}
\begin{array}{r@{\;}l@{\;}l}
\fun{sfst}(\el,x) & \rightarrow & \fbcode{CCCCC}\,;\\
\fun{sfst}(\cons{y}{s},x) & \rightarrow & \fbcode{CCCCC}\,.
\end{array}
\end{equation*}
Then, just as we tried with \fun{mem/2}, we must distinguish the
case when \(x\)~is the same as~\(y\):
\begin{equation*}
\begin{array}{r@{\;}l@{\;}l}
\fun{sfst}(\el,x) & \rightarrow & \fbcode{CCCCC}\,;\\
\underline{\fun{sfst}(\cons{x}{s},x)} & \rightarrow & \fbcode{CCCCC}\,;\\
\fun{sfst}(\cons{y}{s},x) & \rightarrow & \fbcode{CCCCC}\,.
\end{array}
\end{equation*}
This method is a \emph{linear search}\index{linear search}: the items
in the stack are compared one by one to~\(x\), starting from the top,
until the bottom or an item equal to~\(x\) is reached. Since we know
that the last rule deals with the case when \(x \neq y\), we must
memorise~\(y\) and go on comparing~\(x\) with the other items in~\(s\)
(if any). This is where the recursive call with a context, discussed
above, is set as \(\cons{y}{\text{\textvisiblespace}}\):
\begin{equation*}
\begin{array}{r@{\;}l@{\;}l}
\fun{sfst}(\el,x) & \rightarrow & \fbcode{CCCCC}\,;\\
\fun{sfst}(\cons{x}{s},x) & \rightarrow & \fbcode{CCCCC}\,;\\
\fun{sfst}(\cons{y}{s},x) & \rightarrow & \cons{y}{\fun{sfst}(s,x)}.
\end{array}
\end{equation*}
Very importantly, let us remark that the position of~\(y\) in the
result is the same as in the input (the top). The second rule
corresponds to the case where the item we are looking for,
namely~\(x\), is found to be the top of the current stack, which is a
substack of the original input. A stack made of successive items from
the beginning of a given stack is called a \emph{prefix} of the
latter. When a stack is a substack of another, that is, it is made of
successive items including the last, it is called a \emph{suffix}. We
know that the~\(x\) in \(\cons{x}{s}\) is the first occurrence
of~\(x\) in the original stack (the one in the first call), because we
wouldn't be dealing with this case \emph{again}: the specification
states that this first occurrence must be absent from the resulting
stack; since it is now at the top of a suffix, we just need to end
with~\(s\), \emph{which we do not visit}:
\begin{equation*}
\begin{array}{r@{\;}l@{\;}l}
\fun{sfst}(\el,x)         & \rightarrow & \fbcode{CCCCC}\,;\\
\fun{sfst}(\cons{x}{s},x) & \rightarrow & \underline{s}\,;\\
\fun{sfst}(\cons{y}{s},x) & \rightarrow & \cons{y}{\fun{sfst}(s,x)}\,.
\end{array}
\end{equation*}
The first rule handles the case where we traversed the whole original
stack (up to \(\el\)) without finding~\(x\). Thus the result is simply
the empty stack because the empty stack without~\(x\) is the empty
stack:
\begin{equation*}
\begin{array}{r@{\;}l@{\;}l}
\fun{sfst}(\el,x)         & \rightarrow & \el;\\
\fun{sfst}(\cons{x}{s},x) & \rightarrow & s;\\
\fun{sfst}(\cons{y}{s},x) & \rightarrow & \cons{y}{\fun{sfst}(s,x)}.
\end{array}
\end{equation*}

Let us run some tests now and, in order avoid mistakes, it is handy to
label the rules with some Greek letters:
\begin{equation*}
\begin{array}{r@{\;}l@{\;}l}
\fun{sfst}(\el,x)          & \smashedrightarrow{\theta} & \el;\\
\fun{sfst}(\cons{x}{s},x)  & \smashedrightarrow{\iota}  & s;\\
\fun{sfst}(\cons{y}{s},x)  & \smashedrightarrow{\kappa} &
\cons{y}{\fun{sfst}(s,x)}.
\end{array}
\end{equation*}
The item is absent in~\(\theta\), the item is found in~\(\iota\) and
the search continues with~\(\kappa\). Note that equality is implicitly
meant in non\hyp{}linear rules like~\(\iota\); in other words, the
cost of such equality test is~\(0\) in our model. Also remark how
important is for rule~\(\iota\) to be written before~\(\kappa\),
otherwise \(\iota\)~would be useless (so\hyp{}called \emph{dead
  code}\index{functional language!dead code}). Here is an
example\index{stack!skipping an item!example} of a successful search:
\begin{equation*}
\fun{sfst}([3,0,1,2],1) \xrightarrow{\smash{\kappa}}
\cons{3}{\fun{sfst}([0,1,2],1)} \xrightarrow{\smash{\kappa}}
\cons{3,0}{\fun{sfst}([1,2],1)} \xrightarrow{\smash{\iota}} [3,0,2].
\end{equation*}
Now an example of an unsuccessful\index{stack!skipping an
  item!example} search:
\begin{equation*}
\fun{sfst}([3,0],4) \xrightarrow{\smash{\kappa}}
\cons{3}{\fun{sfst}([0],4)} \xrightarrow{\smash{\kappa}}
\cons{3,0}{\fun{sfst}(\el,4)} \xrightarrow{\smash{\theta}}
[3,0].
\end{equation*}
More complicated examples, given \vpageref{sfst_ex}, yield
\begin{equation*}
\begin{array}{r@{\;}l@{\;}l}
\fun{sfst}([4,[1,2],\el,\el,4],\el)
& \smashedrightarrow{\kappa} &
  [4|\fun{sfst}([[1,2],\el,\el,4],\el)]\\
& \smashedrightarrow{\kappa} &
  [4|[[1,2]|\fun{sfst}([\el,\el,4],\el)]]\\
& = & [4,[1,2]|\fun{sfst}([\el,\el,4],\el)]\\
& \smashedrightarrow{\iota} & [4,[1,2]|[\el,4]]\\
& = & [4,[1,2],\el,4].
\end{array}
\end{equation*}
\begin{equation*}
\begin{array}{r@{\;}l@{\;}l}
\fun{sfst}([3,\el],[5,2])
& \smashedrightarrow{\kappa} & [3|\fun{sfst}([\el],[5,2])]\\
& \smashedrightarrow{\kappa} & [3|[\el|\fun{sfst}(\el,[5,2])]]\\
& = & [3,\el|\fun{sfst}(\el,[5,2])]\\
& \smashedrightarrow{\theta} & [3,\el|\el]\\
& = & [3,\el].
\end{array}
\end{equation*}
Once we are convinced that our definition is correct\index{soundness}
and complete with respect to the specification, there is a little
extra worth testing: we can check \emph{what happens for inputs which
  are not expected by the specification.} Our specification says at
one point that the second argument of \fun{sfst/2} is a stack. What
happens if we supply an integer instead? For example, we have
\(\fun{sfst}(3,\el) \nrightarrow\). We have a match failure, that is,
the rewrites are stuck, so that our definition is not
\emph{robust},\index{robustness} in other words, it fails abruptly on
unspecified inputs.

When programming in the small, as we do here, robustness is usually
not a concern because we want to focus on learning a language by
expressing simple algorithms, but when developing large applications,
we must take care of making the code robust by catching and signalling
errors. Notice that a program can be complete\index{completeness} but
not robust,\index{robustness} because completeness is relative to
what is specified behaviour (all valid inputs must be accepted and not
lead to an error), whereas robustness is relative to what is left
unspecified.

These considerations are germane to discussing the merits and
weaknesses of scripting languages, whose semantics try hard to ignore
errors by defaulting on special values (like the empty string) to keep
running. In the setting of our abstract functional language, we can
use a data constructor, that is, a function without evaluation rules,
like \(\fun{error}()\), to signal an error or notify some piece of
information about the arguments. For instance, here is the definition
of a function which distinguishes between stacks and non\hyp{}stack
arguments:
\begin{equation*}
\begin{array}{r@{\;}l@{\;}l}
\fun{is\_a\_stack}(\el) & \rightarrow & \fun{yes}();\\
\fun{is\_a\_stack}(\cons{x}{s}) & \rightarrow & \fun{yes}();\\
\fun{is\_a\_stack}(s) & \rightarrow & \fun{no}().
\end{array}
\end{equation*}
Data constructors come handy in signalling errors because they are
like unique identifiers, therefore they cannot be confused with any
other kind of data the function computes and so can be detected easily
by the caller. Consider this robust\index{robustness} version of
\fun{sfst/2} which discriminates errors:
\begin{equation*}
\begin{array}{r@{\;}l@{\;}l}
\fun{sfst}(\el, x) & \rightarrow & \el;\\
\fun{sfst}(\cons{x}{s},x) & \rightarrow & s;\\
\fun{sfst}(\cons{y}{s},x) & \rightarrow & \cons{y}{\fun{sfst}(s,x)};\\
\fun{sfst}(s,x) & \rightarrow & \underline{\fun{error}()}.
\end{array}
\end{equation*}
Then a function calling \fun{sfst/2} can make the difference between a
normal rewrite and an error by using a data constructor in a pattern:
\begin{equation*}
\begin{array}{r@{\;}l@{\;}l}
\fun{caller}(s,x) & \rightarrow & \fun{check}(\fun{sfst}(s,x)).\\
\fun{check}(\fun{error}()) & \rightarrow & \fbcode{CCCCC}\,;\\
\fun{check}(r) & \rightarrow & \fbcode{CCCCC}\,.
\end{array}
\end{equation*}
%%% END

\paragraph{Cost}

In general, the cost
\(\C{\fun{sfst}}{n}\)\index{sfst@$\C{\fun{sfst}}{n}$|(} of
\(\fun{sfst}(s,x)\), where \(n\)~is the length of~\(s\), depends
on~\(x\) being present in~\(s\) or not. In the latter case, the trace
is \(\kappa^n\theta\), so \(\C{\fun{sfst}}{n} = \len{\kappa^n\theta} =
n + 1\). If the former, the cost depends on the position of~\(x\)
in~\(s\). Let us set that the top of~\(s\) is at position~\(0\) and
\(x\)~occurs at position~\(j\). We then have \(\C{\fun{sfst}}{n,j} =
\len{\kappa^j\iota} = j + 1\)\index{sfst@$\C{\fun{sfst}}{n}$|)}. If we
decide that position~\(n\) (or greater) means absence, we can actually
retain the last formula for both cases.

The minimum cost \(\B{\fun{sfst}}{n}\)\index{sfst@$\B{\fun{sfst}}{n}$}
is then the minimum\index{stack!skipping an item!minimum cost} value
of \(\C{\fun{sfst}}{n,j}\), for \(j\)~ranging from \(0\)~to~\(n\),
therefore \(\B{\fun{sfst}}{n} = \C{\fun{sfst}}{n,0} = 1\), that is,
when the item occurs at the top, and, dually, the maximum
cost\index{stack!skipping an item!maximum cost}
is\index{sfst@$\W{\fun{sfst}}{n}$} \(\W{\fun{sfst}}{n} =
\C{\fun{sfst}}{n,n} = n + 1\), that is, when the item is absent. The
average cost
\(\M{\fun{sfst}}{n}\)\index{sfst@$\M{\fun{sfst}}{n}$}\index{stack!skipping
  an item!average cost} of a successful search assumes that \(j\)~can
take all the positions in the stack:
\begin{equation*}
  \M{\fun{sfst}}{n} = \frac{1}{n}\sum_{j=0}^{n-1}\C{\fun{sfst}}{n,j} =
  \frac{1}{n}\sum_{j=0}^{n-1}(j+1) = \frac{1}{n}\sum_{j=0}^{n}j
  = \frac{n+1}{2} \sim \frac{n}{2},
\end{equation*}
by equation~\eqref{eq:sum_k} \vpageref{eq:sum_k}.

Notice that rule~\(\kappa\) implies the creation of a
(\texttt{|})\hyp{}node, which we call
\emph{cons\hyp{}node}\index{cons-node}, as shown in
\fig~\vref{fig:sfst_dag}.
\begin{figure}
\centering
\includegraphics[bb=71 664 345 721]{sfst_dag}
\caption{Directed acyclic graphs for \fun{sfst/2}
\label{fig:sfst_dag}}
\end{figure}
Hence, whilst the contents of the new stack is shared with the
original stack, \(j\)~nodes are newly allocated if~\(x\) occurs at
position~\(j\) in~\(s\). The worst case happens when \(x\)~is absent
so the memory\index{stack!skipping an item!memory} needed amounts to
\(n\)~nodes, all of which being useless because in this case
\(\fun{sfst}(s,x) \equiv s\). If we want to avoid this situation,
another definition of \fun{sfst/2} has to be devised, one that
discards all constructed nodes and allows us to reference the input
when~\(x\) is missing.

The crux of the matter is embodied in the construct
\(\cons{y}{\texttt{\textvisiblespace}}\) of rule~\(\kappa\), called
the \emph{context}\index{functional language!context of a call} of the
call \(\fun{sfst}(s,x)\)\index{sfst@\fun{sfst/2}|)}, which we want
if~\(x\) is present, but not otherwise. To resolve this conflicting
requirement, we opt for removing the context and store the information
it contains (\(y\)) into an accumulator\index{functional
  language!accumulator}, in a new rule~\(\xi\) derived
from~\(\kappa\). We use the accumulator in a new rule~\(\nu\) derived
from~\(\iota\). The new \fun{sfst/2} is called
\fun{sfst\(_0\)/2}\index{sfst0@\fun{sfst\(_0\)/2}} and is shown in
\fig~\ref{fig:sfst0}.\index{sfst@\fun{sfst/4}}
\begin{figure}[b]
\begin{equation*}
\boxed{%
\begin{array}{@{}r@{\;}l@{\;}lr@{\;}l@{\;}l@{}}
\fun{rcat}(\el,t)              & \xrightarrow{\smash{\zeta}} & t; &
\fun{sfst}(\el,x,t,u)          & \xrightarrow{\smash{\mu}} & u;\\
\fun{rcat}(\cons{x}{s},t)      & \xrightarrow{\smash{\eta}}
                               & \fun{rcat}(s,\cons{x}{t}). &
\fun{sfst}(\cons{x}{s},x,t,u)  & \xrightarrow{\smash{\nu}}
                               & \fun{rcat}(t,s);\\
\fun{sfst}_0(s,x)              & \xrightarrow{\smash{\lambda}}
                               & \fun{sfst}(s,x,\el,s). &
\fun{sfst}(\cons{y}{s},x,t,u)  & \xrightarrow{\smash{\xi}}
                               & \fun{sfst}(s,x,\cons{y}{t},u).
\end{array}}
\end{equation*}
\caption{Skipping the first occurrence with maximum sharing
\label{fig:sfst0}}
\end{figure}
Of course, whilst in~\(\iota\) we just referenced~\(s\), the
construction corresponding to the now missing context of~\(\kappa\)
must be performed by~\(\nu\). Also, we must add another argument which
refers to the original stack, so we can use it in a new rule~\(\mu\),
the pendant of~\(\theta\). Note the shapes of the right\hyp{}hand
sides: they are either a value~(\(\zeta\) and~\(\mu\)) or a function
call whose arguments contain values. In other words, no function call
has a context.

This syntactic property of a definition is named \emph{tail
  form}\index{functional language!tail form}. Intuitively, the
practical consequence of such a form is that terminating calls unfold
until a value is reached and nothing else is left to be done:
\emph{the value of the last call is the value of the first call}. This
kind of definition enables the sharing in rule~\clause{\mu}, where
\(u\) (the reference to the original stack) becomes the value, instead
of \(\fun{rev}(t)\).

Implementations of functional language often use this property to
optimise the evaluation, as we shall see in the last part of this
book. The downside of \fun{sfst\(_0\)/2} with respect to \fun{sfst/2}
is the additional cost incurred by having to reverse~\(t\) in
rule~\clause{\nu}, that is, the call \(\fun{rcat}(t,s)\). More
precisely, there are two complementary cases: either \(x\)~is missing
in~\(s\) or \(x\)~occurs in~\(s\). Let us assume that \(s\)~contains
\(n\)~items and \(x\)~is absent in~\(s\). The evaluation trace
\index{functional language!evaluation!trace} of the call
\(\fun{sfst}_0(s,x)\) is \(\lambda\xi^n\mu\), so
\begin{equation*}
  \C{\fun{sfst}_0}{n}
= \len{\lambda\xi^n\mu} = \len{\lambda} + n \len{\xi} + \len{\mu} = n
+ 2.
\end{equation*}
Let us now assume that \(x\)~occurs at position \(k\) in~\(s\),
with the first item having position~\(0\). The evaluation trace is
then \(\lambda \xi^{k}\nu\eta^k\zeta\), hence
\begin{equation*}
  \C{\fun{sfst}_0}{n,k} = \len{\lambda \xi^{k}\nu\eta^k\zeta} = 2k + 3.
\end{equation*}
Clearly now,
\begin{align*}
\B{\fun{sfst}_0}{0} & = 2,\\
\B{\fun{sfst}_0}{n} & = \min_{0 \leqslant k < n}\{\C{\fun{sfst}_0}{n}, \C{\fun{sfst}_0}{n,k}\}
                    = \min_{0 \leqslant k < n}\{n+2,2k+3\} = 3,\\
\W{\fun{sfst}_0}{n} & = \max_{0 \leqslant k < n}\{n+2,2k+3\} = 2n+1,
\end{align*}
where the minimum cost occurs when the item is the top of the stack;
the maximum cost happens when the sought item is last in the stack (at
the bottom).

Since calling \fun{rcat/2}\index{rcat@\fun{rcat/2}} to reverse the
visited items is the source of the extra cost, we might try to
maintain the order of these items in \fig~\vref{fig:sfst1}\index{sfst1@\fun{sfst\(_1\)/1}}
\begin{figure}[b]
\begin{equation*}
\boxed{%
\begin{array}{r@{\;}l@{\;}l}
\fun{sfst}_1(s,x)               & \rightarrow
                                & \fun{sfst}_2(s,x,\el,s).\\
\fun{sfst}_2(\el,x,t,u)         & \rightarrow & u;\\
\fun{sfst}_2(\cons{x}{s},x,t,u) & \rightarrow & \fun{cat}(t,s);\\
\fun{sfst}_2(\cons{y}{s},x,t,u) & \rightarrow
                                & \fun{sfst}_2(s,x,\fun{cat}(t,[y]),u).
\end{array}}
\end{equation*}
\caption{Skipping the first occurrence (bad design)
\label{fig:sfst1}}
\end{figure}
but using stack catenation instead of pushing. The problem is that the
last rule of \fun{sfst\(_2\)/4}\index{sfst@\fun{sfst\(_2\)/4}}
\index{cat@\fun{cat/2}} yields
\(\fun{cat}(\dots\fun{cat}(\fun{cat}(\el,[x_1]),[x_2])\dots)\), whose
cost we know to be quadratic \index{cost!quadratic $\sim$} as in the
rewrite~\eqref{eq:rev0}
\vpageref{eq:rev0}\index{rev0@\fun{rev\(_0\)/1}}, from which we can
quickly conclude that \(\W{\fun{sfst}_1}{n} \sim \tfrac{1}{2}n^2\).

\paragraph{Last occurrence}
\index{stack!skipping an item!last occurrence}

Let us suppose that \(\fun{slst}(s,x)\)\index{slst@\fun{slst/2}}
(\emph{skip the last occurrence}) evaluates in a stack identical
to~\(s\) but without the last occurrence of~\(x\). In particular,
if~\(x\) is absent in~\(s\), then the value of the call is identical
to~\(s\). The first design that may come to mind is to see this
problem as the dual problem of ignoring the first occurrence:
\begin{equation}
\fun{slst}_0(s,x) \xrightarrow{\smash{\pi}}
                  \fun{rev}(\fun{sfst}(\fun{rev}(s),x)).
\label{eq:slst0}
\end{equation}
If~\(x\) is missing in~\(s\), we
have\index{slst0@$\C{\fun{slst}_0}{n}$}\index{rev@$\C{\fun{rev}}{n}$}
\(\C{\fun{slst}_0}{n} = 1 + \C{\fun{rev}}{n} + \W{\fun{sfst}}{n} +
\C{\fun{rev}}{n} = 3n+6\). If~\(x\) occurs in~\(s\) at position~\(k\),
\(\C{\fun{slst}_0}{n,k} = 1 + \C{\fun{rev}}{n} +
\C{\fun{sfst}}{n,n-k-1} + \C{\fun{rev}}{n-1} = 3n - k +
4\). Therefore, we can derive the minimum and
maximum\index{stack!skipping an item!minimum
  cost}\index{stack!skipping an item!maximum cost} costs:
\begin{equation*}
\abovedisplayskip=0pt
\belowdisplayskip=0pt
\B{\fun{slst}_0}{n} = \min_{k < n}\{3n+6,3n-k+4\} = 2n + 5,
\end{equation*}
when\index{slst@$\B{\fun{slst}_0}{n}$} \(x\)~is last in~\(s\), and
\begin{equation*}
\abovedisplayskip=0pt
\belowdisplayskip=0pt
\W{\fun{slst}_0}{n} = \max_{k < n}\{3n+6,3n-k+4\} = 3n+6,
\end{equation*}
when\index{slst@$\W{\fun{slst}_0}{n}$} \(x\)~is missing in~\(s\).  The
mean cost\index{stack!skipping an item!average
  cost}\index{slst@$\M{\fun{slst}_0}{n}$} when \(x\)~is present is
\begin{equation*}
\abovedisplayskip=0pt
\belowdisplayskip=0pt
\M{\fun{slst}_0}{n} = \frac{1}{n}\sum_{k=0}^{n-1}\C{\fun{slst}_0}{n,k}
                    = \frac{1}{n}\sum_{k=0}^{n-1}(3n - k + 4)
                    = \frac{5n+9}{2} \sim \frac{5}{2}n.
\end{equation*}
When \(x\)~is present, the worst case is when it is the top of the
stack: \(\W{\fun{slst}_0}{n} = \max_{k<n}\{3n - k + 4\} = 3 n+ 4
\leqslant 3n + 6\).

In any case, the maximum cost is asymptotically equivalent to~\(3n\),
that is, three complete traversals of~\(s\) are performed, whilst the
absence of~\(x\) could be detected with one. Dually, the minimum cost
is asymptotically equivalent to~\(2n\), accounting for two full
traversals, whilst \(x\)~being the last item could be assessed with
one. All this suggests that a better design is worth thinking about.

Consider \fig~\ref{fig:slst},
\begin{figure}[b]
\begin{equation*}
\boxed{%
\begin{array}{@{}r@{\;}l@{\;}lr@{\;}l@{\;}l@{}}
  \fun{slst}(\el,x)           & \xrightarrow{\smash{\rho}} & \el;
& \fun{slst}(\el,x,t)         & \xrightarrow{\smash{\upsilon}} & t;\\
  \fun{slst}(\cons{x}{s},x)   & \xrightarrow{\smash{\sigma}} &
                                           \fun{slst}(s,x,s);
& \fun{slst}(\cons{x}{s},x,t) & \xrightarrow{\smash{\phi}} &
                                           \cons{x}{\fun{slst}(t,x)};\\
  \fun{slst}(\cons{y}{s},x)   & \xrightarrow{\smash{\tau}} &
                                           \cons{y}{\fun{slst}(s,x)}.
& \fun{slst}(\cons{y}{s},x,t) & \xrightarrow{\smash{\chi}} &
                                           \fun{slst}(s,x,t).
\end{array}}
\end{equation*}
\caption{Skipping the last occurrence with \fun{slst/2}
\label{fig:slst}\index{stack!skipping an item!last occurrence}}
\end{figure}
where, with a linear search\index{linear search} (rules
\(\rho\)~and~\(\tau\)), we find the first occurrence of~\(x\)
(rule~\(\sigma\)), but, in order to check whether it is also the last,
another linear search has to be run~(\(\chi\)). If it is successful
(\(\phi\)), we retain the occurrence find earlier (\(x\)) and resume
another search; if it is unsuccessful~(\(\upsilon\)), the~\(x\) found
earlier was indeed the last occurrence.  Notice how we have two
mutually recursive functions, \fun{slst/2}\index{slst@\fun{slst/2}}
and~\fun{slst/3}\index{slst@\fun{slst/3}}. The definition of the
latter features a third parameter, \(t\), which is a copy of the
stack~\(s\) when an occurrence of~\(x\) was found
by~\fun{slst/2}\index{slst@\fun{slst/2}} (\(\sigma\)). This copy is
used to resume~(\(\phi\)) the search from where the previous
occurrence was found. This is necessary as \(y\) in rule~\(\chi\) must
be discarded because we do not know at that point whether the
previous~\(x\) was the last. Consider \fig~\vref{fig:slst_27071}.
\begin{figure}
\begin{equation*}
\boxed{%
\begin{array}{r@{\;}l@{\;}l}
\fun{slst}([2,7,0,7,1],7)
& \xrightarrow{\smash{\tau}}
& \cons{2}{\fun{slst}([7,0,7,1],7)}\\
& \xrightarrow{\smash{\sigma}}
& \cons{2}{\fun{slst}([0,7,1],7,[0,7,1])}\\
& \xrightarrow{\smash{\chi}}
& \cons{2}{\fun{slst}([7,1],7,[0,7,1])}\\
& \xrightarrow{\smash{\phi}}
& \cons{2,7}{\fun{slst}([0,7,1],7)}\\
& \xrightarrow{\smash{\tau}}
& \cons{2,7,0}{\fun{slst}([7,1],7)}\\
& \xrightarrow{\smash{\sigma}}
& \cons{2,7,0}{\fun{slst}([1],7,[1])}\\
& \xrightarrow{\smash{\chi}}
& \cons{2,7,0}{\fun{slst}(\el,7,[1])}\\
& \xrightarrow{\smash{\upsilon}}
& \cons{2,7,0}{[1]} = [2,7,0,1].
\end{array}}
\end{equation*}
\caption{\(\fun{slst}([2,7,0,7,1],7) \twoheadrightarrow
  [2,7,0,1]\)\label{fig:slst_27071}\index{stack!skipping an item!example}}
\end{figure}
If the item is missing, the linear search fails as usual with a cost
of \(\len{\tau^n\rho} = n + 1\). Otherwise, let us name \( 0 \leqslant
x_1 < x_2 < \dots < x_p < n\) the positions of the \(p\)~occurrences
of~\(x\) in~\(s\). The evaluation trace\index{functional
  language!evaluation!trace} is
\begin{equation*}
\abovedisplayskip=0pt
\belowdisplayskip=0pt
\tau^{x_1}
\cdot
\prod_{k=2}^{p}(\sigma\chi^{x_k-x_{k-1}-1})(\phi\tau^{x_k-x_{k-1}-1})
\cdot
(\sigma\chi^{n-x_p-1}\upsilon),
\end{equation*}
whose length is \(x_1 + 2\sum_{k=2}^{p}(x_k-x_{k-1}) + (n - x_p + 1) =
n + x_p - x_1 + 1\). In other words, if the position of the first
occurrence is noted~\(f\) and the position of the last is~\(l\), we
find that\index{slst@$\C{\fun{slst}}{n}$}
\begin{equation*}
\C{\fun{slst}}{n,f,l} = n + l - f + 1.
\end{equation*}
We deduce that the minimum cost\index{stack!skipping an item!minimum
  cost} happens when \(l-f+1 = p\), that is, when all the occurrences
are consecutive, so \(\B{\fun{slst}}{n,p} = n +
p\)\index{slst@$\B{\fun{slst}}{n}$}. The maximum
cost\index{stack!skipping an item!maximum cost} occurs when~\({f=0}\)
and~\({l=n-1}\), that is, when there is at least two occurrences
of~\(x\), one at the top and one at the bottom: \(\W{\fun{slst}}{n} =
2n\)\index{slst@$\W{\fun{slst}}{n}$}. We can check that when the stack
is entirely made of~\(x\), minimum and maximum costs concur
in~\(2n\). The average cost\index{stack!skipping an item!average cost}
\index{slst@$\M{\fun{slst}}{n}$} when \(x\)~is present requires
determining the cost for every possible pair \((f,l)\), with \(0
\leqslant f \leqslant l < n\):
\begin{align*}
\M{\fun{slst}}{n}
  &= \frac{2}{n(n+1)} \sum_{f=0}^{n-1}\sum_{l=f}^{n-1}{\C{\fun{slst}}{n,f,l}}
   = \frac{2}{n(n+1)} \sum_{f=0}^{n-1}\sum_{l=f}^{n-1}{(n+l-f+1)}\\
  &= \frac{2}{n(n+1)} \sum_{f=0}^{n-1}\bigg(\!\!(n-f+1)(n-f)
     + \!\!\sum_{l=0}^{n-f-1}\!\!(l+f)\!\!\bigg)\\
  &= \frac{1}{n(n+1)}\sum_{f=0}^{n-1}(3n + 1 - f)(n-f)\\
  &= \frac{n(3n+1)}{n+1} - \frac{4n+1}{n(n+1)}\sum_{f=0}^{n-1}f
     + \frac{1}{n(n+1)}\sum_{f=0}^{n-1}f^2
  = \frac{4}{3}{n} + \frac{2}{3} \sim \frac{4}{3}n,
\end{align*}
where \(\sum_{f=0}^{n-1}{f} = n(n-1)/2\) is equation~\eqref{eq:sum_k},
\vpageref{eq:sum_k}, and the sum of the successive squares is obtained
as follows. We use the \emph{telescoping} or \emph{difference} method
on the series \((k^3)_{k>0}\). We start with the equality \((k+1)^3 =
k^3 + 3k^2 + 3k + 1\), hence \((k+1)^3 - k^3 = 3k^2 + 3k + 1\). Then
we can sum these differences, whose terms cancel out, leaving the
first and the last:
\begin{align}
  && (1+1)^3 - \boxed{1^3} &= 3 \cdot 1^2 + 3 \cdot 1 + 1\notag\\
+ && (2+1)^3 - 2^3         &= 3 \cdot 2^2 + 3 \cdot 2 + 1\notag\\
+ &&&\;\;\vdots\notag\\
+ && \boxed{(n+1)^3} - n^3 &= 3n^2 + 3n + 1\notag
\intertext{\rule{\linewidth}{0.4pt}}
\Rightarrow
  &&\boxed{(n+1)^3} - \boxed{1^3}
  &= 3 \sum_{k=1}^{n}{k^2} + 3 \sum_{k=1}^{n}{k} + n\notag\\
  && n^3 + 3n^2 + 3n
  &= 3 \sum_{k=1}^{n}{k^2} + 3 \cdot \frac{n(n+1)}{2} + n\notag\\
\Leftrightarrow && \sum_{k=1}^{n}{k^2} &= \frac{n(n+1)(2n+1)}{6}.
\label{eq:sum_of_squares}
\end{align}

\paragraph{Exercises}
\begin{enumerate}

  \item Prove that \(\fun{sfst/2} = \fun{sfst\(_0\)/2}\).
    \index{sfst@\fun{sfst/2}}\index{sfst0@\fun{sfst\(_0\)/2}}

  \item Show that \(\B{\fun{sfst}_0}{n} = 3\), \(\W{\fun{sfst}_0}{n} =
    2n+1\) and \(\M{\fun{sfst}_0}{n} = n+2\) (successful
    search).

  \item Prove \fun{slst/2} = \fun{slst\(_0\)/2}.

  \item Show that, in a worst case to be identified,
    \(\fun{slst}_0(s,x)\) creates \(3n\)~useless nodes if~\(s\)
    contains \(n\)~items. Compare the memory usage of
    \fun{slst\(_0\)/2}\index{slst@\fun{slst\(_0\)/2}} with that of
    \fun{slst/2}\index{slst@\fun{slst/2}}.

\end{enumerate}