%!TEX root = ../main.tex

%===============================================================================
%
%    Chapter: math_fundamentals
%
%===============================================================================

\chapter{Math fundamentals}
\label{chapter:math_fundamentals}

In this chapter we'll review the fundamental ideas of mathematics, including numbers, equations, and functions.
To understand college-level textbooks, you need to be comfortable with mathematical calculations.
Many people have trouble with math, however.
Some people say they \emph{hate} math, or could never learn it.
It's not uncommon for children who score poorly on their school math exams to develop math complexes in their grown lives.
If you are carrying any such emotional baggage, you can drop it right here and right now.

Do NOT worry about math!
You are an adult, and you can learn math much more easily than when you were a kid.
We'll review \emph{everything} you need to know about high school math, and by the end of this chapter,
you'll see that math is nothing to worry about.

\smallskip

\begin{figure}[H]
\centering
\! \includegraphics[width=1.01\textwidth]{figures/concept_maps/precalculus.pdf}
	\caption{A concept map showing the mathematical topics that we will cover in this chapter.
			We'll learn how to solve equations using algebra,
			how to model the world using functions,
			and how to think geometrically.
			The material in this chapter is required for your understanding of the more advanced topics in this book.}
\label{fig:precalculus_concept_map}
\end{figure}

%=======================================================================  solving_equations
\section{Solving equations}
\label{sec:solving_equations}

	Most math skills boil down to being able to manipulate and solve equations.
	Solving an equation means finding the value of the unknown in the equation.

	Check this \pgt{}{shit} out:
	\[
	 x^2-4=45.
	\]
	To solve the above equation is to answer
	the question ``What is $x$?''
	More precisely, we want to find the number that can take the
	place of $x$ in the equation so that the equality holds.
	In other words, we're asking,
	\[
	  \text{``Which number times itself minus four gives 45?''}
	\]
	That is quite a mouthful, don't you think?
	To remedy this verbosity, mathematicians often use specialized symbols to describe math operations.
	The problem is that these specialized symbols can be very confusing.
	Sometimes even the simplest math concepts are inaccessible if you don't know what the symbols mean.

	What are your feelings about math, dear reader? Are you afraid of it?
	Do you have anxiety attacks because you think it will be too difficult for you?
	Chill! Relax, my brothers and sisters. There's nothing to it.
	Nobody can magically guess the solution to an equation immediately.
	To find the solution, you must break the problem into simpler steps.
	Let's walk through this one together.

	To find $x$, we can manipulate the original equation,
	transforming it into a different equation (as true as the first) that looks like this:
	\[
	  x \; = \textrm{ only numbers.}
	\]

	\noindent
	That's what it means to \emph{solve} an equation:
	the equation is solved because the unknown is isolated on one side,
	while the constants are grouped on the other side.
	You can type the numbers on the right-hand side into a calculator and obtain the numerical value of $x$.

	By the way, before we continue our discussion,
	let it be noted: the equality symbol ($=$) means that all that is to the left of $=$
	is equal to
	all that is to the right of $=$.
	To keep this equality statement true,
	\textbf{for every change you apply to the left side of the equation,
	you must apply the same change to the right side of the equation}.

	% Keeping that rule in mind,
	To find $x$,
	we need to manipulate the original equation into its final form,
	simplifying it step by step until it can't be simplified any further.
	The only requirement is that the manipulations we make transform one true equation into another true equation.
	In this example,
	the first simplifying step is to add the number four to both sides of the equation:
	\[
	 	x^2-4  \; + 4  	=	45    \; + 4, 	    \\
	\]
	which simplifies to
	\[
		x^2 	 		=	49.
	\]
	Now the expression looks simpler, yes?
	How did I know to perform this operation?
	I wanted to ``undo'' the effects of the operation $-4$.
	We undo an operation by applying its \emphindexdef{inverse}.
	In the case where the operation is the subtraction of some amount,
	the inverse operation is the addition of the same amount.

	We're getting closer to our goal of \emph{isolating} $x$ on one side of the equation,							\index{isolate}
	leaving only numbers on the other side.
	The next step is to undo the square $x^2$ operation.
	The inverse operation of squaring a number $x^2$ is to take its square root $\sqrt{\phantom{a}\; }$,
	so that's what we'll do next. We obtain
	\[
	   \sqrt{x^2} 		= 	\sqrt{49}.
	\]
	Notice how we applied the square root  to both sides of the equation?
	If we don't apply the same operation to both sides, we'll break the equality!

	The equation $\sqrt{x^2}= \sqrt{49}$ simplifies to
	\[
	 	|x|	= 	7.
	 \]
	What's up with the vertical bars around $x$?
	The notation $|x|$ stands for the \emph{absolute value} of $x$,											\index{absolute value}
	which is the same as $x$ except we ignore the sign that indicates whether $x$ is positive or negative.
	For example $|5|=5$ and $|-5|=5$, too.
	The equation $|x|=7$ indicates that both $x=7$ and $x=-7$ satisfy the equation $x^2 = 49$.
	Seven squared is 49, $7^2=49$, and negative seven squared is also 49, $(-7)^2 = 49$,
	because the two negative signs cancel each other out.

	The final solutions to the equation $x^2-4=45$ are													\index{solution set}
	\[
	 x  = 7 \qquad \textrm{and} \qquad   x=  - 7.
	\]
	Yes, there are \emph{two} possible answers.
	You can check that both of the above values of $x$ satisfy the initial equation $x^2-4=45$.

	\bigskip

	If you are comfortable with all the notions of high school math
	and you feel you could have solved the equation $x^2-4=45$ on your own,		% TODOv6: special comment for readers who expected there to be two solutions
	then you can skim through this chapter quickly.
	If on the other hand you are wondering how the squiggle killed the power two,
	then this chapter is for you!
	In the following sections we will review all the essential concepts from
	high school math that you will need to power through the rest of this book.
	First, let me tell you about the different kinds of numbers.

%!TEX root = ../main.tex

%=======================================================================  numbers
\section{Numbers}
\label{sec:numbers}

	In the beginning, we must define the main players in the world of math: numbers.

	\subsection{Definitions}
	\label{numbers:definitions}

		Numbers are the basic objects we use to count, measure, quantify, and calculate things.
		Mathematicians like to classify the different kinds of number-like objects into categories called \emph{sets}:					\index{set}
		\begin{itemize}
			\item  The natural numbers: $\mathbb{N} = \{0,1,2,3,4,5,6,7, \ldots \, \}$
			\item  The integers: $\mathbb{Z} = \{\ldots, -3,-2,-1,0,1,2,3 , \ldots  \, \}$
			\item  The rational numbers: $\mathbb{Q} = \{\frac{5}{3}, \frac{22}{7}, 1.5, 0.125,  -7, \, \ldots \, \}$
			\item  The real numbers: $\mathbb{R} = \{-1,0,1, \sqrt{2}, e,\pi, \;  4.94\ldots, \; \ldots \, \}$
			\item  The complex numbers: $\mathbb{C} = \{ -1, 0, 1, i,  1+i, 2+3i,  \ldots \, \}$
		\end{itemize}
		%
		These categories of numbers should be somewhat familiar to you.
		Think of them as neat classification labels for everything that you would normally call a number.
	 	Each group in the above list is a \emph{set}.
		A set is a collection of items of the same kind.
		Each collection has a name and a precise definition for which items belong in that collection.
		Note also that each of the sets in the list contains all the sets above it,												\index{set!subset}
		as illustrated in Figure~\ref{fig:nested_sets}.
		For now, we don't need to go into the details of sets and set notation,
		but we do need to be aware of the different sets of numbers.

		\begin{figure}[H]
			\centering
			\includegraphics[width=0.87\textwidth]{figures/math/nested_sets.pdf}
			\caption{	An illustration of the nested containment structure of the different number sets.
					The set of natural numbers is contained in the set of integers,
					which in turn is contained in the set of rational numbers.
					The set of rational numbers is contained in the set of real numbers,
					which is contained in the set of complex numbers.}
			\label{fig:nested_sets}
		\end{figure}

		\noindent
		Why do we need so many different sets of numbers?
		Each set of numbers is associated with more and more advanced mathematical problems.

		The simplest numbers are the natural numbers $\mathbb{N}$,
		which are sufficient for all your math needs if all you're going to do is \emph{count} things.
		How many goats? Five goats here and six goats there so the total is 11 goats.
		The sum of any two natural numbers is also a natural number.

		As soon as you start using \emph{subtraction} (the inverse operation of addition),
		you start running into negative numbers,
		which are numbers outside the set of natural numbers.
		If the only mathematical operations you will ever use are \emph{addition} and \emph{subtraction},
		then the set of integers $\mathbb{Z} = \{ \ldots, -2, -1, 0, 1, 2, \ldots \}$ will be sufficient.
		Think about it.
		Any integer plus or minus any other integer is still an integer.

		You can do a lot of interesting math with integers.
		There is an entire field in math called \emph{number theory} that deals with integers.
		However, to restrict yourself solely to integers is somewhat limiting---a rotisserie
		menu that offers $\frac{1}{2}$ of a chicken would be totally confusing.

		If you want to use division in your mathematical calculations,														\index{rational}
		you'll need the rationals~$\mathbb{Q}$.
		The set of rational numbers corresponds to all numbers that can be expressed as \emph{fractions} of the form $\frac{m}{n}$		\index{fraction}
		where $m$ and $n$ are integers, and $n \neq 0$.
		You can add, subtract, multiply, and divide rational numbers, and the result will always be a rational number.
		However, even the rationals are not enough for all of math!

		In geometry, we can obtain \emph{irrational} quantities like $\sqrt{2}$ (the diagonal of a square with side 1)
		and $\pi$ (the ratio between a circle's circumference and its diameter).
		There are no integers $x$ and $y$ such that $\sqrt{2}=\frac{x}{y}$,
		therefore we say that $\sqrt{2}$ is \emph{irrational} (not in the set $\mathbb{Q}$).
		An irrational number has an infinitely long decimal expansion that doesn't repeat.
		For example, $\pi = 3.141592653589793\ldots$ where the dots indicate
		that the decimal expansion of $\pi$ continues all the way to infinity.

		Combining the irrational numbers with the rationals gives us all the useful numbers,
		which we call the set of real numbers $\mathbb{R}$.
		The set $\mathbb{R}$ contains the integers,
		the rational numbers $\mathbb{Q}$,
		as well as irrational numbers like $\sqrt{2}=1.4142135\ldots$.
		By using the reals you can compute pretty much anything you want.
		From here on in the text, when I say \emph{number},
		I mean an element of the set of real numbers $\mathbb{R}$.

		The only thing you can't do with the reals is to take the square root of a negative number---you 							\index{complex number}
		need the complex numbers $\mathbb{C}$ for that.
		We defer the discussion on $\mathbb{C}$ until the end of Chapter~\ref{chapter:vectors}.

	\subsection{Exercises}
	\label{numbers:exercises}

		\input{problems/chapter1_numbers_exercises.tex}

%!TEX root = ../main.tex

%=======================================================================  hyperbola
\section{Hyperbola}
\label{sec:hyperbola}

	The \emphindexdef{hyperbola} is another fundamental shape of nature.

	\subsection{The conic sections}

		There is a deep connection between the geometric shapes of the circle,
		the ellipse, the parabola, and the hyperbola.
		These seemingly different shapes can be obtained, geometrically speaking,
		from a single object: the cone.															\index{cone}
		We can obtain the four curves by slicing the cone at different angles,
		as illustrated in Figure~\ref{fig:conic_sections_four-shapes}.

		\begin{figure}[htb]
			\centering
			\includegraphics[width=0.8\textwidth]{figures/math/conic_sections_four-shapes.pdf}
			\vspace{-2mm}
			\caption{	Taking slices through a cone at different angles produces different geometric shapes:
					a circle, an ellipse, a parabola, or a hyperbola.}
			\label{fig:conic_sections_four-shapes}
		\end{figure}

	\subsection{Conic sections in polar coordinates}

		All four conic sections can be described by the same function in polar coordinates:					\index{polar coordinates}
		\[
		  r(\theta) = \frac{q(1+\varepsilon)}{1 + \varepsilon\cos(\theta)}\,,
		\]
		where $q$ is the curve's closest distance to a focal point
		and $\varepsilon$ is the curve's eccentricity.												\index{eccentricity}
		For a circle, $q=R$ (the radius) and the eccentricity parameter is $\varepsilon=0$.
		For an ellipse, $q=a(1-\varepsilon)$ and the eccentricity parameter varies between $0$ and $1$ ($0\leq \varepsilon < 1$).
		Note we include the case $\varepsilon=0$ since a circle is a special case of an ellipse.
		For a parabola, $q=f$ (the focal length) and the eccentricity is $\varepsilon=1$.
		For a hyperbola, $q = a(\varepsilon-1)$ and the eccentricity is $\varepsilon>1$.

		We can use the eccentricity parameter $\varepsilon$ to classify all four curves.
		Depending on the value of $\varepsilon$,
		the equation $r(\theta)$ defines either a circle, an ellipse, a parabola, or a hyperbola.	\index{circle}\index{ellipse}\index{hyperbola}\index{parabola}
		Table~\ref{table:conics} summarizes all our observations regarding conic sections.

		\begin{table}[htb]
		{ \small

		\centering
		\begin{longtable}{@{}lllll@{}}
		\toprule
		Conic section 	\! 	&  Equation  	\!\!
		& Polar function
		&	Eccentricity \!\!\!\!\!\!\!\!\!\!\!\!				\\
		\midrule
		Circle				& $x^2+y^2=R^2$ \; 					& $r(\theta)=R$			& $\varepsilon = 0$								\\[1mm]
		Ellipse				& $\frac{x^2}{a^2}+\frac{y^2}{b^2}=1$	& $r(\theta)=\frac{a(1-\varepsilon^2)}{1 + \varepsilon\cos(\theta)}$ &	$\varepsilon\!=\!\sqrt{1\!-\!\frac{b^2}{a^2}}\,, \; 0\!\leq\!\varepsilon\!<\!1$\!\!	\\[2mm]
		Parabola				& $y^2=4fx$						& $r(\theta)=\frac{2f}{1 + \cos(\theta)}$ &	$\varepsilon=1$			 						\\[1mm]
		Hyperbola			& $\frac{x^2}{a^2}-\frac{y^2}{b^2}=1$	& $r(\theta)=\frac{a(\varepsilon^2-1)}{1 + \varepsilon\cos(\theta)}$ &	$\varepsilon\!=\!\sqrt{1\!+\!\frac{b^2}{a^2}}\,, \; 1\!<\!\varepsilon\!<\!\infty$ \\[1mm]
		\bottomrule
		\end{longtable}

		}
		\vspace{4mm}
		\caption{The four conic sections and their eccentricity parameters. }
		\label{table:conics}
		\end{table}

		The motion of the planets is explained by Newton's law of gravitation.
		The gravitational interaction between two bodies always leads one of the two bodies to follow a trajectory described by one of the conic sections
		for which the other body is the focal point.
%!TEX root = ../main.tex

\section{Math problems}
\label{sec:math_problems}

	We've now reached the first section of problems in this book.
	The purpose of these problems is to give you a way to comprehensively practice your math fundamentals.

{\small

\begin{problems}{ch1}

	\vspace*{3mm}

	%%%  SOLVING EQUATIONS     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

	\begin{problem}
		Solve for $x$ in the equation $x^2-9=7$.
		\begin{answer}$x=\pm 4$.\end{answer}
	\end{problem}

	\begin{problem}
		Solve for $x$ in the equation $\cos^{-1}\!\left( \frac{x}{A} \right) - \phi = \omega t$.
		\begin{answer}$x=A\cos(\omega t+\phi)$.\end{answer}
	\end{problem}

	\begin{problem}		\label{mathprob:ch1:fractions2}
		Solve for $x$ in the equation $\frac{1}{x}=\frac{1}{a}+\frac{1}{b}$.
		\begin{answer}$x=\frac{ab}{a+b}$.\end{answer}
	\end{problem}

	\begin{problem}
		Use a calculator to find the values of the following expressions:
		\fourcol
			\textbf{a)}~$\sqrt[4]{3^3}$

			\textbf{b)}~$2^{10}$

			\textbf{c)}~$7^{^{\frac{1}{4}}}-10$

			\textbf{d)}~$\frac{1}{2}\ln(e^{22})$
		\endfourcol
		%\begin{hint}
		%\end{hint}
		\begin{answer}\textbf{a)}~$2.2795$.
					\textbf{b)}~$1024$.
					\textbf{c)}~$-8.373$.
					\textbf{d)}~$11$.\end{answer}
		%\begin{solution}
		%\end{solution}
	\end{problem}

	%% FRACTIONS    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

	\begin{problem}
		Compute the following expressions involving fractions:
		\threecol
			\textbf{a)}~$\dfrac{1}{2} + \dfrac{1}{4}$

			\textbf{b)}~$\dfrac{4}{7} - \dfrac{23}{5}$

			\textbf{c)}~$1\frac{3}{4} + 1\frac{31}{32}$
		\endthreecol
		\begin{answer}\textbf{a)}~$\frac{3}{4}$.
					\textbf{b)}~$\frac{-141}{35}$.
					\textbf{c)}~$3\frac{23}{32}$.\end{answer}
		\begin{solution}
			For \textbf{c)},
			$1\frac{3}{4} + 1\frac{31}{32}
				= \frac{7}{4} + \frac{63}{32}
				= \frac{56}{32} + \frac{63}{32} = \frac{119}{32}=3\frac{23}{32}$.
		\end{solution}
	\end{problem}

