\documentclass[a4paper,11pt]{article}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{float}
\usepackage[utf8]{inputenc}

\title{MaS Assignment I\\
\large The Chirikov Map}
\author{Klaas Kliffen s2369494}
\date{\today}

\begin{document}

\maketitle

\section{Introduction}

The Chirikov map is an area preserving chaotic map. It is displayed in a square with 2$\pi$ as the length of the sides.

The Chirikov map is defined by the set of functions:
\begin{align}
p_{n+1} &= p_n + K \text{ sin}[2\pi x_n] / (2*\pi)\\
x_{n+1} &= x_n + p_{n+1}
\end{align}
These are taken modulo 1, to produce the following functions:
\begin{align}
p_{n+1} &= \{p_n + K \text{ sin}[2\pi x_n] / (2*\pi)\} \text{ mod} 1\\
x_{n+1} &= \{x_n + p_{n+1}\} \text{ mod} 1
\end{align}

Taking the modulo causes the Chirikov map to be displayed in a square with 1 as the length of the sides.
The functions take the parameter $K$ as a parameter. The initial conditions are defined by values of $p$ and $x$ each in $[0,1]$.

\subsection*{Initialisation}
The system is initialised by $\{x_0,p_0\} \in [0,1] \times [0,1]$

\subsection*{a) 'Orbits' in the $x$-$p$ plane for fixed $K$}
The first part of the assignment assumes a fixed value of $K = 1$ and varies the initial conditions. The aim of this part will be to distinguish between different kinds of 'orbits'.

\subsection*{b) Behaviour for different values of $K$}
The second part will use the orbits from the first part and will use a varying value for $K$.
The aim of this part will be to explore the changing orbits for an increasing value of $K$

\section{Methods}
This section globally describes the adapted functions used in this assignment.

\subsection*{a) 'Orbits' in the $x$-$p$ plane for fixed $K$}
To generate the values describing the orbit, a step function is created to accept 2 coordinates and calculates the next coordinates from the equations 3 and 4.
This function can be used to explore the different kind of orbits given the initial values.\\
For each initial value, the first thousand iterations are run. To determine the order in which points are visited, another number of iterations may be given. These will be noted in the result section.\\

\subsection*{b) Behaviour for different values of $K$}
To plot multiple orbits for a given $K$, the step function is called multiple times, each with different pseudo-random initial values. The calculated values are then stored in two arrays to be plotted.\\
For each value of K, 50 orbits are calculated with pseudo random initial conditions, each with 1000 iterations.

\section{Results}
\subsection{a) 'Orbits' in the $x$-$p$ plane for fixed $K$}
There are three types of orbits that can be found. These will be addressed each in its own section.
\subsubsection{Points}
Barely visible are two single points in figure \ref{pointorbit} at $(0,0.5)$ and $(0.5,0.5)$. From figure \ref{pointval} it is clear that $p$ stays constant, with $x$ switching between the two points. The value of $p$ is constant, because in formula (3) the sinus component is constant with $x$ varying between two values: 0 and 0.5, both yielding a zero value for the sinus. Since $p$ is constant at 0.5, from formula (4), it is clear that x with initial condition 0.5, can only be 0 and 0.5 in an alternating order. Therefor, only 20 iterations are shown in figure \ref{pointval}.
\begin{figure}[H]
\centering
\includegraphics[width=.9\textwidth]{img/orbitpoint.png}
\caption{Orbit plot of two points}
\label{pointorbit}
\end{figure}
\begin{figure}[H]
\centering
\includegraphics[width=.9\textwidth]{img/valuepoint.png}
\caption{The values for $p$ and $x$ in the first 20 iterations}
\label{pointval}
\end{figure}

\subsubsection{Curves}
\begin{figure}[H]
\centering
\includegraphics[width=.9\textwidth]{img/orbitcurve.png}
\caption{Orbit plot of four curves}
\label{curveorbit}
\end{figure}
\begin{figure}[H]
\centering
\includegraphics[width=.9\textwidth]{img/valuecurve.png}
\caption{The values for $p$ and $x$ in the first 250 iterations }
\label{curveval}
\end{figure}

\newpage

Figure \ref{curveorbit} represent curves. These curves are closed and symmetrical.The values of $p$ are in two ranges: a higher range, yielding the top curve and a lower range, yielding the lower curve. The value of $x$ varies in one range. In figure \ref{curveval} it can be seen that $x$ varies between two peaks. The curves are visited in a fixed order as figure \ref{curveval} shows for the first 250 iterations. The top curve is visited first with a few iterations. After that, the bottom curve is visited with with a similar amount of iterations.

\subsubsection{Areas}
In figure \ref{areaorbit} two dotted areas can be seen. One on the top layer and one on the bottom. These areas seem to be symmetrical around the centre point of the image. These areas seem to be filled with chaotic values. In figure \ref{areaval} the values for $p$ seem to be on the lower half of the image, but after about 300 iterations, these values seem to spike to the upper half of the image. The values for $x$ seem to be chaotic and spread about all of the $[0,1]$ range.

\begin{figure}[H]
\centering
\includegraphics[width=.9\textwidth]{img/orbitarea.png}
\caption{Orbit plot of two areas}
\label{areaorbit}
\end{figure}

\newpage
\begin{figure}[H]
\centering
\includegraphics[width=.9\textwidth]{img/valuearea.png}
\caption{The values for $p$ and $x$ in the first 500 iterations}
\label{areaval}
\end{figure}

\subsection{b) Behaviour for different values of $K$}

The figures 7 to 22 are taken at different values for $K$ in an increasing order.

\subsubsection{Increasing value of $K$}

\begin{figure}[H]
\centering
\begin{minipage}{0.5\textwidth}
\centering
\includegraphics[width=\textwidth]{img/k0.png}
\caption{Orbits for $K = 0$}
\end{minipage}\hfill
\begin{minipage}{0.5\textwidth}
\centering
\includegraphics[width=\textwidth]{img/k01.png}
\caption{Orbits for $K = 0.1$}
\end{minipage}
\end{figure}

For value $K=0$ the orbits are plotted in figure 7. These are all horizontal lines. Since the sinus component form formula (3) is completely neglected, this value is constant. The value for $x$ however, could be anywhere in the range of [0,1]. This yields horizontal lines.

\begin{figure}[H]
\centering
\begin{minipage}{0.5\textwidth}
\centering
\includegraphics[width=\textwidth]{img/k025.png}
\caption{Orbits for $K = 0.25$}
\end{minipage}\hfill
\begin{minipage}{0.5\textwidth}
\centering
\includegraphics[width=\textwidth]{img/k05.png}
\caption{Orbits for $K = 0.5$}
\end{minipage}
\end{figure}

\begin{figure}[H]
\centering
\begin{minipage}{0.5\textwidth}
\centering
\includegraphics[width=\textwidth]{img/k075.png}
\caption{Orbits for $K = 0.75$}
\end{minipage}\hfill
\begin{minipage}{0.5\textwidth}
\centering
\includegraphics[width=\textwidth]{img/k095.png}
\caption{Orbits for $K = 0.95$}
\end{minipage}
\end{figure}

\begin{figure}[H]
\centering
\begin{minipage}{0.5\textwidth}
\centering
\includegraphics[width=\textwidth]{img/kc.png}
\caption{Orbits for $K_c = 0.97164$}
\end{minipage}\hfill
\begin{minipage}{0.5\textwidth}
\centering
\includegraphics[width=\textwidth]{img/k098.png}
\caption{Orbits for $K = 0.98$}
\end{minipage}
\end{figure}

\begin{figure}[H]
\centering
\begin{minipage}{0.5\textwidth}
\centering
\includegraphics[width=\textwidth]{img/k1.png}
\caption{Orbits for $K = 1.0$}
\end{minipage}\hfill
\begin{minipage}{0.5\textwidth}
\centering
\includegraphics[width=\textwidth]{img/k125.png}
\caption{Orbits for $K = 1.25$}
\end{minipage}
\end{figure}

\begin{figure}[H]
\centering
\begin{minipage}{0.5\textwidth}
\centering
\includegraphics[width=\textwidth]{img/k15.png}
\caption{Orbits for $K = 1.5$}
\end{minipage}\hfill
\begin{minipage}{0.5\textwidth}
\centering
\includegraphics[width=\textwidth]{img/k175.png}
\caption{Orbits for $K = 1.75$}
\end{minipage}
\end{figure}

\begin{figure}[H]
\centering
\begin{minipage}{0.5\textwidth}
\centering
\includegraphics[width=\textwidth]{img/k2.png}
\caption{Orbits for $K = 2.0$}
\end{minipage}\hfill
\begin{minipage}{0.5\textwidth}
\centering
\includegraphics[width=\textwidth]{img/k225.png}
\caption{Orbits for $K = 2.25$}
\end{minipage}
\end{figure}

\begin{figure}[H]
\centering
\begin{minipage}{0.5\textwidth}
\centering
\includegraphics[width=\textwidth]{img/k25.png}
\caption{Orbits for $K = 2.5$}
\end{minipage}\hfill
\begin{minipage}{0.5\textwidth}
\centering
\includegraphics[width=\textwidth]{img/k3.png}
\caption{Orbits for $K = 3.0$}
\end{minipage}
\end{figure}

In figures 8 to 12 ($K$ values in the range of [0.1,0.95]) it can be seen that the straight lines at $K = 0$ slowly curve into two curves at the horizontal centre of the image. In between are similar structures of curves visible.

Above  $K = 0.75$ areas of chaotic dots appear and take up most of the image after $K = 2$, destroying the curves in the centre of the image.
After values of 3.0 for $K$ most of the area of the image is taken up by the chaotic region.\\

Around $K = 1.25$ six smaller curves can be seen on the edges of the two large curves on the top and the bottom of the image. These become bigger and move outwards away from the centre of the large curve. When $K$ reaches a value of 2.0, these satellite curves are dissolved into the chaotic region of the image. This behaviour seems similar to the period doubling of the logistic map. For values of $K$ between 2.25 and 3.0, another instance of these smaller curves can be seen.

\subsubsection{KAM orbits}
KAM orbits are orbits spanning the entire range of [0,1]. These only exist below a certain threshold value for $K$ called $K_c$. The value 0.971635 as described by Greene\cite{kval} is used as the value for $K_c$. Figures 12 to 15 show plots of orbits for values of $K$ between 0.95 and 1.0. Although figures 14 and 15 show orbits spanning the entire range of $[0,1]$, when zooming in, it becomes clear these do not follow a closed curve and are more of a chain of curves.

\section{Conclusion and discussion}

\subsection{a) 'Orbits' in the $x$-$p$ plane for fixed $K$}
From the different initial conditions three types of 'orbits' can be determined: Points, curves and areas. Each of these types has a certain order in which points are visited. For point orbits, these are clearly defined. For curves both values will vary in between ranges. For areas, the $x$ value will vary over the complete interval of $[0,1]$ and $p$ will vary only over smaller ranges.

\subsection{b) Behaviour for different values of $K$}
For an increasing value of $K$ the amount of chaotic regions will increase. For values up to $K = 0.9$ the regions will be very small to non existent. After that the regions will grow. For values of $K$ in between $[1.0,1.75]$ and $[2.0,3.0]$ there will be a process similar to period doubling with smaller curves moving outward of the large centre curve. After $K = 3.0$ the image will only consist of the centre two curves and a chaotic region.\\
Exploring the limit of KAM orbits around the value of $K_c = 0.971635$ did not lead to observable results. Possibly to issues with the resolution of the plot and the amount of points per orbit plotted.

\subsection{Discussion}
The resolution on the images is limited. It is hard to prove from figures alone that a line could cross the entire horizontal scale of an image and not be distorted by a chaotic region. Depending on the resolution and the amount of points generated for each orbit, the image yield a better view of KAM orbits.

\begin{thebibliography}{1337}
\bibitem{kval}
Greene, John M.\\
\emph{A method for determining a stochastic transition}\\
Journal of Mathematical Physics, 20, 1183-1201 (1979)\\ DOI:http://dx.doi.org/10.1063/1.524170

\end{thebibliography}
\end{document}


\chapter{Collisions}\label{ch:collisions}
Many musical instruments rely on collisions in some shape or form. Examples are the collision between a hammer and a piano string, a guitar pick and the string, and even the collision between the lips of a trumpet player.

This work uses collision models that rely on penalising methods. The colliding objects -- although possibly perfectly rigid -- are supposed to interpenetrate, and collision is interpreted as a \textit{penalty}. The eventual force acting on the colliding objects is then dependent on the level of penetration. For deformable objects, such as the hammer felt tip of a piano, the penalty is dependent on the level of deformation. These collision models were first used in a musical context by e.g. \cite{Chatziioannou2013, Bilbao2014}.

The discretisations proposed in \cite{Chatziioannou2013, Bilbao2014} rely on implicit nonlinear schemes which require an iterative method, such as the Newton-Raphson method presented in Section \ref{sec:newtonRaphson}, to obtain their solution.
The exact number of iterations required per time step, especially in interactive applications, is usually unknown. This could be detrimental to real-time applications, as the number of iterations, and consequently the extra number of computations, could be very large in a particular situation. Furthermore, and perhaps more importantly, existence and uniqueness of the solution might not be available.

In \cite{Ducceschi2021} (co-authored by the PhD student [\hyperref[ch:listOfPublications]{O3}]), Ducceschi et al. propose a method based on quadratisation of the collision potential energy, that circumvents the need of an iterative method to solve nonlinear collisions. Energy quadratisation for explicit schemes first appeared in the context of Port-Hamiltonian systems and was due to Lopes et al. in \cite{Lopes2015}. The introduction of an additional state variable, which is what Ducceschi's work is based on, was introduced in \cite{Yang2017, Jiang2019}. Papers \citeP[D] and \citeP[E] follow an earlier iteration of the non-iterative collision algorithm from \cite{Ducceschi2019, Bilbao2019} which exhibited spurious oscillations that Ducceschi et al. resolve in \cite{Ducceschi2021}. Paper \citeP[H] uses the corrected collision model for the collision between the lips exciting the trombone. The corrected model will be used in this work and presented in this chapter.

