% Like most advanced LaTeX files, this one begins with a lot of
% boilerplate. You don't need to understand (or even read) most of it.
% All you need to do is fill in your name, UMN ID, email address,
% and the number of the pset. (Search for "METADATA" to find the place
% for this.) Then, you can go straight to the "EXERCISE 1"
% section and start writing your solutions.
% The "VARIOUS USEFUL COMMANDS" section is probably worth taking a
% look at at some point.

%----------------------------------------------------------------------------------------
%	PACKAGES AND OTHER DOCUMENT CONFIGURATIONS
%----------------------------------------------------------------------------------------
\documentclass[paper=a4, fontsize=12pt]{scrartcl} % A4 paper and 12pt font size
\usepackage[T1]{fontenc} % Use 8-bit encoding that has 256 glyphs
\usepackage[english]{babel} % English language/hyphenation
\usepackage{amsmath,amsfonts,amsthm,amssymb} % Math packages
\usepackage{mathrsfs}    % More math packages
\usepackage{sectsty}  % Allows customizing section commands
\allsectionsfont{\centering \normalfont\scshape} % Make all section titles centered, the default font and small caps %remove this to left align section tites
\usepackage{hyperref} % Turns cross-references into hyperlinks,
                      % and defines \url and \href commands.
\usepackage{graphicx} % For embedding graphics files.
\usepackage{framed}   % For the "leftbar" environment used below.
\usepackage{ifthen}   % Used for the \powset command below.
\usepackage{lastpage} % for counting the number of pages
\usepackage[headsepline,footsepline,manualmark]{scrlayer-scrpage}
\usepackage[height=10in,a4paper,hmargin={1in,0.8in}]{geometry}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tikz}     % This is a powerful tool to draw vector
                      % graphics inside LaTeX. In particular, you can
                      % use it to draw graphs.
\usepackage{verbatim} % For the "verbatim" environment, in which
                      % special symbols can be used freely without
                      % confusing the compiler. (And it's typeset in
                      % a constant-width font.)
                      % Useful, e.g., for quoting code (or ASCII art).

%\numberwithin{table}{section} % Number tables within sections (i.e. 1.1, 1.2, 2.1, 2.2 instead of 1, 2, 3, 4)

\setlength\parindent{20pt} % Makes indentation for paragraphs longer.
                           % This makes paragraphs stand out more.

