\section{The {\tt simplesine} Model}
\label{sec:simplesine-model}

In the following chapters,
we use a simple sine wave model, {\tt simplesine}, to illustrate \TrickHLA\ in action.
Understanding the model and how it is used in a Trick \sdefine file
is important for those chapters.
This section introduces \simplesine with that in mind.

% -----------------------------------------------------------------------
\subsection{Description}

The system modeled by \simplesine is an undamped harmonic oscillator,
the dynamics of which are governed by the differential equation
\begin{equation}
\ddot{x} + w^2 x = 0,
\label{eq:EOM}
\end{equation}
which has an analytic solution of the form
\begin{subequations}
\label{eq:analytic-equations}
\begin{align}
x(t)       &= A        \sin {(\omega t + \phi)}, \\
\dot{x}(t) &= A \omega \cos {(\omega t + \phi)}.
\end{align}
\end{subequations}

The relevant model data are
the dynamic {\em state}, $(x, \dot{x})$ and
the constant system {\em parameters}, $(A, \phi, \omega)$,
where the parameters are specified as inputs
and the state is calculated dynamically as simulation outputs.\footnote{
In this model, the initial state cannot be specified as inputs explicitly
but rather through the parameters $A$ and $\phi$.
}

The \simplesine model has functions that may be used to calculate the state
analytically based on equations~\ref{eq:analytic-equations}.
It can also propagate the state approximately
based on numerical integration of the differential equations.
The integration involves the calculation of the derivative of
the 2-vector $\boldsymbol{z}$ defined as

\begin{equation}
\label{eq:differential-equations}
  \boldsymbol{z}(t)
  \equiv \left\{
            \begin{array}{c}
              x(t)\\ \dot{x}(t)
            \end{array}
     \right\} \\
  = \left\{
            \begin{array}{r}
              A \sin{(\omega t + \phi)} \\ A \omega \cos{(\omega t + \phi)}
            \end{array}
     \right\}
\end{equation}

So that

\begin{equation}
\label{eq:derivative-equations}
  \dot{\boldsymbol{z}}(t)
  = \left\{
            \begin{array}{c}
              \dot{x}(t)\\ \ddot{x}(t)
            \end{array}
     \right\} \\
  = \left\{
            \begin{array}{r}
              \dot{x}(t)\\ - \omega^2 x(t)
            \end{array}
     \right\} \\
  = \left\{
            \begin{array}{r}
                    A w \cos{(\omega t + \phi)} \\
                    - A w^2 \omega \sin{(\omega t + \phi)}
            \end{array}
     \right\}
\end{equation}

% -------------------------------------
\subsection{Model}

The source code for the \simplesine model is organized into three directories:
{\tt data}, {\tt include} and {\tt src}.
The files in these directories are discussed below.

% ----------
\subsubsection{Include Files}

The \simplesine C/C++ include files are in directory {\tt simplesine/include}.
A list of the files is shown below.

{
\begin{center}
\scriptsize
\begin{tabular}{|l|l|}
\hline
{\em filename} \\
\hline
\hline
{\tt simplesine.h} \\
\hline
{\tt simplesine\_InteractionHandler.h} \\
\hline
{\tt simplesine\_LagCompensator.h} \\
\hline
{\tt simplesine\_Packing.h} \\
\hline
{\tt simplesine\_proto.h} \\
\hline
\end{tabular}
\end{center}
}

\paragraph{\tt simplesine.h}
This file declares the fundamental \simplesine data structures.
There is a single data structure that in turn holds state- and parameter-related
data structures.
In the simulations that follow, the {\tt simplesine\_T} structure will be
frequently declared as a {\tt sim\_object} in the \sdefine files
when the simulations need to model a sine wave.

The {\tt simplesine\_T} structure consists of a {\em state} substructure,
which holds $x$ and $\dot{x}$
and a parameters substructure which holds the constant {\em parameters,}
$A$, $\phi$ and $\omega$.
In this model, only the parameters
may be set from the input processor.
The state may only be calculated by a Trick job.
The purpose of this is to ensure that the state and parameters are never
initialized to inconsistent values.\footnote{
  Of course, this requires that developers remember to explicitly
  call {\tt simplesine\_calc()} as an initialization job for every
  \simplesine sim variable.
}

The complete file is shown in Appendix~\ref{sec:simplesine-h}.

\paragraph{\tt simplesine\_proto.h}
This file declares the C functions exported by the \simplesine model.
These functions may be used in an \sdefine file as Trick jobs.
Functions of particular interest are
\begin{itemize}
  \item{\tt simplesine\_calc()}, which calculates the state, $(x, \dot{x})$
    according to equations~\ref{eq:analytic-equations},\footnote{
      Since only the \simplesine parameters have default values,
      this job may be used as an initialization job to initialize
      the state from the parameters.
    }
  \item{\tt simplesine\_deriv()} and {\tt simplesine\_integ()}, which
    are used to numerically integrate equations~\ref{eq:differential-equations}
    using the standard Trick integration scheme,
  \item{\tt simplesine\_copyXXX()}, several routines which copy \simplesine
    data from one data structure to another, and
  \item{\tt simplesine\_calcError()}, which calculates the error between
one \simplesine state and the true values based on equations~\ref{eq:analytic-equations}.
\end{itemize}

The complete file is shown in Appendix~\ref{sec:simplesine-proto-h}.

\paragraph{\tt simplesine\_InteractionHandler.hh}
This file declares the \simplesine C++ class which acts as a
\TrickHLA\ {\em interaction handler}.
It is a subclass of {\tt TrickHLAInteractionHandler},
which is the \TrickHLA\ class which defines how interactions
are sent and received.

The complete file is shown in Appendix~\ref{sec:simplesine-InteractionHandler-hh}.

\paragraph{\tt simplesine\_LagCompensator.hh}
This file declares the \simplesine C++ class which acts as a
\TrickHLA\ {\em lag compensator}.
It is a subclass of {\tt TrickHLALagCompensator},
which is the \TrickHLA\ class which defines how federates may compensate
for HLA-time lags created as a result of sending data to remote federates
and transfering ownership between federates.

The complete file is shown in Appendix~\ref{sec:simplesine-LagCompensator-hh}.

\paragraph{\tt simplesine\_Packing.hh}
This file declares the \simplesine C++ class which may be optionally
used by developers to {\em pack} outbound data prior to sending via HLA
and {\em upack} is upon receipt from HLA.
It is a subclass of {\tt TrickHLAPacking},
which has {\tt pack()} and {\tt unpack()} virtual methods that implement
the application-specific packing and unpacking logic.

The complete file is shown in Appendix~\ref{sec:simplesine-Packing-hh}.

% ----------
\subsubsection{Source Files}

The \simplesine C/C++ source files are in the directory {\tt simplesine/src}.

The implementation of the \simplesine functions and classes is in
{\tt .c} and {\tt .cpp} files located in the {\tt simplesine/src} directory.
The C functions are mainly implemented one function per file\footnote{
  The copy functions
  ({\tt simplesine\_copyParams()},
  {\tt simplesine\_copyParams()}, and
  {\tt simplesine\_copyParams()})
  are located in a single file, {\tt simplesine\_copy.c}.
},
and the C++ classes are implemented one class per file.
The file names are shown in the table below:

{
\begin{center}
\scriptsize
\begin{tabular}{|l|l|}
\hline
{\em filename} & {\em implements what?} \\
\hline
\hline
{\tt simplesine\_calc.c} & {\tt simplesine\_copy()} \\
\hline
{\tt simplesine\_calcError.c} & {\tt simplesine\_calcError()} \\
\hline
{\tt simplesine\_compensate.c} & {\tt simplesine\_compensate()} \\
\hline
{\tt simplesine\_copy.c} & the {\tt simplesine\_copyXXX()} functions \\
\hline
{\tt simplesine\_deriv.c} & {\tt simplesine\_deriv()} \\
\hline
{\tt simplesine\_integ.c} & {\tt simplesine\_integ()} \\
\hline
{\tt simplesine\_propagate.c} & {\tt simplesine\_propagate()} \\
\hline
\hline
{\tt simplesine\_InteractionHandler.cpp} & the \TrickHLA\ interaction handler class \\
\hline
{\tt simplesine\_LagCompensator.cpp} & the \TrickHLA\ lag compensator class \\
\hline
{\tt simplesine\_Packing.cpp} & the \TrickHLA\ packing/unpacking class \\
\hline
\end{tabular}
\end{center}
}

% ----------
\subsubsection{Data Files}

This {\tt simplesine/data} directory
consists Trick input files for default \simplesine data.

Depending on how you build your \sdefine file, these files may be used
as ``fallback'' initializations for your data.
In the simulations that follow, the actual initial values
will often override the defaults in these files.\footnote{
  Sine the \simplesine state data are output-only,
  they cannot be set directly from the Trick input processor.
  Consequently, there is no default data file for
  {\tt simplesine\_state\_T}.
}

The files are shown below.

{
\begin{center}
\scriptsize
\begin{tabular}{|l|l|}
\hline
{\em filename} & {\em description} \\
\hline
\hline
{\tt integ.d}
  &
  Default numerical integration parameters.
  \\
\hline
{\tt simplesine.d}
  &
  Default \simplesine parameters with uninitialized state.
  \\
\hline
\hline
{\tt simplesine\_params.d}
  &
  Default \simplesine parameters.
  \\
\hline
\end{tabular}
\end{center}
}


\section{Predicate Logic \& Correctness}

\begin{itemize}

	\item Propositional operators

	\begin{itemize}

		\item not ($ \lnot $), and ($ \land $), or ($ \lor $)

		\item implication ($ \implies $) -- $ P \implies Q \equiv \lnot P \lor Q $

		\item equivalence ($ \iff $) -- $ P \iff Q \equiv (P \implies Q) \land (Q \implies P)$

	\end{itemize}

	\item Operator precedence (tightest-binding first): $ \lnot $, $ \land $, $ \lor $, $ \implies $

	\item Quantifiers

	\begin{itemize}

		\item for all ($ \forall $)

		\item there exists ($ \exists $)

		\item Example: $ \forall x \in \mathbb{N} \cdot x \ge 0 $

		\item The type of the bound variable may be implicit, e.g. $ \forall x \cdot x \ge 0 $

	\end{itemize}

	\item Entailment

	\begin{itemize}

		\item If $ P \implies Q $ is a tautology (always true) then $ P $ is stronger than $ Q $

		\item Equivalently: $ P $ entails $ Q $ ($ P \entails Q $)

	\end{itemize}

	\item Substitution

	\begin{itemize}

		\item $ P[x \backslash a] $ -- Substitute all occurrences of $ x $ by $ a $ in $ P $

		\item $ P[x, y \backslash a, b] $ -- Substitute $ a $ and $ b $ for $ x $ and $ y $ \textbf{simultaneously}

	\end{itemize}

	\item Hoare triples -- $ \hoare{P}{S}{Q} $

	\begin{itemize}

		\item $ P $ is the precondition, $ S $ is the program and $ Q $ is the postcondition

		\item If $ P $ is true before $ S $ executes, then $ S $ will terminate and $ Q $ will be true when it does

		\item The program must terminate if started in $ \text{States}_{_P} $ (total correctness)

	\end{itemize}

	\item Weakest preconditions

	\begin{itemize}

		\item For a program $ S $ and postcondition $ Q $, $ wp(S, Q) $ is the unique weakest possible precondition such that the triple $ \hoare{P}{S}{Q} $ will be true

		\item $ \forall P \cdot (\hoare{P}{S}{Q}) \implies (P \entails wp(S, Q)) $

	\end{itemize}

\end{itemize}


\subsection{Simplifications}

\begin{frame}
  \frametitle{Simplifications}

  \scriptsize

  We use {\bf simplification} rules to fight the two problems in previous slide,
  before bit-blasting everything
  \vfill
  \pause
  \begin{center}
  \scalebox{.4}{\input{simplification.pdf_t}}
  \end{center}
  \vfill
  \pause
  Simplifications exploit properties of \bitvectors to try to reduce
  the complexity of the formula. We see here some examples, but many
  more rules do exist. Also, it is very important the way they are
  combined together

\end{frame}

\begin{frame}
  \frametitle{Trivial Simplifications}

  The following are trivial consequences of the semantic of \bitvectors and
  \formulae in general
  \vfill
  \pause
  \bitvectors trivial simplifications
  \begin{itemize}
    \item $t = t\ \Rightarrow\ \top$\ \ \ \ for a generic term
    \item $c = d\ \Rightarrow\ \bot$\ \ \ \ for two different constants $c$ and $d$
    \item $t \band 0\ldots0\ \Rightarrow\ 0\ldots0$\ \ \ \ for a generic term
    \item \ldots
  \end{itemize}
  \vfill
  \pause
  \begin{itemize}
    \item $\varphi \wedge \varphi\ \Rightarrow\ \varphi$\ \ \ \ for a generic formula
    \item $\varphi \wedge \top\ \Rightarrow\ \varphi$\ \ \ \ for a generic formula
    \item $\varphi \vee \top\ \Rightarrow\ \top$\ \ \ \ for a generic formula
    \item \ldots
  \end{itemize}

\end{frame}

\begin{frame}
  \frametitle{Ground Term evaluation}

  If a term is {\bf ground}, i.e., it contains no variables, then it
  can be always simplified to a single constant
  \vfill
  \pause
  Examples:
  \begin{itemize}
    \item $0000 :: 1000\ \Rightarrow 00001000$
    \item $0010[1:0]\ \Rightarrow 10$
    \item $0100 + 0101\ \Rightarrow 1001$
  \end{itemize}

\end{frame}

