\section{Main idea}

Consider a \emph{Riordan array} $\mathcal{R}\left(d(t),h(t)\right)$
defined over generating functions $d$ and $h$.
By definition, coefficients lying on
column $k$ are the coefficients of the following combination:
\begin{displaymath}
    d(t)h(t)^k
\end{displaymath}
our idea is to characterize $\mathcal{R}$ doing a variable change, as the
following manipulation catches:
\begin{displaymath}
    d(t)h(t)^k = d(t)(1 + (h(t)-1))^k = \left[ \left. d(\hat{h}(1+y))(1+y)^k \right|y = h(t)-1  \right]
\end{displaymath}
where function $\hat{h}$ is the compositional inverse of $h$, the one that
satisfies $\hat{h}(h(t)) = t$: in order to get $t$ back, apply $\hat{h}$ to
both members of $y+1 = h(t)$.
Inside the square brackets there's a shape of a Riordan array, therefore:
\begin{displaymath}
    \begin{split}
        \mathcal{R}\left(d(t),h(t)\right) &= \left[ \mathcal{R}\left(d(\hat{h}(1+y)), 1+y\right) \left| y = h(t)-1 \right. \right]\\
        &= \mathcal{R}_{y=h(t)-1}\left( f(y), 1+y \right) =  \mathcal{R}_{h(t)}\left( g(h(t)), h(t) \right)
    \end{split}
\end{displaymath}
$\mathcal{R}_{y=h(t)-1}$ is interesting since its first component $f(y)$ allows to
develop a new array $\mathcal{R}_{h(t)}$ where it's first component
is a function $g$ in the ``variable'' $h(t)$, eventually the moral is:
\begin{quote}
    \graffito{$h$-characterization}
    \emph{function composition $d$ with $\hat{h}$ yields array $\mathcal{R}_{h(t)}$,\\
        the $h$-characterization of $\mathcal{R}$, which depends on function $h$ only }
