\section{Global Sobol Sensitivity: Sudret}
Associated external model: \texttt{sudret\_sobol\_poly.py}

This model provides analytic Sobol sensitivities for a flexible number of input parameters.  It is taken from
\cite{sudret2007} and has the following form:
\begin{equation}
  u(Y) = \frac{1}{2^N} \prod_{n=1}^N \left(3y_n^2 + 1\right).
\end{equation}
The variables $y_n$ are distributed uniformly on [0,1].  For three input variables ($N=3$), the Sobol sensitivities are
as follows, to 12 digits of accuracy:
\begin{align}
  S_1 = S_2 = S_3 &= \frac{25}{91}\hspace{10pt} (0.2747), \\
  S_{1,2} = S_{1,3} = S_{2,3} &= \frac{5}{91}\hspace{10pt} (0.0549), \\
  S_{1,2,3} &= \frac{1}{91}\hspace{10pt} (0.0110).
\end{align}
The mean is 1.0 and the variance is 0.728.

\subsection{Second-Order ANOVA of Second-Order Cut-HDMR Expansion of Sudret}
One particular analytic tests involves calculating Sobol sensitivities for the second-order cut-HDMR expansion
of the Sudret.  We use three variables and equate $u(Y) = f(x,y,z)$.  The reference cut point is $(\bar
x,\bar y, \bar z) = (\frac{1}{2},\frac{1}{2},\frac{1}{2})$.

The first step is to construct the second-order cut-HDMR expansion $T$,
\begin{equation}
  f(x,y,z) \approx T[f](x,y,z) = t_r + t_x + t_y + t_z + t_{xy} + t_{xz} + t_{yz},
\end{equation}
\begin{equation}
  t_r \equiv f(\bar x,\bar y,\bar z),
\end{equation}
\begin{equation}
  t_x \equiv f(x,\bar y,\bar z) -t_r,
\end{equation}
\begin{equation}
  t_{xy} \equiv f(x,y,\bar z) - t_x - t_y - t_r.
\end{equation}
Symmetry between $x,y,z$ provides similar expressions for the remaining terms.
The cut-plane evaluations are
\begin{equation}
  f_0 = t_r \equiv f(\bar x,\bar y,\bar z) = \frac{343}{512},
\end{equation}
\begin{equation}
  f_x \equiv f(x,\bar y, \bar z) = \frac{49}{128}(3x^2+1),
\end{equation}
\begin{equation}
  f_{xy} \equiv f(x,y, \bar z) = \frac{7}{32}(3x^2+1)(3y^2+1),
\end{equation}
and similar for the remaining terms.  Expanding the cut-HDMR expression,
\begin{align}
  T[f](x,y,z) = t_r &+ (f_x-t_r) + (f_y-t_r) + (f_y-t_r) +\nonumber\\
  &[f_{xy} - (f_x-t_r) - (f_y-t_r) - t_r]+ \nonumber\\
  &[f_{xz} - (f_x-t_r) - (f_z-t_r) - t_r]+ \nonumber\\
  &[f_{yz} - (f_y-t_r) - (f_z-t_r) - t_r],
\end{align}
and collecting terms
\begin{equation}
  T[f](x,y,z) = t_r - f_x - f_y - f_z + f_{xy} + f_{xz} + f_{yz}.
\end{equation}
The ANOVA terms are recovered by integration of the cut-HDMR expansion.  The ANOVA expansion $H$ is similar in
appearance to the cut-HDMR but uses different definitions; in fact, cut-HDMR is a coarse approximation of the
ANOVA expansion.  Here we use ANOVA to approximate the cut-HDMR expansion, instead of the original model.

\begin{equation}
  H[T](x,y,z) = h_0 + h_x + h_y + h_z + h_{xy} + h_{xz} + h_{yz},
\end{equation}
\begin{equation}
  h_0 \equiv \int_0^1\int_0^1\int_0^1 T[f](x,y,z)\ dx\ dy\ dz,
\end{equation}
\begin{equation}
  h_x \equiv \int_0^1\int_0^1 T[f](x,y,z)\ dy\ dz - h_0,
\end{equation}
\begin{equation}
  h_{xy} \equiv \int_0^1 T[f](x,y,z)\ dz - h_x - h_y - h_0,
\end{equation}
and similarly for the remaining terms.  We evaluate the necessary integrals in the expansion.
\begin{align}
  \int_0^1\int_0^1\int_0^1 T[f](x,y,z)\ dxdydz &= \int_0^1\int_0^1\int_0^1 t_r - f_x - f_y - f_z + f_{xy} +
    f_{xz} + f_{yz}\ dxdydz,\nonumber \\
    &= \frac{343}{512} - 3\left(\frac{49}{64}\right) + 3\left(\frac{7}{8}\right), \nonumber\\
    &= \frac{511}{512}.\nonumber
\end{align}
\begin{equation}
  h_0 = \int_0^1\int_0^1\int_0^1 T[f](x,y,z)\ dxdydz = \frac{511}{512}.
\end{equation}
\begin{align}
  \int_0^1\int_0^1 T[f](x,y,z)\ dxdy &= \int_0^1\int_0^1 t_r - f_x - f_y - f_z + f_{xy} +
    f_{xz} + f_{yz}\ dxdy, \nonumber\\
    &= \frac{259}{512} + \frac{189}{128}z^2. \nonumber
\end{align}
\begin{equation}
  h_z = \int_0^1\int_0^1 T[f](x,y,z)\ dxdy - h_0 = \frac{259}{512} + \frac{189}{128}z^2 - \frac{511}{512} =
  -\frac{63}{128} + \frac{189}{128}z^2.
\end{equation}
\begin{align}
  \int_0^1 T[f](x,y,z)\ dx &= \int_0^1 t_r - f_x - f_y - f_z + f_{xy} + f_{xz} + f_{yz}\ dx, \nonumber\\
  &= \frac{119}{512} + \frac{105}{128}(y^2+z^2)+ \frac{63}{32}y^2z^2,\nonumber
\end{align}
\begin{align}
  h_{yz} &=\int_0^1 T[f](x,y,z)\ dx - h_y - h_z - h_0,\nonumber\\
  &= \frac{119}{512} + \frac{105}{128}(y^2+z^2)+ \frac{63}{32}y^2z^2 - \nonumber\\
  &=\frac{7}{32} - \frac{21}{32}(y^2+z^2) + \frac{63}{32}y^2z^2.
\end{align}
The other terms are obtained similarly, and are symmetric.  In summary,
\begin{align}
  h_0 &= \frac{511}{512}, \\
  h_x &= -\frac{63}{128} + \frac{189}{128}x^2, \\
  h_y &= -\frac{63}{128} + \frac{189}{128}y^2, \\
  h_z &= -\frac{63}{128} + \frac{189}{128}z^2, \\
  h_{xy} &= \frac{7}{32} - \frac{21}{32}(x^2+y^2) + \frac{63}{32}x^2y^2, \\
  h_{xz} &= \frac{7}{32} - \frac{21}{32}(x^2+z^2) + \frac{63}{32}x^2z^2, \\
  h_{yz} &= \frac{7}{32} - \frac{21}{32}(y^2+z^2) + \frac{63}{32}y^2z^2.
\end{align}
It can be shown that the expectation value of any ANOVA expansion term is zero, with the exception of the
first term $h_0$.  Additionally, each term is orthogonal to each other term.  The second moment can thus be
calculated as
\begin{align}
  \langle H[T](x,y,z)^2\rangle &\equiv \int_0^1\int_0^1\int_0^1 (h_0+h_x+h_y+h_z+h_{xy}+h_{xz}+h_{yz})^2\
  dxdydz, \nonumber\\
  &= h_0^2+\int_0^1\int_0^1\int_0^1 h_x^2+h_y^2+h_z^2+h_{xy}^2+h_{xz}^2+h_{yz}^2\ dxdydz.
\end{align}
To obtain the variance $\sigma_\text{tot}^2$, we subtract the square of the mean,
\begin{equation}
  \sigma_\text{tot}^2 = \int_0^1\int_0^1\int_0^1 h_x^2+h_y^2+h_z^2+h_{xy}^2+h_{xz}^2+h_{yz}^2\ dxdydz.
\end{equation}
The partial variance $\sigma_k^2$ of any subset $k$ is the integral of the square of that ANOVA term.
\begin{equation}
  \sigma_x^2 = \sigma_y^2 = \sigma_z^2 = \frac{3969}{20480} \approx0.19379883,
\end{equation}
\begin{equation}
  \sigma_{xy}^2 = \sigma_{xz}^2 = \sigma_{yz}^2 = \frac{49}{1600} =0.030625.
\end{equation}
The total variance is a sum of the partial variances,
\begin{equation}
  \sigma_\text{tot}^2 = 3\left(\frac{3969}{20480}\right) + 3\left(\frac{49}{1600}\right) =
  \frac{68943}{102400} \approx 0.67327.
\end{equation}

The Sobol indices are the ratio of the partial variance to the total,

\begin{equation}
  \mathcal{S}_x =\mathcal{S}_y = \mathcal{S}_z = \frac{135}{469} \approx 0.287846482,
\end{equation}
\begin{equation}
  \mathcal{S}_{xy} =\mathcal{S}_{xz} = \mathcal{S}_{yz} = \frac{64}{1407} \approx 0.045486851.
\end{equation}
%
%
%
%
%
%
%
\section{Global Sobol Sensitivity: Ishigami}
Associated external model: \texttt{ishigami.py}

This model has interesting properties for its sensitivity indices, in that $y_3$ has zero impact alone but a
nonzero impact when coupled with $y_1$.  Additionally, the sinusoidal expression is not trivially represented
by polynomial expansion.  It is listed in \cite{saltelli2000} and has the following form:
\begin{equation}
  u(Y) = \sin(y_1) + a\sin^2(y_2) + b y_3^4\sin(y_1),
\end{equation}
where in this case $a=7$ and $b=0.1$, and all $y_n$ are uniformly distributed on $[-\pi,\pi]$.