\begin{frame}
  \frametitle{Variable Elimination Rule}

  Suppose that the input formula $\varphi$ is of the kind
  \vfill
  $$
  \varphi'\ \wedge\ (\w{x}{n} = \w{t}{n})
  $$
  \vfill
  where $\w{x}{n}$ is a variable, and $\w{t}{n}$ is a term {\bf not containing} $\w{x}{n}$
  \pause
  \vfill
  then we can rewrite $\varphi$ as
  $$
  \varphi'[\w{t}{n}/\w{x}{n}]
  $$
  \vfill
  i.e., we replace every occurrence of $\w{x}{n}$ by $\w{t}{n}$. \pause We save $n$
  Boolean variables in the reduction to SAT

\end{frame}

\begin{frame}
  \frametitle{Concatenation Elimination Rule}

  Suppose that we have the equality
  \vfill
  $$
  \w{t}{n} :: \w{s}{m} = \w{r}{n} :: \w{u}{m}
  $$
  \vfill
  then, because the concatenations {\bf match}, we can rewrite it as
  \pause
  \vfill
  $$
  (\w{t}{n} = \w{r}{n}) \wedge (\w{s}{m} = \w{u}{m})
  $$
  \vfill
  this rewriting may give more opportunity for applications of previous rules

\end{frame}

\begin{frame}
  \frametitle{Exercizes}

  \begin{enumerate}

    \item Complete the missing cases in procedures Bit-Blast-Term and Bit-Blast
    \vfill
    \item Bit-Blast the formula $\neg(\w{x}{3} = 000) \wedge (\w{x}{3} \band \w{y}{3}) = (\w{x}{3} + \w{y}{3})$
    \vfill
    \item Simplify the formula $(\w{x}{4} :: \w{y}{4}) = (\w{z}{4} :: \w{x}{4}) \wedge \neg( \w{y}{4} = \w{z}{4})$

  \end{enumerate}

\end{frame}


\subsection{Cubic polynomials}

Cubic polynomials are of the form \(ax^3+bx^2+cx+d=0\).

\subsection{Solving specific cases}

We start by solving when \(b=0\), that is:

\(aX^3+bx+c=0\)

\subsection{Solving the general case}


\section{Average Value of a Function}{}{}\label{sec:Average Value}
The average of some finite set of values is a familiar concept. If,
for example, the class scores on a quiz are 10, 9, 10, 8, 7, 5, 7, 6,
3, 2, 7, 8, then the average score is the sum of these numbers divided
by the size of the class:
$$
  \hbox{average score} = {10+ 9+ 10+ 8+ 7+ 5+ 7+ 6+
  3+ 2+ 7+ 8\over 12}={82\over 12}\approx 6.83.
$$
Suppose that between $t=0$ and $t=1$ the speed of an object is
$\sin(\pi t)$. What is the average speed of the object over that time?
The question sounds as if it must make sense, yet we can't merely add
up some number of speeds and divide, since the speed is changing
continuously over the time interval.

To make sense of ``average'' in this context, we fall back on the idea
of approximation. Consider the speed of the object at tenth of a
second intervals: $\sin 0$, $\sin(0.1\pi)$, $\sin(0.2\pi)$,
$\sin(0.3\pi)$,\dots, $\sin(0.9\pi)$. The average speed ``should'' be
fairly close to the average of these ten speeds:
$$
  {1\over 10}\sum_{i=0}^9 \sin(\pi i/10)\approx {1\over 10}6.3=0.63.
$$
Of course, if we compute more speeds at more times, the average of
these speeds should be closer to the ``real'' average. If we take the
average of $n$ speeds at evenly spaced times, we get:
$${1\over n}\sum_{i=0}^{n-1} \sin(\pi i/n).$$
Here the individual times are $\ds t_i=i/n$, so rewriting slightly we have
$${1\over n}\sum_{i=0}^{n-1} \sin(\pi t_i).$$
This is almost the sort of sum that we know turns into an integral;
what's apparently missing is $\Delta t$---but in fact, $\Delta t=1/n$,
the length of each subinterval. So rewriting again:
$$
  \sum_{i=0}^{n-1} \sin(\pi t_i){1\over n}=
  \sum_{i=0}^{n-1} \sin(\pi t_i)\Delta t.
$$
Now this has exactly the right form, so that in the limit we get
$$
  \hbox{average speed} = \int_0^1 \sin(\pi t)\,dt=
  \left.-{\cos(\pi t)\over\pi}\right|_0^1=
  -{\cos(\pi)\over \pi}+{\cos(0)\over\pi}={2\over\pi}\approx
  0.6366\approx 0.64.
$$

It's not entirely obvious from this one simple example how to compute
such an average in general. Let's look at a somewhat more complicated
case. Suppose that the velocity of an object is $\ds 16
t^2+5$ feet per second.
What is the average velocity between $t=1$ and $t=3$? Again we
set up an approximation to the average:
$${1\over n}\sum_{i=0}^{n-1} 16t_i^2+5,$$
where the values $\ds t_i$ are evenly spaced
times between 1 and 3. Once again we are ``missing'' $\Delta t$, and
this time $1/n$ is not the correct value. What is $\Delta t$ in
general? It is the length of a subinterval; in this case we take the
interval $[1,3]$ and divide it into $n$ subintervals, so each
has length $(3-1)/n=2/n=\Delta t$. Now with the usual ``multiply and
divide by the same thing'' trick we can rewrite the sum:
$$
  {1\over n}\sum_{i=0}^{n-1} 16t_i^2+5=
  {1\over 3-1}\sum_{i=0}^{n-1} (16t_i^2+5){3-1\over n}=
  {1\over 2}\sum_{i=0}^{n-1} (16t_i^2+5){2\over n}=
  {1\over 2}\sum_{i=0}^{n-1} (16t_i^2+5)\Delta t.
$$
In the limit this becomes
$${1\over 2}\int_1^3 16t^2+5\,dt={1\over 2}{446\over 3}={223\over 3}.$$
Does this seem reasonable? Let's picture it: In
Figure~\xrefn{fig:average speed} we see the velocity function together
with the horizontal line $y=223/3\approx 74.3$. Certainly
the height of the
horizontal line looks at least plausible for the average height of the
curve.

\figure[H]
%\texonly
\centerline{\vbox{\beginpicture
\normalgraphs
%\ninepoint
\setcoordinatesystem units <3truecm,0.4truemm>
\setplotarea x from 0 to 3.1, y from 0 to 150
\axis bottom ticks numbered from 0 to 3 by 1 /
\axis left ticks numbered from 0 to 150 by 25 /
\putrule from 1 74.3 to 3 74.3
\setquadratic
\plot 0.000 5.000 0.150 5.360 0.300 6.440 0.450 8.240 0.600 10.760
0.750 14.000 0.900 17.960 1.050 22.640 1.200 28.040 1.350 34.160
1.500 41.000 1.650 48.560 1.800 56.840 1.950 65.840 2.100 75.560
2.250 86.000 2.400 97.160 2.550 109.040 2.700 121.640 2.850 134.960
3.000 149.000 /
\setdashes
\putrule from 1 0 to 1 74.3
\putrule from 3 0 to 3 74.3
\endpicture}}
%\endtexonly
%\htmlfigure{Integration_applications-average_value.html}
\caption{\label{fig:average speed}
Average velocity.
}
\endfigure

Here's another way to interpret ``average'' that may make our
computation appear even more reasonable. The object of our example
goes a certain distance between $t=1$ and $t=3$. If instead the object
were to travel at the average speed over the same time, it should go
the same distance. At an average speed of $223/3$ feet per second for
two seconds the object would go $446/3$ feet. How far does it actually
go? We know how to compute this:
$$\int_1^3 v(t)\,dt = \int_1^3 16t^2+5\,dt={446\over 3}.$$
So now we see that another interpretation of the calculation is:
$${1\over 2}\int_1^3 16t^2+5\,dt={1\over 2}{446\over 3}={223\over 3}$$
which is the total distance traveled divided by the time in transit, namely,
the usual interpretation of average speed.

In the case of speed, or more properly velocity, we can always
interpret ``average'' as total (net) distance divided by time. However, in
the case of a different sort of quantity this interpretation does not
obviously apply, while the approximation approach always does. We might
interpret the same problem geometrically: What is the average height
of $16x^2+5$ on the interval $[1,3]$? We approximate this in exactly
the same way, by adding up many sample heights and dividing by the
number of samples. In the limit we get the same result:
$$
  \lim_{n\to\infty}{1\over n}\sum_{i=0}^{n-1} 16x_i^2+5=
  {1\over 2}\int_1^3 16x^2+5\,dx={1\over 2}{446\over 3}={223\over 3}.
$$
We can interpret this result in a slightly different way. The area
under $y=16x^2+5$ above $[1,3]$ is
$$\int_1^3 16t^2+5\,dt={446\over 3}.$$
The area under $y=223/3$ over the same interval $[1,3]$ is simply the
area of a rectangle that is 2 by $223/3$ with area $446/3$. So the
average height of a function is the height of the horizontal line that
produces the same area over the given interval.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Opensolutionfile{solutions}[ex]
\section*{Exercises for \ref{sec:Average Value}}

\begin{enumialphparenastyle}

%%%%%%%%%%
\begin{ex}
 Find the average height of $\cos x$ over the intervals
$[0,\pi/2]$, $[-\pi/2,\pi/2]$, and $[0,2\pi]$.
\begin{sol}
 $2/\pi$; $2/\pi$; $0$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 Find the average height of $\ds x^2$ over the interval
$[-2,2]$.
\begin{sol}
 $4/3$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 Find the average height of $\ds 1/x^2$ over the interval
$[1,A]$.
\begin{sol}
 $1/A$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 Find the average height of $\ds \sqrt{1-x^2}$ over the interval
$[-1,1]$.
\begin{sol}
 $\pi/4$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 An object moves with velocity $\ds v(t)=-t^2+1$ feet per second
between $t=0$ and $t=2$. Find the average velocity and the average
speed of the object between $t=0$ and $t=2$.
\begin{sol}
 $-1/3$, $1$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 The observation deck on the 102nd floor of the Empire State Building
is 1,224 feet above the ground. If a steel ball is dropped from the
observation deck its velocity at time $t$ is approximately $v(t)=-32t$
feet per second. Find the average speed between the time it is dropped
and the time it hits the ground, and find its speed when it hits the
ground.
\begin{sol}
 $\ds -4\sqrt{1224}$ ft/s; $\ds -8\sqrt{1224}$ ft/s
\end{sol}
\end{ex}

\end{enumialphparenastyle}


\subsection{Modelling homogeneous preferences with multinomial logit}

\subsection{Recap}

Our model is:

\(U_{ij}=\Theta z_{ij} + \epsilon_{ij}\)

\(U_{ij}=\alpha_i x_j -\beta_i p_{ij} + \theta_j d_i +\epsilon_{ij}\)

\subsection{Homogeneous model}

We model all customers as having the same preferences.

\(U_{ij}=\alpha x_j -\beta p_{ij} + \theta_j d_i +\epsilon_{ij}\)

\subsection{The multinomial logit assumption}

If errors are IID and extreme we get:

\(P_{ij}=\dfrac{e^{\Theta z_j}}{\sum_k e^{\Theta z_k }}\)

\subsection{The outside option}

A user has the option of not buying anything.

\(U_0=0\)

This gives us the following shares:

\(P_{ij}=\dfrac{e^{\Theta z_j}}{e^0+\sum_{k=1} e^{\Theta z_k }}\)

\(P_{ij}=\dfrac{e^{\Theta z_j}}{1+\sum_{k=1} e^{\Theta z_k }}\)

\subsection{Own-price elasticity of demand}

\(P_{ij}=\dfrac{e^{\Theta z_j}}{1+\sum_{k=1}} e^{\Theta z_k }\)

\(P_{ij}=\dfrac{e^{\alpha x_j -\beta p_j + \theta_j d_i}}{1+\sum_{k=1} e^{ \alpha x_k -\beta p_k +\theta_k d_i}}\)

\(\dfrac{\delta P_{ij} }{\delta p_j}\dfrac{p_j}{P_{ij}}=-\beta p_j(1-P_{ij})\)

\(\dfrac{\delta P_{ij} }{\delta p_k}\dfrac{p_j}{P_{ij}}=\beta p_kP_{ij}\)

This means that the lower the price, the lower the own price elasticity of demand.

This means that mark ups are higher for cheaper goods, which doesn't always match reality.

This can be adjusted by changing the form. For example we could use \(\ln p\) or \(p^2\).

However, we are still getting the shape by assumption.

\subsection{Cross-price elasticity of demand}

\subsection{Getting aggregate market shares}

\(s_j = \dfrac{1}{n}\sum_i P_{ij}\)


\documentclass[letter]{article}
\renewcommand{\baselinestretch}{1.25}

\usepackage[margin=1in]{geometry}
\usepackage{physics}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{pythonhighlight}
\usepackage{hyperref}

\allowdisplaybreaks

%opening
\title{MECH 6327 - Homework 2}
\author{Jonas Wagner}
\date{2021, March 3}

\begin{document}

\maketitle

\newpage
\textbf{(Probably not necessary... but its long)}
\tableofcontents

\newpage
\section{Problem Set 1: Convex Sets}

\subsection{Problem 2.5}
\textbf{Problem:}\\
What is the distance between two parallel hyperplanes: $\{x \in \real^n | a^T x = b_1\}$ and $\{x \in \real^n | a^T x = b_2\}$?\\