\end{quote}
It's important to understand the meaning of those new symbols:
\begin{itemize}
    \item $\mathcal{R}_{\gamma=\Omega(t)}$ denotes an array where a variable
        substitution has to take place, namely $\Omega(t)$ is the substitution
        for variable $\gamma$;
        \marginpar{$\mathcal{R}_{t}(d(t),h(t))$ denotes verbosly, but correctly, the same
            array $\mathcal{R}(d(t),h(t))$ denotes}
    \item $\mathcal{R}_{\Omega(t)}$ denotes an array where $\Omega(t)$ should
        be seen as a variable, like a \emph{datum}, and should not be expanded,
        even if $\Omega$'s definition is known.
        % the same concept can be expressed as follow:
            %(a point very important to understand: here $h(t)$ shouldn't be
            %interpreted as a \emph{function}, the one in $\mathcal{R}$'s definition, instead
            %abstract over it and consider it as a \emph{variable}).
        % this has been cut from a paragraph within `Group operations, revisited' section
\end{itemize}

\label{par:h:characterization:is:an:array:polymorphism} Moreover, the
$h$-characterization $\mathcal{R}_{h(t)}\left(g(h(t)),h(t)\right)$,
\marginpar{$\mathcal{R}_{h(t)}$ is polymorphic} for some function $g$, can be
used as a classic Riordan array, %\emph{trasparently},
where traditional function $d$ satisfies $d(t) = g(h(t))$.
On the other hand, function $h$ is interpreted as usual.

\subsection{Applying it to known triangles}

In the following sections we characterize some well known
arrays under this development.

\subsubsection{Pascal}
Let $\mathcal{P}$ be the Riordan array for the Pascal triangle,
defined as:
\begin{displaymath}
    \mathcal{P} = \left(\frac{1}{1-t}, \frac{t}{1-t}  \right)
\end{displaymath}
computing the compositional inverse of $h$ yields:
\begin{displaymath}
    \hat{h}(y) = \frac{y}{1+y}
\end{displaymath}
so $f(y)=d(\hat{h}(1+y))=2+y$ therefore:
\begin{displaymath}
    \mathcal{P}_{y=h(t)-1}\left( 2+y, 1+y \right)= \mathcal{P}_{h(t)}\left( 1+h(t), h(t) \right)
\end{displaymath}
Now study the generating function which carries coefficients lying on column $k$
of $\mathcal{P}_{h(t)}$:
\marginpar{this recall the Rogers' work about \emph{renewal arrays},
which we described in \autoref{sec:back:to:the:basics:rogers}}
\begin{displaymath}
    h(t)^k + h(t)^{k+1}
\end{displaymath}
hence column $k$ is the sum of $k$-fold and $(k+1)$-fold convolutions
of function $h$ with itself.

\subsubsection{Fibonacci}
Let $\mathcal{F}$ be the Riordan array for the Fibonacci triangle,
defined as:
\begin{displaymath}
    \mathcal{F} = \left(\frac{1}{1-t-t^2}, \frac{1-\sqrt{1-4t}}{2}  \right)
\end{displaymath}
computing the compositional inverse of $h$ yields:
\begin{displaymath}
    \hat{h}(y) = y - y^2
\end{displaymath}
so $f(y)=d(\hat{h}(1+y))=\frac{1}{1+y-2y^3-y^4}$ therefore:
\begin{displaymath}
    \begin{split}
        & \mathcal{F}_{y=h(t)-1}\left( \frac{1}{1+y-2y^3-y^4}, 1+y \right) = \mathcal{F}_{h(t)}\left( \frac{1}{1-h(t)+2h(t)^3-h(t)^4}, h(t) \right)\\
    \end{split}
\end{displaymath}
Now study the generating function which carries coefficients lying on column $k$
of $\mathcal{F}_{h(t)}$:
\begin{displaymath}
    \frac{h(t)^k}{1-h(t)+2h(t)^3-h(t)^4}
\end{displaymath}

\subsubsection{Catalan}
Let $\mathcal{C}$ be the Riordan array for the Catalan triangle,
defined as:
\begin{displaymath}
    \mathcal{C} = \left(\frac{1-\sqrt{1-4t}}{2t}, \frac{1-\sqrt{1-4t}}{2}  \right)
\end{displaymath}
computing the compositional inverse of $h$ yields:
\begin{displaymath}
    \hat{h}(y) = y - y^2
\end{displaymath}
so $f(y)=d(\hat{h}(1+y))=\frac{\sqrt{4 \, y^{2} + 4 \, y + 1} - 1}{2 \, {\left(y + 1\right)} y}$ therefore:
\marginpar{$h_{\mathcal{F}}=h_{\mathcal{C}}$, here function $d$ makes the difference}
\begin{displaymath}
    \begin{split}
        &\mathcal{C}_{y=h(t)-1}\left(\frac{\sqrt{4 \, y^{2} + 4 \, y + 1} - 1}{2 \, {\left(y + 1\right)} y}, 1+y \right) \\
        &= \mathcal{C}_{h(t)}\left(\frac{\sqrt{4 \, h\left(t\right)^{2} - 4 \, h\left(t\right) + 1} - 1}{2 \, {\left(h\left(t\right) - 1\right)} h\left(t\right)}, h(t) \right)\\
        &= \mathcal{C}_{h(t)}\left(\frac{\sqrt{(2 h\left(t\right) -  1)^2} - 1}{2 \, {\left(h\left(t\right) - 1\right)} h\left(t\right)}, h(t) \right)\\
    \end{split}
\end{displaymath}
By cases on $\sqrt{(2 h\left(t\right) -  1)^2}$:
\marginpar{$\sqrt{x^{2}}=\pm x$ trick}
\begin{itemize}
    \item $\sqrt{(2 h\left(t\right) -  1)^2}=2 h\left(t\right) -  1$, hence:
        \begin{displaymath}
            \mathcal{C}_{h(t)}\left(\frac{2(h\left(t\right) -  1)}{2 \, {\left(h\left(t\right) - 1\right)} h\left(t\right)}, h(t) \right)=
            \mathcal{C}_{h(t)}\left(\frac{1}{h\left(t\right)}, h(t) \right)
        \end{displaymath}
        since $h(0)=0$ the previous result has no meaning, therefore discard this case;
    \item $\sqrt{(2 h\left(t\right) -  1)^2}=1 -2 h\left(t\right)$, hence:
        \begin{displaymath}
            \mathcal{C}_{h(t)}\left(\frac{h\left(t\right)}{ {\left(1-h\left(t\right) \right)} h\left(t\right)}, h(t) \right)=
            \mathcal{C}_{h(t)}\left(\frac{1}{1-h\left(t\right)}, h(t) \right)
        \end{displaymath}
        checking against $h(0)=0$ the previous result has meaning, therefore accept this case.
\end{itemize}
Now study the generating function which carries coefficients lying on column $k$
of $\mathcal{C}_{h(t)}$:
\begin{displaymath}
    \frac{h(t)^{k}}{1-h\left(t\right)}
\end{displaymath}

\subsubsection{Motzkin, classic version}

Let $\mathcal{M}$ be the Riordan array for the Motzkin triangle, defined as:
\begin{displaymath}
    \mathcal{M} =\left( \frac{1-t-\sqrt{1-2t-3t^2}}{2t^2}, \frac{1-t-\sqrt{1-2t-3t^2}}{2t}  \right)
\end{displaymath}
computing the compositional inverse of $h$ yields:
\begin{displaymath}
    \hat{h}(y) = \frac{y}{1+y+y^2}
\end{displaymath}
so $f(y)=d(\hat{h}(1+y))$ equals:
\begin{lenghtydisplaymath}
    \begin{split}
        & -\frac{{\left(y^{2} \sqrt{\frac{{\left(y^{2} + 4 \, y + 4\right)} y^{2}}{{\left(y^{2} + 3 \, y + 3\right)}^{2}}} - y^{2} + 3 \, y \sqrt{\frac{{\left(y^{2} + 4 \, y + 4\right)} y^{2}}{{\left(y^{2} + 3 \, y + 3\right)}^{2}}} - 2 \, y + 3 \, \sqrt{\frac{{\left(y^{2} + 4 \, y + 4\right)} y^{2}}{{\left(y^{2} + 3 \, y + 3\right)}^{2}}} - 2\right)} {\left(y^{2} + 3 \, y + 3\right)}}{2 \, {\left(y + 1\right)}^{2}} \\
        & = -\frac{{ \left(\sqrt{\frac{{\left(y^{2} + 4 \, y + 4\right)} y^{2}}{{\left(y^{2} + 3 \, y + 3\right)}^{2}}} \left( y^{2}+3y+3 \right) -\left(y^{2}+2y+2\right)\right){\left(y^{2} + 3 \, y + 3\right)}}}{2 \, {\left(y + 1\right)}^{2}} \\
        & = -\frac{ \left(\sqrt{\left(y^{2} + 4 y + 4\right) y^{2}} -\left(y^{2}+2y+2\right)\right)\left(y^{2} + 3y + 3\right)}{2 {\left(y + 1\right)}^{2}} \\
    \end{split}
\end{lenghtydisplaymath}
Recognize a perfect square $y^{2} + 4 y + 4 = (y+2)^{2}$:
\marginpar{we could also stop here and reason by cases about $\pm\left(\left(y + 2\right) y\right)$
    without going on substituting variable $y$ too soon\ldots}
\begin{lenghtydisplaymath}
        -\frac{ \left(\sqrt{\left(\left(y + 2\right) y\right)^{2}} -\left(y^{2}+2y+2\right)\right)\left(y^{2} + 3y + 3\right)}{2 {\left(y + 1\right)}^{2}}
\end{lenghtydisplaymath}
therefore:
\begin{lenghtydisplaymath}
    \begin{split}
        &\mathcal{M}_{y=h(t)-1}\left(
            -\frac{ \left(\sqrt{\left(\left(y + 2\right) y\right)^{2}} -\left(y^{2}+2y+2\right)\right)\left(y^{2} + 3y + 3\right)}{2 {\left(y + 1\right)}^{2}} , 1+y \right) \\
        &= \mathcal{M}_{h(t)}\left(
        -\frac{{\left(\sqrt{\frac{{\left(h(t)^{2} + 2 \, h(t) + 1\right)} {\left(h(t) - 1\right)}^{2}}{{\left(h(t)^{2} + h(t) + 1\right)}^{2}}}\left( h(t)^{2} + h(t) + 1\right)
            - (h(t)^{2} + 1) \right)} {\left(h(t)^{2} + h(t) + 1\right)}}{2 \, h(t)^{2}} , h(t) \right)\\
        &= \mathcal{M}_{h(t)}\left(
        -\frac{{\left(\sqrt{\left(h(t)^2 - 1\right)^{2}} - (h(t)^{2} + 1) \right)} {\left(h(t)^{2} + h(t) + 1\right)}}{2 \, h(t)^{2}} , h(t) \right)\\
    \end{split}
\end{lenghtydisplaymath}
By cases on $\sqrt{\left(h(t)^2 - 1\right)^{2}}$:
\begin{itemize}
    \item $\sqrt{\left(h(t)^2 - 1\right)^{2}}= h(t)^2 - 1$, hence:
        \begin{displaymath}
            \mathcal{M}_{h(t)}\left(\frac{ h(t)^{2} + h(t) + 1}{h(t)^{2}} , h(t) \right)\\
        \end{displaymath}
        since $h(0)=0$ the previous result has no meaning, therefore discard this case;
    \item $\sqrt{\left(h(t)^2 - 1\right)^{2}}= 1-h(t)^2$, hence:
        \begin{displaymath}
            \mathcal{M}_{h(t)}\left( h(t)^{2} + h(t) + 1, h(t) \right)\\
        \end{displaymath}
        checking against $h(0)=0$ the previous result has meaning, therefore accept this case.
\end{itemize}

It is interesting to note that the tedious derivation developed above can have
a simpler handling if we defer substituting $h(t)-1$ for $y$, consider the following \ldots
by cases on $\sqrt{\left(\left(y + 2\right) y\right)^{2}}$:
\begin{itemize}
    \item $\sqrt{\left(\left(y + 2\right) y\right)^{2}}=\left(y + 2\right) y$, hence:
        \begin{displaymath}
            \mathcal{M}_{y=h(t)-1}\left(\frac{y^{2} + 3y + 3}{{\left(y + 1\right)}^{2}} , 1+y \right) =
                \mathcal{M}_{h(t)}\left( \frac{1+h(t)+h(t)^2}{h(t)^2}, h(t) \right)
        \end{displaymath}
        since $h(0)=0$ the previous result has no meaning, therefore discard this case;
    \item $\sqrt{\left(\left(y + 2\right) y\right)^{2}}=-\left(y + 2\right) y$, hence:
        \begin{displaymath}
            \mathcal{M}_{y=h(t)-1}\left(y^{2} + 3y + 3 , h(t) \right) =
                \mathcal{M}_{h(t)}\left( 1+h(t)+h(t)^2, h(t) \right)
        \end{displaymath}
        checking against $h(0)=0$ the previous result has meaning, therefore accept this case.
\end{itemize}

In both branches same definitions for $\mathcal{M}_{h(t)}$ are reached applying
same checks, while at the same time substituting variable $y$ is easier.
\\\\
However, here it is the generating function which carries coefficients
lying on column $k$ of $\mathcal{M}_{h(t)}$:
\begin{displaymath}
    h(t)^{k}+h(t)^{k+1}+h(t)^{k+2}
\end{displaymath}

\subsubsection{Motzkin, $\mathcal{T}$ variant}

Let $\mathcal{T}$ be the Riordan array for a variant
of the Motzkin array defined as:
\begin{displaymath}
    \mathcal{T} = \left(\frac{1}{\sqrt{1-2t-3t^2}},
       \frac{1-t-\sqrt{1-2t-3t^2}}{2t}  \right)
\end{displaymath}
computing the compositional inverse of $h$ yields:
\begin{displaymath}
    \hat{h}(y) = \frac{y}{1+y+y^2}
\end{displaymath}
so $f(y)=d(\hat{h}(1+y))=\sqrt{\left(\frac{y^2+3y+3}{(y+2)y}\right)^{2}}$ therefore:
\begin{displaymath}
    \mathcal{T}_{y=h(t)-1}\left( \sqrt{\left(\frac{y^2+3y+3}{(y+2)y}\right)^{2}}, 1+y \right) =
        \mathcal{T}_{h(t)}\left( \sqrt{\left(\frac{h(t)^2+h(t)+1}{h(t)^2-1}\right)^{2}}, h(t) \right)
\end{displaymath}
By cases on $\sqrt{\left(\frac{h(t)^2+h(t)+1}{h(t)^2-1}\right)^{2}}$:
\begin{itemize}
    \item $\sqrt{\left(\frac{h(t)^2+h(t)+1}{h(t)^2-1}\right)^{2}}=\frac{h(t)^2+h(t)+1}{h(t)^2-1}$, hence:
        \begin{displaymath}
            \mathcal{T}_{h(t)}\left(\frac{h(t)^2+h(t)+1}{h(t)^2-1}, h(t) \right)
        \end{displaymath}
        requirement $h(0)=0$ doesn't raise a non-sense, so use the
        constraint $d(0)=1$, mandatory in order to have a \emph{proper} array, although
        condition can be relaxed to $d(0)\neq0$:
        \begin{displaymath}
            \left. \frac{h(t)^2+h(t)+1}{h(t)^2-1} \right|_{t=0} = -1 \not= 1
        \end{displaymath}
        so discard this case;
    \item $\sqrt{\left(\frac{h(t)^2+h(t)+1}{h(t)^2-1}\right)^{2}}=\frac{h(t)^2+h(t)+1}{1-h(t)^2}$, hence:
        \begin{displaymath}
            \mathcal{T}_{h(t)}\left(\frac{h(t)^2+h(t)+1}{1-h(t)^2}, h(t) \right)
        \end{displaymath}
        requirement $h(0)=0$ doesn't raise a non-sense, so check $d(0)=1$ again:
        \begin{displaymath}
            \left. \frac{h(t)^2+h(t)+1}{1-h(t)^2} \right|_{t=0} = 1
        \end{displaymath}
        which fulfill the constraint, so accept this case.
\end{itemize}
Now study the generating function which carries coefficients lying on column $k$
of $\mathcal{T}_{h(t)}$:
\marginpar{same numerator as in classic $\mathcal{M}$ array, what's
    the relation among function $d$ and multiplier $\frac{1}{1-h(t)^{2}}$?}
\begin{displaymath}
    \frac{h(t)^{k}+h(t)^{k+1}+h(t)^{k+2}}{1-h(t)^2 }
\end{displaymath}

\subsubsection{Delannoy}

Let $\mathcal{D}$ be the Riordan array for the Delannoy triangle, defined as:
\begin{displaymath}
    \mathcal{D} =\left( \frac{1}{1-t}, \frac{t(1+t)}{1-t}  \right)
\end{displaymath}
computing the compositional inverse of $h$ yields:
\begin{displaymath}
    \hat{h}(y) = \frac{\sqrt{1+6y+y^2}-y-1}{2}
\end{displaymath}
so $f(y)=d(\hat{h}(1+y))=\frac{2}{4 + y - \sqrt{y^2 + 8y + 8} }$ therefore:
\begin{displaymath}
    \begin{split}
        & \mathcal{D}_{y=h(t)-1}\left( \frac{2}{4+y-\sqrt{y^2+8y+8}}, 1+y \right)\\
        &= \mathcal{D}_{h(t)}\left( \frac{2}{3+h(t)-\sqrt{h(t)^2+6h(t)+1}}, h(t) \right) \\
    \end{split}
\end{displaymath}
Since $h(t)^2+6h(t)+1$ isn't a perfect square, no problem arises with the previous
derivation; now study the generating function which carries coefficients lying on column $k$
of $\mathcal{D}_{h(t)}$:
\begin{displaymath}
    \frac{2\,h(t)^k}{3+h(t)-\sqrt{h(t)^2+6h(t)+1}}
\end{displaymath}

\subsection{Points of view}

Let $\mathcal{R}\left(d(t),h(t)\right)$ be a Riordan array and $\mathcal{R}_{h(t)}$ be
its $h$-characterization.

The first point of view we would like to introduce is to see
$\mathcal{R}_{h(t)}$ as a \emph{factorization} \marginpar{$\mathcal{R}_{h(t)}$ factorizes $\mathcal{R}$}
of $\mathcal{R}$ in terms of $h(t)$. Namely, the structure of the characterization
depends both on functions $d$ and $\hat{h}$, but the building block is function $h$
alone. Moreover, second component of $\mathcal{R}_{h(t)}$ is \emph{always} function $h$ itself,
therefore $k$-fold convolution of function $h$ with itself in the generic
$k$ column expansion is common both to $\mathcal{R}$ both to $\mathcal{R}_{h(t)}$.

The second point of view is to see $\mathcal{R}_{h(t)}$ as a \emph{schema} array
\marginpar{$\mathcal{R}_{h(t)}$ is a schema}.
To understand this concept, abstract over $h(t)$ and think about it as a ``plugin'',
a ``context'' that can be filled with any function $g(t)$ you like, to get a new array
$\mathcal{R}^{\stackrel{g(t)}{\rightarrow}}$ (this notation is just a reminder that the array is obtained
by plugging in $g(t)$ into the schema $\mathcal{R}_{h(t)}$). It's pretty easy, and sound, to check:
\marginpar{plugging in $h(t)$ itself within schema $\mathcal{R}_{h(t)}$ safely gets back
    to the original array $\mathcal{R}$}
\begin{displaymath}
    \mathcal{R}^{\stackrel{h(t)}{\rightarrow}} = \mathcal{R}_{y=h(t)-1}\left( d(\hat{h}(1+y)), 1+y \right) = \mathcal{R}
\end{displaymath}
while considering an arbitrary function $g(t)$:
\begin{displaymath}
    \mathcal{R}^{\stackrel{g(t)}{\rightarrow}} = \mathcal{R}_{y=g(t)-1}\left( d(\hat{h}(1+y)), 1+y \right) =
    \left( d(\hat{h}(g(t))), g(t) \right)
\end{displaymath}
observe how the first component of $\mathcal{R}^{\stackrel{g(t)}{\rightarrow}}$ depends on
$\mathcal{R}$ by composition of functions $d$ and $\hat{h}$. So, every array seems to have
a ``nested schema'' that allows to build new arrays.
\\\\
We finish with a theorem about arrays in the \emph{renewal} subgroup.

\begin{theorem}
    Let $\mathcal{R}\left(d(t), h(t)\right)$ be a Riordan array belonging
    to the \emph{renewal} subgroup, so $h(t)=td(t)$. Then:
    \marginpar{it can be written as $\left[\left.\mathcal{R}\left(A(y), y)\right)\right|y=h(t)\right]$
        or as $\mathcal{R}_{y=h(t)}\left(A(y), y)\right)$ too}
    \begin{displaymath}
        \mathcal{R}_{h(t)}\left(A(h(t)), h(t))\right)
    \end{displaymath}
    where $A$ is a \ac{fps} over $\mathcal{R}$'s $A$-sequence
    $\lbrace a_i \rbrace_{i\in\mathbb{N}}$.
\end{theorem}

\begin{proof}
    Recall that $A$-sequence of a Riordan array $\mathcal{R}\left(d(t), h(t)\right)$
    satisfies:
    \begin{displaymath}
        h(t)=tA(h(t))
    \end{displaymath}
    By hypothesis, assume $\mathcal{R}$ belongs to the \emph{renewal} subgroup, therefore
    \begin{displaymath}
        \mathcal{R}\left(A(h(t)), h(t)\right)
    \end{displaymath}
    Let $\mathcal{R}_{h(t)}\left(g(h(t)), h(t)\right)$ be the $h$-characterization of $\mathcal{R}$,
    for some function $g$. Now plug $h(t)$ into $\mathcal{R}_{h(t)}$:
    \begin{displaymath}
        \mathcal{R}^{\stackrel{h(t)}{\rightarrow}} =
            \mathcal{R}\left(g(h(t)), h(t)\right)
    \end{displaymath}
    But $\mathcal{R}^{\stackrel{h(t)}{\rightarrow}} = \mathcal{R}$, therefore
    $[g(y)=A(y)|y=h(t)]$ follows, as required.

\end{proof}

%\marginpar{$\mathcal{R}_{h(t)}\left(A(h(t)), h(t))\right)\rightarrow
    %\mathcal{R}\left(d(t), t\,d(t)\right)$ holds too}
\marginpar{the converse is true indeed, by $A$-sequence uniqueness}
Looking at it deeply, it is possible to have a stronger formulation, namely the
converse holds indeed: the reason for this is the unique existence of an $A$-sequence
for each Riordan array, the argument follows using $h(t)=tA(h(t))$ again .
\\\\
This little theorem shows a possible application of the factorization point of view.
Consider the Motzkin array defined as:
\begin{displaymath}
    \mathcal{M} =\left( \frac{1-t-\sqrt{1-2t-3t^2}}{2t^2},
       \frac{1-t-\sqrt{1-2t-3t^2}}{2t}  \right)
\end{displaymath}
Surely $\mathcal{M}$ is a renewal array and, being in the Riordan group,
has an $A$-sequence, which satisfies $A(t)=1+t+t^2$:
this information \emph{can't} be read from the above definition.
\\\\
\marginpar{finding $\mathcal{M}$'s $A$-sequence}
Let $h(t)=\frac{1-t-\sqrt{1-2t-3t^2}}{2t}$ be the second
component of Motzkin array $\mathcal{M}$, which factor as follows:
\begin{displaymath}
    \mathcal{M}_{h(t)}\left( 1+h(t)+h(t)^2, h(t) \right)
\end{displaymath}
using this $h$-characterization, $A$-sequence $[A(y)=1+y+y^2|y=h(t)]$ can
be read from $\mathcal{M}_{h(t)}$'s first component directly, without
solving the equation as usual:
\begin{displaymath}
    \frac{1-t-\sqrt{1-2t-3t^2}}{2t} = t\,A\left(\frac{1-t-\sqrt{1-2t-3t^2}}{2t}\right)
\end{displaymath}


% !TEX root = main.tex

%-------------------------------------------------
\section{Null hypothesis siginficance testing}\label{sec:nhst}

Let $X$ be a random variable, let $\mathcal{M}=\{F(x;\theta):\theta\in\Theta\}$ be a statistical model for its distribution and let $\{\Theta_0, \Theta_1\}$ be a \emph{partition} of the parameter space:
\[
\Theta_0\cup\Theta_1 = \Theta \qquad\text{and}\qquad \Theta_0\cap\Theta_1 = \emptyset.
\]
This partition defines two possible statistical models for the distribution of $X$.
\[
\begin{array}{ll}
\text{The \emph{null} model:}			&\quad\mathcal{M}_0	 =\{F(x,\theta):\theta\in\Theta_0\}.	\\
\text{The \emph{alternative} model:}	&\quad\mathcal{M}_1	 =\{F(x,\theta):\theta\in\Theta_1\}.
\end{array}
\]
To decide which model is the `correct' one, we need to test the claim that the true parameter belongs to the set $\Theta_0$ against the alternative claim that it belongs to $\Theta_1$. We denote these two hypotheses by $H_0$ and $H_1$ respectively and refer to them as follows.