The variance and partial variances are as follows:
\begin{align}
  D_\text{tot} &= \frac{a^2}{8} + \frac{b\pi^4}{5} + \frac{b^2\pi^8}{18} + \frac{1}{2}, \\
  D_1 &= \frac{b\pi^4}{5} + \frac{b^2\pi^8}{50} + \frac{1}{2} ,\\
  D_2 &= \frac{a^2}{8}, \\
  D_3 &= 0, \\
  D_{1,2} &= 0, \\
  D_{2,3} &= 0, \\
  D_{1,3} &= \frac{8b^2\pi^8}{225}, \\
  D_{1,2,3} &= 0.
\end{align}
The corresponding variance values and Sobol sensitivities are listed in Table \ref{tab:ishigami sens}.
\begin{table}[h]
  \centering
  \begin{tabular}{c|c|c|c}
    Variable & Partial Variance & Sobol Index & Sobol Total Index\\ \hline
    (total) & 13.8446 & 1 & -\\
    $y_1$         & 4.34589 & 0.3138 & 0.5574\\
    $y_2$         & 6.125   & 0.4424 & 0.4424\\
    $y_3$         & 0       & 0      & 0.2436\\
    $y_1,y_2$     & 0       & 0      & -\\
    $y_1,y_3$     & 3.3737  & 0.2436 & -\\
    $y_2,y_3$     & 0       & 0      & -\\
    $y_1,y_2,y_3$ & 0       & 0      & -\\
  \end{tabular}
  \caption{Ishigami sensitivities and variances}
  \label{tab:ishigami sens}
\end{table}

%
%
%
%
%
%
\section{Sobol G-Function}
Associated external model: \texttt{gFunction.py}

This function developed by Sobol has the benefit of tuning factors $a_n$ that allow the importance of any
particular term to be increased or decreased.  Because of the absolute value, this function is quite
challenging for polynomial expansion.  Documentation can be found in \cite{sobol2003}.  The function is
represented by
\begin{equation}
  u(Y) = \prod_{n=1}^N \frac{|4y_n - 2|+a_n}{1+a_n},
\end{equation}
where $y_n$ are distributed uniformly on [0,1] and $a_n$ are non-negative.  $a_n$ are generally integers, and
smaller values lead to greater impact of corresponding $y_n$.  As in \cite{sudret2007} we use $N=8$ with
$a=[1,2,5,10,20,50,100,500]$.  The partial variances are given by
\begin{equation}
  D_n = \frac{1}{3(1+a_n)^2},
\end{equation}
\begin{equation}
  D_\text{tot} = \prod_{n=1}^N (D_n+1)-1.
\end{equation}
Analytic values for Sobol sensitivities are given in Table \ref{tab:gfunc sens}.
\begin{table}[h]
  \centering
  \begin{tabular}{c|c|c}
    Variable & Sobol sensitivity & Sobol total sensitivity\\ \hline
    $y_1$ & 0.6037 & 0.6342\\
    $y_2$ & 0.2683 & 0.2945\\
    $y_3$ & 0.0671 & 0.0756\\
    $y_4$ & 0.0200 & 0.0227\\
    $y_5$ & 0.0055 & 0.0062\\
    $y_6$ & 0.0009 & 0.0011\\
    $y_7$ & 0.0002 & 0.0003\\
    $y_8$ & 0.0000 & 0.0000\\
  \end{tabular}
  \caption{G-Function sensitivities and variances}
  \label{tab:gfunc sens}
\end{table}


This work consists in solving the two main problems of ``k-means''-like
algorithms described in Section \ref{related} and \ref{problem_definition}:
choosing the $k$ parameter and placing the $k$ initial centroids.
The only assumption is that the dataset is generated from one or more gaussian
disitributions.

\subsection{The choice of $k$}
The most common scenario does not involve a domain expert. As a matter of fact,
no prior knowledge can be used to guess a proper and reasonable value for $k$.\\
The main idea behind the solution proposed in this work is that of exploiting global
and local density analysis to get an initial estimate of the number of clusters
in the data.

More formally, a \emph{peak detection} procedure is used to retrieve
all the peaks (i.e. local maxima) in the density function for each feature\footnote{If
the item considered is a point in an n-dimensional euclidean space, this is equivalent to
finding all the local maxima in the density functions for each axis.} of the items in
the dataset. In this way, a \emph{peak} is a data sample that is larger than
its two neighbour samples.
This results in a list of peaks for each feature space and finally a n-dimensional grid
matching every possible peak among all the features available is created
(see Algorithm \ref{alg:grid_creation}).
\input{pseudo/grid_creation.tex}

As Figure~\ref{grid} shows, the black diamonds on the top and on
the left sub-figures are the peaks found on the density distribution function among all
the points in the dataset, while the orange points in the central sub-figure represent the
grid of all the potential centroids.
In this way, the worst-case scenario is taken into account, allowing this approach not
to miss any potential centroid in the whole dataset\footnote{This approach produces an
over-estimation of the real number of centroids, introducing possible false-negatives.}.

\begin{figure*}
  \center{\includegraphics[width=0.7\textwidth]{unbalance_rescaled_coolfig.png}}
  \caption{The grid with all the potential centroids computed from the peaks.}
  \label{grid}
\end{figure*}

Moreover, among all the potential peaks created, only those which have at least one data
item assigned after a run of the k-means are kept. This allows to discard all the
false-positives that were found creating all the possible combinations in all the feature spaces.
Furthermore, it represents the refinement step for the algorithm.

Finally, the items in this grid are the initial centroids, representing both the value for the
$k$ parameter (i.e. the number of items in this grid) and their coordinates.

\subsection{Centroids refinement}
The other important phase is to find a good positioning of the $k$ initial centroids.
The main idea behind this feature is the following:
\begin{enumerate}
    \item \label{step1} use peaks' locations as the ones of the centroids.
    \item build an ellipse around every centroid with the $(a,b)$ parameters (namely the x-axis
        and the y-axis radii) applying the formula in Equation \ref{ellipse_params} (see
        Algorithm~\ref{alg:find_ellipses}).
    \item merge all the ellipses that intersect with each other (see
        Algorithm~\ref{alg:merge_procedure}).
    \item go to \ref{step1} until convergence or some other exit criterion is met.
\end{enumerate}

\input{pseudo/find_ellipses.tex}
\input{pseudo/merge_procedure.tex}

The formula described in Equation~\ref{ellipse_params} computing the heigth and the width of
the ellipses is the key aspect of the proposed merging strategy.
\begin{equation}
\label{ellipse_params}
    f(\sigma, cdens) = ((\sigma * 2 * 0.35) + (cdens * 0.7)) * 5
\end{equation}
Where:
\begin{itemize}
    \item $\sigma$ is the standard deviation of the gaussian distribution underlying the cluster.
    \item $cdens$ is the value of the gaussian distribution Probability Distribution Function (PDF)
        in the mean.
\end{itemize}

To better understand the reasoning behind this formuls, a more precise explenation of its components
is needed.\\
Given the mean and the standard deviation of a cluster,
$\sigma$ is doubled to take both the sides of the gaussian distribution into account.
The 0.35 and the 0.7 values allow the density in the mean value of the gaussian distribution ($cdens$)
to influence more the size of the ellipse rather than its standard deviation ($\sigma$). Finally, 5 is
an overall scaling factor useful to allow the comparison of cluster with very different densities.

From an higher point of view, Equation \ref{ellipse_params} represents the \emph{Estimated Influence Area}
(EIA) for each cluster. As the name suggests, this depicts (also visually) the area of influence a
cluster has on all the others.\\
As it is possible to see from Figure~\ref{start}, the ellipses represent all the initial
\emph{EIAs} starting from all the potential centroids after discarding
all the centroids that has no item belonging to them (which are clearly false-positives).
If two clusters' estimated influence area has a non-empty intersection, it
means that they can be collapsed and become a single bigger cluster.
The Figures~\ref{start}, \ref{middle} and \ref{end} depicts the trend and the position of the
\emph{EIAs} during an example merging procedure.

Moreover, as it is possible to notice from Figure \ref{end}, the approach described in this
section started with many potential centroids (see Figure~\ref{start}) and after a
few iterations converged finding the 8 clusters in the dataset successfully
(see Figure~\ref{end}).

\begin{figure}[t]
  \center{\includegraphics[width=0.5\textwidth]{unbalance_rescaled_density_0.png}}
  \caption{The first estimated influence areas after filtering some of the false-positives.}
  \label{start}
\end{figure}

\begin{figure}[t]
  \center{\includegraphics[width=0.5\textwidth]{unbalance_rescaled_density_3.png}}
  \caption{The situation after 4 iterations.}
  \label{middle}
\end{figure}

\begin{figure}[t]
  \center{\includegraphics[width=0.5\textwidth]{unbalance_rescaled_density_6.png}}
  \caption{The final outcome of the algorithm.}
  \label{end}
\end{figure}

A noteworthy aspect is that all the aforementioned factors used inside Equation \ref{ellipse_params} have
been set after several tuning stages through empirical tests using many datasets with different
clusters' properties.

Nevertheless, representing the clusters by means of sufficient statistics allows to implement an $O(1)$
merging procedure. More in detail, every cluster is internally represented as a touple with the
following information:
\begin{equation*}
    \left(\left[\sum_{p}^{|C|} feat[0],\dots,\sum_{p}^{|C|} feat[n]\right],|C|\right)
\end{equation*}
In a 2-dimensional euclidean space, this metadata result in a list containing the the sum of all the
points' coordinates among the 2 axes and the number of points within the cluster taken into consideration.
This structure contains the least possible information to compute the centroid's coordinates as the
barycenter of that particolar cluster.

Finally, this merging strategy enables all the \emph{Data Mining Desiderata} mentioned in Section
\ref{intro}. It makes use of sufficient statistics to internally represent every cluster, hence
requiring an overall amount of memory that is linear in the number of peaks ($O(k)$, \emph{limited memory}
property). Furthermore, the \emph{online} behaviour is guaranteed by default, since the centroid
bootstrap and merging procedure always return a solution at any point in time. Finally, it allows
to deal with \emph{straming} data thanks both to its linear memory consumption and to its ability to
work with chunks of data by default, propagating all the sufficient statistics needed to update the
centroids from one iteration to another. This also guarantees that if the computation is stopped
it can be restarted without the need to re-process everything from scratch.

