\documentclass[12pt]{article}
%Sets size of page and margins
\oddsidemargin 1mm  \evensidemargin 1mm
%\topmargin -25mm %   \headheight 0pt   \headsep 0pt
\textwidth 16cm

\title{Manual for LIEPDE}
\author{Thomas Wolf \\
        Department of Mathematics \\
        Brock University \\
        St.Catharines \\
        Ontario, Canada L2S 3A1 \\
        twolf@brocku.ca}

\begin{document}
\maketitle
\section{Purpose}
The procedure {\tt LIEPDE} computes infinitesimal symmetries
for a given single/system of differential equation(s) (ODEs or PDEs)
%\begin{equation}
%     u^{\alpha}_J = w^{\alpha}(x,u^{\beta},...,u^{\beta}_K,...)  \label{a1}
%\end{equation}
\begin{equation}
H_A = 0.              \label{PDEs}
\end{equation}
To obey symmetries, differential equations (\ref{PDEs})
for unknown functions $u^\alpha$ of independent variables $x^i$
must be form-invariant under infinitesimal transformations
\begin{equation}
\tilde{x}^i = x^i + \varepsilon \xi^i, \;\; \;\;\;
        \tilde{u}^\alpha = u^\alpha + \varepsilon \eta^\alpha  \label{tran}
\end{equation}
of first order in $\varepsilon.$ To transform the equations (\ref{PDEs})
by (\ref{tran}), derivatives of $u^\alpha$ must be transformed, i.e. the part
linear in $\varepsilon$ must be determined. The corresponding formulas are
(see e.g. \cite{Olv}, \cite{Step})
\begin{eqnarray}
\tilde{u}^\alpha_{j_1\ldots j_k} & = &
u^\alpha_{j_1\ldots j_k} + \varepsilon
\eta^\alpha_{j_1\ldots j_k} + O(\varepsilon^2)  \nonumber \\ \vspace{3mm}
\eta^\alpha_{j_1\ldots j_{k-1}j_k} & = &
  \frac{D \eta^\alpha_{j_1\ldots j_{k-1}}}{D x^k} -
  u^\alpha_{ij_1\ldots j_{k-1}}\frac{D \xi^i}{D x^k} \label{recur}
\end{eqnarray}
and the complete symmetry condition then takes the form
\begin{eqnarray}
X H_A & = & 0 \;\; \;\pmod{ H_A = 0}  \label{sbed1} \\
X & = & \xi^i \frac{\partial}{\partial x^i} +
 \eta^\alpha \frac{\partial}{\partial u^\alpha} +
 \eta^\alpha_m \frac{\partial}{\partial u^\alpha_m} +
 \eta^\alpha_{mn} \frac{\partial}{\partial u^\alpha_{mn}} + \ldots +
 \eta^\alpha_{mn\ldots p} \frac{\partial}{\partial u^\alpha_{mn\ldots p}} ,
\label{sbed2}
\end{eqnarray}
where mod $H_A = 0$ means that the original PDE-system is used to replace
some partial derivatives of $u^\alpha$ to reduce the number of independent
variables, because the symmetry condition (\ref{sbed1}) must be
fulfilled identically in $x^i, u^\alpha$ and all partial
derivatives of $u^\alpha.$

For point symmetries $\xi^i, \eta^\alpha$ are functions of $x^j,
u^\beta$ only. For more general higher order symmetries $\xi^i, \eta^\alpha$
may depend on derivatives of $u^\beta$. For those symmetries one can
without loss of generality set $\xi^i=0$ due to a symmetry of the symmetry
conditions on the manifold of solutions of $H_A=0$
themselves (e.g.\ $\S$5.1 in \cite{Olv}). The shifted generators
\[\tilde{\xi^i} = \xi^i + h^i, \; \; \; \;
  \tilde{\eta^{\alpha}} = \eta^{\alpha} + h^i u^{\alpha},_i\]
with arbitrary $h^i=h^i(x^j, u^{\beta},\ldots, u^{\beta}_K)$
represent generators of the same symmetry.

\section{Syntax of {\tt LIEPDE}}
The procedure {\tt LIEPDE} is called through \\
{\tt LIEPDE({\it problem,symtype,flist,inequ}); } \\
All parameters are lists. \vspace{6pt} \\
The first parameter specifies the DEs to be investigated: \\
{\it problem} has the form \{{\it equations, ulist, xlist}\} where
\begin{tabbing}
\hspace{0.5cm}
 {\it equations } \=  is a list of equations,
              each has the form {\tt df(ui,..)=...} where \\
       \> the LHS (left hand side) {\tt df(ui,..)} is selected such that \\
       \>  - The RHS (right h.s.) of an equations must not include     \\
       \>$\;\,$ the derivative on the LHS nor a derivative of it.  \\
       \>  - Neither the LHS nor any derivative of it of any equation \\
       \>$\;\,$ may occur in any other equation.\\
       \>  - Each of the unknown functions occurs on the LHS of \\
       \>$\;\,$ exactly one equation.  \\
\hspace{0.5cm}
 {\it ulist} \>  is a list of function names, which can be chosen freely \\
\hspace{0.5cm}
 {\it xlist}  \>  is a list of variable names, which can be chosen freely
\end{tabbing}
Equations can be given as a list of single differential expressions and then
the program will try to bring them into the `solved form' {\tt df(ui,..)=...}
automatically. If equations are given in the solved form then the above
conditions are checked and execution is stopped it they are not satisfied.
An easy way to get the equations in the desired form is to use \\
\verb+    FIRST SOLVE({+{\it eq1,eq2,}...\verb+},{+{\it one highest
derivative for each function u}\verb+})+  \\
(see the example of the Karpman equations in {\tt LIEPDE.TST}).
The example of the Burgers equation in {\tt LIEPDE.TST} demonstrates
that the number of symmetries for a given maximal order of the infinitesimal
generators depends on the derivative chosen for the LHS.

The second parameter {\it symtype} of {\tt LIEPDE} is a list $\{\;\}$ that
specifies the symmetry to be calculated. {\it symtype} can have the following
values and meanings:
\begin{tabbing}
\verb+{"point"}         + \= Point symmetries with $\xi^i=\xi^i(x^j,u^{\beta}),\;
                           \eta^{\alpha}=\eta^{\alpha}(x^j,u^{\beta})$ are \\
                          \> determined.\\
\verb+{"contact"}+ \> Contact symmetries with $\xi^i=0, \;
                            \eta=\eta(x^j,u,u_k)$ are \\
\> determined $(u_k = \partial u/\partial x^k)$, which is only applicable if a \\
                      \> single equation (\ref{PDEs}) with an order $>1$ for a
                             single function \\
                      \> $u$ is to be investigated. (The {\it symtype}
                             \verb+{"contact"}+ \\
\> is equivalent to \verb+{"general",1}+ (see below) apart from \\
\> the additional checks done for \verb+{"contact"}+.)\\
\verb+{"general"+,{\it order}\verb+}+ \> where {\it order} is an integer $>0$.
      Generalized symmetries $\xi^i=0,$ \\
\> $\eta^{\alpha}=\eta^{\alpha}(x^j,u^{\beta},\ldots,u^{\beta}_K)$
      of a specified order are determined \\
\> (where $_K$ is a multiple index representing {\it order} many indices.) \\
\> NOTE: Characteristic functions of generalized symmetries \\
\> ($= \eta^{\alpha}$ if $\xi^i=0$) are equivalent if they are equal on\\
\> the solution manifold. Therefore, all dependences of\\
\> characteristic functions on the substituted derivatives \\
\> and their derivatives are dropped. For example, if the heat \\
\> equation is given as $u_t=u_{xx}$ (i.e.\ $u_t$ is substituted by $u_{xx}$) \\
\> then \verb+{"general",2}+ would not include characteristic \\
\> functions depending on $u_{tx}$ or $u_{xxx}$. \\
\> THEREFORE: \\
\> If you want to find {\it all} symmetries up to a given order then either \\
\> - avoid using $H_A=0$ to substitute lower order \\
\> $\;\,$derivatives by expressions involving higher derivatives, or \\
\> - increase the order specified in {\it symtype}. \\
\> For an illustration of this effect see the two symmetry \\
\> determinations of the Burgers equation in the file \\
\> {\tt LIEPDE.TST}. \\
\verb+{xi_+{\it x1}\verb+ =...,..., +  \>  \\
\verb+ eta_+{\it u1}\verb+=...,...}+  \> It is possible to specify an
ansatz for the symmetry. Such \\
\> an ansatz must specify all $\xi^i$ for all independent variables and \\
\> all $\eta^{\alpha}$ for all dependent variables in terms of differential \\
\> expressions which may involve unknown functions/constants. \\
\> The dependences of the unknown functions have to be declared \\
\> in advance by using the {\tt DEPEND} command. For example, \\
\> \verb+    DEPEND f, t, x, u$  + \\
\> specifies $f$ to be a function of $t,x,u$. If one wants to have $f$ as \\
\> a function of derivatives of $u(t,x)$, say $f$ depending on $u_{txx}$, \\
\> then one \underline{{\it cannot}} write \\
\> \verb+    DEPEND f, df(u,t,x,2)$ + \\
\> but instead must write \\
\> \verb+    DEPEND f, u!`1!`2!`2$ + \\
\> assuming {\it xlist} has been specified as \verb+ {t,x}+.
   Because $t$ is the \\
\> first variable and $x$ is the second variable in {\it xlist} and $u$ is \\
\> differentiated once wrt.\ $t$ and twice wrt.\ $x$ we therefore \\
\> use \verb+ u!`1!`2!`2+. The character {\tt !} is the escape character \\
\> to allow special characters like ` to occur in an identifier. \\
\> \hspace{4mm} For generalized symmetries one usually sets all $\xi^i=0$.\\
\> Then the $\eta^{\alpha}$ are equal to the characteristic functions.
\end{tabbing}
\noindent The third parameter {\it flist} of {\tt LIEPDE} is a list $\{\;\}$
that includes
\begin{itemize}
\item all parameters and functions in the equations which are to
      be determined such that symmetries exist (if any such
      parameters/functions are
      specified in {\it flist} then the symmetry conditions
      formulated in {\tt LIEPDE}
      become non-linear conditions which may be much harder for
      {\tt CRACK} to solve with many cases and subcases to be considered,
      see the last example in {\tt LIEPDE.TST}),
\item all unknown functions and constants in the ansatz
      \verb+xi_..+ and \verb+eta_..+
      if that has been specified in {\it symtype}.
\end{itemize}
\noindent The fourth parameter {\it inequ} of {\tt LIEPDE} is a list $\{\;\}$
that includes all non-vanishing expressions which represent
inequalities for the functions in flist.

The procedure {\tt LIEPDE} returns a list of cases. Normally this will be
only one case but if a parameter in the equation(s) had to be determined
then there may be different cases, each with different values of these
parameters, just like in the last example of {\tt LIEPDE.TST}, see also
the end of the log file {\tt LIEPDE.RLG}. Each case contains a list of
unsolved conditions if any, a list containing the general solution for
$\xi^i, \eta^{\alpha}$ and a list of constants and functions appearing
in the general solution or in the remaining unsolved conditions.

\section{Flags, parameters}
Two flags specify whether symmetry conditions are formulated
and solved in stages or in one go.

If the equation to be investigated is of higher than first order
and point symmetries are investigated then {\tt LIEPDE} allows
a set of preliminary conditions
to be formulated and solved before formulating and
solving the full set of conditions for this equation (more
details in \cite{Step},
\cite{Wo}). This successive execution is enabled by setting \\
\verb+  LISP(PRELIM_:=t)$+. \\
The default value is \\
\verb+  LISP(PRELIM_:=NIL)$+. \\
If the preliminary conditions are easy to solve completely then
it is advantageous
to formulate and solve them first, otherwise the formulation of
the complete more
overdetermined condition is better. Examples for both cases are
given together with comments in {\tt LIEPDE.TST}.

If symmetries of a system of equations are to be investigated
then with the setting \\
\verb+LISP(INDIVIDUAL_:=t)$+ conditions for the equations are formulated
and solved individually which provides a speed up if symmetry conditions are
very overdetermined. The default value is \\
\verb+  LISP(INDIVIDUAL_:=NIL)$+.

If parameters of the equation(s) are to be determined such that
symmetries exist, i.e.\ the parameter {\it flist} is not the empty
list \verb+{}+ then {\tt PRELIM\_} and {\tt INDIVIDUAL\_} are both
automatically set to {\tt NIL}.

