\section{Misc}

\begin{lemma}
	Let the $h$ be the curvature of quadratic function $f$. With $\overline{x}_t$ being the expectation of $x_t$, we assume, without loss of generality, the optimum point of the quadratic $f$ is 0. Then we have the following recurrence
	\begin{equation}
		\begin{pmatrix}
			\overline{x}_{t + 1} \\
			\overline{x}_t
		\end{pmatrix} =
		\begin{pmatrix}
			1-\alpha h + \mu & - \mu\\
			1 & 0 \\
		\end{pmatrix}
		\begin{pmatrix}
			\overline{x}_1 \\
			\overline{x}_0
		\end{pmatrix}
	\end{equation}
\end{lemma}

\begin{lemma}
	We define $U_t=\E \| x_t - \overline{x}_t \|^2$ and $V_t= \E (x_t - \overline{x}_t) (x_{t-1} - \overline{x}_{t-1})$ with $\overline{x}_t$ being the expectation of $x_t$. For quadratic function $f(x)$ with curvature $h \in \mathbb{R}$, We have the following recurrence
	\begin{equation}
		\begin{pmatrix}
			U_{t+1} \\
			U_t \\
			V_{t + 1}
		\end{pmatrix} =
		(\mat{I} - \mat{S}^T)(\mat{I} - \mat{S})^{-1}
		\begin{pmatrix}
			\alpha^2 \\
			0 \\
			0
		\end{pmatrix}
	\end{equation}
	where
	\begin{equation}
		\mat{S} =
		\begin{pmatrix}
		(1-\alpha h + \mu)^2 &  \mu^2 & -2\mu(1-\alpha h + \mu)\\
		1 & 0 & 0 \\
		1-\alpha h + \mu & 0 & - \mu
		\end{pmatrix}
	\end{equation}
\end{lemma}

\begin{lemma}
\label{lem:2times2_spectral_radius}
	Let $\rho(\mat{T})$ be the spectral radius of matrix
\begin{equation}
	\mat{T} = \begin{pmatrix}
	1-\alpha h + \mu & - \mu\\
	1 & 0 \\
	\end{pmatrix}
\end{equation}
where $h\in \mathbb{R}$. $\rho(\mat{T}) = \sqrt{\mu}$ if the following condition holds
\begin{align}
{(1-\sqrt{\mu})^2\over \alpha} &\leq h \leq {(1+\sqrt{\mu})^2\over \alpha}.
\label{equ:control_condition_scalar}
\end{align}
\end{lemma}

\begin{lemma}
\label{lem:3times3_spectral_radius}
Let $\rho(\mat{S})$ be the spectral radius of matrix
\begin{equation}
	%\rho_V(\alpha, \mu) = \rho \left(
\mat{S} = {\begin{pmatrix}
(1-\alpha h + \mu)^2 &  \mu^2 & -2\mu(1-\alpha h + \mu)\\
1 & 0 & 0 \\
1-\alpha h + \mu & 0 & - \mu
\end{pmatrix}}
	%\right)
\end{equation}
where $h\in \mathbb{R}$.
$\rho(\mat{S})=\mu$ if the following condition holds for
\begin{equation}
{(1-\sqrt{\mu})^2\over \alpha} \leq h \leq {(1+\sqrt{\mu})^2\over \alpha}
\label{equ:control_condition_var}
\end{equation}
\end{lemma}

\begin{corollary}
\label{cor:}
Under the conditions in Eqaution~\eqref{equ:control_condition_var}, we get
\begin{equation}
	\E \| x_T \|^2
	= \mu^T \| x_0 \|^2
		+ (1-\mu^T) \frac{\alpha^2 C}{1-\mu}
	\label{equ:noisy_square_dist}
\end{equation}
\label{cor:noisy_dist}
\end{corollary}

Now, if we know, or can approximately estimate some of these quantities we can tune the learning rate and momentum hyperparameters to minimize  \eqref{equ:noisy_square_dist}.

\section{Proof of Lemma~\ref{}}
The generalization of scalar analysis to multiple dimensional case.
\begin{lemma}
\label{lem:robustness}
Let the gradients of a function $f$ be described by
\begin{equation}
	\nabla f(\mat{x}_t) = \mat{H}_t (\mat{x}_t - \mat{x}^*),
\end{equation}
for $\mat{H}_t \in \mathbb{R}^{n\times n}$.
The the momentum update can be expressed as a linear operator:
\begin{align}
{\begin{pmatrix}
\mat{y}_{t+1}\\
\mat{y}_t \\
\end{pmatrix}}
=
{\begin{pmatrix}
\mat{I}-\alpha \mat{H}_t + \mu \mat{I} & - \mu \mat{I} \\
\mat{I} & \mat{0} \\
\end{pmatrix}}
{\begin{pmatrix}
\mat{y}_t \\
\mat{y}_{t-1} \\
\end{pmatrix}}
=\mat{T}_t
{\begin{pmatrix}
\mat{y}_t \\
\mat{y}_{t-1} \\
\end{pmatrix}},
\end{align}
where $\mat{y}_t\triangleq \mat{x}_t - \mat{x}^*$.
Now, assume that the following condition holds for all eigenvalue $\rho(\mat{H}_t)$ of $\mat{H}_t$ for all $t$:
\begin{align}
{(1-\sqrt{\mu})^2\over \alpha} &\leq \rho(\mat{H}_t ) \leq {(1+\sqrt{\mu})^2\over \alpha}.
\label{equ:control_condition}
\end{align}
then the spectral radius of $\mat{T}_t$ is controlled by momentum with
$	\rho(\mat{T}_t) = \sqrt{\mu}.$

\begin{proof}
Let $\lambda_t$ be an eigenvalue of matrix $\mat{T}_t$, it gives
$\det\left(\mat{T}_t - \lambda_t \mat{I} \right) = 0$.
We define $\mat{A}_t = \mat{I} - \alpha \mat{H}_t + \mu \mat{I} - \lambda_t \mat{I}$, $\mat{B}_t = -\mu \mat{I}$,
$\mat{C}_t = \mat{I}$ and $\mat{D}_t = -\lambda_t \mat{I}$ which gives
\[
\det \left( \mat{T}_t - \lambda_t \mat{I}\right) = \det{\mat{D}_t} \det{\left(\mat{A}_t - \mat{B}_t \mat{D}_t^{-1}
\mat{C}_t \right)} = 0
\]
assuming generally $\mat{D}_t$ is invertable. The equation $\det{\left(\mat{A}_t - \mat{B}_t \mat{D}_t^{-1}
\mat{C}_t \right)} = 0$ implies that
\begin{equation}
\det \left( \lambda_t^2\mat{I} - \lambda_t \mat{M}_t + \mu \mat{I} \right) = 0
\label{equ:control_condition_2}
\end{equation}
with $\mat{M}_t = \left( \mat{I} - \alpha \mat{H}_t + \mu \mat{I} \right)$. In other words, $\lambda_t$ satisfied that $\lambda_t^2 - \lambda_t \rho(\mat{M}_t) + \mu = 0$ with $\rho(\mat{M}_t)$ being one eigenvalue of $\mat{M_t}$. I.e.
\begin{equation}
	\lambda_t = \frac{\rho(\mat{M}_t) \pm \sqrt{\rho(\mat{M}_t)^2 - 4\mu}}{2}
\end{equation}

On the other hand, Equation~\eqref{equ:control_condition} guarantees that $(1 - \alpha \rho(\mat{H}_t) + \mu)^2 \leq 4\mu$. We know both $\mat{H}_t$ and $\mat{I} - \alpha \mat{H}_t + \mu \mat{I}$ are symmetric. Thus for all eigenvalues $\rho(\mat{M}_t)$ of $\mat{H}$, we have $\rho(\mat{M}_t) = (1 - \alpha \rho(\mat{H}_t) + \mu)^2 \leq 4\mu$ which guarantees $| \lambda_t | \leq \sqrt{\mu}$ for all $\lambda_t$ and all $t$. As the spectral radius is equal to the absolute value of the largest eigenvalue of $\mat{T}_t$, we have the spectral radius of $\mat{T}_t$ is $\sqrt{\mu}$ for all $t$.

\end{proof}

\end{lemma}

\begin{corollary}
	Let the gradients of a function $f$ be described by
\begin{equation}
	\nabla f(x_t) = h_t (x_t - x^*),
\end{equation}
for $h_t \in \mathbb{R}$.
The the momentum update can be expressed as a linear operator:
\begin{align}
{\begin{pmatrix}
y_{t+1}\\
y_t \\
\end{pmatrix}}
=
{\begin{pmatrix}
1-\alpha h_t + \mu & - \mu\\
1 & 0 \\
\end{pmatrix}}
{\begin{pmatrix}
y_t \\
y_{t-1} \\
\end{pmatrix}}
=\mat{T}_t
{\begin{pmatrix}
y_t \\
y_{t-1} \\
\end{pmatrix}},
\end{align}
where $y_t\triangleq x_t - x^*$.
Now, assume that the following condition holds for all $t$:
\begin{align}
{(1-\sqrt{\mu})^2\over \alpha} &\leq h_t \leq {(1+\sqrt{\mu})^2\over \alpha}.
\label{equ:control_condition_scalar}
\end{align}
then the spectral radius of $\mat{T}_t$ is controlled by momentum with
$	\rho(\mat{T}_t) = \sqrt{\mu}.$
\end{corollary}

\begin{lemma}
Let $\rho(\mat{S})$ be the spectral radius of matrix
\begin{equation}
	%\rho_V(\alpha, \mu) = \rho \left(
\mat{S} = {\begin{pmatrix}
(\mat{I}-\alpha \mat{H} + \mu \mat{I})^T(\mat{I}-\alpha \mat{H} + \mu \mat{I}) &  \mu^2 \mat{I} & -2\mu(\mat{I}-\alpha \mat{H} + \mu \mat{I})\\
\mat{I} & \mat{0} & \mat{0} \\
\mat{I}-\alpha \mat{H} + \mu \mat{I} & \mat{0} & - \mu \mat{I}
\end{pmatrix}}
	%\right)
\end{equation}
$\rho(\mat{S})=\mu$ if the following condition holds for all eigenvalues $\rho(\mat{H})$ of $\mat{H}\in \mathbb{R}^{n \times n}$
\begin{equation}
{(1-\sqrt{\mu})^2\over \alpha} \leq \rho(\mat{H}) \leq {(1+\sqrt{\mu})^2\over \alpha}
\label{equ:control_condition_var}
\end{equation}

\begin{proof}
	Let $\lambda$ be an eigenvalue of matrix $\mat{S}$, it gives
$\det\left(\mat{S} - \lambda \mat{I} \right) = 0$ which can be alternatively expressed as
\begin{equation}
\det \left( \mat{S} - \lambda \mat{I}\right) = \det{\mat{D}} \det{\left(\mat{A} - \mat{B} \mat{D}^{-1}
\mat{C} \right)} = 0
\label{equ:control_condition_var_1}
\end{equation}
assuming $\mat{D}$ is invertible, i.e. $\lambda + \mu \neq 0$, where \begin{equation*}
		\mat{A} = \left( { \begin{array}{c c}
 			\mat{M}^T\mat{M} - \lambda \mat{I} &  \mu^2 \mat{I} \\
 			\mat{I} & - \lambda \mat{I}
 		\end{array} } \right),
 		\mat{B} = \left( { \begin{array}{c}
 			-2\mu \mat{M} \\
 			\mat{0}
 		\end{array}}\right),
 		\mat{C} = \left( {\begin{array}{c}
 			\mat{M} \\
 			\mat{0}
 		\end{array}} \right)^T,
 		\mat{D} = -\mu \mat{I} - \lambda \mat{I}
	\end{equation*}
	with $\mat{M}=\mat{I}-\alpha \mat{H} + \mu \mat{I}$. It can be transformed using straight-forward algebra as
	\begin{equation}
		\det \left( \begin{array}{c c}
 			(\lambda - \mu) \mat{M}^T\mat{M} - (\lambda + \mu) \lambda \mat{I} & (\lambda + \mu)\mu^2 \mat{I} \\
 			(\lambda + \mu) \mat{I} & -(\lambda + \mu)\lambda \mat{I}
 		\end{array} \right)
		\label{equ:control_condition_var_2}
	\end{equation}
	Using similar simplification technique as in Equation~\eqref{equ:control_condition_var_1}, we can further simplify into
	\begin{equation}
		(\lambda - \mu)\det \left( (\lambda + \mu)^2 \mat{I} - \lambda \mat{M}^T\mat{M} \right) = 0
	\end{equation}
	if $\lambda \neq \mu$, as $(\lambda + \mu)^2 \mat{I} - \lambda \mat{M}^T\mat{M}$ is diagonalizable, we have $(\lambda + \mu)^2 - \lambda \rho(\mat{M})^2 = 0$ with $\rho(\mat{M})$ being an eigenvalue of $\mat{M}$. The analytic solution to the equation can be explicitly expressed as
	\begin{equation}
		\lambda = \frac{\rho(\mat{M})^2 - 2\mu \pm \sqrt{(\rho(\mat{M})^2 - 2\mu)^2 - 4\mu^2}}{2}
		\label{equ:control_condition_var_3}
	\end{equation}.

	When the condition in Equation~\eqref{equ:control_condition_var} holds, we have $(1 - \alpha \rho(\mat{H}_t) + \mu)^2 \leq 4\mu$. One can verify that

	\begin{equation}
		\begin{aligned}
			(\rho(\mat{M})^2 - 2\mu)^2 - 4\mu^2 & = && (\rho(\mat{M})^2 - 4\mu)\rho(\mat{M})^2 \\
			& = &&\left( (1 - \alpha \rho(\mat{H} ) + \mu)^2 - 4\mu\right)\rho(\mat{M})^2 \\
			& \leq && 0
		\end{aligned}
	\end{equation}
	Thus the roots in~\eqref{equ:control_condition_var_3} are conjugate and we have $| \lambda | = \mu$. In conclusion, the condition in Equation~\eqref{equ:control_condition_var} can guarantee all the eigenvalues of $\mat{S}$ has magnitude $\mu$. Thus the spectral radius of $\mat{S}$ is controlled by $\mu$
\end{proof}

\end{lemma}

\begin{corollary}

Let $\rho(\mat{S})$ be the spectral radius of matrix
\begin{equation}
	%\rho_V(\alpha, \mu) = \rho \left(
\mat{S} = {\begin{pmatrix}
(1-\alpha h + \mu)^2 &  \mu^2 & -2\mu(1-\alpha h + \mu)\\
1 & 0 & 0 \\
1-\alpha h + \mu & 0 & - \mu
\end{pmatrix}}
	%\right)