\end{problems}

} %/small


\documentclass[11pt,landscape]{article}
%\documentclass[11pt]{article}
\usepackage[top=1cm,bottom=1cm]{geometry}
\usepackage[T1]{fontenc}
\usepackage{url}
\usepackage{hyperref}
\usepackage{xcolor}
%
\usepackage{amsthm}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{ifthen}
\usepackage{mathpartir}
\input{../../slides/macros}
%
\begin{document}

% \title{CO663 Part II: Worksheet 3 (model answer)}
\title{CO663 Part II: Worksheet 2 (model answer)}

\date{\vspace{-5ex}}
\maketitle

\pagenumbering{gobble}

\newcommand{\answerbox}[1]{\framebox{\parbox[c][#1]{\textwidth}{
      \color{white}{h}%
    }}}

\section{Introduction}

The objective of this class is to put in practice what we have covered
in the lectures so far.
%
We will focus on Language $\lggeEF$.
%
Type-check \emph{and} evaluate (until your reach a value)
\emph{three} of the following expressions.
%
If you do not have much time, you should focus on evaluation.
%
% There is two modes depending on whether you are working in groups or
% not.
% %
% You should answer at least one mode.
%
% Submit your answer on Moodle (pictures of readable handwriting is
% accepted).

% \begin{center}
%   \emph{This class assessment is worth {\color{red} 6} points of your final mark for this module.}
% \end{center}

% \section{Interactive mode}
% %

% Form a team of two, then follow \emph{individually} the steps below
% \underline{\emph{twice}}.

% \begin{enumerate}
% \item Write down \emph{one expression} in Language $\lggeEF$ that you
%   think is well-typed and you have \underline{not} seen before. Make
%   sure that the expression ($i$) is syntactically correct and ($ii$)
%   contains at least \emph{three} operators.\footnote{The depth of the
%     abstract syntax tree is at least $3$.}
%   %
% \item \emph{Exchange} what you have produced in Step 1 with your
%   teammate.
%   %
% \item \emph{Check} that the expression you are given is
%   syntactically correct (if not, fix it).

% \item \emph{Type-check} (formally) the expression.
%   %
% \item \emph{Exchange} what you have produced in Step 4 with your
%   teammate.
%   %
% \item \emph{Check} that the type derivation you are given is correct
%   (if not, fix it).
%   %
% \item \emph{Evaluate} the expression (until you reach a value).
%   %
% \item \emph{Exchange} what you have produced in Step 7 with your
%   teammate.
%   %
% \item \emph{Check} that the evaluation derivation(\emph{s}) you are
%   given are correct (if not, fix them).
% \end{enumerate}

% \section{Passive mode}

%
\begin{enumerate}
\item $\clam{\enum{}}{x}{\etimes{\var{x}}{\var{x}}}$
  %
\item { $\elet{\clam{\enum{}}{x}{
        \clam{\enum{}}{y}{
          \etimes{\var x}{\var y}
        }
      }
    }{f}{
      \eapp
      {\eapp
        {\var{f}}
        {\enum{4}}
      }
      {\enum{4}}
    }
    $}

\item $\clam{(\tau_1 \rightarrow \tau_1) \rightarrow \tau_2}{t}{
    \clam{\tau_3}{v}{
      \capp{\var{t}}{\clam{\tau_1}{u}{\var u}}
    }
  }$

  Hint: start with $((\tau_1 \rightarrow \tau_1) \rightarrow \tau_2) \rightarrow \tau_3 \rightarrow \tau_2$

\item $\elet{\clam{\enum{}}{x}{
      \eapp{\var f}{\eplus{\var{x}}{\enum{1}}}
    }
  }{f}{
    \eapp{\var{f}}{0}
  }$
\end{enumerate}

\section*{\LaTeX\ template}

