% !TEX root = thesis.tex

\chapter{Extracting information from higher-dimensional models}
\label{ch:slicing}

The previous chapters have discussed various aspects of higher-dimensional representations and operations.
As powerful as they can be, these representations and operations are generally incompatible with current 2D/3D software and are also hard to imagine and visualise.
In order for them to be used in practice, it is therefore important to have methods to extract meaningful 2D and 3D subsets from a higher-dimensional model.

While fully developing methods and algorithms to do so is outside the scope of this thesis, steps towards formalising this problem and looking for potential solutions were made within the context of this thesis, and they are thus documented here.
The chapter starts by giving some of the background behind the problem in \refse{se:slicing-notions}, explaining at a high level the process to extract lower-dimensional information from a higher-dimensional model.
Using a simple camera analogy common in computer graphics, \refse{se:3dto2d} explains how a 3D viewpoint is placed in a 3D scene through the application of different transformations, capturing a 2D view of it by applying a given projection.
\refse{se:4dto3d} uses this analogy to give an intuitive description of how this process extends to higher dimensions, later formulating a simple dimension-independent generalisation of the orthographic and perspective projection methods and explaining other projection possibilities.
Finally, \refse{se:slicing-conclusions} concludes with some ideas on the value of these methods, what pieces are still missing and how the overall process could be implemented.

\section{Background}
\label{se:slicing-notions}

The process to obtain a lower-dimensional subset of a higher-dimensional dataset can be regarded as a function that maps a subset of $\mathbb{R}^n$ to a subset of $\mathbb{R}^m$, $m < n$, which is obtained by cutting through the dataset in a geometrically meaningful way.
For instance, it is possible to cut orthogonally to an axis for a snapshot at one point in space/time, or obliquely for a subset that combines different parametrised characteristics, such as the evolution of different parts of an area in time.

The process to extract these slices can be conceived as consisting of two broad steps: (i) optionally selecting a subset of the objects in the scene, and (ii) applying a transformation that projects this subset to a lower dimension.
In computer graphics, these steps would normally be followed by \emph{rendering} the selected and projected objects, obtaining a raster image as an end-product.

\textbf{Selecting} a subset of a dataset is useful to bring certain parts of the dataset into view.
For instance, this is commonly applied to parts of a dataset that are normally not visible, such as the interior of an object.
It is also widely used to reduce the difficulty of the overall problem and avoiding the need to make unnecessary computations, such as projecting and rendering parts of the dataset that will be clearly out of view.

This selection process can be relatively simple, such as using all objects that are within a certain bounding box, that are within a certain distance of a point, or being of a given dimension.
For instance, 3D rendering programs and 3D games will usually only render the 2D faces of the objects lying (approximately) within a region (\eg\ frustum), not their (filled-in) volumes or any 2D faces that are clearly out of view.

However, more complex selection processes are useful in many instances, often requiring specialised data structures and geometric algorithms.
In particular, a great number of visibility determination algorithms have been developed \citep[Ch.~36]{Hughes14}, which solve various approximations of the surfaces that are visible in a scene.
Another example is a selection process is the computation of cross-sections of volumetric 3D objects, which requires the computation of a Boolean set intersection of a point set with a plane, such as the typical \emph{conic sections} shown in \reffig{fig:conic}.

\begin{figure*}[tb]
\centering
\subfloat[]{\includegraphics[width=0.25\linewidth]{figs/conic-circle}}
\subfloat[]{\includegraphics[width=0.25\linewidth]{figs/conic-ellipse}}
\subfloat[]{\includegraphics[width=0.25\linewidth]{figs/conic-parabola}}
\subfloat[]{\includegraphics[width=0.25\linewidth]{figs/conic-hyperbola}}
\caption[Conic sections]{Four types of conic sections: (a) circle, (b) ellipse, (c) parabola and (d) hyperbola.
These are obtained by the Boolean set intersection of a pair of cones with a plane.}
\label{fig:conic}
\end{figure*}

\textbf{Projecting} the selected subset yields a given view of the scene and reduces the actual dimension of the objects being represented.
Many types of projections for this purpose can be defined, usually in the form of a transformation that is applied to the objects.
For example, orthographic and perspective projections (\reffig{fig:scheduling}) can directly map an $n$D scene to an $m$D subspace.
However, more complex schemes are also possible, such as projecting first inwards/outwards to an $m$-sphere (for a hypothetical $(m+1)$D Nef polyhedra implementation), then to an $m$D subspace (\eg\ using an equiangular projection as shown in \reffig{fig:ioh-equiangle}).

\begin{figure}[tb]
\centering
\subfloat[]{\includegraphics[width=0.4\linewidth]{figs/scheduling-orthographic}}
\qquad
\subfloat[]{\includegraphics[width=0.4\linewidth]{figs/scheduling-perspective}}
\caption[Orthographic and perspective projections]{The (a) orthographic projection projects objects orthogonally to the projection plane.
The (b) perspective projection projects objects in manner such that those that are nearby appear comparatively larger than farther away.
Note how parallel lines (\eg\ wall edges) remain parallel in the former but not in the latter.}
\label{fig:scheduling}
\end{figure}

