\section{Momentum Equilibrium and Elbow Angle Optimization}
\label{sec:72methodology}

\minitoc[-7mm]{70mm}{4}

\noindent
In this section, we give an overview of the methodology of our approach.
We continue following the presentation of \cite{Valentin18Gradient}.

\subsection{From Muscle Forces to Equilibrium Angles}
\label{sec:721equilibrium}

\paragraph{Model inputs and outputs}

In the following, we regard simulations of the
human upper limb model described in \cref{sec:71model} as a black box,
which receives as its input
the elbow angle $\elbang \in \clint{\ang{10}, \ang{150}}$
and the activation parameters
$(\actT, \actB) \in \clint{\*0, \*1} = \clint{0, 1}^2$
of triceps and biceps.%
\footnote{%
  Here and in the following, the subscripts T, B, and L stand for
  triceps, biceps, and load, respectively.%
}
The outputs of the black box simulation are the forces
$\forceT(\elbang, \actT)$ and $\forceB(\elbang, \actB)$
that triceps and biceps exert.
These forces depend on the elbow angle as well as on the respective
activation parameter.
Gravitational forces due to the masses of bones or muscles
are neglected in this context.
However, we allow the specification of an external load $\forceL$,
which is applied to the end of the forearm.
This load may be the weight force of some object
that the arm is supposed to keep in position.

\paragraph{Moments and lever arms}

Each force exerts a \term{moment} (or \term{torque}) on the elbow joint.
The moments are the products of the forces $\forceX$
with the respective lever arms $\armX$
($X \in \{\mathrm{T}, \mathrm{B}, \mathrm{L}\}$).
The lever arms are approximated as in
\multicite{Roehrle16Two,Valentin18Gradient} by using
the tendon-displacement method of \cite{An84Determination}:
\begin{subequations}
  \begin{align}
    \armT(\elbang)
    &\ceq (-0.0009399 \{\elbang\}^2 + 0.1126 \{\elbang\} + 22.21)\;
    \si{\milli\meter},\\
    \armB(\elbang)
    &\ceq (-0.001482 \{\elbang\}^2 + 0.1776 \{\elbang\} + 35.02)\;
    \si{\milli\meter},\\
    \armL(\elbang)
    &\ceq \sin(\elbang) \cdot \SI{282.5}{\milli\meter},
  \end{align}
\end{subequations}
where $\{\elbang\}$ denotes the dimensionless value of $\elbang$
in degrees.
The lever arms are non-negative and the forces are signed, i.e.,
positive forces pull the forearm downwards and
negative forces pull it upwards.
In general, $\forceT, \forceL \ge \SI{0}{\newton}$ and
$\forceB \le \SI{0}{\newton}$.

\paragraph{Total moment and equilibrium elbow angle}

The \term{total moment} of the system is given by the function
\begin{subequations}
  \label{eq:totalMoment}
  \begin{gather}
    \moment_{\forceL,\actT,\actB}\colon
    \clint{\ang{10}, \ang{150}} \to \real,\\
    \moment_{\forceL,\actT,\actB}(\elbang)
    \ceq \forceT(\elbang, \actT) \armT(\elbang) +
    \forceB(\elbang, \actB) \armB(\elbang) +
    \forceL \armL(\elbang),
  \end{gather}
\end{subequations}
cf.\ \cite{Valentin18Gradient}.
The system is in \term{equilibrium}
if the total moment vanishes, i.e.,
$\moment_{\forceL,\actT,\actB}(\elbang) = \SI{0}{\newton\meter}$.
We call the corresponding angle $\elbang$ the
\term{equilibrium elbow angle}
for the load $\forceL$ and the activation parameters $\actT, \actB$.
To find this angle for a given load $\forceL$ and activation parameters
$\actT$ and $\actB$, we first note that
$\moment_{\forceL,\actT,\actB}$ may have zero, exactly one,
or multiple zeros in $\clint{\ang{10}, \ang{150}}$.
Hence, the inverse function evaluated at $\SI{0}{\newton\meter}$
is partially defined depending on the load and the activation parameters:
\begin{equation}
  \label{eq:equilibriumAngle}
  \equielbang{\forceL}\colon \actdomain{\forceL} \!\to
  \clint{\ang{10}, \ang{150}},\quad
  \actdomain{\forceL} \subset \clint{\*0, \*1},\quad
  \equielbang{\forceL}(\actT,\actB)
  \ceq (\moment_{\forceL,\actT,\actB})^{-1}(\SI{0}{\newton\meter}),
\end{equation}
which is well-defined whenever $\moment_{\forceL,\actT,\actB}$
has a unique root.
We approximate $\equielbang{\forceL}(\actT,\actB)$ with the Newton method
\multicite{Roehrle16Two,Valentin18Gradient}:
\begin{equation}
  \label{eq:newtonAngle}
  \elbang^{(j+1)}
  \ceq \elbang^{(j)} -
  \frac{
    \moment_{\forceL,\actT,\actB}(\elbang^{(j)})
  }{
    \partialderiv{\partialdiff{} \elbang}{\moment_{\forceL,\actT,\actB}}
    (\elbang^{(j)})
  },\quad
  j \in \nat,
\end{equation}
with an initial value
$\elbang^{(0)} \in \clint{\ang{10}, \ang{150}}$
and the stopping criterion of
$\abs{\moment_{\forceL,\actT,\actB}(\elbang^{(j)})} <
\SI{e-9}{\newton\meter}$.
We repeat the Newton method for the initial values
$\elbang^{(0)} = \ang{80}, \ang{40}, \ang{120}$
and use the first converged result
(i.e., we check if $\elbang^{(0)} = \ang{80}$ converges;
if not, we proceed with $\elbang^{(0)} = \ang{40}$, and so on).
If all three initial values do not converge,
we conclude that $(\actT, \actB) \notin \actdomain{\forceL}$.

\subsection{Optimization Problems}
\label{sec:722optimization}

\paragraph{General problem}

The general problem in our setting is as follows:
For a given external load $\forceL$ and a target elbow angle $\tarelbang$,
find activation parameters $(\actT, \actB) \in \clint{\*0, \*1}$
such that the target elbow angle is attained in the equilibrium,
i.e., $\equielbang{\forceL}(\actT,\actB) = \tarelbang$.
Example applications of such a scenario are medicine and robotics,
when a specific movement should be carried out.

\paragraph{List of optimization problems}

As discussed in \cref{sec:711models},
musculoskeletal systems with an antagonistic muscle pair
such as our human upper limb model are usually overdetermined.
This means that there are multiple solutions to this general problem.
As a remedy, one may solve one of the following two
optimization problems \cite{Valentin18Gradient}:

\begin{enumerate}[label=O\arabic*.,ref=O\arabic*,leftmargin=2.7em]
  \item
  \label{item:biomech2MinSum}
  For a given external load $\forceL$ and a target angle
  $\tarelbang \in \clint{\ang{10}, \ang{150}}$,
  find the activation parameters $(\actT, \actB) \in \clint{\*0, \*1}$
  such that $\actT + \actB$ is minimized under the constraint
  $\equielbang{\forceL}(\actT, \actB) = \tarelbang$.

  \item
  \label{item:biomech2MinDist}
  For a given external load $\forceL(t_2)$ for a time $t_2 > t_1$,
  a target angle $\tarelbang(t_2) \in \clint{\ang{10}, \ang{150}}$,
  and initial activation parameters
  $(\actT(t_1), \actB(t_1)) \in \clint{\*0, \*1}$,
  find new activation parameters
  $(\actT(t_2), \actB(t_2)) \in \clint{\*0, \*1}$ such that
  $(\actT(t_2) - \actT(t_1))^2 + (\actB(t_2) - \actB(t_1))^2$
  is minimized under the constraint
  $\equielbang{\forceL(t_2)}(\actT(t_2), \actB(t_2)) = \tarelbang(t_2)$.
\end{enumerate}

\noindent
The motivation of both problems is that the human body tries to
achieve a given movement with minimal energy effort.

\paragraph{Motivation of problem \ref{item:biomech2MinSum}}

For the first problem \ref{item:biomech2MinSum},
this effort is quantified by $\actT + \actB$,
i.e., the energy effort for each muscle is assumed to be proportional
to its activation parameter.

\paragraph{Motivation of problem \ref{item:biomech2MinDist}}

The second problem \ref{item:biomech2MinDist} is motivated as follows:
Before time $t = t_1$, the musculoskeletal system is in equilibrium for
the external load $\forceL(t_1)$,
activation parameters $\actT(t_1), \actB(t_1)$, and elbow angle
$\tarelbang(t_1) \ceq \equielbang{\forceL(t_1)}(\actT(t_1), \actB(t_1))$, i.e.,
$\moment_{\forceL(t_1),\actT(t_1),\actB(t_1)}(\tarelbang(t_1))
= \SI{0}{\newton\meter}$.
Directly after $t = t_1$,
the external force and/or the target angle is suddenly changed
to $\forceL(t_2)$ and $\tarelbang(t_2)$, respectively.
Consequently, triceps and biceps adapt their activation parameters
such that the musculoskeletal system returns to equilibrium
at some time $t = t_2 > t_1$.
Hence, we have to determine the new activation parameters
$\actT(t_2), \actB(t_2)$ such that
$\moment_{\forceL(t_2),\actT(t_2),\actB(t_2)}(\tarelbang(t_2))
= \SI{0}{\newton\meter}$.
Again, these parameters
$\actT(t_2)$ and $\actB(t_2)$ are not uniquely determined.
Therefore, we want to find the pair of activation parameters
that is closest (in terms of the Euclidean norm) to the initial
activation parameters $\actT(t_1), \actB(t_1)$.

\paragraph{Optimization method}

Problems \ref{item:biomech2MinSum} and \ref{item:biomech2MinDist}
are both constrained optimization problems.
For their solution, we employ the augmented Lagrangian method as
described in \cref{sec:513gradientBasedConstrained}
using an adaptive gradient descent algorithm
for the gradient-based optimization of the penalized objective function
(see \cref{sec:512gradientBasedUnconstrained}).

\subsection{B-Spline Surrogates on Sparse Grids}
\label{sec:723surrogates}

\paragraph{Complexity}

To solve optimization problems
\ref{item:biomech2MinSum} and \ref{item:biomech2MinDist},
the optimization method needs to evaluate the objective
and constraint functions multiple times during the algorithm.
This requires the evaluation of $\equielbang{\forceL}$,
which in turn has to be approximated with the Newton method.
As we see in \cref{eq:newtonAngle},
each iteration of the Newton method needs not only the values of the
muscle forces $\forceT$ and $\forceB$, but also their
partial derivatives with respect to $\elbang$.
These partial derivatives have to be approximated with finite differences.

Unfortunately, simulations of continuum-mechanical models are
computationally expensive.
One evaluation of the muscle force pair $\forceT, \forceB$
requires the solution of a solid mechanics model
with a complex constitutive law, pre-stretch, and contact between
bone and muscles \cite{Valentin18Gradient}.
On average, a single evaluation of $\forceT$ and $\forceB$ takes
about half an hour on current desktop computers.
%
If we assume that we need four Newton iterations on average,
then a single iteration of the optimization algorithm to solve
problems \ref{item:biomech2MinSum} and \ref{item:biomech2MinDist}
will take four hours to complete
(assuming one evaluation of objective and constraint functions
per optimizer iteration and
two evaluations of the muscle force pair
per Newton iteration to approximate the missing derivative).
Consequently, the whole optimization process takes
two weeks to complete, if the optimizer converges after 100 iterations.

\paragraph{Sparse grid surrogates}

A popular way to reduce complexity is to employ surrogates.
In this case, the idea is to replace the muscle force functions
$\forceT, \forceB$ with surrogates $\forceTintp, \forceBintp$
\cite{Valentin18Gradient}, e.g., by interpolation.
We then automatically obtain a surrogate
\begin{subequations}
  \label{eq:totalMomentSurrogate}
  \begin{gather}
    \momentintp_{\forceL,\actT,\actB}\colon
    \clint{\ang{10}, \ang{150}} \to \real,\\
    \momentintp_{\forceL,\actT,\actB}(\elbang)
    \ceq \forceTintp(\elbang, \actT) \armT(\elbang) +
    \forceBintp(\elbang, \actB) \armB(\elbang) +
    \forceL \armL(\elbang),
  \end{gather}
\end{subequations}
for the total moment (cf.\ \cref{eq:totalMoment}) and,
consequently, a surrogate
\begin{equation}
  \label{eq:equilibriumAngleSurrogate}
  \equielbangintp{\forceL}\colon \actdomainintp{\forceL} \!\to
  \clint{\ang{10}, \ang{150}},\quad
  \actdomainintp{\forceL} \subset \clint{\*0, \*1},\quad
  \equielbangintp{\forceL}(\actT,\actB)
  \ceq (\momentintp_{\forceL,\actT,\actB})^{-1}(\SI{0}{\newton\meter}),
\end{equation}
for the equilibrium elbow angle function (cf.\ \cref{eq:equilibriumAngle}).
Since the surrogates are much cheaper to evaluate,
the computation time is decreased by up to seven orders of magnitude,
as experiments show.

The approach in \cite{Valentin18Gradient} and in this thesis is
to determine surrogates
$\forceXintp\colon \clint{\*0, \*1} \to \real$
($X \in \{\mathrm{T}, \mathrm{B}\}$) by sparse grid interpolation.
Compared to surrogate construction techniques based on full grids,
sparse grids help to reduce the number of samples that
are necessary to build ``reasonably'' accurate surrogates,
especially if the number of dimensions is moderately large
($d \ge 4$, \term{curse of dimensionality}).

The present model only has $d = 2$ dimensions ($\actT$ and $\actB$),
since the model contains only two muscles.
However, as we will see,
already for this low-dimensional problem,
sparse grids outperform conventional full grid interpolation.
The results have to be seen as a proof of concept.
One will be able to handle higher dimensionalities
(i.e., models with a larger number of muscles) similarly with little
or even no adjustments at all.
The low dimensionality of the model in this thesis
enables us to compute and compare against reference solutions,
which would not be possible in a higher-dimensional setting.