This chapter first provides a definition for the collision potential as well as its quadratisation, used as the basis for the explicit method. Then, the method will be applied to a simple mass-barrier collision and finally, to a mass-spring -- string collision which can be used to model a finger-fretted string.

\subsubsection{Collision potential}
Collisions can be modelled using a nonlinear \textit{collision potential}, which can be defined as
\begin{equation}\label{eq:potential}
    \phi(\eta) = \frac{K_\ctxt}{\alpha_\ctxt+1}[\eta]_+^{\alpha_\ctxt+1},
\end{equation}
with collision stiffness $K_\ctxt \geq 0$ (in N/m$^{\alpha_\ctxt}$) and dimensionless nonlinear collision coefficient $\alpha_\ctxt \geq 1$. Here $\eta = \eta(t)$ describes the relative displacement between the two colliding bodies (in m). The $[\cdot ]_+$ operator, defined as
\begin{equation}\label{eq:etaPlus}
    [\cdot]_+ = \frac{\cdot + |\cdot|}{2},
\end{equation}
describes the `positive part of' and when applied to $\eta$ in Eq. \eqref{eq:potential} causes the potential $\phi$ to only be non-zero when the two colliding bodies are in contact.
% See Figure \ref{fig:eta}.
% %
% \begin{figure}[h]
% \centerline{\includegraphics[width=0.6\columnwidth]{figures/interactions/eta.eps}}
% \caption{\label{fig:eta}{A plot of $[\eta]_+$.}}
% \end{figure}

The derivative of Eq. \eqref{eq:potential} with respect to $\eta$ is defined as
\begin{equation}\label{eq:derivPotential}
    \phi'(\eta) = K_\ctxt[\eta]_+^{\alpha_\ctxt}
\end{equation}
and can then be used in the PDE at hand.

The issue with this form of the collision potential, is that an iterative method, such as Newton-Raphson presented in Section \ref{sec:newtonRaphson}, needs to be used in order to solve the system \cite{Ducceschi2021}.

\subsubsection{Quadratic form}
In \cite{Ducceschi2021}, the authors propose to rewrite the potential in Eq. \eqref{eq:derivPotential} in a quadratic form. Using the chain rule and  $\psi = \psi(\eta)$, Eq. \eqref{eq:derivPotential} can be rewritten as
\begin{equation}\label{eq:quadraticPotential}
    \phi'(\eta) = \psi\psi' \quad \text{where} \quad \psi = \sqrt{2\phi} \quad \text{and} \quad \psi' = \frac{\dot{\psi}}{\dot{\eta}}\ ,
\end{equation}
where a dot denotes a single derivative with respect to time.

This form of the potential can be discretised to a FD scheme that can be solved explicitly. This process will be shown below, using an example of the simple mass -- rigid barrier collision.

\section{The mass -- rigid barrier collision}\label{sec:massRigidBarrier}
As a test case, a mass colliding with a rigid barrier is presented here, which is arguably the simplest case of a collision. Consider a mass at location $u = u(t)$ (in m) colliding with a barrier at location $b$ (in m).

If the barrier is placed above the mass, the force it exerts on the mass will be negative and its system would be described as
\begin{equation}\label{eq:massBarrierPre}
    M\ddot u = -\psi\psi',
\end{equation}
with mass $M$ (in kg) and $\psi = \psi(\eta)$ and $\psi'$ are as defined in Eq. \eqref{eq:quadraticPotential} with $\eta = \eta(t) = u(t) - b$.

Looking towards the discretisation the mass-barrier collision, one could use the definitions in Eq. \eqref{eq:quadraticPotential} to rewrite Eq. \eqref{eq:massBarrierPre} to the following system of equations
\begin{subequations}\label{eq:massBarrier}
    \begin{align}
        M\ddot u &= -\psi g,\label{eq:massBarrierPDE1}\\
        \dot\psi &= g \dot\eta,\label{eq:massBarrierPDE2}\\
        \eta(t) &= u(t) - b,\label{eq:massBarrierPDE3}
    \end{align}
\end{subequations}
where $g = \psi'$.

\subsubsection{Relative location of objects}
When working with multiple interacting objcts, it is important to consider whether an object is located `above' or `below' the other,
%
% Notice that
% it is important to keep in mind the relative location of the colliding objects, i.e., whether one object is `above' or `below' an other.
i.e., which (generally) has a more positive or negative displacement than the other. A mass with a displacement of $0.01$ m will thus be `above' a barrier with a displacement of $-0.05$ m. Along these lines, a positive force acting on an element will accelerate it upwards and a negative force will accelerate it downwards.

The relative location of the two colliding objects will affect two things in system \eqref{eq:massBarrier}:

Firstly, the location of the object determines the direction of the collision force, i.e., the sign of the right-hand side in system \eqref{eq:massBarrier}. In this case, the barrier is placed above the mass, and will exert a downwards (negative) force on the mass during collision. If the barrier was placed below the mass, the opposite would have applied.

Secondly, the definition of $\eta$ in \eqref{eq:massBarrierPDE3} is affected by the relative location of the objects. The collision potential in Eq. \eqref{eq:potential} is only non-zero when $\eta$ is positive. If the barrier is placed above the mass, $u(t)-b$ will be positive on collision. It is thus important to remember that $\eta$ should be defined as the element above subtracted from the element below.

\subsection{Discrete time}
Before discretising system \eqref{eq:massBarrier} in full, the discrete approximation to the collision potential will be elaborated on.
Following \cite{Ducceschi2021}, $\psi$ is placed on an interleaved temporal grid (see Section \ref{sec:firstOrderDiscrete}) using
\begin{equation}\label{eq:psiHalfDef}
    \psi^{n-1/2} = \mu_{t-}\psi^n,
\end{equation}
where the interleaved temporal grid is used here as it results in energy conservation in discrete time (see Section \ref{sec:energyAnalysisMassBarrier}). Approximations to $\psi$ and $g$ in Eq. \eqref{eq:massBarrier} can then be made as
\begin{equation}
    \psi \approxeq \mtp \psi^{n-1/2}
\end{equation}
and
\begin{equation}\label{eq:approxPsi}
    g \approxeq g^n = \frac{\delta_{t+}\psi^{n-1/2}}{\delta_{t\cdot}\eta^n} ,
\end{equation}
respectively. Notice that applying a first-order difference operator to a grid function on an interleaved grid is second-order accurate.\footnote{$\ \dtp \psi^{n-1/2}\ \overset{\text{Eq. \eqref{eq:approxPsi}}}{=}\ \dtp \mtm \psi^n\ \overset{\text{Eq. \eqref{eq:identity2}}}{=}\  \dtd \psi^n$ which is second-order accurate (see Section \ref{sec:FDoperators}).}
The result of the approximation in Eq. \eqref{eq:approxPsi} allows $\psi$ to be treated as an independent time series:
\begin{equation}
    \dtp \psi^{n-1/2} = g^n \dtd \eta^n.
\end{equation}
With the above approximations in place, system \eqref{eq:massBarrier} can be discretised and yields the following system of equations:
\begin{subequations}\label{eq:massBarrierSystem}
    \begin{align}
        M\dtt \un &= -\left(\mtp\psi^{n-1/2}\right)g^n,\label{eq:massBarrierSystem1}\\
        \dtp \psi^{n-1/2} &= g^n \dtd \eta^n,\label{eq:massBarrierSystem2}\\
        \eta^n &= \un - b.\label{eq:massBarrierSystem3}
    \end{align}
\end{subequations}

\subsubsection{An explicit definition for $g^n$}
To be able to calculate $\psi^{n+1/2}$ and $u^{n+1}$ in system \eqref{eq:massBarrierSystem} explicitly, a definition for $g^n$ only based on known values must be found. As $g^n \approxeq \psi'$ as per Eq. \eqref{eq:approxPsi}, the derivative can be computed analytically according to
\begin{equation}
    g^n = \psi'\bigg\rvert_{\eta=\eta^n} \quad \overset{\text{Eq. \eqref{eq:quadraticPotential}}}{=}\quad  \frac{\phi'}{\sqrt{2\phi}}\bigg\rvert_{\eta=\eta^n}.
\end{equation}
Recalling \eqref{eq:derivPotential} and \eqref{eq:potential}, this can conveniently be rewritten to
\begin{equation}\label{eq:gn}
    g^n = \frac{K_\ctxt[\eta^n]_+^{\alpha_\ctxt}}{\sqrt{\frac{2K_\ctxt}{\alpha_\ctxt+1}[\eta^n]_+^{\alpha_\ctxt+1}}}=K_\ctxt\sqrt{\frac{\alpha_\ctxt+1}{2K_\ctxt}}[\eta^n]_+^{\alpha_\ctxt}[\eta^n]_+^{\frac{-(\alpha_\ctxt+1)}{2}}=\sqrt{\frac{K_\ctxt(\alpha_\ctxt+1)}{2}}[\eta^n]_+^{\frac{\alpha_\ctxt-1}{2}}\,.
\end{equation}
For implementation purposes, one can expand the $[\cdot]_+$ operator as the following (equivalent) condition:
\begin{subnumcases}{ \label{eq:gDefOld} g^n =}
    \sqrt{\frac{K_\text{c}(\alpha_\text{c}+1)}{2}}\cdot(\eta^n)^{\frac{\alpha_\text{c}-1}{2}},
    & if $\eta^n \geq 0$ \label{eq:collCorr1Old}\\
    0, & $\text{if } \eta^n < 0$\label{eq:collCorr2Old}
\end{subnumcases}
This implementation is the one presented in \cite{Ducceschi2019}, but exhibited spurious oscillations and `sticking' behaviour. This is due to the possibility of negative forces for positive penetrations due to the discontinuity in the definition for $g^n$ at $\eta^n = 0$.

In \cite{Ducceschi2021}, the definition for $g^n$ in \eqref{eq:gDefOld} is extended, starting out by using an implicit equation for $g^n$ by directly discretising Eq. \eqref{eq:approxPsi}
\begin{equation}\label{eq:gImp}
    g_\text{imp}^n = 2\frac{\psi^{n+1/2} - \psi^{n-1/2}}{\eta^{n+1} - \eta^{n-1}}\ .
\end{equation}
If there is, however, no collision at $n+1/2$, $\psi^{n+1/2} = 0$ and Eq. \eqref{eq:gImp} reduces to
\begin{equation*}
    g_\text{imp}^n = -2\frac{\psi^{n-1/2}}{\eta^{n+1} - \eta^{n-1}}\ .
\end{equation*}
Furthermore, due to the fact that there is no collision, $\eta^{n+1}$ can be calculated according to $\eta^{n+1} = \eta^\star = u^\star - b$, where $u^\star$ is the value of $u^{n+1}$ calculated using the scheme in Eq. \eqref{eq:massBarrierSystem1} without the collision force. Expanding Eq. \eqref{eq:massBarrierSystem1} without the collision force yields
\begin{equation*}
    \frac{M}{k^2}\left(u^\star - 2\un + u^{n-1}\right) = 0 \quad \Longrightarrow \quad u^\star = 2u^n - u^{n-1}.
\end{equation*}
Thus, if there is no collision, $g^n_\text{imp}$ can now be explicitly calculated from known values and be used in the definition for $g^n$ in Eq. \eqref{eq:gDefOld} according to \cite{Ducceschi2021}
\begin{subnumcases}{ \label{eq:gDef} g^n =}
    \kappa\sqrt{\frac{K_\text{c}(\alpha_\text{c}+1)}{2}}\cdot(\eta^n)^{\frac{\alpha_\text{c}-1}{2}},
    & if $\eta^n \geq 0,$ \label{eq:collCorr1}\\
    -2 \frac{\psi^{n-1/2}}{\eta^\star-\eta^{n-1}}, & if $\eta^n < 0\ \text{ and } \ \eta^{\star} \neq \eta^{n-1},$\label{eq:collCorr2}\\
    0, & $\text{if } \eta^n < 0\ \text{ and } \ \eta^{\star} = \eta^{n-1}.\qquad$\label{eq:collCorr3}