\end{equation}
where $h\in \mathbb{R}$.
$\rho(\mat{S})=\mu$ if the following condition holds for
\begin{equation}
{(1-\sqrt{\mu})^2\over \alpha} \leq h \leq {(1+\sqrt{\mu})^2\over \alpha}
\label{equ:control_condition_var}
\end{equation}

\end{corollary}

\begin{theorem}
refer to the main content to theorem 2. \textbf{We need to assume quadratic in this theorem}
\begin{proof}
	From the recurrence of momentum SGD, we have $x_{t+1} = x_t - \alpha \nabla f_{S_t} (x_t) + \mu (x_t - x_{t-1})$. And the expected squared distance to the optimal point can be decomposed as $\E \| x_t - x^* \|^2 = \E \| x_t - \overline{x}_t \|^2 + \| \overline{x}_t- x^* \|^2$, where $\overline{x}_t$ is the expectation of $x_t$.
	Without loss of generality, we assume the optimum $x^*=0$ and investigate the bias term $\| \overline{x}_t- x^* \|^2$ and variance term $\E \| x_t - \overline{x}_t \|^2$ respectively.

	For the bias term, we have $\| \overline{x}_{t + 1} \|^2 = \| \overline{x}_t - \alpha \nabla f( \overline{x}_t) + \mu ( \overline{x}_t -  \overline{x}_{t - 1})\|^2$
	\begin{equation}
		\left(\begin{array}{c}
		 \overline{x}_{t+1} \\
		 \overline{x}_{t}
		\end{array}\right) =
		\left(\begin{array}{c c}
			1 - \alpha h_t + \mu & - \mu \\
			1 & 0
		\end{array}\right)
		\left(\begin{array}{c}
		 \overline{x}_{t} \\
		 \overline{x}_{t-1}
		\end{array}\right)
	\end{equation}

	For the variance term recurrence, we have $\nabla f(x_t) = h x_t$
	\begin{equation}
		\begin{aligned}
			\E \| x_{t + 1} - \overline{x}_{t + 1} \|^2 = \E \| x_{t} - \alpha \nabla f_{S_t}(x_t) + \mu (x_{t} - x_{t - 1})
				- \overline{x_t} + \alpha \nabla f(\overline{x}_t) - \mu (
				\overline{x}_t - \overline{x}_{t - 1}) \|^2
		\end{aligned}
	\end{equation}
\end{proof}

\end{theorem}


% !TEX root = main.tex

%-------------------------------------------------
\section{Likelihood}\label{sec:likelihood}

Let $f(x;\theta)$ denote the PMF/PDF of a random variable $X$. We have so far considered $f(x;\theta)$ to be a function of $x$ and $\theta$ as a fixed parameter. We now change the emphasis and regard $x$ as a fixed observation and $\theta$ as a variable parameter. To underline this new emphasis we refer to $f(x;\theta)$ as the \emph{likelihood function} of $\theta$.

\bigskip
Let $\mathbf{X}=(X_1,X_2,\ldots,X_n)$ be a random sample from the distribution of $X$, let $\mathbf{x}=(x_1,x_2,\ldots,x_n)$ be a realisation of the sample and let $\Theta$ denote the parameter space.

\begin{definition}
Given $\mathbf{X}=\mathbf{x}$, the \emph{likelihood function} $L:\Theta\to [0,\infty)$ is
\[
L(\theta;\mathbf{x}) = \displaystyle\prod_{i=1}^n f(x_i;\theta).
\]
\end{definition}

Note that the right-hand side is just the joint PMF/PDF of the random sample. Until now we have interpreted this as the probability that the observed sample is $\mathbf{x}$ with $\theta$ a fixed parameter. For statistical inference, we instead consider the likelihood $L(\theta;\mathbf{x})$ that $\theta$ is the true parameter value given the fixed observations $\mathbf{x}$: such observations are often referred to as \emph{data}.

\bigskip
The value of $\theta$ which maximises the likelihood function provides an estimator of the true parameter value, subject to the following regularity conditions.

\begin{condition}\label{cond:regularity1}
%Let $\theta_0$ be the true value of the unknown parameter $\theta$. Our statistical model $\mathcal{M}=\{f(x;\theta):\theta\in\Theta\}$ is assumed to satisfy the following conditions.
Our statistical model $\mathcal{M}=\{f(x;\theta):\theta\in\Theta\}$ satisfies the following conditions.
\ben
%\it[C.1] The PDFs are distinct, i.e. $\theta\neq\theta' \implies f(x;\theta)\neq f(x;\theta')$.
%\it[C.2] The PDFs have common support for all $\theta\in\Theta$.
%\it[C.3] The true value of $\theta$ is an interior point of the parameter space $\Theta$.
\it PDFs are distinct, i.e. $\theta\neq\theta' \implies f(x;\theta)\neq f(x;\theta')$.
\it PDFs have a common support for all $\theta\in\Theta$.
\it The true value of $\theta$ is an interior point of the parameter space $\Theta$.
\een
\end{condition}
We shall not consider these conditions in detail.

%We also need Jensen's inequality and the notion of convex functions.
% defn: convex function
%\begin{definition}
%A function $g:\R\to\R$ is said to be \emph{strictly convex} if
%\[
%g\big[\lambda x_1 + (1-\lambda)x_2\big] < \lambda g(x_i) + (1-\lambda)g(x_2)
%\quad\text{for all $x_1\neq x_2$ and all $\lambda\in (0,1)$.}
%\]