\begin{figure}[tb]
\centering
\includegraphics[width=\linewidth]{figs/ioh-equiangle}
\caption[Equirectangular projection]{The Equirectangular projection directly maps angles to coordinates.
A 360$^\circ$ view is here mapped into a rectangular 180$^\circ\times$360$^\circ$ image.
Rendered from a viewpoint inside the IfcOpenHouse dataset\protect\footnotemark.}
\label{fig:ioh-equiangle}
\end{figure}
\footnotetext{\url{http://blog.ifcopenshell.org/2012/11/say-hi-to-ifcopenhouse.html}}

Many of these projections can be done with techniques that are closely related to computer graphics, which has been dealing with the case of converting a 3D scene into a 2D image for decades, and less frequently covering higher-dimensional cases as well.
In this sense, it is useful to consider the same camera analogy that is often used in computer graphics, where a movable camera\footnote{Even if the movable camera is implemented by moving the dataset instead.} captures a scene as seen from a particular viewpoint.
In a higher-dimensional setting, this analogy would consist of an $m$D camera capturing an ($m$D) view of an $n$D scene.
While this is somewhat more difficult to imagine, this analogy emphasises three important aspects that remain true in any dimension:

\begin{itemize}
\item
Moving the camera around a scene and orienting it makes it possible to capture the data from any given viewpoint.
Such a viewpoint can be parametrised---and thus easily defined and stored---by a set of values containing its \emph{location} and \emph{orientation}.

\item
It is possible to obtain different views---intuitively corresponding to camera lenses---through various projections.
These can also have their own customisable parameters, such as their field of view.

\item
Despite the fact that the dimension of the data is reduced (from $n$ to $m$), characteristics that would seem to have been lost in this process can be preserved in the form of attributes and used for further computations.
For instance, in computer graphics, the distance from an object to the camera (\ie\ the depth) is regularly used for computations of 3D-to-2D projections, either as a simple computation where the objects that are nearer (along a line of sight) occlude those that are farther away, or for more complex effects, such as transparency and reflectivity.
In cartography, hypsometric tints, shaded relief, contour lines are all frequently used to encode the height information in a 2D map (\reffig{fig:wigwam}).
\end{itemize}

\marginpar{
\captionsetup{type=figure}
\centering
\includegraphics[width=\marginparwidth]{figs/wigwam}
\caption[Grand Teton National Park map]{An excerpt of the map for the Grand Teton National Park. From \citet{Patterson02}.}
\label{fig:wigwam}
}

Continuing with the camera analogy, it is useful to consider the $n$D scene as consisting of a set of 0D--$n$D objects.
In the context of this thesis, these would be represented as an $n$-dimensional simplicial complex or cell complex with linear geometries, such that every vertex in the map is embedded in a location in $\mathbb{R}^n$.
The simplices/cells of the complex can be translated, rotated or scaled, if necessary, using the operations described in \refse{se:ndmath}.
They can then be individually projected by the camera, possibly taking into account occlusion or other visual effects, in order to obtain the $m$D scene.

This projection is simplest when two conditions are met: (i) the objects have linear geometries that are stored as coordinates attached to their vertices (as in this thesis), and (ii) the projection transformation preserves the linearity of the objects.
In this case, it is only necessary to apply the transformation to every vertex individually.

However, even when one or both of these conditions are not met, it is possible to obtain a good approximation by first subdividing a simplex/cell into small simplices/cells (up to an arbitrary $\varepsilon$ threshold), assuming that linear geometries will remain approximately linear and projecting only the vertices of the subdivided complex.
This subdivision and approximation method is used for the cover of this thesis and the example in \reffigp{fig:4dhouse}.

The following sections will therefore assume that the data to be projected consists only of a set of $k$ points with $n$ coordinates, which is stored as a $k \times n$ matrix.
These will be given new coordinates in $\mathbb{R}^m$ by applying one or more operations expressed in terms of linear algebra.

\section{3D to 2D projections}
\label{se:3dto2d}

Projecting a set of 3D objects into a 2D view is one of the most common tasks in computer graphics.
As such, most computer graphics books derive their own versions of the transformations required to apply different types of projections, most of which are interchangeable but are often parametrised in different ways.

From a practical perspective, the OpenGL Programming Guide (better known as the Red Book) \citep{Shreiner13} provides very intuitive forms of 3D-to-2D perspective and orthographic projections, which are based on defining a \emph{viewing frustum} or \emph{box} where the objects to be viewed should be located/placed.
This can be achieved by translating, rotating and scaling the objects as described in \refse{se:ndmath}.

Assuming a setup as shown in \reffig{fig:frustum}, where a camera is placed at $z = 0$ and pointing towards higher values on the $z$ axis, with parameters defining the $x$ coordinates for the left ($l$) and right ($r$), $y$ coordinates for the bottom ($b$) and top ($t$), and $z$ coordinates for the near ($n$) and far ($f$) planes, \citet[Ch.~5]{Shreiner13} define a transformation that applies a perspective projection $P_p$ and an orthographic projection $P_o$ as:

\begin{figure}[tb]
\centering
\subfloat[]{\includegraphics[width=0.3\linewidth]{figs/frustum}}
\qquad
\subfloat[]{\includegraphics[width=0.4\linewidth]{figs/ortho}}
\caption[Perspective projection's frustum and orthographic projection's box]{The left ($l$), right ($r$), bottom ($b$), top ($t$), near ($n$) and far ($f$) planes that form (a) a perspective projection's frustum and (b) an orthographic projection's box.}
\label{fig:frustum}
\end{figure}

\begin{align*}
P_p &= \begin{bmatrix}
\frac{2n}{r-l} & 0 & \frac{2(l+r)}{r-l} & 0 \\
0 & \frac{2n}{t-b} & \frac{2(t+b)}{t-b} & 0 \\
0 & 0 & \frac{f+n}{n-f} & \frac{-2fn}{f-n} \\
0 & 0 & -1 & 0 \\
\end{bmatrix} &
P_o &= \begin{bmatrix}
\frac{2}{r-l} & 0 & 0 & \frac{l+r}{l-r} \\
0 & \frac{2}{t-b} & 0 & \frac{b+t}{b-t} \\
0 & 0 & \frac{-2}{f-n} & \frac{n+f}{n-f} \\
0 & 0 & 0 & 1 \\
\end{bmatrix}
\end{align*}

However, as this thesis attempts to obtain a dimension-independent formulation, it is more interesting to consider a somewhat more complex method that can be extended more readily to higher dimensions.
\citet{Foley92} and \citet[Ch.~13]{Hughes14}, among others, start from the definition of a triplet of vectors $\hat{x}$, $\hat{y}$ and $\hat{z}$, which are computed from a point $from$ where the camera is located, a point $to$ that the camera directly points towards, and an arbitrary vector $\overrightarrow{up}$ pointing approximately upwards\footnote{Strictly, it can be defined pointing toward any direction except along the $z$ axis.}.
The latter vector is necessary because there is still one degree of rotational freedom even as the camera is pointing towards $to$.
The unit vectors $\hat{x}$, $\hat{y}$ and $\hat{z}$ correspond to the $x$, $y$ and $z$ axes from the camera's perspective and are defined as:

% TODO: Figure for vectors

\begin{align*}
\hat{x} &= \frac{\hat{z} \times \overrightarrow{up}}{\left\lVert{}\hat{z} \times \overrightarrow{up}\right\rVert} &
\hat{y} &= \hat{x} \times \hat{z} &
\hat{z} &= \frac{to-from}{\left\lVert{}to-from\right\rVert}
\end{align*}

Note that if $\overrightarrow{up}$ is orthogonal to $\hat{z}$, $\hat{y}$ defines the up direction as a normalised $\overrightarrow{up}$.
If they are not orthogonal, $\hat{y}$ defines the up direction differently from $\overrightarrow{up}$.

Based on the three vectors $\hat{x}$, $\hat{y}$ and $\hat{z}$, a $n \times 3$ matrix of $n$ point coordinates in terms of the scene $P_\mathrm{world}$ (world coordinates) can be converted into a matrix of points coordinates in terms of the camera (eye coordinates).
This would be computed as:

\begin{equation*}
P_\mathrm{eye} = \begin{bmatrix} P_\mathrm{world} - from \end{bmatrix}
\begin{bmatrix} \hat{x} & \hat{y} & \hat{z} \end{bmatrix}
\end{equation*}

In a 3D to 2D \textbf{orthographic projection}, the $x$ and $y$ coordinates of this matrix, here denoted as $x_\mathrm{eye}$ and $y_\mathrm{eye}$, are directly usable as the coordinates of the points in $\mathbb{R}^2$.
As shown in \reffig{fig:trig-ortho}, the $z_\mathrm{eye}$ coordinates represent the distance between the point and the \emph{projection plane}, \ie\ the camera, and thus define the \emph{depth}.
Points with positive $z_\mathrm{eye}$ values close to zero are thus close to the camera while points with larger $z_\mathrm{eye}$ values are farther away.
Note that negative $z_\mathrm{eye}$ values mean that the point lies behind the camera, and so these would be usually omitted.

\marginpar{
\captionsetup{type=figure}
\centering
\includegraphics[width=\marginparwidth]{figs/trig-ortho}
\caption[Geometry of an orthographic projection]{The geometry of an orthographic projection for a point $p$. The situation of the $y$ axis is identical to that of the $x$ axis.}
\label{fig:trig-ortho}
}

For visualisation purposes, it is often convenient to normalise the coordinates so that the range of the $x$ and $y$ coordinates extend as much as possible along a given frame, resulting in objects that are not too small or too large.
% For this, the the interval $[-1, 1]$ is commonly used.
This can be accomplished by finding the point that is farthest along the $\hat{x}$ and $\hat{y}$ compared to the desired aspect ratio of the frame in which they should fit.
As \citet{Foley92} point out, in the case of the square $[-1, 1] \times [-1, 1]$, which is commonly used, point coordinates can be normalised by dividing them by the longest distance of any point to the $to$ point.

In a 3D to 2D \textbf{perspective projection}, objects are projected towards a point (the camera viewpoint's coordinates) rather than a plane.
This results in new $x_\mathrm{pers}$ and $y_\mathrm{pers}$ coordinates that are scaled inwards in inverse proportion to the depth, which is here defined as the distance between a point and the camera viewpoint's coordinates.
Intuitively, this means that if an object is $n$ times farther than another identical object, it is depicted $n$ times smaller, or $\frac{1}{n}$ of its size.

\marginpar{
\captionsetup{type=figure}
\centering
\includegraphics[width=\marginparwidth]{figs/trig-pers}
\caption[Geometry of an perspective projection]{The geometry of a perspective projection for a point $p$. The situation of the $y$ axis is identical to that of the $x$ axis.}
\label{fig:trig-pers}
}

As shown in \reffig{fig:trig-pers}, this distance can be easily computed as the hypotenuse of a right-angled triangle, where the adjacent cathetus of an angle $\vartheta$ is given by the (orthogonal) distance of the point to the projection plane ($z_\mathrm{eye}$), the opposite cathetus is given by the point's world coordinates ($x_\mathrm{eye}$ or $y_\mathrm{eye}$), and $\vartheta$ is the viewing angle between $\hat{z}$ (which lies on a line that passes through $from$ and $to$) and the line between the $to$ point and the current point.
New $x_\mathrm{pers}$ and $y_\mathrm{pers}$ coordinates are thus computed using this angle as follows:

\begin{align*}
x_\mathrm{pers} &= \frac{x_\mathrm{eye}}{z_\mathrm{eye} \tan(\vartheta / 2)} &
y_\mathrm{pers} &= \frac{y_\mathrm{eye}}{z_\mathrm{eye} \tan(\vartheta / 2)} \\
\end{align*}

There are many other types of projections that can be defined and can be interesting in higher dimensions, such as the equirectangular projection shown in \reffig{fig:ioh-equiangle} where evenly spaced angles along a \emph{rotation plane} (\refse{se:ndmath}) can be directly converted into evenly spaced coordinates.
However, they will not be discussed here.
See \citet[Chs.~5--7]{Salomon11} for a good reference on how to apply many different types of linear and non-linear projections.

\section{Higher-dimensional projections}
\label{se:4dto3d}

Based on the 3D to 2D projection methods described by \citet{Foley92}, \citet{Hollasch91} extends them to perform 4D to 3D orthographic and perspective projections.
This section further extends these methods to describe the $n$-dimensional to ($n-1$)-dimensional case, changing some aspects to better explain the geometric meaning of each vector.

First, starting from a point $from \in \mathbb{R}^n$ where the camera is located, a point $to \in \mathbb{R}^n$ that the camera directly points towards, and a set of $n-2$ vectors $\overrightarrow{v}_1, \ldots, \overrightarrow{v}_{n-2}$ in $\mathbb{R}^n$ that are all linearly independent from each other and from the vector $to - from$, it is possible to define a set of unit vectors $\hat{x}_0, \ldots, \hat{x}_{n-1}$ that define the axes $x_0, \ldots, x_{n-1}$ of a coordinate system in $\mathbb{R}^n$ as:

% \hspace{-\marginparwidth}\hspace{-\marginparsep}\makebox[\overflowingheadlen][l]{
% \begin{minipage}{\overflowingheadlen}
\begin{align*}
\hat{x}_0 &= \frac{\overrightarrow{v}_1 \times \cdots \times \overrightarrow{v}_{n-2} \times \hat{x}_{n-1}}{\begin{Vmatrix} \overrightarrow{v}_1 \times \cdots \times \overrightarrow{v}_{n-2} \times \hat{x}_{n-1} \end{Vmatrix}} \\
\hat{x}_i &= \frac{\overrightarrow{v}_{i+1} \times \cdots \times \overrightarrow{v}_{n-2} \times \hat{x}_{n-1} \times \hat{x}_0 \times \cdots \times \hat{x}_{i-1}}{\begin{Vmatrix} \overrightarrow{v}_{i+1} \times \cdots \times \overrightarrow{v}_{n-2} \times \hat{x}_{n-1} \times \hat{x}_0 \times \cdots \times \hat{x}_{i-1} \end{Vmatrix}}, \mathrm{\quad{}for\ } 0 < i < n-2 \\
\hat{x}_{n-2} &= \hat{x}_{n-1} \times \hat{x}_0 \times \cdots \times \hat{x}_{n-2} \\
\hat{x}_{n-1} &= \frac{to - from}{\begin{Vmatrix} to - from \end{Vmatrix}} \\
\end{align*}
% \end{minipage}
% }

The vector $\hat{x}_{n-1}$ is the first that needs to be computed and is oriented along the line from the camera ($from$) and the point that it is oriented towards ($to$).
Afterwards, the vectors are computed in order from $\hat{x}_0$ to $\hat{x}_{n-2}$ as normalised $n$-dimensional cross products of $n-1$ vectors.
These contain a mixture of the input vectors $\overrightarrow{v}_1, \ldots, \overrightarrow{v}_{n-2}$ and the computed unit vectors $\hat{x}_0, \ldots, \hat{x}_{n-1}$, starting from $n-2$ input vectors and one unit vector for $\hat{x}_0$, and removing one input vector and adding the previously computed unit vector for the next $\hat{x}_i$ vector.
Note that if $\overrightarrow{v}_1, \ldots, \overrightarrow{v}_{n-2}$ and $\hat{x}_{n-1}$ are all orthogonal to each other, $\forall 0 < i < n-1$, $\hat{x}_i$ is simply a normalised $\overrightarrow{v}_i$.

Similarly to the 3D-to-2D case, the vectors $\hat{x}_0, \ldots, \hat{x}_{n-1}$ can be used to transform an $m \times n$ matrix of $m$ $n$D points in world coordinates $P$ into an $m \times n$ matrix of $m$ $n$D points in eye coordinates $E$ by applying the following transformation:

\begin{equation*}
E = \begin{bmatrix} P - from \end{bmatrix}
\begin{bmatrix} \hat{x}_0 & \cdots & \hat{x}_{n-1} \end{bmatrix}
\end{equation*}

As before, if $E$ has rows of the form $\begin{bsmallmatrix} e_0 & \cdots & e_{n-1} \end{bsmallmatrix}$ representing points, $e_0, \ldots, e_{n-2}$ are directly usable as the coordinates in $\mathbb{R}^{n-1}$ of the projected point in an $n$-dimensional to ($n-1$)-dimensional \textbf{orthographic projection}, while $e_{n-1}$ represents the distance between the point and the projection ($n-1$)-dimensional subspace, which can be used for visual cues\footnote{Visual cues can still be useful in higher dimensions. See \url{http://eusebeia.dyndns.org/4d/vis/08-hsr}.}.
The coordinates along $e_0, \ldots, e_{n-2}$ could be made to fit within a certain bounding box by computing their extent along each axis, then scaling appropriately using the extent that is largest in proportion to the extent of the bounding box's corresponding axis.

For an $n$-dimensional to ($n-1$)-dimensional \textbf{perspective projection}, it is only necessary to compute the distance between a point and the camera as the hypotenuse of a right-angled triangle by taking into account the viewing angle $\vartheta$ between $\hat{x}_{n-1}$ and the line between the $to$ point and every point.
This situation is the same as that of the 3D-to-2D case shown previously in \reffig{fig:trig-pers} and results in new $e_0^\prime, \ldots, e_{n-2}^\prime$ coordinates that are shifted inwards.
The coordinates are computed as:

\begin{equation*}
e_i^\prime = \frac{e_i}{e_{n-1} \tan{\vartheta / 2}}, \mathrm{\quad{}for\ } 0 \leq i \leq n-2
\end{equation*}

The ($n-1$)-dimensional coordinates generated by this process can then be recursively projected down to progressively lower dimensions using this method.
The objects represented by these coordinates can also be discretised into images of any dimension.
For instance, \citet{Hanson94} describes how to perform many of the operations that would be required, such as dimension-independent clipping tests and ray-tracing methods.

In addition to orthographic and perspective projections, there are other interesting projections that can be applied in higher dimensions.
In fact, since we lack an intuitive understanding of higher dimensions, there is little benefit in using projections that work in similar ways as the ways in which we mentally process 3D information.
For instance, Jenn 3D\footnote{\url{http://www.math.cmu.edu/~fho/jenn/}} visualises polyhedra and polychora by first projecting them inwards/outwards to the volume of a 3-sphere\footnote{Intuitively, an unbounded volume that wraps around itself, much like a 2-sphere can be seen as an unbounded surface that wraps around itself.}, resulting in curved edges, faces and volumes.
In a dimension-independent setting, this projection can be easily done by considering the angles $\vartheta_0, \ldots, \vartheta_{n-2}$ in an \emph{$n$-dimensional spherical coordinate system}.
\citet[\S{}12.2]{Steeb11} formulates such a system as:

% TODO: Match code

\begin{align*}
r &= \sqrt{x_0^2 + \cdots + x_{n-1}^2} \\
\vartheta_i &= \cos^{-1} \left( \frac{x_i}{\sqrt{r^2 - \sum_{j=0}^{i-1} x_j^2}} \right), \mathrm{\quad{}for\ } 0 \leq i < n-2 \\
\vartheta_{n-2} &= \tan^{-1} \left( \frac{x_{n-1}}{x_{n-2}} \right) \\
\end{align*}

It is worth to note that the radius $r$ of such a coordinate system is a measure of the depth with respect to the projection $(n-1)$-sphere $S^{n-1}$ and can be used similarly to the previous projection examples.
The points can then be converted back into points on the surface of an $(n-1)$-sphere of radius 1 by making $r = 1$ and applying the inverse transformation.
\citet[\S{}12.2]{Steeb11} formulates it as:

\begin{align*}
x_i &= r \cos \vartheta_i \prod_{j=0}^{i-1} \sin \vartheta_j, \mathrm{\quad{}for\ } 0 \leq i < n-2 \\
x_{n-1} &= r \prod_{j=0}^{n-2} \sin \vartheta_j \\
\end{align*}

The projections on the 3-sphere used by Jenn 3D are then stereographically projected to $\mathbb{R}^3$, then with a perspective projection (\reffig{fig:stereo-earth}) down to 2D.
The final result of this 4D-to-2D projection in multiple stages is shown in \reffig{fig:jenn}.
A stereographic projection is also easy to apply in higher dimensions, mapping an $(n+1)$-dimensional point $x = (x_0, \ldots, x_n)$ on an $n$-sphere $S^n$ to an $n$-dimensional point $x^\prime = (x_0, \ldots, x_{n-1})$ in the $n$-dimensional Euclidean space $\mathbb{R}^n$.
\citet{Chisholm00} formulates this projection as:

\marginpar{
\captionsetup{type=figure}
\centering
\includegraphics[width=\marginparwidth]{figs/stereo-earth}
\caption[Stereographic projection]{A 2-sphere to $\mathbb{R}^2$ stereographic projection can map the surface of the Earth to the plane.
Here, every point $p$ on the sphere is projected to the intersection of the plane with a line passing through the North pole and $p$.
From \citet{Leys08}.}
\label{fig:stereo-earth}
}

\begin{equation*}
x_i^\prime = \frac{x_i}{x_n-1}, \mathrm{\quad{}for\ } 0 \leq i < n
\end{equation*}

\begin{figure}[b]
\centering
\subfloat[]{\includegraphics[width=0.5\linewidth]{figs/jenn-cube}}
\subfloat[]{\includegraphics[width=0.5\linewidth]{figs/jenn-24-cell}}
\caption[Polyhedron and polychoron in Jenn 3D]{A polyhedron and a polychoron in Jenn 3D:\ (a) a cube and (b) a 24-cell.}
\label{fig:jenn}
\end{figure}

\reffig{fig:4dhouse} shows the application of this method to a 4D model of a house.
For this, all the cells of the model were manually defined similar to how it was done in \refse{se:concrete-example} using their boundary cells and 0-embeddings set in $\mathbb{R}^4$.
The 1- and 2-cells were first refined into small line segments and triangles, the 0-, 1- and 2-cells were then projected inwards/outwards to the volume of a 3-sphere ($S^3$), and then stereographically projected to $\mathbb{R}^3$.
The refined 2-cells were exported as to an\ {}.obj file with materials that reflect which 3- and 4-cell they belong to.
Icospheres with a set radius were generated around every 0-cell and approximations of cylinders were generated around every refined 1-cell.
All of these geometries were then imported into Blender 3D and rendered using a perspective projection down to 2D.

\begin{figure}[tb]
\centering
\includegraphics[width=\linewidth]{figs/4dhouse}
\caption[4D to 2D projection of a 4D house]{A 4D model of a house is projected from $\mathbb{R}^4$ inwards/outwards to the 3-sphere $S^3$, then stereographically to $\mathbb{R}^3$, finally using a perspective projection down to $\mathbb{R}^2$.
A $\pi / 5$ rotation along the plane passing through the $x_0$ and $x_3$ (LOD) axes makes the bottom and back 2-cells larger than the rest.}
\label{fig:4dhouse}
\end{figure}

\section{Conclusions and possibilities}
\label{se:slicing-conclusions}

Using a combination of data \emph{selection} and \emph{projections}, it is possible to extract meaningful lower-dimensional information from higher-dimensional datasets, making it possible for these datasets to be used in standard software and visualised.
Selecting an arbitrary subset of an $\mathbb{R}^n$ point set is very challenging, as this process might require the computation of Boolean set intersection operations in any dimension.
However, simple selections of the objects within a region or selections involving a finite number of discrete points are relatively straightforward and can be implemented with existing techniques \citep{Hanson94}.

On the other hand, projecting higher-dimensional datasets into lower dimensions is simple.
As the projection methods explained and developed in this chapter have shown, many projections have dimension-independent formulations using linear algebra that are not that different from their 3D-to-2D versions.
They are therefore relatively easy to implement using similar pipelines as current processes \citep{Chu09}, even if they have not been implemented for general datasets within this thesis due to time constraints.

As this chapter focused on extracting lower-dimensional information from higher-dimensional models from a high-level perspective and in a generic way, many interesting techniques covering useful special cases have not been explored here.
For instance, several authors discuss the visualisation of specific classes of objects, generally in 4D.
\citet{Hoffmann90} describes methods to visualise surfaces embedded in 4D space.
\citet{Balsys07} render implicit (parametrised) surfaces of 4D objects as evaluated sets of points.

Also missing from this chapter was any mention of user interaction and the definition of good camera parameters, both of which are important in order to define values for the input variables described in every projection method.
\citet{Feiner90} implemented a system where a user sets such variables using a glove.
\citet{Zhang07} uses haptic controllers to explore the 3D shadows casted by 4D objects.

It is good to note that while the techniques mentioned in this chapter are also applicable to different types of objects than those used in GIS and useful for generic applications.
\citet{Hanson01} uses similar techniques to those mentioned here in order to visualise relativity, \citet{Bajaj98} does so for $n$-dimensional scalar fields.


\chapterimage{head2.png} % Chapter heading image
\chapter{Bayesian Inference Framework 2}
\section{Graphical Model}
\begin{definition}[Cartesian Space Graphical Model]\label{graphmodelx}
The model can be shown as
\begin{center}
        \includegraphics[scale=0.8]{ch8/hmm_graphical_xspace.pdf}
\end{center}
The joint distribution for this model is given by
\begin{equation}
    p(\textbf{y}_1,\cdots,\textbf{y}_N,\textbf{x}_1,\cdots,\textbf{x}_N) = p(\textbf{x}_1)\left[\prod_{n=2}^{N}p(\textbf{x}_n|\textbf{x}_{n-1})\right]\prod_{n=1}^{N}p(\textbf{y}_n|\textbf{x}_n)
\end{equation}
\end{definition}

\begin{definition}[Eigenspace Graphical Model]\label{graphmodels}
The model can be shown as
\begin{center}
    \includegraphics[scale=0.8]{ch8/hmm_graphical_eigenspace.pdf}
\end{center}
The joint distribution for this model is given by
\begin{equation}
    p(\textbf{y}_1,\cdots,\textbf{y}_N,\textbf{s}_1,\cdots,\textbf{s}_N) = p(\textbf{s}_1)\left[\prod_{n=2}^{N}p(\textbf{s}_n|\textbf{s}_{n-1})\right]\prod_{n=1}^{N}p(\textbf{y}_n|\textbf{s}_n)
\end{equation}
\end{definition}

\section{Cartesian Space Probability Amplitude and Eigenspace Probability Vector}
\begin{definition}[Cartesian Space Probability and Probability Amplitude]
Given $p_{\rm{eq}}(x)$
\begin{equation}
    \rho(x,0) = \frac{p(x,0)}{\sqrt{p_{\rm{eq}}(x)}}
\end{equation}
We can express $\rho(x,0)$ as the linear combination of eigenfunctions, which fully determined by $p_{\rm{eq}}(x)$
\begin{equation}
    \rho(x,0) = \sum_{k=1}^{N_v} a_k \psi_{k}(x)
\end{equation}
By bra-ket notation:
\begin{equation}
    \left| \rho_0 \right> = \sum_{k=1}^{N_v} a_k \left| \psi_{k} \right>~~~~\text{where}~~~\sum_{k=1}^{N_v} a_k^2 = 1
\end{equation}
\end{definition}

\begin{definition}[Eigenspace Probability Vector]
We can define a probability vector
\begin{equation}
    p(\textbf{s}, t=0) =
    \begin{bmatrix}
        a_1^2 & a_2^2 & \cdots & a_{N_v}^2
    \end{bmatrix}^{T}
\end{equation}
\end{definition}

\section{The Mechanism of Time Evolution}
\begin{definition}[Cartesian Space: $\rho(x,\Delta t)$]
\begin{equation}
    \rho(x,\Delta t) = \tilde{a}_1 \psi_{1}(x) + \sum_{k=2}^{N_v} a_k e^{-\lambda_k \Delta t}\psi_{k}(x)
\end{equation}
where
\begin{equation}
    \tilde{a}_1 = \pm \sqrt{1 - \sum_{k=2}^{N_v} a_k^2 e^{-2\lambda_k \Delta t}}
\end{equation}
The sign of $\tilde{a}_1$ is determined by $\left<\psi_{1}|\rho_{\rm{eq}}\right>$. And we also can express the above operation in terms of bra-ket notation:
\begin{equation}
    \left< \rho_0 \right| e^{-\textbf{H} \Delta t} =
    \begin{bmatrix}
        \tilde{a}_1 & a_2 e^{-\lambda_2 \Delta t} & \cdots & a_{N_v} e^{-\lambda_{N_v} \Delta t}
    \end{bmatrix}
\end{equation}
\end{definition}

\begin{definition}[Eigenspace: $p(\textbf{s}, \Delta t)$]
\begin{equation}
    p(\textbf{s}, \Delta t) =
    \begin{bmatrix}
        \tilde{a}_1^2 & a_2^2 e^{-2\lambda_2 \Delta t} & \cdots & a_{N_v}^2 e^{-2\lambda_{N_v} \Delta t}
    \end{bmatrix}^{T}
\end{equation}
\end{definition}

\begin{definition}[Eigenspace: Stochastic matrix $\textbf{A}$]
\begin{equation}
        \textbf{A} p(\textbf{S}, 0) = p(\textbf{S}, \Delta t)
\end{equation}
In terms of matrix
\begin{equation}
\begin{bmatrix}
    A_{11} & A_{12} & \cdots & A_{1,N_v} \\
    A_{21} & A_{22} & \cdots & A_{2,N_v} \\
    \vdots & \vdots & \cdots & \vdots \\
    A_{N_v,1} & A_{N_v,2} & \cdots & A_{N_v,N_v}
\end{bmatrix}
\begin{bmatrix}
    a_1^2 \\ a_2^2 \\ \vdots \\ a_{N_v}^2
\end{bmatrix} =
\begin{bmatrix}
    1 - \sum_{k=2}^{N_v} a_k^2 e^{-2\lambda_k \Delta t} \\ a_2^2 e^{-2\lambda_2 \Delta t} \\ \vdots \\ a_{N_v}^2 e^{-2\lambda_{N_v} \Delta t}
\end{bmatrix}
\end{equation}
Therefore,
\begin{equation}
\textbf{A} =
\begin{bmatrix}
    1 & 1 -  e^{-2 D_2 \lambda'_2 \Delta t} & \cdots & 1 - e^{-2D_{N_v}\lambda'_{N_v} \Delta t} \\
    0 & e^{-2D_2 \lambda'_2 \Delta t} & \cdots & 0 \\
    \vdots & \vdots & \cdots & \vdots \\
    0 & 0 & \cdots & e^{-2D_{N_v}\lambda'_{N_v} \Delta t}
\end{bmatrix}
\end{equation}
\begin{center}
    \includegraphics[scale=0.45]{ch8/A_first_row.pdf}
\end{center}
\end{definition}

\begin{definition}[D from A]
\begin{equation}
    D_k = \frac{\ln{(1-A_{1k})}}{-2 \lambda'_k \Delta t}
\end{equation}
\begin{center}
    \includegraphics[scale=0.45]{ch8/EM_D_result_1.pdf}
\end{center}
\begin{center}
    \includegraphics[scale=0.45]{ch8/EM_D_result_2.pdf}
\end{center}
\end{definition}

\section{The Initial State}
Now, we encode the time variable into subscripts such as $\textbf{x}_1$,...,$\textbf{x}_{N}$, and $\textbf{s}_1$,...,$\textbf{s}_{N}$
\begin{definition}[Cartesian Space: Probability Amplitude]
\begin{equation}
    \rho(\textbf{x}_1) = \sum_{k=1}^{N_v} \pi_k \psi_k(x_1)
\end{equation}
By bra-ket notation:
\begin{equation}
    \left< \rho_1 \right| = \sum_{k=1}^{N_v} \pi_k \left< \psi_k \right|
\end{equation}
\end{definition}

\begin{definition}[Eigenspace: Probability Vector]
\begin{equation}
        p(\textbf{s}_1) =
\begin{bmatrix}
        \pi_1^2 & \pi_2^2 & \cdots & \pi_{N_v}^2
\end{bmatrix}^{T}
~~~~~\text{where}~~\sum_{k=1}^{N_v}\pi_{k}^2=1
\end{equation}
\end{definition}

\section{Photon-Operator and Emission Probability}
\begin{definition}[Cartesian Space: Photon Operator]
Given $\textbf{y}_n=\mu_n$, the photon operator $\hat{\textbf{y}}_n$ is defined by a normal distribution
\begin{equation}
    \hat{\textbf{y}}_n = N(\mu_n,\sigma ^{2}) = f_n(x)
\end{equation}
\begin{center}
    \includegraphics[scale=0.45]{ch2/photon_mat_example.pdf}
\end{center}
\end{definition}

\begin{definition}[Cartesian Space: Emission Probability]
\begin{equation}
    p(\textbf{y}_n|\textbf{x}_n) = N(\mu_n=\textbf{x}_n,\sigma ^{2})
\end{equation}
and
\begin{equation}
    p(\textbf{y}_n|\textbf{s}_n)
\end{equation}
\begin{center}
    \includegraphics[scale=0.6]{ch8/emission_prob_illu.pdf}
\end{center}

\end{definition}

\begin{definition}[Eigenspace: Photon Operator]
Transform $\hat{\textbf{y}}_n$ from Cartesian space to eigenspace $\textbf{y}_n$, and the element of $\textbf{y}_n$:
\begin{equation}
    y_{ij} = \langle \psi_i | \hat{\textbf{y}}_{n} | \psi_j \rangle = \int \psi_i(x) f_n(x) \psi_j(x) dx
\end{equation}
For example,
\begin{align*}
    \left< \rho_1 \right| \textbf{y}_1 &=\begin{bmatrix}
        \pi_1 & \pi_2 & \cdots & \pi_{N_v}
    \end{bmatrix}
    \begin{bmatrix}
        y_{11} & y_{12} & \cdots & y_{1,N_v} \\
        y_{21} & y_{22} & \cdots & y_{2,N_v} \\
        \vdots & \vdots & \cdots & \vdots \\
        y_{N_v, 1} & y_{12} & \cdots & y_{N_v,N_v}
    \end{bmatrix}\\
    &=\begin{bmatrix}
        \sum_{k=1}^{N_v}\pi_k y_{k1} & \sum_{k=1}^{N_v}\pi_k y_{k2} & \cdots & \sum_{k=1}^{N_v}\pi_k y_{k,N_v}
    \end{bmatrix}
\end{align*}
\end{definition}

\begin{definition}[Eigenspace: Emission Probability]
In our case, the form of emission probability $p(\textbf{y}_n|\textbf{s}_n)$ is complicated. But we always can use the photon operator to get the target joint probability. For example,
\begin{align*}
    &p(\textbf{y}_1,\textbf{s}_1) = p(\textbf{s}_1)p(\textbf{y}_1|\textbf{s}_1)\\
    &=\begin{bmatrix}
        \left(\sum_{k=1}^{N_v}\pi_k y_{k1}\right)^2 & \left(\sum_{k=1}^{N_v}\pi_k y_{k2}\right)^2 & \cdots & \left(\sum_{k=1}^{N_v}\pi_k y_{k,N_v}\right)^2
    \end{bmatrix}
\end{align*}
\end{definition}

\section{Expectation Maximization in Eigenspace}
\begin{definition}[E-step: $Q(\bm{\theta}, \bm{\theta}^{\text{old}})$]
Find the posterior distribution of the latent variables
\begin{equation}
    p(\bm{S}|\textbf{Y}, \bm{\theta}^{\rm{old}})
\end{equation}
Then, use it to evaluate the expectation of the logarithm of the complete-data likelihodd function
\begin{equation}
    Q(\bm{\theta}, \bm{\theta}^{\text{old}}) = \sum_{\textbf{S}}p(\textbf{S}|\textbf{Y}, \bm{\theta}^{\rm{old}}) \ln{p(\textbf{Y},\textbf{S}|\bm{\theta})}
\end{equation}
\end{definition}

\begin{definition}[E-step: $\gamma$, $\xi$]
$\gamma(\textbf{s}_n)$ is the marginal posterior distribution of a latent variable $\textbf{s}_n$
\begin{equation}
    \gamma(\textbf{s}_n) = p(\textbf{s}_n|\textbf{Y},\bm{\theta}^{\text{old}})
\end{equation}
and $\xi(\textbf{s}_{n-1},\textbf{s}_n)$ is the joint posterior distribution of two successive latent variables
\begin{equation}
    \xi(\textbf{s}_{n-1},\textbf{s}_n) = p(\textbf{s}_{n-1},\textbf{s}_n|\textbf{Y},\bm{\theta}^{\text{old}})
\end{equation}
By using these two posterior probability, we can rewrite $Q(\bm{\theta}, \bm{\theta}^{\text{old}})$
\begin{equation}
    Q(\bm{\theta}, \bm{\theta}^{\text{old}}) = \sum_{k=1}^{N_v}\gamma(s_{1k})\ln{\pi_k} + \sum_{n=2}^{N}\sum_{j=1}^{N_v}\sum_{k=1}^{N_v} \xi(s_{n-1,j},~s_{nk})\ln{A_{jk}} + \sum_{n=1}^{N}\sum_{k=1}^{N_v} \gamma(s_{nk}) \ln{p(\textbf{y}_n|\phi_k)}
\end{equation}
\end{definition}

\begin{definition}[M-step]
Maximize $Q(\bm{\theta}, \bm{\theta}^{\text{old}})$ with respect to the parameter $\bm{\theta}=\{\pi, \textbf{A}, \phi\}$ in which we treat $\gamma(\textbf{s}_n)$ and $\xi(\textbf{s}_{n-1},\textbf{s}_n)$ as constant. We get
\begin{equation}
    \pi_k = \frac{\gamma(s_{1k})}{\sum_{j=1}^{N_v}\gamma(s_{1j})}
\end{equation}
and
\begin{equation}
    A_{jk} = \frac{ \sum_{n=2}^{N} \xi(s_{n-1,j},~s_{nk}) }{ \sum_{l=1}^{N_v}\sum_{n=2}^{N} \xi(s_{n-1,j},~s_{nl}) }
\end{equation}
\end{definition}

\begin{definition}[M-step: Example]
Say we have two states, so
\begin{align*}
    A = \begin{bmatrix}
        A_{11} & A_{12} \\
        A_{21} & A_{22}
    \end{bmatrix}
\end{align*}
and
\begin{align*}
    A_{11} + A_{12} &= 1 \\
    A_{21} + A_{22} &= 2
\end{align*}
and
\begin{align*}
    \tilde{Q} &= \xi(s_{1,1},~s_{2,1})\ln{A_{11}} + \xi(s_{1,1},~s_{2,2})\ln{A_{12}} + \xi(s_{1,2},~s_{2,1})\ln{A_{21}} + \xi(s_{1,2},~s_{2,2})\ln{A_{22}} \\
    &= \xi(s_{1,1},~s_{2,1})\ln{A_{11}} + \xi(s_{1,1},~s_{2,2})\ln{(1-A_{11})} + \xi(s_{1,2},~s_{2,1})\ln{A_{21}}\\
    & + \xi(s_{1,2},~s_{2,2})\ln{(1-A_{21})}
\end{align*}
and
\begin{align*}
    \frac{d \tilde{Q}}{dA_{11}} = \frac{\xi(s_{1,1},~s_{2,1})}{A_{11}}-\frac{\xi(s_{1,1},~s_{2,2})}{1-A_{11}} = 0
\end{align*}
so
\begin{align*}
    \frac{\xi(s_{1,1},~s_{2,1})}{A_{11}} &= \frac{\xi(s_{1,1},~s_{2,2})}{1-A_{11}} \\
    \xi(s_{1,1},~s_{2,1}) - A_{11}  \xi(s_{1,1},~s_{2,1}) &= \xi(s_{1,1},~s_{2,2}) A_{11} \\
    A_{11} &= \frac{\xi(s_{1,1},~s_{2,1})}{\xi(s_{1,1},~s_{2,1})+ \xi(s_{1,1},~s_{2,2})}
\end{align*}
\end{definition}

\section{Forward-Backward Algorithm}
We seek an efficient procedure for evaluating the quantities $\gamma(s_{nk})$ and $\xi(s_{n-1,j},~s_{nk})$, corresponding to the E step of the EM algorithm.

\begin{definition}[$\gamma$,$\alpha$,$\beta$]
\begin{equation}
    \gamma(\textbf{s}_n) = p(\textbf{s}_n|\textbf{Y})
\end{equation}
Using the conditional independence property,
\begin{equation}
    \gamma(\textbf{s}_n) = \frac{ p(\textbf{y}_1,\cdots,\textbf{y}_n,\textbf{s}_n) p(\textbf{y}_{n+1},\cdots,\textbf{y}_N|\textbf{s}_n) }{p(\textbf{Y})} = \frac{ \alpha(\textbf{s}_n) \beta(\textbf{s}_n) }{p(\textbf{Y})}
\end{equation}
where we have defined
\begin{align}
    \alpha(\textbf{s}_n) &= p(\textbf{y}_1,\cdots,\textbf{y}_n,\textbf{s}_n) \\
    \beta(\textbf{s}_n) &= p(\textbf{y}_{n+1},\cdots,\textbf{y}_N|\textbf{s}_n)
\end{align}
\end{definition}

\begin{definition}[$\alpha(\textbf{s}_n)$]
In terms of probability amplitude:
\begin{equation}
    \left< \alpha_{n} \right| = \left< \alpha_{n-1} \right| e^{-\textbf{H} \Delta t} \textbf{y}_n
\end{equation}
and the probability vector in eigenspace is
\begin{equation}
    \alpha(\textbf{s}_n) =
    \begin{bmatrix}
        \left(\left< \alpha_{n} | \psi_1 \right>\right)^2 & \left(\left< \alpha_{n} | \psi_2 \right>\right)^2 & \cdots & \left(\left< \alpha_{n} | \psi_{N_v} \right>\right)^2
    \end{bmatrix}^{T}
\end{equation}
\end{definition}

\begin{definition}[$\beta(\textbf{s}_n)$]
In terms of probability amplitude:
\begin{equation}
    \left| \beta_{n} \right> =  e^{-\textbf{H} \Delta t} \textbf{y}_{n+1}  \left| \beta_{n+1} \right>
\end{equation}
and the probability vector in eigenspace is
\begin{equation}
    \beta(\textbf{s}_n) =
    \begin{bmatrix}
        \left(\left< \psi_1 | \beta_{n} \right>\right)^2 & \left(\left<  \psi_2 | \beta_{n} \right>\right)^2 & \cdots & \left(\left< \psi_{N_v} | \beta_{n} \right>\right)^2
    \end{bmatrix}^{T}
\end{equation}
\end{definition}

\begin{definition}[$\xi(\textbf{s}_{n-1},~\textbf{s}_{n})$]
\begin{equation}
    \xi(\textbf{s}_{n-1},~\textbf{s}_{n}) = \frac{\alpha(\textbf{s}_{n-1}) p(\textbf{y}_n|\textbf{s}_n) p(\textbf{s}_n|\textbf{s}_{n-1}) \beta(\textbf{s}_n) }{p(\textbf{Y})}
\end{equation}
\end{definition}

\section{Forward-Backward Algorithm: Scaling Factor}
\begin{definition}[Scaling Factor $c_n$]
\begin{equation}
    c_n = p(\textbf{y}_n|\textbf{y}_1,\cdots,\textbf{y}_{n-1})
\end{equation}
From the product rule, we then have
\begin{equation}
    p(\textbf{y}_1,\cdots,\textbf{y}_{n}) = \prod_{m=1}^{n}c_m
\end{equation}
\end{definition}

\begin{definition}[$\hat{\alpha}$]
In terms of probability amplitude and bra-ket notation:
\begin{equation}
    \left< \hat{\alpha}_n \right| = \frac{ \left< \hat{\alpha}_{n-1} \right| e^{-\textbf{H}\Delta t} \textbf{y}_n }{ \norm{\left< \hat{\alpha}_{n-1} \right| e^{-\textbf{H}\Delta t} \textbf{y}_n} }
\end{equation}
and the scaling factor is
\begin{equation}
    c_n = \norm{\left< \hat{\alpha}_{n-1} \right| e^{-\textbf{H}\Delta t} \textbf{y}_n}^2
\end{equation}
For the probability vector in eigenspace, we define a normalized version of $\alpha$ given by
\begin{equation}
    \hat{\alpha}(\textbf{s}_n) = p(\textbf{s}_n|\textbf{y}_1,\cdots,\textbf{y}_n) = \frac{\alpha(\textbf{s}_n)}{p(\textbf{y}_1,\cdots,\textbf{y}_n)}
\end{equation}
and
\begin{equation}
    \hat{\alpha}(\textbf{s}_n) =
    \begin{bmatrix}
        \left(\left< \hat{\alpha}_n | \psi_1 \right>\right)^2 & \left(\left< \hat{\alpha}_n | \psi_2 \right>\right)^2 & \cdots & \left(\left< \hat{\alpha}_n | \psi_{N_v} \right>\right)^2
    \end{bmatrix}^{T}
\end{equation}
and it should satisfy $\sum_{k=1}^{N_v} \left(\left< \hat{\alpha}_n | \psi_k \right>\right)^2 = 1$
\end{definition}

\begin{definition}[$\gamma$]
In Cartesian space,
\begin{equation}
    \gamma(\textbf{x}_n) = p(\textbf{x}_n| \textbf{Y}) = \left(\left< \hat{\alpha}_n | x \right> \right)^2 \left( \left<  x | \hat{\beta}_n \right> \right)^2
\end{equation}
and the probability amplitude of $\gamma(\textbf{x}_n)$ is
\begin{equation}
    \left| \gamma_n \right> = \sum_{k=1}^{N_v} g_k \left| \psi_k \right>
\end{equation}
where $g_k$ is
\begin{equation}
    g_k = \int (\gamma(x_n))^{1/2} \psi_k(x_n) dx_n
\end{equation}
Then,
\begin{equation}
    \gamma(\textbf{s}_n) =
    \begin{bmatrix}
        g^2_1 & g^2_2 & \cdots & g^2_{N_v}
    \end{bmatrix}
\end{equation}
Note
\begin{align}
    \int \gamma(\textbf{x}_n) d\textbf{x}_n &= 1 \\
    \sum_{\textbf{s}_n} \gamma(\textbf{s}_n) &= 1 \\
    \norm{\left| \gamma_n \right>}&= 1
\end{align}
\end{definition}

\begin{definition}[$r~$: Scaling factor for $\beta$]
For the last index of photon $N$,
\begin{align}
    r_N = \int \left(\left< \hat{\alpha}_N | x \right> \right)^2 \left( \left<  x | \beta_N \right> \right)^2 dx = 1
\end{align}
For other index $n$,
\begin{align}
    r_n = \int \left(\left< \hat{\alpha}_n | x \right> \right)^2 \left( \left< x | e^{-\textbf{H} \Delta t} \textbf{y}_{n+1} | \hat{\beta}_{n+1} \right> \right)^2 dx
\end{align}
\end{definition}

\begin{definition}[$\hat{\beta}_N$]
Because $r_N=1$
\begin{equation}
    \left| \hat{\beta}_{N} \right> = \frac{\left| \beta_{N} \right>}{\sqrt{r_{N}}} = \left| \beta_{N} \right>
\end{equation}
\end{definition}

\begin{definition}[$\hat{\beta}_n$]
In terms of probability amplitude and bra-ket notation:
\begin{equation}
    \left| \hat{\beta}_{n} \right> = \frac{e^{-\textbf{H}\Delta t}\textbf{y}_{n+1}\left| \hat{\beta}_{n+1} \right>}{\sqrt{r_{n}}}
\end{equation}
Note that
\begin{equation}
    \left<\hat{\alpha}_{n} | \hat{\beta}_{n} \right> = 1~~~\forall~~n\neq N
\end{equation}
and
\begin{equation}
    \hat{\beta}(\textbf{s}_n) =
    \begin{bmatrix}
        \left(\left< \psi_1 | \hat{\beta}_{n} \right>\right)^2 &\left(\left< \psi_2 | \hat{\beta}_{n} \right>\right)^2 & \cdots & \left(\left< \psi_{N_v} | \hat{\beta}_{n} \right>\right)^2
    \end{bmatrix}^{T}
\end{equation}
\end{definition}

\begin{definition}[Check the consistency for $r_n$ and $\hat{\beta}$]
Since the posterior probability should satisfy
\begin{equation}
    \int \gamma(\textbf{x}_n) d\textbf{x}_n = 1
\end{equation}
and we know that
\begin{align*}
    \int \gamma(\textbf{x}_n) d\textbf{x}_n &= \int \left(\left< \hat{\alpha}_n | x \right> \right)^2 \left( \left<  x | \hat{\beta}_n \right> \right)^2 d\textbf{x}_n \\
    &= \int \left(\left< \hat{\alpha}_n | x \right> \right)^2  \left( \frac{\left< x|e^{-\textbf{H}\Delta t}\textbf{y}_{n+1}| \hat{\beta}_{n+1} \right>}{\sqrt{r_{n}}}\right)^2 d\textbf{x}_n
\end{align*}
so
\begin{equation}
    \int \gamma(\textbf{x}_n) d\textbf{x}_n = \frac{1}{r_n} \int \left(\left< \hat{\alpha}_n | x \right> \right)^2  \left( \left< x|e^{-\textbf{H}\Delta t}\textbf{y}_{n+1}| \hat{\beta}_{n+1} \right>\right)^2 d\textbf{x}_n = 1
\end{equation}
and we finally get
\begin{align*}
    r_n = \int \left(\left< \hat{\alpha}_n | x \right> \right)^2  \left( \left< x|e^{-\textbf{H}\Delta t}\textbf{y}_{n+1}| \hat{\beta}_{n+1} \right>\right)^2 d\textbf{x}_n
\end{align*}
\end{definition}

\begin{definition}[Likelihood Function $p(\textbf{Y})$]
\begin{equation}
    p(\textbf{Y}) = \prod_{n=1}^{N}c_n
\end{equation}
and log-likelihood is
\begin{equation}
    \ln{p(\textbf{Y})} = \sum_{n=1}^{N}\ln{c_n}
\end{equation}
\end{definition}

\begin{definition}[$\xi$]
\begin{equation}
    \xi(\textbf{s}_{n-1},\textbf{s}_n) = c_n^{-1} \hat{\alpha}(\textbf{s}_{n-1}) p(\textbf{y}_n|\textbf{s}_n) p(\textbf{s}_n|\textbf{s}_{n-1}) \hat{\beta}(\textbf{s}_n)
\end{equation}
The matrix form is
\begin{equation}
    \xi(\textbf{s}_{n-1},\textbf{s}_n) =
    \begin{bmatrix}
        \xi(s_{n-1,1},~s_{n1}) & \xi(s_{n-1,1},~s_{n2}) & \cdots & \xi(s_{n-1,1},~s_{n,N_v}) \\
        \xi(s_{n-1,2},~s_{n1}) & \xi(s_{n-1,2},~s_{n2}) & \cdots & \xi(s_{n-1,2},~s_{n,N_v}) \\
        \vdots & \vdots & \cdots & \vdots \\
        \xi(s_{n-1,N_v},~s_{n1}) & \xi(s_{n-1,N_v},~s_{n2}) & \cdots & \xi(s_{n-1,N_v},~s_{n,N_v})
    \end{bmatrix}
\end{equation}
We also can use bra-ket notation to get the matrix
\begin{equation}
\xi(\textbf{s}_{n-1},\textbf{s}_{n}) = \frac{\left( \textbf{y}_n | \hat{\beta}_n \left>\right<\hat{\alpha}_{n-1} |e^{-\textbf{H}\Delta t}\right)^2}{c_n}
\end{equation}
\end{definition}

\begin{definition}[Example: $\xi$]
\begin{equation}
    \xi(\textbf{s}_{4},\textbf{s}_{5}) = p(\textbf{s}_{4},\textbf{s}_{5}|\textbf{Y})
\end{equation}
The following graphical model shows $\xi(\textbf{s}_{4},\textbf{s}_{5})$
\begin{center}
    \includegraphics[scale=0.6]{ch8/xi_meaning_graphical.pdf}
\end{center}
and the following shows $\alpha(\textbf{s}_{4})$ and $\beta(\textbf{s}_5)$
\begin{center}
    \includegraphics[scale=0.6]{ch8/xi_alpha_beta_meaning_graphical.pdf}
\end{center}
Then,
\begin{center}
    \includegraphics[scale=0.6]{ch8/xi_alpha_eHdt.pdf}
\end{center}
\begin{center}
    \includegraphics[scale=0.6]{ch8/xi_y_beta.pdf}
\end{center}
and
\begin{equation}
    p(\textbf{s}_{4},\textbf{s}_{5},\textbf{Y}) = \left( \textbf{y}_5\left| \beta_5 \right>\left<\alpha_4\right|e^{-\textbf{H}\Delta t}\right)^2
\end{equation}
\begin{center}
    \includegraphics[scale=0.6]{ch8/xi_ybeta_alphaeHdt.pdf}
\end{center}
Then, because
\begin{equation}
    \xi(\textbf{s}_{4},\textbf{s}_{5}) = p(\textbf{s}_{4},\textbf{s}_{5}|\textbf{Y}) = \frac{p(\textbf{s}_{4},\textbf{s}_{5},\textbf{Y})}{p(\textbf{Y})} = \frac{p(\textbf{s}_{4},\textbf{s}_{5},\textbf{Y})}{\prod_{m=1}^{7}c_m}
\end{equation}
we also know
\begin{align}
    \left< \hat{\alpha}_{4} \right| &= \frac{\left< \alpha_4 \right|}{\left(\prod_{m=1}^{4}c_m\right)^{1/2}} \\
    \left| \hat{\beta}_5 \right> &= \frac{\left| \beta_5 \right>}{\left(\prod_{m=6}^{7}c_m\right)^{1/2}}
\end{align}
Therefore, we get
\begin{equation}
    \xi(\textbf{s}_{4},\textbf{s}_{5}) = \frac{\left( \textbf{y}_5 | \hat{\beta}_5 \left>\right<\hat{\alpha}_4 |e^{-\textbf{H}\Delta t}\right)^2}{c_5}
\end{equation}
\end{definition}

\begin{definition}[First Part: $\xi(\textbf{x}_4, \textbf{x}_5)$]
\begin{align}
    \left(\left<\hat{\alpha}_4| x \right>\right)^2 &= p(\textbf{x}_4 | \textbf{y}_1,...,\textbf{y}_4)\\
    \left(\left<\hat{\alpha}_4|e^{-\textbf{H}\Delta t} | x \right>\right)^2 &= p(\textbf{x}_5| \textbf{y}_1,...,\textbf{y}_4,\textbf{x}_4)
\end{align}
and
\begin{align*}
    \left(\left<\hat{\alpha}_4| x \right>\right)^2 \left(\left<\hat{\alpha}_4|e^{-\textbf{H}\Delta t} | x \right>\right)^2 = p(\textbf{x}_4, \textbf{x}_5 | \textbf{y}_1,...,\textbf{y}_4)
\end{align*}
\begin{center}
    \includegraphics[scale=0.4]{ch8/p_x4_x5_given_y1toy4.pdf}
\end{center}
In bra-ket form:
\begin{equation}
    p(\textbf{s}_4, \textbf{s}_5 | \textbf{y}_1,...,\textbf{y}_4) = \left( \left| \hat{\alpha}_4 \right> \left<\hat{\alpha}_4 \right| e^{-\textbf{H}\Delta t} \right)^2
\end{equation}
\begin{center}
    \includegraphics[scale=0.4]{ch8/p_s4_s5_given_y1toy4.pdf}
\end{center}
\end{definition}

\begin{definition}[Second Part: $\xi(\textbf{x}_4, \textbf{x}_5)$]
\begin{align}
   \left( \left< x| \hat{\beta}_5 \right> \right)^2 &= \frac{p(\textbf{y}_6,...,\textbf{y}_N|\textbf{x}_5)}{p(\textbf{y}_6,...,\textbf{y}_N|\textbf{y}_1,...,\textbf{y}_5)} \\
   \left( \left< x| \textbf{y}_5 | \hat{\beta}_5 \right> \right)^2 &= \frac{p(\textbf{y}_5 |\textbf{x}_5) p(\textbf{y}_6,...,\textbf{y}_N|\textbf{x}_5)}{p(\textbf{y}_6,...,\textbf{y}_N|\textbf{y}_1,...,\textbf{y}_5)} = \frac{p(\textbf{y}_5,...,\textbf{y}_N|\textbf{x}_5)}{p(\textbf{y}_6,...,\textbf{y}_N|\textbf{y}_1,...,\textbf{y}_5)}
\end{align}
and
\begin{equation}
    \left( \frac{\left< x| \textbf{y}_5 | \hat{\beta}_5 \right>}{ \norm{\textbf{y}_5 \left| \hat{\beta}_5 \right>}} \right)^2 = \frac{p(\textbf{y}_5,...,\textbf{y}_N|\textbf{x}_5)}{p(\textbf{y}_5|\textbf{y}_1,...,\textbf{y}_4) p(\textbf{y}_6,...,\textbf{y}_N|\textbf{y}_1,...,\textbf{y}_5)}
\end{equation}
\begin{center}
    \includegraphics[scale=0.4]{ch8/beta_x5_xi.pdf}
\end{center}
In eigenspace
\begin{center}
    \includegraphics[scale=0.4]{ch8/beta_s5_xi.pdf}
\end{center}
\end{definition}

\begin{definition}[Third Part: $\xi(\textbf{x}_4, \textbf{x}_5)$]
\begin{align*}
    &\left(\left<\hat{\alpha}_4| x \right>\right)^2 \left(\left<\hat{\alpha}_4|e^{-\textbf{H}\Delta t} | x \right>\right)^2 \left( \frac{\left< x| \textbf{y}_5 | \hat{\beta}_5 \right>}{ \norm{\textbf{y}_5 \left| \hat{\beta}_5 \right>}} \right)^2 \\
    &= \frac{p(\textbf{x}_4, \textbf{x}_5, \textbf{y}_1,...,\textbf{y}_4)}{p(\textbf{y}_1,...,\textbf{y}_4)} \frac{p(\textbf{y}_5,...,\textbf{y}_N|\textbf{x}_5)}{p(\textbf{y}_5|\textbf{y}_1,...,\textbf{y}_4) p(\textbf{y}_6,...,\textbf{y}_N|\textbf{y}_1,...,\textbf{y}_5)} \\
    &= \frac{p(\textbf{x}_4, \textbf{x}_5, \textbf{y}_1,...,\textbf{y}_N)}{p(\textbf{y}_1,...,\textbf{y}_N)} \\
    &= p(\textbf{x}_4, \textbf{x}_5|\textbf{Y}) = \xi(\textbf{x}_4, \textbf{x}_5)
\end{align*}
\begin{center}
    \includegraphics[scale=0.4]{ch8/xi_1.pdf}
\end{center}
\begin{center}
    \includegraphics[scale=0.4]{ch8/xi_2.pdf}
\end{center}
\begin{center}
    \includegraphics[scale=0.4]{ch8/xi_3.pdf}
\end{center}
\end{definition}

\section{Forward-Backward: Example}
We will use the following graphical model as an example to check the detail of calculation:
\begin{center}
    \includegraphics[scale=0.6]{ch8/five_states_example.pdf}
\end{center}

\subsection{Forward Part}
\begin{definition}[$p(\textbf{s}_1)$]
We let $p(\textbf{x}_1)=p_{\rm{eq}}(x)$
\begin{center}
    \includegraphics[scale=0.4]{ch8/pi_example.pdf}
\end{center}
The probability amplitude $\rho_1$ is
\begin{equation}
    \left< \rho_{1} \right| = \left(p(\textbf{s}_1)\right)^{1/2}
\end{equation}
\end{definition}

\begin{definition}[$\alpha(\textbf{s}_1)$]
\begin{equation}
    \left< \alpha_{1} \right| = \left< \rho_{1} \right| \textbf{y}_1
\end{equation}
and
\begin{equation}
    \alpha(\textbf{s}_1) = p(\textbf{s}_1, \textbf{x}_1) = \begin{bmatrix}\left(\left< \alpha_{1} | \psi_1 \right>\right)^2 & \left(\left< \alpha_{1} | \psi_2 \right>\right)^2 & \cdots & \left(\left< \alpha_{1} | \psi_{N_v} \right>\right)^2 \end{bmatrix}^{T}
\end{equation}
\begin{center}
    \includegraphics[scale=0.4]{ch8/alpha1_example.pdf}
\end{center}
Then, we do normalization
\begin{equation}
    \left< \hat{\alpha}_{1} \right| = \frac{\left< \alpha_{1} \right|}{\norm{\left< \alpha_{1} \right|}}
\end{equation}
\begin{center}
    \includegraphics[scale=0.4]{ch8/alpha_hat_1_example.pdf}
\end{center}
\end{definition}

\begin{definition}[$\alpha(\textbf{s}_2)$]
\begin{equation}
    \left< \hat{\alpha}_{1} \right| e^{-\textbf{H}\Delta t}
\end{equation}
\begin{center}
    \includegraphics[scale=0.4]{ch8/alpha_hat_1_edt_example.pdf}
\end{center}
\begin{equation}
    \left< \hat{\alpha}_{1} \right| e^{-\textbf{H}\Delta t}\textbf{y}_2
\end{equation}
\begin{center}
    \includegraphics[scale=0.4]{ch8/alpha_hat_1_edt_y2_example.pdf}
\end{center}
\begin{equation}
    \left< \hat{\alpha}_{2} \right| = \frac{\left< \hat{\alpha}_{1} \right| e^{-\textbf{H}\Delta t}\textbf{y}_2}{\norm{\left< \hat{\alpha}_{1} \right| e^{-\textbf{H}\Delta t}\textbf{y}_2}}
\end{equation}
\begin{center}
    \includegraphics[scale=0.4]{ch8/alpha_hat_2_example.pdf}
\end{center}
\end{definition}

\subsection{Posterior Probability}
\begin{definition}[Special case: $p(\textbf{x}_N|\textbf{Y})$]
\begin{equation}
    p(\textbf{x}_N | \textbf{Y}) =\frac{(\left<\alpha_{N}|\textbf{x} \right>)^2 (\left<\textbf{x}|\beta_{N} \right>)^2 }{p(\textbf{Y})}
\end{equation}
where
\begin{align}
    (\left<\alpha_{N}|\textbf{x} \right>)^2 &=  p(\textbf{y}_1,\cdots,\textbf{y}_N,\textbf{x}_N) = p(\textbf{Y}, \textbf{x}_N) \\
    (\left<\textbf{x}|\beta_{N} \right>)^2 &= p(\textbf{y}_{N+1}|\textbf{x}_{N})\text{~~~Some problem here!}
\end{align}
As you can see, we can not define $\left<\textbf{x}|\beta_{N} \right>$ because there is no $y_{N+1}$. Also, we notice
\begin{align*}
    p(\textbf{x}_N | \textbf{Y}) = \frac{(\left<\alpha_{N}|\textbf{x} \right>)^2}{p(\textbf{Y})} = \frac{p(\textbf{Y}, \textbf{x}_N)}{p(\textbf{Y})}
\end{align*}
Therefore, we just define
\begin{equation}
    \left<\textbf{x}|\beta_{N} \right>=1~~\forall \textbf{x}
\end{equation}
\end{definition}

\subsection{Backward Part}
\begin{definition}[$\beta(\textbf{s}_5)$]
Because
\begin{equation}
    \left<\textbf{x}|\beta_{5} \right>=1~~\forall \textbf{x}
\end{equation}
so
\begin{equation}
    \beta(\textbf{x}_5) = \left(\left<\textbf{x}|\beta_{5} \right>\right)^2=1~~\forall \textbf{x}
\end{equation}
and
\begin{equation}
    \left<\textbf{x}|\beta_{5} \right>=1=\sum_{k=1}^{N_v}b_k\psi_k(\textbf{x})
\end{equation}
so
\begin{equation}
    \left| \beta_{5} \right> =
    \begin{bmatrix}
        b_1 & b_2 & \cdots & b_{N_v}
    \end{bmatrix}^T
\end{equation}
and
\begin{equation}
    \beta(\textbf{s}_5)=
    \begin{bmatrix}
        b^2_1 & b^2_2 & \cdots & b^2_{N_v}
    \end{bmatrix}^T
\end{equation}
\begin{center}
    \includegraphics[scale=0.4]{ch8/beta_s5_example.pdf}
\end{center}
\end{definition}

\begin{definition}[$\hat{\beta}(\textbf{s}_5)$]
First, in Cartesian space
\begin{equation}
    \gamma(\textbf{x}_5) = p(\textbf{x}_5| \textbf{Y}) = \left(\left< \hat{\alpha}_5 | x \right> \right)^2 \left( \left<  x | \hat{\beta}_5 \right> \right)^2 =  \left(\left< \hat{\alpha}_5 | x \right> \right)^2
\end{equation}
Therefore,
\begin{equation}
    \left<  x | \hat{\beta}_5 \right> = 1~~~\forall~x
\end{equation}
so
\begin{align}
    \left| \hat{\beta}_{5} \right> &=
    \begin{bmatrix}
        b_1 & b_2 & \cdots & b_{N_v}
    \end{bmatrix}^T \\
    \hat{\beta}(\textbf{s}_5) &=
    \begin{bmatrix}
        b^2_1 & b^2_2 & \cdots & b^2_{N_v}
    \end{bmatrix}^T
\end{align}
\begin{center}
    \includegraphics[scale=0.4]{ch8/beta_hat_s5_example.pdf}
\end{center}
\end{definition}

\begin{definition}[$\gamma(\textbf{s}_5)$]
We start from
\begin{align}
    \gamma(\textbf{x}_5) &= \left(\left< \hat{\alpha}_5 | x \right> \right)^2 \\
    \left| \gamma_5 \right> &= \sum_{k=1}^{N_v} g_k \left| \psi_k \right>
\end{align}
Then
\begin{equation}
    \gamma(\textbf{s}_5) =
    \begin{bmatrix}
        g^2_1 & g^2_2 & \cdots & g^2_{N_v}
    \end{bmatrix}
\end{equation}
\begin{center}
    \includegraphics[scale=0.4]{ch8/gamma5_example.pdf}
\end{center}
\end{definition}

\begin{definition}[$\beta(\textbf{s}_4)$]
\begin{equation}
    e^{-\textbf{H}\Delta t}\textbf{y}_{5}\left| \hat{\beta}_{5} \right>
\end{equation}
\begin{center}
    \includegraphics[scale=0.4]{ch8/y_beta5.pdf}
\end{center}
\begin{equation}
    \left| \hat{\beta}_4 \right> = \frac{e^{-\textbf{H}\Delta t}\textbf{y}_{5}\left| \hat{\beta}_{5} \right>}{\sqrt{r_5}}
\end{equation}
\begin{center}
    \includegraphics[scale=0.4]{ch8/beta4_example.pdf}
\end{center}
\end{definition}

\begin{definition}[$\beta(\textbf{s}_3)$]
\begin{equation}
    e^{-\textbf{H}\Delta t}\textbf{y}_{4}\left| \hat{\beta}_{4} \right>
\end{equation}
\begin{center}
    \includegraphics[scale=0.4]{ch8/y_beta4.pdf}
\end{center}
\begin{equation}
    \left| \hat{\beta}_3 \right> = \frac{e^{-\textbf{H}\Delta t}\textbf{y}_{4}\left| \hat{\beta}_{4} \right>}{\sqrt{r_4}}
\end{equation}
\begin{center}
    \includegraphics[scale=0.4]{ch8/beta3_example.pdf}
\end{center}
\end{definition}


% !TEX root = hott_intro.tex

\section{The circle}
\index{circle|(}
\index{inductive type!circle|(}

We have seen inductive types, in which we describe a type by its constructors and an induction principle that allows us to construct sections of dependent types. Inductive types are freely generated by their constructors, which describe how we can construct their terms.

However, many familiar constructions in algebra involve the construction of algebras by generators and relations.
For example, the free abelian group with two generators is described as the group with generators $x$ and $y$, and the relation $xy=yx$.

In this chapter we introduce higher inductive types\index{higher inductive type!circle}, where we follow a similar idea: to allow in the specification of inductive types not only \emph{point constructors}, but also \emph{path constructors} that give us relations between the point constructors.
The ideas behind the definition of higher inductive types are introduced by studying the simplest non-trivial example: the \emph{circle}.

\subsection{The induction principle of the circle}
The \emph{circle} is defined as a higher inductive type $\sphere{1}$\index{S 1@{$\sphere{1}$}|see {circle}} that comes equipped with\index{base@{$\base$}}\index{loop@{$\lloop$}}\index{circle!base@{$\base$}}\index{circle!loop@{$\lloop$}}
\begin{align*}
\base & : \sphere{1} \\
\lloop & : \id{\base}{\base}.
\end{align*}
Just like for ordinary inductive types, the induction principle for higher inductive types provides us with a way of constructing sections of dependent types. However, we need to take the \emph{path constructor}\index{path constructor} $\lloop$ into account in the induction principle.

By applying a section $f:\prd{x:\sphere{1}}P(x)$ to the base point of the circle, we obtain a term $f(\base):P(\base)$. Moreover, using the dependent action on paths\index{dependent action on paths} of $f$ of \cref{defn:apd} we also obtain for any dependent function $f:\prd{x:\sphere{1}}P(x)$ a path
\begin{align*}
\apd{f}{\lloop} & : \id{\tr_P(\lloop,f(\base))}{f(\base)}
\end{align*}
in the fiber $P(\base)$.

\begin{defn}
Let $P$ be a type family over the circle. The \define{dependent action on generators}\index{dependent action on generators!for the circle} is the map\index{dgen_S1@{$\dgen_{\sphere{1}}$}}
\begin{equation}\label{eq:dgen_circle}
\dgen_{\sphere{1}}:\Big(\prd{x:\sphere{1}}P(x)\Big)\to\Big(\sm{y:P(\base)}\id{\tr_P(\lloop,y)}{y}\Big)
\end{equation}
given by $\dgen_{\sphere{1}}(f)\defeq\pairr{f(\base),\apd{f}{\lloop}}$.
\end{defn}

We now give the full specification of the circle.

\begin{defn}
The \define{circle}\index{circle} is a type $\sphere{1}$\index{S 1@{$\sphere{1}$}} that comes equipped with\index{base@{$\base$}}\index{loop@{$\lloop$}}
\begin{align*}
\base & : \sphere{1} \\
\lloop & : \id{\base}{\base},
\end{align*}
and satisfies the \define{induction principle of the circle}\index{induction principle!of the circle}, which provides for each type family $P$ over $\sphere{1}$ a map
\begin{equation*}
\ind{\sphere{1}}:\Big(\sm{y:P(\base)}\id{\tr_P(\lloop,y)}{y}\Big)\to \Big(\prd{x:\sphere{1}}P(x)\Big),
\end{equation*}
and a homotopy witnessing that $\ind{\sphere{1}}$ is a section of $\dgen_{\sphere{1}}$
\begin{equation*}
\comphtpy{\sphere{1}}:\dgen_{\sphere{1}}\circ \ind{\sphere{1}}\htpy \idfunc
\end{equation*}
for the computation rule\index{computation rules!of the circle}.
\end{defn}

\begin{rmk}\label{rmk:circle-induction}
  The type of identifications $(y,p)=(y',p')$ in the type
  \begin{equation*}
    \sm{y:P(\base)}\tr_P(\lloop,y)=y
  \end{equation*}
  is equivalent to the type of pairs $(\alpha,\beta)$ consisting of an identification $\alpha:y=y'$, and an identification $\beta$ witnessing that the square
  \begin{equation*}
    \begin{tikzcd}[column sep=6em]
      \tr_P(\lloop,y) \arrow[d,equals,swap,"p"] \arrow[r,equals,"\ap{\tr_P(\lloop)}{\alpha}"] & \tr_P(\lloop,y') \arrow[d,equals,"{p'}"] \\
      y \arrow[r,equals,swap,"\alpha"] & y'
    \end{tikzcd}
  \end{equation*}
  commutes. Therefore it follows from the induction principle of the circle that for any $(y,p):\sm{y:P(\base)}\tr_P(\lloop,y)=y$, there is a dependent function $f:\prd{x:\sphere{1}}P(x)$ equipped with an identification
  \begin{equation*}
    \alpha : f(\base)=y,
  \end{equation*}
  and an identification $\beta$ witnessing that the square
  \begin{equation*}
    \begin{tikzcd}[column sep=6em]
      \tr_P(\lloop,f(\base)) \arrow[d,equals,swap,"{\apd{f}{\lloop}}"] \arrow[r,equals,"\ap{\tr_P(\lloop)}{\alpha}"] & \tr_P(\lloop,y) \arrow[d,equals,"{p}"] \\
      f(\base) \arrow[r,equals,swap,"\alpha"] & y
    \end{tikzcd}
  \end{equation*}
  commutes.
\end{rmk}

\subsection{The (dependent) universal property of the circle}
\subsectionmark{The universal property of the circle}

Our goal is now to use the induction principle of the circle to derive the \define{universal property}\index{universal property!of the circle} of the circle. This universal property states that, for any type $X$ the canonical map
\begin{equation*}
  \Big(\sphere{1}\to X\Big)\to\Big(\sm{x:X}x=x\Big)
\end{equation*}
given by $f\mapsto(f(\base),\ap{f}{\lloop})$ is an equivalence. It turns out that it is easier to prove the \define{dependent universal property}\index{dependent universal property!of the circle} first. The dependent universal property states that for any type family $P$ over the circle, the canonical map
\begin{equation*}
  \Big(\prd{x:\sphere{1}}P(x)\Big)\to\Big(\sm{y:P(\base)}\tr_P(\lloop,y)=y\Big)
\end{equation*}
given by $f\mapsto(f(\base),\apd{f}{\lloop})$ is an equivalence.

\begin{thm}\label{thm:circle-dependent-universal-property}
  For any type family $P$ over the circle, the map
  \begin{equation*}
    \Big(\prd{x:\sphere{1}}P(x)\Big)
    \to
    \Big(\sm{y:P(\base)}\tr_P(\lloop,y)=y\Big)
  \end{equation*}
  given by $f\mapsto(f(\base),\apd{f}{\lloop})$ is an equivalence.
\end{thm}

\begin{proof}
  By the induction principle of the circle we know that the map has a section, i.e., we have
  \begin{align*}
    \ind{\sphere{1}} & : \Big(\sm{y:P(\base)}\tr_P(\lloop,y)=y\Big) \to \Big(\prd{x:\sphere{1}}P(x)\Big) \\
    \comphtpy{\sphere{1}} & : \dgen_{\sphere{1}}\circ\ind{\sphere{1}}\htpy\idfunc
  \end{align*}
  Therefore it remains to construct a homotopy
  \begin{equation*}
    \ind{\sphere{1}}\circ\dgen_{\sphere{1}}\htpy\idfunc.
  \end{equation*}
  Thus, for any $f:\prd{x:\sphere{1}}P(x)$ our task is to construct an identification
  \begin{equation*}
    \ind{\sphere{1}}(\dgen_{\sphere{1}}(f))=f.
  \end{equation*}
  By function extensionality it suffices to construct a homotopy
  \begin{equation*}
    \prd{x:\sphere{1}} \ind{\sphere{1}}(\dgen_{\sphere{1}}(f))(x)= f(x).
  \end{equation*}
  We proceed by the induction principle of the circle using the family of types $E_{g,f}(x)\defeq g(x)=f(x)$ indexed by $x:\sphere{1}$, where $g$ is the function
  \begin{equation*}
    g\defeq\ind{\sphere{1}}(\dgen_{\sphere{1}}(f)).
  \end{equation*}
  Thus, it suffices to construct
  \begin{align*}
    \alpha & : g(\base)=f(\base)\\
    \beta  & : \tr_{E_{g,f}}(\lloop,\alpha)=\alpha.
  \end{align*}
  An argument by path induction on $p$ yields that
  \begin{equation*}
    \Big(\ct{\apd{g}{p}}{r}=\ct{\ap{\tr_P(p)}{q}}{\apd{f}{p}}\Big)\to\Big(\tr_{E_{g,f}}(p,q)=r\Big),
  \end{equation*}
  for any $f,g:\prd{x:X}P(x)$ and any $p:x=x'$, $q:g(x)=f(x)$ and $r:g(x')=f(x')$.
  Therefore it suffices to construct an identification $\alpha:g(\base)=f(\base)$ equipped with an identification $\beta$ witnessing that the square
  \begin{equation*}
    \begin{tikzcd}[column sep=6em]
      \tr_P(\lloop,g(\base)) \arrow[d,equals,swap,"\apd{g}{\lloop}"] \arrow[r,equals,"\ap{\tr_P(\lloop)}{\alpha}"] & \tr_P(\lloop,f(\base)) \arrow[d,equals,"\apd{f}{\lloop}"] \\
      g(\base) \arrow[r,equals,swap,"\alpha"] & f(\base)"
    \end{tikzcd}
  \end{equation*}
  commutes. Notice that we get exactly such a pair $(\alpha,\beta)$ from the computation rule of the circle, by \cref{rmk:circle-induction}.
\end{proof}

As a corollary we obtain the following uniqueness principle for dependent functions defined by the induction principle of the circle.

\begin{cor}
  Consider a type family $P$ over the circle, and let
  \begin{align*}
    y & : P(\base) \\
    p & : \tr_{P}(\lloop,y)=y.
  \end{align*}
  Then the type of functions $f:\prd{x:\sphere{1}}P(x)$ equipped with an identification
  \begin{equation*}
    \alpha: f(\base)=y
  \end{equation*}
  and an identification $\beta$ witnessing that the square
  \begin{equation*}
    \begin{tikzcd}[column sep=6em]
      \tr_P(\lloop,f(\base)) \arrow[d,equals,swap,"{\apd{f}{\lloop}}"] \arrow[r,equals,"\ap{\tr_P(\lloop)}{\alpha}"] & \tr_P(\lloop,y) \arrow[d,equals,"{p}"] \\
      f(\base) \arrow[r,equals,swap,"\alpha"] & y
    \end{tikzcd}
  \end{equation*}
  commutes, is contractible.
\end{cor}

Now we use the dependent universal property to derive the ordinary universal property of the circle. It would be tempting to say that it is a direct corollary, but we need to address the transport that occurs in the dependent universal property.

\begin{thm}\label{thm:circle_up}
For each type $X$, the \define{action on generators}\index{action on generators!for the circle}\index{gen_S1@{$\mathsf{gen}_{\sphere{1}}$}}
\begin{equation*}
\mathsf{gen}_{\sphere{1}}:(\sphere{1}\to X)\to \sm{x:X}x=x
\end{equation*}
given by $f\mapsto (f(\base),\ap{f}{\lloop})$ is an equivalence.
\end{thm}

\begin{proof}
  We prove the claim by constructing a commuting triangle
  \begin{equation*}
    \begin{tikzcd}[column sep=-2em]
      \phantom{\Big(\sm{x:X}\tr_{\const_X}(\lloop,x)=x\Big)} & (\sphere{1}\to X) \arrow[dl,swap,"\gen_{\sphere{1}}"] \arrow[dr,"\dgen_{\sphere{1}}"] \\
      \Big(\sm{x:X}x=x\Big) \arrow[rr,swap,"\simeq"] & & \Big(\sm{x:X}\tr_{\const_X}(\lloop,x)=x\Big)
    \end{tikzcd}
  \end{equation*}
  in which the bottom map is an equivalence. Indeed, once we have such a triangle, we use the fact from \cref{thm:circle-dependent-universal-property} that $\dgen_{\sphere{1}}$ is an equivalence to conclude that $\gen_{\sphere{1}}$ is an equivalence.

  To construct the bottom map, we first observe that for any constant type family $\const_B$ over a type $A$, any $p:a=a'$ in $A$, and any $b:B$, there is an identification
  \begin{equation*}
    \mathsf{tr\usc{}const}_B(p,b)=b.
  \end{equation*}
  This identification is easily constructed by path induction on $p$. Now we construct the bottom map as the induced map on total spaces of the family of maps
  \begin{equation*}
    l\mapsto \ct{\mathsf{tr\usc{}const}_X(\lloop,x)}{l},
  \end{equation*}
  indexed by $x:X$. Since concatenating by a path is an equivalence, it follows by \cref{thm:fib_equiv} that the induced map on total spaces is indeed an equivalence.

  To show that the triangle commutes, it suffices to construct for any $f:\sphere{1}\to X$ an identification witnessing that the triangle
  \begin{equation*}
    \begin{tikzcd}[column sep=1em]
      \tr_{\const_X}(\lloop,f(\base)) \arrow[dr,equals,swap,"\apd{f}{\lloop}"] \arrow[rr,equals,"{\mathsf{tr\usc{}const}_X(\lloop,f(\base))}"] & & f(\base) \arrow[dl,equals,"\ap{f}{\lloop}"] \\
      & f(\base) & \phantom{\tr_{\const_X}(\lloop,f(\base))}
    \end{tikzcd}
  \end{equation*}
  commutes. This again follows from general considerations: for any $f:A\to B$ and any $p:a=a'$ in $A$, the triangle
  \begin{equation*}
    \begin{tikzcd}[column sep=1em]
      \tr_{\const_B}(p,f(a)) \arrow[dr,equals,swap,"\apd{f}{p}"] \arrow[rr,equals,"{\mathsf{tr\usc{}const}_B(p,f(a))}"] & & f(a) \arrow[dl,equals,"\ap{f}{p}"] \\
      & f(a') & \phantom{\tr_{\const_B}(p,f(a))}
    \end{tikzcd}
  \end{equation*}
  commutes by path induction on $p$.
\end{proof}

\begin{cor}
  For any loop $l:x=x$ in a type $X$, the type of maps $f:\sphere{1}\to X$ equipped with an identification
  \begin{equation*}
    \alpha : f(\base)=x
  \end{equation*}
  and an identification $\beta$ witnessing that the square
  \begin{equation*}
    \begin{tikzcd}
      f(\base) \arrow[r,equals,"\alpha"] \arrow[d,equals,swap,"\ap{f}{\lloop}"] & x \arrow[d,equals,"l"] \\
      f(\base) \arrow[r,equals,swap,"\alpha"] & x
    \end{tikzcd}
  \end{equation*}
  commutes, is contractible.
\end{cor}

\subsection{Multiplication on the circle}
\label{sec:mulcircle}

One way the circle arises classically, is as the set of complex numbers at distance $1$ from the origin. It is an elementary fact that $|xy|=|x||y|$ for any two complex numbers $x,y\in\mathbb{C}$, so it follows that when we multiply two complex numbers that both lie on the unit circle, then the result lies again on the unit circle. Thus, using complex multiplication we see that there is a multiplication operation on the circle. And there is a shadow of this operation in type theory, even though our circle arises in a very different way!

\begin{defn}\label{defn:mul-circle}
  We define a binary operation
\begin{equation*}
  \mulcircle : \sphere{1}\to(\sphere{1}\to\sphere{1}).
\end{equation*}
\end{defn}

\begin{proof}[Construction]
  Using the universal property of the circle, we define $\mulcircle$ as the unique map $\sphere{1}\to(\sphere{1}\to\sphere{1})$ equipped with an identification
  \begin{equation*}
    \basemulcircle :\mulcircle(\base)=\idfunc
  \end{equation*}
  and an identification $\loopmulcircle$ witnessing that the square
  \begin{equation*}
    \begin{tikzcd}[column sep=huge]
      \mulcircle(\base) \arrow[r,equals,"\basemulcircle"] \arrow[d,equals,swap,"\ap{\mulcircle}{\lloop}"] & \idfunc \arrow[d,equals,"\eqhtpy(\htpyidcircle)"] \\
      \mulcircle(\base) \arrow[r,equals,swap,"\basemulcircle"] & \idfunc
  \end{tikzcd}
  \end{equation*}
  commutes. Note that in this square we have a homotopy $\htpyidcircle:\idfunc\htpy\idfunc$, which is not yet defined. We  use the dependent universal property of the circle with respect to the family $E_{\idfunc,\idfunc}$ given by
  \begin{equation*}
    E_{\idfunc,\idfunc}(x) \defeq (x=x),
  \end{equation*}
  to define $\htpyidcircle$ as the unique homotopy equipped with an identification
  \begin{equation*}
    \basehtpyidcircle : \htpyidcircle(\base)=\lloop
  \end{equation*}
  and an identification $\loophtpyidcircle$ witnessing that the square
  \begin{equation*}
    \begin{tikzcd}[column sep=8em]
      \tr_{E_{\idfunc,\idfunc}}(\lloop,\htpyidcircle(\base)) \arrow[r,equals,"\ap{\tr_{E_{\idfunc,\idfunc}}(\lloop)}{\basehtpyidcircle}"] \arrow[d,equals,swap,"\apd{\htpyidcircle}{\lloop}"] & \tr_{E_{\idfunc,\idfunc}}(\lloop,\lloop) \arrow[d,equals,"\gamma"] \\
      \htpyidcircle(\base) \arrow[r,equals,swap,"\basehtpyidcircle"] & \lloop
    \end{tikzcd}
  \end{equation*}
  commutes. Now it remains to define the path $\gamma:\tr_{E_{\idfunc,\idfunc}}(\lloop,\lloop)=\lloop$ in the above square. To proceed, we first observe that a simple path induction argument yields a function
  \begin{equation*}
    \Big(\ct{p}{r}=\ct{q}{p}\Big)\to\Big(\tr_{E_{\idfunc,\idfunc}}(p,q)=r\Big),
  \end{equation*}
  for any $p:\base=x$, $q:\base=\base$ and $r:x=x$. In particular, we have a function
  \begin{equation*}
    \Big(\ct{\lloop}{\lloop}=\ct{\lloop}{\lloop}\Big)\to\Big(\tr_{E_{\idfunc,\idfunc}}(\lloop,\lloop)=\lloop\Big).
  \end{equation*}
  Now we apply this function to $\refl{\ct{\lloop}{\lloop}}$ to obtain the desired identification
  \begin{equation*}
    \gamma:\tr_{E_{\idfunc,\idfunc}}(\lloop,\lloop)=\lloop.\qedhere
  \end{equation*}
\end{proof}

\begin{rmk}
  In the definition of $H:\idfunc\htpy\idfunc$ above, it is important that we didn't choose $H$ to be $\reflhtpy$. If we had done so, the resulting operation would be homotopic to $x,y\mapsto y$, which is clearly not what we had in mind with the multiplication operation on the circle. See also \cref{ex:circle-constant}.
\end{rmk}

The left unit law $\mulcircle(\base,x)=x$ holds by the computation rule of the universal property. More precisely, we define
\begin{equation*}
  \leftunit_{\sphere{1}}\defeq \htpyeq(\basemulcircle).
\end{equation*}
For the right unit law, however, we need to give a separate argument that is surprisingly involved, because all the aspects of the definition of $\mulcircle$ will come out and play their part.

\begin{thm}
  The multiplication operation on the circle satisfies the right unit law, i.e., we have
  \begin{equation*}
    \mulcircle(x,\base)=x
  \end{equation*}
  for any $x:\sphere{1}$.
\end{thm}

\begin{proof}
  The proof is by induction on the circle. In the base case we use the left unit law
  \begin{equation*}
    \leftunit_{\sphere{1}}(\base):\mulcircle(\base,\base)=\base.
  \end{equation*}
  Thus, it remains to show that
  \begin{equation*}
    \tr_P(\lloop,\leftunit_{\sphere{1}}(\base))=\leftunit_{\sphere{1}}(\base),
  \end{equation*}
  where $P$ is the family over the circle given by
  \begin{equation*}
    P(x) \defeq \mulcircle(x,\base)=x.
  \end{equation*}
  Now we observe that there is a function
  \begin{equation*}
    \Big(\ct{\htpyeq(\ap{\mulcircle}{p})(\base)}{r}=\ct{q}{p}\Big)\to\Big(\tr_{P}(p,q)=r\Big),
  \end{equation*}
  for any
  \begin{align*}
    p & : \base=x \\
    q & : \mulcircle(\base,\base)=\base \\
    r & : \mulcircle(x,\base)=x.
  \end{align*}
  Thus we see that, in order to construct an identification
  \begin{equation*}
    \tr_{P}(\lloop,\leftunit_{\sphere{1}})=\leftunit_{\sphere{1}},
  \end{equation*}
  it suffices to show that the square
  \begin{equation*}
    \begin{tikzcd}[column sep=8em]
      \mulcircle(\base,\base) \arrow[d,equals,swap,"\htpyeq(\ap{\mulcircle}{\lloop})(\base)"] \arrow[r,equals,"\leftunit_{\sphere{1}}(\base)"] & \base \arrow[d,equals,"\lloop"] \\
      \mulcircle(\base,\base) \arrow[r,equals,swap,"\leftunit_{\sphere{1}}(\base)"] & \base
    \end{tikzcd}
  \end{equation*}
  commutes. Now we note that we have an identification $H(\base)=\lloop$. It is indeed at this point, where it is important that $H$ is not the trivial homotopy, because now we can proceed by observing that the above square commutes if and only if the square
  \begin{equation*}
    \begin{tikzcd}[column sep=12em]
      \mulcircle(\base,\base) \arrow[d,equals,swap,"\htpyeq(\ap{\mulcircle}{\lloop})(\base)"] \arrow[r,equals,"\htpyeq(\basemulcircle)(\base)"] & \base \arrow[d,equals,"H(\base)"] \\
      \mulcircle(\base,\base) \arrow[r,equals,swap,"\htpyeq(\basemulcircle)(\base)"] & \base
    \end{tikzcd}
  \end{equation*}
  commutes. The commutativity of this square easily follows from the identification $\loopmulcircle$ constructed in \cref{defn:mul-circle}.
\end{proof}

\begin{exercises}
  \exercise \label{ex:circle-connected}
  \begin{subexenum}
  \item Let $P:\sphere{1}\to\prop$ be a family of propositions over the circle. Show that
    \begin{equation*}
      P(\base)\to\prd{x:\sphere{1}}P(x).
    \end{equation*}
    In this sense the circle is \emph{connected}.
  \item Show that any embedding $m:\sphere{1}\to\sphere{1}$ is an equivalence.
  \item Show that for any embedding $m:X\to\sphere{1}$, there is a proposition $P$ and an equivalence $e:\eqv{X}{\sphere{1}\times P}$ for which the triangle
    \begin{equation*}
      \begin{tikzcd}[column sep=0]
        X \arrow[dr,swap,"m"] \arrow[rr,"e"] & & \sphere{1}\times P \arrow[dl,"\proj 1"] \\
        \phantom{\sphere{1}\times P} & \sphere{1}
      \end{tikzcd}
    \end{equation*}
    commutes. In other words, all the embeddings into the circle are of the form $\sphere{1}\times P\to \sphere{1}$.
  \end{subexenum}
  \exercise \label{ex:circle-constant}
  Show that for any type $X$ and any $x:X$, the map
  \begin{equation*}
    \ind{\sphere{1}}(x,\refl{x}):\sphere{1}\to X
  \end{equation*}
  is homotopic to the constant map $\mathsf{const}_x$.
  \exercise \label{ex:mulcircle-is-equiv}
  \begin{subexenum}
  \item Show that for any $x:\sphere{1}$, both functions
    \begin{equation*}
      \mulcircle(x,\blank)\qquad\text{and}\qquad\mulcircle(\blank,x)
    \end{equation*}
    are equivalences.
  \item Show that the function
    \begin{equation*}
      \mulcircle : \sphere{1}\to(\sphere{1}\to\sphere{1})
    \end{equation*}
    is an embedding. Compare this fact with \cref{ex:groupop-embedding}.
  \item Show that multiplication on the circle is associative and commutative.
  \end{subexenum}
  \exercise \label{ex:circle_connected}
  \begin{subexenum}
  \item Show that a type $X$ is a set if and only if the map
    \begin{equation*}
      \lam{x}{t} x : X \to (\sphere{1}\to X)
    \end{equation*}
is an equivalence.
\item Show that a type $X$ is a set if and only if the map
  \begin{equation*}
    \lam{f}f(\base) : (\sphere{1}\to X)\to X
  \end{equation*}
  is an equivalence.
  \end{subexenum}
  \exercise Show that the multiplicative operation on the circle is commutative, i.e.~construct an identification
  \begin{equation*}
    \mulcircle(x,y)=\mulcircle(y,x).
  \end{equation*}
  for every $x,y:\sphere{1}$.
  \exercise Show that the circle, equipped with the multiplicative operation $\mulcircle$ is an abelian group, i.e.~construct an inverse operation
  \begin{equation*}
    \invcircle : \sphere{1}\to\sphere{1}
  \end{equation*}
  and construct identifications
  \begin{align*}
    \leftinv_{\sphere{1}} & : \mulcircle(\invcircle(x),x) = \base \\
    \rightinv_{\sphere{1}} & : \mulcircle(x,\invcircle(x)) = \base.
  \end{align*}
  Moreover, show that the square
  \begin{equation*}
    \begin{tikzcd}
      \invcircle(\base) \arrow[d,equals] \arrow[r,equals] & \mulcircle(\base,\invcircle(\base)) \arrow[d,equals] \\
      \mulcircle(\invcircle(\base),\base) \arrow[r,equals] & \base
    \end{tikzcd}
  \end{equation*}
  commutes.
  \exercise Show that for any multiplicative operation
  \begin{equation*}
    \mu:\sphere{1}\to(\sphere{1}\to\sphere{1})
  \end{equation*}
  that satisfies the condition that $\mu(x,\blank)$ and $\mu(\blank,x)$ are equivalences for any $x:\sphere{1}$, there is a term $e:\sphere{1}$ such that
  \begin{equation*}
    \mu(x,y)=\mulcircle(x,\mulcircle(\bar{e},y))
  \end{equation*}
  for every $x,y:\sphere{1}$, where $\bar{e}\defeq\invcircle(e)$ is the complex conjucation of $e$ on $\sphere{1}$.
\end{exercises}


\documentclass{standalone}
\begin{document}
	\chapter{Inequalities}
	\section{Quadratic Inequalities}
	\begin{example}
		Solve the inequality $x^2-2x>3$
	\end{example}

	\begin{center}
		$$x^2-2x>3$$
		$$\implies x^2-2x-3>0$$
		$$\text{Let }x^2-2x-3=0$$
		$$\implies(x-3)(x+1)=0$$
	\end{center}
	\begin{center}
		\begin{tikzpicture}
			\begin{axis}[
				width=8cm,
				height=6cm,
				axis line style={-},
				xmin=-4,
				xmax=4,
				ymin=-4,
				ymax=5,
				xtick={-1,3}, % remove all ticks from x-axis
				ytick={-3}, % ditto for y-axis
				xlabel=$x$,
				ylabel=$y$,
				axis lines=center, % default is to make a box around the axis
				samples=100]
				\addplot [black] {x^2 - 2*x - 3}
				;
			\end{axis}
		\end{tikzpicture}
	\end{center}
	\hrulefill
	\begin{example}
		Solve the inequality $\quad \dfrac{2x^2}{5} \leq \dfrac{7x+10}{2}$
	\end{example}
	\begin{center}
		$$4x^2 \leq 35x + 50$$
		$$\implies 4x^2-35x-50\leq 0$$
		$$\text{Let }\quad 4x^2-35x-50=0$$
		$$(4x+5)(x-10)=0$$
		\begin{tikzpicture}
			\begin{axis}[
				width=8cm,
				height=6cm,
				axis line style={-},
				xmin=-2,
				xmax=11,
				ymin=-127,
				ymax=50,
				xtick={-5/4,10},
				ytick={-50},
				xlabel=$x$,
				ylabel=$y$,
				axis lines=center,
				samples=100]
				\addplot [black] coordinates{
					(-1,-17)(0,-48)(1,-79)(2,-110)(3,-141)(4,-172)(5,-203)(6,-234)(7,-265)(8,-296)(9,-327)(10,-358)(11,-389)
				};
			\end{axis}
		\end{tikzpicture}
	\end{center}

	\hrulefill
	\end{document}


\lab{NumPy Visual Guide}{NumPy Visual Guide}
\label{appendix:numpy-visual-guide}
\objective{NumPy operations can be difficult to visualize, but the concepts are straightforward.
This appendix provides visual demonstrations of how NumPy arrays are used with slicing syntax, stacking, broadcasting, and axis-specific operations.
Though these visualizations are for 1- or 2-dimensional arrays, the concepts can be extended to $n$-dimensional arrays.
% See Lab \ref{lab:NumPy} for an introduction to NumPy operations and synatx.
}

\section*{Data Access} % ======================================================

The entries of a 2-D array are the rows of the matrix (as 1-D arrays).
To access a single entry, enter the row index, a comma, and the column index.
Remember that indexing begins with $0$.

\begin{align*}
A[0] = \left[\begin{array}{rrrrr}
\tikzmarkin{row1}\times & \times & \times & \times & \times\tikzmarkend{row1}\\
\times & \times & \times & \times & \times\\
\times & \times & \times & \times & \times\\
\times & \times & \times & \times & \times\\
\end{array}\right]
&&
A[2,1] = \left[\begin{array}{rrrrr}
\times & \times & \times & \times & \times\\
\times & \times & \times & \times & \times\\
\times & \tikzmarkin{entry} \times \tikzmarkend{entry} & \times & \times & \times\\
\times & \times & \times & \times & \times
\end{array}\right]
\end{align*}

\section*{Slicing} % ==========================================================

A lone colon extracts an entire row or column from a 2-D array.
The syntax \li{[a:b]} can be read as ``the $a^{th}$ entry up to (but not including) the $b^{th}$ entry.''
Similarly, \li{[a:]} means ``the $a^{th}$ entry to the end'' and \li{[:b]} means ``everything up to (but not including) the $b^{th}$ entry.''

\begin{align*}
\text{\li{A[1]}} = \text{\li{A[1,:]}} = \left[\begin{array}{rrrrr}
\times & \times & \times & \times & \times\\
\tikzmarkin{row2}\times & \times & \times & \times & \times\tikzmarkend{row2}\\
\times & \times & \times & \times & \times\\
\times & \times & \times & \times & \times\\
\end{array}\right]
&&
\text{\li{A[:,2]}} = \left[\begin{array}{rrrrr}
\times & \times & \tikzmarkin{col}\times & \times & \times\\
\times & \times & \times & \times & \times\\
\times & \times & \times & \times & \times\\
\times & \times & \times\tikzmarkend{col} & \times & \times
\end{array}\right]
\end{align*}

\begin{align*}
\text{\li{A[1:,:2]}} = \left[\begin{array}{rrrrr}
\times & \times & \times & \times & \times\\
\tikzmarkin{block}\times & \times & \times & \times & \times\\
\times & \times & \times & \times & \times\\
\times & \times\tikzmarkend{block} & \times & \times & \times
\end{array}\right]
&&
\text{\li{A[1:-1,1:-1]}} = \left[\begin{array}{rrrrr}
\times & \times & \times & \times & \times\\
\times & \tikzmarkin{interior} \times & \times & \times & \times\\
\times & \times & \times & \times \tikzmarkend{interior} & \times\\
\times & \times & \times & \times & \times\end{array}\right]
\end{align*}

\section*{Stacking} % =========================================================

\li{np.hstack()} stacks sequence of arrays horizontally and \li{np.vstack()} stacks a sequence of arrays vertically.

\begin{align*}
A = \left[\begin{array}{ccc}
\textcolor{blue}{\times}&\textcolor{blue}{\times}&\textcolor{blue}{\times}\\
\textcolor{blue}{\times}&\textcolor{blue}{\times}&\textcolor{blue}{\times}\\
\textcolor{blue}{\times}&\textcolor{blue}{\times}&\textcolor{blue}{\times}
\end{array}\right]
&&
B = \left[\begin{array}{ccc}
\textcolor{red}{*} & \textcolor{red}{*} & \textcolor{red}{*} \\
\textcolor{red}{*} & \textcolor{red}{*} & \textcolor{red}{*} \\
\textcolor{red}{*} & \textcolor{red}{*} & \textcolor{red}{*}
\end{array}\right]
\end{align*}

\begin{align*}
\text{\li{np.hstack((A,B,A))}} =
\left[\begin{array}{ccccccccc}
\textcolor{blue}{\times}&\textcolor{blue}{\times}&\textcolor{blue}{\times}&
\textcolor{red}{*} & \textcolor{red}{*} & \textcolor{red}{*}&
\textcolor{blue}{\times}&\textcolor{blue}{\times}&\textcolor{blue}{\times}\\
\textcolor{blue}{\times}&\textcolor{blue}{\times}&\textcolor{blue}{\times}&
\textcolor{red}{*} & \textcolor{red}{*} & \textcolor{red}{*}&
\textcolor{blue}{\times}&\textcolor{blue}{\times}&\textcolor{blue}{\times}\\
\textcolor{blue}{\times}&\textcolor{blue}{\times}&\textcolor{blue}{\times}&
\textcolor{red}{*} & \textcolor{red}{*} & \textcolor{red}{*}&
\textcolor{blue}{\times}&\textcolor{blue}{\times}&\textcolor{blue}{\times}
\end{array}\right]
\end{align*}

\begin{align*}
\text{\li{np.vstack((A,B,A))}} =
\left[\begin{array}{ccc}
\textcolor{blue}{\times}&\textcolor{blue}{\times}&\textcolor{blue}{\times}\\
\textcolor{blue}{\times}&\textcolor{blue}{\times}&\textcolor{blue}{\times}\\
\textcolor{blue}{\times}&\textcolor{blue}{\times}&\textcolor{blue}{\times}\\
\textcolor{red}{*} & \textcolor{red}{*} & \textcolor{red}{*} \\
\textcolor{red}{*} & \textcolor{red}{*} & \textcolor{red}{*} \\
\textcolor{red}{*} & \textcolor{red}{*} & \textcolor{red}{*} \\
\textcolor{blue}{\times}&\textcolor{blue}{\times}&\textcolor{blue}{\times}\\
\textcolor{blue}{\times}&\textcolor{blue}{\times}&\textcolor{blue}{\times}\\
\textcolor{blue}{\times}&\textcolor{blue}{\times}&\textcolor{blue}{\times}
\end{array}\right]
\end{align*}
Because 1-D arrays are flat, \li{np.hstack()} concatenates 1-D arrays and \li{np.vstack()} stacks them vertically.
To make several 1-D arrays into the columns of a 2-D array, use \li{np.column_stack()}.

\begin{align*}
x = \left[\begin{array}{cccc}
\textcolor{blue}{\times}&\textcolor{blue}{\times}&\textcolor{blue}{\times}&\textcolor{blue}{\times}
\end{array}\right]
&&
y = \left[\begin{array}{cccc}
\textcolor{red}{*}&\textcolor{red}{*}&\textcolor{red}{*}&\textcolor{red}{*}\end{array}\right]
\end{align*}

\begin{align*}
\text{\li{np.hstack((x,y,x))}} =
\left[\begin{array}{cccccccccccc}
\textcolor{blue}{\times}&\textcolor{blue}{\times}&\textcolor{blue}{\times}&\textcolor{blue}{\times}&
\textcolor{red}{*}&\textcolor{red}{*}&\textcolor{red}{*}&\textcolor{red}{*}&
\textcolor{blue}{\times}&\textcolor{blue}{\times}&\textcolor{blue}{\times}&\textcolor{blue}{\times}
\end{array}\right]
\end{align*}

\begin{align*}
\text{\li{np.vstack((x,y,x))}} =
\left[\begin{array}{cccc}
\textcolor{blue}{\times}&\textcolor{blue}{\times}&\textcolor{blue}{\times}&\textcolor{blue}{\times}\\
\textcolor{red}{*}&\textcolor{red}{*}&\textcolor{red}{*}&\textcolor{red}{*}\\
\textcolor{blue}{\times}&\textcolor{blue}{\times}&\textcolor{blue}{\times}&\textcolor{blue}{\times}
\end{array}\right]
&&
\text{\li{np.column_stack((x,y,x))}} =
\left[\begin{array}{ccc}
\textcolor{blue}{\times}&\textcolor{red}{*}&\textcolor{blue}{\times}\\
\textcolor{blue}{\times}&\textcolor{red}{*}&\textcolor{blue}{\times}\\
\textcolor{blue}{\times}&\textcolor{red}{*}&\textcolor{blue}{\times}\\
\textcolor{blue}{\times}&\textcolor{red}{*}&\textcolor{blue}{\times}
\end{array}\right]
\end{align*}

\section*{Broadcasting} % =====================================================

NumPy automatically aligns arrays for component-wise operations whenever possible.
% The default behavior adds the first element to the first element of each row, the second element to the second element of each row, and so on.
See \url{http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html} for more in-depth examples and broadcasting rules.

\begin{align*}
A = \left[\begin{array}{ccc}
1 & 2 & 3\\
1 & 2 & 3\\
1 & 2 & 3\\
\end{array}\right]
&&
x = \left[\begin{array}{ccc}
10 & 20 & 30
\end{array}\right]
\end{align*}

\begin{align*}
\text{\li{A + x}}
&= \begin{blockarray}{ccc}
\begin{block}{[ccc]}
1 & 2 & 3\\
1 & 2 & 3\\
1 & 2 & 3\\
\end{block}
  & + &  \\
\begin{block}{[ccc]}
10 & 20 & 30\\
\end{block}
\end{blockarray}
&= \left[\begin{array}{ccc}
11 & 22 & 33\\
11 & 22 & 33\\
11 & 22 & 33
\end{array}\right]
\\ \\
\text{\li{A + np.vstack(x)}}
&= \left[\begin{array}{ccc}
1 & 2 & 3 \\
1 & 2 & 3 \\
1 & 2 & 3 \\
\end{array}\right]
+ \left[\begin{array}{c}
10 \\ 20 \\ 30\\
\end{array}\right]
&= \left[\begin{array}{ccc}
11 & 12 & 13\\
21 & 22 & 23\\
31 & 32 & 33\\
\end{array}\right]
\end{align*}

\section*{Operations along an Axis} % =========================================

Most array methods have an \li{axis} argument that allows an operation to be done along a given axis.
To compute the sum of each column, use \li{axis=0}; to compute the sum of each row, use \li{axis=1}.

\begin{align*}
A = \left[\begin{array}{cccc}
1 & 2 & 3 & 4\\
1 & 2 & 3 & 4\\
1 & 2 & 3 & 4\\
1 & 2 & 3 & 4
\end{array}\right]
\end{align*}

\begin{align*}
\text{\li{A.<<sum>>(axis=0)}} &= %np.array([sum(A[:,i] for i in xrange(A.shape[1]))])}} =
\left[\begin{array}{cccc}
\tikzmarkin{col1}1 & \tikzmarkin{col2}2 & \tikzmarkin{col3}3 & \tikzmarkin{col4}4\\
1 & 2 & 3 & 4\\
1 & 2 & 3 & 4\\
1\tikzmarkend{col1} & 2\tikzmarkend{col2} & 3\tikzmarkend{col3} & 4\tikzmarkend{col4}
\end{array}\right]
= \left[\begin{array}{cccc} 4 & 8 & 12 & 16 \end{array}\right]
\\ \\
\text{\li{A.<<sum>>(axis=1)}} &= %np.array([sum(A[i,:] for i in xrange(A.shape[0]))])}} =
\left[\begin{array}{cccc}
\tikzmarkin{rowA}1 & 2 & 3 & 4\tikzmarkend{rowA}\\
\tikzmarkin{rowB}1 & 2 & 3 & 4\tikzmarkend{rowB}\\
\tikzmarkin{rowC}1 & 2 & 3 & 4\tikzmarkend{rowC}\\
\tikzmarkin{rowD}1 & 2 & 3 & 4\tikzmarkend{rowD}\\
\end{array}\right]
= \left[\begin{array}{cccc} 10 & 10 & 10 & 10 \end{array}\right]
\end{align*}


\subsection{Mathematical Background and Cryptographic Definitions}
\label{ssec:math_def}

We let $\G_{1}$, $\G_{2}$, and $\G_{T}$ be cyclic groups of order $q$.
Let $g_{1}, h_{1}\in\G_{1}$ and $h_{2}\in\G_{2}$ be generators and
require that the discrete logarithm $\dlog_{g_{1}}h_{1}$ is unknown.
The groups we use were described in Sec.~\ref{ssec:pk_curve_specifics}.
We let $e:\G_{1}\times\G_{2}\to\G_{T}$ be an efficiently computable
nondegenerate bilinear pairing.
In our case, signatures will be elements of $\G_{1}$ while
public keys will be elements of $\G_{2}$.
See Table~\ref{tab:helper_funcs} for additional functions
that will be used in our algorithms.

\input{tables/helper_funcs.tex}

We are building a sidechain on top of Ethereum.
All of the validators will be required to have an Ethereum
public key.
The DKG algorithm requires us to index the participants
from $1$ to $n$.
To do so, we order the participants with respect to their sorted
Ethereum public keys.
Our algorithm will need an open broadcast channel;
this will take place via smart contracts on the Ethereum
network.

At times we will want to ensure

\begin{equation}
    e\parens{h_{1}^{\alpha},h_{2}} \overset{?}{=}
    e\parens{h_{1},h_{2}^{\beta}}.
\end{equation}

\noindent
Due to how \textsc{PairingCheck} is defined, we
will need to check the equivalent

\begin{equation}
    e\parens{h_{1}^{\alpha},h_{2}^{-1}}\cdot
        e\parens{h_{1},h_{2}^{\beta}}
        \overset{?}{=} 1.
\end{equation}

\noindent
If $h^{\alpha} = \parens{x,y}$, then
$\parens{h^{\alpha}}^{-1} = \parens{x,-y}$; this holds for all
elliptic curves.
For ease of notation, we set $\bar{h} = h^{-1}$.
It may be convenient to store both $h_{2}$ and $\bar{h}_{2}$.