\chapter{Transport domain analysis}

In this chapter, we will analyze two variants of the Transport domain: sequential and temporal. To do this, we will describe the datasets
used for devising experiments and discuss the properties of Transport
that will help us in developing better quality planners.

\section{Problem complexity}

When domain-independent planners solve a sequential Transport problem,
they face a harder task than planners that have access to domain knowledge ahead of time.
For domain-independent planners, deciding whether a plan of a given length exists
(the \textsc{Plan-Length} decision problem) is
a NEXPTIME-complete task.
Deciding if a plan exists at all (the \textsc{Plan-Existence} decision problem)
is an EXPSPACE-complete task \citep[Table~3.2]{Ghallab2004}.

That does not mean domain knowledge makes Transport easy, as is evident from
the very thorough analysis by \citet{Helmert2001, Helmert2001a}.
We will categorize our problems using Helmert's notation to
be able to apply their results to our domain.
Helmert's \textsc{Transport} task is a 9-tuple $(V, E, M, P, fuel_0, l_0, l_G, cap, road),$
where:
\begin{itemize}
\item $(V, E)$ is the road graph;
\item $M$ is a finite set of vehicles (mobiles);
\item $P$ is a finite set of packages (portables);
\item $fuel_0 : V \to \N_0$ is the fuel function;
\item $l_0: (M \cup P) \to V$ is the initial location function;
\item $l_G: P \to V$ is the goal location function;
\item $cap: M \to \N$ is the capacity function; and
\item $road: M \to 2^E$ is the movement constraints function.
\end{itemize}
$V$, $M$, and $P$ are pairwise disjoint.
No Transport domain variants assume movement constraints, therefore, $road$ is a constant
function $\forall m \in M : road(m) = E$.

A simplified notation is also introduced in \citet{Helmert2001a} for special cases of \textsc{Transport} tasks.
For $i,j \in \{1, \infty, *\},$ $k \in \{1, +, *\}$ a $\textsc{Transport}_{i,j,k}$ task is defined as a
general \textsc{Transport} task (defined above) that satisfies:
\begin{itemize}
\item if $i=1$, then $\forall m \in M : cap(m) = 1$ (vehicles can only carry one package);
\item if $i=\infty$, then $\forall m \in M : cap(m) = |P|$ (vehicles have unlimited capacity);
\item if $j=1$, then $\forall v \in V : fuel_0(v) = 1$ (one fuel unit per location);
\item if $j=\infty$, then $\forall v \in V : fuel_0(v) = \infty$ (unlimited fuel per location);
\item if $k=+$, then $\forall m \in M : road(m) = E$ (no movement restrictions); and
\item if $k=1$, then $M = \{m\} \And road(m) = E$ (single vehicle, no restrictions).
\end{itemize}
The $*$ value for $i$, $j$ or $k$ signifies no restriction on that property.
Note that
\textsc{Transport} refers to the notation from \citet{Helmert2001a}, while Transport refers to our studied domain.

Using this notation, the sequential Transport domain could be thought of as a $\textsc{Transport}_{c,\infty,+}$ task, where $c \in \N$
(equivalent to $\textsc{Transport}_{1,\infty,+}$).
Similarly, the temporal variant represents a $\textsc{Transport}_{c,f,+}$ task, for $c, f \in \N$
(equivalent to $\textsc{Transport}_{1,1,+}$).

For sequential and temporal Transport without fuel, the \textsc{Plan-Existence} problem
reduces to verifying reachability of each package by at least one vehicle
and the reachability of target locations from the starting locations of all packages,
which we can do in polynomial time, as noted in \citet[Theorem 8]{Helmert2001}.
With fuel, there is no straightforward way of determining
if a plan exists and this problem is NP-complete, which is proven in \citet[Theorem 9 and 10]{Helmert2001}.

Even though fuel constraints are modeled differently than in Transport (constraints per location versus per vehicle), the proof of
NP-completeness of \textsc{Plan-Existence} for $\textsc{Transport}_{\infty,1,1}$
present in \citet[Theorem~3.9]{Helmert2001a}
can be trivially edited to prove the NP-completeness
of \textsc{Plan-Existence} for temporal Transport.
Instead of adding fuel conditions
to the entrance and exit nodes of a location,
we simply add it to edge between them. The rest of the proof holds as was presented originally.

Similarly, the \textsc{Plan-Length} problem is NP-complete for all mentioned variants of Transport
\citep[Section~3.6]{Helmert2001a}.
The fact that all the mentioned proofs work for temporal variants is explained in \citet[Section~3.5]{Helmert2001a}.
All of these results make clear that looking for an explicit planning algorithm is infeasible,
despite the advantage we gain by only focusing on one planning domain.

\section{Domain information}\label{domain-info}

There are several interesting properties and invariants that hold in both sequential and temporal Transport, which might prove useful for designing planners:
\begin{enumerate}
\item \textbf{Do not pick up delivered packages}: The simplest and trivially correct decision is to never touch packages that are already at their destinations, since there is nothing
we can do using those packages that would result in a plan with a lower total cost.

\item \textbf{Drop when at the destination}: Likewise, it is
always correct for a vehicle containing a package with a destination equal
to the vehicle's location to do a \drop{} action immediately.

\item \textbf{Do not drop and pick up}: It never makes sense to plan a \drop{} and \pickup{}
action of
the same package by the same vehicle in succession. We will only get to the same state
by using a longer plan. This rule also applies if an action of a different vehicle
gets between the two successive actions, even if it does an action with the
dropped package.
It is important to note that this is a symmetric property: picking up
and then dropping equally results in a worse plan.
\begin{enumerate}
\item \textbf{Do not drop a package where we picked it up}: A generalization
of the previous rule is that vehicles should never drop a package
at the location they last picked it up, independent of the actions they took
between the relevant \pickup{} and \drop{}. This rule is also symmetric.

\item \textbf{Never drop after picking up at a location}:
While the order of successive \pickup{} and \drop{} actions does not
influence the optimality of a plan, it makes the search space smaller and the implementation of these rules simpler,
without loss of generality.
\end{enumerate}

\item \textbf{Do not drive suboptimally}: If a vehicle does a series of
\drive{} actions from location $A$ to $B$ without ``touching'' packages
or refueling at any of the locations it visits,
it has to follow the shortest possible path from $A$ to $B$. If it does not,
the induced plan can be made less costly or shorter by swapping the actual \drive{} actions
for precalculated optimal \drive{} actions along the shortest path.
Do note, that it is not important for the application of this rule whether actions are in direct succession (in a sequential plan) or not.
\begin{enumerate}
\item \textbf{Do not drive in cycles}: A special but important case of the previous rule is that vehicles should not drive in cycles.
\end{enumerate}

\item \textbf{Do not forward packages using other vehicles}:
Let $p$ be a package of size $|p|$ located at $A$.
Let $v$ be a vehicle which drove through location $A$ to location $B \neq A$
and picked up $p$ at $B$,
without having less than $|p|$ free space in any intermediate state between leaving $A$ and picking up $p$.
If this sequence of events occurs, the plan is suboptimal in a sequential setting, because
$v$ could have picked up $p$ when driving through $A$, and the total plan cost
would have gone down by at least 2. The reason is that a different vehicle had to pick up, drive, and drop package $p$ at $B$.
While we cannot say if the \drive{} actions themself
were redundant, the \pickup{} and \drop{} actions definitely were.
By removing them, we save
2 on the total cost.

In a temporal domain without fuel, assuming that vehicles only drive along the shortest routes,
the plan does not necessarily have to be suboptimal, but it
is of equal length or longer: due to concurrent actions, the vehicles could have driven simultaneously.
In a few cases, the other vehicle could have dropped $p$ at $B$ before $v$ wants to pick it up,
which means that the total makespan of the partial plan did not become longer, but stayed the same.
The plan could not have become shorter, because $v$ does not have any time in this scenario when it is available to do another action.

In a temporal domain with fuel, it is not safe to say whether
such a scenario hurts the plan duration. If there is a petrol station at $B$
and $v$ wants to refuel there, the other vehicle could have enabled the parallelization
of the \refuel{} and \pickup{} actions, therefore shaving off 1 time unit in total.
However, if there is no petrol
station at $B$, this situation reduces to the no-fuel variant. Given the relative rarity of petrol stations, this will reduce the search space somewhat.
\end{enumerate}
Another insight can only be applied to the sequential variant of Transport:
\begin{enumerate}
\item \textbf{Drop from an active vehicle only}: Without loss of generality,
we can prune all plans where a \drop{} action of a vehicle happens
right after an action of a different vehicle. It is trivial to see that if we had a plan where
a \drop{} action
occurs after an action of a different vehicle, we can swap that action with the \drop{} action without changing the total plan cost or changing the validity of the plan.

Doing this repeatedly will yield an equivalent plan, in which the \drop{} action
occurs right after a different action of the same vehicle and the plan is of the same total cost and validity as the original plan. Repeating this process for each \drop{} action will yield a plan equivalent to the original plan, which additionally satisfies this rule.
\end{enumerate}
Finally, these are the properties that only meaningfully apply to the temporal variant:
\begin{enumerate}
\item \textbf{Refueling and dropping/picking up can occur at the same time}:
A plan in which a vehicle starts to pick up a package at the same location it just refueled at is suboptimal, if there was a time point during the \refuel{} action
when the vehicle was not dropping or picking up packages and the package was already
co-located with the vehicle at that time.

\item \textbf{No fuel left means refueling or ignoring the vehicle}:
If a vehicle is stuck with no fuel left, or with less fuel than is required for
any valid \drive{} action,
the correct thing to do is to either refuel or drop all packages and ignore the vehicle in further planning. Unfortunately,
we cannot say anything about the (non-)optimality of a plan where this occurs.
\end{enumerate}

\section{Datasets \& problem instances}\label{datasets}

For evaluation and comparison with other planners, we have acquired several problem datasets from previous runs of the IPC.
Table~\ref{tab:ipc-datasets} provides an overview of the individual datasets, their associated IPC competition, the track at the competition and the domain variant the problems are modeled in.

\begin{table}[tb]
\centering
\begin{tabular}{lclc}
\toprule
{\hspace{0.75em}\textbf{Dataset}} & \textbf{Competition} & {\hspace{2.5em}\textbf{IPC Track}} & \textbf{Formulation}\\
\midrule
netben-opt-6 & \multirow{4}{*}{IPC-6} & \href{http://icaps-conference.org/ipc2008/deterministic/NetBenefitOptimization.html}{Net-benefit: optimization} & Numeric \\
seq-opt-6 & & \href{http://icaps-conference.org/ipc2008/deterministic/SequentialOptimization.html}{Sequential: optimization} & STRIPS \\
seq-sat-6 & & \href{http://icaps-conference.org/ipc2008/deterministic/SequentialSatisficing.html}{Sequential: satisficing} & STRIPS \\
tempo-sat-6 & & \href{http://icaps-conference.org/ipc2008/deterministic/TemporalSatisficing.html}{Temporal: satisficing} & Temporal \\
\midrule
seq-mco-7 & \multirow{3}{*}{IPC-7} & \href{http://www.plg.inf.uc3m.es/ipc2011-deterministic/SequentialMulticore.html}{Sequential: multi-core} & \multirow{3}{*}{STRIPS} \\
seq-opt-7 & & \href{http://www.plg.inf.uc3m.es/ipc2011-deterministic/SequentialOptimization.html}{Sequential: optimization} &  \\
seq-sat-7 & & \href{http://www.plg.inf.uc3m.es/ipc2011-deterministic/SequentialSatisficing.html}{Sequential: satisficing} &  \\
\midrule
seq-agl-8 & \multirow{4}{*}{IPC-8} & \href{https://helios.hud.ac.uk/scommv/IPC-14/seqagi.html}{Sequential: agile} & \multirow{4}{*}{STRIPS} \\
seq-mco-8 & & \href{https://helios.hud.ac.uk/scommv/IPC-14/seqmulti.html}{Sequential: multi-core} &  \\
seq-opt-8 & & \href{https://helios.hud.ac.uk/scommv/IPC-14/seqopt.html}{Sequential: optimization} &  \\
seq-sat-8 & & \href{https://helios.hud.ac.uk/scommv/IPC-14/seqsat.html}{Sequential: satisficing} &  \\
\bottomrule
\end{tabular}
\caption[Transport datasets from the 2008, 2011, and 2014 IPCs.]{Transport datasets from the 2008, 2011, and 2014 IPCs. All formulations assume capacitated vehicles. Numeric and temporal formulations also contain fuel demands and capacities. The temporal formulation additionally adds concurrent actions and a notion of time. More information can be found in Section~\ref{domain-desc}.}
\label{tab:ipc-datasets}
\end{table}

Short descriptions of the various tracks and subtracks can be found in the rule pages of IPC-6,\puncfootnote{\url{http://icaps-conference.org/ipc2008/deterministic/CompetitionRules.html}}
IPC-7,\puncfootnote{\url{http://www.plg.inf.uc3m.es/ipc2011-deterministic/CompetitionRules.html}}
and IPC-8.\puncfootnote{\url{https://helios.hud.ac.uk/scommv/IPC-14/rules.html}}
We have decided to split our further research based on the tracks at the IPC: we will focus on constructing
Transport-specific planners for the seq-sat-6, seq-sat-7, seq-sat-8, and tempo-sat-6 datasets,
corresponding to the sequential and temporal variants of Transport.

The datasets labeled seq-opt correspond to sequential optimality planning tracks,
where only optimal plans for problems are accepted as correct.
Datasets labeled seq-mco are used in multi-core satisficing tracks (multi-threaded planners)
and seq-agl are used in agile tracks (minimize the CPU time required to find a satisficing plan).
The netben-opt-6 dataset contains Net Benefit problems, where the aim
is to compensate between achieving \textit{soft goals} and minimizing the total cost.
Soft goals are goals that do not necessarily have to be satisfied in a goal state,
but it is usually better for the total score if they are.
Each problem usually specifies a metric used for calculation of the score.
We will not focus on
these problems in this work.

In addition to the domain definition, we need to take a look at the individual problems to fully utilize our knowledge advantage.
Both the seq-sat-6 and tempo-sat-6 contain 30 problems, while seq-sat-7 and seq-sat-8 only contain 20 problems each. Table~\ref{tab:dataset-dimensions} shows the
dimensions of each problem instance for each mentioned dataset.

While the planners (including our domain-specific ones) do not know this,
each problem was constructed with a scenario in mind. Locations in problems are not just
placed randomly, but usually belong to cities. Inside a city, the road network
tends to be dense and road lengths small, while roads connecting cities
are rare and usually significantly longer.

\begin{table}[p]
\scriptsize
\centering
\begin{subtable}[t]{0.42\textwidth}
\centering
\csvreader[tabular=rrrrrrl,
    table head=\toprule\textbf{\#} & \rot{\textbf{Vehicles}} & \rot{\textbf{Packages}} & \rot{\textbf{Cities}} & \rot{\textbf{Locations}} & \rot{\textbf{Roads}} & \rot{\textbf{States}}\\\midrule,
    late after line=\mbox{},
    table foot=\\\bottomrule]%
{../data/seq-sat-6.csv}{Problem=\problem,Vehicles=\vehicles,Packages=\packages,Cities=\cities,Locations=\locations,%
Roads=\roads,Stateslat=\stateslat}%
{\problem & \vehicles & \packages & \cities & \locations & \roads & \stateslat}%
\caption{Problem dimensions of seq-sat-6.}
\label{tab:seq-sat-6-dims}
\end{subtable}
\quad
\begin{subtable}[t]{0.54\textwidth}
\centering
\csvreader[tabular=rrrrrrrl,
    table head=\toprule\textbf{\#} & \rot{\textbf{Vehicles}} & \rot{\textbf{Packages}} & \rot{\textbf{Cities}} & \rot{\textbf{Locations}} & \rot{\textbf{Roads}} & \rot{\textbf{Petrol}} & \rot{\textbf{States}}\\\midrule,
    late after line=\mbox{},
    table foot=\\\bottomrule]%
{../data/tempo-sat-6.csv}{Problem=\problem,Vehicles=\vehicles,Packages=\packages,Cities=\cities,Locations=\locations,%
Roads=\roads,Petrol=\petrol,Stateslat=\stateslat}%
{\problem & \vehicles & \packages & \cities & \locations & \roads & \petrol & \stateslat}%
\caption{Problem dimensions of tempo-sat-6.}
\label{tab:tempo-sat-6-dims}
\end{subtable}

\vspace{0.21cm}
\begin{subtable}[t]{0.42\textwidth}
\centering
\csvreader[tabular=rrrrrrl,
    table head=\toprule\textbf{\#} & \rot{\textbf{Vehicles}} & \rot{\textbf{Packages}} & \rot{\textbf{Cities}} & \rot{\textbf{Locations}} & \rot{\textbf{Roads}} & \rot{\textbf{States}}\\\midrule,
    late after line=\mbox{},
    table foot=\\\bottomrule]%
{../data/seq-sat-7.csv}{Problem=\problem,Vehicles=\vehicles,Packages=\packages,Cities=\cities,Locations=\locations,%
Roads=\roads,Stateslat=\stateslat}%
{\problem & \vehicles & \packages & \cities & \locations & \roads & \stateslat}%
\caption{Problem dimensions of seq-sat-7.}
\label{tab:seq-sat-7-dims}
\end{subtable}
\quad
\begin{subtable}[t]{0.54\textwidth}
\centering
\csvreader[tabular=rrrrrrl,
    table head=\toprule\textbf{\#} & \rot{\textbf{Vehicles}} & \rot{\textbf{Packages}} & \rot{\textbf{Cities}} & \rot{\textbf{Locations}} & \rot{\textbf{Roads}} & \rot{\textbf{States}}\\\midrule,
    late after line=\mbox{},
    table foot=\\\bottomrule]%
{../data/seq-sat-8.csv}{Problem=\problem,Vehicles=\vehicles,Packages=\packages,Cities=\cities,Locations=\locations,%
Roads=\roads,Stateslat=\stateslat}%
{\problem & \vehicles & \packages & \cities & \locations & \roads & \stateslat}%
\caption{Problem dimensions of seq-sat-8.}
\label{tab:seq-sat-8-dims}
\end{subtable}
\caption[Problem dimensions of selected Transport IPC datasets.]{Problem dimensions of selected Transport IPC datasets.
The ``states'' value is a state space size estimate as discussed in Section~\ref{datasets} (in temporal domains calculated with $f_{max} = 100$ and the $\mt{GCD}$ of \texttt{fuel-demand}s equal to 1).
Bold problem instances correspond to Figure~\ref{fig:ipc08_seq-sat_p13} and Figure~\ref{fig:ipc08_tempo-sat_p30}, respectively.}
\label{tab:dataset-dimensions}
\end{table}

\begin{figure}[tbp]
\centering
\includegraphics[width=1.0\textwidth]{../img/ipc08_tempo-sat_p30_land}
\caption[Road network visualization of the \texttt{p30} problem from the tempo-sat track of IPC 2008.]{Road network visualization of the \texttt{p30} problem from the tempo-sat track of IPC 2008. Red dots represent locations (graph nodes), roads (graph edges) are represented by black arrows, vehicles are plotted as blue squares, and packages as purple squares. Darker red dots represent locations with petrol stations. In this specific problem, the circle of darker nodes in the center represents truck hubs and each of the attached subgraphs are individual cities.}
\label{fig:ipc08_tempo-sat_p30}
\end{figure}

All sequential problem instances in seq-sat datasets have symmetric roads and road lengths and can, therefore,
be simplified by assuming the use of an undirected graph.
All packages are always positioned at locations
in the initial state, not in vehicles (in all domain variants).

The temporal problems in tempo-sat-6 do not have the same properties;
the problems 1--20 have symmetric roads and lengths, but
the 21--30 problems only have symmetric roads, not lengths in general.
The same applies to fuel demands of roads. Additionally,
these problems have vehicle target locations, which means that not only packages,
but also
vehicles will need to be positioned at specific locations
after package delivery finishes. We can interpret this goal
in a similar way as in a VRP, where a vehicle target location is thought to be
a truck depot or hub. A visualization of such a problem can be seen in Figure~\ref{fig:ipc08_tempo-sat_p30}.
No sequential problem has this requirement, even though the domain formulation allows it.

Given a specific Transport problem, we can calculate the size of the set of states $S$.
For sequential Transport, the state space size can be estimated as: $$l^v \cdot (l+v)^p,$$ where $l$ is the number of locations,
$v$ the number of vehicles, and $p$ the number of packages. The formula represents
the number of choices for the location of vehicles, combined with the number of choices
for the location of packages (these include being loaded onto a vehicle). We have eliminated
invalid states arising from inconsistent $\mt{in}(p)$ and $\mt{at}(p)$ state variable values,
but some invalid states are still left in the state size estimate (for example states,
where vehicles are loaded beyond maximum capacity). We did not include a notion of
capacity in this estimate because it can be computed from the locations of packages.

For temporal Transport, the problem state space size estimate is more complicated,
due to actions being parallel. A reasonable estimate could be:
$$(l+r)^v \cdot \left(\frac{f_{max}}{\mt{GCD} \{\mt{fuel-demand}(l_1, l_2) | (l_1, l_2) \in R\}}\right)^v \cdot (l+v)^p,$$ where $R$ represents the set of roads, $r = |R|$ is the number of roads,
$\mt{GCD}$ is the greatest common divisor function,
and $f_{max}$ is the maximum fuel capacity for vehicles. The $f_{max}$ value presents a simplification, where all vehicles
have an equal maximum fuel capacity.
The formula expresses the choice of positions of vehicles (vehicles can now be in the middle
of a \drive{} action), the choice of the current fuel capacity of vehicles (cannot be simply
calculated from the other state variables, only from all previous actions),
and the choice of location for packages.

We can see that problems vary not only in size but also in what features they include
and what assumptions they make.
A summary of the acquired dataset-specific insights is available in Table~\ref{tab:problem-properties}.

\begin{table}[tb]
\centering
\begin{tabular}{cccccr}
\toprule
\multirow{3}{*}{\textbf{Dataset}} & \multirow{3}{*}{\textbf{Problems}} & \textbf{Sym.} & \textbf{Sym.} & \textbf{Vehicle} & \multirow{3}{*}{\textbf{$\approx$ \# states}}\\
& &  \textbf{road} & \textbf{fuel} & \textbf{fuel} &\\
& & \textbf{lengths} & \textbf{demands} & \textbf{locations} &\\
\midrule
seq-sat-6 & 01--30 & Yes & N/A & No & $10^{3} \to 10^{43}$\\
seq-sat-7 & 01--30 & Yes & N/A & No & $10^{25} \to 10^{59}$\\
seq-sat-8 & 01--30 & Yes & N/A & No & $10^{50} \to 10^{78}$\\\midrule
\multirow{2}{*}{tempo-sat-6} & 01--20 & Yes & Yes & No & $10^{8} \to 10^{52}$\\
& 21--30 & No & No & Yes & $10^{18} \to 10^{81}$\\
\bottomrule
\end{tabular}
\caption[Summary of problem instance properties in IPC Transport datasets.]{Summary of problem instance properties in IPC Transport datasets.
State space size estimates in temporal domains
are calculated using $f_{max} = 100$ and the $\mt{GCD}$ of \texttt{fuel-demand}s equal to 1.}
\label{tab:problem-properties}
% https://www.wolframalpha.com/input/?i=l%5Ev+*+(v%2Bl)%5Ep,+p+%3D+2,+v+%3D+2,+l+%3D+5
% https://www.wolframalpha.com/input/?i=(l%2Br)%5Ev+*+c%5Ev+*(v%2Bl)%5Ep,+p+%3D+2,+v+%3D+2,+l+%3D5,+r%3D12,+c%3D100
\end{table}


% declare document class and geometry
\documentclass[12pt]{article} % use larger type; default would be 10pt
\usepackage[margin=1in]{geometry} % handle page geometry

% import packages and commands
\input{../header2.tex}

\title{Math 217 -- Geometry and Physics -- Lec03}
\author{UCLA, Fall 2014}
\date{\formatdate{08}{10}{2014}} % Activate to display a given date or no date (if empty),
         % otherwise the current date is printed

\begin{document}
\maketitle

\section{Stuff}

Seminal paper: Witten --- Supersymmetry and Morse Theory (J. Diff. Geometry, 1982)

\subsection{Chern conjecture}

Let's go over the Chern conjecture from last time. If $M = M^{2n}$ is a compact closed manifold, then
\begin{equation}
\chi(M) = \int_M \varepsilon(TM).
\end{equation}
Given the metric $g$ on $TM$, we have the Levi-Civita connection $\nabla^g$. Then we have $R^{\nabla^g} = (\nabla^g)^2$ where $R$ is skew-symmetric, and
\begin{eqn}
\abs{Pf(R^{\nabla^g})} = \varepsilon(TM) \in H^{2n}(M).
\end{eqn}
[not sure transcribed correctly, was fast and messy on the board.] If $R^{\nabla^g} = 0$ then $\chi(M)= 0$ where
\begin{eqn}
\chi(M) = \sum_{i=0}^{2n} (-1)^i b_i, \qquad b_i = \dim_\R H^i (M).
\end{eqn}

The Chern conjecture is based on that. Conjecture: If $M^{2n}$ is an affine flat closed compact manifold, then $\chi(M) = 0$. Let's define what affine flat means. Write $M = \cup_{\alpha \in I} U_\alpha$. Then it is affine flat iff
\begin{eqn}
\varphi_\beta \circ \varphi_\alpha^{-1}(x_\alpha) = A_{\alpha\beta} x_\alpha + B_{\alpha\beta},
\end{eqn}
where $A_{\alpha\beta}, B_{\alpha\beta}$ are constant matrices $\iff$ there exists an affine connection
\begin{eqn}
\nabla : \Gamma(TM) \rightarrow \Gamma(T^* M \otimes TM)
\end{eqn}
where $\nabla(fs) = f \nabla s + f \nabla s$.

The Chern conjecture is known in 2 dimensions (the torus). Not sure if known in any other dimensions. (See Sullivan-Kostant-Milnor for special cases.)

\subsection{Poincare duality}

Says that $H^p \cong H^{n-p}$.

\begin{exercise}
As an exercise, show (cf. Bott-Tu):
\begin{enumerate}
\item $M = \R^n$.
\begin{equation}
	H^k (\R^n) =
	\begin{cases}
	0, & k = 0, \\
	\R, & k \neq 0
	\end{cases}
	\qquad
	H_c^k (\R^n) =
	\begin{cases}
	0, & k \neq n \\
	\R, & k = n.
	\end{cases}
\end{equation}
\item $M = \cup_{\alpha \in I} U_\alpha$.
\end{enumerate}
\end{exercise}

Now, given $\Delta \subseteq M$ a $p$-cycle in $M$, we have
\begin{eqn}
[\Delta] \in H_p (M, \R) \cong H^{n-p}(M, \R)
\end{eqn}
Then we can write
\begin{eqn}
\begin{matrix}
\int_\Delta \omega & : & H_\text{dR}^p(M) & \rightarrow & \R \\
&& [\omega] & \mapsto & \int_\Delta \omega
\end{matrix}
\end{eqn}
for a $p$-form $\omega$. Write $\Delta = \sum_i a_i \underbrace{\Delta_i}_{=\varphi_i(\tilde{\Delta}_i)}$ where $\tilde{\Delta}_i \subseteq [\text{something}] \overset{\varphi_i}{\longrightarrow} M$

\subsection{stuff}

[missed a lot here on whatever he was talking about]

\subsection{Lie groups}

\begin{definition}
A Lie group $G$ is a smooth manifold with a group structure in the following sense:
\begin{enumerate}
\item Multiplication $G \times G \rightarrow G$ taking $(g, g') \mapsto gg'$, and
\item Inverse $G \rightarrow G$ taking $g \mapsto g^{-1}$
\end{enumerate}
\end{definition}

Some examples are given by: $\C, \C, \Q, \Q_p, \dots$. Write $M_n(K) = \text{$n \times n$ matrices with entries in $K$}$. The group with elements $g = (a_{ij})_{n \times n} \in GL_n(\R)$ with $\det g \neq 0$ is called the general linear group. Note that any subgroup submanifold is also a Lie group.

\begin{definition}
A Lie subgroup $H$ of $G$ is a subgroup which is also a submanifold. If $H \cong \R$ then $i : \R \rightarrow G$ defines a one-parameter subgroup.
\end{definition}

Some more examples: $SL_n(\R), O_n(\R), SO_n(\R), U_n, SU_n$

\begin{definition}
A Lie group homomorphism $\psi : G \rightarrow G'$ is a map satisfying $\psi(gg') = \psi(g) \psi(g')$. If $\psi$ is a diffeomorphism then it is called an isomorphism. It will be useful to also define
\begin{itemize}
\item $R_g : G \rightarrow G$ mapping $R_g (g') = g' g$ for all $g' \in G$
\item $L_g : G \rightarrow G$ mapping $L_g (g') = g g'$ for all $g' \in G$.
\end{itemize}
\end{definition}

\begin{definition}
A vector field $X$ on $G$ is said to be left invariant if $L_g X = X$.
\end{definition}

\end{document}


\documentclass{article}

\usepackage[margin=1in]{geometry}
\usepackage{syntax}
\usepackage{float}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{arrows,automata}
\usepackage{minted}

\title{Deep Strictness: Milestone 2}
\author{Kenny, Hengchu}

\begin{document}
\maketitle

We aim to extend QuickCheck with the ability to probabilistically verify the
laziness/strictness behavior of Haskell functions. That is, we will allow users
to write a specification of the strictness behavior of a particular function,
and by fuzzing inputs to the function, determine if the function is
\emph{exactly} as strict as the user has specified.

Before describing in detail how our proposed tool will work, we give several
definitions regarding laziness in Haskell:

A lazy function takes an \emph{input} to an \emph{output}. For now, we consider
only inputs and outputs restricted to single algebraic datatypes which do not
contain functions (that is, all functions we can speak about are unary
first-order functions). We plan to extend this to multi-argument and
higher-order functions in later stages of this project.

When a lazy function is applied to an input, it is done so in a \emph{context}
which makes use of some part of its output---that is, the context may not use
the entire output data structure to continue its own computation. We call the
portion of the input which is used by the context the \emph{demand} on the
output. The semantics of lazy evaluation dictate that any portion of the input
which is not required to compute the demanded portion of the output are not
evaluated in the first place---that is, evaluation only occurs as much as is
necessary to satisfy the demand of the calling context.

For example, let us consider a function \verb|f :: Int -> Int -> Int| where
\verb|f x n = fst (x, fib n)|. Although computing the second component of the
pair in \verb|f 1 100000000| would be very expensive, this computation never
occurs because the demand placed upon the pair (extracting the first component)
only requires the first component to be evaluated. We say that \verb|f| is
\emph{strict} in its first parameter (\verb|x|) and \emph{lazy} in its second
parameter (\verb|n|).

We can capture this notion formally as a function from a given demand of the
output to a demand of the input. We call this a \emph{demand
  specification}. Here, we can concretely represent a demand as a
\emph{subshape} of the input/output---that is, a data structure corresponding to
one of these data types, but which can be truncated at any arbitrary position in
the structure of the data type. We call this derived data type the \emph{demand
  type} corresponding to a data type.

For example, the corresponding demand type for the type \verb|(Int, Int)| would be:

\verb|Demand (Demand Int, Demand Int)|

where

\begin{minted}{haskell}
data Demand a = ~   -- ^ Not demanding
              | ! a -- ^ Demanding the constructor at this level
              | *   -- ^ Demanding a primitive or nullary constructor
\end{minted}

All the values of these types are:
\verb|~, (~, ~), ! (*, ~), ! (~, *)|, and \verb|! (*, *)|. These
represent all of the possible demands which a context could place on
the type \verb|(Int, Int)|.

Suppose we have a function \verb|g :: (Int, Int) -> Int|. The corresponding
demand specification for g would be a function
\verb|gSpec :: Demand Int -> Demand (Demand Int, Demand Int)|.

Concretely, consider the function \verb|fst| (for integers):

\begin{minted}{haskell}
fst :: (Int, Int) -> Int
fst (a, b) = a
\end{minted}

An accurate demand specification for \verb|fst| is:

\begin{minted}{haskell}
fstSpec :: Demand Int -> Demand (Demand Int, Demand Int)
fstSpec ~ = ~
fstSpec * = ! (*, ~)
\end{minted}

This states that if we demand nothing from the result of \verb|fst|, we do not
need to evaluate its argument at all. (This is true for \emph{every} function in
a lazy language!) However, if we demand the resultant integer from \verb|fst|,
we must evaluate the first integer in the input pair, but we do not need to
evaluate the second element of the pair.

For functions like \verb|fst|, the demand on input is a \emph{static demand}:
that is, for each demand on the output, any input to the function will be
evaluated to the same degree.

This is not true in general, and not merely in pathological examples. Many
(perhaps most!) useful functions have dynamic demand behavior---that is, their
demand specification depends on the values of the input. We say that such
functions have \emph{dependent demand}: that is, the demand specification of
these functions must consider the input values of the function as well.

\begin{minted}{haskell}
take :: (Int, [Int]) -> [Int]
take (n, xs) =
  case (n, xs) of
    (0, _)     -> []
    (n',x:xs') -> x:(take ((n'-1), xs'))
    (n', [])   -> []
\end{minted}

Just given some demand on the output list, we can't guess the demand on the
second element of the input pair, although we do know that the first element of
the pair will always be evaluated.

Suppose we have demand the first 2 elements of some result of take, there is no
one input demand that corresponds to this output demand. However, if we know
what the first argument to \verb|take| is, we can determine the degree to which
the input list must be evaluated. Note that the demand on the output still is
another factor in determining the demand on the input, since if we demand
nothing on the output, then nothing on the input will be demanded.

The demand specification of \verb|take| could be

\begin{minted}{haskell}
takeSpec :: (Int, [Int]) -> Demand [Demand Int] -> Demand (Demand Int, Demand [Demand Int])
takeSpec _       ~    =  ~
takeSpec (0, _)  ![]  =  !(*, ~)
takeSpec (n, xs) !ds  =
  case (ds, xs) of
    ([],     _)    -> !(*, ![])
    (d:ds', x:xs') ->
      let !(_, !ds'') = takeSpec (n-1, xs') !ds' in
      !(*, !(d:ds''))
\end{minted}

Determining statically whether a function meets its demand spec is undecidable
due to the Halting Problem. So, instead of statically approximating these
behavior, we choose to apply runtime instrumentation with randomized testing,
this gives a high level of confidence.

We choose to implement this as an extension to QuickCheck (CITATION). We will
use generic programming to derive the demand types of functions under
consideration. We will use existing QuickCheck generator infrastructure to fuzz
inputs to the function, and demands on the output of the function. Because only
certain shapes of demand are realizable on a given output, we specialize the
demand generators to produce only sensible demands on each invocation of the
function. We then run the function on the random input to obtain un-evaluated
output, and place a random demand on this output while instrumenting the
function to determine the resultant demand on the input. We can then use the
demand specification to check whether the demand specification exactly matches
what the actual demand behavior of the function was in this trial.

Because we integrate with the existing functionality of QuickCheck, we can
employ its \emph{shrinking} abilities to produce minimal counterexamples of
input/demand pairs which fail to satisfy a given demand specification, if this
specification is faulty.

Note that there are two ways in which a specification may be faulty: it may fail
to produce an input demand which exactly matches the real observed input demand,
or it may fail to cover all possible realizable combinations of input and
demand.

To avoid unnecessary metaprogramming, we choose not to calculate an instrumented
version of the function under consideration. We do not need to resort to such
strategies because we can instead instrument the input data structure to report
to us how it is evaluated by the function. As such, we may treat our functions
as a black box.

In order to instrument a lazy data structure, we must use so-called ``unsafe''
features of Haskell---in particular, the function \verb|unsafePerformIO| which
allows us to attach a callback to a particular lazy value so that the callback
is run when that value is evaluated. We may traverse the generated input data
structure to add such a callback to every node in that structure. By tracking
which of these callbacks are invoked when we demand a particular part of the
output of some function applied to the instrumented structure, we produce an
exact representation of the input demand of this function given a particular
output demand and input value.

Once we have obtained a pair of generated output demand and observed input
demand, it's trivial to run the demand specification on the output demand and
check whether it exactly matches the observed demand. If it does not, we may
repeat this process by shrinking inputs to the function to compute a minimal
example exhibiting the detected violation of the demand specification.

We need not only to consider how to \emph{check} such specifications, but also
how to specify them in the first place! To that end, we will use Haskell's
generic programming features to provide a syntax for users to succinctly write
these demand specifications without worrying about the details of our
implementation. Ideally, this syntax would look very similar to the syntax we
use above, but we may need to make some concessions due to the limitations of
the implementation strategy we choose for this interface.

Moving forward, we want to allow users to check demand specifications of
multi-argument functions, higher-order functions, and functions on data
structures which themselves contain functions.

\section{Progress as of Second Milestone}

As of now, we have successfully implemented a proof-of-concept for the
instrumentation of functions over lists. By making use of a mutable
pointer-based data structure, we managed to implement this instrumentation such
that the execution of the instrumented function is a constant multiplicative
factor slower than the execution of the uninstrumented function. We have also
(mostly) implemented the module which will enable us to handle instrumenting
functions of an arbitrary number of arguments. Tomorrow, we plan to meet with
Jos\'e Manuel Calder\'on Trilla tomorrow, who's done quite a bit on strictness
analysis. We are also anticipating collaborations with Leo Lampropoulos (expert
in property based random testing) and Antal Spector-Zabusky (well versed in
generic programming in Haskell).

Here are some examples of functions that operate on the \verb|List| data type.
Each of these functions demonstrate different strictness on the input list. For
example, the lazy \verb|Context| doesn't inspect the input list at all, and thus
is completely lazy. By contrast, \verb|spineStrict| evaluates the structure of
the list without forcing its contents to be evaluated. A more complex example of
a demand context is \verb|evenStrict| which, like \verb|spineStrict|, evaluates
the entire structure of the list, but which also evaluates every other element
contained in the list.

\begin{minted}{haskell}
type Context a = a -> ()

lazy :: Context [a]
lazy = const ()

whnf :: Context [a]
whnf = flip seq ()

nthStrict :: Int -> Context [a]
nthStrict n = flip seq () . (!! n)

spineStrict :: Context [a]
spineStrict = flip seq () . foldl' (flip (:)) []

allStrict :: NFData a => Context [a]
allStrict = rnf

oddStrict :: NFData a => Context [a]
oddStrict []           = ()
oddStrict [x]          = rnf x
oddStrict (x : _ : xs) = rnf x `seq` oddStrict xs

evenStrict :: NFData a => Context [a]
evenStrict []           = ()
evenStrict [_]          = ()
evenStrict (_ : x : xs) = rnf x `seq` evenStrict xs
\end{minted}

To observe the behavior of these functions, we used the \verb|demandList|
function on each of the contexts defined above. We evaluate the same function
(\verb|take 6|) on the same data (\verb|[1..5]|), but with differing demands on
the result.

\begin{minted}{haskell}
mapM_ printDemand_primList $
   map (\context -> demandList context (take 6) [1..5]) $
      [lazy,
       whnf,
       spineStrict,
       nthStrict 2,
       allStrict,
       oddStrict,
       evenStrictt]
\end{minted}

We then print a representation of the demand on the \emph{input} which is
incurred by this evaluation. An ``O'' indicates that the data at that list cell
was forced in the corresponding context, and an underscore indicates means that
the data at its list cell was not evaluated. An ellipsis indicates that the rest
of the list was not evaluated, while a closing square bracket indicates that the
list was evaluated all the way down to its end.

\begin{verbatim}
lazy:         ...
whnf:         [_, ...
spineStrict:  [_, _, _, _, _, _]
nthStrict 2:  [_, _, O, ...
allStrict:    [O, O, O, O, O, O]
oddStrict:    [O, _, O, _, O, _]
evenStrict:   [_, O, _, O, _, O]
\end{verbatim}

The process of \emph{currying} takes a function of one argument which returns
another function of one argument and converts it into a function taking two
arguments and returning a result. Conversely, \emph{uncurrying} takes such a
function of two arguments and converts it into a function of one argument which
returns another function of one argument.

With clever type-level programming, we can generalize currying to functions with
arbitrary numbers of arguments. This is necessary to enable our library to
handle testing such ``curried'' functions, which are ubiquitous in Haskell. As
such, we have implemented the following (non-trivial) functions, which will be a
necessary part of our internal infrastructure.

\begin{minted}{haskell}
curryAll   :: function -> (Tuple (Args function) -> Result function)
uncurryAll :: (Tuple (Args function) -> Result function) -> function
\end{minted}

There is a mechanical transformation between the type of a function and the
corresponding type of its demand specification. Similarly, there is a mechanical
transformation from the type of a value to the type of the representation of the
demand upon it.

In order to perform these transformations, we use a feature in Haskell called
Type Families. Type Families allow us to write recursive functions over the
structure of types to compute other types. The Haskell compiler GHC has another
feature called Generics, which transforms any type to a generic representation
that uses sums, products and arrows. We have named our two Type Families
\verb|Spec| and \verb|Demand| respectively, and here is their definition.

\begin{minted}{haskell}
data Thunk a = T | E a

Demand (a :+: b) = Demand a :+: Demand b
Demand (a :*: b) = Thunk (Demand a) :*: Thunk (Demand b)
Demand (a -> b)  = FuncDemand

Spec (a :+: b)   = Spec a :+: Spec b
Spec (a :*: b)   = Spec a :*: Spec b
Spec (a -> b)    = (a, Spec a) -> Demand b -> Thunk (Demand a)
\end{minted}

We use the data type \verb|Thunk| to introduce an additional layer of
indirection. A value of type \verb|Demand a| can only ever be a prefix of its
corresponding value at the type \verb|a|, and thunks represent the locations
where the remaining structure can be chopped off by a lack of further evaluation
of the data structure.

With higher-order functions, uncurrying all of the arguments of the higher order
function before applying \verb|Spec| is critical to generate the most
fine-grained type of its demand specification. Consider the example \verb|map|
which maps a function over a list.
\begin{minted}{haskell}
map :: (a -> b) -> [a] -> [b]
\end{minted}

If we don't uncurry \verb|map|, then its specification will have type
\begin{figure}[H]
  \centering
\begin{minted}{haskell}
(a -> b, (a, Spec a) -> Demand b
                     -> Thunk (Demand a))
-> Demand ([a] -> [b])
-> Thunk (Demand (a -> b))
\end{minted}
\caption{curried \texttt{map} specification type}
\end{figure}

This type just simplifies to
\begin{minted}{haskell}
(a -> b, (a, Spec a) -> Demand b
                     -> Thunk (Demand a))
-> FuncDemand
-> Thunk FuncDemand
\end{minted}

\verb|FuncDemand| only captures whether the function was called or not. However,
in a typical implementation of \verb|map|, the usage of the input function
depends on whether the input list is empty or not. It's impossible to specify
the demand behavior of Haskell standard library's implementation of \verb|map|
using this type signature.

However, consider the uncurried \verb|map|, which has type
\begin{minted}{haskell}
(a -> b, [a]) -> [b]
\end{minted}

\clearpage
Applying \verb|Spec| to this type produces
\begin{figure}[H]
  \centering
\begin{minted}{haskell}
((a -> b, [a]), ((a, Spec a) -> Demand b
                             -> Thunk (Demand a), Spec [a]))
-> Demand [b]
-> Thunk (Demand [a])
\end{minted}
\caption{uncurried \texttt{map} specification type}
\end{figure}

Here, we see that this type in fact takes the input list \verb|[a]| as an input
to the specification as well! This allows a precise and correct specification of
\verb|map|. This observation applies to general higher-order functions.

We will integrate the \verb|Spec| and \verb|Demand| Type Families with GHC's
generics mechanism to automatically derive specification types of algebraic data
types. We will then write generic generators for representations of
specifications and demands, and then we will develop a presentable API for
programmers to write property based test cases to check the demand behaviors of
Haskell programs.
\end{document}


\section{Optimizations}\label{sec:method}

%Now comes the ``beef'' of the paper, where you explain what you
%did. Again, organize it in paragraphs with titles. As in every section
%you start with a very brief overview of the section.

%For this class, explain all the optimizations you performed. This mean, you first very briefly
%explain the baseline implementation, then go through locality and other optimizations, and finally SSE (every project will be slightly different of course). Show or mention relevant analysis or assumptions. A few examples: 1) Profiling may lead you to optimize one part first; 2) bandwidth plus data transfer analysis may show that it is memory bound; 3) it may be too hard to implement the algorithm in full generality: make assumptions and state them (e.g., we assume $n$ is divisible by 4; or, we consider only one type of input image); 4) explain how certain data accesses have poor locality. Generally, any type of analysis adds value to your work.

The code was optimized in an iterative manner, driven by insights from analysing and profiling the code. In a first phase, we focused on general optimizations that would not change the interfaces and generality of the implementation. In a second phase the special property of the recommender system were exploited, trading in generality for speed and interfering drastically with the interface of the library.

In both phases, we started off by reducing the OP count, without considering the computational intensity. We then profiled our code to find critical points where we could optimize memory access patterns or reorder operations to increase the performance.

We also considered to work in the logarithmic domain. This would have enabled us to express the product over the messages as a sum,  making it less likely to suffer from numerical rounding errors and sometimes making the computation faster. In our case the accuracy did not improve. Furthermore the runtime increased due to the additional overhead when transforming from/to the log domain. We therefore decided to not utilize this option.

% -- Optimize calculation of product --
\mypar{Optimize calculation of product (Phase 1)}
We observed that the incoming message product is recomputed each time a message gets updated. This is expensive because the product often spans a hundred or more factors. To improve the situation we pre-computed the product once over all messages. When changing a message the product is updated by multiplying with the new message and dividing by the old one.
Speed-up: 1.6 on the big dataset.

%TODO: sicherstellen, ziemlich früh zu erwähnen, dass alle performance angaben stark vom datensatz (size AND shape) abhängen. Konvention: wir beziehen den speedup immer auf den datensatz u1, den wir zu beginn immer verwendet haben

% -- C++ refactoring --
\mypar{Memory friendly C++ (Phase 1)}
Encouraged by profiling results, a subsequent optimization step consisted in the re-factoring of the code base with the main objective to reduce memory ops. The biggest improvement we achieved by modifying functions to operate in-place. Another improvement consisted in the creation of buffers for intermediate results and to avoid superfluous creation and destruction of data.
Speed-up: 2.0 on the big dataset.

%-- Switch to single precision
\mypar{Switch to single precision  (Phase 1)}
At this stage, the code was memory bound. To further reduce memory traffic, we enabled the support for single point precision. However, because the sum-product algorithm requires to calculate products with a large number of factors $f \in [0,1]$, a trade-off approach was chosen: the calculation of products was still performed with double precision, but the messages were stored in single precision (after normalisation). Due to decreased precision, the number of message processed was reduced by a factor of 6 while giving a total speed-up of around 12, so a net speed-up of 2.0 can be achieved. Our code can still be compiled for both single and double precision (storage) through a compiler flag.

%-- Heap/Multimap
\mypar{Using a Heap (Phase 1)}
The baseline implementation used a sorted \texttt{std::multimap} to store the residuals. Updating a specific value was, however, expensive because the \texttt{std::multimap} only supports erase/insert. We introduced a pairing heap from the boost library which provided an efficient update method. Initially the heap introducing more memory intensive operations. In our first versions this seemed to have a negative effect on the runtime. However, as we moved forward we became less memory bound, in the final version the heap results in a speed-up of 3 compared to the multimap.

\mypar{Efficient storage of messages (Phase 2)}
%-- More efficient storage of messages.
For every iteration, a possibly large number of messages is read and stored. Therefore, it is beneficial to optimize the message lookups. Because a message $m$ represents a normalized probability distribution that satisfies $\sum_{i=1}^N m_i = 1$, it suffices to store only $N-1$ values for every message. The re\-commender system uses binary variables:  like and dislike. Therefore the amount of bytes required to store a message can be divided by two.

%-- Exploit the special pattern how messages are updated
\mypar{Exploit special patterns (Phase 2)}
Pattern analysis of message products revealed a repeating pattern. Hard-coding this pattern is beneficial because an index lookup can be avoided. Technically, the optimization is comparable with a loop unrolling where the innermost loop necessary for the index lookup is flattened away.
Efficient storage of messages and exploiting special patterns result in a speed-up of 2.0 for the big dataset.

\mypar{Compressing the graph (Phase 2)}
We compress the graph by using the same factor object for all factors. This was possible because the factors do not change and are always represented by the same $2\times2$ matrix. Using only a single factor greatly reduced memory traffic and resulted in a speed-up of 1.4 for the big dataset.

%-- Precalculate reciprocals
\mypar{Pre-calculate reciprocals (Phase 2)}
The first optimization (pre-calculation of message products, with later division of a message) increased the pressure on the division unit. This turned into one of the major bottlenecks of the code. We tried to solve this issue by pre-calculating reciprocals (“1/message”) but the speed-up was not significant. We tried to utilize vectorization to compute multiple divisions at once and make use of the fast but less accurate \_mm\_rcp\_ps. Unfortunately our experiments showed that the lack of accuracy with the \_mm\_rcp\_ps instruction lead to a worse convergence of our algorithm, greatly increasing the runtime. Furthermore, vectorizing turned out to be harder than anticipated because our computations strongly depend on each other. We could not predict which message we would update next unless we finished updating the current message. This is why vectorization had not a significant impact on our performance.

%As important as the final results is to show that you took a structured, organized approach to the optimization and that you explain why you did what you did.

%Mention and cite any external resources including library or other code.

%Good visuals or even brief code snippets to illustrate what you did are good. Pasting large amounts of code to fill the space is not good.


\documentclass[11pt]{article}
\title{Numerically Solving Newton's Second Law}
\date{}

\begin{document}
\maketitle

\section{Pre-notebook}
Work through these questions with your group before you move onto the IPython notebook.

\subsection{Solving Newton's Second Law for a mass on a spring}\label{Newtons}
Write down Newton's Second Law for a mass $m$, on a spring with spring constant $k$.
Initially assume that there is no friction in the system.
\begin{itemize}
\item Can you rewrite this in terms of derivatives of the position variable?
\item Solve this differential equation and find an expression for the position of the mass as a function of time.
\end{itemize}

\subsection{Conservation of Energy}
Given the solution you found in section~\ref{Newtons}, find the velocity of the mass as a function of time.
With the position and velocity expressions, check to make sure that the system conserves energy.

Okay, now go and start working through the notebook.

\section{Numerical Updates for Position and Velocity}
We want to model our system discretely so it can be solved on a computer.
\subsection{Written Updates}
Given the ``delta t'' versions of the acceleration and velocity equations, what are the updates for $v_{t+\Delta t}$ and $x_{t+\Delta t}$.
\subsection{Coded Updates}
We've filled in the update for $x$ and $t$ for you. Write the code to update $a$

\section{Problems with the Model}
If you made your $\Delta t$ larger, you might have noticed that the oscillations started to blow up in our numerical version.
\subsection{Conservation of Energy}
Can you show if energy is conserved given the updates for $x$ and $v$ that you derived earlier?
\subsection{Adding Drag}
What if we want to add drag to the model? Write down the updates for $x$ and $v$ again with a drag term included.

\end{document}


\documentclass{article}

\usepackage{fancyhdr}
\usepackage{extramarks}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amssymb}
\usepackage{amsfonts}
\usepackage{tikz}
%\usepackage{physics}
\usepackage[plain]{algorithm}
\usepackage{algpseudocode}
\usepackage{hyperref}
\usepackage[arrowdel]{physics}

\usetikzlibrary{automata,positioning}

%
% Basic Document Settings
%

\topmargin=-0.45in
\evensidemargin=0in
\oddsidemargin=0in
\textwidth=6.5in
\textheight=9.0in
\headsep=0.25in

\linespread{1.1}

\pagestyle{fancy}
\lhead{\hmwkAuthorName}
\chead{\hmwkClass\ : \hmwkTitle}
\rhead{\firstxmark}
\lfoot{\lastxmark}
\cfoot{\thepage}

\renewcommand\headrulewidth{0.4pt}
\renewcommand\footrulewidth{0.4pt}

\setlength\parindent{0pt}

%
% Create Problem Sections
%
\newcommand{\be}{\begin{equation}}
\newcommand{\ee}{\end{equation}}
\newcommand{\bes}{\begin{equation*}}
\newcommand{\ees}{\end{equation*}}
\newcommand{\bea}{\begin{flalign*}}
\newcommand{\eea}{\end{flalign*}}

\newcommand{\enterProblemHeader}[1]{
    \nobreak\extramarks{}{Problem \arabic{#1} continued on next page\ldots}\nobreak{}
    \nobreak\extramarks{Problem \arabic{#1} (continued)}{Problem \arabic{#1} continued on next page\ldots}\nobreak{}
}

\newcommand{\exitProblemHeader}[1]{
    \nobreak\extramarks{Problem \arabic{#1} (continued)}{Problem \arabic{#1} continued on next page\ldots}\nobreak{}
    \stepcounter{#1}
    \nobreak\extramarks{Problem \arabic{#1}}{}\nobreak{}
}

\setcounter{secnumdepth}{0}
\newcounter{partCounter}
\newcounter{homeworkProblemCounter}
\setcounter{homeworkProblemCounter}{1}
\nobreak\extramarks{Problem \arabic{homeworkProblemCounter}}{}\nobreak{}

%
% Homework Problem Environment
%
% This environment takes an optional argument. When given, it will adjust the
% problem counter. This is useful for when the problems given for your
% assignment aren't sequential. See the last 3 problems of this template for an
% example.
%
\newenvironment{homeworkProblem}[1][-1]{
    \ifnum#1>0
        \setcounter{homeworkProblemCounter}{#1}
    \fi
    \section{Problem \arabic{homeworkProblemCounter}}
    \setcounter{partCounter}{1}
    \enterProblemHeader{homeworkProblemCounter}
}{
    \exitProblemHeader{homeworkProblemCounter}
}

%
% Homework Details
%   - Title
%   - Due date
%   - Class
%   - Section/Time
%   - Instructor
%   - Author
%

\newcommand{\hmwkTitle}{Pset\ \#2}
\newcommand{\hmwkDueDate}{Due on 6th February, 2019}
\newcommand{\hmwkClass}{Electromagnetism}
\newcommand{\hmwkClassTime}{}
\newcommand{\hmwkClassInstructor}{}
\newcommand{\hmwkAuthorName}{\textbf{Aditya Vijaykumar}}

%
% Title Page
%

\title{
    %\vspace{2in}
    \textmd{\textbf{\hmwkClass:\ \hmwkTitle}}\\
    \normalsize\vspace{0.1in}\small{\hmwkDueDate\ }\\
%    \vspace{3in}
}

\author{\hmwkAuthorName}
\date{}

\renewcommand{\part}[1]{\textbf{\large Part \Alph{partCounter}}\stepcounter{partCounter}\\}

%
% Various Helper Commands
%

% Useful for algorithms
\newcommand{\alg}[1]{\textsc{\bfseries \footnotesize #1}}

% For derivatives
\newcommand{\deriv}[1]{\frac{\mathrm{d}}{\mathrm{d}x} (#1)}

% For partial derivatives
\newcommand{\pderiv}[2]{\frac{\partial}{\partial #1} (#2)}

% Integral dx
\newcommand{\dx}{\mathrm{d}x}

% Alias for the Solution section header
\newcommand{\solution}{\textbf{\large Solution}}

% Probability commands: Expectation, Variance, Covariance, Bias
\newcommand{\E}{\mathrm{E}}
\newcommand{\Var}{\mathrm{Var}}
\newcommand{\Cov}{\mathrm{Cov}}
\newcommand{\Bias}{\mathrm{Bias}}

\begin{document}

\maketitle
(\textbf{Acknowledgements} - I would like to thank Junaid Majeed for discussions.)
\\

\begin{homeworkProblem}
	\textbf{Zangwill - Problem} 24.1\\
	\textbf{Part (a)}
	Given,
	\begin{align*}
	\pdv{F_i}{\dot{r}_j} &= - \pdv{F_j}{\dot{r}_i} \\
	\implies \pdv[2]{F_i}{\dot{r}_j}{\dot{r}_k} &= - \pdv{F_j}{\dot{r}_i}{\dot{r}_k}\\
	\pdv[2]{F_i}{\dot{r}_k}{\dot{r}_j}&= - \pdv{F_j}{\dot{r}_k}{\dot{r}_i}\\
	-\pdv[2]{F_k}{\dot{r}_i}{\dot{r}_j} &=  \pdv{F_k}{\dot{r}_j}{\dot{r}_i}\\
	\implies  \pdv{F_k}{\dot{r}_j}{\dot{r}_i} &=0
	\end{align*}
	From Hemholtz first relation, we know that integrating the above equation should give us an object which is antisymmetric under the exchange of indices. We can take this into account by introducing our friendly neighbourhood antisymmetric object, namely the \textit{Levi-Civita symbol}.
	\begin{equation*}
	\implies \pdv{F_i}{\dot{r}_j} =  \epsilon_{ijk} Q_k (\vb{r},t) \implies F_i = \epsilon_{ijk} Q_k (\vb{r},t) \dot{r}_j + P(\vb{r}, t)
	\end{equation*}
	Hence proved.
	\\

	\textbf{Part (b)}
	The second Helmholtz relation says,
	\begin{align*}
	\pdv{F_i}{r_j} - \pdv{F_j}{r_i} &= \dfrac{1}{2} \dv{t} \qty(\pdv{F_i}{\dot{r}_j} - \pdv{F_j}{\dot{r}_i} )\\
	\pdv{P_i}{r_j} - \pdv{P_j}{r_i} + \epsilon_{ijk} \qty(\pdv{Q_k}{r_j} + \pdv{Q_k}{r_i})  &=\dv{t} (\epsilon_{ijk} Q_k(\vb{r},t) )\\
	\pdv{P_i}{r_j} - \pdv{P_j}{r_i} + \epsilon_{ijk} \qty(\pdv{Q_k}{r_j} + \pdv{Q_k}{r_i})  &=\epsilon_{ijk} \qty(\pdv{Q_k}{t} + \pdv{Q_k}{r_a}\dot{r}_a)\\
	\end{align*}
	As we can see, there are no terms involving $ \dot{r}_a $ on the LHS. Hence, the term involving $ \dot{r}_a $ on the RHS should be zero!
	\begin{equation*}
	\implies \pdv{Q_k}{r_a}\dot{r}_a = \grad{Q} = 0
	\end{equation*}
	Next, we multiply both sides of the equation with $ \epsilon_{l j i} $, and use the fact that $ \epsilon_{lmk} \epsilon_{ijk} = (\delta_{li} \delta_{mj} - \delta_{lj} \delta_{mi}) $,
	\begin{align*}
	\epsilon_{l j i} \qty(\pdv{P_i}{r_j} - \pdv{P_j}{r_i}) + \epsilon_{l j i}\epsilon_{ijk}  \qty(\pdv{Q_k}{r_j} + \pdv{Q_k}{r_i})  &= \epsilon_{l j i} \epsilon_{ijk}  \qty(\pdv{Q_k}{t} )\\
	\epsilon_{lmk} \qty(\pdv{P_i}{r_j} - \pdv{P_j}{r_i}) +  \qty(\pdv{Q_k}{r_m} + \pdv{Q_k}{r_l} - \pdv{Q_k}{r_l} - \pdv{Q_k}{r_m})  &=  \qty(\pdv{Q_k}{t} )\\
	\therefore \epsilon_{lmk} \qty(\pdv{P_i}{r_j} - \pdv{P_j}{r_i})  &=  \qty(\pdv{Q_k}{t} )\\
	\therefore \curl{P} &= \pdv{Q}{t}
	\end{align*}

	\textbf{Zangwill - Problem} 24.2\\
	\textbf{Part (a)}\\
	\begin{align*}
	H(r,p) &= \dfrac{p^2}{2m} + g(r) + p^2 f(r) = \qty(\dfrac{1 + 2 fm}{2m}) p^2 + g(r)\\
	\implies \dot{r} &= \pdv{H}{p} = \dfrac{p}{m} + 2p f(r) \\
	\implies p &= \dfrac{m \dot{r}}{1 + 2 fm}
	\end{align*}
	\begin{equation*}
	\therefore L(r, \dot r) = \dfrac{m \dot{r}^2}{1 + 2 fm} - \dfrac{m \dot{r}^2}{2(1 + 2 fm)} - g(r) = \dfrac{m \dot{r}^2}{2(1 + 2 fm)} - g(r)
	\end{equation*}
	\textbf{Part (b)}\\
	We write the Euler Lagrange equations for this Lagrangian,
	\begin{align*}
	\dv{t} \qty(\dfrac{m \dot{r}}{1 + 2 fm}) + \pdv{g}{r} &= 0  \\
	\dfrac{m \ddot{r}}{1 + 2 fm} - \dfrac{m \dot{r}}{(1 + 2 fm)^2} \dot{r} \dv{f}{r} + \dv{g}{r}&= 0 \\
	{m \ddot{r}} - \dfrac{m \dot{r}^2}{(1 + 2 fm)} \dv{f}{r} + (1 + 2 fm)\dv{g}{r}&= 0
	\end{align*}
	As one can see from the above equation, the force depends on $ \ddot{r}, \dot{r}, f , g $.
	\\

	\textbf{Zangwill - Problem} 24.4\\
	Let's first work with $ c=1 $. We shall restore the factors of $ c $ at the end.
	\begin{align*}
	S &= - m\int \dd{s} - g \int \dd{s} \phi (\va{r}(s))\\
	&= - m\int \dd{t} \dv{s}{t} - g \int \dd{t} \dv{s}{t} \phi (\va{r}(s))\\
	S &= \int - \dfrac{m}{\gamma} \dd{t} +  \int \dfrac{- g \phi}{\gamma } \dd{t}
	\end{align*}
	From the above form of action $ S $, the Lagrangian is evidently,
	\begin{equation*}
	L = - \dfrac{m + g \phi}{\gamma} \qq{where} \gamma = \dfrac{1}{\sqrt{1 - \va{v} \vdot \va{v}}} =  \dfrac{1}{\sqrt{1 - \dot{\va{r}} \vdot\dot{\va{r}} }}
	\end{equation*}
	\begin{align*}
	\therefore \dv{t} \qty( \pdv{L}{\dot{\va{r}}}) &= \pdv{L}{\va{r}} \\
	\dv{(m + g \phi) \gamma \dot{\va{r}}}{t} &= - \dfrac{g \grad{\phi}}{\gamma}
	\end{align*}
	Restoring the factors of $ c $,
	\begin{equation*}
	\dv{(m + g \phi/c) \gamma \dot{\va{r}}}{t} = - \dfrac{g c \grad{\phi}}{\gamma}
	\end{equation*}
	This differs from the electric field force equation, and has an extra term on the left (second term).
	\\

	\textbf{Zangwill - Problem} 24.11\\
	\begin{equation*}
	L_{CS} = \int \dd^3 r [\rho \phi - \va{j} \vdot \va{A} + 1/2 \{\epsilon_0 (\va{E}^2 - c^2 \va{B}^2) - \phi  (\va{d} \vdot \va{B}/c) + \va{d} \vdot (\va{A} \cross \va{E}/c) + d_0 \va{A} \vdot \va{B}  \}]
	\end{equation*}
	It is given that the Lagrangian remains invariant under usual gauge tranformations $ \phi \rightarrow \phi + \partial_t {\lambda} $ and $ \va{A} \rightarrow \va{A} - \grad{\lambda} $. Hence,
	\begin{align*}
	L'_{CS} - L_{CS} = \int \dd^3 r [ \rho \partial_t \lambda + \va{j} \vdot \grad{\lambda} + 1/2(- \va{d} \vdot (\grad{\lambda} \cross \va{E}/c) - d_0 \grad{\lambda} \vdot \va{B} - \partial_t \lambda (\va{d} \vdot \va{B}/c)  )] \\
	= \int \dd^3 r [ \partial_t(\rho \lambda) - \lambda \partial_t \rho + \div{(\lambda \va{j})} - \lambda \div{\va{j}} + 1/2(- \va{d} \vdot (\grad{\lambda} \cross \va{E}/c) - d_0 \grad{\lambda} \vdot \va{B} - \partial_t \lambda (\va{d} \vdot \va{B}/c)  )]
	\end{align*}
	The first four terms above vanish in the integral - the first and the third because they are boundary terms and the other two because of conservation.
	\begin{align*}
	L'_{CS} - L_{CS} &= \int \dd^3 r [ 1/2(- \va{d} \vdot (\grad{\lambda} \cross \va{E}/c) - d_0 \grad{\lambda} \vdot \va{B} - \partial_t \lambda (\va{d} \vdot \va{B}/c)  )] \\
	L'_{CS} - L_{CS} &= \int \dd^3 r [ 1/2(\va{d} \vdot \lambda/c \curl{\va{E}}  - d_0 \div{\lambda \va{B}} + d_0 \lambda \div{\va{B}}  - \partial_t [\lambda (\va{d} \vdot \va{B}/c) )] + \lambda/c(\partial_t\va{d} \vdot \va{B} + \partial_t\va{B} \vdot \va{d})] \\
	&= \int \dd^3 r [ 1/2(- \va{d} \vdot\curl{(\lambda \va{E}/c)} + \lambda\va{d} \vdot\curl{( \va{E}/c)}  + d_0 \lambda \div{\va{B}} - d_0 \div - \partial_t \lambda (\va{d} \vdot \va{B}/c)  )]
	\end{align*}

	\textbf{Zangwill - Problem} 24.12\\
	\begin{equation*}
	L = \va{j} \vdot \va{A} - \rho \phi - \dfrac{1}{2} \epsilon_0 (\va{E}^2 - c^2 \va{B}^2 ) - \epsilon_0 \va{E} \vdot (
	\grad{\phi} + \dot{\va{A}} ) - \epsilon_0 c^2 \va{B} \vdot (\curl{\va{A}})
	\end{equation*}
	\begin{align*}
	\pdv{L}{\va{E}} = - \epsilon_0 \va{E} - \epsilon_0  (\grad{\phi} + \dot{\va{A}} ) &\qq{,} \pdv{L}{\dot{\va{E}}} = 0 \implies \va{E} = -\grad{\phi} - \dot{\va{A}} \\
	\pdv{L}{\va{B}} = \epsilon_0 c^2 \va{B} - \epsilon_0 c^2 \curl{\va{A}} &\qq{,}  \pdv{L}{\dot{\va{B}}} = 0 \implies \va{B} = \curl{\va{A}}\\
	\pdv{L}{\phi} = - \rho - \epsilon_0 \pdv{\va{E} \vdot \grad{\phi}}{\phi} =  - \rho + \epsilon_0 \div{\va{E}} &\qq{,} \pdv{L}{\dot{\phi}} = 0 \implies \div{\va{E}} = \dfrac{\rho}{\epsilon_0} \\
	\pdv{L}{\va{A}} = \va{j}  -\epsilon_0 c^2  \pdv{\va{B} \vdot (\curl{\va{A}})}{\va{A}} =  \va{j}  -\epsilon_0 c^2 & \pdv{(\va{A} \vdot (\curl{\va{B}}) + \div{\va{A} \cross \va{B}} )}{\va{A} } = \va{j} -\epsilon_0 c^2 \curl{\va{B}}\\
	\pdv{L}{\dot{\va{A}}} = - \epsilon_0 \va{E} &\implies \curl{B} = \dfrac{\va{j}}{\epsilon_0 c^2} + \dfrac{1}{c^2} \dot{\va{E}}
	\end{align*}
	The first two equations can be rewritten as $ \div{B} = 0 $ and $ \curl{E} = - \pdv{\va{B}}{t} $. Hence we have all the Maxwell equations.

	There are 7 primary constraints (whose momenta vanish).
\end{homeworkProblem}

\begin{homeworkProblem}[2]
	\textbf{Part (a)}\\
	Given that,
	\begin{align*}
	L_D &= \sum_a \qty(\dfrac{1}{2} m_a \va{u}_a^2 + \dfrac{1}{8c^2} m_a \va{u}_a^4 ) + \sum_a \sum_{b\ne a} \qty[ - \dfrac{q_a q_b}{8 \pi \epsilon_0 r_{ab}} + \dfrac{q_a q_b}{16 \pi \epsilon_0 c^2 r_{ab}} \qty( \va{u}_a \vdot \va{u}_b + (\va{u}_a \vdot \vu{r}_{ab}) (\va{u}_b \vdot \vu{r}_{ab}) )]   \\
	\implies \pdv{L_D}{\va{u}_a}  &= \qty( m_a \va{u}_a+ \dfrac{1}{2c^2} m_a \va{u}_a^2 \va{u}_a ) + \sum_{b\ne a} \qty[\dfrac{q_a q_b}{16 \pi \epsilon_0 c^2 r_{ab}} \qty(  2 \va{u}_b + 2 \vu{r}_{ab} (\va{u}_b \vdot \vu{r}_{ab}) )]  \\
	  \pdv{L_D}{\va{u}_a}  &= \underbrace{ \sum_{b\ne a} \qty[\dfrac{q_b}{8 \pi \epsilon_0 c^2 r_{ab}} \qty(  \va{u}_b + \vu{r}_{ab} (\va{u}_b \vdot \vu{r}_{ab}) )]}_{\va{p}_a^{kin}}  +q_a   \underbrace{ \sum_{b\ne a} \qty[\dfrac{q_b}{8 \pi \epsilon_0 c^2 r_{ab}} \qty(  \va{u}_b + \vu{r}_{ab} (\va{u}_b \vdot \vu{r}_{ab}) )]}_{\va{A}_a}
	\end{align*}
	which is the required form.

	\textbf{Part (b)}\\

	\textbf{Part (c)}\\
	Consider,
	\begin{align*}
	\pdv{r_{ab}}{\va{r}_a} &= \pdv{\sqrt{(\va{r}_a - \va{r}_b)\vdot (\va{r}_a - \va{r}_b)}}{\va{r}_a} = \dfrac{2 }{2 r_{ab}} (\va{r}_a - \va{r}_b) = \vu{r}_{ab} \qq{and} \\
	\pdv{\vu{r}_{ab}}{\va{r}_a} &= \pdv{(\va{r}_{ab}/r_{ab})}{\va{r}_a} = \dfrac{1}{r_{ab}}  - \dfrac{\va{r}_{ab} \vdot \vu{r}_{ab}}{r_{ab}^2} = 0
	\end{align*}
	\begin{align*}
	\implies \pdv{L_D}{\va{r}_a} &=  2\sum_{b\ne a} \qty[ - \dfrac{q_a q_b}{8 \pi \epsilon_0 r_{ab}^2} \vu{r}_{ab} + \dfrac{q_a q_b \vu{r}_{ab}}{16 \pi \epsilon_0 c^2 r_{ab}^2} \qty( \va{u}_a \vdot \va{u}_b + (\va{u}_a \vdot \vu{r}_{ab}) (\va{u}_b \vdot \vu{r}_{ab}) )]    \\
	-q_a \pdv{\va{r}_a} (\phi_a - \va{u}_a \vdot \va{A}_a ) &=  \sum_{b\ne a} \qty[ - \dfrac{q_a q_b}{4 \pi \epsilon_0 r_{ab}^2} \vu{r}_{ab} + \dfrac{q_a q_b \vu{r}_{ab}}{8 \pi \epsilon_0 c^2 r_{ab}^2} \qty( \va{u}_a \vdot \va{u}_b + (\va{u}_a \vdot \vu{r}_{ab}) (\va{u}_b \vdot \vu{r}_{ab}) )]   \\
	\implies \pdv{L_D}{\va{r}_a} &= -q_a \pdv{\va{r}_a} (\phi_a - \va{u}_a \vdot \va{A}_a ) = -q_a \grad_a{\phi_a}  + q_a \grad_a{(\va{u}_a \vdot \va{A}_a)}
	\end{align*}
	Note that, in the first expression, there is an extra factor of $ 2 $ due to the summation over $ a $.

	\textbf{Part (d)}\\
	We write our equations of motion,
	\begin{align*}
	\dv{t} \qty(\pdv{L_D}{\va{u}_a} ) &= \pdv{L_D}{\va{r}_a} \\
	\dv{\va{p}_a^{kin}}{t} + q_a \dot{\va{A}}_a &= -q_a \grad_a{\phi_a}  + q_a \grad_a{(\va{u}_a \vdot \va{A}_a)}
	\end{align*}
	Using $ \grad{(\va{a} \vdot \va{b}) } = (\va{a} \vdot \grad) \va{b} + (\va{b} \vdot \grad) \va{a} + \va{a} \cross (\curl{\va{b}}) +  \va{b} \cross (\curl{\va{a}} )  $,
	\begin{align*}
	\dv{\va{p}_a^{kin}}{t} + q_a \partial_t\va{A}_a + q_a ( \va{u}_a \vdot \grad_a )\va{A}_a &= -q_a \grad_a{\phi_a}  + q_a ((\va{u}_a \vdot \grad_a) \va{A}_a + (\va{A}_a \vdot \grad_a) \va{u}_a + \va{u}_a \cross (\curl{\va{A}_a}) +  \va{A}_a \cross (\curl{\va{u}_a }) ) \\
	\dv{\va{p}_a^{kin}}{t} + q_a \partial_t\va{A}_a &= -q_a \grad_a{\phi_a}  + q_a ((\va{u}_a \vdot \grad_a) \va{A}_a + (\va{A}_a \vdot \grad_a) \va{u}_a + \va{u}_a \cross (\curl{\va{A}_a}) +  \va{A}_a \cross (\curl{\va{u}_a }) )
	\end{align*}
\end{homeworkProblem}

\begin{homeworkProblem}
	\textbf{Part (a)}\\
	\begin{equation*}
	L_{BI} = \dfrac{B_0^2}{\mu_0} - \dfrac{B_0}{\mu_0} \sqrt{B_0^2 + \va{B}^2 - \dfrac{1}{c^2} \va{E}^2  - \dfrac{(\va{E} \vdot \va{B})^2}{(cB_0)^2}}
	\end{equation*}
	We know that,
	\begin{align*}
	\va{D} = \pdv{L_{BI}}{\va{E}} &= - \dfrac{B_0}{2\mu_0} \dfrac{-\dfrac{2 \va{E}}{c^2} - \dfrac{2(\va{E}\vdot \va{B}) \va{B}}{c^2 B_0^2}}{\sqrt{B_0^2 + \va{B}^2 - \dfrac{1}{c^2} \va{E}^2  - \dfrac{(\va{E} \vdot \va{B})^2}{(cB_0)^2}}} \\
	\implies c\va{D} &=  \dfrac{B_0}{\mu_0 c} \dfrac{{ \va{E}} + \dfrac{(\va{E}\vdot \va{B}) \va{B}}{ B_0^2}}{\sqrt{B_0^2 + \va{B}^2 - \dfrac{1}{c^2} \va{E}^2  - \dfrac{(\va{E} \vdot \va{B})^2}{(cB_0)^2}}} \\
	\implies c\va{D}&= \eta_E (\va{E} + \alpha c \va{B})
	\end{align*}
	\begin{align*}
	\va{H} = -\pdv{L_{BI}}{\va{B}} &=  \dfrac{B_0}{2 \mu_0} \dfrac{{2 \va{B} - \dfrac{2  (\va{E} \vdot \va{B}) \va{E}}{(cB_0)^2}}}{\sqrt{B_0^2 + \va{B}^2 - \dfrac{1}{c^2} \va{E}^2  - \dfrac{(\va{E} \vdot \va{B})^2}{(cB_0)^2}}} \\
	\va{H} &= \dfrac{B_0}{ \mu_0 c} \dfrac{{ c\va{B} - \dfrac{  (\va{E} \vdot \va{B}) \va{E}}{cB_0^2}}}{\sqrt{B_0^2 + \va{B}^2 - \dfrac{1}{c^2} \va{E}^2  - \dfrac{(\va{E} \vdot \va{B})^2}{(cB_0)^2}}}\\
	\implies \va{H} &= \eta_E(c \va{B} - \alpha \va{E})
 	\end{align*}
 	From the above expressions, one can write,
 	\begin{align*}
 	c \va{D} \vdot \va{H} &= \eta_E^2 [- \alpha\va{E}^2 + \alpha c^2 \va{B}^2 + (1 - \alpha^2 )c \va{E} \vdot \va{B}] \\
 	&= \eta_E^2 [- \alpha\va{E}^2 + \alpha c^2 \va{B}^2 + (1 - \alpha^2 )c^2 \alpha B_0^2 ]\\
 	&= \eta_E^2 (c^2 \alpha B_0^2 )\qty[- \dfrac{\va{E}^2 }{c^2 B_0^2}+  \dfrac{\va{B}^2 }{B_0^2}+ (1 - \alpha^2 )]\\
 	c \va{D} \vdot \va{H} &= \eta_E^2 \va{E} \vdot \va{B} c \dfrac{1}{\eta_E^2 (\mu_0 c)^2}\\
 	\implies  (\mu_0 c)^2 \va{D} \vdot \va{H} &=\va{E} \vdot \va{B} \\
 	\implies \alpha &= \dfrac{(\mu_0 c)^2 \va{D} \vdot \va{H}}{c B_0^2}
 	\end{align*}
 	\textbf{Part (b)}\\
 	\begin{align*}
 	\va{D}^2 - \dfrac{1}{c^2} \va{H}^2 &= \dfrac{\eta_E^2}{c^2} (\va{E}^2 + \alpha^2 c^2 \va{B}^2 + 2 \alpha c \va{E} \vdot \va{B} - c^2 \va{B}^2 - \alpha^2 \va{E}^2 + 2 \alpha c \va{E} \vdot \va{B}) \\
 	&= \dfrac{\eta_E^2}{c^2} ((1- \alpha^2)(\va{E}^2 - c^2 \va{B}^2) + 4 \alpha^2 c^2 B_0^2 )
 	\end{align*}
 	But we know,
 	\begin{align*}
 	\eta_E = \dfrac{1}{ \mu_0 c} \dfrac{1}{\sqrt{1 - \alpha^2 + \dfrac{1}{B_0^2} \qty(\va{B}^2 - \dfrac{1}{c^2} \va{E}^2)}} \implies B_0^2 \qty(\dfrac{1}{(\mu_0 c)^2 \eta_E^2} + \alpha^2 -1) = \qty(\va{B}^2 - \dfrac{1}{c^2} \va{E}^2) \\
 	\eta_H = \dfrac{\mu_0 c}{\sqrt{1 - \alpha^2 + \qty(\dfrac{\mu_0 c}{B_0})^2 \qty(\va{D}^2 - \dfrac{1}{c^2} \va{H}^2)}} \implies \qty[ \qty(\dfrac{\mu_0c}{\eta_H})^2 + \alpha^2 -1 ] \qty(\dfrac{B_0}{\mu_0 c})^2 = \va{D}^2 - \dfrac{1}{c^2} \va{H}^2
 	\end{align*}
 	Hence, we can rewrite the expression as,
 	\begin{align*}
	 \qty[ \qty(\dfrac{\mu_0c}{\eta_H})^2 + \alpha^2 -1 ] \qty(\dfrac{B_0}{\mu_0 c})^2  &= - {\eta_E^2} \qty((1- \alpha^2) \qty[ B_0^2 \qty(\dfrac{1}{(\mu_0 c)^2 \eta_E^2} + \alpha^2 -1)] + 4 \alpha^2 c^2 B_0^2 ) \\
	 \qty(\dfrac{\mu_0c}{\eta_H})^2 + \alpha^2 -1   &=  (\alpha^2 - 1) \qty[ {1} + {\eta_E^2}(\mu_0 c)^2 (\alpha^2 -1)] - 4 \alpha^2 c^2 {\eta_E^2} (\mu_0 c)^2 \\
	  \qty(\dfrac{\mu_0c}{\eta_H})^2   &= (\mu_0 c)^2 (\alpha^2 - 1)  {\eta_E^2} (\alpha^2 -1) - 4 \alpha^2 c^2 {\eta_E^2} \\
	   \qty(\dfrac{1}{\eta_H})^2   &=  {\eta_E^2} \qty[(\alpha^2 - 1)^2  - {4 \alpha^2 c^2 }] = \eta_E^2 (\alpha^2 + 1)^2
 	\end{align*}
 	\begin{equation*}
 	\qq{Hence,} \eta_E \eta_H (1 + \alpha^2) = 1
 	\end{equation*}
 	We know,
 	\begin{equation*}
 	\va{H} = \eta_E(c \va{B} - \alpha \va{E}) \qq{and} c\va{D} = \eta_E (\va{E} + \alpha c \va{B})
 	\end{equation*}
 	Hence,
 	\begin{align*}
 	\alpha c \va{D} + \va{H} = \eta_E c ( 1 + \alpha^2 ) \va{B} &\implies \va{B} = {\eta_H} \qty({\alpha  \va{D} + \dfrac{\va{H}}{c}}) \\
 	c \va{D} - \alpha \va{H} = \eta_E (1 + \alpha^2 ) \va{E} &\implies \va{E} = \eta_H (c \va{D} - \alpha \va{H})
 	\end{align*}
 	Applying $ \dfrac{\va{E}}{\mu_0 c} \leftrightarrow \va{H}$ and $ \dfrac{\va{B}}{\mu_0 c} \leftrightarrow \va{D} $, we have,
 	\begin{align*}
 	\eta_H = \dfrac{\mu_0 c}{\sqrt{1 - \alpha^2 + \qty(\dfrac{1}{B_0})^2 \qty(\va{B}^2 - \dfrac{1}{c^2} \va{E}^2)}} &= (\mu_0 c)^2 \eta_E\\
 	\dfrac{\va{E}}{\mu_0 c} = \eta_E(\mu_0 c^2 \va{D} - \alpha \mu_0 c \va{H}) &\implies \va{E} = (\mu_0 c)^2 \eta_E(c \va{D} - \alpha \va{H}) = \eta_H (c \va{D} - \alpha \va{H}) \\
 	c\dfrac{\va{B}}{\mu_0 c} = \eta_E (\mu_0 c) (\va{H} + \alpha c \va{D}) &\implies \va{B} = {\eta_H} \qty({\alpha  \va{D} + \dfrac{\va{H}}{c}}) \\
 	 \mu_0 c \va{D} = {\eta_H} \dfrac{1}{\mu_0 c} \qty({\alpha  \va{B} + \dfrac{\va{E}}{c}}) &\implies c\va{D} = \eta_E (\va{E} + \alpha c \va{B})\\
 	 \mu_0 c \va{H} = \dfrac{\eta_H}{\mu_0 c} (c \va{B} - \alpha \va{E}) &\implies \va{H} = \eta_E(c \va{B} - \alpha \va{E})
 	\end{align*}
 	Thus, we have shown that BI theory has a duality transformation.

 	\textbf{Part (c)}\\
	\begin{align*}
	\delta  L_{BI} =& - \dfrac{B_0}{2\mu_0} \dfrac{1}{\sqrt{B_0^2 + \va{B}^2 - \dfrac{1}{c^2} \va{E}^2  - \dfrac{(\va{E} \vdot \va{B})^2}{(cB_0)^2}}} \qty(2 \va{B} \vdot \delta \va{B} - \dfrac{2 \va{E} \vdot \delta \va{E}}{c^2} - \dfrac{2 \va{E} \vdot \va{B}}{(cB_0)^2} (\va{B} \vdot \delta \va{E} + \va{E} \vdot \delta \va{B})) \\
	&= c \eta_E (\va{E} \vdot \delta \va{E} ) = - \div{\qty(\eta_E \dfrac{\va{E}}{c} \delta \phi)} - \partial_t \qty(\eta_E \dfrac{\va{E}}{c} \vdot \delta \va{A}) + \div{\qty(\eta_E \dfrac{\va{E}}{c})} + \partial_t \qty{\eta_E \dfrac{\va{E}}{c}} \vdot \delta \va{A}
	\end{align*}
	\begin{align*}
	c \eta_E (\va{E} \vdot \delta \va{E} ) &= - \div{\qty(\eta_E \dfrac{\va{E}}{c} \delta \phi)} - \partial_t \qty(\eta_E \dfrac{\va{E}}{c} \vdot \delta \va{A}) + \div{\qty(\eta_E \dfrac{\va{E}}{c})} + \partial_t \qty{\eta_E \dfrac{\va{E}}{c}} \vdot \delta \va{A} \\
	-c \eta_E (\va{B} \vdot \delta \va{B} ) &= \div{(c \eta_E \va{B} \cross \delta \va{A})}  = \delta \va{A} \vdot \curl{c \eta_E \va{B}}\\
	\eta_E\alpha \va{E} \vdot \delta \va{B} &= \eta_E \alpha \va{E} \vdot \curl{\delta \va{A}} - \delta \va{A} \vdot \curl{(c \eta_E \va{B})}\\
	\eta_E\alpha \va{B} \vdot \delta \va{E} &= - \div{(\eta_E \alpha \va{E} \cross \delta \va{A})} - \partial_t (\eta_E \alpha \va{B} \vdot \delta \va{A})
	\end{align*}

 	\textbf{Part (d)}\\
 	Including the point charge,
 	\begin{equation*}
 	L = \dfrac{B_0^2}{\mu_0} - \dfrac{B_0}{2\mu_0} \sqrt{B_0^2 + \va{B}^2 - \dfrac{1}{c^2} \va{E}^2  - \dfrac{(\va{E} \vdot \va{B})^2}{(cB_0)^2}} + \va{J} \vdot \va{A} - \rho \phi
 	\end{equation*}
\end{homeworkProblem}
\end{document}


\section{IBE and AKE}

This section aims to describe the IBE and AKE schemes developed as part of the SAFEcrypto project from an implementation perspective. It should be noted that both schemes are almost identical.

\subsection{Key Generation}

IBE and AKE rely upon the algorithm in Figure \ref{fig:ibe_keygen} to generate the public key \textit{h} and \textit{Master Secret Key \textbf{B}}. In IBE the \textit{Extract} function (see Figure \ref{fig:ibe_extract}) is detached from Key Generation as it is required to derive a secret key for each user from the master key, whilst it is incorporated into AKE's key generation function as it effectively has only one user \textit{id} (see Figure \ref{fig:ake_digital_signature}).

The key generation function is quite involved. The principle operations involve repeated trials of the randomly generated keys \textit{(f, g)} with Extended GCD and GCD functions until co-prime relationships have been found. An added complexity of this step is the need to compute the GCD's using large integer arithmetic in order to obtain an integer GCD and Bezout coefficients.

\begin{figure}[H]
\centering
\includegraphics[width=11cm]{ibe_keygen.png}
\caption{IBE KeyGen}
\label{fig:ibe_keygen}
\end{figure}

\begin{figure}[H]
\centering
\includegraphics[width=11cm]{ibe_extract.png}
\caption{IBE Extract}
\label{fig:ibe_extract}
\end{figure}

\begin{figure}[H]
\centering
\includegraphics[width=9cm]{ake_hash_and_sign.png}
\caption{Whole is less... Digital Signature Scheme}
\label{fig:ake_digital_signature}
\end{figure}

\subsection{IBE Encrypt and Decrypt}

The IBE encrypt and decrypt operations involve mapping binary bits onto the lattice and recovering them - in a similar manner to the RLWE encryption scheme. It should be noted that this scheme uses large modulus values to reduce the failure probability of the encryption operation - therefore decryption failures should be expected at a negligible rate during any testing of the scheme.

\begin{figure}[H]
\centering
\includegraphics[width=14cm]{ibe_encrypt.png}
\caption{IBE Encrypt}
\label{fig:ibe_encrypt}
\end{figure}

\begin{figure}[H]
\centering
\includegraphics[width=14cm]{ibe_decrypt.png}
\caption{IBE Decrypt}
\label{fig:ibe_decrypt}
\end{figure}

\subsection{Extract and Signing}

AKE signing is almost identical to IBE extract, both requiring Gaussian Sampling over a lattice.

\subsection{AKE Signatures with Message Recovery}

AKE also defines a \textit{Signature with Message Recovery} scheme (see Figure \ref {fig:ake_signature_with_recovery}) that can be used to produce smaller signatures at the cost of more CPU cycles. Instead of sending the signature as $(s_1, m)$ and recovering $s_2$, it is instead transmitted as $(s_1, s_2)$ and the message $m$ is recovered by the verifier.

\begin{figure}[H]
\centering
\includegraphics[width=11cm]{ake_with_message_recovery.png}
\caption{Whole is less... Digital Signature Scheme with Message Recovery}
\label{fig:ake_signature_with_recovery}
\end{figure}

\subsection{Software Implementation}

The generic AKE scheme shown in Figure \ref{fig:generic_ake} indicates that the signature keys are typically generated once whilst the KEM keys are generated on a per-session basis. Therefore the complexity of AKE \textit{SigKeyGen} should not be a burden as it can effectively be treated as an off-line computation. Real performance gains in a system will be achieved by optimising the regularly used \textit{Sign, Verify, KEMKeyGen, Encapsulate} and \textit{Decapsulate} operations. Similarly for IBE, the regularly used \textit{Extract, Encrypt} and \textit{Decrypt} functions should be the focus of optimisation.

\begin{figure}[H]
\centering
\includegraphics[width=9cm]{ake_kem.png}
\caption{AKE KEM}
\label{fig:ake_kem}
\end{figure}

\begin{figure}[H]
\centering
\includegraphics[width=7cm]{generic_ake.png}
\caption{A Generic AKE Construction from a KEM and a Digital Signature}
\label{fig:generic_ake}
\end{figure}

The main areas of work that are foreseen in implementing both IBE and AKE within the SAFEcrypto library are the following:

\begin{enumerate}[1]
\item A generic implementation of the IBE \textit{Master\_Keygen} function to be used by both IBE and AKE.
\begin{enumerate}[a]
\item Big integer arithmetic functions (add/sub, multiply, divide, GCD) [currently using GMP, libtommath has been added to the build system].
\item XGCD with integer polynomial coefficients (NTL and FLINT have this function and can be used as a reference).
\end{enumerate}
\item Implementing a discrete Gaussian Sampling scheme over a lattice using the polynomial basis \textit{\textbf{B}}, i.e. IBE Extract and AKE SigKeyGen.
\item Verifying the correctness of the already implemented AKE KEM as one of the parameter sets appears to be incorrect.
\item Acceleration of the Whole KEM scheme, in particular inversion modulo 2 used in \textit{KEMKeyGen}.
\item Re-factoring the RLWE Encryption encrypt/decrypt source code for IBE encrypt/decrypt.
\end{enumerate}


\documentclass[12pt]{article}
\usepackage[usenames]{color} %used for font color
\usepackage{amsmath, amssymb, amsthm}
\usepackage{wasysym}
\usepackage[utf8]{inputenc} %useful to type directly diacritic characters
\usepackage{graphicx}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{float}
\usepackage{mathtools}
\usepackage [english]{babel}
\usepackage [autostyle, english = american]{csquotes}
\MakeOuterQuote{"}
\graphicspath{ {./} }
\newcommand{\Z}{\mathbb{Z}}
\newcommand{\N}{\mathbb{N}}
\newcommand{\R}{\mathbb{R}}
\newcommand{\Q}{\mathbb{Q}}
\newcommand{\prob}{\mathbb{P}}
\newcommand{\degrees}{^{\circ}}
\DeclarePairedDelimiter\ceil{\lceil}{\rceil}
\DeclarePairedDelimiter\floor{\lfloor}{\rfloor}

\author{Tianshuang (Ethan) Qiu}
\begin{document}
\title{Math 74, Week 15}
\maketitle

\section{Mon Lec, 2b}
$g_2 = \sqrt{a_1a_2}$, so $(1+g_2)^2 = 1 + a_1a_2 + 2\sqrt{a_1a_2}$
\newline
Our left hand side should be $(1+a_1)(1+a_2) = 1 + a_1a_2 + a_1 + a_2$. By Am-GM, $LHS \geq RHS$
\newline
Now we consider 3 elements. $g_3 = \sqrt[3]{a_1a_2a_3}$, and $(1+g_3)^3 = 1 + a_1a_2a_3 + 3\sqrt[3]{a_1a_2a_3} + 3(a_1a_2a_3)^{2/3}$.
\newline
Now LHS has $(1+a_1)(1+a_2)(1+a_3) = 1 + a_1 + a_2 + a_3 + a_1a_2 + a_1a_3 + a_2a_3 + a_1a_2a_3 $
Here we can cancel the $1$ on both sides, and by AM-GM we have $a_1+a_2+a_3 \geq 3\sqrt[3]{a_1a_2a_3}$. Now let the three terms be $a_1a_2$, $ a_1a_3$, and $ a_2a_3$. By AM-GM we have $a_1a_2 + a_1a_3 + a_2a_3 \geq 3 \sqrt[3]{a_1^2a_2^2a_3^2}$.
\newline
Thus we have shown that $LHS \geq RHS$ term by term.

\section{Mon Lec, 3c}
Since our plane passes through the point $(5,9,12)$, we know that the equation of a plane can be given by $\frac{x}{r} +
\frac{y}{s} + \frac{z}{t} = 1$.
Furthermore we have $\frac{5}{r} + \frac{9}{s} + \frac{12}{t} = 1$. Now we apply the Hamonic Mean-GM inequality:
$$\frac{3}{\frac{5}{r} + \frac{9}{s} + \frac{12}{t}} \leq \sqrt[3]{\frac{rst}{540}}$$
Now from the equation of the plane we know that $LHS = 3$, so now $\sqrt[3]{\frac{rst}{540}} \geq 3$, $\frac{rst}{540} \geq 27$
Finally, since the volume of this terahedron is equal to $\frac{1}{2}rst$, we know that $V \geq 7290$.
\newline
When the terms $\frac{5}{r}, \frac{9}{s},\frac{12}{t}$ are equal, we have $V = 7290$. Furthermore their sum is equal to 1. Therefore they are eaech a third. $r = 15, s = 27, t = 36$.
\newpage

\section{Mon Dis, 5c}
If two functions are convex, then their second derivatives must be non-negative. Then the sum must have a second derivatie that i also non-negative. Therefore this sum must also be convex.

\newpage

\section{Wed Lec, 2}
\subsection{c}
$g(0)=1$, $g(1) = 1$. The second derivative of $\frac{1}{x+1}$ is $\frac{1}{(x+1)^3}$, which on the domain of $[0,1]$, is positive. The second half to the function is linear and therefore convex. The sum of two convex functions is also convex, thus $g$ is convex on $[0,1]$
\newline
By the convex function theorem the maximum of $g$ on $[0,1]$ is $1$, so $g(x)\leq 1$.

\subsection{e}
We can first fix $b, c$ in $[0,1]$, in this case our function consists of a constant, a linear function, and two convex functions (from $c$ we know that the second derivative of fractional functions is positive).
Thus by our theorem that the sum of convex functions is convex, the original function is also convex.
\newline
Now we find the values of these functions at the end points: $f(0) = 1, f(1) = 1$. The above is true also for fixed $a,b$ or $a,c$, thus the original is maximized when they are either $0$ or $1$. Finally we can use the convex theorem to state that the original function must be less than or equal to $1$ on the domain $[0,1]$.

\section{Wed Lec, 4}
\subsection{b}
$f(x) = \frac{1}{x}$. Its second derivative is positive for $x>0$. By JI,
$$\frac{f(x_1)+f(x_2)+...+f(x_n)}{n} \geq f(\frac{x_1+x_2+...+x_n}{n})$$
$$\frac{\frac{1}{x_1}+...+\frac{1}{x_n}}{n} \geq \frac{n}{x_1+x_2+...+x_n}$$
The above inequality is equivalent to
$$\frac{n}{\frac{1}{x_1}+...+\frac{1}{x_n}} \leq \frac{x_1+x_2+...+x_n}{n}$$
, which is true due to the Arithmetic-Harmonic mean inequality.

\subsection{c}
$f(x) = x^{7/3}$. Its second derivative is positive for $x>0$. By JI,
$$\frac{f(x_1)+f(x_2)+...+f(x_n)}{n} \geq f(\frac{x_1+x_2+...+x_n}{n})$$
$$\frac{x_1^{\frac{7}{3}} + ... x_n^{\frac{7}{3}}}{n} \geq (\frac{x_1+x_2+...+x_n}{n})^{\frac{7}{3}}$$
The above inequality is equivalent to
$$(\frac{x_1^{\frac{7}{3}} + ... x_n^{\frac{7}{3}}}{n})^{\frac{3}{7}}\geq \frac{x_1+x_2+...+x_n}{n}$$
, which is true due to the Arithmetic-Power mean inequality. This power mean is equal to $\frac{7}{3} > 1$, so it is greater than or equal to the arithmetic mean.

\section{Wed Lec, 5c}
Per the algebraic definition for convex functions: $(\lambda x_1+(1- \lambda)x_2) \geq \lambda f(x_1)+(1- \lambda)f(x_2)$
By our assumption $\frac{f(x_1)+f(x_2)+f(x_3)+f(x_4)}{4} \geq f(\frac{x_1+x_2+x_3+x_4}{4})$
\newline
$\frac{x_1+x_2+x_3+x_4+x_5}{5} = \frac{4}{5}\frac{x_1+x_2+x_3+x_4}{4} + \frac{x_5}{5}$. let $\lambda = \frac{4}{5}$, and since the function is convex we have
$$f(\frac{x_1+x_2+x_3+x_4+x_5}{5}) = \lambda f(y_1)+(1- \lambda)f(y_2) \leq (\lambda f(y_1)+(1- \lambda)f(y_2))$$
$$ = \frac{4}{5}f(x_1+x_2+x_3+x_4) + \frac{1}{5}f(x_5) $$
Now by our inductive hypothesis the last term is less than or equal to $\frac{4}{5}\frac{f(x_1)+f(x_2)+f(x_3)+f(x_4)}{4} + \frac{f(x_5)}{5} = \frac{f(x_1)+f(x_2)+f(x_3)+f(x_4)+f(x_5)}{5}$
Thus we have shown that
$$f(\frac{x_1+x_2+x_3+x_4+x_5}{5}) \leq \frac{f(x_1)+f(x_2)+f(x_3)+f(x_4)+f(x_5)}{5}$$
And our proof is complete.
\newpage

\section{Wed Dis, 3a}
We know that both sides of the equation is positive, so the inequality is equivalent to us taking the natural log of both sides
$$\ln{x^x} \geq \ln{(\frac{x+1}{2})^{x+1}}$$
$$x \ln{x} \geq (x+1)\ln{\frac{x+1}{2}}$$
Now consider the function $y\ln(y)$ and two values $1, x$. By Jensen's inequality we have $(\ln(1)+x\ln(x))/2 \geq \frac{x+1}{2}\ln\frac{x+1}{2}$, which is identical to our initial statement when we multiply both sides by 2.

\newpage

\section{Friday Lec, 2}
\subsection{a}
$x_1 = 10, x_2 = 36, x_3 = 74$.
$$AM = \frac{x_1+x_2+x_3}{3} = 40$$
$$HM = \frac{3}{\frac{1}{10} + \frac{1}{36} + \frac{1}{74}} = \frac{19980}{941} \approx 21.2 < AM$$
Property holds.

\subsection{b}
$$x_1 = 10, x_2 = 36, x_3 = 74, AM = 40, HM \approx 21.2$$
$$x_1 = 10, x_2 = 40, x_3 = 70, AM = 40, HM \approx 21.5$$
$$x_1 = 40, x_2 = 40, x_3 = 40, AM = 40, HM = 40$$
Performed this operation twice.

\subsection{c}
Given $x_1<a<x_2$, we compute $x_1+x_2-a+a = x_1+x_2$. Therefore the sum of these two before and after the operation is the same, and the arithmetic change remains constant.
\newline
Now consider $\frac{1}{x_1}+\frac{1}{x_2} = \frac{x_1+x_2}{x_1x_2}$. $\frac{1}{a}+\frac{1}{x_1+x_2-a} = \frac{x_1+x_1}{x_1a+x_2a-a^2}$. In order to compare the size of the denominators, we take their difference: $x_1x_2 -(x_1a+x_2a-a^2) = (x_1)(x_2-a) - a(x_2-a) = (x_2-a)(x_1-a) $. Since $x_1<a<x_2$, the difference is negative.
Thus the denominator of the latter is larger, so $\frac{1}{x_1}+\frac{1}{x_2}>\frac{1}{a}+\frac{1}{x_1+x_2-a}$

\subsection{d}
This smoothing proccess eventually stops when all terms are equal to the arithmetic mean, or when all the terms are the same. This process means that for any number, we have a process in which we can inrease its harmonic mean and eventually be equal to the arithmetic mean.
This process implies that the harmonic mean is less than or equal to the arithmetic mean, otherwise this process would have been erroneous. Therefore it proves the AM-HM inequality.
\end{document}


\documentclass[11pt]{article}
%\usepackage{fancyheadings}
\usepackage{wrapfig}
\usepackage{epsfig}
\usepackage{hyperref}
\setlength{\headheight}{0pt}
%\setlength{\footheight}{0pt}
\setlength{\topmargin}{-.5in}
\setlength{\oddsidemargin}{-0.25in}
\setlength{\evensidemargin}{-0.25in}
\setlength{\textwidth}{7truein}
\setlength{\textheight}{9truein}
\setlength{\parskip}{6pt}

\begin{document}

\section*{Social Distancing}

%\subsection*{Description}

\begin{wrapfigure}{r}{3in}
\vspace{-10pt}
\epsfig{figure=restaurant,width=3in}
\vspace{-30pt}
\end{wrapfigure}

You own a restaurant, and you want your customers to be as happy as possible. You've learned over the years that customers really don't like sitting very close to each other. They prefer if others cannot hear their conversations. This however, is a pretty hard problem, so you've decided to construct an algorithm that can figure out where to seat your patrons to maximize the distance between them.

Your restaurant is in an old building, and thus has a peculiar shape. The restuarant is very long and not very wide (it was the cheapest space available for lease!). Because of this your tables are all in a horizontal line down your restaurant, but the distances between the tables vary. Given a list of the positions of the tables in your restaurant, and the number of patrons that wish to be seated, return the maximum distance between any two of the patrons after being seated in the optimal arrangement.

\subsection*{Input}
The input file will begin with one line containing $n \leq 10^6$ and $p \leq 10^6$, the number of tables in your restaurant and the number of patrons to sit respectively. The following $n$ lines will each contain a single integer $l_i \leq 10^8$ describing the integer position of table $i$. These positions will be sorted in increasing order. All positions will be unique.

\subsection*{Output}
Output the largest possible value of the minimum distance between patrons after being seated in the optimal seating arrangement.

\vspace{0.25in}\hspace{-0.3in}\begin{tabular}{ll}

%\subsection*{Sample Input}
\parbox{3in}{{\large\bf Sample Input}

\vspace{0.15in}

{\tt
5 3\linebreak
1\linebreak
2\linebreak
4\linebreak
8\linebreak
9
}
}

&

\parbox{3in}{{\large\bf Sample Output}

\vspace{0.15in}

{\tt
3
}
}

\\
\end{tabular}

\end{document}


\documentclass{article}
\usepackage[utf8]{inputenc}

\title{MAT257 Notes}
\author{Jad Elkhaleq Ghalayini}
\date{October 2 2018}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{mathtools}
\usepackage{enumitem}
\usepackage{graphicx}
\usepackage{cancel}

\usepackage[margin=1in]{geometry}

\newtheorem{theorem}{Theorem}
\newtheorem{lemma}{Lemma}
\newtheorem{definition}{Definition}
\newtheorem*{corollary}{Corollary}
\newtheorem{exercise}{Exercise}

\newcommand{\reals}[0]{\mathbb{R}}
\newcommand{\nats}[0]{\mathbb{N}}
\newcommand{\ints}[0]{\mathbb{Z}}
\newcommand{\rationals}[0]{\mathbb{Q}}
\newcommand{\brac}[1]{\left(#1\right)}
\newcommand{\sbrac}[1]{\left[#1\right]}
\newcommand{\mc}[1]{\mathcal{#1}}
\newcommand{\eval}[3]{\left.#3\right|_{#1}^{#2}}
\newcommand{\ip}[2]{\left\langle#1,#2\right\rangle}
\newcommand{\prt}[2]{\frac{\partial #1}{\partial #2}}

\begin{document}

\maketitle

Examples:
\begin{enumerate}
  \item Let \[f(x, y) = \int_a^{x + y}g\] where \(g: \reals \to \reals\) is continuous. We compute \(Df(c, d)\) as follows:
  We have that \(f = q \circ s\), where
  \[q(t) = \int_a^tg, s(x, y) = x + y\]
  We have
  \[Df(c, d) =  g'(s(c, d))s'(c, d) = g(c + d)(c + d)\]
  since \(q'(t) = g(t)\).

  \item Let \[f(x, y) = \int_a^{x^y}g\] We have
  \[f = g \circ \xi, \xi(x, y) = x^y = e^{y\log x}\]
  Hence,
  \[D\xi(x, y) = x^yD(y\log x) = x^y((0, 1)\log x + (1/x, 0)y) = x^y(y/x, \log x)\]
  \[\implies Df(c, d) = q'(\xi(c, d))\xi'(c, d) = g(c^d)c^d(d/c, \log c)\]
\end{enumerate}

\section*{Higher-order Derivatives}
Let \(f: U \to \reals\) be a function where \(U \subseteq \reals^m\) and suppose
\[D_if = \prt{f}{x_i}: U \to \reals\]
exists for all \(i\). So we could now consider
\[D_j(D_if) = \prt{}{x_j}\left(\prt{f}{x_i}\right)\]
We'll write \(D_{ij}f\) to denote the above. Notice \(i\) is applied \textit{first}. We may also write \(f_{x_ix_j}\) and \(\frac{\partial^2 f}{\partial x_j \partial x_i}\). We have that
\[\frac{\partial^2 f}{\partial x_j \partial x_i}(a) = \frac{\partial^2 f}{\partial x_i \partial x_j}(a)\]
if both mixed partials exist and are continuous in a neighborhood of \(a\) (proof uses \(\int\)).

In general, we can consider taking higher order partials as well, as in
\[\frac{\partial^{\alpha_1 + ... + \alpha_n}f}{\partial x_1^{\alpha_1} ... \partial x_n^{\alpha_n}}\]
Of course we have to worry about the order, but the order is irrelevant if \(f\) is \(\mc{C}^\infty\), i.e. that all partial derivatives of all orders exist (and are hence continuous).

\section*{Multi-index notation}
In multi-index notation, \(\alpha = (\alpha_1,...,\alpha_m)\) is a vector of non-negative integers. We define the \textit{total order of \(\alpha\)}
\[|\alpha| = \alpha_1 + ... + \alpha_m\]
Furthermore, we define
\[x = (x_1,...,x_m) \implies x^\alpha = x_1^{\alpha_1}...x_m^{\alpha_m}\]
Once we look at Taylor's theorem, the notation
\[x! = x_1!...x_m!\]
will also come in handy.

We can now perform another example:
\begin{itemize}

  \item [3.] Let
  \[f(x, y) = \left\{\begin{array}{cc}
    xy\frac{x^2 - y^2}{x^2 + y^2} & (x, y) \neq (0, 0) \\
    0 & (x, y) = (0, 0)
  \end{array}\right.\]
  Is this function differentiable at the origin? Yes: \(f'(0, 0) = (0, 0)\). Let's check: we need to show that
  \[\lim_{(x, y) \to (0, 0)}\frac{f(x, y) - f(0, 0) - \cancel{(0, 0)\begin{pmatrix} x \\ y \end{pmatrix}}}{\sqrt{x^2 + y^2}} = \lim_{(x, y) \to (0, 0)}xy\frac{x^2 - y^2}{x^2 + y^2}\frac{1}{\sqrt{x^2 + y^2}} = 0\]
  We have that
  \[\left|xy\frac{x^2 - y^2}{x^2 + y^2}\frac{1}{\sqrt{x^2 + y^2}}\right| \leq \frac{|xy|}{\sqrt{x^2 + y^2}} \to 0\]

\end{itemize}

\end{document}