\paragraph{Benefiting from B-splines}

As in \cite{Valentin18Gradient},
we use higher-order hierarchical B-splines as basis functions
for the sparse grid surrogates.
This has three advantages when compared with conventional
sparse grid bases such as piecewise linear functions:
%
First, the partial derivative
$\partialderiv{\partialdiff{} \elbang}{\momentintp}$ needed
for the Newton method in \cref{eq:newtonAngle} is continuous and
explicitly known.
There is no need to approximate the derivative with
finite differences, reducing both error and computation time.
%
Second, we can use gradient-based optimization methods
for the solution of the optimization problems \ref{item:biomech2MinSum} and
\ref{item:biomech2MinDist},
which involve the equilibrium elbow angle function
$\equielbangintp{\forceL}\colon \clint{\*0, \*1} \to \real$.
With the implicit function theorem \cite{Kudryavtsev95Implicit},
we obtain for the derivative of $\equielbangintp{\forceL}$
\begin{equation}
  \gradient{\actT,\actB}{\equielbangintp{\forceL}}
  = -\,(\gradient{\actT,\actB}{\momentintp}) \cdot
  (\gradient{\elbang}{\momentintp})^{-1}
  = -\,\frac{
    \gradient{\actT,\actB}{\momentintp}
  }{
    \partialderiv{\partialdiff{} \elbang}{\momentintp}
  },
\end{equation}
where $\gradient{\actT,\actB}{}$ is the transposed Jacobian
with respect to $\actT$ and $\actB$.%
\footnote{%
  For example, the first column is the gradient with respect to $\actT$
  and the second column is the gradient with respect to $\actB$.%
}
For B-splines,
both the transposed Jacobian $\gradient{\actT,\actB}{\momentintp}$ and
the partial derivative $\partialderiv{\partialdiff{} \elbang}{\momentintp}$
are continuous, explicitly known, and can be evaluated fast.
%
Third and finally,
the usage of higher-order B-splines as basis functions
increases the order of convergence of interpolation errors
as shown for test functions in \cref{sec:541interpolation}.
Thus, fewer interpolation points are necessary to construct a surrogate
with the same error as for piecewise linear functions.


%!TEX root = maths.tex
\newcommand{\adj}{\text{adj}\,}
\newcommand{\identitymatrix}{\begin{bmatrix*}[c]1&0&0\\0&1&0\\0&0&1\end{bmatrix*}}
\begin{document}
\chapter{Matrices II}
\section{Determinant of a 3x3 Matrix}
The determinant of a 3x3 matrix is calculated by extracting a row of 2x2 determinants from the given 3x3 matrix. These 2x2 determinants are referred to as minors.

\begin{example}
	Find the determinant of the matrix $\mathbf A = \left(\begin{smallmatrix*}[r]
	2 &-1 &4\\
	3 &0 &-3\\
	4 &5 &6
	\end{smallmatrix*}\right)$
\end{example}
\section{[Parentheses about vector product]}
Consider the vectors $\mathbf{a} = x_1\mathbf{i} + y_1\mathbf{j} + z_1\mathbf{k}$ and b $\mathbf{b} = x_2\mathbf{i} + y_2\mathbf{j} + z_2\mathbf{k}$.\\

Since vector product is distributive across addition:\\

\begin{align*}
	\mathbf{a} \times \mathbf{b} & = \left(  x_1\mathbf{i} + y_1\mathbf{j} + z_1\mathbf{k} \right) \times \left( x_2\mathbf{i} + y_2\mathbf{j} + z_2\mathbf{k} \right)\\
	&+\cancel{x_1x_2(\mathbf i \times \mathbf i)} + x_1y_2(\mathbf i \times \mathbf j) + x_1z_2(\mathbf i \times \mathbf k)\\
	&+ y_1x_2(\mathbf j \times \mathbf i) + \cancel{y_1y_2(\mathbf j \times \mathbf j)} + y_1z_2(\mathbf j \times \mathbf k)\\
	&+z_1x_2(\mathbf k \times \mathbf i) + z_1y_2(\mathbf k \times \mathbf j) + \cancel{z_1z_2(\mathbf k \times \mathbf k)}\\
	&=x_1y_2\mathbf k - x_1z_2\mathbf j - y_1x_2 \mathbf k + y_1z_2\mathbf i +z_1x_2\mathbf j -z_1y_2\mathbf i\\
	&=(y_1z_2-z_1y_2)\mathbf i - (x_1z_2 - z_1 x_2)\mathbf j + (x_1y_2-y_1x_2)\mathbf k
\end{align*}

\section{Some properties of determinant}
The value of the determinant is unaltered if all the rows and columns of a given matrix are interchanged. If the above happens, the resulting determinant will be of opposite sign.\\
 If one row/column of a determinant $D$ is multiplied by $\lambda$, the resulting determinant is equal to $\lambda D$

\section{The Inverse of a 3x3 matrix}

\subsection{Matrix of cofactors}
Consider the matrix $\mathbf A = \begin{bmatrix}a&b&c\\d&e&f\\g&h&i\end{bmatrix}$\\

\noindent For each element of any given matrix $\mathbf A$:
	\begin{itemize}
\item{Ignore the values of the current row and column.}
\item{Calculate the determinant of the remaining values.}
\item{Apply alternating signs starting from $+$.}
\item{Input this determinant into a new matrix}
	\end{itemize}1
The result would be the below matrix referred to as \emph{the matrix of co-factors}.

\[
\mathbf C=
\begin{bmatrix*}[r]
	\begin{vmatrix}e&f\\h&i\end{vmatrix}&&-\begin{vmatrix}d&g\\f&i\end{vmatrix}&&\begin{vmatrix}d&e\\g&h\end{vmatrix}\phantom{-}\\\\
	-\begin{vmatrix}e&f\\h&i\end{vmatrix}&&\begin{vmatrix}d&g\\f&i\end{vmatrix}&&-\begin{vmatrix}d&e\\g&h\end{vmatrix}\phantom{-}\\\\
	\begin{vmatrix}e&f\\h&i\end{vmatrix}&&-\begin{vmatrix}d&g\\f&i\end{vmatrix}&&\begin{vmatrix}d&e\\g&h\end{vmatrix}\phantom{-}
\end{bmatrix*}
\]
\subsection{Adjugate matrix}
The next step of finding the inverse matrix of $\mathbf A$ is finding the \emph{adjugate} matrix of $\mathbf A$. This is done by obtaining the \emph{transpose} of the matrix of cofactors, in our case, $\mathbf C$.
\[
\adj A =
\begin{bmatrix*}[r]
\begin{vmatrix}e&f\\h&i\end{vmatrix}&&-\begin{vmatrix}e&f\\h&i\end{vmatrix}&&\begin{vmatrix}e&f\\h&i\end{vmatrix}\phantom{-}\\\\
-\begin{vmatrix}d&g\\f&i\end{vmatrix}&&\begin{vmatrix}d&g\\f&i\end{vmatrix}&&-\begin{vmatrix}d&g\\f&i\end{vmatrix}\phantom{-}\\\\
\begin{vmatrix}d&e\\g&h\end{vmatrix}&&-\begin{vmatrix}d&e\\g&h\end{vmatrix}&&\begin{vmatrix}d&e\\g&h\end{vmatrix}\phantom{-}
\end{bmatrix*}
\]
\subsection{Inverse matrix}
Let the inverse of a matrix $A$ be $A^{-1}$. It is defined as a matrix of the same size of $A$ such that \[AA^{-1} = A^{-1}A = I\]
This same matrix $A^{-1}$ is defined more particularly as \[A^{-1} = \frac{1}{\det A}\quad\text{adj }A\]
\begin{example}
	Find the inverse of the matrix $A = \begin{pmatrix*}
										2&3&1\\
										1&1&1\\
										5&-1&0
										\end{pmatrix*}$
	\end{example}
\begin{example}
Solve using the inverse matrix method the system of equations:\\
$x+y+z=7$\\
$x-y+2z=9$\\
$2x+y-z=1$
\end{example}
\newpage
\section{Transformation Matrices in 3D}
\subsection{Reflection along the xy plane}

\begin{center}
\begin{tikzpicture}[x=0.5cm,y=0.5cm,z=0.3cm,>=stealth]
% The axes
\draw[->] (xyz cs:x=-5) -- (xyz cs:x=5) node[above] {$x$};
\draw[->] (xyz cs:y=-5) -- (xyz cs:y=5) node[right] {$y$};
\draw[->] (xyz cs:z=-5) -- (xyz cs:z=5) node[above] {$z$};

\foreach \coo in {-4,-3,...,4}
{
	\draw (\coo,-1.5pt) -- (\coo,1.5pt);
	\draw (-1.5pt,\coo) -- (1.5pt,\coo);
	\draw (xyz cs:y=-0.15pt,z=\coo) -- (xyz cs:y=0.15pt,z=\coo);
}
%\draw (xyz cs:y=3,z=2);
\end{tikzpicture}
\end{center}

\begin{align*}
\begin{bmatrix*}[c]1&0&0\\0&1&0\\0&0&1\end{bmatrix*} \rightarrow \begin{bmatrix*}[c]1&0&0\\0&1&0\\0&0&-1\end{bmatrix*}
\end{align*}

\subsection{Rotation along the y-axis}

\begin{align*}
\begin{bmatrix*}[c]1&0&0\\0&1&0\\0&0&1\end{bmatrix*} \rightarrow \begin{bmatrix*}[c]\cos\theta&0&-\sin\theta\\0&1&0\\\sin\theta&0&\cos\theta\end{bmatrix*}
\end{align*}
\subsection{Rotation along the z-axis}

\begin{align*}
\begin{bmatrix*}[c]1&0&0\\0&1&0\\0&0&1\end{bmatrix*} \rightarrow \begin{bmatrix*}[c]\cos\theta&-\sin\theta&0\\\sin\theta&\cos\theta&0\\0&0&1\end{bmatrix*}
\end{align*}
\subsection{Rotation along the z-axis}

\begin{align*}
\begin{bmatrix*}[c]1&0&0\\0&1&0\\0&0&1\end{bmatrix*} \rightarrow \begin{bmatrix*}[c]1&0&0\\0&\cos\theta&-\sin\theta\\0&\sin\theta&\cos\theta\end{bmatrix*}
\end{align*}
\subsection{Enlargement}
When we enlarge (or conversely, reduce) by scale factor $n$, the unit base vector is multiplied by $n$.\\
Thus the matrix representing an enlargement by scale factor n is given by:
\begin{align*}
n\identitymatrix = \begin{bmatrix*}[c]n&0&0\\0&n&0\\0&0&n\end{bmatrix*}
\end{align*}
\section{Geometric Interpretation of the Determinant}
Consider an object with volume $V$ transformed by the matrix $\mathbf A$ with determinant $|\mathbf A|$.
The volume of the image is given by $|\mathbf A|V$. Thus, the determinant of the transformation matrix denotes the number of times by which th volume of the object increases or decreases.
\begin{example}
	Describe the effect on volume in $3$D space of the transformation given by $\mathbf{A} = \begin{bmatrix*}[r]1&2&3\\5&0&-1\\2&4&-3\end{bmatrix*}$
\end{example}

\begin{example}
	Find the images of $P(4,5,1)$, $Q(3,-1,-2)$, $R(6,-2,0)$ under the transformation given by \\$\mathbf A = \left(\begin{smallmatrix*}[r]4&3&2\\-1&5&0\\6&2&-3\end{smallmatrix*}\right)$
\end{example}
\begin{example}
	Find the equation of the line which is the image of the line $\mathbf r = 3\mathbf i +\mathbf j +\mathbf k + \lambda(2\mathbf i + \mathbf j - 5\mathbf k)$ under the transformation given by $\mathbf A =\begin{bmatrix}2&3&1\\-1&2&4\\0&6&1\end{bmatrix}$
\end{example}
Find two points on the line.
Find images of points.
Find equation of the image line

\begin{example}
	Find the image of the plane $x+2y-7z=2$ under the transformation defined by $\mathbf A = \begin{smallmatrix}-1&2&1\\-3&1&4\\0&1&2\end{smallmatrix}$
\end{example}
\end{document}


\newcommand\WWtil{{\tilde{\mathbf{W}}}}
\newcommand\hh{{\boldsymbol{\mathit{h}}}}

\chapter{Random Walks}

\sloppy

Today, we talk about random walks on graphs and how the spectrum of the Laplacian guides convergence of random walks. We start by giving the definition of a random walk on a weighted graph $G=(V,E,w)$.

\section{A Primer on Random Walks}

\paragraph{Random Walk Basics.} We call a random sequence of vertices $v_0, v_1, \dots$ a \emph{random walk} on $G$, if $v_0$ is a vertex in $G$ chosen according to some probability distribution $\pp_0 \in \mathbb{R}^V$; and for any $t \geq 0$, we have
\[
\mathbb{P}[v_{t+1} = v \; | v_t = u] = \begin{cases}
    w(u,v)/\dd(u) & \text{if } \{u,v\} \in E,\\
    0 & \text{otherwise}.
\end{cases}
\]

\begin{figure}[!ht]
    \centering\label{fig:randomWalkSimple}
    \includegraphics[scale=0.4]{fig/lec_RandomWalks_fig1.png}
    \caption{A (possibly random) walk where the red edges indicate the edges that the particle moves along. Here the walk visits the vertices $v_0 = 1, v_1 = 2, v_2 = 3, v_3 = 2, v_4 = 3, v_5 = 4$.}
    \label{fig:my_label}
\end{figure}

To gain some intuition for the definition,  assume first that the graph $G$ is undirected. Consider a {\color{red}particle} that is placed at a random vertex $v_0$ initially. Then at each step the particle is moved to a neighbor of the current vertex it is resting at, where the neighbor is chosen uniformly at random.