By default {\tt LIEPDE} computes $\xi$ and $\eta$ for each
symmetry. If a prolongation of the symmetry vector shall be calculated
then the order of this prolongation can be specified by the setting
\verb+LISP(PROLONG_ORDER:= ...)$+ (see last example in {\tt LIEPDE.TST}).

With \verb+LISP(BATCH_MODE_SUB:=nil}$+ one can solve the overdetermined system
interactively. By default the program {\tt CRACK} will try to solve the
symmetry conditions automatically. Interactive solution is to be preferred if
the computation is very large or if the symmetry condition is non-linear,
i.e.\ if {\it flist} contains parameters of the equation which also have
to be computed.

Flags that control the solution of the symmetry conditions by {\tt CRACK}
are shown after pressing {\tt h} in interactive mode. You can also
try them out online in \\ \verb+http://lie.math.brocku.ca/crack/demo+.
Among them is: \\
\verb+  LISP (PRINT_:= NIL/0/1/ ...)$+ \\
\verb+PRINT_=NIL+ suppresses all CRACK output, for \verb+PRINT_=n+
($n$ a positive integer)
{\tt CRACK} prints only equations with at most $n$ factors in their terms.

\section{Requirements}

{\tt REDUCE 3.6} or higher and
the files {\tt CRACK.RED, LIEPDE.RED} and
all files {\tt CR*.RED} which are read in from {\tt CRACK.RED}. \\
\verb+    IN "crack.red","liepde.red"$+ \\
(and appropriate paths) or compilation with \\
\verb+    FASLOUT "crack"$+ \\
\verb+    IN "crack.red"$+ \\
\verb+    FASLEND$+ \\
\verb+    FASLOUT "liepde"$+ \\
\verb+    IN "liepde.red"$+ \\
\verb+    FASLEND$+ \\
\verb+    BYE$+ \\
and loading afterwards with \verb+ LOAD_PACKAGE crack,liepde$.+

\begin{thebibliography}{99}
\bibitem{Olv} P.J. Olver, Applications of Lie Groups to Differential
      Equations, Springer-Verlag, New York (1986).
\bibitem{Step} H. Stephani, Differential Equations, Their solution using
      symmetries, Ed. M.A.H. MacCallum, Cambridge Univ. Press (1989).
\bibitem{Wo} T. Wolf, An efficiency improved program LIEPDE for
          determining Lie-symmetries
          of PDEs, Proceedings of ``Modern Group Analysis: advanced analytical
          and computational methods in mathematical physics'', Acireale, Italy,
          October 1992, Kluwer Academic Publishers, 377-385, 1993.
\end{thebibliography}

\end{document}