\subsection{Applications}
The approach presented in this work can have two main applications. On the one hand,
this procedure can be used as a bootstrapping phase for a  ``k-means''-like algorithm.
On the other, it can be integrated with other partitional clustering algorithms to
refine the local solution during the computation phase, allowing the system as a
whole to find increasingly better clusters.

Moreover, this work is agnostic with respect
to the metric used to compute the distance between the items. For the seek of
completeness, all the figures presented in this work are computed using a
standard euclidean distance.


% file: problems/tree-traversal.tex

\section{Tree Traversal (UD Problem 4.2)}

\begin{enumerate}[(a)]
  \item Write an algorithm which, given a tree $T$,
    calculates the sum of the depths of all the nodes of $T$.
  \item Write an algorithm which, given a tree $T$ and a positive integer $K$,
    calculates the number of nodes in $T$ at depth $K$.
  \item Write an algorithm which, given a tree $T$,
    checks whether it has any leaf at an even depth.
\end{enumerate}

\subsection{Solution}

\input{algs/sum-of-depths}
\input{algs/nodes-at-depth-K}


% !TeX root = ../libro.tex
% !TeX encoding = utf8
%
%*******************************************************
% Summary
%*******************************************************

\selectlanguage{english}
\chapter{Summary}

Prime numbers are of special importance when it comes to Mathematics in general and, in specific, the branch of Number Theory. Their applications go from purely theoretic results to practical uses like cryptography, which is the base of the security on the Internet.\\

Primality testing has been extensively studied throughout history, and specially during the second half of the $20^{th}$ and $21^{st}$ centuries with the formalitation of complexity theory by \textit{Alan Turing}.\\

