\chapter{Additional Features}
Various features of {\ViennaMath}, which are not necessarily standard features of a symbolic math library, are covered in this chapter.
Additional feature requests should be sent to
\begin{center}
\texttt{viennamath-support$@$lists.sourceforge.net}
\end{center}

  \section{\LaTeX{} Output}
Since {\ViennaMath} encourages a high-level description and manipulation of the underlying mathematical problem formulation in source code, it is natural to
generate \LaTeX{} code from {\ViennaMath} expressions for debugging purposes. The generated code can be copy\&paste'd to LaTeX rendering webpages or used for
the automatic generation of program log files in the form of a \LaTeX{} document.

All conversion is carried out by a separate converter object of type \lstinline|rt_latex_translator<InterfaceType>| as defined in
\lstinline|viennamath/manipulation/latex.hpp|. A convenience shortcut \lstinline|latex_translator| is available for the default runtime expression interface.
Conversion is triggered by providing the expression to be converted to the functor:
\begin{lstlisting}
 latex_translator  to_latex;

 expr f = sqrt( x + y );
 to_latex( f );     //returns the string '\sqrt{x_{0}+x_{1}}'
\end{lstlisting}
By default, variables are printed as $x_0$, $x_1$, etc. This and other output routines can be customized by using the \lstinline|customize()| member function
of the converter. For example, to print 'x' and 'y' instead of 'x\_\{0\}' and 'x\_\{1\}', the code
\begin{lstlisting}
 to_latex.customize(x, "x");
 to_latex.customize(y, "y");
\end{lstlisting}
is sufficient. Similar customizations can be applied for the output of types and features described in the remainder of this chapter.

\NOTE{The \LaTeX{} generator works with runtime expression types only. Thus, compiletime expression types need to be converted to runtime expression types
first.}

  \section{Differential Operators}
For enabling dimension-independent programming, dimension-independent mathematical differential operations are also provided with {\ViennaMath}.
In {\ViennaMathversion}, the gradient and the divergence operators are provided by the functions \lstinline|grad()| and \lstinline|div()| respectively:
\begin{lstlisting}
 expr u = grad(x+y);
 expr v = grad(x-y);
 expr w = div(grad(x*x + y*y));
\end{lstlisting}
Note that expression containing differential operators cannot be evaluated directly, since a coordinate system needs to be specified first.

A coordinate system is applied to the previous expressions by using the free function \lstinline|apply_coordinate_system()|, which is defined in the header file \lstinline|viennamath/manipulation/apply_coordinate_system.hpp|. The first function argument is a tag identifying the coordinate system (either of type \lstinline|cartesian<1>|, \lstinline|cartesian<2>|, or \lstinline|cartesian<3>| for a Cartesian coordinate system in one, two or three dimensions). The second function argument is the expression to which the coordinate system should be applied:
\begin{lstlisting}
 apply_coordinate_system(cartesian<1>(), u); //returns 1 + y
 apply_coordinate_system(cartesian<2>(), v); //returns (1, -1)
 apply_coordinate_system(cartesian<3>(), w); //returns 4
\end{lstlisting}
Differential operators are very handy in combination with function symbols explained in Sec.~\ref{sec:function-symbols}.

%%%%%%%%%%%%%%%
  \section{Integration Symbols}
In certain cases the form of an integral expression is known, but the actual integration domain is determined at some later stage.
Here, a symbolic integration domain of type \lstinline|symbolic_interval| identified by an ID can be specified and substituted by the final integration interval later on.
For example, the integral $\int_\Omega x^2 \: \mathrm{d} \Omega$, where $\Omega$ is specified at some later point, is specified using {\ViennaMath} as
\begin{lstlisting}
 expr my_integral = integral(symbolic_interval(), x*x);
\end{lstlisting}
The interface is again such that it can be used with both runtime and compiletime types. For simplicity, the resulting integral expression is here assigned to a runtime expression \lstinline|my_integral|. An ID can be provided to the constructor of \lstinline|symbolic_interval| for distinguishing between different symbolic intervals. By default, an ID of $0$ is used.

In order to substitute the symbolic interval with the actual integration interval, the function \lstinline|substitute()| as explained in Sec.~\ref{sec:substitute} is used.
Since the replacement consists of both the integration interval and the variable over which integration is to be carried out, the replacement argument is packed into a \lstinline|pair| as defined in the C++ STL. Thus, in order to specify $\Omega$ as the interval $[0,1]$ with an integration over the $x$-variable, the code
\begin{lstlisting}
 expr my_integral2 = substitute( symbolic_interval(),
                                 std::make_pair(interval(0, 1), x),
                                 my_integral);
\end{lstlisting}
is sufficient. Note that in the current release of {\ViennaMath} only the substitution with a one-dimensional integration domain is supported, but no nested integrals are possible yet.

\TIP{Note that \lstinline|std::make_pair()| is defined in the header \lstinline|<utility>|.}

%%%%%%%%%%%%%%%
  \section{Function Symbols} \label{sec:function-symbols}
For discretization schemes based on weak formulations of partial differential equations it is appropriate to work with abstract functions rather than with concrete expressions.
For example, the weak form of the Poisson equation,
\begin{align} \label{eq:weak-poisson}
 \int_\Omega \nabla u \cdot \nabla v \: \mathrm{d} x = \int_\Omega fv \: \mathrm{d} x
\end{align}
for all test functions in a certain test space $\mathcal{V}$, is formulated for functions $u$ and $v$, which are during the discretization replaced by certain trial and test functions, which finally yields a system of linear equations.
Such function labels (\emph{function symbols}) are modeled in {\ViennaMath} by the type \lstinline|rt_function_symbol<InterfaceType>| at runtime (with convenience shortcut \lstinline|function_symbol| for the default runtime interface type) and by \lstinline|ct_function_symbol<T>| at compiletime, where \lstinline|T| is a tag identifying the function symbol.

As a simple example, the expression $uv$ is considered, where $u$ is then substituted with the expression $(1+x)$ and $v$ is replaced by $(1-x)$:
\begin{lstlisting}
 function_symbol u(0);
 function_symbol v(1);
 expr f = u * v;
 expr g = substitute(u, 1.0 + x,
                     substitute(v, 1.0 - x, f)
                    );    // g becomes (1+x)*(1-x)
\end{lstlisting}
The constructor arguments denote the function symbol ID used for distinguishing the individual function symbols.

Reconsidering the weak form \eqref{eq:weak-poisson}, function symbols at compiletime can use any arbitrary tag class for identification.
{\ViennaMath} provides the predefined tags \lstinline|unknown_tag<id>| and \lstinline|test_tag<id>|, where the integer template parameter \lstinline|tag| is used for distinguishing between several function symbols of the same tag. The previous code snippet rewritten for compiletime manipulation thus becomes
\begin{lstlisting}
 ct_function_symbol< unknown_tag<0> > u;
 ct_function_symbol<    test_tag<0> > v;
 substitute(u, 1.0 + x,
            substitute(v, 1.0 - x, u*v)
           );    // returns (1+x)*(1-x)
\end{lstlisting}
Note that the result type of \lstinline|substitute()| encodes the result \lstinline|(1+x)*(1-x)|, thus the result is usually directly passed to another function (e.g.~\lstinline|eval()|) in order to avoid writing the return type explicitly.

As a final example, the weak form \eqref{eq:weak-poisson} (with $f \equiv 1$ for simplicity) is specified directly as a {\ViennaMath} runtime expression and converted to {\LaTeX}-code using the functionality presented in this chapter:
\begin{lstlisting}
 function_symbol u(0);
 function_symbol v(1);
 equation weak_form = make_equation( integral( symbolic_interval(),
                                               grad(u) * grad(v) ),
                                     integral( symbolic_interval(), v ) );
 latex_translator to_latex;
 std::cout << to_latex(weak_form) << std::endl;
\end{lstlisting}