\noindent
\textbf{Solution:}\\
Under the assumption that $a\in \real^n$ and $b_1,b_2 \in \real$, the quantity $a^T x_0$ represents the component of $x_0$ in the normal direction. Similarly, the quantities $b_1$ and $b_2$ represent the euclidean distance of the hyperplane from the origin (in the normal direction). Since the hyperplanes are parrellel, the distance between them is the difference between their offsets:
\begin{equation}
	\text{Distance between hyperplanes: } b_1 - b_2
\end{equation}

\subsection{Problem 2.7}
\textbf{Problem:}\\
\textit{Voronoi description of halfspace.} Let $a$ and $b$ be distinct points in $\real^n$. Show that the set of all points that are closer to $a$ than $b$ via the euclidean norm is a halfspace. Describe it explicitly as an inequality and draw a picture.

\noindent
\textbf{Solution:}\\
The set of all points closer to $a$ then $b$ can be defined as:
\begin{equation}
	\{x \in \real^n \ | \ \norm{x-a}_2 \leq \norm{x-b}_2\}
\end{equation}

The boundary defining this halfspace will be a plane defined by the normal vector $c$ representing the distance between $a$ and $b$, and the offset coefficient $d$ describing intersection of the plane through the half-way point between $a$ and $b$. The quantities $c$ and $d$ can therefore be defined by:
\begin{equation}
	\begin{aligned}
		c &= b - a\\
		d &= \frac{c^T a + c^T b}{2}\\
		  &= \frac{1}{2} c^T (a+b)
	\end{aligned}
\end{equation}

The halfspace, that is equivalent to $x$, can be described by the following:
\begin{equation}
	\{x\in \real^n \ | \ c^T x \leq d\}
\end{equation}

This can be visualized in two dimensions for $a = \mqty[2\\4]$ and $b = \mqty[-3\\7]$. The boundary (the red line) is calculated in the standard form using $$x_2 = \frac{-1}{c_2} \qty(c_1 * x_1 - d)$$ and then plotted. The half-space itself is the region below the boundary.

\begin{figure}[h]
	\centering
	\includegraphics[width = 0.7\linewidth]{fig/pblm2_7}
	\caption{Visualization of the boundary for the halfspace.}
	\label{fig:pblm2_7}
\end{figure}

%\newpage
\subsection{Problem 2.12}
\textbf{Problem:}\\
Which of the following sets are convex?\\

\noindent
\textbf{Solution:}
\subsubsection{(a) - Slab}
A slab defined as $$\{x \in \real^n \ | \ \alpha \leq a^T x \leq \beta\}$$ \textbf{is convex} as it consists of the intersection of two halfspaces which themselves are complex.

\subsubsection{(b) - Rectangle/Hyperrectangle}
A rectangle set defined as $$\{x \in \real^n \ | \ \alpha_i \leq x_i \leq \beta_i, \ i = 1,\dots,n\}$$ \textbf{is convex} as it is composed of the intersections of half spaces which are themselves convex. This is similar to the polyhedrons/polytopes that by definition are also convex.

\subsubsection{(c) - Wedge}
A wedge set given as $$\{x \in \real^n \ | \ a_1^T x \leq b_1, a_2^T x \leq b_2\}$$ \textbf{is convex} as it is just an intersection of two halfspaces (a polyhedron).

\subsubsection{(d) - Closer to a point then a set}
A set of points closer to a given point than a given set is defined as $$\{x \| \norm{x-x_0}_2 \leq \norm{x - y}_2 \ \forall y \in S\}$$ where $S\subset\real^n$ \textbf{is not convex} in general. This is because there is not enough information about $y$ for a conclusion to be made whether it is convex or not. A counter example would be if $y$ is a point in orbit around a convex shape $S$ that would end up generating a concave $x$.

\subsubsection{(e) - Closer to a set then another set}
A set of points closer to a given set than another given set is defined as $$\{x \ | \ \textbf{dist}(x,S) \leq \textbf{dist}(x,T)\}$$ where $S, T \subset\real^n$, and $$\textbf{dist}(x,S) = \inf\{\norm{x-z}_2 \ | \ z \in S\}$$ \textbf{is not convex} in general. This is because there is not enough information about $S$ and $T$ for a conclusion to be made whether it is convex or not. A counter example includes if $S$ or $T$ themselves are a concave shave that causes the set $x$ to also be concave and therefore not convex.

\subsubsection{(f) - Set of the sum being within a convex set}
The set defined as $$\{x \ | \ x + S_2 \subseteq S_1\}$$ with $S_1$ being convex \textbf{is not complex} in general. This is because we do not know enough information about $S_2$ to conclude that $x$ is conncex purely due to the relationship with the convex set $S_1$.\\

\subsubsection{(g) - Set with weighted distances to two points}
The set of all points that is closer to $a$ then $b$ by at least a factor of $\theta$, defined as $$\{x \in \real^n \ | \ \norm{x-a}_2 \leq \theta \norm{x-b}_2\}$$ with $a \neq b$ and $0 \leq \theta \leq 1$ \textbf{is convex.} This is know because, as proven in a previous problem, a hyperplane is formed for a similarity stated problem which itself is convex. When the distance to $a$ must be less then a portion of the distance to $b$ it will cause the psudo-hyperplane to curve inwards and untimely remain convex.

\newpage
\subsection{Problem 2.28}
\textbf{Problem:}\\
Define the positive semi-definite cone ($S_+^n$) for $n = 1, 2, 3$ in terms of ordinary inequalities with the matrix coefficients themselves.

\noindent
\textbf{Solution:}\\
The positive semi-definite cone is defined for size $n$ as the set of all symmetric matrices that are positive semi-definite:
\begin{equation}
	S_+^n \equiv \{x \in S^n \ | \ x \succeq 0\}
\end{equation}
One method to ensure that a matrix is positive semi-definite is to ensure that its leading principle minors are all non-negative (strictly positive for positive definite).\\

For $n=1$ the required inequalities are simple,
\begin{equation}
	X = \mqty[x_1] \in S_+^1 \iff x_1 \geq 0
\end{equation}

For $n=2$ the inequalities can be found by ensuring the leading principle minors are all non negative:
\begin{equation}
	\begin{aligned}
		m_1 &= \det[x_1]\\
		&= x_1\\
		m_2 &= \det \mqty[x_1 & x_2 \\ x_2 & x_3] \\
		&= x_1 x_3 - x_2^2
	\end{aligned}
\end{equation}
These definitions of the minors can be then be used to construct inequalities such that all the minors are positive:
\begin{equation}
	X = \mqty[x_1 & x_2 \\ x_2 & x_3] \in S_+^2 \ \iff \ \mqty{x_1 \geq 0\\  x_1 x_3 \geq x_2^2}
\end{equation}

For $n=3$ the inequalities can be found by ensuring the leading principle minors are all non negative:
\begin{equation}
	\begin{aligned}
		m_1 &= \det[x_1]\\
		&= x_1\\
		m_2 &= \det \mqty[x_1 & x_2 \\ x_2 & x_4] \\
		&= x_1 x_4 - x_2^2\\
		m_3 &= \det \mqty[x_1 & x_2 & x_3\\ x_2 & x_4 & x_5\\ x_3 & x_5 & x_6]\\
		&= x_1 (x_1 x_4 - x_2^2) - x_2 (x_2 x_6 - x_3 x_5) + x_3 (x_2 x_5 - x_3 x_4)\\
		&= x_1^2 x_4 - x_1 x_2^2 - x_2^2 x_6 + x_2 x_3 x_5 + x_2 x_3 x_5 - x_3^2 x_4
	\end{aligned}
\end{equation}
These definitions of the minors can be then be used to construct inequalities such that all the minors are positive:
\begin{equation}
	X = \mqty[x_1 & x_2 & x_3\\ x_2 & x_4 & x_5\\ x_3 & x_5 & x_6] \in S_+^2 \ \iff \
	\mqty{x_1 \geq 0\\
			x_1 x_4 \geq x_2^2\\
		 x_1 x_2^2 + x_2^2 x_6 + x_3^2 x_4 \geq x_1^2 x_4 + 2 x_2 x_3 x_5}
\end{equation}

\subsection{Problem 2.33}
The monotone non-negative cone is defined as all the nonnegative vectors with components sorted in non-increasing order:
\begin{equation}
	K_{m+} = \{x \in \real^n \ | \ x_1 \geq x_2 \geq \cdots \geq x_n \geq n\}
\end{equation}

\subsubsection{Part a}
\textbf{Problem:}\\
Show that $K_{m+}$ is a proper cone.

\noindent
\textbf{Solution:}\\
A set, $C \subseteq \real^n$, is considered a cone if
\begin{equation}
	\theta x \in C \ \forall x \in C \text{ and } \theta \geq 0
\end{equation}

It can be easily seen that the set $K_{m+}$ satisfies this condition as scaling each element of the matrix $x \in K_{m+}$ will equally be scaled by the same amount and the conditions of non-increasing order will still apply. This guarantees that $K_{m+}$ is in fact a cone.\\
To ensure convexity, the definition of convexity and of a cone can be incorporated into the following test:\\
The set $C \subseteq \real^n$ is a convex cone iff
\begin{equation}
	\theta_1 x_1 + \theta_2 x_2 \in C \ \forall x_1,x_2 \in C, \theta_1, \theta_2 \geq 0
\end{equation}
It is also clear that $K_{m+}$ will satisfy as if each element element of one of the matrices is scaled it maintains the nonincreasing order. The same is true for summing two $K_{m+}$ matrices as the nonincreasing order will be maintained.\\
It is also clear that the cone is closed because the complimentary set $K_{m+}'$ is clearly open.\\
Similarily, $K_{m+}$ is solid because its definition includes all all of the subspace above the cone's boundary.\\
The cone is also known to be pointed because the definition defines that all of the vectors contained within the set are nonnegative. It is therefore only ever possible for elements to be in the positive sector (quadrant/octant/etc.). This means the cone cannot contain a line.\\
We can finish stating that $K_{m+}$ is a proper cone because it is a cone, convex, closed, solid, and pointed.

\newpage
\subsubsection{Part b}
\textbf{Problem:}\\
Find the dual cone, $K_{m+}^*$.

\noindent
\textbf{Solution:}\\
A dual cone for $K$ is defined as:
\begin{equation}
	K^* = \{y \in \real^n \ | \ x^ Ty \geq 0 \ \forall x \in K\}
\end{equation}
The left side of the inequality that defines the dual cone can also be written in summation form as: $$x^T y = \sum_{i=1}^{n} x_i y_i \geq 0$$
It is known that the following is equivalent:
\begin{equation}
	\begin{aligned}
		\sum_{i=1}^{n} x_i y_i &= (x_1 - x_2) y_1 + (x_2 - x_3) (y_1 + y_2) + \cdots \\
						&= + (x_{n-1} - x_n) (y_1 + \cdots + y_{n-1}) + x_n (y_1 + \cdots + y_n)
	\end{aligned}
\end{equation}
By definition, $x_1 \geq x_2 \geq \cdots \geq x_n \geq n$, thus it can be said that each $(x_i - x_j)$ term in the expanded form is positive. In order for the inequality to always hold true, each $(y_1 + \dots + y_i)$, must be positive as well. This can be achieved by defining that the elements of y are never decreasing.\\
Thus, the dual cone of $K_{k+}$ can be defined as:
\begin{equation}
	K_{m+}^{*} = K_{m-} = \{x \in \real_{+}^n \ | \ x_1 \leq x_2 \leq \dots \leq x_n\}
\end{equation}

\newpage
\section{Problem Set 2: Convex Functions}