If you would like to use \LaTeX\ to typeset your solution, you can use
the template here:
\url{https://github.com/ukc-co663/pl-design-latex-template}.

% \end{document}

\newpage

\section{Model answer}
For the sake of space I have removed the prefix of the rule names,
e.g., I write {\sc apn} for {\sc \bf EF}$-${\sc se}$-${\sc
  apn}.

\subsection{Expression \#1}

\subsubsection{Type derivation}

\[
\tyrule{{lam}}
{
  \tyrule{{times}}
  {
    \tyrule{{var}}
    {\,}
    {
      \tyjudge{\var{x} : \enum{}}
      {\var{x}}
      {\enum{}}
    }
    \\
    \tyrule{{var}}
    {\,}
    {
      \tyjudge{\var{x} : \enum{}}
      {\var{x}}
      {\enum{}}
    }
  }
  {  \tyjudge{\var{x} : \enum{}}
    {\etimes{\var{x}}{\var{x}}}
    {\enum{}}
  }
}
{
  \tyjudge{\emptyset}
  {\clam{\enum{}}{x}{\etimes{\var{x}}{\var{x}}}}
  {\enum{} \rightarrow \enum{}}
}
\]

\subsubsection{Evaluation} It is a value, we cannot evaluate further.

\[
\semrule{lam}{\,}
{\valjudge{\clam{\enum{}}{x}{\etimes{\var{x}}{\var{x}}}}}
\]

\subsection{Expression \#2}

\subsubsection{Type derivation}
\[
\tiny
\tyrule{let}
{
  \tyrule
  {lam}
  {
    \tyrule{lam}
    {
      \tyrule{times}
      {
        \tyrule{var}
        {\,}
        {
          \tyjudge{\var{x}: \enum{}, \var{y}: \enum{}}{\var{x}}{\enum{}}
        }
        \\
        \tyrule{var}
        {\,}
        {
          \tyjudge{\var{x}: \enum{}, \var{y}: \enum{}}{\var{y}}{\enum{}}
        }
      }
      {
        { \tyjudge{\var{x}: \enum{}, \var{y}: \enum{}}{{{
                \etimes{\var x}{\var y}
              }
            }
          }{\enum{}}}
      }
    }
    { \tyjudge{\var{x}: \enum{}}{{
          \clam{\enum{}}{y}{
            \etimes{\var x}{\var y}
          }
        }
      }{\enum{} \rightarrow \enum{}}
    }
  }
  { \tyjudge{\emptyset}{\clam{\enum{}}{x}{
        \clam{\enum{}}{y}{
          \etimes{\var x}{\var y}
        }
      }
    }{\enum{} \rightarrow (\enum{} \rightarrow \enum{})}}
  \\
  \tyrule{ap}
  {
    \tyrule{ap}
    {
      \tyrule{var}
      {\,}
      {
        \tyjudge{\var{f} : {\enum{} \rightarrow (\enum{} \rightarrow \enum{})}}
        {
          {\var{f}}
        }
        {\enum{} \rightarrow (\enum{} \rightarrow \enum{})}
      }
      \\
      \tyrule{num}
      {\,}
      {
        \tyjudge{\var{f} : (\ldots)}
        {\enum{4}}
        {\enum{}}
      }
    }
    {
      \tyjudge{\var{f} : {\enum{} \rightarrow (\enum{} \rightarrow \enum{})}}
      {\eapp
        {\var{f}}
        {\enum{4}}
      }
      {\enum{} \rightarrow \enum{}}
    }
    \\
    \tyrule{num}
    {\,}
    {
      \tyjudge{\var{f} : (\ldots)}
      {\enum{4}}
      {\enum{}}
    }
  }
  { \tyjudge{\var{f} : {\enum{} \rightarrow (\enum{} \rightarrow \enum{})}}{
      \eapp
      {\eapp
        {\var{f}}
        {\enum{4}}
      }
      {\enum{4}}
    }{\enum{}}}
}
{
  \tyjudge{\emptyset}{
    \elet{\clam{\enum{}}{x}{
        \clam{\enum{}}{y}{
          \etimes{\var x}{\var y}
        }
      }
    }{f}{
      \eapp
      {\eapp
        {\var{f}}
        {\enum{4}}
      }
      {\enum{4}}
    }
  }
  {\enum{}}
}
\]

\subsubsection{Evaluation (call-by-name)}

For convenience we define the following expressions:
\begin{itemize}
\item $F \defi {\clam{\enum{}}{x}{
      \clam{\enum{}}{y}{
        \etimes{\var x}{\var y}
      }
    }
  } $
\item $B \defi {
    \eapp
    {\eapp
      {\var{f}}
      {\enum{4}}
    }
    {\enum{4}}
  }$
\end{itemize}

Next, we evaluate step-by-step until we reach a value:

\begin{enumerate}
\item
  \[
  \semrule
  {let}
  {\,}
  {\jtrans{
      \elet{F}{f}{B}
    }{
      {
        \subs{B}{F}{x} \equiv
        {
          \eapp
          {\eapp
            {F}
            {\enum{4}}
          }
          {\enum{4}}
        }
      }
    }
  }
  \]

\item \[
  \semrule{ap}
  {
    \semrule
    {apn}
    {
      \,
    }
    {
      \jtrans{{\eapp
          {F}
          {\enum{4}}
        }
      }
      {
        {
          \clam{\enum{}}{y}{
            \etimes{\enum{4}}{\var y}
          }
        }
      }
    }
  }
  {
    \jtrans{
      \eapp
      {\eapp
        {F}
        {\enum{4}}
      }
      {\enum{4}}
    }{
      \eapp{
        \clam{\enum{}}{y}{
          \etimes{\enum{4}}{\var y}
        }
      }
      {\enum{4}}
    }
  }
  \]
\item
  \[
  \semrule{apn}
  {\,}
  {\jtrans{
      \eapp{
        \clam{\enum{}}{y}{
          \etimes{\enum{4}}{\var y}
        }
      }
      {\enum{4}}
    }{ \etimes{\enum{4}}{\enum{4}}}}
  \]
\item
  \[
  \semrule{times}
  {\,}
  {\jtrans{ \etimes{\enum{4}}{\enum{4}}}{\enum{16}}}
  \]
\item
  \[
  \valjudge{\enum{16}}
  \]
\end{enumerate}

\subsection{Expression \#3}

\subsubsection{Type derivation}

We the concrete syntax:

\[
\tiny
\tyrule{lam}
{
\tyrule{lam}
{
  \tyrule{ap}
  {
    \tyrule{var}
    {\,}
    {
      \tyjudge{\var{t} : (\tau_1 \rightarrow \tau_1) \rightarrow \tau_2, \var{v} : \tau_3}
      {\var{t}}
      {(\tau_1 \rightarrow \tau_1) \rightarrow \tau_2}
    }
    \\
    \tyrule{lam}
    {
      \tyrule{var}
      {\,}
      {
        \tyjudge{\var{t} : (\tau_1 \rightarrow \tau_1) \rightarrow \tau_2, \var{v} : \tau_3, \var{u} : \tau_1}
        {\var u}
        {\tau_1}
      }
    }
    {
      \tyjudge{\var{t} : (\tau_1 \rightarrow \tau_1) \rightarrow \tau_2, \var{v} : \tau_3}
      {\clam{\tau_1}{u}{\var u}}
      {\tau_1 \rightarrow \tau_1}
    }
  }
  {
    \tyjudge{\var{t} : (\tau_1 \rightarrow \tau_1) \rightarrow \tau_2, \var{v} : \tau_3}
    {
        \capp{\var{t}}{\clam{\tau_1}{u}{\var u}}
    }
    {\tau_2}
  }
}
{
  \tyjudge{\var{t} : (\tau_1 \rightarrow \tau_1) \rightarrow \tau_2}
  {
      \clam{\tau_3}{v}{
        \capp{\var{t}}{\clam{\tau_1}{u}{\var u}}
      }
  }
  {\tau_3 \rightarrow \tau_2}
}
}
{
  \tyjudge{\emptyset}
  {
    \clam{(\tau_1 \rightarrow \tau_1) \rightarrow \tau_2}{t}{
      \clam{\tau_3}{v}{
        \capp{\var{t}}{\clam{\tau_1}{u}{\var u}}
      }
    }
  }
  {((\tau_1 \rightarrow \tau_1) \rightarrow \tau_2) \rightarrow \tau_3 \rightarrow \tau_2}
}
\]

With the abstract syntax:

\[
\tiny
\tyrule{lam}
{
\tyrule{lam}
{
  \tyrule{ap}
  {
    \tyrule{var}
    {\,}
    {
      \tyjudge{\var{t} : {\tyarr{\tyarr{\tau_1}{\tau_1}}{\tau_2}}, \var{v} : \tau_3}
      {\var{t}}
      {\tyarr{\tyarr{\tau_1}{\tau_1}}{\tau_2}}
    }
    \\
    \tyrule{lam}
    {
      \tyrule{var}
      {\,}
      {
        \tyjudge{\var{t} : {\tyarr{\tyarr{\tau_1}{\tau_1}}{\tau_2}}, \var{v} : \tau_3, \var{u} : \tau_1}
        {\var u}
        {\tau_1}
      }
    }
    {
      \tyjudge{\var{t} :{\tyarr{\tyarr{\tau_1}{\tau_1}}{\tau_2}}, \var{v} : \tau_3}
      {\clam{\tau_1}{u}{\var u}}
      {\tyarr{\tau_1}{\tau_1}}
    }
  }
  {
    \tyjudge{\var{t} :{\tyarr{\tyarr{\tau_1}{\tau_1}}{\tau_2}}, \var{v} : \tau_3}
    {
        \eapp{\var{t}}{\clam{\tau_1}{u}{\var u}}
    }
    {\tau_2}
  }
}
{
  \tyjudge{\var{t} :{\tyarr{\tyarr{\tau_1}{\tau_1}}{\tau_2}}}
  {
      \clam{\tau_3}{v}{
        \eapp{\var{t}}{\clam{\tau_1}{u}{\var u}}
      }
  }
  {\tyarr{\tau_3}{\tau_2}}
}
}
{
  \tyjudge{\emptyset}
  {
    \clam{{\tyarr{\tyarr{\tau_1}{\tau_1}}{\tau_2}}}{t}{
      \clam{\tau_3}{v}{
        \eapp{\var{t}}{\clam{\tau_1}{u}{\var u}}
      }
    }
  }
  {\tyarr{
      \tyarr{\tyarr{\tau_1}{\tau_1}}{\tau_2}}
    {\tyarr{\tau_3}{\tau_2}}
  }
}
\]

\subsubsection{Evaluation}

The expression is already a value:

\[
\valjudge{
    \clam{(\tau_1 \rightarrow \tau_1) \rightarrow \tau_2}{t}{
      \clam{\tau_3}{v}{
        \capp{\var{t}}{\clam{\tau_1}{u}{\var u}}
      }
    }
  }
\]

\subsection{Expression \#4}

\subsubsection{Type derivation}

This expression is ill-typed because $\var{f}$ is used outside of its
scope.
%
When an expression does not type check, it is enough to show that one
branch of the derivation tree fails. Here I show the whole tree for
illustrative purpose.
\[
\tiny
\tyrule{let}
{
  \tyrule
  {ap}
  {
    \tyrule{lam}
    {
      \tyrule{}
      {{\color{red} f \text{ undecl.}}}
      {
        \tyjudge{\var{x} : \enum{}}
        {\var f}
        {{\enum{}} \rightarrow \enum{}}
      }
      \\
      \tyrule{plus}
      {
        \tyrule{var}
        {p}
        { \tyjudge{\var{x} : \enum{}}
          {\var{x}}
          {\enum{}}
        }
        \\
        \tyrule{num}
        {\,}
        { \tyjudge{\var{x} : \enum{}}
          {\enum{1}}
          {\enum{}}
        }
      }
      {
        \tyjudge{\var{x} : \enum{}}
        {\eplus{\var{x}}{\enum{1}}}
        {\enum{}}
      }
    }
    {
      \tyjudge{\var{x} : \enum{}}
      {
        \eapp{\var f}{\eplus{\var{x}}{\enum{1}}}
      }
      {\enum{}}
    }
  }
  {
    \tyjudge{\emptyset}
    {\clam{\enum{}}{x}{
        \eapp{\var f}{\eplus{\var{x}}{\enum{1}}}
      }
    }
    {\enum{} \rightarrow \enum{}}
  }
  \\
  \tyrule
  {ap}
  {
    \tyrule
    {var}
    {\,}
    {
      \tyjudge{\var{f} : \enum{} \rightarrow \enum{}}
      {\var{f}}
      {\enum{} \rightarrow \enum{}}
    }
    \\
    \tyrule
    {num}
    {\,}
    {
      \tyjudge{\var{f} : \enum{} \rightarrow \enum{}}
      {\enum{0}}
      {\enum{}}
    }
  }
  {
    \tyjudge{\var{f} : \enum{} \rightarrow \enum{}}
    {
      \eapp{\var{f}}{\enum{0}}
    }
    {\enum{}}
  }
}
{
  \tyjudge{\emptyset}
  {
    \elet{\clam{\enum{}}{x}{
        \eapp{\var f}{\eplus{\var{x}}{\enum{1}}}
      }
    }{f}{
      \eapp{\var{f}}{\enum{0}}
    }
  }
  {
    \enum{}
  }
}
\]
\subsubsection{Evaluation (call-by-name)}

For convenience we define the following expressions:
\begin{itemize}
\item $F \defi {\clam{\enum{}}{x}{
        \eapp{\var f}{\eplus{\var{x}}{\enum{1}}}
      }
    }$
\item $B \defi  \eapp{\var{f}}{\enum{0}}$
\end{itemize}

This expression is ill-typed, but we can still evaluate it (and
possibly get stuck).
%
We evaluate step-by-step until we cannot apply any rule:

\begin{enumerate}
\item
\[
\semrule{let}
{\,}
{
  \jtrans{\elet{F}{f}{B}}
  { \subs{B}{F}{x} \equiv  \eapp{F}{\enum{0}}}
}
\]
\item
  \[
  \semrule{apn}
  {\,}
  {\jtrans
    {\eapp{F}{\enum{0}}}
    {
      {{
        \eapp{\var f}{\eplus{\enum{0}}{\enum{1}}}
      }
    }
    }
  }
  \]
\item Now we are stuck with ${
        \eapp{\var f}{\eplus{\enum{0}}{\enum{1}}}
      }$ since $\var{f}$ is ``unknown''.
\end{enumerate}

\textbf{NB:}
We can fix this expression by replacing $\var{f}$ in the left branch
of the let by, e.g., the identity function --- ${\clam{\enum{}}{y}{\var{y}}}$:
\[
{
  \elet{\clam{\enum{}}{x}{
      \eapp{\clam{\enum{}}{y}{\var{y}}}{\eplus{\var{x}}{\enum{1}}}
    }
  }{f}{
    \eapp{\var{f}}{\enum{0}}
  }
}
\]

\end{document}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:


\documentclass[jou]{apa6}

\usepackage[american]{babel}

\usepackage{csquotes}
\usepackage[style=apa,sortcites=true,sorting=nyt,backend=biber]{biblatex}
\DeclareLanguageMapping{american}{american-apa}
\addbibresource{bibliography.bib}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Discrete Structures
%% The start of RBS stuff
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Working internal and external links in PDF
\usepackage{hyperref}
% Extra math symbols in LaTeX
\usepackage{amsmath}
\usepackage{gensymb}
\usepackage{amssymb}
% Enumerations with (a), (b), etc.
\usepackage{enumerate}

\let\OLDitemize\itemize
\renewcommand\itemize{\OLDitemize\addtolength{\itemsep}{-6pt}}

\usepackage{etoolbox}
\makeatletter
\preto{\@verbatim}{\topsep=3pt \partopsep=3pt }
\makeatother

% These sizes redefine APA for A4 paper size
\oddsidemargin 0.0in
\evensidemargin 0.0in
\textwidth 6.27in
\headheight 1.0in
\topmargin -24pt
\headheight 12pt
\headsep 12pt
\textheight 9.19in

\title{Sample Quiz 8}
\author{Discrete Structures, Spring 2020}
\affiliation{RBS}

\leftheader{Discrete Sample Quiz 8}

\abstract{%
}

%\keywords{}

\setlength\parindent{0pt}

\begin{document}

%\thispagestyle{empty}

\twocolumn
\section{Worksheet 10: Binary Relations}

\vspace{6pt}
{\bf Question 1 (A reminiscence about variance)}\\
Assume that you want to encode six-letter alphabet
$\mathcal{A} = \{ \mathtt{a}, \mathtt{b}, \mathtt{c}, \mathtt{d}, \mathtt{e}, \mathtt{f} \}$
and transmit it over a computer network.
You assign $2$ or $3$ bit codes to these letters:

\begin{tabular}{|l|l|} \hline
{\tt a} & {\tt 00} \\ \hline
{\tt b} & {\tt 01} \\ \hline
{\tt c} & {\tt 100} \\ \hline
{\tt d} & {\tt 101} \\ \hline
{\tt e} & {\tt 110} \\ \hline
{\tt f} & {\tt 111} \\ \hline
\end{tabular}

For example, the 11-bit sequence {\tt "10100100110"} means {\tt "dace"}.
Denote by $X$ the random variable \textendash{} the number of
bits used to encode a single letter. (All $6$ letters have
equal probabilities.)

Find $E(X)$ and $V(X)$.\\
Write them as two fractions: {\tt P1/Q1,P2/Q2}\\
(Separate the fractions by comma,
do not leave any spaces.)

\vspace{6pt}
{\bf Question 2 (Rosen7e, Ch.9, Q10-Q23).}\\
Determine whether the binary relation is: (1) reflexive, (2) symmetric, (3) antisymmetric, (4) transitive.
Express your answer as as a 4-letter string of T/F (true/false values that are
answer to these $4$ questions). For example, {\tt TFTT} etc.
%For example, the relationship $a \leq b$ on $\mathbb{Z}$ has these answers: {\tt TFTT}. It is reflexive, antisymmetric
%and transitive. But $\leq$ is not symmetric ($a \leq b$ does not imply that $b \leq a$).
\begin{enumerate}[(A)]
\item The relation $R$ on $\{1, 2, 3,\ldots\}$ where $aRb$ means $a\,\mid\,b$.
\item The relation $R$ on $\{w, x, y, z\}$ where $R = \{(w, w), (w, x), (x, w), (x, x), (x, z), (y, y), (z, y), (z, z)\}$.
\item The relation $R$ on $\mathbb{Z}$ where $aRb$ means $|a - b| \leq 1$.
\item The relation $R$ on $\mathbb{Z}$ where $aRb$ means $a \neq b$.
\item The relation $R$ on $\mathbb{Z}$ where $aRb$ means that the units digit of $a$ is equal to the units digit of $b$.
\item The relation $R$ on the set of all subsets of $\{1, 2, 3, 4\}$ where $SRT$ means $S \subseteq T$.
\item The relation $R$ on the set of all people where $aRb$ means that a is younger than $b$.
\item The relation $R$ on the set $\{(a, b) \,\mid\, a, b \in \mathbb{Z}\}$ where $(a, b)R (c, d)$ means $a = c$ or $b = d$.
\end{enumerate}

\vspace{6pt}
{\bf Question 3 (Rosen7e, Ch.9, Q35-Q38).}\\
Construct a matrix of the relations defined below. Output the matrix as a list of lists:\\
{\tt [[a11,a12,...],[a21,a22,...],...]}
\begin{enumerate}[(A)]
\item $R$ on $\{1, 2, 3, 4, 6, 12\}$ where $aRb$ means $a\,\mid\,b$.
\item $R$ on $\{1, 2, 3, 4, 6, 12\}$ where $aRb$ means $a \leq b$.
\item $R^2$, where $R$ is the relation on $\{1, 2, 3, 4\}$ such that $aRb$
means $|a - b| \leq 1$.
\end{enumerate}

\vspace{6pt}
{\bf Question 4 (Rosen7e, Ch.9, Q42).}\\
Define
$$M_R = \left( \begin{array}{cccc}
1 & 0 & 1 & 0 \\
1 & 0 & 0 & 1 \\
1 & 1 & 1 & 0 \\
1 & 1 & 0 & 1 \\
\end{array} \right)$$
determine if $R$ is: (1) reflexive (2) symmetric (3) antisymmetric (4) transitive.
Express your answer as as a 4-letter string of T/F (true/false values that are
answer to these $4$ questions). For example,
{\tt TFTT} etc.

\vspace{6pt}
{\bf Question 5 (Rosen7e, Ch.9, Q47).}\\
Let $A$ be the set of all positive divisors of $60$ (including $1$ and $60$ itself).
Draw the Hasse diagram for the relation $R$ on $A$ where $aRb$ means $a\,\mid\,b$.

\vspace{6pt}
{\bf Question 6 (Rosen7e, Ch.9, Q51).}\\
Find the transitive closure of $R$ if
$$M_R  = \left( \begin{array}{cccc}
1 & 0 & 1 & 0 \\
1 & 0 & 0 & 1 \\
0 & 1 & 1 & 0 \\
0 & 1 & 0 & 0 \\
\end{array} \right).$$

\vspace{6pt}
{\bf Question 7 (Rosen7e, Ch.9, Q59).}\\
Find the join of the 3-ary relation:
\begin{verbatim}
{ (Wages,MS410,N507),
  (Rosen,CS540,N525),
  (Michaels,CS518,N504),
  (Michaels,MS410,N510) }
\end{verbatim}
and the 4-ary relation:
\begin{verbatim}
{ (MS410,N507,Monday,6:00),
  (MS410,N507,Wednesday,6:00),
  (CS540,N525,Monday,7:30),
  (CS518,N504,Tuesday,6:00),
  (CS518,N504,Thursday,6:00) }
\end{verbatim}
with respect to the last two fields of the first relation and
the first two fields of the second relation.

\vspace{6pt}
{\bf Question 8 (Rosen7e, Ch.9, Q69-Q71).}
Give an example of a relation or state that there are none.\\
{\bf (A)} A relation on $\{a, b, c\}$ that is reflexive and transitive, but not antisymmetric.\\
{\bf (B)} A relation on $\{1, 2\}$ that is symmetric and transitive, but not reflexive.\\
{\bf (C)} A relation on $\{1, 2, 3\}$ that is reflexive and transitive, but not symmetric.

\vspace{6pt}
{\bf Question 9 (Rosen7e, Ch.9, Q73).}\\
Suppose $|A| = 7$. Find the number of reflexive, symmetric binary relations on $A$.

\newpage

\subsection{Answers}

\vspace{6pt}
{\bf Question 1.}
The random variable $X$ takes value $x_1 = 2$ (with probability $p_1 = \frac{1}{3}$)
and value $x_2 = 3$ (with probability $p_2 = \frac{2}{3}$).
We can compute:
$$E(X) = x_1p_1 + x_2p_2 = \frac{8}{3}.$$
$$V(X) = (x_1 - E(X))^2p_1 + (x_2 - E(X))^2p_2 = \frac{2}{9}.$$

\vspace{6pt}
{\bf Question 2.}
\begin{enumerate}[(A)]
\item {\tt TFTT}
\item {\tt TFFF}
\item {\tt TTFF}
\item {\tt FTFF}
\item {\tt TTFT}
\item {\tt TFTT}
\item {\tt FFTT}
\item {\tt TTFF}
\end{enumerate}

\vspace{6pt}
{\bf Question 3.}\\
{\bf (A)} Divisibility on the set $\{1, 2, 3, 4, 6, 12\}$
$$M_R  = \left( \begin{array}{cccccc}
1 & 1 & 1 & 1 & 1 & 1 \\
0 & 1 & 0 & 1 & 1 & 1 \\
0 & 0 & 1 & 0 & 1 & 1 \\
0 & 0 & 0 & 1 & 0 & 1 \\
0 & 0 & 0 & 0 & 1 & 1 \\
0 & 0 & 0 & 0 & 0 & 1 \\
\end{array} \right).$$
{\bf (B)} Relation $\leq$ on the set $\{1, 2, 3, 4, 6, 12\}$
$$M_R  = \left( \begin{array}{cccccc}
1 & 1 & 1 & 1 & 1 & 1 \\
0 & 1 & 1 & 1 & 1 & 1 \\
0 & 0 & 1 & 1 & 1 & 1 \\
0 & 0 & 0 & 1 & 1 & 1 \\
0 & 0 & 0 & 0 & 1 & 1 \\
0 & 0 & 0 & 0 & 0 & 1 \\
\end{array} \right).$$
{\bf (C)} Relation $R^2$, where $aRb$ iff $|a-b| \leq 1$.
$$M_R  = \left( \begin{array}{cccc}
1 & 1 & 1 & 0 \\
1 & 1 & 1 & 1 \\
1 & 1 & 1 & 1 \\
0 & 1 & 1 & 1 \\
\end{array} \right).$$
The only pairs that do not belong to $R^2$ are
$(1;4)$ and $(4;1)$.

\vspace{6pt}
{\bf Question 4.} Answer: {\tt FFFF}\\
Imagine that the relation $R$ is defined on a set of these 4 elements:
$a,b,c,d$.
\begin{itemize}
\item $R$ is not reflexive, since $bRb$ is false (the matrix has $m_{22}=0$).
\item $R$ is not symmetric, since $bRa$ does not imply $aRb$
(the matrix has $m_{12}=0$, but $m_{21}=1$).
\item $R$ is not antisymmetric, since $aRc$ and $cRa$ both hold, but
$a \neq c$.
\item $R$ is not transitive, since $aRc$ and $cRb$, but $aRb$ is not true.
\end{itemize}

\vspace{6pt}
{\bf Question 5}:\\
Hasse diagram connects only those numbers $a,b$ where $a$ divides $b$, and
there is no third  number in-between (such that $a\,\mid\,c$ and $c\,\mid\,b$).
For example, $1$ and $2$ are connected, but $1$ and $4$ are not
(because the relation $1\,\mid\,4$ can be inferred from
$1\,\mid\,2$ and $2\,\mid\,4$).

\begin{figure}[!htb]
\center{\includegraphics[width=2.5in]{quiz-sample-10/hasse-diagram-60.png}}
\caption{\label{fig:hasse-diagram-60} Hasse diagram: Divisors of 60.}
\end{figure}

Note that the Hasse diagram for divisibility is centrally symmetric.
(This is true for any set of divisors for some number.)

\vspace{6pt}
{\bf Question 6}:\\
$$M_R  = \left( \begin{array}{cccc}
1 & 0 & 1 & 0 \\
1 & 0 & 0 & 1 \\
0 & 1 & 1 & 0 \\
0 & 1 & 0 & 0 \\
\end{array} \right).$$

Denote the elements of the set by $a,b,c,d$. We want to find,
what are the ``relational paths'' between them:
\begin{itemize}
\item Path $aRc$, $cRb$, $bRd$ adds $(a,b)$, $(a,d)$ to the transitive closure.
\item Path $bRa$, $aRc$, $cRb$ adds $(b,c)$, $(b,b)$ to the transitive closure.
\item Path $cRb$, $bRd$ adds $(c,d)$ to the transitive closure.
\item Path $cRb$, $bRa$ adds $(c,a)$ to the transitive closure.
\item Path $dRb$, $bRa$, $aRc$ adds $(d,a)$, $(d,c)$.
\item Path $dRb$, $bRd$ adds $(d,d)$.
\end{itemize}

Here is the matrix
of the transitive closure after all the new pairs are added:

$$M_{R\ast}  = \left( \begin{array}{cccc}
1 & 1 & 1 & 1 \\
1 & 1 & 1 & 1 \\
1 & 1 & 1 & 1 \\
1 & 1 & 1 & 1 \\
\end{array} \right).$$

\vspace{6pt}
{\bf Question 7}\\
TBD (see Section 9.2.4 of the textbook (Definition 4, page 615)). This problem
is based entirely on applying the definition.

\vspace{6pt}
{\bf Question 8}\\
{\bf (A)} TBD\\
{\bf (B)}. Yes. We can have a relation $R$ which is
never satisfied. It is symmetric and also transitive
(since $xRy$ and $yRz$ can never happen, so we do not
need to care about $xRz$).\\
{\bf (C)}. TBD.

\vspace{6pt}
{\bf Question 9} Answer: {\tt 2097152}\\
The matrix $M$ of any relation $R$ on a set of $7$ elements has $49$ entries.
The entries on the main diagonal ($m_{11},m_{22},\ldots,m_{77}$)
should all equal $1$ ($R$ is reflexive).
Also, any entry $m_{ij}$ above the main diagonal ($i<j$ - row number is
less than the column number) is symmetric to some entry below the diagonal
$m_{ji}$ (where $i>j$).

Therefore we can freely choose only those $m_{ij}$ that are above the main
diagonal; everything else is predetermined. There are $1+2+\ldots+6=21$ such
elements in the matrix. The total number of ways to choose them is
$2^{21} = 2097152$.

\end{document}


%% \listfiles
\documentclass[apj]{emulateapj}
%\documentclass[preprint2,12pt]{emulateapj}
%% \usepackage{natbib}
\usepackage{graphicx}
\usepackage{epsfig}
\usepackage{amssymb,amsmath}
\usepackage{array}
\usepackage{threeparttable}

\singlespace

%definitions
\newcommand{\Msol}{${\rm M_{\sun}}$}

%% Editing markup...
\usepackage{color}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% WARNING: This LaTeX block was generated automatically by authors.py
% Do not change by hand: your changes will be lost.

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

% --------------------- Ancillary information ---------------------
\shortauthors{SURP et al.}
\shorttitle{my short-title}
\slugcomment{Draft: \today}

\begin{document}

\title{CTA200 Assignment 2}
 %% ---------

\author{Rosalind Liang\altaffilmark{1}}
\altaffiltext{1}{CITA, University of Toronto}

\section{Question 1}
\label{sec:Question 1}

In this question, we are asked to study the behaviour of the sequence $z_{i+1}=z_i^2+c$ in the complex plane, where $c = x + yi$ and $-2<x<2,   -2<y<2$. In particular, we seek to 1) distinguish the points on this domain for which the sequence converges and the points on which it diverges; 2) to map the iteration numbers for which each divergence occurs.
\subsection{Q1 methods}
\label{sec:cmb_data}

Part 1: To locate the coordinates for which the sequence diverges, I used a for loop to pass through 100 iterations of the sequence and collected points of divergence with np.nan(z), which recognizes when either the real or imaginary component of z is no longer a recognizable number (suggesting divergence to infinity). Matplotlib was then used to plot these coordinates.

Part 2: A similar for loop  was used with np.isfinite(z) to count the number of iterations each coordinate took before diverging. For cases where divergence did not occur within 100 iterations, a special value of -20 was assigned to create contrast in the colourmap, before plotting with matplotlib again.

\subsection{Q1 esults}
\label{sec:Q1 results}
The resulting plot of part 1 is shown in Figure 1, where all points of divergence are in yellow and points that remain bounded are in black. The plot for part 2 is shown in Figure 2, where all bounded points are in purple and the rest of the plot is a colourmap of each point's divergence iteration. We observe a fractal shaped zone where the sequence stays bounded, ranging from -1 to 1 in the complex axis and -2 to .5 in the real axis. In figure 2, we see that iterations of divergence are significantly higher around the contours of this zone, and drop to approach zero moving away from it.

\section{Question 2}
\label{sec:Question 2}

In this question, we are asked to study the SIR model of disease spread. In particular, we seek to integrate and plot the ODE's $dS/dt$,    $dI/dt$   and   $dR/dt$ given in the assignment, and repeat the process with three different sets of the constants $\beta$ and $\gamma$

\subsection{Q2 methods}
\label{sec:Q2 methods}

To integrate each of the ODE's, I defined the model as a function and used odeint from scipy to perform the integration. I then played around with different values of $\beta$ and $\gamma$ before selecting a few which generated interesting plots.

\subsection{Q2 results}
\label{sec:Q2 results}
I first tried setting both $\beta$ and $\gamma$ to 1, to see the raw behaviour of the integrated function (Figure 3).
I then tried $\beta = .1$ and $\gamma = -.1$ (Figure 4) and $\beta = 3$ and $\gamma = 2$ (Figure 5), all of which produced distinct behaviours between the susceptible, infected and recovered proportions over time. Since a discussion section is not required, I will just let you look at them as I'm very tired and would like to go to bed.

\begin{figure}
\includegraphics[width=1\columnwidth]{q11.png}
\caption{Zone where sequence stays bound is in black, zone of divergence in yellow}
\label{fig:figureOfSpectrum}

\includegraphics[width=1\columnwidth]{q12.png}
\caption{Zone where sequence stays bound is in purple, divergence zone coloured by iteration of divergence}
\label{fig:figureOfSpectrum}
\end{figure}

\begin{figure}
\includegraphics[width=1\columnwidth]{q21.png}
\caption{First set of constants: $\beta=1, \gamma=1$}
\label{fig:figureOfSpectrum}

\includegraphics[width=1\columnwidth]{q22.png}
\caption{First set of constants: $\beta=.1, \gamma=-.1$}
\label{fig:figureOfSpectrum}

\includegraphics[width=1\columnwidth]{q23.png}
\caption{First set of constants: $\beta=3, \gamma=2$}
\label{fig:figureOfSpectrum}
\end{figure}

%\acknowledgments

%% %% \bibliographystyle{act}
%% \bibliographystyle{apj}

%% \bibliography{lenscib_refs.bib,apj-jour}

\end{document}


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

\section*{Wed Jan 08 2020}

\section{Gravitational waves and interferometry}

Guest lecture by Angelo Ricciardone.

An outline:
\begin{enumerate}
  \item introduction about gravitational waves;
  \item frequency bands \(\iff\) sources of GW \(\iff\) detectors;
  \item GWs and observables;
  \item stochastic background of GW: characterization, sources, detection (here we discuss \emph{cosmological} sources).
\end{enumerate}

References:

\begin{enumerate}
  \item Book: ``Gravitational Waves - theory and experiments'' by Michele Maggiore (Oxford University Press, 2007);
  \item Book: ``Gravitational Waves - Astrophysical sources'' (2018);
  \item Paper: ``The basics of gravitational wave theory'', F.\ Flannagan (\url{https://arxiv.org/abs/gr-qc/0501041});
  \item Review: ``Gravitational waves from inflation'', (\url{https://arxiv.org/abs/1605.01615});
  \item Review: ``Cosmological background of gravitational waves'', (\url{https://arxiv.org/abs/1801.04268});
  \item Kind of unrelated review: ``Inflation and the Theory of Cosmological Perturbations'' \url{https://arxiv.org/abs/hep-ph/0210162}.
\end{enumerate}

We start with some general facts.

Gravitational waves appear naturally in GR: they are
propagating oscillations of the gravitational field.

The gravitational interaction is \emph{weak}: this implies that GWs travel freely, but they are also hard to detect.

The frequencies we see for the EM spectrum are in the range \(f_{EM} \sim \SI{e4}{Hz} \divisionsymbol \SI{e20}{Hz}\), radio waves to \(\gamma \) rays.

The typical frequencies of GWs are, instead, in  the range \(f_{GW} \sim \SI{e-16}{Hz} \divisionsymbol \SI{e4}{Hz}\), the lower end of this range is the frequency of the CMB GWs while the higher end is given off by astrophysical sources.
Ground-based detectors can detect the higher end of this spectrum.

\todo[inline]{But the CMB is electromagnetic! Is there gravitational radiation corresponding to it?}

The wavelength and frequency are typically comparable to the size of the object which is emitting the GWs.

Let us consider astrophysical sources:
%
\begin{align}
T = 2 \pi \sqrt{\frac{R^3}{GM}} \implies f = \sqrt{\frac{G \rho }{4 \pi^2}}
\,,
\end{align}
%
and since the mass is related to the density by \(M = \rho V= \frac{4}{3} \pi R^3 \rho \), then we have
%
\begin{align}
f_{GW} \approx \frac{1}{2 \pi } \sqrt{\frac{3GM}{4 \pi R^3}}
\,.
\end{align}
%
We know that the radius of the object must be greated than the Schwarzschild radius \(R_s = 2 GM / c^2\): substituting this in we get
%
\begin{align}
f_{GW} \approx \frac{1}{4 \pi } \frac{c^3}{GM}
\,,
\end{align}
%
which means, substituting the numbers, that
%
\begin{align}
f_{GW} \approx \SI{e4}{Hz} \frac{M_{\odot}}{M}
\,,
\end{align}
%

\todo[inline]{Is the difference between the formulas coming from different geometries of the problem? Like, a rotating objects versus two inspiralling ones?}

We make some estimates for neutron stars. Typically they have \(M \sim 1.4 M_{\odot}\) and \(R \sim \SI{e4}{m}\). So, we have \(f \sim \SI{e4}{Hz}\).

For small BHs, we have \(M \sim 30 M_{\odot}\), so \(f_{GW} \sim \SI{300}{Hz}\). This is the band in which LIGO/VIRGO works: these interferometers cannot measure GWs with frequency smaller than \SI{1}{Hz}.

If we increase the mass, if we use \(M \sim \num{e7} M_{\odot}\), we get \(f_{GW} \sim \SI{e-3}{Hz}\): this is the band in which LISA will work.

There are indirect evidences of GW emission.

Pulsars slow down: this is the Hulse-Taylor binary pulsar, two neutron stars which are rotating around each other emit gravitational waves. The GW emission back-reacts on the dynamics of the binary, on a timescale which is observable.

A reference for this is \url{https://arxiv.org/abs/astro-ph/0407149}.

We can make a plot: radius of the system vs mass of the system. Since  \(f \propto M^{1/2} R^{-3/2}\), constant frequency means a powerlaw in this plot, so a straight line in \(\log R \) vs \( \log M\).

The chirp of the inspiral is \emph{also} a powerlaw.
The GR predictions for the period decrease of the Hulse-Taylor binary NS match observations very precisely.

On the 14/09/2015, we had the first direct evidence of GWs with the first detection.

On 17/08/2017 we had the first detection of a NS/NS merger: this was the birth of multimessenger astronomy, since we saw the event with optical telescopes also. This meant that the signal got to us with a speed which is the same as the electromagnetic speed of light.

Now, there is an app: the ``GW events app'' \url{https://apps.apple.com/us/app/gravitational-wave-events/id1441897107}.

In GR, gravitational waves can be polarized in two different ways. These are called ``plus'' and ``cross'' polarizations.

\todo[inline]{Can one of these be rotated into the other? There might be some issue since one is a pseudotensor\dots}

We distinguish: the High Frequency band goes from \SI{e4}{Hz} to \SI{1}{Hz}; the Low Frequency band goes from \SI{1}{Hz} to \SI{e-4}{Hz}; the Very Low Frequency band goes from \SI{e-7}{Hz} to \SI{e-9}{Hz}; the Extremely Low frequency badn goes from \SI{e-15}{Hz} to \SI{e-18}{Hz}.

There are gaps since in certain frequency regions the methods we have on either side fail for different reasons leaving a gap.

Let us start from HF: it is the domain of Earth-based interferometers: LIGO (Livingstone \& Hanford), which has \SI{4}{km} arms, the two detectors are separated by \SI{3000}{km}. Also, there is VIRGO near Pisa: an arm is \SI{3}{km} long.

There is also Geo600 in Hannover, Germany: it has \SI{600}{m} arms. In Japan there is Kagra: it has \SI{4}{km} arms.

There are several reasons to have more than one detector: we can identify the position of the source, we can verify signals.

There will be the new Einstein Telescope, in a triangular configuration, in Italy or the Netherlands.
There will be the Cosmic Explorer, with \SI{40}{km} arms.

Now, let us discuss the main sources in the HF band.
We have:
\begin{enumerate}
  \item coalescence of stellar-mass BH binaries and NSs, for these we have an upper bound of \(M \lesssim \num{e3}M_{\odot} \);
  \item rotation of neutron stars (pulsar);
  \item stellar collapse: supernova to BH or NS.
\end{enumerate}

In the LF band, the domain of space-based interferometers, we will have LISA, in a triangular shape with \SI{2.5e6}{km}, and the Japanese DECIGO, with \(L \sim \SI{1000}{km}\).

In the LF band, the sources are:
\begin{enumerate}
  \item white dwarves merging;
  \item NS merging;
  \item inspiral and coalescence of SMBH (masses from 100 to \num{e8} \(M_{\odot}\)).
\end{enumerate}

In the VLF band, we can use Pulsar-Time Array. The sources in this frequency range are:
\begin{enumerate}
  \item GWs from SMBH with \(M > \num{11} M_{\odot}\), but it seems like there are no black holes this large;
  \item GWs from cosmic strings \& from phase transitions;
\end{enumerate}

In the ELF band, we have cosmological sources:
\begin{enumerate}
  \item primordial GWs: here we have \(h \sim (E _{\text{infl}} / M_P)^2\).
\end{enumerate}

Typically we have amplitudes increasing as the frequency decreases.

For scalar perturbations we have
%
\begin{align}
\frac{\Delta T}{T} \propto \delta \phi
\,.
\end{align}

Vector perturbations decay with the expansion of the universe. We do have tensor perturbations.  B mode polarizations correspond to primordial gravitational waves.

\todo[inline]{What are B modes?}

In a simple MM interferometry, \(\Delta L \propto h\).

LISA will orbit the Sun at \SI{20}{\degree} from the Earth.
The astrophysical targets for LISA are MBHBs, EMRIs and compact WDs.

Also, there are potential cosmological sources. We have first order phase transitions around the \SI{}{TeV}, inflationary GWs, cosmic strings and using MBHBs as standard sirens.

We put powerlaw amplitude spectra in a graph. What is \(\Omega \)?

We saw powerlaw spectra with \(\Omega \) increasing with \(f\): do they not have a UV problem since the energy density increased with the frequency?
No, since because of the transfer function between the early universe and now at a certain point the powerlaw becomes decreasing.

Cosmological sources are stochastic: they give a background.

If we will have a detector with high sensitivity, we will see many events: they will form a stochastic background.

\todo[inline]{Can we not correlate the signals in such a way that we only look at GWs from a specific angular region?}

A stochastic background of GW comes from a large number of independent uncorrelated sources that are not individually resolvable.

CSGWB is a candidate source for LISA.

For tomorrow, we can either give an overview of cosmological sources for GWs or we can derive the relation
%
\begin{align}
\frac{\Delta L}{L} \propto h
\,,
\end{align}

\end{document}


\documentclass[12pt, titlepage]{article}

% Author's up-front packages
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{longtable}

%Packages on template
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{xr-hyper}
\usepackage{hyperref}

% ---- Author's choice to remove them ----
%\usepackage[round]{natbib}

% Author's packages

\usepackage{amsmath, mathtools}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{cite}
\usepackage{indentfirst}
\usepackage{float}
\usepackage{csquotes}
\usepackage{cleveref}
\hypersetup{
    colorlinks,
    citecolor=black,
    filecolor=black,
    linkcolor=red,
    urlcolor=blue
}
\newcommand{\progname}{STEM Moir{\'e} GPA}
\externaldocument[ext:]{../SRS/SRS}

%Set the custom referencing system (author's initiative)
	% Goal Statement
\newtheorem{GS}{GS}
\crefname{GS}{GS}{GSs}
	% Assumption
\newtheorem{A}{A}
\crefname{A}{A}{As}
	% Theoretical Model
\newtheorem{T}{T}
\crefname{T}{T}{Ts}
	% Data Definition
\newtheorem{DD}{DD}
\crefname{DD}{DD}{DDs}
	% Data Constraints
\newtheorem{DC}{DC}
\crefname{DC}{DC}{DCs}
	% Instance Model
\newtheorem{IM}{IM}
\crefname{IM}{IM}{IMs}
	% General Definition
\newtheorem{GD}{GD}
\crefname{GD}{GD}{GDs}
	% Requirement
\newtheorem{R}{R}
\crefname{R}{R}{Rs}
	% Non Functional Requirement
\newtheorem{NR}{NR}
\crefname{NR}{NR}{NRs}
	% Likely change
\newtheorem{LC}{LC}
\crefname{LC}{LC}{LCs}
	% Test
\newtheorem{Test}{Test}
\crefname{Test}{Test}{Tests}

\input{../Comments}

\begin{document}

\title{Test Plan:\\
		STEM Moir{\'e} GPA}
\author{Alexandre Pofelski \\
		macid: pofelska \\
		github: slimpotatoes}
\date{\today}

\maketitle

\pagenumbering{roman}

\section{Revision History}

\begin{table}[h]
\caption{\bf Revision History}
\begin{tabularx}{\textwidth}{p{3cm}p{2cm}X}
\toprule {\bf Date} & {\bf Version} & {\bf Notes}\\
\midrule
23/10/2017 & 1.0 & First Draft\\
13/11/2017 & 1.1 & Multiple modifications + Test dataset to be implemented\\
26/1/2018 & 1.2 & Correction in \cref{Test-Basic-affine-transformation}\\
\bottomrule
\end{tabularx}
\end{table}

~\newpage

\section{Symbols, Abbreviations and Acronyms}
\label{symbols}

Symbols, Abbreviations and Acronyms used in the Test Plan document are regrouped under the following table, in \cref{ext:table_symbols_SRS} and in \cref{ext:table_acro_SRS} of the SRS document. \par\medskip

\renewcommand{\arraystretch}{1.2}
\begin{tabular}{l l}
  \toprule
  \textbf{symbol} & \textbf{description}\\
  \midrule
  GUI & Graphical Unit Interface \\
  N/A & Non-Applicable \\
  \bottomrule
  \label{table_acro_Test_Plan}
\end{tabular}

\newpage

\tableofcontents

\listoftables

\newpage

\pagenumbering{arabic}

\section{General Information}

\subsection{Purpose}

The purpose of the document is to provide the plan for testing
\href{https://github.com/slimpotatoes/STEM_Moire_GPA}{\progname{}} software with
respect to the requirements specified in the SRS document.

\subsection{Scope}

The scope of the test plan is described below:
\begin{itemize}
\item \progname{} will be written in Python.
\item The proposed test plan is focusing on system and unit testing to
  verify the functional and non functional requirements of
  \progname{}. Because of the nature of \cref{ext:IM_1},
  \cref{ext:IM_2} and \cref{ext:IM_3} (see SRS document), a complete
  verification of their respective outputs is not possible. In these
  cases, the system tests are designed as validation tests (including
  algorithm errors) and will not catch a code error from a modelling error. A
physical
  understanding of the test output is recommended to interpret
  properly its quantitative aspect, since an assessment by the test
  team on the quality of the test results is required to validate it. If the
error observed is unreasonably high, an
  error in the code tested might be postulated. At current stage of the
development, an \enquote{unreasonable error} is difficult to qualify, however a
relative error greater than 50{\%} of the expected results is not acceptable for
\progname{}. This limit is excessively high but will be adjusted once the error
of the GPA algorithm is analytically studied.
\item Regarding the numerous requirements of \progname{} and their
  relative complexity to be fully verified, only a portion of
  \progname{} is proposed to be tested. A specific focus is proposed
  on key aspects that are identified as the highest potential source
  of errors. The testing of the GUI won't be approached in this
  document but should be definitely considered in the future.

\end{itemize}

\section{Plan}

Based on the scope of the test plan, only \cref{ext:R_2}, \cref{ext:R_3}, \cref{ext:R_6}, \cref{ext:R_7}, \cref{ext:R_9}, \cref{ext:R_10}, \cref{ext:R_12}, \cref{ext:R_13}, \cref{ext:NR_1}, \cref{ext:NR_2}, \cref{ext:NR_8} and \cref{ext:NR_9} are planned to be verified and/or validated.

\subsection{Software Description}

\progname{} software is converting a STEM Moir{\'e} hologram into deformation maps. Details on the goal and the requirements of \progname{} are provided in the Problem Statement and the SRS documents.

\subsection{Test Team}

Alexandre Pofelski is the only member of the test team.

\subsection{Automated Testing Approach}

By predefining some user inputs, an automated testing approach can be considered. The unit tests and most of the system tests will be executed and validated automatically. Some system tests (\cref{T_Phase-Extraction-No-Strain}, \cref{T_Phase-Extraction-Known-Strain}, \cref{T_Phase-Extraction-Mask}, \cref{T_constant-delta-g-with-noise}, \cref{T_varying-delta-g}, \cref{black_box_STEM_Moire_GPA}) require at least a manual assessment of the test results and are thus removed from the automated scope.

\subsection{Verification Tools}

With respect to the limited experience of the test team regarding verification and validation, only a unit test framework is going to be used as verification tools. \textbf{Pytest}, a Python unit test framework, will be used to test \progname{} software.

\subsection{Non-Testing Based Verification}

N/A

\section{System Test Description}

Numerous system tests are using analytical functions as inputs and sometimes expect analytical functions as outputs. Those functions need to be implemented into proper format for \progname{} using specific values for the parameters. Dedicated test dataset (inputs and expected outputs) will be explicitly described and stored in the \href{https://github.com/slimpotatoes/STEM_Moire_GPA}{\progname{}} repository once they are designed.

\subsection{Tests for Functional Requirements}
\renewcommand{\labelitemi}{$\star$}

\subsubsection{Output Verfication (and/or Validation) tests}

\paragraph{Test \cref{ext:R_3} in \cref{ext:IM_1}: Correctness of the sampling theory application when undersampling g}

\begin{Test}\normalfont\underline{Test-aliasing-frequency-simple}
\label{aliasing-frequency-simple}
\begin{itemize}
\item Type: Functional, Dynamical, Automated
\item Initial State: N/A
\item Input: $I_{C_{\text{ref}}}=e^{2i\pi gx}$, $p$ such that $\overrightarrow{q}=\frac{1}{p}\vec{u_x}$
\item Expected output : $\widetilde{I}_{\mathit{SHM}_{\text{sim}}}=\delta(\vec{\nu}-\vec{q})$
\item Output: ${\widetilde{I}_{\mathit{SHM}_{\text{sim}}}}^{t}$ to be tested as a Dirac delta function at $\overrightarrow{q}$
\end{itemize}
\end{Test}

The position in frequency space of the aliased frequency (transformation of one $\overrightarrow{g_{j}}^{C_{\text{ref}}}$ from $I_{C_{\text{ref}}}$ to one $\overrightarrow{g_{j}}^{M_{\text{ref}}}$ of $I_{\mathit{SMH}_{\text{sim}}}$) is key to identify the vector $\overrightarrow{q_{n_j,m)j}}$. \Cref{aliasing-frequency-simple} is designed to check if the most basic transformation when under sampling is correctly performed. More complex versions of \Cref{aliasing-frequency-simple} can be designed with reference functions having, for example, a continuous description in Fourier space. Nevertheless, the more complex tests will cover way more cases than what $I_{C_{\text{ref}}}$ typically is (a sparse function). Depending on the time availability, a more complete version of \cref{aliasing-frequency-simple} will be designed.

\paragraph{Test \cref{ext:R_7} in \cref{ext:IM_2}: Correctness of the GPA method application}

\begin{Test}\normalfont\underline{Test-Phase-Extraction-No-Strain}
\label{T_Phase-Extraction-No-Strain}
\begin{itemize}
\item Type: Functional, Dynamical, Manual
\item Initial State: N/A
\item Input: $I_{SMH_{exp}}=e^{2i\pi gx}$, Mask $M$ of one pixel at $g\overrightarrow{u_x}$ in $\widetilde{I}_{SMH_{exp}}$
\item Expected output $P_{\Delta \overrightarrow{g_{j}}^{M_{exp}}}=0$, $\Delta \overrightarrow{g_{j}}^{M_{exp}}=\overrightarrow{0}$,
\item Test output:${P_{\Delta \overrightarrow{g_{j}}^{M_{exp}}}}^{t}$, $\Delta {\overrightarrow{g_{j}}^{M_{exp}}}^{t}$
	\begin{itemize}
	\item $\forall \vec{r} \in \mathbb{I}, \ E_{P_{\Delta \overrightarrow{g_{j}}^{M_{exp}}}}(\vec{r})=|{P_{\Delta \overrightarrow{g_{j}}^{M_{exp}}}}^{t}(\vec{r})|$
	\item $\forall \vec{r} \in \mathbb{I}, \ E_{\Delta {\overrightarrow{g_{j}}^{M_{exp}}}}(\vec{r})=|{\Delta {\overrightarrow{g_{j}}^{M_{exp}}}}^t(\vec{r})|$
	\end{itemize}
\end{itemize}
\end{Test}

\begin{Test}\normalfont\underline{Test-Phase-Extraction-Known-Strain}
\label{T_Phase-Extraction-Known-Strain}
\begin{itemize}
\item Type: Functional, Dynamical, Manual
\item Initial State: N/A
\item Input: $I_{SMH_{exp}}=e^{2i\pi (g+K(x))x}$, Mask $M$ centred on $g\overrightarrow{u_x}$ in $\widetilde{I}_{SMH_{exp}}$ and with the minimum radius to include $K(x)$.
\item Expected output $P_{\Delta \overrightarrow{g_{j}}^{M_{exp}}}=K(x)x$, $\Delta \overrightarrow{g_{j}}^{M_{exp}}=K(x)\overrightarrow{u_x}$,
\item Test output:${P_{\Delta \overrightarrow{g_{j}}^{M_{exp}}}}^{t}$, $\Delta {\overrightarrow{g_{j}}^{M_{exp}}}^{t}$
	\begin{itemize}
	\item $\forall \vec{r} \in \mathbb{I}, \ E_{P_{\Delta \overrightarrow{g_{j}}^{M_{exp}}}}(\vec{r})=|{P_{\Delta \overrightarrow{g_{j}}^{M_{exp}}}}^{t}(\vec{r})-P_{\Delta \overrightarrow{g_{j}}^{M_{exp}}}(\vec{r})|$
	\item $\forall \vec{r} \in \mathbb{I}, \ E_{\Delta {\overrightarrow{g_{j}}^{M_{exp}}}}(\vec{r})=|{\Delta {\overrightarrow{g_{j}}^{M_{exp}}}}^t(\vec{r})-{\Delta {\overrightarrow{g_{j}}^{M_{exp}}}}(\vec{r})|$
	\end{itemize}
\end{itemize}
\end{Test}

\begin{Test}\normalfont\underline{Test-Phase-Extraction-Mask}
\label{T_Phase-Extraction-Mask}
\begin{itemize}
\item Type: Functional, Dynamical, Manual
\item Initial State: N/A
\item Input: $I_{SMH_{exp}}=e^{2i\pi (g+H(x))x}$, Mask $M$ centred on $g\overrightarrow{u_x}$ in $\widetilde{I}_{SMH_{exp}}$ with different radius $\epsilon$.
\item Expected output $P_{\Delta \overrightarrow{g_{j}}^{M_{exp}}}=H(x)x$, $\Delta \overrightarrow{g_{j}}^{M_{exp}}=H(x)\overrightarrow{u_x}$,
\item Test output:${P_{\Delta \overrightarrow{g_{j}}^{M_{exp}}}}^{t}$, $\Delta {\overrightarrow{g_{j}}^{M_{exp}}}^{t}$
	\begin{itemize}
	\item $\forall \vec{r} \in \mathbb{I}, \ E_{P_{\Delta \overrightarrow{g_{j}}^{M_{exp}}}}(\vec{r},\epsilon)=|{P_{\Delta \overrightarrow{g_{j}}^{M_{exp}}}}^{t}(\vec{r},\epsilon)-P_{\Delta \overrightarrow{g_{j}}^{M_{exp}}}(\vec{r})|$
	\item $\forall \vec{r} \in \mathbb{I}, \ E_{\Delta {\overrightarrow{g_{j}}^{M_{exp}}}}(\vec{r},\epsilon)=|{\Delta {\overrightarrow{g_{j}}^{M_{exp}}}}^t(\vec{r},\epsilon)-{\Delta {\overrightarrow{g_{j}}^{M_{exp}}}}(\vec{r})|$
	\end{itemize}
\end{itemize}
\end{Test}

Because the GPA method is itself based on an approximation (see
\cref{ext:IM_2} in SRS document), errors from the algorithm are added
to the errors from the code. Both are probed at the same time in the
tests and cannot be fully
separated. \Cref{T_Phase-Extraction-No-Strain} is probing the accuracy
and the precision in the most basic case possible (no strain) and is a
first indicator of the \cref{ext:IM_2}
performance. \Cref{T_Phase-Extraction-Known-Strain} and
\cref{T_Phase-Extraction-Mask} are closer to a real case scenario and
highlight a more realistic version of \cref{ext:IM_2} performance. To
get quantitative trends, the error will be characterized as a function
of the mask properties (such as the radius) and the deformation
magnitude. This characterization will allow the GPA algorithm of
\progname{} to be compared with other software using the same GPA
algorithm. Another interest of the performance characterization is to
see a quantitative effect of the user inputs on \cref{ext:IM_2} and
provide a ``good practice guidance'' in the user manual.

\paragraph{Test \cref{ext:R_10} in \cref{ext:IM_3}: Correctness of the unstrained reference calculation}

\begin{Test}\normalfont\underline{Test-constant-delta-g}
\label{T_constant-delta-g}
\begin{itemize}
\item Type: Functional, Dynamical, Automated
\item Initial State:
\item Input:
	\begin{itemize}
	\item $\forall \vec{r} \in \mathbb{I}, \  \Delta \overrightarrow{g}^{M_{\text{exp}}}(\vec{r})=\overrightarrow{C}$
	\item $U$ array of 1 pixel wherever on $\Delta \overrightarrow{g}^{M_{\text{exp}}}(\vec{r})$
	\item $\overrightarrow{g}^{M_{\text{exp}}}={g_x}^{M_{\text{exp}}}\overrightarrow{u_x}+{g_y}^{M_{\text{exp}}}\overrightarrow{u_y}$
	\end{itemize}
\item Expected output: $\overrightarrow{g}_{\text{uns}}^{M_{\text{exp}}}=\overrightarrow{g}^{M_{\text{exp}}}-\overrightarrow{C}$, $\Delta\overrightarrow{g_{j}}_{\text{cor}}^{M_{\text{exp}}}=\overrightarrow{0}$
\item Output:${\overrightarrow{g}_{\text{uns}}^{M_{\text{exp}}}}^{t}$ , ${\Delta\overrightarrow{g_{j}}_{\text{cor}}^{M_{\text{exp}}}}^{t}$
	\begin{itemize}
	\item $E_{\overrightarrow{g}_{\text{uns}}^{M_{\text{exp}}}}=||{\overrightarrow{g}_{\text{uns}}^{M_{\text{exp}}}}^{t}-\overrightarrow{g}_{\text{uns}}^{M_{\text{exp}}}||$
	\item $\forall \vec{r} \in \mathbb{I}, \ E_{\Delta\overrightarrow{g_{j}}_{\text{cor}}^{M_{\text{exp}}}}=||{\Delta\overrightarrow{g_{j}}_{\text{cor}}^{M_{\text{exp}}}}^{t}||$
	\end{itemize}
\end{itemize}
\end{Test}

\Cref{T_constant-delta-g} is not realistic but test the code
directly. Increasing the size of $U$ should have absolutely no effect on the output.

\begin{Test}\normalfont\underline{Test-constant-delta-g-with-noise}
\label{T_constant-delta-g-with-noise}
\begin{itemize}
\item Type: Functional, Dynamical, Manual (can be automated)
\item Initial State:
\item Input:
	\begin{itemize}
	\item $\overrightarrow{N}$, 2D random noise
	\item $\forall \vec{r} \in \mathbb{I}, \ \Delta \overrightarrow{g}^{M_{\text{exp}}}(\vec{r})=\overrightarrow{C} + \overrightarrow{N}$
	\item $U$ array of $n \times m$ pixels wherever on $\Delta \overrightarrow{g}^{M_{\text{exp}}}(\vec{r})$
	\item $\overrightarrow{g}^{M_{\text{exp}}}={g_x}^{M_{\text{exp}}}\overrightarrow{u_x}+{g_y}^{M_{\text{exp}}}\overrightarrow{u_y}$
	\end{itemize}
\item Expected output: $\overrightarrow{g}_{\text{uns}}^{M_{\text{exp}}}=\overrightarrow{g}^{M_{\text{exp}}}-\overrightarrow{C}$,  $\Delta\overrightarrow{g_{j}}_{\text{cor}}^{M_{\text{exp}}}=\overrightarrow{0}$
\item Output:${\overrightarrow{g}_{\text{uns}}^{M_{\text{exp}}}}^{t}, {\Delta\overrightarrow{g_{j}}_{\text{cor}}^{M_{\text{exp}}}}^{t}$
	\begin{itemize}
	\item $E_{\overrightarrow{g}_{\text{uns}}^{M_{\text{exp}}}}=||{\overrightarrow{g}_{\text{uns}}^{M_{\text{exp}}}}^{t}-\overrightarrow{g}_{\text{uns}}^{M_{\text{exp}}}||$
	\item $\forall \vec{r} \in \mathbb{I}, \ E_{\Delta\overrightarrow{g_{j}}_{\text{cor}}^{M_{\text{exp}}}}=||{\Delta\overrightarrow{g_{j}}_{\text{cor}}^{M_{\text{exp}}}}^{t}||$
	\end{itemize}
\end{itemize}
\end{Test}

\Cref{T_constant-delta-g-with-noise} is more representative of a real
case than \cref{T_constant-delta-g} and will be use to characterize
the evolution of the error with respect to the level of noise and the
size of U. If the error is unreasonably high, an error in the code can
be postulated. The evaluation of
$\overrightarrow{g}_{\text{uns}}^{M_{\text{exp}}}$ is critical on the
quantitative estimation of strain and rotation.  \wss{I like this
  test, but (as for several other tests), there are still decisions
  that could be made that you have postponed.  Specifically, what are
  the values of $m$ and $n$?  If the data is difficult to specify,
  maybe you could point to a sample file in your repo that you are
  going to use?} \an{I fully agree. Will do it later once the tests dataset are
designed}

\begin{Test}\normalfont\underline{Test-varying-delta-g}
\label{T_varying-delta-g}
\begin{itemize}
\item Type: Functional, Dynamical, Manual
\item Initial State:
\item Input:
	\begin{itemize}
	\item $\forall \vec{r} \in \mathbb{I}, \ \overrightarrow{g}^{M_{\text{exp}}}(\vec{r})=\overrightarrow{C}(\vec{r})$
	\item $U$ array of $n \times m$ pixels wherever on $\Delta \overrightarrow{g}^{M_{\text{exp}}}(\vec{r})$
	\item $\overrightarrow{g}^{M_{\text{exp}}}={g_x}^{M_{\text{exp}}}\overrightarrow{u_x}+{g_y}^{M_{\text{exp}}}\overrightarrow{u_y}$
\end{itemize}
\item Expected output: $\overrightarrow{g}_{\text{uns}}^{M_{\text{exp}}}=\overrightarrow{g}^{M_{\text{exp}}}-F(\overrightarrow{C}(\vec{r}))$,   $\Delta\overrightarrow{g_{j}}_{\text{cor}}^{M_{\text{exp}}}=\overrightarrow{C}(\vec{r})-F(\overrightarrow{C}(\vec{r}))$ where $F(\overrightarrow{C}(\vec{r}))$ is the best possible linear fit of $\overrightarrow{C}(\vec{r})$ in $U$.
\item Output:${\overrightarrow{g}_{\text{uns}}^{M_{\text{exp}}}}^{t}, {\Delta\overrightarrow{g_{j}}_{\text{cor}}^{M_{\text{exp}}}}^{t}$
	\begin{itemize}
	\item $E_{\overrightarrow{g}_{\text{uns}}^{M_{\text{exp}}}}=||{\overrightarrow{g}_{\text{uns}}^{M_{\text{exp}}}}^{t}-\overrightarrow{g}_{\text{uns}}^{M_{\text{exp}}}||$
	\item $\forall \vec{r} \in \mathbb{I}, \ E_{\Delta\overrightarrow{g_{j}}_{\text{cor}}^{M_{\text{exp}}}}=||{\Delta\overrightarrow{g_{j}}_{\text{cor}}^{M_{\text{exp}}}}^{t}-\Delta\overrightarrow{g_{j}}_{\text{cor}}^{M_{\text{exp}}}||$
	\end{itemize}
\end{itemize}
\end{Test}

\Cref{T_varying-delta-g} might be impossible to properly interpret but the idea
is to see the effect an improper choice of an unstrained reference. If a non
uniform deformation field is present in the reference, \progname{} is trying to
find the best linear fit. A solution is calculated whatever the precision of the
fit. However, if the quality of the fit is poor, it could be possible that
\progname{} informs/warns the user of a potential problem in the reference
chosen.

\subsection{Tests for Nonfunctional Requirements}

\subsubsection{Evolution of precision with noise in \progname{}}

\paragraph{Test \cref{ext:NR_8} and \cref{ext:NR_9}}

\begin{Test}\normalfont\underline{Test-Black-Box-STEM-Moir{\'e}-GPA}
\label{black_box_STEM_Moire_GPA}
\begin{itemize}
\item Type: Functional, Dynamical, Black box, Manual (can be automated)
\item Initial State: All user inputs are predefined ($p,M_1,M_2,U,\overrightarrow{q_{n_1,m_1}},\overrightarrow{q_{n_2,m_2}}$) except the STEM Moir{\'e} hologram
\item Input: STEM Moir{\'e} holograms with different level of noise (from no noise to high level of noise):
	\begin{itemize}
	\item	$I_{\mathit{SMH}_\text{exp}} = I(\vec{r}) + N(\vec{r})$ with $I$ a perfect SMH (with a defined strain field $\varepsilon(\vec{r})$ and rotation field $\omega(\vec{r})$) and $N$ a random noise
	\end{itemize}
\item Expected output $\forall \vec{r} \in \mathbb{I}, \ \varepsilon(\vec{r})^{t}=\varepsilon(\vec{r}) \wedge \omega\vec({r})^{t}=\omega(\vec{r})$
\item Output:
	\begin{itemize}
	\item $\forall \vec{r} \in \mathbb{I}, \ E^{\text{\progname{}}}_{\varepsilon}=|\varepsilon(\vec{r})^{t}-\varepsilon(\vec{r})|$
	\item $\forall \vec{r} \in \mathbb{I}, \ E^{\text{\progname{}}}_{\omega}=|\omega(\vec{r})^{t}-\omega(\vec{r})|$
	\end{itemize}
\end{itemize}
\end{Test}

\Cref{black_box_STEM_Moire_GPA} looks more like a validation test than a
verification test. Nevertheless, if the error in \cref{black_box_STEM_Moire_GPA}
is unreasonably high, it is possible to consider the presence of a mistake in
the code somewhere. On the validation side, \cref{black_box_STEM_Moire_GPA} is
highlighting the accuracy (without noise), and the precision (with noise) of
\progname{} seen by the end-user (code, user and algorithm errors included). The
performance is then characterized as the error output function of the noise (or
of the quality factor of $I_{\mathit{SMH}_{\text{exp}}}$ like the signal on
noise ratio $\mathit{SNR}=\frac{I_{\mathit{SMH}_\text{exp}}}{N}$). The
performance characterization can be used to see from which quality factor the
accuracy and sensitivity of \progname{} is too poor to consider the strain and
rotation results reliable and warn the user in this case. The performance graph
can also be as a base of comparison with another software (if it exists) doing
the same data treatment.

\subsubsection{Evolution of the time calculations with data size}

\paragraph{Test \cref{ext:NR_2}}

\begin{Test}\normalfont\underline{Test-stress-STEM-Moir{\'e}-GPA}
\label{stress_test_STEM_Moire_GPA}
\begin{itemize}
\item Type: Functional, Dynamical, Black box, Manual
\item Initial State: All user inputs are predefined ($p,M_1,M_2,U,\overrightarrow{q_{n_1,m_1}},\overrightarrow{q_{n_2,m_2}}$) except the STEM Moir{\'e} hologram
\item Input: STEM Moir{\'e} holograms with different 2D array size.
\item Output: Time to perform \progname{} calculations and plot execution time vs data size.
\end{itemize}
\end{Test}

\subsubsection{Evolution of the accuracy of \progname{} with environment (reproducibility)}

\paragraph{Test \cref{ext:NR_1}}

\begin{Test}\normalfont\underline{Test-Black-Box-STEM-Moir{\'e}-GPA-env}
\label{black_box_STEM_Moire_GPA-env}
\begin{itemize}
\item Type: Functional, Dynamical, Black box, Manual
\item Initial State: Various environments (x64 Linux, x64 Windows, x64 Mac OS X) with all user inputs are predefined ($p,M_1,M_2,U,\overrightarrow{q_{n_1,m_1}},\overrightarrow{q_{n_2,m_2}}$) except $I_{\mathit{SMH}_\text{exp}}$
\item Input: A perfect generated STEM Moir{\'e} hologram representing all the strain state in each quadrant $\mathbb{I}_{i}$ of the array $\mathbb{I}$ (unstrained in $\mathbb{I}_{1}$, 1{\%} uniaxial strain along $\vec{u_x}$ and $\vec{u_y}$ in $\mathbb{I}_{2}$, 1{\%} shear strain in $\mathbb{I}_{3}$, and  20 mrad rotation $\mathbb{I}_{4}$)
\item Expected output: In each quadrant
\begin{itemize}
\item $\forall \vec{r} \in \mathbb{I}_{1}, \ \varepsilon(\vec{r})=\begin{bmatrix}
0 & 0 \\
0 & 0
\end{bmatrix} \ \wedge \ \omega\vec({r})=\begin{bmatrix}
0 & 0 \\
0 & 0
\end{bmatrix}$
\item $\forall \vec{r} \in \mathbb{I}_{2}, \ \varepsilon(\vec{r})=\begin{bmatrix}
0.01 & 0 \\
0 & 0.01
\end{bmatrix} \ \wedge \ \omega\vec({r})=\begin{bmatrix}
0 & 0 \\
0 & 0
\end{bmatrix}$
\item $\forall \vec{r} \in \mathbb{I}_{3}, \ \varepsilon(\vec{r})=\begin{bmatrix}
0 & 0.01 \\
0.01 & 0
\end{bmatrix} \ \wedge \ \omega\vec({r})=\begin{bmatrix}
0 & 0 \\
0 & 0
\end{bmatrix}$
\item $\forall \vec{r} \in \mathbb{I}_{4}, \ \varepsilon(\vec{r})=\begin{bmatrix}
0 & 0 \\
0 & 0
\end{bmatrix} \ \wedge \ \omega\vec({r})=\begin{bmatrix}
0 & 0.2 \\
-0.2 & 0
\end{bmatrix}$
\end{itemize}
\item Output: $\varepsilon(\vec{r})^{t}, \omega(\vec{r})^{t}$
	\begin{itemize}
	\item $\forall \vec{r} \in \mathbb{I}, \ E^{\text{\progname{}}}_{\varepsilon\text{ env}}=|\varepsilon(\vec{r})^{t}-\varepsilon(\vec{r})|$
	\item $\forall \vec{r} \in \mathbb{I}, \ E^{\text{\progname{}}}_{\omega\text{ env}}=|\omega(\vec{r})^{t}-\omega(\vec{r})|$
	\end{itemize}
\end{itemize}
\end{Test}

The output of \cref{black_box_STEM_Moire_GPA-env} is not expected to be a
perfect match but should be matching errors obtained in
\cref{black_box_STEM_Moire_GPA} (or relatively close to). The errors will be
then displayed as a function of the environment (Linux, Windows and Mac). The
relative errors between the different environments can be also considered.

\section{Unit Testing Plan}

\subsection{Input Verification test}

\paragraph{Test \cref{ext:R_2} in \cref{ext:IM_1}: Existence and Format of $I_{\mathit{SMH}_{\text{exp}}}$, $I_{C_{\text{ref}}}$ and $p$}

\begin{Test}\normalfont\underline{Test-Existence-SMH}
\label{Test-Existence-SMH}
\begin{itemize}
\item Type: Dynamical, Automated
\item Initial State: Waiting for $I_{\mathit{SMH}_{\text{exp}}}$ user input
\item Input: $I_{\mathit{SMH}_{\text{exp}}}$ = $\emptyset$
\item Output: Error message $Err_{I_{\mathit{SMH}_{\text{exp}}}}$ should match: \enquote{No STEM Moir{\'e} hologram, please load a proper image}
\end{itemize}
\end{Test}

\begin{Test}\normalfont\underline{Test-Format-SMH}
\label{Test-Format-SMH}
\begin{itemize}
\item Type: Dynamical, Automated
\item Initial State: Waiting for $I_{\mathit{SMH}_{\text{exp}}}$ user input
\item Input: Various $I_{\mathit{SMH}_{\text{exp}}}$ improper format
\item Output: Error message $Err_{I_{\mathit{SMH}_{\text{exp}}}}$ should match: \enquote{Invalid STEM Moir{\'e} hologram format (expecting a 2D array)}
\end{itemize}
\end{Test}

\begin{Test}\normalfont\underline{Test-read-dm3-format}
\label{Test-read-dm3-format}
\begin{itemize}
\item Type: Dynamical, Automated
\item Initial State: Waiting for $I_{\mathit{SMH}_{\text{exp}}}$ or $I_{C_{\text{ref}}}$ user input
\item Input: dm3 file with pre defined tags (calibration, microscope, high tension, ...)
\item Output: Read the tag from the dm3 file and and test if each tag collected matches with the pre defined tags
\end{itemize}
\end{Test}

\begin{Test}\normalfont\underline{Test-Existence-pixel}
\label{Test-Existence-pixel}
\begin{itemize}
\item Type: Dynamical, Automated
\item Initial State: After importing $I_{\mathit{SMH}_{\text{exp}}}$ and format validated
\item Input: $p$=$\emptyset$
\item Output: Error message $Err_{p}$ should match: \enquote{No pixel size found}
\end{itemize}
\end{Test}

\begin{Test}\normalfont\underline{Test-Format-pixel}
\label{Test-Format-pixel}
\begin{itemize}
\item Type: Dynamical, Automated
\item Initial State: After importing $I_{\mathit{SMH}_{\text{exp}}}$ and format validated
\item Input: Improper format of $p$
\item Output: Error message $Err_{p}$ should match: \enquote{Invalid pixel size}
\end{itemize}
\end{Test}

\begin{Test}\normalfont\underline{Test-Format-Reference}
\label{Test-Format-Reference}
\begin{itemize}
\item Type: Dynamical, Automated
\item Initial State: Waiting for $I_{C_{\text{ref}}}$ user input
\item Input: Various $I_{C_{\text{ref}}}$ improper format
\item Output: Error message $Err_{I_{C_{\text{ref}}}}$ should match: \enquote{Invalid Reference image format (expecting a 2D array)}
\end{itemize}
\end{Test}

\begin{Test}\normalfont\underline{Test-Existence-Reference}
\label{Test-Existence-Reference}
\begin{itemize}
\item Type: Dynamical, Automated
\item Initial State: Waiting for $I_{C_{\text{ref}}}$ user input
\item Input: $I_{C_{\text{ref}}}$ = $\emptyset$
\item Output: Error message $Err_{I_{C_{\text{ref}}}}$ should match: \enquote{No Reference image, please load a proper image}
\end{itemize}
\end{Test}

\paragraph{Test \cref{ext:R_6} in \cref{ext:IM_2}: Test of the mask user input}

\begin{Test}\normalfont\underline{Test-Existence-Mask}
\label{Test-Existence-Mask}
\begin{itemize}
\item Type: Dynamical, Automated
\item Initial State: Waiting for $M$ user input on $\widetilde{I}_{\mathit{SMH}_{\text{exp}}}$
\item Input: $M$=$\emptyset$
\item Output:  Error message $Err_{M}$ should match: \enquote{No Mask found}
\end{itemize}
\end{Test}

\begin{Test}\normalfont\underline{Test-Format-Mask}
\label{Test-Format-Mask}
\begin{itemize}
\item Type: Dynamical, Automated
\item Initial State: Waiting for $M$ user input on $\widetilde{I}_{\mathit{SMH}_{\text{exp}}}$
\item Input: $M$ improper format (such as 1D array, 2D array out of bounds)
\item Output:  Error message $Err_{M}$ should match: \enquote{Improper mask format}
\end{itemize}
\end{Test}

\begin{Test}\normalfont\underline{Test-Position-Radius-Mask}
\label{Test-Position-Radius-Mask}
\begin{itemize}
\item Type: Dynamical, Automated
\item Initial State: Waiting for $M$ user input on $\widetilde{I}_{\mathit{SMH}_{\text{exp}}}$
\item Input: Mask center =$(0,0)$, Mask radius=3
\item Output: Center of the circle mask positioned in the middle of the 2D array $\widetilde{I}_{\mathit{SMH}_{\text{exp}}}$ and with a radius of 3 pixels
\end{itemize}
\end{Test}

\paragraph{Test \cref{ext:R_9} in \cref{ext:IM_3}: Test of the unstrained region user input}

\begin{Test}\normalfont\underline{Test-Existence-U}
\label{Test-Existence-U}
\begin{itemize}
\item Type: Dynamical, Automated
\item Initial State: Waiting for $U$ user input on $P_{\Delta \overrightarrow{g_{j}}^{M_{exp}}}$
\item Input: $U$=$\emptyset$
\item Output:  Error message $Err_{U}$ should match: \enquote{No reference in phase image found}
\end{itemize}
\end{Test}

\begin{Test}\normalfont\underline{Test-Format-U}
\label{Test-Format-U}
\begin{itemize}
\item Type: Dynamical, Automated
\item Initial State: Waiting for $U$ user input on $P_{\Delta \overrightarrow{g_{j}}^{M_{exp}}}$
\item Input: $U$ improper format
\item Output:  Error message $Err_{U}$ should match: \enquote{Improper reference in phase image format}
\end{itemize}
\end{Test}

\begin{Test}\normalfont\underline{Test-Position-U}
\label{Test-Position-U}
\begin{itemize}
\item Type: Dynamical, Automated
\item Initial State: Waiting for $U$ user input on $P_{\Delta \overrightarrow{g_{j}}^{M_{exp}}}$
\item Input:
	\begin{itemize}
	\item Position left top corner=$(10,10)$,
	\item Position right bottom corner=$(30,40)$.
	\end{itemize}
\item Output: Rectangle center position $(20,25)$, short length $l=20$ pixels and long length $L=30$ pixels
\end{itemize}
\end{Test}

\subsection{Output Verification test}

\paragraph{Test \cref{ext:R_12} in \cref{ext:IM_4}: Affine vectorial transformation}

\begin{Test}\normalfont\underline{Test-Basic-affine-transformation}
\label{Test-Basic-affine-transformation}
\begin{itemize}
\item Type: Functional, Dynamical, Automated
\item Initial State: N/A
\item Input: $\overrightarrow{q_{n,m}}=\begin{bmatrix}
1 \\
-2
\end{bmatrix}$, $\overrightarrow{g_{j}}_{\text{uns}}^{M_{\text{exp}}}=\Delta\overrightarrow{g_{j}}_{\text{cor}}^{M_{\text{exp}}}=\begin{bmatrix}
1 \\
1
\end{bmatrix}$, $p=\frac{1}{3}$
\item Output:  $\overrightarrow{g_{j}}_{\text{uns}}^{C_{\text{exp}}}=\begin{bmatrix}
4\\
-5
\end{bmatrix}$, $\Delta\overrightarrow{g_{j}}^{C_{\text{exp}}}=\begin{bmatrix}
1\\
1
\end{bmatrix}$
\end{itemize}
\end{Test}

\begin{Test}\normalfont\underline{Test-Error-pixel-affine-transformation}
\label{Test-Error-pixel-affine-transformation}
\begin{itemize}
\item Type: Functional, Dynamical, Automated
\item Initial State: Whatever $\overrightarrow{q_{n,m}}, \overrightarrow{g_{j}}_{\text{uns}}^{M_{\text{exp}}}, \Delta\overrightarrow{g_{j}}_{\text{cor}}^{M_{\text{exp}}}$
\item Input:$p=0$ or $p=-10$
\item Output: Error message $Err_{p_{\text{affine}}}$ should match: \enquote{Invalid pixel size}
\end{itemize}
\end{Test}

\begin{Test}\normalfont\underline{Test-Error-q-affine-transformation}
\label{Test-Error-q-affine-transformation}
\begin{itemize}
\item Type: Functional, Dynamical, Automated
\item Initial State: Whatever $\overrightarrow{q_{n,m}}, \overrightarrow{g_{j}}_{\text{uns}}^{M_{\text{exp}}}, \Delta\overrightarrow{g_{j}}_{\text{cor}}^{M_{\text{exp}}},$ and $p>0$
\item Input:$\overrightarrow{q_{n,m}}=\begin{bmatrix}
1.3 \\
-1
\end{bmatrix}$ or $\overrightarrow{q_{n,m}}=\begin{bmatrix}
1 \\
-\sqrt{2}
\end{bmatrix}$
\item Output: Error message $Err_{\vec{q}_{\text{affine}}}$ should match: \enquote{Invalid sampling vector $\overrightarrow{q_{n,m}}$}
\end{itemize}
\end{Test}

\paragraph{Test\cref{ext:R_13} in \cref{ext:IM_5}: Strain and rotation calculation}

\begin{Test}\normalfont\underline{Test-No-2D-strain}
\label{T_No-2D-strain}
\begin{itemize}
\item Type: Functional, Dynamical, Automated
\item Initial State: N/A
\item Input:
\begin{itemize}
	\item $G_{\text{uns}}^{\text{exp}} =
	\begin{bmatrix}
	g_{1_{{\text{uns}}_x}}^{C_{\text{exp}}} & g_{2_{{\text{uns}}_x}}^{C_{\text{exp}}} \\
	g_{2_{{\text{uns}}_x}}^{C_{\text{exp}}} & g_{2_{{\text{uns}}_y}}^{C_{\text{exp}}}
	\end{bmatrix} =\begin{bmatrix}
	1 & 2 \\
	3 & 4
	\end{bmatrix} $
	\item $\Delta G^{\text{exp}}(\vec{r})=
	\begin{bmatrix}
	\Delta g_{1_{x}}^{C_{\text{exp}}}(\vec{r}) & \Delta g_{1_{y}}^{C_{\text{exp}}}(\vec{r}) \\
	\Delta g_{2_{x}}^{C_{\text{exp}}}(\vec{r}) & \Delta g_{2_{y}}^{C_{\text{exp}}}(\vec{r})
	\end{bmatrix} = \begin{bmatrix}
	0 & 0 \\
	0 & 0
	\end{bmatrix} $
	\end{itemize}
\item Expected output:
	\begin{itemize}
	\item  $\forall \vec{r} \in \mathbb{I}, \ \varepsilon^{\text{exp}}(\vec{r}) = \begin{bmatrix}
	\varepsilon_{\mathit{xx}^{\text{exp}}}(\vec{r}) & \varepsilon_{\mathit{xy}^{\text{exp}}}(\vec{r}) \\
	\varepsilon_{\mathit{xy}^{\text{exp}}}(\vec{r}) & \varepsilon_{\mathit{yy^{\text{exp}}}}(\vec{r})
	\end{bmatrix} = \begin{bmatrix}
	0 & 0 \\
	0 & 0
	\end{bmatrix} $
	\item $\forall \vec{r} \in \mathbb{I}, \  \omega^{\text{exp}}(\vec{r}) = \begin{bmatrix}
	0 & \omega_{\mathit{xy}^{\text{exp}}}(\vec{r}) \\
	-\omega_{\mathit{xy}^{\text{exp}}}(\vec{r}) & 0
	\end{bmatrix} = \begin{bmatrix}
	0 & 0 \\
	0 & 0
	\end{bmatrix} $
	\end{itemize}
\end{itemize}
\end{Test}

\begin{Test}\normalfont\underline{Test-known-constant-2D-strain}
\label{T_known-constant-2D-strain}
\begin{itemize}
\item Type: Functional, Dynamical, Automated
\item Initial State: N/A
\item Input:
\begin{itemize}
	\item $G_{\text{uns}}^{\text{exp}} =
	\begin{bmatrix}
	g_{1_{{\text{uns}}_x}}^{C_{\text{exp}}} & g_{2_{{\text{uns}}_x}}^{C_{\text{exp}}} \\
	g_{2_{{\text{uns}}_x}}^{C_{\text{exp}}} & g_{2_{{\text{uns}}_y}}^{C_{\text{exp}}}
	\end{bmatrix} =\begin{bmatrix}
	1 & 0 \\
	0 & 1
	\end{bmatrix}$
	\item Various $\Delta G^{\text{exp}}(\vec{r}) =
	\begin{bmatrix}
	\Delta g_{1_{x}}^{C_{\text{exp}}}(\vec{r}) & \Delta g_{1_{y}}^{C_{\text{exp}}}(\vec{r}) \\
	\Delta g_{2_{x}}^{C_{\text{exp}}}(\vec{r}) & \Delta g_{2_{y}}^{C_{\text{exp}}}(\vec{r})
	\end{bmatrix}$
		\begin{itemize}
		\item Uniaxial strain along $\vec{u_x}$, $\Delta G^{\text{exp}}(\vec{r}) = \begin{bmatrix}
	0.1 & 0 \\
	0 & 0
	\end{bmatrix} $
		\item Uniaxial strain along $\vec{u_y}$, $\Delta G^{\text{exp}}(\vec{r}) = \begin{bmatrix}
	0 & 0 \\
	0 & 0.1
	\end{bmatrix} $
		\item Pure shear strain, $\Delta G^{\text{exp}}(\vec{r}) = \begin{bmatrix}
	0 & 0.1 \\
	0.1 & 0
	\end{bmatrix} $
		\item Pure rotation, $\Delta G^{\text{exp}}(\vec{r}) = \begin{bmatrix}
	0 & 0.1 \\
	-0.1 & 0
	\end{bmatrix} $
		\end{itemize}

\end{itemize}
\item Expected output:
	\begin{itemize}
	\item Uniaxial strain along $\vec{u_x}$,\\
	$\begin{cases}
	\forall \vec{r} \in \mathbb{I}, \ \varepsilon^{\text{exp}}(\vec{r}) = \begin{bmatrix}
	-\frac{1}{11} & 0 \\
	0 & 0
	\end{bmatrix}  \\
	\forall \vec{r} \in \mathbb{I}, \  \omega^{\text{exp}}(\vec{r}) = \begin{bmatrix}
	0 & 0 \\
	0 & 0
	\end{bmatrix}
	\end{cases}$
	\item Uniaxial strain along $\vec{u_y}$,\\
	$\begin{cases}
	\forall \vec{r} \in \mathbb{I}, \ \varepsilon^{\text{exp}}(\vec{r}) = \begin{bmatrix}
	0 & 0 \\
	0 & -\frac{1}{11}
	\end{bmatrix}  \\
	\forall \vec{r} \in \mathbb{I}, \  \omega^{\text{exp}}(\vec{r}) = \begin{bmatrix}
	0 & 0 \\
	0 & 0
	\end{bmatrix}
	\end{cases}$
	\item Pure shear strain,\\
	$\begin{cases}
	\forall \vec{r} \in \mathbb{I}, \ \varepsilon^{\text{exp}}(\vec{r}) = \begin{bmatrix}
	0 & -\frac{1}{11} \\
	-\frac{1}{11} & 0
	\end{bmatrix}  \\
	\forall \vec{r} \in \mathbb{I}, \  \omega^{\text{exp}}(\vec{r}) = \begin{bmatrix}
	0 & 0 \\
	0 & 0
	\end{bmatrix}
	\end{cases}$
	\item Pure rotation,\\
	$\begin{cases}
	\forall \vec{r} \in \mathbb{I}, \ \varepsilon^{\text{exp}}(\vec{r}) = \begin{bmatrix}
	0 & 0 \\
	0 & 0
	\end{bmatrix}  \\
	\forall \vec{r} \in \mathbb{I}, \  \omega^{\text{exp}}(\vec{r}) = \begin{bmatrix}
	0 & \frac{1}{11} \\
	-\frac{1}{11} & 0
	\end{bmatrix}
	\end{cases}$
	\end{itemize}

\item Output:  ${\varepsilon^{\text{exp}}(\vec{r})}^{t}-\varepsilon^{\text{exp}}(\vec{r}), {\omega^{\text{exp}}(\vec{r})}^{t}- \omega^{\text{exp}}(\vec{r})$  and test them to be 0:
\end{itemize}
\end{Test}

\begin{Test}\normalfont\underline{Test-Improper-$\Delta G^{\text{exp}}$}
\label{T_Bad-G_ref}
\begin{itemize}
\item Type: Functional, Dynamical, Automated
\item Initial State: Whatever $\Delta G^{\text{exp}}(\vec{r})$
\item Input:
\begin{itemize}
	\item $G_{\text{uns}}^{\text{exp}} =
	\begin{bmatrix}
	g_{1_{{\text{uns}}_x}}^{C_{\text{exp}}} & g_{2_{{\text{uns}}_x}}^{C_{\text{exp}}} \\
	g_{2_{{\text{uns}}_x}}^{C_{\text{exp}}} & g_{2_{{\text{uns}}_y}}^{C_{\text{exp}}}
	\end{bmatrix} =\begin{bmatrix}
	1 & 1 \\
	1 & 1
	\end{bmatrix} $
	\end{itemize}
\item Output:
Error message $Err_{G_{\text{uns}}^{\text{exp}}}$ should match:
\enquote{Matrix $G_{\text{uns}}^{\text{exp}}$ non invertible, the
  crystalline wave vectors are linked (collinear). Please choose another
combination of crystalline wave vectors.}

\end{itemize}
\end{Test}

\begin{Test}\normalfont\underline{Test-Improper-$G_{\text{uns}}^{\text{exp}}$}
\label{T_Bad-Delta-G}
\begin{itemize}
\item Type: Functional, Dynamical, Automated
\item Initial State: N/A
\item Input:
\begin{itemize}
	\item $\Delta G^{\text{exp}}+G_{\text{uns}}^{\text{exp}} =
	\begin{bmatrix}
	1 & 1 \\
	1 & 1
	\end{bmatrix} $
	\end{itemize}
\item Output:
Error message $Err_{\Delta G^{\text{exp}}}$ should match: \enquote{Matrix $G^{\text{exp}}$ non invertible. If  $G_{\text{uns}}^{\text{exp}}$ is invertible, something is probably wrong with $\Delta G^{\text{exp}}$.}
\end{itemize}
\end{Test}

\section{Traceability: Tests Cases vs Requirements}

\noindent
\begin{longtable}{l p{10cm}}
\toprule
\textbf{Requirement} & \textbf{Test}\\
\midrule
\endhead
\cref{ext:R_1} & Not covered in the Test Plan (GUI)\\
\cref{ext:R_2} & \cref{Test-Existence-SMH}, \cref{Test-Format-SMH}, \cref{Test-read-dm3-format}, \cref{Test-Existence-pixel}, \cref{Test-Format-pixel}, \cref{Test-Format-Reference}, \cref{Test-Existence-Reference} \\
\cref{ext:R_3} & \cref{aliasing-frequency-simple}\\
\cref{ext:R_4} & Not covered in the Test Plan (GUI)\\
\cref{ext:R_5} & Not covered in the Test Plan (GUI)\\
\cref{ext:R_6} & \cref{Test-Existence-Mask}, \cref{Test-Format-Mask}, \cref{Test-Position-Radius-Mask}\\
\cref{ext:R_7} & \cref{T_Phase-Extraction-No-Strain}, \cref{T_Phase-Extraction-Known-Strain}, \cref{T_Phase-Extraction-Mask} \\
\cref{ext:R_8} & Not covered in the Test Plan (GUI)\\
\cref{ext:R_9} & \cref{Test-Existence-U}, \cref{Test-Format-U}, \cref{Test-Position-U} \\
\cref{ext:R_10} & \cref{T_constant-delta-g}, \cref{T_constant-delta-g-with-noise}, \cref{T_varying-delta-g}\\
\cref{ext:R_11} & Not covered in the Test Plan (GUI)\\
\cref{ext:R_12} & \cref{Test-Basic-affine-transformation}, \cref{Test-Error-q-affine-transformation}, \cref{Test-Error-pixel-affine-transformation} \\
\cref{ext:R_13} & \cref{T_No-2D-strain}, \cref{T_known-constant-2D-strain}, \cref{T_Bad-G_ref}, \cref{T_Bad-Delta-G}\\
\cref{ext:R_14} & Not covered in the Test Plan (GUI)\\
\cref{ext:NR_1} & \cref{black_box_STEM_Moire_GPA-env}\\
\cref{ext:NR_2} & \cref{stress_test_STEM_Moire_GPA}\\
\cref{ext:NR_3} & Not covered in the Test Plan (GUI)\\
\cref{ext:NR_4} & Not covered in the Test Plan (GUI)\\
\cref{ext:NR_5} & Not covered in the Test Plan (Not a priority)\\
\cref{ext:NR_6} & Not covered in the Test Plan (Not a priority)\\
\cref{ext:NR_7} & Not covered in the Test Plan (Not a priority)\\
\cref{ext:NR_8} & \cref{black_box_STEM_Moire_GPA}\\
\cref{ext:NR_9} & \cref{black_box_STEM_Moire_GPA}\\
\bottomrule
\caption{\textbf{Traceability Table: Test vs Requirements}}
\label{table_traceability}
\end{longtable}

\section{Prioritization}
The resources available for the test phase of \progname{} are relatively limited and all aspects of the test plan won't be covered. Therefore, the test tasks have been prioritized by focusing first on the system tests targeting the functional requirements, then the unit tests testing the functional requirements and finally the system tests assessing the non functional requirements. The details of the prioritization are described in the following table.

\noindent
\begin{longtable}{l p{3cm} p{7cm}}

\toprule
\textbf{Priority} & \textbf{Requirement tested} & \textbf{Associated Tests} \\
\midrule
\endhead
P0 & \cref{ext:R_3}, \cref{ext:R_7}, \cref{ext:R_10} & \cref{aliasing-frequency-simple}, \cref{T_Phase-Extraction-No-Strain}, \cref{T_Phase-Extraction-Known-Strain}, \cref{T_constant-delta-g}, \cref{T_constant-delta-g-with-noise} \\
P1 & \cref{ext:R_12}, \cref{ext:R_13} & \cref{Test-Basic-affine-transformation}, \cref{Test-Error-pixel-affine-transformation}, \cref{Test-Error-q-affine-transformation}, \cref{T_No-2D-strain}, \cref{T_known-constant-2D-strain}, \cref{T_Bad-G_ref}, \cref{T_Bad-Delta-G}\\
P2 & \cref{ext:R_7}, \cref{ext:R_10} & \cref{T_Phase-Extraction-Mask}, \cref{T_varying-delta-g} \\
P3 & \cref{ext:R_2}, \cref{ext:R_6}, \cref{ext:R_9} & \cref{Test-Existence-SMH}, \cref{Test-Format-SMH}, \cref{Test-read-dm3-format}, \cref{Test-Existence-pixel}, \cref{Test-Format-pixel}, \cref{Test-Format-Reference}, \cref{Test-Existence-Reference}, \cref{Test-Existence-Mask}, \cref{Test-Format-Mask}, \cref{Test-Position-Radius-Mask}, \cref{Test-Existence-U}, \cref{Test-Format-U}, \cref{Test-Position-U} \\
P4 & \cref{ext:NR_8}, \cref{ext:NR_9} & \cref{black_box_STEM_Moire_GPA} \\
P5 & \cref{ext:NR_1}, \cref{ext:NR_2} & \cref{black_box_STEM_Moire_GPA-env}, \cref{stress_test_STEM_Moire_GPA} \\
P6 & GUI and other lower priority tasks & N/A \\
\bottomrule
\caption{\textbf{Priority Table}}
\label{priority_table}
\end{longtable}

\bibliography{TestPlan_biblio}
\bibliographystyle{ieeetr}

\newpage

\end{document}


\subsection{Branching} % (fold)
\label{sub:branching}

There are two main ways of controlling the sequence of actions in a program. The first of these is called \textbf{branching}, or \textbf{selection}. Branching allows you to get the computer to take one of a number of paths based on the value of a \emph{condition}.

\begin{figure}[h]
   \centering
   \includegraphics[width=0.8\textwidth]{./topics/control-flow/diagrams/Branching}
   \caption{Branching commands the computer to take one of a number of possible paths}
   \label{fig:branching}
\end{figure}

\mynote{
\begin{itemize}
  \item Branching is a kind of \textbf{action}. You can command the computer to take of a number of paths.
  \item A branch has a \textbf{condition} that is evaluated, and based on the condition the computer takes one path.
  \item The branch is the act of choosing the path, when its command is performed the computer evaluates the condition and then moves to the instructions in the indicated path.
  \item Languages usually offer two kinds of branching statements:
  \begin{itemize}
    \item \nameref{sub:if_statement} to select between two paths based on a Boolean expression.
    \item \nameref{sub:case_statement}  to select a path based on an ordinal\footnote{Integers and Characters are ordinal values. Ordinal values have a defined sequence, so it is possible to say which value comes next in the sequence. Integers are Ordinal as you can say that the number after 1 is 2. Real numbers are not ordinal as you cannot say which value comes next in the sequence.} value.
  \end{itemize}
  \item The Branch will have one entry point, and one exit point. This feature allows you to combine statements together like building blocks. This idea comes from the principles of \textbf{Structured Programming}, where each component in the code should have a single entry and exit point.
\end{itemize}
}

% subsection branching (end)