\begin{center}
\begin{tabular}{ll} \hline
The \emph{null} hypothesis:		& \quad $H_0: \theta\in\Theta_0$. \\
The \emph{alternative} hypothesis:	& \quad $H_1: \theta\in\Theta_1$. \\ \hline
\end{tabular}
\end{center}

\begin{definition}
\bit
\it A hypothesis which specifies a particular value of $\theta$ is called a \emph{simple hypothesis}.
\it A hypothesis which specifies a set of values for $\theta$ is called a \emph{composite hypothesis}.
\eit
\end{definition}

For example, $H_0:\theta = \theta_0$ is a simple hypothesis while $H_1:\theta\neq\theta_0$ is a composite hypothesis.

%-----------------------------
\subsection{Type I and Type II errors}

In the absence of any evidence to the contrary, we assume that $H_0$ is correct. Suppose we now obtain a random sample $\mathbf{x}=(x_1,x_2,\ldots,x_n)$ from the distribution of $X$, and compute an estimate $T(\mathbf{x})$ of the true parameter value $\theta$. We decide which hypothesis is correct based on the computed value of $T(\mathbf{x})$:
\bit
\it if $T(\mathbf{x})\in\Theta_0$ we retain the null hypothesis $H_0:\theta\in\Theta_0$;
\it if $T(\mathbf{x})\in\Theta_1$ we reject $H_0$ in favour of the alternative hypothesis $H_1:\theta\in\Theta_1$.
\eit

\begin{definition}
\bit
\it A \emph{Type I error} occurs when $\theta\in\Theta_0$ but $T(\mathbf{x})\in\Theta_1$, which leads us to incorrectly reject $H_0$.
\it A \emph{Type II error} occurs when $\theta\in\Theta_1$ but $T(\mathbf{x})\in\Theta_0$, which leads us to incorrectly retain $H_0$.
\eit
\end{definition}

% decision tables
Hypothesis tests can be respresented by \emph{decision tables}:
\begin{center}
\begin{tabular}{|c|c|c|} \hline
											& \multicolumn{2}{c|}{Reality} \\
Decision									& $H_0$ true ($\theta\in\Theta_0$)	& $H_0$ false ($\theta\in\Theta_1$) \\ \hline
Retain $H_0$ ($T\in\Theta_0$)	& Correct decision 					& Type II error \\
Reject $H_0$ ($T\in\Theta_1$)	& Type I error 						& Correct decision \\ \hline
\end{tabular}
\end{center}

%\begin{center}
%\begin{tabular}{|c|c|c|} \hline
%							& \multicolumn{2}{c|}{Reality} \\
%Decision					& $\theta\in\Theta_0$		& $\theta\in\Theta_1$ \\ \hline
%$T\in\Theta_0$	& Correct decision 	& Type II error \\
%$T\in\Theta_1$	& Type I error 		& Correct decision \\ \hline
%\end{tabular}
%\end{center}
%
% example
Different applications use different terminology. A decision table for a radar system, where the null hypothesis asserts the absence of a target, might be as follows:
%Here is decision table for a radar systems, where the null hypothesis asserts the absence of a target:
\begin{center}
\begin{tabular}{|c|c|c||c|} \hline
				& \multicolumn{2}{c||}{Reality} & \\
Decision		& 	Target Absent	& Target Present 	& Action\\  \hline
Target Absent	&	Clear			& Miss 				& Stay silent \\
Target Present	&	False alarm 	& Hit 				& Sound alarm \\  \hline
\end{tabular}
\end{center}
Here is a decision table for a medical diagnosis, where the null hypothesis asserts the absence of a disease:
\begin{center}
\begin{tabular}{|c|c|c||c|} \hline
				& \multicolumn{2}{c||}{Reality} &  \\
Decision		& Disease Absent	& Disease Present	& Action \\  \hline
Disease Absent	& True negative		& False negative	& Do nothing  \\
Disease Present	& False positive 	& True positive 	& Prescribe \\  \hline
\end{tabular}
\end{center}

%-----------------------------
\subsection{Critical regions}

\begin{definition}
Let $\mathbf{X}=(X_1,X_2,\ldots,X_n)$ be a random sample from the distribution of $X$.
The set of all possible realisations of $\mathbf{X}$ is called the \emph{sample space} which we denote by $D\subseteq\R^n$.
\end{definition}

\begin{center}
%\begin{table*}[ht]
%\centering
\begin{tabular}{lcl}\hline
Model ($\mathcal{M}$)	& & Sample space ($D$) \\ \hline
Bernoulli				& & binary vectors (of length $n$)\\
Poisson					& & vectors of non-negative integers \\
Normal					& & vectors of real numbers \\ \hline
\end{tabular}
%\caption*{Examples of sample spaces}
%\end{table*}
\end{center}

%\bigskip
%It can be shown that for every $\theta\in\Theta$, there exits a unique probability measure
%\[\begin{array}{rccl}
%\prob_{\theta}:	& \mathcal{B}_n	& \to 		& [0,1] \\
%				& A				& \mapsto	& \prob(\mathbf{X}\in A)
%\end{array}\]
%(where $\mathcal{B}_n$ is the Borel $\sigma$-field over $\R^n$) for which the component variables $X_i$ are independent and identically distribted according to $F(x;\theta)$. This is called the \emph{probability measure induced by $\mathbf{X}$} on the sample space.
%
% defn: critical region
\begin{definition}
A \emph{hypothesis test} of a null hypothesis $H_0$ against an alternative hypothesis $H_1$ is defined by a subset of the sample space called the \emph{critical region} of the test: we \emph{reject} $H_0$ if $\mathbf{X}\in C$ but \emph{retain} $H_0$ if $\mathbf{X}\notin C$.
\end{definition}

Critical regions can be specified in terms of a \emph{test statistic} say $T:D\to\R$, in which case the critical region is specified by one or more \emph{critical values}. For example, we might define
\[
C = \{\mathbf{x}\in D : c_1\leq T(\mathbf{x})\leq c_2\}
\]
in which case we reject $H_0$ if $T(\mathbf{x})$ falls between the critial values $c_1$ and $c_2$.

%% simple example
%\begin{example}
%Let $X_1,X_2,\ldots,X_n$ be a random sample from the $N(\mu,\sigma^2)$ distribution, where $\mu$ is unknown but $\sigma^2$ is known. Using the sample mean as a test statistic, define a suitable critical region for testing the null hypothesis $H_0:\mu=\mu_0$ against the alternative hypothesis $H_1:\mu\neq\mu_0$.
%\begin{solution}
%If we assume that $H_0$ is true, then $X_i\sim N(\mu_0,\sigma^2)$ and
%\[
%\frac{1}{n}\sum_{i=1}^n X_i \sim N\left(\mu_0, \frac{\sigma^2}{n}\right).
%\]
%For a particular realisation $\boldx=(x_1,x_2,\ldots,x_n)$ of the sample, if
%\bit
%\it $\displaystyle\frac{1}{n}\sum_{i=1}^n x_i\approx\mu_0$, we might decide to retain the null hypothesis $H_0:\mu=\mu_0$, but if
%\it $\displaystyle\frac{1}{n}\sum_{i=1}^n x_i\not\approx\mu_0$, we might decide to reject $H_0$ in favour of the alternative $H_1:\mu\neq\mu_0$.
%\eit
%Thus we define the critical region
%\[
%C = \left\{\boldx\in D: \left|\frac{1}{n}\sum_{i=1}^n x_i - \mu_0\right| > c\right\},
%\]
%where the critical value $c$ is chosen appropriately.
%\end{solution}
%\end{example}

%-----------------------------
\subsection{The size (or significance level) of a test}

We would like to choose a critical region $C$ that minimises the probability of making both Type I and Type II errors. These are conflicting objectives, as illustrated by the following extreme cases.
\bit
\it If we choose $C=\emptyset$ we will never reject $H_0$ (because the random sample never falls into $C$), so we never make Type I errors when $\theta\in\Theta_0$ but always make Type II errors when $\theta\in\Theta_1$.
\it If we choose $C=D$ we will always reject $H_0$ (because the random sample always falls into $C$), so we never make Type II errors when $\theta\in\Theta_1$ but always make Type I errors when $\theta\in\Theta_0$.
\eit

% remark: status quo
\begin{remark}[Conservatie testing]
The null hypothesis represents the \textit{status quo}. From a conservative standpoint, rejecting the status quo incorrectly (Type I error) is worse than retaining the status quo incorrectly (Type II error). As a result, hypothesis tests usually proceed in two stages:
\ben
\it find a set of tests for which $\prob(\text{Type I error})$ is bounded above by some acceptable value, then
\it choose one of these tests so that $\prob(\text{Type II error})$ is as small as possible.
\een
\end{remark}

%We compute these probabilities over all possible realisations of the sample $\mathbf{X}=(X_1,X_2,\ldots,X_n)$. If $F(x;\theta)$ is the common CDF of the $X_i$, it can be shown that for every $\theta\in\Theta$, there exits a unique probability measure
\begin{definition}
Let $\mathbf{X}=(X_1,X_2,\ldots,X_n)$ be a random sample and let $F_{\theta}(x)$ be the common CDF of the component variables $X_i$. It can be shown that for every $\theta\in\Theta$ there exits a unique probability measure $\prob_\theta$ on subsets of $\R^n$ for which the component variables $X_i$ are independent and identically distribted according to $F_{\theta}(x)$. This is called the \emph{probability distribution induced by $\mathbf{X}$} on the sample space and is denoted by $\prob_{\theta}(A)=\prob_{\theta}(\mathbf{X}\in A)$, which is the probability that %$\mathbf{X}$
the random sample falls into the set $A\subseteq\R^n$ when the parameter value is $\theta$.%: we often write $\prob_{\theta}(\mathbf{X}\in A)$ instead of $\prob_{\theta}(A)$.
\end{definition}

% defn: size
\begin{definition}
The \emph{size} of a critical region (also called the \emph{significance level} of the test) is the maximum probability of making a Type I error. This is usually denoted by $\alpha$,
\[
\alpha = \max_{\strut\theta\in\Theta_0}\ \prob_{\theta}(\mathbf{X}\in C).
\]
If $H_0$ is a simple hypothesis, say $H_0:\theta=\theta_0$, this reduces to $\alpha = \prob_{\theta_0}(\mathbf{X}\in C)$.
\end{definition}

\begin{remark}[$p$-values]
Let $T:D\to\R$ be a test statistic, let $C = \{\mathbf{x} : T(\mathbf{x}) \leq c\}$ be a critical region and suppose that $\mathbf{x}_{\text{obs}}$ is the observed sample realisation. Then the \emph{empirical size} (or $p$-value) of the test is defined to be
\[
p = \max_{\theta\in\Theta_0}\,\prob_{\theta}\big[ T(\mathbf{X})\leq T(\mathbf{x}_{\text{obs}})\big].
\]
If $H_0$ is a simple hypothesis, say $H_0:\theta=\theta_0$, this reduces to
\[
p = \prob_{\theta_0}\big[ T(\mathbf{X})\leq T(\mathbf{x}_{\text{obs}})\big].
\]
For a test of size $\alpha$, we reject $H_0$ if the empirical size satisfies $p\leq\alpha$.
\end{remark}

%-----------------------------
\subsection{The power of a test}

Among all critical regions of size $\alpha$ we would like to choose one that minimises the probability $\prob_{\theta}(\mathbf{X}\notin C)$ of making Type II errors for every $\theta\in\Theta_1$, or equivalently a critical region that maximises $\prob_{\theta}(\mathbf{X}\in C)$ for every $\theta\in\Theta_1$.

\begin{definition}
Let $C$ be a critical region for testing $H_0:\theta\in\Theta_0$ against $H_1:\theta\in\Theta_1$.
The \emph{power function} of the associated test is
\[
\gamma(\theta) = \prob_{\theta}(\mathbf{X}\in C) \quad\text{which is defined for all $\theta\in\Theta_1$.}
\]
The value $\gamma(\theta)$ is called the \emph{power of the test to detect the alternative hypothesis at $\theta\in\Theta_1$}.
\end{definition}

% remark: beta
\begin{remark}
The probability of making Type II errors is often denoted by $\beta$:
\begin{align*}
\beta(\theta)
	& = \prob_{\theta}(\mathbf{X}\notin C) \\
	& = 1-\gamma(\theta).% \quad \text{for $\theta\in\Theta_1$.}
\end{align*}
To maximise the power $\gamma(\theta)$ is to minimise the probability $\beta(\theta)$ of making a Type II error.
\end{remark}

%% more powerful
%Let $C_1$ and $C_2$ be two critical regions of size $\alpha$, and let $\gamma_1$ and $\gamma_2$ respectively denote the power functions of the associated tests. If $\gamma_1(\theta) > \gamma_2(\theta)$ for all $\theta\in\Theta_1$, we say that $C_1$ is a \emph{more powerful test} than $C_2$. Later we prove that the \emph{simple likelihood ratio test} is at least as powerful as any other test of a simple null hypothesis against a simple alternative.
%
\begin{example}
Let $X_1,X_2,\ldots,X_8$ be a random sample from the $\text{Poisson}(\theta)$ distribution, where $\theta>0$ is unknown. We reject the simple null hypothesis $H_0:\theta=0.5$ in favour of the alternative $H_1:\theta>0.5$ whenever the observed sum satisfies $\sum_{i=1}^8 X_i \geq 8$.
\ben
\it Compute the size of the test.
\it Compute the power of the test at $\theta = 0.75$, $\theta = 1.0$ and $\theta = 1.25$.
\een
Use the fact that if $X\sim\text{Poisson}(\theta_1)$ and $Y\sim\text{Poisson}(\theta_2)$ then $X+Y\sim\text{Poisson}(\theta_1+\theta_2)$.
\end{example}

\begin{solution}
The critical region of the test is
\[
C = \big\{\mathbf{x} : T(\mathbf{x}) \geq 8\big\}
\text{\quad where\quad}
T(\mathbf{X}) = \sum_{i=1}^8 X_i.
\]
By the hint, if $X_i\sim\text{Poisson}(\theta)$, then $T(\boldX)\sim\text{Poisson}(8\theta)$. This is our test statistic.
\ben
\it % << (i)
Under $H_0:\theta=0.5$, we have $T\sim\text{Poisson}(4)$ so
\begin{align*}
\alpha	= \prob_{0.5}\big(\boldX\in C\big)
		& = \prob(T\geq 8) \text{ where } T\sim\text{Poisson}(4) \\
		& = 1 - \prob(T\leq 7) \text{ where } T\sim\text{Poisson}(4) \\
		& = 1 - 0.9489 = 0.0511 \quad\text{(from tables).}
\end{align*}
\it % << (ii)
Under $H_1:\theta=0.75$, we have $T\sim\text{Poisson}(6)$ so
\begin{align*}
\gamma(0.75) = \prob_{0.75}\big(\boldX\in C\big)
			& = \prob(T\geq 8)\text{ where }T\sim\text{Poisson}(6) \\
			& = 1 - \prob\big(T\leq 7)\text{ where }T\sim\text{Poisson}(6) \\
			& = 1 - 0.7440 = 0.2560 \quad\text{(from tables).}
\end{align*}
Under $H_1:\theta=1$, we have $T\sim\text{Poisson}(8)$ so
\begin{align*}
\gamma(1.0)	= \prob_{1.0}\big(\boldX\in C\big)
			& = \prob(T\geq 8)\text{ where }T\sim\text{Poisson}(8) \\
			& = 1 - \prob\big(T\leq 7)\text{ where } T\sim\text{Poisson}(8) \\
			& = 1 - 0.4530 = 0.4570 \quad\text{(from tables).}
\end{align*}
Under $H_1:\theta=1.25$, we have $T\sim\text{Poisson}(10)$ so
\begin{align*}
\gamma(1.25) = \prob_{1.25}\big(\boldX\in C;\theta\big)
			& = \prob(T\geq 8)\text{ where } T\sim\text{Poisson}(10) \\
			& = 1 - \prob(T\leq 7)\text{ where } T\sim\text{Poisson}(10) \\
			& = 1 - 0.2202 = 0.7798 \quad\text{(from tables).}
\end{align*}
Notice that the power of the test to detect the alternative hypothesis \emph{increases} as $\theta$ moves away from the value $\theta=0.5$ specified by the null hypothesis.
\een
\end{solution}


\newpage
\section{Group extensions}
Today our goal is to understand how addition of 2-digit numbers is equivalent to another abstract construction in group theory called group extensions.
Using this interpretation and some facts from group cohomology, we will eventually be able to answer the question,
\begin{q*}
  What are all the normalized, symmetric, 2-cocycles $c:\bbz/10 \times \bbz/10 \rightarrow \bbz/10$?
\end{q*}
\begin{mdframed}
  \adjustbox{scale=1,center}{%
    \begin{tikzcd}
      &\mbox{Adding 2-digit numbers}
        \ar[ddddr, leftrightarrow]
        &
      \\\\\\\\
      \mbox{\color{white}2-cocycle condition}
      & & \text{Group extensions}
    \end{tikzcd}
  }
\end{mdframed}

\subsection{Review: Group theory}
All our groups will be abelian unless otherwise specified.

A map between abelian groups $\varphi: G_1 \longrightarrow G_2$ is a group homomorphism if it satisfies
\begin{align*}
  \varphi(a + b) = \varphi(a) + \varphi(b)
\end{align*}

\begin{qbox}(Practice problem)
  \label{q:groupHomsExamples}
  Find all the group homomorphisms
  \begin{enumerate}
    \item $\bbz \longrightarrow \bbz$,
    \item $\bbz \longrightarrow \bbz/n$,
    \item $\bbz/n \longrightarrow \bbz$,
    \item $\bbz/n \longrightarrow \bbz/n^2$,
    \item $\bbz/n^2 \longrightarrow \bbz/n$,
  \end{enumerate}
  where $n$ is a positive integer.
\end{qbox}

\begin{qbox}
  Let $M$ be an abelian group. Describe all the group homomorphisms $ \bbz/n \longrightarrow M$, where $n$ is a positive integer.
  (This set will show up again when we discuss group cohomology.)
\end{qbox}

\begin{definition}
  The \emph{kernel} of a group homomorphism  $\varphi: G_1 \longrightarrow G_2$ is the set of elements $g \in G_1$ such that $\varphi(g) = 0$.
  \begin{align*}
    \ker \varphi = \set{ g \in G_1 : \varphi(g) = 0}
  \end{align*}
\end{definition}

\begin{definition}
  The \emph{image} of a group homomorphism  $\varphi: G_1 \longrightarrow G_2$ is the set of elements $\varphi(g) \in G_2$ where $g \in G_1$.
  \begin{align*}
    \im \varphi = \set{ \varphi(g) : g \in G_1}
  \end{align*}
\end{definition}

\begin{qbox}
  Find the kernel and image of the group homomorphisms you found in Q.\ref{q:groupHomsExamples}.
\end{qbox}

\begin{qbox}
  Which of the group homomorphisms in Q.\ref{q:groupHomsExamples} are
  \begin{enumerate}
    \item injective (=one-to-one)?
    \item surjective (=onto)?
    \item isomorphisms (=one-to-one and onto)?
  \end{enumerate}
\end{qbox}

\subsection*{Optional practice problems}

\begin{qbox}
  Show that the image of a group homomorphism  $\varphi: G_1 \longrightarrow G_2$ is a subgroup of $G_2$.
\end{qbox}

\begin{qbox}
  Show that the kernel of a group homomorphism  $\varphi: G_1 \longrightarrow G_2$ is a subgroup of $G_1$.
\end{qbox}

\begin{qbox}
  Show that for a group homomorphism $\varphi: G_1 \longrightarrow G_2$ we have $G_1 / \ker \varphi \cong \im \varphi$.
\end{qbox}

\newpage
\subsection{Group extensions}

\begin{qbox}
  \label{q:inclusionMaps}
  Consider the two inclusion maps,
  \begin{align*}
    i_u: \bbz/10 &\longrightarrow \bbz/100  &&& i_t: \bbz/10 & \longrightarrow \bbz/100 \\
    b &\longmapsto \tens{0}\units{b} &&& a &\longmapsto \tens{a}\units{0}
  \end{align*}
  Which of these two maps is a group homomorphism?
\end{qbox}

\begin{qbox}
  \label{q:projectionMaps}
  Consider the two projection maps,
  \begin{align*}
    p_u: \bbz/100 &\longrightarrow \bbz/10 &&& p_t: \bbz/100 &\longrightarrow \bbz/10 \\
    \tens{a}\units{b} &\longmapsto {b} &&& \tens{a}\units{b} &\longmapsto a
  \end{align*}
  Which of these two maps is a group homomorphism?
\end{qbox}

\begin{qbox}
  \label{q:SES1}
  For the $i$ and $p$ in Questions \ref{q:inclusionMaps} and \ref{q:projectionMaps} that are group homomorphisms, check that
  \begin{enumerate}
    \item $i$ is injective,
    \item $p$ is surjective,
    \item $\im i = \ker p$.
  \end{enumerate}
\end{qbox}

\begin{definition}
  An \emph{extension} of a group $K$ by $H$ is a group $G$ along with a pair of maps
  \begin{align*}
    i: H \longrightarrow G && p: G \longrightarrow K
  \end{align*}
  such that
  \begin{enumerate}
    \item $i$ is injective,
    \item $p$ is surjective,
    \item $\im i = \ker p$.
  \end{enumerate}
\end{definition}

This is often written as a \emph{short exact sequence}
\begin{equation*}
  \begin{tikzcd}
    0 \ar[r] & H \ar[r] &  G  \ar[r] & K \ar[r] & 0
  \end{tikzcd}
\end{equation*}

\newpage
Q.\ref{q:SES1} is saying that the following is a SES
\begin{equation*}
  \begin{tikzcd}
    0 \ar[r] & \bbz/10 \ar[r,"i"] &  \bbz/100  \ar[r,"p"] & \bbz/10 \ar[r] & 0 \\
     & \mbox{tens} \ar[u, equal]  &  \mbox{2-digit numbers}  \ar[u,equal] & \mbox{units}  \ar[u, equal] &
  \end{tikzcd}
\end{equation*}

In fact, every group extension arises in this manner.
Consider an extension of abelian groups,
\begin{equation*}
  \begin{tikzcd}
    0 \ar[r] & H \ar[r,"i"] &  G  \ar[r, "p"] & K \ar[r] & 0.
  \end{tikzcd}
\end{equation*}
i.e.
\begin{enumerate}
  \item $i$ is injective,
  \item $p$ is surjective,
  \item $\im i = \ker p$.
\end{enumerate}

For an element $a \in H$, denote by $\tens{a}\units{0} \in G$ the element $i(a)$.
For an element $b \in K$, let $\tens{0}\units{b}$ be \emph{some} element in $G$ such that $p(\tens{0}\units{b}) = b$.
Define $\tens{a} \units{b}$ to be the element $\tens{a}\units{0} + \tens{0}\units{b}$ in $G$.

\begin{qbox}
  Show that $p(\tens{a}\units{0}) = 0$ for any $a \in H$.
\end{qbox}

% \begin{qbox}
%   % \begin{enumerate}
%   %   \item
%     Check that $\tens{a_1}\units{0} + \tens{a_2}\units{0} = \tens{a_1 + a_2}\units{0}$.
%     % \item Is it true that $\tens{0}\units{b_1} + \tens{0}\units{b_2} = \tens{0}\units{b_1 + b_2}$?
%   % \end{enumerate}
% \end{qbox}

\begin{qbox}
  \label{q:uniquenessOfDigits}
  Let $a_1$, $a_2$ be elements in $H$ and let $b_1$, $b_2$ be elements in $K$, show that\hint{Apply $p$ to both sides of $[a_1][b_1] = [a_2][b_2]$.}
\begin{align*}
    [a_1][b_1] = [a_2][b_2] \implies a_1 = a_2 \mbox{ and } b_1 = b_2.
\end{align*}
\end{qbox}

\begin{qbox}
  \label{q:existenceOfDigits}
  Show that for every element $g \in G$, there exist unique elements $a \in H$ and $ b \in K $ such that $g = \units{a}\tens{b}$.
  \hint{Look at $p(g)$ and $g - \tens{p(g)}\units{0}$.}
\end{qbox}


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath,amssymb}
\newcommand\set[1]{\left\{#1\right\}}

\begin{document}
\section{Probability method}

Take a die. If the outcome is $5$ or $6$, roll again.
If the outcome is between $1$ and $4$, the number denotes the student's group.

If one group has reached cardinality $4$, it is excluded just like $5$ and $6$.

\textbf{Rationale:} As the result of $5$ or $6$ does not influence
the probability of outcomes $1$ to $4$, this defines a uniform discrete probability distribution.

\section{Probability space}

\[ \Omega = \operatorname{permutations}(\set{1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4}) \]
where the groups are implicitly defined by indices 1--4, 5--8, 9--12 and 13--16.
\[ \mathcal A = \mathcal P(\Omega) \]
\[ \mathbb P(A) = \frac14^{|A|} \]

\section{In a group of friends}

Let Peter and Alice be Moritz' friends.
Let $\mathbb P[M=1]$ be the probability that Moritz is assigned to group 1.
Let $\mathbb P[P=N]$ and $\mathbb P[A=N]$ be correspondingly.

\[ \mathbb P[M=1] = \frac14 \]
\[ \mathbb P[M=N] = \frac14 \]

\[ \mathbb P[M=1, P=1] = \frac14 \cdot \frac14 \]
\[ \mathbb P[M=N, P=N] = \sum_{N=1}^4 \frac14 \cdot \frac14 = \frac14 \]

\[ \mathbb P[M=1, P=1, A=1] = \frac14 \cdot \frac14 \cdot \frac14 = \frac14^3 \]
\[ \mathbb P[M=N, P=N, A=N] = \sum_{N=1}^4 \frac14^3 = 4 \frac14^3 \]

\section{Blackboard solution}

Approach: All children draw balls from one urn.

\[ \Omega = \set{\text{set partitions of $\{1, \dots, 16\}$ in $4$-tary subsets}} \]
$(k_1, \dots, k_{16})$

We can arrange 16 elements ($16!$).
In each group, you can arrange the students as well ($(4!)^5$)

\[ \frac{16!}{(4!)^5} = 2627625 \]

Moritz' group consists of members:
\[ \set{M, F_1, F_2, R} \]

We can $13$ choices for the remaining child.
\[ \frac{13 {12 \choose 4} {8 \choose 4} {4 \choose 4} \cdot 4}{{16 \choose 4} {12 \choose 4} {8 \choose 4} {4 \choose 4}} = \frac{1}{35} \]

You can also draw probability trees.
You get one branch $\left(\frac14 \cdot \frac3{15} \cdot \frac2{14}\right) \cdot 4 = \frac1{35}$

\end{document}


\newpage
\subsection{Sample generators}
\label{sec:samplers}
When rendering an image, Mitsuba has to solve a high-dimensional integration problem that involves the
geometry, materials, lights, and sensors that make up the scene. Because of the mathematical complexity
of these integrals, it is generally impossible to solve them analytically --- instead, they
are solved \emph{numerically} by evaluating the function to be integrated at a large number of
different positions referred to as \emph{samples}. Sample generators are an essential ingredient to this
process: they produce points in a (hypothetical) infinite dimensional hypercube $[0,1]^{\infty}$ that
constitute the canonical representation of these samples.

To do its work, a rendering algorithm, or \emph{integrator}, will send many queries to the sample generator.
Generally, it will request subsequent 1D or 2D components of this infinite-dimensional ``point'' and map
them into a more convenient space (for instance,  positions on surfaces). This allows it to construct
light paths to eventually evaluate the flow of light through the scene.

Since the whole process starts with a large number of points in the abstract space $[0,1]^{\infty}$,
it is natural to consider different ways of positioning them. Desirable properties of a sampler are
that it ``randomly'' covers the whole space evenly with samples, but without placing samples too close
to each other. This leads to such notions as \emph{stratified sampling} and \emph{low-discrepancy}
number sequences. The samplers in this section make different guarantees on the quality of generated
samples based on these criteria. To obtain intuition about their behavior, the provided point plots
illustrate the resulting sample placement.


% This is part of the TFTB Reference Manual.
% Copyright (C) 1996 CNRS (France) and Rice University (US).
% See the file refguide.tex for copying conditions.

\markright{tfrpwv}
\section*{\hspace*{-1.6cm} tfrpwv}

\vspace*{-.4cm}
\hspace*{-1.6cm}\rule[0in]{16.5cm}{.02cm}
\vspace*{.2cm}

{\bf \large \sf Purpose}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
Pseudo Wigner-Ville time-frequency distribution.
\end{minipage}
\vspace*{.5cm}

{\bf \large \sf Synopsis}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
\begin{verbatim}
[tfr,t,f] = tfrpwv(x)
[tfr,t,f] = tfrpwv(x,t)
[tfr,t,f] = tfrpwv(x,t,N)
[tfr,t,f] = tfrpwv(x,t,N,h)
[tfr,t,f] = tfrpwv(x,t,N,h,trace)
\end{verbatim}
\end{minipage}
\vspace*{.5cm}

{\bf \large \sf Description}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
        {\ty tfrpwv} computes the pseudo Wigner-Ville distribution of a
        discrete-time signal {\ty x}, or the cross pseudo Wigner-Ville
        distribution between two signals. The pseudo Wigner-Ville
        distribution writes
\[PW_x(t,\nu)=\int_{-\infty}^{+\infty} h(\tau)\ x(t+\tau/2)\ x^*(t-\tau/2)\
e^{-j2\pi \nu \tau}\ d\tau.\]

\hspace*{-.5cm}\begin{tabular*}{14cm}{p{1.5cm} p{8cm} c}
Name & Description & Default value\\
\hline
        {\ty x}     & signal if auto-PWV, or {\ty [x1,x2]} if cross-PWV
			({\ty Nx=length(x)}) \\
        {\ty t}     & time instant(s)          & {\ty (1:Nx)}\\
        {\ty N}     & number of frequency bins & {\ty Nx}\\
        {\ty h}     & frequency smoothing window, in the time-domain,
                {\ty h(0)} being forced to {\ty 1}   & {\ty window(odd(N/4))}\\
        {\ty trace}  & if nonzero, the progression of the algorithm is shown
                                         & {\ty 0}\\
     \hline {\ty tfr}   & time-frequency representation \\
        {\ty f}     & vector of normalized frequencies\\

\hline
\end{tabular*}
\vspace*{.2cm}

When called without output arguments, {\ty tfrpwv} runs {\ty tfrqview}.
\end{minipage}
\vspace*{1cm}

{\bf \large \sf Example}
\begin{verbatim}
         sig=fmlin(128,0.1,0.4);
         tfrpwv(sig);
\end{verbatim}

\newpage

{\bf \large \sf See Also}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
all the {\ty tfr*} functions.
\end{minipage}
\vspace*{.5cm}

{\bf \large \sf Reference}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
[1] T. Claasen, W. Mecklenbrauker ``The Wigner Distribution - A Tool for
Time-Frequency Signal Analysis'' {\it 3 parts} Philips
J. Res., Vol. 35, No. 3, 4/5, 6, pp. 217-250, 276-300, 372-389, 1980.
\end{minipage}


% Copyright 2018 Melvin Eloy Irizarry-Gelpí
\chapter{Dual Complex Numbers}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
An exo-1 binion has the form
\begin{equation}
    a_{0} + a_{1} A + a_{2} W + a_{3} AW
\end{equation}
These follow from a parabolic Cayley-Dickson construct on the binions.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Arithmetic Operations}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
...
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Multiplication}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
...
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Conjugate Operations}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
...
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Asterisk Conjugation}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
...
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Quadrance}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
...
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Cloak Conjugation}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
...
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Dagger Conjugation}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
...
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Hodge Star}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
...
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Differential Operators}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
...


\section{Leader Value Calculation}
\label{sec:leader-value-calc}

This section details how we determine whether a node is entitled to lead (under
the Praos protocol) given the output of its verifiable random function
calculation.

\begin{figure}
  \emph{Values associated with the leader value calculations}
  \begin{equation*}
  \begin{array}{r@{~\in~}lr}
    \var{certNat} & \{n | n \in \N, n \in [0,2^{512})\} & \text{Certified natural value from VRF} \\
    \var{f} & [0,1] & \text{Active slot coefficient} \\
    \sigma & [0,1] & \text{Stake proportion}
  \end{array}
  \end{equation*}
\end{figure}

\subsection{Computing the leader value}

The verifiable random function gives us a 64-byte random output. We interpret
this as a natural number $\var{certNat}$ in the range $[0,2^{512})$.

\subsection{Node eligibility}

As per \cite{ouroboros_praos}, a node is eligible to lead when its leader value
$p < 1 - (1 - f)^\sigma$. We have

\begin{align*}
  p & < 1 - (1 -f)^\sigma \\
  \iff \left(\frac{1}{1-p}\right) & < \exp{(-\sigma \cdot \ln{(1-f)})}
\end{align*}

The latter inequality can be efficiently computed through use of its Taylor
expansion and error estimation to stop computing terms once we are certain that
the result will be either above or below the target value.

We carry out all computations using fixed precision arithmetic (specifically, we
use 34 decimal bits of precision, since this is enough to represent the fraction
of a single lovelace.)

As such, we define the following:

\begin{align*}
  p & = \frac{\var{certNat}}{2^{512}} \\
  q & = 1 - p \\
  c & = \ln{(1 - f)}
\end{align*}

and define the function \textit{checkLeaderVal} as follows:

\begin{equation*}
  \fun{checkLeaderVal}~\var{certNat}~\sigma~\var{f} =
    \left\{
      \begin{array}{l@{~}r}
        \mathsf{True}, & f = 1 \\
        \frac{1}{q} < \exp{(-\sigma \cdot c)}, & \text{ otherwise}
      \end{array}
    \right.
\end{equation*}


\subsection{Complex Roots}
Consider the $k^{th}$ power map $p_k:z\mapsto z^k$.
We can ``invert'' it by $\sqrt[k]{z}=\exp(k^{-1}\log z)$.
This multi-valued function can be analysed analogously to how we analyse $\log$.
Take $I(n)=((n-1)\pi/2,(n+1)\pi/2)$ as usual and $U_{I(n)}$ as before equipped with the same $f_{I(n)}:U_{I(n)}\to\mathbb C$ we did for $\log$.
Then consider $g_{I(n)}(z)=\exp(k^{-1}f_{I(n)}(z))$ and the function elements $G_{I(n)}=(g_{I(n)},U_{I(n)})$.
Now $G_{I(n)}$ only depends on $n\bmod k$, so WLOG we can think of $n\in\mathbb Z/k\mathbb Z$.
Everything else is similar to what we did before, and we can get $G_{I(n)}\sim G_{I(m)}$ iff $n-m\equiv 0,\pm 1\pmod{k}$.
Furthermore, a similar gluing constructio defines a path-connected Hausdorff space $R_k$ and maps
\[
    \begin{tikzcd}
        R_k\arrow{r}{g}\arrow[swap]{dr}{\pi}&\mathbb C_\star\arrow{d}{p_k}\\
        &\mathbb C_\star
    \end{tikzcd}
\]
So $g$ gives what we want from the $k^{th}$ root.


\section{Permutations}

\prob{https://artofproblemsolving.com/community/c5h1434000p8108658}{USAMO 2017
    P5}{M}{
    Let $m_1, m_2, \ldots, m_n$ be a collection of $n$ positive integers, not
    necessarily distinct. For any sequence of integers $A = (a_1, \ldots,
    a_n)$ and any permutation $w = w_1, \ldots, w_n$ of $m_1, \ldots, m_n$,
    define an $A$-inversion of $w$ to be a pair of entries $w_i, w_j$ with $i
    < j$ for which one of the following conditions holds:
    \[\begin{aligned}
    a_i \ge w_i > w_j,\quad
    w_j > a_i \ge w_i,\quad
    w_i > w_j > a_i
    \end{aligned}\]
    Show that, for any two sequences of integers $A = (a_1, \ldots, a_n)$ and $B =
    (b_1, \ldots, b_n)$, and for any positive integer $k$, the number of
    permutations of $m_1, \ldots, m_n$ having exactly $k$ $A$-inversions is equal
    to the number of permutations of $m_1, \ldots, m_n$ having exactly $k$ $B$-inversions.

    \index[cat]{Permutations!USAMO 2017 P5}
    \index[strat]{Bijection!USAMO 2017 P5}
}

\begin{solution}
    Notice that if we take $B$ as a sequence with all elements greater than
    all $w_i$, then we have the $B$-inversions to be normal inversions wrt
    $M$. So we need to show that there exists a bijection between $A$-inversion and
    normal inversion. \\

    So we can either show that there for a permutation $w$ with $k$ normal
    inversions, there is a permutation $p$ with $k$ $A$-inversions. But we
    soon figure out it is pretty hard.\\

    If we try the other way, show that for every $w$ with $k$ $A$-inversions,
    there is a $p$ with the same $k$ normal inversions, and if we can show
    injectivity, we will be done. It turns out that this is much easier.
\end{solution}

\prob{https://artofproblemsolving.com/community/c6h271833p1472110}
{ISL 2008 C2}{E}{
    Let $n \in \mathbb N$ and $A_n$ set of all permutations $(a_1, \ldots,
    a_n)$ of the set $\{1, 2, \ldots , n\}$ for which
    \[k\ |\ 2(a_1 + \cdots+ a_k), \text{ for all } 1 \leq k \leq n.\]
    Find the number of elements of the set $A_n$.

    \index[cat]{Permutations!ISL 2008 C2}
    \index[strat]{Induction!ISL 2008 C2}
}

\begin{solution}
    First we try some smaller cases: $ |A_1| = 1$, $ |A_2| = 2$, $ |A_3| = 6$,
    $ |A_4| = 12$, which has a clear pattern. So we proceed with induction.\\

    With induction, we focus on $a_n$ only, it can have values eiher $n, 1$ or
    $\frac{n+1}{2}$. But the later case is impossible, and so we only have two
    options for $a_n$, which gives us our desired inductive relation.
\end{solution}


\documentclass[11pt, oneside]{article}   	% use "amsart" instead of "article" for AMSLaTeX format
\usepackage{fullpage}
\usepackage{graphicx}
\usepackage{amssymb}

\title{Brief Article}
\author{The Author}
%\date{}							% Activate to display a given date or no date

\begin{document}
%\maketitle

\section{Mapping cylinder to 4 sides of cube}

\begin{center}
\includegraphics[width=0.6\textwidth]{cube_cylinder}
\end{center}

Given the cylinder $x^2 + y^2 = 1$ inscribed inside a $2 \times 2 \times 2$ cube
centered at the origin, we can project the cylindrical panorama onto the corresponding
four faces of the cube as follows.
For every pixel in the output image we cast a ray from the cube's center at $(0,0,0)$
through the corresponding point on one of the cube's faces and determine where that
ray intersects the cylinder. We then map this point on the cylinder to the corresponding
point in the input image and (bilinearly) sample the input image at the point.
The resulting pixel color is assigned to the current output pixel.

We parameterize the ray emanating from
the cube's center $(0,0,0)$ through a point $(u,v,w)$ on a face of the cube as
\begin{equation}
  \mathbf{r}(t) = (0,0,0) + t\cdot(u,v,w). \label{eq:r}
\end{equation}
This ray intersects the cylinder where
\begin{equation}
  (ut)^2 + (vt)^2 = 1.
\end{equation}
Solving we get $t = 1/\sqrt{u^2 + v^2},$ which we plug back into Equation~\ref{eq:r} and
get the cylindrical point
\begin{equation}
  \mathbf{p} = (x,y,z) = \frac{(u,v,w)}{\sqrt{u^2 + v^2}}. \label{eq:p-cyl}
\end{equation}
In cylindrical coordinates we have
\begin{eqnarray}
\mathbf{p} = \left(\theta, z\right), \ \ \
  \theta = \mbox{atan2}\left(y,x\right) + \pi/4. \label{eq:theta}
\end{eqnarray}
We added $\pi/4$ to $\theta$ so that the left edge of the image corresponds to
the left edge of the cube's $x = 1$ face (instead of the middle of the face);
Then we adjust for the appropriate image ``wrap-around:''
\begin{equation}
\theta' = \left\{\begin{array}{ll}
  \theta        & \theta \geq 0 \\
  \theta + 2\pi & \theta < 0
  \end{array}\right.
\end{equation}
Now the left edge of the input image corresponds to the left edge of the output image.
The resulting pixel coordinate in the input image is
\begin{equation}
(r,c) = \left(\frac{H}{2}\left(z + 1\right),\ \frac{W}{2\pi}\cdot \theta'\right).
\end{equation}

\begin{verbatim}
for row = 0 .. outImage.H-1 {
    for col = 0 .. outImage.W-1 {
       s = 4.0*col/outImage.W
       face = floor(s)  // face = 0,1,2,3
       f = s - face     // f = frac(s)
       if face == 0 {   // (u,v,w) = point on cube
           u = 1
           v = 2*f - 1
       } else if face == 1 {
           u = 1 - 2*f
           v = 1
       } else if face == 2 {
           u = -1
           v = 1 - 2*f
       } else {
           u = 2*f - 1
           v = -1
       }
       w = 2.0*row/outImage.H - 1
       (x,y,z) = (u,v,w)/sqrt(u*u + v*v) // project onto cylinder
       theta = atan2(y,x) + pi/4;        // cyl. coords, -3pi/4 <= theta <= 5pi/4
       if theta < 0                      // map to [0,2*pi)
           theta += 2*pi
       r = inImage.H*(z + 1)/2.0         // map to input pixel
       c = imImage.W*theta/(2*pi)
       outImage(row,col) = inImage.sample(r,c)
     }
}
\end{verbatim}

\section{Mapping sphere to 6 sides of cube}

\begin{center}
\includegraphics[width=0.6\textwidth]{cube_sphere}
\end{center}

Given the sphere $x^2 + y^2 + z^2= 1$ inscribed inside a $2 \times 2 \times 2$ cube
centered at the origin, we can project the spherical panorama onto the corresponding
four faces of the cube as follows.
For every pixel in the output image we cast a ray from the cube's center at $(0,0,0)$
through the corresponding point on one of the cube's faces and determine where that
ray intersects the sphere. We then map this point on the sphere to the corresponding
point in the input image and (bilinearly) sample the input image at the point.
The resulting pixel color is assigned to the current output pixel.

We parameterize the ray emanating from
the cube's center $(0,0,0)$ through a point $(u,v,w)$ on a face of the cube
and find where it intersects the unit sphere. We use the mapping defined at
\begin{verbatim}
http://mathproofs.blogspot.com/2005/07/mapping-cube-to-sphere.html
\end{verbatim}
yielding
\begin{eqnarray}
x &=& u\sqrt{1 - \frac{v^2}{2} - \frac{w^2}{2} + \frac{v^2 w^2}{3}}, \\
y &=& v\sqrt{1 - \frac{w^2}{2} - \frac{u^2}{2} + \frac{w^2 u^2}{3}}, \\
z &=& w\sqrt{1 - \frac{u^2}{2} - \frac{v^2}{2} + \frac{u^2 v^2}{3}}.
\end{eqnarray}
We convert $(x,y,z)$ to the spherical coordinate $(\theta, \phi)$ where
the azimuthal angle is
\begin{equation}
\theta = \mbox{atan2}\left(y,x\right)
\end{equation}
and the elevation angle is
\begin{equation}
\phi = \mbox{atan2}\left(\sqrt{x^2+y^2},\ z\right).
\end{equation}

\pagebreak

We divvy up the output image into $3 \times 4 = 12$ squares where only 6 of the
squares actually map to the actual faces of the cube:
\begin{center}
\includegraphics[width=0.6\textwidth]{cube-coords}
\end{center}
We pick $(u,v,w)$ coordinates for the unused corners so that the image has a toroidal topology.

\begin{verbatim}
for row = 0 .. outImage.H-1 {
    for col = 0 .. outImage.W-1 {
       a = 4.0*col/outImage.W
       b = 3.0*row/outImage.H
       (i,j) = (floor(a),floor(b))            // map to one of 12 squares
       (s,t) = (a - i, b - j)                 // uvw interpolation values
       (u,v,w) = bilerp(corners[j][i]), s,t)  // get cube coords w/in square
       (x,y,z) = cubeToSphereCoords(u,v,w)
       (theta,phi) = cartesianToSpherical(x,y,z)
       r = theta*inImage.W/(2*pi)
       c = (phi + pi/2)*inImage.H/pi
       outImage(row,col) = inImage.sample(r,c)
     }
}
\end{verbatim}

\end{document}


\documentclass[epsfig,10pt,fullpage]{article}

\newcommand{\LabNum}{2}
\newcommand{\CommonDocsPath}{../../common/docs}
\input{\CommonDocsPath/preamble.tex}

\begin{document}

\centerline{\huge OpenCL}
~\\
\centerline{\huge Laboratory Exercise \LabNum}
~\\
\centerline{\large Accelerating Computer Vision Techniques}
~\\

This exercise introduces you to accelerating computer vision techniques using the Intel FPGA SDK for OpenCL.

\section*{The Canny Edge-detection Technique}

\noindent
In this exercise, we will implement a variation of the \textit{Canny edge detector}, which is
a widely-used edge-detection scheme in computer vision applications. Figures~\ref{fig:tracks} and \ref{fig:tracks_edges}
show a sample image that is provided as the input to a Canny edge detector, as well as the
resulting edge-detected output image.

~\\
\noindent
The Canny edge-detection algorithm involves five stages which are applied to the input image in
succession. The details of these stages are given below. As well, we will see how the sample
input image from Figure~\ref{fig:tracks} is transformed as it passes through
each stage.

~\\
~\\
\begin{figure}[h]
\centering
\begin{minipage}[b]{0.475\textwidth}
	\includegraphics[width=\textwidth]{figures/tracks.png}
	\caption{Original image.}
	\label{fig:tracks}
\end{minipage}
\hfill
\begin{minipage}[b]{0.475\textwidth}
	\includegraphics[width=\textwidth]{figures/tracks_edges.png}
	\caption{Edge-detected image.}
	\label{fig:tracks_edges}
\end{minipage}
\end{figure}

\pagebreak

\noindent
\section*{Stage 1: Grayscale Conversion}

Figure~\ref{fig:sample_stage1} shows the state of our sample image at the end of the grayscale
conversion stage. This stage converts the input 24-bit bitmap color image (8 bits each for red,
green, and blue) into an 8-bit grayscale image. The grayscale value at each pixel is calculated
as the average of the three 8-bit color values of the original image.

\begin{figure}[H]
   \begin{center}
       \includegraphics[scale = 0.85]{figures/fig_stage1_grayscale.png}
   \end{center}
   \caption{The sample input image after the grayscale conversion stage.}
	\label{fig:sample_stage1}
\end{figure}

\noindent
\section*{Stage 2: Gaussian Smoothing}

Figure~\ref{fig:sample_stage2} shows the state of our sample image at the end of the gaussian
smoothing stage. In this stage, a gaussian filter is used to smooth out the image, by
modifying noisy pixels (pixels that are unlike their neighbouring pixels) to be more like
their neighbours. Shown below is the 5 x 5 gaussian filter operation that is applied to the
image. Note that the * denotes convolution, A is the original image, and B is the resulting
filtered image. The effect of this operation is that each pixel gets assigned the weighted
average value of the 5 x 5 grid of pixels surrounding each pixel.

\[
B = 1/159
\begin{bmatrix}
    2 & 4 & 5 & 4 & 2 \\
    4 & 9 & 12 & 9 & 4 \\
    5 & 12 & 15 & 12 & 5 \\
    4 & 9 & 12 & 9 & 4 \\
    2 & 4 & 5 & 4 & 2
\end{bmatrix} * A
\]

\begin{figure}[H]
   \begin{center}
       \includegraphics[scale = 0.85]{figures/fig_stage2_gaussian.png}
   \end{center}
   \caption{The sample input image after the gaussian smoothing stage.}
	\label{fig:sample_stage2}
\end{figure}

\noindent
\section*{Stage 3: Sobel Operator}

Figure~\ref{fig:sample_stage3} shows the state of our sample image at the end of the sobel operator stage. This stage overwrites each pixel with the overall intensity gradient at that pixel. To calculate the overall intensity gradient, the gradient is first calculated in the horizontal ($C_x$) and vertical ($C_y$) directions across the pixel, using the matrices below:

\[
C_x =
\begin{bmatrix}
    -1 & 0 & 1  \\
    -2 & 0 & 2  \\
    -1 & 0 & 1
\end{bmatrix} * B
\ \ \ \ \ \ \  \ \ \ \ \  C_y =
\begin{bmatrix}
    -1 & -2 & -1 \\
    0 & 0 & 0 \\
    1 & 2 & 1
\end{bmatrix} * B
\]

~\\
\noindent
The magnitudes of the two gradients are then added to calculate the overall gradient intensity value for each pixel, resulting in the image C:

\[
C = 0.5|C_x| + 0.5|C_y|
\]

~\\
\noindent
In image C, which is the final output of this stage, the edges of the original image are highlighted as brighter pixels. Non-edges, which are areas with low intensity gradients, appear as darker pixels.

~\\
~\\
\begin{figure}[H]
   \begin{center}
       \includegraphics[scale = 0.85]{figures/fig_stage3_sobel.png}
   \end{center}
   \caption{The sample input image after the sobel operator stage.}
	\label{fig:sample_stage3}
\end{figure}

~\\
\noindent
To illustrate the effect of the sobel operator, let us examine different image boundaries that may exist in an image as shown in Figure~\ref{fig:image_boundaries}. For each 3 x 3 image shown, we can use the sobel operator to calculate the intensity gradient at the center pixel. Let us examine the vertical boundary example, where there is a boundary between darker pixels on the left side of the image, and brighter pixels on the right side. In this example, $|C_x|$ of the center pixel can be calculated as |\{-1*2 + 0*98 + 1*181\} + \{-2*1 + 0*94 + 2*178\} + \{-1*6 + 0*91 + 1*184\}| = 711. In the vertical direction, $|C_y|$ for this pixel is calculated to be 7. The intensity gradient for this pixel is high in the horizontal direction, and low in the vertical direction, which is to be expected along a vertical boundary. The total intensity gradient for this pixel, is $0.5*711 + 0.5*7 = 359$ which saturates the 8-bit grayscale channel to value 255. The sobel operator would therefore detect this boundary as a strong edge, and the center pixel would become bright with a value of 255.

\begin{figure}[H]
   \begin{center}
       \includegraphics[scale = 0.80]{figures/fig_image_boundaries.pdf}
   \end{center}
   \caption{Examples of boundaries in an image.}
	\label{fig:image_boundaries}
\end{figure}

~\\
\noindent
In the horizontal boundary case, we see that the vertical gradient is high ($|C_y|$ = 726) and the horizontal gradient is low ($|C_x|$ =  2). For the diagonal boundary case, the gradient is high in both directions with $|C_x|$ = 516 and $|C_y|$ = 552. Finally, for the non-boundary case, the gradients are low in both directions with $|C_x|$ = 4 and $|C_y|$ = 36. As the gradients are low, the center pixel would become dark with a value of $0.5*4 + 0.5*36 = 20$.

\noindent
\section*{Stage 4: Non-Maximum Suppression}

\noindent
Figure~\ref{fig:sample_stage4} shows the state of our sample image at the end of the non-maximum suppression stage. This stage aims to thin the thick and/or blurry edges that may have resulted from the sobel operator stage. Thick edges are problematic as many applications of edge detection benefit from the edges being as thin as possible. For example, to accurately calculate the surface area of an object, thin edges are desired as to not overlap with the surface. The non-maximum suppression stage thins the edges by removing the weaker (non-maximum) pixels of each edge, and keeping only the maxima. Figure~\ref{fig:nonmaximum_suppression} shows the effect of non-maximum suppression on a sample image containing a blurry vertical line. Notice that the vertical line, which is originally three-pixels wide, becomes one-pixel wide.

~\\
~\\
\begin{figure}[H]
   \begin{center}
       \includegraphics[scale = 0.85]{figures/fig_stage4_nonmax_suppression.png}
   \end{center}
   \caption{The sample input image after the non-maximum suppression stage.}
	\label{fig:sample_stage4}
\end{figure}

\begin{figure}[H]
   \begin{center}
       \includegraphics[scale = 0.8]{figures/fig_nonmaximum_suppression.pdf}
   \end{center}
   \caption{The effect of nonmaximum suppression on a blurred vertical line.}
	\label{fig:nonmaximum_suppression}
\end{figure}

\noindent
\section*{Stage 5: Hysteresis}

Figure~\ref{fig:sample_stage5} shows the end result of the hysteresis stage. The goal of the hysteresis stage is to remove pixels that do not belong to an edge and weak edges altogether. This stage uses two user-defined thresholds: the high threshold and the low threshold. The hysteresis algorithm examines each pixel to determine whether:

\begin{enumerate}
\item the pixel exceeds the high threshold, or
\item the pixel exceeds the low threshold value and there exists at least one adjacent pixel (horizontally, vertically, or diagonally) that exceeds the high threshold.
\end{enumerate}

If at least one of the two criteria are met, the pixel is preserved. Otherwise, the pixel is removed by turning it black.

~\\
~\\
\begin{figure}[H]
   \begin{center}
       \includegraphics[scale = 0.85]{figures/fig_stage5_hysteresis.png}
   \end{center}
   \caption{The sample input image after the hysteresis stage.}
	\label{fig:sample_stage5}
\end{figure}

\newpage
\section*{Part I}

Write a C++-language program that implements the five stages of the canny edge detector as described in the preceding sections. Start with the skeleton code provided in \textit{/design\_files/part1/}, which contains functionality for loading and storing 24-bit color BMP image files. Once a 24-bit color image is loaded into memory, your code should transform the pixels according to the five stages, then store the resulting edge-detected image. The skeleton also contains code that measures the runtime of your program, which we will use to compare with the OpenCL version. Test your program on the sample BMP
images provided in \textit{/design\_files/}

\section*{Part II}

Before we proceed with implementing a Canny edge detector using OpenCL, let us devise an efficient memory architecture for
storing the pixel values as they undergo transformations at each Canny stage. Recall that you have two types of memory at your disposal: global memory outside
the FPGA, and local memories inside the FPGA. The host program provides input data to the accelerator by placing it in global memory.
Since global memory accesses are slow, your accelerator should cache the required pixels into local memory before using them.
The question then, is how to configure the FPGA's local memory resources to best cache the pixels.

To determine the best memory configuration, let us consider the pixel usage pattern for box operations.
Note that the Gaussian smoothing, Sobel operator, non-max suppression, and hysteresis stages of the detector are all
types of box operations, as they work on a box (or frame) of pixels to determine each output pixel.
Figure~\ref{fig:box_operation} depicts a 3x3 box operation (such as the 3x3 sobel operation) on three adjacent pixels of a 10-pixel-wide image.
Notice that there is significant overlap (6 pixels) in the frames of successive operations.

\begin{figure}[H]
   \begin{center}
       \includegraphics[scale = 0.85]{figures/fig_box_operation}
   \end{center}
   \caption{A 3x3 box operation on three adjacent pixels of a 10-pixel-wide image.}
	\label{fig:box_operation}
\end{figure}

To take advantage of the overlap in successive frames of box operations, we will use the FPGA's local memory resources to construct
the shift register design shown in Figure~\ref{fig:shift_register}. The figure shows the shift register design for a 3x3 box operation that
works on 10-pixel wide images, but the idea can be extended for arbitrary box sizes and image widths.
Once a sufficient number of pixels have been loaded, the shift register provides a new 3x3 frame for the box operation at every cycle by simply
shifting in a new pixel and shifting out the oldest pixel which is no longer be used.

\begin{figure}[H]
   \begin{center}
       \includegraphics[scale = 0.85]{figures/fig_shift_register}
   \end{center}
   \caption{The shift register for 3x3 box operations on 10-pixel wide images providing 3 frames over 3 cycles.}
	\label{fig:shift_register}
\end{figure}

How many shift registers will you require for your canny edge detector? How large should each of these shift registers be, given that
your circuit works on images that are 720 pixels wide? How many pixels must be loaded into each shift register before the corresponding box operation
can start? Using such a shift register design requires you to zero pad the input image before
shifting in its pixels, to properly operate on the boundaries of the image.
Why is zero padding necessary, and what is the necessary padding size for a given box size?

\section*{Part III}

Create an OpenCL application that implements the five stages of the canny edge detector.
Use shift registers to hold the pixels as they undergo the Canny stages, and apply the box operations on the pixels
while they are in flight through the shift registers. The shift registers should be the minimum length necessary to hold
required pixels.
When operating in its steady state, your accelerator should have a throughput of one pixel per clock cycle.
The accelerator is considered to be in its steady state when its shift registers are full.
For simplicity, do not zero pad the boundaries of the input image and accept some error along the boundary edge pixels.
Your application must be able to operate on images that are 720 pixels wide with variable height.
Start with the skeleton code provided in \textit{/design\_files/part3/}.

~\\
The shift-register design methodology requires the use of Intel FPGA SDK for OpenCL's ability to infer shift registers from
your OpenCL code. This feature is described in Section \textit{Inferring a Shift Register} of the document \textit{Intel FPGA SDK for OpenCL Programming Guide}.

\input{\CommonDocsPath/copyright.tex}
\end{document}


\section{Exercises in Simulation}\label{S:xsSimulation}
\begin{ExerciseList}
\Exercise Suppose the continuous RV $X$ has PDF:
\[
f_X(x) = \left(\pi(1+x^2) \right)^{-1}
\]
Devise an algorithm to transform samples from $\uniform(0,1)$ RV to those from $X$. Present your answer as pseudo-code.
\Answer
This is nothing but the inversion sampler for the standard Cauchy RV $X$.
\end{ExerciseList}


\documentclass{article}
\usepackage[utf8]{inputenc}

\title{Debugging your Neural Nets and checking your Gradients}
\author{Rishit Dagli}
\date{April 2020}

\begin{document}

Note this only contains major formulas and notes, the complete article could be found here - https://medium.com/@rishit.dagli/debugging-your-neural-nets-and-checking-your-gradients-f4d7f55da167

\maketitle
\section{Introduction}
This document contains a brief and all the major formulas discussed in my blog "Debugging your Neural Nets and checking your Gradients"

\maketitle
\section{Double sided derivatives}

A formula derived to compute double sided derivative
$$\frac{f(\theta + \epsilon)-f(\theta - \epsilon)}{2 \cdot \epsilon} \approx g(\theta)$$

Working thee double sided derivative with example numbers
$$\frac{(1.01)^3  - (0.99)^3}{2 \cdot 0.01} = 3.0001$$

Actual derivative for $f(\theta ^ 3)$
$$g(\theta) = 3 \cdot \theta ^2 = 3$$

One sided derivative
$$g(\theta) = 3.0301$$

Comparing methods
\vspace{10}

1. One sided derivative

Approximation error = $0.0301$
\vspace{5}

2. Double sided derivative

Approximation error = $0.0001$

\maketitle
\section{Going a bit deeper}

$$f^{'}=\lim_{\epsilon \to 0} \frac{f(\theta + \epsilon)-f(\theta - \epsilon)}{2 \cdot \epsilon} \approx g(\theta)$$

iff $\lim_{\epsilon \to 0}$
\vspace{5}

if $\epsilon \neq 0$ then $error = O(\epsilon ^2)$
\vspace{5}

Again $\epsilon$ is a very small number of course less than 1, so $\epsilon >> \epsilon ^2$

The error function in case of double sided approach would be
$$error = O(\epsilon ^2)$$

And for single sided approach it would be
$$error = O(\epsilon)$$

\maketitle
\section{Gradient Checking}

Concatenate all $w^{[i]}$ and $b^{[i]}$ into one single huge vector called $\theta$

So, now our cost function
$$J(w^{[1]}, b^{[1]}, w^{[3]} ... w^{[n]}, b^{[n]}) = J(\theta) \hspace{20} \cdot\cdot\cdot\cdot\cdot(*)$$

Now you can also write
$$J(\theta) = J(\theta_1, \theta_2, \theta_3 ... \theta_n)$$

Example for single iteration of $\theta_5$
$$d \theta^{[5]} _{approx.} = \frac{J(\theta_1, \theta_2 ... \theta_5 + \epsilon ... \theta_n) - J(\theta_1, \theta_2 ... \theta_5 - \epsilon ... \theta_n)}{2 \cdot \epsilon}$$

Considering the double sided discussion earlier
$$d \theta^{[5]} _{approx.} = \frac{J(\theta_1, \theta_2 ... \theta_5 + \epsilon ... \theta_n) - J(\theta_1, \theta_2 ... \theta_5 - \epsilon ... \theta_n)}{2 \cdot \epsilon} \approx d \theta^{[i]} = \frac{\partial J}{\partial \theta_5}$$

Having this clear to us we can now repeat the same process not just for θ₅ but for all $i$ such that $i \in (1, n)$ . So a pseudo Python code for this would look something like this-

\vspace{10}
for i in range(1, n+1):
$$d \theta^{[i]} _{approx.} = \frac{J(\theta_1, \theta_2 ... \theta_i + \epsilon ... \theta_n) - J(\theta_1, \theta_2 ... \theta_i - \epsilon ... \theta_n)}{2 \cdot \epsilon}$$

\maketitle
\section{Approaching an outcome}

Computing similarity
$$\frac{||d\theta_{approx.} - d\theta||_2}{||d\theta_{approx.}||_2 + ||d\theta||_2}$$

\maketitle
\section{Implementing in practice}

\begin{table}[]
\begin{tabular}{ll}
Difference                               & Comments                                             \\
                \\
                \\
\textless{}= 10\textasciicircum{}\{-7\}  & Keep up the good work, you are doing just wonderful! \\
                \\
10\textasciicircum{}\{-5\}               & Okay, Double check components of my vectors          \\
                  \\
                                         & and check that none of the components are too large  \\
                                                        \\
=\textgreater 10\textasciicircum{}\{-3\} & Something to worry about, there might be a bug
\end{tabular}
\end{table}
\end{document}


Essential programming concepts and data structures are described in this chapter. The rest of the thesis relies on these terms.

\section{Related Data Structures}

\subsection{Binary Search Tree}

A binary search tree is a data structure that implements \findop, \insertop, and \removeop operations on a set of keys. Key~$K$ can be of any type, that has a total order. Throughout the thesis, trees with distinct keys are discussed, although of course, it is not the necessary condition.

A binary tree (not to be confused with a binary search tree) consists of nodes. A node~$N$ consists of a key (defined as~\func{key}{N}) and two references to other nodes~-- left child (defined as~\func{left}{N}) and right child (defined as~\func{right}{N}). A reference may contain a link to an existing node or a special value $nil$, that means that the reference is empty. A node that contains a link to a child is called a \emph{parent} of this child node. Nodes may contain other attributes as well, but those are not substantial for this explanation. A node that has no children (\(\func{left}{N} = nil \land \func{right}{N} = nil\)) is called a \emph{leaf}.

From the perspective of the graph theory, the binary tree is a simple oriented acyclic graph, where vertices are represented as nodes and edges are represented as links between a node and its left and right children. Every vertex in such graph has at most one incoming edge, i.e. every node can have at most one parent. Moreover, only one node has no parent~-- this node is called the \emph{root} of a binary tree. The length of the longest path from any leaf to the root is known as the \emph{height} of a binary tree. The \emph{subtree} with a root in $N$ defined as $N$ and a set of nodes that can be reached from $N$ by child links.

A binary \emph{search} tree (\emph{BST}) is a binary tree that satisfies the following condition: for each $N$, subtrees with the root in \func{left}{N} and with the root in \func{right}{N} contain only nodes with keys, that are less or equal than \func{key}{N} and larger or equal than \func{key}{N}, respectively. Using this property, it is possible to implement a fast lookup of a key $K$ in a binary search tree (see \Cref{alg:bst_find}).

%\begin{float}{tbp}
\begin{algorithm}[t]
\caption{Lookup in $BST$}\label{alg:bst_find}
\begin{algorithmic}[1]
  \Procedure{Find}{$root,K$}\Comment{The node with key = $K$ or $nil$}
    \State $node\gets root$
    \While{$node\not= nil$}
    \If{$K = \func{key}{node}$}
      \State \textbf{return} $node$
    \ElsIf{$K < \func{key}{node}$}
      \State $node \gets \func{left}{node}$
    \Else \Comment{$K > \func{key}{node}$}
      \State $node \gets \func{right}{node}$
    \EndIf
    \EndWhile
    \State \textbf{return} $nil$\Comment{The node was not found}
  \EndProcedure
\end{algorithmic}
\end{algorithm}
%\end{float}

The complexity of this search algorithm is~$\mathcal{O}(\func{height}{T})$ (assuming that key comparison takes~$\mathcal{O}(1)$). Furthermore, two remaining operations of a binary search tree, \insertop and \removeop, are implemented in the same fashion, and both of those operations have complexity~$\mathcal{O}(\func{height}{T})$ as well. Their implementation is described in detail in~\cite[p.~327]{sedgewick}.

The tree height can vary between $\mathcal{O}(\func{size}{T})$,
 and $\mathcal{O}(\log(\func{size}{T}))$ in case of a complete binary tree~\cite{complete_bt}, where~$\func{size}{T}$ is the count of nodes in~$T$. BST will maintain optimal operation time only if its structure is close to a complete binary tree and the height is bounded by $c \times \log(\func{size}{T})$, where $c$ is a constant factor greater or equal~$1$.

 To keep the height logarithmic, even in a worst-case scenario, the \emph{tree rebalancing} has been invented. The idea is that a tree keeps track of its structure and if it is not optimal, then the rebalancing is applied to restore optimal structure. The rebalancing can be achieved with the \emph{tree rotation}\cite[p.~435]{sedgewick}~-- the operation, that swaps a node with its parent in a way, that preserves the $BST$ property.

\subsection{AVL Tree}

AVL tree was invented in 1962 by Georgy Adelson-Velsky and Evgenii Landis\cite{avl_tree}. It is a classic example of a self-balancing $BST$. In fact, the height of the AVL tree is never greater than $1.4405\times \log(\func{size}{T}) - 0.3277$ \cite[p.~460]{knuth3}.

Self-balancing is achieved with the following approach: every node holds the difference between the heights of its left and right subtrees; this difference is called \emph{balance factor}.
\emph{AVL property} requires the balance factor of every node to be in range of values $-1$, $0$ and~$1$.

After every operation, that modifies the tree structure~-- \insertop and \removeop, balance factors are updated.
If at any step the balance factor happens to be $-2$ or $2$, a rotation or a double rotation is applied.
The rotation adjusts the heights of the left and right subtrees and, consequently, restores the AVL property. The exact AVL tree implementation is described in \cite[p.~458]{knuth3}.

\subsection{Splay Tree}
\label{sssec:splay}
Another approach on tree balancing is presented in the Sleator and Tarjan work\cite{splay_tree}~-- ``The efficiency of splay trees comes not from an explicit structural constraint, as with balanced trees, but from applying a simple restructuring heuristic, called splaying, whenever the tree is accessed.'' Term \emph{splaying} stands for the process of using rotations (similar to ones in the AVL tree) to bring the last accessed node to the root.

Sleator and Tarjan proved that by using this approach, all three basic operations (\findop, \insertop and \removeop) have a logarithmic time bound. Another benefit of splaying is that the most frequently accessed items tend to gather near the root, therefore improving access speed, especially on skewed input sequences~-- the sequences, in which only a small number of items are accessed often while other items occur rarer. This property is exploited in the Splay eviction policy (\Cref{sssec:spolicy}).

Even though splay trees show several interesting theoretical properties, in practice they are outperformed by more conventional BSTs, like AVL or Red-Black tree\cite{splay_overview} (the performance evaluation of \numdbname (\Cref{sec:secanalysis}) reaffirms this statement). This is due to the fact that in the splay tree the structure of the tree is altered on every operation, including find operation, while AVL, for instance, modifies the tree only during insertions and removals.

The typical use scenario for those data structures is a scenario, where a vast majority of operations is the search operation, while updates are not so often. AVL and Red-Black trees happen to be faster because they execute fewer instructions per find operation. Moreover, they do not make any writes to memory during the lookup, and, as a consequence, there is lower load on the memory bus and the system cache.

Further researches on splay trees were focused in the main on how to reduce the number of rotations during splaying. An extensive overview of those optimizations is provided in \cite{splay_overview}. One of the described techniques, the partial splaying is a modification of a conventional splay tree, where every node contains a counter that denotes a total count of accesses to this node. As usual, splaying is performed on every access, but the node is splayed only until its access count is less than the access count of its parent.

W. Klostermeyer showed that this modification does not gain any noticeable advantage over a standard splay tree \cite{partial_splaying}. However, partial splaying and other derived modifications can have some interesting properties specifically in application to a numerical database. It will be discussed in \Cref{ch:alt}.

\subsection{Hash Table}

A hash table is another popular data structure that implements dictionary abstract data type. It uses the entirely different approach on item storage and lookup. A hash table allocates a contiguous array $A$, which size is bounded by the expected number of items to be stored, often multiplied by the \mbox{\emph{load~factor}~$\alpha$}. The items are stored in $A$.

Firstly, let’s look at the simplified case: the key $K$ that is used in a hash table is of an integer type. Having $A$, $K$ and the value $V$, associated with $K$, it is possible to use a remainder of the division of $K$ by the size of $A$ as an index in $A$. Then, $V$ will be stored in $A$ at this index. This approach would give the best performance possible, as the $V$ can be retrieved immediately and \emph{the~lookup time does not depend on~the~total count of~items} in the hash table. However, since the $modulo$ operation has been used, there can be several keys that point at the same index in $A$. This circumstance is called \emph{collision}.

To deal with a collision, it is necessary to store $K$ itself together with $V$, so that in case of a collision it would be possible to tell if the stored $V$ is actually associated with the $K$ or another $K'$, that collides with $K$. Secondly, one must pick a strategy on how to deal with the case when two different keys, $K_1$ and $K_2$, that point at the same index are inserted. There are two main approaches:
\begin{description}
\item[Separate chaining (open hashing)]-- each element in $A$ is a linked list (or another data structure), that stores all pairs $\langle K, V\rangle$ that collides.
\pagebreak
\item[Linear probing (closed hashing)]-- if during insertion of $K$ in $A$ at the index $i$ a collision occurs ($i$ is already occupied), a special function $F$ is used to determine the second index at which $K$ can be inserted. If it is also occupied, 3rd and all consequent positions, generated by $F$, are used to try to insert the element.
\end{description}

The approach described above can be generalized on keys of any type $T$. It is achieved with the help of a \emph{hash function}. This function takes an argument of type $T$ and maps it to an integer, called \emph{hash}. This function must satisfy two properties:

\begin{block-description}
\blockitem[Determinism]-- it should \emph{always} map the same input to the same hash.
\blockitem[Uniformity]-- if used with a uniformly generated random sequence of objects on input, the hash function should produce a uniformly distributed sequence of hashes.
\end{block-description}

In \cite[p.~464]{sedgewick} Sedgewick and Wayne provide the detailed explanation of collision avoidance strategies as well as general information about hash tables. More information about hash function properties and hash function construction is presented in \cite{knott}.

\section{Thread Synchronization}

\subsection{Coarse-grained Locking}
\label{sec:cgl}
A trivial way to parallelize a sequential data structure is to eliminate a concurrent access at all. It can be achieved with a single mutual exclusion lock~-- \emph{mutex}.
While a thread holds a mutex, no other threads can lock the same mutex.

The sequential data structure is wrapped into the helper type, that locks the mutex in the beginning of every operation and releases it in the end, so that only one thread can access the data structure at a time, no matter how many threads are involved.
This approach is called the \emph{coarse-grained locking}, in contrast with the \emph{fine-grained locking}, where many locks are used and each lock protects only a part of the data structure, so other threads can freely access other parts.

Pros of this approach is a very trivial implementation and the absence of any special requirements on the underlying data structure. However, coarse-grained locking is only suitable when a data structure has a support role in the program and is used occasionally. If the data structure is the key element of the application, then a single lock becomes the bottleneck in the software, drastically decreasing program scalability. In this case one should use more sophisticated parallelization approaches.

\subsection{Binning}
\label{sec:pre_bin}
The evolution of coarse-grained locking is the \emph{binning}. The main drawback of the previous approach is that a single lock becomes the main point of contention between threads. One way to cope with this is to increase the number of locks. In contrast with the fine-grained locking, the binning does not involve any modifications of the underlying container.

Firstly, the numbers of bins~-- independent data structure instances~-- is chosen. Then a mapping between the item domain and a bin number is introduced. The mapping should yield a uniform distribution of mapped values. Every item is stored only in its assigned bin. Every bin has its own mutex, therefore the access to every bin is serialized. But since items are mapped uniformly, it is expected to produce much less contention than in case of a single lock.

\subsection{Fine-grained Locking}
\label{sec:fgl}
The fine-grained locking usually offers better scalability, than the previously discussed approaches. Instead of a single lock, many mutexes are used simultaneously. Every mutex protects its part of data. The contention between threads is lower as it is unlikely that several threads will access the same portion of data at the same time.

However, this is true only if every portion has the same probability of being accessed (like in a concurrent hash table). In some data structures, typically binary trees, there are some nodes that are accessed (and are locked before access) oftener than others, e.g. the root in a binary tree.

\emph{Substantial modifications} got to be made to the data structure to integrate the fine-grained locking. Sometimes the overhead added by this approach is so big, that it brings to naught any potential speed-up. Fine-grained locking is not a silver bullet, but usually it offers a reasonable trade-off between implementation complexity and application scalability.


\section{Classifying spaces of groups}\label{classifying-g-bundles}
%I learned this attitude towards classifying spaces from Segal.
%An article is on the website.
%
The constructions of the previous sections can be summarized in a single
diagram:
\begin{equation*}
    \xymatrix{
	\Cat\ar[r]^{\text{nerve}} & s\Set\ar[d]^{|-|}\\
	\mathbf{Gp}\ar@{^(->}[u]\ar[r]_{B} & \Top
    }
\end{equation*}
The bottom functor is defined as the composite along the outer edge of the
diagram. The space $BG$ for a group $G$ is called the \emph{classifying space
of $G$}. At this point, it is far from clear what $BG$ is classifying. The goal
of the next few sections is to demystify this definition.

\begin{lemma}\label{conjugation-homotopic}
    Let $G$ be a group, and $g\in G$.  Let $c_g:G\to G$ via $x\mapsto
    gxg^{-1}$.  Then the map $Bc_g:BG\to BG$ is homotopic to the identity.
\end{lemma}
\begin{proof}
    The homomorphism $c_g$ is a functor from $G$ to itself. It suffices to
    prove that there is a natural transformation $\theta$ from the identity to
    $c_g$. This is rather easy to define: it sends the only object to the only
    object: we define $\theta_\ast:\ast\to \ast$ to be the map given by
    $\ast\xrightarrow{g}\ast$ specified by $g\in \Hom_G(\ast,\ast) = G$. In
    order for $\theta$ to be a natural transformation, we need the following
    diagram to commute, which it obviously does:
    \begin{equation*}
	\xymatrix{
	    \ast\ar[d]_1\ar[r]^g & \ast\ar[d]^{gxg^{-1}}\\
	    \ast\ar[r]_{g} & \ast.
	    }
    \end{equation*}
\end{proof}
Groups are famous for acting on objects. Viewing groups as categories allows
for an abstract definition a group action on a set: it is a functor $G\to
\Set$. More generally, if $\cc$ is a category, an action of $\cc$ is a functor
$\cc\xrightarrow{X}\Set$. We write $X_c = X(c)$ for an object $c$ of $\cc$.
\begin{definition}
    The ``translation'' category $X\cc$ has objects given by
    $$\mathrm{ob}(X\cc) = \coprod_{c\in \cc}X_c,$$
    and morphisms defined via $\Hom_{X\cc}(x\in X_c, y\in X_d) = \{f:c\to
    d:f_\ast(x) = y\}$.
\end{definition}
There is a projection $X\cc\to \cc$. (For those in the know: this is a special
case of the Grothendieck construction.)
\begin{example}
    The group $G$ acts on itself by left translation. We will write
    $\widetilde{G}$ for this $G$-set. The translation category $\widetilde{G}G$
    has objects as $G$, and maps $x\to y$ are elements $yx^{-1}$. This category
    is ``unicursal'', in the sense that there is exactly one map from one
    object to another object. Every object is therefore initial and terminal,
    so the classifying space of this category is trivial by the discussion at
    the end of \S \ref{classifying-space-properties}. We will denote by $EG$
    the classifying space $B(\widetilde{G}G)$. The map $\widetilde{G}G\to G$
    begets a canonical map $EG\to BG$.
\end{example}
The $G$ also acts on itself by right translation. Because of associativity, the
right and left actions commute with each other. It follows that the right
action is equivariant with respect to the left action, so we get a right action
of $G$ on $EG$.
\begin{claim}
    This action of $G$ on $EG$ is a principal action, and the orbit projection
    is $EG\to BG$.
\end{claim}
To prove this, let us contemplate the set $N(\widetilde{G}G)_n$. An element is
a chain of composable morphisms. In this case, it is actually just a sequence
of $n+1$ elements in $G$, i.e., $N(\widetilde{G}G)_n = G^{n+1}$. The right
action of $G$ is simply the diagonal action. We claim that this is a free
action. More precisely:
\begin{lemma}[Shearing]
    If $G$ is a group and $X$ is a $G$-set, and if $X\times^\Delta G$ has the
    diagonal $G$-action and $X\times G$ has $G$ acting on the second factor by
    right translation, then $X\times^\Delta G\simeq X\times G$ as $G$-sets.
\end{lemma}
\begin{proof}
    Define a bijection $X\times^\Delta G\mapsto X\times G$ via $(x,g)\mapsto
    (xg^{-1},g)$. This map is equivariant since $(x,g)\cdot h = (xh, gh)$,
    while $(xg^{-1}, g) \cdot h = (xg^{-1}, gh)$. The element $(xh, gh)$ is
    sent to $(xh(gh)^{-1}, gh)$, as desired.
    The inverse map $X\times G\to X\times^\Delta G$ is given by $(x,g)\mapsto
    (xg, g)$.
\end{proof}
We know that $G$ acts freely on $N(\widetilde{G}G)_n$, soo a nonidentity group
element is always going to send a simplex to another simplex. It follows that
$G$ acts freely on $EG$.

To prove the claim, we need to understand the orbit space. The shearing lemma
shows that quotienting out by the action of $G$ simply cancels out one copy of
$G$ from the product $N(\widetilde{G}G) = G^n$. In symbols:
$$N(\widetilde{G} G)/G\simeq G^n\simeq (NG)_n.$$
Of course, it remains to check the compatibility with the face and degeneracy
maps. We will not do this here; but one can verify that everything works out:
the realization is just $BG$!

We need to be careful: the arguments above establish that $EG/G \simeq BG$ when
$G$ is a finite group. The case when $G$ is a topological group is more
complicated. To describe this generalization, we need a preliminary categorical
definition.

Let $\cc$ be a category, with objects $\cc_0$ and morphisms $\cc_1$. Then we
have maps $\cc_1\times_{\cc_0}\cc_1\xrightarrow{\text{compose}}\cc_1$ and two
maps (source and target) $\cc_1\to \cc_0$, and the identity $\cc_0\to\cc_1$.
One can specify the same data in any category $\cd$ with pullbacks. Our
interest will be in the case $\cd = \Top$; in this case, we call $\cc$ a
``category in $\Top$''.

%The story described above works just as well if we were to consider functors
%$X:\cc\to \Top$ instead of $X:\cc\to \Set$. Let $G$ be a topological group. Define $\widetilde{G}$ to be a category in $\Top$.
%
%An example is a topological group -- that's a category in $\Top$.
Let $G$ be a topological group acting on a space $X$. We can again define $XG$,
although it is now a category in $\Top$. Explicitly, $(XG)_0 = X$ and $(XG)_1 =
G\times X$ as spaces. The nerve of a topological category begets a simplicial
space. In general, we will have
$$(N\cc)_n = \cc_1\times_{\cc_0}\cc_1\times\cdots\times_{\cc_0}\cc_1.$$
The geometric realization functor works in exactly the same way, so the
realization of a simplicial space gets a topological space.
%
%You need to put some mild topological conditions on the group for the theorem we proved above to be true.
%Again, we have $\widetilde{G}$ with $G$ acting on it.
%Again, we let $EG = B\widetilde{G}G$.
The above discussion passes through with some mild topological conditions on
$G$ (namely, if $G$ is an absolute neighborhood retract of a Lie group); we
conclude:
\begin{theorem}
    Let $G$ be an absolute neighborhood retract of a Lie group. Then $EG$ is
    contractible, and $G$ acts from the right principally. Moreover, the map
    $EG\to BG$ is the orbit projection.
\end{theorem}
A generalization of this result is:
\begin{exercise}
    Let $X$ be a $G$-set. Show that
    $$EG\times_G X \simeq B(XG).$$
\end{exercise}