If the graph is weighted, then instead of choosing a neighbor $v_{t+1}$ of a vertex $v_t$ at each step uniformly at random, one chooses a neighbor $v$ of $v_t$ with probability $w(v,v_t)$ divided by the degree $\dd(v_t)$.

\paragraph{The Random Walk Matrix.} We now define the random walk matrix $\WW$ by
\[
    \WW = \AA \DD^{-1}
\]
and observe that for all vertices $u,v \in V$ (and any $t$), we have that
\[
\WW_{vu} = \begin{cases}
    w(u,v)/\dd(u) & \text{if } \{u,v\} \in E,\\
    0 & \text{otherwise}.
\end{cases}
\]
Thus, $\WW_{vu} = \mathbb{P}[v_{t+1} = v \; | v_t = u]$ (for any $t$).

Therefore, $W \vecone_u$ is the distribution over the vertices that the random walk visits them at the next time step, given that it currently is at $u$. More generally, we can now compute the distribution $\pp_1$ over the vertices that they are visited at time $1$ by $\WW \pp_0$, the distribution $\pp_2$ by $\WW \pp_1 = \WW ( \WW \pp_0)$ and so on. Another way of writing this is $\pp_t = \WW^t \pp_{0}$.

\section{Convergence Results for Random Walks}

In this first part of the chapter, we are interested mostly in convergence of random walks that is the two questions:
\begin{itemize}
	\item How does a random walk behave after a large number of steps are taken?
	\item How many steps does it take asymptotically until the random walk behaves as if an infinite number of steps were taken?
\end{itemize}

To start shedding some light on these questions, we introduce stationary distributions.

\paragraph{Stationary Distribution.} We call a distribution $\ppi \in \mathbb{R}^V$, a \emph{stationary distribution} if $\WW\ppi = \ppi$. That is $\ppi$ is an eigenvector of $\WW$ associated with eigenvalue $1$. It turns out such a stationary distribution always exists.

\begin{lemma}\label{lma:thereExistsStationaryDistr}
Every graph $G$ has a stationary distribution.
\end{lemma}
\begin{proof}
Let $\ppi = \frac{\dd}{\vecone^\trp\dd}$. Clearly, we have that $\|\ppi\|_1 = \sum_{v \in V} \dd(v)/\vecone^\trp\dd = \frac{1}{\vecone^\trp\dd} \sum_{v \in V} \dd(v) = 1$, so $\ppi$ is indeed a distribution. Further note that
\[
\WW \ppi = \AA \DD^{-1} \cdot  \frac{\dd}{\vecone^\trp\dd} = \frac{\AA \vecone}{\vecone^\trp \dd} = \frac{\dd}{\vecone^\trp \dd} = \ppi.
\]
\end{proof}

For many graphs one can show that for $t \to \infty$, we have that $\pp_t \to \ppi$, i.e. that independent of the starting distribution $\pp_0$, the random walk always converges to distribution $\ppi$.

Unfortunately, this is not true for all graphs: take the graph of two vertices connected by a single edge with $\pp_0$ being $1$ at one vertex and $0$ at the other.

\subsection{Making Random Walks Lazy}

\paragraph{Lazy Random Walks.} Luckily, we can overcome this issue by using a \emph{lazy random walk}. A lazy random walk behaves just like a random walk, however, at each time step, with probability $\frac{1}{2}$ instead of transitioning to a neighbor, it simply stays put. We give the lazy random walk matrix by
\[
 \WWtil = \frac{1}{2}\II + \frac{1}{2} \WW = \frac{1}{2}\left(\II + \AA \DD^{-1}\right).
\]
It is not hard to see that the stationary distribution $\ppi$ for $\WW$, is also a stationary distribution for $\WWtil$.

\begin{figure}[!ht]
    \centering\label{fig:randomLazyWalkSimple}
    \includegraphics[scale=0.2]{fig/lec_RandomWalks_fig2.jpg}
    \caption{A lazy random walk where the red edges indicate the edges that the particle moves along. Here the lazy walk visits the vertices $v_0 = 1, v_1 = 2, v_2 = 2, v_3 = 3, v_4 = 3, v_5 = 2$.}
    \label{fig:my_label}
\end{figure}

\paragraph{Lazy Random Walks and the Normalized Laplacian.} Recall that we defined $\NN = \DD^{-1/2}\LL \DD^{-1/2} = \II - \DD^{-1/2}\AA \DD^{-1/2} \iff \DD^{-1/2}\AA \DD^{-1/2} = \II - \NN$. We can therefore derive
\begin{align*}
    \WWtil &= \frac{1}{2}\II + \frac{1}{2} \AA \DD^{-1}\\
    &= \frac{1}{2}\II + \frac{1}{2} \DD^{1/2}\DD^{-1/2} \AA \DD^{-1/2}\DD^{-1/2}\\
     &= \frac{1}{2}\II + \frac{1}{2} \DD^{1/2}(\II - \NN) \DD^{-1/2}\\
    &= \frac{1}{2}\II + \frac{1}{2} \DD^{1/2}\II \DD^{-1/2} - \frac{1}{2} \DD^{1/2}\NN \DD^{-1/2}\\
    &= \II - \frac{1}{2} \DD^{1/2}\NN \DD^{-1/2}
\end{align*}

We will now start to reason about the eigenvalues and eigenvectors of $\WWtil$ in terms of the normalized laplacian $\NN$ that we are already familiar with.

For the rest of the lecture, we let $\nu_1 \leq \nu_2 \leq \dots \leq \nu_n$ be the eigenvalues of $\NN$ associated with the orthogonal eigenvectors $\ppsi_1, \ppsi_2, \dots, \ppsi_n$ where we know that such eigenvectors exist by the Spectral Theorem. We note in particular that from the last lecture, we have that $\ppsi_1 = \frac{\dd^{1/2}}{(\vecone^{\trp} \dd)^{1/2}}$ (see Equation \ref{eq:plugInNormalizedVector} where we added a normalization such that $\ppsi_1^{\trp} \ppsi_1 = 1$).

\begin{lemma}\label{lma:correspondenceEigenvectorWalkMatrix}
For the $i^{th}$ eigenvalue $\nu_i$ of $\NN$ associated with eigenvector $\bm{\psi}_i$, we have that $\WWtil$ has an eigenvalue of $(1 - \frac{1}{2}\nu_i)$ associated with eigenvector $\DD^{1/2} \ppsi_i$.
\end{lemma}
\begin{proof}
The proof is by straight-forward calculations
\begin{align*}
    \WWtil \DD^{1/2} \bm{\psi}_i &= (\II - \frac{1}{2} \DD^{1/2}\NN \DD^{-1/2})\DD^{1/2} \bm{\psi}_i \\ &= \DD^{1/2} \bm{\psi}_i - \frac{1}{2} \DD^{1/2}\NN \bm{\psi}_i \\
    &=\DD^{1/2} \bm{\psi}_i - \frac{1}{2} \DD^{1/2}\bm{\psi}_i \nu_i = \DD^{1/2} \bm{\psi}_i (1 - \frac{1}{2}\nu_i).
\end{align*}
\end{proof}

\begin{corollary}
Every eigenvalue of $\WWtil$ is in $[0,1]$.
\end{corollary}
\begin{proof}
Recall that $\LL \pleq 2\DD$ which implies that $\NN \pleq 2 \II$. But this implies that every eigenvalue of $\NN$ is in $[0,2]$. Thus, using Lemma \ref{lma:correspondenceEigenvectorWalkMatrix}, the corollary follows.
\end{proof}

\subsection{Convergence of Lazy Random Walks}

We have now done enough work to obtain an interesting result. We can derive an alternative characterization of $\pp_t$ by expanding $\pp_0$ along an orthogonal eigenvectors basis and then we can repeatedly apply $\WWtil$ by taking powers of the eigenvalues.

Unfortunately, $\WWtil$ is not symmetric so its eigenvectors are not necessarily orthogonal. Instead, we use a simple trick that allows to expand along the eigenvectors of $\NN$
\begin{align}\label{eq:definitionOfAlphaForWalks}
     \forall i, \ppsi_i^{\trp} \DD^{-1/2} \pp_0 = \alpha_i \iff \DD^{-1/2} \pp_0 = \sum_{i=1}^n \alpha_i \ppsi_i \iff  \pp_0 = \sum_{i=1}^n \alpha_i \DD^{1/2} \ppsi_i.
\end{align}
The above equivalences are best understood if you start from the middle. To get to the left side, you need to observe that multiplying both sides by $\ppsi_i^{\trp}$ cancels all terms $\ppsi_j$ with $j \neq i$ in the sum by orthogonality. To get the right hand side expression, one can simply left-multiply by $\DD^{1/2}$. Technically, we have to show that $\DD^{-1/2}\pp_0$ lives in the eigenspace of $\NN$ but we leave this as an exercise.

This allows us to express a right multiplication by $\WWtil$ as
\[
    \pp_1 = \WWtil \pp_0 = \sum_{i=1}^n \alpha_i \WWtil \DD^{1/2} \ppsi_i = \sum_{i=1}^n \alpha_i \left(1-\frac{\nu_i}{2}\right) \DD^{1/2} \ppsi_i.
\]
And as promised, if we apply $\WWtil$, the lazy random walk operator, $t$ times, we now obtain
\begin{align}\label{eq:distributionRandomWalkTSteps}
    \pp_t = \sum_{i=1}^n \alpha_i \left(1-\frac{\nu_i}{2}\right)^t \DD^{1/2} \ppsi_i = \alpha_1 \DD^{1/2} \ppsi_1 + \sum_{i=2}^n \alpha_i \left(1-\frac{\nu_i}{2}\right)^t \DD^{1/2} \ppsi_i.
\end{align}
where we use in the last equality that $\nu_1 = 0$. Using this simple characterization, we immediately get that $\pp_t \to \ppi$ if $\nu_i > 0$ for all $i \geq 2$ (which is exactly when the graph is connected as you will prove in an exercise). To see this, observe that as $t$ grows sum vanishes. We have that
\[
    \lim_{t \to \infty} \pp_t = \alpha_1 \DD^{1/2} \ppsi_1 = \ppi.
\]
where we used in the equality that $\DD^{1/2} \ppsi_1 = \frac{\dd }{(\vecone^{\trp} \dd)^{1/2}}$ and the value of $\alpha_1$ (from \ref{eq:definitionOfAlphaForWalks}).

\begin{theorem}
For any connected graph $G$, we have that the lazy random walk converges to the stationary distribution of $G$.
\end{theorem}

\subsection{The Rate of Convergence}

Let us now come to the main result that we want to prove this lecture.

\begin{theorem}
For any $\pp_0$, at any time step $t$, we have for $\pp_t = \WWtil^t \pp_0$ that
\[
\| \pp_t - \ppi \|_{\infty}  \leq e^{-\nu_2 \cdot t/2} \sqrt{n}
\]
\end{theorem}

Instead of proving the theorem above, we prove the lemma below which gives point-wise convergence. This makes it more convenient to derive a proof and it is not hard to deduce the theorem above as a corollary.

\begin{lemma}
For all $a,b \in V$, and any time step $t$, we have for $\pp_0 = \vecone_a$ and $\pp_t = \WWtil^t \pp_0$ that
\[
  |\pp_t(b) - \ppi(b)| \leq e^{-\nu_2 \cdot t/2} \sqrt{\dd_b/\dd_a}
\]
\end{lemma}

From Equation \ref{eq:distributionRandomWalkTSteps}, we obtain that
\begin{align}\label{eq:piecewiseDistribution}
    \pp_t(b) - \ppi(b) &= \vecone_b^\trp(\pp_t - \ppi) = \vecone_b^\trp\left( \sum_{i=2}^n \alpha_i \left(1-\frac{\nu_i}{2}\right)^t \DD^{1/2} \ppsi_i \right)
    \\ & =   \sum_{i=2}^n \alpha_i \left(1-\frac{\nu_i}{2}\right)^t \vecone_b^\trp\DD^{1/2} \ppsi_i
    \leq \left(1-\frac{\nu_2}{2}\right)^t \cdot \sum_{i=2}^n \alpha_i  \vecone_b^\trp\DD^{1/2} \ppsi_i
\end{align}
Taking the absolute value on both sides, we obtain that
\begin{align*}\small
|\pp_t(b) - \ppi(b)|
&\leq \left(1-\frac{\nu_2}{2}\right)^t\sum_{i=2}^n \left|\alpha_i  \vecone_b^\trp\DD^{1/2} \ppsi_i \right|\leq \left(1-\frac{\nu_2}{2}\right)^t \sqrt{\left( \sum_{i=2}^n \alpha_i^2 \right) \left( \sum_{i=2}^n \left(\vecone_b^\trp\DD^{1/2} \ppsi_i \right)^2 \right)}
\end{align*}
where we use Cauchy-Schwarz in the last inequality, i.e. $|\langle \uu, \vv \rangle|^2 \leq \langle \uu, \uu \rangle \cdot \langle \vv, \vv \rangle$. Let us finally bound the two sums:
\begin{itemize}
    \item By \ref{eq:definitionOfAlphaForWalks}, $\sum_{i=2}^n \alpha_i^2 = \sum_{i=2}^n \left(\ppsi^\trp_i \DD^{-1/2}\pp_0 \right)^2 \leq \|\DD^{-1/2}\pp_0\|_2^2 =  \|\DD^{-1/2}\vecone_a\|_2^2 = 1/\dd_a$.
    \item Finally, we show that $\sum_{i=2}^n \left(\vecone_b^\trp\DD^{1/2} \ppsi_i \right)^2 \leq \sum_{i=1}^n \left(\vecone_b^\trp\DD^{1/2} \ppsi_i \right)^2 = \|\DD^{1/2}\vecone_b\|_2^2 = \dd_b$ (we only show the first equality, the other inequalities are straight-forward). We first expand the vector $\DD^{1/2}\vecone_b$ along the eigenvectors using some values $\beta_i$ defined
    \[
        \DD^{1/2}\vecone_b = \sum_{i=1}^n \beta_i \ppsi_i \iff  \ppsi_i^\trp \DD^{1/2}\vecone_b = \beta_i \iff  \vecone_b^\trp \DD^{1/2}\ppsi_i= \beta_i
    \]
    We used orthogonality to get the first equivalence, and then just take the transpose to get the second. We can now write
    \[
    \|\DD^{1/2}\vecone_b\|_2^2 = (\DD^{1/2}\vecone_b)^{\trp} (\DD^{1/2}\vecone_b) = \left(\sum_{i=1}^n \beta_i \ppsi_i^\trp\right) \left(\sum_{i=1}^n \beta_i \ppsi_i\right) = \sum_{i=2}^n \beta_i^2
    \]
    where we again used orthogonality of $\ppsi_i$. The equality then follows by definition of $\beta_i$.
