\documentclass{article}
\usepackage{cite}
\usepackage{amsmath}
\title{Introduction}
\author{Seth Chaiken}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Specialized Symbols
%
%   Disjoint Union
%\newcommand{\dunion}{\uplus}
\newcommand{\dunion}
%{\mbox{\hbox{\hskip4pt$\cdot$\hskip-4.62pt$\cup$\hskip2pt}}}
%{\mbox{\hbox{\hskip6pt$\cdot$\hskip-5.50pt$\cup$\hskip2pt}}}
{\mbox{\hbox{\hskip0.45em$\cdot$\hskip-0.44em$\cup$\hskip0.2em}}}
%{\mbox{\hbox{\hskip0.45em$+$\hskip-0.70em$\cup$\hskip0.3em}}}
%
% Dot inside a cup.
% If there is a better, more Latex like way
% (more invariant under font size changes) way,
% I'd like to know.

\newcommand{\Bases}[1]{\ensuremath{{\mathcal{B}}(#1)}}
\newcommand{\Reals}{\ensuremath{\mathbb{R}}}
\newcommand{\FieldK}{\ensuremath{K}}
\newcommand{\Perms}{\ensuremath{\mathfrak{S}}}
%\newcommand{\rank}{{\rho}}% {{\mbox{rank}}}
%\newcommand{\Rank}{{\rho}}% {{\mbox{rank}}}
\newcommand{\rank}{{\mbox{r}}}% {{\mbox{rank}}}
\newcommand{\Rank}{{\mbox{r}}}% {{\mbox{rank}}}
\newcommand{\Card}[1]{\ensuremath{{\left|#1\right|}}}
\newcommand{\ext}[1]{\ensuremath{\mathbf{#1}}}
%\newcommand{\extvee}{\ensuremath{\mathbf{\vee}}}
\newcommand{\extvee}{\;\;}
\newcommand{\Plucker}{Pl\"{u}cker\ }

% Set Complement
% command to mess with overline, bar or custom
% alternatives for sequence or set complement
%
\newcommand{\scomp}[1]{\ensuremath{\overline{#1}}}
%\newcommand{\scomp}[1]{\ensuremath{\bar{#1}}}
%
%   Put a symbol for a matroid in a box, or brackets
%\newcommand{\MVAR}[1]{{\boxed{#1}\;}}
\newcommand{\MVAR}[1]{{[#1]\;}}
%
\newcommand{\UNION}{\cup} %try to make this bold.
%%%%%%%%End of specialized symbols%%%%%%%%%%%%%%%%%%%%%%

\begin{document}
\maketitle

(here we go, geronimo)

\rule{\textwidth}{3pt}
(1)

Why Electricity, EE?
\begin{itemize}
\item $>$ Some 100 years geometry-like intuition

(Marc Thompson ``Intuitive Analog Circuit Design'' 2nd ed 2013
\cite{intuitAna}, Hasler, Neirynck translates into
Oriented Matroid pair analysis \cite{HaslerNeirynck})

\item Geometry of linear spaces and oriented matroids.

(Our Tutte decomposed exterior algebraic
function of oriented matroids.)

(Techniques from Barnabi, Brini and Rota's Exterior Calculus
\cite{exteriorCalc})

\item
Real behavior $\approx$ ideal plus perturbations

\item
Ideal constraints result in intended real behavior

(Ideal predicts the real)

(how geometry is applied everyday)

\item
Interesting, accessible, intuitively understandable
(because of (human, intentential) design), applicable,
easy to both simulate and build physically, structure of moderate
dimension (about 12 or 24, depending on what they may count / formulation)
\item
Analogs to chemical (and real algebraic geometry
\cite{signsInChemRAG}), biological, mechanical, etc., random walks ...

\item
Merely one scalar non-linearity can cause chaos.
\end{itemize}
\rule{\textwidth}{3pt}

Analogies:
\begin{enumerate}
\item Electric: Voltage, Resistance, Current, Capacitance, Inductance
\item Mech. Dyn: Force, Viscous friction, Velocity, Elasticity, Mass
\item Statics: Displacement, Elasticity, Force,  ???, ???
\item Random Walk: First hit probability, transition probability, flow, ???, ??? \\
Doyle and Snell \cite{DoyleSnellRandom},
Lyons and Peres \cite{ProbOnTreesNetworks}
(ref? Thanks, David G. Wagner's course notes).
\item Traffic: Time, Clogginess (travel time/flow ratio), flow, ???, ???\\
Condition: All vehicles reach their destinations in equal times regardless of
route (ref? Thanks, Peter Shor).
\item Knot theory: ???, $\pm 1$ crossing sign, ???, ???, ???\\
Equivalent resistance ``Conductance Invariant'' of rational tangles is a
Reinmeister move
invariant\cite{RationalTangles} (Thanks, Jay Goldman).
\end{enumerate}

KCL+KVL+Ohms=(one of KCL)+Ohms+(min power)

Future: Memistor?

\subsection{Equivalent Resistance}

\pagebreak[3] \begin{quote}\rule{\textwidth}{3pt}
(2)\\
(Kirchhoff (1847), Maxwell (1891)) The equivalent resistance problem
is solved by
the Matrix Tree Theorem.

\rule{\textwidth}{3pt}
\end{quote}

Kirchhoff\cite{Kirchhoff} and
Maxwell\cite{MaxR} solved the equivalent resistance problem
by the matrix tree theorem.

\rule{\textwidth}{3pt}\\
(3)\\
\begin{minipage}{.4\textwidth}
(resistor edge diagram)\\
$i_e$\\
$v_e=v_1-v_2$\\
$g_ev_e=r_ei_e$
\end{minipage}
\begin{minipage}{.4\textwidth}
(port edge diagram, environment)\\
$i_p$\\
$v_p=v_1-v_2$\\
mathematics sign convention.\\
$g_ev_e=r_ei_e$
\end{minipage}\\
\rule{\textwidth}{3pt}\\

\pagebreak[3] \begin{quote}\rule{\textwidth}{3pt}\\
(4)\\
(KCL) $(i_e)_{e\in S}$ is a cycle (a flow)\\
(KVL) $(v_e)_{e\in S}$ is a cocycle\\
(constituitive Law) $i_e=g_e(v_e)$
non-linear, usually monotonic increasing $R\rightarrow R$
(Ohm's approximation $i_e=g_ev_e$\\
signs ($\pm$) have oriented matroid structure
(combinatorial, geometric, topological)\\
\rule{\textwidth}{3pt}
\end{quote}

\pagebreak[3] \begin{quote}\rule{\textwidth}{3pt}\\
(5)\\
Equivalent resistance is a \textbf{ratio}
\[
\frac{\mbox{WTS}(G/p)}
{\mbox{WTS}(G\backslash p)}
\]
\[
\mbox{(WeightedTreeSum)}\mbox{WTS}(G') =
g_e \mbox{WTS}(G'/e) + r_e \mbox{WTS} (G \backslash e)
\text{\ for all\ }e \not\in P
\]
\rule{\textwidth}{3pt}
\end{quote}

\pagebreak[3] \begin{quote}\rule{\textwidth}{3pt}\\
(6)\\
Benefits of Multiple ports:

\begin{itemize}
\item
Formalize composition of systems\cite{NarayananDecompVS1986}
\item
Label variables to observe
\item
Model practical devices (transistors, op amps)
\item
Interesting \textbf{non-commutative ranges} of
new Tutte functions with pattern:
\[
\text{TF}(N(P\dunion E)) = F(N(P\dunion E)/E)
\]
\item
Systematize ad-hoc vertex-based proofs.
\end{itemize}

\rule{\textwidth}{3pt}
\end{quote}

\pagebreak[3] \begin{quote}\rule{\textwidth}{3pt}\\
(7)\\
Equivalent resistance is a coefficient ratio in an
implicitly defined linear function.

(diagram)

In other words
\[
R_pi_p + v_p = 0
\]
or dually,
\[
\mathcal{B} = \{(i_p,v_p)\}
= \{ t(-1, R_p) | t\in R\}
\]

\rule{\textwidth}{3pt}
\end{quote}

(linear subspace over the $R^{\mathbf{P}}$)

\pagebreak[3] \begin{quote}\rule{\textwidth}{3pt}\\
(8)\\
The $2\times d$ port variable constraint space,
and its solution spaces, are $d$-dimensional.

We represent these spaces by carefully defined
\textbf{extensors}, as Barnabei, Brini and Rota \cite{exteriorCalc}
term ``decomposible antisymmetric tensors''

The solution extensor (not a ray) satisfies:

\[
E(N) = \text{sign}(...)(g_e E(N/e) + r_e E(N\backslash e)
\text{\ for\ }e\not\in P
\]

\rule{\textwidth}{3pt}
\end{quote}

The port elements represent the basis chosen so coordinates correspond
to voltages and currents belonging to network edges.  Geometric aspects
of extensors are surveyed by
Barnabei and Brini \cite{exteriorCalc}.

Cordovil\cite{CommAlgOMs}
presented a \textbf{commutative algebraic} function of
oriented matroids in which monomial signs encode orientation information.
The analogy to our \textbf{exterior algebraic} functions must be explored.

\pagebreak[3] \begin{quote}\rule{\textwidth}{3pt}\\
(9)\\
\[
E(N) = \ldots + C_i\mbox{\bf XXX} + \ldots
\]
\[
R = C_i/C_j
\]
All the other $C_k$'s have similar interpretations.

Each $C_k$ is a determinant.

Each $C_k$ is a signed weighted enumerator of
forests satisfying \textbf{conditions ...}

Each $C_k$ satisfies
\[
C_k(N) = g_e C_k(N/e) + r_e C_k(N\backslash e)
\text{\ for\ }e\not\in {P}
\]

\rule{\textwidth}{3pt}
\end{quote}

\pagebreak[3] \begin{quote}\rule{\textwidth}{3pt}\\
(10)\\
The \textbf{conditions ...} are best described with
a \textbf{pair of ported oriented matroids}
(aka \textbf{relative or set-pointed}

The conditions for a given $C_k$ \textit{sometimes}
make all the signs the same (example: $C_i$ and
$C_j$ in 1-port equivalent resistance $R=C_i/C_j$)

\textit{Othertimes}, the oriented \textbf{P-minors}
in the completed Tutte decomposition of $C_k$ determine
the sign.

(diagram of the two orientations of $C_2$)

\rule{\textwidth}{3pt}
\end{quote}

\pagebreak[3] \begin{quote}\rule{\textwidth}{3pt}\\
(11)\\
What is the nature of the conditions?  We state them using the
network's graphic oriented matroid.

(diagram--glob w/ ports)

\rule{\textwidth}{3pt}
\end{quote}

\textbf{port} (sdc, engineering literature),
\textbf{distinguished element},
element of the restriction's set (Diao and Hetyei),
a element in the set of points (Las Vergnas).

\pagebreak[3] \begin{quote}\rule{\textwidth}{3pt}\\
(12)\\
Voltage and Current graphs

\begin{minipage}{0.4\textwidth}
``Voltage graph'' (EE, NOT Gross, et. al.) represents KVL
\[\mathbf{v}\in \text{Cocycle space}\]
\end{minipage}
\begin{minipage}{0.4\textwidth}
``Current graph'' represents KCL
\[\mathbf{i}\in \text{Cycle space}\]
\end{minipage}
They are equal graphs for resistor networks.

For networks with idealized amplifiers, they are not
equal.

(more) Realistic amplifier model $=$ idealized amplifiers $+$
resistors.

(ideal op amp nullator/norator T diagram)

The output voltage and current are whatever makes the input
voltage and current BOTH BE zero.

\begin{minipage}{0.4\textwidth}
\[
\text{(nullator)\ }N_v = N/e//
N_I = N\backslash e
\]
\end{minipage}
\begin{minipage}{0.4\textwidth}
\[
\text{(norator)\ }N_v = N\backslash e//
N_I = N/e
\]
\end{minipage}

\rule{\textwidth}{3pt}
\end{quote}

\pagebreak[3] \begin{quote}\rule{\textwidth}{3pt}\\
(13)\\
What are the conditions like?

\[
\text{Extensor\ }= \sigma(C)(a_1\vee a_2\vee \ldots \vee a_d)
\]
\[
C = \ldots + t + \ldots
\]
$t \leftrightarrow \text{\ sets of (``contractible'' non-port edges}
E_1, E_2$ for which
\[
N_V/E_1\backslash(E\setminus E_1) = \text{\ certain OMs on\ } P
\]
AND
\[
N_I/E_2\backslash(E\setminus E_2) = \text{\ certain OMs on\ } P
\]

Transfer resistance might be 0 and might be $\neq 0$ iff
\[
\exists E_1, E_2 \text{so (diag) and (diag)}
\]

($K_4$ Wheatstone bridge diagram)

\[
\frac{R_1}{R_2} > \frac{R_3}{R_4}
\text{\ neg\ }
\frac{R_1}{R_2} < \frac{R_3}{R_4}
\text{\ pos\ }
\]

\rule{\textwidth}{3pt}
\end{quote}

\pagebreak[3] \begin{quote}\rule{\textwidth}{3pt}\\
(14)\\
(Rayleigh)

\rule{\textwidth}{3pt}
\end{quote}

\pagebreak[3] \begin{quote}\rule{\textwidth}{3pt}\\
(15)\\

\rule{\textwidth}{3pt}
\end{quote}

\pagebreak[3] \begin{quote}\rule{\textwidth}{3pt}\\
(16)\\

\rule{\textwidth}{3pt}
\end{quote}

\pagebreak[3] \begin{quote}\rule{\textwidth}{3pt}\\
(17)\\

\rule{\textwidth}{3pt}
\end{quote}

\bibliography{../../bib/MathOfElec}{}
\bibliographystyle{plain}

\end{document}


\documentclass[a4paper,10pt]{article}
\usepackage[dvips]{color,graphicx}
\usepackage[dvips, bookmarks, colorlinks=false]{hyperref}

%opening
\title{Math508 Homework 12}
\author{Yu Huang}
\date{2007-04-26}

\begin{document}

\maketitle

\begin{abstract}
Continuous state space Filtering
\end{abstract}

\section{Problem 3}
\subsection{Solve integral by Riemann sum}
The difficult part is how to integrate the probability function. The idea is to replace the integral with a Riemann sum over the support where probability mass is not trivial. Based on the model, the signal $X_n$ is concentrated around 7 with small variance. As the sequence progresses, the general trend of $X_n$ goes upward though it's slow (due to the fact $X_{n+1} = 1.004X_n + 0.06X_nVn$).

\begin{equation}
\int_a^b f(x) dx = \sum_{x_i=a}^{x_i<b} f(x_i) \Delta x
\end{equation}

The support we pick is $[-20, 30]$. Different $\Delta x$ were tried. Smaller $\Delta x$, smaller mean squared error.

Figure~\ref{f1} is $\Delta x=0.15$. Figure~\ref{f2} is $\Delta x=0.34$. $\Delta x=0.5, 0.35$ could result in zero float division in normal density.

\subsection{algorithm of partical filter or sequential importance resampling}
The idea is
\begin{equation}
E(F(\mathbf{X}_t)) = \int F_t(\mathbf{X}_t) \pi_t(\mathbf{X}_t|\mathbf{Y}_t) d\mathbf{X}_t = \int F_t(\mathbf{X}_t) \frac{\pi_t(\mathbf{X}_t|\mathbf{Y}_t)}{P(\mathbf{X}_t|\mathbf{Y}_{t-1})} P(\mathbf{X}_t|\mathbf{Y}_{t-1}) d\mathbf{X}_t
\end{equation}

\begin{eqnarray}
\pi_t(\mathbf{X}_t|\mathbf{Y}_t) = \frac{P(X_t, Y_t, \mathbf{X}_{t-1}|\mathbf{Y}_{t-1})}{P(Y_t)} \\
P(X_t, Y_t, \mathbf{X}_{t-1}|\mathbf{Y}_{t-1}) = P(Y_t|X_t)P(X_t|\mathbf{X}_{t-1})P(\mathbf{X}_{t-1}|\mathbf{Y}_{t-1}) \\
P(\mathbf{X}_{t-1}|\mathbf{Y}_{t-1}) = \pi_t(\mathbf{X}_{t-1}|\mathbf{Y}_{t-1}) \\
\pi_t(\mathbf{X}_t|\mathbf{Y}_t) = \frac{P(Y_t|X_t)P(X_t|X_{t-1})\pi_t(\mathbf{X}_{t-1}|\mathbf{Y}_{t-1})}{P(Y_t)}
\end{eqnarray}

The proposal distribution is $P(\mathbf{X}_t|\mathbf{Y}_{t-1}) = P(X_t|X_{t-1})\pi_t(\mathbf{X}_{t-1}|\mathbf{Y}_{t-1})$. The importance weight is $\pi_t(\mathbf{X}_t|\mathbf{Y}_t)/P(\mathbf{X}_t|\mathbf{Y}_{t-1}) = P(Y_t|X_t)/P(Y_t)$. As $P(Y_t)$ could be regarded as constant, the weight is just $P(Y_t|X_t)$.

For $t=0$, the proposal distribution is $P(X_0)$ and the importance weight is $P(Y_0|X_0)$.

The algorithm looks like
\begin{itemize}
\item sample $x_t^i$ from $P(X_t|X_{t-1})$ for $i=1,...,n$
\item weight each sample by $w(x_t^i) = P(Y_t|x_t^i)$
\item estimate is $\hat{x}_t = \sum_{i=1}^n x_t^i*w(x_t^i)$. Note: it's not the $x_t^i$ with maximum weight cuz it simply picks the one closest to the observation, $Y_t$.
\item resample from $\{x_t^1, ..., x_t^n\}$ with probability proportional to $w(x_t^i)$ to produce a random sample $\{x_t^{*1}, ..., x_t^{*n}\}$
\end{itemize}

Figure~\ref{f3} is the result by SIR and it's pretty good (mse=0.4908) with 10000 samplings each iteration.

\section{Figures}

\begin{figure}
\includegraphics[width=1\textwidth]{hw12_3_gap_0_15.eps}
\caption{}\label{f1}
\end{figure}

\begin{figure}
\includegraphics[width=1\textwidth]{hw12_3_gap_0_34.eps}
\caption{}\label{f2}
\end{figure}

\begin{figure}
\includegraphics[width=1\textwidth]{hw12_3_SIR_10000.eps}
\caption{}\label{f3}
\end{figure}

\end{document}


\documentclass[11pt]{article}

\usepackage[utf8]{inputenc}
\usepackage{fancyhdr}
\usepackage{hyperref}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{natbib}

\graphicspath{ {images/} }

\pagestyle{fancy}
\fancyhf{}
\lhead{University of the Witwatersrand}
\rfoot{School of Computer Science and Applied Mathematics}
\pagenumbering{roman}
\fancyfoot[R]{\thepage}
\renewcommand{\headrulewidth}{0pt} % to remove line on header
\renewcommand{\footrulewidth}{0pt} % to remove line on footer

\begin{document}
\begin{page}
\thispagestyle{empty}

\newcommand{\HRule}{\rule{\linewidth}{0.3mm}} % Defines a new command for the horizontal lines, change thickness here
\renewcommand\section{\@startsection{section}{1}{\z@}%
                                  {-3.5ex \@plus -1ex \@minus -.2ex}%
                                  {2.3ex \@plus.2ex}%
                                  {\normalfont\large\bfseries}}
\setlength{\parindent}{0pt}

\center % Center everything on the page

%----------------------------------------------------------------------------------------
% HEADING SECTIONS
%----------------------------------------------------------------------------------------

\textsc{\LARGE University of the Witwatersrand}\\[1.5cm] % Name of your university/college
\textsc{\Large School of Computer Science and Applied Mathematics}\\[0.5cm] % Major heading such as course name

%----------------------------------------------------------------------------------------
% TITLE SECTION
%----------------------------------------------------------------------------------------

\HRule \\[0.4cm]
{ \huge \bfseries COMS3008: Parallel Computing Lab Assignment 3}\\[0.4cm] % Title of your document \\
  \large 12 August 2016
\HRule \\[1.5cm]

%----------------------------------------------------------------------------------------
% AUTHOR SECTION
%----------------------------------------------------------------------------------------
\begin{minipage}{1\textwidth}
  \Large \emph By Chalom, J. (711985)\\
\end{minipage}

\vfill % Fill the rest of the page with whitespace

\end{page}

\begin{page}
\lfoot{School of Computer Science and Applied Mathematics}
\clearpage
\setcounter{page}{1}
\pagenumbering{arabic}

\section{General Assumptions and Methodology}
All drawn diagrams were drawn using \url{http://draw.io/}, and all results from empirical analysis, were statistically analysed by the data processing program used in lab 1. Those averaged results were then plotted using Microsoft Excel.\\

\noindent The system used for empirical analysis, had an i3 Ivy-bridge CPU, with four processing units and 8GB of RAM. This system can open up a maximum of 26000 threads of computation for a given program excluding any background and operating system tasks.\\

\subsection{Glossary Of Terms:}
\noindent \textbf{\textit{Trapezoid}} - is defined as the British definition (Trapezium) as a quadrilateral with no sides parallel \\
\noindent \textbf{\textit{IBT}} - is an acronym for integration by parts.\\
\noindent \textbf{\textit{STR}} - is an acronym for Simpson's Trapezoidal Rule.\\
\noindent \textbf{\textit{CTR}} - is an acronym for Composite Trapezoidal Rule.\\

\section{Analytical Solution of Given Integral}
\noindent Let $f(x) = \int_{0}^{20} xe^{-x} dx$\\
\\
\noindent By IBT let $\int fdg = fg - \int gdf$, \\
where $f = x$, $dg = e^{-x} dx$, $df = dx$ and $g = -e^{-x}$.\\

\begin{equation}
\begin{aligned}
    \therefore{} \int xe^{-x} dx = -e^{-x} + \int e^{-x} dx\\
\end{aligned}
\end{equation}
\\
\noindent Let $u = -x$ and $dy = -dx$

\begin{equation}
\begin{aligned}
    \therefore{} -e^{-x} + \int e^{-x} dx &= -e^{-x}x - \int e^{u} du\\
    & = -e^{u} - e^{-x}x + c\\
    & = -e^{-x} - e^{-x} + c
\end{aligned}
\end{equation}
\\

\begin{equation}
\begin{aligned}
    \therefore{} \int_{0}^{20} xe^{-x} dx &= \left[-e^{-x} - e^{-x} + c\right]_{0}^{20} \\
    & = -e^{-20}(20) - e^{-20} + e^{0}(0) + e^{0}\\
    & = 1 - e^{-20}(20) - e^{-20}\\
    & = 1 - \epsilon{}\\
    & \simeq{} 1,00
\end{aligned}
\end{equation}

Note: $\epsilon{} \simeq{} 4,329422607 x 10^{-8}$.
\\
\break
\section{Parallel Trapezoidal Integration}
\subsection{The Trapezium:}
\noindent Let $a$ be the shortest side of a trapezoid (Fig 1), $b$ be the longest side and $h$ be the base of the trapezoid (Fig 1).

\begin{figure}[ht]
\centering
     \includegraphics[width=0.40\textwidth]{trapezium}\\
     Fig 1. An example trapezium
\end{figure}

\subsection{The area of a trapezoid is defined as:}

\begin{equation}
\begin{aligned}
    A &= (a . h) + (\frac{1}{2}(b - a).h)\\
    & = h.(a + \frac{1}{2}(b - a))\\
    & = h.(a + \frac{1}{2}b - \frac{1}{2}a)\\
    & = h.(\frac{1}{2}a + \frac{1}{2}b)\\
    & = \frac{a+b}{2}h
\end{aligned}
\end{equation}
\\
\subsection{Derivation of the rule:}\\
\noindent Let $f(x)$ be a function where $x \in [a,b]$, such that\\
 $a = x_0 < x_1 < ... < x_n = b \in \mathbb{Z}$

\begin{figure}[ht]
\centering
     \includegraphics[width=0.80\textwidth]{approx_graph}\\
     Fig 2. Approximation of a polynomial
\end{figure}
\\
\noindent Note: \\
\begin{equation}
\begin{aligned}
    h &= x_{n+1} - x_n\\
    &= \Delta{}x
\end{aligned}
\end{equation}

\noindent The area of the $n^{th}$ trapezoid is given by:\\

\begin{equation}
\begin{aligned}
    A_n = \Delta{x}\Bigg[\frac{f(x_i) + f(x_{i+1})}{2}\Bigg]
\end{aligned}
\end{equation}\\

\noindent Since $\Delta{x}$ is fixed for each partition as it is assumed that there are a uniform number of trapezoids under $f(x)$,\\

\begin{equation}
\begin{aligned}
    \Delta{x} &= \frac{b-a}{n}\\
    \therefore{A_n} &= \Bigg[\frac{f(x_i) + f(x_{i+1})}{2}\Bigg]
\end{aligned}
\end{equation}\\

\noindent Let $m = n-1$, This implies that\\

\begin{equation}
\begin{aligned}
    \int_a^b f(x) dx &\simeq \sum_{i=0}^{m}A_i\\
    &\simeq \sum_{i=0}^{m} \Delta{x} \Bigg[\frac{f(x_i) + f(x_{i+1})}{2}\Bigg]\\
    &\simeq \Delta{x} \sum_{i=0}^{m} \Bigg[\frac{f(x_i) + f(x_{i+1})}{2}\Bigg]\\
    &\simeq \frac{b-a}{2n} \sum_{i=0}^{m} \Bigg[f(x_i) + f(x_{i+1})\Bigg]\\
\end{aligned}
\end{equation}

\subsection{Method of Implementation:}
\noindent The derivation of the Trapezoidal rule (from equation 8) was used and both serial and parallel implementations were programmed. The serial function's results are the baseline for the analysis. The parallel decomposition was a standard openMP parallel for directive where a reduction on the approximation was specified.\\

\noindent The error calculated in these experiments is the result of subtracting the analytical result derived in section 2 by the final approximation at each iteration. If the error is negative then the specific step approximated the integral under the analytical result otherwise it was over the result.\\

\noindent Three result sets were captured. One was the serial implementation where only the main thread was used. This algorithm starts at 1 partition and increments by 100 for 260 iterations. These are the result data points generated.\\

\noindent In the two parallel result sets, one looked at a fixed number of threads (4 threads) and an incrementing number of partitions and the other looked at a fixed number of partitions (1000000) and an incrementing the number of threads from 1 to 26000 threads, at 100 increments 260 times.\\

\noindent All results included the time each specific trapezoidal approximation took to complete, either the number of threads or the number of partitions, the error, and the absolute error.

\subsection{Implementation Results:}\\
\noindent The results of this empirical experiment show that this parallel decomposition of the Trapezoidal Rule is not as efficient or fast as the serial implementation used.

\begin{figure}[ht]
\centering
     \includegraphics[width=1.00\textwidth]{TrapezoidalLineGraphCompletionTimes}\\
\end{figure}\\

\noindent Another result shows that as the number of threads of the parallel implementation increase so does the completion time of the rule (At a liner rate).\\

\begin{figure}[ht]
\centering
     \includegraphics[width=1.00\textwidth]{TrapezoidalThreadCountTime}\\
\end{figure}\\

\noindent The error of both the parallel implementation of the trapezoid and its serial counterpart have very similar exponentially decreasing errors.

\begin{figure}[ht]
\centering
     \includegraphics[width=1.00\textwidth]{TrapezoidalSerialError}\\
     Trapezoidal Serial Error
\end{figure}

\begin{figure}[ht]
\centering
     \includegraphics[width=1.00\textwidth]{TrapezoidalParallelError}\\
    Trapezoidal Parallel Error
\end{figure}\\

\break
\subsection{Implementation Analysis:}
\noindent The results produced show that a different parallel decomposition would be required as the one tested here are far less efficient or desirable compared to the results produced by the serial implementation. There is also a problem related to iso-efficiency where there is a linear increase in the time complexity of the parallel implementation as the number of threads increases. The respective errors of both the serial and the parallel are comparable and have exponentially decreasing error as the number of partitions increases.

\section{Parallel Monte Carlo Integration}
\subsection{Method of Implementation:}
\noindent A boundary was setup which went from $a = 0$ to $b = 20$ on the $X$ axis and from $c$ to $d$ on the $Y$ axis. $c,d$ are the results of $c = f(a) + 1$ and $d = f(b) + 1$. This produces a rectangle where part of its area is the integral of the function defined in section 2. Random $x$ and $y$ values were generated for every particle in the experiment. They were generated in the range $[a,b]$ and $[c,d]$ respectively. The experiment was conducted using a purely serial function and also a parallel function. The serial function's results are the baseline for the analysis. Each increment taken was repeated 25 times and after the experiment was run averages and other statistics were taken and those results were used to plot the graphs in the results subsection.\\

\noindent The error calculated in these experiments is the result of subtracting the analytical result derived in section 2 by the final approximation at each iteration. If the error is negative then the specific step approximated the integral under the analytical result otherwise it was over the result.\\

\noindent The serial implementation iterated through the number of particles per experiment (from 1 to 26000, at 100 increments) and returned the approximation, an error, the time taken by each calculation and an absolute error.\\

\noindent Both Parallel implementations made used of the same decomposition using openMP's parallel for directive. The private variable for each execution unit was the result of the randomly generated point as the points were never stored in memory for later use. There were shared variables where were $c$ and $d$. A reduction operation was also done on the only variable used outside of the parallel code block, which was variable counting the number of points which occurred below the graph (in side the area of the graph), as the total number of points was known before-hand.\\

\noindent The one parallel experiment locked the number of threads (4 threads) and incremented the number of particles which to generate from 1 particle to 26000 particles incrementing by 100 each time. The other one locked the number of particles at 1000000 and incremented the number of threads from 1 thread to 26000 threads, incrementing by 100 at each step.

\subsection{Implementation Results:}
\begin{figure}[ht]
\centering
     \includegraphics[width=1\textwidth]{MonteCarloTimes}
\end{figure}\\

\noindent The results of this empirical analysis show that initially the parallel implementation of the Monte Carlo Integration algorithm is very unpredictable versus the serial version. As the number of partitions increase the parallel implementation's time complexity tends towards imitating the serial one.\\

\begin{figure}[ht]
\centering
     \includegraphics[width=1\textwidth]{MonteCarloErrors}
\end{figure}\\

\noindent The error trends of both the serial and parallel implementations are comparable. The almost random nature and oscillation between positive and negative errors is noted. This shows that there is both under and over estimation occurring at different increments.\\

\begin{figure}[ht]
\centering
     \includegraphics[width=1\textwidth]{MonteCarloThreadTimes}
\end{figure}\\

\noindent Again it can be seen that as the thread count increases so does the time complexity of the parallel algorithm.

\break
\subsection{Implementation Analysis:}
Again the results show that the decomposition used in this analysis did not have desired results. The serial equivalent was by far more reliable and faster. These results can also be due to system interference or problems in the respective implementations. The reliability of this parallel decomposition is also in question. It can be seen from the completion times graph that this implementation is produces unstable results. Again iso-efficiency can be observed where the increase of threads produces a linear trend.

\section{Conclusion}
These experiments show that the current parallel decompositions are not effective in improving performance of these approximations of a finite integral. Better parallel decompositions and implementations must be used. Some experimental methods should also be evaluated and changed as necessary to provide more accurate results.

\break
\section{Appendix A: Alternative Derivation of Trapezoidal Rule}
\subsection{Derivation of the Trapezoidal Rule (STR):}\\
\noindent Let $P_n$ be a polynomial function of n terms.\\

\begin{equation}
\begin{aligned}
    \int_{a}^{b} P_n(x) dx &\simeq{} \int_{a}^{b} f[a]dx + \int_{a}^{b} f[a,b](x-a)dx\\
    &\simeq{} (b-a)f(x) + (\frac{f(b)-f(a)}{(b-a)}).\frac{1}{2}(x-a)^{2}]_{a}^{b}\\
    &\simeq{} (b-a)f(a) + (\frac{f(b)-f(a)}{(b-a)}).\frac{1}{2}(b-a)^{2}\\
    &\simeq{} \frac{b-a}{2}(f(a)+f(b))
\end{aligned}
\end{equation}

\begin{equation}
\begin{aligned}
    Error = \frac{-(b-a)^{3}}{12}f^{''}(\phi)
\end{aligned}
\end{equation}
\\

\subsection{Derivation of the Composite Trapezoidal Rule (CTR):}\\
\noindent For sub-intervals: $a = x_0 < x_1 < ... < x_n = b$
\\

\begin{equation}
\begin{aligned}
    n &= \frac{b-a}{m}\\
    & = \Delta{x}\\
    & = (x_1 - x_0)
\end{aligned}
\end{equation}
Note: $\Delta{x}$ is fixed.
\\
\begin{equation}
\begin{aligned}
    \int_a^b f(x) dx &= \int_{a=x_0}^{x_1} f(x) + \int_{x_1}^{x_2} f(x) + ... + \int_{x_{n-1}}^{x_n=b} f(x)\\
    &\simeq{} (\frac{x_1 - x_0}{2})[f(x_0) + f(x_1)] + ... + (\frac{x_m - x_{m-1}}{2})[f(x_{m-1}) + f(x_m)]\\
    &\simeq{} \frac{n}{2}[f_0 + 2f_1 + 2f_2 + ... + 2f_{m-1} + f_m]
\end{aligned}
\end{equation}
\\

\begin{equation}
\begin{aligned}
    Error_C_T_R = \frac{(b-a)^3}{12m^2}$ $f^{0}(\phi)$, $\phi \in [a,b]
\end{aligned}
\end{equation}

\end{page}

\end{document}


\documentclass[11pt]{beamer} %Add 'handout' in the options for printing

\usepackage{beamercolorthemewolverine}
\usetheme{AnnArbor}

\usepackage[english]{babel}
\usepackage{amsmath,amssymb}
\usepackage{commath}
\usepackage{amssymb,amscd,amsfonts,amsbsy}
\usepackage{enumerate}
\usepackage{epsfig}
\usepackage{pgfpages}
\usepackage{graphics}
\usepackage{graphicx}
\usepackage{multicol}
\usepackage{layouts}
\usepackage[center]{caption}
\usepackage{natbib}
\usepackage{movie15}
\usepackage{etex}
%\usepackage{dsfont}
\usepackage{embedfile}
%\usepackage[natbib=true, bibstyle=authoryear, citestyle=authoryear-comp]{biblatex}
%\usepackage{apacite}

\embedfile{Slides_Template.tex}

\setbeamertemplate{navigation symbols}{}%remove navigation symbols

\graphicspath{ {./figs/} }

% Figures within a column...
\makeatletter
\newenvironment{tablehere}
{\def\@captype{table}}
{}
\newenvironment{figurehere}
{\def\@captype{figure}}
{}
\makeatother

\newcommand{\beginbackup}{
   \newcounter{framenumbervorappendix}
   \setcounter{framenumbervorappendix}{\value{framenumber}}
}
\newcommand{\backupend}{
   \addtocounter{framenumbervorappendix}{-\value{framenumber}}
   \addtocounter{framenumber}{\value{framenumbervorappendix}}
}

%---------------------------------------------------------------------------
\setbeamercolor{alerted text}{fg=red}
\setbeamertemplate{frametitle}
{\vskip-18.5pt
  \leavevmode
  \hbox{%
  \begin{beamercolorbox}[wd=\paperwidth,ht=1.35ex,dp=1.35ex]{frametitle}%
    \raggedright\hspace*{2em}\vspace{-7pt}\small{\insertframetitle}
  \end{beamercolorbox}
  }%
}
\def\newblock{\hskip .11em plus .33em minus .07em}

\setbeamercovered{dynamic}

\newcommand{\RR}{{\mathbb{R}}}
\newcommand{\NN}{{\mathbb{N}}}
\newcommand{\ZZ}{{\mathbb{Z}}}
\newcommand{\CC}{{\mathbb{C}}}
\newcommand{\eps}{\varepsilon}
\newcommand{\bp}{\noindent {\it Proof}.\,\,\,}
\newcommand{\ep}{\hfill$\Box$ \vskip 0.08in}
\newcommand{\dint}{\int\!\!\!\int}
\newcommand{\vs}{\vskip 0.5cm}
\newcommand{\po}{{\partial\Omega}}
\newcommand{\meanint}{{\int{\mkern-19mu}-}}
\def\ring{\mathaccent"0017 }

\title[Research Summary]{Presentation Title}
\author[Author Name] {}

\institute[UM]{}
\date[\today]{}

\begin{document}
\frame{ \vspace{-1cm}

\titlepage \vspace{-2.70cm}
\centering
\begin{columns}
\column{.25\textwidth}
\begin{figure} \centering \includegraphics[width=.75\columnwidth]{MichiganSeal} \end{figure}
\column{.5\textwidth} \begin{center}
\normalsize{Your name here} \vspace{4pt} \\ \footnotesize{Your lab here} \vspace{4pt}\\
\footnotesize{University of Michigan, Ann Arbor} \vspace{4pt} \\ \scriptsize{Department of Stuff Engineering} \vspace{4pt} \end{center}
\column{.25\textwidth}
\begin{figure} \centering \includegraphics[width=.75\columnwidth]{MElogo} \end{figure}
\end{columns}
\vspace{.45cm}
\begin{center}
\footnotesize {\today} \vspace{-.45cm}
\end{center}
\vspace{\fill}
}

\begin{frame}
\frametitle{Here's my first frame title}

\begin{center}
{\small Here are some pictures of bubbles}
\end{center}

\begin{figure}
\includegraphics[width=.3\textwidth]{Undex} \hspace*{.2cm}
\includegraphics[width=.3\textwidth, height=.65\textheight]{Ultracav} \hspace*{.2cm}
\includegraphics[width=.3\textwidth, height=.65\textheight]{propeller}\\
\hspace*{-.6cm}
\alert{Underwater Explosions \quad Ultrasound Probes \qquad Rotating Propellers}
\end{figure}
{\small I put some words here for filler when I'm giving a real presentation.  Some audiences like that, I'm not sure why.}
\end{frame}

\begin{frame}
This slide can act as an example of sequentially changing pictures.
\begin{itemize}
\item Bullet points are nice.
\item You don't have to say much to take up space.
\item If you have dynamic slides like this, use the [handout] option in the document class at the top to condense animations for printing.
\end{itemize}
\vspace{-.5cm}
\begin{figure}
\invisible<1>{\includegraphics<1>[width=.99\textwidth]{CavEvent1}}
\includegraphics<2>[width=\textwidth]{CavEvent1}
\includegraphics<3>[width=\textwidth]{CavEvent2}
\includegraphics<4>[width=\textwidth]{CavEvent3}
\includegraphics<5>[width=\textwidth]{CavEvent4}
\includegraphics<6>[width=\textwidth]{CavEvent5}
\only<7->{ \includegraphics[width=.95\textwidth]{BleedMechs2}}
\end{figure}
\end{frame}

\begin{frame}\frametitle{Normally I talk about my research here}
{\small
I kept this slide because it shows how to imbed a video into the slide.  I recommend mp4 because it works on most platforms.
\begin{minipage}{.58\textwidth}
\begin{itemize}
\item bullet 1
\item bullet 2
\item etc...
\end{itemize}
\end{minipage}
}
\begin{minipage}{.4\textwidth}
%\begin{figure} \vspace*{-.15cm}
%\includemovie[poster,text={\small(Loading Video...)},rate=.5]{\textwidth}{.45\textwidth}{histotripsy.mp4}
%\end{figure}
\%\end{minipage}
\vspace*{0.5cm}
{\small
\uncover<3>{
\\To organize this page, I used the minipage environment, it's handy for slides.
\begin{minipage}{.5\textwidth}
\begin{itemize}
\item Here, talk about the pictures and stuff
\item That pictures of a bloody kidney
\item You can put whatever picture you want there.
\end{itemize}
\vspace*{\fill}
\end{minipage}
\begin{minipage}{.45\textwidth}
\begin{figure}
\includegraphics[width=\textwidth]{Kidney_Bleed}
\end{figure}
\end{minipage}
}}
\end{frame}

\section{Methods}
\subsection{Bubble Simulation}

\frame{
\vspace{.2cm}
\uncover<1->{\alert<1->{The Keller-Miksis Equation} \scriptsize{
\begin{align*}
	 \left(1-\frac{\dot{R}}{Ma}\right) R \ddot{R} + \frac{3}{2}\left(1-\frac{\dot{R}}{3 Ma}\right)\dot{R}^2 = \frac{R}{Ma}\left(\left(1+\frac{2}{We}\right) \frac{3\gamma}{R^{3\gamma+1}}\dot{R}+\frac{2 \dot{R}}{We R^2}  +  \dot{\tau}_{RR}  \right)
\end{align*}\vspace{-.4cm}
\begin{align*}
+ \left(1+\frac{\dot{R}}{Ma}\right)\left[\left(1+\frac{2}{We}\right)\frac{1}{R^{3\gamma}}-\frac{2}{We R}  + \tau_{RR}
	 -1-p_a(t)-\frac{R}{Ma} \dot{p}_a\right] \end{align*}}} \\
\pause
\uncover<2->{ \normalsize{\alert<2->{Kelvin-Voigt} model for stress, $\tau_{rr}(R)$.  \normalsize
\vspace{-.85cm}
\scriptsize{
\begin{columns}
\column{.7\textwidth}
\begin{align*}
    \\ \tau_{rr} = -\frac{4}{3 Ca}\left(1-\frac{1}{R^{3}}\right)-\frac{4}{Re}\left( \frac{\dot{R}}{R}\right)
 \end{align*}
 \column{.3\textwidth}
\begin{figure}
	\centering \vspace{.15cm} \invisible<1>{
\includegraphics<1->[width=\columnwidth]{Voigt}}
\end{figure}
 \end{columns}
 }}}

 \vspace{\fill} \pause
\vspace{-.3cm} \uncover<3->{
 \scriptsize \begin{center}
		\begin{tabular}{l l c l}
		  Parameter: & Dimensional & & Dimensionless \\ \hline
%		  Viscosity & $\mu=.015 \, \, \,  (Pa\cdot s)$ & $\mapsto$ & $Re=\rho R_0 c / \mu =2/3$ \\
		  Elasticity & $G=5,\alert{100},1000$ \,  $(kPa)$ & $\mapsto$ & $Ca=c^2 \rho / G=20,1,0.1$ 	\\
%		  Surface Tension & $s=.05\, \, \, (N/m)$ & $\mapsto$ & $We=\rho R_0 c^2 / s = 2$ \\
%		  Speed of Sound & $c_0=1570\, \, \, (m/s)$ & $\mapsto$ & $Ma=c_0/c=157$ \\
%		  Relaxation Time & $t_c=0-1$  $(s)$ & $\mapsto$ &	$De=\lambda c / R_0=0-10^7$ \\
		  Initial Radius & $R(0)=0.1-2 \, \, \, (\mu m)$  & & \\
		  Adiabatic Index & $ $  $ $ & $ $ &	$\gamma=1.13,1.4$ \\

		\end{tabular}\\ \end{center}  \vspace{-.2cm}
		 \hspace{1.15cm}$R_0=1$ $\mu m$, $c = \sqrt{p_{atm}/\rho}$, $We=\rho R_0 c^2 / s$, $Re=\rho R_0 c / \mu$, $Ca= c^2\rho/G$, $Ma=c_0/c$}
		 \\ \hspace{1.15cm}\citep{HollandApfel90,YangChurch05}
}

\section{Conclusion}
\frame{
\frametitle{Summary and conclusions}
\begin{itemize}
\uncover<1->{\alert<1>{\item \LaTeX is sweet
\begin{itemize}
\item I've been recycling this presentation script since undergrad.
\end{itemize}} \vspace{.25cm}} %\pause
\uncover<2->{\alert<2>{\item If you want to learn more about LaTeX, there's tons of info online. \vspace{.25cm}}}
\uncover<3->{\alert<3>{\item You can use this template as a starting point for building your own presentations. }}\vspace{.25cm}
\vspace{\fill} \vspace{-.1cm}
\end{itemize}
}

\section{Citations}
\bibliography{mybib}
\bibliographystyle{authordate1}

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

\begin{frame} \vspace{\fill} \begin{center} \Huge
Thanks a lot!\\ \vspace{\fill} Questions?
\end{center} \vspace{\fill} \end{frame}

\end{document}

%  Template Created by Brandon Patterson - awesome@umich.edu


\documentclass[11pt,a4paper]{article}

% These are extra packages that you might need for writing the equations:
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{booktabs}
\usepackage{hyperref}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{graphicx}
\usepackage{subfig}
\usepackage{float}

\lstset {language=C++,
		 basicstyle=\ttfamily,
         keywordstyle=\color{blue}\ttfamily,
         stringstyle=\color{red}\ttfamily,
         commentstyle=\color{purple}\ttfamily,
         morecomment=[l][\color{magenta}]{\#},
       	 basicstyle=\tiny}

% You need the following package in order to include figures in your report:
\usepackage{graphicx}

% With this package you can set the size of the margins manually:
\usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm]{geometry}

\begin{document}

% Enter the exercise number, your name and date here:
\noindent\parbox{\linewidth}{
 \parbox{.25\linewidth}{ \large HPCSE I, Exercise 07 }\hfill
 \parbox{.5\linewidth}{\begin{center} \large Beat Hubmann \end{center}}\hfill
 \parbox{.2\linewidth}{\begin{flushright} \large Nov 16, 2018 \end{flushright}}
}
\noindent\rule{\linewidth}{2pt}

\section{Question 1: conv2d}

Done as instructed; tests passed.

% \begin{figure}[ht]
% \begin{center}
% \includegraphics[scale=0.5]{results.png}
% \end{center}
% \caption{Parallel Monte Carlo integration on Euler compute cluster.}
% \label{fig1}
% \end{figure}

\section{Question 2: Pararellelization}

Done as instructed and tested for correctness. Marginal improvement.\\

% \begin{figure}[H]
% \begin{tabular}{ccccc}
% \subfloat[Component 0]{\includegraphics[width = 1.1in]{lin_component_0.eps}} &
% \subfloat[Component 1]{\includegraphics[width = 1.1in]{lin_component_1.eps}} &
% \subfloat[Component 2]{\includegraphics[width = 1.1in]{lin_component_2.eps}} &
% \subfloat[Component 3]{\includegraphics[width = 1.1in]{lin_component_3.eps}} &
% \subfloat[Component 4]{\includegraphics[width = 1.1in]{lin_component_4.eps}} \\
% \subfloat[Component 5]{\includegraphics[width = 1.1in]{lin_component_5.eps}} &
% \subfloat[Component 6]{\includegraphics[width = 1.1in]{lin_component_6.eps}} &
% \subfloat[Component 7]{\includegraphics[width = 1.1in]{lin_component_7.eps}} &
% \subfloat[Component 8]{\includegraphics[width = 1.1in]{lin_component_8.eps}} &
% \subfloat[Component 9]{\includegraphics[width = 1.1in]{lin_component_9.eps}}
% \end{tabular}
% \caption{10 Principal components of MNIST data set obtained from single linear hidden layer.}
% \label{fig:1}
% \end{figure}

\section{Question 3: Tweaking hyper-parameters}

I first of all changed the batch size from 512 to 128 and then went with the
following architecture:

\begin{itemize}
\item Conv2D layer: Filter size: $6 \times 6$, filter depth: $8$, strides: $2, 2$, output: $12\times12\times8$
\item LeakyReLU
\item Conv2D layer: Filter size: $5 \times 5$, filter depth: $16$, strides: $1, 1$, output: $8\times8\times16$
\item LeakyReLU
\item Conv2D layer: Filter size: $4 \times 4$, filter depth: $32$, strides: $1, 1$, output: $5\times5\times32$
\item LeakyReLU
\item Conv2D layer: Filter size: $3 \times 3$, filter depth: $64$, strides: $2, 2$, output: $2\times2\times64$
\item LeakyReLU
\item Conv2D layer: Filter size: $2 \times 2$, filter depth: $128$, strides: $1, 1$, output: $1\times1\times128$
\item LeakyReLU
\item Flatten to fully connected linear layer: Size: 128
\item LeakyReLU
\item Fully connected linear layer: Size: 10
\item SoftMax
\end{itemize}

Training over the same 50 epochs and overfitting a bit, a test set MSE of 0.053001
and a test set precision of 0.985377 were achieved with early stopping after 42 epochs.
This is quite a bit better than the untuned test results after 50 epochs (MSE:0.107567 precision:0.966900).

% \begin{figure}[H]
% \begin{tabular}{ccccc}
% \subfloat[Component 0]{\includegraphics[width = 1.1in]{component_0.eps}} &
% \subfloat[Component 1]{\includegraphics[width = 1.1in]{component_1.eps}} &
% \subfloat[Component 2]{\includegraphics[width = 1.1in]{component_2.eps}} &
% \subfloat[Component 3]{\includegraphics[width = 1.1in]{component_3.eps}} &
% \subfloat[Component 4]{\includegraphics[width = 1.1in]{component_4.eps}} \\
% \subfloat[Component 5]{\includegraphics[width = 1.1in]{component_5.eps}} &
% \subfloat[Component 6]{\includegraphics[width = 1.1in]{component_6.eps}} &
% \subfloat[Component 7]{\includegraphics[width = 1.1in]{component_7.eps}} &
% \subfloat[Component 8]{\includegraphics[width = 1.1in]{component_8.eps}} &
% \subfloat[Component 9]{\includegraphics[width = 1.1in]{component_9.eps}} i\\
% \subfloat[-Component 0]{\includegraphics[width = 1.1in]{component_10.eps}} &
% \subfloat[-Component 1]{\includegraphics[width = 1.1in]{component_11.eps}} &
% \subfloat[-Component 2]{\includegraphics[width = 1.1in]{component_12.eps}} &
% \subfloat[-Component 3]{\includegraphics[width = 1.1in]{component_13.eps}} &
% \subfloat[-Component 4]{\includegraphics[width = 1.1in]{component_14.eps}} \\
% \subfloat[-Component 5]{\includegraphics[width = 1.1in]{component_15.eps}} &
% \subfloat[-Component 6]{\includegraphics[width = 1.1in]{component_16.eps}} &
% \subfloat[-Component 7]{\includegraphics[width = 1.1in]{component_17.eps}} &
% \subfloat[-Component 8]{\includegraphics[width = 1.1in]{component_18.eps}} &
% \subfloat[-Component 9]{\includegraphics[width = 1.1in]{component_19.eps}}
% \end{tabular}
% \caption{10 Principal components of MNIST data set obtained from three hidden layers with $\tanh$ activation function.}
% \label{fig:2}
% \end{figure}

\end{document}


\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{amssymb}
\pagestyle{plain}
\usepackage{epsf}
\usepackage{epsfig}
\usepackage{graphics}      % standard graphics specifications
\usepackage{graphicx}
\usepackage[margin=2.3cm]{geometry}
%\usepackage[top=1.8cm, bottom=1.8cm, right=2.2cm, left=2.2cm]{geometry}
\linespread{1.2}
\usepackage{hyperref}

%\textheight 22cm
%\textwidth  16cm
%\hoffset= -0.6in
%\voffset= -0.5in
%\setlength{\parindent}{0cm}
%\setlength{\parskip}{10pt plus 2pt minus 2pt}
%\pagenumbering{roman}
%\setcounter{page}{-9}

\newcommand{\be}{\begin{equation}}
\newcommand{\ee}{\end{equation}}
\newcommand{\bd}{\begin{displaymath}}
\newcommand{\ed}{\end{displaymath}}
\begin{document}

\title{Write, Run, and Publish Script M-Files}
\date{}
\maketitle

\section{Create m-files}
\begin{enumerate}
\item Open Matlab. Notice the sections of the window: editor, command line, history, data window, etc.

\item Create a new folder and change the MATLAB current directory to the folder you created.

%\item Follow the instructions on \href{http://www-users.math.umd.edu/~jow/206/tutorial/ScriptMFile_1.shtml}{Script M-Files} to create your own script M-file:
\item There are three ways to open a new m-file:\\
(1) Find and click this icon \includegraphics{newMfile.jpg}\\
(2) Go to File $-$ New $-$ Blank M-File\\
(3) Type \fbox{edit \textit{filename}.m} in the Command Window%; note that for function m-files, \textit{filename} must match the name of the function you are going to write in this m-file.
\end{enumerate}

%\section{Write function m-files}
%\begin{enumerate}
%\item In the blank M-file you just opened in the editor:
%\begin{itemize}
%\item Type \fbox{function f = myFunc(x, y)} as the first line. If your file name is, e.g. "lab4func.m", type \fbox{function f = lab4func(x, y)} instead. \textbf{The function name has to match the file name.} By including this first line in the M-file, you are telling MATLAB that this M-file is a user-specified function. A function file is not executable by itself; it can only be called in other commands.
%\item Then choose a function, e.g. $f(x, y) = x^2 + y$, type \fbox{f = x.*x + y } as the content of the M-file.
%\end{itemize}
%\item Save your M-file; if you chose method (1) or (2) to open your blank M-file, now you'll need to give your M-file a name which matches your function name.
%\end{enumerate}
%Note that you should use \fbox{.*} instead of \fbox{*} because we want the function input \fbox{x} and \fbox{y} to be vectors, and the dot-operator \fbox{.} is meant to repeat operations on the members of the vector. See \href{http://www.astro.umd.edu/~cychen/MATLAB/ASTR121/labIntro/html/VecOpr.html#4}{Basic Operations: Dot-Operators} for more details.

\section{Write script m-files}
\begin{enumerate}
\item Now create another blank M-file to read and plot the function you just wrote:
\begin{itemize}
\item In this file, you need to \\
(a) generate a vector $x$, e.g. \fbox{x = linspace(-1, 1, 200);} \\
(b) generate a number $y$, e.g. \fbox{y = 5;}\\
(c) generate a vector $out$ to be a function of $x$ and $y$, e.g. \fbox{out = x.*x + y;}\\
(d) make a plot of $out$ vs. $x$, e.g. \fbox{plot(x, out)}
\item You can also add documentation for your plot using \fbox{xlabel}, \fbox{ylabel}, \fbox{title}, or \fbox{legend}.
\end{itemize}
\item Save this file. This time, name your M-file something like ``myPlot.m" or ``myScript.m", whatever you want, but NOT ``plot.m" since ``plot" is already a MATLAB internal function.

\end{enumerate}

\section{Run script m-files}
\begin{itemize}
\item There are three ways to test your script M-file:\\
(1) Type \fbox{myPlot} or \fbox{myScript} (whatever your filename is) at the command line\\
(2) Go to Cell $-$ Evaluate Current Cell\\
(3) Find and click this icon \includegraphics{runMfile.jpg} on the Toolbar in the Editor window
\item Do you see a figure window popping up?
\end{itemize}

%\item If you can't get through Steps 3$-$5, don't worry. Go to the \href{http://www.astro.umd.edu/~cychen/MATLAB/ASTR121/LabBB/}{Lab04} website and download two files: $<$functionExample.m$>$ and $<$plotExample.m$>$. Save them into the folder you created on the desktop. Open each of these files into the editor in MATLAB and read through them. Pay close attention to the comment made at the end of each line telling what that command instructs MATLAB to do. Then run $<$plotExample.m$>$ by typing \fbox{plotExample} in the Command window or click \includegraphics{runMfile.jpg} in the Editor window. What do you see?
%\item Run $<$lab1plot.m$>$ by typing ``lab1plot" at the command line. Note that you may have to change the matlab current directory to the folder you created in order for matlab to find those files when you execute this command at the prompt.

%\item Now, try a few of the commands inside these matlab function files out for yourself at the command line. See $<$simpleCommands.html$>$ for guidance.

%\item Now try making adjustments to these files and run them again with the same command as before. Note any differences. What change did you make and what effect did it have? I suggest simple changes each time, so that you can tell exactly what the change was. (We call this changing one parameter at a time in physics and it's useful in many areas including programming!) Note that you will need to save the file after each change you make before it will take effect when you run the function file at the command line.

%\item Now let's turn the heat up. Open the Editor window with your function M-file and choose a new function to define in this function file. Examples: $\sin()$, $\cos()$, $\exp()$, $1/x$, etc. Save and run your script M-file. (\textit{Note:} you may need the dot-operator for some arithmetic operations. On the other hand, some MATLAB internal functions (like $\sin()$, $\cos()$, $\log()$) are vectorized, which means they operate automatically over each member of an array without the need for an explicit loop.)

\section{Publish script m-files}
\textit{MATLAB Product Documentation:} MATLAB software enables you to publish your MATLAB code quickly, so you can describe and share your code with others, even if they do not have MATLAB software. You can publish in various formats, including HTML, XML, and LaTeX. \textit{(Typically, the default output file format is HTML)}\\

There are two ways to publish your script M-file:
\begin{itemize}
\item Go to File $-$ Publish \textit{filename}.m
\item Find and click this icon \includegraphics{pubMfile.jpg} on the Toolbar in the Editor window
\end{itemize}

Both the MATLAB code in your script M-file and the results of running the code (e.g., output to the Command Window, figures created or modified by the code) will be included in the published file. Therefore, this is convenient for handing in your lab assignments!
\end{document}


\documentclass[12pt]{article}

% packages
\usepackage{setspace}
\usepackage{array}
\usepackage[margin=0.75in]{geometry}
\usepackage{amsmath,bm}
\usepackage{amssymb}
\usepackage{bbold}
\usepackage{physics}
\usepackage{xcolor}
\usepackage{indentfirst}
\usepackage{enumerate}
\usepackage{mathtools}
\usepackage{fancyhdr}
\usepackage{hyperref}

\newenvironment{changemargin}[2]{%
\begin{list}{}{%
\setlength{\topsep}{0pt}%
\setlength{\leftmargin}{#1}%
\setlength{\rightmargin}{#2}%
\setlength{\listparindent}{\parindent}%
\setlength{\itemindent}{\parindent}%
\setlength{\parsep}{\parskip}%
}%
\item[]}{\end{list}}

\allowdisplaybreaks

\title{Project 2: Variational Quantum Eigensolver: Constructing Potential Energy Surfaces for Small Molecules }
\date{}

\begin{document}

\maketitle

\section*{Introduction}

Currently, the Variational Quantum Eigensolver (VQE)~\cite{Peruzzo2014}
is the most feasible technique for solving the electronic structure problem on a near-term
noisy quantum computer. Yet, one needs to understand that VQE is a quite general framework that requires several choices to be maid by the user
to be efficient. In what follows, we will illustrate state-of-the-art techniques to make VQE efficient for obtaining potential energy surfaces (PESs)
for small molecules.

A set of molecules suggested for investigation is: H$_2$, LiH, H$_4$, H$_2$O, N$_2$. They are of the increasing level of difficulty. A simple
qualitative rule from chemistry is: the system PES is harder to calculate accurately if more chemical bonds are destroyed at the same time.
Therefore, H$_2$ and LiH are the easiest systems (single bond), H$_4$ and H$_2$O are harder (two bonds), and N$_2$ is the hardest (triple bond).
(Actually, 1-2\% of the entire worldwide energy is spent on breaking N$_2$ bonds to make ammonia (NH$_3$) that is turned into fertilizers.
Thus being able to model such processes is a necessary step for computational development of new catalysts for the ammonia production.)
As a smaller correction to this qualitative picture of computational hardness, one can add a number of electrons in the system, the larger the number
of electrons the more computationally demanding is the study. Thus, LiH is harder than H$_2$ and H$_2$O is harder than H$_4$.

Another qualitative indicator of hardness is whether classical computing approximate methods for electronic structure that have a polynomial
scaling are able to treat the system with chemical accuracy (1 kcal/mol). Turns out, if one breaks more than a single bond state-of-the-art approaches
like coupled cluster single and double (CCSD) cannot achieve chemical accuracy for all points of PES. Thus, quantum computing methods based
on VQE were thought as a viable alternative to treat such so-called strongly correlated systems.

\subsection*{More on the topic:}

\begin{itemize}
\item Lecture 1: Electronic Structure \url{https://www.youtube.com/watch?v=0cYq9yJFYkc}
\item Lecture 2: Second Quantization \url{https://www.youtube.com/watch?v=_q4j9_D6AEg}
\item Lecture 3: Fermion-qubit Mappings \url{https://www.youtube.com/watch?v=W8SW3qp3RzY}
\item Lecture 4:  Variational Approach \url{https://www.youtube.com/watch?v=bX5Y8mPa9oQ}
\item Lecture 5:  VQE \url{https://www.youtube.com/watch?v=UBkb54_MOLo}
\item Lecture 6:  Unitary Transformations \url{https://www.youtube.com/watch?v=981jc3Xdgvc}
\item Lecture 7:  Measurements \url{https://www.youtube.com/watch?v=U4xX5251b-E}
\item Lecture 8:  Constraints and Symmetry Projections \url{https://www.youtube.com/watch?v=mhlgldoCfx4}
\end{itemize}

\section*{Your task} \label{sec:tasks}

Using H$_2$ and H$_2$O examples,
we will illustrate main steps of the VQE process from the setup of the Hamiltonian till obtaining the circuit for the IBM quantum computer.
Your task will be to repeat those steps for one or two other molecules so that you can obtain the ground state PES of your molecules
both on a classical computer using classical methods and VQE simulator, and on a quantum computer.

Recommended bonds elongation for H$_4$ and H$_2$O are depicted in Fig.~\ref{fig:Rch}.
Tequila is a free software package where all calculations will be illustrated.

\begin{figure}
    \begin{center}
        \includegraphics[width=0.5\linewidth]{Rch.pdf}
    \end{center}
    \caption{Recommended distortions: H$_2$O, symmetric OH bond length changes around $R$(O-H)=0.75 \AA~and fixed
    $\angle$ HOH = 107.6$^\circ$; H$_4$, change of $\beta$ in the range $[85^\circ,95^\circ]$ keeping planar configuration with fixed $R$ = 1.738 \AA.}
    \label{fig:Rch}
\end{figure}

\subsubsection*{Step \#0: Setting up your computer environment}

Instructions: \texttt{S0\_Setup.pdf}.

\subsubsection*{Step \#1: Generating PES using classical methods}

Hartree-Fock (HF), Configuration Interaction Singles and Doubles (CISD),
Coupled Cluster Singles and Doubles (CCSD), and the exact answer within the chosen basis Full Configuration Interaction (FCI)~\cite{Helgaker}.
All these methods can easily be used for all molecules in minimal atomic basis, STO-3G.
Comparing accuracy of PES generated for different species using different methods
you can see the difference between weakly correlated (H$_2$, LiH)
and strongly correlated (H$_4$,H$_2$O, N$_2$) systems. Even though the FCI method provides the exact answer it
scales exponentially with the number of basis functions and thus cannot be used for larger systems (system size can be either correlated
with the number of electrons or number of basis functions). All approximated methods (HF, CISD, CCSD) scale polynomially with the system
size but fail to deliver chemical accuracy along PESs.\\

Illustrative code: \texttt{S1\_Classical\_Methods.ipynb}.

\subsubsection*{Step \#2: Generating the qubit Hamiltonian}

To proceed to VQE one needs to generate the qubit Hamiltonian, the easiest path is via first
generating the electronic Hamiltonian in the second quantized form and then transform it into the qubit form using one of
the fermion-to-qubit transformations: Jordan-Wigner or Bravyi-Kitaev~\cite{Seeley:2012/jcp/224109}.
Next, some qubit operators can be substituted by numbers ($\pm 1$)
because their states are stationary for the specific electronic state (e.g. ground state). This reduction is very useful for fitting larger problem in
a fewer qubit description and is based on Hamiltonian symmetries, which are discussed in Refs.~\cite{Bravyi:2017wba,Yen:2019bs,Setia:2019uz} \\

Illustrative code: \texttt{S2\_Hamiltonian\_gen.ipynb}.

\subsubsection*{Step \#3: Unitary transformations}

There are three main paths : 1) historically first, the unitary coupled cluster (UCC) approach has originated from the ideas of the classical
electronic structure theory~\cite{Romero2018,Hempel2018},
2) hardware-efficient, direct optimization of gates and their sequence~\cite{Kandala:2017/nature/242},
and 3)  the qubit coupled cluster (QCC) based approach~\cite{Ryabinkin2018}.
We will illustrate the first and third approaches, since the second one frequently runs into difficulties with energy optimization~\cite{McClean2018}.
Among the two, even though UCC follows directly from a classical molecular orbital picture, it doesn't provide an efficient framework
for generating gates later. In contrast, QCC can efficiently optimize energy and generate efficient circuits.

In this step, we will look at how to generate elementary unitary operations in the UCC and QCC schemes for model
systems and to optimize their continuous parameters (amplitudes). \\

Illustrative code: \texttt{S3\_U\_ansatz.ipynb}.

\subsubsection*{Step \#4: Hamiltonian measurements}

To obtain the expectation value of the qubit Hamiltonian it needs to be measured at the end of the VQE circuit.
The problem is that the entire Hamiltonian cannot be measured using current hardware.
In this step, we show how to partition the Hamiltonian to a minimal number of groups, whose elements can be all measured simultaneously~\cite{Verteletskyi:2020do,Yen2019b}.\\

Illustrative code: \texttt{S4\_Measurement.ipynb}.

\subsubsection*{Step \#5: Use of quantum hardware}

To carry out the VQE algorithm on the actual quantum hardware one needs to present unitary transformations found in steps 3 and 4 as a
sequence of gates (i.e. circuit).  Here, we will illustrate circuit generation using compilers. Obtained circuits will allow one to either carry out
the entire VQE optimization procedure by optimizing amplitudes of step 3 unitaries or its single step using amplitudes for step 3 obtained on a
classical simulator (easier route). \\

Illustrative code: \texttt{S5\_Circuits.ipynb}.

\subsection*{Further Challenges} \label{sec:challenges}

Now you're on your own.  Discuss ideas for expanding on the above steps with your group.  Some possible directions for more advanced treatment:
\begin{enumerate}
\item How to obtain excited electronic states of the same or different symmetry? This task is highly relevant to any spectroscopic
application of quantum computing because in real experiments and applications not absolute energy but their differences are measured and are important.

\item Hamiltonian measurements (Step \#4): There are more advanced techniques to consider that are based on partitioning in the fermionic operator space and then bringing the fragments into the qubit space~\cite{huggins2019efficient,Yen:2020tt}.

\item Unitary transformations (Step \#3): Some of the unitary transformations can be applied on the Hamiltonian instead of the wavefunction, this increases
accuracy without increasing the circuit depth, but it increases the size of the Hamiltonian and thus introduces the measurement overhead.
See Refs.~\cite{Ryabinkin2019b,Lang:2020uv} for details.

\item Qubit Hamiltonian preparation (Step \#2): Typical fermion-qubit transformations substitute a single molecular spin-orbit by a qubit, which restricts calculations to relatively small basis sets because of the current limitations in the number of qubits. There were several techniques proposed recently to
compress larger basis sets into smaller number of qubits~\cite{Takeshita:2019wn,Bauman2019b}.

\item What are alternatives to VQE for the electronic structure problem using quantum computers with shallow circuits without error-correction?
%\item Optimization of the

\end{enumerate}

Business-related questions in the context of quantum entrepreneurship:

\begin{enumerate}
\item What other molecular properties one can obtain using VQE for the purpose of rational material and drug design?
\item What are the systems (molecules / materials) which are challenging for classical computing and whose modelling is valuable ?
\item What businesses can benefit from more accurate electronic structure calculations ?
\end{enumerate}

\bibliography{refs}
\bibliographystyle{unsrt}

\end{document}


\chapter{Conclusion} \label{sec:conclusionChapter}

We conclude this report by highlighting the contributions of this project:
\begin{itemize}
    \item
    Within the scope defined in Section~\ref{sec:scope}, we have explained the data structure presented in \cite{patrascu2014dynamic}, together with helper functions and algorithms. This has been complemented with illustrative examples.

    \item
    We have implemented and documented the data structure, making it publicly available for future work.

    \item
    We have implemented and included correctness tests in the repository. These attest to the correctness of the implementations we present and that were designed such that they can be used to assess the correctness of further implementations.

    \item
    We have listed other data structures that are of interest within the context of the dynamic predecessor problem, pointing to future work in the form of benchmarks.
\end{itemize}

\section{Final Remarks}

\begin{itemize}
    \item
    There is a fine balance between space and time consumption when developing and implementing sub-logarithmic data structures and algorithms. For example, van Emde Boas trees is a very fast data structure, but with a big space consumption drawback. Fusion Trees improve on space consumption, but updates take excessive time. Dynamic Fusion Trees, introduced in this project, seem to find the perfect balance between space and time consumption.

    \item
    As mentioned in Section~\ref{sec:modelsofcomputation}, theoretical bounds have the potential to hide big constants.

    This project has been about implementing algorithms that use a constant number of operations to compute the result, assuming that these are an improvement in comparison with logarithmic algorithms (such as binary search).
    We want to highlight that despite the fact that it is plausible to implement the \textit{Dynamic Fusion Node} using only $O(1)$ operations of the word RAM model, the hidden constant might not be negligible.

    For instance, the {\ttfamily DynamicFusionNodeBinaryRank} implementation from Section~\ref{sec:DynamicFusionNodeBinaryRank} computes $\text{rank}(x)$ with binary search.
    In this particular implementation, and because we set $w = 64$, allowing us to store at most $k = 16$ keys, we know that the worst-case running time of a $\text{rank}(x)$ query will be when the node is full, e.g., $n = k = 16$.
    Computing $\log_2 16 = 4$.
    We conclude that, when this implementation is full, it will take at most $4$ iterations for rank to compute the answer.

    The {\ttfamily DynamicFusionNodeDontCaresRank} implementation from Section~\ref{sec:rankWithDontCares} uses $O(1)$ operations to compute a $\text{rank}(x)$ query. The constant number of operations used by this method is larger than the $4$ iterations taken by the binary search rank algorithm from Section~\ref{sec:DynamicFusionNodeBinaryRank}.
    The {\ttfamily match} subroutine alone, used by the rank with "don't cares" algorithm, uses $5$ word RAM operations, and the whole rank algorithm needs to run this subroutine at least once; and if the key is not in the set, it needs to run it once again. That is already $10$ operations, not counting with some other necessary subroutines such as the select query and finding the branching bit, $j$.

    Our conclusion is that $w = 64$ is potentially a very small word size to take advantage of the running time of these algorithms.
\end{itemize}

\section{Future Work} \label{sec:futureWork}
In this section, we leave some suggestions on how further work on this topic can be conducted. We have split these into three main categories:
\begin{enumerate}
    \item
    Implementation, which covers specific data structures or algorithms.

    \item
    Optimization, which entails improving the present code.

    \item
    Benchmarking, which points to other data structures that either solve partially or totally the dynamic predecessor

\end{enumerate}

\subsection{Implementation} \label{sec:FutureWorkImplementation}

\paragraph*{Delete methods}
The implementation featured in Section~\ref{sec:InsertDontCares} implements the {\ttfamily delete} method naively, but all the ingredients needed to implement it while adhering to \textit{Inserting a key} section of \cite{patrascu2014dynamic} are already present in the implementation.

\paragraph*{Key compression in constant time}
One of the bottlenecks of implementations from Sections~\ref{sec:rankWithDontCares} and \ref{sec:InsertDontCares} is how the compressed keys with "don't cares" are maintained. Specifically, the next iterative step in the implementation from Section~\ref{sec:InsertDontCares} is to implement functions that compute the compressed keys in $O(1)$ time, as explained in Chapters $[3.2 - 3.3]$ of \cite{patrascu2014dynamic}.

\paragraph*{Dynamic Fusion Tree}
After enabling all the operations at the node level in $O(1)$ time with the previous steps' implementation, all that remains to complete the implementation is to implement a $k$-ary tree using a \textit{Dynamic Fusion Node} as its node. This is covered in Chapter $4$ of \cite{patrascu2014dynamic}.

\paragraph*{Non-recursive implementation}
Chapter 4 of \cite{patrascu2014dynamic} mentions that once the dynamic fusion tree is implemented, the rank operation on a tree is given by a recursive function. Recursion in Java can be slow \cite{shirazi2003java}, and for this reason, a non-recursive alternative is preferred.

\subsection{Benchmarking}

\paragraph*{msb functions}
It would be interesting to see how the different msb functions implemented in this project compare to each other in terms of time.

\paragraph*{Dynamic Predecessor Problem Data Structures}
Once the \textit{Dynamic Fusion Tree} is fully implemented, the next logical step would be to benchmark it with other data structures that solve, either partially or entirely, the dynamic predecessor problem. These would be the data structures mentioned in Section~\ref{sec:IntegerSets}.

\subsection{Optimization}

\paragraph*{Space}
Space consumption can be improved by, for instance, avoiding and combining some of the fields. When $k = 8$, $bKey$ would only use $8$ of the $64$ allocated bits, and $index$ would use $3 \times 8 = 24$ bits. So, in total, $32$ bits for both words. This is an example of how space can be improved.

\paragraph*{Simulate a longer word size}
One of the limitations of the present implementation is that the maximum integer size is 64 bits on a common modern machine. An interesting improvement would be to simulate larger word size $w$, which would increase $k$, allowing to store more keys at the node level. This would have to be carefully studied to keep every operation in $O(1)$ time.

\paragraph*{Lookup {\ttfamily M} constant}
The {\ttfamily Util} class includes a function, {\ttfamily M}, which takes $b$ and $w$ as parameters and returns the word $(0^{b-1}1)^{(w/b)}$. It is a widely used function in this repository, and it has been implemented with a loop. With limited word size, this could easily be implemented as a lookup function, improving the running time up to $O(1)$.

\paragraph*{Profiling}
This technique allows us to see which sections of the code the CPU uses most of the time, indicating possible code bottlenecks. By using it, the implementation can be fine-tuned up to the point where it can be competitive.


% !TEX options=--shell-escape
\documentclass [12pt]{article}

\usepackage {amsmath}
\usepackage {amsthm}
\usepackage {amssymb}
\usepackage {graphicx}
\usepackage {float}
\usepackage {multirow}
\usepackage {xcolor}
\usepackage {algorithmic}
\usepackage [ruled,vlined,commentsnumbered,titlenotnumbered]{algorithm2e} \usepackage {array}
\usepackage {booktabs}
\usepackage {url}
\usepackage {parskip}
\usepackage [margin=1in]{geometry}
\usepackage [T1]{fontenc}
\usepackage {cmbright}
\usepackage [many]{tcolorbox}
\usepackage [colorlinks = true,
            linkcolor = blue,
            urlcolor  = blue,
            citecolor = blue,
            anchorcolor = blue]{hyperref}
\usepackage {enumitem}
\usepackage {xparse}
\usepackage {verbatim}
\usepackage{listings}
\usepackage{xcolor}
\usepackage[cache=false]{minted}
\usepackage{mdframed}

\BeforeBeginEnvironment{minted}{\begin{mdframed}}
\AfterEndEnvironment{minted}{\end{mdframed}}

\DeclareTColorBox {Solution}{}{breakable, title={Solution}}
\DeclareTColorBox {Solution*}{}{breakable, title={Solution (provided)}}
\DeclareTColorBox {Instruction}{}{boxrule=0pt, boxsep=0pt, left=0.5em, right=0.5em, top=0.5em, bottom=0.5em, arc=0pt, toprule=1pt, bottomrule=1pt}
\DeclareDocumentCommand {\Expecting }{+m}{\textbf {[We are expecting:} #1\textbf {]}}
\DeclareDocumentCommand {\Points }{m}{\textbf {(#1 pt.)}}

\begin {document}

{\LARGE \textbf {COMP 285 (NC A\&T, Spr `22)}\hfill \textbf {Homework 1} }
\vspace {1em}
\begin {Instruction}

\paragraph {Due.} Tuesday, January 25th, 2022 @ 11:59 PM!
\end {Instruction}

\vspace {1em}
\begin {Instruction} \paragraph {Homework Expectations:} Please see the \href{https://www.comp285.ml/homework/#general-homework-information}{Homework} part of the Course Website (\href{https://www.comp285.ml/policies/#collaboration-policy-and-honor-code}{comp285.ml/policies}) for guidance on what we are looking for in homework solutions. We will grade according to these standards, and you should cite all sources you used outside course material.

\paragraph {What we expect:} Make sure to look at the ``\textbf {We are expecting}'' blocks below each problem to see what we will be grading for in each problem! \end {Instruction}

\vspace {1em}
\begin {Instruction}

\paragraph {Exercises} The following questions are exercises. We suggest you do these on your own. As with any homework question, though, you may ask the course staff for help.

\paragraph {Points} This assignment is worth a total of 100 points.
\end {Instruction}

\begin{centering}
\section*{Fun with Algorithms}
\end{centering}

This course will be a mixture of written homework and coding assignments. Both types of assignments will be submitted through \href{https://www.gradescope.com/courses/350304}{Gradescope}.

In order to reduce the overhead to the course staff, all coding assignments will be released through \href{https://repl.it/}{repl.it}, an in-browser IDE. \textit{This will be the only supported IDE} for the class.

By \textit{supported}, we mean that if you run into issues on \href{https://repl.it/}{repl.it}, the course staff will do everything in their power to resolve them. We \textit{strongly encourage} you to use repl.it for the coding portions of the homework assignments.

\section {Exercise: Setting up repl.it}
\Points{20} As such, the this graded exercise will walk you through setting up your \href{https://repl.it/}{repl.it} account, joining the course, and getting your copy of the starter code. Even if you \textbf{do not plan} to use \href{https://repl.it/}{repl.it} for writing/developing your solutions (because you have another preferred IDE), you will use it to obtain your starter code.

First, let's start by getting you an account and joining the right project:

\begin{enumerate}
    \item If you don't already have a repl.it account with your ``aggies.ncat.edu'' email, head over to the repl.it \href{https://repl.it/}{homepage}. On the top-right, click the `Sign Up' and use your NCAT email address to sign-up.
    \item Complete the registration process (eg, there's a verification email you should get)
    \item Join the @COMP285 Team by following \href{https://repl.it/teams/join/axywewyfiltmqtcgovikrywbqisfdgss-COMP285}{this link}.
\end{enumerate}

Next, let's get your starter code. From your repl.it homepage, you should have a menu down the left-hand side of the page (if not, maybe click the hamburger menu to open it?). Click on "Teams" as shown in Figure \ref{fig:replit_homepage}. You should see `North Carolina A\&T' under `Education', as well as the `COMP 285' Team Project.

Click on `COMP 285', which will take you to the Team Homepage. From there, under `Projects', you should see a link to get started on HW1-Code. Clicking it will give you the starter code, as well as set you up with an online.

If the above does not work, you can also try going directly to the \href{https://replit.com/team/COMP285/HW1-Code}{project link}.

\begin{figure}[h!]
\centering
\includegraphics[scale=0.5]{replit.png}
\caption{View of the repl.it Homepage for a student.}
\label{fig:replit_homepage}
\end{figure}

Either way, you should now have the starter code. Clicking the big green button at the top of the IDE will execute the \texttt{main} function defined in \texttt{main.cpp}. It'll fail, but that's okay. The rest of the assignment will walk you through implementing different functions in your starter code.

\Expecting{You should have a copy of your starter code now available! You can use replit to make edits and run your code. The course staff can see who has started the project, so that is how you'll get credit for this question. No need to submit anything}

\pagebreak
\section{Interview Practice: Who's Missing!}

\Points{20} Trinitee is just wrapping up her software engineering internship at Foogle \footnote{This is a meant to be a fake company.}. She recently developed an algorithm, \texttt{genereateIDs}, that gives unique IDs, starting from $0$ and in increasing order, to each Google account in a particular region. This is how she defined her algorithm, in a `.h' file.

\vspace{2em}

\begin{minted}[linenos=true]{cpp}
#include <vector>

// Returns a list of unique IDs for the specified `region'.
//
// `region' is a string that specifies a county, place, or location.
// This algorithm looks up every Foogle account in the location,
// and assigns a unique ID. IDs are generated sequentially
// (starting at with 0) and up to `n-1', where `n' is the number
// of people in the given region.
//
// For example:
//      If a region has 3 accounts, return {2, 0, 1}.
//      If a region has 5 accounts, return {1, 0, 3, 2, 4};
//
// Note that the returned IDs are not necessarily sorted.
std::vector<int> generateIDs(const std::string& region);
\end{minted}

However, she's running into a few issues with her code. It seems that the returned vector is always too small - precisely, it seems to always be \textit{exactly one element} too small.

For example, Trinitee knows that in the country of Algolandia \footnote{This is a fake country! Don't worry if you've never heard of it!}, there are exactly $2^{20} = 1,048,576$ accounts. However, when she runs the following lines of code:

\vspace{2em}
\begin{minted}[linenos=true]{cpp}
const auto ids = genereateIDs("Algolandia");
std::cout << "Length of my ids: " << ids.size();
\end{minted}

It prints $1,048,575$, which is precisely one short. Confused, Trinitee decides to ask Asha, a fellow intern, for some help.

\subsection{Is it sorted?}
\Points{5} Asha suggest to Trinitee that they first write a program to check if the IDs she's getting back are \textbf{sorted in descending order}. She thinks that if they're sorted, it'll be easy for them to find which one is missing!

She starts writing out the program to do this (check if a vector of integers is \textbf{sorted in descending order}), but suddently, someone comes a long and deletes part of her code! Below is what she has left.

\vspace{2em}

\begin{minted}[linenos=true]{cpp}
// Checks if a vector<int> called `elements' is sorted in
// descending order. That is:
// {1, 2, 3, 4} returns false, because it's sorted but increasing.
// {4, 3, 2, 1} returns true, because it's sorted and decreasing.
bool isSortedDescending(const std::vector<???>& elements) {
    for (int i = 0; i < element.size(); i++) {
        if (???) {
            return false;
        }
    }
    return true;
}
\end{minted}

Flabergasted, Asha turns to you for help!

\begin{enumerate}
    \item \Points{2} What should go in the first set of `???' in line 5 above? \footnote{If you're having trouble, read the comment for the function carefully!}
    \item \Points{2} What should go in the second set of `???' in line 7 above? \footnote{If you're having rouble, this is very similar to what we did in class, but instead of \textbf{ascending}, here we want to check if the list is \textbf{descending}.}
    \item \Points{1} What is the big-Oh running time of the \texttt{isSortedDescending} function, if we let $n$ be the number of elements?
\end{enumerate}

\Expecting{You should provide an answer for each of the three sub-questions above. The first two are snippets of code (just write it out), and the last one should be $O(\dots)$ where you fill-in the $\dots$. In total, they are worth 5 points out of 20 for this problem.}

\subsection{So what account is missing?}
\Points{5} Sadly, it turns out the lists are not sorted! I guess the documentation was right this time! You, Trinitee, and Asha start brainstorming other solutions. How can we figure out which ID is the one that's missing?

Trinitee gives another suggestion. On the white board, she writes the numbers:
$$
0, 1, 2, 3, 4, 5, 6, 7
$$

And asks:

\begin{enumerate}
    \item [2.2.1] \Points{1} What do the numbers above add up to?
\end{enumerate}

She then erases the numbers, and writes:
$$
(0, 7); (1, 6); (2, 5); (3, 4)
$$

She turns to you, and asks:

\begin{enumerate}
    \item [2.2.2] \Points{1} What do each of the pairs add up to? How many pairs are there?
\end{enumerate}

After thinking about the above for a bit, all three of you jump up excitedly! Asha goes to the board and writes:
$$
0, 1, 2, 3, 4, \dots, n-3, n-2, n-1, n
$$

and then writes:

$$
(0, n); (1, n-1); (2, n-2); (3, n-3); (4, n-4), \dots
$$

Turns out both Asha and Trinitee are quite smart! They turn to you, and ask:

\begin{enumerate}
    \item [2.2.3] \Points{3} If we add up all the numbers from $0$ to $n$, what do they add up to? Is there a closed-form formula? \footnote{If you're having trouble, check-out this \href{https://www.youtube.com/watch?v=aaFrAFZATKU&t=343s}{video}}
\end{enumerate}

\Expecting{An answer for each of the above questions. The first two should be a number, and the last should be a formula which uses $n$.}

\subsection{Finding The Missing ID}
\Points{10} Having figured out the above, the three of you start coding! You come up with the following pseudo-code:

\begin{verbatim}
algorithm discoverMissingID
  Input: The output from the malfunctioning generateIDs function above.
  Output: The missing ID.

  s1 = the sum of all numbers in the given vec<int>
  s2 = what the sum should be if no numbers were missing. See answer to 2.2.3.
  return s2 - s1
\end{verbatim}

Now, Trinitee and Asha look at you, and say: "Can you code it up for us?"

\Expecting{You should implement the above pseudo-code in \texttt{answers.cpp}, which is part of the starter code you retrieved in Exercise 1}.

\pagebreak
\section{Interview Practice: The Best Basketball Player}
\Points{20} Hassan has been following closely the latest happening with the Denver Nuggest \footnote{If you're like me and do now know who they are, suffice it to say that they're a basketball team}. He's a bit disappointed with their performance this season, so he reaches out to his friend Caleb.

Caleb is a data scientist working for the U.S. Census. He tells Hassan that a big issue with the Denver Nuggest this season is that they're players are too short! What they really need is a tall player - wait, not a tall player, \textbf{the tallest player} in America.

Thankfully, Caleb has just the data for the job! He shows Hassan that the census collects every person's height as an integer in inches \footnote{This is not true. It would be a violation of privacy if it was...} (they're too lazy, so they always round to the nearest inch).

\subsection{Finding the Tallest American}
\Points{10} Hassan and Caleb feel like they've done a good job so far, so they reach out to you. They know you're an expert in C++, do they ask you to write a function - \texttt{findTallestPerson} - which takes in a vector of integers correspondong to each person's height from the census data Caleb has, and returns \textbf{the index} of the largest height. If there are multiple largest heights, it returns the smaller index.

They give you the following examples:

\vspace{2em}
\begin{minted}[linenos=true]{cpp}
findTallestPerson({1, 3, 4, 5, 2});  // should return 3, the index of 5
findTallestPerson({-1, -3, -4, -5, -2});  // should return 0, the index of -1
findTallestPerson({});  // should throw std::invalid_argument
\end{minted}

You look at them a bit funny, and wonder to yourself: "Who in the world has negative height? And why would the list ever be empty?" Still, you want to do a good job, so you do what they ask.

\Expecting{You should implement \texttt{findTallestPerson} in \texttt{answers.cpp}, which is part of the starter code you retrieved in Exercise 1}.

\subsection{Is it Correct?}
\Points{5} After implementing the above algorithm, you want to show all your friends how good you've gotten at \textit{correcness} and \textit{runtime} analysis. You write down the pseudocode:

\begin{verbatim}
algorithm findTallestPerson
    Input: A vector of integers representing height in inches
    Output: The index of the tallest person.

    if input is empty:
        throw error

    tallestIndex = 0
    for index in {1, ..., input.size() - 1}:
        if input[index] > input[tallestIndex]:
            tallestIndex = index
    return tallestIndex
\end{verbatim}

You want to \textit{prove} to Caleb and Hassan the algorithm above is correct. You start walking them through a proof by induction. Fill out the missing pieces.

\begin{itemize}
    \item \textbf{Inductive Hypothes}: \texttt{tallestIndex} points to the index of the largest element in \texttt{input[:i+1]} after the $i$-th iteration (of the for loop)
    \item \Points{3} \textbf{Base Case (i=0)} \textit{TODO: FILL THIS OUT}
    \item \Points{2} \textbf{Inductive step} Assume our \textbf{inductive hypothesis} is true for $i = k$ (eg, at the $k$-th) iteration. \textit{Explain why} it will also be true for $i = k + 1$ (eg, at the next iteration of the for-loop).
    \item \textbf{Conclusion}: As shown above, the inductive hypothesis holds for $i = 0$, (\textbf{base case}). We also argued as part of the \textbf{inductive step}, that if it holds for $i=0$, it is also true for $i=1$, and if it's true for $i =1$, it's true for $i=2$, and so on. As such, it must be true for $i = n -1$ (aka, when our for-loop finishes). Therefore, \texttt{tallestIndex} points to the index of the largest element in \texttt{input[:n]} after the $n-1$-th iteration, which is exactly what we wanted.
\end{itemize}

\Expecting{You should provide written responses to the two missing pieces in the above proof. The first is the base case, the second is an explanation for why the inductive step works.}

\subsection{How fast is it?}
\Points{5} You also want to show everyone how good you are at analyzing the runtime of your algorithm. For your reference, you write down the formal definition of big-Oh: $T(n)= O(g(n))$ if and only if:
$$
\exists c, n_0 \text{ s.t. } \forall n > n_0, T(n) \leq c \cdot g(n)
$$

Then, you answer the following:
\begin{itemize}
    \item \Points{2} What is the big-Oh runtime of the pseudo-code shown above?
    \item \Points{2} After practicing the guitar, Vincent (another amazing C++ coder),  tells you: ``I implemented your code, and counted up all the operations. There are exactly $T(n) = 3n + 1$ operations in my implementation! I think $T(n) = O(n)$''. What values of $c, n_0$ would convince you that Vincent is right, and his implementation is in-fact $O(n)$?
    \item \Points{1} Vincent thinks for a bit, and says - I think $T(n) = O(n^2)$! Is he right? If yes, what are the $n_0$ and $c$ values that convince you he's right? If no, briefly explain why such values cannot exist.
\end{itemize}

\Expecting{And answer to each of the above. The first should be of the form $O(\dots)$, where you've filled in the $\dots$. The second should be two numbers, the $c$ and $n_0$ that convince you. The last should be either two numbere (the $c$ and $n_0$ that convince you), or a short sentence explaning why they don't exist.}

\pagebreak
\section{Interview Practice: Game Development}
\Points{20} Nkumbu is a world-renowed game developer working for Big Corp Studios \footnote{Again, fake name.}. His game works by randomly generating pets, each which costs a random amount of coins (only integer values). The player is then allowed to purchase precisely \textit{two} pets.

Being an awesome developer, Nkumbu wants to make sure that no matter how many coins the player has, there are always \textit{two} pets that we can purchase at the cost of all his coins.

\subsection{Finding the Most Expensive Pets}
\Points{10} Having been stumped with this problem for quite a bit, he reaches out to Isaiah, whose an avid gamer and recently took COMP 285: Analysis of Algorithms for help. He asks Isaiah to write a C++ function \texttt{findTwoPets}, which takes as input a vector of integers (corresponding to the cost of each pet) as well as a target value (corresponding to how many coins the player currently has), and returns a pair that represents two \textbf{distinct}\footnote{Should be pretty obvious that we're not going to let a player buy the same pet twice!} indices of pet costs that sum up to the target value.

For some reason, Nkumbu also wants the smaller index to be first. To be super clear, he gives a few examples to Isaiah:

\vspace{2em}
\begin{minted}[linenos=true]{cpp}
std::vector<int> pet_costs = {0, 2, 3, 4, 5};
std::pair<int, int> out1 = findTwoPets(pet_costs, /*coins=*/6);
std::pair<int, int> out2 = findTwoPets(pet_costs, /*coinst*/10);

std::cout << out1.first << " " << out1.second;  // should output 1 3
std::cout << out2.first << " " << out2.second;  // should output -1 -1
\end{minted}

In the first example, \texttt{vec[1] + vec[3] = 2 + 4 = 6}. In the second example, we returned \texttt{-1, -1} because there are not two distinct elements within the vector that sum to 10 (you can't use 5 twice).

Help Isaiah by writing an algorithm (in C++) that solves this problem. Your solution should be written in \texttt{answer.cpp} which is part of the starter code.

For context, there's a straight-forward $O(n^2)$ algorithm that you can start with (this is probably the first one you'll think of). There is also an $O(n \log n)$ algorithm (Hint: what if the list was sorted?), and even a far more efficient $O(n)$ algorithm.

For this homework, \textit{there are no explicit time complexity constraints} here (i.e. algorithm just needs to work as intended). You \textit{might} receive bonus points if you implement one of the faster ones, though.

\Expecting{You should implement \texttt{findTwoPets} in \texttt{answer.cpp} which is part of the starter code. It does not need to be the best algorithm, it just needs to work.}

\subsection{Correctness of Algorithm}
\Points{5} For whichever algorithm you implemented, please provide a few sentences \textit{explaning why} it is correct. This does not need to be formal (but feel free to try a proof by induction).

\Expecting{A few sentences explaining why the algorithm you write works. You can be informal about it, but try to be precise.}

\subsection{Runtime of Algorithm}
\Points{5} For whichiever algorithm you implemented, what is the big-Oh runtime?

\Expecting{An answer in the form of $O(\dots)$ with $\dots$ filled in.}

\pagebreak
\section{Exercise: Refresher on Nodes, Pointers, etc.}
\Points{20} In this exercise, we'll write some code that will set you up for implementing Karatsuba's algorithm later in the class (maybe as extra credit!).

\subsection{Adding Two Numbers}
\Points{10}
Almost every programming languages already has `+' and `*' implemented for you, on integers. So to make this more interesting, your task is to write the function \texttt{add}, which given two numbers represented as linked lists of single-digit
integers, returns their sum as a linked list of integers. Return the head node.

The linked list is represented as follows:
\begin{minted}[linenos=true]{cpp}
template <typename T>
struct Node {
  T value_;
  Node<T>* next_ = nullptr;
};
\end{minted}

We have provided a util function \texttt{makeList} to convert an integer to its corresponding list representation. Feel free to take a look at the function in \texttt{util.cpp} from the starter code and note how we extract the digits (\texttt{num \% 10} and \texttt{num / 10} in the loop).

For convenience, the list stores the digits from the lowest to the highest, e.g., \texttt{makeList(123) => head -> [3 -> 2 -> 1]}.

Some examples for \texttt{add}:
\begin{minted}[linenos=true]{cpp}
Node<int>* q4bInput1 = makeList(123);  // [3 -> 2 -> 1]
Node<int>* q4bInput2 = makeList(242);  // [2 -> 4 -> 2]
Node<int>* q4bOut1 = add(q4bInput1, q4bInput2);
std::cout << listToString(q4bOut1)
        << std::endl;  // should output [5 -> 6 -> 3] (meaning 365)
freeList(q4bInput1);
freeList(q4bInput2);
freeList(q4bOut1);

Node<int>* q4bInput3 = makeList(999);
Node<int>* q4bInput4 = makeList(1);
Node<int>* q4bOut2 = add(q4bInput3, q4bInput4);
std::cout << listToString(q4bOut2)
        << std::endl;  // should output [0 -> 0 -> 0 -> 1] (meaning 1000)
freeList(q4bInput3);
freeList(q4bInput4);
freeList(q4bOut2);
\end{minted}

Refresher on pointers, memory allocation, and deallocation for your reference:

\begin{itemize}
    \item To create a \texttt{Node} object, use \texttt{Node<int>* ptr = new Node<int>()}. \texttt{new} dynamically allocates memory for this.
    \item We provided a \texttt{freeList} function that will traverse the linked list and free every node. \texttt{freeList} is all we need here, but always remember when you have allocated memory (i.e. with `new`) to free it when finished (i.e. `delete ptr`).
    \item To check whether there are "memory leaks" (i.e., allocated memory but not deallocated), you can run valgrind on repl.it with `valgrind --leak-check-full ./main`
\end{itemize}

Please note that this question is at the upper-end of the C++ intricacies we will learn on in the course. Having familiarity with pointers here will serve us well later on (e.g. with trees).

\Expecting{You should implement \texttt{add} in \texttt{answers.cpp} in your starter code.}

\subsection{Runtime of Addition}
\Points{10} The algorithm above forces you to implement addition the way you would normally do it by hand (eg, add numbers and carry when they add to more than 10). In this context, let us define ``operation'' as being a 1-digit addition. What is the big-Oh running time of this algorithm, in terms of $n$ where $n$ is the length of the input lists (eg, numbers).

\Expecting{A single answer in the form $O(\dots)$ where you've filled in $\dots$. No need to prove or argue this is correct, simply provide your answer.}

\pagebreak
\section*{Submitting the Assignment}

This assignment is a combination of written and programming questions. Both portions of the assignment should be submitted through \href{https://www.gradescope.com/courses/350304}{Gradescope} before the assignment due date on January 25th, 2022 @ 11:59PM.

The "Homework 1: Fun with Algorithms" assignment is the written portion, for which you should submit a \textbf{typed} response to the non-coding questions above. Each response should clearly be marked with its corresponding number (for example, 2.1 or 2.2.2) etc. You are free to use the provided templates, or to simply type your responses on a blank document (whatever works for you).

The "Homework 1: Coding" is the programming portion of the assignment. For this portion, download the ".zip" file from replit (see Figure \ref{fig:replit_download}), and upload this ".zip" file as your answer to \href{https://www.gradescope.com/courses/350304}{Gradescope} (see Figure \ref{fig:replit_download}). You can upload the assignment as many times as you want - each time a set of automatic tests will run that test your code for correctness. Feel free to use this to detect when your code is wrong.

\begin{figure}[h!]
\centering
\includegraphics[scale=0.3]{replit-download.png}
\includegraphics[scale=0.3]{gradescope-upload.png}
\caption{(left) View of the hamburger menu which holds the option to download your source code as a ".zip" which can be directly uploaded to \href{https://www.gradescope.com/courses/350304}{Gradescope}. (right) View of the screen you should see after uploading your ".zip" file. Make sure all the files are present, since they're requires for the autograder to run.}
\label{fig:replit_download}
\end{figure}

To summarize, do the following:
\begin{enumerate}
    \item Submit your \textbf{typed} responses to the non-coding portion of the assignment on Gradescope for "Homework 1: Fun with Algorithms"
    \item Submit your \textbf{code files} as a ".zip" to Gradescope for "Homework 1: Coding".
    \item Fill out this \textbf{Google Form} (\href{https://forms.gle/pvoM4pYV2u4yYXc96}{https://forms.gle/pvoM4pYV2u4yYXc96}) to help me track your thoughts on the homework.
\end{enumerate}

\end {document}