\section{Fitting Non-XAFS Data with {\ifeffit}} \label{Ch:Minimize}

{\index{Minimization}}
Well, fitting XAFS data to {\feff} calculations is OK, but sometimes you
just want to fit a simple line, polynomial, or Gaussian to some data.  This
can be done using the {\texttt{minimize}} command, which gives a simple but
powerful interface to a non-linear least-squares fitting routine.  There
are a few implementation quirks, but this general approach is definitely
sufficient to fit simple functions to data, and to add a set of known XANES
spectra to fit an unknown spectrum.  This chapter will describe the
{\texttt{minimize}} command and give a few examples of its use.
{\indexcmd{minimize}}

When fitting data, the general idea is to minimize some function in the
least-squares sense. Usually the function to minimize is the difference
between the data and a parameterized model describing the data.  This
function to be minimized is sometimes called the {\emph{residual}} of the
fit.  In keeping with that spirit, the {\tt{minimize}} function in
{\ifeffit} takes a residual vector that you define, and adjusts the defined
variables until this residual is minimized.  An example complete would look
like this:

{\small{
%\begin{latexonly}
 \begin{Sbox}\begin{minipage}{5.00truein}
%\end{latexonly}
\begin{Verbatim}
 read_data(file=my.dat, group= data, label= "x y")
 guess (a0 = 1, a1 = 2, a2 = 0.012)
 fit.y     = a0 + a1 * data.x + a2 * data.x^2
 fit.resid = fit.y - data.y
 minimize(fit.resid)
\end{Verbatim}
%\begin{latexonly}
 \end{minipage}\end{Sbox}\setlength{\fboxsep}{2mm}{%
 \begin{flushright}\shadowbox{\TheSbox}\end{flushright}}
%\begin{latexonly}
}}\noindent
Here we read in the data, rename the default array names to more convenient
names.  The model function {\tt{fit.y}} is defined as a simple quadratic
polynomial, with the three coefficients defined as variables.  The residual
is then simply the difference of model and data, and the variables are
optimized to minimize the sum of the squares of {\texttt{fit.resid}}.
Really, that's pretty much all there is to it.  {\tt{minimize}} is
remarkably simple and powerful.

There are a few bells and whistles to the {\tt{minimize}} command.
Sometimes you'll want to fit a limited portion of an array, say just over
some peak.  Of course, you could edit the data to only include the portion
of the data you want to fit.  But {\tt{minimize}} gives an alternative to
this: you can specify the {\emph{ordinate}} (or $x$-array) corresponding to
the data you're fitting, and a minimum and/or maximum values for the
$x$-array.  In the above example, we could have said

\begin{verbatim}
  minimize(fit.resid, x = data.x, xmin = 3., xmax=10.)
\end{verbatim}
\noindent
to limit the fitting range.

A fit is generally of limited use without some idea of the uncertainties in
the fitted parameters.  Many otherwise bright people seem to ignore this,
and believe they can judge the reliability of fitted parameters by the
overall quality of a fit --usually by some visual inspection.  Well, a
reliable estimate of uncertainties in fitted parameters is a bit more
involved than that.  In general, it's difficult to get a reasonable
estimate without a good estimate of the uncertainties in the data itself.
This is further discussed in {\XAIBook} and in standard data analysis
texts.  For now, the important point is that if you have a good estimate of
the uncertainties in the data, you can use them to determine the
uncertainties in the fitted parameters.  To do this, you can specify an
{\emph{array}} of uncertainties in the data -- the same length as the data
and residual itself, of course.  To use such an array, the
{\tt{uncertainty}} keyword will be helpful:
\begin{verbatim}
  read_data(file=my.dat, group= data, label= 'x y dy')
  guess (a0 = 1, a1 = 2, a2 = 0.012)
  fit.y     = a0 + a1 * data.x + a2 * data.x^2
  fit.resid = fit.y - data.y

  minimize(fit.resid, uncertainty = data.dy)
\end{verbatim}
\noindent
As with the {\tt{feffit}} function, this will create scalars for the
estimated uncertainties in the fitted variables with names based on the
variable name itself.  In this case, the created variables
{\tt{delta\_a0}}, {\tt{delta\_a1}}, and {\tt{delta\_a2}} will hold the
estimated uncertainties.

At this writing, restraints and multiple-data-set fits are not supported in
{\cmnd{minimize}}.   This will probably change in the future.


\subsection{Power Expression}

\begin{grammar}
\underbrace{\nonterminal{power-expression}}_{=:P}
    \produces
	\underbrace{\nonterminal{unary-expression}}_{=:U} \\
    \produces
	\nonterminal{unary-expression}
	\lexkeyword{\textasciicircum}
	\nonterminal{power-expression} \\
\end{grammar}

\paragraph{Item automata for P}
\[
\begin{tikzpicture}[
    every text node part/.style={align=center},
    initial text =
]
    \node[state,initial]
	(S)[rounded rectangle, draw]
	{
	    {$[P\to .U\,\hat{}\, P]$} \\
	    {$[P\to .U]$}
	};
    \node[state,accepting]
	(U)[rounded rectangle, draw,right=of S]
	{
	    {$[P\to U.\,\hat{}\, P]$} \\
	    {$[P\to U.]$}
	};
    \node[state]
	(Hat)[rounded rectangle, draw,right=of U]
	{
	    {$[P\to U\,\hat{}\, .P]$}
	};
    \node[state,accepting]
	(P)[rounded rectangle, draw,right=of Hat]
	{
	    {$[P\to U\,\hat{}\, P.]$}
	};
    \path[->] (S) edge  node [above] {U} (U);
    \path[->] (U) edge  node [above] {\lexkeyword{\textasciicircum}} (Hat);
    \path[->] (Hat) edge  node [above] {P} (P);
\end{tikzpicture}
\]


\documentclass[a4paper,11pt,twoside,titlepage,openright]{book}

\usepackage[english]{babel}
\usepackage{color}
\usepackage{graphicx}
\usepackage{amsmath}
\numberwithin{equation}{section}
\usepackage[margin=3cm]{geometry}
\usepackage{hyperref}
\usepackage{epsfig,amsfonts}
\usepackage{xcolor,import}

\pagestyle{plain}