\end{itemize}

Putting everything together (and using $1+x \leq e^x, \forall x \in \mathbb{R}$), we obtain
\[
    |\pp_t(b) - \ppi(b)| \leq \left(1-\frac{\nu_2}{2}\right)^t \sqrt{\dd_b/\dd_a} \leq e^{-\nu_2 \cdot t/2} \sqrt{\dd_b/\dd_a}
\]

\section{Properties of Random Walks}

We now shift our focus away from convergence of random walks and consider some interesting properties of random walks. Here, we are no longer interested in lazy random walks, although all proofs can be straight-forwardly adapted. While in the previous section, we relied on computing the second eigenvalue of the Normalized Laplacian efficiently, here, we will discover that solving Laplacian systems, that is finding an $\xx$ such that $\LL\xx = \bb$ can solve a host of problems in random walks.

\subsection{Hitting Times}

One of the most natural questions one can ask about a random walk starting in a vertex $a$ (i.e. $\pp_0 = \vecone_a$) is how many steps it takes to get to a special vertex $s$. This quantity is called the \emph{hitting time} from $a$ to $s$ and we denote it by $H_{a,s} = \inf \{ t \;|\; \vv_t = s \}$. For the rest of this section, we are concerned with computing the expected hitting time, i.e. $\mathbb{E}[H_{a,s}]$.

It turns out, that it is more convenient to compute \emph{all} expected hitting times $H_{a,s}$ for vertices $a \in V$ to a fixed $s$. We denote by $\hh \in \mathbf{R}^V$, the vector with $\hh_a = \mathbb{E}[H_{a,s}]$. We now show that we can compute $\hh$ by solving a Laplacian system $\LL \hh = \bb$. We will see later in the course that such systems (spoiler alert!) can be solved in time $\tilde{O}(m)$, so this will imply a near-linear time algorithm to compute the hitting times.

\paragraph{Hitting Time and the Random Walk Matrix.} Let us first observe that if $s = a$, then the answer becomes trivially $0$, i.e. $\hh_s = 0$.

We compute the rest of the vector by writing down a system of equations that recursively characterizes $\hh$. Observe therefore first that for any $a \neq s$, we have that the random walks starting at $a$ will next visit a neighbor $b$ of $a$. If the selected neighbor $b = s$, the random walks stops; otherwise, the random walks needs in expectation $\mathbb{E}[H_{b,s}]$ time to move to $s$.

We can express this algebraically by
\[
\hh_a = 1 + \sum_{a \sim b} \mathbb{P}[v_{t+1} = b \;| v_t = a]  \cdot \hh_b = 1 + \sum_{a \sim b} \frac{w(a,b)}{\dd(a)}  \cdot \hh_b =  1 + (\WW\vecone_a)^{\trp} \hh = 1 + \vecone_a^{\trp} \WW^{\trp} \hh.
\]
Using that $\hh_a = \vecone_a^{\trp} \hh =  \vecone_a^{\trp} \II \hh$, we can rewrite this as
\[
1 = \vecone_a^\trp (\II - \WW^\trp) \hh.
\]
This gives a system of (linear) equations, that can be neatly summarized by
\[
\vecone - \alpha \cdot \vecone_s = (\II - \WW^\trp) \hh
\]
where we have an extra degree of freedom in choosing $\alpha$ in formulating a constraint $1 - \alpha = \vecone_s^\trp (\II -\WW^\trp) \hh$. This extra degree of freedom stems from the fact that $n-1$ equations suffice for us to enforce that the returned vector $\xx$ to the system  from the system are indeed the hitting times (possibly shifted by the value assigned to coordinate $t$).

\paragraph{Finding Hitting Times via Laplacian System Solve.} Since we assume $G$ connected, we have that multiplying with $\DD = \DD^\trp$ preserves equality. Further since $\WW = \AA \DD^{-1}$, we obtain
\[
\dd - \alpha \cdot \dd(s) \cdot \vecone_s = (\DD - \AA) \hh.
\]
Defining $\bb = \dd - \alpha \cdot \dd(s) \cdot \vecone_s$, and observing $\LL = \DD - \AA$, we have $\LL\hh = \bb$.

Finally, we observe that we only have a solution to the above system if and only if $\bb \in \ker(\LL)^\perp =  \Span(\vecone)^\perp$. We thus have to set $\alpha$ such that
\[
\vecone^{\trp}(\dd - \alpha \cdot \dd(s) \cdot \vecone_s) = \|\dd\|_1 - \alpha \cdot \dd(s) \iff \alpha =  \|\dd\|_1 /\dd(s).
\]
We have now formalized $\LL$ and $\bb$ completely. A last detail that we should not forget about is that any solution $\xx$ to such system $\LL \xx = \bb$ is not necessarily equal $\hh$ but has the property that it is shifted from $\hh$ by the all-ones vector. Since we require $\hh_s = 0$, we can reconstruct $\hh$ from $\xx$ straight-forwardly by subtracting $\xx_s \vecone$.

\begin{theorem}
	Given a connected graph $G$, a special vertex $s \in V$. Then, we can formalize a Laplacian system $\LL \xx = \bb$ (where $\LL$ is the Laplacian of $G$) such that the expected hitting times to $s$ are given by $\hh = \xx - \xx_s \vecone$. We can reconstruct $\hh$ from $\xx$ in time $O(n)$.
\end{theorem}

\paragraph{Hitting Times and Electrical Networks.} Seeing that hitting times can be computed by formulating a Laplacian system $\LL \xx = \bb$. You might remember that in the first lecture, we argued that a system $\LL \xx = \bb$ also solves the problem of routing a demand $\bb$ via an electrical flow with voltages $\xx$.

Indeed, we can interpret computing expected hitting times $\hh$ to a special vertex $s$ as the problem of computing the electrical voltages $\xx$ where we insert (or more technically correct apply) $\dd(a)$ units of current at every vertex $a \neq s$ and where we remove $\vecone^\trp\dd - \dd(s)$ units of current at the vertex $s$. Then, we can express expected hitting time to some vertex $a$ as the voltage difference to $s$: $\mathbb{E}[H_{a,s}] = \hh_a = \xx_a - \xx_s$.

\subsection{Commute Time}

A topic very related to hitting times are \emph{commute times}. That is for two vertices $a,b$, the commute time is the time in a random walk starting in $a$ to visit $b$ and then to return to $a$ again. Thus, it can be defined $C_{a,b} = H_{a,b} + H_{b,a}$.

\paragraph{Commute Times via Electric Flows.} Recall that expected hitting times have an electric flow interpretation.

Now, let us denote by $\xx$ a solution to the Laplacian system $\LL \xx = \bb_b$ where the demand is $\bb_b = \dd - \dd^\trp \vecone \cdot \vecone_b \in \ker(\vecone)^\perp$. Recall that we have $\mathbb{E}[H_{z,b}] = \xx_z - \xx_b$ for all $z$.

Similarly, we can compute voltages $\yy$ to the Laplacian system $\LL \yy = \bb_a$ where $\bb_a = \dd - \dd^\trp \vecone \cdot \vecone_a \in \ker(\vecone)^\perp$. Again, $\mathbb{E}[H_{z,a}] = \yy_z - \yy_a$ for all $z$. Note that, if we revert the flow by negating $\yy$, then we can still compute the hitting time by taking $\mathbb{E}[H_{z,a}] = -(-\yy_z - (-\yy_a)) = -(\yy_a-\yy_z)$.

Thus, inducing voltages $\xx - \yy$ on the graph $G$, we now have by linearity that $\mathbb{E}[C_{a,b}] = \mathbb{E}[H_{a,b}+H_{b,a}] = |\xx_a - \xx_b| + |\yy_a-\yy_b|$. But these voltages are also induced by $\LL (\xx - \yy) = \bb_b - \bb_a = \dd^\trp \vecone (\vecone_a - \vecone_b)$ (again by linearity). That is the flow that routes $\|\dd\|_1$ units of flow from $b$ to $a$.

\begin{theorem}
	Given a graph $G=(V,E)$, for any two fixed vertices $a,b \in V$, the expected commute time $C_{a,b}$ is given by the voltage difference between $a$ and $b$ for any solution $\zz$ to the Laplacian system $\LL \zz = \|\dd\|_1 \cdot (\vecone_b - \vecone_a)$.
\end{theorem}

We note that the voltage difference between $a$ and $b$ in an electrical flow routing demand $\vecone_b - \vecone_a$ is also called the \emph{effective resistance} $\er(a,b)$. This quantity will play a crucial role in the next roles. In the next lecture, we introduce $\er(a,b)$ slightly differently as the energy required by the electrical flow that routes $\vecone_b - \vecone_a$, however, it is not hard to show that these two definitions are equivalent.

Our theorem can now be restated as saying that the expected commute time $\mathbb{E}[C_{a,b}] = \|\dd\|_1 \cdot \er(a,b)$. This is a classic result.


\documentclass{memoir}
\usepackage{notestemplate}

%\logo{~/School-Work/Auxiliary-Files/resources/png/logo.png}
%\institute{Rice University}
%\faculty{Faculty of Whatever Sciences}
%\department{Department of Mathematics}
%\title{Class Notes}
%\subtitle{Based on MATH xxx}
%\author{\textit{Author}\\Gabriel \textsc{Gress}}
%\supervisor{Linus \textsc{Torvalds}}
%\context{Well, I was bored...}
%\date{\today}

%\makeindex

\begin{document}

% \maketitle

% Notes taken on

\subsection{Quadratic Integer Rings}
\label{sub:quadratic_integer_rings}

Let \(D\) be a rational number that is not a perfect square in \(\Q\) and define
\begin{align*}
	\Q(\sqrt{D} ) = \left\{a+b\sqrt{D}  \mid a,b \in \Q \right\}
\end{align*}
as a subset of \(\C\). One can check that the set is closed under subtraction and under the multiplication defined by
\begin{align*}
	(a+b\sqrt{D})(c+d\sqrt{D} ) = (ac+bdD) + (ad+bc)\sqrt{D}
\end{align*}
shows that it is closed under multiplication. Hence \(\Q(\sqrt{D} ) \leq \C\) as a subring (and of \(\R\) if \(D>0\)) and hence is commutative with identity.\\

The assumption that \(D\) is not a square allows us to write every element of \(\Q(\sqrt{D} )\) uniquely in the form \(a+b\sqrt{D} \). Furthermore, if \(a,b\) are not both zero, then \(a^2-Db^2\neq 0\), and because
\begin{align*}
	(a+b\sqrt{D} )(a-b\sqrt{D} ) = a^2-Db^2
\end{align*}
then if \(a+b\sqrt{D} \neq 0\) we have
\begin{align*}
	(a+b\sqrt{D} )^{-1} = \dfrac{a-b\sqrt{D} }{a^2-Db^2}.
\end{align*}
This shows that every nonzero element in the commutative ring is a unit and hence \(\Q(\sqrt{D} )\) is a field called a \textbf{quadratic field}.\\

The rational number \(D\) can be written by \(D = q^2D'\) for some rational number \(q\) and a unique integer \(D'\), where \(z^2\not\mid D'\) for all \(z \in \Z_+\) greater than \(1\). We call \(D'\) the \textbf{squarefree part} of \(D\). Because \(\sqrt{D} =q\sqrt{D'} \) it holds that \(\Q(\sqrt{D} )=\Q(\sqrt{D'} )\), and hence we can use squarefree integers instead in the definition of the quadratic field.\\

Let \(D\) be a squarefree integer. One can check that
\begin{align*}
	\Z[\sqrt{D} ] = \left\{a + b\sqrt{D}  \mid a,b \in \Z \right\}
\end{align*}
forms a subring of the quadratic field \(\Q(\sqrt{D} )\). In the case where \(D \equiv 1 \pmod{4}\), then we can form a slightly larger subring by
\begin{align*}
	\Z\left[ \frac{1+\sqrt{D}}{2} \right] = \left\{ a + b \frac{1+\sqrt{D} }{2} \mid a,b \in \Z \right\} .
\end{align*}
Now define
\begin{align*}
	\mathcal{O} = \mathcal{O}_{\Q(\sqrt{D} )} = \Z[\omega ] = \left\{a + b \omega  \mid a,b \in \Z \right\} \\
	\omega = \begin{cases}
		\sqrt{D}  & D \equiv 2, 3 \pmod{4}\\
		\frac{1+\sqrt{D} }{2} & D \equiv 1 \pmod{4}
	\end{cases}.
\end{align*}
We call \(\mathcal{O}\) the \textbf{ring of integers} in the quadratic field \(\Q(\sqrt{D} )\)-- despite the fact that elements are not actually integers. This terminology arises because the properties of \(\mathcal{O}\) are similar to those of \(\Z\leq \Q\) as subrings. In fact, we will later see that it is the \textit{integral closure} of \(\Z\) in \(\Q(\sqrt{D} )\).\\

In the special case where \(D = -1\), we obtain the ring \(\Z[i]\) of \textbf{Gaussian integers}. We will cover the Gaussian integers later, as they have important ties to number theory.

