\documentclass[11pt]{article}
\usepackage{geometry}
\geometry{a4paper,left=2.5cm,right=2.5cm,top=2.5cm,bottom=2.5cm}
\usepackage{natbib}
\usepackage{color}
\definecolor{mygreen}{RGB}{28,172,0} % color values Red, Green, Blue
\definecolor{mylilas}{RGB}{170,55,241}
\usepackage{epsfig}
\usepackage{amssymb,amsmath}
\usepackage{enumerate}
\usepackage{enumitem}
\usepackage[utf8]{inputenc}
\usepackage{hyperref}
\usepackage{mathtools}

\newcommand{\ssd}{\text{ssd}}
\newcommand{\sS}{\mathsf{S}}
\newcommand{\tot}{\text{tot}}

\begin{document}

\input{symbols}

\section*{Layered quasigeostrophic model}

The $\nmax$-layer quasigeostrophic (QG) potential vorticity is

\begin{align}
{q_1} &= \lap\psi_1 + \frac{f_0^2}{H_1} \left(\frac{\psi_{2}-\psi_1}{g'_{1}}\right)\,,  \qquad & i =1\com \nonumber \\
{q_n} &= \lap\psi_n + \frac{f_0^2}{H_n} \left(\frac{\psi_{n-1}-\psi_n}{g'_{n-1}}  - \frac{\psi_{i}-\psi_{n+1}}{g'_{i}}\right)\,,  \qquad &i = 2,\nmax-1 \com \nonumber \\
{q_\nmax} &= \lap\psi_\nmax + \frac{f_0^2}{H_\nmax} \left(\frac{\psi_{\textsf{N}-1}-\psi_\nmax}{g'_{\nmax-1}}\right) + \frac{f_0}{H_\nmax}h_b (x,y)\,,  \qquad & i =\nmax\,,
\end{align}
where $q_n$ is the n'th layer QG potential vorticity, and $\psi_n$ is the streamfunction,
 $f_0$ is the inertial frequency, n'th $H_n$ is the layer depth, and $h_b$ is the
bottom topography. (Note that in QG $h_b/H_\nmax << 1$.) Also the n'th buoyancy
jump (reduced gravity) is
\begin{equation}
g'_n \equiv g \frac{\rho_{n}-\rho_{n+1}}{\rho_n}\com
\end{equation}
where $g$ is the acceleration due to gravity and $\rho_n$ is the layer density.

The dynamics of the system is given by the evolution of PV. In particular, assuming a background
flow with background velocity $\vec{V} = (U,V)$ such that
\begin{align}
\label{eq:Uequiv}
u_n^{{\tot}} = U_n - \psi_{n y}\com \nonumber \\
v_n^{\tot} = V_n + \psi_{n x} \com
\end{align}
and
\begin{equation}
q_n^{\tot} = Q_n + \delta_{n\nmax}\frac{f_0}{H_\nmax}h_b + q_n \com
\end{equation}
where $Q_n + \delta_{n\nmax}\frac{f_0}{H_\nmax}h_b$ is n'th layer background PV,
we obtain the evolution equations
\begin{align}
\label{eq:qg_dynamics}
{q_n}_t + \mathsf{J}(\psi_n,q_n + \delta_{n \nmax} \frac{f_0}{H_\nmax}h_b )& + U_n ({q_n}_x + \delta_{n \nmax} \frac{f_0}{H_\nmax}h_{bx}) + V_n ({q_n}_y + \delta_{n \nmax} \frac{f_0}{H_\nmax}h_{by})+ \nonumber
\\ & {Q_n}_y {\psi_n}_x - {Q_n}_y {\psi_n}_y = \ssd
- r_{ek} \delta_{n\nmax} \lap \psi_n \com \qquad n = 1,\nmax\com
\end{align}
where $\ssd$ is
stands for small scale dissipation, which is achieved by an spectral exponential filter
or hyperviscosity, and $r_{ek}$ is the linear bottom drag coefficient. The Dirac delta,
$\delta_{nN}$, indicates that the drag is only applied in the bottom layer.

\subsection*{Equations in spectral space}

The evolutionary equation in spectral space is

\begin{align}
    \hat{q}_{nt} + (\mathrm{i} k U + \mathrm{i} l V) \left(\hat{q}_n + \delta_{n \nmax} \frac{f_0}{H_\nmax}\hat{h}_b\right) + (\mathrm{i} k\, {Q_y} -  - \mathrm{i} l\,{Q_x}){\hat{\psi}_n} + \mathsf{\hat{J}}(\psi_n, q_n + \delta_{n \nmax} \frac{f_0}{H_\nmax}h_b )   \nonumber \\ =  \ssd + \mathrm{i}  \delta_{n \nmax} r_{ek} \kappa^2 \hat{\psi}_n \,, \qquad i = 1,\textsf{N}\com
\end{align}
where $\kappa^2 = k^2 + l^2$. Also, in the pseudo-spectral spirit we write the transform of the nonlinear
terms and the non-constant coefficient linear term as the transform of the products, calculated in physical space, as opposed to double convolution sums.  That is $\mathsf{\hat{J}}$ is the Fourier transform of Jacobian computed in physical space.

The inversion relationship is

\begin{equation}
    \hat{q}_i = {\left(\sS - \kappa^2 \sI \right)} \hat{\psi}_i\com
\end{equation}
where $\sI$ is the $\nmax\times\nmax$ identity matrix, and the stretching matrix is

\begin{equation}
\textsf{S} \equiv  f_0^2
\begin{bmatrix}
    -\frac{1}{g'_1 H_1}& & \frac{1}{g'_1 H_1} &  & 0 \dots& \\
 & 0 & & & & &\\
    \vdots & \ddots& &\ddots &\ddots & & & &\\
       & \frac{1}{g'_{i-1} H_i}& &  -\left(\frac{1}{g'_{i-1} H_i} + \frac{1}{g'_{i} H_i}\right)& & \frac{1}{g'_{i} H_i}\,\,\,\,\,\,\, \\
       & \ddots& & \ddots &\ddots & & & &\\
& & & & & \\
& \dots & 0 & \frac{1}{ g'_{\nmax-1} H_\nmax}& & -\frac{1}{g'_{\nmax-1} H_\nmax}
\end{bmatrix}
\per
\end{equation}

\subsection*{Energy spectrum}
The equation for the energy spectrum,
\begin{equation}
E(k,l) \equiv {\frac{1}{2 H}\sum_{i=1}^{\nmax} H_i \kappa^2 |\hat{\psi}_i|^2} \,\,\,\,+ \,\,\,\,\,\, {\frac{1}{2 H} \sum_{i=1}^{\nmax-1} \frac{f_0^2}{g'_i}|\hat{\psi}_{i}- \hat{\psi}_{i+1}|^2}\,\,\,\,,
\end{equation}
is

\begin{align}
    \frac{d}{dt} E(k,l) = {\frac{1}{H}\sum_{i=1}^{\mathsf{N}} H_i \text{Re}[\hat{\psi}_i^\star {\mathsf{\hat{J}}}(\psi_i,\nabla^2\psi_i)]} +
    {\frac{1}{H}\sum_{i=1}^{\mathsf{N}} H_i\text{Re}[\hat{\psi}_i^\star \hat{\mathsf{J} (\psi_i,(\sS \psi)_i)}]} \nonumber \\
    + {\frac{1}{H}\sum_{i=1}^{\mathsf{N}} H_i ( k U_i +  l V_i)\, \text{Re}[i \, \hat{\psi}^\star_i (\mathsf{S}\hat{\psi}_i)]} \,\,\,\,\,\,\,{- r_{ek} \frac{H_\mathsf{N}}{H} \kappa^2 |\hat{\psi}_{\mathsf{N}}|^2}  +{ {{E_\ssd}}} \com
\end{align}
where $\star$ stands for complex conjugation, and the terms above on the right represent, from
left to right,

\begin{description}
    \item[I:]  The spectral divergence of the kinetic energy flux;
    \item[II:] The spectral divergence of the potential energy flux;
    \item[III:] The spectrum of the potential energy generation;
    \item[IV:] The spectrum of the energy dissipation by linear bottom drag;
    \item[V:] The spectrum of energy loss due to small scale dissipation.
\end{description}
We assume that $V$ is relatively small, and that, in statistical steady state, the
budget above is dominated by I through IV.

\subsection*{Enstrophy spectrum}
Similarly the evolution of the barotropic enstrophy spectrum,

\begin{equation}
Z(k,l) \equiv \frac{1}{2H} \sum_{i=1}^{\nmax} H_i |\hat{q}_i|^2\com
\end{equation}
is governed by
\begin{equation}
    \frac{d}{d t} Z(k,l) = {\text{Re}[\hat{q}_i^\star {\mathsf{\hat{J}}(\psi_i,q_i) ]}}
    {-(k Q_y - l Q_x)\text{Re}[(\sS \hat{\psi}_i^\star)\hat{\psi}_i]}
    + { {\hat{Z_\ssd}}}\com
\end{equation}
where the terms above on the right represent, from
left to right,
\begin{description}
    \item[I:]   The spectral divergence of barotropic potential enstrophy flux;
    \item[II:]  The spectrum of  barotropic potential enstrophy generation;
    \item[III:] The spectrum of  barotropic potential enstrophy loss due to small scale dissipation.
\end{description}
The enstrophy dissipation is concentrated at the smallest scales resolved in the model and, in statistical steady
state, we expect the budget above to be dominated by the balance between I and II.

\section*{Special case: two-layer model}
With $\nmax = 2$, an alternative notation for the perturbation of potential vorticities can be written as
\begin{align}
    q_1 &= \lap \psi_1 + F_1 (\psi_2 - \psi_1) \nonumber\\
    q_2 &= \lap \psi_2 + F_2 (\psi_1  - \psi_2)\com
\end{align}
where we use the following definitions
where
\begin{equation}
F_1 \equiv \frac{k_d^2}{1 + \delta^2}\,, \qquad \:\:\text{and} \qquad F_2 \equiv \delta \,F_1\,,
\end{equation}
with the deformation wavenumber
\begin{equation}
k_d^2 \equiv \, \frac{f_0^2}{g} \frac{H_1+H_2}{H_1 H_2} \per
\end{equation}
With this notation, the ``stretching matrix'' is simply
\begin{equation}
\sS = \begin{bmatrix}
- F_1 \qquad \:\:\:\:F_1\\
F_2 \qquad -  + F_2
\end{bmatrix}\per
\end{equation}
The inversion relationship in Fourier space is
\begin{equation}
\begin{bmatrix}
\hat{\psi}_1\\
\hat{\psi}_2\\
\end{bmatrix}
= \frac{1}{\text{det} \: \sB}
\begin{bmatrix}
-(\kappa^2 + F_2) \qquad \:\:\:\:-F_1\\
\:\:\:\: -F_2 \qquad - (\kappa^2 + F_1)
\end{bmatrix}
\begin{bmatrix}
\hat{q}_1\\
\hat{q}_2\\
\end{bmatrix}\com
\end{equation}
where
\begin{equation}
\qquad \text{det}\, \sB = \kappa^2\left(\kappa^2 + F_1 + F_2\right)\,.
\end{equation}

\end{document}


\documentclass{ecnreport}

\stud{Control \& Robotics Master}
\topic{Manipulator Modeling \& Control}

\begin{document}

  \inserttitle{Manipulator Modeling \& Control}

  \section{Content of this lab}

  The goal of this lab is to program in C++ the three fundamentals models for robot manipulators:
  \begin{itemize}
    \item Direct Geometric Model: what is the pose of the end-effector for a given joint configuration?
    \item Inverse Geometric Model: what joint values correspond to a given end-effector pose?
    \item Kinematic Model: what is the velocity screw of the end-effector when the joints move?
  \end{itemize}
  These models will be applied on three different robots, and used to perform basic point-to-point control.\\

  As it is not a lab on C++, most of the code is already written and you only have to fill the required functions:
  \begin{itemize}
    \item \texttt{Robot::init\_wMe()} to initialize the fixed transform between the wrist and end-effector
    \item \texttt{Robot::fMw(q)} for the direct geometric model wrist-to-fixed frame
    \item \texttt{Robot::inverseGeometry(M)} for...well, the inverse geometry
    \item \texttt{Robot::fJw} for the kinematic model wrist-to-fixed frame
  \end{itemize}

  \subsection{Installing and loading the lab in Qt Creator}

  This project uses the ROS\footnote{Robot Operating System, http://www.ros.org} framework which imposes some particular steps to configure the environment. An actual ROS course will be held in the second semester, for now just configure as follows:

  \begin{enumerate}
    \item Open a terminal and create (if needed) a ROS workspace: \texttt{mkdir -p $\sim$/ros/src}
    \item Go inside this folder \texttt{cd $\sim$/ros/src}
    \item Download this repository: \texttt{git clone https://github.com/oKermorgant/ecn\_manip}.
    \item Compile the workspace: \texttt{cd .. \&\& catkin build}
    \item Create the QtCreator configuration file: \texttt{cd src/ecn\_manip \&\& gqt}
    \item Open QtCreator and load the \texttt{$\sim$/ros/src/ecn\_manip/CMakeLists.txt} file
    \item The files should be displayed and ready to run
    \item Compilation is done by clicking the bottom-left hammer
    \item Run your program with the green triangle. It can be stopped by clicking on the red square
  \end{enumerate}

  \subsection{Expected work}

  The {\bf only} files to be modified are:
  \begin{itemize}
    \item \texttt{control.cpp}: main file where the control is done depending on the current robot mode
    \item \texttt{robot\_turret.cpp}: model of the RRP turret robot
    \item \texttt{robot\_kr16.cpp}: model of the industrial Kuka KR16 robot
    \item \texttt{robot\_ur10.cpp}: model of the Universal Robot UR-10
  \end{itemize}
  We will use the ViSP\footnote{Visual Servoing Platform, http://visp.inria.fr} library to manipulate mathematical vectors and matrices, including frame transforms, rotations, etc. The main classes are detailed in Appendix \ref{visp}.\\
  At the end of the lab, you should upload them through the \link{http://pagesperso.ls2n.fr/~kermorgant-o/student links.php}{portal on my website}

  \section{The robots}

  \def\wMe{{}^w\M_e}

  Three robots are proposed with increasing complexity. You should start with the Turret, then the Kuka KR16 then the UR-10.\\
  For each of these robots, the table of Modified Denavit-Hartenberg parameters should be defined. As we saw during the lectures, once you have the table then the Direct Geometric and Direct Kinematic Models can be almost automatically derived. A tool is provided in this package to generate C++ code for the Geometric and Kinematic models, see Appendix \ref{dhcode} for details on this tool.

  The fixed frame and the end-effector frame are imposed by the schematics.
  All intermediary frames have to be placed according to MDH convention, with a final fixed transform between the wrist and end-effector frames.

  \subsection{Turret RRP robot}

  \begin{minipage}{.8\linewidth}
    This is a very simple robot to begin the modeling, as shown in the figure.\\
    The fixed frame $\Frame{0}$ and end-effector frame $\Frame{e}$ are imposed, as well as the joint direction.\\
    The constant values for this model are: $b = 0.5$ m and $d = 0.1$ m.\\~\\
    All computation may be done first without using the Denavit-Hartenberg parameters as the robot is quite simple.
  \end{minipage}
  \begin{minipage}{.2\linewidth}
    \includegraphics{fig/turret}\label{turret}
  \end{minipage}

  \subsection{Kuka KR16 anthropomorphic robot}

  This robot is a classical 6R architecture with a spherical wrist, as shown in the next figure:

  \begin{figure}[h!]\centering
    \includegraphics[width=.6\linewidth]{fig/kr16}
    \caption{Model of the Kuka KR16 with imposed fixed and end-effector frames}
  \end{figure}

  The wrist frame is typically at the common point between joints 4, 5 and 6.
  The constant transform $\wMe$ between the wrist and the end-effector should include the $0.158$ distance and potential rotations.

  \subsection{Universal Robot UR-10 anthropomorphic robot}

  This robot is a less classical 6R architecture, without spherical wrist. A tool is also attached with an arbitrary end-effector offset.

  \begin{figure}[h!]\centering
    \includegraphics[width=.6\linewidth]{fig/ur10}
    \caption{Model of the UR-10 with imposed fixed and end-effector frames}
  \end{figure}

  The Inverse Geometry is given for this robot.
  The constant transform $\wMe$ between the wrist and the end-effector should include the $0.1922$ distance and potential rotations.

  \newpage
  \subsection{Running the simulation}

  Once everything is installed and compiled, the base simulation can be run from a terminal (only 1 line depending on the robot you work on):
  \cppstyle
  \begin{lstlisting}
  roslaunch ecn_manip turret.launch
  roslaunch ecn_manip kr16.launch
  roslaunch ecn_manip ur10.launch
  \end{lstlisting}

  This will display two windows:
  \begin{itemize}
    \item RViz is the 3D simulator to show the current configuration of the robot. It will also display the base and end-effector frames, and the one that you compute. You can thus easily check if the computation is fine.
    \item The other window is composed of 4 panels:
    \begin{itemize}
      \item Buttons at the top left to change the current exercise
      \item Sliders at the bottom left to manually control the robot (exercises 1 and 2)
      \item Sliders at the top to change the switching time of the position control (exercises 3+)
      \item A plotting region with two tabs, one for the operational space and one for the joint space. The joint space is displayed in a normalized fashion, depending on the joint limits (upper and lower dotted lines)
    \end{itemize}
  \end{itemize}

  \section{Building the models}

  \subsection*{The main code}

  In the file \texttt{control.cpp}, the \texttt{if - else if} blocks correspond to the 6 exercises to be done for each robot. Note that if they work for the Turret robot, then
  they should work for all robots as long as the model is correct. \\
  The current exercise is changed by using the buttons in the GUI.

  \subsection*{Exercise 1: Checking the Direct Geometric Model}

  In this exercise, the robot follows the manually given joint positions. The task is to build and print the direct geometric model, depending on the current value of $\q$. This is to be done in two functions:
  \begin{itemize}
    \item \texttt{Robot::init\_wMe()} where you have to write the constant matrix $\wMe$ from the end-effector to the wrist frame
    \item \texttt{Robot::fMw(q)}  where you have to write and return the transform $\M$ between the fixed and wrist frame
  \end{itemize}
  Once you have computed the \texttt{M} matrix, the code calls \texttt{robot->checkPose(M);} in order to print both your matrix and the true one. No need to tell that they should be equal.\\
  What can be told is that it is useless to continue until they are.

  \subsection*{Exercise 2: Checking the Direct Kinematic Model (Jacobian)}

  If you select \texttt{Manual Operational Velocity} in the GUI then the sliders allow you to give a desired operational velocity (ie twist) for the end-effector.\\
  This velocity is ${}^e\v_e$ and is defined in the end-effector frame.\\
  The robot Jacobian has to be defined in the function \texttt{Robot::fJw(q)} that should return the Jacobian of the wrist in the fixed frame.\\

  In the main code, the commanded velocity then to be mapped to the joint velocity in two steps:
  \begin{enumerate}
    \item Map to the fixed frame: ${}^f\mathbf{v}_e^* = \left[\begin{array}{cc}{}^f\R_e & 0 \\ 0 & {}^f\R_e\end{array}\right]{}^e\mathbf{v}_e^*$\\
    Call \texttt{M.getRotationMatrix()} to get this rotation.\\
    The \texttt{ecn::putAt} function may be of good use.
    \item Map to the joint space: $\dot\q = \J^+.{}^f\mathbf{v}_e^*$\\
    Call \texttt{robot->fJe(q).pseudoInverse()} to get the Jacobian pseudo-inverse
  \end{enumerate}
  You can check that the end-effector is able to follow straight 3D lines in x, y or z direction in its own frame.
  Similarly, it should be able to rotate around x, y or z. This means the Jacobian computation is correct.

  \newpage
  \subsection*{Exercise 3: Checking the Inverse Geometric Model}

  If you select \texttt{Direct point-to-point} in the GUI then the robot will receive two operational setpoints, at a given sampling time that can be changed with the \texttt{Switching time} slider in the GUI. The current setpoint is in the matrix \texttt{Md} and its corresponding joint position is in the vector \texttt{qf}.

  Nothing has to be done in the main code, but of course the \texttt{Robot::inverseGeometry(M)} function has to be filled in order to compute the actual inverse geometry solution. The robot will try to have all joints reach their desired value as soon as possible, which will lead to a discontinuous motion.\\

  In practice, this exercise is to check that the Inverse Geometric Model is correct: the robot should reach the desired pose.\\

  When computing the inverse geometry, try to find a solution as near as possible to the passed \texttt{q0}.\\

  Many useful functions are available to solve classical trigonometric equations. They are detailed in Appendix \ref{trigsolve}.\\

  A given potential solution should be stored using \texttt{addCandidate(\{q1, q2, ..., qn\});}\\ At the end of the Inverse Geometry function, use \texttt{return bestCandidate(q0);} that will pick the candidate that is the nearest from the current joint position and also within joint limits.

  \subsection*{Exercise 4: Interpolated point-to-point control}

  In this exercise the previous pose setpoint \texttt{M0} and its corresponding joint position \texttt{q0} should be used to interpolate the joint positions.\\

  Here the goal is to compute the current setpoint $\q_c = \q_0 + p(t)(\q_f-\q_0)$. The interpolation function $p(t)$ was defined during the lectures and should take into account the maximum joint velocity and acceleration that are stored in the \texttt{vMax} and \texttt{aMax} vectors. Basically this amounts to computing the minimum time \texttt{tf} needed to reach $\q_f$ from $\q_0$. This computation should be done in the \texttt{if(robot->newRef())} and then used to compute the current \texttt{qCommand}.

  \subsection*{Exercise 5: Operational control through Inverse Geometric Model}

  Exercises 3 and 4 lead to the correct pose but not in a straight 3D line. Indeed the robot is only controlled in the joint space with no constraints between the two extreme poses.\\
  In this exercise, the goal is to perform the interpolation not in the joint space but in the Cartesian space. In practice, we will compute many poses between
  \texttt{M0} and \texttt{Md} and command the robot to reach sequentially all these poses. As they will be pretty close one from each other, the resulting motion
  will be close to a straight line.

  The \texttt{robot->intermediaryPose(M0, Md, alpha)} function returns a pose between \texttt{M0} and \texttt{Md}, where \texttt{alpha} is between 0 and 1 and should be computed from $t$, $t_0$ and $t_f$ = 1 s.

  Then, the inverse geometry of this pose should be sent to the robot as joint position setpoint.

  \newpage
  \subsection*{Exercise 6: Operational control through Jacobian}

  In this exercise the goal is still to follow a straight line between the two points, but this time by sending a joint velocity command.\\

  As seen in class, this command should make the end-effector approach its desired pose. The steps are:
  \begin{enumerate}
    \item Compute the pose error in the desired frame: ${}^{e*}\M_e = {}^{f}\M_{e*}^{-1}.{}^{f}\M_e$
    \begin{itemize}
      \item in the code, ${}^{f}\M_{e*}$ corresponds to \texttt{Md} and ${}^{f}\M_e$ to \texttt{M}
      \item In practice we use the $(\mathbf{t}, \theta\u)$ representation with \texttt{p.buildFrom()}
    \end{itemize}
    \item Compute the desired linear and angular velocities: $v = -\lambda{}^{f}\M_{e*}\mathbf{t}$, \quad $\omega = -\lambda{}^{f}\M_{e} (\theta\u)$
    \item Map these velocities to the joint space: $\dot\q = \J^+ \left[\begin{array}{c}v\\\omega\end{array}\right]$
  \end{enumerate}
  $\lambda$ is a gain that can be changed from the GUI. Increasing it will lead to a faster convergence. It can be obtained through \texttt{robot->lambda()}.

  \appendix

  \newpage

  \section{Using ViSP}\label{visp}

  ViSP is a library for Visual Servoing (wait for the M2 Robotics to know more!) and also includes all necessary tools to play with mathematical vectors, matrices and 3D transforms.
  The full documentation is here: \link{http://visp-doc.inria.fr/doxygen/visp-3.1.0/classes.html}{http://visp-doc.inria.fr/doxygen/visp-3.1.0/classes.html}.\\
  The main classes to be used are:
  \begin{itemize}
    \item \texttt{vpMatrix}: a classical matrix of given dimensions, can be multiplied with other matrices and vectors if the dimensions are ok. The pseudo-inverse of matrix \texttt{M}
    is available with \texttt{M.pseudoInverse()}
    \item \texttt{vpColVector}: a column vector of given dimension
    \item \texttt{vpHomogeneousMatrix}: the classical $4\times 4$ 3D transform matrix
  \end{itemize}
  Most of the model computation is about initializing a matrix or vector to correct dimensions (which is already done) and fill its elements with formulae.\\
  To write 1 at element (i,j) of matrix M, just write \texttt{M[i][j] = 1;}\\
  Similarly, to write 1 at element (i) of vector q, just write \texttt{q[i] = 1;}\\

  3D transforms can be easily changed between homogeneous matrix, rotation matrix, translation vector or $\theta\u$ vector. Another type called a pose vector
  consists in the full $(\mathbf{t}, \theta\u)$ vector of dimension 6:
  \cppstyle
  \begin{lstlisting}
  vpHomogeneousMatrix M;
  vpPoseVector p;
  vpRotationMatrix R;
  vpTranslationVector t;
  vpColVector tu(3);

  M.getRotationMatrix()*t;	// extract the rotation part from M and multiply with t
  p.buildFrom(M);			// build (t, theta-u) from M
  t = M.getTranslationVector();	// extract the translation part

  // copy the theta-u part of p into tu
  for(int i = 0; i < 3; ++i)
  tu[i] = p[i+3];

  // compute inverse transform
  M = M.inverse();
  \end{lstlisting}

  Two utility functions are provided to put a matrix or vector into a bigger one:
  \begin{itemize}
    \item \texttt{void ecn::putAt(M, Ms, r, c)}: Writes matrix \texttt{Ms} inside matrix \texttt{M}, starting from given row and column.
    \item \texttt{void ecn::putAt(V, Vs, r)}: Writes column vector \texttt{Vs} inside vector \texttt{V}, starting from given row. These two functions do nothing but print an error
    if the submatrix or vector does not fit in the main one.
  \end{itemize}

  \newpage
  \section{Code generation from Modified Denavit-Hartenberg parameters}\label{dhcode}

  The core of robot modeling is to build the MDH table. From this, the DGM and KM can be derived automatically. The Inverse Geometry still has to be done by hand (even if some advanced tools allow code-generation for the IG resolution).

  As it is quite tedious to have to compute all matrices when a solution of MDH is investigated, a tool is provided to generate the C++ code. An example can be found in the file \texttt{dh\_example.yml}:
  \cppstyle
  \begin{lstlisting}
  notation: [alpha, a, theta, r]
  joint:
  1: [0, 0, q1, 0]
  2: [-pi/2, 0, 0, q2]
  3: [pi/2, 0, q3, r3]
  4: [-pi/2, a4, q4+pi/2, 0]
  \end{lstlisting}
  The command to be run is: \texttt{rosrun ecn\_manip dh\_code.py <name of the file>}.\\
  In the case of the above MDH table, it leads to these outputs:

  \begin{minipage}{.45\linewidth}
    \cppstyle \raggedright
    \begin{lstlisting}
    // Generated pose code
    const double c1 = cos(q[0]);
    const double c1p3 = cos(q[0]+q[2]);
    const double c4 = cos(q[3]);
    const double s1 = sin(q[0]);
    const double s1p3 = sin(q[0]+q[2]);
    const double s4 = sin(q[3]);
    M[0][0] = -s4*c1p3;
    M[0][1] = -c4*c1p3;
    M[0][2] = -s1p3;
    M[0][3] = a4*c1p3 - q[1]*s1;
    M[1][0] = -s4*s1p3;
    M[1][1] = -s1p3*c4;
    M[1][2] = c1p3;
    M[1][3] = a4*s1p3 + q[1]*c1;
    M[2][0] = -c4;
    M[2][1] = s4;
    M[2][2] = 0;
    M[2][3] = r3;
    M[3][0] = 0;
    M[3][1] = 0;
    M[3][2] = 0;
    M[3][3] = 1.;
    // End of pose code
    \end{lstlisting}
  \end{minipage}
  \begin{minipage}{.1\linewidth}
    \quad\quad
  \end{minipage}
  \begin{minipage}{.45\linewidth}
    \cppstyle \raggedright
    \begin{lstlisting}
    // Generated Jacobian code
    const double c1 = cos(q[0]);
    const double c1p3 = cos(q[0]+q[2]);
    const double s1 = sin(q[0]);
    const double s1p3 = sin(q[0]+q[2]);
    J[0][0] = -a4*s1p3 - q[1]*c1;
    J[0][1] = -s1;
    J[0][2] = -a4*s1p3;
    //J[0][3] = 0;
    J[1][0] = a4*c1p3 - q[1]*s1;
    J[1][1] = c1;
    J[1][2] = a4*c1p3;
    //J[1][3] = 0;
    //J[2][0] = 0;
    //J[2][1] = 0;
    //J[2][2] = 0;
    //J[2][3] = 0;
    //J[3][0] = 0;
    //J[3][1] = 0;
    //J[3][2] = 0;
    J[3][3] = -s1p3;
    //J[4][0] = 0;
    //J[4][1] = 0;
    //J[4][2] = 0;
    J[4][3] = c1p3;
    J[5][0] = 1.;
    //J[5][1] = 0;
    J[5][2] = 1.;
    //J[5][3] = 0;
    // End of Jacobian code
    \end{lstlisting}
  \end{minipage}
  This is exactly what you would get by doing the manual computation from the initial table, which I hope is quite appreciated.\\

  Fixed transforms for initial and end-effector frames can also be written with keys \texttt{f:} and \texttt{e:}\\

  If passing \texttt{--display} the script will display the direct geometry with classical math notations. It can be useful for the inverse geometry computation.\\
  For a 6-DOF robot, passing \texttt{--wrist} will display useful information to benefit from a spherical wrist: math expressions for $\Pass{0}{\mathbf{t}}{6}(q_1,q_2,q_3)$ and  $\Pass{3}{\R}{6}(q_4,q_5,q_6)$, and code for $\Pass{0}{\R}{3}(q_1,q_2,q_3)$.

  \newpage
  \section{Trigonometric solvers}\label{trigsolve}

  The code contains solvers for all types of trigonometric equations (except type 1 that is obvious).\\
  They take as argument the coefficients of the equations. They return:
  \begin{itemize}
    \item A \texttt{std::vector<double>} for equations of 1 unknown (types 2 \& 3).\\We can go through the valid solutions with:
    \cppstyle
    \begin{lstlisting}
    // assume q1 should ensure type 2: X.sin(q1) + Y.cos(q1) = Z

    for(double q1: solveType2(X, Y, Z))
    {
    // here q1 is a valid solution, may be used to deduce other joint values
    }
    \end{lstlisting}
    \item A \texttt{std::vector<JointSolution>} for equations of 2 unknowns (types 4 to 8).\\In this case, the valid solutions can be explored with:
    \cppstyle
    \begin{lstlisting}
    // assume q1 and q2 should ensure type 6 equation:
    //		W.sin(q2) = X.cos(q1) + Y.sin(q1) - Z1
    //		W.cos(q2) = X.sin(q1) - Y.cos(q1) - Z1

    for(auto qs: solveType6(X, Y, Z1, Z2, W))
    {
    // here a (q1,q2) solution is available in qs.qi and qs.qj
    }
    \end{lstlisting}
  \end{itemize}

  Furthermore, a function allows retrieving the 12 elements of $\Pass{0}{\M}{n*}$ from the matrix $\Pass{f}{\M}{e*}$ given to the inverse geometry solver:
  \begin{cppcode}
    const auto [xx,xy,xz,yx,yy,yz,zx,zy,zz,tx,ty,tz] = explodeMatrix(fMe_des);
\end{cppcode}
This function output the matrix terms according to:
\begin{equation*}
\Hom{0}{n*} = \Hom{0}{f}\Hom{f}{e*}\Hom{e}{n} = \left[\begin{matrix}x_x & y_x & z_x & t_x\\x_y & y_y & z_y & t_y\\x_z & y_z & z_z & t_z\\0 & 0 & 0 & 1\end{matrix}\right]
\end{equation*}

  \newpage
  Below are listed the syntax for all trigonometric solvers:\\

  %\setlength\extrarowheight{5pt}
  \renewcommand{\arraystretch}{2.2}
  \begin{tabular}{|c|c|C{7cm}|C{2cm}|}\hline
    Type & Equation & Syntax & Returns (vector of)\\\hline
    1 & $Xq_i = Y$& does not exist &$q_i$	\\\hline
    2 & $Xs_i+Yc_i = Z$ & \texttt{solveType2(x, y, z)}&$q_i$\\\hline
    3 & $\begin{array}{l}X_1s_i+Y_1c_i = Z_1 \\ X_2s_i+Y_2c_i = Z_2\end{array}$&\texttt{solveType3(x1, y1, z1,
      x2, y2, z2)} &$q_i$\\\hline
    4 & $\begin{array}{l}X_1q_js_i = Y_1 \\ X_2q_jc_i = Y_2\end{array}$& \texttt{solveType4(x1, y1, x2, y2)}&$(q_i,q_j)$ \\\hline
    5 & $\begin{array}{l}X_1s_i = Y_1+Z_1q_j \\ X_2c_i = Y_2+Z_2q_j\end{array}$& \texttt{solveType5(x1, y1, z1,
      x2, y2, z2)}&$(q_i,q_j)$\\\hline
    6 & $\begin{array}{l}Ws_j = Xc_i + Ys_i+Z_1 \\ Wc_j = Xs_i - Yc_i+Z_2\end{array}$& \texttt{solveType6(x, y,
      z1, z2, w)}&$(q_i,q_j)$\\\hline
    7 & $\begin{array}{l}W_1c_j +W_2s_j = Xc_i + Ys_i+Z_1 \\ W_1s_j -W_2c_j = Xs_i - Yc_i+Z_2\end{array}$& \texttt{solveType7(x, y, z1, z2, w1, w2)}&$(q_i,q_j)$\\\hline
    8 &$\begin{array}{l}Xc_i+Yc_{ij} =Z_1 \\ Xs_i+Ys_{ij} =Z_2\end{array}$ & \texttt{solveType8(x, y, z1, z2)}&$(q_i,q_j)$\\\hline
  \end{tabular}

\end{document}


%!TEX root = ../PhD_thesis__Lilian_Besson
% ******************************* Thesis Appendix A ****************************
\chapter{Additional Mathematical Tools}
\label{app:1:mathsTools}

In this appendix, we present some mathematical tools that are used in various proofs in this thesis.

% ----------------------------------------------------------------------------
\section{Concentration Inequalities}
\label{sec:app1:ConcentrationInequalities}

\TODOL{I probably need to include Hoeffding's inequality, Chernoff's method etc?}

Reference: \cite{Boucheron2013} and maybe the original paper by Hoeffding, Chernoff etc.

% ----------------------------------------------------------------------------
\section{Useful lemmas}
\label{sec:app1:UsefulLemmas}

\TODOL{I probably need to include a bunch of useful lemmas, used in various places in the thesis, like Pinsker's inequality, etc.}

% ----------------------------------------------------------------------------
\section{Results about klUCB}
\label{sec:app1:resultsklUCB}

\TODOL{I probably need to include some results about klUCB, mainly coming from \cite{KLUCBJournal}, used in various places in the thesis?}


\documentclass[11pt]{article}

\usepackage{alltt,fullpage,graphics,color,epsfig,amsmath, amssymb}
\usepackage{hyperref}
\usepackage{boxedminipage}
\usepackage[ruled,vlined]{algorithm2e}
\usepackage{mathtools}
\usepackage{amsmath}
\DeclareMathOperator*{\argmax}{arg\,max}
\DeclareMathOperator*{\argmin}{arg\,min}
\newcommand\myeq{\stackrel{\mathclap{\normalfont\mbox{rank}}}{=}}
\newcommand{\floor}[1]{\lfloor #1 \rfloor}
\newcommand{\ceil}[1]{\lceil #1 \rceil}

\title{CS 510 Assignment 2}
\author{Daniel Campos}
\date{March 14th,2021}
\begin{document}
\maketitle
\section{Probabilistic Retrieval Model}
\subsection{Multinominal Ranking}
\subsubsection{Show that ranking if a document is relevant to a query is equivalent to the sum of the probability of each word being relevant to the query}
First off, recapping the RSJ model where $score(Q,D) \myeq  \sum_{i=1, d_i=q_i=1}^k \log \frac{p_i(1-q_i)}{q_i(1-p_i)} $. We know that a multi-variate Bernoulli models focuses on the presence and absence of a feature where for IR this feature is occurrence in the document. We also know that a Multinomial model leverages the number of counts of a feature, commonly referred to as term frequency. Essentially, a Bernoulli model is the same as a multinomial model where all frequencies have been simplified to 1. Thus the first step is to introduce the term frequency into the calculation which gives $score(Q,D) \myeq  \sum_{i=1, d_i=q_i=1}^k c(w,D) \log \frac{p_i(1-q_i)}{q_i(1-p_i)} $. We then amplify the summation from all terms in the query present in the document ($\sum_{i=1, d_i=q_i=1}^k$) to all the words in the vocabulary ($\sum_{w \in V}$) since we can no longer just focus on term occurrence but now have to focus on frequency. Since we have now are modeling the frequency of a feature instead of its absence/existence we drop the normalization of $(1-q_i)$ and $(1-p_i)$. Thus we arrive at our target formula $score(Q,D) \myeq  \sum_{w \in V} c(w,D) \log \frac{p(w|Q, R=1)}{p(w|Q, R=0)} $
\subsubsection{How many parameters are in such a model}
2: $p(w|Q, R=1)$ for $w \in V$, and $p(w|Q, R=0)$ for $w \in V$
\subsection{Give the formula for Maximum likelihood estimate of $p(w | Q, R= 0)$}
$p(w | Q, R=0) = \sum_{D \in C} \frac{tf_w}{|D|}$ where $tf_w$ is the amount of times a word $w$ occurs in document $D$ and $|D|$ is the document length.
\subsection{Give the formula for Maximum likelihood estimate of $p(w | Q, R= 1)$ where we use the query as the only relevant document}
$p(w | Q, R=1) = \frac{tf_w}{|Q|}$ where $tf_w$ indicates the occurrence of word $w$ in the query $Q$ and $|Q|$ represents the query length.
\subsection{Give the formula for smoothing the maximum likelihood estimate using Jelinek-Mercer with the collection language model}
$p(w | Q, R=1) = (1- \beta) \frac{tf_w}{|Q|}+ \beta p(w|C)$ where $tf_w$ indicates the occurrence of word $w$ in the query $Q$ and $|Q|$ represents the query length, $\beta$ is a Jelinek-Mercer smoothing coefficient and $p(w|C)$ is the language model unigram probability.
\subsection{Write Down the retrieval function. Does the retrieval function capture TF, IDF, document length normalization?}
$score(Q,D) \myeq  \sum_{w \in V} c(w,D) \log \frac{(1- \beta) \frac{tfq_w}{|Q|}+ \beta p(w|C)}{\sum_{D \in C} \frac{tfd_w}{|D|}} $ where $tfq_w$ is the term frequency in the query $Q$ and $tfd_w$ is the terfm frequency in document $D$, $|Q|$ represents the query length,$|D|$ is the document length, $\beta$ is a Jelinek-Mercer smoothing coefficient and $p(w|C)$ is the language model unigram probability. \\
This captures TF because we take the term probability and multiply it by the frequency, it captures IDF because we normalize the probability of the word in Query given how common the word is in the corpus and it captures document length normalization because each of our estimations normalize frequency by query/document length.
\section{Language Models}
\subsection{Show that if we use query likelihood scoring method with Jelinker-Mercer smoothing function we can rank documents with \\$score(Q,D) \myeq \sum_{w \in Q \cap D} c(w,Q)\log(1 + \frac{(1-\lambda)c(w,D)}{\lambda p (w| C)*\abs{D}})$}
This ranking function is ranking documents by assigning documents score by the recall of query terms. First, the sum $\sum_{w \in D \cap D}$ means this will rank documents that have matches on query terms higher than any without. This is necessary to remove any noise that non matched by high probability terms may have for document score. Next, this function multiplies the probability by the count of word occurrence in the query, $c(q,Q)$ which serves to add higher importance to common query terms. Finally the ranking function includes a measure of term relevance in the given document. The numerator $(1-\lambda)c(w,D)$ models the term occurrence in the document. The denominator $\lambda p(w|C)*|D|$ represents the expected occurrence of the term in the document given its commonality in the corpus since $p(w|C)$ is overall unigram occurrence which is multiplied by document length $|D$ to provide an expected amount of term occurrences in document. This means that documents that have higher than the corpus wide expected term occurrence(and thus more relevant) have a higher probability than those that have a less than corpus average occurrence.
\subsection{Vector Space}
\subsubsection{What would be the query vector}
The query vector in this case is represented by $\sum_{w \in Q \cap D} c(w,Q)$ as we can essentially consider it a one hot vector for query document term matches. Since we only sum over terms that match all unmatched terms are set to 0. Since we multiply by $c(w,Q)$ any term that is not 0(and thus occurs) is set to its occurrence in the query.
\subsubsection{What would be the document vector}
The document vector is the $\log(1 + \frac{(1-\lambda)c(w,D)}{\lambda p (w| C)*\abs{D}})$ portion of the summation. Terms that occur in both the document and query are given the value of their occurrence in the document vs their expected occurrence in the document.
\subsubsection{What is the similarity function}
The similarity function is the multiplication by query vector and the document vector. Terms that occur in the document more than the LM predicts make a document more relevant than terms that occur less. These probabilities are summed over the multiplied query and document vector to produce a similarity score.
\subsubsection{Does the term weight in the document vector capture TF-IDF weighting and the document length normalization heuristics?}
It captures all 3 heuristics because of the document vector model. TF is captured by $c(w,D)$, IDF is captured by the use of the LM probability in the denominator($p(w|C)$) and document length normalization is captured by the multiplication of the IDF by document length($p(w|C) * |D|$).
\subsection{Are artificially long documents penalized using Jelinek-Mercer and Dirichlet prior smoothing?}
In Jelinek-Mercer artificially long documents are not penalized because the smoothing function is static everywhere in the prediction. In other words if we increase the document by size $k$ the scores will remain the same. On the other hand Dirichlet prior smoothing actually prefers longer documents since there is a separation in the normalization of document length ($n \log \frac{\mu}{|d| + \mu}$ and the term occurrence ($\log (1+\frac{c(w,d)}{\mu p(w|C)}$ and thus is we increase the document length by a factor of k our first term grows by a factor of k which places more importance on it than the document length
\section{KL-Divergence}
\subsection{Show that KL Divergence covers the query likelihood retrieval function if the language model is set to the word distribution in the query}
Kullback-Leibler divergence (or relative entropy) is written as $D(P | Q) = \sum_x p(x) \log \frac{p(x)}{q(x)}$. If we build on the assumption that $p(w|\theta_q)= \frac{c(w,Q)}{|Q|}$ to represent word likelihood given a query and $p(d|\theta_D) = \frac{c(w,D)}{|D|}$ then we can treat $\theta_q$ and $\theta_D$ as estimates of the query and document language models then the relevance can be measured by the negative KL divergence. We use the negative KL divergence because items that have a lower entropy between $q$ and $d$ are more relevant. Our query likelihood function becomes $score(Q,D) \myeq - D(\theta_Q| \theta_D) = \sum_w p(w|\theta_Q)\log p(w|\theta_D) + (- \sum_w p(w|\theta_q) \log p(w| \theta_q))$. The second term is a query dependent constant which is ignored for ranking.
\section{Divergence Minimization Feedback}
We start off with out optimization problem $f(\theta *) = \argmin [\frac{1}{n} (\sum_{i=1}^n D(\theta || \theta_{e_i}) - \lambda D(\theta || \theta_C$. \\ Using the fact that $g(p_1, p_2, ..., p_n) = \sum_{w \in V} p(w| \theta) = 1$ we use the Lagrange multiplier to find the point of minimum entropy across all probability distributions, $p'$ which we do by requiring that $\frac{\delta}{\delta p'} (f + \lambda(g-1))| = 0$. \\ This gives a system of $\lambda$ equations such that $\frac{\delta}{\delta p_k} (-(\sum_{j=1}^lambda p_j \log (p_j)) + \lambda(\sum_{j=1}^\lambda -1)) = 0$. Carrying out the differentiation we get $-(\frac{1}{\ln 2}+log_2 p'_k) + \lambda = 0$ which shows that all $p'_k$ are equal because they depend only on $\lambda$. \\
By then using our constraint that $\sum_{w \in V} p(w|\theta) = 1$ we find that $p'_k= \frac{1}{\lambda}$ meaning that the uniform distribution is that uniform with the most entropy and thus maxima. As a result the minima happens at  $p'_k= \frac{1}{1-\lambda}$\\
Substituting this information in, $p(w|\theta) \alpha exp(\frac{1}{1-\lambda}\frac{1}{|n|}\sum_{i=1}^n D(\theta || \theta_{E_i})) - \frac{\lambda}{1-\lambda} D(\theta || \theta_C)$. \\
Knowing that $D(\theta || \theta_C) = \log p(w|C)$ and $D(\theta || \theta_{E_i}) = \log p(w|\theta)$ we rewrite to our final form. $p(w|\theta) \alpha exp(\frac{1}{1-\lambda}\frac{1}{|n|}\sum_{i=1}^n \log(p|\theta_i) - \frac{\lambda}{1-\lambda}\log p(w|C))$
\end{document}


\section{Risk Invariance Algorithms}
  \subimport{thesis/}{riskinvalgsintro.tex}
  \subimport{thesis/definitions/}{trustreduction.tex}

  \subimport{thesis/theorems/}{saturationtheorem.tex}
  \subimport{thesis/proofs/}{saturationproof.tex}

  \subimport{thesis/theorems/}{trusttransfertheorem.tex}
  \subimport{thesis/proofs/}{trusttransferproof.tex}

  \subimport{thesis/definitions/}{restrictedflow.tex}

  \subimport{thesis/lemmas/}{flowlimitlemma.tex}
  \subimport{thesis/proofs/}{flowlimitproof.tex}

  \subimport{thesis/theorems/}{trustsavingtheorem.tex}
  \subimport{thesis/proofs/}{trustsavingproof.tex}

  \subimport{thesis/theorems/}{invtrustrednaivetheorem.tex}
  \subimport{thesis/proofs/}{invtrustrednaiveproof.tex}

  Until now $MaxFlow$ has been viewed purely as an algorithm. This algorithm is not guaranteed to always return the same
  flow when executed muliple times on the same graph. However, the corresponding flow value, $maxFlow$, is always the same.
  Thus $maxFlow$ can be also viewed as a function from a matrix of capacities to a non-negative real number. Under this
  perspective, we prove the following theorem. Let $\mathcal{C}$ be the family of all capacity matrices
  $C = [c_{vw}]_{V\left(\mathcal{G}\right) \times V\left(\mathcal{G}\right)}$.
  \subimport{thesis/theorems/}{maxflowcontinuitytheorem.tex}
  \subimport{thesis/proofsketches/}{maxflowcontinuityproofsketch.tex}

  Here we show three naive algorithms for calculating new direct trusts so as to maintain invariable risk when paying
  a trusted party. Let $F = \sum\limits_{i=1}^{n}x_i$. To prove the correctness of the algorithms, it suffices to prove that
  \begin{equation}
  \label{naive:req1}
     \forall i \in [n], c'_i \leq x_i \mbox{ and}
  \end{equation}
  \begin{equation}
  \label{naive:req2}
     \sum\limits_{i=1}^{n}c'_i = F - V \enspace.
  \end{equation}
  Proofs of correctness and complexity can be found in the Appendix.
  \subimport{thesis/algorithms/}{fcfscode.tex}
  This algorithm simply chooses to nullify all outgoing trust to one player after another in the order they are given at the
  input, until the desired indirect trust is achieved. The complexity of this algorithm is $O\left(n\right)$.
  \newpage
  \subimport{thesis/algorithms/}{abscode.tex}

  This algorithm finds a \texttt{reduction} value such that the configuration
  $\forall i \in \left[n\right], c'_i = \max{\left(0, x_i - \mbox{\texttt{reduction}}\right)}$ achieves the desired flow.

  The function \texttt{preprocess(}$x_i$\texttt{)} returns a data structure \texttt{X} containing the set of flows
  $\left(x_i\right)$, such that the corresponding function \texttt{popMin(X)} is able to repeatedly return a tuple
  consisiting of the index of the minimum element and a new data structure missing exactly the minimum element.
  Examples of such pairs of functions are:
  \begin{equation*}
  \begin{gathered}
    \begin{cases}
      \texttt{preprocess = quickSort} \\
      \texttt{popMin = (}x_1\texttt{, X}\setminus x_1\texttt{)}
    \end{cases}
    \mbox{ and} \\
    \begin{cases}
      \texttt{preprocess = FibonacciHeap} \\
      \texttt{popMin = (find-min(X),delete-min(X))}
    \end{cases} \enspace.
  \end{gathered}
  \end{equation*}
  In the general case, the complexity of the algorithm \texttt{abs} is proven to be
  $O\left(preprocess\right) + O\left(n\right)O\left(popMin\right)$. In both specific cases, the complexity is
  $O\left(n\log{n}\right)$. This algorithm also minimizes the $||\Delta_i||_\infty$ norm for the specific set of old flows
  $x_i$ given as input. A proof of this fact can be found in the Appendix.

  \subimport{thesis/algorithms/}{propcode.tex}
  We can see that this algorithm is simpler than the previous two. It simply reduces all outgoing direct trust proportionally
  in such a way that the desired flow is agieved. The complexity of this algorithm is proven to be $O\left(n\right)$.

  Naive algorithms result in $c'_i \leq x_i$, thus according to the Invariability Theorem (\ref{invariability}),
  $||\delta_i||_1$ is invariable for any of the possible results $C'$ of these algorithms and the resulting norm is not
  necessarily the minimum. The following algorithms concentrate on finding a configuration $C'$ that achieves $F' = F - V$
  while minimizing two $\delta_i$ norms, $||\delta_i||_\infty$ and $||\delta_i||_1$. We start with the
  $||\delta_i||_\infty$ minimizer.

  \subimport{thesis/algorithms/}{dinfmincode.tex}
  Since trust should be considered as a continuous unit and binary search bisects the interval containing the solution
  on each recursive call, inclusion of the $\epsilon$-parameters in \texttt{BinSearch} is necessary for the algorithm to
  complete in a finite number of steps.
  \subimport{thesis/algorithms/}{dinfbinsearchcode.tex}
  Let $\delta \in \left[0, \max\limits_{1 \leq i \leq n}{\{c_i\}}\right]$. Furthermore, let $C'$ such that
  $\forall i \in \left[n\right], c'_i = \max{\left(0, c_i - \delta\right)}$. We define $maxFlow\left(\delta\right) =
  maxFlow\left(A, B, C'\right) = F'$ and $MaxFlow\left(\delta\right) = X'$. Conventions similar to $F$, $X$ and $C$ hold
  for $F'$, $X'$ and $\delta$ as far as subscripts are concerned.
  \subimport{thesis/lemmas/}{maxflowmonotonicitylemma.tex}
  \subimport{thesis/proofsketched/}{maxflowmonotonicityproofsketch.tex}
  From the previous lemma we deduce that, given a $V \in \left(0, F\right)$, if we determine a $\delta$ such that
  $maxFlow\left(\delta\right) = F - V$, this $\delta$ is unique. Furthermore, it is
  \begin{equation*}
    ||\delta_i||_\infty = \max\limits_{1 \leq i \leq n}{\delta_i} = \max\limits_{1 \leq i \leq n}{\left(c_i - c'_i\right)} =
    \max\limits_{1 \leq i \leq n}{\left(c_i - \max{\left(0, c_i - \delta\right)}\right)} = \delta \enspace.
  \end{equation*}
  It is proven that the two algorithms work as expected, that is an invocation of \texttt{dinfmin} with valid inputs returns
  a capacity $C'$ that yields the desired $maxFlow$ and minimizes $||\delta_i||_\infty$. The complexity of \texttt{BinSearch}
  is $O\left(\left(maxFlow + n\right)\log_2\left(\frac{top - bot}{\epsilon_1 + \epsilon_2}\right)\right)$ and the complexity
  of \texttt{dinfmin} is
  $O\left(\left(maxFlow + n\right)\log_2\left(\frac{\delta_{max}}{\epsilon_1 + \epsilon_2}\right)\right)$.

  We will now concentrate on finding a capacity configuration $C'$ such that $F' = F - V$ and that minimizes
  $\sum\limits_{i=1}^{n}\left(c_i-c'_i\right) = ||\delta_i||_1$ as well. We treat the flow problem as a linear programming
  problem. Next we see the formulation of the problem in this form, along with a breakdown of each relevant matrix and
  vector.
  \subimport{thesis/lp/}{lpprimal.tex}
  We would like to find a solution that, except for maximizing the flow, also minimizes $||\delta_i||_1$ at the same time.
  More precisely, we would like to optimize
  \begin{equation*}
    \min{\sum\limits_{v \in \mathcal{V}}\left(c_{Av} - c'_{Av}\right)}
  \end{equation*}
  as well.
  Since we wish to optimize with regards to two objective functions, we approach the problem as follows: Initially, we ignore
  the minimization and derive the dual of the previous problem with respect to the maximisation. We then substitute the two
  problems' optimisations with an additional constraint that equates the two objective functions. Due to the Strong Duality
  theorem of linear programming \cite{amo}, this equality can be achieved only by the common optimal solution of the
  two problems. Next we treat the combination of constraints and variables of the primal and the dual problem, along with the
  newly introduced constraint and the previously ignored $||\delta_i||_1$ minimisation as a new linear problem. For every
  $j \in \left[n\right]$, the solution to this problem contains a $c'_{1j}$. These capacities will comprise the new
  configuration that player $A$ requires.

  We will now describe the dual problem in detail.
  \subimport{thesis/lp/}{lpdual.tex}
  Everything is now in place to define the linear problem whose solution $C'$ yields $maxFlow\left(C'\right) = F - V$ and
  minimizes $||\delta_i||_\infty$. The constraints are (\ref{lp:primal:constraints}) and (\ref{lp:dual:constraints})
  supplemented with a constraint that equates the two problems' optimality functions:
  \begin{equation*}
    \sum\limits_{j \in \left[n\right]}f'_{1j} = \sum\limits_{j \in \left[n\right]}c_{1j}y_{c1j} +
    \sum\limits_{i \in \left[n\right]}\sum\limits_{j \in \left[n\right]}c_{ij}y_{fcij} + \left(F - V\right)y_F \enspace.
  \end{equation*}
  The desired optimisation is
  \begin{equation*}
    \min{\sum\limits_{j \in \left[n\right]}\left(c_{1j} - c'_{1j}\right)} \enspace.
  \end{equation*}
  The final linear program consists of $2n^2 + 2n - 1$ variables and $2n^2 + 2n$ constraints. There exist a variety of
  algorithms that solve linear programs, such as simplex and ellipsoid algorithm. The ellipsoid algorithm requires polynomial
  time in the worst-case scenario, but for practical purposes algorithms of the simplex category seem to exhibit better
  behavior \cite{it}.


\documentclass[12pt]{amsart}
\usepackage{geometry} % see geometry.pdf on how to lay out the page. There's lots.
\usepackage{bsymb}
\usepackage{unitb}
\usepackage{calculational}
\usepackage{ulem}
\usepackage{hyperref}
\normalem
\geometry{a4paper} % or letter or a5paper or ... etc
% \geometry{landscape} % rotated page geometry

% See the ``Article customise'' template for some common customisations

\title{}
\author{}
\date{} % delete this line to display the current date

%%% BEGIN DOCUMENT
\begin{document}

\maketitle
\tableofcontents

%\section{}
%\subsection{}

\begin{machine}{m0}

%\hide{
	\begin{align*}
\variable{		n,a : \Int}
	\end{align*}
%}

\begin{align*}
\invariant{inv0}
{	a = n^3	}
\end{align*}
%
%%\begin{proof}{INIT/INV/inv0}
%%\begin{calculation}
%%
%%\end{calculation}
%%\end{proof}

\newevent{evt}

%
\begin{align*}
\initialization{init0}
{	n = 0 \land a = 0	}
\end{align*}
%%
%%
\begin{align*}
\evassignment{evt}{a0}
{	n' = n + 1	}
\end{align*}
%
We use the proof obligation of \ref{inv0} to deduce a proper assignment to $a$:
%
\begin{proof}{evt/INV/inv0}
	\begin{calculation}
		(n')^3
	\hint{=}{ \ref{a0} }
		(n+1)^3
	\hint{=}{ arithmetic }
		n^3 + 3 \cdot n^2 + 3 \cdot n + 1
	\hint{=}{ \ref{inv0} }
		a + 3 \cdot n^2 + 3 \cdot n + 1
	\hint{=}{ we add a variable $b$: \ref{inv1}, see below }
		a + b
	\hint{=}{ \ref{a1} }
%	\hint{=}{ }
		a'
	\end{calculation}
\end{proof}
%
\begin{align*}
\variable{	b : \Int}
\end{align*}
%
\begin{align*}
\invariant{inv1}
{	b = 3 \cdot n^2 + 3 \cdot n + 1	}
\end{align*}
%%%%%\begin{invariant}{inv2}
%%%%%	b ~=~ 3 \cdot n^2 + 3 \cdot n + 1
%%%%%\end{invariant}
%%%%
\begin{align*}
\evassignment{evt}{a1}
{	a' = a + b	}
\end{align*}
%
We now have a new invariant to preserve. It is easy to see how to establish it initially:
%%
\begin{align*}
\initialization{in1}
{	b = 1	}
\end{align*}
%
%\begin{itemize}
%\item label initialization predicates
%\item spacing commands
%\item \sout{dummy section}
%\item \sout{test case: train system}
%\item refinement
%
%	add refinement environments for: changing schedules, transforming progress properties
%\item po labels: check that proofs match a po
%\item translate the tags in proof obligations
%
%	create toc entries with the proof environment, in unitb.sty
%\end{itemize}
%
%\section{Todo:}
%\begin{itemize}
%\item grammar: make operator grammar generic
%\item grammar: include prefix operators and quantifiers
%\item
%\item in Z3.Z3.merge, remove the exceptions to integrate it to error handling
%\item spacing in the middle of event and set declarations
%\item testing the input validation, error messages
%\item proof structures (proof by cases, etc)
%\item types
%\item invariant theorems
%\item error checking
%\item better LaTeX formatting
%\item lazy proof checking
%\item \sout{continuous checking}
%\item \sout{\emph{bug}: last of empty list of calculation steps}
%\item \sout{error report: report line number instead of step number when proof is incorrect}
%\item generate documentation
%\item handle the ``\textbackslash input'' commands in latex and use it to produce a project hierarchy
%\end{itemize}
%
%I'm now describing how I came up with the proof. It came to me in a dream and I forgot it in another dream.
%%
\begin{proof}{evt/INV/inv1}
	\begin{calculation}
		3 \cdot (n')^2 + 3 \cdot n' + 1
	\hint{=}{ \ref{a0} }
		3 \cdot (n+1)^2 + 3 \cdot (n+1) + 1
	\hint{=}{ arithmetic }
		3 \cdot (n^2+2\cdot n+1) + 3 \cdot (n+1) + 1
	\hint{=}{ arithmetic }
		3 \cdot n^2+6\cdot n+3 + 3 \cdot n + 3 + 1
	\hint{=}{ arithmetic }
		3 \cdot n^2 + 3 \cdot n + 1 +6\cdot n+3 + 3
	\hint{=}{ \ref{inv1} }
		b+6\cdot n+3+3
	\hint{=}{ \ref{inv2}, see below }
		b+c
	\hint{=}{ \ref{a2}, see below }
%	\hint{=}{ }
		b'
	\end{calculation}
\end{proof}
%
\begin{align*}
\variable{	c : \Int}
\end{align*}
%
\begin{align*}
\invariant{inv2}
{	c = 6 \cdot n + 6	}
\end{align*}
%
\begin{align*}
\evassignment{evt}{a2}
{	b' = b + c	}
\end{align*}

Invariant \ref{inv2} is easy to satisfy initially:

\begin{align*}
\initialization{in2}
{	c = 6	}
\end{align*}
%

	If we increase $c$ by 6, we can easily see that \ref{inv2} is preserved:
\begin{align*}
\evassignment{evt}{a3}
{	c' = c + 6	}
\end{align*}

\begin{proof}{evt/INV/inv2}
	\easy
%	\begin{calculation}
%		6 \cdot (n') + 6
%	\hint{=}{ \ref{a0} }
%		6 \cdot (n+1) + 6
%	\hint{=}{ arithmetic }
%		6 \cdot n + 6 + 6
%	\hint{=}{ \ref{inv2} }
%		c + 6
%	\hint{=}{ \ref{a3} }
%		c'
%	\end{calculation}
\end{proof}
%%

\begin{align*}
\invariant{inv3}
{	f = \qfun{i}{ 0 \le i \land i < n }{ i^3 } }
\end{align*}

\begin{align*}
\variable{	f : \Int \pfun \Int}
\end{align*}

\begin{align*}
\dummy{	i,k : \Int}
\end{align*}

\with{functions} \with{sets}

\begin{proof}{INIT/INV/inv3}
	\begin{calculation}
		\qfun{i}{ 0 \le i \land i < n }{ i^3 }
	\hint{=}{ \ref{init0} }
		\qfun{i}{ 0 \le i \1\land i < 0 }{ i^3 }
	\hint{=}{ arithmetic }
%		\qfun{i}{ \false }{ i^3 }
%	\hint{=}{ arithmetic }
		\oftype{ \emptyfun }{ \pfun [ \Int, \Int ] }
	\hint{=}{ \ref{init3} }
		f
	\end{calculation}
\end{proof}

\begin{align*}
\initialization{init3}
{	f = \emptyfun	}
\end{align*}

\begin{proof}{evt/INV/inv3}
	\begin{calculation}
		\qfun{i}{ 0 \le i \land i < n' }{ i^3 }
	\hint{=}{ \ref{a0} }
		\qfun{i}{ 0 \le i \1\land i < n+1 }{ i^3 }
	\hint{=}{ split range with \ref{inv4} (see below) }
		\qfun{i}{ 0 \le i \1\land i < n }{ i^3 } \3 | n \fun n^3
	\hint{=}{ \ref{inv3} }
%		\qfun{i}{ 0 \le i \1\land i < n }{ i^3 } \3 | \qfun{i}{ i = n }{ i^3 }
%	\hint{=}{ one-point rule }
%		\qfun{i}{ 0 \le i \1\land i < n }{ i^3 } \2 | n \fun n^3
%	\hint{=}{ \ref{inv3} }
		f \1 | n \fun n^3
	\hint{=}{ \ref{inv0} }
		f \1 | n \fun a
	\hint{=}{ \ref{a4} }
		f'
	\end{calculation}
\end{proof}

\begin{align*}
\invariant{inv4}
{ 0 \le n }
\end{align*}

\begin{align*}
\evassignment{evt}{a4}
{ f' \2 = f \1 | n \fun a }
\end{align*}

\begin{align*}
\invariant{inv5}
{	\qforall{i}{0 \le i \1\land i < n}{ f.i = i^3 }		}
\end{align*}

\begin{proof}{evt/INV/inv5}
%	\begin{align}
%	\assert{asm0}{ \qforall{i}{}{ i \in \dom.f \2\equiv 0 \le i \land i < n } }
%	\end{align}
	\begin{calculation}
		\qforall{i}{0 \le i \1\land i < n'}{ f'.i = i^3 }
	\hint{=}{ \ref{a0} }
		\qforall{i}{0 \le i \1\land i < n+1}{ f'.i = i^3 }
	\hint{=}{ split range with \ref{inv4} }
		\qforall{i}{0 \le i \1\land i < n}{ f'.i = i^3 } \2 \land f'.n = n^3
	\hint{=}{ \ref{a4} and \ref{inv0} }
		\qforall{i}{0 \le i \1\land i < n}{ f'.i = i^3 } % \2 \land a = n^3
	\hint{=}{ \ref{a4}  }
		\qforall{i}{0 \le i \1\land i < n  }
			{ f.i = i^3 }
	\hint{=}{ \ref{inv5} }
%		\qforall{i}{0 \le i \1\land i < n
%			\1\land i \in \dom.f \setminus \dom.(n \fun a) }
%			{ f'.i = i^3 }
%	\hint{=}{  }
		\true
	\end{calculation}
%	\begin{subproof}{asm0}
%	\easy
%	\end{subproof}
\end{proof}

\begin{align*}
\invariant{inv6}
{	\dom.f = \qset{i}{0 \le i \land i < n }{ i }		}
\end{align*}

\begin{proof}{evt/INV/inv6}
	\begin{calculation}
		\qset{i}{0 \le i \land i < n' }{ i }
	\hint{=}{ \ref{a0} }
		\qset{i}{0 \le i \land i < n+1 }{ i }
	\hint{=}{ split range }
		\qset{i}{0 \le i \land i < n }{ i } \bunion \qset{i}{ 0 \le i \land i = n}{ i }
	\hint{=}{ \ref{inv6} }
		\dom.f \bunion \qset{i}{ 0 \le i \land i = n}{ i }
	\hint{=}{ one point rule with \ref{inv4} }
		\dom.f \bunion \{ n \}
	\hint{=}{ split range }
		\dom.f'
	\end{calculation}
\end{proof}

\begin{align*}
\constant{	N: \Int}
\end{align*}

\begin{align*}
&	\progress{prog0}{\true}{ f = \qfun{i}{0 \le i \land i < N}{i^3} }
\refine{prog0}{monotonicity}{prog1}{ with \ref{inv3} }
&	\progress{prog1}{\true}{ N = n  }
\refine{prog1}{induction}{prog2}{ over $\var{n}{up}{N}$ up to $N$ }
&	\progress{prog2}{n = k}{ k < n \2\lor n = N }
\refine{prog2}{trading}{prog3}{}
&	\progress{prog3}
		{ n = k \1\land \neg n = N}
		{ k < n \1\lor n = N }
\refine{prog3}{PSP}{ prog4, saf0 }{}
&	\progress{prog4}
		{n = k \1\land \neg n = N }
		{ \neg n = k }
\\ & 	\safety{saf0}{ k \le n }{ N = n }
%\refine{prog2}{trading}{prog3}{}
%&	\progress{prog3}{n = k \2\land \neg n = N}{ n < k } \\
 \refine{prog4}{discharge}{tr0,saf1}{}
& 	\safety{saf1}{ n = k \1\land \neg n = N }{ \neg n = k }
\end{align*}

\begin{align*}
\transient{evt}{tr0}
{	n = k \1\land \neg n = N
	}
\end{align*}

\begin{align*}
\cschedule{evt}{c0}
{	n < N
	}
\end{align*}

\begin{align*}
\evguard{evt}{grd0}
{	\neg n = N
	}
\end{align*}

\begin{align*}
\invariant{inv7}
{	n \le N
	} \\
\assumption{asm3}
{	0 \le N	}
\end{align*}

% \removecoarse{evt}{default} % \weakento{evt}{default}{c0}

\end{machine}

\end{document}


% A simple template for LaTeX documents
%
% To produce pdf run:
%   $ pdflatex paper.tex
%

\documentclass[10pt, twocolumn]{article}
%\documentclass[12pt]{article}

% Begin paragraphs with new line
\usepackage{parskip}

% Change margin size
\usepackage[margin=0.5in]{geometry}

% Graphics Example:  (PDF's make for good plots)
\usepackage{graphicx}
% \centerline{\includegraphics{figure.pdf}}

% Allows hyperlinks
\usepackage{hyperref}

% Blocks of code
\usepackage{listings}
\lstset{basicstyle=\ttfamily, title=\lstname}
% Insert code like this. replace `plot.R` with file name.
% \lstinputlisting{plot.R}

% Monospaced fonts
%\usepackage{inconsolata}
% GNU \texttt{make} is a nice tool.

% Supports proof environment
\usepackage{amsthm}

% Allows writing \implies and align*
\usepackage{amsmath}

% Allows mathbb{R}
\usepackage{amsfonts}

% Numbers in scientific notation
\usepackage{siunitx}

% Use tables generated by pandas
\usepackage{booktabs}

% norm and infinity norm
\newcommand{\norm}[1]{\left\lVert#1\right\rVert}
\newcommand{\inorm}[1]{\left\lVert#1\right\rVert_\infty}

% Statistics essentials
\newcommand{\iid}{\text{ iid }}
\newcommand{\Expect}{\operatorname{E}}
\newcommand{\Var}{\operatorname{Var}}
\newcommand{\Cov}{\operatorname{Cov}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

\newpage
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{verbatim}
```{r, include=FALSE, results='hide'}
legend('topright', c('a', 'b', 'c'), pch=1:3)
boxplot(Y ~ somefactor, data = mydata)
options(contrasts=c('contr.sum', 'contr.sum'))
means = tapply(Y, somefactor, mean)
G = expand.grid(k=1:3, j=1:4, i=1:3)
leaps::regsubsets(y ~ ., data=d, ...)
MASS::stepAIC(model0, ...)
\end{verbatim}

\textbf{Binomial}
$X \sim B(n, p)$
\[
    p(k) = \binom{n}{k} p^k (1-p)^{n-k}
    \qquad k = 0, 1, \dots, n
\]
$\Expect X = np, \quad \Var X = np(1-p)$

\textbf{Poisson}
$X \sim P(\lambda)$
\[
    p(k) = \frac{e^{-\lambda} \lambda^k}{k!}
    \qquad k = 0, 1, \dots
\]
$\Expect X = \lambda, \quad \Var X = \lambda$

\textbf{Normal}
$X \sim N(\mu, \Sigma)$, $\Sigma$ positive definite
\[
    f(x) = \frac{\exp\{ - \frac{1}{2}(x - \mu)^T \Sigma^{-1} (x - \mu) \}}
        {(2\pi)^{\frac{k}{2}} \sqrt{\det(\Sigma)}}
        = \frac{1}{\sqrt{2 \pi} \sigma} e^{-\frac{(x - \mu)^2}{2
        \sigma^2}}
\]
mgf: $M_X (t) = \exp (\mu' t + \frac{1}{2} t' \Sigma t)$

\textbf{Beta}
$ X \sim \text{Beta}(\alpha, \beta)$
\[
    f(x) = \frac{x^{\alpha-1}(1 - x)^{\beta-1}}{B(\alpha, \beta)}
    \qquad 0 \leq x \leq 1
\]
$\Expect X = \frac{\alpha}{\alpha + \beta},
\quad \Var X = \frac{\alpha \beta}{(\alpha + \beta)^2 (\alpha + \beta + 1)}$

\textbf{Gamma}
$X \sim \text{Gamma}(\alpha, \beta)$
\[
    f(x) = \frac{\beta^\alpha x^{\alpha-1} e^{-\beta x}}{\Gamma(\alpha)}
    \qquad x > 0
\]
$\Expect X = \frac{\alpha}{\beta},
\quad \Var X = \frac{\alpha}{\beta^2}$

$X \sim \text{Gamma}(\alpha, \beta) \iff \beta X \sim \text{Gamma}(\alpha, 1)$

$X_i \iid \text{Gamma}(\alpha_i, \beta)$, then
\[
    \sum X_i \sim \text{Gamma}(\sum \alpha_i, \beta)
\]
\textbf{Exponential}
Special case: $X \sim \text{Exp}(\lambda) \equiv \text{Gamma}(1, \lambda)$

$
f(x) = \lambda e^{-\lambda x},
\quad x > 0
\qquad \Expect X = \frac{1}{\lambda},
\quad \Var X = \frac{1}{\lambda ^2}
$

CDF $F(x) = 1 - e^{-\lambda x}$

\textbf{Chi square}
Special case: $X \sim \chi^2_n \equiv \text{Gamma}(\frac{n}{2}, \frac{1}{2})$

$
f(x) \propto x^{\frac{n}{2} - 1} e^{\frac{-x}{2}},
\quad x > 0
\qquad \Expect X = n,
\quad \Var X = 2n
$

Let $Z_i$ be iid $N(0, 1)$.
$\sum_{i=1}^n Z_i^2 \sim \chi^2_n$

Noncentral $\chi^2$. Let $Y \sim N(\mu, I)$ be an $n$ vector. Then
\[
    \norm{Y}^2 \sim \chi^2_n(\norm{\mu}^2)
\]
\textbf{F} if num. and den. independent then
\[
    F(m, n) \equiv \frac{\frac{\chi^2_m}{m}}
        {\frac{\chi^2_n}{n}}
\]
\textbf{T} if num. and den. independent then
\[
    t(n) = \frac{N(0, 1)}
    {\sqrt{\frac{\chi^2_n}{n}}}
\]
Conditional pdf:
\[
    f_{X|Y}(x | y) \equiv \frac{f_{X, Y}(x, y)}{f_Y(y)}
\]
Iterated expectation:
\[
    E(Y) = E(E(Y | X))
\]
Conditional variance formula:
\[
    \Var(Y) = \Var(E(Y | X)) + E(\Var(Y | X))
\]
Singular Value Decompostion (SVD) Any matrix $X$ can be written
\[
    X = UDV^T
\]
with $U, V$ orthogonal, and $D$ diagonal.

Moore Penrose Psuedoinverse $A^+$ exists uniquely for every matrix $A$.
Properties: $AA^+A = A, \quad A^+AA^+ = A^+$ and $AA^+, A^+A$ are symmetric.

Projection matrix $P$ are symmetric and idempotent. They have eigenvalues
either 0 or 1.
\[
    P = P^T \qquad P^2 = P
\]
Covariance of linear transformations
\[
    Cov(Ay, Bx) = A Cov(y, x) B^T
\]
Invert $2 \times 2$ matrix:
$
    A =
    [\begin{smallmatrix}
        a & b \\
        c & d \\
    \end{smallmatrix}]
$
\[
    A^{-1} =
    \frac{1}{\det (A)}
    \begin{bmatrix}
        d & -b \\
        -c & a \\
    \end{bmatrix}
\]
Block matrix:
$
\begin{bmatrix}
    A & B \\
    C & D
\end{bmatrix}^{-1} =
$
\[
\begin{bmatrix}
                     (A - BD^{-1}C)^{-1}         & -A^{-1}B(D -
    CA^{-1}B)^{-1} \\
                     -D^{-1}C(A - BD^{-1}C)^{-1} & (D - CA^{-1}B)^{-1}
\end{bmatrix}
\]
Sum identities:
\[
    \sum_{k=0}^{\infty} p^k = \frac{p}{1 - p} \qquad
    \sum_{k=0}^{\infty} k p^k = \frac{p}{(1 - p)^2} \qquad |p| < 1
\]
Integration by parts:
\[
    \int uv' = uv - \int u'v
\]
Matrix / Vector differentiation

$\frac{\partial A^T \beta}{\partial \beta} = A$,
$\frac{\partial \beta^T A \beta}{\partial \beta} = (A + A^t) \beta =
2A\beta$ for $A$ symmetric.

$\frac{\partial}{\partial \theta_i} \log (|A|) =
tr( A^{-1} \frac{\partial A}{\partial \theta_i}$)

\newpage
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\subsection*{232B}

\textbf{Linear mixed models} Standard assumptions:
\[
    y = X \beta + Z \alpha + \epsilon
\]
where $\alpha \sim N(0, G)$ independent of  $\epsilon \sim N(0, R)$.

Marginal model $y \sim N(X \beta, V)$ where $V = R + ZGZ'$.

Restricted maximum likelihood (REML) uses a linear transformation
$Ay$ to remove the fixed effects, and then estimates the variance.
We have $\text{rank} (A) = n - p$ where $p = \text{rank} (X)$ and $A'X =
0$. Then $z = A'y \sim N(0, A'VA)$ and we can maximize the restricted log likelihood
to estimate the variance parameters. Let $P = A(A'VA)^{-1}A'$ and solve
\[
    0 = \frac{\partial l_R}{\partial \theta_i} = \frac{1}{2}
    \left( y'P \frac{\partial V}{\partial \theta_i} Py
    - tr (P \frac{\partial V}{\partial \theta_i})
    \right)
\]
\textbf{Predictions}
Let $\xi = b' \beta + a' \alpha$ be a mixed effect. These are what we're
interested in estimating. We call them predictions rather than estimations
because we're predicting a random component.

BLUE - Best linear unbiased estimator,
$\tilde{\beta} = (X' V^{-1} X)^{-1} X' V^{-1} y$. This is the MLE of
$\beta$.

BLUP - Best linear predictor, which plugs in $\tilde{\beta}$ into BP.

BP - Best predictor, $\Expect (\xi | y) = b' \beta + a' G Z' V^{-1} (y - X
\beta)$ a theoretical ideal that's
usually difficult or impossible to derive.

EBLUP - Empirical best linear predictor, plugs in estimates for both fixed
and variance components into the BP. This is
typically the one we compute and use.

\begin{verbatim}
library(lme4)
library(HLMdiag)
fit1 = lmer(y ~ X1 + X2 + (1 | V), data=somedata)
s1 = summary(fit1)
# 2 ways to extract variance estimates:
s1$sigma^2
s1$varcar$V
HLMdiag::varcomp.mer(fit1)
getME(fit1, ...)  # Extract various components
predict(fit1, data=newdata)  # EBLUPs
\end{verbatim}

2 ways to get standard errors for variance estimates are parametric
bootstrap and asymptotic covariance matrix of the estimates.

\newpage
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\subsection*{232A}

Least Squares Principle
\[
    \text{arg min}_\beta \norm{Y - X\beta}^2
\]
Normal Equations - Any $b$ satistfying this solves the least squares
\[
    X^T X b = X^T y
\]
Gauss Markov Theorem - $\hat{\beta}$ is Best Linear Unbiased
Estimator (BLUE) of $\beta$.
\[
    \hat{\beta} = (X^T X)^{-1} X^T y \sim N(\beta, \sigma^2 (X^T X)^{-1})
\]
variance:
\[
    \frac{\norm{y - X \hat{\beta}}^2}{\sigma^2} \sim
\chi^2_{n-p}.
    \qquad \implies \qquad
    \hat{\sigma}^2 = \frac{\norm{y - X \hat{\beta}}^2}{n - p}
\]
Use t test for hypothesis testing and confidence intervals for the value of
a particular $\beta_j$ coefficient.
Let $w_{ii}$ be the $i$th diagonal entry of $(X^T X)^{-1}$.
\[
    \frac{\beta_j - \beta_j^*}{\hat{\sigma} \sqrt{w_{ii}}} \sim t_{n-p}
\]
$1 - \alpha$ Confidence intervals for new observation $Y_h$ at $x_h$ and $E[Y_h]$:
\[
    E[y_h] \approx \hat{y_h} \pm t(n-p, 1 - \frac{\alpha}{2}) \hat{\sigma}
        \sqrt{x_h^T (X^T X)^{-1} x_h}
\]
\[
    y_h \approx \hat{y_h} \pm t(n-p, 1 - \frac{\alpha}{2}) \hat{\sigma}
        \sqrt{1 + x_h^T (X^T X)^{-1} x_h}
\]
Simultaneous (Working-Hotelling) confidence interval for $\Expect (y_h)$:
\[
    \hat{y}_h \pm \sqrt{p F_{p, n - p, 1 - \alpha}} se\{ \hat{y}_h \}
\]
\[
    \frac{(\hat{\beta} - \beta)^T X^T X (\hat{\beta} - \beta) /
    p}{\hat{\sigma}^2}
    \sim F_{p, n - p}
\]
General linear tests. Partition $\beta = (\beta_1, \beta_2)$ where $\beta_1$
is an $r$ vector and $\beta_2$ is $p - r$. Null hypothesis $H_0: \beta_2 =
\beta_2^*$ (often 0), and $H_a: \beta_2 \neq \beta_2^*$. Then
$SSE_r = \norm{y - X_2 \beta_2^* - X_1 \tilde{\beta_1}}^2$ is the sum of
squared error for the reduced model and
$SSE_f = \norm{y - X \hat{\beta}}^2$ is the squared sum of error for the
full model.
Under $H_0$:
\[
    \frac{\frac{SSE_r - SSE_f}{p - r}}
         {\frac{SSE_f}{n - p}}
         \sim F_{p-r, n-p}
\]
Alternate forms of linear test, and testing a linear combination if $R\beta
= r$, for $R$ full rank $s \times p$ matrix.
\[
    \frac{(R \hat{\beta} - r)^T (R(X^T X)^{-1} R^T)^{-1} (R \beta - r) / s}
    {\hat{\sigma}^2} \sim F_{s, n-p}
\]
$SSTO = \sum_{i=1}^n (y_i - \bar{y}) = \norm{y - \bar{y} 1_n }^2
        = \norm{(I - J)y}^2$

$SSR = \sum_{i=1}^n (\hat{y}_i - \bar{y}) = \norm{\hat{y} - \bar{y} 1_n}^2
    = \norm{(H - P)y}^2$

$SSE = \sum_{i=1}^n (y_i - \hat{y}_i) = \norm{y - \hat{y}}^2
        = \norm{(I - H)y}^2$

\begin{verbatim}
SSE = sum(residuals(fit)^2)
SSR = sum((fitted(fit) - mean(y))^2)
SSTO = sum((y - mean(y))^2)
\end{verbatim}

If the model contains the intercept in the column space of $X$  then $SSTO = SSR + SSE$.

$R^2 = 1 - \frac{SSE}{SSTO}$

Adjusted $R^2_a = 1 - \frac{SSE / (n-p)}{SSTO / (n-1)}$

$AIC = n \log SSE + 2p$

$BIC = n \log SSE + p \log n$

$Cp = \frac{SSE}{MSE} - (n - 2p)$

\begin{verbatim}
influence.measures, cooks.distance
\end{verbatim}

Residuals: $\hat{\epsilon}_i = y_i - \hat{y}_i$

Studentized residuals (\texttt{rstandard} in R):
\[
    \gamma_i =
    \frac{\hat{\epsilon}_i}{ s \{ \hat{\epsilon}_i \} } =
    \frac{\hat{\epsilon}_i}{\hat{\sigma} \sqrt{1 - h_{ii}}}
\]
Studentized deleted residuals (\texttt{rstudent} in R):
\[
    t_i = \frac{\hat{\epsilon}_i}{\sqrt{MSE_{(-i)} (1 - h_{ii})}} \sim t_{n - p -1}
\]
Where $MSE_{(-i)} = SSE_{(-i)} / (n - 1 - p)$ and
$SSE_{(-i)} = SSE - \frac{\hat{\epsilon}_i}{1 - h_{ii}}$ can be used to
calculate without refitting model.

Prediction sum of squares (PRESS) is the same as leave one out cross
validation (LOOCV). Prediction error on $i$th observation is called deleted
residuals:
\[
    y_i - \hat{y}_{i (-i)} = \frac{y_i - \hat{y}_i}{1 - H_{ii}}
\]
Works for ridge regression also, letting
$H = X(X^T X + \lambda I)^{-1} X^T$.

\textbf{Ridge Regression} for $\lambda > 0$ solves
\[
    \min_\beta ||Y - X\beta||^2 + \lambda ||\beta||^2
\]
\begin{verbatim}
W = solve(XtX + lambda * Ip)
betahat = W %*% t(X) %*% y
\end{verbatim}

\subsection*{ANOVA}

Three principles of experimental design: 1) Replication 2) Randomization 3)
Blocking

One way ANOVA with $n$ total observations, $K$ groups:

{
\centering
\begin{tabular}{lll}
    SS   &  & DF     \\
    SSTR & $\sum_{j=1}^K n_j (\bar{y_{j \cdot}} - \bar{y_{\cdot \cdot}})^2$  & K - 1 \\
    SSE  & $\sum_{i=1}^n (y_{ij} - \bar{y_{j \cdot}})^2$  & n - K \\
    SSTO & $\sum_{i=1}^n (y_{ij} - \bar{y_{\cdot \cdot}})^2$  & n - 1
\end{tabular}
}

Contrasts are sums of the form $\Phi = \sum_{i=1}^K c_i \mu_i$ with
$\sum_{i=1}^K c_i = 0$.
Tukey's works for all pairwise contrasts.
Scheffe's and extended Tukey works for all contrasts.
Bonferroni's is for a limited number of pre specified contrasts.

Example of contrast $L$ with standard error $se(\hat{L}) = \hat{\sigma}
\sqrt{\sum_{i=1}^I c_i^2 / n_i}$.
Each way to compute a confidence interval for $L$ can be expressed in the form:
$$
    \hat{L} \pm se(\hat{L}) \times \text{multiplier}
$$

\begin{verbatim}
m = c(simple = qt(1 - alpha / 2, fit$df.residual)
, bonferroni = qt(1 - alpha / (2 * 20), fit$df.residual)
, ex_tukey = qtukey(1 - alpha, I, n - I)
, scheffe = sqrt((I - 1) *
                qf(1 - alpha, I - 1, fit$df.residual)))
width = seL * multipliers
TukeyHSD(aov(y ~ x, data))  # pairwise intervals
\end{verbatim}

\newpage

\subsection*{Multivariate Normal}

log likelihood for $k$ vector $x \sim N(\mu, \Sigma)$
\[
    l_x = -\frac{k}{2} \log 2 \pi - \frac{1}{2}
    \{ \log \det \Sigma + (x - \mu)^T \Sigma^{-1} (x - \mu) \}
\]
Stein's formula: $X \sim N(\mu, \sigma)$
\[
    \Expect (g(X) (X - \mu)) = \sigma^2 \Expect(g'(X))
\]
assuming these expectations are finite.

$X \sim N(\mu, \Sigma)$, $A$ an $m \times n$ matrix,
then
\[
    AX \sim N(A \mu, A \Sigma A^t)
\]
For $\Sigma$ full rank it's possible to transform between $Z \sim
N(0, I)$ and $X$:
\[
    X = \Sigma^{1/2} Z + \mu \qquad Z = \Sigma^{-1/2} (X - \mu)
\]
In block matrix form:
\[
    X =
    \begin{bmatrix}
        X_1 \\
        X_2 \\
    \end{bmatrix}
    \sim N \left(
    \begin{bmatrix}
        \mu_1 \\
        \mu_2 \\
    \end{bmatrix}
    ,
    \begin{bmatrix}
        \Sigma_{11} & \Sigma_{12} \\
        \Sigma_{21} & \Sigma_{22} \\
    \end{bmatrix}
\right)
\]
Assuming $\Sigma_{11}$ is positive definite then the conditional
distribution
\[
    X_2 | X_1 \sim N(\mu_2 + \Sigma_{21} \Sigma_{11}^{-1} (X_1 - \mu_1),
    \Sigma_{22} - \Sigma_{21} \Sigma_{11}^{-1} \Sigma_{12})
\]

\newpage
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\end{document}


\chapter{Similarity Renormalization Group Formalism}\label{srg_formalism}

In this chapter, we introduce the physics concepts and formalism necessary to understand the 1-dimensional problems to which SRG is applied. We then introduce SRG and discuss details and open questions regarding its use.

\section{Quantum Mechanics Operators}

Operators in quantum mechanics act linearly on state vectors, which represent specific states of the system. Each operator corresponds to some physical quantity; for example, the Hamiltonian, $\hat{H}$, which is the sum of kinetic and potential energy operators, $\hat{T}$ and $\hat{V}$ respectively, is connected with the possible energies of the system. A state, which in Dirac notation is denoted by an abstract ket, $\ket{\Psi}$, is said to be an eigenstate with eigenvalue $\omega$ of some operator, $\hat{\Omega}$, when the following equation holds:
\begin{equation}
\hat{\Omega}\ket{\Psi} = \omega\ket{\Psi}.
\end{equation}
These eigenvalues are the \textit{observables} (i.e. measurable quantities) of quantum mechanical systems.

Operators exist in a Hilbert space spanned by a basis of state vectors, $\ket{\Psi_i}$. An operator's representation with respect to a basis is given by its matrix elements,
\begin{equation}
\hat{\Omega}_{ij} = \bra{\Psi_i}\Omega\ket{\Psi_j}.
\end{equation}
An operator can be transformed to a different basis through a unitary transformation, which preserves its eigenvalues and thus its observables. Examples of 1-dimensional single-particle bases include particle position, $x$, particle momentum, $p$, and the 1-dimensional harmonic oscillator basis, which will be discussed next.

\subsection{1-Dimensional Harmonic Oscillator}

\begin{figure}[t]
\begin{center}
\subfloat[\label{fig:ho_wf_w_1}]{\includegraphics[width=0.5\linewidth]{ho_plot_1}}
\subfloat[\label{fig:ho_wf_w_4}]{\includegraphics[width=0.5\linewidth]{ho_plot_4}}
\end{center}
\caption{1-dimensional harmonic oscillator wavefunctions for the three lowest energy states for (a) $\omega=1$ and (b) $\omega=4$.}
\label{fig:ho_wf}
\end{figure}

The 1-dimensional harmonic oscillator is a single particle in a purely quadratic potential, $\hat{V}(x) = m\omega^2 \hat{x}^2/2$. The Hamiltonian for the quantum harmonic oscillator is given by
\begin{equation}
\hat{H}_{HO} = \frac{\hat{p}^2}{2m} + \frac{1}{2}m\omega^2\hat{x}^2,
\end{equation}
which has eigenstates $\ket{n}$, which have corresponding wavefunctions
\begin{equation}
\Psi_n(x) = \frac{1}{\sqrt{2^n n!}}\left(\frac{m \omega}{\pi \hbar}\right)^{1/4}e^{-m \omega x^2 / 2 \hbar} H_n\left(\sqrt{\frac{m \omega}{\hbar}}x\right)
\end{equation}
\begin{equation}
\Phi_n(p) = \frac{(-i)^n}{\sqrt{2^n n!}}\left(\frac{1}{\pi m \hbar \omega}\right)^{1/4}e^{-p^2 / 2 m \hbar \omega} H_n\left(\frac{p}{\sqrt{m \omega \hbar}}\right)
\end{equation}
where $H_n(x)$ is the $n$-th Hermite polynomial. The three lowest energy momentum-space wavefunctions, $\Phi_n(p)$, can be seen in Fig.~\ref{fig:ho_wf}. The harmonic oscillator Hamiltonian can also be rewritten as
\begin{equation}
\hat{H}_{HO} = \hbar \omega \left(\hat{a}^\dagger \hat{a} + \frac{1}{2}\right),
\end{equation}
where $\hat{a}^\dagger$ and $\hat{a}$ are raising and lowering operators which act on the harmonic oscillator eigenstates like
\begin{equation}
\hat{a}^\dagger\ket{n} = \sqrt{n + 1}\ket{n+1}
\end{equation}
\begin{equation}
\hat{a}\ket{n} = \sqrt{n}\ket{n-1}.
\end{equation}

For an operator, $\hat{V}$, represented with respect to a momentum basis, $V(p, p')$, a transformation to a harmonic oscillator basis can be achieved by the following calculation:
\begin{equation}\label{eq:ho_transform}
\bra{n}\hat{V}\ket{n'} = \iint V(p, p') \Phi_n^*(p) \Phi_{n'}(p') \,dp\,dp'.
\end{equation}

\subsection{Many-particle states}

For a system of $A$ non-interacting particles, the energy eigenstates of the system can be written as a product of the eigenstates of the single particle Hamiltonians:
\begin{equation}
\ket{\Psi} = \prod_{i=1}^{A}\ket{\Psi_k}_i
\end{equation}
where $\ket{\Psi_k}_i$ is the $k$-th eigenstate of the $i$-th particle's single particle Hamiltonian. For $A$ non-interacting particles in a harmonic oscillator potential, these product states are denoted by:
\begin{equation}
\ket{n_1 n_2 ... n_A} = \prod_{i=1}^{A}\ket{n_i}_i.
\end{equation}

\section{1-Dimensional System of Bosons}

The Hamiltonian for a 1-dimensional system of $A$ identical bosons with mass $m$ that interact via a local two-body potential is as follows:
\begin{equation}
\hat{H} = \frac{1}{2 m} \sum_{i=1}^A \hat{k}_i^2 + \sum_{i=1}^A\sum_{j=i+1}^{A} \hat{V}(x_i - x_j).
\end{equation}
In this equation, the $\hat{k}_i$ are the single-particle momenta and the $x_i$ are the single-particle coordinates. A local potential is a function of the distance between two particles, as opposed to a function of both of their absolute coordinates.

\subsection{Jacobi Coordinates}

A factorization of the Hamiltonian into a center-of-mass component, which is unaffected by the local potential, and a component relative to the center of mass is possible. This is achieved by a transformation to relative momentum Jacobi coordinates \cite{Jurgenson:2008jp}. These are defined to be:
\begin{equation}
p_i = \sqrt{\frac{i}{i+1}} \left(\frac{1}{i} \sum_{j=1}^{i}(k_j - k_{i+1})\right),
\end{equation}
with $k_i$ being the single-particle momenta as mentioned above. It is worth noting that for a system of $A$ particles, we only need $A-1$ Jacobi momentum coordinates. We can get $\hat{V}$ in terms of incoming Jacobi momentum $p$ and outgoing momentum $p'$ by taking the following Fourier transform of $\hat{V}(x_1 - x_2)$
\begin{equation}
\hat{V}(p, p') = \int \hat{V}(\sqrt{2} l_1) e^{- i (p - p') l_1} \,dl_1,
\end{equation}
where $l_1$ is the coordinate conjugate of $p_1$, $(x_1 - x_2)/\sqrt{2}$. Removing the center-of-mass component of the Hamiltonian, we find the new Hamiltonian to be
\begin{equation}
\hat{H} = \frac{1}{2 \mu} \sum_{i=1}^{A-1} \hat{p}_i^2 + \sum_{i=1}^{A-1}\hat{V}(p_i, p_i'),
\end{equation}
where $\mu$ is the reduced mass given by $\mu = m / A$ for particles of equal mass. For the purposes of this work, we will be starting with a potential defined with respect to the Jacobi momentum coordinates, avoiding the process of Fourier transforming a local coordinate-space potential.

\subsection{Transformation To Harmonic Oscillator States}

In momentum space, SRG evolutions require separate treatment of 2-body, 3-body, and higher-body potentials, to avoid delta functions caused by spectator particles \cite{glöckle1983quantum}. A particle is a spectator particle when it is in a state where it does not interact with two particles that are interacting with each other. To avoid the cognitive load of handling all these potentials separately, we can make a transformation to a discrete basis. The discrete basis of choice for this project is the harmonic oscillator basis with respect to the Jacobi coordinates. This transformation can be achieved as shown in Eq.~\ref{eq:ho_transform}. From here on out, $\ket{n_i}$ will mean the $n$-th harmonic oscillator state with respect to the $i$-th Jacobi coordinate. So for a full treatment of an $A$-particle system, we will need product states for $A-1$ Jacobi coordinates,
\begin{equation}
\ket{n_1 n_2 ... n_{A-1}} = \prod_{i=1}^{A-1}\ket{n_i}.
\end{equation}

\subsection{Symmetrizing the Harmonic Oscillator Basis}

According to the spin-statistics theorem, the state of a system of bosons must
be symmetric under any permutation of the particle coordinates
\cite{streater2000pct}.
We approach the problem of generating a basis of states that reflects this symmetry recursively, by first symmetrizing the 2-body system and then going from a symmetrized $(A-1)$-body basis to a symmetrized $A$-body basis. At each stage, we must diagonalize the symmetrizer, the form of which we will show for the 2-body and 3-body cases.

For the 2-particle system, we are only working with the first Jacobi coordinate and the harmonic oscillator states corresponding to it, $\ket{n_1}$. The symmetrizer is $\hat{S} = (1 + \hat{P}_{12})/2$, where $\hat{P}_{ij}$ is the operator that exchanges the coordinates between $i$-th and $j$-th particles. Because harmonic oscillator wavefunctions are either even for even $n$ or odd for odd $n$, $\hat{P}_{12} \ket{n_1} = (-1)^{n_1} \ket{n_1}$ and the symmetrizer is already diagonal with eigenvalue 0 for odd $n_1$ and eigenvalue 1 for even $n_1$. We select the states with eigenvalue 1 to create our symmetric harmonic oscillator basis for the 2-particle system.

To generate the partially symmetrized basis for the 3-body system, we take the states $\ket{n_1 n_2}$ where the possible $n_1$ values come from the symmetrized 2-body basis. The general 3-body symmetrizer is governed by the permutation group $S_3$, generated by $\hat{P}_{12}$ and $\hat{P}_{23}$, and has the form
\begin{equation}
\hat{S} = \frac{1}{6}(1 + \hat{P}_{12} + \hat{P}_{23} + \hat{P}_{12}\hat{P}_{23} + \hat{P}_{23}\hat{P}_{12} + \hat{P}_{12}\hat{P}_{23}\hat{P}_{12})
\end{equation}
Since the states included in our to-be-symmetrized basis are already symmetric with respect to $\hat{P}_{12}$, the symmetrizer simplifies to $\hat{S} = (1 + 2\hat{P}_{23})/3$. The matrix elements of $\hat{P}_{23}$ in our partially symmetrized basis are
\begin{equation}
\bra{n_1' n_2'}\hat{P}_{23}\ket{n_1 n_2} = \delta_{N',N} \bra{n_1' n_2'}\ket{n_1 n_2}_3,
\end{equation}
where $N'=n_1' +n_2'$, $N=n_1 + n_2$, and $\bra{n_1' n_2'}\ket{n_1 n_2}_3$ is the 1-dimensional harmonic oscillator transformation bracket for particles with mass ratio 3. This transformation bracket value is calculated as
\begin{equation}
\bra{n_1' n_2'}\ket{n_1 n_2}_3 =
\frac{\sqrt{n_1!n_2!}}{\sqrt{n_1'!n_2'!}}\sum_{k=0}^{n_1'}\binom{n_1'}{k}\binom{n_2'}{n_2
- k}\left(\frac{1}{2}\right)^{n_1' + n_2 -
2k}\left(\frac{\sqrt{3}}{2}\right)^{n_2' - n_2 + 2k} (-1)^{n_2-k}
\delta_{n_{1}' + n_{2}',n_{1} + n_{2}}.
\end{equation}
Diagonalizing $\hat{S}$ will give eigenvectors that are normalized linear combinations of product states with the same total harmonic oscillator number $N$. We again select the ones with eigenvalue 1 and keep those as our fully symmetrized 3-body basis. While the project leading up to this thesis only worked with 2-particle and 3-particle systems and thus only the treatment of symmetrizing those bases is relevant to this work, the procedure is generalizable to symmetrize up to any $A$-particle basis. The details of this procedure are explained in Ref.~\cite{Jurgenson:2008jp}.

\subsection{Matrix Elements in the 3-Body Harmonic Oscillator Space}

From Eq.~\ref{eq:ho_transform}, we are able to transform both parts of our 2-body Hamiltonian into the symmetrized 2-body harmonic oscillator basis. In order to work in a symmetrized 3-body basis, we need to compute the kinetic energy for the 3-body system and embed the 2-body potential in the 3-body space. Both the $A$-body kinetic energy and the $A$-body embedded 2-body potential are defined with respect to their $(A-1)$-body counterparts \cite{Jurgenson:2008jp}, so we will set up the discussion to give those formulas. Let $\ket{N_A i_A}$ be a symmetrized $A$-body state with total harmonic oscillator number $N_A$ and degeneracy label $i_A$. $\ket{N_A i_A}$ is defined as
\begin{equation}
\ket{N_A i_A} = \sum_{l=1}^k c_l \ket{N_{A-1} i_{A-1}; n_{A-1}},
\end{equation}
where $k$ is the number of states in the linear combination, the $c_l$ are the coefficients of the linear combination of product states that make up the symmetrized state, $\ket{n_{A-1}}$ is a harmonic oscillator state with respect to the $(A-1)$-th Jacobi coordinate, and $\ket{N_{A-1} i_{A-1}}$ is a symmetrized $(A-1)$-body state. The $A$-body kinetic energy is calculated as
\begin{equation}
\begin{aligned}
\bra{N_A' i_A'}\hat{T}_A\ket{N_A i_A} = & \sum_{l=1}^k \sum_{l'=1}^{k'} c_l c_{l'}' (\bra{N_{A-1}'
i_{A-1}'}\hat{T}_{A-1}\ket{N_{A-1} i_{A-1}}\delta_{n_{A-1}',n_{A-1}} \\
                                        & -
                                        \frac{\omega}{4}\delta_{N_{A-1}',N_{A-1}}\delta_{i_{A-1}',i_{A-1}}(\sqrt{(n_{A-1} + 1)(n_{A-1} + 2)}\delta_{n_{A-1}',n_{A-1} + 2} \\
& + \sqrt{n_{A-1}(n_{A-1} - 1)}\delta_{n_{A-1}',n_{A-1} - 2} \\
& - (2 n_{A-1} + 1)\delta_{n_{A-1}',n_{A-1}})).
\end{aligned}
\end{equation}
Similarly, the embedded 2-body potential in the $A$-body basis is calculated as
\begin{equation}
\bra{N_A' i_A'}\hat{V}_A\ket{N_A i_A} = \frac{A}{A-2} \sum_{l=1}^k \sum_{l'=1}^{k'} c_l c_{l'}'
\bra{N_{A-1}' i_{A-1}'}\hat{V}_{A-1}\ket{N_{A-1} i_{A-1}}\delta_{n_{A-1}',n_{A-1}}.
\end{equation}

\section{Similarity Renormalization Group}

The similarity renormalization group (SRG), whose use in low-energy nuclear physics was initially explored at OSU \cite{Jurgenson:2007td}, is one method of reducing the computational complexity of low-energy nuclear calculations. The idea behind it is to continuously unitarily transform the operator of interest (for example, the Hamiltonian) into a simpler form. This simpler form is chosen to allow the large basis to be truncated without affecting the operator eigenvalues, which are essential to the truncated operator's utility in later calculations. The SRG transformation is given by the flow equation for the evolving Hamiltonian $\hat{H}_s$,
\begin{equation}
\frac{d \hat{H}_s}{ds} = [\hat{\eta}_s, \hat{H}_s],
\end{equation}
where $[A, B]$ is the matrix commutator and $\hat{\eta}_s$ is the generator of the transformation which is defined as
\begin{equation}
\hat{\eta}_s = [\hat{G}_s, \hat{H}_s],
\end{equation}
where $\hat{G}_s$ is known as the flow operator. The changes in the Hamiltonian over the course of the SRG evolution are absorbed into the potential, $\hat{V}_s$, leaving the kinetic energy, $\hat{T}$, independent of $s$, the flow parameter. $s$ is taken to be 0 for the un-evolved Hamiltonian. It is often convenient to use $\lambda = 1 / s^{1/4}$ as an alternative flow parameter, so instead of going from $s=0$ towards $s=\infty$ over the course of an SRG transformation, you are going from $\lambda=\infty$ towards $\lambda=0$. For our purposes, $\lambda=40$ (or $s=3.9 \times 10^{-7}$) will be a good enough place to start with the initial Hamiltonian.

\subsection{Induced Many-Body Forces and Flow Operators}

The SRG evolution is fully unitary for an $A$-body operator when done in the $A$-body space. However, working in the full many-body space is only feasible for the smallest of systems, due to the combinatorial scaling of the basis size with respect to $A$. When evolving an $A$-body system operator in some smaller basis, the SRG evolution induces many-body forces which show up as an error when eventually computing operator observables further down the line. This error has limited the SRG's domain of applicability to calculations for small to medium-sized systems.

Certain results have suggested that alternative choices for the flow operator, $\hat{G}_s$, could reduce this many-body force induction and thereby reduce the error induced by applying SRG to calculations \cite{Dicaire:2014fra}. The standard choice for $\hat{G}_s$ is the kinetic energy, which is easy to calculate and represent in most problems. Alternative flow operator choices have been explored previously, but not thoroughly.

\subsection{Value of the 1-Dimensional Laboratory}

SRG was explored initially in the 1-dimensional setting, which made it easy to test and allowed those exploring it to learn a great deal, such as the more careful treatment necessary for $A$-body evolutions in momentum space. Moreover, the setup of the 1-dimensional system is analogous to that of more complex calculations, so the results from the 1-dimensional setting generalize to application of the SRG in 3-dimensional calculations. If there is something to be learned about the connection between flow operator and many-body force induction, it is best comprehensively explored in a simple 1-dimensional problem and then generalized to expensive, difficult 3-dimensional calculations.

In chapter \ref{python_lib}, we will explain the structure of the Python framework designed to make it easy to explore SRG in this 1-dimensional setting.


\subsubsection{complex -- a numeric type}
Complex numbers have a real and imaginary part, which are each a floating point number.
To extract these parts from a complex number z, use z.real and z.imag.
(The standard library includes the additional numeric types fractions.Fraction, for rationals, and decimal.Decimal, for floating-point numbers with user-definable precision.)