\newcommand{\ud}[1]{\underline{#1}}
\newcommand{\e}[1]{\underline{e}_{#1}}
\newcommand{\lt}{\left}
\newcommand{\rt}{\right}
\DeclareMathOperator{\n}{\underline{n}}
\DeclareMathOperator{\ei}{\underline{e}_1}
\DeclareMathOperator{\et}{\underline{e}_2}
\DeclareMathOperator{\ex}{\underline{e}_x}
\DeclareMathOperator{\ey}{\underline{e}_y}
\DeclareMathOperator{\ez}{\underline{e}_z}
\DeclareMathOperator{\nin}{\underline{n}_{in}}
\DeclareMathOperator{\nout}{\underline{n}_{out}}
\DeclareMathOperator{\np}{\underline{n}_{P}}
\DeclareMathOperator{\bragg}{\theta_{bragg}}
\DeclareMathOperator{\DD}{\cos(\theta)^2 - \sin(\psi)^2}
\newcommand{\wdg}{\wedge}
\newcommand{\hypot}[1]{\textbf{\textcolor{green}{#1}}}

\begin{document}

\title{ToFu geometric tools\\ Intersection of a cone with a plane}
\author{Didier VEZINET}
\date{15.10.2019}
\maketitle

\tableofcontents

\chapter{Geometry}
\label{chap:Geometry}

\section{Generic cone and plane}

Let's consider a half-cone $C_1$ (defined only for $z > 0$), with summit on the cartesian frame's origin $(O, \ex, \ey, \ez)$.
The cone's axis is the $(O,\ez)$ axis.
It's angular half-opening is $\theta$.

Let's consider plane $P_1$, of normal $\n$, intersection axis $(O,\ez)$ at point $P$ of coordinates $(0,0,Z_P)$.
Vector $\n$ is oriented by angles $\phi$ and $\psi$ such that one can define the local frame $(P, \ei, \et, \n)$:
$$
\lt\{
	\begin{array}{ll}
		\ei & = \cos(\phi)\ex + \sin(\phi)\ey\\
		\et & = \lt(-\sin(\phi)\ex + \cos(\phi)\ey\rt)\cos(\psi) + \sin(\psi)\ez\\
		\n & = \ei \wdg \et\\
		   & = \lt( \sin(\phi)\ex - \cos(\phi)\ey \rt)\sin(\psi) + \cos(\psi)\ez
	\end{array}
\rt.
$$

We want to find all points $M$ of coordinates $(x, y, z)$ and $(x_1, x_2)$ belonging both to the cone $C_1$ and the plane $P_1$.

$$
M \in C_1 \Leftrightarrow \underline{OM}.\ez = \cos(\theta) \|\underline{OM}\|
$$

$$
M \in P_1 \Leftrightarrow \underline{PM}.\n = 0
$$

\section{Intersection}

If $M$ belongs to both $P_1$ and $C_1$, then:
$$
(\underline{OM}.\ez)^2 = \cos(\theta)^2 \|\underline{OM}\|^2
$$

Given that:
$$
\begin{array}{lll}
	\underline{OM} & = \underline{OP} + \underline{PM}\\
		       & = Z_P\ez + x_1\ei + x_2\et\\
		       & = Z_P\ez + x_1\lt(\cos(\phi)\ex + \sin(\phi)\ey\rt) + x_2\lt(\lt(-\sin(\phi)\ex + \cos(\phi)\ey\rt)\cos(\psi) + \sin(\psi)\ez\rt)\\
		       & = Z_P\ez + x_1\cos(\phi)\ex + x_1\sin(\phi)\ey - x_2\sin(\phi)\cos(\psi)\ex + x_2\cos(\phi)\cos(\psi)\ey + x_2\sin(\psi)\ez\\
	               & = \lt(x_1\cos(\phi) - x_2\sin(\phi)\cos(\psi)\rt)\ex + \lt(x_1\sin(\phi) + x_2\cos(\phi)\cos(\psi)\rt)\ey + \lt(Z_P + x_2\sin(\psi)\rt)\ez\\
\end{array}
$$

We have:
$$
\begin{array}{lll}
	(\underline{OM}.\ez)^2  & = \lt(Z_P + x_2\sin(\psi)\rt)^2\\
				& = Z_P^2 + 2Z_Px_2\sin(\psi) + x_2^2\sin(\psi)^2
\end{array}
$$

And:
$$
\begin{array}{lll}
	\|\underline{OM}\|^2 	& = & \| \lt(x_1\cos(\phi) - x_2\sin(\phi)\cos(\psi)\rt)\ex + \lt(x_1\sin(\phi) + x_2\cos(\phi)\cos(\psi)\rt)\ey + \lt(Z_P + x_2\sin(\psi)\rt)\ez\|^2\\
				& = & \lt( x_1\cos(\phi) - x_2\sin(\phi)\cos(\psi) \rt)^2\\
				&   & + \lt(x_1\sin(\phi) + x_2\cos(\phi)\cos(\psi)\rt)^2\\
				&   & + \lt(Z_P + x_2\sin(\psi)\rt)^2\\
				& = & x_1^2\cos(\phi)^2 - 2x_1x_2\cos(\phi)\sin(\phi)\cos(\psi) + x_2^2\sin(\phi)^2\cos(\psi)^2\\
				&   & + x_1^2\sin(\phi)^2 + 2x_1x_2\sin(\phi)\cos(\phi)\cos(\psi) + x_2^2\cos(\phi)^2\cos(\psi)^2\\
				&   & + Z_P^2  + 2Z_Px_2\sin(\psi) + x_2^2\sin(\psi)^2\\
				& = & x_1^2 + x_2^2\cos(\psi)^2\\
				&   & + Z_P^2  + 2Z_Px_2\sin(\psi) + x_2^2\sin(\psi)^2\\
				& = & x_1^2 + x_2^2 + 2Z_Px_2\sin(\psi) + Z_P^2\\
\end{array}
$$

Thus:
$$
\begin{array}{lll}
	& (\underline{OM}.\ez)^2 = \cos(\theta)^2 \|\underline{OM}\|^2\\
	\Leftrightarrow & Z_P^2 + 2Z_Px_2\sin(\psi) + x_2^2\sin(\psi)^2 = \cos(\theta)^2 \lt(x_1^2 + x_2^2 + 2Z_Px_2\sin(\psi) + Z_P^2\rt)\\
	\Leftrightarrow & Z_P^2\lt(1-\cos(\theta)^2\rt) + 2Z_Px_2\sin(\psi)\lt(1-\cos(\theta)^2\rt) = x_1^2\cos(\theta)^2 + x_2^2\lt(\cos(\theta)^2 - \sin(\psi)^2\rt)\\
	\Leftrightarrow & Z_P^2\sin(\theta)^2 + 2Z_Px_2\sin(\psi)\sin(\theta)^2 = x_1^2\cos(\theta)^2 + x_2^2\lt(\cos(\theta)^2 - \sin(\psi)^2\rt)\\
\end{array}
$$

Considering that by hypothesis $\theta>0$:
$$
\begin{array}{lll}
	& (\underline{OM}.\ez)^2 = \cos(\theta)^2 \|\underline{OM}\|^2\\
	\Leftrightarrow & x_1^2\cos(\theta)^2 + x_2^2\lt(\DD\rt) - 2Z_Px_2\sin(\psi)\sin(\theta)^2 - Z_P^2\sin(\theta)^2 = 0\\
	\Leftrightarrow & x_1^2\frac{\cos(\theta)^2}{\DD} + x_2^2 - 2x_2Z_P\frac{\sin(\psi)\sin(\theta)^2}{\DD} - Z_P^2\frac{\sin(\theta)^2}{\DD} = 0\\
	\Leftrightarrow & x_1^2\frac{\cos(\theta)^2}{\DD} + \lt(x_2 - Z_P\frac{\sin(\psi)\sin(\theta)^2}{\DD}\rt)^2 - Z_P^2\frac{\sin(\psi)^2\sin(\theta)^4}{\lt(\DD\rt)^2} - Z_P^2\frac{\sin(\theta)^2}{\DD} = 0\\
	\Leftrightarrow & x_1^2\frac{\cos(\theta)^2}{\DD} + \lt(x_2 - Z_P\frac{\sin(\psi)\sin(\theta)^2}{\DD}\rt)^2 = Z_P^2 \frac{\sin(\theta)^2}{\lt(\DD\rt)^2} \lt(\sin(\psi)^2\sin(\theta)^2 + \DD \rt)\\
	\Leftrightarrow & x_1^2\frac{\cos(\theta)^2}{\DD} + \lt(x_2 - Z_P\frac{\sin(\psi)\sin(\theta)^2}{\DD}\rt)^2 = Z_P^2 \frac{\sin(\theta)^2}{\lt(\DD\rt)^2} \lt(-\sin(\psi)^2\cos(\theta)^2 + \cos(\theta)^2 \rt)\\
	\Leftrightarrow & x_1^2\frac{\cos(\theta)^2}{\DD} + \lt(x_2 - Z_P\frac{\sin(\psi)\sin(\theta)^2}{\DD}\rt)^2 = Z_P^2 \frac{\sin(\theta)^2\cos(\psi)^2\cos(\theta)^2}{\lt(\DD\rt)^2} \\
	\Leftrightarrow & \frac{x_1^2}{ Z_P^2\frac{\sin(\theta)^2\cos(\psi)^2}{\DD} } + \frac{\lt(x_2-Z_P\frac{\sin(\psi)\sin(\theta)^2}{\DD}\rt)^2}{ Z_P^2\frac{\sin(\theta)^2\cos(\psi)^2\cos(\theta)^2}{\lt(\DD\rt)^2} } = 1
\end{array}
$$

Or, in a reduced conic form:

$$
\frac{x_1^2}{ A } + \frac{\lt(x_2-x_2(C)\rt)^2}{b^2} = 1
$$

With:
$$
\lt\{
	\begin{array}{lll}
		x_2(C) & = Z_P\frac{\sin(\psi)\sin(\theta)^2}{\DD} & \text{    $x_2$ coordinate of the center} \\
		A & = Z_P^2\frac{\sin(\theta)^2\cos(\psi)^2}{\DD} & \text{  $\pm$ squared minor radius} \\
		b^2 & = Z_P^2\frac{\sin(\theta)^2\cos(\psi)^2\cos(\theta)^2}{\lt(\DD\rt)^2} & \text{    squared major radius} \\
		b^2 & = A\frac{\cos(\theta)^2}{\DD} \Leftrightarrow A = b^2\lt(1-\frac{\sin(\psi)^2}{\cos(\theta)^2}\rt)
	\end{array}
\rt.
$$

The distance $d_{CF}$ between the center $C$ and the focal point $F$ can be deduced from:
$$
\begin{array}{lll}
	d_{CF}^2 & = b^2-A\\
		& = b^2\frac{\sin(\psi)^2}{\cos(\theta)^2}\\
		& = Z_P^2\frac{\sin(\theta)^2\cos(\psi)^2\sin(\psi)^2}{\lt(\DD\rt)^2}
\end{array}
$$

Hence, the $x_2$ coordinate of $F$ is:
$$
\begin{array}{lll}
	x_2(F) & = x_2(C) \pm d_{CF}\\
	       & = Z_P\frac{\sin(\psi)\sin(\theta)^2}{\DD} \pm Z_P\frac{\sin(\theta)\cos(\psi)\sin(\psi)}{\DD}\\
	       & = Z_P\frac{\sin(\psi)\sin(\theta)^2 \pm \sin(\theta)\cos(\psi)\sin(\psi)}{\DD}\\
	       & = Z_P\frac{\sin(\psi)\sin(\theta)}{\DD}\lt(\sin(\theta) \pm \cos(\psi)\rt)\\
\end{array}
$$

It is worth noticing that the neither the focal point nor the center correspond to the intersection between the axes and the plane $P$.

\section{Nature of the Conic}

We have found the following general form of the intersection:
$$
\frac{x_1^2}{ A } + \frac{\lt(x_2-x_2(C)\rt)^2}{b^2} = 1
$$

With:
$$
\lt\{
	\begin{array}{lll}
		x_2(C) & = Z_P\frac{\sin(\psi)\sin(\theta)^2}{\DD}\\
		b^2 & = Z_P^2\frac{\sin(\theta)^2\cos(\psi)^2\cos(\theta)^2}{\lt(\DD\rt)^2}\\
		A = b^2\lt(1-\frac{\sin(\psi)^2}{\cos(\theta)^2}\rt)
	\end{array}
\rt.
$$

The conic can an hyperbola, a parabola, an ellipse or a circle depending on the
sign and value of the ratio $A/b^2$:
$$
\begin{array}{lll}
    & \frac{A}{b^2} \geq 0\\
    \Leftrightarrow & 1-\frac{\sin(\psi)^2}{\cos(\theta)^2} \geq 0\\
    \Leftrightarrow & \sin(\psi)^2 \leq \cos(\theta)^2\\
\end{array}
$$

Now, if we consider that the plane is not arbitrary, but corresponds to a
detector placed tangentially to the Rowland radius for a wavelength whose bragg
angle is the cone opening $\theta$.

In such conditions, it can be shown that the distance between the cone summit
$O$ and the detector center is $R_C\sin(\theta_{bragg})$, where $R_C$ is the curvature
radius of the crystal, and that the unit vector perpendicular to the plane
representing the detector surface is:
$$
\begin{array}{lll}
    \underline{n}
    & = -\cos(2\theta_{bragg})\underline{n_{cr}} - \sin(2\theta_{bragg})\underline{e_{1,cr}}\\
\end{array}
$$

Where $\underline{n_{cr}} = -\underline{e_{z}}$ is the normal to the crystal and
$\underline{e_{1,cr}}$ is the unit vector parallel to $(O, \underline{e_{x}},
\underline{e_{y}})$.

Also, the bragg angle $\theta_{bragg}$ is defined from the tangential
direction, while we defined the cone half-opening angle $\theta$ from the
cone's axis, so they are complementary:
$$
\theta_{bragg} = \frac{\pi}{2} - \theta
$$

In these conditions we have:
$$
\begin{array}{lll}
    & \underline{n}\cdot\underline{e_{z}} = \cos(\psi)\\
    \Leftrightarrow & \cos(2\theta_{bragg}) = \cos(\psi)\\
    \Leftrightarrow & \cos(\pi - 2\theta) = \cos(\psi)\\
\end{array}
$$

If the cosines are equal, the cosines squared too, so the sines squared too:
$$
\sin^2(\pi - 2\theta) = \sin^2(\psi)
$$

Hence the condition on the nature of the conic can now be rewritten:
$$
\begin{array}{lll}
    & \frac{A}{b^2} \geq 0\\
    \Leftrightarrow & \sin(\psi)^2 \leq \cos(\theta)^2\\
    \Leftrightarrow & \sin^2(\pi - 2\theta) \leq \cos(\theta)^2\\
    \Leftrightarrow & \sin^2(2\theta_{bragg}) \leq \cos(\frac{\pi}{2} -
    \theta_{bragg})^2\\
    \Leftrightarrow & \sin^2(2\theta_{bragg}) \leq \sin(\theta_{bragg})^2\\
\end{array}
$$

There is only one intersection to this inequality, for $\theta_{bragg} \approx
60$ degrees.

\begin{figure}
\begin{center}
    \includegraphics[width=0.6\linewidth]{Criterion_EllipseHyperbola.png}
    \caption{Criterion for the nature of the conic, for a tangential detector}
    \label{fig:boat1}
\end{center}
\end{figure}

Hence, for a tangential detector:
$$
\lt\{
	\begin{array}{lll}
        \theta_{bragg} < 60 \text{ degrees} \Rightarrow \text{ Hyperbola}\\
        \theta_{bragg} = 60 \text{ degrees} \Rightarrow \text{ Parabola}\\
        \theta_{bragg} > 60 \text{ degrees} \Rightarrow \text{ Ellipse}\\
	\end{array}
\rt.
$$

A similar computation can be done for a detector also on the rowland circle but
oriented in a direction perpendicular to the direction of the crystal, in this
case the results are:
$$
\lt\{
	\begin{array}{lll}
        \theta_{bragg} < 45 \text{ degrees} \Rightarrow \text{ Hyperbola}\\
        \theta_{bragg} = 45 \text{ degrees} \Rightarrow \text{ Parabola}\\
        \theta_{bragg} > 45 \text{ degrees} \Rightarrow \text{ Ellipse}\\
	\end{array}
\rt.
$$

\section{Parametric equation}

In our case, only the axes $(O, \ez)$, fixed by the crystal's summit and normal, is independent from the cone's angular opening $\theta$.
It makes sense to design an ad-hoc coordinate system centered on the ellipse's center $C$ to use its parameterized equation.

Knowing all geometrical parameters, it is possible to compute all points on the ellipse parameterizing them with $t$:
$$
\lt\{
	\begin{array}{lll}
		x_1 = a\cos(t)\\
		x_2 = x_2(C) + b\sin(t)
	\end{array}
\rt.
$$

\emph{BEWARE} : parameter $t$ is not the angle of the point with respect to the ellipse's center.

Now, we would like to parameterize the ellipse not with $t$ but with the angle $\beta$ with respect to the point $P$, because it is the physically relevant angle since it is taken with respect to the axis $(O, \ez)$ and relates to the impact point of the photon beam on the crystal's center. Also, it is the only common element to all ellipses.
The angle $\epsilon$ taken with respect to the center is not relevant because each ellipse has a different center.

In this perspective:
$$
\lt\{
	\begin{array}{lll}
		x_1 = l(\beta)\cos(\beta)\\
		x_2 = l(\beta)\sin(\beta)
	\end{array}
\rt.
$$

Keeping in mind that the ellipse is defined as:
$$
\frac{x_1^2}{a^2} + \frac{(x_2-x_2(C))^2}{b^2} = 1
$$

We can write:
$$
\begin{array}{lll}
	& l^2b^2\cos(\beta)^2 + a^2\lt(l^2\sin(\beta)^2 - 2lx_2(C)\sin(\beta) + x_2(C)^2\rt) = a^2b^2\\
	\Leftrightarrow
	& l^2\lt(b^2\cos(\beta)^2 + a^2\sin(\beta)^2\rt) - 2la^2x_2(C)\sin(\beta) + a^2x_2(C)^2 - a^2b^2 = 0
\end{array}
$$

Has solutions if:
$$
\begin{array}{lll}
	& \Delta = 4a^4x_2(C)^2\sin(\beta)^2 - 4\lt(b^2\cos(\beta)^2 + a^2\sin(\beta)^2\rt)\lt(a^2x_2(C)^2 - a^2b^2\rt) \geq 0\\
	\Leftrightarrow
	& \Delta = 4a^2\lt[a^2x_2(C)^2\sin(\beta)^2 - \lt(b^2\cos(\beta)^2 + a^2\sin(\beta)^2\rt)\lt(x_2(C)^2 - b^2\rt)\rt] \geq 0\\
	\Leftrightarrow
	& \Delta = 4a^2\lt[a^2x_2(C)^2\sin(\beta)^2 - b^2x_2(C)^2\cos(\beta)^2 - a^2x_2(C)^2\sin(\beta)^2 + b^4\cos(\beta)^2 + a^2b^2\sin(\beta)^2\rt] \geq 0\\
	\Leftrightarrow
	& \Delta = 4a^2\lt[-b^2x_2(C)^2\cos(\beta)^2 + b^4\cos(\beta)^2 + a^2b^2\sin(\beta)^2\rt] \geq 0\\
	\Leftrightarrow
	& \Delta = 4a^2b^2\lt[-x_2(C)^2\cos(\beta)^2 + b^2\cos(\beta)^2 + a^2\sin(\beta)^2\rt] \geq 0\\
\end{array}
$$

Which is equivalent to, keeping in mind that $b^2-a^2 = d_{CF}^2$:
$$
\begin{array}{lll}
	& \Delta = 4a^2b^2\lt[a^2 + \lt(b^2-a^2-x_2(C)^2\rt)\cos(\beta)^2\rt] \geq 0\\
	\Leftrightarrow
	& \lt(b^2-a^2-x_2(C)^2\rt)\cos(\beta)^2 \geq -a^2
	\Leftrightarrow
	& \lt(d_{CF}^2-x_2(C)^2\rt)\cos(\beta)^2 \geq -a^2
\end{array}
$$

If $d_{CF}^2-x_2(C)^2 \ geq 0$, this is true for all $\beta$ values, and this condition is met if:
$$
\begin{array}{lll}
	& b^2-a^2-x_2(C)^2 \geq 0\\
	\Leftrightarrow
	& \frac{Z_P^2}{\lt(\DD\rt)^2}\lt(\sin(\theta)^2\cos(\psi)^2\cos(\theta)^2 - \sin(\theta)^2\cos(\psi)^2\cos(\theta)^2 + \sin(\theta)^2\cos(\psi)^2\sin(\psi)^2 - \sin(\psi)^2\sin(\theta)^4\rt) \geq 0\\
	\Leftrightarrow
	& \sin(\theta)^2\sin(\psi)^2\lt(\cos(\psi)^2 -\sin(\theta)^2\rt) \geq 0
\end{array}
$$

Which is true if we have an ellipse, which is the only case of interest.
Hence $\Delta=4a^2b^2\lt[a^2 + \lt(b^2-a^2-x_2(C)^2\rt)\cos(\beta)^2\rt] \geq 0$, so:
$$
\begin{array}{lll}
	& l_{1,2} = \frac{2a^2x_2(C)\sin(\beta) \pm \sqrt{\Delta}}{2\lt(b^2\cos(\beta)^2 + a^2\sin(\beta)^2\rt)}\\
	\Leftrightarrow
	& l_{1,2} = \frac{a^2x_2(C)\sin(\beta) \pm ab\sqrt{a^2 + \lt(b^2-a^2-x_2(C)^2\rt)\cos(\beta)^2}}{b^2\cos(\beta)^2 + a^2\sin(\beta)^2}\\
\end{array}
$$

And we only want the positive solution:
$$
l = \frac{a^2x_2(C)\sin(\beta) + ab\sqrt{a^2 + \lt(b^2-a^2-x_2(C)^2\rt)\cos(\beta)^2}}{b^2\cos(\beta)^2 + a^2\sin(\beta)^2}
$$

\subsection{From bragg angle and parameter to local cartesian coordinates}

Keep in mind that the frame $(P, \ei, \et)$ is, by definition aligned on the minor and major axes of the ellipse.
Hence, for an arbitrary frame $(R, \ud{e}_i, \ud{e}_j)$ on plane $P_1$, translated and rotated by $\alpha$ with respect to $(P, \ei, \et)$:

$$
\lt\{
	\begin{array}{lll}
		\ud{e}_i = \cos(\alpha)\ei + \sin(\alpha)\et\\
		\ud{e}_j = -\sin(\alpha)\ei + \cos(\alpha)\et\\
		\ei = \cos(\alpha)\ud{e_i} - \sin(\alpha)\ud{e}_j
		\et = \sin(\alpha)\ud{e_i} + \cos(\alpha)\ud{e}_j
	\end{array}
\rt.
$$

Or, in coordinate tranforms:
$$
\lt\{
	\begin{array}{lll}
		x_1 = x_1(R) + x_i\cos(\alpha) - x_j\sin(\alpha)\\
		x_2 = x_2(R) + x_i\sin(\alpha) + x_j\cos(\alpha)\\
		x_i = (x_1-x_1(R))\cos(\alpha) + (x_2-x_2(R))\sin(\alpha)\\
		x_j = -(x_1-x_1(R))\sin(\alpha) + (x_2-x_2(R))\cos(\alpha)
	\end{array}
\rt.
$$

Hence:
$$
\lt\{
	\begin{array}{lll}
		x_i = \lt(a\cos(\epsilon)-x_1(R)\rt)\cos(\alpha) + \lt(x_2(C)-x_2(R) + b\sin(\epsilon)\rt)\sin(\alpha)\\
		x_j = -\lt(a\cos(\epsilon)-x_1(R)\rt)\sin(\alpha) + \lt(x_2(C)-x_2(R) + b\sin(\epsilon)\rt)\cos(\alpha)
	\end{array}
\rt.
$$

But
$$
\lt\{
	\begin{array}{lll}
		\|\ud{PM}\|^2 = x_1^2 + x_2^2 = \\
		x_1 = \|\ud{PM}\|\cos(\beta)\\
		x_2 = \|\ud{PM}\|\sin(\beta)
	\end{array}
\rt.
$$

\subsection{From local cartesian coordinates to bragg angle}

Knowing $(x_i, x_j)$ and all geometric parameters, we now want to derive $(\theta, \epsilon)$.

From the previous equation, we can write:
$$
\lt\{
	\begin{array}{lll}
        x_i\cos(\alpha) - x_j\sin(\alpha) = a\cos(\epsilon)-x_1(R) & (1)\\
        x_i\sin(\alpha) + x_j\cos(\alpha) = x_2(C)-x_2(R) + b\sin(\epsilon) &
        (2)
	\end{array}
\rt.
$$

The dependency in $\theta$ is hidden in the expressions of $a$, $b$ and
$x_2(C)$.

By squaring and summing, it is possible to get rid of the $\epsilon$
dependency:
$$
\lt\{
	\begin{array}{lll}
        a^2\cos(\epsilon)^2 = \lt(x_i\cos(\alpha) - x_j\sin(\alpha) +
        x_1(R)\rt)^2\\
        b^2\sin(\epsilon)^2 = \lt(x_i\sin(\alpha) + x_j\cos(\alpha) - x_2(C) +
        x_2(R)\rt)^2
	\end{array}
\rt.
$$

Hence, keeping in mind that $a^2 = b^2\frac{\DD}{\cos(\theta)^2}$ and re-using the definitions of $x_1$ and $x_2$ which do not depend on the unknowns $(\theta, \epsilon)$:
$$
\begin{array}{lllll}
	& b^2 & = & \frac{\cos(\theta)^2}{\DD}x_1^2 + \lt(x_2 - x_2(C)\rt)^2\\
    \Leftrightarrow
    & b^2\lt(\DD\rt) & = & \cos(\theta)^2x_1^2 + \lt(\DD\rt)\lt(x_2^2 - 2x_2x_2(C) + x_2(C)^2\rt)\\
    \Leftrightarrow
    &  Z_P^2\frac{\sin(\theta)^2\cos(\psi)^2\cos(\theta)^2}{\DD} & = & \cos(\theta)^2x_1^2 + \lt(\DD\rt)\lt(x_2^2 - 2x_2x_2(C) + x_2(C)^2\rt)\\
    \Leftrightarrow
    &  Z_P^2\frac{\sin(\theta)^2\cos(\psi)^2\cos(\theta)^2}{\DD} & = & \cos(\theta)^2\lt(x_1^2+x_2^2\rt) - \sin(\psi)^2x_2^2 - \lt(\DD\rt)\lt(2x_2x_2(C) - x_2(C)^2\rt)\\
    \Leftrightarrow
    &  Z_P^2\sin(\theta)^2\cos(\psi)^2\cos(\theta)^2 & = & \cos(\theta)^2\lt(\DD\rt)\lt(x_1^2+x_2^2\rt) - \sin(\psi)^2\lt(\DD\rt)x_2^2\\
    \Leftrightarrow
    & & & - \lt(\DD\rt)^2\lt(2x_2x_2(C) - x_2(C)^2\rt)\\
    \Leftrightarrow
    &  Z_P^2\sin(\theta)^2\cos(\psi)^2\cos(\theta)^2 & = &\cos(\theta)^4\lt(x_1^2+x_2^2\rt) - \cos(\theta)^2\sin(\psi)^2\lt(x_1^2+2x_2^2\rt) + \sin(\psi)^4x_2^2\\
    & & & - \lt(\DD\rt)^2\lt(2x_2x_2(C) - x_2(C)^2\rt)\\
    \Leftrightarrow
    &  Z_P^2\sin(\theta)^2\cos(\psi)^2\cos(\theta)^2 & = & \cos(\theta)^4\lt(x_1^2+x_2^2\rt) - \cos(\theta)^2\sin(\psi)^2\lt(x_1^2+2x_2^2\rt) + \sin(\psi)^4x_2^2\\
    & & & - \lt[2x_2Z_P\sin(\psi)\sin(\theta)^2\lt(\DD\rt) - Z_P^2\sin(\psi)^2\sin(\theta)^4\rt]\\
    \Leftrightarrow
    &  Z_P^2\cos(\psi)^2\lt(\cos(\theta)^2-\cos(\theta)^4\rt) & = &\cos(\theta)^4\lt(x_1^2+x_2^2\rt) - \cos(\theta)^2\sin(\psi)^2\lt(x_1^2+2x_2^2\rt) + \sin(\psi)^4x_2^2\\
    & & & - 2x_2Z_P\sin(\psi)\sin(\theta)^2\lt(\DD\rt)\\
    & & & + Z_P^2\sin(\psi)^2\lt(1-2\cos(\theta)^2+\cos(\theta)^4\rt)\\
    \Leftrightarrow
    &  Z_P^2\cos(\psi)^2\lt(\cos(\theta)^2-\cos(\theta)^4\rt) & = &\cos(\theta)^4\lt(x_1^2+x_2^2\rt) - \cos(\theta)^2\sin(\psi)^2\lt(x_1^2+2x_2^2\rt) + \sin(\psi)^4x_2^2\\
    & & & - 2x_2Z_P\sin(\psi)\lt(\cos(\theta)^2 - \cos(\theta)^4 - \sin(\psi)^2\lt(1-\cos(\theta)^2\rt)\rt)\\
    & & & + Z_P^2\sin(\psi)^2\lt(1-2\cos(\theta)^2+\cos(\theta)^4\rt)
\end{array}
$$

Then introducing $X = \cos(\theta)^2$:
$$
\begin{array}{lllll}
	& b^2 & = & \frac{\cos(\theta)^2}{\DD}x_1^2 + \lt(x_2 - x_2(C)\rt)^2\\
    \Leftrightarrow
    &  Z_P^2\cos(\psi)^2\lt(X-X^2\rt) & = & X^2\lt(x_1^2+x_2^2\rt) - X\sin(\psi)^2\lt(x_1^2+2x_2^2\rt) + \sin(\psi)^4x_2^2\\
    & & & - 2x_2Z_P\sin(\psi)\lt(X - X^2 - \sin(\psi)^2 + X\sin(\psi)^2\rt)\\
    & & & + Z_P^2\sin(\psi)^2\lt(1 - 2X + X^2\rt)
\end{array}
$$

Which boils down to:
$$
\begin{array}{lllll}
    & 0 & = & X^2\lt[ x_1^2+x_2^2 + 2x_2Z_P\sin(\psi) + Z_P^2\sin(\psi)^2 + Z_P^2\cos(\psi)^2 \rt]\\
    & & & + X\lt[ -\sin(\psi)^2\lt(x_1^2+2x_2^2\rt) - \lt(1+\sin(\psi)^2\rt)2x_2Z_P\sin(\psi) - 2Z_P^2\sin(\psi)^2 - Z_P^2\cos(\psi)^2\rt]\\
    & & & + \sin(\psi)^4x_2^2 + 2x_2Z_P\sin(\psi)^3 + Z_P^2\sin(\psi)^2\\
    \Leftrightarrow
    & 0 & = & X^2\lt[ x_1^2 + \lt(x_2 + Z_P\sin(\psi)\rt)^2 + Z_P^2\cos(\psi)^2 \rt]\\
    & & & + X\lt[ -\sin(\psi)^2\lt(x_1^2+2x_2^2\rt) - \lt(1+\sin(\psi)^2\rt)2x_2Z_P\sin(\psi) - 2Z_P^2\sin(\psi)^2 - Z_P^2\cos(\psi)^2\rt]\\
    & & & + \sin(\psi)^2\lt(\sin(\psi)^2x_2^2 + 2x_2Z_P\sin(\psi) + Z_P^2\rt)\\
    \Leftrightarrow
    & 0 & = & X^2\lt[ x_1^2 + \lt(x_2 + Z_P\sin(\psi)\rt)^2 + Z_P^2\cos(\psi)^2 \rt]\\
    & & & - X\lt[ \sin(\psi)^2\lt(x_1^2+2x_2^2\rt) + \lt(1+\sin(\psi)^2\rt)2x_2Z_P\sin(\psi) + 2Z_P^2\sin(\psi)^2 + Z_P^2\cos(\psi)^2\rt]\\
    & & & + \sin(\psi)^2\lt(x_2\sin(\psi) + Z_P\rt)\\
    \Leftrightarrow
    & 0 & = & X^2\lt[ x_1^2 + \lt(x_2 + Z_P\sin(\psi)\rt)^2 + Z_P^2\cos(\psi)^2 \rt]\\
    & & & - X\lt[ \sin(\psi)^2\lt(x_1^2+x_2^2\rt) + x_2^2\sin(\psi)^2 + 2x_2Z_P\sin(\psi) + 2x_2Z_P\sin(\psi)^3 + Z_P^2 + Z_P^2\sin(\psi)^2\rt]\\
    & & & + \sin(\psi)^2\lt(x_2\sin(\psi) + Z_P\rt)\\
    \Leftrightarrow
    & 0 & = & X^2\lt[ x_1^2 + \lt(x_2 + Z_P\sin(\psi)\rt)^2 + Z_P^2 - Z_P^2\sin(\psi)^2 \rt]\\
    & & & - X\lt[ \sin(\psi)^2\lt(x_1^2+x_2^2\rt) + \lt(x_2\sin(\psi) + Z_P\rt)^2 + Z_P\sin(\psi)^2\lt(2x_2\sin(\psi) + Z_P\rt)\rt]\\
    & & & + \sin(\psi)^2\lt(x_2\sin(\psi) + Z_P\rt)\\
    \Leftrightarrow
    & 0 & = & X^2\lt[ x_1^2 + x_2^2 + \lt(x_2\sin(\psi) + Z_P\rt)^2 - x_2^2\sin(\psi)^2 \rt]\\
    & & & - X\lt[ \sin(\psi)^2\lt(x_1^2+x_2^2\rt) + \lt(x_2\sin(\psi) + Z_P\rt)^2 + Z_P\sin(\psi)^2\lt(2x_2\sin(\psi) + Z_P\rt)\rt]\\
    & & & + \sin(\psi)^2\lt(x_2\sin(\psi) + Z_P\rt)\\
    \Leftrightarrow
    & 0 & = & AX^2 - BX + C
\end{array}
$$

With:

$$
\lt\{
	\begin{array}{lll}
		A = x_1^2 + x_2^2 + \lt(x_2\sin(\psi) + Z_P\rt)^2 - x_2^2\sin(\psi)^2\\
		B = \sin(\psi)^2\lt(x_1^2+x_2^2\rt) + \lt(x_2\sin(\psi) + Z_P\rt)^2 + Z_P\sin(\psi)^2\lt(2x_2\sin(\psi) + Z_P\rt)\\
		C = \sin(\psi)^2\lt(x_2\sin(\psi) + Z_P\rt)
	\end{array}
\rt.
$$

Solutions exist if:
$$
\begin{array}{lllll}
    & \Delta & \geq & 0\\
    \Leftrightarrow
    & B^2 - 4AC & \geq & 0\\
\end{array}
$$

In which case, only solutions in $\lt[0,1\rt]$ are acceptable:
$$
\cos(\theta)^2 = \frac{B \pm \sqrt(\Delta)}{2A} \in \lt[0,1\rt]
$$

And by definition, $\theta \in \lt[0,\frac{\pi}{2}\rt]$, hence $\cos(\theta)\geq0$ and:
$$
\theta = \arccos\lt(\sqrt{\frac{B \pm \sqrt(\Delta)}{2A}}\rt)
$$

\subsubsection{Alternative method for $\theta$}

By definition:
$$
\underline{OM}.\ez = \cos(\theta)\|OM\|
$$

And:
$$
\begin{array}{lllll}
	\underline{OM} 	& = & \underline{OP} + \underline{PR} + \underline{RM}\\
			& = & \\
\end{array}
$$

\newpage
\section{Generalization}

This time, the crystal of curvature radius $R$ has center $C$ of coordinates $(x_C, y_C, z_C)$ in the tokamak's frame $(O, \ex, \ey, \ez)$.\\
The direct orthonormal systems are:

\begin{figure}[hbt]
	\includegraphics[width=\linewidth]{SpectroX2D_Crystal.pdf}
	\caption{Definition of the generilazed geometry}
	\label{fig:sketch}
\end{figure}

$$
\lt\{
	\begin{array}{lll}
		(O, \ex, \ey, \ez)\\
		(C, \nout, \e{1}, \e{2})\\
		(P, \np, \e{a}, \e{b})\\
		(R, \np, \e{i}, \e{j})
	\end{array}
\rt.
$$

\subsection{Direct problem}

We know all geometrical parameters, in particular, we know:
$$
\lt\{
	\begin{array}{lll}
		\ud{OC} = x(C)\ex + y(C)\ey + z(C)\ez\\
		\ud{CS} = R\nout\\
		\ud{SP} = -L\nout\\
		\ud{PR} = x_a(R)\e{a} + x_b(R)\e{b}\\
		\ud{RM} = x_i\e{i} + x_j\e{j}
	\end{array}
\rt.
$$

\appendix
\chapter{Appendices}

\section{Section}
\subsection{Subsection}

\end{document}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Focus Beamer Presentation
% LaTeX Template
% Version 1.0 (8/8/18)
%
% This template has been downloaded from:
% http://www.LaTeXTemplates.com
%
% Original author:
% Pasquale Africa (https://github.com/elauksap/focus-beamertheme) with modifications by
% Vel (vel@LaTeXTemplates.com)
%
% Template license:
% GNU GPL v3.0 License
%
% Important note:
% The bibliography/references need to be compiled with bibtex.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%----------------------------------------------------------------------------------------
%	PACKAGES AND OTHER DOCUMENT CONFIGURATIONS
%----------------------------------------------------------------------------------------

\documentclass[aspectratio=169]{beamer}

\usetheme{focus} % Use the Focus theme supplied with the template
% Add option [numbering=none] to disable the footer progress bar
% Add option [numbering=fullbar] to show the footer progress bar as always full with a slide count

% Uncomment to enable the ice-blue theme
%\definecolor{main}{RGB}{92, 138, 168}
%\definecolor{background}{RGB}{240, 247, 255}

%------------------------------------------------

\usepackage{booktabs} % Required for better table rules

%----------------------------------------------------------------------------------------
%	 TITLE SLIDE
%----------------------------------------------------------------------------------------

\title{Nonnegative Matrix Factorization }

\subtitle{Analysing Fruit Spectograms}

\author{Christoph Lange}

\titlegraphic{\includegraphics[scale=0.25]{images/strawberry.png}} % Optional title page image, comment this line to remove it
% Thanks cleanpng for the picture : https://www.cleanpng.com/png-strawberry-shortcake-clip-art-cartoon-strawberry-198600/
%\institute{Institute Name \\ Institute Address}

\date{\today}

%------------------------------------------------

\begin{document}

%------------------------------------------------

\begin{frame}
	\maketitle % Automatically created using the information in the commands above

\end{frame}

%----------------------------------------------------------------------------------------
%	 SECTION 1
%----------------------------------------------------------------------------------------

\section{Fruit Spectrogram  \\ Approximation} % Section title slide, unnumbered

%------------------------------------------------

\begin{frame}{Fruit Puree Spectrograms}
	\begin{columns}
		\column{0.35\textwidth}
		Fruit puree  \href{https://data.mendeley.com/datasets/frrv2yd9rg/1}{\color{brown}{dataset}} contains
			 \begin{itemize}
				\item  $N = 983$ fruit spectrograms
				\item $p = 235$ frequencies per spectrogram
				\item labels "Strawberry" or "NON-Strawberry"
			\end{itemize}

		\column{0.65\textwidth}
			\includegraphics[width=\linewidth]{images/three_examples.png}
	\end{columns}

\end{frame}

%------------------------------------------------

\begin{frame}{Nonnegative Matrix Factorization}

We represent $n$ spectrograms by
\begin{equation*}
X \in \mathbb{R}^{n x p}  \quad x_{i j} \geq 0
\end{equation*}
with one row $x_i$ being one spectrogram and approximate them.
\begin{equation*}
X \approx W \cdot H    \qquad  W \in \mathbb{R}^{n \times r}, H \in \mathbb{R}^{r \times p} \quad w_{i j} \geq 0 , h_{i j} \geq 0
\end{equation*}
Remarks:
\begin{itemize}
\item each row  $x_i = (x_{i 1}, x_{i 2}, \dots , x_{i p} ) $ is one spectogram
\item rows of H, $h_i = (h_{i, 1}, \dots , h_{i, p})   \quad i \in \{1, \dots , r \}$, form an $r$ dimensional basis
\item rows of W, $w_i = (w_{i 1}, \dots , w_{i r} )$ are the coordinates
\item $x_i \approx \sum_{j=1}^{r} w_{i j } h_{j} $
\item $(W \cdot H)^T = H^T \cdot W^T $, i.e. we can swap the roles of $W$ and $H$
\end{itemize}
\end{frame}

%------------------------------------------------

\begin{frame}{Example Factorization}

	\begin{columns}
		\column{0.35\textwidth}
			Steps
			 \begin{itemize}
				\item choose $r=4 $ dimensions
				\item find $W$ and $H$ such that $X \approx W \cdot H$ for  training set
				\item plot $\{ h_1, h_2, h_3, h_4 \} $
			\end{itemize}

		\column{0.65\textwidth}
			\includegraphics[width=\linewidth]{images/four_basis_vectors.png}
	\end{columns}
\end{frame}

%------------------------------------------------

\begin{frame}{Example Factorization}

	\begin{columns}
		\column{0.35\textwidth}
			Pick one spectrogram $x_i$ and compare
			\begin{equation*}
			x_i \approx \sum_{j=1}^{r} w_{i j } h_{j} = w_i \cdot H
			\end{equation*}
			The right hand side is the i-th row of $W \cdot H$.
		\column{0.65\textwidth}
			\includegraphics[width=\linewidth]{images/original_approx.png}
	\end{columns}
\end{frame}

%------------------------------------------------
\section{How does Nonnegative Matrix Factorization approximate?}

\begin{frame}{How does NMF approximate?}

Each spectrogram $x_i \in R^p$ should be close to it approximation $w_i H \in \mathbb{R}^p $  , i.e. the distance
\begin{equation*}
d(x_i, w_i  H) = \left\lVert x_i - w_i  H \right\rVert
\end{equation*}
between both should be small for all $ i  \in \{ 1, \dots , n \} $. Hence we minimize the sum of squared distances
\begin{equation*}
\sum_{i = 1}^{n} \left\lVert x_i - w_i H\right\rVert^2  = \left\lVert X - W H\right\rVert^2_F
\end{equation*}
which is the Frobenius norm of both matricies.
\end{frame}

%------------------------------------------------
\begin{frame}{Minimization Problem}

	\begin{columns}
		\column{0.35\textwidth}
			Choose $r$ components and minimize
			\begin{equation*}
			 \min_{ W, H }  \quad \left\lVert X - W H\right\rVert^2_F
			\end{equation*}
			How close is good enough?
		\column{0.65\textwidth}
			\includegraphics[width=\linewidth]{images/error_per_component.png}
	\end{columns}
\end{frame}

%------------------------------------------------

\begin{frame}{Use NMF for Strawberry Detection}

	\begin{columns}
		\column{0.35\textwidth}
			We do not want to minimize
			\begin{equation*}
			 \min_{ W, H }  \quad \left\lVert X - W H\right\rVert^2_F
			\end{equation*}
			but perform well on the classification task
			\begin{equation*}
			\min_{r}  - \sum_{i} \sum_{j =1}^{2}  y_i \log( \hat{y}_i)
			\end{equation*}
			$r=21$ might be optimal, but we choose $r=15$ for illustrative purposes.
		\column{0.65\textwidth}
			\includegraphics[width=\linewidth]{images/fruit_accuracies.png}
	\end{columns}
\end{frame}
%------------------------------------------------

\begin{frame}{Strawberry Basis Vectors}

	\begin{columns}
		\column{0.35\textwidth}
		Take a look at all basis vectors $h_i$ where the model thinks
		\begin{equation*}
		\hat{y}(e_i) > 1 - \hat{y}(e_i)
		\end{equation*}
		$h_i$ is a Strawberry.
		\begin{itemize}
		\item spectrograms $h_i$ capture distinct characteristica
		\item many frequencies $h_{i j} = 0$
		\end{itemize}

		\column{0.65\textwidth}
			\includegraphics[width=\linewidth]{images/strawberry_basis_vectors.png}
	\end{columns}
\end{frame}

%------------------------------------------------
\begin{frame}{Properties of NMF}
	\begin{columns}[t]

	\column{0.55 \textwidth}
	\textbf{advantages}
	\begin{itemize}
	\item filters noise $X - W H $
	\item basis vectors $h_i$ capture joint phenomena
	\item basis vectors $h_i$ can be interpreted to draw conclusions about the dataset
	\item imposing $W W^T  = I $ makes NMF equivalent to K-means clustering
	\end{itemize}
	\column{0.45 \textwidth}
	\textbf{disadvantages}
	\begin{itemize}
	\item Finding coordinates $W$ requires solving $\min_{ W}  \left\lVert X - W H\right\rVert^2_F$.
	\item limited to nonnegative matricies
	\end{itemize}
	\end{columns}
\end{frame}

%------------------------------------------------

\begin{frame}[focus]
	Thank you \\
	Questions?
	\vfill
	\href{https://github.com/spirousschuh/presentations/blob/master/NMF_fruit_purees/presentation/fruit_purees.pdf}
		{\small{Link to Slides}}

\end{frame}

%----------------------------------------------------------------------------------------
%	 CLOSING/SUPPLEMENTARY SLIDES
%----------------------------------------------------------------------------------------

\appendix

\begin{frame}{References}
	\nocite{*} % Display all references regardless of if they were cited
	\bibliography{NMF_papers}
	\bibliographystyle{plain}
\end{frame}

%------------------------------------------------

\begin{frame}{NON Strawberry Basis Vectors}

	\begin{columns}
		\column{0.35\textwidth}

		\column{0.65\textwidth}
			\includegraphics[width=\linewidth]{images/non_strawberry_basis_vectors.png}
	\end{columns}
\end{frame}

%------------------------------------------------

\begin{frame}{Bayesian Approach}
	The NMF solution $W, H$ that minimizes
	\begin{equation*}
	 \quad \left\lVert X - W H\right\rVert^2_F
	\end{equation*}
	is the maximum likelihood estimator assuming Gaussian noise
	\begin{equation*}
	X \sim N( W H, \sigma^2)
	\end{equation*}
	Bayesian approach enables
	\begin{equation*}
	P(W, H \mid X) \propto  P(X \mid W, H ) P(W, H)
	\end{equation*}
	incorporating domain knowledge about $P(H) $.
\end{frame}
%------------------------------------------------
\end{document}


\documentclass{article}
\title{CSE 847 Homework One}
\author{Reuben Lewis}
\date{05 February, 2021}

\usepackage{amsfonts}
\usepackage{amsmath}

\newcommand{\R}{{\rm\hbox{I\kern-.15em R}}}
\newcommand{\IR}{{\rm\hbox{I\kern-.15em R}}}
\newcommand{\IN}{{\rm\hbox{I\kern-.15em N}}}
\newcommand{\IZ}{{\sf\hbox{Z\kern-.40em Z}}}
\newcommand{\IS}{{\rm\hbox{S\kern-.45em S}}}
\newcommand{\Real}{I\!\!R}

\begin{document}
    \maketitle
    \section{Introduction}
    \subsection{Pg 58, 1.3}
        \begin{tabular}{ l | l | l | l | l}
            Box & Apples & Oranges & Limes & Selection Prob $p$ \\
            \hline
            tab$r$ & 3 & 4 & 3 & 0.2\\
            $b$ & 1 & 1 & 0 & 0.2\\
            $g$ & 3 & 3 & 4 & 0.6\\
        \end{tabular}

        (1) What is the probability of selecting is an apple?
            $p(A) = p(A,r) + p(A,b) + p(A,g)$ \\
            $p(A) = p(A|r)*p(r) + p(A|b)*p(b) + p(A|g)*p(g)$ \\
            $p(A) = 0.3*0.2 + 0.5*0.2 + 0.3*0.6$ \\
            $p(A) = 0.34$ \\

        (2) If the selected fruit is an orange, what is the probability
            it came from the green box? \\
            $p(O) = p(O,r) + p(O,b) + p(O,g)$ \\
            $p(O) = p(O|r)*p(r) + p(O|b)*p(b) + p(O|g)*p(g)$ \\
            $p(O) = 0.4*0.2 + 0.5*0.2 + 0.3*0.6$ \\
            $p(O) = 0.36$ \\
            $p(g|O) = \frac{p(O|g)p(g)}{p(O)}$ \\
            $p(g|O) = \frac{0.3 * 0.6}{0.34}$ \\
            $p(g|O) = 0.5$

    \subsection{Pg 59, 1.6}
    $cov[x,y] = 0$\\
    $\mathbb{E} [xy] - \mathbb{E}[x] \mathbb{E}[y] = 0$\\
    $\mathbb{E} [xy] = \mathbb{E}[x] \mathbb{E}[y]$\\
    $\sum_{x,y} xy P_{x,y}(x,y)= \sum_x x P(x) * \sum_y y P(y)$\\
    $P(x,y)= P(x) * P(y)$\\
    This is the definition of independence, so, if $x$ and $y$ are independent,
    their covariance is going to be $0$.

    \subsection{Pg 59, 1.11}
    Solve first for $\mu_{ML}$, then for $\sigma^{2}$ to verify the results of (1.55) and (1.56). This
    involves first taking the partial derivative with respect to the value we are solving for, then
    setting that equation to $0$ and solving for the value.\\
    (1) $\mu_{ML}$ \\
    \begin{center}
        $-\frac{1}{2\sigma^2} \sum_{n-1}^{N}(x_n -\mu)^2 - \frac{N}{2}\ln(\sigma^2) - \frac{N}{2}\ln(2\pi)$\\
        $\frac{\partial}{\partial \mu} \Rightarrow -\frac{1}{2\sigma^2} \sum_{n-1}^{N}\frac{\partial}{\partial \mu} (x_n -\mu)^2$\\
        $-\frac{1}{2\sigma^2} \sum_{n-1}^{N} 2 * -1 * (x_n -\mu)$\\
        $\frac{1}{\sigma^2} \sum_{n-1}^{N} (x_n -\mu)$\\
        $0 = \frac{1}{\sigma^2} \sum_{n-1}^{N} (x_n -\mu)$\\
        $0 = \sum_{n-1}^{N} (x_n -\mu)$\\
        $0 = \sum_{n-1}^{N} x_n - N*\mu$\\
        $N * \mu = \sum_{n-1}^{N} x_n$\\
        $\mu_{ML} = \frac{1}{N}\sum_{n-1}^{N} x_n$\\
    \end{center}
    (2) $\sigma^2$ \\
    \begin{center}
        $-\frac{1}{2\sigma^2} \sum_{n-1}^{N}(x_n -\mu)^2 - \frac{N}{2}\ln(\sigma^2) - \frac{N}{2}\ln(2\pi)$\\
        $\frac{\partial}{\partial \sigma^2} \Rightarrow -\frac{\partial}{\partial \sigma^2}\frac{1}{2\sigma^2} \sum_{n-1}^{N} (x_n -\mu)^2 - \frac{\partial}{\partial \sigma^2}\frac{N}{2}\ln(\sigma^2)$\\
        $\frac{1}{\sigma^4} \sum_{n-1}^{N} (x_n -\mu)^2 - \frac{N}{2\sigma^2}$\\
        $0 = \frac{1}{2\sigma^4} \sum_{n-1}^{N} (x_n -\mu)^2 - \frac{N}{2\sigma^2}$\\
        $\frac{N}{2\sigma^2} = \frac{1}{2\sigma^4} \sum_{n-1}^{N} (x_n -\mu)^2 $\\
        $N*\sigma^2 = \sum_{n-1}^{N} (x_n -\mu)^2 $\\
        $\sigma^2 = \frac{1}{N}\sum_{n-1}^{N} (x_n -\mu)^2 $\\
    \end{center}

    \section{Linear Algebra}
    \begin{enumerate}
        \item Matrix Calculations
        $$
        A = \begin{bmatrix} 1 & 2& 3\\ 2 & 1 & 4 \end{bmatrix},
        B = \begin{bmatrix} 1 & 0 \\ 2 & 1 \\ 3 & 2\end{bmatrix},
        C = \begin{bmatrix} 3 & -1 & 3 \\ 4 & 1 & 5\\ 2 & 1 & 3 \end{bmatrix},
        D = \begin{bmatrix} 2 & -4 & 5 \\ 0 & 1 & 4\\ 3 & 2 & 1 \end{bmatrix},
        E = \begin{bmatrix} 3 & -2 \\ 2 & 4 \end{bmatrix}.
        $$
        \begin{enumerate}
            \item $(2A)^T$
                First, scale $A$ by 2, then transpose it.
                \begin{center}
                    $2*A = \begin{bmatrix} 2 & 4& 6\\ 4 & 2 & 8 \end{bmatrix}$\\
                    $(2*A)^T = \begin{bmatrix} 2 & 4 \\ 4 & 2\\ 6 & 8 \end{bmatrix}$
                \end{center}
            \item $(A-B)^T$
                Because $A$ and $B$ don't have the same dimension, this equation doesn't work. $A - B^T$ would, however, since $B$ would
                then have the same dimensions as $A$.
            \item $(3B^T - 2A)^T$
                First, scale the matrices, then apply the transpose onto $B$. Next, subtract the matrices and transpose that output.
                \begin{center}
                    $2*A = \begin{bmatrix} 2 & 4& 6\\ 4 & 2 & 8 \end{bmatrix}$\\
                    $3*B = \begin{bmatrix} 3 & 0\\ 6 & 3 \\ 9 & 6 \end{bmatrix}$\\
                    $3*B^T = \begin{bmatrix} 3 & 6 & 9\\ 0 & 3 & 6 \end{bmatrix}$\\
                    $3*B^T - 2*A = \begin{bmatrix} 1 & 2 & 3\\ -4 & 1 & -2 \end{bmatrix}$\\
                    $(3*B^T - 2*A)^T = \begin{bmatrix} 1 & -4 \\ 2 & 1\\ 3 & -2 \end{bmatrix}$\\
                \end{center}
            \item $(-A)^T E$
                First, distribute the $-1*A$, and then transpose it. Next, multiply it by $E$, since there inner dimensions match (3x2 * 2x2).\\
                \begin{center}
                    $-1*A = \begin{bmatrix} -1 & -2& -3\\ -2 & -1 & -4 \end{bmatrix}$
                    $(-1*A)^T = \begin{bmatrix} -1 & -2 \\ -2 & -1\\  -3 & -4 \end{bmatrix}$
                    $(-1*A)^T E = \begin{bmatrix} -1*3 + -2*2 & -2*-1  + -2*4\\ -2*3 + -1*2  & -2*2 + -1*4  \\ -3*3 + -4*2 & -3*-2 + -4*4\end{bmatrix}$
                    $(-1*A)^T E = \begin{bmatrix} -7 & -6 \\ -8  & 0  \\ -17 & -10 \end{bmatrix}$
                \end{center}
            \item $(C+ D^T + E)^T$\\
                Because $E$ is not the same dimensions as $C$ or $D$, it can not be added to the others, so this operation cannot be done.
        \end{enumerate}
        \item Subspace of $\IR^2$? Justify your answer.
            \begin{enumerate}
                \item $\{(x,y ) \in \IR^2 | x^2 + y^2 = 0 \}$\\
                    This is not a subspace, since two vectors $(1,0) + (1,1) = (2,1)$ and show's it's not closed under addition.
                \item $\{(x,y ) \in \IR^2 | x^2 - y^2 = 0 \}$\\
                    This is not a subspace, since two vectors $(1,1) + (1,-1) = (2,0)$ and show's it's not closed under addition.
                \item $\{(x,y ) \in \IR^2 | x^2 - y = 0 \}$\\
                    This is not a subspace, since two vectors $(1,1) + (1,-1) = (2,0)$ and show's it's not closed under addition.
                \item $\{(x,y ) \in \IR^2 | x - y = 0 \}$\\
                    This is a subspace, since it satisfies the three main properties. The $0$ vector is a part of the space,
                    it's closed under vector addition, and it's closed under scalar multiplication. This is a special case of a
                    line with a slope of 1.
                    \begin{center}
                        Closed under Vector addition:\\
                        $u = (x_1, y_1), v = (x_2, y_2)$\\
                        $u + v = (x_1+x_2, y_1+y_2)$\\
                        $u + v = (x_1+x_2, x_1+x_2)$\\
                    \end{center}
                    This process is similar for scaling as well.
                \item $\{(x,y ) \in \IR^2 | x - y = 1 \}$\\
                    This is not a subspace because it doesn't contain the origin.
            \end{enumerate}
        \item $$ A = \begin{bmatrix}1 & 2\\ 3 & 2\end{bmatrix}, B =  \begin{bmatrix}2 & -1\\ -3 & 4\end{bmatrix}.$$
        Is $AB = BA$?
        No.\\
        \begin{center}
            $AB =  \begin{bmatrix}1*2 + 2*-3 & 1*-1 + 2*4\\ 3*2 + 2*-2 & 3*-1 + 2*4\end{bmatrix}$\\
            $AB =  \begin{bmatrix}-4 & 7\\ 2 & 5\end{bmatrix}$\\
            $BA =  \begin{bmatrix}2*1 + -1*3 & 2*2 + -1*2\\ -3*1 + 4*3 & -3*2 + 4*2\end{bmatrix}$\\
            $BA =  \begin{bmatrix}-1 & 2\\ 9 & 2\end{bmatrix}$
        \end{center}
        \item
        \begin{enumerate}
            \item Because of how matrix multiplication works, you multiply the rows of the first matrix by the columns of the
            second. This means that if there is a row of 0s in the first matrix, that row would be multiplied against every
            column of the second matrix. This would cause a row of 0s in the resulting matrix, since multiplying any value
            by 0 results in 0. As an example, imagine $A = 2 X 3$ matrix and $B = 3 X 4$, with a row of 0s at $i = 2$. Row 2
            of the resulting matrix would be calculated like so:
            \begin{center}
                $A_{2k} = \sum_{j} 0_{2,j} * b_{j,k}$\\
                $\begin{bmatrix}0*b_{0,0} + 0*b_{1,0} + 0*b_{2,0} & 0*b_{0,1} + 0*b_{1,1} + 0*b_{2,1}\end{bmatrix}$\\
                $\begin{bmatrix}0 & 0 \end{bmatrix}$\\
            \end{center}
            \item The best way to show this is through an example. Let
            \begin{center}
                $$
                A = \begin{bmatrix} 1 & 0\\ 1 & 0 \end{bmatrix},
                B = \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix}
                $$
            \end{center}
            If we multiply $BA$, then we get the following matrix. This is because when we multiply rows of $B$ through the
            columns of $A$, the column of 0s in $A$ nullifies the values in the row of $B$, resulting in a column of 0s in
            the resulting matrix.
            \begin{center}
                $BA = \begin{bmatrix} 1*1 + 2*1 & 1*0  + 2*0 \\ 3*1 + 4*1 & 3*0  + 4*0 \end{bmatrix}$\\
                $BA = \begin{bmatrix} 3 & 0 \\ 7 & 0 \end{bmatrix}$\\
            \end{center}
        \end{enumerate}
        \item Show that $\mbox{rank}(AB) \leq \min\{\mbox{rank}(A), \mbox{rank}(B)\}$.
        \begin{center}
            Remember that $rank(AB) = dim(ran(AB)), rank(A) = dim(ran(A))$
            By definition, $ran(AB) = {y \in \IR^n : y = ABx for some x \in \IR^n}$
            Substitute $z = Bx \Rightarrow y = A(Bx) \Rightarrow {y \in \IR^n : y = Az for some x \in \IR^n} = ran(a)$
        \end{center}
        This shows that the vector space $ran(AB)$ is a subspace of $ran(A)$, which allows us to say
        $rank(AB) = dim(ran(AB)) \leq dim(ran(A)) = rank(A)$. Now what about $rank(B)$?
        \begin{center}
            Remember that $rank(A) = rank(A^T)$
            Let $C = A^T$ and $D = B^T$
            $rank(DC) \leq rank(D) \Rightarrow rank(C^TD^T) \leq rank(D^T)$
            Translating that back over, we get $rank(AB) \leq rank(B)$
        \end{center}
        So, by that logic, $rank(AB)$ is less than or equal to whichever of $A$ or $B$ has the lower
        rank.
    \end{enumerate}

\end{document}


% Part: model-theory
% Chapter: models-of-arithmetic
% Section: computable-models

\documentclass[../../../include/open-logic-section]{subfiles}

\begin{document}

\olfileid{mod}{mar}{cmp}
\section{Computable Models of Arithmetic}

\begin{explain}
The standard model~$\Struct{N}$ has two nice features. Its domain is
the natural numbers~$\Nat$, i.e., its elements are just the kinds of
things we want to talk about using the language of arithmetic, and the
standard numeral~$\num{n}$ actually picks out~$n$. The other nice
feature is that the interpretations of the non-logical symbols
of~$\Lang{L_A}$ are all \emph{computable}. The successor, addition,
and multiplication functions which serve as $\Assign{\prime}{N}$,
$\Assign{+}{N}$, and $\Assign{\times}{N}$ are computable functions of
numbers. (Computable by Turing machines, or definable by primitive
recursion, say.) And the less-than relation on~$\Struct{N}$, i.e.,
$\Assign{<}{N}$, is decidable.

Non-standard models of arithmetical theories such as $\Th{Q}$ and
$\Th{PA}$ must contain non-standard elements. Thus their domains
typically include !!{element}s in addition to~$\Nat$. However, any
countable !!{structure} can be built on any !!{denumerable} set,
including~$\Nat$. So there are also non-standard models with
domain~$\Nat$. In such models~$\Struct{M}$, of course, at least some
numbers cannot play the roles they usually play, since some~$k$ must
be different from~$\Value{\num{n}}{M}$ for all~$n \in \Nat$.
\end{explain}

\begin{defn}
!!^a{structure}~$\Struct{M}$ for $\Lang{L_A}$ is \emph{computable} iff
  $\Domain{M} = \Nat$ and $\Assign{\prime}{M}$, $\Assign{+}{M}$,
  $\Assign{\times}{M}$ are computable functions and $\Assign{<}{M}$ is
  a decidable relation.
\end{defn}

\begin{ex}\ollabel{ex:comp-model-q}
Recall the structure $\Struct{K}$ from \olref[mdq]{ex:model-K-of-Q}.
Its domain was $\Domain{K} = \Nat
\cup \{a\}$ and interpretations
\begin{align*}
  \Assign{\Obj{0}}{K} & = 0\\
  \Assign{\prime}{K}(x) & =
  \begin{cases}
    x+1 & \text{if $x\in \Nat$}\\
    a & \text{if $x = a$}
  \end{cases}\\
  \Assign{+}{K}(x, y) & =
  \begin{cases}
    x+y & \text{if $x$, $y \in\Nat$}\\
    a & \text{otherwise}
  \end{cases}\\
  \Assign{\times}{K}(x, y) & =
  \begin{cases}
    xy & \text{if $x$, $y \in\Nat$}\\
    0 & \text{if $x=0$ or $y=0$}\\
    a & \text{otherwise}\\
  \end{cases}\\
  \Assign{<}{K} & =
  \Setabs{\tuple{x,y}}{x, y \in \Nat \text{ and } x<y} \cup
  \Setabs{\tuple{x,a}}{n \in \Domain{K}}
\end{align*}
But $\Domain{K}$ is !!{denumerable} and so is equinumerous
with~$\Nat$. For instance, $g\colon \Nat \to \Domain{K}$ with $g(0) =
a$ and $g(n) = n+1$ for $n>0$ is !!a{bijection}.  We can turn it into
an isomorphism between a new model~$\Struct{K'}$ of~$\Th{Q}$ and
$\Struct{K}$.  In $\Struct{K'}$, we have to assign different functions
and relations to the symbols of~$\Lang{L_A}$, since different
!!{element}s of~$\Nat$ play the roles of standard and non-standard
numbers.

Specifically, $0$ now plays the role of~$a$, not of the smallest
standard number. The smallest standard number is now~$1$. So we assign
$\Assign{\Obj{0}}{K'} = 1$. The successor function is also different
now: given a standard number, i.e., an $n > 0$, it still returns
$n+1$. But $0$ now plays the role of~$a$, which is its own
successor. So $\Assign{\prime}{K'}(0) = 0$.  For addition and
multiplication we likewise have
\begin{align*}
\Assign{+}{K'}(x, y) & =
  \begin{cases}
    x+y-1 & \text{if $x$, $y >0$}\\
    0 & \text{otherwise}
  \end{cases}\\
  \Assign{\times}{K'}(x, y) & =
  \begin{cases}
    1 & \text{if $x = 1$ or $y = 1$}\\
    xy - x - y + 2 & \text{if $x$, $y > 1$}\\
    0 & \text{otherwise}\\
  \end{cases}
\end{align*}
And we have $\tuple{x, y} \in \Assign{<}{K'}$ iff $x < y$ and $x > 0$
and $y > 0$, or if $y = 0$.

All of these functions are computable functions of natural numbers and
$\Assign{<}{K'}$ is a decidable relation on~$\Nat$---but they are not
the same functions as successor, addition, and multiplication
on~$\Nat$, and $\Assign{<}{K'}$ is not the same relation as~$<$
on~$\Nat$.
\end{ex}

\begin{prob}
Give !!a{structure}~$\Struct{L'}$ with $\Domain{L'} = \Nat$ isomorphic
to~$\Struct{L}$ of \olref[mod][mar][mdq]{ex:model-L-of-Q}.
\end{prob}

\begin{explain}
\Olref{ex:comp-model-q} shows that $\Th{Q}$ has computable
non-standard models with domain~$\Nat$.  However, the following result
shows that this is not true for models of~$\Th{PA}$ (and thus also for
models of~$\Th{TA}$).
\end{explain}

\begin{thm}[Tennenbaum's Theorem]
$\Struct{N}$ is the only computable model of~$\Th{PA}$.
\end{thm}

\end{document}


\documentclass[a4paper]{article}
\usepackage[inline]{enumitem}
\usepackage{tasks}
\settasks{label =•}
\usepackage{enumitem}
\setenumerate{label = \alph*)}
\input{head}
\newcolumntype{Y}[1]{>{\centering\arraybackslash} X{#1}}
\begin{document}

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

\fancyhead[C]{}
\hrule \medskip % Upper rule
\begin{minipage}{0.295\textwidth}
\raggedright
\footnotesize
Winston Peng \hfill\\
50364686 \hfill\\
wpeng2@buffalo.edu
\end{minipage}
\begin{minipage}{0.4\textwidth}
\centering
\large
Homework 2 - Predicates \& Logical Proofs\\
\normalsize
Intro To Discrete Structures, 2021\\
\end{minipage}
\begin{minipage}{0.295\textwidth}
\raggedleft
\today\hfill\\
\end{minipage}
\medskip\hrule
\bigskip

\section{Translate English To Math - Predicates}
\large Predicates:
\normalsize
\begin{itemize}
    \item \textbf{Sitting(x, y):} x is sitting on y
    \item \textbf{Free(y):} y is free
    \item \textbf{Top(y, z):} y is on top of z
    \item \textbf{Brown(y):} y is brown
    \item \textbf{Black(y):} y is black
\end{itemize}
\large Domains:
\normalsize
\begin{itemize}
    \item domain of x: {a group of people}
    \item domain of y and z: {a set of deck boxes}
\end{itemize}
\large Problems: \\
\smallskip
\normalsize
\underline{Note}: B is a constant. Top(y, B): y is on top of B.
\begin{enumerate}
    \item \textbf{Everyone is sitting on some box.} $\forall x \:\exists y, \: Sitting(x, y)$
    \item \textbf{Every box that is free has nobody sitting on it.} $\forall y \; \neg \exists x, \: Sitting(x, Free(y))$
    \item \textbf{Every box that is brown is free.} $\forall y, \: Brown(y)) \implies Free(y)$
    \item \textbf{Every box that is not black and on top of box B is brown.} $\forall y, \: \neg(Black(y) \land Top(y, B)) \implies Brown(y)$
    \item \textbf{There is a box that is black and not free.} $\exists y, \: Black(y) \land \neg Free(y) $
\end{enumerate}

\bigskip %----------------------------------------

\section{Translate Math To English - Predicates}
\large Predicates:
\normalsize
\begin{itemize}
    \item \textbf{Edge(x, y):} x is connected to y
    \item \textbf{Color(x, z):} x has color z
\end{itemize}
\large Domains:
\normalsize
\begin{itemize}
    \item domain of x and y: {all nodes in a graph},
    \item domain of z: {RED, GREEN, BLUE, YELLOW},
\end{itemize}
\large Problems:
\normalsize
\boldmath
\begin{enumerate}
    \item $\forall x \, \exists y \; Edge(x, y)$ \\
    All nodes in the graph are connected to at least one node in the graph.
    \item $\forall z \, \exists x \; Color(x, z)$ \\
    A node in the graph is RED, GREEN, BLUE, and YELLOW.
    \item $\forall x \forall y \forall z \: (Edge(x, y) \land Color(x, z) \implies \neg Color(y, z))$ \\
    Every box that is RED, GREEN, BLUE, and YELLOW is connected to another box. It is not connected to a box that is RED, GREEN, BLUE, and, YELLOW
    \item $\neg \forall x \; Color(x, YELLOW)$ \\
    Not every node is yellow.
    \item $\forall x \, \exists y \; (Edge(x, y) \land Color(x, BLUE) \implies Color(y, RED) \lor Color(y, GREEN))$ \\
    Every blue node in the graph is connected to a node that is either red or green.
\end{enumerate}
\unboldmath

\bigskip %------------------------------------

\section{Negate Given Statements (Quantifier Negation)}
\begin{enumerate}
    \item \boldmath $\exists x \; (P(x) \lor Q(x))$ \unboldmath

    $\neg(\exists x \; (P(x) \lor Q(x)))$ \\
    $\forall x \; \neg (P(x) \lor Q(x))$ \\
    $\forall x \; \neg P(x) \land \neg Q(x)$

    \item \boldmath $\forall x \, \exists y \; (P(x) \lor Q(y))$ \unboldmath

    $\neg(\forall x \, \exists y \; (P(x) \lor Q(y)))$ \\
    $\exists x \, \forall y \; \neg (P(x) \lor Q(y))$ \\
    $\exists x \, \forall y \; \neg P(x) \land \neg Q(y)$

    \item \boldmath $\exists x \; (P(x) \land \forall y (P(y) \implies Q(y)))$ \unboldmath

    $\neg (\exists x \; (P(x) \land \forall y (P(y) \implies Q(y))))$ \\
    $\forall x \; \neg (P(x) \land \forall y (P(y) \implies Q(y)))$ \\
    $\forall x \; \neg P(x) \lor \neg(\forall y) \neg (P(y) \implies Q(y))$ \\
    $\forall x \; \neg P(x) \lor \exists y \, P(y) \land \neg Q(y)$
\end{enumerate}

\bigskip %------------------------------------------

\section{Prove With Logical Equivalence Laws (Math)}
% \begin{center}
% \begin{tabularx}{0.8\textwidth}{X X X X}
% % \begin{enumerate}
%     \mutlicolumn{2}{c}{$p \implies q$} & \mutlicolumn{2}{X}{$\neg p \land q$} \\
%     \multicolumn{2}{X}{$q \implies r$} & $\neg(p \land r) \implies s$\\
%     $\neg r$ & $r \implies p$ \\
%     % \hline & \hline \\
%     $\therefore \neg p$ & $\therefore s$
% % \end{enumerate}
% \end{tabularx}
% \end{center}
\begin{enumerate}
\boldmath
    \item  $p \implies q$ \\
    $q \implies r$ \\
    $\neg r$ \\
    \begin{tabularx}{0.1\textwidth}{X}
        \hline
    \end{tabularx}\\
    $\therefore \neg p$
\unboldmath
\smallskip

\begin{tabularx}{0.6\textwidth}{X X}
\toprule
1) $p \implies q$ & Hypothesis\\
2) $q \implies r$ & Hypothesis\\
3) $\neg r$ & Hypothesis\\
4) $p \implies r$ & Hypothetical syllogism (1, 2)\\
5) $\neg p$ & Modus tollens (3, 4)\\
\end{tabularx}

\boldmath
    \item $\neg p \land q$ \\
    $\neg(p \land r) \implies s$ \\
    $r \implies p$ \\
    \begin{tabularx}{0.1\textwidth}{X}
        \hline
    \end{tabularx}\\
    $\therefore s$
\unboldmath

\smallskip

\begin{tabularx}{0.6\textwidth}{X X}
\toprule
1) $\neg p \land q$ & Hypothesis\\
2) $\neg(p \land r) \implies s$ & Hypothesis\\
3) $r \implies p$ & Hypothesis\\
% 4) $p \implies q$ & Conditional\\
% 5) $q$ & Modus ponens (4) \\
4) $\neg p$ & Simplification (1) \\
5) $\neg r$ & Modus tollens (4, 3) \\
6) $\neg \neg(p \lor r) \lor s$ & Conditional \\
7) $(p \lor r) \lor s$ & Double Negation Law \\
8) $s$ & Disjunctive Syllogism (7, 4, 5)
\end{tabularx}
\end{enumerate}

\bigskip %-------------------------------------------------------

\section{Prove Or Disprove With Logical Equivalence Laws (English)}
\begin{center}
\begin{tabularx}{0.8\textwidth}{|X|}
\hline
If it is not windy or not cloudy, then we will visit the nearby town and do shopping \\
If we visit the nearby town, then we'll dine at the restaurant \\
we did not dine at the restaurant. \\ [1ex]

\toprule \\

$\therefore$ It was cloudy \\
\hline
\end{tabularx}
\end{center}

\begin{itemize}
    \item p: It is windy
    \item q: It is cloudy
    \item r: Visit the nearby town
    \item s: Do shopping
    \item t: Dine at restaurant
\end{itemize}

\begin{tabularx}{0.6\textwidth}{X X}
\toprule \\
1) $(\neg p \lor \neg q) \implies (r \land s)$ & Hypothesis \\
2) $r \implies t$ & Hypothesis \\
3) $\neg t$ & Hypothesis \\
4) $\neg r$ & Moudus tollens (3, 2) \\
5) $(\neg p \lor \neg q) \implies F$ & Simplification (1) \\
6) $\neg (\neg p \lor \neg q)$ & Modus tollens (5) \\
7) $p \land q$ & DeMorgan's Law \\
8) $p$ & Simplification
\end{tabularx}

\bigskip %----------------------------

\section{Direct Proof}
\boldmath
\begin{tabularx}{1.0\textwidth}{|X|}
\hline
Prove that \textbf{If x is an even integer, then $x^2 - 6x + 5$ is odd} \\
\hline
\end{tabularx}
\unboldmath
\begin{itemize}
    \item Decomposition of statement to $P(x) \implies Q(x)$
    \begin{itemize}
        \item The domain of $x$ is all integers.
        \item $P(x)$: $x$ is an even integer
        \item $Q(x)$: $x^2 - 6x + 5$ is odd.
    \end{itemize}
\end{itemize}
\smallskip

\textbf{\underline{Proof}:} Assume $x$ is an even integer. \\
Then exists an integer $k$ such that $x = 2k$.  \\
Hence: $(2k)^2 - 6x + 5$ + $4k^2 - 6(2k) + 5$ $2(2)k^2 - 6 * 2k + 5$. \\
Since $k$ is an integer, $4k^2 - 6(2k) + 5$ $2(2)k^2 - 6 * 2k + 5$ is also an integer (call it $j$). \\
So $n^2 - 6x + 5$ has the form $2j + 5$.
Therefore, $n^2 -6x + 5$ is an odd integer.
% As multiplying anything by $2$ results in 2, everything is even, until you add $5$, as $even + odd = odd$, $x^2 - 6x + 5$ is odd.

\bigskip %--------------------------------------

\section{Prove By Contraposition}
\boldmath
\begin{tabularx}{1.0\textwidth}{|X|}
\hline
Prove that \textbf{If $x$ is an integer and $x^2$ is even, then $x$ is even} \\
\hline
\end{tabularx}
\unboldmath
\begin{itemize}
    \item Decomposition of statement to $P(x) \implies Q(x)$
    \begin{itemize}
        \item The domain of $x$ is all integers.
        \item $P(x)$: $x^2$ is an even integer
        \item $Q(x)$: $x$ is even.
    \end{itemize}
\end{itemize}

\textbf{\underline{Proof}:} Assume $x$ is not an even integer. So $x$ is odd. \\
Then exists an integer $k$ such that $x = 2k + 1$. \\
Hence: $(2k + 1)^{2} = 4k^2 + 4k + 1 = 2(2k^2 + 2k) + 1$ \\
Since $k$ is an integer, $2(2k^2 + 2k) + 1$ is also an integer (call it $j$) \\
So, $n^2$ has the form $2j + 1$ \\
Therefore, $x^2$ is an odd integer, so $x^2$ is not an odd integer. \\
Thus, $\neg Q(n) \implies \neg P(n)$. \\
Then, $P(n) \implies Q(n)$ is also true. \\
\end{document}


\documentclass[11pt,a4paper]{article}

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

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

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

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

\begin{document}

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

\section{Introduction}

Both the Newton-Raphson method and the Secant method were implemented and employed
to find an extremum of a continuous multivariate function. If started on a point close
enough to the extremum, both (very similar) methods were found to converge on the maximum
which was known analytically in the case under consideration.

\section{Algorithm Description}
While the algorithm as such is quite simple, using it on the given function $F(x, y) = e^{-(x - x_0)^2 -(y -y_0)^2}$ requires
some setup.\\
Analogous to the univariate case, to find the (global) maximum of $F(x,y)$, we must find the root of $\vec{\nabla} F(x, y)$,
or in other words, the solution of $ \vec{0} = \vec{f}(x,y) \coloneqq \vec{\nabla}F(x,y)$ (see equation~\ref{eqn:1}).
The Jacobi matrix of $f(x,y)$ required by the Newton-Raphson method then is as described
by equation~\ref{eqn:2}.

\begin{equation}
	\begin{pmatrix}
		0 \\
		0
	\end{pmatrix}
	=
	\vec{f}(x,y) \coloneqq
	\begin{pmatrix}
		\partial F(x,y)/\partial x \\
		\partial F(x,y)/\partial y
	\end{pmatrix}
	=
	\begin{pmatrix}
		-2(x-x_0) \cdot e^{-(x - x_0)^2 -(y -y_0)^2} \\
		-2(y-y_0) \cdot e^{-(x - x_0)^2 -(y -y_0)^2}
	\end{pmatrix}
	\label{eqn:1}
\end{equation}

\begin{equation}
	\begin{array}{lll}
	\vec{\vec{J}}_f(x,y)
	& =
	\begin{pmatrix}
		\partial f_x(x,y)/ \partial x & \partial f_x(x,y)/ \partial y \\
		\partial f_y(x,y)/ \partial x & \partial f_y(x,y)/ \partial y \\
	\end{pmatrix}
	 =
	\begin{pmatrix}
		\partial^2 F(x,y) / \partial x^2 & \partial^2 F(x,y) / \partial x \partial y \\
		\partial^2 F(x,y) / \partial y \partial x & \partial^2 F(x,y) / \partial x^2
	\end{pmatrix}
	= \\
	& =
	e^{-(x - x_0)^2 -(y -y_0)^2} \cdot
	\begin{pmatrix}
	4(x - x_0) - 2 & 4(x-x_0)(y-y_0) \\
	4(x - x_0)(y - y_0) & 4(y - y_0)^2 - 2
	\end{pmatrix}
\end{array}
	\label{eqn:2}
\end{equation}

For brevity, we refer to the task description for the interpolating Jacobian of
the Secant method.

With the only difference between the Newton-Raphson method and the Secant method
being the way the Jacobian matrix is calculated, the algorithm then works as follows:
\begin{outline}
\1 Choose starting point $\vec{x}_0$ close enough to suspected maximum
\1 While $f(x_{n+1}) > \epsilon$
	\2 Do $\vec{x}_{n+1} = \vec{x}_n - (\vec{\vec{J}}_f(\vec{x}_n))^{-1} \cdot f(\vec{x}_n)$
\1 Output $\vec{x}_{n+1}$
\end{outline}
\section{Results}

The program was implemented as described above and submitted with this report.
For the given problem, the Jacobian matrices of the methods are almost equal,
and both were found to converge for starting values $x,y$ in the range $x \in [x_0 - \xi, x_0 + \xi ], y \in [y_0 - \upsilon, y_0 + \upsilon]; \quad \xi=\upsilon=0.35$
(see figures~\ref{fig:1}~and~\ref{fig:2}).

\begin{figure}[ht]
\begin{center}
\includegraphics[scale=1.2]{figure1.eps}
\end{center}
\caption{Convergence steps of Newton-Raphson method to find maximum of $F(x,y)=e^{-(x - x_0)^2 -(y -y_0)^2}; x_0=y_0=2$.}
\label{fig:1}
\end{figure}

\begin{figure}[ht]
\begin{center}
\includegraphics[scale=1.2]{figure2.eps}
\end{center}
\caption{Convergence steps of Secant method to find maximum of $F(x,y)=e^{-(x - x_0)^2 -(y -y_0)^2}; x_0=y_0=2$.}
\label{fig:2}
\end{figure}

\section{Discussion}
Both methods were found to converge in an equal amount of steps. As expected,
the starting coordinates have to be chosen close to the actual maximum for the methods
to converge.
% \begin{thebibliography}{99}

% \bibitem{metropolis}
% Metropolis, N.,
% Rosenbluth, A.W.,
% Rosenbluth, M.N.,
% Teller, A.H.,
% Teller, E.\\
% \emph{Equations of State Calculations by Fast Computing Machines},\\
% Journal of Chemical Physics. 21 (6): 1087,\\
% 1953.

% \bibitem{herrmann}
% 	Herrmann, H. J.,
% 	Singer, H. M.,
% 	Mueller L.,
% 	Buchmann, M.-A.,\\
% 	\emph{Introduction to Computational Physics - Lecture Notes},\\
% 	ETH Zurich,\\
% 	2017.

% \end{thebibliography}

\end{document}


\chapter{Proportional EMG-Driven Angle Control}
\label{ch:ProportionalControl}

Many authors applied successfully a proportional EMG control for a mechanism (e.g. \cite{Bottomley411}). Hogan \cite{1103644} stated that, for contractions of the muscle below 30\% of the maximum voluntary contraction, the relative force applied by the biceps muscle to the elbow joint and the EMG signal can be considered as proportional.

Lets assume a simplified model of the arm with one degree of freedom on the elbow joint.

\begin{equation}\label{eq:simpleModel}
T = (J + M\cdot L^2)\cdot \ddot{\theta}  + B \cdot \dot{\theta}  + (m\cdot l + M \cdot L) \cdot g \cdot cos(\theta)
\end{equation}

If we assume low speed and low acceleration, the equation can be simplified to:

\begin{equation}\label{eq:kcos}
T = K \cdot cos(\theta)
\end{equation}

\begin{equation}
K = (m\cdot l + M\cdot L)\cdot g
\end{equation}

Where K is the coefficient for the gravity-related forces.

This equation states a direct relationship between joint angle and torque and consequently, for low muscle activation levels, joint angle and EMG magnitude.

From the experiments developed at chapter \ref{ch:preliminarExperiment}, a powerful relation between the elbow angle and the sEMG signal can be observed, especially at the test set where the user is carrying no extra weight in his hand, shown in figure \ref{EmgAngleDirect}.

\begin{figure}[thpb]
      \centering
      \includegraphics[scale=0.35]{Images/EmgAngleDirect.jpg}
      \caption{Comparison between the measured elbow angle and the EMG values for the continuous movement. The EMG values were multiplied by a constant for better visualization.}
      \label{EmgAngleDirect}
   \end{figure}

   The correlation between these two signals is 97.78\%. It shows that, at least for the configuration of the proposed experiment, there is a high correlation between the EMG signal and the elbow joint angle.

   The first problem of this relation appears when comparing the EMG signals with the intermittent movement. See figure \ref{EmgAngleInt}.

\begin{figure}[thpb]
      \centering
      \includegraphics[scale=0.35]{Images/EmgAngleInt.jpg}
      \caption{Comparison between the measured elbow angle and the EMG values for the intermittent movement. The EMG values were multiplied by a constant for better visualization.}
      \label{EmgAngleInt}
   \end{figure}

   As the image shows, for the upward movement, there is a proportional relation between the EMG values and the angle of the elbow joint. When the elbow joint stays in a static angle, the EMG magnitude lowers, even without elbow movement.

   To address this problem, further investigations must be made. Two main ideias will be explored. The first one is to use the Time Domain analysis of the EMG signal. The TD analysis is commonly used in pattern recognition for EMG signals. It uses the Mean Absolute Value,
Zero Crossing, Slope Sign Changes and Waveform Length of the signal to acquire more information about the user's intention of movement. Another ideia is to use the wavelet analysis to the EMG signal. The wavelet analysis is extensively used in EMG pattern recognition systems. It analyses a larger bandwidth than just the 1Hz to 3Hz commonly used in EMG proportional control. By analyzing higher frequencies, it is possible to acquire more information about the intention of movement. "Borrowing" this technique to the proportional control, we may be able to detect the intention to make an extension movement and then using the proportional control to determine the desired position of the joint.


\hypertarget{group__numpp__differentiation__automatic}{}\section{Automatic Differentiation}
\label{group__numpp__differentiation__automatic}\index{Automatic Differentiation@{Automatic Differentiation}}

This module provides the means for performing automatic differentiation.

\subsection*{Modules}
\begin{DoxyCompactItemize}
\item
\hyperlink{group__numpp__differentiation__backward__automatic}{Backward Automatic Differentiation}
\begin{DoxyCompactList}\small\item\em First order automatic differentiation in reverse mode. \end{DoxyCompactList}\item
\hyperlink{group__numpp__differentiation__forward__automatic}{Forward Automatic Differentiation}
\begin{DoxyCompactList}\small\item\em First order automatic differentiation in forward mode. \end{DoxyCompactList}\end{DoxyCompactItemize}

\subsection{Detailed Description}
This module provides the means for performing automatic differentiation.

Only forward mode as of now, backward propagation should be provided later.

{\bfseries Only first order partial differentiation supported.}

{\bfseries For higher order derivatives check \hyperlink{group__numpp__differentiation__symbolic}{Symbolic Differentiation} }

\begin{DoxyWarning}{Warning}
Only arithmetic types are backed by the implementation. To specify your own type, you have to specialize is\+\_\+arithmetic S\+TL trait. Such type has to provide overload for addition and mathematical oprations like sin, cos etc. (constexpr versions for G\+CC compiler)
\end{DoxyWarning}
Namespace\+:
\begin{DoxyCode}
\hyperlink{namespacenumpp_1_1differentiation_1_1automatic}{numpp::differentiation::automatic}
\end{DoxyCode}

Include\+:
\begin{DoxyCode}
\hyperlink{namespacenumpp}{numpp}/differentiation/automatic.hpp #includes every method in the module
\hyperlink{namespacenumpp}{numpp}/differentiation/automatic/SUBCLASS.hpp #include specific SUBCLASS
\end{DoxyCode}

\begin{DoxyWarning}{Warning}
{\bfseries Second include option S\+H\+O\+U\+L\+DN\textquotesingle{}T be used}
\end{DoxyWarning}


% -*- root: developer-guide.tex -*-

\section{Random Clifford sampling procedure}

This section provides documentation for the routine found in \texttt{src/cliffords/swap-representation.lisp}. The n-qubit Clifford group grows rapidly with the number of qubits, in particular as $\prod^n_{i=1} 2(4^i - 1)4^i$. In addition, the Clifford group often comes about in quantum computation, in particular because it stabilizes the Pauli operators, and can thus be written as a basis map that that only has size polynomial in the number of qubits under investigation. In addition, schemes like randomized benchmarking leverage the fact that the Clifford group is a unitary 2-design to estimate the error on a gate.

In routines like randomized benchmarking it is necessary to sample randomly from the Clifford group, and one could imagine a form of classical benchmarking where random clifford circuits are sampled, simulted, and compared with quantum hardware. From the Gottesman-Knill theorem, this simulation can be done efficiently.

The naive approach to sampling from the Clifford group scales poorly - generating the entire group quickly becomes infeasible after two qubits. One way of attempting to improve this would be to still do a tree search over the group, but to implement a more intelligent pruning technique. Namely, if we can recognize that a collection of paths will lead to the same kind of sub-trees, we can ignore those paths, and cut down the search time.

A natural way to attempt this is to consider Clifford elements to be equivalent up to \SWAP\ operations. The \SWAP\ group is an asymptotically large, easy to understand, and relatively uninteresting subgroup of the Clifford group. In particular, looking at $C_n/\SWAP_n$ we see that the sizes grow more slowly, supressed by a factor of $n!$.

In order to work with this subset of the Clifford group, one needs to generate a procedure for picking out a canonical representative for each coset of the \SWAP\ group in the Clifford group efficiently. Such a procedure is given (currently without proof) below, and implemented in the code. A nice property of this particular routine is that it canonizes \SWAP\ operations to the identity.

\begin{algorithm}[H]
\caption*{\textsc{\textbf{Canonical Representative}}}
\hspace*{\algorithmicindent} \textbf{Input} $c\in C_n$, $c\equiv[X_1, Z_1, ..., X_n, Z_n]$\\
\hspace*{\algorithmicindent} \textbf{Output} $\Pi(c)$, $\Pi\in \SWAP_n$\\
\begin{algorithmic}
\State \textbf{initialize} $\Pi$ = Identity
\For{$1 \leq i \leq n$}
    \State \textbf{defun} MaxIndices(A, k, n) := \{$\ell\mid A_\ell$ = n and $\ell >k$\}
    \State indices = MaxIndices(M($X_i$), i, max($M(X_i)$))
    \For{$i \leq j \leq n$}
        \State curr\_max = max(\{$M(X_j)_\ell\mid\ell\in$indices\})
        \State indices = indices $\cap$ MaxIndices(M($X_j$), j, curr\_max)
        \State curr\_max = max(\{$M(Z_j)_\ell\mid\ell\in$indices\})
        \State indices = indices $\cap$ MaxIndices(M($Z_j$), j, curr\_max)
    \EndFor
    \State first = indices[0]
    \State $\pi := \SWAP(\textrm{first}, \Pi^{-1}(0))$
    \State set $\Pi := \pi\circ\Pi$
\EndFor
\State \Return $\Pi(c)$

\end{algorithmic}
\end{algorithm}

The main idea behind this algorithm is that for each basis vector (sorted in a standard order) we can choose swaps such that the $i$\textsuperscript{th} image has a Pauli term with the largest base four representation in the $i$\textsuperscript{th} position. This immediately satisfies the property that SWAPs are canonized to the identity, if the basis vectors were sorted as $X_1, Z_1, ..., X_n, Z_n$. The only remaining problem is that there are in general many tensor factors in the image that have the same highest base four representation. To break these ties unambiguously we must iterate through the remaining basis vectors, indexed by $j$, and look at their images. If we call the indices of the tied tensor factors in the image `indices` as above, then we can break ties in the $i$\textsuperscript{th} image by looking at the base four representation of all elements of `indices`, and only keeping the terms that are maximal for each $j$. This process will either terminate with one unique element, in which case we should swap that element to the $i$\textsuperscript{th} position, or it will terminate with many elements, from which we are free to pick one. If there is more than one element, they have the same base four representation in each image, are therefore indistinguishable by swapping, and we are free to pick any one in the $i$\textsuperscript{th} step.

As a final remark, we give a rough estimate of the runtime of this algorithm. The number of basis vectors is linear in the number of qubits, $n$. For each (even) basis vector, we look at all following basis vectors, of which there are at most $n$. In addition, for each following basis vector, we must look at all tensor factors in its image, which is also $n$. Therefore the runtime of this algorithm is $\mathcal{O}(n^3)$.


\documentclass{ximera}
\input{../preamble}
\title{Volume By General Cross Sections}
%%%%%\author{Philip T. Gressman}

\begin{document}
\begin{abstract}
We use cross-sectional area to compute volume.
\end{abstract}
\maketitle

\section*{Online Texts}
\begin{itemize}
\item \link[OpenStax II 2.2: Determining Volumes by Slicing]{https://openstax.org/books/calculus-volume-2/pages/2-2-determining-volumes-by-slicing}
\item \link[Ximera OSU: Accumulated Cross Sections]{https://ximera.osu.edu/mooculus/calculus2/accumulatedCrossSections/titlePage}
\end{itemize}

\section*{Examples}

\begin{example}
The base of a solid region is bounded by the curves $x = 0$, $y= 0$, and $y = \sqrt{1-x^2}$. The cross sections perpendicular to the $x$ axis are squares. Compute the volume of the region.
\begin{solution}
Lines in the $xy$-plane which are perpendicular to the $x$-axis are vertical, so the base of a typical $x$ cross section will extend from $y=0$ to $y = \sqrt{1-x^2}$. Since each cross section will have area
\[ A(x) = \left( \sqrt{1-x^2} - 0 \right)^2 = 1-x^2. \]
To compute volume, we integrate $dV = A(x) dx$ between $x=0$ and $x=1$, since these are the most extreme values of $x$ found in our region. Therefore
\[ V = \int_0^1 ( 1 -x^2) dx = \left. x - \frac{x^3}{3} \right|_0^1 = 1 - \frac{1}{3} = \frac{2}{3}. \]
\end{solution}
\end{example}

\begin{example}
The base of a solid region is bounded by the curves $y = 0$, $x = \sqrt{y}$, and $x = 1$. The cross sections perpendicular to the $y$-axis are squares. Compute the volume of the region.
\begin{solution}
Lines in the $xy$-plane which are perpendicular to the $y$-axis are \wordChoice{\choice[correct]{horizontal}\choice{vertical}}, so the base of a typical $y$ cross section will extend from the graph $x=\answer{\sqrt{y}}$ to the graph $x = \answer{1}$. The length of the base is the difference of $x$-coordinates (since all points on a slice have the same $y$-coordinate), so the length of the base is $\answer{1 - \sqrt{y}}$, giving the square an area of
\[ A(y) = \answer{(1-\sqrt{y})^2} \]
(note that the answer is a function of $y$ because different $y$ cross sections will generally have different areas).
To compute volume, we integrate $dV = A(y) dy$ between $y=0$ and $y=1$, since these are the most extreme values of $y$ found in our region (note that we can find the upper value $y=1$ by solving for the intersection of the curves $x = \sqrt{y}$ and $x=1$). Therefore we integrate $A(y) dy$ to conclude
\[ V = \int_{\answer{0}}^{\answer{1}} \answer{(1-\sqrt{y})^2} dy = \answer{\frac{1}{6}}. \]
\end{solution}
\end{example}

\end{document}


\section{Introduction}
In many situations, visual representations of matrices facilitate the
understanding of linear algebra properties, relations, and operations
enormously.  This package provides simple tools to bring such representations
to \LaTeX.  For instance,
\[
  \drawmatrix[upper]A \;
  \drawmatrix[width=.5]X +
  \drawmatrix[width=.5]X \;
  \drawmatrix[upper, size=.5, bbox height=1]B =
  \drawmatrix[width=.5]C
\]
is typeset as follows:
\begin{dispListing*}{listing only}
\[
  \drawmatrix[upper]A \;
  \drawmatrix[width=.5]X +
  \drawmatrix[width=.5]X \;
  \drawmatrix[upper, size=.5, bbox height=1]B =
  \drawmatrix[width=.5]C
\]
\end{dispListing*}

\section{Drawing Matrices}
\begin{docCommand}{drawmatrix}{\oarg{options}\marg{label}}
    Draws a matrix labeled \meta{label}.  The optional \meta{options}, which
    modify various aspects of drawn matrix through PGF's key-value system, are
    introduced in the following sections.
\end{docCommand}

The label is typeset in the surrounding mode and style.
\begin{dispExample*}{righthand width=.35\textwidth}
    $\drawmatrix A$
    {\bfseries\drawmatrix A}
    {\large\drawmatrix A}
\end{dispExample*}
In equations, parentheses (spanned with \cs{left} and \cs{right}), subscripts,
and superscripts naturally extend to the drawn shape:
\begin{dispExample*}{righthand width=.35\textwidth}
    $\left(
    \drawmatrix A_i +
    \drawmatrix B^{-1}
    \right)
    \drawmatrix C$
\end{dispExample*}
Used in matrix products, a little space (\cs;) helps to yield a more natural
result:
\begin{dispExample}
    $\drawmatrix A \drawmatrix B$

    $\drawmatrix A \; \drawmatrix B$
\end{dispExample}

\begin{docDmKey}{label text}{=\meta{text}}{no default, initially \meta{label}}
    Stores the label text.  It overwrites \refCom{drawmatrix}'s \meta{label}
    argument.
    \begin{dispExample}
        $\drawmatrix[label text=B]A$
    \end{dispExample}
\end{docDmKey}

\subsection{Size}

\begin{docDmKey}[][docmulti]{height}{=\meta{dimension}}{no default, initially 1}
\end{docDmKey}
\begin{docDmKey}{width}{=\meta{dimension}}{no default, initially 1}
    Width and height of the drawmatrix in \TikZ's coordinate system |canvas|.
    May be given in units such as |em| or |cm|.
    \begin{dispExample}
        \drawmatrix[width=.5]A
        \drawmatrix[width=2ex]B
        \drawmatrix[height=.35cm]C
    \end{dispExample}
\end{docDmKey}

A width or height of |0| are useful to represent vectors:
\begin{dispExample}
    \drawmatrix[width=0]x
\end{dispExample}

\begin{docDmKey}{size}{=\meta{dimension}}{style, no default}
    Shortcut for setting both \refKey{/drawmatrix/height} and
    \refKey{/drawmatrix/width} to the same \meta{dimension}, resulting in a
    square matrix.
\end{docDmKey}

\subsection{Shape}

By default matrices are rectangular.

\subsubsection{Triangular and Trapezoidal Matrices}
\begin{docDmKey}[][docmulti]{lower}{}{style, no value, initially unset}
\end{docDmKey}
\begin{docDmKey}{upper}{}{style, no value, initially unset}
    Result in, respectively, lower- and upper-triangular matrices.  Non-square
    matrices become trapezoidal.
    \begin{dispExample}
        \drawmatrix[lower]L
        \drawmatrix[upper, width=1.5]U
    \end{dispExample}
\end{docDmKey}

\subsubsection{Banded Matrices}

\begin{docDmKey}[][docmulti]{lower banded}{}{style, no value, initially unset}
\end{docDmKey}
\begin{docDmKey}{upper banded}{}{style, no value}
    Draw matrices as banded with bandwidth |0.3|.
    \begin{dispExample}
        \drawmatrix[lower banded]B
        \drawmatrix[upper banded]B
    \end{dispExample}
\end{docDmKey}

\begin{docDmKey}{banded}{}{style, no value}
    Shortcut for setting both \refKey{/drawmatrix/lower banded} and
    \refKey{/drawmatrix/upper banded}.
    \begin{dispExample}
        \drawmatrix[banded]B
    \end{dispExample}
\end{docDmKey}

\begin{docDmKey}[][docmulti]{lower bandwidth}{=\meta{dimension}}{no default, initially empty}
\end{docDmKey}
\begin{docDmKey}{upper bandwidth}{=\meta{dimension}}{no default, initially empty}
    The bandwidths, i.e., the horizontal/vertical extent from the diagonal.
    \begin{dispExample}
        \drawmatrix[lower bandwidth=.5]B
        \drawmatrix[upper bandwidth=.5]B
    \end{dispExample}
\end{docDmKey}
\begin{docDmKey}{bandwidth}{=\meta{dimension}}{style, no default}
    Shortcut for setting both \refKey{/drawmatrix/lower bandwidth} and
    \refKey{/drawmatrix/upper bandwidth}.
    \begin{dispExample}
        \drawmatrix[bandwidth=.5]B
    \end{dispExample}
\end{docDmKey}

Banding on rectangular matrices applies to the smaller of the two dimensions:
\begin{dispExample}
    \drawmatrix[banded, width=.8]B
    \drawmatrix[upper banded, height=.7]B
\end{dispExample}

\refKey{/drawmatrix/banded} can be combined with \refKey{/drawmatrix/lower} or
\refKey{/drawmatrix/upper} to draw the intersection of both shapes.
\begin{dispExample}
    \drawmatrix[banded, lower]L
\end{dispExample}

\subsubsection{Diagonal Matrices}

\begin{docDmKey}{diag}{}{style, no value}
    Shortcut for \refKey{/drawmatrix/bandwidth}|=0|.
    \begin{dispExample}
        \drawmatrix[diag]D
    \end{dispExample}
\end{docDmKey}

\subsubsection{Super- and Subscripts}

\begin{docDmKey}{label base}{=\meta{text}}{no default, initially empty}
    Defines the label to be centered in the drawmatrix, and to which the actual
    \refKey{/drawmatrix/label text} is aligned.  This feature is useful to,
    e.g., draw centered labels with exponents:
    \begin{dispExample}
        $\drawmatrix[size=.5]{A^T}$
        $\drawmatrix[size=.5, label base=A]{A^T}$
    \end{dispExample}
\end{docDmKey}

\begin{docDmKey}{label base anchor}{=\meta{anchor}}{no default, initially |base west|}
    The anchor of the \refKey{/drawmatrix/label text} with respect to the
    \refKey{/drawmatrix/label base}.
    \begin{dispExample}
        $\drawmatrix[size=.5, label base=A, label base anchor=base east]{^0_1A}$
    \end{dispExample}
\end{docDmKey}

\begin{docDmKey}{exponent}{=\meta{text}}{style, no default}
    Shortcut to add an exponent to matrix without offsetting the label.  It sets
    the \refKey{/drawmatrix/label base} to the current \refKey{/drawmatrix/label
    text} and adds the exponent \meta{text} to \refKey{/drawmatrix/label text}.
    \begin{dispExample}
       $\drawmatrix[size=.5, exponent=T]A$
    \end{dispExample}
\end{docDmKey}

\subsection{Colors and Style}

By default, matrices are drawn in gray and filled white.  The \TikZ keys
|draw=|\meta{color} and |fill=|\meta{color} change these colors.  In fact, all
keys not recognized by this package are passed to the \TikZ \cs{filldraw} command
drawing the matrix.
\begin{dispExample}
   \drawmatrix[fill=yellow, draw=blue]A
   \drawmatrix[very thick, dashed]A
\end{dispExample}

\subsection{The Bounding Box}

All matrices are contained in a rectangular bounding box.

\begin{docDmKey}{bbox}{}{style, no default, initially empty}
    Add options to the \TikZ \cs{node} that is the bounding box..
    This is useful to, e.g., to visualize the 0 entries in the matrix:
    \begin{dispExample}
        \drawmatrix[lower, bbox/.append style={fill=blue!10}]L
    \end{dispExample}
\end{docDmKey}
\begin{docDmKey}{bbox style}{=\marg{style}}{style, no default}
    Shortcut for \refKey{/drawmatrix/bbox}|/.append style=|\meta{style}.
\end{docDmKey}

\begin{docDmKey}[][docmulti]{bbox height}{=\meta{dimension}}{no default, initially empty}
\end{docDmKey}
\begin{docDmKey}{bbox width}{=\meta{dimension}}{no default, initially empty}
    Explicitly set the height and width of the bounding box.
    If unset, the bounding box is just large enough to contain the matrix.
    The label of the matrix (and thus the alignment with respect to the
    surrounding text) are by default fixed at the center\footnote{See
    \refKey{/drawmatrix/label anchor}.} of the bounding box, while the matrix is
    positioned at its top-left corner.
    \begin{dispExample}
        \drawmatrixset{bbox style={fill=blue!10}}

        \drawmatrix[bbox width=2, bbox height=1.5]A
    \end{dispExample}
\end{docDmKey}

\begin{docDmKey}{bbox size}{=\meta{dimension}}{style, no default}
    Shortcut for setting \refKey{/drawmatrix/bbox height} and
    \refKey{/drawmatrix/bbox width} to the same value.
    \begin{dispExample}
        \drawmatrixset{bbox style={fill=blue!10}}

        \drawmatrix[bbox size=1.5]A
    \end{dispExample}
\end{docDmKey}

\begin{docDmKey}[][docmulti]{offset height}{=\meta{dimension}}{no default, initially |0|}
\end{docDmKey}
\begin{docDmKey}{offset width}{=\meta{dimension}}{no default, initially |0|}
    Sets the vertical and horizontal offset of the drawn matrix within its
    bounding box.
    \begin{dispExample}
       \drawmatrixset{bbox style={fill=blue!10}}

       \drawmatrix[bbox size=2, offset width=.5, offset height=.75]A
    \end{dispExample}
\end{docDmKey}

\begin{docDmKey}{offset}{=\meta{dimension}}{style, no default}
    Shortcut for setting \refKey{/drawmatrix/offset height} and
    \refKey{/drawmatrix/bbox width} to the same value.
    \begin{dispExample}
       \drawmatrixset{bbox style={fill=blue!10}}

       \drawmatrix[bbox size=2, offset=.5]A
    \end{dispExample}
\end{docDmKey}

\subsection{Coordinate System Transformations}

\begin{docDmKey}{scale}{=\meta{factor}}{style, no default}
    Scales all dimensions passed to a matrix.  Can be used repeatedly to
    multiply scales
    \begin{dispExample}
        \drawmatrix[scale=.6]A
        \drawmatrix[scale=.6, width=.5]B
        \drawmatrix[scale=.7, scale=.7]B
    \end{dispExample}
\end{docDmKey}

\begin{docDmKey}[][docmulti]x{=\meta{value}}{style, no default}
\end{docDmKey}
\begin{docDmKey}y{=\meta{value}}{style, no default}
    Define the coordinate system for all unit-less dimensions.
    \begin{dispExample}
        \drawmatrix[x=.6cm, y=.4cm]A
        \drawmatrix[x=.6cm, y=.4cm, width=1cm]B
    \end{dispExample}
\end{docDmKey}

\subsection{Position of the Label and Baseline}

By default, the label's |mid| is positioned at the bounding box's |center| and
its |base| is used as the whole drawing's baseline.

\begin{docDmKey}{label anchor}{=\meta{anchor}}{style, no default, initially |mid|}
    Set the anchor of label's \TikZ \cs{node}.
    \begin{dispExample}
        \drawmatrix[label anchor=north]A
    \end{dispExample}
\end{docDmKey}

\begin{docDmKey}{label pos}{=\meta{position}}{style, no default, initially |bbox.center|}
    Define the position of the label's \TikZ \cs{node} within the picture.  The
    following nodes and their anchors are available: |bbox| (the bounding box)
    and the |matrix| (matrix itself).
    \begin{dispExample}
        \drawmatrix[label pos=bbox.south]A
        \drawmatrix[label pos=matrix.north west]B
    \end{dispExample}
\end{docDmKey}

\begin{docDmKey}{baseline}{=\meta{position}}{style, no default, initially |label.base|}
    Specify how the picture is vertically aligned with the surrounding text's
    baseline.  Options are the same anchors as for \refKey{/drawmatrix/label
    pos} and anchors of |label| (the label).
    \begin{dispExample}
        A
        \drawmatrix[baseline=label.north]A
        \drawmatrix[baseline=bbox.south]A
    \end{dispExample}
\end{docDmKey}

\section{Changing Defaults}

\begin{docCommand}{drawmatrixset}{\marg{options}}
    Applies options to to all following uses of \refCom{drawmatrix} within the current
    scope.
    \begin{dispExample}
       \drawmatrixset{height=.5, lower}

       $\drawmatrix A \; \drawmatrix B$
    \end{dispExample}
\end{docCommand}

\begin{docDmKey}[][docmulti]{every picture}{}{style, no value}
\end{docDmKey}
\begin{docDmKey}[][docmulti]{every bbox}{}{style, no value}
\end{docDmKey}
\begin{docDmKey}[][docmulti]{every drawmatrix}{}{style, no value}
\end{docDmKey}
\begin{docDmKey}[][docmulti]{every label}{}{style, no value}
\end{docDmKey}
\begin{docDmKey}{every label}{}{style, no value}
    Settings for all drawmatrix pictures, bounding boxes, matrices, and labels.
    Options should be added not with |/.style=| but with |/.append style=| to
    avoid messing with internals.
    \begin{dispExample}
        \drawmatrixset{every drawmatrix/.append style={rounded corners=5pt}}

        $\drawmatrix A \; \drawmatrix[lower]B$
    \end{dispExample}
\end{docDmKey}

\section{Externalization}

\refCom{drawmatrix} behaves as any other \TikZ picture, therefore when
externalization is enabled, all matrix visualizations are also externalized.
However, since there are usually many \refCom{drawmatrix} pictures, each of
which is very small and fast to produce, their externalization would mean a
tremendous overhead.

\begin{docDmKey}{externalize}{=true\textbar false}{no default, initially |true|}
    Explicitly disables externalization for all \refCom{drawmatrix} pictures. It
    does not enable externalization.
\end{docDmKey}