\begin{defn}[Field Norm]
	Define the \textbf{field norm} \(N:\Q(\sqrt{D} ) \to \Q\) by
	\begin{align*}
		N(a+b\sqrt{D} ) = (a+b\sqrt{D} )(a-b\sqrt{D} ) = a^2 - Db^2 \in \Q
	\end{align*}
	This norm gives a notion of size in the field. For example, when \(D=-1\), the norm of \(a+bi\) is \(a^2+b^2\).
\end{defn}
One can check that \(N\) is \textbf{multiplicative}-- that is, \(N(\alpha \beta ) = N(\alpha )N(\beta )\). We can also see that on the subring \(\mathcal{O}\) the field norm is given by
\begin{align*}
	N(a+b\omega ) = (a+b\omega )(a+b \overline{\omega }) = \begin{cases}
		a^2-Db^2 & D \equiv 2,3 \pmod{4}\\
		a^2+ab + \frac{1-D}{4}b^2 & D \equiv 1 \pmod{4}
	\end{cases}\\
	\overline{\omega } = \begin{cases}
		-\sqrt{D} & D \equiv 2,3 \pmod{4}\\
		\frac{1-\sqrt{D} }{2} & D \equiv 1 \pmod{4}
	\end{cases}
\end{align*}
And hence \(N(\alpha )\) is in fact an integer for every \(\alpha  \in \mathcal{O}\). This in fact characterizes the units of \(\mathcal{O}\)-- if \(\alpha \in \mathcal{O}\) has field norm \(N(\alpha ) = \pm 1\), then
\begin{align*}
	(a + b\omega )^{-1} = \pm (a + b \overline{\omega })
\end{align*}
and hence \(\alpha \) is a unit. The multiplicative property directly tells us that the converse holds, and hence \(\alpha \in \mathcal{O}\) is a unit if and only if \(N(\alpha ) = \pm 1\).\\

In number theory, finding solutions to the equation \(x^2-Dy^2 = \pm 1\) is equivalent to the determination of units in \(\mathcal{O}\).

\begin{hw}
	Show that if \(D>0\) then the group of units \(\mathcal{O}^{\times }\) is always infinite. Find a class of units in \(\mathcal{O} = \Z[\sqrt{2} ]\) that exemplifies this.\\

	Show that \(\mathcal{O}_{\Q(\sqrt{-3} )}\) has only a finite number of elements, and list them. Are there other values of \(D\) with more units than \(\left\{ \pm 1 \right\} \)?
\end{hw}

\subsection{Polynomial Rings}
\label{sub:polynomial_rings}

\begin{defn}[Polynomial Ring]
	Let \(R\) be a commutative ring. We define a \textbf{polynomial} in \(x\) to be the formal sum
	\begin{align*}
		a_n x^{n} + a_{n-1}x^{n-1} + \ldots + a_1 x + a_0
	\end{align*}
	where \(n\geq 0\) and \(a_i \in R\). If \(a_n \neq 0\), then the polynomial is of \textbf{degree \(n\)}, and \(a_nx^{n}\) is te \textbf{leading term} (\(a_n\) is the \textbf{leading coefficient}). Furthermore, we say the polnyomial is \textbf{monic} if \(a_n=1\).\\

	The set of all such polynomials is called the \textbf{ring of polynomials in \(\R\)} and will be denoted \(R[x]\). We define addition and multiplication by the standard version from algebra:
	\begin{align*}
		(a_nx^{n} + \ldots + a_1x + a_0) + (b_nx^{n} + \ldots + b_1x + b_0) = (a_n+b_n)x^{n} + \ldots + (a_1+b_1)x + (a_0 + b_0)\\
		(a_0+a_1x + a_2x^2+\ldots) \times (b_0 + b_1x + b_2x^2 + \ldots) = a_0b_0 + (a_0b_1 + a_1b_0)x + (a_0b_2 + a_1b_1 + a_2b_0)x^2 + \ldots
	\end{align*}
	That is, the coefficient in the product of \(x^{k}\) is \(\sum_{i=0}^{k} a_i b_{k-i}\).
\end{defn}
We can see that \(R\leq R[x]\) as the \textbf{constant polynomials}. Notice further that \(R[x]\) is also a commutative ring.

\begin{prop}
	Let \(R\) be an integral domain and let \(p(x),q(x) \in R[x]\) be nonzero polynomials. Then the degree of \(p(x)q(x) = \textrm{deg}p(x) + \textrm{deg}q(x)\).\\

	The units of \(R[x]\) are exactly the same units of \(R\), and \(R[x]\) is an integral domain.
\end{prop}
If \(R\) has zero divisors, then \(R[x]\) does as well. We can also see that \(S\leq R \implies S[x] \leq R[x]\).