\end{subnumcases}
%
Here, $\kappa = 1$ if $\psi^{n-1/2} \geq 0$, otherwise $\kappa = -1$ and aims to resolve the `sticking' behaviour by forcing an outwardly-directed force at all times. As was done in paper \citeP[H], condition \eqref{eq:collCorr3} has been added to the definition of $g^n$ from \cite{Ducceschi2021} to prevent a division by 0 in Eq. \eqref{eq:collCorr2}.

This definition for $g^n$ does not exhibit the spurious oscillations that the old definition in Eq. \eqref{eq:gDefOld} did, and can still be explicitly calculated from known values of the system.

\subsection{Solving the system}\label{sec:solvingMassBarrier}
To implement the system in Eq. \eqref{eq:massBarrierSystem}, its definitions need to be slightly rewritten. Using identity \eqref{eq:identity3}, $\mtp \psi^{n-1/2}$ can be rewritten to
\begin{equation*}
    \mu_{t+}\psi^{n-1/2} = \frac{k}{2}\delta_{t+}\psi^{n-1/2} + \psi^{n-1/2}.
\end{equation*}
Then, substituting \eqref{eq:massBarrierSystem2} into this, yields
\begin{equation}\nonumber
    \mu_{t+}\psi^{n-1/2} = \frac{k}{2}g^n\delta_{t\cdot}\eta^n + \psi^{n-1/2},
\end{equation}
and inserting this into \eqref{eq:massBarrierSystem1}, yields
\begin{equation}\label{eq:substitutionFDS}
    M\delta_{tt}u^n = -\left(\frac{k}{2}g^n\delta_{t\cdot}\eta^n + \psi^{n-1/2}\right)g^n\ .
\end{equation}
As the position of barrier $b$ is static, the following is true:
\begin{equation}\label{eq:derEtaEqDerU}
    \frac{d\eta}{dt} = \frac{d}{dt}\Big(u - b\Big)\quad \Longrightarrow \quad \delta_{t\cdot}\eta^n = \delta_{t\cdot}u^n,
\end{equation}
i.e., the time derivative of $\eta$ equals the time derivative of $u$.\footnote{Note that if the barrier was placed underneath the mass, making \eqref{eq:massBarrierSystem3} $\eta^n = b-u^n$, this would result in $\delta_{t_\cdot}\eta^n = -\delta_{t\cdot}u^n$.} Eq. \eqref{eq:substitutionFDS} can now be solved explicitly as $u^{n+1}$ is the only unknown in the system
\begin{equation}
    \bigg(\frac{M}{k^2} + \frac{(g^n)^2}{4}\bigg)u^{n+1} = \frac{M}{k^2}(2u^n-u^{n-1})+\frac{(g^n)^2}{4}u^{n-1}-\psi^{n-1/2}g^n\ ,
\end{equation}
and can be solved by a simple division.

Finally, $u^{n+1}$ can be used to calculate $\eta^{n+1}$ by evaluating \eqref{eq:massBarrierSystem3} at $n+1$:
\begin{equation}\label{eq:etaNPlus1}
    \eta^{n+1} = u^{n+1}-b,
\end{equation}
which is used to calculate $\psi^{n+1/2}$ by expanding and rewriting \eqref{eq:massBarrierSystem2} to
\begin{equation}\label{eq:psiNPlusHalf}
    \psi^{n+1/2} = \psi^{n-1/2} + \frac{\eta^{n+1} - \eta^{n-1}}{2}\ .
\end{equation}

Figure \ref{fig:massBarrierCollision} shows the mass -- rigid barrier collision over time for two values of $K_\text{c}$. The mass is initialised with an initial (upwards) velocity using $u^0 = -1$ m and $u^1 = -0.95$ m. The figure shows that the penetration of the mass with the barrier causes a downwards force on the mass. As expected, this force is higher for a larger value of $K_\ctxt$ and causes the mass to accelerate downwards more quickly.

\def\figSpacing{0.01\textwidth}
\def\figWidth{0.49\textwidth}
\begin{figure}[h]
    \centering
    \subfloat[$K_\ctxt = 10^7$. \label{fig:massBarrierLow}]{\includegraphics[width=\figWidth]{figures/interactions/massBarrierLow.eps}}\hspace{\figSpacing}
    \subfloat[$K_\ctxt = 10^9$.\label{fig:massBarrierHigh}]{\includegraphics[width=\figWidth]{figures/interactions/massBarrierHigh.eps}}
    \caption{The mass -- rigid barrier collision over time with $\alpha_\text{c} = 1.3$ for different values of $K_\text{c}$. \label{fig:massBarrierCollision}}
\end{figure}

\subsection{Energy analysis}\label{sec:energyAnalysisMassBarrier}
To prove that the collision term does not add any additional energy into the system (retaining passivity) and that it does not add additional constraints on the stability of the system, the energy analysis techniques presented in Section \ref{sec:energyAnalysis} can be used. Notice that for brevity, the steps presented in Section \ref{sec:energyAnalysis} will not explicitly be followed.

Multiplying Eq. \eqref{eq:massBarrierSystem1} by $(\dtd \un)$ yields
\begin{equation*}
    \dtp \h_\mtxt  = -\left(\mtp\psi^{n-1/2}\right)g^n (\dtd \un)
\end{equation*}
where the energy of the mass is defined as (see Eq. \eqref{eq:energyBalanceMassSpring})
\begin{equation}
    \h_\mtxt = \frac{M}{2}(\dtm\un)^2.
\end{equation}
Expanding $g^n$ yields
\begin{align*}
    \dtp \h_\mtxt  &= -\left(\mtp\psi^{n-1/2}\right)\frac{\dtp \psi^{n-1/2}}{\dtd \eta^n}(\dtd \un)\\
    \xLeftrightarrow{\mystrut\ \text{Eq. \eqref{eq:derEtaEqDerU}}} \qquad & = -\left(\mtp\psi^{n-1/2}\right)\dtp \psi^{n-1/2},
\end{align*}
which, using identity \eqref{eq:prodIdentity3}, can be rewritten to
\begin{equation}
    \dtp(\h_\mtxt + \h_\ctxt) = 0,
\end{equation}
with collision energy
\begin{equation}\label{eq:collisionEnergy}
    \h_\ctxt = \frac{(\psi^{n-1/2})^2}{2}.
\end{equation}
Recall that in order for a scheme to be passive, its energy must be non-negative, and the fact that $\psi$ is squared proves passivity for system \eqref{eq:massBarrierSystem}.

Figure \ref{fig:massBarrierEnergy} shows the energetic output of the mass -- rigid barrier collision corresponding to Figure \ref{fig:massBarrierLow}. The left panel shows that the kinetic energy of the mass is transferred into the energy of the collision, after which it is converted into kinetic energy of the mass again.

\begin{figure}[h]
    \centering
    \begin{tikzpicture}[->,node distance=3cm,
        thick,main node/.style={circle,draw}]

        \node[] (image) at (0,0) {
        \includegraphics[width=\textwidth]{figures/interactions/massBarrierEnergy.eps}
        };

        \node[] (he) at (0.2,0.5) {\small $\mathfrak{h}_\text{e}$};

        \node[] (h) at (-5.8, 1) {\small $\mathfrak{h}$};
        \node[] (t) at (-5.8, 0.5) {\small $\color{blue}\mathfrak{h}_\text{m}$};
        \node[] (v) at (-5.8, 0) {\small $\color{red}\mathfrak{h}_\ctxt$};

      \end{tikzpicture}
      \caption{The energy of the mass (blue), the collision (green) and the total energy (black) of the mass -- rigid barrier collision. The energy corresponds to the behaviour in Figure \ref{fig:massBarrierLow}. The right panel shows the normalised energy (according to Eq. \eqref{eq:normalisedEnergy}) shows that the deviation of the energy is within machine precision. \label{fig:massBarrierEnergy}}
\end{figure}

\section{Mass-spring -- string collision}\label{sec:massString}
The mass-spring -- string collision is slightly trickier than the mass -- rigid barrier collision, as there are two moving components rather than one. This system is chosen as an example as it has the interesting use-case of fretting a string to change the pitch, modelling the fretting finger as a mass.

Consider a lossless stiff string of length $L$, its transverse displacement described by $u = u(x,t)$ (in m) and defined for $t\geq 0$ and $x\in \D$ with domain $\D = [0, L]$. The mass with displacement $w = w(t)$ (in m) and $t\geq 0$ will model the fretting finger. The PDE for the stiff string and its parameter definitions can be found in Eq. \eqref{eq:stiffStringPDENoLosses} and for the mass-spring system in Eq. \eqref{eq:massSpringPDE}. Placing the string above the mass, the following system emerges:
\begin{subequations}\label{eq:massStringPDE}
\begin{align}
    \rho A \ptt u & =T\pxx u - EI\pxxxx u + \delta(x-x_\text{m}) \psi g \label{eq:massStringPDE1}\\
    M \ddot w &=-Kw - \psi g\label{eq:massStringPDE2}\\
    \dot\psi &= g \dot\eta,\label{eq:massStringPDE3}\\
    \eta(t) &= w(t) - u(x_\text{m}, t),\label{eq:massStringPDE4}
\end{align}
\end{subequations}
where spatial Dirac delta function $\delta(x-x_\text{m})$ localises the mass (finger) along the string at location $x_\text{m} \in \D$ (see Eq. \eqref{eq:spatialDirac}). Furthermore, $\psi = \psi(\eta)$ and $g=\psi'$ are as defined in Eq. \eqref{eq:quadraticPotential}.

Discretising system \eqref{eq:massStringPDE}, with the collision discretised according to the process explained in Section \ref{sec:massRigidBarrier}, yields
\begin{subequations}
    \begin{align}
        \rho A \delta_{tt}u^n_l & =T\delta_{xx}u^n_l - EI\delta_{xxxx}u_l^n + J_l(x_\text{m}) \big(\mu_{t+}\psi^{n-1/2}\big)g^n, \label{eq:massString1}\\
        M \delta_{tt}w^n &=-Kw^n - \big(\mu_{t+}\psi^{n-1/2}\big)g^n,\label{eq:massString2}\\
        \delta_{t+}\psi^{n-1/2} &= g^n\delta_{t\cdot}\eta^n,\label{eq:massString3}\\
        \eta^n &= w^n - I_l(x_\text{m})\uln,\label{eq:massString4}
    \end{align}
\end{subequations}
where $l\in d$ with discrete domain $d=\{0, \hdots, N\}$ and number of grid points along the string $N+1$. Furthermore, spreading and interpolation operators $I_l(x_\text{m}) = I_{l, o}(x_\text{m})$ and $J_l(x_\text{m}) = J_{l, o}(x_\text{m})$ are as defined in Section \ref{sec:interpolationSpreading}. The order $o$ is left unspecified. Following the same process as in Section \ref{sec:solvingMassBarrier}, Eqs. \eqref{eq:massString1} and \eqref{eq:massString2} can be rewritten to
\begin{subequations}\label{eq:massStringComb}
    \begin{align}
        \rho A \delta_{tt}u^n_l & =T\delta_{xx}u^n_l - EI\delta_{xxxx}u_l^n + J_l(x_\text{m}) \left(\frac{k}{2}g^n\delta_{t\cdot}\eta^n + \psi^{n-1/2}\right)g^n,\label{eq:massStringComb1}\\
        M \delta_{tt}w^n &=-Kw^n - \left(\frac{k}{2}g^n\delta_{t\cdot}\eta^n + \psi^{n-1/2}\right)g^n,\label{eq:massStringComb2}
    \end{align}
\end{subequations}
which can be used as a starting point for solving the system.

\subsection{Solving the system}
As the colliding objects are both moving, Eq. \eqref{eq:derEtaEqDerU} is not valid anymore and another strategy needs to be used.
To start, one must isolate the string at the collision location $x_\text{m}$ by taking an inner product of Eq. \eqref{eq:massStringComb1} with $J_l(x_\text{m})$ over discrete domain $d$. Using identity \eqref{eq:identityIJ} and dividing all terms by $\rho A$ yields
\begin{align*}
    \delta_{tt}I_l(x_\text{m})u^n_l =c^2I_l(x_\text{m})\delta_{xx}u^n_l &- \kappa^2I_l(x_\text{m})\delta_{xxxx}u_l^n \\
    &\quad + \frac{\lVert J_l(x_\text{m})\rVert^2_d}{\rho A} \left(\frac{k}{2}g^n\delta_{t\cdot}\eta^n + \psi^{n-1/2}\right)g^n.
\end{align*}
with $c = \sqrt{T/\rho A}$ and $\kappa = \sqrt{EI/\rho A}$. Expanding the temporal FD operators yields
\begin{equation}\label{eq:stringMassunp1}
    I_l(x_\text{m})u_l^{n+1} = u^\star+ \underbrace{\frac{\lVert J_l(x_\text{m})\rVert^2_d k^2}{\rho A}}_{\mathfrak{J}_l} \left(\frac{(g^n)^2}{4}\left(\eta^{n+1}-\eta^{n-1}\right) + \psi^{n-1/2}g^n\right),
\end{equation}
where
\begin{equation*}
    u^\star = I_l(x_\text{m})(2u_l^n -u_l^{n-1}) + c^2k^2I_l(x_\text{m})\delta_{xx}\uln- \kappa^2k^2I_l(x_\text{m})\delta_{xxxx}\uln
\end{equation*}
is the result of the update equation of the string at $x_\text{m}$ without the collision term. Then, Eq. \eqref{eq:massString4} evaluated at $n+1$, which is $\eta^{n+1} = w^{n+1} - I(x_\text{m})u_l^{n+1}$, can be substituted into Eq. \eqref{eq:stringMassunp1}, which results in
\begin{equation}\label{eq:expandedMassString1}
    \begin{aligned}
    \left(1 + \mathfrak{J}_l\frac{(g^n)^2}{4}\right)I_l(x_\text{m})u_l^{n+1} - \mathfrak{J}_l&\frac{(g^n)^2}{4} w^{n+1}= u^\star\\
    & + \mathfrak{J}_l \left(-\frac{(g^n)^2}{4}\eta^{n-1} + \psi^{n-1/2}g^n\right).
    \end{aligned}
\end{equation}
Performing this same process on the FD scheme of the mass in Eq. \eqref{eq:massStringComb2} yields
\begin{equation}\label{eq:expandedMassString2}
    \begin{aligned}
    -\frac{(g^n)^2 k^2}{4M} I_l(x_\text{m})u_l^{n+1} + &\left(1 + \frac{(g^n)^2 k^2}{4M}\right)w^{n+1} = w^\star\\
    & - \frac{k^2}{M} \left(-\frac{(g^n)^2}{4}\eta^{n-1} + \psi^{n-1/2}g^n\right),
    \end{aligned}
\end{equation}
where
\begin{equation*}
    w^\star = 2w^n - w^{n-1} - \frac{Kk^2}{M}w^n
\end{equation*}
is (again) the result of the update equation of the mass without the collision term.
Equations \eqref{eq:expandedMassString1} and \eqref{eq:expandedMassString2} can be treated as a system of linear equations (see Section \ref{sec:linearEquations}) with unknowns $I_l(x_\text{m})u^{n+1}$ and $w^{n+1}$. Writing the aforementioned equations in matrix form yields
\begin{align}
    \begin{bmatrix}
            I_l(x_\text{m})u^{n+1}_l\\
            w^{n+1}
        \end{bmatrix}
        =
        \mathbf{A}^{-1}\mathbf{v}
    \end{align}
    where
    \begin{equation}
    \begin{gathered}
    \mathbf{A} =
        \begin{bmatrix}
            \left(1 + \mathfrak{J}_l\frac{(g^n)^2}{4}\right) & -\mathfrak{J}_l\frac{(g^n)^2}{4}\\
            -\frac{(g^n)^2 k^2}{4M} &\left(1 + \frac{(g^n)^2 k^2}{4M}\right)
        \end{bmatrix}
        \quad \text{and}\\
        \mathbf{v} =
        \begin{bmatrix}
            u^\star + \mathfrak{J}_l \left(-\frac{(g^n)^2}{4}\eta^{n-1} + \psi^{n-1/2}g^n\right)\\
            w^\star- \frac{k^2}{M} \left(-\frac{(g^n)^2}{4}\eta^{n-1} + \psi^{n-1/2}g^n\right)
        \end{bmatrix}.
        \nonumber
    \end{gathered}
\end{equation}
From this, $\eta^{n+1}$ can be calculated, and can consequently be applied to the string and mass in system \eqref{eq:massStringComb}.

Figure \ref{fig:massStringCollision} shows an implementation of the mass spring collision. The mass is initialised with an upwards initial velocity, where $w^0 = -0.2, w^1 = -0.1$, and collides with the string almost instantly after the start of the simulation. As the first panel shows, the collision model allows for interpenetration of the objects. Immediately after, the collision force accelerates the string upwards and the mass downwards.

\begin{figure}[h]
    \centering
    \includegraphics[width=\textwidth]{figures/interactions/stringMassCollision.eps}
    \caption{The collision of the mass (blue) and the string (red). The collision model allows for interpenetration of the objects as shown in the left panel. \label{fig:massStringCollision}}
\end{figure}

\subsection{Energy analysis}
This section follows Section \ref{sec:energyAnalysis} without explicitly following the steps for brevity.

One can obtain the energy of the stiff string FD scheme in Eq. \eqref{eq:massString1} by taking the inner product of scheme by $(\dtd \uln)$ over discrete domain $d$ to obtain
\begin{equation}\label{eq:rOCEnergyString}
    \dtp \h_\text{s} = \left\langle (\dtd \uln), J_l(x_\text{m})\left(\mtp \psi^{n-1/2}\right)g^n\right\rangle_d
\end{equation}
where the energy of the string is (see Eq. \eqref{eq:energyBalanceStiffString})
\begin{equation*}
    \begin{gathered}
        \h_\text{s} = \t_\text{s} + \v_\text{s}, \qwiq \t_\text{s} = \frac{\rho A}{2}\lVert\dtm \uln\rVert^2_d,\quad \text{and} \\
        \v_\text{s} = \frac{T}{2}\langle\dxp\uln, e_{t-}\dxp\uln\rangle_{\underline{d}} + \frac{EI}{2}\langle\dxx\uln, e_{t-}\dxx\uln\rangle_{\overline{\underline{d}}}\ .
    \end{gathered}
\end{equation*}
Energy analysis for the mass in Eq. \eqref{eq:massString2} can be done by multiplying the scheme by $(\dtd w^n)$ to get
\begin{equation}\label{eq:rOCEnergyMass}
    \dtp \h_\text{m} = -(\dtd w^n)\left(\mtp \psi^{n-1/2}\right)g^n,
\end{equation}
where (see Eq. \eqref{eq:energyBalanceMassSpring})
\begin{equation*}
    \h_\text{m} = \t_\text{m} + \v_\text{m}, \qwiq \t_\text{m} = \frac{M}{2}(\dtm w^n)^2, \qaq \v_\text{m} = \frac{K}{2}w^n e_{t-}w^n.
\end{equation*}
The total energy in the system is the addition of Eqs. \eqref{eq:rOCEnergyString} and \eqref{eq:rOCEnergyMass}, which, using identity \eqref{eq:identityIJ} for the former, can be written as:
\begin{align*}
    \dtp(\h_\text{s} + \h_\text{m}) &= \Big(I_l(x_\text{m})(\dtd \uln) - (\dtd w^n)\Big)\left(\mtp \psi^{n-1/2}\right)g^n,\\
    &= \underbrace{\dtd\left(I_l(x_\text{m})\uln - w^n\right)}_{-\dtd \eta^n}\left(\mtp \psi^{n-1/2}\right)g^n.
\end{align*}
Then, expanding $g^n$ according to Eq. \eqref{eq:approxPsi} yields
\begin{align*}
    \dtp(\h_\text{s} + \h_\text{m}) &= - \dtd\eta^n\left(\mtp \psi^{n-1/2}\right)\frac{\delta_{t+}\psi^{n-1/2}}{\delta_{t\cdot}\eta^n}\\
    &= -\left(\mtp \psi^{n-1/2}\right)\delta_{t+}\psi^{n-1/2}
\end{align*}
which, using identity \eqref{eq:prodIdentity3}, can be rewritten as
\begin{equation}
    \dtp(\h_\text{s} + \h_\text{m} + \h_\text{c}) = 0,
\end{equation}
with collision energy
\begin{equation*}
    \h_\text{c} = \frac{(\psi^{n-1/2})^2}{2}.
\end{equation*}
Again, the fact that $\psi$ is squared here, means that $\h_\text{c}$ is non-negative, and proves passivity of the system.

Figure \ref{fig:massStringCollisionEnergy} shows the energy of the mass-spring collision corresponding to the behaviour shown in Figure \ref{fig:massStringCollision}. One can observe that energy of the mass is transferred to the string almost immediately after the start of the simulation. Furthermore, the figure shows that the mass and the string collide again at $n\approx 110$. The interpenetration of the two colliding objects can be observed from the small peaks in the value for $\h_\ctxt$ at these times.

\begin{figure}[h]
    \centering
    \begin{tikzpicture}[->,node distance=3cm,
        thick,main node/.style={circle,draw}]

        \node[] (image) at (0,0) {
        \includegraphics[width=\textwidth]{figures/interactions/stringMassCollisionEnergy.eps}
        };

        \node[] (he) at (0.2,0.5) {\small $\mathfrak{h}_\text{e}$};

        \node[] (h) at (-5.8, 1) {\small $\mathfrak{h}$};
        \node[] (t) at (-5.8, 0.5) {\small $\color{blue}\mathfrak{h}_\text{m}$};
        \node[] (v) at (-5.8, 0) {\small $\color{red}\mathfrak{h}_\stxt$};
        \node[] (c) at (-5.8, -0.5) {\small $\color[HTML]{00DB00}\mathfrak{h}_\ctxt$};

      \end{tikzpicture}
      \caption{The energy of the mass (blue), the string (red), the collision (green) and the total energy (black) of the mass-string collision. The energy corresponds to the system in Figure \ref{fig:massStringCollision}. The right panel shows the normalised energy (according to Eq. \eqref{eq:normalisedEnergy}) shows that the deviation of the energy is within machine precision. \label{fig:massStringCollisionEnergy}}
\end{figure}
\section{Two-sided collision: A connection}\label{sec:twoSidedCollision}

% As an alternative to the method connections shown in Chapter \ref{ch:connections} showed ways to connect various resonators using techniques presented in \cite{theBible}. An alternative method to establish these connections can be devised using the methods presented in this chapter.
%

Using the methods presented in this chapter, one could devise a two-sided collision and alter the collision potential in Eq. \eqref{eq:potential} to \cite{Bilbao2019}
\begin{equation}\label{eq:twoSidedPotential}
    \phi(\eta) = \frac{K}{\alpha_\ctxt+1}|\eta|^{\alpha_\ctxt+1},
\end{equation}
and taking its derivative with respect to $\eta$ yields
\begin{equation}
    \phi'(\eta) = \sgn(\eta)K|\eta|^{\alpha_\ctxt}.
\end{equation}
One can observe that, as opposed to the (one-sided) potential presented in Eq. \eqref{eq:potential}, the collision force will be non-zero, for both a positive and negative $\eta$. This two-sided collision can be used as a connection -- as an alternative to connections presented in Chapter \ref{ch:connections} -- and has been used in papers \citeP[D] and \citeP[E] in combination with Eq. \eqref{eq:potential} to model the mechanics of the tromba marina. See Chapter \ref{ch:tromba} for more details.


\chapter{Derivations}\label{app:derivations}
\def\Psilp{\Psi_{l+1}^n}
\def\Psilm{\Psi_{l-1}^n}
\def\Psinp{\Psi_l^{n+1}}
\def\Psinm{\Psi_l^{n-1}}

\section{Webster's update equation (Eq. \eqref{eq:webstersUpdateEq})}\label{app:webstersUpdateEq}
This section derives the update equation for Webster's equation in Eq. \eqref{eq:webstersUpdateEq}:
\begin{align*}
    &\begin{aligned}
    \frac{\Sbar}{k^2}(\Psi_l^{n+1} - 2\Psiln+\Psi_l^{n-1}) &= c^2\bigg((\dxm \Sp)(\mxm \dxp \Psiln)\\
    & + (\mxm \Sp)(\dxm \dxp \Psiln)\bigg),\\
    \Psinp - 2\Psiln+\Psinm &= \frac{c^2k^2}{\Sbar}\bigg(\frac{1}{h}(\Sp - \Sm)\frac{1}{2h}\overbrace{(\Psilp -\Psilm)}^{\mxp\dxm\Psiln = \delta_{x\cdot}\Psiln}\nonumber\\
    &+\frac{1}{2}(\Sp + \Sm)\frac{1}{h^2}(\Psilp-2\Psiln+\Psilm)\bigg),
    \end{aligned}\\[-0.5em]
    &\begin{aligned}
        \Psinp &= 2\Psiln -\Psinm +\overbrace{\frac{\lambda^2}{2\Sbar}}^{\lambda = \frac{ck}{h}}\Big(\Sp\Psilp - \Sp\Psilm\\
        &\qquad\qquad - \Sm\Psilp + \Sm\Psilm+ \Sp\Psilp + \Sp\Psilm \\
        &\qquad\qquad + \Sm\Psilp + \Sm\Psilm - 2 (\Sp + \Sm)\Psiln\Big)\nonumber,\\
        \Psinp &= 2\Psiln-\Psinm+ \frac{\lambda^2}{2\Sbar}\Big(2\Sp\Psilp + 2\Sm\Psilm - 4\Sbar\Psiln\Big)\nonumber,\\
        \Psinp &= 2\Psiln-\Psinm+ \frac{\lambda^2\Sp}{\Sbar}\Psilp + \frac{\lambda^2\Sm}{\Sbar}\Psilm - 2\lambda^2\Psiln\nonumber,\\
        \Psinp &= 2(1-\lambda^2)\Psiln-\Psinm+ \frac{\lambda^2\Sp}{\Sbar}\Psilp + \frac{\lambda^2\Sm}{\Sbar}\Psilm.
    \end{aligned}
\end{align*}

\section{Boundary terms Webster's equation (Eq. \eqref{eq:centStrictDissip})}\label{app:boundaryWebster}
This section derives process of obtaining the values for $\el$ and $\er$ such that the boundary terms in Webster's equation are strictly dissipative.

Starting with the second term in the energy balance in Eq. \eqref{eq:powerBalanceWebster}:
\begin{equation}
    - c^2\langle\dtd\Psiln, \dxm\big(\Sp(\dxp\Psiln)\big)\rangle_d^{\el,\er}.
\end{equation}
Using identity \eqref{eq:weightedIdentityMinus}
\begin{equation*}
    \begin{aligned}
    \langle f_l^n, \dxm g_l^n \rangle_d^{\el,\er}  = -&\langle \dxp f_l^n, g_l^n \rangle_{\underline{d}}+ f_N^ng_{N-1}^n - f_0^ng_0^n \\
    &+ \frac{\epsilon_\text{r}}{2}f_N^n(g_N^n-g_{N-1}^n)+ \frac{\epsilon_\text{l}}{2}f_0^n(g_0^n - g_{-1}^n),
    \end{aligned}
\end{equation*}
this can be rewritten to (with $f=\dtd\Psi$ and $g = S_{l+1/2}(\dxp\Psi)$)
\begin{equation*}
        -c^2\langle \dtd\Psiln,\dxm \big(\Sp(\dxp\Psiln)\big) \rangle_{d}^{\epsilon_\text{l},\epsilon_\text{r}} = c^2\langle \dtd\dxp \Psiln, \big(\Sp(\dxp\Psiln)\big)\rangle_{\underline{d}} - \b.
\end{equation*}
where
\begin{equation}
    \b = \b_\text{r} - \b_\text{l}
\end{equation}
with
\begin{equation*}
    \mathfrak{b}_\text{r} =  \begin{aligned}[t]
        &c^2 (\dtd\Psi_N^n)\Big(S_{N-1/2}\overbrace{(\dxp\Psi_{N-1}^n)}^{(\dxm\Psi_{N}^n)}\Big)\\
        &+ \frac{\epsilon_\text{r}}{2}(\dtd\Psi_{N}^n)\Big(S_{N+1/2}(\dxp\Psi_N^n)- S_{N-1/2}\underbrace{(\dxp \Psi_{N-1}^n)}_{(\dxm\Psi_{N}^n)}\Big)
    \end{aligned}
\end{equation*}
and
\begin{equation*}
    \mathfrak{b}_\text{l} = \begin{aligned}[t]
        &c^2(\dtd\Psi_0^n)\Big(S_{1/2}(\dxp\Psi_0^n)\Big)\\
        & -\frac{\epsilon_\text{l}}{2}(\dtd\Psi_0^n)\Big(S_{1/2}(\dxp\Psi_0^n)-S_{-1/2}\underbrace{(\dxp\Psi_{-1}^n)}_{(\dxm\Psi_0^n)}\Big)
    \end{aligned}
\end{equation*}
which can be rewritten to
\begin{align}
    \b_\text{r} &= c^2(\dtd\Psi_N^n)\left(\frac{\epsilon_\text{r}}{2}S_{N+1/2}(\dxp \Psi_N^n) + \left(1-\frac{\epsilon_\text{r}}{2}\right)S_{N-1/2}(\dxm\Psi_N^n)\right),\\
    \b_\text{l} &= c^2(\dtd\Psi_0^n)\left(\frac{\epsilon_\text{l}}{2}S_{-1/2}(\dxm\Psi_0^n)+\left(1-\frac{\epsilon_\text{l}}{2}\right)S_{1/2}(\dxp \Psi_0^n))\right).
\end{align}
% \\
% &= \\
% &\qquad - c^2(\dtd\Psi_0^n)\left(\frac{\epsilon_\text{l}}{2}S_{-1/2}(\dxm\Psi_0^n)+\left(1-\frac{\epsilon_\text{l}}{2}\right)S_{1/2}(\dxp \Psi_0^n))\right)

Then, for the centred radiating boundary condition in Eq. \eqref{eq:centRadBound} to be strictly dissipative, i.e., $\dxd \Psiln = 0 \ \Rightarrow \ \b_\text{r} = 0$ the special choice for $\epsilon_\text{r} = S_{N-1/2} / \mu_{xx}S_N$ needs to be made:
\begin{equation}
    \begin{aligned}
        \mathfrak{b}_\text{r} &= c^2 (\dtd\Psi_N^n)\!\left(\frac{S_{N-1/2}}{2\mu_{xx}S_N}S_{N+1/2}(\dxp\Psi_N^n)+\left(1\!-\!\frac{S_{N-1/2}}{2\mu_{xx}S_N}\right)\!S_{N-1/2}(\dxm\Psi_N^n)\!\right),\\
        &= c^2 (\dtd\Psi_N^n)S_{N-1/2}\left(\frac{S_{N+1/2}}{2\mu_{xx}S_N}(\dxp\Psi_N^n)+\left(1-\frac{S_{N-1/2}}{2\mu_{xx}S_N}\right)(\dxm\Psi_N^n)\right),\\
        &=c^2 (\dtd\Psi_N^n)S_{N-1/2}\left(1-\frac{S_{N-1/2}}{2\mu_{xx}S_N}\right)\left(\frac{\frac{S_{N+1/2}(\dxp\Psi_N^n)}{2\mu_{xx}S_N}}{\left(1-\frac{S_{N-1/2}}{2\mu_{xx}S_N}\right)}+\dxm\Psi_N^n\right),\\
        &=c^2 (\dtd\Psi_N^n)S_{N-1/2}\left(1-\frac{\epsilon_\text{r}}{2}\right)\left(\frac{\frac{S_{N+1/2}(\dxp\Psi_N^n)}{2\mu_{xx}S_N}}{\left(\frac{2\mu_{xx}S_N - S_{N-1/2}}{2\mu_{xx}S_N}\right)}+\dxm\Psi_N^n\right),\\
        &=c^2 (\dtd\Psi_N^n)S_{N-1/2}\left(1-\frac{\epsilon_\text{r}}{2}\right)\left(\frac{S_{N+1/2}(\dxp\Psi_N^n)}{2\mu_{xx}S_N - S_{N-1/2}}+\dxm\Psi_N^n\right),\\
        &=c^2 (\dtd\Psi_N^n)S_{N-1/2}\left(1-\frac{\epsilon_\text{r}}{2}\right)\left(\frac{S_{N+1/2}(\dxp\Psi_N^n)}{S_{N+1/2} + S_{N-1/2} - S_{N-1/2}}+\dxm\Psi_N^n\right),\\
        &=c^2 (\dtd\Psi_N^n)S_{N-1/2}\left(1-\frac{\epsilon_\text{r}}{2}\right)\left(\dxp\Psi_N^n+\dxm\Psi_N^n\right),\\
        &=c^2 (\dtd\Psi_N^n)S_{N-1/2}\left(1-\frac{\epsilon_\text{r}}{2}\right)\left(\frac{1}{h}\left(\Psi_{N+1}^n - \Psi_N^n + \Psi_N^n - \Psi_{N-1}^n\right)\right),\\
&= c^2 (\dtd\Psi_N^n)S_{N-1/2}\left(1-\frac{\epsilon_\text{r}}{2}\right)2(\delta_{x\cdot}\Psi_N^n),\\
&= c^2 (\dtd\Psi_N^n)S_{N-1/2}\left(2-\epsilon_\text{r}\right)(\delta_{x\cdot}\Psi_N^n).
    \end{aligned}
\end{equation}
The same can be done for $\mathfrak{b}_\text{l}$ with $\epsilon_\text{l} = S_{1/2}/\mu_{xx}S_0$ to get
\begin{equation}
    \mathfrak{b}_\text{l} = c^2(\dtd\Psi_0^n)S_{1/2}\left(2-\epsilon_\text{l}\right)(\delta_{x\cdot}\Psi_0^n).
\end{equation}

\section{Derivatives for Newton-Raphson for the elasto-plastic friction model (Eq. \eqref{eq:NRit})}\label{app:elastoDeriv}
Recalling the functions needed for to compute the Newton-Raphson iteration for the elasto-plastic bow model in Section \ref{sec:elastoPlastic}, being Eq. \eqref{eq:elastog1}
\begin{equation*}
    g_1(v^n,z^n) = \left(\frac{2}{k} + 2\sz\right)v^n + \lVert J_l(x_\Btxt^n)\rVert_d^2\frac{f(v^n,z^n)}{\rho A}+ b^n= 0,
\end{equation*}
and Eq. \eqref{eq:elastog2}:
\begin{equation*}
    g_2(v^n, z^n) = r^n - a^n = 0,
\end{equation*}
the derivatives needed to solve the Newton-Raphson iteration in Eq. \eqref{eq:NRit}
\begin{equation*}
    \begin{bmatrix}
        v^n\\
        z^n
        \end{bmatrix}_{i+1}
        =
        \begin{bmatrix}
        v^n\\
        z^n
        \end{bmatrix}_i
        -
        \begin{bmatrix}
        \frac{\partial g_1}{\partial v} & \frac{\partial g_1}{\partial z}\\
        \frac{\partial g_2}{\partial v} & \frac{\partial g_2}{\partial z}\\
        \end{bmatrix}^{-1}
        \begin{bmatrix}
        g_1\\
        g_2
        \end{bmatrix}\,
        .
\end{equation*}
can be shown to be
\begin{align*}
    \frac{\partial g_1}{\partial v} &= \frac{2}{k} + 2\sigma_0 + \frac{s_1\lVert J_l(x_\Btxt^n)\rVert_d^2}{\rho A}\frac{\partial r}{\partial v}+\frac{s_2\lVert J_l(x_\Btxt^n)\rVert_d^2}{\rho A},\\
    \frac{\partial g_1}{\partial z} &= \frac{s_0\lVert J_l(x_\Btxt^n)\rVert_d^2}{\rho A} + \frac{s_1\lVert J_l(x_\Btxt^n)\rVert_d^2}{\rho A}\frac{\partial r}{\partial z},\\
    \frac{\partial g_2}{\partial v} &= \frac{\partial r}{\partial v}
    \\\frac{\partial g_2}{\partial z}&= \frac{\partial r}{\partial z} -\frac{2}{k}.
\end{align*}
Recalling from Eq. \eqref{eq:r} that
\begin{equation*}
    r^n = r(v^n,z^n) = v^n\bigg[1-\alpha(v^n,z^n)\frac{z^n}{z_\text{ss}(v^n)}\bigg]
\end{equation*}
its derivatives can be computed as
\begin{align*}
    \frac{\partial r}{\partial v} &= 1-z^n\Bigg(\frac{(\alpha^n+\frac{\partial \alpha^n}{\partial v}v^n)z_\text{ss}^n - \frac{\partial z_\text{ss}^n}{\partial v}\alpha^n v^n}{(z_\text{ss}^n)^2}\Bigg),\\
    \frac{\partial r}{\partial z} &= -\frac{v^n}{z_\text{ss}^n}\bigg(\frac{\partial \alpha^n}{\partial z}z^n + \alpha^n\bigg),
    \end{align*}
with
\begin{align*}
    \frac{\partial\alpha^n}{\partial v} &=\sgn(z_\text{ss}^n)\frac{\partial z_\text{ss}^n}{\partial v}\frac{z_\text{ba} - |z^n|}{(|z_\text{ss}^n| - z_\text{ba})^2}\frac{\pi}{2}\cos\big(\sgn(z^n)\Phi\big),\\
    \frac{\partial\alpha^n}{\partial z}&=\frac{\sgn(z^n)\pi\cos\big(\sgn(z^n)\Phi\big)}{2(|z_\text{ss}^n|-z_\text{ba})},\\
    \frac{\partial z_\text{ss}^n}{\partial v} &= -\frac{2|v^n|}{v_\text{S}^2 s_0}(f_\text{S}-f_\text{C})e^{-(v^n/v_\text{S})^2}.
\end{align*}


\section{Important Shapes in 2D \& 3D}
\noindent
In 2D space $(\R^2)$, the most common and simple shapes you will encounter are lines and circles.
\begin{itemize}
	\item Lines have a form like $x=1$ or $y=2x+1$.
	\item Circles have a form like $\left(x-1\right)^2 + \left(y-2\right)^2 = 9$.
\end{itemize}

\noindent
In 3D space $(R^3)$, these 2D shapes have higher dimensional versions: planes, cylinders, and spheres.
\begin{itemize}
	\item{Planes have forms like $x = 1$, $y = 2x+1$, and $z = 2x+3y+5$.\\
		\small{Note the similarity to the equation for lines. One can think of a plane as a line extruded in a direction.}}
	\item{Cylinders have forms like $\left(x-4\right)^2 + \left(y-5\right)^2 = 36$.\\
		\small{Note the similarity to the equation for circles. One can think of a cylinder as a circle extruded away from it's face.}}
	\item{Spheres have forms like $\left(x-7\right)^2 + \left(y-8\right)^2 + \left(z-9\right)^2 = 100$.\\
		\small{Note the similarity to the equations for circles. One can think of a sphere as a circle rotated and extruded about its center.}}
\end{itemize}


%auto-ignore
\providecommand{\MainFolder}{..}
\documentclass[\MainFolder/Text.tex]{subfiles}

\begin{document}
\section{Standard Hodge propagator for Euclidean space}\label{Sec:HeatRN}
\allowdisplaybreaks

We will use Propositions~\ref{Prop:StdCodifInt} and~\ref{Prop:HeatKerFormulas} to prove in two ways that $\StdPrpg$ for $\R^n$ extends smoothly to the blow-up. We will start with the following well known formulas.
\begin{Proposition}[Green form and heat kernel for $\R^n$]\label{Prop:GreenKernelRn}
The Green form for $\R^n$, which can be equivalently characterized as the unique solution $\GKer\in \DR^n(\R^n\times\R^n\backslash\Diag)$ of
\begin{equation}\label{Eq:GrRn}
\Laplace_y \GKer(x,y) = \delta(x-y)(\Diff{x}^1-\Diff{y}^1)\dotsb(\Diff{x}^n-\Diff{y}^n),
\end{equation}
where $\delta(x-y)$ is the Dirac delta function, satisfies
$$ \GKer(x,y) = \begin{cases}
\displaystyle\frac{1}{(n-2) \Vol(\Sph{n-1})} \frac{1}{\Abs{x-y}^{n-2}}(\Diff{x}^1 - \Diff{y}^1) \dotsb (\Diff{x}^n - \Diff{y}^n) & \text{for }n\ge 3, \\[1em]
-\dfrac{1}{2\pi}\ln \Abs{x-y}(\Diff{x}^1 - \Diff{y}^1)(\Diff{x}^2 - \Diff{y}^2)& \text{for }n=2.
\end{cases} $$
The heat kernel for $\R^n$, i.e., the solution of $\Laplace_y \QKer_t(x,y) = -\frac{\partial}{\partial t}\QKer_t(x,y)$,  is given by
\begin{equation}\label{Eq:EuclidHeatKer}
\QKer_t(x,y) = (4\pi t)^{-\frac{n}{2}}\exp\Bigl(-\frac{\Abs{x-y}^2}{4t}\Bigr) (\Diff{x}^1 - \Diff{y}^1)\ldots (\Diff{x}^n-\Diff{y}^n).
\end{equation}
\end{Proposition}
\begin{proof}
See \cite{BGV}.
\end{proof}

\begin{Proposition}[Standard Hodge propagator for $\R^n$]\label{Prop:StdHodgePropRn}
The standard Hodge propagator for $\R^n$ with $n\ge 2$ satisfies
$$\StdPrpg(x,y) = \frac{(-1)^{n+1}}{\Vol(\Sph{n-1})} \sum_{i=1}^n (-1)^{i-1} \frac{x^i-y^i}{\Abs{x-y}^n}(\Diff{x}^1 - \Diff{y}^1)\dotsb\reallywidehat{(\Diff{x}^i - \Diff{y}^i)}\dotsb(\Diff{x}^n-\Diff{y}^n) $$
and extends smoothly to $\Bl_\Diag(\R^n\times \R^n)$.
\end{Proposition}
\begin{proof}[Proof using the Green form]
Consider the formulas from Proposition~\ref{Prop:GreenKernelRn}. Recall the formula for the codifferential
\begin{equation}\label{Eq:CodiffRn}
\CoDd f(x)\Diff{x}^I = - \sum_{i\in I} \varepsilon(i,I) \frac{\partial f(x)}{\partial x^i} \Diff{x}^{I\backslash\{i\}}.
\end{equation}
For $n\ge 3$, we compute
\begin{align*}
&(\Id\otimes\CoDd_y)\GKer(x,y) \\
&\quad=\frac{1}{(n-2)\Vol(\Sph{n-1})} \sum_I (-1)^{n\Abs{I}} (\Id \otimes \CoDd_y)\Bigl(\frac{1}{\Abs{x-y}^{n-2}}(\Star\Diff{x}^I)\wedge\Diff{y}^I\Bigr) \\
&\quad=\frac{1}{\Vol(\Sph{n-1})} \sum_{I} (-1)^{n\Abs{I} + \Abs{I} + n+1} \sum_{i\in I}\frac{x^i-y^i}{\Abs{x-y}^n}\varepsilon(i,I)(\Star\Diff{x}^{I})\wedge\Diff{y}^{I\backslash\{i\}}\\
&\quad=\frac{1}{\Vol(\Sph{n-1})} \sum_{I} (-1)^{n\Abs{I} + \Abs{I} + n+1} \sum_{i\in I}\frac{x^i-y^i}{\Abs{x-y}^n}\varepsilon(i,I)\varepsilon(I,I^c) \Diff{x}^{I^c}\wedge\Diff{y}^{I\backslash\{i\}}\\
&\quad=\begin{multlined}[t]\frac{1}{\Vol(\Sph{n-1})} \sum_{i=1}^n \frac{x^i-y^i}{\Abs{x-y}^n} \sum_{\substack{J\subset \{1,\dotsc,\hat{i},\dotsc,n\}}}(-1)^{n\Abs{J} + \Abs{J}}\varepsilon(i,J)\varepsilon(J\cup\{i\},J^c\backslash\{i\})\\ \Diff{x}^{J^c\backslash\{i\}} \wedge \Diff{y}^{J}\end{multlined}\\
&\quad=\frac{1}{\Vol(\Sph{n-1})} \sum_{i=1}^n (-1)^{i-1} \frac{x^i-y^i}{\Abs{x-y}^n}(\Diff{x}^1 - \Diff{y}^1)\dotsb\reallywidehat{(\Diff{x}^i - \Diff{y}^i)}\dotsb(\Diff{x}^n-\Diff{y}^n).
\end{align*}
In the last step, we used that
$$ \varepsilon(i,J)\varepsilon(J\cup\{i\},J^c\backslash\{i\}) = (-1)^{i-1}\varepsilon(J,J^c\backslash\{i\}) $$
and
\begin{align*}
&\Diff{x}^{J^c\backslash\{i\}} \wedge \Diff{y}^{J}\\
& = (-1)^{\Abs{J}} \varepsilon(J^c\backslash\{i\},J)[(\Diff{x}^1-\Diff{y}^1)\dotsb\reallywidehat{(\Diff{x}^i-\Diff{y}^i)}\dotsb (\Diff{x}^n-\Diff{y}^n)]_{J^{c}\backslash\{i\}, J}\\
& = (-1)^{n \Abs{J} + \Abs{J}}[(\Diff{x}^1-\Diff{y}^1)\dotsb\reallywidehat{(\Diff{x}^i-\Diff{y}^i)}\dotsb (\Diff{x}^n-\Diff{y}^n)]_{J^{c}\backslash\{i\}, J},
\end{align*}
where $[\cdot]_{I_1,I_2}$ denotes the part of the product which picks the first variable at positions $I_1$ and the second at positions $I_2$. The computation for $n=2$ gives the same result, and the formula for $\StdPrpg$ is justified by Proposition~\ref{Prop:StdCodifInt}.
%\begin{align*}
%(\Diff{x}^1 - \Diff{y}^1) \dotsb (\Diff{x}^n - \Diff{y}^n) &=\sum_{k=0}^n(-1)^{n-k} \sum_{\substack{I\subset \{1,\dotsc,n\}\\\Abs{I}=k}} \varepsilon(I I^c \mapsto[n])\Diff{x}^{I} \wedge \Diff{y}^{I^c} \\
%&=\sum_{k=0}^n(-1)^{n-k}\sum_{\substack{I \subset \{1,\dotsb,n\}\\\Abs{I}=k}} \Diff{x}^I \wedge (\Star \Diff{y}^I)
%\end{align*}

We will now study whether $\StdPrpg$ extends smoothly to the blow-up. Consider the polar coordinates in one variable
\begin{align*}
\phi: [0,\infty) \times \Sph{n} \times \R^n  &\longrightarrow \R^n \times \R^n \\
(r,\omega,u) &\longmapsto (u+r\omega, u).
\end{align*}
There is a unique smooth map $\tilde{\phi}$ which fits in the blow-up diagram
$$\begin{tikzcd}
 & \Bl_\Diag(\R^n\times \R^n)\arrow{d}{\pi}\\{}
 [0,\infty)\times\Sph{n}\times\R^n\arrow{r}{\phi}\arrow{ru}{\tilde{\phi}}& \R^n\times \R^n,
\end{tikzcd}$$
and it is a diffeomorphism of manifolds with boundary. We denote by
$$ \phi_0 \coloneqq \Restr{\phi}{(0,\infty) \times \Sph{n}\times\R^n} : (0,\infty)\times\Sph{n}\times\R^n\longrightarrow \R^n \times \R^n\backslash\Diag $$
the restriction of $\phi$ to the interior. The commutative diagram
$$\begin{tikzcd}{}
[0,\infty)\times\Sph{n}\times\R^n\arrow{r}{\tilde{\phi}}& \Bl_\Diag(\R^n\times\R^n) \\
(0,\infty)\times\Sph{n}\times\R^n\arrow{r}{\phi_0}\arrow[hook]{u} & \R^n\times\R^n\backslash\Diag \arrow[hook]{u}{\tilde{\iota}}
\end{tikzcd}$$
shows that we can equivalently study whether the form
$$ \StdPrpgPol\coloneqq \phi_0^* \StdPrpg $$
admits a smooth extension $\widetilde{\StdPrpgPol}$ to $[0,\infty)\times\Sph{n-1}\times\R^n$. We have
\begin{align*}
\StdPrpgPol(r,\omega,u) &= \phi_0^*\bigl(\CoDd_y \GKer)(r,\omega,u) \\
& = \begin{multlined}[t]
\frac{(-1)^{n+1}}{r^{n-1}\Vol(\Sph{n-1})}\sum_{i=1}^n (-1)^{i-1} \omega^i (r\Diff{\omega}^1 + \omega^1 \Diff{r}) \dotsb \reallywidehat{(r\Diff{\omega}^i + \omega^i \Diff{r})} \dotsb \\ (r\Diff{\omega}^n + \omega^n \Diff{r})
\end{multlined}\\
 & = \begin{aligned}[t]
 \frac{(-1)^{n+1}}{r^{n-1}\Vol(\Sph{n-1})} \Bigl(r^{n-1}&\sum_{1\le i \le n}(-1)^{i-1}\omega^i\Diff{\omega}^1\dotsb\widehat{\Diff{\omega}^i}\dotsb\Diff{\omega}^n    \\
  - r^{n-2}&\sum_{1\le i < j \le n}(-1)^{i+j} \omega^i \omega^j \Diff{r}\Diff{\omega}^1 \dotsb \widehat{\Diff{\omega}^i} \dotsb \widehat{\Diff{\omega}^j} \dotsb\Diff{\omega}^n \\
  + r^{n-2}&\sum_{1 \le j < i \le n}(-1)^{i+j} \omega^j\omega^i \Diff{r} \Diff{\omega}^1 \dotsb \widehat{\Diff{\omega}^j} \dotsb \widehat{\Diff{\omega}^i} \dotsb\Diff{\omega}^n\Bigr)
  \end{aligned} \\
 & = (-1)^{n+1}\frac{\Vol_{\Sph{n-1}}(\omega)}{\Vol(\Sph{n-1})},
\end{align*}
where the last two sums canceled. The result extends smoothly beyond $r=0$ because it does not depend on $r$ at all.
\end{proof}
\begin{proof}[Proof using the heat kernel]
Consider the polar coordinates with respect to the diagonal
$$ \begin{aligned}
   \varphi: [0,\infty)\times \Sph{n-1} \times \R^n &\longrightarrow \R^n \times \R^n \backslash \Diag \\
 (r,\omega,u) &\longmapsto (x,y) = (u+r\omega, u-r\omega).
   \end{aligned} $$
There is a unique smooth map $\tilde{\varphi}$ which fits in the blow-up diagram
$$\begin{tikzcd}
 & \Bl_\Diag(\R^n\times \R^n)\arrow{d}{\pi}\\{}
 [0,\infty)\times\Sph{n}\times\R^n\arrow{r}{\varphi}\arrow{ru}{\tilde{\varphi}}& \R^n\times \R^n,
\end{tikzcd}$$
and it is a diffeomorphism of manifolds with boundary. The heat kernel \eqref{Eq:EuclidHeatKer} transforms under $\varphi$ as
\begin{align*}
\QKerPol_t(r,\omega,u) &\coloneqq (\varphi^* \QKer_t)(r,\omega,u) \\
 & = (4 \pi t)^{-\frac{n}{2}} \exp(-\frac{r^2}{t}) 2^n r^{n-1} \Diff{r} \Vol(\omega) \\
 &= (\pi t)^{-\frac{n}{2}} \exp(-\frac{r^2}{t}) r^{n-1} \Diff{r} \Vol(\omega).
\end{align*}
It follows from Proposition~\ref{Prop:HeatKerFormulas} that for $(r,\omega,u)\in (0,\infty) \times \Sph{n-1}\times \R^n$ we have
\begin{equation}\label{Eq:StdPrpgPol}
\StdPrpgPol(r,\omega,u) = (-1)^{n+1}\frac{1}{2}\lim_{t\to 0} \int_t^\infty \CoDdPol \QKerPol_\tau(r,\omega,u) \Diff{\tau},
\end{equation}
where $\CoDdPol$ is the codifferential on $\R^n\times \R^n$ computed with respect to the pullback of the product Riemannian metric. If $g: \R^n \otimes \R^n\rightarrow \R$ is an inner product, then the pullback Riemannian metric satisfies
\begin{equation}\label{Eq:PullbackMetric}
g^{\mathrm{pol}}\coloneqq \varphi^*(g\oplus g)(r,\omega,u) = 2\begin{pmatrix}
g(\omega,\omega) & 0 & 0 \\
0 & r^2 g & 0 \\
0 & 0 & g
\end{pmatrix};
\end{equation}
it is degenerate at the boundary $r=0$. Next, it is easy to check that the conformal transformation $g\mapsto\lambda g$ on vectors induces the conformal transformation $g\mapsto\frac{1}{\lambda^k}g$ on $k$-forms. Thus, if $\Norm{\cdot}^\mathrm{pol}$ denotes the point-wise norm with respect to $g^{\mathrm{pol}}$ for the standard Euclidean metric, then
$$ \Norm{\Diff{r}}^{\mathrm{pol}} = 2^{-\frac{1}{2}},\quad\Norm{\Vol(u)}^{\mathrm{pol}} = 2^{-\frac{n}{2}},\quad\Norm{\Vol(\omega)}^{\mathrm{pol}} = 2^{-\frac{n-1}{2}} r^{-(n-1)}, $$
and hence
$$ \VolPol(r,\omega,u) = 2^{n}r^{n-1}\Diff{r}\Vol(\omega)\Vol(u). $$
In order to get the Hodge star $\StarPol$, we compute
\begin{align*}
\Diff{r}\Vol(\omega)\wedge \StarPol \bigl(\Diff{r}\Vol(\omega)\bigr) & \overset{!}{=} {\Norm{\Diff{r}\Vol(\omega)}^{\mathrm{pol}}}^2 \Vol^{\mathrm{pol}}(r,\omega,u) \\
&=r^{-(n-1)} \Diff{r}\Vol(\omega)\Vol(u), \\
\Diff{r}\Vol(u) \wedge \StarPol(\Diff{r}\Vol(u)) &\overset{!}{=} {\Norm{\Diff{r} \Vol(u)}^{\mathrm{pol}}}^2\Vol^{\mathrm{pol}}(r,\omega,u) \\
&=\frac{1}{2} r^{n-1} \Diff{r} \Vol(\omega) \Vol(u).
\end{align*}
Using the product structure \eqref{Eq:PullbackMetric}, it follows that
\begin{align*}
\StarPol\bigl(\Diff{r}\Vol(\omega)\bigr)&=r^{-(n-1)} \Vol(u)\quad\text{and}\\
\StarPol(\Diff{r}\Vol(u))&= \frac{1}{2} r^{n-1} \Vol(\omega).
\end{align*}
Recalling the definition of the codifferential $\CoDd \alpha = (-1)^{n(k-1)+1} \Star\Dd\Star \alpha$, we compute
\begin{align*}
(\StarPol \Dd \StarPol)\bigl(\QKerPol_t(r,\omega,u)\bigr) &=  \StarPol \Dd \Bigl( (\pi t)^{-\frac{n}{2}} \exp(-\frac{r^2}{t}) \Vol(u) \Bigr)  \\
& = \StarPol\Bigl(\frac{-2r}{t}(\pi t)^{-\frac{n}{2}}\exp(-\frac{r^2}{t}) \Diff{r} \Vol(u)\Bigr)\\
&= -\frac{1}{t}(\pi t)^{-\frac{n}{2}} \exp(-\frac{r^2}{t}) r^n \Vol(\omega),
\end{align*}
and because the product dimension is even, we obtain
$$ \CoDdPol \QKerPol_t(r,\omega,u) = \frac{1}{t}(\pi t)^{-\frac{n}{2}} \exp(-\frac{r^2}{t}) r^n \Vol(\omega). $$
In order to integrate this according to \eqref{Eq:StdPrpgPol}, we will make use of the $\Gamma$-function $\Gamma(z) = \int_0^\infty x^{z-1} e^{-x} \Diff{x}$. Then, we have
\begin{align*}
(-1)^{n+1}\StdPrpgPol(r,\omega,u)&=\frac{1}{2}\lim_{t\to 0} \int_t^\infty \CoDdPol \QKerPol_\tau(r,\omega,u)\Diff{\tau}\\
&=\frac{1}{2}\pi^{-\frac{n}{2}}r^n \Bigl(\lim_{t\to 0}\int_{t}^\infty \tau^{-\frac{n}{2} - 1} \exp(-\frac{r^2}{\tau})\Diff{\tau}\Bigr)\Vol(\omega)\\
&=\frac{1}{2}\pi^{-\frac{n}{2}}r^n \Bigl(\lim_{t\to 0}\int_{\frac{r^2}{t}}^0 \Bigl(\frac{r^2}{z}\Bigr)^{-\frac{n}{2} - 1}\exp(-z)\bigl(-z^{-2}r^2\Diff{z}\bigr)\Bigr)\Vol(\omega)\\
&= \frac{1}{2}\pi^{-\frac{n}{2}}\Bigl(\lim_{t\to 0} \int_0^{\frac{r^2}{t}} z^{\frac{n}{2}-1} \exp(-z) \Diff{z}\Bigr)\Vol(\omega) \\
&= \frac{\Gamma(\frac{n}{2})}{2\pi^{\frac{n}{2}}}\Vol(\omega) \\
& = \frac{\Vol(\omega)}{\Vol(\Sph{n-1})},
\end{align*}
which recovers the formula in the other proof of Proposition~\ref{Prop:StdHodgePropRn}.
\end{proof}

\begin{Example}[The case $n=1$]\label{Ex:SDFSDF}
It is easy to check directly that
$$ \Prpg(x,y) \coloneqq \theta(x-y), $$
where $\theta$ is the Heavyside step function, is the Schwartz form of a Hodge homotopy $\Htp$. Indeed, for any smooth $f: \R\rightarrow\R$ with compact support, we have
$$ \Htp(f \Diff{x})(y) = \int_x \Prpg(x,y)f(x)\Diff{x} = \int_{y}^{\infty} f(x)\Diff{x}, $$
and hence $\Dd\circ\Htp = -\Id$. It is also easy to check that the following is the Laplace Green form:
$$ \GKer(x,y) = - \frac{1}{2}\Abs{x-y}(\Diff{x}-\Diff{y}). $$
Indeed, using $\Sgn(x) = 2\theta(x) - 1$, we get
$$ \frac{\partial^2}{\partial y^2} \frac{1}{2}\Abs{x-y} = \frac{\partial}{\partial y}\frac{1}{2}\Sgn(y-x) = \delta(y-x) = \delta(x-y), $$
which shows \eqref{Eq:GrRn}. We carefully compute
\begin{align*}
(\Id\otimes \CoDd_y)\GKer(x,y) & = \CoDd_y \Bigl(\frac{1}{2}\Abs{x-y}\Diff{y}\Bigr) \\
& = -\frac{1}{2} \frac{\partial}{\partial y}\Abs{x-y} \\
& = -\frac{1}{2}(2\theta(y-x)-1) \\
& = - \theta(y-x) + \frac{1}{2} \\
& = \theta(x-y) - \frac{1}{2}
\end{align*}
and apply Proposition~\ref{Prop:StdCodifInt} to get
$$ \StdPrpg(x,y) = \theta(x-y) - \frac{1}{2}. $$
In particular, we see that the sign agrees with the direct computation of the Hodge propagator above. Smoothness on the blow-up is for $n=1$ equivalent to smoothness on the closures of connected components of $\R\times\R\backslash\Diag$; this is readily satisfied.
\end{Example}

\begin{Question}
	Is it possible to use the asymptotic heat kernel expansion and formula~\eqref{Eq:PrpgUsingHeatKernel} or the asymptotic Green kernel expansion and formula \eqref{Eq:FormForPUsingG} to infer from the case of $\R^n$ with the standard Euclidean metric $g_0$ that $\StdPrpg$ extends smoothly to the blow-up for flat manifolds, i.e., locally isometric to $(\R^n,g_0)$?
\end{Question}

%NOTICE THAT ALREADY $\GKer$ EXTENDS SMOOTHLY TO THE BLOW-UP. BUT ACCORDING TO EXAMPLE BLAH BLAH WE CAN NOT USE IT TO PROVE IT BECAUSE OF THE POLARZIED OPERATORS. MAYBE YES BECAUSE OF ITS SIMPLE FORM?
\end{document}


\chapter{constElm}

\section{Introduction}

A linearized process may have parameters that do not change value throughout the state space of that process.
These parameters are essentially \emph{constants}.

Constants can be removed from a process as follows:
\begin{itemize}
\item Substitute references to a constant by the explicit value of that constant;
\item Remove the constant from the process parameter list.
\end{itemize}

The advantages of removing constants from a process are smaller state vectors (and therefore reduced memory usage) and faster performance in general.
These advantages may be small; however, the detection and removal of constants can be done fairly cheaply.

\section{Algorithm}

The algorithm consists of the following steps \cite{groote2001computer}:

\begin{enumerate}

\item Mark all process parameters.
(This means that, initially, we assume that all process parameters are constants.)

\item Let $P$ be the set of all marked process parameters.
Define a substitution $\rho = [p \rightarrow v_0(p) \;|\; p \in P]$, where $v_0$ is a function that gives the initial value of a given process parameter.

\item Consider each summand $s$ of the LPE.
Construct an equation $c_s \rightarrow p = v_s(p)$ for all $p \in P$, where $c_s$ is the guard of $s$ and where $v_s$ is a function that gives the expression of which the value is assigned to the process parameter $p$ in the instantiation, and apply the substitution $\rho$ to it.
(This gives $(c_s \rightarrow p = v_s(p))[\rho] \Leftrightarrow {c_s}[\rho] \rightarrow v_0(p) = v_s(p)[\rho]$.)

If the obtained equation is a tautology (that is, if its negation is unsatisfiable) for all $s$, $p$ remains marked; otherwise, unmark $p$.

\item Repeat the previous two steps until $P$ remains constant.
All remaining marked process parameters can be safely removed the process.

\end{enumerate}

\section{Example}

Consider the following LPE:

\begin{lstlisting}
//Process definition:
PROCDEF example[A](x, y, z :: Int)
  = A [[z = 2]] >-> example[A](z-1, 1, 2)
  + A >-> example[A](y, x, x+y)
  + A >-> example[A](1, x, z+1)
  ;

//Initialization:
example[A](1, 1, 2);
\end{lstlisting}

First, $\rho = [ x \rightarrow 1, y \rightarrow 1, z \rightarrow 2 ]$.

We must check the following equations:

\begin{align*}
(z = 2 \rightarrow x = z-1)[\rho] &\Leftrightarrow (2 = 2 \rightarrow 1 = 2-1) \Leftrightarrow \textit{true} \\
(z = 2 \rightarrow y = 1)[\rho] &\Leftrightarrow (2 = 2 \rightarrow 1 = 1) \Leftrightarrow \textit{true} \\
(z = 2 \rightarrow z = 2)[\rho] &\Leftrightarrow (2 = 2 \rightarrow 2 = 2) \Leftrightarrow \textit{true} \\
(x = y)[\rho] &\Leftrightarrow (1 = 1) \Leftrightarrow \textit{true} \\
(y = x)[\rho] &\Leftrightarrow (1 = 1) \Leftrightarrow \textit{true} \\
(z = x+y)[\rho] &\Leftrightarrow (2 = 1+1) \Leftrightarrow \textit{true} \\
(x = 1)[\rho] &\Leftrightarrow (1 = 1) \Leftrightarrow \textit{true} \\
(y = x)[\rho] &\Leftrightarrow (1 = 1) \Leftrightarrow \textit{true} \\
(z = z+1)[\rho] &\Leftrightarrow (2 = 2+1) \Leftrightarrow \textit{false} \\
\end{align*}

The last equation is not a tautology, and so $z$ is unmarked.

The new value of $\rho$ is $[ x \rightarrow 1, y \rightarrow 1 ]$.

\clearpage
The equations are now the following:

\begin{align*}
(z = 2 \rightarrow x = z-1)[\rho] &\Leftrightarrow (z = 2 \rightarrow 1 = z-1) \Leftrightarrow \textit{true} \\
(z = 2 \rightarrow y = 1)[\rho] &\Leftrightarrow (z = 2 \rightarrow 1 = 1) \Leftrightarrow \textit{true} \\
(x = y)[\rho] &\Leftrightarrow (1 = 1) \Leftrightarrow \textit{true} \\
(y = x)[\rho] &\Leftrightarrow (1 = 1) \Leftrightarrow \textit{true} \\
(x = 1)[\rho] &\Leftrightarrow (1 = 1) \Leftrightarrow \textit{true} \\
(y = x)[\rho] &\Leftrightarrow (1 = 1) \Leftrightarrow \textit{true} \\
\end{align*}

All of the equations above are tautologies, and so $x$ and $y$ remain marked.
Removing the marked parameters from the LPE gives

\begin{lstlisting}
//Process definition:
PROCDEF example[A](z :: Int)
  = A [[z==2]] >-> example[A](2)
  + A >-> example[A](2)
  + A >-> example[A](z+1)
  ;

//Initialization:
example[A](2);
\end{lstlisting}

Obviously, more simplification is possible.


\lab{Facial Recognition using Eigenfaces}{Facial Recognition using Eigenfaces}
\label{lab:FacialRecognition}

\objective{Use the singular value decomposition to build a facial recognition system.}

Suppose we have a large database containing images of human faces.
We would like to identify people by matching their pictures to those in the database.
This task is called \emph{facial recognition}.

Facial recognition is important in law enforcement, as well as other situations.
For example, facial recognition can be combined with video surveillance to identify when a person is not authorized to be in a location.

Humans can easily compare two face images and determine whether they belong to the same person, but automating this process is more challenging.
One technique for automated facial recognition uses \emph{eigenfaces}.

\section*{Load the data}
Eigenfaces are an efficient way to store and query a database of face images.
As the name suggests, this method uses eigenvectors of matrices related to the collection of face images.
Essentially, the method of eigenfaces projects face images to a lower-dimensional subspace in a way that preserves their distinguishing characteristics.
In the lower-dimensional subspace, comparing face images is much faster.

Recall that a digital image may be stored as an $m \times n$ array of pixel intensities.
In this lab, we will store the images as $mn$-vectors by concatenating the rows of the $m \times n$ arrays.

\begin{problem}
\label{prob:getTrainingFaces}
The problems in this lab will help you write a class \li{FacialRec} to perform facial recognition.
First we need to get a database of face images.
\begin{enumerate}
\item Download the \li{faces94} face image database found at \url{http://cswww.essex.ac.uk/mv/allfaces/faces94.html} and extract the files.
You should now have a directory named ``faces94" which contains photographs of many people, organized into folders by person.
\end{enumerate}
With this directory we can begin to write our \li{FacialRec} class. This class is outlined below.
\begin{lstlisting}
import numpy as np
from scipy import linalg as la
from os import walk
from scipy.ndimage import imread
from matplotlib import pyplot as plt
import matplotlib.cm as cm
from random import sample

class FacialRec:
    ##########Members##########
    #   F, mu, Fbar, and U
    ###########################
    def __init__(self,path):
        self.initFaces(path)
        self.initMeanImage()
        self.initDifferences()
        self.initEigenfaces()

    def initFaces(self, path):
        self.F = getFaces(path)
    def initMeanImage(self):
        pass
    def initDifferences(self):
        pass
    def initEigenfaces(self):
        pass
    def project(self, s):
        pass
    def findNearest(self, image):
        pass
\end{lstlisting}

The function \li{getFaces()} should construct a database of face images by selecting exactly one face image for each person in the directory.
It should return an array whose columns are the selected face images.
One implementation of this function is found at the end of this lab.

\begin{enumerate}[resume]
\item Initialize a \li{FacialRec} instance with the command \li{facialRec = FacialRec(\"./faces94\")}. You may have to replace the parameter \li{\"./faces94\"} with the location of the directory \li{faces94} on your machine. Check that \li{facialRec.F} is a $360000 \times 153$ array. The columns of this array are 153 face images of 153 different people.
\end{enumerate}
\end{problem}

\section*{Shift by the mean}

The facial recognition algorithm is more robust if we first \emph{shift by the mean}.
Suppose we have a collection of $k$ face images represented as vectors $\f_1, \f_2, \ldots, \f_k$ of length $mn$.
Define the \emph{mean face} ${\boldsymbol \mu}$ to be the average of the $\f_i$:
\[
{\boldsymbol \mu} = \frac{1}{k}\sum_{i=1}^k \f_i.
\]
\begin{figure}
\includegraphics[width=0.3\textwidth]{meanFace.png}
\caption{The mean face.}
\label{facialRecognition:meanFace}
\end{figure}

\begin{problem}
\label{prob:meanFace}
\leavevmode
\begin{enumerate}
\item Implement the method \li{FacialRec.initMeanImage()} as follows.
\begin{lstlisting}
def initMeanImage(self):
    self.mu = # Compute the mean face of the images in self.F
\end{lstlisting}
This can be done in one line using \li{np.mean} and specifying the correct axis.

\item Plot the mean face.
The function \li{show()} at the end of this lab will plot a flattened grayscale image.
Your mean face should match Figure \ref{facialRecognition:meanFace}.
\end{enumerate}
\end{problem}

\begin{figure}
\begin{subfigure}[b]{0.3\textwidth}
\includegraphics[width=\textwidth]{differenceFace0.png}
\end{subfigure}
\begin{subfigure}[b]{0.3\textwidth}
\includegraphics[width=\textwidth]{differenceFace1.png}
\end{subfigure}
\begin{subfigure}[b]{0.3\textwidth}
\includegraphics[width=\textwidth]{differenceFace2.png}
\end{subfigure}
\caption{Three mean-shifted faces from the dataset.}
\label{facialRecognition:differenceFaces}
\end{figure}
For each $i = 1,\ldots, k$, define $\bar{\f}_i := \f_i - {\boldsymbol \mu}$.
The mean-shifted face vector $\bar{\f}_i$ is the deviation of the $i$-th face from the mean, and thus captures the unique features of the face.
Now form the $mn \times k$ matrix $\bar{F}$ whose columns are given by the mean-shifted face vectors, i.e.
\[
\bar{F} = \begin{bmatrix}
\bar{\f}_1 & \bar{\f}_2 & \cdots & \bar{\f}_k
\end{bmatrix}.
\]
\begin{problem}
\leavevmode
\begin{enumerate}
\item Implement the method \li{FacialRec.initDifferences()} to compute $\bar{F}$ as follows.
\begin{lstlisting}
def initDifferences(self):
    self.Fbar = # Compute the mean-shifted face vectors Fbar
\end{lstlisting}
This can be done in one line using array broadcasting.
\item
Plot the first mean-shifted face, i.e. the first column of the matrix \li{Fbar}.
It should match the first face in Figure \ref{facialRecognition:differenceFaces}.
\end{enumerate}
\end{problem}

\section*{Project to a subspace}
Now suppose we have a new face vector $\g$.
The closest face image to $\g$ should be the vector $\bar{\f}_i$ that minimizes $\|\bar{\g}-\bar{\f}_i\|_2$, where $\bar{\g} = \g - {\boldsymbol \mu}$.
Unfortunately, computing $\|\bar{\g}-\bar{\f}_i\|_2$ for each $i$ is computationally intractable when the length $mn$ of the vectors is large.
Since a low-resolution photo may easily have $100\times 100 = 10,000$ pixels, in practice $mn$ is very large indeed.

Here is the trick: instead of computing in the $mn$-dimensional space of all possible images, we will compute in a lower-dimensional subspace.
We could start by using the subspace spanned by the vectors $\bar{\f}_1, \ldots \bar{\f}_k$, which is at most $k$-dimensional.
Unfortunately, in practice $k$ is still to large for this subspace to be computationally efficient.
%We need to project our problem into some subspace of the span of $\bar{\f}_1, \ldots \bar{\f}_k$.

Therefore, we want to project to a subspace of $\bar{\f}_1, \ldots \bar{\f}_k$ in a way that retains as much information about the $\bar{\f_i}$ as possible.
Mathematically, we want to find the $s$-dimensional subspace of $\text{span}\{\bar{\f}_1, \ldots \bar{\f}_k\}$ that is closest to the $\bar{\f_i}$ in the least-squares sense.
In a minute, we will prove that the SVD of $\bar{F}$ (whose columns are $\bar{\f}_1, \ldots \bar{\f}_k$) solves this problem.
But first, let us summarize how the solution works.

Let $U \Sigma V^T$ be an SVD of $\bar{F}$ with $\u_i$ the columns of $U$.
Then the ``best'' $s$-dimensional subspace for approximating $\text{span}\{\bar{\f}_1, \ldots \bar{\f}_k\}$ is the span of $\u_1, \ldots, \u_s$.
The matrix for this projection is $P_s = U_s U_s^T$ where $U_s = [ \u_1\; \ldots \; \u_s]$.

Because the vectors $\u_i$ are eigenvectors of $\bar{F}\bar{F}^T$, we call them \emph{eigenfaces}.
Therefore, the best $s$-dimensional subspace for solving the facial recognition problem is exactly the span of the first $s$ eigenfaces.

\subsection*{The proof: SVD as a least squares solution}
\begin{theorem}
Let $\f_1, \ldots, \f_k$ be vectors on $\mathbb{R}^{mn}$, and let $\bar{F} = [\bar{f}_1 \; \ldots \; \bar{f}_k]$. Suppose $U\Sigma V^T$ is an SVD for $\bar{F}$. Then the $s$-dimensional subspace that solves the least squares problem for $\f_1, \ldots, \f_k$ is the span of the first $s$ columns of $U$. If $U_s$ is the first $s$ columns of $U$, then the matrix $U_sU_s^T$ is projection onto this subspace.
\end{theorem}
\begin{proof}
We seek a rank-$s$ projection matrix $P_s$ so that $\sum_{i=1}^k \|P_s\bar{\f}_i - \bar{\f}_i\|_2^2$ is minimized---i.e.,
 the sum of the squares of the ``errors'' is minimal when we project $\bar{\f}_i$ via $P_s$.
 But minimizing this quantity is the same as minimizing its square, which happens to equal the Frobenius norm of $P_s\bar{F} - \bar{F}$.
Written mathematically,
 \begin{align*}
\inf_{\text{rank}(P_s)=s} \sum_{i=1}^k \|P_s\bar{\f}_i - \bar{\f}_i\|_2^2 &=  \inf_{\text{rank}(P_s)=s} \left( \sum_{i=1}^k \|P_s\bar{\f}_i - \bar{\f}_i\|_2^2 \right) ^2\\
 & =  \inf_{\text{rank}(P_s)=s} \| P_s\bar{F}-\bar{F}\|_F.
 \end{align*}

Now let $U \Sigma V^T$ be an SVD of $\bar{F}$ with $\u_i$ the columns of $U$, $\v_i$ the columns of $V$, and $\sigma_i$ the singular values of $\bar{F}$.
If $P_s = \sum_{i=1}^s \u_i \u_i^T$, then
\begin{align*}
P_s\bar{F} &=  \left( \sum_{i=1}^s \u_i \u_i^T \right)\left(  \sum_{j=1}^k \sigma_j \u_i \v_i^T \right)
= \sum_{i=1}^s\sum_{j=1}^k \sigma_j \u_i\u_i^T\u_j\v_j^T\\
&=  \sum_{i=1}^s\sum_{j=1}^k \sigma_j \u_i\delta_{ij}\v_j^T
= \sum_{i=1}^s \sigma_i \u_i\v_i^T.
\end{align*}

In fact, the Schmidt-Eckart-Young-Mirsky Theorem from Lab \ref{lab:SVD} tells us that $X = \sum_{i=1}^s \sigma_i \u_i\v_i^T$ is exactly the rank-$s$ matrix that minimizes $\|X - \bar{F}\|_F$.
Since $P_s \bar{F}$ will always have rank $s$ or less, the projection $P_s =  \sum_{i=1}^s \u_i \u_i^T$ is the one we seek.
If we let $U_s = [ \u_1\; \ldots \; \u_s]$, then we may write $P_s = U_sU_s^T$. Notice that $P_s$ is projection onto the subspace spanned by the columns of $U_s$.
\end{proof}

\begin{problem}
\label{prob:svd}
\leavevmode
\begin{enumerate}
\item Implement the method \li{FacialRec.initEigenfaces()} as follows.
\begin{lstlisting}
def initEigenfaces(self):
    self.U, s, Vt = # Compute the SVD of Fbar
\end{lstlisting}
This can be done in one line with the function \li{linalg.svd()}.
Because we will only use the first few columns of $U$, specify the keyword parameter \li{full_matrices=False} to compute only the compact SVD.
\item Plot the first eigenface (i.e. the first column of \li{U}).
It should match the first eigenface shown in Figure \ref{facialRecognition:eigenfaces}.
\end{enumerate}
\end{problem}

\section*{Change basis}

It does us no good to project all our vectors into an $s$-dimensional space if we still store them as vectors in $\mathbb{R}^{nm}$.
Instead, we must store our face vectors in terms of the columns of $U_s$.
This way, each vector is a lenth-$s$ array in NumPy, instead of a length-$mn$ array.

The change-of-basis matrix is $U_s^T$, so $U_s^T\widehat{P}_s = U_s^TU_sU_s^T = U_s^T$.
Thus we can project into the subspace and change basis by multiplying by $U_s^T$.
To change back to the full $mn$-vector, multiply by $U_s$.

\begin{figure}
\begin{subfigure}[b]{0.3\textwidth}
\includegraphics[width=\textwidth]{eigenface0.png}
\end{subfigure}
\begin{subfigure}[b]{0.3\textwidth}
\includegraphics[width=\textwidth]{eigenface1.png}
\end{subfigure}
\begin{subfigure}[b]{0.3\textwidth}
\includegraphics[width=\textwidth]{eigenface2.png}
\end{subfigure}
\caption{The top three eigenfaces.}
\label{facialRecognition:eigenfaces}
\end{figure}

\begin{problem}
\label{prob:top_n}
\leavevmode
\begin{enumerate}
\item Implement the method  \li{FacialRec.project(s)} as follows.
\begin{lstlisting}
def project(self,s):
        # Return U_s^T
\end{lstlisting}
\item (Optional) Let \li{face} be the first mean-shifted face from the database (the first column of \li{facialRec.Fbar}). Do the following:
\begin{enumerate}
\item Project \li{face} onto the subspace spanned by the first 75 eigenfaces.
\item Change basis back to the standard basis on $\mathbb{R}^{mn}$.
\item Add back the mean face \li{facialRec.mu}.
\item Plot the resulting image.
\end{enumerate}
Your image should match Figure \ref{fig:75_efaces}.
\end{enumerate}
\end{problem}

\begin{figure}

\begin{subfigure}[b]{0.3\textwidth}
\includegraphics[width=\textwidth]{rebuiltThirtySecond.png}
\caption{5 eigenfaces, about 1/32 of the total.}
\label{fig:5_efaces}
\end{subfigure}
\begin{subfigure}[b]{0.3\textwidth}
\includegraphics[width=\textwidth]{rebuiltSixteenth.png}
\caption{9 eigenfaces, or 1/16 of the total.}
\label{fig:9_efaces}
\end{subfigure}
\begin{subfigure}[b]{0.3\textwidth}
\includegraphics[width=\textwidth]{rebuiltEighth.png}
\caption{19 eigenfaces, about 1/8 of the total.}
\label{fig:19_efaces}
\end{subfigure}
\begin{subfigure}[b]{0.3\textwidth}
\includegraphics[width=\textwidth]{rebuiltFourth.png}
\caption{38 eigenfaces, about 1/4 of the total.}
\label{fig:38_efaces}
\end{subfigure}
\begin{subfigure}[b]{0.3\textwidth}
\includegraphics[width=\textwidth]{rebuiltHalf.png}
\caption{75 eigenfaces, about 1/2 of the total.}
\label{fig:75_efaces}
\end{subfigure}
\begin{subfigure}[b]{0.3\textwidth}
\includegraphics[width=\textwidth]{rebuiltAll.png}
\caption{All 153 of the eigenfaces.}
\label{fig:all_efaces}
\end{subfigure}
\caption{Image rebuilt with various numbers of eigenfaces. The image is somewhat recognizable when it is reconstructed with only 1/8 of the eigenfaces.}
\label{fig:rebuiltImage}
\end{figure}

\begin{comment}
Of course, moving to a lower-dimensional subspace by using only the top $n$ eigenfaces introduces some error, in the sense that we cannot perfectly
reconstruct the face images from their coordinate vectors.
However, this error is tolerable, provided $n$ is not \emph{too} small.
Note that we can approximately reconstruct $\bar{f}_i$ from the coordinate vector $\hat{f}_i$ by
\[
\bar{f}_i \approx U_n\hat{f}_i.
\]
See Figure \ref{facialRecognition:rebuiltImage} for examples of reconstructed face images giving different numbers of eigenfaces.
\end{comment}

\section*{Recognizing faces}
Finally, we are ready to identify which mean-shifted image $\bar{\f}_i$ is closest to $\bar{\g}$.
We begin by projecting all vectors to some $s$-dimensional subspace and writing them in terms of an orthonormal basis for that subspace.
This is accomplished with multiplication by $U_s^T$:
\[
\widehat{\f}_i = U_s^T(\f_i-{\boldsymbol \mu}) \qquad \widehat{\g} = U_s^T(\widehat{\g}-{\boldsymbol \mu}).
\]

Next, we compute which $\widehat{\f}_i$ is closest to $\widehat{\g}$.
Since the columns of $U_s$ are an orthonormal basis, we get the same result doing the computation in this basis as we would in the standard Euclidean basis.
Define
\[
i^* = \text{argmin}_i \|\widehat{\f}_i - \widehat{\g}\|_2.
\]
Then the $i^*$-th face image is the best match for $\g$.

\begin{problem}
\label{prob:nearest}
\leavevmode
\begin{enumerate}
\item
Implement the method \li{FacialRec.findNearest()} as follows.
\begin{lstlisting}
def findNearest(self, image, s=38):
    Ut = self.project(s)
    ghat = # Shift `image' by the mean and project, producing g-hat as defined above
    Fhat = # Project Fbar, producing a matrix whose columns are the f-hat defined above

    # Return the index that minimizes || fhat_i - ghat ||_2.