\TIP{ Have a look at \texttt{ViennaFEM}~(\texttt{http://viennafem.sourceforge.net/}) if you are interested in a software package using {\ViennaMath} for the finite element method. }


\chapter{Introduction}
\label{chapter:introduction}
\par
The Lanczos eigensolver
finds selected eigenvalues and eigenvectors of
% three types of eigenproblems,
$AX = B X \Lambda$, where $X$ are eigenvectors
and $\Lambda$ is a diagonal matrix whose elements are eigenvalues.
Three types of eigenproblems are supported.
\begin{itemize}
\item
An ``ordinary'' eigenvalue problem
where $A$ is symmetric and $B = I$.
\item
An ``vibration'' eigenvalue problem
where $A$ is symmetric and
$B$ is symmetric positive semidefinite.
\item
A ``buckling'' eigenvalue problem
$A$ is symmetric positive semidefinite and
$B$ is symmetric.
\end{itemize}
For the vibration and buckling problems, there must exist a
$\sigma$ that is not an eigenvalue such that
$A - \sigma B$ is nonsingular,
i.e., $A$ and $B$ cannot share the same null space.
\par
During the computations, the eigensolver requires the following
sparse linear algebra computations.
\begin{itemize}
\item
Sparse factorizations of the form $A - \sigma B$.
\item
Solves of the form $(A - \sigma B) Z = Y$.
\item
Multiplies of the form $Z = B Y$ (for the vibration problem)
or $Z = A Y$ (for the buckling problem).
\end{itemize}
The Lanczos eigensolver has defined a specific interface with an
external linear algebra package to perform these three operations.
The eigensolver currently interfaces with the {\bf BCSLIB-EXT} linear
solver in a serial environment and the {\bf SPOOLES} linear solver
in serial, multithreaded and MPI environments.
\par
This paper documents the {\bf SPOOLES} objects and functions
that interface with the eigensolver.
The three following chapters describe the serial, multithreaded and
MPI objects, their data structures, and their methods.
The appendix contains listings of three driver programs to exercise
the eigensolver using the {\bf SPOOLES} library.
\par
Symmetric permutations of the eigensystem do not change the
eigenvalues, and the eigenvectors can be easily constructed
using the permutation matrix.
$$
A X = B X \Lambda
\longrightarrow
{\widehat A} {\widehat X}
= {\widehat B} {\widehat X} \Lambda
\quad \mbox{where} \quad
{\widehat A} = PAP^T, \quad
{\widehat B} = PBP^T, \quad
\mbox{and} \quad
{\widehat X} = PX
$$
The linear algebra package is free to use any permutation matrix
$P$ to most efficiently perform the factorizations and solves
involving ${\widehat A}$ and ${\widehat B}$.
This permutation matrix $P$ is typically found by ordering the
graph of $A + B$ using a variant of minimum degree or nested
dissection.
The ordering is performed prior to any action by the eigensolver.
This ``setup phase'' includes more than just finding the
permutation matrix, e.g., various data structures must be
initialized.
In a parallel environment, there is even more setup work to do,
analyzing the factorization and solves and specifying which threads
or processors perform what computations and store what data.
In a distributed environment, the entries of $A$ and $B$ must
also be distributed among the processors in preparation for
the factors and multiplies.
\par
For each of the three environments --- serial, multithreaded and
MPI --- the {\bf SPOOLES} solver has constructed a ``bridge''
object to span the interface between the linear system solver
and the eigensolver.
Each of the {\tt Bridge}, {\tt BridgeMT} and {\tt BridgeMPI}
objects have five methods: set-up, factor, solve, matrix-multiply
and cleanup.
The factor, solve and matrix-multiply methods follow the calling
sequence convention imposed by the eigensolver, and are passed
to the eigensolver at the beginning of the Lanczos run.
The set-up method is called prior to the eigensolver, and the
cleanup method is called after the eigenvalues and eigenvectors
have been determined.


\subsection{Homework}
\begin{enumerate}
\item Search the internet for equations containing a Laplace operator
  and models from science, economics, etc.
\item Energy minimization and weak formulation
\item Prove Poincare inequality (1D)
\end{enumerate}

\subsection{Classroom topics}
\begin{enumerate}
\item Model problem: Poisson equation, boundary conditions
\item Lack of solution theory
\item Weak formulation, natural boundary condition, analogy to linear algebra
\item Elementary integration
\item Elementary Sobolev spaces, Lax-Milgram
\end{enumerate}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "main"
%%% End:


\documentclass[11pt]{article}
\setcounter{secnumdepth}{0}

\usepackage{relsize}

\newcommand{\mono}[1]{{\smaller\texttt{#1}}}                    % literal (to be typed): code, program names

\begin{document}

\section{Fitting a mixture Dirichlet to counts}

\mono{esl\_mixdchlet\_Fit()} infers a maximum likelihood mixture
Dirichlet distribution for a data set of count vectors. It uses
conjugate gradient descent from an initial starting point. The result
is only a local optimum, so we typically run it multiple times with
different starting points. The partial derivatives of the log
likelihood function are persnickety, and the purpose of these notes is
to enshrine the derivation that corresponds to the implementation.

We have $N$ count vectors $c_i$, with each vector consisting of $K$
counts for individual symbols $c_{ia} \geq 0$. The mixture Dirichlet
$\theta$ consists of $Q$ components $\alpha_k$, with each parameter
vector containing $K$ parameters $\alpha_{ka} > 0$, and $Q$ mixture
coefficients $q_k > 0, \sum_k q_k = 1$.

The log likelihood of the data is:

\[
  L = \log P(\mbox{data} \mid \theta) = \sum_i \log P(c_i \mid \theta) = \sum_i \log \sum_k q_k P(c_i \mid \alpha_k)
\]

\mono{esl\_mixdchlet\_logpdf\_c()} calculates $\log P(c_i \mid
\theta)$.

$P(c_i \mid \alpha_k)$, the probability of one count vector given one
Dirichlet component, is:

\[
P(c_i \mid \alpha_k) = \frac{ |c_i|! }
                            { \prod_a c_{ia}! }
                       \frac{ \prod_a \Gamma \left( c_{ia} + \alpha_{ia} \right) }
                            { \Gamma ( |c_i + \alpha_k| ) }
                       \frac{ \Gamma ( |\alpha_k| ) }
                            { \prod_a \Gamma \left( \alpha_{ka} \right) }
\]

\mono{esl\_dirichlet\_logpdf\_c()} calculates $\log P(c_i \mid \alpha_k)$.

The conjugate gradient descent code works with unconstrained
real-valued parameters. The Dirichlet parameters $\alpha_{ka}$ are
constrained to $>0$, and mixture coefficients $q_k$ are constrained to
$>0$ and $\sum_k q_k = 1$. Define a change of variables in terms of
unconstrained parameters $\lambda_k$ for the mixture coefficients and
$\beta_{ka}$ for Dirichlet parameters:

\begin{eqnarray*}
  q_k          & = & \frac{ e^{\lambda_k} } { \sum_m e^{\lambda_m} } \\
  \alpha_{ka}  & = & e^{\beta_{ka}}
\end{eqnarray*}

After variable substitution, partial differentiation w.r.t. the
unconstrained parameters, and substituting back the original
parameters, we have for the mixture coefficients:

\[
  \frac{\partial L}{\partial \lambda_k} = \sum_i P(k \mid \theta, c_i) - q_k
\]

i.e., the difference between the posterior probability of component
$k$ $P(k \mid \theta, c_i)$, calculated by \mono{mixdchlet\_postq()},
and its prior $q_k$.

For the Dirichlet parameters:

\[
\frac{\partial L}{\partial \beta_{ka}}  =  \sum_i
 \alpha_{ka} P(k \mid \theta, c_i)
    \left( \Psi \left( c_{ia} + \alpha_{ka} \right)
        -  \Psi \left( | c_i | + | \alpha_k | \right)
        +  \Psi \left( | \alpha_k | \right)
        -  \Psi \left( \alpha_{ka} \right)
    \right)
\]

$\Psi(x)$ is the digamma function $\frac{d}{dx} \log \Gamma(x) =
\frac{\Gamma'(x)}{\Gamma(x)}$, for $x > 0$, implemented by
\mono{esl\_stats\_Psi()}.

The Easel conjugate gradient descent optimizer is a minimizer, not a
maximizer.  The implementation provides the negative log likelihood
and the negative gradient to the CG routine.

\end{document}


\documentclass[main.tex]{subfiles}
\begin{document}

\section*{3 October 2019}

Marco Peloso, \url{marco.peloso@pd.infn.it}

\section{Special relativity}

\begin{definition}
    An inertial frame is one in which Newton's laws hold: a free body moves with acceleration \(a^{i} = 0\).
\end{definition}

Newton's first law establishes the \emph{existence} of inertial frames.

\begin{proposition}
    The frames \(O\) and \(O'\) are both inertial frames iff \(O'\) moves with constant velocity wrt \(O\).
\end{proposition}

\begin{proposition}
    Coordinate transformations between inertial frames are Lorentz boosts, which in some coordinate frame can be written as
    %
    \begin{subequations}
    \begin{align}
      t' &= \gamma_v \qty(t - \frac{vx}{c^2})  \\
      x' &= \gamma_v \qty(x - vt)  \\
      y' &= y \\
      z' &= z\,,
    \end{align}
    \end{subequations}
    %
    where \(\gamma_v = 1/ \sqrt{1 - v^2 / c^2} \).
\end{proposition}

If \(v \ll c\), so \(v/c \sim 0\), they simplify to the identity for \(t\), \(y\), \(z\) and \(x' = x -vt\): these are Galilean transformations.

If we have two events, \(x^\mu\) and \(y^\mu\), they occur with some time and space separation \(\Delta x^\mu = x^\mu - y^\mu\). We can compute \(\Delta s^2 = \eta_{\mu\nu} \Delta x^\mu \Delta x^\nu \), where
%
\begin{equation}
  \eta_{\mu \nu} = \diag{-c^2, 1, 1, 1} \,.
\end{equation}

\begin{proposition}
Under Lorentz transformations \(\Delta s^2\) is invariant.
\end{proposition}

We can classify separations between events as
%
\begin{itemize}
    \item time-like when \(\Delta s^2 <0\);
    \item null-like when \(\Delta s^2 =0\);
    \item space-like when \(\Delta s^2 >0\).
\end{itemize}

We can draw spacetime diagrams. A light cone is the set of points which are null-like separated from a select point. Things can be only causally related to events inside the light-cone, with \(\Delta s^2 \leq 0\).

\subsection{Time dilation}

Take two events which occur at the same location for \(O'\). In the primed frame they will have coordinates \(x^{\mu} = (t_0, x_0)\) and \(y^\mu = (t_1, x_0)\).

\begin{definition}
    The \emph{proper time} between these two events is \(t_1 - t_0 \defeq \Delta \tau\).
\end{definition}

We now see that \(\Delta s'\,^2 = -c^2 \Delta \tau^2\). Then, any other observer will see the same  \(\Delta s^2 = - c^2 \Delta t^2 + \Delta x^2 = \Delta s'\,^2\).

This directly implies that \(\Delta \tau \leq \Delta t\) for any observer, since \(\Delta \tau^2 = \Delta t^2 - \Delta x^2 / c^2\). This effect is called \emph{time dilation}.

By how much exacly is time dilated? Of course \(\Delta x = v \Delta t\), therefore \(\Delta t = \gamma_v \Delta \tau\).

This effect explains a peculiar phenomenons: certain particles in the upper atmosphere decay into muons, which have a very short half-life. So short, in fact, that if we did not account for special relativity we'd expect to see next to none at the surface, since by the time they got here they would have alredy gone through several halving times.
However, we must apply the rule of relativistic time dilation: the muons are travelling very fast towards the ground, therefore in the ground's frame of reference their time passes slower, allowing them to decay slower.
So, a significant fraction of them arrives at the ground.

Inverse Lorentz transformation have the same expression as direct ones, but with \(v \rightarrow -v\).
This can be proved both mathematically by solving the equations and phisically by reasoning about their meaning. There is no preferential inertial frame.

A Lorentz transformation can be written in matrix form in the \((ct, x)\) plane as:

\begin{equation}
  \Lambda = \begin{bmatrix}
    \gamma & -\gamma \beta \\
    -\gamma \beta & \gamma
  \end{bmatrix}
  = \begin{bmatrix}
  \cosh \theta & -\sinh \theta \\
    -\sinh \theta &  \cosh \theta
  \end{bmatrix}
\end{equation}
%
where we introduced the notation \(\beta = v / c\).

The second equation is justified by the fact that there is an angle \(\theta\) such that \(\gamma = \cosh \theta\) and \(\gamma \beta = \sinh \theta\): the angle \(\theta\) will be \(\theta = \tanh^{-1} \qty(v/c)\).
This is true because \(\gamma^2 - \beta^2 \gamma^2 = 1\), which is the same law that the hyperbolic functions obey: \(\cosh^2 x - \sinh^2 x =1 \) holds for any \(x\).

After a boost the \(ct'\) and \(x'\) axes are rotated into, respectively, the lines \(ct=x/\beta\) and \(ct = \beta x\): this comes directly from the transformation law.
The \(ct'\) axis is defined by the equation \(x' =0\), which in the transformed coordinates \(ct\) and \(x\) reads \(\gamma (x - \beta ct) = 0\), or \(ct  = x/ \beta \).

Similarly \(ct' = 0\) in the new coordinates reads \(\gamma (ct - \beta x)=0\), or  \(ct = \beta x\).

The axes are rotated by an angle which can approach \(\pi /4\) but never reach it, since its tangent is defined by \(\beta \), which can never reach 1.

\end{document}


\chapter{Evaluation}

In this section, we first categorize \ematching patterns into three categories. Next, we evaluate our prototype of relational \ematching with two preliminary experiments, choosing patterns from each of the categories, and discussed our progress on evaluating full-system benchmarks. More experiment will be presented in the forthcoming paper.

To investigate the kinds of conjunctive queries an \ematching pattern would generate and its performance implication on the join algorithms, we categorize the \ematching patterns into three categories according to the kinds of conjunctive queries they generate:
\begin{enumerate}
    \item Linear patterns: this is the simplest form of patterns, where no variables occur more than once in the pattern. Examples of this category include $f(\alpha, \beta)$ and $f(g(\alpha),g(\beta))$).
    \item Non-linear acyclic patterns: we define non-linear acyclic patterns to be patterns that are not linear and satisfy that every multi-occurrence of a variable must occur between \enodes that has ``distance'' $\leq 1$ with each other. Examples of this category include  $f(\alpha,g(\alpha))$ and $f(f(\alpha,\beta), \alpha)$.
    \item Cyclic patterns: we define all other patterns to be cyclic patterns. Examples include $f(g(\alpha), g(\alpha))$ and $f(f(\alpha,\beta), g(\beta))$). Note that \ematching patterns of this category are always reduced to cyclic queries.
\end{enumerate}

All three categories of patterns exist in real-world applications like equality saturation.
For example, the search patterns for commutative law (e.g., $a+b$) and associative law (e.g., $(a+b)+c$) is linear, and the search patterns for the distributive law (e.g., $a\times b+a\times c$) is cyclic, while that for the rule for reciprocal (e.g., $x\times (1/x)$) is non-linear acyclic.

We choose two benchmarks from the \egg's test suites, namely the math test suite, which saturates mathematical expressions, and the lambda test suite, which saturates lambda calculus terms. Both of them are representative of a standard application of equality saturation. For example, the math test suite has many overlapping rules with Herbie \citep{herbie}, an application of equality saturation in the domain of floating-point arithmetic.

We did two preliminary experiments. The first experiment tries to understand the asymptotic speedup of relational \ematching. In this experiment, we manually write the implementation for relational \ematching for three patterns, which are representative of linear patterns, non-linear acyclic patterns, and cyclic patterns. We benchmark against the \ematching algorithm in \egg, which implements the backtracking-based \ematching algorithm described in \citet{efficient-ematching}\footnote{In fact, \egg does not implement all of the instructions of the backtracking virtual machine described in \citet{efficient-ematching}, since some of the instructions and their compilation are very complicated.}, on different \egraph sizes.

\begin{figure}
    \centering
    \includegraphics[width=\linewidth]{figures/egdb.png}
    \caption{Speedup of relational \ematching over backtracking-based \ematching algorithms}
    \label{fig:bench1}
\end{figure}

The result is shown in Figure \ref{fig:bench1}. On both cyclic and non-linear acyclic case, our relational \ematching achieves asymptotically better performance, up to 426$\times$, over backtracking-based \ematching by taking advantages of the equality constraints. In the linear
case, because no variable occurs more than once, relational \ematching
achieves similar performance as the backtracking-based \ematching up to a constant factor.\footnote{Note that the comparison here is between the handwritten relational \ematching, which is compiled, and the \ematching engine in \egg, which is interpreted, because this experiment is performed for the PLDI SRC, when we have not developed a fully working version of relational \ematching inside \egg. Therefore, the comparison is not perfectly apple-to-apple. However, we can still see the asymptotic trends.}

\begin{figure}
\vspace{-4em}
     \centering
     \begin{subfigure}[b]{0.8\textwidth}
         \centering
         \includegraphics[width=\textwidth]{figures/bench2-1.png}
         \caption{}
         \label{bench2-1}
     \end{subfigure}
     \\
     \begin{subfigure}[b]{0.8\textwidth}
         \centering
         \includegraphics[width=\textwidth]{figures/bench2-0.png}
         \caption{}
         \label{bench2-0}
     \end{subfigure}
     \caption{(a) Speedup of relationl \ematching over backtracking-based \ematching on microbenchmarks without index building time. (b) Speedup of relationl \ematching over backtracking-based \ematching on microbenchmarks with index building time.}
     \label{bench2}
\end{figure}

Next, we compare the performance of relational \ematching as we implemented in \egg against \egg's original \ematching implementation on several benchmarks. The result is presented with two figures in Figure \ref{bench2}. Figure~\ref{bench2-1} only compares the enumeration procedure of backtracking-based \ematching to that of relational \ematching, excluding time that may need to build the index before performing generic join, which are shared among different patterns, and Figure~\ref{bench2-1} makes the same comparison but includes the index building time.

According to Figure~\ref{bench2-1}, relational \ematching achieves substantial speedups for complex patterns. However, on some simple patterns, relational \ematching does not achieve a significant speedup compared to backtracking-based \ematching algorithms, and sometimes relational \ematching is slower.
Most of such patterns are linear patterns like \texttt{(+ (+ ?a ?b) ?c)} and \texttt{(fix ?v ?e)}. In particular, patterns like \texttt{(fix ?v ?e)} are equivalent to a simple enumeration of all $f$-application terms for some function symbol $f$, which we call singleton patterns. The relational \ematching has the most slowdown on several singleton patterns because running singleton patterns is very fast, taking only a few microseconds, so the overhead related to relational \ematching such as query compilation dominates the run time. Still, because they only take a few microseconds, the run time for matching such patterns are not an issue for real applications. Moreover,  note the relational \ematching performs order of magnitude  better on pattern \texttt{(* ?a ?a)}, which is a singleton pattern, as our query optimizer exploits the fact that both children of the *-application node are the same and thus only does a filter over the corresponding relation. In contrast, backtracking-based \ematching without ad hoc handling can only express this as a backtracking process, which has a larger overhead.

Comparing Figure~\ref{bench2-1} against  Figure~\ref{bench2-0}, we observe the time taken for building indices to be significant. Many plans where relational \ematching is faster now becomes slower. This is consistent with the observation made by \cite{eval-wcoj}, where index building is sometimes the bottleneck of the generic join algorithm. Since our current implementation is only a prototype, we are currently working on improving the index building time so that the performance of relational \ematching is able to dominate that of the current implemented \ematching in \egg.

Finally, our next step of experiment is to evaluate relational \ematching on full-system applications that use \egg. We attempted to run Herbie with relational \ematching as the \ematching procedure. However, we are unable to observe any significant speedup by using relational \ematching. We speculate the cause is that the workload of Herbie is running only simple, linear patterns on small \egraphs, so there are no additional equality constraints that relational \ematching could exploit. We are working on evaluating relational \ematching on other applications, such as Tensat \citep{tensat} and Szalinski \citep{2020-pldi-szalinski-cad-eqsat}.


\problemname{Red, Black Cards}
%\illustration{.5}{filename}{Image by \href{url}{Author}}

John is playing a fancy card game with Bowen.
$N$ cards are laid on the table, facing down.
The cards are numbered from $1$ to $N$.
Each card has a color, and is either red or black.
The colors of the cards are unknown to John.
John's goal is to correctly identify the colors of all the cards without looking at any card.
For that, he asks Bowen $Q$ questions.
John's questions are numbered from $1$ to $Q$.
Each question may ask about whether a pair of cards have the same color, or what color a particular card has.
Bowen checks the colors of cards for John according to John's questions.
John thus receives a sequence of $Q$ answers, each being one of the following:

\begin{itemize}
\item {\tt d x y} : Card $x$ and card $y$ have different colors.
\item {\tt s x y} : Card $x$ and card $y$ have a same color.
\item {\tt r x} : Card $x$ has red color.
\item {\tt b x} : Card $x$ has black color.
\end{itemize}

However, since John asks so many questions, Bowen gets tired and sometimes made mistakes in answering the questions.
Fortunately, being a clever player, John can immediately catch Bowen's mistake if there is a contradiction from Bowen's answers.
When that happens, the current answer that results in a contradiction is void and the game proceeds.
The game ends immediately as soon as John gets enough information to identify the colors of all the cards.
The game also ends after $Q$ questions are all answered, even if John did not successfully identify the colors.

In this task, you will go over the questions asked by John and the answers to those questions given by Bowen.
You are to reproduce John's responses during the game.

\section*{Input}
The first line of the input has an integer $T$, the number of games played.\\
Each case has two integer $N, Q$ on the first line.\\
The next $Q$ lines describe John's questions and their answers.
Each line is in one of the four forms given above.\\
The first letter of each line is `{\tt d}', `{\tt s}', `{\tt r}' or `{\tt b}'.
If the letter is `{\tt d}' or `{\tt s}', the question asks about whether a pair of cards have the same color, and has two following integers giving a pair of card numbers.
If the letter is `{\tt r}' or `{\tt b}', the question asks about the color of a single card, and has one following integer giving the number of that card.\\
Duplicate questions may be asked, but it is possible that Bowen gave different answers to a same question (which results in contradiction).
\section*{Output}

There are two types of outputs, {\it response} and {\it result}.\\

If the answer to a question leads to a contradiction or John's success on the game, print a {\it response}:
\begin{itemize}
\item If it is a contradiction, output a single question mark ``{\tt ?}''.
Note that this answer is then ignored and not used for identifying colors.
\item If John can successfully identify the colors of all the cards, output ``{\tt I know}''.
The game then ends immediately and all the following questions are ignored.
\end{itemize}

Print the question number before each {\it response}.
Refer to the sample output for formatting details of question numbers.\\

At the end of the game, print the {\it result} of the game on a new line:
\begin{itemize}
\item If the game ends with John's success, output a string with $N$ letters.
The $i$-th letter is either `{\tt r}' or `{\tt b}' to describe the color of card $i$ being red or black.
\item If John is not able to identify the colors after all the $Q$ questions, output ``{\tt I am not sure}''.
\end{itemize}

\section*{Constraints}
\begin{itemize}
\item $1 \leq T\leq 15$
\item $1 \leq N \leq 10^5$
\item $1 \leq Q \leq 10^5$
\item $1\leq x, y \leq N, x \neq y$ for all the answers to John's questions.
\end{itemize}

\section*{Subtasks}
\begin{itemize}
\item Original constraints
\end{itemize}


\subsubsection{\stid{3.14} ALExa}

\paragraph{Overview}

The ALExa project ({\sl Accelerated Libraries for Exascale}) focuses on
preparing the DTK and Tasmanian libraries for exascale platforms and
integrating these libraries into ECP applications.  These libraries deliver
capabilities identified as needs of ECP applications: (1) the ability to
transfer computed solutions between grids with differing layouts on parallel
accelerated architectures, enabling multiphysics projects to seamlessly
combine results from different computational grids to perform their required
simulations (DTK); and
%
(2) the ability to construct fast and memory efficient surrogates to large
scale engineering models with multiple inputs and large number of outputs,
enabling uncertainty quantification (both forward and inverse) as well as
optimization and efficient multi-physics simulations in projects such as
ExaStar (Tasmanian).

These capabilities are being developed through ongoing interactions with our
ECP application project collaborators to ensure they will satisfy requirements
of these customers.  The libraries in turn take advantage of other ECP/SW
capabilities currently in development, including Trilinos and ForTrilinos,
Kokkos and SLATE.  The final outcome of the ECP project will be a set of
libraries deployed to facilities and also made broadly available as part of
the xSDK4ECP project.

{\bf DTK} (Data Transfer Kit)

{\it Purpose:} Transfers computed solutions between grids with differing
layouts on parallel accelerated architectures.

{\it Significance:} Coupled applications frequently have different grids with
different parallel distributions; DTK is able to transfer solution values
between these grids efficiently and accurately.

{\it Mesh and mesh-free interpolation capabilities:} multivariate data
interpolation between point clouds and grids; compactly supported radial basis
functions; nearest-neighbor and moving least square implementations; support
for standard finite-element shape functions and user-defined interpolants;
common applications include conjugate heat transfer, fluid structure
interaction, and mesh deformation.

{\it Performance portable search capabilities:} shared memory and GPU
implementations of spatial tree construction; shared memory and GPU
implementations of various spatial tree queries; MPI front-end for
coordinating distributed spatial searches between sets of geometric objects
with different decompositions; communication plan generation based on spatial
search results.

{\it URL:} https://github.com/ORNL-CEES/DataTransferKit

{\bf Tasmanian} (Toolkit for Adaptive Stochastic Modeling and Non-Intrusive
Approximation)

{\it Purpose:} Constructs efficient surrogate models for high dimensional
problems and performs parameter calibration and optimization geared towards
applications in uncertainty quantification (UQ).

{\it Significance:} UQ pertains to the statistical properties of the output
from a complex model with respect to variability in multiple model inputs;
large number of simulations are required to compute reliable statistics which
is prohibitive when dealing with computationally expensive engineering
models. A surrogate model is constructed from a moderate set of simulations
using carefully chosen input values; analysis can then be performed on the
efficient surrogate.

{\it Sparse grids capabilities:} surrogate modeling and design of experiments
(adaptive multi-dimensional interpolation); reduced (lossy) representation of
tabulated scientific data; high dimensional numerical quadrature; data mining
and manifold learning.

{\it DiffeRential Evolution Adaptive Metropolis (DREAM) capabilities:}
Bayesian inference; parameter estimation/calibration; model validation.
global optimization and optimization under uncertainty.

{\it URL:} http://tasmanian.ornl.gov

\paragraph{Key Challenges}

\indent

{\bf DTK:} General data transfer between grids of unrelated applications
requires many-to-many communication which is increasingly challenging as
communication to computation ratios are decreasing on successive HPC systems.
Search procedures to locate neighboring points and mesh cells require tree
search methods difficult to optimize on modern accelerated architectures due
to vector lane or thread divergence. Maintaining high accuracy for the
transfer requires careful attention to the mathematical properties of the
interpolation methods and is highly application-specific.

{\bf Tasmanian:} Complex models usually have significant variability in
execution time for different model inputs, which leads to massive down-time
when employing the standard fork-join adaptive sparse grid algorithms.  After
the surrogate has been constructed, collecting the samples for statistical
analysis (or multi-physics simulations) requires a massive number of basis
evaluations and many sparse and dense linear operations.

\paragraph{Solution Strategy}

\nobreak

\indent

{\bf DTK:} State-of-the-art, mathematically rigorous methods are used in DTK
to preserve accuracy of interpolated solutions.  Algorithms are implemented in
a C++ code base with extensive unit testing on multiple platforms.  Trilinos
packages are used to support interpolation methods.  Kokkos is used to achieve
performance portability across accelerated platforms.

{\bf Tasmanian:} Implement asynchronous DAG-based sparse grids construction
methods that preserve the convergence properties of the fork-join algorithms
but are insensitive to fluctuations in model simulation time.  Port the basis
evaluations and linear algebra to the GPU accelerators, and leverage the
SLATE/MAGMA capabilities to ensure performance portability across relevant
platforms.

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

\paragraph{Recent Progress}

\indent

{\bf DTK:} Extensive optimization work has yielded significant performance
improvements on accelerated and heterogeneous architectures. Work with partner
application ExaAM (WBS 2.2.1.05) created a preliminary multiphysics driver
capability for additive manufacturing simulations.

\begin{figure}[htb]
        \centering
        \includegraphics[width=3.0in]{projects/2.3.3-MathLibs/2.3.3.14-ALExa-ForTrilinos/dtk-gpu}
        \caption{\label{fig:dtk-gpu}DTK search performance relative to Boost with Intel Xeon E5-2698 and Nvidia P100. 10M points randomly distributed in a unit cube, 1M queries. The time is in seconds. Speedup in bold.}
\end{figure}

{\bf Tasmanian:} The infrastructure of Tasmanian has been upgraded to support
the broader ECP focus of the work.  GPU acceleration of sparse grid surrogates
has been implemented.  Tasmanian recently enabled the ExaStar project to
reduce the size of a large-memory table of neutrino opacities by 100X while
still preserving accuracy.

\begin{figure}[htb]
        \centering
        \includegraphics[width=1.5in]{projects/2.3.3-MathLibs/2.3.3.14-ALExa-ForTrilinos/tasmanian-gpu}
        \caption{\label{fig:tasmanian-gpu}Tasmanian approximation (right) of neutrino capacities (left).}
\end{figure}

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

\paragraph{Next Steps}

\indent

{\bf DTK:} DTK search and communication capabilities will deployed in a new,
lightweight library, ArborX, to provide these ECP investments to a broader
user base.

{\bf Tasmanian:} Work will continue with the development of the asynchronous
construction methods that exploit the native sparse grids DAG hierarchy.

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


\documentclass[twocolumn]{scrartcl}

\usepackage[utf8]{inputenc}

\usepackage{fixltx2e}

% Step environment
% <https://tex.stackexchange.com/a/12943/13262>
\usepackage{amsthm}
\newtheorem*{remark}{Remark}
%
\newtheoremstyle{named}{}{}{\itshape}{}{\bfseries}{.}{.5em}{\thmnote{#1 }#3}
\theoremstyle{named}
\newtheorem*{step}{Step}

\usepackage{microtype}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage{booktabs}
\usepackage{tabularx}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\usepackage{siunitx}

\newcommand\mytitle{Algorithmic improvements for the CIECAM02 and CAM16 color appearance models}
\newcommand\myauthor{Nico Schlömer}

\usepackage[
  pdfencoding=unicode,
  ]{hyperref}
\hypersetup{
  pdfauthor={\myauthor},
  pdftitle={\mytitle}
}

% <https://tex.stackexchange.com/a/43009/13262>
\DeclarePairedDelimiter\abs{\lvert}{\rvert}%

\usepackage[T1]{fontenc}
\usepackage{newtxtext}
\usepackage{newtxmath}

% degree symbol
\usepackage{gensymb}

% % <https://tex.stackexchange.com/a/413899/13262>
% \usepackage{etoolbox}
% \makeatletter
% \long\def\etb@listitem#1#2{%
%   \expandafter\ifblank\expandafter{\@gobble#2}
%     {}
%     {\expandafter\etb@listitem@i
%      \expandafter{\@secondoftwo#2}{#1}}}
% \long\def\etb@listitem@i#1#2{#2{#1}}
% \makeatother

% Okay. Don't use biblatex/biber for now. There are breaking changes in every
% revision, and we'd have to stick to the exact version that arxiv.org has,
% otherwise it's error messages like
% ```
% Package biblatex Warning: File 'main.bbl' is wrong format version
% - expected 2.8.
% ```
% \usepackage[sorting=none]{biblatex}
% \bibliography{bib}

\usepackage{amsmath}
\DeclareMathOperator{\sign}{sign}

\usepackage{bm}
\newcommand\rgb{\bm{R}}

\title{\mytitle\footnote{The LaTeX sources of this article are on \url{https://github.com/nschloe/note-on-cam16}}}
\author{\myauthor}

\begin{document}

\maketitle
\begin{abstract}
  This note is concerned with the CIECAM02 color appearance model and its
  successor, the CAM16 color appearance model. Several algorithmic flaws are
  pointed out and remedies are suggested. The resulting color model is
  algebraically equivalent to CIECAM02/CAM16, but shorter, more efficient, and
  works correctly for all edge cases.
\end{abstract}

\section{Introduction}

The CIECAM02 color appearance model~\cite{ciecam02} has attracted much
attention and was generally thought of as a successor to the ever so popular
CIELAB color model. However, it was quickly discovered that CIECAM02 breaks
down for certain input values. A fair number of research articles suggests
fixes for this behavior, most of them by modifying the first steps of the
forward model. Luo and Li give an overview of the suggested
improvements~\cite{ciecam02-recent}; see references therein.  Most recently,
Li and Luo~\cite{cam16} gave their own suggestion on how to best
circumvent the breakdown~\cite{cam16}. The updated algorithm differs from the
original CIECAM02 only in the first steps of the forward model.

It appears that that the rest of the algorithm has not received much attention
over the years. In both CIECAM02 and its updated version CAM16, some of the
steps are more complicated than necessary, and in edge cases lead to break
downs once again. The present document describes those flaws and suggests
improvements (Section~\ref{sec:ff}). The resulting model description
(Section~\ref{sec:full}) is entirely equivalent to the CIECAM02/CAM16, but is
simpler -- hence faster and easier to implement -- and works in all edge
cases.

All findings in this article are implemented in the open-source software package
colorio~\cite{colorio}.

\section{Flaws and fixes}\label{sec:ff}

This section describes the flaws of CAM16 and suggests fixes for them. Some of
them are trivial, others are harder to see. All listed steps also appear in the
CIECAM02 color appearance model and trivially apply there.

\subsection{Step 3, forward model}

The original Step 3 of the forward model reads

\begin{step}[3]
Calculate the postadaptation cone response
(resulting in dynamic range compression).
\[
  R_a = 400 \frac{\left(\frac{F_L R_c}{100}\right)^{0.42}}{\left(\frac{F_L R_c}{100}\right)^{0.42} + 27.13} + 0.1
\]
If $R_c$ is negative, then
\[
  R_a = -400 \frac{\left(\frac{-F_L R_c}{100}\right)^{0.42}}{\left(\frac{-F_L R_c}{100}\right)^{0.42} + 27.13} + 0.1
\]
and similarly for the computations of $G_a$ and $B_a$.
\end{step}

If the $\sign$ operator is used here as it is used later in step 5 of the
inverse model, the above description can be shortened.

Furthermore, the term $0.1$ is added here, but in all of the following steps in
which $R_a$ is used -- except the computation of $t$ in Step 9 --, it cancels
out algebraically.  Unfortunately, said cancellation is not always exact when
computed in floating point arithmetic. Luckily, the adverse effect of such
rounding errors is rather limited here. The results will only be distorted for
very small input values, e.g., $X=Y=Z=0$; see Table~\ref{tab:zero}. For the
sake of consistency, it is advisable to include the term $0.1$ only in the
computation of $t$ in Step 9:
\[
  R'_a = 400 \sign(R_c) \frac{{\left(\frac{F_L \abs{R_c}}{100}\right)}^{0.42}}{{\left(\frac{F_L \abs{R_c}}{100}\right)}^{0.42} + 27.13}.
\]

\begin{table}\centering
  \begin{tabularx}{\linewidth}{XXX}
  \toprule
          & with fixes & without\\
  \midrule
    $J$ & \texttt{0.0} & \texttt{3.258e-22}\\
    $C$ & \texttt{0.0} & \texttt{4.071e-24}\\
    $h$ & \texttt{0.0} & \texttt{0.0}\\
    $Q$ & \texttt{0.0} & \texttt{2.233e-10}\\
    $M$ & \texttt{0.0} & \texttt{2.943e-24}\\
    $s$ & \texttt{0.0} & \texttt{1.148e-05}\\
  \bottomrule
\end{tabularx}
\caption{CAM16 values upon input $X=Y=Z=0$ with and without the fixes in
  this article.  The exact solutions are zeros for every
  entry.}\label{tab:zero}
\end{table}

\subsection{Linear combinations, forward model}

In the forward model, four linear combinations of $R'_a$, $G'_a$, and $B'_a$
have to be formed. They can conveniently be expressed as the matrix-vector
multiplication
\[
  \begin{pmatrix}
    p'_2\\[0.5ex]
    a\\[0.5ex]
    b\\[0.5ex]
    u
  \end{pmatrix}
  \coloneqq
  \begin{pmatrix}
    2 & 1 & \tfrac{1}{20}\\[0.5ex]
    1 & -\tfrac{12}{11} & \tfrac{1}{11}\\[0.5ex]
    \tfrac{1}{9} & \tfrac{1}{9} & -\tfrac{2}{9}\\[0.5ex]
    1 & 1 & \tfrac{21}{20}
  \end{pmatrix}
  \begin{pmatrix}
    R'_a\\G'_a\\B'_a
  \end{pmatrix}
\]
which on many platforms can be computed significantly faster than four
individual dot-products.
The last variable $u$ is used in the computation of $t$ in step 9.

\subsection{Step 9, forward model}

\begin{step}[9]
  Calculate the correlates of [\dots] saturation ($s$).
  \[
    s \coloneqq 100 \sqrt{M/Q}.
  \]
\end{step}
This expression is not well-defined if $Q=0$, a value occurring if the
input values are $X=Y=Z=0$. When making use of the definition of $M$ and $Q$,
one gets to an expression for $s$ that is well-defined in all cases:
\begin{align}
  \label{eq:alpha}
  \alpha&\coloneqq t^{0.9} {(1.64-0.29^n)}^{0.73},\\
  \nonumber
  s &\coloneqq 50 \sqrt{\frac{c\alpha}{A_w + 4}}.
\end{align}

\subsection{Steps 2 and 3, inverse model}

\begin{step}[2]
Calculate $t$, $e_t$, $p_1$, $p_2$, and $p_3$.
\begin{align*}
  t &= {\left(\frac{C}{\sqrt{\frac{J}{100}} {(1.64 - 0.29^n)}^{0.73}}\right)}^\frac{1}{0.9},\\
  e_t &= \frac{1}{4} \left[\cos(h'\pi/180\degree + 2) + 3.8\right],\\
  p_1 &= \frac{50000}{13} N_c N_{cb} e_t \frac{1}{t},\\
  p_2 &= \frac{A}{N_{bb}} + 0.305,\\
  p_3 &= \frac{21}{20}.
\end{align*}
\end{step}

\begin{step}[3]
Calculate $a$ and $b$.
If $t=0$, then $a=b=0$ and go to Step 4.
In the next computations be sure transform $h$ from degrees to radians before
calculating $\sin(h)$ and $\cos(h)$: If $\abs{\sin(h)} \ge \abs{\cos(h)}$
then
\begin{align*}
  p_4 &= \frac{p_1}{\sin(h)},\\
  b &= \frac{p_2 (2+p_3) \frac{460}{1403}}{p_4 + (2+p_3) \frac{220}{1403} \frac{\cos(h)}{\sin(h)} - \frac{27}{1403} + p_3 \frac{6300}{1403}},\\
  a &= b \frac{\cos(h)}{\sin(h)}.
\end{align*}
If $\abs{\cos(h)} > \abs{\sin(h)}$ then
\begin{align*}
  p_5 &= \frac{p_1}{\cos(h)},\\
  a &= \frac{p_2 (2+p_3) \frac{460}{1403}}{%
    p_5
    + (2+p_3) \frac{220}{1403} -
    \left(\frac{27}{1403}  - p_3 \frac{6300}{1403}\right) \frac{\sin(h)}{\cos(h)}
  },\\
  b &= a \frac{\sin(h)}{\cos(h)}.
\end{align*}
\end{step}

Some of the complications in this step stem from the fact that the variable $t$
might be $0$ in the denominator of $p_1$. Likewise, the distinction of cases in $\sin(h)$ and $\cos(h)$ is necessary
to avoid division by $0$ in $a$ and $b$.

It turns out that both of these problems can be avoided quite elegantly.
Consider, in the case $\abs{\sin(h)} \ge \abs{\cos(h)}$:
\begin{align*}
  p'_1 &\coloneqq \frac{50000}{13} N_c N_{cb} e_t,\\
  b &= \frac{p_2 (2+p_3) \frac{460}{1403}}{\frac{p'_1}{t\sin(h)} + (2+p_3) \frac{220}{1403} \frac{\cos(h)}{\sin(h)} - \frac{27}{1403} + p_3 \frac{6300}{1403}}\\
   &= \frac{t \sin(h) p_2 (2+p_3) \frac{460}{1403}}{p'_1 + t (2+p_3) \frac{220}{1403} \cos(h) + t \sin(h) \frac{6588}{1403}}\\
   &= \frac{23 t \sin(h) p_2}{23 p'_1 + 11 t \cos(h) + 108 t \sin(h)},
\end{align*}
and
\[
  a = \frac{23 t \cos(h) p_2}{23 p'_1 + 11 t \cos(h) + 108 t \sin(h)}.
\]
Conveniently, the exact same expressions are retrieved in the case
$\abs{\cos(h)} > \abs{\sin(h)}$. These expressions are always well-defined since
\begin{multline*}
  23 p'_1 + 11 t \cos(h) + 108 t \sin(h)\\
  = \frac{23 p'_1 p_2}{R'_a + G'_a + \tfrac{21}{20}B'_a + 0.305}
  > 0.
\end{multline*}

In the algorithm, the value of $t$ can be retrieved via
$\alpha$~\eqref{eq:alpha} from the input variables. Indeed, if the saturation
correlate $s$ is given, one has
\[
  \alpha \coloneqq {\left(\frac{s}{50}\right)}^2 \frac{A_w+4}{c};
\]
if $M$ is given, one can compute $C\coloneqq M / F_L^{0.25}$ and then
\[
\alpha\coloneqq\begin{dcases*}
  0 &if $J=0$,\\
  \frac{C}{\sqrt{J/100}}&otherwise.
\end{dcases*}
\]
It is mildly unfortunate that one has to introduce a case distinction for
$J=0$ here, but this is an operation that can still be performed at reasonable
efficiency.

\begin{figure}
\input{perf.tex}
  \caption{Performance comparison of the conversion from CAM16 to XYZ (with
  $J$, $C$, and $h$), implemented in colorio~\cite{colorio}. The suggested
  improvements in the inverse model lead to a speed-up of about 5\%.}
\end{figure}

\appendix
\section{Full model\label{sec:full}}

For the convenience of the reader, both forward and inverse steps of the
improved CAM16 algorithm are given here. The wording is taken from~\cite{cam16}
where applicable.
The steps that differ from the original model are marked with an asterisk~(*).

As an abbreviation, the bold letter $\rgb$ is used whenever the equation applies
to $R$, $G$, and $B$ alike.

\paragraph{Illuminants, viewing surrounds set up and background
parameters}
(See the note at the end of Part 2 of Appendix B of~\cite{cam16} for determining
all parameters.)

\begin{itemize}
  \item Adopted white in test illuminant: $X_w$, $Y_w$, $Z_w$
  \item Background in test conditions: $Y_b$
  \item Reference white in reference illuminant:
    $X_{wr}=Y_{wr} = Z_{wr}=100$, fixed in the model
  \item Luminance of test adapting field (\si{\candela\per\meter\squared}): $L_A$.
$L_A$ is computed using
    \[
      L_A = \frac{E_W}{\pi} \frac{Y_b}{Y_W} = \frac{L_W Y_b}{Y_W},
    \]
  where $E_W =\pi L_W$ is the illuminance of reference white in \si{\lux};
    $L_W$ is the luminance of reference white in
    \si{\candela\per\meter\squared}; $Y_b$ is the luminance factor of the
    background; and $Y_w$ is the luminance factor of the reference white.

  \item Surround parameters are given in Table~\ref{tab:surround}:
To determine the surround conditions see the note at the
    end of Part 1 of Appendix A of~\cite{cam16}.
\item $N_c$ and $F$ are modelled as a function of $c$, and their values
  can be linearly interpolated, using the data from~\ref{tab:surround}.
\end{itemize}

Let $M_{16}$ be given by
\[
  M_{16} \coloneqq \begin{pmatrix}
    0.401288  & 0.650173 & -0.051461\\
    -0.250268 & 1.204414 & 0.045854\\
    -0.002079 & 0.048952 & 0.953127
  \end{pmatrix}.
\]

\subsection{Forward model}

\begin{step}[0*]
Calculate all values/parameters which are independent
of the input sample.
\begin{align*}
  &\begin{pmatrix}R_w\\G_w\\B_w\end{pmatrix}
    = M_{16}
  \begin{pmatrix}X_w\\Y_w\\Z_w\end{pmatrix},\\
  &D = F \left[1 - \tfrac{1}{3.6} \exp\left(\tfrac{-L_a-42}{92}\right)\right].
\end{align*}
If $D$ is greater than one or less than zero, set it to one or zero,
respectively.
\begin{align*}
  &D_{\rgb} = D\frac{Y_W}{\rgb_W} -1 + D,\\
  &k = \frac{1}{5L_A + 1},\\
  &F_L = k^4 L_A + 0.1 {(1-k^4)}^2 {(5L_A)}^{1/3},\\
  &n = \frac{Y_b}{Y_W},\\
  &z = 1.58 + \sqrt{n},\\
  &N_{bb} = \frac{0.725}{n^{0.2}},\\
  &N_{cb} = N_{bb},\\
  &\rgb_{wc} = D_{\rgb} \rgb_w,\\
  &\rgb_{aw} = 400
  \frac
  {{\left(\frac{F_L \rgb_{wc}}{100}\right)}^{0.42}}
  {{\left(\frac{F_L \rgb_{wc}}{100}\right)}^{0.42} + 27.13},\\
  &A_w = \left(2R_{aw} + G_{aw} + \tfrac{1}{20} B_{aw}\right) \cdot N_{bb}.
\end{align*}
\end{step}

\begin{table}\centering
  \begin{tabularx}{\linewidth}{XXXX}
  \toprule
          & $F$ & $c$   & $N_c$\\
  \midrule
  Average & 1.0 & 0.69  & 1.0\\
  Dim     & 0.9 & 0.59  & 0.9\\
  Dark    & 0.8 & 0.525 & 0.8\\
  \bottomrule
\end{tabularx}
  \caption{Surround parameters.}\label{tab:surround}
\end{table}

\begin{table}\centering
  \begin{tabularx}{\linewidth}{XXXXXX}
  \toprule
        & Red   & Yellow & Green & Blue   & Red\\
  \midrule
  $i$   & 1     & 2     & 3      & 4      & 5\\
  $h_i$ & 20.14 & 90.00 & 164.25 & 237.53 & 380.14\\
  $e_i$ & 0.8   & 0.7   & 1.0    & 1.2    & 0.8\\
  $H_i$ & 0.0   & 100.0 & 200.0  & 300.0  & 400.0\\
  \bottomrule
\end{tabularx}
  \caption{Unique hue data for calculation of hue quadrature.}\label{table:hue}
\end{table}

\begin{step}[1]
Calculate `cone' responses.
\[
\begin{pmatrix}R\\G\\B\end{pmatrix}
= M_{16} \begin{pmatrix}X\\Y\\Z\end{pmatrix}
\]
\end{step}

\begin{step}[2]
Complete the color adaptation of the illuminant in
the corresponding cone response space (considering various
luminance levels and surround conditions included in $D$, and
hence in $D_R$, $D_G$, and $D_B$).
\[
  \rgb_c = D_{\rgb} \cdot \rgb
\]
\end{step}

\begin{step}[3*]
Calculate the modified postadaptation cone response
(resulting in dynamic range compression).
\[
  \rgb'_a = 400 \sign(\rgb_c)
    \frac
    {{\left(\frac{F_L \abs{\rgb_c}}{100}\right)}^{0.42}}
    {{\left(\frac{F_L \abs{\rgb_c}}{100}\right)}^{0.42} + 27.13}.
\]
\end{step}

\begin{step}[4*]
Calculate Redness--Greenness ($a$), Yellowness--Blueness ($b$) components,
  hue angle ($h$), and auxiliary variables ($p'_2$, $u$).
\begin{align*}
  \begin{pmatrix}
    p'_2\\[0.5ex]
    a\\[0.5ex]
    b\\[0.5ex]
    u
  \end{pmatrix}
  &\coloneqq
  \begin{pmatrix}
    2 & 1 & \tfrac{1}{20}\\[0.5ex]
    1 & -\tfrac{12}{11} & \tfrac{1}{11}\\[0.5ex]
    \tfrac{1}{9} & \tfrac{1}{9} & -\tfrac{2}{9}\\[0.5ex]
    1 & 1 & \tfrac{21}{20}
  \end{pmatrix}
  \begin{pmatrix}
    R'_a\\G'_a\\B'_a
  \end{pmatrix},\\
  % a&\coloneqq R'_a - \tfrac{12}{11} G'_a + \tfrac{1}{11} B'_a\\
  % b&\coloneqq \tfrac{1}{9} R'_a + \tfrac{1}{9} G'_a - \tfrac{2}{9} B'_a\\
  h&\coloneqq \arctan(b/a).
\end{align*}
(Make sure that $h$ is between $0\degree$ and $360\degree$.)
\end{step}

\begin{step}[5]
Calculate eccentricity [$e_t$, hue quadrature composition
($H$) and hue composition ($H_c$)].

Using the following unique hue data in table~\ref{table:hue}, set
$h'= h + 360\degree$ if $h < h_1$, otherwise $h'=h$.
Choose a proper $i\in\{1,2,3,4\}$ so that $h_i\le h' < h_{i+1}$.
Calculate
\[
  e_t = \tfrac{1}{4}
  \left[
    \cos(h'\pi/180\degree + 2) + 3.8
  \right]
\]
which is close to, but not exactly the same as, the eccentricity factor given
in table~\ref{table:hue}.

Hue quadrature is computed using the formula
\[
  H = H_i + \frac{100 e_{i+1} (h'-h_i)}{e_{i+1}(h'-h_i) + e_i (h_{i+1}-h')}
\]
and hue composition $H_c$ is computed according to $H$.  If $i=3$ and $H =
241.2116$ for example, then $H$ is between $H_3$ and $H_4$ (see
table~\ref{table:hue} above). Compute $P_L=H_4-H = 58.7884$; $P_R = H – H_3 =
41.2116$ and round $P_L$ and $P_R$ values to integers $59$ and $41$. Thus,
according to table~\ref{table:hue}, this sample is considered as having 59\%
of green and 41\% of blue, which is the $H$c and can be reported as 59G41B or
41B59G.
\end{step}

\begin{step}[6*]
Calculate the achromatic response
\[
  A\coloneqq p'_2 \cdot N_{bb}.
  \]
\end{step}

\begin{step}[7]
Calculate the correlate of lightness
\[
  J \coloneqq 100 {(A / A_w)}^{cz}.
\]
\end{step}

\begin{step}[8]
  Calculate the correlate of brightness
  \[
    Q \coloneqq \frac{4}{c} \sqrt{\frac{J}{100}} (A_w+4) F_L^{0.25}.
    \]
\end{step}

\begin{step}[9*]
Calculate the correlates of chroma ($C$), colorfulness ($M$), and saturation
  ($s$).
\begin{align*}
  t&\coloneqq \frac{50000/13 N_c N_{cb} e_t \sqrt{a^2 + b^2}}{u + 0.305},\\
  \alpha&\coloneqq t^{0.9} {(1.64 - 0.29^n)}^{0.73},\\
  C&\coloneqq \alpha \sqrt{\frac{J}{100}},\\
  M&\coloneqq C\cdot F_L^{0.25},\\
  s &\coloneqq 50 \sqrt{\frac{\alpha c}{A_w + 4}}.
\end{align*}
\end{step}

\subsection{Inverse model}

\begin{step}[1]
  Obtain $J$, $t$, and $h$ from $H$, $Q$, $C$, $M$, $s$.

  The input data can be different combinations of perceived correlates, that
  is, $J$ or $Q$; $C$, $M$, or $s$; and $H$ or $h$. Hence, the following
  sub-steps are needed to convert the input parameters to the parameters $J$,
  $t$, and $h$.
\end{step}

\begin{step}[1--1]
Compute $J$ from $Q$ (if input is $Q$)
\[
  J\coloneqq 6.25 \frac{cQ}{(A_w+4) F_L^{0.25}}.
\]
\end{step}

\begin{step}[1--2*]
Calculate $t$ from $C$, $M$, or $s$.
\begin{itemize}
  \item If input is $C$ or $M$:
    \begin{align*}
      C &\coloneqq M / F_L^{0.25} \:\text{if input is $M$}\\
      \alpha &\coloneqq \begin{dcases*}
          0 &if $J=0$,\\
          \frac{C}{\sqrt{J/100}}& otherwise.
      \end{dcases*}
    \end{align*}
  \item If input is $s$:
    \[
    \alpha \coloneqq {\left(\frac{s}{50}\right)}^2 \frac{A_w+4}{c}
    \]
\end{itemize}
Compute $t$ from $\alpha$:
\[
  t \coloneqq {\left(\frac{\alpha}{{(1.64 - 0.29^n)}^{0.73}}\right)}^{1/0.9}
\]
\end{step}

\begin{step}[1--3]
Calculate $h$ from $H$ (if input is $H$).
The correlate of hue ($h$) can be computed by using data in
table~\ref{table:hue} in the forward model.
Choose a proper $i\in\{1,2,3,4\}$ such that
$H_i \le H < H_{i+1}$. Then
\[
  h' = \frac{(H-H_i)(e_{i+1}h_i - e_i h_{i+1}) - 100 h_i e_{i+1}}{(H-H_i)(e_{i+1}-e_i) - 100 e_{i+1}}.
\]
Set $h = h' - 360\degree$ if $h' > 360\degree$, and $h=h'$ otherwise.
\end{step}

\begin{step}[2*]
Calculate $e_t$, $A$, $p'_1$, and $p'_2$
\begin{align*}
  e_t &= \tfrac{1}{4} (\cos(h\pi/180\degree + 2) + 3.8),\\
  A &= A_w  {(J/100)}^{1/(cz)},\\
  p'_1 &= e_t \tfrac{50000}{13} N_c N_{cb},\\
  p'_2 &= A / N_{bb}.
\end{align*}
\end{step}

\begin{step}[3*]
Calculate $a$ and $b$
  \begin{align*}
    \gamma &\coloneqq \frac{23 (p'_2+0.305) t}{23 p'_1 + 11 t \cos(h) + 108 t \sin(h)},\\
    a &\coloneqq \gamma \cos(h),\\
    b &\coloneqq \gamma \sin(h).
  \end{align*}
\end{step}

\begin{step}[4]
  Calculate $R'_a$, $G'_a$, and $B'_a$.
  \[
  \begin{pmatrix}
    R'_a\\G'_a\\B'_a
  \end{pmatrix}
  =
  \frac{1}{1403}
  \begin{pmatrix}
    460 & 451 & 288\\
    460 & -891 & -261\\
    460 & -220 & -6300
  \end{pmatrix}
  \begin{pmatrix}
    p'_2\\a\\b
  \end{pmatrix}.
  \]%
\end{step}

\begin{step}[5*]
Calculate $R_c$, $G_c$, and $B_c$,
  \[
  \rgb_c = \sign(\rgb'_a)
  \frac{100}{F_L} {\left(
    \frac{27.13 \abs{\rgb'_a}}{400 - \abs{\rgb'_a}}
    \right)}^{1/0.42}.
  \]
\end{step}

\begin{step}[6]
Calculate $R$, $G$, and $B$ from $R_c$, $G_c$, and $B_c$.
\[
  \rgb = \rgb_c / D_{\rgb}.
\]
\end{step}

\begin{step}[7]
Calculate $X$, $Y$, and $Z$. (For the coefficients of the inverse matrix, see
the note at the end of the appendix B of~\cite{cam16}.)
\[
\begin{pmatrix}X\\Y\\Z\end{pmatrix}
  = M_{16}^{-1}
\begin{pmatrix}R\\G\\B\end{pmatrix}.
\]
\end{step}

% \printbibliography{}
\bibliography{bib}{}
\bibliographystyle{plain}

\end{document}


\documentclass[landscape]{article}

\usepackage{amsmath}
\usepackage{booktabs}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{hyperref}

\title{Statistic Formulas}
\author{RobinXSI}
\date{Juli 2015}
\begin{document}
   \maketitle
   	\section{Introduction}

   		Wichtig beim aufstellen von Hypothesen ist, dass die Auswertung eine statistische Signifikanz haben.

   		Statistical significance means:
		\begin{itemize}
			\item rected the null hypothesis
			\item results are not likely due to chance (sampling error)
		\end{itemize}

		Als Endresultat der Statistik sollten folgendes Dokument erstellt werden können:
		\begin{itemize}
			\item Desciriptive Statistics
				\begin{itemize}
					\item M - Mean
					\item Sd - Standard Deviation
				\end{itemize}
			\item Inferential Statistics
				\begin{itemize}
					\item Hypothesis Test \(\alpha\)
					\begin{itemize}
						\item kind of test, bsp: one-sample t-test
						\item test statistic, bsp: t-value
						\item df - degrees of freedom
						\item p-value
						\item direction of test, bsp: one-tail-test or two-tail-test
					\end{itemize}
					\item APA style
						  \\\(t(df) = X.XX, p = X.XX,\) direction
						  \\bsp: \(t(24) = -2.50, p < .05,\) one-tailed
					\item Confidence intervals
					\begin{itemize}
						\item Confidence leve, bsp: 95\%
						\item Lower Limit
						\item Upper Limit
						\item CI on what?
					\end{itemize}
					\item APA style - CIs
						  \\Confidence interval on the mean difference;
						  \\95\% CI = (4 to 6)
					\item Effect size measures
						\begin{itemize}
							\item \(d\)
							\item \(r^2\)
						\end{itemize}
					\item APA style - CIs
						  \\\(d = X.XX\)
						  \\\(r^2 = .XX\)

				\end{itemize}
		\end{itemize}

	\section{Statistical Formulas}
		\pgfmathdeclarefunction{gauss}{2}{%
		  \pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
		}

		\begin{tikzpicture}
			\begin{axis}[
			  no markers, domain=0:10, samples=100,
			  axis lines*=left, xlabel=$x$, ylabel=$y$,
			  every axis y label/.style={at=(current axis.above origin),anchor=south},
			  every axis x label/.style={at=(current axis.right of origin),anchor=west},
			  height=5cm, width=15cm,
			  xtick={4}, ytick=\empty,
			  enlargelimits=false, clip=false, axis on top,
			  grid = major
			  ]
			  \addplot [fill=cyan!20, draw=none, domain=0:2.4] {gauss(4,1)} \closedcycle;
			  \addplot [fill=cyan!20, draw=none, domain=5.6:10] {gauss(4,1)} \closedcycle;
			  \addplot [very thick,cyan!50!black] {gauss(4,1)};

			\draw [yshift=-0.6cm, latex-latex](axis cs:4,0) -- node [fill=white] {$1.96\sigma$} (axis cs:5.6,0);
			\end{axis}

		\end{tikzpicture}

		\subsection{Empirical Mean}
		\(\gamma = \frac{\sum{x}}{N}\)

		Properties (for independent random variables X and Y):
		\begin{enumerate}
			\item \(Mean(X + Y) = Mean(X) + Mean(Y)\)
			\item \(Mean(X \times Y) = Mean(X) \times Mean(Y)\)
		\end{enumerate}

		\subsection{Variance}
		\(\sigma^2 = Var(X)\newline
			\sigma^2 = \frac{\sum{(x_i - \gamma)^2}}{N}\newline
			\sigma^2 = \frac{\sum{X_i^2}}{N} - \frac{(\sum{X_i})^2}{N^2}\newline
			\sigma^2 = \frac{\sum{(x_i - \gamma)^2}}{N}\)\newline

		\subsection{Standard Deviation}
		\(\sigma = \sqrt{Var(X)}\newline
			\sigma = \sqrt{\frac{\sum{(X_i - \gamma)^2}}{N}}\)\newline

		\subsection{Standard Error}
		\(Sd = \frac{\sigma}{N}\)

		\subsection{Z-Score}
		Z-Score \(= \frac{x - \bar{x}}{Sd}\)

		\subsection{Standard Normal Distribution}
		\(\frac{1}{\sqrt{2 \cdot \pi \cdot \sigma^2}} \cdot e^{[-\frac{1}{2} \cdot \frac{(x-\gamma)^2}{\sigma^2}]}\)

		\subsection{Confidence Interval}
		\(CI = 1.96 \cdot \sqrt{\frac{p(1 - p)}{N}}\newline
			General Form:\newline
			Size of CI = a \cdot \sqrt{\frac{\sigma^2}{N}}\newline
			\frac{\sum{X_i \pm a \cdot \sqrt{\frac{\sigma^2}{N}}}}{N}\)

			\textbf{Note}

			\begin{enumerate}
			\item $a = 1.96 for N \geq 30$
			\item $a$ is the t-value computed for $(N - 1)$ degrees of freedom and confidence level $p$.
			\end{enumerate}

		\subsection{Additional Formulas}
		\begin{tabular}{lllll}
			\hline
			Name & Population Symbol & Sample Symbol & Sample Calculation & Beschreibung \\ \hline
			Mean &  & $\bar{x}$ & $\bar{x} = \frac{\sum{x}}{N}$ & Durchschnitt aller Daten \\
			Variance & $\sigma_x^2$ & $s_x^2$ & $s_x^2 = \frac{\sum{(x - \bar{x})^2}}{N - 1}$ & Abweichung \\
			Standard Dev & $\sigma_x$ & $s_x$ & $s_x = \sqrt{s_x^2}$ &  \\
			Covariance & $\sigma_xy$ & $s_xy$ & $s_xy = \frac{\sum{(x - \bar{x})(y - \bar{y})}}{N - 1}$ &  \\
			Correlation & $\rho_xy$ & $r_xy$ & \begin{tabular}[c]{@{}l@{}}$r_xy = \frac{s_xy}{s_x s_y}$\\ $r_xy = \frac{\sum{(z_x z_y)}}{N - 1}$\end{tabular} &  \\ \hline
			z-score & $z_x$ & $z_x$ & $z_x = \frac{x-\bar{x}}{s_x}; \bar{z} = 0; s_x^2 = 1$ &  \\ \hline
		\end{tabular}

	\subsection{T-Test}
		\[
		t = \frac{\bar{x_D} - 0}{s_D / \sqrt{n}}
		\]

	\section{Lesson 10 - Dependent samples}

		Dependent samples (repeated measures)
		\begin{itemize}
			\item Two Conditions
			\item Longitudinal
			\item Pre-Test, Post-Test
		\end{itemize}

		\subsection{Important formulas}
			\(x = [ARRAY of VALUES] \Rightarrow\) Population Data
			\(X = [ARRAY of VALUES] \Rightarrow\) Sample Data
			\\\(n \Rightarrow\) Sample und Population Size
			\\\(\mu = \frac{\sum{x_i}}{n}\Rightarrow\) Population Mean
			\\\(\bar{X} = \frac{\sum{X_i}}{n} \Rightarrow\) Sample Mean
			\\\(s_D = \sqrt{\frac{\sum{(X_i - \mu)^2}}{n}} \Rightarrow\) Standard Deviation for sample
			\\\(\alpha \Rightarrow\) tail probability on t-table
			\\\(t_{critical} \Rightarrow\) from \href{https://s3.amazonaws.com/udacity-hosted-downloads/t-table.jpg}{t-table}
			\\\(df = n - 1 \Rightarrow\) Degrees of Freedom
			\\\(SEM = \frac{s_D}{\sqrt{n}} \Rightarrow\) Standard Error of the Mean
			\\\(t = \frac{\bar{X} - \mu}{SEM} \Rightarrow\) One Sample t-Test
			\\margin of error \(= (t^{critical} \times SEM)\)
			\\\(CI = \bar{X} \pm \) margin of error \(\Rightarrow\) Confidence Interval
			\\\(d = \frac{\bar{X}-\mu}{s_D} \Rightarrow\) Cohen's d \(\Rightarrow\) Standardized mean difference
			\\\(r^2 = \frac{t^2}{t^2 + df} \Rightarrow\) bestimmt die Stärke des Zusammenhangs zwischen zwei Variablen als Proportion. Beispiel: \(r^2 gibt\) an wieviel das Geschlecht einer Person zum Unterschied der beiden Samples beigetragen hat

		\subsection{Hypothese}
			``US families spent an average of \$151 per week on food''
			\\Beispiel Null Hypothese: ``the program did not change the cost of food''
			\\Beispiel Alternative Hypothese: ``the program reduced the cost of food''
			\\
			\\\(null \rightarrow H_0:\mu_program >= 151\)
			\\\(alt \rightarrow H_A:\mu_program < 151\)

	\section{Lesson 11 - Independent Samples}
		Voneinander abhängige Daten brauchen nicht so viele Testkandidaten, ist kosteneffektiv und weniger zeitaufwendig. Diese Art an Daten zu kommen hat aber auch Nachteile. Beispielsweise könnten die Probanden beim zweiten Ausfüllen eines Tests die Antworten schon kennen.

		Aus diesem Grund braucht es Independent Samples, also je einen Teil des Samples, der das Treatment gemacht hat und einen anderen Teil der es nicht gemacht hat.
		\\\(s_1 \sqrt{\frac{\sum{(X_i1 - \mu)^2}}{n - 1}} \Rightarrow\) Standard Deviation for sample with
		 \href{Bessel\'s correction}{http://www.wikiwand.com/en/Bessel\%27s\_correction}
		\\\(s_2 \sqrt{\frac{\sum{(X_i2 - \mu)^2}}{n - 1}} \Rightarrow\) Standard Deviation for sample with \href{Bessel\'s correction}{http://www.wikiwand.com/en/Bessel\%27s\_correction}
		\\\(n_1 \Rightarrow\) Size of Sample 1
		\\\(n_2 \Rightarrow\) Size of Sample 2
		\\\(S_D = \sqrt{s_1^2 + s_2^2}\) Standard Deviation for Samples
		\\\(SEM = \sqrt{\frac{s_1^2}{n_1} + \frac{s_2^2}{n_2}} \Rightarrow\) Standard Error for independent samples
		\\\(df = n_1 + n_2 - 2 \Rightarrow\) Degrees of freedom for independent samples
		\\\(t = \frac{\bar{X}_1 - \bar{X}_2}{SEM} \Rightarrow\) Two Sample t-Test
		\\\(CI = (\bar{X}_1 - \bar{X}_2) \pm \) margin of error \(\Rightarrow\) Confidence Interval
		\\\(SS_x = \sum{(x_i - \bar{x})^2} \Rightarrow\) Sum of Squared Deviations
		\\\(S_p^2 = \frac{SS_1 + SS_2}{df_1 + df_2} \Rightarrow\) Pooled variance
		\\\(SEM_{CORRECTED} (S_{\bar{x_1} - \bar{x_2}}) = \sqrt{\frac{s_p^2}{n_1} + \frac{s_p^2}{n_2}} \Rightarrow\) Corrected Standard Error
		\\\(t_{CORRECTED} = \frac{\bar{x_1} - \bar{x_2}}{S_{\bar{x_1}-\bar{x_2}}} \Rightarrow\) t-Statistic Corrected

	\section{Lesson 12 - ANOVA (Analysis of Variance)}
		ANOVA for samples with the same size
		\\\(N \Rightarrow\) number of values from all samples
		\\\(k \Rightarrow\) number of samples
		\\\(\bar{x}_G = \frac{\sum{\bar{x}_i}}{N}\Rightarrow\) Grand Mean
		\\\(df = N - k \Rightarrow\) degrees of freedom
		\\\(df_1 = k - 1)\)
		\\\(df_2 = N - k)\)
		\\\(df_{total} = N - 1 \Rightarrow\) total degrees of freedom
		\\\(F = \frac{n * \sum{(\bar{x}_K - \bar{x}_G)^2 / df_1}}{\sum{(x_i - \bar{x}_k)^2 / df_2}} \Rightarrow\) between-group variability / within-group variability. \href{http://www.socr.ucla.edu/applets.dir/f\_table.html}{F-Table}

\end{document}


\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amssymb}
\usepackage{mathtools}
\usepackage{ulem}
\usepackage{url}
\usepackage{graphicx}
 \usepackage{geometry}
\geometry{margin=1.0in}

\usepackage{lmodern}

\makeatletter

\makeatother

\usepackage{etoolbox}

%%%%%%%%% STUDENTS CHANGE THIS

\providetoggle{undergrad}
\settoggle{undergrad}{false}     %%% "true" if 3395 or "false" if 6390

\providetoggle{final}
\settoggle{final}{false}        %%% "true" for your final homework submission (removes instructions)

%%%%%%%%%%%%%%%%%%%%% ^^^^^
\usepackage[colorlinks=true]{hyperref}

\begin{document}

\setlength{\parskip}{0.3cm} \setlength{\parindent}{0cm}

\begin{center}
\textbf{{IFT 6390 Fundamentals of Machine Learning \\ Vijaya lakshmi Kuruba\\ Saadaoui Houda}}
\par\end{center}{\large \par}

\begin{center}
\textbf{\LARGE{{Model Card - SVM Linear Classifier}}}
\par\end{center}{\LARGE \par}

Our model is a supervised learning which consists of classifying gender using tweets and users profile description.
Since it is a supervised learning task we used a dataset consisting of tweets labeled with the gender “male” or “female”.

The dataset used has 20,000 samples and has many features but to build our model we chose only two features: tweets and description.

Before building our model, we first cleaned the data using the following steps:
\begin{itemize}
    \item Choosing only the gender "female" and "male" (the data has also unknown and brand as genders)
    \item Preprocessing the data using the Preprocessor library
\end{itemize}

After filetring and preprocessing the dataset, we ended up with 12,895 samples which we split into training and testing with 80\% and 20\% respectively of the dataset.  \\
We chose to use the linear classifier Support Vector Machine SVM to make the classification task.

Model parameter is validated with K fold cross validation and classifier metrics has been reported.

\paragraph{SVM Linear Classifier}:

$$\includegraphics[scale=.5]{svm.JPG}$$

We consider labeled data $(\mathbf{x},y)$, where $\mathbf{x}$ is a $d$-dimensional input, i.e. $\mathbf{x} \in \mathbb{R}^{d}$ and $y\in\{-1,1\}$. We have a dataset of $n$ such pairs $(\mathbf{x}_i,y_i)$. We want to train a linear classifier on this dataset. \\

\textbf{Linear Model:}
\begin{equation}
 f(x) = \mathbf{w}^T \mathbf{x} + b
\end{equation}
On Hyper plane :
\begin{equation}
f(x) =\mathbf{w}^T \mathbf{x} + b=0
\end{equation}
Constraints are:
\begin{equation}
 \mathbf{w}^T \mathbf{x_i} + b\geq 1 \quad \text{if} \quad y_i=1
 \end{equation}
\begin{equation}
 \mathbf{w}^T \mathbf{x_i} + b\leq -1 \quad \text{if} \quad y_i=-1
\end{equation}

In general
\begin{equation}
    y_i(\mathbf{w}^T \mathbf{x_i} + b)\geq 1 \quad \forall \quad i \in 1...N
\end{equation}

To simplify code and notation, we can get rid of the bias term $b$. To do  so, we concatenate $1$ to every $\mathbf{x}$ vector, so that $\mathbf{x}' = (\mathbf{x}, 1) \in \mathbb{R}^{d+1}$, and we concatenate $b$ to $\mathbf{w}$, so that $\mathbf{w}' = (\mathbf{w}, b)$. Then $\mathbf{w}^T \mathbf{x} + b = \mathbf{w}'^T \mathbf{x}'$. We can write everything in terms of linear transformations instead of affine transformations.

We will omit the bias term from now on, and consider $\mathbf{x}$ and $\mathbf{w}$ themselves as $\mathbf{x}'$ and $\mathbf{w}'$

New notation:
\begin{align}
    y_i(\mathbf{w}^T \mathbf{x_i} )\geq 1 \quad \forall \quad i \in 1...N
\end{align}

\textbf{Loss Function :} \\
\\The hinge loss is used for "maximum-margin" classification.\\
Loss function is given by:

\begin{equation}
 g(w;\mathbf{x}, y) =
\begin{cases}
0 \quad \quad \quad \quad\quad \text{if}\quad 1-yf(x) \geq 1 \\ 1-yf(x) \quad\text{otherwise.}
\end{cases}
\end{equation}

\textbf{Objective Function:}\\
 \\The loss function stated above is called convex surrogate losses. They are convex and have a gradient so we can optimize them with gradient descent.\\

The training objective that we are going to minimize is the average of the losses over each training example plus an $\ell^2$ regularization with hyperparameter $\lambda$:
\begin{align}
 L(w) = \frac{1}{n}\sum_{i=1}^n g(w;\mathbf{x_i}, y_i)  + \frac{\lambda}{2} \| \mathbf{w}\|^2
\end{align}

Note:  Minimising w allows to maximize the margen

where the parameter $\lambda$  determines the trade-off between increasing the margin size and ensuring that the $\mathbf {x} _{i}$ lie on the correct side of the margin. Thus, for sufficiently small values of $\lambda$ , the second term in the loss function will become negligible, hence, it will behave similar to the hard-margin SVM, if the input data are linearly classifiable, but will still learn if a classification rule is viable or not.

\textbf{Gradient Desecent:}

SVM classifier amounts to minimizing the loss funtion Equation 8 with Gradient descent technique.
We want to minimize the loss $L(w)$. It is differentiable, so we can use the gradient descent algorithm: start from any initialization parameter $\mathbf{w}_0$, and repeat for $t\in\{0, \dots, t_{\max} \}$:
$$\mathbf{w}_{t+1} = \mathbf{w}_t - \eta\ \nabla L (\mathbf{w}_t) \; .$$

Under some conditions on the step-size $\eta$ and the loss $L$, this algorithm is guaranteed to converge to a minimum of $L(w)$.

$$\nabla L (\mathbf{w}_t)
=
\begin{cases}
\lambda \| \mathbf{w}\| \quad \quad \quad \quad\quad \text{if}\quad 1-yf(x) \geq 1 \\ \lambda \| \mathbf{w}\|-y_ix_i \quad\text{otherwise.}
\end{cases}
$$

To choose the $\lambda $ parameter, we used a Randomized Search to get the best value among the following list [1e-4, 1e-3, 1e-2, 1e-1, 1e0, 1e1, 1e2, 1e3]. The obtained value of $\lambda $ is 0.001

\paragraph{K fold Crossvalidation}:

To evaluate the performance of a model on a dataset, we need to measure how well the predictions made by the model match the observed data.
When data is scarce, we may not be able to afford using a validation set, So we use K fold crossvalidation technique.\\
Approach : split the dataset into K equal partitions.
\begin{itemize}
    \item 1. Set one partition aside for prediction/validation, and train the model on the remaining K-1 partitions.\\
   \item 2. Repeat the above step for each partition and report the average prediction error.

\end{itemize}

In pratice: common to pick 5-fold or 10-fold cross-validation.

Mathematically:
$$
        CV({\hat{f}}) = \frac{1}{N} \sum_{i=1}^{N} L(y_{i}, {\hat{f}}^{(\kappa(i))}(x_{i})) \enspace $$
where $\hat{f}(k)$ denotes the model obtained by removing the k-th partition and training on the
rest. Furthermore, k(i) is a function which, given a datapoint index, returns the partition to
which it belongs. If the i-th datapoint belongs to the j-th partion, then we want the model
$f^{(j)}$
, where the j-th partition has been excluded.

For our model, after cleaning and filtering the data we used K-Folds Cross Validation technique using k = 5 to validate it.

\paragraph{Classifier Accuracy }:
\begin{align*}
    Accuracy (y, {\hat{y}})= \frac{1}{N} \sum_{i=1}^{N} {1}_{{\hat{y_{i}}} = y}
\end{align*}
where ${1} (x)$  is the indicator function.

\paragraph{Time Complexity}:

N= Number of training examples\\
k=k is the number of iterations (epochs)\\
d=Dimensions of the features

Time complexity for training is : O(Nkd)

Time complexity for testing is : O(Nd)

\paragraph{Space Complexity}:

N= Number of training examples\\
d=Dimensions of the features

Space complexity for training is : O(Nd)

Space complexity for testing is : O(Nd)
\end{document}


\section{Nomenclature}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Nomenclature}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

$W \equiv$ system width

$L \equiv$ system length

$y \equiv$ perpendicular position, varies between $0$ and $W$

$z \equiv$ parallel position, varies between $0$ and $L$

$\ell_{tmfp} \equiv$ transport mean free path

$k \equiv$ total wave number

$k_{\parallel} \equiv$ wave number parallel to wave propagation direction (along $z$), see Eq.~\ref{eq:k_parallel}

$k_{\bot} \equiv$ wave number perpendicular to wave propagation direction (along $y$), see Eq.~\ref{eq:k_perpendicular}

$\kappa \equiv$ imaginary total wave number, is similarly broken into parallel and perpendicular components

$n \equiv$ channel index, varies from $1$ to $\infty$

$N_o \equiv$ number of open channels, also known as $N_{open}$

$N_{max} \equiv$ number of open and closed channels (finite)

$\omega \equiv$ frequency

$E \equiv$ electric field

${\cal H} \equiv$ magnetic field

${\cal E} \equiv$ energy

$E^+ \equiv$ plane wave electric field propagating from left to right

$E^- \equiv$ plane wave electric field propagating from right to left

$E' \equiv$ first order electric field derivative wrt space

$\alpha \equiv$ scatterer strength (unitless)

$\Delta \equiv$ a small change in the variable

$c \equiv$ speed of light

$\mu_0 \equiv$ vacuum permeability

$\epsilon_0 \equiv$ vacuum permittivity


\documentclass[12pt]{report}
\usepackage{amsmath,textcomp,amssymb}
\usepackage{geometry}
\usepackage{indentfirst}
\usepackage{graphicx}
\usepackage{float}
\usepackage{expdlist}
\usepackage{xcolor}
\usepackage{hyperref}
\usepackage{multicol}

\def\Title{BLOM Quick Start Guide}
\def\Name{MPC Lab}
\def\contacts{Jason Kong: \url{jasonjkong@berkeley.edu}\\ Tony Kelman: \url{kelman@berkeley.edu} \\ Kyle Chiang: \url{kylechiang@berkeley.edu}}
\title{\Title}
\author{\Name \\ \contacts}
\date{Last updated: \today}
\markboth{\Title}{\Title}
\pagestyle{myheadings}
\setlength\parindent{0pt}
\setlength{\parskip}{12pt}
\footskip = 0pt
\textheight = 670pt

\newenvironment{itemize*}
  {\begin{itemize}
    \setlength{\itemsep}{1pt}
    \setlength{\parskip}{1pt}}
  {\end{itemize}}

\newcommand{\textbu}[1]{\textbf{\underline{#1}}}
\newcommand{\red}[1]{\textcolor{red}{#1}}

\begin{document}
\maketitle
\tableofcontents
\setcounter{secnumdepth}{0}

\clearpage \section{What is BLOM?}
\begin{itemize}
\item Provides a graphical interface to allow users to create optimization problems using Simulink blocks.
\item Exports mathematical model to solvers (eg. ipopt)
\item Great for optimization problems with "dynamics" that evolve over time
$$\min_x f(x)$$
$$g(x) \leq 0$$
$$h(x) = 0$$
\vspace{-50pt}
\end{itemize}•

\section{BLOM Library}
\begin{figure}[H]
\center
\includegraphics[width=100mm]{figures/BLOM_Lib.png}
\vspace{-40pt}
\end{figure}•

\textbu{Externals:} Labels External Variables that can be changed via script or command line for different calls of the solver \\[10pt]
\textbu{Inputs:} Labels Input Variables to be optimized by solver \\[10pt]
\textbu{Bounds:} Sets upper/lower bounds on a variable\\[10pt]
\textbu{Cost:} Cost variable to be minimized.  \\[10pt]
\textbu{Polyblocks:} BLOM's convenient way to create nonlinear functions\\[10pt]

\section{Polyblocks}
BLOM's internal representation of blocks.

$$ y_1=2x^2_1x_2 , \hspace{3mm} y_2=3x_1+x_2^4 $$
\[
P = \begin{bmatrix}
2 & 1 \\
1 & 0 \\
0 & 4
\end{bmatrix}
\hspace{3mm}
K = \begin{bmatrix}
2 & 0 & 0\\
0 & 3 & 1
\end{bmatrix}
\]

$$y_1 = 2x_1+3sin(x_2),  \hspace{3mm} y_2 = 3x_1^2e^{x_3}+0.2tan(x_2)x_4^3$$
\[P= \begin{bmatrix}
1 & 0 & 0 & 0 \\
0 & \text{BLOM\_FunctionCode(`sin')} & 0 & 0\\
2 & 0 & \text{BLOM\_FunctionCode(`exp')} & 0 \\
0 & \text{BLOM\_FunctionCode(`tan')} & 0 & 3
\end{bmatrix}
\]
\section{Setting Up BLOM on Your Computer}
\begin{enumerate}
\item \url{http://mpclab.net/Trac/wiki/SVNsetup} Here are instructions on how to get SVN and how to get BLOM running
\item in command line, \texttt{svn checkout http://www.mpclab.net/BLOM/ \red{desired\_directory}}
\item Each time you open up BLOM, make sure to get the latest version by typing \texttt{svn update} within that folder (or update through TortoiseSVN)
\item On Mac or Linux machines, you may need to compile IPOPT and then run \texttt{BLOM\_Setup} (Instructions for doing so at \url{http://mpclab.net/Trac/wiki/CompilingIpopt})

\end{enumerate}•

\clearpage
\section{Creating Model Example}
\vspace{-20pt}$$\max  f(x)=3x_1+x_2-x_3^2+2x_3$$
$$x_1^2+x_2^2\leq5$$
$$x_1-x_2\leq1$$
$$x_3\geq0$$

\begin{figure}[H]
\textbu{Step 1:} Place Input and External Blocks for input and external variables
\center
\includegraphics[width=120mm]{figures/Example_Step1.png}
\end{figure}
\begin{figure}[H]
\textbu{Step 2:} For each bound limitation and cost function, drag and drop math blocks to satisfy equations.  Use subsystems and/or polyblocks as needed
\center
\includegraphics[width=50mm]{figures/Example_Step2_1_subsys.png}
\includegraphics[width=120mm]{figures/Example_Step2_1.png}
\end{figure}
\begin{figure}[H]
\textbu{Step 3:} Attach bound and cost blocks and set limits/time relevances
\center
\includegraphics[width=120mm]{figures/Example_Step3_1.png}
\includegraphics[width=60mm]{figures/Example_Step3_1_bound.png}
\end{figure}
\begin{figure}[H]
Repeat steps 2 and 3 as necessary
\center
\includegraphics[width=120mm]{figures/Example_Step3_2.png}
\end{figure}
\begin{figure}[H]
\center
\includegraphics[width=120mm]{figures/Example_final.png}
\end{figure}

\section{Calling BLOM}
\begin{enumerate}
\item Always remember to run \texttt{BLOM\_addpath} to add all the BLOM related files into your path
\item Create model in simulink
\item \texttt{BLOM\_SetDataLogging(\red{`ModelName'})}
\item \texttt{ModelSpec = BLOM\_ExtractModel(\red{`ModelName'}, \red{\#timesteps})}
\item \texttt{[RunResults ResultsVec] = BLOM\_RunModel(ModelSpec)}
\item \texttt{[OptGuess ExtVars InitialStates ] = BLOM\_SplitResults(ModelSpec,RunResults)}
\item \texttt{SolverStruct = BLOM\_ExportToSolver(ModelSpec,\red{`Solver'})}
\item \texttt{SolverStructData =  BLOM\_SetProblemData(SolverStruct,ModelSpec,OptGuess, ExtVars, InitialStates)}
\item \texttt{SolverResult  =  BLOM\_RunSolver(SolverStructData,ModelSpec)}
\end{enumerate}•
\textbu{Using Externals:} Items 7-9 can be run in a loop using outputs from \texttt{SolverResult} to populate \texttt{OptGuess} and \texttt{InitialStates} in subsequent iterations.  \texttt{OptGuess}, \texttt{ExtVars}, and \texttt{InitialStates} can all be filled in from the command line (e.g. \texttt{ExtVars.x1=5}, and \texttt{OptGuess=SolverResult})

\section{Optimizing Your Model}
\begin{itemize}
\item Use fewer blocks.  Outputs of blocks (with the exception of subsystems, from/goto tags, mux/demux) represent variables.  Having fewer blocks and therefore fewer variables allows for faster computation.
\item Switch to polyblocks.  Converting groups of mathematical operations into polyblocks can also reduce the number of variables
\item For polyblocks with sparse entries, create matrices using Matlab function sparse.  This reduces memory storage and computations are optimized within Matlab.
\end{itemize}•

\clearpage
\section{Currently Supported Simulink Blocks}
\begin{multicols}{2}
\begin{itemize*}
\item Sum, Add, Subtract
\item Product, Multiply, Divide
\item Gain
\item Unary Minus
\item Bias
\item Math
\vspace{-5pt}
	\begin{itemize*}
	\item square
	\item sqrt
	\item reciprocal
	\item exp
	\item 10\string^u
	\item log
	\item log10
	\item magnitude\string^2 (Reals only)
	\item 1/sqrt, rsqrt
	\item hypot
	\end{itemize*}•
\item Trigonometry
\vspace{-5pt}
	\begin{itemize*}
	\item sin
	\item cos
	\item tan
	\item asin
	\item acos
	\item atan
	\item sincos
	\end{itemize*}•
\item Polynomial
\item Constant
\item Unit Delay
\item Subsystem
\item From, Goto
\item Mux, Demux
\end{itemize*}•

\end{multicols}

\end{document}


\documentclass[12pt]{cdblatex}
\usepackage{bssn-eqtns}

\begin{document}

\section*{PhysRevD.67.084023 equation (19)}

\begin{cadabra}
   from shared import *
   import cdblib

   jsonfile = 'bssn-constraints.json'
   cdblib.create (jsonfile)

   # --------------------------------------------------------------------------
   # Hamiltonian constraint

   Ham := R + K_{a b} g^{a b} K_{c d} g^{c d} - K_{a b} K_{c d} g^{a c} g^{b d}.
                                                             # cdb(Ham.101,Ham)

   Ham := R + (2/3) (trK)**2 - ABar_{a b} ABar^{a b}.        # cdb(Ham.102,Ham)

\end{cadabra}

\begin{dgroup*}[spread=5pt]
   \begin{dmath*}
      {\cal H}
         = \Cdb*{Ham.101}
         = \Cdb*{Ham.102}
   \end{dmath*}
\end{dgroup*}

\clearpage

\section*{PhysRevD.67.084023 equation (20)}

\begin{cadabra}
   # --------------------------------------------------------------------------
   # Momentum constraint

   confMom := 6 ABar^{i a} \partial_{a}{\phi}
              + \partial_{a}{ABar^{i a}}
              + ABar^{a b} GammaBar^{i}_{a b}
              - (2/3) gBar^{i a} \partial_{a}{trK}.

   defGammaBar := GammaBar^{a}_{b c} ->
                  (1/2) gBar^{a e} (   \partial_{b}{gBar_{e c}}
                                     + \partial_{c}{gBar_{b e}}
                                     - \partial_{e}{gBar_{b c}}).

   substitute (confMom, defGammaBar)                       # cdb(confMom.101,confMom)
   distribute (confMom)                                    # cdb(confMom.102,confMom)

   confMom = product_sort (confMom)                        # cdb(confMom.103,confMom)

   rename_dummies (confMom)                                # cdb(confMom.104,confMom)
   canonicalise   (confMom)                                # cdb(confMom.105,confMom)

   foo := \partial_{a}{ABar^{i a}} -> \partial_{a}{gBar^{i c} gBar^{a d} ABar_{c d}}.

   substitute   (confMom, foo)                             # cdb(confMom.106,confMom)
   product_rule (confMom)                                  # cdb(confMom.107,confMom)

   confMom = product_sort (confMom)                        # cdb(confMom.108,confMom)

   rename_dummies (confMom)                                # cdb(confMom.109,confMom)
   canonicalise   (confMom)                                # cdb(confMom.110,confMom)

   cdblib.put ('Ham',Ham,jsonfile)
   cdblib.put ('confMom',confMom,jsonfile)
\end{cadabra}

\clearpage

\begin{dgroup*}
   \begin{dmath*}
      \exp(4\phi) {\cal D}^{j}
         = \Cdb*{confMom.101}
         = \Cdb*{confMom.102}
         = \Cdb*{confMom.103}
         = \Cdb*{confMom.104}
         = \Cdb*{confMom.105}
         = \Cdb*{confMom.106}
         = \Cdb*{confMom.107}
         = \Cdb*{confMom.108}
         = \Cdb*{confMom.109}
         = \Cdb*{confMom.110}
   \end{dmath*}
\end{dgroup*}

\end{document}


\documentclass[11pt,a4paper]{report}
\usepackage{amsmath,amsfonts,amssymb,amsthm,epsfig,epstopdf,titling,url,array}
\usepackage{enumitem}
\usepackage{changepage}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{listings}
\usepackage{color}
\usepackage{hyperref}
\theoremstyle{plain}
\newtheorem{thm}{Theorem}[section]
\newtheorem{lem}[thm]{Lemma}
\newtheorem{prop}[thm]{Proposition}
\newtheorem*{cor}{Corollary}
\theoremstyle{definition}
\newtheorem{defn}{Definition}[section]
\newtheorem{conj}{Conjecture}[section]
\newtheorem{exmp}{Example}[section]
\newtheorem{exercise}{Exercise}[section]
\theoremstyle{remark}
\newtheorem*{rem}{Remark}
\newtheorem*{note}{Note}
\def\changemargin#1#2{\list{}{\rightmargin#2\leftmargin#1}\item[]}
\let\endchangemargin=\endlist
\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}
\hypersetup{
	colorlinks=true,
	linkcolor=blue,
	filecolor=magenta,
	urlcolor=cyan,
}
\urlstyle{same}
\lstdefinestyle{mystyle}{
	backgroundcolor=\color{backcolour},
	commentstyle=\color{codegreen},
	keywordstyle=\color{magenta},
	numberstyle=\tiny\color{codegray},
	stringstyle=\color{codepurple},
	basicstyle=\footnotesize,
	breakatwhitespace=false,
	breaklines=true,
	captionpos=b,
	keepspaces=true,
	numbers=left,
	numbersep=5pt,
	showspaces=false,
	showstringspaces=false,
	showtabs=false,
	tabsize=2
}
\lstset{style=mystyle}
\begin{document}

\section*{Problem}
Suppose that you have one thousand single dollar bills and you want to put it into a set of envelopes in such a way that any amount up to \$1000 can be made by combining the contents of some subset of the envelopes.  What is the smallest number of envelopes that you need and how would you stuff them?
\\
\\
\textit{Source:} This problem is from \href{https://www.cartalk.com}{Car Talk}

\section*{Solution}
 Use 1, 2, 4, 8, 16, 32, 64, 128, 256 and 489.  To see why this will give all the sums, note that every number has a binary expansion and the expansion of any number up to 511 is a sum of the first nine powers of 2.  To get the rest of the numbers, you can start with 489 and then get the remainder in the same way.
 \\
 \\
 What is interesting to prove is that this can't be done with fewer than 10 envelopes and this solution is unique.

\end{document}


\lesson{5}{Dec 01 2021 Wed (19:09:04)}{Polynomial Identities and Proofs}{Unit 3}

\begin{definition}[Algebraic Proofs]
    \begin{itemize}
        \item \bf{Polynomial Identities} can be proven to be true by simplifying the identity through application of \bf{Algebraic Theorems} and \bf{Principles}.
        \item Start with the side of the identity that can be simplified the easiest.
        \item Sometimes, following a \it{“clue”} will lead to a dead-end in your \bf{Proof}. Do not give up. Just follow a different \it{“clue”}. The more practice you have with proofs, the more you will be able to predict the dead-ends.
    \end{itemize}
\end{definition}

\begin{example}

\end{example}

\subsubsection*{Application to Numerical Relationships}

Polynomial identities apply to more than just polynomials. Replacing the variables with numbers can help prove numerical relationships as well.

\newpage


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                 %
%  GEANT manual in LaTeX form                              %
%                                                                 %
%  Michel Goossens (for translation into LaTeX)                   %
%  Version 1.00                                                   %
%  Last Mod. Jan 24 1991  1300   MG + IB                          %
%                                                                 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Origin{R.Brun, F.Carena}
\Submitted{01.06.83}             \Revised{14.12.93}
\Version{Geant 3.16}\Routid{GEOM200}
\Makehead{Rotation matrices}

The relative position of a volume inside its mother is expressed in
{\tt GEANT} by a translation vector and a rotation matrix which are
arguments of the routines \Rind{GSPOS} and \Rind{GSPOSP}. The rotation
matrix expresses the transformation from the {\tt M}other {\tt R}eference
{\tt S}ystem to the {\tt D}aughter {\tt R}eference {\tt S}ystem.

A rotation matrix is described to {\tt GEANT} by giving the polar and
azimuthal angles of the axes of the {\tt DRS} ($x', y', z'$) in the
{\tt MRS} via the routine \Rind{GSROTM}.

\Shubr{GSROTM}{(IROT,THETA1,PHI1,THETA2,PHI2,THETA3,PHI3)}
\begin{DLtt}{MMMMMMMM}
\item[IROT] ({\tt INTEGER}) number of the rotation matrix;
\item[THETA1] ({\tt REAL}) polar angle for axis $x'$;
\item[PHI1] ({\tt REAL}) azimuthal angle for axis $x'$;
\item[THETA2] ({\tt REAL}) polar angle for axis $y'$;
\item[THI2] ({\tt REAL}) azimuthal angle for axis $y'$;
\item[THETA3] ({\tt REAL}) polar angle for axis $z'$;
\item[PHI3] ({\tt REAL}) azimuthal angle for axis $z'$.
\end{DLtt}
Stores rotation matrix {\tt IROT} in the data structure {\tt JROTM}. If the
matrix is not orthonormal, it will be corrected by setting $y' \perp x'$ and
then $z' = x' \times y'$. A warning message is printed in this case.

{\bf Note:}
the angles {\tt THETA} and {\tt PHI} must be given in degrees.

\section*{Examples of use}
The unit matrix is defined in the following way:

\[
\left . \begin{array}{lcl}
x' & \| & x \\
y' & \| & y \\
z' & \| & z
\end{array} \right \}
\Rightarrow
\left \{
\begin{array}{lcr@{\mbox{\hspace{3mm};\hspace{8mm}}}lcr}
\theta_1 & = & 90^{\circ} & \phi_1 & = & 0^{\circ} \\
\theta_2 & = & 90^{\circ} & \phi_2 & = & 90^{\circ} \\
\theta_3 & = & 0^{\circ} & \phi_3 & = & 0^{\circ}
\end{array} \right .
\]

This is just an example. There is in fact no need to define a unit rotation
matrix. Giving the value 0 to the rotation matrix number in the call to
\Rind{GSPOS} and \Rind{GSPOSP} is equivalent to a positioning without
rotation and it improves tracking performance.

The result of a $90^{\circ}$ counterclockwise rotation around $z$, followed
by a $90^{\circ}$ counterclockwise rotation around the new $x$ is a cyclic
shift of the axes: $x \rightarrow z', \; y \rightarrow x', \; z \rightarrow
y'$. This is expressed by the following rotation matrix:

\[
\left . \begin{array}{lcl}
x' & \| & y \\
y' & \| & z \\
z' & \| & x
\end{array} \right \}
\Rightarrow
\left \{
\begin{array}{lcr@{\mbox{\hspace{3mm};\hspace{8mm}}}lcr}
\theta_1 & = & 90^{\circ} & \phi_1 & = & 90^{\circ} \\
\theta_2 & = & 0^{\circ} & \phi_2 & = & 0^{\circ} \\
\theta_3 & = & 90^{\circ} & \phi_3 & = & 0^{\circ}
\end{array} \right .
\]

Sometimes the rotation matrix is known or it can be constructed. In this case
the arguments to the routine \Rind{GSROTM} can be calculated with the help
of the routine \Rind{GFANG} in the following way:

\begin{verbatim}
      DIMENSION ROTMAT(3,3), ROWMAT(3), PHI(3), THETA(3)
      LOGICAL ROTATE
      .
      .
      .
      DO 10 I=1,3
         ROWMAT(1) = ROTMAT(I,1)
         ROWMAT(2) = ROTMAT(I,2)
         ROWMAT(3) = ROTMAT(I,3)
         CALL GFANG(ROWMAT,COSTH,SINTH,COSPH,SINPH,ROTATE)
         THETA(I) = ATAN2(SINTH,COSTH)
         PHI(I)   = ATAN2(SINPH,COSPH)
  10  CONTINUE
      .
      . {\sl Transform to degrees}
      .
      CALL GSROTM(IROT,THETA(1),PHI(1),THETA(2),PHI(2),THETA(3),PHI(3))
\end{verbatim}

\Shubr{GPROTM}{(IROT)}
Prints the rotation matrix elements and angles.
\begin{DLtt}{MMMMMMMM}
\item[IROT]  ({\tt INTEGER}) rotation matrix number: if {\tt IROT}=0 all
rotation matrixes will be printed, if {\tt IROT}$<$0, matrix number
{\tt |IROT|} will be printed without header information.
\end{DLtt}