\begin{exmp}
	Consider the polynomial ring \((\Z_3[x]\). This ring consists of nonnegative powers of \(x\) with coefficients in \(\left\{ 0,1,2 \right\} \) with calculations being done in modulus \(3\). For example, let
	\begin{align*}
		p(x) = x^2+2x+1 \quad q(x) = x^3+x +2.
	\end{align*}
	Then
	\begin{align*}
		p(x) + q(x) = x^3+x^2\\
		p(x)q(x) = x^{5}+2x^{4}+2x^3+x^2+2x+2
	\end{align*}
	Polynomials behave very differently even under simple modulus structures.
\end{exmp}
We will see more of polynomial rings after building more theory.

\subsection{Matrix Rings}
\label{sub:matrix_rings}

\begin{defn}[Matrix Rings]
	Let \(R\) be a ring and \(n\in \Z_+\). We define \(M_n(R)\) to be the set of all \(n\times n\) matrices with entries in \(R\). The element \(A \in M_n(R)\) is an \(n\times n\) square array of elements of \(R\) whose entry in row \(i\) and column \(j\) is \(A_{ij} \in R\). We see that this set of matices becomes a ring under the usual matrix addition and multiplication, called the \textbf{matrix ring of rank \(n\)}.
\end{defn}
Notice that if \(n\geq 2\), then \(M_n(R)\) is not commutative, regardless of the commutativity of \(R\). Furthermore, it will also have zero divisors.\\

We say \(A \in M_n(R)\) is a \textbf{scalar matrix} if \(a_{ii}=a\) for all \(i \in \left\{ 1,\ldots,n \right\} \), and \(a_{ij}=0\) if \(i\neq j\). This forms a subgring of \(M_n(R)\), and is in fact isomorphic to \(R\). If \(R\) is commutative, the scalar matrices commute with all elements of \(M_n(R)\).\\

Note that the units of \(M_n(R)\) are the invertible \(n\times n\) matrices-- this forms a subgroup called the \textbf{general linear group of degree \(n\) over \(R\)} (written by \(GL_n(R)\)).\\

Similar to polynomial rings, if \(S\leq R\), then \(M_n(S) \leq M_n(R)\). Another subring is the set of upper triangular matrices.

\subsection{Group Rings}
\label{sub:group_rings}

\begin{defn}[Group Rings]
Let \(R\) be a commutative ring and \(G \) a finite group. The \textbf{group ring \(RG\) of \(G\)} is the set of all sums
\begin{align*}
	RG = \left\{ a_1g_1 + a_2g_2 + \ldots + a_ng_n \mid a_i\in R, g_i \in G\right\}
\end{align*}
We define addition and multiplication by
\begin{align*}
	(a_1g_1+ a_2g_2 + \ldots + a_ng_n) + (b_1g_1 + b_2g_2 + \ldots + b_ng_n) = (a_1+b_1)g_1 + (a_2+b_2)g_2 + \ldots + (a_n+b_n)g_n\\
		(a_1g_1+ \ldots + a_ng_n)(b_1g_1 + \ldots + b_ng_n) = \sum_{g_ig_j = g_k} (a_ib_j)g_k
\end{align*}
where the multiplication is the natural construction derived from defining \((ag_i)(bg_j) = (ab)(g_ig_j)\).
\end{defn}
\(RG\) is commutative if and only if \(G\) is commutative. We can see \(R \leq RG\) by the "constant" sums of \(a_ie_G\). In fact, \(G\leq RG\) as well by taking \(a_i = e_R\), and because elements of \(G\) has inverses, \(G\) is a subgroup of the group of units of \(RG\).\\

If \(\left| G \right| >1\) then \(RG\) has zero divisors, given by
\begin{align*}
	(1-g)(1+g+\ldots+g^{m-1}) = 1-g^{m} = 1-1 = 0
\end{align*}
where \(g\) is an element with order \(m>1\).\\

If \(S\) is a subring of \(R\) then \(SG \leq RG\). Similarly, if \(H\leq G\), then \(RH \leq RG\).

\begin{exmp}[\(\Z D_8\)]
	Let \(G = D_8\) be the dihedral group of order \(8\) and \(R = \Z\). Some example of elements in \(\Z D_8\) could be \(\alpha  = r + r^2 - 2s\) and \(\beta = -3r^2 + rs\), and one can see that
	\begin{align*}
		\alpha + \beta = r - 2r^2 -2s +rs\\
		\alpha \beta = (r+r^2-2s)(-3r^2+rs) = r(-3r^2+rs) + r^2(-3r^2+rs) - 2s(-3r^2+rs) = -3 - 5r^3 + 7r^2s + r^3s
	\end{align*}
\end{exmp}

\begin{exmp}[\(\R Q_8\)]
	An interesting example is the group ring \(\R Q_8\). This ring is distinct from the Hamilton quaternions \(\mathbb{H}\) even though \(Q_8 \subset \mathbb{H}\). The unique element of order \(2\) in \(Q_8\) is NOT the additive inverse of \(1 \) in \(\R Q_8\), even though it is in \(\mathbb{H}\). It also contains zero divisors and hence is not a division ring.\\

	However, if one takes the quotient \(\R Q_8 / \left( 1 + (-1), i + (-i), j + (-j), k + (-k) \right) \), then it is isomorphic to \(\mathbb{H}\).
\end{exmp}
In other words, we only apply the group operation between elements of the group ring when multiplying two elements. The group elements hence serve as sort of a basis, that interacts multiplicatively via the group action.

\subsection{Rings of Fractions}
\label{sub:rings_of_fractions}

Let \(R\) be a commutative ring. Recall that if we have a non-zero non-zero divisor element \(a \in R\), then \(ab = ac \implies b = c\). This property is similar to division even if \(a\) is not a unit. Our goal will be to define a larger ring \(Q\geq R\) so that elements like \(a\) are units. This becomes particularly useful when \(R\) is an integral domain, as then \(Q\) becomes a field known as the \textbf{field of fractions} or \textbf{quotient field}.

\begin{thm}
	Let \(R\) be a commutative ring and \(D\subset R\) a subset without \(0\), zero divisors, and is closed under multiplication. Then there is a commutative ring \(Q\) such that \(R\leq Q\) and for all \(d \in D\), \(d \in Q\) is a unit.\\

	Furthermore, every element of \(Q\) is of the form \(rd^{-1}\) for some \(r \in R\) and \(d \in D\). If \(D = R - \left\{ 0 \right\} \), then \(Q\) is a field.\\

	The ring \(Q\) is the smallest ring containing \(R\) in which all elements of \(D\) become units. That is, let \(S\) be a commutative ring and \(\varphi :R\to S\) be an injective ring homomorphism such that \(\varphi (d)\) is a unit in \(S\) for every \(d \in D\). Then there is an injective homomorphism \(\Phi :Q\to S\) such that \(\Phi\mid_R = \varphi \). In other words, any other ring that makes \(D\) into units must contain an isomorphic copy of \(Q\).\\

	We call the ring \(Q\) the \textbf{ring of fractions of \(D\)} and denote it by \(D^{-1}R\). If \(R\) is integral, then \(Q\) is the \textbf{field of fractions} of \(R\).
\end{thm}
What does this actually look like? Caution must be exercised, as when dealing with fractions we are dealing with equivalence classes. Hence we will define an equivalence class on \((r,d)\) with \(r \in R\) and \(d \in D\) by
\begin{align*}
	\dfrac{r}{d} = \left\{(a,b) \mid a \in R,\, b \in D,\, rb = ad \right\}
\end{align*}
Then \(Q\) is the set of equivalence classes \(\dfrac{r}{d}\). Then we define addition and multiplication by
\begin{align*}
	\dfrac{a}{b}+ \dfrac{c}{d}= \dfrac{ad+bc}{bd} \quad \dfrac{a}{b} \times \dfrac{c}{d} = \dfrac{ac}{bd}.
\end{align*}
We leave it as an exercise to verify that this indeed gives \(Q\) the structure of a commutative ring. We embed \(R\) into \(Q\) by defining
\begin{align*}
	\iota : R\to Q \quad \iota:r\mapsto \dfrac{rd}{d}
\end{align*}
for any \(d \in D\). This is obviously in the equivalence class of \(\dfrac{re}{e}\), so the choice of \(d\) does not matter. This is a ring homomorphism and is in fact injective because \(d\) is not a zero divisor, and so this tells us that \(\iota(R)\leq Q\) is isomorphic to \(R\).\\

We can also see that \(d \in D\) has a multiplicative inverse in \(Q\) as desired. That is,
\begin{align*}
	\left( \dfrac{de}{e} \right)^{-1} = \dfrac{e}{de}
\end{align*}
and one can see that every element of \(Q\) can be written by \(r \cdot d^{-1}\) for some \(r \in R\) and \(d \in D\).

\begin{rmrk}
	Recall that if \(A\subset F\) is a subset of a field, then the intersection of all the subfields of \(F\) containing \(A\) is a subfield of \(F\) called the \textbf{subfield generated by \(A\)}.\\

	This subfield is the smallest subfield of \(F\) containing \(A\).
\end{rmrk}

\begin{cor}
	Let \(R\) be an integra domain and \(Q\) be the field of fractions of \(R\). If a field \(F\) contains a subring \(R'\) isomorphic to \(R\) then the subfield of \(F\) generated by \(R'\) is isomorphic to \(Q\).
\end{cor}
% \printindex
\end{document}


%description: Math 290 HW Template

%%%%% Beginning of preamble %%%%%

\documentclass[12pt]{article}  %What kind of document (article) and what size

%Packages to load which give you useful commands
\usepackage{subfig}
\usepackage{graphicx}
\usepackage{hyperref}
\usepackage{amssymb, amsmath, amsthm}

%Sets the margins

\textwidth = 6.5 in
\textheight = 9 in
\oddsidemargin = 0.0 in
\evensidemargin = 0.0 in
\topmargin = 0.0 in
\headheight = 0.0 in
\headsep = 0.0 in
\parskip = 0.2in
\parindent = 0.0in

%defines a few theorem-type environments
% \newtheorem{theorem}{Theorem}
% \newtheorem{corollary}[theorem]{Corollary}
% \newtheorem{definition}{Definition}

\newtheorem{definition}{Definition}
\newtheorem{fact}{Fact}
\newtheorem{remark}{Remark}
\newtheorem{theorem}{Theorem}
\newtheorem{proposition}{Proposition}
\newtheorem{lemma}{Lemma}
\newtheorem{corollary}{Corollary}

\renewcommand{\labelenumi}{\arabic{enumi}.}
\renewcommand{\labelenumii}{\arabic{enumi}.\arabic{enumii}.}
\renewcommand{\labelenumiii}{\arabic{enumi}.\arabic{enumii}.\arabic{enumiii}.}
\renewcommand{\labelenumiv}{\arabic{enumi}.\arabic{enumii}.\arabic{enumiii}.\arabic{enumiv}.}
\newlength{\alginputwidth}
\newlength{\algboxwidth}
\newcommand{\alginput}[1]{\makebox[1.5cm][l]{ {\sc Input:}} \parbox[t]{\alginputwidth}{{\it #1}}}
\newcommand{\algoutput}[1]{\makebox[1.5cm][l]{ {\sc Output:}} \parbox[t]{\alginputwidth}{{\it #1}}}
\newcommand{\algtitle}[1]{\underline{Algorithm \ {\bf #1}} \vspace*{1mm}\\}

%%%%% End of preamble %%%%%

\begin{document}

\title{AI for Risk Game}

\author{
{Wah Loon Keng}\thanks{
Lafayette College,
Easton, PA 18042, USA.
kengw{\tt @}lafayette.edu.}
\qquad
{Benjamin H. Draves}\thanks{
Lafayette College,
Easton, PA 18042, USA.
dravesb{\tt @}lafayette.edu.}
      % \affaddr{Department of Computer Science}\\
%       \affaddr{Lafayette College}\\
%       \affaddr{Easton, PA 18042, USA}\\
%       \email{gexia@cs.lafayette.edu}
}
% \date{}
\maketitle

\begin{abstract}
We implement an AI to play the board game $\emph{Risk}$. The game is formalized as an optimization problem in graph theory, where countries are represented as nodes in an undirected graph, and decisions are considered based on the graph properties. We introduce solution algorithms, with variable parametrizations, which are then implement as AIs with different personalities in {\tt JavaScript}. We set up games among AIs to measure their performances, and discover unusual game strategies.
\end{abstract}

\section{Introduction} \label{intro}

The board game $\emph{World Domination RISK \circledR}$ is a game of military strategy, where players of different factions try to conquer all 42 countries on the map, by deploying armies to attack and defend. We use the classic $\emph{Hasbro}$ version of the game; the rules are well known and can easily be found online, but they will be included as we walk through our algorithms.

In this paper, we first set up our formalization of the game as a graph optimization problem. Next we introduce graph algorithms to carry out each of the game moves, and explain our reasoning behind them. Then, we combine these to implement an AI, with variable personalities based on the parametrization of its internal algorithms. The different AIs then play multiple games against one another, with their results recorded. Finally, we analyze their performances, and find several unusual, interesting strategies discovered by the AIs that are never observed from human players.

This entire project is public on GitHub: \url{https://github.com/kengz/Risk-game}.

\section{Formalization} \label{formalization}

The game is inherently dependent on the board, which is a world map of 42 countries, interconnected in specific ways. Decisions to attack or defend are based on the distribution of the armies, the surroundings of a location, and connectivity of countries. These motivate our formalizing the game board and algorithms based on an undirected graph. From now we shall refer to the graph representation as $\emph{map}$:

\begin{definition} \label{map}
A $\textbf{map}$ is a connected, undirected planar graph, with 42 nodes, each representing a country. The nodes are named with indices $0-41$, and are connected the same way as are countries on the game board by undirected edge of weight 1.
\end{definition}

We assign data fields to each node, namely its country name, the continent it is in, its player owner, the number of armies of the owner in it, its worth and pressure as determined by some metric described below.

\begin{definition} \label{region}
A $\textbf{region}$ is a connected subgraph consisting of nodes all owned by the same player. Each player can own many regions, which together partition the map.
\end{definition}

\begin{definition} \label{border}
A $\textbf{border}$ node of an AI is its node that is adjacent to at least an enemy node.
\end{definition}

\begin{definition} \label{attackable}
A $\textbf{attackable}$ node for an AI an enemy node adjacent to its border.
\end{definition}

\begin{definition} \label{shape}
The $\textbf{shape}$ of a region is the measure of its shape/roundess. To compute the shape, find the maximum and minimum distances between the border nodes in the region, and shape=(max-min)/max. If a region is round, shape = 0; if it is a line, shape = 1.
\end{definition}

\begin{definition} \label{radius}
$\textbf{Radius}$ is the measure of shortest distance from an origin node. We identify the neighbors of node $\mathcal{O}$ at radius k to be the nodes whose shortest distance from $\mathcal{O}$ is k.
\end{definition}

Furthermore, we define the fields that will be useful in our algorithms:

\begin{definition} \label{worth}
The $\textbf{worth}$ of a node is the measure of its importance to an AI, as calculated by its internal metric algorithm, and is used by the AI to prioritize its decisions: which node should it defend/attack first.
\end{definition}

\begin{definition} \label{pressure}
The $\textbf{pressure}$ of a node as perceived by an AI is the measure of the average army distribution around the node, up to 5 unit radii away. It is calculated by the AI's internal metric and used to prioritize decisions.
\end{definition}

Note that the worth and pressure of a node are not the same when calculated by opposing AIs due to different perceptions, metric and AI personalities. Each AI will be calculating these values for all 42 nodes at each turn.

Finally, we introduce a data structure as the raw representation of overall army distribution on the map for various calculations:

\begin{definition} \label{RMAM}
The $\textbf{Radius Matrix (RM)}$ from an origin node $\mathcal{O}$ is the matrix that better represents the connectivity of neighbor nodes of the origin within some radius. It is enumerated by the Radius Matrix Algorithm below, and each entry is the name of some node.

Its corresponding $\textbf{Army Matrix (AM)}$ is a different representation of the RM, with each entry now being $z \in \mathbb{Z}$, where $|z|$ is the number of armies at the node, and $z$ is positive if the node is owned by the calculating AI, and negative otherwise.
\end{definition}

\section{Algorithms}

We now enumerate the algorithms for each step of the game, which will collectively form the final algorithm used by the AI to play the game.

\subsection{The Matrix Algorithms}

\algtitle{Radius Matrix (RM) for an origin node $\mathcal{O}$}
Starting from an origin node $\mathcal{O}$, initialize an empty matrix for its RM,
\begin{enumerate}
	\item Add the index of each adjacent node (at radius 1) of $\mathcal{O}$ to a new row in RM.
	\item Repeat for $i \in \{2,3,...,n\}$, where $n$ is the maximum radius covered:

	For each entry $p$ at column $i$, get all $n_p$ of its adjacent nodes at radius $i+1$ from $\mathcal{O}$.

	\item Duplicate the row of entry $p$ while appending to it each of the $n_p$ adjacent nodes at column $i+1$. If $n_p=0$, append $``empty"$ instead. The process is akin to a Cartesian product.

	\item Return the RM for $\mathcal{O}$.
\end{enumerate}

Note that the column number will coincide with the radius from $\mathcal{O}$. The RM with $n$ columns is a representation of the connectivity from the origin up to radius $n$, where each row is the shortest path from the origin to a point at radius $n$, and there may exist many such paths.

\algtitle{Army Matrix (AM) for an origin node $\mathcal{O}$}
We can convert an RM into AM, a representation using the number of armies,
\begin{enumerate}
	\item Find the RM for node $\mathcal{O}$ using the RM algorithm.
	\item For each entry $p$ in RM, if node $p$ has the same owner as $\mathcal{O}$, replace the entry with the number of army at $p$; else, replace with the negative of the number of army at $p$. If an entry $p$ is $``empty"$, append 0 instead.
	\item return the AM for $\mathcal{O}$.
\end{enumerate}

This transforms an RM into its alternate form AM, which gives a representation of the army distribution and connectivity around the origin node $\mathcal{O}$. This matrix can be used for calculating the $\textbf{pressure}$ from definition \ref{pressure}. For our project we calculate the matrices up to radius 5, which we think is sufficient given that per game turn a player can only move adjacently among nodes.

\subsection{The Pressure Algorithm}

The pressure of each node from an AI's point of view is the average number of army surrounding the node. More positive pressure indicates the node is a better stronghold of the AI; more negative pressure indicates is surrounded by more enemies.

The calculation of pressure depends on the AI's perception of threat, which can be represented using a metric that varies based on its personality.

\begin{definition}
The $\textbf{threat perception}$ of an AI is the way it sees the threat of army distribution up to some radius away poses on an origin node. E.g. 10 enemy armies further away poses less threat than 5 enemy armies nearby. The $\textbf{threat perception}$ is quantified by defining a metric: a normalized vector or length = max radius of AM, where the individual value of the vector is the weight multiplied to the army number at that radius. The procedure is describe below.
\end{definition}

\algtitle{The Metric Algorithm}
To enumerate the metric for an AI's threat perception, with scope radius = 5,
\begin{enumerate}
	\item Choose a weight function, for example, constant, Gaussian,
	\item Evaluate function values for with the input distance vector $\{1,2,3,4,5\}$
	\item Renormalize the output vector and return it as the metric vector.
\end{enumerate}

This metric vector $\mathbf{w}$ is then dotted with a row $\mathbf{r}$ in the AM, which is a list of number of armies at incremental distance away from an origin node $\mathcal{O}$, and the partial pressure $\emph{PP}$ for it is:
$$\emph{PP}(\mathbf{r}) = \mathbf{w} \cdot \mathbf{r}$$

\algtitle{The Pressure Algorithm}
The AI calculates the pressure for each node using its personality trait $\textbf{threat-perception}$, or the metric vector $\mathbf{w}$:
\begin{enumerate}
	\item Update the data fields of the map and call the AM algorithm to compute the AMs for all 42 nodes.
	\item For each node $\mathcal{O}$, compute the dot product between $\mathbf{w}$ and each row of the node's AM; the result is a column vector $\mathbf{c}$.
	\item The first column of the original RM is a repeated list of $m$ adjacent nodes of $\mathcal{O}$, suppose each node $i$ repeats $q_i$ times in the column, so in total the column has length $q_1 + q_2 + \cdots + q_m$. Renormalize this sequence into $nq_1 + nq_2 + \cdots + nq_m$ For each batch $q_i$ of the column vector $\mathbf{c}$ from above, take its mean, then multiply by the renormalized weight $nq_i$. Then sum all $m$ of the results, call this scalar $s(\mathcal{O})$.
	\item Now that the column $\mathbf{c}$ has been reduced to a scalar representing the average army distribution around the origin $\mathcal{O}$, account for the number of armies (sign-sensitive, negative for enemy) here $a(\mathcal{O})$ by adding the scalar, and return the pressure of node $\mathcal{O}$, $P(\mathcal{O}) = s(\mathcal{O}) + a(\mathcal{O})$.
\end{enumerate}

Thus at each turn, the AI updates the data fields and calculates the pressure, i.e. the average number of surrounding armies, for each node, using its threat perception metric.

\subsection{The Worth Algorithm}

\algtitle{The Worth Algorithm}
At each turn, the AI evaluates the worth of each node to prioritize its attacks and defenses. Suppose it considers $m$ factors, each of which assumes a real positive value, with more positive being more worthy. To compute the final worth scalar, simply order the $m$ factors from the most vital, and dot it with a factor vector $\{10^{m-1}, ..., 100, 10, 1\}$.

For our AI, we consider the following factors (ordered from the most important). For each node $\mathcal{O}$ calculate and append to the list of factors:

\begin{enumerate}
	\item continent-fraction = $\frac{\text{(number of nodes with the same owner in the same continent $\mathcal{O}$)}}{\text{(total number of nodes in the continent)}}$
	\item If $\mathcal{O}$ is own node, the region-index: Enumerate for each player its nodes, and group them by regions, then order them from the biggest to the smallest regions. The region-index of $\mathcal{O}$ is its index in this list. Or if $\mathcal{O}$ is enemy, the attackable index: of the region list enumerated above, extract the sublist with nodes that are attackable, i.e. is an enemy adjacent to one of your nodes. The attackable index of $\mathcal{O}$ is its index in this sublist; -1 otherwise.
	\item shape: find the region $\mathcal{O}$ is in and compute the shape as in definition \ref{shape}.
	\item degree: the degree of $\mathcal{O}$, i.e. the number of adjacent nodes it has.
	\item pressure: as calculated from the pressure algorithm.
	\item Finally, return the dot product between this factor list and $\{10^{4}, 1000, 100, 10, 1\}$.
\end{enumerate}

After obtaining an ordered list of worth nodes, we can partition it while preserving the order into lists of border nodes and attackable nodes, and reorder them based on strategies. Furthermore, the AI makes it context-sensitive by remembering the pressures from the past turn, and reorder the list based on pressure-drop between turns.

To justify our factors above, conquering a whole continent gives a player extra armies per turn while strengthening the region. Furthermore, a larger region is harder to attack than a smaller region, thus we place more importance on the nodes there.

The shape of a region is vital for defense and army mobility during fortification. This is the classic problem of minimizing the surface/volume ratio, or in this lower dimensional case, the perimeter/surface ratio. Intuitively, a thin region is vulnerable, and has bad army mobility. A thick, concentric shape is stronger. The average distance between nodes is also shorter and thus aids mobility. Moreover, a node with higher degree improves mobility since it can reach many other nodes.

The pressure measures the ease of attacking or defending a node; the less negative a node is, the less enemy presence it has. It is wise to not attack the enemy's stronghold, but to seek its weak point of entry, which can be detected from a less-negative pressure.

\subsection{The Priority Algorithm}
At each game turn, the AI updates the priority nodes to attack/defend. The list of priority nodes depends on the AI's personality trait $\textbf{priority}$, whether it is agressive (attack-then-defend) or defensive (defend-then-attack).

\algtitle{The Priority Algorithm}
\begin{enumerate}
	\item Update the data fields for the AI.
	\item Call the pressure algorithm on the map.
	\item Call the worth algorithm on the map.
	\item Repartition the worth nodes and reorder by attackables/borders first based on the AI's personality, whether agressive or defensive. Furthermore, for the attackable, choose the best origin of attack by the highest pressure.
\end{enumerate}

\subsection{The Placement Algorithm}
This describes how the AI places the armies into the its priority nodes based on its personality trait $\textbf{placement}$.

\algtitle{The Placement Algorithm}
\begin{enumerate}
	\item If the trait is $\textbf{cautious}$, place armies along its  priority nodes (if is enemy, use the best origin of attack) until all pressures are $>0$, then with the extra armies, place 4 each down the same list; repeat until none left.
	\item If the trait is $\textbf{tactical}$, place armies down the list until node pressure is $>4$, then with extra, place 4 each down the list; repeat until none left.
\end{enumerate}

\subsection{The Attack Algorithm}
The AI decides to launch attacks from the best attack origin (calculate with in priority list) based on its personality trait $\textbf{attack}$.

\algtitle{The Attack Algorithm}
For all attackables down the priority list,
\begin{enumerate}
	\item If the trait is $\textbf{rusher}$, the AI harassess constantly, i.e. while the best attack origin has 2 more armies than the enemy target, keep attacking before moving to next target.
	\item If the trait is $\textbf{carry}$, same as above, but the difference threshold is 4 (higher). Furthermore, the AI will accumulate the cards to reserve more armies for late game.
\end{enumerate}

\subsection{The Fortifying Algorithm}
All AIs use the same fortifying algorithm.

\algtitle{The Fortifying Algorithm}

\begin{enumerate}
	\item Find the border node $\mathcal{O}$ with the lowest pressure, and find a non-border ally node with higher pressure, transfer all but 1 troop to the border node if possible. This is to always push the unused central forces out to the borders where armies are mostly needed.
	\item If no fortification done above, find a border node with the highest pressure, and transfer any neighboring armies (all but 1) to it. This is for the accumulation of armies during late game by making strong node even stronger.
\end{enumerate}

This algorithm aims to create a center-weak border-strong army distribution within a region with, that is to utilize the maximum number of armies by putting them to the border nodes. Such distribution is efficient for both offense and defense. The second part of the algorithm kicks in when there is less border nodes during the late game, when one player controls a larger region. This will accumulate all armies toward a border node to overwhelm the enemy, hopefully ending the game quicker.

\section{AI Algorithms and Personalities}

We now put everything together to form the AI, which has four personality traits parametrized in its algorithms:

\begin{enumerate}
	\item The $\textbf{threat perception}$ trait /metric in the Pressure Algorithm; function variations: \{Constant, Survival\}.
	\item The $\textbf{priority}$ trait in the Priority Algorithm; variations: \{agressive, defensive\}.
	\item The $\textbf{placement}$ trait in the Placement Algorithm; variations: \{cautious, tactical\}.
	\item The $\textbf{attack}$ trait in the Attack Algorithm; variations: \{rusher, carry\}.
\end{enumerate}

Thus there are $2^4 = 16$ AI personalities, and more if we allow richer variations.

Corresponding to the game moves per turn:
\begin{enumerate}
\item Getting and placing new armies;
\item Attacking, if you choose to, by rolling the dice;
\item Fortifying your position (moving troops between an adjacent pair of your nodes),
\end{enumerate}

the AI has these primary methods:
\begin{enumerate}
\item Update: Call the Priority and Worth algorithms.
\item Get-and-Place-Armies: Call the Placement Algorithm.
\item Attack: Call the Attack Algorithm as many times as wanted.
\item Fortify: Call the Fortify Algorithm.
\end{enumerate}

A game can have as many participating AIs as permitted by the rules. During the initial game setup, countries are randomly assigned to the AIs. Then, the AIs take turn to call their Update and Get-and-Place-Armies methods to complete the setup. Then the game begins and the AIs take turn to call all their primary methods in sequence, until the game terminates, or ties at the maximum number of rounds.

As opposed to physical game, the virtual game has no limit on the number of army pieces $-$ it just keeps creating more as needed; nor it has the limit on the cards $-$ it keeps reshuffling a new deck once an old one runs out.

Next we present the results from our implementation.

\section{Implementation Results}

\subsection{Formalization of Problem}
Our initial approach to the problem is one that focuses on the gross number of countries and armies an AI possesses throughout the game. As the game progresses, we monitor how the AI’s interact in their attempt to win both armies and territories based on their personalities. From this, conclusions were drawn regarding personality types, player order effect, as well as game starting conditions.

\subsection{Algorithms and Decisions}

\subsubsection{Initial Concerns}
An initial issue was normalization of the time axis across several games. While games were initially capped at 100 rounds, where the game was declared a draw, the games that did not end in a draw had variable game length. The majority of the data analysis done in this project was done where there was a definitive winner. From this, the time axis was normalized by considering games in terms of percentage of game length. To the AI the length of the game is unknown during gameplay. However, the analysis can show where pivotal points occur across the game. While initially, players will be unable to use this information an understanding of both territorial and army control will influence their decisions.

\subsubsection{The Difference Plots}
In this analysis, the percentage of both armies and countries in total will be compared to the percentage of the game in where these points occur. These points for players \emph{p1, p2} will be found using the following
$$\emph{diff\_army} = \frac{\emph{p1\_army - p2\_army}}{\emph{p1\_army - p2\_army}}$$
$$\emph{diff\_country} = \frac{\emph{p1\_country - p2\_country}}{\emph{p1\_country - p2\_country}}$$

This data is stored in a matrix that holds both army numbers and round numbers. In this matrix, data from games resulting in draws is discounted. The equation will hold for any winner. From this we expect a divergent trend from zero as the game proceeds to an end. From this matrix the difference in both armies and countries are calculated and plotted in R. From this, R will produce a trend line. This trend line will provide information for both the initial game play configuration and the development of the game.

\subsubsection{The Fun Function}
In this analysis a fun function was built to hold and later display some basic analysis of the games played. The fun function includes, total wins of both AI’s, probability of winning, odds of winning, the average total armies, the average total armies during a win, the maximum number of armies needed to win , the minimum number of armies needed to win, average turns, and average turns without ties. When a game play data set is passed from the JSON through the rjson package, the function returns this summary of game play.

Imbedded in this function is the survival plot of two AI’s. The survival package in R allows from the plotting of the survival of both armies over the course of the game. As before, the data is filtered to view only games that have a definitive winner. These graphs show the deterioration of the two AI’s over the course of the game. From this, conclusions regarding how the game develops can be made.

\subsection{Results, Performance, and Analysis }
We immediately see several trends given by the fun function. A clear player order effect was found between nearly every AI. While some AI’s were simply more powerful and able to win regardless of characteristics, in nearly every case we see that more wins are generated from the AI if they play first compared to when they play second. For some more closely related AI’s, the odds of winning changes in its favor given the player order effect. From this we can conclude that for certain comparable AI’s, player order may greatly affect the outcome of the game.

Furthermore, in every case, the trend line from the total army plot simply shifted its intercept value but held consistent slope. This intercept can be credited to first round play. After the first round (when the first point is calculated) the army advantage can be a one player advantage. This first round however rarely changes the results of the game. Consider figure 1.

\begin{center}
\includegraphics[scale=0.6]{images/Figure_1.jpg}\\
{\footnotesize Figure 1.}
\end{center}

Here the green and red dots represent AI0’s and AI4’s percent total armies respectively over time. Again, the dots only represent games in which a definitive winner was found. Here we see that AI0 has a clear advantage but when the player order is changed, AI4 wins more games. Notice, however, while the trend line shifts down, that the slope stays constant. This implies that while the game is certainly affected by player order, on average, the game develops the same regardless of player order.

For the same AI match, the fun function provides two survival charts. Now consider figure 2.

\begin{center}
\includegraphics[scale=0.6]{images/Figure_2.jpg}\\
{\footnotesize Figure 2.}
\end{center}

In the first plot we see that player two holds several armies until near round 50. Suddenly over the next two rounds however, we see an incredible drop off. AI0 has mostly defensive characteristics. From this it appears that AI0 condenses its portion of the graph and builds armies. Somewhere near halfway through the game, it branches out and gains several territories in a very small period of time. When the order of the game is reversed we see a much more even trend. Both AI’s seem to lose armies at a consistent rate. This effect could be accredited to player order effect or the characteristics of the players in regards to their section of the sub graph.

While these trends, in general, hold true, one AI in particular defied the general trends. We will call this AI, AI3. AI3, in general, tended to produce more wins when playing in the second player position. This trend was tested more rigorously against 15 new AI’s. From this data, the fun function produced the win records for both AI3 and the other AI’s for both player order. The difference between wins was then calculated. That is $\Delta \emph{win} = \emph{AI3\_wins} - \emph{AIOpponent\_wins}$. This figure was calculated for both player orders. This data is represented in figure 3.

\begin{center}
\includegraphics[scale=0.6]{images/Figure_3.jpg}\\
{\footnotesize Figure 3.}
\end{center}

As in figure 3, on average AI3 wins 3.625 games while playing second in comparison to playing first where the AI won only 0.5 games on average. This in turn would seem to offer a solution to the player order effect, mentioned above. By simply playing the AI3 characteristics (“Survival”, “Defensive”, “Cautious”, “Carry”), on average the advantage would go to the second player, thus making player order obsolete.

Another nuanced observation shows that this conclusion may not be entirely true. Under further observation, another AI which we will call AI4 (“Survival”, “Aggressive”, “Tactical”, “Rusher”) seemed to be superior to AI3 in both player orders. A more rigorous study of these two AI’s revealed that the assumption was correct. The fun function showed that on average, AI4 wins more often than AI3. Consider figure 4 and figure 5.

\begin{center}
\includegraphics[scale=0.6]{images/Figure_4.jpg}\\
{\footnotesize Figure 4.}\\
\includegraphics[scale=0.6]{images/Figure_5.jpg}\\
{\footnotesize Figure 5.}
\end{center}

Notice that these two figures show very low correlation when player order changes. When AI3 goes first, it grosses more wins than AI4. When AI3 goes second, however, AI4 wins the majority of the games. While by a narrow margin, when AI4 goes first, AI4 wins 56 more games than AI3 in a 500 game trial. Thus if AI3 plays second they will on average lose to AI4.

\subsection{AI behaviors and Surprises}
In this project several AI’s were posed against each other in a game of Risk. Initially, it was predicted that a large player order effect would affect the game. This hypothesis held true for the majority of AI’s that were tested in this experiment. AI3 however, proved that by using the correct strategy, a player would actually hold an advantage by going second in the game. From this however, AI4 proved that their still exist AI’s that can beat AI3 while being the first player to move.

From this project, we advise future Risk players to abstain from declaring their player strategy until the player order is determined. From this, we advise that second players play a cautious, defensive game, while first players play with aggressive tendencies. A consistent analysis of strategy will reveal the best strategy for either player to take throughout the duration of the game.

\section{Conclusion}
We have reformalized the board game \emph{Risk} as a graph optimization, and implemented an AI to solve the problem, i.e. to play the game. Several decision algorithms were devised based on graph properties, and they in turn form the personalities of the AI. Our study of 4 binary traits, or a total of 16 personality variations of the AI, is merely the beginning. We have discovered some interesting AI behaviours and performance at a level beyond any human players. One can potentially investigate even more variations of the traits.

For future studies, an analysis of subgraphs would provide deep insight into the value of individual territories. The AI’s are built in such a way that the priority function is based off of current demographic of troops and shape of their graph. Evaluating the graph as a whole would provide insight to the value of different subgraphs that would proactively affect the priority function. This conditional thinking is much more like the ideas of a human player and thus could inform future strategies.

Furthermore, game shifts could be analyzed using the fun function and the aforementioned subgraph analysis. This would allow for defensive players an understanding of which territories that are worth defending and worth conceding. This shift parameter would allow for insight to game length, army distribution, and other key factors.

\section{Citations}

Hasbro, \emph{Risk, The World Conquest Game \circledR, Rules ©1959, 1963, 1975, 1980, 1990, 1993}, Parker Brothers, Division of Tonka Corporation, Beverly, MA 01915, U.S.A.

\end{document}


\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{unicode,math}
\usepackage[margin=30mm]{geometry}
\newenvironment{eqsplit}{\equation\aligned}{\endaligned\endequation}

\begin{document}
\title{Useful geometric computatoins}

\section{Uniform approximation of a parabolic arc by a polygonal chain}%««1
\begin{prop}\label{prop:points}
Let~$𝒫$ be the parabola with equation $y = f(x) = a/2 + x²/2a$.
Let~$H$ be the hypergeometric function
$H(u) = {}_2F_1(1/4,1/2;\;3/2\;-u^2)$.

Choose real numbers~$s_1$, $s_2$ and an integer~$n ≥ 1$
and define, for~$i = 0, …, n$,
$x_i = a H^{-1}((1-i/n) s_1 + (i/n) s_2)$.

Then, for $n$ large enough,
the polygonal line with vertices~$A_i = (x_i, f(x_i))$
lies within a Minkowski distance~$δ = \frac{a}{8n^2}
(s_2-s_1)^2+O(n^{-4})$ of the parabola~$𝒫$.
\end{prop}

\begin{proof}
It is enough to prove this for an individual segment of the polygonal line,
for example the segment $(A_0, A_1)$.
For~$x_0 ≤ x ≤ x_1$, the distance between a point~$(x, f(x)) ∈ 𝒫$
and the segment~$(A_0, A_1)$ is given by the function
% 	the maximum on the interval~$[x_0,x_1]$ of the function
\begin{equation}\begin{split}
g(x)
%  &= \frac{(x_0+x_1)/(2a) x - f(x) + x_0 x_1/(2a) + a/2}{√{1+(x_0+x_1/2a)^2}}\\
 &= \frac{(x_0+x_1) x - 2a f(x) + x_0 x_1 + a^2}{√{(2a)^2+(x_0+x_1)^2}}\\
%  &= \frac{(x_0+x_1) x - a^2 - x^2 + x_0 x_1 + a^2}{√{(2a)^2+(x_0+x_1)^2}}\\
 &= \frac{(x-x_0)(x_1-x)}{√{(2a)^2+(x_0+x_1)^2}}.\\
\end{split}\end{equation}
The maximum value~$δ_0$ of~$g$ on the interval~$[x_0,x_1]$
is reached for~$x = (x_0+x_1)/2$
and amounts to $δ_0 = \frac{(x_1-x_0)^2}{4√{(2a)^2+(x_0+x_1)^2}}$.

Let~$G = H^{-1}$ and~$x(t) = a G((1-t) s_1 + t s_2)$.
Then for all~$i = 0,…, n$, $x_i = x(i/n)$
and $x_{i+1} - x_i = x'(i/n)/n + O(1/n^2)$.
Therefore
\begin{equation}
δ_i = \frac{x'(i/n)^2}{8n^2 √{a^2 + x_i^2}} + O(1/n^4).
\end{equation}

Since $H'(u)^4 = 1/(1+u^2)$,
the inverse~$G$ of~$H$ satisfies the differential equation
$(G')^4 = 1 + G^2$,
and hence $x'(t)^2 = a (s_2 - s_1)^2 √{a^2 + x(t)^2}$.
Accordingly, one finds
\begin{equation}
δ_i = \frac{a}{8n^2} (s_2-s_1)^2 + O(1/n^4).
\end{equation}
Since this is true for all~$i = 0,…,n-1$, the proposition follows.
\end{proof}

\begin{prop}\label{prop:approx}
The following algorithm approximates the parabola~$𝒫$
on the interval~$[X_1,X_2]$ within a distance~$≤ δ + O(δ^2)$.
\begin{enumerate}
\item For~$i = 1,2$, define $s_i = H(X_i/a)$.
\item Let~$n ≥ \abs{s_2-s_1} √{a/(8δ)}$.
\item Define the points~$(x_i=a H^{-1}(s_1 + \frac{i}{n}(s_2-s_1), f(x_i))$
as in Prop.~\ref{prop:points}.
\end{enumerate}
\end{prop}

% \section{Arc length of an Archimedean spiral (wrapped line segment)}%««1
%
% Let~$F$ be the transformation on~$ℝ^2$ defined by
% $F(x,y) = (x \cos(y/x), x \sin(y/x)$.
%
% \begin{prop}
% Let~$S = (p_0, p_1)$ be a line segment, with $p_i = (x_i, y_i)$,
% such that $0 < x_0 < x_1$.
% Let~$a = x_0 - x_1$, $D = x_0 y_1 - y_0 x_1$,
% and $z_i = D+√{a^2 x_i^2 + D^2}$.
% Then the arc length of the image curve~$F(S)$ is
% \[ \begin{cases}
%   \frac{D}{a} \log \frac{x_1 z_0}{x_0 z_1}
% 	+ a \pa{\frac{x_1^2}{z_1}-\frac{x_0^2}{z_0}}&
% 	\text{if $x_0 ≠ x_1$;}\\
% 	x_0 \abs{y_0 - y_1} &\text{if $x_0 = x_1$.}\end{cases}
% \]
% \end{prop}
% \begin{proof}
% The segment is parametrized as $p(t) = (1-t) p_0 + p_1
%  = (t (x_1-x_0) + x_0, t (y_1 - y_0) + y_0).
%  = (a t +  b, c t + d)$
% with $a = (x_1-x_0)$, $b = x_0$, $c = (y_1 - y_0)$, $d = y_0$.
% In complex notation, one finds
% $F(p(t)) = (a t + b) \exp(i \frac{ct+d}{at+b})$ and hence
% \begin{eqsplit} F(p(t))'
% &= a e^{i…} + (a t + b) i \frac{bc-ad}{(at+b)^2} e^{i…} \\
% &= \pa{a + i \frac{bc - ad}{at + b}} \exp\pa{i \frac{ct+d}{at+b}};\\
% \abs{F'(p(t))}
% &= √{a^2 + \frac{(ad-bc)^2}{(at+b)^2}}.
% \end{eqsplit}
% The arc length is therefore
% \begin{eqsplit}
% &= ∫_0^1 \abs{F'(p(t))} dt
% &= ∫_0^1 √{a^2 + \frac{(ad-bc)^2}{(at+b)^2}} dt\\
% &= ∫_0^1 √{a^2(at+b)^2 + (ad-bc)^2} \;\frac{dt}{at+b} \\
% % &= a ∫_0^1 √{(at+b)^2 + (d-bc/a)^2} \;\frac{dt}{at+b} \\
% \end{eqsplit}
% Define $s = (at+b)/(D+√{a^2(at+b)^2+D^2})$,
% so that $t = -b/a+2sD/(a^2(1-s^2))$,
% $√{(at+b)^2+D^2/a^2} = D\frac{1+s^2}{1-s^2}$,
% and $dt = 2D/a^2 \frac{1+s^2}{(1-s^2)^2} ds$.
% Then
% \begin{eqsplit}
% G(t) = ∫ \abs{F'(p(t))} dt
% &= \frac{D}{a} ∫ \pa{\frac{1+s^2}{1-s^2}}^2 \frac{ds}{s}\\
% &= \frac{D}{a} ∫ \pa{\frac 1s +\frac{1}{(s-1)^2} - \frac{1}{(s+1)^2}} ds\\
% &= \frac{D}{a} \pa{\log s + \frac{2}{1-s^2}}\\
% &= \frac{D}{a} \pa{\log \frac{at+b}{D+√{a^2(at+b)^2+D^2}}
%  + 2 \frac{(D+√{a^2(at+b)^2+D^2})^2}{(D+√{a^2(at+b)^2+D^2})^2-(at+b)^2}}\\
% &= \frac{D}{a} \pa{\log \frac{at+b}{D+√{…}}
%  + 2 + \frac{a^2}{D}\frac{(at+b)^2}{D+√{…}}}\\
% &= \frac{D}{a} \log \frac{at+b}{D+√{a^2(at+b)^2+D^2}} + \frac{2D}{a}
%  + a \frac{(at+b)^2}{D+√{a^2(at+b)^2+D^2}}
% \end{eqsplit}
% The primitive~$G-2D/a$ takes the following values:
% \begin{eqsplit}
% G(0)
% &= \frac{D}{a} \log \frac{x_0}{D+√{a^2 x_0^2 + D^2}}
%  + \frac{a x_0^2}{D+√{a^2 x_0^2+D^2}}\\
% G(1)
% &= \frac{D}{a} \log \frac{x_1}{D+√{a^2 x_1^2+D^2}}
%  + \frac{a x_1^2}{D+√{a^2 x_1^2+D^2}}\\
% \end{eqsplit}
% This concludes the case where~$a ≠ 0$. The other case is trivial.
% \end{proof}
% % »»1
% \section{Arc length of wrapped segment (correct)}%««1
% Let~$F(x,y,z) = ((x+r) \cos(y/r), (x+r) \sin(y/r), z)$.
% \begin{prop}
% Let~$S = (p_0, p_1)$ be a line segment, with $p_i = (x_i, y_i, z_i)$.
% Define~$a = x_0 - x_1$ and~$c = z_0 - z_1$,
% and $λ_i = √{b^2 x_i^2/r^2 + a^2 + c^2}$.
% If $a ≠ 0$ and $b ≠ 0$ then the arc length of the image curve~$F(S)$ is
% \[
%   \frac{1}{2a}(λ_1 x_1 - λ_0 x_0)
% + \frac{a^2 b^2+c^2 r^2}{2a b r} \log \frac{b x_1 + r λ_1}{b x_0 + r λ_0}.
% \]
% If $a = 0$ or~$b = 0$ then this arc length is $√{a^2 + b^2 x_0^2/r^2 + c^2}$.
% \end{prop}
% The segment is parametrized as
% $p(t) = (1-t) p_0 + p_1
%  = (t(x_1-x_0) + x_0, t (y_1 - y_0) + y_0, t (z_1 - z_0) + z_0)
%  = (a t + x_0, b t + y_0, c t + z_0)$.
% \begin{eqsplit}
% F(p(t))
% &= ((a t+x_0+r) \cos \frac{b t+y_0}{r}, (at+x_0+r)\sin\frac{bt+y_0}{r},
% ct+z_0) \\
% F(p(t))'
% &= (a \cos φ(t) - x(t)b/r \sin φ(t) , a \sin φ(t) + x(t)b/r \cos φ(t), c) \\
% \norm{F(p(t))'}
% &= √{a^2 + (at+x_0)^2 b^2/r^2 + c^2}
% \end{eqsplit}
% Using $∫ √{(α t+β)^2+γ^2} d t = \frac{α t+β}{2 α} √{…}
%   + \frac{γ^2}{2 α} \log(α t+β+√{…})$
% with $α = ab/r$, $β=x_0 b/r$, $γ^2 = a^2+c^2$:
% \begin{eqsplit}
% ∫ \norm{F(p(t))'} dt
% &= \frac{(b/r)(a t+x_0)}{2ab/r} √{…}
%   + \frac{a^2 b^2/r^2+c^2}{2ab/r} \log((b/r)(at+x_0)+√{(at+b)^2+a^2+c^2})\\
% &= \frac{x(t)}{2a} √{b^2 x(t)^2/r^2+a^2+c^2}
%   + \frac{a^2 b^2+c^2 r^2}{2a b r} \log(b x(t)/r+√{b^2 x(t)^2/r^2+a^2+c^2})\\
% \end{eqsplit}
% % Define $q = √{a^2+e^2}$ and~$s = (at+b)/(q+√{q^2+(at+b)^2})$; then
% % $t = -b/a+2sq/a(1-s^2)$, $√{q^2+(at+b)^2} = q(1+s^2)/(1-s^2)$,
% % and $dt = 2q/a (1+s^2)/(1-s^2)^2$.
% % Thus the arc length~$L$ is
% % \begin{eqsplit}
% % ∫_0^1 |F(p(t))'|\;dt
% % &= ∫_0^1 √{q^2+(at+b)^2}\; dt \\
% % &= \frac{2 q^2}{a} ∫ \frac{(1+s^2)^2}{(1-s^2)^3}\;ds \\
% % &= \frac{2 q^2}{a} ∫
% % \pa{\frac{1/4}{s+1}-\frac{1/4}{s-1}+\frac{s^4+6s^2+1}{2(1-s^2)^3}}\; ds\\
% % &= \frac{2 q^2}{a} \pa{ \frac 14 \log \frac{s+1}{s-1}
% %   + \frac 12 \frac{s(s^2+1)}{(1-s^2)^2}}\\
% % &= \frac{2 q^2}{a} \pa{\frac 14 \log \frac{q+b+u}{q-b+u}
% %  + \frac 12
% % \end{eqsplit}
%
% % »»1
\section{Sagitta of wrapped segment}%««1
Let~$F(x,y,z) = ((x+r) \cos(y/r), (x+r) \sin(y/r))$.
\begin{prop}
Let~$S = (p_0, p_1)$ be a line segment, with $p_i = (x_i, y_i)$.
% Define~$a = x_0 - x_1$ and~$c = z_0 - z_1$,
% and $λ_i = √{b^2 x_i^2/r^2 + a^2 + c^2}$.
The \emph{sagitta} of~$F(S)$ is the Minkowski distance
between the image curve $F(S)$ and the straight segment $(F(p_0),F(p_1))$.

\end{prop}
Distance in polar coordinates $(r e^{iθ})$:
\begin{eqsplit}
\norm{r_0 e^{iθ_0} - r_1 e^{iθ_1}}^2
&= \norm{r_0 e^{iθ_0}}^2 + \norm{r_1 e^{i θ_1}}^2
  - 2 (r_0 e^{iθ_0})⋅(r_1 e^{i θ_1}) \\
&= r_0^2 + r_1^2 - 2 r_0 r_1 \cos(θ_0 - θ_1).
\end{eqsplit}
Let~$Σ = (F(p_0), F(p_1))$,
then $ℓ(Σ)^2 = (x_0+r)^2 + (x_1+r)^2 - 2 (x_0+r)(x_1+r) \cos((y_0-y_1)/r)$.

The segment $(p_0, p_1)$ is parametrized as
$p(t) = (1-t) p_0 + p_1
 = (t(x_1-x_0) + x_0, t (y_1 - y_0) + y_0)
 = (a t + x_0, b t + y_0)$,
so that
$F(p(t)) = (ξ(t), η(t)) = ((r+x(t)) \cos(y(t)/r), (r+x(t)) \sin(y(t)/r))$.
The squared distance of a point $F(p(t))$ to the
segment~$Σ=(F(p_0),F(p_1))$
is
\begin{eqsplit}
δ(t)^2
&= \frac{1}{\norm{Σ}^2} \chev{F(p(t))-F(p_0),F(p_1)-F(p_0)}^2\\
&= \frac{(x(t)-x_0)(y_1 -y_0) - (y(t) - y_0)(x_1 - x_0)}{\norm{Σ}^2}\\
\end{eqsplit}

\end{document}
% vim: fdm=marker fmr=««,»»: