\documentclass[11pt, letter]{article}

\usepackage[margin=1in]{geometry}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amssymb}
\usepackage{enumitem}

\newcommand{\piece}[2]{\mathbf{#1}\text{#2}}

\theoremstyle{definition}
\newtheorem{alg}{Algorithm}

\title{Training a Linear Heuristic on Chess Positions for the Minimax Algorithm}
\author{Tae Hyung Kim}

\begin{document}

\maketitle

\section{Introduction}
First, we make clear the notation that we shall use. We shall represent the type
of chess pieces by the following abbreviations:
\[\{ \mathbf{K} \leftrightarrow \text{King},
\mathbf{Q} \leftrightarrow \text{Queen},
\mathbf{R} \leftrightarrow \text{Rook},
\mathbf{B} \leftrightarrow \text{Bishop},
\mathbf{N} \leftrightarrow \text{Knight},
\mathbf{P} \leftrightarrow \text{Pawn}
\}. \]
In addition, we shall use the standard method of identifying location of a piece.
That is, each column shall be lettered $\text{a}, \dots, \text{f}$ while each row
will be numbered $1, \dots, 8$. Each position will be written with the column
letter and the row number consecutively; for example, we would say that the
white king initially occupies the position $\text{a}4$. Finally, we represent a
piece, which includes its position, by the piece type followed by its position.
For example, the king is denoted by $\piece{K}{a4}$.

Recall the algorithm of minimax with alpha-beta pruning. Suppose that we
represent a \texttt{Board} as an object (assume Python for simplicity) with the
method \texttt{get\_moves}. In addition, suppose that we have a heuristic
function $h : \mathtt{Board} \to \mathbb R$ which evaluates how good a board is.
\renewcommand*{\thealg}{M}
\begin{alg}
    Suppose we have a board \texttt{board}.
    \begin{enumerate}[label=\textbf{\thealg\arabic*}.]
        \item If
    \end{enumerate}
\end{alg}

Define variables $\theta_{P_1, P_2}$ where $P_1, P_2$ are any two pieces.
Now, the heuristic of interest in this paper is $h_\theta : \mathtt{Board} \to
\mathbb{R}$ defined by
\[ h_\theta(\texttt{board}) = \sum_{P_1, P_2 \in \texttt{board}} \theta_{P_1, P_2} \cdot \chi_\texttt{board}(P_1) \chi_\texttt{board}(P_2) \]
where $\chi_\texttt{board} : \texttt{Piece} \to \{0, 1\}$ is the indicator of
whether a certain piece is in the board or not. Note that the product
$\chi_\texttt{board}(P_1) \chi_\texttt{board}(P_2)$ simply returns $1$ if the
pair of pieces $P_1, P_2$ is in the board, and $0$ otherwise.

The ultimate goal is to find the $\theta$ which will provide the best heuristic
for the minimax algorithm. Given that the data we will be given is whether a
certain board is ``good'' or ``bad'', the problem of computing $\theta$ is a
logistic regression problem, as we wish to minimize the cost $\log(g(h_\theta(\texttt{board})$ where $g$ is the sigmoid function.

\end{document}


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[margin=1in]{geometry}
\usepackage{graphicx}
\title{\fontfamily{cmss}\selectfont Plagiarism Checker}
\author{Navneel Singhal}
\date{August 2020}

\newcommand{\ttt}{\texttt}

\begin{document}

\maketitle

\section*{Description}

The assignment was to implement a plagiarism checker in C.

\section*{Usage}

To compile the code, run the command \texttt{make}. \\
To run the code, run \texttt{./plagChecker <LOCATION\_OF\_TEST\_FILE> <LOCATION\_OF\_CORPUS\_FOLDER>}.
\\
\textbf{Important Note:} When passing the location of the corpus folder, ensure that it ends with a /
\\
This code has been tested to run on Linux (Ubuntu 20.04), and couldn't be tested on Windows due to the lack of a Windows machine. However this will work on POSIX-compliant machines.

\section*{The Model}

\subsection*{Introduction to $N$-grams}
A concept widely used in natural language processing, word $N$-grams are essentially ``phrases'' of length $N$ in a given text, for our purposes.
Their main importance is in the fact that they can represent Markov chains of length $N$, which allows for a simple representation of phrases which is also natural fit for languages like English, because the main structures that stand out in a text are linear owing to the low nesting-levels in most languages.
Their relevance to our use-case is that in a plagiarized document, the frequency of any particular phrase should be heuristically similar to that in the document where it was plagiarized from.

\subsection*{High-level Description of the Algorithm}

From any given text, we find the sorted array containing all possible $3$-gram hashes in the text. To find the similarity between the texts, we extract frequency vectors of $3$-grams corresponding to both texts, sorted according to $3$-gram hashes in both the texts (and if a hash is not found in one file, we simply assign the value 0 to the frequency of that hash), and find the cosine similarity between any two such vectors. This is going to be the measure of similarity we will use in this implementation.\\
Cosine similarity for two vectors $(a_1, \dots, a_n)$ and $(b_1, \dots, b_n)$ is defined as $$\mathcal{S}((a_1, \dots, a_n), (b_1, \dots, b_n)) = \frac{a_1b_1 + \cdots + a_nb_n}{\sqrt{(a_1^2 + \cdots + a_n^2)(b_1^2 + \cdots + b_n^2)}}$$
The reasoning behind this is as follows.
\begin{enumerate}
        \item For relatively more frequent $3$-grams, the weight of that feature should be intuitively high, and sparse features are quite unimportant.
        \item This measure is symmetric between the corpus and the queried file, so it doesn't introduce a bias between who plagiarised whom.
        \item It is resistant to scaling; i.e., if there is a small document which has been plagiarised from a larger document, the results are similar to those if the smaller document was slightly larger.
\end{enumerate}
To make feature extraction robust and resistant to the usage of different cases, we tokenize the text with respect to whitespace, commas, full-stops and newline characters, and then for each of the extracted strings, we convert them to lower case.
To make feature extraction simpler, we use a polynomial hash (with base $127$) for each string, and for each $3$-gram (which is now a triple of integers modulo $10^9 + 7$), we use a different polynomial hash (with base $997$ so as to not bring in any unwanted correlation between concatenated words or something else of this sort).\\
We also remove all words whose length is less than 3, so as to remove commonly used words from consideration.

\section*{Working of Code}

\begin{enumerate}
    \item \texttt{long long int stringHash(char* s)}: This function computes the hash of a string as mentioned above. Time complexity is $\mathcal{O}$(length of string), and space complexity is $\mathcal{O}(1)$.
    \item \texttt{long long int ngramHash(long long int a, long long int b, long long int c)}: This function computes the hash of a $3$-gram, as mentioned above. Time complexity is $\mathcal{O}(1)$ and space complexity is $\mathcal{O}(1)$.
    \item \texttt{void sort(long long int* a, int l, int r)}: This function sorts the subarray \texttt{a[l..r]} using merge-sort. Time complexity is $\mathcal{O}(r - l + 1)$ and space complexity is $\mathcal{O}(1)$ (since we use a global buffer).
    \item \texttt{long long int* workFile(char* fileName)}: This function returns a pointer to the sorted array containing the hashes of all the $3$-grams in the file whose name is \texttt{fileName}. Time complexity is $\mathcal{O}$(total length + $N \log N$) and space complexity is $\mathcal{O}(N)$ where $N$ is the number of words in the file (we reuse the buffer used in the previous function).
    \item \texttt{float findSimilarity(long long int* a, int n, long long int* b, int m)}: This function computes the frequency arrays for the arrays pointed to by \texttt{a} and \texttt{b}, and then finds the cosine similarity between them and returns the similarity as a ratio between $0$ and $1$. Time complexity is $\mathcal{O}(n + m)$ and space complexity is also $\mathcal{O}(n + m)$.
    \item \texttt{int main(int argc, char* argv[])}: This is the main function, and here we traverse over all files in the given corpus directory, and find the similarity between the queried file and the corpus files, one corpus file at a time.
        For the time complexity, suppose the number of corpus files is $t$, the total number of characters in all corpus text files and the queried test file is $s$, the total number of words in file $i$ is $n_i$, with the queried file being file $0$, and the rest being corpus files. Then the time complexity is $\mathcal{O}(s + \sum_{i = 0}^t n_i \log n_i + tn_0)$. The space complexity is $\mathcal{O}(n_0 + \max_{i = 1}^t n_i)$, apart from a global buffer.
\end{enumerate}

\end{document}


\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{bm}
\usepackage{hyperref}
\usepackage{enumerate}
\usepackage[margin=2cm]{geometry}

\title{Metropolis--Hastings proposal distribution\\for $\theta$}
\author{Darjus Hosszejni}
\date{\today}

\begin{document}

\maketitle

\section*{Proposal}

We want a nice proposal for $\pi(\theta\vert y^\ast,d,s)$.
We'll use the second order Taylor expansion of $g(\theta)=\log(\pi(\theta\vert y^\ast,d,s))$ as the base for the parameters of a normal approximation.
The approx. should be somehow around the mode with the correct second order structure.

We're only interested in approximations $f$ for which $f(\theta^\text{new})-f(\theta^\text{old})\sim g(\theta^\text{new})-g(\theta^\text{old})$,
so constants that cancel out don't matter.
It turns out that a good candidate is
\begin{equation*}
f(\theta)=-\frac12(\theta-\theta_\ast)^\prime\Sigma_\ast^{-1}(\theta-\theta_\ast),
\end{equation*}
where
\begin{align*}
\theta_\ast &= \hat\theta+\Sigma_\ast\gamma_\ast, \\
\hat\theta &= \text{arg}\max_\theta g(\theta), \qquad \text{the mode of the posterior}, \\
\gamma_\ast &= \left.\frac{\partial g(\theta)}{\partial\theta}\right\rvert_{\theta=\hat\theta}, \qquad \text{the gradient},\\
-\Sigma_\ast^{-1} &= \left.\frac{\partial^2 g(\theta)}{\partial\theta^\prime\partial\theta}\right\rvert_{\theta=\hat\theta}, \qquad \text{the Hessian},
\end{align*}
i.e. $\mathcal{N}(\theta_\ast,\Sigma_\ast)$ fits, its density is a constant times the second order Taylor expansion of the posterior density.

When the Hessian is not negative definite we use $\Sigma_\ast=cI$ with a large $c$.

\subsection*{Taylor expansion}

This is the explanation and proof for the above.
\begin{align*}
g(\theta) &\sim g\left(\hat\theta\right)+g^\prime\left(\hat\theta\right)^\text{T}\left(\theta-\hat\theta\right)+\frac12\left(\theta-\hat\theta\right)^\text{T}g^{\prime\prime}\left(\hat\theta\right)\left(\theta-\hat\theta\right), \\
&= -\frac12\left[-\gamma_\ast^\text{T}\left(\theta-\hat\theta\right)+\left(\theta-\hat\theta\right)^\text{T}\Sigma_\ast^{-1}\left(\theta-\hat\theta\right)\right]+g\left(\hat\theta\right), \\
&= -\frac12(\theta-\theta_\ast)^\text{T}\Sigma_\ast^{-1}(\theta-\theta_\ast)+\frac12\gamma_\ast^\text{T}\Sigma_\ast\gamma_\ast+g\left(\hat\theta\right), \\
&= f(\theta)+\text{constant}(\hat\theta).
\end{align*}

\end{document}


In this introductory section we review theoretical concepts about the
\textit{Riordan group} that will be useful in subsequent chapters;
additionally, we provide a very short introduction to symbolic computation
using the \verb|sympy| module implemented using the Python programming
language, giving a taste of our programming style.

\section{Riordan Arrays, formally}

A \textit{Riordan array} is an infinite lower triangular array $(d_{n,k} )_{n,k
\in \mathbb{N}},$ defined by a pair of formal power series $(d(t),h(t))$ such
that\newline\noindent$d(0)\neq 0, h(0)=0$ and $h^\prime(0)\neq 0$; furthermore,
each element \begin{displaymath}
    d_{n,k}=[t^n]d(t)h(t)^k, \qquad n,k \geq 0,
\end{displaymath}
is the coefficient of monomial $t^{n}$ in the series
expansion of $d(t)h(t)^{k}$ %where $d_{n,k}=0$ for $k>n$,
and the bivariate generating function
\begin{equation}
    \label{bgf}
    R(t,w) = \sum_{n,k \in\mathbb{N}}{d_{n,k} t^n w^k} = {d(t) \over 1-wh(t)}
\end{equation}
enumerates the sequence $(d_{n,k})_{n,k \in\mathbb{N}}$.

These arrays were introduced in \citep{SHAPIRO1991229}, with the aim of
defining a class of infinite, lower triangular arrays and since then they have
attracted, and continue to attract, a lot of attention in the literature and
recent applications can be found in \citep{LUZON201475}.

\begin{example}
The most simple Riordan matrix could be the \textit{Pascal triangle}
$$\mathcal{R}\left(\frac{1}{1-t}, \frac{t}{1-t}\right)\quad\text{where}\quad
d_{n,k}={n\choose k};$$ moreover, another remarkable matrix is the
\textit{Catalan triangle}
$$\mathcal{R}\left(\frac{1-\sqrt{1-4\,t}}{2\,t}, \frac{1-\sqrt{1-4\,t}}{2\,t}\right)$$
where $\displaystyle d_{n,k}={{2n-k}\choose{n-k}} - {{2n-k}\choose{n-k-1}}$.
\end{example}

An important property of Riordan arrays concerns the computation of
combinatorial sums; precisely, it is encoded by the identity
\begin{equation}
    \label{somme}
    \sum_{k=0}^n d_{n,k}f_k=[t^n]d(t)f(h(t))
\end{equation}
and it is extensively commented in
\citep{LUZON2012631,Merlini:2009:CSI:2653507.2654195,SPRUGNOLI1994267}.
It states that every combinatorial sum involving a Riordan array can be computed by
extracting the coefficient of $t^n$ from the series expansion of $d(t)f(h(t))$,
where $f(t)=\mathcal{G}(f_k)=\sum_{k\geq 0}f_kt^k$ denotes the generating function
of the sequence $(f_k)_{k \in\mathbb{N}}$ and the symbol $\mathcal{G}$
denotes the generating function operator. Due to its importance, relation
(\ref{somme}) is commonly known as the \textit{fundamental rule} of Riordan arrays.
For short and when no confusion arises, the notation $(f_k)_{k}$ will be used
as an abbreviation of $(f_k)_{k\in\mathbb{N}}$.

As it is well-known (see, e.g., \citep{LUZON201475,MRSV97,SHAPIRO1991229}),
Riordan arrays constitute an \textit{algebraic group} with respect to the usual
row-by-column product between matrices; formally, the group operation $\cdot$ applied
to two Riordan arrays $D_1(d_1(t),\ h_1(t))$ and $D_2(d_2(t),\ h_2(t))$ is
carried out as
\begin{equation}
  D_1 \cdot D_2 =(d_1(t)d_2(h_1(t)),\ h_2(h_1(t))).
  \label{eq:riordan:group:op}
\end{equation}
Moreover, the Riordan array $I = (1,\ t)$ acts as the identity element and the
inverse of $D =(d(t), h(t))$ is the Riordan array
\begin{equation}
D^{-1} = \left( \frac{1}{d(\overline{h}(t))}, \overline{h}(t) \right)
  \label{eq:riordan:group:inverse}
\end{equation}
where $\overline{h}(t)$ denotes the compositional inverse of $h(t)$.

An equivalent characterization of a each matrix $\mathcal{R}(d(t), h(t))$ in
the Riordan group is given by two sequences of coefficients
$\left(a_{n}\right)_{n\in\mathbb{N}}$  and
$\left(z_{n}\right)_{n\in\mathbb{N}}$ called $A$-sequence and $Z$-sequence,
respectively. The former one can be used to define every
coefficient $d_{n,k}$ with $k>0$,
\begin{displaymath}
    d_{n+1, k+1} = a_{0}d_{n,k} + a_{1}d_{n,k+1} + a_{2}d_{n,k+2} + \ldots + a_{j}d_{n,k+j} + \ldots %+ a_{j+1}d_{n,k+j+1} + \cdots
\end{displaymath}
where the sum is finite because exists $j\in\mathbb{N}$ such that $n=k+j$; on
the other hand, the latter one can be used to define every coefficient
$d_{n,0}$ lying on the first column,
\begin{displaymath}
    d_{n+1, 0} = z_{0}d_{n,0} + z_{1}d_{n,1} + z_{2}d_{n,2} + \ldots + z_{n}d_{n,n}
\end{displaymath}
where the sum is finite because $d_{n,k+j}=0$ for $j>n-k$.

Moreover, let $A(t)$ and $Z(t)$ be the generating functions of the $A$-sequence
and $Z$-sequence respectively, then relations
\begin{displaymath}
    h(t) = tA(h(t)) \quad\text{and}\quad d(t)=\frac{d_{0,0}}{1-tZ(h(t))}
\end{displaymath}
connect them with functions $d(t)$ and $h(t)$, where $d_{0,0}$ is the very
first element of $\mathcal{R}$; for the sake of completeness,
\citep{MRSV97} collects more alternative characterizations.

\section{Symbolic computation}

The main part of the symbolic computations supporting the topics discussed in
this dissertation has been coded using the Python language, relying on the
module \verb|sympy| for what concerns mathematical stuff. Quoting from
\url{http://www.sympy.org/}:
\begin{center}
\textit{ ``SymPy is a Python library for symbolic mathematics. It aims to
become a full-featured computer algebra system (CAS) while keeping the code as
simple as possible in order to be comprehensible and easily extensible.''}
\end{center}
The paper \citep{10.7717/peerj-cs.103} explains it and many other resources can
be found online; for example, a comprehensive documentation is
\citep{sympy:doc} and a well written, understandable tutorial
\citep{sympy:tutorial} is provided by the original development team.

Here we avoid to duplicate the tutorial with similar examples, instead we state
the methodology used while coding our definitions. Python is a very expressive
language allowing programmers to use both the object-oriented and functional
paradigms. It can tackle different application domains by means of
\textit{modules} and \verb|sympy| is an example that targets the manipulation
of symbolic terms.  Contrary to other proprietary software like Maple and
Mathematica which ship their own languages, \verb|sympy| is implemented
entirely in Python, allowing a transparent and easy integration in other Python
programs, as we will see in later chapters.

The main point to grasp in our opinion is the difference between the
\textit{meta language}, which is Python, and the \textit{object language},
which is the mathematical expressions denoted by \verb|sympy| objects.

\begin{example}
\verb|Symbol| is a fundamental class of objects that introduces arbitrary
mathematical symbols.
\begin{minted}[baselinestretch=0.8]{python}
>>> from sympy import Symbol
>>> a_sym = Symbol('a')
>>> a_sym
a
\end{minted}
The previous snippet allows us to clarify the duality among meta and object
languages; precisely, the mathematical expression $a$ is denoted by the Python
object \verb|a_sym|.
\end{example}

The above example is the first one found by the reader and it shows a common
pattern used through this document to illustrate \textit{computations}; in
particular, when a line starts with (i)~\verb|>>>| then it is an \textit{input}
line holding code to be executed, (ii)~\verb|...| then it is a
\textit{continuation} line holding an unfinished code expression, otherwise
(iii) it is an \textit{output} line reporting the result of the evaluation.

A second fundamental methodology that we embrace in our symbolic manipulations is
\textit{equational reasoning}, namely we use equations denoted by \verb|Eq| objects
to express identities to reason about, used both to define things and
to be solved with respect to a desired symbol.
\begin{example}
Introduction of \verb|Eq|, \verb|solve| and \verb|symbols| functions:
\begin{minted}[baselinestretch=0.8]{python}
>>> from sympy import Eq, solve, symbols
>>> a, t = symbols('a t')
>>> a_def = Eq(a, 3)
>>> at_eq = Eq(a+5*t, 1/(1-t))
>>> a_def, at_eq
\end{minted}
\begin{displaymath}
\left(
a=3,\quad a + 5 t = \frac{1}{- t + 1}
\right)
\end{displaymath}
\begin{minted}[baselinestretch=0.8]{python}
>>> sols = [Eq(t, s) for s in solve(at_eq, t)]
>>> sols
\end{minted}
\begin{displaymath}
\begin{array}{c}
\left [ t = - \frac{a}{10} - \frac{1}{10} \sqrt{a^{2} + 10 a + 5} + \frac{1}{2}, \right . \\
\left. t = - \frac{a}{10} + \frac{1}{10} \sqrt{a^{2} + 10 a + 5} + \frac{1}{2}\right ]
\end{array}
\end{displaymath}
\end{example}

Due to the importance of equations in our code, we introduce two
helper functions. First, \verb|define| builds a definition:
\inputminted[baselinestretch=0.8,stripnl=false,firstline=5,lastline=10]
    {python}{deps/simulation-methods/src/commons.py}

\begin{example}
Introduction of \verb|Function| objects:
\begin{minted}[baselinestretch=0.8]{python}
>>> from sympy import Function, sqrt
>>> f = Function('f')
>>> f(3)
f(3)
>>> t = symbols('t')
>>> define(let=f(t), be=(1-sqrt(1-4*t))/(2*t), ctor=FEq)
\end{minted}
\begin{displaymath}
f{\left (t \right )} = \frac{1}{2 t} \left(- \sqrt{- 4 t + 1} + 1\right)
\end{displaymath}
\end{example}

The keyword argument \verb|ctor=FEq| asks \verb|define| to promote
the equation we are defining as a \verb|callable| object by means of
the \verb|FEq| class,
\inputminted[baselinestretch=0.8,stripnl=false,firstline=46, lastline=78]
    {python}{deps/simulation-methods/src/commons.py}
\noindent which provides methods that allows it to be used as a substitution
too; in turn, it depends on
\inputminted[baselinestretch=0.8,stripnl=false,firstline=12, lastline=19]
    {python}{deps/simulation-methods/src/commons.py}

\begin{example}
Introduction of \verb|IndexedBase| objects:
\begin{minted}[baselinestretch=0.8]{python}
>>> from commons import lift_to_Lambda
>>> from sympy import IndexedBase
>>> a = IndexedBase('a')
>>> aeq = Eq(a[n], n+a[n-1])
>>> aeq(n+1)
\end{minted}
\begin{displaymath}
    a_{n + 1} = n + a_{n} + 1
\end{displaymath}
\begin{minted}[baselinestretch=0.8]{python}
>>> b = Function('b')
>>> beq = Eq(b(n), n+b(n-1))
>>> beq(n+1)
\end{minted}
\begin{displaymath}
    b{\left (n + 1 \right )} = n + b{\left (n \right )} + 1
\end{displaymath}
\end{example}

\section{Riordan Arrays, computationally}

In this section we describe a little framework that implements parts
of the concepts seen in the previous section; in particular, we provide
some strategies to build Riordan arrays, to find corresponding production
matrices and their group inverse elements, respectively.

First of all we introduce (i)~\textit{function symbols} \verb|d_fn| and
\verb|h_fn| to denote arbitrary symbolic functions $d$ and $h$,
(ii)~\textit{indexed symbols} \verb|d| and \verb|h| to denote arbitrary
symbolic and indexed coefficients
\begin{minted}[baselinestretch=0.8]{python}
>>> d_fn, h_fn = Function('d'), Function('h')
>>> d, h = IndexedBase('d'), IndexedBase('h')
\end{minted}
respectively. To build Riordan matrices we use \verb|Matrix| objects; in
particular, the expression \verb|Matrix(r, c, ctor)| denotes a matrix with
\verb|r| rows and \verb|c| columns where each coefficient $d_{n,k}$ in the
matrix is defined according to \verb|ctor| which is a \textit{callable}
\marginnote{From the official doc at
\url{https://docs.python.org/3/library/functions.html\#callable}:
\texttt{callable(object)} return \texttt{True} if the \texttt{object} argument
appears callable, \texttt{False} if not. If this returns true, it is still
possible that a call fails, but if it is false, calling object will never
succeed. Note that classes are callable (calling a class returns a new
instance); instances are callable if their class has a \texttt{\_\_call\_\_}
method.} object consuming two arguments $n$ and $k$, its row and column
coordinates.  We call it \verb|ctor| as abbreviation for \textit{constructor},
because it allows us to code the definition of each coefficient with a Python
callable object.

Here we show how to build a pure symbolic matrix:
\begin{minted}[baselinestretch=0.8]{python}
>>> from sympy import Matrix
>>> rows, cols = 5, 5
>>> ctor = lambda i,j: d[i,j]
>>> Matrix(rows, cols, ctor)
\end{minted}
\begin{displaymath}
\left[\begin{matrix}d_{0,0} & d_{0,1} & d_{0,2} & d_{0,3} & d_{0,4}\\d_{1,0} & d_{1,1} & d_{1,2} & d_{1,3} & d_{1,4}\\d_{2,0} & d_{2,1} & d_{2,2} & d_{2,3} & d_{2,4}\\d_{3,0} & d_{3,1} & d_{3,2} & d_{3,3} & d_{3,4}\\d_{4,0} & d_{4,1} & d_{4,2} & d_{4,3} & d_{4,4}\end{matrix}\right]
\end{displaymath}

In the following sections we show a collection of such \verb|ctor|s, each one
of them implements one theoretical characterization used to denote Riordan
arrays and corresponding examples are given.

\subsection{Convolution ctor}

The following definition implements a ctor that allows us to build Riordan
arrays by convolution of their $d$ and $h$ functions; here it is,

\notbreakable{
    \inputminted[baselinestretch=0.8,stripnl=false,firstline=26, lastline=45]{python}{deps/simulation-methods/src/sequences.py}
}

\begin{example}
Symbolic Riordan array built by two polynomials with symbolic coefficients:
\begin{minted}[baselinestretch=0.8]{python}
>>> d_series = Eq(d_fn(t), 1+sum(d[i]*t**i for i in range(1,m)))
>>> h_series = Eq(h_fn(t), t*(1+sum(h[i]*t**i for i in range(1,m-1)))).expand()
>>> d_series, h_series
\end{minted}
\begin{displaymath}
\left ( d{\left (t \right )} = t^{4} d_{4} + t^{3} d_{3} + t^{2} d_{2} + t d_{1} + 1, \quad h{\left (t \right )} = t^{4} h_{3} + t^{3} h_{2} + t^{2} h_{1} + t\right )
\end{displaymath}
\begin{minted}[baselinestretch=0.8]{python}
>>> R = Matrix(m, m, riordan_matrix_by_convolution(m, d_series, h_series))
>>> R
\end{minted}
\begin{displaymath}
\left[\begin{matrix}1 &   &   &   &  \\d_{1} & 1 &   &   &  \\d_{2} & d_{1} + h_{1} & 1 &   &  \\d_{3} & d_{1} h_{1} + d_{2} + h_{2} & d_{1} + 2 h_{1} & 1 &  \\d_{4} & d_{1} h_{2} + d_{2} h_{1} + d_{3} + h_{3} & 2 d_{1} h_{1} + d_{2} + h_{1}^{2} + 2 h_{2} & d_{1} + 3 h_{1} & 1\end{matrix}\right]
\end{displaymath}
\end{example}

\begin{example}
The Pascal triangle built using closed generating functions:
\begin{minted}[baselinestretch=0.8]{python}
>>> d_series = Eq(d_fn(t), 1/(1-t))
>>> h_series = Eq(h_fn(t), t*d_series.rhs)
>>> d_series, h_series
\end{minted}
\begin{displaymath}
\left ( d{\left (t \right )} = \frac{1}{1-t}, \quad h{\left (t \right )} = \frac{t}{1-t}\right )
\end{displaymath}
\begin{minted}[baselinestretch=0.8]{python}
>>> R = Matrix(10, 10, riordan_matrix_by_convolution(10, d_series, h_series))
>>> R
\end{minted}
\begin{displaymath}
\left[\begin{matrix}1 &   &   &   &   &   &   &   &   &  \\1 & 1 &   &   &   &   &   &   &   &  \\1 & 2 & 1 &   &   &   &   &   &   &  \\1 & 3 & 3 & 1 &   &   &   &   &   &  \\1 & 4 & 6 & 4 & 1 &   &   &   &   &  \\1 & 5 & 10 & 10 & 5 & 1 &   &   &   &  \\1 & 6 & 15 & 20 & 15 & 6 & 1 &   &   &  \\1 & 7 & 21 & 35 & 35 & 21 & 7 & 1 &   &  \\1 & 8 & 28 & 56 & 70 & 56 & 28 & 8 & 1 &  \\1 & 9 & 36 & 84 & 126 & 126 & 84 & 36 & 9 & 1\end{matrix}\right]
\end{displaymath}
\end{example}

\subsection{Recurrence ctor}

The following definition implements a ctor that allows us to build Riordan
arrays by a recurrence relation over coefficients $d_{n+1, k+1}$; here it is,

\inputminted[baselinestretch=0.8,stripnl=false,firstline=81, lastline=107, mathescape=true]
    {python}{deps/simulation-methods/src/sequences.py}

\begin{example}
Symbolic Riordan Array built according to the recurrence:
\begin{displaymath}
\begin{split}
d_{n+1, 0} &= \bar{b}\,d_{n, 0} + c\,d_{n,1}, \quad n \in\mathbb{N} \\
d_{n+1, k+1} &= a\,d_{n, k} + b\,d_{n, k} + c\,d_{n,k+1}, \quad n,k \in\mathbb{N}
\end{split}
\end{displaymath}
\begin{minted}[baselinestretch=0.8]{python}
>>> dim = 5
>>> a, b, b_bar, c = symbols(r'a b \bar{b} c')
>>> M = Matrix(dim, dim,
...            riordan_matrix_by_recurrence(
...               dim, lambda n, k: {(n-1, k-1):a,
...                                  (n-1, k): b if k else b_bar,
...                                  (n-1, k+1):c}))
>>> M
\end{minted}
\begin{displaymath}
\footnotesize
\left[\begin{matrix}1 &   &   &   &  \\\bar{b} & a &   &   &  \\\bar{b}^{2} + a c & \bar{b} a + a b & a^{2} &   &  \\\bar{b}^{3} + 2 \bar{b} a c + a b c & \bar{b}^{2} a + \bar{b} a b + 2 a^{2} c + a b^{2} & \bar{b} a^{2} + 2 a^{2} b & a^{3} &  \\\bar{b}^{4} + 3 \bar{b}^{2} a c + 2 \bar{b} a b c + 2 a^{2} c^{2} + a b^{2} c & \bar{b}^{3} a + \bar{b}^{2} a b + 3 \bar{b} a^{2} c + \bar{b} a b^{2} + 5 a^{2} b c + a b^{3} & \bar{b}^{2} a^{2} + 2 \bar{b} a^{2} b + 3 a^{3} c + 3 a^{2} b^{2} & \bar{b} a^{3} + 3 a^{3} b & a^{4}\end{matrix}\right]
\end{displaymath}
\begin{minted}[baselinestretch=0.8]{python}
>>> production_matrix(M)
\end{minted}
\begin{displaymath}
\left[\begin{matrix}\bar{b} & a &   &  \\c & b & a &  \\  & c & b & a\\  &   & c & b\end{matrix}\right]
\end{displaymath}
Forcing $a=1$ and $\bar{b} = b$ yield the easier matrix \verb|Msubs|
\begin{minted}[baselinestretch=0.8]{python}
>>> Msubs = M.subs({a:1, b_bar:b})
>>> Msubs, production_matrix(Msubs)
\end{minted}
\begin{displaymath}
\left ( \left[\begin{matrix}1 &   &   &   &  \\b & 1 &   &   &  \\b^{2} + c & 2 b & 1 &   &  \\b^{3} + 3 b c & 3 b^{2} + 2 c & 3 b & 1 &  \\b^{4} + 6 b^{2} c + 2 c^{2} & 4 b^{3} + 8 b c & 6 b^{2} + 3 c & 4 b & 1\end{matrix}\right], \quad \left[\begin{matrix}b & 1 &   &  \\c & b & 1 &  \\  & c & b & 1\\  &   & c & b\end{matrix}\right]\right )
\end{displaymath}
and the correspoding production matrix checks the substitution.
\end{example}

Previous examples uses the function \verb|production_matrix| to compute the
\textit{production matrix} \citep{DEUTSCH2005101,Deutsch2009} of a Riordan
array, here is its definition with two helper \verb|ctor|s:

\inputminted[baselinestretch=0.8,stripnl=false,firstline=159, lastline=180]{python}{deps/simulation-methods/src/sequences.py}

\noindent implemented according to \citep[page~$215$]{barry2017riordan}.

\subsection{$A$ and $Z$ sequences ctor}

The following definition implements a ctor that allows us to build Riordan
arrays by their $Z$ and $A$ sequences; here it is,
\inputminted[baselinestretch=0.8,stripnl=false,firstline=47, lastline=78]
    {python}{deps/simulation-methods/src/sequences.py}
in words, (i)~it deconstructs \verb|seqs| into objs denoting Z and A seqs,
promoting both of them as callables, (ii)~it introduces the formal variable \verb|t|,
(iii)~it performs two Taylor expansions with respect to \verb|t|.
In order to build the resulting matrix, it
visits each cell according to the order given by \verb|lattice| then
if the cell lies not on the first column, it combines using \verb|A_series| coefficients
up to the cell in position \verb|(k-1, dim-1)|;
otherwise, %if the cell lies on the first column,
it combine using \verb|Z_series| coefficients up to the cell in position
\verb|(k-1, dim-1)|. Finally, it stores the combination and
if some post processing is desired, it does so; at last,
it returns a lambda that uses the computed matrix \verb|R|.

\begin{example}
Again the Pascal triangle built using $A$ and $Z$ sequences
\begin{minted}[baselinestretch=0.8]{python}
>>> A, Z = Function('A'), Function('Z')
>>> A_eq = Eq(A(t), 1 + t)
>>> Z_eq = Eq(Z(t),1)
>>> A_eq, Z_eq
\end{minted}
\begin{displaymath}
\left ( A{\left (t \right )} = t + 1, \quad Z{\left (t \right )} = 1\right )
\end{displaymath}
\begin{minted}[baselinestretch=0.8]{python}
>>> R = Matrix(10, 10, riordan_matrix_by_AZ_sequences(10, (Z_eq, A_eq)))
>>> R, production_matrix(R)
\end{minted}
\begin{displaymath}
\left ( \left[\begin{matrix}1 &   &   &   &   &   &   &   &   &  \\1 & 1 &   &   &   &   &   &   &   &  \\1 & 2 & 1 &   &   &   &   &   &   &  \\1 & 3 & 3 & 1 &   &   &   &   &   &  \\1 & 4 & 6 & 4 & 1 &   &   &   &   &  \\1 & 5 & 10 & 10 & 5 & 1 &   &   &   &  \\1 & 6 & 15 & 20 & 15 & 6 & 1 &   &   &  \\1 & 7 & 21 & 35 & 35 & 21 & 7 & 1 &   &  \\1 & 8 & 28 & 56 & 70 & 56 & 28 & 8 & 1 &  \\1 & 9 & 36 & 84 & 126 & 126 & 84 & 36 & 9 & 1\end{matrix}\right], \quad \left[\begin{matrix}1 & 1 &   &   &   &   &   &   &  \\  & 1 & 1 &   &   &   &   &   &  \\  &   & 1 & 1 &   &   &   &   &  \\  &   &   & 1 & 1 &   &   &   &  \\  &   &   &   & 1 & 1 &   &   &  \\  &   &   &   &   & 1 & 1 &   &  \\  &   &   &   &   &   & 1 & 1 &  \\  &   &   &   &   &   &   & 1 & 1\\  &   &   &   &   &   &   &   & 1\end{matrix}\right]\right )
\end{displaymath}
\end{example}

\begin{example}
Catalan triangle built using $A$ and $Z$ sequences,
\begin{minted}[baselinestretch=0.8]{python}
>>> A_ones = Eq(A(t), 1/(1-t)) # A is defined as in the previous example
>>> R = Matrix(10, 10, riordan_matrix_by_AZ_sequences(10, (A_ones, A_ones)))
>>> R, production_matrix(R)
\end{minted}
\begin{displaymath}
\left ( \left[\begin{matrix}1 &   &   &   &   &   &   &   &   &  \\1 & 1 &   &   &   &   &   &   &   &  \\2 & 2 & 1 &   &   &   &   &   &   &  \\5 & 5 & 3 & 1 &   &   &   &   &   &  \\14 & 14 & 9 & 4 & 1 &   &   &   &   &  \\42 & 42 & 28 & 14 & 5 & 1 &   &   &   &  \\132 & 132 & 90 & 48 & 20 & 6 & 1 &   &   &  \\429 & 429 & 297 & 165 & 75 & 27 & 7 & 1 &   &  \\1430 & 1430 & 1001 & 572 & 275 & 110 & 35 & 8 & 1 &  \\4862 & 4862 & 3432 & 2002 & 1001 & 429 & 154 & 44 & 9 & 1\end{matrix}\right], \quad \left[\begin{matrix}1 & 1 &   &   &   &   &   &   &  \\1 & 1 & 1 &   &   &   &   &   &  \\1 & 1 & 1 & 1 &   &   &   &   &  \\1 & 1 & 1 & 1 & 1 &   &   &   &  \\1 & 1 & 1 & 1 & 1 & 1 &   &   &  \\1 & 1 & 1 & 1 & 1 & 1 & 1 &   &  \\1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 &  \\1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1\\1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1\end{matrix}\right]\right )
\end{displaymath}
\end{example}

\begin{example}
Symbolic Riordan arrays built using $A$ and $Z$ sequences,
\begin{minted}[baselinestretch=0.8]{python}
>>> dim = 5
>>> a = IndexedBase('a')
>>> A_gen = Eq(A(t), sum((a[j] if j else 1)*t**j for j in range(dim)))
>>> R = Matrix(dim, dim, riordan_matrix_by_AZ_sequences(dim, (A_gen, A_gen)))
>>> R
\end{minted}
\begin{displaymath}
\footnotesize
\left[\begin{matrix}1 &   &   &   &  \\1 & 1 &   &   &  \\a_{1} + 1 & a_{1} + 1 & 1 &   &  \\a_{1}^{2} + 2 a_{1} + a_{2} + 1 & a_{1}^{2} + 2 a_{1} + a_{2} + 1 & 2 a_{1} + 1 & 1 &  \\a_{1}^{3} + 3 a_{1}^{2} + 3 a_{1} a_{2} + 3 a_{1} + 2 a_{2} + a_{3} + 1 & a_{1}^{3} + 3 a_{1}^{2} + 3 a_{1} a_{2} + 3 a_{1} + 2 a_{2} + a_{3} + 1 & 3 a_{1}^{2} + 3 a_{1} + 2 a_{2} + 1 & 3 a_{1} + 1 & 1\end{matrix}\right]
\end{displaymath}
\begin{minted}[baselinestretch=0.8]{python}
>>> z = IndexedBase('z')
>>> A_gen = Eq(A(t), sum((a[j] if j else 1)*t**j for j in range(dim)))
>>> Z_gen = Eq(Z(t), sum((z[j] if j else 1)*t**j for j in range(dim)))
>>> Raz = Matrix(dim, dim, riordan_matrix_by_AZ_sequences(dim, (Z_gen, A_gen)))
>>> Raz
\end{minted}
\begin{displaymath}
\footnotesize
\left[\begin{matrix}1 &   &   &   &  \\1 & 1 &   &   &  \\z_{1} + 1 & a_{1} + 1 & 1 &   &  \\a_{1} z_{1} + 2 z_{1} + z_{2} + 1 & a_{1}^{2} + a_{1} + a_{2} + z_{1} + 1 & 2 a_{1} + 1 & 1 &  \\ \left(\begin{split} a_{1}^{2} z_{1} &+ 2 a_{1} z_{1} + 2 a_{1} z_{2} + a_{2} z_{1} +\\ z_{1}^{2} &+ 3 z_{1} + 2 z_{2} + z_{3} + 1\end{split}\right) & a_{1}^{3} + a_{1}^{2} + 3 a_{1} a_{2} + 2 a_{1} z_{1} + a_{1} + a_{2} + a_{3} + 2 z_{1} + z_{2} + 1 & 3 a_{1}^{2} + 2 a_{1} + 2 a_{2} + z_{1} + 1 & 3 a_{1} + 1 & 1\end{matrix}\right]
\end{displaymath}
\begin{minted}[baselinestretch=0.8]{python}
>>> production_matrix(R), production_matrix(Raz)
\end{minted}
\begin{displaymath}
\left ( \left[\begin{matrix}1 & 1 &   &  \\a_{1} & a_{1} & 1 &  \\a_{2} & a_{2} & a_{1} & 1\\a_{3} & a_{3} & a_{2} & a_{1}\end{matrix}\right], \quad \left[\begin{matrix}1 & 1 &   &  \\z_{1} & a_{1} & 1 &  \\z_{2} & a_{2} & a_{1} & 1\\z_{3} & a_{3} & a_{2} & a_{1}\end{matrix}\right]\right )
\end{displaymath}
\end{example}

\subsection{Exponential ctor}

The following definition implements a ctor that allows us to build an
exponential Riordan array; here it is,
\inputminted[baselinestretch=0.8,stripnl=false,firstline=23, lastline=24]
    {python}{deps/simulation-methods/src/sequences.py}

\begin{example}
Build the triangle of Stirling numbers of the II kind:
\begin{minted}[baselinestretch=0.8]{python}
>>> d_series = Eq(d_fn(t), 1)
>>> h_series = Eq(h_fn(t), exp(t)-1)
>>> d_series, h_series
\end{minted}
\begin{displaymath}
\left ( d{\left (t \right )} = 1, \quad h{\left (t \right )} = e^{t} - 1\right )
\end{displaymath}
\begin{minted}[baselinestretch=0.8]{python}
>>> R = matrix(10, 10, riordan_matrix_exponential(
...                     riordan_matrix_by_convolution(
...                       10, d_series, h_series)))
>>> R
\end{minted}
\begin{displaymath}
\left[\begin{matrix}1 &   &   &   &   &   &   &   &   &  \\  & 1 &   &   &   &   &   &   &   &  \\  & 1 & 1 &   &   &   &   &   &   &  \\  & 1 & 3 & 1 &   &   &   &   &   &  \\  & 1 & 7 & 6 & 1 &   &   &   &   &  \\  & 1 & 15 & 25 & 10 & 1 &   &   &   &  \\  & 1 & 31 & 90 & 65 & 15 & 1 &   &   &  \\  & 1 & 63 & 301 & 350 & 140 & 21 & 1 &   &  \\  & 1 & 127 & 966 & 1701 & 1050 & 266 & 28 & 1 &  \\  & 1 & 255 & 3025 & 7770 & 6951 & 2646 & 462 & 36 & 1\end{matrix}\right]
\end{displaymath}
\begin{minted}[baselinestretch=0.8]{python}
>>> production_matrix(R), production_matrix(R, exp=True)
\end{minted}
\begin{displaymath}
\left ( \left[\begin{matrix}0 & 1 &   &   &   &   &   &   &  \\  & 1 & 1 &   &   &   &   &   &  \\  &   & 2 & 1 &   &   &   &   &  \\  &   &   & 3 & 1 &   &   &   &  \\  &   &   &   & 4 & 1 &   &   &  \\  &   &   &   &   & 5 & 1 &   &  \\  &   &   &   &   &   & 6 & 1 &  \\  &   &   &   &   &   &   & 7 & 1\\  &   &   &   &   &   &   &   & 8\end{matrix}\right], \quad \left[\begin{matrix}0 & 1 &   &   &   &   &   &   &  \\  & 1 & 2 &   &   &   &   &   &  \\  &   & 2 & 3 &   &   &   &   &  \\  &   &   & 3 & 4 &   &   &   &  \\  &   &   &   & 4 & 5 &   &   &  \\  &   &   &   &   & 5 & 6 &   &  \\  &   &   &   &   &   & 6 & 7 &  \\  &   &   &   &   &   &   & 7 & 8\\  &   &   &   &   &   &   &   & 8\end{matrix}\right]\right )
\end{displaymath}
\begin{minted}[baselinestretch=0.8]{python}
>>> inspect(R)
nature(is_ordinary=False, is_exponential=True)
\end{minted}
\end{example}
In the above example we introduced another function \verb|inspect| that studies
the type of array it consumes. Before reporting its definition we remark that
the matrix on the left is an usual production matrix (which tells us that
$d_{6,4} = d_{5,3} + 4d_{5,4} = 25 + 4\cdot 10 = 65$, for example); on the
other hand, the matrix on right helps to decide if the array is an exponential
one by proving that each diagonal is an \textit{arithmetic progression}, for
more on this see \citep{barry2017riordan}.
\inputminted[baselinestretch=0.8,stripnl=false,firstline=183, lastline=209]
    {python}{deps/simulation-methods/src/sequences.py}

\begin{example}
In this example we explore an exponential Riordan array starting from the
generating functions of the Pascal triangle. Surprisingly the array we get back
is known in the OEIS (\url{https://oeis.org/A021009}) and looking for some
comments we quote the observation\newline
\begin{center}
\textit{"the generalized Riordan array $(e^x, x)$ with respect
to\newline the sequence $n!$ is Pascal's triangle A007318"}
\end{center}
by Peter Bala.
\begin{minted}[baselinestretch=0.8]{python}
>>> d_series, h_series = Eq(d_fn(t), 1/(1-t)), Eq(h_fn(t), t/(1-t))
>>> d_series, h_series
\end{minted}
\begin{displaymath}
\left ( d{\left (t \right )} = \frac{1}{1-t}, \quad h{\left (t \right )} = \frac{t}{1-t}\right )
\end{displaymath}
\begin{minted}[baselinestretch=0.8]{python}
>>> R = matrix(10, 10, riordan_matrix_exponential(
...                     riordan_matrix_by_convolution(
...                       10, d_series, h_series)))
>>> R
\end{minted}
\begin{displaymath}
\left[\begin{matrix}1 &   &   &   &   &   &   &   &   &  \\1 & 1 &   &   &   &   &   &   &   &  \\2 & 4 & 1 &   &   &   &   &   &   &  \\6 & 18 & 9 & 1 &   &   &   &   &   &  \\24 & 96 & 72 & 16 & 1 &   &   &   &   &  \\120 & 600 & 600 & 200 & 25 & 1 &   &   &   &  \\720 & 4320 & 5400 & 2400 & 450 & 36 & 1 &   &   &  \\5040 & 35280 & 52920 & 29400 & 7350 & 882 & 49 & 1 &   &  \\40320 & 322560 & 564480 & 376320 & 117600 & 18816 & 1568 & 64 & 1 &  \\362880 & 3265920 & 6531840 & 5080320 & 1905120 & 381024 & 42336 & 2592 & 81 & 1\end{matrix}\right]
\end{displaymath}
\begin{minted}[baselinestretch=0.8]{python}
>>> production_matrix(R), production_matrix(R, exp=True)
\end{minted}
\begin{displaymath}
\left ( \left[\begin{matrix}1 & 1 &   &   &   &   &   &   &  \\1 & 3 & 1 &   &   &   &   &   &  \\  & 4 & 5 & 1 &   &   &   &   &  \\  &   & 9 & 7 & 1 &   &   &   &  \\  &   &   & 16 & 9 & 1 &   &   &  \\  &   &   &   & 25 & 11 & 1 &   &  \\  &   &   &   &   & 36 & 13 & 1 &  \\  &   &   &   &   &   & 49 & 15 & 1\\  &   &   &   &   &   &   & 64 & 17\end{matrix}\right], \quad \left[\begin{matrix}1 & 1 &   &   &   &   &   &   &  \\1 & 3 & 2 &   &   &   &   &   &  \\  & 2 & 5 & 3 &   &   &   &   &  \\  &   & 3 & 7 & 4 &   &   &   &  \\  &   &   & 4 & 9 & 5 &   &   &  \\  &   &   &   & 5 & 11 & 6 &   &  \\  &   &   &   &   & 6 & 13 & 7 &  \\  &   &   &   &   &   & 7 & 15 & 8\\  &   &   &   &   &   &   & 8 & 17\end{matrix}\right]\right )
\end{displaymath}
\begin{minted}[baselinestretch=0.8]{python}
>>> inspect(R)
nature(is_ordinary=False, is_exponential=True)
\end{minted}
%More surprisingly, the same array is used in \citep{barry2017riordan} as a case study.
\end{example}

\subsection{Group inverse elements}

In this final section we show how to compute the compositional inverse of a
function and then apply this procedure to find the inverse of a given Riordan
array. By small steps, your task is to find the compositional inverse of
Pascal array's $h$ function
\begin{displaymath}
h(t)= \frac{t}{1-t},
\end{displaymath}
namely you want to find a function $\bar{h}$ such that $\bar{h}(h(t))=t$.
Starting from this very last identity we use the substitution notation
\begin{displaymath}
\bar{h}(h(t)) = t \leftrightarrow \left[ \bar{h}(y) = t\, | \, y = h(t) \right]
\end{displaymath}
that allows us to reduce the original problem to solve $y = h(t)$ with respect
to $t$; formally, using the definition of $h$ we rewrite
\begin{displaymath}
y = \frac{t}{1-t} \quad\text{that implies}\quad t = \frac{y}{1+y}.
\end{displaymath}
The latter identity can be used back in $\bar{h}(y) = t$ as substitution for
$t$ yielding $\displaystyle \bar{h}(y)= \frac{y}{1+y} $ as required; this
procedure is promptly implemented as
\inputminted[baselinestretch=0.8,stripnl=false,firstline=212, lastline=229, mathescape=true]
    {python}{deps/simulation-methods/src/sequences.py}
\inputminted[baselinestretch=0.8,stripnl=false,firstline=231, lastline=254, mathescape=true]
    {python}{deps/simulation-methods/src/sequences.py}

\begin{example}
Compositional inverse of Catalan tringle's $h$ generating function:
\begin{minted}[baselinestretch=0.8]{python}
>>> catalan_term = (1-sqrt(1-4*t))/(2*t)
>>> d_series = Eq(d_fn(t), catalan_term)
>>> h_series = Eq(h_fn(t), t*catalan_term)
>>> h_series, compositional_inverse(h_series)
\end{minted}
\begin{displaymath}
\left ( h{\left (t \right )} = - \frac{1}{2} \sqrt{- 4 t + 1} + \frac{1}{2}, \quad \bar{ h }{\left (y \right )} = - y \left(y - 1\right)\right )
\end{displaymath}
\begin{minted}[baselinestretch=0.8]{python}
>>> C_inverse = group_inverse(d_series, h_series, post=radsimp)
>>> C_inverse
\end{minted}
\begin{displaymath}
\left ( g{\left (t \right )} = \frac{1}{2} \sqrt{4 t^{2} - 4 t + 1} + \frac{1}{2}, \quad f{\left (t \right )} = t \left(- t + 1\right)\right )
\end{displaymath}
\begin{minted}[baselinestretch=0.8]{python}
>>> R = Matrix(10, 10, riordan_matrix_by_convolution(
...                     10, C_inverse[0], C_inverse[1]))
>>> R
\end{minted}
\begin{displaymath}
\left[\begin{matrix}1 &   &   &   &   &   &   &   &   &  \\-1 & 1 &   &   &   &   &   &   &   &  \\  & -2 & 1 &   &   &   &   &   &   &  \\  & 1 & -3 & 1 &   &   &   &   &   &  \\  &   & 3 & -4 & 1 &   &   &   &   &  \\  &   & -1 & 6 & -5 & 1 &   &   &   &  \\  &   &   & -4 & 10 & -6 & 1 &   &   &  \\  &   &   & 1 & -10 & 15 & -7 & 1 &   &  \\  &   &   &   & 5 & -20 & 21 & -8 & 1 &  \\  &   &   &   & -1 & 15 & -35 & 28 & -9 & 1\end{matrix}\right]
\end{displaymath}
\end{example}

\section*{Conclusions}

This chapter offers to the reader a concise review of the theory of Riordan
Arrays by recalling definitions, characterizations and their fundamental
properties; moreover, we pair these formal arguments with a set of software
abstractions that allow us to mimic the theory with objects living in a
computer. Coding our definitions using the Python programming language and
taking advantage of the symbolic module \textit{Sympy}, we provide a coherent
and unified environment to experiment in.


\chapter{EMT Problems}
\section{Magnetostatics}
\begin{enumerate}
	\item  A spherical shell of radius $R$, carrying a uniform surface charge $\sigma$, is set spinning at angular velocity $\omega$. Find its Magnetic dipole moment.
	\begin{answer}
		 The total charge on the shaded ring is
		 \begin{figure}[H]
		 	\centering
		 	\includegraphics[height=5cm,width=4.5cm]{EM prblm-01}
		 \end{figure}
		\begin{align*}
		d q&=\sigma(2 \pi R \sin \theta) R d \theta\\
	\text{	Time for one revolution is }d t&=\frac{2 \pi}{\omega}\\
		\Rightarrow\text{ Current in the ring }I&=\frac{d q}{d t}=\sigma \omega R^{2} \sin \theta d \theta
		\intertext{Area of the ring $=\pi(\mathrm{R} \sin \theta)^{2}$, so the magnetic moment of the ring is}
		d m&=\left(\sigma \omega R^{2} \sin \theta d \theta\right) \times \pi R^{2} \sin ^{2} \theta\\
		m=\sigma \omega R^{4} \int_{0}^{\pi} \sin ^{3} \theta d \theta&=\frac{4}{3} \pi \times . \sigma \omega R^{4} \Rightarrow \vec{m}=\frac{4 \pi}{3} \sigma \omega R^{4} \hat{z}
		\end{align*}
	\end{answer}
	\item  A solid cylinder of radius $R$ has total charge $Q$ distributed uniformly over its volume and uniform mass density has total mass $M$. It is rotating about its axis with angular speed $\omega$. If its angular momentum is $L$ and magnetic moment is $\mu$, then find the ratio $\frac{\mu}{L} .$
	\begin{answer}
		\begin{align*}
	I=\frac{1}{2} M R^{2}, L&=I \omega=\frac{1}{2} M R^{2} \omega\\
		\text{Magnetic moment due to disc }\mu&=\frac{\pi \sigma \omega R^{4}}{4}\\
		\text{Due to cylinder }d \mu&=\frac{\pi \omega R^{4}}{4}(\rho d z) \quad(\sigma \rightarrow \rho d z)\\
		\mu=\frac{\pi \omega R^{4}}{4} \int_{0}^{L} \frac{Q}{\pi R^{2} L} d z&=\frac{Q \omega R^{2}}{4} \quad \Rightarrow \frac{\mu}{L}=\frac{\underline{Q} \omega R^{2}}{\frac{1}{2} M R^{2} \omega}=\frac{Q}{2 M}
		\end{align*}
	\end{answer}
	\item  A current carrying loop is placed in a uniform magnetic field in four different orientations I, II, III and IV. Arrange them in the decreasing order of potential energy.
	 \begin{tasks}(2)
		\task[\textbf{I.}]\begin{figure}[H]
			\centering
			\includegraphics[height=2cm,width=3.5cm]{EM prblm-02}
		\end{figure}
		\task[\textbf{II.}]\begin{figure}[H]
			\centering
			\includegraphics[height=1.5cm,width=3cm]{EM prblm-03}
		\end{figure}
		\task[\textbf{III.}]\begin{figure}[H]
			\centering
			\includegraphics[height=2cm,width=2.5cm]{EM prblm-04}
		\end{figure}
		\task[\textbf{IV.}] \begin{figure}[H]
			\centering
			\includegraphics[height=2cm,width=3cm]{EM prblm-05}
		\end{figure}
	\end{tasks}
	\begin{answer}
		\begin{align*}
				\because U&=-\vec{m} \cdot \vec{B}=-m B \cos \theta \\
	\text { I. } \theta=180^{\circ} \Rightarrow U&=+m B, \hspace{1.5cm} \text { II. } \theta=90^{\circ} \Rightarrow U=0 \\
	 \text { III. } \theta=\text { Acute angle } \Rightarrow U&=-v e,  \text { IV. } \hspace{1cm}\theta=\text { Obtuse angle } \Rightarrow U=+v e \\ &\text { Thus } 1>\mathrm{IV}>\mathrm{II}>\mathrm{III}
		\end{align*}
	\end{answer}
	\item The region between $x=0$ and $x=L$ is filled with uniform, steady magnetic field $B_{0} \hat{k}$. A particle of mass $m$, positive charge $q$ and velocity $v_{0} \hat{i}$ travels along $x$-axis and enters the region of magnetic field. Neglect gravity throughout the question.\\
	(a) Find the value of $L$ if the particle emerges from the region of magnetic field with its final velocity at an angle $30^{\circ}$ to the initial velocity.\\
	(b) Find the final velocity of the particle and the time spent by it in the magnetic field, if the field now extends up to $x=2.1 L$.
	\begin{answer}
		(a) As the initial velocity of the particle is perpendicular to the field the particle will move along the arc of a circle as shown.
		If $r$ is the radius of the circle, then
		\begin{figure}[H]
			\centering
			\includegraphics[height=3.7cm,width=5cm]{EM prblm-06}
		\end{figure}
		\begin{align*}
		\frac{m v_{0}^{2}}{r}&=q v_{0} B_{0}\\
		\text{Also from geometry,}\hspace{1cm}
		L&=r \sin 30^{\circ}\\
		\Rightarrow r&=2 L\\
		\text{or}
		L&=\frac{m v_{0}}{2 q B_{0}}\hspace{1cm}\\
	\text{	(b) In this case,}
		L&=\frac{2.1 m v_{0}}{2 q B_{0}}>r
		\end{align*}
		\begin{figure}[H]
			\centering
			\includegraphics[height=3.5cm,width=3.5cm]{EM prblm-07}
		\end{figure}
			Hence the particle will complete semi-circular path and emerge from the field with velocity $v_{0} \hat{i}$ as shown. Time spent by the particle in the magnetic field
		\begin{align*}
		T=\frac{\pi r}{v_{0}}&=\frac{\pi m}{q B_{0}}
		\end{align*}
		The speed of the particle does not change due to magnetic field.
	\end{answer}
	\item A straight segment $O C$ (of length $L$ meter) of a circuit carrying a current 1 ampere is placed along the $x$-axis. Two infinitely long straight wire $A$ and $B$, each exiuding $z=-\infty$ to $+\infty$ are fixed at $y=-a$ metre and $y=+$ a metre respectively, as shown in the figure. If the wires $A$ and $B$ each carry a current $I$ ampere into the plane of the paper, obtain the cxpression for the force acting on segment $O C$. What will be the force on $O C$ if the current in the wire $B$ is reversed?
	\begin{figure}[H]
		\centering
		\includegraphics[height=3.2cm,width=5cm]{EM prblm-08}
	\end{figure}
	\begin{answer}
			Magnetic field $B_{A}$ produced at $P(x, 0,0)$ due to wire, $B_{A}=\mu_{0} I / 2 \pi R, B_{B}=\mu_{0} I / 2 \pi R .$ Components of $B_{A}$ and $B_{B}$ along $x$-axis cancel, while those along $y$-axis add up to give total field.
			\begin{figure}[H]
				\centering
				\includegraphics[height=3.5cm,width=5cm]{EM prblm-09}
			\end{figure}
		\begin{align*}
		B&=2\left(\frac{\mu_{0} I}{2 \pi R}\right) \cos \theta=\frac{2 \mu_{0} I}{2 \pi R} \cdot \frac{x}{R}=\frac{\mu_{0} I}{\pi} \frac{x}{\left(a^{2}+x^{2}\right)}\text{ (along $y$ direction)}\\
		\text{The force }&d F\text{ acting on the current element is }d \vec{F}=I(d \vec{l} \times \vec{B})\\
		\therefore \quad d F&=\frac{\mu_{0} I^{2}}{\pi} \frac{x d x}{a^{2}+x^{2}}\hspace{2cm}\left[\because \sin 90^{\circ}=1\right]\\
		\Rightarrow \quad F&=\frac{\mu_{0} I^{2}}{\pi} \int_{0}^{L} \frac{x d x}{a^{2}+x^{2}}=\frac{\mu_{0} I^{2}}{2 \pi} \ln \frac{a^{2}+L^{2}}{a^{2}}
		\end{align*}
		If the current in $B$ is reversed, the magnetic field due to the two wires would be only along $x$-direction and the force on the current along $x$-direction will be zero.
	\end{answer}
\item A wire loop carrying a current $I$ is placed in the $x-y$ plane as shown in figure, (a) If a particle with charge $q$ and mass $m$ is placed at the centre $P$ and given a velocity $v$ along $N P$, find its instantaneous acceleration. (b) If an external uniform magnetic induction $\vec{B}=B \hat{i}$ is applied, find the force and torque acting on the loop.
\begin{figure}[H]
	\centering
	\includegraphics[height=4.5cm,width=4.7cm]{EM prblm-10}
\end{figure}
\begin{answer}
		As in case of current-carrying straight conductor and arc, the magnitude of $B$ is given by\\
		\begin{figure}[H]
			\centering
			\includegraphics[height=4.5cm,width=4.7cm]{EM prblm-11}
		\end{figure}
	\begin{align*}
	B_{1}&=\frac{\mu_{0} I}{4 \pi d}(\sin \alpha+\sin \beta) \\
	\text{and}\hspace{1cm}B_{2}&=\frac{\mu_{0} I \phi}{4 \pi r}
	\intertext{So in accordance with right hand screw rule,}
	\end{align*}
	\begin{align*}
	\left(\vec{B}_{w}\right)&=\frac{\mu_{0}}{4 \pi} \frac{1}{(a \cos 60)} \times 2 \sin 60(-\hat{k})\\
	\text{and due to arc}\hspace{1cm}
	(\vec{B})_{M N}&=\frac{\mu_{0}}{4 \pi} \cdot \frac{I}{a} \times\left(\frac{2}{3} \pi\right)(+\hat{k})
\intertext{	and hence net $\vec{B}$ at $P$ due to the given loop}
	\vec{B}&=\vec{B}_{w}+\vec{B}_{A} \\
	\Rightarrow \vec{B}&=\frac{\mu_{0}}{4 \pi} \cdot \frac{2 I}{a}\left[\sqrt{3}-\frac{\pi}{3}\right](-\hat{k})
\intertext{	Now as force on charged particle in a magnetic field is given by}
	\vec{F}&=q(\vec{v} \times \vec{B})\\
\text{	So here,}\hspace{1cm}
	\vec{F}&=q v B \sin 90^{\circ} \text { along } P F \\
	\vec{F}&=\frac{\mu_{0}}{4 \pi} \frac{2 q v}{a}\left[\sqrt{3}-\frac{\pi}{3}\right] \text { along } P F\\
\text{	and so }\quad
	\vec{a}&=\frac{\vec{F}}{m}=10^{-7} \frac{2 q v I}{a}\left[\sqrt{3}-\frac{\pi}{3}\right] \text { along } P F\\
	\text{(b) As }\hspace{1cm}d \vec{F}&=I d \vec{L} \times \vec{B},\text{ so }\vec{F}=\int I d \vec{L} \times \vec{B}\\
\text{	As here $I$ and $\vec{B}$ are constant
	}\quad
	\vec{F}&=I[\oint d \vec{L}] \times \vec{B}=0\quad[\text { as } \oint d \vec{L}=0]\\
	\text{Further as area of coil,}\quad
	\vec{S}&=\left[\frac{1}{3} \pi a^{2}-\frac{1}{2} \cdot 2 a \sin 60^{\circ} \times a \cos 60^{\circ}\right] \hat{k}=a^{2}\left[\frac{\pi}{3}-\frac{\sqrt{3}}{4}\right] \hat{k}\\
\text{	So}\quad
	\vec{M}&=I \vec{S}=I a^{2}\left[\frac{\pi}{3}-\frac{\sqrt{3}}{4}\right] \hat{k}\\
\text{	and hence}\quad
	\vec{\tau}
	&=\vec{M} \times \vec{B}=I a^{2} B\left[\frac{\pi}{3}-\frac{\sqrt{3}}{4}\right](\hat{k} \times \hat{i})\\
	 \text{i.e.}\qquad\vec{\tau}&=I a^{2}\qquad B\left[\frac{\pi}{3}-\frac{\sqrt{3}}{4}\right] \hat{j} N-m
	\quad\operatorname{as}(\hat{k} \times \hat{i}=\hat{j})
	\end{align*}
\end{answer}
	\item 11. A uniform magnetic field of $30 m T$ exists in the $+x$ direction. A particle of charge $+e$ and mass $1.67 \times 10^{-27} \mathrm{~kg}$ is projected into the field along the $+y$ direction with a speed of $4.8 \times 10^{6} \mathrm{~m} / \mathrm{s} .$\\
	(a) Find the force on the charged particle in magnitude and direction.\\
	(b) Find the force if the particle were negatively charged.\\
	(c) Describe the nature of path followed by the particle in both the cases.
	\begin{answer}
			(a) Force acting on a charge particle moving in the magnetic field
			\begin{figure}[H]
				\centering
				\includegraphics[height=3cm,width=3cm]{EM prblm-12}
			\end{figure}
		\begin{align*}
	\vec{F}&=q(\vec{v} \times \vec{B})\\
	\text{Magnetic field}
	\vec{B}&=30(m T) \hat{j}\\
\text{	Velocity of the charge particle }\quad \vec{v}&=4.8 \times 10^{6}(\mathrm{~m} / \mathrm{s}) \hat{j}\\
\vec{F}&=1.6 \times 10^{-19}\left[\left(4.8 \times 10^{6} \hat{j}\right) \times\left(30 \times 10^{-3}\right)(\hat{i})\right]\\
\vec{F}&=230.4 \times 10^{-16}(-\hat{k}) N
\intertext{	(b) If the particle were negatively charged, the magnitude of the force will be the same but the direction will be along $(+z)$ direction.}
\intertext{	(c) Since, $\vec{v} \perp \vec{B}$, the path described is a circle}
 R &=\frac{m v}{q B}=\left(1.67 \times 10^{-27}\right) \cdot\left(4.8 \times 10^{6}\right) /\left(1.6 \times 10^{-19}\right) \cdot\left(30 \times 10^{-3}\right) \\ &=1.67 \mathrm{~m} .
		\end{align*}
	\end{answer}
	\item A disc of radius $R$ rotates at an angular velocity $\omega$ about the axis perpendicular to its surface and passing through its centre. If the disc has a uniform surface charge density $\sigma$, find the magnetic induction on the axis of rotation at a distance $x$ from the centre.
	\begin{answer}
		Consider a ring of radius $r$ and width $d r$.
		\begin{align}
		\text{	Charge on the ring,}	d q&=(2 \pi r d r) \sigma\notag\\
	\text{	Current due to ring is }d I&=\frac{d q}{T}\notag\\
	&=\frac{\omega d q}{2 \pi}=\sigma \omega r d r\notag
\intertext{	Magnetic field due to ring at point $P$ is}\notag
	d B&=\frac{\mu_{0} d l r^{2}}{2\left(r^{2}+x^{2}\right)^{3 / 2}}\notag\\
\text{	or}\quad
	B&=\int d B=\frac{\mu_{0} \sigma \omega}{2} \int_{0}^{R} \frac{r^{3} d r}{\left(r^{2}+x^{2}\right)^{3 / 2}}\label{emp20}\\
\text{	Putting }r^{2}+x^{2}=t^{2}\text{ and }2 r d r&=2 t d t\text{ and integrating (\ref{emp20}), we get}\notag\\
	B&=\frac{\mu_{0} \sigma \omega}{2}\left[\frac{R^{2}+2 x^{2}}{\sqrt{R^{2}+x^{2}}}-2 x\right]\notag
		\end{align}
	\end{answer}

\end{enumerate}


\documentclass{article}
\usepackage[british]{babel}

\title{Building Management System}
\author{Dr Alun Moon}

\usepackage{tlatex}
\usepackage{color}
\definecolor{boxshade}{gray}{.8}
\setboolean{shading}{true}

\begin{document}
\maketitle
\section{Requirements}
\begin{quote}
To track people as they enter and leave a building.
\end{quote}

\section{Specification}
\begin{tla}
------------------------------ MODULE building ------------------------------
(* Sample solution for first TLA+ exercise *)
CONSTANT
    People      \* we're dealing with people here
                \* this is the set of all people
VARIABLE
    register,   \* Set of registered users
    in,         \* Set of people in the building
    out         \* Set of people out of the building

TypeOK ==  \* type invarient
    /\ register \subseteq People    \* Everyone on the register is a person
    /\ register = in \union out     \* everyones location is known
    /\ in \intersect out = {}       \* noone can be both in and out of the building

------

Init ==
   /\ register = {}    \* Initially no-one is registered
   /\  in      = {}    \*           no-one is inside
   /\  out     = {}    \*           no-one is outside

Register(p) ==
    /\  p \in People \ register        \* p is a person and not registered
    /\ register' = register \union {p} \* add p to register
    /\ out' = out \union {p}           \* p is outside
    /\ in' = in                        \* must keep set of those inside the same

Enter(p) ==
    /\ p \in out               \* p is outside the building
    /\ in' = in \union {p}     \* add p to the inside set
    /\ out' = out \ {p}        \* remove p from the outside set
    /\ register' = register    \* register is unchanged

Leave(p) ==
    /\ p \in in                \* p is in the building
    /\ in' = in \ {p}          \* remove p from the inside set
    /\ out' = out \union {p}   \* add p to the outside set
    /\ register' = register    \* resigter is unchanged

Next ==
    \exists p \in People :   \* There is a person who can either
        \/ Register(p)       \* be registered, or
        \/ Enter(p)          \* enter the building, or
        \/ Leave(p)          \* leave the building

=============================================================================
\* Modification History
\* Last modified Wed Oct 02 10:31:48 BST 2019 by alun
\* Last modified Tue Sep 10 12:27:57 BST 2019 by cgam1
\* Created Mon Sep 24 11:53:39 BST 2018 by cgam1
\end{tla}
\begin{tlatex}
\@x{}\moduleLeftDash\@xx{ {\MODULE} building}\moduleRightDash\@xx{}%
\@x{}%
\@y{%
  Sample solution for first TLA+ exercise
}%
\@xx{}%
\@x{ {\CONSTANT}}%
\@x{\@s{16.4} People\@s{20.5}}%
\@y{%
  we're dealing with people here
}%
\@xx{}%
\@x{\@s{65.90}}%
\@y{%
  this is the set of all people
}%
\@xx{}%
\@x{ {\VARIABLE}}%
\@x{\@s{16.4} register ,\,\@s{10.62}}%
\@y{%
  Set of registered users
}%
\@xx{}%
\@x{\@s{16.4} in ,\,\@s{33.93}}%
\@y{%
  Set of people in the building
}%
\@xx{}%
\@x{\@s{16.4} out\@s{34.75}}%
\@y{%
  Set of people out of the building
}%
\@xx{}%
\@pvspace{8.0pt}%
\@x{ TypeOK \.{\defeq}\@s{4.1}}%
\@y{%
  type invarient
}%
\@xx{}%
\@x{\@s{16.4} \.{\land}\@s{9.74} register \.{\subseteq} People\@s{18.61}}%
\@y{%
  Everyone on the register is a person
}%
\@xx{}%
\@x{\@s{16.4} \.{\land}\@s{9.74} register \.{=} in \.{\cup} out\@s{16.4}}%
\@y{%
  everyones location is known
}%
\@xx{}%
\@x{\@s{16.4} \.{\land}\@s{9.74} in \.{\cap} out \.{=} \{ \}\@s{35.06}}%
\@y{%
  noone can be both in and out of the building
}%
\@xx{}%
\@pvspace{8.0pt}%
\@x{}\midbar\@xx{}%
\@pvspace{8.0pt}%
\@x{ Init \.{\defeq}}%
\@x{\@s{12.29} \.{\land} register \.{=} \{ \}\@s{14.78}}%
\@y{%
  Initially no-one is registered
}%
\@xx{}%
\@x{\@s{12.29} \.{\land}\@s{4.1} in\@s{21.69} \.{=} \{ \}\@s{12.29}}%
\@y{%
            no-one is inside
}%
\@xx{}%
\@x{\@s{12.29} \.{\land}\@s{4.10} out\@s{13.91} \.{=} \{ \}\@s{14.78}}%
\@y{%
            no-one is outside
}%
\@xx{}%
\@pvspace{8.0pt}%
\@x{ Register ( p ) \.{\defeq}}%
 \@x{\@s{16.4} \.{\land}\@s{8.33} p \.{\in} People \.{\,\backslash\,}
 register\@s{28.7}}%
\@y{%
  p is a person and not registered
}%
\@xx{}%
 \@x{\@s{16.4} \.{\land} register \.{'} \.{=} register \.{\cup} \{ p
 \}\@s{17.68}}%
\@y{%
  add p to register
}%
\@xx{}%
\@x{\@s{16.4} \.{\land} out \.{'} \.{=} out \.{\cup} \{ p \}\@s{53.71}}%
\@y{%
  p is outside
}%
\@xx{}%
\@x{\@s{16.4} \.{\land} in \.{'} \.{=} in\@s{91.15}}%
\@y{%
  must keep set of those inside the same
}%
\@xx{}%
\@pvspace{8.0pt}%
\@x{ Enter ( p ) \.{\defeq}}%
\@x{\@s{16.4} \.{\land} p \.{\in} out\@s{57.4}}%
\@y{%
  p is outside the building
}%
\@xx{}%
\@x{\@s{16.4} \.{\land} in \.{'} \.{=} in \.{\cup} \{ p \}\@s{29.31}}%
\@y{%
  add p to the inside set
}%
\@xx{}%
 \@x{\@s{16.4} \.{\land} out \.{'} \.{=} out \.{\,\backslash\,} \{ p
 \}\@s{21.51}}%
\@y{%
  remove p from the outside set
}%
\@xx{}%
\@x{\@s{16.4} \.{\land} register \.{'} \.{=} register\@s{9.55}}%
\@y{%
  register is unchanged
}%
\@xx{}%
\@pvspace{8.0pt}%
\@x{ Leave ( p ) \.{\defeq}}%
\@x{\@s{16.4} \.{\land} p \.{\in} in\@s{62.69}}%
\@y{%
  p is in the building
}%
\@xx{}%
 \@x{\@s{16.4} \.{\land} in \.{'} \.{=} in \.{\,\backslash\,} \{ p
 \}\@s{32.09}}%
\@y{%
  remove p from the inside set
}%
\@xx{}%
\@x{\@s{16.4} \.{\land} out \.{'} \.{=} out \.{\cup} \{ p \}\@s{18.73}}%
\@y{%
  add p to the outside set
}%
\@xx{}%
\@x{\@s{16.4} \.{\land} register \.{'} \.{=} register\@s{9.55}}%
\@y{%
  resigter is unchanged
}%
\@xx{}%
\@pvspace{8.0pt}%
\@x{ Next \.{\defeq}}%
\@x{\@s{16.4} \exists\, p \.{\in} People \.{:}\@s{21.44}}%
\@y{%
  There is a person who can either
}%
\@xx{}%
\@x{\@s{32.8} \.{\lor} Register ( p )\@s{24.59}}%
\@y{%
  be registered, or
}%
\@xx{}%
\@x{\@s{32.8} \.{\lor} Enter ( p )\@s{36.89}}%
\@y{%
  enter the building, or
}%
\@xx{}%
\@x{\@s{32.8} \.{\lor} Leave ( p )\@s{19.13}}%
\@y{%
  leave the building
}%
\@xx{}%
\@pvspace{8.0pt}%
\@x{}\bottombar\@xx{}%
\@x{}%
\@y{%
  Modification History
}%
\@xx{}%
\@x{}%
\@y{%
  Last modified Wed Oct 02 10:31:48 BST 2019 by alun
}%
\@xx{}%
\@x{}%
\@y{%
  Last modified Tue Sep 10 12:27:57 BST 2019 by cgam1
}%
\@xx{}%
\@x{}%
\@y{%
  Created Mon Sep 24 11:53:39 BST 2018 by cgam1
}%
\@xx{}%
\end{tlatex}

\section{Model}
\paragraph{What is the Model}
The model defines the constant \textit{People}.
\begin{tla}
	People <- {"Alun", "Neil", "David", "Michael"}
\end{tla}
\begin{tlatex}
 \@x{\@s{32.8} People \.{\leftarrow} \{\@w{Alun} ,\,\@w{Neil} ,\,\@w{David}
 ,\,\@w{Michael} \}}%
\end{tlatex}

\paragraph{What is the Behaviour spec?}  The behaviour specification is given
by an \emph{Initial predicate and next-state relation}
\subparagraph{Init} \textit{Init}
\subparagraph{Next} \textit{Next}

\paragraph{Invariants} The invariants checked are :
\begin{table}[h]
\begin{tabular}{ll}
$\mathit{TypeOK}$ & The type invariant from the specification\\
$\forall p \in \mathit{register} : p \in \mathit{People}$ & every
registered person is in People.\\
\begin{tla}
register\subseteq People
\end{tla}
\begin{tlatex}
\@x{ register \.{\subseteq} People}%
\end{tlatex}
 & register is a subset of People\\
\begin{tla}
\A p \in out : p \in People
\end{tla}
\begin{tlatex}
\@x{ \A\, p \.{\in} out \.{:} p \.{\in} People}%
\end{tlatex}
 & Everyone outside the building is a person (see next invariant)\\
\begin{tla}
out \subseteq People
\end{tla}
\begin{tlatex}
\@x{ out \.{\subseteq} People}%
\end{tlatex}
 & out is a subset of people (says the same thing as the last invariant) \\
\begin{tla}
\A p \in in : p \in register
\end{tla}
\begin{tlatex}
\@x{ \A\, p \.{\in} in \.{:} p \.{\in} register}%
\end{tlatex}
 & everyone in the building is registered \\
\begin{tla}
in \subseteq register
\end{tla}
\begin{tlatex}
\@x{ in \.{\subseteq} register}%
\end{tlatex}
 & in is a subset of register \\
\end{tabular}
\end{table}

\section{Results}
A summary of the numbers of states found by the model checking is shown below.

\subsection{Statistics}
\paragraph{States found} for model as a whole

\begin{table}[h]
\begin{tabular}{lr}
	\hline
	States Found & 325 \\
	Distinct States & 81 \\
\hline
\end{tabular}
\end{table}

\paragraph{Number of next states found} for the actions is:

\begin{table}[h]
\begin{tabular}{lr}
	\hline
	\textbf{Action} & \textbf{States found} \\ \hline
	\textit{Init} (line 18) & 1 \\
	\textit{Register} (line 23) & 108 \\
	\textit{Enter} (line 29) & 108 \\
	\textit{Leave} (line 35) & 108 \\
	\hline
\end{tabular}
\end{table}

\section{Discussion}
The (simple) building model has three state variables; the register of users,
the list of people inside the building, and the list of those outside the
building.  The model has the set of people the specification applies to.

There is a redundancy in the state variables, and consistency is enforced by
the type invariant $\textit{register}=\textit{in}\cup\textit{out}$

\paragraph{The Next action } can be interpreted as follows.
\begin{quote}
	There is a person, who can either; be registered, or can enter the building,
	or can Leave the building.
\end{quote}

\begin{tla}
Next ==
    \exists p \in People :   \* There is a person who can either
        \/ Register(p)       \* be registered, or
        \/ Enter(p)          \* enter the building, or
        \/ Leave(p)          \* leave the building
\end{tla}
\begin{tlatex}
\@x{ Next \.{\defeq}}%
\@x{\@s{16.4} \exists\, p \.{\in} People \.{:}\@s{21.44}}%
\@y{%
  There is a person who can either
}%
\@xx{}%
\@x{\@s{32.8} \.{\lor} Register ( p )\@s{24.59}}%
\@y{%
  be registered, or
}%
\@xx{}%
\@x{\@s{32.8} \.{\lor} Enter ( p )\@s{36.89}}%
\@y{%
  enter the building, or
}%
\@xx{}%
\@x{\@s{32.8} \.{\lor} Leave ( p )\@s{19.13}}%
\@y{%
  leave the building
}%
\@xx{}%
\end{tlatex}
\end{document}


\section{Linear algebra}\label{sec:linear_algebra}

Linear algebra is a branch of mathematics that is both very accessible and enormously useful. It studies \hyperref[def:vector_space]{vector spaces}, mostly over \hyperref[def:set_of_real_numbers]{real} or \hyperref[def:set_of_complex_numbers]{complex} numbers, and \hyperref[def:semimodule/homomorphism]{linear maps} between them. For \hyperref[def:vector_space_dimension]{finite-dimensional} vector spaces, this reduces to studying \hyperref[def:array/matrix]{matrices}, which also leads to a very rich computational theory.

We have discussed the basics of vector spaces in \fullref{subsec:modules} in the context of general \hyperref[def:module]{modules over rings}. The key takeaways are:
\begin{itemize}
  \item Vector spaces, defined incrementally in \fullref{def:semimodule}, \fullref{def:module} and \fullref{def:vector_space}, with some properties listed in \fullref{thm:def:vector_space}.
  \item Linear combinations, defined in \fullref{def:free_semimodule} and characterized by \fullref{thm:free_semimodule_universal_property}, with important remarks in \fullref{rem:linear_combinations}.
  \item Linear spans, defined in \fullref{def:semimodule/submodel} and characterized via \fullref{thm:span_via_linear_combinations}.
  \item Linear maps, defined in \fullref{def:semimodule/homomorphism}, and multilinear maps, defined in \fullref{def:multilinear_function}.
  \item Quotient spaces, discussed in \fullref{def:module/quotient} for general modules.
  \item \Fullref{thm:quotient_module_universal_property} and \Fullref{thm:quotient_submodule_lattice_theorem}.
  \item Linear (in)dependence, defined in \fullref{def:linear_dependence} with some properties listed in \fullref{thm:def:linear_dependence}.
  \item Hames bases, defined in \fullref{def:hamel_basis} with some properties listed in \fullref{thm:def:hamel_basis}.
  \item Basis decomposition defined in \fullref{def:basis_decomposition}.
  \item Vector space dimensions, whose existence and uniqueness is shown in \fullref{thm:vector_space_dimension}.
  \item \Fullref{thm:rank_nullity_theorem}.
\end{itemize}


\pgfplotsset{
  layers/axis lines on top/.define layer set={
    axis background,
    axis grid,
    axis ticks,
    axis tick labels,
    pre main,
    main,
    axis lines,
    axis descriptions,
    axis foreground,
  }{/pgfplots/layers/standard},
}

\pgfkeys{
	/pgfplots/unit circle/.style={
		set layers=axis lines on top,
		width=9cm, height=9cm,
		axis x line=middle,
		axis y line=middle,
		xlabel=$x$,
		ylabel=$y$,
		every axis x label/.style={
			at={(ticklabel* cs:1.01)},
			anchor=west,
		},
		every axis y label/.style={
			at={(ticklabel* cs:1.01)},
			anchor=south,
		},
		axis line style={stealth-stealth, thick},
		label style={font=\Large},
		tick label style={font=\Large},
		samples=100,
		xmin=-1.25, xmax=1.25,
		ymin=-1.25, ymax=1.25,
		xtick={-1,0,1},
		ytick={-1,0,1},
		xticklabels={},
		yticklabels={},
		domain=-1.5:1.5,
		grid=both,
		major grid style={black!5},
	},
}

\section{Trigonometric functions}
\subsection{Basic Definitions}
Consider a \emph{right triangle} $\triangle ABC$ with sides $a,b$, and Hypotenuse $c$, where the angle $\angle ACB$ is $\ang{90}$, and the angle $\angle BAC$ is denoted as $\alpha$:

\centering
\begin{tikzpicture}[node distance=3mm]
		\coordinate (A) at (0,0);
		\coordinate (B) at (4,3);
		\coordinate (C) at (4,0);

		\node[left of=A] {$A$};
		\node[above of=B] {$B$};
		\node[right of=C] {$C$};

		\draw[fill=xblue!30] (A) -- node (c) [midway, above, rotate=36.87] {$c$ (Hypotenuse)} (B) -- node (a) [midway, right] {$a$ (Opposite)} (C) -- node (b) [midway, below] {$b$ (Adjacent)} cycle;
		\draw[thick] ($(C)+(0,0.3)$) rectangle ($(C)-(0.3,0)$);
		\draw[thick, xpurple!50!black, fill=xpurple!45] (A) -- ($(A)+(1,0)$) arc (0:36.87:1) node [midway, xshift=-3mm, yshift=-2pt] {$\alpha$} -- cycle;
		%\draw[thick, xblue!50!black, fill=xblue!45] (B) -- ($(B)+(0,-0.8)$) arc (270:216.97:0.8) node [midway, above, xshift=5pt] {$\beta$} -- cycle;
		\draw[thick] (A) -- (B) -- (C) -- cycle;
	\end{tikzpicture}
\flushleft

We use the ratios between the three sides of the triangle to define three functions of $\alpha$:
\vspace{5mm}
\begin{itemize}
	\item The \emph{sine} of the angle $\alpha$ is $\sin(\alpha)=\frac{a}{c}$,
	\item the \emph{cosine} of the angle $\alpha$ is $\cos(\alpha)=\frac{b}{c}$, and
	\item the \emph{tangent} of the angle $\alpha$ is $\tan(\alpha)=\frac{a}{b}$, which in turn is equal to $\frac{\sin(\alpha)}{\cos(\alpha)}$.
\end{itemize}

We can rearrange the above definitions:
\begin{align}
	a &= c\sin(\alpha),\nonumber\\
	b &= c\cos(\alpha).
	\label{eq:basic_trig_rearrange}
\end{align}

Normally, the Hypotenuse is the longest side of a right triangle. We will consider here the two edge cases where one of the sides $a$ or $b$ is equal to the Hypotenuse (and the other side is thus $0$):
\begin{itemize}
	\item if $a=c$ then $\alpha=\ang{90}$,\
	\item if $b=c$ then $\alpha=\ang{0}$.
\end{itemize}

The possible length of $a$ is therefore in the range $0\leq a \leq c$, which means that $0\leq \frac{a}{c} \leq 1$. Since $\sin(\alpha)=\frac{a}{c}$ this means that the image of $\sin(\alpha)$ is $[0,1]$. The same idea is also true for $b$, and therefore $[0,1]$ is the image of $\cos(\alpha)$ as well.

As a reminder, the \emph{Pythagorean theorem}\footnote{It's worth mentioning that no three positive integers $a, b$, and $c$ satisfy the equation $a^{n}+b^{n}=c^{n}$ for any integer value of $n>2$. \href{https://en.wikipedia.org/wiki/Fermat\%27s_Last_Theorem}{This can be proven, however the proof is too large to fit in the footnotes}.} states that for a right triangle with sides $a, b$ and $c$,
\begin{equation}
	a^{2} + b^{2} = c^{2}.
	\label{eq:pythagorean_theorem}
\end{equation}
By substituting \xref[eq]{basic_trig_rearrange} into the Pythagorean theorem we get
\begin{align*}
	c^{2} &= a^{2}+b^{2}\\
	&= \left[ c\sin(\alpha) \right]^{2} + \left[ c\cos(\alpha) \right]^{2}\\
	&= c^{2}\sin^{2}(\alpha) + c^{2}\cos^{2}(\alpha)\\
	&= c^{2}\left[ \sin^{2}(\alpha) + \cos^{2}(\alpha) \right],
\end{align*}
and therefore
\begin{equation}
	\sin^{2}(x) + \cos^{2}(x) = 1.
	\label{eq:sin sqr plus cos sqr equals 1}
\end{equation}

\subsection{The Unit Circle}
We defined $\sin(\alpha)$ and $\cos(\alpha)$ so far in way such that their domains are both $[\ang{0},\ang{90}]$, and their images are both $[0,1]$. However, there is a simple way to extend these functions such that both their domains are $\mathbb{R}$, and both their images are $[-1,1]$: by using a \emph{unit circle}.

\autoref{fig:unit_circle} depicts a unit circle: it is simply a circle of radius $R=1$, which is placed such that its center lies at the origin of a 2-dimensional axis system (i.e. at the point $\bm{O}=(0,0)$. We then draw a line from $\bm{O}$ to a point $\bm{P}=(x,y)$ on the circumference of the unit circle. We call the angle between the line $\bm{OP}$ and the $x$-axis $\theta$. We then draw another line, this time from the point $\bm{P}$ to a point $\bm{D}$ on the $x$-axis, such that $\bm{PD}$ is perpendicular to the $x$-axis.

The triangle $\triangle OPD$ is a right triangle. Therefore, we can use the trigonometric functions to calculate the coordinates of the point $\bm{P}=(x,y)$:
\begin{align}
	x &= R\cos(\theta) = \cos(\theta),\nonumber\\
	y &= R\sin(\theta) = \sin(\theta).
	\label{eq:xy_P}
\end{align}

We then define $\cos(\theta)$ and $\sin(\theta)$ as a function of $\theta$:
\begin{align}
	\sin(\theta) &= y,\nonumber\\
	\cos(\theta) &= x.
	\label{eq:unit circle definition of sin and cos}
\end{align}

Using this definition, the angle $\theta$ can take any value between $\ang{0}$ and $\ang{360}$. In fact, the values of $\theta$ can be extended to any real number in degrees: any real value of degrees is equivalent to some value in the range $[\ang{0},\ang{360}]$, the first and most obvious example is that $\ang{360}$ is equivalent to $\ang{0}$. Similarly, $\ang{-30}\equiv\ang{330}$, $-\ang{180}\equiv\ang{180}$, $-\ang{90}\equiv\ang{270}$, etc (see \autoref{fig:angles equivalency}). In fact, this property makes the trigonometric functions periodic, with a period $T=\ang{360}$.

\begin{figure}
	\centering
	\begin{tikzpicture}[every node/.style={font=\small}]
		\def\R{3}
		\draw[very thick, fill=xpurple!15] (0,0) circle (\R);
		\foreach \th in {0, 45, 90, 135, 180, 200, 270, 330}{
			\draw[ultra thick, densely dashed, white] (0,0) -- ({1.05*\R*cos(\th)},{1.05*\R*sin(\th)});
			\draw[very thick] ({0.95*\R*cos(\th)},{0.95*\R*sin(\th)}) -- ({1.05*\R*cos(\th)},{1.05*\R*sin(\th)});
			\node at ({1.075*\R*cos(\th)},{1.075*\R*sin(\th)}) (\th) {};
		}
		\fill[black] (0,0) circle (0.15);
		\node[right] at (0) {$\ang{0}\equiv\ang{360}\equiv\ang{720}\equiv\ang{-360}\equiv\dots$};
		\node[right] at (45) {$\ang{45}\equiv\ang{-315}\equiv\ang{395}\equiv\dots$};
		\node[right] at (90) {$\ang{90}\equiv\ang{-270}\equiv\ang{450}\equiv\dots$};
		\node[left] at (135) {$\ang{135}\equiv\ang{-225}\equiv\dots$};
		\node[left] at (180) {$\ang{180}\equiv\ang{-180}\equiv\ang{540}\equiv\dots$};
		\node[left] at (200) {$\ang{200}\equiv\ang{-160}\equiv\ang{560}\equiv\dots$};
		\node[right] at (270) {$\ang{270}\equiv\ang{-90}\equiv\dots$};
		\node[right] at (330) {$\ang{330}\equiv\ang{-30}\equiv\dots$};
	\end{tikzpicture}
	\caption{Angles equivalency on a circle.}
	\label{fig:angles equivalency}
\end{figure}

\subsection{Radians}
Using degrees to measure angles in a sphere creates an inconvenience: the domain and image of the trigonometric functions have different units. In order to measure both these magnitudes using the same unit we switch to measuring angles on a circle using \emph{radians} instead of degrees. One radian equals the length of a single radius $R$ of the circle (in the case of the unit circle this is always $R=1$). We define an inner angle $\theta$ to equal one radian if the arc length it represents is equal to $R$ (see \autoref{fig:radians}).

How much is a radian in degrees? The full circumference of any circle with radius $R$ equals $2\pi R$, which means that a single radian $R$ is equivalent to $\frac{\ang{180}}{\pi} \approx \ang{57.3}$. \autoref{tab:rad_degs} shows some common angles and their equivalent value in radians.

\begin{table}
	\caption{Common angles in radians, and their respective images for the three main trigonometric functions.}
	\label{tab:rad_degs}
	\centering
	\begin{tabular}{lcccc}
		\toprule
		\multicolumn{2}{c}{$\theta$} & $\sin(\theta)$ & $\cos(\theta)$ & $\tan(\theta)$\\
		degrees & radians & & &\\
		\midrule
		\ang{0}   & 0								 & $0$									& $1$									 & $0$\\
		\ang{30}  & $\frac{\pi}{6}$	 & $\frac{1}{2}$				& $\frac{\sqrt{3}}{2}$ & $\frac{1}{\sqrt{3}}$\\
		\ang{45}  & $\frac{\pi}{4}$  & $\frac{\sqrt{2}}{2}$ & $\frac{\sqrt{2}}{2}$ & $1$\\
		\ang{60}  & $\frac{\pi}{3}$  & $\frac{\sqrt{3}}{2}$ & $\frac{1}{2}$				 & $\sqrt{3}$\\
		\ang{90}  & $\frac{\pi}{2}$  & $1$									& $0$									 & undefined\\
		\ang{180} & $\pi$						 & $0$									& $-1$								 & $0$\\
		\ang{270} & $\frac{3\pi}{2}$ & $-1$									& $0$									 & undefined\\
		\ang{360} & $2\pi$					 & $0$									& $1$ 								 & $0$\\
		\bottomrule
	\end{tabular}
\end{table}

\begin{figure}
	\centering
	\begin{tikzpicture}[scale=0.9]
		\pgfmathsetmacro{\ax}{4.5}
		\pgfmathsetmacro{\un}{3.5}
		\pgfmathsetmacro{\th}{35}
		\coordinate (D) at ({\un*cos(\th)},0);

		\def\xcol{xred}
		\draw[thick, fill=\xcol!5] (A) circle (\un);
		\draw[vector, <->] (-\ax,0) -- (\ax,0) node [right] {\Large$x$};
		\draw[vector, <->] (0,-\ax) -- (0,\ax) node [above] {\Large$y$};
		\draw[ultra thick, \xcol, rotate=\th] (A) -- node [midway, above, rotate=\th] {$R=1$} (\un,0) node (B) {};
		\draw[very thick, densely dotted, black!50] (B.center) -- ({\un*cos(\th)},0);
		\draw[thick] ($(A)+(1.1,0)$) arc (0:\th:1.1) node [midway, xshift=-2mm, yshift=-2pt] {$\theta$};
		\draw[thick] ($(D)+(0,0.3)$) -- ++(-0.3,0) -- ++(0,-0.3);

		\filldraw (A) circle (2pt) node[below left] {$(0,0)=\bm{O}$};
		\filldraw (\un,0) circle (2pt) node[below right] {$(1,0)$};
		\filldraw (0,\un) circle (2pt) node[above right] {$(0,1)$};
		\filldraw (-\un,0) circle (2pt) node[below left] {$(-1,0)$};
		\filldraw (0,-\un) circle (2pt) node[below right] {$(0,-1)$};
		\filldraw (D) circle (2pt) node[below, anchor=north east, xshift=2mm] {$(x,0)=\bm{D}$};
		\filldraw (B) circle (2pt) node[right, xshift=1mm, yshift=1mm] {$\bm{P}=(x,y)$};
	\end{tikzpicture}
	\caption{A unit circle with a point $\bm{P}=(x,y)$ on its circumference. The triangle $\triangle \bm{OPD}$ is a right triangle with sides $\bm{OD}=x$, $\bm{OP}=y$ and an angle $\theta$ opposing the side $\bm{DP}$.}
	\label{fig:unit_circle}
\end{figure}

\begin{figure}
	\centering
	\begin{tikzpicture}[node distance=5mm, every node/.style={font=\large}]
		\begin{axis}[
			unit circle,
		]
			\def\xcol{xpurple}
			\def\sr{0.3}
			\def\rad{57.3}
			\coordinate (O) at (0,0);
			\coordinate (A) at (1,0);
			\coordinate (B) at ({cos(\rad)},{sin(\rad)});
			\coordinate (C) at ({\sr*cos(\rad)},{\sr*sin(\rad)});
			\coordinate (T) at ({\sr/1.5*cos(\rad/2)},{\sr/1.5*sin(\rad/2)});
			\coordinate (L1) at ({0.8*cos(\rad/2)},{0.8*sin(\rad/2)});
			\coordinate (L2) at ({0.35*cos(\rad/2)},{0.35*sin(\rad/2)});

			\draw[very thick, fill=\xcol!5] (A) arc (0:360:1);
			%\draw[line width=5mm, xpurple!75] (A) arc (0:\rad:1);
			\def\outershift#1{\raisebox{1ex}}
			\draw[very thick, fill=\xcol!50, postaction={decorate, decoration={text along path, text align=center, text={|\large\outershift|arc length=$R$}}}] (O) -- node[midway, above, rotate=\rad] {$R$} (B) arc (\rad:0:1) -- node[midway, below] {$R$} cycle;
			\def\innershift#1{\raisebox{-2.5ex}}
			\draw[postaction={decorate, decoration={text along path, text align=center, text={|\large\innershift|$1$ radians}}}] (B) arc (\rad:0:1);
			\draw[very thick, fill=\xcol!20] (O) -- (C) arc (\rad:0:\sr) -- cycle;

			\addplot+[only marks, black] coordinates {(0,0) (1,0) ({cos(\rad)},{sin(\rad)})};
			\node[below left of=O] {$\bm{O}$};
			\node[below right of=A] {$\bm{A}$};
			\node[above of=B] {$\bm{B}$};
			\node at (T) {$\theta$};
			\draw[vector, thin] (L1) -- (L2);
		\end{axis}
	\end{tikzpicture}
	\caption{In this figure the arc $\bm{AB}$ has the same length of the radii $\bm{OA}$ and $\bm{OB}$ (all are equal to $R$), and therefore $\theta=1$ radians.}
	\label{fig:radians}
\end{figure}

\subsection{Graphs}
As seen previously, the functions $\sin(x)$ and $\cos(x)$ are periodic, having both the period $T=2\pi$. Their graphs are depicted in \autoref{fig:sin and cos graphs}. The value of $\sin(x)$ is always equal to that of $\cos\left(x-\frac{\pi}{2}\right)$: we say that the two functions have a \emph{phase difference} of $\pi/2$. The graph of $\tan(x)$ is depicted in \autoref{fig:tan graph}.

\begin{figure}
	\centering
	\begin{tikzpicture}
		\begin{axis}[
				graph2d,
				width=15cm, height=4cm,
				xmin=-10, xmax=10,
				ymin=-1.2, ymax=1.2,
				xtick={-9.425,-7.854,...,9.425,10.996},
				xticklabels={$-3\pi$, $-\frac{5}{2}\pi$, $-2\pi$, $-\frac{3}{2}\pi$, $-\pi$, $-\frac{1}{2}\pi$, , $\frac{1}{2}\pi$, $\pi$, $\frac{3}{2}\pi$, $2\pi$, $\frac{5}{2}\pi$, $3\pi$},
				domain=-10:10,
			]
			\addplot[function, xred] {sin(deg(\x))};
			\addplot[function, xgreen] {cos(deg(\x))};
		\end{axis}
	\end{tikzpicture}
	\caption{The graphs of \textcolor{xred}{\bm{$\sin(x)$}} and \textcolor{xgreen}{\bm{$\cos(x)$}} for $x\in[-10,10]$. Note how the graph of \textcolor{xgreen}{$\bm{\cos(x)}$} is ``lagging'' behind the graph of \textcolor{xred}{$\bm{\sin(x)}$} by $\pi/2$.}
	\label{fig:tan graph}
\end{figure}

\begin{figure}
	\centering
	\begin{tikzpicture}
		\begin{axis}[
				graph2d,
				width=15cm, height=8cm,
				xmin=-10, xmax=10,
				ymin=-4, ymax=4,
				xtick={-9.425,-7.854,...,9.425,10.996},
				xticklabels={$-3\pi$, $-\frac{5}{2}\pi$, $-2\pi$, $-\frac{3}{2}\pi$, $-\pi$, $-\frac{1}{2}\pi$, , $\frac{1}{2}\pi$, $\pi$, $\frac{3}{2}\pi$, $2\pi$, $\frac{5}{2}\pi$, $3\pi$},
				domain=-10:10,
				restrict y to domain=-5:5,
				samples=500,
			]
			\addplot[function, xblue] {tan(deg(\x))};
		\end{axis}
	\end{tikzpicture}
	\caption{The graphs of $\tan(x)$ on the domain $[-3\pi,3\pi]$.}
	\label{fig:sin and cos graphs}
\end{figure}

\subsection{Identities}
The following are some useful facts and connections between trigonometric functions:
\begin{itemize}
	\item Pythagorean identity:
		\begin{equation}
			\sin^{2}(\theta) + \cos^{2}(\theta) = 1
		\end{equation}

	\item Symmetry/Antisymmetry:
		\begin{align}
			\sin(-\theta) &= -\sin(\theta).\\
			\cos(-\theta) &= \cos(\theta).\\
			\tan(-\theta) &= -\tan(\theta).
		\end{align}

	\item Tangent from sine and cosine:
		\begin{equation}
			\tan(\theta)=\frac{\sin(\theta)}{\cos(\theta)}.
		\end{equation}

	\item Phase between sine and cosine:
		\begin{align}
			\sin\left(\theta\pm\frac{\pi}{2}\right) &= \pm\cos(\theta).\\
			\cos\left(\theta\pm\frac{\pi}{2}\right) &= \mp\sin(\theta).
		\end{align}

	\item Half-period shift:
		\begin{align}
			\sin(\theta+\pi) &= -\sin(\theta).\\
			\cos(\theta+\pi) &= -\cos(\theta).
		\end{align}

	\item Angle sum:
		\begin{align}
			\sin(\alpha\pm\beta) &= \sin(\alpha)\cos(\beta)\pm\cos(\alpha)\sin(\beta).\\
			\cos(\alpha\pm\beta) &= \cos(\alpha)\cos(\beta)\mp\sin(\alpha)\sin(\beta).
		\end{align}

	\item Double angle:
		\begin{align}
			\sin(2\theta) &= 2\sin(\theta)\cos(\theta) = \frac{2\tan \left( \theta \right)}{1+\tan^{2} \left( \theta \right)}.\\
			\cos(2\theta) &= 1-2\sin^{2}(\theta) = \frac{1-\tan^{2} \left( \theta \right) }{1+\tan^{2} \left( \theta \right)}.
			\label{eq:tan_double_angles}
		\end{align}

	\item Half angle:
		\begin{align}
			\sin\left( \frac{\theta}{2} \right) &= \pm\sqrt{\frac{1-\cos(\theta)}{2}}.\\
			\cos\left( \frac{\theta}{2} \right) &= \pm\sqrt{\frac{1+\cos(\theta)}{2}}.\\
			\tan\left( \frac{\theta}{2} \right) &= \frac{\sin(\theta)}{1+\cos(\theta)}.
		\end{align}

	\item Product to sum:
		\begin{align}
			\sin(\theta)\sin(\varphi) &= \frac{1}{2}\left[ \cos(\theta-\varphi)-\cos(\theta+\varphi) \right].\\
			\cos(\theta)\cos(\varphi) &= \frac{1}{2}\left[ \cos(\theta-\varphi)+\cos(\theta+\varphi) \right].\\
			\sin(\theta)\cos(\varphi) &= \frac{1}{2}\left[ \sin(\theta+\varphi) + \sin(\theta-\varphi) \right].\\
			\tan(\theta)\tan(\varphi) &= \frac{\cos(\theta-\varphi)-\cos(\theta+\varphi)}{\cos(\theta-\varphi)+\cos(\theta+\varphi)}.
			\label{eq:trig product to sum}
		\end{align}

	\item Sum to product:
		\begin{align}
			\sin(\theta)\pm\sin(\varphi) &= 2\sin\left( \frac{\theta\pm\varphi}{2} \right)\cos\left( \frac{\theta\mp\varphi}{2} \right).\\
			\cos(\theta)+\cos(\varphi) &= 2\cos\left( \frac{\theta+\varphi}{2} \right)\cos\left( \frac{\theta-\varphi}{2} \right).\\
			\cos(\theta)-\cos(\varphi) &= -2\cos\left( \frac{\theta+\varphi}{2} \right)\cos\left( \frac{\theta-\varphi}{2} \right).\\
			\tan(\theta)\pm\tan(\varphi) &= \frac{\sin(\theta\pm\varphi)}{\cos(\theta)\cos(\varphi)}.
			\label{eq:trig sum to product}
		\end{align}
\end{itemize}

\subsection{Useful theorems}
The area $S$ of a triangle $\triangle ABC$ can be calculated using the length $L$ any side of the triangle (in this context called a \emph{base}) and the height $h$ to its opposing vertex (see \autoref{fig:area of a triangle}):
\begin{equation}
	S = \frac{1}{2}Lh.
	\label{eq:area of a triangle}
\end{equation}

\begin{figure}
	\centering
	\begin{tikzpicture}
		\coordinate (A) at (-2,1);
		\coordinate (B) at (1,3.5);
		\coordinate (C) at (2,1);
		\coordinate (D) at (B|-C);
		\draw[very thick, fill=xblue!20] (A) node[left] {$A$} -- (B) node[above] {$B$} node[midway, above left] {$c$} -- node[midway, right] {$a$} (C) node[right] {$C$} -- cycle node[midway, below] {$b$};
		\draw[thick, dashed] (B) -- (D) node[midway, left] {$h$};
		\draw[thick] (D) -- ++(0.2,0) -- ++(0,0.2) -- ++(-0.2,0);
		\pic[draw, thick, angle radius=11mm, angle eccentricity=0.7, "$\alpha$"] {angle=C--A--B};
	\end{tikzpicture}
	\caption{The area of a triangle using the side $b$ as a base, and its corresponding height to the point $B$. The angle opposing the side $A$ is marked as $\alpha$.}
	\label{fig:area of a triangle}
\end{figure}

The triangle with sides $cbh$ is a right triangle, $c$ being its hypotenuse. We can therefore infer the size of $h$ using $\alpha$:
\begin{equation}
	h = c\sin(\alpha).
	\label{eq:first equation in law of sines}
\end{equation}
Substituting this back to \autoref{eq:area of a triangle} yields that the area of the triangle is
\begin{equation}
	S = \frac{1}{2}bc\sin(\alpha).
	\label{eq:area using sin theta}
\end{equation}
There is nothing special about choosing the side $b$ as a base: we can also use $a$ or $c$ for the calculation. This will yield, respectively,
\begin{align}
	S &= \frac{1}{2}ac\sin(\gamma),\\
	S &= \frac{1}{2}ac\sin(\beta),
	\label{eq:}
\end{align}
where $\beta$ is the angle opposing $b$ and $\gamma$ is the angle opposing $c$. Since $S$ is the same in all cases, we simply multiply each of the area equations by $2$ and divide by $abc$, which yields
\begin{equation}
	\frac{\sin(\alpha)}{a} = \frac{\sin(\beta)}{b} = \frac{\sin(\gamma)}{c},
	\label{eq:law of sines}
\end{equation}
i.e. in a triangle, the ratio between any side and the sine of its opposing angle is always the same no matter which side we choose. This theorem is called the \emph{law of sines}.

\begin{example}{Law of sines}{}
	Given the triangle $\triangle ABC$ below, what are $\beta$ and $b$?

	\vspace{2em}
	\centering
	\begin{tikzpicture}
		\coordinate (A) at (2,-1);
		\coordinate (B) at (2,3);
		\coordinate (C) at (-1,4);
		% a=3.15, b=5.83, c=4
		% α=30.79 β=108.67 γ=40.54
		\draw[very thick, fill=xpurple!40] (A) -- (B) node[midway, right] {$c$} -- node[midway, above right] {$a=3.15$} (C) -- cycle node[midway, below left] {$b$};

		% "local bounding box" gives the pic its label
		\pic[local bounding box=alpha, draw, thick, angle radius=11mm, angle eccentricity=0.7, "$\alpha$"] {angle=B--A--C};
		\pic[local bounding box=beta, draw, thick, angle radius=7mm, angle eccentricity=0.6, "$\beta$"] {angle=C--B--A};
		\pic[local bounding box=gamma, draw, thick, angle radius=11mm, angle eccentricity=0.7, "$\gamma$"] {angle=A--C--B};

		\node[above right of=alpha, xshift=2mm] (alphatxt) {$\ang{30.79}$};
		\node[below left of=gamma, xshift=-2mm] (gammatxt) {$\ang{40.54}$};
		\draw[vector, thick] (alphatxt.south) to [out=-90, in=0] (alpha);
		\draw[vector, thick] (gammatxt.north) to [out=90, in=180] (gamma);
	\end{tikzpicture}

	\vspace{1em}
	\flushleft
	Since all angles in a triangle must add up to $\ang{180}$,
	\[
		\beta=\ang{180}-\ang{30.79}-\ang{40.54}=\ang{108.67}.
	\]

	Using the law of sines,
	\[
		b = \frac{a}{\sin(\alpha)}\cdot\sin(\beta) = \frac{3.15}{\sin(\ang{30.79})}\cdot\sin(\ang{108.67}) \approx 5.83,
	\]
	and
	\[
		c = \frac{a}{\sin(\alpha)}\cdot\sin(\gamma) = \frac{3.15}{\sin(\ang{30.79})}\cdot\sin(\ang{40.54}) \approx 4.
	\]
\end{example}

\begin{note}{Ambiguity of solutions}{}
	The above example reveals an issue that might arise due to the symmetrical nature of $\sin(x)$ around $x=\pi$ ($\ang{180}$): say we wanted to calculate $\beta$ using the law of sines instead of by using $\beta=\ang{180}-\alpha-\gamma$. In this case we would solve the equation
	\[
		\frac{\sin(\alpha)}{a} = \frac{\sin(\beta)}{b},
	\]
	which would result in $\beta=\arcsin\left( \frac{b\sin(\alpha)}{a}  \right)=\arcsin\left( 0.95 \right)$. However, two angles can fit this requirement: the sines of $\ang{71.34}$ and $\ang{108.67}$ are both equal to $0.95$! Therefore, we must be careful when using the law of sine and make sure we always choose values that make sense (e.g. such that all angles add up to $\ang{180}$).
\end{note}

Of course, the sine function is not unique in having its own named ``Law'': another useful theorem is the so-called \emph{law of cosines} (also \emph{al-Kashi's theorem}). This theorem states that given a triangle with sides $a,b,c$ and an angle $\gamma$ opposing $c$,
\begin{equation}
	c^{2} = a^{2} + b^{2} - 2ab\cos(\gamma).
	\label{eq:law of cosines}
\end{equation}

Much like the law of sines, the choice of angle does not matter, as long as we plug the correct sides to the equation: for $\alpha$ and $\beta$ being the angles opposing to $a$ and $b$ respectively,
\begin{align}
	a^{2} &= b^{2} + c^{2} - 2bc\cos(\alpha),\nonumber\\
	b^{2} &= a^{2} + c^{2} - 2ac\cos(\beta).
\end{align}

If the triangle in question is a right triangle then one of the angles is equal to $\ang{90}$. Without loss of generality, let us assume that this is $\gamma$. Since $\cos(\ang{90})=0$ we get that in the case of a right triangle
\begin{equation}
	c^{2} = a^{2} + b^{2},
	\label{eq:pythagorean theorem from law of cosines}
\end{equation}
i.e. we retrieve back the Pythagorean theorem.

\begin{example}{Law of cosines}{}
	Calculate all angles in the following triangle:

	\vspace{2em}
	\centering
	\begin{tikzpicture}
		\coordinate (A) at (-3,0);
		\coordinate (B) at (2,-1);
		\coordinate (C) at (-1,3);
		% a=5 b=3.61 c=5.1
		% α=70.55 β=67.58 γ=41.87
		\draw[very thick, fill=xgreen!50] (A) -- (B) node[midway, below left] {$c=5.1$} -- node[midway, above right] {$a=5$} (C) -- cycle node[midway, above left] {$b=3.61$};

		% "local bounding box" gives the pic its label
		\pic[local bounding box=alpha, draw, thick, angle radius=9mm, angle eccentricity=0.6, "$\alpha$"] {angle=B--A--C};
		\pic[local bounding box=beta, draw, thick, angle radius=11mm, angle eccentricity=0.7, "$\beta$"] {angle=C--B--A};
		\pic[local bounding box=gamma, draw, thick, angle radius=9mm, angle eccentricity=0.6, "$\gamma$"] {angle=A--C--B};
	\end{tikzpicture}

	\vspace{1em}
	\flushleft
	Using the law of cosines:
	\begin{align*}
		\cos(\gamma) &= \frac{c^{2}-b^{2}-a^{2}}{-2ab} = \frac{5.1^{2}-3.61^{2}-5^{2}}{-2\cdot5\cdot3.61} \approx 0.33302\Rightarrow \gamma=\ang{70.54},\\
		\cos(\beta) &= \frac{b^{2}-a^{2}-c^{2}}{-2ac} = \frac{3.61^{2}-5^{2}-5.1^{2}}{-2\cdot5\cdot5.1} \approx 0.74466\Rightarrow \beta=\ang{41.87},\\
		\cos(\alpha) &= \frac{a^{2}-b^{2}-c^{2}}{-2cb} = \frac{5^{2}-3.61^{2}-5.1^{2}}{-2\cdot5.1\cdot3.61} \approx 0.38135\Rightarrow \alpha=\ang{67.58}.
	\end{align*}
\end{example}

%\begin{figure}
%	\centering
%	\begin{tikzpicture}
%		\pgfmathsetmacro{\ax}{4.5}
%		\pgfmathsetmacro{\un}{3.5}
%		\pgfmathsetmacro{\th}{35}
%		\coordinate (D) at ({\un*cos(\th)},0);
%
%		\filldraw[xred!35] (A) -- (\un,0) arc (0:90:\un);
%		\filldraw[xblue!35] (A) -- (0,\un) arc (90:180:\un);
%		\filldraw[xgreen!35] (A) -- (-\un,0) arc (180:270:\un);
%		\filldraw[xorange!35] (A) -- (0,-\un) arc (270:360:\un);
%		\node at ({ \un/2.5},{ \un/2.5}) {\Huge$1$};
%		\node at ({-\un/2.5},{ \un/2.5}) {\Huge$2$};
%		\node at ({-\un/2.5},{-\un/2.5}) {\Huge$3$};
%		\node at ({ \un/2.5},{-\un/2.5}) {\Huge$4$};
%
%		\draw[thick] (A) circle (\un);
%		\draw[vector, <->] (-\ax,0) -- (\ax,0) node [right] {\Large$x$};
%		\draw[vector, <->] (0,-\ax) -- (0,\ax) node [above] {\Large$y$};
%		\filldraw (A) circle (2pt) node[below right] {$(0,0)$};
%		\filldraw (\un,0) circle (2pt) node[below right] {$(1,0)$};
%		\filldraw (0,\un) circle (2pt) node[above right] {$(0,1)$};
%		\filldraw (-\un,0) circle (2pt) node[below left] {$(-1,0)$};
%		\filldraw (0,-\un) circle (2pt) node[below right] {$(0,-1)$};
%	\end{tikzpicture}
%	\caption{The different quadrants of the unit circle.}
%	\label{fig:unit_circle_quadrants}
%\end{figure}

%\begin{table}
%	\caption{Text}
%	\label{tab:quadrants_trig_vals}
%	\centering
%	\begin{tabular}{lll}
%		\toprule
%		Quadrant & $\cos(\theta)=x$ & $\sin(\theta)=y$\\
%		\midrule
%		\rowcolor{xred!35}1 & $\left[ 0,1 \right]$ & $\left[ 0,1 \right]$\\
%		\rowcolor{xblue!35}2 & $\left[-1,0 \right]$ & $\left[ 0,1 \right]$\\
%		\rowcolor{xgreen!35}3 & $\left[-1,0 \right]$ & $\left[-1,0 \right]$\\
%		\rowcolor{xorange!35}4 & $\left[ 0,1 \right]$ & $\left[-1,0 \right]$\\
%		\bottomrule
%	\end{tabular}
%\end{table}


\subsection{Estimating infinite state Markov chains}

We can represent the transition matrix as a series of rules to reduce the number of dimensions

\(P(x_t |y_{t-1})=f(x,y)\)

can represent states as number, rather than atomic. could be continuous, or even real.

in more complex, can use vectors.