\subsection{Problem 3.6}
(Note to self... remember to remove this time: Proofs don't really seem required... pretty simple question)\\
\textbf{Problem:}\\
What is the epigraph for the following convex functions?\\

\noindent
\textbf{Solution:}\\
The epigraph is defined as
\begin{equation}
	\textbf{epi} f = \{(x,t) \ | \ x \in \textbf{dom} f, f(x) \leq t\}
\end{equation}
where $\textbf{epi} f \subset R^{n+1}$. This is equivalent to saying the space 'above' the function.

\subsubsection{(a) - Epigraph of a halfspace}
The epigraph of a halfspace in $n$ dimensions is a halfspace in $n+1$ dimensions.

\subsubsection{(b) - Epigraph of a convex cone}
The epigraph of a convex cone is another convex cone of in a higher dimension. (It might be possible to generalize it to a triangular slab type thing)

\subsubsection{(c) - Epigraph of a polyhedron}
The epigraph of a polyhedron is another polyhedron.

\newpage
\subsection{Problem 3.16}
\textbf{Problem:}\\
Determine if the following functions are convex, concave, quasiconvex, or quasiconcave.

\noindent
\textbf{Solution:}\\
\subsubsection{(a) - $e^x - 1$}
Let $$f(x) = e^x - 1$$ on $\real$.\\
$f(x)$ \textbf{is convex} and can be proven in multiple ways. For instance, visualizing the epigraph of the function, it is clear that a convex set is produced. Additionally, $f(x)$ can be constructed by putting the convex function $e^x$ through the convex affine function $x - 1$.

\subsubsection{(b) - $x_1 x_2$}
Let $$f(x_1,x_2) = x_1 x_2$$ over the domain $\real_{++}^2$.\\
This function \textbf{is convex} and can be demonstrated using the definition:
\begin{align}
	f(\theta x + (1-\theta)y) &= \theta f(x) + (1-\theta) f(y)\\
	 (\theta x_1 + (1-\theta) y_1) (\theta x_2 + (1-\theta) y_2)&= \theta x_1 x_2 + (1- \theta) y_1 y_2\nonumber\\
	\theta^2 x_1 x_2 + \theta(1-\theta)y_1 x_2 + \theta (1-\theta) x_1 y_2 + (1-\theta)^2 y_1 y_2 &= \theta x_1 x_2 + (1- \theta) y_1 y_2\nonumber\\
	\theta^2 x_1 x_2 + (\theta-\theta^2) (x_1 y_2 + x_2 y_1) + (1- 2 \theta + \theta^2) y_1 y_2 &= \theta x_1 x_2 + (1- \theta) y_1 y_2
\end{align}
If we analyze each set of terms it not explicitly clear by the result, but within the domain $\real_{++}^2$ the equality holds true.

\subsubsection{(c) - $1/(x_1 x_2)$}
Let $$f(x_1,x_2) = 1/(x_1 x_2)$$ be defined over the domain $R_{++}^2$.\\

This function \textbf{is convex}.

\subsubsection{(d) - $x_1/x_2$}
Let $$f(x_1,x_2) = x_1/x_2$$ be defined over the domain $R_{++}^2$.\\
This function \textbf{is convex}.

\subsection{(e) - $x_1^2 / x_2$}
Let $$f(x_1,x_2) = x_1^2 / x_2$$ be defined over the domain $R \cross R_{+}$.
This function \textbf{is convex}.

\subsection{(f) - $x_1^\alpha x_2^{1-\alpha}$}
Let $$f(x_1,x_2) = x_1^\alpha x_2^{1-\alpha}$$ with $0 \leq \alpha 1$ be defined over the domain $\real_{++}^2$.\\
This function \textbf{is not convex}.

\newpage
\subsection{Problem 3.18a}
\textbf{Problem:}\\
Using the proof of concavity of the log-determinant function to show that $$f(X) = \trace(X^{-1})$$ is convex over the domain $S_{++}^n$.\\

\noindent
\textbf{Solution:}\\
A proof of the concavity for the $$f(X) = \log \det X$$ is given as follows:\\
Given an arbritrary line, $X = Z + t V$, with $Z, V \in S^n$, the function $$g(t) = f(Z + t V)$$ can be defined within $\{x \ | \ Z + t V \succ 0\}$. It can then be assumed (without loss of generalizty) that $t = 0$ is in the interval (so that $Z \succ 0$ is defined). This then allows the following:
\begin{align}
	g(t) &= \log \det(Z+tV)\\
		 &= \log \det(Z^{1/2} (I + t Z^{-1/2}V Z^{-1/2})Z^{1/2})\\
		 &= \sum_{i=1}^n \log(1 + t \lambda_i) + \log \det Z
\end{align}
with $\lambda_1, \dots, \lambda_n$ being the eigenvalues of $Z^{-1/2} V Z^{-1/2}$.\\
This allows for the first and second derivatives to be computed as:
\begin{align}
	g'(t) &= \sum_{i=1}^n \cfrac{\lambda_i}{1 + t \lambda_i}\\
	g''(t) &= -\sum_{i=1}^n \cfrac{\lambda_i^2}{(1 + t \lambda_i)^2}
\end{align}
Since $g''(t) \leq 0$, it can be concluded that $f$ is concave.\\

This proof can then be applied to the trace(X{-1}) is also concave.\\
First, Let
\begin{equation}
	f(X) = \trace(X^{-1})
\end{equation}
be defined on the domain $\mathbf{S}_{++}^n$.\\
An arbritrary line can then be defined as
\begin{equation}
	g(t) = f(Z + tV)
\end{equation}
over the domain $\{x \ | \ Z + t V \succ 0\}$.
It can then be assumed (without loss of generality) that $t = 0$ is in the interval (so that $Z \succ 0$ is defined). This then allows the following:
\begin{align}
	g(t) &= \trace\qty(\qty(Z+tV)^{-1})\\
	&= \trace\qty(\qty(Z^{1/2} (I + t Z^{-1/2}V Z^{-1/2})Z^{1/2})^{-1})\\
	\intertext{from the fact that $(ABC)^{-1} = C^{-1}B^{-1}A^{-1}$, this can be manipulated to be}
	&= \trace\qty(Z^{-1/2} \qty(I + t Z^{-1/2}V Z^{-1/2})^{-1}Z^{-1/2})\\
	\intertext{since $\trace(ABC) = \trace(CAB)$,}
	&= \trace\qty(Z^{-1/2} Z^{-1/2} \qty(I + t Z^{-1/2} V Z^{-1/2})^{-1})\\
	\intertext{by taking the eigenfactor decomposition of $Z^{-1/2} V Z^{-1/2} = Q \Lambda Q^{-1}$, this can be rewritten as:}
	&= \trace\qty(Z^{-1} \qty(I + t Q \Lambda Q^{-1})^{-1})\\
	&= \trace\qty(Z^{-1} \qty(Q \qty( I + t \Lambda )Q^{-1})^{-1})\\
	&= \trace\qty(Z^{-1} Q \qty( I + t \Lambda)^{-1}Q^{-1})\\
	\intertext{since $\trace(ABC) = \trace(CAB)$,}
	&= \trace\qty(Q Z^{-1} Q^{-1}\qty( I + t \Lambda)^{-1})\\
	\intertext{By the definition of the trace, we can rewite this as}
	&= \sum_{i=1}^n \qty(Q^T Z^{-1} Q)_{ii} \qty(1 + t \lambda_i)^{-1}
\end{align}
This allows for the first and second derivatives to be computed as:
\begin{align}
	g'(t) &= -\sum_{i=1}^n \qty(Q^T Z^{-1} Q)_{ii} \cfrac{\lambda_i}{\qty(1 + t \lambda_i)^{-2}}\\
	g''(t) &= 2\sum_{i=1}^n \qty(Q^T Z^{-1} Q)_{ii} \cfrac{\lambda_i^2}{\qty(1 + t \lambda_i)^{-3}}
\end{align}
Since $g''(t) \leq 0$, it can be concluded that $f$ is concave.\\

\newpage
\subsection{Problem 3.22}
\textbf{Problem:}\\
Use various composition rules to show that the following functions are convex.

\noindent
\textbf{Solution:}\\
\subsubsection{(a) - double log functions}
Let $$f(x) = -\log(-\log(\sum_{i=1}^m e^{a_i^T x + b_i}))$$ be defined over the domain $\{x \ | \ \sum_{i=1}^m e^{a_i^T x + b_i} < 1\}$.\\
It is known that $$\log(\sum_{i=1}^{n} e^{y_i})$$ is convex. Since all compositions of convex functions with affine functions are also convex, it can be said that $$\sum_{i=1}^m e^{a_i^T x + b_i}$$ is also convex.\\
Additionally, it is known that the $\log()$ function is concave, but when the sign changes it becomes convex, thus the composition of $-g(-g(h(x)))$ is convex for the concave function $g(x) = \log(x)$.\\
Therefore, the function $f(x)$ is convex.

\subsubsection{(b) - square root of some product sum}
Let $$f(x,u,v) = - \sqrt{uv - x^T x}$$ be defined over the domain $\{(x,u,v) \ | \ uv > x^T x, u, v > 0\}$.\\
It is known that $g(x) = x^x / u$ is convex for $u>0$ and that $h(x)-\sqrt{x_1 x_2}$ is convex on $\real_{++}^2$.\\
The function $f(x,u,v)$ can be manipulated as follows:
\begin{align*}
	f(x,u,v) 	&= - \sqrt{uv - x^T x}\\
				&= - \sqrt{u \qty(v -\cfrac{x^T x}{u})}
\end{align*}
From what we know about the underling functions, it can be said that the convex function $g(x)$ is summed with $v$ (a convex combination) and then used as an input to the convex function $h(x)$, resulting in an overall convex function.

\newpage
\subsubsection{(c) - log of some product sum}
Let $$f(x,u,v) = - \log(uv - x^T x)$$ be defined over the domain $\{(x,u,v) \ | \ uv > x^T x, u, v > 0\}$.\\
It is known that $g(x) = x^x / u$ is convex for $u>0$ as well as that the $h(x) = \log(x)$ function is concave.\\
Performing the same manipulation as in the previous problem, $f(x,u,v)$ can be written as: $$f(x,u,v) = -\log(u \qty(v - \cfrac{x^Tx}{u}))$$
From this it can be derived that the convex function $g(x)$ is summed with $v$ (a convex combination) and then used as an input to the concave function $h(x)$ but is then negated to result in an overall convex function.

\subsubsection{(d) - complicated root of a powered sum and norm}
Let $$ f(x,t) = -\qty(t^p - \norm{x}_p^p)^{1/p}$$ be defined with $p>1$ over the domain $\{(x,t) \ | \ t \geq \norm{x}_p\}$.\\
It is known that $g(x,u) = \norm{x}_p^p / u^{p-1}$ is convex for $u >0$ and that $h(x,y) = -x^{1/p} y^{1 - 1/p}$ is convex over $\real_{++}^2$.\\
$f(x,t)$ can be manipulated as follows:
\begin{align}
	f(x,t)	&= -\qty(t^p - \norm{x}_p^p)^{1/p}\\
			&= -\qty(t^{p-1}\qty(t - \cfrac{\norm{x}_p^p}{t^{p-1}}))^{1/p}\\
			&= -t^{1 - 1/p}\qty(t - \cfrac{\norm{x}_p^p}{t^{p-1}})^{1/p}\\
\end{align}
From this it is clear that the known convex function $g(x,t)$ is put through a weighted sum (which over the domain is never negative) and then is composed with the known convex function $h(x,t)$. This means that the original function $f(x,t)$ is a convex function.

\newpage
\subsubsection{(e) - complicated log of a powered sum and norm}
Let $$ f(x,t) = - \log(t^p - \norm{x}_p^p)$$ with $p >1$ be defined over the domain $\{(x,t) \ | \ t \geq \norm{x}_p\}$.\\
It is known that $g(x,u) = \norm{x}_p^p / u^{p-1}$ is convex for $u > 0$ and that (with the negative sign) the function $h(x) = - log(x)$ is convex.
$f(x,t)$ can be manipulated as follows:
\begin{align}
	f(x,t)	&= -\log(t^p - \norm{x}_p^p)\\
			&= -\log\qty(t^{p-1}\qty(t - \cfrac{\norm{x}_p^p}{t^{p-1}}))\\
			&= -\log\qty(t^{p-1}) - \log\qty(t - \cfrac{\norm{x}_p^p}{t^{p-1}})
\end{align}
From this it is clear that the known convex function $g(x,t)$ is put through a weighted sum (which over the domain is never negative) and then is composed with the known convex function $h(x,t)$. This means that the original function $f(x,t)$ is a convex function.

\end{document}


%!TEX root = TTK4150-Summary.tex
\section{Feedback linearization}
Consider a class of nonlinear systems of the form

\begin{equation}\label{eq:nonlinear-system}
	\begin{split}
		\dot{x} &= f(x) + G(x)u             \\
		y       &= h(x)                     \\
		u       &= \alpha (x) + \beta (x) v \\
		z       &= T(x)
	\end{split}
\end{equation}
where $u$ is a state feedback conboller, and $T$ is a change of variables.

To be able to cancel nonlinearities with feedback the input and non-linearities must appear together as a sum $\lambda (x) + u$ or as a product $\lambda (x) u$, where the matrix $\lambda(x)$ is non-singular in the domain of interest, and $u = \beta(x) v, \beta(x) = \lambda^{-1}$.

\paragraph{Definition 13.1}
A nonlinear system as \eqref{eq:nonlinear-system} where $f:D \to R^n$ and $G : D \to R^{n \times p}$ are sufficiently smooth on a domain $D \subseteq R^n$, is said to be feedback linearizable  (or input-state linearizable) if there exists a diffeomorphism $T:D \to R^n$ such that $D_z = T(D)$ contains the origin and the change of variables $z = T(x)$ transforms \eqref{eq:nonlinear-system} into the form
\begin{equation}
	\dot{z} = Az + B\lambda (x) [u - \alpha(x)]
\end{equation}
with $(A,B)$ controllable and $\lambda(x)$ nonsingular $\forall x \in D$.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Input-output linearization}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Consider \eqref{eq:nonlinear-system} which satisfies Def. 13.1. The derivative $\dot{y}$ is given by
\begin{equation}
	\dot{y} = \pd{h}{x} \del{f(x) + g(x)} \triangleq L_f h(x) + L_g h(x) u
\end{equation}
where $L_f h(x) \triangleq \pd{h}{x} f(x) $ is the \emph{Lie Derivative} of $h$ w.r.t. $f$.

\paragraph{Relative degree}
The relative degree is the number of times $y$ must be differentiated until $u \in D_0 \subseteq D$ appears. A system must have a well defined relative degree to be input-output linearizable. (It must also be minimum phase.)

\paragraph{Diffeomorphism}
Wikipedia: \emph{In mathematics, a diffeomorphism is an isomorphism of smooth manifolds. It is an invertible function that maps one differentiable manifold to another such that both the function and its inverse are smooth.}

\paragraph{Theorem 13.1}
Consider \eqref{eq:nonlinear-system} with relative degree $\rho \leq n$ in D. If $\rho = n$, then for every $x_0 \in D$, a neighborhood N of $x_0$ exists such that the map
\begin{equation}
	T(x) =
	\left[
	\begin{array}{ccc}
		h(x)     \\
		L_f h(x) \\
		\vdots   \\
		L_f^{n-1} h(x)
	\end{array}
	\right]
\end{equation}
restricted to N, is a diffeomorphism on N. If $\rho < n$, then, for every $x_0 \in D$, a neighborhood N of $x_0$ and smooth function $\phi_1 (x), \dots , \phi_{n-\rho} (x)$ exist such that
\begin{equation}
	\pd{\phi_i}{x} g(x) = 0, \mbox{ for } 1 \leq i \leq n-\rho, \forall x \in D_0