\end{lstlisting}
The functions \li{la.norm()} and \li{np.argmin()} will be useful for the last line.

\item Test your facial recognition system on faces selected randomly from the \li{faces94} dataset.
The function \li{sampleFaces(n_tests, path)} at the end of this lab will build an array of \li{n_tests} random faces from the \li{faces94} database.

Plot the random face beside the face returned by your facial recognition code to see if your system is accurately recognizing faces.
The function \li{show2()} at the end of this lab will plot two face vectors side-by-side.

\end{enumerate}
\end{problem}

By this point, you have created a basic facial recognition system.
We can extend the system to detect when a face doesn't match anything currently in the database, and then add this new face into the database.
We can also make the system more robust by including multiple pictures of the same face with different expressions and lighting conditions.

Although there are other approaches to facial recognition that utilize more complex techniques, the method of eigenfaces remains
a wonderfully simple and effective solution, illustrating another application of the singular value decomposition.

\section*{Appendix: Helper Code}
This section contains some functions to help you code up the facial recognition class outlined in the problems of this lab.

\begin{lstlisting}
def getFaces(path="./faces94"):
    """
    Traverse the directory specified by `path' and return an array containing
    one column vector per subdirectory.

    For the faces94 dataset, this gives an array with just one column for each
    face in the database. Each column corresponds to a flattened grayscale image.
    """

    # Traverse the directory and get one image per subdirectory
    faces = []
    for (dirpath, dirnames, filenames) in walk(path):
        for f in filenames:
            if f[-3:]=="jpg": # only get jpg images
                # load image, convert to grayscale, flatten into vector
                face = imread(dirpath+"/"+f).mean(axis=2).ravel()
                faces.append(face)
                break

    # put all the face vectors column-wise into a matrix
    F = np.array(faces).T
    return F

def show(im, w=200, h=180):
    """
    Plot the flattened grayscale image `im' of width `w' and height `h'
    """

    plt.imshow(im.reshape((w,h)), cmap=cm.Greys_r)
    plt.show()

def sampleFaces(n_tests,path = "./faces94")
    """
    Return an array containing a sample of n_tests images contained
    in the path as flattened images in the columns of the output
    """
    files = []
    for (dirpath, dirnames, filenames) in walk(path):
        for f in filenames:
            if f[-3:]=="jpg": # only get jpg images
            files.append(dirpath+"/"+f)

    #Get a sample of the images
    test_files = sample(files, n_tests)
    #Flatten and average the pixel values
    images = np.array([imread(f).mean(axis=2).ravel() for f in test_files]).T
    return images

def show2(im1, im2, w=200, h=180):
    """
    Convenience function for plotting two flattened grayscale images of
    the specified width and height side by side
    """
    plt.subplot(121)
    plt.imshow(im1.reshape((w,h)), cmap=cm.Greys_r)
    plt.subplot(122)
    plt.imshow(im2.reshape((w,h)), cmap=cm.Greys_r)
    plt.show()

\end{lstlisting}