\vsssub
\subsubsection{~$S_{is}$: Floe-size dependent scattering and dissipation} \label{sec:IS2}
\vsssub

\opthead{IS2}{\ws}{F. Ardhuin, C. Sevigny, G. Boutin,  D. Dumont,  T. Williams}

The implementation of this scattering term generally
follows the approach of \cite{art:MM06}, to which has been added an estimation of the breakup of the ice
by waves to be able to update a maximum floe size diameter. Finally a creep-based dissipation was also
combined with the scattering.

The scattering source term is defined by the scattering coefficient $\beta_{\mathrm{is},\mathrm{MIZ}}(\theta-\theta')$ which by default is isotropic, but can be given any directional dependency by setting {\code IS2ISOSCAT=FALSE} in namelist {\F SIS2}. The source term is thus,
\begin{equation}
 \frac{S_{\mathrm{is}}(k,\theta)}{\sigma} =  \int_0^{2\pi}\beta_{\mathrm{is},\mathrm{MIZ}}(\theta-\theta') [s_\mathrm{scat}  N(k,\theta')-N(k,\theta)] d\theta'  .
 \end{equation}
where $s_\mathrm{scat}$ is set to 1.0 by default but can be modified by
{\code IS2BACKSCAT} in namelist {\F SIS2}. \\

The determination of scattering coefficients  $\beta_{\mathrm{is},\mathrm{MIZ}}$ is based on the theoretical
reflection coefficient $\alpha_n(\sigma,h)$
for waves with a normal incidence going from a half-plane of open water to a half-plane of ice-covered water with a
constant ice thickness $h$.
Values of $\alpha_n(\sigma,h)$ as computed by \cite{art:KM08}
 (if {\code IS2WIM1=0.}) or by \cite{art:Ben12} (if {\code IS2WIM1=1.})  are tabulated
in the {\code W3SIS2MD} module. Following \cite{art:Dea11}, the broken ice is treated as a series of
such ice-water interfaces. Neglecting multiple reflections, the scattering parameterization defines the attenuation
per unit time as if the ice-covered part of a grid cell was a succession of floes of mean diameter $D_{m}$ with a partial
reflection $\alpha_n(\sigma,h)$ for each floe, giving,
\begin{equation}
\beta_{\mathrm{is},\mathrm{MIZ}}=c_i~c_g \alpha_n(\sigma,h) / D_{m},
 \end{equation}
where $c_i$ is the ice concentration. \\

The estimation of the mean floe diameter $D_m$ is based on an assumed power law for the number of
floes of diameter $D$, taken  proportional to $D^{-\gamma}$. This power law is further assumed to
apply for $D$ ranging from
the minimum $D_{\min}$ and a maximum $D_{\max}$. The average is thus given by
\begin{equation}
D_m=\frac{\gamma}{\gamma -1}\times\frac{D_{\max}^{-\gamma +1}-D_{\min}^{-\gamma +1}}{D_{\max}^{-\gamma}-D_{\min}^{-\gamma}}.
\label{analytic_Dbar}
\end{equation}

\noindent
At present $D_{\min}$ is a user-supplied value. $D_{\max}$ can either be
provided as a forcing field, e.g. from an
ice model or some observations, or, if the namelist parameter {\code IS2BREAK} is set to {\code TRUE},
estimated from
the breaking of the ice by the local wave field. If the namelist parameter {\code IS2DUPATE}  is set to
{\code TRUE}, small values of $D_{\max}$ will persist even if the waves become too small to be able to break the ice to
that size. This is probably the proper model
use when  external forcing/coupling is available (e.g. advection of ice properties in an ice model).
 On the contrary, if {\code IS2DUPDATE} is set to  {\code FALSE}, the value of $D_{\max}$ will be always adjusted to the local sea state,
even if that means increasing $D_{\max}$.\\

Ice breaking by waves of wavelength $\lambda$ is assumed to produce floes of diameter $\lambda / 2$.
In the parametrization, ice breaking occurs if the three following criteria are fulfilled \citep{art:Wea13}:
\begin{enumerate}
\item $\lambda/2 \geq D_{\min} \quad \mathrm{and} \quad  \lambda/2 \leq D_{\max}$
\item $D_{\max}>D_c$, as it exists a critical diameter, which depends on ice properties, below which no flexural failure is possible
\item $\varepsilon>\varepsilon_c$, the strain due to the incoming wave has to
be greater than a defined critical strain
\end{enumerate}
The first criterion is simply checking that the new value of $D_{\max}$ will be larger than $D_{\min}$ and smaller
than the previous value of $D_{\max}$.\\

The second criterion relies on \cite{inc:M86} with the correction specified in \cite{art:Bea18}, where $D_{c}$ is defined as
\begin{equation}
D_{c}=\dfrac{1}{2}\left(\frac{\pi^4 Y^* h^3}{48 \rho g (1 -\nu ^2)}\right)^{1/4}.
\end{equation}

The third criterion corresponds to the flexural strain threshold. The horizontal
strain caused by waves is related to
the curvature of the ice layer, which, in one dimension is
$\varepsilon = 0.5 h \partial ^2 \eta_{\mathrm ice} / \partial x^2$.
The strain variance is given
\begin{equation}
\langle\varepsilon^2\rangle = \left( \frac{h}{2} \right) ^2   \int_{k_1}^{k_2} k_{ice}^4 F(k)dk ,
\label{strain_sign}
\end{equation}
where $h$ is the ice thickness and $k_{ice}$ is the wavenumber $2 \pi /\lambda_{ice}$.
Borrowing from wave breaking
ideas \citep{art:BBY00}, the integration of the curvature variance is limited around
the local wavenumber $k_{\mathrm ice}$.
We also note that we have defined an effective minimum ice thickness $h_{\min}$ so that,
if $h<h_{\min}$, the strain
variance is computed with $h=h_{\min}$ to avoid unbreakable elastic thin ice in the model
that does not correspond to
usual observations. We thus take $D_{\max}$ to be half the wavelength of the shortest
waves for which the following criterion is met
\begin{equation}
F_{break}\, \sqrt{\varepsilon^2} > \frac{\sigma_c}{Y^*} ,
\label{strain_crit}
\end{equation}
where $\sigma_c$ is the ice flexural strength. $F_{break}$ is a factor representing random waves and adjustable with
the {\F SIS2} namelist parameter  {\code IS2BREAKF}. It should in theory depend on the duration for which the ice is forced by the
waves, and, based on the typical maximum value over 500 Rayleigh-distributes waves, was taken to be $F_{break}=3.6$.
$F_{break} \, E_s$ is thus the maximum strain for random waves. \\

Inelastic dissipation was added in this routine, following \cite{art:Wad73}, because it critically
depends on the floe size.  It assumes that the floes deformation is not fully elastic, and that the secondary creep under the wave-induced cyclic causes the dissipation of wave energy into heat.
We use the ice floe law
\begin{equation}
%\left(\frac{d\varepsilon}{dt}\right)_{ij}=\frac{\tau^{n-1}}{B^n}\sigma_{i,j}'
\left(\frac{d\varepsilon}{dt}\right)_{ij}=\frac{\tau^2}{B^3}\sigma_{i,j}',
\end{equation}
$B$ is the floe law  constant and is a function of ice temperature. Using the normalized parameter estimated by \cite{art:Cea98} from
laboratory experiments, $A=10^{11}$,  and  a uniform ice temperature of 270~K  gives a value
of $B=10^7$~s$^{1/3}$. The volumic dissipation rate is
\begin{equation}
%\frac{de}{dt}= | \sigma_{xx}^{n+1}/(2B)^{n} |.
\frac{de}{dt}= | \sigma_{xx}^{4}/(2B)^3 |.
\end{equation}
Also, the cyclic deformation of the ice can require a much larger elastic energy than the gravity potential
energy, but this is only true if the ice is not broken. As a result, working with a wave elevation spectrum $E(k)$
could introduce large changes in $E(k)$ when the ice is broken or reformed. Instead we prefer
to work with an energy spectrum $ R C_g E(k)/C_{g,\mathrm{ice}}$, using the coefficient $R$ introduced by \cite{art:Wad73}, which
is the ratio of elastic to gravity potential energies.
%For monochromatic waves of amplitude $a$, the energy by unit surface of waves in ice is thus
%$R= \frac{1}{2} \rho g a^2 C_g$. It results that, without reflection nor dissipation, the wave variance in ice is such as :
%\begin{equation}
%E_{ice}=\frac{E\,C_{g}}{Cg_{ice}\,R}
%\label{eq:RWad}
%\end{equation}
%Note that if creep is activated (hence if {\code IS2BREAKE} $ > 0$), eq.~\ref{eq:RWad}
%is also applied for the scattering curvature variance computation in $S_{IS2}$.
For unbroken ice  $R$ is
\begin{equation}
R=1+C_R\frac{4Y^*h^3\pi^4}{3\rho g \lambda^4(1-\nu^2)},
\label{R}
\end{equation}
where we have been careful that \cite{art:Wad73} used $2h$ for the  ice thickness,
and $C_R$ is by default set to 1.0 using the namelist parameter {\code IS2BREAKE}, but it can
be set to zero to work with the true elevation spectrum instead. This factor $R$ is also
applied in the calculation of ice breakup by the waves.\\

The inelastic dissipation is linearized as  $S_{\mathrm{ine}}=-\alpha_{\mathrm{ine}} E_{ice}$.
The coefficient $\alpha_{\mathrm{creep}}$ was adapted from the
\cite{art:Wad73} monochromatic formula and is equal to
\begin{equation}
%\alpha_{\mathrm{ine}}=0.05 B h^5 \left(\frac{Y^*}{2B(1-\nu^2)}\right)^{(n+1)} I_n k^{n+1} \frac{C_g^2}{\rho g C_{g_{ice}}R^2} F \int_{k_1}^{k_2} k_{ice}^4 E(k)dk
\alpha_{\mathrm{ine}}=0.05 B h^5 \left(\frac{Y^*}{2B(1-\nu^2)}\right)^{4} I_3 k^4 \frac{C_g^2}{\rho g C_{g_{ice}}R^2} F_{\rm broken} \int_{k_1}^{k_2} k_{ice}^4 E(k)dk,
\label{eq:alpha_creep}
\end{equation}
where $I_3=\frac{1}{\pi} \int_0^\pi \sin^{4}\beta d\beta$. Details of the computation are given in \cite{art:Bea18}.
$F_{\rm broken}$  is a heuristic smooth transition from unbroken to broken ice, so that the dissipation
gradually goes to zero for waves much longer than the floe sizes, because in that case the ice does
not deform and produces no dissipation of wave energy,
\begin{equation}
F_{\rm broken}=\tanh \left( {\frac{D_{\max}-C_{\lambda}\lambda_{ice}} {D_{\max}C_{smooth} } }\right) .
\label{smooth}
\end{equation}
Inelastic dissipation is computed after updating $D_{\max}$.
%dissipation is full for
%$\lambda_{ice}\leq D_{\max}$ and disappears for $C_{\lambda}\lambda_{ice}< D_{\max}$.
The two parameters in this smooth transition $C_{\lambda}$ and $C_{smooth}$ are
set to  $0.4$ and $0.2$ by the adjustable namelist parameters {\code IS2CREEPD} and {\code IS2CREEPC}.\\

Possibility of subsituting this inelastic dissipation by an anelastic dissipation term has also been added. It is activated by setting the namelist parameter {\code IS2ANDISB} to {\code TRUE}.
Anelastic dissipation corresponds to the energy dissipated into heat during the oscillatory motion of the dislocations induced by the cyclic stress associated to waves.  This behaviour results in a hysteresis that can be seen in stress-strain diagrams when sea ice is submitted to a sinusoidal stress as presented in Fig.~4 of \cite{art:Cea98} study. This latter article also suggests a model for the anelastic behaviour of sea ice, with a stress-strain relationship that enables to compute the area within the ellipse which results from the hysteresis. Similarly to what has been done for inelastic dissipation, anelastic dissipation is linearized as  $S_{\mathrm{ane}}=-\alpha_{\mathrm{ane}} E_{ice}$.
\cite{art:Bea18} have derived  $\alpha_{\mathrm{ane}}$  from \cite{art:Cea98} model for a monochromatic stress. They obtained the following coefficient:
\begin{equation}
\alpha_{\mathrm{ane}}= \dfrac{A}{6}\left(k_i^{2}\dfrac{Y^*}{(1-\nu^{2})\rho gG}\right)^{2}h^{3}\frac{C_g}{GC_{g,i}} F_{\rm broken},
\label{eq:alpha_ane}
\end{equation}
where  $F_{\rm broken}$ is the same as for inelastic dissipation and $A$ is equal to:
\begin{equation}
A=\dfrac{4}{3}\sigma \alpha_{d}~\delta D^{d}~\dfrac{1}{\exp(\alpha_{d}s)+\exp(-\alpha_{d}s)},
\label{eq:A_ane}
\end{equation}
in which terms are detailed in the table at the end of this section.\\

Finally we recall the various model parameters used in IS2 in the following table. Some are defined
as constants in the {\code W3IS2MD} module, others can be adjusted with the {\F SIS2} namelist.