%----------------------------------------------------------------------------------------
%	VARIOUS USEFUL COMMANDS
%----------------------------------------------------------------------------------------
% The commands below might be convenient. For example, you probably
% prefer to write $\powset[2]{V}$ for the set of $2$-element subsets
% of $V$, rather than writing $\mathcal{P}_2(V)$.
% Notice that you can easily define your own commands like this.
% Caveat: Some of these commands need to be properly "guarded" when
% they occur in subscripts or superscripts. So you should not write
% $K_\CC$, but rather $K_{\CC}$.
\newcommand{\CC}{\mathbb{C}} % complex numbers
\newcommand{\RR}{\mathbb{R}} % real numbers
\newcommand{\QQ}{\mathbb{Q}} % rational numbers
\newcommand{\NN}{\mathbb{N}} % nonnegative integers
\newcommand{\PP}{\mathbb{P}} % positive integers
\newcommand{\Z}[1]{\mathbb{Z}/#1\mathbb{Z}} % integers modulo k
                                            % (syntax: "\Z{k}")
\newcommand{\ZZ}{\mathbb{Z}} % integers
\newcommand{\id}{\operatorname{id}} % identity map
\newcommand{\lcm}{\operatorname{lcm}}
% Lowest common multiple. For historical reasons, LaTeX has a \gcd
% command built in, but not an \lcm command. The preceding line
% rectifies that.
\newcommand{\set}[1]{\left\{ #1 \right\}}
% $\set{...}$ compiles to {...} (set-brackets).
\newcommand{\abs}[1]{\left| #1 \right|}
% $\abs{...}$ compiles to |...| (absolute value, or size of a set).
\newcommand{\tup}[1]{\left( #1 \right)}
% $\tup{...}$ compiles to (...) (parentheses, or tuple-brackets).
\newcommand{\ive}[1]{\left[ #1 \right]}
% $\ive{...}$ compiles to [...] (Iverson bracket, aka truth value; also, set of first n integers).
\newcommand{\floor}[1]{\left\lfloor #1 \right\rfloor}
% $\floor{...}$ compiles to |_..._| (floor function).
\newcommand{\underbrack}[2]{\underbrace{#1}_{\substack{#2}}}
% $\underbrack{...1}{...2}$ yields
% $\underbrace{...1}_{\substack{...2}}$. This is useful for doing
% local rewriting transformations on mathematical expressions with
% justifications. For example, try this out:
% $ \underbrack{(a+b)^2}{= a^2 + 2ab + b^2 \\ \text{(by the binomial formula)}} $
\newcommand{\powset}[2][]{\ifthenelse{\equal{#2}{}}{\mathcal{P}\left(#1\right)}{\mathcal{P}_{#1}\left(#2\right)}}
% $\powset[k]{S}$ stands for the set of all $k$-element subsets of
% $S$. The argument $k$ is optional, and if not provided, the result
% is the whole powerset of $S$.
\newcommand{\horrule}[1]{\rule{\linewidth}{#1}} % Create horizontal rule command with 1 argument of height
\newcommand{\nnn}{\nonumber\\} % Don't number this line in an "align" environment, and move on to the next line.

%----------------------------------------------------------------------------------------
%	MAKING SUMMATION SIGNS ALWAYS PUT THEIR BOUNDS ABOVE AND BELOW
%	THE SIGN
%----------------------------------------------------------------------------------------
% The following are hacks to ensure that sums (such as
% $\sum_{k=1}^n k$) always put their bounds (i.e., the $k=1$ and the
% $n$) underneath and above the sign, as opposed to on its right.
% Same for products (\prod), set unions (\bigcup) and set
% intersections (\bigcap). Remove the 8 lines below if you do not want
% this behavior.
\let\sumnonlimits\sum
\let\prodnonlimits\prod
\let\cupnonlimits\bigcup
\let\capnonlimits\bigcap
\renewcommand{\sum}{\sumnonlimits\limits}
\renewcommand{\prod}{\prodnonlimits\limits}
\renewcommand{\bigcup}{\cupnonlimits\limits}
\renewcommand{\bigcap}{\capnonlimits\limits}

%----------------------------------------------------------------------------------------
%	ENVIRONMENTS
%----------------------------------------------------------------------------------------
% The incantations below define how theorem environments
% (\begin{theorem} ... \end{theorem}) and their likes will look like.
\newtheoremstyle{plainsl}% <name>
  {8pt plus 2pt minus 4pt}% <Space above>
  {8pt plus 2pt minus 4pt}% <Space below>
  {\slshape}% <Body font>
  {0pt}% <Indent amount>
  {\bfseries}% <Theorem head font>
  {.}% <Punctuation after theorem head>
  {5pt plus 1pt minus 1pt}% <Space after theorem headi>
  {}% <Theorem head spec (can be left empty, meaning `normal')>

% Environments which make the text inside them slanted:
\theoremstyle{plainsl}
  \newtheorem{theorem}{Theorem}[section]
  \newtheorem{proposition}[theorem]{Proposition}
  \newtheorem{lemma}[theorem]{Lemma}
  \newtheorem{corollary}[theorem]{Corollary}
  \newtheorem{conjecture}[theorem]{Conjecture}
% Environments that don't:
\theoremstyle{definition}
  \newtheorem{definition}[theorem]{Definition}
  \newtheorem{example}[theorem]{Example}
  \newtheorem{exercise}[theorem]{Exercise}
  \newtheorem{examples}[theorem]{Examples}
  \newtheorem{algorithm}[theorem]{Algorithm}
  \newtheorem{question}[theorem]{Question}
 \theoremstyle{remark}
  \newtheorem{remark}[theorem]{Remark}
\newenvironment{statement}{\begin{quote}}{\end{quote}}
\newenvironment{fineprint}{\begin{small}}{\end{small}}

%----------------------------------------------------------------------------------------
%	METADATA
%----------------------------------------------------------------------------------------
\newcommand{\myname}{Darij Grinberg} % ENTER YOUR NAME HERE
\newcommand{\myid}{00000000} % ENTER YOUR UMN ID HERE
\newcommand{\mymail}{dgrinber@umn.edu} % ENTER YOUR EMAIL HERE
\newcommand{\psetnumber}{2} % ENTER THE NUMBER OF THIS PSET HERE

%----------------------------------------------------------------------------------------
%	HEADER AND FOOTER
%----------------------------------------------------------------------------------------
\ihead{Solutions to homework set \#\psetnumber} % Page header left
\ohead{page \thepage\ of \pageref{LastPage}} % Page header right
\ifoot{\myname, \myid} % left footer
\ofoot{\mymail} % right footer

%----------------------------------------------------------------------------------------
%	TITLE SECTION
%----------------------------------------------------------------------------------------
\title{
\normalfont \normalsize
\textsc{University of Minnesota, School of Mathematics} \\ [25pt] % Your university, school and/or department name(s)
\horrule{0.5pt} \\[0.4cm] % Thin top horizontal rule
\huge Math 4281: Introduction to Modern Algebra, \\
Spring 2019:
Homework \psetnumber\\% The assignment title
\horrule{2pt} \\[0.5cm] % Thick bottom horizontal rule
}
\author{\myname}

\begin{document}

\maketitle % Print the title

\begin{center} % Delete this if you want to save space!
{\large due date: \textbf{Wednesday, 13 February 2019} at the beginning of class, \\
or before that by email or canvas.

Please solve \textbf{at most 3 of the 6 exercises}!}
\end{center}

%----------------------------------------------------------------------------------------
%	EXERCISE 1
%----------------------------------------------------------------------------------------
\horrule{0.3pt} \\[0.4cm]

\section{Exercise 1: gcd basics}

\subsection{Problem}

Prove the following:

\begin{enumerate}

\item[\textbf{(a)}]
If $a_1, a_2, b_1, b_2$ are integers satisfying $a_1 \mid b_1$
and $a_2 \mid b_2$, then
$\gcd\tup{a_1, a_2} \mid \gcd\tup{b_1, b_2}$.

\item[\textbf{(b)}]
If $a, b, c, s$ are integers, then
$\gcd\tup{sa, sb, sc} = \abs{s} \gcd\tup{a, b, c}$.

\end{enumerate}

\subsection{Solution}

[...]

%----------------------------------------------------------------------------------------
%	EXERCISE 2
%----------------------------------------------------------------------------------------
\horrule{0.3pt} \\[0.4cm]

\section{Exercise 2: Products of gcds}

\subsection{Problem}

Prove the following:

%\begin{enumerate}

%\item[\textbf{(a)}]
Any four integers $u, v, x, y$ satisfy
$\gcd\tup{u, v} \gcd\tup{x, y}
= \gcd\tup{ux, uy, vx, vy}$.
% Note to self: solb6.tex, Lemma 0.3, rename p and q as x and y.

%\item[\textbf{(b)}]
%Any three integers $a, b, c$ satisfy
%$\gcd\tup{a, b} \gcd\tup{a, c}
%= \gcd\tup{a \gcd\tup{a, b, c}, bc}$.

%\end{enumerate}

\subsection{Solution}

[...]

%----------------------------------------------------------------------------------------
%	EXERCISE 3
%----------------------------------------------------------------------------------------
\horrule{0.3pt} \\[0.4cm]

\section{Exercise 3: The gcd-lcm connection for three numbers}

\subsection{Problem}

Let $a, b, c$ be three integers.
Prove that $\lcm\tup{a, b, c} \gcd\tup{bc, ca, ab} = \abs{abc}$.

\subsection{Solution}

[...]

%----------------------------------------------------------------------------------------
%	EXERCISE 4
%----------------------------------------------------------------------------------------
\horrule{0.3pt} \\[0.4cm]

\section{Exercise 4: Divisibility tests for $3, 9, 11, 7$}

\subsection{Problem}

Let $n$ be a positive integer.
Let ``$d_k d_{k-1} \cdots d_0$'' be the decimal representation
of $n$;
this means that $d_0, d_1, \ldots, d_k$ are digits
(i.e., elements of $\set{0, 1, \ldots, 9}$) such that
$n = d_k 10^k + d_{k-1} 10^{k-1} + \cdots + d_0 10^0$.
The digits $d_0, d_1, \ldots, d_k$ are called the
\textit{digits of $n$}.

(Incidentally, the quickest way to find these digits
is by repeated division with remainder:
To obtain the decimal representation of $n \geq 10$, you
take the decimal representation of $n // 10$ and append
the digit $n \% 10$ at the end. Thus,
\[
 d_0 = n \% 10, \qquad
 d_1 = \tup{n // 10} \% 10, \qquad
 d_2 = \tup{\tup{n // 10} // 10} \% 10,
 \qquad \text{etc.}
\]
But in this exercise, you can just assume that the decimal
representation exists.)

\begin{enumerate}

\item[\textbf{(a)}]
Prove that $3 \mid n$
if and only if $3 \mid d_k + d_{k-1} + \cdots + d_0$.
(In other words, a positive integer $n$ is divisible
by $3$ if and only if the sum of its digits is
divisible by $3$.)

\item[\textbf{(b)}]
Prove that $9 \mid n$
if and only if $9 \mid d_k + d_{k-1} + \cdots + d_0$.
(In other words, a positive integer $n$ is divisible
by $9$ if and only if the sum of its digits is
divisible by $9$.)

\item[\textbf{(c)}]
Prove that $11 \mid n$
if and only if
$11 \mid \tup{-1}^k d_k + \tup{-1}^{k-1} d_{k-1} + \cdots + \tup{-1}^0 d_0$.
(In other words, a positive integer $n$ is divisible
by $11$ if and only if the sum of its digits in the
even positions minus the sum of its digits in the odd
positions is divisible by $11$.)

\item[\textbf{(d)}]
Let $q = d_k 10^{k-1} + d_{k-1} 10^{k-2} + \cdots + d_1 10^0$.
(Equivalently, $q = n // 10 = \dfrac{n-d_0}{10}$;
this is the number obtained from $n$ by dropping
the least significant digit.)
Prove that $7 \mid n$ if and only if $7 \mid q - 2 d_0$.

(This gives a recursive test for divisibility by $7$.)

\end{enumerate}

% Note to self: sol1.tex, problems 9-11.

\subsection{Solution}

[...]

%----------------------------------------------------------------------------------------
%	EXERCISE 5
%----------------------------------------------------------------------------------------
\horrule{0.3pt} \\[0.4cm]

\section{Exercise 5: A divisibility}

\subsection{Problem}

Let $n \in \NN$.
Prove that $7 \mid 3^{2n+1} + 2^{n+2}$.

\subsection{Solution}

[...]

%----------------------------------------------------------------------------------------
%	EXERCISE 6
%----------------------------------------------------------------------------------------
\horrule{0.3pt} \\[0.4cm]

\section{Exercise 6: A binomial coefficient sum}

\subsection{Problem}

Let $n \in \NN$. Prove that
\begin{align}
\sum_{k=0}^n \dbinom{-2}{k}
= \tup{-1}^n \tup{\tup{n+2} // 2} .
\end{align}

\subsection{Solution}

[...]

\begin{thebibliography}{99999999}                                                                                %

% Feel free to add your sources -- or copy some from the source code
% of the class notes ( http://www.cip.ifi.lmu.de/~grinberg/t/19s/notes.tex ).

% This is the bibliography: The list of papers/books/articles/blogs/...
% cited. The syntax is: "\bibitem[name]{tag}Reference",
% where "name" is the name that will appear in the compiled
% bibliography, and "tag" is the tag by which you will refer to
% the source in the TeX file. For example, the following source
% has name "GrKnPa94" (so you will see it referenced as
% "[GrKnPa94]" in the compiled PDF) and tag "GKP" (so you
% can cite it by writing "\cite{GKP}").

\bibitem[GrKnPa94]{GKP}Ronald L. Graham, Donald E. Knuth, Oren Patashnik,
\textit{Concrete Mathematics, Second Edition}, Addison-Wesley 1994.\\
See \url{https://www-cs-faculty.stanford.edu/~knuth/gkp.html} for errata.

\bibitem[Grinbe19]{detnotes}Darij Grinberg,
\textit{Notes on the combinatorial fundamentals of algebra},
10 January 2019. \\
\url{http://www.cip.ifi.lmu.de/~grinberg/primes2015/sols.pdf}
\\
The numbering of theorems and formulas in this link might shift
when the project gets updated; for a ``frozen'' version whose
numbering is guaranteed to match that in the citations above, see
\url{https://github.com/darijgr/detnotes/releases/tag/2019-01-10} .

\end{thebibliography}

\end{document}


\graphicspath{{RL/fig}}
\chapter{Reinforcement Learning}
\label{chap:RL}

In previous chapters we discussed methods which required knowledge about the model of the environment. This knowledge was embedded in the probability transition matrix P. Often times it is not possible to know the model of the environment beforehand. This is where reinforcement learning is useful because it does not require a model of the environment.

In reinforcement learning, the way an agent learns the best actions to take, is by experiencing sequences of states and actions, defined as an episode. When the agent reaches the final state, we say the episode has terminated. In this chapter we discuss two methods of reinforcement learning (Monte Carlo and temporal difference methods), both of which can be implemented via prediction or control methods. We will not cover the Monte Carlo control method, but it can be found in Sutton and Barto \cite{sutton_barto}. We begin in the next section with the prediction methods.
\section{Model-free prediction}
\subsection{Monte Carlo prediction}
According to Sutton and Barto the term Monte Carlo is generally used for
any estimation technique where a large portion of its operation is random \cite{sutton_barto}.
Sutton and Barto specifically describes Monte Carlo methods as solving reinforcement learning problems by averaging sample returns \cite{sutton_barto}.
Monte-Carlo methods require only experience, which it obtains by sampling sequences of states, actions and rewards. By sampling enough times, by the law of large numbers, eventually an accurate state-value function will be obtained  \cite{sutton_barto}.

Monte-Carlo policy evaluation works as follows. We define a total return S in state s as S(s) and a integer counter N(s) which represents the amount of times s is seen. We then initialize both S(s) and N(s) initialized to zero.
Then at time step t and state s we increment the counter N(s) = N(s)+1. This can be done in two ways, one being only incrementing N(s) the \textit{first} time state s is seen, the other being incrementing N(s) \textit{every} time states s is seen \cite{sutton_barto}.

In this chapter we use the method of incrementing the counter every instance that state s is seen.
Then we increment the total reward S(s) =  S(s) + $G_t$ every time the agent reaches state s.
Finally we calculate the state value estimate function as $V(s)=\frac{S(s)}{N(s)}$.
Then $V(s) \to V_\pi(s)$ as N(s) $\to \infty$,  $\forall$ s $\in$ S. In other words when we have seen all states infinitely many times, we will have obtained exactly $V_\pi(s)$. Although theoretically we need to visit each state infinitely many times, practically there is a finite time-step where $V(s)$ converges to $V_\pi(s)$.
In order to describe incremental Monte-Carlo updates, David Silver introduces the concept of an incremental mean $u_k$ \cite{David_Silver}. The derivation for $u_k$ from a standard mean is as follows:
\begin{align}
	u_k &= \frac{1}{k}\sum_{j=1}^{k}x_j\\
	&= \frac{1}{k}(x_k + \sum_{j=1}^{k-1}x_j) \nonumber\\
	&= \frac{1}{k}(x_k +(k-1)u_{k-1})\nonumber\\
	&= u_{k-1} + \frac{1}{k}(x_k - u_{k-1}).
	\label{eq:u_k}
\end{align}
Since V(s) is a mean, we break it up similarly to Equation \ref{eq:u_k}, with $u_{k-1}=V(s)$, $N(s)=t=k$ and return $G_t =x_k$. Substituting these variables into equation \ref{eq:u_k} yields that at every time-step t:

\begin{align}
	N(S_t) &\gets N(S_t) + 1 \\
	V(S_t) &\gets V(S_t) + \frac{1}{N(S_t)}(G_t - V(S_t)).
\end{align}
Sutton and Barto in \cite{sutton_barto} sets $\frac{1}{N(S_t)}=\alpha$, where $0<\alpha<1$ resulting in
\begin{equation}
	V(S_t) \gets V(S_t) + \alpha(G_t - V(S_t)).
	\label{eq:monte_carlo}
\end{equation}
Equation \ref{eq:monte_carlo} gives us a way to find the optimal state-value function $V_\pi(S_t)$ by iteratively updating $V(S_t)$. Sutton and Barto names Equation \ref{eq:monte_carlo}, $constant-\alpha$ Monte Carlo.

\subsection{Temporal Difference (TD) Learning}
Temporal difference (TD) learning uses a combination of Monte Carlo and dynamic programming (DP) ideas. TD is one of the unique aspects of reinforcement learning, unlike Monte Carlo and DP which have more general applications besides reinforcement learning \cite{sutton_barto}. Like Monte Carlo TD learns without requiring a model of the environment. Because TD is a reinforcement method, it can be split into prediction and control methods as was stated in the introduction of this chapter. Here in this section we only describe the prediction method, while the next section will discuss two TD control methods, namely SARSA and Q-learning. The aim of TD learning is to learn a value function $v_\pi$ from experience by following policy $\pi$.
The simplest form of TD is replacing the return $G_t$ in Equation \ref{eq:monte_carlo} from the previous section as follows:
\begin{equation}
	V(S_t) = V(S_t) + \alpha({\color{red}R_{t+1} +  \gamma V(S_{t+1})} - V(S_t)).
	\label{eq:monte_carlo_TD}
\end{equation}
Where $[{\color{red}R_{t+1} +  \gamma V(S_{t+1})}]$ is known as the TD target, the value which $V(S_t)$ tends to as $t \to \infty$ and
$[{\color{red}R_{t+1} +  \gamma V(S_{t+1})} - V(S_t)]$ is known as the TD error. The aim of TD learning is to reduce the TD error to zero and update $V(S_t)$ to the estimated return ${\color{red}R_{t+1} +  \gamma V(S_{t+1})}$.
Suttton and Barto mathematically describes the relationship between MC and TD using what they define as the n-step return $G_{t}^{(n)}$ described by:
\begin{equation}
	G_{t}^{(n)} = R_{t+1} + \gamma R_{t+2} + ... + \gamma^{n-1}R_{t+n} + \gamma^{n}V(S_{t+1}).
	\label{eq:n_step_gt}
\end{equation}
For clarity we show
$G_{t}^{(n)}$ for a few values of n as follows:\\
\begin{align}
	 {\color{red}(TD)} n &= 1: G_{t}^{(1)} = R_{t+1}+\gamma V(S_{t+1}) \label{eq:td} \\
	n &= 2 : G_{t}^{(2)} = R_{t+1}+ \gamma R_{t+2}+\gamma^2 V(S_{t+2}) \\
	{\color{red}(MC)} n &= \infty : G_{t}^{(\infty)} = R_{t+1}+ \gamma R_{t+2}+...+\gamma^{T-1} R_T. \label{eq:mc_td}
\end{align}
What can be seen is, as n $\to$ $\infty$, the return in TD learning turns into the same form as in Monte Carlo. Hence Monte Carlo can be said to be a special case of TD learning.
Substituting $G_{t}^{(n)}$ into Equation \ref{eq:monte_carlo_TD} yields what Sutton and Barto calls n-step TD as:
\begin{equation}
	V(S_t) \gets V(S_t) + \alpha(G_t^{(n)} - V(S_t)).
	\label{eq:n_step_TD}
\end{equation}
What we now have is a method to control how many steps we want an agent to look ahead. This is useful because $V(S_t)$ can converge more quickly to $V_\pi(S_t)$ if n is chosen well. Normally one has to test for different values of n to see which value works best for the specific problem at hand.
In the next section we no longer look at estimating the state-value function $V_\pi(S_t)$, but instead work with \textit{state-action} pairs in order to find the optimal state-action function $q_*(s,a)$.

\section{Model-free control}
Model-free control can be implemented in two ways. \textbf{On-policy learning}, which lets the agent learn characteristics of a policy $\pi$ by sampling from $\pi$ and
\textbf{off-policy learning}, learning characteristics of policy $\pi$ by sampling another policy.

\noindent In order to find the optimal state-action function $q_*(s,a)$, we require a method of evaluating a state-action policy. Previously, when using greedy policy improvement over the state-value function V(s) we had in Equation \ref{pi'}:
\[\pi^{'}(s) = \max\limits_{a \in A}(R^{a}_s+\gamma\sum_{s'\in S}P^{a}_{ss'}v_*(s'))\]
Sutton and Barto applies greedy policy improvement over the state action function Q(s,a) as follows:
\begin{equation}
	\pi^{'}(s) = \max\limits_{a \in A}Q(s,a).
	\label{eq:pi'}
\end{equation}
One should notice that to find the optimal policy (by using Equation \ref{eq:pi'} for policy improvement), we no longer need a model of the environment. In other words there is no dependency on the probability matrix $P^{a}_{ss'}$. In the next section we will see that there is a problem if an agent only uses Equation \ref{eq:pi'} for policy improvement. In the same section a solution is then given to the problem.

\subsection{Exploration and exploitation}
In reinforcement learning, exploration refers to an agent taking a random action and exploitation when the agent acts greedily. When an agent exploits it can obtain a larger return and when it explores it gets a more accurate estimate of the action-value function. The agent can not simultaneously explore and exploit which results in what is known as the exploration-exploitation dilemma \cite{e_greedy_Michel_T}.
To ensure that the agent explores occasionally instead of always taking the greedy action with respect to the state-action function Q(s,a), we let the agent act randomly with a probability $\epsilon$. This can mathematically be expressed as:
\begin{align}
	\pi(a|s)=\begin{cases}
		1-\epsilon, & \text{if $a^* = \argmax\limits_{a \in A}Q (s,a)$}\\
		\epsilon, & \text{otherwise take a random action}
	\end{cases}
	\label{eq:pi_epsilon-greedy}
\end{align}
Equation \ref{eq:pi_epsilon-greedy} is known as the $\epsilon$-greedy policy.
Using the $\epsilon$-greedy policy results in the agent exploring occasionally, which allows for faster convergence if $\epsilon$ is chosen appropriately. Usually, an $\epsilon$ of 0.1 is a good starting value.

\subsection{SARSA: On policy TD control}
\begin{figure}[!htb]
	\centering
	\includegraphics[width=0.5\linewidth]{RL/fig/sarsa_state_diagram.pdf}
	\caption{SARSA backup diagram representation\cite{David_Silver}}
	\label{fig:sarsa_state_diagram}
\end{figure}

We now look at using TD methods for control. The backup diagram for SARSA is shown in Figure \ref{fig:sarsa_state_diagram}, where we visually see a sequence of a State-Action-Reward-State-Action (SARSA). This is where the name SARSA originates from. By following the same process that we used in the TD prediction section, we can arrive to an equation similar to Equation \ref{eq:monte_carlo_TD}. Instead of updating the state-value function $V(S)$, we update the state-action function $Q(S,A)$. For every state action pair, we can calculate the state-action function Q(S,A) using TD learning as follows:

\begin{equation}
	Q(S,A) = Q(S,A) + \alpha[R+\gamma Q(S',A') - Q(S,A)].
	\label{eq:sarsa_update}
\end{equation}
Where $R+\gamma Q(S',A')$ is the TD target. Similar to Monte Carlo prediction where a n-step state value return $G^n_t$ was defined in Equation \ref{eq:n_step_gt}, Sutton and Barto also defines n-step Q-return as:
\begin{equation}
	q_{t}^{(n)} = R_{t+1} + \gamma R_{t+2} +...+ \gamma^{n-1}R_{t+n}+\gamma^{n}Q(S_{t+n}).
	\label{eq:q_return}
\end{equation}
Although Sutton and Barto do not explicitly mention it, but $q_{t}^{(n)}$ is assumed to be zero for all $n < 1$.
The goal of n-step SARSA is to update Q(s,a) towards the n-step Q-return $q_t^{(n)}$,
\begin{equation}
	Q(S_t,A_t) = Q(S_t,A_t) + \alpha[q_{t}^{(n)} - Q(S_t,A_t)].
	\label{eq:sarsa_n_step}
\end{equation}
Sutton and Barto states that SARSA always converges towards an optimal action-value function, as long as all state–action
pairs are visited enough times \cite{sutton_barto}. One can also deduce this fact from the law of large numbers, as was stated in the Monte Carlo prediction section.

Algorithm \ref{alg:SARSA} below, adapted from Sutton and Barto, shows the pseudo-code which can be used to implement SARSA. Lines 4 and 7 in algorithm \ref{alg:SARSA} is policy improvement. Line 8 is the SARSA update line corresponding to Equation \ref{eq:sarsa_n_step}, with n=1. Later in chapter \ref{chap:Experiments_and_Results} we will apply this algorithm, where will adjust the hyper parameters to see how they affect the agents learning process.

\begin{algorithm}
	\SetKwInOut{Input}{Input}
	\SetKwInOut{Output}{Output}
	\caption{SARSA algorithm to find optimal state action function $Q_*(S,A)$}
	\underline{function SARSA $(\alpha,\gamma,\epsilon)$}\;
	\Input{Three nonnegative integers, learning rate $\alpha$, discount $\gamma$ and exploration probability $\epsilon$}
	\Output{$Q(S,A)$}

	\begin{algorithmic}[1]
		\State Initialize Q(s,a), $\forall$s$\in$S, a$\in$A(s) arbitrarily and Q(terminal state, \{all actions\}) = 0
		\State for each episode:
			\State \hskip2.0em Initialize S
			\State \hskip2.0em Choose A from S using $\epsilon$-greedy policy derived from Q
			\State \hskip2.0em for each step in episode:
				\State \hskip6.0em Take action A, observe R,S'
				\State \hskip6.0em  Choose A' from S' using $\epsilon$-greedy policy derived from Q
				\State \hskip6.0em Q(S,A) $\gets$ Q(S,A) + $\alpha$[R + $\gamma$(Q,S',A') - Q(S,A)]
				\State \hskip6.0em S $\gets$ S'; A $\gets$ A';
			\State \hskip2.0em Until S is terminal
	\end{algorithmic}
	\label{alg:SARSA}
\end{algorithm}
\newpage
\subsection{Q-learning: Off policy TD control}
\begin{figure}[!htb]
	\centering
	\includegraphics[width=0.5\linewidth]{RL/fig/q_learning_state_diagram.pdf}
	\caption{Q-learning backup diagram where the red line indicates a max across the actions that can be taken from state S'\cite{David_Silver}}
	\label{fig:q_learning_state_action_diagram}
\end{figure}
\begin{equation}
	Q(S,A) \leftarrow Q(S,A) + \alpha(R + \gamma\max\limits_{a'}Q(S',a') - Q(S,A)).
	\label{eq:q_learning_update_function}
\end{equation}
We describe now Q-learning which falls under off policy learning. What we have been dealing with up to this chapter, was on-policy learning. Q-learning is an off-policy method, because it not only updates Q(S,A), but it also updates the TD target $[R + \gamma\max\limits_{a'}Q(S',a')]$. By comparing Figure \ref{fig:q_learning_state_action_diagram} and Figure \ref{fig:sarsa_state_diagram}, we can see that the only difference between Q-learning and SARSA is that Q-learning takes the action a', which results in the maximum state-action function $Q(S',a')$. We will also apply the Q-learning algorithm in Chapter \ref{chap:Experiments_and_Results}. In this chapter we have covered various methods of reinforcement learning, of which we will only be applying TD control methods, namely SARSA and Q-learning. In the next chapter we will also discuss what effects each hyper parameter has on th agents learning process.\\
\begin{algorithm}[H]
	\SetKwInOut{Input}{Input}
	\SetKwInOut{Output}{Output}
	\caption{Q-learning algorithm to find optimal state action function $Q_*(S,A)$}
	\underline{function Q-learning $(\alpha,\gamma,\epsilon)$}\;
	\Input{Three nonnegative integers, learning rate $\alpha$, discount $\gamma$ and exploration probability $\epsilon$}
	\Output{$Q(S,A)$}

	\begin{algorithmic}[1]
		\State Initialize Q(s,a), $\forall$s$\in$S, a$\in$A(s) arbitrarily and Q(terminal state, \{all actions\}) = 0
		\State for each episode:
		\State \hskip2.0em Initialize S
		\State \hskip2.0em for each state in episode:
		\State \hskip6.0em Choose A from S using policy derived from Q (eg., $\epsilon$-greedy)
		\State \hskip6.0em Take action A, observe R,S'
		\State \hskip6.0em Q(S,A) $\gets$ Q(S,A) + $\alpha$[R + $\gamma$$\max\limits_{a \in A}$(Q,S',a) - Q(S,A)]
		\State \hskip6.0em S $\gets$ S';
		\State \hskip2.0em Until S is terminal
	\end{algorithmic}
	\label{alg:Q_learning}
\end{algorithm}


\documentclass[11pt,oneside]{article}	%use"amsart"insteadof"article"forAMSLaTeXformat
\usepackage{geometry}		%Seegeometry.pdftolearnthelayoutoptions.Therearelots.
\geometry{letterpaper}		%...ora4paperora5paperor...
%\geometry{landscape}		%Activateforforrotatedpagegeometry
%\usepackage[parfill]{parskip}		%Activatetobeginparagraphswithanemptylineratherthananindent
\usepackage{graphicx}				%Usepdf,png,jpg,orepsßwithpdflatex;useepsinDVImode
								%TeXwillautomaticallyconverteps-->pdfinpdflatex
\usepackage{amssymb}
\usepackage[colorlinks]{hyperref}

%----macros begin---------------------------------------------------------------
\usepackage{color}
\usepackage{amsthm}

\def\conv{\mbox{\textrm{conv}\,}}
\def\aff{\mbox{\textrm{aff}\,}}
\def\E{\mathbb{E}}
\def\R{\mathbb{R}}
\def\Z{\mathbb{Z}}
\def\tex{\TeX}
\def\latex{\LaTeX}
\def\v#1{{\bf #1}}
\def\p#1{{\bf #1}}
\def\T#1{{\bf #1}}

\def\vet#1{{\left(\begin{array}{cccccccccccccccccccc}#1\end{array}\right)}}
\def\mat#1{{\left(\begin{array}{cccccccccccccccccccc}#1\end{array}\right)}}

\def\lin{\mbox{\rm lin}\,}
\def\aff{\mbox{\rm aff}\,}
\def\pos{\mbox{\rm pos}\,}
\def\cone{\mbox{\rm cone}\,}
\def\conv{\mbox{\rm conv}\,}
\newcommand{\homog}[0]{\mbox{\rm homog}\,}
\newcommand{\relint}[0]{\mbox{\rm relint}\,}

\def\vint{\int\!\!\int\!\!\int}
\def\sint{\int\!\!\int}
\def\isum{\sum_{i=1}^n}
\def\asum{\sum_{\alpha=0}^n}
\def\bsum{\sum_{\beta=0}^m}
\def\csum{\sum_{\gamma=0}^p}
\def\sssum{\asum\bsum\csum}
\def\XXa{\sum_{\tau\in T}  |{\bf a} \times {\bf b}| }
\def\XXb{\pmatrix{\alpha\cr  h\cr}}
\def\XXc{\pmatrix{\beta\cr k\cr}}
\def\XXd{\pmatrix{\gamma\cr m\cr}}
\def\XXe{\pmatrix{h\cr i\cr}}
\def\XXf{\pmatrix{k\cr j\cr}}
\def\XXg{\pmatrix{m\cr l\cr}}
\def\XXh{\pmatrix{p\!\!+\!\!1\cr h\cr}}

%----macros end-----------------------------------------------------------------

\title{Finite domain integration of polynomials
\footnote{This document is part of the \emph{Linear Algebraic Representation with CoChains} (LAR-CC) framework~\cite{cclar-proj:2013:00}. \today}
}
\author{Alberto Paoluzzi}
%\date{}							%Activatetodisplayagivendateornodate

\begin{document}
\maketitle
\nonstopmode

\begin{abstract}
In order to plan or control the static/dynamic behaviour of models in CAD applications, it is often necessary to evaluate integral properties ot solid models (i.e. volume, centroid, moments of inertia, etc.). This module deals with the exact evaluation of inertial properties of homogeneous polyhedral objects.
\end{abstract}

\tableofcontents

\section{Introduction}

A finite integration method from\cite{CattaniP-BIL1990} is developed here the computation of various order monomial integrals over polyhedral solids and surfaces in 3D space. The integration method can be used for the exact evaluation of domain integrals of trivariate polynomial forms.

\section{Integration of polynomials over polyhedral domains}
\index{Integration}
\label{sec:cattani}

Here we summarize from~\cite{CattaniP-BIL1990} an exact and symbolic solution
both to the surface and volume integration of polynomials, by using a
triangulation of the volume boundary.   The evaluation of
surface and volume integrals is achieved by transforming them into line
integrals over the boundary of every 2-simplex of a domain
triangulation.  A different approach to integration, using a
decomposition into volume elements induced by a boundary triangulation
is given in~\cite{Kajiya:84} where a closed formula for volume
integration over polyhedral volumes, by decomposing the solid into a set
of solid tetrahedra, but such a method cannot be used for surface
integrations.

\paragraph{Problem statement}\index{Integration!problem}

The finite method~\cite{CattaniP-BIL1990} to compute double and triplet integrals
of monomials over linear regular polyhedra in $\R^3$ is discussed.
In particular, this method enables practical formulae for the
exact evaluation of integrals to be achieved:
\begin{eqnarray}
II_{S} \equiv  \sint_{S} f({\bf p})\,dS, \qquad
III_{P} \equiv  \vint_{P} f({\bf p})\,dV,\label{2}
\end{eqnarray}
where $S$, and $P$ are linear and regular 2- or 3-polyhedra in $\R^3$,
$dS$ and $dV$ are the differential surface
and the differential volume.  The integrating function
is a  trivariate polynomial
\[
f({\bf p})
=
\asum\bsum\csum
a_{\alpha\beta\gamma} x^\alpha y^\beta z^\gamma,
\]
where $\alpha,\beta,\gamma$ are non-negative integers.

Since the extension to $f({\bf p})$ is straightforwardly given by the
linearity of integral operator, we may focus on the calculation of
integrals of monomials:
\begin{eqnarray}
II_{S}^{\alpha\beta\gamma} \equiv
\sint_{S} x^\alpha y^\beta z^\gamma \,dS,\qquad
III_{P}^{\alpha\beta\gamma} \equiv
\vint_{P} x^\alpha y^\beta z^\gamma \,dV.
\label{7}
\end{eqnarray}

\paragraph{Algorithm preview}\index{Algorithm!integration}

Surface integrals are computed as a summation of integrals over a
triangulation of the surface.  Any triangle is mapped into the unit
triangle in the 2-space of parameters, where integrals
of monomials become particularly
simple.  Then formulae for integrals over polyhedral volumes are
given.  They are easily derived by transforming volume integrals in
surface integrals.  It is possible to show that such integrals are
computable in polynomial time, and that inertia moments are computable
in $O(E)$ time, $E$ being the number of edges of the solid model of
the integration domain.

A very important feature of the integration formulae presented here is
that they can also be used with a partial model of a polyhedron,
consisting of the collection of its face loops.  Loops are oriented
counter-clockwise if external, clockwise if internal to another loop.
Such a  model, without explicit storage of face adjacencies, is
very frequently adopted in Computer Graphics.

In this case it is sufficient to consider any $n+1$-sided (also
unconnected or multiply connected) face as topological sum of $n-1$
oriented triangles $t_i$, with vertices $\langle v_0, v_i,
v_{i+1}\rangle$, where $1\le i\le n-1$ .  In applying formulae
(\ref{19}) or (\ref{27}) to such a set of triangles, any edge that
does not belong to the original polygon will be computed twice, in the
two opposite directions.  These contributions to the whole integral
will mutually cancel each other out, as they correspond to pairs of
line integrals evaluated along opposite paths.

\paragraph{Surface integration}\index{Surface!integration}

We call \emph{structure product}  the integral of a monomial over a
simplicial complex.  Exact formulae for
 structure products over  n-sided polygons in 2-space,
the unit triangle in 2-space, and an arbitrary triangle in 3-space,
are derived in the following.
Structure products  are a generalization of the usual products
and moments of inertia, that can be obtained from (\ref{7}) by assuming
$\alpha + \beta + \gamma\leq 2$.

\paragraph{Polygon integrals}\index{Polygon integrals}

A structure product over a polygon $\pi$ in the plane $xy$ is
\begin{equation} \label{9}
II_\pi^{\alpha\beta} = \sint_{\pi} x^\alpha y^\beta \,dS,
\qquad \alpha,\beta\geq 0, integers.
\end{equation}
Such integrals can be exactly expressed, when
$\pi$ is a  polygon with $n$ oriented edges, as:
\begin{equation} \label{10}
II_\pi^{\alpha\beta} =
{1\over{\alpha \!+\!1}}
\isum\sum_{h=0}^{\alpha+1}
{\alpha+1\choose  h}
x_i^{\alpha+1-h} X_i^h
\sum_{k=0}^{\beta}
{{{\beta\choose  k}}\over{h+k+1}}
y_i^{\beta-k} Y_i^{k+1}
\end{equation}
where ${\bf p}_i=(x_i, y_i)$, $X_i=x_{i+1}-x_{i}$, $Y_i=y_{i+1}-y_{i}$
and ${\bf p}_{n+1} = {\bf p}_1$.  The derivation of the formula
(\ref{10}) is based on the application of Green's theorem and on
Newton's expression for binomial powers.

\paragraph{Unit triangle integrals}\index{Unit triangle integrals}

The general formula (\ref{10})  can be specialized
for the unit triangle
$\tau' = \langle {\bf w}_o, {\bf w}_a, {\bf w}_b \rangle$, with vertices
\begin{equation} \label{14}
{\bf w}_o=(0,0),\qquad {\bf w}_a=(1,0),\qquad {\bf w}_b=(0,1),
\end{equation}
getting a very simplified expression.
With some  algebraic manipulations, we obtain\footnote{
$II_\pi^{\alpha\beta}$ is substituted,
when referred to the unit triangle, by  the symbol
$II^{\alpha\beta}$.
}
\begin{equation}\label{15}
II^{\alpha\beta} =
{1\over{\alpha +1}}
\sum_{h=0}^{\alpha+1}
{\alpha+1\choose  h}
{{(-1)^h}\over{h+\beta+1}},
\end{equation}
which reduces, for $\alpha=\beta=0$, to the area of the triangle (\ref{14}):
$II^{0 0} = 1/2$.

\paragraph{Triangle integrals}\index{Triangle!integrals}

In the following we derive the general expression for structure products
evaluated on  an arbitrary triangle
$\tau=\langle {\bf v}_o,{\bf v}_a,{\bf v}_b \rangle$  of
the 3-space $xyz$, defined by ${\bf v}_o=(x_o,y_o,z_o)$ and
by the vectors ${\bf a}={\bf v}_a-{\bf v}_o$ and
${\bf b}={\bf v}_b-{\bf v}_o$.
The parametric equation of its embedding plane is:
\begin{equation}\label{11}
{\bf p} = {\bf v}_o+ u \,{\bf a}+ v\, {\bf b},
\end{equation}
where the area element is
\begin{equation}\label{12}
d\tau=
|J|\,du\,dv=
\bigg| {{\partial {\bf p}}\over {\partial u}}
\times {{\partial {\bf p}}\over {\partial v}} \bigg|\,du\,dv=
|{\bf a}\times{\bf b}|\,du\,dv.
\end{equation}
A structure product over a triangle $\tau$ in 3-space can be transformed
by a coordinates transformation, as follows:
\begin{equation}\label{13}
II_\tau^{\alpha\beta\gamma}=
\sint_\tau x^\alpha y^\beta z^\gamma \,d\tau
=
|{\bf a}\times{\bf b}|
\sint_{\tau'} {x^{\alpha}(u,v)} {y^{\beta}(u,v)} {z^{\gamma}(u,v)}\,du\,dv,
\end{equation}
where $\tau'$ is the $uv$ domain that corresponds to $\tau$ under the
coordinate transformation (\ref{11}).  In this case we have (the proof
is given in~\cite{CAD:90}):
\begin{eqnarray}
II_\tau^{\alpha\beta\gamma}&=&
|{\bf a}\times{\bf b}|
\sum_{h=0}^{\alpha}
{\alpha\choose h}
x_o^{\alpha-h}
\sum_{k=0}^{\beta}
{\beta \choose k}
y_o^{\beta-k}
\sum_{m=0}^{\gamma}
{\gamma \choose m}
z_o^{\gamma-m} \cdot \nonumber\\ & &
\cdot\sum_{i=0}^{h}
{h\choose i}
a_x^{h-i} b_x^i
\sum_{j=0}^{k}
{k\choose j}
a_y^{k-j} b_y^j
\sum_{l=0}^{m}
{m\choose l}
a_z^{m-l} b_z^l
\,II^{\mu\nu} \label{17}  ,
\end{eqnarray}
where $\mu=(h+k+m)-(i+j+l), \nu=(i+j+l)$, and $II^{\mu\nu}$ is  a structure product
over the triangle (\ref{14}), as given by formula (\ref{15}).
Of course the area of a triangle $\tau$
is:
\begin{equation}\label{18}
II_\tau^{000}=\sint_\tau \,d\tau=
|{\bf a}\times{\bf b}|\,
II^{00}=
{{|{\bf a}\times{\bf b}|}\over 2}.
\end{equation}

\paragraph{Surface integrals}\index{Surface!integrals}

In conclusion, a structure product over a polyhedral surface $S$,
open or closed, is a summation  of structure products (\ref{17}) over
the 2-simplices of a triangulation $K_2$ of $S$:
\begin{equation}\label{19}
II_S^{\alpha\beta\gamma}=
\sint_S x^\alpha y^\beta z^\gamma \,dS=
\sum_{\tau\in K_2}
II_\tau^{\alpha\beta\gamma}.
\end{equation}

\paragraph{Volume integration}\index{Volume integration}

Let $P$ be a three-dimensional polyhedron bounded by a polyhedral
surface $\partial P$. The regularity of the integration domain and the continuity of
the integrating function enable us to apply the divergence theorem, which can be
briefly summarized, for
a vector field ${\bf F}={\bf F}({\bf p})$ as:
\begin{equation}\label{21}
\vint_P \nabla\cdot {\bf F} \,dx\,dy\,dz=
\sint_{\partial P} {\bf F}\cdot{\bf n} \,dS=
\sum_{\tau\in K_2}
\sint_{\tau} {\bf F}\cdot{\bf n}_\tau \,d\tau,
\end{equation}
where ${\bf n}$ is the outward vector normal to the surface
portion $dS$, and hence ${\bf n}_\tau={{{\bf a}\times{\bf b}}/{|{\bf a}\times{\bf b}|}}$.

As the function $x^\alpha y^\beta z^\gamma$ equates the divergence of
the vector field $ {\bf F}= ({{x^{\alpha+1} y^\beta
z^\gamma}/{\alpha+1}}, 0, 0) $, an expression for
$III_P^{\alpha\beta\gamma}$ is easily derived, which depends only on
the 1-simplices of a triangulation of the domain boundary and on the
structure products over its 2-simplices.

As a matter of fact, we have:
\begin{eqnarray}
III_P^{\alpha\beta\gamma}
&=&
\vint_P x^\alpha y^\beta z^\gamma \,dx\,dy\,dz\nonumber\\
&=&
\vint_P
{\partial\over{\partial x}}\left(
{{1}\over{\alpha+1}}
x^{\alpha+1} y^\beta z^\gamma \right)\,dx\,dy\,dz\nonumber\\
&=&
{1\over{\alpha+1}}
\sum_{\tau'\in {K'}_2}
({\bf a}\times{\bf b})_x
\sint_{\tau'}
x^{\alpha+1} y^\beta z^\gamma\,du\,dv.\label{26}
\end{eqnarray}

Taking into account  equations (\ref{12}) and (\ref{13}), we can substitute the integral in
the previous equation, getting finally:
\begin{equation}\label{27}
III_P^{\alpha\beta\gamma}
=
{1\over{\alpha+1}}
\sum_{\tau\in {K}_2}
\left[
{{({\bf a}\times{\bf b})_x}\over{|{\bf a}\times{\bf b}|}}
\right]_\tau
II_\tau^{\alpha+1,\beta,\gamma}
\end{equation}
where the surface integrals are evaluated by using the formula (\ref{17}).

\paragraph{Computation of inertia has linear complexity}
\index{Computation!inertia}

Surface and volume integrals over linear polyhedra are computable in
linear time.  In particular, \emph{surface and volume integrals of
a monomial $x^\alpha y^\beta z^\gamma$ over a linear 2-or 3-polyhedron
are computable in $O(\alpha^3\beta^2\gamma^2 E)$ time,  $E$ being the
number of edges of the polyhedron.  }

In fact for the surface and volume integrals it is very easy to see,
from inspection of the given equations, that both integrals may be
evaluated in $O(\alpha^3\beta^2\gamma^2 T)$ time, $T$ being the number
of triangles of a minimal triangulation of the domain.  It is easy to
show that the relation $T=2E-2F<2E$ holds between the number $T$ of
triangles of a minimal triangulation of a polyhedron boundary and the
numbers $E$ and $F$ of its original edges and faces respectively.
When all triangle faces are triangular, the relation reduces to
$T={2\over 3} E$.

This property is very important for Computer-Aided Design and
Ro\-bo\-tics applications: it demonstrates that the inertia tensor of a
linear polyhedral solid is easy to compute.  It directly implies that
\emph{the inertia tensor of a linear polyhedron is computable in $O(E)$
time.  }
As a matter of fact the elements of the inertia matrix of a
homogeneous object $B$, namely its \emph{mass} $M$, \emph{first moments}
$M_x$,$M_y$,$M_z$, \emph{products of inertia} $M_{xy},M_{yz},M_{zx}$,
and \emph{second moments} $M_{xx},M_{yy},M_{zz}$, can be all expressed
as
\begin{equation}\label{31}
\rho \int_B x^\alpha y^\beta z^\gamma \,dV,
\end{equation}
where  $\rho$
is the constant density, and where $0\le\alpha+\beta+\gamma\le
2$.  Being $\alpha,\beta,\gamma$ bounded, the assertion follows from
the previous claim.

\section{Implementation}

\subsection{High-level interfaces}

\paragraph{Surface and volume integrals}
%-------------------------------------------------------------------------------
@D Surface and volume integrals
@{""" Surface and volume integrals """
def Surface(P, signed=False):
    return II(P, 0, 0, 0, signed)
def Volume(P):
    return III(P, 0, 0, 0)
@}
%-------------------------------------------------------------------------------

\paragraph{Terms of the Euler tensor}
%-------------------------------------------------------------------------------
@D Terms of the Euler tensor
@{""" Terms of the Euler tensor """
def FirstMoment(P):
    out = [None]*3
    out[0] = III(P, 1, 0, 0)
    out[1] = III(P, 0, 1, 0)
    out[2] = III(P, 0, 0, 1)
    return out

def SecondMoment(P):
    out = [None]*3
    out[0] = III(P, 2, 0, 0)
    out[1] = III(P, 0, 2, 0)
    out[2] = III(P, 0, 0, 2)
    return out

def InertiaProduct(P):
    out = [None]*3
    out[0] = III(P, 0, 1, 1)
    out[1] = III(P, 1, 0, 1)
    out[2] = III(P, 1, 1, 0)
    return out
@}
%-------------------------------------------------------------------------------

\paragraph{Vectors and covectors of mechanical interest}
%-------------------------------------------------------------------------------
@D Vectors and covectors of mechanical interest
@{""" Vectors and covectors of mechanical interest """
def Centroid(P):
    out = [None]*3
    firstMoment = FirstMoment(P)
    volume = Volume(P)
    out[0] = firstMoment[0]/volume
    out[1] = firstMoment[1]/volume
    out[2] = firstMoment[2]/volume
    return out

def InertiaMoment(P):
    out = [None]*3
    secondMoment = SecondMoment(P)
    out[0] = secondMoment[1] + secondMoment[2]
    out[1] = secondMoment[2] + secondMoment[0]
    out[2] = secondMoment[0] + secondMoment[1]
    return out
@}
%-------------------------------------------------------------------------------

\paragraph{Basic integration functions}
%-------------------------------------------------------------------------------
@D Basic integration functions
@{""" Basic integration functions """
def II(P, alpha, beta, gamma, signed):
    w = 0
    V, FV = P
    for i in range(len(FV)):
        tau = [V[v] for v in FV[i]]
        term = TT(tau, alpha, beta, gamma, signed)
        w += term
    return w

def III(P, alpha, beta, gamma):
    w = 0
    V, FV = P
    for i in range(len(FV)):
        tau = [V[v] for v in FV[i]]
        vo,va,vb = tau
        a = VECTDIFF([va,vo])
        b = VECTDIFF([vb,vo])
        c = VECTPROD([a,b])
        det = VECTNORM(c)
        if det != 0: w += (c[0]/det) * TT(tau, alpha+1, beta, gamma)
    return w/(alpha + 1)

def M(alpha, beta):
    a = 0
    for l in range(alpha + 2):
        a += CHOOSE([alpha+1,l]) * POWER([-1,l])/(l+beta+1)
    return a/(alpha + 1)
@}
%-------------------------------------------------------------------------------

\paragraph{The main integration routine}
%-------------------------------------------------------------------------------
@D The main integration routine
@{""" The main integration routine """
def TT(tau, alpha, beta, gamma, signed=False):
	vo,va,vb = tau
	a = VECTDIFF([va,vo])
	b = VECTDIFF([vb,vo])
	sl = 0;
	for h in range(alpha+1):
		for k in range(beta+1):
			for m in range(gamma+1):
				s2 = 0
				for i in range(h+1):
					s3 = 0
					for j in range(k+1):
						s4 = 0
						for l in range(m+1):
							s4 += CHOOSE([m, l]) * POWER([a[2], m-l]) \
								* POWER([b[2], l]) * M( h+k+m-i-j-l, i+j+l )
						s3 += CHOOSE([k, j]) * POWER([a[1], k-j]) \
							* POWER([b[1], j]) * s4
					s2 += CHOOSE([h, i]) * POWER([a[0], h-i]) * POWER([b[0], i]) * s3;
				sl += CHOOSE ([alpha, h]) * CHOOSE ([beta, k]) * CHOOSE ([gamma, m]) \
					* POWER([vo[0], alpha-h]) * POWER([vo[1], beta-k]) \
					* POWER([vo[2], gamma-m]) * s2
	c = VECTPROD([a, b])
	if not signed: return sl * VECTNORM(c)
	elif isclose(a[2],0.0) and isclose(b[2],0.0):
		return sl * VECTNORM(c) * SIGN(c[2])
	else: print "error: in signed surface integration"
@}
%-------------------------------------------------------------------------------

\paragraph{Surface integration}

An exact evaluation of the surface of every cell of a cellular complex may be obtained by integration of the constant scalar field $x^0y^0z^0 = 1$ over the 2D polygonal cells of our hospital model, embedded in the subspace $z=0$.

The integration over a generic planar polygon, either convex or non-convex, may by computed by summing the integrals over the coherently-oriented \emph{triangles} generated by the (oriented) edges of the (oriented) polygon and by its first vertex. An implicit assumption on the input: the vertices in each \texttt{FV} element (face) are counterclockwise ordered, so that the \texttt{triangles} are coherently oriented.

The \texttt{Surface} integration function is imported from the \texttt{lar-cc} module \texttt{integr}. The input LAR \texttt{model} is the triple of 2D vertices \texttt{V} and the two compressed characteristic matrices \texttt{FV} and \texttt{EV}, giving the set of vertices incident on every face and edge of the cellular decomposition, respectively. The \texttt{cochain} output is the numeric array of surface areas indexed by faces.

%-------------------------------------------------------------------------------
@D Surface integration
@{from integr import *
""" Surface integration """
def surfIntegration(model,signed=False):
    V,FV,EV = model
    V = [v+[0.0] for v in V]
    cochain = []
    for face in FV:
        triangles = AA(C(AL)(face[0]))(TRANS([face[1:-1],face[2:]]))
        P = V,triangles
        area = Surface(P,signed)
        cochain += [area]
    return cochain

def signedSurfIntegration(model,signed=False):
    V,FV,EV = model
    V = [v+[0.0] if len(v)==2 else v for v in V]
    cochain = []
    for face in FV:
        triangles = AA(C(AL)(face[0]))(TRANS([face[1:-1],face[2:]]))
        P = V,triangles
        area = Surface(P,signed)
        cochain += [area]
    return cochain
@}
%-------------------------------------------------------------------------------

\subsection{Exporting the \texttt{integr} module}

%-------------------------------------------------------------------------------
@O larlib/larlib/integr.py
@{# -*- coding: utf-8 -*-
"""Module for integration of polynomials over 3D volumes and surfaces"""
from larlib import *

@< Surface and volume integrals @>
@< Terms of the Euler tensor @>
@< Vectors and covectors of mechanical interest @>
@< Basic integration functions @>
@< The main integration routine @>
@< Surface integration @>
@}
%-------------------------------------------------------------------------------

\section{Test examples}

\paragraph{Integrals on the standard triangle}
%-------------------------------------------------------------------------------
@O test/py/integr/test01.py
@{""" Integrals on the standard triangle """
from larlib import *

V = [[0,0,0],[1,0,0],[0,1,0]]
FV = [[0,1,2]]
P = (V,FV)
print II(P, 0, 0, 0, False)
@}
%-------------------------------------------------------------------------------

\paragraph{Integrals on the standard tetrahedron}
%-------------------------------------------------------------------------------
@O test/py/integr/test02.py
@{""" Integrals on the standard triangle """
from larlib import *

V = [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]
FV = [[1,2,3],[0,3,2],[0,1,3],[0,1,2]]
P = (V,FV)
print Volume(P)
@}
%-------------------------------------------------------------------------------

\paragraph{Integrals on the standard 3D cube}

Here we give a \emph{triangulation} of the boundary of the unit standard cube in 3D,
positively oriented, i.e. with all the triangle normals pointing towards the
exterior of the solid cube.

Notice that inverting the boundary orientation changes the sign of the volume,
but does not change the centroid.

%-------------------------------------------------------------------------------
@O test/py/integr/test03.py
@{""" Integrals on the standard 3D cube """
from larlib import *

V = [[0,0,0],[1,0,0],[0,1,0],[1,1,0],[0,0,1],[1,0,1],[0,1,1],[1,1,1]]

FV = [[1,0,2],[0,1,4],[2,0,4],[1,2,3],[1,3,5],[4,1,5],[3,2,6],[2,4,6],
	  [5,3,7],[3,6,7],[4,5,6],[6,5,7]]

P = (V,FV)
print Volume(P)
print Centroid(P)

""" changing the boundary orientation changes the sign of volume,
	 but not changes the centroid """

P = (V,AA(REVERSE)(FV))
print Volume(P)
print Centroid(P)
@}
%-------------------------------------------------------------------------------

\paragraph{Integrals on a non-convex 2D polyline}

Some problem appear when using the integration method~\cite{CattaniP-BIL1990} on 2D models.
In fact, the basic integration formulas refer to the three-variate monomial $x^\alpha y^\beta z^\gamma$,
and work even for the integration over 2D domains embedded in 3D, but of course in this case they do not return
the integral values as \emph{signed} real numbers, but as positive numbers, since a positive orientation for a open 2D domain in 3D may be defined only locally.

The problem is solved by using the variable \texttt{signed}, set to \texttt{True} for non-convex polygons embedded in $z=0$,
as shown in the following example.

%-------------------------------------------------------------------------------
@O test/py/integr/test04.py
@{""" Integrals on 2D non-convex polyline """
from larlib import *

polyline = TRANS([[10,10,20,40,30,30,15,15],[10,20,30,20,10,15,15,10]])
model = polyline2lar([polyline])
VIEW(POLYLINE(polyline+[polyline[0]]))

V,FV,EV = model
tria = FV[0]
triangles = AA(C(AL)(0))(TRANS([tria[1:-1],tria[2:]]))
V = [v+[0.0] for v in V]
P = V,triangles

area = Surface(P,signed=True)
print "area =",area
@}
%-------------------------------------------------------------------------------

%-------------------------------------------------------------------------------
@O test/py/integr/test05.py
@{""" Integrals on 2D non-convex polyline """
from larlib import *

V,FV,EV = openCourt11.body[0]
tria = FV[0]
triangles = AA(C(AL)(0))(TRANS([tria[1:-1],tria[2:]]))
V = [v+[0.0] for v in V]
P = V,triangles

area = Surface(P,signed=True)
print "area =",area

P = V, AA(REVERSE)(triangles)
area = Surface(P,signed=True)
print "area =",area
@}
%-------------------------------------------------------------------------------

\appendix
\section{Utilities}

\bibliographystyle{amsalpha}
\bibliography{integr}

\end{document}


\clearpage
\subsection{Pascal Program (with Functions)} % (fold)
\label{sub:pas_program_with_functions}

Your Pascal program can contain definitions for your own \nameref{sub:function}s.

\passyntax{passynt:function-decl-program}{a program (with functions)}{function-decl/program-with-func}

\mynote{
\begin{itemize}
  \item A Pascal program may contain declarations for custom \nameref{sub:function}s.
  \item The function must be declared before it is used.
  \item See \nameref{sub:c_function_declaration} for the Syntax for declaring your own Functions.
\end{itemize}
}

\clearpage

\passection{\pascode{plst:ftoc}{Example of declaring a function in a Pascal program}{code/pascal/storing-using-data/FtoC.pas}}

\mynote{
\begin{itemize}
  \item \lref{plst:ftoc} contains a program with a custom function that converts temperature from Fahrenheit to Celsius.
\end{itemize}
}

% subsection c_program_with_procedures_ (end)


%% TeX file for the TransportationTableaux user guide.
%% Code Author: Russell Andrew Edson, russell.andrew.edson@gmail.com

% Document setup parameters
\documentclass[a4paper,12pt]{article}
\topmargin -1cm
%\oddsidemargin -0.74cm
%\evensidemargin -0.74cm
%\textwidth 17.59cm
\textheight 23.94cm
\hyphenpenalty=100000
\parindent=0cm

% Import Libraries
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage{caption}
\usepackage{bbding}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{subcaption}
\usepackage{DejaVuSansMono}
\usepackage[cc]{titlepic}
\usepackage[titletoc,toc]{appendix}

\begin{document}

\Huge\textbf{TransportationTableaux User Guide} \\ \hspace*{1cm} \hrulefill \\
\normalsize

Created by Russell Andrew Edson \\
\texttt{russell.andrew.edson@gmail.com}\\
\texttt{https://github.com/RussellAndrewEdson/}\\

% Picture here.
\begin{figure}[h!]
\centering
\fbox{\includegraphics[width=0.8\textwidth]{transportationtableaux_v3screenshot.png}}
\end{figure}

\section*{TransportationTableaux}

The TransportationTableaux program was designed to solve Transportation problems in a step-by-step manner. Every detail of the solution process can be examined (the $u_i$/$v_j$ variables, finding the star pair, constructing the tableau cycle, adjusting the allocations, etc).\\

The main hope is that the program provides the user with a deeper understanding of how the solution process works. As such, it has primarily been developed as a teaching/demonstration tool.\\

\newpage

\section*{An example of a Transportation Problem}
Suppose we are given the following transportation problem to solve:\\

We have a situation where we have a group of suppliers that can satisfy demand for a particular commodity. (This can be anything; for the sake of the example, suppose that we have a group of paper suppliers that can deliver paper to places that need it.)\\

Now each supplier will have a different amount of paper that they can supply:
\begin{itemize}
\item
Supplier $S_A$ has 15 tons of paper available.
\item
Supplier $S_B$ has 17 tons of paper available.

\item
Supplier $S_C$ has 18 tons of paper available.\\
\end{itemize}

Just as the suppliers will have different amounts that they can supply, their customers will have different levels of demand:
\begin{itemize}
\item
Customer $D_A$ demands 18 tons of paper.

\item
Customer $D_B$ demands 15 tons of paper.

\item
Customer $D_C$ demands 10 tons of paper.

\item
Customer $D_D$ demands \;\;7 tons of paper.

\end{itemize}

Note that the total supply equals the total demand (we call such a problem a \textit{Balanced Transportation Problem}). This crucial fact is what allows our algorithm to work, but you can always balance an unbalanced problem (just add fictitious suppliers or customers to pick up the slack.) \\

Of course, there's always a cost associated with moving stuff from one place to another; eg. it might cost 1-hundred dollars to transport a single ton of paper from supplier $S_A$ to customer $D_A$, but 5-hundred dollars to transport a ton of paper from supplier $S_A$ to customer $D_B$. So it is 5 times more expensive for Supplier $S_A$ to send paper to customer $D_B$. (The actual cost is not so important, just the relative magnitudes between the costs.)\\

We represent this with the \textit{link-flow cost matrix}:\\

\begin{tabular}{| l | r | r | r | r | }
\hline
From/To & Customer $D_A $ & Customer $D_B $ & Customer $D_C $ & Customer $D_D $ \\
\hline
Supplier $S_A$ & 1 & 5 & 3 & 6 \\
\hline
Supplier $S_B$ & 3 & 2 & 6 & 2 \\
\hline
Supplier $S_C$ & 6 & 3 & 7 & 5  \\
\hline
\end{tabular}\\[6pt]

(* If we had fictitious suppliers and customers, they would have a transport cost of 0 to everything else in this matrix.)\\

\newpage

So we wish to find the \textbf{smallest possible cost of transportation}: that is, we wish to meet all demand, but in the cheapest way possible.\\

We do this using the \textit{Primal-Dual Transportation Algorithm}. The explanation of the algorithm involves some complex mathematics and is (for the moment) beyond the scope of this guide. But we don't have to worry about it -- the program takes care of the entire solution process.\\

(For those that already have some knowledge of the algorithm: as you'll see, the program displays all of the information from its solution process, so you can see exactly how the algorithm works anyway.)\\

\section*{Solving with the TransportationTableaux program}
So given the above example, all we need to do is enter the values into the fields of the program.

\begin{enumerate}
\item
Upon first firing up the program, the default tableau size is for a problem with 3 supplies and 3 customers (demands).

\begin{figure}[h!]
\centering
\fbox{\includegraphics[width=0.72\textwidth]{screen01.png}}
\caption{The initial tableau for the program after startup.}
\end{figure}

\newpage

\item
However, we have a problem with 4 demands, so we adjust the number of demands and hit 'Resize'.

\begin{figure}[h!]
\centering
\fbox{\includegraphics[width=0.75\textwidth]{screen02.png}}
\caption{We can resize the tableau by clicking the 'Resize' button.}
\end{figure}

\item
We then enter in the values for the supplies into the fields as shown. (ie. the number of tons of paper available to each supplier, in the case of our example.) We've entered them in the order $S_A$, $S_B$, $S_C$ here, but note that the order is arbitrary at this point.

\begin{figure}[h!]
\centering
\fbox{\includegraphics[width=0.75\textwidth]{screen03.png}}
\caption{The values for the supplies can be entered straight into the grid.}
\end{figure}

\newpage

\item
Similarly, we enter in the values for the demands (customers). The order is still arbitrary at this point, but for simplicity we've simply entered them in the order $D_A$, $D_B$, $D_C$, $D_D$ as we go from left to right.

\begin{figure}[h!]
\centering
\fbox{\includegraphics[width=0.75\textwidth]{screen04.png}}
\caption{The values for the demands can also be entered straight into the grid.}
\end{figure}

\item
Next, we enter in the link-flow costs. However, now the order does matter -- we line up each supply and demand with its corresponding transportation cost. (So the first supply box is $S_A$ and the first demand box is $D_A$, so the box in row 1, column 1 of the grid is the cost of transporting from $S_A$ to $D_A$, and so on.)

\begin{figure}[h!]
\centering
\fbox{\includegraphics[width=0.75\textwidth]{screen05.png}}
\caption{Each link-flow cost can be entered into the top-right corner of the grid cells.}
\end{figure}

\newpage

\item
We can now simply step through the solution to the problem (or if you're in a hurry, simply press 'Solve' to jump straight to the optimal solution tableau.)

\begin{figure}[h!]
\centering
\fbox{\includegraphics[width=0.75\textwidth]{screen06.png}}
\caption{Once the values have been entered, press 'Step' to begin the solution process with the North-West corner rule.}
\end{figure}

The North-West corner rule is performed to give us an initial solution to the problem, but it probably won't be optimal. The cost and basic solution are shown below the tableau (the basic solution should be read as: $x_{1,1} = 15$ means that the 1st supply $S_A$ should send 15 tons of paper to the 1st demand $D_A$. )\\

For those with knowledge of the Primal-Dual Transportation algorithm, note that the $u_i$ and $v_j$ dual variables have also been found for the new tableau. These appear in the outer row and columns as shown. \\

\newpage

\item
Now the solution found by the North-West corner rule is not optimal. So we aim to modify our allocations toward a more optimal solution, and we do this in a methodical way by making sure that our adjustments always give us a smaller cost (by seeking \textit{dual feasibility}, a concept that is out of the scope of this tutorial).\\

Suffice it to say that we (methodically) find a supply-demand pair that doesn't already have an allocation, and give it one. We call such a pair a \textit{star-pair}. Clicking 'Step' again will have the program find this pair for us.\\

 \begin{figure}[h!]
\centering
\fbox{\includegraphics[width=0.75\textwidth]{screen07.png}}
\caption{Pressing 'Step' again finds us the star-pair.}
\end{figure}

The star-pair can be seen in the grid, marked with a $*$ in the top-left corner.\\

\newpage

\item
Of course, keep in mind that our transportation problem is balanced, and our initial solution from the North-West corner rule already allocated all of the supply. So if we want to give the star-pair an allocation, we need to make adjustments to all of the other supply-demand pairs too!\\

To explain this, suppose we wish to give the star-pair (at row 1, column 3) an allocation of 2. Then to keep the supply balanced, we need to remove a total of 2 from the other allocations in the same row (row 1); and to keep the demand balanced, we need to remove a total of 2 from the allocations in the same column (column 3).\\

But then since we removed 2 from the supply-demand pair at row 1, column 1, this has thrown the demand in column 1 out of balance. So we need to add 2 back to some other allocation in column 1, which will further throw the tableau out of balance if we're not careful about it. Things get pretty dicey very quickly!\\

We handle this by finding a cycle in the tableau through the star-pair and the existing allocations. The program will do this for us when we click 'Step' again.

\begin{figure}[h!]
\centering
\fbox{\includegraphics[width=0.75\textwidth]{screen08.png}}
\caption{Clicking 'Step' will now give us our cycle along which to adjust the allocations safely.}
\end{figure}

\newpage

Note also that in addition to the cycle, some $+$ and $-$ signs have shown up in the bottom-right corner of those cells at the vertices of the cycle.

\begin{figure}[h!]
\centering
\fbox{\includegraphics[width=0.37\textwidth]{screen081.png}}
\caption{The tableau cycle.}
\end{figure}

In terms of the adjustments, the $+$ signifies that the allocation for that supply-demand pair will be increased for the next tableau. Likewise, the $-$ signifies that the allocation will be decreased.\\

The star-pair always has its allocation increased (that was the whole point, remember?) and we alternate $+$, $-$, $+$, $-$ as we walk along the cycle. This fixes our allocation problem mentioned earlier; every time we increase ($+$) an allocation, we also decrease ($-$) another allocation from the same row and the same column, and vice versa. So everything balances out.\\

As for the amount that the allocations are adjusted by? This is chosen to be the minimum of all of the current allocations that are to be decreased ($-$). The reasoning is beyond the scope of this tutorial, but choosing this amount works.\\

\newpage

\item
Finally, clicking 'Step' again will adjust the allocations along the cycle and bring us to the next tableau.

\begin{figure}[h!]
\centering
\fbox{\includegraphics[width=0.75\textwidth]{screen09.png}}
\caption{Clicking 'Step' adjusts the allocations in the grid.}
\end{figure}

Note that the cost has decreased (as we'd hoped), and the basic solution has changed. (The $u_i$ and $v_j$ variables have also been recalculated for the new tableau.)\\

The tableau is still not optimal yet though. So we repeat the process -- we find the star-pair, construct the cycle, and adjust the allocations along the cycle accordingly to get to the next tableau, which will have a more optimal solution. Clicking 'Step' repeatedly will take us through each of these steps in turn, showing the star-pairs, cycles and tableau at every stage.\\

(Note that sometimes the cost might not have changed. This happens when the adjustment does not modify the total cost of transportation, but simply changes the supply-demand pairs around so that it can get to a smaller cost later.)\\

\newpage

\item
Finally, when you've arrived at the last tableau (or when you've skipped right to the end by clicking the 'Solve' button), you'll be told such in the status at the bottom of the window. (The buttons will also be disabled, as shown.)

\begin{figure}[h!]
\centering
\fbox{\includegraphics[width=0.75\textwidth]{screen10.png}}
\caption{We've reached the last tableau with the optimal solution.}
\end{figure}

The cost given here is the minimum possible cost of transportation for the problem that results in all of the demand being met.The basic solution gives the allocations for this minimum cost.\\

So we've solved the problem! The minimum cost for our example is\\ 139(-hundred dollars), achieved when we send:
\begin{itemize}
\item 8 \;\;tons of paper from supplier $S_A$ to customer $D_A$,
\item 7 \;\;tons of paper from supplier $S_A$ to customer $D_C$,
\item 10 tons of paper from supplier $S_B$ to customer $D_A$,
\item 7 \;\;tons of paper from supplier $S_B$ to customer $D_D$,
\item 15 tons of paper from supplier $S_C$ to customer $D_B$,
\item 3 \;\;tons of paper from supplier $S_C$ to customer $D_C$.
\end{itemize}

\end{enumerate}

\end{document}


\documentclass[12pt]{article}

\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{subfigure}
\usepackage{float}
\usepackage{ulem}
\usepackage{bm}
\usepackage{anysize}
\usepackage{pythonhighlight}

\marginsize{2cm}{2cm}{0.9cm}{1.8cm}

\title{EECE 5639 Computer Vision\\ [2ex] \begin{large} Homework \#6 \end{large} }
\author{Jiyu Tian}
\date{}

\begin{document}
\maketitle
\pagestyle{empty}
%%---------------------------------------------------------------
%% Question 1
%%---------------------------------------------------------------
\section{Solution:}
The planar homography relates the transformation between two views when the scene is planar or the two views were generated by a camera rotating around its center of projection. It is a $3\times3$ matrix of rank 3. It has no information about the scene depth.\\
\\
The Essential and Fundamental matrices completely describe the geometric relationship between corresponding points of a stereo pair. They are $3\times3$ matrices of rank 2. They provide the epipolar line of second image to search along once given a point in first image.
%%---------------------------------------------------------------
%% Question 2
%%---------------------------------------------------------------
\section{Solution:}
If two cameras have parallel optical axes and the line through the two centers of projection is perpendicular to the optic axes, the corresponding epipoles are at infinity.

%%---------------------------------------------------------------
%% Question 3
%%---------------------------------------------------------------
\section{Solution:}
Projection of planar points to image plane is an affine transformation, if the planar surface is perpendicular to the optic axis:
\begin{equation*}
\begin{aligned}
\left[ \begin{array}{c}
x\\
y\\
1
\end{array} \right] &\sim \left[ \begin{array}{cccc}
f & 0 & 0 & 0\\
0 & f & 0 & 0\\
0 & 0 & 1 & 0
\end{array} \right]\left[ \begin{array}{cccc}
r_{11} & r_{12} & r_{13} & t_x \\
r_{21} & r_{22} & r_{23} & t_y\\
r_{31} & r_{32} & r_{33} & t_z\\
0 & 0 & 0 & 1
\end{array} \right]\left[ \begin{array}{c}
X\\
Y\\
0\\
1
\end{array} \right] \\
&\sim \left[ \begin{array}{cccc}
f & 0 & 0\\
0 & f & 0\\
0 & 0 & 1
\end{array} \right]\left[ \begin{array}{cccc}
r_{11} & r_{12} & t_x \\
r_{21} & r_{22} & t_y\\
r_{31} & r_{32} & t_z\\
\end{array} \right]\left[ \begin{array}{c}
X\\
Y\\
1
\end{array} \right] \\
&\sim \left[ \begin{array}{cccc}
fr_{11} & fr_{12} & ft_x \\
fr_{21} & fr_{22} & ft_y\\
r_{31} & r_{32} & t_z\\
\end{array} \right]\left[ \begin{array}{c}
X\\
Y\\
1
\end{array} \right]\\
&\sim \frac{1}{t_z} \left[ \begin{array}{cccc}
f\cos\theta & -f\sin\theta &  ft_x \\
f\sin\theta & f\cos\theta & ft_y\\
0 & 0 & t_z\\
\end{array} \right]\left[ \begin{array}{c}
X\\
Y\\
1
\end{array} \right]
\end{aligned}
\end{equation*}
It is also a 2D affine transformation from film coordinates $(x,y)$ to pixel coordinates $(u,v)$:
\begin{equation*}
\left[ \begin{array}{c}
u\\
v\\
1
\end{array} \right] \sim \left[ \begin{array}{cccc}
a_{11} & a_{12} & a_{13} \\
a_{21} & a_{22} & a_{23}\\
0 & 0 & 1\\
\end{array} \right]\left[ \begin{array}{c}
x\\
y\\
1
\end{array} \right]
\end{equation*}
Since the combination of affine transformations is also an affine transformation, the projection of a point in a planar scene at world coordinates $(X, Y)$ to pixel coordinates
$(u, v)$ in an image plane can be represented using a \textit{planar affine camera model}.\\
\\
The affine viewing condition is small depth variation in the scene along the optic axis compared to its average distance from the camera. There are 6 $D.O.F.$ in the model. Minimum of 3 calibration points are needed to estimate the transformation. When an affine transform is applied to two parallel lines, the transformed lines will also be parallel.
%%---------------------------------------------------------------
%% Question 4
%%---------------------------------------------------------------
\section{Solution:}
Let $O_{i1}$, $O_{i2}$ be the origin of image plane 1 and 2 respectively, we have $O_{i1}(0,9)$, $O_{i1}(0, -9)$.
\begin{equation*}
\begin{aligned}
\left.
     \begin{array}{cc}
        \text{Line } PO_1: y = \frac{1}{5}x + 12\\
        \text{Line } p_1O_{i1}: y = 10x + 9\\
     \end{array}
\right\}&\to  p_1(\frac{15}{49}, \frac{591}{49})\\
\left.
     \begin{array}{cc}
        \text{Line } PO_2: y = \frac{3}{5}x -4 \\
        \text{Line } p_2O_{i2}: y = -10x - 9\\
     \end{array}
\right\}&\to  p_2(-\frac{25}{53}, -\frac{227}{53})
\end{aligned}
\end{equation*}

Disparity
\begin{equation*}
\begin{aligned}
    p_1O_{i1} &= \sqrt{(\frac{15}{49})^2 + (\frac{591}{49} - 9)^2} = \frac{15}{49}\sqrt{101}\\
    p_2O_{i2} &= \sqrt{(-\frac{25}{53})^2 + (-\frac{227}{53} + 9)^2} = \frac{25}{53}\sqrt{101}\\
    d &= p_1O_{i1} - p_2O_{i2} = \frac{430}{2597}\sqrt{101}
\end{aligned}
\end{equation*}

%%---------------------------------------------------------------
%% Question 5
%%---------------------------------------------------------------
\section{Solution:}
Given a set of points $\textbf{p}_i = [x_i, y_i, 1]^T$ with $i = 1, ..., n$ from either side, we can define
\begin{equation*}
    \bar{x} = \frac{1}{n}\sum_{i=1}^nx_i,\ \bar{y} = \frac{1}{n}\sum_{i=1}^ny_i,\ \bar{d} = \frac{\sum_{i=1}^n\sqrt{(x_i - \bar{x})^2 + (y_i - \bar{y})^2 }}{n\sqrt{2}}
\end{equation*}
Solve $H\textbf{p}_i = \hat{\textbf{p}}_i$ for a $3\times3$ matrix $H$ with $\hat{\textbf{p}}_i = [(x_i - \bar{x})/d,\ (y_i - \bar{y})/d,\ i]^T$:
\begin{equation*}
H = \left[ \begin{array}{ccc}
1/d & 0 & -\bar{x}/d \\
0 & 1/d & -\bar{y}/d \\
0 & 0 & 1
\end{array} \right]
\end{equation*}

%%---------------------------------------------------------------
%% Question 6
%%---------------------------------------------------------------
\section{Solution:}
The planar affine transformation is given by
\begin{equation*}
\left[ \begin{array}{c}
x'\\
y'\\
1
\end{array} \right] = \left[ \begin{array}{ccc}
h_{11} & h_{12} & h_{13} \\
h_{21} & h_{22} & h_{23} \\
0 & 0 & 1
\end{array} \right]\left[ \begin{array}{c}
x\\
y\\
1
\end{array} \right]
\end{equation*}
Since there are 6 $D.O.F.$, 3 points pairs are needed for solution. Given the map relation from $(x_1, y_1)$, $(x_2, y_2)$, $(x_3, y_3)$ to $(x'_1, y'_1)$, $(x'_2, y'_2)$, $(x'_3, y'_3)$, we obtain

\begin{equation*}
    Ax = b
\end{equation*}
where
\begin{equation*}
A = \left[ \begin{array}{cccccc}
x_1 & y_1 & 1 & 0 & 0 & 0\\
0 & 0 & 0 & x_1 & y_1 & 1\\
x_2 & y_2 & 1 & 0 & 0 & 0\\
0 & 0 & 0 & x_2 & y_2 & 1\\
x_3 & y_3 & 1 & 0 & 0 & 0\\
0 & 0 & 0 & x_3 & y_3 & 1
\end{array} \right],\  x= \left[ \begin{array}{c}
h_{11} \\
h_{12} \\
h_{13} \\
h_{21} \\
h_{22} \\
h_{23}
\end{array} \right],\  b= \left[ \begin{array}{c}
x'_1\\
y'_1\\
x'_2\\
y'_2\\
x'_3\\
y'_3
\end{array} \right]
\end{equation*}
To solve via least squares
\begin{equation*}
    X = \arg\max_{x}||Ax-b||^2 = (A^TA)^{-1}A^Tb
\end{equation*}
Here we can separate the $6\times6$ system into $2$ sub-equations:
\begin{equation*}
A_1x_1 = b_1, A_1 = \left[ \begin{array}{ccc}
x_1 & y_1 & 1\\
x_2 & y_2 & 1\\
x_3 & y_3 & 1
\end{array} \right],\  x_1= \left[ \begin{array}{c}
h_{11} \\
h_{12} \\
h_{13}
\end{array} \right],\  b_1= \left[ \begin{array}{c}
x'_1\\
x'_2\\
x'_3
\end{array} \right]
\end{equation*}
\begin{equation*}
A_2x_2 = b_2, A_2 = \left[ \begin{array}{cccccc}
x_1 & y_1 & 1\\
x_2 & y_2 & 1\\
x_3 & y_3 & 1
\end{array} \right],\  x_2 = \left[ \begin{array}{c}
h_{21} \\
h_{22} \\
h_{23}
\end{array} \right],\  b_2 = \left[ \begin{array}{c}
y'_1\\
y'_2\\
y'_3
\end{array} \right]
\end{equation*}
which can be solved via
\begin{equation*}
    X_1 = \arg\max_{x_1}||A_1x_1-b_1||^2 = (A_1^TA_1)^{-1}A_1^Tb_1
\end{equation*}
\begin{equation*}
    X_2 = \arg\max_{x_2}||A_2x_2-b_2||^2 = (A_2^TA_2)^{-1}A_2^Tb_2
\end{equation*}

\begin{equation*}
X = \left[ \begin{array}{c}
X_1\\
X_2
\end{array} \right]
\end{equation*}
%%---------------------------------------------------------------
%% Question 7
%%---------------------------------------------------------------
\section{Solution:}
\begin{equation*}
T_1 = \left[ \begin{array}{cccc}
1 & 0 & 0 & -2\\
0 & 1 & 0 & -2\\
0 & 0 & 1 & -4\\
0 & 0 & 0 & 1
\end{array} \right],\ T_2 = \left[ \begin{array}{cccc}
1 & 0 & 0 & -4\\
0 & 1 & 0 & -3\\
0 & 0 & 1 & -3\\
0 & 0 & 0 & 1
\end{array} \right]
\end{equation*}

\begin{equation*}
R_{Z90}  = \left[ \begin{array}{cccc}
0 & -1 & 0 & 0\\
1 & 0 & 0 & 0\\
0 & 0 & 1 & 0\\
0 & 0 & 0 & 1\\
\end{array} \right],\ R_{Y180}  = \left[ \begin{array}{cccc}
-1& 0 & 0 & 0\\
0 & 1 & 0 & 0\\
0 & 0 & -1& 0\\
0 & 0 & 0 & 1
\end{array} \right]
\end{equation*}

\begin{equation*}
M_1 = R_{Y180} R_{Z90} T_1 = \left[ \begin{array}{cccc}
0 & 1 & 0 & -2\\
1 & 0 & 0 & -2\\
0 & 0 & -1 & 4\\
0 & 0 & 0 & 1
\end{array} \right]
\end{equation*}

\begin{equation*}
M_2 = R_{Y180} R_{Z90} T_2 = \left[ \begin{array}{cccc}
0 & 1 & 0 & -3\\
1 & 0 & 0 & -4\\
0 & 0 & -1 & 3\\
0 & 0 & 0 & 1
\end{array} \right]
\end{equation*}

\noindent (a)
\begin{equation*}
C_2^1 = M_1C_2 = \left[ \begin{array}{cccc}
0 & 1 & 0 & -2\\
1 & 0 & 0 & -2\\
0 & 0 & -1 & 4\\
0 & 0 & 0 & 1
\end{array} \right] \left[ \begin{array}{c}
4\\
3\\
3\\
1
\end{array} \right] = \left[ \begin{array}{c}
1\\
2\\
1\\
1
\end{array} \right], \ E_1^1 = \left[ \begin{array}{c}
f\frac{x}{z}\\
f\frac{y}{z}\\
f
\end{array} \right]  = \left[ \begin{array}{c}
1\\
2\\
1
\end{array} \right]
\end{equation*}

\begin{equation*}
C_1^2 = M_2C_1 = \left[ \begin{array}{cccc}
0 & 1 & 0 & -3\\
1 & 0 & 0 & -4\\
0 & 0 & -1 & 3\\
0 & 0 & 0 & 1
\end{array} \right] \left[ \begin{array}{c}
2\\
2\\
4\\
1
\end{array} \right] = \left[ \begin{array}{c}
-1\\
-2\\
-1\\
1
\end{array} \right], \ E_2^2 = \left[ \begin{array}{c}
f\frac{x}{z}\\
f\frac{y}{z}\\
f
\end{array} \right]  = \left[ \begin{array}{c}
1\\
2\\
1
\end{array} \right]
\end{equation*}

\noindent (b) The camera 1 coordinates of fly are
\begin{equation*}
f^1 =  \left[ \begin{array}{c}
0\\
2k\\
k\\
1
\end{array} \right]
\end{equation*}
The camera 2 coordinates of fly are

\begin{equation*}
f^2 = M_2M_1^{-1}f^1 = \left[ \begin{array}{cccc}
1 & 0 & 0 & -1\\
0 & 1 & 0 & -2\\
0 & 0 & 1 & -1\\
0 & 0 & 0 & 1
\end{array} \right] \left[ \begin{array}{c}
0\\
2k\\
k\\
1
\end{array} \right] = \left[ \begin{array}{c}
-1\\
2k-2\\
k-1\\
1
\end{array} \right]
\end{equation*}
The camera 2 coordinates of the image in the second camera
are
\begin{equation*}
f_2^2 = \left[ \begin{array}{c}
\frac{1}{1-k}\\
2\\
1
\end{array} \right]
\end{equation*}
Note that this is the epipolar line of fly in camera 2 coordinates

\begin{equation*}
\left\{
     \begin{array}{cc}
    y = 2\\
    z = 1
     \end{array}
\right.
\end{equation*}
The epipolar plane also passes through origin $(0,0,0)$, and thus we have its expression:
\begin{equation*}
y-2z = 0
\end{equation*}

\noindent (c) The camera 1 coordinates of fly at $t$ are
\begin{equation*}
f_{1t}^1 = M_1 \left ( M_1^{-1}f^1 + vt \right ) = \left[ \begin{array}{cccc}
0 & 1 & 0 & -2\\
1 & 0 & 0 & -2\\
0 & 0 & -1 & 4\\
0 & 0 & 0 & 1
\end{array} \right] \left[ \begin{array}{c}
2 + 2k - 3t\\
2 - 2t\\
4 - k - t\\
1
\end{array} \right] = \left[ \begin{array}{c}
-2t\\
2k-3t\\
k+t\\
1
\end{array} \right]
\end{equation*}
The camera 1 coordinates of its image are
\begin{equation*}
p_{1t}^1 = \left[ \begin{array}{c}
f\frac{x}{z}\\
f\frac{y}{z}\\
f
\end{array} \right]  = \left[ \begin{array}{c}
\frac{-2t}{k+t}\\
\frac{2k-3t}{k+t}\\
1
\end{array} \right]
\end{equation*}
FOE is
\begin{equation*}
p^1_0 = \lim\limits_{t\to\infty}p_{1t}^1 = \lim\limits_{t\to\infty} \left[ \begin{array}{c}
\frac{-2t}{k+t}\\
\frac{2k-3t}{k+t}\\
1
\end{array} \right] = \left[ \begin{array}{c}
-2\\
-3\\
1
\end{array} \right]
\end{equation*}
Therefore the camera coordinates of the FOE in camera 1 are $(-2, -3, 1)$.

%%---------------------------------------------------------------
%% Question 8
%%---------------------------------------------------------------
\section{Solution:}
(a) The complexity of the underlying dynamics is $2$ since rank$(H) = 2$.\\
\\
(b) $a_1 = 2, a_2 = 1$\\
\\
(C) $x = 29$, $y = 169$.

\end{document}


\section{\textbf{Safe Boolean MRSW Register}}
% ------------------------------------------------------------------------------------ %
\subsection{Particular Case}
\par
In class we saw the difference between Safe, Regular and Atomic Registers. In
this exercise we are experimenting with a Safe Boolean Register. In particular,
we are trying a register that allows one single writer whose written value can be
examined by multiple readers. We need to remember that Safe registers are those
that :
\begin{itemize}
\item If a read does not overlap with a write, then the read returns the last
written value
\item If a read overlaps with a write, then the read can return any value in the
domain of the register's values
\end{itemize}
% ------------------------------------------------------------------------------------ %
\subsection{Solution}
\par
One way to achieve this behavior is using a SRSW Safe register. The code that is
provided with the book does this by having a boolean SRSW register per thread,
in other words, it is an array of boolean values. When doing a write, the writer
iterates over this array and writes the new value. The readers simply access its
assigned slot in the array (based on the thread id) and return the stored
value. The code for this type of register is shown next:
\par
\begin{lstlisting}[style=numbers]
  public Boolean read() {
    return s_table[ThreadID.get()];
  }

  public void write(Boolean x) {
    for (int i = 0; i < s_table.length; i++)
      s_table[i] = x;
  }
\end{lstlisting}
\hfill
\par
Please note how this type of register does not make use of any synchronization
mechanism.
% ------------------------------------------------------------------------------------ %
\subsection{Experiment Description}
\par
This program provides two test cases. The first one is a sequential test and the
second one is a parallel test. The former simply calls a write and then a read
from the same thread. This is not rocket science. The reader must retrieve what
the writer wrote.
\par
The second test case is slightly more interesting. The writer writes first one
value and then another one. After that, 8 reader threads are started and they
read the last written value. According to the rules of a safe register, all
threads must read the same value because the reads and the writes did not
overlap.
\par
% ------------------------------------------------------------------------------------ %
\subsection{Sample Results}
\par
We found out that the tests failed occasionally. The manifestation of the failure is a
\par
\textit{indexOutOfBoundsException} as shown above.
\begin{verbatim}
[oraadm@gdlaa008 Register]$ junit register.SafeBooleanMRSWRegisterTest
.sequential read and write
.parallel read
Exception in thread "Thread-7" java.lang.ArrayIndexOutOfBoundsException: 8
        at register.SafeBooleanMRSWRegister.read(SafeBooleanMRSWRegister.java:26)
        at register.SafeBooleanMRSWRegisterTest$ReadThread.run(SafeBooleanMRSWRegisterTest.java:62)

Time: 0.008

OK (2 tests)
\end{verbatim}
\par
So, the way to fix this problem was to make sure that in each test case
the threads ids are reset calling the static method $ThreadID.reset()$. With
this hack, the tests passed.
\par
\begin{lstlisting}[style=numbers]
  /**
   * Sequential calls.
   */
  public void testSequential() {
    ThreadID.reset(); // We have to reset the thread ids
    System.out.println("sequential read and write");
    instance.write(true);
    boolean result = instance.read();
    assertEquals(result, true);
  }

  /**
   * Parallel reads
   */
  public void testParallel() throws Exception {
    ThreadID.reset(); // We have to reset the thread ids
    instance.write(false);
    instance.write(true)
    System.out.println("parallel read"
    for (int i = 0; i < THREADS; i++) {
      thread[i] = new ReadThread(
    }
    for (int i = 0; i < THREADS; i ++) {
      thread[i].start();
    }
    for (int i = 0; i < THREADS; i ++) {
      thread[i].join();
    }
  }
\end{lstlisting}
\par
And here is the resulting execution after this fix:
\begin{verbatim}
[oraadm@gdlaa008 Register]$ junit register.SafeBooleanMRSWRegisterTest
.sequential read and write
.parallel read

Time: 0.004

OK (2 tests)
\end{verbatim}
\par
% ------------------------------------------------------------------------------------ %
\subsection{Interpretation}
\par
In this experiment we saw a way of implementing Safe Multi-Reader Single-Writer
registers. These registers were able of storing boolean values.
\par
Unfortunately, none of the test cases exercises the case where we have
concurrent writes and reads. Hence, this experiment only exercised one of the
two rules of a safe register.
% ------------------------------------------------------------------------------------ %


\documentclass{article}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{bm}
\usepackage{pdfpages}

\newcommand{\bra}[1]{\langle#1|}
\newcommand{\ket}[1]{|#1\rangle}

\begin{document}

\title{Untangling hyperfine level malarkey}
\date{}
\maketitle

\section{Context}

Figure \ref{data_peaks} shows absorption peaks for laser light to helium at frequencies denoted by the x-axis in a DC magnetic field of strength 11G and 18G (blue and red respectively). The atom is initially in the $m_J = 2$ state of the $2^3P_2$ level.

These lines are identified as transitions to the $5^3D_2$ and $5^3D_3$ levels. We also have measured frequencies for the $2^3P_2\rightarrow5^3D_1$ transition in both background fields. The problem statement is: \emph{Given} the experimentally measured peak frequencies and \emph{assuming} the quantum mechanical description of the atom, \emph{determine} the absolute frequencies of the $2^3P_2\rightarrow 5_D$ transitions in the unperturbed helium atom. The outcome is currently a partial solution, and an indication of what is needed to reach a more complete solution.

\begin{figure}[h]
\includegraphics[width=\textwidth]{figs/5^3D_3_plot_combo.png}
\label{data_peaks}
\caption{Beautiful peaks, look at them shine}
\end{figure}

\subsection{Old method}
Previously, I used a linear fit to the peak values as a function of field strength to extrapolate to the zero-field values. This isn't horrifically wrong but it is incorrect because for small field values, there is a nonlinear shift in the closely-spaced energies of the $D_2$ and $D_3$ levels, as explained below. Therefore one must use Actual Theory to work obtain the field-free transition energies.

I asked Danny for some assistance here, and what follows is an extension of the background material he provided.

\section{Identifing peaks}

Figure \ref{theory_plots} shows the frequencies of the transitions to the $J=2$ and $J=3$ levels, relative to the zero-field $J=3$ level, as a function of field strength assuming a 21MHz splitting between the bare levels. Shown are the $m_j$ = 1 (black), 2 (blue), and 3 (red) lines from each level (the $J=3$ being the lowest). Notice that at 11G and 18G, the experimentally relevant fields, the $|J,m_J\rangle = |3,2\rangle$ and $|J,m_J\rangle = |2,1\rangle$ lines are very close, or even intersecting. In the red spectrum in figure 1, one can just make out two distinct peaks in the central peak. We conclude that the larger central peak in both spectra is the coincidence of the two transitions. Checking the metadata for this experimental run confirms that the waveplate was set at an angle such that some $\pi$ light was present, which is different from the other measured spectra (not shown here).

The question now is to determine the zero-field value of the fine structure splitting. We will use the smaller extremal peaks, the $|J,m_J\rangle = |3,1\rangle$ and $|J,m_J\rangle = |2,2\rangle$ lines, for this calculation as they are sufficiently distinct. The peak separation depends on the zero-field splitting and the magnetic field strength. By fixing the latter we can constrain the former. By minimizing the squared difference between the predicted and measured interval $f_{|3,1\rangle}-f_{|2,2\rangle}$, we constrain the splitting. Figure 2b. shows the squared error as a function of splitting value, which is minimized at 21MHz, 2MHz larger than the 19MHz prediction given in Wiese and Fuhr.

This does not, unfortunately, give us the absolute frequency of these levels. In this calculation, the $J=1-J=3$ interval was assumed to be 315 MHz (actually larger than the predicted 303MHz, but the interference from a peak this far away should be small). In the next part I suggest a few ways forward.

\begin{figure}
\includegraphics[width=\textwidth]{figs/theory_splitting_work.png}
\label{theory_plots}
\caption{Theoretical lines as a function of magnetic field strength for fixed splitting, and the sum-square error of the predicted $f_{|3,1\rangle}-f_{|2,2\rangle}$, summed over measurements at both field values, versus the fine structure splitting.}
\end{figure}

\section{Determination of the absolute transition frequencies}

The current methods allow us to determine the fine structure splitting between the $J=2$ and $J=3$ levels of the helium $5^3D$ manifold (Are those terms right?). Determining the absolute frequency will require additional intervals. Up until now, the $J=3-J=1$ interval was input to the calculations. This is circular, assuming the theory we are trying to test. We would do better to use our experimental data and the quantum theory of the atom to work backwards to the field intervals. We could do this by extending the treatment above with a three-parameter fit - the absolute frequency of the $J=1$ transition, and the fine structure intervals (or, equivalently, the absolute frequencies of each transition), fitting to all available peaks as listed in table \ref{peaktable}.

\section{Extending work}

Whereas previously we just used the CG coefficients givin the $\bra{Jm_J}$ basis in terms of the $\bra{m_L m_S}$ basis, using only the $m_J=1$ subspace, we now need to extend this to the $m_J=2$ and $m_J=3$ subspace. This is just a matter of writing some bigger matrices, but involves calculating/looking up more CG coefficients.

(remember the $\ket{\alpha}$ basis is the $\ket{Jm_J}$ basis and the $\ket{\beta}$ basis is the $\ket{m_Lm_S}$ basis.)

So we want to work in the subspace spanned by
\begin{equation}
	\bm{\alpha} = \{\ket{11},\ket{21},\ket{22},\ket{31},\ket{32}\}
\end{equation}

We also have one (bad) measurement which includes the $\ket{33}$ line but we don't talk about that.

What's the basis we want to project back on to, then? The $\ket{\beta}$ basis, subject to $m_L+m_S=m_J$.
In the upper state, $L=2$ so $m_L\in\{-2,-1,0,1,2\}$ and $m_S\in\{-1,0,1\}$ right?
and $m_J\in\{1,2\}$.

\begin{equation}
	\bm{\beta} = \{\ket{01},\ket{10},\ket{11},\ket{2-1},\ket{20}\}
\end{equation}

Ok great, now just need to look up the CG coefficients! And can look at Danny's work to confirm. U transforms the $m_L m_S$ basis into the $J m_J$ basis.
% We have L = j1 = 2, S = j2 = 1 (angular and spin momenta)
% And the coefficients are: m_L = 2 1 or 0, m_S = -1 0 or 1

% j=1 m=1
% mL mS CG
% 2 -1 \sqrt{3/4)
% 1 0 -\sqrt{3/10)
% 0 1 \sqrt{1/10)

% j=2 m=1
% mL mS CG
% 2 -1 \sqrt{1/3)
% 1 0 \sqrt{1/6)
% 0 1 -\sqrt{1/2)

% j=2 m=2
% mL mS CG
% 2 0 \sqrt{2/3)
% 1 1 -\sqrt{1/3)

% j=3 m=1
% mL mS CG
% 2 -1 \sqrt{1/15)
% 1 0 \sqrt{8/15)
% 0 1 \sqrt{2/5)

% j=3 m=2
% mL mS CG
% 2 0  \sqrt{1/3)
% 1 1 \sqrt{2/3)
H\alpha = \mu_B/\hbar B (m_L+2m_s)

U is:
\begin{equation}
\begin{bmatrix}
% 01		  	10					11			2-1			20
\sqrt{1/10} &-\sqrt{3/10}	&   0		&  \sqrt{3/5}	&    0	\\ %\J mJ = 11
-\sqrt{1/2}	& \sqrt{1/6}	&	0 		&  \sqrt{1/3}	&    0	\\ %\J mJ = 21
	0		&	0			&-\sqrt{1/3}&  	0  			&   \sqrt{2/3} 	\\ %\J mJ = 22
\sqrt{2/5}  &	\sqrt{8/15}	&	 0		&  \sqrt{1/15}  &    0 	\\ %\J mJ = 31
	0		&	0			&\sqrt{2/3} &  0   			&    \sqrt{1/3}	\\ %\J mJ = 32
\end{bmatrix}
\end{equation}
\begin{table}
\label{peaktable}
\begin{tabular}{|c c|c|c|}
\hline
Line & Field & Peak centre & peak width\\
\hline
$5^3D_{1}$ & $|B| = 18G$& 744396451.51(0.10) MHz & 6.43(0.29) MHZ\\
			& $|B| = 11G$& 744396475.92(0.12) MHz & 5.15(0.33) MHZ\\
\hline
$5^3D_{2,3}$ & $|B| = 18G$& 744396158.56(0.21) MHz & 3.44(0.55) MHZ\\
						&& 744396191.07(0.16) MHz & 8.56(0.49) MHZ\\
						&& 744396221.11(0.16) MHz & 3.67(0.44) MHZ\\
			& $|B| = 11G$& 744396180.50(0.14) MHz & 4.34(0.40) MHZ\\
						&& 744396204.54(0.13) MHz & 7.33(0.39) MHZ\\
						&& 744396222.65(0.16) MHz & 4.75(0.47) MHZ\\
\hline
\end{tabular}
\caption{Relevant measured peak frequencies}
\end{table}

\appendix
\includepdf[pages=-]{doc/20200218_danny_theory_1}

\end{document}