\chapter{Calculus in 1-Complexity}

\section{Introduction and Motivation}
This chapter will present a specific set of $rComplexity$ classes, highlighting \textbf{Big \textit{r-}Theta}, \textbf{Big \textit{r-}O} and \textbf{Big \textit{r-}Omega} with unitary parameter (i.e. $r = 1$) by providing useful properties for a more straightforward calculation. The last part of the chapter introduce a new concept for \textit{monotonic, continuous function}: \textbf{normal form representation} and defines a new workflow in $rComplexity$: \textbf{normalized rComplexity calculus}.

Calculus in $rComplexity$ is dependent on the parameter $r \in \mathbb{R}_{+}$, and as a result a large number of operations may require rudimentary \textit{conversions} using relations described in \textit{Common properties} and \textit{Notable properties} chapters. Working with unitary $rComplexity$ classes comes effortless and brings an agile manner of operating ample
calculus using this Complexity Model.

\section{Main notations in 1-Complexity Calculus}
The following notations and names will be used for describing the asymptotic behavior of a algorithm's complexity characterized by a function, $f:\mathbb{N}\longrightarrow\mathbb{R}$ in $1-Complexity$. \\
We define the set of all complexity calculus $\mathcal{F}= \lbrace f:\mathbb{N}\longrightarrow\mathbb{R} \rbrace$
\\Assume that $n, n_{0}\in\mathbb{N}$. Also, we will consider an arbitrary complexity function $g \in \mathcal{F}$.
The following notations are particularization ($r = 1$) of notations provided in $rComplexity$:
\begin{definition}
    \textbf{Big \textit{1-}Theta}: This set defines the group of mathematical functions similar in magnitude with  $g(n)$ in the study of asymptotic behavior. A set-based description of this group can be expressed as:
    \[\begin{split}
          \Theta_{1}(g(n)) = \lbrace f \in \mathcal{F}\ |\ \forall c_{1}, c_{2} \in \mathbb{R}^{*}_{+} \ s.t. c_{1} < 1 < c_{2} , \exists n_{0} \in \mathbb{N}^{*}\ \\ s.t.\ \ c_{1} \cdot g(n) \leq f(n) \leq c_{2} \cdot g(n)\ ,\  \forall n \geq n_{0} \rbrace
    \end{split} \]
\end{definition}
\begin{definition}
    \textbf{Big \textit{1-}O}: This set defines the group of mathematical functions that are known to have a similar or lower
    asymptotic performance in comparison with  $g(n)$. The set of such functions is defined as it follows:
    \[\mathcal{O}_{1}(g(n)) = \lbrace f \in \mathcal{F}\ |\ \forall c  \in \mathbb{R}^{*}_{+} \ s.t.\  1 < c, \exists n_{0} \in \mathbb{N}^{*}\ s.t.\  f(n) \leq c \cdot g(n),\  \forall n \geq n_{0} \rbrace\]
\end{definition}
\begin{definition}

    \textbf{Big \textit{1-}Omega}: This set defines the group of mathematical functions that are known to have a similar or higher asymptotic performance in comparison with  $g(n)$. The set of all function is defined as:
    \[\Omega_{1}(g(n)) = \lbrace f \in \mathcal{F}\ |\ \forall c  \in \mathbb{R}^{*}_{+}\ s.t. \ c < 1, \exists n_{0} \in \mathbb{N}^{*}\ s.t.\  f(n) \geq c \cdot g(n),\  \forall n \geq n_{0} \rbrace\]