%\ben
%\it \emph{convex} if
%\[
%g\big[\lambda x_1 + (1-\lambda)x_2\big] \leq \lambda g(x_i) + (1-\lambda)g(x_2) \quad\text{for all $x_1,x_2\in\R$ and $\lambda\in [0,1]$.}
%\]
%\it \emph{strictly convex} if
%\[
%g\big[\lambda x_1 + (1-\lambda)x_2\big] < \lambda g(x_i) + (1-\lambda)g(x_2) \quad\text{for all $x_1\neq x_2$ and $\lambda\in (0,1)$.}
%\]
%\ben
%\it \emph{convex} if $g\big[\lambda x_1 + (1-\lambda)x_2\big] \leq \lambda g(x_i) + (1-\lambda)g(x_2)$ for all $x_1,x_2\in\R$ and $\lambda\in [0,1]$.
%\it \emph{strictly convex} if $g\big[\lambda x_1 + (1-\lambda)x_2\big] < \lambda g(x_i) + (1-\lambda)g(x_2)$ for all $x_1\neq x_2$ and $\lambda\in (0,1)$.
%\een
%\end{definition}
%
%% lemma: Jensen
%\begin{lemma}[Jensen's inequality]
%If $g:\R\to\R$ is a convex function then $g\big[\expe(X)\big] \leq \expe\big[g(X)\big]$.
%\end{lemma}
%
%The following theorem shows that $L(\theta)$ is maximised at the true value of $\theta$ as $n\to\infty$.
%
%% thm: mle
%\begin{theorem}\label{thm:mle}
%Let $\theta_0$ be the true value of the unknown parameter $\theta$. Under conditions C.1 and C.2,
%\[
%\lim_{n\to\infty} \prob_{\theta_0}\big[L(\theta_0,\mathbf{X})>L(\theta,\mathbf{X})\big] = 1
%\quad\text{for all $\theta\neq\theta_0$.}
%\]
%where $\prob_{\theta_0}$ is the probability measure on $\R^n$ induced by the random sample.
%\end{theorem}
%\begin{proof}
%Taking logs, the inequality $L(\theta_0,\mathbf{X})-L(\theta,\mathbf{X})$ is equivalent to
%\[
%\frac{1}{n}\sum_{i=1}^n\log\left(\frac{f(X_i;\theta)}{f(X_i;\theta_0)}\right) < 0.
%\]
%By the law of large numbers applied to the random variable $\log\big[f(X;\theta)/f(X,\theta_0)\big]$,
%\[
%\frac{1}{n}\sum_{i=1}^n\log\left(\frac{f(X_i;\theta)}{f(X_i;\theta_0)}\right)
%\to \expe_{\theta_0}\log\left(\frac{f(X_i;\theta)}{f(X_i;\theta_0)}\right)
%\quad\text{in probability as $n\to\infty$.}
%\]
%Furthermore, because $-\log(x)$ is strictly convex, by Jensen's inequality we have
%\[
%\expe_{\theta_0}\log\left(\frac{f(X_i;\theta)}{f(X_i;\theta_0)}\right)
%\leq\log\expe_{\theta_0}\left(\frac{f(X_i;\theta)}{f(X_i;\theta_0)}\right)
%\]
%and because the PDFs $f(x;\theta)$ have a common support,
%\[
%\expe_{\theta_0}\left(\frac{f(X_i;\theta)}{f(X_i;\theta_0)}\right)
%	= \int \frac{f(x;\theta)}{f(x;\theta_0)}f(x;\theta_0)\,dx
%	= \int f(x;\theta)\,dx
%	= 1.
%\]
%Finally, because $\log 1 = 0$ the result follows.
%\end{proof}
%
%Theorem~\ref{thm:mle} shows that a good estimator of $\theta_0$ is one which maximises the likelihood function.
%
%%-----------------------------
%\subsection{The score function}
%The first derivative of the log-likelihood function is called the \emph{score function}.
%\begin{definition}
%\end{definition}


\documentclass{article}

\usepackage[letterpaper, margin=1.3cm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{siunitx}
\usepackage{mathtools}
\usepackage{multicol}

\title{ECE 240 Assignment 9}
\author{Michael Kwok}
\begin{document}

\maketitle
\begin{multicols}{2}
\section*{1}
\subsection*{i}
$$F(s) = \frac{s+2}{s(s+1)^2}$$
\begin{align*}
    \frac{s+2}{s(s+1)^2} &= \frac{A}{s}+\frac{B}{s+1} + \frac{C}{(s+1)^2}\\
    s+2 &= A(s+1)^2 + Bs(s+1) + Cs\\
    \text{Let } s &= 0:\\
    A &= 2\\
    \text{Let } s &= -1:\\
    1 &= A(0) -B(0) -C\\
    C &= -1\\
    s+2 &= 2(s+1)^2 + B(s^2 + s) - s\\
    2s + 2 &= 2(s+1)^2 + B(s^2 + s)\\
    2 &= 2(s+1) + Bs\\
    -2s &= Bs\\
    B &= -2\\
    F(s) &= \frac{2}{s} - \frac{2}{s+1} - \frac{1}{(s+1)^2}
\end{align*}
\begin{align*}
\mathcal{L}^{-1}\{F(s)\} &= f(t)\\
&= 2\delta(t) - 2 e^{-t}u(t)-te^{-t}u(t)
\end{align*}
\subsection*{ii}
let $x = e^{-s}$
\begin{align*}
    F(s) = \frac{x+x^2+1}{s^2+3s+2} &= \frac{x+x^2+1}{(s+1)(s+2)}\\
    \frac{x+x^2+1}{(s+1)(s+2)} &= \frac{A}{s+1} + \frac{B}{s+2}\\
    x^2+x+1 &= A(s+2) + B (s+1)\\
    \text{let } s &= -1\\
    A(-1+2) &= e^2 + e + 1\\
    \text{let } s &= -2\\
    B(-2+1) &= e^4 + e^2 + 1\\
    F(s) &= \frac{e^2+e+1}{s+1} - \frac{e^4+e^2+1}{s+2}\\
    f(t) &= e^{-t}u(t)(e^2+e+e)\\
     &- e^{-2}u(t)(e^4+e^2+1)
\end{align*}
\section*{2}
\begin{align*}
&s^2 Y(s) - s y(0^-) - y'(0^-) + 6 sY(s) - 6y(0^-)+25Y(s)\\
&= sF(s) - f(0^-) + 2F(s)\\
\end{align*}
\[
(s^2+6s+25)Y(s) = \overbrace{s+7}^{\text{Zero input}} + \overbrace{sF(s) - f(0^-) + sF(s)}^{\text{Zero state}}
\]
\begin{align*}
    F(s) &= \frac{25}{s}\\
    f(0) &= 0
\end{align*}
\begin{align*}
    Y(s) &= \frac{s+7}{s^2+6s+25} + \frac{25+ \frac{50}{s}}{s^2+6s+25}\\
    &= \frac{(s+3)+4}{(s+3)^2+4^2} + \frac{2}{s} + \frac{13-2s}{s^2+6s+25}\\
    &= \frac{s+3}{(s+3)^2+4^2} + \frac{4}{(s+3)^2+4^2} + \frac{2}{s}\\
    &-2 \frac{s+3}{(s+3)^2+4^2} + \frac{19}{4}\frac{4}{(s+3)^2+4^2}\\
    y(t) &= \overbrace{\left[\left( cos(4t) + sin(4t) \right) e^{-3t} + 2\right]u(t)}^{\text{Zero Input}}\\
    &\overbrace{\left[- 2e^{-3t}cos(4t) + \frac{19}{4}e^{-3t}sin(4t)\right]u(t)}^{\text{Zero State}}
\end{align*}
\section*{3}
\begin{align*}
&s^3 Y(s) + 6s^2 Y(s) -11 sY(s) + 6 Y(s) =\\
&3s^2 F(s) + 7sF(s) +5F(s)
\end{align*}
\begin{align*}
    (s^3 + 6s^2 -11 s + 6) Y(s) &= (3s^2 + 7s +5) F(s)\\
    \frac{Y(s)}{F(s)} &= \boxed{\frac{3s^2 + 7s +5}{s^3 + 6s^2 -11 s + 6}}
\end{align*}
\section*{4}
\subsection*{a}
\begin{align*}
    F(s) &= \frac{1}{s+3}\\
    Y(s) &= \frac{s+5}{(s+2)(s+3)^2}\\
    &= \frac{A}{s+2} + \frac{B}{s+3} + \frac{C}{(s+3)^2}
\end{align*}

\begin{align*}
    s+5 = A(s+3)^2 &+ B(s+2)(s+3) + C(s+2)\\
    \text{let } s &= -2,\\
    A &= 3\\
    \text{let } s &= -3,\\
    2 &= C(-3+2)\\
    C &= -2\\
    s+5 &= 3(s+3)^2 + B(s+2)(s+3) + -2(s+2)\\
    -3s^2-15s-18 &= B(s^2+5s+6)\\
    B &+ -3\\
\end{align*}

\begin{align*}
    Y(s) &= \frac{3}{s+2} - \frac{3}{s+3} - \frac{2}{(s+2)^2}\\
    y(t) &= 3 e^{-2t} u(t) - 3 e^{-3t} u(t) - 2t e^{-3t}u(t)\\
    &= \left[ 3e^{-2t} e^{-3t} (3+2t) \right] u(t)
\end{align*}
\subsection*{b}
\begin{align*}
    H(s) &= \frac{s+5}{s^2+5s+6}\\
    (s^2 +5s +6) Y(s) &= (s+5) F(s)\\
    y''(t) + 5 y'(t) + 6y(t) &= f'(t) + 5 f(t)
\end{align*}
\section*{5}
\subsection*{a}
\begin{align*}
    H_1(s) &= \frac{s+1}{s^2+8s+12}\\
    &= \frac{s+1}{(s+2)(s+6)}\\
\end{align*}
Poles at $s_1 = -2, s_2 = -6$

System is BIBO Stable.
\subsection*{b}
$$
H_2(s) = \frac{s+5}{s(s+1)(s+2)}
$$
Poles at $s_1 = 0, s_2 = -1, s_3 = -2$

System is BIBO Unstable.
\subsection*{c}
Poles at $s_1 = 0, s_2 = \sqrt{2}, s_3 = -\sqrt{2}$

System is BIBO Unstable.
\subsection*{d}
Poles at $s_1 = -1, s_2 = 5, s_3 = 1$

System is BIBO Unstable.
\section*{6}
\subsection*{a}
$$\mathcal{L}\{H(s)\} = e^{-2t}u(t)$$
\subsection*{b}
$s = \sigma + j\omega$

Fourier transform is identical to Laplace at $\sigma = 0$, basically a special case of the Laplace transform.

\begin{align*}
    H(j\omega) &= \frac{1}{j\omega + 2}\\
    \mathcal{F}\{H(j\omega)\} &= e^{-2t} u(t)\\
\end{align*}
\end{multicols}
\end{document}


\section*{Kernels = Non-Parametric}
\subsection*{Reformulating the perceptron}
Ansatz: $w=\sum_{j=1}^n \alpha_j y_j x_j$\\
$\min \limits_{w\in\mathbb{R}^d} \sum_{i=1}^n \max [0, -y_i w^T x_i]$\\
%$= \min \limits_{\alpha_{1:n}} \sum_{i=1}^n \max [0,-y_i  ( \sum_{j=1}^n \alpha_j y_j x_j  )^T x_i ]$\\
$= \min \limits_{\alpha_{1:n}} \sum_{i=1}^n \max  [0,- \sum_{j=1}^n \alpha_j y_i y_j x_i^T x_j ]$

%\subsection*{Polynomial kernel}
%$k(x,y) = (x^Ty)^m$  all monomials of deg. m \\
%$k(x,y) = (1+x^Ty)^m$ all monomials up to deg. m

\subsection*{Kernelized Perceptron}
1. Initialize $\alpha_1 = ... = \alpha_n = 0$\\
2. For $t = 1, 2, ...$ do \\
Pick data $(x_i,y_i) \in_{u.a.r} D$\\
Predict $\hat{y} = sign(\sum_{j=1}^n \alpha_j y_j k(x_j,x_i))$\\
If $\hat{y} \not = y_i$ set $\alpha_i = \alpha_i + \eta_t$\\
Predict new point x: $\hat{y} = sign(\sum_{j=1}^n \alpha_j y_j k(x_j,x))$

\subsection*{Properties of a Kernel}
k must be a function: $f: X \times X \rightarrow R$\\
k must be symmetric: $k(x,y) = k(y,x)$\\
Matrix K must be positive semi-definite (psd).

\subsection*{Kernel Matrix K}
$K =
\begin{bmatrix}
	k(x_1,x_1) & \dots & k(x_1,x_n) \\
	\vdots & \ddots & \vdots \\
	k(x_n, x_1) & \dots & k(x_n,x_n)
\end{bmatrix}$\\
positive semi-definite matrices $\Leftrightarrow$ kernels
%$\left ( XX^T \right )$ for inner product as kernel.

\subsection*{Definition of PSD}
$M \in \mathbb{R}^{n\times n}$ is psd $\Leftrightarrow$\\
$\forall x \in \mathbb{R}^n: x^TMx \geq 0 \Leftrightarrow$\\
all eigenvalues of $M$ are positive: $\lambda_i\geq 0$

\subsection*{Nearest Neighbor k-NN}
$y=sign(\sum_{i=1}^n y_i [x_i \text{ among k nn of } x])$

\subsection*{Examples of kernels on $\mathbb{R}^d$}
Linear kernel: $k(x,y)=x^T y$\\
Polynomial kernel: $k(x,y)=(x^T y + 1)^d$\\
Gaussian kernel: $k(x,y) = exp(-||x-y||_2^2/h^2)$\\
Laplacian kernel: $k(x,y) = exp(-||x-y||_1/h)$\\
h = bandwidth $\approx 1\sigma$

\subsection*{Kernel Properties / Rules}
$k_1(x,y) + k_2(x,y)$; $k_1(x,y) \cdot k_2(x,y)$; $c \cdot k_1(x,y)$,$c>0$;\\
$f(k_1(x,y))$, where $f$ is a polyinomial with pos. coeffs. or the exponential function

%without Parametric vs. Nonparametric
\iffalse
\subsection*{Parametric vs. Nonparametric}
\emph{Parametric}: have finite set of parameters\\
E.g. linear regression, linear perceptron,...\\
%$f(x) = w^Tx, w\in \mathbb{R}^d$ (d is independent of \# data)\\
\emph{Nonparametric}: grow in complexity with the size of the data\\
E.g. kernelized Perceptron, k-NN,...
%$f(x) = \sum_{i=1}^n \alpha_i y_i k(x_i,x_n)$ (depends on \# data)\\
\fi

\subsection*{Perceptron and SVM}
Perceptron: $\underset{\alpha}{min}\sum_{i=1}^n max\{0,-y_i \alpha^T k_i\}$\\
SVM: $k_i=[y_1 k(x_i,x_1), ..., y_n k(x_i,x_n)]$:\\
$\underset{\alpha}{min}\sum_{i=1}^n max\{0,1-y_i \alpha^T k_i\} +\lambda\alpha^T D_y K D_y \alpha$\\
Prediction: $y = sign(\sum_{j=1}^n \alpha_j y_j k(x_j,x))$

\subsection*{Kernelized linear regression}
Ansatz: $w^*=\sum_i \alpha_i x$\\
Parametric: $w^* = \underset{w}{\operatorname{argmin}} \sum_i (w^Tx_i-y_i)^2 + \lambda ||w||_2^2$\\
%$= \underset{\alpha_{1:n}}{\operatorname{argmin}} \sum \limits_{i=1}^n (\sum \limits_{j=1}^n \alpha_j x_j^T x_i - y_i)^2 + \lambda \sum \limits_i \sum \limits_j \alpha_i \alpha_j (x_i^T x_j)$\\
%$= \underset{\alpha_{1:n}}{\operatorname{argmin}} \sum \limits_{i=1}^n (\alpha^T K_i - y_i)^2 + \lambda \alpha^T K \alpha$\\
$= \underset{\alpha}{\operatorname{argmin}} ||\alpha^T K -y||_2^2 + \lambda \alpha^T K \alpha$\\
Closed form: $\alpha^* = (K+\lambda I)^{-1} y$\\
Prediction: $y = w^{*T} x = \sum \limits_{i=1}^n \alpha_i^* k(x_i,x)$


\section{Capacity Provisioning}
\label{sec:Capacity-Provisioning}

\begin{theorem}[Square-Root Staffing Rule]
\label{thm:Square-Root-Staffing-Rule}
	For any $M/M/m$ with high resource requirement $R$, the minimum number of servers $m_{\alpha}^{*}$ to ensure $P_{Q} < \alpha$ is

	\begin{equation}
	\label{eqn:Square-Root-Staffing-Rule}
	m_{\alpha}^{*} \approx R + c \sqrt{R}
	\end{equation}

	with $c$ solution of the equation

	\begin{equation}
	\label{eqn:Square-Root-Staffing-Rule-c}
		\frac{c \Phi(c)}{\phi(c)} = \frac{1 - \alpha}{\alpha}
	\end{equation}

	where
	$\Phi(\cdot)$ is the c.d.f. of the Standard Normal, and
	$\phi(\cdot)$ is its p.d.f.

	\begin{proof}
		For a formal demonstration, see \cite{harchol2013performance}.
	\end{proof}
\end{theorem}


\subsection{Function oscillation}\label{subsec:function_oscillation}

\begin{definition}\label{def:function_oscillation}
  Let \( \mscrX \) be a nonempty set and \( (\mscrY, \rho_{\mscrY}) \) be a metric space. We define the \term{oscillation} of a function on a set as
  \begin{balign*}
     &\omega: \fun(\mscrX, \mscrY) \times \pow(\mscrX) \to [0, \infty] \\
     &\omega(f, A) \coloneqq \sup \Big\{ \rho_{\mscrY}(f(x), f(y)) \colon (x, y) \in A \Big\}.
  \end{balign*}

  In particular, if \( \mscrX \) is itself a metric space, we define its \term{modulus of continuity} \( \omega(f, \delta) \) as the oscillation of \( f \) on the ball \( B(0, \delta) \).
\end{definition}

\begin{proposition}\label{thm:def:function_oscillation/properties}
  The \hyperref[def:function_oscillation]{modulus of continuity} has the following basic properties:
  \begin{thmenum}
    \thmitem{thm:def:function_oscillation/properties/continuity_condition} \( f \) is globally \hyperref[def:uniform_continuity]{uniformly continuous} if and only if for every \( \varepsilon > 0 \) there exists \( \delta > 0 \) such that \( \omega(f, \delta) < \varepsilon \).

    \thmitem{thm:def:function_oscillation/properties/monotone} \( \omega(f, \delta) \) is monotone in \( \delta \).

    \thmitem{thm:def:function_oscillation/properties/cauchy_inequality}\mcite[28]{Николов2020Лекции}For all \( \lambda, \delta > 0 \), we have the following analog of \fullref{thm:cauchy_bunyakovsky_schwarz_inequality}
    \begin{equation}\label{thm:def:function_oscillation/properties/cauchy_inequality/inequality}
      \omega(f, \lambda \delta) \leq \omega(f, \lambda^2) + \omega(f, \delta^2).
    \end{equation}

    \thmitem{thm:def:function_oscillation/properties/single_inequality}\mcite[28]{Николов2020Лекции}For all \( \lambda, \delta > 0 \),
    \begin{equation}\label{thm:def:function_oscillation/properties/single_inequality/inequality}
      \omega(f, \lambda \delta) \leq (\lambda + 1) \omega(f, \delta).
    \end{equation}
  \end{thmenum}
\end{proposition}
\begin{proof}
  \SubProofOf{thm:def:function_oscillation/properties/continuity_condition} Follows directly from \fullref{def:uniform_continuity}.

  \SubProofOf{thm:def:function_oscillation/properties/monotone} A supremum on a larger set is larger.

  \SubProofOf{thm:def:function_oscillation/properties/cauchy_inequality} If \( \lambda \leq \delta \), clearly \( \lambda \delta \leq \delta^2 \). Otherwise, \( \lambda \delta < \lambda^2 \).

  Combining the two inequalities with \fullref{thm:def:function_oscillation/properties/monotone}, we obtain \fullref{thm:def:function_oscillation/properties/cauchy_inequality/inequality}.

  \SubProofOf{thm:def:function_oscillation/properties/single_inequality} Note that
  \begin{equation*}
    \rho_{\mscrX}(x, y) < \delta \T{implies} \rho_{\mscrY}(f(x), f(y)) < \omega(f, \delta).
  \end{equation*}

  We can multiply this by \( \lambda \) to obtain
  \begin{equation*}
    \lambda \rho_{\mscrX}(x, y) < \lambda \delta \T{implies} \lambda \rho_{\mscrY}(f(x), f(y)) < \lambda \omega(f, \delta).
  \end{equation*}

  If \( \lambda \geq 1 \), then \( \rho_{\mscrX}(x, y) \leq \lambda \rho_{\mscrX}(x, y) \) and \( \rho_{\mscrY}(f(x), f(y)) \leq \lambda \rho_{\mscrY}(f(x), f(y)) \) and hence
  \begin{equation*}
    \omega(f, \lambda \delta) \leq \lambda \omega(f, \delta).
  \end{equation*}

  Otherwise, \( \lambda < 1 \) and clearly \( \lambda \delta < \delta \), which by \fullref{thm:def:function_oscillation/properties/monotone} implies
  \begin{equation*}
    \omega(f, \lambda \delta) \leq \omega(f, \delta).
  \end{equation*}

  Combining the two cases, we obtain
  \begin{equation*}
    \omega(f, \lambda \delta) \leq \lambda \omega(f, \delta) + \omega(f, \delta),
  \end{equation*}
  which we wanted to prove.
\end{proof}


\subsection{Array Functions}

Arrays could be directly treated using array functions. Since array is a very usable form of holding numbers and strings, it's good for you to know what they could do. Here is the list.

\begin{shaded}\begin{indentCom}
\item \textbf{Array.concat(array1,array2)} Returns a new array created by
joining two or more arrays or values.
\item \textbf{Array.copy(array)} Returns a copy of array.
\item \textbf{Array.fill(array, value)} Assigns the specified numeric value to
each element of array.
\item \textbf{Array.findMaxima(array, tolerance)} Returns an array holding the peak positions (sorted with descending strength). Tolerance is the minimum amplitude difference to needed to separate two peaks. There is an optional 'excludeOnEdges' argument that defaults to 'true'. Examples. Requires 1.48c.
\item \textbf{Array.findMinima(array, tolerance)} Returns an array holding the minima positions. Requires 1.48c.
\item \textbf{Array.fourier(array, windowType)} Calculates and returns the Fourier amplitudes of array. WindowType can be "none", "Hamming", "Hann", or "flat-top", or may be omitted (meaning "none"). See the TestArrayFourier macro for an example and more documentation. Requires 1.49i.
\item \textbf{Array.getStatistics(array, min, max, mean, stdDev)} Returns the
min, max, mean, and stdDev of array, which must contain all numbers.
\item \textbf{Array.print(array)} Prints the array on a single line.
\item \textbf{Array.rankPositions(array)} Returns, as an array, the rank
positions of array, which must contain all numbers or all strings.
\item \textbf{Array.resample(array,len)} Returns an array which is linearly resampled to a different length. Requires 1.47j.
\item \textbf{Array.reverse(array)} Reverses (inverts) the order of the
elements in array.
\item \textbf{Array.show(array)} Displays the contents of array in a window. Requires 1.48d.
\item \textbf{Array.show("title", array1, array2, ...)} Displays one or more arrays in a Results window (examples). If title (optional) is "Results", the window will be the active Results window, otherwise, it will be a dormant Results window (see also IJ.renameResults). If title ends with "(indexes)", a 0-based Index column is shown. If title ends with "(row numbers)", the row number column is shown. Requires 1.48d.
\item \textbf{Array.slice(array,start,end)} Extracts a part of an array and
returns it.
\item \textbf{Array.sort(array)} Sorts array, which must contain all numbers
or all strings. String sorts are case-insensitive in v1.44i or later.
\item \textbf{Array.trim(array, n)} Returns an array that contains the first n
elements of array.
\end{indentCom}\end{shaded}

For example, array could be sorted and reversed. Try the following codes.

\begin{lstlisting}[numbers=none]
EMBL = newArray("Heidelberg","Hamburg","Hixton","Grenoble","Monterotondo");
Array.print(EMBL);
Array.sort(EMBL);
Array.print(EMBL);
Array.reverse(EMBL);
Array.print(EMBL);
\end{lstlisting}
The output of this code is:
\begin{lstlisting}[numbers=left]
Heidelberg,Hamburg,Hixton,Grenoble,Monterotondo
Grenoble,Hamburg,Heidelberg,Hixton,Monterotondo
Monterotondo,Hixton,Heidelberg,Hamburg,Grenoble
\end{lstlisting}
The first line is printed in the order when the array was initialized. After
sorting, names are in alphabetical order. Third line shows the reversed
elements.

Some functions return an array rather than taking array/s as argument. See Appendix for a list of those functions (\ref{subsec:arrayreturn}).


\section{Partial Recursive Functions}

\subsection{Partial Functions, Definition by Composition \& Primitive Recursion}

\begin{itemize}

	\item Classes of functions:

	\begin{itemize}

		\item Let $ P $ be the set of partial functions, $ P = \setcomp{f}{f \text{ is a partial function } \Nat^n \to \Nat \text{ for some } n > 0} $

		\item Let $ T $ be the set of total functions, $ T = \setcomp{f \in P}{f \text{ is total}} $

		\item A \textit{class} of functions means a subset of $ P $, and a class of total functions means a subset of $ T $

		\item Goal: build a class of functions which we might call `computable'

	\end{itemize}

	\item Let $ g: \Nat^r \to \Nat, h_1 \dots h_r: \Nat^n \to \Nat $ be partial functions.

	Then the partial function $ f: \Nat^n \to \Nat $ obtained from $ g, h_1, \dots, h_r $ by composition is defined by:
	\begin{equation*}
	f(\vec{x}) = g(h_1(\vec{x}), \dots, h_r(\vec{x}))
	\end{equation*}
	\begin{itemize}
		\item We write $f = g \circ (h_1, \dots, h_r) $
	\end{itemize}

	\item Let $ g: \Nat^n \to \Nat, h: \Nat^{n+2} \to \Nat $ be partial functions.

	Then the partial function $ f: \Nat^{n+1} \to \Nat $ obtained from $ g $ and $ h $ by primitive recursion is defined by:
	\begin{align*}
	&f(\vec{x}, 0) = g(\vec{x})\\
	&f(\vec{x}, y + 1) = h(\vec{x}, y, f(\vec{x}, y))
	\end{align*}
	\begin{itemize}
		\item For a given $ \vec{x} $, $ f(\vec{x}, y) $ is defined for no $ y $, for all $ y $, or for $ 0 \le y \le r $ for some $ r \in \Nat $
		\item Where the `counter' parameter is placed does not matter - it could equally be at the start
	\end{itemize}

\end{itemize}

\subsection{Primitive Recursive Functions}

\begin{itemize}

	\item We define the \textit{initial functions} to be the following functions:

	\begin{itemize}

		\item The zero function $ z: \Nat \to \Nat $, such that $ z(x) = 0 $ for all $ x \in \Nat $

		\item The successor function $ \sigma: \Nat \to \Nat $, such that $ \sigma(x) = x + 1 $ for all $ x \in \Nat $

		\item The projection functions $ \pi_{i, n}: \Nat^n \to \Nat $, where for $ n \ge 1 $ and $ 1 \le i \le n $, $ \pi_{i, n}(x_1, \dots, x_n) = x_i $

	\end{itemize}

	\item A class $ \Class $ of total functions is \textit{primitively recursively closed} if:

	\begin{itemize}

		\item $ \Class $ contains all the initial functions

		\item $ \Class $ is closed under composition

		\item $ \Class $ is closed under primitive recursion

	\end{itemize}

	\item The smallest primitively recursively closed class (i.e. the intersection of all prim. rec. closed classes) is called \textit{the class of primitive recursive functions}

	\item Example: addition function $ S: \Nat^2 \to \Nat $, such that $ S(x, y) = x + y $
	\begin{align*}
	S(x, 0) &= g(x), g = \pi_{1, 1}\\
	S(x, y  + 1) &= S(x, y) + 1\\
				 &= \sigma(S(x, y))\\
				 &= h(x, y, S(x, y)), h = \sigma \circ \pi_{3, 3}
	\end{align*}

	\newpage

	\item Useful tips for showing a function is in a primitively recursively closed class $ \Class $:

	\begin{itemize}

		\item Given $ f: \Nat^n \to \Nat $ is in $ \Class $

		If $ g: \Nat^m \to \Nat $ is defined by $ g(x_1, \dots, x_m) = f(y_1, \dots, y_n) $ where each $ y_i $ is either a constant or $ x_j $ for some $ j $, then $ g \in \Class $ -- lets you manipulate arity

		\item To show a unary function $ f: \Nat \to \Nat $ is in $ \Class $ by primitive recursion, define $ f': \Nat^2 \to \Nat $ such that $ f'(x, y) = f(y) $; then, if $ f' $ can be shown to be in $ \Class $, $ f $ will be also

		\item Let $ a \in \Nat $ and $ h: \Nat^2 \to \Nat $ be in $ \Class $

		Then, for $ f: \Nat \to \Nat $, if $ f(0) = a $ and $ f(y + 1) = h(y, f(y)) $, $ f \in \Class $

	\end{itemize}

	\item A \textit{primitive recursive definition} of $ f: \Nat^n \to \Nat $ is a finite sequence of functions $ f_0, f_1, \dots, f_k = f $, where for each $ i $:

	\begin{itemize}

		\item $ f_i $ is initial; or

		\item $ f_i $ is obtained from composition of some functions $ f_j $, $ j < i $; or

		\item $ f_i $ is obtained by primitive recursion from two of $ f_j $, $ j < i $

	\end{itemize}

	\item Example: addition function $ S $ can be defined by $ \pi_{1, 1}, \pi_{3, 3}, \sigma, \sigma \circ \pi_{3, 3} $

	\item The class $ \Class_1 $ of primitive recursive functions is the same as the class $ \Class_2 $ of functions that have a primitive recursive definition (seems trivial, but isn't!)

	Prove by showing $ \Class_1 \subseteq \Class_2 $ (i.e. $ \Class_2 $ is prim. rec. closed) and that $ \Class_2 \subseteq \Class_1 $ (i.e. $ \Class_2 $ is contained in any prim. rec. closed class)

	\item Let $ \Class $ be a prim. rec. closed class, and let $ g: \Nat^{n+1} \to \Nat $ be in $ \Class $; then the functions $ f_1: \Nat^{n+1} \to \Nat $ and $ f_2: \Nat^{n+1} \to \Nat $ defined by:

	$ f_1(\vec{x}, y) = \sum_{t=0}^{y} g(\vec{x}, t) $

	$ f_2(\vec{x}, y) = \prod_{t=0}^{y} g(\vec{x}, t) $

	are also in $ \Class $

	\item Useful prim. rec. functions:

	\begin{itemize}
		\item Proper subtraction $ x \monus y = max\set{x - y, 0} $

		\item Sign $ sg(x) = \begin{cases}
		0 \text{ if } x = 0\\
		1 \text{ if } x \ge 0
		\end{cases} $

	\end{itemize}

\end{itemize}

\newpage

\subsection{Predicates}

\begin{itemize}
	\item A predicate $ P(x_1, \dots, x_n) $ of $ n $ variables is a statement concerning $ x_i \in \Nat$ which is either true or false

	\item We can identify $ P $ with the set $ A_P = \setcomp{\vec{x} \in \Nat^n}{P(\vec{x}) \text{ is true}} $

	\subitem E.g. $ P(x, y) $ means ``$ x $ divides $ y $", so $ A_P = \set{(1, 6), (2, 6), (3, 6), (6, 6), (1, 3) \dots} $

	\item The \textit{characteristic function} of a set $ \chi_A: \Nat^n \to \set{0, 1} $ of $ A \subseteq \Nat^n $ is defined by:
	\begin{equation*}
	\chi_A(\vec{x}) = \begin{cases}
	1 \text{ if } \vec{x} \in A\\
	0 \text{ if } \vec{x} \notin A
	\end{cases}
	\end{equation*}
	\item For a predicate $ P $, we define $ \chi_P $ to be $ \chi_{A_P} $

	\item Let $ \Class $ be a prim. rec. closed class; then a subset $ A \subseteq \Nat^n $ is in $ \Class $ if $ \chi_A \in \Class $

	\subitem So a predicate $ P $ of $ n $ variables is in $ \Class $ if $ \chi_P \in \Class $

	\item If $ A, B \subseteq \Nat^n $ are in $ \Class $, then $ A \cup B $, $ A \cap B $ and $ \Nat^n \setminus A $ are in $ C $

	\subitem So if $ P, Q $ are predicates of $ n $ variables in $ \Class $, $ P \lor Q $, $ P \land Q $ and $ \lnot P $ are in $ \Class $

	\subitem Proof: $ \chi_{A \cup B}(x) = sg(\chi_A(x) + \chi_B(x)) $, ~~~ $ \chi_{A \cap B} = \chi_A(x) \cdot \chi_B(x) $, ~~~ $ \chi_{\Nat^n \setminus A}(x) = 1 \monus \chi_A(x) $

	\item The predicates $ x = y $, $ x \ne y $, $ x \le y $, $ x < y $, $ x \ge y $, $ x > y $ are prim. rec.

	\subitem Proof: Note that $ \chi_{\ne} (x, y) = sg(\abs{x - y}) $ and $ \chi_{>} (x, y) = sg(x \monus y) $

	\item Bounded quantifiers:

	Assume $ P $ is a pred. of $ n + 1 $ variables in $ \Class $; then $ Q, R $ of $ n + 1 $ variables defined below are in $ \Class $:

	$ Q(x_1, \dots x_n, z) $ is true if and only if $ \exists_{y \le z} (P(x_1, \dots, x_n, y) \text{ is true})$

	$ R(x_1, \dots x_n, z) $ is true if and only if $ \forall_{y \le z} (P(x_1, \dots, x_n, y) \text{ is true})$

	\subitem Proof: $ \chi_Q (\vec{x}, z) = sg(\sum_{y=0}^{z} \chi_P(\vec{x}, y)) $, and $ \chi_R (\vec{x}, z) = \prod_{y=0}^{z} \chi_P(\vec{x}, y) $

\end{itemize}

\subsection{More Primitive Recursive Functions}

\subsubsection{Bounded Minimisation}

Let $ P $ be a pred. of $ n + 1 $ variables. Define $ f: \Nat^{n+1} \to \Nat $ by:
\begin{equation*}
f(\vec{x}, z) = \begin{cases}
\text{the least } y \le z \text{ s.t. } P(\vec{x}, y) \text{ is true}\\
z + 1 \text{ if no such } y \text{ exists}
\end{cases}
\end{equation*}
Then, $ f(\vec{x}, z) = \mu ~ {y \le z} ~ P(\vec{x}, y) $, called \textit{bounded minimisation}. We have that if $ P \in \Class$ (a prim. rec. closed class), then $ f $ is in $ \Class $.

\begin{proof}

Define $ g(\vec{x}, t) = \prod_{y=0}^{t} sg(1 \monus \chi_P(\vec{x}, y) \text{ is true})$. Note that $ g(\vec{x}, t) =  \begin{cases}
0 \text{ if } \exists_{y \le t} P(x, y) \text{ is true}\\
1 \text{ if } \forall_{y \le t} P(x, y) \text{ is false}
\end{cases} $

Let $ y \le z $ be the least s.t. $ P(\vec{x}, y) $ is true.

Then the values of $ g $ look like: \begin{tabular}{r | c c c c c c c c}
	$ t $ & 0 & 1 & $ \dots $ & $ y - 1 $ & $ y $ & $ y + 1 $ & $ \dots $ & $ z $ \\ \hline
	$ g(\vec{x}, t) $ & 1 & 1 & $ \dots $ & 1 & 0 & 0 & $ \dots $ & 0
\end{tabular}

Let $ f(\vec{x}, z) = \sum_{t=0}^{z} g(\vec{x}, t) $, then we will have $ f $ as required for bounded minimisation. If there is no such $ y $, then by the definition of $ g $ we would have $ f(\vec{x}, z) = z + 1 $

\end{proof}

\subsubsection{Definition By Cases}

Let $ f_1, \dots, f_k : \Nat^n \to \Nat $ be in  prim. rec. closed $ \Class $ and let $ P_1, \dots, P_k $ be predicates in $ \Class $ of $ n $ variables. Suppose that for each $ \vec{x} \in \Nat^n $ \textit{exactly} one of $ P_1(\vec{x}), \dots, P_k(\vec{x}) $ is true. Define $ f: \Nat^n \to \Nat $ by:
\begin{equation*}
f(\vec{x}) = f_i(\vec{x}) \text{ if } P_i(\vec{x}) \text{ is true}
\end{equation*}
Then $ f $ is in $ \Class $.

\begin{proof}

$ f(\vec{x}) = f_1(\vec{x}) \cdot \chi_{P_1}(\vec{x}) + \dots + f_k(\vec{x}) \cdot \chi_{P_k}(\vec{x}) $

\end{proof}

\subsubsection{Iteration}

Let $ X $ be a set, with a partial function $ f: X \to X $. The \textit{iterate} of $ f $ is the partial function $ F: X \times \Nat \to X $ defined by:
\begin{align*}
&F(x, 0) = x\\
&F(x, n + 1) = f(F(x, n))
\end{align*}
We have a notion of a function $ f: \Nat^n \to \Nat $ being in a class $ \Class $. This can be extended to functions $ f: \Nat^n \to \Nat^k $ by saying that $ f $ is in $ \Class $ if $ \pi_{i, k} \circ f $ is in $ \Class $ for each $ 1 \le i \le k $.

A class $ \Class $ is closed under iteration if, whenever $ f: \Nat^n \to \Nat^n $ is in $ \Class $, then its iterate $ F: \Nat^{n+1} \to \Nat^n $ is in $ \Class $.

Let $ \Class $ be a prim. rec. closed class. Then if $ f: \Nat^n \to \Nat^n $ is in $ \Class $, its iterate $ F: \Nat^{n+1} \to \Nat^n $ is also in $ \Class $. So any prim. rec. closed class is closed under iteration.

\begin{proof}

This shows only the $ n = 1 $ case.

Define $ f': \Nat^3 \to \Nat $ by $ f'(x, y, z) = f(z) $, which is in $ \Class $.

Then the iterate of $ f $ is defined by:

\begin{align*}
&F(z, 0) = z\\
&F(z, y + 1) = f(F(z, y)) = f'(x, y, F(x, y))
\end{align*}

This is defined by primitive recursion, so $ F $ is in $ \Class $.

\end{proof}

\newpage

\subsection{Recursive and Partial Recursive Functions}

\subsubsection{Minimisation}

Let $ f: \Nat^{n+1} \to \Nat $ be a partial function. The function obtained from $ f $ by \textit{minimisation} is the partial function $ g: \Nat^n \to \Nat $ defined by
\begin{equation*}
g(\vec{x}) = \begin{cases}
r &\text{ if } f(\vec{x}, r) = 0 \text{ and for } s < r, f(\vec{x}, s) \text{ is defined and not 0}\\
\textit{undefined} &\text{ otherwise}
\end{cases}
\end{equation*}
We write $ g(\vec{x}) = \mu y (f(\vec{x}, y) = 0) $. It is also called the $ \mu $-operator or unbounded search operator. The function $ g $ may be partial, even if $ f $ is total, and vice versa.

Note that it is not \textit{quite} accurate to say $ g(x) = \mu y (f(\vec{x}, y) = 0) $ is the least $ y $ s.t. $ f(x, y) = 0 $; if there is some least $ y $ s.t. $ f(x, y) = 0 $, but $ f(x, s) $ is undefined for some $ s < y $, then $ g(x) $ is undefined.

\subsubsection{The Class of Recursive Functions}

\begin{itemize}

	\item A total function $ f(\vec{x}, y) $ is \textit{regular} if for any $ \vec{x} \in \Nat^n $, there exists $ y \in \Nat $ such that $ f(\vec{x}, y) = 0 $

	\item The regular functions are exactly those to which we can apply minimisation and end up with a total function

	\item The function $ g $ is obtained from $ f $ by \textit{regular minimisation} if $ g(\vec{x}) = \mu y (f(\vec{x}, y) = 0) $ where $ f $ is regular

	\item The \textit{class of recursive functions} is the smallest class $ \Class $ of total functions which is primitively recursively closed and is closed under regular minimisation

	\item Note that there are recursive functions which are \textit{not} primitive recursive

	\item Example: the two-argument Ackermann function defined by:
	\begin{equation*}
	A(m, n) = \begin{cases}
	n + 1 &\text{ if } m = 0\\
	A(m - 1, 1) &\text{ if } m > 0 \text{ and } n = 0\\
	A(m - 1, A(m, n - 1)) &\text{ if } m > 0 \text{ and } n > 0
	\end{cases}
	\end{equation*}

	\item The \textit{class of partial recursive functions} is the smallest class of partial functions which contains the initial functions, and is closed under composition, primitive recursion and minimisation

	\begin{itemize}
		\item Note that this is \textit{not} a primitively recursively closed class -- that term only applies to a class of total functions
	\end{itemize}

\end{itemize}


\section{Machine Learning Theory (Nonlinear Separators)}
Here we being by considering what the notion of a `God-given' distribution that we are trying to learn really is in the context of classification. So let $X = \mathbb{R}^d$ denote the space of features (i.e. the image space) and $Y = \{1,...,k\}$ denote the set of labels. The fundamental object whose existence we must assume is a probability distribution over the space $X\times Y$. Below we give a few equivalent ways of describing such a probability distribution.

\begin{lemma}
 The three objects following are equivalent (note that we always consider the Borel $\sigma$-algebra when defining our notion of measure):
 \begin{itemize}
  \item A probability measure $\pi$ on $X\times Y$
  \item A probability measure on $Y$, defined by probabilities $p_1,...,p_k$ with $0\leq p_i\leq 1$ and $\sum_{i=1}^k p_i = 1$, combined with $k$ \textit{conditional} probability measures $\pi_i$ on $X$, representing the conditional distribution given the $i$-th label
  \item A probability measure on $\pi_X$ on $X$, combined with a family of conditional distributions on the labels, defined by $p_i(x)$ for each $i = 1,...,k$ and $x\in X$, which must satify $0\leq p_i(x)\leq 1$ and $\sum_{i=1}^k p_i(x) = 1$.
 \end{itemize}

\end{lemma}

\begin{proof}
 We will show how to get a probability measure on $X\times Y$ from each of the other two objects, the reverse is more technical. Let $A\subset X\times Y$. We define
 \begin{equation}
  \pi(A) = \sum_{i=1}^k p_i\pi_i(A\cap (X\times \{i\}))
 \end{equation}
 and
 \begin{equation}
  \pi(A) = \int_X \sum_{i:(x,i)\in A} p_i(x)d\pi_X
 \end{equation}

\end{proof}

We call a distribution non-linearly separable if the label is uniquely determined by image.
\begin{definition}
 A distribution is called non-linearly separable if the following two equivalent conditions hold
 \begin{itemize}
  \item $\pi_i$ and $\pi_j$ have disjoint support for $i\neq j$
  \item $p_i(x)$ is either equal to $0$ or $1$ for every $x\in X$
 \end{itemize}

\end{definition}

We have the following lemma
\begin{lemma}
 If $\pi$ is non-linearly separable, then there exists a classification function $f_c:X\rightarrow Y$ such that
 \begin{equation}
  \mathbb{P}_{(x,y)\sim \pi}(f_c(x) = y) = 1.
 \end{equation}
 Equivalently, the true risk
 \begin{equation}
  R(f_c) = \mathbb{E}_{(x,y)\sim\pi}(\chi_{f_c(x) \neq y})
 \end{equation}
 is equal to $0$.

\end{lemma}

More generally, the labels may not be uniquely determined by the features. In this more general situation the true risk above may not be $0$. Consequently, we consider a situation where no label can be ruled out for any $x\in X$, which probably corresponds more closely to a realistic situation.

\begin{definition}
 A distribution $\pi$ is completely label-ambiguous if
 $p_i(x) > 0$ for every $i$ and every $x\in X$.
\end{definition}

\begin{lemma}
 If $\pi$ is label-ambiguous, the probability distributions $p_i(x)$ can be written as
 \begin{equation}
  \{p_i(x)\} = \text{softmax}(\Psi(x))
 \end{equation}
 for some function $\Psi:X\rightarrow \mathbb{R}^k$.
\end{lemma}

Note that the function $\Psi$ completely contains exactly the conditional probabilities over the labels given the images and it is this function which we are usually interested in learning (i.e. we don't care about the image distribution $\pi_X$).


\section{Multivariate Calculus}
In many real world applications, functions we might be interested in can involve more than one independent variable.
\begin{example}
    Waves along a string.
    Let $f(x,t)$ be the displacement where $x$ be the position and $t$ be the time.
    The shape of the wave (represented by $f(x,t_0)$ where $t_0$ is fixed) is dependent on time.
\end{example}
How do we define the derivative when a function depends on more than one variable?
Suppose that $f(x,y)$ is the elevation of the terrain at the specific location $x,y$.
We can draw a contour map of its projection on a plane.
If we are interested in the steepness at a point $a$ on the surface, one should note that different trails going though $a$ may have different steepness.
So the general point is that the slope of a function at a given point depends on the direction.
\subsection{Partial Derivative}
We want to find the derivative of a multivariate function with respect to one variable while keeping others fixed.
\begin{definition}
    Mathematically speaking, we define partial derivative $f(x,y)$ with respect to $x$ fixing $y$ is the limit
    $$\left.\frac{\partial f}{\partial x}\right|_y=\lim_{h\to0}\frac{f(x+h,y)-f(x,y)}{h}$$
    provided that it exists.
    We can define the partial derivative with respect to $y$ fixing $x$ similarly.
\end{definition}
\begin{example}
    Let $F(x,y)=x^2+y^3+e^{xy^2}$, so
    $$\left.\frac{\partial f}{\partial x}\right|_y=2x+y^2e^{xy^2},\left.\frac{\partial f}{\partial y}\right|_x=3y^2+2xye^{xy^2}$$
\end{example}
We can do partial derivatives recursively as well.
$$\left.\frac{\partial^2 f}{\partial x^2}\right|_y=2+y^4e^{xy^2}$$
Now we can define cross-derivative as well,
$$\left.\frac{\partial}{\partial y}\left(\left.\frac{\partial f}{\partial x}\right|_y\right)\right|_x=2ye^{xy^2}+2xy^3e^{xy^2}$$
Since the notation is cumbersome, we sometimes omit the symbol $|_y$.\\
There is a symmetry involved in mixed partial derivatives.
By that we mean
$$\frac{\partial^2 f}{\partial x\partial y}=\frac{\partial^2 f}{\partial y\partial x}$$
given that all these partial derivatives exist.
Some properties are required for this equality to hold, but they are out of the scope of this course.\\
On higher dimensions (where we can define partial derivatives analogously), for example $f(x,y,z)$, when we sometimes say
$$\left.\frac{\partial f}{\partial x}\right|_y$$
its value would depend on the path it takes in the $x-z$ plane.\\
We sometimes use the shorthand notation $f_x,f_{xy},f_{xx}$ for the partial derivatives.
\subsection{The Chain Rule on Higher Dimensions}
Consider $f(x(t),y(t))$, we first want to have the concept of a differential of a function.
$$\delta f=f(x+\delta x, y+\delta y)-f(x,y)$$
So we have
$$\delta f=f(x+\delta x, y+\delta y)-f(x+\delta x,y)+f(x+\delta x,y)-f(x,y)$$
When $y$ is held constant, $f(x+\delta x,y)=f(x,y)+\delta x(\partial f/\partial x)(x,y)+o(\delta x)$ as $\delta x\to0$.
Similarly $f(x+\delta x,y+\delta y)=f(x,y)+\delta y(\partial f/\partial y)(x+\delta x,y)+o(\delta y)$ as $\delta y\to0$.
Therefore as $\delta x\to 0,\delta y\to0$
\begin{align*}
    \delta f
    &=f(x+\delta x,y)+\delta y\frac{\partial f}{\partial y}(x+\delta x,y)+o(\delta y)-f(x+\delta x,y)\\
    &\quad+f(x,y)+\delta x\frac{\partial f}{\partial x}(x,y)+o(\delta x)-f(x,y)\\
    &=\delta y\frac{\partial f}{\partial y}(x+\delta x,y)+\delta x\frac{\partial f}{\partial x}(x,y)+o(\delta x)+o(\delta y)\\
    &=\delta y\frac{\partial f}{\partial y}(x,y)+\delta x\frac{\partial f}{\partial x}(x,y)\\
    &\quad+\delta x\delta y\frac{\partial}{\partial x}\frac{\partial f}{\partial y}(x,y)+o(\delta x)+o(\delta y)+o(\delta x\delta y)
\end{align*}
Note that $o(\delta x\delta y)+o(\delta x)=o(\delta x)$ as $\delta y\to0$, so we can ignore that term.\\
We let $\delta x,\delta y\to 0$, so
\begin{align*}
    \delta f&=\delta y\frac{\partial f}{\partial y}+\delta x\frac{\partial f}{\partial x}+\delta x\delta y\frac{\partial}{\partial x}\frac{\partial f}{\partial y}+o(\delta x)+o(\delta y)\\
    \mathrm df&=\frac{\partial f}{\partial y}\mathrm dy+\frac{\partial f}{\partial x}\mathrm dx
\end{align*}
This is called the chain rule in differential form.
We can obtain the chain rule by dividing by another differential $\mathrm dt$ before applying the limit.
$$\frac{\mathrm df}{\mathrm dt}=\frac{\partial f}{\partial y}\frac{\mathrm dy}{\mathrm dt}+\frac{\partial f}{\partial x}\frac{\mathrm dx}{\mathrm dt}$$
This is called the multivariate chain rule.\\
Suppose $f(x,y(x))$, then
$$\frac{\mathrm df}{\mathrm dx}=\frac{\partial f}{\partial x}+\frac{\partial f}{\partial y}\frac{\mathrm dy}{\mathrm dx}$$
We can integrate it back
$$\int\mathrm df=\int\frac{\partial f}{\partial x}\,\mathrm dx+\int\frac{\partial f}{\partial y}\,\mathrm dy$$
Note that we need to integrate the above equation along a given path, but if the function is nice enough, only the endpoints matter.
\begin{example}
    We choose the paths
    $$(x_1,y_1)\to(x_2,y_1)\to(x_2,y_2)$$
    and
    $$(x_1,y_1)\to(x_1,y_2)\to(x_2,y_2)$$
    then
    $$f(x_2,y_2)-f(x_1,y_1)=\int_{x_1}^{x_2}\frac{\partial f}{\partial x}(x,y_1)\,\mathrm dx+\int_{y_1}^{y_2}\frac{\partial f}{\partial y}(x_2,y)\,\mathrm dy$$
    $$=\int_{x_1}^{x_2}\frac{\partial f}{\partial x}(x,y_2)\,\mathrm dx+\int_{y_1}^{y_2}\frac{\partial f}{\partial y}(x_1,y)\,\mathrm dy$$
\end{example}
An application of the multivariate chain rule is the change of variables.
It is often useful to write a differential equation in a different coordinate system before solving it.
To do this, we need to transform the derivatives from one to the other.
\begin{example}
    We try to transform from Cartesian to polar coordinate, so $x=r\cos\theta,y=r\sin\theta$, so we can write $f(x(r,\theta),y(r,\theta))$, so
    $$\left.\frac{\partial f}{\partial r}\right|_\theta=\left.\frac{\partial f}{\partial x}\right|_y\left.\frac{\partial x}{\partial r}\right|_\theta+\left.\frac{\partial f}{\partial y}\right|_x\left.\frac{\partial y}{\partial r}\right|_\theta=\left.\frac{\partial f}{\partial x}\right|_y\cos\theta+\left.\frac{\partial f}{\partial y}\right|_x\sin\theta$$
\end{example}
We can also apply it to implicit differentiation.
Consider $f(x,y,z)=c$ where $c$ is a constant.
It implicitly defined $z(x,y),x(y,z),y(z,x)$.
For example, we can take $xy+y^2z+z^5=1$, so $x=(1-z^5-y^2z)/y$ and we can find $y$ by quadratic formula, but we cannot do it easily with $z$ since it's quintic.\\
However, we can find the derivative $\partial z/\partial x$ fixing $y$ by observing
$$0=\left.\frac{\partial f}{\partial x}\right|_y=y+y^2\left.\frac{\partial z}{\partial x}\right|_y+5z^4\left.\frac{\partial z}{\partial x}\right|_y$$
which we can solve for the desired derivative.\\
Now consider $f(x,y,z(x,y))=0$,
$$\mathrm df=\left.\frac{\partial f}{\partial x}\right|_{y,z}\mathrm dx+\left.\frac{\partial f}{\partial y}\right|_{x,z}\mathrm dy+\left.\frac{\partial f}{\partial z}\right|_{x,y}\mathrm dz$$
We want the derivative of $z$ wrt $x$ with $y$ fixed, so
$$\left.\frac{\partial f}{\partial x}\right|_y=\left.\frac{\partial f}{\partial x}\right|_{y,z}+\left.\frac{\partial f}{\partial z}\right|_{x,y}\left.\frac{\partial z}{\partial x}\right|_y$$
In fact, $(\partial f/\partial x)|_y=0$.\\
The chain rule allows us to differentiate an integral.
Consider  function $f(x,c)$ where each value of $c$ gives a different function $f$.
We want to find the (partial) derivative of the integral
$$\int_0^bf(x,c)\,\mathrm dx$$
Then
\begin{align*}
    \left.\frac{\partial I(b,c)}{\partial b}\right|_c
    &=\lim_{h\to 0}\frac{1}{h}\int_b^{b+h}f(x,c)\,\mathrm dx\\
    &=f(b,c)
\end{align*}
Similarly,
\begin{align*}
    \left.\frac{\partial I(b,c)}{\partial c}\right|_b
    &=\lim_{h\to 0}\frac{1}{h}\int_0^bf(x,c+h)-f(x,c)\,\mathrm dx\\
    &=\int_0^b\left.\frac{\partial I(x,c)}{\partial c}\right|_x\,\mathrm dx
\end{align*}
Suppose that $b,c$ depends on $t$, so $I(b(t),c(t))$, so
\begin{align*}
    \frac{\mathrm dI}{\mathrm dt}
    &=\frac{\partial I}{\partial b}\frac{\mathrm db}{\mathrm dt}+\frac{\partial I}{\partial c}\frac{\mathrm dc}{\mathrm dt}\\
    &=f(b,c)\dot{b}+\dot{c}\int_0^b\left.\frac{\partial I(x,c)}{\partial c}\right|_x\,\mathrm dx
\end{align*}
In general,
$$\frac{\mathrm d}{\mathrm dt}\int_{a(t)}^{b(t)}f(x,c(t))\,\mathrm dx=\dot{c}\int_{a(t)}^{b(t)}\frac{\partial f}{\partial c}\,\mathrm dx+f(b,c)\dot{b}-f(a,c)\dot{a}$$
Note that the reciprocal rule also apply.
The same rule applies to partial derivatives given that the same parameter is kept constant.
$$\left.\frac{\partial r}{\partial x}\right|_y=\frac{1}{\partial x/\partial r|_y}$$


\documentclass{article}

\usepackage[a4paper, total={6in, 8in}]{geometry}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{braket}
\usepackage{listings}
\usepackage{mathtools}

\title{Simulation of Shor's Algorithm Report}
\author{
  José Semedo\\
  ist178294
  \and
  Rodrigo Bernardo\\
  ist178942
}

\date{}

\begin{document}
\maketitle

\section{Introduction}
In this project we studied and implemented a simulator of Shor's quantum
algorithm for integer factorization. The problem is reduced to the problem of
order finding. Although it is not known if order finding is hard in a classical
setting, Shor \cite{Shor} demonstrated that it is solvable in polynomial time if
one has access to a quantum computer, thus showing integer factorization is
solvable in polynoial time.

\section{Language and Tools}
Our simulation is implemented in Python 3, together with the numpy library.

\section{Implementation Overview}

\subsection{Memory}
The algorithm takes an odd integer $N$, such that it is not a prime nor a power
of a prime, and an integer $x, 1 < x < N$, and tries to find the multiplicative
order of $x$ modulo $N$.

It starts by allocating $t + n$ qubits, with $n = \lceil{\log_2 N}\rceil$ and
$N^2 \leq 2^t < 2N^2$, and initializes the state to $\ket{\psi_{0}} = \ket{0,
0}$.

We represent the memory by explicitly saving all the $2^{t+n}$ possible states and
their corresponding amplitudes, equivalent to the representation of the quantum
state as a linear vector combination $\ket{\psi} =
\sum_{j=0}^{t+n}a_{j}\ket{j}$.

\subsection{Hadamard Gates}
The algorithm then applies Hadamard gates to the first $t$ qubits. This creates
a quantum superposition where the amplitudes are equidistributed between the
first t bits. The state becomes $\ket{\psi_1} = H^{\otimes{t}}\ket{\psi_0} =
2^{-t/2}\sum_{j=0}^{2^t-1}\ket{j}\ket{0}$.

We simulate this step by explicitly reaching for the states where the last $n$
qubits are $\ket{0}$ and setting their amplitudes to $2^{-t/2}$.

\subsection{Modular Exponentiation}

In the next step, the operator $\ket{j, k} \mapsto \ket{j, k + x^j \pmod N}$ is
applied to all the qubits, giving the state
$\ket{\psi_2} =
2^{-t/2}\sum_{j=0}^{2^t-1}\ket{j}\ket{x^b}$.

This step is fast because it generates all the powers
simultaneously by quantum parallelism.

Here we take advantage of Python's built-in modular exponentiation function and
simulate this by applying the operator to all the states sequentially.

\subsection{Quantum Fourier Transform}
The discrete Fourier transform is then applied to the first t qubits. This step
is $O(n2^{n})$ if done classically, but can be done polynomially with a quantum
computer.

We simulate this step by sequentially applying the formula
$\ket{k} \mapsto 2^{-t/2} \sum_{j=0}^{2^{t}-1}\omega^{jk}\ket{j}$, where
$\omega^{jk} = e^{2\pi i j k / N}$,
to all the possible states, i.e., for each state $\ket{\phi} = \ket{k}$, its
amplitude is changed to $2^{-t/2} \sum_{j=0}^{2^{t}-1}\omega^{jk}$.

After the quantum state of the system is
$2^{-t/2}\sum_{j=0}^{2^{t}-1}\sum_{k=0}^{2^{t}-1}\omega^{jk}\ket{k}\ket{x^{j}}$

\subsection{Obtaining the Order}
Finally, a measurement is taken, leaving the state to collapse to one vector of
the computational basis. After the application of the previous operations we are
left with an approximation of a number $a/r, a \in \mathbb{Z}$ with high
probability.

We use a known efficient classical algorithm \cite{HW} for extracting $r$ based
on the best approximation property of the convergents of continued fractions. If
we succeed to find $r$, we return it, else the algorithm is restarted.

\section{Execution}

We can run the program by issuing the command
\begin{lstlisting}
  $ chmod +x shor.py
  $ ./shor.py N
\end{lstlisting}

\subsection{Examples}

\begin{lstlisting}
  $ ./shor.py 15
  | picked random a = 3
  | got lucky, 15 = 3 * 5, trying again...
  | --------------------------------------------
  | picked random a = 8
  | measured 59, approximation for 0.23046875 is 3/13
  | 8^13 mod 15 = 8
  | failed, trying again ...
  | measured 246, approximation for 0.9609375 is 1/1
  | 8^1 mod 15 = 8
  | failed, trying again ...
  | measured 109, approximation for 0.42578125 is 3/7
  | 8^7 mod 15 = 2
  | failed, trying again ...
  | measured 222, approximation for 0.8671875 is 7/8
  | 8^8 mod 15 = 1
  | got 8
  | found factor: 15 = 5 * 3
  5
\end{lstlisting}

\begin{lstlisting}
  $ ./shor.py 21
  | picked random a = 10
  | measured 152, approximation for 0.296875 is 3/10
  | 10^10 mod 21 = 4
  | failed, trying again ...
  | measured 342, approximation for 0.66796875 is 2/3
  | 10^3 mod 21 = 13
  | failed, trying again ...
  | measured 37, approximation for 0.072265625 is 1/14
  | 10^14 mod 21 = 16
  | failed, trying again ...
  | measured 53, approximation for 0.103515625 is 2/19
  | 10^19 mod 21 = 10
  | failed, trying again ...
  | measured 42, approximation for 0.08203125 is 1/12
  | 10^12 mod 21 = 1
  | got 12
  | found factor: 21 = 7 * 3
  7
\end{lstlisting}

\begin{thebibliography}{9}
\bibitem{Shor}
  Peter Shor,
  \emph{Polynomial-Time Algorithms for Prime Factorization and Discrete
Logarithms on a Quantum Computer}

\bibitem{HW}
  G. H. Hardy,
  E. M. Wright,
  \emph{Introduction to Theory of Numbers},
  Oxford University Press,
  4th Edition,
  1975.

\end{thebibliography}

\end{document}


\subsection{Divergence Theorem}
\begin{proposition}
    Let $\underline{F}$ be a continuously differentiable vector field, and let $V$ be a volume in $\mathbb R^3$ with piecewise regular boundary $\partial V$, then
    $$\int_V\nabla\cdot\underline{F}\,\mathrm dV=\int_{\partial V}\underline{F}\cdot\mathrm d\underline{S}$$
    where the normal points out of the volume $V$.
\end{proposition}
\begin{proposition}
    Let $\underline{F}$ be a continuously differentiable vector field in $\mathbb R^2$, and let $D$ be a subset of $\mathbb R^2$ be a region with piecewise smooth boundary $\partial D$, then
    $$\int_D\nabla\cdot\underline{F}\,\mathrm dA=\int_{\partial D}\underline{F}\cdot\underline{n}\,\mathrm ds$$
    where $\underline{n}$ points out of the region $D$.
\end{proposition}
\begin{example}
    Let $\underline{F}(\underline{x})=\underline{x}$ and $V$ the cylinder, so
    $$V=\{\underline{x}=\underline{x}(\rho,\phi,z):0\le\rho\le R,0\le\phi\le 2\pi,-h\le z\le h\}$$
    So $\nabla\cdot\underline{F}=3$, hence
    $$\int_V\nabla\cdot\underline{F}\,\mathrm dV=3\int_V\mathrm dV=6\pi R^2h$$
    As for the surface integral, we write $\partial V=S_+\cup S_-\cup S$ where $S_+,S_-$ are the top and lower disks, and $S$ is the curved surface in between.
    $$S=\{R\underline{e_\rho}+z\underline{e_z}:z\in[-h,h],\phi\in [0,2\pi]\}$$
    So $\mathrm d\underline{S}=\underline{e_\rho}R\,\mathrm d\phi\,\mathrm dz$, hence by calculation,
    $$\int_S\underline{F}\cdot\mathrm d\underline{S}=4\pi R^2h$$
    Now $S_{\pm}=\{\rho\underline{e_\rho}\pm h\underline{e_z}:\rho\in[0,R],\phi\in[0,2\pi]\}$.
    We will also find that $\mathrm d\underline{S_\pm}=\pm\underline{e_z}\rho\,\mathrm d\rho\,\mathrm d\phi$.
    $$\int_{S_{\pm}}\underline{F}\cdot\mathrm d\underline{S_{\pm}}=\pi R^2h$$
    So adding them together does give $6\pi R^2h$.
\end{example}
\begin{proposition}
    If $\underline{F}$ is continuously differentiable and for all closed surfaces $S$ we have
    $$\int_S\underline{F}\cdot\mathrm d\underline{S}=0$$
    Then $\nabla\cdot\underline{F}=0$.
\end{proposition}
\begin{proof}
    Assume that it is not zero, so WLOG we can take a point $\underline{x_0}$ such that $\nabla\cdot\underline{F}(\underline{x_0})=\epsilon>0$, then there is some $\delta>0$ with $|\underline{x}-\underline{x_0}|<\delta\implies\nabla\cdot\underline{F}(\underline{x})>\epsilon/2$.
    Take the volume $V$ to be the ball $\{\underline{x}\in\mathbb R^3:|\underline{x}-\underline{x_0}|<\delta\}$ with boundary $\partial V$, then
    $$0=\int_{\partial V}\underline{F}\cdot\mathrm d\underline{S}=\int_V\nabla\cdot\underline{F}\,\mathrm dV>\frac{\epsilon}{2}\operatorname{Volume}(V)>0$$
    by Divergence Theorem.
    Contradiction.
\end{proof}
\begin{example}
    Let $V_\epsilon$ be a volume contained inside a ball of radius $\epsilon$ centered at $\underline{x_0}$.
    Then
    \begin{align*}
        \int_{\partial V_\epsilon}\underline{F}\cdot\mathrm d\underline{S}&=\int_{V_\epsilon}\nabla\cdot\underline{F}\,\mathrm dV\\
        &=\int_{V_\epsilon}\nabla\cdot\underline{F}(\underline{x_0})\,\mathrm dV+\left( \int_{V_\epsilon}\nabla\cdot\underline{F}\,\mathrm dV-\int_{V_\epsilon}\nabla\cdot\underline{F}(\underline{x_0})\,\mathrm dV \right)\\
        &=\nabla\cdot\underline{F}\operatorname{Volume}(V_\epsilon)+\left( \int_{V_\epsilon}\nabla\cdot\underline{F}-\nabla\cdot\underline{F}(\underline{x_0})\,\mathrm dV \right)
    \end{align*}
    But as we did before,
    \begin{align*}
        \left| \int_{V_\epsilon}\nabla\cdot\underline{F}-\nabla\cdot\underline{F}(\underline{x_0})\,\mathrm dV \right|&\le\operatorname{Volume}(V_\epsilon)\sup_{\underline{x}\in V_\epsilon}|\nabla\cdot\underline{F}(\underline{x})-\nabla\cdot\underline{F}(\underline{x_0})|\\
        &=o(\operatorname{Volume(V_\epsilon)})
    \end{align*}
    as $\epsilon\to 0^+$.
    Hence
    $$\nabla\cdot\underline{F}(\underline{x_0})=\lim_{\epsilon\to0^+}\frac{1}{\operatorname{Volume}(V_\epsilon)}\int_{\partial V_\epsilon}\underline{F}\cdot\mathrm d\underline{S}$$
    That is, $\nabla\cdot\underline{F}$ measures the infinitesimal flux per unit volume.
    Therefore $\nabla\cdot\underline{F}(\underline{x_0})>0$ means that the field is going out of $\underline{x_0}$, and it being negative means that the field is going into $\underline{x_0}$.
    If it is zero at that point, then the field is incompressible there.
\end{example}
\begin{example}
    1. Take again $\underline{F}(\underline{x})=\underline{x}$ and $V_\epsilon=\{\underline{x}:|\underline{x}|<\epsilon\}$, then we calculate
    \begin{align*}
        \nabla\cdot\underline{F}(\underline{0})&=\lim_{\epsilon\to 0^+}\frac{1}{\operatorname{Volume}(V_\epsilon)}\int_{\partial V_\epsilon}\underline{F}\cdot\mathrm d\underline{S}\\
        &=3
    \end{align*}
    as desired.\\
    2. Call equations of the form
    $$\frac{\partial\rho}{\partial t}+\nabla\cdot\underline{J}=0$$
    as \textit{conservation laws}.
    We claim that if $|\underline{J}|\to 0$ as $|\underline{x}|\to\infty$, then the charge
    $$Q(t)=\int_{\mathbb R^3}\rho(\underline{x},t)\,\mathrm dV$$
    remains constant.
    We differentiate to get
    \begin{align*}
        \frac{\mathrm dQ}{\mathrm dt}&=\int_{\mathbb R^3}\frac{\partial\rho}{\partial t}\,\mathrm dV\\
        &=-\int_{\mathbb R^3}\nabla\cdot\underline{J}\,\mathrm dV\\
        &=-\lim_{R\to\infty}\int_{|\underline{x}|<R}\nabla\cdot\underline{J}\,\mathrm dV\\
        &=-\lim_{R\to\infty}\int_{|\underline{x}|=R}\underline{J}\cdot\mathrm d\underline{S}\\
        &=0
    \end{align*}
    So $Q$ is constant.
\end{example}


\section{RNG01: Random Number Generators -- 01}

\subsection*{DECSRC}
This is just a wrapper for the DEC SRC-supplied {\tt Random.T.longreal}.

\subsection*{ran0}
NR92, pg 278, describes Schrage's approximatrixion to Park and Miller's
proposed {\em Minimal Standard} generator. The basic approach is:
\begin{verbatim}
     a) z = a*(z mod q) - r*(z div q)
     b) If z <0 then z = z+m
     c) To get back to 0.0 --> 1.0, result = float(z) / float(m)
\end{verbatim}

{\tt mod} is tricky, because you have to do at least:
\begin{equation}
  z \bmod q = z - (z \mbox{div} q)*q
\end{equation}
And maybe deal with negatives.  I suspect M3's MOD function
is fairly complex compared to a simple idiv.  So we ought to
take advantage of knowing z is positive, and do our own mod.

Notice that z div q can be pulled out as a temp.:
\begin{eqnarray}
     tmp & = & z \mbox{div} q\\
     z & = & a*(z - \mbox{tmp}*q) - r*\mbox{tmp}
\end{eqnarray}

Next, remember that dividing by float(m) is more expensive than
multiplying by 1.0/float(m), which can be done once at
initialization:
\begin{verbatim}
     Mrecip = 1.0 / float(m)
     ...
     result = float(z) * Mrecip
\end{verbatim}

Not too surprisingly, this is what NR92 uses.  To claim original
code, we can use one of the alternate sets of constants.
We'll use the first alternative.

NR92 makes a point of telling us to not include the
endpoints (0.0 and 1.0).  Thus there is a Min value and a
Max value.  Min should be such that we don't get a 0.0 when
captured in REAL64 format.  Min can be a few times bigger than EPS.
Max is then that close to 1.0, or (1.0-Min), which can be precalculated.

\subsection*{ran1}
From NR92.  The trick here is to use the ran0 stuff, but use that
result to access a shuffle table.  The shuffle table (per
NR92, pg 281, fig 7.1.1) requires us to take the result
modulo the table size.  But as noted above, mod can be done
inline.  Use that to get the shuffled value, and put the old
one in its place.
\begin{verbatim}
     ndx:=z - (z div size)*size
     z2:= table[ndx]
     table[ndx]:=z
\end{verbatim}

Bays-Durham further cooks the data by saving z2 and using it
for the ndx computation.  That means we have to save it and
have to initialize it.  NR92 initializes it with the first
table value.  Just to be different, we'll use the 3rd table
value.  NR92 also warms up the generator with 8 throwaways.
We'll pass on that.


% LaTeX source for ``Python for Informatics: Exploring Information''
% Copyright (c)  2010-  Charles R. Severance, All Rights Reserved

\chapter{Conditional execution}

\section{Boolean expressions}
\index{boolean expression}
\index{expression!boolean}
\index{logical operator}
\index{operator!logical}

A {\bf boolean expression} is an expression that is either true
or false.  The following examples use the
operator {\tt ==}, which compares two operands and produces
{\tt True} if they are equal and {\tt False} otherwise:

\beforeverb
\begin{verbatim}
>>> 5 == 5
True
>>> 5 == 6
False
\end{verbatim}
\afterverb
%
{\tt True} and {\tt False} are special
values that belong to the type {\tt bool}; they are not strings:

\index{True special value}
\index{False special value}
\index{special value!True}
\index{special value!False}
\index{bool type}
\index{type!bool}

\beforeverb
\begin{verbatim}
>>> type(True)
<type 'bool'>
>>> type(False)
<type 'bool'>
\end{verbatim}
\afterverb
%
The {\tt ==} operator is one of the {\bf comparison operators}; the
others are:

\beforeverb
\begin{verbatim}
      x != y               # x is not equal to y
      x > y                # x is greater than y
      x < y                # x is less than y
      x >= y               # x is greater than or equal to y
      x <= y               # x is less than or equal to y
      x is y               # x is the same as y
      x is not y           # x is not the same as y
\end{verbatim}
\afterverb
%
Although these operations are probably familiar to you, the Python
symbols are different from the mathematical symbols for the same
operations.  A common error
is to use a single equal sign ({\tt =}) instead of a double equal sign
({\tt ==}).  Remember that {\tt =} is an assignment operator and
{\tt ==} is a comparison operator.   There is no such thing as
{\tt =<} or {\tt =>}.

\index{comparison operator}
\index{operator!comparison}

\section {Logical operators}
\index{logical operator}
\index{operator!logical}

There are three {\bf logical operators}: {\tt and}, {\tt
or}, and {\tt not}.  The semantics (meaning) of these operators is
similar to their meaning in English.  For example,

{\tt x > 0 and x < 10}

is true only if {\tt x} is greater than 0
\emph{and} less than 10.

\index{and operator}
\index{or operator}
\index{not operator}
\index{operator!and}
\index{operator!or}
\index{operator!not}

{\tt n\%2 == 0 or n\%3 == 0} is true if \emph{either} of the conditions
is true, that is, if the number is divisible by 2 \emph{or} 3.

Finally, the {\tt not} operator negates a boolean
expression, so {\tt not (x > y)} is true if {\tt x > y} is false;
that is, if {\tt x} is less than or equal to {\tt y}.

Strictly speaking, the operands of the logical operators should be
boolean expressions, but Python is not very strict.
Any nonzero number is interpreted as ``true.''

\beforeverb
\begin{verbatim}
>>> 17 and True
True
\end{verbatim}
\afterverb
%
This flexibility can be useful, but there are some subtleties to
it that might be confusing.  You might want to avoid it until
you are sure you know what you are doing.

\section{Conditional execution}
\label{conditional execution}

\index{conditional statement}
\index{statement!conditional}
\index{if statement}
\index{statement!if}
\index{conditional execution}

In order to write useful programs, we almost always need the ability
to check conditions and change the behavior of the program
accordingly.  {\bf Conditional statements} give us this ability.  The
simplest form is the {\tt if} statement:

\beforeverb
\begin{verbatim}
if x > 0 :
    print 'x is positive'
\end{verbatim}
\afterverb
%
The boolean expression after the {\tt if} statement is
called the {\bf condition}.  We end the {\tt if}
statement with a colon character (:) and the line(s)
after the if statement are indented.

\beforefig
\centerline{\includegraphics[height=1.75in]{figs2/if.eps}}
\afterfig

If the logical condition is true, then the indented
statement gets executed.  If the logical condition is
false, the indented statement is skipped.

\index{condition}
\index{compound statement}
\index{statement!compound}

{\tt if} statements have the same structure as function definitions
or {\tt for} loops\footnote{We will learn about functions in Chapter 4
and loops in Chapter 5.}.The statement consists of a header line
that ends with the colon character (:)
followed by an indented block.  Statements like this are
called {\bf compound statements} because they stretch
across more than one line.

There is no limit on the number of statements that can appear in
the body, but there must be at least one.
Occasionally, it is useful to have a body with no statements (usually
as a placekeeper for code you haven't written yet).  In that
case, you can use the {\tt pass} statement, which does nothing.

\index{pass statement}
\index{statement!pass}

\beforeverb
\begin{verbatim}
if x < 0 :
    pass          # need to handle negative values!
\end{verbatim}
\afterverb
%
If you enter an {\tt if} statement in the Python interpreter, the prompt will change
from three chevrons to three dots to indicate you are in the middle of a block of
statements, as shown below:

\beforeverb
\begin{verbatim}
>>> x = 3
>>> if x < 10:
...    print 'Small'
...
Small
>>>
\end{verbatim}
\afterverb
%

\section{Alternative execution}
\label{alternative execution}

\index{alternative execution}
\index{else keyword}
\index{keyword!else}

A second form of the {\tt if} statement is {\bf alternative execution},
in which there are two possibilities and the condition determines
which one gets executed.  The syntax looks like this:

\beforeverb
\begin{verbatim}
if x%2 == 0 :
    print 'x is even'
else :
    print 'x is odd'
\end{verbatim}
\afterverb
%
If the remainder when {\tt x} is divided by 2 is 0, then we
know that {\tt x} is even, and the program displays a message to that
effect.  If the condition is false, the second set of statements is
executed.

\beforefig
\centerline{\includegraphics[height=1.75in]{figs2/if-else.eps}}
\afterfig

Since the condition must either be true or false, exactly one of
the alternatives will be executed.  The alternatives are called
{\bf branches}, because they are branches in the flow of execution.

\index{branch}

\section{Chained conditionals}
\index{chained conditional}
\index{conditional!chained}

Sometimes there are more than two possibilities and we need more than
two branches.  One way to express a computation like that is a {\bf
chained conditional}:

\beforeverb
\begin{verbatim}
if x < y:
    print 'x is less than y'
elif x > y:
    print 'x is greater than y'
else:
    print 'x and y are equal'
\end{verbatim}
\afterverb
%
{\tt elif} is an abbreviation of ``else if.''  Again, exactly one
branch will be executed.

\beforefig
\centerline{\includegraphics[height=3.00in]{figs2/elif.eps}}
\afterfig

There is no limit on the number of {\tt
elif} statements.  If there is an {\tt else} clause, it has to be
at the end, but there doesn't have to be one.

\index{elif keyword}
\index{keyword!elif}

\beforeverb
\begin{verbatim}
if choice == 'a':
    print 'Bad guess'
elif choice == 'b':
    print 'Good guess'
elif choice == 'c':
    print 'Close, but not correct'
\end{verbatim}
\afterverb
%
Each condition is checked in order.  If the first is false,
the next is checked, and so on.  If one of them is
true, the corresponding branch executes, and the statement
ends.  Even if more than one condition is true, only the
first true branch executes.

\section{Nested conditionals}
\index{nested conditional}
\index{conditional!nested}

One conditional can also be nested within another.  We could have
written the three-branch example like this:

\beforeverb
\begin{verbatim}
if x == y:
    print 'x and y are equal'
else:
    if x < y:
        print 'x is less than y'
    else:
        print 'x is greater than y'
\end{verbatim}
\afterverb
%
The outer conditional contains two branches.  The
first branch contains a simple statement.  The second branch
contains another {\tt if} statement, which has two branches of its
own.  Those two branches are both simple statements,
although they could have been conditional statements as well.

\beforefig
\centerline{\includegraphics[height=2.50in]{figs2/nested.eps}}
\afterfig

Although the indentation of the statements makes the structure
apparent, {\bf nested conditionals} become difficult to read very
quickly. In general, it is a good idea to avoid them when you can.

Logical operators often provide a way to simplify nested conditional
statements.  For example, we can rewrite the following code using a
single conditional:

\beforeverb
\begin{verbatim}
if 0 < x:
    if x < 10:
        print 'x is a positive single-digit number.'
\end{verbatim}
\afterverb
%
The {\tt print} statement is executed only if we make it past both
conditionals, so we can get the same effect with the {\tt and} operator:

\beforeverb
\begin{verbatim}
if 0 < x and x < 10:
    print 'x is a positive single-digit number.'
\end{verbatim}
\afterverb

\section{Catching exceptions using try and except}
\label{catch1}

Earlier we saw a code segment where we used the \verb"raw_input" and
{\tt int} functions to read and parse an integer number entered by
the user.  We also saw how treacherous doing this could be:

\beforeverb
\begin{verbatim}
>>> speed = raw_input(prompt)
What...is the airspeed velocity of an unladen swallow?
What do you mean, an African or a European swallow?
>>> int(speed)
ValueError: invalid literal for int()
>>>
\end{verbatim}
\afterverb
%
When we are executing these statements in the Python interpreter,
we get a new prompt from the interpreter, think ``oops'', and move
on to our next statement.

However if you place this code in a
Python script and this error occurs, your script immediately
stops in its tracks with a traceback.
It does not execute the following statement.
\index{traceback}

Here is a sample program to convert a Fahrenheit temperature
to a Celsius temperature:
\index{fahrenheit}
\index{celsius}
\index{temperature conversion}

\beforeverb
\begin{verbatim}
inp = raw_input('Enter Fahrenheit Temperature:')
fahr = float(inp)
cel = (fahr - 32.0) * 5.0 / 9.0
print cel
\end{verbatim}
\afterverb
%
If we execute this code and give it invalid input, it simply fails
with an unfriendly error message:

\beforeverb
\begin{verbatim}
python fahren.py
Enter Fahrenheit Temperature:72
22.2222222222

python fahren.py
Enter Fahrenheit Temperature:fred
Traceback (most recent call last):
  File "fahren.py", line 2, in <module>
    fahr = float(inp)
ValueError: invalid literal for float(): fred
\end{verbatim}
\afterverb
%
There is a conditional execution structure built into
Python to handle these types of expected and unexpected
errors called ``try / except''.  The idea of {\tt try}
and {\tt except} is that you know that some sequence
of instruction(s) may have a problem and you want to
add some statements to be executed if an error occurs.
These extra statements (the except block) are ignored
if there is no error.

You can think of the {\tt try} and {\tt except} feature
in Python as an ``insurance policy'' on a sequence of
statements.

We can rewrite our temperature converter as follows:

\beforeverb
\begin{verbatim}
inp = raw_input('Enter Fahrenheit Temperature:')
try:
    fahr = float(inp)
    cel = (fahr - 32.0) * 5.0 / 9.0
    print cel
except:
    print 'Please enter a number'
\end{verbatim}
\afterverb
%

Python starts by executing the
sequence of statements in the
{\tt try} block.  If all goes
well, it skips the {\tt except} block and proceeds.  If an
exception occurs in the {\tt try} block,
Python jumps out of the {\tt try} block and
executes the sequence of statements in the {\tt except} block.

\beforeverb
\begin{verbatim}
python fahren2.py
Enter Fahrenheit Temperature:72
22.2222222222

python fahren2.py
Enter Fahrenheit Temperature:fred
Please enter a number
\end{verbatim}
\afterverb
%

Handling an exception with a {\tt try} statement is called {\bf
catching} an exception.  In this example, the {\tt except} clause
prints an error message.  In general,
catching an exception gives you a chance to fix the problem, or try
again, or at least end the program gracefully.

\section{Short-circuit evaluation of logical expressions}
\index{short circuit}

When Python is processing a logical expression such as
{\tt x >= 2 and (x/y) > 2}, it evaluates the expression
from left to right.  Because of the definition of {\tt and},
if {\tt x} is less than 2, the expression {\tt x >= 2} is
{\tt False} and so the whole expression is {\tt False} regardless
of whether {\tt (x/y) > 2} evaluates to {\tt True} or {\tt False}.

When Python detects that there is nothing to be gained by evaluating
the rest of a logical expression, it stops its evaluation and does
not do the computations in the rest of the logical expression.
When the evaluation of a logical expression stops because the overall
value is already known, it is called {\bf short-circuiting}
the evaluation.

\index{guardian pattern}
\index{pattern!guardian}
While this may seem like a fine point, the short-circuit behavior
leads to a clever technique called the {\bf guardian pattern}.
Consider the following code sequence in the Python interpreter:

\beforeverb
\begin{verbatim}
>>> x = 6
>>> y = 2
>>> x >= 2 and (x/y) > 2
True
>>> x = 1
>>> y = 0
>>> x >= 2 and (x/y) > 2
False
>>> x = 6
>>> y = 0
>>> x >= 2 and (x/y) > 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero
>>>
\end{verbatim}
\afterverb
%
The third calculation failed because Python was evaluating {\tt (x/y)}
and {\tt y} was zero, which causes a runtime error.  But the second example
did \emph{not} fail because the first part of the expression {\tt x >= 2}
evaluated to {\tt False} so the {\tt (x/y)} was not ever executed
due to the {\bf short-circuit} rule and there was no error.

We can construct the logical expression to strategically place a {\bf guard}
evaluation just before the evaluation that might cause an error as follows:

\beforeverb
\begin{verbatim}
>>> x = 1
>>> y = 0
>>> x >= 2 and y != 0 and (x/y) > 2
False
>>> x = 6
>>> y = 0
>>> x >= 2 and y != 0 and (x/y) > 2
False
>>> x >= 2 and (x/y) > 2 and y != 0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero
>>>
\end{verbatim}
\afterverb
%
In the first logical expression, {\tt x >= 2} is {\tt False} so the evaluation
stops at the {\tt and}.  In the second logical expression, {\tt x >= 2} is {\tt True}
but {\tt y != 0} is {\tt False} so we never reach {\tt (x/y)}.

In the third logical expression, the {\tt y != 0} is \emph{after} the
{\tt (x/y) } calculation so the expression fails with an error.

In the second expression, we say that {\tt y != 0} acts as a {\bf guard}
to insure that we only execute {\tt (x/y)} if {\tt y} is non-zero.

\section{Debugging}
\label{whitespace}
\index{debugging}
\index{traceback}

The traceback Python displays when an error occurs contains
a lot of information, but it can be overwhelming.  The most
useful parts are usually:

\begin{itemize}

\item What kind of error it was, and

\item Where it occurred.

\end{itemize}

Syntax errors are usually easy to find, but there are a few
gotchas.  Whitespace errors can be tricky because spaces and
tabs are invisible and we are used to ignoring them.

\index{whitespace}

\beforeverb
\begin{verbatim}
>>> x = 5
>>>  y = 6
  File "<stdin>", line 1
    y = 6
    ^
SyntaxError: invalid syntax
\end{verbatim}
\afterverb
%
In this example, the problem is that the second line is indented by
one space.  But the error message points to {\tt y}, which is
misleading.  In general, error messages indicate where the problem was
discovered, but the actual error might be earlier in the code,
sometimes on a previous line.

\index{error!runtime}
\index{runtime error}

The same is true of runtime errors.  Suppose you are trying
to compute a signal-to-noise ratio in decibels.  The formula
is $SNR_{db} = 10 \log_{10} (P_{signal} / P_{noise})$.  In Python,
you might write something like this:

\beforeverb
\begin{verbatim}
import math
signal_power = 9
noise_power = 10
ratio = signal_power / noise_power
decibels = 10 * math.log10(ratio)
print decibels
\end{verbatim}
\afterverb
%
But when you run it, you get an error message\footnote{In Python 3.0,
  you no longer get an error message; the division operator performs
  floating-point division even with integer operands.}:

\index{exception!OverflowError}
\index{OverflowError}

\beforeverb
\begin{verbatim}
Traceback (most recent call last):
  File "snr.py", line 5, in ?
    decibels = 10 * math.log10(ratio)
OverflowError: math range error
\end{verbatim}
\afterverb
%
The error message indicates line 5, but there is nothing
wrong with that line.  To find the real error, it might be
useful to print the value of {\tt ratio}, which turns out to
be 0.  The problem is in line 4, because dividing two integers
does floor division.  The solution is to represent signal power
and noise power with floating-point values.

\index{floor division}
\index{division!floor}

In general, error messages tell you where the problem was discovered,
but that is often not where it was caused.

\section{Glossary}

\begin{description}

\item[body:] The sequence of statements within a compound statement.
\index{body}

\item[boolean expression:]  An expression whose value is either
{\tt True} or {\tt False}.
\index{boolean expression}
\index{expression!boolean}

\item[branch:] One of the alternative sequences of statements in
a conditional statement.
\index{branch}

\item[chained conditional:]  A conditional statement with a series
of alternative branches.
\index{chained conditional}
\index{conditional!chained}

\item[comparison operator:] One of the operators that compares
its operands: {\tt ==}, {\tt !=}, {\tt >}, {\tt <}, {\tt >=}, and {\tt <=}.

\item[conditional statement:]  A statement that controls the flow of
execution depending on some condition.
\index{conditional statement}
\index{statement!conditional}

\item[condition:] The boolean expression in a conditional statement
that determines which branch is executed.
\index{condition}

\item[compound statement:]  A statement that consists of a header
and a body.  The header ends with a colon (:).  The body is indented
relative to the header.
\index{compound statement}

\item[guardian pattern:] Where we construct a logical expression
with additional
comparisons to take advantage of the short-circuit behavior.
\index{guardian pattern}
\index{pattern!guardian}

\item[logical operator:] One of the operators that combines boolean
expressions: {\tt and}, {\tt or}, and {\tt not}.

\item[nested conditional:]  A conditional statement that appears
in one of the branches of another conditional statement.
\index{nested conditional}
\index{conditional!nested}

\item[traceback:]  A list of the functions that are executing,
printed when an exception occurs.
\index{traceback}

\item[short circuit:]  When Python is part-way through evaluating a
logical expression and stops the evaluation because Python
knows the final value for the expression
without needing to evaluate the rest of the expression.
\index{short circuit}

\end{description}

\section{Exercises}

\begin{ex}
Rewrite your pay computation to give the employee 1.5
times the hourly rate for
hours worked above 40 hours.

\begin{verbatim}
Enter Hours: 45
Enter Rate: 10
Pay: 475.0
\end{verbatim}
\end{ex}

\begin{ex}
Rewrite your pay program using {\tt try} and {\tt except}
so that your program handles non-numeric input gracefully
by printing a message and exiting the program.
The following shows two executions of the program:

\begin{verbatim}
Enter Hours: 20
Enter Rate: nine
Error, please enter numeric input

Enter Hours: forty
Error, please enter numeric input
\end{verbatim}
\end{ex}

\begin{ex}
Write a program to prompt for a score between 0.0 and 1.0.
If the score is out of range, print an error message.  If the score
is between 0.0 and 1.0, print a grade using the following
table:

\begin{verbatim}
Score   Grade
>= 0.9     A
>= 0.8     B
>= 0.7     C
>= 0.6     D
< 0.6    F

Enter score: 0.95
A

Enter score: perfect
Bad score

Enter score: 10.0
Bad score

Enter score: 0.75
C

Enter score: 0.5
F
\end{verbatim}

Run the program repeatedly as shown above to test the
various different values for input.
\end{ex}


\section{Model Description}
There two types of RW control torque interfaces, analog and digital. This modules assumes the RW is controlled through a set of voltages sent to the RW motors.  This module is developed in a general manner where a voltage deadband is assumed and the module can be run in a pure open-loop manner, or with a closed-loop torque tracking control mode.  Finally, if a RW availability message is present, then the RW is set to zero if the corresponding availability is set to {\tt UNAVAILABLE}.

\begin{figure}[htb]
	\centerline{
		\includegraphics[]{Figures/us2V}
	}
	\caption{Illustration of RW motor torque to voltage conversion}
	\label{fig:us2V}
\end{figure}
\subsection{Open-loop voltage conversion}
This module requires the RW configuration message to contain the maximum RW motor torque values $u_{\text{max}}$.  The user must specify the minimum and maximum output voltages as shown in Figure~\ref{fig:us2V}.  The minimum voltage is a voltage below which the motor doesn't apply a torque, i.e. a deadzone.

Let the intermediate voltage value $V_{\text{int}}$ as
\begin{equation}
\label{eq:rwMV:1}
V_{\text{int}} = \frac{V_{\text{max}} - V_{\text{min}}}{u_{\text{max}}} u_{s}
\end{equation}
The output voltage is thus determined through
\begin{equation}
V = V_{\text{int}} + V_{\text{min}} *\text{sgn}(V_{\text{int}} )
\end{equation}

\subsection{RW Availability}
If the input message name {\tt rwAvailInMsgName} is defined, then the RW availability message is read in. The voltage mapping is only performed if the individual RW availability setting is {\tt AVAILABLE}.  If it is {\tt UNAVAILABLE} then the output voltage is set to zero.

\subsection{Closed-loop commanded torque tracking}
The requested RW motor torque is given by $u_{s}$.  The RW wheel speed $\Omega$ is monitored to see if the actual torque being applied matches the commanded torque.  Let $J_{s}$ be the RW spin inertia about the RW spin axis $\hat{\bm g}_{s}$.   In the following development the motor torque equation is approximated as
\begin{equation}
u_{s} = J_{s} \dot\Omega
\end{equation}
where the assumption is made that the spacecraft angular accelerations are small compared to the RW angular accelerations.  The $\dot\Omega$ term is digitally evaluated using a backwards difference method:
\begin{equation}
\dot\Omega_{n} = \frac{\Omega_{n} - \Omega_{n-1}}{\Delta t}
\end{equation}
Care is taken that the old RW speed information $\Omega_{n-1}$ is not used unless a history of wheel speeds is available, in particular, after a module reset.  Thus, the actual RW torque is evaluated as
\begin{equation}
u_{n} = J_{s} \dot\Omega_{n}
\end{equation}
Finally, the closed loop motor torque value is computed with a proportional feedback component as
\begin{equation}
u_{s,CL} = u_{s} - K (u_{n} - u_{s})
\end{equation}
where $K>0$ is a positive feedback gain value.  Finally, this $u_{s,CL}$ is fed to the voltage conversion process in Eq.~\eqref{eq:rwMV:1}.

\subsection{Saturation and Dead Band}
If the calculated voltage is outside of $\pm V_{\mathrm{max}}$, then the voltage is saturated at the $\pm V_{\mathrm{max}}$ value. Note, this corresponds to the reaction wheel torques being saturated. Similarly, if the calculated voltage is inside $\pm V_{\mathrm{min}}$, then the voltage is set to $\pm V_{\mathrm{min}}$. This simulates the dead band. If the $V_{\mathrm{min}} = 0$, then there is no dead band.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CS624: Analysis of Algorithms
% Copyright 2015 Pejman Ghorbanzade <pejman@ghorbanzade.com>
% Creative Commons Attribution-ShareAlike 4.0 International License
% More info: https://github.com/ghorbanzade/beacon
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section*{Question 10}

Assume that all the keys in a binary search tree are distinct.
Let $x$ be a node in a binary search tree.
Show that
\begin{enumerate}
\item If $a$ is an ancestor of $x$ and $a.key > x.key$, then for any descendant $d$ of $x$, we have $a.key > d.key$.

\item If $a$ is the successor of $x$, then $a$ is either an ancestor or descendant of $x$.

\item If the right subtree of $x$ is nonempty, then the successor of $x$ is just the leftmost node in the right subtree.

\item If the right subtree of $x$ is empty and if $x$ has a successor $y$ (i.e., $x$ is not the largest element in the tree), then $y$ is the lowest ancestor of $x$ whose left child is also an ancestor of $x$.

\item If $x$ has a right child, then the successor of $x$ does not have a left child.

\item Similarly, if $x$ has a left child, then the predecessor of $x$ does not have a right child.
\end{enumerate}

\subsection*{Solution}

\begin{enumerate}
\item Property of the binary search trees suggest that the key of any node $d$ of a binary search tree is smaller than the key stored in its ancestors.
Therefore, for any descendant $d$ of $x$, we have $x.key > d.key$.
Since $a.key > x.key$, then $a.key > d.key$ would immediately follow.
\end{enumerate}