\end{equation}
is satisfied $\forall x \in N$ and the map
\begin{equation}
	z = T(x) =
	\left[
	\begin{array}{ccc}
		\phi_1(x)         \\
		\vdots            \\
		\phi_{n-\rho}(x)  \\
		---               \\
		h(x)              \\
		\vdots            \\
		L_f^{\rho-1} h(x)
	\end{array}
	\right]
	\triangleq
	\left[
	\begin{array}{ccc}
		\phi(x) \\
		---     \\
		\psi(x)
	\end{array}
	\right]
	\triangleq
	\left[
	\begin{array}{ccc}
		\eta \\
		---  \\
		\xi
	\end{array}
	\right]
\end{equation}
restricted to N, is a diffeomorphism on N.

\paragraph{Method}
\begin{enumerate}
	\item Set system on following form $\dot{x} = f(x) + g(x)u$
	\item Find the relative degree $\rho$, ($\rho = n \Rightarrow$ no internal dynamics)
	\item Write the system in normal form (external and internal dynamics)
	\item Choose $u$ to cancel the nonlinearities
	\item Analyze the zero-dynamics
	\item Choose $v$ to solve the control problem
\end{enumerate}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Full-state linearization}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{State feedback control}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

TTK4150 Nonlinear control systems/sec-feedback-linearization.tex


% !TEX root = ./Basilisk-avsLibrary20170812.tex

\section{User Guide}

\subsection{{\tt linearAlgebra} Library}
The linear algebra library provides numerous C-based functions to perform basic matrix math operations.  For a complete list of functions supported, consult the {\tt linearAlgebra.h} file.

\subsubsection{Vector Operations}
The vector related functions all begin with a letter '{\tt v}' and are broken down in the following categories depending on if the linear algebra operation is performed on a matrix of a general size, or of a specific size of 2, 3, 4 or 6.
\begin{itemize}
	\item if the function is  {\tt vXXXX()} the code works on a $n$x1 matrix of arbitrary length $n$.
	\item if the function is {\tt v2XXXX()}  the code works on a matrix of length 2 defined as {\tt double vec[2]}.
	\item if the function is {\tt v3XXXX()}  the code works on a matrix of length 2 defined as {\tt double vec[3]}.
	\item if the function is {\tt v4XXXX()} the code works on a matrix of length 2 defined as {\tt double vec[4]}.
\end{itemize}

The most extensive set of linear algebra support functions are for operation on 3 and 4 dimensional matrices common with orbital mechanics (position and velocity vector representations) and attitude dynamics (working with 3- and 4-parameter attitude descriptions).  The following list details what vector operation each function performs:
\begin{itemize}
	\item {\tt Copy}: Copies vector $\bm v_{1}$ into vector $\bm v_{2}$
	\item {\tt SetZero}: Returns a zero vector
	\item {\tt Set}: Sets the vector representation to specific values
	\item {\tt Add}: Sums  $\bm v_{1}$ and  $\bm v_{2}$
	\item {\tt Subtract}: Returns the difference  $\bm v_{1}$ - $\bm v_{2}$
	\item {\tt Scale}: Returns $\bm v_{1}$ multiplied by a scalar $\alpha$
	\item {\tt Dot}: Returns the dot product $\bm v_{1} \cdot \bm v_{2}$
	\item {\tt OuterProduct}: Returns the outer product $\bm v_{1} \bm v_{2}^{T}$
	\item {\tt Normalize}: Returns a normalized vector $\bm v_{1}/ v_{1}$
	\item {\tt MaxAbs}: Returns the largest element of a vector
	\item {\tt IsEqual}: Checks if two vector represnetations are identical
	\item {\tt IsZero}: Checks if a vector is full of zero elements
	\item {\tt Print}: Prints the vector representation to a file
	\item {\tt Sort}: Sorts the vector elements by size
	\item {\tt Print}: Prints the vector representation to a file
	\item {\tt PrintScreen}: Prints the vector representation to the screen
\end{itemize}

\subsubsection{Matrix Operations}
The matrix related functions are all labeled with a letter '{\tt M}' and are broken down into the following matrix dimension related categories:
\begin{itemize}
	\item  {\tt mXXXXy()}: the code works on a $n\times m$ matrix of arbitrary dimension $n$ and $m$.
	\item {\tt m22XXXX()}: the code works on a $2\times 2$ matrix defined through {\tt mat[2][2]}.
	\item {\tt m33XXXX()}: the code works on a $3\times 3$ matrix defined through {\tt mat[3]3]}.
	\item {\tt m44XXXX()}: the code works on a $4\times 4$ matrix defined through {\tt mat[4][4]}.
\end{itemize}

The following list provides an overview of the supported matrix functions.  Note that not al dimensions have all functions provided, but the 2, 3, and 4 dimensional matrix support is pretty complete.
\begin{itemize}
	\item {\tt SetIdentity}: Returns an identity matrix
	\item {\tt SetZero}: Returns a zero matrix
	\item {\tt Set}: Creates a matrix with specific values
	\item {\tt Copy}: Copies the matrix $[M_{1}]$ into $[M_{2}]$
	\item {\tt m33MultM33}: Performs the matrix to matrix multiplication $[M_{1}][M_{2}]$
	\item {\tt m33MultM33t}: Performs the matrix to matrix multiplication $[M_{1}][M_{2}]^{T}$
	\item {\tt m33tMultM33}: Performs the matrix to matrix multiplication $[M_{1}]^{T}[M_{2}]$
	\item {\tt m33MultV3}: Computes $[M_{1}]\bm v_{1}$
	\item {\tt m33tMultV3}: Computes $[M_{1}]^{T}\bm v_{1}$
	\item {\tt v3tMultM33}: Computes $\bm v_{1}^{T}[M_{1}]$
	\item {\tt v3tMultM33t}: Computes  $\bm v_{1}^{T}[M_{1}]^{T}$
	\item {\tt Tilde}: Returns the skew-symmetric matrix $[\tilde{\bm v}_{1}]$
	\item {\tt Transpose}: Returns $[M_{1}]^{T}$
	\item {\tt Add}: Returns the sum $[M_{1}] + [M_{2}]$
	\item {\tt Subtract}: Returns the difference $[M_{1}] - [M_{2}]$
	\item {\tt Scale}: Returns the scaled matrix $\alpha [M_{1}]$
	\item {\tt Trace}: Compute the matrix trace $\sum M_{ii}$
	\item {\tt Determinant}: Returns the square matrix determinant
	\item {\tt IsEqual}: Checks if all matrix elements are equal
	\item {\tt IsZero}: Checks if all matrix elements are zero
	\item {\tt Print}: Prints the matrix representation to a file
	\item {\tt PrintScreen}: Prints the matrix representation to the screen
	\item {\tt Inverse}: Returns the matrix inverse $[M_{1}]^{-1}$
	\item {\tt SingularValues}: Computes the singular values of $[M_{1}]$
	\item {\tt EigenValues}: Computes the Eigenvalues of $[M_{1}]$
	\item {\tt ConditionNumber}: Computes the condition number of $[M_{1}]$
\end{itemize}

\subsection{{\tt rigidBodyKinematics} Library}

The following discussion is a brief overview of the {\tt rigidBodyKinematics} library function notation.  Please see Appendix E in Reference~\citenum{schaub} for a complete description:
\begin{itemize}
	\item {\tt XXX2YYY}: Converts the attitude coordinates {\tt XXX} to {\tt YYY}
	\item {\tt addXXX}: Add the two attitude descriptions $\bm x_{1} = \bm x_{\cal B/F}$ and $\bm x_{2}= \bm x_{\cal F/N}$ to return the sequential rotation of first $\bm x_{1}$ and then $\bm x_{2}$.  Returns is the description $\bm x_{3} = \bm x_{\cal B/N}$
	\item {\tt subXXX}: Subtract the two attitude descriptions $\bm x_{1} = \bm x_{\cal F/N}$ and $\bm x_{2}= \bm x_{\cal B/N}$ to return the relative orientation $\bm x_{3} = \bm x_{\cal F/B}$
	\item {\tt BmatXXX}: Returns the matrix $[B]$ of the differential kinematic equations of the attitude parameters {\tt XXX} in Reference~\citenum{schaub}.  Note the scalar multiplier is not included here.  For example, with MRPs\cite{survey} $[B]$ is defined as $$\dot{\bm \sigma}_{\cal B/N} = \frac{1}{4}[B(\bm\sigma_{\cal B/N})] \leftexp{B}{\bm\omega}_{\cal B/N}$$
	\item {\tt BinvXXX}: Returns the matrix inverse $[B]^{-1}$ of the attitude parameters {\tt XXX}
	\item {\tt dX}: Returns the time derivatives of the attitude parameters {\tt XXX} as a function of these parameters and the body angular velocity vector $\bm \omega$
	\item {\tt Mi}: Returns the single-axis DCM about the $\hat{\bm b}_{i}$ axis
	\item {\tt wrapToPi}: Makes sure that an angle  lies within $\pm\pi$
\end{itemize}

\subsection{{\tt orbitalMotion} Library}

\subsubsection{Orbit Anomaly Angle Conversions}
To convert between the various anomaly angles, the following functions are defined:
\begin{itemize}
	\item {\tt double f2E(double f, double e)}
	\item {\tt double E2f(double Ecc, double e)}
	\item {\tt double E2M(double Ecc, double e)}
	\item {\tt double M2E(double M, double e)}, with a change conversion tolerance of $10^{-13}$ and a maximum iteration limit of 200
	\item {\tt double f2H(double f, double e)}
	\item {\tt double H2f(double H, double e)}
	\item {\tt double H2N(double H, double e)}
	\item {\tt double N2H(double N, double e)}, with a change conversion tolerance of $10^{-13}$ and a maximum iteration limit of 200
\end{itemize}

\subsubsection{Orbit Element Conversion}
To convert from classical orbit elements to inertial Cartesian coordinates, the function
\begin{verbatim}
	elem2rv(double mu, classicElements *elements, double *rVec, double *vVec)
\end{verbatim}
is used where {\tt mu} is the gravitational constant of the 2-body problem, the classical elements are defined through
$$
	(a, e, i, \Omega, \omega, f)
$$
where the anomaly angle is typically given by $f$, unless the orbit is a rectilinear motion in which case the anomaly angle input is $E$.  The function returns the inertial position and velocity vectors in the arrays {\tt rVec} and {\tt vVec}.

To convert from  inertial Cartesian coordinates to classical orbit elements, the function
\begin{verbatim}
	rv2elem(double mu, double *rVec, double *vVec, classicElements *elements)
\end{verbatim}
Beyond the classical elements listed above, this routine also stores the radius of perapses $r_{p}$ and apoapses $r_{a}$, as well as $\alpha = \dfrac{1}{a}$.

\subsubsection{Space Environment Functions}
\paragraph{Atmospheric Density}
The Earth's atmospheric density $\rho$ is computed using\\
\indent	{\tt double atmosphericDensity(double alt)} \\
The density is returned as a scalar value.

\paragraph{Mean Debye Length}
The mean Debye Length for the near-Earth environment is approximated, very crudely, through a polynomial fit.  The function returns the scalar $\lambda_{d}$ and is called through \\
\indent {\tt double debyeLength(double alt)}

\paragraph{Atmospheric Drag Acceleration}
To compute an estimate of  the Earth's atmospheric drag acceleration, use the function: \\
\indent {\tt void atmosphericDrag(double Cd, double A, double m, double *rvec, double *vvec, double *advec)}

The inputs are the ballistic drag coefficient {\tt Cd}, the velocity-projected cross-sectional area {\tt A}, as well as the spacecraft mass {\tt m}.  Given the inertial position vectors {\tt rvec} and {\tt vvec}, the function returns the drag acceleration {\tt advec}.

\paragraph{Gravitational Zonal Harmonics}
This function returns the inertial acceleration due to a planets zonal Harmonic.  The function call is:\\
\indent {\tt  void jPerturb(double *rvec, int num, double *ajtot, ...)}

If not option argument is provided, then the zonal harmonics of Earth are simulated.  About other celestial objects only the $J_{2}$ harmonic is implemented.  Here the object is specified through the {\tt CelestialObject\_t} enumeration.  For example, to get the $J_{2}$ zonal harmonic about Venus the argument {\tt CELESTIAL\_VENUS} is provided.

\paragraph{Solar Radiation Pressure Acceleration}
To compute the inertial disturbance acceleration due to the solar radiation pressure use  the function\\
\indent {\tt void solarRad(double A, double m, double *sunvec, double *arvec)}

Here {\tt A} is the projected surface area, {\tt m} is the spacecraft mass, {\tt sunvec} is the sun position vector to the Sun in units of AU.


\documentclass[epsfig,10pt,fullpage]{article}

\newcommand{\LabNum}{7}
\newcommand{\CommonDocsPath}{../../../common/docs}
\input{\CommonDocsPath/preamble.tex}

\begin{document}

\centerline{\huge Digital Logic}
~\\
\centerline{\huge Laboratory Exercise \LabNum}
~\\
\centerline{\large Finite State Machines}
~\\

\noindent
This is an exercise in using finite state machines.
~\\

\section*{Part I}
\addcontentsline{toc}{1}{Part I}
We wish to implement a finite state machine (FSM) that recognizes two specific sequences
of applied input symbols, namely four consecutive 1s or four consecutive 0s. There is an
input $w$ and an output $z$. Whenever $w = 1$
or $w = 0$ for four consecutive clock pulses the value of $z$ has to be 1;
otherwise, $z = 0$. Overlapping sequences
are allowed, so that if $w = 1$ for five consecutive clock pulses the output $z$
will be equal to 1 after the fourth and
fifth pulses. Figure~\ref{fig:sequence_diagram} illustrates the required relationship between $w$ and $z$.

\begin{figure}[H]
\centerline{
\includegraphics{figures/figure1.pdf}}
\caption{Required timing for the output $z$.}
\label{fig:sequence_diagram}
\end{figure}

A state diagram for this FSM is shown in Figure~\ref{fig:state_diagram}. For this part you are to manually
derive an FSM circuit that implements this state diagram, including the logic expressions
that feed each of the state flip-flops. To
implement the FSM use nine state flip-flops called $y_8, \ldots, y_0$ and the
one-hot state assignment given in Table~\ref{tab:state_assignment}.

\begin{figure}[H]
\centerline{
\includegraphics{figures/figure2.pdf}}
\caption{A state diagram for the FSM.}
\label{fig:state_diagram}
\end{figure}

\begin{table}[H]
\begin{center}
\begin{tabular}{c|c}
~ & State Code \\
Name & $y_8 y_7 y_6 y_5 y_4 y_3 y_2 y_1 y_0$ \\ \hline
\rule[-0.075in]{0in}{0.25in}{\bf A} & $000000001$ \\
{\bf B} & $000000010$ \\
{\bf C} & $000000100$ \\
{\bf D} & $000001000$ \\
{\bf E} & $000010000$ \\
{\bf F} & $000100000$ \\
{\bf G} & $001000000$ \\
{\bf H} & $010000000$ \\
{\bf I} & $100000000$ \\
\end{tabular}
\end{center}
\caption{One-hot codes for the FSM.}
\label{tab:state_assignment}
\end{table}
~\\
~\\
Design and implement your circuit on your DE-series board as follows:

\begin{enumerate}
\item Create a new Quartus\textsuperscript{\textregistered} project for the FSM circuit.

\item Write a Verilog file that instantiates the nine flip-flops in the circuit and which
specifies the logic expressions that drive the flip-flop input ports. Use only
simple {\bf assign} statements in your Verilog code to specify the logic feeding the
flip-flops. Note that the one-hot code enables you to derive these expressions by
inspection.

Use the toggle switch {\it SW}$_0$ as an active-low synchronous reset input
for the FSM, use {\it SW}$_1$ as the {\it w} input, and the pushbutton {\it KEY}$_0$ as the clock input which
is applied manually.  Use the red light {\it LEDR}$_9$ as the output $z$,
and assign the state flip-flop outputs to the red lights {\it LEDR}$_8$ to {\it LEDR}$_0$.

\item Include the Verilog file in your project, and assign the pins on the FPGA to
connect to the switches and the LEDs.

\item Simulate the behavior of your circuit.

\item Once you are confident that the circuit works properly as a result of your
simulation, download the circuit into the FPGA chip.  Test the functionality of your
design by applying the input sequences and observing the output LEDs. Make sure that the
FSM properly transitions between states as displayed on the red LEDs, and that it produces
the correct output values on {\it LEDR}$_9$.

\item Finally, consider a modification of the one-hot code given in Table~\ref{tab:state_assignment}. It is often
desirable to set all flip-flop outputs to the value 0 in the reset state.

Table~\ref{tab:modified_state_assignment} shows a modified one-hot state assignment in which the reset state, {\it A},
uses all 0s. This is accomplished by inverting the state variable $y_0$.
Create a modified version of your Verilog code that implements this state
assignment. ({\it Hint}: you should need to make very few changes to the logic expressions
in your circuit to implement the modified state assignment.)

\item Compile your new circuit and test it.

\begin{table}[H]
\begin{center}
\begin{tabular}{c|c}
~ & State Code \\
Name & $y_8 y_7 y_6 y_5 y_4 y_3 y_2 y_1 y_0$ \\ \hline
\rule[-0.075in]{0in}{0.25in}{\bf A} & $000000000$ \\
{\bf B} & $000000011$ \\
{\bf C} & $000000101$ \\
{\bf D} & $000001001$ \\
{\bf E} & $000010001$ \\
{\bf F} & $000100001$ \\
{\bf G} & $001000001$ \\
{\bf H} & $010000001$ \\
{\bf I} & $100000001$ \\
\end{tabular}
\end{center}
\caption{Modified one-hot codes for the FSM.}
\label{tab:modified_state_assignment}
\end{table}

\end{enumerate}

\section*{Part II}
\addcontentsline{toc}{2}{Part II}
For this part you are to write another style of Verilog code for the FSM in Figure 2. In
this version of the code you should not manually derive the logic expressions needed for
each state flip-flop. Instead, describe the state table for the FSM by using a
Verilog {\bf case} statement in an {\bf always} block, and use another {\bf always} block to
instantiate the state flip-flops. You can use a third {\bf always} block or simple assignment
statements to specify the output $z$. To implement the FSM, use four state flip-flops
$y_3, \ldots, y_0$ and binary codes, as shown in Table~\ref{tab:bincodes}.

\begin{table}[H]
\begin{center}
\begin{tabular}{c|c}
~ & State Code \\
Name & $y_3 y_2 y_1 y_0$ \\ \hline
\rule[-0.075in]{0in}{0.25in}{\bf A} & $0000$ \\
{\bf B} & $0001$ \\
{\bf C} & $0010$ \\
{\bf D} & $0011$ \\
{\bf E} & $0100$ \\
{\bf F} & $0101$ \\
{\bf G} & $0110$ \\
{\bf H} & $0111$ \\
{\bf I} & $1000$ \\
\end{tabular}
\end{center}
\caption{Binary codes for the FSM.}
\label{tab:bincodes}
\end{table}

A suggested skeleton of the Verilog code is given in Figure~\ref{fig:skeleton_code}.

\begin{figure}[H]
\begin{center}
\begin{minipage}[t]{12.5 cm}
\begin{tabbing}
ZZ\=ZZ\=ZZ\=ZZ\=ZZ\=ZZ\=ZZ\=ZZ\=ZZ\=ZZ\=ZZ\kill
{\bf module} ~part2 (~$\ldots$~);\\
\>$\ldots$ define input and output ports\\
~\\
\>$\ldots$ define signals\\
\>{\bf reg} [3:0] y\_Q, Y\_D;  \>\>\>\>\>\>\>\>// y\_Q represents current state, Y\_D represents next state\\
\>{\bf parameter} A = 4'b0000, B = 4'b0001, C = 4'b0010, D = 4'b0011, E = 4'b0100,\\
\>\>F = 4'b0101, G = 4'b0110, H = 4'b0111, I = 4'b1000;\\
~\\
\>{\bf always} @(w, y\_Q)\\
\>{\bf begin}: state\_table\\
\>\>{\bf case} (y\_Q)\\
\>\>\>A:	\>{\bf if} (!w) Y\_D = B;\\
\>\>\>\>{\bf else} Y\_D = F;\\
\>\>\>$\ldots$ remainder of state table \\
\>\>\>{\bf default}: Y\_D = 4'bxxxx;\\
\>\>{\bf endcase}\\
\>{\bf end} // state\_table\\
\\
\>{\bf always} @({\bf posedge} Clock)\\
\>{\bf begin}: state\_FFs\\
\>\>$\ldots$ \\
\>{\bf end} // state\_FFS\\
\\
\>$\ldots$ assignments for output z and the LEDs\\
{\bf endmodule}
~\rule{5.0in}{0in}\\
\end{tabbing}
\end{minipage}
\end{center}
\caption{Skeleton Verilog code for the FSM.}
\label{fig:skeleton_code}
\end{figure}
~\\
Implement your circuit as follows.

\begin{enumerate}
\item Create a new project for the FSM.

\item Include in the project your Verilog file that uses the style of code in Figure 3.
Use the same switches, pushbuttons, and lights that were used in Part I.

\item Before compiling your code it is necessary to explicitly tell the Synthesis tool in
Quartus that you wish to have the finite state machine implemented using the state
assignment specified in your Verilog code. If you do not explicitly give this
setting to Quartus, the Synthesis tool will automatically use a state assignment of
its own choosing, and it will ignore the state codes specified in your Verilog code. To
make this setting, choose {\sf Assignments > Settings} in Quartus, and click on the
{\sf Compiler Settings} item on the left side of the window, then click on
the {\sf Advanced Settings (Synthesis)} button.  As indicated in Figure 4, change the
parameter {\sf State Machine Processing} to the setting {\sf User-Encoded}.

\item Compile your project. To examine the circuit produced by Quartus open
the RTL Viewer tool. Double-click
on the box shown in the circuit that represents the finite state machine, and determine
whether the state diagram that it shows properly corresponds to the one in Figure 2.
To see the state codes used for your FSM, open the Compilation Report, select the {\sf Analysis
and Synthesis} section of the report, and click on {\sf State Machines}.

\item Download the circuit into the FPGA chip and test its functionality.

\item In step 3 you instructed the Quartus Synthesis tool to use the state
assignment given in your Verilog code. To see the result of removing this setting, open
again the Quartus settings window by choosing {\sf Assignments > Settings}, and
click on the {\sf Compiler Settings} item on the left side of the window, then click on
the {\sf Advanced Settings (Synthesis)} button. Change the setting for
{\sf State Machine Processing} from {\sf User-Encoded} to {\sf One-Hot}. Recompile the
circuit and then open the report file, select the {\sf Analysis
and Synthesis} section of the report, and click on {\sf State Machines}.
Compare the state codes shown to those given in Table~\ref{tab:modified_state_assignment}, and
discuss any differences that you observe.
\end{enumerate}

\begin{figure}[H]
\centerline{
\includegraphics[scale = 0.75]{figures/figure4.png}}
\caption{Specifying the state assignment method in Quartus.}
\label{fig:fig7_4}
\end{figure}

\section*{Part III}
\addcontentsline{toc}{3}{Part III}
The sequence detector can be implemented in a straightforward manner using shift registers,
instead of using the more formal approach described above. Create Verilog code that
instantiates two 4-bit shift registers; one is for recognizing a sequence of four 0s, and
the other for four 1s. Include the appropriate logic expressions in your design
to produce the output $z$. Make a Quartus project for your design and implement
the circuit on your DE-series board. Use the switches and LEDs on the board in a similar way as
you did for Parts I and II and observe the behavior of your shift registers and the
output $z$. Answer the following question: could you use just one 4-bit shift
register, rather than two? Explain your answer.

~\newpage
\section*{Part IV}
\addcontentsline{toc}{4}{Part IV}
In this part of the exercise you are to implement a Morse-code encoder using an FSM. The Morse code uses
patterns of short and long pulses to represent a message. Each letter is represented as a sequence of dots
(a short pulse), and dashes (a long pulse). For example, the first eight letters of the alphabet have the
following representation:
~\\
\begin{center}
\begin{minipage}[t]{12.5 cm}
\begin{tabbing}
ZZ\=ZZ\=ZZ\=ZZ\=ZZ\=ZZ\=ZZ\=ZZ\=ZZ\=ZZ\=ZZ\kill
\>A\>\>{\bf $\bullet$  ---}\\
\>B\>\>{\bf ---  $\bullet$  $\bullet$  $\bullet$}\\
\>C\>\>{\bf ---  $\bullet$  ---  $\bullet$}\\
\>D\>\>{\bf ---  $\bullet$  $\bullet$}\\
\>E\>\>{\bf $\bullet$}\\
\>F\>\>{\bf $\bullet$  $\bullet$  ---  $\bullet$}\\
\>G\>\>{\bf ---  ---  $\bullet$}\\
\>H\>\>{\bf $\bullet$  $\bullet$  $\bullet$  $\bullet$}\\
\end{tabbing}
\end{minipage}
\end{center}

~\\
Design and implement a Morse-code encoder circuit using an FSM. Your circuit should take as input one of the first eight letters of the alphabet and display the Morse code for it on a red LED. Use switches {\it SW}$_{2-0}$ and pushbuttons {\it KEY}$_{1-0}$ as inputs. When a user presses {\it KEY}$_1$, the circuit should display the Morse code for a letter specified by {\it SW}$_{2-0}$ (000 for A, 001 for B, etc.), using 0.5-second pulses to represent dots, and 1.5-second pulses to represent dashes. Pushbutton {\it KEY}$_0$ should function as an asynchronous reset.

A high-level schematic diagram of a possible circuit for the Morse-code encoder
is shown in Figure~\ref{fig:morse_code_cct}.

\begin{figure} [H]
\begin{center}
	\includegraphics[scale = 0.9]{figures/fig_morse_code_circuit_schematic.pdf}
\end{center}
\caption{High-level schematic diagram of the circuit for Part IV.}
\label{fig:morse_code_cct}
\end{figure}

\input{\CommonDocsPath/copyright.tex}
\end{document}


\documentclass[letterpaper]{article}

\usepackage{fullpage}
\usepackage{nopageno}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage[utf8]{inputenc}
\allowdisplaybreaks

\newcommand{\abs}[1]{\left\lvert #1 \right\rvert}

\begin{document}
\title{Notes}
\date{November 7, 2014}
\maketitle
\section*{5.3.J}
Let $m(x,y)=\max\{x,y\}$. prove that $m$ is continuous and then show that $h(x)=\max\{f(x),g(x)\}$ is continuous if $f(x)$ and $g(x)$ are continuous.

case 1, $m(x,y)=x,m(x_1,y_1)=x_1$ then $|m(x,y)-m(x_1,y_1)|=|x-x_1|$. take $r=\varepsilon$ and then $||(x,y)-(x_1,y_1)||<r$ then $|m(x,y)-m(x_1,y_1)|<\varepsilon$. similar with $m(x,y)=x$ and $m(x_1,y_1)=y_1$ is similar

now if $m(x,y)=x$ and $m(x_1,y_1)=y_1$ then $-\varepsilon<x-y_1<\varepsilon$.
\begin{align*}
  -r<x-x_1<r\\
  -r<y-y_1<r\\
  x>y\\
  y_1>x_1\\
  -y_1<-x_1\\
  (x-y_1)<(x-x_1)\\
\end{align*}
So if $x-x_1<\varepsilon$ then $(x-y_1)<\varepsilon$.
\begin{align*}
  y-y1<x-y1\\
  \text{if }y-y1>-\varepsilon\\
  \text{then }x-y1>-\varepsilon\\
\end{align*}
so $r=\varepsilon$ will give us $|x-y1|<\varepsilon$

now let $\varepsilon\circ f(x)=\varepsilon_1(f(x)=(f(x),0)$ and $\varepsilon_2\circ g(x)=\varepsilon_2(g(x)=(0,g(x))$. and $h(x)=m\circ(\varepsilon\circ f(x)+\varepsilon\circ g(x))$

because sum and composition of continuous functions are continuous, then h is continuous. induction gives us continuity for $\max\{f_1(x),\dots,f_m(x)\}$

\section*{5.4H,I\& 5.5F}
let $f:\mathbb{R}\to\mathbb{R}$ be periodic and continuous, ie $\exists d>0$ such that $\forall x\in \mathbb{R}, f(x+d)=f(x)$.

\subsection*{show it attains max min}
look at $y\in[x,x+d]$ then $\exists a,b\in [x,x+d]$ such that $f(a)\le f(y)\le f(b)$. periodicity gives us same properties on all of $\mathbb{R}$
\subsection*{show that it is bounded and uniformally continuous}
\subsection*{find a function $f:\mathbb{R}^2\to \mathbb{R}$ such that $f(x+1,y)=f(x,y)\forall(x,y)$ but $f$ is not bounded or does not attain it's max}
\end{document}


%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Local B-Spline}
\label{local_b_spline}

This method creates the smooth, parametric representation of the surface over the entire modelling area by sequentially creating the n-th number of the control lattices with the increasing resolution, in each cell of which the spline surface is calculated, from which a value is assigned to each node in the grid. This is mine sentence and I want it to stay, anyway. So, let's merge it! An undeniable advantage of the method is the qualitative approximation of highly scattered, irregularly spaced set of the points in a fully automatic mode that does not require any complicated settings from the user and a significant increase in the productivity due to the use of B-Splines in the hierarchy of the control grids.\\

The method algorithm has the following steps:`
\begin{itemize}
	\item
	The $ \Omega = {(x,y) | 0 \le x < m, 0 \le y < n} $ area is created, covering all $ P $ input data which equals $ {(x_{c},y_{c},z_{c})} $ in 3D space, where $ (x_{c},y_{c}) $ is the point of input data in 2D space of $ \Omega $ area;
	\item
	The control lattices $ \Phi_{n} $ are created sequentially with increasing resolution (i.e. the number of lattice cells is constantly increasing), covering the $ \Omega $ area.

	The lattice creation algorithm:
	\begin{itemize}
		\item
		The $ \Phi_{n} (m=1, n=1) $ lattice is created (see \pic{phi_lattice}) containing $ (m+3)\times(n+3) $ nodes (at the beginning $ (1+3)\times(1+3) = 16 nodes $, then at each iteration the value of m and n is multiplied by 1.25 and the resulting number of nodes is rounded). Each lattice cell will be described in one spline surface, by calculating the bicubic spline function using the formula:

		$$
		f(x,y) = \sum_{k=0}^3 \sum_{l=0}^3 B_{k}(s)B_{l}(t)\phi_{(i+k)(j+l)},
		$$

		where $ i = \lfloor x \rfloor -1 $, $ \lfloor y \rfloor -1 $, $ s= x - \lfloor x \rfloor $, $ t = y - \lfloor y \rfloor $, $ B_{k} $ and $ B_{l} $ - uniform cubic B-spline basis functions defined as:

		$$
		B_{0}(t) = (1-t)^3 \div 6,
		$$

		$$
		B_{1}(t) = (3t^3 - 6t^2 + 4) \div 6,
		$$

		$$
		B_{2}(t) = (-3t^3 +3t^2 +3t + 1) \div 6,
		$$

		$$
		B_{3}(t) = t^3 \div 6.
		$$

		\begin{figure}[h]
			\center{
				\includegraphics[width=0.6\textwidth]{pics_geology/phi_lattice.png}
			}
			\caption{Schematic representation of the conditional lattice $ \Phi $ covering the $ \Omega $ area with the set of the scattered points as well as the proximity data set $ \phi $ with the control point $ \phi_{ij} $. In this case, the resulting B-spline function value will be assigned to the node where the control point is located $ \phi_{ij} $.}
			\label{ris:phi_lattice}
		\end{figure}

		\clearpage

		\item
		Each lattice cell of $ \Phi_{n} $ will be presented as a spline patch, on the basis of which a value is assigned to each lattice node, thus enabling a parametric surface model to be created. For the approximation of the scattered source point data $ P $, a bicubic B-Spline function is defined inside the cell of the $ \Phi $ lattice. Each cell of the $ \Phi_{n} $ lattice will be presented as a spline surface, from which a value is assigned to each node in the grid;

		The spline surface creation algorithm:
		\begin{itemize}
			\item
			The proximity data set $ \phi $ is created, consisting of 16 nodes, which allows you to limit the number of points affecting the value of a node to only those points that are inside this area (see \pic{phi_lattice});
			\item
			The impact of each source point $ (x_{c},y_{c}) $  which has been placed in the proximity data set $ \phi $ is sequentially calculated using the formula:

			$$
			\phi_{ij} = \frac{\sum_{c} \omega_{c}^2 \phi_{c}}{\sum_{c}, \omega_{c}^2}
			$$

			where $ \omega_{c} = \omega_{kl} =  B_{k}(s)B_{l}(t) $, $ k = (i+1) - \lfloor x_{c} \rfloor $, $ l = (j+1) - \lfloor y_{c} \rfloor $, $ s = x_{c} - \lfloor x_{c} \rfloor $, $ t = y_{c} - \lfloor y_{c} \rfloor $, and $ \phi_{c} $ in turn is calculated using the formula:

			$$
			\phi_{c} = \frac{\omega_{c} z_{c}}{\sum_{a=0}^3 \sum_{b=0}^3 \omega_{ab}^2}
			$$

			\item
			The value that affects the resulting function of the bicubic spline $ f $ is assigned to the coefficient $ \phi_{ij} $.
		\end{itemize}
		\begin{info}
			Cells with no source points inside it may appear while the lattice resolution increases. In this case, such cells will be represented by spline surfaces with zero values.
		\end{info}
		\item
		After estimating the obtained values in each $ \Phi_{n} $ node, we subtract them from the source data (i.e. we calculate the discrepancies between the source data and the surface model $ \Phi_{n} $);
		\item
		The algorithm continues to create the new lattices multiplying the number of cells by 1.25 at each subsequent iteration. The iterative process is repeated until the lattice $ \Phi_{n} $ becomes as detailed as requested by the user at the \textbf{Grid Properties};
	\end{itemize}
	\item
	The algorithm then splits all lattices of the previous iterations $ \Phi_{n-1} $ to achieve the resolution of the last $ \Phi_{n} $ lattice, sums up the obtained values of all lattices, which results in the desired surface $ \Phi_{ij} $.
\end{itemize}

The parametric surface model built on each iteration represents the difference between the source data and the sum of the surfaces obtained on all previous iterations (see \pic{local_b_spline}).

\begin{figure}[h]
	\center{
		\includegraphics[width=0.5\textwidth]{pics_geology/local_b_spline.png}
	}
	\caption{Interpolation of points set using the Local B-spline method on the grid in 10 m steps, both X and Y.}
	\label{ris:local_b_spline}
\end{figure}

\clearpage


\section{Numerical methods}
Recall that we compute the buckling eigenmodes via computing the zeros
of the determinant of linear system corresponding to a Nystr\"{o}m
discretization of the integral operator $A(k)$ in~\cref{eq:inteqfinal}.
As in section~\cref{sec:spec}, let $\tilde{A}_{k}$ denote the discretized
linear system.
Thus, the three main ingredients for the fast and accurate computation
of buckling eigenvalues (and its corresponding eigenfunctions) are
high order Nystr\"{o}m discretizations of the integral operator
$A(k)$; for a given parameter $k$, the rapid evaluation of the determinant
$\tilde{A}_{k}$; and finding the zeros of $\text{det}(\tilde{A}_{k})$ (which
is non-linear in $k$) while minimizing the function evaluations.

For discretizing the integral operator using the
Nystr\"{o}m method, we divide the boundary $\Gamma$ into
panels and represent the unknown density and the
boundary data by their values at scaled Gauss-Legendre
nodes on each panel.
Let $n_{p}$ denote the number of Gauss-Legendre panels.
We discretize each panel using 16 scaled Gauss-Legendre nodes.
Then $n_{d} = 16 n_{p}$ is the number of discretization points
on the boundary.
Let $\bx_{j}$ denote the discretization nodes, $w_{j}$ denote the
appropriately scaled Gauss-Legendre quadrature weights for smooth functions,
and $\bmu_{j}$ denote the unknown density at $\bx_{j}$.
When forming the linear system, we use scaled unknowns,
$\bmu_{j} \sqrt{w_{j}}$, so that the spectral properties of the
discrete system with respect to the $l_2$ norm
are approximations of the spectral properties of the continuous
system as on operator on $L_2$ (for more on this point of view,
see \cite{bremer2012}).
The integral kernels $K_{ij}$ in~\cref{eq:inteqfinal}
are either smooth or have a weak (logarithmic) singularity.
For the smooth kernels, we use standard
Gauss-Legendre quadrature rule.
For kernels with a logarithmic singularity, we use order $20$
generalized Gaussian quadrature rule~\cite{bremer2010,bremer2010u}.
Note that the resulting discretized linear system $\tilde{A}_{k}$
is a dense matrix of size $M=2n_{d}+N+1$,
where $N$ is the number of connected components.

For small problems ($M \leq 10^3$), we directly compute
$\text{det}(\tilde{A}_{k})$ using standard linear algebra routines
from LAPACK.
However, since the matrix $\tilde{A}_{k}$ is dense,
the computational complexity of computing $\text{det}(\tilde{A}_{k})$
using LAPACK routines is $O(M^3)$.
Moreover, for evaluating the buckling eigenmodes, $\text{det}(\tilde{A}_{k})$
must be evaluated for many different values of $k$.
Thus, for larger problems, we use the HIFIE method for evaluating
$\text{det}(\tilde{A}_{k})$.
This method exploits the rank structure of $\tilde{A}_{k}$ (large blocks
of $\tilde{A}_{k}$ corresponding to interactions between well-separated
clusters are low-rank) and evaluates the determinant in O($M \cdot \log{M})$
CPU-time.


\section{Supplementary Information}
\subsection{Absorption Harmonic Oscillator Model}
We will find useful here the generic form of the Franck Condon Overlap factor for a displaced harmonic oscillator with the same curvature in the ground and excited states:
\begin{align}
	O_{m}^{n} &= \left(-1\right)^{n} \sqrt{\frac{e^{-S}S^{m+n}}{m!n!}} \sum_{j=0}^{\min \left( m,n\right)} \frac{m!n!(-1)^j S^{-j}}{j!(m-j)!(n-j)!}
	\label{eqn:overlap}
\end{align}
\subsubsection{Linear Correction}
To calculate the electric field ratio for a linear non-Condon correction for a system starting in the 0th state:
\begin{align*}
	\mu_{0,\lambda} =& \mu_0 \bra{0} \left( 1 + c_1\left(\hat{a} + \hat{a}^{\dagger}\right) \right) \ket{\lambda} \\
	=& \mu_0\left( \delta_{0,\lambda} + c_1 \delta_{1, \lambda}\right)
\end{align*}

\begin{align}
	H_{(1)}(0,k) &=\frac{\sum_{\lambda,\nu} \left[ \delta_{0,\lambda} + c^*\delta_{\lambda, 1} \right]\left[ \delta_{0,\nu} + c \delta_{\nu, 1} \right]   O_{\lambda}^{k+1} O_{\nu}^{k+1} }{\sum_{l,n} \left[ \delta_{0,l} + c^*\delta_{l, 1} \right]\left[ \delta_{0,n} + c \delta_{n, 1} \right]   O_{l}^{k} O_{n}^{k} }\\
	&= \frac{ \left[ O_{0}^{k+1} + c^*O_{1}^{k+1} \right]\left[ O_{0}^{k+1} + c O_{1}^{k+1} \right]   }{\left[ O_{0}^{k} + c^*O_{1}^{k} \right]\left[ O_{0}^{k} + c O_{1}^{k}\right]     } \\
	&=  \frac{\left| O_{0}^{k+1} + cO_{1}^{k+1}  \right|^2 }{\left|O_{0}^{k} + cO_{1}^{k} \right|^2} \\
	&= \left(\frac{O_{0}^{k+1}}{O_{0}^{k}} \right)^2   \frac{ \left|1 + c\frac{O_{1}^{k+1}}{O_{0}^{k+1}}  \right|^2}{\left|1 + c\frac{O_{1}^{k} }{O_{0}^{k}}\right|^2}
\end{align}

For this, several quantities can be computed

\begin{align}
	O_{0}^{k+1} &= \left(-1\right)^{k+1} \sqrt{\frac{e^{-S}S^{k + 1}}{(k+1)!}} \\
	O_{1}^{k+1} &= \left(-1\right)^{k+1} \sqrt{\frac{e^{-S}S^{1+k+1}}{(k+1)!}} \sum_{j=0}^{\min \left( 1,k+1 \right)} \frac{(k+1)!(-1)^j S^{-j}}{j!(1-j)!(k+1-j)!}\\
	&= \left(-1\right)^{k+1} \sqrt{\frac{e^{-S}S^{1+k+1}}{(k+1)!}} \left[  \frac{(k+1)! }{(k+1)!}  - \frac{(k+1)! S^{-1}}{k!}\right]\\
	&= \left(-1\right)^{k+1} \sqrt{\frac{e^{-S}S^{1+k+1}}{(k+1)!}} \left[  1  - \frac{(k+1) }{S}\right]
\end{align}
then the desired ratio is
\begin{align}
	\frac{O_{1}^{k+1}}{ O_{0}^{k+1}} &=  \frac{\left(-1\right)^{k+1} \sqrt{\frac{e^{-S}S^{1+k+1}}{(k+1)!}} \left[  1  - \frac{(k+1) }{S}\right]}{\left(-1\right)^{k+1} \sqrt{\frac{e^{-S}S^{k + 1}}{(k+1)!}}}\\
	&=  \frac{ \sqrt{\frac{e^{-S}S^{1+k+1}}{(k+1)!}} }{ \sqrt{\frac{e^{-S}S^{k + 1}}{(k+1)!}}} \left[  1  - \frac{(k+1) }{S}\right]\\
	&= \sqrt{S} \left[  1  - \frac{(k+1) }{S}\right]
\end{align}
as for the other ratio:
\begin{align}
	O_{0}^{k} &= \left(-1\right)^{k} \sqrt{\frac{e^{-S}S^{k }}{k!}} \\
	O_{1}^{k} &= \left(-1\right)^{k} \sqrt{\frac{e^{-S}S^{1+k}}{k!}} \sum_{j=0}^{\min \left( 1,k \right)} \frac{k!(-1)^j S^{-j}}{j!(1-j)!(k-j)!}
\end{align}
first we start with the case where $k\geq1$
\begin{align}
	O_{1}^{k} &= \left(-1\right)^{k} \sqrt{\frac{e^{-S}S^{1+k}}{k!}} \left[ \frac{k! }{k!} - \frac{k! S^{-1}}{(k-1)!} \right] \\
	&= \left(-1\right)^{k} \sqrt{\frac{e^{-S}S^{1+k}}{k!}} \left[ 1 - \frac{k}{S} \right]
\end{align}
if $k=0$ then the summation above will be unity and the same equation will thus work for both.  Now when we bring it back into the ratio:
\begin{align}
	\frac{O_{1}^{k}}{O_{0}^{k}} &= \frac{\left(-1\right)^{k} \sqrt{\frac{e^{-S}S^{1+k}}{k!}} \left[ 1 - \frac{k}{S} \right]}{\left(-1\right)^{k} \sqrt{\frac{e^{-S}S^{k }}{k!}}}\\
	&= \sqrt{S} \left[  1  - \frac{k }{S}\right]
\end{align}
Which after substitution leads to
\begin{align}
	H_{(1)}(0,k)&= \frac{S }{k+1 } \frac{\left| 1 + c\sqrt{S} \left( 1  - \frac{k+1}{S} \right) \right|^2}{\left| 1 + c \sqrt{S} \left( 1  - \frac{k}{S} \right)\right|^2 }
\end{align}

\subsubsection{Quadratic Correction}
but for a quadratic correction:
\begin{align*}
	\mu^{(2)}_{0,\lambda} =& \mu_0 \bra{0} \left( 1 + c_2\left(\hat{a} + \hat{a}^{\dagger}\right)^2\right) \ket{\lambda}\\
	=& \mu_0 \bra{0} \left( 1 + c_2\left(\hat{N} + 1 + \hat{a}\hat{a} + \hat{a}^{\dagger}\hat{a}^{\dagger}\right)\right) \ket{\lambda}\\
	=& \mu_0 \left( \left(1 + c_2\right) \delta_{0,\lambda} + c_2\sqrt{2}\delta_{2,\lambda} \right) \\
	=& \frac{\mu_0}{\left(1 + c_2\right)} \left(  \delta_{0,\lambda} + \frac{c_2\sqrt{2}}{\left(1 + c_2\right)} \delta_{2,\lambda} \right) \\
	=& \mu_0' \left(  \delta_{0,\lambda} + c_2' \delta_{2,\lambda} \right)
\end{align*}
which gives us for the peak ratios instead
\begin{align}
	H_{(2)}(0,k) &= \frac{\left| O_{0}^{k+1} + c_2' O_{2}^{k+1}  \right|^2 }{\left|O_{0}^{k} +  c_2' O_{2}^{k} \right|^2} \\
	&=  \left(\frac{O_{0}^{k+1}}{O_{0}^{k}} \right)^2   \frac{ \left|1 + c_2'\frac{O_{2}^{k+1}}{O_{0}^{k+1}}  \right|^2}{\left|1 + c_2'\frac{O_{2}^{k} }{O_{0}^{k}}\right|^2}
\end{align}

Instead of getting algebraic expressions for $O_{n\geq2}^{k}$ as we did for $O_{1}^{k}$, we instead implement Equation \ref{eqn:overlap} directly in our solver.

\subsubsection{3  Correction}
The cubic correction looks like so:
\begin{align*}
	\mu^{(3)}_{0,\lambda} =& \mu_0 \bra{0} \left( 1 + c_3\left(\hat{a} + \hat{a}^{\dagger}\right)^3\right) \ket{\lambda}\\
	=& \mu_0 \bra{0} \left( 1 + c_3 \left(\hat{a} + \hat{a}^{\dagger}\right) \left(\hat{N} + 1 + \hat{a}\hat{a} + \hat{a}^{\dagger}\hat{a}^{\dagger}\right)\right)\ket{\lambda}\\
	=& \mu_0 \left( \bra{0}  + c_3 \bra{1}  \left(\hat{N} + 1 + \hat{a}\hat{a} + \hat{a}^{\dagger}\hat{a}^{\dagger}\right)\right)\ket{\lambda}\\
	=& \mu_0 \left( \bra{0}  + c_3   \left(2\bra{1}  +\sqrt{6}\bra{3} \right)\right)\ket{\lambda}\\
	=& \mu_0 \left( \delta_{0,\lambda}  + c_3   \left(2\delta_{1,\lambda}  +\sqrt{6}\delta_{3,\lambda} \right)\right)
\end{align*}
which indicates it makes more sense to treat (1,3) and not just (3):
\begin{align*}
	\mu^{(1,3)}_{0,\lambda} =& \mu_0 \left( \delta_{0,\lambda}  +  \delta_{1,\lambda} \left(2c_3 + c_1\right)   +c_3 \sqrt{6}\delta_{3,\lambda} \right)\\
	=& \mu_0 \left( \delta_{0,\lambda}  +  c_1' \delta_{1,\lambda} +c_3 \sqrt{6}\delta_{3,\lambda} \right)\\
	c_1' =& \left(2c_3 + c_1\right)\\
	c_3' =& c_3 \sqrt{6} \\
	c_1 =& \left(c_1'  - 2\frac{c_3'}{\sqrt{6}} \right)
\end{align*}
\subsubsection{4  Correction}
Looking at a quadratic correction:
\begin{align*}
	\mu^{(4)}_{0,\lambda} =& \mu_0 \bra{0} \left( 1 + c_4\left(\hat{a} + \hat{a}^{\dagger}\right)^4\right) \ket{\lambda}\\
	=& \mu_0 \bra{0} \left( 1 + c_4 \left(\hat{N} + 1 + \hat{a}\hat{a} + \hat{a}^{\dagger}\hat{a}^{\dagger}\right)^2 \right) \ket{\lambda} \\
	=& \mu_0  \left( \delta_{0, \lambda} + c_4 \bra{0} \left(1 + \hat{a}\hat{a}\right)\left(\hat{N} + 1 + \hat{a}\hat{a} + \hat{a}^{\dagger}\hat{a}^{\dagger}\right) \ket{\lambda}\right)  \\
	=& \mu_0  \left( \delta_{0, \lambda} + c_4\left[ 3 \delta_{0, \lambda} + 4\sqrt{2}\delta_{2, \lambda}  + 2\sqrt{6}\delta_{4, \lambda}  \right]\right)  \\
	=& \mu_0  \left( \delta_{0, \lambda} \left( 1 + 3c_4 \right) + 4\sqrt{2}c_4\delta_{2, \lambda}  + 2\sqrt{6}c_4\delta_{4, \lambda}\right)\\
	=& \mu_0'  \left( \delta_{0, \lambda}  + \frac{4\sqrt{2}c_4}{1 + 3c_4 }\delta_{2, \lambda}  + \frac{2\sqrt{6}c_4}{1 + 3c_4 }\delta_{4, \lambda}\right)
\end{align*}
much like for (3) we had to consider (1,3) to be more accurate, it makes no sense to consider (4) on its own; we'll need to introduce (2) as well to get (2,4):
\begin{align*}
	\mu^{(2,4)}_{0,\lambda} =& \mu_0  \left( \delta_{0, \lambda} \left( 1 + c_2 + 3c_4 \right) + \left( \sqrt{2} c_2 + 4\sqrt{2}c_4 \right)\delta_{2, \lambda}  + 2\sqrt{6}c_4\delta_{4, \lambda}\right)\\
	=& \mu_0'  \left( \delta_{0, \lambda} + \frac{\sqrt{2} c_2 + 4\sqrt{2}c_4 }{1 + c_2 + 3c_4 }\delta_{2, \lambda}  + \frac{2\sqrt{6}c_4}{1 + c_2 + 3c_4 }\delta_{4, \lambda}\right) \\
	c_2' &= \frac{\sqrt{2} c_2 + 4\sqrt{2}c_4 }{1 + c_2 + 3c_4 } \\
	c_4' &= \frac{2\sqrt{6}c_4}{1 + c_2 + 3c_4 }
\end{align*}
which when inverted turns into:
\begin{align*}
	c_2 &= -\frac{2 \left(\sqrt{6} c_2'-2 \sqrt{2} c_4'\right)}{2 \sqrt{6} c_2'-\sqrt{2} c_4'-4 \sqrt{3}}\\
	c_4 &= \frac{c_4' \left(3 \sqrt{2} c_4'-4 \sqrt{3}\right)}{\left(2 \sqrt{6}-3 c_4'\right) \left(2 \sqrt{6} c_2'-\sqrt{2} c_4'-4 \sqrt{3}\right)}
\end{align*}

\subsubsection{Generic Correction}
It becomes clear that we should be able to define an arbitrary-order correction (at least for $\eta=0$) rather simply by adding together generic $x^n$ operator

\begin{align*}
	\mu_{0,\lambda} = \sum_n c_n \mu^{(n)}_{0,\lambda} =& \sum_n c_n \bra{0} \left(\hat{a} + \hat{a}^{\dagger}\right)^n  \ket{\lambda}
\end{align*}
this would then feed in to our expression for $H$:
\begin{align}
	H_{(\{i\})}(0,k)&= \frac{S}{k+1}   \frac{ \left|1 + \sum_{l = \{i\}} c'_l \frac{O_{l}^{k+1}}{O_{0}^{k+1}}  \right|^2}{\left|1 + \sum_{l = \{i\}} c'_l \frac{O_{l}^{k} }{O_{0}^{k}}\right|^2}
\end{align}
where the transformation from $c'$ back into $c$ would have to be calculated for any individual  $H_{(\{i\})}$ but is non-trivial.  We saw above that the squared operator brought amplitude back to zero and in general, all even operators will bring back amplitude to all even operators in order below and the same with odd operators.

% GRAVEYARD
% \begin{align}
% 	O_{2}^{k} &= \left(-1\right)^{k} \sqrt{\frac{e^{-S}S^{2+k}}{2!k!}} \sum_{j=0}^{\min \left( 2,k\right)} \frac{2!k!(-1)^j S^{-j}}{j!(2-j)!(k-j)!}
% \end{align}
% if $k>2$:
% \begin{align}
% 	O_{2}^{k} &= \left(-1\right)^{k} \sqrt{\frac{e^{-S}S^{2+k}}{2!k!}} \sum_{j=0}^{2} \frac{2!k!(-1)^j S^{-j}}{j!(2-j)!(k-j)!} \\
% 	&= \left(-1\right)^{k} \sqrt{\frac{e^{-S}S^{2+k}}{2!k!}} \left(1 - \frac{2 k}{S} + \frac{k(k-1)}{S^2 }  \right)
% \end{align}
% then one can see like we did above that this holds for $k=0,1$ and we can continue on to the ratios:
% \begin{align}
% 	\frac{O_{2}^{k}}{O_{0}^{k}} &= \frac{\left(-1\right)^{k} \sqrt{\frac{e^{-S}S^{2+k}}{2!k!}} \left(1 - \frac{2 k}{S} + \frac{k(k-1)}{S^2 }  \right)}{\left(-1\right)^{k} \sqrt{\frac{e^{-S}S^{k }}{k!}}}\\
% 	&= \sqrt{\frac{S^{2}}{2}} \left(1 - \frac{2 k}{S} + \frac{k(k-1)}{S^2 }  \right)\\
% 	\frac{O_{2}^{k+1}}{O_{0}^{k+1}} &= \sqrt{\frac{S^{2}}{2}} \left(1 - \frac{2 (k+1)}{S} + \frac{k(k+1)}{S^2 }  \right)
% \end{align}