There has been many attempts to come up with efficient techniques to prove the primality of a number. The definition of prime numbers provides by itself a primality test: check if some number below $\sqrt{n}$ divides $n$. This test has complexity $O(\sqrt{n})$, which is far from ideal. We want a test that runs in logarithmic time. A great attempt for that is the \textit{Little Fermat's Theorem}, which states that if $n$ is prime, then $a^n \equiv a \mod(n)$ for every $a \in \Z$. With that, we can check some values of $a$ and see if the congruence holds. If it doesn't hold for some value, then $n$ is definitely composite. Otherwise it is probably prime. This almost gives us an efficient test which runs in $\Omega(\log(n))$.\\

Unfortunately, there exists some numbers for which the congruence holds for every value of $a$. They are called \textit{Charmichael Numbers}. Therefore, this test is not valid, but we can make it work with a generalization of the \textit{Little Fermat's Theorem}.\\

Let $n > 1$ and $a \in \Z$. Then $n$ is prime if, and only if,

\begin{equation}
(X + a)^n \equiv X^n + a \mod(n)
\end{equation}

where $X$ is an indeterminate variable.\\

This property leads us to a general, deterministic and inconditional primality test: try the congruence for some $a$ and check if it holds. The problem with this approach is that it gives us a test with complexity $\Omega(n)$ due to the fact that we need to evaluate $n$ coefficients in the left hand side of the congruence.\\

We can speed up the process if we reduce the amount of coefficients to evaluate by restricting the congruence to the ring $\Z_n[X]/(X^r - 1)$, where $r$ is sufficiently small. This way, the congruence above is transformed into the next one below

\begin{equation}
(X + a)^n \equiv X^n + a \mod(n, X^r - 1)
\end{equation}

This congruence still holds if $n$ is prime for every $a \in \Z$ and every $r$. However, it also holds for some values of $a$ and $r$ when $n$ is composite. This property can be almost completely restored if we appropriately choose $r$ and test it for some values of $a$. We are going to prove that $r$ and $a$ are $O(\log^c(n))$ for some constant $c$, which leads us to a deterministic polynomial algorithm.\\

The algorithm is of great interest when it comes to the theory, as it is the first polynomial, deterministic, general and unconditional primality test. This opens the door to the development of better algorithms that also run in polynomial time.\\

However, this algorithm falls behind some other tests that are currently used. For example, the \textit{Miller-Rabin} test is of probabilistic nature, but its runtime is superior, which makes it more eligible when it comes to test for primality in branches like cryptography, where we need to test really big numbers (normally bigger than $1024$ bits) really fast.\\

Even other primality tests that are deterministic and non-polynomial, like the ones based in elliptic curves, perform better than the \textbf{AKS} in most useful cases.\\

An empirical study and comparison with other probabilistic tests is going to let us jump to that conclusion.\\

The algorithm is easy to implement, but some care must be taken when dealing with polynomial multiplication. A good algorithm for polynomial multiplication is needed so that the test is not completely useless. We will see that a bad algorithm for polynomial multiplication can lead to an efficiency of $O^\sim(\log^{31/2}(n))$ instead of $O^\sim(\log^{21/2}(n))$. This is going to make the test struggle for inputs bigger than $16$ bits.\\

The implementation uses C++ as the main programming language for its raw speed and control over the memory. For multiprecision, \textbf{GMP} is the library that we are going to use to implement the algorithm, as it has been extensively tested and is one of the most used libraries. It is written in C, and it has a C++ API, which makes the integration easier.

% Al finalizar el resumen en inglés, volvemos a seleccionar el idioma español para el documento
\selectlanguage{spanish}
\endinput


\subsection{NOT}

We can perform basic operations on inputs.

A simple operation is the unary NOT operator, which returns the reverse of the values of all bits.

We also have binary operators, which take two bits and return another bit. Of note are, AND, OR and XOR.

These operations are a model of Boolean algebra. So there are \(16\) possible binary functions and \(4\) possible unary operators.

As in logic, we can combine elementary logical gates to create other logic gates.

These operations can also be performed on a series of bits, however each individual bit is independent of other bits for these operations.


\paragraph{Confidence interval}
is a random interval, within which a true value of an estimated parameter $\param$ lies. We cannot say
where exactly inside the interval the above-mentioned value is placed.
\[ P(T_L < \param < T_U) \geq 1 - \alpha \]

\paragraph{Confidence interval length}
is defined as $|T_U - T_L|$.

\vfill

\paragraph{Confidence level}
is defined as $ 1 - \alpha $ or $ (1 - \alpha)100\% $, depending on what units we want to use.
$\alpha$ is significance level, described later on.

\paragraph{Confidence interval properties}
with sample size $n$, confidence interval length $l = |T_U - T_L|$, confidence level $c = 1 - \alpha$

\vspace{10pt} \noindent are as follows:
\begin{itemize}[noitemsep,nolistsep]
  \item $c \nearrow \; \Rightarrow \; l \nearrow$

  \item for fixed $c$: sample size $n \nearrow \; \Rightarrow \; l \searrow$

  %\item coś jeszcze?

\end{itemize}


%mainfile: ../../master.tex
\section{Expressions}
\label{sec:Expressions}
This section describes expressions. In The Language Described in This Report, expressions are defined as the constructs that have a value. These can be used together with specific operators to create larger expressions as one would do in mathematics. Apart from combining expressions, they are often used as the right hand side of an assignment, but can also be used for indexing, the condition in conditional statements, for return values and in general all places where a value is expected.

\subsection{Operators}\label{subsec:operators}
First we look at the mathematical operators that can combine expressions.
\setlength{\grammarindent}{100pt}
\begin{grammar}
<Expression> ::= <Expression> <Psevenoperator> <OP6>
 \alt <OP7>

<OP7> ::= <OP7> <Psixoperator> <OP6>
 \alt <OP6>

<OP6> ::= <Pfiveoperator> <OP6>
 \alt <OP5>

<OP5> ::= <OP5> <Pfouroperator> <OP4>
 \alt <OP4>

<OP4> ::= <OP4> <Pthreeoperator> <OP3>
 \alt <OP3>

<OP3> ::= <OP3> <Ptwooperator> <OP2>
 \alt <OP2>

<OP2> ::= <OP2> <Poneoperator> <OP1>
 \alt <OP1>

<OP1> ::= <Pzerooperator> <OP1>
 \alt <OP0>

<OP0> ::= <Operand>
 \alt '(' <Expression> ')'

<PZEROOPERATOR> ::= '(''int' | 'real' | 'char' | 'bool'')'

<PONEOPERATOR> ::= '$\Twedge$' | '\#'

<PTWOOPERATOR> ::= '*' | '/' | '\%'

<PTHREEOPERATOR> ::= '+' | '-'

<PFOUROPERATOR> ::= '=' | '!=' | '$\textless$' | '$\textless$=' | '$\textgreater$' | '$\textgreater$='

<PFIVEOPERATOR> ::= 'NOT'

<PSIXOPERATOR> ::= 'AND' | 'NAND'

<PSEVENOPERATOR> ::= 'OR' | 'XOR' | 'NOR'
\end{grammar}
The precedence of the operators are created in the grammar. Here, the parse tree will be created, such that the $\braket{PSEVENOPERATOR}$ is placed highest in the tree and the $\braket{PZEROOPERATOR}$ is placed lowest as standard, meaning that the precedence goes from $\braket{PZEROOPERATOR}$ to $\braket{PSEVENOPERATOR}$ with zero having highest precedence. This precedence can be overwritten by parentheses, which resets the order so expressions inside parentheses are placed lowest in the tree. All operators have left associativity. An example can be seen in \cref{precedenceExamples} this shows how the parse tree is created from the expression: \\
\begin{center}
$\Tnot a \Tand b \Txor 2 < 3 * (2 + 2) + 4$
\end{center}

\begin{figure}[h]
\centering
\includegraphics[width=0.7\textwidth]{Design/Expressions/precidenceExamples.png}
\caption{The parse tree for \textnormal{\enquote{$\Tnot a \Tand b \Txor 2 < 3 * (2 + 2) + 4$}}} %[XOR [AND [NOT [Bool a]][Bool b]] [< [Integer 2] [+ [* [Integer 3] [+ [Integer 2] [Integer 2]]] [Integer 4]]]]
\label{precedenceExamples}
\end{figure}

We can see that the operator placed lowest in the tree is \enquote{+} even though \enquote{*} has a higher precedence, and should therefore be lower in the tree. This is done because the parentheses overwrite the order, so that everything inside gets higher precedence, as intended. If we insert parentheses to illustrate the implicit precedence in the expression it would look like this:
\begin{center}
$((\Tnot a) \Tand b) \Txor (2 < ((3 * (2 + 2)) + 4))$
\end{center}
In \cref{precedenceExamples} we can see that the \enquote{XOR} operator is placed highest in the tree meaning that it has the lowest precedence. We can also see from the tree that this operator takes the result from \enquote{AND} and \enquote{<} as arguments where \enquote{<} again takes the result from \enquote{+} and the integer 2 as arguments and so on. We can see that the arguments change type as we traverse the tree. We now look at the semantics and what impact this has on the formal type rules.

\subsubsection{Arithmetic Expressions}

First we look at arithmetic expressions. Arithmetic expressions are in TLDR defined as expressions that evaluate to a number, either a real or integer. The operators that create the arithmetic expressions are +, -, *, /, \%, $\Tpot$ and \#. The semantics for these operators are as follows:

\begin{itemize}
\item "+" is a binary operator that adds two numbers of the same type

\begin{align*}
&\inference[$\text{ADD}_\text{L}$]{sEnv \vdash a_1 \Rightarrow_A a_1'}
                    {sEnv \vdash  a_1 + a_2 \Rightarrow_A a_1' + a_2}
&
&\inference[$\text{ADD}_\text{R}$]{sEnv\vdash a_1 \Rightarrow_A a_1'}
                    {sEnv\vdash a_2 + a_1 \Rightarrow_A a_2 + a_1'}
\\\\
&\inference[$\text{ADD}_\text{V}$]{}
                    {v_1 + v_2 \Rightarrow_A v}
                    {, v_1 + v_2 = v}
\end{align*}

\item "-" is a binary operator that subtracts two numbers of the same type

\begin{align*}
&\inference[$\text{SUB}_\text{L}$]{sEnv\vdash a_1 \Rightarrow_A a_1'}
                    {sEnv\vdash a_1 - a_2 \Rightarrow_A a_1' - a_2}
&
&\inference[$\text{SUB}_\text{R}$]{sEnv\vdash a_1 \Rightarrow_A a_1'}
                    {sEnv\vdash a_2 - a_1 \Rightarrow_A a_2 - a_1'}
\\\\
&\inference[$\text{SUB}_\text{V}$]{}
                    {v_1 - v_2 \Rightarrow_A v}
                    {, v_1 - v_2 = v}
\end{align*}

\item "*" is a binary operator that multiplies two numbers of the same type

\begin{align*}
&\inference[$\text{MULT}_\text{L}$]{sEnv\vdash a_1 \Rightarrow_A a_1'}
                     {sEnv\vdash a_1 * a_2 \Rightarrow_A a_1' * a_2}
&
&\inference[$\text{MULT}_\text{R}$]{sEnv\vdash a_1 \Rightarrow_A a_1'}
                     {sEnv\vdash a_2 * a_1 \Rightarrow_A a_2 * a_1'}
\\\\
&\inference[$\text{MULT}_\text{V}$]{}
                     {v_1 * v_2 \Rightarrow_A v}
                     {, v_1 * v_2 = v}
\end{align*}

\item "/" is a binary operator that divides two numbers of the same type

\begin{align*}
&\inference[$\text{DIV}_\text{L}$]{sEnv\vdash a_1 \Rightarrow_A a_1'}
                    {sEnv\vdash a_1 / a_2 \Rightarrow_A a_1' / a_2}
&
&\inference[$\text{DIV}_\text{R}$]{sEnv\vdash a_1 \Rightarrow_A a_1'}
                    {sEnv\vdash a_2 / a_1 \Rightarrow_A a_2 / a_1'}
\\\\
&\inference[$\text{DIV}_\text{V}$]{}
                    {v_1 / v_2 \Rightarrow_A v}
                    {, \frac{v_1}{v_2} = v}
\end{align*}

\item "\%" is a binary operator that returns the remainder of a floored division of two numbers of the same type

\begin{align*}
&\inference[$\text{MOD}_\text{L}$]{sEnv\vdash a_1 \Rightarrow_A a_1'}
                    {sEnv\vdash a_1 \% a_2 \Rightarrow_A a_1' \% a_2}
&
&\inference[$\text{MOD}_\text{R}$]{sEnv\vdash a_1 \Rightarrow_A a_1'}
                    {sEnv\vdash a_2 \% a_1 \Rightarrow_A a_2 \% a_1'}
\\\\
&\inference[$\text{MOD}_\text{V}$]{}
                    {v_1 \% v_2 \Rightarrow_A v}
                    {, v_1 \;\; \textrm{mod} \;\; v_2 = v}
\end{align*}

\item "\^{}" is a binary operator that lifts the first number to the power of the second number

\begin{align*}
&\inference[$\text{POW}_\text{L}$]{sEnv\vdash a_1  \Rightarrow_A a_1'}
                    {sEnv\vdash a_1 \Twedge a_2 \Rightarrow_A a_1' \Twedge a_2}
&
&\inference[$\text{POW}_\text{R}$]{sEnv\vdash a_1 \Rightarrow_A a_1'}
                    {sEnv\vdash a_2 \Twedge a_1 \Rightarrow_A a_2 \Twedge a_1'}
\\\\
&\inference[$\text{POW}_\text{V}$]{}
                    {v_1 \Twedge v_2 \Rightarrow_A v}
                    {, v_1 ^ {v_2} = v}
\end{align*}

\item "\#" is a binary operator that roots the first operand to the second operand
\begin{align*}
&\inference[$\text{ROOT}_\text{L}$]{sEnv\vdash a_1 \Rightarrow_A a_1'}
                    {sEnv\vdash a_1 \# a_2 \Rightarrow_A a_1' \# a_2}
&
&\inference[$\text{ROOT}_\text{R}$]{sEnv\vdash a_1 \Rightarrow_A a_1'}
                    {sEnv\vdash a_2 \# a_1 \Rightarrow_A a_2 \# a_1'}
\\\\
&\inference[$\text{ROOT}_\text{V}$]{}
                    {v_1 \# v_2 \Rightarrow_A v}
                    {, \sqrt[v_1]{v_2} = v}
\end{align*}

\item "( )" Parentheses gives what they surrounds the highest precedence.

\begin{align*}
&\inference[$\text{PARENS}_\text{A}$]{sEnv\vdash a_1 \Rightarrow_A a_1'}
                       {sEnv\vdash (a_1) \Rightarrow_A (a_1')}
&
&\inference[$\text{PARENS}_\text{V}$]{}
                       {(v) \Rightarrow_A v}
\end{align*}
\end{itemize}

For simplicity we create the following set since all type rules for these are the same.

\begin{center}
$\Taop = \left\{ {+, -, *, /, \%, \; \Tpot \;} \right\}$
\end{center}

Due to the semantics of all \enquote{AOP} operators these cannot evaluate to a real, if the two inputs are both integers. Therefore all operators in this set can take either two integers and evaluate to an integer or two reals and evaluate to a real.

The \enquote{\#} operator is a bit different. When taking the an integer root of another integer it can still evaluate to a real, for instance $\sqrt[2]{2}$ evaluates to $1.4142\dots$ Therefore both rules for \enquote{\#} evaluate to a real.

No operator can take a combination of real and integer. This is done since all implicit type casts are avoided in TLDR. The reason for this is that the language is designed to give the programmer all errors as early as possible, preferably on compile-time, see \cref{typesys}. With no implicit type casts the programmer is always aware when type casts are performed and will therefore not be as prone to make runtime type errors.

\begin{align*}
&\inference[$\text{EXPR}_{\Tint,\Tint}$]{\Tenv e_1  : \Tint &
                       \Tenv e_2 : \Tint}
                    {\Tenv e_1 \mathbin{\text{op}} e_2 : \Tint},  \text{op} \in \Taop
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\\\\
&\inference[$\text{EXPR}_{\Treal,\Treal}$]{\Tenv e_1 : \Treal &
                       \Tenv e_2 : \Treal}
                    {\Tenv e_1 \mathbin{\text{op}} e_2 : \Treal},  \text{op} \in \Taop
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\\\\
&\inference[$\text{ROOT}_{\Tint,\Tint}$]{\Tenv e_1 : \Tint &
                       \Tenv e_2 : \Tint}
                    {\Tenv e_1 \mathbin{\#} e_2 : \Treal}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\\\\
&\inference[$\text{ROOT}_{\Treal,\Treal}$]{\Tenv e_1 : \Treal &
                       \Tenv e_2 : \Treal}
                    {\Tenv e_1 \mathbin{\#} e_2 : \Treal}
\end{align*}

\subsubsection{Boolean Expressions}
Boolean expressions are in TLDR defined as expressions that takes boolean types as arguments and returns a boolean type. The boolean expressions can be constructed from the operators AND, NAND, OR, NOR, XOR, NOT.

The following are the semantics for all boolean operators and their truth table.

\begin{itemize}
\item "AND" is a binary operator that returns true if both values are true. False otherwise
\begin{figure}[H]
\centering
  \begin{minipage}[c]{0.45\linewidth}
	  \centering
    \begin{align*}
    &\inference[$\text{AND}_1$]{sEnv \vdash b_1 \Rightarrow_B \bot}
                               {sEnv \vdash b_1 \Tand b_2 \Rightarrow_B \bot}
    \\\\
    &\inference[$\text{AND}_2$]{sEnv \vdash b_1 \Rightarrow_B \top \\ sEnv \vdash b_2 \Rightarrow_B \bot}
                               {sEnv \vdash b_1 \Tand b_2 \Rightarrow_B \bot}
    \\\\
    &\inference[$\text{AND}_3$]{sEnv \vdash b_1 \Rightarrow_B \top \\ sEnv \vdash b_2 \Rightarrow_B \top}
                               {sEnv \vdash b_1 \Tand b_2 \Rightarrow_B \top}
    \end{align*}
  \end{minipage}
	\quad
	\begin{minipage}[c]{0.45\linewidth}
	  \centering
    \begin{tabular}{ | c | c | c | }
      \hline
      $e_1$ & $e_2$ & $e_1 \Tand e_2$ \\\hline
      $\bot$ & $\bot$ & $\bot$ \\\hline
      $\bot$ & $\top$ & $\bot$ \\\hline
      $\top$ & $\bot$ & $\bot$ \\\hline
      $\top$ & $\top$ & $\top$ \\\hline
    \end{tabular}
  \end{minipage}
\end{figure}

\item "OR" is a binary operator that returns true if at least one value is true. False otherwise

\begin{align*}
&\inference[OR]{}
                 {sEnv \vdash b_1 \Tor b_2 \Rightarrow_B \Tnot(\Tnot b_1 \Tand \Tnot b_2)}
\end{align*}

\begin{center}
\begin{tabular}{ | c | c | c | }
\hline
$e_1$ & $e_2$ & $e_1 \Tor e_2$ \\\hline
$\bot$ & $\bot$ & $\bot$ \\\hline
$\bot$ & $\top$ & $\top$ \\\hline
$\top$ & $\bot$ & $\top$ \\\hline
$\top$ & $\top$ & $\top$ \\\hline
\end{tabular}
\end{center}

\item "XOR" is a binary operator that returns true if only one operand is true. False otherwise

\begin{align*}
&\inference[XOR]{}
                  {sEnv \vdash b_1 \Txor b_2 \Rightarrow_B (\Tnot(b_1 \Tand b_2)) \Tand (b_1 \Tor b_2)}
\end{align*}

\begin{center}
\begin{tabular}{ | c | c | c | }
\hline
$e_1$ & $e_2$ & $e_1 \Txor e_2$ \\\hline
$\bot$ & $\bot$ & $\bot$ \\\hline
$\bot$ & $\top$ & $\top$ \\\hline
$\top$ & $\bot$ & $\top$ \\\hline
$\top$ & $\top$ & $\bot$ \\\hline
\end{tabular}
\end{center}

\item "NOR" is OR negated.

\begin{align*}
&\inference[NOR]{}
                   {sEnv \vdash b_1 \Tnor b_2 \Rightarrow_B \Tnot( b_1 \Tor b_2 )}
\end{align*}

\begin{center}
\begin{tabular}{ | c | c | c | }
\hline
$e_1$ & $e_2$ & $e_1 \Tnor e_2$ \\\hline
$\bot$ & $\bot$ & $\top$ \\\hline
$\bot$ & $\top$ & $\bot$ \\\hline
$\top$ & $\bot$ & $\bot$ \\\hline
$\top$ & $\top$ & $\bot$ \\\hline
\end{tabular}
\end{center}

\item "NOT" is a unary operator that returns the opposite value of the operand.

\begin{figure}[H]
\centering
\begin{minipage}[c]{0.45\linewidth}
\centering
\begin{align*}
&\inference[$\text{NOT}_\top$]{sEnv \vdash b_1 \Rightarrow_B \top}
                       {sEnv \vdash \Tnot b_1 \Rightarrow_B \bot}
\\\\
&\inference[$\text{NOT}_\bot$]{sEnv \vdash b_1 \Rightarrow_B \bot}
                       {sEnv \vdash \Tnot b_1 \Rightarrow_B \top}
\end{align*}
\end{minipage}
\quad
\begin{minipage}[c]{0.45\linewidth}
\centering
\begin{tabular}{ | c | c | }
\hline
$e_1$ & $ \Tnot e_1$ \\\hline
$\bot$ & $\top$ \\\hline
$\top$ & $\bot$ \\\hline
\end{tabular}
\end{minipage}
\end{figure}

\item "NAND" is a binary operator that returns true if none or a single operand is true. False otherwise.

\begin{align*}
&\inference[NAND]{}
                   {sEnv \vdash b_1 \Tnand b_2 \Rightarrow_B \Tnot( b_1 \Tand b_2 )}
\end{align*}

\begin{center}
\begin{tabular}{ | c | c | c | }
\hline
$e_1$ & $e_2$ & $e_1 \Tnand e_2$ \\\hline
$\bot$ & $\bot$ & $\top$ \\\hline
$\bot$ & $\top$ & $\top$ \\\hline
$\top$ & $\bot$ & $\top$ \\\hline
$\top$ & $\top$ & $\bot$ \\\hline
\end{tabular}
\end{center}

\item "()" Parentheses gives what they surround the highest precedence.

\begin{align*}
&\inference[$\text{PARENS}_\text{B}$]{sEnv \vdash b_1 \Rightarrow_B b_1'}
                       {sEnv \vdash (b_1) \Rightarrow_B (b_1')}
\end{align*}
\end{itemize}

All boolean operators, except NOT, takes two booleans and returns a boolean. For the simplicity of the type rules we create the Boolean Operator (BOP):

\begin{center}
$\Tbop = \left\{ {\text{AND, NAND, OR, NOR, XOR}} \right\}$
\end{center}

Note that \enquote{NOT} is not included in the set since it only takes one boolean as input and return a boolean.

\begin{align*}
&\inference[$\text{BOOL}_{BOP}$]{\Tenv e_1 : \Tbool &
                       \Tenv e_2 : \Tbool}
                    {\Tenv e_1 \mathbin{\text{op}} e_2 : \Tbool}, \text{op} \in \Tbop
\\\\
&\inference[$\text{BOOL}_{NOT}$]{\Tenv e : \Tbool}
                    {\Tenv \mathbin{\text{NOT}} \; e : \Tbool}
\end{align*}

\subsubsection{Logical Operations}
\label{sec:logicOps}

Logical operators are defined as operators that take numbers, i.e. integers and reals, and evaluate to boolean types.

For logical comparisons we chose \enquote{=}. This was done in accordance with the goal of keeping a natural mathematical language. In mathematics \enquote{=} is read as \enquote{is equal to} or simply \enquote{equals}, and is used for stating that two parts are equivalent to each other. Sometimes mathematicians use this statement in a contradicting manner, where they expect to prove the statement to be false. It is from this perspective of being a statement, either true or false, that we chose \enquote{=} to be a logical comparison. The same arguments exist for other types of logical operations.

\begin{itemize}
\item "=" is a binary operator that compares the two operands for equality. Returns true if equal. False otherwise.

\begin{align*}
&\inference[$\text{EQUALS}_\text{L}$]{sEnv \vdash a_1 \Rightarrow_B a_1'}
                    {sEnv \vdash a_1 = a_2 \Rightarrow_B a_1' = a_2}
&
&\inference[$\text{EQUALS}_\text{R}$]{sEnv \vdash a_1 \Rightarrow_B a_1'}
                    {sEnv \vdash a_2 = a_1 \Rightarrow_B a_2 = a_1'}
\\\\
&\inference[$\text{EQUALS}_\text{V1}$]{}
                    {v_1 = v_2 \Rightarrow_B \top}
                    {, v_1 = v_2}
&
&\inference[$\text{EQUALS}_\text{V2}$]{}
                    {v_1 = v_2 \Rightarrow_B \bot}
                    {, v_1 \neq v_2}
\end{align*}

\item "!=" is a binary operator that compares the two operands for equality. Returns true if not equal. False otherwise.

\begin{align*}
&\inference[$NEQUALS$]{}
                    {sEnv \vdash a_1 != a_2 \Rightarrow_B \Tnot (a_1 = a_2)}
\end{align*}

\item "<" is a binary operator that compares the two operands. Returns true if the first operand is strictly less than the second operand. False otherwise.

\begin{align*}
&\inference[$\text{LT}_\text{L}$]{sEnv \vdash a_1 \Rightarrow_A a_1'}
                    {sEnv \vdash a_1 < a_2 \Rightarrow_A a_1' < a_2}
&
&\inference[$\text{LT}_\text{R}$]{sEnv \vdash a_1 \Rightarrow_A a_1'}
                    {sEnv \vdash a_2 < a_1 \Rightarrow_A a_2 < a_1'}
\\\\
&\inference[$\text{LT}_\text{V1}$]{}
                    {v_1 < v_2 \Rightarrow_B \top}
                    {, v_1 < v_2}
&
&\inference[$\text{LT}_\text{V2}$]{}
                    {v_1 < v_2 \Rightarrow_B \bot}
                    {, v_1 \geq v_2}
\end{align*}

\item "<=" is a binary operator that compares the two operands. Returns true if the first operand is less than or equal to the second operand. False otherwise.

\begin{align*}
&\inference[$LTEQ$]{}
                    {sEnv \vdash a_1 <= a_2 \Rightarrow_A (a_1 < a_2) \Tor (a_1 = a_2)}
\end{align*}

\item ">" is a binary operator that compares the two operands. Returns true if the first operand is strictly greater than the second operand. False otherwise.

\begin{align*}
&\inference[$\text{GT}_\text{L}$]{sEnv \vdash a_1 \Rightarrow_A a_1'}
                    {sEnv \vdash a_1 > a_2 \Rightarrow_A a_1' > a_2}
&
&\inference[$\text{GT}_\text{R}$]{sEnv \vdash a_1 \Rightarrow_A a_1'}
                    {sEnv \vdash a_2 > a_1 \Rightarrow_A a_2 > a_1'}
\\\\
&\inference[$\text{GT}_\text{V1}$]{}
                    {v_1 > v_2 \Rightarrow_B \top}
                    {, v_1 > v_2}
&
&\inference[$\text{GT}_\text{V2}$]{}
                    {v_1 > v_2 \Rightarrow_B \bot}
                    {, v_1 \leq v_2}
\end{align*}

\item ">=" is a binary operator that compares the two operands. Returns true if the first operand is greater than or equal to the second operand. False otherwise.

\begin{align*}
&\inference[$GTEQ$]{}
                    {sEnv \vdash a_1 >= a_2 \Rightarrow_A (a_1 > a_2) \Tor (a_1 = a_2)}
\end{align*}
\end{itemize}

Since all logical operators take a number and returns a boolean we create the set Logical Operators (LOP):
\begin{center}
$\Tlop = \left\{ {=, !=, <, <=, >, >=} \right\}$
\end{center}

\begin{align*}
&\inference[$\text{BOOL}_{\Tint,\Tint}$]{\Tenv e_1 : \Tint &
                       \Tenv e_2 : \Tint}
                    {\Tenv e_1 \mathbin{\text{op}} e_2 : \Tbool}, \text{op} \in \Tlop
\\\\
&\inference[$\text{BOOL}_{\Treal,\Treal}$]{\Tenv e_1 : \Treal &
                       \Tenv e_2 : \Treal}
                    {\Tenv e_1 \mathbin{\text{op}} e_2 : \Tbool}, \text{op} \in \Tlop
\end{align*}

\subsection{Operands}\label{operands}

When creating expressions, one also needs operands to use in the operators. The operands are as follows in formal syntax:
\begin{grammar}
<Operand>	::= <Block>
 \alt <Integer>
 \alt <Real>
 \alt <Boolean>
 \alt <Literals>
 \alt <Invocation>
\end{grammar}
Some of these operands can evaluate something that is neither an Integer, Real or Boolean; for instance lists, actors and structures. This means that, due to the type system seen in \cref{subsec:operators}, the operand cannot be an argument to any operand. This however does not mean that it cannot be an expression, they can because all operands by themselves also have value and are an expression.

\subsubsection{Integer}
The integer is a literal integer that is written directly in the code, for instance in the statement \enquote{$\Tlet \Tx := 2;$}, \enquote{2} is an integer literal. All integers are interpreted as decimal numbers and can be any combination of the symbols 0-9 in any length. They can be either positive or negative, illustrated with the symbol \enquote{-}, but may not start with a 0 unless it is only the number \enquote{0}. This is done since readability is weighted higher than writeability, see \cref{analsum}, and it was assessed for instance \enquote{00023} is not very readable compared to \enquote{23}.

\paragraph{Syntax}

\begin{grammar}
<Integer> ::= '-'?[1-9][0-9]* | '0'
\end{grammar}

\paragraph{Semantics}

\begin{align*}
\intertext{The num rule is mapping numerals to numbers.}
&\inference[NUM]{}
                  {sEnv \vdash n \Rightarrow_A v}
                  {, \mathcal{N}(n) = v}
\end{align*}

\paragraph{Type Rules}

\begin{align*}
\intertext{The type of a integer literal is simply $int$}
&\inference[NUM]{\Tenv n:\Tint}
                 {\Tenv n: \Tint}
\end{align*}

\subsubsection{Real}
The real is a literal real that is written directly in the code, for instance in the statement \enquote{$\Tlet \Tx := 4.3;$}, \enquote{4.3} is an real literal. All real are interpreted as decimal numbers and is an integer followed by a \enquote{.} and then any combination of the symbols 0-9. They can like integers be either positive or negative, indicated with the symbol \enquote{-}. There must be at least one number both before and after the \enquote{.}. This means that neither \enquote{.1} and \enquote{1.} are allowed. This is done since it was assessed that for instance \enquote{-0.1} is more readable than \enquote{-.1}.

\paragraph{Syntax}

\begin{grammar}
<Real> ::= ('-'?[1-9][0-9]* | '0')'.'[0-9]+
\end{grammar}

\paragraph{Semantics}

\begin{align*}
\intertext{The real rule is mapping numerals to reals}
&\inference[REAL]{}
                  {sEnv \vdash n \Rightarrow_A v}
                  {, \mathcal{R}(n) = v}
\end{align*}

\paragraph{Type Rules}

\begin{align*}
&\inference[REAL]{\Tenv n_1:\Tint & \Tenv n_2:\Tint}
                 {\Tenv n_1.n_2: \Treal}
\end{align*}

\subsubsection{Boolean}
Booleans are symbols that hold the value of either true or false. For instance we can in mathematics write \enquote{2 > 3}, this is a contradiction and therefore false.

\paragraph{Syntax}

\begin{grammar}
<Boolean> ::= 'true' | 'false'
\end{grammar}

\paragraph{Semantics}

\begin{align*}
\intertext{The bool rules are mapping boolean literals to boolean values}
&\inference[TRUE]{}
                   {\Braket{\Ttrue,sEnv} \Rightarrow_B \top}
&\inference[FALSE]{}
                   {\Braket{\Tfalse,sEnv} \Rightarrow_B \bot}
\end{align*}

\paragraph{Type Rules}

\begin{align*}
\intertext{The type of a bool literal is simply a bool type}
&\inference[BOOL]{\Tenv b:\Tbool}
                 {\Tenv b: \Tbool}
\end{align*}

\subsubsection{Invocation}\label{subsubsec:invocation}
An invocation in TLDR is defined as a series of characters, from here on referred to as the symbol, which is associated with a value. The value can either depend on arguments, a function, or be independent from any input, a constant or variable. If the value depends on arguments the evaluation is done lazy, meaning that the output value is only associated with the symbol when the symbol is invoked. Otherwise the symbol is associated with the functionality, i.e. its statements and how to evaluate the function.

We differentiate between direct and indirect arguments. Direct arguments are put inside parentheses after the symbol. These arguments can be any value as long as the types follow the type rules. A symbol can be dependent on a value, that is not given as a direct input, but outside its body, as long as it is in scope. We call these indirect arguments. If a function is dependent on indirect arguments we call it impure. Note that this property will be important in \cref{subsubsec:BasicActorFunctionality}.

A symbols dependence on arguments, both direct and indirect, is illustrated with parentheses. Inside the parentheses are the direct arguments. Note that these parentheses can be empty, if the symbol is only dependent on indirect or no arguments. For instance if x is declared as a symbol that takes an integer and returns an integer, see \cref{subsubsec:BasicActorFunctionality} for declarations, writing \enquote{y := x} would mean that y also has to be a function that takes an integer and returns an integer, but writing \enquote{z := x(2)} means that z is an integer. Any symbol, both invoked and uninvoked, can be considered expressions, i.e. having a value.

This syntax was decided since it is very close to mathematics, where functions take arguments in parentheses.
\begin{grammar}
<Invocation> ::= <Identifier> ('(' (<Expression> (',' <Expression>)*)? ')')?;

<Identifier> ::= 'me'
 \alt <Id>
 \alt <Id> <Accessor>

<Id> ::= [a-zA-Z][a-zA-Z\_0-9]*-('let' | 'var' | 'bool' | 'integer' | 'real' | 'char' | 'struct' | 'actor' | 'receive' | 'send' | 'spawn' | 'return' | 'for' | 'in' | 'if' | 'else' | 'while' | 'die' | 'me');

<Accessor> ::= '.' <Identifier>
 \alt '.' '[' <Expression> ']'

<Primitive> ::= 'int' | 'real' | 'char' | 'bool'
\end{grammar}

\begin{align*}
\intertext{A symbol, $x$, is evaluated to a value by finding the symbol in the symbol environment $sEnv$.}
&\inference[$\text{INVOKE}_{A1}$]{\Braket{S,sEnv} \Rightarrow_A v}
                  {\Braket{x,sEnv} \Rightarrow_A v}
                  {,sEnv(x) = \Braket{S,\epsilon}}
\intertext{A function, $x_1$ is invoked with the parameter $x_2$ and evaluated to a value by finding $x_1$ in the symbol environment $sEnv$. This lookup results in the statement $S_1$ which is the body of the function, and the formal parameter $x_1'$. The actual parameter $x_2$ is then looked up in the symbol environment $sEnv$ for the statement $S_2$ which is the body of the actual parameter, and $x_2'$ which is the parameter for $x_2$. To evaluate the function, the formal parameter $x_1'$ in the symbol environment is assigned to the result of the lookup of the formal parameter $x_2$, $S_2$ and $x_2'$.}
&\inference[$\text{INVOKE}_{A2}$]{\Braket{S_1,sEnv[x_1' \mapsto \Braket{S_2,x_2'}]} \Rightarrow_A v}
                  {\Braket{x_1(x_2),sEnv} \Rightarrow_A v}
                  {,sEnv(x_1) = \Braket{S_1,x_1'}, sEnv(x_2) = \Braket{S_2,x_2'}}
\intertext{A symbol $x$ is evaluated to a boolean value of true, when the body $S$ of the symbol is found in the symbol environment $sEnv$, evaluates to true.}
&\inference[$\text{INVOKE}_{B1\top}$]{\Braket{S,sEnv} \Rightarrow_B \top}
                  {\Braket{x,sEnv} \Rightarrow_B \top}
                  {,sEnv(x) = \Braket{S,\epsilon}}
\intertext{This rule is the same as the above, but in this case, the body of the symbol $x$ evaluates to false, and therefore so does $x$ too.}
&\inference[$\text{INVOKE}_{B1\bot}$]{\Braket{S,sEnv} \Rightarrow_B \bot}
                  {\Braket{x,sEnv} \Rightarrow_B \bot}
                  {,sEnv(x) = \Braket{S,\epsilon}}
\intertext{This rule is similar to the rule $INVOKE_{A2}$, but instead of the value being a arithmetic value, it is a boolean value.}
&\inference[$\text{INVOKE}_{B2\top}$]{\Braket{S_1,sEnv[x_1' \mapsto \Braket{S_2,x_2'}]} \Rightarrow_B \top}
                  {\Braket{x_1(x_2),sEnv} \Rightarrow_B \top}
                 {,sEnv(x_1) = \Braket{S_1,x_1'}, sEnv(x_2) = \Braket{S_2,x_2'}}
\intertext{This rule is the same as $INVOKE_{B2\top}$, but instead of the boolean value being true, it is false.}
&\inference[$\text{INVOKE}_{B2\bot}$]{\Braket{S_1,sEnv[x_1' \mapsto \Braket{S_2,x_2'}]} \Rightarrow_B \bot}
                  {\Braket{x_1(x_2),sEnv} \Rightarrow_B \bot}
                 {,sEnv(x_1) = \Braket{S_1,x_1'}, sEnv(x_2) = \Braket{S_2,x_2'}}
\end{align*}

\begin{align*}
\intertext{The type of an invocation can be looked up in the type environment $E$.}
&\inference[INVOKE]{\Tenv x: \Tt}
                 {\Tenv x: \Tt}
\end{align*}

\subsubsection{Literals}
There are four types of literals other than the numerical types and booleans. These are \emph{char}, \emph{list}, \emph{structLiteral} and \emph{tuple}.\\
A \emph{char} is a single alphanumerical character, written in the code with apostrophes on either side. An example of a char is \emph{'a'}.\\
A \emph{list} is a collection type which can contain an arbitrary number of elements of a single type. A \emph{list} is written in the code inside square brackets, with the elements either separated by commas or written as a range from one number to another signified by \emph{..}. Two examples of a \emph{list} are \emph{[1,2,3]} and \emph{[1 .. 10]}.\\
A \emph{structliteral} is an initialisation of a struct. If we have a struct \emph{struct s := \{x:int; y:real;\};}, a literal of \emph{s} could for example be \emph{\{x := 3; y:= 2.3;\}}.\\
A \emph{tuple} is much like a \emph{struct} in the way it can contain an arbitrary amount of fields with no restrictions on types. In \emph{tuples} however, the fields have no name. An example of a \emph{tuple} is \emph{(1, 2.5 , \enquote{hello world} )}.\\
\begin{grammar}
<Literals> ::= <String>
 \alt <Char>
 \alt <List>
 \alt <StructLiteral>
 \alt <Tuple>

<String> ::= '\textquotedbl' (U+0020 .. U+007E)* '\textquotedbl'

<Char> ::= '\textquotesingle' U+0020 .. U+007E '\textquotesingle'
\end{grammar}

\subsubsection{Block}
In TLDR, blocks are a way to encapsulate statements, see \cref{sec:statements}. This is done via curly brackets, \enquote{\{} starting the block, and \enquote{\}} ending it.

\begin{grammar}
<Block> ::= '\{' <Body> '\}'

<Body> ::= <Body> ';' <Statement>
 \alt <Body> ';'
 \alt <Statement>
\end{grammar}

Note the that the last semicolon in a block is optional.

Statements do not have a value in TLDR, but they update the state of the program or at least have the possibility to do so. When a block is evaluated the statements inside it are run.

\begin{align*}
&\inference[$\text{BLOCK}_{S1}$]{\Braket{S,sEnv} \Rightarrow_S \Braket{S',sEnv'}}
                                {\Braket{\{S\},sEnv} \Rightarrow_S \Braket{\{S'\},sEnv'}}
\end{align*}
A block is bit different than most other constructs in TLDR in that it is a statement, but can at the same time be an expression too. If the last that is run inside the block is a statement the block itself is only a statement:
\begin{align*}
&\inference[$\text{BLOCK}_{S2}$]{\Braket{S,sEnv} \Rightarrow_S sEnv'}
                                {\Braket{\{S\},sEnv} \Rightarrow_S sEnv'}
\end{align*}
An expression by itself is in principle a statement in TLDR, for instance \enquote{2 + 2;}. Note that this is not allowed in an expression, for instance \enquote{2 + 2; + 4}. An expression which takes the form of a statement, will not have value, but can give a block value if it is the last thing that is evaluated in it. If it is, the block takes the value of this expression thereby making the block an expression. So for instance the block:
\begin{lstlisting}
{
  let a:int := 5;
  a * 2
}
\end{lstlisting}
will have the value 10 when evaluated. This is formally, for aritmetic expressions, described as:
\begin{align*}
&\inference[$\text{BLOCK}_{A3}$]{\Braket{x,sEnv} \Rightarrow_A \Braket{x',sEnv}}
                         {\Braket{\{x\},sEnv} \Rightarrow_A \Braket{\{x'\},sEnv}}
&
&\inference[$\text{BLOCK}_{A4}$]{\Braket{x,sEnv} \Rightarrow_A v}
                         {\Braket{\{x\},sEnv} \Rightarrow_A v}
\end{align*}
And for boolean and logical expressions:
\begin{align*}
&\inference[$\text{BLOCK}_{B1}$]{\Braket{x,sEnv} \Rightarrow_B \Braket{x',sEnv}}
                         {\Braket{\{x\},sEnv} \Rightarrow_B \Braket{\{x'\},sEnv}}
\\\\
&\inference[$\text{BLOCK}_{B2}$]{\Braket{x,sEnv} \Rightarrow_B \top}
                         {\Braket{\{x\},sEnv} \Rightarrow_B \top}
&
&\inference[$\text{BLOCK}_{B3}$]{\Braket{x,sEnv} \Rightarrow_B \bot}
                         {\Braket{\{x\},sEnv} \Rightarrow_B \bot}
\end{align*}
The keyword \enquote{return} forces an exit from the block and gives the block the value of whatever that expression that is after the keyword. This is done simply by inserting the expression and removing any further statements.
\begin{align*}
&\inference[$\text{RETURN}_{1}$]{}
                   {\Braket{\{\Treturn \Tx;S\},sEnv} \Rightarrow_S \Braket{\{x\},sEnv}}
\\\\
&\inference[$\text{RETURN}_{2}$]{}
                   {\Braket{\{\Treturn \Tx\},sEnv} \Rightarrow_S \Braket{\{x\},sEnv}}
\\\\
&\inference[$\text{RETURN}_{3}$]{}
                   {\Braket{\{\Treturn;S\},sEnv} \Rightarrow_S \Braket{sEnv}}
\\\\
&\inference[$\text{RETURN}_{3}$]{}
                   {\Braket{\{\Treturn\},sEnv} \Rightarrow_S \Braket{sEnv}}
\end{align*}
If the block is not an expression we say it has the return type \enquote{unit}. Any piece of code, that does not evaluate to a value, is said to be of type unit and cannot be used in an assignment. Note that all statements by themselves have the type unit, but that a block only has the type unit when evaluated.

\begin{align*}
\intertext{Blocks have the type of the last statement run in the block.}
&\inference[BLOCK]{\Tenv s_1: \Tt & \Tenv s_2: \Tt'}
                 {\Tenv \{s_1; s_2\} : \Tt'}
\end{align*}


\section{The dispersion method in three and two dimensions}\label{sec:3D dispersion}
In this section we explicitly derive the dispersion formalism in both three and two dimensions by renormalizing the contact interaction on a lattice.
This non-perturbative renormalization allows to extract regularization independent observables (see also \Refs{Seki:2005ns, Epelbaum:2018zli}).
We show that it is possible to tune the contact strength parameter in a finite volume for a given discretization scheme such that one directly obtains continuum infinite volume results when using the dispersion formalism---without any further extrapolation.

\subsection{Three dimensions}

According to \eqref{T matrix}, \eqref{I0} and \eqref{spherical FD}, we find that the phase shifts are related to the contact interaction by
\begin{equation}\label{eq:blah blah}
	p \cot \delta_3(p)
	= \lim\limits_{\Lambda \to \infty}\frac{2 \pi}{\mu}\frac{1}{T(p, \Lambda)} + i p
	= \lim\limits_{\Lambda \to \infty}
		\frac{2 \pi}{\mu} \left[
			\frac{1}{C(\Lambda)} - I_3(p, \Lambda)
		\right]
	\, ,
\end{equation}
with
\begin{equation}
	I_3(p, \Lambda)
	=
	-\frac{\mu}{2 \pi}
	\left[
	i p + \frac{2 \Lambda}{\pi} + \frac{2  p}{\pi} \log \left( \frac{\Lambda - p}{\Lambda + p}\right)
	\right]
\end{equation}
The contact interaction cannot depend on any dynamic momenta; it is only possible to absorb momentum independent regulator terms when renormalizing the contact interaction.
It is still possible to renormalize the interaction such that the phase shifts, in the limit of $\Lambda \to \infty$, are independent of the cutoff by choosing the renormalized strength $C_R$ according to
\begin{equation}\label{eq:three-d-counterterm}
	\frac{2 \pi}{\mu} \frac{1}{C_R(\Lambda)} + \frac{2 \Lambda}{\pi}
	\equiv
	- \frac{1}{a_3}
	=
	p \cot \delta_3(p)
	\, .
\end{equation}

In particular, because the limit of $\Lambda \to \infty$ is well defined for this choice of the contact interaction parameter $C_R(\Lambda)$, one is able to evaluate both sides for a given momentum, such as $p=0$
\begin{equation}
	- \frac{1}{a_3}
	=
	\lim\limits_{p \to 0}\lim\limits_{\Lambda \to \infty}
		\left[
			\frac{2 \pi}{\mu}\frac{1}{T(p, \Lambda)} \bigg|_{C=C_R} + i p
	\right]
	=
	\lim\limits_{\Lambda \to \infty}
	\frac{2 \pi}{\mu}
		\left[
		\frac{1}{C_R(\Lambda)} - I_3(0, \Lambda)
		\right]
	\, .
\end{equation}

We now want to find an equivalent expression to the finite-volume zeta functions in presence of a discretization scheme.
In particular, the discretization scheme depends on the implementation of the kinetic operator $K^{(n_s)}$ and thus depends on the $n_s$ parameter.
The lattice spacing can be identified with the hard momentum cutoff $\Lambda = \pi / \epsilon$.
That is, the expectation value of the dispersion scales as $\hat K^{(n_s)}(\epsilon) \ket{p} = p^2 [1 + \mathcal O(\epsilon p)^{2 n_s}]\ket{p}$.

If one replaces the continuum momentum dispersion $q^2$ in $I_3$ with the kinetic operator for a given lattice spacing and discretization, one defines a sequence in $n_s$ which converges against $I_3$ in the limit of $n_s \to \infty$
\begin{equation}
	\lim\limits_{n_s \to \infty} I^{(n_s)}_3(p, \Lambda) = I_3(p, \Lambda)
	\, , \qquad
	I^{(n_s)}_3\left(p, \Lambda=\frac{\pi}{\epsilon} \right)
	=
	    \int\limits_{-\pi/\epsilon}^{+\pi/\epsilon}
        \mathrm{d}^3 \vec{q}
        \left[
            \PV \left(
                \frac{1}{
                    E - \frac{1}{2\mu} K_{qq}^{(n_s)} }
                \right)
            -i \pi \delta\left(E - \frac{1}{2\mu}K_{qq}^{(n_s)}\right)
        \right]
        \, .
\end{equation}
We furthermore define a sequence for the contact strength parameter depending on the cutoff and the employed discretization scheme which equivalently converges against the continuum result.
This sequence is determined by matching against the dispersion integral for each value of the cutoff and for each discretization scheme
\begin{equation}\label{eq:dispersion-renormalization}
	\lim\limits_{n_s \to \infty} C^{(n_s)}_R(\Lambda) = C_R(\Lambda) \, ,
	\qquad
	- \frac{1}{a_3}
	\equiv
	\frac{2 \pi}{\mu}
		\left[
		\frac{1}{C_R^{(n_s)}(\Lambda)} - I_3^{(n_s)}(0, \Lambda)
		\right]
	\, .
\end{equation}
It is possible to make this choice since both terms do not depend on any external momentum $p$.
This is specific for the contact interaction.
One can view this choice as the renormalization equation for contact interaction in presence of lattice discretization which, by definition, trivially satisfies
\begin{equation}
	- \frac{1}{a_3}
	=
	\lim\limits_{\Lambda \to \infty} \lim\limits_{n_s \to \infty}
	\frac{2 \pi}{\mu}
		\left[
		\frac{1}{C_R^{(n_s)}(\Lambda)} - I_3^{(n_s)}(0, \Lambda)
		\right]
	\, .
\end{equation}
In fact, it satisfies this equation even without the limits.

Next we address how this renormalization choice relates to the dispersion zeta function.
For any lattice implementation of a contact interaction with strength $c^\dispersion$ in finite volume, the Schr\"odinger equation can be rewritten as
\begin{equation}\label{eq:schroe}
	\hat G(E) \hat V \ket{\psi} = E\ket{\psi}
	\quad \Rightarrow \quad
	0 = 1 - c^\dispersion I_{3, \FV}^{(n_s)}\left(\sqrt{2 \mu E^\dispersion}, \Lambda = \frac{\pi}{\epsilon}\right) \, ,
\end{equation}
where $E^\dispersion$ are the finite volume energy levels, which depend on the employed discretization scheme and on the contact interaction of strength $c^\dispersion$.
The finite volume sum $I_{3, \FV}^{(n_s)}(p, \pi/\epsilon)$ is obtained by replacing the integral $d^3 \vec q$ in $I_3^{(n_s)}(p, \Lambda)$ with a sum  over vectors $\vec q = 2 \pi \vec n / L$.
Because the above equation is true for any value of $c^\dispersion$ and its corresponding spectrum, it is especially true for $c^\dispersion = C_R^{(n_s)}(\Lambda)$.
This means that
\begin{equation}
	- \frac{1}{a_3}
	=
	\frac{2 \pi}{\mu}
		\left[
		I_{3, \FV}^{(n_s)}\left(\sqrt{2 \mu E_i}, \frac{\pi}{\epsilon}\right)
		- I_3^{(n_s)}\left(0, \frac{\pi}{\epsilon}\right)
		\right]
	\, ,
\end{equation}
which defines the dispersion zeta function
\begin{align}\label{eq:dispersion-zeta-form}
	- \frac{1}{a_3}
	=
	\frac{1}{\pi L}
	S^{\dispersion}_3(x^\dispersion)
	&=\frac{1}{\pi L}\left(\sum\limits_{n \in \BZ}\frac{1}{K_{nn}^{(n_s)} - x^\dispersion} - \mathcal{L}_3^{\dispersion} \frac{N}{2}\right)
	\, ,
	\\ \label{eq:dispersion-zeta-contact}
	\mathcal{L}_3^\dispersion
	&=
	\frac{2 \pi^2 L}{\mu}
	I_3^{(n_s)}\left(0, \Lambda = \frac{\pi}{\epsilon}\right)
	\overset{n_s\to\infty}{\longrightarrow} 15.348
	\, .
\end{align}
See \Secref{dispersion-counterterm} for the computation of this coefficient.
Equation \eqref{dispersion-zeta-form} explains why results directly match the continuum infinite volume phase shifts when computed with this modified zeta function.
Note that this result does not hold for general finite-range interactions if it is not possible to make an equivalent choice as in \eqref{dispersion-renormalization}.
We stress that this derivation uses the analytic  expression for the $T$-matrix and simplifies drastically because the phase shifts for a renormalized contact interaction are momentum independent.
This momentum independence had the consequence that the counter term in \eqref{dispersion-zeta-contact} is momentum independent as well.

\subsection{Two dimensions}

In two dimensions the analog of~\eqref{blah blah} is
\begin{equation}
\cot \delta_2(p) - i =\lim_{\Lambda\to\infty}\frac{2}{\mu}\left(\frac{1}{C(\Lambda)}- I_2(p, \Lambda)\right)\ , %\frac{1}{2\pi}\mathcal{P}\int_0^\Lambda  \mathrm { d } q \ q \left( \frac { 1 } { E - \frac{\vec{q}^2}{m} } \right)\right)\ ,
\end{equation}
with
\begin{equation}
I_2(p, \Lambda)
=
-\frac{\mu}{\pi } \log \left(\frac{p}{\sqrt{\Lambda ^2-p^2}}\right)
+ i\frac{\mu }{2}
\ .
\end{equation}
Our renormalized coefficient is defined by using the phase shift condition for a contact interaction in 2-D~\eqref{2d contact phase shift} in the $\Lambda\to\infty$ limit,
\begin{equation}\label{eq:log stuff}
	\frac{2}{\mu}\frac{1}{C_R(\Lambda)} + \frac{2}{\pi } \log \left(\frac{p}{\Lambda}\right)
	=
	\frac { 2 } { \pi } \log \left( p \tilde a _ { 2 } \right)\ ,
\end{equation}
which ensures the renormalized contact strength $C_R(\Lambda)$ is momentum independent.
With the kinetic operator for a given lattice spacing and discretization, we again define a sequence in $n_s$ which converges against $I_2$ in the limit of $n_s \to \infty$,
\begin{equation}
	\lim\limits_{n_s \to \infty} I^{(n_s)}_2(p, \Lambda) = I_2(p, \Lambda)
	\, , \qquad
	I^{(n_s)}_2\left(p, \Lambda=\frac{\pi}{\epsilon} \right)
	=
	    \int\limits_{-\pi/\epsilon}^{+\pi/\epsilon}
        \mathrm{d}^2 \vec{q}
        \left[
            \PV \left(
                \frac{1}{
                    E - \frac{1}{2\mu} K_{qq}^{(n_s)} }
                \right)
            -i \pi \delta\left(E - \frac{1}{2\mu}K_{qq}^{(n_s)}\right)
        \right]
        \, .
\end{equation}
As was done prior to~\eqref{dispersion-renormalization}, we also define a sequence for the discrete coefficient $C^{(n_s)}_R(\Lambda)$ that is determined by matching against the dispersion integral for each value of the cutoff and for each discretization scheme.  However, in this case, due to the presence of logarithms in~\eqref{log stuff}, we first subtract the expression $\frac{2}{\pi}\log(pL/2\pi)$ prior to setting $p=0$,
\begin{equation}
%	\lim\limits_{n_s \to \infty} C^{(n_s)}_R(\Lambda) = C_R(\Lambda) \, ,
%	\quad
	\lim\limits_{p\to 0}
	\left[
		\frac { 2 } { \pi } \log \left( p \tilde a _ { 2 } \right)-\frac{2}{\pi } \log \left(\frac{pL}{2\pi}\right)
	\right]
	=
	\frac { 2 } { \pi } \log \left(2\pi \frac{\tilde a _ { 2 }}{L} \right)
	\equiv
	\frac{2 \pi}{\mu}
		\left[
		\frac{1}{C_R^{(n_s)}(\Lambda)} - \left.\left(I_2^{(n_s)}(p, \Lambda) - i\frac{\mu}{2}+\frac{\mu}{\pi^2} \log \left(\frac{pL}{2\pi}\right)\right)\right|_{p=0}
		\right]
	\, .
\end{equation}
Our sequence $\lim_{n_s\to\infty}C^{(n_s)}_R(\Lambda) = C_R(\Lambda)$ is well defined but implicitly depends on an external length scale $L$ due to the presence of the logarithm.  To arrive at the dispersion equation in two dimensions one repeats the steps from~\eqref{schroe} leading up to~\eqref{dispersion-zeta-form}, but now~\eqref{dispersion-zeta-form} becomes
\begin{equation}
    \frac{2}{\pi} \log \left(\frac{2\pi \tilde a_{2}}{L}\right)=\frac{1}{\pi^2}S^{\dispersion}_2\left(x^\dispersion\right)
    =
    \frac{1}{\pi^2}
    \left(
        \sum_{n\in\operatorname{B.Z.}}\frac{1}{\tilde{K}^N_{nn}-x^\dispersion}
        -2\pi \log \left(\mathcal{L}^\dispersion_2\frac{N}{2}\right)
    \right)\ .
\end{equation}
Here
\begin{equation}
    \mathcal{L}^\dispersion_{2}
    =
    \exp \left(\log (2)-G \frac{2}{\pi}\right)
    =
    1.116306393581637659468497 \ldots
\end{equation}
and $G$ is Catalan's constant.  We derive this counterterm in \ref{sec:dispersion-counterterm}.  The renormalized coefficient in this case is
\begin{equation}\label{eq:C2-dispersion}
C^{(n_s)}_R(\Lambda)=-\frac{ \pi}{\mu \log \left(\tilde a_{2} \counterterm^\dispersion_2\Lambda\right)}\ ,
\end{equation}
where now the coefficient $\counterterm_2^\dispersion$ carries a $n_s$ dependence.