\epigraph{``It's known in theory that log(log(n)) approaches infinity, but no one has ever observed it in practice."}{Grant Sanderson}

To prove the hypothesis that `it is possible to create open source implementation to simulating atmospheric dynamics both in the troposphere and the stratosphere, that such a software is consistent and reliable, and that the software consists of high quality source code', it is necessary to carry out a series of appropriate benchmarks.

Considering there is no open source software to which one can compare the software, it was determined that it was necessary to carry out a series of appropriate benchmarking within the following areas: performance, accuracy, and code quality. The performance benchmark would demonstrate whether or not the software has consistent and reliable performance, the accuracy benchmark would highlight whether or not the forecasts produced by the software had a reasonable level of accuracy, and the code quality benchmark would indicate whether or not the source code of the software was of high quality.

Please note that the source code of all the benchmarks that will be performed can be found in Appendix \ref{code}.

\section{Performance Benchmark}
When undertaking a software benchmark of any kind, it is essential to ensure that the benchmarking method is repeatable and minimises sources of error as much as possible.

In my case, this was achieved by performing the benchmark a total of ten times each on three completely different machines. The first machine selected was the MacBook Air (Retina, 2018), the second machine being a custom PC desktop build, and the final machine being a virtual machine hosted by the Google Cloud Platform. These machines were chosen quite deliberately, as the three machines provided a contrast of performance between a  high end laptop, a high end desktop, and a virtual machine.

The hardware specifications for all the machines selected are detailed in subsection \ref{specs}.

\subsection{Hardware Specifications}\label{specs}
\begin{center}
\begin{tabular}{|c|c|}
 \hline
  & MacBook Air (Retina, 2018) \\
 \hline
 \textbf{CPU} & 1.6 GHz Intel Core i5-8210Y \\
 \hline
 \textbf{Memory} & 8 GB \\
 \hline
 \textbf{Operating System} & macOS Catalina (Version 10.15) \\
 \hline
\end{tabular}\par
\bigskip
Table 5.1.: MacBook Air (2018) Hardware Specifications
\end{center}

\begin{center}
\begin{tabular}{|c|c|}
 \hline
  & Custom PC Desktop Build \\
 \hline
 \textbf{CPU} &  3.2 GHz Intel Core i5-6500 \\
 \hline
 \textbf{Memory} & 8 GB \\
 \hline
 \textbf{Operating System} & Windows 10 Home Version 1803 \\
 \hline
\end{tabular}\par
\bigskip
Table 5.2.: Custom PC Desktop Build Hardware Specifications
\end{center}

\begin{center}
\begin{tabular}{|c|c|}
 \hline
  & Compute Engine n2-standard-2 \\
 \hline
 \textbf{CPU} & 2.8 GHz Intel Xeon CPU \\
 \hline
 \textbf{Memory} & 8 GB \\
 \hline
 \textbf{Operating System} & Ubuntu 19.04 \\
 \hline
\end{tabular}\par
\bigskip
Table 5.3.: Compute Engine Hardware Specifications
\end{center}

\subsection{Benchmarking Method}
\begin{definition}
Algorithm is a set of mathematical instructions or rules that, especially if given to a computer, will help to calculate an answer to a problem.
\end{definition}

\subsubsection{Python vs. Cython Benchmark}
The aspect of the performance that is being benchmarked in this particular section, is the length of time it takes to generate a forecast of a fixed length at different levels of computational detail, and following which, comparing the performances of the Cython version of AMSIMP and the Python version. Due to Cython's statically typed nature, my hypothesis was that `there is a significant difference in execution time between the Cython version of AMSIMP and the Python version'. To demonstrate that using the programming language of Cython will result in a statistically significant difference in performance, it was decided to analyse the results of the benchmark using Welch's t-test in order to determine the p-value of the null hypothesis. The primary reason for choosing Welch's t-test was that it did not assume an equal population variance. The null hypothesis states that `there is no significant difference in execution time between the Cython version of AMSIMP and the Python version'. The algorithm for this particular benchmark is included below:

\begin{algorithm}[H]
    \caption{Comparison Algorithm}
    \begin{algorithmic}[1]
        \State $versions \gets $ The versions of AMSIMP being utilised (Python, and Cython).
        \State $num \gets $ Number of samples.
        \State $forecast \gets $ The maximum length of a forecast.
        \Function{benchmark}{$v, n, f$}
            \For{$v \texttt{ in } versions$} $\gets v$ is the version of AMSIMP that will be benchmarked in a particular round.
                \While{$n \leq num$}
                    \If{$f \leq forecast$}
                        \State $start \gets$ Time at outset.
                        \State $\texttt{Generates a forecast for the specified number of days.}$
                        \State $finish \gets$ Time at completion.
                        \State $execution = finish - start \gets $ Execution time.
                    \EndIf
                \EndWhile
            \EndFor
        \EndFunction
    \end{algorithmic}
\end{algorithm}

\subsubsection{Performance Reliability Benchmark}
The aspect of the performance that is being benchmarked in this particular section, is the length of time it takes to generate a forecast of different lengths at a fixed level of computational detail. The algorithm for this particular benchmark is quite similar to the algorithm present in the previous sub-subsection and is included below:

\begin{algorithm}[H]
    \caption{Performance Reliability Algorithm}
    \begin{algorithmic}[1]
        \State $num \gets $ Number of samples.
        \State $forecast \gets $ The maximum length of a forecast.
        \Function{benchmark}{$n, f$}
            \While{$n \leq num$}
                \If{$f \leq forecast$}
                    \State $start \gets$ Time at outset.
                    \State $\texttt{Generates a forecast for the specified number of days.}$
                    \State $finish \gets$ Time at completion.
                    \State $execution = finish - start \gets $ Execution time.
                \EndIf
            \EndWhile
        \EndFunction
    \end{algorithmic}
\end{algorithm}

To prove, however, that the software is consistent and reliable, it is necessary to perform a statistical analysis. In my specific case, the standard deviation was used in order to determine how spread out the data is at each detail level, or how wildly the performances varies at each detail level. After which, the coefficient of variation was determined. As a rule of thumb, a coefficient of variation $\geq$ 1 indicates a relatively high variation, while a coefficient of variation $<$ 1 can be considered low.

\section{Accuracy Benchmark}
The aspect that is being benchmarked in this particular section, is the accuracy of the forecasted conditions produced by the software, on a given day. The algorithm for this particular benchmark is quite similar to the algorithm present in the previous sub-subsection and is included below:

\begin{algorithm}[H]
    \caption{Accuracy Algorithm}
    \begin{algorithmic}[1]
        \State $ current \gets $ The atmospheric conditions on a given day.
        \State $ n \gets $ The forecasted conditions on a given day.
        \Function{benchmark}{$n, f$}
            \State $mape = mean(\frac{\mid current - n \mid}{current})$
            \State $mape = median(\frac{\mid current - n \mid}{current})$
        \EndFunction
    \end{algorithmic}
\end{algorithm}

In order to determine whether the forecast produced was accurate or inaccurate based on the mean and median absolute percentage error, it was necessary to carry out a comparison in relation to something. To do this, the following table was taken from the book entitled, `Industrial and business forecasting methods'\cite{mape}:

\hfill

\begin{center}
    \begin{tabular}{|c|c|}
     \hline
     MAPE / MdAPE & Interpretation \\
     \hline
     $<10$  & Highly accurate forecasting \\
     \hline
     $\geq 10<20 $ & Good forecasting \\
     \hline
     $\geq 20<50 $ & Reasonable forecasting \\
     \hline
     $>50$ & Inaccurate forecasting \\
     \hline
    \end{tabular}\par
    \bigskip
    Table 5.4.: Interpretation of the Mean and Median Absolute Percentage Errors.
\end{center}

\section{Code Quality Benchmark}
\subsection{What is High Quality Source Code?}
High quality source code is a crucial factor in determining the usefulness of any particular piece of software. It can generally be identified by three distinct factors: it does what it is supposed to do, it does not contain defects, and it is easy to read, maintain and extend\cite{code_quality}.

The last factor is of particular importance to the open source development nature of a piece of software, like mine. Imagine for a moment, that a developer would like to add a new feature and the original developer is unavailable to do so. If the code is easy to read, it’s also easy to add new features without disrupting existing ones. If the code is not, the opposite is true.

\subsection{Linting Benchmark}
So, high quality source code is important, but, how can we tell if my software consists of such code? By using a tool known as a linter.

\begin{definition}
Linters analyse code to detect various categories of lint (A lint is basically a piece of defective code).
\end{definition}

According to Alexander VanTol\cite{code_quality}, those categorises can be defined by the following:

\begin{enumerate}
    \item Logical Lint
    \begin{itemize}
        \item Code errors
        \item Code with potentially unintended results
        \item Dangerous code patterns
    \end{itemize}
    \item Stylistic Lint
    \begin{itemize}
        \item Code not conforming to defined conventions
    \end{itemize}
\end{enumerate}

For the purposes of this project and considering Python was originally utilised as the programming language for the software, a linter known as, Pylint\cite{pylint} was used. Pylint is one of the oldest linters and is extremely well-maintained. It also has been around long enough that most major bugs have been fixed. This particular linter was chosen primarily because of its aforementioned software stability, but also, due to the fact that it provides a numerical rating of the quality of the source code. This numerical rating can then be correlated to a meaning, as is shown in Table 5.3.\cite{pylint_score}.
\hfill

\begin{center}
\begin{tabular}{ c|c }
 Pylint Score & Means \\
 \hline
 $<$ 0.0 & Trouble ahead \\
 0.0 - 5.0 & Needs cleanup \\
 5.0 - 7.0 & Reasonable quality \\
 $>$ 7.0 & Great code!
\end{tabular}\par
\bigskip
Table 5.4.: Code Score from Pylint
\end{center}

To perform this particular benchmark on my software, the following command was excuted in a terminal (bash):

\begin{minted}[mathescape,linenos,frame=lines]{bash}
$ pylint amsimp
\end{minted}

\subsection{Coverage Benchmark}
\begin{definition}
Code Coverage is a measure used to describe the degree to which the source code of a program is executed when a particular test suite runs.
\end{definition}

The aspect that is being benchmarked in this particular section, is the amount of source code executed by the test suite of the software. For the purposes of this project Coverage.py will be utilised, as it is the sole coverage analysis tool for the programming languages of Python, and Cython \cite{coverage_py}. The formula used for calculating the coverage of a particular file, or package can be seen in equation \ref{coverage_py}, and is usually presented in percentage form:

\begin{equation}
    \label{coverage_py}
    \% = \frac{\texttt{The number of lines of code that are executed}}{\texttt{The total number of lines of executable code}} \cdot 100
\end{equation}

A high code coverage, above approximately 80 \%, tells us that a piece of software has more of its source code executed during testing, which implies that the software has a lower chance of containing undetected bugs, ultimately demonstrating that the quality of the source code is high. In order to present the results in a more accessible format, Codecov was utilised\cite{codecov}. Codecov provides a wide variety of graphs, and a summary of the results of the coverage benchmark.

\begin{figure}[H]
    \centering
    \includegraphics[width=.8\linewidth]{Images/codecov.png}
    \caption{A screenshot of the coverage benchmark results for the software on Codecov.}
    \label{codecov_image}
\end{figure}


%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Problem Set/Assignment Template to be used by the
%% Food and Resource Economics Department - IFAS
%% University of Florida's graduates.
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Version 1.0 - November 2019
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Ariel Soto-Caro
%%  - asotocaro@ufl.edu
%%  - arielsotocaro@gmail.com
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\documentclass[12pt]{article}
\usepackage{design_ASC}

\theoremstyle{definition}
\usepackage{longtable}
\newtheorem{exmp}{Example}[section]
\newtheorem{slo}{Definition}[section]
\newcommand*{\Perm}[2]{{}^{#1}\!P_{#2}}%
\newcommand*{\Comb}[2]{{}^{#1}C_{#2}}%
\setlength\parindent{0pt} %% Do not touch this
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
%% -----------------------------
%% TITLE
%% -----------------------------
\title{\textbf{Discrete Random variable}} %% Assignment Title

\author{\textbf{Ibrahim Abou Elenein}}

\date{\today} %% Change "\today" by another date manually
%% -----------------------------
%% -----------------------------

%% %%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\setlength{\droptitle}{-5em}
%% %%%%%%%%%%%%%%%%%%%%%%%%%
\maketitle
\tableofcontents
\pagebreak
% --------------------------
% Start here
% --------------------------

% %%%%%%%%%%%%%%%%%%%
\section{Random Variable}
A random variable is a quantity “X ” resulting from an experiment, by chance,
that can assume different values.\\

A random variable is a variable “X” that
has a single numerical value determined by chance, for each outcome of a
procedure.\\

If a sample space S is discrete, then every R.V.  defined on S is
also discrete, i.e., its range is countable (think of random counts for
examples).
\section{Discrete Random Variable}
A Discrete Random Variable is a variable that can assume only certain clearly
separated values. \\

A Discrete Random Variable has either a finite or countable
number of values, where “countable” refers to the fact that there might be
infinitely many values, but they can be associated with a counting process.
\subsection{Examples of Discrete Random Variables}
\begin{itemize}
    \item The outcome of rolling a single die.
    \item The number of boys in a family with three children.
    \item The number of heads that appear when a coin is flipped nine times.
    \item The sum of the numbers on the dice, when k dice are rolled.
    \item The number of bits received in error when n bits are received.
    \item The number of bits received until the r-th error.
\end{itemize}
\section{Discrete Probability Distributions}
A discrete probability distribution is a listing of
all possible values of a random variable along
with their probabilities. \\
\begin{equation}
    \displaystyle \frac{X}{P(X)} \frac{|x_1|}{|p_1|} \frac{|x_2|}{|p_2|} \frac{|x_2|}{|p_2|}
    \frac{|\dots|}{|\dots|} \frac{|x_k|}{|p_k|}; \  \ \  \sum _{k \geq 1} p_k = 1.
\end{equation}
\begin{enumerate}
    \item The sum of all probabilities must be 1in any probability distribution
    \item All probability values must be in [0,1]
\end{enumerate}
\begin{exmp}
    x $\Rightarrow$ The number of heads appearing when a coin is flipped three times.

\end{exmp}

% Please add the following required packages to your document preamble:
% Note: It may be necessary to compile the document several times to get a multi-page table to line up properly
\begin{longtable}[c]{lllll}
    X    & 0   & 1   & 2   & 3   \\
    \endfirsthead
    %
    \endhead
    %
    P(X) & 1/8 & 3/8 & 3/8 & 1/8
\end{longtable}

\subsection{Bernoulli}
A Bernoulli trail is an experiment with only two outcomes Success and Failure
\begin{enumerate}
    \item $P(S) = p = $ Probability of a success
    \item $P(F) = q = 1 - p = $ Probability of a failure
\end{enumerate}
If a Bernoulli random variable, X, denotes No. of successes, then
\begin{enumerate}
    \item $ X = 1$ if the outcome is success
    \item $ X = 0$ if the outcome is failure
\end{enumerate}
\begin{exmp}
    A bit is transmitted, it is received in error with probability “0.1”.\\
    Assume that 8 bits are transmitted independently.
    \begin{enumerate}
        \item How many bits can be received in error?
            \begin{center}
                $\Perm{8}{2}$
            \end{center}
        \item What is the probability that 2 bits are received in error
            \begin{center}
                $P(X=2) = \Perm{8}{2} \times (0.1)^2 (0.9)^6  $
            \end{center}
    \end{enumerate}
\end{exmp}
\subsection{Binomial Distribution}
In general, let X stand for the number of bits received in error, when $n$ bits are transmitted,
with the probability of a single bit in error being $p$
\begin{equation}
    P (X = k) = \Comb{n}{k} \times p^k \times (1-p)^{n - k}
\end{equation}

\begin{equation}
    \sum_{k = 0}^{n} p_k  = \Comb{n}{k} \times p^k \times (1-p)^{n - k} = [(p)+(1-p)]^n = 1 ^ n = 1
\end{equation}
\begin{exmp}
    A fair coin is tossed 10 times, what is the probability of getting:
    \begin{enumerate}
        \item  Exactly 6 heads.
            \begin{center}
                $ n = 10; \ \ p = 0.5; q = 0.5 $
            \end{center}
            \begin{center}
                $P(X = 6) = \Comb{10}{6} \times (0.5)^6 \times (0.5)^4   $
            \end{center}
        \item  At least 6 heads.
            \begin{center}
                $P(X \geq 6) = P(X = 6) + P(X = 7) + P(X = 8) + P(X = 9) + P(X = 10)  $
            \end{center}
    \end{enumerate}
\end{exmp}

\subsection{Geometric Distribution}

A bit is transmitted, it is received in error with probability “0.1”.

Assume that bits are transmitted independently, until the first bit is received
in error.
\begin{enumerate}

    \item How many bits can be received?
        \begin{center}
            Infinity many many
        \end{center}
    \item What is the probability that the 5-th bit is received in error?
        \begin{center}
            $  P(X = 5) = 0.1 \times 0.9^4 $
        \end{center}
    \item What is the probability that at least 5 (i.e. 5 or more) bits are received until the first error
        \begin{center}
            $  P(X \geq 5) = P(X = 5) + P(X = 6) + \dots $  =
        \end{center}
        \begin{center}
            $  \displaystyle \sum_{k = 5}^{\infty} P(X = K) = \sum_{k = 5}^{\infty}(0.1)(0.9)^{k-1} = 0.656$
        \end{center}

\end{enumerate}
So The General case is
\begin{equation}
    P(X = k) = p \times (1 - p)^{k-1} ; \ \ \ k \geq 1
\end{equation}

\begin{equation}
    \displaystyle \sum_{k = 1}^{\infty}   P(X = k) = \sum_{k = 1}^{\infty}p \times (1 - p)^{k-1}
    = p\sum_{k = 1}^{\infty}(1 - p)^{k-1} = p \times \frac{1}{p} = 1
\end{equation}
\begin{exmp}
    Given that the first $k$ trials were Failures. Find the probability that $(k+1)$-th trial will be a Success. \\

    we need to find that $P(X = k + 1 | X > k)$
    \begin{center}
        $ \displaystyle  P(X = k + 1 | X > k) = \frac{P(X=k+1 \cap X>k)}{P(X>k)}
        = \frac{p \times (1-p)^k}{(1-p)^k} = p = p(X = 1)$  this called lack of memory where the probability
        of k + 1 is the same as the first
    \end{center}
\end{exmp}
\subsection{Negative Binomial}
When a bit is transmitted, it is received in error with probability “0.1”.
Assume that bits are transmitted independently, until FOUR bits are received in error.
\begin{enumerate}
    \item At least, how many bits can be received ?
        \begin{center}
            at least 4 bits.
        \end{center}
    \item What is the probability that exactly 10 bits will be received
        \begin{center}
            $  P(X= 10) = (0.1)^9 \times \Comb{9}{3} \times (0.9)^6 \times (0.1)^3 $
        \end{center}
        In General, $P(X = k) = \Comb{k-1}{3} \times (1-p)^{k-4} \times p^4$

\end{enumerate}
\begin{slo}
    In general, let X stand for the number of bits
    received until r bits are received in error, with
    the probability of a single bit in error being p.
\end{slo}
\begin{equation}
    P(X = k) =  \underbrace{p^r}_{\text{last trial}}
    \ \ \underbrace{\Comb{k-1}{r-1} (1-p)^{k-r}}_{\text{$(r - 1)$success in$(k - 1)$trial}}; \ \ k \geq r.
\end{equation}
\begin{equation}
    \displaystyle \sum_{k=r}^{\infty} \Comb{k-1}{r-1} p^r(1-p)^{k-r} =
    \sum_{k' = 0}^{\infty} \Comb{k' + r -1}{k'} \ p^r (1-p)^{k'}
\end{equation}
given that $ \displaystyle \Comb{n}{k} = \Comb{n}{n- k}$

\begin{equation}
    \displaystyle \sum_{k' = 0}^{\infty} \Comb{k' + r -1}{k'} \ p^r (1-p)^{k'} =
    p^r \sum_{k'= 0}^{\infty}  \Comb{k' + r-1}{r-1}(1-p)^{k'}
\end{equation}
given that $ \displaystyle \frac{1}{(1-x)^r} = \sum_{k=0}^{\infty} \Comb{k+r-1}{r-1}x^r $
\begin{equation}
    \displaystyle p^r \sum_{k'= 0}^{\infty}  \Comb{k' + r-1}{r-1}(1-p)^{k'} = p^r * \frac{1}{(1-(1-p))^r} = 1
\end{equation}
\section{Notes}
\begin{enumerate}
    \item Recall that a binomial random variable is a count of the number
        of successes in n Bernoulli trials. That is, the number of trials n is
        predetermined, and the number of successes represents the random variable X.
    \item  A negative binomial random variable is a count of the number of
        trials required to obtain r successes. That is, the number of
        successes r is predetermined, and the number of trials (n or k)
        represents the random variable X.
\end{enumerate}

\section{Probability Mass Function}
Using the probability distribution “PD” of a discrete RV,
X, with possible values $x_1, x_2\dots, x_n.$ A probability mass function is a function defined such that:
\begin{enumerate}
    \item \[    f(X_k) = P(X = x_k) = p_k,  \]
    \item \[          f(x_k) \geq 0,  \]
    \item \[  \displaystyle \sum_{k=1}^{n} f(x_k) = 1.       \]
\end{enumerate}
Example :\[ f(x)= \begin{cases}
    p_k, & x=x_k \\
    0, & otherwise
\end{cases}
\] $f$ is a function that is non-trivial only on the range of $X$.

\begin{center}
    \includegraphics[width=201px]{2.png}
\end{center}
\section{Cumulative Probability Function}
The following function provides an alternative definition for X.
The cumulative probability function of a DRV X is defined by:
\[
    \displaystyle F(x) = P(X \leq x) = \sum_{x_k \leq x}f(x_k)
\]
\[ F(x)= \begin{cases}
    0, & x < x_1 \\
    p_1, & x_1 \leq x < x_2 \\
    p_1 + p_2, & x_2 \leq x < x_3 \\
    \dots
\end{cases}
\]
\begin{center}
    \includegraphics[width=200px]{3.png}
\end{center}
\subsection{Notes}
\begin{enumerate}
    \item $F$ is a step function
    \item $  0 \leq F(X) \leq 1   $
    \item if $x \leq y \Rightarrow F(x) \leq F(y)$
\end{enumerate}
\section{Average of a Discrete Random Variable}
\begin{itemize}
    \item Each time a random experiment is carried out, an associated RV takes on a
        value. Is it possible find an “average” value of the RV ?
    \item For instance, on average, how many bits are received in error
        when 8 bits are transmitted?
    \item Or, on average, how many bits must be received until the first (the
        r-th) error occurs?
        \begin{itemize}
            \item Note that in both cases, you can take a sample, i.e. repeat the
                respective experiment several times and take the value of the
                RV each time.
            \item An average computed this way is, however, dependent on the samle!
            \item Is it possible to get a sample independent value?
        \end{itemize}
\end{itemize}
\section{Expectation}
The expected value, or expectation, of a discrete random variable $X$ is given by
\[
    \displaystyle \mu = E(X) = \sum X *P(X) = \sum_k x_k p_k
\]
The formula gives a weighted average of values
of a random variable, with the probabilities being
the weights of the respective values.
(One can see that the expected value as a weighted probability).
\section{Variance and Standard Deviation}
The average spread of a DRV around its expectation is called the standard
deviation, and is computed via the so-called \textsc{variance}

\[
    \displaystyle \sigma^2 = V(X) = \sum_k  [(x_k -\mu)^2.p_k]\ \ \  \text{or} =
\]
\[
    \displaystyle [\sum_k x^2_k.p_k] - \mu^2 = E(X^2) - [E(X)]^2
\]
Standard Deviation is $\sigma = \sqrt{\sigma^2}$
\begin{exmp}
    Compute the mean and the standard deviation of the number of girls in a family with 3 children.\\

    \ \ \ \   \includegraphics[width=400px]{4.png}
    \[
        \sigma = \sqrt{\sigma^2} = \sqrt{[\sum X^2.P(X)]- \mu^2} = \sqrt{3-(1.5)^2}
    \]
\end{exmp}
\subsection{Expectation and Variance of Bernoulli Random Variable}
\begin{longtable}[c]{l|l|l}
    X    &  1   & 0   \\ \hline
    \endfirsthead
    %
    \endhead
    %
    P(X) & p&  q
\end{longtable}
\[
    \mu = 1.p + 0(1-p) = p
\]
\[
    \sigma^2 = [1^2.p + 0^2(1-p)] - p^2 = p(1-p) = pq.
\]
\subsection{Expectation and Variance of the Binomial RV}
For a binomial RV with parameters n and p we have:
\[
    \mu = np,
\]
\[
    \sigma^2 = np(1-p) = npq.
\]
\begin{exmp}
    The Statistical Bulletin published by Metropolitan Life Insurance Co.
    reported that 2\% of all American births result in twins. If a random
    sample of 8000 births is taken, find the mean, variance, and standard
    deviation of the number of births that would result in twins.
    \[
        X \Rightarrow \text{The number of births that would result in twins}
    \]
    \[
        S \Rightarrow \text{twins}; \ \ F \Rightarrow \text{not twins}
    \]
    \[
        n = 8000; p = 0.2; q = 0.98
    \]
    \[
        \mu = n.p = 8000 \times 0.02
    \]
    \[
        \sigma^2 = n.p.q = (8000) \times (0.02) \times (0.98) = 156.8 \Rightarrow \sigma = 12.5
    \]
\end{exmp}
\subsection{Expectation and Variance of the Geometric r.v}
For a geometric RV with parameter p we have:
\[
    \mu = \frac{1}{p}.
\]
\[
    \sigma^2 = \frac{1-p}{p^2} = \frac{q}{p^2}.
\]
\end{document}


\chapter{Convex sets}

\clearpage
\section{Affine and convex sets}

\clearpage
\section{Some important examples}

\clearpage
\section{Operations that preserve convexity}

\clearpage
\section{Generalized inequalities}

\clearpage
\section{Separating and supporting hyperplanes}

\clearpage
\section{Dual cones and generalized inequalities}

\clearpage
\hfil\includegraphics[width=.5\textwidth]{../Graphics/022.png}\hfil

\clearpage
\hfil\includegraphics[width=.5\textwidth]{../Graphics/024a.png}\hfil

\clearpage
\hfil\includegraphics[width=.5\textwidth]{../Graphics/024b.png}\hfil

\clearpage
\hfil\includegraphics[width=.5\textwidth]{../Graphics/026a.png}\hfil

\clearpage
\hfil\includegraphics[width=.5\textwidth]{../Graphics/026b.png}\hfil

\clearpage
\hfil\includegraphics[width=.5\textwidth]{../Graphics/028a.png}\hfil

\clearpage
\hfil\includegraphics[width=.5\textwidth]{../Graphics/028b.png}\hfil

\clearpage
\hfil\includegraphics[width=.5\textwidth]{../Graphics/029.png}\hfil

\clearpage
\hfil\includegraphics[width=.5\textwidth]{../Graphics/030.png}\hfil

\clearpage
\hfil\includegraphics[width=.5\textwidth]{../Graphics/031.png}\hfil

\clearpage
\hfil\includegraphics[width=.5\textwidth]{../Graphics/032.png}\hfil

\clearpage
\hfil\includegraphics[width=.5\textwidth]{../Graphics/035.png}\hfil

\clearpage
\hfil\includegraphics[width=.5\textwidth]{../Graphics/037a.png}\hfil

\clearpage
\hfil\includegraphics[width=.5\textwidth]{../Graphics/037b.png}\hfil

\clearpage
\hfil\includegraphics[width=.5\textwidth]{../Graphics/040.png}\hfil

\clearpage
\hfil\includegraphics[width=.5\textwidth]{../Graphics/042.png}\hfil

\clearpage
\hfil\includegraphics[width=.5\textwidth]{../Graphics/046.png}\hfil

\clearpage
\hfil\includegraphics[width=.5\textwidth]{../Graphics/047a.png}\hfil

\clearpage
\hfil\includegraphics[width=.5\textwidth]{../Graphics/047b.png}\hfil

\clearpage
\hfil\includegraphics[width=.5\textwidth]{../Graphics/048.png}\hfil

\clearpage
\hfil\includegraphics[width=.5\textwidth]{../Graphics/051.png}\hfil

\clearpage
\hfil\includegraphics[width=.5\textwidth]{../Graphics/052.png}\hfil

\clearpage
\hfil\includegraphics[width=.5\textwidth]{../Graphics/055.png}\hfil

\clearpage
\hfil\includegraphics[width=.5\textwidth]{../Graphics/056a.png}\hfil

\clearpage
\hfil\includegraphics[width=.5\textwidth]{../Graphics/056b.png}\hfil

\clearpage
\hfil\includegraphics[width=.5\textwidth]{../Graphics/057.png}\hfil

\clearpage
\hfil\includegraphics[width=.5\textwidth]{../Graphics/058.png}\hfil

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "../LectureNotes-Optimization"
%%% End:


% Created 2018-11-29 jue 11:55
\documentclass[a4paper]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fixltx2e}
\usepackage{graphicx}
\usepackage{longtable}
\usepackage{float}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{textcomp}
\usepackage{marvosym}
\usepackage{wasysym}
\usepackage{amssymb}
\usepackage{hyperref}
\tolerance=1000
\usepackage{khpreamble}
\newcommand{\tustin}{\frac{2}{h}\frac{z-1}{z+1}}
\author{Kjartan Halvorsen}
\date{2018-11-29}
\title{Computerized control - Final Exam (28\%)}
\hypersetup{
  pdfkeywords={},
  pdfsubject={},
  pdfcreator={Emacs 24.5.1 (Org mode 8.2.10)}}
\begin{document}

\maketitle
The dynamic model of a ship with input $u$ being the rudder angle and the output $y$ being the heading (see figure \ref{fig:tanker}) can be described as a continuous-time second order system with a pole in the origin
\[ G(s) = \frac{K}{s(s + a)}. \]
For fully loaded, large tankers this dynamics is often unstable, meaning that $a<0$ \footnote{Fossen, Thor I. Handbook of marine craft hydrodynamics and motion control. John Wiley \& Sons, 2011.} .
\begin{figure}[h]
\begin{center}
\includegraphics[]{tanker}
\caption{Heading of a ship controlled by rudder input.}
\label{fig:tanker}
\end{center}
\end{figure}

Consider for this exam the normalized continuous-time model of the tanker
\[ G(s) = \frac{1}{s(s - 1)}. \]
with the discrete-time model obtained by zero-order hold
\begin{equation*}
 H(z) = \frac{(-1+\mexp{h} -h)z + 1 - (1-h)\mexp{h}}{(z-1)(z-\mexp{h})}.
\end{equation*}
Specifically, use sampling time $h=0.2$, which gives the (approximate) model
\begin{equation}
 H(z) = \frac{0.02z + 0.02}{(z-1)(z-1.2)} = \frac{0.02z + 0.02}{z^2-2.2z+1.2}.
\label{eq:model}
\end{equation}

\textbf{All answers should be well motivated!}

\section*{Problem 1}
\label{sec-1}
\begin{enumerate}
\item In figure \ref{fig:complex-plane} draw the poles (crosses) and zero (circle) for the  discrete-time pulse-transfer function in \eqref{eq:model}.
\begin{figure}[h]
\begin{center}
\includegraphics[]{complex-plane}
\caption{Problem 1: Plot the poles and zeros of the discrete-time system.}
\label{fig:complex-plane}
\end{center}
\end{figure}
\item Assume that the tanker with model \eqref{eq:model} is stabilized using error-feedback and a PD-controller. The  Bode-diagram of the resulting \textbf{closed-loop} system is  given in figure \ref{fig:bode}. What is the bandwidth of the closed-loop system? At what frequency is the resonance peak?
\begin{figure}[h]
\begin{center}
\includegraphics[]{bode-closed}
\caption{Problem 1: Bode diagram of closed-loop system with PD-control}
\label{fig:bode}
\end{center}
\end{figure}
\end{enumerate}

\section*{Problem 2}
\label{sec-2}
Figure \ref{fig:rst} shows a system controlled with an RST controller. Note that the system includes an anti-aliasing filter modelled as a pure time-delay of two sampling periods. What is the closed-loop pulse-transfer function from the disturbance $d$ to the output $y$? You do not need to multiply the polynomials. It is sufficient to state your answer in terms of $A(z)$, $B(z)$, $R(z)$, $S(z)$ and $z^2$.
\begin{figure}[h]
\begin{center}
\includegraphics[]{rst-anti-aliasing}
\caption{Problem 2: Two-degree-of-freedom controller with anti-aliasing filter.}
\label{fig:rst}
\end{center}
\end{figure}

\section*{Problem 3}
\label{sec-3}
When designing an RST-controller for the system in Problem 2, $R(z)$ and $S(z)$ are determined from a diophantine equation, based on the required placement of the closed-loop poles. Assume the following desired closed-loop denominator:
\begin{equation}
A_{cl} = \underbrace{(z-p_1)(z-p_2)z^2}_{A_c}\underbrace{(z-p_3)^3}_{A_o}
\end{equation}
\begin{enumerate}
\item Write the diophantine equation in terms of $A_c(z)$, $A_o(z)$, $A(z)$, $B(z)$, $R(z)$, $S(z)$ and $z^2$.
\item Let the controller polynomials $R(z)$ and $S(z)$ have the same order. Determine this order, so that all the controller parameters can be determined from the diophantine equation. Note that you only need to determine the \textbf{order} of the controller. You do not need to write the equation for the controller parameters.
\end{enumerate}

\section*{Problem 4}
\label{sec-4}
The controllable canonical state-space representation of \eqref{eq:model} is given by
\begin{equation}
\begin{split}
x(k+1) &= \bbm 2.2 & -1.2\\1 & 0\ebm \x(k) + \bbm 1\\0\ebm u(k)\\
y(k) &= \bbm 0.02 & 0.02 \ebm x(k),
\end{split}
\end{equation}
with
\[ x(k) = \bbm x_1(k)\\x_2(k)\ebm. \]
Introduce the state-feedback law $u(k) = -l_1x_1(k) -l_2x_2(k)$ and determine $l_1$ and $l_2$ so that the closed-loop system has the characteristic polynomial
\[ (z-0.9+0.1i)(z-0.9-0.1i) = z^2 -1.8z + 0.82. \]
% Emacs 24.5.1 (Org mode 8.2.10)
\end{document}


%-----------------------------------------------------------------------------%
%                                                                             %
%    K A P I T E L   4                                                        %
%                                                                             %
%-----------------------------------------------------------------------------%

\chapter{Bipedal Walking Variants}\label{c4}
This chapter studies the proposed motion planning approach for bipedal walking gaits of the full-size humanoid RH5. It starts by describing the individual building blocks of the optimization problem, then discusses the simulation results obtained for increasing gait dynamics and finally provides an evaluation of the contact stability of the dynamic motions.

\section{Formulation of the Optimization Problem}\label{sec:BipedFormulation}
This section gives information about the adopted contact and impact modeling techniques and introduces the constraints used for generating physically consistent walking trajectories. The core formulation is based on the legged gaits described in \cite{mastalli20crocoddyl} but contains various improvements necessary for application on real robots. All motions presented in this chapter are solved for a predefined sequence of contacts and step timings.
Recapitulating \cref{sec:TheoryDDP}, we formulate the optimization problem as
\begin{equation}\label{eqn:optimizationProblem}
\myM{X}^*,\myM{U}^*=
\arg\min_{\mathbf{X},\mathbf{U}} l_N(x_N)+\sum_{k=0}^{N-1} \int_{t_k}^{t_k+\Delta t} l(\mathbf{x},\mathbf{u})dt.
\end{equation}

\subsection{Contact and Impact Modeling}
During a walking motion, the body is always in contact with the ground either in single support, or in double support. In \cref{sec:TheoryConstrainedDDP} we have discovered, how rigid contacts can be expressed as a kinematic constraint on the \gls{EoM} (see \cref{eqn:unconstrainedDynamics}).
Analogously, one can describe the impulse dynamics
\footnote{Impulse dynamics account for the physical effects that occur at a switch from non-contact to contact condition \cite{featherstone2014rigid}.}
of a multibody system as
\begin{equation}\label{eqn:ImpulseDynamics}
\left[\begin{matrix}\myM{M} & \myM{J}^{\top}_c \\{\myM{J}_{c}} & \myM{0}\end{matrix}\right] \left[\begin{matrix} \myM{v}^+ \\ -\boldsymbol{\Lambda} \end{matrix}\right] = \left[\begin{matrix} \myM{M}\myM{v}^- \\ -e\myM{J}_c \myM{v}^-\end{matrix}\right],
\end{equation}
where $\boldsymbol{\Lambda}$ is the contact impulse, $\myM{v}^-$ and $\myM{v}^+$ are the generalized velocities before and after the impact and $e\in [0,1]$ is the restitution coefficient that accounts for the elasticity of the collision. For all motions, we use this impulse model to account for the infinitesimal short change in the contact situation. To improve the numerical integration stability, terms defined by Baumgarte Stabilization \cite{baumgarte1972stabilization} are used along with the rigid contact constraint described with \cref{eqn:gaussMinimization}. This numerical stabilization on the constraints can be expressed as:
\begin{equation}\label{eqn:BaumgarteStabilization}
a_0=a_{\lambda(c)}-\alpha M_{\lambda(c)}^{\text{ref}}\ominus M_{\lambda(c)}-\beta v_{\lambda(c)},
\end{equation}
where $a_0$ is the desired acceleration in the constrained space, $v_{\lambda(c)}, a_{\lambda(c)}$ are the spatial velocity and acceleration of the contact $\lambda(c)$, respectively, $M_{\lambda(c)}^{\text{ref}}\ominus M_{\lambda(c)}$ is the inverse composition between the reference contact placement and the current one \cite{mastalli20crocoddyl}. The PD gains $\alpha$ and $\beta$ enable an adequate numerical stabilization of the constraints depending on the dynamics of the movement.

\subsection{Robot Tasks}
Robot tasks, such as grasping and object or performing a step, are an essential goal of motion planning. As outlined in \cref{sec:TheoryConstrainedDDP}, these task-related constraints are considered in the optimization process as regulator functions. In the context of this thesis two tasks are of specific interest, namely the foot tracking and \gls{CoM} tracking.

\paragraph{Foot Tracking Cost}
In order to perform a symmetric gait (see \cref{sec:TheoryBiped}), the design of dedicated foot trajectories is crucial. We use piecewise-linear functions to describe the swing foot reference trajectory. Deviation from this time-depended reference foot trajectory is highly penalized. The foot tracking cost can be formulated via the squared Euclidean norm (L2 norm) as
\begin{equation*}
\Phi_{\text{foot}}=\mid\mid \myM{f}(t)-\myM{f}^{\text{ref}}(t)\mid\mid^2_2,
\end{equation*}
where $\myM{f}(t)$ is the actual \gls{CoM} position at time-step $t$ and $\myM{f}^\text{ref}(t)$ is the according reference. Start and end position as well as timings are predefined and combined with a desired step height to shape the desired foot trajectory.

\paragraph{\Gls{CoM} Tracking Cost}
In bipedal locomotion, the three-dimensional position of the \gls{CoM} of the whole-body turned out to be crucial \cite{carpentier2017centre}. This is especially true for quasi-static motions, where the \gls{FCoM} is used as static stability margin as explained in \cref{sec:TheoryBiped}. Analogously to the foot cost, the \gls{CoM} tracking cost is formulated as
\begin{equation*}
\Phi_{\text{CoM}}=\mid\mid \myM{c}(t)-\myM{c}^\text{ref}(t)\mid\mid^2_2.
\end{equation*}
In order to account for the static stability in these motions, we perform a dedicated shifting of the \gls{FCoM} to the foot center, before performing the swing-foot task.
%Additionally, the \gls{CoM} is kept at a constant height to prevent unnecessary forces acting on the base of the body.

\subsection{Inequality Constraints for Physical Consistency}
Essential demands on physically consistent motion planning are that (i) the robot limits (torque, joints) are considered and (ii) the generated trajectories are inherently balanced. To this end, we consider joint limits, friction cone and the novel \gls{CoP} bound as inequality constraints in our formulation, while torque constraints are covered in the algorithm itself.

\paragraph{Contact Stability Constraints}
As detailed in \cref{c3} with the concept of contact stability constrained \gls{DDP}, we constrain unilaterality, friction and \gls{CoP} for each foot in contact. For the sake of clarity, \cref{eqn:CoPCostComputation} is again recalled as
\begin{equation*}
\Phi_{\text{CoP}}=
\begin{cases}
\quad\dfrac{1}{2}\myM{r}^T\myM{r} &\mid \text{lb} > \myM{r} > \text{ub} \\[10pt]
\quad 0 &\mid \text{lb} \leq \myM{r} \leq \text{ub},
\end{cases}
\end{equation*}
where the \gls{CoP} position is bound to lie inside the foot contact area by the lower and upper bounds $\text{lb}$ and $\text{ub}$, respectively.
In the same manner friction cone constraints along with the unilaterality are considered as
\begin{equation*}
\Phi_{\text{friction}}=
\begin{cases}
\quad\dfrac{1}{2}\myM{r}^T\myM{r} &\mid \text{lb} > \myM{r} > \text{ub} \\[10pt]
\quad 0 &\mid \text{lb} \leq \myM{r} \leq \text{ub},
\end{cases}
\end{equation*}
where $\text{lb}$ and $\text{ub}$ bound the resulting contact force to lie inside a four-sided polygonal approximation of the spatial friction cone \cite{kao2016contact}.

\paragraph{Joint Limits}
Physical boundaries of the joints must not be exceeded to avoid damage to the system. They are covered via a bounded quadratic activation as
\begin{equation*}
\Phi_{\text{joints}}=
\begin{cases}
\quad\dfrac{1}{2}\myM{r}^T\myM{r} &\mid \text{lb} > \myM{r} > \text{ub} \\[10pt]
\quad 0 &\mid \text{lb} \leq \myM{r} \leq \text{ub},
\end{cases}
\end{equation*}
where $\text{lb}$ and $\text{ub}$ correspond to the lower and upper bounds for joint position and velocities, respectively.

\subsection{Further Regularization Terms}
Additional to the described constraints for tasks and physical consistency, we optimize for minimization of the torques and regularize the robot posture.
\paragraph{Torque Minimization}
In order to improve the energy efficiency of the motions and maintain a human-like torque at the joints \cite{kim1994modeling}, we minimize the joint torques for realistic dynamic movements via
\begin{equation*}
\Phi_{\text{torque}}=\mid\mid \btau(t)\mid\mid^2_2.
\end{equation*}
\paragraph{Posture Regularization}
Finally, we deal with the redundancy of multi-body dynamics by applying a weighted least-squares cost function to regularize the state with respect to the nominal robot posture:
\begin{equation*}
\Phi_{\text{posture}}=\mid\mid \myM{q}(t)-\myM{q}^\text{ref}(t)\mid\mid^2_2.
\end{equation*}

\section{Simulation Results for Increasing Gait Dynamics}\label{sec:BipedSimulation}
This section presents the simulation results for bipedal walking motions obtained by solving an optimization problem based on the described building blocks from the previous section. It studies both static and dynamic walking gaits, respectively.

\subsection{Static Walking}\label{subsec:StaticWalking}
The analysis of static walking gaits provides detailed insights on the optimization structure and allows a thorough experimental validation (see \cref{c7}).

Focus of investigation is a slow two step walking motion. We assume a quasi-static motion and hence deploy a static stability criterion as introduced in \cref{sec:TheoryStability} by following a dedicated \gls{FCoM} trajectory. To this end, the optimization problem is composed of a total of five locomotion phases visualized in \cref{fig:walkStatic_Snaps}. From (a) an initial pose, (b) shift the \gls{FCoM} above the \gls{LF}, (c) perform a right step, (d) shift the \gls{FCoM} above the \gls{RF}, (e) perform a left step and (f) shift the \gls{FCoM} to the half length of the line intersecting both feet center while returning to the initial pose.

Additionally, the \gls{CoM} is optimized for a constant height over the whole gait. \cref{tab:walkStatic} gives a compact overview of the desired gait characteristics and the applied constraints of the optimization. In order to comply with the quasi-static assumption, the motion is performed about a time horizon of 15 s with a total stride length of 20 cm a robot model with fixed arms.
\begin{table}[t]
\centering
\caption[Static walking gait characteristics and optimization constraints]{Static walking gait characteristics and applied optimization constraints.}
\begin{tabular}{|ll|ll|}
\hline
\multicolumn{2}{|l|}{\textbf{Gait Characteristics}} & \multicolumn{2}{l|}{\textbf{Optimization Constraints}} \\ \hline
Step length:& 10 cm 	& Tasks: 			& $\Phi_{\text{foot}}$, $\Phi_{\text{CoM}}$\\ \hline
Step height:& 5 cm 	& Stability: 		& $\Phi_{\text{friction}}$\\ \hline
Time:& 3 s/phase 	& Limits: 			& $\Phi_{\text{joint}}$, torques \\ \hline
Step size:& 0.03 s	& Regularization: 	& $\Phi_{\text{posture}}$, $\Phi_{\text{torque}}$\\ \hline
\end{tabular}
\label{tab:walkStatic}
\end{table}

\begin{figure}
\begin{subfigure}{.16\textwidth}
	\includegraphics[width=.95\linewidth]{fig/walkStatic/snaps/1}
	\caption{}
\end{subfigure}%
\begin{subfigure}{.16\textwidth}
	\includegraphics[width=.95\linewidth]{fig/walkStatic/snaps/2}
	\caption{}
\end{subfigure}%
\begin{subfigure}{.16\textwidth}
	\includegraphics[width=.95\linewidth]{fig/walkStatic/snaps/4}
	\caption{}
\end{subfigure}%
\begin{subfigure}{.16\textwidth}
	\includegraphics[width=.95\linewidth]{fig/walkStatic/snaps/6}
	\caption{}
\end{subfigure}%
\begin{subfigure}{.16\textwidth}
	\includegraphics[width=.95\linewidth]{fig/walkStatic/snaps/8}
	\caption{}
\end{subfigure}%
\begin{subfigure}{.16\textwidth}
	\includegraphics[width=.95\linewidth]{fig/walkStatic/snaps/9}
	\caption{}
\end{subfigure}
\caption[Static walking based on dedicated \gls{FCoM} shifting]{Static walking gait based on dedicated \gls{FCoM} motion, consisting of the locomotion phases (a) initial pose, (b) \gls{FCoM} shift above \gls{LF}, (c) right step, (d) \gls{FCoM} shift above \gls{RF}, (e) left step and (f) pose recovery. Significant lateral shifts of the \gls{FCoM} are required to establish the static stability. \href{https://github.com/julesser/ma-thesis-simulation-results/blob/master/HumanoidFixedArms/StaticWalking_NoCoPCost_ComHeightConstant/crocoddyl.mp4}{[Video]}}
\label{fig:walkStatic_Snaps}
\end{figure}

The results of the optimization problem \cref{eqn:optimizationProblem} are shown in \cref{fig:walkStatic_TaskSpace} and \cref{fig:walkStatic_JointState}.
\cref{fig:walkStatic_TaskSpace} presents the resulting base and end-effector trajectories. As becomes clear, the \gls{FCoM} tracking is sufficiently good and the \gls{CoM} height remains in a reasonable range of $\pm$ 1 cm. Equally, the desired foot trajectory is tracked with adequate accuracy. Also, the foot velocities are reasonable with a maximum velocity of 0.1 m/s at the impact. The pursued step height is not reached exactly but is about one centimeter less. This effect can be explained by the immediately reverse direction at the vertex of the piecewise-linear trajectory, which is smoothened by the solver.

\cref{fig:walkStatic_JointState} shows the resulting joint trajectories for the torso, \gls{LF} and \gls{RF}. Both the joint position limits and the maximum permissible joint speeds remain far below the limits due to the slow nature of the motion. Interestingly, the pursued \gls{FCoM} shifting turns out to be realized mostly based on a shift in the body roll activation rather than on a shift in the hips. Furthermore, it becomes evident that the posture regularization is effective since all joints end closely to the initial position.

\begin{figure}[h!]
\centering
\includegraphics[width=1\textwidth]{fig/walkStatic/TaskSpace}
\caption[Static walking gait solution in task space]{Static walking gait solution in task space. Both the \gls{FCoM} task and the swing foot task are satisfied with acceptable accuracy.}
\label{fig:walkStatic_TaskSpace}
\end{figure}

\begin{figure}[h!]
\centering
\includegraphics[width=1\textwidth]{fig/walkStatic/JointState}
\caption[Static walking gait solution of the joint states]{Static walking gait solution of the joint states. Due to the slow nature of the motion, joint limits are far from being reached and velocity discontinuities remain within reasonable ranges.}
\label{fig:walkStatic_JointState}
\end{figure}

\subsection{Dynamic Walking}
The analysis of a dynamic walking gait is concerned about generating efficient motions with higher velocities. This part forms the basis of the stability evaluation in the next section.

Focus of investigation is a dynamic walking motion. Compared to the previously studied static walking gait these motions are characterized by higher velocities and dynamic forces exceeding the static ones. These characteristics imply that dynamic stability criteria become necessary. To this end, we apply the proposed approach of contact stability constrained DDP described in \cref{c3}. Consequently, the \gls{CoP} of each foot is constrained instead of following a reference \gls{CoM} trajectory. By this, the solver is enabled to find an optimal, dynamic \gls{CoM} shifting along with the requested contact stability constraints.

\cref{tab:walkDynamic} compactly summarizes the gait characteristics and applied optimization constraints. The optimization problem is composed of a total of five locomotion phases visualized in \cref{fig:walkDynamic_Snaps}. From (a) an initial pose in \gls{DS}, (b,c) perform a right step, (d,e) perform a left step and (f) recover to the initial pose. In accordance with biomechanical findings \cite{kuo2001simple}, we choose a desired step length of 40 \nolinebreak cm with 1.5 s per step and deliberately define a stance to swing ratio of one to three.

\begin{table}[t]
\centering
\caption[Dynamic walking gait characteristics and optimization constraints]{Dynamic walking gait characteristics and optimization constraints for a stance to swing ratio of one to three.}
\begin{tabular}{|ll|ll|}
\hline
\multicolumn{2}{|l|}{\textbf{Gait Characteristics}} & \multicolumn{2}{l|}{\textbf{Optimization Constraints}} \\ \hline
Step length:& 40 cm 	& Tasks: 			& $\Phi_{\text{foot}}$\\ \hline
Step height:& 5 cm 	& Stability: 		&$\Phi_{\text{CoP}}$, $\Phi_{\text{friction}}$ \\ \hline
Time:& 1.5 s/step	& Limits: 			& $\Phi_{\text{joint}}$, torques \\ \hline
Step size:& 0.03 s	& Regularization: 	& $\Phi_{\text{posture}}$, $\Phi_{\text{torque}}$\\ \hline
\end{tabular}
\label{tab:walkDynamic}
\end{table}

\begin{figure}
\begin{subfigure}{.16\textwidth}
	\includegraphics[width=.95\linewidth]{fig/walkDynamic/snaps/1}
	\caption{}
\end{subfigure}%
\begin{subfigure}{.16\textwidth}
	\includegraphics[width=.95\linewidth]{fig/walkDynamic/snaps/3}
	\caption{}
\end{subfigure}%
\begin{subfigure}{.16\textwidth}
	\includegraphics[width=.95\linewidth]{fig/walkDynamic/snaps/4}
	\caption{}
\end{subfigure}%
\begin{subfigure}{.16\textwidth}
	\includegraphics[width=.95\linewidth]{fig/walkDynamic/snaps/6}
	\caption{}
\end{subfigure}%
\begin{subfigure}{.16\textwidth}
	\includegraphics[width=.95\linewidth]{fig/walkDynamic/snaps/7}
	\caption{}
\end{subfigure}%
\begin{subfigure}{.16\textwidth}
	\includegraphics[width=.95\linewidth]{fig/walkDynamic/snaps/8}
	\caption{}
\end{subfigure}
\caption[Dynamic walking based on the contact stability constrained \gls{DDP}]{Dynamic walking gait based on the contact stability constrained \gls{DDP} approach, consisting of the locomotion phases (a) initial pose, (b,c) right step, (d,e) left step and (f) pose recovery. A natural \gls{CoM} shifting emerges resulting from the inequality constraints for the \gls{CoP} of each foot. \href{https://github.com/julesser/ma-thesis-simulation-results/blob/master/DynamicWalking_LargeSteps_CoP100_ArmsFreed/crocoddyl.mp4}{[Video]}}
\label{fig:walkDynamic_Snaps}
\end{figure}

As becomes clear from \cref{fig:walkDynamic_TaskSpace}, a natural \gls{CoM} shifting to the sides emerges resulting from the inequality constraints for the \gls{CoP}, which is about half of the amount as for the static walking case. Although the \gls{CoM} height is not explicitly constrained, it stays in a reasonable range of about $+$3 cm, which may be caused from the final posture regularization. The end-effector velocities in z-direction are about twice as high as for the case of static walking, which is explained by the higher walking speed.
\cref{fig:walkDynamic_JointState} shows the resulting joint states for the dynamic walking gait. The velocity and acceleration contain higher peaks and the joint deflections are stronger, which is reasonable due to the higher walking speed.

\begin{figure}[h!]
\centering
\includegraphics[width=.9\textwidth]{fig/walkDynamic/TaskSpace}
\caption[Dynamic walking gait solution in task space]{Dynamic walking gait solution in task space. It becomes evident that a natural \gls{CoM} shifting in y-direction emerges resulting from the inequality constraints for the \gls{CoP} of each foot.}
\label{fig:walkDynamic_TaskSpace}
\end{figure}

\begin{figure}[h!]
\centering
\includegraphics[width=.9\textwidth]{fig/walkDynamic/JointState}
\caption[Dynamic walking gait solution of the joint states.]{Dynamic walking gait solution of the joint states. Both joint velocities and acceleration show higher peaks along with higher joint deflections compared to the static walking, which is reasonable due to the higher walking speed.}
\label{fig:walkDynamic_JointState}
\end{figure}

%\subsection{Dynamic Walking: Long Gait as Sequence of OC Problems}
%\subsection{Dynamic Walking: The Effect of Different Velocities}

\section{Evaluation of Contact Stability}\label{sec:BipedEvaluation}
This section evaluates, based on the presented dynamic walking gait from the previous section, the proposed approach of contact stability constrained DDP (\cref{c3}).

\subsection{Dynamically Balanced Walking Motion}
As detailed in \cref{sec:TheoryStability}, the central characteristic for dynamically balanced motions is that the \gls{CoP} or \gls{ZMP} remains within the \gls{SP}. The generic approach presented in \cref{c3} has been applied to the dynamic walking gait presented in the previous section. It utilizes the \gls{CoP} criterion for each contact surface and hence the motion can be called dynamically balanced only if the \gls{CoP} of each foot in contact stays within the according \gls{SP} along the whole motion.

\cref{fig:walkDynamic_StabilityCoP100} shows the top view of the presented dynamic walking  gait. The rectangles correspond to the true-to-scale dimensions of the robot feet. For clarity, only the first and last \gls{DS} phase are visualized.
The blue curve shows the time course of the resulting \gls{CoM} trajectory, with relevant points in time marked separately. Since the \gls{CoM} trajectory between lift-off and touch-down is largely outside the respective foot area, no static stability can be present, as expected.
The orange and green crosses mark the time-dependent position of the \gls{CoP}s for both feet. It is evident that both \gls{CoP}s remain within the corresponding SP over the entire time course of the movement, which is why the movement can be classified as dynamically balanced.

\subsection{Different Levels of CoP Restriction}
Although the dynamic walking motion is inherently balanced, it becomes clear from \cref{fig:walkDynamic_StabilityCoP100} that the \gls{CoP} partly lies \textit{on} or \textit{near} the border of the respective foot area. This effect can be attributed to the formulation of the \gls{CoP} cost function (\cref{eqn:CoPCostComputation}), which is defined to be zero whenever the \gls{CoP} lies within the given foot area and a quadratic penalization prevents the \gls{CoP} to leave the \gls{SP}.
Theoretically, this formulation is sufficient to generate balanced motions.
In practice however, it might be convenient for real-world experiments to consider a dedicated safety factor so that the \gls{CoP}s maintain a certain distance from the edge. With the presented \gls{CoP} cost function, this objective can be easily achieved by reducing the desired foot geometry.
\cref{fig:walkDynamic_StabilityCoP50} shows the dynamic walking gait, where the \gls{CoP} inequality constraints are active for a \gls{SP} reduced to 50 percent of the original foot geometry. It becomes evident that also these more conservative contact stability constraints can be solved, which might be useful for the purpose of experimental validation.

\begin{figure}[h!]
\centering
\includegraphics[width=1\textwidth]{fig/walkDynamic/StabilityAnalysis_CoP100}
\caption[Stability Analysis of the dynamic walking gait]{Stability Analysis of the dynamic walking gait. As both \gls{CoP}s remain within the corresponding SP, the movement can be classified as dynamically balanced. Hence, the functionality of the proposed contact stability constrained \gls{DDP} approach is verified.}
\label{fig:walkDynamic_StabilityCoP100}
\end{figure}

\begin{figure}[h!]
\centering
\includegraphics[width=1\textwidth]{fig/walkDynamic/StabilityAnalysis_CoP50}
\caption[Dynamic walking stability analysis with conservative \gls{CoP} restriction]{Stability analysis of the dynamic walking gait with a \gls{CoP} that is constrained in a \gls{SP} reduced by 50 percent. By this, the stability is more conservative, which may be convenient for real-world experiments}
\label{fig:walkDynamic_StabilityCoP50}
\end{figure}

In this section we have seen that the proposed contact stability constrained DDP produces motions that are dynamically balanced. In \cref{c6} we investigate if the generated motions can be tracked by a simple online stabilizer based on position control in joint space. Beforehand, in \cref{c5}, we explore the effect of the motion planning approach and physical system limits on highly dynamic movements.


%% Author: ph-u
%% Script: autocorrelation.Rnw
%% Desc: pdf report creation for an annual temperature `Rdata` dataset
%% Input: None -- need to "Compile PDF" within RStudio.app
%% Output: pdf report in `Code` subdirectory (and other auto-generated files)
%% Arguments: 0
%% Date: Oct 2019

\documentclass{article}

\usepackage[margin=1in]{geometry}
\usepackage{amsmath,hyperref}

\title{Question: Autocorrelation in weather}
\author{ph-u (CID: 01786076)}
\date{}

\usepackage{Sweave}
\begin{document}
\input{autocorrelation-concordance}

\maketitle
\begin{center}
  Hypothesis: Annual temperature ($^o$C) is influenced by the previous year.
\end{center}
\section{Load \textbf{KeyWestAnnualMeanTemperature.Rdata}}
\begin{Schunk}
\begin{Sinput}
> load("../Data/KeyWestAnnualMeanTemperature.RData");ls()
\end{Sinput}
\begin{Soutput}
[1] "ats"
\end{Soutput}
\end{Schunk}

\section{Examine correlation coefficient of data\label{osp}}
\begin{Schunk}
\begin{Sinput}
> print(b<-unlist(cor(ats,method = "spearman"))[1,2])
\end{Sinput}
\begin{Soutput}
[1] 0.5255559
\end{Soutput}
\end{Schunk}

\section{Plot data\label{plot}}
\includegraphics{autocorrelation-004}
\\
Pairing annual temperature by shifting yearly data by 1\\
\begin{Schunk}
\begin{Sinput}
> ats.0<-data.frame(ats[,2][1:dim(ats)[1]-1],ats[,2][2:dim(ats)[1]])
\end{Sinput}
\end{Schunk}
\clearpage
And plot the paired data\\
\includegraphics{autocorrelation-006}

\section{Sample Spearman correlation 10K times by random shuffles\label{ssp}}
\begin{Schunk}
\begin{Sinput}
> dm<-1e4
> a<-rep(NA,dm)
> i<-1
> for(x in 1:dm){
+
+   ## shuffle data into random pairs
+   ats.0<-sample(ats[,2],dim(ats)[1],replace = F)
+   ats.0<-data.frame(ats.0[1:length(ats.0)-1],ats.0[2:length(ats.0)])
+
+   ## Spearman correlation on newly-shuffled pairs
+   a[i]<-unlist(cor(ats.0,method = "spearman"))[1,2]
+   i<-i+1
+ }
\end{Sinput}
\end{Schunk}
With Spearman correlation coefficient mean (from sampling) calculated as:\\
\begin{Schunk}
\begin{Sinput}
> mean(a)
\end{Sinput}
\begin{Soutput}
[1] -0.009002796
\end{Soutput}
\end{Schunk}
\section{Fraction of sampling $>$ overall coefficient (approx. p.val)\label{pval}}
\includegraphics{autocorrelation-010}
\begin{Schunk}
\begin{Sinput}
> length(a[which(a>b)])/length(a)
\end{Sinput}
\begin{Soutput}
[1] 0
\end{Soutput}
\end{Schunk}
\section{Discussion}
Correlation coefficient from overall (Sec.\ref{osp}) and sampled (Sec.\ref{ssp}) were different with strong statistical significance (Sec.\ref{pval}, p$<<$0.01).  It showed that the current year temperature is influenced/correlated with previous year's.
%Correlation coefficient from both overall (Sec.\ref{osp}) and sampled (Sec.\ref{ssp}) were only in medium levels.  The approximated p-value (Sec.\ref{pval}) is falsifying the hypothesis (p$>>$0.05).\\\\
%The results showed that time (i.e. year) is not a statistically-significant factor for the annual temperature for Florida in the twentieth century.  Hence the ``best-fitted" curve in the plot (Sec.\ref{plot}) was a mis-interpretation.  Other factors (including but not limited to atmospheric carbon dioxide levels, atmospheric sulphur dioxide levels and suspended particulates level) should also be considered in future analyses.

\end{document}


\documentclass[11pt, oneside]{article}   	% use "amsart" instead of "article" for AMSLaTeX format

% \usepackage{draftwatermark}
% \SetWatermarkText{Draft}
% \SetWatermarkScale{5}
% \SetWatermarkLightness {0.9}
% \SetWatermarkColor[rgb]{0.7,0,0}

\usepackage{geometry}                		% See geometry.pdf to learn the layout options. There are lots.
\geometry{letterpaper}                   		% ... or a4paper or a5paper or ...
%\geometry{landscape}                		% Activate for for rotated page geometry
%\usepackage[parfill]{parskip}    		% Activate to begin paragraphs with an empty line rather than an indent
\usepackage{graphicx}				% Use pdf, png, jpg, or eps� with pdflatex; use eps in DVI mode
								% TeX will automatically convert eps --> pdf in pdflat						\label{thm:integral_domain}

								% TeX will automatically convert eps --> pdf in pdflatex
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{mathrsfs}
\usepackage[hyphens,spaces,obeyspaces]{url}
\usepackage{url}
\usepackage{hyperref}
\usepackage{subcaption}
\usepackage{authblk}
\usepackage{mathtools}
\usepackage{graphicx}
\usepackage[export]{adjustbox}
\usepackage{fixltx2e}
\usepackage{hyperref}
\usepackage{alltt}
\usepackage{color}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{float}
\usepackage{bigints}
\usepackage{braket}
\usepackage{siunitx}

\usepackage[hyphenbreaks]{breakurl}

\newtheorem{thm}{Theorem}[section]
% \newtheorem{defn}[thm]{Definition}
\theoremstyle{definition}
\newtheorem{definition}{Definition}[section]
\newtheorem{proposition}{Proposition}[section]
\newtheorem{lemma}{Lemma}[section]

\newcommand{\veq}{\mathrel{\rotatebox{90}{$=$}}}
\DeclareMathOperator{\bda}{\Big \downarrow}

\DeclareMathOperator{\E}{\mathbb{E}}
\newcommand{\argmax}{\operatornamewithlimits{argmax}}
\newcommand{\argmin}{\operatornamewithlimits{argmin}}

\title{A Few Notes on the Division Algorithm (WIP)}
\author{David Meyer \\ dmm@\{1-4-5.net,uoregon.edu\}}

\date{Last update: \today}							% Activate to display a given date or no date

\begin{document}
\maketitle

\section{Introduction}

TBD

\section{Fields and the Division Algorithm}

\begin{definition}
Let $f$ and $g$ be polynomials in $F[x$]. Then we say that $f$ divides $g$, or $g$ is divisible by $f$ if there is a polynomial $h$ with $g = fh$.
\end{definition}

\noindent
For integers, the greatest common divisor of two integers $a$ and $b$ is the largest integer dividing both
$a$ and $b$. This definition doesn't  quite work for polynomials. In particular, while we cannot talk
about "largest" polynomial in the same manner as we do for integers, we can talk about the degree
of a polynomial.

\bigskip
\noindent
Recall that the degree of a nonzero polynomial $f$ is the largest integer $m$
for which the coefficient $a_m$ of $x^m$ is nonzero. For example, if $f(x) = a_nx^n + \hdots + a_1x+a_0$ with $a_n \neq 0$,
then the degree of $f(x)$, written $\deg f$, is $n$. The degree function allows
us to measure size of polynomials.

\bigskip
\noindent
There is one extra complication with this definition of $\deg f$. Consider for example that any polynomial of the form
$ax^2$ with $a \neq 0$ divides $x^2$ and $x^3$. Thus, there isn't a unique polynomial of
highest degree that divides a pair of polynomials. To get around this uniqueness problem the convention is to consider
the \emph{monic} polynomials, that is, those polynomials whose leading coefficient is $1$. For example, $x^2$ is the
monic polynomial of degree $2$ that divides both $x^2$ and $x^3$, while $5x^2$ is not monic.

\bigskip
\noindent
Finally, I'll use $ab$ to represent $a \cdot b$ where the meaning is unambiguous.

\begin{definition}
Let $f$ and $g$ be polynomials over $F$, where $f$ and $g$ are not both zero. Then a greatest common divisor of $f$ and $g$, $\gcd(f,g)$,
is a monic polynomial of largest degree that divides both $f$ and $g$.
\label{def:gcd}
\end{definition}

\noindent
Unfortunately, there is a problem with Definition \ref{def:gcd} which has to do with uniqueness. In particular, could there be more than one greatest common
divisor of a pair of polynomials?  As we will se below, the answer turns out to be no.

\bigskip
\noindent
Note that the main reason for assuming that the coefficients of our polynomials lie in a field is to ensure that the division algorithm is valid. Why?
Because division is well behaved in integral domains (where there are no zero divisors), and because every field is an integral domain and every finite
integral domain is a field. But why is that?

\begin{thm}
Every field $F$ is an integral domain.
\label{thm:integral_domain}
\end{thm}

\noindent
\textbf{Proof:} Recall that if $F$ is a field then each non-zero $r \in F$ has an inverse $r^{-1}$.
So suppose $r,s \in F$ and $r \neq 0$ such that $r \cdot s = 0$. Then the claim is that $s = 0$.
Why? Consider

\begin{equation}
\begin{array}{rcll}
r \cdot s
&=& 0                                                                                &\quad  \mathrel{\#} \text{assumption with $r \neq 0$}         \\
&\Rightarrow& r^{-1} \cdot (r \cdot s) = r^{-1} \cdot 0        &\quad  \mathrel{\#} \text{multiply both sides by $r^{-1}$}    \\
&\Rightarrow&  r^{-1} \cdot (r \cdot s) =  0                        &\quad  \mathrel{\#} x \cdot 0 = 0                                          \\
&\Rightarrow&  (r^{-1} \cdot r) \cdot s =  0                        &\quad  \mathrel{\#} \text{multiplication is associative}         \\
&\Rightarrow&  1 \cdot s = 0                                             &\quad  \mathrel{\#} r^{-1} \cdot r = 1                                   \\
&\Rightarrow&  s = 0                                                         &\quad  \mathrel{\#} 1 \cdot x = x
\end{array}
\end{equation}

\bigskip
\noindent
So $r$ is not a zero divisor. But every non-zero element $r$ of the field $F$ has an inverse ($r$ is a "unit")
so $F$ has no zero divisors and is by definition an integral
domain. $\square$

\bigskip
\begin{thm}
Every finite integral domain is a field.
\label{thm:finite_id_is_a_field}
\end{thm}

\noindent
\textbf{Proof:}  The proof is based on the fact that since $R$ is an integral domain it has a cancellation law (or equivalently, $R$ has no zero divisors).
Having a cancellation law means that

\begin{equation}
ab = ac \implies b = c
\label{eqn:cancellation_law}
\end{equation}

\bigskip
\noindent
To see why any finite integral domain $R$ is a field, consider $R = \{r, r^2, r^3, \hdots, r^n\}$  where
$r^k \neq 0$ for $1 \leq k \leq n$.  Since $R$ is finite we will have $r^k = r^l$ for some $k$ and $l$ such that $k > l$. Then

\bigskip
\begin{equation*}
\begin{array}{rlll}
r^k
&=& r^l                                                                                &    \qquad \mathrel{\#} \text{$R$ is a finite integral domain}        \\
&\Rightarrow& r \cdot r^{k-1} = r \cdot r^{l -1}                             &    \qquad \mathrel{\#} \text{factor out $r$}                                  \\
&\Rightarrow& r^{k-1} = r^{l -1}                                                   &    \qquad\mathrel{\#} \text{use cancellation law (cancel $r$, Equation \ref{eqn:cancellation_law}})    \\
&\Rightarrow& r \cdot r^{k-2} = r \cdot r^{l -2}                             &    \qquad\mathrel{\#} \text{factor out $r$}                                   \\
&\Rightarrow& r^{k-2} = r^{l -2}                                                   &    \qquad\mathrel{\#} \text{use cancellation law (cancel $r$, Equation \ref{eqn:cancellation_law})}  \\
&\vdots                                                                                && \qquad\mathrel{\#} \text{iterate $l - 1$ times}                          \\
&\Rightarrow& r^{k - l + 1} = r^1                                                 &     \qquad\mathrel{\#} \text{...}                                                    \\
&\Rightarrow& r \cdot r^{k - l} = r \cdot r^0                                 &     \qquad\mathrel{\#} \text{factor out $r$}                                   \\
&\Rightarrow& r^{k-l} = r^{0}                                                       &    \qquad\mathrel{\#} \text{use cancellation law (cancel $r$, Equation \ref{eqn:cancellation_law})}   \\
&\Rightarrow& r^{k-l} = 1                                                            &     \qquad\mathrel{\#} r^0 = 1
\end{array}
\end{equation*}

\bigskip
\noindent
So $ r^{k-l} = 1 $. If $k-l = 1$ then $r$ a unit since $r^{k-l} = r^{1} = 1$ so $r^{-1}$ is $\frac{1}{r}$. Otherwise
$k-l > 1$ and $r^{k-l} = 1 \Rightarrow r^{k-l-1} = \frac{1}{r}$.   So $r^{-1} = r^{k-l -1}$ and
every $r \neq 0 \in R$ has an inverse. Thus every non-zero  $r \in R$ is a unit and so $R$ is a field. $\square$

\bigskip
\noindent
So every field is an integral domain and every finite integral domain is a field and so division behaves reasonably in these cases.

\bigskip
\noindent
A few conventions: set $\deg 0 = -\infty$ and $-\infty + -\infty = - \infty$ and $-\infty +n = -\infty$ for $n \in \mathbb{Z}$.

\begin{lemma}
Let $F$ be a field and let $f$ and $g$ be polynomials over $F$. Then $\deg fg = \deg f  + \deg g$.
\label{lemma:degree}
\end{lemma}

\noindent
\textbf{Proof:} If either $f = 0$ or $g = 0$, then the equality $\deg fg = \deg f + \deg g$ is true by our convention above. So, suppose that
$f \neq 0$ and $g \neq 0$. Write $f = a_n x^n + \hdots + a_0$ and $g = b_m x^m + \hdots + b_0$ with $a_n \neq 0$ and $b_m \neq 0$.
Therefore, $\deg f = n$ and $\deg g = m$. The definition of polynomial multiplication yields

\begin{equation*}
fg=(a_nb_m)x^{n+m} +(a_nb_{m-1} + a_{n-1} b_m)  x^{n+m-1} + \hdots +a_0b_0
\end{equation*}

\bigskip
\noindent
Since every field is an integral domain and since the coefficients come from a field we know that there are no zero divisors and so we can conclude
that $a_nb_m \neq 0$, and so $\deg fg = n + m = \deg f+ \deg g$, as desired. $\square$

% \newpage

\begin{definition}
The characteristic of a field $F$, $\text{char}(F)$, is the smallest positive integer $n$ such that

\begin{equation*}
n \cdot 1 = \underbrace{1 + 1 + \cdots + 1}_{n \text{ times}} = 0
\end{equation*}
\end{definition}

\bigskip
\noindent
The characteristic has all kinds of interesting properties. For example

\begin{lemma}
Let $F$ be a field. Then
\begin{enumerate}
\item If $\text{char}(F) > 0$  then $\text{char} (F)$ is prime
\label{char:1}
\item If $F$ is a finite field then $\text{char} (F)  >  0$
\label{char:2}
\item If $F$ is a finite field then $\text{char} (F)$ is prime
\end{enumerate}
\end{lemma}

\bigskip
\noindent
\textbf{Proof:}

\begin{enumerate}
\item Assume that $n$ is not prime so that there exists a nontrivial factorization of $\text{char}(F) = n = p \cdot  q$. Then

\begin{equation*}
\begin{array}{rcll}
0
&=&  n \cdot 1                                                           &\qquad \mathrel{\#} \text{definition of $\text{char}(F)$} \\
&=&  (p \cdot q) \cdot 1                                             &\qquad \mathrel{\#} n = p \cdot q \\
&=&  p \cdot (q \cdot 1)                                             &\qquad \mathrel{\#} \text{multiplication is associative} \\
&=& (p \cdot 1) \cdot (q \cdot 1)                                &\qquad \mathrel{\#} p = p \cdot 1
\end{array}
\end{equation*}

So $(p \cdot 1) \cdot (q \cdot 1) = 0$. But we know that $F$ is a field and fields have no zero divisors so either $(p \cdot 1) = 0$ or
$(q \cdot 1) = 0$. But this contradicts the assumption that  $\text{char}(F)$ was the smallest positive integer $n$ such that $n \cdot 1 = 0$
since $p, q \neq 0$ and $n = p \cdot q$. So $n = \text{char}(F)$ must have been prime.
\label{enumerate:1}

Another way to think about this proof is to notice that if $F$ is a field of characteristic $n$ then the elements $\{0,1,2,\hdots,,n-1\}$ of $F$
obey the rules for addition and  multiplication modulo $n$. Therefore there is a copy of $\mathbb{Z}_n$ inside of $F$. Since $\mathbb{Z}_n$
has zero divisors when $n$ is not prime (and a field is an integral domain and so has no zero divisors, see Theorem \ref{thm:integral_domain}),
it follows that the characteristic of a field must be prime.

Thus every finite field $F$ must have characteristic $p$ for some prime $p$ and the elements $\{0,1,2,\hdots,p-1\}$ form a copy of
$\mathbb{Z}_p$ inside of $F$. This copy of $\mathbb{Z}_p$ is known as the \emph{prime subfield of F}.

\item Since $F$ is finite not every element can be unique. In particular,  consider a finite field $F = \{f, f^2, f^3, \hdots, f^n\}$  where
$f^k \neq 0$ for $1 \leq k \leq n$.  Since $F$ is finite we will have $(f^k \cdot 1) = (f^l \cdot 1)$ for some $k$ and $l$ where $k > l$.
Then $(f^k -  f^l ) \cdot 1  = 0$ and so $\text{char}(F) > 0$.
\label{enumerate:2}

\item Immediate by \ref{enumerate:1}. and \ref{enumerate:2}.
\end{enumerate}
\noindent
$\square$

\begin{proposition} \textbf{(Division Algorithm)}
Let $F$ be a field and let $f$ and $g$ be polynomials over $F$ with $f$ nonzero. Then there are unique polynomials $q$ and $r$ with $g = qf + r$
with $\deg r  < \deg f$.
\end{proposition}

\noindent
\textbf{Proof:}  Let $\mathcal{S} = \{t \in F[x] \mid t = g - qf \text{ for
some } q \in F[x] \}$. Now, we know $\mathcal{S} \neq \emptyset$
since $g \in \mathcal{S}$ (see Equation \ref{eqn:f} below). We also know by the well ordering property
of the integers \cite{well_ordering_principle} that there is a polynomial $r$ of least degree in $\mathcal{S}$. Then
by definition there is a $q \in F[x]$ with $r = g - qf$ and so $g = qf +r$.

\bigskip
\noindent
We need to show that $\deg r < \deg f$. So suppose that $\deg r \geq \deg f$, say with  $n = \deg f$ and $m = \deg r$.
If $f = a_nx^n + \hdots +a_0$ and
$r = r_mx^m + \hdots +r_0$ with $a_n \neq 0$ and $r_m \neq 0$, then by the method of
long division of polynomials, we see that $r = (r_m a_n^{-1})x^{m - n} f + r^\prime$ with  $\deg r^\prime < m = \deg r$. But then

\begin{equation*}
\begin{array}{rcll}
g
&=& qf + r                                                                  &\quad  \mathrel{\#} g \in \mathcal{S} \text{ and well ordering principle} \\
&=& qf + (r_m a_n^{-1}) x^{m - n}f + r^\prime           &\quad  \mathrel{\#}  r = (r_m a_n^{-1})x^{m - n} f + r^\prime \\
&=& (q + r_m a_n^{-1} x^{m - n}) f + r^\prime           &\quad  \mathrel{\#} \text{factor out $f$} \\
&\implies& r^\prime \in \mathcal{S}                           &\quad  \mathrel{\#} r^\prime = g - (q + r_m a_n^{-1} x^{m - n}) f \in \mathcal{S}
\end{array}
\end{equation*}

\bigskip
\noindent
Since $\deg r^\prime < \deg r$,   $r^\prime \in \mathcal{S} $ contradicts our choice of $r$ (the assumption that $r$ was the polynomial of least degree
in $\mathcal{S}$).  Therefore $\deg r \geq \deg f$ is false so $\deg r  < \deg f$ is true and this proves existence of $q$ and $r$.

\bigskip
\noindent
We still have to show the uniqueness of $q$ and $r$. To do this, suppose that $g = qf +r$ and $g = q^{\prime} f + r^\prime$ for some polynomials
$q,q^\prime, r,r^\prime \in F[x]$, and with both $\deg r < \deg f$ and $\deg r^\prime <  \deg f$. Then $qf +r = q^{\prime} f +r^{\prime}$, so
$(q -  q^{\prime})f = r^\prime - r$. Taking degrees and using Lemma \ref{lemma:degree}, we have

\begin{equation}
\deg (q - q^{\prime}) + \deg f  = \deg (r^\prime - r)
\label{eqn:qminusqprime}
\end{equation}

\bigskip
\noindent
Since $\deg r <  \deg f$ and $\deg r^\prime <  \deg f$, we have $\deg (r^\prime - r) < 0$. However, if $\deg(q^\prime - q) \geq 0$,
we get a contradiction of Equation \ref{eqn:qminusqprime}. In particular, by our convention the only way for this to happen is for
$\deg(q^\prime - q) = \deg(r^\prime - r) = - \infty$, and the only way for that to happen is if $q^\prime - q = 0 = r^\prime - r$. So $q^\prime = q$ and
$r^\prime =r$, which shows uniqueness. $\square$

\bigskip
\begin{proposition}
Let $F$ be a field and let $f$ and $g$ be polynomials over $F$, with not both $f$ and $g$ zero. Then $\gcd(f, g)$ exists and is unique.
Furthermore, there are polynomials $h$ and $k$ with $\gcd(f, g) = hf + kg$.
\end{proposition}

\noindent
\textbf{Proof:}  Here we have to show that $\gcd(f, g)$ exists and that it is unique. To show that $\gcd(f, g)$ exists,
let $\mathcal{S} = \{hf + kg : h, k \in F [x]\}$. Then $\mathcal{S}$ contains nonzero polynomials $f$ and $g$ (at least), since
for some $f^\prime \in \mathcal{S}$

\begin{equation}
\begin{array}{rcll}
f^\prime
&=& hf +  kg                                            &\quad  \mathrel{\#} \text{definition of elements of } \mathcal{S}\\
&=& 1 \cdot f + 0 \cdot g                         &\quad  \mathrel{\#} \text{let $h = 1$ and $k = 0$}  \\
&=& f                                                       &\quad  \mathrel{\#} \text{so $f \in \mathcal{S}$}                    \\
\end{array}
\label{eqn:f}
\end{equation}

\bigskip
\noindent
Likewise for $g$ (let $h = 0$ and $k = 1$). So $f,g \in \mathcal{S}$. But this means that there is a nonzero polynomial $d \in \mathcal{S}$ of
smallest degree by the well ordering principle \cite{well_ordering_principle}, so we can write $d = hf + kg$ for some $h, k \in F [x]$. By dividing by the leading coefficient of
$d$, we may assume that $d$ is monic without changing the  condition that $d$ is the polynomial of least degree in $\mathcal{S}$.

\bigskip
\noindent
Now the claim is that $d = \gcd(f,g)$. To show that $d$ is a common divisor of $f$ and $g$, first consider $f$. By the division algorithm, we can
write $f$ as $f = qd + r$ for some polynomials $q$ and $r$ with $\deg r < \deg d$. Then

\begin{equation}
\begin{array}{rcll}
f
&=& qd +  r                                            &\mathrel{\#}  f \in \mathcal{S} \text{ by Equation \ref{eqn:f}}, \mathcal{S} \text{ has a division algorithm} \\
&\implies & r = f  - qd                             &\mathrel{\#} \text{solve for $r$}\\
&\implies& r = f - q(hf + kg)                    &\mathrel{\#}  d = hf + kg \\
&\implies& r = f - qhf  - qkg                     &\mathrel{\#} \text{arithmetic}\\
&\implies& r = (1 - qh)f  + (-qk)g             &\mathrel{\#} F[x]  \text{ is closed under */+ so } r  \text{ is of the form } hf + kg \\
&\implies&  r \in \mathcal{S}                   &\mathrel{\#} r \text{ is of the form } hf + kg \text{ so } r \in \mathcal{S}
\end{array}
\label{eqn:division}
\end{equation}

\bigskip
\noindent
So $r \in \mathcal{S}$. Now, if $r \neq 0$ we have contradiction since we assumed that  $\deg(r) < \deg(d)$ and that $d$ was the polynomial of lowest degree
in $\mathcal{S}$. Thus $r$ must equal $0$, which shows that $f = qd$, and so $d$ divides $f$. Similarly, $d$ divides $g$ and so $d$ is a common divisor of $f$ and $g$.

\bigskip
\noindent
If  $e$ is any other common divisor of $f$ and $g$, then e divides any combination of $f$ and $g$; in particular, $e$ divides $d= hf + kg $.
This forces $\deg(e) \leq \deg(d)$ by Lemma \ref{lemma:degree}. Thus, $d$ is the monic polynomial of largest degree that divides $f$ and $g$, so $d$ is a greatest
common divisor of $f$ and $g$.  This shows that $\gcd(f,g)$ exists.

\bigskip
\noindent
To show that $\gcd(f,g)$ is unique, suppose that $d$ and $d^\prime$ are both monic common divisors of $f$ and
$g$ of largest degree.  By Equation \ref{eqn:division}, we can write both $d$ and $d^\prime$ as combinations of $f$ and $g$. In addition, the argument above shows that
$d$ divides $d^\prime$ and vice-versa. If $d^\prime = ad$ and $d = bd^\prime$, then $d = bd^\prime = bad$. Taking degrees shows that $\deg(ba) = 0$, which means
that $a$ and $b$ are both constants. However since $d$ and $d^\prime$ are monic, for $d^\prime = ad$ to be monic it must be that $a = 1$. Thus, $d^\prime = ad = d$.
This shows that the greatest common divisor is unique, completing the proof.

\bigskip
\section{Acknowledgements}

\newpage
\bibliographystyle{plain}
\bibliography{/Users/dmm/papers/bib/qc}
\end{document}