{\centering
\begin{tabular}{l  c c  c}
Parameters                   & Symbol        &  namelist parameter &  default values \\
\hline
Minimum floe size            & $D_{\min}$    & N. A. & 20~m \\
Initial floe size            & $D_{init}$    & N. A. & 1000~m \\
Ice fragility                & $\xi $        & N. A. & $0.9$ \\
Ice density                  & $\rho_{ice}$  & N. A. & 922.5~kg~m$^{-3}$ \\
Effective Young Modulus      & $Y^{*}$       & N. A. & 5.49~GPa \\
Poisson Coefficient          & $\nu$         & N. A. & 0.3 \\
Flexural strength            &  $\sigma_c$   & N. A. & 0.27~MPa \\
Flow law parameter           &$n$            & {\code IS2CREEPN} & 3\\
Flow law parameter           &$B$            & {\code IS2CREEPB} &  10$^7$~s$^{1/3}$\\
Elastic energy correction    & $C_R$         & {\code IS2BREAKE} & 1.0 \\
Relax. of disloc. compliance & $\delta D^{d}$& {\code IS2ANDISD} & $\Delta_d \Omega b^2/K$ (Pa$^{-1}$)\\
Dislocation density          & $\Delta_d$    & N. A. & 1.8$\times10^{9}$ (m$^{-2}$)\\
Restoring stress term        & $K$           & N. A. & 0.07 (Pa) \\
Orientation factor           & $\Omega$      & N. A. & $\pi^{-1}$ \\
Burgers vector               & $b$           & N. A. & 4.52$\times10^{-10}$ (m) \\
Drag term                    & $B$           & N. A. & $B_0 \exp(Q_v/(k_bT_K)$ (Pa.s)\\
 -                           & $B_0$         & N. A. & 1.205$\times10^{-9}$ (Pa.s)\\
 Boltzmann constant          & $k_B$         & N. A. & 8.617$\times10^{-5}$ (eV.K$^{-1}$) \\
Activation energy            & $Q_v$         & {\code IS2ANDISE} & 0.55 (eV) \\
 Peak broadening term        & $\alpha_{d}$  & N. A. & 0.54 \\
 Reduced variable            & $s$           & N. A. & $\log(\tau\omega)$ \\
 Relax. time of disloc.      & $\tau$        & N. A. & $B/K$ (s) \\
 Temperature of sea ice      & $T_K$         & N. A. & $268.15$ (K)\\
\hline
\end{tabular}
}


\documentclass{article}
\usepackage{amsfonts, amsbsy, amssymb, amsmath, graphicx, float, subfigure}

\tolerance=5000
\textwidth=16.6 cm
\oddsidemargin=-.04 cm
\evensidemargin=-.04 cm
\topmargin=-1.3 cm
\textheight=22.9 cm

\newtheorem{definition}{Definition}
\newtheorem{assumption}{Assumption}
\newtheorem{hyp}{Hypothesis}
\newtheorem{theorem}{Theorem}
\newtheorem{lemma}{Lemma}[section]
\newtheorem{corollary}{Corollary}[section]
\newtheorem{proposition}{Proposition}[section]

\begin{document}

\section*{One Degree-of-Freedom (DoF) Hamiltonian Bifurcation of Equilibria}

We will now consider two examples of bifurcation of equilibria in two dimensional Hamiltonian system; in particular, the Hamiltonian saddle-node and Hamiltonian pitchfork bifurcations.

\subsection*{Hamiltonian saddle-node bifurcation}

We consider the Hamiltonian:

\begin{equation}
H (q, p) = \frac{p^2}{2} - \lambda q + \frac{q^3}{3}, \quad (q, p) \in \mathbb{R}^2.
\label{eq:hamApp13}
\end{equation}

\noindent
where $\lambda$ is considered to be a parameter that can be varied. From this Hamiltonian, we derive Hamilton's equations:

\begin{eqnarray}
\dot{q} & = & \frac{\partial H}{\partial p} = p, \nonumber \\
\dot{p} & = & -\frac{\partial H}{\partial q} =\lambda - q^2.
\label{eq:hamApp14}
\end{eqnarray}

\noindent
The fixed points for \eqref{eq:hamApp14} are:

\begin{equation}
(q, p) = (\pm\sqrt{\lambda}, 0),
\end{equation}

\noindent
from which it follows that there are no fixed points for $\lambda <0$, one fixed point for $\lambda =0$, and  two fixed points for $\lambda >0$. This is the scenario for a saddle-node bifurcation.

Next we examine stability of the fixed points. The Jacobian of \eqref{eq:hamApp14} is given by:

\begin{equation}
\left(
\begin{array}{cc}
0 & 1\\
-2 q & 0
\end{array}
\right).
\label{eq:hamApp15}
\end{equation}

\noindent
The eigenvalues of this matrix are:

\[
\lambda_{1, 2} = \pm \sqrt{-2q}.
\]

\noindent
Hence $(q, p) = (-\sqrt{\lambda}, 0)$ is a saddle, $(q, p) = (\sqrt{\lambda}, 0)$ is a center, and $(q, p) = (0, 0)$  has two zero eigenvalues. The phase portraits are shown in Fig. \ref{fig:appC_fig3}.

 \begin{figure}[htb!]
\begin{center}
\includegraphics[width=0.6\columnwidth]{ham_sn.png}
\end{center}
\caption{The phase portraits for the Hamiltonian saddle-node bifurcation.}
\label{fig:appC_fig3}
\end{figure}

\subsection*{Hamiltonian pitchfork bifurcation}

We consider the Hamiltonian:

\begin{equation}
H (q, p) = \frac{p^2}{2} - \lambda \frac{q^2}{2} + \frac{q^4}{4},
\label{eq:hamApp16}
\end{equation}

\noindent
where $\lambda$ is considered to be a parameter that can be varied. From this Hamiltonian, we derive Hamilton's equations:

\begin{eqnarray}
\dot{q} & = & \frac{\partial H}{\partial p} = p, \nonumber \\
\dot{p} & = & -\frac{\partial H}{\partial q} =\lambda q  -  q^3.
\label{eq:hamApp17}
\end{eqnarray}

\noindent
The fixed points for \eqref{eq:hamApp17} are:

\begin{equation}
(q, p) = (0, 0), \, (\pm\sqrt{\lambda}, 0),
\end{equation}

\noindent
from which it follows that there is one fixed point  for $\lambda <0$, one fixed point for $\lambda =0$, and  three fixed points for $\lambda >0$. This is the scenario for a pitchfork  bifurcation.

Next we examine stability of the fixed points. The Jacobian of \eqref{eq:hamApp17} is given by:

\begin{equation}
\left(
\begin{array}{cc}
0 & 1\\
\lambda-3q^2 & 0
\end{array}
\right).
\label{eq:hamApp18}
\end{equation}

\noindent
The eigenvalues of this matrix are:

\[
\lambda_{1, 2} = \pm \sqrt{\lambda-3q^2}.
\]

\noindent
Hence $(q, p) = (0, 0)$ is a center for $\lambda <0$, a saddle for $\lambda >0$ and  has two zero eigenvalues for $\lambda =0$. The fixed points $(q, p) = (\sqrt{\lambda}, 0)$ are centers for $\lambda >0$. The phase portraits are shown in Fig. \ref{fig:appC_fig4}.

 \begin{figure}[htb!]
\begin{center}
\includegraphics[width=0.6\columnwidth]{ham_pitch.png}
\end{center}
\caption{The phase portraits for the Hamiltonian pitchfork bifurcation.}
\label{fig:appC_fig4}
\end{figure}

\end{document}


\section{Simple harmonic oscillators}

\subsection{Single oscillator}
Let the canonical hamiltonian be
\begin{equation}
H = \frac{p^2}{2m} + \frac{1}{2}m\Omega^2 x^2.
\end{equation}

\begin{equation}
\widehat{H} = \Omega\rbr{\widehat{a}^+\widehat{a}^- + \frac{1}{2}}.
\end{equation}

\begin{nameddef}{Quantum ground state}
\begin{equation}
\widehat{a}^-\Ket{0} = 0;
\end{equation}

\begin{equation}
\Braket{x | 0} = N_0 \ee^{-m\Omega x^2/2}.
\end{equation}

\begin{equation}
N_0 = \rbr{\frac{m\Omega}{\pp}}^{1/4}.
\end{equation}

\begin{equation}
\Braket{x | 0} = \rbr{\frac{m\Omega}{\pp}}^{1/4} \ee^{-m\Omega x^2/2}.
\end{equation}

\end{nameddef} % Quantum ground state

\begin{nameddef}{Time evolution}

\end{nameddef} % Time evolution

\subsection{Double oscillators with identical frequency}

\begin{align}
H &= H_1 + H_2, \\
H_i &= \frac{p_i^2}{2m} + \frac{1}{2}m\Omega^2 x_i^2,\qquad i = 1, 2.
\end{align}

\begin{nameddef}{Quantum ground state}
\begin{equation}
\widehat{a}_i^-\Ket{0} = 0;
\end{equation}

\begin{equation}
\Braket{x_1, x_2 | 0} = \rbr{\frac{m\Omega}{\pp}}^{1/2}
\cfun{\exp}{-\frac{m\Omega}{2}  \rbr{x_1^2+x_2^2}}.
\end{equation}

\end{nameddef} % Quantum ground state

\begin{nameddef}{Time evolution}

\end{nameddef} % Time evolution


\subsection{Estimating the SVC using quadratic equations}


\documentclass{article}
\usepackage{graphicx}
\usepackage{amsfonts}
\usepackage{tikz}
\usepackage[lined,boxed,commentsnumbered]{algorithm2e}

\begin{document}

\title{CSCI 232 Project: \\ Designing and Implementing a Graph Coloring Algorithm in Java}
\author{Gustavo H. Barrionuevo}

\maketitle

\section{Graph Coloring Problem}

It is often necessary to partition the vertices of a graph into sets of independent vertices. Suppose that we want to split a group into subgroups which contain only compatible elements, for example, consider the following graph:
\begin{center}
\begin{tikzpicture}
  [scale=.8,auto=left,every node/.style={circle,draw=black,color=black, fill=white}]
  \node (a) at (10,10) {a};
  \node (b) at (8,8)  {b};
  \node (c) at (10,8)  {c};
  \node (d) at (12,8) {d};
  \node (e) at (9,6)  {e};
  \node (f) at (11,6)  {f};

  \foreach \from/\to in {a/b,a/d,b/c,b/e,b/f,c/d,d/e,e/f}
    \draw (\from) -- (\to);

\end{tikzpicture}
\end{center}
A possible partition of independent sets of vertices is: $\{ a \}$, $\{ b, d \}$, $ \{ c, f \}$,$ \{ e \}$. It is clear that is not the best solution. An optimal solution would be: $\{ a, c, e \} $, $ \{ b, d \}$, $ \{  f \}$. In Graph Theory, this problem is showed as a \emph{Graph Coloring Problem} (GCP)\cite{chartrand2008chromatic}.

The formal definition for the GCP is: given a undirected graph $G = (V, E)$, with a set of vertices $V$ and a set of edges $E$, which are incident to the vertices of $G$, the problem is to assign $k$ colors for each vertice in $G$, so that no two adjacent vertices have the same color. So, the \emph{coloring} can be considered as a function $c : V(G) \to \mathbb{N}$ (where $\mathbb{N}$ is the set of positive integers) such that $c(u) \neq c(v)$ where $u$ and $v$ are adjacent in $G$\cite{chartrand2008chromatic}. If $k$ is the minimum number of colors necessary to color the graph, we say that $k$ is the chromatic number of $G$, and it is denoted by $\chi(G)$. Therefore, a graph is $k$-colorable if and only if $\chi(G) \leq k$.

The GCP can be formulated in two ways. First as a decision problem, where the goal is to identify whether a graph is $k$-colorable. In the second formulation the goal is to find a value $k$ to be minimal.

The study of the GCP has a great theoretical importance because it is a NP-complete problem\cite{MR519066}. Solving this class of problems considered intractable motivates various research in computing, mathematics and operations research.

From a practical standpoint, the study of the GCP is very important because its structure is widely used to model problems in such diverse areas as: scheduling\cite{Lotfi198627}, optimization of register allocation\cite{Chow}, train platforming\cite{Caprara2007129}, frequency assignment\cite{1623376}, communication networks\cite{120165}, and many others.

\section{Analysis and Solution Design}

\subsection{A Backtracking Algorithm for the k-Coloring Problem}

One way to solve GCP would be to generate all possible colorings of the graph. Although this approach will find a solution eventually (if one exists), it is not fast. In this case, for $|V|$ vertices and $k$ colors, there are $k^{|V|}$ different coloring, which means that this brute-force method is feasible just for small numbers of vertices. However, if while generation the possible colorings of the graph we can often avoid  having to check all these colorings. This is exactly what a backtracking algorithm does.

Backtracking is a modified depth-first search of a tree.  A path from the root to a leaf is a candidate solution. This tree is called a \emph{state space tree}. To determine the solutions, we check each candidate solution in sequence. But it does not take advantage of any signs along the way. We can make the search more efficient by looking for such signs. For example, no two adjacent vertices are the same color. This sign tells us that this node can lead to nothing but dead ends. So, after determining that a node can lead to nothing but dead ends, we go back (``backtrack'') to the node's parent and proceed with the search on the next child. This procedure is caller \emph{pruning} the state space tree.  We call a node \emph{nonpromising} if when visiting the node we determine that  it cannot possibly lead to a solution. Otherwise, we call it \emph{promising}.
A backtracking algorithm to solve the instance of the GCP is as follows:
\begin{algorithm}[H]
\DontPrintSemicolon % Some LaTeX compilers require you to use \dontprintsemicolon instead
\KwIn{$G=(V,E)$ (an undirected graph). Let $S = $ set of vertices that have already been colored together with their color. $S$ is initially empty.}
\KwOut{An assignment of one of $k$ colors to each vertex of $G$}
\eIf{$S = V$} {
    return S\;
} {
 	pick a vertex $v$ not in S\;

\For{$C \gets 1$ \textbf{to} $k$}{
	 // verify if some neighbor of $v$ is colored with color $C$ \;
		\If{promising($v$)}	{
    add $v$ to S with color $C$\;
    $k\_coloring(G,S)$\;
	}
		}
}
\caption{{\texttt{k\_coloring}:} a backtracking solution to the graph coloring problem.}
\label{algo:coloring}
\end{algorithm}

\subsection{Complexity}

For a rough analysis of \texttt{k\_coloring}, consider a less efficient version that does not perform pruning and instead checks the validity of a coloring only when a coloring
is complete. In the worst case, the algorithm must check the validity of every possible node in the state space tree, which is equal to:
\begin{equation}
1 + k + k^2 + \dots + k^{|V|} = \frac{k^{|V|+1}-1}{k-1}
\end{equation}
Thus the running time for this algorithm is bounded above by $O(k^{|V|})$.

Although backtracking algorithms for problems such as the GCP are still exponential-time in the worst case, they are useful because they can be efficient for a particular large instance.

\section{Algorithm Behaviour: Experimental Analysis}

\subsection{Experiment Setup and Procedure}

Among the possibilities of instances that could be used to analyze the performance of the algorithms, the following types of graphs were chosen and listed below, based on 10 runs in each set, totaling 900 different instances of graphs.

\begin{itemize}

\item DENSE:  30 randomly generated graphs of which the number of vertices varies uniformly in the range $ n \in [ 100 , 3000 ] $ with a probability of connection between vertices defined in $70\%$ . These graphs are part of the set of instances testing for presenting a high value for $\chi(G)$, and therefore are the ones that require more computational time to be colored. In this scenario we analyzed 300 graphs.

\item LARGE: 30 randomly generated graphs of which the number of vertices is fixed at $n = 1000$ and its density varies uniformly between $1\%$ and $99\%$. With this set of instances, we intend to analyze the influence of $\chi(G)$ variation on the computational time of the studied algorithm. In this scenario , we analyzed 300 graphs.

\item REGULAR: 30 graphs $k$-regular randomly generated  with the fixed number of vertices in $n = 1000$ and the value of $k$ varying uniformly between 30 and 999. With this set of instances, it is intended to determine whether the high symmetry in this graph $k$-regular can influence the computational time. In this scenario, we analyzed 300 graphs.

\end{itemize}

It is believed that the selected instances covers all the important features to be studied in this text. Other types of graphs such as  planar or  bipartite graphs were not considered.

\subsection{Data Analysis}

In all the graphs to be displayed, each point on the graph represents the average of 10 runs of the algorithm studied.

\begin{figure}[!htb]
\label{fig:dense}
{\centering \includegraphics[width=1\textwidth]{dense}
\caption{Running time of dense graphs with probability of connectedness $p=0.70$.}
}
\end{figure}

\begin{figure}[!htb]
\label{fig:large}
{\centering \includegraphics[width=1\textwidth]{large}
\caption{Running time of large graphs with 1000 vertices.}
}
\end{figure}

\begin{figure}[!htb]
\label{fig:regular}
{\centering \includegraphics[width=1\textwidth]{regular}
\caption{Running time of regular graphs with 1000 vertices.}
}
\end{figure}

\bibliographystyle{ieeetr}
\bibliography{refs}

\end{document}


% !TEX root = ../design.tex
\chapter[SVM]{SVM}

\newcommand{\Pcal}{{\cal P}}
\newcommand{\Ncal}{{\cal N}}

\begin{moduleinfo}
\item[Authors] {}
\item[History]
    \begin{modulehistory}

        \item[v0.1] Initial version
         \end{modulehistory}
\end{moduleinfo}

% Abstract. What is the problem we want to solve?

Support Vector Machines (SVMs) are a commonly used technique for approaching
regression and classification problems. SVM models have two particularly
desirable features: robustness in the presence of noisy data, and applicability
to a variety of data schemes. At its core, a \textit{linear} SVM model is a
hyperplane separating the two distinct classes of data (in the case of
classification problems), in such a way that the \textit{margin} is maximized.
Using kernels, one can approximate a large variety of decision boundaries.

%\section{Linear Methods for Regression} % (fold)
%\label{sub:linear_methods_for_regression}

% subsection linear_methods_for_regression (end)

\section{Linear SVM}\label{sec:linear}

%Cite Smola's book for this section

Suppose we have a classification task with training data
$(x_1,y_1),\ldots,(x_n,y_n) \in \R^d\times \{0,1\}$. A hyperplane in $\R^d$ is
determined by $w\in R^d$ and $b\in R$: $x\in\R^d$ is on the plane if $\langle w,
x\rangle + b = 0$. To solve the classification problem, we seek a hyperplane
which separates the data, and therefore coefficients $(w,b) \in \R^d\times\R$
such that  for all $i\leq n$, $y_i = \text{sgn}(\langle w, x_i\rangle + b)$.\\

The \textit{margin} determined by a hyperplane and the training data is the
smallest distance from a data point to the hyperplane. For convenience, the
length of $w$ can be modified so that the margin is equal to $\frac{1}{||w||}$.
In addition to merely separating the data, it is desirable to find a hyperplane
which maximizes the margin; inuitively, a larger margin will lead to better
predictions on future data.\\

The \textit{support vectors} are the members of the training data whose distance
to hyperplane is exactly the margin; that is, the training points which are
closest to the hyperplane. Learning with SVM lends itself to large datasets in
part because the model in depends only on these support vectors; all other
training data do not influence the final model. When assigning values to new
data, one needs only to check it against the support vectors.\\

Thus, we are left with the following convex programming problem:

\begin{align*}
\underset{w,b}{\text{Minimize }} \frac{1}{2}||w||^2, w\in \R^d, \\
\text{subject to } y_i(\langle w, x_i \rangle + b) \geq 1 \text{ for } i=1\ldots n.
\end{align*}

If the data are not linearly separable, \textit{slack variables} $\xi_i$ are
introduced to allow for some points to be classified incorrectly. We do not want
to sacrifice too much to capture outliers, so we introduce an extra term in the
objective function to control the error. The optimization problem now becomes

\begin{align*}
\underset{w,\vec{\xi},b}{\text{Minimize }} \frac12 ||w||^2 + \frac{C}{n}\sum_{i=1}^n \xi_i \\
\text{subject to } y_i(\langle w, x_i \rangle + b) \geq 1 - \xi_i,,\\
\xi_i\geq 0.
\end{align*}

The parameter $C$ is free, and should be chosen to optimize the model. One can use cross-validation, for example, to optimize the choice of $C$. \\

\subsection{Unconstrained optimization}

One can formulate the same problem without constraints:

\begin{equation}
\underset{w}{\text{Minimize }} \lambda ||w||^2 + \frac{1}{n}\sum_{i=1}^n \ell(y_i,f(x_i))
\end{equation}

where $\ell(y,f(x)) = \max(0,1-yf(x))$ is the \textit{hinge loss}. Also, in the
case of linear SVM, $f(x) = \langle w, x\rangle + b$ (Note: there are a couple
ways to handle the variable $b$; one can include it in the gradient
calculations, or add extra feature to the data space.) Also note that this loss
function is not smooth. However, one can solve this unconstrained convex problem
using the techniques outlined in the ``Convex Programming Framework'' section of
this document. In particular, the subgradient of this loss function which is
used in our gradient-descent method is:
$$
v(y,f(x)) = \left\{
        \begin{array}{ll}
            0 & \quad 1-yf(x) \leq 0 \\
            -y\cdot x & \quad  1-yf(x) > 0
        \end{array}
    \right.
$$

See \cite{ShSS07} (extended version) for details.

% \subsection{$\nu$-SVM}
%Another formulation of the SVM optimization problem is \textit{$\nu$-SVM}.
%
%\begin{align*}
%\underset{w,\vec{\xi},\rho,b}{\text{Minimize }} \frac{1}{2} ||w||^2 -\nu\rho + \frac{1}{n}\sum_{i=1}^n \xi_i \\
%\text{subject to } y_i(\langle w, x_i \rangle + b) \geq \rho - \xi_i,,\\
%\xi_i,\rho\geq 0.
%\end{align*}
%
%The parameters $\nu$ take the place of the parameter $C$ in this formulation. The advantage of this is that, unlike $C$, $\nu$ has a nice interpretation: $\nu$ is a lower bound on the fraction of vectors which are support vectors in the resulting decision boundary. An additional variable $\rho$ is also introduced, which one thinks of as controlling the margin of the data.\\
%
%It is unclear at this point how to convert this to an unconstrained optimization problem.

\subsection{$\epsilon$-Regression}

SVM can also be used to predict the values of an affine function $f(x) = \langle
w, x\rangle+b$, given sample input-output pairs $(x_1,y_1),\ldots,(x_n,y_n)$. If
we allow ourselves an error bound of $\epsilon>0$, and some error controlled by
the slack variables $\xi^*$, it is a matter of simply modifying the above convex
problem. By demanding that our function is relatively ``flat," and that it
approximates the true $f$ reasonably, the relevant optimization problem is:

\begin{align*}
\underset{w,\vec{\xi},\vec{\xi^*_i},b}{\text{Minimize }} & \frac{1}{2}||w||^2 + \frac{C}{n}\sum_{i=1}^n \xi_i + \xi^*_i \\
\text{subject to } & y_i - \langle w, x_i\rangle - b \leq \epsilon + \xi_i,\\
						&  \langle w, x_i\rangle + b - y_i \leq \epsilon + \xi^*_i, \\
						& \xi_i,\xi^*_i \geq 0
\end{align*}

One can also formulate $\epsilon$-regression as an unconstrained optimization
problem just as we did with classification. In fact, the objective function
remains the same, except rather than hinge loss we would  use the loss function

\[ \ell(y,f(x)) = \max(0,|y-f(x)|-\epsilon) \]

whose subgradient is

$$
v(y,f(x)) = \left\{
        \begin{array}{ll}
            x & \quad f(x) - y > \epsilon \\
            -x  & \quad  y - f(x)  > \epsilon \\
            0 & \quad \text{otherwise}
        \end{array}
    \right.
$$

\section{Nonlinear SVM}\label{sec:nonlinear}

One can generalize the above optimization problems using \textit{kernels}. A
kernel is nothing more than a continuous, symmetric, positive-definite function
$k:X\times X \to \R$. This replacement, known as the \textit{kernel trick},
allows one to compute decision boundaries which are more general than
hyperplanes. \\

Two commonly used kernels are polynomial kernels, of the form $k(x,y) = (\langle
x,y\rangle +q)^r$, and Gaussian kernels, $k(x,y) = e^{-\gamma||x-y||^2}$,
$\gamma$ a free parameter.\\

The approach we take to learning with these kernels, following \cite{RR07}, is
to approximate them with linear kernels and then apply the above gradient
descent based algorithm for learning linear kernels. The approximation takes the
following form: we embed the data into a finite dimensional feature space,
$z:\R^d\to \R^D$, so that the inner product between transformed points
approximates the kernel:

\[ k(x,y) \approx \langle z(x), z(y)\rangle.\]

This map $z$ will be a randomly generated (in a manner depending on the kernel)
by methods outlined in the papers \cite{KK12,RR07}. We then use $z$ to embed
both the training data and the test data into $\R^D$ and run linear SVM on the
embedded data.\\

\subsection{Gaussian kernels}

A kernel $k$ is said to be shift-invariant if, for all $x,y,z$, $k(x,y) =
k(x-z,y-z)$. In the paper \cite{RR07}, the authors show that for any shift-
invariant kernel $k$, there exists a probability measure $p(w)$ on its domain
such that

\[ k(x,y) = k(x-y,0) = \int_X p(w)e^{i\langle w, x-y\rangle} dw = E_w[e^{i\langle w,x\rangle}e^{-i\langle w,y\rangle}]
\]

where the expectation above is taken with respect to the measure $p$. In the
particular case of $k(x,y) = \exp(-\gamma||x-y||^2)$,

\[ p(w) = \left(\dfrac{1}{\sqrt{4\pi\gamma}}\right)^d \exp\left(\frac{-||w||^2}{4\gamma}\right)\]

where $d$ is the dimension of the sample data. In other words, $p(w)$ is a
Gaussian distribution with mean zero and standard deviation
$\sigma = \sqrt{2\gamma}$.\\

Since both the kernel and the probability distribution are real-valued in the
above integral, the complex exponential can be replaced with
$\cos(\langle w, x-y\rangle)$. Define a random function
$z: x\mapsto \sqrt{2}\cos(\langle w,x\rangle + b)$,
where $w$ is drawn from the distribution $p$ and $b$ uniformly
from $[0,2\pi]$. Then $\langle z(x),z(y)\rangle$ is an unbiased estimator of
$k(x,y)=k(x-y,0)$. To see this, use the sum of angles formula:

\begin{align*}
z(x)z(y) &= 2\cos(\langle w,x\rangle+b)\cos(\langle w,y\rangle+b)\\
			&= 2\cos(\langle w,x\rangle)\cos(\langle w,y\rangle)\cos^2(b) \\
				& +\sin(\langle w, x\rangle)\sin(\langle w, y\rangle)\sin^2(b) - \sin(\langle w, x\rangle+\langle w, y\rangle)\cos(b)\sin(b)
\end{align*}

Since $w,b$ are chosen independently,

\begin{align*}
E[2\cos(\langle w,x\rangle)\cos(\langle w,y\rangle)\cos^2(b)] &= 2E[2\cos(\langle w,x\rangle)\cos(\langle w,y\rangle)]E[\cos^2(b)]\\
	&= 2E[\cos(\langle w,x\rangle)\cos(\langle w,y\rangle)]\cdot \frac12 \\
	&= E[\cos(\langle w,x\rangle)\cos(\langle w,y\rangle)]
\end{align*}

and similarly with the other terms. Finally,

\begin{align*}
E_{w,b}[z(x)z(y)] &= E[\cos(\langle w,x\rangle)\cos(\langle w,y\rangle) + \sin(\langle w, x\rangle)\sin(\langle w, y\rangle)]\\
						&= E[\cos(\langle w, x-y\rangle)].
\end{align*}

To lower the variance of our estimate, we sample several $(w,b)$ pairs and
average the resulting values of $z$.\\

\subsubsection{Formal Description}

\begin{algorithm}[Random Fourier Features]
\alginput{Training data $X$, an $n\times d$ matrix representing $n$ data points in dimension $d$,\\
          $\gamma$ parameter of Gaussian distribution $e^{-\gamma ||x||^2}$\\
		  dimension of range space, $D$,\\
		  random normal generator seed}
\algoutput{$X'$, an $n\times D$ dimension matrix of data in feature space to be sent to linear solver,
           as well as the $\Omega$ and $\vec{b}$ used to compute $X'$, to be used by the predictor.}

\begin{algorithmic}[1]
	\State $\Omega \set d\times D $ matrix of samples drawn from the standard normal distribution $ stdnormal$,
    then scaled by a factor of $\sqrt{2\gamma}$ to simulate a Gaussian with $\sigma=\sqrt{2\gamma}$
    (see fit function from RBFsampler class of scikit-learn)
	\State $\vec{b} \set$ vector of length $D$, each entry a uniform random sample from $[0,2\pi]$
	\State $X' \set  X\cdot \Omega$
	\State $X' \set X' + \vec{b} $ ($\vec{b}$ is added to each row)
	\State $X' \set \cos(X')$ (take the cosine of each entry)
	\State $X' \set \sqrt{\frac{2}{D}}\cdot X'$
	\State return $X', \Omega, \vec{b}$
\end{algorithmic}

\end{algorithm}

\subsubsection{Parallelization}
Since the random cosine features are generated independently, each coordinate
could be computed independently in parallel, as long as each node has access to
the distribution $p$.

\subsection{Dot product kernels}

As in the case of Gaussian kernels, we can use random feature maps to
approximate polynomial kernels,  $k(x,y)=(\langle x,y\rangle+q)^r$. Again, the
embedding takes the following simple form:

\begin{align*}
z: \R^d\to \R^D\\
x\mapsto \frac{1}{\sqrt{D}} (z_1(x), \ldots, z_D(x)).
\end{align*}

The idea here is to choose each random features $z_i$ such that it satisfies
$E[z_i(x)z_i(y)] = k(x, y)$. Then the concentration of measure phenomenon, e.g.,
as $D$ goes to infinity, ensures $|k(x, y) - z(x)^Tz(y)|$ to be small with high
probability. We describe the process of generating $z_i$ below. Note that it
applies to any positive definite kernel
$k: (x, y) \mapsto f(\langle x, y \rangle)$,
where $f$ admits a Maclaurin expansion, i.e.,
$f(x) = \sum_{N=0}^{\infty} a_N x^N$,
where $a_N = f^{(N)}(0) / N!$.
For example, in the case of polynomial kernels
$k(x,y)=(\langle x,y\rangle+q)^r$, $f(x)$ will be
$(x + q)^r$ and $a_N$ will be the N-th derivative of $(x + q)^r$ divided by $N!$.

\begin{enumerate}
	\item Randomly pick a number $N \in \N \cup \{0\}$ with $\Pcal[N] = \frac{1}{p^{N+1}}$.
    Here $p$ is a positive number larger than 1 and in practice $p=2$ is often a
    good choice since it establishes a normalized measure over $\N \cup \{0\}$ \cite{KK12}.
	\item Pick $N$ independent Rademacher vector $w_1, ..., w_N$, i.e., each
    component of Rademacher vector is chosen independently using a fair coin toss from the set $\{-1, 1\}$.
	\item Compute $z_i(x)$ as follows,
	\begin{align}
	\label{equ:svm-zi}
		z_i(x) =
		\begin{cases}
			\sqrt{a_0p}, &\quad \mbox{ if } N = 0,\\
			\sqrt{a_Np^{N+1}}\prod_{j=1}^N w_j^Tx, &\quad \mbox{ otherwise. }
		\end{cases}
	\end{align}
\end{enumerate}

The reason why we pick $N$ from $\N \cup \{0\}$ with probability $\Pcal[N] =
\frac{1}{p^{N+1}}$ becomes obvious in the following simple proof. It establishes
that the random feature product $z_i(x)z_i(y)$ approximates the kernel product
$k(x, y)$ with high probability for any $x, y \in \R^d$, i.e., $E[z_i(x)z_i(y)]
= k(x, y)$:

\begin{align*}
    E[z_i(x)z_i(y)]
    &= E_N [E_{w_1, ..., w_N} [z_i(x)z_i(y)] | N]  \\
    &= E_N[a_N p^{N+1} E_{w_1, ..., w_N} [\prod_{j=1}^N w_j^T x \prod_{j=1}^N w_j^T y]] \\
    &= E_N[a_N p^{N+1} (E_{w} [w^Tx \cdot w^Ty])^N] \\
    &= E_N[a_N p^{N+1} \langle x, y \rangle^N] \\
    &= \sum_{n=0}^{\infty} \frac{1}{p^{n+1}} \cdot a_n p^{n+1} \langle x, y \rangle^n \\
    &= f(\langle x, y \rangle) = k(x, y)
\end{align*}

See the paper of Kar and Karnick \cite{KK12} for more details.

\subsubsection{Formal Description}

\begin{algorithm}[Random Features for Polynomial Kernels]
\alginput{Training data $X$, an $n\times d$ matrix representing $n$ data points in dimension $d$, \\
Parameters of a polynomial kernel, i.e. degree $r$, dimension of linear feature space $D$\\}
\algoutput{$X'$, an $n\times D$ dimension matrix of data in feature space to be sent to linear solver \\
$\Omega, \Ncal$ generated by the algorithm, to be used by the predictor.\\
}
\begin{algorithmic}[1]
	\State $\Ncal \set $ array of D entries, each generated from the exponential probability distribution $\frac{1}{2^{N+1}}$
	\State $\Omega \set $ matrix of dimensions $d\times sum(\Ncal) \cdot D$ with $\{-1,1\}$ entries, chosen with a fair coin flip
	\State $X'\set X\cdot \Omega,$ now a matrix of dimensions $n \times sum(\Ncal)$
	\label{alg:x-omega}
	\For {each $row$ in $X'$}\label{alg:x-for}
		\State $row' \set $ dividing $row$ into D segments, with $i$th segment consisting of $\Ncal_i$ number of entries.
		\State $row'' \set $ array of D entries aggregating from each segment of $row'$ according to \eqref{equ:svm-zi}
	\EndFor
	\State $X'' \set $ the transformed $X'$ divided by $\frac{1}{\sqrt{D}}$
	\State return $X'',\Omega, \Ncal$
\end{algorithmic}
\end{algorithm}

\subsubsection{Parallelization}
In the above algorithm, Step \ref{alg:x-omega} is done by broadcasting the
matrix $\Omega$ to each row of $X$, and computing the matrix-vector product
locally in parallel for each row; Similarly, Step \ref{alg:x-for} can also be
distributed since the computations for each row are independent of the others.

\section{Novelty Detection}

Suppose we have training data $x_1, x_2, \ldots x_n \in \R^d$, the goal of
novelty detection is to learn a hyperplane in $\R^d$ that separates the training
data from the origin with maximum margin. We model this as a one-class
classification problem by transforming the training data to
$(x_1,y_1),\ldots,(x_n,y_n) \in \R^d\times \{1\}$, indicating that the dependent
variable of each training instance is assumed to be the same. The origin is
treated as a negative class data point and all input data points are treated as
part of the positive class. Given such a mapping, we use the SVM classification
mechanisms detailed in Sections~\ref{sec:linear} and~\ref{sec:nonlinear} to
learn a one-class classification model.


\section{Before Learning}
As said in \cref{sec:intro}, the goal of \textit{L*} and \textit{NL*} is to ``learn'' a regular unknown language $\U$ and output a conjecture which, when accepted, will be an automaton $\A$ recognizing $\U$.

Both algorithms work on the idea that a \textit{Learner} interacts with a \textit{Teacher} knowing $\U$ via two kinds of queries: \textit{membership} and \textit{equivalence} queries.

In a \textit{membership query} the \textit{Learner} asks the \textit{Teacher} if a word $\omega$ belongs to $\U$.

\begin{equation}
  \begin{aligned}
    \label{eq:interactions}
    member(\omega) & =
    \begin{cases}
      \text{True}  & \quad  \text{if } \omega \in \U \\
      \text{False} & \quad   \text{otherwise}
    \end{cases}
  \end{aligned}
\end{equation}

In an \textit{equivalence query} the \textit{Learner} sends an Automaton (representing a conjecture) to the \textit{Teacher} which can answer \textit{Yes} if $\LA = \U$, or a word $\omega$ belonging to $\U$ but not to $\LA$ or the other way round. In this case, $\omega$ is called a \textit{counter-example}.

A counter-example can also be defined as a word $\omega$ belonging to the symmetrical difference (noted $\Delta$) between $\LA$ and $\U$.

\begin{equation}
  \begin{aligned}
    equiv(\A) & =
    \begin{cases}
      \text{Yes}               & \quad \text{if } \LA = \U \\
      \omega \in \U \Delta \LA & \quad \text{otherwise}
    \end{cases}
  \end{aligned}
\end{equation}


\section{High-level Proof Strategy}
\label{sect.high-level.proof.strategy}

Since our formalisation of distributed algorithms goes into greater depth than prior work on strong eventual consistency, it is important to have a structure that keeps the proofs manageable.
Our approach breaks the proof into simple modules with cleanly defined properties---called \emph{locales}, a standard sectioning mechanism of Isabelle/HOL that will be described in Section~\ref{subsect.an.overview.of.isabelle} below---and composes them in order to describe more complex objects.
This locale structure is illustrated in Figure~\ref{fig.proof.structure} and explained below.
%See Section~\ref{sect.isabelle.locales} for more detail on locales.

\begin{figure}
\centering
\input{proof-structure}
\caption{The main locales (modules) of our proof, and the relationships between them.
Solid arrows indicate a more specialised locale that extends a more general locale (like extending interfaces in OOP).
Dashed arrows indicate a sublocale that satisfies the assumptions of the superlocale (like implementing an interface in OOP).
}\label{fig.proof.structure}
\end{figure}

By lines of code, more than half of our proof is used to construct a general-purpose model of consistency in distributed systems, described in Section~\ref{sect.abstract.convergence}, and an axiomatic model of a computer network, described in Section~\ref{sect.network}, with both modules independent of any particular replication algorithm.
The remainder describes a formalisation of three CRDTs and their proofs of correctness, described in Sections~\ref{sect.rga} and~\ref{sect.simple.crdts}.
By keeping the general-purpose modules abstract and implementation-independent, we construct a reusable library of specifications and theorems.

We describe our formalisation of strong eventual consistency in Section~\ref{sect.abstract.convergence}.
In particular, we define what we mean by convergence, and prove an \emph{abstract convergence theorem}, which shows that the state of nodes converges if concurrent operations commute.
We are able to prove this fact without mentioning networks or any particular CRDT, but merely by reasoning about the ordering and properties of operations.
This definition constitutes a formal specification of what we mean by strong eventual consistency.

In Section~\ref{sect.network} we describe an axiomatic model of asynchronous networks.
The definition of the network is important because it allows us to prove that the desired properties hold in \emph{all} possible network behaviours, and that we are not making any dangerous assumptions that might be violated---an aspect that has dogged previous verification efforts for related algorithms (see Section~\ref{sect.related.verification}).
The network is the only part of our proof in which we make any axiomatic assumptions, and we show in Section~\ref{sect.network} that our assumptions are realistic, reflecting both standard conventions for modelling distributed systems, and the practical realities of network protocols today.
We then prove that our network satisfies the ordering properties required by the abstract convergence theorem of Section~\ref{sect.abstract.convergence}, and thus deduce a convergence theorem for our network model.

We use the general-purpose theorems and definitions from Sections~\ref{sect.abstract.convergence} and~\ref{sect.network} to prove the strong eventual consistency properties of concrete algorithms.
In Section~\ref{sect.rga} we describe our formalisation of the Replicated Growable Array (RGA), a CRDT for ordered lists.
We first show how to implement the RGA's insert and delete operations, with proofs that each operation commutes with itself, and that all operations commute with each other.
Insertion and deletion only commute under various conditions, so we prove that these conditions are satisfied in all possible network behaviours, and thus we obtain a concrete convergence theorem for our RGA implementation.
Next, in Section~\ref{sect.simple.crdts}, we demonstrate the generality of our proof framework with definitions of two simple CRDTs: a Counter and an Observed-Remove Set.
%We show that with the previously established tooling in place, it is very easy to prove that these algorithms implement the abstract specification of strong eventual consistency.

As illustrated in Figure~\ref{fig.proof.structure}, the $\isa{counter}$, $\isa{orset}$, and $\isa{rga}$ locales can use the definitions and lemmas of the network model because they extend that model.
We then prove that all three locales satisfy the abstract specification $\isa{strong-eventual-consistency}$, and therefore show that these algorithms provide strong eventual consistency.


\documentclass[11pt]{article}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{textcomp}
\usepackage{fullpage}
\usepackage{url}
\usepackage{ocamldoc}
\begin{document}
\tableofcontents
\section{Module {\tt{Ecc}} : An elliptic curve point.}
\label{module:Ecc}\index{Ecc@\verb`Ecc`}
 It is either infinity or a point (x,y).

\ocamldocvspace{0.5cm}

\begin{ocamldoccode}
{\tt{module }}{\tt{Ecc}}{\tt{ : }}\end{ocamldoccode}
\label{module:Ecc.Ecc}\index{Ecc@\verb`Ecc`}

\begin{ocamldocsigend}

\label{type:Ecc.Ecc.point}\begin{ocamldoccode}
type point =
  | Infinity
  | Point of Z.t * Z.t
\end{ocamldoccode}
\begin{ocamldoccomment}
An elliptic curve point. It is either infinity or a point (x,y).
\end{ocamldoccomment}
\index{point@\verb`point`}

\label{type:Ecc.Ecc.elliptic-underscorecurve}\begin{ocamldoccode}
type elliptic_curve = {\char123}
  p : Z.t ;
  a : Z.t ;
  b : Z.t ;
  g : point ;
  n : Z.t ;
  h : Z.t ;
{\char125}
\end{ocamldoccode}
\index{elliptic-underscorecurve@\verb`elliptic_curve`}
\begin{ocamldocdescription}
The type of domain parameters

\end{ocamldocdescription}

\label{val:Ecc.Ecc.inverse}\begin{ocamldoccode}
val inverse : Z.t -> Z.t -> Z.t
\end{ocamldoccode}
\index{inverse@\verb`inverse`}
\begin{ocamldocdescription}
Ecc.inverse a n inverses the number a modulo n

\end{ocamldocdescription}

\label{val:Ecc.Ecc.verify-underscorerange}\begin{ocamldoccode}
val verify_range : Z.t -> Z.t -> Z.t -> bool
\end{ocamldoccode}
\index{verify-underscorerange@\verb`verify_range`}
\begin{ocamldocdescription}
Ecc.verify\_range a l h returns true if l $\leq$ a $\leq$ h or false otherwise

\end{ocamldocdescription}

\label{val:Ecc.Ecc.is-underscorepoint}\begin{ocamldoccode}
val is_point : point -> elliptic_curve -> bool
\end{ocamldoccode}
\index{is-underscorepoint@\verb`is_point`}
\begin{ocamldocdescription}
Returns true if a point belongs to an elliptic curve or false otherwise

\end{ocamldocdescription}

\label{val:Ecc.Ecc.double-underscorepoint}\begin{ocamldoccode}
val double_point : point -> elliptic_curve -> point
\end{ocamldoccode}
\index{double-underscorepoint@\verb`double_point`}
\begin{ocamldocdescription}
Given a point P on an elliptic curve and the elliptic curve retuns the point 2P
        on that curve.

\end{ocamldocdescription}

\label{val:Ecc.Ecc.add-underscorepoint}\begin{ocamldoccode}
val add_point : point -> point -> elliptic_curve -> point
\end{ocamldoccode}
\index{add-underscorepoint@\verb`add_point`}
\begin{ocamldocdescription}
Given two points P and Q, both on the same elliptic curve, and the elliptic curve returns
        P+Q on that curve.

\end{ocamldocdescription}

\label{val:Ecc.Ecc.multiply-underscorepoint}\begin{ocamldoccode}
val multiply_point : point -> Z.t -> elliptic_curve -> point
\end{ocamldoccode}
\index{multiply-underscorepoint@\verb`multiply_point`}
\begin{ocamldocdescription}
Given a point P on an elliptic curve, an integer k and the elliptic curve returns
        the scalar multiplication kP on that curve.

\end{ocamldocdescription}

\label{val:Ecc.Ecc.integer-underscoreof-underscoreoctet}\begin{ocamldoccode}
val integer_of_octet : string -> int
\end{ocamldoccode}
\index{integer-underscoreof-underscoreoctet@\verb`integer_of_octet`}

\label{val:Ecc.Ecc.octList-underscoreof-underscoreoctStr}\begin{ocamldoccode}
val octList_of_octStr : string -> string list
\end{ocamldoccode}
\index{octList-underscoreof-underscoreoctStr@\verb`octList_of_octStr`}

\label{val:Ecc.Ecc.integer-underscoreof-underscoreoctStr}\begin{ocamldoccode}
val integer_of_octStr : string -> Z.t
\end{ocamldoccode}
\index{integer-underscoreof-underscoreoctStr@\verb`integer_of_octStr`}

\label{val:Ecc.Ecc.brainpool-underscoreP256-underscorer1}\begin{ocamldoccode}
val brainpool_P256_r1 : elliptic_curve
\end{ocamldoccode}
\index{brainpool-underscoreP256-underscorer1@\verb`brainpool_P256_r1`}
\begin{ocamldocdescription}
An elliptic curve used for ECC as defined by Brainpool

\end{ocamldocdescription}

\label{val:Ecc.Ecc.test-underscorecurve}\begin{ocamldoccode}
val test_curve : elliptic_curve
\end{ocamldoccode}
\index{test-underscorecurve@\verb`test_curve`}
\begin{ocamldocdescription}
An elliptic curve with small domain parameters for testing purposes

\end{ocamldocdescription}

\label{val:Ecc.Ecc.random-underscorebig-underscoreint}\begin{ocamldoccode}
val random_big_int : Z.t -> Z.t
\end{ocamldoccode}
\index{random-underscorebig-underscoreint@\verb`random_big_int`}
\begin{ocamldocdescription}
Ecc.random\_big\_int bound returns a random integer in {\tt{1, bound-1}}

\end{ocamldocdescription}

\label{val:Ecc.Ecc.sign}\begin{ocamldoccode}
val sign : string -> Z.t -> elliptic_curve -> Z.t * Z.t
\end{ocamldoccode}
\index{sign@\verb`sign`}
\begin{ocamldocdescription}
Ecc.sign message sk curve where sk is secret key of the user s and curve
       the public elliptic curve, returns the signature (r, s) of the message.

\end{ocamldocdescription}

\label{val:Ecc.Ecc.verify}\begin{ocamldoccode}
val verify : string -> Z.t * Z.t -> point -> elliptic_curve -> bool
\end{ocamldoccode}
\index{verify@\verb`verify`}
\begin{ocamldocdescription}
Ecc.verify message (r, s) pk curve where pk is the public key of the user
       who signed the message, returns true if the (r, s) is a valid
       signature or false otherwise.

\end{ocamldocdescription}

\label{val:Ecc.Ecc.create-underscorekeys}\begin{ocamldoccode}
val create_keys : elliptic_curve -> point * Z.t
\end{ocamldoccode}
\index{create-underscorekeys@\verb`create_keys`}
\begin{ocamldocdescription}
Creates a tuple (public\_key, secret\_key) where public\_key is a point of
       the curve and secret\_key an integer.

\end{ocamldocdescription}
\end{ocamldocsigend}

\end{document}


\section{Histogram Clustering}

% ===
\emph{Least Angle Clust. (LAC): {\normalfont\sffamily[Idea]}}\\
Similarity\enspace $S(\bm x_i, \bm x_j) = w_{ij} \cos(\phi_{ij}) = w_{ij} \bm e_i \cdot \bm e_j$\enspace with unit vectors\enspace $\bm e_i \coloneqq \bm x_i / \norm{\bm x_i}$,\enspace e.g. choice $w_{ij} = \norm{\bm x_i} \cdot \norm{x_j}$.

% ===
\columnbreak
\emph{Dyadic data:}\enspace
$\mathcal Z = \brace{ (x_{i(r)}, y_{j(r)}) ; 1\leq r\leq \ell }$

\begin{itemize}
    \item prototype / ``centroid'':\enspace
        $q(y_j \mid \alpha)$
    \item empirical dist.:\enspace
        $\hat p(y_j\mid x_i)
        = \frac{\hat p(x_i, y_j)}{\hat p(x_i)}
        \:\substack{
            \color{gray} \leftarrow \text{scr. (5.10)} \\[2pt]
            \color{gray} \leftarrow \text{scr. (5.11)}
        }$
    \iffalse
    $= \frac{
        \frac1\ell \sum_{r\leq\ell} \delta_{x_i, x_{i(r)}} \delta_{y_j, y_{j(r)}}
        %\frac1\ell \sum_{r\leq\ell} \bm 1 \brace{(x_{i(r)}, y_{j(r)} = (x_i, y_i)}
        %\frac1\ell \sum_{r\leq\ell} \Delta_{x_i, x_{i(r)}} \Delta_{y_j, y_{j(r)}}
      }{
        \sum_{j\leq m} \hat p(x_i, y_j)
        %\frac1\ell \sum_{r\leq\ell} \sum_{j\leq m} \Delta_{x_i, x_{i(r)}} \Delta_{y_j, y_{j(r)}}
    }$
    \fi
    %\item $p(x_i,y_j \mid c,q) = p(x_i) q(y_j\mid c(i))$
\end{itemize}

Likelihood:\enspace
$P(\mathcal Z\mid c,q) = \prod_{r\leq\ell} p(x_{i(r)}, y_{j(r)} \mid c,q)$\\\quad
$= \overset{\text{\color{gray} scr. (5.12)}}{\ldots}
= \prod_i \prod_j \brack{ q(y_j\vert c(i)) \cdot p(c(i)) }^{\ell \hat p(x_i, y_i)}$

\textit{Assume} $p(\alpha)=1/k$ \textit{and} $\hat p(x_i) = 1/n$

$\Rightarrow$ \emph{Cost:}
$R\ap{hc} (c, q, \mathcal Z) = \cancel{\frac{\ell}{n}} \sum_{i\leq n} D\ped{KL} \brack*{\hat p(\cdot\mid x_i) \parallel q(\cdot\mid c(i))}$

Solving the \textbf{Gibbs dist.} $p(c \mid q, \hat p) = \prod_{i\leq n} P_{i, c(i)}$\\
via Lagrange yields \highlight*{$q^\ast(y_j\mid\alpha) = \frac{\sum_{i\leq n} P_{i\alpha} \cdot \hat p(y_j\mid x_i)}{\sum_{i\leq n} P_{i\alpha}}$}
{\color{gray}\scriptsize \shortstack{Lemma 2\\ch.3 p.36}}

% ===
\subsection{Information Bottleneck Method}

Find efficient code $X \mapsto \hat X$ (codebook vector) and preserve relevant info. about context $Y$.

\emph{Criterion:}\enskip
$R\ap{IB}(q(\hat x\mid x)) = I(X; \hat X) - \beta I(\hat X; Y)$

\emph{Markov chain:}\enskip
$\hat X \xrightarrow{q(\hat x\mid x)} X \xrightarrow{p(y\mid x)} Y$

\emph{Generation process:}\enskip
w/ \textit{distortion} $d(x,\hat x) = D\ped{KL}[\cdot]$\\
$\begin{cases}\begin{matrix*}[l]
    q_{\color{OrangeRed} t}(\hat x\vert x) &
        \propto q_{\color{OrangeRed} t}(\hat x) \cdot \exp\paren{ -\beta \: D\ped{KL}\brack*{p(y\vert x) \parallel p_{\color{red} t}(y\vert \hat x)} }
    \\
    q_{\color{OrangeRed} t{+}1}(\hat x) &
        = \sum_x p(x) \cdot q_{\color{OrangeRed} t}(\hat x \mid x)
    \\
    p_{\color{OrangeRed} t{+}1}(y\vert \hat x) &
        = \sum_{x} p(y\mid x) \cdot p(x) \cdot q_{\color{OrangeRed} t}(\hat x\mid x) \,/\, q_{\color{OrangeRed} t}(\hat x)
\end{matrix*}\end{cases}$

% ===
\subsection{Parametric Distributional Clustering}

\textbf{Idea:} Use a mixture of Gaussian prototypes, i.e.\\ \phantom{\textbf{Idea:}} $\textcolor{gray}{p(y_j\mid\nu)\equiv\:} p(b\mid\nu) = \sum_{\alpha\leq s} p(\alpha\vert\nu) \; G_\alpha(b)$ .

\begin{minipage}{\linewidth}
    \centering
    $x_i \xrightarrow{c(i) = \nu} \nu \xrightarrow{p(b\mid\nu)} \hat p(b\mid i)$
\end{minipage}

\textit{Note:}\enspace
Feature values $y_j$ (``bins'' $b$) only depend on cluster index $\nu$ and not explicitly on the site $x_i$!

\textbf{Notation:}\enspace
$x_i \leftarrow i$, \enskip
$y_j \leftarrow b$ (\textit{bins}), \enskip
$\nu \leftarrow$ clusters

\emph{Likelihood:}\enspace
(both equivalent if $p(i)=\frac1n$)\\
\enspace $P(X\mid c,\theta) = \prod_{i\leq n} p(\textcolor{red}{c(i)}) \prod_{b\leq m} [p(b\mid \textcolor{red}{c(i)})]^{\ell \hat p(i,b)}$,\\
\enspace $P(X,M\mid \theta) = \prod_{i\leq n} \textcolor{red}{\prod_{\nu\leq k}} \big[ p(\textcolor{red}{\nu}) \cdot \prod_{b\leq m} p(b\mid\textcolor{red}{\nu})^{n_{ib}} \big]^{M_{i\nu}}$
\\
where\enskip
\begin{minipage}[t]{\linewidth-\widthof{where\enskip}}
    $n_{ib}$ : \#occur. an observ. at site $i$ is inside $I_b$ \\
    $M_{i\nu} = p(\nu\mid i) \in \{0,1\}$ \enskip clust. membersh. assign.
\end{minipage}

\emph{Cost (IB):}\enspace
$R\ap{PDC}(c, p_{\cdot\mid c}) = -\log P(X,M\theta) = \ldots$
\hfill $\ldots = -\sum_{i\leq n} \big[ \log p_{c(i)} + \frac{\ell}{n} \sum_{b\leq m} \hat p(b\mid i) \log p(b\mid c(i)) \big]$

\begin{highlightbox}
\begin{tabularx}{\linewidth}{@{} l @{\:\:} X @{}}
    \textbf{E-step:}
        & $h_{i\nu} \!= {-}\log p_\nu {-} \sum_b \frac{\ell}{n} \hat p(b\mid i) \log p(b\mid\nu)$\\
        & $q_{i\nu} = \E{\mathbb I_{\brace{c(i) = \nu}}} \propto \exp(- h_{i\nu} / T)$\\
    \textbf{M-step:}
        & $p_\nu = \frac1n \sum_{i\leq n} q_{i\nu}$
        \\
        & No closed form sol. for $p(\alpha\mid\nu)$. Thus, iteratively optimize pairs s.t. $\sum_\alpha p(\alpha\mid\nu) = 1$.
\end{tabularx}
\end{highlightbox}

% ===


\subsection{Distance Metrics}
\label{sec:dist}

We represent each context with a sparse high dimensional probability
vector called the substitute vector as described in the previous
section.  In this section we compare various distance metrics in this
high dimensional space with the goal of discovering one that will
judge vectors that belong to the same syntactic category similar and
vectors that belong to different syntactic categories distant.  The
distance metrics we have considered are listed in
Table~\ref{tab:metrics}.

% we should remove KL2 metric from here, because we have sparse vectors
% also update the table caption

\begin{table}[ht] \centering
%\small
\caption{Similarity metrics.  JS is the Jensen-Shannon divergence and
  KL2 is a symmetric implementation of Kullback-Leibler
  divergence. Bold lower case letters represent vectors.}
\begin{tabular}{|lll|}
\hline
%% Pretify this table norm numbers looks weird.
Cosine($\mathbf{p}, \mathbf{q}$) & = & $<\mathbf{p},\mathbf{q}> / (\|\mathbf{p}\|_{_{2}} \|\mathbf{q}\|_{_{2}})$ \\
Euclid($\mathbf{p}, \mathbf{q}$) & = & $\|\mathbf{p} - \mathbf{q}\|_{_{2}}$ \\
Manhattan($\mathbf{p}, \mathbf{q}$) & = & $\|\mathbf{p} - \mathbf{q}\|_{_{1}}$ \\
Maximum($\mathbf{p}, \mathbf{q}$) & = & $\|\mathbf{p} - \mathbf{q}\|_{_{\infty}}$ \\
KL2($\mathbf{p}, \mathbf{q}$) & = & $\sum_i p_iln(p_i/q_i) + q_iln(q_i/p_i) $\\
JS($\mathbf{p}, \mathbf{q}$) & = & $\sum_i p_iln(p_i/m_i) + q_iln(q_i/m_i) $\\
& & where $m_i = 0.5(p_i + q_i)$\\
%JS($\mathbf{p}, \mathbf{q}$) & = & KL2($\mathbf{p},0.5(\mathbf{p}+\mathbf{q})$)\\
\hline
\end{tabular}
\label{tab:metrics}
\end{table}

% are we still using k = 30 ? update if necessary
To judge the merit of each distance metric we obtained supervised
baseline scores using leave-one-out cross validation and the weighted
k-nearest-neighbor algorithm\footnote{Neighbors were weighted using
  1/distance, $k=30$ was chosen empirically.} on the gold tags of the
PTB24K.  The results are listed in
Table~\ref{tab:distscores} sorted by score.

% update scores in this table
\begin{table}[h] \centering
\caption{Supervised baseline scores with different distance metrics.
  Log-metric indicates that metric applied to the log of the
  probability vectors.}
\begin{tabular}{|l|c|}
\hline
Metric & Accuracy \\
\hline
%% K=30 24K WSJ
KL2 & .6889 \\
Manhattan & .6865 \\
Jensen & .6801 \\
Cosine & .6706 \\
Maximum & .6663 \\
Euclid & .6255 \\
log-Maximum & .5361 \\
log-Cosine & .4847 \\
log-Euclid & .4038 \\
log-Manhattan & .3729 \\
%% K=30 1M WSJ
%% KL2 & ? \\
%% Manhattan & .7354\\ %0.735381668919
%% Jensen & .7317 \\ %0.731718247078
%% Cosine & .7240 \\ %0.724018245545
%% Maximum & ? \\
%% Euclid & .6109 \\ %0.610962491672
%% lg2-Maximum & ? \\
%% lg2-Cosine & ? \\
%% lg2-Euclid & ? \\
%% lg2-Manhattan & ? \\
\hline
\end{tabular}
\label{tab:distscores}
\end{table}
% K=1 1M WSJ
% KL2 can we define kl2 on sparse vectors
% Manhattan
% Jensen 0.684500999347
% Cosine 0.672916918704
% Maximum
% Euclid 0.597443613122
% lg2-Maximum
% lg2-Cosine
% lg2-Euclid
% lg2-Manhattan
% K=20 1M WSJ
% KL2 & ?\\
% Manhattan & ?\\
% Jensen & 0.732678404384\\
% Cosine & 0.720252797472\\
% Maximum & ?\\
% Euclid & 0.617086369856\\
% log-Maximum & ?\\
% log-Cosine & ?\\
% log-Euclid & ?\\
% log-Manhattan & ?\\

% update scores in this paragraph as well
The entries with the log-prefix indicate a metric applied to the log
of the probability vectors.  Distance metrics on log probability
vectors performed poorly compared to their regular counterparts
indicating that differences in low probability words are relatively
unimportant and high probability substitutes determine syntactic
category.  The surprisingly good result achieved by the simple Maximum
metric (which identifies the dimension with the largest difference
between two vectors) also support this conclusion.  The maximum score
of .68 can be taken as a rough upper bound for an unsupervised learner
using this space on the PTB24K corpus because 32\% of the instances
are assigned to the wrong part of speech by the majority of their
closest neighbors.  We will discuss alternative ways to push this
upper bound higher by enforcing a one-tag-per-word rule in
Section~\ref{sec:clustering} and considering the context together with
the co-occurring word type in Section~\ref{sec:code}.

% moreover we are using probability vectors so its more natural to use
% kl2

%%%% Distance calcualtion on sparse 24K vectors
% K=30
% KL2 & ?\\
% Manhattan & 0.694629475437\\
% Jensen & 0.684554537885\\
% Cosine & 0.668068276436\\
% Maximum & 0.663821815154\\
% Euclid & 0.639841798501\\
% log-Maximum & ?\\
% log-Cosine & ?\\
% log-Euclid & ?\\
% log-Manhattan & ?\\
% Distance files are located at:
% /work/upos_alllang/run.mali/test.wsj24k.knn[0,1,2,3,4].gz


\section{Linear complexity RPE
implementation}

In this section we will detail how the relative positional encoding
 proposed by \citet{shaw2018selfattention}
can in fact be computed with linear complexity. The $S_{rel}$ matrix of scores is computed as
${S_{rel}}_{ij} = \vec{Q_i} \cdotp \vec{RP}_{clip(i-j, -k, k)}$. Here
below the colors represent the index of the query and the number the
index of the relative position.

\begin{figure}
\centering
\includegraphics[width=0.9\linewidth]{images/S_rel.png}
\caption{$S_{rel}$ calculation}
\end{figure}

This score matrix must then be multiplied by the matrix of value
vectors: $A_{rel} = S_{rel} \times V$. Each row can be interpreted as
a set of weights in the weighted sum of the value vectors.

\begin{figure}
\centering
\includegraphics[width=0.9\linewidth]{images/S_rel_V.png}
\caption{$A_{rel}$ calculation}
\end{figure}

One can observe that the some weights are repeated several times in the
$S_{rel}$ matrix. So calculating the whole matrix can be avoided by
instead calculating the dot product of each query vector with each
relative position's embedding (complexity
$O \left(L_Q\times d\times(2k+1)\right)$). The operation can then be
decomposed in three terms: the lower triangle, the diagonal and the
upper triangle respectively.

\begin{itemize}
\item A set of weights that multiply a
accumulated sum of value vectors (complexity $O(max(L_Q, L_K))$)
\item An
element-wise multiplication between two tensors (complexity
$O(L_Q\times (2k-1) \times d)$)
\item A set of weights that multiply a
accumulated sum of value vectors (complexity $O(max(L_Q, L_K))$).
\end{itemize}

\begin{figure}
\centering
\includegraphics[width=0.9\linewidth]{images/S_rel_V_detailed.png}
\caption{$A_{rel}$ simplified calculation}
\end{figure}

Thus the attention can in fact be computed with linear complexity if we
get rid of the softmax function.

The memory used can be further reduced by observing that the right side
of the second element is a strided view of a padded copy of the value
matrix. Moving of one cell along the first dimension is equivalent to
moving along one cell of the second dimension.

The calculation can be implemented as follow, using zero-based slice
notation for compactness:

\begin{itemize}
	\item
	$\phi(Q)$, $\phi(V)$ and $RP$, are matrices of shape
	$(L_Q, d)$, $(L_K, d)$ and $(2k+1, d)$ respectively
	\item
	The dot product of each query and each relative position embedding is
	calculated as $weights = \phi(Q) \times RP^T$
	\item
	The weights of the horizon before and after are
	$H_{before} = weights_{[:, 0]}$ and
	$H_{after} = weights_{[:, -1]}$
	\item
	The weights of the diagonal are defined as
	$window = weights_{[:,1:-1]}$. For the masked case, columns on the
	right of the $k^{th}$ columns are set to 0
	($window_{[:, k+1:]} = 0$)
	\item
	The left term is calculated as
	$left = H_{before} * align(lower, min(max(0, L_Q-k), L_K))$ with
	$lower$ the concatenation of:

	\begin{itemize}
		\item
		a tensor of zeros of shape $\left(min(k, L_Q), d\right)$
		\item
		$cumsum(V, dim=0)$
	\end{itemize}
	\item
	The middle term is calculated as $middle = diagonal \odot strided$
	with $strided$ the strided view of:

	\begin{itemize}
		\item
		a tensor of 0 of shape $(max(0, k-1), d)$
		\item
		the value matrix $V$
		\item
		a tensor of shape $(max(0, L_Q-L_K), d)$
	\end{itemize}
	\item
	The right term is calculated as $right = 0$ for masked case, or for
	bidirectional case, $right = H_{right} * reverse$, with reverse the
	concatenation of:

	\begin{itemize}
		\item
		a tensor of 0 of shape $(L_Q-max(0, L_K-k), d)$
		\item
		$sum(V_{[inf, sup]}, dim=0) - cumsum(V_{[inf, sup-1]}, dim=0)$
		with $inf = k-1$, $sup = min(L_Q+k, L_K)$
	\end{itemize}
	\item
	The resulting attention function is $A = left + middle + right$
\end{itemize}

with:

\begin{itemize}
	\item
	$*$ the element-wise multiplication
	\item
	$\odot$ the element-wise multiplication followed by sum along second
	dimension
	\item
	all concatenations done along the first dimension
\end{itemize}

\endinput


\section{mjd}

\cc{mjd} provides conversion between a calendar date and MJD and shows the current MJD.

\subsection{usage}

\begin{description*}
\item[-d \textless DD MM YYYY\textgreater] convert date to MJD
\item[-h] print help and exit
\item[-m \textless MJD\textgreater] convert MJD to date
\item[-t] print today's MJD and exit
\item[-v] print version information and exit
\end{description*}


\par
\section{Driver programs for the {\tt SemiImplMtx} object}
\label{section:SemiImplMtx:drivers}
\par
This section contains brief descriptions of the driver programs.
\par
%=======================================================================
\begin{enumerate}
%-----------------------------------------------------------------------
\item
\begin{verbatim}
testGrid msglvl msgFile n1 n2 n3 maxzeros maxsize seed type symmetryflag
         sparsityflag pivotingflag tau droptol nrhs depth
\end{verbatim}
This driver program tests the {\tt SemiImplMtx} creation and solve
methods for a matrix from a regular 2-D or 3-D grid.
The matrix can be real or complex and is loaded with random
entries.
The linear system $AX = B$ is solved as follows.
\begin{itemize}
\item
First $A$ is factored, and a {\tt FrontMtx} object is created to
hold the factorization.
\item
The system is solved using the {\tt FrontMtx} object.
\item
A {\tt SemiImplMtx} matrix object is constructed from the {\tt
FrontMtx} object and $A$.
\item
The system is solved using the {\tt SemiImplMtx} object.
\end{itemize}
Various statistics and CPU timings are written to the message
file to compare the two solution processes.
Use the {\tt do\_grid} shell script for testing.
\par
\begin{itemize}
\item
The {\tt msglvl} parameter determines the amount of output.
\item
The {\tt msgFile} parameter determines the message file --- if {\tt
msgFile} is {\tt stdout}, then the message file is {\it stdout},
otherwise a file is opened with {\it append} status to receive any
output data.
\item
{\tt n1} is the number of grid points in the first direction.
\item
{\tt n2} is the number of grid points in the second direction.
\item
{\tt n3} is the number of grid points in the third direction.
\item
{\tt maxzeros} is the maximum number of zeroes to place into a front.
\item
{\tt maxsize} is the maximum number of internal rows and columns
in a front.
\item
{\tt type} must be either
{\tt SPOOLES\_REAL} or
{\tt SPOOLES\_COMPLEX}.
\item
{\tt symmetryflag} must be either
{\tt SPOOLES\_SYMMETRIC},
{\tt SPOOLES\_HERMITIAN} or
\item
{\tt sparsityflag} must be either
{\tt FRONTMTX\_DENSE\_FRONTS} or
{\tt FRONTMTX\_SPARSE\_FRONTS}.
\item
{\tt pivotingflag} must be either
{\tt SPOOLES\_PIVOTING},
{\tt SPOOLES\_NO\_PIVOTING} or
\item
{\tt tau} is used when pivoting is enabled,
it is an upper bound on the magnitude of the entries in $L$ and $U$.
\item
{\tt droptol} is used when an approximate factorization is called
for, (i.e., when {\tt sparsityflag} is {\tt FRONTMTX\_SPARSE\_FRONTS}).
It is a lower bound on the magnitude of the entries in $L$ and $U$
that are stored and used in computations.
\item
{\tt nrhs} is the number of right hand sides.
\item
{\tt depth} is used to specify the schur complement.
It is based on separators, not on fronts. (Recall that large
separators can be split into smaller fronts for efficiency reasons.)
All fronts found in separators lower than {\tt depth} in depth
(the top level separator has depth zero) belong in domains.
\end{itemize}
%-----------------------------------------------------------------------
\item
\begin{verbatim}
testSimple msglvl msgFile inFrontMtxFile inInpMtxFile inIVfile
\end{verbatim}
This driver program is used to construct a {\tt SemiImplMtx} object.
It reads in a {\tt FrontMtx} and {\tt InpMtx} from files.
It also reads in an {\tt IV} object that specifies whether a front
is to be in the domains (the (1,1) block) or the
Schur complement (the (2,2) block).
It then creates the {\tt SemiImplMtx} object and writes it to the
message file.
Use the {\tt do\_simple} script file for testing.
\par
\begin{itemize}
\item
The {\tt msglvl} parameter determines the amount of output.
\item
The {\tt msgFile} parameter determines the message file --- if {\tt
msgFile} is {\tt stdout}, then the message file is {\it stdout},
otherwise a file is opened with {\it append} status to receive any
output data.
\item
The {\tt FrontMtx} object is read from the {\tt inFrontMtxFile} file,
which must be of the form {\tt *.frontmtxf} or {\tt *.frontmtxb}.
\item
The {\tt InpMtx} object is read from the {\tt inInpMtxFile} file,
which must be of the form {\tt *.inpmtxf} or {\tt *.inpmtxb}.
\item
The map vector {\tt IV} object is read from the {\tt inIVfile} file,
which must be of the form {\tt *.ivf} or {\tt *.ivb}.
\end{itemize}
%-----------------------------------------------------------------------
\end{enumerate}