\end{definition}
\begin{definition}
    \textbf{Small \textit{1-}O}:
    This set defines the group of mathematical functions that are known to have a humble
    asymptotic performance in comparison with  $g(n)$. The set of such functions is defined as it follows:
    \[o_{1}(g(n)) = \lbrace f \in \mathcal{F}\ |\ \forall c \in \mathbb{R}^{*}_{+}, \exists n_{0} \in \mathbb{N}^{*}\ s.t.\  f(n) < c \cdot g(n),\  \forall n \geq n_{0} \rbrace\]
    Note that $o_{r}(g(n) = o_{1}(g(n) \forall r\in  \mathbb{R}_{+}$, as Small \textit{1-}O notation is $r$-independent.
\end{definition}
\begin{definition}
    \textbf{Small \textit{1-}Omega}:
    This set defines the group of mathematical functions that are known to have a commanding asymptotic performance in comparison with  $g(n)$.
    The set of such functions is defined as it follows:
    \[\omega_{1}(g(n)) = \lbrace f \in \mathcal{F}\ |\ \forall c \in \mathbb{R}^{*}_{+}, \exists n_{0} \in \mathbb{N}^{*}\ s.t.\  f(n) > c \cdot g(n),\  \forall n \geq n_{0} \rbrace\]
    Note that $\omega_{r}(g(n) = \omega_{1}(g(n) \forall r\in  \mathbb{R}_{+}$, as Small \textit{1-}Omega notation is $r$-independent.
\end{definition}

\section{Asymptotic Analysis}
Calculus in $1-Complexity$ can be performed as well using either limits of sequences or limits of functions. Consider any two complexity functions $f,g:\mathbb{N}_{+}\longrightarrow\mathbb{R}_{+}$.

\begin{theorem}
    Admittance of a function $f$ in \textbf{Big \textit{1-}Theta} class defined by a function $g$:
    \[ f \in \Theta_{r}(g(n)) \Leftrightarrow \lim_{n\to\infty} \dfrac{f(n)}{g(n)} = 1 \]
\end{theorem}
\begin{theorem}
    Admittance of a function $f$ in \textbf{Big \textit{1-}O} class defined by a function $g$:
    \[ f \in \mathcal{O}_{1}(g(n)) \Leftrightarrow \lim_{n\to\infty} \dfrac{f(n)}{g(n)} = l,\ l \in \left[ 0, 1 \right] \]
\end{theorem}
\begin{theorem}
    Admittance of a function $f$ in \textbf{Big \textit{1-}Omega} class defined by a function $g$:
    \[ f \in \Omega_{1}(g(n)) \Leftrightarrow \lim_{n\to\infty} \dfrac{f(n)}{g(n)} = l,\ l \in \left[ 1, \infty \right) \]
\end{theorem}
\begin{theorem}
    Admittance of a function $f$ in \textbf{Small \textit{1-}O} class defined by a function $g$:
    \[ f \in o_{r}(g(n)) \Leftrightarrow \lim_{n\to\infty} \dfrac{f(n)}{g(n)} = 0 \]
\end{theorem}
\begin{theorem}
    Admittance of a function $f$ in \textbf{Small \textit{1-}Omega} class defined by a function $g$:
    \[ f \in \omega_{r}(g(n)) \Leftrightarrow \lim_{n\to\infty} \dfrac{f(n)}{g(n)} = \infty \]
\end{theorem}

\section{Common properties}
This chapter will present new implications in terms of reflexivity, transitivity, symmetry and projections properties of calculus in $1-Complexity$.
\begin{theorem}
    Reflexivity in $1-Complexity$ - Big 1-Omega notation:
    \[ f \in \Theta_{1} \left( f(n) \right)\ \]
\end{theorem}
\begin{proof}
    Let $r = 1$ and use Reflexivity property described in Common properties sections for classes in $rComplexity$ calculus.
    \[ f \in \Theta_{r} \left( \frac{1}{r} \cdot f(n) \right)\ \forall r \neq 0 \]
    Therefore for $r = 1$:
    \[ f \in \Theta_{1} \left( f(n) \right)\ \]
\end{proof}

\begin{theorem}
    Reflexivity in $1-Complexity$ - Big 1-O notation:
    \[ f \in \mathcal{O}_{1} \left( x \cdot f(n) \right)\ \forall x \geq 1 \]
\end{theorem}
\begin{proof}
    Let $r = 1$ and use Reflexivity property for Big \textit{r-}O class in $rComplexity$ calculus.
\end{proof}

\begin{theorem}
    Reflexivity in $1-Complexity$ - Big 1-Omega notation:
    \[ f \in \Omega_{1} \left( x \cdot f(n) \right)\ \forall x \leq 1 \]
\end{theorem}
\begin{proof}
    Let $r = 1$ and use Reflexivity property for Big \textit{r-}Omega class in $rComplexity$ calculus.
\end{proof}

The reflexivity does not hold \textbf{either} for Small \textit{1-}o and Small \textit{1-}Omega, as these two sets are equal with the classical sets defined in \textit{Bachmann–Landau notations}.
\begin{remark}
    Reflexivity in $1-Complexity$ - Small 1-o notation:
    $ f \notin o_{1}(f(n)) $
\end{remark}
\begin{remark}
    Reflexivity in $1-Complexity$ - Small 1-Omega notation:
    $ f \notin \omega_{1}(f(n)) $
\end{remark}

\begin{theorem}
    Transitivity in $1-Complexity$ - Big 1-Theta notation:  \\  $ f \in \Theta_{1}(g(n)), g \in \Theta_{1}(h(n)) \Rightarrow  f \in \Theta_{1}(h(n))$
\end{theorem}
\begin{proof}
    Let $r = 1$ and use Transitivity property for Big \textit{r-}Theta class in $rComplexity$ calculus.
\end{proof}

\begin{theorem}
    Transitivity in $1-Complexity$ - Big 1-O notation:
    $ f \in \mathcal{O}_{1}(g(n)), g \in \mathcal{O}_{1}(h(n)) \Rightarrow  f \in \mathcal{O}_{1}(h(n))$
\end{theorem}
\begin{proof}
    Let $r = 1$ and use Transitivity property for Big \textit{r-}O class in $rComplexity$ calculus.
\end{proof}
\begin{theorem}
    Transitivity in $1-Complexity$ - Big 1-Omega notation:
    $ f \in \Omega_{1}(g(n)), g \in \Omega_{1}(h(n)) \Rightarrow  f \in \Omega_{1}(h(n))$
\end{theorem}
\begin{proof}
    Let $r = 1$ and use Transitivity property for Big \textit{r-}Omega class in $rComplexity$ calculus.
\end{proof}
\begin{lemma}
    Transitivity in $1-Complexity$ - small notations:
    \[ f \in o_{1}(g(n)), g \in o_{1}(h(n)) \Rightarrow  f \in o_{1}(h(n)) \]
    \[ f \in \omega_{1}(g(n)), g \in \omega_{1}(h(n)) \Rightarrow  f \in \omega_{1}(h(n)) \]
\end{lemma}
\begin{proof}
    Small \textit{1-}o and Small \textit{1-}Omega are equal with the classical sets defined in \textit{Bachmann–Landau notations} and thus they conserve transitivity properties
\end{proof}

\begin{theorem}
    \textbf{Symmetry in $1-Complexity$:}  \\  $ f \in \Theta_{1}(g(n)) \Rightarrow g \in \Theta_{1}(f(n)) $
\end{theorem}
\begin{proof}
    Let $r = 1$ and use Symmetry property for Big \textit{r-}Theta class in $rComplexity$ calculus.
\end{proof}

\begin{theorem}
    \textbf{Transpose symmetry in $1-Complexity$:}  \\  $ f \in \mathcal{O}_{1}(g(n)) \Leftrightarrow g \in \Omega_{1}(f(n)) $
\end{theorem}
\begin{proof}
    Let $r = 1$ and use Transpose symmetry property described in $rComplexity$ calculus.
\end{proof}
\begin{theorem}
    Transpose symmetry in $1-Complexity$ in small notations:
    $ f \in o_{1}(g(n)) \Leftrightarrow g \in \omega_{1}(f(n)) $
\end{theorem}
\begin{proof}
    Small \textit{1-}o and Small \textit{1-}Omega are equal with the classical sets defined in \textit{Bachmann–Landau notations} and thus they conserve the Transpose symmetry property.
\end{proof}

\begin{theorem}
    \textbf{Projection in $1-Complexity$:}  \\  $ f \in \Theta_{1}(g(n)) \Leftrightarrow f \in \mathcal{O}_{1}(g(n)), f \in \Omega_{1}(g(n)) $
\end{theorem}
\begin{proof}
    Let $r = 1$ and use Projection property described in $rComplexity$ calculus.
\end{proof}

\section{Addition properties}
Addition properties are obtained by assuming $r = 1$ in the $r$Complexity model.
\begin{theorem}
    Addition properties in \textbf{Big 1-Theta}:  \\
    The following relations hold for any correctly defined functions $f, g, f', g', h:\mathbb{N}\longrightarrow\mathbb{R}$, where $ h(n) = f'(n) + g'(n)\  \forall n \in  \mathbb{N} $, where $f',g'$ are two arbitrary functions such that $ f' \in \Theta_{1}(f), g' \in \Theta_{1}(g) $:
    \begin{itemize}
        \item \textbf{If} $ \lim_{n\to\infty} \dfrac{f(n)}{g(n)} = 0 \Rightarrow  h \in \Theta_{1}(g) $.
        \item \textbf{If} $ \lim_{n\to\infty} \dfrac{f(n)}{g(n)} = \infty \Rightarrow  h \in \Theta_{1}(f) $.
        \item \textbf{If} $ \lim_{n\to\infty} \dfrac{f(n)}{g(n)} = t, \ t \in \mathbb{R}_{+} \Rightarrow  h \in \Theta_{1} \left( f + g \right) $.
    \end{itemize}
\end{theorem}

\begin{theorem}
    Addition properties \textbf{Big 1-O}: \\
    The following relations hold for any correctly defined functions $f, g, f', g', h:\mathbb{N}\longrightarrow\mathbb{R}$, where $ h(n) = f'(n) + g'(n)\  \forall n \in \mathbb{N} $, where $f',g'$ are two arbitrary functions such that $ f' \in \mathcal{O}_{1}(f), g' \in \mathcal{O}_{1}(g) $:
    \begin{itemize}
        \item \textbf{If} $ \lim_{n\to\infty} \dfrac{f(n)}{g(n)} = 0 \Rightarrow  h \in \mathcal{O}_{1}(g) $.
        \item \textbf{If} $ \lim_{n\to\infty} \dfrac{f(n)}{g(n)} = \infty \Rightarrow  h \in \mathcal{O}_{1}(f) $.
        \item \textbf{If} $ \lim_{n\to\infty} \dfrac{f(n)}{g(n)} = t, \ t \in \mathbb{R}_{+} \Rightarrow  h \in \mathcal{O}_{1} \left( f + g \right) $.
    \end{itemize}
\end{theorem}

\begin{theorem}
    Addition properties \textbf{Big 1-Omega}: \\
    The following relations hold for any correctly defined functions $f, g, f', g', h:\mathbb{N}\longrightarrow\mathbb{R}$, where $ h(n) = f'(n) + g'(n)\  \forall n \in \mathbb{N} $, where $f',g'$ are two arbitrary functions such that $ f' \in \Omega_{1}(f), g' \in \Omega_{1}(g) $:
    \begin{itemize}
        \item \textbf{If} $ \lim_{n\to\infty} \dfrac{f(n)}{g(n)} = 0 \Rightarrow  h \in \Omega_{1}(g) $.
        \item \textbf{If} $ \lim_{n\to\infty} \dfrac{f(n)}{g(n)} = \infty \Rightarrow  h \in \Omega_{1}(f) $.
        \item \textbf{If} $ \lim_{n\to\infty} \dfrac{f(n)}{g(n)} = t, \ t \in \mathbb{R}_{+} \Rightarrow  h \in \Omega_{r} \left( f + g \right) $.
    \end{itemize}

\end{theorem}

In a relax notation (consider that by any  $1-$Complexity class notation, we denote an arbitrary function part of the class), the following relations can be settled:

\begin{lemma}
    If $\lim_{n\to\infty} \dfrac{f(n)}{g(n)} = 0$:
    \begin{itemize}
        \item \textbf{Big 1-Theta}:
        \[  \Theta_{1}(f) + \Theta_{1}(g) = \Theta_{1}(g)\]
        \item \textbf{Big 1-O}:
        \[  \mathcal{O}_{1}(f) + \mathcal{O}_{1}(g) = \mathcal{O}_{1}(g)\]
        \item \textbf{Big 1-Omega}:
        \[  \Omega_{1}(f) + \Omega_{1}(g) = \Omega_{1}(g)\]
    \end{itemize}
\end{lemma}

\begin{lemma}
    If $\lim_{n\to\infty} \dfrac{f(n)}{g(n)} = \infty$:
    \begin{itemize}
        \item \textbf{Big 1-Theta}:
        \[  \Theta_{1}(f) + \Theta_{1}(g) = \Theta_{1}(f)\]
        \item \textbf{Big 1-O}:
        \[  \mathcal{O}_{1}(f) + \mathcal{O}_{1}(g) = \mathcal{O}_{1}(f)\]
        \item \textbf{Big 1-Omega}:
        \[  \Omega_{1}(f) + \Omega_{1}(g) = \Omega_{1}(f)\]
    \end{itemize}
\end{lemma}

\begin{lemma}
    If $\lim_{n\to\infty} \dfrac{f(n)}{g(n)} = t, \ t \in \mathbb{R}_{+}$:
    \begin{itemize}
        \item \textbf{Big 1-Theta}:
        \[  \Theta_{1}(f) + \Theta_{1}(g) = \Theta_{1}(f + g)\]
        \item \textbf{Big 1-O}:
        \[  \mathcal{O}_{1}(f) + \mathcal{O}_{1}(g) = \mathcal{O}_{1}(f + g)\]
        \item \textbf{Big 1-Omega}:
        \[  \Omega_{1}(f) + \Omega_{1}(g) = \Omega_{1}(f + g)\]
    \end{itemize}
\end{lemma}

\begin{proof}
    All proofs are based on the particularizing $r=1$ and rewrite the addition properties from rComplexity.
\end{proof}

\section{Normal form functions}
In this section, we will take further steps in simplifying calculus by introducing a new concept: the normal form of a monotonic, continuous function.
\begin{definition}
    Let $g:\mathbb{N}^{*}\longrightarrow\mathbb{R}_{+} $ be a monotonic, continuous function. Let the following:
    \[ g(n) = \sum_{i=1}^{p} g_{i}(n)\]
    be a decomposition, with correctly defined function $g_{i}:\mathbb{N}^{*}\longrightarrow\mathbb{R}_{+} \forall i \in [1, p]$, with the properties:
    \begin{itemize}

        \item $ \exists \ j, \lim_{n\to\infty} \dfrac{g_{j}(n)}{g(n)} = 1$
        \item $\forall i \neq j \  lim_{n\to\infty} \dfrac{g_{j}(n)}{g_{i}(n)} = \infty$
        \item there is no another decomposition for $g_{j}(n) = \sum_{k=1}^{p'} g_{j_{k}}(n)$ such that $\lim_{n\to\infty} \dfrac{g_{j}(n)}{g_{j_{k}}(n)} = \infty, \forall k \in [1, p']$.
    \end{itemize}
    Then, we call $g_{j}$ to be a function in \textbf{normal form} or in \textbf{atomic form}.
\end{definition}

\begin{remark}
    We will refer to a monotonic, continuous function $g$ that is in normal form using the notation $g_{1}(n)$ .
\end{remark}

\begin{remark}
    Working in $1$-Complexity with functions in normal form will involve converting an arbitrary monotonic, continuous function into an atomic representation, given by the normal form $g_{1}(n)$, by applying the decomposition presented in the definition above.This step involve few additional overhead in calculus, but overall the conversions are accessible and simplifies a lot the progress of complexity detection for various algorithms. Nonetheless, it provides powerful benchmarks solutions for comparing algorithms' efficiency.
\end{remark}

An useful subset of rComplexity Calculus is defined by Calculus using Normalized rComplexity class functions.
\begin{definition}
    We will denote by:
    \[ f(n) \in \Theta_{1}(g_{1}(n)) \]
    a function f that is in the Big 1-Theta $1-$Complexity class defined by the function $g_{1}$ and $g_{1}$ is in normal form. Working with normal form functions for the class characterization in $1-$Complexity Calculus will be named \textbf{normalized rComplexity calculus}.
\end{definition}

\begin{remark}
    Working in $r$-Complexity with the required conversions such that $r = 1$ and the functions defining complexity classes is in normal form $(g = g_{1})$ is the most simple
    calculus model while working with $r$-Complexity classes.
\end{remark}

\begin{remark}
    A practical use case of this notation will be distinguishable when discussing the N Queens problems analysis for asymptotic time metric while working with $r$-Complexity classes.
\end{remark}


% Sample of typesetting algorithmic code in LaTeX
% Great option for writing pseudocode.
% Adopted from: https://tex.stackexchange.com/a/56877
%

\documentclass{article}[journal]
\usepackage{algorithm}      % http://ctan.org/pkg/algorithm
\usepackage{algpseudocode}  % http://ctan.org/pkg/algorithmicx

\begin{document}

\section*{Typesetting \texttt{algorithmic code} in \LaTeX} % Heading

\begin{algorithm}  % Begin algorithmic code
  \caption{Euclid’s algorithm}\label{euclid}  % Label the code
  \begin{algorithmic}[1]  % Begin algorithmic code, layer 1

    % Declare the beginning of the procedure
    \Procedure{Euclid}{$a,b$} \Comment{The g.c.d. of a and b}
      \State $r\gets a\bmod b$

      \While{$r\not=0$} \Comment{We have the answer if r is 0}  % Declare a while loop
        \State $a\gets b$
        \State $b\gets r$
        \State $r\gets a\bmod b$
      \EndWhile\label{euclidendwhile}  % End the while loop

      \For{\texttt{<condition>}}  % Declare a for loop
        \State \texttt{<do something>}
      \EndFor  % End the for loop

      \State \textbf{return} $b$ \Comment{The gcd is b}

    \EndProcedure  % End the procedure

  \end{algorithmic}
\end{algorithm}

\end{document}

% EOF


%!TEX root = /home/renaud/Documents/EPL/tfe/latex/tfe.tex
Two test cases adapted from \textit{Eric Deleersnijder}'s working paper \cite{deleersnijder2011test} are presented here: after introducing the problems, the analytic solutions are computed as well as their main properties. The code is then run on those problems, and the numerical solution and its properties are compared to the analytical ones. Rather than a validation of the numerical solver, this can be considered as a \textit{sanity check}.

%----------------------------------GOVERNING EQUATIONS--------------------------------------------------------%
\section{Governing equations}
Let us consider a water domain, whose width is denoted $B(\b{x},t)$, where $\b{x} = (y,z)$ is the position vector and $t$ is the time. The continuity equation is
\begin{equation} \label{eq:testcase:continuity}
	\frac{\partial B}{\partial t} + \nabla \cdot (B\b{u}) = 0,
\end{equation}
where $\b{u}(t,\b{x})$ is the latitudinally-averaged meridional velocity. Assuming that mixing along the parallels is sufficiently efficient, we may study the concentration of a passive tracer by means of a two-dimensional model. The latitudinally-averaged concentration of the tracer $C(\b x, t)$ obeys the following partial differential equation :
\begin{equation} \label{eq:testcase:conservative}
	\frac{\partial (BC)}{\partial t} + \nabla \cdot (B \b{u} C) = Q\delta(\b{x} - \b{x}_1) + \nabla \cdot (B\b{K} \cdot \nabla C),
\end{equation}
where $\b{K}$ is the diffusivity tensor (symmetric and positive definite); $\delta$ is the Dirac delta function with $\delta(\b{x}-\b{x}_n) = \delta(x-x_n)\delta(y-y_n)$; $Q(t)$ is the rate of release of a lineic source of length $B$ along the latitude direction located at $\b{x} = \b{x}_1$. If $C(\b x,t)$ represents the
density of the tracer in water, then $Q(t)$ is the mass of tracer released per second by the source.

Equation~\eqref{eq:testcase:conservative} is the so-called conservative form of the model. The convective form is obtained by combining equations~\eqref{eq:testcase:continuity} and~\eqref{eq:testcase:conservative}:
\begin{equation}  \label{eq:testcase:convective}
	\frac{\partial C}{\partial t} + \b{u} \cdot \nabla C = \frac{Q}{B} \delta(\b{x} - \b{x}_1) + \frac{1}{B} \nabla \cdot (B\b{K} \cdot \nabla C).
\end{equation}

%----------------------------------AN IDEALISED MODEL----------------------------------------------------------------------%
\section{An idealized model}
For our test case to be interesting, we must be able to compute its analytical solution. Accordingly, we make some simplifying assumptions which will allow us to compute the solution analytically.
First, we assume a constant width $B$ and a constant velocity field
\begin{equation} \label{eq:testcase:velocity}
	\b{u}(t,\b{x}) = v \b e_y + w \b e_z,
\end{equation}
where $\b e_y$ and $\b e_z$ are the unit vectors associated respectively with the $y$- and $z$-coordinate axis. Furthermore, the diffusivity tensor is supposed constant and diagonal :
\begin{equation} \label{eq:testcase:diffusivity}
	\b{K} = \begin{pmatrix}
			K_{yy} & 0 \\
			0 & K_{zz}
			\end{pmatrix},
\end{equation}
where $K_{yy},\ K_{zz} > 0$. Finally, we consider a sudden pointwise release of tracer at $t=0$. Hence, $Q(t)$ is of the form :
\begin{equation}
	Q(t) = M\delta(t),
\end{equation}
where $M$ is the mass of tracer released at $t=0$.

Under these assumptions, equation~\eqref{eq:testcase:convective} simplifies to :
\begin{equation} \label{eq:testcase}
	\frac{\partial C}{\partial t} + v \frac{\partial C}{\partial y} + w \frac{\partial C}{\partial z} = J \delta(t) \delta(y - y_1)\delta(z-z_1) + K_{yy} \frac{\partial^2 C}{\partial y^2} + K_{zz} \frac{\partial^2 C}{\partial z^2},
\end{equation}
where $J := M/B$.
For the sake of simplicity, we can forget about the fact that our model is width-integrated and consider that it is a purely two-dimensional model with a point-source
\begin{equation}
	Q := J\delta(t).
\end{equation}
A part of the physical meaning of the model is lost but this makes representations of the problem easier. $C$ now represents the two-dimensional density (i.e., in [$kg/m^2$]) of the tracer in water. $J$ can then be regarded as the mass of tracer released by the sudden point source at $\b x = \b x_1$. The three- and two-dimensional interpretations of the problem are represented on figure~\ref{fig:testcase_scheme}.
% \footnote{This could be a bit confusing. As we switch from a 3-dimensional interpretation of the model to a 2-dimensional one, the meaning of the parameters changes. Hence, in the 3-dimensional interpretation, $C$ represents a 3D density ([$kg/m^3$]) and $M$ is a mass, whereas in the 2-dimensional interpretation, $C$ is a 2D density ([$kg/m^2$]) and $M$ has units of [$kg\,m$]. A part of the physical meaning of the model is lost, but this makes representations of the problem much easier.} \textcolor{red}{On peut aussi simplement considérer B = 1. Attention, si on opte finalement pour cette option, un facteur B doit être présent pour le calcul de certains diagnostiques.}
\begin{figure}[H]
	\centering
	\scalebox{.8}{\input{fig/testcase/testcase_scheme3D}}
	\scalebox{.8}{\input{fig/testcase/testcase_scheme2D}}
	\caption{Illustration of the 3D and 2D interpretations of the model.}
    \label{fig:testcase_scheme}
\end{figure}

%------------------------------------PARAMETERS FOR TEST CASE 1--------------------------------------------------------------%
\subsection{Test case 1 : infinite domain}
The first (an most simple) test case consists in considering an infinite domain, i.e.
\begin{equation} \label{eq:testcase:domain}
	-\infty < y,\, z < \infty,
\end{equation}
with nonzero velocities $v$ and $w$. This test case provides a check that our numerical implementation handles the diffusion and advection processes properly both in the $y$- and $z$-directions. The parameters are chosen from the values proposed by \textit{C. Timmermans} for the overturner model in her master's thesis \cite{timmermans2006masterthesis}:
\begin{equation}
	v = \frac{\Psi}{H} = 4\e{-4}\mbox{ [$m/s$],}\quad  w = \frac{\Psi}{L}=1.33\e{-7}\mbox{ [$m/s$],}
\end{equation}
and
\begin{equation}
	K_{yy} = 10^{3} \mbox{ [$m^2/s$],}\quad K_{zz} = 10^{-4} \mbox{ [$m^2/s$].}
\end{equation}
For the length scales of the overturner model, those diffusivities corresponds to Péclet numbers
\begin{equation}
	Pe_y = \frac{v}{K_{yy}/L} = 6,\quad Pe_z = \frac{w}{K_{zz}/H} = 6.67.
\end{equation}
Hence, in both the $y$- and $z$-directions, the transport is neither dominated by advection nor by diffusion. This is interesting as a test case since it allows to assess how the numerical solver handles both physical processes in both directions.
Finally, $J = 10\,000$ particles are released at $t=0$ at the location $(y_1,z_1) = (0,0)$.

%------------------------------------PARAMETERS FOR TEST CASE 2--------------------------------------------------------------%
\subsection{Test case 2 : semi-infinite domain}
Another interesting case is to consider a semi-infinite domain with a wall at $z=0$ :
\begin{equation}
	-\infty < y < \infty,\quad 0 < z < \infty.
\end{equation}
This is useful to assess how our numerical model handles no-through boundary conditions. Again, the parameters values are related to the ones proposed in \textit{C. Timmermans}'s master's thesis for the overturner model :
\begin{equation}
	v = \frac{\Psi}{H} = 4\e{-4}\mbox{ [$m/s$],}\quad  w = 0\mbox{ [$m/s$],}
\end{equation}
and
\begin{equation}
	K_{yy} = 10^{3} \mbox{ [$m^2/s$],}\quad K_{zz} = 10^{-1} \mbox{ [$m^2/s$].}
\end{equation}
Notice the choice  of $K_{zz}$ : it is chosen $10^3$ times larger than the value chosen for test case 1. The goal here is to assess that the boundary condition is well handled by the solver. Since $w=0$, only the vertical diffusivity could possibly drive the particles towards the wall. By increasing $K_{zz}$, we ensure that more particles will bounce against the wall, which is relevant in this context. $J = 10\,000$ particles are released at $t=0$ at the location $(y_1,z_1) = (0,H)$.

\section{Analytical solution and properties}
%------------------------------------------------Analytic GREEN's FUNCTION ---------------------------------------------%
\subsection{Green's function}
In order to build the analytical solution of the problem, we need to compute the Green's function associated to this particular problem. We derive the Green's function $G$ associated to test case 1. We will show later how this function can be used to compute the concentration for both test case 1 and test case 2. $\G$ is zero for $t<t'$ and is the solution of
\begin{equation} \label{eq:testcase_green}
	\begin{cases}
		\frac{\partial G}{\partial t} + v \frac{\partial G}{\partial y} + w\frac{\partial G}{\partial z} = K_{yy}\frac{\partial^2 G}{\partial y^2} + K_{zz}\frac{\partial^2 G}{\partial z^2}\\[.1cm]
		\left. G(y,z,t,t') \right \rvert_{t=t'} = \delta(y)\delta(z)
	\end{cases}
\end{equation}
for $t \ge 0$, and on an infinite domain $\infty < y,\, z < \infty$. It can be shown that
\begin{equation}
	G(y,z,t,t') = \frac{\exp\left[-\frac{(y-s_v)^2}{4K_{yy}\tau} -\frac{(z-s_w)^2}{4K_{zz}\tau} \right]}{4\pi\sqrt{K_{yy}K_{zz}}\tau},
\end{equation}
where $\tau = t-t'$ and
\begin{equation}
	\b s(t,t') = (s_v(t,t'), s_w(t,t')) = \left(\int_{t'}^{t}v \rm d \xi, \int_{t'}^{t}w \rm d \xi \right) = \left(v\tau, w\tau\right).
\end{equation}

$G$ has some interesting properties. The "mass" of the solution is
\begin{equation} \label{eq:testcase:propmass}
	m(t,t') \equiv \int_{\R[2]} \G \rm d \b x = 1.
\end{equation}
The "center of mass" is located at
\begin{equation} \label{eq:testcase:propcenter}
	\b r(t,t') \equiv \frac{1}{m(t,t')} \int_{\R[2]} \b x \G \rm d \b x  = \b s(t,t').
\end{equation}
The variance of the solution is
\begin{equation} \label{eq:testcase:propvar}
	\sigma^2(t,t') \equiv \frac{1}{m(t,t')} \int_{\R[2]} \lvert \b x - \b r(t,t') \rvert^2 \G \rm d \b x = 2 (K_{yy}+K_{zz}) \tau.
\end{equation}

%------------------------------------------------Analytic TEST CASE 1 ---------------------------------------------%
\subsection{Test case 1}
The analytical solution of test case 1 is now obtained with the help of the Green's function derived above by computing the convolution between $G$ and the source terms :
\begin{align}
	C(\b x,t) &= \int_{0}^{t} \int_{\R[2]} G(\b x - \b x',t,t') J \delta(t) \delta(\b x- \b x_1) \rm d \b{x}' \rm dt'\nonumber\\
	&= J G(\b x - \b x_1,t,0).
\end{align}
The concentration profile for test case 1 is thus
\begin{equation}
	C(y,z,t) = \frac{J}{4\pi\sqrt{K_{yy}K_{zz}}t}\exp\left[-\frac{(y-s_v)^2}{4K_{yy}t} -\frac{(z-s_w)^2}{4K_{zz}t} \right].
\end{equation}

The total mass of tracer present in the domain is
\begin{equation}
	m(t) \equiv \int_{\R[2]} C(\b x,t) \rm d \b x = J.
\end{equation}
Note that this number is independent of the transport processes.

The mass center is located at
\begin{align}
	\b r(t) &\equiv \frac{1}{m(t)} \int_{\R[2]} \b x C(\b x,t) \rm d \b x \nonumber \\
	&= \int_{\R[2]} \b x G(\b x - \b x_1,t,0) \rm d \b x \nonumber\\
	&= \int_{\R[2]} (\b x-\b x_1) G(\b x - \b x_1,t,0) + \b x_1 G(\b x - \b x_1,t,0) \rm d \b x\nonumber\\
	&= \b x_1 + \b s(t,0),
\end{align}
where properties~\eqref{eq:testcase:propmass} and~\eqref{eq:testcase:propcenter} are used to perform the last step.

Finally, the variance of the solution is
\begin{align}
	\sigma^2(t) &= \frac{1}{m(t)} \int_{\R[2]} \lvert \b x - \b r(t) \rvert^2 C(\b x,t) \rm d \b x \nonumber \\
	&= \int_{\R[2]} \lvert (\b x - \b x_1) - \b s(t,0)\rvert^2 G(\b x - \b x_1,t,0) \rm d \b x \nonumber \\
	&= 2(K_{yy}+K_{zz})t,
\end{align}
where property~\eqref{eq:testcase:propvar} is used.

%------------------------------------------------Analytic TEST CASE 2 ---------------------------------------------%
\subsection{Test case 2}
To compute the solution to test case 2, a little trick must be applied. Consider the problem on an infinite domain with two sudden point sources of equal intensity located at $z = H$ and $z = -H$. By symmetry, one can see that the concentration of that problem in the region $[-\infty, \infty] \times [0, \infty]$ is precisely the concentration of test case 2. Hence, we can use the Green's function $G$ derived for test case 1 to compute the concentration. In this case, the convolution has to be performed with two point sources :
\begin{align}
	C(\b x,t) &= \int_{0}^{t} \int_{-\infty}^{\infty} \int_{-\infty}^{\infty} J \delta(t') \delta(y'- y_1)\left[\delta(z'-z_1)+\delta(z'+z_1)\right] G(y-y',z-z',t,t') \rm dy' \rm dz' \rm dt' \nonumber\\
	&= J \int_{0}^{t} \delta(t')[G(y-y_1,z-z_1,t,t')+G(y-y_1,z+z_1,t,t')] \label{tc2_eq2} \nonumber\\
	&= J[G(y-y_1,z-z_1,t,0)+G(y-y_1,z+z_1,t,0)].
\end{align}
The concentration of the tracer for test case 2 is thus
\begin{equation}
	C(y,z,t) = \frac{J}{4\pi\sqrt{K_{yy}K_{zz}}t}\exp\left[-\frac{(y-s_v)^2}{4K_{yy}t}\right]\left\{\exp\left[-\frac{(z-z_1)^2}{4K_{zz}t} \right] + \exp\left[-\frac{(z+z_1)^2}{4K_{zz}t} \right] \right\}
\end{equation}

The mass is obtained as
\begin{equation}
	m(t) \equiv \int_{0}^{\infty} \int_{-\infty}^{\infty} C(y,z,t) \rm dy \rm dz = J,
\end{equation}
i.e. the number of particles released at $t=0$. This result is obvious since there is no other source or sink, and we impose a no-through condition at the boundary.

No analytic solution has been found for the center of mass and the variance. However, an accurate estimation of those quantities can be computed numerically from the expression of the concentration.

% The mass center is
% \begin{align}
% 	\b r(t) &\equiv \frac{1}{m(t)} \int_{0}^{\infty} \int_{-\infty}^{\infty} (y \b e_y + z \b e_z) C(t,y,z) \rm dy \rm dz \nonumber \\
% 	&= \int_{0}^{\infty} \int_{-\infty}^{\infty} (y \b e_y + z \b e_z) \left[G(t,0,y-y_1,z-z_1)+G(t,0,y-y_1,z+z_1)\right] \rm dy \rm dz \nonumber\\
% 	&= \int_{\R[2]} (\b x-\b x_1) G(t,0,\b x - \b x_1) + \b x_1 G(t,0,\b x - \b x_1) \rm d \b x\nonumber\\
% 	&= \b x_1 + \b s(t,0),
% \end{align}

%-------------------------------------VALIDATION--------------------------------------------------------------------------------%
\section{Validation of the numerical solver}
This section aims to show that the numerical results obtained with the solver are in good agreement with the analytical ones. As the combination of the two test cases cover the main features of the problems considered in this work, this constitutes a basic validation of the solver.

Both test cases are simulated for 1 year with a time step of 1 hour, and $P = 10\,000$ particles are released at $t=0$.
The concentration is computed at the final time $T = 1$ year on the domain $\Omega = [y_{min},y_{max}] \times [z_{min},z_{max}]$, where the subscripts \textit{min} and \textit{max} stands for the minimal and maximal position at time $T$ amongst all the particles. The domain $\Omega$ is divided into $20 \times 20$ boxes denoted $\Omega_i$ for $i = 1,\dots,200$. Let $C_i(t)$ denote the concentration in box $i$ at time $t$ and $C_{max}(t) = \max_i C_i(t)$. The scaled concentration in box $i$ is computed as
\begin{equation}
	\tilde C_i(T) := \frac{C_i(T)}{C_{i,max}(T)} = \frac{P_i(T)}{P_{max}(T)},
\end{equation}
where $P_i(t)$ is the number of particles in box $i$ at time $t$ and $P_{max}(t) = \max_i P_i(t)$. The last equality is true because all the boxes have the same volume. The analytic solutions of the concentration are scaled identically:
\begin{equation}
	\tilde C(t) = \frac{C(t)}{C_{max}(t)}.
\end{equation}

%-------------------------------------RESULTS CASE 1----------------------------------------------------------------------------%
\subsection{Test case 1}
Figure~\ref{fig:testcase_surf} shows a comparison between the numerical result and the analytical solution for the scaled concentration $C(\b x,T)/\tilde C$. Dark (resp. light) shaded areas correspond to zones where the numerically computed concentration is "above" (resp. "below") the exact concentration. It would probably have been more rigorous to represent the numerically computed concentration as a three-dimensional histogram since the box-counting method computes the mean value of the concentration in each box. However, here we have considered that the numerically computed mean concentration in a box corresponds to the concentration at the center of that box. The continuous representation of $\tilde{C}_{num}$ is then obtained by interpolation. Figures~\ref{fig:testcase_fixedy} and~\ref{fig:testcase_fixedz} represent respectively a cut of the concentrations at fixed $y = r_{y,exact}$ and at fixed $z = r_{z,exact}$. The numerically computed concentration $\tilde C_{num}$ seems to be an appreciable approximation of the exact concentration $\tilde C_{exact}$. To be more specific, the maximal local error is
\begin{equation}
	\|\tilde C_{exact}- \tilde C_{num}\|_{\infty} = 7.017\e{-2}.
\end{equation}
The centers of mass are located at
\begin{equation}
	\b r_{exact} = (12\,614.4,\,4.2) \mbox{ [$m$],}\quad \b r_{num} = (11\,715.3,\,4.5) \mbox{ [$m$]}.
\end{equation}
The relative error is
\begin{equation}
	\b e_r = \left \lvert \frac{\b r_{exact}-\b r_{num}}{\b r_{exact}} \right \rvert =  \left(7.13\e{-2}, 7.89\e{-2}\right),
\end{equation}
where the division is taken element-wise on the vectors. The euclidean norm of the relative error is
\begin{equation}
	\| \b e_r \|_2 = 1.06\e{-1}.
\end{equation}
This can be seen as a quantification of the error on advection. To quantify the error on diffusion, we compute the variance of the concentration :
\begin{equation}
	\sigma^2_{exact} = 6.31\e{10} \mbox{ [$m^2$],}\quad \sigma^2_{num} = 6.43\e{10} \mbox{ [$m^2$].}
\end{equation}
The relative error is
\begin{equation}
	e_{\sigma^2} = \left \lvert \frac{\sigma^2_{exact}-\sigma^2_{num}}{\sigma^2_{exact}}\right \rvert = 1.86\e{-2}.
\end{equation}
\begin{figure}[H]
	\centering
	\includegraphics[width = \textwidth]{fig/testcase/testcase_surf.eps}
	\caption{Comparison of the concentrations obtained analytically and numerically. The "centers of mass" of the concentration obtained numerically (black cross) and numerically (white bullet) are also shown on the figure.}
	\label{fig:testcase_surf}
\end{figure}
\begin{figure}[H]
	\centering
	\begin{subfigure}[b]{0.49\textwidth}
		\includegraphics[width=\textwidth]{fig/testcase/testcase_fixedy.eps}
		\caption{$\tilde C_{num}(y_1+vT,z)$ and $\tilde C_{exact}(y_1+vT,z)$.}
		\label{fig:testcase_fixedy}
	\end{subfigure}
	\begin{subfigure}[b]{0.49\textwidth}
		\includegraphics[width=\textwidth]{fig/testcase/testcase_fixedz.eps}
		\caption{$\tilde C_{num}(y,z_1+wT)$ and $\tilde C_{exact}(y,z_1+wT)$.}
		\label{fig:testcase_fixedz}
	\end{subfigure}
	\caption{Cut of the concentrations at fixed $y = r_{y,exact}$ and at fixed $z = r_{z,exact}$. The dashed line represent $\tilde C_{num}$ and the continuous line is for $\tilde C_{exact}$.}
\end{figure}

%-------------------------------------------RESULTS CASE 2-----------------------------------------------------------------------%
\subsection{Test case 2}
Figure~\ref{fig:testcaseSI_surf} shows a comparison between the numerical result and the analytical solution for the scaled concentrations for test case 2. The same remark as the one made for test case 1 applies concerning the representation of the numerically computed concentration. Since we do not have analytical expressions of the center of mass and of the variance for this test case, the "exact" values are approximated numerically thanks to the analytical expression of the concentration. Figures~\ref{fig:testcaseSI_fixedy} and~\ref{fig:testcaseSI_fixedz} represent respectively a cut of the concentrations at fixed $y = r_{y,exact}$ and along the boundary $z = 0$. The big picture about test case 2 is the presence of a boundary with no-through condition. A first verification is to check if all the particles are still in the domain, which is indeed the case. Although this might seem trivial here, it is sometimes a real challenge to ensure that no particle crosses the boundary, especially for geometrically complex domains. Besides, one can see on figure~\ref{fig:testcaseSI_fixedz} that the concentration profile is well approximated along the boundary. Indeed, the maximal local error at the boundary is
\begin{equation}
	\|\tilde C_{exact}(y,0)- \tilde C_{num}(y,0)\|_{\infty} = 2.964\e{-2},
\end{equation}
The maximal local error on the whole domain is
\begin{equation}
	\|\tilde C_{exact}- \tilde C_{num}\|_{\infty} = 8.668\e{-2}.
\end{equation}
The centers of mass are located at
\begin{equation}
	\b r_{exact} = (1.26\e{4},5.06\e{3}) \mbox{ [$m$],}\quad \b r_{num} = (1.37\e{4},5.05\e{3}) \mbox{ [$m$]}.
\end{equation}
The relative error is
\begin{equation}
	\b e_r = \left \lvert \frac{\b r_{exact}-\b r_{num}}{\b r_{exact}} \right \rvert =  \left(8.566\e{-2}, 2.718\e{-3}\right).
\end{equation}
The euclidean norm of the relative error is
\begin{equation}
	\| \b e_r \|_2 = 8.571\e{-2}.
\end{equation}
This can be seen as a quantification of the error on advection. To quantify the error on diffusion, we compute the variance of the concentration :
\begin{equation}
	\sigma^2_{exact} = 6.39\e{10} \mbox{ [$m^2$],}\quad \sigma^2_{num} = 6.25\e{10} \mbox{ [$m^2$].}
\end{equation}
The relative error is
\begin{equation}
	e_{\sigma^2} = \left \lvert \frac{\sigma^2_{exact}-\sigma^2_{num}}{\sigma^2_{exact}}\right \rvert = 2.269\e{-2}.
\end{equation}
\begin{figure}[H]
	\centering
	\includegraphics[width = \textwidth]{fig/testcase/testcaseSI_surf.eps}
	\caption{Comparison of the concentrations obtained analytically and numerically. The "centers of mass" of the concentration obtained numerically (black cross) and numerically (white bullet) are also shown on the figure.}
	\label{fig:testcaseSI_surf}
\end{figure}
\begin{figure}[H]
	\centering
	\begin{subfigure}[b]{0.49\textwidth}
		\includegraphics[width=\textwidth]{fig/testcase/testcaseSI_fixedy.eps}
		\caption{$\tilde C_{num}(y_1+vT,z)$ and $\tilde C_{exact}(y_1+vT,z)$.}
		\label{fig:testcaseSI_fixedy}
	\end{subfigure}
	\begin{subfigure}[b]{0.49\textwidth}
		\includegraphics[width=\textwidth]{fig/testcase/testcaseSI_fixedz.eps}
		\caption{$\tilde C_{num}(y,0)$ and $\tilde C_{exact}(y,0)$.}
		\label{fig:testcaseSI_fixedz}
	\end{subfigure}
	\caption{Cut of the concentrations at fixed $y = r_{y,exact}$ and along the wall (fixed $z = 0$). The dashed line represent $\tilde C_{num}$ and the continuous line is for $\tilde C_{exact}$.}
\end{figure}


\subsubsection{Linear Factors}
\noindent
This is the the most basic type where the degree of the numerator is less than the degree of the denominator and the denominator factors into all linear factors with no repeated roots. In this case we can write
\begin{equation*}
	\frac{P(x)}{Q(x)} = \frac{A_1}{(x-a_1)} + \ldots + \frac{A_n}{(x-a_n)}
\end{equation*}
Multiplying each side by $Q(x)$,
\begin{equation*}
	P(x) = A_1(x-a_2) \ldots (x-a_n) + \ldots + A_n(x-a_1) \ldots (x-a_{n-1})
\end{equation*}
We can then find each $A_i$ by evaluating both sides at $x=a_i$, since every term except the ith has an $(x-a_i)$ factor that will go to 0. So,
\begin{equation*}
	A_i = \frac{P(a_i)}{(x-a_i) \ldots (x-a_{i-1})(x-a_{i+1}) \ldots (x-a_n)}
\end{equation*}

\ifodd\includeBackgroundReviewExamples\input{./backgroundReview/algebraPreCalc/linearFactors_example.tex}\fi


\section{Comparison Tests}\label{sec:ComparisonTests}

As we begin to compile a list of convergent and divergent series, new
ones can sometimes be analyzed by comparing them to ones that we
already understand.

\begin{example}{}{}
Does $\ds\sum_{n=2}^\infty {1\over n^2\ln n}$ converge?
\end{example}
\begin{solution}
The obvious first approach, based on what we know, is the integral test.
Unfortunately, we can't compute the required antiderivative. But
looking at the series, it would appear that it must converge, because
the terms we are adding are smaller than the terms of a $p$-series,
that is,
$${1\over n^2\ln n}<{1\over n^2},$$
when $n\ge3$. Since adding up the terms $\ds 1/n^2$ doesn't get ``too
big'', the new series ``should'' also converge. Let's make this more
precise.

The series $\ds\sum_{n=2}^\infty {1\over n^2\ln n}$ converges if and
only if $\ds\sum_{n=3}^\infty {1\over n^2\ln n}$ converges---all we've
done is dropped the initial term. We know that
$\ds\sum_{n=3}^\infty {1\over n^2}$ converges. Looking at two typical
partial sums:
$$
  s_n={1\over 3^2\ln 3}+{1\over 4^2\ln 4}+{1\over 5^2\ln 5}+\cdots+
  {1\over n^2\ln n} < {1\over 3^2}+{1\over 4^2}+
  {1\over 5^2}+\cdots+{1\over n^2}=t_n.
$$
Since the $p$-series converges, say to $L$, and since the terms are positive,
$\ds t_n<L$. Since the terms of the new series are positive, the $\ds s_n$
form an increasing sequence and $\ds s_n<t_n<L$ for all $n$. Hence the
sequence $\ds \{s_n\}$ is bounded and so converges.
\end{solution}

Sometimes, even when the integral test applies, comparison to a known
series is easier, so it's generally a good idea to think about doing a
comparison before doing the integral test.

\begin{example}{}{AbsSineOverNSquared}
Does $\ds\sum_{n=2}^\infty {|\sin n|\over n^2}$ converge?
\end{example}
\begin{solution}
We can't apply the integral test here, because the terms of this
series are not decreasing. Just as in the previous example, however,
$$ {|\sin n|\over n^2}\le {1\over n^2},$$
because $|\sin n|\le 1$. Once again the partial sums are
non-decreasing and bounded above by $\ds \sum 1/n^2=L$, so the new series
converges.
\end{solution}

Like the integral test, the comparison test can be used to show both
convergence and divergence. In the case of the integral test, a single
calculation will confirm whichever is the case. To use the comparison
test we must first have a good idea as to convergence or divergence
and pick the sequence for comparison accordingly.

\begin{example}{}{}
Does $\ds\sum_{n=2}^\infty {1\over\sqrt{n^2-3}}$ converge?
\end{example}
\begin{solution}
We observe that the $-3$ should have little effect compared to the
$\ds n^2$ inside the square root, and therefore guess that the terms are
enough like $\ds 1/\sqrt{n^2}=1/n$ that the series should diverge. We
attempt to show this by comparison to the harmonic series. We note
that
$${1\over\sqrt{n^2-3}} > {1\over\sqrt{n^2}} = {1\over n},$$
so that
$$
  s_n={1\over\sqrt{2^2-3}}+{1\over\sqrt{3^2-3}}+\cdots+
  {1\over\sqrt{n^2-3}} > {1\over 2} + {1\over3}+\cdots+{1\over n}=t_n,
$$
where $\ds t_n$ is 1 less than the corresponding partial sum of the
harmonic series (because we start at $n=2$ instead of $n=1$). Since
$\ds\lim_{n\to\infty}t_n=\infty$, $\ds\lim_{n\to\infty}s_n=\infty$ as
well.
\end{solution}

So the general approach is this: If you believe that a new series is
convergent, attempt to find a convergent series whose terms are
larger than the terms of the new series; if you believe that a new
series is divergent, attempt to find a divergent series whose terms
are smaller than the terms of the new series.

\begin{example}{}{}
Does $\ds\sum_{n=1}^\infty {1\over\sqrt{n^2+3}}$ converge?
\end{example}
\begin{solution}
Just as in the last example, we guess that this is very much like the
harmonic series and so diverges. Unfortunately,
$${1\over\sqrt{n^2+3}} < {1\over n},$$
so we can't compare the series directly to the harmonic series.
A little thought leads us to
$${1\over\sqrt{n^2+3}} > {1\over\sqrt{n^2+3n^2}} = {1\over2n},$$ so if
$\sum 1/(2n)$ diverges then the given series diverges. But since $\sum
1/(2n)=(1/2)\sum 1/n$, Theorem~\xrefn{thm:SeriesLinear} implies
that it does indeed diverge.
\end{solution}

For reference we summarize the comparison test in a theorem.

\begin{theorem}{Comparison Theorem}{ComparisonTheorem}
Suppose that $\ds a_n$ and $\ds b_n$ are non-negative for all $n$ and
that $\ds a_n\le b_n$ when $n\ge N$, for some $N$.

\begin{itemize}
\item{} If $\ds\sum_{n=0}^\infty b_n$ converges, so does
$\ds\sum_{n=0}^\infty a_n$.
\item{} If $\ds\sum_{n=0}^\infty a_n$ diverges, so does
$\ds\sum_{n=0}^\infty b_n$.
\end{itemize}
\end{theorem}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Opensolutionfile{solutions}[ex]
\section*{Exercises for \ref{sec:ComparisonTests}}

\begin{enumialphparenastyle}

Determine whether the series converge or diverge.

\begin{multicols}{2}
\begin{ex}
$\ds\sum_{n=1}^\infty {1\over 2n^2+3n+5} $
\begin{sol}
converges
\end{sol}
\end{ex}

\begin{ex}
$\ds\sum_{n=2}^\infty {1\over 2n^2+3n-5} $
\begin{sol}
converges
\end{sol}
\end{ex}

\begin{ex}
$\ds\sum_{n=1}^\infty {1\over 2n^2-3n-5} $
\begin{sol}
converges
\end{sol}
\end{ex}

\begin{ex}
$\ds\sum_{n=1}^\infty {3n+4\over 2n^2+3n+5} $
\begin{sol}
diverges
\end{sol}
\end{ex}

\begin{ex}
$\ds\sum_{n=1}^\infty {3n^2+4\over 2n^2+3n+5} $
\begin{sol}
diverges
\end{sol}
\end{ex}

\begin{ex}
$\ds\sum_{n=1}^\infty {\ln n\over n}$
\begin{sol}
diverges
\end{sol}
\end{ex}

\begin{ex}
$\ds\sum_{n=1}^\infty {\ln n\over n^3}$
\begin{sol}
converges
\end{sol}
\end{ex}

\begin{ex}
$\ds\sum_{n=2}^\infty {1\over \ln n}$
\begin{sol}
diverges
\end{sol}
\end{ex}

\begin{ex}
$\ds\sum_{n=1}^\infty {3^n\over 2^n+5^n}$
\begin{sol}
converges
\end{sol}
\end{ex}

\begin{ex}
$\ds\sum_{n=1}^\infty {3^n\over 2^n+3^n}$
\begin{sol}
diverges
\end{sol}
\end{ex}
\end{multicols}

\end{enumialphparenastyle}


\section{Free Vibrations}
\noindent
Free damped vibrations, like in a massed spring system, are a common application of second order linear ODEs. In a massed spring system, there are three main forces acting on the mass that make up external forces.
\begin{enumerate}[label=\arabic*)]
	\item Acceleration of The Mass -- Since acceleration is the 2nd derivative of position $y(t)$, and Newton's Second Law tells us that $F = ma$, the force from the acceleration of the mass is $my''$.
	\item Dampening -- We'll assume that this term is proportional to the velocity, $y'$, and a term $b$. So, the force from dampening is $by'$.
	\item Spring Stretch -- Hooke's Law tells us that the force from a spring is $ky$, where $k$ is some term that gives the spring's "stiffness"
\end{enumerate}
Since we assume that the net force is 0 (that's what free means), our equations is
\begin{equation*}
	my'' + by' + ky = 0
\end{equation*}

\noindent
Extracting the coefficients and solving the auxiliary equation,
\begin{equation*}
	mr^2 + br + k = 0 \implies r = \frac{-b \pm \sqrt{b^2 - 4mk}}{2m}
\end{equation*}
We will consider two cases. One in which there is no damping ($b = 0$), and one in which there is damping ($b > 0$).

\input{./higherOrder/freeVibrs/freeUndamped.tex}
\input{./higherOrder/freeVibrs/freeDamped.tex}


% This LaTeX was auto-generated from an M-file by MATLAB.
% To make changes, update the M-file and republish this document.

\subsection*{covSEard.m}

\begin{par}
Squared Exponential covariance function with Automatic Relevance Detemination (ARD) distance measure. The covariance function is parameterized as:
\end{par} \vspace{1em}
\begin{par}
k(x\^{}p,x\^{}q) = sf2 * exp(-(x\^{}p - x\^{}q)'*inv(P)*(x\^{}p - x\^{}q)/2)
\end{par} \vspace{1em}
\begin{par}
where the P matrix is diagonal with ARD parameters ell\_1\^{}2,...,ell\_D\^{}2, where D is the dimension of the input space and sf2 is the signal variance. The hyperparameters are:
\end{par} \vspace{1em}
\begin{par}
loghyper = [ log(ell\_1)              log(ell\_2)               .              log(ell\_D)              log(sqrt(sf2)) ]
\end{par} \vspace{1em}
\begin{par}
For more help on design of covariance functions, try "help covFunctions".
\end{par} \vspace{1em}
\begin{par}
(C) Copyright 2006 by Carl Edward Rasmussen (2006-03-24)
\end{par} \vspace{1em}

\begin{lstlisting}
function [A, B] = covSEard(loghyper, x, z)
\end{lstlisting}

\subsection*{Code}

\begin{lstlisting}
if nargin == 0, A = '(D+1)'; return; end          % report number of parameters

persistent K;

[n D] = size(x);
ell = exp(loghyper(1:D));                         % characteristic length scale
sf2 = exp(2*loghyper(D+1));                                   % signal variance

if nargin == 2
  K = sf2*exp(-sq_dist(diag(1./ell)*x')/2);
  A = K;
elseif nargout == 2                              % compute test set covariances
  A = sf2*ones(size(z,1),1);
  B = sf2*exp(-sq_dist(diag(1./ell)*x',diag(1./ell)*z')/2);
else                                                % compute derivative matrix

  % check for correct dimension of the previously calculated kernel matrix
  if any(size(K)~=n)
    K = sf2*exp(-sq_dist(diag(1./ell)*x')/2);
  end

  if z <= D                                           % length scale parameters
    A = K.*sq_dist(x(:,z)'/ell(z));
  else                                                    % magnitude parameter
    A = 2*K;
    clear K;
  end
end
\end{lstlisting}


% !TeX root = ../main.tex

\section{Introduction}
The earliest algorithms developed in recommender systems were
neighborhood-based collaborative filtering algorithms. These algorithms
utilize the similarities between either of users or items, based on the ratings.
The data in a recommender system can be presented as a matrix called the ratings
matrix($\mathcal{R}$).
\begin{table}[H]
\centering
\begin{tabular}{ |c|c|c|c|c| }
\hline
\diagbox{$User$}{$Item$} & \textbf{$Item_1$} & \textbf{$Item_2$} & \textbf{$Item_3$} & \textbf{$Item_4$} \\
\hline
\textbf{$User_1$} & 5 & 2 & \textbf{?} & 3 \\
\hline
\textbf{$User_2$} & 1 & \textbf{?} & 4 & 2 \\
\hline
\textbf{$User_3$} & \textbf{?}  & 3 & 5 & 4 \\
\hline
\textbf{$User_4$} & 5 & 2 & 3 &  \textbf{?} \\
\hline
\textbf{$User_5$} & 1 & \textbf{?}  & 2 & 4 \\
\hline
\textbf{$User_6$} & 3 & 5 & \textbf{?}  & 3 \\
\hline
\textbf{$User_7$} & 3 & 1 & \textbf{?}  & 5 \\
\hline
\end{tabular}
\caption{Ratings Matrix}
\label{table:Ratings Matrix}
\end{table}

\justify
The rows of the matrix represent users($\mathcal{U}$) and the
columns represent items($\mathcal{I}$). A rating($r$) is a number in this case an integer in
the range [1 - 5] that a user has given to an item, where 1 means that
the users totally disliked the item and 5 that the user totally liked the item. The missing ratings are those
that the users haven't reviewed yet and also where the rating predictions apply.
The known ratings are used for the similarity computations and therefore for
the rating predictions. There are two types of neighborhood-based algorithms:
\begin{itemize}
	\item \textbf{User-based CF:}  This type utilizes the ratings matrix
	row-wise meaning the recommender system is trying to find similar patterns
	between users. Given the ratings matrix, to predict the rating that
	$User_4$ would give to $Item_4$ first it would try to find users
	that have rated $Item_4$ and out of these users to select those
	(also called nearest neighbors) who have liked similar items with $User_4$
	in the past. Then based on the most similar nearest neighbors to $User_4$
	a prediction would be computed as an aggregation of the target user's nearest neighbors
	ratings. This method assumes two things. The first one is that if users
	had similar interests in the past they will have similar interests in the future.
	The second assumption is that user preferences remain stable and
	consistent over time. \citep{Jannach}

	\item \textbf{Item-based CF:} This type utilizes the ratings matrix
	column-wise and it is trying to find patterns between the items. Again
	from the ratings matrix, to predict the rating that $User_4$ would give to
	$Item_4$ it would try to find other items, that this user has rated. The
	rating prediction would  be an aggregation of the ratings of the most similar
	items between those and $Item_4$. Item-based CF is considered a more viable approach in
	real cases for two reasons. The first reason is that the items are usually far
	less than the users	in the system. This reduces both the computation
	time and storage needed for the similarities. The second reason is that
	users tend to rate a small proportion of items, which distorts the
	user similarities because of the sparseness. Instead, each item accumulates more
	ratings, as items are less than users, which makes item-based
	similarities more reliable.\citep{sarwar2001item}
\end{itemize}
The main advantages of the neighborhood-based algorithms\citep{Ricci} are:
\begin{itemize}
	\item \textbf{Simplicity:} To implement a neighborhood-based algorithm
	the only factor that is considered is the ratings in the matrix in order
	to find similarities.
	\item \textbf{Justifiability:} It is very clear how the rating
	predictions were computed and why it makes sense from the data as the
	similarities that gave these predictions can be provided to the users, so
	as to justify their relevance.
	\item \textbf{Efficiency:} The training cost in neighbor-based methods
	consists only of the similarity pre-computations between users or items
	which can be computed offline in the ratings matrix which is much cheaper than
	most model-based training. Offline learning means that the system
	will not adapt immediately to any change. Instead it must recompute
	the similarities from scratch. Moreover, storing only the nearest neighbors
	in memory requires only a little use of RAM, instead of storing every user
	which could form a similarity with the active user.
	\item \textbf{Stability:} In large scale recommenders systems, typically in
	item-based approach, it is observed that the addition of users, items and ratings slightly
	affect the decisions of the system after item similarities have been computed.
	That means that the RS would not need further re-training to make better recommendations.
	Also when new items enter the system, they can be trained solely without affecting other items'
	existing similarities.
\end{itemize}

As discussed in the previous chapter, neighborhood-based algorithms suffer from the sparsity of
the ratings matrix. When the similarities are calculated from
only a few common ratings between, e.g. two items, this often leads to
an ambiguous similarity score that can produce spurious predictions.
Another symptom of sparseness is that sometimes no similarities between
items or users can be found and this inability leaves those items or users
without neighbors. Thus, without neighbors they won't participate in
the rating prediction process which means that the RS won't be able to provide
recommendations for those entities.

In \autoref{sec:2.2} different similarity metrics will be discussed.
In \autoref{sec:2.3} The KNN algorithm will be explained and a full example will be
demonstrated for further clarification.

\section{Similarity Metrics}\label{sec:2.2}
The most important part in a neighborhood-based CF approach is the computation of
the similarities which, as we discussed above, can be user-based or item-based.
Below, different similarity metrics will be mentioned. Some of them are widely known metrics
in the literature. These are Cosine Similarity, Pearson Correlation Coefficient,
Adjusted Cosine Similarity, Mean Absolute Difference, Mean Squared Difference and
Jaccard Coefficient. The rest of them are modifications
of the original metrics that we introduce and do not exist in the literature.
These are the Modified Cosine Similarity, Modified Adjusted Cosine Similarity,
Modified Pearson Correlation Coefficient 1 and Modified Pearson Correlation Coefficient 2.
Some of the widely known similarity metrics have been introduced as a user-based approach
and others as item-based. In this thesis we will define each similarity metric both for
user-based and item-based. Also we will assume that the values of the ratings matrix
consist of integers in the interval [1, 5] in order to define the interval for
each similarity metric.
\subsection{Mathematical Notation}
For consistency over the similarity metrics formulas that will be defined next,
a global mathematical notation is used for the rest of this section.
Uppercase $\mathcal{I}$ is used to denote the set of items in the system.
The notation $\mathcal{I}_u$ is the set of items that have been rated by
user $u$, $\mathcal{I}_v$ is the set of items that have been rated by
user $v$ and $\mathcal{I}_{uv}$ is $\mathcal{I}_u \cap \mathcal{I}_v$ that is the set of items that users $u$ and $v$ have
rated in common.
Similarly, uppercase $\mathcal{U}$ is used to denote the set of users in the system.
$\mathcal{U}_i$ is the set of users that have rated
item $i$, $\mathcal{U}_j$ is the set of users that have rated
item $j$ and $\mathcal{U}_{ij}$ is $\mathcal{U}_i \cap \mathcal{U}_j$ that is the set of users that have rated both
items $i$ and $j$. The notation $\mathopen|\mathcal{I}_{u}\mathclose|$ denotes
the number of items user $u$ has rated, $\mathopen|\mathcal{I}_{v}\mathclose|$
denotes the number of items user $v$ has rated, and $\mathopen|\mathcal{I}_{uv}\mathclose|$
is the number of items that users $u$ and $v$ have rated in common. Likewise,
$\mathopen|\mathcal{U}_{i}\mathclose|$ denotes the number of users that have
rated item $i$, $\mathopen|\mathcal{U}_{j}\mathclose|$ denotes the number of users that have rated
item $j$, and $\mathopen|\mathcal{U}_{ij}\mathclose|$ is the number of users that
have both rated items $i$ annd $j$.
$r_{ui}$ is the rating that user $u$ gave to item $i$ and
$r_{vi}$ the rating that user $v$ gave to item $i$.
$r_{iu}$ is the rating item $i$ received from user $u$ and
$r_{ju}$ the rating item $j$ received from user $u$.

\subsection{Cosine similarity}
Cosine similarity is a metric that measures the cosine of the angle that two vectors form.
This metric can take any value in the interval [-1, 1], but specifically for our
own case where the ratings matrix is defined in the interval [1, 5], this
metric is defined in the interval [0, 1].\\\\
User-based cosine similarity between users $u$ and $v$ is defined as:
\begin{equation}\label{eq:cosine}
    cos(u,v) = \frac{\sum_{i \in \mathcal{I}_{uv}}r_{ui}r_{vi}}
		    {\sqrt{\sum_{i \in \mathcal{I}_{u}}r_{ui}^2}
		     \sqrt{\sum_{i \in \mathcal{I}_{v}}r_{vi}^2}}
\end{equation}
The largest the similarity value the higher is chance that these users rate by the same way.
A zero similarity score indicates that $u$ and $v$ have no items in common and therefore
they have no similarity.\\\\
From \autoref{table:Ratings Matrix}, the cosine similarity
between $User_1$ and $User_3$ would thus be computed as follows:
\begin{align*}
cos(User_1,User_3) = \frac{2*3 + 3*4}
						{\sqrt{5^2 + 2^2 + 3^2} * \sqrt{3^2 + 5^2 + 4^2}} = 0.4129
\end{align*}
Similarly, instead of using the row vectors to calculate the similarity between users,
column vectors can be used to calculate the similarity between item pairs.\\\\
Item-based cosine similarity between items $i$ and $j$ is defined as:
\begin{equation}
    cos(i,j) = \frac{\sum_{u \in \mathcal{U}_{ij}}r_{iu}r_{ju}}
		    {\sqrt{\sum_{u \in \mathcal{U}_{i}}r_{iu}^2}
		     \sqrt{\sum_{u \in \mathcal{U}_{j}}r_{ju}^2}}
\end{equation}
Again from \autoref{table:Ratings Matrix}, the cosine similarity
between $Item_2$ and $Item_3$ would thus be computed as follows:
\begin{align*}
cos(Item_2,Item_3) = \frac{3*5 + 2*3}
						{\sqrt{2^2 + 3^2 + 2^2 + 5^2 + 1^2} * \sqrt{4^2 + 5^2 + 3^2 + 2^2}} = 0.4358
\end{align*}
\subsection{Modified Cosine Similarity}
Modified cosine similarity differs from the standard cosine similarity in the
sense that the computations in the denominator apply only to the common items
between $u$ and $v$.
Its values are defined in the interval [0, 1].\\\\
User-based modified cosine similarity between users $u$ and $v$ is defined as:
\begin{equation}\label{eq:modified_cosine}
    MC(u,v) = \frac{\sum_{i \in \mathcal{I}_{uv}}r_{ui}r_{vi}}
		   {\sqrt{\sum_{i \in \mathcal{I}_{uv}}r_{ui}^2}
                    \sqrt{\sum_{i \in \mathcal{I}_{uv}}r_{vi}^2}}
\end{equation}
From \autoref{table:Ratings Matrix}, MC
between $User_1$ and $User_3$ would thus be computed as follows:
\begin{align*}
MC(User_1,User_3) = \frac{2*3 + 3*4}
					   {\sqrt{2^2 + 3^2} * \sqrt{3^2 + 4^2}} = 0.9984
\end{align*}
We can reverse the above formula in order to calculate the similarities
in terms of items.\\
Item-based modified cosine similarity between items $i$ and $j$ is defined as:
\begin{equation}
    MC(i,j) = \frac{\sum_{u \in \mathcal{U}_{ij}}r_{iu}r_{ju}}
   {\sqrt{\sum_{u \in \mathcal{U}_{ij}}r_{iu}^2}
            \sqrt{\sum_{u \in \mathcal{U}_{ij}}r_{ju}^2}}
\end{equation}
From \autoref{table:Ratings Matrix}, MC
between $Item_2$ and $Item_3$ would thus be computed as follows:
\begin{align*}
MC(Item_2,Item_3) = \frac{3*5 + 2*3}
						{\sqrt{3^2 + 2^2} * \sqrt{5^2 + 3^2}} = 0.9988
\end{align*}
\subsection{Adjusted Cosine Similarity}
Adjusted cosine similarity was originally created as an
item-based approach. The reason for that was because each item vector consists of
users who have different rating behaviors and cosine similarity did not
take into account these disparities. That means that a user could use the
rating 3 for items that really liked and 1 for items that really disliked while
another user could give a 5 rating to items that really liked and 1 to them that really
disliked. Adjusted cosine similarity is computed by looking into the co-rated items
($\mathcal{U}_{ij}$) only. It normalizes each user's rating behavior by
subtracting the corresponding user's mean value \citep{sarwar2001item}.
Adjusted cosine similarity values are defined in the interval [-1, 1].
The value -1 means perfect dissimilarity between two items while the value 1 means
perfect similarity. The zero value means there is no similarity between these items.\\\\
Item-based adjusted cosine similarity between items $i$ and $j$ is defined as:
\begin{equation}\label{eq:adjusted_cosine}
    \begin{split}
    &AC(i,j) = \frac{\sum_{u \in \mathcal{U}_{ij}}(r_{iu}-\bar{r_{u}})(r_{ju}-\bar{r_{u}})}
		    {\sqrt{\sum_{u \in \mathcal{U}_{ij}}(r_{iu}-\bar{r_{u}})^2}
                     \sqrt{\sum_{u \in \mathcal{U}_{ij}}(r_{ju}-\bar{r_{u}})^2}} \\\\
    &\bar{r_{u}} = \frac{\sum_{i \in \mathcal{I}_u}r_{ui}}
 		        {\mathopen|\mathcal{I}_u\mathclose|}
    \end{split}
\end{equation}
where,
\begin{itemize}
	\item[] $\bar{r_u}$ is the mean value of user $u$ for each $u \in \mathcal{U}_{ij}$
\end{itemize}
From \autoref{table:Ratings Matrix}, AC
between $Item_2$ and $Item_3$ would thus be computed as follows:
\begin{align*}
	\begin{split}
		&\bar{r}_{User_3} = \frac{3 + 5 + 4}{3} = 4\\
		&\bar{r}_{User_4} = \frac{5 + 2 + 3}{3} = 3.33
	\end{split}
\end{align*}
$$AC(Item_2,Item_3) = \frac{(3 - 4)*(5 - 4) + (2 - 3.33)*(3 - 3.33)}
						 {\sqrt{(3 - 4)^2 + (2 - 3.33)^2}*
						  \sqrt{(5 - 4)^2 + (3 - 3.33)^2}} = −0.3202$$
The result $-0.3202$ means that $Item_2$ and $Item_3$ are somewhat dissimilar.\\\\
We could use the same reasoning adjusted cosine was build on,
to look at this formula from a user-based perspective. That is, if an item's
lowest rating is 3 and its highest rating is 5, that means that it is treated
differently than an item that its lowest rating is 1 and the highest 4.
Therefore, we could have each co-rated item, by two users, mean centered by the corresponding item
and calculate a user-based adjusted cosine similarity.\\\\
User-based adjusted cosine similarity between users $u$ and $v$ is defined as:
\begin{equation}
\begin{split}
    &AC(u,v) = \frac{\sum_{i \in \mathcal{I}_{uv}}(r_{ui}-\bar{r_{i}})(r_{vi}-\bar{r_{i}})}
		    {\sqrt{\sum_{i \in \mathcal{I}_{uv}}(r_{ui}-\bar{r_{i}})^2}
                     \sqrt{\sum_{i \in \mathcal{I}_{uv}}(r_{vi}-\bar{r_{i}})^2}} \\\\
    &\bar{r_{i}} = \frac{\sum_{u \in \mathcal{U}_i}r_{iu}}
 		        {\mathopen|\mathcal{U}_i\mathclose|}
\end{split}
\end{equation}
where,
\begin{itemize}
	\item[] $\bar{r_i}$ is the mean value of item $i$ for each $i \in \mathcal{I}_{uv}$
\end{itemize}
From \autoref{table:Ratings Matrix}, AC
between $User_1$ and $User_3$ would thus be computed as follows:
\begin{align*}
	\begin{split}
		&\bar{r}_{Item_2} = \frac{2 + 3 + 2 + 5 + 1}{5} = 2.6\\
		&\bar{r}_{Item_4} = \frac{3 + 2 + 4 + 4 + 3 + 5}{6} = 3.5
	\end{split}
\end{align*}
$$AC(User_1,User_3) = \frac{(2 - 2.6)*(3 - 2.6) + (3 - 3.5)*(4 - 3.5)}
						 {\sqrt{(2 - 2.6)^2 + (3 - 3.5)^2}*
						  \sqrt{(3 - 2.6)^2 + (4 - 3.5)^2}} = −0.9798$$
$User_1$ and $User_3$ are almost perfectly dissimilar.
\subsection{Modified Adjusted Cosine Similarity}
A modification to adjusted cosine similarity that we implemented was to
use every user corresponding to $i$ and $j$ instead of using
only the users that have rated both items in the denominator. Its values are also in the interval [-1, 1].\\\\
Item-based modified adjusted cosine similarity between items $i$ and $j$ is defined as:
\begin{equation}
\begin{split}
&MAC(i,j) = \frac{\sum_{u \in \mathcal{U}_{ij}}(r_{iu}-\bar{r_{u}})(r_{ju}-\bar{r_{u}})}
				 {\sqrt{\sum_{u \in \mathcal{U}_{i}}(r_{iu}-\bar{r_{u}})^2}
				  \sqrt{\sum_{u \in \mathcal{U}_{j}}(r_{ju}-\bar{r_{u}})^2}} \\\\
&\bar{r_{u}} = \frac{\sum_{i \in \mathcal{I}_u}r_{ui}}
					{\mathopen|\mathcal{I}_u\mathclose|}
\end{split}
\end{equation}
where,
\begin{itemize}
	\item[] $\bar{r_u}$ is the mean value of user $u$ for each $u \in \mathcal{U}_{ij}$ in the numerator
	\item[] $\bar{r_u}$ is the mean value of user $u$ for each $u \in \mathcal{U}_{i}$ and $u \in \mathcal{U}_{j}$ respectively in the denominator
\end{itemize}
From \autoref{table:Ratings Matrix}, MAC
between $Item_2$ and $Item_3$ would thus be computed as follows:
\footnotesize
\begin{align*}
	\begin{split}
		&\bar{r}_{User_1} = \frac{5 + 2 + 3}{3} = 3.33\\
		&\bar{r}_{User_2} = \frac{1 + 4 + 2}{3} = 2.33\\
		&\bar{r}_{User_3} = \frac{3 + 5 + 4}{3} = 4\\
		&\bar{r}_{User_4} = \frac{5 + 2 + 3}{3} = 3.33\\
		&\bar{r}_{User_5} = \frac{1 + 2 + 4}{3} = 2.33\\
		&\bar{r}_{User_6} = \frac{3 + 5 + 3}{3} = 3.66\\
		&\bar{r}_{User_7} = \frac{3 + 1 + 5}{3} = 3\\\\
&MAC(Item_2,Item_3) = \\&\frac{(3 - 4)*(5 - 4) + (2 - 3.33)*(3 - 3.33)}
						 {\sqrt{(2 - 3.33)^2 + (3 - 4)^2 + (2 - 3.33)^2 + (5 - 3.66)^2 + (1 - 3)^2}*
						  \sqrt{(4 - 2.33)^2 + (5 - 4)^2 + (3 - 3.33)^2 + (2 - 2.33)^2}} \\&= −0,0872
  \end{split}
\end{align*}
\normalsize
Compared to item-based AC the item-based MAC between $Item_2$ and $Item_3$ still generates
a negative similarity but it is very close to zero this time as it takes into account each
item's vector length.\\\\
As previously mentioned a user-based approach can also be implemented.\\
User-based modified adjusted cosine similarity between users $u$ and $v$ is defined as:
\begin{equation}\label{eq:modified_adjusted_cosine}
\begin{split}
    &MAC(u,v) = \frac{\sum_{i \in \mathcal{I}_{uv}}(r_{ui}-\bar{r_{i}})(r_{vi}-\bar{r_{i}})}
                     {\sqrt{\sum_{i \in \mathcal{I}_{u}}(r_{ui}-\bar{r_{i}})^2}
                      \sqrt{\sum_{i \in \mathcal{I}_{v}}(r_{vi}-\bar{r_{i}})^2}} \\\\
    &\bar{r_{i}} = \frac{\sum_{u \in \mathcal{U}_i}r_{iu}}
                        {\mathopen|\mathcal{U}_i\mathclose|}
\end{split}
\end{equation}
where,
\begin{itemize}
	\item[] $\bar{r_i}$ is the mean value of user $u$ for each $u \in \mathcal{I}_{uv}$ in the numerator
	\item[] $\bar{r_i}$ is the mean value of user $u$ for each $u \in \mathcal{I}_{u}$ and $u \in \mathcal{I}_{v}$ respectively in the denominator
\end{itemize}
From \autoref{table:Ratings Matrix}, MAC
between $User_1$ and $User_3$ would thus be computed as follows:
\begin{align*}
	\begin{split}
		&\bar{r}_{Item_1} = 3\\
		&\bar{r}_{Item_2}  = 2.6\\
		&\bar{r}_{Item_3} = 3.5\\
		&\bar{r}_{Item_4} = 3.5\\\\
MAC(User_1,User_3) &= \frac{(2 - 2.6)*(3 - 2.6) + (3 - 3.5)*(4 - 3.5)}
						 {\sqrt{(5 - 3)^2 + (2 - 2.6)^2 + (3 - 3.5)^2}*
						  \sqrt{(3 - 2.6)^2 + (5 - 3.5)^2 + (4 - 3.5)^2}} \\&= −0.1399
  \end{split}
\end{align*}
Compared to user-based AC, the user-based MAC between $User_1$ and $User_3$ still generates
a negative similarity but it is far weaker than AC's as it takes into account the length of
each user's vector.
\subsection{Pearson Correlation Coefficient}
Users do not always rate in the same way. One user might never use a rating of 5
for items that really likes.
Another user might never rate an item below 3 for items that really dislikes.
A third user might only use a rating of 4 despite of how much he liked an item.
This means is that most of the users have a particularity in their
ratings. Due to this user behavior, it is difficult to understand the similarity
based on the actual rating. The Pearson correlation coefficient
computes the linear correlation between two users, to alleviate this problem. Linear correlation is
defined as the proportion of dependence between two variables X and Y.
Regarding the similarity between two users, the Pearson correlation coefficient can also be
thought of as the covariance between two users A and B divided by the standard deviation of
$User_A$ multiplied by the standard deviation of $User_B$.\\\\
User-based Pearson correlation coefficient between users $u$ and $v$ is defined as:
\begin{equation}\label{eq:pearson}
\begin{split}
    &PCC(u,v) = \frac{\sum_{i \in \mathcal{I}_{uv}}(r_{ui}-\bar{r_{u}})(r_{vi}-\bar{r_{v}})}
                     {\sqrt{\sum_{i \in \mathcal{I}_{uv}}(r_{ui}-\bar{r_{u}})^2}
                      \sqrt{\sum_{i \in \mathcal{I}_{uv}}(r_{vi}-\bar{r_{v}})^2}} \\\\
    &\bar{r_{u}} = \frac{\sum_{i \in \mathcal{I}_u}r_{ui}}
                        {\mathopen|\mathcal{I}_u\mathclose|}\\
    &\bar{r_{v}} = \frac{\sum_{i \in \mathcal{I}_v}r_{vi}}
                        {\mathopen|\mathcal{I}_v\mathclose|}
\end{split}
\end{equation}
where,
\begin{itemize}
	\item[] $\bar{r_u}$ is the mean value of user $u$
	\item[] $\bar{r_v}$ is the mean value of user $v$
\end{itemize}
The PCC varies between -1 and 1 and based on its value two objects inspected for correlation can
be classified as:
\begin{itemize}
	\item Positively correlated, when $PCC > 0$
	\item Negatively correlated, when $PCC < 0$
	\item Uncorrelated,  when $PCC = 0$
\end{itemize}
From \autoref{table:Ratings Matrix}, PCC
between $User_1$ and $User_3$ would thus be computed as follows:
\begin{align*}
	\begin{split}
		&\bar{r}_{User_1} = 3.33\\
		&\bar{r}_{User_3} = 4\\\\
		PCC(User_1,User_3) &= \frac{(2 - 3.33) * (3 - 4) + (3 - 3.33) * (4 - 4)}
								  {\sqrt{(2 - 3.33)^2 + (3 - 3.33)^2} *
								   \sqrt{(3 - 4)^2 + (4 - 4)^2}} = 0.9705
	\end{split}
\end{align*}
Thus $User_1$ and $User_3$ have a strong positive correlation.\\\\
PCC was initially used as a user-based approach\citep{shardanand1995social} but we will
reverse it for item-based computations in order to measure the correlation between the items.\\\\
Item-based Pearson correlation coefficient between items $i$ and $j$ is defined as:
\begin{equation}
	\begin{split}
	&PCC(i,j) = \frac{\sum_{u \in \mathcal{U}_{ij}}(r_{iu}-\bar{r_{i}})(r_{ju}-\bar{r_{j}})}
					 {\sqrt{\sum_{u \in \mathcal{U}_{ij}}(r_{iu}-\bar{r_{i}})^2}
					  \sqrt{\sum_{u \in \mathcal{U}_{ij}}(r_{ju}-\bar{r_{j}})^2}} \\\\
	&\bar{r_{i}} = \frac{\sum_{u \in \mathcal{U}_i}r_{iu}}
						{\mathopen|\mathcal{U}_i\mathclose|}\\
	&\bar{r_{j}} = \frac{\sum_{u \in \mathcal{U}_j}r_{ju}}
						{\mathopen|\mathcal{U}_j\mathclose|}
\end{split}
\end{equation}
where,
\begin{itemize}
	\item[] $\bar{r_i}$ is the mean value of item $i$
	\item[] $\bar{r_j}$ is the mean value of item $j$
\end{itemize}
From \autoref{table:Ratings Matrix}, PCC
between $Item_2$ and $Item_3$ would thus be computed as follows:
\begin{align*}
	\begin{split}
		&\bar{r}_{Item_2} = 2.6\\
		&\bar{r}_{Item_3} = 3.5\\\\
		PCC(Item_2,Item_3) &= \frac{(3 - 2.6) * (5 - 3.5) + (2 - 2.6) * (3 - 3.5)}
								  {\sqrt{(3 - 2.6)^2 + (2 - 2.6)^2} *
								   \sqrt{(5 - 3.5)^2 + (3 - 3.5)^2}} = 0.7893
	\end{split}
\end{align*}
Thus $Item_2$ and $Item_3$ have a strong positive correlation.
\subsection{Modified Pearson Correlation Coefficient 1}
The first modification to PCC that we implemented was to average only the ratings that
two users have in common. Its values are in the interval [-1, 1].\\\\
User-based modified Pearson correlation coefficient 1 between users $u$ and $v$ is defined as:
\begin{equation}\label{eq:pearson_1}
\begin{split}
    &MPCC1(u,v) = \frac{\sum_{i \in \mathcal{I}_{uv}}(r_{ui}-\tilde{r_{u}})(r_{vi}-\tilde{r_{v}})}
                       {\sqrt{\sum_{i \in \mathcal{I}_{uv}}(r_{ui}-\tilde{r_{u}})^2}
                        \sqrt{\sum_{i \in \mathcal{I}_{uv}}(r_{vi}-\tilde{r_{v}})^2}} \\\\
    &\tilde{r_{u}} = \frac{\sum_{i \in \mathcal{I}_{uv}}r_{ui}}
                          {\mathopen|\mathcal{I}_{uv}\mathclose|}\\
    &\tilde{r_{v}} = \frac{\sum_{i \in \mathcal{I}_{uv}}r_{vi}}
                          {\mathopen|\mathcal{I}_{uv}\mathclose|}
\end{split}
\end{equation}
where,
\begin{itemize}
	\item[] $\tilde{r_u}$ is the mean value of user $u$ using only the co-rated items with user $v$
	\item[] $\tilde{r_v}$ is the mean value of user $v$ using only the co-rated items with user $u$
\end{itemize}
From \autoref{table:Ratings Matrix}, MPCC1
between $User_1$ and $User_3$ would thus be computed as follows:
\begin{align*}
	\begin{split}
		&\tilde{r}_{User_1} = \frac{2 + 3}{2} = 2.5\\
		&\tilde{r}_{User_3} = \frac{3 + 4}{2} = 3.5\\\\
		MPCC1(User_1,User_3) &= \frac{(2 - 2.5) * (3 - 3.5) + (3 - 2.5) * (4 - 3.5)}
								  {\sqrt{(2 - 2.5)^2 + (3 - 2.5)^2} *
								   \sqrt{(3 - 3.5)^2 + (4 - 3.5)^2}} = 1
	\end{split}
\end{align*}
Compared to user-based PCC, the user-based MPCC1 between $User_1$ and $User_3$
using the modified mean values yields a perfect correlation.\\\\
Item-based modified Pearson correlation coefficient 1 between items $i$ and $j$ is defined as:
\begin{equation}
	\begin{split}
	&MPCC1(i,j) = \frac{\sum_{u \in \mathcal{U}_{ij}}(r_{iu}-\tilde{r_{i}})(r_{ju}-\tilde{r_{j}})}
					 {\sqrt{\sum_{u \in \mathcal{U}_{ij}}(r_{iu}-\tilde{r_{i}})^2}
					  \sqrt{\sum_{u \in \mathcal{U}_{ij}}(r_{ju}-\tilde{r_{j}})^2}} \\\\
	&\tilde{r_{i}} = \frac{\sum_{u \in \mathcal{U}_{ij}}r_{iu}}
						{\mathopen|\mathcal{U}_{ij}\mathclose|}\\
	&\tilde{r_{j}} = \frac{\sum_{u \in \mathcal{U}_{ij}}r_{ju}}
						{\mathopen|\mathcal{U}_{ij}\mathclose|}
\end{split}
\end{equation}
where,
\begin{itemize}
	\item[] $\tilde{r_i}$ is the mean value of item $i$ using only the common user ratings with item $j$
	\item[] $\tilde{r_j}$ is the mean value of item $j$ using only the common user ratings with item $i$
\end{itemize}
From \autoref{table:Ratings Matrix}, MPCC1
between $Item_2$ and $Item_3$ would thus be computed as follows:
\begin{align*}
	\begin{split}
		&\tilde{r}_{Item_2} = \frac{3 + 2}{2} = 2.5\\
		&\tilde{r}_{Item_3} = \frac{5 + 3}{2} = 4\\\\
		MPCC1(Item_2,Item_3) &= \frac{(3 - 2,5) * (5 - 4) + (2 - 2,5) * (3 - 4)}
								  {\sqrt{(3 - 2,5)^2 + (2 - 2,5)^2} *
								   \sqrt{(5 - 4)^2 + (3 - 4)^2}} = 1
	\end{split}
\end{align*}
Compared to item-based PCC, item-based MPCC1 between $Item_2$ and $Item_3$
using the modified mean values yields a perfect correlation.
\subsection{Modified Pearson Correlation Coefficient 2}
The second modification to PCC is to change the denominator
in a sense that it includes all the items corresponding to $u$ and $v$ respectively.
Its values are also in the interval [-1, 1].\\\\
User-based modified Pearson correlation coefficient 2 between users $u$ and $v$ is defined as:
\begin{equation}\label{eq:pearson_2}
\begin{split}
    &MPCC2(u,v) = \frac{\sum_{i \in \mathcal{I}_{uv}}(r_{ui}-\bar{r_{u}})(r_{vi}-\bar{r_{v}})}
                       {\sqrt{\sum_{i \in \mathcal{I}_{u}}(r_{ui}-\bar{r_{u}})^2}
                        \sqrt{\sum_{i \in \mathcal{I}_{v}}(r_{vi}-\bar{r_{v}})^2}} \\\\
    &\bar{r_{u}} = \frac{\sum_{i \in \mathcal{I}_u}r_{ui}}
                        {\mathopen|\mathcal{I}_u\mathclose|}\\
    &\bar{r_{v}} = \frac{\sum_{i \in \mathcal{I}_v}r_{vi}}
                        {\mathopen|\mathcal{I}_v\mathclose|}
\end{split}
\end{equation}
where,
\begin{itemize}
	\item[] $\bar{r_u}$ is the mean value of user $u$
	\item[] $\bar{r_v}$ is the mean value of user $v$
\end{itemize}
From \autoref{table:Ratings Matrix}, MPCC2
between $User_1$ and $User_3$ would thus be computed as follows:
\begin{align*}
	\begin{split}
		&\bar{r}_{User_1} = 3.33\\
		&\bar{r}_{User_3} = 4\\\\
		MPCC2(User_1,User_3) &= \frac{(2 - 3.33) * (3 - 4) + (3 - 3.33) * (4 - 4)}
								  {\sqrt{(5 - 3.33)^2 + (2 - 3.33)^2 + (3 - 3.33)^2} *
								   \sqrt{(3 - 4)^2 + (5 - 4)^2 + (4 - 4)^2}} = 0.4353
	\end{split}
\end{align*}
Compared to user-based PCC, the user-based MPCC2 between $User_1$ and $User_3$
retained a positive correlation but it has significantly decreased as it now
takes into account the mean centered length of each user's vector.\\\\
Item-based modified Pearson correlation coefficient 2 between items $i$ and $j$ is defined as:
\begin{equation}
	\begin{split}
	&MPCC2(i,j) = \frac{\sum_{u \in \mathcal{U}_{ij}}(r_{iu}-\bar{r_{i}})(r_{ju}-\bar{r_{j}})}
					 {\sqrt{\sum_{u \in \mathcal{U}_{i}}(r_{iu}-\bar{r_{i}})^2}
					  \sqrt{\sum_{u \in \mathcal{U}_{j}}(r_{ju}-\bar{r_{j}})^2}} \\\\
	&\bar{r_{i}} = \frac{\sum_{u \in \mathcal{U}_i}r_{iu}}
						{\mathopen|\mathcal{U}_i\mathclose|}\\
	&\bar{r_{j}} = \frac{\sum_{u \in \mathcal{U}_j}r_{ju}}
						{\mathopen|\mathcal{U}_j\mathclose|}
\end{split}
\end{equation}
From \autoref{table:Ratings Matrix}, MPCC2
between $Item_2$ and $Item_3$ would thus be computed as follows:
\footnotesize
\begin{align*}
	\begin{split}
		&\bar{r}_{Item_2} = 2.6\\
		&\bar{r}_{Item_3} = 3.5\\\\
		&MPCC2(Item_2,Item_3) = \\&\frac{(3 - 2,6) * (5 - 3,5) + (2 - 2,6) * (3 - 3,5)}
								  {\sqrt{(2 - 2,6)^2 + (3 - 2,6)^2 + (2 - 2,6)^2 + (5 - 2,6)^2 + (1 - 2,6)^2 } *
								   \sqrt{(4 - 3,5)^2 + (5 - 3,5)^2 + (3 - 3,5)^2 + (2 - 3,5)^2 }} = 0,1327
	\end{split}
\end{align*}
\normalsize
Compared to item-based PCC, item-based MPCC2 between $Item_2$ and $Item_3$
retained a positive correlation but it has significantly decreased as it now
takes into account the mean centered length of each item's vector.
\subsection{Mean Squared Difference Similarity}
Mean squared difference\citep{shardanand1995social} is a very simple metric.
\begin{equation}\label{eq:msd}
    MSD(u,v) = \frac{\sum_{i \in \mathcal{I}_{uv}}(r_{ui}-r_{vi})^2}
					{\mathopen|\mathcal{I}_{uv}\mathclose|}
\end{equation}
It designates the degree of dissimilarity between $u$ and $v$ by aggregating the squared differences
between the ratings on the rated items they have in common.
If we reverse the numerator with the denominator it designates the degree
of the similarity between $u$ and $v$ instead.\\\\
User-based mean squared difference similarity between users $u$ and $v$ is defined as:
\begin{equation}
    MSD(u,v) = \frac{\mathopen|\mathcal{I}_{uv}\mathclose|}
                    {\sum_{i \in \mathcal{I}_{uv}}(r_{ui}-r_{vi})^2}
\end{equation}
Its values are in the
interval (0, $\mathopen|\mathcal{I}_{uv}\mathclose|$] in user-based approach. There
is a chance $u$ and $v$ have given exactly the same ratings to each common item between them. In that case MSD similarity
is not defined. That can also be interpreted as $u$ and $v$ have zero dissimilarity.\\\\
From \autoref{table:Ratings Matrix}, MSD
between $User_1$ and $User_3$ would thus be computed as follows:
$$MSD(User_1,User_3) = \frac{2}{(2 - 3)^2 + (3 - 4)^2} = 1$$\\
The maximum similarity between $User_1$ and $User_3$ could be 2. They have
similarity 1 which means that they are rating somewhat by the same way.\\
Item-based mean squared difference similarity between items $i$ and $j$ is defined as:
\begin{equation}
        MSD(i,j) = \frac{\mathopen|\mathcal{U}_{ij}\mathclose|}
                        {\sum_{u \in \mathcal{U}_{ij}}(r_{iu}-r_{ju})^2}
\end{equation}
Its values are in the interval (0, $\mathopen|\mathcal{U}_{ij}\mathclose|$] in item-based approach.\\\\
From \autoref{table:Ratings Matrix}, MSD
between $Item_2$ and $Item_3$ would thus be computed as follows:
$$MSD(Item_2,Item_3) = \frac{2}{(3 - 5)^2 + (2 - 3)^2} = 0.4$$\\
The maximum similarity between $Item_2$ and $Item_3$ could be 2. They have
similarity 0.4 which means that these items are not rated similarly.
\subsection{Mean Absolute Difference Similarity}
Another very similar metric to MSD, is mean absolute difference similarity.
Its difference from the previous method is that it uses the absolute value of
the difference between the ratings of $u$ and $v$ instead of squaring them.
When big differences between the ratings exist, MSD is significantly penalized when squaring the values.
For that reason, using the absolute values helps in moderating the penalty.\\\\
User-based mean absolute difference similarity between users $u$ and $v$ is defined as:
\begin{equation}\label{eq:mad}
    MAD(u,v) = \frac{\mathopen|\mathcal{I}_{uv}\mathclose|}
                    {\sum_{i \in \mathcal{I}_{uv}}\mathopen|r_{ui}-r_{vi}\mathclose|}
\end{equation}
Its values are in the
interval (0, $\mathopen|\mathcal{I}_{uv}\mathclose|$] in user-based approach.\\\\
From \autoref{table:Ratings Matrix}, MAD
between $User_1$ and $User_3$ would thus be computed as follows:
$$MAD(User_1,User_3) = \frac{2}{\mathopen|2 - 3\mathclose| + \mathopen|3 - 4\mathclose|} = 1$$\\\\
Item-based mean absolute difference between items $i$ and $j$ is defined as:
\begin{equation}
MAD(i,j) = \frac{\mathopen|\mathcal{U}_{ij}\mathclose|}
				{\sum_{u \in \mathcal{U}_{ij}}\mathopen|r_{iu}-r_{ju}\mathclose|}
\end{equation}
Its values are in the interval (0, $\mathopen|\mathcal{U}_{ij}\mathclose|$] in item-based approach.\\\\
From \autoref{table:Ratings Matrix}, MAD
between $Item_2$ and $Item_3$ would thus be computed as follows:
$$MAD(Item_2,Item_3) = \frac{2}{\mathopen|3 - 5\mathclose| + \mathopen|2 - 3\mathclose|} = 0.4$$\\
\subsection{Jaccard Coefficient}
Jaccard coefficient measures the overlap of the ratings between two users.
This similarity metric does not use the rating scores that the users have given, but counts
how many common items they have have rated $\mathopen|\mathcal{I}_{uv}\mathclose|$, and how many distinct
items have rated together in total $\mathopen|\mathcal{I}_u\mathclose| \cup \mathopen|\mathcal{I}_v\mathclose| = \mathopen|\mathcal{I}_{u}\mathclose| +
\mathopen|\mathcal{I}_{v}\mathclose| - \mathopen|\mathcal{I}_{uv}\mathclose|$.
Its values are in the interval [0, 1].\\\\
User-based mean absolute difference similarity between users $u$ and $v$ is defined as:
\begin{equation}\label{eq:jaccard}
    J(u,v) = \frac{\mathopen|\mathcal{I}_{uv}\mathclose|}
                  {\mathopen|\mathcal{I}_{u}\mathclose| +
		   \mathopen|\mathcal{I}_{v}\mathclose| -
		   \mathopen|\mathcal{I}_{uv}\mathclose|}
\end{equation}
The table below is an extension of
\autoref{table:Ratings Matrix} for $User_1$ and $User_7$ for demonstration purpose.
\begin{table}[H]
\centering
\begin{tabular}{ |c|c|c|c|c|c|c| }
\hline
\diagbox{User}{Item} & \textbf{$Item_1$} & \textbf{$Item_2$} & \textbf{$Item_3$} & \textbf{$Item_4$} & \textbf{$Item_5$} & \textbf{$Item_6$} \\
\hline
\textbf{$User_1$} & 5 & 2 & \textbf{?}  & 3 & \textbf{?} & \textbf{?}  \\
\hline
\textbf{$User_7$} & 3 & 1 & \textbf{?} & 5 & 2 & 4 \\
\hline
\end{tabular}
\caption{$User_1$ and $User_7$ extended vectors}
\label{table:jaccard_example1}
\end{table}
The first step is to transform the ratings vector between two users to ones and zeros. Ones are the existing ratings and zeros are the ratings that are missing.
\begin{table}[H]
\centering
\begin{tabular}{ |c|c|c|c|c|c|c| }
\hline
\diagbox{User}{Item} & \textbf{$Item_1$} & \textbf{$Item_2$} & \textbf{$Item_3$} & \textbf{$Item_4$} & \textbf{$Item_5$} & \textbf{$Item_6$} \\
\hline
\textbf{$User_1$} & 1 & 1 & 0 & 1 & 0 & 0 \\
\hline
\textbf{$User_7$} & 1 & 1 & 0 & 1 & 1 & 1 \\
\hline
\end{tabular}
\caption{$User_1$ and $User_7$ vectors transformed}
\label{table:jaccard_example2}
\end{table}
Then count the amount of items $u$ and $v$ have rated in common $\mathopen|\mathcal{I}_{uv}\mathclose|$, in this example $User_1$ and $User_7$,
divided by the amount of their items union $\mathopen|\mathcal{I}_u\mathclose| \cup \mathopen|\mathcal{I}_v\mathclose|$.
$$J(User_1,User_7) = \frac{3}{3 + 5 - 3} = 0.6$$\\\\
Similarly, this metric can be used for item-based similarities.\\
Item-based mean absolute difference between items $i$ and $j$ is defined as:
\begin{equation}
J(i,j) = \frac{\mathopen|\mathcal{U}_{ij}\mathclose|}
		  {\mathopen|\mathcal{U}_{i}\mathclose| +
   \mathopen|\mathcal{U}_{j}\mathclose| -
   \mathopen|\mathcal{U}_{ij}\mathclose|}
\end{equation}
From \autoref{table:Ratings Matrix}, Jaccard coefficient
between $Item_2$ and $Item_3$ would thus be computed as follows:
$$J(Item_2,Item_3) = \frac{2}{5 + 4 - 2} = 0.2857$$
\section{K-Nearest Neighbors Algorithm}\label{sec:2.3}
The K-Nearest Neighbors algorithm is a very straightforward technique.
In a user-based KNN setting, in order to predict the rating of $User_A$ for an
$Item_B$ not rated by $User_A$, KNN consists of the following steps:
\begin{itemize}
	\item[] \textbf{Step 1:} Find users that have rated $Item_B$.
	\item[] \textbf{Step 2:} Compute the similarities between $User_A$ and the users that have
	rated $Item_B$.
	\item[] \textbf{Step 3:}  Sort that similarities in descending order.
	\item[] \textbf{Step 4:}  Choose how many neighbors will contribute in the rating
	prediction by selecting the top $\mathcal{K}$ out of all the available
	neighbors($\mathcal{K}$ can be in range [1 - $\mathcal{N}$] where $\mathcal{N}$ is all
	the available neighbors).
	\item[] \textbf{Step 5:} Use an aggregation formula to calculate the rating prediction of
	$User_A$ to $Item_B$. In this case the weighted sum \citep{sarwar2001item} is used.
\begin{equation}\label{nearest_neighbors}
	\hat{r}(User_A,Item_B) = \frac{\sum_{u \in \mathcal{K}}{similarity(User_A,User_u) * r(User_u,Item_B)}}
						    {\sum_{u \in \mathcal{K}}{\mathopen|similarity(User_A,User_u)\mathclose|}}
\end{equation}
\end{itemize}

Thus, the numerator computes the weighted of ratings of the nearest neighbors,
weighted by their similarities with $User_A$.
In a way the similarity can be interpreted as how much users influence
$User_A$. The denominator sums the absolute values of similarities in order to scale the
outcome of the rating prediction in the range [1 - 5]. The absolute value is used because
similarity metrics like Pearson correlation coefficient can take negative values which
will disturb the scaling.\\

In an item-based KNN setting, in order to predict the rating of $User_A$ for an
$Item_B$ not rated by $User_A$, KNN consists of the following steps:
\begin{itemize}
	\item[] \textbf{Step 1:} Find items that have been rated by $User_A$.
	\item[] \textbf{Step 2:} Compute the similarities between $Item_B$ and the items that have
	been rated by $User_A$.
	\item[] \textbf{Step 3:}  Sort that similarities in descending order.
	\item[] \textbf{Step 4:}  Choose how many neighbors will contribute in the rating
	prediction.
	\item[] \textbf{Step 5:} Use an aggregation formula to calculate the rating prediction of
	$User_A$ to $Item_B$.
\begin{equation}\label{nearest_neighbors}
	\hat{r}(User_A,Item_B) = \frac{\sum_{i \in \mathcal{K}}{similarity(Item_B,Item_i) * r(User_A,Item_i)}}
						    {\sum_{i \in \mathcal{K}}{\mathopen|similarity(Item_B,Item_i)\mathclose|}}
\end{equation}
\end{itemize}

There are 4 important things to take into consideration in order to obtain the most accurate
outcome out with the KNN algorithm:
\begin{enumerate}
	\item Utilize User-based or Item-based CF.
	\item Choose the most appropriate similarity metric that will surface the best connections
	between users or items.
	\item Choose the optimal $\mathcal{K}$ for the rating predictions. It is argued from
	experiments that a value of $\mathcal{K}$ between [20 - 50] often yields the best
	results. A small $\mathcal{K}$ typically < 10 has not accounted for enough opinions and a
	large $\mathcal{K}$ > 50 adds a lot of "noise" to the prediction \citep{herlocker2002empirical,Jannach}.
	\item Choose an appropriate prediction formula.
\end{enumerate}

\section{KNN Example}
To demonstrate the KNN algorithm the previous steps from \autoref{sec:2.3} will be used
to predict how $User_4$ would rate $Item_4$ from the ratings matrix
(\autoref{table:Ratings Matrix}), based on a user-based KNN procedure:
\begin{itemize}
	\item[] \textbf{Step 1:} Find users that have rated $Item_4$.\\
	All other users in the ratings matrix have rated $Item_4$.
	\item[] \textbf{Step 2:} Compute the similarities between $User_4$ and the users that have
	rated $Item_4$.\\
	For this example \autoref{eq:cosine} will be used to compute the similarities.\\
	\begin{align*}
		\begin{split}
			&cos(User_1, User_4) = \frac{5*5 + 2*2}{\sqrt{5^2 + 2^2 + 3^2}*\sqrt{5^2 + 2^2 + 3^2}} = 0.7632\\
			&cos(User_2, User_4) = \frac{1*5 + 4*3}{\sqrt{1^2 + 4^2 + 2^2}*\sqrt{5^2 + 2^2 + 3^2}} = 0.6018\\
			&cos(User_3, User_4) = \frac{3*2 + 5*3}{\sqrt{3^2 + 5^2 + 4^2}*\sqrt{5^2 + 2^2 + 3^2}} = 0.4818\\
			&cos(User_4, User_5) = \frac{5*1 + 3*2}{\sqrt{5^2 + 2^2 + 3^2}*\sqrt{1^2 + 2^2 + 4^2}} = 0.3894\\
			&cos(User_4, User_6) = \frac{5*3 + 2*5}{\sqrt{5^2 + 2^2 + 3^2}*\sqrt{3^2 + 5^2 + 3^2}} = 0.6185\\
			&cos(User_4, User_7) = \frac{5*3 + 2*1}{\sqrt{5^2 + 2^2 + 3^2}*\sqrt{3^2 + 1^2 + 5^2}} = 0.4661\\
		\end{split}
	\end{align*}
	\begin{figure}[H]
	\centering
	\includegraphics[width=0.5\textwidth]{chapter_2/KNN_example.eps}
	\caption{Cosine Similarities for $User_4$}
	\label{figure:KNN_example}
	\end{figure}
	\item[] \textbf{Step 3:}  Sort that similarities in descending order.
	\begin{align*}
		\begin{split}
			&cos(User_1, User_4) = 0.7632\\
			&cos(User_4, User_6) = 0.6185\\
			&cos(User_2, User_4) = 0.6018\\
			&cos(User_3, User_4) = 0.4818\\
			&cos(User_4, User_7) = 0.4661\\
			&cos(User_4, User_5) = 0.3894\\
		\end{split}
	\end{align*}
	\item[] \textbf{Step 4:}  Choose how many neighbors will contribute in the rating
	prediction in this case we choose for example, $\mathcal{K}=3$.
	\begin{align*}
		\begin{split}
			&cos(User_1, User_4) = 0.7632\\
			&cos(User_4, User_6) = 0.6185\\
			&cos(User_2, User_4) = 0.6018\\
		\end{split}
	\end{align*}
	\begin{figure}[H]
	\centering
	\includegraphics[width=0.5\textwidth]{chapter_2/KNN_example1.eps}
	\caption{Select top $\mathcal{K}=3$ similar users}
	\label{figure:neighbor_selection}
	\end{figure}
	\item[] \textbf{Step 5:} Predict how $User_4$ will rate $Item_4$ using the weighted sum.
	$$\hat{r}(User_4,Item_4) = \frac{0.7632*3 + 0.6018*2 + 0.6185*3}{0.7632 + 0.6018 + 0.6185} = 2.7$$
\end{itemize}


%!TEX root = ../Thesis.tex
\chapter{Code} \label{app:code-ho-pe}

\section{Harmonic Oscillator} \label{app:ho}
\begin{adjustwidth*}{0cm}{-0.4cm}
\begin{lstlisting}[language=Python]
"""
Harmonic Oscillator Numerical Solver Module
===========================================
A collection of various numerical solvers for the harmonic oscillator:
    Euler:
        Explicit
        Implicit
        Symplectic

Functions:
    euler: Solves harmonic oscillator by 1st order Euler method explicitely,
           implicitely or symplectically.

We assume k/m = 1 in all of the following.

"""

import numpy as np

def euler(duration,h,kind,x0=0,v0=1):
    """Euler algorithm for harmonic oscillator. Includes three variations: explicit, implicit and symplectic.

    Args:
        duration (float): Time duration of simulation.
        h (int): Step size.
        kind (str): Valid strings: 'explicit', 'implicit', 'symplectic'.
        x0 (float): Initial position (default value: 0)
        v0 (float): Initial velocity (default value: 1)

    Returns:
        Tuple of time-, position- and speed lists

    """
    # Number of time steps
    n = int(duration/h)
    # Vector initialization
    xlist=np.zeros(n+1) # n steps + ic's --> n+1
    vlist=np.zeros(n+1)
    # Initialize initial conditions
    xlist[0] = x0
    vlist[0] = v0

    # Make tlist, ensure it always have length n+1
    if (duration/h % 2) == 0:
        tlist=np.arange(0,duration+h/2,h)  # Includes the endpoint
    else:
        tlist=np.arange(0,duration,h)

    # Euler algorithms
    if kind == 'explicit':
        for i in range(n):
            xlist[i+1] = xlist[i] + h*vlist[i]
            vlist[i+1] = vlist[i] - h*xlist[i]

    elif kind == 'implicit':
        for i in range(n):
            xlist[i+1] = 1/(h**2 + 1)*(xlist[i] + h*vlist[i])
            vlist[i+1] = 1/(h**2 + 1)*(vlist[i] - h*xlist[i])

    elif kind == 'symplectic':
        for i in range(n):
            xlist[i+1] = xlist[i] + h*vlist[i]
            vlist[i+1] = (1-h**2)*vlist[i] - h*xlist[i]

    elif kind == 'symplectic2':
        for i in range(n):
            xlist[i+1] = (1-h**2)*xlist[i] + h*vlist[i]
            vlist[i+1] = vlist[i] - h*xlist[i]

    else:
        print("Error: 3rd argument 'kind' must be 'explicit', 'implicit' or 'symplectic'")

    return tlist,xlist,vlist
\end{lstlisting}
\end{adjustwidth*}

\section{Pendulum} \label{app:pe}
\begin{adjustwidth*}{0cm}{-0.4cm}
\begin{lstlisting}[language=Python]
"""
Pendulum Numerical Solver Module
================================
A collection of various numerical solvers for the mathematical penduluim:
    Euler:
        Explicit
        Implicit
        Symplectic

Functions:
    euler: Solves the pendulum by Euler method explicitely, implicitely or symplectically.

We assume 1/ml = 1  and  m*g*l = 1 in all of the following.

"""

import numpy as np
import root_finding as rf

def euler(duration,h,kind,theta0=0,omega0=1):
    """Euler algorithm for the pendulum. Includes three variations: explicit, implicit and symplectic.

    Args:
        duration (float): Time duration of simulation.
        h (int): Step size.
        kind (str): Valid strings: 'explicit', 'implicit', 'symplectic'.
        theta0 (float): Initial angle (default value: 0)
        omega0 (float): Initial angular velocity (default value: 1)

    Returns:
        Tuple of time-, angle- and angular velocity lists

    """
    # Number of time steps
    n = int(duration/h)
    # Vector initialization
    thetalist=np.zeros(n+1) # n steps + ic's --> n+1
    omegalist=np.zeros(n+1)
    # Initialize initial conditions
    thetalist[0] = theta0
    omegalist[0] = omega0

    # Make tlist, ensure it always have length n+1
    if (duration/h % 2) == 0:
        tlist=np.arange(0,duration+h/2,h)  # Includes the endpoint
    else:
        tlist=np.arange(0,duration,h)

    # Euler algorithms
    if kind == 'explicit':
        for i in range(n):
            thetalist[i+1] = thetalist[i] + h*omegalist[i]
            omegalist[i+1] = omegalist[i] - h*np.sin(thetalist[i])

    elif kind == 'implicit':
        for i in range(n):
            # f(x) and f'(x) used in Newton-Raphson root finder below
            f = lambda x: thetalist[i] - x + (omegalist[i] - np.sin(x)*h)*h
            g = lambda x: -1 - np.cos(x)*h*h  # g = f'
            # Find thetalist[i+1] numerically by root finding, guess thetalist[i]
            thetalist[i+1] = rf.newton_raphson(f,g,thetalist[i])
            omegalist[i+1] = omegalist[i] - np.sin(thetalist[i+1])*h

    elif kind == 'symplectic':
        for i in range(n):
            omegalist[i+1] = omegalist[i] - h*np.sin(thetalist[i])
            thetalist[i+1] = thetalist[i] + h*omegalist[i+1]

    else:
        print("Error: 3rd argument 'kind' must be 'explicit', 'implicit' or 'symplectic'")

    return tlist,thetalist,omegalist
\end{lstlisting}
\end{adjustwidth*}